Skip to content

Commit 28f8cd9

Browse files
committed
CHC: add gcc-like function attributes
1 parent cc690ac commit 28f8cd9

15 files changed

+400
-85
lines changed

CodeHawk/CHC/cchanalyze/cCHGenerateAndCheck.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ open CHTimingLog
3939

4040
(* cchlib *)
4141
open CCHBasicTypes
42+
open CCHFileContract
4243
open CCHSettings
4344
open CCHUtilities
4445

@@ -348,6 +349,7 @@ let generate_and_check_process_file (domains: string list) =
348349
let _ = read_cfile_interface_dictionary () in
349350
let _ = read_cfile_assignment_dictionary () in
350351
let _ = read_cfile_contract () in
352+
let _ = file_contract#collect_file_attributes in
351353
let _ = log_info "Read file-level xml files" in
352354
let functions = fenv#get_application_functions in
353355
let _ = log_info "Processing %d functions" (List.length functions) in

CodeHawk/CHC/cchanalyze/cCHPOCheckValidMem.ml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(* =============================================================================
2-
CodeHawk C Analyzer
2+
CodeHawk C Analyzer
33
Author: Henny Sipma
44
------------------------------------------------------------------------------
55
The MIT License (MIT)
6-
6+
77
Copyright (c) 2005-2019 Kestrel Technology LLC
88
Copyright (c) 2020-2024 Henny B. Sipma
99
Copyright (c) 2024 Aarno Labs LLC
@@ -14,10 +14,10 @@
1414
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1515
copies of the Software, and to permit persons to whom the Software is
1616
furnished to do so, subject to the following conditions:
17-
17+
1818
The above copyright notice and this permission notice shall be included in all
1919
copies or substantial portions of the Software.
20-
20+
2121
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2222
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2323
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -31,13 +31,13 @@
3131
open CHLanguage
3232
open CHNumerical
3333
open CHPretty
34-
34+
3535
(* chutil *)
3636
open CHPrettyUtil
37-
37+
3838
(* xprlib *)
3939
open XprTypes
40-
40+
4141
(* cchlib *)
4242
open CCHBasicTypes
4343
open CCHFileContract
@@ -49,7 +49,7 @@ open CCHMemoryBase
4949
open CCHPOPredicate
5050
open CCHPreTypes
5151
open CCHProofObligation
52-
52+
5353
(* cchanalyze *)
5454
open CCHAnalysisTypes
5555

@@ -59,10 +59,10 @@ let e2s e = p2s (exp_to_pretty e)
5959
let cd = CCHDictionary.cdictionary
6060

6161
(* -----------------------------------------------------------------------------
62-
* The IH guarantees that any region pointed to by an argument is valid memory
63-
* at function entry point (checked at the time of the call. Similarly any
62+
* The IH guarantees that any region pointed to by an argument is valid memory
63+
* at function entry point (checked at the time of the call). Similarly any
6464
* region pointed to by a return value from a callee is valid memory at the
65-
* point where the pointer value is received. For any other address
65+
* point where the pointer value is received. For any other address
6666
* locations received from outside the proof obligation should be delegated.
6767
* If the application does not contain any calls to free at all (indicated by
6868
* global_free) the valid-mem obligation is vacuously valid.
@@ -105,7 +105,7 @@ object (self)
105105
method private memref_to_string memref =
106106
"memory base: " ^ (p2s (memory_base_to_pretty memref#get_base))
107107

108-
108+
109109
(* ----------------------------- safe ------------------------------------- *)
110110

111111
method get_calls (v: variable_t) =
@@ -237,7 +237,7 @@ object (self)
237237
end
238238
| _::tl ->
239239
let exps = List.map cd#get_exp (List.map int_of_string tl) in
240-
let preds =
240+
let preds =
241241
List.map (fun e ->
242242
let pred = PDistinctRegion (e,v#getName#getSeqNumber) in
243243
begin
@@ -257,7 +257,7 @@ object (self)
257257
^ (p2s v#toPretty) ^ " is not a fixed value");
258258
None
259259
end
260-
260+
261261
method private call_preserves_validity (v: variable_t) (sym: symbol_t) =
262262
let sideeffects = poq#get_sym_sideeffects sym in
263263
let callee = poq#env#get_callsym_callee sym in
@@ -270,7 +270,7 @@ object (self)
270270
let msg = callee.vname ^ " preserves all memory" in
271271
Some (deps, msg)
272272
else if List.exists (fun (se, _) ->
273-
match se with
273+
match se with
274274
| XPreservesAllMemoryX _ -> true
275275
| _ -> false) sideeffects then
276276
let xexps = sym#getAttributes in
@@ -445,7 +445,7 @@ object (self)
445445
self#check_regions_safe inv
446446
| _ -> None in
447447
r
448-
448+
449449
method check_safe =
450450
self#global_free
451451
|| (match invs with
@@ -480,7 +480,7 @@ object (self)
480480
end
481481
| _ -> false) false einvs
482482
| _ -> false))
483-
483+
484484
(* ----------------------- violation -------------------------------------- *)
485485
method check_violation = false
486486
(* ----------------------- delegation ------------------------------------- *)
@@ -524,7 +524,7 @@ object (self)
524524
| Some x -> self#xpr_implies_delegation inv#index x
525525
| _ -> None in
526526
r
527-
527+
528528
method check_delegation =
529529
match invs with
530530
| [] -> false
@@ -538,8 +538,8 @@ object (self)
538538
true
539539
end
540540
| _ -> false) false invs
541-
542-
541+
542+
543543
end
544544

545545

CodeHawk/CHC/cchanalyze/cCHPOQuery.ml

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,23 @@ object (self)
127127
match self#get_summary callee.vname with
128128
| Some fs -> (fs.fs_postconditions, fs.fs_error_postconditions)
129129
| _ ->
130-
let callcontext = env#get_callsym_context s in
131-
if proof_scaffolding#has_direct_callsite fname callcontext then
132-
let directcallsite =
133-
proof_scaffolding#get_direct_callsite fname callcontext in
134-
(directcallsite#get_postassumes, [])
135-
else if proof_scaffolding#has_indirect_callsite fname callcontext then
136-
let indirectcallsite =
137-
proof_scaffolding#get_indirect_callsite fname callcontext in
138-
(indirectcallsite#get_postassumes, [])
130+
if file_contract#has_function_contract callee.vname then
131+
let pcs =
132+
(file_contract#get_function_contract callee.vname)#get_postconditions in
133+
let pcs = List.map (fun pc -> (pc, NoAnnotation)) pcs in
134+
(pcs, [])
139135
else
140-
([], [])
136+
let callcontext = env#get_callsym_context s in
137+
if proof_scaffolding#has_direct_callsite fname callcontext then
138+
let directcallsite =
139+
proof_scaffolding#get_direct_callsite fname callcontext in
140+
(directcallsite#get_postassumes, [])
141+
else if proof_scaffolding#has_indirect_callsite fname callcontext then
142+
let indirectcallsite =
143+
proof_scaffolding#get_indirect_callsite fname callcontext in
144+
(indirectcallsite#get_postassumes, [])
145+
else
146+
([], [])
141147
else
142148
raise
143149
(CCHFailure
@@ -154,17 +160,22 @@ object (self)
154160
match self#get_summary callee.vname with
155161
| Some fs -> fs.fs_sideeffects
156162
| _ ->
157-
let callcontext = env#get_callsym_context s in
158-
if proof_scaffolding#has_direct_callsite fname callcontext then
159-
let directcallsite =
160-
proof_scaffolding#get_direct_callsite fname callcontext in
161-
directcallsite#get_postassumes
162-
else if proof_scaffolding#has_indirect_callsite fname callcontext then
163-
let indirectcallsite =
164-
proof_scaffolding#get_indirect_callsite fname callcontext in
165-
indirectcallsite#get_postassumes
163+
if file_contract#has_function_contract callee.vname then
164+
let l =
165+
(file_contract#get_function_contract callee.vname)#get_sideeffects in
166+
List.map (fun x -> (x, ExternalCondition callee.vname)) l
166167
else
167-
[]
168+
let callcontext = env#get_callsym_context s in
169+
if proof_scaffolding#has_direct_callsite fname callcontext then
170+
let directcallsite =
171+
proof_scaffolding#get_direct_callsite fname callcontext in
172+
directcallsite#get_postassumes
173+
else if proof_scaffolding#has_indirect_callsite fname callcontext then
174+
let indirectcallsite =
175+
proof_scaffolding#get_indirect_callsite fname callcontext in
176+
indirectcallsite#get_postassumes
177+
else
178+
[]
168179
else
169180
raise
170181
(CCHFailure

CodeHawk/CHC/cchcil/cHCilSumTypeSerializer.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ object
214214
"aalignof";
215215
"aalignofe";
216216
"aalignofs";
217+
"aassign";
217218
"abinop";
218219
"acons";
219220
"adot";

CodeHawk/CHC/cchlib/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ MLIS := \
4949
cCHInterfaceDictionary \
5050
cCHExternalPredicate \
5151
cCHFunctionSummary \
52+
cCHCAttributes \
5253
cCHFileContract \
5354

5455
CMIS := $(addprefix cmi/,$(MLIS:%=%.cmi))
@@ -75,6 +76,7 @@ SOURCES := \
7576
cCHInterfaceDictionary \
7677
cCHExternalPredicate \
7778
cCHFunctionSummary \
79+
cCHCAttributes \
7880
cCHFileContract \
7981

8082
OBJECTS := $(addprefix cmx/,$(SOURCES:%=%.cmx))

CodeHawk/CHC/cchlib/cCHBasicTypes.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ and attrparam =
138138
| AAddrOf of attrparam
139139
| AIndex of attrparam * attrparam
140140
| AQuestion of attrparam * attrparam * attrparam
141+
| AAssign of attrparam * attrparam
141142

142143
and compinfo = {
143144
cstruct: bool ;

CodeHawk/CHC/cchlib/cCHDictionary.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ object (self)
129129
| ADot (a,s) -> (tags @ [s], [self#index_attrparam a])
130130
| AStar a -> (tags, [self#index_attrparam a])
131131
| AAddrOf a -> (tags, [self#index_attrparam a])
132+
| AAssign (a1, a2) ->
133+
(tags, [self#index_attrparam a1; self#index_attrparam a2])
132134
| AIndex (a1,a2) ->
133135
(tags, [self#index_attrparam a1; self#index_attrparam a2])
134136
| AQuestion (a1, a2, a3) ->
@@ -159,6 +161,7 @@ object (self)
159161
| "adot" -> ADot (self#get_attrparam (a 0), (t 1))
160162
| "astar" -> AStar (self#get_attrparam (a 0))
161163
| "aaddrof" -> AAddrOf (self#get_attrparam (a 0))
164+
| "aassign" -> AAssign (self#get_attrparam (a 0), self#get_attrparam (a 1))
162165
| "aindex" -> AIndex (self#get_attrparam (a 0), self#get_attrparam (a 1))
163166
| "aquestion" ->
164167
AQuestion (self#get_attrparam (a 0), self#get_attrparam (a 1),

0 commit comments

Comments
 (0)