From b8d358ecb2472a9bc6ecdd00b1de5560dd8a5b2d Mon Sep 17 00:00:00 2001 From: Themis Demetriades Date: Mon, 11 Nov 2024 13:13:21 +0000 Subject: [PATCH] Update stack initialization to handle overflow by allocating a second page for argument pointers --- src/userprog/process.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/userprog/process.c b/src/userprog/process.c index c935a07..792fe76 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -88,6 +88,8 @@ process_execute (const char *cmd) return tid; } + +static bool install_page (void *upage, void *kpage, bool writable); static bool process_init_stack (char *cmd_saveptr, void **esp, char *file_name); static void *push_to_stack (void **esp, void *data, size_t data_size); #define push_var_to_stack(esp, var) (push_to_stack (esp, &var, sizeof (var))) @@ -186,10 +188,13 @@ process_init_stack (char *cmd_saveptr, void **esp, char *file_name) + return_addr_size; /* If pushing the rest of the data required for the stack would cause - overflow, allocate an extra page. */ + overflow, allocate an extra page that is contiguous within the + virtual address space (below the current address range). */ if (PHYS_BASE - *esp + remaining_size > PGSIZE) { - /* TODO: Allocate an extra page for the rest of the process stack. */ + uint8_t *kpage = palloc_get_page (PAL_USER | PAL_ZERO); + if (!install_page (((uint8_t *) PHYS_BASE) - PGSIZE * 2, kpage, true)) + return false; } /* Align stack pointer to word size before pushing argv elements for @@ -485,8 +490,6 @@ load (const char *file_name, void (**eip) (void), void **esp) /* load() helpers. */ -static bool install_page (void *upage, void *kpage, bool writable); - /* Checks whether PHDR describes a valid, loadable segment in FILE and returns true if so, false otherwise. */ static bool