Commit 33634d76 authored by Thibaud Michaud's avatar Thibaud Michaud Committed by V8 LUCI CQ

[wasm] Add initial stack switching data structures

Add initial stack memory, jump buffer and continuation objects.

R=ahaas@chromium.org
CC=fgm@chromium.org

Bug: v8:12191
Change-Id: I0c6bde4e5f15e9c539e5e8af1a3b84e5cb5bc9a9
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3220342
Commit-Queue: Thibaud Michaud <thibaudm@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77400}
parent c0a8a36c
...@@ -2198,6 +2198,7 @@ filegroup( ...@@ -2198,6 +2198,7 @@ filegroup(
"src/wasm/signature-map.h", "src/wasm/signature-map.h",
"src/wasm/simd-shuffle.cc", "src/wasm/simd-shuffle.cc",
"src/wasm/simd-shuffle.h", "src/wasm/simd-shuffle.h",
"src/wasm/stacks.h",
"src/wasm/streaming-decoder.cc", "src/wasm/streaming-decoder.cc",
"src/wasm/streaming-decoder.h", "src/wasm/streaming-decoder.h",
"src/wasm/struct-types.h", "src/wasm/struct-types.h",
......
...@@ -3442,6 +3442,7 @@ v8_header_set("v8_internal_headers") { ...@@ -3442,6 +3442,7 @@ v8_header_set("v8_internal_headers") {
"src/wasm/object-access.h", "src/wasm/object-access.h",
"src/wasm/signature-map.h", "src/wasm/signature-map.h",
"src/wasm/simd-shuffle.h", "src/wasm/simd-shuffle.h",
"src/wasm/stacks.h",
"src/wasm/streaming-decoder.h", "src/wasm/streaming-decoder.h",
"src/wasm/struct-types.h", "src/wasm/struct-types.h",
"src/wasm/value-type.h", "src/wasm/value-type.h",
......
...@@ -1839,6 +1839,14 @@ void WasmArray::WasmArrayPrint(std::ostream& os) { ...@@ -1839,6 +1839,14 @@ void WasmArray::WasmArrayPrint(std::ostream& os) {
os << "\n"; os << "\n";
} }
void WasmContinuationObject::WasmContinuationObjectPrint(std::ostream& os) {
PrintHeader(os, "WasmContinuationObject");
os << "\n - parent: " << parent();
os << "\n - jmpbuf: " << jmpbuf();
os << "\n - stack: " << stack();
os << "\n";
}
void WasmInstanceObject::WasmInstanceObjectPrint(std::ostream& os) { void WasmInstanceObject::WasmInstanceObjectPrint(std::ostream& os) {
JSObjectPrintHeader(os, *this, "WasmInstanceObject"); JSObjectPrintHeader(os, *this, "WasmInstanceObject");
os << "\n - module_object: " << Brief(module_object()); os << "\n - module_object: " << Brief(module_object());
......
...@@ -151,6 +151,8 @@ namespace internal { ...@@ -151,6 +151,8 @@ namespace internal {
V(_, TEMPLATE_OBJECT_DESCRIPTION_TYPE, TemplateObjectDescription, \ V(_, TEMPLATE_OBJECT_DESCRIPTION_TYPE, TemplateObjectDescription, \
template_object_description) \ template_object_description) \
V(_, TUPLE2_TYPE, Tuple2, tuple2) \ V(_, TUPLE2_TYPE, Tuple2, tuple2) \
IF_WASM(V, _, WASM_CONTINUATION_OBJECT_TYPE, WasmContinuationObject, \
wasm_continuation_object) \
IF_WASM(V, _, WASM_EXCEPTION_TAG_TYPE, WasmExceptionTag, wasm_exception_tag) \ IF_WASM(V, _, WASM_EXCEPTION_TAG_TYPE, WasmExceptionTag, wasm_exception_tag) \
IF_WASM(V, _, WASM_INDIRECT_FUNCTION_TABLE_TYPE, WasmIndirectFunctionTable, \ IF_WASM(V, _, WASM_INDIRECT_FUNCTION_TABLE_TYPE, WasmIndirectFunctionTable, \
wasm_indirect_function_table) wasm_indirect_function_table)
......
// Copyright 2021 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.
#ifndef V8_WASM_STACKS_H_
#define V8_WASM_STACKS_H_
#if !V8_ENABLE_WEBASSEMBLY
#error This header should only be included if WebAssembly is enabled.
#endif // !V8_ENABLE_WEBASSEMBLY
#include "src/base/build_config.h"
#include "src/common/globals.h"
#include "src/execution/isolate.h"
#include "src/utils/allocation.h"
namespace v8 {
namespace internal {
namespace wasm {
struct JumpBuffer {
void* sp;
void* fp;
void* stack_limit;
// TODO(thibaudm/fgm): Add general-purpose registers.
};
constexpr int kJmpBufSpOffset = offsetof(JumpBuffer, sp);
constexpr int kJmpBufFpOffset = offsetof(JumpBuffer, fp);
constexpr int kJmpBufStackLimitOffset = offsetof(JumpBuffer, stack_limit);
class StackMemory {
public:
static StackMemory* New() { return new StackMemory(); }
// Returns a non-owning view of the current stack.
static StackMemory* GetCurrentStackView(Isolate* isolate) {
byte* limit =
*reinterpret_cast<byte**>(isolate->stack_guard()->address_of_jslimit());
return new StackMemory(limit);
}
~StackMemory() {
PageAllocator* allocator = GetPlatformPageAllocator();
if (owned_) allocator->DecommitPages(limit_, size_);
}
void* limit() { return limit_; }
void* base() { return limit_ + size_; }
// Track external memory usage for Managed<StackMemory> objects.
size_t owned_size() { return sizeof(StackMemory) + (owned_ ? size_ : 0); }
private:
// This constructor allocates a new stack segment.
StackMemory() : owned_(true) {
PageAllocator* allocator = GetPlatformPageAllocator();
size_ = allocator->AllocatePageSize();
// TODO(thibaudm): Leave space for runtime functions.
limit_ = static_cast<byte*>(allocator->AllocatePages(
nullptr, size_, size_, PageAllocator::kReadWrite));
}
// Overload to represent a view of the libc stack.
explicit StackMemory(byte* limit) : limit_(limit), size_(0), owned_(false) {}
byte* limit_;
size_t size_;
bool owned_;
};
} // namespace wasm
} // namespace internal
} // namespace v8
#endif // V8_WASM_STACKS_H_
...@@ -50,6 +50,7 @@ TQ_OBJECT_CONSTRUCTORS_IMPL(WasmFunctionData) ...@@ -50,6 +50,7 @@ TQ_OBJECT_CONSTRUCTORS_IMPL(WasmFunctionData)
TQ_OBJECT_CONSTRUCTORS_IMPL(WasmTypeInfo) TQ_OBJECT_CONSTRUCTORS_IMPL(WasmTypeInfo)
TQ_OBJECT_CONSTRUCTORS_IMPL(WasmStruct) TQ_OBJECT_CONSTRUCTORS_IMPL(WasmStruct)
TQ_OBJECT_CONSTRUCTORS_IMPL(WasmArray) TQ_OBJECT_CONSTRUCTORS_IMPL(WasmArray)
TQ_OBJECT_CONSTRUCTORS_IMPL(WasmContinuationObject)
CAST_ACCESSOR(WasmInstanceObject) CAST_ACCESSOR(WasmInstanceObject)
......
...@@ -1832,6 +1832,40 @@ void DecodeI64ExceptionValue(Handle<FixedArray> encoded_values, ...@@ -1832,6 +1832,40 @@ void DecodeI64ExceptionValue(Handle<FixedArray> encoded_values,
*value = (static_cast<uint64_t>(msb) << 32) | static_cast<uint64_t>(lsb); *value = (static_cast<uint64_t>(msb) << 32) | static_cast<uint64_t>(lsb);
} }
// static
Handle<WasmContinuationObject> WasmContinuationObject::New(
Isolate* isolate, std::unique_ptr<wasm::StackMemory> stack,
HeapObject parent) {
Handle<WasmContinuationObject> result = Handle<WasmContinuationObject>::cast(
isolate->factory()->NewStruct(WASM_CONTINUATION_OBJECT_TYPE));
auto jmpbuf = std::make_unique<wasm::JumpBuffer>();
jmpbuf->stack_limit = stack->limit();
jmpbuf->fp = stack->base();
jmpbuf->sp = stack->base();
Handle<Foreign> managed_stack = Managed<wasm::StackMemory>::FromUniquePtr(
isolate, stack->owned_size(), std::move(stack));
Handle<Foreign> managed_jmpbuf = Managed<wasm::JumpBuffer>::FromUniquePtr(
isolate, sizeof(wasm::JumpBuffer), std::move(jmpbuf));
result->set_stack(*managed_stack);
result->set_jmpbuf(*managed_jmpbuf);
result->set_parent(parent);
return result;
}
// static
Handle<WasmContinuationObject> WasmContinuationObject::New(
Isolate* isolate, std::unique_ptr<wasm::StackMemory> stack) {
auto parent = ReadOnlyRoots(isolate).undefined_value();
return New(isolate, std::move(stack), parent);
}
// static
Handle<WasmContinuationObject> WasmContinuationObject::New(
Isolate* isolate, WasmContinuationObject parent) {
auto stack = std::unique_ptr<wasm::StackMemory>(wasm::StackMemory::New());
return New(isolate, std::move(stack), parent);
}
#ifdef DEBUG #ifdef DEBUG
namespace { namespace {
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "src/objects/js-function.h" #include "src/objects/js-function.h"
#include "src/objects/js-objects.h" #include "src/objects/js-objects.h"
#include "src/objects/objects.h" #include "src/objects/objects.h"
#include "src/wasm/stacks.h"
#include "src/wasm/struct-types.h" #include "src/wasm/struct-types.h"
#include "src/wasm/value-type.h" #include "src/wasm/value-type.h"
...@@ -960,6 +961,24 @@ class WasmArray : public TorqueGeneratedWasmArray<WasmArray, WasmObject> { ...@@ -960,6 +961,24 @@ class WasmArray : public TorqueGeneratedWasmArray<WasmArray, WasmObject> {
TQ_OBJECT_CONSTRUCTORS(WasmArray) TQ_OBJECT_CONSTRUCTORS(WasmArray)
}; };
class WasmContinuationObject
: public TorqueGeneratedWasmContinuationObject<WasmContinuationObject,
Struct> {
public:
static Handle<WasmContinuationObject> New(
Isolate* isolate, std::unique_ptr<wasm::StackMemory> stack);
static Handle<WasmContinuationObject> New(Isolate* isolate,
WasmContinuationObject parent);
DECL_PRINTER(WasmContinuationObject)
TQ_OBJECT_CONSTRUCTORS(WasmContinuationObject)
private:
static Handle<WasmContinuationObject> New(
Isolate* isolate, std::unique_ptr<wasm::StackMemory> stack,
HeapObject parent);
};
#undef DECL_OPTIONAL_ACCESSORS #undef DECL_OPTIONAL_ACCESSORS
namespace wasm { namespace wasm {
......
...@@ -63,6 +63,12 @@ extern class WasmIndirectFunctionTable extends Struct { ...@@ -63,6 +63,12 @@ extern class WasmIndirectFunctionTable extends Struct {
refs: FixedArray; refs: FixedArray;
} }
extern class WasmContinuationObject extends Struct {
stack: Foreign;
jmpbuf: Foreign;
parent: WasmContinuationObject|Undefined;
}
extern class WasmExceptionTag extends Struct { extern class WasmExceptionTag extends Struct {
// Note that this index is only useful for debugging purposes and it is not // Note that this index is only useful for debugging purposes and it is not
// unique across modules. The GC however does not allow objects without at // unique across modules. The GC however does not allow objects without at
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment