open Core_kernel.Std
open Bap.Std
open Symbol_utils       

let name = "CWE467"
let version = "0.1"

let get_pointer_size arch =
  Size.in_bytes @@ Arch.addr_size arch    

let check_input_is_pointer_size proj prog sub blk jmp tid_map symbols =
  Seq.iter (Term.enum def_t blk) ~f:(fun d ->  match Exp.eval @@ Def.rhs d with
      | Imm w ->
        begin
        try
          if get_pointer_size (Project.arch proj) = (Word.to_int_exn w) then
            Log_utils.warn "[%s] {%s} (Use of sizeof on a Pointer Type) sizeof on pointer at %s (%s)."
              name
              version
              (Address_translation.translate_tid_to_assembler_address_string (Term.tid blk) tid_map)
              (Symbol_utils.get_symbol_name_from_jmp jmp symbols)
        with _ -> Log_utils.error "Caught exception in module [CWE467]."
      end
      | _ -> ())

                      
let check_cwe prog proj tid_map symbol_names =
  let symbols = Symbol_utils.build_symbols symbol_names prog in 
  let calls = call_finder#run prog [] in
  let relevant_calls = filter_calls_to_symbols calls symbols in
  check_calls relevant_calls prog proj tid_map symbols check_input_is_pointer_size