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
4.74M
{
63
4.74M
  int i;
64
65
150M
  for (i = 0; i < max; i++) {
66
150M
    if (map[i].id == id) {
67
4.73M
      return map[i].name;
68
4.73M
    }
69
150M
  }
70
71
  // nothing match
72
9.99k
  return NULL;
73
4.74M
}
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
378k
{
79
378k
  if (!MI->flat_insn->detail)
80
0
    return;
81
82
378k
  uint16_t *regs_write = MI->flat_insn->detail->regs_write;
83
380k
  for (int i = 0; i < MAX_IMPL_W_REGS; ++i) {
84
380k
    if (i == MI->flat_insn->detail->regs_write_count) {
85
359k
      regs_write[i] = Reg;
86
359k
      MI->flat_insn->detail->regs_write_count++;
87
359k
      return;
88
359k
    }
89
20.6k
    if (regs_write[i] == Reg)
90
18.4k
      return;
91
20.6k
  }
92
378k
}
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
{
98
#ifndef CAPSTONE_DIET
99
  if (!MI->flat_insn->detail)
100
    return;
101
102
  cs_detail *detail = MI->flat_insn->detail;
103
  unsigned Opcode = MCInst_getOpcode(MI);
104
  unsigned i = 0;
105
  uint16_t reg = imap[Opcode].regs_use[i];
106
  while (reg != 0) {
107
    if (i >= MAX_IMPL_R_REGS ||
108
        detail->regs_read_count >= MAX_IMPL_R_REGS) {
109
      printf("ERROR: Too many implicit read register defined in "
110
             "instruction mapping.\n");
111
      return;
112
    }
113
    detail->regs_read[detail->regs_read_count++] = reg;
114
    reg = imap[Opcode].regs_use[++i];
115
  }
116
#endif // CAPSTONE_DIET
117
}
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
{
123
#ifndef CAPSTONE_DIET
124
  if (!MI->flat_insn->detail)
125
    return;
126
127
  cs_detail *detail = MI->flat_insn->detail;
128
  unsigned Opcode = MCInst_getOpcode(MI);
129
  unsigned i = 0;
130
  uint16_t reg = imap[Opcode].regs_mod[i];
131
  while (reg != 0) {
132
    if (i >= MAX_IMPL_W_REGS ||
133
        detail->regs_write_count >= MAX_IMPL_W_REGS) {
134
      printf("ERROR: Too many implicit write register defined in "
135
             "instruction mapping.\n");
136
      return;
137
    }
138
    detail->regs_write[detail->regs_write_count++] = reg;
139
    reg = imap[Opcode].regs_mod[++i];
140
  }
141
#endif // CAPSTONE_DIET
142
}
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
1.67M
{
148
1.67M
#ifndef CAPSTONE_DIET
149
1.67M
  if (!MI->flat_insn->detail)
150
0
    return;
151
152
1.67M
  cs_detail *detail = MI->flat_insn->detail;
153
1.67M
  unsigned Opcode = MCInst_getOpcode(MI);
154
1.67M
  unsigned i = 0;
155
1.67M
  uint16_t group = imap[Opcode].groups[i];
156
3.60M
  while (group != 0) {
157
1.93M
    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
1.93M
    detail->groups[detail->groups_count++] = group;
162
1.93M
    group = imap[Opcode].groups[++i];
163
1.93M
  }
164
1.67M
#endif // CAPSTONE_DIET
165
1.67M
}
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
1.67M
{
172
  // binary searching since the IDs are sorted in order
173
1.67M
  unsigned int left, right, m;
174
1.67M
  unsigned int max = imap_size;
175
176
1.67M
  right = max - 1;
177
178
1.67M
  if (MC_Opcode < imap[0].id || MC_Opcode > imap[right].id)
179
    // not found
180
0
    return -1;
181
182
1.67M
  left = 0;
183
184
18.8M
  while (left <= right) {
185
18.8M
    m = (left + right) / 2;
186
18.8M
    if (MC_Opcode == imap[m].id) {
187
1.67M
      return m;
188
1.67M
    }
189
190
17.1M
    if (MC_Opcode < imap[m].id)
191
6.09M
      right = m - 1;
192
11.0M
    else
193
11.0M
      left = m + 1;
194
17.1M
  }
195
196
0
  return -1;
197
1.67M
}
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
1.67M
{
203
1.67M
  unsigned int i = find_cs_id(MCInst_getOpcode(MI), imap, imap_size);
204
1.67M
  if (i != -1) {
205
1.67M
    MI->flat_insn->id = imap[i].mapid;
206
1.67M
    return;
207
1.67M
  }
208
0
  printf("ERROR: Could not find CS id for MCInst opcode: %d\n",
209
0
         MCInst_getOpcode(MI));
210
0
  return;
211
1.67M
}
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
13.3M
{
220
13.3M
  assert(MI);
221
13.3M
  assert(MI->Opcode < map_size);
222
13.3M
  assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) /
223
13.3M
             sizeof(insn_ops_map[MI->Opcode].ops[0]));
224
225
13.3M
  return insn_ops_map[MI->Opcode].ops[OpNum].type;
226
13.3M
}
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
5.02M
{
235
5.02M
  assert(MI);
236
5.02M
  assert(MI->Opcode < map_size);
237
5.02M
  assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) /
238
5.02M
             sizeof(insn_ops_map[MI->Opcode].ops[0]));
239
240
5.02M
  cs_ac_type access = insn_ops_map[MI->Opcode].ops[OpNum].access;
241
5.02M
  if (MCInst_opIsTied(MI, OpNum) || MCInst_opIsTying(MI, OpNum))
242
351k
    access |= (access == CS_AC_READ) ? CS_AC_WRITE : CS_AC_READ;
243
5.02M
  return access;
244
5.02M
}
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);