@@ -37,16 +37,12 @@ impl KaniSession {
37
37
self . goto_sanity_check ( output) ?;
38
38
}
39
39
40
- self . instrument_contracts ( harness, output) ?;
41
-
42
- if self
40
+ let is_loop_contracts_enabled = self
43
41
. args
44
42
. common_args
45
43
. unstable_features
46
- . contains ( kani_metadata:: UnstableFeature :: LoopContracts )
47
- {
48
- self . instrument_loop_contracts ( harness, output) ?;
49
- }
44
+ . contains ( kani_metadata:: UnstableFeature :: LoopContracts ) ;
45
+ self . instrument_contracts ( harness, is_loop_contracts_enabled, output) ?;
50
46
51
47
if self . args . checks . undefined_function_on ( ) {
52
48
self . add_library ( output) ?;
@@ -172,42 +168,46 @@ impl KaniSession {
172
168
self . call_goto_instrument ( args)
173
169
}
174
170
175
- /// Make CBMC enforce a function contract.
176
- pub fn instrument_contracts ( & self , harness : & HarnessMetadata , file : & Path ) -> Result < ( ) > {
177
- let Some ( assigns) = harness. contract . as_ref ( ) else { return Ok ( ( ) ) } ;
171
+ /// Apply annotated function contracts and loop contracts with goto-instrument.
172
+ pub fn instrument_contracts (
173
+ & self ,
174
+ harness : & HarnessMetadata ,
175
+ is_loop_contracts_enabled : bool ,
176
+ file : & Path ,
177
+ ) -> Result < ( ) > {
178
+ // Do nothing if neither loop contracts nor function contracts is enabled.
179
+ if !is_loop_contracts_enabled && harness. contract . is_none ( ) {
180
+ return Ok ( ( ) ) ;
181
+ }
178
182
179
- let mut args: Vec < OsString > = vec ! [
180
- "--dfcc" . into( ) ,
181
- ( & harness. mangled_name) . into( ) ,
182
- "--enforce-contract" . into( ) ,
183
- assigns. contracted_function_name. as_str( ) . into( ) ,
184
- "--no-malloc-may-fail" . into( ) ,
185
- file. into( ) ,
186
- file. into( ) ,
187
- ] ;
188
- if let Some ( tracker) = & assigns. recursion_tracker {
189
- args. push ( "--nondet-static-exclude" . into ( ) ) ;
190
- args. push ( tracker. as_str ( ) . into ( ) ) ;
183
+ let mut args: Vec < OsString > =
184
+ vec ! [ "--dfcc" . into( ) , ( & harness. mangled_name) . into( ) , "--no-malloc-may-fail" . into( ) ] ;
185
+
186
+ if is_loop_contracts_enabled {
187
+ args. append ( & mut vec ! [
188
+ "--apply-loop-contracts" . into( ) ,
189
+ "--loop-contracts-no-unwind" . into( ) ,
190
+ // Because loop contracts now are wrapped in a closure which will be a side-effect expression in CBMC even they
191
+ // may not contain side-effect. So we disable the side-effect check for now and will implement a better check
192
+ // instead of simply rejecting function calls and statement expressions.
193
+ // See issue: diffblue/cbmc#8393
194
+ "--disable-loop-contracts-side-effect-check" . into( ) ,
195
+ ] ) ;
191
196
}
192
- self . call_goto_instrument ( & args)
193
- }
194
197
195
- /// Apply annotated loop contracts.
196
- pub fn instrument_loop_contracts ( & self , harness : & HarnessMetadata , file : & Path ) -> Result < ( ) > {
197
- let args: Vec < OsString > = vec ! [
198
- "--dfcc" . into( ) ,
199
- ( & harness. mangled_name) . into( ) ,
200
- "--apply-loop-contracts" . into( ) ,
201
- "--loop-contracts-no-unwind" . into( ) ,
202
- "--no-malloc-may-fail" . into( ) ,
203
- // Because loop contracts now are wrapped in a closure which will be a side-effect expression in CBMC even they
204
- // may not contain side-effect. So we disable the side-effect check for now and will implement a better check
205
- // instead of simply rejecting function calls and statement expressions.
206
- // See issue: diffblue/cbmc#8393
207
- "--disable-loop-contracts-side-effect-check" . into( ) ,
208
- file. into( ) ,
209
- file. into( ) ,
210
- ] ;
198
+ if let Some ( assigns) = harness. contract . as_ref ( ) {
199
+ args. push ( "--enforce-contract" . into ( ) ) ;
200
+ args. push ( assigns. contracted_function_name . as_str ( ) . into ( ) ) ;
201
+
202
+ if let Some ( tracker) = & assigns. recursion_tracker {
203
+ args. push ( "--nondet-static-exclude" . into ( ) ) ;
204
+ args. push ( tracker. as_str ( ) . into ( ) ) ;
205
+ }
206
+ }
207
+
208
+ args. push ( file. into ( ) ) ;
209
+ args. push ( file. into ( ) ) ;
210
+
211
211
self . call_goto_instrument ( & args)
212
212
}
213
213
0 commit comments