-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstackFrame_x64.cpp
266 lines (240 loc) · 7.37 KB
/
stackFrame_x64.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright 2017 Andrei Pangin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef __x86_64__
#include "stackFrame.h"
#include "vmStructs.h"
#include <errno.h>
#include <string.h>
#include <sys/syscall.h>
#ifdef __APPLE__
#define REG(l, m) _ucontext->uc_mcontext->__ss.__##m
#else
#define REG(l, m) _ucontext->uc_mcontext.gregs[REG_##l]
#endif
uintptr_t &StackFrame::pc() { return (uintptr_t &)REG(RIP, rip); }
uintptr_t &StackFrame::sp() { return (uintptr_t &)REG(RSP, rsp); }
uintptr_t &StackFrame::fp() { return (uintptr_t &)REG(RBP, rbp); }
uintptr_t &StackFrame::retval() { return (uintptr_t &)REG(RAX, rax); }
uintptr_t StackFrame::link() {
// No link register on x86
return 0;
}
uintptr_t StackFrame::arg0() { return (uintptr_t)REG(RDI, rdi); }
uintptr_t StackFrame::arg1() { return (uintptr_t)REG(RSI, rsi); }
uintptr_t StackFrame::arg2() { return (uintptr_t)REG(RDX, rdx); }
uintptr_t StackFrame::arg3() { return (uintptr_t)REG(RCX, rcx); }
uintptr_t StackFrame::jarg0() { return arg1(); }
uintptr_t StackFrame::method() { return (uintptr_t)REG(RBX, rbx); }
uintptr_t StackFrame::senderSP() { return (uintptr_t)REG(R13, r13); }
void StackFrame::ret() {
pc() = stackAt(0);
sp() += 8;
}
bool StackFrame::unwindStub(instruction_t *entry, const char *name,
uintptr_t &pc, uintptr_t &sp, uintptr_t &fp) {
instruction_t *ip = (instruction_t *)pc;
if (ip == entry || *ip == 0xc3 || strncmp(name, "itable", 6) == 0 ||
strncmp(name, "vtable", 6) == 0 ||
strcmp(name, "InlineCacheBuffer") == 0) {
pc = ((uintptr_t *)sp)[0] - 1;
sp += 8;
return true;
}
if (entry == nullptr) {
return false;
}
if (reinterpret_cast<uintptr_t>(entry) % alignof(unsigned int) != 0) {
return false;
}
if (*(unsigned int *)entry == 0xec8b4855) {
// The stub begins with
// push rbp
// mov rbp, rsp
if (ip == entry + 1) {
pc = ((uintptr_t *)sp)[1] - 1;
sp += 16;
return true;
} else if (withinCurrentStack(fp)) {
sp = fp + 16;
fp = ((uintptr_t *)sp)[-2];
pc = ((uintptr_t *)sp)[-1] - 1;
return true;
}
}
return false;
}
bool StackFrame::unwindCompiled(NMethod *nm, uintptr_t &pc, uintptr_t &sp,
uintptr_t &fp) {
instruction_t *ip = (instruction_t *)pc;
instruction_t *entry = (instruction_t *)nm->entry();
if (ip <= entry || *ip == 0xc3 // ret
|| *ip == 0x55 // push rbp
|| ip[-1] == 0x5d // after pop rbp
|| (ip[0] == 0x41 && ip[1] == 0x85 && ip[2] == 0x02 &&
ip[3] == 0xc3)) // poll return
{
// Subtract 1 for PC to point to the call instruction,
// otherwise it may be attributed to a wrong bytecode
pc = ((uintptr_t *)sp)[0] - 1;
sp += 8;
return true;
} else if (*ip == 0x5d) {
// pop rbp
fp = ((uintptr_t *)sp)[0];
pc = ((uintptr_t *)sp)[1] - 1;
sp += 16;
return true;
} else if (ip <= entry + 15 && ((uintptr_t)ip & 0xfff) && ip[-1] == 0x55) {
// push rbp
pc = ((uintptr_t *)sp)[1] - 1;
sp += 16;
return true;
} else if (ip <= entry + 7 && ip[0] == 0x48 && ip[1] == 0x89 &&
ip[2] == 0x6c && ip[3] == 0x24) {
// mov [rsp + #off], rbp
sp += ip[4] + 16;
pc = ((uintptr_t *)sp)[-1] - 1;
return true;
} else if ((ip[0] == 0x41 && ip[1] == 0x81 && ip[2] == 0x7f &&
*(u32 *)(ip + 4) == 1) ||
(ip >= entry + 8 && ip[-8] == 0x41 && ip[-7] == 0x81 &&
ip[-6] == 0x7f && *(u32 *)(ip - 4) == 1)) {
// cmp [r15 + #off], 1
// nmethod_entry_barrier: frame is fully constructed here
sp += nm->frameSize() * sizeof(void *);
fp = ((uintptr_t *)sp)[-2];
pc = ((uintptr_t *)sp)[-1];
return true;
}
return false;
}
void StackFrame::adjustSP(const void *entry, const void *pc, uintptr_t &sp) {
// Not needed
}
bool StackFrame::unwindAtomicStub(const void*& pc) {
// Not needed
return false;
}
// Skip failed MOV instruction by writing 0 to destination register
bool StackFrame::skipFaultInstruction() {
unsigned int insn = *(unsigned int *)pc();
if ((insn & 0x80fff8) == 0x008b48) {
// mov r64, [r64 + offs]
unsigned int reg = ((insn << 1) & 8) | ((insn >> 19) & 7);
switch (reg) {
case 0x0:
REG(RAX, rax) = 0;
break;
case 0x1:
REG(RCX, rcx) = 0;
break;
case 0x2:
REG(RDX, rdx) = 0;
break;
case 0x3:
REG(RBX, rbx) = 0;
break;
case 0x4:
return false; // Do not modify RSP
case 0x5:
REG(RBP, rbp) = 0;
break;
case 0x6:
REG(RSI, rsi) = 0;
break;
case 0x7:
REG(RDI, rdi) = 0;
break;
case 0x8:
REG(R8, r8) = 0;
break;
case 0x9:
REG(R9, r9) = 0;
break;
case 0xa:
REG(R10, r10) = 0;
break;
case 0xb:
REG(R11, r11) = 0;
break;
case 0xc:
REG(R12, r12) = 0;
break;
case 0xd:
REG(R13, r13) = 0;
break;
case 0xe:
REG(R14, r14) = 0;
break;
case 0xf:
REG(R15, r15) = 0;
break;
}
unsigned int insn_size = 3;
if ((insn & 0x070000) == 0x040000)
insn_size++;
if ((insn & 0x400000) == 0x400000)
insn_size++;
pc() += insn_size;
return true;
}
return false;
}
// when accessing the immediate value to read the syscal value, we do not check
// for alignment issue This can have performance penalties, though it is OK on
// x86_64
__attribute__((no_sanitize("address"))) bool
StackFrame::checkInterruptedSyscall() {
#ifdef __APPLE__
// We are not interested in syscalls that do not check error code, e.g.
// semaphore_wait_trap
if (*(instruction_t *)pc() == 0xc3) {
return true;
}
// If CF is set, the error code is in low byte of eax,
// some other syscalls (ulock_wait) do not set CF when interrupted
if (REG(EFL, rflags) & 1) {
return (retval() & 0xff) == EINTR || (retval() & 0xff) == ETIMEDOUT;
} else {
return retval() == (uintptr_t)-EINTR;
}
#else
if (retval() == (uintptr_t)-EINTR) {
// Workaround for JDK-8237858: restart the interrupted poll() manually.
// Check if the previous instruction is mov eax, SYS_poll with infinite
// timeout or mov eax, SYS_ppoll with any timeout (ppoll adjusts timeout
// automatically)
uintptr_t pc = this->pc();
if ((pc & 0xfff) >= 7 && *(instruction_t *)(pc - 7) == 0xb8) {
// mainly to satisfy sanitizer complaints around alignment issue: we rely
// on memcpy
int nr = 0;
memcpy(&nr, (void *)(pc - 6), sizeof(nr));
if (nr == SYS_ppoll || (nr == SYS_poll && (int)REG(RDX, rdx) == -1) ||
(nr == SYS_epoll_wait && (int)REG(R10, r10) == -1) ||
(nr == SYS_epoll_pwait && (int)REG(R10, r10) == -1)) {
this->pc() = pc - 7;
}
}
return true;
}
return false;
#endif
}
bool StackFrame::isSyscall(instruction_t *pc) {
return pc[0] == 0x0f && pc[1] == 0x05;
}
#endif // __x86_64__