-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtam_header.c
72 lines (57 loc) · 1.3 KB
/
tam_header.c
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
#include "tam_header.h"
#define TAM_NO_REVERSE_STACK
#ifdef TAM_NO_REVERSE_STACK
#ifndef TAM_STACK_SIZE
#define TAM_STACK_SIZE -1
#endif
int main(int argc, char* argv[])
{
size_t page_size;
if(TAM_STACK_SIZE < 0)
page_size = sysconf(_SC_PAGESIZE);
else
page_size = TAM_STACK_SIZE;
size_t stack_base[page_size];
return tam_body(stack_base);
}
#else
void init_segv()
{
if(pipe(fd_segv) < 0)
exit(-1);
}
void close_segv()
{
close(fd_segv[0]);
close(fd_segv[1]);
}
bool check_segv(void* addr)
{
return (write(fd_segv[1], addr, sizeof(size_t)) > 0);
}
int main(int argc, char* argv[])
{
init_segv();
size_t page_size = sysconf(_SC_PAGESIZE);
size_t stack_size = 0;
size_t* frame_base = &page_size + 1;
for(size_t step = page_size; step > 0; --step)
{
if(check_segv(frame_base - stack_size))
{
stack_size += step;
++step;
} else {
if(step > stack_size)
stack_size -= stack_size;
else
stack_size -= step;
step /= 2;
}
}
for(size_t i = 0; i < page_size; ++i)
assert(check_segv(frame_base - stack_size + i));
close_segv();
return tam_body(frame_base - stack_size + 1);
}
#endif