From 20e4950860053e3c35f2c47d808a3f9e3059630d Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Thu, 21 Dec 2023 19:13:17 +0100 Subject: [PATCH 1/2] Adapt tests/scripts/check_names.py to work for psa-crypto Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/build_tree.py | 4 +- tests/scripts/check_names.py | 209 ++++++++++++++++++++++-------- 2 files changed, 160 insertions(+), 53 deletions(-) diff --git a/scripts/mbedtls_dev/build_tree.py b/scripts/mbedtls_dev/build_tree.py index ec67e4cdfab3..919d2bb47599 100644 --- a/scripts/mbedtls_dev/build_tree.py +++ b/scripts/mbedtls_dev/build_tree.py @@ -58,8 +58,8 @@ def check_repo_path(): """Check that the current working directory is the project root, and throw an exception if not. """ - if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): - raise Exception("This script must be run from Mbed TLS root") + if not looks_like_root("."): + raise Exception("This script must be run from project root") def chdir_to_root() -> None: """Detect the root of the Mbed TLS source tree and change to it. diff --git a/tests/scripts/check_names.py b/tests/scripts/check_names.py index 9e8ed219a4c2..4a0e0729ec1b 100755 --- a/tests/scripts/check_names.py +++ b/tests/scripts/check_names.py @@ -55,6 +55,127 @@ CONSTANTS_PATTERN = PUBLIC_MACRO_PATTERN IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$" +class MbedTLS(): + config_files = [ + "include/mbedtls/mbedtls_config.h" + ] + + object_files = [ + "library/libmbedcrypto.a", + "library/libmbedtls.a", + "library/libmbedx509.a" + ] + + use_cmake = False + + public_macro_files = [ + "include/mbedtls/*.h", + "include/psa/*.h", + "3rdparty/everest/include/everest/everest.h", + "3rdparty/everest/include/everest/x25519.h" + ] + + internal_macro_files = [ + "library/*.h", + "tests/include/test/drivers/*.h" + ] + + private_macro_files = [ + "library/*.c" + ] + + enum_const_files = [ + "include/mbedtls/*.h", + "include/psa/*.h", + "library/*.h", + "library/*.c", + "3rdparty/everest/include/everest/everest.h", + "3rdparty/everest/include/everest/x25519.h" + ] + + identifier_files = [ + "include/mbedtls/*.h", + "include/psa/*.h", + "library/*.h", + "3rdparty/everest/include/everest/everest.h", + "3rdparty/everest/include/everest/x25519.h" + ] + + excluded_identifier_files = [ + "3rdparty/p256-m/p256-m/p256-m.h" + ] + + mbed_psa_word_files = [ + "include/mbedtls/*.h", + "include/psa/*.h", + "library/*.h", + "3rdparty/everest/include/everest/everest.h", + "3rdparty/everest/include/everest/x25519.h", + "library/*.c", + "3rdparty/everest/library/everest.c", + "3rdparty/everest/library/x25519.c" + ] + + excluded_mbed_psa_word_files = [ + "library/psa_crypto_driver_wrappers.h" + ] + +class TF_PSA_Crypto(): + config_files = [ + "drivers/builtin/mbedtls_config.h" + ] + + object_files = [ + "core/libtfpsacrypto.a" + ] + + use_cmake = True + + public_macro_files = [ + "include/TF_PSA_Crypto/*.h", + "include/psa/*.h", + "drivers/builtin/include/mbedtls/*.h" + ] + + internal_macro_files = [ + "core/*.h", + "drivers/builtin/src/*.h", + "tests/include/test/drivers/*.h" + ] + + private_macro_files = [ + "core/*.c", + "drivers/builtin/src/*.c" + ] + + enum_const_files = [ + "drivers/builtin/include/mbedtls/*.h", + "include/psa/*.h", + "core/*.h", + "drivers/builtin/src/*.c", + "core/*.c" + ] + + identifier_files = [ + "drivers/builtin/include/mbedtls/*.h", + "include/psa/*.h", + "core/*.h" + ] + + excluded_identifier_files = [ + ] + + mbed_psa_word_files = [ + "drivers/builtin/include/mbedtls/*.h", + "include/psa/*.h", + "core/*.h", + "drivers/builtin/src/*.c", + "core/*.c" + ] + + excluded_mbed_psa_word_files = [ + ] + class Match(): # pylint: disable=too-few-public-methods """ A class representing a match, together with its found position. @@ -214,6 +335,11 @@ def __init__(self, log): self.log = log build_tree.check_repo_path() + if build_tree.looks_like_mbedtls_root("."): + self.project = MbedTLS() + elif build_tree.looks_like_tf_psa_crypto_root("."): + self.project = TF_PSA_Crypto() + # Memo for storing "glob expression": set(filepaths) self.files = {} @@ -235,44 +361,16 @@ def comprehensive_parse(self): ) all_macros = {"public": [], "internal": [], "private":[]} - all_macros["public"] = self.parse_macros([ - "include/mbedtls/*.h", - "include/psa/*.h", - "3rdparty/everest/include/everest/everest.h", - "3rdparty/everest/include/everest/x25519.h" - ]) - all_macros["internal"] = self.parse_macros([ - "library/*.h", - "tests/include/test/drivers/*.h", - ]) - all_macros["private"] = self.parse_macros([ - "library/*.c", - ]) - enum_consts = self.parse_enum_consts([ - "include/mbedtls/*.h", - "include/psa/*.h", - "library/*.h", - "library/*.c", - "3rdparty/everest/include/everest/everest.h", - "3rdparty/everest/include/everest/x25519.h" - ]) - identifiers, excluded_identifiers = self.parse_identifiers([ - "include/mbedtls/*.h", - "include/psa/*.h", - "library/*.h", - "3rdparty/everest/include/everest/everest.h", - "3rdparty/everest/include/everest/x25519.h" - ], ["3rdparty/p256-m/p256-m/p256-m.h"]) - mbed_psa_words = self.parse_mbed_psa_words([ - "include/mbedtls/*.h", - "include/psa/*.h", - "library/*.h", - "3rdparty/everest/include/everest/everest.h", - "3rdparty/everest/include/everest/x25519.h", - "library/*.c", - "3rdparty/everest/library/everest.c", - "3rdparty/everest/library/x25519.c" - ], ["library/psa_crypto_driver_wrappers.h"]) + all_macros["public"] = self.parse_macros(self.project.public_macro_files) + all_macros["internal"] = self.parse_macros(self.project.internal_macro_files) + all_macros["private"] = self.parse_macros(self.project.private_macro_files) + enum_consts = self.parse_enum_consts(self.project.enum_const_files) + identifiers, excluded_identifiers = self.parse_identifiers( + self.project.identifier_files, + self.project.excluded_identifier_files) + mbed_psa_words = self.parse_mbed_psa_words( + self.project.mbed_psa_word_files, + self.project.excluded_mbed_psa_word_files) symbols = self.parse_symbols() # Remove identifier macros like mbedtls_printf or mbedtls_calloc @@ -673,10 +771,8 @@ def parse_symbols(self): symbols = [] # Back up the config and atomically compile with the full configuration. - shutil.copy( - "include/mbedtls/mbedtls_config.h", - "include/mbedtls/mbedtls_config.h.bak" - ) + for config_file in self.project.config_files: + shutil.copy(config_file, config_file + ".bak") try: # Use check=True in all subprocess calls so that failures are raised # as exceptions and logged. @@ -687,6 +783,23 @@ def parse_symbols(self): ) my_environment = os.environ.copy() my_environment["CFLAGS"] = "-fno-asynchronous-unwind-tables" + + if self.project.use_cmake: + # Delete the cmake cache to apply CFLAGS changes + cache_file = "CMakeCache.txt" + if os.path.exists(cache_file): + os.remove(cache_file) + + # Run cmake configuration + subprocess.run( + ["cmake", "."], + env=my_environment, + universal_newlines=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + check=True + ) + # Run make clean separately to lib to prevent unwanted behavior when # make is invoked with parallelism. subprocess.run( @@ -704,11 +817,7 @@ def parse_symbols(self): ) # Perform object file analysis using nm - symbols = self.parse_symbols_from_nm([ - "library/libmbedcrypto.a", - "library/libmbedtls.a", - "library/libmbedx509.a" - ]) + symbols = self.parse_symbols_from_nm(self.project.object_files) subprocess.run( ["make", "clean"], @@ -721,10 +830,8 @@ def parse_symbols(self): finally: # Put back the original config regardless of there being errors. # Works also for keyboard interrupts. - shutil.move( - "include/mbedtls/mbedtls_config.h.bak", - "include/mbedtls/mbedtls_config.h" - ) + for config_file in self.project.config_files: + shutil.move(config_file + ".bak", config_file) return symbols From b9dac431ac9d97cc5f5406185b4a06f8ea845013 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Thu, 21 Dec 2023 20:06:31 +0100 Subject: [PATCH 2/2] Fix pylint issues Signed-off-by: Gabor Mezei --- tests/scripts/check_names.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/scripts/check_names.py b/tests/scripts/check_names.py index 4a0e0729ec1b..587efff78049 100755 --- a/tests/scripts/check_names.py +++ b/tests/scripts/check_names.py @@ -44,6 +44,8 @@ import subprocess import logging +from typing import List + import scripts_path # pylint: disable=unused-import from mbedtls_dev import build_tree @@ -55,7 +57,10 @@ CONSTANTS_PATTERN = PUBLIC_MACRO_PATTERN IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$" -class MbedTLS(): +class MbedTLS(): # pylint: disable=too-few-public-methods + """ + A class for Mbed TLS specific parsing options. + """ config_files = [ "include/mbedtls/mbedtls_config.h" ] @@ -120,7 +125,10 @@ class MbedTLS(): "library/psa_crypto_driver_wrappers.h" ] -class TF_PSA_Crypto(): +class TFPSACrypto(): # pylint: disable=too-few-public-methods + """ + A class for TF PSA Crypto specific parsing options. + """ config_files = [ "drivers/builtin/mbedtls_config.h" ] @@ -163,7 +171,7 @@ class TF_PSA_Crypto(): ] excluded_identifier_files = [ - ] + ] # type: List[str] mbed_psa_word_files = [ "drivers/builtin/include/mbedtls/*.h", @@ -174,7 +182,7 @@ class TF_PSA_Crypto(): ] excluded_mbed_psa_word_files = [ - ] + ] # type: List[str] class Match(): # pylint: disable=too-few-public-methods """ @@ -338,7 +346,7 @@ def __init__(self, log): if build_tree.looks_like_mbedtls_root("."): self.project = MbedTLS() elif build_tree.looks_like_tf_psa_crypto_root("."): - self.project = TF_PSA_Crypto() + self.project = TFPSACrypto() # Memo for storing "glob expression": set(filepaths) self.files = {}