Landin library reference discuss source

core/fault/fault.ldn

1--  Generic deterministic failure injection.  The handle holds a copy of the
2--  supplied provider's handle and delegates every successful free.
3
4import core/mem
5
6counters: type = struct
7    remaining: usize
8    attempts: usize
9    delegated: usize
10    successes: usize
11    injected_failures: usize
12    inner_failures: usize
13    frees: usize
14    live: usize
15    growths: usize
16end counters
17
18--- Caller-owned state of an injector: its budget and cumulative counters.
19--- Declare it `zeroed` and give it to `over`, which fills it; it must outlive
20--- the injector handle.
21public space: type = counters
22
23injector_value: type (inner_provider: type) = struct
24    state: ptr mut counters
25    inner: inner_provider
26end injector_value
27
28--- An allocator wrapper that injects failures deterministically and keeps
29--- cumulative counters. It is a handle: copies share one budget. It holds a
30--- copy of its inner provider's handle and delegates to it the allocations,
31--- resizes and in-place growths the budget permits, and every free.
32public injector: type (inner_provider: type) = injector_value(inner_provider)
33
34--- Fill `state` for an injector over `inner` with a budget of
35--- `allowed_attempts` allocation, resize and in-place growth calls that may
36--- reach it, and return its handle. An inner failure consumes one of them
37--- too; an injected failure never calls the inner provider and counts only in
38--- `attempts` and `injected_failures`.
39public over: (inner_provider: type is mem.allocator, inout state: space,
40              inner: inner_provider, allowed_attempts: usize)
41             -> (handle: injector(inner_provider) from state, inner) =
42    state = (remaining: allowed_attempts,
43             attempts: 0, delegated: 0, successes: 0,
44             injected_failures: 0, inner_failures: 0,
45             frees: 0, live: 0, growths: 0)
46    handle = (state: addr state, inner: inner)
47end over
48
49--- Replace the future delegation budget without resetting the counters, so
50--- failed and successful calls stay distinguishable across retries.
51public permit: (inner_provider: type, memory: injector(inner_provider),
52                allowed_attempts: usize) -> none =
53    memory.state.val.remaining = allowed_attempts
54end permit
55
56injector_alloc: (inner_provider: type is mem.allocator,
57                 self: injector(inner_provider),
58                 size: usize, alignment: usize)
59                -> (block: ptr mut u8) ! mem.out_of_memory =
60    state: ptr mut counters = self.state
61    inc state.val.attempts
62    if state.val.remaining == 0 then
63        inc state.val.injected_failures
64        fail mem.out_of_memory
65    end if
66
67    dec state.val.remaining
68    inc state.val.delegated
69    block = mem.allocate(self.inner, size, alignment)
70        else (problem)
71        _ = problem
72        inc state.val.inner_failures
73        fail mem.out_of_memory
74    end
75    inc state.val.successes
76    inc state.val.live
77end injector_alloc
78
79injector_free: (inner_provider: type is mem.allocator,
80                self: injector(inner_provider),
81                block: ptr mut u8, size: usize) -> none =
82    mem.release(self.inner, block, size)
83    --  Allocator free has no result. These two counters are therefore exact
84    --  under the allocator contract: the caller frees each successful block
85    --  once with its original extent. A malformed or duplicate free is still
86    --  delegated, but an inner rejection cannot be reflected in live.
87    state: ptr mut counters = self.state
88    inc state.val.frees
89    if state.val.live > 0 then
90        dec state.val.live
91    end if
92end injector_free
93
94--  Growing in place is an attempt as an allocation is: it spends the
95--  budget, may be refused without reaching the inner provider, and is
96--  otherwise delegated, so a container tested through an injector takes
97--  the in-place path its provider offers (D356).  A refusal of either kind
98--  leaves the block as it was, and the container moves it instead.
99injector_grow: (inner_provider: type is mem.allocator,
100                self: injector(inner_provider), block: ptr mut u8,
101                old_size: usize, new_size: usize, alignment: usize)
102               -> (grown: bool) =
103    state: ptr mut counters = self.state
104    inc state.val.attempts
105    grown = false
106    if state.val.remaining == 0 then
107        inc state.val.injected_failures
108        return
109    end if
110    dec state.val.remaining
111    inc state.val.delegated
112    grown = mem.grow(self.inner, block, old_size, new_size, alignment)
113    if grown then
114        inc state.val.growths
115    else
116        inc state.val.inner_failures
117    end if
118end injector_grow
119
120--  A resize may allocate, so it is an attempt as an allocation is: it spends
121--  the budget, may be refused without reaching the inner provider, and is
122--  otherwise delegated.  The block it returns replaces the one it was given,
123--  so the live count does not change.
124injector_resize: (inner_provider: type is mem.allocator,
125                  self: injector(inner_provider), block: ptr mut u8,
126                  old_size: usize, new_size: usize, alignment: usize)
127                 -> (moved: ptr mut u8) ! mem.out_of_memory =
128    state: ptr mut counters = self.state
129    inc state.val.attempts
130    if state.val.remaining == 0 then
131        inc state.val.injected_failures
132        fail mem.out_of_memory
133    end if
134
135    dec state.val.remaining
136    inc state.val.delegated
137    moved = mem.resize(self.inner, block, old_size, new_size, alignment)
138        else (problem)
139        _ = problem
140        inc state.val.inner_failures
141        fail mem.out_of_memory
142    end
143    inc state.val.successes
144end injector_resize
145
146(inner_provider: type is mem.allocator) injector_value(inner_provider)
147    is mem.allocator (alloc: injector_alloc, grow: injector_grow,
148                      resize: injector_resize, free: injector_free)
149
150--- Return how many attempts may still reach the inner provider.
151public remaining: (inner_provider: type,
152                   memory: injector(inner_provider)) -> (count: usize) =
153    count = memory.state.val.remaining
154end remaining
155
156--- Return all attempts, including injected and inner failures.
157public attempts: (inner_provider: type,
158                  memory: injector(inner_provider)) -> (count: usize) =
159    count = memory.state.val.attempts
160end attempts
161
162--- Return the attempts passed through to the inner provider.
163public delegated: (inner_provider: type,
164                   memory: injector(inner_provider)) -> (count: usize) =
165    count = memory.state.val.delegated
166end delegated
167
168--- Return the number of blocks a delegated allocation or resize gave; a
169--- growth in place gives none and is counted by `growths`.
170public successes: (inner_provider: type,
171                   memory: injector(inner_provider)) -> (count: usize) =
172    count = memory.state.val.successes
173end successes
174
175--- Return attempts rejected by the wrapper before calling the inner provider.
176public injected_failures: (inner_provider: type,
177                           memory: injector(inner_provider))
178                          -> (count: usize) =
179    count = memory.state.val.injected_failures
180end injected_failures
181
182--- Return allocation failures reported by the inner provider.
183public inner_failures: (inner_provider: type,
184                        memory: injector(inner_provider)) -> (count: usize) =
185    count = memory.state.val.inner_failures
186end inner_failures
187
188--- Return the number of blocks the inner provider grew where they are.
189public growths: (inner_provider: type,
190                 memory: injector(inner_provider)) -> (count: usize) =
191    count = memory.state.val.growths
192end growths
193
194--- Return the number of delegated free calls.
195public frees: (inner_provider: type,
196               memory: injector(inner_provider)) -> (count: usize) =
197    count = memory.state.val.frees
198end frees
199
200--- Return successful allocations minus free calls, bounded below by zero.
201--- Exact under the allocator contract; malformed or duplicate frees are not
202--- validated here.
203public live: (inner_provider: type,
204              memory: injector(inner_provider)) -> (count: usize) =
205    --  Successful allocations less delegated frees under valid-free use.
206    count = memory.state.val.live
207end live