我正在做 Pintos 项目,以了解有关操作系统的更多信息。我完成了项目 1 并开始了第二个项目。我已经验证了设置堆栈并且可以正常工作(通过 hex_dump)。现在我在获取正确的系统调用参数时遇到问题。
在 user/syscall.c 中有 4 个程序集存根(0 - 4 个存根)由用户系统调用包装。
#define syscall3(NUMBER, ARG0, ARG1, ARG2) \
({ \
int retval; \
asm volatile \
("pushl %[arg2]; pushl %[arg1]; pushl %[arg0]; " \
"pushl %[number]; int $0x30; addl $16, %%esp" \
: "=a" (retval) \
: [number] "i" (NUMBER), \
[arg0] "g" (ARG0), \
[arg1] "g" (ARG1), \
[arg2] "g" (ARG2) \
: "memory"); \
retval; \
}) (this code is given to us)
我的 syscall_handler 中有一些代码在内核中调用正确的函数。
static void syscall_handler (struct intr_frame *f) {
uint32_t *args = f->esp;
if (args[0] == SYS_WRITE) {
f->eax = write(args);
}
在我的 write 函数中,我打印出 FD 和 Size
int sysCallNumber = (int) args[0];
int fd = (int) args[1];
const char *buffer = (char *) args[2];
unsigned size = (unsigned) args[3];
printf("FD is %d\n", fd);
printf("Size is %d\n", size);
运行“echo hello stack overflow 1 22 333”将产生以下结果。注意我在括号中添加了注释。() <- 有些事情搞砸了,FD 被大小覆盖(包括空终止符)
FD is 6 (hello)
Size is 6
FD is 6 (stack)
Size is 6
FD is 9 (overflow)
Size is 9
FD is 2 (1)
Size is 2
FD is 3 (22)
Size is 3
FD is 4 (333)
Size is 4
FD is 1 (this is from the printf("\n") in echo.c)
Size is 1
我已经用 GDB 设置断点和转储帧来运行它,但无法弄清楚。有没有人遇到过类似的事情?如果是这样,您是如何解决的?
谢谢!