Skip to content

Commit 0a72636

Browse files
committed
CHC: add external state variable
1 parent c888f84 commit 0a72636

16 files changed

+286
-56
lines changed

CodeHawk/CHC/cchanalyze/cCHAnalysisTypes.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ object
102102
(bool * int * int) list -> typ -> variable_type_t -> variable_t
103103
method mk_augmentation_variable:
104104
string -> string -> int -> variable_type_t -> variable_t
105+
method mk_external_state_variable: string -> variable_type_t -> variable_t
105106
method mk_memory_variable: int -> offset -> variable_type_t -> variable_t
106107
method mk_stack_memory_variable:
107108
varinfo -> offset -> typ -> variable_type_t -> variable_t
@@ -274,6 +275,10 @@ class type orakel_int =
274275
object
275276

276277
method get_external_value : program_context_int -> xpr_t -> xpr_t option
278+
279+
method get_external_state_value:
280+
program_context_int -> variable_t -> xpr_t option
281+
277282
method get_regions: program_context_int -> xpr_t -> symbol_t list
278283

279284
end

CodeHawk/CHC/cchanalyze/cCHCallTranslator.ml

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,15 @@ object (self)
299299
let rvar = exp_translator#translate_lhs context lval in
300300
if rvar#isTmp then
301301
let memoryvars = env#get_memory_variables in
302+
let _ =
303+
chlog#add
304+
"abstract memory variables (call lhs)"
305+
(LBLOCK [
306+
STR "callee: ";
307+
STR fname;
308+
STR "; ";
309+
pretty_print_list
310+
memoryvars (fun v -> v#toPretty) "" ", " ""]) in
302311
[make_c_cmd (ABSTRACT_VARS memoryvars)]
303312
else
304313
let ty = fenv#get_type_unrolled (env#get_variable_type rvar) in
@@ -307,9 +316,25 @@ object (self)
307316
(* problem: rvar is not a registered memory variable, so env will
308317
return all memory variables, which will all be abstracted,
309318
causing loss of precision *)
310-
let memoryvars = env#get_memory_variables_with_base rvar in
319+
let optbasevar =
320+
self#get_external_post_value postconditions fnargs in
321+
let memoryvars =
322+
match optbasevar with
323+
| Some (XVar v) -> env#get_memory_variables_with_base v
324+
| _ -> env#get_memory_variables_with_base rvar in
311325
let fieldcode = make_c_cmd (ABSTRACT_VARS memoryvars) in
312-
let (rcode,rval) =
326+
let _ =
327+
chlog#add
328+
"abstract memory variables with base (call lhs)"
329+
(LBLOCK [
330+
STR "callee: ";
331+
STR fname;
332+
STR "; base: ";
333+
rvar#toPretty;
334+
STR "; ";
335+
pretty_print_list
336+
memoryvars (fun v -> v#toPretty) "[" ", " "]"]) in
337+
let (rcode, rval) =
313338
self#get_arg_post_value postconditions fnargs frVar returntype in
314339
let assign = make_c_cmd (ASSIGN_NUM (rvar, rval)) in
315340
let postassert =
@@ -397,16 +422,34 @@ object (self)
397422
(_f:exp)
398423
(_args:exp list) = []
399424

425+
method private get_external_post_value
426+
(postconditions:
427+
annotated_xpredicate_t list * annotated_xpredicate_t list)
428+
(_args: xpr_t list): xpr_t option =
429+
let get_external_pc_value (acc: xpr_t option) ((pc, _): annotated_xpredicate_t) =
430+
match acc with
431+
| Some _ -> acc
432+
| _ ->
433+
match pc with
434+
| XExternalStateValue (ReturnValue, ExternalState name) ->
435+
let lhs = env#mk_external_state_variable name NUM_VAR_TYPE in
436+
orakel#get_external_state_value context lhs
437+
| _ -> acc in
438+
match postconditions with
439+
| ([], _) -> None
440+
| (pl, _) -> List.fold_left get_external_pc_value None pl
441+
400442
method private get_arg_post_value
401-
(postconditions:annotated_xpredicate_t list * annotated_xpredicate_t list)
443+
(postconditions:
444+
annotated_xpredicate_t list * annotated_xpredicate_t list)
402445
(args:xpr_t list)
403446
(returnvalue:variable_t)
404447
(returntype:typ) =
405448
let rval =
406449
match postconditions with
407-
| (l,[])
450+
| (l, [])
408451
| (l, [(XNull _, _)]) ->
409-
List.fold_left (fun acc (pc,_) ->
452+
List.fold_left (fun acc (pc, _) ->
410453
match acc with
411454
| Some _ -> acc
412455
| _ ->
@@ -440,7 +483,7 @@ object (self)
440483
let basevar = env#mk_base_address_value returnvalue NoOffset t in
441484
(make_c_cmd SKIP, NUM_VAR basevar)
442485
| _ ->
443-
(make_c_cmd SKIP, NUM_VAR returnvalue)
486+
(make_c_cmd SKIP, NUM_VAR returnvalue)
444487

445488
method private get_post_assert
446489
(postconditions:annotated_xpredicate_t list * annotated_xpredicate_t list)
@@ -472,7 +515,8 @@ object (self)
472515
| XRelationalExpr (op,ArgValue(ParFormal n,ArgNoOffset),ReturnValue) ->
473516
let arg = List.nth args (n-1) in
474517
get_assert (XOp (binop_to_xop op, [arg; XVar rvar]))
475-
| XRelationalExpr (Lt, ReturnValue, ByteSize(ArgValue(ParFormal n, ArgNoOffset))) ->
518+
| XRelationalExpr (Lt, ReturnValue,
519+
ByteSize(ArgValue(ParFormal n, ArgNoOffset))) ->
476520
let arg = List.nth args (n-1) in
477521
let msg =
478522
LBLOCK [
@@ -604,6 +648,21 @@ object (self)
604648
[] in
605649
assign :: subassigns
606650

651+
| XInitializesExternalState (ExternalState name,
652+
ArgValue (ParFormal n, ArgNoOffset)) ->
653+
let arg = List.nth fnargs (n - 1) in
654+
let assigns =
655+
match arg with
656+
| CastE (_, Const (CInt (i64, _, _)))
657+
when (Int64.compare i64 Int64.zero) = 0 -> []
658+
| _ ->
659+
let xarg = exp_translator#translate_exp context arg in
660+
let lhs = env#mk_external_state_variable name NUM_VAR_TYPE in
661+
match xarg with
662+
| XVar v -> [make_c_cmd (ASSIGN_NUM (lhs, NUM_VAR v))]
663+
| _ -> [] in
664+
assigns
665+
607666
| XFormattedInput (ArgValue (ParFormal n,ArgNoOffset)) ->
608667
let (assignments,_) =
609668
List.fold_left (fun (acc, i) arg ->
@@ -780,7 +839,8 @@ object (self)
780839
p]))
781840

782841
method private get_arg_post_value
783-
(postconditions:annotated_xpredicate_t list * annotated_xpredicate_t list)
842+
(postconditions:
843+
annotated_xpredicate_t list * annotated_xpredicate_t list)
784844
(args:xpr_t list)
785845
(returnvalue:variable_t)
786846
(returntype:typ) =
@@ -825,7 +885,8 @@ object (self)
825885
(make_c_cmd SKIP, NUM_VAR returnvalue)
826886

827887
method private get_post_assert
828-
(postconditions:annotated_xpredicate_t list * annotated_xpredicate_t list)
888+
(postconditions:
889+
annotated_xpredicate_t list * annotated_xpredicate_t list)
829890
(fname:string)
830891
(_fvid:int)
831892
(rvar:variable_t)
@@ -854,7 +915,9 @@ object (self)
854915
| XRelationalExpr (op,ArgValue(ParFormal n,ArgNoOffset),ReturnValue) ->
855916
let arg = List.nth args (n-1) in
856917
get_assert (XOp (binop_to_xop op, [arg; XVar rvar]))
857-
| XRelationalExpr (Lt,ReturnValue, ByteSize(ArgValue(ParFormal n,ArgNoOffset))) ->
918+
| XRelationalExpr (Lt,
919+
ReturnValue,
920+
ByteSize(ArgValue(ParFormal n,ArgNoOffset))) ->
858921
let arg = List.nth args (n-1) in
859922
let msg =
860923
LBLOCK [
@@ -1409,7 +1472,7 @@ object (self)
14091472
match f with
14101473
| Lval (Var (fname,fvid), NoOffset) -> (* direct call *)
14111474
let fnargs = List.map (exp_translator#translate_exp ctxt) args in
1412-
let sideeffects = self#get_sideeffects fname fvid args in
1475+
let sideeffects = self#get_sideeffects fname loc fvid args in
14131476
let callop =
14141477
make_c_cmd
14151478
(OPERATION
@@ -1526,6 +1589,14 @@ object (self)
15261589
| XVar v -> Some (SYM_VAR v)
15271590
| _ -> acc
15281591
end
1592+
(* | XExternalStateValue (ReturnValue, ExternalState name) ->
1593+
let lhs = env#mk_external_state_variable name SYM_VAR_TYPE in
1594+
let regions = orakel#get_regions context (XVar lhs) in
1595+
let basevars =
1596+
List.map (fun r ->
1597+
let memreg = memregmgr#get_memory_region r#getSeqNumber in
1598+
memreg#get_memory_base) regions in
1599+
acc *)
15291600
| _ -> acc) None summary.fs_postconditions in
15301601

15311602
let with_null_branch cmd =
@@ -1578,6 +1649,7 @@ object (self)
15781649

15791650
method private get_sideeffects
15801651
(fname:string)
1652+
(_loc: location)
15811653
(_fvid:int)
15821654
(args:exp list) =
15831655
let sideeffects = get_sideeffects env#get_functionname (Some fname) context in
@@ -1636,6 +1708,25 @@ object (self)
16361708
| _ ->
16371709
[] in
16381710
cancel :: subcancels
1711+
| XInitializesExternalState (ExternalState name,
1712+
ArgValue (ParFormal n, ArgNoOffset)) ->
1713+
let arg = List.nth args (n - 1) in
1714+
let effects =
1715+
match arg with
1716+
| CastE (_, Const (CInt (i64, _, _)))
1717+
when (Int64.compare i64 Int64.zero) = 0 -> []
1718+
| _ ->
1719+
let xarg = exp_translator#translate_exp context arg in
1720+
let lhs = env#mk_external_state_variable name SYM_VAR_TYPE in
1721+
(match xarg with
1722+
| XVar v ->
1723+
let sym = memregmgr#mk_stack_region_sym v in
1724+
[make_c_cmd (ASSIGN_SYM (lhs, SYM sym))]
1725+
| XConst (SymSet [s]) ->
1726+
[make_c_cmd (ASSIGN_SYM (lhs, SYM s))]
1727+
| _ -> []) in
1728+
effects
1729+
16391730
| _ -> []) sideeffects) in
16401731
seeffects
16411732
end

CodeHawk/CHC/cchanalyze/cCHEnvironment.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ object(self)
274274
let cVal = vmgr#mk_check_variable l vtyp in
275275
self#add_chifvar cVal vtype
276276

277+
method mk_external_state_variable (name: string) (vt: variable_type_t) =
278+
let cvar = vmgr#mk_external_state_variable name in
279+
self#add_chifvar cvar vt
280+
277281
method mk_augmentation_variable
278282
(name:string) (purpose:string) (index:int) (vt:variable_type_t) =
279283
let cVal = vmgr#mk_augmentation_variable name purpose index in

CodeHawk/CHC/cchanalyze/cCHFunctionTranslator.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ object
254254
let fsymbol = EU.symbol f.svar.vname in
255255
let proc =
256256
EU.mkProcedure fsymbol ~signature:[] ~bindings:[] ~scope ~body:fbody in
257+
(* let _ = pr_debug [proc#toPretty] in *)
257258
let csystem = EU.mkSystem (new symbol_t "c-system") in
258259
let _ = csystem#addProcedure proc in
259260
(None,csystem)
@@ -434,6 +435,7 @@ object
434435
let fsymbol = EU.symbol f.svar.vname in
435436
let proc =
436437
EU.mkProcedure fsymbol ~signature:[] ~bindings:[] ~scope ~body:fbody in
438+
(* let _ = pr_debug [proc#toPretty; NL; NL] in *)
437439
let csystem = EU.mkSystem (new symbol_t "c-system") in
438440
let _ = csystem#addProcedure proc in
439441
(None, csystem)

CodeHawk/CHC/cchanalyze/cCHOrakel.ml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,20 @@ object (self)
6161
method private get_invariant (context:program_context_int) =
6262
invio#get_location_invariant context
6363

64-
method private get_expressions (context:program_context_int) (var:variable_t) =
64+
method get_external_state_value
65+
(context: program_context_int) (var: variable_t): xpr_t option =
66+
let facts = self#get_expressions context var in
67+
List.fold_left (fun acc nrv ->
68+
match acc with
69+
| Some _ -> acc
70+
| _ ->
71+
match nrv with
72+
| FSymbolicExpr x -> Some x
73+
| _ -> acc) None facts
74+
75+
method private get_expressions
76+
(context:program_context_int)
77+
(var:variable_t): non_relational_value_t list =
6578
let cmp i1 i2 =
6679
match (i1#const_value,i2#const_value) with
6780
| (Some _, Some _) -> 0

0 commit comments

Comments
 (0)