Skip to content

Commit fe0c916

Browse files
committed
CHC: record all domains that conclude locations are unreachable
1 parent 8f1d1b4 commit fe0c916

3 files changed

Lines changed: 49 additions & 13 deletions

File tree

‎CodeHawk/CHC/cchanalyze/cCHExpTranslator.ml‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,20 @@ object (self)
11791179
~ok:(fun ftype ->
11801180
match x with
11811181
| Const c -> self#translate_const_expr c
1182-
| CastE (_, xx) -> self#translate_expr xx
1182+
| CastE (_, xx) ->
1183+
(* An integer conversion that may change the value (C11
1184+
6.3.1.3p2-3; issue 21) is applied to a constant operand and
1185+
makes any other operand unknown. Previously every cast was
1186+
the identity: (uint8_t)x > 100 with x == 300 folded to TRUE
1187+
and made the else branch unreachable (issue 25). *)
1188+
let inner = self#translate_expr xx in
1189+
(match CCHIntegerSemantics.get_correction env context x with
1190+
| None -> inner
1191+
| Some c ->
1192+
match inner with
1193+
| XConst (IntConst n) ->
1194+
num_constant_expr (CCHIntegerSemantics.fold_constant c n)
1195+
| _ -> random_constant_expr)
11831196
| Lval lval -> self#translate_variable_expr lval ftype
11841197
| AddrOf lval
11851198
| StartOf lval -> self#translate_address lval ftype

‎CodeHawk/CHC/cchanalyze/cCHPOChecker.ml‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
The MIT License (MIT)
66
77
Copyright (c) 2005-2019 Kestrel Technology LLC
8-
Copyright (c) 2020-2023 Henny B. Sipma
8+
Copyright (c) 2020-2025 Henny B. Sipma
99
Copyright (c) 2026 Aarno Labs LLC
1010
1111
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -202,10 +202,10 @@ object
202202
end
203203

204204

205-
let is_unreachable facts =
206-
List.fold_left (fun acc f ->
207-
match acc with Some _ -> acc | _ -> f#is_unreachable)
208-
None facts
205+
(* domains that report the location unreachable *)
206+
let unreachable_domains facts =
207+
List.sort_uniq Stdlib.compare
208+
(List.filter_map (fun f -> f#is_unreachable) facts)
209209

210210
let _check_file_assumptions _fenv _file_assumptions _p = false
211211

@@ -261,22 +261,42 @@ let check_proof_obligations
261261
| CCHFailure s -> po_check_failure (msg s)
262262
| Failure s -> po_check_failure (msg (STR s)) in
263263

264-
match is_unreachable
264+
match unreachable_domains
265265
(invIO#get_location_invariant
266266
p#get_context#project_on_cfg)#get_invariants with
267-
| Some domain ->
267+
| (domain :: _) as domains ->
268268
if system_settings#use_unreachability then
269269
ignore ((make_unreachable p domain))
270270
else
271+
(* Unreachability is rare in real code and usually
272+
points to an error or omission in the analysis; it must not
273+
silently discharge the proof obligation. The other domains'
274+
facts at this location are not used either: they are vacuous
275+
if the location is unreachable (a violation would be a false
276+
alarm, an api delegation a spurious precondition) and
277+
suspect otherwise. Only the statement-local check, which does
278+
not use invariants, is performed; if the proof obligation
279+
remains open it is marked with an explicit unreachable
280+
diagnostic that is reported separately. *)
271281
begin
272282
chlog#add
273283
"unreachable"
274284
(LBLOCK [
275285
location_to_pretty p#get_location;
276-
STR " (domain: ";
277-
STR domain;
286+
STR " (domains: ";
287+
STR (String.concat ", " domains);
278288
STR ")"]);
279-
default ()
289+
(try
290+
check_ppo_validity env#get_functionname env#get_fdecls p
291+
with
292+
| CCHFailure s -> po_check_failure (msg s)
293+
| Failure s -> po_check_failure (msg (STR s)));
294+
if p#is_closed then
295+
()
296+
else
297+
List.iter (fun d ->
298+
p#add_diagnostic_key_msg
299+
"unreachable" ("proven by domain " ^ d)) domains
280300
end
281-
| _ ->
301+
| [] ->
282302
default ()) proofObligations

‎CodeHawk/CHC/cchpre/cCHProofObligation.ml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
Copyright (c) 2005-2019 Kestrel Technology LLC
88
Copyright (c) 2020 Henny B. Sipma
9-
Copyright (c) 2021-2025 Aarno Labs LLC
9+
Copyright (c) 2021-2026 Aarno Labs LLC
1010
1111
Permission is hereby granted, free of charge, to any person obtaining a copy
1212
of this software and associated documentation files (the "Software"), to deal
@@ -208,6 +208,8 @@ object (self)
208208
method clear =
209209
begin
210210
H.clear invarianttable;
211+
H.clear argmessages;
212+
H.clear keymessages;
211213
messages#removeList messages#toList
212214
end
213215

@@ -267,6 +269,7 @@ object (self)
267269
method is_empty =
268270
(H.length invarianttable) = 0
269271
&& (H.length argmessages = 0)
272+
&& (H.length keymessages = 0)
270273
&& messages#isEmpty
271274

272275
method private write_xml_situated_msg

0 commit comments

Comments
 (0)