1#!/usr/bin/env python3
2#
3# Python script for preparing the code base for the CBMC proofs.
4#
5# Copyright (C) 2022 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25import logging
26import os
27import sys
28import textwrap
29from subprocess import CalledProcessError
30
31from make_common_makefile import main as make_common_file
32from make_configuration_directories import main as process_configurations
33from make_proof_makefiles import main as make_proof_files
34from make_cbmc_batch_files import create_cbmc_yaml_files
35
36CWD = os.getcwd()
37sys.path.append(os.path.normpath(os.path.join(CWD, "..", "patches")))
38
39#from compute_patch import create_patches
40#from compute_patch import DirtyGitError
41#from compute_patch import PatchCreationError
42from patches_constants import HEADERS
43
44from compute_patch import find_all_defines
45from compute_patch import manipulate_headerfile
46
47import patch
48
49PROOFS_DIR = os.path.dirname(os.path.abspath(__file__))
50
51LOGGER = logging.getLogger("PrepareLogger")
52
53################################################################
54
55def patch_headers(headers):
56    """Patch headers so we can define symbols on the command line.
57
58    When building for CBMC, it is convenient to define symbols on the
59    command line and know that these definitions will override the
60    definitions of the same symbols in header files.
61
62    The create_patches function takes a list of header files, searches
63    the Makefile.json files for symbols that will be defined in the
64    Makefiles, and creates patch files that protect the definition of
65    those symbols in header files with #ifndef/#endif.  In this way,
66    command line definitions will override header file definitions.
67
68    The create_patches function, however, depends on the fact that all
69    header files being modified are included in the top-level git
70    repository.  This assumption is violated if header files live in
71    submodules.
72
73    This function just updates the header files in place without
74    creating patch files.  One potential vulnerability of this
75    function is that it could cause preexisting patch files to fail if
76    they patch a file being modified here.
77    """
78    defines = find_all_defines()
79    for header in headers:
80        manipulate_headerfile(defines, header)
81
82################################################################
83
84def build():
85    process_configurations()
86    make_common_file()
87    make_proof_files()
88    try:
89        create_cbmc_yaml_files()
90    except CalledProcessError as e:
91        logging.error(textwrap.dedent("""\
92            An error occured during cbmc-batch generation.
93            The error message is: {}
94            """.format(str(e))))
95        exit(1)
96
97    # Patch headers directly instead of creating patch files.
98    patch.patch()
99    patch_headers(HEADERS)
100
101    #try:
102    #    create_patches(HEADERS)
103    #except (DirtyGitError, PatchCreationError) as e:
104    #    logging.error(textwrap.dedent("""\
105    #        An error occured during patch creation.
106    #        The error message is: {}
107    #        """.format(str(e))))
108    #    exit(1)
109
110################################################################
111
112if __name__ == '__main__':
113    logging.basicConfig(format="{script}: %(levelname)s %(message)s".format(
114        script=os.path.basename(__file__)))
115    build()
116