7 #include <pthread_spis.h>
9 #include "darwintest_defaults.h"
11 #define CHILD_STACK_COUNT 1024
12 static uint64_t child_stack
[CHILD_STACK_COUNT
];
15 pthread_runner(void* __unused arg
)
17 T_PASS("mach -> pthread conversion successful");
22 mach_bootstrap(void * __unused arg
)
25 pthread_create_from_mach_thread(&thread
, NULL
, pthread_runner
, NULL
);
27 swtch_pri(0); // mach_yield
31 T_DECL(pthread_create_from_mach_thread
, "pthread_create_from_mach_thread",
32 T_META_ALL_VALID_ARCHS(YES
),
33 // Having leaks running will suppress the behavior we are testing
34 T_META_CHECK_LEAKS(false),
35 T_META_ENVVAR("MallocStackLogging=1")
38 T_PASS("MallocStackLogging: %s", getenv("MallocStackLogging"));
40 // Create a mach_thread to start with
41 mach_port_t task
= mach_task_self();
43 thread_state_flavor_t flavor
;
44 mach_msg_type_number_t count
;
46 uintptr_t start_addr
= (uintptr_t)&mach_bootstrap
;
47 // Force alignment to 16-bytes
48 uintptr_t stack_top
= ((uintptr_t)&child_stack
[CHILD_STACK_COUNT
]) & ~0xf;
50 #if defined(__x86_64__)
52 flavor
= x86_THREAD_STATE64
;
53 count
= x86_THREAD_STATE64_COUNT
;
54 x86_thread_state64_t state
= {
56 // Must be 16-byte-off-by-8 aligned <rdar://problem/15886599>
57 .__rsp
= stack_top
- 8,
59 #elif defined(__arm64__)
61 flavor
= ARM_THREAD_STATE64
;
62 count
= ARM_THREAD_STATE64_COUNT
;
63 arm_thread_state64_t state
= { };
64 arm_thread_state64_set_pc_fptr(state
, &mach_bootstrap
);
65 arm_thread_state64_set_sp(state
, stack_top
);
67 #elif defined(__arm__)
69 flavor
= ARM_THREAD_STATE
;
70 count
= ARM_THREAD_STATE_COUNT
;
71 arm_thread_state_t state
= {
77 #error Unknown architecture
80 thread_state_t state_ptr
= (thread_state_t
)&state
;
82 T_PASS("Launching Thread");
84 kern_return_t ret
= thread_create_running(task
, flavor
, state_ptr
, count
, &task_thread
);
85 T_ASSERT_MACH_SUCCESS(ret
, "mach thread created");
88 T_QUIET
; T_ASSERT_POSIX_ZERO(sigemptyset(&empty
), NULL
);
89 while (sigsuspend(&empty
)) {
92 T_FAIL("Didn't wait forever?");