/src/capstonenext/Mapping.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Capstone Disassembly Engine */ |
2 | | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */ |
3 | | /* Rot127 <unisono@quyllur.org>, 2022-2023 */ |
4 | | |
5 | | #include "Mapping.h" |
6 | | #include "capstone/capstone.h" |
7 | | #include "utils.h" |
8 | | |
9 | | // create a cache for fast id lookup |
10 | | static unsigned short *make_id2insn(const insn_map *insns, unsigned int size) |
11 | 32.8k | { |
12 | | // NOTE: assume that the max id is always put at the end of insns array |
13 | 32.8k | unsigned short max_id = insns[size - 1].id; |
14 | 32.8k | unsigned int i; |
15 | | |
16 | 32.8k | unsigned short *cache = |
17 | 32.8k | (unsigned short *)cs_mem_calloc(max_id + 1, sizeof(*cache)); |
18 | | |
19 | 73.9M | for (i = 1; i < size; i++) |
20 | 73.9M | cache[insns[i].id] = i; |
21 | | |
22 | 32.8k | return cache; |
23 | 32.8k | } |
24 | | |
25 | | // look for @id in @insns, given its size in @max. first time call will update |
26 | | // @cache. return 0 if not found |
27 | | unsigned short insn_find(const insn_map *insns, unsigned int max, |
28 | | unsigned int id, unsigned short **cache) |
29 | 3.73M | { |
30 | 3.73M | if (id > insns[max - 1].id) |
31 | 0 | return 0; |
32 | | |
33 | 3.73M | if (*cache == NULL) |
34 | 32.8k | *cache = make_id2insn(insns, max); |
35 | | |
36 | 3.73M | return (*cache)[id]; |
37 | 3.73M | } |
38 | | |
39 | | // Gives the id for the given @name if it is saved in @map. |
40 | | // Returns the id or -1 if not found. |
41 | | int name2id(const name_map *map, int max, const char *name) |
42 | 155k | { |
43 | 155k | int i; |
44 | | |
45 | 23.4M | for (i = 0; i < max; i++) { |
46 | 23.3M | if (!strcmp(map[i].name, name)) { |
47 | 110k | return map[i].id; |
48 | 110k | } |
49 | 23.3M | } |
50 | | |
51 | | // nothing match |
52 | 45.5k | return -1; |
53 | 155k | } |
54 | | |
55 | | // Gives the name for the given @id if it is saved in @map. |
56 | | // Returns the name or NULL if not found. |
57 | | const char *id2name(const name_map *map, int max, const unsigned int id) |
58 | 7.03M | { |
59 | 7.03M | int i; |
60 | | |
61 | 159M | for (i = 0; i < max; i++) { |
62 | 159M | if (map[i].id == id) { |
63 | 7.03M | return map[i].name; |
64 | 7.03M | } |
65 | 159M | } |
66 | | |
67 | | // nothing match |
68 | 0 | return NULL; |
69 | 7.03M | } |
70 | | |
71 | | /// Adds a register to the implicit write register list. |
72 | | /// It will not add the same register twice. |
73 | | void map_add_implicit_write(MCInst *MI, uint32_t Reg) |
74 | 500k | { |
75 | 500k | if (!MI->flat_insn->detail) |
76 | 0 | return; |
77 | | |
78 | 500k | uint16_t *regs_write = MI->flat_insn->detail->regs_write; |
79 | 504k | for (int i = 0; i < MAX_IMPL_W_REGS; ++i) { |
80 | 504k | if (i == MI->flat_insn->detail->regs_write_count) { |
81 | 481k | regs_write[i] = Reg; |
82 | 481k | MI->flat_insn->detail->regs_write_count++; |
83 | 481k | return; |
84 | 481k | } |
85 | 23.1k | if (regs_write[i] == Reg) |
86 | 19.5k | return; |
87 | 23.1k | } |
88 | 500k | } |
89 | | |
90 | | /// Adds a register to the implicit read register list. |
91 | | /// It will not add the same register twice. |
92 | | void map_add_implicit_read(MCInst *MI, uint32_t Reg) |
93 | 203k | { |
94 | 203k | if (!MI->flat_insn->detail) |
95 | 0 | return; |
96 | | |
97 | 203k | uint16_t *regs_read = MI->flat_insn->detail->regs_read; |
98 | 215k | for (int i = 0; i < MAX_IMPL_R_REGS; ++i) { |
99 | 215k | if (i == MI->flat_insn->detail->regs_read_count) { |
100 | 187k | regs_read[i] = Reg; |
101 | 187k | MI->flat_insn->detail->regs_read_count++; |
102 | 187k | return; |
103 | 187k | } |
104 | 27.9k | if (regs_read[i] == Reg) |
105 | 16.3k | return; |
106 | 27.9k | } |
107 | 203k | } |
108 | | |
109 | | /// Removes a register from the implicit write register list. |
110 | | void map_remove_implicit_write(MCInst *MI, uint32_t Reg) |
111 | 39.0k | { |
112 | 39.0k | if (!MI->flat_insn->detail) |
113 | 0 | return; |
114 | | |
115 | 39.0k | uint16_t *regs_write = MI->flat_insn->detail->regs_write; |
116 | 39.0k | bool shorten_list = false; |
117 | 42.9k | for (int i = 0; i < MAX_IMPL_W_REGS; ++i) { |
118 | 42.9k | if (shorten_list) { |
119 | 3.86k | regs_write[i - 1] = regs_write[i]; |
120 | 3.86k | } |
121 | 42.9k | if (i >= MI->flat_insn->detail->regs_write_count) |
122 | 39.0k | return; |
123 | | |
124 | 3.86k | if (regs_write[i] == Reg) { |
125 | 3.86k | MI->flat_insn->detail->regs_write_count--; |
126 | | // The register should exist only once in the list. |
127 | 3.86k | assert(!shorten_list); |
128 | 3.86k | shorten_list = true; |
129 | 3.86k | } |
130 | 3.86k | } |
131 | 39.0k | } |
132 | | |
133 | | /// Copies the implicit read registers of @imap to @MI->flat_insn. |
134 | | /// Already present registers will be preserved. |
135 | | void map_implicit_reads(MCInst *MI, const insn_map *imap) |
136 | 1.96M | { |
137 | 1.96M | #ifndef CAPSTONE_DIET |
138 | 1.96M | if (!MI->flat_insn->detail) |
139 | 0 | return; |
140 | | |
141 | 1.96M | cs_detail *detail = MI->flat_insn->detail; |
142 | 1.96M | unsigned Opcode = MCInst_getOpcode(MI); |
143 | 1.96M | unsigned i = 0; |
144 | 1.96M | uint16_t reg = imap[Opcode].regs_use[i]; |
145 | 2.05M | while (reg != 0) { |
146 | 88.2k | if (i >= MAX_IMPL_R_REGS || |
147 | 88.2k | detail->regs_read_count >= MAX_IMPL_R_REGS) { |
148 | 0 | printf("ERROR: Too many implicit read register defined in " |
149 | 0 | "instruction mapping.\n"); |
150 | 0 | return; |
151 | 0 | } |
152 | 88.2k | detail->regs_read[detail->regs_read_count++] = reg; |
153 | 88.2k | reg = imap[Opcode].regs_use[++i]; |
154 | 88.2k | } |
155 | 1.96M | #endif // CAPSTONE_DIET |
156 | 1.96M | } |
157 | | |
158 | | /// Copies the implicit write registers of @imap to @MI->flat_insn. |
159 | | /// Already present registers will be preserved. |
160 | | void map_implicit_writes(MCInst *MI, const insn_map *imap) |
161 | 1.96M | { |
162 | 1.96M | #ifndef CAPSTONE_DIET |
163 | 1.96M | if (!MI->flat_insn->detail) |
164 | 0 | return; |
165 | | |
166 | 1.96M | cs_detail *detail = MI->flat_insn->detail; |
167 | 1.96M | unsigned Opcode = MCInst_getOpcode(MI); |
168 | 1.96M | unsigned i = 0; |
169 | 1.96M | uint16_t reg = imap[Opcode].regs_mod[i]; |
170 | 2.30M | while (reg != 0) { |
171 | 333k | if (i >= MAX_IMPL_W_REGS || |
172 | 333k | detail->regs_write_count >= MAX_IMPL_W_REGS) { |
173 | 0 | printf("ERROR: Too many implicit write register defined in " |
174 | 0 | "instruction mapping.\n"); |
175 | 0 | return; |
176 | 0 | } |
177 | 333k | detail->regs_write[detail->regs_write_count++] = reg; |
178 | 333k | reg = imap[Opcode].regs_mod[++i]; |
179 | 333k | } |
180 | 1.96M | #endif // CAPSTONE_DIET |
181 | 1.96M | } |
182 | | |
183 | | /// Adds a given group to @MI->flat_insn. |
184 | | /// A group is never added twice. |
185 | | void add_group(MCInst *MI, unsigned /* arch_group */ group) |
186 | 71.5k | { |
187 | 71.5k | #ifndef CAPSTONE_DIET |
188 | 71.5k | if (!MI->flat_insn->detail) |
189 | 0 | return; |
190 | | |
191 | 71.5k | cs_detail *detail = MI->flat_insn->detail; |
192 | 71.5k | if (detail->groups_count >= MAX_NUM_GROUPS) { |
193 | 0 | printf("ERROR: Too many groups defined.\n"); |
194 | 0 | return; |
195 | 0 | } |
196 | 185k | for (int i = 0; i < detail->groups_count; ++i) { |
197 | 114k | if (detail->groups[i] == group) { |
198 | 124 | return; |
199 | 124 | } |
200 | 114k | } |
201 | 71.3k | detail->groups[detail->groups_count++] = group; |
202 | 71.3k | #endif // CAPSTONE_DIET |
203 | 71.3k | } |
204 | | |
205 | | /// Copies the groups from @imap to @MI->flat_insn. |
206 | | /// Already present groups will be preserved. |
207 | | void map_groups(MCInst *MI, const insn_map *imap) |
208 | 1.96M | { |
209 | 1.96M | #ifndef CAPSTONE_DIET |
210 | 1.96M | if (!MI->flat_insn->detail) |
211 | 0 | return; |
212 | | |
213 | 1.96M | cs_detail *detail = MI->flat_insn->detail; |
214 | 1.96M | unsigned Opcode = MCInst_getOpcode(MI); |
215 | 1.96M | unsigned i = 0; |
216 | 1.96M | uint16_t group = imap[Opcode].groups[i]; |
217 | 4.25M | while (group != 0) { |
218 | 2.28M | if (detail->groups_count >= MAX_NUM_GROUPS) { |
219 | 0 | printf("ERROR: Too many groups defined in instruction mapping.\n"); |
220 | 0 | return; |
221 | 0 | } |
222 | 2.28M | detail->groups[detail->groups_count++] = group; |
223 | 2.28M | group = imap[Opcode].groups[++i]; |
224 | 2.28M | } |
225 | 1.96M | #endif // CAPSTONE_DIET |
226 | 1.96M | } |
227 | | |
228 | | /// Returns the pointer to the supllementary information in |
229 | | /// the instruction mapping table @imap or NULL in case of failure. |
230 | | const void *map_get_suppl_info(MCInst *MI, const insn_map *imap) |
231 | 70.7k | { |
232 | 70.7k | #ifndef CAPSTONE_DIET |
233 | 70.7k | if (!MI->flat_insn->detail) |
234 | 0 | return NULL; |
235 | | |
236 | 70.7k | unsigned Opcode = MCInst_getOpcode(MI); |
237 | 70.7k | return &imap[Opcode].suppl_info; |
238 | | #else |
239 | | return NULL; |
240 | | #endif // CAPSTONE_DIET |
241 | 70.7k | } |
242 | | |
243 | | // Search for the CS instruction id for the given @MC_Opcode in @imap. |
244 | | // return -1 if none is found. |
245 | | unsigned int find_cs_id(unsigned MC_Opcode, const insn_map *imap, |
246 | | unsigned imap_size) |
247 | 1.96M | { |
248 | | // binary searching since the IDs are sorted in order |
249 | 1.96M | unsigned int left, right, m; |
250 | 1.96M | unsigned int max = imap_size; |
251 | | |
252 | 1.96M | right = max - 1; |
253 | | |
254 | 1.96M | if (MC_Opcode < imap[0].id || MC_Opcode > imap[right].id) |
255 | | // not found |
256 | 0 | return -1; |
257 | | |
258 | 1.96M | left = 0; |
259 | | |
260 | 22.3M | while (left <= right) { |
261 | 22.3M | m = (left + right) / 2; |
262 | 22.3M | if (MC_Opcode == imap[m].id) { |
263 | 1.96M | return m; |
264 | 1.96M | } |
265 | | |
266 | 20.4M | if (MC_Opcode < imap[m].id) |
267 | 6.54M | right = m - 1; |
268 | 13.8M | else |
269 | 13.8M | left = m + 1; |
270 | 20.4M | } |
271 | | |
272 | 0 | return -1; |
273 | 1.96M | } |
274 | | |
275 | | /// Sets the Capstone instruction id which maps to the @MI opcode. |
276 | | /// If no mapping is found the function returns and prints an error. |
277 | | void map_cs_id(MCInst *MI, const insn_map *imap, unsigned int imap_size) |
278 | 1.96M | { |
279 | 1.96M | unsigned int i = find_cs_id(MCInst_getOpcode(MI), imap, imap_size); |
280 | 1.96M | if (i != -1) { |
281 | 1.96M | MI->flat_insn->id = imap[i].mapid; |
282 | 1.96M | return; |
283 | 1.96M | } |
284 | 0 | printf("ERROR: Could not find CS id for MCInst opcode: %d\n", |
285 | 0 | MCInst_getOpcode(MI)); |
286 | 0 | return; |
287 | 1.96M | } |
288 | | |
289 | | /// Returns the operand type information from the |
290 | | /// mapping table for instruction operands. |
291 | | /// Only usable by `auto-sync` archs! |
292 | | const cs_op_type mapping_get_op_type(MCInst *MI, unsigned OpNum, |
293 | | const map_insn_ops *insn_ops_map, |
294 | | size_t map_size) |
295 | 17.6M | { |
296 | 17.6M | assert(MI); |
297 | 17.6M | assert(MI->Opcode < map_size); |
298 | 17.6M | assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) / |
299 | 17.6M | sizeof(insn_ops_map[MI->Opcode].ops[0])); |
300 | | |
301 | 17.6M | return insn_ops_map[MI->Opcode].ops[OpNum].type; |
302 | 17.6M | } |
303 | | |
304 | | /// Returns the operand access flags from the |
305 | | /// mapping table for instruction operands. |
306 | | /// Only usable by `auto-sync` archs! |
307 | | const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum, |
308 | | const map_insn_ops *insn_ops_map, |
309 | | size_t map_size) |
310 | 5.94M | { |
311 | 5.94M | assert(MI); |
312 | 5.94M | assert(MI->Opcode < map_size); |
313 | 5.94M | assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) / |
314 | 5.94M | sizeof(insn_ops_map[MI->Opcode].ops[0])); |
315 | | |
316 | 5.94M | cs_ac_type access = insn_ops_map[MI->Opcode].ops[OpNum].access; |
317 | 5.94M | if (MCInst_opIsTied(MI, OpNum) || MCInst_opIsTying(MI, OpNum)) |
318 | 515k | access |= (access == CS_AC_READ) ? CS_AC_WRITE : CS_AC_READ; |
319 | 5.94M | return access; |
320 | 5.94M | } |
321 | | |
322 | | /// Returns the operand at detail->arch.operands[op_count + offset] |
323 | | /// Or NULL if detail is not set. |
324 | | #define DEFINE_get_detail_op(arch, ARCH) \ |
325 | | cs_##arch##_op *ARCH##_get_detail_op(MCInst *MI, int offset) \ |
326 | 24.2M | { \ |
327 | 24.2M | if (!MI->flat_insn->detail) \ |
328 | 24.2M | return NULL; \ |
329 | 24.2M | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ |
330 | 24.2M | assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \ |
331 | 24.2M | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ |
332 | 24.2M | } Line | Count | Source | 326 | 16.2M | { \ | 327 | 16.2M | if (!MI->flat_insn->detail) \ | 328 | 16.2M | return NULL; \ | 329 | 16.2M | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ | 330 | 16.2M | assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \ | 331 | 16.2M | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ | 332 | 16.2M | } |
Line | Count | Source | 326 | 767k | { \ | 327 | 767k | if (!MI->flat_insn->detail) \ | 328 | 767k | return NULL; \ | 329 | 767k | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ | 330 | 767k | assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \ | 331 | 767k | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ | 332 | 767k | } |
Unexecuted instantiation: TriCore_get_detail_op Line | Count | Source | 326 | 6.05M | { \ | 327 | 6.05M | if (!MI->flat_insn->detail) \ | 328 | 6.05M | return NULL; \ | 329 | 6.05M | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ | 330 | 6.05M | assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \ | 331 | 6.05M | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ | 332 | 6.05M | } |
Unexecuted instantiation: Alpha_get_detail_op Unexecuted instantiation: HPPA_get_detail_op Unexecuted instantiation: LoongArch_get_detail_op Line | Count | Source | 326 | 717k | { \ | 327 | 717k | if (!MI->flat_insn->detail) \ | 328 | 717k | return NULL; \ | 329 | 717k | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ | 330 | 717k | assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \ | 331 | 717k | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ | 332 | 717k | } |
Line | Count | Source | 326 | 525k | { \ | 327 | 525k | if (!MI->flat_insn->detail) \ | 328 | 525k | return NULL; \ | 329 | 525k | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ | 330 | 525k | assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \ | 331 | 525k | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ | 332 | 525k | } |
|
333 | | |
334 | | DEFINE_get_detail_op(arm, ARM); |
335 | | DEFINE_get_detail_op(ppc, PPC); |
336 | | DEFINE_get_detail_op(tricore, TriCore); |
337 | | DEFINE_get_detail_op(aarch64, AArch64); |
338 | | DEFINE_get_detail_op(alpha, Alpha); |
339 | | DEFINE_get_detail_op(hppa, HPPA); |
340 | | DEFINE_get_detail_op(loongarch, LoongArch); |
341 | | DEFINE_get_detail_op(mips, Mips); |
342 | | DEFINE_get_detail_op(riscv, RISCV); |
343 | | |
344 | | /// Returns true if for this architecture the |
345 | | /// alias operands should be filled. |
346 | | /// TODO: Replace this with a proper option. |
347 | | /// So it can be toggled between disas() calls. |
348 | 3.83M | bool map_use_alias_details(const MCInst *MI) { |
349 | 3.83M | assert(MI); |
350 | 3.83M | return !(MI->csh->detail_opt & CS_OPT_DETAIL_REAL); |
351 | 3.83M | } |
352 | | |
353 | | /// Sets the setDetailOps flag to @p Val. |
354 | | /// If detail == NULLit refuses to set the flag to true. |
355 | 3.77M | void map_set_fill_detail_ops(MCInst *MI, bool Val) { |
356 | 3.77M | assert(MI); |
357 | 3.77M | if (!detail_is_set(MI)) { |
358 | 0 | MI->fillDetailOps = false; |
359 | 0 | return; |
360 | 0 | } |
361 | | |
362 | 3.77M | MI->fillDetailOps = Val; |
363 | 3.77M | } |
364 | | |
365 | | /// Sets the instruction alias flags and the given alias id. |
366 | 0 | void map_set_is_alias_insn(MCInst *MI, bool Val, uint64_t Alias) { |
367 | 0 | assert(MI); |
368 | 0 | MI->isAliasInstr = Val; |
369 | 0 | MI->flat_insn->is_alias = Val; |
370 | 0 | MI->flat_insn->alias_id = Alias; |
371 | 0 | } |
372 | | |
373 | 314k | static inline bool char_ends_mnem(const char c) { |
374 | 314k | return (!c || c == ' ' || c == '\t'); |
375 | 314k | } |
376 | | |
377 | | /// Sets an alternative id for some instruction. |
378 | | /// Or -1 if it fails. |
379 | | /// You must add (<ARCH>_INS_ALIAS_BEGIN + 1) to the id to get the real id. |
380 | 1.88M | void map_set_alias_id(MCInst *MI, const SStream *O, const name_map *alias_mnem_id_map, int map_size) { |
381 | 1.88M | if (!MCInst_isAlias(MI)) |
382 | 1.81M | return; |
383 | | |
384 | 65.0k | char alias_mnem[16] = { 0 }; |
385 | 65.0k | int i = 0, j = 0; |
386 | 65.0k | const char *asm_str_buf = O->buffer; |
387 | | // Skip spaces and tabs |
388 | 89.6k | while (is_blank_char(asm_str_buf[i])) { |
389 | 24.5k | if (!asm_str_buf[i]) { |
390 | 0 | MI->flat_insn->alias_id = -1; |
391 | 0 | return; |
392 | 0 | } |
393 | 24.5k | ++i; |
394 | 24.5k | } |
395 | 314k | for (; j < sizeof(alias_mnem) - 1; ++j, ++i) { |
396 | 314k | if (char_ends_mnem(asm_str_buf[i])) |
397 | 65.0k | break; |
398 | 249k | alias_mnem[j] = asm_str_buf[i]; |
399 | 249k | } |
400 | | |
401 | 65.0k | MI->flat_insn->alias_id = name2id(alias_mnem_id_map, map_size, alias_mnem); |
402 | 65.0k | } |
403 | | |
404 | | /// Does a binary search over the given map and searches for @id. |
405 | | /// If @id exists in @map, it sets @found to true and returns |
406 | | /// the value for the @id. |
407 | | /// Otherwise, @found is set to false and it returns UINT64_MAX. |
408 | | /// |
409 | | /// Of course it assumes the map is sorted. |
410 | | uint64_t enum_map_bin_search(const cs_enum_id_map *map, size_t map_len, |
411 | | const char *id, bool *found) |
412 | 0 | { |
413 | 0 | size_t l = 0; |
414 | 0 | size_t r = map_len; |
415 | 0 | size_t id_len = strlen(id); |
416 | |
|
417 | 0 | while (l <= r) { |
418 | 0 | size_t m = (l + r) / 2; |
419 | 0 | size_t j = 0; |
420 | 0 | size_t i = 0; |
421 | 0 | size_t entry_len = strlen(map[m].str); |
422 | |
|
423 | 0 | while (j < entry_len && i < id_len && id[i] == map[m].str[j]) { |
424 | 0 | ++j, ++i; |
425 | 0 | } |
426 | 0 | if (i == id_len && j == entry_len) { |
427 | 0 | *found = true; |
428 | 0 | return map[m].val; |
429 | 0 | } |
430 | | |
431 | 0 | if (id[i] < map[m].str[j]) { |
432 | 0 | r = m - 1; |
433 | 0 | } else if (id[i] > map[m].str[j]) { |
434 | 0 | l = m + 1; |
435 | 0 | } |
436 | 0 | if (m == 0 || (l + r) / 2 >= map_len) { |
437 | | // Break before we go out of bounds. |
438 | 0 | break; |
439 | 0 | } |
440 | 0 | } |
441 | 0 | *found = false; |
442 | 0 | return UINT64_MAX; |
443 | 0 | } |
444 | | |