2017-09-14 17:34:15 +00:00
|
|
|
// 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.
|
|
|
|
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/init/v8.h"
|
2018-10-12 15:16:19 +00:00
|
|
|
#include "src/objects/managed.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/utils/vector.h"
|
2017-09-14 17:34:15 +00:00
|
|
|
#include "src/wasm/module-decoder.h"
|
|
|
|
#include "src/wasm/streaming-decoder.h"
|
2017-12-05 00:28:35 +00:00
|
|
|
#include "src/wasm/wasm-engine.h"
|
2017-09-14 17:34:15 +00:00
|
|
|
#include "src/wasm/wasm-module-builder.h"
|
|
|
|
#include "src/wasm/wasm-module.h"
|
2018-10-12 15:16:19 +00:00
|
|
|
#include "src/wasm/wasm-objects-inl.h"
|
|
|
|
#include "src/wasm/wasm-objects.h"
|
|
|
|
#include "src/wasm/wasm-serialization.h"
|
2017-09-14 17:34:15 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2020-05-25 10:02:01 +00:00
|
|
|
#include "test/common/wasm/flag-utils.h"
|
2017-09-14 17:34:15 +00:00
|
|
|
#include "test/common/wasm/test-signatures.h"
|
|
|
|
#include "test/common/wasm/wasm-macro-gen.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
class MockPlatform final : public TestPlatform {
|
|
|
|
public:
|
2017-11-15 14:19:06 +00:00
|
|
|
MockPlatform() : task_runner_(std::make_shared<MockTaskRunner>()) {
|
2017-09-14 17:34:15 +00:00
|
|
|
// Now that it's completely constructed, make this the current platform.
|
|
|
|
i::V8::SetPlatformForTesting(this);
|
|
|
|
}
|
2017-11-15 14:19:06 +00:00
|
|
|
|
2020-08-05 07:22:27 +00:00
|
|
|
~MockPlatform() {
|
|
|
|
for (auto* job_handle : job_handles_) job_handle->ResetPlatform();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<v8::JobHandle> PostJob(
|
|
|
|
v8::TaskPriority priority,
|
|
|
|
std::unique_ptr<v8::JobTask> job_task) override {
|
Revert "Reland "[wasm]: Use CancelAndDetach and barrier on BackgroundCompileJob.""
This reverts commit 064ee3c8358195dfce7f34e4deaa3f74f0caa325.
Reason for revert: Causing blink_web_tests to fail on builder "WebKit Linux MSAN"
https://bugs.chromium.org/p/chromium/issues/detail?id=1153968
Original change's description:
> Reland "[wasm]: Use CancelAndDetach and barrier on BackgroundCompileJob."
>
> Reason for revert: Data race:
> https://ci.chromium.org/p/v8/builders/ci/V8%20Linux64%20TSAN/34121
>
> It was assume that MockPlatform runs everything on 1 thread. However,
> MockPlatform::PostJob previously would schedule the job through
> TestPlatform, which eventually posts concurrent tasks, thus causing
> data race.
> Fix: Manually calling NewDefaultJobHandle and passing the MockPlatform
> ensures the jobs also run sequentially.
>
> Additional change:
> - CancelAndDetach is now called in ~CompilationStateImpl() to make sure
> it's called in sequence with ScheduleCompileJobForNewUnits
>
> Original CL description:
> To avoid keeping around a list of job handles, CancelAndDetach() is
> used in CancelCompilation. Dependency on WasmEngine is handled by a
> barrier that waits on all jobs to finish.
>
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2498659
> Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
> Reviewed-by: Ulan Degenbaev <ulan@chromium.org>
> Reviewed-by: Clemens Backes <clemensb@chromium.org>
> Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
> Cr-Original-Commit-Position: refs/heads/master@{#71074}
> Change-Id: Ie9556f7f96f6fb9a61ada0e5cbd58d4fb4a0f571
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2559137
> Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
> Reviewed-by: Andreas Haas <ahaas@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#71459}
TBR=ulan@chromium.org,jkummerow@chromium.org,ahaas@chromium.org,clemensb@chromium.org,etiennep@chromium.org
Bug: chromium:1153968, v8:11209, v8:11210, v8:11212
# Not skipping CQ checks because original CL landed > 1 day ago.
Change-Id: I2c8406bea81ee7cf6c5726c2fec50fffdce09611
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2566446
Reviewed-by: Clemens Backes <clemensb@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71519}
2020-12-01 01:01:08 +00:00
|
|
|
auto orig_job_handle = TestPlatform::PostJob(priority, std::move(job_task));
|
2020-08-05 07:22:27 +00:00
|
|
|
auto job_handle =
|
|
|
|
std::make_unique<MockJobHandle>(std::move(orig_job_handle), this);
|
|
|
|
job_handles_.insert(job_handle.get());
|
|
|
|
return job_handle;
|
|
|
|
}
|
|
|
|
|
2017-11-15 14:19:06 +00:00
|
|
|
std::shared_ptr<TaskRunner> GetForegroundTaskRunner(
|
|
|
|
v8::Isolate* isolate) override {
|
|
|
|
return task_runner_;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:44:23 +00:00
|
|
|
void CallOnWorkerThread(std::unique_ptr<v8::Task> task) override {
|
|
|
|
task_runner_->PostTask(std::move(task));
|
2017-09-14 17:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IdleTasksEnabled(v8::Isolate* isolate) override { return false; }
|
|
|
|
|
2020-08-05 07:22:27 +00:00
|
|
|
void ExecuteTasks() {
|
|
|
|
for (auto* job_handle : job_handles_) {
|
2020-10-16 17:53:20 +00:00
|
|
|
if (job_handle->IsValid()) job_handle->Join();
|
2020-08-05 07:22:27 +00:00
|
|
|
}
|
|
|
|
task_runner_->ExecuteTasks();
|
|
|
|
}
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
private:
|
2017-11-15 14:19:06 +00:00
|
|
|
class MockTaskRunner final : public TaskRunner {
|
|
|
|
public:
|
|
|
|
void PostTask(std::unique_ptr<v8::Task> task) override {
|
2020-11-23 18:11:44 +00:00
|
|
|
base::MutexGuard lock_scope(&tasks_lock_);
|
2018-11-06 12:49:05 +00:00
|
|
|
tasks_.push(std::move(task));
|
2017-11-15 14:19:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 15:58:39 +00:00
|
|
|
void PostNonNestableTask(std::unique_ptr<Task> task) override {
|
|
|
|
PostTask(std::move(task));
|
|
|
|
}
|
|
|
|
|
2017-11-15 14:19:06 +00:00
|
|
|
void PostDelayedTask(std::unique_ptr<Task> task,
|
|
|
|
double delay_in_seconds) override {
|
2020-11-02 15:58:39 +00:00
|
|
|
PostTask(std::move(task));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostNonNestableDelayedTask(std::unique_ptr<Task> task,
|
|
|
|
double delay_in_seconds) override {
|
|
|
|
PostTask(std::move(task));
|
2019-02-13 00:33:17 +00:00
|
|
|
}
|
2017-11-15 14:19:06 +00:00
|
|
|
|
|
|
|
void PostIdleTask(std::unique_ptr<IdleTask> task) override {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2019-02-13 00:33:17 +00:00
|
|
|
bool IdleTasksEnabled() override { return false; }
|
2020-11-02 15:58:39 +00:00
|
|
|
bool NonNestableTasksEnabled() const override { return true; }
|
|
|
|
bool NonNestableDelayedTasksEnabled() const override { return true; }
|
2017-11-15 14:19:06 +00:00
|
|
|
|
|
|
|
void ExecuteTasks() {
|
2020-11-23 18:11:44 +00:00
|
|
|
std::queue<std::unique_ptr<v8::Task>> tasks;
|
|
|
|
{
|
|
|
|
base::MutexGuard lock_scope(&tasks_lock_);
|
|
|
|
tasks.swap(tasks_);
|
|
|
|
}
|
|
|
|
while (!tasks.empty()) {
|
|
|
|
std::unique_ptr<Task> task = std::move(tasks.front());
|
|
|
|
tasks.pop();
|
2017-11-15 14:19:06 +00:00
|
|
|
task->Run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-11-23 18:11:44 +00:00
|
|
|
base::Mutex tasks_lock_;
|
2017-11-15 14:19:06 +00:00
|
|
|
// We do not execute tasks concurrently, so we only need one list of tasks.
|
2018-11-06 12:49:05 +00:00
|
|
|
std::queue<std::unique_ptr<v8::Task>> tasks_;
|
2017-11-15 14:19:06 +00:00
|
|
|
};
|
|
|
|
|
2020-08-05 07:22:27 +00:00
|
|
|
class MockJobHandle : public JobHandle {
|
|
|
|
public:
|
|
|
|
explicit MockJobHandle(std::unique_ptr<JobHandle> orig_handle,
|
|
|
|
MockPlatform* platform)
|
|
|
|
: orig_handle_(std::move(orig_handle)), platform_(platform) {}
|
|
|
|
|
|
|
|
~MockJobHandle() {
|
|
|
|
if (platform_) platform_->job_handles_.erase(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResetPlatform() { platform_ = nullptr; }
|
|
|
|
|
|
|
|
void NotifyConcurrencyIncrease() override {
|
|
|
|
orig_handle_->NotifyConcurrencyIncrease();
|
|
|
|
}
|
|
|
|
void Join() override { orig_handle_->Join(); }
|
|
|
|
void Cancel() override { orig_handle_->Cancel(); }
|
2020-10-05 20:27:31 +00:00
|
|
|
void CancelAndDetach() override { orig_handle_->CancelAndDetach(); }
|
2020-08-13 21:14:37 +00:00
|
|
|
bool IsCompleted() override { return orig_handle_->IsCompleted(); }
|
2020-10-30 18:41:00 +00:00
|
|
|
bool IsActive() override { return orig_handle_->IsActive(); }
|
2020-08-05 07:22:27 +00:00
|
|
|
bool IsRunning() override { return orig_handle_->IsRunning(); }
|
2020-10-16 17:53:20 +00:00
|
|
|
bool IsValid() override { return orig_handle_->IsValid(); }
|
2020-08-05 07:22:27 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<JobHandle> orig_handle_;
|
|
|
|
MockPlatform* platform_;
|
|
|
|
};
|
|
|
|
|
2017-11-15 14:19:06 +00:00
|
|
|
std::shared_ptr<MockTaskRunner> task_runner_;
|
2020-08-05 07:22:27 +00:00
|
|
|
std::unordered_set<MockJobHandle*> job_handles_;
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-05-24 21:22:27 +00:00
|
|
|
enum class CompilationState {
|
|
|
|
kPending,
|
|
|
|
kFinished,
|
|
|
|
kFailed,
|
|
|
|
};
|
|
|
|
|
2018-08-02 09:50:08 +00:00
|
|
|
class TestResolver : public CompilationResultResolver {
|
2018-05-24 21:22:27 +00:00
|
|
|
public:
|
2020-11-02 15:58:39 +00:00
|
|
|
TestResolver(i::Isolate* isolate, CompilationState* state,
|
|
|
|
std::string* error_message,
|
2018-10-12 15:16:19 +00:00
|
|
|
std::shared_ptr<NativeModule>* native_module)
|
2020-11-02 15:58:39 +00:00
|
|
|
: isolate_(isolate),
|
|
|
|
state_(state),
|
2019-03-18 15:34:58 +00:00
|
|
|
error_message_(error_message),
|
|
|
|
native_module_(native_module) {}
|
2018-05-24 21:22:27 +00:00
|
|
|
|
|
|
|
void OnCompilationSucceeded(i::Handle<i::WasmModuleObject> module) override {
|
|
|
|
*state_ = CompilationState::kFinished;
|
2018-10-12 15:16:19 +00:00
|
|
|
if (!module.is_null()) {
|
2018-12-11 10:37:32 +00:00
|
|
|
*native_module_ = module->shared_native_module();
|
2018-10-12 15:16:19 +00:00
|
|
|
}
|
2018-05-24 21:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnCompilationFailed(i::Handle<i::Object> error_reason) override {
|
|
|
|
*state_ = CompilationState::kFailed;
|
2019-03-18 15:34:58 +00:00
|
|
|
Handle<String> str =
|
2020-11-02 15:58:39 +00:00
|
|
|
Object::ToString(isolate_, error_reason).ToHandleChecked();
|
2019-03-18 15:34:58 +00:00
|
|
|
error_message_->assign(str->ToCString().get());
|
2018-05-24 21:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-11-02 15:58:39 +00:00
|
|
|
i::Isolate* isolate_;
|
2019-03-18 15:34:58 +00:00
|
|
|
CompilationState* const state_;
|
|
|
|
std::string* const error_message_;
|
|
|
|
std::shared_ptr<NativeModule>* const native_module_;
|
2018-05-24 21:22:27 +00:00
|
|
|
};
|
|
|
|
|
2017-09-14 17:34:15 +00:00
|
|
|
class StreamTester {
|
|
|
|
public:
|
2020-11-02 15:58:39 +00:00
|
|
|
explicit StreamTester(v8::Isolate* isolate)
|
2018-10-12 15:16:19 +00:00
|
|
|
: zone_(&allocator_, "StreamTester"),
|
2020-11-02 15:58:39 +00:00
|
|
|
internal_scope_(reinterpret_cast<i::Isolate*>(isolate)) {
|
|
|
|
Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
|
|
|
2018-05-24 21:22:27 +00:00
|
|
|
stream_ = i_isolate->wasm_engine()->StartStreamingCompilation(
|
2019-11-26 16:25:14 +00:00
|
|
|
i_isolate, WasmFeatures::All(), v8::Utils::OpenHandle(*context),
|
2019-04-30 14:04:56 +00:00
|
|
|
"WebAssembly.compileStreaming()",
|
2020-11-02 15:58:39 +00:00
|
|
|
std::make_shared<TestResolver>(i_isolate, &state_, &error_message_,
|
2019-03-18 15:34:58 +00:00
|
|
|
&native_module_));
|
2017-09-14 17:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<StreamingDecoder> stream() { return stream_; }
|
|
|
|
|
2018-10-12 15:16:19 +00:00
|
|
|
// Compiled native module, valid after successful compile.
|
|
|
|
std::shared_ptr<NativeModule> native_module() { return native_module_; }
|
|
|
|
|
2017-09-14 17:34:15 +00:00
|
|
|
// Run all compiler tasks, both foreground and background tasks.
|
2017-10-09 13:35:31 +00:00
|
|
|
void RunCompilerTasks() {
|
|
|
|
static_cast<MockPlatform*>(i::V8::GetCurrentPlatform())->ExecuteTasks();
|
|
|
|
}
|
2017-09-14 17:34:15 +00:00
|
|
|
|
2018-05-24 21:22:27 +00:00
|
|
|
bool IsPromiseFulfilled() { return state_ == CompilationState::kFinished; }
|
2017-09-14 17:34:15 +00:00
|
|
|
|
2018-05-24 21:22:27 +00:00
|
|
|
bool IsPromiseRejected() { return state_ == CompilationState::kFailed; }
|
2017-09-14 17:34:15 +00:00
|
|
|
|
2018-05-24 21:22:27 +00:00
|
|
|
bool IsPromisePending() { return state_ == CompilationState::kPending; }
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
void OnBytesReceived(const uint8_t* start, size_t length) {
|
|
|
|
stream_->OnBytesReceived(Vector<const uint8_t>(start, length));
|
|
|
|
}
|
|
|
|
|
2019-07-16 11:39:38 +00:00
|
|
|
void FinishStream() { stream_->Finish(); }
|
2017-09-14 17:34:15 +00:00
|
|
|
|
2018-10-12 15:16:19 +00:00
|
|
|
void SetCompiledModuleBytes(const uint8_t* start, size_t length) {
|
|
|
|
stream_->SetCompiledModuleBytes(Vector<const uint8_t>(start, length));
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:34:15 +00:00
|
|
|
Zone* zone() { return &zone_; }
|
|
|
|
|
2019-03-18 15:34:58 +00:00
|
|
|
const std::string& error_message() const { return error_message_; }
|
|
|
|
|
2017-09-14 17:34:15 +00:00
|
|
|
private:
|
|
|
|
AccountingAllocator allocator_;
|
|
|
|
Zone zone_;
|
2018-10-12 15:16:19 +00:00
|
|
|
i::HandleScope internal_scope_;
|
2018-05-24 21:22:27 +00:00
|
|
|
CompilationState state_ = CompilationState::kPending;
|
2019-03-18 15:34:58 +00:00
|
|
|
std::string error_message_;
|
2018-10-12 15:16:19 +00:00
|
|
|
std::shared_ptr<NativeModule> native_module_;
|
2017-09-14 17:34:15 +00:00
|
|
|
std::shared_ptr<StreamingDecoder> stream_;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2020-11-02 15:58:39 +00:00
|
|
|
#define RUN_STREAM(name) \
|
|
|
|
MockPlatform mock_platform; \
|
|
|
|
CHECK_EQ(V8::GetCurrentPlatform(), &mock_platform); \
|
|
|
|
v8::Isolate::CreateParams create_params; \
|
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); \
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(create_params); \
|
|
|
|
{ \
|
|
|
|
v8::HandleScope handle_scope(isolate); \
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate); \
|
|
|
|
v8::Context::Scope context_scope(context); \
|
|
|
|
RunStream_##name(&mock_platform, isolate); \
|
|
|
|
} \
|
|
|
|
isolate->Dispose();
|
|
|
|
|
2020-05-25 10:02:01 +00:00
|
|
|
#define STREAM_TEST(name) \
|
2020-11-02 15:58:39 +00:00
|
|
|
void RunStream_##name(MockPlatform*, v8::Isolate*); \
|
|
|
|
UNINITIALIZED_TEST(Async##name) { RUN_STREAM(name); } \
|
2020-05-25 10:02:01 +00:00
|
|
|
\
|
2020-11-02 15:58:39 +00:00
|
|
|
UNINITIALIZED_TEST(SingleThreaded##name) { \
|
2020-05-25 10:02:01 +00:00
|
|
|
i::FlagScope<bool> single_threaded_scope(&i::FLAG_single_threaded, true); \
|
2020-11-02 15:58:39 +00:00
|
|
|
RUN_STREAM(name); \
|
2020-05-25 10:02:01 +00:00
|
|
|
} \
|
2020-11-02 15:58:39 +00:00
|
|
|
void RunStream_##name(MockPlatform* platform, v8::Isolate* isolate)
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
// Create a valid module with 3 functions.
|
|
|
|
ZoneBuffer GetValidModuleBytes(Zone* zone) {
|
|
|
|
ZoneBuffer buffer(zone);
|
|
|
|
TestSignatures sigs;
|
|
|
|
WasmModuleBuilder builder(zone);
|
|
|
|
{
|
|
|
|
WasmFunctionBuilder* f = builder.AddFunction(sigs.i_iii());
|
2019-10-08 12:38:48 +00:00
|
|
|
uint8_t code[] = {kExprLocalGet, 0, kExprEnd};
|
2017-09-14 17:34:15 +00:00
|
|
|
f->EmitCode(code, arraysize(code));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
WasmFunctionBuilder* f = builder.AddFunction(sigs.i_iii());
|
2019-10-08 12:38:48 +00:00
|
|
|
uint8_t code[] = {kExprLocalGet, 1, kExprEnd};
|
2017-09-14 17:34:15 +00:00
|
|
|
f->EmitCode(code, arraysize(code));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
WasmFunctionBuilder* f = builder.AddFunction(sigs.i_iii());
|
2019-10-08 12:38:48 +00:00
|
|
|
uint8_t code[] = {kExprLocalGet, 2, kExprEnd};
|
2017-09-14 17:34:15 +00:00
|
|
|
f->EmitCode(code, arraysize(code));
|
|
|
|
}
|
2019-07-08 09:16:39 +00:00
|
|
|
builder.WriteTo(&buffer);
|
2017-09-14 17:34:15 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-10-12 15:16:19 +00:00
|
|
|
// Create the same valid module as above and serialize it to test streaming
|
|
|
|
// with compiled module caching.
|
2020-11-02 15:58:39 +00:00
|
|
|
ZoneBuffer GetValidCompiledModuleBytes(v8::Isolate* isolate, Zone* zone,
|
|
|
|
ZoneBuffer wire_bytes) {
|
2018-10-12 15:16:19 +00:00
|
|
|
// Use a tester to compile to a NativeModule.
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2018-10-12 15:16:19 +00:00
|
|
|
tester.OnBytesReceived(wire_bytes.begin(), wire_bytes.size());
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
// Serialize the NativeModule.
|
|
|
|
std::shared_ptr<NativeModule> native_module = tester.native_module();
|
|
|
|
CHECK(native_module);
|
2020-08-11 11:19:17 +00:00
|
|
|
native_module->compilation_state()->WaitForTopTierFinished();
|
2018-12-07 14:13:55 +00:00
|
|
|
i::wasm::WasmSerializer serializer(native_module.get());
|
2018-10-12 15:16:19 +00:00
|
|
|
size_t size = serializer.GetSerializedNativeModuleSize();
|
|
|
|
std::vector<byte> buffer(size);
|
2020-08-11 11:19:17 +00:00
|
|
|
CHECK(serializer.SerializeNativeModule(VectorOf(buffer)));
|
2018-10-12 15:16:19 +00:00
|
|
|
ZoneBuffer result(zone, size);
|
|
|
|
result.write(buffer.data(), size);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:34:15 +00:00
|
|
|
// Test that all bytes arrive before doing any compilation. FinishStream is
|
|
|
|
// called immediately.
|
|
|
|
STREAM_TEST(TestAllBytesArriveImmediatelyStreamFinishesFirst) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
ZoneBuffer buffer = GetValidModuleBytes(tester.zone());
|
|
|
|
|
|
|
|
tester.OnBytesReceived(buffer.begin(), buffer.end() - buffer.begin());
|
|
|
|
tester.FinishStream();
|
|
|
|
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that all bytes arrive before doing any compilation. FinishStream is
|
|
|
|
// called after the compilation is done.
|
|
|
|
STREAM_TEST(TestAllBytesArriveAOTCompilerFinishesFirst) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
ZoneBuffer buffer = GetValidModuleBytes(tester.zone());
|
|
|
|
|
|
|
|
tester.OnBytesReceived(buffer.begin(), buffer.end() - buffer.begin());
|
|
|
|
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetFunctionOffset(i::Isolate* isolate, const uint8_t* buffer,
|
|
|
|
size_t size, size_t index) {
|
2020-08-13 15:04:08 +00:00
|
|
|
ModuleResult result = DecodeWasmModule(
|
|
|
|
WasmFeatures::All(), buffer, buffer + size, false,
|
|
|
|
ModuleOrigin::kWasmOrigin, isolate->counters(),
|
|
|
|
isolate->metrics_recorder(), v8::metrics::Recorder::ContextId::Empty(),
|
|
|
|
DecodingMethod::kSyncStream, isolate->wasm_engine()->allocator());
|
2017-09-14 17:34:15 +00:00
|
|
|
CHECK(result.ok());
|
2019-08-08 15:59:35 +00:00
|
|
|
const WasmFunction* func = &result.value()->functions[index];
|
2017-09-14 17:34:15 +00:00
|
|
|
return func->code.offset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that some functions come in the beginning, some come after some
|
|
|
|
// functions already got compiled.
|
|
|
|
STREAM_TEST(TestCutAfterOneFunctionStreamFinishesFirst) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
ZoneBuffer buffer = GetValidModuleBytes(tester.zone());
|
|
|
|
|
2020-11-02 15:58:39 +00:00
|
|
|
Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
|
|
|
|
size_t offset =
|
|
|
|
GetFunctionOffset(i_isolate, buffer.begin(), buffer.size(), 1);
|
2017-09-14 17:34:15 +00:00
|
|
|
tester.OnBytesReceived(buffer.begin(), offset);
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK(tester.IsPromisePending());
|
|
|
|
tester.OnBytesReceived(buffer.begin() + offset, buffer.size() - offset);
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that some functions come in the beginning, some come after some
|
|
|
|
// functions already got compiled. Call FinishStream after the compilation is
|
|
|
|
// done.
|
|
|
|
STREAM_TEST(TestCutAfterOneFunctionCompilerFinishesFirst) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
ZoneBuffer buffer = GetValidModuleBytes(tester.zone());
|
|
|
|
|
2020-11-02 15:58:39 +00:00
|
|
|
Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
|
|
|
|
size_t offset =
|
|
|
|
GetFunctionOffset(i_isolate, buffer.begin(), buffer.size(), 1);
|
2017-09-14 17:34:15 +00:00
|
|
|
tester.OnBytesReceived(buffer.begin(), offset);
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK(tester.IsPromisePending());
|
|
|
|
tester.OnBytesReceived(buffer.begin() + offset, buffer.size() - offset);
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a module with an invalid global section.
|
|
|
|
ZoneBuffer GetModuleWithInvalidSection(Zone* zone) {
|
|
|
|
ZoneBuffer buffer(zone);
|
|
|
|
TestSignatures sigs;
|
|
|
|
WasmModuleBuilder builder(zone);
|
|
|
|
// Add an invalid global to the module. The decoder will fail there.
|
2020-07-09 13:21:45 +00:00
|
|
|
builder.AddGlobal(kWasmStmt, true, WasmInitExpr::GlobalGet(12));
|
2017-09-14 17:34:15 +00:00
|
|
|
{
|
|
|
|
WasmFunctionBuilder* f = builder.AddFunction(sigs.i_iii());
|
2019-10-08 12:38:48 +00:00
|
|
|
uint8_t code[] = {kExprLocalGet, 0, kExprEnd};
|
2017-09-14 17:34:15 +00:00
|
|
|
f->EmitCode(code, arraysize(code));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
WasmFunctionBuilder* f = builder.AddFunction(sigs.i_iii());
|
2019-10-08 12:38:48 +00:00
|
|
|
uint8_t code[] = {kExprLocalGet, 1, kExprEnd};
|
2017-09-14 17:34:15 +00:00
|
|
|
f->EmitCode(code, arraysize(code));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
WasmFunctionBuilder* f = builder.AddFunction(sigs.i_iii());
|
2019-10-08 12:38:48 +00:00
|
|
|
uint8_t code[] = {kExprLocalGet, 2, kExprEnd};
|
2017-09-14 17:34:15 +00:00
|
|
|
f->EmitCode(code, arraysize(code));
|
|
|
|
}
|
2019-07-08 09:16:39 +00:00
|
|
|
builder.WriteTo(&buffer);
|
2017-09-14 17:34:15 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test an error in a section, found by the ModuleDecoder.
|
|
|
|
STREAM_TEST(TestErrorInSectionStreamFinishesFirst) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
ZoneBuffer buffer = GetModuleWithInvalidSection(tester.zone());
|
|
|
|
|
|
|
|
tester.OnBytesReceived(buffer.begin(), buffer.end() - buffer.begin());
|
|
|
|
tester.FinishStream();
|
|
|
|
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM_TEST(TestErrorInSectionCompilerFinishesFirst) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
ZoneBuffer buffer = GetModuleWithInvalidSection(tester.zone());
|
|
|
|
|
|
|
|
tester.OnBytesReceived(buffer.begin(), buffer.end() - buffer.begin());
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM_TEST(TestErrorInSectionWithCuts) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
ZoneBuffer buffer = GetModuleWithInvalidSection(tester.zone());
|
|
|
|
|
|
|
|
const uint8_t* current = buffer.begin();
|
|
|
|
size_t remaining = buffer.end() - buffer.begin();
|
|
|
|
while (current < buffer.end()) {
|
|
|
|
size_t size = std::min(remaining, size_t{10});
|
|
|
|
tester.OnBytesReceived(current, size);
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
current += 10;
|
|
|
|
remaining -= size;
|
|
|
|
}
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
|
|
|
|
|
|
|
ZoneBuffer GetModuleWithInvalidSectionSize(Zone* zone) {
|
|
|
|
// We get a valid module and overwrite the size of the first section with an
|
|
|
|
// invalid value.
|
|
|
|
ZoneBuffer buffer = GetValidModuleBytes(zone);
|
|
|
|
// 9 == 4 (wasm magic) + 4 (version) + 1 (section code)
|
|
|
|
uint8_t* section_size_address = const_cast<uint8_t*>(buffer.begin()) + 9;
|
2017-12-02 00:30:37 +00:00
|
|
|
// 0x808080800F is an invalid module size in leb encoding.
|
2017-09-14 17:34:15 +00:00
|
|
|
section_size_address[0] = 0x80;
|
|
|
|
section_size_address[1] = 0x80;
|
|
|
|
section_size_address[2] = 0x80;
|
|
|
|
section_size_address[3] = 0x80;
|
2017-12-02 00:30:37 +00:00
|
|
|
section_size_address[4] = 0x0F;
|
2017-09-14 17:34:15 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM_TEST(TestErrorInSectionSizeStreamFinishesFirst) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
ZoneBuffer buffer = GetModuleWithInvalidSectionSize(tester.zone());
|
|
|
|
tester.OnBytesReceived(buffer.begin(), buffer.end() - buffer.begin());
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM_TEST(TestErrorInSectionSizeCompilerFinishesFirst) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
ZoneBuffer buffer = GetModuleWithInvalidSectionSize(tester.zone());
|
|
|
|
tester.OnBytesReceived(buffer.begin(), buffer.end() - buffer.begin());
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM_TEST(TestErrorInSectionSizeWithCuts) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
ZoneBuffer buffer = GetModuleWithInvalidSectionSize(tester.zone());
|
|
|
|
const uint8_t* current = buffer.begin();
|
|
|
|
size_t remaining = buffer.end() - buffer.begin();
|
|
|
|
while (current < buffer.end()) {
|
|
|
|
size_t size = std::min(remaining, size_t{10});
|
|
|
|
tester.OnBytesReceived(current, size);
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
current += 10;
|
|
|
|
remaining -= size;
|
|
|
|
}
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test an error in the code section, found by the ModuleDecoder. The error is a
|
|
|
|
// functions count in the code section which differs from the functions count in
|
|
|
|
// the function section.
|
|
|
|
STREAM_TEST(TestErrorInCodeSectionDetectedByModuleDecoder) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // body size
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(1 + arraysize(code) * 2), // section size
|
|
|
|
U32V_1(2), // !!! invalid function count !!!
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.FinishStream();
|
|
|
|
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test an error in the code section, found by the StreamingDecoder. The error
|
|
|
|
// is an invalid function body size, so that there are not enough bytes in the
|
|
|
|
// code section for the function body.
|
|
|
|
STREAM_TEST(TestErrorInCodeSectionDetectedByStreamingDecoder) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(26), // !!! invalid body size !!!
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(1 + arraysize(code) * 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.FinishStream();
|
|
|
|
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test an error in the code section, found by the Compiler. The error is an
|
|
|
|
// invalid return type.
|
|
|
|
STREAM_TEST(TestErrorInCodeSectionDetectedByCompiler) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // !!! invalid body size !!!
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t invalid_code[] = {
|
|
|
|
U32V_1(4), // !!! invalid body size !!!
|
|
|
|
U32V_1(0), // locals count
|
|
|
|
kExprI64Const, 0, kExprEnd // body
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
2017-09-14 17:34:15 +00:00
|
|
|
U32V_1(1 + arraysize(code) * 2 +
|
|
|
|
arraysize(invalid_code)), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.OnBytesReceived(invalid_code, arraysize(invalid_code));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Abort before any bytes arrive.
|
|
|
|
STREAM_TEST(TestAbortImmediately) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Abort within a section.
|
|
|
|
STREAM_TEST(TestAbortWithinSection1) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
const uint8_t bytes[] = {
|
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1) // type count
|
|
|
|
// Type section is not yet complete.
|
|
|
|
};
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Abort within a section.
|
|
|
|
STREAM_TEST(TestAbortWithinSection2) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
2017-09-14 17:34:15 +00:00
|
|
|
// Function section is not yet complete.
|
|
|
|
};
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Abort just before the code section.
|
|
|
|
STREAM_TEST(TestAbortAfterSection) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Abort after the function count in the code section. The compiler tasks
|
|
|
|
// execute before the abort.
|
|
|
|
STREAM_TEST(TestAbortAfterFunctionsCount1) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(20), // section size
|
|
|
|
U32V_1(3), // functions count
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Abort after the function count in the code section. The compiler tasks
|
|
|
|
// do not execute before the abort.
|
|
|
|
STREAM_TEST(TestAbortAfterFunctionsCount2) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(20), // section size
|
|
|
|
U32V_1(3), // functions count
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Abort after some functions got compiled. The compiler tasks execute
|
|
|
|
// before the abort.
|
|
|
|
STREAM_TEST(TestAbortAfterFunctionGotCompiled1) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // !!! invalid body size !!!
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(20), // section size
|
|
|
|
U32V_1(3), // functions count
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Abort after some functions got compiled. The compiler tasks execute
|
|
|
|
// before the abort.
|
|
|
|
STREAM_TEST(TestAbortAfterFunctionGotCompiled2) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // !!! invalid body size !!!
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(20), // section size
|
|
|
|
U32V_1(3), // functions count
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Abort after all functions got compiled.
|
|
|
|
STREAM_TEST(TestAbortAfterCodeSection1) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // body size
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(1 + arraysize(code) * 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Abort after all functions got compiled.
|
|
|
|
STREAM_TEST(TestAbortAfterCodeSection2) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // body size
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(1 + arraysize(code) * 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM_TEST(TestAbortAfterCompilationError1) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // !!! invalid body size !!!
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t invalid_code[] = {
|
|
|
|
U32V_1(4), // !!! invalid body size !!!
|
|
|
|
U32V_1(0), // locals count
|
|
|
|
kExprI64Const, 0, kExprEnd // body
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
2017-09-14 17:34:15 +00:00
|
|
|
U32V_1(1 + arraysize(code) * 2 +
|
|
|
|
arraysize(invalid_code)), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(invalid_code, arraysize(invalid_code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM_TEST(TestAbortAfterCompilationError2) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-09-14 17:34:15 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // !!! invalid body size !!!
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-09-14 17:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t invalid_code[] = {
|
|
|
|
U32V_1(4), // !!! invalid body size !!!
|
|
|
|
U32V_1(0), // locals count
|
|
|
|
kExprI64Const, 0, kExprEnd // body
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
2017-09-14 17:34:15 +00:00
|
|
|
U32V_1(1 + arraysize(code) * 2 +
|
|
|
|
arraysize(invalid_code)), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(invalid_code, arraysize(invalid_code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.stream()->Abort();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
}
|
|
|
|
|
2017-11-20 13:23:47 +00:00
|
|
|
STREAM_TEST(TestOnlyModuleHeader) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-11-20 13:23:47 +00:00
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM_TEST(TestModuleWithZeroFunctions) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-11-20 13:23:47 +00:00
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1), // section size
|
|
|
|
U32V_1(0), // type count
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1), // section size
|
|
|
|
U32V_1(0), // functions count
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(1), // section size
|
|
|
|
U32V_1(0), // functions count
|
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
|
|
|
|
2017-12-13 15:34:37 +00:00
|
|
|
STREAM_TEST(TestModuleWithMultipleFunctions) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-12-13 15:34:37 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // body size
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-12-13 15:34:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(1 + arraysize(code) * 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
2017-12-13 15:34:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
|
|
|
|
2017-12-14 15:52:45 +00:00
|
|
|
STREAM_TEST(TestModuleWithDataSection) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-12-14 15:52:45 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // body size
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2017-12-14 15:52:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(1 + arraysize(code) * 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
2017-12-14 15:52:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t data_section[] = {
|
|
|
|
kDataSectionCode, // section code
|
|
|
|
U32V_1(1), // section size
|
|
|
|
U32V_1(0), // data segment count
|
|
|
|
};
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.OnBytesReceived(data_section, arraysize(data_section));
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
2017-11-21 10:21:29 +00:00
|
|
|
// Test that all bytes arrive before doing any compilation. FinishStream is
|
|
|
|
// called immediately.
|
|
|
|
STREAM_TEST(TestModuleWithImportedFunction) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2017-11-21 10:21:29 +00:00
|
|
|
ZoneBuffer buffer(tester.zone());
|
|
|
|
TestSignatures sigs;
|
|
|
|
WasmModuleBuilder builder(tester.zone());
|
|
|
|
builder.AddImport(ArrayVector("Test"), sigs.i_iii());
|
|
|
|
{
|
|
|
|
WasmFunctionBuilder* f = builder.AddFunction(sigs.i_iii());
|
2019-10-08 12:38:48 +00:00
|
|
|
uint8_t code[] = {kExprLocalGet, 0, kExprEnd};
|
2017-11-21 10:21:29 +00:00
|
|
|
f->EmitCode(code, arraysize(code));
|
|
|
|
}
|
2019-07-08 09:16:39 +00:00
|
|
|
builder.WriteTo(&buffer);
|
2017-11-21 10:21:29 +00:00
|
|
|
|
|
|
|
tester.OnBytesReceived(buffer.begin(), buffer.end() - buffer.begin());
|
|
|
|
tester.FinishStream();
|
|
|
|
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
2018-01-24 16:08:10 +00:00
|
|
|
|
|
|
|
STREAM_TEST(TestModuleWithErrorAfterDataSection) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2018-01-24 16:08:10 +00:00
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 1), // section size
|
|
|
|
U32V_1(1), // functions count
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(6), // section size
|
|
|
|
U32V_1(1), // functions count
|
|
|
|
U32V_1(4), // body size
|
|
|
|
U32V_1(0), // locals count
|
|
|
|
kExprLocalGet, // some code
|
|
|
|
0, // some code
|
|
|
|
kExprEnd, // some code
|
|
|
|
kDataSectionCode, // section code
|
|
|
|
U32V_1(1), // section size
|
|
|
|
U32V_1(0), // data segment count
|
|
|
|
kUnknownSectionCode, // section code
|
|
|
|
U32V_1(1), // invalid section size
|
2018-01-24 16:08:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
2018-10-12 15:16:19 +00:00
|
|
|
|
|
|
|
// Test that cached bytes work.
|
|
|
|
STREAM_TEST(TestDeserializationBypassesCompilation) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2018-10-12 15:16:19 +00:00
|
|
|
ZoneBuffer wire_bytes = GetValidModuleBytes(tester.zone());
|
|
|
|
ZoneBuffer module_bytes =
|
2020-11-02 15:58:39 +00:00
|
|
|
GetValidCompiledModuleBytes(isolate, tester.zone(), wire_bytes);
|
2018-10-12 15:16:19 +00:00
|
|
|
tester.SetCompiledModuleBytes(module_bytes.begin(), module_bytes.size());
|
|
|
|
tester.OnBytesReceived(wire_bytes.begin(), wire_bytes.size());
|
|
|
|
tester.FinishStream();
|
|
|
|
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that bad cached bytes don't cause compilation of wire bytes to fail.
|
|
|
|
STREAM_TEST(TestDeserializationFails) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2018-10-12 15:16:19 +00:00
|
|
|
ZoneBuffer wire_bytes = GetValidModuleBytes(tester.zone());
|
|
|
|
ZoneBuffer module_bytes =
|
2020-11-02 15:58:39 +00:00
|
|
|
GetValidCompiledModuleBytes(isolate, tester.zone(), wire_bytes);
|
2018-10-12 15:16:19 +00:00
|
|
|
// corrupt header
|
|
|
|
byte first_byte = *module_bytes.begin();
|
|
|
|
module_bytes.patch_u8(0, first_byte + 1);
|
|
|
|
tester.SetCompiledModuleBytes(module_bytes.begin(), module_bytes.size());
|
|
|
|
tester.OnBytesReceived(wire_bytes.begin(), wire_bytes.size());
|
|
|
|
tester.FinishStream();
|
|
|
|
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:28:31 +00:00
|
|
|
// Test that a non-empty function section with a missing code section fails.
|
|
|
|
STREAM_TEST(TestFunctionSectionWithoutCodeSection) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2018-11-29 16:28:31 +00:00
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
2018-11-29 16:28:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.FinishStream();
|
|
|
|
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
}
|
|
|
|
|
2019-01-18 09:46:49 +00:00
|
|
|
STREAM_TEST(TestSetModuleCompiledCallback) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2019-01-18 09:46:49 +00:00
|
|
|
bool callback_called = false;
|
|
|
|
tester.stream()->SetModuleCompiledCallback(
|
|
|
|
[&callback_called](const std::shared_ptr<NativeModule> module) {
|
|
|
|
callback_called = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(4), // body size
|
|
|
|
U32V_1(0), // locals count
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
2019-01-18 09:46:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(1 + arraysize(code) * 3), // section size
|
|
|
|
U32V_1(3), // functions count
|
2019-01-18 09:46:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
CHECK(callback_called);
|
|
|
|
}
|
|
|
|
|
2019-03-18 15:34:58 +00:00
|
|
|
// Test that a compile error contains the name of the function, even if the name
|
|
|
|
// section is not present at the time the error is detected.
|
|
|
|
STREAM_TEST(TestCompileErrorFunctionName) {
|
|
|
|
const uint8_t bytes_module_with_code[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(2), // section size
|
|
|
|
U32V_1(1), // functions count
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(4), // section size
|
|
|
|
U32V_1(1), // functions count
|
|
|
|
2, // body size
|
|
|
|
0, // local definitions count
|
|
|
|
kExprNop, // body
|
2019-03-18 15:34:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes_names[] = {
|
|
|
|
kUnknownSectionCode, // section code
|
|
|
|
U32V_1(11), // section size
|
|
|
|
4, // section name length
|
|
|
|
'n', // section name
|
|
|
|
'a', // section name
|
|
|
|
'm', // section name
|
|
|
|
'e', // section name
|
|
|
|
NameSectionKindCode::kFunction, // name section kind
|
|
|
|
4, // name section kind length
|
|
|
|
1, // num function names
|
|
|
|
0, // function index
|
|
|
|
1, // function name length
|
|
|
|
'f', // function name
|
|
|
|
};
|
|
|
|
|
|
|
|
for (bool late_names : {false, true}) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2019-03-18 15:34:58 +00:00
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes_module_with_code,
|
|
|
|
arraysize(bytes_module_with_code));
|
|
|
|
if (late_names) tester.RunCompilerTasks();
|
|
|
|
tester.OnBytesReceived(bytes_names, arraysize(bytes_names));
|
|
|
|
tester.FinishStream();
|
|
|
|
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
CHECK(tester.IsPromiseRejected());
|
|
|
|
CHECK_EQ(
|
2019-04-30 14:04:56 +00:00
|
|
|
"CompileError: WebAssembly.compileStreaming(): Compiling function "
|
2020-05-16 12:47:46 +00:00
|
|
|
"#0:\"f\" failed: function body must end with \"end\" opcode @+26",
|
2019-03-18 15:34:58 +00:00
|
|
|
tester.error_message());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 11:53:02 +00:00
|
|
|
STREAM_TEST(TestSetModuleCodeSection) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
2020-03-17 11:53:02 +00:00
|
|
|
|
|
|
|
uint8_t code[] = {
|
|
|
|
U32V_1(1), // functions count
|
|
|
|
U32V_1(4), // body size
|
|
|
|
U32V_1(0), // locals count
|
|
|
|
kExprLocalGet, 0, kExprEnd // body
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t bytes[] = {
|
2020-09-29 07:46:36 +00:00
|
|
|
WASM_MODULE_HEADER, // module header
|
|
|
|
kTypeSectionCode, // section code
|
|
|
|
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
|
|
|
|
U32V_1(1), // type count
|
|
|
|
SIG_ENTRY_x_x(kI32Code, kI32Code), // signature entry
|
|
|
|
kFunctionSectionCode, // section code
|
|
|
|
U32V_1(1 + 1), // section size
|
|
|
|
U32V_1(1), // functions count
|
|
|
|
0, // signature index
|
|
|
|
kCodeSectionCode, // section code
|
|
|
|
U32V_1(arraysize(code)), // section size
|
2020-03-17 11:53:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tester.OnBytesReceived(bytes, arraysize(bytes));
|
|
|
|
tester.OnBytesReceived(code, arraysize(code));
|
|
|
|
tester.FinishStream();
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
CHECK_EQ(tester.native_module()->module()->code.offset(), arraysize(bytes));
|
|
|
|
CHECK_EQ(tester.native_module()->module()->code.length(), arraysize(code));
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
}
|
|
|
|
|
2020-06-05 17:54:58 +00:00
|
|
|
// Test that profiler does not crash when module is only partly compiled.
|
|
|
|
STREAM_TEST(TestProfilingMidStreaming) {
|
2020-11-02 15:58:39 +00:00
|
|
|
StreamTester tester(isolate);
|
|
|
|
Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
|
2020-06-05 17:54:58 +00:00
|
|
|
Zone* zone = tester.zone();
|
|
|
|
|
|
|
|
// Build module with one exported (named) function.
|
|
|
|
ZoneBuffer buffer(zone);
|
|
|
|
{
|
|
|
|
TestSignatures sigs;
|
|
|
|
WasmModuleBuilder builder(zone);
|
|
|
|
WasmFunctionBuilder* f = builder.AddFunction(sigs.v_v());
|
|
|
|
uint8_t code[] = {kExprEnd};
|
|
|
|
f->EmitCode(code, arraysize(code));
|
|
|
|
builder.AddExport(VectorOf("foo", 3), f);
|
|
|
|
builder.WriteTo(&buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start profiler to force code logging.
|
|
|
|
v8::CpuProfiler* cpu_profiler = v8::CpuProfiler::New(isolate);
|
|
|
|
v8::CpuProfilingOptions profile_options;
|
|
|
|
cpu_profiler->StartProfiling(v8::String::Empty(isolate), profile_options);
|
|
|
|
|
|
|
|
// Send incomplete wire bytes and start compilation.
|
|
|
|
tester.OnBytesReceived(buffer.begin(), buffer.end() - buffer.begin());
|
|
|
|
tester.RunCompilerTasks();
|
|
|
|
|
|
|
|
// Trigger code logging explicitly like the profiler would do.
|
|
|
|
CHECK(WasmCode::ShouldBeLogged(i_isolate));
|
|
|
|
i_isolate->wasm_engine()->LogOutstandingCodesForIsolate(i_isolate);
|
|
|
|
CHECK(tester.IsPromisePending());
|
|
|
|
|
|
|
|
// Finalize stream, stop profiler and clean up.
|
|
|
|
tester.FinishStream();
|
|
|
|
CHECK(tester.IsPromiseFulfilled());
|
|
|
|
v8::CpuProfile* profile =
|
|
|
|
cpu_profiler->StopProfiling(v8::String::Empty(isolate));
|
|
|
|
profile->Delete();
|
|
|
|
cpu_profiler->Dispose();
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:34:15 +00:00
|
|
|
#undef STREAM_TEST
|
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|