Coverage Report

Created: 2025-08-26 06:30

/src/capstonenext/arch/WASM/WASMDisassembler.c
Line
Count
Source (jump to first uncovered line)
1
/* Capstone Disassembly Engine */
2
/* By Spike, xwings 2019 */
3
4
#include <string.h>
5
#include <stddef.h> // offsetof macro
6
// alternatively #include "../../utils.h" like everyone else
7
8
#include "WASMDisassembler.h"
9
#include "WASMMapping.h"
10
#include "../../cs_priv.h"
11
12
static const short opcodes[256] = {
13
  WASM_INS_UNREACHABLE,
14
  WASM_INS_NOP,
15
  WASM_INS_BLOCK,
16
  WASM_INS_LOOP,
17
  WASM_INS_IF,
18
  WASM_INS_ELSE,
19
  -1,
20
  -1,
21
  -1,
22
  -1,
23
  -1,
24
  WASM_INS_END,
25
  WASM_INS_BR,
26
  WASM_INS_BR_IF,
27
  WASM_INS_BR_TABLE,
28
  WASM_INS_RETURN,
29
  WASM_INS_CALL,
30
  WASM_INS_CALL_INDIRECT,
31
  -1,
32
  -1,
33
  -1,
34
  -1,
35
  -1,
36
  -1,
37
  -1,
38
  -1,
39
  WASM_INS_DROP,
40
  WASM_INS_SELECT,
41
  -1,
42
  -1,
43
  -1,
44
  -1,
45
  WASM_INS_GET_LOCAL,
46
  WASM_INS_SET_LOCAL,
47
  WASM_INS_TEE_LOCAL,
48
  WASM_INS_GET_GLOBAL,
49
  WASM_INS_SET_GLOBAL,
50
  -1,
51
  -1,
52
  -1,
53
  WASM_INS_I32_LOAD,
54
  WASM_INS_I64_LOAD,
55
  WASM_INS_F32_LOAD,
56
  WASM_INS_F64_LOAD,
57
  WASM_INS_I32_LOAD8_S,
58
  WASM_INS_I32_LOAD8_U,
59
  WASM_INS_I32_LOAD16_S,
60
  WASM_INS_I32_LOAD16_U,
61
  WASM_INS_I64_LOAD8_S,
62
  WASM_INS_I64_LOAD8_U,
63
  WASM_INS_I64_LOAD16_S,
64
  WASM_INS_I64_LOAD16_U,
65
  WASM_INS_I64_LOAD32_S,
66
  WASM_INS_I64_LOAD32_U,
67
  WASM_INS_I32_STORE,
68
  WASM_INS_I64_STORE,
69
  WASM_INS_F32_STORE,
70
  WASM_INS_F64_STORE,
71
  WASM_INS_I32_STORE8,
72
  WASM_INS_I32_STORE16,
73
  WASM_INS_I64_STORE8,
74
  WASM_INS_I64_STORE16,
75
  WASM_INS_I64_STORE32,
76
  WASM_INS_CURRENT_MEMORY,
77
  WASM_INS_GROW_MEMORY,
78
  WASM_INS_I32_CONST,
79
  WASM_INS_I64_CONST,
80
  WASM_INS_F32_CONST,
81
  WASM_INS_F64_CONST,
82
  WASM_INS_I32_EQZ,
83
  WASM_INS_I32_EQ,
84
  WASM_INS_I32_NE,
85
  WASM_INS_I32_LT_S,
86
  WASM_INS_I32_LT_U,
87
  WASM_INS_I32_GT_S,
88
  WASM_INS_I32_GT_U,
89
  WASM_INS_I32_LE_S,
90
  WASM_INS_I32_LE_U,
91
  WASM_INS_I32_GE_S,
92
  WASM_INS_I32_GE_U,
93
  WASM_INS_I64_EQZ,
94
  WASM_INS_I64_EQ,
95
  WASM_INS_I64_NE,
96
  WASM_INS_I64_LT_S,
97
  WASM_INS_I64_LT_U,
98
  WASN_INS_I64_GT_S,
99
  WASM_INS_I64_GT_U,
100
  WASM_INS_I64_LE_S,
101
  WASM_INS_I64_LE_U,
102
  WASM_INS_I64_GE_S,
103
  WASM_INS_I64_GE_U,
104
  WASM_INS_F32_EQ,
105
  WASM_INS_F32_NE,
106
  WASM_INS_F32_LT,
107
  WASM_INS_F32_GT,
108
  WASM_INS_F32_LE,
109
  WASM_INS_F32_GE,
110
  WASM_INS_F64_EQ,
111
  WASM_INS_F64_NE,
112
  WASM_INS_F64_LT,
113
  WASM_INS_F64_GT,
114
  WASM_INS_F64_LE,
115
  WASM_INS_F64_GE,
116
  WASM_INS_I32_CLZ,
117
  WASM_INS_I32_CTZ,
118
  WASM_INS_I32_POPCNT,
119
  WASM_INS_I32_ADD,
120
  WASM_INS_I32_SUB,
121
  WASM_INS_I32_MUL,
122
  WASM_INS_I32_DIV_S,
123
  WASM_INS_I32_DIV_U,
124
  WASM_INS_I32_REM_S,
125
  WASM_INS_I32_REM_U,
126
  WASM_INS_I32_AND,
127
  WASM_INS_I32_OR,
128
  WASM_INS_I32_XOR,
129
  WASM_INS_I32_SHL,
130
  WASM_INS_I32_SHR_S,
131
  WASM_INS_I32_SHR_U,
132
  WASM_INS_I32_ROTL,
133
  WASM_INS_I32_ROTR,
134
  WASM_INS_I64_CLZ,
135
  WASM_INS_I64_CTZ,
136
  WASM_INS_I64_POPCNT,
137
  WASM_INS_I64_ADD,
138
  WASM_INS_I64_SUB,
139
  WASM_INS_I64_MUL,
140
  WASM_INS_I64_DIV_S,
141
  WASM_INS_I64_DIV_U,
142
  WASM_INS_I64_REM_S,
143
  WASM_INS_I64_REM_U,
144
  WASM_INS_I64_AND,
145
  WASM_INS_I64_OR,
146
  WASM_INS_I64_XOR,
147
  WASM_INS_I64_SHL,
148
  WASM_INS_I64_SHR_S,
149
  WASM_INS_I64_SHR_U,
150
  WASM_INS_I64_ROTL,
151
  WASM_INS_I64_ROTR,
152
  WASM_INS_F32_ABS,
153
  WASM_INS_F32_NEG,
154
  WASM_INS_F32_CEIL,
155
  WASM_INS_F32_FLOOR,
156
  WASM_INS_F32_TRUNC,
157
  WASM_INS_F32_NEAREST,
158
  WASM_INS_F32_SQRT,
159
  WASM_INS_F32_ADD,
160
  WASM_INS_F32_SUB,
161
  WASM_INS_F32_MUL,
162
  WASM_INS_F32_DIV,
163
  WASM_INS_F32_MIN,
164
  WASM_INS_F32_MAX,
165
  WASM_INS_F32_COPYSIGN,
166
  WASM_INS_F64_ABS,
167
  WASM_INS_F64_NEG,
168
  WASM_INS_F64_CEIL,
169
  WASM_INS_F64_FLOOR,
170
  WASM_INS_F64_TRUNC,
171
  WASM_INS_F64_NEAREST,
172
  WASM_INS_F64_SQRT,
173
  WASM_INS_F64_ADD,
174
  WASM_INS_F64_SUB,
175
  WASM_INS_F64_MUL,
176
  WASM_INS_F64_DIV,
177
  WASM_INS_F64_MIN,
178
  WASM_INS_F64_MAX,
179
  WASM_INS_F64_COPYSIGN,
180
  WASM_INS_I32_WARP_I64,
181
  WASP_INS_I32_TRUNC_S_F32,
182
  WASM_INS_I32_TRUNC_U_F32,
183
  WASM_INS_I32_TRUNC_S_F64,
184
  WASM_INS_I32_TRUNC_U_F64,
185
  WASM_INS_I64_EXTEND_S_I32,
186
  WASM_INS_I64_EXTEND_U_I32,
187
  WASM_INS_I64_TRUNC_S_F32,
188
  WASM_INS_I64_TRUNC_U_F32,
189
  WASM_INS_I64_TRUNC_S_F64,
190
  WASM_INS_I64_TRUNC_U_F64,
191
  WASM_INS_F32_CONVERT_S_I32,
192
  WASM_INS_F32_CONVERT_U_I32,
193
  WASM_INS_F32_CONVERT_S_I64,
194
  WASM_INS_F32_CONVERT_U_I64,
195
  WASM_INS_F32_DEMOTE_F64,
196
  WASM_INS_F64_CONVERT_S_I32,
197
  WASM_INS_F64_CONVERT_U_I32,
198
  WASM_INS_F64_CONVERT_S_I64,
199
  WASM_INS_F64_CONVERT_U_I64,
200
  WASM_INS_F64_PROMOTE_F32,
201
  WASM_INS_I32_REINTERPRET_F32,
202
  WASM_INS_I64_REINTERPRET_F64,
203
  WASM_INS_F32_REINTERPRET_I32,
204
  WASM_INS_F64_REINTERPRET_I64,
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
  -1,
269
};
270
271
// input  | code: code pointer start from varuint32
272
//        | code_len: real code len count from varint
273
//        | leng: return value, means length of varint. -1 means error
274
// return | varint
275
static uint32_t get_varuint32(const uint8_t *code, size_t code_len, size_t *leng)
276
23.8k
{
277
23.8k
  uint32_t data = 0;
278
23.8k
  int i;
279
280
31.1k
  for(i = 0;; i++) {
281
31.1k
    if (code_len < i + 1) {
282
238
      *leng = -1;
283
238
      return 0;
284
238
    }
285
286
287
30.9k
    if (i > 4 || (i == 4 && (code[i] & 0x7f) > 0x0f)) {
288
345
      *leng = -1;
289
345
      return 0;
290
345
    }
291
292
30.5k
    data = data + (((uint32_t) code[i] & 0x7f) << (i * 7));
293
30.5k
    if (code[i] >> 7 == 0) {
294
23.2k
      break;
295
23.2k
    }
296
30.5k
  }
297
298
23.2k
  *leng = i + 1;
299
300
23.2k
  return data;
301
23.8k
}
302
303
// input  | code : code pointer start from varuint64
304
//        | code_len : real code len count from varint
305
//        | leng: return value, means length of varint. -1 means error
306
// return   | varint
307
static uint64_t get_varuint64(const uint8_t *code, size_t code_len, size_t *leng)
308
277
{
309
277
  uint64_t data;
310
277
  int i;
311
312
277
  data = 0;
313
631
  for(i = 0;; i++){
314
631
    if (code_len < i + 1) {
315
5
      *leng = -1;
316
5
      return 0;
317
5
    }
318
319
626
    if (i > 9 || (i == 9 && (code[i] & 0x7f) > 0x01)) {
320
1
      *leng = -1;
321
1
      return 0;
322
1
    }
323
324
625
    data = data + (((uint64_t) code[i] & 0x7f) << (i * 7));
325
625
    if (code[i] >> 7 == 0) {
326
271
      break;
327
271
    }
328
625
  }
329
330
271
  *leng = i + 1;
331
332
271
  return data;
333
277
}
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
454
{
340
454
  memcpy(dest, code, 4);
341
454
}
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
562
{
348
562
  memcpy(dest, code, 8);
349
562
}
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
756
{
357
756
  int8_t data;
358
359
756
  if (code_len < 1) {
360
0
    *leng = -1;
361
0
    return -1;
362
0
  }
363
364
756
  *leng = 1;
365
366
756
  if (code[0] == 0x40) {
367
69
    return -1;
368
69
  }
369
370
687
  data = code[0] & 0x7f;
371
372
687
  return data;
373
756
}
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, uint16_t *param_size, MCInst *MI)
381
3.01k
{
382
3.01k
  size_t len = 0;
383
3.01k
  uint32_t data;
384
385
3.01k
  data = get_varuint32(code, code_len, &len);
386
3.01k
  if (len == -1) {
387
27
    return false;
388
27
  }
389
390
2.99k
  if (MI->flat_insn->detail) {
391
2.99k
    MI->flat_insn->detail->wasm.op_count = 1;
392
2.99k
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_VARUINT32;
393
2.99k
    MI->flat_insn->detail->wasm.operands[0].size= len;
394
2.99k
    MI->flat_insn->detail->wasm.operands[0].varuint32= data;
395
2.99k
  }
396
397
2.99k
  MI->wasm_data.size = len;
398
2.99k
  MI->wasm_data.type = WASM_OP_VARUINT32;
399
2.99k
  MI->wasm_data.uint32 = data;
400
2.99k
  *param_size = len;
401
402
2.99k
  return true;
403
3.01k
}
404
405
// input  | code : code pointer start from varuint64
406
//      | code_len : start from the code pointer to the end, how long is it
407
//      | param_size : pointer of the param size
408
//      | MI : Mcinst handler in this round of disasm
409
// return   | true/false if the function successfully finished 
410
static bool read_varuint64(const uint8_t *code, size_t code_len, uint16_t *param_size, MCInst *MI)
411
277
{
412
277
  size_t len = 0;
413
277
  uint64_t data;
414
415
277
  data = get_varuint64(code, code_len, &len);
416
277
  if (len == -1) {
417
6
    return false;
418
6
  }
419
420
271
  if (MI->flat_insn->detail) {
421
271
    MI->flat_insn->detail->wasm.op_count = 1;
422
271
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_VARUINT64;
423
271
    MI->flat_insn->detail->wasm.operands[0].size = len;
424
271
    MI->flat_insn->detail->wasm.operands[0].varuint64 = data;
425
271
  }
426
427
271
  MI->wasm_data.size = len;
428
271
  MI->wasm_data.type = WASM_OP_VARUINT64;
429
271
  MI->wasm_data.uint64 = data;
430
271
  *param_size = len;
431
432
271
  return true;
433
277
}
434
435
// input  | code : code pointer start from memoryimmediate
436
//      | code_len : start from the code pointer to the end, how long is it
437
//      | param_size : pointer of the param size (sum of two params)
438
//      | MI : Mcinst handler in this round of disasm
439
// return   | true/false if the function successfully finished 
440
static bool read_memoryimmediate(const uint8_t *code, size_t code_len, uint16_t *param_size, MCInst *MI)
441
6.67k
{
442
6.67k
  size_t tmp, len = 0;
443
6.67k
  uint32_t data[2];
444
445
6.67k
  if (MI->flat_insn->detail) {
446
6.67k
    MI->flat_insn->detail->wasm.op_count = 2;
447
6.67k
  }
448
449
6.67k
  data[0] = get_varuint32(code, code_len, &tmp);
450
6.67k
  if (tmp == -1) {
451
48
    return false;
452
48
  }
453
454
6.62k
  if (MI->flat_insn->detail) {
455
6.62k
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_VARUINT32;
456
6.62k
    MI->flat_insn->detail->wasm.operands[0].size = tmp;
457
6.62k
    MI->flat_insn->detail->wasm.operands[0].varuint32 = data[0];
458
6.62k
  }
459
460
6.62k
  len = tmp;
461
6.62k
  data[1] = get_varuint32(&code[len], code_len - len, &tmp);
462
463
6.62k
  if (MI->flat_insn->detail) {
464
6.62k
    MI->flat_insn->detail->wasm.operands[1].type = WASM_OP_VARUINT32;
465
6.62k
    MI->flat_insn->detail->wasm.operands[1].size = tmp;
466
6.62k
    MI->flat_insn->detail->wasm.operands[1].varuint32 = data[1];
467
6.62k
  }
468
469
6.62k
  len += tmp;
470
6.62k
  MI->wasm_data.size = len;
471
6.62k
  MI->wasm_data.type = WASM_OP_IMM;
472
6.62k
  MI->wasm_data.immediate[0] = data[0];
473
6.62k
  MI->wasm_data.immediate[1] = data[1];
474
6.62k
  *param_size = len;
475
476
6.62k
  return true;
477
6.67k
}
478
479
// input  | code : code pointer start from uint32
480
//      | code_len : start from the code pointer to the end, how long is it
481
//      | param_size : pointer of the param size
482
//      | MI : Mcinst handler in this round of disasm
483
// return   | true/false if the function successfully finished 
484
static bool read_uint32(const uint8_t *code, size_t code_len, uint16_t *param_size, MCInst *MI)
485
232
{
486
232
  if (code_len < 4) {
487
5
    return false;
488
5
  }
489
490
227
  get_uint32(code, &(MI->wasm_data.uint32));
491
492
227
  if (MI->flat_insn->detail) {
493
227
    MI->flat_insn->detail->wasm.op_count = 1;
494
227
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_UINT32;
495
227
    MI->flat_insn->detail->wasm.operands[0].size = 4;
496
227
    get_uint32(code, &(MI->flat_insn->detail->wasm.operands[0].uint32));
497
227
  }
498
499
227
  MI->wasm_data.size = 4;
500
227
  MI->wasm_data.type = WASM_OP_UINT32;
501
227
  *param_size = 4;
502
503
227
  return true;
504
232
}
505
506
// input  | code : code pointer start from uint64
507
//      | code_len : start from the code pointer to the end, how long is it
508
//      | param_size : pointer of the param size
509
//      | MI : Mcinst handler in this round of disasm
510
// return   | true/false if the function successfully finished 
511
static bool read_uint64(const uint8_t *code, size_t code_len, uint16_t *param_size, MCInst *MI)
512
288
{
513
288
  if (code_len < 8) {
514
7
    return false;
515
7
  }
516
517
281
  get_uint64(code, &(MI->wasm_data.uint64));
518
519
281
  if (MI->flat_insn->detail) {
520
281
    MI->flat_insn->detail->wasm.op_count = 1;
521
281
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_UINT64;
522
281
    MI->flat_insn->detail->wasm.operands[0].size = 8;
523
281
    get_uint64(code, &(MI->flat_insn->detail->wasm.operands[0].uint64));
524
281
  }
525
526
281
  MI->wasm_data.size = 8;
527
281
  MI->wasm_data.type = WASM_OP_UINT64;
528
281
  *param_size = 8;
529
530
281
  return true;
531
288
}
532
533
// input  | code : code pointer start from brtable
534
//      | code_len : start from the code pointer to the end, how long is it
535
//      | param_size : pointer of the param size (sum of all param)
536
//      | MI : Mcinst handler in this round of disasm
537
// return   | true/false if the function successfully finished 
538
static bool read_brtable(const uint8_t *code, size_t code_len, uint16_t *param_size, MCInst *MI)
539
992
{
540
992
  uint32_t length, default_target;
541
992
  int tmp_len = 0, i;
542
992
  size_t var_len;
543
544
  // read length
545
992
  length = get_varuint32(code, code_len, &var_len);
546
992
  if (var_len == -1) {
547
9
    return false;
548
9
  }
549
550
983
  tmp_len += var_len;
551
983
  MI->wasm_data.brtable.length = length;
552
983
  if (length >= UINT32_MAX - tmp_len) {
553
    // integer overflow check
554
1
    return false;
555
1
  }
556
982
  if (code_len < tmp_len + length) {
557
    // safety check that we have minimum enough data to read
558
45
    return false;
559
45
  }
560
  // base address + 1 byte opcode + tmp_len for number of cases = start of targets
561
937
  MI->wasm_data.brtable.address = MI->address + 1 + tmp_len;
562
563
937
  if (MI->flat_insn->detail) {
564
937
    MI->flat_insn->detail->wasm.op_count = 1;
565
937
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_BRTABLE;
566
937
    MI->flat_insn->detail->wasm.operands[0].brtable.length = MI->wasm_data.brtable.length;
567
937
    MI->flat_insn->detail->wasm.operands[0].brtable.address = MI->wasm_data.brtable.address;
568
937
  }
569
570
  // read data
571
6.56k
  for(i = 0; i < length; i++){
572
5.65k
    if (code_len < tmp_len) {
573
0
      return false;
574
0
    }
575
576
5.65k
    get_varuint32(code + tmp_len, code_len - tmp_len, &var_len);
577
5.65k
    if (var_len == -1) {
578
19
      return false;
579
19
    }
580
581
5.63k
    tmp_len += var_len;
582
5.63k
  }
583
584
  // read default target
585
918
  default_target = get_varuint32(code + tmp_len, code_len - tmp_len, &var_len);
586
918
  if (var_len == -1) {
587
23
    return false;
588
23
  }
589
590
895
  MI->wasm_data.brtable.default_target = default_target;
591
895
  MI->wasm_data.type = WASM_OP_BRTABLE;
592
895
  *param_size = tmp_len + var_len;
593
594
895
  if (MI->flat_insn->detail) {
595
895
    MI->flat_insn->detail->wasm.operands[0].size = *param_size;
596
895
    MI->flat_insn->detail->wasm.operands[0].brtable.default_target = MI->wasm_data.brtable.default_target;
597
895
  }
598
599
895
  return true;
600
918
}
601
602
// input  | code : code pointer start from varint7
603
//      | code_len : start from the code pointer to the end, how long is it
604
//      | param_size : pointer of the param size
605
//      | MI : Mcinst handler in this round of disasm
606
// return   | true/false if the function successfully finished 
607
static bool read_varint7(const uint8_t *code, size_t code_len, uint16_t *param_size, MCInst *MI)
608
756
{
609
756
  size_t len = 0;
610
611
756
  MI->wasm_data.type = WASM_OP_INT7;
612
756
  MI->wasm_data.int7 = get_varint7(code, code_len, &len);
613
756
  if (len == -1) {
614
0
    return false;
615
0
  }
616
617
756
  if (MI->flat_insn->detail) {
618
756
    MI->flat_insn->detail->wasm.op_count = 1;
619
756
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_INT7;
620
756
    MI->flat_insn->detail->wasm.operands[0].size = 1;
621
756
    MI->flat_insn->detail->wasm.operands[0].int7 = MI->wasm_data.int7;
622
756
  }
623
624
756
  *param_size = len;
625
626
756
  return true;
627
756
}
628
629
bool WASM_getInstruction(csh ud, const uint8_t *code, size_t code_len,
630
    MCInst *MI, uint16_t *size, uint64_t address, void *inst_info)
631
59.9k
{
632
59.9k
  unsigned char opcode;
633
59.9k
  uint16_t param_size;
634
635
59.9k
  if (code_len == 0)
636
0
    return false;
637
638
59.9k
  opcode = code[0];
639
59.9k
  if (opcodes[opcode] == -1) {
640
    // invalid opcode
641
166
    return false;
642
166
  }
643
644
  // valid opcode
645
59.7k
  MI->address = address;
646
59.7k
  MI->OpcodePub = MI->Opcode = opcode;
647
648
59.7k
  if (MI->flat_insn->detail) {
649
59.7k
    memset(MI->flat_insn->detail, 0, offsetof(cs_detail, wasm)+sizeof(cs_wasm));
650
59.7k
    WASM_get_insn_id((cs_struct *)ud, MI->flat_insn, opcode);
651
59.7k
  }
652
653
  // setup groups
654
59.7k
  switch(opcode) {
655
0
    default:
656
0
      return false;
657
658
500
    case WASM_INS_I32_CONST:
659
500
      if (code_len == 1 || !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
660
12
        return false;
661
12
      }
662
663
488
      if (MI->flat_insn->detail) {
664
488
        MI->flat_insn->detail->wasm.op_count = 1;
665
488
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_NUMBERIC;
666
488
        MI->flat_insn->detail->groups_count++;
667
488
      }
668
669
488
      *size = param_size + 1;
670
671
488
      break;
672
673
282
    case WASM_INS_I64_CONST:
674
282
      if (code_len == 1 || !read_varuint64(&code[1], code_len - 1, &param_size, MI)) {
675
11
        return false;
676
11
      }
677
678
271
      if (MI->flat_insn->detail) {
679
271
        MI->flat_insn->detail->wasm.op_count = 1;
680
271
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_NUMBERIC;
681
271
        MI->flat_insn->detail->groups_count++;
682
271
      }
683
684
271
      *size = param_size + 1;
685
686
271
      break;
687
688
235
    case WASM_INS_F32_CONST:
689
235
      if (code_len == 1 || !read_uint32(&code[1], code_len - 1, &param_size, MI)) {
690
8
        return false;
691
8
      }
692
693
227
      if (MI->flat_insn->detail) {
694
227
        MI->flat_insn->detail->wasm.op_count = 1;
695
227
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_NUMBERIC;
696
227
        MI->flat_insn->detail->groups_count++;
697
227
      }
698
699
227
      *size = param_size + 1;
700
701
227
      break;
702
703
289
    case WASM_INS_F64_CONST:
704
289
      if (code_len == 1 || !read_uint64(&code[1], code_len - 1, &param_size, MI)) {
705
8
        return false;
706
8
      }
707
708
281
      if (MI->flat_insn->detail) {
709
281
        MI->flat_insn->detail->wasm.op_count = 1;
710
281
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_NUMBERIC;
711
281
        MI->flat_insn->detail->groups_count++;
712
281
      }
713
714
281
      *size = param_size + 1;
715
716
281
      break;
717
718
229
    case WASM_INS_I32_EQZ:
719
595
    case WASM_INS_I32_EQ:
720
815
    case WASM_INS_I32_NE:
721
1.03k
    case WASM_INS_I32_LT_S:
722
1.27k
    case WASM_INS_I32_LT_U:
723
1.49k
    case WASM_INS_I32_GT_S:
724
1.75k
    case WASM_INS_I32_GT_U:
725
1.97k
    case WASM_INS_I32_LE_S:
726
3.60k
    case WASM_INS_I32_LE_U:
727
3.99k
    case WASM_INS_I32_GE_S:
728
4.21k
    case WASM_INS_I32_GE_U:
729
4.43k
    case WASM_INS_I64_EQZ:
730
5.35k
    case WASM_INS_I64_EQ:
731
5.84k
    case WASM_INS_I64_NE:
732
6.08k
    case WASM_INS_I64_LT_S:
733
6.33k
    case WASM_INS_I64_LT_U:
734
6.57k
    case WASN_INS_I64_GT_S:
735
6.80k
    case WASM_INS_I64_GT_U:
736
7.11k
    case WASM_INS_I64_LE_S:
737
7.33k
    case WASM_INS_I64_LE_U:
738
7.63k
    case WASM_INS_I64_GE_S:
739
7.85k
    case WASM_INS_I64_GE_U:
740
8.26k
    case WASM_INS_F32_EQ:
741
8.46k
    case WASM_INS_F32_NE:
742
8.83k
    case WASM_INS_F32_LT:
743
9.17k
    case WASM_INS_F32_GT:
744
9.38k
    case WASM_INS_F32_LE:
745
9.60k
    case WASM_INS_F32_GE:
746
13.9k
    case WASM_INS_F64_EQ:
747
14.3k
    case WASM_INS_F64_NE:
748
14.5k
    case WASM_INS_F64_LT:
749
14.8k
    case WASM_INS_F64_GT:
750
15.0k
    case WASM_INS_F64_LE:
751
15.5k
    case WASM_INS_F64_GE:
752
15.7k
    case WASM_INS_I32_CLZ:
753
15.9k
    case WASM_INS_I32_CTZ:
754
16.2k
    case WASM_INS_I32_POPCNT:
755
16.4k
    case WASM_INS_I32_ADD:
756
16.6k
    case WASM_INS_I32_SUB:
757
16.9k
    case WASM_INS_I32_MUL:
758
17.1k
    case WASM_INS_I32_DIV_S:
759
17.5k
    case WASM_INS_I32_DIV_U:
760
17.7k
    case WASM_INS_I32_REM_S:
761
17.9k
    case WASM_INS_I32_REM_U:
762
18.3k
    case WASM_INS_I32_AND:
763
18.5k
    case WASM_INS_I32_OR:
764
18.9k
    case WASM_INS_I32_XOR:
765
19.1k
    case WASM_INS_I32_SHL:
766
19.4k
    case WASM_INS_I32_SHR_S:
767
19.6k
    case WASM_INS_I32_SHR_U:
768
19.9k
    case WASM_INS_I32_ROTL:
769
20.3k
    case WASM_INS_I32_ROTR:
770
20.6k
    case WASM_INS_I64_CLZ:
771
20.9k
    case WASM_INS_I64_CTZ:
772
21.5k
    case WASM_INS_I64_POPCNT:
773
21.7k
    case WASM_INS_I64_ADD:
774
22.5k
    case WASM_INS_I64_SUB:
775
22.7k
    case WASM_INS_I64_MUL:
776
23.0k
    case WASM_INS_I64_DIV_S:
777
23.3k
    case WASM_INS_I64_DIV_U:
778
23.6k
    case WASM_INS_I64_REM_S:
779
23.8k
    case WASM_INS_I64_REM_U:
780
24.0k
    case WASM_INS_I64_AND:
781
24.2k
    case WASM_INS_I64_OR:
782
24.5k
    case WASM_INS_I64_XOR:
783
24.7k
    case WASM_INS_I64_SHL:
784
25.0k
    case WASM_INS_I64_SHR_S:
785
26.8k
    case WASM_INS_I64_SHR_U:
786
27.0k
    case WASM_INS_I64_ROTL:
787
27.2k
    case WASM_INS_I64_ROTR:
788
27.6k
    case WASM_INS_F32_ABS:
789
27.8k
    case WASM_INS_F32_NEG:
790
28.2k
    case WASM_INS_F32_CEIL:
791
28.4k
    case WASM_INS_F32_FLOOR:
792
28.6k
    case WASM_INS_F32_TRUNC:
793
29.0k
    case WASM_INS_F32_NEAREST:
794
29.2k
    case WASM_INS_F32_SQRT:
795
29.6k
    case WASM_INS_F32_ADD:
796
29.8k
    case WASM_INS_F32_SUB:
797
30.0k
    case WASM_INS_F32_MUL:
798
30.2k
    case WASM_INS_F32_DIV:
799
30.4k
    case WASM_INS_F32_MIN:
800
30.6k
    case WASM_INS_F32_MAX:
801
31.1k
    case WASM_INS_F32_COPYSIGN:
802
31.3k
    case WASM_INS_F64_ABS:
803
31.5k
    case WASM_INS_F64_NEG:
804
31.7k
    case WASM_INS_F64_CEIL:
805
32.0k
    case WASM_INS_F64_FLOOR:
806
32.4k
    case WASM_INS_F64_TRUNC:
807
32.7k
    case WASM_INS_F64_NEAREST:
808
32.9k
    case WASM_INS_F64_SQRT:
809
33.4k
    case WASM_INS_F64_ADD:
810
34.0k
    case WASM_INS_F64_SUB:
811
34.2k
    case WASM_INS_F64_MUL:
812
34.4k
    case WASM_INS_F64_DIV:
813
34.7k
    case WASM_INS_F64_MIN:
814
35.0k
    case WASM_INS_F64_MAX:
815
35.3k
    case WASM_INS_F64_COPYSIGN:
816
35.5k
    case WASM_INS_I32_WARP_I64:
817
35.7k
    case WASP_INS_I32_TRUNC_S_F32:
818
36.0k
    case WASM_INS_I32_TRUNC_U_F32:
819
36.3k
    case WASM_INS_I32_TRUNC_S_F64:
820
36.5k
    case WASM_INS_I32_TRUNC_U_F64:
821
36.9k
    case WASM_INS_I64_EXTEND_S_I32:
822
37.1k
    case WASM_INS_I64_EXTEND_U_I32:
823
37.5k
    case WASM_INS_I64_TRUNC_S_F32:
824
37.8k
    case WASM_INS_I64_TRUNC_U_F32:
825
38.0k
    case WASM_INS_I64_TRUNC_S_F64:
826
38.2k
    case WASM_INS_I64_TRUNC_U_F64:
827
38.5k
    case WASM_INS_F32_CONVERT_S_I32:
828
38.7k
    case WASM_INS_F32_CONVERT_U_I32:
829
38.9k
    case WASM_INS_F32_CONVERT_S_I64:
830
39.2k
    case WASM_INS_F32_CONVERT_U_I64:
831
39.5k
    case WASM_INS_F32_DEMOTE_F64:
832
39.8k
    case WASM_INS_F64_CONVERT_S_I32:
833
40.0k
    case WASM_INS_F64_CONVERT_U_I32:
834
40.2k
    case WASM_INS_F64_CONVERT_S_I64:
835
40.5k
    case WASM_INS_F64_CONVERT_U_I64:
836
40.8k
    case WASM_INS_F64_PROMOTE_F32:
837
41.0k
    case WASM_INS_I32_REINTERPRET_F32:
838
41.2k
    case WASM_INS_I64_REINTERPRET_F64:
839
41.4k
    case WASM_INS_F32_REINTERPRET_I32:
840
42.0k
    case WASM_INS_F64_REINTERPRET_I64:
841
42.0k
      MI->wasm_data.type = WASM_OP_NONE;
842
843
42.0k
      if (MI->flat_insn->detail) {
844
42.0k
        MI->flat_insn->detail->wasm.op_count = 0;
845
42.0k
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_NUMBERIC;
846
42.0k
        MI->flat_insn->detail->groups_count++;
847
42.0k
      }
848
849
42.0k
      *size = 1;
850
851
42.0k
      break;
852
853
206
    case WASM_INS_DROP:
854
433
    case WASM_INS_SELECT:
855
433
      MI->wasm_data.type = WASM_OP_NONE;
856
857
433
      if (MI->flat_insn->detail) {
858
433
        MI->flat_insn->detail->wasm.op_count = 0;
859
433
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_PARAMETRIC;
860
433
        MI->flat_insn->detail->groups_count++;
861
433
      }
862
863
433
      *size = 1;
864
865
433
      break;
866
867
448
    case WASM_INS_GET_LOCAL:
868
756
    case WASM_INS_SET_LOCAL:
869
970
    case WASM_INS_TEE_LOCAL:
870
1.22k
    case WASM_INS_GET_GLOBAL:
871
1.46k
    case WASM_INS_SET_GLOBAL:
872
1.46k
      if (code_len == 1 || !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
873
46
        return false;
874
46
      }
875
876
1.42k
      if (MI->flat_insn->detail) {
877
1.42k
        MI->flat_insn->detail->wasm.op_count = 1;
878
1.42k
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_VARIABLE;
879
1.42k
        MI->flat_insn->detail->groups_count++;
880
1.42k
      }
881
882
1.42k
      *size = param_size + 1;
883
884
1.42k
      break;
885
886
216
    case WASM_INS_I32_LOAD:
887
423
    case WASM_INS_I64_LOAD:
888
647
    case WASM_INS_F32_LOAD:
889
859
    case WASM_INS_F64_LOAD:
890
1.22k
    case WASM_INS_I32_LOAD8_S:
891
1.47k
    case WASM_INS_I32_LOAD8_U:
892
1.69k
    case WASM_INS_I32_LOAD16_S:
893
1.96k
    case WASM_INS_I32_LOAD16_U:
894
2.65k
    case WASM_INS_I64_LOAD8_S:
895
3.04k
    case WASM_INS_I64_LOAD8_U:
896
3.41k
    case WASM_INS_I64_LOAD16_S:
897
3.67k
    case WASM_INS_I64_LOAD16_U:
898
3.99k
    case WASM_INS_I64_LOAD32_S:
899
4.22k
    case WASM_INS_I64_LOAD32_U:
900
4.76k
    case WASM_INS_I32_STORE:
901
5.02k
    case WASM_INS_I64_STORE:
902
5.36k
    case WASM_INS_F32_STORE:
903
5.63k
    case WASM_INS_F64_STORE:
904
5.84k
    case WASM_INS_I32_STORE8:
905
6.06k
    case WASM_INS_I32_STORE16:
906
6.29k
    case WASM_INS_I64_STORE8:
907
6.55k
    case WASM_INS_I64_STORE16:
908
6.80k
    case WASM_INS_I64_STORE32:
909
6.80k
      if (code_len == 1 || !read_memoryimmediate(&code[1], code_len - 1, &param_size, MI)) {
910
183
        return false;
911
183
      }
912
913
6.62k
      if (MI->flat_insn->detail) {
914
6.62k
        MI->flat_insn->detail->wasm.op_count = 2;
915
6.62k
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_MEMORY;
916
6.62k
        MI->flat_insn->detail->groups_count++;
917
6.62k
      }
918
919
6.62k
      *size = param_size + 1;
920
921
6.62k
      break;
922
923
261
    case WASM_INS_CURRENT_MEMORY:
924
521
    case WASM_INS_GROW_MEMORY:
925
521
      MI->wasm_data.type = WASM_OP_NONE;
926
927
521
      if (MI->flat_insn->detail) {
928
521
        MI->flat_insn->detail->wasm.op_count = 0;
929
521
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_MEMORY;
930
521
        MI->flat_insn->detail->groups_count++;
931
521
      }
932
933
521
      *size = 1;
934
935
521
      break;
936
937
3.18k
    case WASM_INS_UNREACHABLE:
938
3.55k
    case WASM_INS_NOP:
939
3.80k
    case WASM_INS_ELSE:
940
4.03k
    case WASM_INS_END:
941
4.27k
    case WASM_INS_RETURN:
942
4.27k
      MI->wasm_data.type = WASM_OP_NONE;
943
944
4.27k
      if (MI->flat_insn->detail) {
945
4.27k
        MI->flat_insn->detail->wasm.op_count = 0;
946
4.27k
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_CONTROL;
947
4.27k
        MI->flat_insn->detail->groups_count++;
948
4.27k
      }
949
950
4.27k
      *size = 1;
951
952
4.27k
      break;
953
954
307
    case WASM_INS_BLOCK:
955
543
    case WASM_INS_LOOP:
956
782
    case WASM_INS_IF:
957
782
      if (code_len == 1 || !read_varint7(&code[1], code_len - 1, &param_size, MI)) {
958
26
        return false;
959
26
      }
960
961
756
      if (MI->flat_insn->detail) {
962
756
        MI->flat_insn->detail->wasm.op_count = 1;
963
756
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_CONTROL;
964
756
        MI->flat_insn->detail->groups_count++;
965
756
      }
966
967
756
      *size = param_size + 1;
968
969
756
      break;
970
971
363
    case WASM_INS_BR:
972
610
    case WASM_INS_BR_IF:
973
841
    case WASM_INS_CALL:
974
1.11k
    case WASM_INS_CALL_INDIRECT:
975
1.11k
      if (code_len == 1 || !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
976
31
        return false;
977
31
      }
978
979
1.08k
      if (MI->flat_insn->detail) {
980
1.08k
        MI->flat_insn->detail->wasm.op_count = 1;
981
1.08k
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_CONTROL;
982
1.08k
        MI->flat_insn->detail->groups_count++;
983
1.08k
      }
984
985
1.08k
      *size = param_size + 1;
986
987
1.08k
      break;
988
989
997
    case WASM_INS_BR_TABLE:
990
997
      if (code_len == 1 || !read_brtable(&code[1], code_len - 1, &param_size, MI)) {
991
102
        return false;
992
102
      }
993
994
895
      if (MI->flat_insn->detail) {
995
895
        MI->flat_insn->detail->wasm.op_count = 1;
996
895
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_CONTROL;
997
895
        MI->flat_insn->detail->groups_count++;
998
895
      }
999
1000
895
      *size = param_size + 1;
1001
1002
895
      break;
1003
59.7k
  }
1004
1005
59.3k
  return true;
1006
59.7k
}