From 8aae98b94f0edb979bfd7499a8c28d59a905dfd5 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 17:43:15 +0100 Subject: [PATCH] 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. --- diff --git a/conform/err/FrameSlice.raz b/conform/err/FrameSlice.raz new file mode 100644 index 0000000..15df2b5 --- /dev/null +++ b/conform/err/FrameSlice.raz @@ -0,0 +1,50 @@ +module FrameSlice; +(* a slice of the procedure's own frame can't be returned (report + section 7.3); one of a var parameter, a module variable or what a + pointer points at can *) + +type + Hex = [64]U8; + Pair = record a: [4]U8; s: []U8; end; + +var g: [8]U8; + +proc param(h: Hex): []U8; +begin + return h[0..8]; (* the procedure's copy *) +end; + +proc local: []U8; +var a: [4]U8; +begin + return a; (* the array, silently a slice *) +end; + +proc field(n: INT): ([]U8, INT); +var p: Pair; +begin + return p.a[0..n], n; (* one of several results *) +end; + +proc literal: []INT; +begin + return [1, 2, 3]; (* a hidden variable of the call *) +end; + +proc whole: Hex; +var h: Hex; +begin + return h; (* an array, copied out: fine *) +end; + +proc result: []U8; +begin + return whole()[0..2]; (* the array whole returns, a temporary *) +end; + +proc ok(var h: Hex; s: []U8; p: ^Hex; q: Pair): ([]U8, []U8, []U8, []U8, []U8); +begin + return h[0..8], s[1..2], p^[0..2], g, q.s; +end; + +end FrameSlice. diff --git a/doc/compiler.md b/doc/compiler.md index 61aa309..0e7a7a5 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -19,7 +19,7 @@ | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 1,954 | 1,598 | Items: types, constants, variables, operators, conversions, calls | +| `src/Item.raz` | 1,986 | 1,617 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,000 | 3,478 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | @@ -1270,6 +1270,18 @@ checks, unless the module says `check off;` (`checks`, set by `setChecks`, which `compileFile` sets for each module), and `convert` the check of an enumeration's position. + +The frame: an item's `frame` says, for an array or record, that it lies +in the procedure's own frame (a local, a value parameter, which is a +copy even when passed by address, or a temporary), and for a slice, +that it points there. `makeItem` and `tempItem` set it; `field` and +`index` keep it for a part of the array, and drop it for an element of +a slice or a slice kept in a field, which may point anywhere; `deref` +drops it; `slice`, and `coerce` where an array becomes a slice, carry +it to the slice they make. `notOfFrame`, from `returnValue` and +`returnField`, refuses a returned slice that has it (report 7.3). It +sees only the expression: a slice put in a local slice variable first +has lost it, which the report leaves undefined (section 10). Assignment and conversion: `coerce` makes a value fit the type it's going to, or reports that it can't; it's where a string literal becomes diff --git a/doc/raz.md b/doc/raz.md index b2689f2..324fabe 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -1038,8 +1038,9 @@ A slice returned must not point into the procedure's own frame, which is gone once it returns. It is an error to return, as a slice, a local -array, a value parameter that is an array, or an array literal, or a -slice of one, a field or an element of one included: +array, a value parameter that is an array, an array literal, or an +array a call returns, or a slice of one, a field or an element of one +included: ``` proc short(h: Hex): []U8; (* Hex = [64]U8 *) diff --git a/src/Item.raz b/src/Item.raz index b25b8bf..012ef24 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -55,6 +55,10 @@ deref*: BOOL; (* mVar: at an address from a pointer, which may be nil and isn't tested yet (nilTest) *) obj*: ^Sym.Obj; (* mProc, mBuiltin, mCase; mVar: the variable named *) + frame*: BOOL; (* a slice: points into the procedure's own frame, + which returnValue refuses (report section 7.3); + anything else: lies in that frame, a local, a + value parameter or a temporary, or part of one *) pos*: Diag.Pos; (* where it is in the source, for messages *) end; @@ -276,6 +280,8 @@ else x.base := Gen.bFrame; x.off := o^.val; end; + (* a value parameter passed by address (ind) is a copy all the same *) + x.frame := (o^.lev > 0) and not o^.isVar and (o^.typ^.form # Sym.fSlice); | oProc: x.mode := mProc; | oType: x.mode := mType; | oBuiltin: x.mode := mBuiltin; @@ -422,6 +428,7 @@ x := {}; x.pos := p; x.mode := mVar; x.typ := t; x.base := Gen.bFrame; x.off := off; + x.frame := t^.form # Sym.fSlice; (* a slice made here says where it points *) end; proc toSlot*(var x: Item): (I64, ERROR); @@ -531,7 +538,7 @@ end; proc coerce*(var x: Item; t: ^Sym.Type): ERROR; -var s: ^Sym.Type; tmp: I64; +var s: ^Sym.Type; tmp: I64; inFrame: BOOL; begin if x.mode = mCond then try value(x); end; s := x.typ; @@ -568,8 +575,10 @@ if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end; if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end; if x.view then return noAddress(x, "can't make a slice of a view"); end; + inFrame := x.frame; try address(x); sliceOf(x, t, s^.len); + x.frame := inFrame; elsif (x.mode = mProc) and (t^.form # Sym.fProc) and (s^.base = nil) then return Scan.markAt(x.pos, say1("{} is a procedure, and returns nothing", Diag.source(x.pos))); elsif (x.mode = mProc) and (t^.form # Sym.fProc) then @@ -634,7 +643,7 @@ try value(x); x.mode := mVar; x.base := Gen.bStack; x.off := 0; x.deref := true; x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; - x.view := false; + x.view := false; x.frame := false; return nil; end; @@ -650,6 +659,7 @@ end; x.off := x.off + f^.val; x.typ := f^.typ; + if x.typ^.form = Sym.fSlice then x.frame := false; end; (* a slice kept here points anywhere *) return nil; end; @@ -710,6 +720,9 @@ x.base := Gen.bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false; end; x.typ := t^.base; + (* an element of an array lies where the array does; one of a slice or + through a pointer, and a slice kept in an element, anywhere *) + if (t^.form # Sym.fArray) or (x.typ^.form = Sym.fSlice) then x.frame := false; end; return nil; end; @@ -722,7 +735,7 @@ end; proc slice*(var x, lo, hi: Item): ERROR; -var t, st: ^Sym.Type; tmp: I64; +var t, st: ^Sym.Type; tmp: I64; inFrame: BOOL; begin t := x.typ; if not Sym.isInt(hi.typ) then @@ -733,12 +746,14 @@ if x.view then return noAddress(x, "can't make a slice of a view"); end; if t^.form = Sym.fArray then try notReadOnly(x); end; try value(hi); + inFrame := x.frame and (t^.form # Sym.fPtr); tmp := Gen.allocTemp(16, 8); if t^.form = Sym.fPtr then Gen.slicePtr(t^.base^.size, tmp, checks, lo.pos); else Gen.slice(adrOf(x), t^.form = Sym.fArray, t^.len, t^.base^.size, tmp, checks, lo.pos); end; st := Sym.newType(Sym.fSlice, 16, 8); st^.base := t^.base; tempItem(x, st, tmp); + x.frame := inFrame; return nil; end; @@ -1824,10 +1839,26 @@ (* The result of return x: a value on top, a slice's two words on top, or one copied behind the hidden pointer *) +(* A slice returned must not point into the procedure's own frame, + which is gone once it returns (report section 7.3). Only what can be + seen in the expression is caught: a local array, a value parameter + that is one, an array literal or a temporary, or a slice of one, a + field or an element of one included; x.frame says so, set where the + variable is named and kept through fields, elements and slices. *) +proc notOfFrame(x: Item; t: ^Sym.Type): ERROR; +begin + if (t^.form = Sym.fSlice) and x.frame then + return Scan.markHelp(x.pos, "can't return a slice of the procedure's own frame", + "it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives"); + end; + return nil; +end; + proc returnValue*(var x: Item): ERROR; begin if result = nil then return Scan.markAt(x.pos, "this procedure returns nothing"); end; try coerce(x, result); + try notOfFrame(x, result); if Sym.inTwoRegs(result) then try pushWords(x); elsif Sym.isAggregate(result) then (* a record literal made behind the hidden pointer is there already *) @@ -1845,6 +1876,7 @@ proc returnField*(f: ^Sym.Obj; var x: Item): ERROR; begin try coerce(x, f^.typ); + try notOfFrame(x, f^.typ); if Sym.inTwoRegs(result) then try value(x); elsif Sym.isScalar(f^.typ) then try value(x); diff --git a/src/Raz.raz b/src/Raz.raz index 0babe5e..a0b2c98 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -29,7 +29,7 @@ import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; const - Report = "2026-10.26"; (* the version of doc/raz.md it follows *) + Report = "2026-10.27"; (* the version of doc/raz.md it follows *) MemfdCreate = 319; (* the system call *) MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) Execve = 59; diff --git a/src/check.sh b/src/check.sh index 9cd6653..5aa210b 100755 --- a/src/check.sh +++ b/src/check.sh @@ -12,14 +12,16 @@ # the messages for an I64 that goes to an INT, and a help that # converts the narrower of two integers; and views, v.(T), # whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz -# and, through Math, test/bench/NBody.raz use. +# and, through Math, test/bench/NBody.raz use; and returning a slice of +# the frame as an error (report 2026-10.27). newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz examples/Sh.raz examples/Settings.raz conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz conform/err/IntResultsNarrow.raz conform/err/GenIntTypes.raz - lib/Math.raz examples/Elf.raz test/bench/NBody.raz" + lib/Math.raz examples/Elf.raz test/bench/NBody.raz + conform/err/FrameSlice.raz" newer() { case $1 in conform/*/View*.raz) return 0 ;; esac for g in $newer; do [ $g = $1 ] && return 0; done diff --git a/test/msg/FrameSlice.err b/test/msg/FrameSlice.err new file mode 100644 index 0000000..a08696a --- /dev/null +++ b/test/msg/FrameSlice.err @@ -0,0 +1,21 @@ +conform/err/FrameSlice.raz:14:12: error: can't return a slice of the procedure's own frame + 14 | return h[0..8]; (* the procedure's copy *) + | ^^^^^^^ +help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives +conform/err/FrameSlice.raz:20:12: error: can't return a slice of the procedure's own frame + 20 | return a; (* the array, silently a slice *) + | ^ +help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives +conform/err/FrameSlice.raz:26:12: error: can't return a slice of the procedure's own frame + 26 | return p.a[0..n], n; (* one of several results *) + | ^^^^^^^^^ +help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives +conform/err/FrameSlice.raz:31:12: error: can't return a slice of the procedure's own frame + 31 | return [1, 2, 3]; (* a hidden variable of the call *) + | ^^^^^^^^^ +help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives +conform/err/FrameSlice.raz:42:12: error: can't return a slice of the procedure's own frame + 42 | return whole()[0..2]; (* the array whole returns, a temporary *) + | ^^^^^^^^^^^^^ +help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives +raz: 5 errors