Commit c8b5645c authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm][test] Use Vector in func body decoder unittests

Minor refactoring.

R=titzer@chromium.org

Bug: v8:8238
Change-Id: Ibf3388cf8fc4a8d618e2e0da53209e29e753058d
Reviewed-on: https://chromium-review.googlesource.com/c/1356501Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57958}
parent c75ca589
......@@ -54,39 +54,37 @@ static const WasmOpcode kInt32BinopOpcodes[] = {
val, WASM_ZERO, kExprBrIf, static_cast<byte>(depth)
#define EXPECT_VERIFIES_C(sig, x) \
Verify(true, sigs.sig(), x, x + arraysize(x), kAppendEnd)
Verify(true, sigs.sig(), ArrayVector(x), kAppendEnd)
#define EXPECT_FAILURE_C(sig, x) \
Verify(false, sigs.sig(), x, x + arraysize(x), kAppendEnd)
#define EXPECT_FAILURE_C(sig, x, ...) \
Verify(false, sigs.sig(), ArrayVector(x), kAppendEnd, ##__VA_ARGS__)
#define EXPECT_VERIFIES_SC(sig, x) \
Verify(true, sig, x, x + arraysize(x), kAppendEnd)
#define EXPECT_VERIFIES_SC(sig, x) Verify(true, sig, ArrayVector(x), kAppendEnd)
#define EXPECT_FAILURE_SC(sig, x) \
Verify(false, sig, x, x + arraysize(x), kAppendEnd)
#define EXPECT_FAILURE_SC(sig, x) Verify(false, sig, ArrayVector(x), kAppendEnd)
#define EXPECT_VERIFIES_S(env, ...) \
do { \
static byte code[] = {__VA_ARGS__}; \
Verify(true, env, code, code + arraysize(code), kAppendEnd); \
#define EXPECT_VERIFIES_S(env, ...) \
do { \
static byte code[] = {__VA_ARGS__}; \
Verify(true, env, ArrayVector(code), kAppendEnd); \
} while (false)
#define EXPECT_FAILURE_S(env, ...) \
do { \
static byte code[] = {__VA_ARGS__}; \
Verify(false, env, code, code + arraysize(code), kAppendEnd); \
#define EXPECT_FAILURE_S(env, ...) \
do { \
static byte code[] = {__VA_ARGS__}; \
Verify(false, env, ArrayVector(code), kAppendEnd); \
} while (false)
#define EXPECT_VERIFIES(sig, ...) \
do { \
static const byte code[] = {__VA_ARGS__}; \
Verify(true, sigs.sig(), code, code + sizeof(code), kAppendEnd); \
#define EXPECT_VERIFIES(sig, ...) \
do { \
static const byte code[] = {__VA_ARGS__}; \
EXPECT_VERIFIES_C(sig, code); \
} while (false)
#define EXPECT_FAILURE(sig, ...) \
do { \
static const byte code[] = {__VA_ARGS__}; \
Verify(false, sigs.sig(), code, code + sizeof(code), kAppendEnd); \
#define EXPECT_FAILURE(sig, ...) \
do { \
static const byte code[] = {__VA_ARGS__}; \
EXPECT_FAILURE_C(sig, code); \
} while (false)
class FunctionBodyDecoderTest : public TestWithZone {
......@@ -108,35 +106,32 @@ class FunctionBodyDecoderTest : public TestWithZone {
enum AppendEnd : bool { kAppendEnd, kOmitEnd };
void PrepareBytecode(const byte** startp, const byte** endp,
AppendEnd append_end) {
const byte* start = *startp;
const byte* end = *endp;
Vector<const byte> PrepareBytecode(Vector<const byte> code,
AppendEnd append_end) {
size_t locals_size = local_decls.Size();
size_t total_size = end - start + locals_size;
if (append_end == kAppendEnd) ++total_size;
size_t total_size =
code.size() + locals_size + (append_end == kAppendEnd ? 1 : 0);
byte* buffer = static_cast<byte*>(zone()->New(total_size));
// Prepend the local decls to the code.
local_decls.Emit(buffer);
// Emit the code.
memcpy(buffer + locals_size, start, end - start);
memcpy(buffer + locals_size, code.start(), code.size());
if (append_end == kAppendEnd) {
// Append an extra end opcode.
buffer[total_size - 1] = kExprEnd;
}
*startp = buffer;
*endp = buffer + total_size;
return {buffer, total_size};
}
// Prepends local variable declarations and renders nice error messages for
// verification failures.
void Verify(bool expected_success, FunctionSig* sig, const byte* start,
const byte* end, AppendEnd append_end) {
PrepareBytecode(&start, &end, append_end);
void Verify(bool expected_success, FunctionSig* sig, Vector<const byte> code,
AppendEnd append_end, const char* message = nullptr) {
code = PrepareBytecode(code, append_end);
// Verify the code.
FunctionBody body(sig, 0, start, end);
FunctionBody body(sig, 0, code.start(), code.end());
WasmFeatures unused_detected_features;
DecodeResult result =
VerifyWasmCode(zone()->allocator(), enabled_features_, module,
......@@ -291,9 +286,8 @@ TEST_F(FunctionBodyDecoderTest, RefNull) {
}
TEST_F(FunctionBodyDecoderTest, EmptyFunction) {
byte code[] = {0};
Verify(true, sigs.v_v(), code, code, kAppendEnd);
Verify(false, sigs.i_i(), code, code, kAppendEnd);
Verify(true, sigs.v_v(), {}, kAppendEnd);
Verify(false, sigs.i_i(), {}, kAppendEnd);
}
TEST_F(FunctionBodyDecoderTest, IncompleteIf1) {
......@@ -348,10 +342,10 @@ TEST_F(FunctionBodyDecoderTest, Float64Const) {
TEST_F(FunctionBodyDecoderTest, Int32Const_off_end) {
byte code[] = {kExprI32Const, 0xAA, 0xBB, 0xCC, 0x44};
for (int size = 1; size <= 4; size++) {
Verify(false, sigs.i_i(), code, code + size, kAppendEnd);
for (size_t size = 1; size <= 4; ++size) {
Verify(false, sigs.i_i(), {code, size}, kAppendEnd);
// Should also fail without the trailing 'end' opcode.
Verify(false, sigs.i_i(), code, code + size, kOmitEnd);
Verify(false, sigs.i_i(), {code, size}, kOmitEnd);
}
}
......@@ -529,15 +523,15 @@ TEST_F(FunctionBodyDecoderTest, TeeLocalN_local) {
}
TEST_F(FunctionBodyDecoderTest, BlockN) {
const int kMaxSize = 200;
constexpr size_t kMaxSize = 200;
byte buffer[kMaxSize + 3];
for (int i = 0; i <= kMaxSize; i++) {
for (size_t i = 0; i <= kMaxSize; i++) {
memset(buffer, kExprNop, sizeof(buffer));
buffer[0] = kExprBlock;
buffer[1] = kLocalVoid;
buffer[i + 2] = kExprEnd;
Verify(true, sigs.v_i(), buffer, buffer + i + 3, kAppendEnd);
Verify(true, sigs.v_i(), {buffer, i + 3}, kAppendEnd);
}
}
......@@ -684,8 +678,8 @@ TEST_F(FunctionBodyDecoderTest, BlockN_off_end) {
byte code[] = {WASM_BLOCK(kExprNop, kExprNop, kExprNop, kExprNop)};
EXPECT_VERIFIES_C(v_v, code);
for (size_t i = 1; i < arraysize(code); i++) {
Verify(false, sigs.v_v(), code, code + i, kAppendEnd);
Verify(false, sigs.v_v(), code, code + i, kOmitEnd);
Verify(false, sigs.v_v(), {code, i}, kAppendEnd);
Verify(false, sigs.v_v(), {code, i}, kOmitEnd);
}
}
......@@ -1016,8 +1010,8 @@ TEST_F(FunctionBodyDecoderTest, If_off_end) {
static const byte kCode[] = {
WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))};
for (size_t len = 3; len < arraysize(kCode); len++) {
Verify(false, sigs.i_i(), kCode, kCode + len, kAppendEnd);
Verify(false, sigs.i_i(), kCode, kCode + len, kOmitEnd);
Verify(false, sigs.i_i(), {kCode, len}, kAppendEnd);
Verify(false, sigs.i_i(), {kCode, len}, kOmitEnd);
}
}
......@@ -1619,7 +1613,7 @@ TEST_F(FunctionBodyDecoderTest, IncompleteIndirectCall) {
module = builder.module();
static byte code[] = {kExprCallIndirect};
Verify(false, sig, code, code + arraysize(code), kOmitEnd);
Verify(false, sig, ArrayVector(code), kOmitEnd);
}
TEST_F(FunctionBodyDecoderTest, IncompleteStore) {
......@@ -1630,7 +1624,7 @@ TEST_F(FunctionBodyDecoderTest, IncompleteStore) {
module = builder.module();
static byte code[] = {kExprI32StoreMem};
Verify(false, sig, code, code + arraysize(code), kOmitEnd);
Verify(false, sig, ArrayVector(code), kOmitEnd);
}
TEST_F(FunctionBodyDecoderTest, IncompleteS8x16Shuffle) {
......@@ -1643,7 +1637,7 @@ TEST_F(FunctionBodyDecoderTest, IncompleteS8x16Shuffle) {
static byte code[] = {kSimdPrefix,
static_cast<byte>(kExprS8x16Shuffle & 0xff)};
Verify(false, sig, code, code + arraysize(code), kOmitEnd);
Verify(false, sig, ArrayVector(code), kOmitEnd);
}
TEST_F(FunctionBodyDecoderTest, SimpleImportCalls) {
......@@ -2219,8 +2213,8 @@ TEST_F(FunctionBodyDecoderTest, BrTable2b) {
TEST_F(FunctionBodyDecoderTest, BrTable_off_end) {
static byte code[] = {B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0)))};
for (size_t len = 1; len < sizeof(code); len++) {
Verify(false, sigs.i_i(), code, code + len, kAppendEnd);
Verify(false, sigs.i_i(), code, code + len, kOmitEnd);
Verify(false, sigs.i_i(), {code, len}, kAppendEnd);
Verify(false, sigs.i_i(), {code, len}, kOmitEnd);
}
}
......@@ -2715,13 +2709,10 @@ TEST_F(FunctionBodyDecoderTest, IfParam) {
TEST_F(FunctionBodyDecoderTest, Regression709741) {
AddLocals(kWasmI32, kV8MaxWasmFunctionLocals - 1);
EXPECT_VERIFIES(v_v, WASM_NOP);
byte code[] = {WASM_NOP};
const byte* start = code;
const byte* end = code + sizeof(code);
PrepareBytecode(&start, &end, kAppendEnd);
byte code[] = {WASM_NOP, WASM_END};
for (const byte* i = start; i < end; i++) {
FunctionBody body(sigs.v_v(), 0, start, i);
for (size_t i = 0; i < arraysize(code); ++i) {
FunctionBody body(sigs.v_v(), 0, code, code + i);
WasmFeatures unused_detected_features;
DecodeResult result =
VerifyWasmCode(zone()->allocator(), kAllWasmFeatures, nullptr,
......@@ -2753,7 +2744,7 @@ TEST_F(FunctionBodyDecoderTest, MemoryInitInvalid) {
byte code[] = {WASM_MEMORY_INIT(0, WASM_ZERO, WASM_ZERO, WASM_ZERO),
WASM_END};
for (size_t i = 0; i <= arraysize(code); ++i) {
Verify(i == arraysize(code), sigs.v_v(), code, code + i, kOmitEnd);
Verify(i == arraysize(code), sigs.v_v(), {code, i}, kOmitEnd);
}
}
......@@ -2812,7 +2803,7 @@ TEST_F(FunctionBodyDecoderTest, TableInitInvalid) {
WASM_FEATURE_SCOPE(bulk_memory);
byte code[] = {WASM_TABLE_INIT(0, WASM_ZERO, WASM_ZERO, WASM_ZERO), WASM_END};
for (size_t i = 0; i <= arraysize(code); ++i) {
Verify(i == arraysize(code), sigs.v_v(), code, code + i, kOmitEnd);
Verify(i == arraysize(code), sigs.v_v(), {code, i}, kOmitEnd);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment