From 746d01c4c54df92840f9781309b8525a2cc24c80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Desbiens?= Date: Wed, 11 Nov 2026 17:16:54 -0500 Subject: [PATCH] Added a test that the Cortex-A35 module ports refuse user mode #817 removed TXM_MODULE_USER_MODE and TXM_MODULE_MEMORY_PROTECTION from the supported options of the four Cortex-A35 and Cortex-A35 SMP module port headers without a test, so nothing kept either option from returning and a module from again loading as user-mode while running at the Module Manager's exception level. The new host test compiles the Module Manager's initialization and loaders against each of the four headers, at native 64-bit width because the ports are AArch64. Through the in-place and absolute loaders it expects user mode, alone or with memory protection, to be refused with TXM_MODULE_INVALID_PROPERTIES, and a privileged module, with or without shared external memory access, to load. Memory protection alone is refused on every port, so the test also checks the options the manager initializes from the port. The port's alignment macro makes the loaders report set-but-unused variables, which these targets silence. 4/4 pass on dev with zero warnings in the default, notify-disabled, both stack-checking and trace configurations; 17/17 Module Manager tests pass. With #817 reverted, 4/4 fail, 8 of 27 expectations each; re-adding the options to one header fails only its test. Both loaders take the supported-options check both ways. Assisted-by: Claude Code (Opus 5.5) --- test/tx/cmake/module_manager/CMakeLists.txt | 47 +++ ...odule_manager_cortex_a35_properties_test.c | 344 ++++++++++++++++++ 2 files changed, 391 insertions(+) create mode 100644 test/tx/module_manager/threadx_module_manager_cortex_a35_properties_test.c diff --git a/test/tx/cmake/module_manager/CMakeLists.txt b/test/tx/cmake/module_manager/CMakeLists.txt index d5fef5290..6989a2a0f 100644 --- a/test/tx/cmake/module_manager/CMakeLists.txt +++ b/test/tx/cmake/module_manager/CMakeLists.txt @@ -393,3 +393,50 @@ target_compile_options( add_test(${CMAKE_BUILD_TYPE}::threadx_module_manager_load_bounds_test threadx_module_manager_load_bounds_test) + +# This test checks the module options the Cortex-A35 module ports accept, so it is +# compiled once against each of their four headers rather than against a stand-in +# port. The SMP ports take the SMP kernel's headers. These ports are AArch64, whose +# ALIGN_TYPE is as wide as a pointer only at 64 bits, so these targets build at +# native 64-bit width rather than the suite's -m32. The port's alignment macro +# assigns the code and data alignments in place, including into the loaders' and +# utility's discarded copies, which -Wall reports as set but not used. +foreach(a35_port cortex_a35/gnu cortex_a35/ac6 cortex_a35_smp/gnu cortex_a35_smp/ac6) + string(REPLACE "/" "_" a35_name ${a35_port}) + set(a35_target threadx_module_manager_${a35_name}_properties_test) + + if(a35_port MATCHES "_smp/") + set(a35_common_inc ${REPO_ROOT}/common_smp/inc) + else() + set(a35_common_inc ${REPO_ROOT}/common/inc) + endif() + + add_executable( + ${a35_target} + ${SOURCE_DIR}/threadx_module_manager_cortex_a35_properties_test.c + ${module_manager_dir}/src/txm_module_manager_initialize.c + ${module_manager_dir}/src/txm_module_manager_internal_load.c + ${module_manager_dir}/src/txm_module_manager_in_place_load.c + ${module_manager_dir}/src/txm_module_manager_absolute_load_extended.c + ${module_manager_dir}/src/txm_module_manager_util.c) + + target_include_directories( + ${a35_target} + PRIVATE ${SOURCE_DIR} + ${a35_common_inc} + ${REPO_ROOT}/common_modules/inc + ${module_manager_dir}/inc + ${REPO_ROOT}/ports_module/${a35_port}/inc) + + target_compile_options( + ${a35_target} + PRIVATE -m64 + -Wall + -Wextra + -Wno-unused-but-set-variable + -include ${SOURCE_DIR}/threadx_module_manager_host_test_port.h) + + target_link_options(${a35_target} PRIVATE -m64) + + add_test(${CMAKE_BUILD_TYPE}::${a35_target} ${a35_target}) +endforeach() diff --git a/test/tx/module_manager/threadx_module_manager_cortex_a35_properties_test.c b/test/tx/module_manager/threadx_module_manager_cortex_a35_properties_test.c new file mode 100644 index 000000000..b1ad87363 --- /dev/null +++ b/test/tx/module_manager/threadx_module_manager_cortex_a35_properties_test.c @@ -0,0 +1,344 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * AI Disclosure: This file was largely AI-generated by Claude Code (Opus 5.5). + * The AI-generated portions may be considered public domain (CC0-1.0) + * and not subject to the project's licence. The human contributor has + * reviewed and verified that the code is correct. + * + * SPDX-License-Identifier: MIT and CC0-1.0 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Test */ +/** */ +/** Module Manager Cortex-A35 module properties */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +/* The Cortex-A35 and Cortex-A35 SMP module ports start every module thread at the + Module Manager's own exception level, give it the kernel dispatcher directly, and + program no translation tables for it. They cannot isolate a module, so a module that + requests user mode or memory protection must be refused at load rather than run + privileged while its integrator believes it is confined. + + The build compiles this test once against each of the four Cortex-A35 module port + headers. The supported and required options come from the port's own definitions, + through _txm_module_manager_initialize, and the two loaders that hold a copy of the + property check, the internal loader behind the in-place load and the absolute + loader, are each given modules requesting: + + - user mode, and user mode with memory protection, which must be refused with + TXM_MODULE_INVALID_PROPERTIES; + - no option, and shared external memory access, which must still load. + + A module requesting memory protection alone is refused on every port, because + memory protection requires user mode, so the loaders cannot show memory protection + refused on its own. The test therefore also checks that the supported options the + manager initialized from the port include neither property. */ + +#include +#include +#include +#include + +#include "threadx_module_manager_host_test_port.h" + +#include "tx_initialize.h" +#include "tx_thread.h" +#include "tx_timer.h" +#include "tx_queue.h" +#include "tx_event_flags.h" +#include "tx_semaphore.h" +#include "tx_mutex.h" +#include "tx_block_pool.h" +#include "tx_byte_pool.h" +#include "txm_module.h" + + +/* Define the stand-in interrupt lock the port shim counts through. */ + +unsigned int test_interrupt_disable_depth; +unsigned int test_interrupt_disable_max_depth; +unsigned int test_interrupt_restore_underflows; + + +/* Define the kernel state the sources under test link against; the Module Manager + state is defined by its initialization source. The SMP kernel reads the system + state through a per-core function instead. */ + +#ifdef TX_THREAD_SMP +ULONG _tx_thread_smp_current_state_get(VOID) +{ + return((ULONG) 0); +} +#else +volatile ULONG _tx_thread_system_state; +#endif + +TX_BLOCK_POOL *_tx_block_pool_created_ptr; +ULONG _tx_block_pool_created_count; +TX_BYTE_POOL *_tx_byte_pool_created_ptr; +ULONG _tx_byte_pool_created_count; +TX_EVENT_FLAGS_GROUP *_tx_event_flags_created_ptr; +ULONG _tx_event_flags_created_count; +TX_MUTEX *_tx_mutex_created_ptr; +ULONG _tx_mutex_created_count; +TX_QUEUE *_tx_queue_created_ptr; +ULONG _tx_queue_created_count; +TX_SEMAPHORE *_tx_semaphore_created_ptr; +ULONG _tx_semaphore_created_count; +TX_THREAD *_tx_thread_created_ptr; +ULONG _tx_thread_created_count; +TX_TIMER *_tx_timer_created_ptr; +ULONG _tx_timer_created_count; + + +static UINT test_failures; +static ULONG test_checks; + + +/* Record the outcome of one expectation. */ +static VOID test_expect(const char *description, ULONG actual, ULONG expected) +{ + test_checks++; + + if (actual != expected) + { + printf("FAIL: %s (expected %lu, got %lu)\n", description, (unsigned long) expected, (unsigned long) actual); + test_failures++; + } +} + + +/* The protection mutex, which counts its owner so that a refusal that returns + holding it is caught. */ + +static ULONG test_mutex_depth; + +UINT _tx_mutex_create(TX_MUTEX *mutex_ptr, CHAR *name_ptr, UINT inherit) +{ + (VOID) mutex_ptr; + (VOID) name_ptr; + (VOID) inherit; + + test_mutex_depth = ((ULONG) 0); + + return(TX_SUCCESS); +} + +UINT _tx_mutex_get(TX_MUTEX *mutex_ptr, ULONG wait_option) +{ + (VOID) mutex_ptr; + (VOID) wait_option; + + test_mutex_depth++; + + return(TX_SUCCESS); +} + +UINT _tx_mutex_put(TX_MUTEX *mutex_ptr) +{ + (VOID) mutex_ptr; + + test_mutex_depth--; + + return(TX_SUCCESS); +} + + +/* The byte pool the in-place loader allocates module data from: a bump allocator + over the arena the manager is initialized with. */ + +static UCHAR *test_pool_start; +static ULONG test_pool_size; +static ULONG test_pool_offset; + +UINT _tx_byte_pool_create(TX_BYTE_POOL *pool_ptr, CHAR *name_ptr, VOID *pool_start, ULONG pool_size) +{ + (VOID) pool_ptr; + (VOID) name_ptr; + + test_pool_start = (UCHAR *) pool_start; + test_pool_size = pool_size; + test_pool_offset = ((ULONG) 0); + + return(TX_SUCCESS); +} + +UINT _tx_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr, ULONG memory_size, ULONG wait_option) +{ + +UINT status; + + + (VOID) pool_ptr; + (VOID) wait_option; + + if (memory_size > (test_pool_size - test_pool_offset)) + { + status = TX_NO_MEMORY; + } + else + { + *memory_ptr = (VOID *) &test_pool_start[test_pool_offset]; + test_pool_offset = test_pool_offset + memory_size; + status = TX_SUCCESS; + } + + return(status); +} + +UINT _tx_byte_release(VOID *memory_ptr) +{ + (VOID) memory_ptr; + + return(TX_SUCCESS); +} + + +/* The module image, its data area for the absolute loader, and the manager's arena. + The port's entry addresses are 32 bits wide, so the absolute loader can only place + a module below 4 GiB; the mapping is asked for there. */ + +#define TEST_MAPPING_HINT ((ALIGN_TYPE) 0x10000000) +#define TEST_MAPPING_BYTES ((ULONG) 16384) +#define TEST_IMAGE_BYTES ((ULONG) 512) +#define TEST_DATA_OFFSET ((ULONG) 4096) +#define TEST_ARENA_OFFSET ((ULONG) 8192) +#define TEST_SHELL_OFFSET ((ULONG) 128) +#define TEST_START_OFFSET ((ULONG) 132) +#define TEST_STOP_OFFSET ((ULONG) 136) +#define TEST_CALLBACK_OFFSET ((ULONG) 140) + +static UCHAR *test_mapping; +static TXM_MODULE_INSTANCE test_instance; + + +/* Build a module image requesting the given options. The entry fields are offsets + for the in-place loader, or absolute addresses when absolute is set. */ +static VOID test_image_build(ULONG options, UINT absolute) +{ + +TXM_MODULE_PREAMBLE *preamble; +ULONG base; + + + preamble = (TXM_MODULE_PREAMBLE *) test_mapping; + + memset(test_mapping, 0, TEST_IMAGE_BYTES); + + base = (absolute != TX_FALSE) ? (ULONG) (ALIGN_TYPE) test_mapping : ((ULONG) 0); + + preamble -> txm_module_preamble_id = TXM_MODULE_ID; + preamble -> txm_module_preamble_preamble_size = ((ULONG) sizeof(TXM_MODULE_PREAMBLE)) / ((ULONG) sizeof(ULONG)); + preamble -> txm_module_preamble_property_flags = TXM_MODULE_GNU_COMPILER | options; + preamble -> txm_module_preamble_shell_entry_function = base + TEST_SHELL_OFFSET; + preamble -> txm_module_preamble_start_function = base + TEST_START_OFFSET; + preamble -> txm_module_preamble_stop_function = base + TEST_STOP_OFFSET; + preamble -> txm_module_preamble_callback_function = base + TEST_CALLBACK_OFFSET; + preamble -> txm_module_preamble_start_stop_priority = ((ULONG) 10); + preamble -> txm_module_preamble_start_stop_stack_size = ((ULONG) 256); + preamble -> txm_module_preamble_callback_priority = ((ULONG) 10); + preamble -> txm_module_preamble_callback_stack_size = ((ULONG) 256); + preamble -> txm_module_preamble_code_size = TEST_IMAGE_BYTES; + preamble -> txm_module_preamble_data_size = ((ULONG) 128); +} + + +/* Load a module requesting the given options through one loader, from an empty + manager, and check the status and that the mutex is free afterwards. */ +static VOID test_load_expect(UINT absolute, ULONG options, UINT expected, const char *what) +{ + +char description[200]; +const char *loader_name; +UINT status; + + + memset(&test_instance, 0, sizeof(test_instance)); + _txm_module_manager_loaded_list_ptr = TX_NULL; + _txm_module_manger_loaded_count = ((ULONG) 0); + test_pool_offset = ((ULONG) 0); + + test_image_build(options, absolute); + + if (absolute != TX_FALSE) + { + loader_name = "absolute_load_extended"; + status = _txm_module_manager_absolute_load_extended(&test_instance, "module", test_mapping, test_mapping + TEST_DATA_OFFSET); + } + else + { + loader_name = "in_place_load"; + status = _txm_module_manager_in_place_load(&test_instance, "module", test_mapping); + } + + snprintf(description, sizeof(description), "%s: %s", loader_name, what); + test_expect(description, (ULONG) status, (ULONG) expected); + + snprintf(description, sizeof(description), "%s: %s leaves the mutex free", loader_name, what); + test_expect(description, test_mutex_depth, ((ULONG) 0)); +} + + +int main(void) +{ + +UINT absolute; + + + test_mapping = (UCHAR *) mmap((VOID *) TEST_MAPPING_HINT, (size_t) TEST_MAPPING_BYTES, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if ((test_mapping == (UCHAR *) MAP_FAILED) || + ((ALIGN_TYPE) test_mapping > (ALIGN_TYPE) (((ULONG) 0xFFFFFFFFUL) - TEST_MAPPING_BYTES))) + { + printf("ERROR: could not map the module image below 4 GiB\n"); + return(1); + } + + (VOID) _txm_module_manager_initialize((VOID *) (test_mapping + TEST_ARENA_OFFSET), TEST_MAPPING_BYTES - TEST_ARENA_OFFSET); + + test_expect("the manager is ready", (ULONG) _txm_module_manager_ready, (ULONG) TX_TRUE); + test_expect("the port does not support user mode", + _txm_module_manager_properties_supported & TXM_MODULE_USER_MODE, ((ULONG) 0)); + test_expect("the port does not support memory protection", + _txm_module_manager_properties_supported & TXM_MODULE_MEMORY_PROTECTION, ((ULONG) 0)); + test_expect("the port supports shared external memory access", + _txm_module_manager_properties_supported & TXM_MODULE_SHARED_EXTERNAL_MEMORY_ACCESS, + TXM_MODULE_SHARED_EXTERNAL_MEMORY_ACCESS); + test_expect("the port requires no option", _txm_module_manager_properties_required, ((ULONG) 0)); + + for (absolute = TX_FALSE; absolute <= TX_TRUE; absolute++) + { + test_load_expect(absolute, TXM_MODULE_USER_MODE, TXM_MODULE_INVALID_PROPERTIES, + "a user-mode module is refused"); + test_load_expect(absolute, TXM_MODULE_USER_MODE | TXM_MODULE_MEMORY_PROTECTION, TXM_MODULE_INVALID_PROPERTIES, + "a user-mode, memory-protected module is refused"); + test_load_expect(absolute, TXM_MODULE_USER_MODE | TXM_MODULE_MEMORY_PROTECTION | TXM_MODULE_SHARED_EXTERNAL_MEMORY_ACCESS, + TXM_MODULE_INVALID_PROPERTIES, "a user-mode, memory-protected module with shared memory is refused"); + test_load_expect(absolute, ((ULONG) 0), TX_SUCCESS, "a privileged module loads"); + test_load_expect(absolute, TXM_MODULE_SHARED_EXTERNAL_MEMORY_ACCESS, TX_SUCCESS, + "a privileged module with shared memory loads"); + } + + test_expect("the interrupt lock is balanced", (ULONG) test_interrupt_disable_depth, ((ULONG) 0)); + test_expect("the interrupt lock never underflowed", (ULONG) test_interrupt_restore_underflows, ((ULONG) 0)); + + if (test_failures == 0U) + { + printf("SUCCESS! %lu expectations\n", (unsigned long) test_checks); + return(0); + } + + printf("ERROR: %u expectation(s) failed of %lu\n", test_failures, (unsigned long) test_checks); + return(1); +}