Inspecting Wasm Memory in ChromeDevTools

29 views
Skip to first unread message

Immanuel Haffner

unread,
Sep 14, 2020, 8:21:28 AM9/14/20
to v8-users
Hi all,

I am trying to debug Wasm code that manipulates its linear memory.  I am running the code in embedded V8 and I am using the V8 inspector API to connect with CDT. Although I can verify on the embedder's side that the linear memory was manipulated, all I see in CDT is zeroes.  The attached image shows what I mean.

Screenshot_20200914_141705.png

The memory of the Wasm instance is "all zeroes". What am I doing wrong? Using the V8 API from the embedder I can verify that the linear memory was written and contains some data I'd expect to see in CDT.

Do you have any ideas what I am doing wrong?

Thanks & regards,
Immanuel

Immanuel Haffner

unread,
Sep 22, 2020, 5:46:47 AM9/22/20
to v8-users
I upgraded V8 to v8.6.405 and Chromium to 85.  Still the problem persists that the Wasm linear memory appears "all zeroes" although the Wasm code reads the correct data.  Could anybody please provide some help?

Clemens Backes

unread,
Sep 22, 2020, 6:30:43 AM9/22/20
to v8-u...@googlegroups.com
Hi Immanuel,

the problem is probably related to your manipulation of the raw memory pointer. For debugging, we are not using this pointer, but the WasmMemoryObject which is attached to the WasmInstanceObject. This one is still pointing to the original memory, not the one you set via your added "SetWasmInstanceRawMemory" function.

The WasmMemoryObject has an attached JSArrayBuffer. Maybe you can set the backing store of that array buffer to point to your external memory?
It should be clear that this is totally unsupported territory, so you will have to try if that hack works.

I am thinking something along the lines of (extending your SetWasmInstanceRawMemory function):
void SetWasmInstanceRawMemory(Local<Object> wasmInstance, uint8_t* mem_start,
                              size_t mem_size) {
  auto instance =
      i::Handle<i::WasmInstanceObject>::cast(Utils::OpenHandle(*wasmInstance));
  auto* i_isolate = reinterpret_cast<i::Isolate*>(wasmInstance->GetIsolate());
  instance->SetRawMemory(mem_start, mem_size);
  std::unique_ptr<i::BackingStore> backing_store =
      i::BackingStore::WrapAllocation(i_isolate, mem_start, mem_size,
                                      i::SharedFlag::kNotShared, false);
  instance->memory_object().array_buffer().Attach(std::move(backing_store));
}

This code comes without any guarantees, it might or might not work, but maybe it helps you understand the problem and develop a working solution from there.

Cheers,
Clemens


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/13c9e3ab-3cfc-46be-b485-69f8292a73c9n%40googlegroups.com.


--

Clemens Backes

Software Engineer

clem...@google.com

Google Germany GmbH

Erika-Mann-Straße 33

80636 München


Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

Registergericht und -nummer: Hamburg, HRB 86891

Sitz der Gesellschaft: Hamburg


Diese E-Mail ist vertraulich. Falls sie diese fälschlicherweise erhalten haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, löschen Sie alle Kopien und Anhänge davon und lassen Sie mich bitte wissen, dass die E-Mail an die falsche Person gesendet wurde.


This e-mail is confidential. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it has gone to the wrong person.

Immanuel Haffner

unread,
Sep 22, 2020, 8:00:14 AM9/22/20
to v8-users
Hi Clemens,

thanks a lot for your help. You are brilliant! Your solution works out of the box ;)

wasm_memory.png

Now I can finally tackle those bugs in the Wasm code.

Kind regards,
Immanuel


Am Dienstag, 22. September 2020 12:30:43 UTC+2 schrieb Clemens Backes:
Hi Immanuel,

the problem is probably related to your manipulation of the raw memory pointer. For debugging, we are not using this pointer, but the WasmMemoryObject which is attached to the WasmInstanceObject. This one is still pointing to the original memory, not the one you set via your added "SetWasmInstanceRawMemory" function.

The WasmMemoryObject has an attached JSArrayBuffer. Maybe you can set the backing store of that array buffer to point to your external memory?
It should be clear that this is totally unsupported territory, so you will have to try if that hack works.

I am thinking something along the lines of (extending your SetWasmInstanceRawMemory function):
void SetWasmInstanceRawMemory(Local<Object> wasmInstance, uint8_t* mem_start,
                              size_t mem_size) {
  auto instance =
      i::Handle<i::WasmInstanceObject>::cast(Utils::OpenHandle(*wasmInstance));
  auto* i_isolate = reinterpret_cast<i::Isolate*>(wasmInstance->GetIsolate());
  instance->SetRawMemory(mem_start, mem_size);
  std::unique_ptr<i::BackingStore> backing_store =
      i::BackingStore::WrapAllocation(i_isolate, mem_start, mem_size,
                                      i::SharedFlag::kNotShared, false);
  instance->memory_object().array_buffer().Attach(std::move(backing_store));
}

This code comes without any guarantees, it might or might not work, but maybe it helps you understand the problem and develop a working solution from there.

Cheers,
Clemens


On Tue, Sep 22, 2020 at 11:46 AM Immanuel Haffner <haffner...@gmail.com> wrote:
I upgraded V8 to v8.6.405 and Chromium to 85.  Still the problem persists that the Wasm linear memory appears "all zeroes" although the Wasm code reads the correct data.  Could anybody please provide some help?

Immanuel Haffner schrieb am Montag, 14. September 2020 um 14:21:28 UTC+2:
Hi all,

I am trying to debug Wasm code that manipulates its linear memory.  I am running the code in embedded V8 and I am using the V8 inspector API to connect with CDT. Although I can verify on the embedder's side that the linear memory was manipulated, all I see in CDT is zeroes.  The attached image shows what I mean.

Screenshot_20200914_141705.png

The memory of the Wasm instance is "all zeroes". What am I doing wrong? Using the V8 API from the embedder I can verify that the linear memory was written and contains some data I'd expect to see in CDT.

Do you have any ideas what I am doing wrong?

Thanks & regards,
Immanuel

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages