1719ecb9dc
Like CSP flag 'unsafe-eval', which communicates if both JS source files and WASM binary files may be compiled, this CL adds a similar flag for the compilation of WASM binary files. That is, a WASM binary file will be compiled only if the new flag is defined, or the flag for 'unsafe-eval' allows it. These flags are implemented as callback functions on the isolate. The callbacks get a (CSP) context, and a string, and returns the corresponding value of the flag. Both callbacks are initialized with the nullptr, and is used to communicate that no CSP policy is defined. This allows this concept to work, independent of it running in Chrome. It also does a small clean up in api.cc to use macro CALLER_SETTERS, instead of explicit code when appropriate. Bug: v8:7041 Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng Change-Id: Idb3356574ae2a298057e6b7bccbd3492831952ae Reviewed-on: https://chromium-review.googlesource.com/759162 Reviewed-by: Bill Budge <bbudge@chromium.org> Reviewed-by: Eric Holk <eholk@chromium.org> Commit-Queue: Karl Schimpf <kschimpf@chromium.org> Cr-Commit-Position: refs/heads/master@{#49243}
102 lines
3.5 KiB
C++
102 lines
3.5 KiB
C++
// 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.
|
|
|
|
#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; }
|
|
|
|
typedef bool (*CallbackFn)(Local<v8::Context>, Local<v8::String>);
|
|
|
|
// 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);
|
|
builder->WriteTo(*buffer);
|
|
}
|
|
|
|
bool TestModule(Isolate* isolate,
|
|
v8::WasmCompiledModule::CallerOwnedBuffer wire_bytes) {
|
|
HandleScope scope(isolate);
|
|
|
|
v8::WasmCompiledModule::CallerOwnedBuffer serialized_module(nullptr, 0);
|
|
MaybeLocal<v8::WasmCompiledModule> module =
|
|
v8::WasmCompiledModule::DeserializeOrCompile(
|
|
reinterpret_cast<v8::Isolate*>(isolate), serialized_module,
|
|
wire_bytes);
|
|
return !module.IsEmpty();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(PropertiesOfCodegenCallbacks) {
|
|
v8::internal::AccountingAllocator allocator;
|
|
Zone zone(&allocator, ZONE_NAME);
|
|
ZoneBuffer buffer(&zone);
|
|
BuildTrivialModule(&zone, &buffer);
|
|
v8::WasmCompiledModule::CallerOwnedBuffer wire_bytes = {buffer.begin(),
|
|
buffer.size()};
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
HandleScope scope(isolate);
|
|
testing::SetupIsolateForWasmModule(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();
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace wasm
|
|
} // namespace internal
|
|
} // namespace v8
|