Renamed and retyped the fields relating to valgrind's stack in os_state_t to
make their role clearer and their behaviour more consistent with the fields
describing the client's stack.  Also made the code in x86-linux/syscalls.c
and amd64-linux/syscalls.c more word-size-independent, which is not strictly
necessary but makes the code similarities between the two files more
obvious.

One consequence of this is that Valgrind's stack on AMD64 is now 16384 * 8
bytes, rather than 16384 * 4 bytes.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3520 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/amd64-linux/syscalls.c b/coregrind/amd64-linux/syscalls.c
index 21dda51..150b9d7 100644
--- a/coregrind/amd64-linux/syscalls.c
+++ b/coregrind/amd64-linux/syscalls.c
@@ -180,16 +180,15 @@
 
    They're allocated lazily, but never freed.
  */
-#define FILL	0xdeadbeef
+#define FILL	0xdeadbeefcabafeed
 
-static ULong *allocstack(ThreadId tid)
+static UWord* allocstack(ThreadId tid)
 {
    ThreadState *tst = VG_(get_ThreadState)(tid);
-   ULong* rsp;
-   UInt*  pUInt;
+   UWord* rsp;
 
-   if (tst->os_state.stack == NULL) {
-      void *stk = VG_(mmap)(0, VGA_STACK_SIZE_W * sizeof(Int) + VKI_PAGE_SIZE,
+   if (tst->os_state.valgrind_stack_base == 0) {
+      void *stk = VG_(mmap)(0, VGA_STACK_SIZE_W * sizeof(UWord) + VKI_PAGE_SIZE,
 			    VKI_PROT_READ|VKI_PROT_WRITE,
 			    VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS,
 			    SF_VALGRIND,
@@ -197,54 +196,56 @@
 
       if (stk != (void *)-1) {
 	 VG_(mprotect)(stk, VKI_PAGE_SIZE, VKI_PROT_NONE); /* guard page */
-	 tst->os_state.stack = (UInt *)stk + VKI_PAGE_SIZE/sizeof(UInt);
-	 tst->os_state.stacksize = VGA_STACK_SIZE_W;
+	 tst->os_state.valgrind_stack_base = ((Addr)stk) + VKI_PAGE_SIZE;
+	 tst->os_state.valgrind_stack_szB  = VGA_STACK_SIZE_W * sizeof(UWord);
       } else 
-	 return (ULong *)-1;
+	 return (UWord*)-1;
    }
 
-   for (pUInt = tst->os_state.stack; 
-        pUInt < (tst->os_state.stack + tst->os_state.stacksize); 
-        pUInt++)
-      *pUInt = FILL;
+   for (rsp = (UWord*) tst->os_state.valgrind_stack_base; 
+        rsp < (UWord*)(tst->os_state.valgrind_stack_base +
+                       tst->os_state.valgrind_stack_szB); 
+        rsp++)
+      *rsp = FILL;
    /* rsp is left at top of stack */
-   rsp = (ULong*)pUInt;
 
    if (0)
-      VG_(printf)("stack for tid %d at %p (%x); rsp=%p\n",
-		  tid, tst->os_state.stack, *tst->os_state.stack,
-		  rsp);
+      VG_(printf)("stack for tid %d at %p (%llx); rsp=%p\n",
+		  tid, tst->os_state.valgrind_stack_base,
+                  *(UWord*)(tst->os_state.valgrind_stack_base), rsp);
 
    return rsp;
 }
 
 /* NB: this is identical the the x86 version. */
 /* Return how many bytes of this stack have not been used */
-Int VGA_(stack_unused)(ThreadId tid)
+SSizeT VGA_(stack_unused)(ThreadId tid)
 {
    ThreadState *tst = VG_(get_ThreadState)(tid);
-   UInt *p;
+   UWord* p;
 
-   for (p = tst->os_state.stack; 
-	p && (p < (tst->os_state.stack + tst->os_state.stacksize)); 
+   for (p = (UWord*)tst->os_state.valgrind_stack_base; 
+	p && (p < (UWord*)(tst->os_state.valgrind_stack_base +
+                           tst->os_state.valgrind_stack_szB)); 
 	p++)
       if (*p != FILL)
 	 break;
 
    if (0)
-      VG_(printf)("p=%p %x tst->os_state.stack=%p\n", p, *p, tst->os_state.stack);
+      VG_(printf)("p=%p %llx tst->os_state.valgrind_stack_base=%p\n",
+                  p, *p, tst->os_state.valgrind_stack_base);
 
-   return (p - tst->os_state.stack) * sizeof(*p);
+   return ((Addr)p) - tst->os_state.valgrind_stack_base;
 }
 
-
 /*
    Allocate a stack for the main thread, and call VGA_(thread_wrapper)
    on that stack.
  */
 void VGA_(main_thread_wrapper)(ThreadId tid)
 {
-   ULong *rsp = allocstack(tid);
+   UWord* rsp = allocstack(tid);
+
    vg_assert(tid == VG_(master_tid));
 
    call_on_new_stack_0_1( 
@@ -318,7 +319,7 @@
    ThreadId ctid = VG_(alloc_ThreadState)();
    ThreadState *ptst = VG_(get_ThreadState)(ptid);
    ThreadState *ctst = VG_(get_ThreadState)(ctid);
-   ULong *stack;
+   UWord *stack;
    Segment *seg;
    Int ret;
    vki_sigset_t blockall, savedmask;
diff --git a/coregrind/core.h b/coregrind/core.h
index ace6477..7b03d96 100644
--- a/coregrind/core.h
+++ b/coregrind/core.h
@@ -1681,7 +1681,7 @@
 void VGA_(main_thread_wrapper)(ThreadId tid) __attribute__ ((__noreturn__));
 
 // Return how many bytes of a thread's Valgrind stack are unused
-Int VGA_(stack_unused)(ThreadId tid);
+SSizeT VGA_(stack_unused)(ThreadId tid);
 
 // Terminate the process.  Does not return.
 void VGA_(terminate)(ThreadId tid, VgSchedReturnCode src) __attribute__((__noreturn__));
diff --git a/coregrind/linux/core_os.c b/coregrind/linux/core_os.c
index f71d331..7b25cb1 100644
--- a/coregrind/linux/core_os.c
+++ b/coregrind/linux/core_os.c
@@ -8,8 +8,8 @@
 
 void VGA_(os_state_init)(ThreadState *tst)
 {
-   tst->os_state.stack = 0;
-   tst->os_state.stacksize = 0;
+   tst->os_state.valgrind_stack_base = 0;
+   tst->os_state.valgrind_stack_szB  = 0;
 
    VGA_(os_state_clear)(tst);
 }
diff --git a/coregrind/linux/core_os.h b/coregrind/linux/core_os.h
index 5478434..8457539 100644
--- a/coregrind/linux/core_os.h
+++ b/coregrind/linux/core_os.h
@@ -172,8 +172,8 @@
    ThreadId parent;		/* parent tid (if any) */
 
    /* runtime details */
-   UInt *stack;			/* stack base */
-   UInt stacksize;		/* stack size in UInts */
+   Addr  valgrind_stack_base;	/* Valgrind's stack base */
+   SizeT valgrind_stack_szB;	/* stack size in bytes */
 
    /* exit details */
    Int  exitcode;		/* in the case of exitgroup, set by someone else */
diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c
index ba8f21e..a750570 100644
--- a/coregrind/vg_main.c
+++ b/coregrind/vg_main.c
@@ -2317,7 +2317,7 @@
 
       /* Look for stack overruns.  Visit all threads. */
       for(tid = 1; tid < VG_N_THREADS; tid++) {
-	 Int remains;
+	 SSizeT remains;
 
 	 if (VG_(threads)[tid].status == VgTs_Empty ||
 	     VG_(threads)[tid].status == VgTs_Zombie)
diff --git a/coregrind/vg_mylibc.c b/coregrind/vg_mylibc.c
index e8f7e26..13d2844 100644
--- a/coregrind/vg_mylibc.c
+++ b/coregrind/vg_mylibc.c
@@ -1153,7 +1153,8 @@
    VGA_GET_REAL_STACK_PTR(sp);
    VGA_GET_REAL_FRAME_PTR(fp);
 
-   stacktop = (Addr)(tst->os_state.stack + tst->os_state.stacksize);
+   stacktop = tst->os_state.valgrind_stack_base + 
+              tst->os_state.valgrind_stack_szB;
 
    VG_(get_StackTrace2)(ips, VG_(clo_backtrace_size),
                         ret, fp, sp, stacktop);
diff --git a/coregrind/vg_signals.c b/coregrind/vg_signals.c
index 6eb5f8b..01e42c1 100644
--- a/coregrind/vg_signals.c
+++ b/coregrind/vg_signals.c
@@ -1940,7 +1940,8 @@
                            VGP_UCONTEXT_INSTR_PTR(uc),
                            VGP_UCONTEXT_FRAME_PTR(uc),
                            VGP_UCONTEXT_STACK_PTR(uc),
-                           (Addr)(tst->os_state.stack + tst->os_state.stacksize));
+                           tst->os_state.valgrind_stack_base + 
+                           tst->os_state.valgrind_stack_szB);
       VG_(core_panic_at)("Killed by fatal signal", ips);
    }
 }
diff --git a/coregrind/x86-linux/syscalls.c b/coregrind/x86-linux/syscalls.c
index 5f046cb..bbf566b 100644
--- a/coregrind/x86-linux/syscalls.c
+++ b/coregrind/x86-linux/syscalls.c
@@ -180,13 +180,13 @@
  */
 #define FILL	0xdeadbeef
 
-static UInt *allocstack(ThreadId tid)
+static UWord* allocstack(ThreadId tid)
 {
    ThreadState *tst = VG_(get_ThreadState)(tid);
-   UInt *esp;
+   UWord *esp;
 
-   if (tst->os_state.stack == NULL) {
-      void *stk = VG_(mmap)(0, VGA_STACK_SIZE_W * sizeof(Int) + VKI_PAGE_SIZE,
+   if (tst->os_state.valgrind_stack_base == 0) {
+      void *stk = VG_(mmap)(0, VGA_STACK_SIZE_W * sizeof(UWord) + VKI_PAGE_SIZE,
 			    VKI_PROT_READ|VKI_PROT_WRITE,
 			    VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS,
 			    SF_VALGRIND,
@@ -194,41 +194,46 @@
 
       if (stk != (void *)-1) {
 	 VG_(mprotect)(stk, VKI_PAGE_SIZE, VKI_PROT_NONE); /* guard page */
-	 tst->os_state.stack = (UInt *)stk + VKI_PAGE_SIZE/sizeof(UInt);
-	 tst->os_state.stacksize = VGA_STACK_SIZE_W;
+	 tst->os_state.valgrind_stack_base = ((Addr)stk) + VKI_PAGE_SIZE;
+	 tst->os_state.valgrind_stack_szB  = VGA_STACK_SIZE_W * sizeof(UWord);
       } else 
-	 return (UInt *)-1;
+	 return (UWord*)-1;
    }
 
-   for(esp = tst->os_state.stack; esp < (tst->os_state.stack + tst->os_state.stacksize); esp++)
+   for (esp = (UWord*) tst->os_state.valgrind_stack_base;
+        esp < (UWord*)(tst->os_state.valgrind_stack_base + 
+                       tst->os_state.valgrind_stack_szB); 
+        esp++)
       *esp = FILL;
    /* esp is left at top of stack */
 
    if (0)
       VG_(printf)("stack for tid %d at %p (%x); esp=%p\n",
-		  tid, tst->os_state.stack, *tst->os_state.stack,
-		  esp);
+		  tid, tst->os_state.valgrind_stack_base, 
+                  *(UWord*)(tst->os_state.valgrind_stack_base), esp);
 
    return esp;
 }
 
 /* NB: this is identical the the amd64 version. */
 /* Return how many bytes of this stack have not been used */
-Int VGA_(stack_unused)(ThreadId tid)
+SSizeT VGA_(stack_unused)(ThreadId tid)
 {
    ThreadState *tst = VG_(get_ThreadState)(tid);
-   UInt *p;
+   UWord* p;
 
-   for (p = tst->os_state.stack; 
-	p && (p < (tst->os_state.stack + tst->os_state.stacksize)); 
+   for (p = (UWord*)tst->os_state.valgrind_stack_base; 
+	p && (p < (UWord*)(tst->os_state.valgrind_stack_base +
+                           tst->os_state.valgrind_stack_szB)); 
 	p++)
       if (*p != FILL)
 	 break;
 
    if (0)
-      VG_(printf)("p=%p %x tst->os_state.stack=%p\n", p, *p, tst->os_state.stack);
+      VG_(printf)("p=%p %x tst->os_state.valgrind_stack_base=%p\n",
+                  p, *p, tst->os_state.valgrind_stack_base);
 
-   return (p - tst->os_state.stack) * sizeof(*p);
+   return ((Addr)p) - tst->os_state.valgrind_stack_base;
 }
 
 /*
@@ -237,7 +242,7 @@
  */
 void VGA_(main_thread_wrapper)(ThreadId tid)
 {
-   UInt *esp = allocstack(tid);
+   UWord* esp = allocstack(tid);
 
    vg_assert(tid == VG_(master_tid));
 
@@ -312,7 +317,7 @@
    ThreadId ctid = VG_(alloc_ThreadState)();
    ThreadState *ptst = VG_(get_ThreadState)(ptid);
    ThreadState *ctst = VG_(get_ThreadState)(ctid);
-   UInt *stack;
+   UWord *stack;
    Segment *seg;
    Int ret;
    vki_sigset_t blockall, savedmask;