Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions scripts/make_generated_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
import argparse
import filecmp
import os
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -66,6 +67,18 @@ def get_generation_script_files(generation_script: str):

return files

COMMON_GENERATION_SCRIPTS = [
]

# Once the script has been added to both Mbed TLS and TF-PSA-Crypto,
# we can include this unconditionally.
# https://github.com/Mbed-TLS/mbedtls/issues/10305
if os.path.exists("scripts/generate_config_checks.py"):
COMMON_GENERATION_SCRIPTS.append(GenerationScript(
Path("scripts/generate_config_checks.py"),
get_generation_script_files("scripts/generate_config_checks.py"),
"", None))

if build_tree.looks_like_tf_psa_crypto_root("."):
TF_PSA_CRYPTO_GENERATION_SCRIPTS = [
GenerationScript(
Expand Down Expand Up @@ -244,6 +257,7 @@ def main():
generation_scripts = MBEDTLS_GENERATION_SCRIPTS
else:
raise Exception("No support for Mbed TLS 3.6")
generation_scripts += COMMON_GENERATION_SCRIPTS

if args.list:
files = get_generated_files(generation_scripts)
Expand Down
228 changes: 228 additions & 0 deletions scripts/mbedtls_framework/config_checks_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
"""Generate C preprocessor code to check for bad configurations.

The headers are meant to be included in a specific way in PROJECT_config.c.
See framework/docs/architecture/config-check-framework.md for information.
"""

## Copyright The Mbed TLS Contributors
## SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

import argparse
import enum
import os
import re
import sys
import textwrap
import typing
from typing import Iterator, List

from . import build_tree
from . import typing_util


class Position(enum.Enum):
BEFORE = 0 # Before build_info.h
USER = 1 # Just after reading PROJECT_CONFIG_FILE (*config.h) and PROJECT_USER_CONFIG_FILE
FINAL = 2 # After *adjust*.h and the rest of build_info.h


class Checker:
"""Description of checks for one option.

By default, this class triggers an error if the option is set after
reading the user configuration. To change the behavior, override
the methods `before`, `user` and `final` as needed.
"""

def __init__(self, name: str, suggestion: str = '') -> None:
"""Construct a checker for the given preprocessor macro name.

If suggestion is given, it is appended to the error message.
It should be a short sentence intended for human readers.
This sentence follows a sentence like "<macro_name> is not
a valid configuration option".
"""
self.name = name
self.suggestion = suggestion

def _basic_message(self) -> str:
"""The first sentence of the message to display on error.

It should end with a full stop or other sentence-ending punctuation.
"""
return f'{self.name} is not a valid configuration option.'

def message(self) -> str:
"""The message to display on error."""
message = self._basic_message()
if self.suggestion:
message += ' Suggestion: ' + self.suggestion
return message

def _quoted_message(self) -> str:
"""Quote message() in double quotes. Useful for #error directives."""
return ('"' +
str.replace(str.replace(self.message(),
'\\', '\\\\'),
'"', '\\"') +
'"')

def before(self, _prefix: str) -> str:
"""C code to inject before including the config."""
#pylint: disable=no-self-use
# Derived classes will add content where needed.
return ''

def user(self, _prefix: str) -> str:
"""C code to inject immediately after including the user config."""
return f'''
#if defined({self.name})
# error {self._quoted_message()}
#endif
'''

def final(self, _prefix: str) -> str:
"""C code to inject after finalizing the config."""
#pylint: disable=no-self-use
# Derived classes will add content where needed.
return ''

def code(self, position: Position, prefix: str) -> str:
"""C code to inject at the given position.

Use the given prefix for auxiliary macro names.
"""
methods = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if declaring a dictionary just to use it as an enum map is neccessary. Since we are matching the names we could get away with something like

method = getattr(self, position.name.lower())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's shorter but I don't think it's clearer. And it's not type-safe. So I don't see it as better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I would rather we refrain from using enums in python until we upgrade to 3.10 and have match, unless:

  1. It is clearly an object that is meant to allign with an enum already in C
  2. We save time and complexity
  3. We need to refer to them using their name, or number, or iterate on their items in sequence.

Otherwise we are just adding complexity without much benefit. My proposal was geared towards implemeting 2.

Either way I won't block this Pr for the choice of tooling.

Position.BEFORE: self.before,
Position.USER: self.user,
Position.FINAL: self.final,
}
method = methods[position]
snippet = method(prefix)
return textwrap.dedent(snippet)


class Internal(Checker):
"""Checker for an internal-only option."""


class Removed(Checker):
"""Checker for an option that has been removed."""

def __init__(self, name: str, version: str, suggestion: str = '') -> None:
super().__init__(name, suggestion)
self.version = version
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would include the version in the Checker class, as an optional parameter and forgo the inheritance completely and put the subclass as an attribute which defines how the affected method change.

e.g

class Checker:
    """Description of checks for one option.
    By default, this class triggers an error if the option is set after

    def __init__(self, name: str, suggestion: str = '') -> None:
        self.name = name
        self.suggestion = suggestion
        self.version = version
        self.checker_type = self.determine_type(name, suggestion, version)

Of-course this seems to be the first of many so you have better scope of what this should do in its final form. I just suggest that if we could reduce complexity it would be worth it.

For clarity what I propose when invoked would look like:

from mbedtls_framework.config_checks_generator import * \
    #pylint: disable=wildcard-import,unused-wildcard-import

MBEDTLS_CHECKS = BranchData(
    header_directory='library',
    header_prefix='mbedtls_',
    project_cpp_prefix='MBEDTLS',
    checkers=[
        checker('MBEDTLS_KEY_EXCHANGE_RSA_ENABLED', 'Mbed TLS 4.0', check_for="Removed")
    ],
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of-course this seems to be the first of many so you have better scope of what this should do in its final form. I just suggest that if we could reduce complexity it would be worth it.

I'm not sure but I suspect that it only looks like it could be simpler because the classes here aren't the full story. Can you please take a look at #164 with the added classes (not ready for review yet, but I'm not planning any more changes to that part before 1.0), and see if your comment is still applicable there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. Using inheritance may be the simplest and easier to maintain approach.


def _basic_message(self) -> str:
"""The first sentence of the message to display on error.

It should end with a full stop or other sentence-ending punctuation.
"""
return f'{self.name} was removed in {self.version}.'

def user(self, prefix: str) -> str:
"""C code to inject immediately after including the user config."""
# A removed option is forbidden, just like an internal option.
# But since we're checking a macro that is not defined anywhere,
# we need to tell check_names.py that this is a false positive.
code = super().user(prefix)
return re.sub(rf'^ *# *\w+.*\b{self.name}\b.*$',
lambda m: m.group(0) + ' //no-check-names',
code, flags=re.M)


class BranchData(typing.NamedTuple):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an immutable object, that is used as the source of thruth for the whole logic. I would consider using a dataclass

@dataclass(frozen=True)
class BranchData:

p.s in that case we can also use the post_init to do input sanitatisation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the benefits of a dataclass over a NamedTuple?

I only foresee a single place in each repository where we'll create an instance of that class, so I don't think we need to do any input sanitization.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much in that case, and depends on how we wish to use that.

If that a small immutable, input object, named.tuple is the best choice. If in the future there are plans to do things like conditional immutability, or input checking, then dataclass allows you to attach those extra functionalities.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a small immutable, input object

Right, here this is exactly what it is.

"""The relevant aspects of the configuration on a branch."""

# Subdirectory where the generated headers will be located.
header_directory: str

# Prefix used for the generated headers' basename.
header_prefix: str

# Prefix used for C preprocessor macros.
project_cpp_prefix: str

# Options to check
checkers: List[Checker]


class HeaderGenerator:
"""Generate a header to include before or after the user config."""

def __init__(self, branch_data: BranchData, position: Position) -> None:
self.branch_data = branch_data
self.position = position
self.prefix = branch_data.project_cpp_prefix + '_CONFIG_CHECK'
self.bypass_checks = self.prefix + '_BYPASS'

def write_stanza(self, out: typing_util.Writable, checker: Checker) -> None:
"""Write the part of the output corresponding to one config option."""
code = checker.code(self.position, self.prefix)
out.write(code)

def write_content(self, out: typing_util.Writable) -> None:
"""Write the output for all config options to be processed."""
for checker in self.branch_data.checkers:
self.write_stanza(out, checker)

def output_file_name(self) -> str:
"""The base name of the output file."""
return ''.join([self.branch_data.header_prefix,
'config_check_',
self.position.name.lower(),
'.h'])

def write(self, directory: str) -> None:
"""Write the whole output file."""
basename = self.output_file_name()
with open(os.path.join(directory, basename), 'w') as out:
out.write(f'''\
/* {basename} (generated part of {self.branch_data.header_prefix}config.c). */
/* Automatically generated by {os.path.basename(sys.argv[0])}. Do not edit! */

#if !defined({self.bypass_checks}) //no-check-names

/* *INDENT-OFF* */
''')
self.write_content(out)
out.write(f'''
/* *INDENT-ON* */

#endif /* !defined({self.bypass_checks}) */ //no-check-names

/* End of automatically generated {basename} */
''')


def generate_header_files(branch_data: BranchData,
directory: str,
list_only: bool = False) -> Iterator[str]:
"""Generate the header files to include before and after *config.h."""
for position in Position:
generator = HeaderGenerator(branch_data, position)
yield os.path.join(directory, generator.output_file_name())
if not list_only:
generator.write(directory)


def main(branch_data: BranchData) -> None:
root = build_tree.guess_project_root()
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--list', action='store_true',
help='List generated files and exit')
parser.add_argument('--list-for-cmake', action='store_true',
help='List generated files in CMake-friendly format and exit')
parser.add_argument('output_directory', metavar='DIR', nargs='?',
default=os.path.join(root, branch_data.header_directory),
help='output file location (default: %(default)s)')
options = parser.parse_args()
list_only = options.list or options.list_for_cmake
output_files = generate_header_files(branch_data,
options.output_directory,
list_only=list_only)
if options.list_for_cmake:
sys.stdout.write(';'.join(output_files))
elif options.list:
for filename in output_files:
print(filename)