Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3b316cb
plans: native lease contract (J1), with JDK 28 evidence for arena close
claude Nov 20, 2026
7fa5f9a
plans, R13: do not use post-access timestamps as ordering evidence
claude Nov 20, 2026
220d76d
plans, R13: require the cleanup per close, report overlap as unverifi…
claude Nov 20, 2026
e4e5cd7
R13: release flag instead of the timestamp sign; require a terminal o…
claude Nov 20, 2026
243013e
plans, R13: state the measured conditions exactly
claude Nov 20, 2026
2a8b436
plans: update the last stale close-time figure
claude Nov 20, 2026
f88e4ed
plans, R13: R13 does not establish close behaviour against an in-flig…
claude Nov 20, 2026
f596318
R13: the last F4 comment no longer claims the property it cannot check
claude Nov 20, 2026
eb94766
plans: qualify the close time in 5.3 the same way
claude Nov 20, 2026
a35c9f7
plans: ground release ordering in the JDK close mechanism, not in a test
claude Nov 20, 2026
b6d5a33
plans: align remaining T3 references with its narrowed scope
claude Nov 20, 2026
3be0c24
lease: test the open decisions and record them for re-evaluation
claude Nov 20, 2026
6598f19
plans: scope the release-ordering guarantee to the checked JDK build
claude Nov 20, 2026
11eaa3d
plans, R14: state the close guarantee as no further access, not as un…
claude Nov 20, 2026
346e9d8
plans: limit the bulk-copy timing claim to copies of 64 bytes or more
claude Nov 20, 2026
2956840
R14, plans: close the reproducer's races and narrow what it shows
claude Nov 20, 2026
d891250
R14, plans: rest G4 on the acquire exception; qualify T6 by variant
claude Nov 20, 2026
ab7aa57
plans, R14: state the close wait by its counter; limit T3's poison claim
claude Nov 20, 2026
ae41280
R14: scope the G3 diagnostic to its confirmed rounds
claude Nov 20, 2026
707d6e7
R14: make the G4 progress probe diagnostic only
claude Nov 20, 2026
3bbb99e
plans: align the audit note with the counter wording and T3's limit
claude Nov 20, 2026
20064e1
R14: describe the G4 progress probe as diagnostic in its comment
claude Nov 20, 2026
60b9e77
plans: record the owner's acceptance of the lease contract decisions
claude Nov 20, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .claude/plans/epoch-recheck-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Phase 4 this document. The predecessors are retained, not deleted; where
v3 overrides them it says so. This is the document implementation works
from.

> **Amended 2026-11-20 (owner decision 1).** §3's "no native lifetime lease"
> is superseded for borrowed native-memory views by `native-lease-v1.md`.
> The rest of v3 stands: the liveness probe, the sole-closer contract for
> unleased windows, W1-W6, and every existing falsifier.

The reviewers found one **blocking** defect in v2 (two of them
independently), closed one of v2's open questions outright, and corrected
five factual errors. §8 is the audit ledger.
Expand Down
1,250 changes: 1,250 additions & 0 deletions .claude/plans/native-lease-v1.md

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions valhalla-lab/lease-model/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "lease-model"
version = "0.0.0"
edition = "2021"
publish = false
description = "Executable model of the native lease contract's Choice B variants (not lgj code, no ABI)"

# Stand-alone: not a member of native/'s workspace.
[workspace]

[dependencies]
148 changes: 148 additions & 0 deletions valhalla-lab/lease-model/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//! Executable model of the native lease contract (lance-graph-java
//! .claude/plans/native-lease-v1.md §3, §4.4). NOT lgj code: no ABI symbol, no
//! FFI. It mirrors registry.rs (slots with a generation, entries held by Arc,
//! close = take entry + bump generation) and adds leases three ways:
//!
//! B1 closing state kept in the slot; generation advances at reclaim.
//! B2 close bumps the generation at once and leaves the entry in the slot
//! (the plan's §4.4 B2 as written: "changes only close").
//! B3 close is exactly today's close; each lease holds an Arc clone of the
//! entry, so the memory lives until the last lease is released. No
//! closing state in the slot at all.
//!
//! Reclaim is observed through a drop counter on the payload, never by reading
//! freed memory.
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

pub const OK: i32 = 0;
pub const INVALID_HANDLE: i32 = -2;
pub const WRONG_KIND: i32 = -3;
pub const PARENT_CLOSED: i32 = -7;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Variant { B1, B2, B3 }

pub struct Payload { pub drops: Arc<AtomicUsize> }
impl Drop for Payload { fn drop(&mut self) { self.drops.fetch_add(1, Ordering::SeqCst); } }

pub struct Entry { pub payload: Payload, pub is_lease_of: Option<Arc<Entry>>, pub owner_slot: usize }

struct Slot { gen: u32, entry: Option<Arc<Entry>>, closing: bool, leases: u64 }

pub struct Registry { v: Variant, slots: Mutex<Vec<Slot>>, pub live_leases: AtomicU64 }

fn enc(gen: u32, idx: usize) -> u64 { ((gen as u64) << 32) | idx as u64 }
fn dec(h: u64) -> (u32, usize) { ((h >> 32) as u32, (h & 0xFFFF_FFFF) as usize) }

impl Registry {
pub fn new(v: Variant) -> Self { Registry { v, slots: Mutex::new(Vec::new()), live_leases: AtomicU64::new(0) } }

fn insert(&self, s: &mut Vec<Slot>, e: Entry) -> u64 {
let arc = Arc::new(e);
if let Some((i, sl)) = s.iter_mut().enumerate().find(|(_, x)| x.entry.is_none()) {
sl.entry = Some(arc); sl.closing = false; sl.leases = 0;
return enc(sl.gen, i);
}
s.push(Slot { gen: 1, entry: Some(arc), closing: false, leases: 0 });
enc(1, s.len() - 1)
}

pub fn open(&self, drops: &Arc<AtomicUsize>) -> u64 {
let mut s = self.slots.lock().unwrap();
let idx = s.len();
self.insert(&mut s, Entry { payload: Payload { drops: drops.clone() }, is_lease_of: None, owner_slot: idx })
}

fn bump(sl: &mut Slot) { sl.gen = sl.gen.wrapping_add(1); if sl.gen == 0 { sl.gen = 1; } }

/// What an ordinary operation (describe, bulk op) sees for an owner handle.
pub fn op(&self, h: u64) -> i32 {
let s = self.slots.lock().unwrap();
let (g, i) = dec(h);
let Some(sl) = s.get(i) else { return INVALID_HANDLE };
if sl.gen != g || sl.entry.is_none() { return INVALID_HANDLE; }
if sl.entry.as_ref().unwrap().is_lease_of.is_some() { return WRONG_KIND; }
if self.v == Variant::B1 && sl.closing { return PARENT_CLOSED; }
OK
}

pub fn borrow(&self, h: u64) -> Result<u64, i32> {
let mut s = self.slots.lock().unwrap();
let (g, i) = dec(h);
let owner = {
let Some(sl) = s.get(i) else { return Err(INVALID_HANDLE) };
if sl.gen != g || sl.entry.is_none() { return Err(INVALID_HANDLE); }
let e = sl.entry.clone().unwrap();
if e.is_lease_of.is_some() { return Err(WRONG_KIND); }
if self.v == Variant::B1 && sl.closing { return Err(PARENT_CLOSED); }
e
};
s[i].leases += 1;
let drops = Arc::new(AtomicUsize::new(0));
let lease = Entry { payload: Payload { drops }, is_lease_of: Some(owner), owner_slot: i };
let lh = self.insert(&mut s, lease);
self.live_leases.fetch_add(1, Ordering::SeqCst);
Ok(lh)
}

pub fn release(&self, lh: u64) -> i32 {
let mut s = self.slots.lock().unwrap();
let (g, i) = dec(lh);
let Some(sl) = s.get_mut(i) else { return INVALID_HANDLE };
if sl.gen != g || sl.entry.is_none() { return INVALID_HANDLE; }
if sl.entry.as_ref().unwrap().is_lease_of.is_none() { return WRONG_KIND; }
let lease = sl.entry.take().unwrap();
Self::bump(sl);
let oi = lease.owner_slot;
let mut reclaimed: Option<Arc<Entry>> = None;
match self.v {
Variant::B1 | Variant::B2 => {
let o = &mut s[oi];
o.leases -= 1;
if o.closing && o.leases == 0 {
reclaimed = o.entry.take();
o.closing = false;
if self.v == Variant::B1 { Self::bump(o); }
}
}
Variant::B3 => {}
}
self.live_leases.fetch_sub(1, Ordering::SeqCst);
drop(s);
drop(lease); // B3: may drop the last Arc of the owner -> reclaim here
drop(reclaimed);
OK
}

pub fn close(&self, h: u64) -> i32 {
let mut s = self.slots.lock().unwrap();
let (g, i) = dec(h);
let Some(sl) = s.get_mut(i) else { return INVALID_HANDLE };
if sl.gen != g || sl.entry.is_none() { return INVALID_HANDLE; }
if sl.entry.as_ref().unwrap().is_lease_of.is_some() { return WRONG_KIND; }
let taken;
match self.v {
Variant::B1 => {
if sl.closing { return INVALID_HANDLE; }
if sl.leases > 0 { sl.closing = true; return OK; }
taken = sl.entry.take(); Self::bump(sl);
}
Variant::B2 => {
if sl.leases > 0 { sl.closing = true; Self::bump(sl); return OK; }
taken = sl.entry.take(); Self::bump(sl);
}
Variant::B3 => { taken = sl.entry.take(); Self::bump(sl); }
}
drop(s);
drop(taken);
OK
}

/// Current generation of a slot, so a test can FORGE the next handle the
/// way an attacker or a buggy caller with a stale counter could.
pub fn forge_current(&self, h: u64) -> u64 {
let (_, i) = dec(h);
enc(self.slots.lock().unwrap()[i].gen, i)
}
}
105 changes: 105 additions & 0 deletions valhalla-lab/lease-model/tests/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use lease_model::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

const ALL: [Variant; 3] = [Variant::B1, Variant::B2, Variant::B3];

fn d() -> Arc<AtomicUsize> { Arc::new(AtomicUsize::new(0)) }

#[test]
fn four_orders_reclaim_exactly_once_and_count_returns_to_zero() {
for v in ALL {
// view closes, then owner
let r = Registry::new(v); let x = d(); let o = r.open(&x);
let l = r.borrow(o).unwrap();
assert_eq!(r.release(l), OK); assert_eq!(x.load(Ordering::SeqCst), 0, "{v:?}");
assert_eq!(r.close(o), OK); assert_eq!(x.load(Ordering::SeqCst), 1, "{v:?}");
// owner closes, then view
let r = Registry::new(v); let x = d(); let o = r.open(&x);
let l = r.borrow(o).unwrap();
assert_eq!(r.close(o), OK); assert_eq!(x.load(Ordering::SeqCst), 0, "{v:?} freed early");
assert_eq!(r.release(l), OK); assert_eq!(x.load(Ordering::SeqCst), 1, "{v:?}");
assert_eq!(r.live_leases.load(Ordering::SeqCst), 0);
// view never closes: pinned leak, visible
let r = Registry::new(v); let x = d(); let o = r.open(&x);
let _l = r.borrow(o).unwrap();
assert_eq!(r.close(o), OK);
assert_eq!(x.load(Ordering::SeqCst), 0, "{v:?} a leak must pin, never free");
assert_eq!(r.live_leases.load(Ordering::SeqCst), 1, "{v:?} leak visible in the count");
}
}

#[test]
fn concurrent_close_and_release_reclaim_exactly_once() {
for v in ALL {
for _ in 0..2000 {
let r = Arc::new(Registry::new(v)); let x = d(); let o = r.open(&x);
let l = r.borrow(o).unwrap();
let (r1, r2) = (r.clone(), r.clone());
let a = std::thread::spawn(move || r1.close(o));
let b = std::thread::spawn(move || r2.release(l));
assert_eq!(a.join().unwrap(), OK); assert_eq!(b.join().unwrap(), OK);
assert_eq!(x.load(Ordering::SeqCst), 1, "{v:?}");
assert_eq!(r.live_leases.load(Ordering::SeqCst), 0);
}
}
}

#[test]
fn no_new_lease_and_no_op_on_a_closed_owner() {
for v in ALL {
let r = Registry::new(v); let x = d(); let o = r.open(&x);
let _l = r.borrow(o).unwrap();
r.close(o);
let want = if v == Variant::B1 { PARENT_CLOSED } else { INVALID_HANDLE };
assert_eq!(r.borrow(o).unwrap_err(), want, "{v:?}");
assert_eq!(r.op(o), want, "{v:?}");
}
}

#[test]
fn double_release_is_refused_and_changes_nothing() {
for v in ALL {
let r = Registry::new(v); let x = d(); let o = r.open(&x);
let l1 = r.borrow(o).unwrap(); let _l2 = r.borrow(o).unwrap();
r.close(o);
assert_eq!(r.release(l1), OK);
assert_eq!(r.release(l1), INVALID_HANDLE, "{v:?}");
assert_eq!(x.load(Ordering::SeqCst), 0, "{v:?} second release must not reclaim");
assert_eq!(r.live_leases.load(Ordering::SeqCst), 1);
}
}

/// The finding: a handle forged with the slot's CURRENT generation after a
/// close with live leases. A real handle was never issued with it.
#[test]
fn forged_handle_after_close_with_live_lease() {
let mut accepted = Vec::new();
for v in ALL {
let r = Registry::new(v); let x = d(); let o = r.open(&x);
let _l = r.borrow(o).unwrap();
r.close(o);
let forged = r.forge_current(o);
if forged != o && r.op(forged) == OK { accepted.push(v); }
}
println!("variants that accept a forged handle on a closed, leased owner: {accepted:?}");
assert_eq!(accepted, vec![Variant::B2], "only B2 as written should accept it");
}

/// Choice A. With a two-step borrow (A2: acquire, then describe) there is a
/// reachable state in which a lease is live and the describe is refused; Java
/// must then release on an error path. A1 fills the descriptor inside the
/// borrow's critical section, so that state does not exist.
#[test]
fn a2_gap_lease_live_while_describe_refused() {
for v in ALL {
let r = Registry::new(v); let x = d(); let o = r.open(&x);
let lease = r.borrow(o).unwrap(); // A2 step 1: acquire
r.close(o); // another thread closes the owner
let describe = r.op(o); // A2 step 2: describe
assert_ne!(describe, OK, "{v:?}");
assert_eq!(r.live_leases.load(Ordering::SeqCst), 1, "{v:?} lease live, no descriptor");
assert_eq!(r.release(lease), OK); // the error path Java must not forget
assert_eq!(x.load(Ordering::SeqCst), 1);
}
}
33 changes: 33 additions & 0 deletions valhalla-lab/reproducers/R13-observed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# java --enable-preview --enable-native-access=ALL-UNNAMED -Xmx2g -cp out R13_SharedArenaCloseVersusRead
# 4 vCPU, Intel(R) Xeon(R) Processor @ 2.10GHz
java.version=28-beta vm=28-beta+16-ea
F1/F2 single thread: close, then access; cleanup count
ok read before close returns the value
ok cleanup has not run before close (count=0)
ok cleanup ran exactly once at close (count=1)
ok read after close throws IllegalStateException
ok second close throws IllegalStateException
ok second close does not run the cleanup again (count=1)
ok the pinned memory itself is untouched (a view close frees nothing)
F3 concurrent scalar reads vs close, 200 rounds
reads=7711808 readerSawISE=200/200 closeThrew=0 cleanups=200
ok no read that saw the release flag set before it began returned a value (violations=0)
ok every reader ended by an IllegalStateException
ok each round ran the cleanup once, unless its close threw (cleanups=200, closeThrew=0)
F4 close attempted alongside another thread's one bulk copy of 256 MiB
round 0: copy threw ISE, close returned after 27591 us, close returned after the copy's end stamp, cleanup stamped after the copy's end stamp
round 1: copy threw ISE, close returned after 24962 us, close returned after the copy's end stamp, cleanup stamped after the copy's end stamp
round 2: copy threw ISE, close returned after 24217 us, close returned after the copy's end stamp, cleanup stamped after the copy's end stamp
round 3: copy threw ISE, close returned after 25112 us, close returned after the copy's end stamp, cleanup stamped after the copy's end stamp
round 4: copy threw ISE, close returned after 25592 us, close returned after the copy's end stamp, cleanup stamped after the copy's end stamp
round 5: copy threw ISE, close returned after 25621 us, close returned after the copy's end stamp, cleanup stamped after the copy's end stamp
round 6: copy threw ISE, close returned after 25653 us, close returned after the copy's end stamp, cleanup stamped after the copy's end stamp
round 7: copy threw ISE, close returned after 25081 us, close returned after the copy's end stamp, cleanup stamped after the copy's end stamp
round 8: copy threw ISE, close returned after 35577 us, close returned after the copy's end stamp, cleanup stamped after the copy's end stamp
round 9: copy threw ISE, close returned after 28626 us, close returned after the copy's end stamp, cleanup stamped after the copy's end stamp
summary: copyCompleted=0 copyThrew=10 closeThrew=0 closeReturnedBeforeCopyEnded=0 cleanupStampBeforeCopyEndStamp=0 of 10 (timings: observations only, not ordering evidence)
ok no copy that saw the release flag set before it began completed normally (completedAfterRelease=0)
ok every close that returned ran the cleanup (missing=0)
ok close never threw (closeThrew=0)
observed (overlap unverified): 10 of 10 copies threw IllegalStateException, 0 completed; 10 of 10 readers began before the release
RESULT: all observations as expected
Loading
Loading