-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
148 lines (122 loc) · 3.31 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <tiff.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
char *buffer;
int pagesize;
static void
handler(int sig, siginfo_t *si, void *unused);
void allocate_memory();
uint64 pc;
uint64 rsp;
uint64 rbp;
/**
* jvm safe-point working mechanism,
* this code show how jvm suspend working-thread.
* @param argc
* @param argv
* @return
*/
int main(int argc, char *argv[]) {
/*
* allocate memory and set memory access READ
*/
allocate_memory();
char *p = buffer;
/*
* save context like register pc rsp rbp,
* we manually save those registers to c global variables,
* jvm save those registers for recovery they, we simulate JVM's behavior too
*/
uint64 local_pc = 0;
uint64 local_rsp = 0;
uint64 local_rbp = 0;
__asm__(".intel_syntax;"
"lea rax, [rip];"
"mov [rbp-0x20], rax;"
"mov [rbp-0x28], rsp;"
"mov [rbp-0x30], rbp;"
);
pc = local_pc;
rsp = local_rsp;
rbp = local_rbp;
for (int i = 0; i < 4; i++) {
/*
* we access the pointer p, current thread will handle signal
* jvm will do
*/
*(p) = 'a';
p++;
}
*(p) = '\0';
printf("p = %s\n", buffer);
//printf("%x",local_pc);
/*
* if we didn't restore those registers,
* it should not happen.
*/
printf("Loop completed\n");
exit(EXIT_SUCCESS);
}
void allocate_memory() {
/*
* register signal handle
*/
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
if (sigaction(SIGSEGV, &sa, NULL) == -1)
handle_error("sigaction");
pagesize = sysconf(_SC_PAGE_SIZE);
if (pagesize == -1)
handle_error("sysconf");
/*
* allocate a buffer aligned on a page boundary,
* initial protection is PROT_READ | PROT_WRITE
*/
buffer = memalign(pagesize, 4 * pagesize);
if (buffer == NULL)
handle_error("memalign");
printf("Start of region: 0x%lx\n", (long) buffer);
//set allocated memory access as readonly
if (mprotect(buffer, pagesize * 4,
PROT_READ) == -1)
handle_error("mprotect");
}
static void handler(int sig, siginfo_t *si, void *unused) {
/*
* if memory access signal happen, this handler will be called,
*/
printf("Got SIGSEGV at address: 0x%lx\n",
(long) si->si_addr);
//allow to write , otherwise operation system send signal again.
if (mprotect(buffer, pagesize * 4,
PROT_READ | PROT_WRITE) == -1)
handle_error("mprotect");
/*
* jvm will suspend current thread, wait STW end.
* other GC thread do GC()
*/
//recovery those register
//jmp to previous code , make current thread work
uint64 local_rsp = rsp;
uint64 local_pc = pc;
uint64 local_rbp = rbp;
__asm__(".intel_syntax;"
"mov rsp,[rbp-0x20];"
"mov rax,[rbp-0x28];"
"mov rbp,[rbp-0x30];"
"jmp rax;"
);
//never happen
printf("rsp:%x", local_rsp);
printf("pc:%x", local_pc);
printf("rbp:%x", local_rbp);
exit(EXIT_FAILURE);
}