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"
|
|
|
|
#include "src/wasm/encoder.h"
|
2016-09-12 12:26:37 +00:00
|
|
|
#include "src/wasm/wasm-interpreter.h"
|
2016-08-29 13:55:41 +00:00
|
|
|
#include "src/wasm/wasm-module.h"
|
|
|
|
#include "test/cctest/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-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) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
v8::base::AccountingAllocator allocator;
|
|
|
|
v8::internal::Zone zone(&allocator);
|
|
|
|
|
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
|
|
|
|
|
|
|
uint16_t f1_index = builder.AddFunction();
|
2016-09-12 12:26:37 +00:00
|
|
|
WasmFunctionBuilder* f = builder.FunctionAt(f1_index);
|
2016-08-29 13:55:41 +00:00
|
|
|
f->SetSignature(sigs.i_iii());
|
|
|
|
f->EmitCode(data, static_cast<uint32_t>(size));
|
|
|
|
f->SetExported();
|
|
|
|
f->SetName("main", 4);
|
|
|
|
|
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(
|
2016-09-15 16:19:48 +00:00
|
|
|
i_isolate, &zone, &interpreter_thrower, buffer.begin(), buffer.end(),
|
2016-09-12 22:11:12 +00:00
|
|
|
v8::internal::wasm::ModuleOrigin::kWasmOrigin));
|
2016-09-12 12:26:37 +00:00
|
|
|
|
|
|
|
if (module == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t result_interpreted;
|
|
|
|
{
|
|
|
|
WasmVal args[] = {WasmVal(1), WasmVal(2), WasmVal(3)};
|
|
|
|
result_interpreted = testing::InterpretWasmModule(
|
2016-09-15 16:19:48 +00:00
|
|
|
i_isolate, &interpreter_thrower, module.get(), 0, args);
|
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-09-12 12:26:37 +00:00
|
|
|
module.get());
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
if (result_interpreted == 0xdeadbeef) {
|
|
|
|
CHECK(i_isolate->has_pending_exception());
|
|
|
|
i_isolate->clear_pending_exception();
|
|
|
|
} else {
|
|
|
|
CHECK_EQ(result_interpreted, result_compiled);
|
|
|
|
}
|
2016-08-29 13:55:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|