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