In vg_memory.c, startup_segment_callback, fix initialisation ordering
problem which caused the leak checker to misbehave following recent
PLT-bypass workaround.

In short, it is an error to announce to the skin, segments found which
belong to the low-level memory manager, because the skin may then mark
them as accessible to the client.  This is wrong, and the client
should only acquire accessible memory via malloc etc and stack
movement.  Now we carefully avoid mentioning any segment belonging to
the low level memory manager.

Take the opportunity to improve VG_(within_m_state_static) so that it
also detects pointers within the thread table.  This can reduce the
number of blocks the leak checker spuriously thinks are still
reachable.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1751 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/memcheck/mc_main.c b/memcheck/mc_main.c
index 167a2ba..28a9712 100644
--- a/memcheck/mc_main.c
+++ b/memcheck/mc_main.c
@@ -518,6 +518,27 @@
    exist, *bad_addr is set to the offending address, so the caller can
    know what it is. */
 
+/* Returns True if [a .. a+len) is not addressible.  Otherwise,
+   returns False, and if bad_addr is non-NULL, sets *bad_addr to
+   indicate the lowest failing address.  Functions below are
+   similar. */
+Bool MC_(check_noaccess) ( Addr a, UInt len, Addr* bad_addr )
+{
+   UInt  i;
+   UChar abit;
+   PROF_EVENT(42);
+   for (i = 0; i < len; i++) {
+      PROF_EVENT(43);
+      abit = get_abit(a);
+      if (abit == VGM_BIT_VALID) {
+         if (bad_addr != NULL) *bad_addr = a;
+         return False;
+      }
+      a++;
+   }
+   return True;
+}
+
 Bool MC_(check_writable) ( Addr a, UInt len, Addr* bad_addr )
 {
    UInt  i;
@@ -1655,6 +1676,7 @@
    MAC_( ban_mem_heap)             = & MC_(make_noaccess);
    MAC_(copy_mem_heap)             = & mc_copy_address_range_state;
    MAC_( die_mem_heap)             = & MC_(make_noaccess);
+   MAC_(check_noaccess)            = & MC_(check_noaccess);
 
    VG_(track_new_mem_startup)      ( & mc_new_mem_startup );
    VG_(track_new_mem_stack_signal) ( & MC_(make_writable) );