diff --git a/src/userprog/process.c b/src/userprog/process.c index 6d6fef7..adffb00 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -60,8 +60,7 @@ process_execute (const char *file_name) return tid; } -static void process_init_stack (void *file_name_, char *saveptr, void **esp, - char *file_name); +static bool process_init_stack (char *saveptr, void **esp, char *file_name); static void *push_to_stack (void **esp, void *data, size_t data_size); /* A thread function that loads a user process and starts it @@ -97,8 +96,17 @@ start_process (void *file_name_) thread_exit (); } - process_init_stack (file_name_, saveptr, &if_.esp, file_name); + /* Initialize user process stack and free page used to store the + command that executed the process. */ + success = process_init_stack (saveptr, &if_.esp, file_name); palloc_free_page (file_name_); + + /* If stack initialization failed, free resources and quit. */ + if (!success) + { + process_exit (); + thread_exit (); + } /* Start the user process by simulating a return from an interrupt, implemented by intr_exit (in @@ -111,10 +119,9 @@ start_process (void *file_name_) } /* Helper function that initializes the stack of a newly created - user process. */ -static void -process_init_stack (void *file_name_, char *saveptr, void **esp, - char *file_name) + user process. Returns true if successful, false otherwise. */ +static bool +process_init_stack (char *saveptr, void **esp, char *file_name) { /* Load command line argument *data* to user process stack. This can't cause overflow due to enforcing that the size of @@ -134,9 +141,7 @@ process_init_stack (void *file_name_, char *saveptr, void **esp, { printf("ERROR: Couldn't allocate argument pointer memory for %s!\n", thread_current ()->name); - palloc_free_page (file_name_); - process_exit (); - thread_exit (); + return false; } arg_elem->arg = *esp; @@ -196,11 +201,12 @@ process_init_stack (void *file_name_, char *saveptr, void **esp, /* Push fake return address (null pointer). */ *esp -= sizeof (char *); *(char *) *esp = 0; + + return true; } /* Helper function that pushes the first 'data_size' bytes stored in the - address '*data' into the stack given a pointer to the stack pointer - '**esp'. */ + address '*data' into the stack given a pointer to the stack pointer '**esp'. */ static void * push_to_stack (void **esp, void *data, size_t data_size) {