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

Add client-server build to all.sh #9121

Merged
merged 13 commits into from May 20, 2024

Conversation

valeriosetti
Copy link
Contributor

Description

This PR extends the psasim testing as follows:

  • builds MbedTLS libraries (crypto, x509 and tls) twice, one for the client and one for the server with proper configurations. Most relevant difference is:
    • client builds it with CRYPTO_CLIENT && !CRYPTO_C and links all 3 libraries crypto, x509 and tls;
    • server builds it with CRYPTO_Cand only links against crypto library.
  • implements the first IPC communication between client and server for a PSA operation: psa_crypto_init(). This will be extended in the future

Resolves #8963

PR checklist

  • changelog not required: only internal testing
  • 3.6 backport not required: psasim is a new component
  • 2.28 backport not required: psasim is a new component
  • tests provided

…and server

It includes changes to:
- tests/Makefile: build the library for client and server in different
  folders. It mimica the libtestdriver1 behavior (without functions
  renaming though).
- tests/scripts/all.sh: helper function to build for client and
  server with some default configuration for each of them.
- crypto_spe.h: this is dummy file taken from the already existing
  tests. It's just meant to pacify the compiler, not to provide
  something useful. It will likely be changed in the future.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
…er MbedTLS libraries

Ensure that both server and client can call mbedtls_version_get_string_full()
to verify that they are linked against proper libraries.

Note: each side (client/server) performs the call against its own
MbedTLS library. There is no IPC communication involved in this
test. Client/server communication will come later.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
This commit implements the first useful IPC communication between
the client and the server. The implemented command is simple,
psa_crypto_init(), and its return value is sent back to the client.

Note: the newly added file psa_functions_codes.h is temporary
and it's probably the one that needs to be automatically
generated by a python script to support all crypto functions.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
This is necessary because otherwise the library is not able to
find the seedfile at runtime and it fails the initialization.
However since this test runs on a standard PC we can rely on
platform entropy as source of entropy.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
@valeriosetti valeriosetti added needs-review Every commit must be reviewed by at least two team members, needs-ci Needs to pass CI tests size-s Estimated task size: small (~2d) priority-high High priority - will be reviewed soon labels May 10, 2024
@valeriosetti valeriosetti self-assigned this May 10, 2024
@valeriosetti valeriosetti added this to To Do in Roadmap Board for Mbed TLS via automation May 10, 2024
The goal is to keep psasim as simple as possible:

- do not build a separate lib for psa-ff; build those source
  files as part of server or client
- do not have lot of different makefiles: just 1 that does all
  we need
- do not have several subfolders for headers: only 1 is enough
  for this kind of project

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
tests/Makefile Outdated

libpsaclient libpsaserver:
# Clone the library and include folder for client and server builds.
rm -Rf ./$@
Copy link
Contributor

Choose a reason for hiding this comment

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

(minor) consistency: why not use -rf like at like 176? (Yes, I'm aware we sometimes seem to use -r and sometimes -R for no very good reason)


ifeq ($(DEBUG),1)
CFLAGS += -DDEBUG -O0 -g
CFLAGS += -DDEBUG
Copy link
Contributor

Choose a reason for hiding this comment

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

Why remove -O0 and -g - if we are going to debug, they are very useful options to have built the executables with!

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 know I know, I've used them thousand times in the past, but those are passed in by all.sh. I usually go to all.sh and modify ASAN_FLAGS replacing -O2 with -O0 -g.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, but this is specifically for a DEBUG build (the test on line 3). Why require a developer to modify a file when these can just be kept in?

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 was afraid that adding extra -O0 -g on a line which already contained -O2 (from ASAN_FLAGS) could not work properly, but googling a bit turned out that it actually does what we expect. I fixed this in c98f8ab

Instead of copying the entire library & include folders twice
to build libraries for client and server:

- change the main config file (mbedtls_config.h)
- build in the root library folder
- move the generated library in the psasim folder
- use those library for linking the client/server binaries

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Add DEBUG=1 in test_psasim() to helpers and final make to build
the libraries and the final binaries with debug symbols
enabled.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
@valeriosetti valeriosetti removed the needs-ci Needs to pass CI tests label May 10, 2024
@valeriosetti valeriosetti moved this from To Do to In Review in Roadmap Board for Mbed TLS May 10, 2024
Copy link
Contributor

@minosgalanakis minosgalanakis left a comment

Choose a reason for hiding this comment

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

Looks good overall, some minor tibits.

TARGET=$1
TARGET_LIB=libpsa$TARGET

cp $CONFIG_H $CONFIG_H.bak
Copy link
Contributor

Choose a reason for hiding this comment

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

It this not done by the pre_back_up ref 1 and 2?

What happens if make command fails, or if the config file is the driver config?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It this not done by the pre_back_up ref 1 and 2?

Yes, but pre_back_up dumps files which are restore at the end of run_component. In our case, however, we are modifying config files twice in the same component. So unless I missed something here, we need this extra logic. Wdyt?

What happens if make command fails

Right, we might end up with a modified config file which can erroneously be picked up as the valid one on the next run (unless the user realizes that and clears the status with git restore ...). I will fix this by creating the "new/custom" config files as copy of the original ones so that if something fails in during the build, then those files will be overwritten by a new all.sh run.

if the config file is the driver config

Mmmm right, I didn't thought about this but I need to make a copy also of this file! Thanks for the notification

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update: while implementing what I wrote above I realized that this was making helper_crypto_client_build more complex for probably no reason. So I ended up not copying config files, just relying on the backed-up ones, and then calling cleanup in the end. The final shape is f57afd5. Wdyt?

INCLUDE := -I../include/ -I./psa_manifest
LIB := -L../src -lpsaff
LIBPSASIM_PATH := ..
LIBPSACLIENT_PATH := ../../../libpsaclient
Copy link
Contributor

Choose a reason for hiding this comment

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

could we put the root path e.g ../../.. into a variable so when the library moves it will be simpler?

invec = 0;
outvec = 0;

// read response from server
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a blocker but could we use consistent comments? .e.g /* comment */


static int get_queue_info(char *path, int *cqid, int *sqid)
{

Copy link
Contributor

Choose a reason for hiding this comment

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

(minor) unnecessary blank line at the beginning of this function and the next

static psa_status_t send(int rx_qid, int server_qid, int *internal_server_qid,
int32_t type, uint32_t minor_version, vectors_t *vecs)
{
{
Copy link
Contributor

Choose a reason for hiding this comment

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

(minor) unnecessary extra level of blocking


psa_handle_t psa_connect(uint32_t sid, uint32_t minor_version)
{

Copy link
Contributor

Choose a reason for hiding this comment

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

(minor) blank line

psa_outvec *out_vec,
size_t out_len)
{

Copy link
Contributor

Choose a reason for hiding this comment

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

(minor) blank line

- add quotes to the $@ parameter in helper_crypto_client_build()
- instead of copying mbedtls_config.h to build static libraries,
  we rely on the already existing backup/cleanup mechanism which
  is available in all.sh.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
@valeriosetti
Copy link
Contributor Author

[Personal opinion]
All the recent about blank lines, comments format, etc, emphasize something that I already mentioned in the meetings: we're going to use the psasim code (almost) as-is because it's the fastest solution, but I think that this part might deserve some re-engineering in the future when we'll have some time. For now we can stick with it as long as it does what we need.

This allows to re-enable MBEDTLS_ENTROPY_NV_SEED since the
seedfile is correctly found in the "test" folder at runtime.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
minosgalanakis
minosgalanakis previously approved these changes May 14, 2024
Copy link
Contributor

@minosgalanakis minosgalanakis left a comment

Choose a reason for hiding this comment

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

LGTM

@valeriosetti valeriosetti dismissed minosgalanakis’s stale review May 14, 2024 10:06

The merge-base changed after approval.

Roadmap Board for Mbed TLS automation moved this from In Review to In Development May 14, 2024
@@ -15,6 +15,9 @@

FILENAME = str(sys.argv[1])

SCRIPT_PATH = os.path.dirname(__file__)
Copy link
Contributor

Choose a reason for hiding this comment

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

Small side-note. We have a tool that allows you to find the absolute path.

from mbedtls_dev import build_tree
root_dir = build_tree.guess_mbedtls_root()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I've used it recently in another PR. However in this case I think it is better to keep a relative path instead of an absolute one (relative in this case means local to the psasim project/folder, while absolute means something related to the MbedTLS root folder). It is more portable in my opinion in case in the future we move the psasim project to the framework folder/repo. Wdyt?

sids = open("psa_manifest/sid.h", "a")
man = open(os.path.join(GENERATED_H_PATH, FILENAME + ".h"), "w")
pids = open(os.path.join(GENERATED_H_PATH, "pid.h"), "a")
sids = open(os.path.join(GENERATED_H_PATH, "sid.h"), "a")
Copy link
Contributor

Choose a reason for hiding this comment

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

question: would it make sense to create a function open_in_include_dir() to avoid the repetition of the os.path.joins?

@@ -19,6 +19,9 @@
GENERATED_H_PATH = os.path.join(SCRIPT_PATH, "..", "include", "psa_manifest")
GENERATED_C_PATH = os.path.join(SCRIPT_PATH, "..", "src")

def open_in_include_dir(filename: str):
return open(os.path.join(GENERATED_H_PATH, filename), "w")
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 a fan of this syntax, since it will not close the file handler. I would remove this function and use nested with statements instead,

with open(os.path.join(GENERATED_H_PATH, "pid.h"), "w"):
    with open(os.path.join(GENERATED_H_PATH, "sid.h"), "w"):
         with open(os.path.join(GENERATED_H_PATH, FILENAME + ".h"), "w"): 
             ...other logic....

Alternatively we can use striongIO objects to compile our files and then write them into the headers.

Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC you don't need to nest, but can separate open()s with just ,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, but those files are opened inside another with open() block (for reading the input file) and they are written to in several lines along the script. Following this approach means that a good part of the script would be right indented a lot. Don't get me wrong, I agree with you that what you are proposing is the right way of writing to a file in a python script, but I'm only stating that this change basically requires/suggests to heavily modify that script.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can keep the open_in_include_dir function, but it should be used like open, i.e.

with open_in_include_dir('foo.h') as foo:
    ... foo.read() ...

a good part of the script would be right indented a lot

That's a hint to break the code into more auxiliary functions/methods.

sids.close()
man.close()
with open(MANIFEST_FILE, "wt") as output:
output.write("".join(manifest_content))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should that be output.write("\n".join(manifest_content)) ?

Don' t we need each of those entries to be on its' own line?

Same with SID and PID files bellow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should that be output.write("\n".join(manifest_content)) ?

It actually works because I just replaced sid.write() with sid_content.append(), but I didn't change the content of the parenthesis, so what was generated before it will be also now. However following Tom's suggestion I can remove new lines from the parenthesis so later there will be "".join() as you suggested.

While at this, fix also Makefile so that "make clean" does not
complain if some of the files to be cancelled do not exist.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Copy link
Contributor

@minosgalanakis minosgalanakis left a comment

Choose a reason for hiding this comment

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

Looks good to me.

The psa_autogen.py file is an intermediate file that will be updated as we move to the more generic library version.

For the current purposes it serves the scope to add basic proof-of contect testing target.

Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-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

C_FILENAME = os.path.join(GENERATED_C_PATH, "psa_ff_bootstrap_" + partition_name + ".c")
c_content = []
c_content.append("#include <init.h>")
c_content.append("#include \"" + symbols[0] + "\"")
Copy link
Contributor

Choose a reason for hiding this comment

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

or

print(f'#include "{symbols[0]}"')

Copy link
Contributor

Choose a reason for hiding this comment

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

Just a remark, not an objection (quite the contrary): f-strings are a new feature in Python 3.6. We can use Python 3.6 constructs only since last week and this would be our first non-3.6-compatible code.

Roadmap Board for Mbed TLS automation moved this from In Development to Has Approval May 16, 2024
@tom-cosgrove-arm tom-cosgrove-arm added approved Design and code approved - may be waiting for CI or backports needs-ci Needs to pass CI tests and removed needs-review Every commit must be reviewed by at least two team members, labels May 16, 2024
@valeriosetti valeriosetti removed the needs-ci Needs to pass CI tests label May 17, 2024
@valeriosetti
Copy link
Contributor Author

@tom-cosgrove-arm I just noticed that you added both the approved label and the need-ci one, but this PR has a green CI. I'm not sure I understood your goal. Were you expecting some change?

@tom-cosgrove-arm tom-cosgrove-arm added this pull request to the merge queue May 20, 2024
Merged via the queue into Mbed-TLS:development with commit df1bfec May 20, 2024
6 checks passed
Roadmap Board for Mbed TLS automation moved this from Has Approval to Done May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports priority-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)
Development

Successfully merging this pull request may close these issues.

Add client-server build to all.sh
4 participants