summaryrefslogtreecommitdiff
path: root/test/tx/regression/threadx_thread_stack_fill_value_test.c
blob: fa6eda220530d64c2733bf8a0f9ad78c1a64ef17 (plain)
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
/***************************************************************************/
/* 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.                                    */
/*                                                                         */
/* SPDX-License-Identifier: MIT                                            */
/***************************************************************************/

// Portions of this file were generated with AI assistance.

/* This test checks that the stack fill value recorded in a thread control block is the
   value that was actually written into that thread's stack.  The two used to disagree
   when TX_ENABLE_RANDOM_NUMBER_STACK_FILLING was enabled, because the control block was
   cleared after the random value had been chosen and the stack had been filled with it,
   which left the stack checking and stack analyze routines looking for a pattern that is
   not the one present in the stack.  */

#include   <stdio.h>
#include   "tx_api.h"


/* Number of probe threads created.  A random fill value can legitimately come out as zero,
   which hides the defect for that one thread, so several threads are created and at least
   one of them is required to have produced a non-zero pattern.  */

#define TEST_PROBE_THREADS      16


static unsigned long   thread_0_counter =  0;
static TX_THREAD       thread_0;

static TX_THREAD       probe_thread;
static ULONG           probe_stack[TX_MINIMUM_STACK / sizeof(ULONG)];


/* Define task prototypes.  */

static void    thread_0_entry(ULONG task_input);


/* Prototype for test control return.  */

void  test_control_return(UINT status);


/* Define what the initial system looks like.  */

#ifdef CTEST
void test_application_define(void *first_unused_memory)
#else
void    threadx_thread_stack_fill_value_application_define(void *first_unused_memory)
#endif
{

UINT    status;
CHAR    *pointer;

    pointer =  (CHAR *) first_unused_memory;

    /* Put system definition stuff in here, e.g. thread creates and other assorted
       create information.  */

    status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
            pointer, TEST_STACK_SIZE_PRINTF,
            16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);

    /* Check for status.  */
    if (status != TX_SUCCESS)
    {

        printf("Running Thread Stack Fill Value Test................................ ERROR #1\n");
        test_control_return(1);
    }
}


/* Define the test threads.  */

static void    thread_0_entry(ULONG thread_input)
{

UINT        status;
UINT        i;
ULONG       stack_pattern;
UINT        non_zero_seen =  TX_FALSE;


    /* Increment thread 0 counter.  */
    thread_0_counter++;

    /* Inform user of success getting to this test.  */
    printf("Running Thread Stack Fill Value Test................................ ");

    for (i = 0; i < TEST_PROBE_THREADS; i++)
    {

        /* Create a thread that is never started, so that its stack still holds nothing but
           the fill pattern and the initial stack frame built at the top of the stack.  */
        status =  tx_thread_create(&probe_thread, "probe thread", thread_0_entry, 0,
                probe_stack, sizeof(probe_stack),
                17, 17, TX_NO_TIME_SLICE, TX_DONT_START);

        /* Check for status.  */
        if (status != TX_SUCCESS)
        {

            printf("ERROR #2\n");
            test_control_return(1);
        }

        /* Pick up the pattern the create service wrote into the unused part of the stack.  */
        stack_pattern =  *(((ULONG *) probe_thread.tx_thread_stack_start) + 4);

#ifndef TX_DISABLE_STACK_FILLING
#if defined(TX_ENABLE_RANDOM_NUMBER_STACK_FILLING) && defined(TX_ENABLE_STACK_CHECKING)

        /* The control block must hold the value that was written into the stack.  */
        if (probe_thread.tx_thread_stack_fill_value != stack_pattern)
        {

            printf("ERROR #3\n");
            test_control_return(1);
        }

        /* The random value is duplicated into all four bytes of the fill value.  */
        if ((stack_pattern & 0xFFUL) !=
            ((stack_pattern >> 24) & 0xFFUL))
        {

            printf("ERROR #4\n");
            test_control_return(1);
        }

        if (stack_pattern != ((ULONG) 0))
        {

            non_zero_seen =  TX_TRUE;
        }
#else

        /* Without random filling, the pattern is the fixed one.  */
        if (stack_pattern != ((ULONG) 0xEFEFEFEFUL))
        {

            printf("ERROR #5\n");
            test_control_return(1);
        }

        non_zero_seen =  TX_TRUE;
#endif
#else

        /* Stack filling is disabled, so there is nothing to compare.  */
        non_zero_seen =  TX_TRUE;
#endif

        /* Terminate and delete the probe thread so that the next pass starts from a clean
           slate.  A thread created with TX_DONT_START is suspended rather than completed,
           so it must be terminated before it can be deleted.  */
        status =  tx_thread_terminate(&probe_thread);

        if (status == TX_SUCCESS)
        {

            status =  tx_thread_delete(&probe_thread);
        }

        /* Check for status.  */
        if (status != TX_SUCCESS)
        {

            printf("ERROR #6\n");
            test_control_return(1);
        }
    }

    /* A run in which every fill value came out as zero would prove nothing.  */
    if (non_zero_seen != TX_TRUE)
    {

        printf("ERROR #7\n");
        test_control_return(1);
    }

    /* Success!  */
    printf("SUCCESS!\n");
    test_control_return(0);
}