|
| 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