Debian Codegraph @

js/src/jit/Disassemble.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "jit/Disassemble.h"
8
9 #include <stddef.h> // size_t
10 #include <stdint.h> // uint8_t, int32_t
11
12 #include "js/Printf.h" // JS_smprintf
13 #include "js/Utility.h" // JS::UniqueChars
14
15 #if defined(JS_JITSPEW)
16 # if defined(JS_CODEGEN_X86) || defined(JS_CODEGEN_X64)
17 # include "zydis/ZydisAPI.h" // zydisDisassemble
18 # elif defined(JS_CODEGEN_ARM64)
19 # include "jit/arm/disasm/Disasm-arm.h" // js::jit::disasm::*
20 # include "jit/arm64/vixl/Decoder-vixl.h" // vixl::Decoder
21 # include "jit/arm64/vixl/Disasm-vixl.h" // vixl::Disassembler
22 # include "jit/arm64/vixl/Instructions-vixl.h" // vixl::Instruction
23 # elif defined(JS_CODEGEN_ARM)
24 # include "jit/arm/disasm/Disasm-arm.h" // js::jit::disasm::*
25 # elif defined(JS_CODEGEN_RISCV64)
26 # include "jit/riscv64/disasm/Disasm-riscv64.h" // js::jit::disasm::*
27 # endif
28 #endif
29
30 namespace js {
31 namespace jit {
32
33 #if defined(JS_JITSPEW) && (defined(JS_CODEGEN_X86) || defined(JS_CODEGEN_X64))
34
35 bool HasDisassembler() { return true; }
36
37 void Disassemble(uint8_t* code, size_t length, InstrCallback callback) {
38 zydisDisassemble(code, length, callback);
39 }
40
41 #elif defined(JS_JITSPEW) && defined(JS_CODEGEN_ARM64)
42
43 class ARM64Disassembler : public vixl::Disassembler {
44 public:
45 explicit ARM64Disassembler(InstrCallback callback) : callback_(callback) {}
46
47 protected:
48 void ProcessOutput(const vixl::Instruction* instr) override {
49 AutoEnterOOMUnsafeRegion oomUnsafe;
50 JS::UniqueChars formatted = JS_smprintf(
51 "0x%p %08x %s", instr, instr->InstructionBits(), GetOutput());
52 if (!formatted) {
53 oomUnsafe.crash("ARM64Disassembler::ProcessOutput");
54 }
55 callback_(formatted.get());
56 }
57
58 private:
59 InstrCallback callback_;
60 };
61
62 bool HasDisassembler() { return true; }
63
64 void Disassemble(uint8_t* code, size_t length, InstrCallback callback) {
65 ARM64Disassembler dis(callback);
66 vixl::Decoder decoder;
67 decoder.AppendVisitor(&dis);
68
69 uint8_t* instr = code;
70 uint8_t* end = code + length;
71
72 while (instr < end) {
73 decoder.Decode(reinterpret_cast<vixl::Instruction*>(instr));
74
75 instr += sizeof(vixl::Instr);
76 }
77 }
78
79 #elif defined(JS_JITSPEW) && defined(JS_CODEGEN_ARM)
80
81 bool HasDisassembler() { return true; }
82
83 void Disassemble(uint8_t* code, size_t length, InstrCallback callback) {
84 disasm::NameConverter converter;
85 disasm::Disassembler d(converter);
86
87 uint8_t* instr = code;
88 uint8_t* end = code + length;
89
90 while (instr < end) {
91 disasm::EmbeddedVector<char, disasm::ReasonableBufferSize> buffer;
92 buffer[0] = '\0';
93 uint8_t* next_instr = instr + d.InstructionDecode(buffer, instr);
94
95 JS::UniqueChars formatted =
96 JS_smprintf("0x%p %08x %s", instr, *reinterpret_cast<int32_t*>(instr),
97 buffer.start());
98 callback(formatted.get());
99
100 instr = next_instr;
101 }
102 }
103
104 #elif defined(JS_JITSPEW) && defined(JS_CODEGEN_RISCV64)
105
106 bool HasDisassembler() { return true; }
107
108 void Disassemble(uint8_t* code, size_t length, InstrCallback callback) {
109 disasm::NameConverter converter;
110 disasm::Disassembler d(converter);
111
112 uint8_t* instr = code;
113 uint8_t* end = code + length;
114
115 while (instr < end) {
116 EmbeddedVector<char, ReasonableBufferSize> buffer;
117 buffer[0] = '\0';
118 uint8_t* next_instr = instr + d.InstructionDecode(buffer, instr);
119
120 JS::UniqueChars formatted =
121 JS_smprintf("0x%p %08x %s", instr, *reinterpret_cast<int32_t*>(instr),
122 buffer.start());
123 callback(formatted.get());
124
125 instr = next_instr;
126 }
127 }
128
129 #else
130
131 bool HasDisassembler() { return false; }
132
133 void Disassemble(uint8_t* code, size_t length, InstrCallback callback) {
134 callback("*** No disassembly available ***\n");
135 }
136
137 #endif
138
139 } // namespace jit
140 } // namespace js