raz / commit 1cef5331

commit
1cef53310de1071dba2d16e726c9d31e33560963
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
f9c2a04b
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.

patch · log

12 files changed, +124 -19

PLAN.md (history) modified +5 -3

@@ -93,9 +93,11 @@
9393 getting memory) and what it may leave out. No language change.
9494 For review before anything else.
9595 - [ ] 2. The report, the new language, for review before any code: the
96- system integer, the size of an address, which `LEN`, indexes and
97- sizes give (the owner's choice), and how it converts to and from
98- `I64`; reals a
96+ system integers `INT` and `UINT`, the size of an address, which
97+ `LEN`, indexes, sizes and `p - q` give, and through which
98+ integers and pointers convert (the owner's choices), and how
99+ they convert to and from `I64`; a module's own choice to leave
100+ out its checks of indexes and slices; reals a
99101 target may leave out; object files, a module's interface and its
100102 IR, and a generic's instances in them, named by their arguments
101103 and the fingerprints they were built from.

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

@@ -12,7 +12,7 @@
1212 | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 593 | 494 | Types, objects, scopes, layout |
1414 | `src/x64.pas` | 693 | 557 | Code buffer, chunks, fixups, places, instruction encoder |
15-| `src/gen.pas` | 1,596 | 1,379 | Items: loading, storing, copying, frames, calls |
15+| `src/gen.pas` | 1,619 | 1,398 | Items: loading, storing, copying, frames, calls |
1616 | `src/parse.pas` | 2,443 | 2,202 | Declarations, statements, expressions, built-ins, recovery, warnings |
1717 | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places |
1818 | `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers |

doc/raz.md (history) modified +44 -9

@@ -1,15 +1,15 @@
11 # The programming language raz
22
33 raz is a small systems language for x86-64 Linux. A raz program can lay
4-a record over a device's registers, read and write memory exactly as
5-written, take part in lock-free algorithms, make system calls and call
4+a record over a device's registers, read and write a device's memory
5+exactly as written, take part in lock-free algorithms, make system calls and call
66 C libraries. It has no garbage collector and no hidden run time: what
77 the program does is what its text says.
88
99 This report defines the language. It is short on purpose. Whatever it
1010 doesn't allow, raz doesn't have.
1111
12-This is version 2026-10.9 of the report; section 12 says what each
12+This is version 2026-10.10 of the report; section 12 says what each
1313 version changed.
1414
1515 The language is frozen, softly, from this version on: it changes only
@@ -75,7 +75,9 @@
7575 An integer is an untyped constant, which takes the type it is used as,
7676 up to the largest `I64`. A hexadecimal number above that, such as
7777 `0FFFFFFFFFFFFFFFFH`, has type `U64`: so `-1` never fits a `U64`, and
78-the big number goes nowhere else without a conversion.
78+the big number goes nowhere else without a conversion. Arithmetic on
79+untyped constants doesn't wrap: a result beyond the `I64`s, such as
80+`9223372036854775807 + 1`, is an error.
7981
8082 A real is an untyped constant too, which takes `F32` or `F64`,
8183 whichever it is used as, rounded to the nearest value of that type;
@@ -205,10 +207,15 @@
205207 that a directory holding a link to `lib/Sys.raz` builds a program over
206208 the C library's `Sys` on Linux too.
207209
208-Since the names of a program's modules are one space, the modules of
209-a library share a prefix of their own, as an X11 library's `Xauth` and
210-`Xkeys` do, and programs leave the library's short names (`Buf`,
211-`Map`, `Out`, `C` and the rest of section 11) to it.
210+**Names are flat.** The names of a program's modules are one space,
211+with no packages or paths. So the names of the library's modules
212+(section 11) are kept for it: a program or another library doesn't
213+name a module of its own `Arena`, `Buf`, `C`, `Files`, `Fmt`, `Heap`,
214+`List`, `Lock`, `Map`, `Math`, `Mem`, `Opt`, `Out`, `Rt`, `Sort`,
215+`Str`, `Sys` or `Thread`, nor any name the library takes later. The
216+modules of any other library share a prefix of their own, as an X11
217+library's `Xauth` and `Xkeys` do, and a program that wants a shorter
218+name imports one: `import Auth := Xauth;`.
212219
213220 The compiler holds every file of such a module to the same interface.
214221 When it takes one of `Name.*.raz`, it also reads the declarations of
@@ -545,7 +552,9 @@
545552 - `p - q`, with both of the same pointer type, is the number of
546553 elements between them, an `I64`.
547554 - `p[i]` is `(p + i)^`. `p[lo..hi]` is the slice of the `hi - lo`
548- elements from `p + lo`. Neither is checked. A pointer to an array
555+ elements from `p + lo`. Neither is checked against a length, which a
556+ pointer doesn't have, but a slice of a pointer that runs backwards,
557+ `hi < lo`, is a failed check (section 6.5). A pointer to an array
549558 isn't indexed this way, since a step would be a whole array: its
550559 elements are `p^[i]`.
551560 - Pointers compare with `= # < <= > >=` as unsigned addresses.
@@ -763,6 +772,17 @@
763772 `FENCE` also orders the program's ordinary accesses against those of
764773 other processors and devices.
765774
775+**Ordinary variables.** Only volatile accesses, `CAS`, `XCHG`, `XADD`
776+and `FENCE` are exact. Any other access is ordinary: an implementation
777+may keep a variable in a register, leave out a write that nothing
778+reads, and change the order of ordinary accesses, as long as each
779+thread, on its own, does what this report says. A variable that one
780+thread writes while another reads or writes it, unless both use
781+volatile accesses or the atomic built-ins, or a `FENCE` orders them,
782+holds no particular value then: a read of it gives a value it had or
783+was given, or a mixture of their bytes. Nothing else of the program
784+becomes undefined by it.
785+
766786 ### 7.7 Defer
767787
768788 `defer s;` runs the statement `s` when the program leaves the statement
@@ -1495,6 +1515,14 @@
14951515 `Program`, and `Sys.wait` says which program changed and how. `Str`
14961516 gains UTF-8 and reading a number, `Map` removing and walking, `Fmt`
14971517 reals; `Sort`, `Math`, `Files` and `Opt` are new (section 11).
1518+- **2026-10.10.** Said at last: what the compiler did and the report
1519+ didn't say, all through; what an implementation chooses, and the
1520+ core every target has (appendix A); the whole grammar (appendix B);
1521+ the library's module names are kept for it (section 3). Mended:
1522+ arithmetic on untyped constants that overflows is an error (section
1523+ 2); a slice of a pointer can't run backwards (section 6.4). Ordinary
1524+ accesses are no longer promised to be made exactly as written, only
1525+ volatile and atomic ones (section 7.6).
14981526
14991527 ## Appendix A. What an implementation chooses
15001528
@@ -1521,6 +1549,13 @@
15211549 every implementation; the words that say so are free.
15221550 - **The command line.** How the compiler is run, its options, and
15231551 where its library lives (sections 1 and 3).
1552+
1553+**The core.** A target may leave out what the system under it can't
1554+give: `Sys` past writing, ending the program and getting memory,
1555+`Thread`, `link` and `extern`. What it must have, besides the whole
1556+language, is the core of the library: `Rt`, `Out`, `Mem`, `Arena`,
1557+`Lock`, `Str`, `Buf` and `Fmt`, and of `Sys` what they need. A
1558+program that keeps to the core runs on every target.
15241559
15251560 ## Appendix B. The grammar
15261561

lib/Rt.raz (history) modified +4 -2

@@ -149,18 +149,20 @@
149149 failed, on standard error, then ends the program by exit with
150150 status 2. what is 0 for an index a and 1 for a slice a..b, of
151151 something whose length is len; 2 for a, which is no position of
152- the enumeration called name. *)
152+ the enumeration called name; 3 for a slice a..b of a pointer, which
153+ runs backwards. *)
153154 proc fail*(what, a, b, len: I64; at: U64; name: ^U8);
154155 begin
155156 where(at);
156157 if what = 0 then
157158 put("index ");
158159 putInt(a);
159- elsif what = 1 then
160+ elsif (what = 1) or (what = 3) then
160161 put("slice ");
161162 putInt(a);
162163 put("..");
163164 putInt(b);
165+ if what = 3 then put(" runs backwards"); end;
164166 else
165167 putInt(a);
166168 put(" isn't a position of ");

src/gen.pas modified +26 -3

@@ -673,8 +673,12 @@
673673 Value(hi);
674674 MovRR(RDX, RAX);
675675 PopR(RCX);
676- if t^.form = fPtr then PopR(R8)
677- else begin
676+ if t^.form = fPtr then begin
677+ { the bounds can't be known, but a slice can't run backwards }
678+ PopR(R8);
679+ Alu(aluCmp, RCX, RDX);
680+ Check(ccLE, 3, lo.pos, RCX, NoMem, 0, -1);
681+ end else begin
678682 m := MemOf(x, R8);
679683 ml := m; ml.disp := ml.disp + 8;
680684 if t^.form = fArray then begin CmpI(RDX, t^.len); ml := NoMem end
@@ -708,7 +712,9 @@
708712 if x.mode = mConst then x.val := x.val xor low(int64)
709713 else begin Value(x); if IsF32(x.typ) then Btc(RAX, 31) else Btc(RAX, 63) end;
710714 end else if IsInt(x.typ) then begin
711- if x.mode = mConst then x.val := Cut(-x.val, x.typ)
715+ if (x.mode = mConst) and (x.typ^.form = fConst) and (x.val = low(int64)) then
716+ MarkAt(x.pos, 'the constant is too big for I64')
717+ else if x.mode = mConst then x.val := Cut(-x.val, x.typ)
712718 else begin Value(x); Unary(3, RAX); Norm(x.typ) end;
713719 end else MarkAt(x.pos, 'can''t negate ' + TypeName(x.typ));
714720 end;
@@ -803,10 +809,27 @@
803809 else begin ShiftI(shShl, RCX, k); Alu(aluSub, RAX, RCX) end;
804810 end;
805811
812+{ Whether a op b, for + - *, overflows an I64: an untyped constant
813+ doesn't wrap, it is an error (report section 4). }
814+function Overflows(op: TSym; a, b: int64): boolean;
815+begin
816+ case op of
817+ sPlus: Overflows := ((b > 0) and (a > high(int64) - b)) or ((b < 0) and (a < low(int64) - b));
818+ sMinus: Overflows := ((b < 0) and (a > high(int64) + b)) or ((b > 0) and (a < low(int64) + b));
819+ sTimes:
820+ if (a = 0) or (b = 0) then Overflows := false
821+ else if a = -1 then Overflows := b = low(int64)
822+ else if b = -1 then Overflows := a = low(int64)
823+ else Overflows := (a * b) div b <> a;
824+ else Overflows := false;
825+ end;
826+end;
827+
806828 function FoldInt(op: TSym; a, b: int64; t: PType; const p: TPos): int64;
807829 var u: boolean;
808830 begin
809831 u := (t^.form = fInt) and not t^.signed;
832+ if (t^.form = fConst) and Overflows(op, a, b) then MarkAt(p, 'the constant is too big for I64');
810833 case op of
811834 sPlus: FoldInt := a + b;
812835 sMinus: FoldInt := a - b;

src/raz.pas modified +1 -1

@@ -31,7 +31,7 @@
3131 { what git describe says, made by make: 0.3.0 at the tag v0.3.0,
3232 0.3.0-5-g38d29a8 five commits past it }
3333 Version = {$I version.inc};
34- Report = '2026-10.9'; { the version of doc/raz.md it follows }
34+ Report = '2026-10.10'; { the version of doc/raz.md it follows }
3535 MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it }
3636 MfdCloexec = 1; { an ELF still executes: the kernel opens it first }
3737

test/err/GenConstOverflow.err added +10 -0

@@ -0,0 +1,10 @@
1+test/err/GenConstOverflow.raz:5:12: error: the constant is too big for I64
2+ 5 | Over = Big + 1;
3+ | ^^^^^^^
4+test/err/GenConstOverflow.raz:6:13: error: the constant is too big for I64
5+ 6 | Twice = 4611686018427387904 * 2;
6+ | ^^^^^^^^^^^^^^^^^^^^^^^
7+test/err/GenConstOverflow.raz:7:13: error: the constant is too big for I64
8+ 7 | Under = -Big - 2;
9+ | ^^^^^^^^
10+raz: 3 errors

test/err/GenConstOverflow.raz added +14 -0

@@ -0,0 +1,14 @@
1+module GenConstOverflow;
2+(* An untyped constant doesn't wrap *)
3+const
4+ Big = 9223372036854775807;
5+ Over = Big + 1;
6+ Twice = 4611686018427387904 * 2;
7+ Under = -Big - 2;
8+ Fine = U8(250) + 10;
9+
10+proc main*;
11+begin
12+end;
13+
14+end GenConstOverflow.

test/run/SlicePtrBackwards.expect added +1 -0

@@ -0,0 +1 @@
1+2 0
\ No newline at end of file

test/run/SlicePtrBackwards.raz added +16 -0

@@ -0,0 +1,16 @@
1+module SlicePtrBackwards;
2+(* A slice of a pointer isn't checked against a length, but it can't
3+ run backwards *)
4+import Out;
5+
6+proc main*;
7+var a: [8]U8; p: ^U8; hi: I64;
8+begin
9+ p := ADR(a[0]);
10+ for hi := 4 to 0 by -2 do
11+ Out.int(LEN(p[2..hi]));
12+ Out.str(" ");
13+ end;
14+end;
15+
16+end SlicePtrBackwards.

test/run/SlicePtrBackwards.status added +1 -0

@@ -0,0 +1 @@
1+2

test/run/SlicePtrBackwards.stderr added +1 -0

@@ -0,0 +1 @@
1+test/run/SlicePtrBackwards.raz:11:23: slice 2..0 runs backwards