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