1 // Copyright 2019 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "test/wasm-api-tests/wasm-api-test.h"
6 
7 namespace v8 {
8 namespace internal {
9 namespace wasm {
10 
11 namespace {
12 
DummyCallback(const Val args[],Val results[])13 own<Trap> DummyCallback(const Val args[], Val results[]) { return nullptr; }
14 
15 }  // namespace
16 
TEST_F(WasmCapiTest,StartupErrors)17 TEST_F(WasmCapiTest, StartupErrors) {
18   FunctionSig sig(0, 0, nullptr);
19   byte code[] = {WASM_UNREACHABLE};
20   WasmFunctionBuilder* start_func = builder()->AddFunction(&sig);
21   start_func->EmitCode(code, static_cast<uint32_t>(sizeof(code)));
22   start_func->Emit(kExprEnd);
23   builder()->MarkStartFunction(start_func);
24   builder()->AddImport(base::CStrVector("dummy"), &sig);
25   Compile();
26   own<Trap> trap;
27 
28   // Try to make an Instance with non-matching imports.
29   own<Func> bad_func = Func::make(store(), cpp_i_i_sig(), DummyCallback);
30   Extern* bad_imports[] = {bad_func.get()};
31   own<Instance> instance =
32       Instance::make(store(), module(), bad_imports, &trap);
33   EXPECT_EQ(nullptr, instance);
34   EXPECT_NE(nullptr, trap);
35   EXPECT_STREQ(
36       "Uncaught LinkError: instantiation: Import #0 module=\"\" "
37       "function=\"dummy\" "
38       "error: imported function does not match the expected type",
39       trap->message().get());
40   EXPECT_EQ(nullptr, trap->origin());
41   // Don't crash if there is no {trap}.
42   instance = Instance::make(store(), module(), bad_imports, nullptr);
43   EXPECT_EQ(nullptr, instance);
44 
45   // Try to make an instance with a {start} function that traps.
46   own<FuncType> good_sig =
47       FuncType::make(ownvec<ValType>::make(), ownvec<ValType>::make());
48   own<Func> good_func = Func::make(store(), good_sig.get(), DummyCallback);
49   Extern* good_imports[] = {good_func.get()};
50   instance = Instance::make(store(), module(), good_imports, &trap);
51   EXPECT_EQ(nullptr, instance);
52   EXPECT_NE(nullptr, trap);
53   EXPECT_STREQ("Uncaught RuntimeError: unreachable", trap->message().get());
54   EXPECT_NE(nullptr, trap->origin());
55   // Don't crash if there is no {trap}.
56   instance = Instance::make(store(), module(), good_imports, nullptr);
57   EXPECT_EQ(nullptr, instance);
58 }
59 
60 }  // namespace wasm
61 }  // namespace internal
62 }  // namespace v8
63