Commit e90af2c7 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[api] Properly define BufferReference instead of CallerOwnedBuffer

The name {CallerOwnedBuffer} does not make sense in all situations,
especially if such an object is returned instead of being passed as
argument.
I am working on moving the wasm wire bytes off the JS heap, and hence
will return unowned references via the API. To prepare this change, I
deprecate the existing {CallerOwnedBuffer} and introduce a new
{BufferReference} struct with proper field names.

R=titzer@chromium.org, adamk@chromium.org

Bug: v8:7868
Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Change-Id: Ic8953951447038a831b15a336a52a199bfbeafd5
Reviewed-on: https://chromium-review.googlesource.com/1108207Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53929}
parent 5fd2af24
...@@ -4308,10 +4308,29 @@ class V8_EXPORT Proxy : public Object { ...@@ -4308,10 +4308,29 @@ class V8_EXPORT Proxy : public Object {
class V8_EXPORT WasmCompiledModule : public Object { class V8_EXPORT WasmCompiledModule : public Object {
public: public:
typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> SerializedModule; typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> SerializedModule;
/**
* A buffer that is owned by the caller. // The COMMA macro allows us to use ',' inside of the V8_DEPRECATE_SOON macro.
*/ #define COMMA ,
typedef std::pair<const uint8_t*, size_t> CallerOwnedBuffer; V8_DEPRECATE_SOON(
"Use BufferReference.",
typedef std::pair<const uint8_t * COMMA size_t> CallerOwnedBuffer);
#undef COMMA
/**
* A unowned reference to a byte buffer.
*/
struct BufferReference {
const uint8_t* start;
size_t size;
BufferReference(const uint8_t* start, size_t size)
: start(start), size(size) {}
// Temporarily allow conversion to and from CallerOwnedBuffer.
V8_DEPRECATE_SOON(
"Use BufferReference directly.",
inline BufferReference(CallerOwnedBuffer)); // NOLINT(runtime/explicit)
V8_DEPRECATE_SOON("Use BufferReference directly.",
inline operator CallerOwnedBuffer());
};
/** /**
* An opaque, native heap object for transferring wasm modules. It * An opaque, native heap object for transferring wasm modules. It
...@@ -4328,7 +4347,7 @@ class V8_EXPORT WasmCompiledModule : public Object { ...@@ -4328,7 +4347,7 @@ class V8_EXPORT WasmCompiledModule : public Object {
private: private:
typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> OwnedBuffer; typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> OwnedBuffer;
friend class WasmCompiledModule; friend class WasmCompiledModule;
TransferrableModule(OwnedBuffer&& code, OwnedBuffer&& bytes) TransferrableModule(OwnedBuffer code, OwnedBuffer bytes)
: compiled_code(std::move(code)), wire_bytes(std::move(bytes)) {} : compiled_code(std::move(code)), wire_bytes(std::move(bytes)) {}
OwnedBuffer compiled_code = {nullptr, 0}; OwnedBuffer compiled_code = {nullptr, 0};
...@@ -4365,18 +4384,18 @@ class V8_EXPORT WasmCompiledModule : public Object { ...@@ -4365,18 +4384,18 @@ class V8_EXPORT WasmCompiledModule : public Object {
* uncompiled bytes. * uncompiled bytes.
*/ */
static MaybeLocal<WasmCompiledModule> DeserializeOrCompile( static MaybeLocal<WasmCompiledModule> DeserializeOrCompile(
Isolate* isolate, const CallerOwnedBuffer& serialized_module, Isolate* isolate, BufferReference serialized_module,
const CallerOwnedBuffer& wire_bytes); BufferReference wire_bytes);
V8_INLINE static WasmCompiledModule* Cast(Value* obj); V8_INLINE static WasmCompiledModule* Cast(Value* obj);
private: private:
static MaybeLocal<WasmCompiledModule> Deserialize( static MaybeLocal<WasmCompiledModule> Deserialize(
Isolate* isolate, const CallerOwnedBuffer& serialized_module, Isolate* isolate, BufferReference serialized_module,
const CallerOwnedBuffer& wire_bytes); BufferReference wire_bytes);
static MaybeLocal<WasmCompiledModule> Compile(Isolate* isolate, static MaybeLocal<WasmCompiledModule> Compile(Isolate* isolate,
const uint8_t* start, const uint8_t* start,
size_t length); size_t length);
static CallerOwnedBuffer AsCallerOwned( static BufferReference AsReference(
const TransferrableModule::OwnedBuffer& buff) { const TransferrableModule::OwnedBuffer& buff) {
return {buff.first.get(), buff.second}; return {buff.first.get(), buff.second};
} }
...@@ -4385,6 +4404,15 @@ class V8_EXPORT WasmCompiledModule : public Object { ...@@ -4385,6 +4404,15 @@ class V8_EXPORT WasmCompiledModule : public Object {
static void CheckCast(Value* obj); static void CheckCast(Value* obj);
}; };
// TODO(clemensh): Remove after M69 branch.
WasmCompiledModule::BufferReference::BufferReference(
WasmCompiledModule::CallerOwnedBuffer buf)
: BufferReference(buf.first, buf.second) {}
WasmCompiledModule::BufferReference::
operator WasmCompiledModule::CallerOwnedBuffer() {
return {start, size};
}
// TODO(mtrofin): when streaming compilation is done, we can rename this // TODO(mtrofin): when streaming compilation is done, we can rename this
// to simply WasmModuleObjectBuilder // to simply WasmModuleObjectBuilder
class V8_EXPORT WasmModuleObjectBuilderStreaming final { class V8_EXPORT WasmModuleObjectBuilderStreaming final {
......
...@@ -7481,8 +7481,8 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::FromTransferrableModule( ...@@ -7481,8 +7481,8 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::FromTransferrableModule(
Isolate* isolate, Isolate* isolate,
const WasmCompiledModule::TransferrableModule& transferrable_module) { const WasmCompiledModule::TransferrableModule& transferrable_module) {
MaybeLocal<WasmCompiledModule> ret = MaybeLocal<WasmCompiledModule> ret =
Deserialize(isolate, AsCallerOwned(transferrable_module.compiled_code), Deserialize(isolate, AsReference(transferrable_module.compiled_code),
AsCallerOwned(transferrable_module.wire_bytes)); AsReference(transferrable_module.wire_bytes));
return ret; return ret;
} }
...@@ -7500,14 +7500,13 @@ WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() { ...@@ -7500,14 +7500,13 @@ WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() {
} }
MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize( MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
Isolate* isolate, Isolate* isolate, WasmCompiledModule::BufferReference serialized_module,
const WasmCompiledModule::CallerOwnedBuffer& serialized_module, WasmCompiledModule::BufferReference wire_bytes) {
const WasmCompiledModule::CallerOwnedBuffer& wire_bytes) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
i::MaybeHandle<i::WasmModuleObject> maybe_module_object = i::MaybeHandle<i::WasmModuleObject> maybe_module_object =
i::wasm::DeserializeNativeModule( i::wasm::DeserializeNativeModule(
i_isolate, {serialized_module.first, serialized_module.second}, i_isolate, {serialized_module.start, serialized_module.size},
{wire_bytes.first, wire_bytes.second}); {wire_bytes.start, wire_bytes.size});
i::Handle<i::WasmModuleObject> module_object; i::Handle<i::WasmModuleObject> module_object;
if (!maybe_module_object.ToHandle(&module_object)) { if (!maybe_module_object.ToHandle(&module_object)) {
return MaybeLocal<WasmCompiledModule>(); return MaybeLocal<WasmCompiledModule>();
...@@ -7517,15 +7516,14 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize( ...@@ -7517,15 +7516,14 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
} }
MaybeLocal<WasmCompiledModule> WasmCompiledModule::DeserializeOrCompile( MaybeLocal<WasmCompiledModule> WasmCompiledModule::DeserializeOrCompile(
Isolate* isolate, Isolate* isolate, WasmCompiledModule::BufferReference serialized_module,
const WasmCompiledModule::CallerOwnedBuffer& serialized_module, WasmCompiledModule::BufferReference wire_bytes) {
const WasmCompiledModule::CallerOwnedBuffer& wire_bytes) {
MaybeLocal<WasmCompiledModule> ret = MaybeLocal<WasmCompiledModule> ret =
Deserialize(isolate, serialized_module, wire_bytes); Deserialize(isolate, serialized_module, wire_bytes);
if (!ret.IsEmpty()) { if (!ret.IsEmpty()) {
return ret; return ret;
} }
return Compile(isolate, wire_bytes.first, wire_bytes.second); return Compile(isolate, wire_bytes.start, wire_bytes.size);
} }
MaybeLocal<WasmCompiledModule> WasmCompiledModule::Compile(Isolate* isolate, MaybeLocal<WasmCompiledModule> WasmCompiledModule::Compile(Isolate* isolate,
......
...@@ -58,10 +58,10 @@ void BuildTrivialModule(Zone* zone, ZoneBuffer* buffer) { ...@@ -58,10 +58,10 @@ void BuildTrivialModule(Zone* zone, ZoneBuffer* buffer) {
} }
bool TestModule(Isolate* isolate, bool TestModule(Isolate* isolate,
v8::WasmCompiledModule::CallerOwnedBuffer wire_bytes) { v8::WasmCompiledModule::BufferReference wire_bytes) {
HandleScope scope(isolate); HandleScope scope(isolate);
v8::WasmCompiledModule::CallerOwnedBuffer serialized_module(nullptr, 0); v8::WasmCompiledModule::BufferReference serialized_module(nullptr, 0);
MaybeLocal<v8::WasmCompiledModule> module = MaybeLocal<v8::WasmCompiledModule> module =
v8::WasmCompiledModule::DeserializeOrCompile( v8::WasmCompiledModule::DeserializeOrCompile(
reinterpret_cast<v8::Isolate*>(isolate), serialized_module, reinterpret_cast<v8::Isolate*>(isolate), serialized_module,
...@@ -76,8 +76,8 @@ TEST(PropertiesOfCodegenCallbacks) { ...@@ -76,8 +76,8 @@ TEST(PropertiesOfCodegenCallbacks) {
Zone zone(&allocator, ZONE_NAME); Zone zone(&allocator, ZONE_NAME);
ZoneBuffer buffer(&zone); ZoneBuffer buffer(&zone);
BuildTrivialModule(&zone, &buffer); BuildTrivialModule(&zone, &buffer);
v8::WasmCompiledModule::CallerOwnedBuffer wire_bytes = {buffer.begin(), v8::WasmCompiledModule::BufferReference wire_bytes = {buffer.begin(),
buffer.size()}; buffer.size()};
Isolate* isolate = CcTest::InitIsolateOnce(); Isolate* isolate = CcTest::InitIsolateOnce();
HandleScope scope(isolate); HandleScope scope(isolate);
testing::SetupIsolateForWasmModule(isolate); testing::SetupIsolateForWasmModule(isolate);
......
...@@ -64,26 +64,22 @@ class WasmSerializationTest { ...@@ -64,26 +64,22 @@ class WasmSerializationTest {
builder->WriteTo(*buffer); builder->WriteTo(*buffer);
} }
void ClearSerializedData() { void ClearSerializedData() { serialized_bytes_ = {nullptr, 0}; }
serialized_bytes_.first = nullptr;
serialized_bytes_.second = 0;
}
void InvalidateVersion() { void InvalidateVersion() {
uint32_t* slot = reinterpret_cast<uint32_t*>( uint32_t* slot = reinterpret_cast<uint32_t*>(
const_cast<uint8_t*>(serialized_bytes_.first) + const_cast<uint8_t*>(serialized_bytes_.start) +
SerializedCodeData::kVersionHashOffset); SerializedCodeData::kVersionHashOffset);
*slot = Version::Hash() + 1; *slot = Version::Hash() + 1;
} }
void InvalidateWireBytes() { void InvalidateWireBytes() {
memset(const_cast<uint8_t*>(wire_bytes_.first), '\0', memset(const_cast<uint8_t*>(wire_bytes_.start), 0, wire_bytes_.size / 2);
wire_bytes_.second / 2);
} }
void InvalidateLength() { void InvalidateLength() {
uint32_t* slot = reinterpret_cast<uint32_t*>( uint32_t* slot = reinterpret_cast<uint32_t*>(
const_cast<uint8_t*>(serialized_bytes_.first) + const_cast<uint8_t*>(serialized_bytes_.start) +
SerializedCodeData::kPayloadLengthOffset); SerializedCodeData::kPayloadLengthOffset);
*slot = 0u; *slot = 0u;
} }
...@@ -92,7 +88,7 @@ class WasmSerializationTest { ...@@ -92,7 +88,7 @@ class WasmSerializationTest {
ErrorThrower thrower(current_isolate(), ""); ErrorThrower thrower(current_isolate(), "");
v8::MaybeLocal<v8::WasmCompiledModule> deserialized = v8::MaybeLocal<v8::WasmCompiledModule> deserialized =
v8::WasmCompiledModule::DeserializeOrCompile( v8::WasmCompiledModule::DeserializeOrCompile(
current_isolate_v8(), serialized_bytes(), wire_bytes()); current_isolate_v8(), serialized_bytes_, wire_bytes_);
return deserialized; return deserialized;
} }
...@@ -106,7 +102,7 @@ class WasmSerializationTest { ...@@ -106,7 +102,7 @@ class WasmSerializationTest {
DisallowHeapAllocation assume_no_gc; DisallowHeapAllocation assume_no_gc;
CHECK_EQ(memcmp(reinterpret_cast<const uint8_t*>( CHECK_EQ(memcmp(reinterpret_cast<const uint8_t*>(
module_object->module_bytes()->GetCharsAddress()), module_object->module_bytes()->GetCharsAddress()),
wire_bytes().first, wire_bytes().second), wire_bytes_.start, wire_bytes_.size),
0); 0);
} }
Handle<WasmInstanceObject> instance = Handle<WasmInstanceObject> instance =
...@@ -138,13 +134,6 @@ class WasmSerializationTest { ...@@ -138,13 +134,6 @@ class WasmSerializationTest {
static const char* kFunctionName; static const char* kFunctionName;
Zone* zone() { return &zone_; } Zone* zone() { return &zone_; }
const v8::WasmCompiledModule::CallerOwnedBuffer& wire_bytes() const {
return wire_bytes_;
}
const v8::WasmCompiledModule::CallerOwnedBuffer& serialized_bytes() const {
return serialized_bytes_;
}
void SetUp() { void SetUp() {
ZoneBuffer buffer(&zone_); ZoneBuffer buffer(&zone_);
...@@ -208,8 +197,8 @@ class WasmSerializationTest { ...@@ -208,8 +197,8 @@ class WasmSerializationTest {
v8::internal::AccountingAllocator allocator_; v8::internal::AccountingAllocator allocator_;
Zone zone_; Zone zone_;
v8::WasmCompiledModule::SerializedModule data_; v8::WasmCompiledModule::SerializedModule data_;
v8::WasmCompiledModule::CallerOwnedBuffer wire_bytes_; v8::WasmCompiledModule::BufferReference wire_bytes_ = {nullptr, 0};
v8::WasmCompiledModule::CallerOwnedBuffer serialized_bytes_; v8::WasmCompiledModule::BufferReference serialized_bytes_ = {nullptr, 0};
v8::Isolate* current_isolate_v8_; v8::Isolate* current_isolate_v8_;
}; };
......
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