Stream: wasmtime

Topic: change the size of a string in data section


view this post on Zulip julia (Oct 18 2022 at 02:51):

I need to replace a string in data section and it might effect the size of the string. I do this change when wasmtime loads a module into memory, i.e. ModuleMemoryImages is created in runtime crate (cow.rs). I know that the wasm data is maintained by an [u8] array and this change would effect all offsets. Is there any way to deal with this problem?

view this post on Zulip bjorn3 (Oct 18 2022 at 04:43):

After linking the relocation information necessary to do this is lost.

view this post on Zulip bjorn3 (Oct 18 2022 at 04:45):

If you have control over the source code of the wasm module you could use an exported mutable global variable that contains a pointer to the string and then when you need to change the string append it to the global variable and then change the pointer. (the wasm level global contains the address of the pointer you need to change)

view this post on Zulip Ian Smith (Nov 09 2022 at 15:23):

@julia If I understand your question properly, I think you can do this with some wat hackery. Convert the the Wasm to wat to have a hope of messing with it. Then you add your new value onto the end of the data and then patch up the references to the original to point to your new entry and perhaps the size of the data section. @bjorn3 did I miss something?

view this post on Zulip bjorn3 (Nov 09 2022 at 15:35):

Converting to wat won't work. The linking section's relocation information refers to specific offsets. Round tripping between wat and wasm will likely break this. Especially because linkable wasm modules use a fixed length for uleb128 fields to make the life of linkers easier, while wasm2wat will simply use the shortest possible length to save space. You have to use a binary parser that preserves the code section as is and allows you to only modify data segments afaik.


Last updated: Nov 22 2024 at 17:03 UTC