To get 32-bit programs working on Opteron, VG_(valgrind_end) was recently
changed to name the last byte in Valgrind's section, rather than one past the
last byte.  This was because the last byte is 0xffffffff, and so one past gave
0x0, which screwed things up.

However, when this change was made, all the places where VG_(valgrind_end) is
used weren't adjusted appropriately.  So this commit makes those adjustments.
It also renames the variable as VG_(valgrind_last), which makes the difference
between it and the other VG_(*_end) variables much clearer.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@2672 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/core.h b/coregrind/core.h
index 7144a56..17d4280 100644
--- a/coregrind/core.h
+++ b/coregrind/core.h
@@ -1245,7 +1245,7 @@
 extern Addr VG_(shadow_base);	/* tool's shadow memory */
 extern Addr VG_(shadow_end);
 extern Addr VG_(valgrind_base);	/* valgrind's address range */
-extern Addr VG_(valgrind_end);
+extern Addr VG_(valgrind_last); // Nb: last byte, rather than one past the end
 
 extern vki_rlimit VG_(client_rlimit_data); /* client's original rlimit data */
 
diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c
index b80e286..8ef52cc 100644
--- a/coregrind/vg_main.c
+++ b/coregrind/vg_main.c
@@ -108,11 +108,9 @@
 
 Addr VG_(valgrind_base);	 /* valgrind's address range */
 
-// VG_(valgrind_end) has a slightly different meaning to all the other
-// VG_(*_end) vars -- ie. it names the last byte, whereas the others
-// go one byte past the end.
-
-Addr VG_(valgrind_end);
+// Note that VG_(valgrind_last) names the last byte of the section, whereas
+// the VG_(*_end) vars name the byte one past the end of the section.
+Addr VG_(valgrind_last);
 
 vki_rlimit VG_(client_rlimit_data);
 
@@ -403,12 +401,7 @@
    addr_t client_size, shadow_size;
 
    VG_(valgrind_base)  = (addr_t)&kickstart_base;
-
-   // VG_(valgrind_end) has a slightly different meaning to all the other
-   // VG_(*_end) vars -- ie. it names the last byte, whereas the others
-   // go one byte past the end.
-
-   VG_(valgrind_end)   = ROUNDUP(argc_addr, 0x10000) - 1; // stack
+   VG_(valgrind_last)  = ROUNDUP(argc_addr, 0x10000) - 1; // stack
 
    // This gives the client the largest possible address space while
    // taking into account the tool's shadow needs.
@@ -434,14 +427,14 @@
          "shadow_base        %8x (%dMB)\n"
          "shadow_end         %8x\n"
          "valgrind_base      %8x (%dMB)\n"
-         "valgrind_end       %8x\n",
+         "valgrind_last      %8x\n",
          VG_(client_base),       SEGSIZE(client_base,       client_mapbase),
          VG_(client_mapbase),    SEGSIZE(client_mapbase,    client_end),
          VG_(client_end),        SEGSIZE(client_end,        shadow_base),
          VG_(shadow_base),       SEGSIZE(shadow_base,       shadow_end),
          VG_(shadow_end),
-         VG_(valgrind_base),     SEGSIZE(valgrind_base,     valgrind_end),
-         VG_(valgrind_end)
+         VG_(valgrind_base),     SEGSIZE(valgrind_base,     valgrind_last),
+         VG_(valgrind_last)
       );
 
 #undef SEGSIZE
@@ -2277,7 +2270,7 @@
       symbols.  This is so we know where the free space is before we
       start allocating more memory (note: heap is OK, it's just mmap
       which is the problem here). */
-   if (start >= VG_(valgrind_base) && (start+size) <= VG_(valgrind_end)) {
+   if (start >= VG_(valgrind_base) && (start+size-1) <= VG_(valgrind_last)) {
       flags |= SF_VALGRIND;
       VG_(map_file_segment)(start, size, prot, flags, dev, ino, foffset, filename);
    }
@@ -2310,7 +2303,7 @@
    if (filename != NULL)
       flags |= SF_FILE;
 
-   if (start >= VG_(valgrind_base) && (start+size) <= VG_(valgrind_end))
+   if (start >= VG_(valgrind_base) && (start+size-1) <= VG_(valgrind_last))
       flags |= SF_VALGRIND;
 
    VG_(map_file_segment)(start, size, prot, flags, dev, ino, foffset, filename);
@@ -2438,7 +2431,7 @@
 		 | and mappings            |
                  -                         -
                  | valgrind stack ^^^^^^^^^|
-  valgrind_end   +-------------------------+
+  valgrind_last  +-------------------------+
 		 : kernel                  :
 
   Nb: Before we can do general allocations with VG_(arena_malloc)() and
diff --git a/coregrind/vg_memory.c b/coregrind/vg_memory.c
index 588297e..995a697 100644
--- a/coregrind/vg_memory.c
+++ b/coregrind/vg_memory.c
@@ -493,7 +493,7 @@
    static const Bool debug = False || mem_debug;
    Segment *s;
    Addr ret;
-   Addr limit = (for_client ? VG_(client_end) : VG_(valgrind_end));
+   Addr limit = (for_client ? VG_(client_end)-1 : VG_(valgrind_last));
 
    if (addr == 0)
       addr = for_client ? VG_(client_mapbase) : VG_(valgrind_base);
@@ -537,7 +537,7 @@
 	 VG_(printf)("  s == NULL\n");
    }
 
-   if ((limit - len) < ret)
+   if (((limit - len)+1) < ret)
       ret = 0;			/* no space */
    else
       ret += VKI_BYTES_PER_PAGE; /* skip leading redzone */
@@ -550,7 +550,7 @@
 }
 
 /* Pad the entire process address space, from VG_(client_base)
-   to VG_(valgrind_end) by creating an anonymous and inaccessible
+   to VG_(valgrind_last) by creating an anonymous and inaccessible
    mapping over any part of the address space which is not covered
    by an entry in the segment list.
 
@@ -572,7 +572,7 @@
    args[4] = -1;
    args[5] = 0;
    
-   while (s && addr < VG_(valgrind_end)) {
+   while (s && addr <= VG_(valgrind_last)) {
       if (addr < s->addr) {
          args[0] = (UInt)addr;
          args[1] = s->addr - addr;
@@ -584,9 +584,9 @@
       s = VG_(SkipNode_Next)(&sk_segments, s);
    }
 
-   if (addr < VG_(valgrind_end)) {
+   if (addr <= VG_(valgrind_last)) {
       args[0] = (UInt)addr;
-      args[1] = VG_(valgrind_end) - addr;
+      args[1] = VG_(valgrind_last) - addr + 1;
 
       ret = VG_(do_syscall)(__NR_mmap, (UInt)args);
    }
@@ -602,7 +602,7 @@
    Segment *s = VG_(SkipNode_First)(&sk_segments);
    Int ret;
 
-   while (s && addr < VG_(valgrind_end)) {
+   while (s && addr <= VG_(valgrind_last)) {
       if (addr < s->addr) {
          ret = VG_(do_syscall)(__NR_munmap, (UInt)addr, s->addr - addr);
       }
@@ -611,8 +611,8 @@
       s = VG_(SkipNode_Next)(&sk_segments, s);
    }
 
-   if (addr < VG_(valgrind_end)) {
-      ret = VG_(do_syscall)(__NR_munmap, (UInt)addr, VG_(valgrind_end) - addr);
+   if (addr <= VG_(valgrind_last)) {
+      ret = VG_(do_syscall)(__NR_munmap, addr, (VG_(valgrind_last) - addr) + 1);
    }
 
    return;
@@ -793,7 +793,7 @@
 
 Bool VG_(is_valgrind_addr)(Addr a)
 {
-   return a >= VG_(valgrind_base) && a < VG_(valgrind_end);
+   return a >= VG_(valgrind_base) && a <= VG_(valgrind_last);
 }
 
 Addr VG_(get_client_base)(void)
diff --git a/coregrind/vg_mylibc.c b/coregrind/vg_mylibc.c
index 0b70234..b79b8da 100644
--- a/coregrind/vg_mylibc.c
+++ b/coregrind/vg_mylibc.c
@@ -284,7 +284,7 @@
       if (flags & VKI_MAP_CLIENT) {
          vg_assert(VG_(client_base) <= res && res+length < VG_(client_end));
       } else {
-         vg_assert(VG_(valgrind_base) <= res && res+length < VG_(valgrind_end));
+         vg_assert(VG_(valgrind_base) <= res && res+length <= VG_(valgrind_last));
       }
 
       sf_flags |= SF_MMAP;
@@ -1092,7 +1092,7 @@
    Addr stacktop, sigstack_low, sigstack_high;
 
    asm("movl %%ebp, %0; movl %%esp, %1" : "=r" (ebp), "=r" (esp));
-   stacktop = VG_(valgrind_end);
+   stacktop = VG_(valgrind_last);
    VG_(get_sigstack_bounds)( &sigstack_low, &sigstack_high );
    if (esp >= sigstack_low && esp < sigstack_high)
       stacktop = sigstack_high;
@@ -1663,7 +1663,7 @@
                  VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS, 0, -1, 0);
 
    if (p != ((void*)(-1))) {
-      vg_assert(p >= (void*)VG_(valgrind_base) && p < (void*)VG_(valgrind_end));
+      vg_assert(p >= (void*)VG_(valgrind_base) && p <= (void*)VG_(valgrind_last));
       tot_alloc += (UInt)nBytes;
       if (0)
          VG_(printf)(