From aefa2697d705e316aa5255004e4b6a129e9afe2a Mon Sep 17 00:00:00 2001
From: Robert Morris <rtm@csail.mit.edu>
Date: Wed, 19 Aug 2020 12:35:14 -0400
Subject: usertest for exec() out of memory recovery and fix a few exec() bugs

---
 user/usertests.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

(limited to 'user/usertests.c')

diff --git a/user/usertests.c b/user/usertests.c
index cc88555..acdba0f 100644
--- a/user/usertests.c
+++ b/user/usertests.c
@@ -2452,6 +2452,42 @@ badarg(char *s)
   exit(0);
 }
 
+// test the exec() code that cleans up if it runs out
+// of memory. it's really a test that such a condition
+// doesn't cause a panic.
+void
+execout(char *s)
+{
+  for(int avail = 0; avail < 15; avail++){
+    int pid = fork();
+    if(pid < 0){
+      printf("fork failed\n");
+      exit(1);
+    } else if(pid == 0){
+      // allocate all of memory.
+      while(1){
+        uint64 a = (uint64) sbrk(4096);
+        if(a == 0xffffffffffffffffLL)
+          break;
+      }
+
+      // free a few pages, in order to let exec() make some
+      // progress.
+      for(int i = 0; i < avail; i++)
+        sbrk(-4096);
+      
+      close(1);
+      char *args[] = { "echo", "x", 0 };
+      exec("echo", args);
+      exit(0);
+    } else {
+      wait((int*)0);
+    }
+  }
+
+  exit(0);
+}
+
 //
 // use sbrk() to count how many free physical memory pages there are.
 //
@@ -2520,6 +2556,7 @@ main(int argc, char *argv[])
     void (*f)(char *);
     char *s;
   } tests[] = {
+    {execout, "execout"},
     {copyin, "copyin"},
     {copyout, "copyout"},
     {copyinstr1, "copyinstr1"},
-- 
cgit v1.2.3