syscall: Zero out memory that already exists during the brk system call.
authorTimothy M. Jones <tjones1@inf.ed.ac.uk>
Sat, 24 Oct 2009 17:53:58 +0000 (10:53 -0700)
committerTimothy M. Jones <tjones1@inf.ed.ac.uk>
Sat, 24 Oct 2009 17:53:58 +0000 (10:53 -0700)
Glibc often assumes that memory it receives from the kernel after a brk
system call will contain only zeros. This is important during a calloc,
because it won't clear the new memory itself. In the simulator, if the
new page exists, it will be cleared using this patch, to mimic the kernel's
functionality.

src/sim/syscall_emul.cc

index 9a797bcdb0e0fd4ceef3cc5a0e8a3ac2a441080f..13ad043111c53ea5cd8533de331d757b0c1e4db9 100644 (file)
@@ -144,6 +144,24 @@ brkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
             if (!p->pTable->translate(gen.addr()))
                 p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
                                     VMPageSize);
+
+            // if the address is already there, zero it out
+            else {
+                uint8_t zero  = 0;
+                TranslatingPort *tp = tc->getMemPort();
+
+                // split non-page aligned accesses
+                Addr next_page = roundUp(gen.addr(), VMPageSize);
+                uint32_t size_needed = next_page - gen.addr();
+                tp->memsetBlob(gen.addr(), zero, size_needed);
+                if (gen.addr() + VMPageSize > next_page &&
+                    next_page < new_brk &&
+                    p->pTable->translate(next_page))
+                {
+                    size_needed = VMPageSize - size_needed;
+                    tp->memsetBlob(next_page, zero, size_needed);
+                }
+            }
         }
     }