Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Core/EmulateInstruction.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- EmulateInstruction.cpp --------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "lldb/Core/EmulateInstruction.h"
10
11
#include "lldb/Core/Address.h"
12
#include "lldb/Core/DumpRegisterValue.h"
13
#include "lldb/Core/PluginManager.h"
14
#include "lldb/Core/StreamFile.h"
15
#include "lldb/Symbol/UnwindPlan.h"
16
#include "lldb/Target/Process.h"
17
#include "lldb/Target/RegisterContext.h"
18
#include "lldb/Target/StackFrame.h"
19
#include "lldb/Utility/ConstString.h"
20
#include "lldb/Utility/DataExtractor.h"
21
#include "lldb/Utility/RegisterValue.h"
22
#include "lldb/Utility/Status.h"
23
#include "lldb/Utility/Stream.h"
24
#include "lldb/Utility/StreamString.h"
25
#include "lldb/lldb-forward.h"
26
#include "lldb/lldb-private-interfaces.h"
27
28
#include "llvm/ADT/StringRef.h"
29
30
#include <cstring>
31
#include <memory>
32
33
#include <cinttypes>
34
#include <cstdio>
35
36
namespace lldb_private {
37
class Target;
38
}
39
40
using namespace lldb;
41
using namespace lldb_private;
42
43
EmulateInstruction *
44
EmulateInstruction::FindPlugin(const ArchSpec &arch,
45
                               InstructionType supported_inst_type,
46
21.1k
                               const char *plugin_name) {
47
21.1k
  EmulateInstructionCreateInstance create_callback = nullptr;
48
21.1k
  if (plugin_name) {
49
0
    create_callback =
50
0
        PluginManager::GetEmulateInstructionCreateCallbackForPluginName(
51
0
            plugin_name);
52
0
    if (create_callback) {
53
0
      EmulateInstruction *emulate_insn_ptr =
54
0
          create_callback(arch, supported_inst_type);
55
0
      if (emulate_insn_ptr)
56
0
        return emulate_insn_ptr;
57
0
    }
58
21.1k
  } else {
59
21.1k
    for (uint32_t idx = 0;
60
126k
         (create_callback =
61
126k
              PluginManager::GetEmulateInstructionCreateCallbackAtIndex(idx)) !=
62
126k
         nullptr;
63
105k
         
++idx105k
) {
64
105k
      EmulateInstruction *emulate_insn_ptr =
65
105k
          create_callback(arch, supported_inst_type);
66
105k
      if (emulate_insn_ptr)
67
187
        return emulate_insn_ptr;
68
105k
    }
69
21.1k
  }
70
21.0k
  return nullptr;
71
21.1k
}
72
73
188
EmulateInstruction::EmulateInstruction(const ArchSpec &arch) : m_arch(arch) {}
74
75
bool EmulateInstruction::ReadRegister(const RegisterInfo *reg_info,
76
816
                                      RegisterValue &reg_value) {
77
816
  if (m_read_reg_callback != nullptr)
78
816
    return m_read_reg_callback(this, m_baton, reg_info, reg_value);
79
0
  return false;
80
816
}
81
82
bool EmulateInstruction::ReadRegister(lldb::RegisterKind reg_kind,
83
                                      uint32_t reg_num,
84
762
                                      RegisterValue &reg_value) {
85
762
  RegisterInfo reg_info;
86
762
  if (GetRegisterInfo(reg_kind, reg_num, reg_info))
87
762
    return ReadRegister(&reg_info, reg_value);
88
0
  return false;
89
762
}
90
91
uint64_t EmulateInstruction::ReadRegisterUnsigned(lldb::RegisterKind reg_kind,
92
                                                  uint32_t reg_num,
93
                                                  uint64_t fail_value,
94
762
                                                  bool *success_ptr) {
95
762
  RegisterValue reg_value;
96
762
  if (ReadRegister(reg_kind, reg_num, reg_value))
97
762
    return reg_value.GetAsUInt64(fail_value, success_ptr);
98
0
  if (success_ptr)
99
0
    *success_ptr = false;
100
0
  return fail_value;
101
762
}
102
103
uint64_t EmulateInstruction::ReadRegisterUnsigned(const RegisterInfo *reg_info,
104
                                                  uint64_t fail_value,
105
9
                                                  bool *success_ptr) {
106
9
  RegisterValue reg_value;
107
9
  if (ReadRegister(reg_info, reg_value))
108
9
    return reg_value.GetAsUInt64(fail_value, success_ptr);
109
0
  if (success_ptr)
110
0
    *success_ptr = false;
111
0
  return fail_value;
112
9
}
113
114
bool EmulateInstruction::WriteRegister(const Context &context,
115
                                       const RegisterInfo *reg_info,
116
460
                                       const RegisterValue &reg_value) {
117
460
  if (m_write_reg_callback != nullptr)
118
460
    return m_write_reg_callback(this, m_baton, context, reg_info, reg_value);
119
0
  return false;
120
460
}
121
122
bool EmulateInstruction::WriteRegister(const Context &context,
123
                                       lldb::RegisterKind reg_kind,
124
                                       uint32_t reg_num,
125
0
                                       const RegisterValue &reg_value) {
126
0
  RegisterInfo reg_info;
127
0
  if (GetRegisterInfo(reg_kind, reg_num, reg_info))
128
0
    return WriteRegister(context, &reg_info, reg_value);
129
0
  return false;
130
0
}
131
132
bool EmulateInstruction::WriteRegisterUnsigned(const Context &context,
133
                                               lldb::RegisterKind reg_kind,
134
                                               uint32_t reg_num,
135
394
                                               uint64_t uint_value) {
136
394
  RegisterInfo reg_info;
137
394
  if (GetRegisterInfo(reg_kind, reg_num, reg_info)) {
138
394
    RegisterValue reg_value;
139
394
    if (reg_value.SetUInt(uint_value, reg_info.byte_size))
140
394
      return WriteRegister(context, &reg_info, reg_value);
141
394
  }
142
0
  return false;
143
394
}
144
145
bool EmulateInstruction::WriteRegisterUnsigned(const Context &context,
146
                                               const RegisterInfo *reg_info,
147
20
                                               uint64_t uint_value) {
148
20
  if (reg_info != nullptr) {
149
20
    RegisterValue reg_value;
150
20
    if (reg_value.SetUInt(uint_value, reg_info->byte_size))
151
20
      return WriteRegister(context, reg_info, reg_value);
152
20
  }
153
0
  return false;
154
20
}
155
156
size_t EmulateInstruction::ReadMemory(const Context &context, lldb::addr_t addr,
157
46
                                      void *dst, size_t dst_len) {
158
46
  if (m_read_mem_callback != nullptr)
159
46
    return m_read_mem_callback(this, m_baton, context, addr, dst, dst_len) ==
160
46
           dst_len;
161
0
  return false;
162
46
}
163
164
uint64_t EmulateInstruction::ReadMemoryUnsigned(const Context &context,
165
                                                lldb::addr_t addr,
166
                                                size_t byte_size,
167
                                                uint64_t fail_value,
168
71
                                                bool *success_ptr) {
169
71
  uint64_t uval64 = 0;
170
71
  bool success = false;
171
71
  if (byte_size <= 8) {
172
71
    uint8_t buf[sizeof(uint64_t)];
173
71
    size_t bytes_read =
174
71
        m_read_mem_callback(this, m_baton, context, addr, buf, byte_size);
175
71
    if (bytes_read == byte_size) {
176
71
      lldb::offset_t offset = 0;
177
71
      DataExtractor data(buf, byte_size, GetByteOrder(), GetAddressByteSize());
178
71
      uval64 = data.GetMaxU64(&offset, byte_size);
179
71
      success = true;
180
71
    }
181
71
  }
182
183
71
  if (success_ptr)
184
71
    *success_ptr = success;
185
186
71
  if (!success)
187
0
    uval64 = fail_value;
188
71
  return uval64;
189
71
}
190
191
bool EmulateInstruction::WriteMemoryUnsigned(const Context &context,
192
                                             lldb::addr_t addr, uint64_t uval,
193
44
                                             size_t uval_byte_size) {
194
44
  StreamString strm(Stream::eBinary, GetAddressByteSize(), GetByteOrder());
195
44
  strm.PutMaxHex64(uval, uval_byte_size);
196
197
44
  size_t bytes_written = m_write_mem_callback(
198
44
      this, m_baton, context, addr, strm.GetString().data(), uval_byte_size);
199
44
  return (bytes_written == uval_byte_size);
200
44
}
201
202
bool EmulateInstruction::WriteMemory(const Context &context, lldb::addr_t addr,
203
45
                                     const void *src, size_t src_len) {
204
45
  if (m_write_mem_callback != nullptr)
205
45
    return m_write_mem_callback(this, m_baton, context, addr, src, src_len) ==
206
45
           src_len;
207
0
  return false;
208
45
}
209
210
187
void EmulateInstruction::SetBaton(void *baton) { m_baton = baton; }
211
212
void EmulateInstruction::SetCallbacks(
213
    ReadMemoryCallback read_mem_callback,
214
    WriteMemoryCallback write_mem_callback,
215
    ReadRegisterCallback read_reg_callback,
216
187
    WriteRegisterCallback write_reg_callback) {
217
187
  m_read_mem_callback = read_mem_callback;
218
187
  m_write_mem_callback = write_mem_callback;
219
187
  m_read_reg_callback = read_reg_callback;
220
187
  m_write_reg_callback = write_reg_callback;
221
187
}
222
223
void EmulateInstruction::SetReadMemCallback(
224
0
    ReadMemoryCallback read_mem_callback) {
225
0
  m_read_mem_callback = read_mem_callback;
226
0
}
227
228
void EmulateInstruction::SetWriteMemCallback(
229
0
    WriteMemoryCallback write_mem_callback) {
230
0
  m_write_mem_callback = write_mem_callback;
231
0
}
232
233
void EmulateInstruction::SetReadRegCallback(
234
0
    ReadRegisterCallback read_reg_callback) {
235
0
  m_read_reg_callback = read_reg_callback;
236
0
}
237
238
void EmulateInstruction::SetWriteRegCallback(
239
0
    WriteRegisterCallback write_reg_callback) {
240
0
  m_write_reg_callback = write_reg_callback;
241
0
}
242
243
//
244
//  Read & Write Memory and Registers callback functions.
245
//
246
247
size_t EmulateInstruction::ReadMemoryFrame(EmulateInstruction *instruction,
248
                                           void *baton, const Context &context,
249
                                           lldb::addr_t addr, void *dst,
250
0
                                           size_t dst_len) {
251
0
  if (baton == nullptr || dst == nullptr || dst_len == 0)
252
0
    return 0;
253
254
0
  StackFrame *frame = (StackFrame *)baton;
255
256
0
  ProcessSP process_sp(frame->CalculateProcess());
257
0
  if (process_sp) {
258
0
    Status error;
259
0
    return process_sp->ReadMemory(addr, dst, dst_len, error);
260
0
  }
261
0
  return 0;
262
0
}
263
264
size_t EmulateInstruction::WriteMemoryFrame(EmulateInstruction *instruction,
265
                                            void *baton, const Context &context,
266
                                            lldb::addr_t addr, const void *src,
267
0
                                            size_t src_len) {
268
0
  if (baton == nullptr || src == nullptr || src_len == 0)
269
0
    return 0;
270
271
0
  StackFrame *frame = (StackFrame *)baton;
272
273
0
  ProcessSP process_sp(frame->CalculateProcess());
274
0
  if (process_sp) {
275
0
    Status error;
276
0
    return process_sp->WriteMemory(addr, src, src_len, error);
277
0
  }
278
279
0
  return 0;
280
0
}
281
282
bool EmulateInstruction::ReadRegisterFrame(EmulateInstruction *instruction,
283
                                           void *baton,
284
                                           const RegisterInfo *reg_info,
285
0
                                           RegisterValue &reg_value) {
286
0
  if (baton == nullptr)
287
0
    return false;
288
289
0
  StackFrame *frame = (StackFrame *)baton;
290
0
  return frame->GetRegisterContext()->ReadRegister(reg_info, reg_value);
291
0
}
292
293
bool EmulateInstruction::WriteRegisterFrame(EmulateInstruction *instruction,
294
                                            void *baton, const Context &context,
295
                                            const RegisterInfo *reg_info,
296
0
                                            const RegisterValue &reg_value) {
297
0
  if (baton == nullptr)
298
0
    return false;
299
300
0
  StackFrame *frame = (StackFrame *)baton;
301
0
  return frame->GetRegisterContext()->WriteRegister(reg_info, reg_value);
302
0
}
303
304
size_t EmulateInstruction::ReadMemoryDefault(EmulateInstruction *instruction,
305
                                             void *baton,
306
                                             const Context &context,
307
                                             lldb::addr_t addr, void *dst,
308
0
                                             size_t length) {
309
0
  StreamFile strm(stdout, false);
310
0
  strm.Printf("    Read from Memory (address = 0x%" PRIx64 ", length = %" PRIu64
311
0
              ", context = ",
312
0
              addr, (uint64_t)length);
313
0
  context.Dump(strm, instruction);
314
0
  strm.EOL();
315
0
  *((uint64_t *)dst) = 0xdeadbeef;
316
0
  return length;
317
0
}
318
319
size_t EmulateInstruction::WriteMemoryDefault(EmulateInstruction *instruction,
320
                                              void *baton,
321
                                              const Context &context,
322
                                              lldb::addr_t addr,
323
0
                                              const void *dst, size_t length) {
324
0
  StreamFile strm(stdout, false);
325
0
  strm.Printf("    Write to Memory (address = 0x%" PRIx64 ", length = %" PRIu64
326
0
              ", context = ",
327
0
              addr, (uint64_t)length);
328
0
  context.Dump(strm, instruction);
329
0
  strm.EOL();
330
0
  return length;
331
0
}
332
333
bool EmulateInstruction::ReadRegisterDefault(EmulateInstruction *instruction,
334
                                             void *baton,
335
                                             const RegisterInfo *reg_info,
336
0
                                             RegisterValue &reg_value) {
337
0
  StreamFile strm(stdout, false);
338
0
  strm.Printf("  Read Register (%s)\n", reg_info->name);
339
0
  lldb::RegisterKind reg_kind;
340
0
  uint32_t reg_num;
341
0
  if (GetBestRegisterKindAndNumber(reg_info, reg_kind, reg_num))
342
0
    reg_value.SetUInt64((uint64_t)reg_kind << 24 | reg_num);
343
0
  else
344
0
    reg_value.SetUInt64(0);
345
346
0
  return true;
347
0
}
348
349
bool EmulateInstruction::WriteRegisterDefault(EmulateInstruction *instruction,
350
                                              void *baton,
351
                                              const Context &context,
352
                                              const RegisterInfo *reg_info,
353
0
                                              const RegisterValue &reg_value) {
354
0
  StreamFile strm(stdout, false);
355
0
  strm.Printf("    Write to Register (name = %s, value = ", reg_info->name);
356
0
  DumpRegisterValue(reg_value, &strm, reg_info, false, false, eFormatDefault);
357
0
  strm.PutCString(", context = ");
358
0
  context.Dump(strm, instruction);
359
0
  strm.EOL();
360
0
  return true;
361
0
}
362
363
void EmulateInstruction::Context::Dump(Stream &strm,
364
0
                                       EmulateInstruction *instruction) const {
365
0
  switch (type) {
366
0
  case eContextReadOpcode:
367
0
    strm.PutCString("reading opcode");
368
0
    break;
369
370
0
  case eContextImmediate:
371
0
    strm.PutCString("immediate");
372
0
    break;
373
374
0
  case eContextPushRegisterOnStack:
375
0
    strm.PutCString("push register");
376
0
    break;
377
378
0
  case eContextPopRegisterOffStack:
379
0
    strm.PutCString("pop register");
380
0
    break;
381
382
0
  case eContextAdjustStackPointer:
383
0
    strm.PutCString("adjust sp");
384
0
    break;
385
386
0
  case eContextSetFramePointer:
387
0
    strm.PutCString("set frame pointer");
388
0
    break;
389
390
0
  case eContextAdjustBaseRegister:
391
0
    strm.PutCString("adjusting (writing value back to) a base register");
392
0
    break;
393
394
0
  case eContextRegisterPlusOffset:
395
0
    strm.PutCString("register + offset");
396
0
    break;
397
398
0
  case eContextRegisterStore:
399
0
    strm.PutCString("store register");
400
0
    break;
401
402
0
  case eContextRegisterLoad:
403
0
    strm.PutCString("load register");
404
0
    break;
405
406
0
  case eContextRelativeBranchImmediate:
407
0
    strm.PutCString("relative branch immediate");
408
0
    break;
409
410
0
  case eContextAbsoluteBranchRegister:
411
0
    strm.PutCString("absolute branch register");
412
0
    break;
413
414
0
  case eContextSupervisorCall:
415
0
    strm.PutCString("supervisor call");
416
0
    break;
417
418
0
  case eContextTableBranchReadMemory:
419
0
    strm.PutCString("table branch read memory");
420
0
    break;
421
422
0
  case eContextWriteRegisterRandomBits:
423
0
    strm.PutCString("write random bits to a register");
424
0
    break;
425
426
0
  case eContextWriteMemoryRandomBits:
427
0
    strm.PutCString("write random bits to a memory address");
428
0
    break;
429
430
0
  case eContextArithmetic:
431
0
    strm.PutCString("arithmetic");
432
0
    break;
433
434
0
  case eContextReturnFromException:
435
0
    strm.PutCString("return from exception");
436
0
    break;
437
438
0
  default:
439
0
    strm.PutCString("unrecognized context.");
440
0
    break;
441
0
  }
442
443
0
  switch (info_type) {
444
0
  case eInfoTypeRegisterPlusOffset:
445
0
    strm.Printf(" (reg_plus_offset = %s%+" PRId64 ")",
446
0
                info.RegisterPlusOffset.reg.name,
447
0
                info.RegisterPlusOffset.signed_offset);
448
0
    break;
449
450
0
  case eInfoTypeRegisterPlusIndirectOffset:
451
0
    strm.Printf(" (reg_plus_reg = %s + %s)",
452
0
                info.RegisterPlusIndirectOffset.base_reg.name,
453
0
                info.RegisterPlusIndirectOffset.offset_reg.name);
454
0
    break;
455
456
0
  case eInfoTypeRegisterToRegisterPlusOffset:
457
0
    strm.Printf(" (base_and_imm_offset = %s%+" PRId64 ", data_reg = %s)",
458
0
                info.RegisterToRegisterPlusOffset.base_reg.name,
459
0
                info.RegisterToRegisterPlusOffset.offset,
460
0
                info.RegisterToRegisterPlusOffset.data_reg.name);
461
0
    break;
462
463
0
  case eInfoTypeRegisterToRegisterPlusIndirectOffset:
464
0
    strm.Printf(" (base_and_reg_offset = %s + %s, data_reg = %s)",
465
0
                info.RegisterToRegisterPlusIndirectOffset.base_reg.name,
466
0
                info.RegisterToRegisterPlusIndirectOffset.offset_reg.name,
467
0
                info.RegisterToRegisterPlusIndirectOffset.data_reg.name);
468
0
    break;
469
470
0
  case eInfoTypeRegisterRegisterOperands:
471
0
    strm.Printf(" (register to register binary op: %s and %s)",
472
0
                info.RegisterRegisterOperands.operand1.name,
473
0
                info.RegisterRegisterOperands.operand2.name);
474
0
    break;
475
476
0
  case eInfoTypeOffset:
477
0
    strm.Printf(" (signed_offset = %+" PRId64 ")", info.signed_offset);
478
0
    break;
479
480
0
  case eInfoTypeRegister:
481
0
    strm.Printf(" (reg = %s)", info.reg.name);
482
0
    break;
483
484
0
  case eInfoTypeImmediate:
485
0
    strm.Printf(" (unsigned_immediate = %" PRIu64 " (0x%16.16" PRIx64 "))",
486
0
                info.unsigned_immediate, info.unsigned_immediate);
487
0
    break;
488
489
0
  case eInfoTypeImmediateSigned:
490
0
    strm.Printf(" (signed_immediate = %+" PRId64 " (0x%16.16" PRIx64 "))",
491
0
                info.signed_immediate, info.signed_immediate);
492
0
    break;
493
494
0
  case eInfoTypeAddress:
495
0
    strm.Printf(" (address = 0x%" PRIx64 ")", info.address);
496
0
    break;
497
498
0
  case eInfoTypeISAAndImmediate:
499
0
    strm.Printf(" (isa = %u, unsigned_immediate = %u (0x%8.8x))",
500
0
                info.ISAAndImmediate.isa, info.ISAAndImmediate.unsigned_data32,
501
0
                info.ISAAndImmediate.unsigned_data32);
502
0
    break;
503
504
0
  case eInfoTypeISAAndImmediateSigned:
505
0
    strm.Printf(" (isa = %u, signed_immediate = %i (0x%8.8x))",
506
0
                info.ISAAndImmediateSigned.isa,
507
0
                info.ISAAndImmediateSigned.signed_data32,
508
0
                info.ISAAndImmediateSigned.signed_data32);
509
0
    break;
510
511
0
  case eInfoTypeISA:
512
0
    strm.Printf(" (isa = %u)", info.isa);
513
0
    break;
514
515
0
  case eInfoTypeNoArgs:
516
0
    break;
517
0
  }
518
0
}
519
520
bool EmulateInstruction::SetInstruction(const Opcode &opcode,
521
                                        const Address &inst_addr,
522
143
                                        Target *target) {
523
143
  m_opcode = opcode;
524
143
  m_addr = LLDB_INVALID_ADDRESS;
525
143
  if (inst_addr.IsValid()) {
526
143
    if (target != nullptr)
527
0
      m_addr = inst_addr.GetLoadAddress(target);
528
143
    if (m_addr == LLDB_INVALID_ADDRESS)
529
143
      m_addr = inst_addr.GetFileAddress();
530
143
  }
531
143
  return true;
532
143
}
533
534
bool EmulateInstruction::GetBestRegisterKindAndNumber(
535
    const RegisterInfo *reg_info, lldb::RegisterKind &reg_kind,
536
250
    uint32_t &reg_num) {
537
  // Generic and DWARF should be the two most popular register kinds when
538
  // emulating instructions since they are the most platform agnostic...
539
250
  reg_num = reg_info->kinds[eRegisterKindGeneric];
540
250
  if (reg_num != LLDB_INVALID_REGNUM) {
541
184
    reg_kind = eRegisterKindGeneric;
542
184
    return true;
543
184
  }
544
545
66
  reg_num = reg_info->kinds[eRegisterKindDWARF];
546
66
  if (reg_num != LLDB_INVALID_REGNUM) {
547
50
    reg_kind = eRegisterKindDWARF;
548
50
    return true;
549
50
  }
550
551
16
  reg_num = reg_info->kinds[eRegisterKindLLDB];
552
16
  if (reg_num != LLDB_INVALID_REGNUM) {
553
16
    reg_kind = eRegisterKindLLDB;
554
16
    return true;
555
16
  }
556
557
0
  reg_num = reg_info->kinds[eRegisterKindEHFrame];
558
0
  if (reg_num != LLDB_INVALID_REGNUM) {
559
0
    reg_kind = eRegisterKindEHFrame;
560
0
    return true;
561
0
  }
562
563
0
  reg_num = reg_info->kinds[eRegisterKindProcessPlugin];
564
0
  if (reg_num != LLDB_INVALID_REGNUM) {
565
0
    reg_kind = eRegisterKindProcessPlugin;
566
0
    return true;
567
0
  }
568
0
  return false;
569
0
}
570
571
uint32_t
572
EmulateInstruction::GetInternalRegisterNumber(RegisterContext *reg_ctx,
573
0
                                              const RegisterInfo &reg_info) {
574
0
  lldb::RegisterKind reg_kind;
575
0
  uint32_t reg_num;
576
0
  if (reg_ctx && GetBestRegisterKindAndNumber(&reg_info, reg_kind, reg_num))
577
0
    return reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
578
0
  return LLDB_INVALID_REGNUM;
579
0
}
580
581
0
bool EmulateInstruction::CreateFunctionEntryUnwind(UnwindPlan &unwind_plan) {
582
0
  unwind_plan.Clear();
583
0
  return false;
584
0
}