Skip to content

Commit 5eaebca

Browse files
committed
CHC: add attribute post request for initialized value
1 parent c56db3b commit 5eaebca

7 files changed

Lines changed: 104 additions & 3 deletions

File tree

CodeHawk/CHC/cchanalyze/cCHAnalysisTypes.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,14 @@ object
219219
method get_variable_type: variable_t -> typ
220220
method get_local_variable: variable_t -> (varinfo * offset)
221221
method get_global_variable: variable_t -> (varinfo * offset)
222+
223+
(** [get_memory_variable v] returns the memory reference associated with the
224+
base of variable [v], and the offset from that base.
225+
226+
@raise [BCH_failure] if [v] is not a memory variable
227+
*)
222228
method get_memory_variable: variable_t -> (memory_reference_int * offset)
229+
223230
method get_memory_address: variable_t -> (memory_reference_int * offset)
224231
method get_memory_region: symbol_t -> memory_region_int
225232
method get_vinfo_offset: variable_t -> varinfo -> offset option

CodeHawk/CHC/cchanalyze/cCHPOCheckInitialized.ml

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,30 @@ object (self)
108108
- memlval_vinv_memref_basevar_implies_safe
109109
*)
110110

111+
method private postcondition_implies_safe
112+
(invindex: int)
113+
(callee: varinfo)
114+
(pcs: annotated_xpredicate_t list) =
115+
let mname = "postcondition_implies_safe" in
116+
match pcs with
117+
| [] -> None
118+
| _ ->
119+
List.fold_left (fun facc (pc, _) ->
120+
match facc with
121+
| Some _ -> facc
122+
| _ ->
123+
match pc with
124+
| XInitialized (ArgAddressedValue (ReturnValue, ArgNoOffset)) ->
125+
let deps =
126+
DEnvC ([invindex], [PostAssumption (callee.vid, pc)]) in
127+
let msg =
128+
"value addressed by return value from "
129+
^ callee.vname
130+
^ " is initialized" in
131+
let site = Some (__FILE__, __LINE__, mname) in
132+
Some (deps, msg, site)
133+
| _ -> None) None pcs
134+
111135
method private inv_implies_safe (inv: invariant_int) =
112136
let mname = "inv_implies_safe" in
113137
match inv#get_fact with
@@ -125,7 +149,56 @@ object (self)
125149
let site = Some (__FILE__, __LINE__, mname) in
126150
Some (deps, msg, site)
127151
end
128-
| _ -> None
152+
| _ ->
153+
match inv#expr with
154+
| Some (XVar v) when poq#env#is_initial_value v ->
155+
let var = poq#env#get_initial_value_variable v in
156+
if poq#env#is_memory_variable var then
157+
let (memref, offset) = poq#env#get_memory_variable var in
158+
if is_zero_memory_offset offset then
159+
if memref#has_external_base then
160+
let basevar = memref#get_external_basevar in
161+
if poq#env#is_function_return_value basevar then
162+
let callee = poq#env#get_callvar_callee basevar in
163+
let (pcs, epcs) = poq#get_postconditions basevar in
164+
let r =
165+
match epcs with
166+
| [] ->
167+
self#postcondition_implies_safe inv#index callee pcs
168+
| _ -> None in
169+
match r with
170+
| None ->
171+
let pcr =
172+
XInitialized
173+
(ArgAddressedValue (ReturnValue, ArgNoOffset)) in
174+
begin
175+
poq#mk_postcondition_request pcr callee;
176+
poq#set_diagnostic
177+
("Unable to determine if memory pointed at by the return "
178+
^ "value from "
179+
^ callee.vname
180+
^ " is initialized.");
181+
None
182+
end
183+
| Some _ -> r
184+
else
185+
begin
186+
poq#set_diagnostic_arg
187+
1 ("memvar:base: " ^ (p2s memref#toPretty));
188+
None
189+
end
190+
else
191+
None
192+
else
193+
begin
194+
poq#set_diagnostic_arg
195+
1 ("initial-value: " ^ (p2s v#toPretty));
196+
None
197+
end
198+
else
199+
None
200+
| _ ->
201+
None
129202

130203
method private check_safe_functionpointer (vinfo: varinfo) =
131204
let vinfovalues = poq#get_vinfo_offset_values vinfo in

CodeHawk/CHC/cchlib/cCHCAttributes.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ let convert_attribute_to_function_conditions
158158
| Attr ("returns_nonnull", []) ->
159159
([], [XNotNull ReturnValue], [])
160160

161+
| Attr ("chkc_returns_addressed_initialized", []) ->
162+
([], [XInitialized (ArgAddressedValue (ReturnValue, ArgNoOffset))], [])
163+
161164
| Attr ("chkc_returns_null_terminated_string", []) ->
162165
([], [XNullTerminated ReturnValue], [])
163166

CodeHawk/CHC/cchlib/cCHFileContract.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,16 @@ object (self)
637637
(vinfo: varinfo) (attr: attribute) =
638638
let (xpre, xpost, xside) =
639639
convert_attribute_to_function_conditions vinfo.vname attr in
640+
let logx kind x =
641+
log_diagnostics_result
642+
~tag:("add_function_attribute_conditions:" ^ kind)
643+
~msg:vinfo.vname
644+
__FILE__ __LINE__
645+
[p2s (xpredicate_to_pretty x)] in
640646
begin
647+
List.iter (logx "pre") xpre;
648+
List.iter (logx "post") xpost;
649+
List.iter (logx "side") xside;
641650
List.iter (self#add_precondition vinfo.vname) xpre;
642651
List.iter (self#add_postcondition vinfo.vname) xpost;
643652
List.iter (self#add_sideeffect vinfo.vname) xside

CodeHawk/CHC/cchlib/cCHTypesUtil.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ let rec is_constant_string (e:exp) =
217217
| _ -> false
218218

219219

220+
let is_zero_memory_offset (offset: offset): bool =
221+
match offset with
222+
| NoOffset -> true
223+
| Index (Const (CInt (i64, _, _)), NoOffset) -> i64 = Int64.zero
224+
| _ -> false
225+
226+
220227
let rec type_of_exp (fdecls:cfundeclarations_int) (x:exp) : typ =
221228
try
222229
let ty =

CodeHawk/CHC/cchlib/cCHTypesUtil.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ val is_field_offset: offset -> bool
148148

149149
val is_constant_offset: offset -> bool
150150

151+
val is_zero_memory_offset: offset -> bool
152+
151153
val is_field_lval_exp: exp -> bool
152154

153155
val exp_has_repeated_field: exp -> bool

CodeHawk/CHC/cchlib/cCHVersion.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ object (self)
6262
end
6363

6464
let version = new version_info_t
65-
~version:"0.3.0_20260223"
66-
~date:"2026-02-23"
65+
~version:"0.3.0_20260302"
66+
~date:"2026-03-02"

0 commit comments

Comments
 (0)