[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/1108207
Reviewed-by: Yang Guo <yangguo@chromium.org>
Reviewed-by: Ben Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53929}
This commit is contained in:
Clemens Hammacher 2018-06-21 12:27:53 +02:00 committed by Commit Bot
parent 5fd2af2472
commit e90af2c702
4 changed files with 57 additions and 42 deletions

View File

@ -4308,10 +4308,29 @@ class V8_EXPORT Proxy : public Object {
class V8_EXPORT WasmCompiledModule : public Object {
public:
typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> SerializedModule;
// The COMMA macro allows us to use ',' inside of the V8_DEPRECATE_SOON macro.
#define COMMA ,
V8_DEPRECATE_SOON(
"Use BufferReference.",
typedef std::pair<const uint8_t * COMMA size_t> CallerOwnedBuffer);
#undef COMMA
/**
* A buffer that is owned by the caller.
* A unowned reference to a byte buffer.
*/
typedef std::pair<const uint8_t*, size_t> CallerOwnedBuffer;
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
@ -4328,7 +4347,7 @@ class V8_EXPORT WasmCompiledModule : public Object {
private:
typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> OwnedBuffer;
friend class WasmCompiledModule;
TransferrableModule(OwnedBuffer&& code, OwnedBuffer&& bytes)
TransferrableModule(OwnedBuffer code, OwnedBuffer bytes)
: compiled_code(std::move(code)), wire_bytes(std::move(bytes)) {}
OwnedBuffer compiled_code = {nullptr, 0};
@ -4365,18 +4384,18 @@ class V8_EXPORT WasmCompiledModule : public Object {
* uncompiled bytes.
*/
static MaybeLocal<WasmCompiledModule> DeserializeOrCompile(
Isolate* isolate, const CallerOwnedBuffer& serialized_module,
const CallerOwnedBuffer& wire_bytes);
Isolate* isolate, BufferReference serialized_module,
BufferReference wire_bytes);
V8_INLINE static WasmCompiledModule* Cast(Value* obj);
private:
static MaybeLocal<WasmCompiledModule> Deserialize(
Isolate* isolate, const CallerOwnedBuffer& serialized_module,
const CallerOwnedBuffer& wire_bytes);
Isolate* isolate, BufferReference serialized_module,
BufferReference wire_bytes);
static MaybeLocal<WasmCompiledModule> Compile(Isolate* isolate,
const uint8_t* start,
size_t length);
static CallerOwnedBuffer AsCallerOwned(
static BufferReference AsReference(
const TransferrableModule::OwnedBuffer& buff) {
return {buff.first.get(), buff.second};
}
@ -4385,6 +4404,15 @@ class V8_EXPORT WasmCompiledModule : public Object {
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
// to simply WasmModuleObjectBuilder
class V8_EXPORT WasmModuleObjectBuilderStreaming final {

View File

@ -7481,8 +7481,8 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::FromTransferrableModule(
Isolate* isolate,
const WasmCompiledModule::TransferrableModule& transferrable_module) {
MaybeLocal<WasmCompiledModule> ret =
Deserialize(isolate, AsCallerOwned(transferrable_module.compiled_code),
AsCallerOwned(transferrable_module.wire_bytes));
Deserialize(isolate, AsReference(transferrable_module.compiled_code),
AsReference(transferrable_module.wire_bytes));
return ret;
}
@ -7500,14 +7500,13 @@ WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() {
}
MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
Isolate* isolate,
const WasmCompiledModule::CallerOwnedBuffer& serialized_module,
const WasmCompiledModule::CallerOwnedBuffer& wire_bytes) {
Isolate* isolate, WasmCompiledModule::BufferReference serialized_module,
WasmCompiledModule::BufferReference wire_bytes) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
i::MaybeHandle<i::WasmModuleObject> maybe_module_object =
i::wasm::DeserializeNativeModule(
i_isolate, {serialized_module.first, serialized_module.second},
{wire_bytes.first, wire_bytes.second});
i_isolate, {serialized_module.start, serialized_module.size},
{wire_bytes.start, wire_bytes.size});
i::Handle<i::WasmModuleObject> module_object;
if (!maybe_module_object.ToHandle(&module_object)) {
return MaybeLocal<WasmCompiledModule>();
@ -7517,15 +7516,14 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
}
MaybeLocal<WasmCompiledModule> WasmCompiledModule::DeserializeOrCompile(
Isolate* isolate,
const WasmCompiledModule::CallerOwnedBuffer& serialized_module,
const WasmCompiledModule::CallerOwnedBuffer& wire_bytes) {
Isolate* isolate, WasmCompiledModule::BufferReference serialized_module,
WasmCompiledModule::BufferReference wire_bytes) {
MaybeLocal<WasmCompiledModule> ret =
Deserialize(isolate, serialized_module, wire_bytes);
if (!ret.IsEmpty()) {
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,

View File

@ -58,10 +58,10 @@ void BuildTrivialModule(Zone* zone, ZoneBuffer* buffer) {
}
bool TestModule(Isolate* isolate,
v8::WasmCompiledModule::CallerOwnedBuffer wire_bytes) {
v8::WasmCompiledModule::BufferReference wire_bytes) {
HandleScope scope(isolate);
v8::WasmCompiledModule::CallerOwnedBuffer serialized_module(nullptr, 0);
v8::WasmCompiledModule::BufferReference serialized_module(nullptr, 0);
MaybeLocal<v8::WasmCompiledModule> module =
v8::WasmCompiledModule::DeserializeOrCompile(
reinterpret_cast<v8::Isolate*>(isolate), serialized_module,
@ -76,8 +76,8 @@ TEST(PropertiesOfCodegenCallbacks) {
Zone zone(&allocator, ZONE_NAME);
ZoneBuffer buffer(&zone);
BuildTrivialModule(&zone, &buffer);
v8::WasmCompiledModule::CallerOwnedBuffer wire_bytes = {buffer.begin(),
buffer.size()};
v8::WasmCompiledModule::BufferReference wire_bytes = {buffer.begin(),
buffer.size()};
Isolate* isolate = CcTest::InitIsolateOnce();
HandleScope scope(isolate);
testing::SetupIsolateForWasmModule(isolate);

View File

@ -64,26 +64,22 @@ class WasmSerializationTest {
builder->WriteTo(*buffer);
}
void ClearSerializedData() {
serialized_bytes_.first = nullptr;
serialized_bytes_.second = 0;
}
void ClearSerializedData() { serialized_bytes_ = {nullptr, 0}; }
void InvalidateVersion() {
uint32_t* slot = reinterpret_cast<uint32_t*>(
const_cast<uint8_t*>(serialized_bytes_.first) +
const_cast<uint8_t*>(serialized_bytes_.start) +
SerializedCodeData::kVersionHashOffset);
*slot = Version::Hash() + 1;
}
void InvalidateWireBytes() {
memset(const_cast<uint8_t*>(wire_bytes_.first), '\0',
wire_bytes_.second / 2);
memset(const_cast<uint8_t*>(wire_bytes_.start), 0, wire_bytes_.size / 2);
}
void InvalidateLength() {
uint32_t* slot = reinterpret_cast<uint32_t*>(
const_cast<uint8_t*>(serialized_bytes_.first) +
const_cast<uint8_t*>(serialized_bytes_.start) +
SerializedCodeData::kPayloadLengthOffset);
*slot = 0u;
}
@ -92,7 +88,7 @@ class WasmSerializationTest {
ErrorThrower thrower(current_isolate(), "");
v8::MaybeLocal<v8::WasmCompiledModule> deserialized =
v8::WasmCompiledModule::DeserializeOrCompile(
current_isolate_v8(), serialized_bytes(), wire_bytes());
current_isolate_v8(), serialized_bytes_, wire_bytes_);
return deserialized;
}
@ -106,7 +102,7 @@ class WasmSerializationTest {
DisallowHeapAllocation assume_no_gc;
CHECK_EQ(memcmp(reinterpret_cast<const uint8_t*>(
module_object->module_bytes()->GetCharsAddress()),
wire_bytes().first, wire_bytes().second),
wire_bytes_.start, wire_bytes_.size),
0);
}
Handle<WasmInstanceObject> instance =
@ -138,13 +134,6 @@ class WasmSerializationTest {
static const char* kFunctionName;
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() {
ZoneBuffer buffer(&zone_);
@ -208,8 +197,8 @@ class WasmSerializationTest {
v8::internal::AccountingAllocator allocator_;
Zone zone_;
v8::WasmCompiledModule::SerializedModule data_;
v8::WasmCompiledModule::CallerOwnedBuffer wire_bytes_;
v8::WasmCompiledModule::CallerOwnedBuffer serialized_bytes_;
v8::WasmCompiledModule::BufferReference wire_bytes_ = {nullptr, 0};
v8::WasmCompiledModule::BufferReference serialized_bytes_ = {nullptr, 0};
v8::Isolate* current_isolate_v8_;
};