1413 lines
56 KiB
Plaintext
1413 lines
56 KiB
Plaintext
#! @file native.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the tests of executables: the register allocator and the checker
|
||
|
|
#! replaying what it chose, and programs built and run. The encoder of x86-64
|
||
|
|
#! instructions is tested where it is, as `binary::amd64` in `binary`'s
|
||
|
|
#! repository.
|
||
|
|
#! Programs are built over the standard library's repository beside this one,
|
||
|
|
#! else the `std` beside `sakura` or above it. The debugging information and
|
||
|
|
#! the calls of C are tested by building programs over it, then reading the
|
||
|
|
#! files written and what gdb and valgrind make of them, where those are
|
||
|
|
#! installed; where no standard library is found, those tests pass over what
|
||
|
|
#! they test.
|
||
|
|
|
||
|
|
import std::io;
|
||
|
|
import std::process;
|
||
|
|
import system;
|
||
|
|
import std::testing;
|
||
|
|
import sakura::analyzer;
|
||
|
|
import sakura::compiler;
|
||
|
|
import sakura::machine;
|
||
|
|
import sakura::server;
|
||
|
|
import binary::amd64;
|
||
|
|
import binary::elf;
|
||
|
|
import binary::pe;
|
||
|
|
module sakura::native;
|
||
|
|
|
||
|
|
|
||
|
|
@test def vectors_become_values_and_functions_asking_get_versions(t: &mut tester): void {
|
||
|
|
val held := test_library();
|
||
|
|
val library := held.span;
|
||
|
|
val got := lowered_vectors(library, """
|
||
|
|
import std::math;
|
||
|
|
# 16 bytes of lanes, of SSE2 alone: the sum carried around the loop is a
|
||
|
|
# vector the loop's block takes, as is the sum the loop, rotated, leaves
|
||
|
|
# with, and nothing asks the processor.
|
||
|
|
def sum_words(text: strview): u32 {
|
||
|
|
var total := u32x4::splat(0);
|
||
|
|
var at: u64 = 0;
|
||
|
|
while at + 16 <= text.length {
|
||
|
|
total = total + u8x16::load(text, at).bits::<u32, 4>();
|
||
|
|
at += 16;
|
||
|
|
}
|
||
|
|
return total.sum();
|
||
|
|
}
|
||
|
|
# 32 bytes of lanes, of AVX2 and FMA3: the function asks, holding the lanes
|
||
|
|
# in memory, and its version holds the sum in a register.
|
||
|
|
def dots(xs: span<f32>, ys: span<f32>): f32 {
|
||
|
|
var acc := f32x8::splat(0.0);
|
||
|
|
var at: u64 = 0;
|
||
|
|
while at + 8 <= xs.length {
|
||
|
|
acc = f32x8::load(xs, at).fma(&f32x8::load(ys, at), &acc);
|
||
|
|
at += 8;
|
||
|
|
}
|
||
|
|
return acc.sum();
|
||
|
|
}
|
||
|
|
# The sum is called twice, so it is optimized as a function of its own,
|
||
|
|
# rather than only where one call alone would copy it.
|
||
|
|
def main(): void {
|
||
|
|
val xs: f32[16] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
|
||
|
|
if sum_words("0123456789abcdef0123456789abcdef") > 0 && dots(xs[:], xs[:]) > 0.0 { return; }
|
||
|
|
if sum_words("fedcba9876543210fedcba9876543210") > 0 { return; }
|
||
|
|
}
|
||
|
|
""", "sum_words dots");
|
||
|
|
if !t.expects(got.span == "sum_words: v128 2 v256 0;dots: v128 0 v256 0 asks version: v128 0 v256 2 wide;") {
|
||
|
|
println(got);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# An `f32` held as one in the register an arithmetic of `f64`s puts its result
|
||
|
|
# in is widened before the first operand is loaded there, which would lose it:
|
||
|
|
# at `-O1`, `a * c` was worked as `a * a`, so the ray met the ball at 0.
|
||
|
|
@test def a_single_where_the_result_goes_is_widened_first(t: &mut tester): void {
|
||
|
|
val held := test_library();
|
||
|
|
val library := held.span;
|
||
|
|
val text := """
|
||
|
|
import std::io;
|
||
|
|
import std::math;
|
||
|
|
struct v3 { var x: f32; var y: f32; var z: f32; var _w: f32; }
|
||
|
|
def v3::sub(&this, other: &v3): v3 { return v3{ this.x - other.x, this.y - other.y, this.z - other.z }; }
|
||
|
|
def dot(a: &v3, b: &v3): f32 { return a.x * b.x + a.y * b.y + a.z * b.z; }
|
||
|
|
struct ball { var center: v3; var radius: f32; }
|
||
|
|
struct line { var origin: v3; var direction: v3; }
|
||
|
|
def hit(r: &line, s: ball): maybe<f32> {
|
||
|
|
val m := r.origin - s.center;
|
||
|
|
val c := dot(&m, &m) - s.radius * s.radius;
|
||
|
|
return 0f if c <= 0;
|
||
|
|
val b := dot(&m, &r.direction);
|
||
|
|
return nothing {} if b >= 0;
|
||
|
|
val a := dot(&r.direction, &r.direction);
|
||
|
|
val e := b * b - a * c;
|
||
|
|
return nothing {} if e < 0;
|
||
|
|
return (-b - math::sqrt(e)) / a;
|
||
|
|
}
|
||
|
|
def main(): void {
|
||
|
|
val r := line{ v3{ 0, 0, 10 }, v3{ 0, 0, -1 } };
|
||
|
|
if hit(&r, ball{ v3{ 0, 0, 0 }, 2 }) is some(t) { io::println($"hit {t}"); }
|
||
|
|
}
|
||
|
|
""";
|
||
|
|
val directory := scratch_directory("single");
|
||
|
|
val output := $"{directory}/single";
|
||
|
|
for level in 0..3u {
|
||
|
|
val problems := built(library, "single.sakura", text, output.span, level as u32, false);
|
||
|
|
if !t.expects(problems.length == 0) {
|
||
|
|
println(problems[0]);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val alone: strview[1] = [output.span];
|
||
|
|
t.expects(output_of(alone[:]).span == "hit 8\n");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Each operation of 64 bytes of lanes gives the same bits whichever
|
||
|
|
# instructions work it: AVX-512's where the processor has them, as the program
|
||
|
|
# runs with every set it has, and halves of 32 bytes, quarters of 16, or a lane
|
||
|
|
# at a time where `SAKURA_CPU` names fewer.
|
||
|
|
@test def vectors_of_64_bytes_give_the_same_bits_at_every_level(t: &mut tester): void {
|
||
|
|
val held := test_library();
|
||
|
|
val library := held.span;
|
||
|
|
val text := """
|
||
|
|
import std::io;
|
||
|
|
import std::math;
|
||
|
|
var seed: u64 = 0x9E3779B97F4A7C15;
|
||
|
|
def next(): u64 {
|
||
|
|
seed = seed ^ (seed << 13);
|
||
|
|
seed = seed ^ (seed >> 7);
|
||
|
|
seed = seed ^ (seed << 17);
|
||
|
|
return seed;
|
||
|
|
}
|
||
|
|
def random<L, const N: u64>(): simd<L[N]> where L: numeric, ordered, zeroable {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
for i in 0..N {
|
||
|
|
if const L is real {
|
||
|
|
made[i] = ((next() % 4001) as f64 / 8.0 - 250.0) as L;
|
||
|
|
} else {
|
||
|
|
made[i] = next() as L;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
def mix<L, const N: u64>(total: &mut u64, v: &simd<L[N]>): void where L: numeric, ordered, zeroable {
|
||
|
|
val lanes := v.lanes();
|
||
|
|
val bytes := (&lanes) as *u8;
|
||
|
|
for i in 0..N * size_of::<L>() {
|
||
|
|
total = total * 31 + bytes[i] as u64;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
def work<L, const N: u64>(name: strview): void where L: numeric, ordered, zeroable {
|
||
|
|
var total: u64 = 0;
|
||
|
|
for round in 0..4u {
|
||
|
|
val a := random::<L, N>();
|
||
|
|
val b := random::<L, N>();
|
||
|
|
val c := random::<L, N>();
|
||
|
|
mix(&mut total, &(a + b));
|
||
|
|
mix(&mut total, &(a - b));
|
||
|
|
mix(&mut total, &(a * b));
|
||
|
|
mix(&mut total, &(-a));
|
||
|
|
mix(&mut total, &a.min(&b));
|
||
|
|
mix(&mut total, &a.max(&b));
|
||
|
|
mix(&mut total, &a.abs());
|
||
|
|
mix(&mut total, &select(a.less(&b), a, b));
|
||
|
|
mix(&mut total, &simd::<L[N]>::splat(a[3]));
|
||
|
|
total = total * 31 + a.less_equal(&b).bits() as u64 + a.greater(&b).bits() as u64 + a.not_equal(&c).bits() as u64;
|
||
|
|
if const L is real {
|
||
|
|
mix(&mut total, &(a / b));
|
||
|
|
mix(&mut total, &a.fma(&b, &c));
|
||
|
|
mix(&mut total, &a.abs().sqrt());
|
||
|
|
mix(&mut total, &a.floor());
|
||
|
|
mix(&mut total, &a.ceil());
|
||
|
|
mix(&mut total, &a.trunc());
|
||
|
|
mix(&mut total, &a.round_even());
|
||
|
|
}
|
||
|
|
if const L is integer {
|
||
|
|
mix(&mut total, &(a & b));
|
||
|
|
mix(&mut total, &(a | b));
|
||
|
|
mix(&mut total, &(a ^ b));
|
||
|
|
mix(&mut total, &(~a));
|
||
|
|
mix(&mut total, &(a << (5 * round) as u32));
|
||
|
|
mix(&mut total, &(a >> (7 * round) as u32));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
io::println($"{name} {total}");
|
||
|
|
}
|
||
|
|
def main(): void {
|
||
|
|
work::<f32, 16>("f32x16");
|
||
|
|
work::<f64, 8>("f64x8");
|
||
|
|
work::<i16, 32>("i16x32");
|
||
|
|
work::<u16, 32>("u16x32");
|
||
|
|
work::<i32, 16>("i32x16");
|
||
|
|
work::<u32, 16>("u32x16");
|
||
|
|
work::<i64, 8>("i64x8");
|
||
|
|
work::<u64, 8>("u64x8");
|
||
|
|
}
|
||
|
|
""";
|
||
|
|
# As the machine works it, a lane at a time.
|
||
|
|
val worked := """
|
||
|
|
f32x16 10445650904537259042
|
||
|
|
f64x8 7249810911814254236
|
||
|
|
i16x32 3398466155064983875
|
||
|
|
u16x32 12242828003529389835
|
||
|
|
i32x16 11519081872094451891
|
||
|
|
u32x16 11665688344890517076
|
||
|
|
i64x8 17535756281166082821
|
||
|
|
u64x8 14177733201979375858
|
||
|
|
""";
|
||
|
|
val directory := scratch_directory("wide");
|
||
|
|
val output := $"{directory}/wide";
|
||
|
|
val levels: u32[2] = [0, 2];
|
||
|
|
val cpus: strview[4] = ["x86-64", "x86-64-v2", "x86-64-v3", "x86-64-v4"];
|
||
|
|
for &level in levels {
|
||
|
|
val problems := built(library, "wide.sakura", text, output.span, level, false);
|
||
|
|
if !t.expects(problems.length == 0) {
|
||
|
|
println(problems[0]);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
var outputs := vector::<string>::new();
|
||
|
|
for &cpu in cpus {
|
||
|
|
var started := process::command::new(output.span);
|
||
|
|
started.set_variable("SAKURA_CPU", cpu);
|
||
|
|
started.input(process::stdio::none);
|
||
|
|
val got := started.capture() else (_) { t.expects(false); return; };
|
||
|
|
t.expects(got.error.length == 0);
|
||
|
|
outputs.append(string::copy_of(&got.output));
|
||
|
|
}
|
||
|
|
for &each in outputs {
|
||
|
|
if !t.expects(each.span == worked) {
|
||
|
|
println($"{each}against\n{worked}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def executables_built_on_threads_are_those_built_on_one(t: &mut tester): void {
|
||
|
|
val held := test_library();
|
||
|
|
val library := held.span;
|
||
|
|
val text := """
|
||
|
|
import std::io;
|
||
|
|
struct pair { var name: string; var count: u64; }
|
||
|
|
def tally(words: span<strview>): vector<pair> {
|
||
|
|
var made := vector::<pair>::new();
|
||
|
|
for &word in words {
|
||
|
|
var found := false;
|
||
|
|
for &mut each in made {
|
||
|
|
if each.name.span == word { each.count += 1; found = true; }
|
||
|
|
}
|
||
|
|
if !found { made.append(pair { name = string::from(word, nothing {}), count = 1 }); }
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
def main(): void {
|
||
|
|
val words: strview[6] = ["a", "b", "a", "c", "b", "a"];
|
||
|
|
val counted := tally(words[:]);
|
||
|
|
for &each in counted { println($"{each.name}: {each.count}"); }
|
||
|
|
}
|
||
|
|
""";
|
||
|
|
# Each is built to the same path in turn, read before the next is built, as
|
||
|
|
# `-g` writes into the executable the path of its debug information: its name
|
||
|
|
# alone on Linux, but the PDB file's whole path on Windows. The first is
|
||
|
|
# removed before the second is built, so what is read is what that wrote.
|
||
|
|
val directory := scratch_directory("threads");
|
||
|
|
val output := $"{directory}/threads";
|
||
|
|
for level in 0..3u {
|
||
|
|
for debug in 0..2u {
|
||
|
|
t.expects(built(library, "threads.sakura", text, output.span, level as u32, debug == 1, 1).length == 0);
|
||
|
|
val one_bytes := file_bytes(output.span);
|
||
|
|
remove_file(output.span);
|
||
|
|
t.expects(built(library, "threads.sakura", text, output.span, level as u32, debug == 1, 4).length == 0);
|
||
|
|
val many_bytes := file_bytes(output.span);
|
||
|
|
t.expects(one_bytes.length != 0 && one_bytes.span == many_bytes.span);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def an_executable_leaving_contracts_out_is_that_of_the_source_without_them(t: &mut tester): void {
|
||
|
|
val held := test_library();
|
||
|
|
val library := held.span;
|
||
|
|
val text := """
|
||
|
|
import std::io;
|
||
|
|
def total(xs: span<i64>, limit: i64): i64 {
|
||
|
|
expects(xs.length != 0, "A total is of some numbers.");
|
||
|
|
ensures(r: r <= old(limit) * xs.length as i64);
|
||
|
|
var sum: i64 = 0;
|
||
|
|
val add := def(k: i64): i64 {
|
||
|
|
asserts(k <= limit, "Each is under the limit.");
|
||
|
|
return sum + k;
|
||
|
|
};
|
||
|
|
for &x in xs {
|
||
|
|
ensures(sum >= 0);
|
||
|
|
sum = add(x);
|
||
|
|
}
|
||
|
|
return sum;
|
||
|
|
}
|
||
|
|
def main(): void {
|
||
|
|
val xs: i64[3] = [1, 2, 3];
|
||
|
|
io::println($"{total(xs[:], 5)}");
|
||
|
|
}
|
||
|
|
""";
|
||
|
|
# The same, with each contract written over with spaces, so all else is
|
||
|
|
# where it was.
|
||
|
|
var without := string::new(nothing {});
|
||
|
|
for &line in text.split(&'\n') {
|
||
|
|
val trimmed := line.trim();
|
||
|
|
val contract := trimmed.starts_with("expects(") || trimmed.starts_with("asserts(") || trimmed.starts_with("ensures(");
|
||
|
|
for &byte in line {
|
||
|
|
without.push(contract ? ' ' as u8 : byte);
|
||
|
|
}
|
||
|
|
without.push('\n' as u8);
|
||
|
|
}
|
||
|
|
without.resize(without.length - 1);
|
||
|
|
|
||
|
|
val off_directory := scratch_directory("contracts_off");
|
||
|
|
val plain_directory := scratch_directory("contracts_plain");
|
||
|
|
for level in 0..3u {
|
||
|
|
val off := $"{off_directory}/contracts";
|
||
|
|
val plain := $"{plain_directory}/contracts";
|
||
|
|
t.expects(built(library, "contracts.sakura", text, off.span, level as u32, false, 1, true).length == 0);
|
||
|
|
t.expects(built(library, "contracts.sakura", without.span, plain.span, level as u32, false).length == 0);
|
||
|
|
val off_bytes := file_bytes(off.span);
|
||
|
|
val plain_bytes := file_bytes(plain.span);
|
||
|
|
t.expects(off_bytes.length != 0 && off_bytes.span == plain_bytes.span);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# A program built checked stops at an index past the end, and one built without
|
||
|
|
# its checks reads there, while both stop where the processor faults: at null,
|
||
|
|
# within the first page, and at a division by zero, which their runtime reports
|
||
|
|
# with the frames, as the checks did.
|
||
|
|
@test def executables_unchecked_stop_only_where_the_processor_faults(t: &mut tester): void {
|
||
|
|
val held := test_library();
|
||
|
|
val library := held.span;
|
||
|
|
val text := """
|
||
|
|
import std::io;
|
||
|
|
struct cell { var value: i64; var next: *cell; }
|
||
|
|
def divide(a: i64, b: i64): i64 { return a / b; }
|
||
|
|
def follow(at: *cell): i64 { return at[0].next[1].value; }
|
||
|
|
def past(xs: span<i64>, i: u64): i64 { return xs[i]; }
|
||
|
|
def main(args: span<strview>): void {
|
||
|
|
val xs: i64[4] = [1, 2, 3, 4];
|
||
|
|
if args.length == 0 {
|
||
|
|
io::println($"{contracts()} {past(xs[0:2], 3)}");
|
||
|
|
} else if args[0] == "divide" {
|
||
|
|
io::println($"{divide(7, args.length as i64 - 1)}");
|
||
|
|
} else {
|
||
|
|
val first := cell { value = 1 };
|
||
|
|
io::println($"{follow(&first as *cell)}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
""";
|
||
|
|
val directory := scratch_directory("unchecked");
|
||
|
|
for level in 0..3u {
|
||
|
|
val checked := $"{directory}/checked";
|
||
|
|
val unchecked := $"{directory}/unchecked";
|
||
|
|
t.expects(built(library, "faults.sakura", text, checked.span, level as u32, false).length == 0);
|
||
|
|
# Unchecked, as `build` without `--checks`, its contracts are left out too.
|
||
|
|
t.expects(built(library, "faults.sakura", text, unchecked.span, level as u32, false, 1, true, true).length == 0);
|
||
|
|
val alone: strview[1] = [checked.span];
|
||
|
|
val stopped := output_of(alone[:]);
|
||
|
|
t.expects(holds(stopped.span, "Index or slice is out of bounds.") && holds(stopped.span, "at past (faults.sakura:5:"));
|
||
|
|
val bare: strview[1] = [unchecked.span];
|
||
|
|
t.expects(output_of(bare[:]).span == "false 4\n");
|
||
|
|
val both: strview[2] = [checked.span, unchecked.span];
|
||
|
|
for &each in both {
|
||
|
|
val dividing: strview[2] = [each, "divide"];
|
||
|
|
val divided := output_of(dividing[:]);
|
||
|
|
t.expects(holds(divided.span, "Division by zero.") && holds(divided.span, "at divide (faults.sakura:3:"));
|
||
|
|
val following: strview[2] = [each, "follow"];
|
||
|
|
val followed := output_of(following[:]);
|
||
|
|
t.expects(holds(followed.span, "Null reference was unwrapped.") && holds(followed.span, "at follow (faults.sakura:4:"));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def executables_built_with_checks_stop_where_their_arithmetic_overflows(t: &mut tester): void {
|
||
|
|
# Built as `build --checks` builds, the program's own arithmetic stops where it
|
||
|
|
# gives what its type cannot hold, and std's, which wraps on purpose, does
|
||
|
|
# not; built without, both wrap.
|
||
|
|
val held := test_library();
|
||
|
|
val library := held.span;
|
||
|
|
val text := """
|
||
|
|
import std::io;
|
||
|
|
import std::math;
|
||
|
|
def add(a: u8, b: u8): u8 { return a + b; }
|
||
|
|
def times(a: i64, b: i64): i64 { return a * b; }
|
||
|
|
def main(args: span<strview>): void {
|
||
|
|
val big: u64 = 0xFFFFFFFFFFFFFFFF;
|
||
|
|
if args.length == 0 {
|
||
|
|
io::println($"{add(200, 55)} {times(-3, 4)} {math::wrapping_add(big, 2)}");
|
||
|
|
} else if args[0] == "add" {
|
||
|
|
io::println($"{add(200, args.length as u8 + 55)}");
|
||
|
|
} else {
|
||
|
|
io::println($"{times(0x4000000000000000, args.length as i64 + 1)}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
""";
|
||
|
|
val directory := scratch_directory("overflows");
|
||
|
|
for level in 0..3u {
|
||
|
|
val checked := $"{directory}/checked";
|
||
|
|
val wrapping := $"{directory}/wrapping";
|
||
|
|
t.expects(built(library, "overflows.sakura", text, checked.span, level as u32, false, 1, false, false, false, true).length == 0);
|
||
|
|
t.expects(built(library, "overflows.sakura", text, wrapping.span, level as u32, false).length == 0);
|
||
|
|
val alone: strview[1] = [checked.span];
|
||
|
|
t.expects(output_of(alone[:]).span == "255 -12 1\n");
|
||
|
|
val adding: strview[2] = [checked.span, "add"];
|
||
|
|
val added := output_of(adding[:]);
|
||
|
|
t.expects(holds(added.span, "Integer arithmetic overflowed.") && holds(added.span, "at add (overflows.sakura:3:"));
|
||
|
|
val timing: strview[2] = [checked.span, "times"];
|
||
|
|
val timed := output_of(timing[:]);
|
||
|
|
t.expects(holds(timed.span, "Integer arithmetic overflowed.") && holds(timed.span, "at times (overflows.sakura:4:"));
|
||
|
|
val wrapped: strview[2] = [wrapping.span, "add"];
|
||
|
|
t.expects(output_of(wrapped[:]).span == "0\n");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def comparisons_give_the_conditions_their_jumps_test(t: &mut tester): void {
|
||
|
|
t.expects(condition_of(op::ltu) == cc::below);
|
||
|
|
t.expects(condition_of(op::les) == cc::less_equal);
|
||
|
|
t.expects(opposite(cc::below) == cc::above_equal);
|
||
|
|
t.expects(opposite(cc::equal) == cc::not_equal);
|
||
|
|
t.expects(swapped(cc::less) == cc::greater);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lowers a program, places the values of every function it lowers, and
|
||
|
|
#! replays where they were placed, giving what is wrong.
|
||
|
|
#! @param text The program.
|
||
|
|
#! @returns What is wrong, or `the program does not check`.
|
||
|
|
def placing_problems(text: strview): vector<string> {
|
||
|
|
var held := names::new();
|
||
|
|
var notes := vector::<note>::new();
|
||
|
|
var sources := vector::<source>::new();
|
||
|
|
var syntaxes := vector::<syntax>::new();
|
||
|
|
sources.reserve(1);
|
||
|
|
syntaxes.reserve(1);
|
||
|
|
sources.append(source::from("test.sakura", text));
|
||
|
|
syntaxes.append(syntax::new());
|
||
|
|
var lexing := lexer::new(sources.at(0), &mut held, &mut notes, 0);
|
||
|
|
lexing.tokenize_all(syntaxes.at(0));
|
||
|
|
var parsing := parser::new(syntaxes.at(0), &held, &mut notes, 0);
|
||
|
|
val root := parsing.parse();
|
||
|
|
var checker := checker::new(&mut held, &mut notes);
|
||
|
|
checker.add(sources.at(0), syntaxes.at(0), root);
|
||
|
|
checker.check();
|
||
|
|
|
||
|
|
var problems := vector::<string>::new();
|
||
|
|
if notes.length != 0 {
|
||
|
|
problems.append(string::from("the program does not check", nothing {}));
|
||
|
|
return problems;
|
||
|
|
}
|
||
|
|
var lowering := lowerer::new(&mut checker, lowering { verify = true, everything = true, level = 2 });
|
||
|
|
lowering.compile();
|
||
|
|
for &each in lowering.problems {
|
||
|
|
problems.append(string::copy_of(&each));
|
||
|
|
}
|
||
|
|
for i in 0..lowering.program.functions.length {
|
||
|
|
val function := &lowering.program.functions.at(i);
|
||
|
|
continue if !function.lowered;
|
||
|
|
val point := mark_scratch();
|
||
|
|
var memory := vector::<bool>::new(scratch());
|
||
|
|
for &each in lowering.program.signatures.at(function.signature).params {
|
||
|
|
val shape: layout = lowering.program.layouts.at(each);
|
||
|
|
if in_memory(&shape) {
|
||
|
|
memory.append(true);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
for _ in 0..word_count(&shape) {
|
||
|
|
memory.append(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if function.closure {
|
||
|
|
memory.append(false);
|
||
|
|
memory.append(false);
|
||
|
|
}
|
||
|
|
val placed := place_values(&function.body, memory.span);
|
||
|
|
for &each in check_placing(&function.body, &placed) {
|
||
|
|
problems.append($"{function.name}: {each}");
|
||
|
|
}
|
||
|
|
release_scratch(point);
|
||
|
|
}
|
||
|
|
return problems;
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def values_are_placed_where_they_are_read_across_loops_and_calls(t: &mut tester): void {
|
||
|
|
val found := placing_problems("""
|
||
|
|
def pick(a: u64, b: u64, c: u64): u64 { return a > b ? c : a + b; }
|
||
|
|
def many(a: u64, b: u64, c: u64, d: u64, e: u64, f: u64, g: u64, h: u64, i: u64, j: u64, k: u64, l: u64, m: u64): u64 {
|
||
|
|
return a + b * c - d + e * f + g - h + i * j + k - l + m;
|
||
|
|
}
|
||
|
|
def steps(start: u64): u64 {
|
||
|
|
var n := start;
|
||
|
|
var count: u64 = 0;
|
||
|
|
while n != 1 {
|
||
|
|
if n % 2 == 0 { n = n / 2; } else { n = 3 * n + 1; }
|
||
|
|
count += pick(count, n, 1);
|
||
|
|
}
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
def swap_round(rounds: u64): u64 {
|
||
|
|
var a: u64 = 1;
|
||
|
|
var b: u64 = 2;
|
||
|
|
for _ in 0..rounds {
|
||
|
|
val t := a;
|
||
|
|
a = b;
|
||
|
|
b = t + many(a, b, t, a, b, t, a, b, t, a, b, t, a);
|
||
|
|
}
|
||
|
|
return a ^ b;
|
||
|
|
}
|
||
|
|
def mean(xs: f64[4]): f64 {
|
||
|
|
var total := 0.0;
|
||
|
|
for x in xs { total += x * 0.5; }
|
||
|
|
return total / 4.0;
|
||
|
|
}
|
||
|
|
def main(): void {
|
||
|
|
val s := steps(27) + swap_round(10);
|
||
|
|
val m := mean([1.0, 2.0, 3.0, 4.0]);
|
||
|
|
if s > 0 && m > 0.0 { return; }
|
||
|
|
}
|
||
|
|
""");
|
||
|
|
for &each in found {
|
||
|
|
println(each);
|
||
|
|
}
|
||
|
|
t.expects(found.length == 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Debugging information.
|
||
|
|
|
||
|
|
#! The program the tests of debugging information build and debug.
|
||
|
|
const debugged: strview = "\nimport std::io;\nstruct point { var x: i64; var y: i64; }\ndef scale(p: point, by: i64): point {\n val scaled := point{p.x * by, p.y * by};\n return scaled;\n}\ndef main(): void {\n var total: i64 = 0;\n for i in 1..4 {\n val p := scale(point{i, i + 1}, 2);\n total += p.x + p.y;\n }\n println($\"{total}\");\n}\n";
|
||
|
|
|
||
|
|
#! The standard library the tests build over: its repository beside this one,
|
||
|
|
#! as the corpus reads it, else the `std` beside `sakura` or above it, found as
|
||
|
|
#! `core` is.
|
||
|
|
#! @returns Its folder, made absolute, or empty where there is none.
|
||
|
|
def test_library(): string {
|
||
|
|
val beside := beside_repository("../std");
|
||
|
|
return beside if beside.length != 0;
|
||
|
|
return toolchain_folder("std");
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The `system` the tests build over: the library in its repository beside this
|
||
|
|
#! one, as the corpus reads it, else the `system` beside `sakura` or above it.
|
||
|
|
#! @returns Its folder, made absolute, or empty where there is none.
|
||
|
|
def test_system(): string {
|
||
|
|
val beside := beside_repository("../system/system");
|
||
|
|
return beside if beside.length != 0;
|
||
|
|
return toolchain_folder("system");
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A folder by its path from the root of this repository, where the tests run.
|
||
|
|
#! @param relative The path, as `../std`.
|
||
|
|
#! @returns The folder, made absolute, or empty where there is none.
|
||
|
|
def beside_repository(relative: strview): string {
|
||
|
|
val given := path::parse(relative) else (_) {
|
||
|
|
return string::new(nothing {});
|
||
|
|
};
|
||
|
|
val whole := io::absolute(&given) else (_) {
|
||
|
|
return string::new(nothing {});
|
||
|
|
};
|
||
|
|
return string::from(whole.span, nothing {}) if is_folder(whole.span);
|
||
|
|
return string::new(nothing {});
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reads a program and the files of the libraries it reads, as `entry` reads
|
||
|
|
#! them: `core` whole, the types the syntax makes first, and the runtime's
|
||
|
|
#! modules, which an executable is built with, then, a round at a time, the
|
||
|
|
#! modules of `system`, the runtime, and the standard library that the files
|
||
|
|
#! read refer to, a file of another system read as far as its section's line.
|
||
|
|
#! @param library Where the standard library is, or empty for none.
|
||
|
|
#! @param name The path the program is named by.
|
||
|
|
#! @param text The program.
|
||
|
|
#! @param system The system it is read for, as `linux`.
|
||
|
|
#! @param paths Where the path of each file is written, which its source
|
||
|
|
#! refers to, so they are kept as long as the sources are.
|
||
|
|
#! @param names The program's names, which the words of each file join.
|
||
|
|
#! @param notes Where what reading notes is kept.
|
||
|
|
#! @param sources Where the source of each file is written.
|
||
|
|
#! @param syntaxes Where each file is read into.
|
||
|
|
#! @param roots Where the `document` node of each file is written.
|
||
|
|
#! @returns Whether each file is checked whole, as the program's own and the
|
||
|
|
#! runtime's are; the libraries' are checked where it reaches them.
|
||
|
|
def read_program(
|
||
|
|
library: strview,
|
||
|
|
name: strview,
|
||
|
|
text: strview,
|
||
|
|
system: strview,
|
||
|
|
paths: &mut vector<string>,
|
||
|
|
names: &mut names,
|
||
|
|
notes: &mut vector<note>,
|
||
|
|
sources: &mut vector<source>,
|
||
|
|
syntaxes: &mut vector<syntax>,
|
||
|
|
roots: &mut vector<u32>
|
||
|
|
): vector<bool> {
|
||
|
|
var finder := resolver::new();
|
||
|
|
finder.read_for(system, "x86_64", "native");
|
||
|
|
val core := toolchain_folder("core");
|
||
|
|
finder.whole("core", core.span);
|
||
|
|
val platform := test_system();
|
||
|
|
if platform.length != 0 {
|
||
|
|
finder.add_library("system", platform.span);
|
||
|
|
}
|
||
|
|
val runtime := toolchain_folder("runtime");
|
||
|
|
if runtime.length != 0 {
|
||
|
|
finder.add_library("runtime", runtime.span);
|
||
|
|
}
|
||
|
|
if library.length != 0 {
|
||
|
|
finder.add_library("std", library);
|
||
|
|
}
|
||
|
|
|
||
|
|
paths.append(string::from(name, nothing {}));
|
||
|
|
paths.append(string::from(builtin_path, nothing {}));
|
||
|
|
every_file(core.span, paths);
|
||
|
|
for &each in paths {
|
||
|
|
finder.mark(each.span);
|
||
|
|
}
|
||
|
|
val runtime_from := paths.length;
|
||
|
|
finder.want("runtime", paths);
|
||
|
|
val runtime_to := paths.length;
|
||
|
|
|
||
|
|
val heard := listener {
|
||
|
|
noted = def(made: &mut note): void { notes.append(move(made)); },
|
||
|
|
read = def(file: u32): void {}
|
||
|
|
};
|
||
|
|
var done: u64 = 0;
|
||
|
|
while done < paths.length {
|
||
|
|
val from := done;
|
||
|
|
val to := paths.length;
|
||
|
|
for j in from..to {
|
||
|
|
if j == 0 {
|
||
|
|
sources.append(source::from(name, text));
|
||
|
|
} else if paths[j].span == builtin_path {
|
||
|
|
sources.append(source::from(builtin_path, builtin_text));
|
||
|
|
} else {
|
||
|
|
sources.append(source::open(paths[j].span, finder.cut_of(paths[j].span)));
|
||
|
|
}
|
||
|
|
syntaxes.append(syntax::new());
|
||
|
|
}
|
||
|
|
read_files(sources, syntaxes, roots, names, 1, &heard, from, to);
|
||
|
|
for j in from..to {
|
||
|
|
finder.refer(syntaxes.at(j), names, paths);
|
||
|
|
}
|
||
|
|
done = to;
|
||
|
|
}
|
||
|
|
|
||
|
|
var own := vector::<bool>::new();
|
||
|
|
for j in 0..paths.length {
|
||
|
|
own.append(j == 0 || j >= runtime_from && j < runtime_to);
|
||
|
|
}
|
||
|
|
return own;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Builds a program over a standard library into an executable, as
|
||
|
|
#! `entry build` does.
|
||
|
|
#! @param library Where the standard library is.
|
||
|
|
#! @param name The path the program is named by.
|
||
|
|
#! @param text The program.
|
||
|
|
#! @param output Where the executable is written.
|
||
|
|
#! @param level How much its code is optimized.
|
||
|
|
#! @param debug Whether its debugging information is written too.
|
||
|
|
#! @param threads How many threads optimize it and place its values.
|
||
|
|
#! @param contracts_off Whether its contracts are left out.
|
||
|
|
#! @param look What a Windows executable is shown by: its icons, title,
|
||
|
|
#! version, and whether it's windowed.
|
||
|
|
#! @returns What went wrong, which is nothing where it was built.
|
||
|
|
def built(
|
||
|
|
library: strview, name: strview, text: strview, output: strview, level: u32, debug: bool, threads: u32 = 1, contracts_off: bool = false,
|
||
|
|
checks_off: bool = false, for_windows: bool = false, overflows: bool = false, look: build_options = build_options {}
|
||
|
|
): vector<string> {
|
||
|
|
var paths := vector::<string>::new();
|
||
|
|
var names := names::new();
|
||
|
|
var notes := vector::<note>::new();
|
||
|
|
var sources := vector::<source>::new();
|
||
|
|
var syntaxes := vector::<syntax>::new();
|
||
|
|
var roots := vector::<u32>::new();
|
||
|
|
val system: strview = for_windows ? "windows" : test_os;
|
||
|
|
val own := read_program(library, name, text, system, &mut paths, &mut names, &mut notes, &mut sources, &mut syntaxes, &mut roots);
|
||
|
|
var checker := checker::new(&mut names, &mut notes);
|
||
|
|
checker.target.backend = checker.names.intern(&"native");
|
||
|
|
checker.target.os = checker.names.intern(&system);
|
||
|
|
checker.contracts_off = contracts_off;
|
||
|
|
checker.checks_off = checks_off;
|
||
|
|
for j in 0..sources.length {
|
||
|
|
checker.add(sources.at(j), syntaxes.at(j), roots.at(j), own[j]);
|
||
|
|
}
|
||
|
|
var results := check_evaluating(&mut checker, notes.length == 0);
|
||
|
|
var problems := vector::<string>::new();
|
||
|
|
for &each in notes {
|
||
|
|
val at := sources.at(each.file).lines.place_of(each.offset);
|
||
|
|
problems.append($"{sources.at(each.file).path}:{at.line}:{at.column}: {each.code}");
|
||
|
|
}
|
||
|
|
return problems if problems.length != 0;
|
||
|
|
|
||
|
|
var lowered := lowerer::new(&mut checker, lowering {
|
||
|
|
level = level, keep_locals = level == 0, describe = debug, threads = threads, overflow_files = overflows ? 1u : 0u
|
||
|
|
});
|
||
|
|
lowered.evaluated = move(&mut results);
|
||
|
|
lowered.compile();
|
||
|
|
return move(&mut lowered.problems) if lowered.problems.length != 0;
|
||
|
|
return build_executable(&mut lowered, build_options {
|
||
|
|
output = output, debug = debug, windows = test_windows || for_windows, threads = threads,
|
||
|
|
icons = look.icons, title = look.title, semv = look.semv, windowed = look.windowed
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
# The system the tests build executables for, which is the one they run on, so
|
||
|
|
# the executables they build run.
|
||
|
|
when os == linux {
|
||
|
|
val test_os: strview = "linux";
|
||
|
|
val test_windows: bool = false;
|
||
|
|
}
|
||
|
|
when os == windows {
|
||
|
|
val test_os: strview = "windows";
|
||
|
|
val test_windows: bool = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lowers a program over a standard library as an executable optimized with
|
||
|
|
#! inlining, as `built` does, and describes the code of the functions named:
|
||
|
|
#! for each, the vectors its blocks take, as `v128` and `v256`, whether it asks
|
||
|
|
#! which instructions the processor has, and the same of its version for
|
||
|
|
#! x86-64-v3, where it has one.
|
||
|
|
#! @param library Where the standard library is.
|
||
|
|
#! @param text The program.
|
||
|
|
#! @param names The functions, separated by spaces.
|
||
|
|
#! @returns The description, or what went wrong.
|
||
|
|
def lowered_vectors(library: strview, text: strview, names: strview): string {
|
||
|
|
var paths := vector::<string>::new();
|
||
|
|
var held := names::new();
|
||
|
|
var notes := vector::<note>::new();
|
||
|
|
var sources := vector::<source>::new();
|
||
|
|
var syntaxes := vector::<syntax>::new();
|
||
|
|
var roots := vector::<u32>::new();
|
||
|
|
val own := read_program(library, "test0.sakura", text, test_os, &mut paths, &mut held, &mut notes, &mut sources, &mut syntaxes, &mut roots);
|
||
|
|
var checker := checker::new(&mut held, &mut notes);
|
||
|
|
checker.target.backend = checker.names.intern(&"native");
|
||
|
|
checker.target.os = checker.names.intern(&test_os);
|
||
|
|
for j in 0..sources.length {
|
||
|
|
checker.add(sources.at(j), syntaxes.at(j), roots.at(j), own[j]);
|
||
|
|
}
|
||
|
|
var results := check_evaluating(&mut checker, notes.length == 0);
|
||
|
|
var out := string::new(nothing {});
|
||
|
|
if notes.length != 0 {
|
||
|
|
out.append("the program does not check");
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
var lowered := lowerer::new(&mut checker, lowering { level = 2, verify = true });
|
||
|
|
lowered.evaluated = move(&mut results);
|
||
|
|
lowered.compile();
|
||
|
|
for &each in lowered.problems {
|
||
|
|
out.append(each.span);
|
||
|
|
out.append("\n");
|
||
|
|
}
|
||
|
|
var wanted := string::from(names, nothing {});
|
||
|
|
val functions := &lowered.program.functions;
|
||
|
|
for name in wanted.split(' ') {
|
||
|
|
for i in 0..functions.length {
|
||
|
|
val function := &functions[i];
|
||
|
|
continue if function.name.span != name || function.body.wide;
|
||
|
|
out.append($"{name}:{described_vectors(&function.body)}");
|
||
|
|
if function.version != 0 {
|
||
|
|
out.append($" version:{described_vectors(&functions[function.version - 1].body)}");
|
||
|
|
}
|
||
|
|
out.append(";");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The vectors code's blocks take, and whether it asks which instructions the
|
||
|
|
#! processor has, and holds 32 bytes of lanes, as `lowered_vectors` describes.
|
||
|
|
#! @param body The code.
|
||
|
|
#! @returns The description.
|
||
|
|
def described_vectors(body: &code): string {
|
||
|
|
var narrow: u32 = 0;
|
||
|
|
var wide: u32 = 0;
|
||
|
|
for b in 0..body.blocks.length {
|
||
|
|
continue if body.blocks.at(b).gone;
|
||
|
|
for ¶m in body.blocks.at(b).params.span {
|
||
|
|
val kind := body.origins.at(param).kind;
|
||
|
|
if kind == vt::v128 {
|
||
|
|
narrow++;
|
||
|
|
} else if kind == vt::v256 {
|
||
|
|
wide++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
var asks := false;
|
||
|
|
for i in 0..body.insts.length {
|
||
|
|
asks = asks || body.insts.at(i).kind == op::cpu;
|
||
|
|
}
|
||
|
|
var made := $" v128 {narrow} v256 {wide}";
|
||
|
|
if asks {
|
||
|
|
made.append(" asks");
|
||
|
|
}
|
||
|
|
if body.wide {
|
||
|
|
made.append(" wide");
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A directory of its own for what a test writes, made where it is not yet,
|
||
|
|
#! named for the process too, so tests running at once each write their own.
|
||
|
|
#! @param name The test's name for it.
|
||
|
|
#! @returns Its path.
|
||
|
|
def scratch_directory(name: strview): string {
|
||
|
|
var base: strview = "/tmp";
|
||
|
|
if process::variable("TMPDIR") is some(found) {
|
||
|
|
base = found;
|
||
|
|
} else if process::variable("TEMP") is some(found) {
|
||
|
|
base = found;
|
||
|
|
} else if process::variable("TMP") is some(found) {
|
||
|
|
base = found;
|
||
|
|
}
|
||
|
|
val made := $"{base}/sakura-native-{system::getpid()}-{name}";
|
||
|
|
var at := path::parse(made.span) else (_) { return made; };
|
||
|
|
_ = io::create_directories(&at);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The bytes of a file, or none where it can't be read.
|
||
|
|
#! @param at The file's path.
|
||
|
|
#! @returns The bytes.
|
||
|
|
def file_bytes(at: strview): string {
|
||
|
|
var parsed := path::parse(at) else (_) { return string::new(nothing {}); };
|
||
|
|
var read := io::read_file(&parsed) else (_) { return string::new(nothing {}); };
|
||
|
|
return move(&mut read);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a file is there.
|
||
|
|
#! @param at The file's path.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def file_exists(at: strview): bool {
|
||
|
|
var parsed := path::parse(at) else (_) { return false; };
|
||
|
|
return io::exists(&parsed);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Removes a file, where it is there.
|
||
|
|
#! @param at The file's path.
|
||
|
|
def remove_file(at: strview): void {
|
||
|
|
var parsed := path::parse(at) else (_) { return; };
|
||
|
|
_ = io::remove(&parsed);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A little-endian number of a file's bytes, or 0 past their end.
|
||
|
|
#! @param bytes The bytes.
|
||
|
|
#! @param at Where it begins.
|
||
|
|
#! @param size How many bytes it takes.
|
||
|
|
#! @returns The number.
|
||
|
|
def number_at(bytes: strview, at: u64, size: u64): u64 {
|
||
|
|
return 0 if at + size > bytes.length;
|
||
|
|
var value: u64 = 0;
|
||
|
|
for i in 0..size {
|
||
|
|
value = value | (bytes.at(at + i) as u64 << (8 * i));
|
||
|
|
}
|
||
|
|
return value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A section of an ELF file, as its header describes it.
|
||
|
|
struct found_section {
|
||
|
|
var found: bool;
|
||
|
|
var kind: u64;
|
||
|
|
var address: u64;
|
||
|
|
var size: u64;
|
||
|
|
var bytes: strview; #! What the file holds of it, or nothing where it holds none.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The section of an ELF file named so, or one not found.
|
||
|
|
#! @param bytes The file's bytes.
|
||
|
|
#! @param name The section's name.
|
||
|
|
#! @returns The section.
|
||
|
|
def section_named(bytes: strview, name: strview): found_section {
|
||
|
|
val headers := number_at(bytes, 40, 8);
|
||
|
|
val count := number_at(bytes, 60, 2);
|
||
|
|
val names := headers + 64 * number_at(bytes, 62, 2);
|
||
|
|
val names_at := number_at(bytes, names + 24, 8);
|
||
|
|
for i in 0..count {
|
||
|
|
val at := headers + 64 * i;
|
||
|
|
val named := names_at + number_at(bytes, at, 4);
|
||
|
|
continue if named + name.length >= bytes.length;
|
||
|
|
continue if bytes.substr(named, name.length) != name || bytes.at(named + name.length) != 0;
|
||
|
|
val kind := number_at(bytes, at + 4, 4);
|
||
|
|
val size := number_at(bytes, at + 32, 8);
|
||
|
|
val offset := number_at(bytes, at + 24, 8);
|
||
|
|
return found_section {
|
||
|
|
found = true,
|
||
|
|
kind = kind,
|
||
|
|
address = number_at(bytes, at + 16, 8),
|
||
|
|
size = size,
|
||
|
|
bytes = kind == 8 ? strview::blank() : bytes.substr(offset, size)
|
||
|
|
};
|
||
|
|
}
|
||
|
|
return found_section { bytes = strview::blank() };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A loaded segment of an ELF file, as its program header describes it.
|
||
|
|
struct found_segment {
|
||
|
|
var flags: u64;
|
||
|
|
var offset: u64;
|
||
|
|
var address: u64;
|
||
|
|
var held: u64; #! The bytes the file holds of it.
|
||
|
|
var memory: u64; #! The bytes it takes loaded.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The loaded segments of an ELF file.
|
||
|
|
#! @param bytes The file's bytes.
|
||
|
|
#! @returns The segments, in order.
|
||
|
|
def loads_of(bytes: strview): vector<found_segment> {
|
||
|
|
var made := vector::<found_segment>::new();
|
||
|
|
val headers := number_at(bytes, 32, 8);
|
||
|
|
for i in 0..number_at(bytes, 56, 2) {
|
||
|
|
val at := headers + 56 * i;
|
||
|
|
continue if number_at(bytes, at, 4) != 1;
|
||
|
|
made.append(found_segment {
|
||
|
|
flags = number_at(bytes, at + 4, 4),
|
||
|
|
offset = number_at(bytes, at + 8, 8),
|
||
|
|
address = number_at(bytes, at + 16, 8),
|
||
|
|
held = number_at(bytes, at + 32, 8),
|
||
|
|
memory = number_at(bytes, at + 40, 8)
|
||
|
|
});
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Runs a program, giving what it wrote to its output and its error, one after
|
||
|
|
#! the other, or nothing where it could not be started.
|
||
|
|
#! @param words The program, then its arguments.
|
||
|
|
#! @returns What it wrote.
|
||
|
|
def output_of(words: span<strview>): string {
|
||
|
|
var started := process::command::new(words.at(0));
|
||
|
|
for i in 1..words.length {
|
||
|
|
started.argument(words.at(i));
|
||
|
|
}
|
||
|
|
started.input(process::stdio::none);
|
||
|
|
val got := started.capture() else (_) { return string::new(nothing {}); };
|
||
|
|
var made := string::copy_of(&got.output);
|
||
|
|
made.append(got.error.span);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether text holds other text.
|
||
|
|
#! @param text The text searched.
|
||
|
|
#! @param piece The text looked for.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def holds(text: strview, piece: strview): bool {
|
||
|
|
return true if piece.length == 0;
|
||
|
|
return false if piece.length > text.length;
|
||
|
|
for i in 0..text.length - piece.length + 1 {
|
||
|
|
return true if text.substr(i, piece.length) == piece;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
when os == linux {
|
||
|
|
# A program calls C's own library through an `extern module`: functions taking
|
||
|
|
# and giving numbers, pointers, and a struct by value, a union given by its
|
||
|
|
# address, and a function of the program C calls back.
|
||
|
|
@test def c_functions_are_called_through_extern_sections(t: &mut tester): void {
|
||
|
|
val standard := test_library();
|
||
|
|
return if standard.length == 0;
|
||
|
|
val library := standard.span;
|
||
|
|
val directory := scratch_directory("extern");
|
||
|
|
val made := $"{directory}/calls_c";
|
||
|
|
val problems := built(library, "calls_c.sakura", """
|
||
|
|
import std::io;
|
||
|
|
extern import "libc.so.6" when os == linux;
|
||
|
|
|
||
|
|
def compare_ints(a: *u8, b: *u8): i32 { return (a as *i32)[0] - (b as *i32)[0]; }
|
||
|
|
|
||
|
|
def main(): void {
|
||
|
|
val q := clib::div(17, 5);
|
||
|
|
println($"{clib::length_of("hello\\0")} {q.quot} {q.rem}");
|
||
|
|
var numbers: i32[5] = [5, 3, 1, 4, 2];
|
||
|
|
clib::qsort((&mut numbers) as *mut u8, 5, 4, compare_ints);
|
||
|
|
println($"{numbers[0]} {numbers[1]} {numbers[2]} {numbers[3]} {numbers[4]}");
|
||
|
|
var w := clib::word { whole = 0x0102030405060708 };
|
||
|
|
var copy := clib::word { whole = 0 };
|
||
|
|
clib::memcpy((&mut copy) as *mut u8, (&w) as *u8, 8);
|
||
|
|
println($"{copy.halves[0]} {copy.halves[1]} {copy.bytes[0]}");
|
||
|
|
}
|
||
|
|
|
||
|
|
extern module clib when os == linux;
|
||
|
|
|
||
|
|
pub struct quotient { pub var quot: i32; pub var rem: i32; }
|
||
|
|
pub union word { pub var whole: u64; pub var halves: u32[2]; pub var bytes: u8[8]; }
|
||
|
|
|
||
|
|
pub def strlen(text: *u8): u64;
|
||
|
|
pub def div(a: i32, b: i32): quotient;
|
||
|
|
pub def qsort(base: *mut u8, count: u64, size: u64, compare: cfn<def(*u8, *u8): i32>): void;
|
||
|
|
pub def memcpy(into: *mut u8, from: *u8, count: u64): *mut u8;
|
||
|
|
|
||
|
|
pub def length_of(text: strview): u64 { return strlen(text.data); }
|
||
|
|
""", made.span, 2, false);
|
||
|
|
for &each in problems {
|
||
|
|
println(each);
|
||
|
|
}
|
||
|
|
return if !t.expects(problems.length == 0);
|
||
|
|
val ran: strview[1] = [made.span];
|
||
|
|
t.expects(output_of(ran[:]).span == "5 3 2\n1 2 3 4 5\n84281096 16909060 8\n");
|
||
|
|
}
|
||
|
|
|
||
|
|
# Debugging information goes to a file of its own beside the executable, which
|
||
|
|
# the executable names, and leaves everything the executable loads as it was.
|
||
|
|
@test def debugging_information_is_written_beside_the_executable(t: &mut tester): void {
|
||
|
|
val standard := test_library();
|
||
|
|
return if standard.length == 0;
|
||
|
|
val library := standard.span;
|
||
|
|
val directory := scratch_directory("described");
|
||
|
|
val plain := $"{directory}/plain";
|
||
|
|
val debug := $"{directory}/debug";
|
||
|
|
val petal := $"{directory}/debug.petal";
|
||
|
|
remove_file($"{plain}.petal".span);
|
||
|
|
val plain_problems := built(library, "test0.sakura", debugged, plain.span, 0, false);
|
||
|
|
val debug_problems := built(library, "test0.sakura", debugged, debug.span, 0, true);
|
||
|
|
for &each in plain_problems {
|
||
|
|
println(each);
|
||
|
|
}
|
||
|
|
for &each in debug_problems {
|
||
|
|
println(each);
|
||
|
|
}
|
||
|
|
return if !t.expects(plain_problems.length == 0 && debug_problems.length == 0);
|
||
|
|
t.expects(file_exists(petal.span));
|
||
|
|
t.expects(!file_exists($"{plain}.petal".span));
|
||
|
|
val ran: strview[1] = [debug.span];
|
||
|
|
t.expects(output_of(ran[:]).span == "30\n");
|
||
|
|
|
||
|
|
val plain_file := file_bytes(plain.span);
|
||
|
|
val debug_file := file_bytes(debug.span);
|
||
|
|
val petal_file := file_bytes(petal.span);
|
||
|
|
val loaded: strview[3] = [".rodata", ".text", ".lines"];
|
||
|
|
for &name in loaded {
|
||
|
|
val a := section_named(plain_file.span, name);
|
||
|
|
val b := section_named(debug_file.span, name);
|
||
|
|
val c := section_named(petal_file.span, name);
|
||
|
|
t.expects(a.found && b.found && a.bytes == b.bytes);
|
||
|
|
# The file describes each where the executable has it, holding none of it.
|
||
|
|
t.expects(c.found && c.kind == 8 && c.address == b.address && c.size == b.size);
|
||
|
|
}
|
||
|
|
|
||
|
|
# Debuggers match the file with the executable by the build's note and by
|
||
|
|
# the checksum the executable gives with its name.
|
||
|
|
val note := section_named(plain_file.span, ".note.gnu.build-id");
|
||
|
|
val noted := section_named(debug_file.span, ".note.gnu.build-id");
|
||
|
|
val described := section_named(petal_file.span, ".note.gnu.build-id");
|
||
|
|
t.expects(note.found && note.size == 36 && note.bytes.substr(12, 3) == "GNU");
|
||
|
|
t.expects(note.bytes == noted.bytes && note.bytes == described.bytes);
|
||
|
|
t.expects(!section_named(plain_file.span, ".gnu_debuglink").found);
|
||
|
|
val link := section_named(debug_file.span, ".gnu_debuglink");
|
||
|
|
val name: strview = "debug.petal";
|
||
|
|
val at := (name.length + 1 + 3) / 4 * 4;
|
||
|
|
t.expects(link.found && link.size == at + 4 && link.bytes.substr(0, name.length) == name);
|
||
|
|
t.expects(number_at(link.bytes, at, 4) == elf::crc32(petal_file.span) as u64);
|
||
|
|
|
||
|
|
# The file describes the segments the executable loads, holding none of
|
||
|
|
# them, and the writable one is held in the executable's file.
|
||
|
|
val loads := loads_of(debug_file.span);
|
||
|
|
val described_loads := loads_of(petal_file.span);
|
||
|
|
t.expects(loads.length != 0 && loads.length == described_loads.length);
|
||
|
|
for i in 0..(loads.length < described_loads.length ? loads.length : described_loads.length) {
|
||
|
|
val a: found_segment = loads.at(i);
|
||
|
|
val b: found_segment = described_loads.at(i);
|
||
|
|
t.expects(a.offset == b.offset && a.address == b.address && a.memory == b.memory && a.flags == b.flags && b.held == 0);
|
||
|
|
t.expects((a.flags & 2) == 0 || a.held == a.memory);
|
||
|
|
}
|
||
|
|
|
||
|
|
val debugging: strview[5] = [".debug_info", ".debug_abbrev", ".debug_line", ".debug_frame", ".debug_str"];
|
||
|
|
for &name in debugging {
|
||
|
|
val held := section_named(petal_file.span, name);
|
||
|
|
t.expects(held.found && held.size > 0);
|
||
|
|
t.expects(!section_named(debug_file.span, name).found);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# A section of a PE file, as its header describes it.
|
||
|
|
struct pe_found_section {
|
||
|
|
var found: bool;
|
||
|
|
var address: u64;
|
||
|
|
var size: u64; #! Of what it holds in memory.
|
||
|
|
var flags: u64; #! Its characteristics.
|
||
|
|
var bytes: strview; #! What the file holds of it.
|
||
|
|
}
|
||
|
|
|
||
|
|
# The section of a PE file named so, or one not found. A name longer than eight
|
||
|
|
# bytes is held in the table of strings after the symbols, named by `/` and its
|
||
|
|
# offset there.
|
||
|
|
def pe_section_named(bytes: strview, name: strview): pe_found_section {
|
||
|
|
# The signature `PE\0\0` is at `pe`, and the file header's fields after it.
|
||
|
|
val pe := number_at(bytes, 0x3C, 4);
|
||
|
|
val sections := number_at(bytes, pe + 6, 2);
|
||
|
|
val optional := number_at(bytes, pe + 20, 2);
|
||
|
|
val strings := number_at(bytes, pe + 12, 4) + number_at(bytes, pe + 16, 4) * 18;
|
||
|
|
val table := pe + 24 + optional;
|
||
|
|
for i in 0..sections {
|
||
|
|
val at := table + 40 * i;
|
||
|
|
var named := true;
|
||
|
|
if bytes.at(at) == '/' as u8 {
|
||
|
|
# A long name: its offset into the table of strings, in decimal.
|
||
|
|
var offset := 0u;
|
||
|
|
for k in 1..8u {
|
||
|
|
break if at + k >= bytes.length || bytes.at(at + k) < '0' as u8 || bytes.at(at + k) > '9' as u8;
|
||
|
|
offset = offset * 10 + (bytes.at(at + k) - '0' as u8) as u64;
|
||
|
|
}
|
||
|
|
val held := strings + offset;
|
||
|
|
for k in 0..name.length {
|
||
|
|
if held + k >= bytes.length || bytes.at(held + k) != name[k] {
|
||
|
|
named = false;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
named = named && (held + name.length >= bytes.length || bytes.at(held + name.length) == 0);
|
||
|
|
} else {
|
||
|
|
for k in 0..8u {
|
||
|
|
val c: u8 = k < name.length ? name[k] : 0;
|
||
|
|
if bytes.at(at + k) != c {
|
||
|
|
named = false;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
continue if !named;
|
||
|
|
val size := number_at(bytes, at + 8, 4);
|
||
|
|
val offset := number_at(bytes, at + 20, 4);
|
||
|
|
val held := number_at(bytes, at + 16, 4);
|
||
|
|
return pe_found_section {
|
||
|
|
found = true,
|
||
|
|
address = number_at(bytes, at + 12, 4),
|
||
|
|
size = size,
|
||
|
|
flags = number_at(bytes, at + 36, 4),
|
||
|
|
bytes = bytes.substr(offset, held < size ? held : size)
|
||
|
|
};
|
||
|
|
}
|
||
|
|
return pe_found_section { bytes = strview::blank() };
|
||
|
|
}
|
||
|
|
|
||
|
|
when os == windows {
|
||
|
|
# Debugging information is held in sections of the executable the system loads
|
||
|
|
# but only debuggers read, and leaves everything the executable loads as it was.
|
||
|
|
@test def debugging_information_is_written_within_the_executable(t: &mut tester): void {
|
||
|
|
val standard := test_library();
|
||
|
|
return if standard.length == 0;
|
||
|
|
val library := standard.span;
|
||
|
|
val directory := scratch_directory("described");
|
||
|
|
val plain := $"{directory}/plain.exe";
|
||
|
|
val debug := $"{directory}/debug.exe";
|
||
|
|
val plain_problems := built(library, "test0.sakura", debugged, plain.span, 0, false);
|
||
|
|
val debug_problems := built(library, "test0.sakura", debugged, debug.span, 0, true);
|
||
|
|
for &each in plain_problems {
|
||
|
|
println(each);
|
||
|
|
}
|
||
|
|
for &each in debug_problems {
|
||
|
|
println(each);
|
||
|
|
}
|
||
|
|
return if !t.expects(plain_problems.length == 0 && debug_problems.length == 0);
|
||
|
|
t.expects(!file_exists($"{debug}.petal".span));
|
||
|
|
val ran: strview[1] = [debug.span];
|
||
|
|
t.expects(output_of(ran[:]).span == "30\n");
|
||
|
|
|
||
|
|
val plain_file := file_bytes(plain.span);
|
||
|
|
val debug_file := file_bytes(debug.span);
|
||
|
|
val loaded: strview[4] = [".rodata", ".data", ".text", ".lines"];
|
||
|
|
for &name in loaded {
|
||
|
|
val a := pe_section_named(plain_file.span, name);
|
||
|
|
val b := pe_section_named(debug_file.span, name);
|
||
|
|
t.expects(a.found && b.found && a.address == b.address && a.size == b.size && a.flags == b.flags && a.bytes == b.bytes);
|
||
|
|
}
|
||
|
|
|
||
|
|
# The sections of debugging information are only in a build asked for, and the
|
||
|
|
# system discards them when it loads the executable.
|
||
|
|
val debugging: strview[5] = [".debug_info", ".debug_abbrev", ".debug_line", ".debug_frame", ".debug_str"];
|
||
|
|
for &name in debugging {
|
||
|
|
val held := pe_section_named(debug_file.span, name);
|
||
|
|
t.expects(held.found && held.size > 0 && (held.flags & 0x02000000) != 0);
|
||
|
|
t.expects(!pe_section_named(plain_file.span, name).found);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# A word of a PE file at an address past its base, found by the section
|
||
|
|
# holding it, or 0 where none does.
|
||
|
|
def pe_word_at(bytes: strview, address: u64, size: u64): u64 {
|
||
|
|
val pe := number_at(bytes, 0x3C, 4);
|
||
|
|
val table := pe + 24 + number_at(bytes, pe + 20, 2);
|
||
|
|
for k in 0..number_at(bytes, pe + 6, 2) {
|
||
|
|
val at := table + 40 * k;
|
||
|
|
val begins := number_at(bytes, at + 12, 4);
|
||
|
|
if address >= begins && address < begins + number_at(bytes, at + 8, 4) {
|
||
|
|
return number_at(bytes, number_at(bytes, at + 20, 4) + address - begins, size);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A build for Windows asking for debugging information writes a PDB file beside
|
||
|
|
# the executable, which names the file by its path, its GUID, and its age, as
|
||
|
|
# the file names itself. Every build for Windows says how each function
|
||
|
|
# unwinds. A build on another system makes them too, though it can't run them.
|
||
|
|
@test def a_build_for_windows_names_its_pdb_file(t: &mut tester): void {
|
||
|
|
val standard := test_library();
|
||
|
|
return if standard.length == 0;
|
||
|
|
val library := standard.span;
|
||
|
|
val directory := scratch_directory("pdb");
|
||
|
|
val plain := $"{directory}/plain.exe";
|
||
|
|
val debug := $"{directory}/debug.exe";
|
||
|
|
val pdb := $"{directory}/debug.pdb";
|
||
|
|
remove_file(pdb.span);
|
||
|
|
remove_file($"{directory}/plain.pdb".span);
|
||
|
|
val plain_problems := built(library, "test0.sakura", debugged, plain.span, 0, false, 1, false, false, true);
|
||
|
|
val debug_problems := built(library, "test0.sakura", debugged, debug.span, 0, true, 1, false, false, true);
|
||
|
|
for &each in plain_problems {
|
||
|
|
println(each);
|
||
|
|
}
|
||
|
|
for &each in debug_problems {
|
||
|
|
println(each);
|
||
|
|
}
|
||
|
|
return if !t.expects(plain_problems.length == 0 && debug_problems.length == 0);
|
||
|
|
t.expects(file_exists(pdb.span));
|
||
|
|
t.expects(!file_exists($"{directory}/plain.pdb".span));
|
||
|
|
|
||
|
|
# Each says how its functions unwind, in a table the exception directory names.
|
||
|
|
val plain_file := file_bytes(plain.span);
|
||
|
|
val debug_file := file_bytes(debug.span);
|
||
|
|
val built_files: strview[2] = [plain_file.span, debug_file.span];
|
||
|
|
for &each in built_files {
|
||
|
|
val pe := number_at(each, 0x3C, 4);
|
||
|
|
t.expects(number_at(each, pe + 24 + 112 + 8 * 3, 4) != 0 && number_at(each, pe + 24 + 116 + 8 * 3, 4) % 12 == 0);
|
||
|
|
t.expects(pe_section_named(each, ".pdata").found && pe_section_named(each, ".xdata").found);
|
||
|
|
}
|
||
|
|
t.expects(!pe_section_named(plain_file.span, ".buildid").found);
|
||
|
|
|
||
|
|
# The debug directory's CodeView entry names the file: `RSDS`, its GUID, its
|
||
|
|
# age, and its path.
|
||
|
|
val bytes := debug_file.span;
|
||
|
|
val pe := number_at(bytes, 0x3C, 4);
|
||
|
|
val directory_at := number_at(bytes, pe + 24 + 112 + 8 * 6, 4);
|
||
|
|
t.expects(directory_at != 0 && number_at(bytes, pe + 24 + 116 + 8 * 6, 4) == 28);
|
||
|
|
t.expects(pe_word_at(bytes, directory_at + 12, 4) == 2);
|
||
|
|
val record := pe_word_at(bytes, directory_at + 24, 4) as u64;
|
||
|
|
t.expects(bytes.substr(record, 4) == "RSDS");
|
||
|
|
val path_at := record + 24;
|
||
|
|
t.expects(bytes.substr(path_at, pdb.length) == pdb.span);
|
||
|
|
|
||
|
|
# The PDB file names itself the same way, in the stream naming it: the first
|
||
|
|
# block of the second stream, past the directory's sizes.
|
||
|
|
val held := file_bytes(pdb.span);
|
||
|
|
val file := held.span;
|
||
|
|
t.expects(file.substr(0, 24) == "Microsoft C/C++ MSF 7.00");
|
||
|
|
val block := number_at(file, 32, 4);
|
||
|
|
val directory_block := number_at(file, number_at(file, 52, 4) * block, 4);
|
||
|
|
val streams := number_at(file, directory_block * block, 4);
|
||
|
|
val info := number_at(file, directory_block * block + 4 + 4 * streams, 4) * block;
|
||
|
|
t.expects(number_at(file, info, 4) == 20000404);
|
||
|
|
t.expects(number_at(file, info + 8, 4) == number_at(bytes, record + 20, 4));
|
||
|
|
t.expects(file.substr(info + 12, 16) == bytes.substr(record + 4, 16));
|
||
|
|
}
|
||
|
|
|
||
|
|
# A build for Windows given an icon, a title, and a version holds them as its
|
||
|
|
# resources, which Explorer and the file's properties read, and one asked to be
|
||
|
|
# windowed runs in windows of its own, as it runs with its output piped; one
|
||
|
|
# given none holds none, and runs in a console.
|
||
|
|
@test def a_build_for_windows_holds_its_icon_and_runs_windowed(t: &mut tester): void {
|
||
|
|
val standard := test_library();
|
||
|
|
return if standard.length == 0;
|
||
|
|
val library := standard.span;
|
||
|
|
val directory := scratch_directory("icon");
|
||
|
|
val dressed := $"{directory}/dressed.exe";
|
||
|
|
val plain := $"{directory}/plain.exe";
|
||
|
|
# A PNG's header is all an icon reads of it, 32 pixels each way.
|
||
|
|
val png: strview = "\x89PNG\x0D\x0A\x1A\x0A\x00\x00\x00\x0DIHDR\x00\x00\x00\x20\x00\x00\x00\x20\x08\x06\x00\x00\x00";
|
||
|
|
val icons: strview[1] = [png];
|
||
|
|
val look := build_options { icons = icons[:], title = "Dressed", semv = "1.2.3", windowed = true };
|
||
|
|
val dressed_problems := built(library, "test0.sakura", debugged, dressed.span, 2, false, 1, false, false, true, false, look);
|
||
|
|
val plain_problems := built(library, "test0.sakura", debugged, plain.span, 2, false, 1, false, false, true);
|
||
|
|
for &each in dressed_problems {
|
||
|
|
println(each);
|
||
|
|
}
|
||
|
|
return if !t.expects(dressed_problems.length == 0 && plain_problems.length == 0);
|
||
|
|
|
||
|
|
val held := file_bytes(dressed.span);
|
||
|
|
val bytes := held.span;
|
||
|
|
val header := number_at(bytes, 0x3C, 4);
|
||
|
|
t.expects(number_at(bytes, header + 24 + 68, 2) == 2);
|
||
|
|
t.expects(pe_section_named(bytes, ".rsrc").found && number_at(bytes, header + 24 + 112 + 8 * 2, 4) != 0);
|
||
|
|
if pe::find_resource(bytes, pe::resource_icon_group, 1) is some(group) {
|
||
|
|
# One image, 32 pixels wide, the icon numbered 1.
|
||
|
|
t.expects(group.length == 20 && group[4] == 1 && group[6] == 32 && group[18] == 1);
|
||
|
|
} else {
|
||
|
|
t.expects(false, "no icon group");
|
||
|
|
}
|
||
|
|
if pe::find_resource(bytes, pe::resource_icon, 1) is some(first) {
|
||
|
|
t.expects(first.length == png.length);
|
||
|
|
} else {
|
||
|
|
t.expects(false, "no icon");
|
||
|
|
}
|
||
|
|
t.expects(pe::find_resource(bytes, pe::resource_version, 1) is some);
|
||
|
|
|
||
|
|
val plain_held := file_bytes(plain.span);
|
||
|
|
val plain_bytes := plain_held.span;
|
||
|
|
t.expects(number_at(plain_bytes, number_at(plain_bytes, 0x3C, 4) + 24 + 68, 2) == 3);
|
||
|
|
t.expects(!pe_section_named(plain_bytes, ".rsrc").found && pe::find_resource(plain_bytes, pe::resource_icon_group, 1) is none);
|
||
|
|
|
||
|
|
# Windowed, it runs as it did, its output piped.
|
||
|
|
if test_windows {
|
||
|
|
val words: strview[1] = [dressed.span];
|
||
|
|
t.expects(output_of(words[:]).span.find("30") is some);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What gdb writes debugging an executable with commands.
|
||
|
|
#! @param executable The executable.
|
||
|
|
#! @param commands The commands, each run in turn.
|
||
|
|
#! @returns What it wrote.
|
||
|
|
def debugged_by_gdb(executable: strview, commands: span<strview>): string {
|
||
|
|
var words := vector::<strview>::new();
|
||
|
|
val start: strview[7] = ["gdb", "-batch", "-nx", "-iex", "set debuginfod enabled off", "-iex", "set auto-load no"];
|
||
|
|
for &each in start {
|
||
|
|
words.append(each);
|
||
|
|
}
|
||
|
|
for &each in commands {
|
||
|
|
words.append("-ex");
|
||
|
|
words.append(each);
|
||
|
|
}
|
||
|
|
words.append(executable);
|
||
|
|
return output_of(words.span);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether gdb wrote each piece of text, printing what it wrote where it did
|
||
|
|
#! not.
|
||
|
|
#! @param t The tester.
|
||
|
|
#! @param written What it wrote.
|
||
|
|
#! @param pieces The pieces.
|
||
|
|
def wrote_each(t: &mut tester, written: &string, pieces: span<strview>): void {
|
||
|
|
for &each in pieces {
|
||
|
|
if !t.expects(holds(written.span, each)) {
|
||
|
|
println($" gdb did not write `{each}`, and wrote:\n{written}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Where gdb is, it stops at lines, names the functions running and those
|
||
|
|
# inlined, shows the variables of code left as compiled, in callers too, and
|
||
|
|
# those of optimized code where they are known, and unwinds from a trap's
|
||
|
|
# report to where the program stopped.
|
||
|
|
@test def debuggers_read_the_debugging_information(t: &mut tester): void {
|
||
|
|
val standard := test_library();
|
||
|
|
return if standard.length == 0;
|
||
|
|
val library := standard.span;
|
||
|
|
val version: strview[2] = ["gdb", "--version"];
|
||
|
|
return if !holds(output_of(version[:]).span, "GNU gdb");
|
||
|
|
val directory := scratch_directory("gdb");
|
||
|
|
val executable := $"{directory}/debugged";
|
||
|
|
|
||
|
|
var problems := built(library, "test0.sakura", debugged, executable.span, 0, true);
|
||
|
|
return if !t.expects(problems.length == 0);
|
||
|
|
val unoptimized: strview[5] = ["break test0.sakura:6", "run", "bt", "info locals", "up"];
|
||
|
|
var commands := vector::<strview>::new();
|
||
|
|
for &each in unoptimized {
|
||
|
|
commands.append(each);
|
||
|
|
}
|
||
|
|
commands.append("info locals");
|
||
|
|
val stopped := debugged_by_gdb(executable.span, commands.span);
|
||
|
|
val shown: strview[5] = [
|
||
|
|
"scale (p=..., by=2) at test0.sakura:6", "main () at test0.sakura:11", "scaled = {x = 2, y = 4}", "i = 1",
|
||
|
|
"total = 0"
|
||
|
|
];
|
||
|
|
wrote_each(t, &stopped, shown[:]);
|
||
|
|
|
||
|
|
# Optimized, a call inlined is still a frame, and variables held in values
|
||
|
|
# are shown where they are known: in a register, the frame, or as a
|
||
|
|
# constant.
|
||
|
|
problems = built(library, "test0.sakura", debugged, executable.span, 2, true);
|
||
|
|
return if !t.expects(problems.length == 0);
|
||
|
|
val optimized: strview[3] = ["break scale", "run", "bt"];
|
||
|
|
val inlined := debugged_by_gdb(executable.span, optimized[:]);
|
||
|
|
val named: strview[2] = ["scale (by=2) at test0.sakura:5", "main () at test0.sakura:11"];
|
||
|
|
wrote_each(t, &inlined, named[:]);
|
||
|
|
val looping: strview[5] = ["break test0.sakura:12", "run", "continue", "continue", "info locals"];
|
||
|
|
val carried := debugged_by_gdb(executable.span, looping[:]);
|
||
|
|
val totals: strview[1] = ["total = 16"];
|
||
|
|
wrote_each(t, &carried, totals[:]);
|
||
|
|
|
||
|
|
# A trap is reported by `fail`, which unwinds to where the program stopped.
|
||
|
|
val trapping: strview = "\nimport std::io;\ndef half(n: int): int { return 10 / n; }\ndef main(): void { println($\"{half(0)}\"); }\n";
|
||
|
|
problems = built(library, "test0.sakura", trapping, executable.span, 2, true);
|
||
|
|
return if !t.expects(problems.length == 0);
|
||
|
|
val reporting: strview[3] = ["break fail", "run", "bt"];
|
||
|
|
val unwound := debugged_by_gdb(executable.span, reporting[:]);
|
||
|
|
val stack: strview[2] = ["half (n=0) at test0.sakura:3", "main () at test0.sakura:4"];
|
||
|
|
wrote_each(t, &unwound, stack[:]);
|
||
|
|
}
|
||
|
|
|
||
|
|
# Where valgrind is, it reads the executable's symbols, and the lines of the
|
||
|
|
# file of its debugging information, naming a source by the absolute path it
|
||
|
|
# has.
|
||
|
|
@test def valgrind_reads_the_debugging_information(t: &mut tester): void {
|
||
|
|
val standard := test_library();
|
||
|
|
return if standard.length == 0;
|
||
|
|
val library := standard.span;
|
||
|
|
val version: strview[2] = ["valgrind", "--version"];
|
||
|
|
return if !holds(output_of(version[:]).span, "valgrind");
|
||
|
|
val directory := scratch_directory("valgrind");
|
||
|
|
val source := $"{directory}/debugged.sakura";
|
||
|
|
val executable := $"{directory}/debugged";
|
||
|
|
val problems := built(library, source.span, debugged, executable.span, 2, true);
|
||
|
|
return if !t.expects(problems.length == 0);
|
||
|
|
|
||
|
|
val profile := $"{directory}/debugged.callgrind";
|
||
|
|
val out := $"--callgrind-out-file={profile}";
|
||
|
|
val words: strview[5] = ["valgrind", "--tool=callgrind", "--dump-line=yes", out.span, executable.span];
|
||
|
|
_ = output_of(words[:]);
|
||
|
|
val written := file_bytes(profile.span);
|
||
|
|
remove_file(profile.span);
|
||
|
|
t.expects(holds(written.span, ") main\n"));
|
||
|
|
t.expects(holds(written.span, $") {source}\n".span));
|
||
|
|
}
|