1 // Test pthread_create_key, pthread_delete_key, pthread_setspecific, pthread_getspecific
2 #include <pthread.h>
3 #include "unity.h"
4 #include "freertos/FreeRTOS.h"
5 #include "freertos/task.h"
6 #include "test_utils.h"
7 #include "esp_system.h"
8 
9 TEST_CASE("pthread local storage basics", "[pthread]")
10 {
11     pthread_key_t key;
12     TEST_ASSERT_EQUAL(0, pthread_key_create(&key, NULL));
13 
14     TEST_ASSERT_NULL(pthread_getspecific(key));
15     int val = 3;
16 
17     printf("Setting to %p...\n", &val);
18     TEST_ASSERT_EQUAL(0, pthread_setspecific(key, &val));
19 
20     printf("Reading back...\n");
21     TEST_ASSERT_EQUAL_PTR(&val, pthread_getspecific(key));
22 
23     printf("Setting to NULL...\n");
24     TEST_ASSERT_EQUAL(0, pthread_setspecific(key, NULL));
25 
26     printf("Reading back...\n");
27     TEST_ASSERT_NULL(pthread_getspecific(key));
28 
29     TEST_ASSERT_EQUAL(0, pthread_key_delete(key));
30 }
31 
32 TEST_CASE("pthread local storage unique keys", "[pthread]")
33 {
34     const int NUM_KEYS = 10;
35     pthread_key_t keys[NUM_KEYS];
36 
37     for (int i = 0; i < NUM_KEYS; i++) {
38         TEST_ASSERT_EQUAL(0, pthread_key_create(&keys[i], NULL));
39         printf("New key %d = %d\n", i, keys[i]);
40     }
41 
42     for (int i = 0; i < NUM_KEYS; i++) {
43         for (int j = 0; j < NUM_KEYS; j++) {
44             if (i != j) {
45                 TEST_ASSERT_NOT_EQUAL(keys[i], keys[j]);
46             }
47         }
48     }
49 
50     for (int i = 0; i < NUM_KEYS; i++) {
51         TEST_ASSERT_EQUAL(0, pthread_key_delete(keys[i]));
52     }
53 }
54 
55 static void test_pthread_destructor(void *);
56 static void *expected_destructor_ptr;
57 static void *actual_destructor_ptr;
58 static void *thread_test_pthread_destructor(void *);
59 
60 TEST_CASE("pthread local storage destructor", "[pthread]")
61 {
62     pthread_t thread;
63     pthread_key_t key = -1;
64 
65     expected_destructor_ptr = NULL;
66     actual_destructor_ptr = NULL;
67 
68     TEST_ASSERT_EQUAL(0, pthread_key_create(&key, test_pthread_destructor));
69 
70     TEST_ASSERT_EQUAL(0, pthread_create(&thread, NULL, thread_test_pthread_destructor, (void *)key));
71     TEST_ASSERT_EQUAL(0, pthread_join(thread, NULL));
72 
73     printf("Joined...\n");
74     TEST_ASSERT_NOT_NULL(expected_destructor_ptr);
75     TEST_ASSERT_NOT_NULL(actual_destructor_ptr);
76     TEST_ASSERT_EQUAL_PTR(expected_destructor_ptr, actual_destructor_ptr);
77 
78     TEST_ASSERT_EQUAL(0, pthread_key_delete(key));
79 }
80 
81 static void task_test_pthread_destructor(void *v_key);
82 
83 TEST_CASE("pthread local storage destructor in FreeRTOS task", "[pthread]")
84 {
85     // Same as previous test case, but doesn't use pthread APIs therefore must wait
86     // for the idle task to call the destructor
87     pthread_key_t key = -1;
88 
89     expected_destructor_ptr = NULL;
90     actual_destructor_ptr = NULL;
91 
92     TEST_ASSERT_EQUAL(0, pthread_key_create(&key, test_pthread_destructor));
93 
94     xTaskCreate(task_test_pthread_destructor,
95                 "ptdest", 8192, (void *)key, UNITY_FREERTOS_PRIORITY+1,
96                 NULL);
97 
98     // Above task has higher priority to us, so should run immediately
99     // but we need to wait for the idle task cleanup to run
100     vTaskDelay(20);
101 
102     TEST_ASSERT_NOT_NULL(expected_destructor_ptr);
103     TEST_ASSERT_NOT_NULL(actual_destructor_ptr);
104     TEST_ASSERT_EQUAL_PTR(expected_destructor_ptr, actual_destructor_ptr);
105 
106     TEST_ASSERT_EQUAL(0, pthread_key_delete(key));
107 }
108 
109 
110 
thread_test_pthread_destructor(void * v_key)111 static void *thread_test_pthread_destructor(void *v_key)
112 {
113     printf("Local storage thread running...\n");
114     pthread_key_t key = (pthread_key_t) v_key;
115     expected_destructor_ptr = &key; // address of stack variable in the task...
116     pthread_setspecific(key, expected_destructor_ptr);
117     printf("Local storage thread done.\n");
118     return NULL;
119 }
120 
test_pthread_destructor(void * value)121 static void test_pthread_destructor(void *value)
122 {
123     actual_destructor_ptr = value;
124 }
125 
task_test_pthread_destructor(void * v_key)126 static void task_test_pthread_destructor(void *v_key)
127 {
128     /* call the pthread main routine, then delete ourselves... */
129     thread_test_pthread_destructor(v_key);
130     vTaskDelete(NULL);
131 }
132 
133 #define STRESS_NUMITER 2000000
134 #define STRESS_NUMTASKS 16
135 
thread_stress_test(void * v_key)136 static void *thread_stress_test(void *v_key)
137 {
138     pthread_key_t key = (pthread_key_t) v_key;
139     void *tls_value = (void *)esp_random();
140 
141     pthread_setspecific(key, tls_value);
142 
143     for(int i = 0; i < STRESS_NUMITER; i++) {
144         TEST_ASSERT_EQUAL_HEX32(pthread_getspecific(key), tls_value);
145     }
146 
147     return NULL;
148 }
149 
150 
151 // This test case added to reproduce issues with unpinned tasks and TLS
152 TEST_CASE("pthread local storage stress test", "[pthread]")
153 {
154     pthread_key_t key = -1;
155     pthread_t threads[STRESS_NUMTASKS] = { 0 };
156     TEST_ASSERT_EQUAL(0, pthread_key_create(&key, test_pthread_destructor));
157 
158     for (int i = 0; i < STRESS_NUMTASKS; i++) {
159         TEST_ASSERT_EQUAL(0, pthread_create(&threads[i], NULL, thread_stress_test, (void *)key));
160     }
161     for (int i = 0; i < STRESS_NUMTASKS; i++) {
162         TEST_ASSERT_EQUAL(0, pthread_join(threads[i], NULL));
163     }
164 }
165 
166 
167 #define NUM_KEYS 4 // number of keys used in repeat destructor test
168 #define NUM_REPEATS 17 // number of times we re-set a key to a non-NULL value to re-trigger destructor
169 
170 typedef struct {
171     pthread_key_t keys[NUM_KEYS]; // pthread local storage keys used in test
172     unsigned count; // number of times the destructor has been called
173     int last_idx; // index of last key where destructor was called
174 } destr_test_state_t;
175 
176 static void s_test_repeat_destructor(void *vp_state);
177 static void *s_test_repeat_destructor_thread(void *vp_state);
178 
179 // Test the correct behaviour of a pthread destructor function that uses
180 // pthread_setspecific() to set another value when it runs, and also
181 //
182 // As described in https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html
183 TEST_CASE("pthread local storage 'repeat' destructor test", "[pthread]")
184 {
185     int r;
186     destr_test_state_t state = { .last_idx = -1 };
187     pthread_t thread;
188 
189     for (int i = 0; i < NUM_KEYS; i++) {
190         r = pthread_key_create(&state.keys[i], s_test_repeat_destructor);
191         TEST_ASSERT_EQUAL(0, r);
192     }
193 
194     r = pthread_create(&thread, NULL, s_test_repeat_destructor_thread, &state);
195     TEST_ASSERT_EQUAL(0, r);
196 
197     r = pthread_join(thread, NULL);
198     TEST_ASSERT_EQUAL(0 ,r);
199 
200     // Cheating here to make sure compiler reads the value of 'count' from memory not from a register
201     //
202     // We expect the destructor was called NUM_REPEATS times when it repeated, then NUM_KEYS times when it didn't
203     TEST_ASSERT_EQUAL(NUM_REPEATS + NUM_KEYS, ((volatile destr_test_state_t)state).count);
204 
205     // cleanup
206     for (int i = 0; i < NUM_KEYS; i++) {
207         r = pthread_key_delete(state.keys[i]);
208         TEST_ASSERT_EQUAL(0, r);
209     }
210 }
211 
s_test_repeat_destructor(void * vp_state)212 static void s_test_repeat_destructor(void *vp_state)
213 {
214     destr_test_state_t *state = vp_state;
215 
216     state->count++;
217     printf("Destructor! Arg %p Count %d\n", state, state->count);
218     if (state->count > NUM_REPEATS) {
219         return; // Stop replacing values after NUM_REPEATS destructors have been called, they will be NULLed out now
220     }
221 
222     // Find the key which has a NULL value, this is the key for this destructor. We will set it back to 'state' to repeat later.
223     // At this point only one key should have a NULL value
224     int null_idx = -1;
225     for (int i = 0; i < NUM_KEYS; i++) {
226         if (pthread_getspecific(state->keys[i]) == NULL) {
227             TEST_ASSERT_EQUAL(-1, null_idx); // If more than one key has a NULL value, something has gone wrong
228             null_idx = i;
229             // don't break, verify the other keys have non-NULL values
230         }
231     }
232 
233     TEST_ASSERT_NOT_EQUAL(-1, null_idx); // One key should have a NULL value
234 
235     // The same key shouldn't be destroyed twice in a row, as new non-NULL values should be destroyed
236     // after existing non-NULL values (to match spec behaviour)
237     TEST_ASSERT_NOT_EQUAL(null_idx, state->last_idx);
238 
239     printf("Re-setting index %d\n", null_idx);
240     pthread_setspecific(state->keys[null_idx], state);
241     state->last_idx = null_idx;
242 }
243 
s_test_repeat_destructor_thread(void * vp_state)244 static void *s_test_repeat_destructor_thread(void *vp_state)
245 {
246     destr_test_state_t *state = vp_state;
247     for (int i = 0; i < NUM_KEYS; i++) {
248         pthread_setspecific(state->keys[i], state);
249     }
250     pthread_exit(NULL);
251 }
252