Skip to content

Commit

Permalink
fixes two bugs in the Memory module (#1272)
Browse files Browse the repository at this point in the history
The first bug was breaking the raw loader at least. It bug was
introduced in #1178 during the `Memory.view` optimization. A check was
missed that allowed for creation of invalid views. The invalid views
later were caught with an assertion check, (easily reproduced with
`bap /bin/ls --loader=raw`), e.g.,

```
("Assert_failure bap_memory.ml:72:2"
  "Raised at file \"bap_memory.ml\", line 72, characters 2-31\
 \nCalled from file \"bap_memory.ml\", line 196, characters 2-18\
 \nCalled from file \"bap_memory.ml\", line 410, characters 26-52\
 \nCalled from file \"bap_trie.ml\", line 46, characters 34-53\
 \nCalled from file \"bap_trie.ml\", line 62, characters 4-130\
 \nCalled from file \"bap_byteweight.ml\", line 78, characters 12-39\
 \nCalled from file \"bap_byteweight.ml\", line 129, characters 12-39\
 \nCalled from file \"byteweight_main.ml\", line 50, characters 44-56\
 \nCalled from file \"src/sequence.ml\", line 123, characters 29-36\
 \nCalled from file \"byteweight_main.ml\" (inlined), line 49, characters 4-116\
 \nCalled from file \"byteweight_main.ml\", line 48, characters 4-146\
...
```

The second bug was probably all the time in the library and concerns
`Memory.find_map` and `Memory.find_if`, which are talking an optional
`word_size` parameter that was ignored and functions were always
iterating over bytes.
  • Loading branch information
ivg authored Feb 22, 2021
1 parent 155a3d1 commit b7fd489
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/bap_image/bap_memory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ module Input = struct
let int256 = read `r256
end

let view_exn ?(word_size=`r8) ?from ?words t =
let view_exn ?(word_size=`r8) ?from ?words t =
let amin = Option.value from ~default:(min_addr t) in
let amax = match words with
| None -> max_addr t
Expand All @@ -225,7 +225,7 @@ let view_exn ?(word_size=`r8) ?from ?words t =
if off >= t.off && off < t.off + t.size then match size with
| 0 -> invalid_arg "empty view"
| 1 -> make_byte t amin off
| n when n <= t.size ->
| n when n <= t.size && off + size <= t.off + t.size ->
{ t with size; data = t.data; addr = amin; off}
| _ -> invalid_arg "out-of-bounds"
else invalid_arg "out-of-bounds"
Expand Down Expand Up @@ -354,12 +354,12 @@ module Make_iterators( M : Monad.S) = struct

let find_map ?word_size t ~f =
with_return (fun s ->
iteri t ~f:(fun a w -> f a w >>= function
iteri ?word_size t ~f:(fun a w -> f a w >>= function
| None -> return ()
| some -> s.return (return some)) >>| fun () -> None)

let find_if ?word_size t ~f =
find_map t ~f:(fun a w -> f a w >>| function
find_map ?word_size t ~f:(fun a w -> f a w >>| function
| true -> Some w
| false -> None)
end
Expand Down

0 comments on commit b7fd489

Please sign in to comment.