# Imports/Exports Evolution Detailed
This document tracks the evolution of imports and exports across different stages of the `wasi_virt_layer` transformation process. It serves as a reference for understanding how symbols are renamed, merged, and redirected.
## Stage 1: Initial WASM (Input)
- **Imports:**
- `wasi_snapshot_preview1` functions (e.g., `fd_write`, `environ_get`)
- Any custom imports defined by the module.
- **Exports:**
- `memory` (usually)
- `_start` (entry point)
- Any custom exports.
## Stage 2: Generator Processing
During this stage, the `wasi_virt_layer-cli` processes the input WASM modules.
### Renaming & Unification
- **UniqueName System:**
- `UniqueName` is used to generate collision-free names for internal functions and exports.
- Prefix: `__wasip1_vfs_` (defined as `UniqueName::PREFIX`)
### Special Functions (`SpecialFuncUniqueName`)
- `__wasip1_vfs_{wasm}_resetter`: Resets memory/state for a specific WASM module.
- `__wasip1_vfs_reset_on_thread`: Global reset trigger.
- `__wasip1_vfs_reset_on_thread_once`: Ensures reset happens only once per thread context.
- `__wasip1_vfs_start_init_old`: Original `_start` function (renamed).
- `__wasip1_vfs_{wasm}__start`: Wrapper around the original start function.
- `__wasip1_vfs_{wasm}___main_void`: Void main function wrapper.
### Memory Management (`MemoryUniqueName`)
- `__wasip1_vfs_{wasm}_memory_copy_from`: Copies data from module memory to VFS memory.
- `__wasip1_vfs_{wasm}_memory_copy_to`: Copies data from VFS memory to module memory.
- `__wasip1_vfs_{wasm}_memory_trap`: Helper for memory access checks (if enabled).
- `__wasip1_vfs_{wasm}_memory_trap_anchor`: Export anchor for trap function.
- `__wasip1_vfs_{wasm}_memory_director`: Manages memory offsets/pointers.
- `__wasip1_vfs_{wasm}_memory_director_anchor`: Export anchor for director function.
- `__wasip1_vfs_{wasm}_memory`: Renamed export of the module's memory.
### Threading (`ThreadsSpawnName`)
- `__wasip1_vfs_{wasm}_{import}`: Anchor for thread spawn imports.
- `__wasip1_vfs_thread-spawn_import_anchor`: Required strict thread import-anchor export generated by `wasi_virt_layer` for `thread-spawn` wiring (legacy `thread-spawn_import_anchor` is rejected).
- `__wasip1_vfs_is_root_spawn`: Checks if the current thread is the root.
- `__wasip1_vfs_wasi_thread_spawn_self`: Spawns a thread for the VFS itself.
- `__wasip1_vfs_self_wasi_thread_start`: Entry point for VFS thread.
- `__wasip1_vfs_self_wasi_thread_start_anchor`: Anchor for VFS thread start.
- `__wasip1_vfs_real_thread_spawn_fn`: The actual underlying thread spawn function.
- `__wasip1_vfs_wasi_thread_start_entry`: Entry point wrapper for threads.
- `__wasip1_vfs_{name}_{wasm}`: Thread spawn wrapper for a specific module.
- `__wasip1_vfs_{wasm}_{name}`: Thread start wrapper for a specific module.
### ABI Connection (`Wasip1ABIName`)
- `__wasip1_vfs_{name}_{import}`: Connects module imports to VFS exports (self-default).
- `__wasip1_vfs_{wasm}_{import}`: Connects module imports to VFS exports (target-temporal).
### Shared Globals (`SharedGlobalFnsName`)
- `__wasip1_vfs_memory_grow_{n}`: Shared memory grow function (locked).
- `__wasip1_vfs_global_alt_set`: Alternative global set.
- `__wasip1_vfs_global_alt_get`: Alternative global get.
- `__wasip1_vfs_global_alt_get_no_wait`: Non-blocking global get.
- `__wasip1_vfs_global_alt_init_once`: One-time global initialization.
- `__wasip1_vfs_global_alt_pos`: Global position tracker.
### Start Section (`StartAlternativeName`)
- `__wasip1_vfs_start_alt_{name}_{wasm}`: Alternative start function (specific module).
- `__wasip1_vfs_start_alt_{name}`: Alternative start function (VFS).
- `__wasip1_vfs_start_alt_after_memory_reset`: Start hook after memory reset.
### Export Stack Isolation (`ExportStackName`)
- `__wasip1_vfs_stack_handoff_vfs`: Shared VFS stack handoff record (global).
- `__wasip1_vfs_stack_handoff_targets`: Shared target stack handoff records array (global).
- `__wasip1_vfs_stack_ensure_vfs`: Ensures a valid VFS stack is installed (export/import).
- `__wasip1_vfs_stack_claim_target`: Claims a standby stack for a specific target index (export/import).
- `__wasip1_vfs_{module}_stack_ensure`: Module-specific ensure wrapper (export).
- `__wasip1_vfs_{module}_stack_release`: Module-specific stack release (export).
- `__wasip1_vfs_{module}_stack_info`: Module-specific stack info query (export).
- `__wasip1_vfs_{module}_stack_force_release`: Unsafe module-specific force release (export).
## Stage 3: Output (Combined WASM)
### Threaded reactor initialization (C-2)
The VFS's official Rust `_initialize: () -> ()` is called directly from the
synthesized core start, after `__flesh_vfs_start` (linker memory initialization)
and before `__init_offset_global`, `__save_target_memory`, target starts, and
debug pre-init. This is the thread-initializer slot from WVL v0.6.1; no manual
TLS/constructor initializer is restored and no reactor body is copied.
`__wasip1_vfs_reactor_init_state` is a linker-allocated AtomicU32 exported as an
immutable i32 address by threaded WVL builds. The CLI uses it to elect the single
caller and publish constructor completion to worker core instances. libc's own
guard traps on repeat calls, so it cannot replace this coordination. A trapping
initializer invalidates the shared runtime; its state is not reset or retried.
Before merging, the VFS-only pre-pass validates both exports and renames them to
the reserved markers `__wvl_reactor_initialize` and `__wvl_reactor_init_state`.
Targets cannot supply these reserved names. This preserves VFS provenance across
merging and optimizer function reordering. The post-combine pass consumes the
state marker and emits the guarded direct call.
The function marker remains through all optimization and componentization stages.
Every native wasm-opt invocation assigns it a collision-free internal function
name and applies `--no-inline` before optimization. This prevents production
`-Oz` from removing the official function boundary. The final core writer verifies
exactly one direct call from start and removes the marker export. The old 0.116
fallback optimizer cannot order this protection correctly; folded reactors require
native wasm-opt or `--dev` instead of silently accepting inlining.
wit-component sees neither `_initialize` nor a WIT-exported initialization shim,
so it emits no reactor start-shim core module. Non-threaded reactors retain
`_initialize`; a VFS without it retains the previous start sequence. Prebuilt
threaded reactors missing the state ABI must be rebuilt with the matching WVL
library.
Core start runs before memory-dependent canonical imports are wired to the new
instance. As with the v0.6.1 initialization sequence, reactor constructors must
not use those imports (including host re-entry) during core instantiation.
- **Exports:**
- `_start`: The main entry point (orchestrates everything).
- `wasi:thread/spawn`: Thread spawn export (if threads enabled).
- `memory`: The unified/exported memory.
- (Optional) `__wasip1_vfs_*` exports if debug mode is enabled.
- **Imports:**
- `wasi_snapshot_preview1` (resolved internally or passed through).
- Host functions (e.g., `wasi:thread/spawn` if not polyfilled).