test-wasm-codegen.cc 4.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2017 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.

// Tests effects of (CSP) "unsafe-eval" and "wasm-eval" callback functions.
//
// Note: These tests are in a separate test file because the tests dynamically
// change the isolate in terms of callbacks allow_code_gen_callback and
// allow_wasm_code_gen_callback.

11
#include "src/api/api-inl.h"
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
#include "src/wasm/wasm-module-builder.h"
#include "src/wasm/wasm-objects-inl.h"
#include "src/wasm/wasm-objects.h"

#include "test/cctest/cctest.h"
#include "test/common/wasm/wasm-module-runner.h"

namespace v8 {
namespace internal {
namespace wasm {

namespace {

// Possible values for callback pointers.
enum TestValue {
  kTestUsingNull,   // no callback.
  kTestUsingFalse,  // callback returning false.
  kTestUsingTrue,   // callbacl returning true.
};

constexpr int kNumTestValues = 3;

const char* TestValueName[kNumTestValues] = {"null", "false", "true"};

// Defined to simplify iterating over TestValues;
const TestValue AllTestValues[kNumTestValues] = {
    kTestUsingNull, kTestUsingFalse, kTestUsingTrue};

// This matrix holds the results of setting allow_code_gen_callback
// (first index) and allow_wasm_code_gen_callback (second index) using
// TestValue's. The value in the matrix is true if compilation is
// allowed, and false otherwise.
const bool ExpectedResults[kNumTestValues][kNumTestValues] = {
    {true, false, true}, {false, false, true}, {true, false, true}};

bool TrueCallback(Local<v8::Context>, Local<v8::String>) { return true; }

bool FalseCallback(Local<v8::Context>, Local<v8::String>) { return false; }

51
using CallbackFn = bool (*)(Local<v8::Context>, Local<v8::String>);
52 53 54 55 56 57

// Defines the Callback to use for the corresponding TestValue.
CallbackFn Callback[kNumTestValues] = {nullptr, FalseCallback, TrueCallback};

void BuildTrivialModule(Zone* zone, ZoneBuffer* buffer) {
  WasmModuleBuilder* builder = new (zone) WasmModuleBuilder(zone);
58
  builder->WriteTo(buffer);
59 60
}

61
bool TestModule(Isolate* isolate, v8::MemorySpan<const uint8_t> wire_bytes) {
62
  HandleScope scope(isolate);
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
  v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
  v8::Local<v8::Context> context =
      Utils::ToLocal(Handle<Context>::cast(isolate->native_context()));

  // Get the "WebAssembly.Module" function.
  auto get_property = [context, v8_isolate](
                          v8::Local<v8::Object> obj,
                          const char* property_name) -> v8::Local<v8::Object> {
    auto name = v8::String::NewFromUtf8(v8_isolate, property_name,
                                        NewStringType::kInternalized)
                    .ToLocalChecked();
    return obj->Get(context, name).ToLocalChecked().As<v8::Object>();
  };
  auto wasm_class = get_property(context->Global(), "WebAssembly");
  auto module_class = get_property(wasm_class, "Module");

  // Create an arraybuffer with the wire bytes.
  v8::Local<v8::ArrayBuffer> buf =
      v8::ArrayBuffer::New(v8_isolate, wire_bytes.size());
  memcpy(static_cast<uint8_t*>(buf->GetBackingStore()->Data()),
         wire_bytes.data(), wire_bytes.size());

  // Now call the "WebAssembly.Module" function with the array buffer. Return
  // true if this succeeded, false otherwise.
  v8::TryCatch try_catch(v8_isolate);
  v8::Local<v8::Value> args[] = {buf};
  MaybeLocal<Value> module_object =
      module_class->CallAsConstructor(context, arraysize(args), args);

  CHECK_EQ(try_catch.HasCaught(), module_object.IsEmpty());
  return !module_object.IsEmpty();
94 95 96 97 98 99 100 101 102
}

}  // namespace

TEST(PropertiesOfCodegenCallbacks) {
  v8::internal::AccountingAllocator allocator;
  Zone zone(&allocator, ZONE_NAME);
  ZoneBuffer buffer(&zone);
  BuildTrivialModule(&zone, &buffer);
103
  v8::MemorySpan<const uint8_t> wire_bytes = {buffer.begin(), buffer.size()};
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  Isolate* isolate = CcTest::InitIsolateOnce();
  HandleScope scope(isolate);

  for (TestValue codegen : AllTestValues) {
    for (TestValue wasm_codegen : AllTestValues) {
      fprintf(stderr, "Test codegen = %s, wasm_codegen = %s\n",
              TestValueName[codegen], TestValueName[wasm_codegen]);
      isolate->set_allow_code_gen_callback(Callback[codegen]);
      isolate->set_allow_wasm_code_gen_callback(Callback[wasm_codegen]);
      bool found = TestModule(isolate, wire_bytes);
      bool expected = ExpectedResults[codegen][wasm_codegen];
      CHECK_EQ(expected, found);
      CcTest::CollectAllAvailableGarbage();
    }
  }
}

121 122 123 124 125 126 127 128 129
TEST(WasmModuleObjectCompileFailure) {
  const uint8_t wire_bytes_arr[] = {0xDE, 0xAD, 0xBE, 0xEF};
  v8::MemorySpan<const uint8_t> wire_bytes = {wire_bytes_arr,
                                              arraysize(wire_bytes_arr)};
  Isolate* isolate = CcTest::InitIsolateOnce();
  HandleScope scope(isolate);
  CHECK(!TestModule(isolate, wire_bytes));
}

130 131 132
}  // namespace wasm
}  // namespace internal
}  // namespace v8