#31195

Native backends: hot code reloading by modifying running binary

Openmetif12opened 4d ago

Situation

v -live hot code reloading currently works only on the C backend. It compiles @[live] functions into a shared library, loads it with dlopen/GetProcAddress, and calls through impl_live_* function pointers (vlib/v/gen/c/live.v, vlib/v/live/executable/reloader.c.v).

The native (arm64) backend has no live-reload support:

  • vlib/v/gen/arm64 has no live codegen, no impl_live_* indirection, and no reload runtime.
  • The linker already imports the primitives needed for in-place patching (_mprotect, _sys_icache_invalidate, _dlopen, _dlsym), but they are unused for hot reload.
  • The roadmap item remains unchecked: "Hot code reloading by modifying running binary (using the native backends)".

Goal

Implement hot code reloading for native backends by modifying the running binary in place, rather than relying on a shared library.

Proposed plan

  1. Reproduce the current gap:
    • v -compile-backend arm64 self
    • v -b arm64 -live run examples/hot_reload/message.v
  2. Add native live-function indirection analogous to gen/c/live.v:
    • detect @[live] functions;
    • emit impl_live_* function pointers and wrappers;
    • add a mutex and reload entry point.
  3. Implement a native reload runtime:
    • allocate executable pages;
    • generate replacement function bodies;
    • overwrite the original function entry;
    • mprotect + sys_icache_invalidate.
  4. Add file-watching/recompilation loop equivalent to vlib/v/live/executable/reloader.c.v.
  5. Add tests:
    • smoke test with examples/hot_reload/message.v under -b arm64;
    • native runtime test for patching.

Constraints

  • Native backend is macOS ARM64 only.
  • Requires v -compile-backend arm64 self.
  • This is a large, backend-specific change; should land incrementally behind an experimental flag.

Activity

0

No activity yet.