Coverage Report

Created: 2025-12-31 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bloaty/third_party/capstone/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> // 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
0
{
277
0
  uint32_t data = 0;
278
0
  int i;
279
280
0
  for(i = 0;; i++) {
281
0
    if (code_len < i + 1) {
282
0
      *leng = -1;
283
0
      return 0;
284
0
    }
285
286
287
0
    if (i > 4 || (i == 4 && (code[i] & 0x7f) > 0x0f)) {
288
0
      *leng = -1;
289
0
      return 0;
290
0
    }
291
292
0
    data = data + (((uint32_t) code[i] & 0x7f) << (i * 7));
293
0
    if (code[i] >> 7 == 0) {
294
0
      break;
295
0
    }
296
0
  }
297
298
0
  *leng = i + 1;
299
300
0
  return data;
301
0
}
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
0
{
309
0
  uint64_t data;
310
0
  int i;
311
312
0
  data = 0;
313
0
  for(i = 0;; i++){
314
0
    if (code_len < i + 1) {
315
0
      *leng = -1;
316
0
      return 0;
317
0
    }
318
319
0
    if (i > 9 || (i == 9 && (code[i] & 0x7f) > 0x01)) {
320
0
      *leng = -1;
321
0
      return 0;
322
0
    }
323
324
0
    data = data + (((uint64_t) code[i] & 0x7f) << (i * 7));
325
0
    if (code[i] >> 7 == 0) {
326
0
      break;
327
0
    }
328
0
  }
329
330
0
  *leng = i + 1;
331
332
0
  return data;
333
0
}
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
0
{
340
0
  memcpy(dest, code, 4);
341
0
}
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
0
{
348
0
  memcpy(dest, code, 8);
349
0
}
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
0
{
357
0
  int8_t data;
358
359
0
  if (code_len < 1) {
360
0
    *leng = -1;
361
0
    return -1;
362
0
  }
363
364
0
  *leng = 1;
365
366
0
  if (code[0] == 0x40) {
367
0
    return -1;
368
0
  }
369
370
0
  data = code[0] & 0x7f;
371
372
0
  return data;
373
0
}
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
0
{
382
0
  size_t len = 0;
383
0
  uint32_t data;
384
385
0
  data = get_varuint32(code, code_len, &len);
386
0
  if (len == -1) {
387
0
    return false;
388
0
  }
389
390
0
  if (MI->flat_insn->detail) {
391
0
    MI->flat_insn->detail->wasm.op_count = 1;
392
0
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_VARUINT32;
393
0
    MI->flat_insn->detail->wasm.operands[0].size= len;
394
0
    MI->flat_insn->detail->wasm.operands[0].varuint32= data;
395
0
  }
396
397
0
  MI->wasm_data.size = len;
398
0
  MI->wasm_data.type = WASM_OP_VARUINT32;
399
0
  MI->wasm_data.uint32 = data;
400
0
  *param_size = len;
401
402
0
  return true;
403
0
}
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
0
{
412
0
  size_t len = 0;
413
0
  uint64_t data;
414
415
0
  data = get_varuint64(code, code_len, &len);
416
0
  if (len == -1) {
417
0
    return false;
418
0
  }
419
420
0
  if (MI->flat_insn->detail) {
421
0
    MI->flat_insn->detail->wasm.op_count = 1;
422
0
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_VARUINT64;
423
0
    MI->flat_insn->detail->wasm.operands[0].size = len;
424
0
    MI->flat_insn->detail->wasm.operands[0].varuint64 = data;
425
0
  }
426
427
0
  MI->wasm_data.size = len;
428
0
  MI->wasm_data.type = WASM_OP_VARUINT64;
429
0
  MI->wasm_data.uint64 = data;
430
0
  *param_size = len;
431
432
0
  return true;
433
0
}
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
0
{
442
0
  size_t tmp, len = 0;
443
0
  uint32_t data[2];
444
445
0
  if (MI->flat_insn->detail) {
446
0
    MI->flat_insn->detail->wasm.op_count = 2;
447
0
  }
448
449
0
  data[0] = get_varuint32(code, code_len, &tmp);
450
0
  if (tmp == -1) {
451
0
    return false;
452
0
  }
453
454
0
  if (MI->flat_insn->detail) {
455
0
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_VARUINT32;
456
0
    MI->flat_insn->detail->wasm.operands[0].size = tmp;
457
0
    MI->flat_insn->detail->wasm.operands[0].varuint32 = data[0];
458
0
  }
459
460
0
  len = tmp;
461
0
  data[1] = get_varuint32(&code[len], code_len - len, &tmp);
462
0
  if (len == -1) {
463
0
    return false;
464
0
  }
465
466
0
  if (MI->flat_insn->detail) {
467
0
    MI->flat_insn->detail->wasm.operands[1].type = WASM_OP_VARUINT32;
468
0
    MI->flat_insn->detail->wasm.operands[1].size = tmp;
469
0
    MI->flat_insn->detail->wasm.operands[1].varuint32 = data[1];
470
0
  }
471
472
0
  len += tmp;
473
0
  MI->wasm_data.size = len;
474
0
  MI->wasm_data.type = WASM_OP_IMM;
475
0
  MI->wasm_data.immediate[0] = data[0];
476
0
  MI->wasm_data.immediate[1] = data[1];
477
0
  *param_size = len;
478
479
0
  return true;
480
0
}
481
482
// input  | code : code pointer start from uint32
483
//      | code_len : start from the code pointer to the end, how long is it
484
//      | param_size : pointer of the param size
485
//      | MI : Mcinst handler in this round of disasm
486
// return   | true/false if the function successfully finished 
487
static bool read_uint32(const uint8_t *code, size_t code_len, uint16_t *param_size, MCInst *MI)
488
0
{
489
0
  if (code_len < 4) {
490
0
    return false;
491
0
  }
492
493
0
  get_uint32(code, &(MI->wasm_data.uint32));
494
495
0
  if (MI->flat_insn->detail) {
496
0
    MI->flat_insn->detail->wasm.op_count = 1;
497
0
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_UINT32;
498
0
    MI->flat_insn->detail->wasm.operands[0].size = 4;
499
0
    get_uint32(code, &(MI->flat_insn->detail->wasm.operands[0].uint32));
500
0
  }
501
502
0
  MI->wasm_data.size = 4;
503
0
  MI->wasm_data.type = WASM_OP_UINT32;
504
0
  *param_size = 4;
505
506
0
  return true;
507
0
}
508
509
// input  | code : code pointer start from uint64
510
//      | code_len : start from the code pointer to the end, how long is it
511
//      | param_size : pointer of the param size
512
//      | MI : Mcinst handler in this round of disasm
513
// return   | true/false if the function successfully finished 
514
static bool read_uint64(const uint8_t *code, size_t code_len, uint16_t *param_size, MCInst *MI)
515
0
{
516
0
  if (code_len < 8) {
517
0
    return false;
518
0
  }
519
520
0
  get_uint64(code, &(MI->wasm_data.uint64));
521
522
0
  if (MI->flat_insn->detail) {
523
0
    MI->flat_insn->detail->wasm.op_count = 1;
524
0
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_UINT64;
525
0
    MI->flat_insn->detail->wasm.operands[0].size = 8;
526
0
    get_uint64(code, &(MI->flat_insn->detail->wasm.operands[0].uint64));
527
0
  }
528
529
0
  MI->wasm_data.size = 8;
530
0
  MI->wasm_data.type = WASM_OP_UINT64;
531
0
  *param_size = 8;
532
533
0
  return true;
534
0
}
535
536
// input  | code : code pointer start from brtable
537
//      | code_len : start from the code pointer to the end, how long is it
538
//      | param_size : pointer of the param size (sum of all param)
539
//      | MI : Mcinst handler in this round of disasm
540
// return   | true/false if the function successfully finished 
541
static bool read_brtable(const uint8_t *code, size_t code_len, uint16_t *param_size, MCInst *MI)
542
0
{
543
0
  uint32_t length, default_target;
544
0
  int tmp_len = 0, i;
545
0
  size_t var_len;
546
547
  // read length
548
0
  length = get_varuint32(code, code_len, &var_len);
549
0
  if (var_len == -1) {
550
0
    return false;
551
0
  }
552
553
0
  tmp_len += var_len;
554
0
  MI->wasm_data.brtable.length = length;
555
0
  if (length >= UINT32_MAX - tmp_len) {
556
    // integer overflow check
557
0
    return false;
558
0
  }
559
0
  if (code_len < tmp_len + length) {
560
    // safety check that we have minimum enough data to read
561
0
    return false;
562
0
  }
563
  // base address + 1 byte opcode + tmp_len for number of cases = start of targets
564
0
  MI->wasm_data.brtable.address = MI->address + 1 + tmp_len;
565
566
0
  if (MI->flat_insn->detail) {
567
0
    MI->flat_insn->detail->wasm.op_count = 1;
568
0
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_BRTABLE;
569
0
    MI->flat_insn->detail->wasm.operands[0].brtable.length = MI->wasm_data.brtable.length;
570
0
    MI->flat_insn->detail->wasm.operands[0].brtable.address = MI->wasm_data.brtable.address;
571
0
  }
572
573
  // read data
574
0
  for(i = 0; i < length; i++){
575
0
    if (code_len < tmp_len) {
576
0
      return false;
577
0
    }
578
579
0
    get_varuint32(code + tmp_len, code_len - tmp_len, &var_len);
580
0
    if (var_len == -1) {
581
0
      return false;
582
0
    }
583
584
0
    tmp_len += var_len;
585
0
  }
586
587
  // read default target
588
0
  default_target = get_varuint32(code + tmp_len, code_len - tmp_len, &var_len);
589
0
  if (var_len == -1) {
590
0
    return false;
591
0
  }
592
593
0
  MI->wasm_data.brtable.default_target = default_target;
594
0
  MI->wasm_data.type = WASM_OP_BRTABLE;
595
0
  *param_size = tmp_len + var_len;
596
597
0
  if (MI->flat_insn->detail) {
598
0
    MI->flat_insn->detail->wasm.operands[0].size = *param_size;
599
0
    MI->flat_insn->detail->wasm.operands[0].brtable.default_target = MI->wasm_data.brtable.default_target;
600
0
  }
601
602
0
  return true;
603
0
}
604
605
// input  | code : code pointer start from varint7
606
//      | code_len : start from the code pointer to the end, how long is it
607
//      | param_size : pointer of the param size
608
//      | MI : Mcinst handler in this round of disasm
609
// return   | true/false if the function successfully finished 
610
static bool read_varint7(const uint8_t *code, size_t code_len, uint16_t *param_size, MCInst *MI)
611
0
{
612
0
  size_t len = 0;
613
614
0
  MI->wasm_data.type = WASM_OP_INT7;
615
0
  MI->wasm_data.int7 = get_varint7(code, code_len, &len);
616
0
  if (len == -1) {
617
0
    return false;
618
0
  }
619
620
0
  if (MI->flat_insn->detail) {
621
0
    MI->flat_insn->detail->wasm.op_count = 1;
622
0
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_INT7;
623
0
    MI->flat_insn->detail->wasm.operands[0].size = 1;
624
0
    MI->flat_insn->detail->wasm.operands[0].int7 = MI->wasm_data.int7;
625
0
  }
626
627
0
  *param_size = len;
628
629
0
  return true;
630
0
}
631
632
bool WASM_getInstruction(csh ud, const uint8_t *code, size_t code_len,
633
    MCInst *MI, uint16_t *size, uint64_t address, void *inst_info)
634
0
{
635
0
  unsigned char opcode;
636
0
  uint16_t param_size;
637
638
0
  if (code_len == 0)
639
0
    return false;
640
641
0
  opcode = code[0];
642
0
  if (opcodes[opcode] == -1) {
643
    // invalid opcode
644
0
    return false;
645
0
  }
646
647
  // valid opcode
648
0
  MI->address = address;
649
0
  MI->OpcodePub = MI->Opcode = opcode;
650
651
0
  if (MI->flat_insn->detail) {
652
0
    memset(MI->flat_insn->detail, 0, offsetof(cs_detail, wasm)+sizeof(cs_wasm));
653
0
    WASM_get_insn_id((cs_struct *)ud, MI->flat_insn, opcode);
654
0
  }
655
656
  // setup groups
657
0
  switch(opcode) {
658
0
    default:
659
0
      return false;
660
661
0
    case WASM_INS_I32_CONST:
662
0
      if (code_len == 1 || !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
663
0
        return false;
664
0
      }
665
666
0
      if (MI->flat_insn->detail) {
667
0
        MI->flat_insn->detail->wasm.op_count = 1;
668
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_NUMBERIC;
669
0
        MI->flat_insn->detail->groups_count++;
670
0
      }
671
672
0
      *size = param_size + 1;
673
674
0
      break;
675
676
0
    case WASM_INS_I64_CONST:
677
0
      if (code_len == 1 || !read_varuint64(&code[1], code_len - 1, &param_size, MI)) {
678
0
        return false;
679
0
      }
680
681
0
      if (MI->flat_insn->detail) {
682
0
        MI->flat_insn->detail->wasm.op_count = 1;
683
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_NUMBERIC;
684
0
        MI->flat_insn->detail->groups_count++;
685
0
      }
686
687
0
      *size = param_size + 1;
688
689
0
      break;
690
691
0
    case WASM_INS_F32_CONST:
692
0
      if (code_len == 1 || !read_uint32(&code[1], code_len - 1, &param_size, MI)) {
693
0
        return false;
694
0
      }
695
696
0
      if (MI->flat_insn->detail) {
697
0
        MI->flat_insn->detail->wasm.op_count = 1;
698
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_NUMBERIC;
699
0
        MI->flat_insn->detail->groups_count++;
700
0
      }
701
702
0
      *size = param_size + 1;
703
704
0
      break;
705
706
0
    case WASM_INS_F64_CONST:
707
0
      if (code_len == 1 || !read_uint64(&code[1], code_len - 1, &param_size, MI)) {
708
0
        return false;
709
0
      }
710
711
0
      if (MI->flat_insn->detail) {
712
0
        MI->flat_insn->detail->wasm.op_count = 1;
713
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_NUMBERIC;
714
0
        MI->flat_insn->detail->groups_count++;
715
0
      }
716
717
0
      *size = param_size + 1;
718
719
0
      break;
720
721
0
    case WASM_INS_I32_EQZ:
722
0
    case WASM_INS_I32_EQ:
723
0
    case WASM_INS_I32_NE:
724
0
    case WASM_INS_I32_LT_S:
725
0
    case WASM_INS_I32_LT_U:
726
0
    case WASM_INS_I32_GT_S:
727
0
    case WASM_INS_I32_GT_U:
728
0
    case WASM_INS_I32_LE_S:
729
0
    case WASM_INS_I32_LE_U:
730
0
    case WASM_INS_I32_GE_S:
731
0
    case WASM_INS_I32_GE_U:
732
0
    case WASM_INS_I64_EQZ:
733
0
    case WASM_INS_I64_EQ:
734
0
    case WASM_INS_I64_NE:
735
0
    case WASM_INS_I64_LT_S:
736
0
    case WASM_INS_I64_LT_U:
737
0
    case WASN_INS_I64_GT_S:
738
0
    case WASM_INS_I64_GT_U:
739
0
    case WASM_INS_I64_LE_S:
740
0
    case WASM_INS_I64_LE_U:
741
0
    case WASM_INS_I64_GE_S:
742
0
    case WASM_INS_I64_GE_U:
743
0
    case WASM_INS_F32_EQ:
744
0
    case WASM_INS_F32_NE:
745
0
    case WASM_INS_F32_LT:
746
0
    case WASM_INS_F32_GT:
747
0
    case WASM_INS_F32_LE:
748
0
    case WASM_INS_F32_GE:
749
0
    case WASM_INS_F64_EQ:
750
0
    case WASM_INS_F64_NE:
751
0
    case WASM_INS_F64_LT:
752
0
    case WASM_INS_F64_GT:
753
0
    case WASM_INS_F64_LE:
754
0
    case WASM_INS_F64_GE:
755
0
    case WASM_INS_I32_CLZ:
756
0
    case WASM_INS_I32_CTZ:
757
0
    case WASM_INS_I32_POPCNT:
758
0
    case WASM_INS_I32_ADD:
759
0
    case WASM_INS_I32_SUB:
760
0
    case WASM_INS_I32_MUL:
761
0
    case WASM_INS_I32_DIV_S:
762
0
    case WASM_INS_I32_DIV_U:
763
0
    case WASM_INS_I32_REM_S:
764
0
    case WASM_INS_I32_REM_U:
765
0
    case WASM_INS_I32_AND:
766
0
    case WASM_INS_I32_OR:
767
0
    case WASM_INS_I32_XOR:
768
0
    case WASM_INS_I32_SHL:
769
0
    case WASM_INS_I32_SHR_S:
770
0
    case WASM_INS_I32_SHR_U:
771
0
    case WASM_INS_I32_ROTL:
772
0
    case WASM_INS_I32_ROTR:
773
0
    case WASM_INS_I64_CLZ:
774
0
    case WASM_INS_I64_CTZ:
775
0
    case WASM_INS_I64_POPCNT:
776
0
    case WASM_INS_I64_ADD:
777
0
    case WASM_INS_I64_SUB:
778
0
    case WASM_INS_I64_MUL:
779
0
    case WASM_INS_I64_DIV_S:
780
0
    case WASM_INS_I64_DIV_U:
781
0
    case WASM_INS_I64_REM_S:
782
0
    case WASM_INS_I64_REM_U:
783
0
    case WASM_INS_I64_AND:
784
0
    case WASM_INS_I64_OR:
785
0
    case WASM_INS_I64_XOR:
786
0
    case WASM_INS_I64_SHL:
787
0
    case WASM_INS_I64_SHR_S:
788
0
    case WASM_INS_I64_SHR_U:
789
0
    case WASM_INS_I64_ROTL:
790
0
    case WASM_INS_I64_ROTR:
791
0
    case WASM_INS_F32_ABS:
792
0
    case WASM_INS_F32_NEG:
793
0
    case WASM_INS_F32_CEIL:
794
0
    case WASM_INS_F32_FLOOR:
795
0
    case WASM_INS_F32_TRUNC:
796
0
    case WASM_INS_F32_NEAREST:
797
0
    case WASM_INS_F32_SQRT:
798
0
    case WASM_INS_F32_ADD:
799
0
    case WASM_INS_F32_SUB:
800
0
    case WASM_INS_F32_MUL:
801
0
    case WASM_INS_F32_DIV:
802
0
    case WASM_INS_F32_MIN:
803
0
    case WASM_INS_F32_MAX:
804
0
    case WASM_INS_F32_COPYSIGN:
805
0
    case WASM_INS_F64_ABS:
806
0
    case WASM_INS_F64_NEG:
807
0
    case WASM_INS_F64_CEIL:
808
0
    case WASM_INS_F64_FLOOR:
809
0
    case WASM_INS_F64_TRUNC:
810
0
    case WASM_INS_F64_NEAREST:
811
0
    case WASM_INS_F64_SQRT:
812
0
    case WASM_INS_F64_ADD:
813
0
    case WASM_INS_F64_SUB:
814
0
    case WASM_INS_F64_MUL:
815
0
    case WASM_INS_F64_DIV:
816
0
    case WASM_INS_F64_MIN:
817
0
    case WASM_INS_F64_MAX:
818
0
    case WASM_INS_F64_COPYSIGN:
819
0
    case WASM_INS_I32_WARP_I64:
820
0
    case WASP_INS_I32_TRUNC_S_F32:
821
0
    case WASM_INS_I32_TRUNC_U_F32:
822
0
    case WASM_INS_I32_TRUNC_S_F64:
823
0
    case WASM_INS_I32_TRUNC_U_F64:
824
0
    case WASM_INS_I64_EXTEND_S_I32:
825
0
    case WASM_INS_I64_EXTEND_U_I32:
826
0
    case WASM_INS_I64_TRUNC_S_F32:
827
0
    case WASM_INS_I64_TRUNC_U_F32:
828
0
    case WASM_INS_I64_TRUNC_S_F64:
829
0
    case WASM_INS_I64_TRUNC_U_F64:
830
0
    case WASM_INS_F32_CONVERT_S_I32:
831
0
    case WASM_INS_F32_CONVERT_U_I32:
832
0
    case WASM_INS_F32_CONVERT_S_I64:
833
0
    case WASM_INS_F32_CONVERT_U_I64:
834
0
    case WASM_INS_F32_DEMOTE_F64:
835
0
    case WASM_INS_F64_CONVERT_S_I32:
836
0
    case WASM_INS_F64_CONVERT_U_I32:
837
0
    case WASM_INS_F64_CONVERT_S_I64:
838
0
    case WASM_INS_F64_CONVERT_U_I64:
839
0
    case WASM_INS_F64_PROMOTE_F32:
840
0
    case WASM_INS_I32_REINTERPRET_F32:
841
0
    case WASM_INS_I64_REINTERPRET_F64:
842
0
    case WASM_INS_F32_REINTERPRET_I32:
843
0
    case WASM_INS_F64_REINTERPRET_I64:
844
0
      MI->wasm_data.type = WASM_OP_NONE;
845
846
0
      if (MI->flat_insn->detail) {
847
0
        MI->flat_insn->detail->wasm.op_count = 0;
848
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_NUMBERIC;
849
0
        MI->flat_insn->detail->groups_count++;
850
0
      }
851
852
0
      *size = 1;
853
854
0
      break;
855
856
0
    case WASM_INS_DROP:
857
0
    case WASM_INS_SELECT:
858
0
      MI->wasm_data.type = WASM_OP_NONE;
859
860
0
      if (MI->flat_insn->detail) {
861
0
        MI->flat_insn->detail->wasm.op_count = 0;
862
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_PARAMETRIC;
863
0
        MI->flat_insn->detail->groups_count++;
864
0
      }
865
866
0
      *size = 1;
867
868
0
      break;
869
870
0
    case WASM_INS_GET_LOCAL:
871
0
    case WASM_INS_SET_LOCAL:
872
0
    case WASM_INS_TEE_LOCAL:
873
0
    case WASM_INS_GET_GLOBAL:
874
0
    case WASM_INS_SET_GLOBAL:
875
0
      if (code_len == 1 || !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
876
0
        return false;
877
0
      }
878
879
0
      if (MI->flat_insn->detail) {
880
0
        MI->flat_insn->detail->wasm.op_count = 1;
881
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_VARIABLE;
882
0
        MI->flat_insn->detail->groups_count++;
883
0
      }
884
885
0
      *size = param_size + 1;
886
887
0
      break;
888
889
0
    case WASM_INS_I32_LOAD:
890
0
    case WASM_INS_I64_LOAD:
891
0
    case WASM_INS_F32_LOAD:
892
0
    case WASM_INS_F64_LOAD:
893
0
    case WASM_INS_I32_LOAD8_S:
894
0
    case WASM_INS_I32_LOAD8_U:
895
0
    case WASM_INS_I32_LOAD16_S:
896
0
    case WASM_INS_I32_LOAD16_U:
897
0
    case WASM_INS_I64_LOAD8_S:
898
0
    case WASM_INS_I64_LOAD8_U:
899
0
    case WASM_INS_I64_LOAD16_S:
900
0
    case WASM_INS_I64_LOAD16_U:
901
0
    case WASM_INS_I64_LOAD32_S:
902
0
    case WASM_INS_I64_LOAD32_U:
903
0
    case WASM_INS_I32_STORE:
904
0
    case WASM_INS_I64_STORE:
905
0
    case WASM_INS_F32_STORE:
906
0
    case WASM_INS_F64_STORE:
907
0
    case WASM_INS_I32_STORE8:
908
0
    case WASM_INS_I32_STORE16:
909
0
    case WASM_INS_I64_STORE8:
910
0
    case WASM_INS_I64_STORE16:
911
0
    case WASM_INS_I64_STORE32:
912
0
      if (code_len == 1 || !read_memoryimmediate(&code[1], code_len - 1, &param_size, MI)) {
913
0
        return false;
914
0
      }
915
916
0
      if (MI->flat_insn->detail) {
917
0
        MI->flat_insn->detail->wasm.op_count = 2;
918
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_MEMORY;
919
0
        MI->flat_insn->detail->groups_count++;
920
0
      }
921
922
0
      *size = param_size + 1;
923
924
0
      break;
925
926
0
    case WASM_INS_CURRENT_MEMORY:
927
0
    case WASM_INS_GROW_MEMORY:
928
0
      MI->wasm_data.type = WASM_OP_NONE;
929
930
0
      if (MI->flat_insn->detail) {
931
0
        MI->flat_insn->detail->wasm.op_count = 0;
932
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_MEMORY;
933
0
        MI->flat_insn->detail->groups_count++;
934
0
      }
935
936
0
      *size = 1;
937
938
0
      break;
939
940
0
    case WASM_INS_UNREACHABLE:
941
0
    case WASM_INS_NOP:
942
0
    case WASM_INS_ELSE:
943
0
    case WASM_INS_END:
944
0
    case WASM_INS_RETURN:
945
0
      MI->wasm_data.type = WASM_OP_NONE;
946
947
0
      if (MI->flat_insn->detail) {
948
0
        MI->flat_insn->detail->wasm.op_count = 0;
949
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_CONTROL;
950
0
        MI->flat_insn->detail->groups_count++;
951
0
      }
952
953
0
      *size = 1;
954
955
0
      break;
956
957
0
    case WASM_INS_BLOCK:
958
0
    case WASM_INS_LOOP:
959
0
    case WASM_INS_IF:
960
0
      if (code_len == 1 || !read_varint7(&code[1], code_len - 1, &param_size, MI)) {
961
0
        return false;
962
0
      }
963
964
0
      if (MI->flat_insn->detail) {
965
0
        MI->flat_insn->detail->wasm.op_count = 1;
966
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_CONTROL;
967
0
        MI->flat_insn->detail->groups_count++;
968
0
      }
969
970
0
      *size = param_size + 1;
971
972
0
      break;
973
974
0
    case WASM_INS_BR:
975
0
    case WASM_INS_BR_IF:
976
0
    case WASM_INS_CALL:
977
0
    case WASM_INS_CALL_INDIRECT:
978
0
      if (code_len == 1 || !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
979
0
        return false;
980
0
      }
981
982
0
      if (MI->flat_insn->detail) {
983
0
        MI->flat_insn->detail->wasm.op_count = 1;
984
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_CONTROL;
985
0
        MI->flat_insn->detail->groups_count++;
986
0
      }
987
988
0
      *size = param_size + 1;
989
990
0
      break;
991
992
0
    case WASM_INS_BR_TABLE:
993
0
      if (code_len == 1 || !read_brtable(&code[1], code_len - 1, &param_size, MI)) {
994
0
        return false;
995
0
      }
996
997
0
      if (MI->flat_insn->detail) {
998
0
        MI->flat_insn->detail->wasm.op_count = 1;
999
0
        MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = WASM_GRP_CONTROL;
1000
0
        MI->flat_insn->detail->groups_count++;
1001
0
      }
1002
1003
0
      *size = param_size + 1;
1004
1005
0
      break;
1006
0
  }
1007
1008
0
  return true;
1009
0
}