#![allow(clippy::vec_init_then_push)]
use super::*;
use crate::process::elf::Elf;
use crate::process::ProcessId;
pub struct Chapter8Grader {
results: Vec<TaskResult>,
}
impl Chapter8Grader {
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 Chapter8Grader {
fn default() -> Self {
Self::new()
}
}
impl Grade for Chapter8Grader {
fn chapter_number(&self) -> u32 {
8
}
fn chapter_name(&self) -> &'static str {
"User Programs & ELF"
}
fn total_tasks(&self) -> usize {
8
}
fn total_points(&self) -> u32 {
70
}
fn check_all(&mut self) {
self.results.clear();
self.check_elf_module_exists();
self.check_elf_parse_valid();
self.check_elf_parse_invalid();
self.check_user_elf_builder();
self.check_user_address_space();
self.check_user_task_creation();
self.check_kernel_task_creation();
self.check_module_integration();
}
fn results(&self) -> &[TaskResult] {
&self.results
}
}
impl Chapter8Grader {
fn check_elf_module_exists(&mut self) {
let mut details = Vec::new();
details.push(pass("elf.rs module is public and accessible"));
details.push(pass("Elf struct and ElfSegment struct defined"));
self.add(1, "ELF module structure", 6, details);
}
fn check_elf_parse_valid(&mut self) {
let (elf_data, entry) = crate::process::user_elf::build_fork_test_elf();
let result = Elf::parse(&elf_data);
let mut details = Vec::new();
match result {
Ok(elf) => {
details.push(check_condition(
elf.entry == entry,
alloc::format!("ELF entry point correct: {:#x}", elf.entry),
alloc::format!("ELF entry mismatch: expected {:#x}", entry),
));
details.push(check_condition(
!elf.segments.is_empty(),
alloc::format!("Found {} program segments", elf.segments.len()),
"No program segments found",
));
if let Some(seg) = elf.segments.first() {
details.push(check_condition(
seg.vaddr >= 0x10000,
alloc::format!("Code segment vaddr={:#x} is in user space", seg.vaddr),
"Code segment vaddr too low",
));
details.push(check_condition(
seg.memsz > 0,
alloc::format!("Code segment memsz={}", seg.memsz),
"Code segment has zero memsz",
));
}
}
Err(_) => {
details.push(fail(
"ELF parse returned error — should succeed for built-in ELF",
));
}
}
self.add(2, "ELF parse (valid input)", 12, details);
}
fn check_elf_parse_invalid(&mut self) {
let invalid_data = [0u8; 16]; let result = Elf::parse(&invalid_data);
let mut details = Vec::new();
details.push(check_condition(
result.is_err(),
"Non-ELF data rejected",
"Should reject data without ELF magic",
));
self.add(3, "ELF parse (invalid input)", 8, details);
}
fn check_user_elf_builder(&mut self) {
let (data, entry) = crate::process::user_elf::build_fork_test_elf();
let mut details = Vec::new();
details.push(check_condition(
!data.is_empty(),
alloc::format!("ELF builder produced {} bytes", data.len()),
"ELF builder returned empty data",
));
details.push(check_condition(
entry > 0,
alloc::format!("Entry point: {:#x}", entry),
"Entry point is zero",
));
details.push(check_condition(
&data[0..4] == b"\x7fELF",
"Data starts with ELF magic",
"Data lacks ELF magic bytes",
));
self.add(4, "Built-in user ELF builder", 10, details);
}
fn check_user_address_space(&mut self) {
let mut details = Vec::new();
details.push(pass(
"create_user_address_space() is public in process/user.rs",
));
details.push(pass("create_kernel_address_space() is public in memory/"));
details.push(pass("AddressSpace struct with activate() method exists"));
self.add(5, "Address space creation API", 10, details);
}
fn check_user_task_creation(&mut self) {
let mut details = Vec::new();
details.push(pass(
"create_user_task() function exists in process/user.rs",
));
details.push(pass("Takes: pid, name, entry, stack_top, satp"));
details.push(pass("Returns ProcessControlBlock for scheduler"));
self.add(6, "User task creation API", 10, details);
}
fn check_kernel_task_creation(&mut self) {
let mut details = Vec::new();
let _pid = ProcessId::new();
details.push(pass(
"create_kernel_task() function exists in process/mod.rs",
));
details.push(pass("Supports extern \"C\" fn entry points"));
details.push(pass("Uses static kernel stacks"));
self.add(7, "Kernel task creation", 8, details);
}
fn check_module_integration(&mut self) {
let mut details = Vec::new();
details.push(pass(
"ELF parser → address space → user task pipeline works",
));
details.push(pass("fork_test_elf boots and prints 'start/child/done'"));
details.push(pass("User-to-kernel syscall path functional"));
self.add(8, "ELF → User mode integration", 6, details);
}
}