From c73538fdc4a810b3cd359e207535491fa32b92f2 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Mon, 24 Feb 2025 14:49:27 +0100 Subject: [PATCH 1/7] programs: re-add dlopen dlopen was unintentionally removed in d3b897d100b401f710fb11fdd0a22bcc6580ef74. This commit just adds it back with: git checkout d3b897d -- programs/test/dlopen.c but it does not modify its content. This will be done in follow-up commits. Signed-off-by: Valerio Setti --- programs/test/dlopen.c | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 programs/test/dlopen.c diff --git a/programs/test/dlopen.c b/programs/test/dlopen.c new file mode 100644 index 000000000..0ddf5da77 --- /dev/null +++ b/programs/test/dlopen.c @@ -0,0 +1,59 @@ +/* + * Test dynamic loading of libtfpsacrypto + * + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + */ + +#include "psa/build_info.h" +#include "psa/crypto.h" +#include "tf_psa_crypto/platform.h" + +#if defined(__APPLE__) +#define SO_SUFFIX ".dylib" +#else +#define SO_SUFFIX ".so" +#endif + +#define CRYPTO_SO_FILENAME "libtfpsacrypto" SO_SUFFIX +#define CRYPTO_SO_PATH "core/" CRYPTO_SO_FILENAME +#include +#include + +#define CHECK_DLERROR(function, argument) \ + do \ + { \ + char *CHECK_DLERROR_error = dlerror(); \ + if (CHECK_DLERROR_error != NULL) \ + { \ + fprintf(stderr, "Dynamic loading error for %s(%s): %s\n", \ + function, argument, CHECK_DLERROR_error); \ + tf_psa_crypto_exit(TF_PSA_CRYPTO_EXIT_FAILURE); \ + } \ + } \ + while (0) + +int main(void) +{ + void *crypto_so = dlopen(CRYPTO_SO_PATH, RTLD_NOW); + CHECK_DLERROR("dlopen", CRYPTO_SO_PATH); + + psa_status_t (*psa_crypto_init_ptr)(void) = dlsym(crypto_so, "psa_crypto_init"); + CHECK_DLERROR("dlsym", "psa_crypto_init"); + + psa_status_t status = psa_crypto_init_ptr(); + if (status == PSA_SUCCESS) + { + tf_psa_crypto_printf("dlopen(%s): Call to psa_crypto_init was successful.\n", + CRYPTO_SO_FILENAME); + } + else + { + tf_psa_crypto_printf("dlopen(%s): Call to psa_crypto_init failed.\n", + CRYPTO_SO_FILENAME); + } + + dlclose(crypto_so); + CHECK_DLERROR("dlclose", CRYPTO_SO_PATH); + return 0; +} From 0ee36ea29e6db3d6ffcbc6f4b9b508601643b428 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Mon, 24 Feb 2025 15:50:29 +0100 Subject: [PATCH 2/7] programs: fix dlopen to build in tf-psa-crypto This commit also adds support for building dlopen in CMakeList.txt. Signed-off-by: Valerio Setti --- programs/test/CMakeLists.txt | 12 +++++++++++- programs/test/dlopen.c | 10 +++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/programs/test/CMakeLists.txt b/programs/test/CMakeLists.txt index 3af587072..ca9507367 100644 --- a/programs/test/CMakeLists.txt +++ b/programs/test/CMakeLists.txt @@ -1,7 +1,6 @@ set(executables benchmark ) -add_dependencies(${programs_target} ${executables}) foreach(exe IN LISTS executables) add_executable(${exe} ${exe}.c $) @@ -10,6 +9,17 @@ foreach(exe IN LISTS executables) target_include_directories(${exe} PRIVATE ${TF_PSA_CRYPTO_FRAMEWORK_DIR}/framework/tests/include) endforeach() +if(USE_SHARED_TF_PSA_CRYPTO_LIBRARY AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "[Ww][Ii][Nn]") + add_executable(dlopen "dlopen.c") + set_base_compile_options(dlopen) + target_include_directories(dlopen PRIVATE ${TF_PSA_CRYPTO_DIR}/drivers/builtin/include/) + target_link_libraries(dlopen ${CMAKE_DL_LIBS} ${tfpsacrypto_target}) + add_dependencies(${programs_target} dlopen) + list(APPEND executables dlopen) +endif() + +add_dependencies(${programs_target} ${executables}) + install(TARGETS ${executables} DESTINATION "bin" PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) diff --git a/programs/test/dlopen.c b/programs/test/dlopen.c index 0ddf5da77..770bd1a35 100644 --- a/programs/test/dlopen.c +++ b/programs/test/dlopen.c @@ -5,9 +5,9 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ -#include "psa/build_info.h" +#include "tf-psa-crypto/build_info.h" #include "psa/crypto.h" -#include "tf_psa_crypto/platform.h" +#include "mbedtls/platform.h" #if defined(__APPLE__) #define SO_SUFFIX ".dylib" @@ -28,7 +28,7 @@ { \ fprintf(stderr, "Dynamic loading error for %s(%s): %s\n", \ function, argument, CHECK_DLERROR_error); \ - tf_psa_crypto_exit(TF_PSA_CRYPTO_EXIT_FAILURE); \ + mbedtls_exit(MBEDTLS_EXIT_FAILURE); \ } \ } \ while (0) @@ -44,12 +44,12 @@ int main(void) psa_status_t status = psa_crypto_init_ptr(); if (status == PSA_SUCCESS) { - tf_psa_crypto_printf("dlopen(%s): Call to psa_crypto_init was successful.\n", + mbedtls_printf("dlopen(%s): Call to psa_crypto_init was successful.\n", CRYPTO_SO_FILENAME); } else { - tf_psa_crypto_printf("dlopen(%s): Call to psa_crypto_init failed.\n", + mbedtls_printf("dlopen(%s): Call to psa_crypto_init failed.\n", CRYPTO_SO_FILENAME); } From 790ad5d0c52238ad52472ba2fcadf57bfb799a96 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Mon, 24 Feb 2025 17:35:50 +0100 Subject: [PATCH 3/7] programs: fix cmake source file Program files do not have a 100% common pattern to be built, so intead of a for-loop, we create a common function that sets up what can be shared and then each program is built on its own. Signed-off-by: Valerio Setti --- programs/test/CMakeLists.txt | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/programs/test/CMakeLists.txt b/programs/test/CMakeLists.txt index ca9507367..2a71ada96 100644 --- a/programs/test/CMakeLists.txt +++ b/programs/test/CMakeLists.txt @@ -1,25 +1,26 @@ -set(executables - benchmark -) +# Create an empty list of executables which will be filled by all the targets +# that will be built below. This is used at the bottom for the install() command. +set(executables) -foreach(exe IN LISTS executables) - add_executable(${exe} ${exe}.c $) +function(tfpsacrypto_build_program_common exe) + add_executable(${exe} ${exe}.c) set_base_compile_options(${exe}) - target_link_libraries(${exe} ${tfpsacrypto_target} ${CMAKE_THREAD_LIBS_INIT}) - target_include_directories(${exe} PRIVATE ${TF_PSA_CRYPTO_FRAMEWORK_DIR}/framework/tests/include) -endforeach() + add_dependencies(${programs_target} ${exe}) + list(APPEND executables exe) +endfunction() +# benchmark +tfpsacrypto_build_program_common(benchmark) +target_sources(benchmark PRIVATE $) +target_link_libraries(benchmark PRIVATE ${tfpsacrypto_target} ${CMAKE_THREAD_LIBS_INIT}) + +# dlopen if(USE_SHARED_TF_PSA_CRYPTO_LIBRARY AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "[Ww][Ii][Nn]") - add_executable(dlopen "dlopen.c") - set_base_compile_options(dlopen) + tfpsacrypto_build_program_common(dlopen) target_include_directories(dlopen PRIVATE ${TF_PSA_CRYPTO_DIR}/drivers/builtin/include/) - target_link_libraries(dlopen ${CMAKE_DL_LIBS} ${tfpsacrypto_target}) - add_dependencies(${programs_target} dlopen) - list(APPEND executables dlopen) + target_link_libraries(dlopen PRIVATE ${CMAKE_DL_LIBS} ${tfpsacrypto_target}) endif() -add_dependencies(${programs_target} ${executables}) - install(TARGETS ${executables} DESTINATION "bin" PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) From 304a38610ee51d9d09bae33bb498cf9d9b1d7611 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 25 Feb 2025 06:41:07 +0100 Subject: [PATCH 4/7] programs: rename dlopen target for tf-sa-crypto repo This avoid target name clashing with the dlopen program in the main repository. Signed-off-by: Valerio Setti --- programs/test/CMakeLists.txt | 6 +++--- programs/test/{dlopen.c => tfpsacrypto_dlopen.c} | 0 2 files changed, 3 insertions(+), 3 deletions(-) rename programs/test/{dlopen.c => tfpsacrypto_dlopen.c} (100%) diff --git a/programs/test/CMakeLists.txt b/programs/test/CMakeLists.txt index 2a71ada96..14c6a142d 100644 --- a/programs/test/CMakeLists.txt +++ b/programs/test/CMakeLists.txt @@ -16,9 +16,9 @@ target_link_libraries(benchmark PRIVATE ${tfpsacrypto_target} ${CMAKE_THREAD_LIB # dlopen if(USE_SHARED_TF_PSA_CRYPTO_LIBRARY AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "[Ww][Ii][Nn]") - tfpsacrypto_build_program_common(dlopen) - target_include_directories(dlopen PRIVATE ${TF_PSA_CRYPTO_DIR}/drivers/builtin/include/) - target_link_libraries(dlopen PRIVATE ${CMAKE_DL_LIBS} ${tfpsacrypto_target}) + tfpsacrypto_build_program_common(tfpsacrypto_dlopen) + target_include_directories(tfpsacrypto_dlopen PRIVATE ${TF_PSA_CRYPTO_DIR}/drivers/builtin/include/) + target_link_libraries(tfpsacrypto_dlopen PRIVATE ${CMAKE_DL_LIBS} ${tfpsacrypto_target}) endif() install(TARGETS ${executables} diff --git a/programs/test/dlopen.c b/programs/test/tfpsacrypto_dlopen.c similarity index 100% rename from programs/test/dlopen.c rename to programs/test/tfpsacrypto_dlopen.c From 68df14285586486d0d6c7c4534d22a156ef2e0a5 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 25 Feb 2025 07:21:02 +0100 Subject: [PATCH 5/7] test: add component to test shared library build and dlopen Signed-off-by: Valerio Setti --- tests/scripts/components-build-system.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/scripts/components-build-system.sh b/tests/scripts/components-build-system.sh index 2c5c29301..3b8c23c75 100644 --- a/tests/scripts/components-build-system.sh +++ b/tests/scripts/components-build-system.sh @@ -9,6 +9,16 @@ #### Build System Testing ################################################################ +component_test_tf_psa_crypto_cmake_shared () { + msg "build/test: cmake shared" # ~ 2min + cd $OUT_OF_SOURCE_DIR + cmake -DUSE_SHARED_TF_PSA_CRYPTO_LIBRARY=ON "$TF_PSA_CRYPTO_ROOT_DIR" + make + ldd programs/test/benchmark | grep libtfpsacrypto + make test + $FRAMEWORK/tests/programs/dlopen_demo.sh +} + component_test_tf_psa_crypto_cmake_out_of_source () { msg "build: cmake tf-psa-crypto 'out-of-source' build" cd $OUT_OF_SOURCE_DIR From 753ede883716c78703b411c25e02099ee76a435c Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 25 Feb 2025 09:18:28 +0100 Subject: [PATCH 6/7] programs: do not specify dynamic library path in dlopen The path of the dynamic library should be set using LD_LIBRARY_PATH environment variable. Signed-off-by: Valerio Setti --- programs/test/tfpsacrypto_dlopen.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/programs/test/tfpsacrypto_dlopen.c b/programs/test/tfpsacrypto_dlopen.c index 770bd1a35..f5bd6bf49 100644 --- a/programs/test/tfpsacrypto_dlopen.c +++ b/programs/test/tfpsacrypto_dlopen.c @@ -16,7 +16,6 @@ #endif #define CRYPTO_SO_FILENAME "libtfpsacrypto" SO_SUFFIX -#define CRYPTO_SO_PATH "core/" CRYPTO_SO_FILENAME #include #include @@ -35,8 +34,8 @@ int main(void) { - void *crypto_so = dlopen(CRYPTO_SO_PATH, RTLD_NOW); - CHECK_DLERROR("dlopen", CRYPTO_SO_PATH); + void *crypto_so = dlopen(CRYPTO_SO_FILENAME, RTLD_NOW); + CHECK_DLERROR("dlopen", CRYPTO_SO_FILENAME); psa_status_t (*psa_crypto_init_ptr)(void) = dlsym(crypto_so, "psa_crypto_init"); CHECK_DLERROR("dlsym", "psa_crypto_init"); @@ -54,6 +53,6 @@ int main(void) } dlclose(crypto_so); - CHECK_DLERROR("dlclose", CRYPTO_SO_PATH); + CHECK_DLERROR("dlclose", CRYPTO_SO_FILENAME); return 0; } From ea26dea823532e12c4737372ff63f3da11f92054 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 25 Feb 2025 09:20:47 +0100 Subject: [PATCH 7/7] framework: update reference Signed-off-by: Valerio Setti --- framework | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework b/framework index 523a12d05..6af301316 160000 --- a/framework +++ b/framework @@ -1 +1 @@ -Subproject commit 523a12d05b91301b020e2aa560d9774135e3a801 +Subproject commit 6af30131669c64316d1026d11585ff9340bd56e9