Files
sakurac/tests/core/signals.sakura
T
2026-10-28 22:13:49 -05:00

542 lines
21 KiB
Plaintext

# Tests of signals: sending them, ignoring them, putting back what the system does
# with them, handling them, at the program's safe points and where a wait is
# interrupted, and watching for them. Each test runs in a copy of the program, so what one handles is its
# own. Windows sends a program no signal from another, so there a thread, or a
# copy of the program, sends what another program does on Linux.
import std::io;
import std::process;
import system;
import std::testing;
import std::thread;
# A function of the program's own, which begins with a safe point.
def safe_point(): void {}
when os == linux {
# Runs a copy of the program that puts back what the system does with a signal,
# then sends it to itself, giving the signal that stopped the copy, or zero where
# the copy went on and exited.
def raised_in_copy(sent: system::signal): i32 {
val copy := system::fork();
if copy == 0 {
_ = system::restore(sent);
_ = system::raise(sent);
safe_point();
system::exit(0);
}
var status: system::process_status;
_ = system::wait(copy, &mut status);
return status.exited ? 0 : status.signal;
}
# The signals the system says the program does something with, one bit each by
# number less one: those it handles, where `named` is `SigCgt`, or ignores, where
# it is `SigIgn`.
def dispositions(named: strview): u64 {
var at := path::parse("/proc/self/status") else (_) { return 0; };
val text := io::read_file(&at) else (_) { return 0; };
for line in text.view().split('\n') {
if !line.starts_with(named) || line.length < 16 {
continue;
}
val hex := line.substr(line.length - 16, 16);
var bits: u64 = 0;
for i in 0..hex.length {
val digit := hex[i] >= 'a' ? (hex[i] - 'a') as u64 + 10 : (hex[i] - '0') as u64;
bits = bits * 16 + digit;
}
return bits;
}
return 0;
}
# Waits until a process is in a state, as `/proc` gives it: `S` where it sleeps,
# as it does waiting in the system, so a signal sent to it then interrupts the
# wait, and `T` where it is paused. Gives whether it was.
def reaches(process: i64, state: u8): bool {
var at := path::parse($"/proc/{process}/stat") else (_) { return false; };
for tries in 0..1000000 {
val text := io::read_file(&at) else (_) { return false; };
val view := text.view();
var end := view.length;
while end > 0 && view[end - 1] != ')' {
end--;
}
if end + 1 < view.length && view[end + 1] == state {
return true;
}
}
return false;
}
# Starts a copy of the program that sends it a signal once it sleeps, then waits
# to be stopped, giving the copy's number.
def signalled_once_asleep(sent: system::signal): i64 {
val program := system::getpid();
val copy := system::fork();
if copy == 0 {
_ = system::restore(sent);
if reaches(program, 'S') {
_ = system::kill(program, sent);
}
system::pause();
system::exit(0);
}
return copy;
}
}
# Whether a read failed because a signal ended its wait.
def is_interrupted(error: maybe<io_error>): bool {
if error is some(why) {
return why == io_error::interrupted;
}
return false;
}
# A stream that gives its text in two parts, failing between them as a read does
# that a signal the program handles interrupts.
struct halting : readable {
var first: strview;
var rest: strview;
var halted: bool;
}
def halting::read(&mut this, into: slice<u8>): result<u64, io_error> {
if this.first.length > 0 {
return this.first.read(into);
}
if !this.halted {
this.halted = true;
return io_error::interrupted;
}
return this.rest.read(into);
}
when os == linux {
@test def signals_have_the_numbers_of_the_system(t: &mut tester): void {
# Those a program may handle are noted by the system under their numbers, and
# each handler is given the signal that came. Each is restored after, so that
# `child` interrupts no wait below.
val members: system::signal[12] = [
system::signal::hangup, system::signal::interrupt, system::signal::quit, system::signal::user1,
system::signal::user2, system::signal::pipe, system::signal::alarm, system::signal::terminate,
system::signal::child, system::signal::resume, system::signal::suspend, system::signal::resized
];
val numbers: u64[12] = [1, 2, 3, 10, 12, 13, 14, 15, 17, 18, 20, 28];
var came := system::signal::kill;
for i in 0..members.length {
t.expects(system::handle(members[i], def(s: system::signal) { came = s; }) == 0);
t.expects(((dispositions("SigCgt") >> (numbers[i] - 1)) & 1) == 1);
t.expects(system::raise(members[i]) == 0);
safe_point();
t.expects(came == members[i]);
t.expects(system::restore(members[i]) == 0);
t.expects(((dispositions("SigCgt") >> (numbers[i] - 1)) & 1) == 0);
}
# What stops a program by default stops a copy with its number.
t.expects(raised_in_copy(system::signal::hangup) == 1);
t.expects(raised_in_copy(system::signal::interrupt) == 2);
t.expects(raised_in_copy(system::signal::kill) == 9);
t.expects(raised_in_copy(system::signal::user1) == 10);
t.expects(raised_in_copy(system::signal::user2) == 12);
t.expects(raised_in_copy(system::signal::pipe) == 13);
t.expects(raised_in_copy(system::signal::alarm) == 14);
t.expects(raised_in_copy(system::signal::terminate) == 15);
# `stop` pauses a copy, and `resume` lets it go on.
val copy := system::fork();
if copy == 0 {
_ = system::raise(system::signal::stop);
system::exit(5);
}
t.expects(reaches(copy, 'T'));
_ = system::kill(copy, system::signal::resume);
var status: system::process_status;
_ = system::wait(copy, &mut status);
t.expects(status.exited && status.code == 5);
}
@test def ignored_signals_do_nothing_until_restored(t: &mut tester): void {
t.expects(system::ignore(system::signal::user1) == 0);
t.expects(((dispositions("SigIgn") >> 9) & 1) == 1);
t.expects(system::raise(system::signal::user1) == 0);
safe_point();
t.expects(system::restore(system::signal::user1) == 0);
t.expects(((dispositions("SigIgn") >> 9) & 1) == 0);
t.expects(raised_in_copy(system::signal::user1) == 10);
# Restoring a handled signal stops its handler running.
var count := 0;
t.expects(system::handle(system::signal::user2, def(s: system::signal) { count++; }) == 0);
t.expects(system::ignore(system::signal::user2) == 0);
t.expects(system::raise(system::signal::user2) == 0);
safe_point();
t.expects(count == 0 && ((dispositions("SigCgt") >> 11) & 1) == 0);
}
}
@test def kill_and_stop_cannot_be_handled_nor_child_ignored(t: &mut tester): void {
val nothing_done := def(s: system::signal) {};
t.expects(system::error_of(system::handle(system::signal::kill, nothing_done)) == io_error::invalid);
t.expects(system::error_of(system::handle(system::signal::stop, nothing_done)) == io_error::invalid);
t.expects(system::error_of(system::ignore(system::signal::kill)) == io_error::invalid);
t.expects(system::error_of(system::ignore(system::signal::stop)) == io_error::invalid);
t.expects(system::error_of(system::ignore(system::signal::child)) == io_error::invalid);
}
when os == linux {
@test def signals_watched_for_wait_to_be_asked_for(t: &mut tester): void {
t.expects(system::watch(system::signal::user1) == 0);
t.expects(((dispositions("SigCgt") >> 9) & 1) == 1);
t.expects(system::watched() == 0);
_ = system::raise(system::signal::user1);
safe_point();
t.expects(system::watched() == 1u << 10);
t.expects(system::watched() == 0);
# A handled signal is not given, and one watched for waits while the handlers
# run.
var count := 0;
t.expects(system::handle(system::signal::user2, def(s: system::signal) { count++; }) == 0);
_ = system::raise(system::signal::user1);
_ = system::raise(system::signal::user2);
safe_point();
t.expects(count == 1);
t.expects(system::watched() == 1u << 10);
# Handling a signal watched for stops it being watched for.
t.expects(system::handle(system::signal::user1, def(s: system::signal) { count++; }) == 0);
_ = system::raise(system::signal::user1);
safe_point();
t.expects(count == 2 && system::watched() == 0);
t.expects(system::error_of(system::watch(system::signal::kill)) == io_error::invalid);
t.expects(system::error_of(system::watch(system::signal::stop)) == io_error::invalid);
}
}
@test def handlers_run_at_the_next_safe_point(t: &mut tester): void {
var count := 0;
t.expects(system::handle(system::signal::user1, def(s: system::signal) { count++; }) == 0);
_ = system::raise(system::signal::user1);
t.expects(count == 0);
safe_point();
t.expects(count == 1);
# Signals that come together are handled once each.
_ = system::raise(system::signal::user1);
_ = system::raise(system::signal::user1);
safe_point();
t.expects(count == 2);
# A loop of the program's own runs the handler as it goes round.
var stopping := false;
t.expects(system::handle(system::signal::user2, def(s: system::signal) { stopping = true; }) == 0);
_ = system::raise(system::signal::user2);
var passes := 0;
while !stopping {
passes++;
}
t.expects(passes == 1);
}
when os == linux {
@test def a_loop_calling_nothing_is_ended_by_its_safe_points(t: &mut tester): void {
var flag := false;
t.expects(system::handle(system::signal::user1, def(s: system::signal) { flag = true; }) == 0);
val program := system::getpid();
val copy := system::fork();
if copy == 0 {
_ = system::kill(program, system::signal::user1);
system::exit(0);
}
while !flag {}
var status: system::process_status;
_ = system::wait(copy, &mut status);
t.expects(flag && status.exited);
}
}
@test def vectors_held_across_a_safe_point_keep_their_lanes(t: &mut tester): void {
# The handler works vectors of its own, in the registers the loop holds its
# sums in, which the safe point keeps whole for it: 16 bytes, and 32 where the
# loop's code has AVX.
var ran := 0;
t.expects(system::handle(system::signal::user1, def(s: system::signal) {
var noise := f32x8::splat(-1.0);
var bytes := u32x4::splat(7);
for _ in 0..4 {
noise = noise * noise + f32x8::splat(3.0);
bytes = bytes * bytes;
}
if noise.sum() > 0.0 && bytes.sum() != 0 {
ran++;
}
}) == 0);
# Raised before the loop, which calls nothing, so the handler runs at its
# first pass's safe point, the sums in registers.
_ = system::raise(system::signal::user1);
var narrow := u32x4::splat(0);
var wide := f32x8::splat(0.0);
for i in 0..100u {
narrow = narrow + u32x4::splat(i as u32);
wide = wide + f32x8::splat(1.0);
}
t.expects(ran == 1 && narrow.sum() == 4 * 4950 && wide.sum() == 800.0);
}
@test def handlers_do_not_nest(t: &mut tester): void {
var depth := 0;
var deepest := 0;
var runs := 0;
t.expects(system::handle(system::signal::user1, def(s: system::signal) {
depth++;
runs++;
deepest = depth > deepest ? depth : deepest;
if runs == 1 {
_ = system::raise(system::signal::user1);
safe_point();
}
depth--;
}) == 0);
_ = system::raise(system::signal::user1);
safe_point();
t.expects(runs == 2 && deepest == 1);
}
when os == linux {
@test def a_wait_a_handler_interrupts_fails_with_interrupted(t: &mut tester): void {
var count := 0;
t.expects(system::handle(system::signal::user1, def(s: system::signal) { count++; }) == 0);
val copy := signalled_once_asleep(system::signal::user1);
# The handler has run by the time the wait ends.
var status: system::process_status;
val waited := system::wait(copy, &mut status);
t.expects(system::error_of(waited) == io_error::interrupted && count == 1);
_ = system::kill(copy, system::signal::kill);
t.expects(system::wait(copy, &mut status) == copy && !status.exited && status.signal == 9);
}
@test def pause_returns_once_a_handler_has_run(t: &mut tester): void {
var count := 0;
t.expects(system::handle(system::signal::user1, def(s: system::signal) { count++; }) == 0);
# A signal sent just before is not missed.
_ = system::raise(system::signal::user1);
system::pause();
t.expects(count == 1);
# One sent while it waits ends the wait.
val copy := signalled_once_asleep(system::signal::user1);
system::pause();
t.expects(count == 2);
var status: system::process_status;
_ = system::kill(copy, system::signal::kill);
_ = system::wait(copy, &mut status);
}
@test def exit_in_a_handler_stops_the_program(t: &mut tester): void {
val copy := system::fork();
if copy == 0 {
_ = system::handle(system::signal::user1, def(s: system::signal) { system::exit(7); });
_ = system::raise(system::signal::user1);
safe_point();
system::exit(0);
}
var status: system::process_status;
_ = system::wait(copy, &mut status);
t.expects(status.exited && status.code == 7);
}
}
@test def a_reader_keeps_what_a_signal_cut_short(t: &mut tester): void {
# A line cut short is given whole by the next read.
var lines := io::reader_of(halting { first = "hel", rest = "lo\nworld" });
t.expects(lines.read_line() is none && is_interrupted(lines.error));
val line := lines.read_line() else { t.expects(false); return; };
t.expects(line.view() == "hello" && lines.error is none);
val last := lines.read_line() else { t.expects(false); return; };
t.expects(last.view() == "world");
# So are bytes asked for, and the rest of the input.
var bytes := io::reader_of(halting { first = "0123", rest = "456789" });
t.expects(bytes.read_exactly(6) is error);
val six := bytes.read_exactly(6) else (_) { t.expects(false); return; };
t.expects(six.view() == "012345");
var rest := io::reader_of(halting { first = "ab", rest = "cd" });
t.expects(rest.read_all() is error);
val all := rest.read_all() else (_) { t.expects(false); return; };
t.expects(all.view() == "abcd");
}
when os == windows {
# Runs a copy of the program that puts back what the system does with a signal,
# then sends it to itself, giving the signal that stopped the copy, or zero where
# the copy went on and exited. Windows stops a program by a signal's default with
# 128 more than its number, or by `interrupt` as the console's Ctrl-C does, which
# is the signal itself.
def raised_in_copy(sent: system::signal): i32 {
val status := copied(def() {
_ = system::restore(sent);
_ = system::raise(sent);
safe_point();
});
if !status.exited {
return status.signal;
}
return status.code > 128 && status.code < 192 ? status.code - 128 : 0;
}
# Starts a thread that sends the program a signal once a little time has passed.
# The thread is given the signal, which it owns, since it outlives this call.
def sent_soon(sent: system::signal): result<thread::handle<void>, io_error> {
return thread::spawn_with(def(which: system::signal) {
_ = system::sleep(50000000);
_ = system::raise(which);
}, sent);
}
@test def signals_have_the_numbers_of_the_system(t: &mut tester): void {
# Those a program may handle are noted under their numbers, and each handler
# is given the signal that came.
val members: system::signal[12] = [
system::signal::hangup, system::signal::interrupt, system::signal::quit, system::signal::user1,
system::signal::user2, system::signal::pipe, system::signal::alarm, system::signal::terminate,
system::signal::child, system::signal::resume, system::signal::suspend, system::signal::resized
];
val numbers: u64[12] = [1, 2, 3, 10, 12, 13, 14, 15, 17, 18, 20, 28];
var came := system::signal::kill;
for i in 0..members.length {
t.expects(system::handle(members[i], def(s: system::signal) { came = s; }) == 0);
t.expects(system::raise(members[i]) == 0);
safe_point();
t.expects(came == members[i]);
t.expects(system::watch(members[i]) == 0 && system::raise(members[i]) == 0);
t.expects(system::watched() == 1u << numbers[i]);
t.expects(system::restore(members[i]) == 0);
}
# What stops a program by default stops a copy with its number.
t.expects(raised_in_copy(system::signal::hangup) == 1);
t.expects(raised_in_copy(system::signal::interrupt) == 2);
t.expects(raised_in_copy(system::signal::kill) == 9);
t.expects(raised_in_copy(system::signal::user1) == 10);
t.expects(raised_in_copy(system::signal::user2) == 12);
t.expects(raised_in_copy(system::signal::pipe) == 13);
t.expects(raised_in_copy(system::signal::alarm) == 14);
t.expects(raised_in_copy(system::signal::terminate) == 15);
# Windows cannot pause a program.
t.expects(system::error_of(system::raise(system::signal::stop)) == io_error::unsupported);
}
@test def ignored_signals_do_nothing_until_restored(t: &mut tester): void {
t.expects(system::ignore(system::signal::user1) == 0);
t.expects(system::raise(system::signal::user1) == 0);
safe_point();
t.expects(system::restore(system::signal::user1) == 0);
t.expects(raised_in_copy(system::signal::user1) == 10);
# Restoring a handled signal stops its handler running.
var count := 0;
t.expects(system::handle(system::signal::user2, def(s: system::signal) { count++; }) == 0);
t.expects(system::ignore(system::signal::user2) == 0);
t.expects(system::raise(system::signal::user2) == 0);
safe_point();
t.expects(count == 0);
}
@test def signals_watched_for_wait_to_be_asked_for(t: &mut tester): void {
t.expects(system::watch(system::signal::user1) == 0);
t.expects(system::watched() == 0);
_ = system::raise(system::signal::user1);
safe_point();
t.expects(system::watched() == 1u << 10);
t.expects(system::watched() == 0);
# A handled signal is not given, and one watched for waits while the handlers
# run.
var count := 0;
t.expects(system::handle(system::signal::user2, def(s: system::signal) { count++; }) == 0);
_ = system::raise(system::signal::user1);
_ = system::raise(system::signal::user2);
safe_point();
t.expects(count == 1);
t.expects(system::watched() == 1u << 10);
# Handling a signal watched for stops it being watched for.
t.expects(system::handle(system::signal::user1, def(s: system::signal) { count++; }) == 0);
_ = system::raise(system::signal::user1);
safe_point();
t.expects(count == 2 && system::watched() == 0);
t.expects(system::error_of(system::watch(system::signal::kill)) == io_error::invalid);
t.expects(system::error_of(system::watch(system::signal::stop)) == io_error::invalid);
}
@test def a_loop_calling_nothing_is_ended_by_its_safe_points(t: &mut tester): void {
var flag := false;
t.expects(system::handle(system::signal::user1, def(s: system::signal) { flag = true; }) == 0);
var sender := sent_soon(system::signal::user1) else (_) { t.expects(false); return; };
while !flag {}
sender.close();
t.expects(flag);
}
@test def a_wait_a_handler_interrupts_fails_with_interrupted(t: &mut tester): void {
var count := 0;
t.expects(system::handle(system::signal::user1, def(s: system::signal) { count++; }) == 0);
var pinger := process::command::new("ping");
pinger.arguments(["-n", "30", "127.0.0.1"]);
pinger.output(process::stdio::none);
var running := pinger.spawn() else (_) { t.expects(false); return; };
var sender := sent_soon(system::signal::user1) else (_) { t.expects(false); return; };
# The handler has run by the time the wait ends.
var status: system::process_status;
val waited := system::wait(running.id as i64, &mut status);
t.expects(system::error_of(waited) == io_error::interrupted && count == 1);
sender.close();
t.expects(running.kill() is value);
val stopped := running.wait() else (_) { t.expects(false); return; };
t.expects(stopped == process::status { signaled = 9 });
}
@test def pause_returns_once_a_handler_has_run(t: &mut tester): void {
var count := 0;
t.expects(system::handle(system::signal::user1, def(s: system::signal) { count++; }) == 0);
# A signal sent just before is not missed.
_ = system::raise(system::signal::user1);
system::pause();
t.expects(count == 1);
# One sent while it waits ends the wait.
var sender := sent_soon(system::signal::user1) else (_) { t.expects(false); return; };
system::pause();
t.expects(count == 2);
sender.close();
}
@test def exit_in_a_handler_stops_the_program(t: &mut tester): void {
val status := copied(def() {
_ = system::handle(system::signal::user1, def(s: system::signal) { system::exit(7); });
_ = system::raise(system::signal::user1);
safe_point();
});
t.expects(status.exited && status.code == 7);
}
}