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