raz / commit 11573505

commit
11573505368e12a0c381b3b7bafdaf91dc125784
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
0519c7d5
PROPOSALS: Buf out of memory, Buf.spare, a slice that outlives its frame

Met in porting sauce. The first two break no program; the third is a
defect in section 10's promise, mended by an error in return.

patch · log

1 file changed, +85 -0

PROPOSALS.md modified +85 -0

@@ -152,3 +152,88 @@
152152
153153 Not needed: syncing a directory, so that a rename in it is durable, is
154154 already `sync(open(dir, {reading}))`.
155+
156+## `Buf` when memory runs out
157+
158+`Buf.add` and `Buf.str` call `room`, and when it fails they drop the
159+bytes and return as if they had added them. The buffer is then short,
160+and nothing says so: in sauce a short buffer is an object written to
161+disk with bytes missing, a corrupt store. A program must call `room`
162+before every `add` to be safe, which no one does.
163+
164+The proposal: when `room` fails, `add` and `str` stop the program, as a
165+failed check does: "Buf: out of memory", the calls under way, status
166+2. `room` stays as it is, the way for a program that can do something
167+better to ask first.
168+
169+An `ERROR` result would be raz's usual way, but it would put a `try` or
170+an `_ :=` at every call, for a failure almost no program can deal with,
171+and it breaks every program that calls them as statements (a dropped
172+result, an error under `-Werror`): a thaw. Stopping breaks no program
173+whose memory suffices, and changes only what happens when it doesn't,
174+from silent damage to a stop, so it can come with any release.
175+
176+Met in porting sauce, which wraps every `Buf.str` in a `Buf.room`
177+(`Sbase.add`).
178+
179+## Reading into a `Buf`
180+
181+`Sys.read` fills a `[]U8` it is given. A `Buf`'s free room,
182+`data[len..cap]`, is its own, and `bytes` gives only `data[0..len]`, so
183+there is no slice of it to read into. A program that reads a file into
184+a `Buf` reads into an array on its stack and copies that in with
185+`Buf.str`: every byte is handled twice, and the stack holds a buffer as
186+big as each read.
187+
188+```
189+proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, for a read to fill; "" if the allocator has none *)
190+```
191+
192+The program reads into what `spare` gives and adds what it got to
193+`len`, which is already exported:
194+
195+```
196+s := Buf.spare(ADR(b), 65536);
197+n := Sys.read(fd, s);
198+if n > 0 then INC(b.len, n); end;
199+```
200+
201+The slice is good until the buffer next grows, as `bytes` is. It
202+breaks no program. `len` past `cap` is still the program's mistake to
203+make, as it is today; a `Buf.grow(b, n)` that checks it could stand
204+for the `INC`, if wanted.
205+
206+Met in sauce's `readFd`, which reads every file of the store this way.
207+
208+## A slice that outlives what it points at
209+
210+```
211+proc short(h: Hex): []U8; (* Hex = [64]U8 *)
212+begin
213+ return h[0..8];
214+end;
215+
216+proc word: []U8;
217+var a: [4]U8;
218+begin
219+ ...
220+ return a; (* the array, silently a slice *)
221+end;
222+```
223+
224+Both compile without a word, under `-Werror`, and return a slice of
225+the procedure's own frame, which the next call overwrites. Section 10
226+says a program that reaches memory only through checked variables is
227+defined; a slice is checked, so this is a hole in that promise, a
228+defect rather than a wish, and it can be mended without a thaw.
229+
230+The narrow mend, which needs no analysis of where values go: it is an
231+error to return, as a slice, a slice of a local array or of a value
232+parameter that is an array or holds one, the silent conversion of
233+such an array included. It doesn't catch a slice stored in a module
234+variable or through a pointer, which is the rarer mistake, nor one of
235+an `ADR`, which section 10 already gives no promise for. The report
236+says it in section 7.3, Return; the compiler checks it where it
237+checks the type of `return`'s expression.
238+
239+Met in sauce's `Ssha.short`, which takes `var h: Hex` now.