2016-08-29 13:55:41 +00:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "include/v8.h"
|
|
|
|
#include "src/isolate.h"
|
2016-09-22 12:54:52 +00:00
|
|
|
#include "src/objects.h"
|
2016-11-21 18:00:04 +00:00
|
|
|
#include "src/ostreams.h"
|
2016-09-12 12:26:37 +00:00
|
|
|
#include "src/wasm/wasm-interpreter.h"
|
2016-09-29 11:29:05 +00:00
|
|
|
#include "src/wasm/wasm-module-builder.h"
|
2016-08-29 13:55:41 +00:00
|
|
|
#include "src/wasm/wasm-module.h"
|
2016-10-05 11:59:47 +00:00
|
|
|
#include "test/common/wasm/test-signatures.h"
|
2016-09-14 10:31:23 +00:00
|
|
|
#include "test/common/wasm/wasm-module-runner.h"
|
2016-08-29 13:55:41 +00:00
|
|
|
#include "test/fuzzer/fuzzer-support.h"
|
|
|
|
|
2016-09-22 12:54:52 +00:00
|
|
|
#define WASM_CODE_FUZZER_HASH_SEED 83
|
|
|
|
|
2016-09-12 12:26:37 +00:00
|
|
|
using namespace v8::internal::wasm;
|
|
|
|
|
2016-08-29 13:55:41 +00:00
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
2016-11-21 18:00:04 +00:00
|
|
|
// Save the flag so that we can change it and restore it later.
|
|
|
|
bool generate_test = v8::internal::FLAG_wasm_code_fuzzer_gen_test;
|
|
|
|
if (generate_test) {
|
|
|
|
v8::internal::OFStream os(stdout);
|
|
|
|
|
|
|
|
os << "// Copyright 2016 the V8 project authors. All rights reserved."
|
|
|
|
<< std::endl;
|
|
|
|
os << "// Use of this source code is governed by a BSD-style license that "
|
|
|
|
"can be"
|
|
|
|
<< std::endl;
|
|
|
|
os << "// found in the LICENSE file." << std::endl;
|
|
|
|
os << std::endl;
|
|
|
|
os << "// Flags: --expose-wasm" << std::endl;
|
|
|
|
os << std::endl;
|
|
|
|
os << "load(\"test/mjsunit/wasm/wasm-constants.js\");" << std::endl;
|
|
|
|
os << "load(\"test/mjsunit/wasm/wasm-module-builder.js\");" << std::endl;
|
|
|
|
os << std::endl;
|
|
|
|
os << "(function() {" << std::endl;
|
|
|
|
os << " var builder = new WasmModuleBuilder();" << std::endl;
|
|
|
|
os << " builder.addFunction(\"test\", kSig_i_iii)" << std::endl;
|
|
|
|
os << " .addBody([" << std::endl;
|
|
|
|
}
|
2016-08-29 13:55:41 +00:00
|
|
|
v8_fuzzer::FuzzerSupport* support = v8_fuzzer::FuzzerSupport::Get();
|
|
|
|
v8::Isolate* isolate = support->GetIsolate();
|
|
|
|
v8::internal::Isolate* i_isolate =
|
|
|
|
reinterpret_cast<v8::internal::Isolate*>(isolate);
|
|
|
|
|
|
|
|
// Clear any pending exceptions from a prior run.
|
|
|
|
if (i_isolate->has_pending_exception()) {
|
|
|
|
i_isolate->clear_pending_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Context::Scope context_scope(support->GetContext());
|
|
|
|
v8::TryCatch try_catch(isolate);
|
|
|
|
|
2016-09-20 16:07:25 +00:00
|
|
|
v8::internal::AccountingAllocator allocator;
|
2016-10-17 12:12:30 +00:00
|
|
|
v8::internal::Zone zone(&allocator, ZONE_NAME);
|
2016-08-29 13:55:41 +00:00
|
|
|
|
2016-09-12 12:26:37 +00:00
|
|
|
TestSignatures sigs;
|
2016-08-29 13:55:41 +00:00
|
|
|
|
2016-09-12 12:26:37 +00:00
|
|
|
WasmModuleBuilder builder(&zone);
|
2016-08-29 13:55:41 +00:00
|
|
|
|
2016-09-27 20:46:10 +00:00
|
|
|
v8::internal::wasm::WasmFunctionBuilder* f =
|
|
|
|
builder.AddFunction(sigs.i_iii());
|
2016-08-29 13:55:41 +00:00
|
|
|
f->EmitCode(data, static_cast<uint32_t>(size));
|
2017-01-16 11:27:20 +00:00
|
|
|
uint8_t end_opcode = kExprEnd;
|
|
|
|
f->EmitCode(&end_opcode, 1);
|
2016-10-11 10:35:49 +00:00
|
|
|
f->ExportAs(v8::internal::CStrVector("main"));
|
2016-08-29 13:55:41 +00:00
|
|
|
|
2016-09-12 12:26:37 +00:00
|
|
|
ZoneBuffer buffer(&zone);
|
2016-08-29 13:55:41 +00:00
|
|
|
builder.WriteTo(buffer);
|
|
|
|
|
2016-09-17 01:30:09 +00:00
|
|
|
v8::internal::wasm::testing::SetupIsolateForWasmModule(i_isolate);
|
2016-09-12 12:26:37 +00:00
|
|
|
|
|
|
|
v8::internal::HandleScope scope(i_isolate);
|
|
|
|
|
|
|
|
ErrorThrower interpreter_thrower(i_isolate, "Interpreter");
|
|
|
|
std::unique_ptr<const WasmModule> module(testing::DecodeWasmModuleForTesting(
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
i_isolate, &interpreter_thrower, buffer.begin(), buffer.end(),
|
2016-11-14 19:45:16 +00:00
|
|
|
v8::internal::wasm::ModuleOrigin::kWasmOrigin, true));
|
2016-09-12 12:26:37 +00:00
|
|
|
|
2016-11-21 18:00:04 +00:00
|
|
|
// Clear the flag so that the WebAssembly code is not printed twice.
|
|
|
|
v8::internal::FLAG_wasm_code_fuzzer_gen_test = false;
|
2016-09-12 12:26:37 +00:00
|
|
|
if (module == nullptr) {
|
2016-11-21 18:00:04 +00:00
|
|
|
if (generate_test) {
|
|
|
|
v8::internal::OFStream os(stdout);
|
|
|
|
os << " ])" << std::endl;
|
|
|
|
os << " .exportFunc();" << std::endl;
|
|
|
|
os << " assertThrows(function() { builder.instantiate(); });"
|
|
|
|
<< std::endl;
|
|
|
|
os << "})();" << std::endl;
|
|
|
|
}
|
2016-09-12 12:26:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-11-21 18:00:04 +00:00
|
|
|
if (generate_test) {
|
|
|
|
v8::internal::OFStream os(stdout);
|
|
|
|
os << " ])" << std::endl;
|
|
|
|
os << " .exportFunc();" << std::endl;
|
|
|
|
os << " var module = builder.instantiate();" << std::endl;
|
|
|
|
os << " module.exports.test(1, 2, 3);" << std::endl;
|
|
|
|
os << "})();" << std::endl;
|
|
|
|
}
|
|
|
|
|
2016-11-30 15:02:40 +00:00
|
|
|
ModuleWireBytes wire_bytes(buffer.begin(), buffer.end());
|
2016-09-12 12:26:37 +00:00
|
|
|
int32_t result_interpreted;
|
2016-10-20 14:27:23 +00:00
|
|
|
bool possible_nondeterminism = false;
|
2016-09-12 12:26:37 +00:00
|
|
|
{
|
|
|
|
WasmVal args[] = {WasmVal(1), WasmVal(2), WasmVal(3)};
|
|
|
|
result_interpreted = testing::InterpretWasmModule(
|
2016-11-30 15:02:40 +00:00
|
|
|
i_isolate, &interpreter_thrower, module.get(), wire_bytes, 0, args,
|
2016-10-20 14:27:23 +00:00
|
|
|
&possible_nondeterminism);
|
2016-09-12 12:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorThrower compiler_thrower(i_isolate, "Compiler");
|
|
|
|
v8::internal::Handle<v8::internal::JSObject> instance =
|
2016-09-15 16:19:48 +00:00
|
|
|
testing::InstantiateModuleForTesting(i_isolate, &compiler_thrower,
|
2016-11-30 15:02:40 +00:00
|
|
|
module.get(), wire_bytes);
|
2016-11-21 18:00:04 +00:00
|
|
|
// Restore the flag.
|
|
|
|
v8::internal::FLAG_wasm_code_fuzzer_gen_test = generate_test;
|
2016-09-12 12:26:37 +00:00
|
|
|
if (!interpreter_thrower.error()) {
|
|
|
|
CHECK(!instance.is_null());
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t result_compiled;
|
|
|
|
{
|
|
|
|
v8::internal::Handle<v8::internal::Object> arguments[] = {
|
2016-09-14 08:26:26 +00:00
|
|
|
v8::internal::handle(v8::internal::Smi::FromInt(1), i_isolate),
|
|
|
|
v8::internal::handle(v8::internal::Smi::FromInt(2), i_isolate),
|
|
|
|
v8::internal::handle(v8::internal::Smi::FromInt(3), i_isolate)};
|
2016-09-12 12:26:37 +00:00
|
|
|
result_compiled = testing::CallWasmFunctionForTesting(
|
2016-09-15 16:19:48 +00:00
|
|
|
i_isolate, instance, &compiler_thrower, "main", arraysize(arguments),
|
2016-09-12 22:11:12 +00:00
|
|
|
arguments, v8::internal::wasm::ModuleOrigin::kWasmOrigin);
|
2016-09-12 12:26:37 +00:00
|
|
|
}
|
2016-11-10 12:50:51 +00:00
|
|
|
if (result_interpreted == bit_cast<int32_t>(0xdeadbeef)) {
|
2016-09-12 12:26:37 +00:00
|
|
|
CHECK(i_isolate->has_pending_exception());
|
|
|
|
i_isolate->clear_pending_exception();
|
|
|
|
} else {
|
2016-10-20 14:27:23 +00:00
|
|
|
// The WebAssembly spec allows the sign bit of NaN to be non-deterministic.
|
|
|
|
// This sign bit may cause result_interpreted to be different than
|
|
|
|
// result_compiled. Therefore we do not check the equality of the results
|
|
|
|
// if the execution may have produced a NaN at some point.
|
|
|
|
if (!possible_nondeterminism && (result_interpreted != result_compiled)) {
|
2016-10-04 12:17:54 +00:00
|
|
|
V8_Fatal(__FILE__, __LINE__, "WasmCodeFuzzerHash=%x",
|
|
|
|
v8::internal::StringHasher::HashSequentialString(
|
|
|
|
data, static_cast<int>(size), WASM_CODE_FUZZER_HASH_SEED));
|
2016-09-22 12:54:52 +00:00
|
|
|
}
|
2016-09-12 12:26:37 +00:00
|
|
|
}
|
2016-08-29 13:55:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|