Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

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.view());
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,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 @@
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,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,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.view());
}