Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

@@ -0,0 +1,5 @@
released once
--- exit 1
sakura: runtime error: Released memory that was not allocated, or was already released.
at main (tests/memory/a_block_released_twice_stops_the_program_1.sakura:6:14)
@@ -0,0 +1,8 @@
import std::io;
def main(): void {
val block := raw_allocate(16);
raw_release(block);
println("released once");
raw_release(block);
println("released twice");
}
@@ -0,0 +1,5 @@
3
--- exit 1
sakura: runtime error: Pointer reaches memory the program does not hold, as past the end of what it points into, or memory released.
at main (tests/memory/a_block_used_after_its_release_stops_the_program_1.sakura:7:19)
@@ -0,0 +1,8 @@
import std::io;
def main(): void {
val block := raw_allocate(16) as *mut i64;
block[0] = 3;
println($"{block[0]}");
raw_release(block as *mut u8);
println($"{block[0]}");
}
@@ -0,0 +1,3 @@
held by main as it exits
--- exit 3
@@ -0,0 +1,12 @@
import std::io;
import system;
def finish(text: &string): void {
println(text.span);
system::exit(3);
}
def main(): void {
var text := string::from("held by main", nothing {});
text.append(" as it exits");
val block := raw_allocate(32);
finish(&text);
}
@@ -0,0 +1,5 @@
made
--- exit 1
sakura: runtime error: Pointer reaches memory the program does not hold, as past the end of what it points into, or memory released.
at main (tests/memory/a_reference_outliving_its_frame_stops_the_program_1.sakura:11:16)
@@ -0,0 +1,12 @@
import std::io;
struct point { var x: i64; var y: i64; }
# The warning that `p` ends with the function is quieted, so the program runs.
@allow def made(): &point {
var p := point { x = 7, y = 8 };
return &p;
}
def main(): void {
val p := &made();
println("made");
println($"{p.x}");
}
@@ -0,0 +1,4 @@
--- exit 1
sakura: runtime error: Pointer reaches memory the program does not hold, as past the end of what it points into, or memory released.
at main (tests/memory/a_view_formatted_after_its_vector_closes_stops_the_program_1.sakura:10:11)
@@ -0,0 +1,11 @@
import std::io;
# A view of a vector closed views memory released, so formatting an element
# stops the program as reading it does.
def main(): void {
var v := vector::<u64>::new();
v.append(9);
val s := v.span;
v.close();
println($"{s[0]}");
}
@@ -0,0 +1,6 @@
1
--- exit 1
sakura: runtime error: Memory was read before anything wrote it, as memory `raw_allocate` gives or a variable `undefined`.
at pick (tests/memory/an_undefined_word_is_checked_at_every_level_1.sakura:10:10)
at main (tests/memory/an_undefined_word_is_checked_at_every_level_1.sakura:15:18)
@@ -0,0 +1,16 @@
import std::io;
# A word `undefined` is read before anything writes it on one path, which stops
# the program at any level, as an array `undefined` does.
def pick(k: u64): u64 {
var x: u64 = undefined;
if k > 5 {
x = 1;
}
return x;
}
def main(): void {
println($"{pick(8)}");
println($"{pick(1)}");
}
@@ -0,0 +1,9 @@
done
--- exit 0
sakura: 7 blocks the program allocated, 127 bytes, were never released, and nothing it keeps reaches them:
1 block, 16 bytes, allocated at chain (tests/memory/blocks_nothing_reaches_are_reported_where_they_were_allocated_1.sakura:10:28)
1 block, 16 bytes, allocated at chain (tests/memory/blocks_nothing_reaches_are_reported_where_they_were_allocated_1.sakura:11:31)
1 block, 40 bytes, allocated at hold (tests/memory/blocks_nothing_reaches_are_reported_where_they_were_allocated_1.sakura:17:36)
3 blocks, 48 bytes, allocated at lose (tests/memory/blocks_nothing_reaches_are_reported_where_they_were_allocated_1.sakura:5:26)
1 block, 7 bytes, allocated at named (tests/memory/blocks_nothing_reaches_are_reported_where_they_were_allocated_1.sakura:14:36)
@@ -0,0 +1,26 @@
import std::io;
struct node { var value: u64; var next: *mut node; }
def lose(count: u64): void {
for i in 0..count {
val n := raw_allocate(size_of::<node>()) as *mut node;
n[0].value = i;
}
}
def chain(): *mut node {
val first := raw_allocate(size_of::<node>()) as *mut node;
first[0].next = raw_allocate(size_of::<node>()) as *mut node;
return first;
}
def named(n: u64): string { return $"block {n}"; }
# What a lost block holds is lost with it, though the block holds its address.
def hold(n: u64): *mut string {
val holder := raw_allocate_zeroed(size_of::<string>()) as *mut string;
holder[0] = named(n);
return holder;
}
def main(): void {
lose(3);
_ = chain();
_ = hold(4);
println("done");
}
@@ -0,0 +1 @@
released
@@ -0,0 +1,10 @@
import std::io;
struct named : owning { var text: string; var more: string; }
def named::close(&mut this): void { this.text.close(); }
def main(): void {
{ var held := box::new(named{ text = $"one {1}", more = $"two {2}" }); }
var many := vector::<named>::new();
many.append(named{ text = $"three {3}", more = $"four {4}" });
many.close();
println("released");
}
@@ -0,0 +1,4 @@
--- exit 1
sakura: runtime error: Memory was read before anything wrote it, as memory `raw_allocate` gives or a variable `undefined`.
at main (tests/memory/comparing_arrays_never_written_stops_the_program_1.sakura:10:16)
@@ -0,0 +1,11 @@
import std::io;
# `==` of arrays of numbers compares their bytes, which nothing wrote past the
# first element.
def main(): void {
var a: u64[4] = undefined;
var b: u64[4] = undefined;
a[0] = 1;
b[0] = 1;
println($"{a == b}");
}
@@ -0,0 +1,4 @@
--- exit 1
sakura: runtime error: Pointer reaches memory the program does not hold, as past the end of what it points into, or memory released.
at main (tests/memory/formatting_a_reference_to_released_memory_stops_the_program_1.sakura:12:11)
@@ -0,0 +1,13 @@
import std::io;
# Interpolating a reference reads what it refers to, which a vector growing
# released, so formatting it stops the program as reading through it does.
def main(): void {
var v := vector::<u64>::new();
v.append(7);
val r := &v[0];
for i in 0..1000u {
v.append(i);
}
println($"{r}");
}
@@ -0,0 +1,4 @@
--- exit 1
sakura: runtime error: Memory was read before anything wrote it, as memory `raw_allocate` gives or a variable `undefined`.
at main (tests/memory/formatting_what_was_never_written_stops_the_program_1.sakura:9:11)
@@ -0,0 +1,10 @@
import std::io;
# Formatting a struct reads each of its fields, one of which nothing wrote.
struct pair { var a: u64; var b: u64; }
def main(): void {
var p: pair = undefined;
p.a = 1;
println($"{p}");
}
@@ -0,0 +1 @@
5 6 4
@@ -0,0 +1,23 @@
import std::io;
# A functor too large for a function's buffer is held in a box, which the function
# gives back where it closes, written over, moved, or ended with its scope, so
# nothing is left for the report, the strings the functors hold included.
struct labeled : invokable<def(): u64> {
var label: string;
var spare: u64[3];
}
def labeled::invoke(&mut this): u64 { return this.label.length; }
def made(text: strview): function<def(): u64> {
return function::new(labeled { label = string::from(text, nothing {}) });
}
def main(): void {
var f := made("first");
val n := f();
f = made("second");
var g := move(&mut f);
val m := g();
var many := vector::<function<def(): u64>>::new();
for i in 0..20u { many.append(made("many")); }
g.close();
println($"{n} {m} {many[19]()}");
}
@@ -0,0 +1,7 @@
1
255
--- exit 1
sakura: runtime error: Integer arithmetic overflowed.
at total (tests/memory/integer_arithmetic_that_overflows_stops_the_program_1.sakura:9:9)
at main (tests/memory/integer_arithmetic_that_overflows_stops_the_program_1.sakura:20:19)
@@ -0,0 +1,21 @@
import std::io;
import std::math;
# Run with `--checks`, the program's own integer arithmetic stops where it gives
# what its type cannot hold; std's wrapping helpers, and std itself, wrap.
def total(xs: span<u8>): u8 {
var sum: u8 = 0;
for x in xs {
sum += x;
}
return sum;
}
def main(): void {
val big: u64 = 0xFFFFFFFFFFFFFFFF;
println($"{math::wrapping_add(big, 2)}");
val fits: u8[2] = [200, 55];
println($"{total(fits[:])}");
val over: u8[2] = [200, 56];
println($"{total(over[:])}");
}
@@ -0,0 +1,6 @@
10
unmapped
--- exit 1
sakura: runtime error: Pointer reaches memory the program does not hold, as past the end of what it points into, or memory released.
at main (tests/memory/memory_mapped_from_the_system_is_held_until_unmapped_1.sakura:14:19)
@@ -0,0 +1,33 @@
import std::io;
import system;
def main(): void {
val page := map(8192);
val words := page as *mut u64;
# Two pages, the second word past the first's end on the second.
words[0] = 1;
words[511] = 2;
words[512] = 3;
words[1023] = 4;
println($"{words[0] + words[511] + words[512] + words[1023]}");
unmap(page, 8192);
println("unmapped");
println($"{words[0]}");
}
# Memory is mapped from the system and given back as each system has it: by
# `mmap` and `munmap` on Linux, and `VirtualAlloc` and `VirtualFree` on Windows.
when os == linux {
def map(bytes: u64): u64 {
return system::linux::syscall(9, 0, bytes, 3, 0x22, 0xFFFFFFFFFFFFFFFF, 0) as u64;
}
def unmap(at: u64, bytes: u64): void {
_ = system::linux::syscall(11, at, bytes, 0, 0, 0, 0);
}
}
else {
def map(bytes: u64): u64 {
return system::windows::VirtualAlloc(0, bytes, 0x3000, 0x04);
}
def unmap(at: u64, bytes: u64): void {
_ = system::windows::VirtualFree(at, 0, 0x8000);
}
}
@@ -0,0 +1,6 @@
36
--- exit 1
sakura: runtime error: Memory was read before anything wrote it, as memory `raw_allocate` gives or a variable `undefined`.
at total (tests/memory/reading_an_undefined_variable_before_writing_it_stops_the_program_1.sakura:11:17)
at main (tests/memory/reading_an_undefined_variable_before_writing_it_stops_the_program_1.sakura:18:19)
@@ -0,0 +1,19 @@
import std::io;
# A variable `undefined` holds what its memory happened to until written.
def total(count: u64): u64 {
var parts: u64[8] = undefined;
for i in 0..count {
parts[i] = i + 1;
}
var sum: u64 = 0;
for i in 0..8u {
sum += parts[i];
}
return sum;
}
def main(): void {
println($"{total(8)}");
println($"{total(5)}");
}
@@ -0,0 +1,5 @@
9
--- exit 1
sakura: runtime error: Pointer reaches memory the program does not hold, as past the end of what it points into, or memory released.
at main (tests/memory/reading_past_what_was_allocated_stops_the_program_1.sakura:7:19)
@@ -0,0 +1,8 @@
import std::io;
def main(): void {
# Ten bytes are asked for, though the block is rounded up to sixteen.
val bytes := raw_allocate(10);
bytes[9] = 9;
println($"{bytes[9]}");
println($"{bytes[12]}");
}
@@ -0,0 +1,5 @@
7 9
--- exit 1
sakura: runtime error: Memory was read before anything wrote it, as memory `raw_allocate` gives or a variable `undefined`.
at main (tests/memory/reading_what_raw_allocate_gave_before_writing_it_stops_the_program_1.sakura:11:19)
@@ -0,0 +1,12 @@
import std::io;
# A block `raw_allocate` gave holds what it may until written. Copying it, as
# growing it does, reads nothing, so only what the program reads stops it.
def main(): void {
val words := raw_allocate(16) as *mut u64;
words[0] = 7;
val grown := raw_resize(words as *mut u8, 32) as *mut u64;
grown[2] = 9;
println($"{grown[0]} {grown[2]}");
println($"{grown[1]}");
}
@@ -0,0 +1,2 @@
7
8
@@ -0,0 +1,19 @@
import std::io;
import std::thread;
# Stored with `release` and loaded with `acquire`, the atomic orders the write
# before the read, and joining a thread orders all it did before what follows,
# so nothing races.
def main(): void {
var data: u64 = 0;
var ready := atomic::<u32>::new(0);
var worker := thread::spawn(def() {
data = 7;
ready.store(1, order::release);
}) else (e) { return; };
while ready.load(order::acquire) == 0 {}
println($"{data}");
worker.join();
data = 8;
println($"{data}");
}
@@ -0,0 +1,4 @@
--- exit 1
sakura: runtime error: Threads raced: memory one wrote, another read or wrote, with nothing ordering the two. The other was in <lambda> (tests/memory/threads_reaching_memory_unordered_stop_the_program_1.sakura:12:10).
at main (tests/memory/threads_reaching_memory_unordered_stop_the_program_1.sakura:16:14)
@@ -0,0 +1,18 @@
import std::io;
import std::thread;
# A thread writes a local, then says so through an atomic it stores `relaxed`,
# which orders nothing, so the thread reading the local once it sees the store
# races the write, and the program stops at the read, saying where the write
# was.
def main(): void {
var data: u64 = 0;
var ready := atomic::<u32>::new(0);
var worker := thread::spawn(def() {
data = 7;
ready.store(1, order::relaxed);
}) else (e) { return; };
while ready.load(order::relaxed) == 0 {}
println($"{data}");
worker.join();
}
@@ -0,0 +1,23 @@
import std::io;
import std::thread;
import std::time;
# The thread's handle is kept where nothing closes it, so the program ends
# without waiting for the thread, which holds a block in its frame.
var worker: maybe<thread::handle<void>> = nothing {};
var ready := atomic::<u32>::new(0);
def work(): void {
val mine := raw_allocate(24);
mine[0] = 1;
ready.store(1, order::release);
while true {
thread::sleep(time::milliseconds(1));
mine[0] += 1;
}
}
def main(): void {
worker = thread::spawn(work) else (e) { return; };
while ready.load(order::acquire) == 0 {
thread::sleep(time::milliseconds(1));
}
println("main ends");
}
@@ -0,0 +1,3 @@
true false true true true
made
still 5
@@ -0,0 +1,30 @@
import std::io;
# `_` names nothing, so a test or an arm naming a field's value `_` takes nothing
# out of the union: a box stays with the holder a reference reaches, and one a
# call made is closed once, with the union.
struct holder { var b: maybe<box<u64>>; }
def holds(h: &holder): bool { return h.b is some(_); }
def lacks(h: &holder): bool { return h.b is not some(_); }
def arm(h: &holder): bool {
match h.b {
some(_) => { return true; }
none => { return false; }
}
return false;
}
def made(): maybe<box<u64>> { return box::<u64>::new(7); }
def main(): void {
var h := holder{ b = box::<u64>::new(5) };
val first := holds(&h);
val missing := lacks(&h);
val matched := arm(&h);
println($"{first} {missing} {matched} {holds(&h)} {made() is some(_)}");
match made() {
some(_) => { println("made"); }
none => {}
}
if h.b is some(&b) { println($"still {b.get()}"); }
}
@@ -0,0 +1 @@
1 5 6 2
@@ -0,0 +1,22 @@
import std::io;
import std::containers;
struct node { var value: u64; var next: *mut node; }
var kept: *mut node;
var names: vector<string>;
def main(): void {
# Blocks a global reaches, and those they reach in turn.
kept = raw_allocate(size_of::<node>()) as *mut node;
kept[0].next = raw_allocate(size_of::<node>()) as *mut node;
kept[0].next[0].next = raw_allocate(size_of::<node>()) as *mut node;
names = vector::<string>::new();
names.append(string::from("kept", nothing {}));
# Owners that close what they hold.
var v := vector::<u64>::new();
v.append(1);
val b := box::<u64>::new(5);
var r := rc::<u64>::new(6);
var also := copy(&r);
val text := $"{v[0]} {b.get()} {also.get()} {r.count()}";
println(text.span);
}