summaryrefslogtreecommitdiff
path: root/FreeRTOS/Test/CBMC/proofs/prepare.py
blob: 58f9903db109b5a2508f27221c877fb70e06881f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
#
# Python script for preparing the code base for the CBMC proofs.
#
# Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import logging
import os
import sys
import textwrap
from subprocess import CalledProcessError

from make_common_makefile import main as make_common_file
from make_configuration_directories import main as process_configurations
from make_proof_makefiles import main as make_proof_files
from make_cbmc_batch_files import create_cbmc_yaml_files

CWD = os.getcwd()
sys.path.append(os.path.normpath(os.path.join(CWD, "..", "patches")))

#from compute_patch import create_patches
#from compute_patch import DirtyGitError
#from compute_patch import PatchCreationError
from patches_constants import HEADERS

from compute_patch import find_all_defines
from compute_patch import manipulate_headerfile

import patch

PROOFS_DIR = os.path.dirname(os.path.abspath(__file__))

LOGGER = logging.getLogger("PrepareLogger")

################################################################

def patch_headers(headers):
    """Patch headers so we can define symbols on the command line.

    When building for CBMC, it is convenient to define symbols on the
    command line and know that these definitions will override the
    definitions of the same symbols in header files.

    The create_patches function takes a list of header files, searches
    the Makefile.json files for symbols that will be defined in the
    Makefiles, and creates patch files that protect the definition of
    those symbols in header files with #ifndef/#endif.  In this way,
    command line definitions will override header file definitions.

    The create_patches function, however, depends on the fact that all
    header files being modified are included in the top-level git
    repository.  This assumption is violated if header files live in
    submodules.

    This function just updates the header files in place without
    creating patch files.  One potential vulnerability of this
    function is that it could cause preexisting patch files to fail if
    they patch a file being modified here.
    """
    defines = find_all_defines()
    for header in headers:
        manipulate_headerfile(defines, header)

################################################################

def build():
    process_configurations()
    make_common_file()
    make_proof_files()
    try:
        create_cbmc_yaml_files()
    except CalledProcessError as e:
        logging.error(textwrap.dedent("""\
            An error occured during cbmc-batch generation.
            The error message is: {}
            """.format(str(e))))
        exit(1)

    # Patch headers directly instead of creating patch files.
    patch.patch()
    patch_headers(HEADERS)

    #try:
    #    create_patches(HEADERS)
    #except (DirtyGitError, PatchCreationError) as e:
    #    logging.error(textwrap.dedent("""\
    #        An error occured during patch creation.
    #        The error message is: {}
    #        """.format(str(e))))
    #    exit(1)

################################################################

if __name__ == '__main__':
    logging.basicConfig(format="{script}: %(levelname)s %(message)s".format(
        script=os.path.basename(__file__)))
    build()