Coverage Report

Created: 2026-05-30 06:22

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
11.1k
{
27
11.1k
  int syntax_opt = ((cs_struct *)(uintptr_t)handle)->syntax;
28
29
11.1k
  if (syntax_opt & CS_OPT_SYNTAX_NOREGNAME) {
30
0
    return RISCV_LLVM_getRegisterName(reg, RISCV_NoRegAltName);
31
0
  }
32
11.1k
  return RISCV_LLVM_getRegisterName(reg, RISCV_ABIRegAltName);
33
11.1k
}
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
130k
{
54
130k
  if (!detail_is_set(MI))
55
0
    return;
56
  // are not "true" arguments and has no Capstone equivalent
57
130k
  if (opgroup == RISCV_OP_GROUP_FRMArg ||
58
129k
      opgroup == RISCV_OP_GROUP_FRMArgLegacy)
59
2.02k
    return;
60
61
128k
  if (opgroup == RISCV_OP_GROUP_FPImmOperand) {
62
440
    unsigned Imm = (unsigned)MCInst_getOperand(MI, OpNum)->ImmVal;
63
440
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, OpNum);
64
440
    op->type = RISCV_OP_FP;
65
440
    op->access = (cs_ac_type)map_get_op_access(MI, OpNum);
66
440
    switch (Imm) {
67
21
    case 1: // min
68
21
      switch (MI->Opcode) {
69
12
      case RISCV_FLI_S:
70
12
        op->dimm = (double)FLT_MIN;
71
12
        break;
72
0
      case RISCV_FLI_D:
73
0
        op->dimm = (double)DBL_MIN;
74
0
        break;
75
9
      case RISCV_FLI_H:
76
9
        op->dimm = 6.103515625e-05;
77
9
        break;
78
0
      default:
79
0
        op->dimm = 0.0;
80
0
        break;
81
21
      }
82
21
      break;
83
97
    case 30: // inf
84
97
      op->dimm = INFINITY;
85
97
      break;
86
91
    case 31: // nan
87
91
      op->dimm = NAN;
88
91
      break;
89
231
    default:
90
231
      op->dimm = (double)getFPImm(Imm);
91
231
      break;
92
440
    }
93
440
    RISCV_inc_op_count(MI);
94
440
    return;
95
440
  }
96
128k
  cs_riscv_op *op = RISCV_get_detail_op_at(MI, OpNum);
97
128k
  op->type = (riscv_op_type)map_get_op_type(MI, OpNum);
98
128k
  op->access = (cs_ac_type)map_get_op_access(MI, OpNum);
99
128k
  switch (map_get_op_type(MI, OpNum)) {
100
77.1k
  case CS_OP_REG:
101
77.1k
    op->reg = MCInst_getOperand(MI, OpNum)->RegVal;
102
77.1k
    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
27.4k
  case CS_OP_IMM: {
108
27.4k
    uint64_t val = MCInst_getOperand(MI, OpNum)->ImmVal;
109
27.4k
    if (opgroup != RISCV_OP_GROUP_CSRSystemRegister) {
110
26.1k
      op->imm = val;
111
26.1k
      if (opgroup == RISCV_OP_GROUP_BranchOperand) {
112
4.22k
        op->imm += MI->address;
113
4.22k
      }
114
26.1k
    } else /* system register read-write */ {
115
1.29k
      op->type = RISCV_OP_CSR;
116
1.29k
      op->csr = val;
117
      // CSR instruction always read-writes the system operand
118
1.29k
      op->access = CS_AC_READ_WRITE;
119
1.29k
    }
120
27.4k
    break;
121
0
  }
122
12.7k
  case CS_OP_MEM_REG:
123
12.7k
    op->type = (riscv_op_type)CS_OP_MEM;
124
12.7k
    op->mem.base = MCInst_getOperand(MI, OpNum)->RegVal;
125
12.7k
    break;
126
10.9k
  case CS_OP_MEM_IMM:
127
    // fill in the disp in the last operand
128
10.9k
    op = RISCV_get_detail_op_at(MI, OpNum - 1);
129
10.9k
    op->type = (riscv_op_type)CS_OP_MEM;
130
10.9k
    op->mem.disp = MCInst_getOperand(MI, OpNum)->ImmVal;
131
10.9k
    RISCV_dec_op_count(
132
10.9k
      MI); // don't increase the count, cancel the coming increment
133
10.9k
    break;
134
105
  case CS_OP_INVALID:
135
105
    break;
136
0
  default: {
137
0
    CS_ASSERT(0 && "unhandled operand type");
138
0
  }
139
128k
  }
140
128k
  RISCV_inc_op_count(MI);
141
128k
}
142
143
static inline void RISCV_add_adhoc_groups(MCInst *MI);
144
145
void RISCV_add_groups(MCInst *MI)
146
55.1k
{
147
55.1k
  if (!detail_is_set(MI))
148
0
    return;
149
150
55.1k
  get_detail(MI)->groups_count = 0;
151
152
55.1k
#ifndef CAPSTONE_DIET
153
55.1k
  int i = 0;
154
125k
  while (insns[MI->Opcode].groups[i] != 0) {
155
70.4k
    add_group(MI, insns[MI->Opcode].groups[i]);
156
70.4k
    i++;
157
70.4k
  }
158
55.1k
#endif
159
160
55.1k
  RISCV_add_adhoc_groups(MI);
161
55.1k
}
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
55.1k
{
170
55.1k
  const uint8_t *bytes = MI->flat_insn->bytes;
171
55.1k
  uint8_t opcode = bytes[0] & 0x80;
172
  // no privileged instruction has a major opcode other than SYSTEM
173
55.1k
  if (opcode != RISCV_RISCVOPCODE_SYSTEM) {
174
55.1k
    return;
175
55.1k
  }
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
55.1k
{
196
55.1k
  if (MI->Opcode == RISCV_ECALL || MI->Opcode == RISCV_EBREAK) {
197
102
    add_group(MI, RISCV_GRP_INT);
198
102
  }
199
55.1k
}
200
201
static inline void RISCV_add_interrupt_ret_group(MCInst *MI)
202
55.1k
{
203
55.1k
  if (MI->Opcode == RISCV_MRET || MI->Opcode == RISCV_SRET) {
204
21
    add_group(MI, RISCV_GRP_IRET);
205
21
  }
206
55.1k
}
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
55.1k
{
212
55.1k
  if (MI->Opcode == RISCV_JAL || MI->Opcode == RISCV_JALR) {
213
1.32k
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 0);
214
1.32k
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
215
491
        op->reg != RISCV_REG_X0 && (op->access & CS_AC_WRITE)) {
216
491
      add_group(MI, RISCV_GRP_CALL);
217
491
    }
218
1.32k
    if (MI->Opcode == RISCV_JAL) {
219
269
      add_group(MI, RISCV_GRP_BRANCH_RELATIVE);
220
269
    }
221
1.32k
  }
222
55.1k
}
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
55.1k
{
227
55.1k
  if (MI->Opcode == RISCV_C_JR) {
228
    // indirect jumps whose source is ra
229
561
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 0);
230
561
    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
561
    } else {
234
561
      add_group(MI, RISCV_GRP_JUMP);
235
561
    }
236
561
  }
237
55.1k
  if (MI->Opcode == RISCV_JALR) {
238
    // indirect jumps whose source is ra
239
1.05k
    cs_riscv_op *dstreg = RISCV_get_detail_op_at(MI, 0);
240
1.05k
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 1);
241
1.05k
    cs_riscv_op *op2 = RISCV_get_detail_op_at(MI, 2);
242
1.05k
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
243
685
        op->reg == RISCV_REG_X1 &&
244
306
        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
1.05k
    } else {
249
1.05k
      if (!((dstreg->type == (riscv_op_type)CS_OP_REG) &&
250
254
            dstreg->reg != RISCV_REG_X0 &&
251
802
            (dstreg->access & CS_AC_WRITE))) {
252
802
        add_group(MI, RISCV_GRP_JUMP);
253
802
      }
254
1.05k
    }
255
1.05k
  }
256
55.1k
}
257
258
static inline void RISCV_add_adhoc_groups(MCInst *MI)
259
55.1k
{
260
55.1k
  RISCV_add_privileged_group(MI);
261
55.1k
  RISCV_add_interrupt_group(MI);
262
55.1k
  RISCV_add_interrupt_ret_group(MI);
263
55.1k
  RISCV_add_call_group(MI);
264
55.1k
  RISCV_add_ret_group(MI);
265
55.1k
}
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
55.9k
{
270
55.9k
  if (detail_is_set(MI))
271
55.9k
    memset(get_detail(MI), 0,
272
55.9k
           offsetof(cs_detail, riscv) + sizeof(cs_riscv));
273
55.9k
}
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
55.1k
{
281
55.1k
  if (!detail_is_set(MI))
282
0
    return;
283
55.1k
  cs_riscv_op *ops = RISCV_get_detail(MI)->operands;
284
55.1k
  unsigned int write_pos = 0;
285
286
  // Move valid elements to front
287
496k
  for (unsigned int read_pos = 0; read_pos < NUM_RISCV_OPS; read_pos++) {
288
441k
    if (ops[read_pos].type != (riscv_op_type)CS_OP_INVALID) {
289
128k
      if (write_pos != read_pos) {
290
14.4k
        ops[write_pos] = ops[read_pos];
291
14.4k
      }
292
128k
      write_pos++;
293
128k
    }
294
441k
  }
295
  // fill the rest, if any, with invalid
296
55.1k
  memset((void *)(&ops[write_pos]), CS_OP_INVALID,
297
55.1k
         (NUM_RISCV_OPS - write_pos) * sizeof(cs_riscv_op));
298
55.1k
}
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
55.1k
{
308
55.1k
  if (!detail_is_set(MI))
309
0
    return;
310
55.1k
  if (!isCompressed(MI))
311
20.3k
    return;
312
313
34.8k
  cs_riscv *riscv_details = RISCV_get_detail(MI);
314
34.8k
  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
34.8k
  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
34.8k
}
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
55.1k
{
326
55.1k
  insn_map const *insn_map = NULL;
327
328
55.1k
  if ((insn_map = lookup_insn_map(h, id))) {
329
55.1k
    insn->id = insn_map->mapid;
330
331
55.1k
    if (h->detail_opt) {
332
55.1k
#ifndef CAPSTONE_DIET
333
55.1k
      memcpy(insn->detail->regs_read, insn_map->regs_use,
334
55.1k
             sizeof(insn_map->regs_use));
335
55.1k
      insn->detail->regs_read_count =
336
55.1k
        (uint8_t)count_positive(insn_map->regs_use);
337
338
55.1k
      memcpy(insn->detail->regs_write, insn_map->regs_mod,
339
55.1k
             sizeof(insn_map->regs_mod));
340
55.1k
      insn->detail->regs_write_count =
341
55.1k
        (uint8_t)count_positive(insn_map->regs_mod);
342
343
55.1k
      memcpy(insn->detail->groups, insn_map->groups,
344
55.1k
             sizeof(insn_map->groups));
345
55.1k
      insn->detail->groups_count =
346
55.1k
        (uint8_t)count_positive8(insn_map->groups);
347
348
55.1k
      if (insn_map->branch || insn_map->indirect_branch) {
349
        // this insn also belongs to JUMP group. add JUMP group
350
2.93k
        insn->detail
351
2.93k
          ->groups[insn->detail->groups_count] =
352
2.93k
          RISCV_GRP_JUMP;
353
2.93k
        insn->detail->groups_count++;
354
2.93k
      }
355
55.1k
#endif
356
55.1k
    }
357
55.1k
  }
358
55.1k
}
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
55.1k
{
368
55.1k
#ifndef CAPSTONE_DIET
369
55.1k
  map_set_alias_id(MI, O, insn_alias_mnem_map,
370
55.1k
       ARR_SIZE(insn_alias_mnem_map));
371
55.1k
#endif
372
55.1k
}
373
374
const char *RISCV_insn_name(csh handle, unsigned int id)
375
55.1k
{
376
55.1k
#ifndef CAPSTONE_DIET
377
55.1k
  if (id < RISCV_INS_ENDING)
378
55.1k
    return insn_name_maps[id];
379
380
0
  if (id > RISCV_INS_ALIAS_BEGIN && 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
72.6k
{
407
72.6k
#ifndef CAPSTONE_DIET
408
  // verify group id
409
  // if past the end
410
72.6k
  if (id >= RISCV_GRP_ENDING ||
411
      // or in the encoding gap between generic groups and arch-specific groups
412
72.6k
      (id > RISCV_GRP_BRANCH_RELATIVE && id < RISCV_FEATURE_HASSTDEXTI))
413
0
    return NULL;
414
72.6k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
415
#else
416
  return NULL;
417
#endif
418
72.6k
}
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_reg_access(const cs_insn *insn, cs_regs regs_read,
438
          uint8_t *regs_read_count, cs_regs regs_write,
439
          uint8_t *regs_write_count)
440
0
{
441
0
  const cs_riscv *riscv = &(insn->detail->riscv);
442
0
  uint8_t read_count = 0;
443
0
  uint8_t write_count = 0;
444
445
0
  for (int j = 0; j < riscv->op_count; j++) {
446
0
    const cs_riscv_op *op = &riscv->operands[j];
447
448
0
    if (op->type == RISCV_OP_REG) {
449
0
      if ((op->access & CS_AC_WRITE) &&
450
0
          !arr_exist(regs_write, write_count, op->reg)) {
451
0
        regs_write[write_count++] = (uint16_t)op->reg;
452
0
      }
453
0
      if ((op->access & CS_AC_READ) &&
454
0
          !arr_exist(regs_read, read_count, op->reg)) {
455
0
        regs_read[read_count++] = (uint16_t)op->reg;
456
0
      }
457
0
    } else if (op->type == RISCV_OP_MEM) {
458
0
      if (op->mem.base != RISCV_REG_INVALID &&
459
0
          !arr_exist(regs_read, read_count, op->mem.base)) {
460
0
        regs_read[read_count++] =
461
0
          (uint16_t)op->mem.base;
462
0
      }
463
0
    }
464
0
  }
465
466
0
  *regs_read_count = read_count;
467
0
  *regs_write_count = write_count;
468
0
}
469
470
void RISCV_init(MCRegisterInfo *MRI)
471
1.11k
{
472
1.11k
  MCRegisterInfo_InitMCRegisterInfo(MRI, RISCVRegDesc, RISCV_REG_ENDING,
473
1.11k
            0, 0, RISCVMCRegisterClasses,
474
1.11k
            ARR_SIZE(RISCVMCRegisterClasses), 0,
475
1.11k
            0, RISCVRegDiffLists, 0,
476
1.11k
            RISCVSubRegIdxLists,
477
1.11k
            ARR_SIZE(RISCVSubRegIdxLists), 0);
478
1.11k
}
479
480
#endif