assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

How does AssemblyScript GC work with imported memory

Open radkomih opened this issue 1 year ago • 2 comments

Hi there, just recently I've started working on a Wasm module that is expected to run inside host env, which provides/imports the memory + allocation functions ext malloc/free. My knowledge of Assemblyscript/Wasm is a bit vague, but as far as I know, AssemblyScript implements runtime with GC, also the toolchain supports the --importMemory flag. I am trying to figure out the exact mechanics of how the memory is provided by the host and shared with the Wasm.

  • The host and the Wasm module are both supposed to allocate stuff on the shared memory's heap, but isn't this problematic, since the Wasm's GC doesn't know for allocations done by the host (the host uses ext malloc/free)? Is the memory somehow segmented into separate regions, one region reserved for the Wasm's GC and another for the host?
  • Or maybe, different option will be to implement the GC to use these ext malloc/free?
 ____________________________________________________________________________________
| HOST                                                                                                            
|                                                     ext_malloc/ext_free
|      ____________________________________________________|________________________
|     | WASM                                               | (imported)             |
|     |                                                    |                        |
|     |             ______________________________________\|/_______________________|
|     | (imported) |          |                  |                                  |
|  MEMORY  ----->  | Data     |         <- Stack | Heap ->                          |
|     |            |__________|__________________|__________________________________|
|     |            0     __data_end         __heap_base         /|\      max memory |
|     |                                                          |                  |
|     |                                                          GC                 |
|     |_____________________________________________________________________________| 
| 
|____________________________________________________________________________________

radkomih avatar Aug 05 '22 11:08 radkomih

In AS malloc/free doesn't controlled by host. All memory jobs happening in WebAssembly linear memory. Wasm only can request more memory pages (mamory.grow) and check current number of pages (memory.size). Based on this we have own memory allocator TLSF and GC stuff implemented over it. You can still do some memory alloc / free manually but it will be danger and lead to memory corruption if you do this between memory base and max memory.

--importMemory is just optional flag which allow you setup WebAssembly.Memory on host side. For example if you need preinit linear memory with some ызусшашс value or setup init and max pages dynamically before instantiation.

MaxGraey avatar Aug 05 '22 12:08 MaxGraey

Regarding the manual memory management, are you referring __alloc/__free functions listed in runtime instructions? I assume that the GC does not know about (de)allocations made with __alloc/__free between memory base and max memory and either the GC might override already allocated memory with __alloc or the opposite.

  • Why are these provided if so?
  • Is there a region in memory where it is safe to do manual memory allocations? I am not sure about what memory base means, is it the beginning of the heap or the beginning of the memory as a whole?

@MaxGraey

radkomih avatar Aug 25 '22 08:08 radkomih

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

github-actions[bot] avatar Sep 24 '22 23:09 github-actions[bot]