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