1#![allow(clippy::vec_init_then_push)]
7
8use super::*;
9use crate::syscall::{SyscallContext, SyscallId};
10
11pub struct Chapter5Grader {
12 results: Vec<TaskResult>,
13}
14
15impl Chapter5Grader {
16 pub fn new() -> Self {
17 Self {
18 results: Vec::new(),
19 }
20 }
21
22 fn add(&mut self, task_id: usize, name: &'static str, points: u32, details: Vec<GradeDetail>) {
23 self.results.push(TaskResult {
24 task_id,
25 name,
26 points,
27 details,
28 });
29 }
30}
31
32impl Default for Chapter5Grader {
33 fn default() -> Self {
34 Self::new()
35 }
36}
37
38impl Grade for Chapter5Grader {
39 fn chapter_number(&self) -> u32 {
40 5
41 }
42 fn chapter_name(&self) -> &'static str {
43 "System Calls"
44 }
45 fn total_tasks(&self) -> usize {
46 10
47 }
48 fn total_points(&self) -> u32 {
49 70
50 }
51
52 fn check_all(&mut self) {
53 self.results.clear();
54 self.check_syscall_ids();
55 self.check_context();
56 self.check_dispatcher();
57 self.check_sys_read();
58 self.check_sys_write();
59 self.check_sys_open();
60 self.check_sys_close();
61 self.check_sys_mkdir();
62 self.check_extended_syscalls();
63 self.check_error_handling();
64 }
65
66 fn results(&self) -> &[TaskResult] {
67 &self.results
68 }
69}
70
71impl Chapter5Grader {
72 fn check_syscall_ids(&mut self) {
74 let mut details = Vec::new();
75
76 details.push(check_condition(
78 SyscallId::from(64) == SyscallId::Write,
79 "Write = 64 correct",
80 "Write syscall number mismatch",
81 ));
82 details.push(check_condition(
83 SyscallId::from(63) == SyscallId::Read,
84 "Read = 63 correct",
85 "Read syscall number mismatch",
86 ));
87 details.push(check_condition(
88 SyscallId::from(56) == SyscallId::Open,
89 "Open = 56 correct",
90 "Open syscall number mismatch",
91 ));
92 details.push(check_condition(
93 SyscallId::from(57) == SyscallId::Close,
94 "Close = 57 correct",
95 "Close syscall number mismatch",
96 ));
97 details.push(check_condition(
98 SyscallId::from(34) == SyscallId::Mkdir,
99 "Mkdir = 34 correct",
100 "Mkdir syscall number mismatch",
101 ));
102 details.push(check_condition(
103 SyscallId::from(93) == SyscallId::Exit,
104 "Exit = 93 correct",
105 "Exit syscall number mismatch",
106 ));
107
108 self.add(1, "SyscallId enum definitions", 8, details);
109 }
110
111 fn check_context(&mut self) {
113 let ctx = SyscallContext {
114 syscall_id: 64,
115 arg0: 1,
116 arg1: 0x10000,
117 arg2: 13,
118 arg3: 0,
119 arg4: 0,
120 arg5: 0,
121 sepc: 0x80200000,
122 };
123
124 let mut details = Vec::new();
125 details.push(check_condition(
126 ctx.syscall_id == 64,
127 "syscall_id stored correctly",
128 "syscall_id mismatch",
129 ));
130 details.push(check_condition(
131 ctx.arg0 == 1 && ctx.arg1 == 0x10000 && ctx.arg2 == 13,
132 "Arguments stored correctly",
133 "Argument storage mismatch",
134 ));
135 details.push(check_condition(
136 ctx.sepc == 0x80200000,
137 "sepc stored correctly",
138 "sepc mismatch",
139 ));
140
141 self.add(2, "SyscallContext creation", 6, details);
142 }
143
144 fn check_dispatcher(&mut self) {
146 let mut details = Vec::new();
147
148 let ctx = SyscallContext {
150 syscall_id: 172,
151 arg0: 0,
152 arg1: 0,
153 arg2: 0,
154 arg3: 0,
155 arg4: 0,
156 arg5: 0,
157 sepc: 0,
158 };
159 let ret = crate::syscall::syscall_dispatcher(&ctx);
160 details.push(check_condition(
161 ret == 1, "Dispatcher routes GetPid correctly (returns 1)",
163 "GetPid dispatch failed",
164 ));
165
166 let ctx2 = SyscallContext {
168 syscall_id: 9999,
169 arg0: 0,
170 arg1: 0,
171 arg2: 0,
172 arg3: 0,
173 arg4: 0,
174 arg5: 0,
175 sepc: 0,
176 };
177 let ret2 = crate::syscall::syscall_dispatcher(&ctx2);
178 details.push(check_condition(
179 ret2 == -1,
180 "Dispatcher returns -1 for unknown syscall",
181 "Unknown syscall handling failed",
182 ));
183
184 self.add(3, "Syscall dispatcher routing", 8, details);
185 }
186
187 fn check_sys_read(&mut self) {
189 let mut details = Vec::new();
190 let ret = crate::syscall::syscall_impl::sys_read(0, 0, 64);
192 details.push(check_condition(
193 ret == -1,
194 "sys_read rejects null buffer",
195 "sys_read should reject null buffer",
196 ));
197 self.add(4, "sys_read argument validation", 6, details);
199 }
200
201 fn check_sys_write(&mut self) {
203 let data = b"GRADER";
204 let ret = crate::syscall::syscall_impl::sys_write(1, data.as_ptr(), data.len());
205 let mut details = Vec::new();
206 details.push(check_condition(
207 ret == data.len() as isize,
208 "sys_write to stdout returns byte count",
209 "sys_write stdout failed",
210 ));
211 details.push(check_condition(
212 crate::syscall::syscall_impl::sys_write(1, core::ptr::null(), 10) == -1,
213 "sys_write rejects null buffer",
214 "sys_write null buffer should fail",
215 ));
216 self.add(5, "sys_write stdout + validation", 8, details);
217 }
218
219 fn check_sys_open(&mut self) {
221 use crate::syscall::syscall_impl::open_flags;
222 let path = "/mnt/__grader_test__\0";
223 let fd = crate::syscall::syscall_impl::sys_open(
224 path.as_ptr() as usize,
225 open_flags::O_RDWR | open_flags::O_CREAT,
226 );
227 let mut details = Vec::new();
228 details.push(check_condition(
229 fd >= 0,
230 "sys_open with O_CREAT succeeds",
231 "sys_open O_CREAT failed",
232 ));
233 if fd >= 0 {
234 crate::syscall::syscall_impl::sys_close(fd as usize);
235 }
236 self.add(6, "sys_open file creation", 8, details);
237 }
238
239 fn check_sys_close(&mut self) {
241 let mut details = Vec::new();
242 let ret = crate::syscall::syscall_impl::sys_close(999);
244 details.push(check_condition(
245 ret == -1,
246 "sys_close rejects invalid fd",
247 "sys_close should fail on invalid fd",
248 ));
249 self.add(7, "sys_close validation", 6, details);
250 }
251
252 fn check_sys_mkdir(&mut self) {
254 let dir = "/mnt/__grader_dir__\0";
255 let ret = crate::syscall::syscall_impl::sys_mkdir(dir.as_ptr() as usize);
256 let mut details = Vec::new();
257 details.push(check_condition(
258 ret == 0,
259 "sys_mkdir creates directory",
260 "sys_mkdir failed",
261 ));
262 self.add(8, "sys_mkdir directory creation", 6, details);
263 }
264
265 fn check_extended_syscalls(&mut self) {
267 let mut details = Vec::new();
268
269 details.push(check_condition(
271 crate::syscall::syscall_impl::sys_lseek(999, 0, 0) == -1,
272 "lseek rejects invalid fd",
273 "lseek should fail on bad fd",
274 ));
275
276 details.push(check_condition(
278 crate::syscall::syscall_impl::sys_stat(0, 0) == -1,
279 "stat rejects null path",
280 "stat should fail on null path",
281 ));
282
283 details.push(check_condition(
285 crate::syscall::syscall_impl::sys_dup2(999, 10) == -1,
286 "dup2 rejects invalid old_fd",
287 "dup2 should fail on bad old_fd",
288 ));
289
290 details.push(check_condition(
292 crate::syscall::syscall_impl::sys_pipe(0) == -1,
293 "pipe rejects null pointer",
294 "pipe should fail on null ptr",
295 ));
296
297 self.add(9, "Extended syscalls error handling", 8, details);
298 }
299
300 fn check_error_handling(&mut self) {
302 let mut details = Vec::new();
303 let mut buf = [0u8; 16];
305 let ret = crate::syscall::syscall_impl::sys_read(999, buf.as_mut_ptr() as usize, 16);
306 details.push(check_condition(
307 ret == -1,
308 "sys_read rejects invalid fd",
309 "sys_read should fail on bad fd",
310 ));
311 self.add(10, "Error handling consistency", 6, details);
312 }
313}