Skip to content

Commit c5f6668

Browse files
committed
samples: tflite_ethosu: fix unsafe thread stack allocation
It is unsafe to pass raw buffer as thread stack. Instead, use canonical K_THREAD_STACK_DEFINE or friends to define thread stack. Signed-off-by: Chun-Chieh Li <[email protected]>
1 parent b6d3e23 commit c5f6668

File tree

1 file changed

+10
-16
lines changed
  • samples/modules/tflite-micro/tflm_ethosu/src

1 file changed

+10
-16
lines changed

samples/modules/tflite-micro/tflm_ethosu/src/main.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,15 @@ int main()
225225
k_queue_init(&inferenceQueue);
226226

227227
/* inferenceSender tasks to create and queue the jobs */
228+
const size_t inferenceSenderStackSize = 2048;
229+
static K_THREAD_STACK_ARRAY_DEFINE(inferenceSenderStacks, NUM_JOB_TASKS,
230+
inferenceSenderStackSize);
228231
for (int n = 0; n < NUM_JOB_TASKS; n++) {
229-
const size_t stackSize = 2048;
230-
k_thread_stack_t *stack = static_cast<k_thread_stack_t *>(k_malloc(stackSize));
231-
if (stack == nullptr) {
232-
printf("Failed to allocate stack to 'inferenceSenderTask%i'\n", n);
233-
exit(1);
234-
}
235-
236232
auto &thread = threads[nthreads];
237233
string *name = new string("sender " + to_string(n));
238234

239-
thread.id = k_thread_create(&thread.thread, stack, stackSize, inferenceSenderTask,
235+
thread.id = k_thread_create(&thread.thread, inferenceSenderStacks[n],
236+
inferenceSenderStackSize, inferenceSenderTask,
240237
name, heapPtr, &inferenceQueue, 3, 0, K_FOREVER);
241238
if (thread.id == 0) {
242239
printf("Failed to create 'inferenceSenderTask%i'\n", n);
@@ -248,21 +245,18 @@ int main()
248245

249246
/* Create inferenceProcess tasks to process the queued jobs */
250247
InferenceProcessParams taskParams[NUM_INFERENCE_TASKS];
248+
const size_t inferenceProcessStackSize = 8192;
249+
static K_THREAD_STACK_ARRAY_DEFINE(inferenceProcessStacks, NUM_INFERENCE_TASKS,
250+
inferenceProcessStackSize);
251251
for (int n = 0; n < NUM_INFERENCE_TASKS; n++) {
252-
const size_t stackSize = 8192;
253-
k_thread_stack_t *stack = static_cast<k_thread_stack_t *>(k_malloc(stackSize));
254-
if (stack == nullptr) {
255-
printf("Failed to allocate stack to 'inferenceSenderTask%i'\n", n);
256-
exit(1);
257-
}
258-
259252
auto &thread = threads[nthreads];
260253
auto &taskParam = taskParams[n];
261254
taskParam = InferenceProcessParams(&inferenceQueue, inferenceProcessTensorArena[n],
262255
arenaSize);
263256
string *name = new string("runner " + to_string(n));
264257

265-
thread.id = k_thread_create(&thread.thread, stack, stackSize, inferenceProcessTask,
258+
thread.id = k_thread_create(&thread.thread, inferenceProcessStacks[n],
259+
inferenceProcessStackSize, inferenceProcessTask,
266260
name, heapPtr, &taskParam, 2, 0, K_FOREVER);
267261
if (thread.id == 0) {
268262
printf("Failed to create 'inferenceProcessTask%i'\n", n);

0 commit comments

Comments
 (0)