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
|
/*
* 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 testBrOnCastOpcode(ExtGCOpType opcode)
{
Vector<uint8_t> functionBody = {
0x02, 0x01, // [0] block (result i32 i31ref)
0x41, 0x2a, // [2] i32.const 42
0x20, 0x00, // [4] local.get 0 (anyref param)
0xfb, 0x18, // [6] br_on_cast (ExtGC opcode 0x18)
0x03, // [8] flags (0x03 = both nullable)
0x00, // [9] branch depth 0
0x6e, // [10] source ref type: anyref
0x6c, // [11] target ref type: i31ref
0x41, 0x07, // [12] i32.const 7
0xd0, 0x6c, // [14] ref.null i31ref
0x0c, 0x00, // [16] br 0
0x0b, // [18] end block
0x1a, // [19] drop
0x0b // [20] end function
};
SourceModule module = SourceModule::create()
.withFunctionType({ 0x6e }, { 0x7f }) // Type 0: (anyref) -> i32
.withAdditionalType({ }, { 0x7f, 0x6c }) // Type 1: () -> (i32, i31ref)
.withFunctionBody(functionBody)
.build();
return module.parseAndVerifyDebugInfo(opcode, {
{ 0, { 2 } },
{ 2, { 4 } },
{ 4, { 6 } },
{ 6, { 12, 19 } },
{ 12, { 14 } },
{ 14, { 16 } },
{ 16, { 19 } }
});
}
static bool testBrOnCastFailOpcode(ExtGCOpType opcode)
{
Vector<uint8_t> functionBody = {
0x02, 0x01, // [0] block (result i32 anyref)
0x41, 0x2a, // [2] i32.const 42
0x20, 0x00, // [4] local.get 0 (anyref param)
0xfb, 0x19, // [6] br_on_cast_fail (ExtGC opcode 0x19)
0x03, // [8] flags (0x03 = both nullable)
0x00, // [9] branch depth 0
0x6e, // [10] source ref type: anyref
0x6c, // [11] target ref type: i31ref
0x1a, // [12] drop (i31ref from successful cast)
0x41, 0x07, // [13] i32.const 7
0x20, 0x00, // [15] local.get 0 (push anyref again)
0x0c, 0x00, // [17] br 0
0x0b, // [19] end block
0x1a, // [20] drop (anyref)
0x0b // [21] end function
};
SourceModule module = SourceModule::create()
.withFunctionType({ 0x6e }, { 0x7f }) // Type 0: (anyref) -> i32
.withAdditionalType({ }, { 0x7f, 0x6e }) // Type 1: () -> (i32, anyref)
.withFunctionBody(functionBody)
.build();
return module.parseAndVerifyDebugInfo(opcode, {
{ 0, { 2 } },
{ 2, { 4 } },
{ 4, { 6 } },
{ 6, { 12, 20 } },
{ 13, { 15 } },
{ 15, { 17 } },
{ 17, { 20 } }
});
}
// FIXME: Implement proper test for ExtGCOpType::name
#define DEFINE_EXTGC_OP_TEST(name) \
static bool test##name##Opcode(ExtGCOpType) \
{ \
return true; \
}
DEFINE_EXTGC_OP_TEST(StructNew)
DEFINE_EXTGC_OP_TEST(StructNewDefault)
DEFINE_EXTGC_OP_TEST(StructGet)
DEFINE_EXTGC_OP_TEST(StructGetS)
DEFINE_EXTGC_OP_TEST(StructGetU)
DEFINE_EXTGC_OP_TEST(StructSet)
DEFINE_EXTGC_OP_TEST(ArrayNew)
DEFINE_EXTGC_OP_TEST(ArrayNewDefault)
DEFINE_EXTGC_OP_TEST(ArrayNewFixed)
DEFINE_EXTGC_OP_TEST(ArrayNewData)
DEFINE_EXTGC_OP_TEST(ArrayNewElem)
DEFINE_EXTGC_OP_TEST(ArrayGet)
DEFINE_EXTGC_OP_TEST(ArrayGetS)
DEFINE_EXTGC_OP_TEST(ArrayGetU)
DEFINE_EXTGC_OP_TEST(ArraySet)
DEFINE_EXTGC_OP_TEST(ArrayLen)
DEFINE_EXTGC_OP_TEST(ArrayFill)
DEFINE_EXTGC_OP_TEST(ArrayCopy)
DEFINE_EXTGC_OP_TEST(ArrayInitData)
DEFINE_EXTGC_OP_TEST(ArrayInitElem)
DEFINE_EXTGC_OP_TEST(RefTest)
DEFINE_EXTGC_OP_TEST(RefTestNull)
DEFINE_EXTGC_OP_TEST(RefCast)
DEFINE_EXTGC_OP_TEST(RefCastNull)
DEFINE_EXTGC_OP_TEST(AnyConvertExtern)
DEFINE_EXTGC_OP_TEST(ExternConvertAny)
DEFINE_EXTGC_OP_TEST(RefI31)
DEFINE_EXTGC_OP_TEST(I31GetS)
DEFINE_EXTGC_OP_TEST(I31GetU)
#undef DEFINE_EXTGC_OP_TEST
void testAllExtGCOps()
{
dataLogLn("=== Testing All ExtGC Ops Coverage ===");
dataLogLn("Total ExtGC opcodes in WasmOps.h: ", TOTAL_EXTGC_OPS);
int opsTested = 0;
int opsSucceeded = 0;
#define TEST_EXTGC_OP(name, id, b3, inc) do { \
opsTested++; \
testsRun++; \
if (test##name##Opcode(ExtGCOpType::name)) { \
opsSucceeded++; \
testsPassed++; \
} else { \
testsFailed++; \
dataLogLn("FAILED: ", #name, " ExtGC opcode test"); \
} \
} while (0);
FOR_EACH_WASM_GC_OP(TEST_EXTGC_OP)
#undef TEST_EXTGC_OP
TEST_ASSERT(opsTested == TOTAL_EXTGC_OPS,
makeString("Tested all "_s, String::number(TOTAL_EXTGC_OPS), " ExtGC ops"_s).utf8().data());
TEST_ASSERT(opsSucceeded == TOTAL_EXTGC_OPS,
makeString("All "_s, String::number(TOTAL_EXTGC_OPS), " ExtGC ops completed"_s).utf8().data());
dataLogLn(" Successfully tested: ", opsSucceeded, " / ", opsTested, " ExtGC ops");
dataLogLn("All ExtGC ops coverage testing completed");
}
} // namespace WasmDebugInfoTest
#endif // ENABLE(WEBASSEMBLY)
|