Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: code wrappers subpackage #25

Merged
merged 30 commits into from
Jul 23, 2024

Conversation

minosgalanakis
Copy link
Contributor

@minosgalanakis minosgalanakis commented Jun 7, 2024

This PR adds the placeholder directroy for the code-generation wrappers, refactors generate_psa_wrappers.py and add some minor utility functions in c_parsing_helper.py

In case it gets hidden by GitHub, the PR to incorporate this to the main repo is Mbed-TLS/mbedtls#9224

@minosgalanakis minosgalanakis added size-s Estimated task size: small (~2d) needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Jun 10, 2024
Copy link
Contributor

@valeriosetti valeriosetti left a comment

Choose a reason for hiding this comment

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

I only left 1 comment for a (potential) extra new line. A part from that the rest of the PR looks good to me.

@minosgalanakis minosgalanakis force-pushed the feature/code_wrappers branch from 656ffb8 to 23faa02 Compare June 12, 2024 09:41
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

Unfinished review because psa_buffer.py needs to be in mbedtls_framework.

@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Jun 12, 2024
@minosgalanakis minosgalanakis force-pushed the feature/code_wrappers branch from 8c6bde2 to 8130fc0 Compare June 13, 2024 10:48
@minosgalanakis minosgalanakis added needs-review Every commit must be reviewed by at least two team members, needs-ci Needs to pass CI tests and removed needs-work needs-ci Needs to pass CI tests labels Jun 13, 2024
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

I've reviewed the changes in scripts/mbedtls_framework/c_parsing_helper.py.

For the rest, there's a big bunch of code that disappears in a8d6b10 and reappears with modifications in 4d3153b. This makes it effectively necessary to review the whole code from scratch, which is a waste of time.

Please restructure the commits so that code that is moved appears as a move with git diff --color-moved SHA, and changes to the content are done in separate commits. For example, in Mbed-TLS/mbedtls#8930, “docs: Move TLS 1.3 early data doc to a dedicated file” is a separate commit b372b2e with no content changes.


_C_TYPEDEF_DECLARATION_RE = r'typedef (?:struct ){0,1}(?P<type>\w+) (?P<name>\w+)'

def read_typedefs(filename: str)-> Dict[str, str]:
Copy link
Contributor

Choose a reason for hiding this comment

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

This only captures some typedefs. Why those? What will happen if we edit a header and the typedef no longer follows that format, and how much of a risk is there that this happens?

I don't know what this function is meant to be used for, and currently it isn't used in #25 or Mbed-TLS/mbedtls#9224.

Copy link
Contributor Author

@minosgalanakis minosgalanakis Jun 17, 2024

Choose a reason for hiding this comment

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

Reading simple typedefs is something that could be usefull for a FunctionInfo parsing class.

It is currently not being used, but will be used when parsing psa headers for generating serialisation logic. It can also be very usefull for debugging purposes since it generates a simple to read dictionary e.g

{'mbedtls_svc_key_id_t': 'psa_key_id_t',
 'psa_algorithm_t': 'uint32_t',
 'psa_dh_family_t': 'uint8_t',
 'psa_ecc_family_t': 'uint8_t',
 'psa_key_attributes_t': 'psa_key_attributes_s',
 'psa_key_derivation_step_t': 'uint16_t',
 'psa_key_id_t': 'uint32_t',
 'psa_key_lifetime_t': 'uint32_t',
 'psa_key_location_t': 'uint32_t',
 'psa_key_persistence_t': 'uint8_t',
 'psa_key_production_parameters_t': 'psa_key_production_parameters_s',
 'psa_key_slot_number_t': 'uint64_t',
 'psa_key_type_t': 'uint16_t',
 'psa_key_usage_t': 'uint32_t',
 'psa_status_t': 'int32_t'}

There is very little risk, since we don't change psa headers that much, and evem when we do, the data structures are very rarely updated. Also this will be used in test code generation so whoever uses it needs to make sure it is capturing everything the requirements need it to, and update it accordingly.

For now the simple parsing should work.

@@ -129,3 +153,17 @@ def read_function_declarations(functions: Dict[str, FunctionInfo],
return_type,
name,
arguments)

_C_TYPEDEF_DECLARATION_RE = r'typedef (?:struct ){0,1}(?P<type>\w+) (?P<name>\w+)'
Copy link
Contributor

Choose a reason for hiding this comment

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

Why isn't the struct captured in type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because we do not have to capture it to know it is a structure.

@gilles-peskine-arm gilles-peskine-arm added needs-work priority-high High priority - will be reviewed soon and removed needs-review Every commit must be reviewed by at least two team members, labels Jun 14, 2024
@minosgalanakis minosgalanakis force-pushed the feature/code_wrappers branch from 8130fc0 to dfe7adc Compare June 17, 2024 13:46
@minosgalanakis minosgalanakis added needs-review Every commit must be reviewed by at least two team members, and removed needs-work labels Jun 18, 2024
@minosgalanakis minosgalanakis force-pushed the feature/code_wrappers branch from dfe7adc to a526390 Compare June 18, 2024 10:36
in_headers for PSAWrapper, PSALoggingWrapper,
PSATestWrapper and PSALoggingTestWrapper is
now set as Optional parameter with None as default.

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
@minosgalanakis minosgalanakis added needs-review Every commit must be reviewed by at least two team members, and removed needs-work labels Jul 17, 2024
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

We're getting close!

class PSAWrapperConfiguration:
"""Configuration data class for PSA Wrapper."""

cpp_guards = ["MBEDTLS_PSA_CRYPTO_C", "MBEDTLS_TEST_HOOKS", "!RECORD_PSA_STATUS_COVERAGE_LOG"]
Copy link
Contributor

Choose a reason for hiding this comment

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

(Resuming #25 (comment))

These all should be instance variables, not class variables. Otherwise we will be very confused when we instantiate or override the class.

Why isn't the class a NamedTuple?

Can you elaborate on how that should be addressed?

Either use a NamedTuple, or make it a data class, as in, a class whose sole purpose is to store data into fields — what C would call a struct. Pylint doesn't like data classes, but it doesn't have a very strong justification. It wants to drive you to the mechanisms for building data classes through the standard library, but those mechanisms aren't particularly useful unless you want to do some introspection, which we don't care about here.

A custom data class would be just a constructor that initializes the fields to their default value.

class PSAWrapperConfiguration:
    """Configuration data class for PSA Wrapper."""
    def __init__(self) -> None:
        self.cpp_guards = ["MBEDTLS_PSA_CRYPTO_C", "MBEDTLS_TEST_HOOKS", "!RECORD_PSA_STATUS_COVERAGE_LOG"]
        …

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am getting a bit of mixed signals, as to why we are going around in circles on this one. The aim was to use a generic key:value configuration datastore, which allows other modules to import and .update() as required.

Then as requested this was changed into a class because a using a dictionary is stringy typing? Now I am being asked to use a NamedTuple which completely fails the original intent, and is also stringy typing?

Tuples are immutable, which means in order to update an entry the consumer will have to recreate it. There is a reason that dictionaries are universaly used for configuration input.

Now for dataclass argument, yes I understand that this is not a dataclass as introduced in Python3.7, but the proposal is to basically implement in Python3.6 what the dataclass decorator does.

Is that all that for the shake of adding-complexity really needed?

Copy link
Contributor

Choose a reason for hiding this comment

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

The aim was to use a generic key:value configuration datastore, which allows other modules to import and .update() as required.

Uh? That's the first time this comes up. Why should this be a generic configuration datastore? Why would modules add their own configuration entries that the base class doesn't know about? That seems like a solution in need of a problem.

a NamedTuple which completely fails the original intent, and is also stringy typing?

A NamedTuple isn't extensible. But extensibility is a new requirement and I don't see the point of this new requirement.

A NamedTuple is not stringly typed. Its type is the NamedTuple. The set of keys is known, and the type of the key corresponding to each value is known.

Note once again that I'm not saying you have to use a NamedTuple. I'd be happy if you just made the change I posted above, changing the class fields into instance fields. With # pylint: disable=too-few-public-methods because Pylint is opinionated about not doing your own data class, but there isn't a strong objective justification behind that opinion.

@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, labels Jul 18, 2024
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
@minosgalanakis minosgalanakis added needs-review Every commit must be reviewed by at least two team members, and removed needs-work labels Jul 19, 2024
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

Almost there

@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, labels Jul 22, 2024
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

LGTM except one edge case

Co-authored-by: Gilles Peskine <gilles.peskine@arm.com>
Signed-off-by: minosgalanakis <30719586+minosgalanakis@users.noreply.github.com>
@minosgalanakis minosgalanakis added needs-review Every commit must be reviewed by at least two team members, and removed needs-work labels Jul 22, 2024
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-review Every commit must be reviewed by at least two team members, priority-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

5 participants