Coverage Report

Created: 2026-01-12 07:13

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
19.5k
{
277
19.5k
  uint32_t data = 0;
278
19.5k
  int i;
279
280
28.1k
  for (i = 0;; i++) {
281
28.1k
    if (code_len < i + 1) {
282
235
      *leng = -1;
283
235
      return 0;
284
235
    }
285
286
27.8k
    if (i > 4 || (i == 4 && (code[i] & 0x7f) > 0x0f)) {
287
529
      *leng = -1;
288
529
      return 0;
289
529
    }
290
291
27.3k
    data = data + (((uint32_t)code[i] & 0x7f) << (i * 7));
292
27.3k
    if (code[i] >> 7 == 0) {
293
18.7k
      break;
294
18.7k
    }
295
27.3k
  }
296
297
18.7k
  *leng = i + 1;
298
299
18.7k
  return data;
300
19.5k
}
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
371
{
309
371
  uint64_t data;
310
371
  int i;
311
312
371
  data = 0;
313
1.34k
  for (i = 0;; i++) {
314
1.34k
    if (code_len < i + 1) {
315
9
      *leng = -1;
316
9
      return 0;
317
9
    }
318
319
1.33k
    if (i > 9 || (i == 9 && (code[i] & 0x7f) > 0x01)) {
320
2
      *leng = -1;
321
2
      return 0;
322
2
    }
323
324
1.33k
    data = data + (((uint64_t)code[i] & 0x7f) << (i * 7));
325
1.33k
    if (code[i] >> 7 == 0) {
326
360
      break;
327
360
    }
328
1.33k
  }
329
330
360
  *leng = i + 1;
331
332
360
  return data;
333
371
}
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
550
{
340
550
  memcpy(dest, code, 4);
341
550
}
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
462
{
348
462
  memcpy(dest, code, 8);
349
462
}
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
794
{
357
794
  int8_t data;
358
359
794
  if (code_len < 1) {
360
0
    *leng = -1;
361
0
    return -1;
362
0
  }
363
364
794
  *leng = 1;
365
366
794
  if (code[0] == 0x40) {
367
69
    return -1;
368
69
  }
369
370
725
  data = code[0] & 0x7f;
371
372
725
  return data;
373
794
}
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
2.85k
{
383
2.85k
  size_t len = 0;
384
2.85k
  uint32_t data;
385
386
2.85k
  data = get_varuint32(code, code_len, &len);
387
2.85k
  if (len == -1) {
388
14
    return false;
389
14
  }
390
391
2.83k
  if (MI->flat_insn->detail) {
392
2.83k
    MI->flat_insn->detail->wasm.op_count = 1;
393
2.83k
    MI->flat_insn->detail->wasm.operands[0].type =
394
2.83k
      WASM_OP_VARUINT32;
395
2.83k
    MI->flat_insn->detail->wasm.operands[0].size = len;
396
2.83k
    MI->flat_insn->detail->wasm.operands[0].varuint32 = data;
397
2.83k
  }
398
399
2.83k
  MI->wasm_data.size = len;
400
2.83k
  MI->wasm_data.type = WASM_OP_VARUINT32;
401
2.83k
  MI->wasm_data.uint32 = data;
402
2.83k
  *param_size = len;
403
404
2.83k
  return true;
405
2.85k
}
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
371
{
415
371
  size_t len = 0;
416
371
  uint64_t data;
417
418
371
  data = get_varuint64(code, code_len, &len);
419
371
  if (len == -1) {
420
11
    return false;
421
11
  }
422
423
360
  if (MI->flat_insn->detail) {
424
360
    MI->flat_insn->detail->wasm.op_count = 1;
425
360
    MI->flat_insn->detail->wasm.operands[0].type =
426
360
      WASM_OP_VARUINT64;
427
360
    MI->flat_insn->detail->wasm.operands[0].size = len;
428
360
    MI->flat_insn->detail->wasm.operands[0].varuint64 = data;
429
360
  }
430
431
360
  MI->wasm_data.size = len;
432
360
  MI->wasm_data.type = WASM_OP_VARUINT64;
433
360
  MI->wasm_data.uint64 = data;
434
360
  *param_size = len;
435
436
360
  return true;
437
371
}
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
6.00k
{
447
6.00k
  size_t tmp, len = 0;
448
6.00k
  uint32_t data[2];
449
450
6.00k
  if (MI->flat_insn->detail) {
451
6.00k
    MI->flat_insn->detail->wasm.op_count = 2;
452
6.00k
  }
453
454
6.00k
  data[0] = get_varuint32(code, code_len, &tmp);
455
6.00k
  if (tmp == -1) {
456
37
    return false;
457
37
  }
458
459
5.96k
  if (MI->flat_insn->detail) {
460
5.96k
    MI->flat_insn->detail->wasm.operands[0].type =
461
5.96k
      WASM_OP_VARUINT32;
462
5.96k
    MI->flat_insn->detail->wasm.operands[0].size = tmp;
463
5.96k
    MI->flat_insn->detail->wasm.operands[0].varuint32 = data[0];
464
5.96k
  }
465
466
5.96k
  len = tmp;
467
5.96k
  data[1] = get_varuint32(&code[len], code_len - len, &tmp);
468
469
5.96k
  if (MI->flat_insn->detail) {
470
5.96k
    MI->flat_insn->detail->wasm.operands[1].type =
471
5.96k
      WASM_OP_VARUINT32;
472
5.96k
    MI->flat_insn->detail->wasm.operands[1].size = tmp;
473
5.96k
    MI->flat_insn->detail->wasm.operands[1].varuint32 = data[1];
474
5.96k
  }
475
476
5.96k
  len += tmp;
477
5.96k
  MI->wasm_data.size = len;
478
5.96k
  MI->wasm_data.type = WASM_OP_IMM;
479
5.96k
  MI->wasm_data.immediate[0] = data[0];
480
5.96k
  MI->wasm_data.immediate[1] = data[1];
481
5.96k
  *param_size = len;
482
483
5.96k
  return true;
484
6.00k
}
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
281
{
494
281
  if (code_len < 4) {
495
6
    return false;
496
6
  }
497
498
275
  get_uint32(code, &(MI->wasm_data.uint32));
499
500
275
  if (MI->flat_insn->detail) {
501
275
    MI->flat_insn->detail->wasm.op_count = 1;
502
275
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_UINT32;
503
275
    MI->flat_insn->detail->wasm.operands[0].size = 4;
504
275
    get_uint32(code,
505
275
         &(MI->flat_insn->detail->wasm.operands[0].uint32));
506
275
  }
507
508
275
  MI->wasm_data.size = 4;
509
275
  MI->wasm_data.type = WASM_OP_UINT32;
510
275
  *param_size = 4;
511
512
275
  return true;
513
281
}
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
237
{
523
237
  if (code_len < 8) {
524
6
    return false;
525
6
  }
526
527
231
  get_uint64(code, &(MI->wasm_data.uint64));
528
529
231
  if (MI->flat_insn->detail) {
530
231
    MI->flat_insn->detail->wasm.op_count = 1;
531
231
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_UINT64;
532
231
    MI->flat_insn->detail->wasm.operands[0].size = 8;
533
231
    get_uint64(code,
534
231
         &(MI->flat_insn->detail->wasm.operands[0].uint64));
535
231
  }
536
537
231
  MI->wasm_data.size = 8;
538
231
  MI->wasm_data.type = WASM_OP_UINT64;
539
231
  *param_size = 8;
540
541
231
  return true;
542
237
}
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
1.00k
{
552
1.00k
  uint32_t length, default_target;
553
1.00k
  int tmp_len = 0, i;
554
1.00k
  size_t var_len;
555
556
  // read length
557
1.00k
  length = get_varuint32(code, code_len, &var_len);
558
1.00k
  if (var_len == -1) {
559
7
    return false;
560
7
  }
561
562
999
  tmp_len += var_len;
563
999
  MI->wasm_data.brtable.length = length;
564
999
  if (length >= UINT32_MAX - tmp_len) {
565
    // integer overflow check
566
1
    return false;
567
1
  }
568
998
  if (code_len < tmp_len + length) {
569
    // safety check that we have minimum enough data to read
570
42
    return false;
571
42
  }
572
  // base address + 1 byte opcode + tmp_len for number of cases = start of targets
573
956
  MI->wasm_data.brtable.address = MI->address + 1 + tmp_len;
574
575
956
  if (MI->flat_insn->detail) {
576
956
    MI->flat_insn->detail->wasm.op_count = 1;
577
956
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_BRTABLE;
578
956
    MI->flat_insn->detail->wasm.operands[0].brtable.length =
579
956
      MI->wasm_data.brtable.length;
580
956
    MI->flat_insn->detail->wasm.operands[0].brtable.address =
581
956
      MI->wasm_data.brtable.address;
582
956
  }
583
584
  // read data
585
3.73k
  for (i = 0; i < length; i++) {
586
2.80k
    if (code_len < tmp_len) {
587
0
      return false;
588
0
    }
589
590
2.80k
    get_varuint32(code + tmp_len, code_len - tmp_len, &var_len);
591
2.80k
    if (var_len == -1) {
592
23
      return false;
593
23
    }
594
595
2.78k
    tmp_len += var_len;
596
2.78k
  }
597
598
  // read default target
599
933
  default_target =
600
933
    get_varuint32(code + tmp_len, code_len - tmp_len, &var_len);
601
933
  if (var_len == -1) {
602
28
    return false;
603
28
  }
604
605
905
  MI->wasm_data.brtable.default_target = default_target;
606
905
  MI->wasm_data.type = WASM_OP_BRTABLE;
607
905
  *param_size = tmp_len + var_len;
608
609
905
  if (MI->flat_insn->detail) {
610
905
    MI->flat_insn->detail->wasm.operands[0].size = *param_size;
611
905
    MI->flat_insn->detail->wasm.operands[0].brtable.default_target =
612
905
      MI->wasm_data.brtable.default_target;
613
905
  }
614
615
905
  return true;
616
933
}
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
794
{
626
794
  size_t len = 0;
627
628
794
  MI->wasm_data.type = WASM_OP_INT7;
629
794
  MI->wasm_data.int7 = get_varint7(code, code_len, &len);
630
794
  if (len == -1) {
631
0
    return false;
632
0
  }
633
634
794
  if (MI->flat_insn->detail) {
635
794
    MI->flat_insn->detail->wasm.op_count = 1;
636
794
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_INT7;
637
794
    MI->flat_insn->detail->wasm.operands[0].size = 1;
638
794
    MI->flat_insn->detail->wasm.operands[0].int7 =
639
794
      MI->wasm_data.int7;
640
794
  }
641
642
794
  *param_size = len;
643
644
794
  return true;
645
794
}
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
56.9k
{
651
56.9k
  unsigned char opcode;
652
56.9k
  uint16_t param_size;
653
654
56.9k
  if (code_len == 0)
655
0
    return false;
656
657
56.9k
  opcode = code[0];
658
56.9k
  if (opcodes[opcode] == -1) {
659
    // invalid opcode
660
149
    return false;
661
149
  }
662
663
  // valid opcode
664
56.8k
  MI->address = address;
665
56.8k
  MI->OpcodePub = MI->Opcode = opcode;
666
667
56.8k
  if (MI->flat_insn->detail) {
668
56.8k
    memset(MI->flat_insn->detail, 0,
669
56.8k
           offsetof(cs_detail, wasm) + sizeof(cs_wasm));
670
56.8k
    WASM_get_insn_id((cs_struct *)ud, MI->flat_insn, opcode);
671
56.8k
  }
672
673
  // setup groups
674
56.8k
  switch (opcode) {
675
0
  default:
676
0
    return false;
677
678
212
  case WASM_INS_I32_CONST:
679
212
    if (code_len == 1 ||
680
208
        !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
681
5
      return false;
682
5
    }
683
684
207
    if (MI->flat_insn->detail) {
685
207
      MI->flat_insn->detail->wasm.op_count = 1;
686
207
      MI->flat_insn->detail
687
207
        ->groups[MI->flat_insn->detail->groups_count] =
688
207
        WASM_GRP_NUMBERIC;
689
207
      MI->flat_insn->detail->groups_count++;
690
207
    }
691
692
207
    *size = param_size + 1;
693
694
207
    break;
695
696
376
  case WASM_INS_I64_CONST:
697
376
    if (code_len == 1 ||
698
371
        !read_varuint64(&code[1], code_len - 1, &param_size, MI)) {
699
16
      return false;
700
16
    }
701
702
360
    if (MI->flat_insn->detail) {
703
360
      MI->flat_insn->detail->wasm.op_count = 1;
704
360
      MI->flat_insn->detail
705
360
        ->groups[MI->flat_insn->detail->groups_count] =
706
360
        WASM_GRP_NUMBERIC;
707
360
      MI->flat_insn->detail->groups_count++;
708
360
    }
709
710
360
    *size = param_size + 1;
711
712
360
    break;
713
714
282
  case WASM_INS_F32_CONST:
715
282
    if (code_len == 1 ||
716
281
        !read_uint32(&code[1], code_len - 1, &param_size, MI)) {
717
7
      return false;
718
7
    }
719
720
275
    if (MI->flat_insn->detail) {
721
275
      MI->flat_insn->detail->wasm.op_count = 1;
722
275
      MI->flat_insn->detail
723
275
        ->groups[MI->flat_insn->detail->groups_count] =
724
275
        WASM_GRP_NUMBERIC;
725
275
      MI->flat_insn->detail->groups_count++;
726
275
    }
727
728
275
    *size = param_size + 1;
729
730
275
    break;
731
732
239
  case WASM_INS_F64_CONST:
733
239
    if (code_len == 1 ||
734
237
        !read_uint64(&code[1], code_len - 1, &param_size, MI)) {
735
8
      return false;
736
8
    }
737
738
231
    if (MI->flat_insn->detail) {
739
231
      MI->flat_insn->detail->wasm.op_count = 1;
740
231
      MI->flat_insn->detail
741
231
        ->groups[MI->flat_insn->detail->groups_count] =
742
231
        WASM_GRP_NUMBERIC;
743
231
      MI->flat_insn->detail->groups_count++;
744
231
    }
745
746
231
    *size = param_size + 1;
747
748
231
    break;
749
750
201
  case WASM_INS_I32_EQZ:
751
402
  case WASM_INS_I32_EQ:
752
842
  case WASM_INS_I32_NE:
753
1.04k
  case WASM_INS_I32_LT_S:
754
1.24k
  case WASM_INS_I32_LT_U:
755
1.47k
  case WASM_INS_I32_GT_S:
756
1.67k
  case WASM_INS_I32_GT_U:
757
1.89k
  case WASM_INS_I32_LE_S:
758
2.09k
  case WASM_INS_I32_LE_U:
759
2.91k
  case WASM_INS_I32_GE_S:
760
3.12k
  case WASM_INS_I32_GE_U:
761
3.36k
  case WASM_INS_I64_EQZ:
762
3.56k
  case WASM_INS_I64_EQ:
763
3.89k
  case WASM_INS_I64_NE:
764
4.09k
  case WASM_INS_I64_LT_S:
765
4.31k
  case WASM_INS_I64_LT_U:
766
4.51k
  case WASN_INS_I64_GT_S:
767
4.71k
  case WASM_INS_I64_GT_U:
768
5.04k
  case WASM_INS_I64_LE_S:
769
5.23k
  case WASM_INS_I64_LE_U:
770
5.55k
  case WASM_INS_I64_GE_S:
771
5.75k
  case WASM_INS_I64_GE_U:
772
5.98k
  case WASM_INS_F32_EQ:
773
6.25k
  case WASM_INS_F32_NE:
774
6.47k
  case WASM_INS_F32_LT:
775
6.68k
  case WASM_INS_F32_GT:
776
6.88k
  case WASM_INS_F32_LE:
777
7.11k
  case WASM_INS_F32_GE:
778
7.46k
  case WASM_INS_F64_EQ:
779
7.71k
  case WASM_INS_F64_NE:
780
7.94k
  case WASM_INS_F64_LT:
781
8.17k
  case WASM_INS_F64_GT:
782
8.45k
  case WASM_INS_F64_LE:
783
8.67k
  case WASM_INS_F64_GE:
784
8.89k
  case WASM_INS_I32_CLZ:
785
9.10k
  case WASM_INS_I32_CTZ:
786
9.31k
  case WASM_INS_I32_POPCNT:
787
9.52k
  case WASM_INS_I32_ADD:
788
9.72k
  case WASM_INS_I32_SUB:
789
10.0k
  case WASM_INS_I32_MUL:
790
10.2k
  case WASM_INS_I32_DIV_S:
791
10.5k
  case WASM_INS_I32_DIV_U:
792
10.7k
  case WASM_INS_I32_REM_S:
793
11.0k
  case WASM_INS_I32_REM_U:
794
11.2k
  case WASM_INS_I32_AND:
795
11.5k
  case WASM_INS_I32_OR:
796
11.7k
  case WASM_INS_I32_XOR:
797
11.9k
  case WASM_INS_I32_SHL:
798
12.1k
  case WASM_INS_I32_SHR_S:
799
12.4k
  case WASM_INS_I32_SHR_U:
800
13.2k
  case WASM_INS_I32_ROTL:
801
13.5k
  case WASM_INS_I32_ROTR:
802
13.7k
  case WASM_INS_I64_CLZ:
803
13.9k
  case WASM_INS_I64_CTZ:
804
14.1k
  case WASM_INS_I64_POPCNT:
805
14.3k
  case WASM_INS_I64_ADD:
806
14.6k
  case WASM_INS_I64_SUB:
807
14.8k
  case WASM_INS_I64_MUL:
808
15.0k
  case WASM_INS_I64_DIV_S:
809
15.5k
  case WASM_INS_I64_DIV_U:
810
15.8k
  case WASM_INS_I64_REM_S:
811
16.0k
  case WASM_INS_I64_REM_U:
812
16.2k
  case WASM_INS_I64_AND:
813
16.4k
  case WASM_INS_I64_OR:
814
16.8k
  case WASM_INS_I64_XOR:
815
17.1k
  case WASM_INS_I64_SHL:
816
20.6k
  case WASM_INS_I64_SHR_S:
817
20.9k
  case WASM_INS_I64_SHR_U:
818
21.1k
  case WASM_INS_I64_ROTL:
819
21.4k
  case WASM_INS_I64_ROTR:
820
21.6k
  case WASM_INS_F32_ABS:
821
21.8k
  case WASM_INS_F32_NEG:
822
22.4k
  case WASM_INS_F32_CEIL:
823
22.6k
  case WASM_INS_F32_FLOOR:
824
22.9k
  case WASM_INS_F32_TRUNC:
825
23.2k
  case WASM_INS_F32_NEAREST:
826
23.4k
  case WASM_INS_F32_SQRT:
827
24.4k
  case WASM_INS_F32_ADD:
828
24.6k
  case WASM_INS_F32_SUB:
829
24.8k
  case WASM_INS_F32_MUL:
830
25.0k
  case WASM_INS_F32_DIV:
831
25.2k
  case WASM_INS_F32_MIN:
832
25.7k
  case WASM_INS_F32_MAX:
833
26.1k
  case WASM_INS_F32_COPYSIGN:
834
26.3k
  case WASM_INS_F64_ABS:
835
26.5k
  case WASM_INS_F64_NEG:
836
26.8k
  case WASM_INS_F64_CEIL:
837
27.0k
  case WASM_INS_F64_FLOOR:
838
27.3k
  case WASM_INS_F64_TRUNC:
839
27.6k
  case WASM_INS_F64_NEAREST:
840
27.9k
  case WASM_INS_F64_SQRT:
841
30.4k
  case WASM_INS_F64_ADD:
842
30.6k
  case WASM_INS_F64_SUB:
843
30.8k
  case WASM_INS_F64_MUL:
844
31.0k
  case WASM_INS_F64_DIV:
845
31.2k
  case WASM_INS_F64_MIN:
846
31.6k
  case WASM_INS_F64_MAX:
847
31.8k
  case WASM_INS_F64_COPYSIGN:
848
32.0k
  case WASM_INS_I32_WARP_I64:
849
32.4k
  case WASP_INS_I32_TRUNC_S_F32:
850
32.6k
  case WASM_INS_I32_TRUNC_U_F32:
851
32.8k
  case WASM_INS_I32_TRUNC_S_F64:
852
33.0k
  case WASM_INS_I32_TRUNC_U_F64:
853
33.2k
  case WASM_INS_I64_EXTEND_S_I32:
854
33.4k
  case WASM_INS_I64_EXTEND_U_I32:
855
33.6k
  case WASM_INS_I64_TRUNC_S_F32:
856
33.8k
  case WASM_INS_I64_TRUNC_U_F32:
857
34.1k
  case WASM_INS_I64_TRUNC_S_F64:
858
34.3k
  case WASM_INS_I64_TRUNC_U_F64:
859
34.6k
  case WASM_INS_F32_CONVERT_S_I32:
860
34.8k
  case WASM_INS_F32_CONVERT_U_I32:
861
35.1k
  case WASM_INS_F32_CONVERT_S_I64:
862
35.4k
  case WASM_INS_F32_CONVERT_U_I64:
863
35.5k
  case WASM_INS_F32_DEMOTE_F64:
864
35.8k
  case WASM_INS_F64_CONVERT_S_I32:
865
38.2k
  case WASM_INS_F64_CONVERT_U_I32:
866
38.8k
  case WASM_INS_F64_CONVERT_S_I64:
867
39.0k
  case WASM_INS_F64_CONVERT_U_I64:
868
39.2k
  case WASM_INS_F64_PROMOTE_F32:
869
39.4k
  case WASM_INS_I32_REINTERPRET_F32:
870
39.6k
  case WASM_INS_I64_REINTERPRET_F64:
871
39.8k
  case WASM_INS_F32_REINTERPRET_I32:
872
40.0k
  case WASM_INS_F64_REINTERPRET_I64:
873
40.0k
    MI->wasm_data.type = WASM_OP_NONE;
874
875
40.0k
    if (MI->flat_insn->detail) {
876
40.0k
      MI->flat_insn->detail->wasm.op_count = 0;
877
40.0k
      MI->flat_insn->detail
878
40.0k
        ->groups[MI->flat_insn->detail->groups_count] =
879
40.0k
        WASM_GRP_NUMBERIC;
880
40.0k
      MI->flat_insn->detail->groups_count++;
881
40.0k
    }
882
883
40.0k
    *size = 1;
884
885
40.0k
    break;
886
887
207
  case WASM_INS_DROP:
888
561
  case WASM_INS_SELECT:
889
561
    MI->wasm_data.type = WASM_OP_NONE;
890
891
561
    if (MI->flat_insn->detail) {
892
561
      MI->flat_insn->detail->wasm.op_count = 0;
893
561
      MI->flat_insn->detail
894
561
        ->groups[MI->flat_insn->detail->groups_count] =
895
561
        WASM_GRP_PARAMETRIC;
896
561
      MI->flat_insn->detail->groups_count++;
897
561
    }
898
899
561
    *size = 1;
900
901
561
    break;
902
903
251
  case WASM_INS_GET_LOCAL:
904
627
  case WASM_INS_SET_LOCAL:
905
844
  case WASM_INS_TEE_LOCAL:
906
1.10k
  case WASM_INS_GET_GLOBAL:
907
1.34k
  case WASM_INS_SET_GLOBAL:
908
1.34k
    if (code_len == 1 ||
909
1.31k
        !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
910
36
      return false;
911
36
    }
912
913
1.30k
    if (MI->flat_insn->detail) {
914
1.30k
      MI->flat_insn->detail->wasm.op_count = 1;
915
1.30k
      MI->flat_insn->detail
916
1.30k
        ->groups[MI->flat_insn->detail->groups_count] =
917
1.30k
        WASM_GRP_VARIABLE;
918
1.30k
      MI->flat_insn->detail->groups_count++;
919
1.30k
    }
920
921
1.30k
    *size = param_size + 1;
922
923
1.30k
    break;
924
925
215
  case WASM_INS_I32_LOAD:
926
442
  case WASM_INS_I64_LOAD:
927
772
  case WASM_INS_F32_LOAD:
928
1.12k
  case WASM_INS_F64_LOAD:
929
1.42k
  case WASM_INS_I32_LOAD8_S:
930
2.13k
  case WASM_INS_I32_LOAD8_U:
931
2.35k
  case WASM_INS_I32_LOAD16_S:
932
2.62k
  case WASM_INS_I32_LOAD16_U:
933
2.88k
  case WASM_INS_I64_LOAD8_S:
934
3.11k
  case WASM_INS_I64_LOAD8_U:
935
3.33k
  case WASM_INS_I64_LOAD16_S:
936
3.55k
  case WASM_INS_I64_LOAD16_U:
937
3.77k
  case WASM_INS_I64_LOAD32_S:
938
4.00k
  case WASM_INS_I64_LOAD32_U:
939
4.20k
  case WASM_INS_I32_STORE:
940
4.43k
  case WASM_INS_I64_STORE:
941
4.66k
  case WASM_INS_F32_STORE:
942
4.86k
  case WASM_INS_F64_STORE:
943
5.12k
  case WASM_INS_I32_STORE8:
944
5.48k
  case WASM_INS_I32_STORE16:
945
5.69k
  case WASM_INS_I64_STORE8:
946
5.92k
  case WASM_INS_I64_STORE16:
947
6.14k
  case WASM_INS_I64_STORE32:
948
6.14k
    if (code_len == 1 ||
949
6.00k
        !read_memoryimmediate(&code[1], code_len - 1, &param_size,
950
6.00k
            MI)) {
951
184
      return false;
952
184
    }
953
954
5.96k
    if (MI->flat_insn->detail) {
955
5.96k
      MI->flat_insn->detail->wasm.op_count = 2;
956
5.96k
      MI->flat_insn->detail
957
5.96k
        ->groups[MI->flat_insn->detail->groups_count] =
958
5.96k
        WASM_GRP_MEMORY;
959
5.96k
      MI->flat_insn->detail->groups_count++;
960
5.96k
    }
961
962
5.96k
    *size = param_size + 1;
963
964
5.96k
    break;
965
966
226
  case WASM_INS_CURRENT_MEMORY:
967
456
  case WASM_INS_GROW_MEMORY:
968
456
    MI->wasm_data.type = WASM_OP_NONE;
969
970
456
    if (MI->flat_insn->detail) {
971
456
      MI->flat_insn->detail->wasm.op_count = 0;
972
456
      MI->flat_insn->detail
973
456
        ->groups[MI->flat_insn->detail->groups_count] =
974
456
        WASM_GRP_MEMORY;
975
456
      MI->flat_insn->detail->groups_count++;
976
456
    }
977
978
456
    *size = 1;
979
980
456
    break;
981
982
2.76k
  case WASM_INS_UNREACHABLE:
983
3.13k
  case WASM_INS_NOP:
984
3.34k
  case WASM_INS_ELSE:
985
3.56k
  case WASM_INS_END:
986
3.97k
  case WASM_INS_RETURN:
987
3.97k
    MI->wasm_data.type = WASM_OP_NONE;
988
989
3.97k
    if (MI->flat_insn->detail) {
990
3.97k
      MI->flat_insn->detail->wasm.op_count = 0;
991
3.97k
      MI->flat_insn->detail
992
3.97k
        ->groups[MI->flat_insn->detail->groups_count] =
993
3.97k
        WASM_GRP_CONTROL;
994
3.97k
      MI->flat_insn->detail->groups_count++;
995
3.97k
    }
996
997
3.97k
    *size = 1;
998
999
3.97k
    break;
1000
1001
217
  case WASM_INS_BLOCK:
1002
431
  case WASM_INS_LOOP:
1003
823
  case WASM_INS_IF:
1004
823
    if (code_len == 1 ||
1005
794
        !read_varint7(&code[1], code_len - 1, &param_size, MI)) {
1006
29
      return false;
1007
29
    }
1008
1009
794
    if (MI->flat_insn->detail) {
1010
794
      MI->flat_insn->detail->wasm.op_count = 1;
1011
794
      MI->flat_insn->detail
1012
794
        ->groups[MI->flat_insn->detail->groups_count] =
1013
794
        WASM_GRP_CONTROL;
1014
794
      MI->flat_insn->detail->groups_count++;
1015
794
    }
1016
1017
794
    *size = param_size + 1;
1018
1019
794
    break;
1020
1021
404
  case WASM_INS_BR:
1022
818
  case WASM_INS_BR_IF:
1023
1.02k
  case WASM_INS_CALL:
1024
1.35k
  case WASM_INS_CALL_INDIRECT:
1025
1.35k
    if (code_len == 1 ||
1026
1.33k
        !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
1027
29
      return false;
1028
29
    }
1029
1030
1.32k
    if (MI->flat_insn->detail) {
1031
1.32k
      MI->flat_insn->detail->wasm.op_count = 1;
1032
1.32k
      MI->flat_insn->detail
1033
1.32k
        ->groups[MI->flat_insn->detail->groups_count] =
1034
1.32k
        WASM_GRP_CONTROL;
1035
1.32k
      MI->flat_insn->detail->groups_count++;
1036
1.32k
    }
1037
1038
1.32k
    *size = param_size + 1;
1039
1040
1.32k
    break;
1041
1042
1.01k
  case WASM_INS_BR_TABLE:
1043
1.01k
    if (code_len == 1 ||
1044
1.00k
        !read_brtable(&code[1], code_len - 1, &param_size, MI)) {
1045
106
      return false;
1046
106
    }
1047
1048
905
    if (MI->flat_insn->detail) {
1049
905
      MI->flat_insn->detail->wasm.op_count = 1;
1050
905
      MI->flat_insn->detail
1051
905
        ->groups[MI->flat_insn->detail->groups_count] =
1052
905
        WASM_GRP_CONTROL;
1053
905
      MI->flat_insn->detail->groups_count++;
1054
905
    }
1055
1056
905
    *size = param_size + 1;
1057
1058
905
    break;
1059
56.8k
  }
1060
1061
56.4k
  return true;
1062
56.8k
}