Coverage Report

Created: 2025-11-09 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/WASM/WASMDisassembler.c
Line
Count
Source
1
/* Capstone Disassembly Engine */
2
/* By Spike, xwings 2019 */
3
4
#include <string.h>
5
#include <stddef.h>
6
7
#include "WASMDisassembler.h"
8
#include "WASMMapping.h"
9
#include "../../cs_priv.h"
10
11
static const short opcodes[256] = {
12
  WASM_INS_UNREACHABLE,
13
  WASM_INS_NOP,
14
  WASM_INS_BLOCK,
15
  WASM_INS_LOOP,
16
  WASM_INS_IF,
17
  WASM_INS_ELSE,
18
  -1,
19
  -1,
20
  -1,
21
  -1,
22
  -1,
23
  WASM_INS_END,
24
  WASM_INS_BR,
25
  WASM_INS_BR_IF,
26
  WASM_INS_BR_TABLE,
27
  WASM_INS_RETURN,
28
  WASM_INS_CALL,
29
  WASM_INS_CALL_INDIRECT,
30
  -1,
31
  -1,
32
  -1,
33
  -1,
34
  -1,
35
  -1,
36
  -1,
37
  -1,
38
  WASM_INS_DROP,
39
  WASM_INS_SELECT,
40
  -1,
41
  -1,
42
  -1,
43
  -1,
44
  WASM_INS_GET_LOCAL,
45
  WASM_INS_SET_LOCAL,
46
  WASM_INS_TEE_LOCAL,
47
  WASM_INS_GET_GLOBAL,
48
  WASM_INS_SET_GLOBAL,
49
  -1,
50
  -1,
51
  -1,
52
  WASM_INS_I32_LOAD,
53
  WASM_INS_I64_LOAD,
54
  WASM_INS_F32_LOAD,
55
  WASM_INS_F64_LOAD,
56
  WASM_INS_I32_LOAD8_S,
57
  WASM_INS_I32_LOAD8_U,
58
  WASM_INS_I32_LOAD16_S,
59
  WASM_INS_I32_LOAD16_U,
60
  WASM_INS_I64_LOAD8_S,
61
  WASM_INS_I64_LOAD8_U,
62
  WASM_INS_I64_LOAD16_S,
63
  WASM_INS_I64_LOAD16_U,
64
  WASM_INS_I64_LOAD32_S,
65
  WASM_INS_I64_LOAD32_U,
66
  WASM_INS_I32_STORE,
67
  WASM_INS_I64_STORE,
68
  WASM_INS_F32_STORE,
69
  WASM_INS_F64_STORE,
70
  WASM_INS_I32_STORE8,
71
  WASM_INS_I32_STORE16,
72
  WASM_INS_I64_STORE8,
73
  WASM_INS_I64_STORE16,
74
  WASM_INS_I64_STORE32,
75
  WASM_INS_CURRENT_MEMORY,
76
  WASM_INS_GROW_MEMORY,
77
  WASM_INS_I32_CONST,
78
  WASM_INS_I64_CONST,
79
  WASM_INS_F32_CONST,
80
  WASM_INS_F64_CONST,
81
  WASM_INS_I32_EQZ,
82
  WASM_INS_I32_EQ,
83
  WASM_INS_I32_NE,
84
  WASM_INS_I32_LT_S,
85
  WASM_INS_I32_LT_U,
86
  WASM_INS_I32_GT_S,
87
  WASM_INS_I32_GT_U,
88
  WASM_INS_I32_LE_S,
89
  WASM_INS_I32_LE_U,
90
  WASM_INS_I32_GE_S,
91
  WASM_INS_I32_GE_U,
92
  WASM_INS_I64_EQZ,
93
  WASM_INS_I64_EQ,
94
  WASM_INS_I64_NE,
95
  WASM_INS_I64_LT_S,
96
  WASM_INS_I64_LT_U,
97
  WASN_INS_I64_GT_S,
98
  WASM_INS_I64_GT_U,
99
  WASM_INS_I64_LE_S,
100
  WASM_INS_I64_LE_U,
101
  WASM_INS_I64_GE_S,
102
  WASM_INS_I64_GE_U,
103
  WASM_INS_F32_EQ,
104
  WASM_INS_F32_NE,
105
  WASM_INS_F32_LT,
106
  WASM_INS_F32_GT,
107
  WASM_INS_F32_LE,
108
  WASM_INS_F32_GE,
109
  WASM_INS_F64_EQ,
110
  WASM_INS_F64_NE,
111
  WASM_INS_F64_LT,
112
  WASM_INS_F64_GT,
113
  WASM_INS_F64_LE,
114
  WASM_INS_F64_GE,
115
  WASM_INS_I32_CLZ,
116
  WASM_INS_I32_CTZ,
117
  WASM_INS_I32_POPCNT,
118
  WASM_INS_I32_ADD,
119
  WASM_INS_I32_SUB,
120
  WASM_INS_I32_MUL,
121
  WASM_INS_I32_DIV_S,
122
  WASM_INS_I32_DIV_U,
123
  WASM_INS_I32_REM_S,
124
  WASM_INS_I32_REM_U,
125
  WASM_INS_I32_AND,
126
  WASM_INS_I32_OR,
127
  WASM_INS_I32_XOR,
128
  WASM_INS_I32_SHL,
129
  WASM_INS_I32_SHR_S,
130
  WASM_INS_I32_SHR_U,
131
  WASM_INS_I32_ROTL,
132
  WASM_INS_I32_ROTR,
133
  WASM_INS_I64_CLZ,
134
  WASM_INS_I64_CTZ,
135
  WASM_INS_I64_POPCNT,
136
  WASM_INS_I64_ADD,
137
  WASM_INS_I64_SUB,
138
  WASM_INS_I64_MUL,
139
  WASM_INS_I64_DIV_S,
140
  WASM_INS_I64_DIV_U,
141
  WASM_INS_I64_REM_S,
142
  WASM_INS_I64_REM_U,
143
  WASM_INS_I64_AND,
144
  WASM_INS_I64_OR,
145
  WASM_INS_I64_XOR,
146
  WASM_INS_I64_SHL,
147
  WASM_INS_I64_SHR_S,
148
  WASM_INS_I64_SHR_U,
149
  WASM_INS_I64_ROTL,
150
  WASM_INS_I64_ROTR,
151
  WASM_INS_F32_ABS,
152
  WASM_INS_F32_NEG,
153
  WASM_INS_F32_CEIL,
154
  WASM_INS_F32_FLOOR,
155
  WASM_INS_F32_TRUNC,
156
  WASM_INS_F32_NEAREST,
157
  WASM_INS_F32_SQRT,
158
  WASM_INS_F32_ADD,
159
  WASM_INS_F32_SUB,
160
  WASM_INS_F32_MUL,
161
  WASM_INS_F32_DIV,
162
  WASM_INS_F32_MIN,
163
  WASM_INS_F32_MAX,
164
  WASM_INS_F32_COPYSIGN,
165
  WASM_INS_F64_ABS,
166
  WASM_INS_F64_NEG,
167
  WASM_INS_F64_CEIL,
168
  WASM_INS_F64_FLOOR,
169
  WASM_INS_F64_TRUNC,
170
  WASM_INS_F64_NEAREST,
171
  WASM_INS_F64_SQRT,
172
  WASM_INS_F64_ADD,
173
  WASM_INS_F64_SUB,
174
  WASM_INS_F64_MUL,
175
  WASM_INS_F64_DIV,
176
  WASM_INS_F64_MIN,
177
  WASM_INS_F64_MAX,
178
  WASM_INS_F64_COPYSIGN,
179
  WASM_INS_I32_WARP_I64,
180
  WASP_INS_I32_TRUNC_S_F32,
181
  WASM_INS_I32_TRUNC_U_F32,
182
  WASM_INS_I32_TRUNC_S_F64,
183
  WASM_INS_I32_TRUNC_U_F64,
184
  WASM_INS_I64_EXTEND_S_I32,
185
  WASM_INS_I64_EXTEND_U_I32,
186
  WASM_INS_I64_TRUNC_S_F32,
187
  WASM_INS_I64_TRUNC_U_F32,
188
  WASM_INS_I64_TRUNC_S_F64,
189
  WASM_INS_I64_TRUNC_U_F64,
190
  WASM_INS_F32_CONVERT_S_I32,
191
  WASM_INS_F32_CONVERT_U_I32,
192
  WASM_INS_F32_CONVERT_S_I64,
193
  WASM_INS_F32_CONVERT_U_I64,
194
  WASM_INS_F32_DEMOTE_F64,
195
  WASM_INS_F64_CONVERT_S_I32,
196
  WASM_INS_F64_CONVERT_U_I32,
197
  WASM_INS_F64_CONVERT_S_I64,
198
  WASM_INS_F64_CONVERT_U_I64,
199
  WASM_INS_F64_PROMOTE_F32,
200
  WASM_INS_I32_REINTERPRET_F32,
201
  WASM_INS_I64_REINTERPRET_F64,
202
  WASM_INS_F32_REINTERPRET_I32,
203
  WASM_INS_F64_REINTERPRET_I64,
204
  -1,
205
  -1,
206
  -1,
207
  -1,
208
  -1,
209
  -1,
210
  -1,
211
  -1,
212
  -1,
213
  -1,
214
  -1,
215
  -1,
216
  -1,
217
  -1,
218
  -1,
219
  -1,
220
  -1,
221
  -1,
222
  -1,
223
  -1,
224
  -1,
225
  -1,
226
  -1,
227
  -1,
228
  -1,
229
  -1,
230
  -1,
231
  -1,
232
  -1,
233
  -1,
234
  -1,
235
  -1,
236
  -1,
237
  -1,
238
  -1,
239
  -1,
240
  -1,
241
  -1,
242
  -1,
243
  -1,
244
  -1,
245
  -1,
246
  -1,
247
  -1,
248
  -1,
249
  -1,
250
  -1,
251
  -1,
252
  -1,
253
  -1,
254
  -1,
255
  -1,
256
  -1,
257
  -1,
258
  -1,
259
  -1,
260
  -1,
261
  -1,
262
  -1,
263
  -1,
264
  -1,
265
  -1,
266
  -1,
267
  -1,
268
};
269
270
// input  | code: code pointer start from varuint32
271
//        | code_len: real code len count from varint
272
//        | leng: return value, means length of varint. -1 means error
273
// return | varint
274
static uint32_t get_varuint32(const uint8_t *code, size_t code_len,
275
            size_t *leng)
276
623
{
277
623
  uint32_t data = 0;
278
623
  int i;
279
280
939
  for (i = 0;; i++) {
281
939
    if (code_len < i + 1) {
282
29
      *leng = -1;
283
29
      return 0;
284
29
    }
285
286
910
    if (i > 4 || (i == 4 && (code[i] & 0x7f) > 0x0f)) {
287
0
      *leng = -1;
288
0
      return 0;
289
0
    }
290
291
910
    data = data + (((uint32_t)code[i] & 0x7f) << (i * 7));
292
910
    if (code[i] >> 7 == 0) {
293
594
      break;
294
594
    }
295
910
  }
296
297
594
  *leng = i + 1;
298
299
594
  return data;
300
623
}
301
302
// input  | code : code pointer start from varuint64
303
//        | code_len : real code len count from varint
304
//        | leng: return value, means length of varint. -1 means error
305
// return   | varint
306
static uint64_t get_varuint64(const uint8_t *code, size_t code_len,
307
            size_t *leng)
308
82
{
309
82
  uint64_t data;
310
82
  int i;
311
312
82
  data = 0;
313
140
  for (i = 0;; i++) {
314
140
    if (code_len < i + 1) {
315
2
      *leng = -1;
316
2
      return 0;
317
2
    }
318
319
138
    if (i > 9 || (i == 9 && (code[i] & 0x7f) > 0x01)) {
320
0
      *leng = -1;
321
0
      return 0;
322
0
    }
323
324
138
    data = data + (((uint64_t)code[i] & 0x7f) << (i * 7));
325
138
    if (code[i] >> 7 == 0) {
326
80
      break;
327
80
    }
328
138
  }
329
330
80
  *leng = i + 1;
331
332
80
  return data;
333
82
}
334
335
// input  | code : code pointer start from uint32
336
//      | dest : the pointer where we store the uint32
337
// return | None
338
static void get_uint32(const uint8_t *code, uint32_t *dest)
339
258
{
340
258
  memcpy(dest, code, 4);
341
258
}
342
343
// input  | code : code pointer start from uint32
344
//      | dest : the pointer where we store the uint64
345
// return   | None
346
static void get_uint64(const uint8_t *code, uint64_t *dest)
347
24
{
348
24
  memcpy(dest, code, 8);
349
24
}
350
351
// input  | code : code pointer start from varint7
352
//      | code_len : start from the code pointer to the end, how long is it
353
//      | leng : length of the param , -1 means error
354
// return   | data of varint7
355
static int8_t get_varint7(const uint8_t *code, size_t code_len, size_t *leng)
356
33
{
357
33
  int8_t data;
358
359
33
  if (code_len < 1) {
360
0
    *leng = -1;
361
0
    return -1;
362
0
  }
363
364
33
  *leng = 1;
365
366
33
  if (code[0] == 0x40) {
367
5
    return -1;
368
5
  }
369
370
28
  data = code[0] & 0x7f;
371
372
28
  return data;
373
33
}
374
375
// input  | code : code pointer start from varuint32
376
//      | code_len : start from the code pointer to the end, how long is it
377
//      | param_size : pointer of the param size
378
//      | MI : Mcinst handler in this round of disasm
379
// return   | true/false if the function successfully finished
380
static bool read_varuint32(const uint8_t *code, size_t code_len,
381
         uint16_t *param_size, MCInst *MI)
382
26
{
383
26
  size_t len = 0;
384
26
  uint32_t data;
385
386
26
  data = get_varuint32(code, code_len, &len);
387
26
  if (len == -1) {
388
4
    return false;
389
4
  }
390
391
22
  if (MI->flat_insn->detail) {
392
22
    MI->flat_insn->detail->wasm.op_count = 1;
393
22
    MI->flat_insn->detail->wasm.operands[0].type =
394
22
      WASM_OP_VARUINT32;
395
22
    MI->flat_insn->detail->wasm.operands[0].size = len;
396
22
    MI->flat_insn->detail->wasm.operands[0].varuint32 = data;
397
22
  }
398
399
22
  MI->wasm_data.size = len;
400
22
  MI->wasm_data.type = WASM_OP_VARUINT32;
401
22
  MI->wasm_data.uint32 = data;
402
22
  *param_size = len;
403
404
22
  return true;
405
26
}
406
407
// input  | code : code pointer start from varuint64
408
//      | code_len : start from the code pointer to the end, how long is it
409
//      | param_size : pointer of the param size
410
//      | MI : Mcinst handler in this round of disasm
411
// return   | true/false if the function successfully finished
412
static bool read_varuint64(const uint8_t *code, size_t code_len,
413
         uint16_t *param_size, MCInst *MI)
414
82
{
415
82
  size_t len = 0;
416
82
  uint64_t data;
417
418
82
  data = get_varuint64(code, code_len, &len);
419
82
  if (len == -1) {
420
2
    return false;
421
2
  }
422
423
80
  if (MI->flat_insn->detail) {
424
80
    MI->flat_insn->detail->wasm.op_count = 1;
425
80
    MI->flat_insn->detail->wasm.operands[0].type =
426
80
      WASM_OP_VARUINT64;
427
80
    MI->flat_insn->detail->wasm.operands[0].size = len;
428
80
    MI->flat_insn->detail->wasm.operands[0].varuint64 = data;
429
80
  }
430
431
80
  MI->wasm_data.size = len;
432
80
  MI->wasm_data.type = WASM_OP_VARUINT64;
433
80
  MI->wasm_data.uint64 = data;
434
80
  *param_size = len;
435
436
80
  return true;
437
82
}
438
439
// input  | code : code pointer start from memoryimmediate
440
//      | code_len : start from the code pointer to the end, how long is it
441
//      | param_size : pointer of the param size (sum of two params)
442
//      | MI : Mcinst handler in this round of disasm
443
// return   | true/false if the function successfully finished
444
static bool read_memoryimmediate(const uint8_t *code, size_t code_len,
445
         uint16_t *param_size, MCInst *MI)
446
210
{
447
210
  size_t tmp, len = 0;
448
210
  uint32_t data[2];
449
450
210
  if (MI->flat_insn->detail) {
451
210
    MI->flat_insn->detail->wasm.op_count = 2;
452
210
  }
453
454
210
  data[0] = get_varuint32(code, code_len, &tmp);
455
210
  if (tmp == -1) {
456
2
    return false;
457
2
  }
458
459
208
  if (MI->flat_insn->detail) {
460
208
    MI->flat_insn->detail->wasm.operands[0].type =
461
208
      WASM_OP_VARUINT32;
462
208
    MI->flat_insn->detail->wasm.operands[0].size = tmp;
463
208
    MI->flat_insn->detail->wasm.operands[0].varuint32 = data[0];
464
208
  }
465
466
208
  len = tmp;
467
208
  data[1] = get_varuint32(&code[len], code_len - len, &tmp);
468
469
208
  if (MI->flat_insn->detail) {
470
208
    MI->flat_insn->detail->wasm.operands[1].type =
471
208
      WASM_OP_VARUINT32;
472
208
    MI->flat_insn->detail->wasm.operands[1].size = tmp;
473
208
    MI->flat_insn->detail->wasm.operands[1].varuint32 = data[1];
474
208
  }
475
476
208
  len += tmp;
477
208
  MI->wasm_data.size = len;
478
208
  MI->wasm_data.type = WASM_OP_IMM;
479
208
  MI->wasm_data.immediate[0] = data[0];
480
208
  MI->wasm_data.immediate[1] = data[1];
481
208
  *param_size = len;
482
483
208
  return true;
484
210
}
485
486
// input  | code : code pointer start from uint32
487
//      | code_len : start from the code pointer to the end, how long is it
488
//      | param_size : pointer of the param size
489
//      | MI : Mcinst handler in this round of disasm
490
// return   | true/false if the function successfully finished
491
static bool read_uint32(const uint8_t *code, size_t code_len,
492
      uint16_t *param_size, MCInst *MI)
493
130
{
494
130
  if (code_len < 4) {
495
1
    return false;
496
1
  }
497
498
129
  get_uint32(code, &(MI->wasm_data.uint32));
499
500
129
  if (MI->flat_insn->detail) {
501
129
    MI->flat_insn->detail->wasm.op_count = 1;
502
129
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_UINT32;
503
129
    MI->flat_insn->detail->wasm.operands[0].size = 4;
504
129
    get_uint32(code,
505
129
         &(MI->flat_insn->detail->wasm.operands[0].uint32));
506
129
  }
507
508
129
  MI->wasm_data.size = 4;
509
129
  MI->wasm_data.type = WASM_OP_UINT32;
510
129
  *param_size = 4;
511
512
129
  return true;
513
130
}
514
515
// input  | code : code pointer start from uint64
516
//      | code_len : start from the code pointer to the end, how long is it
517
//      | param_size : pointer of the param size
518
//      | MI : Mcinst handler in this round of disasm
519
// return   | true/false if the function successfully finished
520
static bool read_uint64(const uint8_t *code, size_t code_len,
521
      uint16_t *param_size, MCInst *MI)
522
12
{
523
12
  if (code_len < 8) {
524
0
    return false;
525
0
  }
526
527
12
  get_uint64(code, &(MI->wasm_data.uint64));
528
529
12
  if (MI->flat_insn->detail) {
530
12
    MI->flat_insn->detail->wasm.op_count = 1;
531
12
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_UINT64;
532
12
    MI->flat_insn->detail->wasm.operands[0].size = 8;
533
12
    get_uint64(code,
534
12
         &(MI->flat_insn->detail->wasm.operands[0].uint64));
535
12
  }
536
537
12
  MI->wasm_data.size = 8;
538
12
  MI->wasm_data.type = WASM_OP_UINT64;
539
12
  *param_size = 8;
540
541
12
  return true;
542
12
}
543
544
// input  | code : code pointer start from brtable
545
//      | code_len : start from the code pointer to the end, how long is it
546
//      | param_size : pointer of the param size (sum of all param)
547
//      | MI : Mcinst handler in this round of disasm
548
// return   | true/false if the function successfully finished
549
static bool read_brtable(const uint8_t *code, size_t code_len,
550
       uint16_t *param_size, MCInst *MI)
551
86
{
552
86
  uint32_t length, default_target;
553
86
  int tmp_len = 0, i;
554
86
  size_t var_len;
555
556
  // read length
557
86
  length = get_varuint32(code, code_len, &var_len);
558
86
  if (var_len == -1) {
559
0
    return false;
560
0
  }
561
562
86
  tmp_len += var_len;
563
86
  MI->wasm_data.brtable.length = length;
564
86
  if (length >= UINT32_MAX - tmp_len) {
565
    // integer overflow check
566
0
    return false;
567
0
  }
568
86
  if (code_len < tmp_len + length) {
569
    // safety check that we have minimum enough data to read
570
2
    return false;
571
2
  }
572
  // base address + 1 byte opcode + tmp_len for number of cases = start of targets
573
84
  MI->wasm_data.brtable.address = MI->address + 1 + tmp_len;
574
575
84
  if (MI->flat_insn->detail) {
576
84
    MI->flat_insn->detail->wasm.op_count = 1;
577
84
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_BRTABLE;
578
84
    MI->flat_insn->detail->wasm.operands[0].brtable.length =
579
84
      MI->wasm_data.brtable.length;
580
84
    MI->flat_insn->detail->wasm.operands[0].brtable.address =
581
84
      MI->wasm_data.brtable.address;
582
84
  }
583
584
  // read data
585
93
  for (i = 0; i < length; i++) {
586
10
    if (code_len < tmp_len) {
587
0
      return false;
588
0
    }
589
590
10
    get_varuint32(code + tmp_len, code_len - tmp_len, &var_len);
591
10
    if (var_len == -1) {
592
1
      return false;
593
1
    }
594
595
9
    tmp_len += var_len;
596
9
  }
597
598
  // read default target
599
83
  default_target =
600
83
    get_varuint32(code + tmp_len, code_len - tmp_len, &var_len);
601
83
  if (var_len == -1) {
602
4
    return false;
603
4
  }
604
605
79
  MI->wasm_data.brtable.default_target = default_target;
606
79
  MI->wasm_data.type = WASM_OP_BRTABLE;
607
79
  *param_size = tmp_len + var_len;
608
609
79
  if (MI->flat_insn->detail) {
610
79
    MI->flat_insn->detail->wasm.operands[0].size = *param_size;
611
79
    MI->flat_insn->detail->wasm.operands[0].brtable.default_target =
612
79
      MI->wasm_data.brtable.default_target;
613
79
  }
614
615
79
  return true;
616
83
}
617
618
// input  | code : code pointer start from varint7
619
//      | code_len : start from the code pointer to the end, how long is it
620
//      | param_size : pointer of the param size
621
//      | MI : Mcinst handler in this round of disasm
622
// return   | true/false if the function successfully finished
623
static bool read_varint7(const uint8_t *code, size_t code_len,
624
       uint16_t *param_size, MCInst *MI)
625
33
{
626
33
  size_t len = 0;
627
628
33
  MI->wasm_data.type = WASM_OP_INT7;
629
33
  MI->wasm_data.int7 = get_varint7(code, code_len, &len);
630
33
  if (len == -1) {
631
0
    return false;
632
0
  }
633
634
33
  if (MI->flat_insn->detail) {
635
33
    MI->flat_insn->detail->wasm.op_count = 1;
636
33
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_INT7;
637
33
    MI->flat_insn->detail->wasm.operands[0].size = 1;
638
33
    MI->flat_insn->detail->wasm.operands[0].int7 =
639
33
      MI->wasm_data.int7;
640
33
  }
641
642
33
  *param_size = len;
643
644
33
  return true;
645
33
}
646
647
bool WASM_getInstruction(csh ud, const uint8_t *code, size_t code_len,
648
       MCInst *MI, uint16_t *size, uint64_t address,
649
       void *inst_info)
650
9.82k
{
651
9.82k
  unsigned char opcode;
652
9.82k
  uint16_t param_size;
653
654
9.82k
  if (code_len == 0)
655
0
    return false;
656
657
9.82k
  opcode = code[0];
658
9.82k
  if (opcodes[opcode] == -1) {
659
    // invalid opcode
660
8
    return false;
661
8
  }
662
663
  // valid opcode
664
9.82k
  MI->address = address;
665
9.82k
  MI->OpcodePub = MI->Opcode = opcode;
666
667
9.82k
  if (MI->flat_insn->detail) {
668
9.82k
    memset(MI->flat_insn->detail, 0,
669
9.82k
           offsetof(cs_detail, wasm) + sizeof(cs_wasm));
670
9.82k
    WASM_get_insn_id((cs_struct *)ud, MI->flat_insn, opcode);
671
9.82k
  }
672
673
  // setup groups
674
9.82k
  switch (opcode) {
675
0
  default:
676
0
    return false;
677
678
5
  case WASM_INS_I32_CONST:
679
5
    if (code_len == 1 ||
680
4
        !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
681
2
      return false;
682
2
    }
683
684
3
    if (MI->flat_insn->detail) {
685
3
      MI->flat_insn->detail->wasm.op_count = 1;
686
3
      MI->flat_insn->detail
687
3
        ->groups[MI->flat_insn->detail->groups_count] =
688
3
        WASM_GRP_NUMBERIC;
689
3
      MI->flat_insn->detail->groups_count++;
690
3
    }
691
692
3
    *size = param_size + 1;
693
694
3
    break;
695
696
82
  case WASM_INS_I64_CONST:
697
82
    if (code_len == 1 ||
698
82
        !read_varuint64(&code[1], code_len - 1, &param_size, MI)) {
699
2
      return false;
700
2
    }
701
702
80
    if (MI->flat_insn->detail) {
703
80
      MI->flat_insn->detail->wasm.op_count = 1;
704
80
      MI->flat_insn->detail
705
80
        ->groups[MI->flat_insn->detail->groups_count] =
706
80
        WASM_GRP_NUMBERIC;
707
80
      MI->flat_insn->detail->groups_count++;
708
80
    }
709
710
80
    *size = param_size + 1;
711
712
80
    break;
713
714
130
  case WASM_INS_F32_CONST:
715
130
    if (code_len == 1 ||
716
130
        !read_uint32(&code[1], code_len - 1, &param_size, MI)) {
717
1
      return false;
718
1
    }
719
720
129
    if (MI->flat_insn->detail) {
721
129
      MI->flat_insn->detail->wasm.op_count = 1;
722
129
      MI->flat_insn->detail
723
129
        ->groups[MI->flat_insn->detail->groups_count] =
724
129
        WASM_GRP_NUMBERIC;
725
129
      MI->flat_insn->detail->groups_count++;
726
129
    }
727
728
129
    *size = param_size + 1;
729
730
129
    break;
731
732
13
  case WASM_INS_F64_CONST:
733
13
    if (code_len == 1 ||
734
12
        !read_uint64(&code[1], code_len - 1, &param_size, MI)) {
735
1
      return false;
736
1
    }
737
738
12
    if (MI->flat_insn->detail) {
739
12
      MI->flat_insn->detail->wasm.op_count = 1;
740
12
      MI->flat_insn->detail
741
12
        ->groups[MI->flat_insn->detail->groups_count] =
742
12
        WASM_GRP_NUMBERIC;
743
12
      MI->flat_insn->detail->groups_count++;
744
12
    }
745
746
12
    *size = param_size + 1;
747
748
12
    break;
749
750
37
  case WASM_INS_I32_EQZ:
751
351
  case WASM_INS_I32_EQ:
752
4.03k
  case WASM_INS_I32_NE:
753
4.03k
  case WASM_INS_I32_LT_S:
754
4.04k
  case WASM_INS_I32_LT_U:
755
4.05k
  case WASM_INS_I32_GT_S:
756
4.18k
  case WASM_INS_I32_GT_U:
757
4.24k
  case WASM_INS_I32_LE_S:
758
4.24k
  case WASM_INS_I32_LE_U:
759
4.24k
  case WASM_INS_I32_GE_S:
760
4.24k
  case WASM_INS_I32_GE_U:
761
4.25k
  case WASM_INS_I64_EQZ:
762
4.25k
  case WASM_INS_I64_EQ:
763
4.25k
  case WASM_INS_I64_NE:
764
4.26k
  case WASM_INS_I64_LT_S:
765
4.26k
  case WASM_INS_I64_LT_U:
766
4.26k
  case WASN_INS_I64_GT_S:
767
4.26k
  case WASM_INS_I64_GT_U:
768
4.98k
  case WASM_INS_I64_LE_S:
769
5.04k
  case WASM_INS_I64_LE_U:
770
5.04k
  case WASM_INS_I64_GE_S:
771
5.06k
  case WASM_INS_I64_GE_U:
772
5.09k
  case WASM_INS_F32_EQ:
773
5.09k
  case WASM_INS_F32_NE:
774
5.14k
  case WASM_INS_F32_LT:
775
5.14k
  case WASM_INS_F32_GT:
776
5.17k
  case WASM_INS_F32_LE:
777
5.18k
  case WASM_INS_F32_GE:
778
5.19k
  case WASM_INS_F64_EQ:
779
5.32k
  case WASM_INS_F64_NE:
780
6.15k
  case WASM_INS_F64_LT:
781
6.17k
  case WASM_INS_F64_GT:
782
6.18k
  case WASM_INS_F64_LE:
783
6.23k
  case WASM_INS_F64_GE:
784
6.23k
  case WASM_INS_I32_CLZ:
785
6.23k
  case WASM_INS_I32_CTZ:
786
6.25k
  case WASM_INS_I32_POPCNT:
787
6.26k
  case WASM_INS_I32_ADD:
788
6.27k
  case WASM_INS_I32_SUB:
789
6.28k
  case WASM_INS_I32_MUL:
790
6.28k
  case WASM_INS_I32_DIV_S:
791
6.31k
  case WASM_INS_I32_DIV_U:
792
6.32k
  case WASM_INS_I32_REM_S:
793
6.33k
  case WASM_INS_I32_REM_U:
794
6.35k
  case WASM_INS_I32_AND:
795
6.37k
  case WASM_INS_I32_OR:
796
6.37k
  case WASM_INS_I32_XOR:
797
6.38k
  case WASM_INS_I32_SHL:
798
6.38k
  case WASM_INS_I32_SHR_S:
799
6.38k
  case WASM_INS_I32_SHR_U:
800
6.39k
  case WASM_INS_I32_ROTL:
801
6.39k
  case WASM_INS_I32_ROTR:
802
6.39k
  case WASM_INS_I64_CLZ:
803
6.43k
  case WASM_INS_I64_CTZ:
804
6.43k
  case WASM_INS_I64_POPCNT:
805
6.47k
  case WASM_INS_I64_ADD:
806
6.47k
  case WASM_INS_I64_SUB:
807
6.49k
  case WASM_INS_I64_MUL:
808
6.51k
  case WASM_INS_I64_DIV_S:
809
6.59k
  case WASM_INS_I64_DIV_U:
810
6.73k
  case WASM_INS_I64_REM_S:
811
6.74k
  case WASM_INS_I64_REM_U:
812
6.74k
  case WASM_INS_I64_AND:
813
6.74k
  case WASM_INS_I64_OR:
814
6.78k
  case WASM_INS_I64_XOR:
815
6.78k
  case WASM_INS_I64_SHL:
816
6.80k
  case WASM_INS_I64_SHR_S:
817
6.81k
  case WASM_INS_I64_SHR_U:
818
6.85k
  case WASM_INS_I64_ROTL:
819
7.01k
  case WASM_INS_I64_ROTR:
820
7.01k
  case WASM_INS_F32_ABS:
821
7.06k
  case WASM_INS_F32_NEG:
822
7.22k
  case WASM_INS_F32_CEIL:
823
7.23k
  case WASM_INS_F32_FLOOR:
824
7.23k
  case WASM_INS_F32_TRUNC:
825
7.25k
  case WASM_INS_F32_NEAREST:
826
7.30k
  case WASM_INS_F32_SQRT:
827
7.35k
  case WASM_INS_F32_ADD:
828
7.35k
  case WASM_INS_F32_SUB:
829
7.35k
  case WASM_INS_F32_MUL:
830
7.36k
  case WASM_INS_F32_DIV:
831
7.36k
  case WASM_INS_F32_MIN:
832
7.36k
  case WASM_INS_F32_MAX:
833
7.37k
  case WASM_INS_F32_COPYSIGN:
834
7.40k
  case WASM_INS_F64_ABS:
835
7.40k
  case WASM_INS_F64_NEG:
836
7.45k
  case WASM_INS_F64_CEIL:
837
7.53k
  case WASM_INS_F64_FLOOR:
838
7.54k
  case WASM_INS_F64_TRUNC:
839
7.54k
  case WASM_INS_F64_NEAREST:
840
7.57k
  case WASM_INS_F64_SQRT:
841
7.61k
  case WASM_INS_F64_ADD:
842
7.62k
  case WASM_INS_F64_SUB:
843
7.63k
  case WASM_INS_F64_MUL:
844
7.63k
  case WASM_INS_F64_DIV:
845
7.64k
  case WASM_INS_F64_MIN:
846
7.68k
  case WASM_INS_F64_MAX:
847
7.68k
  case WASM_INS_F64_COPYSIGN:
848
7.68k
  case WASM_INS_I32_WARP_I64:
849
7.72k
  case WASP_INS_I32_TRUNC_S_F32:
850
7.73k
  case WASM_INS_I32_TRUNC_U_F32:
851
7.73k
  case WASM_INS_I32_TRUNC_S_F64:
852
7.73k
  case WASM_INS_I32_TRUNC_U_F64:
853
7.75k
  case WASM_INS_I64_EXTEND_S_I32:
854
7.89k
  case WASM_INS_I64_EXTEND_U_I32:
855
7.89k
  case WASM_INS_I64_TRUNC_S_F32:
856
7.89k
  case WASM_INS_I64_TRUNC_U_F32:
857
7.90k
  case WASM_INS_I64_TRUNC_S_F64:
858
7.91k
  case WASM_INS_I64_TRUNC_U_F64:
859
7.96k
  case WASM_INS_F32_CONVERT_S_I32:
860
7.99k
  case WASM_INS_F32_CONVERT_U_I32:
861
8.05k
  case WASM_INS_F32_CONVERT_S_I64:
862
8.18k
  case WASM_INS_F32_CONVERT_U_I64:
863
8.18k
  case WASM_INS_F32_DEMOTE_F64:
864
8.19k
  case WASM_INS_F64_CONVERT_S_I32:
865
8.19k
  case WASM_INS_F64_CONVERT_U_I32:
866
8.33k
  case WASM_INS_F64_CONVERT_S_I64:
867
8.34k
  case WASM_INS_F64_CONVERT_U_I64:
868
8.40k
  case WASM_INS_F64_PROMOTE_F32:
869
8.57k
  case WASM_INS_I32_REINTERPRET_F32:
870
8.57k
  case WASM_INS_I64_REINTERPRET_F64:
871
8.59k
  case WASM_INS_F32_REINTERPRET_I32:
872
8.59k
  case WASM_INS_F64_REINTERPRET_I64:
873
8.59k
    MI->wasm_data.type = WASM_OP_NONE;
874
875
8.59k
    if (MI->flat_insn->detail) {
876
8.59k
      MI->flat_insn->detail->wasm.op_count = 0;
877
8.59k
      MI->flat_insn->detail
878
8.59k
        ->groups[MI->flat_insn->detail->groups_count] =
879
8.59k
        WASM_GRP_NUMBERIC;
880
8.59k
      MI->flat_insn->detail->groups_count++;
881
8.59k
    }
882
883
8.59k
    *size = 1;
884
885
8.59k
    break;
886
887
33
  case WASM_INS_DROP:
888
37
  case WASM_INS_SELECT:
889
37
    MI->wasm_data.type = WASM_OP_NONE;
890
891
37
    if (MI->flat_insn->detail) {
892
37
      MI->flat_insn->detail->wasm.op_count = 0;
893
37
      MI->flat_insn->detail
894
37
        ->groups[MI->flat_insn->detail->groups_count] =
895
37
        WASM_GRP_PARAMETRIC;
896
37
      MI->flat_insn->detail->groups_count++;
897
37
    }
898
899
37
    *size = 1;
900
901
37
    break;
902
903
3
  case WASM_INS_GET_LOCAL:
904
5
  case WASM_INS_SET_LOCAL:
905
7
  case WASM_INS_TEE_LOCAL:
906
8
  case WASM_INS_GET_GLOBAL:
907
11
  case WASM_INS_SET_GLOBAL:
908
11
    if (code_len == 1 ||
909
9
        !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
910
5
      return false;
911
5
    }
912
913
6
    if (MI->flat_insn->detail) {
914
6
      MI->flat_insn->detail->wasm.op_count = 1;
915
6
      MI->flat_insn->detail
916
6
        ->groups[MI->flat_insn->detail->groups_count] =
917
6
        WASM_GRP_VARIABLE;
918
6
      MI->flat_insn->detail->groups_count++;
919
6
    }
920
921
6
    *size = param_size + 1;
922
923
6
    break;
924
925
2
  case WASM_INS_I32_LOAD:
926
7
  case WASM_INS_I64_LOAD:
927
13
  case WASM_INS_F32_LOAD:
928
53
  case WASM_INS_F64_LOAD:
929
58
  case WASM_INS_I32_LOAD8_S:
930
62
  case WASM_INS_I32_LOAD8_U:
931
62
  case WASM_INS_I32_LOAD16_S:
932
70
  case WASM_INS_I32_LOAD16_U:
933
70
  case WASM_INS_I64_LOAD8_S:
934
71
  case WASM_INS_I64_LOAD8_U:
935
108
  case WASM_INS_I64_LOAD16_S:
936
145
  case WASM_INS_I64_LOAD16_U:
937
154
  case WASM_INS_I64_LOAD32_S:
938
158
  case WASM_INS_I64_LOAD32_U:
939
162
  case WASM_INS_I32_STORE:
940
168
  case WASM_INS_I64_STORE:
941
175
  case WASM_INS_F32_STORE:
942
175
  case WASM_INS_F64_STORE:
943
175
  case WASM_INS_I32_STORE8:
944
182
  case WASM_INS_I32_STORE16:
945
214
  case WASM_INS_I64_STORE8:
946
217
  case WASM_INS_I64_STORE16:
947
228
  case WASM_INS_I64_STORE32:
948
228
    if (code_len == 1 ||
949
210
        !read_memoryimmediate(&code[1], code_len - 1, &param_size,
950
210
            MI)) {
951
20
      return false;
952
20
    }
953
954
208
    if (MI->flat_insn->detail) {
955
208
      MI->flat_insn->detail->wasm.op_count = 2;
956
208
      MI->flat_insn->detail
957
208
        ->groups[MI->flat_insn->detail->groups_count] =
958
208
        WASM_GRP_MEMORY;
959
208
      MI->flat_insn->detail->groups_count++;
960
208
    }
961
962
208
    *size = param_size + 1;
963
964
208
    break;
965
966
41
  case WASM_INS_CURRENT_MEMORY:
967
79
  case WASM_INS_GROW_MEMORY:
968
79
    MI->wasm_data.type = WASM_OP_NONE;
969
970
79
    if (MI->flat_insn->detail) {
971
79
      MI->flat_insn->detail->wasm.op_count = 0;
972
79
      MI->flat_insn->detail
973
79
        ->groups[MI->flat_insn->detail->groups_count] =
974
79
        WASM_GRP_MEMORY;
975
79
      MI->flat_insn->detail->groups_count++;
976
79
    }
977
978
79
    *size = 1;
979
980
79
    break;
981
982
443
  case WASM_INS_UNREACHABLE:
983
477
  case WASM_INS_NOP:
984
480
  case WASM_INS_ELSE:
985
496
  case WASM_INS_END:
986
497
  case WASM_INS_RETURN:
987
497
    MI->wasm_data.type = WASM_OP_NONE;
988
989
497
    if (MI->flat_insn->detail) {
990
497
      MI->flat_insn->detail->wasm.op_count = 0;
991
497
      MI->flat_insn->detail
992
497
        ->groups[MI->flat_insn->detail->groups_count] =
993
497
        WASM_GRP_CONTROL;
994
497
      MI->flat_insn->detail->groups_count++;
995
497
    }
996
997
497
    *size = 1;
998
999
497
    break;
1000
1001
5
  case WASM_INS_BLOCK:
1002
16
  case WASM_INS_LOOP:
1003
38
  case WASM_INS_IF:
1004
38
    if (code_len == 1 ||
1005
33
        !read_varint7(&code[1], code_len - 1, &param_size, MI)) {
1006
5
      return false;
1007
5
    }
1008
1009
33
    if (MI->flat_insn->detail) {
1010
33
      MI->flat_insn->detail->wasm.op_count = 1;
1011
33
      MI->flat_insn->detail
1012
33
        ->groups[MI->flat_insn->detail->groups_count] =
1013
33
        WASM_GRP_CONTROL;
1014
33
      MI->flat_insn->detail->groups_count++;
1015
33
    }
1016
1017
33
    *size = param_size + 1;
1018
1019
33
    break;
1020
1021
0
  case WASM_INS_BR:
1022
1
  case WASM_INS_BR_IF:
1023
10
  case WASM_INS_CALL:
1024
16
  case WASM_INS_CALL_INDIRECT:
1025
16
    if (code_len == 1 ||
1026
13
        !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
1027
3
      return false;
1028
3
    }
1029
1030
13
    if (MI->flat_insn->detail) {
1031
13
      MI->flat_insn->detail->wasm.op_count = 1;
1032
13
      MI->flat_insn->detail
1033
13
        ->groups[MI->flat_insn->detail->groups_count] =
1034
13
        WASM_GRP_CONTROL;
1035
13
      MI->flat_insn->detail->groups_count++;
1036
13
    }
1037
1038
13
    *size = param_size + 1;
1039
1040
13
    break;
1041
1042
86
  case WASM_INS_BR_TABLE:
1043
86
    if (code_len == 1 ||
1044
86
        !read_brtable(&code[1], code_len - 1, &param_size, MI)) {
1045
7
      return false;
1046
7
    }
1047
1048
79
    if (MI->flat_insn->detail) {
1049
79
      MI->flat_insn->detail->wasm.op_count = 1;
1050
79
      MI->flat_insn->detail
1051
79
        ->groups[MI->flat_insn->detail->groups_count] =
1052
79
        WASM_GRP_CONTROL;
1053
79
      MI->flat_insn->detail->groups_count++;
1054
79
    }
1055
1056
79
    *size = param_size + 1;
1057
1058
79
    break;
1059
9.82k
  }
1060
1061
9.77k
  return true;
1062
9.82k
}