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

protects symbolic executor from segfaults when setting memory inputs #1246

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions lib/bap_primus/bap_primus_memory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ let find_layer addr = List.find ~f:(function
| {mem=Dynamic mem} -> inside mem addr
| {mem=Static mem} -> Memory.contains mem addr)

let is_mapped addr {layers} = Option.is_some @@ find_layer addr layers
let is_mapped addr {layers; values} =
Map.mem values addr ||
Option.is_some @@ find_layer addr layers

let empty_state = {
values = Addr.Map.empty;
Expand Down Expand Up @@ -259,27 +261,31 @@ module Make(Machine : Machine) = struct
put_curr {layers; values} >>| fun () ->
Map.find_exn values addr

let read addr {values;layers} = match find_layer addr layers with
| None -> pagefault addr
| Some layer -> match Map.find values addr with
| Some v -> Machine.return v
| None ->
let read addr {values;layers} = match Map.find values addr with
| Some v -> Machine.return v
| None -> match find_layer addr layers with
| None -> pagefault addr
| Some layer ->
memory >>= fun {size} ->
match layer.mem with
| Static mem -> Value.of_word (read_word mem addr size)
| Dynamic {value=g} ->
Generate.word g (Generator.width g) >>=
remembered {values; layers} addr

let write addr value {values;layers} =
match find_layer addr layers with
| None -> pagefault addr
| Some {perms={readonly=true}} -> pagefault addr
| Some _ -> Machine.return {
layers;
values = Map.set values ~key:addr ~data:value;
}
let set_value s addr value = {
s with
values = Map.set s.values ~key:addr ~data:value
}

let write addr value s =
if Map.mem s.values addr
then Machine.return @@ set_value s addr value
else match find_layer addr s.layers with
| None -> pagefault addr
| Some {perms={readonly=true}} -> pagefault addr
| Some _ ->
Machine.return @@ set_value s addr value

let add_layer layer t = {t with layers = layer :: t.layers}
let (++) = add_layer
Expand Down Expand Up @@ -313,7 +319,6 @@ module Make(Machine : Machine) = struct
initialize s.values lower upper f >>= fun values ->
put_curr {s with values}


let allocate
?readonly ?executable ?init ?generator base len =
add_region ()
Expand All @@ -338,15 +343,15 @@ module Make(Machine : Machine) = struct
s with values = Map.remove s.values addr
}


let load addr = get addr >>| Value.to_word
let store addr value = Value.of_word value >>= set addr

let is_mapped addr =
get_curr >>| is_mapped addr

let is_writable addr =
get_curr >>| fun {layers} ->
get_curr >>| fun {layers; values} ->
Map.mem values addr ||
find_layer addr layers |>
function Some {perms={readonly}} -> not readonly
| None -> false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ end = struct
let name = Primus.Memory.Descriptor.name bank in
Machine.Global.get memories >>= fun memories ->
match Map.find memories name with
| None -> Machine.return ()
| None -> allocate bank addr addr
| Some {lower; upper} -> allocate bank lower upper

let set input value = match input with
Expand Down