Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lift_insn.c yields an empty sequence of instructions #19

Closed
contificate opened this issue Jul 21, 2021 · 3 comments · Fixed by #20
Closed

lift_insn.c yields an empty sequence of instructions #19

contificate opened this issue Jul 21, 2021 · 3 comments · Fixed by #20

Comments

@contificate
Copy link

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
Copy link
Member

ivg commented Jul 21, 2021

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
Copy link
Author

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 added a commit that referenced this issue Jul 27, 2021
resolves #19

The example was buggy as the size of the pointer was specified
incorrectly. It was acceptable before but is not longer tolerated
after we enabled [interworking][1] (several architectures in the same
binary).

[1]: BinaryAnalysisPlatform/bap#1226
@ivg
Copy link
Member

ivg commented Jul 27, 2021

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

@ivg ivg closed this as completed in #20 Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants