-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.cc
143 lines (116 loc) · 2.8 KB
/
test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <asm/unistd.h>
#include <signal.h>
#include <stdio.h>
#include <execinfo.h>
#include <exception>
#include <stdlib.h>
template< typename T, size_t n >
static constexpr size_t countof( T (&)[n] ) { return n; }
void backtrace()
{
void *ptrs[100];
backtrace_symbols_fd( ptrs, backtrace( ptrs, countof(ptrs) ), 2 );
}
static bool expecting_abort_backtrace = false;
static bool any_test_failed = false;
[[noreturn]]
void abort_backtrace()
{
if( ! expecting_abort_backtrace ) {
fprintf( stderr, "\n*** Unexpected unhandled exception ***\n" );
any_test_failed = true;
}
backtrace();
if( any_test_failed ) {
fprintf( stderr, "\nOne or more tests failed\n" );
abort();
} else {
fprintf( stderr, "\nAll tests passed\n" );
exit( 0 );
}
}
struct IntendedUncaughtException {};
struct SegFault {};
void backtrace_handler( int )
{
backtrace();
throw SegFault {};
}
void usr1_handler( int )
{
}
void segv_handler( int )
{
// clobber caller-save VFP register to verify it is restored
register float f asm("s0") = 42;
asm volatile( "" :: "t"(f) );
throw SegFault {};
}
// compile with hardfp and optimization to ensure f ends up in s0 and stays
// here until the fprintf
[[gnu::noinline]]
void test_segv( float f = 0 )
{
int volatile *p = NULL;
asm volatile( "" : "+r"(p) );
asm volatile( "" : "+t"(f) );
try {
(void) *p;
} catch( SegFault ) {
f += 1;
}
if( f != 1 )
any_test_failed = true;
fprintf( stderr, "%s\n", f == 1 ? "ok" : "FAIL" );
}
[[gnu::noinline]]
int foo( unsigned n );
[[gnu::noinline]]
int bar( unsigned n ) {
if( n > 0 )
return foo( n-1 );
test_segv();
return 1;
}
[[gnu::noinline]]
int foo( unsigned n ) {
bar( n );
return 0;
}
int main()
{
std::set_terminate( abort_backtrace );
struct sigaction act {};
fprintf( stderr, "backtrace in segv...\n" );
act.sa_handler = backtrace_handler;
act.sa_flags = SA_SIGINFO | SA_RESETHAND;
sigaction( SIGSEGV, &act, NULL );
foo( 3 );
fprintf( stderr, "\n" );
fprintf( stderr, "via return (no siginfo)... " );
act.sa_handler = usr1_handler;
act.sa_flags = SA_RESETHAND;
sigaction( SIGUSR1, &act, NULL );
raise( SIGUSR1 );
printf( "ok\n" );
fprintf( stderr, "via return (with siginfo)... " );
act.sa_handler = usr1_handler;
act.sa_flags = SA_SIGINFO | SA_RESETHAND;
sigaction( SIGUSR1, &act, NULL );
raise( SIGUSR1 );
printf( "ok\n" );
fprintf( stderr, "via exception (no siginfo)... " );
act.sa_handler = segv_handler;
act.sa_flags = SA_RESETHAND;
sigaction( SIGSEGV, &act, NULL );
test_segv();
fprintf( stderr, "via exception (with siginfo)... " );
act.sa_handler = segv_handler;
act.sa_flags = SA_SIGINFO | SA_RESETHAND;
sigaction( SIGSEGV, &act, NULL );
test_segv();
fprintf( stderr, "\n" );
fprintf( stderr, "uncaught exception:\n" );
expecting_abort_backtrace = true;
throw IntendedUncaughtException {};
}