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?
After linking the relocation information necessary to do this is lost.
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)
@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?
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