File: ControlFlowTests.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 (434 lines) | stat: -rw-r--r-- 13,992 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
/*
 * 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>

using namespace JSC;
using namespace JSC::Wasm;

namespace WasmDebugInfoTest {

static bool testUnreachableOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x00, // [0] unreachable
        0x0b // [1] end
    };

    // IPIntGenerator::didParseOpcode() skips debug info recording for unreachable blocks.
    SourceModule module = createWasmModuleWithBytecode(functionBody);
    return module.parseAndVerifyDebugInfo(opcode, { });
}

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

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    // nop, drop, and end are not in the mappings because they're handled directly
    // in ExecutionHandler::step() by setting breakpoint at currentPC + 1
    return module.parseAndVerifyDebugInfo(opcode, { { 1, { 3 } } });
}
static bool testDropOpcode(OpType opcode) { return testNopOpcode(opcode); }
static bool testEndOpcode(OpType opcode) { return testNopOpcode(opcode); }

static bool testBlockOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x02, 0x40, // [0] block $b0
        0x41, 0x01, // [2] i32.const 1
        0x04, 0x40, // [4] if
        0x0c, 0x01, // [6] br 1 (to after $b0)
        0x0b, // [8] end if
        0x41, 0x00, // [9] i32.const 0
        0x04, 0x40, // [11] if
        0x0c, 0x01, // [13] br 1 (to after $b0)
        0x0b, // [15] end if
        0x0b, // [16] end $b0

        0x02, 0x40, // [17] block $b1
        0x41, 0x01, // [19] i32.const 1
        0x04, 0x40, // [21] if
        0x0c, 0x01, // [23] br 1 (to after $b1)
        0x0b, // [25] end if
        0x41, 0x00, // [26] i32.const 0
        0x04, 0x40, // [28] if
        0x0c, 0x01, // [30] br 1 (to after $b1)
        0x0b, // [32] end if
        0x0b, // [33] end $b1

        0x0b // [34] end function
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
        { 4, { 6, 9 } },
        { 6, { 19 } }, // br 1 jumps past $b0 to first instruction in $b1
        { 9, { 11 } },
        { 11, { 13, 16 } },
        { 13, { 19 } }, // br 1 jumps past $b0 to first instruction in $b1
        { 17, { 19 } },
        { 19, { 21 } },
        { 21, { 23, 26 } },
        { 23, { 34 } }, // br 1 jumps past $b1 to end function
        { 26, { 28 } },
        { 28, { 30, 33 } },
        { 30, { 34 } }, // br 1 jumps past $b1 to end function
    });
}
static bool testBrOpcode(OpType opcode) { return testBlockOpcode(opcode); }

static bool testLoopOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x00, // [0] i32.const 0
        0x21, 0x00, // [2] local.set 0 (counter)
        0x03, 0x40, // [4] loop (void)
        0x20, 0x00, // [6] local.get 0
        0x41, 0x03, // [8] i32.const 3
        0x49, // [10] i32.lt_s
        0x04, 0x40, // [11] if (void)
        0x20, 0x00, // [13] local.get 0
        0x41, 0x01, // [15] i32.const 1
        0x6a, // [17] i32.add
        0x21, 0x00, // [18] local.set 0
        0x0c, 0x01, // [20] br 1 (back to loop start)
        0x0b, // [22] end if
        0x0b, // [23] end loop
        0x0b // [24] end function
    };

    SourceModule module = createWasmModuleWithLocals(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
        { 4, { 6 } },
        { 6, { 8 } },
        { 8, { 10 } },
        { 10, { 11 } },
        { 11, { 13, 23 } },
        { 13, { 15 } },
        { 15, { 17 } },
        { 17, { 18 } },
        { 18, { 20 } },
        { 20, { 4 } },
    });
}

static bool testIfOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x01, // [0] i32.const 1
        0x04, 0x40, // [2] if (void)
        0x41, 0x2a, // [4] i32.const 42
        0x1a, // [6] drop
        0x05, // [7] else
        0x41, 0x63, // [8] i32.const 99
        0x1a, // [10] drop
        0x0b, // [11] end if
        0x0b // [12] end function
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4, 8 } },
        { 4, { 6 } },
        { 7, { 12 } },
        { 8, { 10 } },
    });
}
static bool testElseOpcode(OpType opcode) { return testIfOpcode(opcode); }

static bool testBrIfOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x02, 0x40, // [0] block $b0
        0x41, 0x01, // [2] i32.const 1
        0x0d, 0x00, // [4] br_if 0 (break to after block $b0 if true)
        0x41, 0x2a, // [6] i32.const 42
        0x1a, // [8] drop
        0x0b, // [9] end block $b0
        0x02, 0x40, // [10] block $b1
        0x41, 0x00, // [12] i32.const 0
        0x0d, 0x00, // [14] br_if 0 (break to after block $b1 if true)
        0x41, 0x63, // [16] i32.const 99
        0x1a, // [18] drop
        0x0b, // [19] end block $b1
        0x0b // [20] end function
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
        { 4, { 6, 12 } }, // br_if: continue (6) or jump past $b0 end to $b1 start (12)
        { 6, { 8 } },
        { 10, { 12 } },
        { 12, { 14 } },
        { 14, { 16, 20 } }, // br_if: continue (16) or jump to function end (20)
        { 16, { 18 } },
    });
}

static bool testBrTableOpcode(OpType opcode)
{
    // Create a br_table with distinct branch targets
    Vector<uint8_t> functionBody = {
        0x02, 0x40, // [0] block $b0
        0x02, 0x40, // [2] block $b1
        0x02, 0x40, // [4] block $b2
        0x20, 0x00, // [6] local.get 0 (param: i32 selector)
        0x0e, 0x02, 0x02, 0x01, 0x00, // [8] br_table [2, 1] default:0
        // index=0 → label 2 (after $b0), index=1 → label 1 (after $b1), index>=2 → label 0 (after $b2)
        0x0b, // [13] end $b2
        0x41, 0x2a, // [14] i32.const 42 (after $b2)
        0x1a, // [16] drop
        0x0b, // [17] end $b1
        0x41, 0x63, // [18] i32.const 99 (after $b1)
        0x1a, // [20] drop
        0x0b, // [21] end $b0
        0x0b // [22] end function
    };

    SourceModule module = SourceModule::create()
        .withFunctionType({ 0x7f }, { }) // [i32] -> []
        .withFunctionBody(functionBody)
        .build();

    return module.parseAndVerifyDebugInfo(opcode, {
        // FIXME: Block coalescing (offsets 0→2→4→6) should ideally result in { 0, { 6 } } only,
        // but exit handlers in resolveExitTarget/coalesceControlFlow use ADD mode instead of UPDATE mode,
        // accumulating all intermediate targets {2, 4, 6}. This doesn't break debugger functionality
        // but could be optimized to use UPDATE mode like resolveEntryTarget does.
        { 0, { 2, 4, 6 } },
        { 6, { 8 } },
        { 8, { 14, 18, 22 } },
        { 14, { 16 } },
        { 18, { 20 } },
    });
}

static bool testReturnOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x01, // [0] i32.const 1
        0x04, 0x40, // [2] if
        0x0f, // [4] return
        0x0b, // [5] end if
        0x0b // [6] end function
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    // This is handled directly in ExecutionHandler::step().
    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4, 6 } },
    });
}

static bool testSelectOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42
        0x41, 0x63, // [2] i32.const 99
        0x41, 0x01, // [4] i32.const 1
        0x1b, // [6] select
        0x1a, // [7] drop
        0x0b // [8] end
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    // This is handled directly in ExecutionHandler::step().
    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
        { 4, { 6 } },
    });
}

static bool testAnnotatedSelectOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x41, 0x2a, // [0] i32.const 42
        0x41, 0x63, // [2] i32.const 99
        0x41, 0x01, // [4] i32.const 1
        0x1c, 0x01, 0x7f, // [6] select (result i32)
        0x1a, // [9] drop
        0x0b // [10] end
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

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

static bool testBrOnNullOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x02, 0x40, // [0] block $b0
        0xd0, 0x6f, // [2] ref.null extern
        0xd5, 0x00, // [4] br_on_null 0
        0x1a, // drop the non-null ref - offset 6
        0xd0, 0x6f, // [7] ref.null extern
        0xd5, 0x00, // [9] br_on_null 0
        0x1a, // drop the non-null ref - offset 11
        0x0b, // [12] end $b0

        0x02, 0x40, // [13] block $b1
        0xd0, 0x6f, // [15] ref.null extern
        0xd5, 0x00, // [17] br_on_null 0
        0x1a, // drop the non-null ref - offset 19
        0xd0, 0x6f, // [20] ref.null extern
        0xd5, 0x00, // [22] br_on_null 0
        0x1a, // drop the non-null ref - offset 24
        0x0b, // [25] end $b1

        0x0b // [26] end function
    };

    SourceModule module = createWasmModuleWithBytecode(functionBody);

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
        { 4, { 6, 15 } },
        { 7, { 9 } },
        { 9, { 11, 15 } },
        { 13, { 15 } },
        { 15, { 17 } },
        { 17, { 19, 26 } },
        { 20, { 22 } },
        { 22, { 24, 26 } },
    });
}

static bool testBrOnNonNullOpcode(OpType opcode)
{
    Vector<uint8_t> functionBody = {
        0x02, 0x40, // [0] block $b0
        0x20, 0x00, // [2] local.get 0 (funcref param)
        0xd6, 0x00, // [4] br_on_non_null 0
        0x20, 0x00, // [6] local.get 0
        0xd6, 0x00, // [8] br_on_non_null 0
        0x0b, // [10] end $b0

        0x02, 0x40, // [11] block $b1
        0x20, 0x00, // [13] local.get 0
        0xd6, 0x00, // [15] br_on_non_null 0
        0x20, 0x00, // [17] local.get 0
        0xd6, 0x00, // [19] br_on_non_null 0
        0x0b, // [21] end $b1

        0x0b // [22] end function
    };

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

    return module.parseAndVerifyDebugInfo(opcode, {
        { 0, { 2 } },
        { 2, { 4 } },
        { 4, { 6, 13 } },
        { 6, { 8 } },
        { 8, { 10, 13 } },
        { 11, { 13 } },
        { 13, { 15 } },
        { 15, { 17, 22 } },
        { 17, { 19 } },
        { 19, { 21, 22 } },
    });
}

// Exception handling opcodes require runtime testing because genericUnwind() dynamically
// computes handler PCs. See runtime tests in JSTests/wasm/debugger/resources/wasm/:
//   throw-catch.js, throw-catch-all.js, rethrow.js, throw-ref.js, delegate.js, try-table.js
static bool testTryOpcode(OpType) { return true; }
static bool testCatchOpcode(OpType) { return true; }
static bool testThrowOpcode(OpType) { return true; }
static bool testRethrowOpcode(OpType) { return true; }
static bool testThrowRefOpcode(OpType) { return true; }
static bool testDelegateOpcode(OpType) { return true; }
static bool testCatchAllOpcode(OpType) { return true; }
static bool testTryTableOpcode(OpType) { return true; }

void testAllControlFlowOps()
{
    dataLogLn("=== Testing All Control Flow Ops Coverage ===");
    dataLogLn("Total control flow opcodes in WasmOps.h: ", TOTAL_CONTROL_OPS);

    int opsTested = 0;
    int opsSucceeded = 0;

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

    FOR_EACH_WASM_CONTROL_FLOW_OP(TEST_CONTROL_FLOW_OP)

#undef TEST_CONTROL_FLOW_OP

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

} // namespace WasmDebugInfoTest

#endif // ENABLE(WEBASSEMBLY)