Coverage Report

Created: 2026-03-03 06:14

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