fixed memory leak on error path in copy_in_string
[pintos-anon] / solutions / p2.patch
index 10723ebc9f78d5f526e6e4dbb7b0bd347bdd84b0..0940c141ee2d9b97c4c898cf3e8c2c57d1110041 100644 (file)
@@ -1,6 +1,7 @@
+Index: src/threads/thread.c
 diff -u src/threads/thread.c~ src/threads/thread.c
---- src/threads/thread.c~ 2005-06-02 14:35:12.000000000 -0700
-+++ src/threads/thread.c 2005-06-08 13:45:28.000000000 -0700
+--- src/threads/thread.c~
++++ src/threads/thread.c
 @@ -13,6 +13,7 @@
  #include "threads/synch.h"
  #ifdef USERPROG
@@ -31,21 +32,21 @@ diff -u src/threads/thread.c~ src/threads/thread.c
    schedule ();
    NOT_REACHED ();
  }
-@@ -400,6 +404,11 @@ init_thread (struct thread *t, const cha
+@@ -400,6 +404,10 @@ init_thread (struct thread *t, const cha
    strlcpy (t->name, name, sizeof t->name);
    t->stack = (uint8_t *) t + PGSIZE;
    t->priority = priority;
 +  list_init (&t->children);
-+  t->exit_code = -1;
 +  t->wait_status = NULL;
 +  list_init (&t->fds);
 +  t->next_handle = 2;
    t->magic = THREAD_MAGIC;
  }
  
+Index: src/threads/thread.h
 diff -u src/threads/thread.h~ src/threads/thread.h
---- src/threads/thread.h~ 2005-06-02 14:32:36.000000000 -0700
-+++ src/threads/thread.h 2005-06-08 13:47:09.000000000 -0700
+--- src/threads/thread.h~
++++ src/threads/thread.h
 @@ -4,6 +4,7 @@
  #include <debug.h>
  #include <list.h>
@@ -54,12 +55,11 @@ diff -u src/threads/thread.h~ src/threads/thread.h
  
  /* States in a thread's life cycle. */
  enum thread_status
-@@ -89,6 +90,11 @@ struct thread
+@@ -89,6 +90,10 @@ struct thread
      uint8_t *stack;                     /* Saved stack pointer. */
      int priority;                       /* Priority. */
  
 +    /* Owned by process.c. */
-+    int exit_code;                      /* Exit code. */
 +    struct wait_status *wait_status;    /* This process's completion status. */
 +    struct list children;               /* Completion status of children. */
 +
@@ -95,12 +95,13 @@ diff -u src/threads/thread.h~ src/threads/thread.h
 +    struct semaphore dead;              /* 1=child alive, 0=child dead. */
 +  };
 +
- void thread_init (void);
- void thread_start (void);
- void thread_tick (void);
+ /* If false (default), use round-robin scheduler.
+    If true, use multi-level feedback queue scheduler.
+    Controlled by kernel command-line options "-o mlfqs".
+Index: src/userprog/exception.c
 diff -u src/userprog/exception.c~ src/userprog/exception.c
---- src/userprog/exception.c~ 2005-01-01 18:09:59.000000000 -0800
-+++ src/userprog/exception.c 2005-06-08 13:45:28.000000000 -0700
+--- src/userprog/exception.c~
++++ src/userprog/exception.c
 @@ -150,6 +150,14 @@ page_fault (struct intr_frame *f) 
    write = (f->error_code & PF_W) != 0;
    user = (f->error_code & PF_U) != 0;
@@ -116,9 +117,10 @@ diff -u src/userprog/exception.c~ src/userprog/exception.c
    /* To implement virtual memory, delete the rest of the function
       body, and replace it with code that brings in the page to
       which fault_addr refers. */
+Index: src/userprog/process.c
 diff -u src/userprog/process.c~ src/userprog/process.c
---- src/userprog/process.c~ 2005-05-26 13:19:48.000000000 -0700
-+++ src/userprog/process.c 2005-06-08 13:49:13.000000000 -0700
+--- src/userprog/process.c~
++++ src/userprog/process.c
 @@ -14,11 +14,23 @@
  #include "threads/init.h"
  #include "threads/interrupt.h"
@@ -194,7 +196,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
    struct intr_frame if_;
    bool success;
  
-@@ -58,10 +78,28 @@ execute_thread (void *file_name_)
+@@ -58,10 +78,29 @@ execute_thread (void *file_name_)
    if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
    if_.cs = SEL_UCSEG;
    if_.eflags = FLAG_IF | FLAG_MBS;
@@ -217,6 +219,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
 +      lock_init (&exec->wait_status->lock);
 +      exec->wait_status->ref_cnt = 2;
 +      exec->wait_status->tid = thread_current ()->tid;
++      exec->wait_status->exit_code = -1;
 +      sema_init (&exec->wait_status->dead, 0);
 +    }
 +  
@@ -279,15 +282,13 @@ diff -u src/userprog/process.c~ src/userprog/process.c
    return -1;
  }
  
-@@ -95,8 +162,32 @@ void
+@@ -95,8 +162,30 @@ void
  process_exit (void)
  {
    struct thread *cur = thread_current ();
 +  struct list_elem *e, *next;
    uint32_t *pd;
  
-+  printf ("%s: exit(%d)\n", cur->name, cur->exit_code);
-+
 +  /* Close executable (and allow writes). */
 +  file_close (cur->bin_file);
 +
@@ -295,7 +296,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
 +  if (cur->wait_status != NULL) 
 +    {
 +      struct wait_status *cs = cur->wait_status;
-+      cs->exit_code = cur->exit_code;
++      printf ("%s: exit(%d)\n", cur->name, cs->exit_code);
 +      sema_up (&cs->dead);
 +      release_child (cs);
 +    }
@@ -488,17 +489,18 @@ diff -u src/userprog/process.c~ src/userprog/process.c
        else
          palloc_free_page (kpage);
      }
+Index: src/userprog/syscall.c
 diff -u src/userprog/syscall.c~ src/userprog/syscall.c
---- src/userprog/syscall.c~ 2004-09-26 14:15:17.000000000 -0700
-+++ src/userprog/syscall.c 2005-06-08 13:45:28.000000000 -0700
-@@ -1,20 +1,482 @@
+--- src/userprog/syscall.c~
++++ src/userprog/syscall.c
+@@ -1,20 +1,486 @@
  #include "userprog/syscall.h"
  #include <stdio.h>
 +#include <string.h>
  #include <syscall-nr.h>
 +#include "userprog/process.h"
 +#include "userprog/pagedir.h"
-+#include "devices/kbd.h"
++#include "devices/input.h"
 +#include "filesys/filesys.h"
 +#include "filesys/file.h"
 +#include "threads/init.h"
@@ -528,6 +530,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 -
 +static void copy_in (void *, const void *, size_t);
 + 
++/* Serializes file system operations. */
 +static struct lock fs_lock;
 + 
  void
@@ -654,7 +657,10 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +  for (length = 0; length < PGSIZE; length++)
 +    {
 +      if (us >= (char *) PHYS_BASE || !get_user (ks + length, us++)) 
-+        thread_exit (); 
++        {
++          palloc_free_page (ks);
++          thread_exit (); 
++        }
 +       
 +      if (ks[length] == '\0')
 +        return ks;
@@ -674,7 +680,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +static int
 +sys_exit (int exit_code) 
 +{
-+  thread_current ()->exit_code = exit_code;
++  thread_current ()->wait_status->exit_code = exit_code;
 +  thread_exit ();
 +  NOT_REACHED ();
 +}
@@ -818,7 +824,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +  if (handle == STDIN_FILENO) 
 +    {
 +      for (bytes_read = 0; (size_t) bytes_read < size; bytes_read++)
-+        if (udst >= (uint8_t *) PHYS_BASE || !put_user (udst++, kbd_getc ()))
++        if (udst >= (uint8_t *) PHYS_BASE || !put_user (udst++, input_getc ()))
 +          thread_exit ();
 +      return bytes_read;
 +    }
@@ -978,9 +984,10 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +      free (fd);
 +    }
 +}
+Index: src/userprog/syscall.h
 diff -u src/userprog/syscall.h~ src/userprog/syscall.h
---- src/userprog/syscall.h~ 2004-09-05 22:38:45.000000000 -0700
-+++ src/userprog/syscall.h 2005-06-08 13:45:28.000000000 -0700
+--- src/userprog/syscall.h~
++++ src/userprog/syscall.h
 @@ -2,5 +2,6 @@
  #define USERPROG_SYSCALL_H