|
63 | 63 | (def ^:private VAL-KIND |
64 | 64 | {:bool 0 :s8 1 :u8 2 :s16 3 :u16 4 :s32 5 :u32 6 :s64 7 :u64 8 |
65 | 65 | :f32 9 :f64 10 :char 11 :string 12 |
66 | | - :flags 20 :tuple 15}) |
| 66 | + :flags 20 :tuple 15 :own 21 :borrow 21}) |
67 | 67 |
|
68 | 68 | (defn- lib-path [] |
69 | 69 | (or (System/getenv "CLJWIT_WASMTIME_LIB") |
|
83 | 83 | (.invokeWithArguments mh ^java.util.List (vec args))) |
84 | 84 |
|
85 | 85 | (defn- ok! |
86 | | - "wasmtime returns a null error pointer on success." |
87 | | - [what v] |
| 86 | + "wasmtime returns a null error pointer on success. On failure the message is |
| 87 | + the only thing that says *why*, and throwing without it turned a two-minute |
| 88 | + debug into an afternoon once already." |
| 89 | + [api what v] |
88 | 90 | (let [^MemorySegment e v] |
89 | 91 | (when-not (.equals MemorySegment/NULL e) |
90 | | - (throw (ex-info (str what " failed") {:cljwit/error :wasmtime}))) |
| 92 | + (let [msg (with-open [a (Arena/ofConfined)] |
| 93 | + (let [nm ^MemorySegment (.allocate a (long 16))] |
| 94 | + (invoke (:err-message api) e nm) |
| 95 | + (let [n (.get nm I64 (long 0)) |
| 96 | + p ^MemorySegment (.get nm ADDR (long 8)) |
| 97 | + b (byte-array n)] |
| 98 | + (MemorySegment/copy (.reinterpret p n) I8 0 b 0 (int n)) |
| 99 | + (String. b "UTF-8"))))] |
| 100 | + (invoke (:err-delete api) e) |
| 101 | + (throw (ex-info (str what ": " msg) {:cljwit/error :wasmtime})))) |
91 | 102 | v)) |
92 | 103 |
|
93 | 104 | (defn- api |
|
134 | 145 | :instantiate (b "wasmtime_component_linker_instantiate" ADDR ADDR ADDR ADDR ADDR) |
135 | 146 | :export-index (b "wasmtime_component_instance_get_export_index" ADDR ADDR ADDR ADDR ADDR I64) |
136 | 147 | :get-func (b "wasmtime_component_instance_get_func" BOOL ADDR ADDR ADDR ADDR) |
137 | | - :func-call (b "wasmtime_component_func_call" ADDR ADDR ADDR ADDR I64 ADDR I64)})) |
| 148 | + :func-call (b "wasmtime_component_func_call" ADDR ADDR ADDR ADDR I64 ADDR I64) |
| 149 | + :res-clone (b "wasmtime_component_resource_any_clone" ADDR ADDR) |
| 150 | + :res-drop (b "wasmtime_component_resource_any_drop" ADDR ADDR ADDR) |
| 151 | + :res-delete (b "wasmtime_component_resource_any_delete" nil ADDR) |
| 152 | + :err-message (b "wasmtime_error_message" nil ADDR ADDR) |
| 153 | + :err-delete (b "wasmtime_error_delete" nil ADDR)})) |
138 | 154 |
|
139 | 155 | (defn- cstr [^Arena arena ^String t] |
140 | 156 | (let [b (.getBytes t "UTF-8") |
|
237 | 253 | (range (invoke (:var-count api) of)))}) |
238 | 254 | k))) |
239 | 255 |
|
| 256 | +(def ^:private RESOURCE 21) ; wasmtime_component_val_t's kind for a handle |
| 257 | + |
| 258 | +(deftype Handle [^MemorySegment ptr ^MemorySegment ctx api registry |
| 259 | + ^AtomicBoolean closed] |
| 260 | + java.lang.AutoCloseable |
| 261 | + (close [this] |
| 262 | + ;; 0016 B: `delete` is required per handle whatever `drop` did, so it goes |
| 263 | + ;; in a finally. Marking closed first would leak the host memory when |
| 264 | + ;; `drop` throws; marking last would re-drop on a retry. |
| 265 | + (when (.compareAndSet closed false true) |
| 266 | + (swap! registry disj this) |
| 267 | + (try |
| 268 | + (ok! api "resource drop" (invoke (:res-drop api) ctx ptr)) |
| 269 | + (finally (invoke (:res-delete api) ptr)))) |
| 270 | + nil) |
| 271 | + Object |
| 272 | + (toString [_] (str "#cljwit/resource[" (if (.get closed) "closed" "open") "]"))) |
| 273 | + |
| 274 | +(defn- transfer! |
| 275 | + "0016 C: lowering an `own` gives the resource to the guest. The host's handle |
| 276 | + is stale immediately — dropping it afterwards is `unknown handle index` — |
| 277 | + so it is deleted, never dropped." |
| 278 | + [^Handle h] |
| 279 | + (when (.compareAndSet ^AtomicBoolean (.-closed h) false true) |
| 280 | + (swap! (.-registry h) disj h) |
| 281 | + (invoke (:res-delete (.-api h)) (.-ptr h)))) |
| 282 | + |
| 283 | +(defn- live-ptr [^Handle h what] |
| 284 | + (when (.get ^AtomicBoolean (.-closed h)) |
| 285 | + (throw (ex-info (str "resource handle is closed (" what ")") |
| 286 | + {:cljwit/error :closed}))) |
| 287 | + (.-ptr h)) |
| 288 | + |
240 | 289 | ;; --- marshalling ------------------------------------------------------------ |
241 | 290 | ;; Lowering and lifting are *compiled* from the reflected tree once per export, |
242 | 291 | ;; not interpreted per call. An unsupported type fails at instantiation naming |
|
294 | 343 |
|
295 | 344 | (defn- lower-fn [tree] |
296 | 345 | (if (keyword? tree) |
297 | | - (if (= :string tree) |
298 | | - (fn [^Arena arena ^MemorySegment seg v] |
299 | | - (.set seg I8 (long 0) (byte (VAL-KIND :string))) |
300 | | - (write-name! arena seg UNION v)) |
| 346 | + (case tree |
| 347 | + :string (fn [^Arena arena ^MemorySegment seg v] |
| 348 | + (.set seg I8 (long 0) (byte (VAL-KIND :string))) |
| 349 | + (write-name! arena seg UNION v)) |
| 350 | + (:own :borrow) |
| 351 | + (let [own? (= :own tree)] |
| 352 | + (fn [_ ^MemorySegment seg v] |
| 353 | + (let [^Handle h v |
| 354 | + ^MemorySegment p (live-ptr h (if own? "transferring" "borrowing"))] |
| 355 | + (.set seg I8 (long 0) (byte RESOURCE)) |
| 356 | + (.set seg ADDR (long UNION) p) |
| 357 | + ;; A borrow leaves the handle live; an own is stale the moment the |
| 358 | + ;; guest takes it (0016 C). |
| 359 | + (when own? (transfer! h))))) |
301 | 360 | (scalar-lower tree)) |
302 | 361 | (case (:kind tree) |
303 | 362 | :enum (fn [^Arena arena ^MemorySegment seg v] |
|
377 | 436 | (.set seg I64 (long (+ UNION VEC-SIZE)) (long (count fs))) |
378 | 437 | (.set seg ADDR (long (+ UNION VEC-DATA)) buf))))))) |
379 | 438 |
|
380 | | -(defn- lift-fn [tree] |
| 439 | +(defn- lift-fn [tree rt] |
381 | 440 | (if (keyword? tree) |
382 | | - (if (= :string tree) |
383 | | - (fn [^MemorySegment seg] (read-str seg UNION)) |
| 441 | + (case tree |
| 442 | + :string (fn [^MemorySegment seg] (read-str seg UNION)) |
| 443 | + ;; 0016: clone on lift. With one reused result val, the next call deletes |
| 444 | + ;; the previous handle -- and deletes without dropping, so the resource |
| 445 | + ;; leaks and its destructor never runs. |
| 446 | + (:own :borrow) |
| 447 | + (fn [^MemorySegment seg] |
| 448 | + (let [p ^MemorySegment (.get seg ADDR (long UNION)) |
| 449 | + c ^MemorySegment (invoke (:res-clone (:api rt)) p) |
| 450 | + h (->Handle c (:ctx rt) (:api rt) (:registry rt) (AtomicBoolean. false))] |
| 451 | + (swap! (:registry rt) conj h) |
| 452 | + h)) |
384 | 453 | (scalar-lift tree)) |
385 | 454 | (case (:kind tree) |
386 | 455 | :enum (fn [^MemorySegment seg] (keyword (read-str seg UNION))) |
387 | | - :option (let [inner (lift-fn (:ty tree))] |
| 456 | + :option (let [inner (lift-fn (:ty tree) rt)] |
388 | 457 | (fn [^MemorySegment seg] |
389 | 458 | (let [p ^MemorySegment (.get seg ADDR (long UNION))] |
390 | 459 | (when-not (.equals MemorySegment/NULL p) |
391 | 460 | (inner (.reinterpret p (long VAL))))))) |
392 | | - :result (let [lo (some-> (:ok tree) lift-fn) |
393 | | - le (some-> (:err tree) lift-fn)] |
| 461 | + :result (let [lo (when (:ok tree) (lift-fn (:ok tree) rt)) |
| 462 | + le (when (:err tree) (lift-fn (:err tree) rt))] |
394 | 463 | (fn [^MemorySegment seg] |
395 | 464 | (let [ok? (not= 0 (.get seg I8 (long RES-OK))) |
396 | 465 | f (if ok? lo le) |
397 | 466 | p ^MemorySegment (.get seg ADDR (long RES-VAL))] |
398 | 467 | [(if ok? :ok :err) |
399 | 468 | (when (and f (not (.equals MemorySegment/NULL p))) |
400 | 469 | (f (.reinterpret p (long VAL))))]))) |
401 | | - :variant (let [lifts (into {} (map (fn [[n t]] [n (some-> t lift-fn)])) |
| 470 | + :variant (let [lifts (into {} (map (fn [[n t]] [n (when t (lift-fn t rt))])) |
402 | 471 | (:cases tree))] |
403 | 472 | (fn [^MemorySegment seg] |
404 | 473 | (let [nm (read-str seg VAR-NAME) |
|
412 | 481 | p ^MemorySegment (.get seg ADDR (long (+ UNION VEC-DATA))) |
413 | 482 | b (.reinterpret p (long (* 16 n)))] |
414 | 483 | (into #{} (map (fn [i] (keyword (read-str b (* 16 i))))) (range n)))) |
415 | | - :tuple (let [els (mapv lift-fn (:types tree))] |
| 484 | + :tuple (let [els (mapv (fn [t] (lift-fn t rt)) (:types tree))] |
416 | 485 | (fn [^MemorySegment seg] |
417 | 486 | (let [p ^MemorySegment (.get seg ADDR (long (+ UNION VEC-DATA))) |
418 | 487 | b (.reinterpret p (long (* VAL (count els))))] |
419 | 488 | (mapv (fn [i] ((nth els i) (.asSlice b (long (* VAL i)) (long VAL)))) |
420 | 489 | (range (count els)))))) |
421 | | - :list (let [el (lift-fn (:element tree))] |
| 490 | + :list (let [el (lift-fn (:element tree) rt)] |
422 | 491 | (fn [^MemorySegment seg] |
423 | 492 | (let [n (.get seg I64 (long (+ UNION VEC-SIZE))) |
424 | 493 | p ^MemorySegment (.get seg ADDR (long (+ UNION VEC-DATA))) |
425 | 494 | b (.reinterpret p (long (* VAL n)))] |
426 | 495 | (mapv (fn [i] (el (.asSlice b (long (* VAL i)) (long VAL)))) |
427 | 496 | (range n))))) |
428 | | - :record (let [fs (mapv (fn [[n t]] [(keyword n) (lift-fn t)]) (:fields tree))] |
| 497 | + :record (let [fs (mapv (fn [[n t]] [(keyword n) (lift-fn t rt)]) (:fields tree))] |
429 | 498 | (fn [^MemorySegment seg] |
430 | 499 | (let [n (.get seg I64 (long (+ UNION VEC-SIZE))) |
431 | 500 | p ^MemorySegment (.get seg ADDR (long (+ UNION VEC-DATA))) |
|
504 | 573 | nil)) |
505 | 574 |
|
506 | 575 | (deftype Instance [^Artifact artifact ^Arena arena exports aliases sigs |
507 | | - ^MemorySegment store ^AtomicBoolean closed ^AtomicBoolean in-call] |
| 576 | + ^MemorySegment store ^AtomicBoolean closed ^AtomicBoolean in-call |
| 577 | + registry] |
508 | 578 | java.lang.AutoCloseable |
509 | 579 | (close [_] |
510 | 580 | ;; 0014 D: close is an entry into the store like any other. Take `in-call` |
|
516 | 586 | (throw (ex-info "cannot close while a call is in flight" |
517 | 587 | {:cljwit/error :concurrent-use}))) |
518 | 588 | (when (.compareAndSet closed false true) |
| 589 | + ;; Before the store goes: a handle that outlives it holds a dangling |
| 590 | + ;; context, which is the one real use-after-free in this row (0016 D). |
| 591 | + (run! (fn [^java.lang.AutoCloseable h] (.close h)) @registry) |
519 | 592 | (invoke (:store-delete (.-api ^Engine (.-engine artifact))) store) |
520 | 593 | (.close arena))) |
521 | 594 | nil) |
|
555 | 628 | buf ^MemorySegment (.allocate arena (long (alength ^bytes bs))) |
556 | 629 | _ (MemorySegment/copy ^bytes bs 0 buf I8 0 (alength ^bytes bs)) |
557 | 630 | out ^MemorySegment (.allocate arena ^java.lang.foreign.MemoryLayout ADDR)] |
558 | | - (ok! "component_new" |
| 631 | + (ok! a "component_new" |
559 | 632 | (invoke (:comp-new a) (.-ptr e) buf (long (alength ^bytes bs)) out)) |
560 | 633 | (->Artifact e arena (.get out ADDR (long 0)) (AtomicBoolean. false)))) |
561 | 634 |
|
|
584 | 657 | and reused; the result is lifted eagerly and nothing backed by it escapes |
585 | 658 | (`0014` E)." |
586 | 659 | [api ^Arena arena ^MemorySegment ctx ^MemorySegment f sig |
587 | | - ^AtomicBoolean closed ^AtomicBoolean in-call nm] |
| 660 | + ^AtomicBoolean closed ^AtomicBoolean in-call nm rt] |
588 | 661 | (let [ptypes (mapv (comp lower-fn second) (:params sig)) |
589 | 662 | n (count ptypes) |
590 | | - rlift (some-> (:result sig) lift-fn) |
| 663 | + rlift (when (:result sig) (lift-fn (:result sig) rt)) |
591 | 664 | args ^MemorySegment (.allocate arena (long (max 1 (* VAL n)))) |
592 | 665 | res ^MemorySegment (.allocate arena (long VAL)) |
593 | 666 | ;; A confined arena per call would be safer for strings, but nothing |
|
607 | 680 | (with-open [scratch (Arena/ofConfined)] |
608 | 681 | (dotimes [i n] |
609 | 682 | ((nth ptypes i) scratch (.asSlice args (long (* VAL i)) (long VAL)) (nth vs i))) |
610 | | - (ok! (str "call " nm) (invoke call! f ctx args (long n) res (long 1))) |
| 683 | + (ok! api (str "call " nm) (invoke call! f ctx args (long n) res (long 1))) |
611 | 684 | ;; No val_delete on `res`. Its header says it "will deallocate the |
612 | 685 | ;; contents of the value but not the value pointer itself", and on a |
613 | 686 | ;; result or variant — the two kinds carrying a payload pointer — the |
|
631 | 704 | ctx (invoke (:store-context api) store) |
632 | 705 | clink (invoke (:linker-new api) (.-ptr e)) |
633 | 706 | inst ^MemorySegment (.allocate arena (long INSTANCE)) |
634 | | - _ (ok! "linker_instantiate" |
| 707 | + _ (ok! api "linker_instantiate" |
635 | 708 | (invoke (:instantiate api) clink ctx (.-ptr art) inst)) |
636 | 709 | ct (invoke (:comp-type api) (.-ptr art)) |
637 | 710 | closed (AtomicBoolean. false) |
638 | 711 | in-call (AtomicBoolean. false) |
| 712 | + ;; 0016 D: one call can yield handles the caller never destructured, so |
| 713 | + ;; the instance keeps the list and closes what is left before deleting |
| 714 | + ;; the store a handle's context points into. |
| 715 | + registry (atom #{}) |
| 716 | + rt {:ctx ctx :api api :registry registry} |
639 | 717 | item-kind (fn [^MemorySegment it] (bit-and (long (.get it I8 (long 0))) 0xFF)) |
640 | 718 | ;; Every export, flattened. A function inside an interface is keyed the |
641 | 719 | ;; way WIT spells it — "pkg:name/iface@ver#func" — and remembers the |
|
704 | 782 | (when-not (invoke (:get-func api) inst ctx eidx f) |
705 | 783 | (throw (ex-info (str "export " key " vanished between type and instance") |
706 | 784 | {:cljwit/error :wasmtime}))) |
707 | | - (export-fn api arena ctx f sig closed in-call key)))]))) |
| 785 | + (export-fn api arena ctx f sig closed in-call key rt)))]))) |
708 | 786 | found)] |
709 | | - (->Instance art arena fns (alias-map fns) sigs store closed in-call))) |
| 787 | + (->Instance art arena fns (alias-map fns) sigs store closed in-call registry))) |
710 | 788 |
|
711 | 789 | (defn exports |
712 | 790 | "Every exported function, by its exact WIT name." |
|
0 commit comments