BinaryAnalysisPlatform / bap-bindings

C Bindings to BAP
MIT License
15 stars 9 forks source link

`lift_insn.c` yields an empty sequence of instructions #19

Closed contificate closed 3 years ago

contificate commented 3 years ago

I'm currently using BAP 2.3.0 and the only program from examples/ that doesn't work as expected is lift_insn.c.

I'm not really familiar with the overall BAP API so this could just be a API-versioning/usage thing that I'm unaware of.

If you insert this immediately after insns's definition, it will print empty = 1.

printf("empty = %d\n", bap_seq_is_empty((bap_seq_t*) insns));

As a result, running the compiled lift_insn.native executable only prints:

Will disassemble 17 bytes

Similarly, in my efforts to transliterate the example into OCaml, I also get the effect that the sequence is empty (in that nothing is printed):

open Bap.Std
open Core_kernel
open Result.Monad_infix

let code = "\x48\x8d\x00\x48\x83\xec\x08\x48\x83\xc4\x08\xe8\x47\xee\xff\xff" 
let buff = Bigstring.of_string code
let base = Word.of_int ~width:32 0x80000
let (>>) f g x = g (f x)

let _ =
  Memory.create LittleEndian base buff
  >>= Disasm.of_mem `x86_64
  >>=
    (fun d ->
      Seq.iter
        (Disasm.insns d) ~f:(snd >> Insn.asm >> print_endline);
      Ok ())

I'm unsure if what I've done above is a faithful(-enough) translation.

Any assistance or clarification would be much appreciated. Thanks.

ivg commented 3 years ago

The translation is pretty faithful, but you have to initialize bap before using it, e.g., Bap_main.init

I can't say right now what is wrong with the C example, I am on vacation, typing from a phone, but if after you add the initializations the OCaml version still doesn't work, then the problem is with the installation of BAP

contificate commented 3 years ago

The translation is pretty faithful, but you have to initialize bap before using it, e.g., Bap_main.init

I can't say right now what is wrong with the C example, I am on vacation, typing from a phone, but if after you add the initializations the OCaml version still doesn't work, then the problem is with the installation of BAP

Thanks for the response (during your vacation, nonetheless!).

I added a call to Bap_main.init with ~requires:["disassembler"] and the OCaml snippet still doesn't work as expected. The dune libraries stanza is just (libraries findlib.dynload bap) - unsure if that's sufficient.

Following your advice, I did an entire clean installation of BAP (from opam) on a fresh 4.09.0 switch and still have the exact same problems. The installed bap utility works just fine, but lift_insns.native does not (nor does my OCaml snippet from OP, after adding an initialisation call - which does not error). Curiously, all the other programs from examples/ work.

I don't expect a swift response as I don't want to distract you from your vacation. I'll continue to play around with it in the meantime.

Thanks.

ivg commented 3 years ago

I am back :) Thanks to your failfull translation to OCaml it was easy to find the problem. The example was always buggy but up until 2.2.0 the bug was tolerated by bap. Thanks for reporting it and translating the example to OCaml!

Besides, here is the OCaml code with proper initialization, just for the sake of completeness,

open Bap.Std
open Core_kernel
open Result.Monad_infix

let code = "\x48\x8d\x00\x48\x83\xec\x08\x48\x83\xc4\x08\xe8\x47\xee\xff\xff"
let buff = Bigstring.of_string code
let base = Word.of_int ~width:64 0x80000
let (>>) f g x = g (f x)

let run_example () =
  Memory.create LittleEndian base buff >>=
  Disasm.of_mem `x86_64 >>| fun d ->
  Seq.iter (Disasm.insns d) ~f:(fun (mem,insn) ->
      Format.printf "%a: %s@\n"
        Addr.pp (Memory.min_addr mem)
        (Insn.asm insn))

let () = match Bap_main.init () with
  | Error err ->
    Format.eprintf "Failed to initialize bap: %a@."
      Bap_main.Extension.Error.pp err
  | Ok () -> match run_example () with
    | Ok () ->
      Format.printf "Done@.";
      ()
    | Error err ->
      Format.eprintf "The example failed: %a@." Error.pp err