-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmalloc_test.c
134 lines (120 loc) · 2.69 KB
/
malloc_test.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
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
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <setjmp.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include "./ioctl_poc/query_ioctl.h"
#define SIG_UPCALL 44
struct sigaction sig;
__thread jmp_buf critalSection;
__thread int restartable;
void upcall_handler();
void query_driver();
void myprint(unsigned long val){
char buf[8192];
snprintf(buf, 8192, "%lu printed\n",val);
write(STDOUT_FILENO, buf, strlen(buf) + 1);
}
/*
Queries the Driver for the old instruction pointer in order to
resume operation from where it was supposed to
*/
void query_driver(){
myprint(94444);
char *file_name = "/dev/query";
int fd;
fd = open(file_name, O_RDWR);
if (fd == -1) {
perror("Could not open device file");
exit(1);
}
int v;
registered_proc_t q;
q.pid = getpid();
if (ioctl(fd, _GET_PROC_META, &q) == -1)
{
perror("query ioctl set");
}
}
/*
checks if the process thread was in its critical section
if yes then restarts the critical section by making a longjmp
else it resumes the execution.
*/
void upcall_handler(){
myprint(91111);
if (restartable){
myprint(92222);
longjmp(critalSection, 1);
}else{
myprint(93333);
// resume execution
return;
}
}
/*
Registers the process with the driver
*/
void register_to_driver(){
myprint(33330);
char *file_name = "/dev/query";
int fd;
fd = open(file_name, O_RDWR);
if (fd == -1) {
perror("Could not open device file");
exit(1);
}
int v;
registered_proc_t q;
q.pid = getpid();
if (ioctl(fd, _SET_PROC_META, &q) == -1)
{
perror("query ioctl set");
}
myprint(33331);
}
/*
plants the upcall signal handler in the global scope
*/
void attach_upcall_signal(){
myprint(22222);
sig.sa_sigaction = upcall_handler;
sig.sa_flags = SA_SIGINFO;
sigaction(SIG_UPCALL, &sig, NULL);
}
/*
Global constructor gets called one time when the malloc
library gets loaded in the process environment.
*/
__attribute__((constructor))
void myconstructor() {
myprint(11111);
attach_upcall_signal();
register_to_driver();
}
void restartable_critical_section(){
myprint(66666);
for(int i=0; i<1000; i++);
}
void *malloc(size_t size){
myprint(44444);
restartable = 1;
int r = setjmp(critalSection);
restartable_critical_section();
restartable = 0;
myprint(77777);
for(int i=0; i<100000; i++);
myprint(88888);
return NULL;
}