Debian Codegraph @

crt/aws-c-common/verification/cbmc/proofs/Makefile.common

raw
1 # -*- mode: makefile -*-
2 # The first line sets the emacs major mode to Makefile
3
4 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 # SPDX-License-Identifier: MIT-0
6
7 CBMC_STARTER_KIT_VERSION = CBMC starter kit 2.11
8
9 ################################################################
10 # The CBMC Starter Kit depends on the files Makefile.common and
11 # run-cbmc-proofs.py. They are installed by the setup script
12 # cbmc-starter-kit-setup and updated to the latest version by the
13 # update script cbmc-starter-kit-update. For more information about
14 # the starter kit and these files and these scripts, see
15 # https://model-checking.github.io/cbmc-starter-kit
16 #
17 # Makefile.common implements what we consider to be some best
18 # practices for using cbmc for software verification.
19 #
20 # Section I gives default values for a large number of Makefile
21 # variables that control
22 # * how your code is built (include paths, etc),
23 # * what program transformations are applied to your code (loop
24 # unwinding, etc), and
25 # * what properties cbmc checks for in your code (memory safety, etc).
26 #
27 # These variables are defined below with definitions of the form
28 # VARIABLE ?= DEFAULT_VALUE
29 # meaning VARIABLE is set to DEFAULT_VALUE if VARIABLE has not already
30 # been given a value.
31 #
32 # For your project, you can override these default values with
33 # project-specific definitions in Makefile-project-defines.
34 #
35 # For any individual proof, you can override these default values and
36 # project-specific values with proof-specific definitions in the
37 # Makefile for your proof.
38 #
39 # The definitions in the proof Makefile override definitions in the
40 # project Makefile-project-defines which override definitions in this
41 # Makefile.common.
42 #
43 # Section II uses the values defined in Section I to build your code, run
44 # your proof, and build a report of your results. You should not need
45 # to modify or override anything in Section II, but you may want to
46 # read it to understand how the values defined in Section I control
47 # things.
48 #
49 # To use Makefile.common, set variables as described above as needed,
50 # and then for each proof,
51 #
52 # * Create a subdirectory <DIR>.
53 # * Write a proof harness (a function) with the name <HARNESS_ENTRY>
54 # in a file with the name <DIR>/<HARNESS_FILE>.c
55 # * Write a makefile with the name <DIR>/Makefile that looks
56 # something like
57 #
58 # HARNESS_FILE=<HARNESS_FILE>
59 # HARNESS_ENTRY=<HARNESS_ENTRY>
60 # PROOF_UID=<PROOF_UID>
61 #
62 # PROJECT_SOURCES += $(SRCDIR)/libraries/api_1.c
63 # PROJECT_SOURCES += $(SRCDIR)/libraries/api_2.c
64 #
65 # PROOF_SOURCES += $(PROOFDIR)/harness.c
66 # PROOF_SOURCES += $(SRCDIR)/cbmc/proofs/stub_a.c
67 # PROOF_SOURCES += $(SRCDIR)/cbmc/proofs/stub_b.c
68 #
69 # UNWINDSET += foo.0:3
70 # UNWINDSET += bar.1:6
71 #
72 # REMOVE_FUNCTION_BODY += api_stub_a
73 # REMOVE_FUNCTION_BODY += api_stub_b
74 #
75 # DEFINES = -DDEBUG=0
76 #
77 # include ../Makefile.common
78 #
79 # * Change directory to <DIR> and run make
80 #
81 # The proof setup script cbmc-starter-kit-setup-proof from the CBMC
82 # Starter Kit will do most of this for, creating a directory and
83 # writing a basic Makefile and proof harness into it that you can edit
84 # as described above.
85 #
86 # Warning: If you get results that are hard to explain, consider
87 # running "make clean" or "make veryclean" before "make" if you get
88 # results that are hard to explain. Dependency handling in this
89 # Makefile.common may not be perfect.
90
91 SHELL=/bin/bash
92
93 default: report
94
95 ################################################################
96 ################################################################
97 ## Section I: This section gives common variable definitions.
98 ##
99 ## Override these definitions in Makefile-project-defines or
100 ## your proof Makefile.
101 ##
102 ## Remember that Makefile.common and Makefile-project-defines are
103 ## included into the proof Makefile in your proof directory, so all
104 ## relative pathnames defined there should be relative to your proof
105 ## directory.
106
107 ################################################################
108 # Define the layout of the source tree and the proof subtree
109 #
110 # Generally speaking,
111 #
112 # SRCDIR = the root of the repository
113 # CBMC_ROOT = /srcdir/cbmc
114 # PROOF_ROOT = /srcdir/cbmc/proofs
115 # PROOF_SOURCE = /srcdir/cbmc/sources
116 # PROOF_INCLUDE = /srcdir/cbmc/include
117 # PROOF_STUB = /srcdir/cbmc/stubs
118 # PROOFDIR = the directory containing the Makefile for your proof
119 #
120 # The path /srcdir/cbmc used in the example above is determined by the
121 # setup script cbmc-starter-kit-setup. Projects usually create a cbmc
122 # directory somewhere in the source tree, and run the setup script in
123 # that directory. The value of CBMC_ROOT becomes the absolute path to
124 # that directory.
125 #
126 # The location of that cbmc directory in the source tree affects the
127 # definition of SRCDIR, which is defined in terms of the relative path
128 # from a proof directory to the repository root. The definition is
129 # usually determined by the setup script cbmc-starter-kit-setup and
130 # written to Makefile-template-defines, but you can override it for a
131 # project in Makefile-project-defines and for a specific proof in the
132 # Makefile for the proof.
133
134 # Absolute path to the directory containing this Makefile.common
135 # See https://ftp.gnu.org/old-gnu/Manuals/make-3.80/html_node/make_17.html
136 #
137 # Note: We compute the absolute paths to the makefiles in MAKEFILE_LIST
138 # before we filter the list of makefiles for %/Makefile.common.
139 # Otherwise an invocation of the form "make -f Makefile.common" will set
140 # MAKEFILE_LIST to "Makefile.common" which will fail to match the
141 # pattern %/Makefile.common.
142 #
143 MAKEFILE_PATHS = $(foreach makefile,$(MAKEFILE_LIST),$(abspath $(makefile)))
144 PROOF_ROOT = $(dir $(filter %/Makefile.common,$(MAKEFILE_PATHS)))
145
146 CBMC_ROOT = $(shell dirname $(PROOF_ROOT))
147 PROOF_SOURCE = $(CBMC_ROOT)/sources
148 PROOF_INCLUDE = $(CBMC_ROOT)/include
149 PROOF_STUB = $(CBMC_ROOT)/stubs
150
151 # Project-specific definitions to override default definitions below
152 # * Makefile-project-defines will never be overwritten
153 # * Makefile-template-defines may be overwritten when the starter
154 # kit is updated
155 sinclude $(PROOF_ROOT)/Makefile-project-defines
156 sinclude $(PROOF_ROOT)/Makefile-template-defines
157
158 # SRCDIR is the path to the root of the source tree
159 # This is a default definition that is frequently overridden in
160 # another Makefile, see the discussion of SRCDIR above.
161 SRCDIR ?= $(abspath ../..)
162
163 # PROOFDIR is the path to the directory containing the proof harness
164 PROOFDIR ?= $(abspath .)
165
166 ################################################################
167 # Define how to run CBMC
168
169 # Do property checking with the external SAT solver given by
170 # EXTERNAL_SAT_SOLVER. Do coverage checking with the default solver,
171 # since coverage checking requires the use of an incremental solver.
172 # The EXTERNAL_SAT_SOLVER variable is typically set (if it is at all)
173 # as an environment variable or as a makefile variable in
174 # Makefile-project-defines.
175 #
176 # For a particular proof, if the default solver is faster, do property
177 # checking with the default solver by including this definition in the
178 # proof Makefile:
179 # USE_EXTERNAL_SAT_SOLVER =
180 #
181 ifneq ($(strip $(EXTERNAL_SAT_SOLVER)),)
182 USE_EXTERNAL_SAT_SOLVER ?= --external-sat-solver $(EXTERNAL_SAT_SOLVER)
183 endif
184 CHECKFLAGS += $(USE_EXTERNAL_SAT_SOLVER)
185
186 # Job pools
187 # For version of Litani that are new enough (where `litani print-capabilities`
188 # prints "pools"), proofs for which `EXPENSIVE = true` is set can be added to a
189 # "job pool" that restricts how many expensive proofs are run at a time. All
190 # other proofs will be built in parallel as usual.
191 #
192 # In more detail: all compilation, instrumentation, and report jobs are run with
193 # full parallelism as usual, even for expensive proofs. The CBMC jobs for
194 # non-expensive proofs are also run in parallel. The only difference is that the
195 # CBMC safety checks and coverage checks for expensive proofs are run with a
196 # restricted parallelism level. At any one time, only N of these jobs are run at
197 # once, amongst all the proofs.
198 #
199 # To configure N, Litani needs to be initialized with a pool called "expensive".
200 # For example, to only run two CBMC safety/coverage jobs at a time from amongst
201 # all the proofs, you would initialize litani like
202 # litani init --pools expensive:2
203 # The run-cbmc-proofs.py script takes care of this initialization through the
204 # --expensive-jobs-parallelism flag.
205 #
206 # To enable this feature, set
207 # the ENABLE_POOLS variable when running Make, like
208 # `make ENABLE_POOLS=true report`
209 # The run-cbmc-proofs.py script takes care of this through the
210 # --restrict-expensive-jobs flag.
211
212 ifeq ($(strip $(ENABLE_POOLS)),)
213 POOL =
214 INIT_POOLS =
215 else ifeq ($(strip $(EXPENSIVE)),)
216 POOL =
217 INIT_POOLS =
218 else
219 POOL = --pool expensive
220 INIT_POOLS = --pools expensive:1
221 endif
222
223 # Similar to the pool feature above. If Litani is new enough, enable
224 # profiling CBMC's memory use.
225 ifeq ($(strip $(ENABLE_MEMORY_PROFILING)),)
226 MEMORY_PROFILING =
227 else
228 MEMORY_PROFILING = --profile-memory
229 endif
230
231 # Property checking flags
232 #
233 # Each variable below controls a specific property checking flag
234 # within CBMC. If desired, a property flag can be disabled within
235 # a particular proof by nulling the corresponding variable when CBMC's default
236 # is not to perform such checks, or setting to --no-<CHECK>-check when CBMC's
237 # default is to perform such checks. For instance, the following lines:
238 #
239 # CBMC_FLAG_POINTER_CHECK = --no-pointer-check
240 # CBMC_FLAG_UNSIGNED_OVERFLOW_CHECK =
241 #
242 # would disable pointer checks and unsigned overflow checks with CBMC flag
243 # within:
244 # * an entire project when added to Makefile-project-defines
245 # * a specific proof when added to the harness Makefile
246
247 CBMC_FLAG_MALLOC_MAY_FAIL ?= # set to --no-malloc-may-fail to disable
248 CBMC_FLAG_BOUNDS_CHECK ?= # set to --no-bounds-check to disable
249 CBMC_FLAG_CONVERSION_CHECK ?= --conversion-check
250 CBMC_FLAG_DIV_BY_ZERO_CHECK ?= # set to --no-div-by-zero-check to disable
251 CBMC_FLAG_FLOAT_OVERFLOW_CHECK ?= --float-overflow-check
252 CBMC_FLAG_NAN_CHECK ?= --nan-check
253 CBMC_FLAG_POINTER_CHECK ?= #set to --no-pointer-check to disable
254 CBMC_FLAG_POINTER_OVERFLOW_CHECK ?= --pointer-overflow-check
255 CBMC_FLAG_POINTER_PRIMITIVE_CHECK ?= # set to --no-pointer-primitive-check to disable
256 CBMC_FLAG_SIGNED_OVERFLOW_CHECK ?= # set to --no-signed-overflow-check to disable
257 CBMC_FLAG_UNDEFINED_SHIFT_CHECK ?= # set to --no-undefined-shift-check to disable
258 CBMC_FLAG_UNSIGNED_OVERFLOW_CHECK ?= --unsigned-overflow-check
259 CBMC_FLAG_UNWINDING_ASSERTIONS ?= # set to --no-unwinding-assertions to disable
260 CBMC_DEFAULT_UNWIND ?= --unwind 1
261 CBMC_FLAG_FLUSH ?= --flush
262
263 # CBMC flags used for property checking and coverage checking
264
265 CBMCFLAGS += $(CBMC_FLAG_FLUSH)
266
267 # CBMC 6.0.0 enables all standard checks by default, which can make coverage analysis
268 # very slow. See https://github.com/diffblue/cbmc/issues/8389
269 # For now, we disable these checks when generating coverage info.
270 COVERFLAGS ?= --no-standard-checks --malloc-may-fail --malloc-fail-null
271
272 # CBMC flags used for property checking
273
274 CHECKFLAGS += $(CBMC_FLAG_BOUNDS_CHECK)
275 CHECKFLAGS += $(CBMC_FLAG_CONVERSION_CHECK)
276 CHECKFLAGS += $(CBMC_FLAG_DIV_BY_ZERO_CHECK)
277 CHECKFLAGS += $(CBMC_FLAG_FLOAT_OVERFLOW_CHECK)
278 CHECKFLAGS += $(CBMC_FLAG_NAN_CHECK)
279 CHECKFLAGS += $(CBMC_FLAG_POINTER_CHECK)
280 CHECKFLAGS += $(CBMC_FLAG_POINTER_OVERFLOW_CHECK)
281 CHECKFLAGS += $(CBMC_FLAG_POINTER_PRIMITIVE_CHECK)
282 CHECKFLAGS += $(CBMC_FLAG_SIGNED_OVERFLOW_CHECK)
283 CHECKFLAGS += $(CBMC_FLAG_UNDEFINED_SHIFT_CHECK)
284 CHECKFLAGS += $(CBMC_FLAG_UNSIGNED_OVERFLOW_CHECK)
285
286 # Additional CBMC flag to CBMC control verbosity.
287 #
288 # Meaningful values are
289 # 0 none
290 # 1 only errors
291 # 2 + warnings
292 # 4 + results
293 # 6 + status/phase information
294 # 8 + statistical information
295 # 9 + progress information
296 # 10 + debug info
297 #
298 # Uncomment the following line or set in Makefile-project-defines
299 # CBMC_VERBOSITY ?= --verbosity 4
300
301 # Additional CBMC flag to control how CBMC treats static variables.
302 #
303 # NONDET_STATIC is a list of flags of the form --nondet-static
304 # and --nondet-static-exclude VAR. The --nondet-static flag causes
305 # CBMC to initialize static variables with unconstrained value
306 # (ignoring initializers and default zero-initialization). The
307 # --nondet-static-exclude VAR excludes VAR for the variables
308 # initialized with unconstrained values.
309 NONDET_STATIC ?=
310
311 # Flags to pass to goto-cc for compilation and linking
312 COMPILE_FLAGS ?= -Wall -Werror
313 LINK_FLAGS ?= -Wall -Werror
314 EXPORT_FILE_LOCAL_SYMBOLS ?= --export-file-local-symbols
315
316 # During instrumentation, it adds models of C library functions
317 ADD_LIBRARY_FLAG := --add-library
318
319 # Preprocessor include paths -I...
320 INCLUDES ?=
321
322 # Preprocessor definitions -D...
323 DEFINES ?=
324
325 # CBMC object model
326 #
327 # CBMC_OBJECT_BITS is the number of bits in a pointer CBMC uses for
328 # the id of the object to which a pointer is pointing. CBMC uses 8
329 # bits for the object id by default. The remaining bits in the pointer
330 # are used for offset into the object. This limits the size of the
331 # objects that CBMC can model. This Makefile defines this bound on
332 # object size to be CBMC_MAX_OBJECT_SIZE. You are likely to get
333 # unexpected results if you try to malloc an object larger than this
334 # bound.
335 CBMC_OBJECT_BITS ?= 8
336
337 # CBMC loop unwinding (Normally set in the proof Makefile)
338 #
339 # UNWINDSET is a list of pairs of the form foo.1:4 meaning that
340 # CBMC should unwind loop 1 in function foo no more than 4 times.
341 # For historical reasons, the number 4 is one more than the number
342 # of times CBMC actually unwinds the loop.
343 UNWINDSET ?=
344
345 # CBMC early loop unwinding (Normally set in the proof Makefile)
346 #
347 # Most users can ignore this variable.
348 #
349 # This variable exists to support the use of loop and function
350 # contracts, two features under development for CBMC. Checking the
351 # assigns clause for function contracts and loop invariants currently
352 # assumes loop-free bodies for loops and functions with contracts
353 # (possibly after replacing nested loops with their own loop
354 # contracts). To satisfy this requirement, it may be necessary to
355 # unwind some loops before the function contract and loop invariant
356 # transformations are applied to the goto program. This variable
357 # CPROVER_LIBRARY_UNWINDSET is identical to UNWINDSET, and we assume that the
358 # loops mentioned in CPROVER_LIBRARY_UNWINDSET and UNWINDSET are disjoint.
359 CPROVER_LIBRARY_UNWINDSET ?=
360
361 # CBMC function removal (Normally set set in the proof Makefile)
362 #
363 # REMOVE_FUNCTION_BODY is a list of function names. CBMC will "undefine"
364 # the function, and CBMC will treat the function as having no side effects
365 # and returning an unconstrained value of the appropriate return type.
366 # The list should include the names of functions being stubbed out.
367 REMOVE_FUNCTION_BODY ?=
368
369 # CBMC function pointer restriction (Normally set in the proof Makefile)
370 #
371 # RESTRICT_FUNCTION_POINTER is a list of function pointer restriction
372 # instructions of the form:
373 #
374 # <fun_id>.function_pointer_call.<N>/<fun_id>[,<fun_id>]*
375 #
376 # The function pointer call number <N> in the specified function gets
377 # rewritten to a case switch over a finite list of functions.
378 # If some possible target functions are omitted from the list a counter
379 # example trace will be found by CBMC, i.e. the transformation is sound.
380 # If the target functions are file-local symbols, then mangled names must
381 # be used.
382 RESTRICT_FUNCTION_POINTER ?=
383
384 # The project source files (Normally set set in the proof Makefile)
385 #
386 # PROJECT_SOURCES is the list of project source files to compile,
387 # including the source file defining the function under test.
388 PROJECT_SOURCES ?=
389
390 # The proof source files (Normally set in the proof Makefile)
391 #
392 # PROOF_SOURCES is the list of proof source files to compile, including
393 # the proof harness, and including any function stubs being used.
394 PROOF_SOURCES ?=
395
396 # The number of seconds that CBMC should be allowed to run for before
397 # being forcefully terminated. Currently, this is set to be less than
398 # the time limit for a CodeBuild job, which is eight hours. If a proof
399 # run takes longer than the time limit of the CI environment, the
400 # environment will halt the proof run without updating the Litani
401 # report, making the proof run appear to "hang".
402 CBMC_TIMEOUT ?= 21600
403
404 # CBMC string abstraction
405 #
406 # Replace all uses of char * by a struct that carries that string,
407 # and also the underlying allocation and the C string length.
408 STRING_ABSTRACTION ?=
409 ifdef STRING_ABSTRACTION
410 ifneq ($(strip $(STRING_ABSTRACTION)),)
411 CBMC_STRING_ABSTRACTION := --string-abstraction
412 endif
413 endif
414
415 # Optional configuration library flags
416 OPT_CONFIG_LIBRARY ?=
417 CBMC_OPT_CONFIG_LIBRARY := $(CBMC_FLAG_MALLOC_MAY_FAIL) $(CBMC_STRING_ABSTRACTION)
418
419 # Proof writers could add function contracts in their source code.
420 # These contracts are ignored by default, but may be enabled in two distinct
421 # contexts using the following two variables:
422 # 1. To check whether one or more function contracts are sound with respect to
423 # the function implementation, CHECK_FUNCTION_CONTRACTS should be a list of
424 # function names. Use CHECK_FUNCTION_CONTRACTS_REC to check contracts on
425 # recursive functions.
426 # 2. To replace calls to certain functions with their correspondent function
427 # contracts, USE_FUNCTION_CONTRACTS should be a list of function names.
428 # One must check separately whether a function contract is sound before
429 # replacing it in calling contexts.
430 CHECK_FUNCTION_CONTRACTS ?=
431 CBMC_CHECK_FUNCTION_CONTRACTS := $(patsubst %,--enforce-contract %, $(CHECK_FUNCTION_CONTRACTS))
432
433 CHECK_FUNCTION_CONTRACTS_REC ?=
434 CBMC_CHECK_FUNCTION_CONTRACTS_REC := $(patsubst %,--enforce-contract-rec %, $(CHECK_FUNCTION_CONTRACTS_REC))
435
436 USE_FUNCTION_CONTRACTS ?=
437 CBMC_USE_FUNCTION_CONTRACTS := $(patsubst %,--replace-call-with-contract %, $(USE_FUNCTION_CONTRACTS))
438
439 CODE_CONTRACTS := $(CHECK_FUNCTION_CONTRACTS)$(USE_FUNCTION_CONTRACTS)$(APPLY_LOOP_CONTRACTS)
440
441 # Proof writers may also apply function contracts using the Dynamic Frame
442 # Condition Checking (DFCC) mode. For more information on DFCC,
443 # please see https://diffblue.github.io/cbmc/contracts-dev-spec-dfcc.html.
444 USE_DYNAMIC_FRAMES ?=
445 ifdef USE_DYNAMIC_FRAMES
446 ifneq ($(strip $(USE_DYNAMIC_FRAMES)),)
447 CBMC_USE_DYNAMIC_FRAMES := $(CBMC_OPT_CONFIG_LIBRARY) --dfcc $(HARNESS_ENTRY) $(CBMC_CHECK_FUNCTION_CONTRACTS_REC)
448 endif
449 endif
450
451 # Similarly, proof writers could also add loop contracts in their source code
452 # to obtain unbounded correctness proofs. Unlike function contracts, loop
453 # contracts are not reusable and thus are checked and used simultaneously.
454 # These contracts are also ignored by default, but may be enabled by setting
455 # the APPLY_LOOP_CONTRACTS variable.
456 APPLY_LOOP_CONTRACTS ?=
457 ifdef APPLY_LOOP_CONTRACTS
458 ifneq ($(strip $(APPLY_LOOP_CONTRACTS)),)
459 CBMC_APPLY_LOOP_CONTRACTS := --apply-loop-contracts
460 endif
461 endif
462
463 # The default unwind should only be used in DFCC mode without loop contracts.
464 # When loop contracts are applied, we only unwind specified loops.
465 # If any loops remain after loop contracts have been applied, CBMC might try
466 # to unwind the program indefinitely, because we do not pass default unwind
467 # (i.e., --unwind 1) to CBMC when in DFCC mode.
468 # We must not use a default unwind command in DFCC mode, because contract instrumentation
469 # introduces loops encoding write set inclusion checks that must be dynamically unwound during
470 # symex.
471 ifneq ($(strip $(USE_DYNAMIC_FRAMES)),)
472 ifneq ($(strip $(APPLY_LOOP_CONTRACTS)),)
473 UNWIND_0500_FLAGS=$(CBMC_UNWINDSET) $(CBMC_CPROVER_LIBRARY_UNWINDSET) $(CBMC_FLAG_UNWINDING_ASSERTIONS)
474 UNWIND_0500_DESC="$(PROOF_UID): unwinding specified subset of loops"
475 else
476 UNWIND_0500_FLAGS=$(CBMC_UNWINDSET) $(CBMC_CPROVER_LIBRARY_UNWINDSET) $(CBMC_DEFAULT_UNWIND) $(CBMC_FLAG_UNWINDING_ASSERTIONS)
477 UNWIND_0500_DESC="$(PROOF_UID): unwinding all loops"
478 endif
479 endif
480
481 # Silence makefile output (eg, long litani commands) unless VERBOSE is set.
482 ifndef VERBOSE
483 MAKEFLAGS := $(MAKEFLAGS) -s
484 endif
485
486 ################################################################
487 ################################################################
488 ## Section II: This section defines the process of running a proof
489 ##
490 ## There should be no reason to edit anything below this line.
491
492 ################################################################
493 # Paths
494
495 CBMC ?= cbmc
496 GOTO_ANALYZER ?= goto-analyzer
497 GOTO_CC ?= goto-cc
498 GOTO_INSTRUMENT ?= goto-instrument
499 CRANGLER ?= crangler
500 VIEWER ?= cbmc-viewer
501 VIEWER2 ?= cbmc-viewer
502 CMAKE ?= cmake
503
504 GOTODIR ?= $(PROOFDIR)/gotos
505 LOGDIR ?= $(PROOFDIR)/logs
506
507 PROJECT ?= project
508 PROOF ?= proof
509
510 HARNESS_GOTO ?= $(GOTODIR)/$(HARNESS_FILE)
511 PROJECT_GOTO ?= $(GOTODIR)/$(PROJECT)
512 PROOF_GOTO ?= $(GOTODIR)/$(PROOF)
513
514 ################################################################
515 # Useful macros for values that are hard to reference
516 SPACE :=$() $()
517 COMMA :=,
518
519 ################################################################
520 # Set C compiler defines
521
522 CBMCFLAGS += --object-bits $(CBMC_OBJECT_BITS)
523
524 DEFINES += -DCBMC=1
525 DEFINES += -DCBMC_OBJECT_BITS=$(CBMC_OBJECT_BITS)
526 DEFINES += -DCBMC_MAX_OBJECT_SIZE="(SIZE_MAX>>(CBMC_OBJECT_BITS+1))"
527
528 # CI currently assumes cbmc invocation has at most one --unwindset
529
530 # UNWINDSET is designed for user code (i.e., proof and project code)
531 ifdef UNWINDSET
532 ifneq ($(strip $(UNWINDSET)),)
533 CBMC_UNWINDSET := --unwindset $(subst $(SPACE),$(COMMA),$(strip $(UNWINDSET)))
534 endif
535 endif
536
537 # CPROVER_LIBRARY_UNWINDSET is designed for CPROVER library functions
538 ifdef CPROVER_LIBRARY_UNWINDSET
539 ifneq ($(strip $(CPROVER_LIBRARY_UNWINDSET)),)
540 CBMC_CPROVER_LIBRARY_UNWINDSET := --unwindset $(subst $(SPACE),$(COMMA),$(strip $(CPROVER_LIBRARY_UNWINDSET)))
541 endif
542 endif
543
544 CBMC_REMOVE_FUNCTION_BODY := $(patsubst %,--remove-function-body %, $(REMOVE_FUNCTION_BODY))
545
546 ifdef RESTRICT_FUNCTION_POINTER
547 ifneq ($(strip $(RESTRICT_FUNCTION_POINTER)),)
548 CBMC_RESTRICT_FUNCTION_POINTER := $(patsubst %,--restrict-function-pointer %, $(RESTRICT_FUNCTION_POINTER))
549 endif
550 endif
551
552 ################################################################
553 # Targets for rewriting source files with crangler
554
555 # Construct crangler configuration files
556 #
557 # REWRITTEN_SOURCES is a list of crangler output files source.i.
558 # This target assumes that for each source.i
559 # * source.i_SOURCE is the path to a source file,
560 # * source.i_FUNCTIONS is a list of functions (may be empty)
561 # * source.i_OBJECTS is a list of variables (may be empty)
562 # This target constructs the crangler configuration file source.i.json
563 # of the form
564 # {
565 # "sources": [ "/proj/code.c" ],
566 # "includes": [ "/proj/include" ],
567 # "defines": [ "VAR=1" ],
568 # "functions": [ {"function_name": ["remove static"]} ],
569 # "objects": [ {"variable_name": ["remove static"]} ],
570 # "output": "source.i"
571 # }
572 # to remove the static attribute from function_name and variable_name
573 # in the source file source.c and write the result to source.i.
574 #
575 # This target assumes that filenames include no spaces and that
576 # the INCLUDES and DEFINES variables include no spaces after -I
577 # and -D. For example, use "-DVAR=1" and not "-D VAR=1".
578 #
579 # Define *_SOURCE, *_FUNCTIONS, and *_OBJECTS in the proof Makefile.
580 # The string source.i is usually an absolute path $(PROOFDIR)/code.i
581 # to a file in the proof directory that contains the proof Makefile.
582 # The proof Makefile usually includes the definitions
583 # $(PROOFDIR)/code.i_SOURCE = /proj/code.c
584 # $(PROOFDIR)/code.i_FUNCTIONS = function_name
585 # $(PROOFDIR)/code.i_OBJECTS = variable_name
586 # Because these definitions refer to PROOFDIR that is defined in this
587 # Makefile.common, these definitions must appear after the inclusion
588 # of Makefile.common in the proof Makefile.
589 #
590 $(foreach rs,$(REWRITTEN_SOURCES),$(eval $(rs).json: $($(rs)_SOURCE)))
591 $(foreach rs,$(REWRITTEN_SOURCES),$(rs).json):
592 echo '{'\
593 '"sources": ['\
594 '"$($(@:.json=)_SOURCE)"'\
595 '],'\
596 '"includes": ['\
597 '$(subst $(SPACE),$(COMMA),$(patsubst -I%,"%",$(strip $(INCLUDES))))' \
598 '],'\
599 '"defines": ['\
600 '$(subst $(SPACE),$(COMMA),$(patsubst -D%,"%",$(subst ",\",$(strip $(DEFINES)))))' \
601 '],'\
602 '"functions": ['\
603 '{'\
604 '$(subst ~, ,$(subst $(SPACE),$(COMMA),$(patsubst %,"%":["remove~static"],$($(@:.json=)_FUNCTIONS))))' \
605 '}'\
606 '],'\
607 '"objects": ['\
608 '{'\
609 '$(subst ~, ,$(subst $(SPACE),$(COMMA),$(patsubst %,"%":["remove~static"],$($(@:.json=)_OBJECTS))))' \
610 '}'\
611 '],'\
612 '"output": "$(@:.json=)"'\
613 '}' > $@
614
615 # Rewrite source files with crangler
616 #
617 $(foreach rs,$(REWRITTEN_SOURCES),$(eval $(rs): $(rs).json))
618 $(REWRITTEN_SOURCES):
619 $(LITANI) add-job \
620 --command \
621 '$(CRANGLER) [email protected]' \
622 --inputs $($@_SOURCE) \
623 --outputs $@ \
624 --stdout-file $(LOGDIR)/crangler-$(subst /,_,$(subst .,_,$@))-log.txt \
625 --interleave-stdout-stderr \
626 --pipeline-name "$(PROOF_UID)" \
627 --ci-stage build \
628 --description "$(PROOF_UID): removing static"
629
630 ################################################################
631 # Build targets that make the relevant .goto files
632
633 # Compile project sources
634 $(PROJECT_GOTO)0100.goto: $(PROJECT_SOURCES) $(REWRITTEN_SOURCES)
635 $(LITANI) add-job \
636 --command \
637 '$(GOTO_CC) $(CBMC_VERBOSITY) $(COMPILE_FLAGS) $(EXPORT_FILE_LOCAL_SYMBOLS) $(INCLUDES) $(DEFINES) $^ -o $@' \
638 --inputs $^ \
639 --outputs $@ \
640 --stdout-file $(LOGDIR)/project_sources-log.txt \
641 --pipeline-name "$(PROOF_UID)" \
642 --ci-stage build \
643 --description "$(PROOF_UID): building project binary"
644
645 # Compile proof sources
646 $(PROOF_GOTO)0100.goto: $(PROOF_SOURCES)
647 $(LITANI) add-job \
648 --command \
649 '$(GOTO_CC) $(CBMC_VERBOSITY) $(COMPILE_FLAGS) $(EXPORT_FILE_LOCAL_SYMBOLS) $(INCLUDES) $(DEFINES) $^ -o $@' \
650 --inputs $^ \
651 --outputs $@ \
652 --stdout-file $(LOGDIR)/proof_sources-log.txt \
653 --pipeline-name "$(PROOF_UID)" \
654 --ci-stage build \
655 --description "$(PROOF_UID): building proof binary"
656
657 # Remove function bodies from project sources
658 $(PROJECT_GOTO)0200.goto: $(PROJECT_GOTO)0100.goto
659 $(LITANI) add-job \
660 --command \
661 '$(GOTO_INSTRUMENT) $(CBMC_VERBOSITY) $(CBMC_REMOVE_FUNCTION_BODY) $^ $@' \
662 --inputs $^ \
663 --outputs $@ \
664 --stdout-file $(LOGDIR)/remove_function_body-log.txt \
665 --pipeline-name "$(PROOF_UID)" \
666 --ci-stage build \
667 --description "$(PROOF_UID): removing function bodies from project sources"
668
669 # Link project and proof sources into the proof harness
670 $(HARNESS_GOTO)0100.goto: $(PROOF_GOTO)0100.goto $(PROJECT_GOTO)0200.goto
671 $(LITANI) add-job \
672 --command '$(GOTO_CC) $(CBMC_VERBOSITY) --function $(HARNESS_ENTRY) $^ $(LINK_FLAGS) -o $@' \
673 --inputs $^ \
674 --outputs $@ \
675 --stdout-file $(LOGDIR)/link_proof_project-log.txt \
676 --pipeline-name "$(PROOF_UID)" \
677 --ci-stage build \
678 --description "$(PROOF_UID): linking project to proof"
679
680 # Restrict function pointers
681 $(HARNESS_GOTO)0200.goto: $(HARNESS_GOTO)0100.goto
682 $(LITANI) add-job \
683 --command \
684 '$(GOTO_INSTRUMENT) $(CBMC_VERBOSITY) $(CBMC_RESTRICT_FUNCTION_POINTER) --remove-function-pointers $^ $@' \
685 --inputs $^ \
686 --outputs $@ \
687 --stdout-file $(LOGDIR)/restrict_function_pointer-log.txt \
688 --pipeline-name "$(PROOF_UID)" \
689 --ci-stage build \
690 --description "$(PROOF_UID): restricting function pointers in project sources"
691
692 # Fill static variable with unconstrained values
693 $(HARNESS_GOTO)0300.goto: $(HARNESS_GOTO)0200.goto
694 ifneq ($(strip $(CODE_CONTRACTS)),)
695 $(LITANI) add-job \
696 --command 'cp $^ $@' \
697 --inputs $^ \
698 --outputs $@ \
699 --stdout-file $(LOGDIR)/nondet_static-log.txt \
700 --pipeline-name "$(PROOF_UID)" \
701 --ci-stage build \
702 --description "$(PROOF_UID): not setting static variables to nondet (will do during contract instrumentation)"
703 else
704 $(LITANI) add-job \
705 --command \
706 '$(GOTO_INSTRUMENT) $(CBMC_VERBOSITY) $(NONDET_STATIC) $^ $@' \
707 --inputs $^ \
708 --outputs $@ \
709 --stdout-file $(LOGDIR)/nondet_static-log.txt \
710 --pipeline-name "$(PROOF_UID)" \
711 --ci-stage build \
712 --description "$(PROOF_UID): setting static variables to nondet"
713 endif
714
715 # Link CPROVER library if DFCC mode is on
716 $(HARNESS_GOTO)0400.goto: $(HARNESS_GOTO)0300.goto
717 ifneq ($(strip $(USE_DYNAMIC_FRAMES)),)
718 $(LITANI) add-job \
719 --command \
720 '$(GOTO_INSTRUMENT) $(CBMC_VERBOSITY) $(ADD_LIBRARY_FLAG) $(CBMC_OPT_CONFIG_LIBRARY) $^ $@' \
721 --inputs $^ \
722 --outputs $@ \
723 --stdout-file $(LOGDIR)/linking-library-models-log.txt \
724 --pipeline-name "$(PROOF_UID)" \
725 --ci-stage build \
726 --description "$(PROOF_UID): linking CPROVER library"
727 else
728 $(LITANI) add-job \
729 --command 'cp $^ $@' \
730 --inputs $^ \
731 --outputs $@ \
732 --stdout-file $(LOGDIR)/linking-library-models-log.txt \
733 --pipeline-name "$(PROOF_UID)" \
734 --ci-stage build \
735 --description "$(PROOF_UID): not linking CPROVER library"
736 endif
737
738 # Early unwind all loops on DFCC mode; otherwise, only unwind loops in proof and project code
739 $(HARNESS_GOTO)0500.goto: $(HARNESS_GOTO)0400.goto
740 ifneq ($(strip $(USE_DYNAMIC_FRAMES)),)
741 $(LITANI) add-job \
742 --command \
743 '$(GOTO_INSTRUMENT) $(CBMC_VERBOSITY) $(UNWIND_0500_FLAGS) $^ $@' \
744 --inputs $^ \
745 --outputs $@ \
746 --stdout-file $(LOGDIR)/unwind_loops-log.txt \
747 --pipeline-name "$(PROOF_UID)" \
748 --ci-stage build \
749 --description $(UNWIND_0500_DESC)
750 else ifneq ($(strip $(CODE_CONTRACTS)),)
751 $(LITANI) add-job \
752 --command \
753 '$(GOTO_INSTRUMENT) $(CBMC_VERBOSITY) $(CBMC_UNWINDSET) $(CBMC_FLAG_UNWINDING_ASSERTIONS) $^ $@' \
754 --inputs $^ \
755 --outputs $@ \
756 --stdout-file $(LOGDIR)/unwind_loops-log.txt \
757 --pipeline-name "$(PROOF_UID)" \
758 --ci-stage build \
759 --description "$(PROOF_UID): unwinding loops in proof and project code"
760 else
761 $(LITANI) add-job \
762 --command 'cp $^ $@' \
763 --inputs $^ \
764 --outputs $@ \
765 --stdout-file $(LOGDIR)/unwind_loops-log.txt \
766 --pipeline-name "$(PROOF_UID)" \
767 --ci-stage build \
768 --description "$(PROOF_UID): not unwinding loops"
769 endif
770
771 # Replace function contracts, check function contracts, instrument for loop contracts
772 $(HARNESS_GOTO)0600.goto: $(HARNESS_GOTO)0500.goto
773 $(LITANI) add-job \
774 --command \
775 '$(GOTO_INSTRUMENT) $(CBMC_USE_DYNAMIC_FRAMES) $(NONDET_STATIC) $(CBMC_VERBOSITY) $(CBMC_CHECK_FUNCTION_CONTRACTS) $(CBMC_USE_FUNCTION_CONTRACTS) $(CBMC_APPLY_LOOP_CONTRACTS) $^ $@' \
776 --inputs $^ \
777 --outputs $@ \
778 --stdout-file $(LOGDIR)/check_function_contracts-log.txt \
779 --pipeline-name "$(PROOF_UID)" \
780 --ci-stage build \
781 --description "$(PROOF_UID): checking function contracts"
782
783 # Omit initialization of unused global variables (reduces problem size)
784 $(HARNESS_GOTO)0700.goto: $(HARNESS_GOTO)0600.goto
785 $(LITANI) add-job \
786 --command \
787 '$(GOTO_INSTRUMENT) $(CBMC_VERBOSITY) --slice-global-inits $^ $@' \
788 --inputs $^ \
789 --outputs $@ \
790 --stdout-file $(LOGDIR)/slice_global_inits-log.txt \
791 --pipeline-name "$(PROOF_UID)" \
792 --ci-stage build \
793 --description "$(PROOF_UID): slicing global initializations"
794
795 # Omit unused functions (sharpens coverage calculations)
796 $(HARNESS_GOTO)0800.goto: $(HARNESS_GOTO)0700.goto
797 $(LITANI) add-job \
798 --command \
799 '$(GOTO_INSTRUMENT) $(CBMC_VERBOSITY) --drop-unused-functions $^ $@' \
800 --inputs $^ \
801 --outputs $@ \
802 --stdout-file $(LOGDIR)/drop_unused_functions-log.txt \
803 --pipeline-name "$(PROOF_UID)" \
804 --ci-stage build \
805 --description "$(PROOF_UID): dropping unused functions"
806
807 # Final name for proof harness
808 $(HARNESS_GOTO).goto: $(HARNESS_GOTO)0800.goto
809 $(LITANI) add-job \
810 --command 'cp $< $@' \
811 --inputs $^ \
812 --outputs $@ \
813 --pipeline-name "$(PROOF_UID)" \
814 --ci-stage build \
815 --description "$(PROOF_UID): copying final goto-binary"
816
817 ################################################################
818 # Targets to run the analysis commands
819
820 ifdef CBMCFLAGS
821 ifeq ($(strip $(CODE_CONTRACTS)),)
822 CBMCFLAGS += $(CBMC_UNWINDSET) $(CBMC_CPROVER_LIBRARY_UNWINDSET) $(CBMC_DEFAULT_UNWIND) $(CBMC_OPT_CONFIG_LIBRARY)
823 else ifeq ($(strip $(USE_DYNAMIC_FRAMES)),)
824 CBMCFLAGS += $(CBMC_CPROVER_LIBRARY_UNWINDSET) $(CBMC_OPT_CONFIG_LIBRARY)
825 endif
826 endif
827
828 $(LOGDIR)/result.xml: $(HARNESS_GOTO).goto
829 $(LITANI) add-job \
830 $(POOL) \
831 --command \
832 '$(CBMC) $(CBMC_VERBOSITY) $(CBMCFLAGS) $(CBMC_FLAG_UNWINDING_ASSERTIONS) $(CHECKFLAGS) --trace --xml-ui $<' \
833 --inputs $^ \
834 --outputs $@ \
835 --ci-stage test \
836 --stdout-file $@ \
837 $(MEMORY_PROFILING) \
838 --ignore-returns 10 \
839 --timeout $(CBMC_TIMEOUT) \
840 --pipeline-name "$(PROOF_UID)" \
841 --tags "stats-group:safety checks" \
842 --stderr-file $(LOGDIR)/result-err-log.txt \
843 --description "$(PROOF_UID): checking safety properties"
844
845 $(LOGDIR)/result.txt: $(HARNESS_GOTO).goto
846 $(LITANI) add-job \
847 $(POOL) \
848 --command \
849 '$(CBMC) $(CBMC_VERBOSITY) $(CBMCFLAGS) $(CBMC_FLAG_UNWINDING_ASSERTIONS) $(CHECKFLAGS) --trace $<' \
850 --inputs $^ \
851 --outputs $@ \
852 --ci-stage test \
853 --stdout-file $@ \
854 $(MEMORY_PROFILING) \
855 --ignore-returns 10 \
856 --timeout $(CBMC_TIMEOUT) \
857 --pipeline-name "$(PROOF_UID)" \
858 --tags "stats-group:safety checks" \
859 --stderr-file $(LOGDIR)/result-err-log.txt \
860 --description "$(PROOF_UID): checking safety properties"
861
862 $(LOGDIR)/property.xml: $(HARNESS_GOTO).goto
863 $(LITANI) add-job \
864 --command \
865 '$(CBMC) $(CBMC_VERBOSITY) $(CBMCFLAGS) $(CBMC_FLAG_UNWINDING_ASSERTIONS) $(CHECKFLAGS) --show-properties --xml-ui $<' \
866 --inputs $^ \
867 --outputs $@ \
868 --ci-stage test \
869 --stdout-file $@ \
870 --ignore-returns 10 \
871 --pipeline-name "$(PROOF_UID)" \
872 --stderr-file $(LOGDIR)/property-err-log.txt \
873 --description "$(PROOF_UID): printing safety properties"
874
875 $(LOGDIR)/coverage.xml: $(HARNESS_GOTO).goto
876 $(LITANI) add-job \
877 $(POOL) \
878 --command \
879 '$(CBMC) $(CBMC_VERBOSITY) $(CBMCFLAGS) $(COVERFLAGS) --cover location --xml-ui $<' \
880 --inputs $^ \
881 --outputs $@ \
882 --ci-stage test \
883 --stdout-file $@ \
884 $(MEMORY_PROFILING) \
885 --ignore-returns 10 \
886 --timeout $(CBMC_TIMEOUT) \
887 --pipeline-name "$(PROOF_UID)" \
888 --tags "stats-group:coverage computation" \
889 --stderr-file $(LOGDIR)/coverage-err-log.txt \
890 --description "$(PROOF_UID): calculating coverage"
891
892 COVERAGE ?= $(LOGDIR)/coverage.xml
893 VIEWER_COVERAGE_FLAG ?= --coverage $(COVERAGE)
894
895 $(PROOFDIR)/report: $(LOGDIR)/result.xml $(LOGDIR)/property.xml $(COVERAGE)
896 $(LITANI) add-job \
897 --command " $(VIEWER) \
898 --result $(LOGDIR)/result.xml \
899 $(VIEWER_COVERAGE_FLAG) \
900 --property $(LOGDIR)/property.xml \
901 --srcdir $(SRCDIR) \
902 --goto $(HARNESS_GOTO).goto \
903 --reportdir $(PROOFDIR)/report \
904 --config $(PROOFDIR)/cbmc-viewer.json" \
905 --inputs $^ \
906 --outputs $(PROOFDIR)/report \
907 --pipeline-name "$(PROOF_UID)" \
908 --stdout-file $(LOGDIR)/viewer-log.txt \
909 --ci-stage report \
910 --description "$(PROOF_UID): generating report"
911
912 litani-path:
913 @echo $(LITANI)
914
915 # ##############################################################
916 # Phony Rules
917 #
918 # These rules provide a convenient way to run a single proof up to a
919 # certain stage. Users can browse into a proof directory and run
920 # "make -Bj 3 report" to generate a report for just that proof, or
921 # "make goto" to build the goto binary. Under the hood, this runs litani
922 # for just that proof.
923
924 _goto: $(HARNESS_GOTO).goto
925 goto:
926 @ echo Running 'litani init'
927 $(LITANI) init $(INIT_POOLS) --project $(PROJECT_NAME)
928 @ echo Running 'litani add-job'
929 $(MAKE) -B _goto
930 @ echo Running 'litani build'
931 $(LITANI) run-build
932
933 _result: $(LOGDIR)/result.txt
934 result:
935 @ echo Running 'litani init'
936 $(LITANI) init $(INIT_POOLS) --project $(PROJECT_NAME)
937 @ echo Running 'litani add-job'
938 $(MAKE) -B _result
939 @ echo Running 'litani build'
940 $(LITANI) run-build
941
942 _property: $(LOGDIR)/property.xml
943 property:
944 @ echo Running 'litani init'
945 $(LITANI) init $(INIT_POOLS) --project $(PROJECT_NAME)
946 @ echo Running 'litani add-job'
947 $(MAKE) -B _property
948 @ echo Running 'litani build'
949 $(LITANI) run-build
950
951 _coverage: $(LOGDIR)/coverage.xml
952 coverage:
953 @ echo Running 'litani init'
954 $(LITANI) init $(INIT_POOLS) --project $(PROJECT_NAME)
955 @ echo Running 'litani add-job'
956 $(MAKE) -B _coverage
957 @ echo Running 'litani build'
958 $(LITANI) run-build
959
960 _report: $(PROOFDIR)/report
961 report:
962 @ echo Running 'litani init'
963 $(LITANI) init $(INIT_POOLS) --project $(PROJECT_NAME)
964 @ echo Running 'litani add-job'
965 $(MAKE) -B _report
966 @ echo Running 'litani build'
967 $(LITANI) run-build
968
969 _report_no_coverage:
970 $(MAKE) COVERAGE="" VIEWER_COVERAGE_FLAG="" _report
971 report-no-coverage:
972 $(MAKE) COVERAGE="" VIEWER_COVERAGE_FLAG=" " report
973
974 ################################################################
975 # Targets to clean up after ourselves
976 clean:
977 -$(RM) $(DEPENDENT_GOTOS)
978 -$(RM) TAGS*
979 -$(RM) *~ \#*
980 -$(RM) $(REWRITTEN_SOURCES) $(foreach rs,$(REWRITTEN_SOURCES),$(rs).json)
981
982 veryclean: clean
983 -$(RM) -r report
984 -$(RM) -r $(LOGDIR) $(GOTODIR)
985
986 .PHONY: \
987 _coverage \
988 _goto \
989 _property \
990 _report \
991 _report_no_coverage \
992 clean \
993 coverage \
994 goto \
995 litani-path \
996 property \
997 report \
998 report-no-coverage \
999 result \
1000 setup_dependencies \
1001 testdeps \
1002 veryclean \
1003 #
1004
1005 ################################################################
1006
1007 # Run "make echo-proof-uid" to print the proof ID of a proof. This can be
1008 # used by scripts to ensure that every proof has an ID, that there are
1009 # no duplicates, etc.
1010
1011 .PHONY: echo-proof-uid
1012 echo-proof-uid:
1013 @echo $(PROOF_UID)
1014
1015 .PHONY: echo-project-name
1016 echo-project-name:
1017 @echo $(PROJECT_NAME)
1018
1019 ################################################################
1020
1021 # Project-specific targets requiring values defined above
1022 sinclude $(PROOF_ROOT)/Makefile-project-targets
1023
1024 # CI-specific targets to drive cbmc in CI
1025 sinclude $(PROOF_ROOT)/Makefile-project-testing
1026
1027 ################################################################