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 | |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 23 | It contains two major Python classes, CodeParser and NameChecker. They both have |
| 24 | a comprehensive "run-all" function (comprehensive_parse() and perform_checks()) |
| 25 | but the individual functions can also be used for specific needs. |
| 26 | |
| 27 | CodeParser makes heavy use of regular expressions to parse the code, and is |
| 28 | dependent on the current code formatting. Many Python C parser libraries require |
| 29 | preprocessed C code, which means no macro parsing. Compiler tools are also not |
| 30 | very helpful when we want the exact location in the original source (which |
| 31 | becomes impossible when e.g. comments are stripped). |
| 32 | |
| 33 | NameChecker performs the following checks: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 34 | |
| 35 | - All exported and available symbols in the library object files, are explicitly |
Yuto Takano | 159255a | 2021-08-06 17:00:28 +0100 | [diff] [blame] | 36 | declared in the header files. This uses the nm command. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 37 | - All macros, constants, and identifiers (function names, struct names, etc) |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 38 | follow the required regex pattern. |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 39 | - Typo checking: All words that begin with MBED|PSA exist as macros or constants. |
Yuto Takano | fc54dfb | 2021-08-07 17:18:28 +0100 | [diff] [blame] | 40 | |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 41 | The script returns 0 on success, 1 on test failure, and 2 if there is a script |
Yuto Takano | 8246eb8 | 2021-08-16 10:37:24 +0100 | [diff] [blame] | 42 | error. It must be run from Mbed TLS root. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 43 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 44 | |
Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 45 | import abc |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 46 | import argparse |
Gilles Peskine | 89458d1 | 2021-09-27 19:20:17 +0200 | [diff] [blame] | 47 | import fnmatch |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 48 | import glob |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 49 | import textwrap |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 50 | import os |
| 51 | import sys |
| 52 | import traceback |
| 53 | import re |
Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 54 | import enum |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 55 | import shutil |
| 56 | import subprocess |
| 57 | import logging |
| 58 | |
Gilles Peskine | d9071e7 | 2022-09-18 21:17:09 +0200 | [diff] [blame] | 59 | import scripts_path # pylint: disable=unused-import |
| 60 | from mbedtls_dev import build_tree |
| 61 | |
| 62 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 63 | # Naming patterns to check against. These are defined outside the NameCheck |
| 64 | # class for ease of modification. |
Janos Follath | 9938719 | 2022-08-10 11:11:34 +0100 | [diff] [blame] | 65 | PUBLIC_MACRO_PATTERN = r"^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$" |
| 66 | INTERNAL_MACRO_PATTERN = r"^[0-9A-Za-z_]*[0-9A-Z]$" |
| 67 | CONSTANTS_PATTERN = PUBLIC_MACRO_PATTERN |
Yuto Takano | c183893 | 2021-08-05 19:52:09 +0100 | [diff] [blame] | 68 | IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$" |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 69 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 70 | class Match(): # pylint: disable=too-few-public-methods |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 71 | """ |
| 72 | A class representing a match, together with its found position. |
| 73 | |
| 74 | Fields: |
| 75 | * filename: the file that the match was in. |
| 76 | * line: the full line containing the match. |
Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 77 | * line_no: the line number. |
| 78 | * pos: a tuple of (start, end) positions on the line where the match is. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 79 | * name: the match itself. |
| 80 | """ |
Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 81 | def __init__(self, filename, line, line_no, pos, name): |
| 82 | # pylint: disable=too-many-arguments |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 83 | self.filename = filename |
| 84 | self.line = line |
Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 85 | self.line_no = line_no |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 86 | self.pos = pos |
| 87 | self.name = name |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 88 | |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 89 | def __str__(self): |
Yuto Takano | fb86ac7 | 2021-08-16 10:32:40 +0100 | [diff] [blame] | 90 | """ |
| 91 | Return a formatted code listing representation of the erroneous line. |
| 92 | """ |
Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 93 | gutter = format(self.line_no, "4d") |
| 94 | underline = self.pos[0] * " " + (self.pos[1] - self.pos[0]) * "^" |
Yuto Takano | 381fda8 | 2021-08-06 23:37:20 +0100 | [diff] [blame] | 95 | |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 96 | return ( |
Yuto Takano | fb86ac7 | 2021-08-16 10:32:40 +0100 | [diff] [blame] | 97 | " {0} |\n".format(" " * len(gutter)) + |
Yuto Takano | 381fda8 | 2021-08-06 23:37:20 +0100 | [diff] [blame] | 98 | " {0} | {1}".format(gutter, self.line) + |
Yuto Takano | fb86ac7 | 2021-08-16 10:32:40 +0100 | [diff] [blame] | 99 | " {0} | {1}\n".format(" " * len(gutter), underline) |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 100 | ) |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 101 | |
Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 102 | class Problem(abc.ABC): # pylint: disable=too-few-public-methods |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 103 | """ |
Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 104 | An abstract parent class representing a form of static analysis error. |
| 105 | It extends an Abstract Base Class, which means it is not instantiable, and |
| 106 | it also mandates certain abstract methods to be implemented in subclasses. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 107 | """ |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 108 | # Class variable to control the quietness of all problems |
| 109 | quiet = False |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 110 | def __init__(self): |
| 111 | self.textwrapper = textwrap.TextWrapper() |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 112 | self.textwrapper.width = 80 |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 113 | self.textwrapper.initial_indent = " > " |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 114 | self.textwrapper.subsequent_indent = " " |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 115 | |
Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 116 | def __str__(self): |
| 117 | """ |
| 118 | Unified string representation method for all Problems. |
| 119 | """ |
| 120 | if self.__class__.quiet: |
| 121 | return self.quiet_output() |
| 122 | return self.verbose_output() |
| 123 | |
| 124 | @abc.abstractmethod |
| 125 | def quiet_output(self): |
| 126 | """ |
| 127 | The output when --quiet is enabled. |
| 128 | """ |
| 129 | pass |
| 130 | |
| 131 | @abc.abstractmethod |
| 132 | def verbose_output(self): |
| 133 | """ |
| 134 | The default output with explanation and code snippet if appropriate. |
| 135 | """ |
| 136 | pass |
| 137 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 138 | class SymbolNotInHeader(Problem): # pylint: disable=too-few-public-methods |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 139 | """ |
| 140 | A problem that occurs when an exported/available symbol in the object file |
| 141 | is not explicitly declared in header files. Created with |
| 142 | NameCheck.check_symbols_declared_in_header() |
| 143 | |
| 144 | Fields: |
| 145 | * symbol_name: the name of the symbol. |
| 146 | """ |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 147 | def __init__(self, symbol_name): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 148 | self.symbol_name = symbol_name |
| 149 | Problem.__init__(self) |
| 150 | |
Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 151 | def quiet_output(self): |
| 152 | return "{0}".format(self.symbol_name) |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 153 | |
Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 154 | def verbose_output(self): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 155 | return self.textwrapper.fill( |
| 156 | "'{0}' was found as an available symbol in the output of nm, " |
| 157 | "however it was not declared in any header files." |
| 158 | .format(self.symbol_name)) |
| 159 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 160 | class PatternMismatch(Problem): # pylint: disable=too-few-public-methods |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 161 | """ |
| 162 | A problem that occurs when something doesn't match the expected pattern. |
| 163 | Created with NameCheck.check_match_pattern() |
| 164 | |
| 165 | Fields: |
| 166 | * pattern: the expected regex pattern |
| 167 | * match: the Match object in question |
| 168 | """ |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 169 | def __init__(self, pattern, match): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 170 | self.pattern = pattern |
| 171 | self.match = match |
| 172 | Problem.__init__(self) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 173 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 174 | |
Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 175 | def quiet_output(self): |
| 176 | return ( |
| 177 | "{0}:{1}:{2}" |
| 178 | .format(self.match.filename, self.match.line_no, self.match.name) |
| 179 | ) |
| 180 | |
| 181 | def verbose_output(self): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 182 | return self.textwrapper.fill( |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 183 | "{0}:{1}: '{2}' does not match the required pattern '{3}'." |
| 184 | .format( |
| 185 | self.match.filename, |
Yuto Takano | 5f83171 | 2021-08-18 18:03:24 +0100 | [diff] [blame] | 186 | self.match.line_no, |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 187 | self.match.name, |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 188 | self.pattern |
| 189 | ) |
| 190 | ) + "\n" + str(self.match) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 191 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 192 | class Typo(Problem): # pylint: disable=too-few-public-methods |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 193 | """ |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 194 | A problem that occurs when a word using MBED or PSA doesn't |
| 195 | appear to be defined as constants nor enum values. Created with |
| 196 | NameCheck.check_for_typos() |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 197 | |
| 198 | Fields: |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 199 | * match: the Match object of the MBED|PSA name in question. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 200 | """ |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 201 | def __init__(self, match): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 202 | self.match = match |
| 203 | Problem.__init__(self) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 204 | |
Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 205 | def quiet_output(self): |
| 206 | return ( |
| 207 | "{0}:{1}:{2}" |
| 208 | .format(self.match.filename, self.match.line_no, self.match.name) |
| 209 | ) |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 210 | |
Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 211 | def verbose_output(self): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 212 | return self.textwrapper.fill( |
Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 213 | "{0}:{1}: '{2}' looks like a typo. It was not found in any " |
| 214 | "macros or any enums. If this is not a typo, put " |
| 215 | "//no-check-names after it." |
Yuto Takano | 5f83171 | 2021-08-18 18:03:24 +0100 | [diff] [blame] | 216 | .format(self.match.filename, self.match.line_no, self.match.name) |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 217 | ) + "\n" + str(self.match) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 218 | |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 219 | class CodeParser(): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 220 | """ |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 221 | Class for retrieving files and parsing the code. This can be used |
| 222 | independently of the checks that NameChecker performs, for example for |
| 223 | list_internal_identifiers.py. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 224 | """ |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 225 | def __init__(self, log): |
| 226 | self.log = log |
Gilles Peskine | d9071e7 | 2022-09-18 21:17:09 +0200 | [diff] [blame] | 227 | build_tree.check_repo_path() |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 228 | |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 229 | # Memo for storing "glob expression": set(filepaths) |
| 230 | self.files = {} |
| 231 | |
Gilles Peskine | 89458d1 | 2021-09-27 19:20:17 +0200 | [diff] [blame] | 232 | # Globally excluded filenames. |
| 233 | # Note that "*" can match directory separators in exclude lists. |
| 234 | self.excluded_files = ["*/bn_mul", "*/compat-2.x.h"] |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 235 | |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 236 | def comprehensive_parse(self): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 237 | """ |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 238 | Comprehensive ("default") function to call each parsing function and |
| 239 | retrieve various elements of the code, together with the source location. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 240 | |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 241 | Returns a dict of parsed item key to the corresponding List of Matches. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 242 | """ |
| 243 | self.log.info("Parsing source code...") |
Yuto Takano | d24e037 | 2021-08-06 16:42:33 +0100 | [diff] [blame] | 244 | self.log.debug( |
Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 245 | "The following files are excluded from the search: {}" |
Yuto Takano | d24e037 | 2021-08-06 16:42:33 +0100 | [diff] [blame] | 246 | .format(str(self.excluded_files)) |
| 247 | ) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 248 | |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 249 | all_macros = {"public": [], "internal": [], "private":[]} |
Janos Follath | 9938719 | 2022-08-10 11:11:34 +0100 | [diff] [blame] | 250 | all_macros["public"] = self.parse_macros([ |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 251 | "include/mbedtls/*.h", |
| 252 | "include/psa/*.h", |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 253 | "3rdparty/everest/include/everest/everest.h", |
| 254 | "3rdparty/everest/include/everest/x25519.h" |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 255 | ]) |
Janos Follath | 9938719 | 2022-08-10 11:11:34 +0100 | [diff] [blame] | 256 | all_macros["internal"] = self.parse_macros([ |
| 257 | "library/*.h", |
| 258 | "tests/include/test/drivers/*.h", |
| 259 | ]) |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 260 | all_macros["private"] = self.parse_macros([ |
| 261 | "library/*.c", |
| 262 | ]) |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 263 | enum_consts = self.parse_enum_consts([ |
| 264 | "include/mbedtls/*.h", |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 265 | "include/psa/*.h", |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 266 | "library/*.h", |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 267 | "library/*.c", |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 268 | "3rdparty/everest/include/everest/everest.h", |
| 269 | "3rdparty/everest/include/everest/x25519.h" |
| 270 | ]) |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 271 | identifiers, excluded_identifiers = self.parse_identifiers([ |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 272 | "include/mbedtls/*.h", |
| 273 | "include/psa/*.h", |
| 274 | "library/*.h", |
| 275 | "3rdparty/everest/include/everest/everest.h", |
| 276 | "3rdparty/everest/include/everest/x25519.h" |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 277 | ], ["3rdparty/p256-m/p256-m/p256-m.h"]) |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 278 | mbed_psa_words = self.parse_mbed_psa_words([ |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 279 | "include/mbedtls/*.h", |
| 280 | "include/psa/*.h", |
| 281 | "library/*.h", |
| 282 | "3rdparty/everest/include/everest/everest.h", |
| 283 | "3rdparty/everest/include/everest/x25519.h", |
| 284 | "library/*.c", |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 285 | "3rdparty/everest/library/everest.c", |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 286 | "3rdparty/everest/library/x25519.c" |
Archana | 634e0d2 | 2022-01-10 17:37:42 +0530 | [diff] [blame] | 287 | ], ["library/psa_crypto_driver_wrappers.c"]) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 288 | symbols = self.parse_symbols() |
| 289 | |
| 290 | # Remove identifier macros like mbedtls_printf or mbedtls_calloc |
| 291 | identifiers_justname = [x.name for x in identifiers] |
Janos Follath | 9938719 | 2022-08-10 11:11:34 +0100 | [diff] [blame] | 292 | actual_macros = {"public": [], "internal": []} |
| 293 | for scope in actual_macros: |
| 294 | for macro in all_macros[scope]: |
| 295 | if macro.name not in identifiers_justname: |
| 296 | actual_macros[scope].append(macro) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 297 | |
| 298 | self.log.debug("Found:") |
Yuto Takano | 9d9c6dc | 2021-08-16 10:43:45 +0100 | [diff] [blame] | 299 | # Aligns the counts on the assumption that none exceeds 4 digits |
Janos Follath | 9938719 | 2022-08-10 11:11:34 +0100 | [diff] [blame] | 300 | for scope in actual_macros: |
| 301 | self.log.debug(" {:4} Total {} Macros" |
Janos Follath | 8d59c86 | 2022-08-10 15:35:35 +0100 | [diff] [blame] | 302 | .format(len(all_macros[scope]), scope)) |
Janos Follath | 9938719 | 2022-08-10 11:11:34 +0100 | [diff] [blame] | 303 | self.log.debug(" {:4} {} Non-identifier Macros" |
Janos Follath | 8d59c86 | 2022-08-10 15:35:35 +0100 | [diff] [blame] | 304 | .format(len(actual_macros[scope]), scope)) |
Yuto Takano | 9d9c6dc | 2021-08-16 10:43:45 +0100 | [diff] [blame] | 305 | self.log.debug(" {:4} Enum Constants".format(len(enum_consts))) |
| 306 | self.log.debug(" {:4} Identifiers".format(len(identifiers))) |
| 307 | self.log.debug(" {:4} Exported Symbols".format(len(symbols))) |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 308 | return { |
Janos Follath | 9938719 | 2022-08-10 11:11:34 +0100 | [diff] [blame] | 309 | "public_macros": actual_macros["public"], |
| 310 | "internal_macros": actual_macros["internal"], |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 311 | "private_macros": all_macros["private"], |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 312 | "enum_consts": enum_consts, |
| 313 | "identifiers": identifiers, |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 314 | "excluded_identifiers": excluded_identifiers, |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 315 | "symbols": symbols, |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 316 | "mbed_psa_words": mbed_psa_words |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Gilles Peskine | 89458d1 | 2021-09-27 19:20:17 +0200 | [diff] [blame] | 319 | def is_file_excluded(self, path, exclude_wildcards): |
Gilles Peskine | 8a83224 | 2021-09-28 10:12:49 +0200 | [diff] [blame] | 320 | """Whether the given file path is excluded.""" |
Gilles Peskine | 89458d1 | 2021-09-27 19:20:17 +0200 | [diff] [blame] | 321 | # exclude_wildcards may be None. Also, consider the global exclusions. |
| 322 | exclude_wildcards = (exclude_wildcards or []) + self.excluded_files |
| 323 | for pattern in exclude_wildcards: |
| 324 | if fnmatch.fnmatch(path, pattern): |
| 325 | return True |
| 326 | return False |
| 327 | |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 328 | def get_all_files(self, include_wildcards, exclude_wildcards): |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 329 | """ |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 330 | Get all files that match any of the included UNIX-style wildcards |
| 331 | and filter them into included and excluded lists. |
| 332 | While the check_names script is designed only for use on UNIX/macOS |
| 333 | (due to nm), this function alone will work fine on Windows even with |
| 334 | forward slashes in the wildcard. |
| 335 | |
| 336 | Args: |
| 337 | * include_wildcards: a List of shell-style wildcards to match filepaths. |
| 338 | * exclude_wildcards: a List of shell-style wildcards to exclude. |
| 339 | |
| 340 | Returns: |
| 341 | * inc_files: A List of relative filepaths for included files. |
| 342 | * exc_files: A List of relative filepaths for excluded files. |
| 343 | """ |
| 344 | accumulator = set() |
Aditya Deshpande | 0584df4 | 2023-01-16 16:36:31 +0000 | [diff] [blame] | 345 | all_wildcards = include_wildcards + (exclude_wildcards or []) |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 346 | for wildcard in all_wildcards: |
| 347 | accumulator = accumulator.union(glob.iglob(wildcard)) |
| 348 | |
| 349 | inc_files = [] |
| 350 | exc_files = [] |
| 351 | for path in accumulator: |
| 352 | if self.is_file_excluded(path, exclude_wildcards): |
| 353 | exc_files.append(path) |
| 354 | else: |
| 355 | inc_files.append(path) |
| 356 | return (inc_files, exc_files) |
| 357 | |
| 358 | def get_included_files(self, include_wildcards, exclude_wildcards): |
| 359 | """ |
| 360 | Get all files that match any of the included UNIX-style wildcards. |
| 361 | While the check_names script is designed only for use on UNIX/macOS |
| 362 | (due to nm), this function alone will work fine on Windows even with |
| 363 | forward slashes in the wildcard. |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 364 | |
| 365 | Args: |
| 366 | * include_wildcards: a List of shell-style wildcards to match filepaths. |
| 367 | * exclude_wildcards: a List of shell-style wildcards to exclude. |
| 368 | |
| 369 | Returns a List of relative filepaths. |
| 370 | """ |
| 371 | accumulator = set() |
| 372 | |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 373 | for include_wildcard in include_wildcards: |
Gilles Peskine | 89458d1 | 2021-09-27 19:20:17 +0200 | [diff] [blame] | 374 | accumulator = accumulator.union(glob.iglob(include_wildcard)) |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 375 | |
Gilles Peskine | 89458d1 | 2021-09-27 19:20:17 +0200 | [diff] [blame] | 376 | return list(path for path in accumulator |
| 377 | if not self.is_file_excluded(path, exclude_wildcards)) |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 378 | |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 379 | def parse_macros(self, include, exclude=None): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 380 | """ |
| 381 | Parse all macros defined by #define preprocessor directives. |
| 382 | |
| 383 | Args: |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 384 | * include: A List of glob expressions to look for files through. |
| 385 | * exclude: A List of glob expressions for excluding files. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 386 | |
| 387 | Returns a List of Match objects for the found macros. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 388 | """ |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 389 | macro_regex = re.compile(r"# *define +(?P<macro>\w+)") |
| 390 | exclusions = ( |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 391 | "asm", "inline", "EMIT", "_CRT_SECURE_NO_DEPRECATE", "MULADDC_" |
| 392 | ) |
| 393 | |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 394 | files = self.get_included_files(include, exclude) |
Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 395 | self.log.debug("Looking for macros in {} files".format(len(files))) |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 396 | |
Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 397 | macros = [] |
| 398 | for header_file in files: |
Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 399 | with open(header_file, "r", encoding="utf-8") as header: |
Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 400 | for line_no, line in enumerate(header): |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 401 | for macro in macro_regex.finditer(line): |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 402 | if macro.group("macro").startswith(exclusions): |
| 403 | continue |
| 404 | |
| 405 | macros.append(Match( |
| 406 | header_file, |
| 407 | line, |
Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 408 | line_no, |
| 409 | macro.span("macro"), |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 410 | macro.group("macro"))) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 411 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 412 | return macros |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 413 | |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 414 | def parse_mbed_psa_words(self, include, exclude=None): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 415 | """ |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 416 | Parse all words in the file that begin with MBED|PSA, in and out of |
| 417 | macros, comments, anything. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 418 | |
| 419 | Args: |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 420 | * include: A List of glob expressions to look for files through. |
| 421 | * exclude: A List of glob expressions for excluding files. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 422 | |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 423 | Returns a List of Match objects for words beginning with MBED|PSA. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 424 | """ |
Yuto Takano | b47b504 | 2021-08-07 00:42:54 +0100 | [diff] [blame] | 425 | # Typos of TLS are common, hence the broader check below than MBEDTLS. |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 426 | mbed_regex = re.compile(r"\b(MBED.+?|PSA)_[A-Z0-9_]*") |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 427 | exclusions = re.compile(r"// *no-check-names|#error") |
| 428 | |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 429 | files = self.get_included_files(include, exclude) |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 430 | self.log.debug( |
| 431 | "Looking for MBED|PSA words in {} files" |
| 432 | .format(len(files)) |
| 433 | ) |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 434 | |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 435 | mbed_psa_words = [] |
Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 436 | for filename in files: |
Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 437 | with open(filename, "r", encoding="utf-8") as fp: |
Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 438 | for line_no, line in enumerate(fp): |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 439 | if exclusions.search(line): |
Yuto Takano | c62b408 | 2021-08-05 20:17:07 +0100 | [diff] [blame] | 440 | continue |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 441 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 442 | for name in mbed_regex.finditer(line): |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 443 | mbed_psa_words.append(Match( |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 444 | filename, |
| 445 | line, |
Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 446 | line_no, |
| 447 | name.span(0), |
| 448 | name.group(0))) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 449 | |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 450 | return mbed_psa_words |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 451 | |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 452 | def parse_enum_consts(self, include, exclude=None): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 453 | """ |
| 454 | Parse all enum value constants that are declared. |
| 455 | |
| 456 | Args: |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 457 | * include: A List of glob expressions to look for files through. |
| 458 | * exclude: A List of glob expressions for excluding files. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 459 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 460 | Returns a List of Match objects for the findings. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 461 | """ |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 462 | files = self.get_included_files(include, exclude) |
Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 463 | self.log.debug("Looking for enum consts in {} files".format(len(files))) |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 464 | |
Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 465 | # Emulate a finite state machine to parse enum declarations. |
| 466 | # OUTSIDE_KEYWORD = outside the enum keyword |
| 467 | # IN_BRACES = inside enum opening braces |
| 468 | # IN_BETWEEN = between enum keyword and opening braces |
| 469 | states = enum.Enum("FSM", ["OUTSIDE_KEYWORD", "IN_BRACES", "IN_BETWEEN"]) |
Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 470 | enum_consts = [] |
| 471 | for header_file in files: |
Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 472 | state = states.OUTSIDE_KEYWORD |
Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 473 | with open(header_file, "r", encoding="utf-8") as header: |
Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 474 | for line_no, line in enumerate(header): |
Yuto Takano | 13ecd99 | 2021-08-06 16:56:52 +0100 | [diff] [blame] | 475 | # Match typedefs and brackets only when they are at the |
| 476 | # beginning of the line -- if they are indented, they might |
| 477 | # be sub-structures within structs, etc. |
David Horstmann | f91090e | 2022-12-16 13:39:04 +0000 | [diff] [blame] | 478 | optional_c_identifier = r"([_a-zA-Z][_a-zA-Z0-9]*)?" |
Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 479 | if (state == states.OUTSIDE_KEYWORD and |
David Horstmann | f91090e | 2022-12-16 13:39:04 +0000 | [diff] [blame] | 480 | re.search(r"^(typedef +)?enum " + \ |
| 481 | optional_c_identifier + \ |
| 482 | r" *{", line)): |
Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 483 | state = states.IN_BRACES |
| 484 | elif (state == states.OUTSIDE_KEYWORD and |
| 485 | re.search(r"^(typedef +)?enum", line)): |
| 486 | state = states.IN_BETWEEN |
| 487 | elif (state == states.IN_BETWEEN and |
| 488 | re.search(r"^{", line)): |
| 489 | state = states.IN_BRACES |
| 490 | elif (state == states.IN_BRACES and |
| 491 | re.search(r"^}", line)): |
| 492 | state = states.OUTSIDE_KEYWORD |
| 493 | elif (state == states.IN_BRACES and |
| 494 | not re.search(r"^ *#", line)): |
Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 495 | enum_const = re.search(r"^ *(?P<enum_const>\w+)", line) |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 496 | if not enum_const: |
| 497 | continue |
| 498 | |
| 499 | enum_consts.append(Match( |
| 500 | header_file, |
| 501 | line, |
Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 502 | line_no, |
| 503 | enum_const.span("enum_const"), |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 504 | enum_const.group("enum_const"))) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 505 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 506 | return enum_consts |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 507 | |
Gilles Peskine | b4b18c1 | 2021-11-17 20:43:35 +0100 | [diff] [blame] | 508 | IGNORED_CHUNK_REGEX = re.compile('|'.join([ |
| 509 | r'/\*.*?\*/', # block comment entirely on one line |
| 510 | r'//.*', # line comment |
| 511 | r'(?P<string>")(?:[^\\\"]|\\.)*"', # string literal |
| 512 | ])) |
| 513 | |
Gilles Peskine | b9fc488 | 2021-11-17 20:32:31 +0100 | [diff] [blame] | 514 | def strip_comments_and_literals(self, line, in_block_comment): |
| 515 | """Strip comments and string literals from line. |
| 516 | |
| 517 | Continuation lines are not supported. |
| 518 | |
| 519 | If in_block_comment is true, assume that the line starts inside a |
| 520 | block comment. |
| 521 | |
| 522 | Return updated values of (line, in_block_comment) where: |
| 523 | * Comments in line have been replaced by a space (or nothing at the |
| 524 | start or end of the line). |
| 525 | * String contents have been removed. |
| 526 | * in_block_comment indicates whether the line ends inside a block |
| 527 | comment that continues on the next line. |
| 528 | """ |
Gilles Peskine | f303c0d | 2021-11-17 20:45:39 +0100 | [diff] [blame] | 529 | |
| 530 | # Terminate current multiline comment? |
Gilles Peskine | b9fc488 | 2021-11-17 20:32:31 +0100 | [diff] [blame] | 531 | if in_block_comment: |
Gilles Peskine | f303c0d | 2021-11-17 20:45:39 +0100 | [diff] [blame] | 532 | m = re.search(r"\*/", line) |
| 533 | if m: |
| 534 | in_block_comment = False |
| 535 | line = line[m.end(0):] |
| 536 | else: |
| 537 | return '', True |
Gilles Peskine | b4b18c1 | 2021-11-17 20:43:35 +0100 | [diff] [blame] | 538 | |
| 539 | # Remove full comments and string literals. |
| 540 | # Do it all together to handle cases like "/*" correctly. |
| 541 | # Note that continuation lines are not supported. |
| 542 | line = re.sub(self.IGNORED_CHUNK_REGEX, |
| 543 | lambda s: '""' if s.group('string') else ' ', |
Gilles Peskine | b9fc488 | 2021-11-17 20:32:31 +0100 | [diff] [blame] | 544 | line) |
Gilles Peskine | b4b18c1 | 2021-11-17 20:43:35 +0100 | [diff] [blame] | 545 | |
Gilles Peskine | b9fc488 | 2021-11-17 20:32:31 +0100 | [diff] [blame] | 546 | # Start an unfinished comment? |
Gilles Peskine | b4b18c1 | 2021-11-17 20:43:35 +0100 | [diff] [blame] | 547 | # (If `/*` was part of a complete comment, it's already been removed.) |
Gilles Peskine | f303c0d | 2021-11-17 20:45:39 +0100 | [diff] [blame] | 548 | m = re.search(r"/\*", line) |
Gilles Peskine | b9fc488 | 2021-11-17 20:32:31 +0100 | [diff] [blame] | 549 | if m: |
| 550 | in_block_comment = True |
Gilles Peskine | f303c0d | 2021-11-17 20:45:39 +0100 | [diff] [blame] | 551 | line = line[:m.start(0)] |
Gilles Peskine | b4b18c1 | 2021-11-17 20:43:35 +0100 | [diff] [blame] | 552 | |
Gilles Peskine | b9fc488 | 2021-11-17 20:32:31 +0100 | [diff] [blame] | 553 | return line, in_block_comment |
| 554 | |
Gilles Peskine | 9b2fa72 | 2021-11-17 20:23:18 +0100 | [diff] [blame] | 555 | IDENTIFIER_REGEX = re.compile('|'.join([ |
Gilles Peskine | 152de23 | 2021-11-16 20:56:47 +0100 | [diff] [blame] | 556 | # Match " something(a" or " *something(a". Functions. |
| 557 | # Assumptions: |
| 558 | # - function definition from return type to one of its arguments is |
| 559 | # all on one line |
| 560 | # - function definition line only contains alphanumeric, asterisk, |
| 561 | # underscore, and open bracket |
Gilles Peskine | 9b2fa72 | 2021-11-17 20:23:18 +0100 | [diff] [blame] | 562 | r".* \**(\w+) *\( *\w", |
Gilles Peskine | 152de23 | 2021-11-16 20:56:47 +0100 | [diff] [blame] | 563 | # Match "(*something)(". |
Gilles Peskine | 9b2fa72 | 2021-11-17 20:23:18 +0100 | [diff] [blame] | 564 | r".*\( *\* *(\w+) *\) *\(", |
Gilles Peskine | 152de23 | 2021-11-16 20:56:47 +0100 | [diff] [blame] | 565 | # Match names of named data structures. |
Gilles Peskine | 9b2fa72 | 2021-11-17 20:23:18 +0100 | [diff] [blame] | 566 | r"(?:typedef +)?(?:struct|union|enum) +(\w+)(?: *{)?$", |
Gilles Peskine | 152de23 | 2021-11-16 20:56:47 +0100 | [diff] [blame] | 567 | # Match names of typedef instances, after closing bracket. |
Gilles Peskine | 9b2fa72 | 2021-11-17 20:23:18 +0100 | [diff] [blame] | 568 | r"}? *(\w+)[;[].*", |
| 569 | ])) |
Gilles Peskine | 152de23 | 2021-11-16 20:56:47 +0100 | [diff] [blame] | 570 | # The regex below is indented for clarity. |
Gilles Peskine | 9b2fa72 | 2021-11-17 20:23:18 +0100 | [diff] [blame] | 571 | EXCLUSION_LINES = re.compile("|".join([ |
| 572 | r"extern +\"C\"", |
| 573 | r"(typedef +)?(struct|union|enum)( *{)?$", |
| 574 | r"} *;?$", |
| 575 | r"$", |
| 576 | r"//", |
| 577 | r"#", |
| 578 | ])) |
Gilles Peskine | 152de23 | 2021-11-16 20:56:47 +0100 | [diff] [blame] | 579 | |
| 580 | def parse_identifiers_in_file(self, header_file, identifiers): |
| 581 | """ |
| 582 | Parse all lines of a header where a function/enum/struct/union/typedef |
| 583 | identifier is declared, based on some regex and heuristics. Highly |
| 584 | dependent on formatting style. |
| 585 | |
| 586 | Append found matches to the list ``identifiers``. |
| 587 | """ |
| 588 | |
| 589 | with open(header_file, "r", encoding="utf-8") as header: |
| 590 | in_block_comment = False |
| 591 | # The previous line variable is used for concatenating lines |
| 592 | # when identifiers are formatted and spread across multiple |
| 593 | # lines. |
| 594 | previous_line = "" |
| 595 | |
| 596 | for line_no, line in enumerate(header): |
Gilles Peskine | b9fc488 | 2021-11-17 20:32:31 +0100 | [diff] [blame] | 597 | line, in_block_comment = \ |
| 598 | self.strip_comments_and_literals(line, in_block_comment) |
Gilles Peskine | 152de23 | 2021-11-16 20:56:47 +0100 | [diff] [blame] | 599 | |
Gilles Peskine | 9b2fa72 | 2021-11-17 20:23:18 +0100 | [diff] [blame] | 600 | if self.EXCLUSION_LINES.match(line): |
Gilles Peskine | 152de23 | 2021-11-16 20:56:47 +0100 | [diff] [blame] | 601 | previous_line = "" |
| 602 | continue |
| 603 | |
| 604 | # If the line contains only space-separated alphanumeric |
Gilles Peskine | bc1e8f6 | 2021-11-17 20:39:56 +0100 | [diff] [blame] | 605 | # characters (or underscore, asterisk, or open parenthesis), |
Gilles Peskine | 152de23 | 2021-11-16 20:56:47 +0100 | [diff] [blame] | 606 | # and nothing else, high chance it's a declaration that |
| 607 | # continues on the next line |
| 608 | if re.search(r"^([\w\*\(]+\s+)+$", line): |
| 609 | previous_line += line |
| 610 | continue |
| 611 | |
| 612 | # If previous line seemed to start an unfinished declaration |
| 613 | # (as above), concat and treat them as one. |
| 614 | if previous_line: |
| 615 | line = previous_line.strip() + " " + line.strip() + "\n" |
| 616 | previous_line = "" |
| 617 | |
| 618 | # Skip parsing if line has a space in front = heuristic to |
| 619 | # skip function argument lines (highly subject to formatting |
| 620 | # changes) |
| 621 | if line[0] == " ": |
| 622 | continue |
| 623 | |
| 624 | identifier = self.IDENTIFIER_REGEX.search(line) |
| 625 | |
| 626 | if not identifier: |
| 627 | continue |
| 628 | |
| 629 | # Find the group that matched, and append it |
| 630 | for group in identifier.groups(): |
| 631 | if not group: |
| 632 | continue |
| 633 | |
| 634 | identifiers.append(Match( |
| 635 | header_file, |
| 636 | line, |
| 637 | line_no, |
| 638 | identifier.span(), |
| 639 | group)) |
| 640 | |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 641 | def parse_identifiers(self, include, exclude=None): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 642 | """ |
Yuto Takano | 8246eb8 | 2021-08-16 10:37:24 +0100 | [diff] [blame] | 643 | Parse all lines of a header where a function/enum/struct/union/typedef |
Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 644 | identifier is declared, based on some regex and heuristics. Highly |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 645 | dependent on formatting style. Identifiers in excluded files are still |
| 646 | parsed |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 647 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 648 | Args: |
Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 649 | * include: A List of glob expressions to look for files through. |
| 650 | * exclude: A List of glob expressions for excluding files. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 651 | |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 652 | Returns: a Tuple of two Lists of Match objects with identifiers. |
| 653 | * included_identifiers: A List of Match objects with identifiers from |
| 654 | included files. |
| 655 | * excluded_identifiers: A List of Match objects with identifiers from |
| 656 | excluded files. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 657 | """ |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 658 | |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 659 | included_files, excluded_files = \ |
| 660 | self.get_all_files(include, exclude) |
Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 661 | |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 662 | self.log.debug("Looking for included identifiers in {} files".format \ |
| 663 | (len(included_files))) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 664 | |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 665 | included_identifiers = [] |
| 666 | excluded_identifiers = [] |
| 667 | for header_file in included_files: |
| 668 | self.parse_identifiers_in_file(header_file, included_identifiers) |
| 669 | for header_file in excluded_files: |
| 670 | self.parse_identifiers_in_file(header_file, excluded_identifiers) |
| 671 | |
| 672 | return (included_identifiers, excluded_identifiers) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 673 | |
| 674 | def parse_symbols(self): |
| 675 | """ |
| 676 | Compile the Mbed TLS libraries, and parse the TLS, Crypto, and x509 |
| 677 | object files using nm to retrieve the list of referenced symbols. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 678 | Exceptions thrown here are rethrown because they would be critical |
| 679 | errors that void several tests, and thus needs to halt the program. This |
| 680 | is explicitly done for clarity. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 681 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 682 | Returns a List of unique symbols defined and used in the libraries. |
| 683 | """ |
| 684 | self.log.info("Compiling...") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 685 | symbols = [] |
| 686 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 687 | # Back up the config and atomically compile with the full configuration. |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 688 | shutil.copy( |
| 689 | "include/mbedtls/mbedtls_config.h", |
| 690 | "include/mbedtls/mbedtls_config.h.bak" |
| 691 | ) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 692 | try: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 693 | # Use check=True in all subprocess calls so that failures are raised |
| 694 | # as exceptions and logged. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 695 | subprocess.run( |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 696 | ["python3", "scripts/config.py", "full"], |
Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 697 | universal_newlines=True, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 698 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 699 | ) |
| 700 | my_environment = os.environ.copy() |
| 701 | my_environment["CFLAGS"] = "-fno-asynchronous-unwind-tables" |
Yuto Takano | 4b7d23d | 2021-08-17 10:48:22 +0100 | [diff] [blame] | 702 | # Run make clean separately to lib to prevent unwanted behavior when |
| 703 | # make is invoked with parallelism. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 704 | subprocess.run( |
Yuto Takano | 4b7d23d | 2021-08-17 10:48:22 +0100 | [diff] [blame] | 705 | ["make", "clean"], |
| 706 | universal_newlines=True, |
| 707 | check=True |
| 708 | ) |
| 709 | subprocess.run( |
| 710 | ["make", "lib"], |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 711 | env=my_environment, |
Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 712 | universal_newlines=True, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 713 | stdout=subprocess.PIPE, |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 714 | stderr=subprocess.STDOUT, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 715 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 716 | ) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 717 | |
| 718 | # Perform object file analysis using nm |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 719 | symbols = self.parse_symbols_from_nm([ |
| 720 | "library/libmbedcrypto.a", |
| 721 | "library/libmbedtls.a", |
| 722 | "library/libmbedx509.a" |
| 723 | ]) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 724 | |
| 725 | subprocess.run( |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 726 | ["make", "clean"], |
Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 727 | universal_newlines=True, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 728 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 729 | ) |
| 730 | except subprocess.CalledProcessError as error: |
Yuto Takano | 25eeb7b | 2021-08-06 21:27:59 +0100 | [diff] [blame] | 731 | self.log.debug(error.output) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 732 | raise error |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 733 | finally: |
Yuto Takano | 6fececf | 2021-08-07 17:28:23 +0100 | [diff] [blame] | 734 | # Put back the original config regardless of there being errors. |
| 735 | # Works also for keyboard interrupts. |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 736 | shutil.move( |
| 737 | "include/mbedtls/mbedtls_config.h.bak", |
| 738 | "include/mbedtls/mbedtls_config.h" |
| 739 | ) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 740 | |
| 741 | return symbols |
| 742 | |
| 743 | def parse_symbols_from_nm(self, object_files): |
| 744 | """ |
| 745 | Run nm to retrieve the list of referenced symbols in each object file. |
| 746 | Does not return the position data since it is of no use. |
| 747 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 748 | Args: |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 749 | * object_files: a List of compiled object filepaths to search through. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 750 | |
| 751 | Returns a List of unique symbols defined and used in any of the object |
| 752 | files. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 753 | """ |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 754 | nm_undefined_regex = re.compile(r"^\S+: +U |^$|^\S+:$") |
| 755 | 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] | 756 | exclusions = ("FStar", "Hacl") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 757 | |
| 758 | symbols = [] |
| 759 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 760 | # Gather all outputs of nm |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 761 | nm_output = "" |
| 762 | for lib in object_files: |
| 763 | nm_output += subprocess.run( |
| 764 | ["nm", "-og", lib], |
Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 765 | universal_newlines=True, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 766 | stdout=subprocess.PIPE, |
| 767 | stderr=subprocess.STDOUT, |
| 768 | check=True |
| 769 | ).stdout |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 770 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 771 | for line in nm_output.splitlines(): |
Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 772 | if not nm_undefined_regex.search(line): |
| 773 | symbol = nm_valid_regex.search(line) |
Yuto Takano | 12a7ecd | 2021-08-07 00:40:29 +0100 | [diff] [blame] | 774 | if (symbol and not symbol.group("symbol").startswith(exclusions)): |
Yuto Takano | e77f699 | 2021-08-05 20:22:59 +0100 | [diff] [blame] | 775 | symbols.append(symbol.group("symbol")) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 776 | else: |
| 777 | self.log.error(line) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 778 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 779 | return symbols |
| 780 | |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 781 | class NameChecker(): |
| 782 | """ |
| 783 | Representation of the core name checking operation performed by this script. |
| 784 | """ |
| 785 | def __init__(self, parse_result, log): |
| 786 | self.parse_result = parse_result |
| 787 | self.log = log |
| 788 | |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 789 | def perform_checks(self, quiet=False): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 790 | """ |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 791 | A comprehensive checker that performs each check in order, and outputs |
| 792 | a final verdict. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 793 | |
| 794 | Args: |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 795 | * quiet: whether to hide detailed problem explanation. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 796 | """ |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 797 | self.log.info("=============") |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 798 | Problem.quiet = quiet |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 799 | problems = 0 |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 800 | problems += self.check_symbols_declared_in_header() |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 801 | |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 802 | pattern_checks = [ |
Janos Follath | 9938719 | 2022-08-10 11:11:34 +0100 | [diff] [blame] | 803 | ("public_macros", PUBLIC_MACRO_PATTERN), |
| 804 | ("internal_macros", INTERNAL_MACRO_PATTERN), |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 805 | ("enum_consts", CONSTANTS_PATTERN), |
| 806 | ("identifiers", IDENTIFIER_PATTERN) |
| 807 | ] |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 808 | for group, check_pattern in pattern_checks: |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 809 | problems += self.check_match_pattern(group, check_pattern) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 810 | |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 811 | problems += self.check_for_typos() |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 812 | |
| 813 | self.log.info("=============") |
| 814 | if problems > 0: |
| 815 | self.log.info("FAIL: {0} problem(s) to fix".format(str(problems))) |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 816 | if quiet: |
| 817 | self.log.info("Remove --quiet to see explanations.") |
Yuto Takano | fc54dfb | 2021-08-07 17:18:28 +0100 | [diff] [blame] | 818 | else: |
| 819 | self.log.info("Use --quiet for minimal output.") |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 820 | return 1 |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 821 | else: |
| 822 | self.log.info("PASS") |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 823 | return 0 |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 824 | |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 825 | def check_symbols_declared_in_header(self): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 826 | """ |
| 827 | Perform a check that all detected symbols in the library object files |
| 828 | are properly declared in headers. |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 829 | Assumes parse_names_in_source() was called before this. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 830 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 831 | Returns the number of problems that need fixing. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 832 | """ |
| 833 | problems = [] |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 834 | all_identifiers = self.parse_result["identifiers"] + \ |
| 835 | self.parse_result["excluded_identifiers"] |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 836 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 837 | for symbol in self.parse_result["symbols"]: |
| 838 | found_symbol_declared = False |
Aditya Deshpande | dd8ac67 | 2023-01-16 14:57:21 +0000 | [diff] [blame] | 839 | for identifier_match in all_identifiers: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 840 | if symbol == identifier_match.name: |
| 841 | found_symbol_declared = True |
| 842 | break |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 843 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 844 | if not found_symbol_declared: |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 845 | problems.append(SymbolNotInHeader(symbol)) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 846 | |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 847 | self.output_check_result("All symbols in header", problems) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 848 | return len(problems) |
| 849 | |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 850 | def check_match_pattern(self, group_to_check, check_pattern): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 851 | """ |
| 852 | 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] | 853 | Assumes parse_names_in_source() was called before this. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 854 | |
| 855 | Args: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 856 | * group_to_check: string key to index into self.parse_result. |
| 857 | * check_pattern: the regex to check against. |
| 858 | |
| 859 | Returns the number of problems that need fixing. |
| 860 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 861 | problems = [] |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 862 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 863 | for item_match in self.parse_result[group_to_check]: |
Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 864 | if not re.search(check_pattern, item_match.name): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 865 | problems.append(PatternMismatch(check_pattern, item_match)) |
Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 866 | # Double underscore should not be used for names |
| 867 | if re.search(r".*__.*", item_match.name): |
Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 868 | problems.append( |
| 869 | PatternMismatch("no double underscore allowed", item_match)) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 870 | |
| 871 | self.output_check_result( |
| 872 | "Naming patterns of {}".format(group_to_check), |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 873 | problems) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 874 | return len(problems) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 875 | |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 876 | def check_for_typos(self): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 877 | """ |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 878 | Perform a check that all words in the source code beginning with MBED are |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 879 | either defined as macros, or as enum constants. |
Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 880 | Assumes parse_names_in_source() was called before this. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 881 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 882 | Returns the number of problems that need fixing. |
| 883 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 884 | problems = [] |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 885 | |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 886 | # Set comprehension, equivalent to a list comprehension wrapped by set() |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 887 | all_caps_names = { |
| 888 | match.name |
| 889 | for match |
Janos Follath | 9938719 | 2022-08-10 11:11:34 +0100 | [diff] [blame] | 890 | in self.parse_result["public_macros"] + |
Janos Follath | 8d59c86 | 2022-08-10 15:35:35 +0100 | [diff] [blame] | 891 | self.parse_result["internal_macros"] + |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 892 | self.parse_result["private_macros"] + |
Janos Follath | 8d59c86 | 2022-08-10 15:35:35 +0100 | [diff] [blame] | 893 | self.parse_result["enum_consts"] |
Janos Follath | 9938719 | 2022-08-10 11:11:34 +0100 | [diff] [blame] | 894 | } |
Ronald Cron | 7975fae | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 895 | typo_exclusion = re.compile(r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$|" |
Pengyu Lv | f3f1f81 | 2022-11-08 16:56:51 +0800 | [diff] [blame] | 896 | r"MBEDTLS_TEST_LIBTESTDRIVER*|" |
Pengyu Lv | 3bb0e43 | 2022-11-24 17:29:05 +0800 | [diff] [blame] | 897 | r"PSA_CRYPTO_DRIVER_TEST") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 898 | |
Pengyu Lv | cdac0d5 | 2022-11-08 15:55:00 +0800 | [diff] [blame] | 899 | for name_match in self.parse_result["mbed_psa_words"]: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 900 | found = name_match.name in all_caps_names |
| 901 | |
| 902 | # Since MBEDTLS_PSA_ACCEL_XXX defines are defined by the |
| 903 | # PSA driver, they will not exist as macros. However, they |
| 904 | # should still be checked for typos using the equivalent |
| 905 | # BUILTINs that exist. |
| 906 | if "MBEDTLS_PSA_ACCEL_" in name_match.name: |
| 907 | found = name_match.name.replace( |
| 908 | "MBEDTLS_PSA_ACCEL_", |
| 909 | "MBEDTLS_PSA_BUILTIN_") in all_caps_names |
| 910 | |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 911 | if not found and not typo_exclusion.search(name_match.name): |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 912 | problems.append(Typo(name_match)) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 913 | |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 914 | self.output_check_result("Likely typos", problems) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 915 | return len(problems) |
| 916 | |
Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 917 | def output_check_result(self, name, problems): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 918 | """ |
| 919 | Write out the PASS/FAIL status of a performed check depending on whether |
| 920 | there were problems. |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 921 | |
| 922 | Args: |
Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 923 | * name: the name of the test |
| 924 | * problems: a List of encountered Problems |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 925 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 926 | if problems: |
Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 927 | self.log.info("{}: FAIL\n".format(name)) |
| 928 | for problem in problems: |
| 929 | self.log.warning(str(problem)) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 930 | else: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 931 | self.log.info("{}: PASS".format(name)) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 932 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 933 | def main(): |
| 934 | """ |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 935 | Perform argument parsing, and create an instance of CodeParser and |
| 936 | NameChecker to begin the core operation. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 937 | """ |
Yuto Takano | f005c33 | 2021-08-09 13:56:36 +0100 | [diff] [blame] | 938 | parser = argparse.ArgumentParser( |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 939 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 940 | description=( |
| 941 | "This script confirms that the naming of all symbols and identifiers " |
| 942 | "in Mbed TLS are consistent with the house style and are also " |
| 943 | "self-consistent.\n\n" |
Yuto Takano | f005c33 | 2021-08-09 13:56:36 +0100 | [diff] [blame] | 944 | "Expected to be run from the MbedTLS root directory.") |
| 945 | ) |
| 946 | parser.add_argument( |
| 947 | "-v", "--verbose", |
| 948 | action="store_true", |
| 949 | help="show parse results" |
| 950 | ) |
| 951 | parser.add_argument( |
| 952 | "-q", "--quiet", |
| 953 | action="store_true", |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 954 | help="hide unnecessary text, explanations, and highlights" |
Yuto Takano | f005c33 | 2021-08-09 13:56:36 +0100 | [diff] [blame] | 955 | ) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 956 | |
Yuto Takano | f005c33 | 2021-08-09 13:56:36 +0100 | [diff] [blame] | 957 | args = parser.parse_args() |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 958 | |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 959 | # Configure the global logger, which is then passed to the classes below |
| 960 | log = logging.getLogger() |
| 961 | log.setLevel(logging.DEBUG if args.verbose else logging.INFO) |
| 962 | log.addHandler(logging.StreamHandler()) |
| 963 | |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 964 | try: |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 965 | code_parser = CodeParser(log) |
| 966 | parse_result = code_parser.comprehensive_parse() |
Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 967 | except Exception: # pylint: disable=broad-except |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 968 | traceback.print_exc() |
| 969 | sys.exit(2) |
| 970 | |
Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 971 | name_checker = NameChecker(parse_result, log) |
| 972 | return_code = name_checker.perform_checks(quiet=args.quiet) |
| 973 | |
| 974 | sys.exit(return_code) |
| 975 | |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 976 | if __name__ == "__main__": |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 977 | main() |