Coverage Report

Created: 2026-09-28 06:34

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
26.8k
  for (i = 0;; i++) {
281
26.8k
    if (code_len < i + 1) {
282
92
      *leng = -1;
283
92
      return 0;
284
92
    }
285
286
26.7k
    if (i > 4 || (i == 4 && (code[i] & 0x7f) > 0x0f)) {
287
34
      *leng = -1;
288
34
      return 0;
289
34
    }
290
291
26.7k
    data = data + (((uint32_t)code[i] & 0x7f) << (i * 7));
292
26.7k
    if (code[i] >> 7 == 0) {
293
19.4k
      break;
294
19.4k
    }
295
26.7k
  }
296
297
19.4k
  *leng = i + 1;
298
299
19.4k
  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
392
{
309
392
  uint64_t data;
310
392
  int i;
311
312
392
  data = 0;
313
1.10k
  for (i = 0;; i++) {
314
1.10k
    if (code_len < i + 1) {
315
3
      *leng = -1;
316
3
      return 0;
317
3
    }
318
319
1.10k
    if (i > 9 || (i == 9 && (code[i] & 0x7f) > 0x01)) {
320
1
      *leng = -1;
321
1
      return 0;
322
1
    }
323
324
1.10k
    data = data + (((uint64_t)code[i] & 0x7f) << (i * 7));
325
1.10k
    if (code[i] >> 7 == 0) {
326
388
      break;
327
388
    }
328
1.10k
  }
329
330
388
  *leng = i + 1;
331
332
388
  return data;
333
392
}
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
340
{
340
340
  memcpy(dest, code, 4);
341
340
}
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
180
{
348
180
  memcpy(dest, code, 8);
349
180
}
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
495
{
357
495
  int8_t data;
358
359
495
  if (code_len < 1) {
360
0
    *leng = -1;
361
0
    return -1;
362
0
  }
363
364
495
  *leng = 1;
365
366
495
  if (code[0] == 0x40) {
367
18
    return -1;
368
18
  }
369
370
477
  data = code[0] & 0x7f;
371
372
477
  return data;
373
495
}
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.77k
{
383
2.77k
  size_t len = 0;
384
2.77k
  uint32_t data;
385
386
2.77k
  data = get_varuint32(code, code_len, &len);
387
2.77k
  if (len == -1) {
388
14
    return false;
389
14
  }
390
391
2.76k
  if (MI->flat_insn->detail) {
392
2.76k
    MI->flat_insn->detail->wasm.op_count = 1;
393
2.76k
    MI->flat_insn->detail->wasm.operands[0].type =
394
2.76k
      WASM_OP_VARUINT32;
395
2.76k
    MI->flat_insn->detail->wasm.operands[0].size = len;
396
2.76k
    MI->flat_insn->detail->wasm.operands[0].varuint32 = data;
397
2.76k
  }
398
399
2.76k
  MI->wasm_data.size = len;
400
2.76k
  MI->wasm_data.type = WASM_OP_VARUINT32;
401
2.76k
  MI->wasm_data.uint32 = data;
402
2.76k
  *param_size = len;
403
404
2.76k
  return true;
405
2.77k
}
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
392
{
415
392
  size_t len = 0;
416
392
  uint64_t data;
417
418
392
  data = get_varuint64(code, code_len, &len);
419
392
  if (len == -1) {
420
4
    return false;
421
4
  }
422
423
388
  if (MI->flat_insn->detail) {
424
388
    MI->flat_insn->detail->wasm.op_count = 1;
425
388
    MI->flat_insn->detail->wasm.operands[0].type =
426
388
      WASM_OP_VARUINT64;
427
388
    MI->flat_insn->detail->wasm.operands[0].size = len;
428
388
    MI->flat_insn->detail->wasm.operands[0].varuint64 = data;
429
388
  }
430
431
388
  MI->wasm_data.size = len;
432
388
  MI->wasm_data.type = WASM_OP_VARUINT64;
433
388
  MI->wasm_data.uint64 = data;
434
388
  *param_size = len;
435
436
388
  return true;
437
392
}
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
4.80k
{
447
4.80k
  size_t tmp, len = 0;
448
4.80k
  uint32_t data[2];
449
450
4.80k
  if (MI->flat_insn->detail) {
451
4.80k
    MI->flat_insn->detail->wasm.op_count = 2;
452
4.80k
  }
453
454
4.80k
  data[0] = get_varuint32(code, code_len, &tmp);
455
4.80k
  if (tmp == -1) {
456
27
    return false;
457
27
  }
458
459
4.77k
  if (MI->flat_insn->detail) {
460
4.77k
    MI->flat_insn->detail->wasm.operands[0].type =
461
4.77k
      WASM_OP_VARUINT32;
462
4.77k
    MI->flat_insn->detail->wasm.operands[0].size = tmp;
463
4.77k
    MI->flat_insn->detail->wasm.operands[0].varuint32 = data[0];
464
4.77k
  }
465
466
4.77k
  len = tmp;
467
4.77k
  data[1] = get_varuint32(&code[len], code_len - len, &tmp);
468
4.77k
  if (tmp == -1) {
469
60
    return false;
470
60
  }
471
472
4.71k
  if (MI->flat_insn->detail) {
473
4.71k
    MI->flat_insn->detail->wasm.operands[1].type =
474
4.71k
      WASM_OP_VARUINT32;
475
4.71k
    MI->flat_insn->detail->wasm.operands[1].size = tmp;
476
4.71k
    MI->flat_insn->detail->wasm.operands[1].varuint32 = data[1];
477
4.71k
  }
478
479
4.71k
  len += tmp;
480
4.71k
  MI->wasm_data.size = len;
481
4.71k
  MI->wasm_data.type = WASM_OP_IMM;
482
4.71k
  MI->wasm_data.immediate[0] = data[0];
483
4.71k
  MI->wasm_data.immediate[1] = data[1];
484
4.71k
  *param_size = len;
485
486
4.71k
  return true;
487
4.77k
}
488
489
// input  | code : code pointer start from uint32
490
//      | code_len : start from the code pointer to the end, how long is it
491
//      | param_size : pointer of the param size
492
//      | MI : Mcinst handler in this round of disasm
493
// return   | true/false if the function successfully finished
494
static bool read_uint32(const uint8_t *code, size_t code_len,
495
      uint16_t *param_size, MCInst *MI)
496
175
{
497
175
  if (code_len < 4) {
498
5
    return false;
499
5
  }
500
501
170
  get_uint32(code, &(MI->wasm_data.uint32));
502
503
170
  if (MI->flat_insn->detail) {
504
170
    MI->flat_insn->detail->wasm.op_count = 1;
505
170
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_UINT32;
506
170
    MI->flat_insn->detail->wasm.operands[0].size = 4;
507
170
    get_uint32(code,
508
170
         &(MI->flat_insn->detail->wasm.operands[0].uint32));
509
170
  }
510
511
170
  MI->wasm_data.size = 4;
512
170
  MI->wasm_data.type = WASM_OP_UINT32;
513
170
  *param_size = 4;
514
515
170
  return true;
516
175
}
517
518
// input  | code : code pointer start from uint64
519
//      | code_len : start from the code pointer to the end, how long is it
520
//      | param_size : pointer of the param size
521
//      | MI : Mcinst handler in this round of disasm
522
// return   | true/false if the function successfully finished
523
static bool read_uint64(const uint8_t *code, size_t code_len,
524
      uint16_t *param_size, MCInst *MI)
525
96
{
526
96
  if (code_len < 8) {
527
6
    return false;
528
6
  }
529
530
90
  get_uint64(code, &(MI->wasm_data.uint64));
531
532
90
  if (MI->flat_insn->detail) {
533
90
    MI->flat_insn->detail->wasm.op_count = 1;
534
90
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_UINT64;
535
90
    MI->flat_insn->detail->wasm.operands[0].size = 8;
536
90
    get_uint64(code,
537
90
         &(MI->flat_insn->detail->wasm.operands[0].uint64));
538
90
  }
539
540
90
  MI->wasm_data.size = 8;
541
90
  MI->wasm_data.type = WASM_OP_UINT64;
542
90
  *param_size = 8;
543
544
90
  return true;
545
96
}
546
547
// input  | code : code pointer start from brtable
548
//      | code_len : start from the code pointer to the end, how long is it
549
//      | param_size : pointer of the param size (sum of all param)
550
//      | MI : Mcinst handler in this round of disasm
551
// return   | true/false if the function successfully finished
552
static bool read_brtable(const uint8_t *code, size_t code_len,
553
       uint16_t *param_size, MCInst *MI)
554
959
{
555
959
  uint32_t length, default_target;
556
959
  int tmp_len = 0, i;
557
959
  size_t var_len;
558
559
  // read length
560
959
  length = get_varuint32(code, code_len, &var_len);
561
959
  if (var_len == -1) {
562
5
    return false;
563
5
  }
564
565
954
  tmp_len += var_len;
566
954
  MI->wasm_data.brtable.length = length;
567
954
  if (length >= UINT32_MAX - tmp_len) {
568
    // integer overflow check
569
0
    return false;
570
0
  }
571
954
  if (code_len < tmp_len + length) {
572
    // safety check that we have minimum enough data to read
573
24
    return false;
574
24
  }
575
  // base address + 1 byte opcode + tmp_len for number of cases = start of targets
576
930
  MI->wasm_data.brtable.address = MI->address + 1 + tmp_len;
577
578
930
  if (MI->flat_insn->detail) {
579
930
    MI->flat_insn->detail->wasm.op_count = 1;
580
930
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_BRTABLE;
581
930
    MI->flat_insn->detail->wasm.operands[0].brtable.length =
582
930
      MI->wasm_data.brtable.length;
583
930
    MI->flat_insn->detail->wasm.operands[0].brtable.address =
584
930
      MI->wasm_data.brtable.address;
585
930
  }
586
587
  // read data
588
6.24k
  for (i = 0; i < length; i++) {
589
5.32k
    if (code_len < tmp_len) {
590
0
      return false;
591
0
    }
592
593
5.32k
    get_varuint32(code + tmp_len, code_len - tmp_len, &var_len);
594
5.32k
    if (var_len == -1) {
595
11
      return false;
596
11
    }
597
598
5.31k
    tmp_len += var_len;
599
5.31k
  }
600
601
  // read default target
602
919
  default_target =
603
919
    get_varuint32(code + tmp_len, code_len - tmp_len, &var_len);
604
919
  if (var_len == -1) {
605
9
    return false;
606
9
  }
607
608
910
  MI->wasm_data.brtable.default_target = default_target;
609
910
  MI->wasm_data.type = WASM_OP_BRTABLE;
610
910
  if ((uint64_t)(tmp_len + var_len) >= UINT16_MAX) {
611
0
    return false;
612
0
  }
613
910
  *param_size = tmp_len + var_len;
614
615
910
  if (MI->flat_insn->detail) {
616
910
    MI->flat_insn->detail->wasm.operands[0].size = *param_size;
617
910
    MI->flat_insn->detail->wasm.operands[0].brtable.default_target =
618
910
      MI->wasm_data.brtable.default_target;
619
910
  }
620
621
910
  return true;
622
910
}
623
624
// input  | code : code pointer start from varint7
625
//      | code_len : start from the code pointer to the end, how long is it
626
//      | param_size : pointer of the param size
627
//      | MI : Mcinst handler in this round of disasm
628
// return   | true/false if the function successfully finished
629
static bool read_varint7(const uint8_t *code, size_t code_len,
630
       uint16_t *param_size, MCInst *MI)
631
495
{
632
495
  size_t len = 0;
633
634
495
  MI->wasm_data.type = WASM_OP_INT7;
635
495
  MI->wasm_data.int7 = get_varint7(code, code_len, &len);
636
495
  if (len == -1) {
637
0
    return false;
638
0
  }
639
640
495
  if (MI->flat_insn->detail) {
641
495
    MI->flat_insn->detail->wasm.op_count = 1;
642
495
    MI->flat_insn->detail->wasm.operands[0].type = WASM_OP_INT7;
643
495
    MI->flat_insn->detail->wasm.operands[0].size = 1;
644
495
    MI->flat_insn->detail->wasm.operands[0].int7 =
645
495
      MI->wasm_data.int7;
646
495
  }
647
648
495
  *param_size = len;
649
650
495
  return true;
651
495
}
652
653
bool WASM_getInstruction(csh ud, const uint8_t *code, size_t code_len,
654
       MCInst *MI, uint16_t *size, uint64_t address,
655
       void *inst_info)
656
53.3k
{
657
53.3k
  unsigned char opcode;
658
53.3k
  uint16_t param_size;
659
660
53.3k
  if (code_len == 0)
661
0
    return false;
662
663
53.3k
  opcode = code[0];
664
53.3k
  if (opcodes[opcode] == -1) {
665
    // invalid opcode
666
228
    return false;
667
228
  }
668
669
  // valid opcode
670
53.0k
  MI->address = address;
671
53.0k
  MI->OpcodePub = MI->Opcode = opcode;
672
673
53.0k
  if (MI->flat_insn->detail) {
674
53.0k
    memset(MI->flat_insn->detail, 0,
675
53.0k
           offsetof(cs_detail, wasm) + sizeof(cs_wasm));
676
53.0k
    WASM_get_insn_id((cs_struct *)ud, MI->flat_insn, opcode);
677
53.0k
  }
678
679
  // setup groups
680
53.0k
  switch (opcode) {
681
0
  default:
682
0
    return false;
683
684
71
  case WASM_INS_I32_CONST:
685
71
    if (code_len == 1 ||
686
66
        !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
687
7
      return false;
688
7
    }
689
690
64
    if (MI->flat_insn->detail) {
691
64
      MI->flat_insn->detail->wasm.op_count = 1;
692
64
      MI->flat_insn->detail
693
64
        ->groups[MI->flat_insn->detail->groups_count] =
694
64
        WASM_GRP_NUMBERIC;
695
64
      MI->flat_insn->detail->groups_count++;
696
64
    }
697
698
64
    *size = param_size + 1;
699
700
64
    break;
701
702
394
  case WASM_INS_I64_CONST:
703
394
    if (code_len == 1 ||
704
392
        !read_varuint64(&code[1], code_len - 1, &param_size, MI)) {
705
6
      return false;
706
6
    }
707
708
388
    if (MI->flat_insn->detail) {
709
388
      MI->flat_insn->detail->wasm.op_count = 1;
710
388
      MI->flat_insn->detail
711
388
        ->groups[MI->flat_insn->detail->groups_count] =
712
388
        WASM_GRP_NUMBERIC;
713
388
      MI->flat_insn->detail->groups_count++;
714
388
    }
715
716
388
    *size = param_size + 1;
717
718
388
    break;
719
720
177
  case WASM_INS_F32_CONST:
721
177
    if (code_len == 1 ||
722
175
        !read_uint32(&code[1], code_len - 1, &param_size, MI)) {
723
7
      return false;
724
7
    }
725
726
170
    if (MI->flat_insn->detail) {
727
170
      MI->flat_insn->detail->wasm.op_count = 1;
728
170
      MI->flat_insn->detail
729
170
        ->groups[MI->flat_insn->detail->groups_count] =
730
170
        WASM_GRP_NUMBERIC;
731
170
      MI->flat_insn->detail->groups_count++;
732
170
    }
733
734
170
    *size = param_size + 1;
735
736
170
    break;
737
738
96
  case WASM_INS_F64_CONST:
739
96
    if (code_len == 1 ||
740
96
        !read_uint64(&code[1], code_len - 1, &param_size, MI)) {
741
6
      return false;
742
6
    }
743
744
90
    if (MI->flat_insn->detail) {
745
90
      MI->flat_insn->detail->wasm.op_count = 1;
746
90
      MI->flat_insn->detail
747
90
        ->groups[MI->flat_insn->detail->groups_count] =
748
90
        WASM_GRP_NUMBERIC;
749
90
      MI->flat_insn->detail->groups_count++;
750
90
    }
751
752
90
    *size = param_size + 1;
753
754
90
    break;
755
756
207
  case WASM_INS_I32_EQZ:
757
486
  case WASM_INS_I32_EQ:
758
866
  case WASM_INS_I32_NE:
759
1.21k
  case WASM_INS_I32_LT_S:
760
1.50k
  case WASM_INS_I32_LT_U:
761
1.89k
  case WASM_INS_I32_GT_S:
762
2.63k
  case WASM_INS_I32_GT_U:
763
2.86k
  case WASM_INS_I32_LE_S:
764
2.96k
  case WASM_INS_I32_LE_U:
765
3.54k
  case WASM_INS_I32_GE_S:
766
3.85k
  case WASM_INS_I32_GE_U:
767
3.94k
  case WASM_INS_I64_EQZ:
768
4.25k
  case WASM_INS_I64_EQ:
769
4.48k
  case WASM_INS_I64_NE:
770
4.96k
  case WASM_INS_I64_LT_S:
771
5.21k
  case WASM_INS_I64_LT_U:
772
5.44k
  case WASN_INS_I64_GT_S:
773
5.62k
  case WASM_INS_I64_GT_U:
774
5.82k
  case WASM_INS_I64_LE_S:
775
6.16k
  case WASM_INS_I64_LE_U:
776
6.78k
  case WASM_INS_I64_GE_S:
777
6.98k
  case WASM_INS_I64_GE_U:
778
7.61k
  case WASM_INS_F32_EQ:
779
7.83k
  case WASM_INS_F32_NE:
780
8.20k
  case WASM_INS_F32_LT:
781
8.73k
  case WASM_INS_F32_GT:
782
8.91k
  case WASM_INS_F32_LE:
783
9.19k
  case WASM_INS_F32_GE:
784
9.47k
  case WASM_INS_F64_EQ:
785
10.3k
  case WASM_INS_F64_NE:
786
10.5k
  case WASM_INS_F64_LT:
787
10.7k
  case WASM_INS_F64_GT:
788
10.8k
  case WASM_INS_F64_LE:
789
11.1k
  case WASM_INS_F64_GE:
790
11.4k
  case WASM_INS_I32_CLZ:
791
11.8k
  case WASM_INS_I32_CTZ:
792
12.0k
  case WASM_INS_I32_POPCNT:
793
12.4k
  case WASM_INS_I32_ADD:
794
12.7k
  case WASM_INS_I32_SUB:
795
12.9k
  case WASM_INS_I32_MUL:
796
13.1k
  case WASM_INS_I32_DIV_S:
797
13.4k
  case WASM_INS_I32_DIV_U:
798
13.7k
  case WASM_INS_I32_REM_S:
799
14.0k
  case WASM_INS_I32_REM_U:
800
14.6k
  case WASM_INS_I32_AND:
801
14.8k
  case WASM_INS_I32_OR:
802
15.1k
  case WASM_INS_I32_XOR:
803
15.4k
  case WASM_INS_I32_SHL:
804
15.6k
  case WASM_INS_I32_SHR_S:
805
15.8k
  case WASM_INS_I32_SHR_U:
806
16.3k
  case WASM_INS_I32_ROTL:
807
16.9k
  case WASM_INS_I32_ROTR:
808
17.0k
  case WASM_INS_I64_CLZ:
809
17.4k
  case WASM_INS_I64_CTZ:
810
17.5k
  case WASM_INS_I64_POPCNT:
811
17.7k
  case WASM_INS_I64_ADD:
812
18.6k
  case WASM_INS_I64_SUB:
813
18.9k
  case WASM_INS_I64_MUL:
814
19.2k
  case WASM_INS_I64_DIV_S:
815
19.7k
  case WASM_INS_I64_DIV_U:
816
19.9k
  case WASM_INS_I64_REM_S:
817
20.1k
  case WASM_INS_I64_REM_U:
818
20.2k
  case WASM_INS_I64_AND:
819
20.5k
  case WASM_INS_I64_OR:
820
20.8k
  case WASM_INS_I64_XOR:
821
21.0k
  case WASM_INS_I64_SHL:
822
21.6k
  case WASM_INS_I64_SHR_S:
823
21.8k
  case WASM_INS_I64_SHR_U:
824
21.9k
  case WASM_INS_I64_ROTL:
825
22.2k
  case WASM_INS_I64_ROTR:
826
22.5k
  case WASM_INS_F32_ABS:
827
22.6k
  case WASM_INS_F32_NEG:
828
22.8k
  case WASM_INS_F32_CEIL:
829
23.4k
  case WASM_INS_F32_FLOOR:
830
23.7k
  case WASM_INS_F32_TRUNC:
831
23.9k
  case WASM_INS_F32_NEAREST:
832
24.3k
  case WASM_INS_F32_SQRT:
833
24.5k
  case WASM_INS_F32_ADD:
834
24.7k
  case WASM_INS_F32_SUB:
835
25.1k
  case WASM_INS_F32_MUL:
836
25.2k
  case WASM_INS_F32_DIV:
837
25.5k
  case WASM_INS_F32_MIN:
838
25.6k
  case WASM_INS_F32_MAX:
839
26.2k
  case WASM_INS_F32_COPYSIGN:
840
26.4k
  case WASM_INS_F64_ABS:
841
26.7k
  case WASM_INS_F64_NEG:
842
26.9k
  case WASM_INS_F64_CEIL:
843
27.2k
  case WASM_INS_F64_FLOOR:
844
27.4k
  case WASM_INS_F64_TRUNC:
845
27.7k
  case WASM_INS_F64_NEAREST:
846
27.9k
  case WASM_INS_F64_SQRT:
847
28.1k
  case WASM_INS_F64_ADD:
848
28.3k
  case WASM_INS_F64_SUB:
849
28.5k
  case WASM_INS_F64_MUL:
850
29.0k
  case WASM_INS_F64_DIV:
851
29.1k
  case WASM_INS_F64_MIN:
852
29.2k
  case WASM_INS_F64_MAX:
853
29.3k
  case WASM_INS_F64_COPYSIGN:
854
29.4k
  case WASM_INS_I32_WARP_I64:
855
29.6k
  case WASP_INS_I32_TRUNC_S_F32:
856
29.9k
  case WASM_INS_I32_TRUNC_U_F32:
857
30.0k
  case WASM_INS_I32_TRUNC_S_F64:
858
30.0k
  case WASM_INS_I32_TRUNC_U_F64:
859
30.3k
  case WASM_INS_I64_EXTEND_S_I32:
860
30.7k
  case WASM_INS_I64_EXTEND_U_I32:
861
30.8k
  case WASM_INS_I64_TRUNC_S_F32:
862
31.0k
  case WASM_INS_I64_TRUNC_U_F32:
863
31.3k
  case WASM_INS_I64_TRUNC_S_F64:
864
31.5k
  case WASM_INS_I64_TRUNC_U_F64:
865
32.3k
  case WASM_INS_F32_CONVERT_S_I32:
866
32.5k
  case WASM_INS_F32_CONVERT_U_I32:
867
32.5k
  case WASM_INS_F32_CONVERT_S_I64:
868
33.0k
  case WASM_INS_F32_CONVERT_U_I64:
869
34.0k
  case WASM_INS_F32_DEMOTE_F64:
870
34.2k
  case WASM_INS_F64_CONVERT_S_I32:
871
34.3k
  case WASM_INS_F64_CONVERT_U_I32:
872
34.6k
  case WASM_INS_F64_CONVERT_S_I64:
873
34.7k
  case WASM_INS_F64_CONVERT_U_I64:
874
35.0k
  case WASM_INS_F64_PROMOTE_F32:
875
35.0k
  case WASM_INS_I32_REINTERPRET_F32:
876
35.3k
  case WASM_INS_I64_REINTERPRET_F64:
877
35.5k
  case WASM_INS_F32_REINTERPRET_I32:
878
35.7k
  case WASM_INS_F64_REINTERPRET_I64:
879
35.7k
    MI->wasm_data.type = WASM_OP_NONE;
880
881
35.7k
    if (MI->flat_insn->detail) {
882
35.7k
      MI->flat_insn->detail->wasm.op_count = 0;
883
35.7k
      MI->flat_insn->detail
884
35.7k
        ->groups[MI->flat_insn->detail->groups_count] =
885
35.7k
        WASM_GRP_NUMBERIC;
886
35.7k
      MI->flat_insn->detail->groups_count++;
887
35.7k
    }
888
889
35.7k
    *size = 1;
890
891
35.7k
    break;
892
893
235
  case WASM_INS_DROP:
894
285
  case WASM_INS_SELECT:
895
285
    MI->wasm_data.type = WASM_OP_NONE;
896
897
285
    if (MI->flat_insn->detail) {
898
285
      MI->flat_insn->detail->wasm.op_count = 0;
899
285
      MI->flat_insn->detail
900
285
        ->groups[MI->flat_insn->detail->groups_count] =
901
285
        WASM_GRP_PARAMETRIC;
902
285
      MI->flat_insn->detail->groups_count++;
903
285
    }
904
905
285
    *size = 1;
906
907
285
    break;
908
909
337
  case WASM_INS_GET_LOCAL:
910
544
  case WASM_INS_SET_LOCAL:
911
1.05k
  case WASM_INS_TEE_LOCAL:
912
1.27k
  case WASM_INS_GET_GLOBAL:
913
1.50k
  case WASM_INS_SET_GLOBAL:
914
1.50k
    if (code_len == 1 ||
915
1.48k
        !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
916
26
      return false;
917
26
    }
918
919
1.47k
    if (MI->flat_insn->detail) {
920
1.47k
      MI->flat_insn->detail->wasm.op_count = 1;
921
1.47k
      MI->flat_insn->detail
922
1.47k
        ->groups[MI->flat_insn->detail->groups_count] =
923
1.47k
        WASM_GRP_VARIABLE;
924
1.47k
      MI->flat_insn->detail->groups_count++;
925
1.47k
    }
926
927
1.47k
    *size = param_size + 1;
928
929
1.47k
    break;
930
931
167
  case WASM_INS_I32_LOAD:
932
660
  case WASM_INS_I64_LOAD:
933
993
  case WASM_INS_F32_LOAD:
934
1.05k
  case WASM_INS_F64_LOAD:
935
1.20k
  case WASM_INS_I32_LOAD8_S:
936
1.34k
  case WASM_INS_I32_LOAD8_U:
937
1.45k
  case WASM_INS_I32_LOAD16_S:
938
1.80k
  case WASM_INS_I32_LOAD16_U:
939
2.18k
  case WASM_INS_I64_LOAD8_S:
940
2.53k
  case WASM_INS_I64_LOAD8_U:
941
2.76k
  case WASM_INS_I64_LOAD16_S:
942
3.01k
  case WASM_INS_I64_LOAD16_U:
943
3.23k
  case WASM_INS_I64_LOAD32_S:
944
3.36k
  case WASM_INS_I64_LOAD32_U:
945
3.46k
  case WASM_INS_I32_STORE:
946
3.55k
  case WASM_INS_I64_STORE:
947
3.62k
  case WASM_INS_F32_STORE:
948
3.95k
  case WASM_INS_F64_STORE:
949
4.16k
  case WASM_INS_I32_STORE8:
950
4.34k
  case WASM_INS_I32_STORE16:
951
4.56k
  case WASM_INS_I64_STORE8:
952
4.78k
  case WASM_INS_I64_STORE16:
953
4.87k
  case WASM_INS_I64_STORE32:
954
4.87k
    if (code_len == 1 ||
955
4.80k
        !read_memoryimmediate(&code[1], code_len - 1, &param_size,
956
4.80k
            MI)) {
957
154
      return false;
958
154
    }
959
960
4.71k
    if (MI->flat_insn->detail) {
961
4.71k
      MI->flat_insn->detail->wasm.op_count = 2;
962
4.71k
      MI->flat_insn->detail
963
4.71k
        ->groups[MI->flat_insn->detail->groups_count] =
964
4.71k
        WASM_GRP_MEMORY;
965
4.71k
      MI->flat_insn->detail->groups_count++;
966
4.71k
    }
967
968
4.71k
    *size = param_size + 1;
969
970
4.71k
    break;
971
972
317
  case WASM_INS_CURRENT_MEMORY:
973
558
  case WASM_INS_GROW_MEMORY:
974
558
    MI->wasm_data.type = WASM_OP_NONE;
975
976
558
    if (MI->flat_insn->detail) {
977
558
      MI->flat_insn->detail->wasm.op_count = 0;
978
558
      MI->flat_insn->detail
979
558
        ->groups[MI->flat_insn->detail->groups_count] =
980
558
        WASM_GRP_MEMORY;
981
558
      MI->flat_insn->detail->groups_count++;
982
558
    }
983
984
558
    *size = 1;
985
986
558
    break;
987
988
5.67k
  case WASM_INS_UNREACHABLE:
989
5.98k
  case WASM_INS_NOP:
990
6.34k
  case WASM_INS_ELSE:
991
6.55k
  case WASM_INS_END:
992
6.69k
  case WASM_INS_RETURN:
993
6.69k
    MI->wasm_data.type = WASM_OP_NONE;
994
995
6.69k
    if (MI->flat_insn->detail) {
996
6.69k
      MI->flat_insn->detail->wasm.op_count = 0;
997
6.69k
      MI->flat_insn->detail
998
6.69k
        ->groups[MI->flat_insn->detail->groups_count] =
999
6.69k
        WASM_GRP_CONTROL;
1000
6.69k
      MI->flat_insn->detail->groups_count++;
1001
6.69k
    }
1002
1003
6.69k
    *size = 1;
1004
1005
6.69k
    break;
1006
1007
342
  case WASM_INS_BLOCK:
1008
408
  case WASM_INS_LOOP:
1009
511
  case WASM_INS_IF:
1010
511
    if (code_len == 1 ||
1011
495
        !read_varint7(&code[1], code_len - 1, &param_size, MI)) {
1012
16
      return false;
1013
16
    }
1014
1015
495
    if (MI->flat_insn->detail) {
1016
495
      MI->flat_insn->detail->wasm.op_count = 1;
1017
495
      MI->flat_insn->detail
1018
495
        ->groups[MI->flat_insn->detail->groups_count] =
1019
495
        WASM_GRP_CONTROL;
1020
495
      MI->flat_insn->detail->groups_count++;
1021
495
    }
1022
1023
495
    *size = param_size + 1;
1024
1025
495
    break;
1026
1027
362
  case WASM_INS_BR:
1028
724
  case WASM_INS_BR_IF:
1029
1.00k
  case WASM_INS_CALL:
1030
1.23k
  case WASM_INS_CALL_INDIRECT:
1031
1.23k
    if (code_len == 1 ||
1032
1.22k
        !read_varuint32(&code[1], code_len - 1, &param_size, MI)) {
1033
13
      return false;
1034
13
    }
1035
1036
1.21k
    if (MI->flat_insn->detail) {
1037
1.21k
      MI->flat_insn->detail->wasm.op_count = 1;
1038
1.21k
      MI->flat_insn->detail
1039
1.21k
        ->groups[MI->flat_insn->detail->groups_count] =
1040
1.21k
        WASM_GRP_CONTROL;
1041
1.21k
      MI->flat_insn->detail->groups_count++;
1042
1.21k
    }
1043
1044
1.21k
    *size = param_size + 1;
1045
1046
1.21k
    break;
1047
1048
965
  case WASM_INS_BR_TABLE:
1049
965
    if (code_len == 1 ||
1050
959
        !read_brtable(&code[1], code_len - 1, &param_size, MI)) {
1051
55
      return false;
1052
55
    }
1053
1054
910
    if (MI->flat_insn->detail) {
1055
910
      MI->flat_insn->detail->wasm.op_count = 1;
1056
910
      MI->flat_insn->detail
1057
910
        ->groups[MI->flat_insn->detail->groups_count] =
1058
910
        WASM_GRP_CONTROL;
1059
910
      MI->flat_insn->detail->groups_count++;
1060
910
    }
1061
1062
910
    *size = param_size + 1;
1063
1064
910
    break;
1065
53.0k
  }
1066
1067
52.8k
  return true;
1068
53.0k
}