callbacks.cc 7.96 KB
Newer Older
1 2 3 4
// 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.

5 6
#include "test/wasm-api-tests/wasm-api-test.h"

7 8 9 10 11 12 13 14 15 16
#include "src/execution/isolate.h"
#include "src/heap/heap.h"
#include "src/wasm/c-api.h"

namespace v8 {
namespace internal {
namespace wasm {

namespace {

17
own<Trap> Stage2(void* env, const Val args[], Val results[]) {
18 19 20
  printf("Stage2...\n");
  WasmCapiTest* self = reinterpret_cast<WasmCapiTest*>(env);
  Func* stage3 = self->GetExportedFunction(1);
21
  own<Trap> trap = stage3->call(args, results);
22 23
  if (trap) {
    printf("Stage2: got exception: %s\n", trap->message().get());
24 25 26
  } else {
    printf("Stage2: call successful\n");
  }
27
  return trap;
28 29
}

30
own<Trap> Stage4_GC(void* env, const Val args[], Val results[]) {
31 32 33
  printf("Stage4...\n");
  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(env);
  isolate->heap()->PreciseCollectAllGarbage(
34 35
      i::Heap::kForcedGC, i::GarbageCollectionReason::kTesting,
      v8::kNoGCCallbackFlags);
36 37 38 39
  results[0] = Val::i32(args[0].i32() + 1);
  return nullptr;
}

40 41 42 43 44 45
class WasmCapiCallbacksTest : public WasmCapiTest {
 public:
  WasmCapiCallbacksTest() : WasmCapiTest() {
    // Build the following function:
    // int32 stage1(int32 arg0) { return stage2(arg0); }
    uint32_t stage2_index =
46
        builder()->AddImport(CStrVector("stage2"), wasm_i_i_sig());
47
    byte code[] = {WASM_CALL_FUNCTION(stage2_index, WASM_LOCAL_GET(0))};
48 49 50
    AddExportedFunction(CStrVector("stage1"), code, sizeof(code));

    stage2_ = Func::make(store(), cpp_i_i_sig(), Stage2, this);
51 52
  }

53
  Func* stage2() { return stage2_.get(); }
54 55 56 57
  void AddExportedFunction(Vector<const char> name, byte code[],
                           size_t code_size) {
    WasmCapiTest::AddExportedFunction(name, code, code_size, wasm_i_i_sig());
  }
58 59

 private:
60
  own<Func> stage2_;
61
};
62

63 64
}  // namespace

65
TEST_F(WasmCapiCallbacksTest, Trap) {
66 67 68 69 70 71
  // Build the following function:
  // int32 stage3_trap(int32 arg0) { unreachable(); }
  byte code[] = {WASM_UNREACHABLE};
  AddExportedFunction(CStrVector("stage3_trap"), code, sizeof(code));

  Extern* imports[] = {stage2()};
72
  Instantiate(imports);
73 74
  Val args[] = {Val::i32(42)};
  Val results[1];
75
  own<Trap> trap = GetExportedFunction(0)->call(args, results);
76 77
  EXPECT_NE(trap, nullptr);
  printf("Stage0: Got trap as expected: %s\n", trap->message().get());
78 79
}

80
TEST_F(WasmCapiCallbacksTest, GC) {
81 82 83
  // Build the following function:
  // int32 stage3_to4(int32 arg0) { return stage4(arg0); }
  uint32_t stage4_index =
84
      builder()->AddImport(CStrVector("stage4"), wasm_i_i_sig());
85
  byte code[] = {WASM_CALL_FUNCTION(stage4_index, WASM_LOCAL_GET(0))};
86 87 88 89
  AddExportedFunction(CStrVector("stage3_to4"), code, sizeof(code));

  i::Isolate* isolate =
      reinterpret_cast<::wasm::StoreImpl*>(store())->i_isolate();
90
  own<Func> stage4 = Func::make(store(), cpp_i_i_sig(), Stage4_GC, isolate);
91 92
  EXPECT_EQ(cpp_i_i_sig()->params().size(), stage4->type()->params().size());
  EXPECT_EQ(cpp_i_i_sig()->results().size(), stage4->type()->results().size());
93
  Extern* imports[] = {stage2(), stage4.get()};
94
  Instantiate(imports);
95 96
  Val args[] = {Val::i32(42)};
  Val results[1];
97
  own<Trap> trap = GetExportedFunction(0)->call(args, results);
98
  EXPECT_EQ(trap, nullptr);
99 100 101
  EXPECT_EQ(43, results[0].i32());
}

102 103
namespace {

104
own<Trap> FibonacciC(void* env, const Val args[], Val results[]) {
105 106 107 108 109 110 111 112 113 114 115
  int32_t x = args[0].i32();
  if (x == 0 || x == 1) {
    results[0] = Val::i32(x);
    return nullptr;
  }
  WasmCapiTest* self = reinterpret_cast<WasmCapiTest*>(env);
  Func* fibo_wasm = self->GetExportedFunction(0);
  // Aggressively re-use existing arrays. That's maybe not great coding
  // style, but this test intentionally ensures that it works if someone
  // insists on doing it.
  Val recursive_args[] = {Val::i32(x - 1)};
116
  own<Trap> trap = fibo_wasm->call(recursive_args, results);
117 118 119 120 121 122 123 124 125 126 127 128
  DCHECK_NULL(trap);
  int32_t x1 = results[0].i32();
  recursive_args[0] = Val::i32(x - 2);
  trap = fibo_wasm->call(recursive_args, results);
  DCHECK_NULL(trap);
  int32_t x2 = results[0].i32();
  results[0] = Val::i32(x1 + x2);
  return nullptr;
}

}  // namespace

129 130 131 132 133 134 135 136
TEST_F(WasmCapiTest, Recursion) {
  // Build the following function:
  // int32 fibonacci_wasm(int32 arg0) {
  //   if (arg0 == 0) return 0;
  //   if (arg0 == 1) return 1;
  //   return fibonacci_c(arg0 - 1) + fibonacci_c(arg0 - 2);
  // }
  uint32_t fibo_c_index =
137
      builder()->AddImport(CStrVector("fibonacci_c"), wasm_i_i_sig());
138
  byte code_fibo[] = {
139
      WASM_IF(WASM_I32_EQ(WASM_LOCAL_GET(0), WASM_ZERO),
140
              WASM_RETURN1(WASM_ZERO)),
141
      WASM_IF(WASM_I32_EQ(WASM_LOCAL_GET(0), WASM_ONE), WASM_RETURN1(WASM_ONE)),
142
      // Muck with the parameter to ensure callers don't depend on its value.
143
      WASM_LOCAL_SET(0, WASM_I32_SUB(WASM_LOCAL_GET(0), WASM_ONE)),
144
      WASM_RETURN1(WASM_I32_ADD(
145
          WASM_CALL_FUNCTION(fibo_c_index, WASM_LOCAL_GET(0)),
146
          WASM_CALL_FUNCTION(fibo_c_index,
147
                             WASM_I32_SUB(WASM_LOCAL_GET(0), WASM_ONE))))};
148
  AddExportedFunction(CStrVector("fibonacci_wasm"), code_fibo,
149
                      sizeof(code_fibo), wasm_i_i_sig());
150

151
  own<Func> fibonacci = Func::make(store(), cpp_i_i_sig(), FibonacciC, this);
152 153
  Extern* imports[] = {fibonacci.get()};
  Instantiate(imports);
154 155 156
  // Enough iterations to make it interesting, few enough to keep it fast.
  Val args[] = {Val::i32(15)};
  Val results[1];
157
  own<Trap> result = GetExportedFunction(0)->call(args, results);
158 159 160 161
  EXPECT_EQ(result, nullptr);
  EXPECT_EQ(610, results[0].i32());
}

162 163
namespace {

164
own<Trap> PlusOne(const Val args[], Val results[]) {
165 166 167 168 169 170 171 172 173 174 175 176 177 178
  int32_t a0 = args[0].i32();
  results[0] = Val::i32(a0 + 1);
  int64_t a1 = args[1].i64();
  results[1] = Val::i64(a1 + 1);
  float a2 = args[2].f32();
  results[2] = Val::f32(a2 + 1);
  double a3 = args[3].f64();
  results[3] = Val::f64(a3 + 1);
  results[4] = Val::ref(args[4].ref()->copy());  // No +1 for Refs.
  return nullptr;
}

}  // namespace

179
TEST_F(WasmCapiTest, DirectCallCapiFunction) {
180 181
  own<FuncType> cpp_sig =
      FuncType::make(ownvec<ValType>::make(
182 183 184
                         ValType::make(::wasm::I32), ValType::make(::wasm::I64),
                         ValType::make(::wasm::F32), ValType::make(::wasm::F64),
                         ValType::make(::wasm::ANYREF)),
185
                     ownvec<ValType>::make(
186 187 188
                         ValType::make(::wasm::I32), ValType::make(::wasm::I64),
                         ValType::make(::wasm::F32), ValType::make(::wasm::F64),
                         ValType::make(::wasm::ANYREF)));
189
  own<Func> func = Func::make(store(), cpp_sig.get(), PlusOne);
190
  Extern* imports[] = {func.get()};
191 192 193
  ValueType wasm_types[] = {kWasmI32,       kWasmI64,      kWasmF32, kWasmF64,
                            kWasmExternRef, kWasmI32,      kWasmI64, kWasmF32,
                            kWasmF64,       kWasmExternRef};
194 195
  FunctionSig wasm_sig(5, 5, wasm_types);
  int func_index = builder()->AddImport(CStrVector("func"), &wasm_sig);
196
  builder()->ExportImportedFunction(CStrVector("func"), func_index);
197 198 199 200 201 202 203 204 205
  Instantiate(imports);
  int32_t a0 = 42;
  int64_t a1 = 0x1234c0ffee;
  float a2 = 1234.5;
  double a3 = 123.45;
  Val args[] = {Val::i32(a0), Val::i64(a1), Val::f32(a2), Val::f64(a3),
                Val::ref(func->copy())};
  Val results[5];
  // Test that {func} can be called directly.
206
  own<Trap> trap = func->call(args, results);
207 208 209 210 211
  EXPECT_EQ(nullptr, trap);
  EXPECT_EQ(a0 + 1, results[0].i32());
  EXPECT_EQ(a1 + 1, results[1].i64());
  EXPECT_EQ(a2 + 1, results[2].f32());
  EXPECT_EQ(a3 + 1, results[3].f64());
212
  EXPECT_TRUE(func->same(results[4].ref()));
213 214 215 216 217 218 219 220

  // Test that {func} can be called after import/export round-tripping.
  trap = GetExportedFunction(0)->call(args, results);
  EXPECT_EQ(nullptr, trap);
  EXPECT_EQ(a0 + 1, results[0].i32());
  EXPECT_EQ(a1 + 1, results[1].i64());
  EXPECT_EQ(a2 + 1, results[2].f32());
  EXPECT_EQ(a3 + 1, results[3].f64());
221
  EXPECT_TRUE(func->same(results[4].ref()));
222 223
}

224 225 226
}  // namespace wasm
}  // namespace internal
}  // namespace v8