From d288c149d51ca5b070a25296665f261ffd391b38 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Sun, 10 Apr 2022 22:13:52 -0400 Subject: [PATCH] tests: posix: dynamic stack support for pthread_create Tests for calling `pthread_create()` `attr == NULL` or with `attr.stack == NULL` or with `attr.stacksize == 0`. Signed-off-by: Christopher Friedt --- tests/posix/common/src/pthread.c | 52 ++++++++++++++++++++++++++------ tests/posix/common/testcase.yaml | 16 ++++++++++ 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/tests/posix/common/src/pthread.c b/tests/posix/common/src/pthread.c index 4cda28fe34b3108..ff1e57e9e0a39e4 100644 --- a/tests/posix/common/src/pthread.c +++ b/tests/posix/common/src/pthread.c @@ -337,8 +337,13 @@ void test_posix_pthread_execution(void) getschedparam.sched_priority, "scheduling priorities do not match!"); - ret = pthread_create(&newthread[i], &attr[i], thread_top_exec, - INT_TO_POINTER(i)); + if (IS_ENABLED(CONFIG_PTHREAD_DYNAMIC_STACK)) { + ret = pthread_create(&newthread[i], NULL, thread_top_exec, + INT_TO_POINTER(i)); + } else { + ret = pthread_create(&newthread[i], &attr[i], thread_top_exec, + INT_TO_POINTER(i)); + } /* TESTPOINT: Check if thread is created successfully */ zassert_false(ret, "Number of threads exceed max limit"); @@ -495,8 +500,13 @@ void test_posix_pthread_termination(void) schedparam.sched_priority = 2; pthread_attr_setschedparam(&attr[i], &schedparam); pthread_attr_setstack(&attr[i], &stack_t[i][0], STACKS); - ret = pthread_create(&newthread[i], &attr[i], thread_top_term, - INT_TO_POINTER(i)); + if (IS_ENABLED(CONFIG_PTHREAD_DYNAMIC_STACK)) { + ret = pthread_create(&newthread[i], NULL, thread_top_term, + INT_TO_POINTER(i)); + } else { + ret = pthread_create(&newthread[i], &attr[i], thread_top_term, + INT_TO_POINTER(i)); + } zassert_false(ret, "Not enough space to create new thread"); } @@ -540,23 +550,45 @@ static void *create_thread1(void *p1) void test_posix_pthread_create_negative(void) { int ret; + void *unused; pthread_t pthread1; pthread_attr_t attr1; /* create pthread without attr initialized */ - ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1); - zassert_equal(ret, EINVAL, "create thread with NULL successful"); + if (IS_ENABLED(CONFIG_DYNAMIC_PTHREAD_STACKS)) { + ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1); + zassert_equal(ret, 0, "create thread with NULL attr not successful"); + ret = pthread_join(&pthread1, &unused); + zassert_equal(ret, 0, "pthread_join() failed"); + } else { + ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1); + zassert_equal(ret, EINVAL, "create thread with NULL attr successful"); + } /* initialized attr without set stack to create thread */ ret = pthread_attr_init(&attr1); zassert_false(ret, "attr1 initialized failed"); attr1.stack = NULL; - ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1); - zassert_equal(ret, EINVAL, "create successful with NULL attr"); + if (IS_ENABLED(CONFIG_DYNAMIC_PTHREAD_STACKS)) { + ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1); + zassert_equal(ret, 0, "create not successful with NULL stack"); + ret = pthread_join(&pthread1, &unused); + zassert_equal(ret, 0, "pthread_join() failed"); + } else { + ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1); + zassert_equal(ret, EINVAL, "create successful with NULL stack"); + } /* set stack size 0 to create thread */ pthread_attr_setstack(&attr1, &stack_1, 0); - ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1); - zassert_equal(ret, EINVAL, "create thread with 0 size"); + if (IS_ENABLED(CONFIG_DYNAMIC_PTHREAD_STACKS)) { + ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1); + zassert_equal(ret, 0, "create thread with 0 size failed"); + ret = pthread_join(&pthread1, &unused); + zassert_equal(ret, 0, "pthread_join() failed"); + } else { + ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1); + zassert_equal(ret, EINVAL, "create thread with 0 size"); + } } diff --git a/tests/posix/common/testcase.yaml b/tests/posix/common/testcase.yaml index 9d875d4a38c6c55..913e69bc24efe83 100644 --- a/tests/posix/common/testcase.yaml +++ b/tests/posix/common/testcase.yaml @@ -43,3 +43,19 @@ tests: extra_configs: - CONFIG_NEWLIB_LIBC=y - CONFIG_TEST_HW_STACK_PROTECTION=n + portability.posix.common.dynamic_stack: + platform_exclude: nsim_sem_mpu_stack_guard + extra_configs: + - CONFIG_NEWLIB_LIBC=n + - CONFIG_THREAD_STACK_INFO=y + - CONFIG_DYNAMIC_THREAD_STACK=y + - CONFIG_DYNAMIC_PTHREAD_STACK=y + - CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=16384 + portability.posix.common.dynamic_stack.newlib: + platform_exclude: nsim_sem_mpu_stack_guard + filter: TOOLCHAIN_HAS_NEWLIB == 1 + extra_configs: + - CONFIG_NEWLIB_LIBC=y + - CONFIG_THREAD_STACK_INFO=y + - CONFIG_DYNAMIC_THREAD_STACKS=y + - CONFIG_DYNAMIC_PTHREAD_STACKS=y