#![allow(clippy::vec_init_then_push)]
use super::*;
use crate::syscall::{SyscallContext, SyscallId};
pub struct Chapter5Grader {
results: Vec<TaskResult>,
}
impl Chapter5Grader {
pub fn new() -> Self {
Self {
results: Vec::new(),
}
}
fn add(&mut self, task_id: usize, name: &'static str, points: u32, details: Vec<GradeDetail>) {
self.results.push(TaskResult {
task_id,
name,
points,
details,
});
}
}
impl Default for Chapter5Grader {
fn default() -> Self {
Self::new()
}
}
impl Grade for Chapter5Grader {
fn chapter_number(&self) -> u32 {
5
}
fn chapter_name(&self) -> &'static str {
"System Calls"
}
fn total_tasks(&self) -> usize {
10
}
fn total_points(&self) -> u32 {
70
}
fn check_all(&mut self) {
self.results.clear();
self.check_syscall_ids();
self.check_context();
self.check_dispatcher();
self.check_sys_read();
self.check_sys_write();
self.check_sys_open();
self.check_sys_close();
self.check_sys_mkdir();
self.check_extended_syscalls();
self.check_error_handling();
}
fn results(&self) -> &[TaskResult] {
&self.results
}
}
impl Chapter5Grader {
fn check_syscall_ids(&mut self) {
let mut details = Vec::new();
details.push(check_condition(
SyscallId::from(64) == SyscallId::Write,
"Write = 64 correct",
"Write syscall number mismatch",
));
details.push(check_condition(
SyscallId::from(63) == SyscallId::Read,
"Read = 63 correct",
"Read syscall number mismatch",
));
details.push(check_condition(
SyscallId::from(56) == SyscallId::Open,
"Open = 56 correct",
"Open syscall number mismatch",
));
details.push(check_condition(
SyscallId::from(57) == SyscallId::Close,
"Close = 57 correct",
"Close syscall number mismatch",
));
details.push(check_condition(
SyscallId::from(34) == SyscallId::Mkdir,
"Mkdir = 34 correct",
"Mkdir syscall number mismatch",
));
details.push(check_condition(
SyscallId::from(93) == SyscallId::Exit,
"Exit = 93 correct",
"Exit syscall number mismatch",
));
self.add(1, "SyscallId enum definitions", 8, details);
}
fn check_context(&mut self) {
let ctx = SyscallContext {
syscall_id: 64,
arg0: 1,
arg1: 0x10000,
arg2: 13,
arg3: 0,
arg4: 0,
arg5: 0,
sepc: 0x80200000,
};
let mut details = Vec::new();
details.push(check_condition(
ctx.syscall_id == 64,
"syscall_id stored correctly",
"syscall_id mismatch",
));
details.push(check_condition(
ctx.arg0 == 1 && ctx.arg1 == 0x10000 && ctx.arg2 == 13,
"Arguments stored correctly",
"Argument storage mismatch",
));
details.push(check_condition(
ctx.sepc == 0x80200000,
"sepc stored correctly",
"sepc mismatch",
));
self.add(2, "SyscallContext creation", 6, details);
}
fn check_dispatcher(&mut self) {
let mut details = Vec::new();
let ctx = SyscallContext {
syscall_id: 172,
arg0: 0,
arg1: 0,
arg2: 0,
arg3: 0,
arg4: 0,
arg5: 0,
sepc: 0,
};
let ret = crate::syscall::syscall_dispatcher(&ctx);
details.push(check_condition(
ret == 1, "Dispatcher routes GetPid correctly (returns 1)",
"GetPid dispatch failed",
));
let ctx2 = SyscallContext {
syscall_id: 9999,
arg0: 0,
arg1: 0,
arg2: 0,
arg3: 0,
arg4: 0,
arg5: 0,
sepc: 0,
};
let ret2 = crate::syscall::syscall_dispatcher(&ctx2);
details.push(check_condition(
ret2 == -1,
"Dispatcher returns -1 for unknown syscall",
"Unknown syscall handling failed",
));
self.add(3, "Syscall dispatcher routing", 8, details);
}
fn check_sys_read(&mut self) {
let mut details = Vec::new();
let ret = crate::syscall::syscall_impl::sys_read(0, 0, 64);
details.push(check_condition(
ret == -1,
"sys_read rejects null buffer",
"sys_read should reject null buffer",
));
self.add(4, "sys_read argument validation", 6, details);
}
fn check_sys_write(&mut self) {
let data = b"GRADER";
let ret = crate::syscall::syscall_impl::sys_write(1, data.as_ptr(), data.len());
let mut details = Vec::new();
details.push(check_condition(
ret == data.len() as isize,
"sys_write to stdout returns byte count",
"sys_write stdout failed",
));
details.push(check_condition(
crate::syscall::syscall_impl::sys_write(1, core::ptr::null(), 10) == -1,
"sys_write rejects null buffer",
"sys_write null buffer should fail",
));
self.add(5, "sys_write stdout + validation", 8, details);
}
fn check_sys_open(&mut self) {
use crate::syscall::syscall_impl::open_flags;
let path = "/mnt/__grader_test__\0";
let fd = crate::syscall::syscall_impl::sys_open(
path.as_ptr() as usize,
open_flags::O_RDWR | open_flags::O_CREAT,
);
let mut details = Vec::new();
details.push(check_condition(
fd >= 0,
"sys_open with O_CREAT succeeds",
"sys_open O_CREAT failed",
));
if fd >= 0 {
crate::syscall::syscall_impl::sys_close(fd as usize);
}
self.add(6, "sys_open file creation", 8, details);
}
fn check_sys_close(&mut self) {
let mut details = Vec::new();
let ret = crate::syscall::syscall_impl::sys_close(999);
details.push(check_condition(
ret == -1,
"sys_close rejects invalid fd",
"sys_close should fail on invalid fd",
));
self.add(7, "sys_close validation", 6, details);
}
fn check_sys_mkdir(&mut self) {
let dir = "/mnt/__grader_dir__\0";
let ret = crate::syscall::syscall_impl::sys_mkdir(dir.as_ptr() as usize);
let mut details = Vec::new();
details.push(check_condition(
ret == 0,
"sys_mkdir creates directory",
"sys_mkdir failed",
));
self.add(8, "sys_mkdir directory creation", 6, details);
}
fn check_extended_syscalls(&mut self) {
let mut details = Vec::new();
details.push(check_condition(
crate::syscall::syscall_impl::sys_lseek(999, 0, 0) == -1,
"lseek rejects invalid fd",
"lseek should fail on bad fd",
));
details.push(check_condition(
crate::syscall::syscall_impl::sys_stat(0, 0) == -1,
"stat rejects null path",
"stat should fail on null path",
));
details.push(check_condition(
crate::syscall::syscall_impl::sys_dup2(999, 10) == -1,
"dup2 rejects invalid old_fd",
"dup2 should fail on bad old_fd",
));
details.push(check_condition(
crate::syscall::syscall_impl::sys_pipe(0) == -1,
"pipe rejects null pointer",
"pipe should fail on null ptr",
));
self.add(9, "Extended syscalls error handling", 8, details);
}
fn check_error_handling(&mut self) {
let mut details = Vec::new();
let mut buf = [0u8; 16];
let ret = crate::syscall::syscall_impl::sys_read(999, buf.as_mut_ptr() as usize, 16);
details.push(check_condition(
ret == -1,
"sys_read rejects invalid fd",
"sys_read should fail on bad fd",
));
self.add(10, "Error handling consistency", 6, details);
}
}