Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright The Mbed TLS Contributors |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 18 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 19 | This script confirms that the naming of all symbols and identifiers in Mbed TLS |
Yuto Takano | 159255a | 2021-08-06 17:00:28 +0100 | [diff] [blame] | 20 | are consistent with the house style and are also self-consistent. It only runs |
| 21 | on Linux and macOS since it depends on nm. |
| 22 | |
| 23 | The script performs the following checks: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 24 | |
| 25 | - All exported and available symbols in the library object files, are explicitly |
Yuto Takano | 159255a | 2021-08-06 17:00:28 +0100 | [diff] [blame] | 26 | declared in the header files. This uses the nm command. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 27 | - All macros, constants, and identifiers (function names, struct names, etc) |
| 28 | follow the required pattern. |
| 29 | - Typo checking: All words that begin with MBED exist as macros or constants. |
Yuto Takano | fc54dfb | 2021-08-07 17:18:28 +0100 | [diff] [blame] | 30 | |
| 31 | Returns 0 on success, 1 on test failure, and 2 if there is a script error or a |
| 32 | subprocess error. Must be run from Mbed TLS root. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 33 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 34 | |
| 35 | import argparse |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 36 | import glob |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 37 | import textwrap |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 38 | import os |
| 39 | import sys |
| 40 | import traceback |
| 41 | import re |
| 42 | import shutil |
| 43 | import subprocess |
| 44 | import logging |
| 45 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 46 | # Naming patterns to check against. These are defined outside the NameCheck |
| 47 | # class for ease of modification. |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame] | 48 | MACRO_PATTERN = r"^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$" |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 49 | CONSTANTS_PATTERN = MACRO_PATTERN |
Yuto Takano | c183893 | 2021-08-05 19:52:09 +0100 | [diff] [blame] | 50 | IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$" |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 51 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 52 | class Match(): # pylint: disable=too-few-public-methods |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 53 | """ |
| 54 | A class representing a match, together with its found position. |
| 55 | |
| 56 | Fields: |
| 57 | * filename: the file that the match was in. |
| 58 | * line: the full line containing the match. |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 59 | * pos: a tuple of (line_no, start, end) positions on the file line where the |
| 60 | match is. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 61 | * name: the match itself. |
| 62 | """ |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 63 | def __init__(self, filename, line, pos, name): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 64 | self.filename = filename |
| 65 | self.line = line |
| 66 | self.pos = pos |
| 67 | self.name = name |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 68 | |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 69 | def __str__(self): |
Yuto Takano | 381fda8 | 2021-08-06 23:37:20 +0100 | [diff] [blame] | 70 | ln_str = str(self.pos[0]) |
| 71 | gutter_len = max(4, len(ln_str)) |
| 72 | gutter = (gutter_len - len(ln_str)) * " " + ln_str |
| 73 | underline = self.pos[1] * " " + (self.pos[2] - self.pos[1]) * "^" |
| 74 | |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 75 | return ( |
Yuto Takano | 381fda8 | 2021-08-06 23:37:20 +0100 | [diff] [blame] | 76 | " {0} |\n".format(gutter_len * " ") + |
| 77 | " {0} | {1}".format(gutter, self.line) + |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 78 | " {0} | {1}\n".format(gutter_len * " ", underline) |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 79 | ) |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 80 | |
| 81 | class Problem(): # pylint: disable=too-few-public-methods |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 82 | """ |
| 83 | A parent class representing a form of static analysis error. |
| 84 | |
| 85 | Fields: |
| 86 | * textwrapper: a TextWrapper instance to format problems nicely. |
| 87 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 88 | def __init__(self): |
| 89 | self.textwrapper = textwrap.TextWrapper() |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 90 | self.textwrapper.width = 80 |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 91 | self.textwrapper.initial_indent = " > " |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 92 | self.textwrapper.subsequent_indent = " " |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 93 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 94 | class SymbolNotInHeader(Problem): # pylint: disable=too-few-public-methods |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 95 | """ |
| 96 | A problem that occurs when an exported/available symbol in the object file |
| 97 | is not explicitly declared in header files. Created with |
| 98 | NameCheck.check_symbols_declared_in_header() |
| 99 | |
| 100 | Fields: |
| 101 | * symbol_name: the name of the symbol. |
| 102 | """ |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 103 | def __init__(self, symbol_name, quiet=False): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 104 | self.symbol_name = symbol_name |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 105 | self.quiet = quiet |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 106 | Problem.__init__(self) |
| 107 | |
| 108 | def __str__(self): |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 109 | if self.quiet: |
| 110 | return "{0}".format(self.symbol_name) |
| 111 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 112 | return self.textwrapper.fill( |
| 113 | "'{0}' was found as an available symbol in the output of nm, " |
| 114 | "however it was not declared in any header files." |
| 115 | .format(self.symbol_name)) |
| 116 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 117 | class PatternMismatch(Problem): # pylint: disable=too-few-public-methods |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 118 | """ |
| 119 | A problem that occurs when something doesn't match the expected pattern. |
| 120 | Created with NameCheck.check_match_pattern() |
| 121 | |
| 122 | Fields: |
| 123 | * pattern: the expected regex pattern |
| 124 | * match: the Match object in question |
| 125 | """ |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 126 | def __init__(self, pattern, match, quiet=False): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 127 | self.pattern = pattern |
| 128 | self.match = match |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 129 | self.quiet = quiet |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 130 | Problem.__init__(self) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 131 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 132 | def __str__(self): |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 133 | if self.quiet: |
| 134 | return ("{0}:{1}:{3}" |
| 135 | .format( |
| 136 | self.match.filename, |
| 137 | self.match.pos[0], |
| 138 | self.match.name)) |
| 139 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 140 | return self.textwrapper.fill( |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 141 | "{0}:{1}: '{2}' does not match the required pattern '{3}'." |
| 142 | .format( |
| 143 | self.match.filename, |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 144 | self.match.pos[0], |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 145 | self.match.name, |
| 146 | self.pattern)) + "\n" + str(self.match) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 147 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 148 | class Typo(Problem): # pylint: disable=too-few-public-methods |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 149 | """ |
| 150 | A problem that occurs when a word using MBED doesn't appear to be defined as |
| 151 | constants nor enum values. Created with NameCheck.check_for_typos() |
| 152 | |
| 153 | Fields: |
| 154 | * match: the Match object of the MBED name in question. |
| 155 | """ |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 156 | def __init__(self, match, quiet=False): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 157 | self.match = match |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 158 | self.quiet = quiet |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 159 | Problem.__init__(self) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 160 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 161 | def __str__(self): |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 162 | if self.quiet: |
| 163 | return ("{0}:{1}:{2}" |
| 164 | .format( |
| 165 | self.match.filename, |
| 166 | self.match.pos[0], |
| 167 | self.match.name)) |
| 168 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 169 | return self.textwrapper.fill( |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 170 | "{0}:{1}: '{2}' looks like a typo. It was not found in any " |
| 171 | "macros or any enums. If this is not a typo, put " |
| 172 | "//no-check-names after it." |
| 173 | .format( |
| 174 | self.match.filename, |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 175 | self.match.pos[0], |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 176 | self.match.name)) + "\n" + str(self.match) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 177 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 178 | class NameCheck(): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 179 | """ |
| 180 | Representation of the core name checking operation performed by this script. |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 181 | Shares a common logger, and a shared return code. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 182 | """ |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 183 | def __init__(self, verbose=False): |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 184 | self.log = None |
Yuto Takano | fc54dfb | 2021-08-07 17:18:28 +0100 | [diff] [blame] | 185 | self.check_repo_path() |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 186 | self.return_code = 0 |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 187 | |
| 188 | self.setup_logger(verbose) |
| 189 | |
| 190 | # Globally excluded filenames |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 191 | self.excluded_files = ["bn_mul", "compat-2.x.h"] |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 192 | |
| 193 | # Will contain the parse result after a comprehensive parse |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 194 | self.parse_result = {} |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 195 | |
| 196 | def set_return_code(self, return_code): |
| 197 | if return_code > self.return_code: |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame] | 198 | self.log.debug("Setting new return code to {}".format(return_code)) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 199 | self.return_code = return_code |
| 200 | |
Yuto Takano | fc54dfb | 2021-08-07 17:18:28 +0100 | [diff] [blame] | 201 | @staticmethod |
| 202 | def check_repo_path(): |
| 203 | """ |
| 204 | Check that the current working directory is the project root, and throw |
| 205 | an exception if not. |
| 206 | """ |
| 207 | if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): |
| 208 | raise Exception("This script must be run from Mbed TLS root") |
| 209 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 210 | def setup_logger(self, verbose=False): |
| 211 | """ |
| 212 | Set up a logger and set the change the default logging level from |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 213 | WARNING to INFO. Loggers are better than print statements since their |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 214 | verbosity can be controlled. |
| 215 | """ |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 216 | self.log = logging.getLogger() |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 217 | if verbose: |
| 218 | self.log.setLevel(logging.DEBUG) |
| 219 | else: |
| 220 | self.log.setLevel(logging.INFO) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 221 | self.log.addHandler(logging.StreamHandler()) |
| 222 | |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 223 | def get_files(self, wildcard): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 224 | """ |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 225 | Get all files that match a UNIX-style wildcard recursively. While the |
| 226 | script is designed only for use on UNIX/macOS (due to nm), this function |
| 227 | would work fine on Windows even with forward slashes in the wildcard. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 228 | |
| 229 | Args: |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 230 | * wildcard: shell-style wildcards to match filepaths against. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 231 | |
| 232 | Returns a List of relative filepaths. |
| 233 | """ |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 234 | accumulator = [] |
| 235 | |
| 236 | for filepath in glob.iglob(wildcard, recursive=True): |
| 237 | if os.path.basename(filepath) not in self.excluded_files: |
| 238 | accumulator.append(filepath) |
| 239 | return accumulator |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 240 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 241 | def parse_names_in_source(self): |
| 242 | """ |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 243 | Comprehensive function to call each parsing function and retrieve |
| 244 | various elements of the code, together with their source location. |
| 245 | Puts the parsed values in the internal variable self.parse_result, so |
| 246 | they can be used from perform_checks(). |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 247 | """ |
| 248 | self.log.info("Parsing source code...") |
Yuto Takano | d24e037 | 2021-08-06 16:42:33 +0100 | [diff] [blame] | 249 | self.log.debug( |
| 250 | "The following files are excluded from the search: {}" |
| 251 | .format(str(self.excluded_files)) |
| 252 | ) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 253 | |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 254 | m_headers = self.get_files("include/mbedtls/*.h") |
| 255 | p_headers = self.get_files("include/psa/*.h") |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 256 | t_headers = ["3rdparty/everest/include/everest/everest.h", |
| 257 | "3rdparty/everest/include/everest/x25519.h"] |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 258 | d_headers = self.get_files("tests/include/test/drivers/*.h") |
| 259 | l_headers = self.get_files("library/*.h") |
| 260 | libraries = self.get_files("library/*.c") + [ |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 261 | "3rdparty/everest/library/everest.c", |
| 262 | "3rdparty/everest/library/x25519.c"] |
| 263 | |
| 264 | all_macros = self.parse_macros( |
| 265 | m_headers + p_headers + t_headers + l_headers + d_headers) |
| 266 | enum_consts = self.parse_enum_consts( |
| 267 | m_headers + l_headers + t_headers) |
| 268 | identifiers = self.parse_identifiers( |
| 269 | m_headers + p_headers + t_headers + l_headers) |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 270 | mbed_words = self.parse_mbed_words( |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 271 | m_headers + p_headers + t_headers + l_headers + libraries) |
| 272 | symbols = self.parse_symbols() |
| 273 | |
| 274 | # Remove identifier macros like mbedtls_printf or mbedtls_calloc |
| 275 | identifiers_justname = [x.name for x in identifiers] |
| 276 | actual_macros = [] |
| 277 | for macro in all_macros: |
| 278 | if macro.name not in identifiers_justname: |
| 279 | actual_macros.append(macro) |
| 280 | |
| 281 | self.log.debug("Found:") |
| 282 | self.log.debug(" {} Macros".format(len(all_macros))) |
| 283 | self.log.debug(" {} Non-identifier Macros".format(len(actual_macros))) |
| 284 | self.log.debug(" {} Enum Constants".format(len(enum_consts))) |
| 285 | self.log.debug(" {} Identifiers".format(len(identifiers))) |
| 286 | self.log.debug(" {} Exported Symbols".format(len(symbols))) |
| 287 | self.log.info("Analysing...") |
| 288 | |
| 289 | self.parse_result = { |
| 290 | "macros": actual_macros, |
| 291 | "enum_consts": enum_consts, |
| 292 | "identifiers": identifiers, |
| 293 | "symbols": symbols, |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 294 | "mbed_words": mbed_words |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 297 | def parse_macros(self, header_files): |
| 298 | """ |
| 299 | Parse all macros defined by #define preprocessor directives. |
| 300 | |
| 301 | Args: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 302 | * header_files: A List of filepaths to look through. |
| 303 | |
| 304 | Returns a List of Match objects for the found macros. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 305 | """ |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 306 | macro_regex = re.compile(r"# *define +(?P<macro>\w+)") |
| 307 | exclusions = ( |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 308 | "asm", "inline", "EMIT", "_CRT_SECURE_NO_DEPRECATE", "MULADDC_" |
| 309 | ) |
| 310 | |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame] | 311 | self.log.debug("Looking for macros in {} files".format(len(header_files))) |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 312 | |
| 313 | macros = [] |
| 314 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 315 | for header_file in header_files: |
Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 316 | with open(header_file, "r", encoding="utf-8") as header: |
Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 317 | for line_no, line in enumerate(header): |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 318 | for macro in macro_regex.finditer(line): |
| 319 | if not macro.group("macro").startswith(exclusions): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 320 | macros.append(Match( |
| 321 | header_file, |
| 322 | line, |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 323 | (line_no, macro.start(), macro.end()), |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 324 | macro.group("macro"))) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 325 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 326 | return macros |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 327 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 328 | def parse_mbed_words(self, files): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 329 | """ |
Yuto Takano | b47b504 | 2021-08-07 00:42:54 +0100 | [diff] [blame] | 330 | Parse all words in the file that begin with MBED, in and out of macros, |
| 331 | comments, anything. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 332 | |
| 333 | Args: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 334 | * files: a List of filepaths to look through. |
| 335 | |
| 336 | Returns a List of Match objects for words beginning with MBED. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 337 | """ |
Yuto Takano | b47b504 | 2021-08-07 00:42:54 +0100 | [diff] [blame] | 338 | # Typos of TLS are common, hence the broader check below than MBEDTLS. |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 339 | mbed_regex = re.compile(r"\bMBED.+?_[A-Z0-9_]*") |
| 340 | exclusions = re.compile(r"// *no-check-names|#error") |
| 341 | |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame] | 342 | self.log.debug("Looking for MBED names in {} files".format(len(files))) |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 343 | |
| 344 | mbed_words = [] |
| 345 | |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame] | 346 | for filename in files: |
Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 347 | with open(filename, "r", encoding="utf-8") as fp: |
Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 348 | for line_no, line in enumerate(fp): |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 349 | if exclusions.search(line): |
Yuto Takano | c62b408 | 2021-08-05 20:17:07 +0100 | [diff] [blame] | 350 | continue |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 351 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 352 | for name in mbed_regex.finditer(line): |
| 353 | mbed_words.append(Match( |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 354 | filename, |
| 355 | line, |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 356 | (line_no, name.start(), name.end()), |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 357 | name.group(0) |
| 358 | )) |
| 359 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 360 | return mbed_words |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 361 | |
| 362 | def parse_enum_consts(self, header_files): |
| 363 | """ |
| 364 | Parse all enum value constants that are declared. |
| 365 | |
| 366 | Args: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 367 | * header_files: A List of filepaths to look through. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 368 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 369 | Returns a List of Match objects for the findings. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 370 | """ |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 371 | self.log.debug("Looking for enum consts in {} files".format(len(header_files))) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 372 | |
| 373 | enum_consts = [] |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 374 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 375 | for header_file in header_files: |
| 376 | # Emulate a finite state machine to parse enum declarations. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 377 | # 0 = not in enum |
| 378 | # 1 = inside enum |
| 379 | # 2 = almost inside enum |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 380 | state = 0 |
Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 381 | with open(header_file, "r", encoding="utf-8") as header: |
Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 382 | for line_no, line in enumerate(header): |
Yuto Takano | 13ecd99 | 2021-08-06 16:56:52 +0100 | [diff] [blame] | 383 | # Match typedefs and brackets only when they are at the |
| 384 | # beginning of the line -- if they are indented, they might |
| 385 | # be sub-structures within structs, etc. |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 386 | if state == 0 and re.match(r"^(typedef +)?enum +{", line): |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 387 | state = 1 |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 388 | elif state == 0 and re.match(r"^(typedef +)?enum", line): |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 389 | state = 2 |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 390 | elif state == 2 and re.match(r"^{", line): |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 391 | state = 1 |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 392 | elif state == 1 and re.match(r"^}", line): |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 393 | state = 0 |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 394 | elif state == 1 and not re.match(r" *#", line): |
Yuto Takano | 13ecd99 | 2021-08-06 16:56:52 +0100 | [diff] [blame] | 395 | enum_const = re.match(r" *(?P<enum_const>\w+)", line) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 396 | if enum_const: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 397 | enum_consts.append(Match( |
| 398 | header_file, |
| 399 | line, |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 400 | (line_no, enum_const.start(), enum_const.end()), |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 401 | enum_const.group("enum_const"))) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 402 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 403 | return enum_consts |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 404 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 405 | def parse_identifiers(self, header_files): |
| 406 | """ |
| 407 | Parse all lines of a header where a function identifier is declared, |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 408 | based on some huersitics. Highly dependent on formatting style. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 409 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 410 | Args: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 411 | * header_files: A List of filepaths to look through. |
| 412 | |
| 413 | Returns a List of Match objects with identifiers. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 414 | """ |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 415 | identifier_regex = re.compile( |
| 416 | # Match " something(a" or " *something(a". Functions. |
| 417 | # Assumptions: |
| 418 | # - function definition from return type to one of its arguments is |
| 419 | # all on one line (enforced by the previous_line concat below) |
| 420 | # - function definition line only contains alphanumeric, asterisk, |
| 421 | # underscore, and open bracket |
| 422 | r".* \**(\w+) *\( *\w|" |
| 423 | # Match "(*something)(". Flexible with spaces. |
| 424 | r".*\( *\* *(\w+) *\) *\(|" |
| 425 | # Match names of named data structures. |
| 426 | r"(?:typedef +)?(?:struct|union|enum) +(\w+)(?: *{)?$|" |
| 427 | # Match names of typedef instances, after closing bracket. |
| 428 | r"}? *(\w+)[;[].*") |
| 429 | exclusion_lines = re.compile(r"^(" |
Yuto Takano | 12a7ecd | 2021-08-07 00:40:29 +0100 | [diff] [blame] | 430 | r"extern +\"C\"|" |
| 431 | r"(typedef +)?(struct|union|enum)( *{)?$|" |
| 432 | r"} *;?$|" |
| 433 | r"$|" |
| 434 | r"//|" |
| 435 | r"#" |
| 436 | r")") |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 437 | |
| 438 | self.log.debug("Looking for identifiers in {} files".format(len(header_files))) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 439 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 440 | identifiers = [] |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 441 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 442 | for header_file in header_files: |
Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 443 | with open(header_file, "r", encoding="utf-8") as header: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 444 | in_block_comment = False |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 445 | previous_line = "" |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 446 | |
Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 447 | for line_no, line in enumerate(header): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 448 | # Skip parsing this line if a block comment ends on it, |
| 449 | # but don't skip if it has just started -- there is a chance |
| 450 | # it ends on the same line. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 451 | if re.search(r"/\*", line): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 452 | in_block_comment = not in_block_comment |
| 453 | if re.search(r"\*/", line): |
| 454 | in_block_comment = not in_block_comment |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 455 | continue |
| 456 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 457 | if in_block_comment: |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 458 | previous_line = "" |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 459 | continue |
| 460 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 461 | if exclusion_lines.match(line): |
| 462 | previous_line = "" |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 463 | continue |
| 464 | |
Yuto Takano | cfc9e4a | 2021-08-06 20:02:32 +0100 | [diff] [blame] | 465 | # If the line contains only space-separated alphanumeric |
| 466 | # characters (or underscore, asterisk, or, open bracket), |
| 467 | # and nothing else, high chance it's a declaration that |
| 468 | # continues on the next line |
| 469 | if re.match(r"^([\w\*\(]+\s+)+$", line): |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 470 | previous_line += line |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 471 | continue |
| 472 | |
| 473 | # If previous line seemed to start an unfinished declaration |
Yuto Takano | cfc9e4a | 2021-08-06 20:02:32 +0100 | [diff] [blame] | 474 | # (as above), concat and treat them as one. |
| 475 | if previous_line: |
| 476 | line = previous_line.strip() + " " + line.strip() |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 477 | previous_line = "" |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 478 | |
| 479 | # Skip parsing if line has a space in front = hueristic to |
| 480 | # skip function argument lines (highly subject to formatting |
| 481 | # changes) |
| 482 | if line[0] == " ": |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 483 | continue |
Yuto Takano | 6f38ab3 | 2021-08-05 21:07:14 +0100 | [diff] [blame] | 484 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 485 | identifier = identifier_regex.search(line) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 486 | |
| 487 | if identifier: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 488 | # Find the group that matched, and append it |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 489 | for group in identifier.groups(): |
| 490 | if group: |
| 491 | identifiers.append(Match( |
| 492 | header_file, |
| 493 | line, |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 494 | (line_no, identifier.start(), identifier.end()), |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 495 | group)) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 496 | |
| 497 | return identifiers |
| 498 | |
| 499 | def parse_symbols(self): |
| 500 | """ |
| 501 | Compile the Mbed TLS libraries, and parse the TLS, Crypto, and x509 |
| 502 | object files using nm to retrieve the list of referenced symbols. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 503 | Exceptions thrown here are rethrown because they would be critical |
| 504 | errors that void several tests, and thus needs to halt the program. This |
| 505 | is explicitly done for clarity. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 506 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 507 | Returns a List of unique symbols defined and used in the libraries. |
| 508 | """ |
| 509 | self.log.info("Compiling...") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 510 | symbols = [] |
| 511 | |
| 512 | # Back up the config and atomically compile with the full configratuion. |
| 513 | shutil.copy("include/mbedtls/mbedtls_config.h", |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 514 | "include/mbedtls/mbedtls_config.h.bak") |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 515 | try: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 516 | # Use check=True in all subprocess calls so that failures are raised |
| 517 | # as exceptions and logged. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 518 | subprocess.run( |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 519 | ["python3", "scripts/config.py", "full"], |
Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 520 | universal_newlines=True, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 521 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 522 | ) |
| 523 | my_environment = os.environ.copy() |
| 524 | my_environment["CFLAGS"] = "-fno-asynchronous-unwind-tables" |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 525 | subprocess.run( |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 526 | ["make", "clean", "lib"], |
| 527 | env=my_environment, |
Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 528 | universal_newlines=True, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 529 | stdout=subprocess.PIPE, |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 530 | stderr=subprocess.STDOUT, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 531 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 532 | ) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 533 | |
| 534 | # Perform object file analysis using nm |
| 535 | symbols = self.parse_symbols_from_nm( |
| 536 | ["library/libmbedcrypto.a", |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 537 | "library/libmbedtls.a", |
| 538 | "library/libmbedx509.a"]) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 539 | |
| 540 | subprocess.run( |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 541 | ["make", "clean"], |
Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 542 | universal_newlines=True, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 543 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 544 | ) |
| 545 | except subprocess.CalledProcessError as error: |
Yuto Takano | 25eeb7b | 2021-08-06 21:27:59 +0100 | [diff] [blame] | 546 | self.log.debug(error.output) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 547 | self.set_return_code(2) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 548 | raise error |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 549 | finally: |
Yuto Takano | 6fececf | 2021-08-07 17:28:23 +0100 | [diff] [blame] | 550 | # Put back the original config regardless of there being errors. |
| 551 | # Works also for keyboard interrupts. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 552 | shutil.move("include/mbedtls/mbedtls_config.h.bak", |
| 553 | "include/mbedtls/mbedtls_config.h") |
| 554 | |
| 555 | return symbols |
| 556 | |
| 557 | def parse_symbols_from_nm(self, object_files): |
| 558 | """ |
| 559 | Run nm to retrieve the list of referenced symbols in each object file. |
| 560 | Does not return the position data since it is of no use. |
| 561 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 562 | Args: |
| 563 | * object_files: a List of compiled object files to search through. |
| 564 | |
| 565 | Returns a List of unique symbols defined and used in any of the object |
| 566 | files. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 567 | """ |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 568 | nm_undefined_regex = re.compile(r"^\S+: +U |^$|^\S+:$") |
| 569 | nm_valid_regex = re.compile(r"^\S+( [0-9A-Fa-f]+)* . _*(?P<symbol>\w+)") |
Yuto Takano | 12a7ecd | 2021-08-07 00:40:29 +0100 | [diff] [blame] | 570 | exclusions = ("FStar", "Hacl") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 571 | |
| 572 | symbols = [] |
| 573 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 574 | # Gather all outputs of nm |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 575 | nm_output = "" |
| 576 | for lib in object_files: |
| 577 | nm_output += subprocess.run( |
| 578 | ["nm", "-og", lib], |
Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 579 | universal_newlines=True, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 580 | stdout=subprocess.PIPE, |
| 581 | stderr=subprocess.STDOUT, |
| 582 | check=True |
| 583 | ).stdout |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 584 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 585 | for line in nm_output.splitlines(): |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 586 | if not nm_undefined_regex.match(line): |
| 587 | symbol = nm_valid_regex.match(line) |
Yuto Takano | 12a7ecd | 2021-08-07 00:40:29 +0100 | [diff] [blame] | 588 | if (symbol and not symbol.group("symbol").startswith(exclusions)): |
Yuto Takano | e77f699 | 2021-08-05 20:22:59 +0100 | [diff] [blame] | 589 | symbols.append(symbol.group("symbol")) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 590 | else: |
| 591 | self.log.error(line) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 592 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 593 | return symbols |
| 594 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 595 | def perform_checks(self, quiet=False): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 596 | """ |
| 597 | Perform each check in order, output its PASS/FAIL status. Maintain an |
| 598 | overall test status, and output that at the end. |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 599 | Assumes parse_names_in_source() was called before this. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 600 | |
| 601 | Args: |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 602 | * quiet: whether to hide detailed problem explanation. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 603 | """ |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 604 | self.log.info("=============") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 605 | problems = 0 |
| 606 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 607 | problems += self.check_symbols_declared_in_header(quiet) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 608 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 609 | pattern_checks = [("macros", MACRO_PATTERN), |
| 610 | ("enum_consts", CONSTANTS_PATTERN), |
| 611 | ("identifiers", IDENTIFIER_PATTERN)] |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 612 | for group, check_pattern in pattern_checks: |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 613 | problems += self.check_match_pattern(quiet, group, check_pattern) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 614 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 615 | problems += self.check_for_typos(quiet) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 616 | |
| 617 | self.log.info("=============") |
| 618 | if problems > 0: |
| 619 | self.log.info("FAIL: {0} problem(s) to fix".format(str(problems))) |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 620 | if quiet: |
| 621 | self.log.info("Remove --quiet to see explanations.") |
Yuto Takano | fc54dfb | 2021-08-07 17:18:28 +0100 | [diff] [blame] | 622 | else: |
| 623 | self.log.info("Use --quiet for minimal output.") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 624 | else: |
| 625 | self.log.info("PASS") |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 626 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 627 | def check_symbols_declared_in_header(self, quiet): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 628 | """ |
| 629 | Perform a check that all detected symbols in the library object files |
| 630 | are properly declared in headers. |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 631 | Assumes parse_names_in_source() was called before this. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 632 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 633 | Args: |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 634 | * quiet: whether to hide detailed problem explanation. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 635 | |
| 636 | Returns the number of problems that need fixing. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 637 | """ |
| 638 | problems = [] |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 639 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 640 | for symbol in self.parse_result["symbols"]: |
| 641 | found_symbol_declared = False |
| 642 | for identifier_match in self.parse_result["identifiers"]: |
| 643 | if symbol == identifier_match.name: |
| 644 | found_symbol_declared = True |
| 645 | break |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 646 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 647 | if not found_symbol_declared: |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 648 | problems.append(SymbolNotInHeader(symbol, quiet=quiet)) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 649 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 650 | self.output_check_result("All symbols in header", problems) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 651 | return len(problems) |
| 652 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 653 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 654 | def check_match_pattern(self, quiet, group_to_check, check_pattern): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 655 | """ |
| 656 | Perform a check that all items of a group conform to a regex pattern. |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 657 | Assumes parse_names_in_source() was called before this. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 658 | |
| 659 | Args: |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 660 | * quiet: whether to hide detailed problem explanation. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 661 | * group_to_check: string key to index into self.parse_result. |
| 662 | * check_pattern: the regex to check against. |
| 663 | |
| 664 | Returns the number of problems that need fixing. |
| 665 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 666 | problems = [] |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 667 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 668 | for item_match in self.parse_result[group_to_check]: |
| 669 | if not re.match(check_pattern, item_match.name): |
| 670 | problems.append(PatternMismatch(check_pattern, item_match)) |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame] | 671 | # Double underscore is a reserved identifier, never to be used |
Yuto Takano | c763cc3 | 2021-08-05 20:06:34 +0100 | [diff] [blame] | 672 | if re.match(r".*__.*", item_match.name): |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 673 | problems.append(PatternMismatch( |
| 674 | "double underscore", |
| 675 | item_match, |
| 676 | quiet=quiet)) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 677 | |
| 678 | self.output_check_result( |
| 679 | "Naming patterns of {}".format(group_to_check), |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 680 | problems) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 681 | return len(problems) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 682 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 683 | def check_for_typos(self, quiet): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 684 | """ |
| 685 | Perform a check that all words in the soure code beginning with MBED are |
| 686 | either defined as macros, or as enum constants. |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 687 | Assumes parse_names_in_source() was called before this. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 688 | |
| 689 | Args: |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 690 | * quiet: whether to hide detailed problem explanation. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 691 | |
| 692 | Returns the number of problems that need fixing. |
| 693 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 694 | problems = [] |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 695 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 696 | # Set comprehension, equivalent to a list comprehension inside set() |
| 697 | all_caps_names = { |
| 698 | match.name |
| 699 | for match |
| 700 | in self.parse_result["macros"] + self.parse_result["enum_consts"]} |
| 701 | typo_exclusion = re.compile(r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 702 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 703 | for name_match in self.parse_result["mbed_words"]: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 704 | found = name_match.name in all_caps_names |
| 705 | |
| 706 | # Since MBEDTLS_PSA_ACCEL_XXX defines are defined by the |
| 707 | # PSA driver, they will not exist as macros. However, they |
| 708 | # should still be checked for typos using the equivalent |
| 709 | # BUILTINs that exist. |
| 710 | if "MBEDTLS_PSA_ACCEL_" in name_match.name: |
| 711 | found = name_match.name.replace( |
| 712 | "MBEDTLS_PSA_ACCEL_", |
| 713 | "MBEDTLS_PSA_BUILTIN_") in all_caps_names |
| 714 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 715 | if not found and not typo_exclusion.search(name_match.name): |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 716 | problems.append(Typo(name_match, quiet=quiet)) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 717 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 718 | self.output_check_result("Likely typos", problems) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 719 | return len(problems) |
| 720 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 721 | def output_check_result(self, name, problems): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 722 | """ |
| 723 | Write out the PASS/FAIL status of a performed check depending on whether |
| 724 | there were problems. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 725 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 726 | if problems: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 727 | self.set_return_code(1) |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 728 | self.log.info("{}: FAIL\n".format(name)) |
| 729 | for problem in problems: |
| 730 | self.log.warning(str(problem)) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 731 | else: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 732 | self.log.info("{}: PASS".format(name)) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 733 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 734 | def main(): |
| 735 | """ |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 736 | Perform argument parsing, and create an instance of NameCheck to begin the |
| 737 | core operation. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 738 | """ |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 739 | argparser = argparse.ArgumentParser( |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 740 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 741 | description=( |
| 742 | "This script confirms that the naming of all symbols and identifiers " |
| 743 | "in Mbed TLS are consistent with the house style and are also " |
| 744 | "self-consistent.\n\n" |
| 745 | "Expected to be run from the MbedTLS root directory.")) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 746 | |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 747 | argparser.add_argument("-v", "--verbose", |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 748 | action="store_true", |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 749 | help="show parse results") |
| 750 | |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 751 | argparser.add_argument("-q", "--quiet", |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 752 | action="store_true", |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 753 | help="hide unnecessary text, explanations, and highlighs") |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 754 | |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 755 | args = argparser.parse_args() |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 756 | |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 757 | try: |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame^] | 758 | name_check = NameCheck(verbose=args.verbose) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 759 | name_check.parse_names_in_source() |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 760 | name_check.perform_checks(quiet=args.quiet) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 761 | sys.exit(name_check.return_code) |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 762 | except Exception: # pylint: disable=broad-except |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 763 | traceback.print_exc() |
| 764 | sys.exit(2) |
| 765 | |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 766 | if __name__ == "__main__": |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 767 | main() |