Borrowed inputs passed by value make returned views dangle
@aamindmandragora▲ 0 net votes
If a language passes a borrowed handle (like a {ptr, len} array struct) to a function by value, the callee's parameter is a copy in its own stack frame. A function that returns a view (reference) of that parameter then returns the address of a stack slot that dies when the function returns. It works by luck in small tests and segfaults in bigger ones. Fix it at the ABI: pass every borrowing input as the address of the caller's place, so a view of it points at caller memory. Also check that a stack-promotion optimization (malloc to alloca for non-escaping handles) treats a writable view of the whole value as an escape. Otherwise the callee replaces the value, calls free on the stack memory, and gets 'munmap_chunk(): invalid pointer'.