-
Notifications
You must be signed in to change notification settings - Fork 28
/
DwarfOp.cpp
497 lines (438 loc) · 12.6 KB
/
DwarfOp.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Copyright (C) 2017 The Android Open Source Project
*
* 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.
*/
#include <stdint.h>
#include <deque>
#include <string>
#include <vector>
#include <android-base/stringprintf.h>
#include <unwindstack/DwarfError.h>
#include <unwindstack/DwarfMemory.h>
#include <unwindstack/Log.h>
#include <unwindstack/Memory.h>
#include <unwindstack/Regs.h>
#include "DwarfOp.h"
namespace unwindstack {
template <typename AddressType>
constexpr typename DwarfOp<AddressType>::OpCallback DwarfOp<AddressType>::kCallbackTable[256];
template <typename AddressType>
bool DwarfOp<AddressType>::Eval(uint64_t start, uint64_t end) {
is_register_ = false;
stack_.clear();
memory_->set_cur_offset(start);
dex_pc_set_ = false;
// Unroll the first Decode calls to be able to check for a special
// sequence of ops and values that indicate this is the dex pc.
// The pattern is:
// OP_const4u (0x0c) 'D' 'E' 'X' '1'
// OP_drop (0x13)
if (memory_->cur_offset() < end) {
if (!Decode()) {
return false;
}
} else {
return true;
}
bool check_for_drop;
if (cur_op_ == 0x0c && operands_.back() == 0x31584544) {
check_for_drop = true;
} else {
check_for_drop = false;
}
if (memory_->cur_offset() < end) {
if (!Decode()) {
return false;
}
} else {
return true;
}
if (check_for_drop && cur_op_ == 0x13) {
dex_pc_set_ = true;
}
uint32_t iterations = 2;
while (memory_->cur_offset() < end) {
if (!Decode()) {
return false;
}
// To protect against a branch that creates an infinite loop,
// terminate if the number of iterations gets too high.
if (iterations++ == 1000) {
last_error_.code = DWARF_ERROR_TOO_MANY_ITERATIONS;
return false;
}
}
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::Decode() {
last_error_.code = DWARF_ERROR_NONE;
if (!memory_->ReadBytes(&cur_op_, 1)) {
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
last_error_.address = memory_->cur_offset();
return false;
}
const auto* op = &kCallbackTable[cur_op_];
const auto handle_func = op->handle_func;
if (handle_func == nullptr) {
last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
return false;
}
// Make sure that the required number of stack elements is available.
if (stack_.size() < op->num_required_stack_values) {
last_error_.code = DWARF_ERROR_STACK_INDEX_NOT_VALID;
return false;
}
operands_.clear();
for (size_t i = 0; i < op->num_operands; i++) {
uint64_t value;
if (!memory_->ReadEncodedValue<AddressType>(op->operands[i], &value)) {
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
last_error_.address = memory_->cur_offset();
return false;
}
operands_.push_back(value);
}
return (this->*handle_func)();
}
template <typename AddressType>
void DwarfOp<AddressType>::GetLogInfo(uint64_t start, uint64_t end,
std::vector<std::string>* lines) {
memory_->set_cur_offset(start);
while (memory_->cur_offset() < end) {
uint8_t cur_op;
if (!memory_->ReadBytes(&cur_op, 1)) {
return;
}
std::string raw_string(android::base::StringPrintf("Raw Data: 0x%02x", cur_op));
std::string log_string;
const auto* op = &kCallbackTable[cur_op];
if (op->handle_func == nullptr) {
log_string = "Illegal";
} else {
log_string = op->name;
uint64_t start_offset = memory_->cur_offset();
for (size_t i = 0; i < op->num_operands; i++) {
uint64_t value;
if (!memory_->ReadEncodedValue<AddressType>(op->operands[i], &value)) {
return;
}
log_string += ' ' + std::to_string(value);
}
uint64_t end_offset = memory_->cur_offset();
memory_->set_cur_offset(start_offset);
for (size_t i = start_offset; i < end_offset; i++) {
uint8_t byte;
if (!memory_->ReadBytes(&byte, 1)) {
return;
}
raw_string += android::base::StringPrintf(" 0x%02x", byte);
}
memory_->set_cur_offset(end_offset);
}
lines->push_back(std::move(log_string));
lines->push_back(std::move(raw_string));
}
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_deref() {
// Read the address and dereference it.
AddressType addr = StackPop();
AddressType value;
if (!regular_memory()->ReadFully(addr, &value, sizeof(value))) {
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
last_error_.address = addr;
return false;
}
stack_.push_front(value);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_deref_size() {
AddressType bytes_to_read = OperandAt(0);
if (bytes_to_read > sizeof(AddressType) || bytes_to_read == 0) {
last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
return false;
}
// Read the address and dereference it.
AddressType addr = StackPop();
AddressType value = 0;
if (!regular_memory()->ReadFully(addr, &value, bytes_to_read)) {
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
last_error_.address = addr;
return false;
}
stack_.push_front(value);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_push() {
// Push all of the operands.
for (auto operand : operands_) {
stack_.push_front(operand);
}
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_dup() {
stack_.push_front(StackAt(0));
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_drop() {
StackPop();
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_over() {
stack_.push_front(StackAt(1));
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_pick() {
AddressType index = OperandAt(0);
if (index > StackSize()) {
last_error_.code = DWARF_ERROR_STACK_INDEX_NOT_VALID;
return false;
}
stack_.push_front(StackAt(index));
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_swap() {
AddressType old_value = stack_[0];
stack_[0] = stack_[1];
stack_[1] = old_value;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_rot() {
AddressType top = stack_[0];
stack_[0] = stack_[1];
stack_[1] = stack_[2];
stack_[2] = top;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_abs() {
SignedType signed_value = static_cast<SignedType>(stack_[0]);
if (signed_value < 0) {
signed_value = -signed_value;
}
stack_[0] = static_cast<AddressType>(signed_value);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_and() {
AddressType top = StackPop();
stack_[0] &= top;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_div() {
AddressType top = StackPop();
if (top == 0) {
last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
return false;
}
SignedType signed_divisor = static_cast<SignedType>(top);
SignedType signed_dividend = static_cast<SignedType>(stack_[0]);
stack_[0] = static_cast<AddressType>(signed_dividend / signed_divisor);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_minus() {
AddressType top = StackPop();
stack_[0] -= top;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_mod() {
AddressType top = StackPop();
if (top == 0) {
last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
return false;
}
stack_[0] %= top;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_mul() {
AddressType top = StackPop();
stack_[0] *= top;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_neg() {
SignedType signed_value = static_cast<SignedType>(stack_[0]);
stack_[0] = static_cast<AddressType>(-signed_value);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_not() {
stack_[0] = ~stack_[0];
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_or() {
AddressType top = StackPop();
stack_[0] |= top;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_plus() {
AddressType top = StackPop();
stack_[0] += top;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_plus_uconst() {
stack_[0] += OperandAt(0);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_shl() {
AddressType top = StackPop();
stack_[0] <<= top;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_shr() {
AddressType top = StackPop();
stack_[0] >>= top;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_shra() {
AddressType top = StackPop();
SignedType signed_value = static_cast<SignedType>(stack_[0]) >> top;
stack_[0] = static_cast<AddressType>(signed_value);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_xor() {
AddressType top = StackPop();
stack_[0] ^= top;
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_bra() {
// Requires one stack element.
AddressType top = StackPop();
int16_t offset = static_cast<int16_t>(OperandAt(0));
uint64_t cur_offset;
if (top != 0) {
cur_offset = memory_->cur_offset() + offset;
} else {
cur_offset = memory_->cur_offset() - offset;
}
memory_->set_cur_offset(cur_offset);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_eq() {
AddressType top = StackPop();
stack_[0] = bool_to_dwarf_bool(stack_[0] == top);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_ge() {
AddressType top = StackPop();
stack_[0] = bool_to_dwarf_bool(stack_[0] >= top);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_gt() {
AddressType top = StackPop();
stack_[0] = bool_to_dwarf_bool(stack_[0] > top);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_le() {
AddressType top = StackPop();
stack_[0] = bool_to_dwarf_bool(stack_[0] <= top);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_lt() {
AddressType top = StackPop();
stack_[0] = bool_to_dwarf_bool(stack_[0] < top);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_ne() {
AddressType top = StackPop();
stack_[0] = bool_to_dwarf_bool(stack_[0] != top);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_skip() {
int16_t offset = static_cast<int16_t>(OperandAt(0));
uint64_t cur_offset = memory_->cur_offset() + offset;
memory_->set_cur_offset(cur_offset);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_lit() {
stack_.push_front(cur_op() - 0x30);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_reg() {
is_register_ = true;
stack_.push_front(cur_op() - 0x50);
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_regx() {
is_register_ = true;
stack_.push_front(OperandAt(0));
return true;
}
// It's not clear for breg/bregx, if this op should read the current
// value of the register, or where we think that register is located.
// For simplicity, the code will read the value before doing the unwind.
template <typename AddressType>
bool DwarfOp<AddressType>::op_breg() {
uint16_t reg = cur_op() - 0x70;
if (reg >= regs_info_->Total()) {
last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
return false;
}
stack_.push_front(regs_info_->Get(reg) + OperandAt(0));
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_bregx() {
AddressType reg = OperandAt(0);
if (reg >= regs_info_->Total()) {
last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
return false;
}
stack_.push_front(regs_info_->Get(reg) + OperandAt(1));
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_nop() {
return true;
}
template <typename AddressType>
bool DwarfOp<AddressType>::op_not_implemented() {
last_error_.code = DWARF_ERROR_NOT_IMPLEMENTED;
return false;
}
// Explicitly instantiate DwarfOp.
template class DwarfOp<uint32_t>;
template class DwarfOp<uint64_t>;
} // namespace unwindstack