Coverage Report

Created: 2026-04-29 06:06

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