[wasm-c-api] Add test for traps

Change-Id: I51c25c3e41d11ac2419aac1041fe92192f8fdb9e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1698382
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62688}
This commit is contained in:
Jakob Kummerow 2019-07-12 16:52:32 +02:00 committed by Commit Bot
parent 23583f0aa7
commit 6f05773467
5 changed files with 74 additions and 4 deletions

View File

@ -230,6 +230,8 @@ constexpr Vector<const uint8_t> StaticCharVector(const char (&array)[N]) {
return Vector<const uint8_t>::cast(Vector<const char>(array, N - 1));
}
// The resulting vector does not contain a null-termination byte. If you want
// the null byte, use ArrayVector("foo").
inline Vector<const char> CStrVector(const char* data) {
return Vector<const char>(data, strlen(data));
}
@ -250,6 +252,9 @@ inline Vector<char> MutableCStrVector(char* data, size_t max) {
return Vector<char>(data, strnlen(data, max));
}
// For string literals, ArrayVector("foo") returns a vector ['f', 'o', 'o', \0]
// with length 4 and null-termination.
// If you want ['f', 'o', 'o'], use CStrVector("foo").
template <typename T, size_t N>
inline constexpr Vector<T> ArrayVector(T (&arr)[N]) {
return Vector<T>{arr, N};

View File

@ -1134,8 +1134,12 @@ auto ExportType::type() const -> const ExternType* {
i::Handle<i::String> VecToString(i::Isolate* isolate,
const vec<byte_t>& chars) {
size_t length = chars.size();
// Some, but not all, {chars} vectors we get here are null-terminated,
// so let's be robust to that.
if (length > 0 && chars[length - 1] == 0) length--;
return isolate->factory()
->NewStringFromUtf8({chars.get(), chars.size()})
->NewStringFromUtf8({chars.get(), length})
.ToHandleChecked();
}

View File

@ -38,6 +38,7 @@ v8_executable("wasm_api_tests") {
"serialize.cc",
"table.cc",
"threads.cc",
"traps.cc",
"wasm-api-test.h",
]
}

View File

@ -43,7 +43,7 @@ class WasmCapiCallbacksTest : public WasmCapiTest {
// Build the following function:
// int32 stage1(int32 arg0) { return stage2(arg0); }
uint32_t stage2_index =
builder()->AddImport(ArrayVector("stage2"), wasm_i_i_sig());
builder()->AddImport(CStrVector("stage2"), wasm_i_i_sig());
byte code[] = {WASM_CALL_FUNCTION(stage2_index, WASM_GET_LOCAL(0))};
AddExportedFunction(CStrVector("stage1"), code, sizeof(code));
@ -81,7 +81,7 @@ TEST_F(WasmCapiCallbacksTest, GC) {
// Build the following function:
// int32 stage3_to4(int32 arg0) { return stage4(arg0); }
uint32_t stage4_index =
builder()->AddImport(ArrayVector("stage4"), wasm_i_i_sig());
builder()->AddImport(CStrVector("stage4"), wasm_i_i_sig());
byte code[] = {WASM_CALL_FUNCTION(stage4_index, WASM_GET_LOCAL(0))};
AddExportedFunction(CStrVector("stage3_to4"), code, sizeof(code));
@ -134,7 +134,7 @@ TEST_F(WasmCapiTest, Recursion) {
// return fibonacci_c(arg0 - 1) + fibonacci_c(arg0 - 2);
// }
uint32_t fibo_c_index =
builder()->AddImport(ArrayVector("fibonacci_c"), wasm_i_i_sig());
builder()->AddImport(CStrVector("fibonacci_c"), wasm_i_i_sig());
byte code_fibo[] = {
WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_ZERO),
WASM_RETURN1(WASM_ZERO)),

View File

@ -0,0 +1,60 @@
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "test/wasm-api-tests/wasm-api-test.h"
#include <iostream>
namespace v8 {
namespace internal {
namespace wasm {
using ::wasm::Message;
namespace {
own<Trap*> FailCallback(void* env, const Val args[], Val results[]) {
Store* store = reinterpret_cast<Store*>(env);
Message message = Message::make(std::string("callback abort"));
return Trap::make(store, message);
}
void ExpectMessage(const char* expected, const Message& message) {
size_t len = strlen(expected);
EXPECT_EQ(len, message.size());
EXPECT_EQ(0, strncmp(expected, message.get(), len));
}
} // namespace
TEST_F(WasmCapiTest, Traps) {
ValueType i32_type[] = {kWasmI32};
FunctionSig sig(1, 0, i32_type);
uint32_t callback_index = builder()->AddImport(CStrVector("callback"), &sig);
byte code[] = {WASM_CALL_FUNCTION0(callback_index)};
AddExportedFunction(CStrVector("callback"), code, sizeof(code), &sig);
byte code2[] = {WASM_UNREACHABLE, WASM_I32V_1(1)};
AddExportedFunction(CStrVector("unreachable"), code2, sizeof(code2), &sig);
own<FuncType*> func_type = FuncType::make(
vec<ValType*>::make(), vec<ValType*>::make(ValType::make(::wasm::I32)));
own<Func*> cpp_callback = Func::make(store(), func_type.get(), FailCallback,
reinterpret_cast<void*>(store()));
Extern* imports[] = {cpp_callback.get()};
Instantiate(imports);
Func* cpp_trapping_func = GetExportedFunction(0);
own<Trap*> cpp_trap = cpp_trapping_func->call();
EXPECT_NE(nullptr, cpp_trap.get());
ExpectMessage("Uncaught Error: callback abort", cpp_trap->message());
Func* wasm_trapping_func = GetExportedFunction(1);
own<Trap*> wasm_trap = wasm_trapping_func->call();
EXPECT_NE(nullptr, wasm_trap.get());
ExpectMessage("Uncaught RuntimeError: unreachable", wasm_trap->message());
}
} // namespace wasm
} // namespace internal
} // namespace v8