Skip to content

Commit

Permalink
add test for bug in pthread_get_stacksize_np
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusCalhoun-Lopez committed May 8, 2024
1 parent df195e0 commit 239883b
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/test_pthread_get_stacksize_np.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <pthread.h>
#include <assert.h>
#include <stdio.h>
#include <stddef.h>

#ifdef __arm64__
#define kTooSmallStackSize 0x83000 /* default stack size of non-main thread on Apple silicon */
#else
#define kTooSmallStackSize 0x80000 /* from LLVM: 1 << 19, default stack size of non-main thread */
#endif

void *thread_function(void *arg) {
assert( !pthread_main_np() && "Unknown error, thread should *not* be main." );
return NULL;
}

int main()
{
pthread_t thread;
pthread_t thread_detached;

assert( !pthread_create(&thread, NULL, thread_function, NULL) && "Unknown error, cannot create thread" );
assert( (pthread_get_stacksize_np(thread) == kTooSmallStackSize) && "Stack for non-main thread is not the default value" );
printf("Stack size for non-main thread seems to be ok\n");
pthread_join(thread, NULL);

assert( !pthread_create(&thread_detached, NULL, thread_function, NULL) && "Unknown error, cannot create thread" );
assert( !pthread_detach(thread_detached) && "Unknown error, cannot detach thread" );
assert( (pthread_get_stacksize_np(thread_detached) == kTooSmallStackSize) && "Stack for detached non-main thread is not the default value" );
printf("Stack size for detached detached non-main thread seems to be ok\n");

assert( pthread_main_np() && "Unknown error, thread should be main." );
assert( (pthread_get_stacksize_np(pthread_self()) > kTooSmallStackSize) && "Stack for main thread is too small" );
printf("Stack size for main thread seems to be ok\n");

return 0;
}

0 comments on commit 239883b

Please sign in to comment.