xref: /haiku/src/tests/system/glue/StackAlign.cpp (revision d87218f79e132dc809e44f01be114bf279082949)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <OS.h>
5 
6 
7 uint32 gStackPointer;
8 
9 #define CHECK_STACK_ALIGN(from) \
10 	asm volatile ("mov %%esp, %0" : "=r" (gStackPointer) :); \
11 	if (gStackPointer & 0xF) \
12 		printf("In %s, stack is NOT aligned: %lx\n", from, gStackPointer); \
13 	else \
14 		printf("In %s, stack is aligned!\n", from);
15 
function(void)16 int function(void)
17 {
18 	CHECK_STACK_ALIGN("function");
19 
20 	return 0;
21 }
22 
thread(void * arg)23 status_t thread(void* arg)
24 {
25 	CHECK_STACK_ALIGN("thread");
26 
27 	return B_OK;
28 }
29 
handler(int param)30 void handler(int param)
31 {
32 	CHECK_STACK_ALIGN("signal");
33 }
34 
main(void)35 int main(void)
36 {
37 	CHECK_STACK_ALIGN("main");
38 
39 	// Test from called function
40 	function();
41 
42 	// Test from thread
43 	{
44 		status_t rv;
45 		wait_for_thread(spawn_thread(thread, "test", B_NORMAL_PRIORITY, NULL), &rv);
46 	}
47 
48 	// Test from signal handler
49 	{
50 		stack_t signalStack;
51 		struct sigaction action;
52 
53 		signalStack.ss_sp = malloc(SIGSTKSZ);
54 		signalStack.ss_flags = 0;
55 		signalStack.ss_size = SIGSTKSZ;
56 		sigaltstack(&signalStack, NULL);
57 
58 		action.sa_handler = handler;
59 		action.sa_mask = 0;
60 		action.sa_flags = SA_ONSTACK;
61 		sigaction(SIGUSR1, &action, NULL);
62 
63 		kill(getpid(), SIGUSR1);
64 	}
65 
66 	return 0;
67 }
68 
69 struct Foo {
FooFoo70 	Foo()
71 	{
72 		CHECK_STACK_ALIGN("init");
73 	}
74 
~FooFoo75 	~Foo()
76 	{
77 		CHECK_STACK_ALIGN("fini");
78 	}
79 };
80 
81 Foo init;
82