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