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