changeset 89:b97de546208e

6671882: memory access after free in solaris/vm/os_solaris.cpp Summary: Corrected the wrong memory access problem and made some minor clean ups Reviewed-by: dholmes, jcoomes
author xlu
date Thu, 03 Apr 2008 12:21:06 -0700
parents 092ea87cc974
children cf4e16e9ca60
files src/os/solaris/vm/os_solaris.cpp
diffstat 1 files changed, 25 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/os/solaris/vm/os_solaris.cpp	Fri Mar 28 23:35:42 2008 -0700
+++ b/src/os/solaris/vm/os_solaris.cpp	Thu Apr 03 12:21:06 2008 -0700
@@ -4391,61 +4391,52 @@
 // threads. Calling thr_setprio is meaningless in this case.
 //
 bool isT2_libthread() {
-  int i, rslt;
   static prheader_t * lwpArray = NULL;
   static int lwpSize = 0;
   static int lwpFile = -1;
   lwpstatus_t * that;
-  int aslwpcount;
   char lwpName [128];
   bool isT2 = false;
 
 #define ADR(x)  ((uintptr_t)(x))
 #define LWPINDEX(ary,ix)   ((lwpstatus_t *)(((ary)->pr_entsize * (ix)) + (ADR((ary) + 1))))
 
-  aslwpcount = 0;
+  lwpFile = open("/proc/self/lstatus", O_RDONLY, 0);
+  if (lwpFile < 0) {
+      if (ThreadPriorityVerbose) warning ("Couldn't open /proc/self/lstatus\n");
+      return false;
+  }
   lwpSize = 16*1024;
-  lwpArray = ( prheader_t *)NEW_C_HEAP_ARRAY (char, lwpSize);
-  lwpFile = open ("/proc/self/lstatus", O_RDONLY, 0);
-  if (lwpArray == NULL) {
-      if ( ThreadPriorityVerbose ) warning ("Couldn't allocate T2 Check array\n");
-      return(isT2);
-  }
-  if (lwpFile < 0) {
-      if ( ThreadPriorityVerbose ) warning ("Couldn't open /proc/self/lstatus\n");
-      return(isT2);
-  }
   for (;;) {
     lseek (lwpFile, 0, SEEK_SET);
-    rslt = read (lwpFile, lwpArray, lwpSize);
-    if ((lwpArray->pr_nent * lwpArray->pr_entsize) <= lwpSize) {
+    lwpArray = (prheader_t *)NEW_C_HEAP_ARRAY(char, lwpSize);
+    if (read(lwpFile, lwpArray, lwpSize) < 0) {
+      if (ThreadPriorityVerbose) warning("Error reading /proc/self/lstatus\n");
       break;
     }
-    FREE_C_HEAP_ARRAY(char, lwpArray);
+    if ((lwpArray->pr_nent * lwpArray->pr_entsize) <= lwpSize) {
+       // We got a good snapshot - now iterate over the list.
+      int aslwpcount = 0;
+      for (int i = 0; i < lwpArray->pr_nent; i++ ) {
+        that = LWPINDEX(lwpArray,i);
+        if (that->pr_flags & PR_ASLWP) {
+          aslwpcount++;
+        }
+      }
+      if (aslwpcount == 0) isT2 = true;
+      break;
+    }
     lwpSize = lwpArray->pr_nent * lwpArray->pr_entsize;
-    lwpArray = ( prheader_t *)NEW_C_HEAP_ARRAY (char, lwpSize);
-    if (lwpArray == NULL) {
-        if ( ThreadPriorityVerbose ) warning ("Couldn't allocate T2 Check array\n");
-        return(isT2);
-    }
-  }
-
-  // We got a good snapshot - now iterate over the list.
-  for (i = 0; i < lwpArray->pr_nent; i++ ) {
-    that = LWPINDEX(lwpArray,i);
-    if (that->pr_flags & PR_ASLWP) {
-      aslwpcount++;
-    }
-  }
-  if ( aslwpcount == 0 ) isT2 = true;
+    FREE_C_HEAP_ARRAY(char, lwpArray);  // retry.
+  }
 
   FREE_C_HEAP_ARRAY(char, lwpArray);
   close (lwpFile);
-  if ( ThreadPriorityVerbose ) {
-    if ( isT2 ) tty->print_cr("We are running with a T2 libthread\n");
+  if (ThreadPriorityVerbose) {
+    if (isT2) tty->print_cr("We are running with a T2 libthread\n");
     else tty->print_cr("We are not running with a T2 libthread\n");
   }
-  return (isT2);
+  return isT2;
 }