Line data Source code
1 : // Copyright 2017 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 : // Tests effects of (CSP) "unsafe-eval" and "wasm-eval" callback functions.
6 : //
7 : // Note: These tests are in a separate test file because the tests dynamically
8 : // change the isolate in terms of callbacks allow_code_gen_callback and
9 : // allow_wasm_code_gen_callback.
10 :
11 : #include "src/wasm/wasm-module-builder.h"
12 : #include "src/wasm/wasm-objects-inl.h"
13 : #include "src/wasm/wasm-objects.h"
14 :
15 : #include "test/cctest/cctest.h"
16 : #include "test/common/wasm/wasm-module-runner.h"
17 :
18 : namespace v8 {
19 : namespace internal {
20 : namespace wasm {
21 :
22 : namespace {
23 :
24 : // Possible values for callback pointers.
25 : enum TestValue {
26 : kTestUsingNull, // no callback.
27 : kTestUsingFalse, // callback returning false.
28 : kTestUsingTrue, // callbacl returning true.
29 : };
30 :
31 : constexpr int kNumTestValues = 3;
32 :
33 : const char* TestValueName[kNumTestValues] = {"null", "false", "true"};
34 :
35 : // Defined to simplify iterating over TestValues;
36 : const TestValue AllTestValues[kNumTestValues] = {
37 : kTestUsingNull, kTestUsingFalse, kTestUsingTrue};
38 :
39 : // This matrix holds the results of setting allow_code_gen_callback
40 : // (first index) and allow_wasm_code_gen_callback (second index) using
41 : // TestValue's. The value in the matrix is true if compilation is
42 : // allowed, and false otherwise.
43 : const bool ExpectedResults[kNumTestValues][kNumTestValues] = {
44 : {true, false, true}, {false, false, true}, {true, false, true}};
45 :
46 32 : bool TrueCallback(Local<v8::Context>, Local<v8::String>) { return true; }
47 :
48 32 : bool FalseCallback(Local<v8::Context>, Local<v8::String>) { return false; }
49 :
50 : using CallbackFn = bool (*)(Local<v8::Context>, Local<v8::String>);
51 :
52 : // Defines the Callback to use for the corresponding TestValue.
53 : CallbackFn Callback[kNumTestValues] = {nullptr, FalseCallback, TrueCallback};
54 :
55 4 : void BuildTrivialModule(Zone* zone, ZoneBuffer* buffer) {
56 4 : WasmModuleBuilder* builder = new (zone) WasmModuleBuilder(zone);
57 4 : builder->WriteTo(*buffer);
58 4 : }
59 :
60 36 : bool TestModule(Isolate* isolate, v8::MemorySpan<const uint8_t> wire_bytes) {
61 : HandleScope scope(isolate);
62 :
63 36 : v8::MemorySpan<const uint8_t> serialized_module;
64 : MaybeLocal<v8::WasmModuleObject> module =
65 : v8::WasmModuleObject::DeserializeOrCompile(
66 : reinterpret_cast<v8::Isolate*>(isolate), serialized_module,
67 36 : wire_bytes);
68 72 : return !module.IsEmpty();
69 : }
70 :
71 : } // namespace
72 :
73 26643 : TEST(PropertiesOfCodegenCallbacks) {
74 8 : v8::internal::AccountingAllocator allocator;
75 8 : Zone zone(&allocator, ZONE_NAME);
76 : ZoneBuffer buffer(&zone);
77 4 : BuildTrivialModule(&zone, &buffer);
78 : v8::MemorySpan<const uint8_t> wire_bytes = {buffer.begin(), buffer.size()};
79 : Isolate* isolate = CcTest::InitIsolateOnce();
80 : HandleScope scope(isolate);
81 4 : testing::SetupIsolateForWasmModule(isolate);
82 :
83 28 : for (TestValue codegen : AllTestValues) {
84 84 : for (TestValue wasm_codegen : AllTestValues) {
85 36 : fprintf(stderr, "Test codegen = %s, wasm_codegen = %s\n",
86 36 : TestValueName[codegen], TestValueName[wasm_codegen]);
87 36 : isolate->set_allow_code_gen_callback(Callback[codegen]);
88 36 : isolate->set_allow_wasm_code_gen_callback(Callback[wasm_codegen]);
89 36 : bool found = TestModule(isolate, wire_bytes);
90 36 : bool expected = ExpectedResults[codegen][wasm_codegen];
91 36 : CHECK_EQ(expected, found);
92 36 : CcTest::CollectAllAvailableGarbage();
93 : }
94 : }
95 4 : }
96 :
97 : } // namespace wasm
98 : } // namespace internal
99 79917 : } // namespace v8
|