From 1cef53310de1071dba2d16e726c9d31e33560963 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 20:25:41 +0000 Subject: [PATCH] Report 2026-10.10: constants don't overflow, pointer slices don't run backwards Two defects mended, as the freeze allows: arithmetic on untyped constants that leaves the I64s is an error, not a silent wrap; and a slice of a pointer whose hi is below its lo is a failed check, where it made a slice of negative length. Ordinary accesses are promised only to do what one thread would see; volatile and atomic ones stay exact. The library's module names are kept for it, the names of modules staying flat. Appendix A names the core every target has. --- diff --git a/PLAN.md b/PLAN.md index 265cba3..3902f28 100644 --- a/PLAN.md +++ b/PLAN.md @@ -93,9 +93,11 @@ getting memory) and what it may leave out. No language change. For review before anything else. - [ ] 2. The report, the new language, for review before any code: the - system integer, the size of an address, which `LEN`, indexes and - sizes give (the owner's choice), and how it converts to and from - `I64`; reals a + system integers `INT` and `UINT`, the size of an address, which + `LEN`, indexes, sizes and `p - q` give, and through which + integers and pointers convert (the owner's choices), and how + they convert to and from `I64`; a module's own choice to leave + out its checks of indexes and slices; reals a target may leave out; object files, a module's interface and its IR, and a generic's instances in them, named by their arguments and the fingerprints they were built from. diff --git a/doc/compiler.md b/doc/compiler.md index 0afb0c1..ebaae5a 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -12,7 +12,7 @@ | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions | | `src/sym.pas` | 593 | 494 | Types, objects, scopes, layout | | `src/x64.pas` | 693 | 557 | Code buffer, chunks, fixups, places, instruction encoder | -| `src/gen.pas` | 1,596 | 1,379 | Items: loading, storing, copying, frames, calls | +| `src/gen.pas` | 1,619 | 1,398 | Items: loading, storing, copying, frames, calls | | `src/parse.pas` | 2,443 | 2,202 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers | diff --git a/doc/raz.md b/doc/raz.md index cc80e96..f38ee23 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -1,15 +1,15 @@ # The programming language raz raz is a small systems language for x86-64 Linux. A raz program can lay -a record over a device's registers, read and write memory exactly as -written, take part in lock-free algorithms, make system calls and call +a record over a device's registers, read and write a device's memory +exactly as written, take part in lock-free algorithms, make system calls and call C libraries. It has no garbage collector and no hidden run time: what the program does is what its text says. This report defines the language. It is short on purpose. Whatever it doesn't allow, raz doesn't have. -This is version 2026-10.9 of the report; section 12 says what each +This is version 2026-10.10 of the report; section 12 says what each version changed. The language is frozen, softly, from this version on: it changes only @@ -75,7 +75,9 @@ An integer is an untyped constant, which takes the type it is used as, up to the largest `I64`. A hexadecimal number above that, such as `0FFFFFFFFFFFFFFFFH`, has type `U64`: so `-1` never fits a `U64`, and -the big number goes nowhere else without a conversion. +the big number goes nowhere else without a conversion. Arithmetic on +untyped constants doesn't wrap: a result beyond the `I64`s, such as +`9223372036854775807 + 1`, is an error. A real is an untyped constant too, which takes `F32` or `F64`, whichever it is used as, rounded to the nearest value of that type; @@ -205,10 +207,15 @@ that a directory holding a link to `lib/Sys.raz` builds a program over the C library's `Sys` on Linux too. -Since the names of a program's modules are one space, the modules of -a library share a prefix of their own, as an X11 library's `Xauth` and -`Xkeys` do, and programs leave the library's short names (`Buf`, -`Map`, `Out`, `C` and the rest of section 11) to it. +**Names are flat.** The names of a program's modules are one space, +with no packages or paths. So the names of the library's modules +(section 11) are kept for it: a program or another library doesn't +name a module of its own `Arena`, `Buf`, `C`, `Files`, `Fmt`, `Heap`, +`List`, `Lock`, `Map`, `Math`, `Mem`, `Opt`, `Out`, `Rt`, `Sort`, +`Str`, `Sys` or `Thread`, nor any name the library takes later. The +modules of any other library share a prefix of their own, as an X11 +library's `Xauth` and `Xkeys` do, and a program that wants a shorter +name imports one: `import Auth := Xauth;`. The compiler holds every file of such a module to the same interface. When it takes one of `Name.*.raz`, it also reads the declarations of @@ -545,7 +552,9 @@ - `p - q`, with both of the same pointer type, is the number of elements between them, an `I64`. - `p[i]` is `(p + i)^`. `p[lo..hi]` is the slice of the `hi - lo` - elements from `p + lo`. Neither is checked. A pointer to an array + elements from `p + lo`. Neither is checked against a length, which a + pointer doesn't have, but a slice of a pointer that runs backwards, + `hi < lo`, is a failed check (section 6.5). A pointer to an array isn't indexed this way, since a step would be a whole array: its elements are `p^[i]`. - Pointers compare with `= # < <= > >=` as unsigned addresses. @@ -763,6 +772,17 @@ `FENCE` also orders the program's ordinary accesses against those of other processors and devices. +**Ordinary variables.** Only volatile accesses, `CAS`, `XCHG`, `XADD` +and `FENCE` are exact. Any other access is ordinary: an implementation +may keep a variable in a register, leave out a write that nothing +reads, and change the order of ordinary accesses, as long as each +thread, on its own, does what this report says. A variable that one +thread writes while another reads or writes it, unless both use +volatile accesses or the atomic built-ins, or a `FENCE` orders them, +holds no particular value then: a read of it gives a value it had or +was given, or a mixture of their bytes. Nothing else of the program +becomes undefined by it. + ### 7.7 Defer `defer s;` runs the statement `s` when the program leaves the statement @@ -1495,6 +1515,14 @@ `Program`, and `Sys.wait` says which program changed and how. `Str` gains UTF-8 and reading a number, `Map` removing and walking, `Fmt` reals; `Sort`, `Math`, `Files` and `Opt` are new (section 11). +- **2026-10.10.** Said at last: what the compiler did and the report + didn't say, all through; what an implementation chooses, and the + core every target has (appendix A); the whole grammar (appendix B); + the library's module names are kept for it (section 3). Mended: + arithmetic on untyped constants that overflows is an error (section + 2); a slice of a pointer can't run backwards (section 6.4). Ordinary + accesses are no longer promised to be made exactly as written, only + volatile and atomic ones (section 7.6). ## Appendix A. What an implementation chooses @@ -1521,6 +1549,13 @@ every implementation; the words that say so are free. - **The command line.** How the compiler is run, its options, and where its library lives (sections 1 and 3). + +**The core.** A target may leave out what the system under it can't +give: `Sys` past writing, ending the program and getting memory, +`Thread`, `link` and `extern`. What it must have, besides the whole +language, is the core of the library: `Rt`, `Out`, `Mem`, `Arena`, +`Lock`, `Str`, `Buf` and `Fmt`, and of `Sys` what they need. A +program that keeps to the core runs on every target. ## Appendix B. The grammar diff --git a/lib/Rt.raz b/lib/Rt.raz index fd2e1bb..a8d0023 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -149,18 +149,20 @@ failed, on standard error, then ends the program by exit with status 2. what is 0 for an index a and 1 for a slice a..b, of something whose length is len; 2 for a, which is no position of - the enumeration called name. *) + the enumeration called name; 3 for a slice a..b of a pointer, which + runs backwards. *) proc fail*(what, a, b, len: I64; at: U64; name: ^U8); begin where(at); if what = 0 then put("index "); putInt(a); - elsif what = 1 then + elsif (what = 1) or (what = 3) then put("slice "); putInt(a); put(".."); putInt(b); + if what = 3 then put(" runs backwards"); end; else putInt(a); put(" isn't a position of "); diff --git a/src/gen.pas b/src/gen.pas index 3a05500..8ec34d6 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -673,8 +673,12 @@ Value(hi); MovRR(RDX, RAX); PopR(RCX); - if t^.form = fPtr then PopR(R8) - else begin + if t^.form = fPtr then begin + { the bounds can't be known, but a slice can't run backwards } + PopR(R8); + Alu(aluCmp, RCX, RDX); + Check(ccLE, 3, lo.pos, RCX, NoMem, 0, -1); + end else begin m := MemOf(x, R8); ml := m; ml.disp := ml.disp + 8; if t^.form = fArray then begin CmpI(RDX, t^.len); ml := NoMem end @@ -708,7 +712,9 @@ if x.mode = mConst then x.val := x.val xor low(int64) else begin Value(x); if IsF32(x.typ) then Btc(RAX, 31) else Btc(RAX, 63) end; end else if IsInt(x.typ) then begin - if x.mode = mConst then x.val := Cut(-x.val, x.typ) + if (x.mode = mConst) and (x.typ^.form = fConst) and (x.val = low(int64)) then + MarkAt(x.pos, 'the constant is too big for I64') + else if x.mode = mConst then x.val := Cut(-x.val, x.typ) else begin Value(x); Unary(3, RAX); Norm(x.typ) end; end else MarkAt(x.pos, 'can''t negate ' + TypeName(x.typ)); end; @@ -803,10 +809,27 @@ else begin ShiftI(shShl, RCX, k); Alu(aluSub, RAX, RCX) end; end; +{ Whether a op b, for + - *, overflows an I64: an untyped constant + doesn't wrap, it is an error (report section 4). } +function Overflows(op: TSym; a, b: int64): boolean; +begin + case op of + sPlus: Overflows := ((b > 0) and (a > high(int64) - b)) or ((b < 0) and (a < low(int64) - b)); + sMinus: Overflows := ((b < 0) and (a > high(int64) + b)) or ((b > 0) and (a < low(int64) + b)); + sTimes: + if (a = 0) or (b = 0) then Overflows := false + else if a = -1 then Overflows := b = low(int64) + else if b = -1 then Overflows := a = low(int64) + else Overflows := (a * b) div b <> a; + else Overflows := false; + end; +end; + function FoldInt(op: TSym; a, b: int64; t: PType; const p: TPos): int64; var u: boolean; begin u := (t^.form = fInt) and not t^.signed; + if (t^.form = fConst) and Overflows(op, a, b) then MarkAt(p, 'the constant is too big for I64'); case op of sPlus: FoldInt := a + b; sMinus: FoldInt := a - b; diff --git a/src/raz.pas b/src/raz.pas index d45295c..8cec050 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -31,7 +31,7 @@ { what git describe says, made by make: 0.3.0 at the tag v0.3.0, 0.3.0-5-g38d29a8 five commits past it } Version = {$I version.inc}; - Report = '2026-10.9'; { the version of doc/raz.md it follows } + Report = '2026-10.10'; { the version of doc/raz.md it follows } MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } MfdCloexec = 1; { an ELF still executes: the kernel opens it first } diff --git a/test/err/GenConstOverflow.err b/test/err/GenConstOverflow.err new file mode 100644 index 0000000..f0849ac --- /dev/null +++ b/test/err/GenConstOverflow.err @@ -0,0 +1,10 @@ +test/err/GenConstOverflow.raz:5:12: error: the constant is too big for I64 + 5 | Over = Big + 1; + | ^^^^^^^ +test/err/GenConstOverflow.raz:6:13: error: the constant is too big for I64 + 6 | Twice = 4611686018427387904 * 2; + | ^^^^^^^^^^^^^^^^^^^^^^^ +test/err/GenConstOverflow.raz:7:13: error: the constant is too big for I64 + 7 | Under = -Big - 2; + | ^^^^^^^^ +raz: 3 errors diff --git a/test/err/GenConstOverflow.raz b/test/err/GenConstOverflow.raz new file mode 100644 index 0000000..7e77aa7 --- /dev/null +++ b/test/err/GenConstOverflow.raz @@ -0,0 +1,14 @@ +module GenConstOverflow; +(* An untyped constant doesn't wrap *) +const + Big = 9223372036854775807; + Over = Big + 1; + Twice = 4611686018427387904 * 2; + Under = -Big - 2; + Fine = U8(250) + 10; + +proc main*; +begin +end; + +end GenConstOverflow. diff --git a/test/run/SlicePtrBackwards.expect b/test/run/SlicePtrBackwards.expect new file mode 100644 index 0000000..f6f3cec --- /dev/null +++ b/test/run/SlicePtrBackwards.expect @@ -0,0 +1 @@ +2 0 \ No newline at end of file diff --git a/test/run/SlicePtrBackwards.raz b/test/run/SlicePtrBackwards.raz new file mode 100644 index 0000000..93f6ffa --- /dev/null +++ b/test/run/SlicePtrBackwards.raz @@ -0,0 +1,16 @@ +module SlicePtrBackwards; +(* A slice of a pointer isn't checked against a length, but it can't + run backwards *) +import Out; + +proc main*; +var a: [8]U8; p: ^U8; hi: I64; +begin + p := ADR(a[0]); + for hi := 4 to 0 by -2 do + Out.int(LEN(p[2..hi])); + Out.str(" "); + end; +end; + +end SlicePtrBackwards. diff --git a/test/run/SlicePtrBackwards.status b/test/run/SlicePtrBackwards.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/SlicePtrBackwards.status @@ -0,0 +1 @@ +2 diff --git a/test/run/SlicePtrBackwards.stderr b/test/run/SlicePtrBackwards.stderr new file mode 100644 index 0000000..892d235 --- /dev/null +++ b/test/run/SlicePtrBackwards.stderr @@ -0,0 +1 @@ +test/run/SlicePtrBackwards.raz:11:23: slice 2..0 runs backwards