Coverage Report

Created: 2026-04-12 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/RISCV/RISCVMapping.c
Line
Count
Source
1
#include "capstone/cs_operand.h"
2
#include "capstone/riscv.h"
3
#include <stdint.h>
4
#include <float.h>
5
#include <math.h>
6
#ifdef CAPSTONE_HAS_RISCV
7
8
#include <string.h>
9
10
#include "../../Mapping.h"
11
#include "../../cs_simple_types.h"
12
#include "../../utils.h"
13
14
#include "RISCVMapping.h"
15
16
#define GET_INSTRINFO_ENUM
17
#include "RISCVGenInstrInfo.inc"
18
19
#define GET_REGINFO_ENUM
20
#define GET_REGINFO_MC_DESC
21
#include "RISCVGenRegisterInfo.inc"
22
23
#include "RISCVInstPrinter.h"
24
25
const char *RISCV_reg_name(csh handle, unsigned int reg)
26
8.91k
{
27
8.91k
  int syntax_opt = ((cs_struct *)(uintptr_t)handle)->syntax;
28
29
8.91k
  if (syntax_opt & CS_OPT_SYNTAX_NOREGNAME) {
30
0
    return RISCV_LLVM_getRegisterName(reg, RISCV_NoRegAltName);
31
0
  }
32
8.91k
  return RISCV_LLVM_getRegisterName(reg, RISCV_ABIRegAltName);
33
8.91k
}
34
35
static const insn_map insns[] = {
36
#include "RISCVGenCSMappingInsn.inc"
37
};
38
39
const insn_map *RISCV_insns = insns;
40
const unsigned int RISCV_insn_count = ARR_SIZE(insns);
41
42
#ifndef CAPSTONE_DIET
43
static const map_insn_ops insn_operands[] = {
44
#include "RISCVGenCSMappingInsnOp.inc"
45
};
46
47
static const name_map insn_alias_mnem_map[] = {
48
#include "RISCVGenCSAliasMnemMap.inc"
49
};
50
#endif
51
52
void RISCV_add_cs_detail_0(MCInst *MI, riscv_op_group opgroup, unsigned OpNum)
53
90.9k
{
54
90.9k
  if (!detail_is_set(MI))
55
0
    return;
56
  // are not "true" arguments and has no Capstone equivalent
57
90.9k
  if (opgroup == RISCV_OP_GROUP_FRMArg ||
58
90.0k
      opgroup == RISCV_OP_GROUP_FRMArgLegacy)
59
1.11k
    return;
60
61
89.8k
  if (opgroup == RISCV_OP_GROUP_FPImmOperand) {
62
431
    unsigned Imm = (unsigned)MCInst_getOperand(MI, OpNum)->ImmVal;
63
431
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, OpNum);
64
431
    op->type = RISCV_OP_FP;
65
431
    op->access = (cs_ac_type)map_get_op_access(MI, OpNum);
66
431
    switch (Imm) {
67
71
    case 1: // min
68
71
      switch (MI->Opcode) {
69
60
      case RISCV_FLI_S:
70
60
        op->dimm = (double)FLT_MIN;
71
60
        break;
72
0
      case RISCV_FLI_D:
73
0
        op->dimm = (double)DBL_MIN;
74
0
        break;
75
11
      case RISCV_FLI_H:
76
11
        op->dimm = 6.103515625e-05;
77
11
        break;
78
0
      default:
79
0
        op->dimm = 0.0;
80
0
        break;
81
71
      }
82
71
      break;
83
197
    case 30: // inf
84
197
      op->dimm = INFINITY;
85
197
      break;
86
16
    case 31: // nan
87
16
      op->dimm = NAN;
88
16
      break;
89
147
    default:
90
147
      op->dimm = (double)getFPImm(Imm);
91
147
      break;
92
431
    }
93
431
    RISCV_inc_op_count(MI);
94
431
    return;
95
431
  }
96
89.4k
  cs_riscv_op *op = RISCV_get_detail_op_at(MI, OpNum);
97
89.4k
  op->type = (riscv_op_type)map_get_op_type(MI, OpNum);
98
89.4k
  op->access = (cs_ac_type)map_get_op_access(MI, OpNum);
99
89.4k
  switch (map_get_op_type(MI, OpNum)) {
100
53.5k
  case CS_OP_REG:
101
53.5k
    op->reg = MCInst_getOperand(MI, OpNum)->RegVal;
102
53.5k
    break;
103
0
  case CS_OP_MEM:
104
0
    op->mem.base = 0;
105
0
    op->mem.disp = MCInst_getOperand(MI, OpNum)->ImmVal;
106
0
    break;
107
19.4k
  case CS_OP_IMM: {
108
19.4k
    uint64_t val = MCInst_getOperand(MI, OpNum)->ImmVal;
109
19.4k
    if (opgroup != RISCV_OP_GROUP_CSRSystemRegister) {
110
18.5k
      op->imm = val;
111
18.5k
      if (opgroup == RISCV_OP_GROUP_BranchOperand) {
112
3.61k
        op->imm += MI->address;
113
3.61k
      }
114
18.5k
    } else /* system register read-write */ {
115
975
      op->type = RISCV_OP_CSR;
116
975
      op->csr = val;
117
      // CSR instruction always read-writes the system operand
118
975
      op->access = CS_AC_READ_WRITE;
119
975
    }
120
19.4k
    break;
121
0
  }
122
8.88k
  case CS_OP_MEM_REG:
123
8.88k
    op->type = (riscv_op_type)CS_OP_MEM;
124
8.88k
    op->mem.base = MCInst_getOperand(MI, OpNum)->RegVal;
125
8.88k
    break;
126
7.23k
  case CS_OP_MEM_IMM:
127
    // fill in the disp in the last operand
128
7.23k
    op = RISCV_get_detail_op_at(MI, OpNum - 1);
129
7.23k
    op->type = (riscv_op_type)CS_OP_MEM;
130
7.23k
    op->mem.disp = MCInst_getOperand(MI, OpNum)->ImmVal;
131
7.23k
    RISCV_dec_op_count(
132
7.23k
      MI); // don't increase the count, cancel the coming increment
133
7.23k
    break;
134
277
  case CS_OP_INVALID:
135
277
    break;
136
0
  default: {
137
0
    CS_ASSERT(0 && "unhandled operand type");
138
0
  }
139
89.4k
  }
140
89.4k
  RISCV_inc_op_count(MI);
141
89.4k
}
142
143
static inline void RISCV_add_adhoc_groups(MCInst *MI);
144
145
void RISCV_add_groups(MCInst *MI)
146
37.3k
{
147
37.3k
  if (!detail_is_set(MI))
148
0
    return;
149
150
37.3k
  get_detail(MI)->groups_count = 0;
151
152
37.3k
#ifndef CAPSTONE_DIET
153
37.3k
  int i = 0;
154
82.3k
  while (insns[MI->Opcode].groups[i] != 0) {
155
45.0k
    add_group(MI, insns[MI->Opcode].groups[i]);
156
45.0k
    i++;
157
45.0k
  }
158
37.3k
#endif
159
160
37.3k
  RISCV_add_adhoc_groups(MI);
161
37.3k
}
162
163
enum {
164
#define GET_ENUM_VALUES_RISCVOpcode
165
#include "RISCVGenCSSystemOperandsEnum.inc"
166
};
167
168
static inline void RISCV_add_privileged_group(MCInst *MI)
169
37.3k
{
170
37.3k
  const uint8_t *bytes = MI->flat_insn->bytes;
171
37.3k
  uint8_t opcode = bytes[0] & 0x80;
172
  // no privileged instruction has a major opcode other than SYSTEM
173
37.3k
  if (opcode != RISCV_RISCVOPCODE_SYSTEM) {
174
37.3k
    return;
175
37.3k
  }
176
0
  uint8_t func3 = (bytes[1] >> 4) & 0x7;
177
  // no privileged instruction has a minor opcode other than PRIV or PRIVM
178
0
  if (func3 != 0 && func3 != 0x4) {
179
0
    return;
180
0
  }
181
0
  uint16_t func12 = readBytes16(MI, &(bytes[2])) >> 4;
182
  // ecall and ebreak has SYSTEM and PRIV but aren't privileged
183
0
  if (func12 == 0 || func12 == 1) {
184
0
    return;
185
0
  }
186
0
  uint8_t func6 = func12 >> 6;
187
  // a subspace under extension-defined custom SYSTEM instructions that is not privileged
188
0
  if (func6 == 0x23 || func6 == 0x33) {
189
0
    return;
190
0
  }
191
0
  add_group(MI, RISCV_GRP_PRIVILEGE);
192
0
}
193
194
static inline void RISCV_add_interrupt_group(MCInst *MI)
195
37.3k
{
196
37.3k
  if (MI->Opcode == RISCV_ECALL || MI->Opcode == RISCV_EBREAK) {
197
37
    add_group(MI, RISCV_GRP_INT);
198
37
  }
199
37.3k
}
200
201
static inline void RISCV_add_interrupt_ret_group(MCInst *MI)
202
37.3k
{
203
37.3k
  if (MI->Opcode == RISCV_MRET || MI->Opcode == RISCV_SRET) {
204
13
    add_group(MI, RISCV_GRP_IRET);
205
13
  }
206
37.3k
}
207
208
// calls are implemented in RISCV as plain jumps that happen to set a link register containing the return address
209
// but this link register could be given as the null register x0, discarding the return address and making them jumps
210
static inline void RISCV_add_call_group(MCInst *MI)
211
37.3k
{
212
37.3k
  if (MI->Opcode == RISCV_JAL || MI->Opcode == RISCV_JALR) {
213
1.63k
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 0);
214
1.63k
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
215
1.23k
        op->reg != RISCV_REG_X0 && (op->access & CS_AC_WRITE)) {
216
1.23k
      add_group(MI, RISCV_GRP_CALL);
217
1.23k
    }
218
1.63k
    if (MI->Opcode == RISCV_JAL) {
219
1.37k
      add_group(MI, RISCV_GRP_BRANCH_RELATIVE);
220
1.37k
    }
221
1.63k
  }
222
37.3k
}
223
224
// returns are implemented in RISCV as a plain indirect jump that happen to reference the return address register ra == x1
225
static inline void RISCV_add_ret_group(MCInst *MI)
226
37.3k
{
227
37.3k
  if (MI->Opcode == RISCV_C_JR) {
228
    // indirect jumps whose source is ra
229
2.11k
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 0);
230
2.11k
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
231
0
        op->reg == RISCV_REG_X1) {
232
0
      add_group(MI, RISCV_GRP_RET);
233
2.11k
    } else {
234
2.11k
      add_group(MI, RISCV_GRP_JUMP);
235
2.11k
    }
236
2.11k
  }
237
37.3k
  if (MI->Opcode == RISCV_JALR) {
238
    // indirect jumps whose source is ra
239
262
    cs_riscv_op *dstreg = RISCV_get_detail_op_at(MI, 0);
240
262
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 1);
241
262
    cs_riscv_op *op2 = RISCV_get_detail_op_at(MI, 2);
242
262
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
243
212
        op->reg == RISCV_REG_X1 &&
244
143
        op2->type == (riscv_op_type)CS_OP_IMM && op2->imm == 0 &&
245
0
        dstreg->type == (riscv_op_type)CS_OP_REG &&
246
0
        dstreg->reg == RISCV_REG_X0) {
247
0
      add_group(MI, RISCV_GRP_RET);
248
262
    } else {
249
262
      if (!((dstreg->type == (riscv_op_type)CS_OP_REG) &&
250
178
            dstreg->reg != RISCV_REG_X0 &&
251
178
            (dstreg->access & CS_AC_WRITE))) {
252
84
        add_group(MI, RISCV_GRP_JUMP);
253
84
      }
254
262
    }
255
262
  }
256
37.3k
}
257
258
static inline void RISCV_add_adhoc_groups(MCInst *MI)
259
37.3k
{
260
37.3k
  RISCV_add_privileged_group(MI);
261
37.3k
  RISCV_add_interrupt_group(MI);
262
37.3k
  RISCV_add_interrupt_ret_group(MI);
263
37.3k
  RISCV_add_call_group(MI);
264
37.3k
  RISCV_add_ret_group(MI);
265
37.3k
}
266
267
// memset all stalled values in the detail struct to 0 before disassembling any next instruction
268
void RISCV_init_cs_detail(MCInst *MI)
269
38.0k
{
270
38.0k
  if (detail_is_set(MI))
271
38.0k
    memset(get_detail(MI), 0,
272
38.0k
           offsetof(cs_detail, riscv) + sizeof(cs_riscv));
273
38.0k
}
274
275
// for weird reasons some instructions end up with valid operands that are
276
// interspersed with invalid operands, i.e. the operands array is an "island"
277
// of valid operands with invalid gaps between them, this function will compactify
278
// all the valid operands and pad the rest of the array to invalid
279
void RISCV_compact_operands(MCInst *MI)
280
37.3k
{
281
37.3k
  if (!detail_is_set(MI))
282
0
    return;
283
37.3k
  cs_riscv_op *ops = RISCV_get_detail(MI)->operands;
284
37.3k
  unsigned int write_pos = 0;
285
286
  // Move valid elements to front
287
336k
  for (unsigned int read_pos = 0; read_pos < NUM_RISCV_OPS; read_pos++) {
288
298k
    if (ops[read_pos].type != (riscv_op_type)CS_OP_INVALID) {
289
89.5k
      if (write_pos != read_pos) {
290
11.3k
        ops[write_pos] = ops[read_pos];
291
11.3k
      }
292
89.5k
      write_pos++;
293
89.5k
    }
294
298k
  }
295
  // fill the rest, if any, with invalid
296
37.3k
  memset((void *)(&ops[write_pos]), CS_OP_INVALID,
297
37.3k
         (NUM_RISCV_OPS - write_pos) * sizeof(cs_riscv_op));
298
37.3k
}
299
300
// some RISC-V instructions have only 2 apparent operands, one of them is read-write
301
// the actual operand information for those instruction should have 3 operands, the first and second are the same operand,
302
// but once with read and once write access
303
// when those instructions are disassembled only the operand entry with the read access is used,
304
// and therefore the read-write operand is wrongly classified as only-read
305
// this logic tries to correct that
306
void RISCV_add_missing_write_access(MCInst *MI)
307
37.3k
{
308
37.3k
  if (!detail_is_set(MI))
309
0
    return;
310
37.3k
  if (!isCompressed(MI))
311
14.7k
    return;
312
313
22.6k
  cs_riscv *riscv_details = RISCV_get_detail(MI);
314
22.6k
  cs_riscv_op *ops = riscv_details->operands;
315
  // make the detection condition as specific as possible
316
  // so it doesn't accidentally trigger for other cases
317
22.6k
  if (riscv_details->op_count == 2 && ops[0].type == RISCV_OP_INVALID &&
318
0
      ops[1].type == RISCV_OP_REG && ops[1].access == CS_AC_READ) {
319
0
    ops[1].access |= CS_AC_WRITE;
320
0
  }
321
22.6k
}
322
323
// given internal insn id, return public instruction info
324
void RISCV_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
325
37.3k
{
326
37.3k
  insn_map const *insn_map = NULL;
327
328
37.3k
  if ((insn_map = lookup_insn_map(h, id))) {
329
37.3k
    insn->id = insn_map->mapid;
330
331
37.3k
    if (h->detail_opt) {
332
37.3k
#ifndef CAPSTONE_DIET
333
37.3k
      memcpy(insn->detail->regs_read, insn_map->regs_use,
334
37.3k
             sizeof(insn_map->regs_use));
335
37.3k
      insn->detail->regs_read_count =
336
37.3k
        (uint8_t)count_positive(insn_map->regs_use);
337
338
37.3k
      memcpy(insn->detail->regs_write, insn_map->regs_mod,
339
37.3k
             sizeof(insn_map->regs_mod));
340
37.3k
      insn->detail->regs_write_count =
341
37.3k
        (uint8_t)count_positive(insn_map->regs_mod);
342
343
37.3k
      memcpy(insn->detail->groups, insn_map->groups,
344
37.3k
             sizeof(insn_map->groups));
345
37.3k
      insn->detail->groups_count =
346
37.3k
        (uint8_t)count_positive8(insn_map->groups);
347
348
37.3k
      if (insn_map->branch || insn_map->indirect_branch) {
349
        // this insn also belongs to JUMP group. add JUMP group
350
1.63k
        insn->detail
351
1.63k
          ->groups[insn->detail->groups_count] =
352
1.63k
          RISCV_GRP_JUMP;
353
1.63k
        insn->detail->groups_count++;
354
1.63k
      }
355
37.3k
#endif
356
37.3k
    }
357
37.3k
  }
358
37.3k
}
359
360
static const char *const insn_name_maps[] = {
361
#include "RISCVGenCSMappingInsnName.inc"
362
};
363
364
// called from RISCV_LLVM_printInstruction() to avoid exporting
365
// insn_alias_mnem_map and its size via extern declarations
366
void RISCV_set_alias_id(MCInst *MI, SStream *O)
367
37.3k
{
368
37.3k
#ifndef CAPSTONE_DIET
369
37.3k
  map_set_alias_id(MI, O, insn_alias_mnem_map,
370
37.3k
       ARR_SIZE(insn_alias_mnem_map));
371
37.3k
#endif
372
37.3k
}
373
374
const char *RISCV_insn_name(csh handle, unsigned int id)
375
37.3k
{
376
37.3k
#ifndef CAPSTONE_DIET
377
37.3k
  if (id < RISCV_INS_ENDING)
378
37.3k
    return insn_name_maps[id];
379
380
0
  if (id < RISCV_INS_ALIAS_END)
381
0
    return insn_alias_mnem_map[id - RISCV_INS_ALIAS_BEGIN - 1].name;
382
0
#endif
383
0
  return NULL;
384
0
}
385
386
#ifndef CAPSTONE_DIET
387
static const name_map group_name_maps[] = {
388
  // generic groups
389
  { RISCV_GRP_INVALID, NULL },
390
  { RISCV_GRP_JUMP, "jump" },
391
  { RISCV_GRP_CALL, "call" },
392
  { RISCV_GRP_RET, "ret" },
393
  { RISCV_GRP_INT, "int" },
394
  { RISCV_GRP_IRET, "iret" },
395
  { RISCV_GRP_PRIVILEGE, "privileged" },
396
  { RISCV_GRP_BRANCH_RELATIVE, "branch_relative" },
397
398
// architecture specific
399
#include "RISCVGenCSFeatureName.inc"
400
401
  { RISCV_GRP_ENDING, NULL }
402
};
403
#endif
404
405
const char *RISCV_group_name(csh handle, unsigned int id)
406
49.8k
{
407
49.8k
#ifndef CAPSTONE_DIET
408
  // verify group id
409
  // if past the end
410
49.8k
  if (id >= RISCV_GRP_ENDING ||
411
      // or in the encoding gap between generic groups and arch-specific groups
412
49.8k
      (id > RISCV_GRP_BRANCH_RELATIVE && id < RISCV_FEATURE_HASSTDEXTI))
413
0
    return NULL;
414
49.8k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
415
#else
416
  return NULL;
417
#endif
418
49.8k
}
419
420
// map instruction name to public instruction ID
421
riscv_insn RISCV_map_insn(const char *name)
422
0
{
423
0
  unsigned int i;
424
0
  for (i = 1; i < ARR_SIZE(insn_name_maps); i++) {
425
0
    if (!strcmp(name, insn_name_maps[i]))
426
0
      return i;
427
0
  }
428
0
#ifndef CAPSTONE_DIET
429
0
  for (i = 0; i < ARR_SIZE(insn_alias_mnem_map); i++) {
430
0
    if (!strcmp(name, insn_alias_mnem_map[i].name))
431
0
      return insn_alias_mnem_map[i].id;
432
0
  }
433
0
#endif
434
0
  return RISCV_INS_INVALID;
435
0
}
436
437
void RISCV_init(MCRegisterInfo *MRI)
438
923
{
439
923
  MCRegisterInfo_InitMCRegisterInfo(MRI, RISCVRegDesc, RISCV_REG_ENDING,
440
923
            0, 0, RISCVMCRegisterClasses,
441
923
            ARR_SIZE(RISCVMCRegisterClasses), 0,
442
923
            0, RISCVRegDiffLists, 0,
443
923
            RISCVSubRegIdxLists,
444
923
            ARR_SIZE(RISCVSubRegIdxLists), 0);
445
923
}
446
447
#endif