Fix the use of brk.  This change removes the requirement for the "real" brk
segment to be moved up to stage2's brk segment.  Instead, Valgrind's
use of brk is simulated with mmap.  In order to prevent any unwanted use
of the process brk segment, it also sets the RLIMIT_DATA to 0, which will
make brk always fail.  glibc's malloc will use mmap to allocate if brk
fails.  We try to intercept glibc's brk, but malloc seems to always use the
library-internal version.  (The client's use of brk has always been simulated,
and is unaffected by this change.)


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@2266 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c
index f4f95ba..516a3ef 100644
--- a/coregrind/vg_main.c
+++ b/coregrind/vg_main.c
@@ -105,6 +105,8 @@
 Addr VG_(valgrind_mmap_end);	 /* valgrind's mmaps are between valgrind_base and here */
 Addr VG_(valgrind_end);
 
+vki_rlimit VG_(client_rlimit_data);
+
 /* This is set early to indicate whether this CPU has the
    SSE/fxsave/fxrestor features.  */
 Bool VG_(have_ssestate);
@@ -1364,7 +1366,6 @@
    }
 
    info->map_base = VG_(client_mapbase);
-   info->setbrk   = False;
 
    info->exe_base = VG_(client_base);
    info->exe_end  = VG_(client_end);
@@ -2664,6 +2665,7 @@
    Addr esp_at_startup;    /* client's %esp at the point we gained control. */
    UInt * client_auxv;
    VgSchedReturnCode src;
+   vki_rlimit zero = { 0, 0 };
 
    //============================================================
    // Nb: startup is complex.  Prerequisites are shown at every step.
@@ -2671,6 +2673,14 @@
    // *** Be very careful when messing with the order ***
    //============================================================
 
+   // Get the current process datasize rlimit, and set it to zero.
+   // This prevents any internal uses of brk() from having any effect.
+   // We remember the old value so we can restore it on exec, so that
+   // child processes will have a reasonable brk value.
+   VG_(getrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data));
+   zero.rlim_max = VG_(client_rlimit_data).rlim_max;
+   VG_(setrlimit)(VKI_RLIMIT_DATA, &zero);
+   
    //--------------------------------------------------------------
    // Check we were launched by stage1
    //   p: n/a  [must be first step]