Remove the old and in with the new

Added the new testing to the OS files

Some spelling
INOUT => IN/OUT
Added some doc comments to log

Added the new runtime to the build + added the None test mode

Moved some stuff around
None test mode is the default to run/build the OS normally with no runtime tests.

Add the new runtime testing to the CI


Updated README and CI


Increased timeout


Print the log message


Spelling


Move runtime to test folder


Add new RT to tty


Add a log to use the unmapped memory to cause page fault in release


Ensure the integer overflow happens even in release builds
This commit is contained in:
DrDeano 2020-06-23 12:43:52 +01:00
parent d6d99ef667
commit 2c91e6f9d0
No known key found for this signature in database
GPG key ID: 96188600582B9ED7
28 changed files with 667 additions and 405 deletions

View file

@ -1,44 +0,0 @@
def get_test_cases(TestCase):
return [
TestCase("GDT init", [r"Init gdt"]),
TestCase("GDT tests", [r"GDT: Tested loading GDT"]),
TestCase("GDT done", [r"Done gdt"]),
TestCase("IDT init", [r"Init idt"]),
TestCase("IDT tests", [r"IDT: Tested loading IDT"]),
TestCase("IDT done", [r"Done idt"]),
TestCase("PIC init", [r"Init pic"]),
TestCase("PIC tests", [r"PIC: Tested masking"]),
TestCase("PIC done", [r"Done pic"]),
TestCase("ISR init", [r"Init isr"]),
TestCase("ISR tests", [r"ISR: Tested registered handlers", r"ISR: Tested opened IDT entries"]),
TestCase("ISR done", [r"Done isr"]),
TestCase("IRQ init", [r"Init irq"]),
TestCase("IRQ tests", [r"IRQ: Tested registered handlers", r"IRQ: Tested opened IDT entries"]),
TestCase("IRQ done", [r"Done irq"]),
TestCase("Paging init", [r"Init paging"]),
TestCase("Paging tests", [r"Paging: Tested accessing unmapped memory", r"Paging: Tested accessing mapped memory"]),
TestCase("Paging done", [r"Done paging"]),
TestCase("PIT init", [r"Init pit"]),
TestCase("PIT init", [r".+"], r"\[DEBUG\] "),
TestCase("PIT tests", [r"PIT: Tested init", r"PIT: Tested wait ticks", r"PIT: Tested wait ticks 2"]),
TestCase("PIT done", [r"Done pit"]),
TestCase("RTC init", [r"Init rtc"]),
TestCase("RTC tests", [r"RTC: Tested init", r"RTC: Tested interrupts"]),
TestCase("RTC done", [r"Done rtc"]),
TestCase("Syscalls init", [r"Init syscalls"]),
TestCase("Syscalls tests", [r"Syscalls: Tested no args", r"Syscalls: Tested 1 arg", r"Syscalls: Tested 2 args", r"Syscalls: Tested 3 args", r"Syscalls: Tested 4 args", r"Syscalls: Tested 5 args"]),
TestCase("Syscalls done", [r"Done syscalls"]),
TestCase("VGA init", [r"Init vga"]),
TestCase("VGA tests", [r"VGA: Tested max scan line", r"VGA: Tested cursor shape", r"VGA: Tested updating cursor"]),
TestCase("VGA done", [r"Done vga"]),
TestCase("TTY tests", [r"TTY: Tested globals", r"TTY: Tested printing"]),
]

View file

@ -639,7 +639,7 @@ pub fn freeTest() void {
/// IN/OUT self: *Self - Self. This is the mocking object to be modified to add
/// the test parameters.
/// IN fun_name: []const u8 - The function name to add the test parameters to.
/// IN params: arglist - The parameters to add.
/// IN params: var - The parameters to add.
///
pub fn addTestParams(comptime fun_name: []const u8, params: var) void {
var mock_obj = getMockObject();

View file

@ -1,118 +0,0 @@
import atexit
import queue
import threading
import subprocess
import signal
import re
import sys
import datetime
import os
import importlib.util
msg_queue = queue.Queue(-1)
proc = None
class TestCase:
def __init__(self, name, expected, prefix=r"\[INFO\] "):
self.name = name
self.expected = expected
self.prefix = prefix
def failure(msg):
print("FAILURE: %s" %(msg))
sys.exit(1)
def test_failure(case, exp, expected_idx, found):
failure("%s #%d, expected '%s', found '%s'" %(case.name, expected_idx + 1, exp, found))
def test_pass(case, exp, expected_idx, found):
print("PASS: %s #%d, expected '%s', found '%s'" %(case.name, expected_idx + 1, exp, found))
def get_pre_archinit_cases():
return [
TestCase("Serial tests", [r"c", r"123"], ""),
TestCase("Log info tests", [r"Test INFO level", r"Test INFO level with args a, 1", r"Test INFO function", r"Test INFO function with args a, 1"], r"\[INFO\] "),
TestCase("Log debug tests", [r"Test DEBUG level", r"Test DEBUG level with args a, 1", r"Test DEBUG function", r"Test DEBUG function with args a, 1"], r"\[DEBUG\] "),
TestCase("Log warning tests", [r"Test WARNING level", r"Test WARNING level with args a, 1", r"Test WARNING function", r"Test WARNING function with args a, 1"], r"\[WARNING\] "),
TestCase("Log error tests", [r"Test ERROR level", r"Test ERROR level with args a, 1", r"Test ERROR function", r"Test ERROR function with args a, 1"], r"\[ERROR\] "),
TestCase("Mem init", [r"Init mem"]),
TestCase("Mem done", [r"Done mem"]),
TestCase("Panic init", [r"Init panic"]),
TestCase("Panic done", [r"Done panic"]),
TestCase("PMM init", [r"Init pmm"]),
TestCase("PMM tests", [r"PMM: Tested allocation"]),
TestCase("PMM done", [r"Done pmm"]),
TestCase("VMM init", [r"Init vmm"]),
TestCase("VMM tests", [r"VMM: Tested allocations"]),
TestCase("VMM done", [r"Done vmm"]),
TestCase("Arch init starts", [r"Init arch \w+"])
]
def get_post_archinit_cases():
return [
TestCase("Arch init finishes", [r"Arch init done"]),
TestCase("Heap", [r"Init heap", r"Done heap"]),
TestCase("TTY init", [r"Init tty"]),
TestCase("TTY done", [r"Done tty"]),
TestCase("Init finishes", [r"Init done"]),
TestCase("Panic tests", [r"Kernel panic: integer overflow", r"c[a-z\d]+: panic", r"c[a-z\d]+: panic.runtimeTests", r"c[a-z\d]+: kmain", r"c[a-z\d]+: start_higher_half"], r"\[ERROR\] ")
]
def read_messages(proc):
while True:
line = proc.stdout.readline().decode("utf-8")
msg_queue.put(line)
def cleanup():
global proc
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
if __name__ == "__main__":
arch = sys.argv[1]
zig_path = sys.argv[2]
spec = importlib.util.spec_from_file_location("arch", "test/kernel/arch/" + arch + "/rt-test.py")
arch_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(arch_module)
# The list of log statements to look for before arch init is called +
# All log statements to look for, including the arch-specific ones +
# The list of log statements to look for after arch init is called
cases = get_pre_archinit_cases() + arch_module.get_test_cases(TestCase) + get_post_archinit_cases()
if len(cases) > 0:
proc = subprocess.Popen(zig_path + " build run -Drt-test=true -Darch=" + arch, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
atexit.register(cleanup)
case_idx = 0
read_thread = threading.Thread(target=read_messages, args=(proc,))
read_thread.daemon = True
read_thread.start()
# Go through the cases
while case_idx < len(cases):
case = cases[case_idx]
expected_idx = 0
# Go through the expected log messages
while expected_idx < len(case.expected):
e = case.prefix + case.expected[expected_idx]
try:
line = msg_queue.get(block=True, timeout=5)
except queue.Empty:
failure("Timed out waiting for '%s'" %(e))
line = line.strip()
pattern = re.compile(e)
# Pass if the line matches the expected pattern, else fail
if pattern.fullmatch(line):
test_pass(case, e, expected_idx, line)
else:
test_failure(case, e, expected_idx, line)
expected_idx += 1
case_idx += 1
sys.exit(0)

277
test/runtime_test.zig Normal file
View file

@ -0,0 +1,277 @@
const std = @import("std");
const ChildProcess = std.ChildProcess;
const Thread = std.Thread;
const Allocator = std.mem.Allocator;
const Builder = std.build.Builder;
const Step = std.build.Step;
const Queue = std.atomic.Queue([]const u8);
const Node = std.TailQueue([]const u8).Node;
// Creating a new runtime test:
// 1. Add a enum to `TestMode`. The name should try to describe the test in one word :P
// 2. Add a description for the new runtime test to explain to the use what this will test.
// 3. Create a function with in the RuntimeStep struct that will perform the test. At least this
// should use `self.get_msg()` which will get the serial log lines from the OS. Look at
// test_init or test_panic for examples.
// 4. In the create function, add your test mode and test function to the switch.
// 5. Celebrate if it works lel
/// The enumeration of tests with all the runtime tests.
pub const TestMode = enum {
/// This is for the default test mode. This will just run the OS normally.
None,
/// Run the OS's initialisation runtime tests to ensure the OS is properly set up.
Initialisation,
/// Run the panic runtime test.
Panic,
///
/// Return a string description for the test mode provided.
///
/// Argument:
/// IN mode: TestMode - The test mode.
///
/// Return: []const u8
/// The string description for the test mode.
///
pub fn getDescription(mode: TestMode) []const u8 {
return switch (mode) {
.None => "Runs the OS normally (Default)",
.Initialisation => "Initialisation runtime tests",
.Panic => "Panic runtime tests",
};
}
};
/// The runtime step for running the runtime tests for the OS.
pub const RuntimeStep = struct {
/// The Step, that is all you need to know
step: Step,
/// The builder pointer, also all you need to know
builder: *Builder,
/// The message queue that stores the log lines
msg_queue: Queue,
/// The qemu process, this is needed for the `read_logs` thread.
os_proc: *ChildProcess,
/// The argv of the qemu process so can create the qemu process
argv: [][]const u8,
/// The test function that will be run for the current runtime test.
test_func: TestFn,
/// The error set for the RuntimeStep
const Error = error{
/// The error for if a test fails. If the test function returns false, this will be thrown
/// at the wnd of the make function as we need to clean up first. This will ensure the
/// build fails.
TestFailed,
/// This is used for `self.get_msg()` when the queue is empty after a timeout.
QueueEmpty,
};
/// The type of the test function.
const TestFn = fn (self: *RuntimeStep) bool;
/// The time used for getting message from the message queue. This is in milliseconds.
const queue_timeout: usize = 5000;
///
/// This will just print all the serial logs.
///
/// Arguments:
/// IN/OUT self: *RuntimeStep - Self.
///
/// Return: bool
/// This will always return true
///
fn print_logs(self: *RuntimeStep) bool {
while (true) {
const msg = self.get_msg() catch return true;
defer self.builder.allocator.free(msg);
std.debug.warn("{}\n", .{msg});
}
}
///
/// This tests the OS is initialised correctly by checking that we get a `SUCCESS` at the end.
///
/// Arguments:
/// IN/OUT self: *RuntimeStep - Self.
///
/// Return: bool
/// Whether the test has passed or failed.
///
fn test_init(self: *RuntimeStep) bool {
while (true) {
const msg = self.get_msg() catch return false;
defer self.builder.allocator.free(msg);
// Print the line to see what is going on
std.debug.warn("{}\n", .{msg});
if (std.mem.startsWith(u8, msg, "[ERROR] FAILURE")) {
return false;
} else if (std.mem.eql(u8, msg, "[INFO] SUCCESS")) {
return true;
}
}
}
///
/// This tests the OS's panic by checking that we get a kernel panic for integer overflow.
///
/// Arguments:
/// IN/OUT self: *RuntimeStep - Self.
///
/// Return: bool
/// Whether the test has passed or failed.
///
fn test_panic(self: *RuntimeStep) bool {
while (true) {
const msg = self.get_msg() catch return false;
defer self.builder.allocator.free(msg);
// Print the line to see what is going on
std.debug.warn("{}\n", .{msg});
if (std.mem.eql(u8, msg, "[ERROR] Kernel panic: integer overflow")) {
return true;
}
}
}
///
/// The make function that is called by the builder. This will create the qemu process with the
/// stdout as a Pipe. Then create the read thread to read the logs from the qemu stdout. Then
/// will call the test function to test a specifics part of the OS defined by the test mode.
///
/// Arguments:
/// IN/OUT step: *Step - The step of this step.
///
/// Error: Thread.SpawnError || ChildProcess.SpawnError || Allocator.Error || Error
/// Thread.SpawnError - If there is an error spawning the real logs thread.
/// ChildProcess.SpawnError - If there is an error spawning the qemu process.
/// Allocator.Error.OutOfMemory - If there is no more memory to allocate.
/// Error.TestFailed - The error if the test failed.
///
fn make(step: *Step) (Thread.SpawnError || ChildProcess.SpawnError || Allocator.Error || Error)!void {
const self = @fieldParentPtr(RuntimeStep, "step", step);
// Create the qemu process
self.os_proc = try ChildProcess.init(self.argv, self.builder.allocator);
defer self.os_proc.deinit();
self.os_proc.stdout_behavior = .Pipe;
self.os_proc.stdin_behavior = .Inherit;
self.os_proc.stderr_behavior = .Inherit;
try self.os_proc.spawn();
// Start up the read thread
var thread = try Thread.spawn(self, read_logs);
// Call the testing function
const res = self.test_func(self);
// Now kill our baby
_ = try self.os_proc.kill();
// Join the thread
thread.wait();
// Free the rest of the queue
while (self.msg_queue.get()) |node| {
self.builder.allocator.free(node.data);
self.builder.allocator.destroy(node);
}
// If the test function returns false, then fail the build
if (!res) {
return Error.TestFailed;
}
}
///
/// This is to only be used in the read logs thread. This reads the stdout of the qemu process
/// and stores each line in the queue.
///
/// Arguments:
/// IN/OUT self: *RuntimeStep - Self.
///
fn read_logs(self: *RuntimeStep) void {
const stream = self.os_proc.stdout.?.reader();
// Line shouldn't be longer than this
const max_line_length: usize = 128;
while (true) {
const line = stream.readUntilDelimiterAlloc(self.builder.allocator, '\n', max_line_length) catch |e| switch (e) {
error.EndOfStream => {
// When the qemu process closes, this will return a EndOfStream, so can catch and return so then can
// join the thread to exit nicely :)
return;
},
else => unreachable,
};
// put line in the queue
var node = self.builder.allocator.create(Node) catch unreachable;
node.* = Node.init(line);
self.msg_queue.put(node);
}
}
///
/// This return a log message from the queue in the order it would appear in the qemu process.
/// The line will need to be free with allocator.free(line) then finished with the line.
///
/// Arguments:
/// IN/OUT self: *RuntimeStep - Self.
///
/// Return: []const u8
/// A log line from the queue.
///
/// Error: Error
/// error.QueueEmpty - If the queue is empty for more than the timeout, this will be thrown.
///
fn get_msg(self: *RuntimeStep) Error![]const u8 {
var i: usize = 0;
while (i < queue_timeout) : (i += 1) {
if (self.msg_queue.get()) |node| {
defer self.builder.allocator.destroy(node);
return node.data;
}
std.time.sleep(std.time.ns_per_ms);
}
return Error.QueueEmpty;
}
///
/// Create a runtime step with a specific test mode.
///
/// Argument:
/// IN builder: *Builder - The builder. This is used for the allocator.
/// IN test_mode: TestMode - The test mode.
/// IN qemu_args: [][]const u8 - The qemu arguments used to create the OS process.
///
/// Return: *RuntimeStep
/// The Runtime step pointer to add to the build process.
///
pub fn create(builder: *Builder, test_mode: TestMode, qemu_args: [][]const u8) *RuntimeStep {
const runtime_step = builder.allocator.create(RuntimeStep) catch unreachable;
runtime_step.* = RuntimeStep{
.step = Step.init(.Custom, builder.fmt("Runtime {}", .{@tagName(test_mode)}), builder.allocator, make),
.builder = builder,
.msg_queue = Queue.init(),
.os_proc = undefined,
.argv = qemu_args,
.test_func = switch (test_mode) {
.None => print_logs,
.Initialisation => test_init,
.Panic => test_panic,
},
};
return runtime_step;
}
};