raz / commit 8aae98b9

commit
8aae98b94f0edb979bfd7499a8c28d59a905dfd5
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
cd2f687b
A slice of the frame returned is an error (report 2026-10.27)

Item.frame: an array or record in the procedure's frame (a local, a
value parameter, a temporary), or a slice that points there. makeItem
and tempItem set it; field and index keep it for part of an array and
drop it for a slice's element or a slice kept in a field; deref drops
it; slice and coerce's array-to-slice carry it. notOfFrame, from
returnValue and returnField, refuses a slice that has it.

The report gains the array a call returns among what is caught.
conform/err/FrameSlice, with its message in test/msg; newer than the
seed in src/check.sh. 19 lines of code; make check finds no program
of ours that did it.

patch · log

7 files changed, +127 -9

conform/err/FrameSlice.raz (history) added +50 -0

@@ -0,0 +1,50 @@
1+module FrameSlice;
2+(* a slice of the procedure's own frame can't be returned (report
3+ section 7.3); one of a var parameter, a module variable or what a
4+ pointer points at can *)
5+
6+type
7+ Hex = [64]U8;
8+ Pair = record a: [4]U8; s: []U8; end;
9+
10+var g: [8]U8;
11+
12+proc param(h: Hex): []U8;
13+begin
14+ return h[0..8]; (* the procedure's copy *)
15+end;
16+
17+proc local: []U8;
18+var a: [4]U8;
19+begin
20+ return a; (* the array, silently a slice *)
21+end;
22+
23+proc field(n: INT): ([]U8, INT);
24+var p: Pair;
25+begin
26+ return p.a[0..n], n; (* one of several results *)
27+end;
28+
29+proc literal: []INT;
30+begin
31+ return [1, 2, 3]; (* a hidden variable of the call *)
32+end;
33+
34+proc whole: Hex;
35+var h: Hex;
36+begin
37+ return h; (* an array, copied out: fine *)
38+end;
39+
40+proc result: []U8;
41+begin
42+ return whole()[0..2]; (* the array whole returns, a temporary *)
43+end;
44+
45+proc ok(var h: Hex; s: []U8; p: ^Hex; q: Pair): ([]U8, []U8, []U8, []U8, []U8);
46+begin
47+ return h[0..8], s[1..2], p^[0..2], g, q.s;
48+end;
49+
50+end FrameSlice.

doc/compiler.md (history) modified +13 -1

@@ -19,7 +19,7 @@
1919 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2020 | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler |
2121 | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames |
22-| `src/Item.raz` | 1,954 | 1,598 | Items: types, constants, variables, operators, conversions, calls |
22+| `src/Item.raz` | 1,986 | 1,617 | Items: types, constants, variables, operators, conversions, calls |
2323 | `src/Parse.raz` | 4,000 | 3,478 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
@@ -1270,6 +1270,18 @@
12701270 checks, unless the module says `check off;` (`checks`, set by
12711271 `setChecks`, which `compileFile` sets for each module), and `convert`
12721272 the check of an enumeration's position.
1273+
1274+The frame: an item's `frame` says, for an array or record, that it lies
1275+in the procedure's own frame (a local, a value parameter, which is a
1276+copy even when passed by address, or a temporary), and for a slice,
1277+that it points there. `makeItem` and `tempItem` set it; `field` and
1278+`index` keep it for a part of the array, and drop it for an element of
1279+a slice or a slice kept in a field, which may point anywhere; `deref`
1280+drops it; `slice`, and `coerce` where an array becomes a slice, carry
1281+it to the slice they make. `notOfFrame`, from `returnValue` and
1282+`returnField`, refuses a returned slice that has it (report 7.3). It
1283+sees only the expression: a slice put in a local slice variable first
1284+has lost it, which the report leaves undefined (section 10).
12731285
12741286 Assignment and conversion: `coerce` makes a value fit the type it's
12751287 going to, or reports that it can't; it's where a string literal becomes

doc/raz.md (history) modified +3 -2

@@ -1038,8 +1038,9 @@
10381038
10391039 A slice returned must not point into the procedure's own frame, which
10401040 is gone once it returns. It is an error to return, as a slice, a local
1041-array, a value parameter that is an array, or an array literal, or a
1042-slice of one, a field or an element of one included:
1041+array, a value parameter that is an array, an array literal, or an
1042+array a call returns, or a slice of one, a field or an element of one
1043+included:
10431044
10441045 ```
10451046 proc short(h: Hex): []U8; (* Hex = [64]U8 *)

src/Item.raz (history) modified +35 -3

@@ -55,6 +55,10 @@
5555 deref*: BOOL; (* mVar: at an address from a pointer, which may be
5656 nil and isn't tested yet (nilTest) *)
5757 obj*: ^Sym.Obj; (* mProc, mBuiltin, mCase; mVar: the variable named *)
58+ frame*: BOOL; (* a slice: points into the procedure's own frame,
59+ which returnValue refuses (report section 7.3);
60+ anything else: lies in that frame, a local, a
61+ value parameter or a temporary, or part of one *)
5862 pos*: Diag.Pos; (* where it is in the source, for messages *)
5963 end;
6064
@@ -276,6 +280,8 @@
276280 else
277281 x.base := Gen.bFrame; x.off := o^.val;
278282 end;
283+ (* a value parameter passed by address (ind) is a copy all the same *)
284+ x.frame := (o^.lev > 0) and not o^.isVar and (o^.typ^.form # Sym.fSlice);
279285 | oProc: x.mode := mProc;
280286 | oType: x.mode := mType;
281287 | oBuiltin: x.mode := mBuiltin;
@@ -422,6 +428,7 @@
422428 x := {};
423429 x.pos := p;
424430 x.mode := mVar; x.typ := t; x.base := Gen.bFrame; x.off := off;
431+ x.frame := t^.form # Sym.fSlice; (* a slice made here says where it points *)
425432 end;
426433
427434 proc toSlot*(var x: Item): (I64, ERROR);
@@ -531,7 +538,7 @@
531538 end;
532539
533540 proc coerce*(var x: Item; t: ^Sym.Type): ERROR;
534-var s: ^Sym.Type; tmp: I64;
541+var s: ^Sym.Type; tmp: I64; inFrame: BOOL;
535542 begin
536543 if x.mode = mCond then try value(x); end;
537544 s := x.typ;
@@ -568,8 +575,10 @@
568575 if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end;
569576 if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end;
570577 if x.view then return noAddress(x, "can't make a slice of a view"); end;
578+ inFrame := x.frame;
571579 try address(x);
572580 sliceOf(x, t, s^.len);
581+ x.frame := inFrame;
573582 elsif (x.mode = mProc) and (t^.form # Sym.fProc) and (s^.base = nil) then
574583 return Scan.markAt(x.pos, say1("{} is a procedure, and returns nothing", Diag.source(x.pos)));
575584 elsif (x.mode = mProc) and (t^.form # Sym.fProc) then
@@ -634,7 +643,7 @@
634643 try value(x);
635644 x.mode := mVar; x.base := Gen.bStack; x.off := 0; x.deref := true;
636645 x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false;
637- x.view := false;
646+ x.view := false; x.frame := false;
638647 return nil;
639648 end;
640649
@@ -650,6 +659,7 @@
650659 end;
651660 x.off := x.off + f^.val;
652661 x.typ := f^.typ;
662+ if x.typ^.form = Sym.fSlice then x.frame := false; end; (* a slice kept here points anywhere *)
653663 return nil;
654664 end;
655665
@@ -710,6 +720,9 @@
710720 x.base := Gen.bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false;
711721 end;
712722 x.typ := t^.base;
723+ (* an element of an array lies where the array does; one of a slice or
724+ through a pointer, and a slice kept in an element, anywhere *)
725+ if (t^.form # Sym.fArray) or (x.typ^.form = Sym.fSlice) then x.frame := false; end;
713726 return nil;
714727 end;
715728
@@ -722,7 +735,7 @@
722735 end;
723736
724737 proc slice*(var x, lo, hi: Item): ERROR;
725-var t, st: ^Sym.Type; tmp: I64;
738+var t, st: ^Sym.Type; tmp: I64; inFrame: BOOL;
726739 begin
727740 t := x.typ;
728741 if not Sym.isInt(hi.typ) then
@@ -733,12 +746,14 @@
733746 if x.view then return noAddress(x, "can't make a slice of a view"); end;
734747 if t^.form = Sym.fArray then try notReadOnly(x); end;
735748 try value(hi);
749+ inFrame := x.frame and (t^.form # Sym.fPtr);
736750 tmp := Gen.allocTemp(16, 8);
737751 if t^.form = Sym.fPtr then Gen.slicePtr(t^.base^.size, tmp, checks, lo.pos);
738752 else Gen.slice(adrOf(x), t^.form = Sym.fArray, t^.len, t^.base^.size, tmp, checks, lo.pos); end;
739753 st := Sym.newType(Sym.fSlice, 16, 8);
740754 st^.base := t^.base;
741755 tempItem(x, st, tmp);
756+ x.frame := inFrame;
742757 return nil;
743758 end;
744759
@@ -1824,10 +1839,26 @@
18241839
18251840 (* The result of return x: a value on top, a slice's two words on top,
18261841 or one copied behind the hidden pointer *)
1842+(* A slice returned must not point into the procedure's own frame,
1843+ which is gone once it returns (report section 7.3). Only what can be
1844+ seen in the expression is caught: a local array, a value parameter
1845+ that is one, an array literal or a temporary, or a slice of one, a
1846+ field or an element of one included; x.frame says so, set where the
1847+ variable is named and kept through fields, elements and slices. *)
1848+proc notOfFrame(x: Item; t: ^Sym.Type): ERROR;
1849+begin
1850+ if (t^.form = Sym.fSlice) and x.frame then
1851+ return Scan.markHelp(x.pos, "can't return a slice of the procedure's own frame",
1852+ "it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives");
1853+ end;
1854+ return nil;
1855+end;
1856+
18271857 proc returnValue*(var x: Item): ERROR;
18281858 begin
18291859 if result = nil then return Scan.markAt(x.pos, "this procedure returns nothing"); end;
18301860 try coerce(x, result);
1861+ try notOfFrame(x, result);
18311862 if Sym.inTwoRegs(result) then try pushWords(x);
18321863 elsif Sym.isAggregate(result) then
18331864 (* a record literal made behind the hidden pointer is there already *)
@@ -1845,6 +1876,7 @@
18451876 proc returnField*(f: ^Sym.Obj; var x: Item): ERROR;
18461877 begin
18471878 try coerce(x, f^.typ);
1879+ try notOfFrame(x, f^.typ);
18481880 if Sym.inTwoRegs(result) then try value(x);
18491881 elsif Sym.isScalar(f^.typ) then
18501882 try value(x);

src/Raz.raz (history) modified +1 -1

@@ -29,7 +29,7 @@
2929 import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf;
3030
3131 const
32- Report = "2026-10.26"; (* the version of doc/raz.md it follows *)
32+ Report = "2026-10.27"; (* the version of doc/raz.md it follows *)
3333 MemfdCreate = 319; (* the system call *)
3434 MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *)
3535 Execve = 59;

src/check.sh modified +4 -2

@@ -12,14 +12,16 @@
1212 # the messages for an I64 that goes to an INT, and a help that
1313 # converts the narrower of two integers; and views, v.(T),
1414 # whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz
15-# and, through Math, test/bench/NBody.raz use.
15+# and, through Math, test/bench/NBody.raz use; and returning a slice of
16+# the frame as an error (report 2026-10.27).
1617 newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz
1718 conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz
1819 conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz
1920 examples/Sh.raz examples/Settings.raz
2021 conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz
2122 conform/err/IntResultsNarrow.raz conform/err/GenIntTypes.raz
22- lib/Math.raz examples/Elf.raz test/bench/NBody.raz"
23+ lib/Math.raz examples/Elf.raz test/bench/NBody.raz
24+ conform/err/FrameSlice.raz"
2325 newer() {
2426 case $1 in conform/*/View*.raz) return 0 ;; esac
2527 for g in $newer; do [ $g = $1 ] && return 0; done

test/msg/FrameSlice.err (history) added +21 -0

@@ -0,0 +1,21 @@
1+conform/err/FrameSlice.raz:14:12: error: can't return a slice of the procedure's own frame
2+ 14 | return h[0..8]; (* the procedure's copy *)
3+ | ^^^^^^^
4+help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives
5+conform/err/FrameSlice.raz:20:12: error: can't return a slice of the procedure's own frame
6+ 20 | return a; (* the array, silently a slice *)
7+ | ^
8+help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives
9+conform/err/FrameSlice.raz:26:12: error: can't return a slice of the procedure's own frame
10+ 26 | return p.a[0..n], n; (* one of several results *)
11+ | ^^^^^^^^^
12+help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives
13+conform/err/FrameSlice.raz:31:12: error: can't return a slice of the procedure's own frame
14+ 31 | return [1, 2, 3]; (* a hidden variable of the call *)
15+ | ^^^^^^^^^
16+help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives
17+conform/err/FrameSlice.raz:42:12: error: can't return a slice of the procedure's own frame
18+ 42 | return whole()[0..2]; (* the array whole returns, a temporary *)
19+ | ^^^^^^^^^^^^^
20+help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives
21+raz: 5 errors