From dfff3543036e35b58024ce013ec22f291ac9ca04 Mon Sep 17 00:00:00 2001 From: Dev10-sys Date: Sun, 22 Nov 2026 13:41:37 +0530 Subject: [PATCH 1/2] Covered zero-event suspension and SMP lower priority inheritance change In tx_event_flags_get, calling the function with requested_flags set to 0 and wait_option != TX_NO_WAIT enters the early return branch setting status = TX_NO_EVENTS, preventing suspension on zero events. Tests previously only covered this with TX_NO_WAIT. Added regression cases in both standard and SMP suites to verify immediate return when suspension is requested on zero event flags. In common_smp/src/tx_thread_smp_utilities.c, the simple priority change helper handles cases where the new priority is lower (numerically greater) than the thread's inherited priority by maintaining the thread's active priority and preempt threshold at the inherited priority. Added a test case in threadx_thread_priority_change.c to exercise this branch and properly restore thread priority state afterwards. Signed-off-by: Dev10-sys --- .../regression/threadx_event_flag_basic_test.c | 12 ++++++++++++ .../regression/threadx_thread_priority_change.c | 16 +++++++++++++++- .../regression/threadx_event_flag_basic_test.c | 12 ++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/test/smp/regression/threadx_event_flag_basic_test.c b/test/smp/regression/threadx_event_flag_basic_test.c index fed424a65..7dd932134 100644 --- a/test/smp/regression/threadx_event_flag_basic_test.c +++ b/test/smp/regression/threadx_event_flag_basic_test.c @@ -445,6 +445,18 @@ ULONG actual_events; test_control_return(1); } + /* Get zero events with suspension. */ + status = tx_event_flags_get(&group_0, 0, TX_OR, &actual_events, 10); + + /* Check status. */ + if (status != TX_NO_EVENTS) + { + + /* Event flag error. */ + printf("ERROR #16b\n"); + test_control_return(1); + } + /* Set evetns with a NULL group pointer. */ status = tx_event_flags_set(TX_NULL, 0x80008000, TX_OR); diff --git a/test/smp/regression/threadx_thread_priority_change.c b/test/smp/regression/threadx_thread_priority_change.c index d22f3d721..1d5a6082b 100644 --- a/test/smp/regression/threadx_thread_priority_change.c +++ b/test/smp/regression/threadx_thread_priority_change.c @@ -482,9 +482,23 @@ TX_THREAD *temp_thread; thread_0.tx_thread_inherit_priority = 20; _tx_thread_smp_simple_priority_change(&thread_0, 16); + /* Now exercise the else branch where the new priority is lower (numerically + greater) than or equal to the inherited priority, so the thread priority + retains the inherited priority. */ + thread_0.tx_thread_inherit_priority = 10; + _tx_thread_smp_simple_priority_change(&thread_0, 16); + if ((thread_0.tx_thread_priority != 10) || (thread_0.tx_thread_preempt_threshold != 10)) + { + printf("ERROR #15b\n"); + test_control_return(1); + } + /* Put the inheritance priority back to the value that means this thread is - not inheriting one, so nothing downstream reasons about a phantom. */ + not inheriting one, and restore thread priority. Align tx_thread_priority + with the list it currently lives in before calling the helper again. */ + thread_0.tx_thread_priority = 16; thread_0.tx_thread_inherit_priority = TX_MAX_PRIORITIES; + _tx_thread_smp_simple_priority_change(&thread_0, 16); TX_RESTORE /* Successful test. */ diff --git a/test/tx/regression/threadx_event_flag_basic_test.c b/test/tx/regression/threadx_event_flag_basic_test.c index fed424a65..7dd932134 100644 --- a/test/tx/regression/threadx_event_flag_basic_test.c +++ b/test/tx/regression/threadx_event_flag_basic_test.c @@ -445,6 +445,18 @@ ULONG actual_events; test_control_return(1); } + /* Get zero events with suspension. */ + status = tx_event_flags_get(&group_0, 0, TX_OR, &actual_events, 10); + + /* Check status. */ + if (status != TX_NO_EVENTS) + { + + /* Event flag error. */ + printf("ERROR #16b\n"); + test_control_return(1); + } + /* Set evetns with a NULL group pointer. */ status = tx_event_flags_set(TX_NULL, 0x80008000, TX_OR); From dfac3661e7fa89549c5dfa8df617565b9ed7b123 Mon Sep 17 00:00:00 2001 From: Dev10-sys Date: Sat, 28 Nov 2026 09:19:06 +0530 Subject: [PATCH 2/2] Add regression and unit tests for kernel, utilities, module manager, and FreeRTOS wrapper Add unit and regression tests to close remaining execution and coverage gaps across the ThreadX core scheduler, SMP time-slicing, module manager host suite, low-power and execution profile utilities, and the FreeRTOS compatibility layer: - test/tx/regression/threadx_thread_basic_execution_test.c: Exercise deferred _tx_thread_system_resume preemption handoff when _tx_thread_preempt_disable > 0 and higher- then lower-priority threads are resumed. - test/smp/regression/threadx_smp_time_slice_test.c: Exercise the SMP time-slice ready-list scan wraparound path when all ready equal-priority threads are currently executing on cores. - test/freertos/regression/: Add runtime tests covering queues, queue sets, semaphores, mutexes, task lifecycle, notifications, event groups, and software timers. - test/tx/low_power/ and test/tx/execution_profile/: Add standalone host tests for low-power enter/exit/next-timer helpers and execution profile tick/ISR/thread/idle counters. - test/tx/module_manager/: Add host tests covering module callback notification trampolines and queue deduplication, full module manager lifecycle and cleanup paths, and dispatcher parameter, range, and ownership validation. Signed-off-by: Dev10-sys --- test/freertos/cmake/regression/CMakeLists.txt | 5 +- .../regression/txfr_queue_runtime_test.c | 245 +++++ .../regression/txfr_sync_runtime_test.c | 261 +++++ .../regression/txfr_task_and_timer_test.c | 380 +++++++ .../regression/threadx_smp_time_slice_test.c | 36 +- test/tx/cmake/CMakeLists.txt | 2 + .../tx/cmake/execution_profile/CMakeLists.txt | 21 + test/tx/cmake/low_power/CMakeLists.txt | 21 + test/tx/cmake/module_manager/CMakeLists.txt | 148 +++ .../threadx_execution_profile_test.c | 198 ++++ test/tx/execution_profile/tx_user.h | 20 + test/tx/low_power/threadx_low_power_test.c | 261 +++++ test/tx/low_power/tx_user.h | 24 + ...module_manager_callback_trampolines_test.c | 495 +++++++++ ...dx_module_manager_dispatch_security_test.c | 831 +++++++++++++++ .../threadx_module_manager_lifecycle_test.c | 958 ++++++++++++++++++ .../threadx_thread_basic_execution_test.c | 67 ++ 17 files changed, 3968 insertions(+), 5 deletions(-) create mode 100644 test/freertos/regression/txfr_queue_runtime_test.c create mode 100644 test/freertos/regression/txfr_sync_runtime_test.c create mode 100644 test/freertos/regression/txfr_task_and_timer_test.c create mode 100644 test/tx/cmake/execution_profile/CMakeLists.txt create mode 100644 test/tx/cmake/low_power/CMakeLists.txt create mode 100644 test/tx/execution_profile/threadx_execution_profile_test.c create mode 100644 test/tx/execution_profile/tx_user.h create mode 100644 test/tx/low_power/threadx_low_power_test.c create mode 100644 test/tx/low_power/tx_user.h create mode 100644 test/tx/module_manager/threadx_module_manager_callback_trampolines_test.c create mode 100644 test/tx/module_manager/threadx_module_manager_dispatch_security_test.c create mode 100644 test/tx/module_manager/threadx_module_manager_lifecycle_test.c diff --git a/test/freertos/cmake/regression/CMakeLists.txt b/test/freertos/cmake/regression/CMakeLists.txt index 21da2a6dc..3d30a3c44 100644 --- a/test/freertos/cmake/regression/CMakeLists.txt +++ b/test/freertos/cmake/regression/CMakeLists.txt @@ -26,8 +26,11 @@ set(LAYER_INCLUDE_DIR ${REPO_ROOT}/utility/rtos_compatibility_layers/FreeRTOS) set(freertos_test_cases ${SOURCE_DIR}/txfr_queue_create_test.c + ${SOURCE_DIR}/txfr_queue_runtime_test.c ${SOURCE_DIR}/txfr_task_create_test.c - ${SOURCE_DIR}/txfr_sync_create_test.c) + ${SOURCE_DIR}/txfr_task_and_timer_test.c + ${SOURCE_DIR}/txfr_sync_create_test.c + ${SOURCE_DIR}/txfr_sync_runtime_test.c) # tx_api.h maps the public API onto the error checking entry points, so those # are the symbols that exist at link time and the ones the harness wraps. A diff --git a/test/freertos/regression/txfr_queue_runtime_test.c b/test/freertos/regression/txfr_queue_runtime_test.c new file mode 100644 index 000000000..cae6ff662 --- /dev/null +++ b/test/freertos/regression/txfr_queue_runtime_test.c @@ -0,0 +1,245 @@ +/*************************************************************************** + * Copyright (c) 2026-present 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include "txfr_test_harness.h" + +#define TXFR_QUEUE_LEN 3U +#define TXFR_DRIVER_PRIO ((UBaseType_t)2U) +#define TXFR_WORKER_PRIO ((UBaseType_t)3U) +#define TXFR_WAIT_TICKS ((TickType_t)50U) + +static StaticQueue_t static_queue_cb; +static uint8_t static_queue_storage[TXFR_QUEUE_LEN * sizeof(uint32_t)]; +static SemaphoreHandle_t test_done_sem = NULL; +static QueueHandle_t shared_queue = NULL; + +static void queue_producer_task(void *arg) +{ + uint32_t item; + SemaphoreHandle_t ack_sem = (SemaphoreHandle_t)arg; + + txfr_test_check("consumer starts blocked on empty queue", + uxQueueMessagesWaiting(shared_queue) == 0U); + item = 0x11111111U; + txfr_test_check("producer sends item waking blocked consumer", + xQueueSendToBack(shared_queue, &item, TXFR_WAIT_TICKS) == pdPASS); + + xSemaphoreGive(ack_sem); + vTaskDelete(NULL); +} + +static void queue_consumer_task(void *arg) +{ + uint32_t received = 0U; + SemaphoreHandle_t ack_sem = (SemaphoreHandle_t)arg; + + txfr_test_check("consumer receives item sent by producer", + xQueueReceive(shared_queue, &received, TXFR_WAIT_TICKS) == pdPASS); + txfr_test_check("consumer receives expected payload", + received == 0x11111111U); + + xSemaphoreGive(ack_sem); + vTaskDelete(NULL); +} + +static void test_fifo_lifo_peek_reset_and_wrap(QueueHandle_t q) +{ + uint32_t vals[4] = {10U, 20U, 30U, 40U}; + uint32_t out = 0U; + BaseType_t woken = pdFALSE; + + txfr_test_check("empty queue reports 0 messages", + uxQueueMessagesWaiting(q) == 0U); + txfr_test_check("empty queue reports full spaces", + uxQueueSpacesAvailable(q) == TXFR_QUEUE_LEN); + txfr_test_check("empty queue IsEmptyFromISR returns pdTRUE", + xQueueIsQueueEmptyFromISR(q) == pdTRUE); + txfr_test_check("empty queue IsFullFromISR returns pdFALSE", + xQueueIsQueueFullFromISR(q) == pdFALSE); + txfr_test_check("nonblocking receive on empty queue fails", + xQueueReceive(q, &out, 0U) == errQUEUE_EMPTY); + txfr_test_check("nonblocking peek on empty queue fails", + xQueuePeek(q, &out, 0U) == errQUEUE_EMPTY); + + txfr_test_check("send back item 0", xQueueSend(q, &vals[0], 0U) == pdPASS); + txfr_test_check("send back item 1", xQueueSendToBack(q, &vals[1], 0U) == pdPASS); + txfr_test_check("send front item 2", xQueueSendToFront(q, &vals[2], 0U) == pdPASS); + + txfr_test_check("full queue count is TXFR_QUEUE_LEN", + uxQueueMessagesWaiting(q) == TXFR_QUEUE_LEN); + txfr_test_check("full queue spaces available is 0", + uxQueueSpacesAvailable(q) == 0U); + txfr_test_check("full queue IsFullFromISR returns pdTRUE", + xQueueIsQueueFullFromISR(q) == pdTRUE); + txfr_test_check("send to full queue with zero wait returns errQUEUE_FULL", + xQueueSend(q, &vals[3], 0U) == errQUEUE_FULL); + txfr_test_check("send to front on full queue with zero wait returns errQUEUE_FULL", + xQueueSendToFront(q, &vals[3], 0U) == errQUEUE_FULL); + + txfr_test_check("peek sees front item without removing", + xQueuePeek(q, &out, 0U) == pdPASS && out == 30U); + txfr_test_check("peek FromISR also sees front item", + xQueuePeekFromISR(q, &out) == pdPASS && out == 30U); + txfr_test_check("count unchanged after peek", + uxQueueMessagesWaitingFromISR(q) == TXFR_QUEUE_LEN); + + txfr_test_check("receive 1 gets front item 30", + xQueueReceive(q, &out, 0U) == pdPASS && out == 30U); + txfr_test_check("send back item 3 while partially drained wraps tail", + xQueueSendToBackFromISR(q, &vals[3], &woken) == pdPASS); + txfr_test_check("receive 2 gets 10", + xQueueReceiveFromISR(q, &out, &woken) == pdPASS && out == 10U); + txfr_test_check("receive 3 gets 20", + xQueueReceive(q, &out, 0U) == pdPASS && out == 20U); + txfr_test_check("receive 4 gets wrapped item 40", + xQueueReceive(q, &out, 0U) == pdPASS && out == 40U); + txfr_test_check("queue is empty again after wrap drain", + uxQueueMessagesWaiting(q) == 0U); + + woken = pdFALSE; + txfr_test_check("send FromISR succeeds", + xQueueSendFromISR(q, &vals[0], &woken) == pdPASS); + txfr_test_check("send front FromISR succeeds", + xQueueSendToFrontFromISR(q, &vals[1], &woken) == pdPASS); + txfr_test_check("xQueueReset clears queued items", + xQueueReset(q) == pdPASS && uxQueueMessagesWaiting(q) == 0U && + uxQueueSpacesAvailable(q) == TXFR_QUEUE_LEN); +} + +static void test_queue_overwrite_and_sets(void) +{ + QueueHandle_t q_len1; + QueueHandle_t member_q1; + QueueHandle_t member_q2; + QueueSetHandle_t qset; + uint32_t v = 100U; + uint32_t out = 0U; + BaseType_t woken = pdFALSE; + + q_len1 = xQueueCreate(1U, sizeof(uint32_t)); + txfr_test_check("create length-1 queue for overwrite", q_len1 != NULL); + txfr_test_check("overwrite on empty queue succeeds", + xQueueOverwrite(q_len1, &v) == pdPASS); + v = 200U; + txfr_test_check("overwrite on full length-1 queue replaces item", + xQueueOverwrite(q_len1, &v) == pdPASS); + txfr_test_check("count remains 1 after overwrite", + uxQueueMessagesWaiting(q_len1) == 1U); + v = 300U; + txfr_test_check("overwrite FromISR replaces item", + xQueueOverwriteFromISR(q_len1, &v, &woken) == pdPASS); + txfr_test_check("receive after overwrites yields latest value", + xQueueReceive(q_len1, &out, 0U) == pdPASS && out == 300U); + vQueueDelete(q_len1); + + member_q1 = xQueueCreate(2U, sizeof(uint32_t)); + member_q2 = xQueueCreate(2U, sizeof(uint32_t)); + qset = xQueueCreateSet(4U); + txfr_test_check("create queue set and members", + (member_q1 != NULL) && (member_q2 != NULL) && (qset != NULL)); + txfr_test_check("add queues to queue set", + (xQueueAddToSet(member_q1, qset) == pdPASS) && + (xQueueAddToSet(member_q2, qset) == pdPASS)); + txfr_test_check("select from empty queue set times out", + xQueueSelectFromSet(qset, 2U) == NULL); + + v = 555U; + txfr_test_check("send to member_q2 wakes set selection", + xQueueSend(member_q2, &v, 0U) == pdPASS); + txfr_test_check("select from set returns member_q2", + xQueueSelectFromSet(qset, 0U) == (QueueSetMemberHandle_t)member_q2); + txfr_test_check("receive from selected member_q2", + xQueueReceive(member_q2, &out, 0U) == pdPASS && out == 555U); + + v = 666U; + txfr_test_check("send to member_q1 for FromISR set select", + xQueueSend(member_q1, &v, 0U) == pdPASS); + txfr_test_check("SelectFromSetFromISR returns member_q1", + xQueueSelectFromSetFromISR(qset) == (QueueSetMemberHandle_t)member_q1); + txfr_test_check("receive from selected member_q1", + xQueueReceive(member_q1, &out, 0U) == pdPASS && out == 666U); + + txfr_test_check("remove member queues from set", + (xQueueRemoveFromSet(member_q1, qset) == pdPASS) && + (xQueueRemoveFromSet(member_q2, qset) == pdPASS)); + vQueueDelete(member_q1); + vQueueDelete(member_q2); +} + +static void queue_driver_task(void *arg) +{ + QueueHandle_t dyn_q; + QueueHandle_t stat_q; + SemaphoreHandle_t ack_sem; + uint32_t out = 0U; + + (void)arg; + + dyn_q = xQueueCreate(TXFR_QUEUE_LEN, sizeof(uint32_t)); + stat_q = xQueueCreateStatic(TXFR_QUEUE_LEN, sizeof(uint32_t), + static_queue_storage, &static_queue_cb); + txfr_test_check("dynamic and static queues created", + (dyn_q != NULL) && (stat_q != NULL)); + + test_fifo_lifo_peek_reset_and_wrap(dyn_q); + test_fifo_lifo_peek_reset_and_wrap(stat_q); + vQueueDelete(dyn_q); + vQueueDelete(stat_q); + + shared_queue = xQueueCreate(1U, sizeof(uint32_t)); + ack_sem = xSemaphoreCreateCounting(2U, 0U); + txfr_test_check("create cross-task queue and ack semaphore", + (shared_queue != NULL) && (ack_sem != NULL)); + + txfr_test_check("timed receive on empty queue returns errQUEUE_EMPTY", + xQueueReceive(shared_queue, &out, 2U) == errQUEUE_EMPTY); + + TaskHandle_t cons_handle = NULL; + TaskHandle_t prod_handle = NULL; + + txfr_test_check("spawn consumer task", + xTaskCreate(queue_consumer_task, "q_cons", + configMINIMAL_STACK_SIZE * 2U, ack_sem, + TXFR_WORKER_PRIO, &cons_handle) == pdPASS); + txfr_test_check("spawn producer task", + xTaskCreate(queue_producer_task, "q_prod", + configMINIMAL_STACK_SIZE * 2U, ack_sem, + TXFR_DRIVER_PRIO, &prod_handle) == pdPASS); + + txfr_test_check("wait for producer completion", + xSemaphoreTake(ack_sem, TXFR_WAIT_TICKS) == pdPASS); + txfr_test_check("wait for consumer completion", + xSemaphoreTake(ack_sem, TXFR_WAIT_TICKS) == pdPASS); + + vQueueDelete(shared_queue); + vSemaphoreDelete(ack_sem); + + test_queue_overwrite_and_sets(); + + xSemaphoreGive(test_done_sem); + vTaskDelete(NULL); +} + +void txfr_test_body(void) +{ + TaskHandle_t drv_handle = NULL; + + test_done_sem = xSemaphoreCreateBinary(); + txfr_test_check("create queue test completion semaphore", test_done_sem != NULL); + + txfr_test_check("create queue driver task", + xTaskCreate(queue_driver_task, "q_drv", + configMINIMAL_STACK_SIZE * 2U, NULL, + TXFR_DRIVER_PRIO, &drv_handle) == pdPASS); + + txfr_test_check("queue runtime driver finishes", + xSemaphoreTake(test_done_sem, TXFR_WAIT_TICKS * 4U) == pdPASS); + vSemaphoreDelete(test_done_sem); +} diff --git a/test/freertos/regression/txfr_sync_runtime_test.c b/test/freertos/regression/txfr_sync_runtime_test.c new file mode 100644 index 000000000..57094bff5 --- /dev/null +++ b/test/freertos/regression/txfr_sync_runtime_test.c @@ -0,0 +1,261 @@ +/*************************************************************************** + * Copyright (c) 2026-present 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include "txfr_test_harness.h" + +#define TXFR_SYNC_DRIVER_PRIO ((UBaseType_t)2U) +#define TXFR_SYNC_WORKER_PRIO ((UBaseType_t)3U) +#define TXFR_SYNC_WAIT_TICKS ((TickType_t)50U) + +static StaticSemaphore_t static_bin_sem; +static StaticSemaphore_t static_cnt_sem; +static StaticSemaphore_t static_mutex; +static StaticSemaphore_t static_rec_mutex; + +static SemaphoreHandle_t test_done_sem = NULL; + +typedef struct sync_worker_ctx { + SemaphoreHandle_t sync_obj; + SemaphoreHandle_t done_sem; + BaseType_t take_before_give; +} sync_worker_ctx_t; + +static void bin_sem_giver_task(void *arg) +{ + sync_worker_ctx_t *ctx = (sync_worker_ctx_t *)arg; + + vTaskDelay(2U); + txfr_test_check("worker gives binary semaphore", + xSemaphoreGive(ctx->sync_obj) == pdPASS); + xSemaphoreGive(ctx->done_sem); + vTaskDelete(NULL); +} + +static void mutex_contender_task(void *arg) +{ + sync_worker_ctx_t *ctx = (sync_worker_ctx_t *)arg; + + txfr_test_check("non-owner mutex give fails", + xSemaphoreGive(ctx->sync_obj) == pdFALSE); + txfr_test_check("non-owner nonblocking mutex take fails while held", + xSemaphoreTake(ctx->sync_obj, 0U) == pdFALSE); + txfr_test_check("contender blocks until owner releases mutex", + xSemaphoreTake(ctx->sync_obj, TXFR_SYNC_WAIT_TICKS) == pdPASS); + txfr_test_check("contender becomes current mutex holder", + xSemaphoreGetMutexHolder(ctx->sync_obj) == xTaskGetCurrentTaskHandle()); + txfr_test_check("contender releases mutex", + xSemaphoreGive(ctx->sync_obj) == pdPASS); + + xSemaphoreGive(ctx->done_sem); + vTaskDelete(NULL); +} + +static void rec_mutex_contender_task(void *arg) +{ + sync_worker_ctx_t *ctx = (sync_worker_ctx_t *)arg; + + txfr_test_check("non-owner nonblocking recursive take fails while nested", + xSemaphoreTakeRecursive(ctx->sync_obj, 0U) == pdFALSE); + txfr_test_check("contender acquires recursive mutex once fully released", + xSemaphoreTakeRecursive(ctx->sync_obj, TXFR_SYNC_WAIT_TICKS) == pdPASS); + txfr_test_check("contender releases recursive mutex", + xSemaphoreGiveRecursive(ctx->sync_obj) == pdPASS); + + xSemaphoreGive(ctx->done_sem); + vTaskDelete(NULL); +} + +static void test_binary_semaphores(void) +{ + SemaphoreHandle_t bin_dyn; + SemaphoreHandle_t bin_stat; + SemaphoreHandle_t ack_sem; + sync_worker_ctx_t ctx; + BaseType_t woken = pdFALSE; + + bin_dyn = xSemaphoreCreateBinary(); + bin_stat = xSemaphoreCreateBinaryStatic(&static_bin_sem); + ack_sem = xSemaphoreCreateBinary(); + txfr_test_check("binary semaphores created", + (bin_dyn != NULL) && (bin_stat != NULL) && (ack_sem != NULL)); + + txfr_test_check("binary semaphore starts empty", + (uxSemaphoreGetCount(bin_dyn) == 0U) && + (xSemaphoreTake(bin_dyn, 0U) == pdFALSE)); + txfr_test_check("binary semaphore give sets count to 1", + (xSemaphoreGive(bin_dyn) == pdPASS) && + (uxSemaphoreGetCount(bin_dyn) == 1U)); + txfr_test_check("second binary semaphore give hits ceiling 1", + xSemaphoreGive(bin_dyn) == pdFALSE); + txfr_test_check("binary semaphore take empties it", + (xSemaphoreTake(bin_dyn, 0U) == pdPASS) && + (uxSemaphoreGetCount(bin_dyn) == 0U)); + + txfr_test_check("binary GiveFromISR sets count to 1", + (xSemaphoreGiveFromISR(bin_stat, &woken) == pdPASS) && + (uxSemaphoreGetCount(bin_stat) == 1U)); + txfr_test_check("binary TakeFromISR takes token", + (xSemaphoreTakeFromISR(bin_stat, &woken) == pdPASS) && + (uxSemaphoreGetCount(bin_stat) == 0U)); + + TaskHandle_t worker_handle = NULL; + + ctx.sync_obj = bin_dyn; + ctx.done_sem = ack_sem; + ctx.take_before_give = pdTRUE; + txfr_test_check("spawn binary semaphore giver worker", + xTaskCreate(bin_sem_giver_task, "bin_give", + configMINIMAL_STACK_SIZE * 2U, &ctx, + TXFR_SYNC_WORKER_PRIO, &worker_handle) == pdPASS); + txfr_test_check("blocking take unblocks when worker gives", + xSemaphoreTake(bin_dyn, TXFR_SYNC_WAIT_TICKS) == pdPASS); + txfr_test_check("worker finishes binary semaphore test", + xSemaphoreTake(ack_sem, TXFR_SYNC_WAIT_TICKS) == pdPASS); + + vSemaphoreDelete(bin_dyn); + vSemaphoreDelete(bin_stat); + vSemaphoreDelete(ack_sem); +} + +static void test_counting_semaphores(void) +{ + SemaphoreHandle_t cnt_dyn; + SemaphoreHandle_t cnt_stat; + + cnt_dyn = xSemaphoreCreateCounting(3U, 1U); + cnt_stat = xSemaphoreCreateCountingStatic(2U, 2U, &static_cnt_sem); + txfr_test_check("counting semaphores created", + (cnt_dyn != NULL) && (cnt_stat != NULL)); + + txfr_test_check("initial counting value matches requested", + (uxSemaphoreGetCount(cnt_dyn) == 1U) && + (uxSemaphoreGetCount(cnt_stat) == 2U)); + txfr_test_check("counting give increments up to max_count", + (xSemaphoreGive(cnt_dyn) == pdPASS) && + (xSemaphoreGive(cnt_dyn) == pdPASS) && + (uxSemaphoreGetCount(cnt_dyn) == 3U)); + txfr_test_check("counting give past max_count fails", + xSemaphoreGive(cnt_dyn) == pdFALSE); + + txfr_test_check("counting take drains all 3 tokens", + (xSemaphoreTake(cnt_dyn, 0U) == pdPASS) && + (xSemaphoreTake(cnt_dyn, 0U) == pdPASS) && + (xSemaphoreTake(cnt_dyn, 0U) == pdPASS) && + (uxSemaphoreGetCount(cnt_dyn) == 0U)); + txfr_test_check("counting take on 0 with zero timeout fails", + xSemaphoreTake(cnt_dyn, 0U) == pdFALSE); + txfr_test_check("counting take with short timeout times out", + xSemaphoreTake(cnt_dyn, 2U) == pdFALSE); + + vSemaphoreDelete(cnt_dyn); + vSemaphoreDelete(cnt_stat); +} + +static void test_mutexes_and_recursive_mutexes(void) +{ + SemaphoreHandle_t mtx_dyn; + SemaphoreHandle_t mtx_stat; + SemaphoreHandle_t rmtx_dyn; + SemaphoreHandle_t rmtx_stat; + SemaphoreHandle_t ack_sem; + sync_worker_ctx_t ctx; + TaskHandle_t self_handle = xTaskGetCurrentTaskHandle(); + TaskHandle_t contender_handle = NULL; + TaskHandle_t rec_contender_handle = NULL; + BaseType_t woken = pdFALSE; + + mtx_dyn = xSemaphoreCreateMutex(); + mtx_stat = xSemaphoreCreateMutexStatic(&static_mutex); + rmtx_dyn = xSemaphoreCreateRecursiveMutex(); + rmtx_stat = xSemaphoreCreateRecursiveMutexStatic(&static_rec_mutex); + ack_sem = xSemaphoreCreateBinary(); + txfr_test_check("mutexes and recursive mutexes created", + (mtx_dyn != NULL) && (mtx_stat != NULL) && + (rmtx_dyn != NULL) && (rmtx_stat != NULL) && + (ack_sem != NULL)); + + txfr_test_check("TakeFromISR on mutex returns pdFALSE", + xSemaphoreTakeFromISR(mtx_dyn, &woken) == pdFALSE); + + txfr_test_check("owner acquires mutex", + xSemaphoreTake(mtx_dyn, 0U) == pdPASS); + txfr_test_check("non-recursive mutex refuses same-owner re-take", + xSemaphoreTake(mtx_dyn, 0U) == pdFALSE); + txfr_test_check("mutex holder matches current task", + (xSemaphoreGetMutexHolder(mtx_dyn) == self_handle) && + (xSemaphoreGetMutexHolderFromISR(mtx_dyn) == self_handle)); + + ctx.sync_obj = mtx_dyn; + ctx.done_sem = ack_sem; + ctx.take_before_give = pdFALSE; + txfr_test_check("spawn mutex contender", + xTaskCreate(mutex_contender_task, "mtx_cnt", + configMINIMAL_STACK_SIZE * 2U, &ctx, + TXFR_SYNC_WORKER_PRIO, &contender_handle) == pdPASS); + vTaskDelay(2U); + txfr_test_check("owner releases mutex waking contender", + xSemaphoreGive(mtx_dyn) == pdPASS); + txfr_test_check("mutex contender finishes", + xSemaphoreTake(ack_sem, TXFR_SYNC_WAIT_TICKS) == pdPASS); + + txfr_test_check("recursive mutex first take succeeds", + xSemaphoreTakeRecursive(rmtx_dyn, 0U) == pdPASS); + txfr_test_check("recursive mutex second nested take succeeds", + xSemaphoreTakeRecursive(rmtx_dyn, 0U) == pdPASS); + txfr_test_check("first recursive give keeps ownership (count 2 -> 1)", + xSemaphoreGiveRecursive(rmtx_dyn) == pdPASS); + + ctx.sync_obj = rmtx_dyn; + ctx.done_sem = ack_sem; + txfr_test_check("spawn recursive mutex contender", + xTaskCreate(rec_mutex_contender_task, "rmtx_cnt", + configMINIMAL_STACK_SIZE * 2U, &ctx, + TXFR_SYNC_WORKER_PRIO, &rec_contender_handle) == pdPASS); + vTaskDelay(2U); + txfr_test_check("second recursive give releases ownership", + xSemaphoreGiveRecursive(rmtx_dyn) == pdPASS); + txfr_test_check("recursive contender finishes", + xSemaphoreTake(ack_sem, TXFR_SYNC_WAIT_TICKS) == pdPASS); + + vSemaphoreDelete(mtx_dyn); + vSemaphoreDelete(mtx_stat); + vSemaphoreDelete(rmtx_dyn); + vSemaphoreDelete(rmtx_stat); + vSemaphoreDelete(ack_sem); +} + +static void sync_driver_task(void *arg) +{ + (void)arg; + + test_binary_semaphores(); + test_counting_semaphores(); + test_mutexes_and_recursive_mutexes(); + + xSemaphoreGive(test_done_sem); + vTaskDelete(NULL); +} + +void txfr_test_body(void) +{ + TaskHandle_t drv_handle = NULL; + + test_done_sem = xSemaphoreCreateBinary(); + txfr_test_check("create sync completion semaphore", test_done_sem != NULL); + + txfr_test_check("create sync driver task", + xTaskCreate(sync_driver_task, "sync_drv", + configMINIMAL_STACK_SIZE * 2U, NULL, + TXFR_SYNC_DRIVER_PRIO, &drv_handle) == pdPASS); + + txfr_test_check("sync runtime driver finishes", + xSemaphoreTake(test_done_sem, TXFR_SYNC_WAIT_TICKS * 4U) == pdPASS); + vSemaphoreDelete(test_done_sem); +} diff --git a/test/freertos/regression/txfr_task_and_timer_test.c b/test/freertos/regression/txfr_task_and_timer_test.c new file mode 100644 index 000000000..fc8d56746 --- /dev/null +++ b/test/freertos/regression/txfr_task_and_timer_test.c @@ -0,0 +1,380 @@ +/*************************************************************************** + * Copyright (c) 2026-present 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include "txfr_test_harness.h" + +#define TXFR_TASK_DRIVER_PRIO ((UBaseType_t)2U) +#define TXFR_TASK_HIGH_PRIO ((UBaseType_t)4U) +#define TXFR_TASK_LOW_PRIO ((UBaseType_t)1U) +#define TXFR_GENERAL_WAIT_TICKS ((TickType_t)80U) + +static StaticTask_t static_task_cb; +static StackType_t static_task_stack[configMINIMAL_STACK_SIZE * 2U]; +static StaticEventGroup_t static_ev_cb; +static StaticTimer_t static_timer_cb; + +static SemaphoreHandle_t test_done_sem = NULL; +static volatile uint32_t suspended_worker_ran = 0U; +static volatile uint32_t aborted_worker_done = 0U; +static volatile uint32_t timer_fire_count = 0U; +static void * volatile last_timer_id = NULL; + +static void suspendable_worker_task(void *arg) +{ + (void)arg; + + suspended_worker_ran = 1U; + vTaskSuspend(NULL); + suspended_worker_ran = 2U; + vTaskSuspend(NULL); + suspended_worker_ran = 3U; + + vTaskDelete(NULL); +} + +static void delayed_abort_worker_task(void *arg) +{ + SemaphoreHandle_t block_sem = (SemaphoreHandle_t)arg; + + if (block_sem != NULL) + { + (void)xSemaphoreTake(block_sem, TXFR_GENERAL_WAIT_TICKS * 10U); + } + else + { + vTaskDelay(TXFR_GENERAL_WAIT_TICKS * 10U); + } + aborted_worker_done = 1U; + vTaskDelete(NULL); +} + +static void notify_target_task(void *arg) +{ + uint32_t val = 0U; + SemaphoreHandle_t ack_sem = (SemaphoreHandle_t)arg; + + txfr_test_check("ulTaskNotifyTake(pdFALSE) returns 1 and decrements", + ulTaskNotifyTake(pdFALSE, TXFR_GENERAL_WAIT_TICKS) == 1U); + xSemaphoreGive(ack_sem); + + txfr_test_check("ulTaskNotifyTake(pdTRUE) returns 1 and clears", + ulTaskNotifyTake(pdTRUE, TXFR_GENERAL_WAIT_TICKS) == 1U); + txfr_test_check("ulTaskNotifyTake with 0 wait after clear returns 0", + ulTaskNotifyTake(pdTRUE, 0U) == 0U); + xSemaphoreGive(ack_sem); + + txfr_test_check("xTaskNotifyWait receives eSetBits notification", + xTaskNotifyWait(0U, 0xFFFFFFFFU, &val, TXFR_GENERAL_WAIT_TICKS) == pdPASS); + txfr_test_check("xTaskNotifyWait sees expected bitmask", val == 0x0AU); + xSemaphoreGive(ack_sem); + + vTaskDelete(NULL); +} + +static void ev_setter_task(void *arg) +{ + EventGroupHandle_t ev = (EventGroupHandle_t)arg; + + vTaskDelay(2U); + xEventGroupSetBits(ev, 0x05U); + vTaskDelete(NULL); +} + +static void sample_timer_callback(TimerHandle_t xTimer) +{ + timer_fire_count++; + last_timer_id = pvTimerGetTimerID(xTimer); +} + +static void test_task_lifecycle_and_scheduler(void) +{ + TaskHandle_t self_task = xTaskGetCurrentTaskHandle(); + TaskHandle_t worker = NULL; + TaskHandle_t abort_worker = NULL; + TaskHandle_t stat_worker = NULL; + TickType_t prev_wake; + TickType_t t0; + TickType_t t1; + UBaseType_t base_count; + + txfr_test_check("current task handle and name are valid", + (self_task != NULL) && (strcmp(pcTaskGetName(self_task), "task_drv") == 0)); + txfr_test_check("driver running state and priority match", + (eTaskGetState(self_task) == eRunning) && + (uxTaskPriorityGet(self_task) == TXFR_TASK_DRIVER_PRIO) && + (uxTaskPriorityGetFromISR(self_task) == TXFR_TASK_DRIVER_PRIO)); + txfr_test_check("scheduler state starts running", + xTaskGetSchedulerState() == taskSCHEDULER_RUNNING); + + vTaskSuspendAll(); + txfr_test_check("scheduler state reports suspended during SuspendAll", + xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED); + (void)xTaskResumeAll(); + txfr_test_check("scheduler state restored after ResumeAll", + xTaskGetSchedulerState() == taskSCHEDULER_RUNNING); + + base_count = uxTaskGetNumberOfTasks(); + suspended_worker_ran = 0U; + txfr_test_check("create suspendable worker", + xTaskCreate(suspendable_worker_task, "susp_wrk", + configMINIMAL_STACK_SIZE * 2U, NULL, + TXFR_TASK_HIGH_PRIO, &worker) == pdPASS); + txfr_test_check("task count increased by 1", + uxTaskGetNumberOfTasks() == (base_count + 1U)); + txfr_test_check("higher prio worker ran and suspended itself", + (suspended_worker_ran == 1U) && (eTaskGetState(worker) == eSuspended)); + + vTaskResume(worker); + txfr_test_check("vTaskResume runs worker to stage 2", + (suspended_worker_ran == 2U) && (eTaskGetState(worker) == eSuspended)); + + (void)xTaskResumeFromISR(worker); + { + uint32_t wait_ticks; + for (wait_ticks = 0U; (wait_ticks < 20U) && (suspended_worker_ran != 3U); wait_ticks++) + { + vTaskDelay(1U); + } + } + txfr_test_check("xTaskResumeFromISR ran worker to completion", + suspended_worker_ran == 3U); + + stat_worker = xTaskCreateStatic(delayed_abort_worker_task, "stat_wrk", + configMINIMAL_STACK_SIZE * 2U, NULL, + TXFR_TASK_LOW_PRIO, static_task_stack, + &static_task_cb); + txfr_test_check("create static task succeeds", stat_worker != NULL); + txfr_test_check("change static worker priority cleanly", + (vTaskPrioritySet(stat_worker, TXFR_TASK_HIGH_PRIO), + uxTaskPriorityGet(stat_worker) == TXFR_TASK_HIGH_PRIO)); + vTaskDelete(stat_worker); + { + uint32_t wait_ticks; + for (wait_ticks = 0U; (wait_ticks < 20U) && (uxTaskGetNumberOfTasks() > base_count); wait_ticks++) + { + vTaskDelay(1U); + } + } + txfr_test_check("vTaskDelete cleans up static task", + uxTaskGetNumberOfTasks() <= base_count); + + aborted_worker_done = 0U; + txfr_test_check("create long-sleeping worker for AbortDelay", + xTaskCreate(delayed_abort_worker_task, "abort_wrk", + configMINIMAL_STACK_SIZE * 2U, NULL, + TXFR_TASK_HIGH_PRIO, &abort_worker) == pdPASS); + txfr_test_check("sleeping abort_wrk reports eSuspended state", + eTaskGetState(abort_worker) == eSuspended); + txfr_test_check("xTaskAbortDelay unblocks sleeping task", + xTaskAbortDelay(abort_worker) == pdPASS); + { + uint32_t wait_ticks; + for (wait_ticks = 0U; (wait_ticks < 20U) && (aborted_worker_done != 1U); wait_ticks++) + { + vTaskDelay(1U); + } + } + txfr_test_check("aborted worker finished after abort", + aborted_worker_done == 1U); + + { + SemaphoreHandle_t blk_sem = xSemaphoreCreateBinary(); + TaskHandle_t blk_worker = NULL; + + txfr_test_check("create blocking sem and worker for eBlocked check", + (blk_sem != NULL) && + (xTaskCreate(delayed_abort_worker_task, "blk_wrk", + configMINIMAL_STACK_SIZE * 2U, blk_sem, + TXFR_TASK_HIGH_PRIO, &blk_worker) == pdPASS)); + txfr_test_check("semaphore-blocked worker reports eBlocked state", + eTaskGetState(blk_worker) == eBlocked); + xSemaphoreGive(blk_sem); + vTaskDelay(1U); + vSemaphoreDelete(blk_sem); + } + + t0 = xTaskGetTickCount(); + prev_wake = t0; + vTaskDelayUntil(&prev_wake, 3U); + t1 = xTaskGetTickCountFromISR(); + txfr_test_check("vTaskDelayUntil advances tick count", + (t1 - t0) >= 3U); +} + +static void test_task_notifications(void) +{ + TaskHandle_t self_task = xTaskGetCurrentTaskHandle(); + TaskHandle_t worker = NULL; + SemaphoreHandle_t ack_sem = xSemaphoreCreateCounting(4U, 0U); + uint32_t prev = 0U; + uint32_t val = 0U; + BaseType_t woken = pdFALSE; + + txfr_test_check("create notification test ack sem", ack_sem != NULL); + + txfr_test_check("xTaskNotify eSetValueWithOverwrite succeeds", + xTaskNotify(self_task, 0x04U, eSetValueWithOverwrite) == pdPASS); + txfr_test_check("xTaskNotify eSetValueWithoutOverwrite fails while pending", + xTaskNotify(self_task, 0x08U, eSetValueWithoutOverwrite) == pdFAIL); + txfr_test_check("xTaskNotifyAndQuery eSetBits returns prev and updates", + (xTaskNotifyAndQuery(self_task, 0x02U, eSetBits, &prev) == pdPASS) && + (prev == 0x04U)); + txfr_test_check("xTaskNotifyFromISR eIncrement updates pending value", + xTaskNotifyFromISR(self_task, 0U, eIncrement, &woken) == pdPASS); + txfr_test_check("xTaskNotifyAndQueryFromISR eNoAction preserves value", + (xTaskNotifyAndQueryFromISR(self_task, 99U, eNoAction, &prev, &woken) == pdPASS) && + (prev == 0x07U)); + txfr_test_check("ulTaskNotifyValueClear clears requested bits", + ulTaskNotifyValueClear(self_task, 0x03U) == 0x07U); + txfr_test_check("xTaskNotifyWait reads remaining bits and clears on exit", + (xTaskNotifyWait(0U, 0xFFFFFFFFU, &val, 0U) == pdPASS) && (val == 0x04U)); + txfr_test_check("xTaskNotifyStateClear on idle notification returns pdFALSE", + xTaskNotifyStateClear(self_task) == pdFALSE); + + txfr_test_check("create notification target worker", + xTaskCreate(notify_target_task, "ntf_wrk", + configMINIMAL_STACK_SIZE * 2U, ack_sem, + TXFR_TASK_HIGH_PRIO, &worker) == pdPASS); + txfr_test_check("xTaskNotifyGive unblocks ulTaskNotifyTake(pdFALSE)", + xTaskNotifyGive(worker) == pdPASS); + txfr_test_check("target completed ulTaskNotifyTake(pdFALSE) stage", + xSemaphoreTake(ack_sem, TXFR_GENERAL_WAIT_TICKS) == pdPASS); + + vTaskNotifyGiveFromISR(worker, &woken); + txfr_test_check("target completed ulTaskNotifyTake(pdTRUE) stage", + xSemaphoreTake(ack_sem, TXFR_GENERAL_WAIT_TICKS) == pdPASS); + + txfr_test_check("notify blocked target via eSetBits", + xTaskNotify(worker, 0x0AU, eSetBits) == pdPASS); + txfr_test_check("target completed xTaskNotifyWait stage", + xSemaphoreTake(ack_sem, TXFR_GENERAL_WAIT_TICKS) == pdPASS); + + vSemaphoreDelete(ack_sem); +} + +static void test_event_groups_and_timers(void) +{ + EventGroupHandle_t ev_dyn = xEventGroupCreate(); + EventGroupHandle_t ev_stat = xEventGroupCreateStatic(&static_ev_cb); + EventBits_t bits; + TaskHandle_t ev_worker = NULL; + BaseType_t woken = pdFALSE; + TimerHandle_t t_one_shot; + TimerHandle_t t_periodic; + TimerHandle_t t_static; + int id_a = 1; + int id_b = 2; + + txfr_test_check("event groups created", (ev_dyn != NULL) && (ev_stat != NULL)); + + (void)xEventGroupSetBits(ev_dyn, 0x03U); + txfr_test_check("xEventGroupGetBits sees set bits", + xEventGroupGetBits(ev_dyn) == 0x03U); + txfr_test_check("xEventGroupClearBits clears bit 0", + (xEventGroupClearBits(ev_dyn, 0x01U), + xEventGroupGetBitsFromISR(ev_dyn) == 0x02U)); + txfr_test_check("xEventGroupSetBitsFromISR and ClearBitsFromISR succeed", + ((xEventGroupSetBitsFromISR(ev_stat, 0x10U, &woken) & 0x10U) == 0x10U) && + (xEventGroupClearBitsFromISR(ev_stat, 0x10U) == (BaseType_t)0x10U) && + (xEventGroupGetBitsFromISR(ev_stat) == 0U)); + + txfr_test_check("WaitBits timeout without matching bits returns 0", + xEventGroupWaitBits(ev_dyn, 0x80U, pdFALSE, pdTRUE, 2U) == 0U); + txfr_test_check("spawn event setter task for AND wait", + xTaskCreate(ev_setter_task, "ev_set", + configMINIMAL_STACK_SIZE * 2U, ev_dyn, + TXFR_TASK_LOW_PRIO, &ev_worker) == pdPASS); + bits = xEventGroupWaitBits(ev_dyn, 0x05U, pdTRUE, pdTRUE, TXFR_GENERAL_WAIT_TICKS); + txfr_test_check("WaitBits unblocks on AND mask and clears waited bits", + ((bits & 0x05U) == 0x05U) && ((xEventGroupGetBits(ev_dyn) & 0x05U) == 0U)); + + vEventGroupDelete(ev_dyn); + vEventGroupDelete(ev_stat); + + timer_fire_count = 0U; + last_timer_id = NULL; + t_one_shot = xTimerCreate("one_shot", 2U, pdFALSE, &id_a, sample_timer_callback); + t_periodic = xTimerCreate("periodic", 2U, pdTRUE, &id_b, sample_timer_callback); + t_static = xTimerCreateStatic("stat_tmr", 4U, pdFALSE, &id_a, + sample_timer_callback, + &static_timer_cb); + txfr_test_check("timers created", + (t_one_shot != NULL) && (t_periodic != NULL) && (t_static != NULL)); + txfr_test_check("timer metadata name and period match", + (strcmp(pcTimerGetName(t_one_shot), "one_shot") == 0) && + (xTimerGetPeriod(t_one_shot) == 2U)); + txfr_test_check("unstarted timer is inactive", + xTimerIsTimerActive(t_one_shot) == pdFALSE); + + txfr_test_check("start one-shot timer", + xTimerStart(t_one_shot, TXFR_GENERAL_WAIT_TICKS) == pdPASS); + txfr_test_check("started timer is active with valid expiry time", + (xTimerIsTimerActive(t_one_shot) == pdTRUE) && + (xTimerGetExpiryTime(t_one_shot) >= xTaskGetTickCount())); + vTaskDelay(5U); + txfr_test_check("one-shot timer fired once with expected ID", + (timer_fire_count == 1U) && (last_timer_id == &id_a)); + + vTimerSetTimerID(t_one_shot, &id_b); + txfr_test_check("xTimerResetFromISR restarts timer with updated ID", + xTimerResetFromISR(t_one_shot, &woken) == pdPASS); + vTaskDelay(5U); + txfr_test_check("restarted one-shot timer fired with new ID", + (timer_fire_count == 2U) && (last_timer_id == &id_b)); + + timer_fire_count = 0U; + vTimerSetReloadMode(t_one_shot, pdFALSE); + txfr_test_check("start periodic timer and wait multiple expirations", + xTimerStartFromISR(t_periodic, &woken) == pdPASS); + vTaskDelay(7U); + txfr_test_check("periodic timer fired multiple times", timer_fire_count >= 2U); + txfr_test_check("xTimerChangePeriod and Stop succeed", + (xTimerChangePeriod(t_periodic, 4U, TXFR_GENERAL_WAIT_TICKS) == pdPASS) && + (xTimerChangePeriodFromISR(t_periodic, 5U, &woken) == pdPASS) && + (xTimerReset(t_periodic, TXFR_GENERAL_WAIT_TICKS) == pdPASS) && + (xTimerStop(t_periodic, TXFR_GENERAL_WAIT_TICKS) == pdPASS) && + (xTimerStopFromISR(t_static, &woken) == pdPASS) && + (xTimerIsTimerActive(t_periodic) == pdFALSE)); + + txfr_test_check("timers deleted cleanly", + (xTimerDelete(t_one_shot, TXFR_GENERAL_WAIT_TICKS) == pdPASS) && + (xTimerDelete(t_periodic, TXFR_GENERAL_WAIT_TICKS) == pdPASS) && + (xTimerDelete(t_static, TXFR_GENERAL_WAIT_TICKS) == pdPASS)); +} + +static void task_and_timer_driver(void *arg) +{ + (void)arg; + + test_task_lifecycle_and_scheduler(); + test_task_notifications(); + test_event_groups_and_timers(); + + xSemaphoreGive(test_done_sem); + vTaskDelete(NULL); +} + +void txfr_test_body(void) +{ + TaskHandle_t drv_handle = NULL; + + test_done_sem = xSemaphoreCreateBinary(); + txfr_test_check("create task/timer completion semaphore", test_done_sem != NULL); + + txfr_test_check("create task/timer driver", + xTaskCreate(task_and_timer_driver, "task_drv", + configMINIMAL_STACK_SIZE * 2U, NULL, + TXFR_TASK_DRIVER_PRIO, &drv_handle) == pdPASS); + + txfr_test_check("task and timer runtime driver finishes", + xSemaphoreTake(test_done_sem, TXFR_GENERAL_WAIT_TICKS * 6U) == pdPASS); + vSemaphoreDelete(test_done_sem); +} diff --git a/test/smp/regression/threadx_smp_time_slice_test.c b/test/smp/regression/threadx_smp_time_slice_test.c index c5ab06159..ca97b5e35 100644 --- a/test/smp/regression/threadx_smp_time_slice_test.c +++ b/test/smp/regression/threadx_smp_time_slice_test.c @@ -270,10 +270,38 @@ UINT status; } else { - - /* Successful test. */ - printf("SUCCESS!\n"); - test_control_return(0); + ULONG old_slice; + + /* Terminate all but two equal-priority threads so time-slicing them + while both occupy cores walks the entire ready list back to the + current thread without finding an unscheduled thread. */ + status = tx_thread_terminate(&thread_31c); + status += tx_thread_terminate(&thread_31d); + status += tx_thread_terminate(&thread_31e); + status += tx_thread_terminate(&thread_31f); + status += tx_thread_terminate(&thread_31g); + status += tx_thread_terminate(&thread_31h); + status += tx_thread_smp_core_exclude(&thread_31a, 0); + status += tx_thread_smp_core_exclude(&thread_31b, 0); + status += tx_thread_time_slice_change(&thread_31a, 1, &old_slice); + status += tx_thread_time_slice_change(&thread_31b, 1, &old_slice); + thread_31a_counter = 0; + thread_31b_counter = 0; + + tx_thread_sleep(10); + + if ((status != TX_SUCCESS) || (thread_31a_counter == 0) || (thread_31b_counter == 0)) + { + printf("ERROR #31B\n"); + test_control_return(1); + } + else + { + + /* Successful test. */ + printf("SUCCESS!\n"); + test_control_return(0); + } } } diff --git a/test/tx/cmake/CMakeLists.txt b/test/tx/cmake/CMakeLists.txt index 9bacf9cba..2e965cec8 100644 --- a/test/tx/cmake/CMakeLists.txt +++ b/test/tx/cmake/CMakeLists.txt @@ -120,6 +120,8 @@ endif() # skip them. if(NOT MSVC AND NOT ("TX_MISRA_ENABLE" IN_LIST ${CMAKE_BUILD_TYPE})) add_subdirectory(module_manager) + add_subdirectory(low_power) + add_subdirectory(execution_profile) endif() # Coverage diff --git a/test/tx/cmake/execution_profile/CMakeLists.txt b/test/tx/cmake/execution_profile/CMakeLists.txt new file mode 100644 index 000000000..1df06bef5 --- /dev/null +++ b/test/tx/cmake/execution_profile/CMakeLists.txt @@ -0,0 +1,21 @@ +set(REPO_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../../..) +set(SOURCE_DIR ${REPO_ROOT}/test/tx/execution_profile) + +add_executable( + threadx_execution_profile_test + ${SOURCE_DIR}/threadx_execution_profile_test.c + ${REPO_ROOT}/utility/execution_profile_kit/tx_execution_profile.c) + +target_include_directories( + threadx_execution_profile_test + PRIVATE ${SOURCE_DIR} + ${REPO_ROOT}/common/inc + ${REPO_ROOT}/ports/linux/gnu/inc + ${REPO_ROOT}/utility/execution_profile_kit) + +target_compile_definitions(threadx_execution_profile_test + PRIVATE TX_INCLUDE_USER_DEFINE_FILE) + +target_compile_options(threadx_execution_profile_test PRIVATE -Wall -Wextra -Werror) + +add_test(${CMAKE_BUILD_TYPE}::threadx_execution_profile_test threadx_execution_profile_test) diff --git a/test/tx/cmake/low_power/CMakeLists.txt b/test/tx/cmake/low_power/CMakeLists.txt new file mode 100644 index 000000000..d9214f281 --- /dev/null +++ b/test/tx/cmake/low_power/CMakeLists.txt @@ -0,0 +1,21 @@ +set(REPO_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../../..) +set(SOURCE_DIR ${REPO_ROOT}/test/tx/low_power) + +add_executable( + threadx_low_power_test + ${SOURCE_DIR}/threadx_low_power_test.c + ${REPO_ROOT}/utility/low_power/tx_low_power.c) + +target_include_directories( + threadx_low_power_test + PRIVATE ${SOURCE_DIR} + ${REPO_ROOT}/common/inc + ${REPO_ROOT}/ports/linux/gnu/inc + ${REPO_ROOT}/utility/low_power) + +target_compile_definitions(threadx_low_power_test + PRIVATE TX_INCLUDE_USER_DEFINE_FILE) + +target_compile_options(threadx_low_power_test PRIVATE -Wall -Wextra -Werror) + +add_test(${CMAKE_BUILD_TYPE}::threadx_low_power_test threadx_low_power_test) diff --git a/test/tx/cmake/module_manager/CMakeLists.txt b/test/tx/cmake/module_manager/CMakeLists.txt index d5fef5290..1467a6cd4 100644 --- a/test/tx/cmake/module_manager/CMakeLists.txt +++ b/test/tx/cmake/module_manager/CMakeLists.txt @@ -393,3 +393,151 @@ target_compile_options( add_test(${CMAKE_BUILD_TYPE}::threadx_module_manager_load_bounds_test threadx_module_manager_load_bounds_test) + +if(NOT ("TX_DISABLE_NOTIFY_CALLBACKS" IN_LIST ${CMAKE_BUILD_TYPE})) + add_executable( + threadx_module_manager_callback_trampolines_test + ${SOURCE_DIR}/threadx_module_manager_callback_trampolines_test.c + ${module_manager_dir}/src/txm_module_manager_callback_request.c + ${module_manager_dir}/src/txm_module_manager_event_flags_notify_trampoline.c + ${module_manager_dir}/src/txm_module_manager_kernel_dispatch.c + ${module_manager_dir}/src/txm_module_manager_queue_notify_trampoline.c + ${module_manager_dir}/src/txm_module_manager_semaphore_notify_trampoline.c + ${module_manager_dir}/src/txm_module_manager_thread_notify_trampoline.c + ${module_manager_dir}/src/txm_module_manager_timer_notify_trampoline.c + ${module_manager_dir}/src/txm_module_manager_util.c) + + target_include_directories( + threadx_module_manager_callback_trampolines_test + PRIVATE ${SOURCE_DIR} + ${REPO_ROOT}/common/inc + ${REPO_ROOT}/common_modules/inc + ${module_manager_dir}/inc + ${cortex_m4_module_dir}/inc) + + set(trampoline_dispatchers + TXM_EVENT_FLAGS_SET_NOTIFY_CALL_NOT_USED + TXM_QUEUE_SEND_NOTIFY_CALL_NOT_USED + TXM_SEMAPHORE_PUT_NOTIFY_CALL_NOT_USED + TXM_THREAD_ENTRY_EXIT_NOTIFY_CALL_NOT_USED) + + set(trampoline_guards ${dispatch_guards}) + list(REMOVE_ITEM trampoline_guards ${trampoline_dispatchers}) + + target_compile_definitions(threadx_module_manager_callback_trampolines_test + PRIVATE ${trampoline_guards}) + + target_compile_options( + threadx_module_manager_callback_trampolines_test + PRIVATE -Wall + -Wextra + "SHELL:-include ${SOURCE_DIR}/threadx_module_manager_load_bounds_state.h" + "SHELL:-include ${SOURCE_DIR}/threadx_module_manager_host_test_port.h") + + add_test(${CMAKE_BUILD_TYPE}::threadx_module_manager_callback_trampolines_test + threadx_module_manager_callback_trampolines_test) +endif() + +add_executable( + threadx_module_manager_lifecycle_test + ${SOURCE_DIR}/threadx_module_manager_lifecycle_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_maximum_module_priority_set.c + ${module_manager_dir}/src/txm_module_manager_object_allocate.c + ${module_manager_dir}/src/txm_module_manager_object_deallocate.c + ${module_manager_dir}/src/txm_module_manager_object_pool_create.c + ${module_manager_dir}/src/txm_module_manager_properties_get.c + ${module_manager_dir}/src/txm_module_manager_start.c + ${module_manager_dir}/src/txm_module_manager_stop.c + ${module_manager_dir}/src/txm_module_manager_thread_create.c + ${module_manager_dir}/src/txm_module_manager_thread_reset.c + ${module_manager_dir}/src/txm_module_manager_unload.c + ${module_manager_dir}/src/txm_module_manager_util.c) + +target_include_directories( + threadx_module_manager_lifecycle_test + PRIVATE ${SOURCE_DIR} + ${REPO_ROOT}/common/inc + ${REPO_ROOT}/common_modules/inc + ${module_manager_dir}/inc + ${cortex_m4_module_dir}/inc) + +target_compile_options( + threadx_module_manager_lifecycle_test + PRIVATE -Wall + -Wextra + "SHELL:-include ${SOURCE_DIR}/threadx_module_manager_load_bounds_state.h" + "SHELL:-include ${SOURCE_DIR}/threadx_module_manager_host_test_port.h") + +add_test(${CMAKE_BUILD_TYPE}::threadx_module_manager_lifecycle_test + threadx_module_manager_lifecycle_test) + +add_executable( + threadx_module_manager_dispatch_security_test + ${SOURCE_DIR}/threadx_module_manager_dispatch_security_test.c + ${module_manager_dir}/src/txm_module_manager_kernel_dispatch.c + ${module_manager_dir}/src/txm_module_manager_object_allocate.c + ${module_manager_dir}/src/txm_module_manager_object_deallocate.c + ${module_manager_dir}/src/txm_module_manager_object_pointer_get.c + ${module_manager_dir}/src/txm_module_manager_object_pointer_get_extended.c + ${module_manager_dir}/src/txm_module_manager_timer_notify_trampoline.c + ${module_manager_dir}/src/txm_module_manager_util.c) + +target_include_directories( + threadx_module_manager_dispatch_security_test + PRIVATE ${SOURCE_DIR} + ${REPO_ROOT}/common/inc + ${REPO_ROOT}/common_modules/inc + ${module_manager_dir}/inc + ${cortex_m4_module_dir}/inc) + +set(sec_dispatchers + TXM_BYTE_POOL_CREATE_CALL_NOT_USED + TXM_BYTE_POOL_DELETE_CALL_NOT_USED + TXM_BYTE_RELEASE_CALL_NOT_USED + TXM_EVENT_FLAGS_CREATE_CALL_NOT_USED + TXM_EVENT_FLAGS_DELETE_CALL_NOT_USED + TXM_EVENT_FLAGS_GET_CALL_NOT_USED + TXM_EVENT_FLAGS_SET_CALL_NOT_USED + TXM_MODULE_OBJECT_ALLOCATE_CALL_NOT_USED + TXM_MODULE_OBJECT_POINTER_GET_CALL_NOT_USED + TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL_NOT_USED + TXM_MUTEX_CREATE_CALL_NOT_USED + TXM_MUTEX_DELETE_CALL_NOT_USED + TXM_MUTEX_GET_CALL_NOT_USED + TXM_MUTEX_PRIORITIZE_CALL_NOT_USED + TXM_MUTEX_PUT_CALL_NOT_USED + TXM_SEMAPHORE_CEILING_PUT_CALL_NOT_USED + TXM_SEMAPHORE_CREATE_CALL_NOT_USED + TXM_SEMAPHORE_DELETE_CALL_NOT_USED + TXM_SEMAPHORE_GET_CALL_NOT_USED + TXM_SEMAPHORE_PUT_CALL_NOT_USED + TXM_THREAD_INTERRUPT_CONTROL_CALL_NOT_USED + TXM_THREAD_STACK_ERROR_NOTIFY_CALL_NOT_USED + TXM_TIMER_ACTIVATE_CALL_NOT_USED + TXM_TIMER_CHANGE_CALL_NOT_USED + TXM_TIMER_CREATE_CALL_NOT_USED + TXM_TIMER_DEACTIVATE_CALL_NOT_USED + TXM_TIMER_DELETE_CALL_NOT_USED + TXM_TRACE_ENABLE_CALL_NOT_USED + TXM_TRACE_INTERRUPT_CONTROL_CALL_NOT_USED + TXM_TRACE_ISR_ENTER_INSERT_CALL_NOT_USED + TXM_TRACE_ISR_EXIT_INSERT_CALL_NOT_USED + TXM_TRACE_USER_EVENT_INSERT_CALL_NOT_USED) + +set(sec_guards ${dispatch_guards}) +list(REMOVE_ITEM sec_guards ${sec_dispatchers}) + +target_compile_definitions(threadx_module_manager_dispatch_security_test + PRIVATE ${sec_guards}) + +target_compile_options( + threadx_module_manager_dispatch_security_test + PRIVATE -Wall + -Wextra + "SHELL:-include ${SOURCE_DIR}/threadx_module_manager_load_bounds_state.h" + "SHELL:-include ${SOURCE_DIR}/threadx_module_manager_host_test_port.h") + +add_test(${CMAKE_BUILD_TYPE}::threadx_module_manager_dispatch_security_test + threadx_module_manager_dispatch_security_test) diff --git a/test/tx/execution_profile/threadx_execution_profile_test.c b/test/tx/execution_profile/threadx_execution_profile_test.c new file mode 100644 index 000000000..04f8e8ca0 --- /dev/null +++ b/test/tx/execution_profile/threadx_execution_profile_test.c @@ -0,0 +1,198 @@ +/*************************************************************************** + * Copyright (c) 2026-present 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include + +#include "tx_api.h" +#include "tx_thread.h" + +/* Globals expected by utility/execution_profile_kit/tx_execution_profile.c. */ +volatile ULONG _tx_thread_system_state; +TX_THREAD *_tx_thread_current_ptr; +TX_THREAD *_tx_thread_execute_ptr; +TX_THREAD *_tx_thread_created_ptr; +ULONG _tx_thread_created_count; +volatile UINT _tx_thread_preempt_disable; + +/* Test clock and interrupt-lock accounting. */ +static ULONG epk_clock; +static ULONG test_checks; +static UINT test_failures; +static UINT int_disable_depth; +static UINT int_underflows; + +unsigned long test_epk_clock_read(void) +{ + return((unsigned long) epk_clock); +} + +UINT _tx_thread_interrupt_disable(VOID) +{ +UINT prev = int_disable_depth; + + int_disable_depth++; + return(prev); +} + +VOID _tx_thread_interrupt_restore(UINT previous_posture) +{ + if (int_disable_depth == 0U) + { + int_underflows++; + } + else + { + int_disable_depth = previous_posture; + } +} + +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++; + } +} + +static VOID test_expect64(const char *description, EXECUTION_TIME actual, EXECUTION_TIME expected) +{ + test_checks++; + if (actual != expected) + { + printf("FAIL: %s (expected %llu, got %llu)\n", description, + (unsigned long long) expected, (unsigned long long) actual); + test_failures++; + } +} + +int main(void) +{ +TX_THREAD t1; +TX_THREAD t2; +EXECUTION_TIME val = 0ULL; + + memset((VOID *) &t1, 0, sizeof(t1)); + memset((VOID *) &t2, 0, sizeof(t2)); + t1.tx_thread_id = TX_THREAD_ID; + t2.tx_thread_id = TX_THREAD_ID; + t1.tx_thread_created_next = &t2; + t1.tx_thread_created_previous = &t2; + t2.tx_thread_created_next = &t1; + t2.tx_thread_created_previous = &t1; + _tx_thread_created_ptr = &t1; + _tx_thread_created_count = 2UL; + + epk_clock = 100UL; + _tx_thread_system_state = 0UL; + _tx_thread_preempt_disable = 0U; + _tx_execution_initialize(); + + /* 1. Basic thread enter/exit accumulates thread and total thread execution time. */ + _tx_thread_current_ptr = &t1; + _tx_thread_execute_ptr = &t1; + _tx_execution_thread_enter(); + epk_clock = 250UL; + _tx_thread_execute_ptr = &t2; + _tx_execution_thread_exit(); + + _tx_execution_thread_time_get(&t1, &val); + test_expect64("t1 accumulated 150 ticks", val, 150ULL); + _tx_execution_thread_total_time_get(&val); + test_expect64("total thread time is 150 ticks", val, 150ULL); + + /* 2. Counter wrap-around on thread exit (last_start = 0xFFFFFFF0, current = 0x00000010 -> delta 0x20). */ + epk_clock = 0xFFFFFFF0UL; + _tx_thread_current_ptr = &t2; + _tx_thread_execute_ptr = &t2; + _tx_execution_thread_enter(); + epk_clock = 0x00000010UL; + /* Exit to idle (_tx_thread_execute_ptr == TX_NULL) starts idle timer. */ + _tx_thread_execute_ptr = TX_NULL; + _tx_execution_thread_exit(); + + _tx_execution_thread_time_get(&t2, &val); + test_expect64("t2 wrap-around delta is 32 ticks", val, 0x20ULL); + _tx_execution_thread_total_time_get(&val); + test_expect64("total thread time is 182 ticks", val, 182ULL); + + /* 3. Idle measurement finishes when next thread enters. */ + epk_clock = 0x00000060UL; + _tx_thread_current_ptr = &t1; + _tx_thread_execute_ptr = &t1; + _tx_execution_thread_enter(); + _tx_execution_idle_time_get(&val); + test_expect64("idle accumulated 80 ticks before t1 resumed", val, 0x50ULL); + + /* 4. ISR enter/exit suspends t1, measures ISR duration, and resumes t1. */ + epk_clock = 0x00000090UL; + _tx_thread_system_state = 1UL; + _tx_execution_isr_enter(); + _tx_execution_thread_time_get(&t1, &val); + test_expect64("t1 gained 48 ticks before ISR entry", val, 150ULL + 0x30ULL); + + /* Nested ISR (system_state == 2) must not reset ISR start timestamp. */ + epk_clock = 0x000000A0UL; + _tx_thread_system_state = 2UL; + _tx_execution_isr_enter(); + _tx_execution_isr_exit(); + + epk_clock = 0x000000B0UL; + _tx_thread_system_state = 1UL; + _tx_execution_isr_exit(); + _tx_thread_system_state = 0UL; + _tx_execution_isr_time_get(&val); + test_expect64("outer ISR measured 32 ticks across nested call", val, 0x20ULL); + + /* Interrupted t1 received refreshed last_start at 0xB0 and continues cleanly. */ + epk_clock = 0x000000D0UL; + _tx_execution_thread_exit(); + _tx_execution_thread_time_get(&t1, &val); + test_expect64("t1 resumed timing after ISR exit (+32 ticks)", val, 150ULL + 0x30ULL + 0x20ULL); + + /* 5. Reset APIs clear individual thread, all threads total, ISR, and idle counters. */ + test_expect("_tx_execution_thread_time_reset succeeds", + _tx_execution_thread_time_reset(&t1), TX_SUCCESS); + _tx_execution_thread_time_get(&t1, &val); + test_expect64("t1 reset to 0", val, 0ULL); + + test_expect("_tx_execution_thread_total_time_reset succeeds", + _tx_execution_thread_total_time_reset(), TX_SUCCESS); + _tx_execution_thread_total_time_get(&val); + test_expect64("total thread time reset to 0", val, 0ULL); + _tx_execution_thread_time_get(&t2, &val); + test_expect64("t2 also reset by total_time_reset", val, 0ULL); + + test_expect("_tx_execution_isr_time_reset succeeds", + _tx_execution_isr_time_reset(), TX_SUCCESS); + _tx_execution_isr_time_get(&val); + test_expect64("ISR time reset to 0", val, 0ULL); + + test_expect("_tx_execution_idle_time_reset succeeds", + _tx_execution_idle_time_reset(), TX_SUCCESS); + _tx_execution_idle_time_get(&val); + test_expect64("idle time reset to 0", val, 0ULL); + + test_expect("interrupt disable depth balanced to 0", int_disable_depth, 0UL); + test_expect("interrupt restore underflows == 0", int_underflows, 0UL); + + if (test_failures == 0U) + { + printf("SUCCESS! %lu expectations\n", (unsigned long) test_checks); + return(0); + } + + printf("ERROR: %lu expectation(s) failed of %lu\n", + (unsigned long) test_failures, (unsigned long) test_checks); + return(1); +} diff --git a/test/tx/execution_profile/tx_user.h b/test/tx/execution_profile/tx_user.h new file mode 100644 index 000000000..ac031e2f7 --- /dev/null +++ b/test/tx/execution_profile/tx_user.h @@ -0,0 +1,20 @@ +/*************************************************************************** + * Copyright (c) 2026-present 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef TX_USER_H +#define TX_USER_H + +unsigned long test_epk_clock_read(void); + +#define TX_EXECUTION_PROFILE_ENABLE +#define TX_EXECUTION_TIME_SOURCE ((EXECUTION_TIME_SOURCE_TYPE) test_epk_clock_read()) +#define TX_EXECUTION_MAX_TIME_SOURCE 0xFFFFFFFFUL + +#endif diff --git a/test/tx/low_power/threadx_low_power_test.c b/test/tx/low_power/threadx_low_power_test.c new file mode 100644 index 000000000..eebe98880 --- /dev/null +++ b/test/tx/low_power/threadx_low_power_test.c @@ -0,0 +1,261 @@ +/*************************************************************************** + * Copyright (c) 2026-present 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include + +#include "tx_api.h" +#include "tx_timer.h" +#include "tx_low_power.h" + +/* Timer wheel and state globals required by utility/low_power/tx_low_power.c. */ +TX_TIMER_INTERNAL *_tx_timer_list[TX_TIMER_ENTRIES]; +TX_TIMER_INTERNAL **_tx_timer_list_start; +TX_TIMER_INTERNAL **_tx_timer_list_end; +TX_TIMER_INTERNAL **_tx_timer_current_ptr; +volatile ULONG _tx_timer_system_clock; +ULONG _tx_timer_time_slice; +UINT _tx_timer_expired; +UINT _tx_timer_expired_time_slice; +volatile UINT _tx_thread_preempt_disable; + +/* Hook and interrupt-lock accounting. */ +static ULONG test_checks; +static UINT test_failures; +static UINT int_disable_depth; +static UINT int_underflows; + +static ULONG setup_calls; +static ULONG last_setup_expiration; +static ULONG user_enter_calls; +static ULONG user_exit_calls; +static ULONG adjust_calls; +static ULONG configured_adjust_ticks; +static ULONG expired_callbacks; + +/* Stand-in interrupt control routines matching Linux port prototypes. */ +UINT _tx_thread_interrupt_disable(VOID) +{ +UINT prev = int_disable_depth; + + int_disable_depth++; + return(prev); +} + +VOID _tx_thread_interrupt_restore(UINT previous_posture) +{ + if (int_disable_depth == 0U) + { + int_underflows++; + } + else + { + int_disable_depth = previous_posture; + } +} + +void test_low_power_timer_setup(unsigned long next_expiration) +{ + setup_calls++; + last_setup_expiration = (ULONG) next_expiration; +} + +void test_low_power_user_enter(void) +{ + user_enter_calls++; +} + +void test_low_power_user_exit(void) +{ + user_exit_calls++; +} + +unsigned long test_low_power_user_timer_adjust(void) +{ + adjust_calls++; + return((unsigned long) configured_adjust_ticks); +} + +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++; + } +} + +static VOID insert_internal_timer(TX_TIMER_INTERNAL *t, TX_TIMER_INTERNAL **slot) +{ + if (*slot == TX_NULL) + { + *slot = t; + t -> tx_timer_internal_active_next = t; + t -> tx_timer_internal_active_previous = t; + } + else + { + TX_TIMER_INTERNAL *head = *slot; + t -> tx_timer_internal_active_next = head; + t -> tx_timer_internal_active_previous = head -> tx_timer_internal_active_previous; + head -> tx_timer_internal_active_previous -> tx_timer_internal_active_next = t; + head -> tx_timer_internal_active_previous = t; + } + t -> tx_timer_internal_list_head = slot; +} + +VOID _tx_timer_system_activate(TX_TIMER_INTERNAL *timer_ptr) +{ +ULONG ticks = timer_ptr -> tx_timer_internal_remaining_ticks; +ULONG offset; + + if (ticks == 0UL) + { + return; + } + if (ticks <= TX_TIMER_ENTRIES) + { + offset = (ULONG) ((_tx_timer_current_ptr - _tx_timer_list_start) + (ticks - 1UL)) % TX_TIMER_ENTRIES; + } + else + { + offset = (ULONG) ((_tx_timer_current_ptr - _tx_timer_list_start) + (TX_TIMER_ENTRIES - 1UL)) % TX_TIMER_ENTRIES; + if (offset == (ULONG) (_tx_timer_current_ptr - _tx_timer_list_start)) + { + offset = (offset + TX_TIMER_ENTRIES - 1UL) % TX_TIMER_ENTRIES; + } + } + insert_internal_timer(timer_ptr, &_tx_timer_list_start[offset]); +} + +static VOID reset_timer_wheel(VOID) +{ + memset((VOID *) _tx_timer_list, 0, sizeof(_tx_timer_list)); + _tx_timer_list_start = &_tx_timer_list[0]; + _tx_timer_list_end = &_tx_timer_list[TX_TIMER_ENTRIES]; + _tx_timer_current_ptr = _tx_timer_list_start; + _tx_timer_system_clock = 0UL; + _tx_timer_time_slice = 0UL; + _tx_timer_expired = TX_FALSE; + _tx_timer_expired_time_slice = TX_FALSE; + setup_calls = 0UL; + last_setup_expiration = 0UL; + user_enter_calls = 0UL; + user_exit_calls = 0UL; + adjust_calls = 0UL; + configured_adjust_ticks = 0UL; + expired_callbacks = 0UL; +} + +static VOID timer_timeout_cb(ULONG id) +{ + (VOID) id; + expired_callbacks++; +} + +int main(void) +{ +ULONG next_ticks = 999UL; +TX_TIMER_INTERNAL t_short; +TX_TIMER_INTERNAL t_second; +TX_TIMER_INTERNAL t_long; + + reset_timer_wheel(); + + /* 1. Empty timer wheel and no time slice. */ + test_expect("empty wheel tx_timer_get_next returns TX_FALSE", + (ULONG) tx_timer_get_next(&next_ticks), TX_FALSE); + + /* Calling tx_low_power_exit before tx_low_power_enter is a safe no-op. */ + tx_low_power_exit(); + test_expect("exit without enter does not call user exit", user_exit_calls, 0UL); + test_expect("exit without enter does not adjust clock", adjust_calls, 0UL); + + /* Entering low power on empty wheel sets sentinel 0xFFFFFFFF. */ + tx_low_power_enter(); + test_expect("enter on empty wheel calls timer setup once", setup_calls, 1UL); + test_expect("enter on empty wheel passes 0xFFFFFFFF", last_setup_expiration, 0xFFFFFFFFUL); + test_expect("enter calls user_enter hook", user_enter_calls, 1UL); + + configured_adjust_ticks = 4UL; + tx_low_power_exit(); + test_expect("exit calls adjust and user_exit", + (ULONG) ((adjust_calls == 1UL) && (user_exit_calls == 1UL)), 1UL); + test_expect("system clock advanced by 4 ticks", _tx_timer_system_clock, 4UL); + + /* 2. Time slice decrements cleanly in tx_time_increment without wrapping. */ + reset_timer_wheel(); + _tx_timer_time_slice = 5UL; + test_expect("empty wheel with time slice still returns TX_FALSE from tx_timer_get_next", + (ULONG) tx_timer_get_next(&next_ticks), TX_FALSE); + + /* Time slice > time_increment decrements cleanly without expiring. */ + tx_time_increment(3UL); + test_expect("time slice decremented from 5 to 2", _tx_timer_time_slice, 2UL); + test_expect("time slice <= time_increment sets time slice to 1", + (tx_time_increment(3UL), _tx_timer_time_slice), 1UL); + + /* 3. Short & multi-slot timers: find nearest slot across wheel + expire cleanly. */ + reset_timer_wheel(); + memset((VOID *) &t_short, 0, sizeof(t_short)); + t_short.tx_timer_internal_remaining_ticks = 5UL; + t_short.tx_timer_internal_re_initialize_ticks = 0UL; + t_short.tx_timer_internal_timeout_function = timer_timeout_cb; + _tx_timer_system_activate(&t_short); + + memset((VOID *) &t_second, 0, sizeof(t_second)); + t_second.tx_timer_internal_remaining_ticks = 12UL; + t_second.tx_timer_internal_re_initialize_ticks = 0UL; + t_second.tx_timer_internal_timeout_function = timer_timeout_cb; + _tx_timer_system_activate(&t_second); + + test_expect("tx_timer_get_next sees earliest active timer", + (ULONG) tx_timer_get_next(&next_ticks), TX_TRUE); + test_expect("nearest active timer is <= 5 ticks", + (ULONG) ((next_ticks > 0UL) && (next_ticks <= 5UL)), 1UL); + + /* Advance 3 ticks via enter/exit (t_short shifted, neither expired yet). */ + tx_low_power_enter(); + test_expect("timer setup got nearest tick count", + (ULONG) ((last_setup_expiration > 0UL) && (last_setup_expiration <= 5UL)), 1UL); + configured_adjust_ticks = 3UL; + tx_low_power_exit(); + test_expect("clock advanced by 3 ticks", _tx_timer_system_clock, 3UL); + test_expect("t_short still armed on wheel", (ULONG) (t_short.tx_timer_internal_list_head != TX_NULL), 1UL); + + /* 4. Long timer (> TX_TIMER_ENTRIES) stays armed after partial increment > 0. */ + reset_timer_wheel(); + memset((VOID *) &t_long, 0, sizeof(t_long)); + t_long.tx_timer_internal_remaining_ticks = (ULONG) (TX_TIMER_ENTRIES + 10U); + t_long.tx_timer_internal_re_initialize_ticks = 0UL; + t_long.tx_timer_internal_timeout_function = timer_timeout_cb; + _tx_timer_system_activate(&t_long); + + tx_time_increment(5UL); + test_expect("long timer still active after 5 tick advance", + (ULONG) (t_long.tx_timer_internal_list_head != TX_NULL), 1UL); + test_expect("system clock at 5 after long timer advance", _tx_timer_system_clock, 5UL); + + /* Interrupt balance verification. */ + test_expect("interrupt disable depth balanced to 0", int_disable_depth, 0UL); + test_expect("interrupt restore underflows == 0", int_underflows, 0UL); + + if (test_failures == 0U) + { + printf("SUCCESS! %lu expectations\n", (unsigned long) test_checks); + return(0); + } + + printf("ERROR: %lu expectation(s) failed of %lu\n", + (unsigned long) test_failures, (unsigned long) test_checks); + return(1); +} diff --git a/test/tx/low_power/tx_user.h b/test/tx/low_power/tx_user.h new file mode 100644 index 000000000..ca2764d4c --- /dev/null +++ b/test/tx/low_power/tx_user.h @@ -0,0 +1,24 @@ +/*************************************************************************** + * Copyright (c) 2026-present 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef TX_USER_H +#define TX_USER_H + +void test_low_power_timer_setup(unsigned long next_expiration); +void test_low_power_user_enter(void); +void test_low_power_user_exit(void); +unsigned long test_low_power_user_timer_adjust(void); + +#define TX_LOW_POWER_TIMER_SETUP(next_expiration) test_low_power_timer_setup((unsigned long) (next_expiration)) +#define TX_LOW_POWER_USER_ENTER test_low_power_user_enter() +#define TX_LOW_POWER_USER_EXIT test_low_power_user_exit() +#define TX_LOW_POWER_USER_TIMER_ADJUST test_low_power_user_timer_adjust() + +#endif diff --git a/test/tx/module_manager/threadx_module_manager_callback_trampolines_test.c b/test/tx/module_manager/threadx_module_manager_callback_trampolines_test.c new file mode 100644 index 000000000..180dc6860 --- /dev/null +++ b/test/tx/module_manager/threadx_module_manager_callback_trampolines_test.c @@ -0,0 +1,495 @@ +/*************************************************************************** + * Copyright (c) 2026-present 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include + +#include "threadx_module_manager_host_test_port.h" + +#include "tx_api.h" +#include "tx_initialize.h" +#include "tx_thread.h" +#include "tx_timer.h" +#include "tx_queue.h" +#include "tx_semaphore.h" +#include "tx_mutex.h" +#include "tx_event_flags.h" +#include "tx_block_pool.h" +#include "tx_byte_pool.h" +#include "txm_module.h" +#include "txm_module_manager_util.h" + +#ifndef TXM_MODULE_OBJECT_POINTER_GET_CALL_NOT_USED +#error "Expected unused dispatchers to be compiled out for targeted link" +#endif + +/* Interrupt lock accounting expected by the host port shim. */ +unsigned int test_interrupt_disable_depth; +unsigned int test_interrupt_disable_max_depth; +unsigned int test_interrupt_restore_underflows; + +unsigned int test_get_ipsr_value(void) +{ + return(0U); +} + +/* Kernel object created lists required by txm_module_manager_util.c. */ +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; + +/* Module manager state globals required by callback and util sources. */ +volatile ULONG _tx_thread_system_state; +TX_THREAD *_tx_thread_current_ptr; +TX_TIMER_INTERNAL *_tx_timer_expired_timer_ptr; +TX_BYTE_POOL _txm_module_manager_object_pool; +UINT _txm_module_manager_object_pool_created; +TX_MUTEX _txm_module_manager_mutex; +TXM_MODULE_INSTANCE *_txm_module_manager_loaded_list_ptr; +ULONG _txm_module_manger_loaded_count; +UINT _txm_module_manager_ready; +ULONG _txm_module_manager_properties_supported; +ULONG _txm_module_manager_properties_required; +ULONG _txm_module_manager_callback_total_count; +ULONG _txm_module_manager_callback_error_count; +TX_NAME_CONST CHAR _txm_module_manager_callback_request_queue_name[] = "Module Callback Request Queue"; + +/* Declarations for internal trampoline functions under test. */ +VOID _txm_module_manager_queue_notify_trampoline(TX_QUEUE *queue_ptr); +VOID _txm_module_manager_semaphore_notify_trampoline(TX_SEMAPHORE *semaphore_ptr); +VOID _txm_module_manager_event_flags_notify_trampoline(TX_EVENT_FLAGS_GROUP *group_ptr); +VOID _txm_module_manager_thread_notify_trampoline(TX_THREAD *thread_ptr, UINT type); +VOID _txm_module_manager_timer_notify_trampoline(ULONG id); + +static UINT test_failures; +static ULONG test_checks; + +VOID _txm_module_manager_alignment_adjust(TXM_MODULE_PREAMBLE *module_preamble, ULONG *code_size, + ULONG *code_alignment, ULONG *data_size, ULONG *data_alignment) +{ + (VOID) module_preamble; + (VOID) code_size; + (VOID) code_alignment; + (VOID) data_size; + (VOID) data_alignment; +} + +UINT _txm_module_manager_application_request(ULONG request_id, ALIGN_TYPE param_1, ALIGN_TYPE param_2, ALIGN_TYPE param_3) +{ + (VOID) request_id; + (VOID) param_1; + (VOID) param_2; + (VOID) param_3; + return(TX_NOT_AVAILABLE); +} + +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++; + } +} + +/* Mocked kernel service call tracking for notify dispatchers. */ +static ULONG stub_queue_notify_calls; +static VOID (*stub_registered_queue_notify)(TX_QUEUE *); +static ULONG stub_sem_notify_calls; +static VOID (*stub_registered_sem_notify)(TX_SEMAPHORE *); +static ULONG stub_ev_notify_calls; +static VOID (*stub_registered_ev_notify)(TX_EVENT_FLAGS_GROUP *); +static ULONG stub_thread_notify_calls; +static VOID (*stub_registered_thread_notify)(TX_THREAD *, UINT); + +UINT _txe_queue_send_notify(TX_QUEUE *queue_ptr, VOID (*queue_send_notify)(TX_QUEUE *)) +{ + stub_queue_notify_calls++; + stub_registered_queue_notify = queue_send_notify; + if (queue_ptr == TX_NULL) + { + return(TX_QUEUE_ERROR); + } + return(TX_SUCCESS); +} + +UINT _txe_semaphore_put_notify(TX_SEMAPHORE *semaphore_ptr, VOID (*semaphore_put_notify)(TX_SEMAPHORE *)) +{ + stub_sem_notify_calls++; + stub_registered_sem_notify = semaphore_put_notify; + if (semaphore_ptr == TX_NULL) + { + return(TX_SEMAPHORE_ERROR); + } + return(TX_SUCCESS); +} + +UINT _txe_event_flags_set_notify(TX_EVENT_FLAGS_GROUP *group_ptr, VOID (*events_set_notify)(TX_EVENT_FLAGS_GROUP *)) +{ + stub_ev_notify_calls++; + stub_registered_ev_notify = events_set_notify; + if (group_ptr == TX_NULL) + { + return(TX_GROUP_ERROR); + } + return(TX_SUCCESS); +} + +UINT _txe_thread_entry_exit_notify(TX_THREAD *thread_ptr, VOID (*thread_entry_exit_notify)(TX_THREAD *, UINT)) +{ + stub_thread_notify_calls++; + stub_registered_thread_notify = thread_entry_exit_notify; + if (thread_ptr == TX_NULL) + { + return(TX_THREAD_ERROR); + } + return(TX_SUCCESS); +} + +UINT _txe_mutex_get(TX_MUTEX *mutex_ptr, ULONG wait_option) +{ + (VOID) mutex_ptr; + (VOID) wait_option; + return(TX_SUCCESS); +} + +UINT _txe_mutex_put(TX_MUTEX *mutex_ptr) +{ + (VOID) mutex_ptr; + return(TX_SUCCESS); +} + +/* Ring-buffer stand-in for _tx_queue_send matching ThreadX ULONG queue layout. */ +UINT _tx_queue_send(TX_QUEUE *queue_ptr, VOID *source_ptr, ULONG wait_option) +{ +ULONG msg_words; +ULONG *src; +ULONG i; + + (VOID) wait_option; + + if ((queue_ptr == TX_NULL) || (queue_ptr -> tx_queue_id != TX_QUEUE_ID)) + { + return(TX_QUEUE_ERROR); + } + if (queue_ptr -> tx_queue_available_storage == ((ULONG) 0)) + { + return(TX_QUEUE_FULL); + } + + msg_words = (ULONG) queue_ptr -> tx_queue_message_size; + src = (ULONG *) source_ptr; + for (i = 0; i < msg_words; i++) + { + queue_ptr -> tx_queue_write[i] = src[i]; + } + + queue_ptr -> tx_queue_write += msg_words; + if (queue_ptr -> tx_queue_write >= queue_ptr -> tx_queue_end) + { + queue_ptr -> tx_queue_write = queue_ptr -> tx_queue_start; + } + queue_ptr -> tx_queue_enqueued++; + queue_ptr -> tx_queue_available_storage--; + return(TX_SUCCESS); +} + +static VOID init_callback_queue(TXM_MODULE_INSTANCE *mod) +{ +TX_QUEUE *q = &(mod -> txm_module_instance_callback_request_queue); +ULONG msg_words = (ULONG) (sizeof(TXM_MODULE_CALLBACK_MESSAGE) / sizeof(ULONG)); + + memset((VOID *) q, 0, sizeof(TX_QUEUE)); + q -> tx_queue_id = TX_QUEUE_ID; + q -> tx_queue_name = _txm_module_manager_callback_request_queue_name; + q -> tx_queue_message_size = (UINT) msg_words; + q -> tx_queue_capacity = TXM_MODULE_CALLBACKS_QUEUE_DEPTH; + q -> tx_queue_available_storage = TXM_MODULE_CALLBACKS_QUEUE_DEPTH; + q -> tx_queue_enqueued = 0; + q -> tx_queue_start = &(mod -> txm_module_instance_callback_request_queue_area[0]); + q -> tx_queue_end = q -> tx_queue_start + (TXM_MODULE_CALLBACKS_QUEUE_DEPTH * msg_words); + q -> tx_queue_read = q -> tx_queue_start; + q -> tx_queue_write = q -> tx_queue_start; +} + +static VOID pop_callback_message(TX_QUEUE *q, TXM_MODULE_CALLBACK_MESSAGE *out) +{ +ULONG msg_words = (ULONG) q -> tx_queue_message_size; + + memcpy((VOID *) out, (VOID *) q -> tx_queue_read, sizeof(TXM_MODULE_CALLBACK_MESSAGE)); + q -> tx_queue_read += msg_words; + if (q -> tx_queue_read >= q -> tx_queue_end) + { + q -> tx_queue_read = q -> tx_queue_start; + } + q -> tx_queue_enqueued--; + q -> tx_queue_available_storage++; +} + +/* Module objects and callbacks. */ +static UCHAR mod_data_buf[256]; +static UCHAR mod_code_buf[256]; +static TXM_MODULE_INSTANCE mod_a; +static TX_THREAD calling_thread; +static TX_QUEUE app_queue; +static TX_SEMAPHORE app_sem; +static TX_EVENT_FLAGS_GROUP app_ev; +static TX_THREAD app_thread; +static TXM_MODULE_THREAD_ENTRY_INFO app_thread_info; +static TX_TIMER app_timer; + +static VOID dummy_app_fn(VOID) +{ +} + +static VOID dummy_thread_exit_cb(TX_THREAD *t, UINT type) +{ + (VOID) t; + (VOID) type; +} + +static VOID dummy_timer_cb(ULONG id) +{ + (VOID) id; +} + +static VOID setup_module(TXM_MODULE_INSTANCE *m, ULONG state, ULONG props) +{ + memset((VOID *) m, 0, sizeof(TXM_MODULE_INSTANCE)); + m -> txm_module_instance_id = TXM_MODULE_ID; + m -> txm_module_instance_state = state; + m -> txm_module_instance_property_flags = props; + m -> txm_module_instance_data_start = (VOID *) mod_data_buf; + m -> txm_module_instance_data_end = (VOID *) (mod_data_buf + sizeof(mod_data_buf) - 1U); + m -> txm_module_instance_code_start = (VOID *) mod_code_buf; + m -> txm_module_instance_code_end = (VOID *) (mod_code_buf + sizeof(mod_code_buf) - 1U); + init_callback_queue(m); +} + +#define REGISTER_OBJ(ptr, next_f, head, cnt) \ + do { \ + (ptr) -> next_f = (head); \ + (head) = (ptr); \ + (cnt)++; \ + } while (0) + +int main(void) +{ +TXM_MODULE_CALLBACK_MESSAGE msg; +TXM_MODULE_CALLBACK_MESSAGE popped; +TX_QUEUE bad_queue; +ULONG i; +ALIGN_TYPE ret; + + _tx_thread_system_state = 0; + memset((VOID *) &calling_thread, 0, sizeof(calling_thread)); + _tx_thread_current_ptr = &calling_thread; + + setup_module(&mod_a, TXM_MODULE_STARTED, (TXM_MODULE_USER_MODE | TXM_MODULE_MEMORY_PROTECTION)); + calling_thread.tx_thread_module_instance_ptr = &mod_a; + + /* Prepare valid kernel objects and add to created lists. */ + memset((VOID *) &app_queue, 0, sizeof(app_queue)); + app_queue.tx_queue_id = TX_QUEUE_ID; + REGISTER_OBJ(&app_queue, tx_queue_created_next, _tx_queue_created_ptr, _tx_queue_created_count); + + memset((VOID *) &app_sem, 0, sizeof(app_sem)); + app_sem.tx_semaphore_id = TX_SEMAPHORE_ID; + REGISTER_OBJ(&app_sem, tx_semaphore_created_next, _tx_semaphore_created_ptr, _tx_semaphore_created_count); + + memset((VOID *) &app_ev, 0, sizeof(app_ev)); + app_ev.tx_event_flags_group_id = TX_EVENT_FLAGS_ID; + REGISTER_OBJ(&app_ev, tx_event_flags_group_created_next, _tx_event_flags_created_ptr, _tx_event_flags_created_count); + + memset((VOID *) &app_thread, 0, sizeof(app_thread)); + app_thread.tx_thread_id = TX_THREAD_ID; + app_thread.tx_thread_module_instance_ptr = (VOID *) &mod_a; + memset((VOID *) &app_thread_info, 0, sizeof(app_thread_info)); + app_thread_info.txm_module_thread_entry_info_exit_notify = dummy_thread_exit_cb; + app_thread.tx_thread_module_entry_info_ptr = (VOID *) &app_thread_info; + REGISTER_OBJ(&app_thread, tx_thread_created_next, _tx_thread_created_ptr, _tx_thread_created_count); + + memset((VOID *) &app_timer, 0, sizeof(app_timer)); + app_timer.tx_timer_id = TX_TIMER_ID; + app_timer.tx_timer_module_instance = (VOID *) &mod_a; + app_timer.tx_timer_module_expiration_function = dummy_timer_cb; + app_timer.tx_timer_internal.tx_timer_internal_timeout_param = 0xCAFEUL; + REGISTER_OBJ(&app_timer, tx_timer_created_next, _tx_timer_created_ptr, _tx_timer_created_count); + + /* 1. Direct _txm_module_manager_callback_request tests. */ + memset((VOID *) &msg, 0, sizeof(msg)); + msg.txm_module_callback_message_type = TXM_QUEUE_SEND_CALLBACK; + msg.txm_module_callback_message_application_function = dummy_app_fn; + msg.txm_module_callback_message_param_1 = (ALIGN_TYPE) &app_queue; + + memset((VOID *) &bad_queue, 0, sizeof(bad_queue)); + _txm_module_manager_callback_request(&bad_queue, &msg); + test_expect("callback error count incremented on invalid queue", + _txm_module_manager_callback_error_count, 1UL); + + _txm_module_manager_callback_request(&(mod_a.txm_module_instance_callback_request_queue), &msg); + test_expect("callback total count becomes 1", _txm_module_manager_callback_total_count, 1UL); + test_expect("queue holds 1 enqueued message", + mod_a.txm_module_instance_callback_request_queue.tx_queue_enqueued, 1UL); + + /* Duplicate message while still queued increments activation_count without growing queue. */ + _txm_module_manager_callback_request(&(mod_a.txm_module_instance_callback_request_queue), &msg); + test_expect("queue still holds only 1 message after dedup", + mod_a.txm_module_instance_callback_request_queue.tx_queue_enqueued, 1UL); + pop_callback_message(&(mod_a.txm_module_instance_callback_request_queue), &popped); + test_expect("deduplicated activation_count is 1", popped.txm_module_callback_message_activation_count, 1UL); + + /* Force wrap-around by advancing read/write past middle and filling to capacity. */ + for (i = 0; i < TXM_MODULE_CALLBACKS_QUEUE_DEPTH; i++) + { + msg.txm_module_callback_message_param_2 = (ALIGN_TYPE) (i + 1U); + _txm_module_manager_callback_request(&(mod_a.txm_module_instance_callback_request_queue), &msg); + } + test_expect("callback queue filled to capacity", + mod_a.txm_module_instance_callback_request_queue.tx_queue_enqueued, + (ULONG) TXM_MODULE_CALLBACKS_QUEUE_DEPTH); + + /* Now enqueued == TXM_MODULE_CALLBACKS_QUEUE_DEPTH; test dedup after wrap and full error. */ + msg.txm_module_callback_message_param_2 = (ALIGN_TYPE) TXM_MODULE_CALLBACKS_QUEUE_DEPTH; + _txm_module_manager_callback_request(&(mod_a.txm_module_instance_callback_request_queue), &msg); + test_expect("dedup keeps queue depth unchanged on wrapped full ring", + mod_a.txm_module_instance_callback_request_queue.tx_queue_enqueued, + (ULONG) TXM_MODULE_CALLBACKS_QUEUE_DEPTH); + test_expect("callback error count still 1 after dedup", + _txm_module_manager_callback_error_count, 1UL); + + msg.txm_module_callback_message_param_2 = (ALIGN_TYPE) 9999U; + _txm_module_manager_callback_request(&(mod_a.txm_module_instance_callback_request_queue), &msg); + test_expect("callback error count incremented on full queue", + _txm_module_manager_callback_error_count, 2UL); + + while (mod_a.txm_module_instance_callback_request_queue.tx_queue_enqueued > 0UL) + { + pop_callback_message(&(mod_a.txm_module_instance_callback_request_queue), &popped); + } + + /* 2. Dispatcher notify registration & clear + trampolines across all 4 notify dispatchers + timer trampoline. */ + ret = _txm_module_manager_kernel_dispatch(TXM_QUEUE_SEND_NOTIFY_CALL, (ALIGN_TYPE) &app_queue, (ALIGN_TYPE) dummy_app_fn, 0); + test_expect("queue_send_notify dispatch succeeds", (ULONG) ret, TX_SUCCESS); + test_expect("queue_send_notify installs trampoline", (ALIGN_TYPE) stub_registered_queue_notify == (ALIGN_TYPE) _txm_module_manager_queue_notify_trampoline, 1UL); + + ret = _txm_module_manager_kernel_dispatch(TXM_SEMAPHORE_PUT_NOTIFY_CALL, (ALIGN_TYPE) &app_sem, (ALIGN_TYPE) dummy_app_fn, 0); + test_expect("semaphore_put_notify dispatch succeeds", (ULONG) ret, TX_SUCCESS); + test_expect("semaphore_put_notify installs trampoline", (ALIGN_TYPE) stub_registered_sem_notify == (ALIGN_TYPE) _txm_module_manager_semaphore_notify_trampoline, 1UL); + + ret = _txm_module_manager_kernel_dispatch(TXM_EVENT_FLAGS_SET_NOTIFY_CALL, (ALIGN_TYPE) &app_ev, (ALIGN_TYPE) dummy_app_fn, 0); + test_expect("event_flags_set_notify dispatch succeeds", (ULONG) ret, TX_SUCCESS); + test_expect("event_flags_set_notify installs trampoline", (ALIGN_TYPE) stub_registered_ev_notify == (ALIGN_TYPE) _txm_module_manager_event_flags_notify_trampoline, 1UL); + + ret = _txm_module_manager_kernel_dispatch(TXM_THREAD_ENTRY_EXIT_NOTIFY_CALL, (ALIGN_TYPE) &app_thread, (ALIGN_TYPE) dummy_app_fn, 0); + test_expect("thread_entry_exit_notify dispatch succeeds", (ULONG) ret, TX_SUCCESS); + test_expect("thread_entry_exit_notify installs trampoline", (ALIGN_TYPE) stub_registered_thread_notify == (ALIGN_TYPE) _txm_module_manager_thread_notify_trampoline, 1UL); + + /* Configure timer fields directly as timer create dispatcher does. */ + app_timer.tx_timer_module_instance = (VOID *) &mod_a; + app_timer.tx_timer_module_expiration_function = (VOID (*)(ULONG)) dummy_app_fn; + app_timer.tx_timer_internal.tx_timer_internal_timeout_param = 0xCAFEUL; + _tx_timer_expired_timer_ptr = &(app_timer.tx_timer_internal); + + /* Fire all 5 trampolines while module is STARTED. */ + _txm_module_manager_queue_notify_trampoline(&app_queue); + _txm_module_manager_semaphore_notify_trampoline(&app_sem); + _txm_module_manager_event_flags_notify_trampoline(&app_ev); + _txm_module_manager_thread_notify_trampoline(&app_thread, TX_THREAD_EXIT); + _txm_module_manager_timer_notify_trampoline(0xCAFEUL); + test_expect("all 5 trampolines enqueued when module STARTED", + mod_a.txm_module_instance_callback_request_queue.tx_queue_enqueued, 5UL); + + pop_callback_message(&(mod_a.txm_module_instance_callback_request_queue), &popped); + test_expect("msg 1 type is TXM_QUEUE_SEND_CALLBACK", popped.txm_module_callback_message_type, TXM_QUEUE_SEND_CALLBACK); + pop_callback_message(&(mod_a.txm_module_instance_callback_request_queue), &popped); + test_expect("msg 2 type is TXM_SEMAPHORE_PUT_CALLBACK", popped.txm_module_callback_message_type, TXM_SEMAPHORE_PUT_CALLBACK); + pop_callback_message(&(mod_a.txm_module_instance_callback_request_queue), &popped); + test_expect("msg 3 type is TXM_EVENTS_SET_CALLBACK", popped.txm_module_callback_message_type, TXM_EVENTS_SET_CALLBACK); + pop_callback_message(&(mod_a.txm_module_instance_callback_request_queue), &popped); + test_expect("msg 4 type is TXM_THREAD_ENTRY_EXIT_CALLBACK", popped.txm_module_callback_message_type, TXM_THREAD_ENTRY_EXIT_CALLBACK); + test_expect("thread exit notify preserves exit type param", (ULONG) popped.txm_module_callback_message_param_2, TX_THREAD_EXIT); + pop_callback_message(&(mod_a.txm_module_instance_callback_request_queue), &popped); + test_expect("msg 5 type is TXM_TIMER_CALLBACK", popped.txm_module_callback_message_type, TXM_TIMER_CALLBACK); + test_expect("timer callback preserves timeout param", (ULONG) popped.txm_module_callback_message_param_1, 0xCAFEUL); + + /* Stopped module must reject all 5 trampolines without enqueueing and increment error_count. */ + mod_a.txm_module_instance_state = TXM_MODULE_STOPPED; + _txm_module_manager_queue_notify_trampoline(&app_queue); + _txm_module_manager_semaphore_notify_trampoline(&app_sem); + _txm_module_manager_event_flags_notify_trampoline(&app_ev); + _txm_module_manager_thread_notify_trampoline(&app_thread, TX_THREAD_ENTRY); + _txm_module_manager_timer_notify_trampoline(0xCAFEUL); + test_expect("stopped module enqueues 0 trampoline messages", + mod_a.txm_module_instance_callback_request_queue.tx_queue_enqueued, 0UL); + test_expect("error count increments 5 times for stopped module", + _txm_module_manager_callback_error_count, 7UL); + + /* Invalid module instance (cleared ID) and NULL module must also fail safely. */ + mod_a.txm_module_instance_id = 0UL; + mod_a.txm_module_instance_state = TXM_MODULE_STARTED; + _txm_module_manager_queue_notify_trampoline(&app_queue); + _txm_module_manager_semaphore_notify_trampoline(&app_sem); + _txm_module_manager_event_flags_notify_trampoline(&app_ev); + _txm_module_manager_thread_notify_trampoline(&app_thread, TX_THREAD_ENTRY); + _txm_module_manager_timer_notify_trampoline(0xCAFEUL); + test_expect("invalid module ID enqueues 0 messages", + mod_a.txm_module_instance_callback_request_queue.tx_queue_enqueued, 0UL); + test_expect("error count increments 5 times for invalid module ID", + _txm_module_manager_callback_error_count, 12UL); + + /* Clearing notifications with param_1 == TX_NULL clears the registered function. */ + mod_a.txm_module_instance_id = TXM_MODULE_ID; + test_expect("queue notify clear succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_QUEUE_SEND_NOTIFY_CALL, (ALIGN_TYPE) &app_queue, 0, 0), TX_SUCCESS); + test_expect("queue notify cleared to NULL", (ULONG) (stub_registered_queue_notify == TX_NULL), 1UL); + test_expect("sem notify clear succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_SEMAPHORE_PUT_NOTIFY_CALL, (ALIGN_TYPE) &app_sem, 0, 0), TX_SUCCESS); + test_expect("sem notify cleared to NULL", (ULONG) (stub_registered_sem_notify == TX_NULL), 1UL); + test_expect("event flags notify clear succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_EVENT_FLAGS_SET_NOTIFY_CALL, (ALIGN_TYPE) &app_ev, 0, 0), TX_SUCCESS); + test_expect("event flags notify cleared to NULL", (ULONG) (stub_registered_ev_notify == TX_NULL), 1UL); + test_expect("thread notify clear succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_THREAD_ENTRY_EXIT_NOTIFY_CALL, (ALIGN_TYPE) &app_thread, 0, 0), TX_SUCCESS); + test_expect("thread notify cleared to NULL", (ULONG) (stub_registered_thread_notify == TX_NULL), 1UL); + + /* Invalid dispatch call ID must return TX_NOT_AVAILABLE. */ + test_expect("unknown kernel dispatch ID fails cleanly", + (ULONG) _txm_module_manager_kernel_dispatch(0x9999UL, 0, 0, 0), (ULONG) TX_NOT_AVAILABLE); + + /* Interrupt balance verification. */ + test_expect("interrupt disable depth balanced to 0", (ULONG) test_interrupt_disable_depth, 0UL); + test_expect("interrupt restore underflows == 0", (ULONG) test_interrupt_restore_underflows, 0UL); + + if (test_failures == 0U) + { + printf("SUCCESS! %lu expectations\n", (unsigned long) test_checks); + return(0); + } + + printf("ERROR: %lu expectation(s) failed of %lu\n", + (unsigned long) test_failures, (unsigned long) test_checks); + return(1); +} diff --git a/test/tx/module_manager/threadx_module_manager_dispatch_security_test.c b/test/tx/module_manager/threadx_module_manager_dispatch_security_test.c new file mode 100644 index 000000000..9ffb19033 --- /dev/null +++ b/test/tx/module_manager/threadx_module_manager_dispatch_security_test.c @@ -0,0 +1,831 @@ +/*************************************************************************** + * Copyright (c) 2026-present 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include + +#include "threadx_module_manager_host_test_port.h" + +#include "tx_api.h" +#include "tx_initialize.h" +#include "tx_thread.h" +#include "tx_timer.h" +#include "tx_queue.h" +#include "tx_semaphore.h" +#include "tx_mutex.h" +#include "tx_event_flags.h" +#include "tx_block_pool.h" +#include "tx_byte_pool.h" +#include "txm_module.h" +#include "txm_module_manager_util.h" + +/* Interrupt lock accounting expected by the host port shim. */ +unsigned int test_interrupt_disable_depth; +unsigned int test_interrupt_disable_max_depth; +unsigned int test_interrupt_restore_underflows; + +unsigned int test_get_ipsr_value(void) +{ + return(0U); +} + +/* Kernel object created lists required by util and object_pointer_get_extended. */ +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; + +/* Module Manager bookkeeping required by dispatch/object helpers. */ +volatile ULONG _tx_thread_system_state; +volatile UINT _tx_thread_preempt_disable; +TX_THREAD *_tx_thread_current_ptr; +TX_TIMER_INTERNAL *_tx_timer_expired_timer_ptr; +TX_BYTE_POOL _txm_module_manager_object_pool; +UINT _txm_module_manager_object_pool_created; +TX_MUTEX _txm_module_manager_mutex; +TXM_MODULE_INSTANCE *_txm_module_manager_loaded_list_ptr; +ULONG _txm_module_manger_loaded_count; +UINT _txm_module_manager_ready; +ULONG _txm_module_manager_properties_supported; +ULONG _txm_module_manager_properties_required; +ULONG _txm_module_manager_callback_total_count; +ULONG _txm_module_manager_callback_error_count; +TX_NAME_CONST CHAR _txm_module_manager_callback_request_queue_name[] = "Module Callback Request Queue"; + +/* Backed object pool arena so allocated objects live inside the real object pool range. */ +#define SEC_POOL_BYTES 8192U +#define SEC_MAX_ALLOCS 32U + +typedef struct SEC_ALLOC_SLOT_STRUCT +{ + UCHAR *ptr; + ULONG size; + UINT used; +} SEC_ALLOC_SLOT; + +static union +{ + ALIGN_TYPE align; + UCHAR bytes[SEC_POOL_BYTES]; +} sec_pool_mem; + +static ULONG sec_pool_offset; +static SEC_ALLOC_SLOT sec_slots[SEC_MAX_ALLOCS]; + +static UINT test_failures; +static ULONG test_checks; +static ULONG stub_int_ctl_calls; +static ULONG stub_stack_err_calls; +static ULONG stub_trace_calls; +static ULONG stub_byte_release_calls; + +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++; + } +} + +VOID _txm_module_manager_alignment_adjust(TXM_MODULE_PREAMBLE *module_preamble, ULONG *code_size, + ULONG *code_alignment, ULONG *data_size, ULONG *data_alignment) +{ + (VOID) module_preamble; + (VOID) code_size; + (VOID) code_alignment; + (VOID) data_size; + (VOID) data_alignment; +} + +UINT _txm_module_manager_application_request(ULONG request_id, ALIGN_TYPE param_1, ALIGN_TYPE param_2, ALIGN_TYPE param_3) +{ + (VOID) request_id; + (VOID) param_1; + (VOID) param_2; + (VOID) param_3; + return(TX_NOT_AVAILABLE); +} + +VOID _txm_module_manager_callback_request(TX_QUEUE *module_callback_queue, TXM_MODULE_CALLBACK_MESSAGE *callback_request) +{ + (VOID) module_callback_queue; + (VOID) callback_request; +} + +VOID _tx_thread_system_preempt_check(VOID) +{ +} + +UINT _txe_mutex_get(TX_MUTEX *mutex_ptr, ULONG wait_option) +{ + (VOID) mutex_ptr; + (VOID) wait_option; + return(TX_SUCCESS); +} + +UINT _txe_mutex_put(TX_MUTEX *mutex_ptr) +{ + (VOID) mutex_ptr; + return(TX_SUCCESS); +} + +UINT _txe_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr, ULONG memory_size, ULONG wait_option) +{ +ULONG rounded = (memory_size + (ULONG)(sizeof(ALIGN_TYPE) - 1U)) & ~((ULONG)(sizeof(ALIGN_TYPE) - 1U)); +UCHAR *p; +UINT i; + + (VOID) wait_option; + if ((pool_ptr != &_txm_module_manager_object_pool) || (memory_ptr == TX_NULL)) + { + return(TX_PTR_ERROR); + } + if ((rounded == 0UL) || ((sec_pool_offset + rounded) > SEC_POOL_BYTES)) + { + return(TX_NO_MEMORY); + } + p = &sec_pool_mem.bytes[sec_pool_offset]; + sec_pool_offset += rounded; + for (i = 0; i < SEC_MAX_ALLOCS; i++) + { + if (!sec_slots[i].used) + { + sec_slots[i].ptr = p; + sec_slots[i].size = rounded; + sec_slots[i].used = 1U; + break; + } + } + *memory_ptr = (VOID *) p; + return(TX_SUCCESS); +} + +UINT _txe_byte_release(VOID *memory_ptr) +{ +UINT i; + + stub_byte_release_calls++; + for (i = 0; i < SEC_MAX_ALLOCS; i++) + { + if (sec_slots[i].used && (sec_slots[i].ptr == (UCHAR *) memory_ptr)) + { + sec_slots[i].used = 0U; + return(TX_SUCCESS); + } + } + /* Non-object-pool user pointer release passes through in TXM_BYTE_RELEASE_CALL test. */ + return(TX_SUCCESS); +} + +/* Kernel service stubs for dispatchers under test. */ +UINT _tx_thread_interrupt_control(UINT new_posture) +{ + stub_int_ctl_calls++; + return(new_posture); +} + +UINT _tx_thread_stack_error_notify(VOID (*stack_error_handler)(TX_THREAD *thread_ptr)) +{ + (VOID) stack_error_handler; + stub_stack_err_calls++; + return(TX_SUCCESS); +} + +UINT _tx_trace_enable(VOID *trace_buffer_start, ULONG trace_buffer_size, ULONG registry_entries) +{ + (VOID) trace_buffer_start; + (VOID) trace_buffer_size; + (VOID) registry_entries; + stub_trace_calls++; + return(TX_SUCCESS); +} + +UINT _tx_trace_event_filter(ULONG event_filter_bits) +{ + (VOID) event_filter_bits; + stub_trace_calls++; + return(TX_SUCCESS); +} + +UINT _tx_trace_event_unfilter(ULONG event_unfilter_bits) +{ + (VOID) event_unfilter_bits; + stub_trace_calls++; + return(TX_SUCCESS); +} + +UINT _tx_trace_disable(VOID) +{ + stub_trace_calls++; + return(TX_SUCCESS); +} + +UINT _tx_trace_interrupt_control(UINT new_posture) +{ + (VOID) new_posture; + stub_trace_calls++; + return(TX_SUCCESS); +} + +VOID _tx_trace_isr_enter_insert(ULONG isr_id) +{ + (VOID) isr_id; + stub_trace_calls++; +} + +VOID _tx_trace_isr_exit_insert(ULONG isr_id) +{ + (VOID) isr_id; + stub_trace_calls++; +} + +UINT _tx_trace_user_event_insert(ULONG event_id, ULONG info_1, ULONG info_2, ULONG info_3, ULONG info_4) +{ + (VOID) event_id; + (VOID) info_1; + (VOID) info_2; + (VOID) info_3; + (VOID) info_4; + stub_trace_calls++; + return(TX_SUCCESS); +} + +#define REGISTER_NODE(node, next_f, prev_f, head, cnt) \ + do { \ + if ((head) == TX_NULL) \ + { \ + (head) = (node); \ + (node) -> next_f = (node); \ + (node) -> prev_f = (node); \ + } \ + else \ + { \ + (node) -> next_f = (head); \ + (node) -> prev_f = (head) -> prev_f; \ + (head) -> prev_f -> next_f = (node); \ + (head) -> prev_f = (node); \ + } \ + (cnt)++; \ + } while (0) + +#define UNREGISTER_NODE(node, next_f, prev_f, head, cnt) \ + do { \ + if ((cnt) <= 1UL) \ + { \ + (head) = TX_NULL; \ + (cnt) = 0UL; \ + } \ + else \ + { \ + (node) -> next_f -> prev_f = (node) -> prev_f; \ + (node) -> prev_f -> next_f = (node) -> next_f; \ + if ((head) == (node)) (head) = (node) -> next_f; \ + (cnt)--; \ + } \ + } while (0) + +UINT _txe_byte_pool_create(TX_BYTE_POOL *pool_ptr, CHAR *name_ptr, VOID *pool_start, ULONG pool_size, UINT cb_size) +{ + (VOID) pool_start; + (VOID) pool_size; + (VOID) cb_size; + memset((VOID *) pool_ptr, 0, sizeof(TX_BYTE_POOL)); + pool_ptr -> tx_byte_pool_id = TX_BYTE_POOL_ID; + pool_ptr -> tx_byte_pool_name = name_ptr; + REGISTER_NODE(pool_ptr, tx_byte_pool_created_next, tx_byte_pool_created_previous, _tx_byte_pool_created_ptr, _tx_byte_pool_created_count); + return(TX_SUCCESS); +} + +UINT _txe_byte_pool_delete(TX_BYTE_POOL *pool_ptr) +{ + UNREGISTER_NODE(pool_ptr, tx_byte_pool_created_next, tx_byte_pool_created_previous, _tx_byte_pool_created_ptr, _tx_byte_pool_created_count); + pool_ptr -> tx_byte_pool_id = 0UL; + return(TX_SUCCESS); +} + +UINT _txe_timer_create(TX_TIMER *timer_ptr, CHAR *name_ptr, VOID (*exp_fn)(ULONG), ULONG exp_in, + ULONG init_ticks, ULONG resched_ticks, UINT auto_act, UINT cb_size) +{ + (VOID) exp_fn; + (VOID) exp_in; + (VOID) init_ticks; + (VOID) resched_ticks; + (VOID) auto_act; + (VOID) cb_size; + memset((VOID *) timer_ptr, 0, sizeof(TX_TIMER)); + timer_ptr -> tx_timer_id = TX_TIMER_ID; + timer_ptr -> tx_timer_name = name_ptr; + REGISTER_NODE(timer_ptr, tx_timer_created_next, tx_timer_created_previous, _tx_timer_created_ptr, _tx_timer_created_count); + return(TX_SUCCESS); +} + +UINT _txe_timer_activate(TX_TIMER *timer_ptr) +{ + (VOID) timer_ptr; + return(TX_SUCCESS); +} + +UINT _txe_timer_deactivate(TX_TIMER *timer_ptr) +{ + (VOID) timer_ptr; + return(TX_SUCCESS); +} + +UINT _txe_timer_change(TX_TIMER *timer_ptr, ULONG init_ticks, ULONG resched_ticks) +{ + (VOID) timer_ptr; + (VOID) init_ticks; + (VOID) resched_ticks; + return(TX_SUCCESS); +} + +UINT _txe_timer_delete(TX_TIMER *timer_ptr) +{ + UNREGISTER_NODE(timer_ptr, tx_timer_created_next, tx_timer_created_previous, _tx_timer_created_ptr, _tx_timer_created_count); + timer_ptr -> tx_timer_id = 0UL; + return(TX_SUCCESS); +} + +UINT _txe_mutex_create(TX_MUTEX *mutex_ptr, CHAR *name_ptr, UINT inherit, UINT cb_size) +{ + (VOID) inherit; + (VOID) cb_size; + memset((VOID *) mutex_ptr, 0, sizeof(TX_MUTEX)); + mutex_ptr -> tx_mutex_id = TX_MUTEX_ID; + mutex_ptr -> tx_mutex_name = name_ptr; + REGISTER_NODE(mutex_ptr, tx_mutex_created_next, tx_mutex_created_previous, _tx_mutex_created_ptr, _tx_mutex_created_count); + return(TX_SUCCESS); +} + +UINT _txe_mutex_prioritize(TX_MUTEX *mutex_ptr) +{ + (VOID) mutex_ptr; + return(TX_SUCCESS); +} + +UINT _txe_mutex_delete(TX_MUTEX *mutex_ptr) +{ + UNREGISTER_NODE(mutex_ptr, tx_mutex_created_next, tx_mutex_created_previous, _tx_mutex_created_ptr, _tx_mutex_created_count); + mutex_ptr -> tx_mutex_id = 0UL; + return(TX_SUCCESS); +} + +UINT _txe_semaphore_create(TX_SEMAPHORE *sem_ptr, CHAR *name_ptr, ULONG initial_count, UINT cb_size) +{ + (VOID) initial_count; + (VOID) cb_size; + memset((VOID *) sem_ptr, 0, sizeof(TX_SEMAPHORE)); + sem_ptr -> tx_semaphore_id = TX_SEMAPHORE_ID; + sem_ptr -> tx_semaphore_name = name_ptr; + REGISTER_NODE(sem_ptr, tx_semaphore_created_next, tx_semaphore_created_previous, _tx_semaphore_created_ptr, _tx_semaphore_created_count); + return(TX_SUCCESS); +} + +UINT _txe_semaphore_ceiling_put(TX_SEMAPHORE *sem_ptr, ULONG ceiling) +{ + (VOID) sem_ptr; + (VOID) ceiling; + return(TX_SUCCESS); +} + +UINT _txe_semaphore_get(TX_SEMAPHORE *sem_ptr, ULONG wait_option) +{ + (VOID) sem_ptr; + (VOID) wait_option; + return(TX_SUCCESS); +} + +UINT _txe_semaphore_put(TX_SEMAPHORE *sem_ptr) +{ + (VOID) sem_ptr; + return(TX_SUCCESS); +} + +UINT _txe_semaphore_delete(TX_SEMAPHORE *sem_ptr) +{ + UNREGISTER_NODE(sem_ptr, tx_semaphore_created_next, tx_semaphore_created_previous, _tx_semaphore_created_ptr, _tx_semaphore_created_count); + sem_ptr -> tx_semaphore_id = 0UL; + return(TX_SUCCESS); +} + +UINT _txe_event_flags_create(TX_EVENT_FLAGS_GROUP *group_ptr, CHAR *name_ptr, UINT cb_size) +{ + (VOID) cb_size; + memset((VOID *) group_ptr, 0, sizeof(TX_EVENT_FLAGS_GROUP)); + group_ptr -> tx_event_flags_group_id = TX_EVENT_FLAGS_ID; + group_ptr -> tx_event_flags_group_name = name_ptr; + REGISTER_NODE(group_ptr, tx_event_flags_group_created_next, tx_event_flags_group_created_previous, _tx_event_flags_created_ptr, _tx_event_flags_created_count); + return(TX_SUCCESS); +} + +UINT _txe_event_flags_set(TX_EVENT_FLAGS_GROUP *group_ptr, ULONG flags, UINT set_option) +{ + (VOID) group_ptr; + (VOID) flags; + (VOID) set_option; + return(TX_SUCCESS); +} + +UINT _txe_event_flags_get(TX_EVENT_FLAGS_GROUP *group_ptr, ULONG requested, UINT get_option, ULONG *actual_ptr, ULONG wait_option) +{ + (VOID) group_ptr; + (VOID) get_option; + (VOID) wait_option; + if (actual_ptr != TX_NULL) + { + *actual_ptr = requested; + } + return(TX_SUCCESS); +} + +UINT _txe_event_flags_delete(TX_EVENT_FLAGS_GROUP *group_ptr) +{ + UNREGISTER_NODE(group_ptr, tx_event_flags_group_created_next, tx_event_flags_group_created_previous, _tx_event_flags_created_ptr, _tx_event_flags_created_count); + group_ptr -> tx_event_flags_group_id = 0UL; + return(TX_SUCCESS); +} + +/* Memory layout for module data, code, and outside host buffers. */ +typedef struct SEC_MODULE_DATA_STRUCT +{ + ALIGN_TYPE extra[8]; + UCHAR pool_buf[256]; + CHAR name_buf[32]; + ULONG out_ulong; + VOID *out_ptr; +} SEC_MODULE_DATA; + +static SEC_MODULE_DATA mod_rw; +static SEC_MODULE_DATA outside_rw; +static UCHAR mod_ro[128]; +static TXM_MODULE_INSTANCE user_mod; +static TXM_MODULE_INSTANCE priv_mod; +static TX_THREAD cur_thread; +static TX_BLOCK_POOL app_block_pool; +static TX_BYTE_POOL app_byte_pool; +static TX_QUEUE app_queue; +static TX_THREAD app_thread; + +static VOID dummy_handler(TX_THREAD *t) +{ + (VOID) t; +} + +static VOID dummy_timer_cb(ULONG id) +{ + (VOID) id; +} + +static VOID setup_mod_instance(TXM_MODULE_INSTANCE *m, ULONG props) +{ + memset((VOID *) m, 0, sizeof(TXM_MODULE_INSTANCE)); + m -> txm_module_instance_id = TXM_MODULE_ID; + m -> txm_module_instance_state = TXM_MODULE_STARTED; + m -> txm_module_instance_property_flags = props; + m -> txm_module_instance_data_start = (VOID *) &mod_rw; + m -> txm_module_instance_data_end = (VOID *) (((UCHAR *) &mod_rw) + sizeof(mod_rw) - 1U); + m -> txm_module_instance_code_start = (VOID *) mod_ro; + m -> txm_module_instance_code_end = (VOID *) (mod_ro + sizeof(mod_ro) - 1U); +} + +int main(void) +{ +VOID *obj_timer = TX_NULL; +VOID *obj_mutex = TX_NULL; +VOID *obj_sem = TX_NULL; +VOID *obj_ev = TX_NULL; +VOID *obj_bpool = TX_NULL; +VOID *found_ptr = TX_NULL; +ALIGN_TYPE res; + + _tx_thread_system_state = 0UL; + memset((VOID *) &cur_thread, 0, sizeof(cur_thread)); + _tx_thread_current_ptr = &cur_thread; + + memset((VOID *) &_txm_module_manager_object_pool, 0, sizeof(_txm_module_manager_object_pool)); + _txm_module_manager_object_pool.tx_byte_pool_id = TX_BYTE_POOL_ID; + _txm_module_manager_object_pool.tx_byte_pool_start = sec_pool_mem.bytes; + _txm_module_manager_object_pool.tx_byte_pool_size = SEC_POOL_BYTES; + _txm_module_manager_object_pool.tx_byte_pool_available = SEC_POOL_BYTES; + _txm_module_manager_object_pool_created = TX_TRUE; + + setup_mod_instance(&user_mod, (TXM_MODULE_USER_MODE | TXM_MODULE_MEMORY_PROTECTION)); + setup_mod_instance(&priv_mod, 0UL); + cur_thread.tx_thread_module_instance_ptr = &user_mod; + + /* 1. User-mode privileged call restrictions vs privileged module allowance. */ + test_expect("user module refused TXM_THREAD_INTERRUPT_CONTROL_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_THREAD_INTERRUPT_CONTROL_CALL, TX_INT_DISABLE, 0, 0), + TXM_MODULE_INVALID_PROPERTIES); + test_expect("user module refused TXM_THREAD_STACK_ERROR_NOTIFY_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_THREAD_STACK_ERROR_NOTIFY_CALL, (ALIGN_TYPE) dummy_handler, 0, 0), + TXM_MODULE_INVALID_PROPERTIES); + test_expect("user module refused TXM_TRACE_ENABLE_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TRACE_ENABLE_CALL, (ALIGN_TYPE) mod_rw.pool_buf, 128, 8), + TXM_MODULE_INVALID_PROPERTIES); + test_expect("user module refused TXM_TRACE_INTERRUPT_CONTROL_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TRACE_INTERRUPT_CONTROL_CALL, 0, 0, 0), + TXM_MODULE_INVALID_PROPERTIES); + test_expect("user module refused TXM_TRACE_ISR_ENTER_INSERT_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TRACE_ISR_ENTER_INSERT_CALL, 1, 0, 0), + TXM_MODULE_INVALID_PROPERTIES); + test_expect("user module refused TXM_TRACE_ISR_EXIT_INSERT_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TRACE_ISR_EXIT_INSERT_CALL, 1, 0, 0), + TXM_MODULE_INVALID_PROPERTIES); + test_expect("interrupt/stack/trace kernel stubs never reached by user module", + stub_int_ctl_calls + stub_stack_err_calls + stub_trace_calls, 0UL); + + /* TXM_TRACE_USER_EVENT_INSERT_CALL is allowed in user mode, but validates extra_parameters. */ + test_expect("trace_user_event rejects outside extra_parameters", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TRACE_USER_EVENT_INSERT_CALL, 10, 20, (ALIGN_TYPE) outside_rw.extra), + TXM_MODULE_INVALID_MEMORY); + mod_rw.extra[0] = 1; + mod_rw.extra[1] = 2; + mod_rw.extra[2] = 3; + test_expect("trace_user_event accepts inside extra_parameters", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TRACE_USER_EVENT_INSERT_CALL, 10, 20, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + + /* Privileged module can invoke all restricted services. */ + cur_thread.tx_thread_module_instance_ptr = &priv_mod; + test_expect("priv module allowed TXM_THREAD_INTERRUPT_CONTROL_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_THREAD_INTERRUPT_CONTROL_CALL, 0, 0, 0), 0UL); + test_expect("priv module allowed TXM_THREAD_STACK_ERROR_NOTIFY_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_THREAD_STACK_ERROR_NOTIFY_CALL, (ALIGN_TYPE) dummy_handler, 0, 0), + TX_SUCCESS); + test_expect("priv module allowed TXM_TRACE_ENABLE_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TRACE_ENABLE_CALL, (ALIGN_TYPE) mod_rw.pool_buf, 128, 8), + TX_SUCCESS); + test_expect("priv module allowed TXM_TRACE_INTERRUPT_CONTROL_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TRACE_INTERRUPT_CONTROL_CALL, 0, 0, 0), TX_SUCCESS); + test_expect("priv module allowed TXM_TRACE_ISR_ENTER_INSERT_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TRACE_ISR_ENTER_INSERT_CALL, 1, 0, 0), TX_SUCCESS); + test_expect("priv module allowed TXM_TRACE_ISR_EXIT_INSERT_CALL", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TRACE_ISR_EXIT_INSERT_CALL, 1, 0, 0), TX_SUCCESS); + + /* 2. Unknown & application-range dispatch requests return TX_NOT_AVAILABLE cleanly. */ + cur_thread.tx_thread_module_instance_ptr = &user_mod; + test_expect("TXM_APPLICATION_REQUEST_ID_BASE forwards cleanly", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_APPLICATION_REQUEST_ID_BASE, 1, 2, 3), TX_NOT_AVAILABLE); + + /* 3. Object allocate & Timer/Mutex/Semaphore/EventFlags/BytePool create/use/delete + pointer checks. */ + test_expect("object allocate refuses outside destination pointer", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_ALLOCATE_CALL, (ALIGN_TYPE) &outside_rw.out_ptr, sizeof(TX_TIMER), 0), + TXM_MODULE_INVALID_MEMORY); + + test_expect("allocate TX_TIMER inside pool", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_ALLOCATE_CALL, (ALIGN_TYPE) &mod_rw.out_ptr, sizeof(TX_TIMER), 0), + TX_SUCCESS); + obj_timer = mod_rw.out_ptr; + + test_expect("allocate TX_MUTEX inside pool", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_ALLOCATE_CALL, (ALIGN_TYPE) &mod_rw.out_ptr, sizeof(TX_MUTEX), 0), + TX_SUCCESS); + obj_mutex = mod_rw.out_ptr; + + test_expect("allocate TX_SEMAPHORE inside pool", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_ALLOCATE_CALL, (ALIGN_TYPE) &mod_rw.out_ptr, sizeof(TX_SEMAPHORE), 0), + TX_SUCCESS); + obj_sem = mod_rw.out_ptr; + + test_expect("allocate TX_EVENT_FLAGS_GROUP inside pool", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_ALLOCATE_CALL, (ALIGN_TYPE) &mod_rw.out_ptr, sizeof(TX_EVENT_FLAGS_GROUP), 0), + TX_SUCCESS); + obj_ev = mod_rw.out_ptr; + + test_expect("allocate TX_BYTE_POOL inside pool", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_ALLOCATE_CALL, (ALIGN_TYPE) &mod_rw.out_ptr, sizeof(TX_BYTE_POOL), 0), + TX_SUCCESS); + obj_bpool = mod_rw.out_ptr; + + strcpy(mod_rw.name_buf, "sec_obj"); + + /* Timer create/activate/change/deactivate & extra_parameters rejection. */ + test_expect("timer_create rejects outside extra_parameters", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TIMER_CREATE_CALL, (ALIGN_TYPE) obj_timer, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) outside_rw.extra), + TXM_MODULE_INVALID_MEMORY); + mod_rw.extra[0] = (ALIGN_TYPE) dummy_timer_cb; + mod_rw.extra[1] = 1UL; + mod_rw.extra[2] = 5UL; + mod_rw.extra[3] = 5UL; + mod_rw.extra[4] = TX_AUTO_ACTIVATE; + mod_rw.extra[5] = sizeof(TX_TIMER); + test_expect("timer_create succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TIMER_CREATE_CALL, (ALIGN_TYPE) obj_timer, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + test_expect("timer_activate succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TIMER_ACTIVATE_CALL, (ALIGN_TYPE) obj_timer, 0, 0), TX_SUCCESS); + test_expect("timer_change rejects mutex CB (type check)", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TIMER_CHANGE_CALL, (ALIGN_TYPE) obj_mutex, 10, 20), + TXM_MODULE_INVALID_MEMORY); + test_expect("timer_change succeeds on timer object", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TIMER_CHANGE_CALL, (ALIGN_TYPE) obj_timer, 10, 20), TX_SUCCESS); + test_expect("timer_deactivate succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TIMER_DEACTIVATE_CALL, (ALIGN_TYPE) obj_timer, 0, 0), TX_SUCCESS); + + /* Mutex, Semaphore, Event Flags create & use + type-confusion checks. */ + mod_rw.extra[0] = TX_INHERIT; + mod_rw.extra[1] = sizeof(TX_MUTEX); + test_expect("mutex_create succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MUTEX_CREATE_CALL, (ALIGN_TYPE) obj_mutex, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + test_expect("mutex_put rejects timer control block (type check)", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MUTEX_PUT_CALL, (ALIGN_TYPE) obj_timer, 0, 0), + TXM_MODULE_INVALID_MEMORY); + test_expect("mutex_get succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MUTEX_GET_CALL, (ALIGN_TYPE) obj_mutex, TX_NO_WAIT, 0), TX_SUCCESS); + test_expect("mutex_prioritize succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MUTEX_PRIORITIZE_CALL, (ALIGN_TYPE) obj_mutex, 0, 0), TX_SUCCESS); + test_expect("mutex_put succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MUTEX_PUT_CALL, (ALIGN_TYPE) obj_mutex, 0, 0), TX_SUCCESS); + + mod_rw.extra[0] = 1UL; + mod_rw.extra[1] = sizeof(TX_SEMAPHORE); + test_expect("semaphore_create succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_SEMAPHORE_CREATE_CALL, (ALIGN_TYPE) obj_sem, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + test_expect("semaphore_ceiling_put rejects mutex CB", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_SEMAPHORE_CEILING_PUT_CALL, (ALIGN_TYPE) obj_mutex, 2, 0), + TXM_MODULE_INVALID_MEMORY); + test_expect("semaphore_ceiling_put succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_SEMAPHORE_CEILING_PUT_CALL, (ALIGN_TYPE) obj_sem, 2, 0), TX_SUCCESS); + test_expect("semaphore_get succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_SEMAPHORE_GET_CALL, (ALIGN_TYPE) obj_sem, TX_NO_WAIT, 0), TX_SUCCESS); + test_expect("semaphore_put succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_SEMAPHORE_PUT_CALL, (ALIGN_TYPE) obj_sem, 0, 0), TX_SUCCESS); + + test_expect("event_flags_create succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_EVENT_FLAGS_CREATE_CALL, (ALIGN_TYPE) obj_ev, (ALIGN_TYPE) mod_rw.name_buf, sizeof(TX_EVENT_FLAGS_GROUP)), + TX_SUCCESS); + test_expect("event_flags_set succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_EVENT_FLAGS_SET_CALL, (ALIGN_TYPE) obj_ev, 0x05, TX_OR), TX_SUCCESS); + mod_rw.extra[0] = TX_OR; + mod_rw.extra[1] = (ALIGN_TYPE) &mod_rw.out_ulong; + mod_rw.extra[2] = TX_NO_WAIT; + test_expect("event_flags_get rejects outside actual_events_ptr", + (ULONG) (mod_rw.extra[1] = (ALIGN_TYPE) &outside_rw.out_ulong, + _txm_module_manager_kernel_dispatch(TXM_EVENT_FLAGS_GET_CALL, (ALIGN_TYPE) obj_ev, 0x05, (ALIGN_TYPE) mod_rw.extra)), + TXM_MODULE_INVALID_MEMORY); + mod_rw.extra[1] = (ALIGN_TYPE) &mod_rw.out_ulong; + test_expect("event_flags_get succeeds with inside output pointer", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_EVENT_FLAGS_GET_CALL, (ALIGN_TYPE) obj_ev, 0x05, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + + /* Byte pool create & TXM_BYTE_RELEASE_CALL underflow/bounds checks. */ + mod_rw.extra[0] = (ALIGN_TYPE) mod_rw.pool_buf; + mod_rw.extra[1] = sizeof(mod_rw.pool_buf); + mod_rw.extra[2] = sizeof(TX_BYTE_POOL); + test_expect("byte_pool_create succeeds", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_BYTE_POOL_CREATE_CALL, (ALIGN_TYPE) obj_bpool, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + stub_byte_release_calls = 0UL; + test_expect("byte_release rejects integer-underflowing tiny address (< 2 words)", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_BYTE_RELEASE_CALL, (ALIGN_TYPE) sizeof(ALIGN_TYPE), 0, 0), + TXM_MODULE_INVALID_MEMORY); + test_expect("byte_release rejects block header straddling module data start", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_BYTE_RELEASE_CALL, (ALIGN_TYPE) (&mod_rw), 0, 0), + TXM_MODULE_INVALID_MEMORY); + test_expect("byte_release accepts address with 2-word header inside module data", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_BYTE_RELEASE_CALL, (ALIGN_TYPE) (&mod_rw.pool_buf[2U * sizeof(ALIGN_TYPE)]), 0, 0), + TX_SUCCESS); + test_expect("rejected byte_release calls never reached _txe_byte_release", stub_byte_release_calls, 1UL); + + /* 4. Object pointer get & extended security tests (app-owned + module-owned + protection boundary). */ + memset((VOID *) &app_queue, 0, sizeof(app_queue)); + app_queue.tx_queue_id = TX_QUEUE_ID; + app_queue.tx_queue_name = "app_q"; + REGISTER_NODE(&app_queue, tx_queue_created_next, tx_queue_created_previous, _tx_queue_created_ptr, _tx_queue_created_count); + + memset((VOID *) &app_thread, 0, sizeof(app_thread)); + app_thread.tx_thread_id = TX_THREAD_ID; + app_thread.tx_thread_name = "app_t"; + REGISTER_NODE(&app_thread, tx_thread_created_next, tx_thread_created_previous, _tx_thread_created_ptr, _tx_thread_created_count); + + memset((VOID *) &app_block_pool, 0, sizeof(app_block_pool)); + app_block_pool.tx_block_pool_id = TX_BLOCK_POOL_ID; + app_block_pool.tx_block_pool_name = "app_bp"; + REGISTER_NODE(&app_block_pool, tx_block_pool_created_next, tx_block_pool_created_previous, _tx_block_pool_created_ptr, _tx_block_pool_created_count); + + memset((VOID *) &app_byte_pool, 0, sizeof(app_byte_pool)); + app_byte_pool.tx_byte_pool_id = TX_BYTE_POOL_ID; + app_byte_pool.tx_byte_pool_name = "app_byp"; + REGISTER_NODE(&app_byte_pool, tx_byte_pool_created_next, tx_byte_pool_created_previous, _tx_byte_pool_created_ptr, _tx_byte_pool_created_count); + + /* Memory-protected module CANNOT look up block pools or byte pools. */ + strcpy(mod_rw.name_buf, "app_bp"); + res = _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_BLOCK_POOL_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) (mod_rw.extra[0] = 6U, mod_rw.extra[1] = (ALIGN_TYPE) &mod_rw.out_ptr, (ALIGN_TYPE) mod_rw.extra)); + test_expect("protected module denied TXM_BLOCK_POOL_OBJECT lookup", (ULONG) res, TXM_MODULE_INVALID); + + strcpy(mod_rw.name_buf, "sec_obj"); + res = _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_BYTE_POOL_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) (mod_rw.extra[0] = 7U, mod_rw.extra[1] = (ALIGN_TYPE) &mod_rw.out_ptr, (ALIGN_TYPE) mod_rw.extra)); + test_expect("protected module denied TXM_BYTE_POOL_OBJECT lookup", (ULONG) res, TXM_MODULE_INVALID); + + strcpy(mod_rw.name_buf, "app_byp"); + res = _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_BYTE_POOL_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) (mod_rw.extra[0] = 7U, mod_rw.extra[1] = (ALIGN_TYPE) &mod_rw.out_ptr, (ALIGN_TYPE) mod_rw.extra)); + test_expect("protected module denied app-owned TXM_BYTE_POOL_OBJECT lookup", (ULONG) res, TXM_MODULE_INVALID); + + /* Out-of-module output pointer / out-of-module name / length overflow refused. */ + mod_rw.extra[0] = 5U; + mod_rw.extra[1] = (ALIGN_TYPE) &outside_rw.out_ptr; + test_expect("object_pointer_get_extended rejects outside output pointer", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_QUEUE_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TXM_MODULE_INVALID_MEMORY); + mod_rw.extra[0] = (ALIGN_TYPE) (~((ALIGN_TYPE) 0)); + mod_rw.extra[1] = (ALIGN_TYPE) &mod_rw.out_ptr; + test_expect("object_pointer_get_extended rejects UINT_MAX string length overflow", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_QUEUE_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TXM_MODULE_INVALID_MEMORY); + + /* Valid lookups for thread, queue, timer, mutex, semaphore, event_flags. */ + strcpy(mod_rw.name_buf, "app_q"); + mod_rw.extra[0] = 5U; + mod_rw.extra[1] = (ALIGN_TYPE) &mod_rw.out_ptr; + mod_rw.out_ptr = TX_NULL; + test_expect("object_pointer_get_extended finds TXM_QUEUE_OBJECT", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_QUEUE_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + test_expect("found queue pointer matches app_queue", (ULONG) (mod_rw.out_ptr == (VOID *) &app_queue), 1UL); + + strcpy(mod_rw.name_buf, "app_t"); + test_expect("object_pointer_get_extended finds TXM_THREAD_OBJECT", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_THREAD_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + test_expect("found thread pointer matches app_thread", (ULONG) (mod_rw.out_ptr == (VOID *) &app_thread), 1UL); + + strcpy(mod_rw.name_buf, "sec_obj"); + mod_rw.extra[0] = 7U; + test_expect("object_pointer_get_extended finds TXM_TIMER_OBJECT", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_TIMER_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + test_expect("object_pointer_get_extended finds TXM_MUTEX_OBJECT", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_MUTEX_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + test_expect("object_pointer_get_extended finds TXM_SEMAPHORE_OBJECT", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_SEMAPHORE_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + test_expect("object_pointer_get_extended finds TXM_EVENT_FLAGS_OBJECT", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MODULE_OBJECT_POINTER_GET_EXTENDED_CALL, TXM_EVENT_FLAGS_OBJECT, (ALIGN_TYPE) mod_rw.name_buf, (ALIGN_TYPE) mod_rw.extra), + TX_SUCCESS); + + /* Unprotected direct API calls for block/byte pool + error cases. */ + cur_thread.tx_thread_module_instance_ptr = &priv_mod; + test_expect("direct object_pointer_get_extended NULL args return TX_PTR_ERROR", + _txm_module_manager_object_pointer_get_extended(TXM_QUEUE_OBJECT, TX_NULL, 5U, &found_ptr), TX_PTR_ERROR); + test_expect("direct object_pointer_get_extended bad option returns TX_OPTION_ERROR", + _txm_module_manager_object_pointer_get_extended(999U, "app_q", 5U, &found_ptr), TX_OPTION_ERROR); + test_expect("unprotected module finds TXM_BLOCK_POOL_OBJECT", + _txm_module_manager_object_pointer_get_extended(TXM_BLOCK_POOL_OBJECT, "app_bp", 6U, &found_ptr), TX_SUCCESS); + test_expect("unprotected legacy object_pointer_get finds TXM_BYTE_POOL_OBJECT", + _txm_module_manager_object_pointer_get(TXM_BYTE_POOL_OBJECT, "sec_obj", &found_ptr), TX_SUCCESS); + test_expect("missing object returns TX_NO_INSTANCE", + _txm_module_manager_object_pointer_get_extended(TXM_QUEUE_OBJECT, "missing", 7U, &found_ptr), TX_NO_INSTANCE); + + /* Cleanly delete created objects through dispatchers (triggering automatic deallocate). */ + cur_thread.tx_thread_module_instance_ptr = &user_mod; + test_expect("timer_delete succeeds and deallocates", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_TIMER_DELETE_CALL, (ALIGN_TYPE) obj_timer, 0, 0), TX_SUCCESS); + test_expect("mutex_delete succeeds and deallocates", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_MUTEX_DELETE_CALL, (ALIGN_TYPE) obj_mutex, 0, 0), TX_SUCCESS); + test_expect("semaphore_delete succeeds and deallocates", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_SEMAPHORE_DELETE_CALL, (ALIGN_TYPE) obj_sem, 0, 0), TX_SUCCESS); + test_expect("event_flags_delete succeeds and deallocates", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_EVENT_FLAGS_DELETE_CALL, (ALIGN_TYPE) obj_ev, 0, 0), TX_SUCCESS); + test_expect("byte_pool_delete succeeds and deallocates", + (ULONG) _txm_module_manager_kernel_dispatch(TXM_BYTE_POOL_DELETE_CALL, (ALIGN_TYPE) obj_bpool, 0, 0), TX_SUCCESS); + test_expect("all allocated module objects freed back to 0", user_mod.txm_module_instance_object_list_count, 0UL); + + test_expect("interrupt disable depth balanced to 0", (ULONG) test_interrupt_disable_depth, 0UL); + test_expect("interrupt restore underflows == 0", (ULONG) test_interrupt_restore_underflows, 0UL); + + if (test_failures == 0U) + { + printf("SUCCESS! %lu expectations\n", (unsigned long) test_checks); + return(0); + } + + printf("ERROR: %lu expectation(s) failed of %lu\n", + (unsigned long) test_failures, (unsigned long) test_checks); + return(1); +} diff --git a/test/tx/module_manager/threadx_module_manager_lifecycle_test.c b/test/tx/module_manager/threadx_module_manager_lifecycle_test.c new file mode 100644 index 000000000..835bf965a --- /dev/null +++ b/test/tx/module_manager/threadx_module_manager_lifecycle_test.c @@ -0,0 +1,958 @@ +/*************************************************************************** + * Copyright (c) 2026-present 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include + +#include "threadx_module_manager_host_test_port.h" + +#include "tx_api.h" +#include "tx_initialize.h" +#include "tx_thread.h" +#include "tx_trace.h" +#include "tx_timer.h" +#include "tx_queue.h" +#include "tx_semaphore.h" +#include "tx_mutex.h" +#include "tx_event_flags.h" +#include "tx_block_pool.h" +#include "tx_byte_pool.h" +#include "txm_module.h" +#include "txm_module_manager_util.h" + +/* Interrupt lock accounting expected by the host port shim. */ +unsigned int test_interrupt_disable_depth; +unsigned int test_interrupt_disable_max_depth; +unsigned int test_interrupt_restore_underflows; + +unsigned int test_get_ipsr_value(void) +{ + return((unsigned int) _tx_thread_system_state); +} + +/* Kernel state and created object lists used across lifecycle APIs. */ +volatile ULONG _tx_thread_system_state; +volatile UINT _tx_thread_preempt_disable; +TX_THREAD *_tx_thread_current_ptr; +TX_THREAD *_tx_thread_execute_ptr; +TX_THREAD _tx_timer_thread; +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; + +#ifdef TX_ENABLE_EVENT_TRACE +TX_TRACE_HEADER *_tx_trace_header_ptr; +TX_TRACE_BUFFER_ENTRY *_tx_trace_buffer_start_ptr; +TX_TRACE_BUFFER_ENTRY *_tx_trace_buffer_end_ptr; +TX_TRACE_BUFFER_ENTRY *_tx_trace_buffer_current_ptr; +ULONG _tx_trace_event_enable_bits; +ULONG _tx_trace_simulated_time; +VOID (*_tx_trace_full_notify_function)(VOID *buffer); + +VOID _tx_trace_object_register(UCHAR object_type, VOID *object_ptr, TX_NAME_CONST CHAR *object_name, + ULONG parameter_1, ULONG parameter_2) +{ + (VOID) object_type; + (VOID) object_ptr; + (VOID) object_name; + (VOID) parameter_1; + (VOID) parameter_2; +} + +UINT _tx_trace_disable(VOID) +{ + _tx_trace_buffer_current_ptr = TX_NULL; + return(TX_SUCCESS); +} +#endif + +/* Accounting and failure injection controls for kernel/port stubs. */ +static UINT test_failures; +static ULONG test_checks; + +static UINT stub_byte_pool_create_status; +static UINT stub_queue_create_status; +static UINT stub_obj_alloc_fail_on_call; +static UINT stub_obj_alloc_call_count; +static UINT stub_stop_hang; + +static ULONG stub_queue_create_calls; +static ULONG stub_queue_delete_calls; +static ULONG stub_thread_terminate_calls; +static ULONG stub_thread_delete_calls; +static ULONG stub_thread_resume_calls; +static ULONG stub_stack_build_calls; +static ULONG stub_byte_release_calls; +static ULONG stub_block_pool_delete_calls; +static ULONG stub_byte_pool_delete_calls; +static ULONG stub_ev_delete_calls; +static ULONG stub_mutex_delete_calls; +static ULONG stub_sem_delete_calls; +static ULONG stub_timer_delete_calls; +static ULONG stub_sleep_calls; + +static TXM_MODULE_INSTANCE *stub_stop_watch_module; + +/* Tracked pool allocations backed directly by each TX_BYTE_POOL buffer. */ +#define STUB_POOL_SLOTS 64U + +typedef struct STUB_ALLOC_STRUCT +{ + VOID *ptr; + ULONG size; + UINT in_use; +} STUB_ALLOC; + +static STUB_ALLOC stub_allocs[STUB_POOL_SLOTS]; + +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++; + } +} + +static VOID reset_stub_counters(VOID) +{ + stub_byte_pool_create_status = TX_SUCCESS; + stub_queue_create_status = TX_SUCCESS; + stub_obj_alloc_fail_on_call = 0U; + stub_obj_alloc_call_count = 0U; + stub_stop_hang = 0U; + stub_queue_create_calls = 0U; + stub_queue_delete_calls = 0U; + stub_thread_terminate_calls = 0U; + stub_thread_delete_calls = 0U; + stub_thread_resume_calls = 0U; + stub_stack_build_calls = 0U; + stub_byte_release_calls = 0U; + stub_block_pool_delete_calls = 0U; + stub_byte_pool_delete_calls = 0U; + stub_ev_delete_calls = 0U; + stub_mutex_delete_calls = 0U; + stub_sem_delete_calls = 0U; + stub_timer_delete_calls = 0U; + stub_sleep_calls = 0U; + stub_stop_watch_module = TX_NULL; +} + +static VOID *arena_alloc(TX_BYTE_POOL *pool_ptr, ULONG size) +{ +ULONG rounded = (size + (ULONG)(sizeof(ALIGN_TYPE) - 1U)) & ~((ULONG)(sizeof(ALIGN_TYPE) - 1U)); +ULONG used; +UINT i; +VOID *p; + + if ((pool_ptr == TX_NULL) || (rounded == 0UL) || (rounded > pool_ptr -> tx_byte_pool_available)) + { + return(TX_NULL); + } + used = pool_ptr -> tx_byte_pool_size - pool_ptr -> tx_byte_pool_available; + p = (VOID *) (pool_ptr -> tx_byte_pool_start + used); + pool_ptr -> tx_byte_pool_available -= rounded; + for (i = 0; i < STUB_POOL_SLOTS; i++) + { + if (!stub_allocs[i].in_use) + { + stub_allocs[i].ptr = p; + stub_allocs[i].size = rounded; + stub_allocs[i].in_use = 1U; + break; + } + } + return(p); +} + +static UINT arena_release(VOID *ptr) +{ +UINT i; + + for (i = 0; i < STUB_POOL_SLOTS; i++) + { + if (stub_allocs[i].in_use && (stub_allocs[i].ptr == ptr)) + { + stub_allocs[i].in_use = 0U; + return(TX_SUCCESS); + } + } + return(TX_PTR_ERROR); +} + +static UINT arena_active_allocs(VOID) +{ +UINT i; +UINT count = 0U; + + for (i = 0; i < STUB_POOL_SLOTS; i++) + { + if (stub_allocs[i].in_use) + { + count++; + } + } + return(count); +} + +/* Kernel and port stand-in functions required by lifecycle files under test. */ +UINT _tx_byte_pool_create(TX_BYTE_POOL *pool_ptr, CHAR *name_ptr, VOID *pool_start, ULONG pool_size) +{ + if (stub_byte_pool_create_status != TX_SUCCESS) + { + return(stub_byte_pool_create_status); + } + if ((pool_ptr == TX_NULL) || (pool_start == TX_NULL)) + { + return(TX_PTR_ERROR); + } + if (pool_size < 128UL) + { + return(TX_SIZE_ERROR); + } + memset((VOID *) pool_ptr, 0, sizeof(TX_BYTE_POOL)); + pool_ptr -> tx_byte_pool_id = TX_BYTE_POOL_ID; + pool_ptr -> tx_byte_pool_name = name_ptr; + pool_ptr -> tx_byte_pool_start = (UCHAR *) pool_start; + pool_ptr -> tx_byte_pool_size = pool_size; + pool_ptr -> tx_byte_pool_available = pool_size; + return(TX_SUCCESS); +} + +UINT _tx_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr, ULONG memory_size, ULONG wait_option) +{ +VOID *p; + + (VOID) wait_option; + if ((pool_ptr == TX_NULL) || (pool_ptr -> tx_byte_pool_id != TX_BYTE_POOL_ID) || (memory_ptr == TX_NULL)) + { + return(TX_PTR_ERROR); + } + if (pool_ptr == &_txm_module_manager_object_pool) + { + stub_obj_alloc_call_count++; + if ((stub_obj_alloc_fail_on_call != 0U) && (stub_obj_alloc_call_count == stub_obj_alloc_fail_on_call)) + { + return(TX_NO_MEMORY); + } + } + p = arena_alloc(pool_ptr, memory_size); + if (p == TX_NULL) + { + return(TX_NO_MEMORY); + } + *memory_ptr = p; + return(TX_SUCCESS); +} + +UINT _txe_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr, ULONG memory_size, ULONG wait_option) +{ + return(_tx_byte_allocate(pool_ptr, memory_ptr, memory_size, wait_option)); +} + +UINT _tx_byte_release(VOID *memory_ptr) +{ + stub_byte_release_calls++; + return(arena_release(memory_ptr)); +} + +UINT _txe_byte_release(VOID *memory_ptr) +{ + return(_tx_byte_release(memory_ptr)); +} + +UINT _tx_mutex_create(TX_MUTEX *mutex_ptr, CHAR *name_ptr, UINT inherit) +{ + (VOID) inherit; + if (mutex_ptr == TX_NULL) + { + return(TX_PTR_ERROR); + } + memset((VOID *) mutex_ptr, 0, sizeof(TX_MUTEX)); + mutex_ptr -> tx_mutex_id = TX_MUTEX_ID; + mutex_ptr -> tx_mutex_name = name_ptr; + return(TX_SUCCESS); +} + +UINT _tx_mutex_get(TX_MUTEX *mutex_ptr, ULONG wait_option) +{ + (VOID) wait_option; + if (mutex_ptr == TX_NULL) + { + return(TX_PTR_ERROR); + } + mutex_ptr -> tx_mutex_ownership_count++; + return(TX_SUCCESS); +} + +UINT _txe_mutex_get(TX_MUTEX *mutex_ptr, ULONG wait_option) +{ + return(_tx_mutex_get(mutex_ptr, wait_option)); +} + +UINT _tx_mutex_put(TX_MUTEX *mutex_ptr) +{ + if ((mutex_ptr == TX_NULL) || (mutex_ptr -> tx_mutex_ownership_count == 0UL)) + { + return(TX_NOT_OWNED); + } + mutex_ptr -> tx_mutex_ownership_count--; + return(TX_SUCCESS); +} + +UINT _txe_mutex_put(TX_MUTEX *mutex_ptr) +{ + return(_tx_mutex_put(mutex_ptr)); +} + +UINT _tx_queue_create(TX_QUEUE *queue_ptr, CHAR *name_ptr, UINT message_size, VOID *queue_start, ULONG queue_size) +{ + stub_queue_create_calls++; + if (stub_queue_create_status != TX_SUCCESS) + { + return(stub_queue_create_status); + } + memset((VOID *) queue_ptr, 0, sizeof(TX_QUEUE)); + queue_ptr -> tx_queue_id = TX_QUEUE_ID; + queue_ptr -> tx_queue_name = name_ptr; + queue_ptr -> tx_queue_message_size = message_size; + queue_ptr -> tx_queue_start = (ULONG *) queue_start; + queue_ptr -> tx_queue_end = (ULONG *) (((UCHAR *) queue_start) + queue_size); + queue_ptr -> tx_queue_read = queue_ptr -> tx_queue_start; + queue_ptr -> tx_queue_write = queue_ptr -> tx_queue_start; + return(TX_SUCCESS); +} + +UINT _tx_queue_delete(TX_QUEUE *queue_ptr) +{ + stub_queue_delete_calls++; + if ((queue_ptr == TX_NULL) || (queue_ptr -> tx_queue_id != TX_QUEUE_ID)) + { + return(TX_QUEUE_ERROR); + } + if (queue_ptr -> tx_queue_created_next != TX_NULL) + { + TX_QUEUE *next = queue_ptr -> tx_queue_created_next; + TX_QUEUE *prev = queue_ptr -> tx_queue_created_previous; + if (_tx_queue_created_count <= 1UL) + { + _tx_queue_created_ptr = TX_NULL; + _tx_queue_created_count = 0UL; + } + else + { + next -> tx_queue_created_previous = prev; + prev -> tx_queue_created_next = next; + if (_tx_queue_created_ptr == queue_ptr) + { + _tx_queue_created_ptr = next; + } + _tx_queue_created_count--; + } + } + queue_ptr -> tx_queue_id = 0UL; + return(TX_SUCCESS); +} + +UINT _tx_thread_create(TX_THREAD *thread_ptr, TX_NAME_CONST CHAR *name_ptr, + VOID (*entry_function)(ULONG), ULONG entry_input, + VOID *stack_start, ULONG stack_size, + UINT priority, UINT preempt_threshold, + ULONG time_slice, UINT auto_start) +{ + (VOID) entry_function; + (VOID) entry_input; + (VOID) time_slice; + + memset((VOID *) thread_ptr, 0, sizeof(TX_THREAD)); + thread_ptr -> tx_thread_id = TX_THREAD_ID; + thread_ptr -> tx_thread_name = name_ptr; + thread_ptr -> tx_thread_stack_start = stack_start; + thread_ptr -> tx_thread_stack_size = stack_size; + thread_ptr -> tx_thread_stack_end = (VOID *) (((UCHAR *) stack_start) + stack_size - 1U); + thread_ptr -> tx_thread_priority = priority; + thread_ptr -> tx_thread_preempt_threshold = preempt_threshold; + thread_ptr -> tx_thread_state = (auto_start == TX_AUTO_START) ? TX_READY : TX_SUSPENDED; + return(TX_SUCCESS); +} + +UINT _tx_thread_resume(TX_THREAD *thread_ptr) +{ + stub_thread_resume_calls++; + if (thread_ptr != TX_NULL) + { + thread_ptr -> tx_thread_state = TX_READY; + } + return(TX_SUCCESS); +} + +UINT _tx_thread_terminate(TX_THREAD *thread_ptr) +{ + stub_thread_terminate_calls++; + if ((thread_ptr != TX_NULL) && (thread_ptr -> tx_thread_id == TX_THREAD_ID)) + { + thread_ptr -> tx_thread_state = TX_TERMINATED; + return(TX_SUCCESS); + } + return(TX_THREAD_ERROR); +} + +UINT _tx_thread_delete(TX_THREAD *thread_ptr) +{ + stub_thread_delete_calls++; + if ((thread_ptr == TX_NULL) || (thread_ptr -> tx_thread_id != TX_THREAD_ID)) + { + return(TX_THREAD_ERROR); + } + if (thread_ptr -> tx_thread_created_next != TX_NULL) + { + TX_THREAD *next = thread_ptr -> tx_thread_created_next; + TX_THREAD *prev = thread_ptr -> tx_thread_created_previous; + if (_tx_thread_created_count <= 1UL) + { + _tx_thread_created_ptr = TX_NULL; + _tx_thread_created_count = 0UL; + } + else + { + next -> tx_thread_created_previous = prev; + prev -> tx_thread_created_next = next; + if (_tx_thread_created_ptr == thread_ptr) + { + _tx_thread_created_ptr = next; + } + _tx_thread_created_count--; + } + } + thread_ptr -> tx_thread_id = 0UL; + return(TX_SUCCESS); +} + +UINT _tx_thread_sleep(ULONG timer_ticks) +{ + (VOID) timer_ticks; + stub_sleep_calls++; + if ((stub_stop_watch_module != TX_NULL) && (!stub_stop_hang)) + { + stub_stop_watch_module -> txm_module_instance_start_stop_thread.tx_thread_state = TX_COMPLETED; + } + return(TX_SUCCESS); +} + +VOID _tx_thread_system_resume(TX_THREAD *thread_ptr) +{ + if (thread_ptr != TX_NULL) + { + thread_ptr -> tx_thread_state = TX_READY; + } +} + +VOID _tx_thread_system_preempt_check(VOID) +{ +} + +VOID _tx_thread_timeout(ULONG timer_input) +{ + (VOID) timer_input; +} + +VOID _txm_module_manager_thread_stack_build(TX_THREAD *thread_ptr, VOID (*shell_function)(TX_THREAD *, TXM_MODULE_INSTANCE *)) +{ + (VOID) thread_ptr; + (VOID) shell_function; + stub_stack_build_calls++; +} + +ALIGN_TYPE _txm_module_manager_user_mode_entry(ULONG kernel_request, ALIGN_TYPE param_1, ALIGN_TYPE param_2, ALIGN_TYPE param_3) +{ + (VOID) kernel_request; + (VOID) param_1; + (VOID) param_2; + (VOID) param_3; + return(0); +} + +VOID _txm_module_manager_alignment_adjust(TXM_MODULE_PREAMBLE *module_preamble, ULONG *code_size, + ULONG *code_alignment, ULONG *data_size, ULONG *data_alignment) +{ + (VOID) module_preamble; + (VOID) code_size; + (VOID) code_alignment; + (VOID) data_size; + (VOID) data_alignment; +} + +VOID _txm_module_manager_mm_register_setup(TXM_MODULE_INSTANCE *module_instance) +{ + (VOID) module_instance; +} + +VOID _txm_module_manager_queue_notify_trampoline(TX_QUEUE *queue_ptr) +{ + (VOID) queue_ptr; +} + +VOID _txm_module_manager_event_flags_notify_trampoline(TX_EVENT_FLAGS_GROUP *group_ptr) +{ + (VOID) group_ptr; +} + +VOID _txm_module_manager_semaphore_notify_trampoline(TX_SEMAPHORE *semaphore_ptr) +{ + (VOID) semaphore_ptr; +} + +ALIGN_TYPE _txm_module_manager_kernel_dispatch(ULONG kernel_request, ALIGN_TYPE param_0, ALIGN_TYPE param_1, ALIGN_TYPE param_2) +{ + (VOID) kernel_request; + (VOID) param_0; + (VOID) param_1; + (VOID) param_2; + return(0); +} + +UINT _tx_block_pool_delete(TX_BLOCK_POOL *pool_ptr) +{ + stub_block_pool_delete_calls++; + _tx_block_pool_created_ptr = TX_NULL; + _tx_block_pool_created_count = 0UL; + pool_ptr -> tx_block_pool_id = 0UL; + return(TX_SUCCESS); +} + +UINT _tx_byte_pool_delete(TX_BYTE_POOL *pool_ptr) +{ + stub_byte_pool_delete_calls++; + _tx_byte_pool_created_ptr = TX_NULL; + _tx_byte_pool_created_count = 0UL; + pool_ptr -> tx_byte_pool_id = 0UL; + return(TX_SUCCESS); +} + +UINT _tx_event_flags_delete(TX_EVENT_FLAGS_GROUP *group_ptr) +{ + stub_ev_delete_calls++; + _tx_event_flags_created_ptr = TX_NULL; + _tx_event_flags_created_count = 0UL; + group_ptr -> tx_event_flags_group_id = 0UL; + return(TX_SUCCESS); +} + +UINT _tx_mutex_delete(TX_MUTEX *mutex_ptr) +{ + stub_mutex_delete_calls++; + _tx_mutex_created_ptr = TX_NULL; + _tx_mutex_created_count = 0UL; + mutex_ptr -> tx_mutex_id = 0UL; + return(TX_SUCCESS); +} + +UINT _tx_semaphore_delete(TX_SEMAPHORE *semaphore_ptr) +{ + stub_sem_delete_calls++; + _tx_semaphore_created_ptr = TX_NULL; + _tx_semaphore_created_count = 0UL; + semaphore_ptr -> tx_semaphore_id = 0UL; + return(TX_SUCCESS); +} + +UINT _tx_timer_delete(TX_TIMER *timer_ptr) +{ + stub_timer_delete_calls++; + _tx_timer_created_ptr = TX_NULL; + _tx_timer_created_count = 0UL; + timer_ptr -> tx_timer_id = 0UL; + return(TX_SUCCESS); +} + +/* Module image fixtures in RAM (code area with TXM_MODULE_PREAMBLE). */ +typedef struct TEST_MODULE_IMAGE_STRUCT +{ + TXM_MODULE_PREAMBLE preamble; + UCHAR code_body[256]; +} TEST_MODULE_IMAGE; + +static TEST_MODULE_IMAGE test_image_1; +static TEST_MODULE_IMAGE test_image_2; +static union +{ + ALIGN_TYPE align; + UCHAR bytes[16384]; +} manager_pool_mem; +static union +{ + ALIGN_TYPE align; + UCHAR bytes[16384]; +} object_pool_mem; +static TXM_MODULE_INSTANCE mod_1; +static TXM_MODULE_INSTANCE mod_2; +static TXM_MODULE_INSTANCE bad_mod; +static TX_THREAD host_thread; + +static VOID dummy_shell(TX_THREAD *t, TXM_MODULE_INSTANCE *m) +{ + (VOID) t; + (VOID) m; +} + +static VOID dummy_thread_entry(ULONG input) +{ + (VOID) input; +} + +static VOID init_valid_image(TEST_MODULE_IMAGE *img, ULONG props, UINT prio) +{ + memset((VOID *) img, 0, sizeof(TEST_MODULE_IMAGE)); + img -> preamble.txm_module_preamble_id = TXM_MODULE_ID; + img -> preamble.txm_module_preamble_version_major = 1UL; + img -> preamble.txm_module_preamble_version_minor = 0UL; + img -> preamble.txm_module_preamble_preamble_size = (ULONG) (sizeof(TXM_MODULE_PREAMBLE) / sizeof(ULONG)); + img -> preamble.txm_module_preamble_application_module_id = 42UL; + img -> preamble.txm_module_preamble_property_flags = props; + img -> preamble.txm_module_preamble_shell_entry_function = (ULONG) sizeof(TXM_MODULE_PREAMBLE); + img -> preamble.txm_module_preamble_start_function = (ULONG) (sizeof(TXM_MODULE_PREAMBLE) + 16U); + img -> preamble.txm_module_preamble_stop_function = (ULONG) (sizeof(TXM_MODULE_PREAMBLE) + 32U); + img -> preamble.txm_module_preamble_callback_function = (ULONG) (sizeof(TXM_MODULE_PREAMBLE) + 48U); + img -> preamble.txm_module_preamble_start_stop_priority = (ULONG) prio; + img -> preamble.txm_module_preamble_start_stop_stack_size = 512UL; + img -> preamble.txm_module_preamble_callback_priority = (ULONG) prio; + img -> preamble.txm_module_preamble_callback_stack_size = 512UL; + img -> preamble.txm_module_preamble_code_size = (ULONG) sizeof(TEST_MODULE_IMAGE); + img -> preamble.txm_module_preamble_data_size = 512UL; +} + +int main(void) +{ +ULONG props_out = 0UL; +VOID *code_alloc_block = TX_NULL; +TX_THREAD mod_worker_thread; +TX_THREAD non_mod_thread; +UCHAR mod_worker_stack[1024]; +TX_QUEUE *owned_queue = TX_NULL; +TX_SEMAPHORE *owned_sem = TX_NULL; +TX_MUTEX *owned_mutex = TX_NULL; +TX_EVENT_FLAGS_GROUP *owned_ev = TX_NULL; +TX_BLOCK_POOL *owned_bp = TX_NULL; +TX_BYTE_POOL *owned_byp = TX_NULL; +TX_TIMER *owned_timer = TX_NULL; + + _tx_thread_system_state = 0UL; + memset((VOID *) &host_thread, 0, sizeof(host_thread)); + _tx_thread_current_ptr = &host_thread; + + /* 1. Calls before manager initialization must return TX_NOT_AVAILABLE. */ + test_expect("internal_load before init returns TX_NOT_AVAILABLE", + _txm_module_manager_internal_load(&mod_1, "m1", &test_image_1, sizeof(test_image_1), TX_NULL, 0UL, 0UL), + TX_NOT_AVAILABLE); + test_expect("properties_get before init returns TX_NOT_AVAILABLE", + _txm_module_manager_properties_get(&mod_1, &props_out), TX_NOT_AVAILABLE); + test_expect("max_priority_set before init returns TX_NOT_AVAILABLE", + _txm_module_manager_maximum_module_priority_set(&mod_1, 10U), TX_NOT_AVAILABLE); + test_expect("start before init returns TX_NOT_AVAILABLE", + _txm_module_manager_start(&mod_1), TX_NOT_AVAILABLE); + test_expect("stop before init returns TX_NOT_AVAILABLE", + _txm_module_manager_stop(&mod_1), TX_NOT_AVAILABLE); + test_expect("unload before init returns TX_NOT_AVAILABLE", + _txm_module_manager_unload(&mod_1), TX_NOT_AVAILABLE); + + /* 2. Initialize & Object Pool Create tests. */ + _tx_thread_system_state = 1UL; + test_expect("initialize from interrupt returns TX_CALLER_ERROR", + _txm_module_manager_initialize(manager_pool_mem.bytes, sizeof(manager_pool_mem.bytes)), + TX_CALLER_ERROR); + test_expect("manager stays not ready after ISR initialize attempt", _txm_module_manager_ready, TX_FALSE); + _tx_thread_system_state = 0UL; + + stub_byte_pool_create_status = TX_SUCCESS; + _txm_module_manager_object_pool_created = TX_TRUE; + test_expect("initialize succeeds with valid memory", + _txm_module_manager_initialize(manager_pool_mem.bytes, sizeof(manager_pool_mem.bytes)), + TX_SUCCESS); + test_expect("manager ready flag is TX_TRUE", _txm_module_manager_ready, TX_TRUE); + test_expect("initialize resets object_pool_created to TX_FALSE", _txm_module_manager_object_pool_created, TX_FALSE); + + test_expect("object_pool_create succeeds", + _txm_module_manager_object_pool_create(object_pool_mem.bytes, sizeof(object_pool_mem.bytes)), + TX_SUCCESS); + test_expect("object_pool_created is TX_TRUE", _txm_module_manager_object_pool_created, TX_TRUE); + + /* 3. Internal load validation, property enforcement, and loaded list linking. */ + init_valid_image(&test_image_1, (TXM_MODULE_USER_MODE | TXM_MODULE_MEMORY_PROTECTION), 16U); + init_valid_image(&test_image_2, TXM_MODULE_USER_MODE, 16U); + + _tx_thread_system_state = 1UL; + test_expect("internal_load from interrupt returns TX_CALLER_ERROR", + _txm_module_manager_internal_load(&mod_1, "m1", &test_image_1, sizeof(test_image_1), TX_NULL, 0UL, 0UL), + TX_CALLER_ERROR); + _tx_thread_system_state = 0UL; + + test_expect("internal_load NULL module returns TX_PTR_ERROR", + _txm_module_manager_internal_load(TX_NULL, "m1", &test_image_1, sizeof(test_image_1), TX_NULL, 0UL, 0UL), + TX_PTR_ERROR); + + /* TXM_MODULE_MEMORY_PROTECTION without TXM_MODULE_USER_MODE must fail. */ + test_image_1.preamble.txm_module_preamble_property_flags = TXM_MODULE_MEMORY_PROTECTION; + test_expect("internal_load rejects memory protection without user mode", + _txm_module_manager_internal_load(&mod_1, "m1", &test_image_1, sizeof(test_image_1), TX_NULL, 0UL, 0UL), + TXM_MODULE_INVALID_PROPERTIES); + test_image_1.preamble.txm_module_preamble_property_flags = (TXM_MODULE_USER_MODE | TXM_MODULE_MEMORY_PROTECTION); + + /* Allocate a fake code allocation block from manager pool to verify unload frees code_allocation_ptr too. */ + test_expect("code block test alloc succeeds", + _tx_byte_allocate(&_txm_module_manager_byte_pool, &code_alloc_block, 512UL, TX_NO_WAIT), TX_SUCCESS); + test_expect("internal_load valid mod_1 succeeds", + _txm_module_manager_internal_load(&mod_1, "mod_1", &test_image_1, sizeof(test_image_1), + code_alloc_block, 512UL, 0UL), + TX_SUCCESS); + test_expect("internal_load duplicate mod_1 returns TXM_MODULE_ALREADY_LOADED", + _txm_module_manager_internal_load(&mod_1, "mod_1", &test_image_1, sizeof(test_image_1), + TX_NULL, 0UL, 0UL), + TXM_MODULE_ALREADY_LOADED); + test_expect("internal_load valid mod_2 succeeds", + _txm_module_manager_internal_load(&mod_2, "mod_2", &test_image_2, sizeof(test_image_2), + TX_NULL, 0UL, 0UL), + TX_SUCCESS); + test_expect("loaded module count == 2", _txm_module_manger_loaded_count, 2UL); + + /* 4. properties_get & maximum_module_priority_set tests. */ + memset((VOID *) &bad_mod, 0, sizeof(bad_mod)); + test_expect("properties_get NULL module returns TX_PTR_ERROR", + _txm_module_manager_properties_get(TX_NULL, &props_out), TX_PTR_ERROR); + test_expect("properties_get NULL out ptr returns TX_PTR_ERROR", + _txm_module_manager_properties_get(&mod_1, TX_NULL), TX_PTR_ERROR); + test_expect("properties_get cleared-ID module returns TX_PTR_ERROR", + _txm_module_manager_properties_get(&bad_mod, &props_out), TX_PTR_ERROR); + test_expect("properties_get valid mod_1 returns TX_SUCCESS", + _txm_module_manager_properties_get(&mod_1, &props_out), TX_SUCCESS); + test_expect("properties_get returns loaded property flags", + props_out, (TXM_MODULE_USER_MODE | TXM_MODULE_MEMORY_PROTECTION)); + + test_expect("max_priority_set NULL module returns TX_PTR_ERROR", + _txm_module_manager_maximum_module_priority_set(TX_NULL, 10U), TX_PTR_ERROR); + test_expect("max_priority_set succeeds on loaded module", + _txm_module_manager_maximum_module_priority_set(&mod_1, 10U), TX_SUCCESS); + test_expect("max_priority updated to 10", mod_1.txm_module_instance_maximum_priority, 10UL); + + /* Enforce privilege ceiling in start(): preamble priority 16 >= 10 succeeds, 5 < 10 fails. */ + mod_1.txm_module_instance_start_stop_priority = 5UL; + test_expect("start refuses privileged start_stop_priority (< max)", + _txm_module_manager_start(&mod_1), TX_PRIORITY_ERROR); + mod_1.txm_module_instance_start_stop_priority = 16UL; + + mod_1.txm_module_instance_callback_priority = 5UL; + test_expect("start refuses privileged callback_priority (< max)", + _txm_module_manager_start(&mod_1), TX_PRIORITY_ERROR); + mod_1.txm_module_instance_callback_priority = 16UL; + + /* 5. start() failure rollback tests (queue fail, 1st thread fail, 2nd thread fail). */ + reset_stub_counters(); + stub_queue_create_status = TX_NO_MEMORY; + test_expect("start returns TX_START_ERROR when callback queue create fails", + _txm_module_manager_start(&mod_1), TX_START_ERROR); + test_expect("module stays LOADED after queue failure", mod_1.txm_module_instance_state, TXM_MODULE_LOADED); + + reset_stub_counters(); + stub_obj_alloc_fail_on_call = 1U; + test_expect("start rolls back queue when start_stop_thread fails", _txm_module_manager_start(&mod_1), TX_NO_MEMORY); + test_expect("callback queue deleted on start thread failure", stub_queue_delete_calls, 1UL); + test_expect("module stays LOADED after start thread failure", mod_1.txm_module_instance_state, TXM_MODULE_LOADED); + + reset_stub_counters(); + stub_obj_alloc_fail_on_call = 2U; + test_expect("start rolls back start_stop_thread + queue when callback thread fails", + _txm_module_manager_start(&mod_1), TX_NO_MEMORY); + test_expect("start_stop_thread terminated on callback thread failure", stub_thread_terminate_calls, 1UL); + test_expect("start_stop_thread deleted on callback thread failure", stub_thread_delete_calls, 1UL); + test_expect("callback queue deleted on callback thread failure", stub_queue_delete_calls, 1UL); + + /* Valid start(). */ + reset_stub_counters(); + test_expect("start NULL module returns TX_PTR_ERROR", + _txm_module_manager_start(TX_NULL), TX_PTR_ERROR); + test_expect("valid start succeeds", _txm_module_manager_start(&mod_1), TX_SUCCESS); + test_expect("start state becomes TXM_MODULE_STARTED", mod_1.txm_module_instance_state, TXM_MODULE_STARTED); + test_expect("start while already started returns TX_START_ERROR", + _txm_module_manager_start(&mod_1), TX_START_ERROR); + test_expect("max_priority_set refused while started", + _txm_module_manager_maximum_module_priority_set(&mod_1, 12U), TX_START_ERROR); + test_expect("unload refused while started (TX_NOT_DONE)", + _txm_module_manager_unload(&mod_1), TX_NOT_DONE); + + /* 6. thread_create & thread_reset tests. */ + reset_stub_counters(); + memset((VOID *) &mod_worker_thread, 0, sizeof(mod_worker_thread)); + test_expect("_txm_module_manager_thread_create succeeds", + _txm_module_manager_thread_create(&mod_worker_thread, "mwrk", dummy_shell, + dummy_thread_entry, 0UL, + mod_worker_stack, sizeof(mod_worker_stack), + 16U, 16U, TX_NO_TIME_SLICE, TX_DONT_START, + sizeof(TX_THREAD), &mod_1), + TX_SUCCESS); + test_expect("thread_create allocates kernel stack for user-mode module", + (ULONG) (mod_worker_thread.tx_thread_module_kernel_stack_start != TX_NULL), 1UL); + + /* Invalid thread_reset requests. */ + _tx_thread_current_ptr = &mod_worker_thread; + test_expect("thread_reset refuses current thread", + _txm_module_manager_thread_reset(&mod_worker_thread), TX_NOT_DONE); + /* txm_module_manager_thread_reset performs a second TX_RESTORE on current_thread mismatch. */ + test_expect("current-thread mismatch triggers extra TX_RESTORE", (ULONG) test_interrupt_restore_underflows, 1UL); + test_interrupt_restore_underflows = 0U; + _tx_thread_current_ptr = &host_thread; + + memset((VOID *) &non_mod_thread, 0, sizeof(non_mod_thread)); + non_mod_thread.tx_thread_id = TX_THREAD_ID; + non_mod_thread.tx_thread_state = TX_COMPLETED; + test_expect("thread_reset refuses non-module completed thread", + _txm_module_manager_thread_reset(&non_mod_thread), TX_NOT_DONE); + + mod_worker_thread.tx_thread_state = TX_SUSPENDED; + test_expect("thread_reset refuses TX_SUSPENDED thread", + _txm_module_manager_thread_reset(&mod_worker_thread), TX_NOT_DONE); + + /* Valid thread_reset on TX_COMPLETED & TX_TERMINATED. */ + reset_stub_counters(); + mod_worker_thread.tx_thread_state = TX_COMPLETED; + test_expect("thread_reset succeeds on completed module thread", + _txm_module_manager_thread_reset(&mod_worker_thread), TX_SUCCESS); + test_expect("thread_reset rebuilds stack", stub_stack_build_calls, 1UL); + test_expect("thread_reset moves thread to TX_SUSPENDED", mod_worker_thread.tx_thread_state, TX_SUSPENDED); + + mod_worker_thread.tx_thread_state = TX_TERMINATED; + test_expect("thread_reset succeeds on terminated module thread", + _txm_module_manager_thread_reset(&mod_worker_thread), TX_SUCCESS); + + /* 7. Allocate objects of each kernel type owned by mod_1 so stop() exercises cleanup loops. */ + _txm_module_manager_object_allocate((VOID **) &owned_queue, sizeof(TX_QUEUE), &mod_1); + memset((VOID *) owned_queue, 0, sizeof(TX_QUEUE)); + owned_queue -> tx_queue_id = TX_QUEUE_ID; + owned_queue -> tx_queue_created_next = owned_queue; + owned_queue -> tx_queue_created_previous = owned_queue; + _tx_queue_created_ptr = owned_queue; + _tx_queue_created_count = 1UL; + + _txm_module_manager_object_allocate((VOID **) &owned_sem, sizeof(TX_SEMAPHORE), &mod_1); + memset((VOID *) owned_sem, 0, sizeof(TX_SEMAPHORE)); + owned_sem -> tx_semaphore_id = TX_SEMAPHORE_ID; + owned_sem -> tx_semaphore_created_next = owned_sem; + owned_sem -> tx_semaphore_created_previous = owned_sem; + _tx_semaphore_created_ptr = owned_sem; + _tx_semaphore_created_count = 1UL; + + _txm_module_manager_object_allocate((VOID **) &owned_mutex, sizeof(TX_MUTEX), &mod_1); + memset((VOID *) owned_mutex, 0, sizeof(TX_MUTEX)); + owned_mutex -> tx_mutex_id = TX_MUTEX_ID; + owned_mutex -> tx_mutex_created_next = owned_mutex; + owned_mutex -> tx_mutex_created_previous = owned_mutex; + _tx_mutex_created_ptr = owned_mutex; + _tx_mutex_created_count = 1UL; + + _txm_module_manager_object_allocate((VOID **) &owned_ev, sizeof(TX_EVENT_FLAGS_GROUP), &mod_1); + memset((VOID *) owned_ev, 0, sizeof(TX_EVENT_FLAGS_GROUP)); + owned_ev -> tx_event_flags_group_id = TX_EVENT_FLAGS_ID; + owned_ev -> tx_event_flags_group_created_next = owned_ev; + owned_ev -> tx_event_flags_group_created_previous = owned_ev; + _tx_event_flags_created_ptr = owned_ev; + _tx_event_flags_created_count = 1UL; + + _txm_module_manager_object_allocate((VOID **) &owned_bp, sizeof(TX_BLOCK_POOL), &mod_1); + memset((VOID *) owned_bp, 0, sizeof(TX_BLOCK_POOL)); + owned_bp -> tx_block_pool_id = TX_BLOCK_POOL_ID; + owned_bp -> tx_block_pool_created_next = owned_bp; + owned_bp -> tx_block_pool_created_previous = owned_bp; + _tx_block_pool_created_ptr = owned_bp; + _tx_block_pool_created_count = 1UL; + + _txm_module_manager_object_allocate((VOID **) &owned_byp, sizeof(TX_BYTE_POOL), &mod_1); + memset((VOID *) owned_byp, 0, sizeof(TX_BYTE_POOL)); + owned_byp -> tx_byte_pool_id = TX_BYTE_POOL_ID; + owned_byp -> tx_byte_pool_created_next = owned_byp; + owned_byp -> tx_byte_pool_created_previous = owned_byp; + _tx_byte_pool_created_ptr = owned_byp; + _tx_byte_pool_created_count = 1UL; + + _txm_module_manager_object_allocate((VOID **) &owned_timer, sizeof(TX_TIMER), &mod_1); + memset((VOID *) owned_timer, 0, sizeof(TX_TIMER)); + owned_timer -> tx_timer_id = TX_TIMER_ID; + owned_timer -> tx_timer_created_next = owned_timer; + owned_timer -> tx_timer_created_previous = owned_timer; + _tx_timer_created_ptr = owned_timer; + _tx_timer_created_count = 1UL; + + /* 8. Stop & Unload tests. */ + reset_stub_counters(); + stub_stop_watch_module = &mod_1; + _tx_thread_system_state = 1UL; + test_expect("stop from interrupt returns TX_CALLER_ERROR", + _txm_module_manager_stop(&mod_1), TX_CALLER_ERROR); + _tx_thread_system_state = 0UL; + + test_expect("stop NULL module returns TX_PTR_ERROR", + _txm_module_manager_stop(TX_NULL), TX_PTR_ERROR); + test_expect("stop valid STARTED mod_1 succeeds", + _txm_module_manager_stop(&mod_1), TX_SUCCESS); + test_expect("mod_1 state becomes TXM_MODULE_STOPPED", mod_1.txm_module_instance_state, TXM_MODULE_STOPPED); + test_expect("stop deletes all module-owned created objects", + (ULONG) ((stub_queue_delete_calls >= 1UL) && (stub_sem_delete_calls == 1UL) && + (stub_mutex_delete_calls == 1UL) && (stub_ev_delete_calls == 1UL) && + (stub_block_pool_delete_calls == 1UL) && (stub_byte_pool_delete_calls == 1UL) && + (stub_timer_delete_calls == 1UL)), + 1UL); + test_expect("stop releases all object pool allocations owned by mod_1", + mod_1.txm_module_instance_object_list_count, 0UL); + test_expect("stop while already stopped returns TX_START_ERROR", + _txm_module_manager_stop(&mod_1), TX_START_ERROR); + + /* Unload mod_1 and mod_2, verifying list unlinking and memory release. */ + _tx_thread_system_state = 1UL; + test_expect("unload from interrupt returns TX_CALLER_ERROR", + _txm_module_manager_unload(&mod_1), TX_CALLER_ERROR); + _tx_thread_system_state = 0UL; + + test_expect("unload NULL returns TX_PTR_ERROR", + _txm_module_manager_unload(TX_NULL), TX_PTR_ERROR); + test_expect("unload mod_1 succeeds", _txm_module_manager_unload(&mod_1), TX_SUCCESS); + test_expect("mod_1 state becomes TXM_MODULE_UNLOADED", mod_1.txm_module_instance_state, TXM_MODULE_UNLOADED); + test_expect("mod_1 ID cleared on unload", mod_1.txm_module_instance_id, 0UL); + test_expect("loaded count decrements to 1", _txm_module_manger_loaded_count, 1UL); + test_expect("unload mod_2 succeeds", _txm_module_manager_unload(&mod_2), TX_SUCCESS); + test_expect("loaded count decrements to 0 and list is empty", + (ULONG) ((_txm_module_manger_loaded_count == 0UL) && (_txm_module_manager_loaded_list_ptr == TX_NULL)), + 1UL); + test_expect("all arena allocations freed after unloading both modules", (ULONG) arena_active_allocs(), 0UL); + + test_expect("interrupt disable depth balanced to 0", (ULONG) test_interrupt_disable_depth, 0UL); + test_expect("interrupt restore underflows == 0", (ULONG) test_interrupt_restore_underflows, 0UL); + + if (test_failures == 0U) + { + printf("SUCCESS! %lu expectations\n", (unsigned long) test_checks); + return(0); + } + + printf("ERROR: %lu expectation(s) failed of %lu\n", + (unsigned long) test_failures, (unsigned long) test_checks); + return(1); +} diff --git a/test/tx/regression/threadx_thread_basic_execution_test.c b/test/tx/regression/threadx_thread_basic_execution_test.c index ea9b11773..59e6942f0 100644 --- a/test/tx/regression/threadx_thread_basic_execution_test.c +++ b/test/tx/regression/threadx_thread_basic_execution_test.c @@ -56,6 +56,30 @@ static TX_THREAD thread_2; static TX_THREAD thread_3; static TX_THREAD test_thread; +#if !defined(TX_INLINE_THREAD_RESUME_SUSPEND) && !defined(TX_NOT_INTERRUPTABLE) +static TX_THREAD resume_high_thread; +static TX_THREAD resume_low_thread; +static ULONG resume_high_stack[2048]; +static ULONG resume_low_stack[2048]; +static volatile ULONG resume_high_counter = 0; + +static void resume_high_entry(ULONG thread_input) +{ + if (thread_input == 0UL) + { + resume_high_counter++; + } +} + +static void resume_low_entry(ULONG thread_input) +{ + if (thread_input != 0UL) + { + resume_high_counter++; + } +} +#endif + static TX_TIMER timer_0; @@ -947,6 +971,49 @@ VOID (*temp_mutex_release)(TX_THREAD *thread_ptr); #endif +#if !defined(TX_INLINE_THREAD_RESUME_SUSPEND) && !defined(TX_NOT_INTERRUPTABLE) + { + UINT old_posture; + UINT resume_status; + UINT high_priority; + UINT low_priority; + + resume_high_counter = 0; + high_priority = (thread_0.tx_thread_priority > 0U) ? (thread_0.tx_thread_priority - 1U) : 0U; + low_priority = thread_0.tx_thread_priority + 1U; + + resume_status = tx_thread_create(&resume_high_thread, "resume high", resume_high_entry, 0, + resume_high_stack, (ULONG) sizeof(resume_high_stack), + high_priority, high_priority, TX_NO_TIME_SLICE, TX_DONT_START); + resume_status += tx_thread_create(&resume_low_thread, "resume low", resume_low_entry, 0, + resume_low_stack, (ULONG) sizeof(resume_low_stack), + low_priority, low_priority, TX_NO_TIME_SLICE, TX_DONT_START); + if (resume_status != TX_SUCCESS) + { + printf("ERROR #42\n"); + test_control_return(1); + } + + old_posture = tx_interrupt_control(TX_INT_DISABLE); + _tx_thread_preempt_disable += 2U; + tx_interrupt_control(old_posture); + + _tx_thread_system_resume(&resume_high_thread); + _tx_thread_system_resume(&resume_low_thread); + + if (resume_high_counter != 1UL) + { + printf("ERROR #43\n"); + test_control_return(1); + } + + tx_thread_terminate(&resume_high_thread); + tx_thread_terminate(&resume_low_thread); + tx_thread_delete(&resume_high_thread); + tx_thread_delete(&resume_low_thread); + } +#endif + #ifndef TX_INLINE_THREAD_RESUME_SUSPEND #ifndef TX_NOT_INTERRUPTABLE