Update stack initialization code to handle all possible overflows and... #43

Merged
td1223 merged 1 commits from stack-init-overflowfix into master 2024-11-15 13:38:36 +00:00
3 changed files with 18 additions and 7 deletions

View File

@@ -4,7 +4,7 @@ SRCDIR = ..
# To add a new test, put its name on the PROGS list # To add a new test, put its name on the PROGS list
# and then add a name_SRC line that lists its source files. # and then add a name_SRC line that lists its source files.
PROGS = cat cmp cp echo halt hex-dump mcat mcp rm \ PROGS = cat cmp cp echo halt hex-dump mcat mcp rm \
bubsort insult lineup matmult recursor bubsort insult lineup matmult recursor args-ovf
# Should work from task 2 onward. # Should work from task 2 onward.
cat_SRC = cat.c cat_SRC = cat.c
@@ -18,6 +18,7 @@ lineup_SRC = lineup.c
ls_SRC = ls.c ls_SRC = ls.c
recursor_SRC = recursor.c recursor_SRC = recursor.c
rm_SRC = rm.c rm_SRC = rm.c
args-ovf_SRC = args-ovf.c
# Should work in task 3; also in task 4 if VM is included. # Should work in task 3; also in task 4 if VM is included.
bubsort_SRC = bubsort.c bubsort_SRC = bubsort.c

BIN
src/examples/args-ovf Executable file

Binary file not shown.

View File

@@ -28,6 +28,10 @@
(for the purposes of alignment). */ (for the purposes of alignment). */
#define WORD_SIZE 4 #define WORD_SIZE 4
/* Defines non-negative integer division wherein the result is always rounded
up. */
#define DIV_CEIL(x, y) ((x + (y - 1)) / y)
/* Keeps track of the position of pointers to user program arguments /* Keeps track of the position of pointers to user program arguments
within a linked list. */ within a linked list. */
struct arg_elem struct arg_elem
@@ -222,13 +226,19 @@ process_init_stack (char *cmd_saveptr, void **esp, char *file_name)
+ return_addr_size; + return_addr_size;
/* If pushing the rest of the data required for the stack would cause /* If pushing the rest of the data required for the stack would cause
overflow, allocate an extra page that is contiguous within the overflow, allocate as many extra pages as needed to the user process
virtual address space (below the current address range). */ contiguously in the virtual address space below the initial page. */
if (PHYS_BASE - *esp + remaining_size > PGSIZE) int overflow_bytes = (PHYS_BASE - *esp) + remaining_size - PGSIZE;
if (overflow_bytes > 0)
{ {
uint8_t *kpage = palloc_get_page (PAL_USER | PAL_ZERO); int pages_needed = DIV_CEIL (overflow_bytes, PGSIZE);
if (!install_page (((uint8_t *) PHYS_BASE) - PGSIZE * 2, kpage, true)) for (int i = 1; i < pages_needed + 1; i++)
return false; {
uint8_t *kpage = palloc_get_page (PAL_USER | PAL_ZERO);
if (!install_page (((uint8_t *) PHYS_BASE) - PGSIZE * (i + 1),
kpage, true))
return false;
}
} }
/* Align stack pointer to word size before pushing argv elements for /* Align stack pointer to word size before pushing argv elements for