Skip to content
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
168 changes: 92 additions & 76 deletions CodeHawk/CHC/cchanalyze/cCHCallTranslator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,63 @@ let memory_variables_in_region
env#get_memory_variables


(* The argument written by a block-write side effect on parameter [n] of a
library summary, or None if the write has no effect on the program's
memory: the call has no such argument (an optional parameter of a
variadic function, e.g., ioctl or fcntl), the argument is a null pointer
constant (e.g., strtol (s, NULL, 10), time (NULL)), or it is not a pointer
(fcntl (fd, F_SETFL, flags)); casts are stripped for the latter two tests.
Without this filter the dereference of such an argument translates to a
temporary and all memory variables are abstracted. *)
let block_write_arg
(env: c_environment_int) (args: exp list) (n: int): exp option =
let rec strip_casts e = match e with CastE (_, e') -> strip_casts e' | _ -> e in
match List.nth_opt args (n - 1) with
| None -> None
| Some arg ->
match strip_casts arg with
| Const (CInt (i64, _, _)) when Int64.compare i64 Int64.zero = 0 -> None
| e ->
match type_of_exp env#get_fdecls e with
| Ok t ->
(match fenv#get_type_unrolled t with
| TPtr _ | TArray _ -> Some arg
| _ -> None)
| Error _ -> Some arg


(* Memory variables of the object denoted by region symbol [s] (a pointer
value in the symbolic pointer-set domain): all memory variables of the
stack or global variable that is the base of the region (the offset is not
known). Null and external regions (pointers to external memory, covered by
the assumptions on external memory) and string literals have none. For a
symbol that is not a known region, all stack memory variables are
returned. *)
let memory_variables_of_region_sym
(env: c_environment_int) (s: symbol_t): variable_t list =
let memregmgr = env#get_variable_manager#memregmgr in
let stack_memory_variables () =
List.filter (fun mv ->
(env#get_memory_reference mv)#is_stack_reference)
env#get_memory_variables in
let region =
try Some (memregmgr#get_memory_region s#getSeqNumber)
with CCHFailure _ -> None in
match region with
| None -> stack_memory_variables ()
| Some r ->
match r#get_memory_base with
| CNull _ | CBaseVar _ | CStringLiteral _ -> []
| CStackAddress v | CGlobalAddress v ->
let seqnr = v#getName#getSeqNumber in
List.filter (fun mv ->
match (env#get_memory_reference mv)#get_base with
| CStackAddress w | CGlobalAddress w ->
w#getName#getSeqNumber = seqnr
| _ -> false) env#get_memory_variables
| CUninterpreted _ -> stack_memory_variables ()


(* Memory variables that a callee may write through argument [arg] when no
side effects are known for that argument: the memory variables
of the region addressed by the argument, that is, the addressed
Expand Down Expand Up @@ -265,6 +322,9 @@ let arg_written_memory_variables
| XVar v
| XOp ((XPlus | XMinus), [XVar v; _]) when env#is_memory_address v ->
region_of_memory_address v
| XConst (SymSet syms) ->
(* symbolic pointer-set domain: the regions the pointer may point to *)
List.concat_map (memory_variables_of_region_sym env) syms
| XConst _ -> []
| XVar v
| XOp ((XPlus | XMinus), [XVar v; _])
Expand Down Expand Up @@ -886,6 +946,8 @@ object (self)
let assignments =
List.fold_left (fun acc (se, _) ->
match se with
| XBlockWrite (ArgValue (ParFormal n, ArgNoOffset), _)
when Option.is_none (block_write_arg env fnargs n) -> acc
| XBlockWrite (ArgValue (ParFormal n, ArgNoOffset), _) ->
let arg = List.nth fnargs (n - 1) in
let lhs =
Expand Down Expand Up @@ -1366,6 +1428,8 @@ object (self)
let assignments =
List.fold_left (fun acc (se, _) ->
match se with
| XBlockWrite (ArgValue (ParFormal n, ArgNoOffset), _)
when Option.is_none (block_write_arg env fnargs n) -> acc
| XBlockWrite (ArgValue (ParFormal n,ArgNoOffset), _) ->
let arg = List.nth fnargs (n - 1) in
let lhs =
Expand Down Expand Up @@ -1830,84 +1894,18 @@ object (self)
([(List.map (fun xsitevar ->
make_c_cmd (ASSIGN_SYM (xsitevar, SYM sitesym))) xsitevars)]
@ [[make_c_cmd SKIP]]) in
let rec compose_offset base offset =
match base with
| NoOffset -> offset
| Field (fuse, NoOffset) -> Field (fuse,offset)
| Field (fuse, foffset) -> Field (fuse, compose_offset foffset offset)
| Index _ ->
begin
ch_error_log#add
"compose offset"
(LBLOCK [
STR "base: ";
offset_to_pretty base;
STR "; offset: ";
offset_to_pretty offset]);
NoOffset
end in
let rec cancel_se_struct arg baseoffset comp =
List.fold_left (fun acc f ->
let foffset = Field ((f.fname,f.fckey),NoOffset) in
let offset = compose_offset baseoffset foffset in
let lhs = exp_translator#translate_lhs context (Mem arg,offset) in
if lhs#isTmp then
let memoryvars = env#get_memory_variables in
let _ =
log_diagnostics_result
~tag:"abstract memory variables"
~msg:env#get_functionname
__FILE__ __LINE__
(List.map (fun v -> p2s v#toPretty) memoryvars) in
[ make_c_cmd (ABSTRACT_VARS memoryvars)]
else
let cancel = make_c_cmd (ABSTRACT_VARS [lhs]) in
let _ =
log_diagnostics_result
~tag:"abstract memory variables"
~msg:env#get_functionname
__FILE__ __LINE__
[p2s lhs#toPretty] in
let subcancels =
match fenv#get_type_unrolled f.ftype with
| TComp (ckey,_) ->
let comp = fenv#get_comp ckey in
cancel_se_struct arg offset comp
| _ -> [] in
(cancel :: subcancels) @ acc) [] comp.cfields in
let seeffects =
List.concat
(List.map (fun (se,_) ->
match se with
| XBlockWrite (ArgValue (ParFormal n, ArgNoOffset), _) ->
let arg = List.nth args (n - 1) in
let lhs = exp_translator#translate_lhs context (Mem arg,NoOffset) in
if lhs#isTmp then
let memoryvars = env#get_memory_variables in
let _ =
log_diagnostics_result
~tag:"abstract memory variables"
~msg:env#get_functionname
__FILE__ __LINE__
(List.map (fun v -> p2s v#toPretty) memoryvars) in
[make_c_cmd (ABSTRACT_VARS memoryvars)]
else
let ty = fenv#get_type_unrolled (env#get_variable_type lhs) in
let cancel = make_c_cmd (ABSTRACT_VARS [lhs]) in
let _ =
log_diagnostics_result
~tag:"abstract memory variables"
~msg:env#get_functionname
__FILE__ __LINE__
[p2s lhs#toPretty] in
let subcancels =
match ty with
| TComp (ckey, _) ->
let comp = fenv#get_comp ckey in
cancel_se_struct arg NoOffset comp
| _ ->
[] in
cancel :: subcancels
| XBlockWrite _ ->
(* Initialization domain: a block write cannot make a location
uninitialized (a location written is initialized, one not
written keeps its state), so nothing is abstracted; in
particular the initialization recorded by an
initializes-range side effect of the same call (memset,
memcpy, read) is kept. *)
[]

| XFormattedInput (ArgValue (ParFormal n, ArgNoOffset)) ->
let (assignments, _) =
Expand Down Expand Up @@ -2115,7 +2113,9 @@ object (self)
{ op_name = new symbol_t ~atts:[p2s (exp_to_pretty f)]
"indirect call";
op_args = [] }) in
Ok [callop]
(* callee unknown (the symbolic domains do not resolve function
pointers): lhs, argument regions and globals are abstracted *)
Ok (callop :: (unknown_call_abstracts env exp_translator context lhs args))

method private get_post_assigns
(fname:string)
Expand Down Expand Up @@ -2307,10 +2307,15 @@ object (self)
List.concat
(List.map (fun (se,_) ->
match se with
| XBlockWrite (ArgValue (ParFormal n, ArgNoOffset), _)
when Option.is_none (block_write_arg env args n) -> []
| XBlockWrite (ArgValue (ParFormal n, ArgNoOffset), _) ->
let arg = List.nth args (n - 1) in
let regionabstract =
arg_written_abstract env exp_translator context arg in
let lhs =
exp_translator#translate_lhs context (Mem arg, NoOffset) in
regionabstract @
if lhs#isTmp then
let memoryvars = env#get_memory_variables in
let _ =
Expand Down Expand Up @@ -2357,7 +2362,18 @@ object (self)
effects

| _ -> []) sideeffects) in
seeffects
(* Without a library summary, the side effects (callsite
post-assumptions, if any) need not cover all arguments: the objects
addressed by the arguments are abstracted first, as in the numerical
call translator. Globals are abstracted unless protected by a
contract (see global_abstracts). *)
let argabstracts =
match get_function_summary fname with
| Some _ -> []
| None -> arg_written_abstracts env exp_translator context args in
argabstracts
@ seeffects
@ (global_abstracts env (Some fname) context args)
end


Expand Down
Loading
Loading