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