Skip to content

Commit 6dac0a9

Browse files
authored
removes unnecessary units from the knowledge base (#1427)
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.
1 parent 9d215e5 commit 6dac0a9

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

lib/bap/bap_project.ml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,17 @@ module State = struct
123123
KB.Domain.flat ~empty ~equal "disassembly" ~inspect
124124

125125
module Toplevel = struct
126-
let compute_target spec =
127-
Toplevel.eval Theory.Unit.target @@begin
128-
KB.Object.create Theory.Unit.cls >>= fun unit ->
129-
KB.provide Image.Spec.slot unit spec >>| fun () ->
130-
unit
131-
end
126+
let compute_target ?file spec : Theory.target =
127+
let result = Toplevel.var "target" in
128+
let create_unit = match file with
129+
| None | Some "" -> KB.Object.create Theory.Unit.cls
130+
| Some file -> Theory.Unit.for_file file in
131+
Toplevel.put result begin
132+
let* unit = create_unit in
133+
KB.provide Image.Spec.slot unit spec >>= fun () ->
134+
KB.collect Theory.Unit.target unit
135+
end;
136+
Toplevel.get result
132137

133138
let run spec target ~code ~memory file k =
134139
let result = Toplevel.var "disassembly-result" in
@@ -204,7 +209,7 @@ module Input = struct
204209
| #Arch.unknown -> Ogre.Doc.empty
205210
| arch -> Image.Spec.from_arch arch in {
206211
arch; file; code; data; finish;
207-
target = State.Toplevel.compute_target spec;
212+
target = State.Toplevel.compute_target ~file spec;
208213
memory = union_memory code data;
209214
spec;
210215
}
@@ -222,8 +227,8 @@ module Input = struct
222227
| None -> false
223228
| Some s -> not (Image.Segment.is_executable s)
224229

225-
let compute_target ?(target=Theory.Target.unknown) spec =
226-
let target' = State.Toplevel.compute_target spec in
230+
let compute_target ?file ?(target=Theory.Target.unknown) spec =
231+
let target' = State.Toplevel.compute_target ?file spec in
227232
match (Theory.Target.order target target' : KB.Order.partial) with
228233
| LT | EQ -> target'
229234
| GT -> target
@@ -240,7 +245,7 @@ module Input = struct
240245
file;
241246
finish;
242247
spec = Image.spec img;
243-
target = compute_target ?target (Image.spec img);
248+
target = compute_target ~file ?target (Image.spec img);
244249
}
245250

246251
let of_image ?target ?loader filename =

0 commit comments

Comments
 (0)