Skip to content

Commit 66c2d9d

Browse files
committed
fix(runtime): own partial bitstrings in ETS copies
1 parent db23128 commit 66c2d9d

5 files changed

Lines changed: 229 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
All notable milestones for the nera project are documented in this file.
44

5+
## Stored bitstrings keep their bytes and exact bit length
6+
7+
- ETS and `persistent_term` now own copies of non-byte-aligned bitstrings,
8+
just as they already do for whole-byte binaries. Small inline values and
9+
large reference-counted values remain equal after the storing process exits
10+
or collects garbage, as on BEAM.
11+
- Copying reads preserve the final partial byte's logical bit length and remain
12+
valid after the table is deleted or the persistent term is erased.
13+
- The `ets_stored_bitstring` regression exercises both writer lifetimes and
14+
both storage APIs unchanged natively and on BEAM.
15+
516
## Receives nested in after bodies resume at their own wait
617

718
- A `receive` directly inside another receive's `after` body now waits for

‎crates/nera-runtime/src/ets.rs‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ unsafe fn object_heap_words(term: ErlTerm, pending: &mut WorkStack<ErlTerm>) ->
402402
queue(cursor); // tail
403403
return count;
404404
}
405-
if term.is_binary() {
405+
if term.is_bitstring() {
406406
if let Some((_ptr, len)) = unsafe { crate::ffi::binary_data_pub(term) } {
407407
if len <= REFC_BINARY_THRESHOLD {
408408
return 1 + len.div_ceil(8); // header + data words
@@ -517,7 +517,7 @@ trait CopyTarget {
517517
/// Allocate `n` uninitialized words that stay in place.
518518
fn alloc_words(&mut self, n: usize) -> *mut u64;
519519

520-
/// Copy a boxed term that holds no other terms (a binary, float, bigint
520+
/// Copy a boxed term that holds no other terms (a bitstring, float, bigint
521521
/// or reference); any other term is kept as it is.
522522
/// # Safety
523523
/// As for `deep_copy_into_compact`.
@@ -536,16 +536,17 @@ impl CopyTarget for CompactHeap {
536536
REFC_BINARY_OFFSET_WORD_INDEX, REFERENCE_TOTAL_WORDS,
537537
REFERENCE_ID_WORD_INDEX};
538538

539-
if term.is_binary() {
539+
if term.is_bitstring() {
540540
if let Some((ptr, len)) = unsafe { crate::ffi::binary_data_pub(term) } {
541541
let bytes = unsafe { std::slice::from_raw_parts(ptr, len) };
542+
let bit_len = unsafe { term.binary_bit_size().unwrap() } as u64;
542543
if len <= REFC_BINARY_THRESHOLD {
543544
// Inline heap binary: [header | data words...]
544545
let data_words = len.div_ceil(8);
545546
let total_words = 1 + data_words;
546547
let words = self.alloc_words(total_words);
547548
unsafe {
548-
*words = BoxedHeader::new(crate::term::BoxedSubtype::HeapBinary, (len * 8) as u64).0;
549+
*words = BoxedHeader::new(crate::term::BoxedSubtype::HeapBinary, bit_len).0;
549550
std::ptr::copy_nonoverlapping(
550551
bytes.as_ptr(),
551552
(words.add(1)) as *mut u8,
@@ -559,7 +560,7 @@ impl CopyTarget for CompactHeap {
559560
let raw_arc_ptr = Arc::as_ptr(&data) as u64;
560561
let words = self.alloc_words(REFC_BINARY_TOTAL_WORDS);
561562
unsafe {
562-
*words = BoxedHeader::new(crate::term::BoxedSubtype::RefcBinary, (len * 8) as u64).0;
563+
*words = BoxedHeader::new(crate::term::BoxedSubtype::RefcBinary, bit_len).0;
563564
*words.add(REFC_BINARY_PTR_WORD_INDEX) = raw_arc_ptr;
564565
*words.add(REFC_BINARY_OFFSET_WORD_INDEX) = 0;
565566
}
@@ -613,10 +614,16 @@ impl CopyTarget for ProcessHeap {
613614
}
614615

615616
unsafe fn copy_leaf(&mut self, term: ErlTerm) -> ErlTerm {
616-
if term.is_binary() {
617+
if term.is_bitstring() {
617618
if let Some((ptr, len)) = unsafe { crate::ffi::binary_data_pub(term) } {
618619
let bytes = unsafe { std::slice::from_raw_parts(ptr, len) };
619-
return self.alloc_binary_auto(bytes);
620+
let bit_len = unsafe { term.binary_bit_size().unwrap() } as u64;
621+
let copy = self.alloc_binary_auto(bytes);
622+
// Allocation rounds up to bytes; preserve the logical bit
623+
// length rather than turning the trailing bits into a byte.
624+
let header = copy.as_boxed_ptr().unwrap().cast_mut();
625+
unsafe { *header = BoxedHeader(*header).with_arity(bit_len).0; }
626+
return copy;
620627
}
621628
return term;
622629
}
@@ -690,7 +697,7 @@ impl<'a, T: CopyTarget> EtsCopier<'a, T> {
690697
if term.is_cons() {
691698
return unsafe { self.copy_list(term) };
692699
}
693-
if !unsafe { term.is_binary() } {
700+
if !unsafe { term.is_bitstring() } {
694701
if let Some(ptr) = term.as_boxed_ptr() {
695702
let header = BoxedHeader(unsafe { *ptr });
696703
let (subtype, raw, slots) = match header.subtype() {
@@ -3781,3 +3788,7 @@ mod tests {
37813788
}
37823789
}
37833790
}
3791+
3792+
#[cfg(test)]
3793+
#[path = "ets_bitstring_tests.rs"]
3794+
mod bitstring_tests;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! Ownership checks for the bitstring storage regression. The Erlang fixture
2+
//! owns observable lifetime/equality behavior; this test deterministically
3+
//! rejects descriptor aliasing before either source allocation is released.
4+
5+
use super::*;
6+
use crate::binary::BinarySlice;
7+
use crate::term::BoxedSubtype;
8+
9+
#[test]
10+
fn bitstring_copies_own_descriptors_in_both_directions() {
11+
// The reported values, plus a sub-byte value and the inline/refc boundary.
12+
let mut large = vec![b'x'; 200];
13+
large.push(0xc0); // <<3:2>>
14+
let mut boundary = vec![b'b'; crate::heap::REFC_BINARY_THRESHOLD];
15+
boundary.push(0x80); // <<1:1>>
16+
let cases = [
17+
(b"abcdefgh\x50".to_vec(), 4),
18+
(large, 6),
19+
(vec![0xa0], 5),
20+
(boundary, 7),
21+
];
22+
23+
for (bytes, unused_bits) in cases {
24+
let bits = bytes.len() * 8 - unused_bits;
25+
for refc_window in [false, true] {
26+
let mut source = ProcessHeap::new();
27+
let original = if refc_window {
28+
// A byte-offset window must not accidentally copy its prefix.
29+
let prefix = b"prefix";
30+
let backing = [prefix.as_slice(), &bytes, b"suffix"].concat();
31+
let window =
32+
BinarySlice::new_bits(Arc::new(BinaryData::new(backing)), prefix.len(), bits)
33+
.unwrap();
34+
source.alloc_binary_slice(window)
35+
} else {
36+
let term = source.alloc_binary(&bytes);
37+
// SAFETY: the test exclusively owns the new descriptor and
38+
// shortens its bit length within its initialized byte storage.
39+
unsafe {
40+
*term.as_boxed_ptr().unwrap().cast_mut() =
41+
BoxedHeader::new(BoxedSubtype::HeapBinary, bits as u64).0;
42+
}
43+
term
44+
};
45+
46+
// SAFETY: the source is live, immutable and cannot collect here.
47+
let words = unsafe { count_heap_words(original) };
48+
let mut pool = EtsRegionPool::new();
49+
let mut stored = CompactHeap::from_pool(Some(pool.alloc(words)));
50+
let in_storage = unsafe { deep_copy_into_compact(original, &mut stored) };
51+
assert_ne!(
52+
in_storage.as_boxed_ptr(),
53+
original.as_boxed_ptr(),
54+
"storage must own the bitstring descriptor"
55+
);
56+
drop(source);
57+
58+
let mut reader = ProcessHeap::new();
59+
// SAFETY: stored keeps every source descriptor/backing byte live.
60+
let read = unsafe { deep_copy_into_heap(in_storage, &mut reader) };
61+
assert_ne!(
62+
read.as_boxed_ptr(),
63+
in_storage.as_boxed_ptr(),
64+
"copying reads must own the bitstring descriptor"
65+
);
66+
drop(stored);
67+
drop(pool);
68+
69+
let mut roots = [read];
70+
// SAFETY: the sole root belongs to reader; no raw borrows survive.
71+
unsafe { reader.collect_full(&mut roots) };
72+
// SAFETY: the root was updated by GC and reader owns its bytes.
73+
unsafe {
74+
assert_eq!(roots[0].binary_bit_size(), Some(bits));
75+
assert_eq!(roots[0].binary_bytes_any(), Some(bytes.as_slice()));
76+
}
77+
}
78+
}
79+
}

‎tests/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
462462
| `ets_basic.erl` | ETS new/insert/lookup/delete/tab2list | ~15 |
463463
| `ets_keypos.erl` | ETS keypos and named tables | ~8 |
464464
| `ets_stored_fun.erl` | A fun stored in ETS (at the top of a row, nested in a list, tuple and map, after `update_element/3`) or `persistent_term` by a process that then exits or collects garbage keeps its captured list, binaries, tuple, map, float, bignum and inner fun when looked up and called; a fun read by `tab2list/1` or `persistent_term:get/1` still works after the table or term is deleted; completion marker; also run on BEAM | ~16 |
465+
| `ets_stored_bitstring.erl` | Non-byte-aligned heap/refc bitstrings, sub-byte values and extracted tails stored in ETS or `persistent_term` survive writer exit/GC; `update_element/3` and copying reads remain intact after table deletion or term erasure; completion marker; same fixture on BEAM | 4 lifetime scenarios |
465466
| `ets_named_table_return.erl` | ets:new/2 with `named_table` returns the name (Cowboy's `?MODULE = ets:new(?MODULE, [named_table, ...])`): every table type, apply, variable module and fun calls; a table without `named_table` does not take the name, and creating or deleting one leaves a named table with the same atom reachable; the name is released by ets:delete/1; a table created in another process is reached by its name; also run on BEAM | Table-driven |
466467

467468
### Stdlib

‎tests/ets_stored_bitstring.erl‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
%% EXPECT: ets_stored_bitstring_ok
2+
%% Regression for non-byte-aligned bitstrings stored in ETS/persistent_term:
3+
%% the writer's exit or garbage collection must not invalidate the value.
4+
%% Copying reads must also survive deletion of the original storage.
5+
%% Run this unchanged on native Nera and BEAM.
6+
-module(ets_stored_bitstring).
7+
-export([test/0]).
8+
9+
test() ->
10+
ets_lifetime(exit),
11+
ets_lifetime(gc),
12+
persistent_lifetime(exit),
13+
persistent_lifetime(gc),
14+
io:format("ets_stored_bitstring_ok~n"),
15+
ok.
16+
17+
ets_lifetime(Lifetime) ->
18+
T = ets:new(stored_bits, [set, public]),
19+
Writer = store(Lifetime, fun() ->
20+
true = ets:insert(T, {bits, values(), unchanged})
21+
end),
22+
churn(),
23+
[{bits, Actual, unchanged}] = ets:lookup(T, bits),
24+
equal({ets, Lifetime}, values(), Actual),
25+
%% Updating another element copies the bitstrings again.
26+
true = ets:update_element(T, bits, {3, updated}),
27+
[{bits, Listed, updated}] = ets:tab2list(T),
28+
true = ets:delete(T),
29+
churn(),
30+
equal({tab2list, Lifetime}, values(), Listed),
31+
stop_writer(Writer),
32+
ok.
33+
34+
persistent_lifetime(Lifetime) ->
35+
Key = {?MODULE, Lifetime},
36+
Writer = store(Lifetime, fun() ->
37+
ok = persistent_term:put(Key, values())
38+
end),
39+
churn(),
40+
Actual = persistent_term:get(Key),
41+
equal({persistent_term, Lifetime}, values(), Actual),
42+
true = persistent_term:erase(Key),
43+
churn(),
44+
equal({erased, Lifetime}, values(), Actual),
45+
stop_writer(Writer),
46+
ok.
47+
48+
%% Construct inside the writer, not in a closure captured from the reader.
49+
%% Heap and refc binaries, sub-byte values, and tails starting inside a byte
50+
%% all reach the shared storage copier as materialized bitstrings.
51+
values() ->
52+
Small = <<"abcdefgh", (id(5)):4>>,
53+
Bytes = list_to_binary(lists:duplicate(id(200), $x)),
54+
Large = <<Bytes/binary, (id(3)):2>>,
55+
Boundary = list_to_binary(lists:duplicate(id(64), $b)),
56+
[{small, Small}, {large, Large}, {tiny, <<(id(5)):3>>},
57+
{boundary, <<Boundary/binary, (id(1)):1>>},
58+
{small_tail, tail(<<0:3, Small/bitstring>>, 3)},
59+
{large_tail, tail(<<"prefix", Large/bitstring>>, 48)}].
60+
61+
id(X) -> X.
62+
tail(Bits, Skip) ->
63+
<<_:Skip, Rest/bitstring>> = Bits,
64+
Rest.
65+
66+
equal(Context, Expected, Actual) ->
67+
case Actual =:= Expected of
68+
true -> ok;
69+
false -> erlang:error({stored_bitstring_changed, Context, Expected, Actual})
70+
end.
71+
72+
store(exit, Body) ->
73+
{Pid, Ref} = spawn_monitor(fun() -> Body() end),
74+
down(Pid, Ref),
75+
none;
76+
store(gc, Body) ->
77+
Parent = self(),
78+
{Pid, Ref} = spawn_monitor(fun() ->
79+
Body(),
80+
_ = [lists:seq(1, 64) || _ <- lists:seq(1, 2000)],
81+
true = erlang:garbage_collect(),
82+
_ = [lists:seq(1, 64) || _ <- lists:seq(1, 2000)],
83+
true = erlang:garbage_collect(),
84+
Parent ! {self(), stored},
85+
receive stop -> ok end
86+
end),
87+
receive
88+
{Pid, stored} -> {Pid, Ref};
89+
{'DOWN', Ref, process, Pid, Reason} -> erlang:error({writer_failed, Reason})
90+
after 5000 -> erlang:error(writer_timeout)
91+
end.
92+
93+
stop_writer(none) -> ok;
94+
stop_writer({Pid, Ref}) ->
95+
Pid ! stop,
96+
down(Pid, Ref).
97+
98+
down(Pid, Ref) ->
99+
receive
100+
{'DOWN', Ref, process, Pid, normal} -> ok;
101+
{'DOWN', Ref, process, Pid, Reason} -> erlang:error({writer_failed, Reason})
102+
after 5000 -> erlang:error(writer_timeout)
103+
end.
104+
105+
%% Reuse freed writer/storage memory before reading it, with no timing sleep.
106+
churn() ->
107+
Parent = self(),
108+
Workers = [spawn_monitor(fun() ->
109+
Held = [{I, lists:seq(1, 64)} || I <- lists:seq(1, 200)],
110+
Parent ! {self(), ready},
111+
receive stop -> length(Held) end
112+
end) || _ <- lists:seq(1, 32)],
113+
[receive {Pid, ready} -> ok after 5000 -> erlang:error(churn_timeout) end
114+
|| {Pid, _} <- Workers],
115+
_ = [erlang:make_tuple(8, I) || I <- lists:seq(1, 20000)],
116+
true = erlang:garbage_collect(),
117+
[Pid ! stop || {Pid, _} <- Workers],
118+
[down(Pid, Ref) || {Pid, Ref} <- Workers],
119+
ok.

0 commit comments

Comments
 (0)