File: SpecialTests.cpp

package info (click to toggle)
webkit2gtk 2.51.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 477,912 kB
  • sloc: cpp: 3,898,343; javascript: 198,215; ansic: 165,229; python: 50,371; asm: 21,819; ruby: 18,095; perl: 16,953; xml: 4,623; sh: 2,398; yacc: 2,356; java: 2,019; lex: 1,358; pascal: 372; makefile: 197
file content (480 lines) | stat: -rw-r--r-- 13,508 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright (C) 2025 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "TestUtilities.h"

#if ENABLE(WEBASSEMBLY)

#include <wtf/DataLog.h>
#include <wtf/text/MakeString.h>

using namespace JSC;
using namespace JSC::Wasm;

namespace WasmDebugInfoTest {

static bool testI32ConstOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42
        0x1a, // [2] drop
        0x0b // [3] end
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, { { 0, { 2 } } });
}

static bool testI64ConstOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x42, 0x2a, // [0] i64.const 42
        0x1a, // [2] drop
        0x0b // [3] end
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, { { 0, { 2 } } });
}

static bool testF32ConstOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x43, 0x00, 0x00, 0x80, 0x3f, // [0] f32.const 1.0
        0x1a, // [5] drop
        0x0b // [6] end
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, { { 0, { 5 } } });
}

static bool testF64ConstOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, // [0] f64.const 1.0
        0x1a, // [9] drop
        0x0b // [10] end
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, { { 0, { 9 } } });
}

static bool testGetLocalOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42
        0x21, 0x00, // [2] local.set 0
        0x20, 0x00, // [4] local.get 0
        0x1a, // [6] drop
        0x0b // [7] end
    };

    SourceModule module = createWasmModuleWithLocals(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
        { 4, { 6 } }
    });
}

static bool testSetLocalOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42
        0x21, 0x00, // [2] local.set 0
        0x0b // [4] end
    };

    SourceModule module = createWasmModuleWithLocals(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } }
    });
}

static bool testTeeLocalOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42
        0x22, 0x00, // [2] local.tee 0
        0x1a, // [4] drop
        0x0b // [5] end
    };

    SourceModule module = createWasmModuleWithLocals(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } }
    });
}

static bool testGetGlobalOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x23, 0x00, // [0] global.get 0
        0x1a, // [2] drop
        0x0b // [3] end
    };

    SourceModule module = createWasmModuleWithGlobals(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, { { 0, { 2 } } });
}

static bool testSetGlobalOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42
        0x24, 0x00, // [2] global.set 0
        0x0b // [4] end
    };

    SourceModule module = createWasmModuleWithGlobals(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } }
    });
}

static bool testTableGetOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x00, // [0] i32.const 0
        0x25, 0x00, // [2] table.get 0
        0x1a, // [4] drop
        0x0b // [5] end
    };

    SourceModule module = createWasmModuleWithTable(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } }
    });
}

static bool testTableSetOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x00, // [0] i32.const 0 (index)
        0xd0, 0x70, // [2] ref.null func
        0x26, 0x00, // [4] table.set 0
        0x0b // [6] end
    };

    SourceModule module = createWasmModuleWithTable(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
        { 4, { 6 } }
    });
}

static bool testCurrentMemoryOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x3f, 0x00, // [0] memory.size
        0x1a, // [2] drop
        0x0b // [3] end
    };

    SourceModule module = createWasmModuleWithMemory(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, { { 0, { 2 } } });
}

static bool testGrowMemoryOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x01, // [0] i32.const 1
        0x40, 0x00, // [2] memory.grow
        0x1a, // [4] drop
        0x0b // [5] end
    };

    SourceModule module = createWasmModuleWithMemory(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } }
    });
}

static bool testRefNullOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0xd0, 0x70, // [0] ref.null func
        0x1a, // [2] drop
        0x0b // [3] end
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, { { 0, { 2 } } });
}

static bool testRefIsNullOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0xd0, 0x70, // [0] ref.null func
        0xd1, // [2] ref.is_null
        0x1a, // [3] drop
        0x0b // [4] end
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 3 } },
    });
}

static bool testRefFuncOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0xd2, 0x00, // [0] ref.func 0
        0x0b // [2] end
    };

    SourceModule module = SourceModule::create()
        .withFunctionType({ }, { toLEB128(TypeKind::Funcref) })
        .withFunctionBody(functionBody)
        .build();

    return module.parseAndVerifyDebugInfo(opcode, { { 0, { 2 } } });
}

static bool testRefEqOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0xd0, 0x6d, // [0] ref.null eq
        0xd0, 0x6d, // [2] ref.null eq
        0xd3, // [4] ref.eq
        0x0b // [5] end
    };

    SourceModule module = SourceModule::create()
        .withFunctionType({ }, { toLEB128(TypeKind::I32) })
        .withFunctionBody(functionBody)
        .build();

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
        { 4, { 5 } },
    });
}

static bool testRefAsNonNullOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x20, 0x00, // [0] local.get 0
        0xd4, // [2] ref.as_non_null
        0x0b // [3] end
    };

    SourceModule module = SourceModule::create()
        .withFunctionType({ toLEB128(TypeKind::Funcref) }, { toLEB128(TypeKind::Funcref) })
        .withFunctionBody(functionBody)
        .build();

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 3 } },
    });
}

// Note: Call instructions have two debugging behaviors:
// 1. Step-over: Maps call instruction -> next instruction (tested here via offsetToNextInstructions)
// 2. Step-into: Handled at runtime in ExecutionHandler::step() when target is IPInt mode
// This test verifies the step-over case where we map the call to the next instruction.
static bool testCallOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42
        0x10, 0x00, // [2] call 0 (recursive call)
        0x1a, // [4] drop
        0x0b // [5] end
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
    });
}

static bool testCallIndirectOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42 (function argument)
        0x41, 0x00, // [2] i32.const 0 (table index)
        0x11, 0x00, 0x00, // [4] call_indirect type=0 table=0
        0x0b // [7] end (function returns the i32 result)
    };

    SourceModule module = SourceModule::create()
        .withFunctionType({ toLEB128(TypeKind::I32) }, { toLEB128(TypeKind::I32) })
        .withTable()
        .withFunctionBody(functionBody)
        .build();

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
        { 4, { 7 } },
    });
}

static bool testCallRefOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0xd2, 0x00, // [0] ref.func 0
        0x14, 0x00, // [2] call_ref type=0
        0x0b // [4] end
    };

    SourceModule module = SourceModule::create()
        .withFunctionType({ }, { toLEB128(TypeKind::I32) })
        .withFunctionBody(functionBody)
        .build();

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
    });
}

static bool testTailCallOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42
        0x12, 0x00, // [2] tail_call 0 (recursive tail call)
        0x0b // [4] end
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    // Tail calls return to caller's caller, not to the next instruction
    // Step-over is handled at runtime by setting breakpoint at caller
    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } }
    });
}

static bool testTailCallIndirectOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42 (function argument)
        0x41, 0x00, // [2] i32.const 0 (table index)
        0x13, 0x00, 0x00, // [4] tail_call_indirect type=0 table=0
        0x0b // [7] end
    };

    SourceModule module = SourceModule::create()
        .withFunctionType({ toLEB128(TypeKind::I32) }, { toLEB128(TypeKind::I32) })
        .withTable()
        .withFunctionBody(functionBody)
        .build();

    // Tail calls return to caller's caller, not to the next instruction
    // Step-over is handled at runtime by setting breakpoint at caller
    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } }
    });
}

static bool testTailCallRefOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0xd2, 0x00, // [0] ref.func 0
        0x15, 0x00, // [2] tail_call_ref type=0
        0x0b // [4] end
    };

    SourceModule module = SourceModule::create()
        .withFunctionType({ }, { toLEB128(TypeKind::I32) })
        .withFunctionBody(functionBody)
        .build();

    // Tail calls return to caller's caller, not to the next instruction
    // Step-over is handled at runtime by setting breakpoint at caller
    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } }
    });
}

void testAllSpecialOps()
{
    dataLogLn("=== Testing All Special Ops Coverage ===");
    dataLogLn("Total special opcodes in WasmOps.h: ", TOTAL_SPECIAL_OPS);

    int opsTested = 0;
    int opsSucceeded = 0;

#define TEST_SPECIAL_OP(name, id, b3, inc) \
    do { \
        opsTested++; \
        testsRun++; \
        if (test##name##Opcode(OpType::name)) { \
            opsSucceeded++; \
            testsPassed++; \
        } else { \
            testsFailed++; \
            dataLogLn("FAILED: ", #name, " special opcode test"); \
        } \
    } while (0);

    FOR_EACH_WASM_SPECIAL_OP(TEST_SPECIAL_OP)

#undef TEST_SPECIAL_OP

    TEST_ASSERT(opsTested == TOTAL_SPECIAL_OPS,
        makeString("Tested all "_s, String::number(TOTAL_SPECIAL_OPS), " special ops"_s).utf8().data());
    TEST_ASSERT(opsSucceeded == TOTAL_SPECIAL_OPS,
        makeString("All "_s, String::number(TOTAL_SPECIAL_OPS), " special ops completed"_s).utf8().data());

    dataLogLn("  Successfully tested: ", opsSucceeded, " / ", opsTested, " special ops");
    dataLogLn("All special ops coverage testing completed");
}

} // namespace WasmDebugInfoTest

#endif // ENABLE(WEBASSEMBLY)