1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/***************************************************************************
* Copyright (C) 2026 Eclipse ThreadX contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* AI Disclosure: This file was largely AI-generated by Claude Code (Opus 5).
* The AI-generated portions may be considered public domain (CC0-1.0)
* and not subject to the project's licence. The human contributor has
* reviewed and verified that the code is correct.
*
* SPDX-License-Identifier: MIT and CC0-1.0
**************************************************************************/
/**************************************************************************/
/** */
/** ThreadX Component */
/** */
/** FreeRTOS compatibility layer test harness */
/** */
/**************************************************************************/
#include "txfr_test_harness.h"
#include <stdio.h>
#include <stdlib.h>
/* Accounting state. Only the single test thread touches these. */
static int txfr_accounting;
static TXFR_INJECT_TARGET txfr_inject_target;
static int txfr_inject_fail_on;
static int txfr_inject_call_index;
static TXFR_COUNTERS txfr_counters;
static int txfr_failures;
/* The real entry points, supplied by the linker in response to --wrap. */
UINT __real__txe_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr, ULONG memory_size, ULONG wait_option);
UINT __real__txe_byte_release(VOID *memory_ptr);
UINT __real__txe_semaphore_create(TX_SEMAPHORE *semaphore_ptr, TX_NAME_CONST CHAR *name_ptr, ULONG initial_count, UINT semaphore_control_block_size);
UINT __real__txe_semaphore_delete(TX_SEMAPHORE *semaphore_ptr);
UINT __real__txe_mutex_create(TX_MUTEX *mutex_ptr, TX_NAME_CONST CHAR *name_ptr, UINT inherit, UINT mutex_control_block_size);
UINT __real__txe_mutex_delete(TX_MUTEX *mutex_ptr);
UINT __real__txe_event_flags_create(TX_EVENT_FLAGS_GROUP *group_ptr, TX_NAME_CONST CHAR *name_ptr, UINT event_control_block_size);
UINT __real__txe_event_flags_delete(TX_EVENT_FLAGS_GROUP *group_ptr);
UINT __real__txe_timer_create(TX_TIMER *timer_ptr, TX_NAME_CONST CHAR *name_ptr, VOID (*expiration_function)(ULONG input),
ULONG expiration_input, ULONG initial_ticks, ULONG reschedule_ticks,
UINT auto_activate, UINT timer_control_block_size);
UINT __real__txe_timer_delete(TX_TIMER *timer_ptr);
UINT __real__txe_thread_create(TX_THREAD *thread_ptr, TX_NAME_CONST CHAR *name_ptr, VOID (*entry_function)(ULONG entry_input),
ULONG entry_input, VOID *stack_start, ULONG stack_size, UINT priority,
UINT preempt_threshold, ULONG time_slice, UINT auto_start,
UINT thread_control_block_size);
UINT __real__txe_thread_delete(TX_THREAD *thread_ptr);
UINT __real__txe_thread_resume(TX_THREAD *thread_ptr);
/* Determine whether the call now being made is the one the test asked to fail.
Only calls made while accounting is active are considered, which keeps the
objects created by tx_freertos_init() and by the harness out of the count. */
static int txfr_should_fail(TXFR_INJECT_TARGET target)
{
if((txfr_accounting == 0) || (txfr_inject_target != target))
{
return 0;
}
txfr_inject_call_index++;
return (txfr_inject_call_index == txfr_inject_fail_on) ? 1 : 0;
}
UINT __wrap__txe_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr, ULONG memory_size, ULONG wait_option)
{
if(txfr_accounting != 0)
{
txfr_counters.byte_allocate++;
}
return __real__txe_byte_allocate(pool_ptr, memory_ptr, memory_size, wait_option);
}
UINT __wrap__txe_byte_release(VOID *memory_ptr)
{
if(txfr_accounting != 0)
{
txfr_counters.byte_release++;
}
return __real__txe_byte_release(memory_ptr);
}
UINT __wrap__txe_semaphore_create(TX_SEMAPHORE *semaphore_ptr, TX_NAME_CONST CHAR *name_ptr, ULONG initial_count, UINT semaphore_control_block_size)
{
if(txfr_should_fail(TXFR_INJECT_SEMAPHORE_CREATE) != 0)
{
txfr_counters.semaphore_create++;
return TX_SEMAPHORE_ERROR;
}
if(txfr_accounting != 0)
{
txfr_counters.semaphore_create++;
}
return __real__txe_semaphore_create(semaphore_ptr, name_ptr, initial_count, semaphore_control_block_size);
}
UINT __wrap__txe_semaphore_delete(TX_SEMAPHORE *semaphore_ptr)
{
if(txfr_accounting != 0)
{
txfr_counters.semaphore_delete++;
}
return __real__txe_semaphore_delete(semaphore_ptr);
}
UINT __wrap__txe_mutex_create(TX_MUTEX *mutex_ptr, TX_NAME_CONST CHAR *name_ptr, UINT inherit, UINT mutex_control_block_size)
{
if(txfr_should_fail(TXFR_INJECT_MUTEX_CREATE) != 0)
{
txfr_counters.mutex_create++;
return TX_MUTEX_ERROR;
}
if(txfr_accounting != 0)
{
txfr_counters.mutex_create++;
}
return __real__txe_mutex_create(mutex_ptr, name_ptr, inherit, mutex_control_block_size);
}
UINT __wrap__txe_mutex_delete(TX_MUTEX *mutex_ptr)
{
if(txfr_accounting != 0)
{
txfr_counters.mutex_delete++;
}
return __real__txe_mutex_delete(mutex_ptr);
}
UINT __wrap__txe_event_flags_create(TX_EVENT_FLAGS_GROUP *group_ptr, TX_NAME_CONST CHAR *name_ptr, UINT event_control_block_size)
{
if(txfr_should_fail(TXFR_INJECT_EVENT_FLAGS_CREATE) != 0)
{
txfr_counters.event_flags_create++;
return TX_GROUP_ERROR;
}
if(txfr_accounting != 0)
{
txfr_counters.event_flags_create++;
}
return __real__txe_event_flags_create(group_ptr, name_ptr, event_control_block_size);
}
UINT __wrap__txe_event_flags_delete(TX_EVENT_FLAGS_GROUP *group_ptr)
{
if(txfr_accounting != 0)
{
txfr_counters.event_flags_delete++;
}
return __real__txe_event_flags_delete(group_ptr);
}
UINT __wrap__txe_timer_create(TX_TIMER *timer_ptr, TX_NAME_CONST CHAR *name_ptr, VOID (*expiration_function)(ULONG input),
ULONG expiration_input, ULONG initial_ticks, ULONG reschedule_ticks,
UINT auto_activate, UINT timer_control_block_size)
{
if(txfr_should_fail(TXFR_INJECT_TIMER_CREATE) != 0)
{
txfr_counters.timer_create++;
return TX_TIMER_ERROR;
}
if(txfr_accounting != 0)
{
txfr_counters.timer_create++;
}
return __real__txe_timer_create(timer_ptr, name_ptr, expiration_function, expiration_input,
initial_ticks, reschedule_ticks, auto_activate, timer_control_block_size);
}
UINT __wrap__txe_timer_delete(TX_TIMER *timer_ptr)
{
if(txfr_accounting != 0)
{
txfr_counters.timer_delete++;
}
return __real__txe_timer_delete(timer_ptr);
}
UINT __wrap__txe_thread_create(TX_THREAD *thread_ptr, TX_NAME_CONST CHAR *name_ptr, VOID (*entry_function)(ULONG entry_input),
ULONG entry_input, VOID *stack_start, ULONG stack_size, UINT priority,
UINT preempt_threshold, ULONG time_slice, UINT auto_start,
UINT thread_control_block_size)
{
if(txfr_should_fail(TXFR_INJECT_THREAD_CREATE) != 0)
{
txfr_counters.thread_create++;
return TX_THREAD_ERROR;
}
if(txfr_accounting != 0)
{
txfr_counters.thread_create++;
}
return __real__txe_thread_create(thread_ptr, name_ptr, entry_function, entry_input, stack_start,
stack_size, priority, preempt_threshold, time_slice, auto_start,
thread_control_block_size);
}
UINT __wrap__txe_thread_delete(TX_THREAD *thread_ptr)
{
if(txfr_accounting != 0)
{
txfr_counters.thread_delete++;
}
return __real__txe_thread_delete(thread_ptr);
}
UINT __wrap__txe_thread_resume(TX_THREAD *thread_ptr)
{
if(txfr_should_fail(TXFR_INJECT_THREAD_RESUME) != 0)
{
txfr_counters.thread_resume++;
return TX_RESUME_ERROR;
}
if(txfr_accounting != 0)
{
txfr_counters.thread_resume++;
}
return __real__txe_thread_resume(thread_ptr);
}
void txfr_test_account_start(TXFR_INJECT_TARGET target, int fail_on_call)
{
TX_MEMSET(&txfr_counters, 0, sizeof(txfr_counters));
txfr_inject_target = target;
txfr_inject_fail_on = fail_on_call;
txfr_inject_call_index = 0;
txfr_accounting = 1;
}
void txfr_test_account_stop(TXFR_COUNTERS *p_counters)
{
txfr_accounting = 0;
txfr_inject_target = TXFR_INJECT_NONE;
if(p_counters != NULL)
{
*p_counters = txfr_counters;
}
}
void txfr_test_check(const char *label, int condition)
{
printf("%-52s %s\n", label, (condition != 0) ? "PASS" : "FAIL");
if(condition == 0)
{
txfr_failures++;
}
}
void txfr_test_check_counts(const char *label, const TXFR_COUNTERS *p_counters,
int expect_allocate, int expect_release,
int expect_object_create, int expect_object_delete)
{
int object_create;
int object_delete;
int ok;
/* Only one kind of kernel object is exercised per check, so the totals
identify it unambiguously. */
object_create = p_counters->semaphore_create + p_counters->mutex_create +
p_counters->event_flags_create + p_counters->timer_create +
p_counters->thread_create;
object_delete = p_counters->semaphore_delete + p_counters->mutex_delete +
p_counters->event_flags_delete + p_counters->timer_delete +
p_counters->thread_delete;
ok = ((p_counters->byte_allocate == expect_allocate) &&
(p_counters->byte_release == expect_release) &&
(object_create == expect_object_create) &&
(object_delete == expect_object_delete)) ? 1 : 0;
printf("%-52s %s\n", label, (ok != 0) ? "PASS" : "FAIL");
if(ok == 0)
{
printf("%-52s got alloc=%d release=%d create=%d delete=%d\n", "",
p_counters->byte_allocate, p_counters->byte_release, object_create, object_delete);
printf("%-52s expected alloc=%d release=%d create=%d delete=%d\n", "",
expect_allocate, expect_release, expect_object_create, expect_object_delete);
txfr_failures++;
}
}
int txfr_test_failures(void)
{
return txfr_failures;
}
/* Test thread. The byte pool operations under test require thread context. */
static TX_THREAD txfr_test_thread;
static ULONG txfr_test_stack[4096];
static void txfr_test_thread_entry(ULONG id)
{
(void)id;
txfr_test_body();
printf("\n%s\n", (txfr_failures == 0) ? "ALL CHECKS PASSED" : "THERE WERE FAILED CHECKS");
fflush(stdout);
/* The kernel owns the calling thread, so returning here would simply idle.
Leave the process with a status ctest can read. */
exit((txfr_failures == 0) ? 0 : 1);
}
void tx_application_define(void *first_unused_memory)
{
UINT ret;
(void)first_unused_memory;
ret = tx_freertos_init();
if(ret != TX_SUCCESS)
{
printf("tx_freertos_init() failed with %u\n", ret);
exit(2);
}
ret = tx_thread_create(&txfr_test_thread, "txfr_test", txfr_test_thread_entry, 0u,
txfr_test_stack, sizeof(txfr_test_stack), 16u, 16u, 0u, TX_AUTO_START);
if(ret != TX_SUCCESS)
{
printf("tx_thread_create() failed with %u\n", ret);
exit(2);
}
}
int main(void)
{
tx_kernel_enter();
return 0;
}
|