flan/test/programs/bytes-copy.flan
Joseph Ferano 2e203f64b8 bytes copies, bytes-view aliases, and a dev-session segfault parks
The INSERTIONSORT crash, all three rulings (FIX.org 2026-09-20):

- (bytes s) allocates a writable copy through the allocator surface —
  context or (bytes s a), StorageExhausted with retry, a registry note in
  dev builds (flan_bytes_dup, lowered like vec-new). (bytes-view s) is the
  old zero-cost reinterpret, renamed, read-only by convention; every
  in-repo reader swept over to it. (string b) unchanged.
- String constants were already read-only on both backends at -O0; now
  pinned — bytes-copy.flan rows on LLVM/-O0/--x86, and dies_segv rows
  asserting the write-through-view trap on both backends.
- A dev build installs a SIGSEGV/SIGBUS handler by the same dev-only
  constructor slot that arms the registry: one line naming the address and
  the innermost frame, then the trap-hook park — stopped, not dead, the
  daemon serving. No agent: message and re-raise. Release builds untouched.
  Pinned by trap_park over dev-segv.flan.
2026-09-20 23:12:42 +07:00

39 lines
1.5 KiB
Plaintext

;;;; (bytes s) copies, (bytes-view s) aliases — the ruling from the
;;;; INSERTIONSORT dogfooding session. The pin is the exact inverse of the
;;;; old aliasing: a write through the copy leaves the original string
;;;; printing unchanged, where the old (bytes s) either showed the write
;;;; through or trapped, depending on where the string's storage was.
(defvar frame Allocator)
(defn main [] i32
;; 1. The copy is writable and independent. Under the old reinterpret this
;; second line printed ZNSERTIONSORT too (or the whole program died in
;; .rodata) — the original staying itself is the whole of the change.
(let [s "INSERTIONSORT"
b (bytes s)]
(set (at b 0) \Z)
(println (string b)) ; ZNSERTIONSORT
(println s)) ; INSERTIONSORT
;; 2. A literal's copy is writable — the exact form that used to segfault
;; at -O0 and silently do nothing at -O2.
(let [b (bytes "hi")]
(set (at b 0) \H)
(println (string b))) ; Hi
;; 3. The view still costs nothing and reads the string's own storage.
(let [v (bytes-view "abc")]
(println (len v)) ; 3
(println (at v 2))) ; 99
;; 4. (bytes s a) names the allocator, like (vec-new T a) and (clone v a):
;; the copy's block comes from the arena and free-all reclaims it.
(set frame (arena-new 4096))
(let [b (bytes "arena" frame)]
(set (at b 4) \A)
(println (string b))) ; arenA
(free-all frame)
(arena-destroy frame)
0)