Skip to content

Commit

Permalink
removes unnecessary units from the knowledge base (#1427)
Browse files Browse the repository at this point in the history
The Project.create function were creating a separate unit in order to
compute a target from the available image specification. It was also
using Toplevel.eval for that, which computes all properties of an
object. As a result, we had all expensive unit-based computations
triggered twice per each project.

An ideal solution would be to get rid of this call, but, as an
immediate remediation, we can just use the same compilation unit as
for the main program and compute only the target slot.
  • Loading branch information
ivg authored Feb 11, 2022
1 parent 9d215e5 commit 6dac0a9
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/bap/bap_project.ml
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,17 @@ module State = struct
KB.Domain.flat ~empty ~equal "disassembly" ~inspect

module Toplevel = struct
let compute_target spec =
Toplevel.eval Theory.Unit.target @@begin
KB.Object.create Theory.Unit.cls >>= fun unit ->
KB.provide Image.Spec.slot unit spec >>| fun () ->
unit
end
let compute_target ?file spec : Theory.target =
let result = Toplevel.var "target" in
let create_unit = match file with
| None | Some "" -> KB.Object.create Theory.Unit.cls
| Some file -> Theory.Unit.for_file file in
Toplevel.put result begin
let* unit = create_unit in
KB.provide Image.Spec.slot unit spec >>= fun () ->
KB.collect Theory.Unit.target unit
end;
Toplevel.get result

let run spec target ~code ~memory file k =
let result = Toplevel.var "disassembly-result" in
Expand Down Expand Up @@ -204,7 +209,7 @@ module Input = struct
| #Arch.unknown -> Ogre.Doc.empty
| arch -> Image.Spec.from_arch arch in {
arch; file; code; data; finish;
target = State.Toplevel.compute_target spec;
target = State.Toplevel.compute_target ~file spec;
memory = union_memory code data;
spec;
}
Expand All @@ -222,8 +227,8 @@ module Input = struct
| None -> false
| Some s -> not (Image.Segment.is_executable s)

let compute_target ?(target=Theory.Target.unknown) spec =
let target' = State.Toplevel.compute_target spec in
let compute_target ?file ?(target=Theory.Target.unknown) spec =
let target' = State.Toplevel.compute_target ?file spec in
match (Theory.Target.order target target' : KB.Order.partial) with
| LT | EQ -> target'
| GT -> target
Expand All @@ -240,7 +245,7 @@ module Input = struct
file;
finish;
spec = Image.spec img;
target = compute_target ?target (Image.spec img);
target = compute_target ~file ?target (Image.spec img);
}

let of_image ?target ?loader filename =
Expand Down

0 comments on commit 6dac0a9

Please sign in to comment.