blob: 33319b1ed39ae855e350283c911dc2d55ba74609 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Christopher Ferris3958f802017-02-01 15:44:40 -080017#include <elf.h>
18#include <stdint.h>
19#include <sys/ptrace.h>
20#include <sys/uio.h>
21
22#include <vector>
23
Christopher Ferrisd226a512017-07-14 10:37:19 -070024#include <unwindstack/Elf.h>
25#include <unwindstack/MapInfo.h>
26#include <unwindstack/Memory.h>
27#include <unwindstack/Regs.h>
28
Christopher Ferris94167032017-06-28 18:56:52 -070029#include "Check.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080030#include "Machine.h"
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070031#include "Ucontext.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080032#include "User.h"
33
Christopher Ferrisd226a512017-07-14 10:37:19 -070034namespace unwindstack {
Christopher Ferris3958f802017-02-01 15:44:40 -080035
36template <typename AddressType>
Christopher Ferris7b8e4672017-06-01 17:55:25 -070037bool RegsImpl<AddressType>::GetReturnAddressFromDefault(Memory* memory, uint64_t* value) {
Christopher Ferris3958f802017-02-01 15:44:40 -080038 switch (return_loc_.type) {
39 case LOCATION_REGISTER:
Christopher Ferris94167032017-06-28 18:56:52 -070040 CHECK(return_loc_.value < total_regs_);
Christopher Ferris3958f802017-02-01 15:44:40 -080041 *value = regs_[return_loc_.value];
42 return true;
43 case LOCATION_SP_OFFSET:
44 AddressType return_value;
45 if (!memory->Read(sp_ + return_loc_.value, &return_value, sizeof(return_value))) {
46 return false;
47 }
48 *value = return_value;
49 return true;
50 case LOCATION_UNKNOWN:
51 default:
52 return false;
53 }
54}
55
Christopher Ferris7b8e4672017-06-01 17:55:25 -070056RegsArm::RegsArm()
57 : RegsImpl<uint32_t>(ARM_REG_LAST, ARM_REG_SP, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -080058
59uint64_t RegsArm::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
60 if (!elf->valid()) {
61 return rel_pc;
62 }
63
64 uint64_t load_bias = elf->interface()->load_bias();
65 if (rel_pc < load_bias) {
66 return rel_pc;
67 }
68 uint64_t adjusted_rel_pc = rel_pc - load_bias;
69
70 if (adjusted_rel_pc < 5) {
71 return rel_pc;
72 }
73
74 if (adjusted_rel_pc & 1) {
75 // This is a thumb instruction, it could be 2 or 4 bytes.
76 uint32_t value;
77 if (rel_pc < 5 || !elf->memory()->Read(adjusted_rel_pc - 5, &value, sizeof(value)) ||
78 (value & 0xe000f000) != 0xe000f000) {
79 return rel_pc - 2;
80 }
81 }
82 return rel_pc - 4;
83}
84
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070085void RegsArm::SetFromRaw() {
86 set_pc(regs_[ARM_REG_PC]);
87 set_sp(regs_[ARM_REG_SP]);
88}
89
Christopher Ferris7b8e4672017-06-01 17:55:25 -070090RegsArm64::RegsArm64()
91 : RegsImpl<uint64_t>(ARM64_REG_LAST, ARM64_REG_SP, Location(LOCATION_REGISTER, ARM64_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -080092
93uint64_t RegsArm64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
94 if (!elf->valid()) {
95 return rel_pc;
96 }
97
98 if (rel_pc < 4) {
99 return rel_pc;
100 }
101 return rel_pc - 4;
102}
103
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700104void RegsArm64::SetFromRaw() {
105 set_pc(regs_[ARM64_REG_PC]);
106 set_sp(regs_[ARM64_REG_SP]);
107}
108
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700109RegsX86::RegsX86()
110 : RegsImpl<uint32_t>(X86_REG_LAST, X86_REG_SP, Location(LOCATION_SP_OFFSET, -4)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800111
112uint64_t RegsX86::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
113 if (!elf->valid()) {
114 return rel_pc;
115 }
116
117 if (rel_pc == 0) {
118 return 0;
119 }
120 return rel_pc - 1;
121}
122
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700123void RegsX86::SetFromRaw() {
124 set_pc(regs_[X86_REG_PC]);
125 set_sp(regs_[X86_REG_SP]);
126}
127
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700128RegsX86_64::RegsX86_64()
129 : RegsImpl<uint64_t>(X86_64_REG_LAST, X86_64_REG_SP, Location(LOCATION_SP_OFFSET, -8)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800130
131uint64_t RegsX86_64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
132 if (!elf->valid()) {
133 return rel_pc;
134 }
135
136 if (rel_pc == 0) {
137 return 0;
138 }
139
140 return rel_pc - 1;
141}
142
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700143void RegsX86_64::SetFromRaw() {
144 set_pc(regs_[X86_64_REG_PC]);
145 set_sp(regs_[X86_64_REG_SP]);
146}
147
Christopher Ferris3958f802017-02-01 15:44:40 -0800148static Regs* ReadArm(void* remote_data) {
149 arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
150
151 RegsArm* regs = new RegsArm();
152 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700153 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800154 return regs;
155}
156
157static Regs* ReadArm64(void* remote_data) {
158 arm64_user_regs* user = reinterpret_cast<arm64_user_regs*>(remote_data);
159
160 RegsArm64* regs = new RegsArm64();
161 memcpy(regs->RawData(), &user->regs[0], (ARM64_REG_R31 + 1) * sizeof(uint64_t));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700162 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
163 reg_data[ARM64_REG_PC] = user->pc;
164 reg_data[ARM64_REG_SP] = user->sp;
165 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800166 return regs;
167}
168
169static Regs* ReadX86(void* remote_data) {
170 x86_user_regs* user = reinterpret_cast<x86_user_regs*>(remote_data);
171
172 RegsX86* regs = new RegsX86();
173 (*regs)[X86_REG_EAX] = user->eax;
174 (*regs)[X86_REG_EBX] = user->ebx;
175 (*regs)[X86_REG_ECX] = user->ecx;
176 (*regs)[X86_REG_EDX] = user->edx;
177 (*regs)[X86_REG_EBP] = user->ebp;
178 (*regs)[X86_REG_EDI] = user->edi;
179 (*regs)[X86_REG_ESI] = user->esi;
180 (*regs)[X86_REG_ESP] = user->esp;
181 (*regs)[X86_REG_EIP] = user->eip;
182
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700183 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800184 return regs;
185}
186
187static Regs* ReadX86_64(void* remote_data) {
188 x86_64_user_regs* user = reinterpret_cast<x86_64_user_regs*>(remote_data);
189
190 RegsX86_64* regs = new RegsX86_64();
191 (*regs)[X86_64_REG_RAX] = user->rax;
192 (*regs)[X86_64_REG_RBX] = user->rbx;
193 (*regs)[X86_64_REG_RCX] = user->rcx;
194 (*regs)[X86_64_REG_RDX] = user->rdx;
195 (*regs)[X86_64_REG_R8] = user->r8;
196 (*regs)[X86_64_REG_R9] = user->r9;
197 (*regs)[X86_64_REG_R10] = user->r10;
198 (*regs)[X86_64_REG_R11] = user->r11;
199 (*regs)[X86_64_REG_R12] = user->r12;
200 (*regs)[X86_64_REG_R13] = user->r13;
201 (*regs)[X86_64_REG_R14] = user->r14;
202 (*regs)[X86_64_REG_R15] = user->r15;
203 (*regs)[X86_64_REG_RDI] = user->rdi;
204 (*regs)[X86_64_REG_RSI] = user->rsi;
205 (*regs)[X86_64_REG_RBP] = user->rbp;
206 (*regs)[X86_64_REG_RSP] = user->rsp;
207 (*regs)[X86_64_REG_RIP] = user->rip;
208
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700209 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800210 return regs;
211}
212
213// This function assumes that reg_data is already aligned to a 64 bit value.
214// If not this could crash with an unaligned access.
215Regs* Regs::RemoteGet(pid_t pid, uint32_t* machine_type) {
216 // Make the buffer large enough to contain the largest registers type.
217 std::vector<uint64_t> buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t));
218 struct iovec io;
219 io.iov_base = buffer.data();
220 io.iov_len = buffer.size() * sizeof(uint64_t);
221
222 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) {
223 return nullptr;
224 }
225
226 switch (io.iov_len) {
227 case sizeof(x86_user_regs):
228 *machine_type = EM_386;
229 return ReadX86(buffer.data());
230 case sizeof(x86_64_user_regs):
231 *machine_type = EM_X86_64;
232 return ReadX86_64(buffer.data());
233 case sizeof(arm_user_regs):
234 *machine_type = EM_ARM;
235 return ReadArm(buffer.data());
236 case sizeof(arm64_user_regs):
237 *machine_type = EM_AARCH64;
238 return ReadArm64(buffer.data());
239 }
240 return nullptr;
241}
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700242
243static Regs* CreateFromArmUcontext(void* ucontext) {
244 arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
245
246 RegsArm* regs = new RegsArm();
247 memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
248 regs->SetFromRaw();
249 return regs;
250}
251
252static Regs* CreateFromArm64Ucontext(void* ucontext) {
253 arm64_ucontext_t* arm64_ucontext = reinterpret_cast<arm64_ucontext_t*>(ucontext);
254
255 RegsArm64* regs = new RegsArm64();
256 memcpy(regs->RawData(), &arm64_ucontext->uc_mcontext.regs[0], ARM64_REG_LAST * sizeof(uint64_t));
257 regs->SetFromRaw();
258 return regs;
259}
260
Christopher Ferrisa0196652017-07-18 16:09:20 -0700261void RegsX86::SetFromUcontext(x86_ucontext_t* ucontext) {
262 // Put the registers in the expected order.
263 regs_[X86_REG_EDI] = ucontext->uc_mcontext.edi;
264 regs_[X86_REG_ESI] = ucontext->uc_mcontext.esi;
265 regs_[X86_REG_EBP] = ucontext->uc_mcontext.ebp;
266 regs_[X86_REG_ESP] = ucontext->uc_mcontext.esp;
267 regs_[X86_REG_EBX] = ucontext->uc_mcontext.ebx;
268 regs_[X86_REG_EDX] = ucontext->uc_mcontext.edx;
269 regs_[X86_REG_ECX] = ucontext->uc_mcontext.ecx;
270 regs_[X86_REG_EAX] = ucontext->uc_mcontext.eax;
271 regs_[X86_REG_EIP] = ucontext->uc_mcontext.eip;
272 SetFromRaw();
273}
274
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700275static Regs* CreateFromX86Ucontext(void* ucontext) {
276 x86_ucontext_t* x86_ucontext = reinterpret_cast<x86_ucontext_t*>(ucontext);
277
278 RegsX86* regs = new RegsX86();
Christopher Ferrisa0196652017-07-18 16:09:20 -0700279 regs->SetFromUcontext(x86_ucontext);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700280 return regs;
281}
282
Christopher Ferrisa0196652017-07-18 16:09:20 -0700283void RegsX86_64::SetFromUcontext(x86_64_ucontext_t* ucontext) {
284 // R8-R15
285 memcpy(&regs_[X86_64_REG_R8], &ucontext->uc_mcontext.r8, 8 * sizeof(uint64_t));
286
287 // Rest of the registers.
288 regs_[X86_64_REG_RDI] = ucontext->uc_mcontext.rdi;
289 regs_[X86_64_REG_RSI] = ucontext->uc_mcontext.rsi;
290 regs_[X86_64_REG_RBP] = ucontext->uc_mcontext.rbp;
291 regs_[X86_64_REG_RBX] = ucontext->uc_mcontext.rbx;
292 regs_[X86_64_REG_RDX] = ucontext->uc_mcontext.rdx;
293 regs_[X86_64_REG_RAX] = ucontext->uc_mcontext.rax;
294 regs_[X86_64_REG_RCX] = ucontext->uc_mcontext.rcx;
295 regs_[X86_64_REG_RSP] = ucontext->uc_mcontext.rsp;
296 regs_[X86_64_REG_RIP] = ucontext->uc_mcontext.rip;
297
298 SetFromRaw();
299}
300
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700301static Regs* CreateFromX86_64Ucontext(void* ucontext) {
302 x86_64_ucontext_t* x86_64_ucontext = reinterpret_cast<x86_64_ucontext_t*>(ucontext);
303
304 RegsX86_64* regs = new RegsX86_64();
Christopher Ferrisa0196652017-07-18 16:09:20 -0700305 regs->SetFromUcontext(x86_64_ucontext);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700306 return regs;
307}
308
309Regs* Regs::CreateFromUcontext(uint32_t machine_type, void* ucontext) {
310 switch (machine_type) {
311 case EM_386:
312 return CreateFromX86Ucontext(ucontext);
313 case EM_X86_64:
314 return CreateFromX86_64Ucontext(ucontext);
315 case EM_ARM:
316 return CreateFromArmUcontext(ucontext);
317 case EM_AARCH64:
318 return CreateFromArm64Ucontext(ucontext);
319 }
320 return nullptr;
321}
322
323uint32_t Regs::GetMachineType() {
324#if defined(__arm__)
325 return EM_ARM;
326#elif defined(__aarch64__)
327 return EM_AARCH64;
328#elif defined(__i386__)
329 return EM_386;
330#elif defined(__x86_64__)
331 return EM_X86_64;
332#else
333 abort();
334#endif
335}
336
337Regs* Regs::CreateFromLocal() {
338 Regs* regs;
339#if defined(__arm__)
340 regs = new RegsArm();
341#elif defined(__aarch64__)
342 regs = new RegsArm64();
343#elif defined(__i386__)
344 regs = new RegsX86();
345#elif defined(__x86_64__)
346 regs = new RegsX86_64();
347#else
348 abort();
349#endif
350 return regs;
351}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700352
Christopher Ferrisa0196652017-07-18 16:09:20 -0700353bool RegsArm::StepIfSignalHandler(Memory* memory) {
354 uint32_t data;
355 if (!memory->Read(pc(), &data, sizeof(data))) {
356 return false;
357 }
358
359 uint64_t offset = 0;
360 if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
361 // non-RT sigreturn call.
362 // __restore:
363 //
364 // Form 1 (arm):
365 // 0x77 0x70 mov r7, #0x77
366 // 0xa0 0xe3 svc 0x00000000
367 //
368 // Form 2 (arm):
369 // 0x77 0x00 0x90 0xef svc 0x00900077
370 //
371 // Form 3 (thumb):
372 // 0x77 0x27 movs r7, #77
373 // 0x00 0xdf svc 0
374 if (!memory->Read(sp(), &data, sizeof(data))) {
375 return false;
376 }
377 if (data == 0x5ac3c35a) {
378 // SP + uc_mcontext offset + r0 offset.
379 offset = sp() + 0x14 + 0xc;
380 } else {
381 // SP + r0 offset
382 offset = sp() + 0xc;
383 }
384 } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
385 // RT sigreturn call.
386 // __restore_rt:
387 //
388 // Form 1 (arm):
389 // 0xad 0x70 mov r7, #0xad
390 // 0xa0 0xe3 svc 0x00000000
391 //
392 // Form 2 (arm):
393 // 0xad 0x00 0x90 0xef svc 0x009000ad
394 //
395 // Form 3 (thumb):
396 // 0xad 0x27 movs r7, #ad
397 // 0x00 0xdf svc 0
398 if (!memory->Read(sp(), &data, sizeof(data))) {
399 return false;
400 }
401 if (data == sp() + 8) {
402 // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
403 offset = sp() + 8 + 0x80 + 0x14 + 0xc;
404 } else {
405 // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
406 offset = sp() + 0x80 + 0x14 + 0xc;
407 }
408 }
409 if (offset == 0) {
410 return false;
411 }
412
413 if (!memory->Read(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
414 return false;
415 }
416 SetFromRaw();
417 return true;
418}
419
420bool RegsArm64::StepIfSignalHandler(Memory* memory) {
421 uint64_t data;
422 if (!memory->Read(pc(), &data, sizeof(data))) {
423 return false;
424 }
425
426 // Look for the kernel sigreturn function.
427 // __kernel_rt_sigreturn:
428 // 0xd2801168 mov x8, #0x8b
429 // 0xd4000001 svc #0x0
430 if (data != 0xd4000001d2801168ULL) {
431 return false;
432 }
433
434 // SP + sizeof(siginfo_t) + uc_mcontext offset + X0 offset.
435 if (!memory->Read(sp() + 0x80 + 0xb0 + 0x08, regs_.data(), sizeof(uint64_t) * ARM64_REG_LAST)) {
436 return false;
437 }
438
439 SetFromRaw();
440 return true;
441}
442
443bool RegsX86::StepIfSignalHandler(Memory* memory) {
444 uint64_t data;
445 if (!memory->Read(pc(), &data, sizeof(data))) {
446 return false;
447 }
448
449 if (data == 0x80cd00000077b858ULL) {
450 // Without SA_SIGINFO set, the return sequence is:
451 //
452 // __restore:
453 // 0x58 pop %eax
454 // 0xb8 0x77 0x00 0x00 0x00 movl 0x77,%eax
455 // 0xcd 0x80 int 0x80
456 //
457 // SP points at arguments:
458 // int signum
459 // struct sigcontext (same format as mcontext)
460 struct x86_mcontext_t context;
461 if (!memory->Read(sp() + 4, &context, sizeof(context))) {
462 return false;
463 }
464 regs_[X86_REG_EBP] = context.ebp;
465 regs_[X86_REG_ESP] = context.esp;
466 regs_[X86_REG_EBX] = context.ebx;
467 regs_[X86_REG_EDX] = context.edx;
468 regs_[X86_REG_ECX] = context.ecx;
469 regs_[X86_REG_EAX] = context.eax;
470 regs_[X86_REG_EIP] = context.eip;
471 SetFromRaw();
472 return true;
473 } else if ((data & 0x00ffffffffffffffULL) == 0x0080cd000000adb8ULL) {
474 // With SA_SIGINFO set, the return sequence is:
475 //
476 // __restore_rt:
477 // 0xb8 0xad 0x00 0x00 0x00 movl 0xad,%eax
478 // 0xcd 0x80 int 0x80
479 //
480 // SP points at arguments:
481 // int signum
482 // siginfo*
483 // ucontext*
484
485 // Get the location of the sigcontext data.
486 uint32_t ptr;
487 if (!memory->Read(sp() + 8, &ptr, sizeof(ptr))) {
488 return false;
489 }
490 // Only read the portion of the data structure we care about.
491 x86_ucontext_t x86_ucontext;
492 if (!memory->Read(ptr + 0x14, &x86_ucontext.uc_mcontext, sizeof(x86_mcontext_t))) {
493 return false;
494 }
495 SetFromUcontext(&x86_ucontext);
496 return true;
497 }
498 return false;
499}
500
501bool RegsX86_64::StepIfSignalHandler(Memory* memory) {
502 uint64_t data;
503 if (!memory->Read(pc(), &data, sizeof(data)) || data != 0x0f0000000fc0c748) {
504 return false;
505 }
506
507 uint16_t data2;
508 if (!memory->Read(pc() + 8, &data2, sizeof(data2)) || data2 != 0x0f05) {
509 return false;
510 }
511
512 // __restore_rt:
513 // 0x48 0xc7 0xc0 0x0f 0x00 0x00 0x00 mov $0xf,%rax
514 // 0x0f 0x05 syscall
515 // 0x0f nopl 0x0($rax)
516
517 // Read the mcontext data from the stack.
518 // sp points to the ucontext data structure, read only the mcontext part.
519 x86_64_ucontext_t x86_64_ucontext;
520 if (!memory->Read(sp() + 0x28, &x86_64_ucontext.uc_mcontext, sizeof(x86_64_mcontext_t))) {
521 return false;
522 }
523 SetFromUcontext(&x86_64_ucontext);
524 return true;
525}
526
Christopher Ferrisd226a512017-07-14 10:37:19 -0700527} // namespace unwindstack