-
Notifications
You must be signed in to change notification settings - Fork 2
/
Signal.cpp
173 lines (161 loc) · 4.08 KB
/
Signal.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <cstring>
#include <climits>
#include <csignal>
#include <csetjmp>
#include <unistd.h>
const int MEMORY_DUMP_RANGE = 20;
const char* regStr[23] = {"R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15",
"RDI", "RSI", "RBP", "RBX", "RDX", "RAX", "RCX", "RSP", "RIP", "EFL", "CSGSFS",
"ERR", "TRAPNO", "OLDMASK", "CR2"};
static jmp_buf jmpBuf;
void print(const char* string)
{
write(1, string, strlen(string));
}
void printNum(long long num)
{
if (num)
{
printNum(num >> 4);
char symbol = num & 15;
if (symbol < 10)
{
symbol += '0';
} else {
symbol += 'a' - 10;
}
write(1, &symbol, 1);
}
}
void print(long long num)
{
if (num)
{
printNum(num);
} else {
print("0");
}
}
void sigsegvAddressHandler(int num, siginfo_t* siginfo, void* context)
{
if (siginfo -> si_signo == SIGSEGV)
{
siglongjmp(jmpBuf, 1);
}
}
void dumpReg(ucontext_t* context)
{
print("Registers dump \n");
for (size_t reg = 0; reg < NGREG; ++reg)
{
print(regStr[reg]);
print(": 0x");
print(context->uc_mcontext.gregs[reg]);
print("\n");
}
}
void dumpMem(void* address)
{
print("Memory dump \n");
long long shift = (long long)((char*) address - MEMORY_DUMP_RANGE);
long long from = 0;
if (shift > 0)
{
from = shift;
}
shift += MEMORY_DUMP_RANGE;
long long to = LONG_LONG_MAX;
if (shift < LONG_LONG_MAX - MEMORY_DUMP_RANGE)
{
to = shift + MEMORY_DUMP_RANGE;
}
for (long long i = from; i < to; ++i)
{
sigset_t setSignal;
sigemptyset(&setSignal);
sigaddset(&setSignal, SIGSEGV);
sigprocmask(SIG_UNBLOCK, &setSignal, nullptr);
struct sigaction sAction {};
sAction.sa_sigaction = sigsegvAddressHandler;
sAction.sa_flags = SA_SIGINFO;
if (sigaction(SIGSEGV, &sAction, nullptr) == -1)
{
print("Error occurred during sigaction\n");
_exit(-1);
}
print("0x");
print(i);
print(" ");
if (setjmp(jmpBuf) != 0)
{
print("failed to dump\n");
} else {
print((int)((const char*)i)[0]);
print("\n");
}
}
}
void sigsegvHandler(int num, siginfo_t* siginfo, void* context)
{
if (siginfo -> si_signo == SIGSEGV)
{
print("Signal rejected: ");
print((const char*) strsignal(num));
print("\n");
if (siginfo -> si_code == SEGV_MAPERR)
{
print("Reason: nothing is mapped to address\n");
} else if(siginfo -> si_code == SEGV_ACCERR) {
print("Reason: access error\n");
} else {
print("Error code: ");
print(siginfo -> si_code);
print("\n");
}
print("Address: ");
if(siginfo -> si_addr == nullptr)
{
print("nullptr. No memory dump needed\n");
} else {
print("0x");
print((intptr_t) siginfo -> si_addr);
print("\n");
}
dumpReg((ucontext_t*) context);
if (siginfo -> si_addr != nullptr)
{
dumpMem(siginfo -> si_addr);
}
}
_exit(1);
}
int main(int argc, char* argv[], char *envp[])
{
struct sigaction action {};
action.sa_sigaction = sigsegvHandler;
action.sa_flags = SA_SIGINFO;
if (sigaction(SIGSEGV, &action, nullptr) == -1)
{
std::cout << "Error occurred during sigaction\n";
return -1;
}
std::cout << "Choose one of the options: \n";
std::cout << "1. Left-bound test\n2. Right-bound test\n3. nullptr test\n";
unsigned short ans = 0;
std::cin >> ans;
if (ans == 1)
{
char* test = (char*) "check";
*(--test) = 'A';
} else if (ans == 2) {
char* test = (char*) "check";
test[6] = '1';
} else if (ans == 3) {
unsigned short* test = nullptr;
*test = 1;
} else {
std::cout << "Incorrect input: expected number of option\n";
}
return 0;
}