2021-08-05 20:47:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright The Mbed TLS Contributors
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2018-05-08 16:30:59 +02:00
|
|
|
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
This script confirms that the naming of all symbols and identifiers in Mbed TLS
|
2021-08-06 17:22:06 +02:00
|
|
|
are consistent with the house style and are also self-consistent. It performs
|
|
|
|
the following checks:
|
|
|
|
|
|
|
|
- All exported and available symbols in the library object files, are explicitly
|
|
|
|
declared in the header files.
|
|
|
|
- All macros, constants, and identifiers (function names, struct names, etc)
|
|
|
|
follow the required pattern.
|
|
|
|
- Typo checking: All words that begin with MBED exist as macros or constants.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import textwrap
|
2018-05-08 16:30:59 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import logging
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
# Naming patterns to check against. These are defined outside the NameCheck
|
|
|
|
# class for ease of modification.
|
2021-08-05 20:57:58 +02:00
|
|
|
MACRO_PATTERN = r"^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$"
|
2021-08-06 17:22:06 +02:00
|
|
|
CONSTANTS_PATTERN = MACRO_PATTERN
|
2021-08-05 20:52:09 +02:00
|
|
|
IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$"
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
class Match(object):
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
A class representing a match, together with its found position.
|
|
|
|
|
|
|
|
Fields:
|
|
|
|
* filename: the file that the match was in.
|
|
|
|
* line: the full line containing the match.
|
|
|
|
* pos: a tuple of (start, end) positions on the line where the match is.
|
|
|
|
* name: the match itself.
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
def __init__(self, filename, line, pos, name):
|
|
|
|
self.filename = filename
|
|
|
|
self.line = line
|
|
|
|
self.pos = pos
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
class Problem(object):
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
A parent class representing a form of static analysis error.
|
|
|
|
|
|
|
|
Fields:
|
|
|
|
* textwrapper: a TextWrapper instance to format problems nicely.
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.textwrapper = textwrap.TextWrapper()
|
2021-08-06 17:22:06 +02:00
|
|
|
self.textwrapper.width = 80
|
|
|
|
self.textwrapper.initial_indent = " * "
|
|
|
|
self.textwrapper.subsequent_indent = " "
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
class SymbolNotInHeader(Problem):
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
A problem that occurs when an exported/available symbol in the object file
|
|
|
|
is not explicitly declared in header files. Created with
|
|
|
|
NameCheck.check_symbols_declared_in_header()
|
|
|
|
|
|
|
|
Fields:
|
|
|
|
* symbol_name: the name of the symbol.
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
def __init__(self, symbol_name):
|
|
|
|
self.symbol_name = symbol_name
|
|
|
|
Problem.__init__(self)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.textwrapper.fill(
|
|
|
|
"'{0}' was found as an available symbol in the output of nm, "
|
|
|
|
"however it was not declared in any header files."
|
|
|
|
.format(self.symbol_name))
|
|
|
|
|
|
|
|
class PatternMismatch(Problem):
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
A problem that occurs when something doesn't match the expected pattern.
|
|
|
|
Created with NameCheck.check_match_pattern()
|
|
|
|
|
|
|
|
Fields:
|
|
|
|
* pattern: the expected regex pattern
|
|
|
|
* match: the Match object in question
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
def __init__(self, pattern, match):
|
|
|
|
self.pattern = pattern
|
|
|
|
self.match = match
|
|
|
|
Problem.__init__(self)
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.textwrapper.fill(
|
|
|
|
"{0}: '{1}' does not match the required pattern '{2}'."
|
|
|
|
.format(self.match.filename, self.match.name, self.pattern))
|
|
|
|
|
|
|
|
class Typo(Problem):
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
A problem that occurs when a word using MBED doesn't appear to be defined as
|
|
|
|
constants nor enum values. Created with NameCheck.check_for_typos()
|
|
|
|
|
|
|
|
Fields:
|
|
|
|
* match: the Match object of the MBED name in question.
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
def __init__(self, match):
|
|
|
|
self.match = match
|
|
|
|
Problem.__init__(self)
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.textwrapper.fill(
|
|
|
|
"{0}: '{1}' looks like a typo. It was not found in any macros or "
|
|
|
|
"any enums. If this is not a typo, put //no-check-names after it."
|
|
|
|
.format(self.match.filename, self.match.name))
|
2018-05-08 16:30:59 +02:00
|
|
|
|
|
|
|
class NameCheck(object):
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
Representation of the core name checking operation performed by this script.
|
|
|
|
Shares a common logger, common excluded filenames, and a shared return_code.
|
|
|
|
"""
|
2018-05-08 16:30:59 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.log = None
|
|
|
|
self.check_repo_path()
|
|
|
|
self.return_code = 0
|
2021-08-06 17:22:06 +02:00
|
|
|
self.excluded_files = ["bn_mul", "compat-2.x.h"]
|
2018-05-08 16:30:59 +02:00
|
|
|
|
|
|
|
def set_return_code(self, return_code):
|
|
|
|
if return_code > self.return_code:
|
|
|
|
self.return_code = return_code
|
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
def setup_logger(self, verbose=False):
|
|
|
|
"""
|
|
|
|
Set up a logger and set the change the default logging level from
|
2021-08-06 17:22:06 +02:00
|
|
|
WARNING to INFO. Loggers are better than print statements since their
|
2021-08-05 20:47:48 +02:00
|
|
|
verbosity can be controlled.
|
|
|
|
"""
|
2018-05-08 16:30:59 +02:00
|
|
|
self.log = logging.getLogger()
|
2021-08-05 20:47:48 +02:00
|
|
|
if verbose:
|
|
|
|
self.log.setLevel(logging.DEBUG)
|
|
|
|
else:
|
|
|
|
self.log.setLevel(logging.INFO)
|
2018-05-08 16:30:59 +02:00
|
|
|
self.log.addHandler(logging.StreamHandler())
|
|
|
|
|
|
|
|
def check_repo_path(self):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
Check that the current working directory is the project root, and throw
|
|
|
|
an exception if not.
|
|
|
|
"""
|
2018-05-08 16:30:59 +02:00
|
|
|
current_dir = os.path.realpath('.')
|
|
|
|
root_dir = os.path.dirname(os.path.dirname(
|
|
|
|
os.path.dirname(os.path.realpath(__file__))))
|
|
|
|
if current_dir != root_dir:
|
|
|
|
raise Exception("Must be run from Mbed TLS root")
|
|
|
|
|
2021-08-05 21:10:45 +02:00
|
|
|
def get_files(self, extension, directory):
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
Get all files that end with .extension in the specified directory
|
|
|
|
recursively.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
* extension: the file extension to search for, without the dot
|
|
|
|
* directory: the directory to recursively search for
|
|
|
|
|
|
|
|
Returns a List of relative filepaths.
|
|
|
|
"""
|
2018-05-08 16:30:59 +02:00
|
|
|
filenames = []
|
|
|
|
for root, dirs, files in sorted(os.walk(directory)):
|
|
|
|
for filename in sorted(files):
|
|
|
|
if (filename not in self.excluded_files and
|
2021-08-05 21:10:45 +02:00
|
|
|
filename.endswith("." + extension)):
|
2018-05-08 16:30:59 +02:00
|
|
|
filenames.append(os.path.join(root, filename))
|
|
|
|
return filenames
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
def parse_names_in_source(self):
|
|
|
|
"""
|
|
|
|
Calls each parsing function to retrieve various elements of the code,
|
|
|
|
together with their source location. Puts the parsed values in the
|
|
|
|
internal variable self.parse_result.
|
|
|
|
"""
|
|
|
|
self.log.info("Parsing source code...")
|
|
|
|
|
|
|
|
m_headers = self.get_files("h", os.path.join("include", "mbedtls"))
|
|
|
|
p_headers = self.get_files("h", os.path.join("include", "psa"))
|
|
|
|
t_headers = ["3rdparty/everest/include/everest/everest.h",
|
|
|
|
"3rdparty/everest/include/everest/x25519.h"]
|
|
|
|
d_headers = self.get_files("h", os.path.join("tests", "include", "test", "drivers"))
|
|
|
|
l_headers = self.get_files("h", "library")
|
|
|
|
libraries = self.get_files("c", "library") + [
|
|
|
|
"3rdparty/everest/library/everest.c",
|
|
|
|
"3rdparty/everest/library/x25519.c"]
|
|
|
|
|
|
|
|
all_macros = self.parse_macros(
|
|
|
|
m_headers + p_headers + t_headers + l_headers + d_headers)
|
|
|
|
enum_consts = self.parse_enum_consts(
|
|
|
|
m_headers + l_headers + t_headers)
|
|
|
|
identifiers = self.parse_identifiers(
|
|
|
|
m_headers + p_headers + t_headers + l_headers)
|
|
|
|
mbed_names = self.parse_MBED_names(
|
|
|
|
m_headers + p_headers + t_headers + l_headers + libraries)
|
|
|
|
symbols = self.parse_symbols()
|
|
|
|
|
|
|
|
# Remove identifier macros like mbedtls_printf or mbedtls_calloc
|
|
|
|
identifiers_justname = [x.name for x in identifiers]
|
|
|
|
actual_macros = []
|
|
|
|
for macro in all_macros:
|
|
|
|
if macro.name not in identifiers_justname:
|
|
|
|
actual_macros.append(macro)
|
|
|
|
|
|
|
|
self.log.debug("Found:")
|
|
|
|
self.log.debug(" {} Macros".format(len(all_macros)))
|
|
|
|
self.log.debug(" {} Non-identifier Macros".format(len(actual_macros)))
|
|
|
|
self.log.debug(" {} Enum Constants".format(len(enum_consts)))
|
|
|
|
self.log.debug(" {} Identifiers".format(len(identifiers)))
|
|
|
|
self.log.debug(" {} Exported Symbols".format(len(symbols)))
|
|
|
|
self.log.info("Analysing...")
|
|
|
|
|
|
|
|
self.parse_result = {
|
|
|
|
"macros": actual_macros,
|
|
|
|
"enum_consts": enum_consts,
|
|
|
|
"identifiers": identifiers,
|
|
|
|
"symbols": symbols,
|
|
|
|
"mbed_names": mbed_names
|
|
|
|
}
|
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
def parse_macros(self, header_files):
|
|
|
|
"""
|
|
|
|
Parse all macros defined by #define preprocessor directives.
|
|
|
|
|
|
|
|
Args:
|
2021-08-06 17:22:06 +02:00
|
|
|
* header_files: A List of filepaths to look through.
|
|
|
|
|
|
|
|
Returns a List of Match objects for the found macros.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
MACRO_REGEX = r"#define (?P<macro>\w+)"
|
|
|
|
NON_MACROS = (
|
|
|
|
"asm", "inline", "EMIT", "_CRT_SECURE_NO_DEPRECATE", "MULADDC_"
|
|
|
|
)
|
|
|
|
|
|
|
|
macros = []
|
|
|
|
|
|
|
|
for header_file in header_files:
|
2018-05-08 16:30:59 +02:00
|
|
|
with open(header_file, "r") as header:
|
2021-08-05 20:47:48 +02:00
|
|
|
for line in header:
|
2021-08-06 17:22:06 +02:00
|
|
|
for macro in re.finditer(MACRO_REGEX, line):
|
|
|
|
if not macro.group("macro").startswith(NON_MACROS):
|
|
|
|
macros.append(Match(
|
|
|
|
header_file,
|
|
|
|
line,
|
|
|
|
(macro.start(), macro.end()),
|
|
|
|
macro.group("macro")))
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
return macros
|
|
|
|
|
2021-08-05 20:57:58 +02:00
|
|
|
def parse_MBED_names(self, files):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
Parse all words in the file that begin with MBED. Includes macros.
|
2021-08-06 17:22:06 +02:00
|
|
|
There have been typos of TLS, hence the broader check than MBEDTLS.
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
Args:
|
2021-08-06 17:22:06 +02:00
|
|
|
* files: a List of filepaths to look through.
|
|
|
|
|
|
|
|
Returns a List of Match objects for words beginning with MBED.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
MBED_names = []
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:57:58 +02:00
|
|
|
for filename in files:
|
2021-08-05 20:47:48 +02:00
|
|
|
with open(filename, "r") as fp:
|
|
|
|
for line in fp:
|
2021-08-06 17:22:06 +02:00
|
|
|
# Ignore any names that are deliberately opted-out or in
|
|
|
|
# legacy error directives
|
|
|
|
if re.search(r"// *no-check-names|#error", line):
|
2021-08-05 21:17:07 +02:00
|
|
|
continue
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
for name in re.finditer(r"\bMBED.+?_[A-Z0-9_]*", line):
|
|
|
|
MBED_names.append(Match(
|
|
|
|
filename,
|
|
|
|
line,
|
|
|
|
(name.start(), name.end()),
|
|
|
|
name.group(0)
|
|
|
|
))
|
|
|
|
|
|
|
|
return MBED_names
|
|
|
|
|
|
|
|
def parse_enum_consts(self, header_files):
|
|
|
|
"""
|
|
|
|
Parse all enum value constants that are declared.
|
|
|
|
|
|
|
|
Args:
|
2021-08-06 17:22:06 +02:00
|
|
|
* header_files: A List of filepaths to look through.
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
Returns a List of Match objects for the findings.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
enum_consts = []
|
|
|
|
|
|
|
|
for header_file in header_files:
|
|
|
|
# Emulate a finite state machine to parse enum declarations.
|
2021-08-06 17:22:06 +02:00
|
|
|
# 0 = not in enum
|
|
|
|
# 1 = inside enum
|
|
|
|
# 2 = almost inside enum
|
2018-05-08 16:30:59 +02:00
|
|
|
state = 0
|
|
|
|
with open(header_file, "r") as header:
|
2021-08-05 20:47:48 +02:00
|
|
|
for line in header:
|
2018-05-08 16:30:59 +02:00
|
|
|
if state is 0 and re.match(r"^(typedef )?enum {", line):
|
|
|
|
state = 1
|
|
|
|
elif state is 0 and re.match(r"^(typedef )?enum", line):
|
|
|
|
state = 2
|
|
|
|
elif state is 2 and re.match(r"^{", line):
|
|
|
|
state = 1
|
|
|
|
elif state is 1 and re.match(r"^}", line):
|
|
|
|
state = 0
|
2021-08-05 21:32:55 +02:00
|
|
|
elif state is 1 and not re.match(r"^#", line):
|
2018-05-08 16:30:59 +02:00
|
|
|
enum_const = re.match(r"^\s*(?P<enum_const>\w+)", line)
|
|
|
|
if enum_const:
|
2021-08-05 20:47:48 +02:00
|
|
|
enum_consts.append(Match(
|
|
|
|
header_file,
|
|
|
|
line,
|
|
|
|
(enum_const.start(), enum_const.end()),
|
|
|
|
enum_const.group("enum_const")))
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
return enum_consts
|
|
|
|
|
|
|
|
def parse_identifiers(self, header_files):
|
|
|
|
"""
|
|
|
|
Parse all lines of a header where a function identifier is declared,
|
2021-08-06 17:22:06 +02:00
|
|
|
based on some huersitics. Highly dependent on formatting style.
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
Args:
|
2021-08-06 17:22:06 +02:00
|
|
|
* header_files: A List of filepaths to look through.
|
|
|
|
|
|
|
|
Returns a List of Match objects with identifiers.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-06 17:22:06 +02:00
|
|
|
EXCLUDED_LINES = (
|
|
|
|
r"^("
|
|
|
|
r"extern \"C\"|"
|
|
|
|
r"(typedef )?(struct|union|enum)( {)?$|"
|
|
|
|
r"};?$|"
|
|
|
|
r"$|"
|
|
|
|
r"//|"
|
|
|
|
r"#"
|
|
|
|
r")"
|
2018-05-08 16:30:59 +02:00
|
|
|
)
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
identifiers = []
|
|
|
|
|
|
|
|
for header_file in header_files:
|
2018-05-08 16:30:59 +02:00
|
|
|
with open(header_file, "r") as header:
|
2021-08-05 20:47:48 +02:00
|
|
|
in_block_comment = False
|
2021-08-06 17:22:06 +02:00
|
|
|
previous_line = None
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
for line in header:
|
2021-08-06 17:22:06 +02:00
|
|
|
# Skip parsing this line if a block comment ends on it,
|
|
|
|
# but don't skip if it has just started -- there is a chance
|
|
|
|
# it ends on the same line.
|
2021-08-05 20:47:48 +02:00
|
|
|
if re.search(r"/\*", line):
|
2021-08-06 17:22:06 +02:00
|
|
|
in_block_comment = not in_block_comment
|
|
|
|
if re.search(r"\*/", line):
|
|
|
|
in_block_comment = not in_block_comment
|
2021-08-05 20:47:48 +02:00
|
|
|
continue
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
if in_block_comment:
|
|
|
|
previous_line = None
|
2021-08-05 20:47:48 +02:00
|
|
|
continue
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
if re.match(EXCLUDED_LINES, line):
|
|
|
|
previous_line = None
|
2021-08-05 20:47:48 +02:00
|
|
|
continue
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
# Match "^something something$", with optional inline/static
|
|
|
|
# This *might* be a function with its argument brackets on
|
|
|
|
# the next line, or a struct declaration, so keep note of it
|
|
|
|
if re.match(
|
|
|
|
r"(inline |static |typedef )*\w+ \w+$",
|
|
|
|
line):
|
|
|
|
previous_line = line
|
|
|
|
continue
|
|
|
|
|
|
|
|
# If previous line seemed to start an unfinished declaration
|
|
|
|
# (as above), and this line begins with a bracket, concat
|
|
|
|
# them and treat them as one line.
|
|
|
|
if previous_line and re.match(" *[\({]", line):
|
|
|
|
line = previous_line.strip() + line.strip()
|
|
|
|
previous_line = None
|
|
|
|
|
|
|
|
# Skip parsing if line has a space in front = hueristic to
|
|
|
|
# skip function argument lines (highly subject to formatting
|
|
|
|
# changes)
|
|
|
|
if line[0] == " ":
|
2021-08-05 20:47:48 +02:00
|
|
|
continue
|
2021-08-05 22:07:14 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
identifier = re.search(
|
2021-08-06 17:22:06 +02:00
|
|
|
# Match something(
|
|
|
|
r".* \**(\w+)\(|"
|
|
|
|
# Match (*something)(
|
|
|
|
r".*\( *\* *(\w+) *\) *\(|"
|
|
|
|
# Match names of named data structures
|
|
|
|
r"(?:typedef +)?(?:struct|union|enum) +(\w+)(?: *{)?$|"
|
|
|
|
# Match names of typedef instances, after closing bracket
|
|
|
|
r"}? *(\w+)[;[].*",
|
2021-08-05 20:47:48 +02:00
|
|
|
line
|
|
|
|
)
|
|
|
|
|
|
|
|
if identifier:
|
2021-08-06 17:22:06 +02:00
|
|
|
# Find the group that matched, and append it
|
2021-08-05 20:47:48 +02:00
|
|
|
for group in identifier.groups():
|
|
|
|
if group:
|
|
|
|
identifiers.append(Match(
|
|
|
|
header_file,
|
|
|
|
line,
|
|
|
|
(identifier.start(), identifier.end()),
|
2021-08-06 17:22:06 +02:00
|
|
|
group))
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
return identifiers
|
|
|
|
|
|
|
|
def parse_symbols(self):
|
|
|
|
"""
|
|
|
|
Compile the Mbed TLS libraries, and parse the TLS, Crypto, and x509
|
|
|
|
object files using nm to retrieve the list of referenced symbols.
|
2021-08-06 17:22:06 +02:00
|
|
|
Exceptions thrown here are rethrown because they would be critical
|
|
|
|
errors that void several tests, and thus needs to halt the program. This
|
|
|
|
is explicitly done for clarity.
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
Returns a List of unique symbols defined and used in the libraries.
|
|
|
|
"""
|
|
|
|
self.log.info("Compiling...")
|
2021-08-05 20:47:48 +02:00
|
|
|
symbols = []
|
|
|
|
|
|
|
|
# Back up the config and atomically compile with the full configratuion.
|
|
|
|
shutil.copy("include/mbedtls/mbedtls_config.h",
|
2021-08-06 17:22:06 +02:00
|
|
|
"include/mbedtls/mbedtls_config.h.bak")
|
2018-05-08 16:30:59 +02:00
|
|
|
try:
|
2021-08-06 17:22:06 +02:00
|
|
|
# Use check=True in all subprocess calls so that failures are raised
|
|
|
|
# as exceptions and logged.
|
2021-08-05 20:47:48 +02:00
|
|
|
subprocess.run(
|
2021-08-06 17:22:06 +02:00
|
|
|
["python3", "scripts/config.py", "full"],
|
2021-08-05 20:47:48 +02:00
|
|
|
encoding=sys.stdout.encoding,
|
|
|
|
check=True
|
2018-05-08 16:30:59 +02:00
|
|
|
)
|
|
|
|
my_environment = os.environ.copy()
|
|
|
|
my_environment["CFLAGS"] = "-fno-asynchronous-unwind-tables"
|
2021-08-05 20:47:48 +02:00
|
|
|
subprocess.run(
|
2018-05-08 16:30:59 +02:00
|
|
|
["make", "clean", "lib"],
|
|
|
|
env=my_environment,
|
2021-08-05 20:47:48 +02:00
|
|
|
encoding=sys.stdout.encoding,
|
|
|
|
stdout=subprocess.PIPE,
|
2018-05-08 16:30:59 +02:00
|
|
|
stderr=subprocess.STDOUT,
|
2021-08-05 20:47:48 +02:00
|
|
|
check=True
|
2018-05-08 16:30:59 +02:00
|
|
|
)
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
# Perform object file analysis using nm
|
|
|
|
symbols = self.parse_symbols_from_nm(
|
|
|
|
["library/libmbedcrypto.a",
|
|
|
|
"library/libmbedtls.a",
|
|
|
|
"library/libmbedx509.a"])
|
|
|
|
|
|
|
|
symbols.sort()
|
|
|
|
|
|
|
|
subprocess.run(
|
2018-05-08 16:30:59 +02:00
|
|
|
["make", "clean"],
|
2021-08-05 20:47:48 +02:00
|
|
|
encoding=sys.stdout.encoding,
|
|
|
|
check=True
|
2018-05-08 16:30:59 +02:00
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError as error:
|
|
|
|
self.set_return_code(2)
|
2021-08-06 17:22:06 +02:00
|
|
|
raise error
|
2021-08-05 20:47:48 +02:00
|
|
|
finally:
|
|
|
|
shutil.move("include/mbedtls/mbedtls_config.h.bak",
|
|
|
|
"include/mbedtls/mbedtls_config.h")
|
|
|
|
|
|
|
|
return symbols
|
|
|
|
|
|
|
|
def parse_symbols_from_nm(self, object_files):
|
|
|
|
"""
|
|
|
|
Run nm to retrieve the list of referenced symbols in each object file.
|
|
|
|
Does not return the position data since it is of no use.
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
Args:
|
|
|
|
* object_files: a List of compiled object files to search through.
|
|
|
|
|
|
|
|
Returns a List of unique symbols defined and used in any of the object
|
|
|
|
files.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
UNDEFINED_SYMBOL = r"^\S+: +U |^$|^\S+:$"
|
|
|
|
VALID_SYMBOL = r"^\S+( [0-9A-Fa-f]+)* . _*(?P<symbol>\w+)"
|
2021-08-05 21:22:59 +02:00
|
|
|
EXCLUSIONS = ("FStar", "Hacl")
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
symbols = []
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
# Gather all outputs of nm
|
2021-08-05 20:47:48 +02:00
|
|
|
nm_output = ""
|
|
|
|
for lib in object_files:
|
|
|
|
nm_output += subprocess.run(
|
|
|
|
["nm", "-og", lib],
|
|
|
|
encoding=sys.stdout.encoding,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
check=True
|
|
|
|
).stdout
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
for line in nm_output.splitlines():
|
|
|
|
if not re.match(UNDEFINED_SYMBOL, line):
|
|
|
|
symbol = re.match(VALID_SYMBOL, line)
|
2021-08-05 21:22:59 +02:00
|
|
|
if symbol and not symbol.group("symbol").startswith(EXCLUSIONS):
|
|
|
|
symbols.append(symbol.group("symbol"))
|
2021-08-05 20:47:48 +02:00
|
|
|
else:
|
|
|
|
self.log.error(line)
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
return symbols
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
def perform_checks(self, show_problems: True):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
Perform each check in order, output its PASS/FAIL status. Maintain an
|
|
|
|
overall test status, and output that at the end.
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
* show_problems: whether to show the problematic examples.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-06 17:22:06 +02:00
|
|
|
self.log.info("=============")
|
2021-08-05 20:47:48 +02:00
|
|
|
problems = 0
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
problems += self.check_symbols_declared_in_header(show_problems)
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
pattern_checks = [
|
|
|
|
("macros", MACRO_PATTERN),
|
2021-08-06 17:22:06 +02:00
|
|
|
("enum_consts", CONSTANTS_PATTERN),
|
2021-08-05 20:47:48 +02:00
|
|
|
("identifiers", IDENTIFIER_PATTERN)]
|
|
|
|
for group, check_pattern in pattern_checks:
|
2021-08-06 17:22:06 +02:00
|
|
|
problems += self.check_match_pattern(
|
|
|
|
show_problems, group, check_pattern)
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
problems += self.check_for_typos(show_problems)
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
self.log.info("=============")
|
|
|
|
if problems > 0:
|
|
|
|
self.log.info("FAIL: {0} problem(s) to fix".format(str(problems)))
|
2021-08-06 17:22:06 +02:00
|
|
|
if not show_problems:
|
|
|
|
self.log.info("Remove --quiet to show the problems.")
|
2021-08-05 20:47:48 +02:00
|
|
|
else:
|
|
|
|
self.log.info("PASS")
|
2018-05-08 16:30:59 +02:00
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
def check_symbols_declared_in_header(self, show_problems):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
Perform a check that all detected symbols in the library object files
|
|
|
|
are properly declared in headers.
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
Args:
|
|
|
|
* show_problems: whether to show the problematic examples.
|
|
|
|
|
|
|
|
Returns the number of problems that need fixing.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
problems = []
|
|
|
|
for symbol in self.parse_result["symbols"]:
|
|
|
|
found_symbol_declared = False
|
|
|
|
for identifier_match in self.parse_result["identifiers"]:
|
|
|
|
if symbol == identifier_match.name:
|
|
|
|
found_symbol_declared = True
|
|
|
|
break
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
if not found_symbol_declared:
|
|
|
|
problems.append(SymbolNotInHeader(symbol))
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
self.output_check_result("All symbols in header", problems, show_problems)
|
2021-08-05 20:47:48 +02:00
|
|
|
return len(problems)
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
def check_match_pattern(self, show_problems, group_to_check, check_pattern):
|
|
|
|
"""
|
|
|
|
Perform a check that all items of a group conform to a regex pattern.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
* show_problems: whether to show the problematic examples.
|
|
|
|
* group_to_check: string key to index into self.parse_result.
|
|
|
|
* check_pattern: the regex to check against.
|
|
|
|
|
|
|
|
Returns the number of problems that need fixing.
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
problems = []
|
|
|
|
for item_match in self.parse_result[group_to_check]:
|
|
|
|
if not re.match(check_pattern, item_match.name):
|
|
|
|
problems.append(PatternMismatch(check_pattern, item_match))
|
2021-08-05 21:06:34 +02:00
|
|
|
if re.match(r".*__.*", item_match.name):
|
|
|
|
problems.append(PatternMismatch("double underscore", item_match))
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
self.output_check_result(
|
|
|
|
"Naming patterns of {}".format(group_to_check),
|
|
|
|
problems,
|
|
|
|
show_problems)
|
2021-08-05 20:47:48 +02:00
|
|
|
return len(problems)
|
2018-05-08 16:30:59 +02:00
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
def check_for_typos(self, show_problems):
|
|
|
|
"""
|
|
|
|
Perform a check that all words in the soure code beginning with MBED are
|
|
|
|
either defined as macros, or as enum constants.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
* show_problems: whether to show the problematic examples.
|
|
|
|
|
|
|
|
Returns the number of problems that need fixing.
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
problems = []
|
|
|
|
all_caps_names = list(set([
|
|
|
|
match.name for match
|
|
|
|
in self.parse_result["macros"] + self.parse_result["enum_consts"]]
|
2018-05-08 16:30:59 +02:00
|
|
|
))
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
TYPO_EXCLUSION = r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$"
|
|
|
|
|
|
|
|
for name_match in self.parse_result["mbed_names"]:
|
2021-08-06 17:22:06 +02:00
|
|
|
found = name_match.name in all_caps_names
|
|
|
|
|
|
|
|
# Since MBEDTLS_PSA_ACCEL_XXX defines are defined by the
|
|
|
|
# PSA driver, they will not exist as macros. However, they
|
|
|
|
# should still be checked for typos using the equivalent
|
|
|
|
# BUILTINs that exist.
|
|
|
|
if "MBEDTLS_PSA_ACCEL_" in name_match.name:
|
|
|
|
found = name_match.name.replace(
|
|
|
|
"MBEDTLS_PSA_ACCEL_",
|
|
|
|
"MBEDTLS_PSA_BUILTIN_") in all_caps_names
|
|
|
|
|
|
|
|
if not found and not re.search(TYPO_EXCLUSION, name_match.name):
|
2021-08-05 20:47:48 +02:00
|
|
|
problems.append(Typo(name_match))
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
self.output_check_result("Likely typos", problems, show_problems)
|
|
|
|
return len(problems)
|
|
|
|
|
|
|
|
def output_check_result(self, name, problems, show_problems):
|
|
|
|
"""
|
|
|
|
Write out the PASS/FAIL status of a performed check depending on whether
|
|
|
|
there were problems.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
* show_problems: whether to show the problematic examples.
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
if problems:
|
2018-05-08 16:30:59 +02:00
|
|
|
self.set_return_code(1)
|
2021-08-06 17:22:06 +02:00
|
|
|
self.log.info("{}: FAIL".format(name))
|
|
|
|
if show_problems:
|
|
|
|
self.log.info("")
|
|
|
|
for problem in problems:
|
|
|
|
self.log.warn(str(problem) + "\n")
|
2018-05-08 16:30:59 +02:00
|
|
|
else:
|
2021-08-06 17:22:06 +02:00
|
|
|
self.log.info("{}: PASS".format(name))
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
2021-08-06 17:22:06 +02:00
|
|
|
Perform argument parsing, and create an instance of NameCheck to begin the
|
|
|
|
core operation.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
|
|
description=(
|
|
|
|
"This script confirms that the naming of all symbols and identifiers "
|
|
|
|
"in Mbed TLS are consistent with the house style and are also "
|
|
|
|
"self-consistent.\n\n"
|
|
|
|
"Expected to be run from the MbedTLS root directory."))
|
|
|
|
|
|
|
|
parser.add_argument("-v", "--verbose",
|
|
|
|
action="store_true",
|
2021-08-06 17:22:06 +02:00
|
|
|
help="show parse results")
|
|
|
|
|
|
|
|
parser.add_argument("-q", "--quiet",
|
|
|
|
action="store_true",
|
|
|
|
help="hide unnecessary text and problematic examples")
|
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
args = parser.parse_args()
|
2018-05-08 16:30:59 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
name_check = NameCheck()
|
2021-08-05 20:47:48 +02:00
|
|
|
name_check.setup_logger(verbose=args.verbose)
|
|
|
|
name_check.parse_names_in_source()
|
2021-08-06 17:22:06 +02:00
|
|
|
name_check.perform_checks(show_problems=not args.quiet)
|
|
|
|
sys.exit(name_check.return_code)
|
|
|
|
except subprocess.CalledProcessError as error:
|
|
|
|
traceback.print_exc()
|
|
|
|
print("!! Compilation faced a critical error, "
|
|
|
|
"check-names can't continue further.")
|
2018-05-08 16:30:59 +02:00
|
|
|
sys.exit(name_check.return_code)
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-08-05 20:47:48 +02:00
|
|
|
main()
|