Coverage Report

Created: 2026-08-14 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonev5/Mapping.c
Line
Count
Source
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
7
// create a cache for fast id lookup
8
static unsigned short *make_id2insn(const insn_map *insns, unsigned int size)
9
24.7k
{
10
  // NOTE: assume that the max id is always put at the end of insns array
11
24.7k
  unsigned short max_id = insns[size - 1].id;
12
24.7k
  unsigned int i;
13
14
24.7k
  unsigned short *cache =
15
24.7k
    (unsigned short *)cs_mem_calloc(max_id + 1, sizeof(*cache));
16
24.7k
  if (cache == NULL) {
17
0
    return NULL;
18
0
  }
19
20
69.3M
  for (i = 1; i < size; i++)
21
69.3M
    cache[insns[i].id] = i;
22
23
24.7k
  return cache;
24
24.7k
}
25
26
// look for @id in @insns, given its size in @max. first time call will update
27
// @cache. return 0 if not found
28
unsigned short insn_find(const insn_map *insns, unsigned int max,
29
       unsigned int id, unsigned short **cache)
30
2.65M
{
31
2.65M
  if (id > insns[max - 1].id)
32
0
    return 0;
33
34
2.65M
  if (*cache == NULL)
35
24.7k
    *cache = make_id2insn(insns, max);
36
2.65M
  if (*cache == NULL) {
37
0
    return 0;
38
0
  }
39
40
2.65M
  return (*cache)[id];
41
2.65M
}
42
43
// Gives the id for the given @name if it is saved in @map.
44
// Returns the id or -1 if not found.
45
int name2id(const name_map *map, int max, const char *name)
46
55.3k
{
47
55.3k
  int i;
48
49
10.4M
  for (i = 0; i < max; i++) {
50
10.3M
    if (!strcmp(map[i].name, name)) {
51
32.8k
      return map[i].id;
52
32.8k
    }
53
10.3M
  }
54
55
  // nothing match
56
22.4k
  return -1;
57
55.3k
}
58
59
// Gives the name for the given @id if it is saved in @map.
60
// Returns the name or NULL if not found.
61
const char *id2name(const name_map *map, int max, const unsigned int id)
62
2.03M
{
63
2.03M
  int i;
64
65
42.9M
  for (i = 0; i < max; i++) {
66
42.9M
    if (map[i].id == id) {
67
2.03M
      return map[i].name;
68
2.03M
    }
69
42.9M
  }
70
71
  // nothing match
72
0
  return NULL;
73
2.03M
}
74
75
/// Adds a register to the implicit write register list.
76
/// It will not add the same register twice.
77
void map_add_implicit_write(MCInst *MI, uint32_t Reg)
78
0
{
79
0
  if (!MI->flat_insn->detail)
80
0
    return;
81
82
0
  uint16_t *regs_write = MI->flat_insn->detail->regs_write;
83
0
  for (int i = 0; i < MAX_IMPL_W_REGS; ++i) {
84
0
    if (i == MI->flat_insn->detail->regs_write_count) {
85
0
      regs_write[i] = Reg;
86
0
      MI->flat_insn->detail->regs_write_count++;
87
0
      return;
88
0
    }
89
0
    if (regs_write[i] == Reg)
90
0
      return;
91
0
  }
92
0
}
93
94
/// Copies the implicit read registers of @imap to @MI->flat_insn.
95
/// Already present registers will be preserved.
96
void map_implicit_reads(MCInst *MI, const insn_map *imap)
97
0
{
98
0
#ifndef CAPSTONE_DIET
99
0
  if (!MI->flat_insn->detail)
100
0
    return;
101
102
0
  cs_detail *detail = MI->flat_insn->detail;
103
0
  unsigned Opcode = MCInst_getOpcode(MI);
104
0
  unsigned i = 0;
105
0
  uint16_t reg = imap[Opcode].regs_use[i];
106
0
  while (reg != 0) {
107
0
    if (i >= MAX_IMPL_R_REGS ||
108
0
        detail->regs_read_count >= MAX_IMPL_R_REGS) {
109
0
      printf("ERROR: Too many implicit read register defined in "
110
0
             "instruction mapping.\n");
111
0
      return;
112
0
    }
113
0
    detail->regs_read[detail->regs_read_count++] = reg;
114
0
    reg = imap[Opcode].regs_use[++i];
115
0
  }
116
0
#endif // CAPSTONE_DIET
117
0
}
118
119
/// Copies the implicit write registers of @imap to @MI->flat_insn.
120
/// Already present registers will be preserved.
121
void map_implicit_writes(MCInst *MI, const insn_map *imap)
122
0
{
123
0
#ifndef CAPSTONE_DIET
124
0
  if (!MI->flat_insn->detail)
125
0
    return;
126
127
0
  cs_detail *detail = MI->flat_insn->detail;
128
0
  unsigned Opcode = MCInst_getOpcode(MI);
129
0
  unsigned i = 0;
130
0
  uint16_t reg = imap[Opcode].regs_mod[i];
131
0
  while (reg != 0) {
132
0
    if (i >= MAX_IMPL_W_REGS ||
133
0
        detail->regs_write_count >= MAX_IMPL_W_REGS) {
134
0
      printf("ERROR: Too many implicit write register defined in "
135
0
             "instruction mapping.\n");
136
0
      return;
137
0
    }
138
0
    detail->regs_write[detail->regs_write_count++] = reg;
139
0
    reg = imap[Opcode].regs_mod[++i];
140
0
  }
141
0
#endif // CAPSTONE_DIET
142
0
}
143
144
/// Copies the groups from @imap to @MI->flat_insn.
145
/// Already present groups will be preserved.
146
void map_groups(MCInst *MI, const insn_map *imap)
147
0
{
148
0
#ifndef CAPSTONE_DIET
149
0
  if (!MI->flat_insn->detail)
150
0
    return;
151
152
0
  cs_detail *detail = MI->flat_insn->detail;
153
0
  unsigned Opcode = MCInst_getOpcode(MI);
154
0
  unsigned i = 0;
155
0
  uint16_t group = imap[Opcode].groups[i];
156
0
  while (group != 0) {
157
0
    if (detail->groups_count >= MAX_NUM_GROUPS) {
158
0
      printf("ERROR: Too many groups defined in instruction mapping.\n");
159
0
      return;
160
0
    }
161
0
    detail->groups[detail->groups_count++] = group;
162
0
    group = imap[Opcode].groups[++i];
163
0
  }
164
0
#endif // CAPSTONE_DIET
165
0
}
166
167
// Search for the CS instruction id for the given @MC_Opcode in @imap.
168
// return -1 if none is found.
169
unsigned int find_cs_id(unsigned MC_Opcode, const insn_map *imap,
170
      unsigned imap_size)
171
0
{
172
  // binary searching since the IDs are sorted in order
173
0
  unsigned int left, right, m;
174
0
  unsigned int max = imap_size;
175
176
0
  right = max - 1;
177
178
0
  if (MC_Opcode < imap[0].id || MC_Opcode > imap[right].id)
179
    // not found
180
0
    return -1;
181
182
0
  left = 0;
183
184
0
  while (left <= right) {
185
0
    m = (left + right) / 2;
186
0
    if (MC_Opcode == imap[m].id) {
187
0
      return m;
188
0
    }
189
190
0
    if (MC_Opcode < imap[m].id)
191
0
      right = m - 1;
192
0
    else
193
0
      left = m + 1;
194
0
  }
195
196
0
  return -1;
197
0
}
198
199
/// Sets the Capstone instruction id which maps to the @MI opcode.
200
/// If no mapping is found the function returns and prints an error.
201
void map_cs_id(MCInst *MI, const insn_map *imap, unsigned int imap_size)
202
0
{
203
0
  unsigned int i = find_cs_id(MCInst_getOpcode(MI), imap, imap_size);
204
0
  if (i != -1) {
205
0
    MI->flat_insn->id = imap[i].mapid;
206
0
    return;
207
0
  }
208
0
  printf("ERROR: Could not find CS id for MCInst opcode: %d\n",
209
0
         MCInst_getOpcode(MI));
210
0
  return;
211
0
}
212
213
/// Returns the operand type information from the
214
/// mapping table for instruction operands.
215
/// Only usable by `auto-sync` archs!
216
const cs_op_type mapping_get_op_type(MCInst *MI, unsigned OpNum,
217
             const map_insn_ops *insn_ops_map,
218
             size_t map_size)
219
0
{
220
0
  assert(MI);
221
0
  assert(MI->Opcode < map_size);
222
0
  assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) /
223
0
             sizeof(insn_ops_map[MI->Opcode].ops[0]));
224
225
0
  return insn_ops_map[MI->Opcode].ops[OpNum].type;
226
0
}
227
228
/// Returns the operand access flags from the
229
/// mapping table for instruction operands.
230
/// Only usable by `auto-sync` archs!
231
const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum,
232
               const map_insn_ops *insn_ops_map,
233
               size_t map_size)
234
0
{
235
0
  assert(MI);
236
0
  assert(MI->Opcode < map_size);
237
0
  assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) /
238
0
             sizeof(insn_ops_map[MI->Opcode].ops[0]));
239
240
0
  cs_ac_type access = insn_ops_map[MI->Opcode].ops[OpNum].access;
241
0
  if (MCInst_opIsTied(MI, OpNum) || MCInst_opIsTying(MI, OpNum))
242
0
    access |= (access == CS_AC_READ) ? CS_AC_WRITE : CS_AC_READ;
243
0
  return access;
244
0
}
245
246
/// Returns the operand at detail->arch.operands[op_count + offset]
247
/// Or NULL if detail is not set.
248
#define DEFINE_get_detail_op(arch, ARCH) \
249
  cs_##arch##_op *ARCH##_get_detail_op(MCInst *MI, int offset) \
250
0
  { \
251
0
    if (!MI->flat_insn->detail) \
252
0
      return NULL; \
253
0
    int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \
254
0
    assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \
255
0
    return &MI->flat_insn->detail->arch.operands[OpIdx]; \
256
0
  }
257
258
0
DEFINE_get_detail_op(arm, ARM);
259
0
DEFINE_get_detail_op(ppc, PPC);
260
DEFINE_get_detail_op(tricore, TriCore);