[BACK]Return to dyn_load.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / gc

Diff for /OpenXM_contrib2/asir2000/gc/dyn_load.c between version 1.3 and 1.4

version 1.3, 2000/12/01 09:26:10 version 1.4, 2001/04/20 07:39:18
Line 26 
Line 26 
  * None of this is safe with dlclose and incremental collection.   * None of this is safe with dlclose and incremental collection.
  * But then not much of anything is safe in the presence of dlclose.   * But then not much of anything is safe in the presence of dlclose.
  */   */
 #ifndef MACOS  #if !defined(MACOS) && !defined(_WIN32_WCE)
 #  include <sys/types.h>  #  include <sys/types.h>
 #endif  #endif
 #include "gc_priv.h"  #include "private/gc_priv.h"
   
 /* BTL: avoid circular redefinition of dlopen if SOLARIS_THREADS defined */  /* BTL: avoid circular redefinition of dlopen if SOLARIS_THREADS defined */
 # if (defined(LINUX_THREADS) || defined(SOLARIS_THREADS) \  # if (defined(LINUX_THREADS) || defined(SOLARIS_THREADS) \
       || defined(HPUX_THREADS) || defined(IRIX_THREADS)) && defined(dlopen) \        || defined(HPUX_THREADS) || defined(IRIX_THREADS)) && defined(dlopen) \
      && !defined(USE_LD_WRAP)       && !defined(GC_USE_LD_WRAP)
     /* To support threads in Solaris, gc.h interposes on dlopen by       */      /* To support threads in Solaris, gc.h interposes on dlopen by       */
     /* defining "dlopen" to be "GC_dlopen", which is implemented below.  */      /* defining "dlopen" to be "GC_dlopen", which is implemented below.  */
     /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the   */      /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the   */
Line 46 
Line 46 
 #   undef GC_must_restore_redefined_dlopen  #   undef GC_must_restore_redefined_dlopen
 # endif  # endif
   
 #if (defined(DYNAMIC_LOADING) || defined(MSWIN32)) && !defined(PCR)  #if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE)) \
       && !defined(PCR)
 #if !defined(SUNOS4) && !defined(SUNOS5DL) && !defined(IRIX5) && \  #if !defined(SUNOS4) && !defined(SUNOS5DL) && !defined(IRIX5) && \
     !defined(MSWIN32) && !(defined(ALPHA) && defined(OSF1)) && \      !defined(MSWIN32) && !defined(MSWINCE) && \
       !(defined(ALPHA) && defined(OSF1)) && \
     !defined(HPUX) && !(defined(LINUX) && defined(__ELF__)) && \      !defined(HPUX) && !(defined(LINUX) && defined(__ELF__)) && \
     !defined(RS6000) && !defined(SCO_ELF)      !defined(RS6000) && !defined(SCO_ELF) && \
       !(defined(NETBSD) && defined(__ELF__))
  --> We only know how to find data segments of dynamic libraries for the   --> We only know how to find data segments of dynamic libraries for the
  --> above.  Additional SVR4 variants might not be too   --> above.  Additional SVR4 variants might not be too
  --> hard to add.   --> hard to add.
Line 121  GC_FirstDLOpenedLinkMap()
Line 124  GC_FirstDLOpenedLinkMap()
   
 #endif /* SUNOS5DL ... */  #endif /* SUNOS5DL ... */
   
   /* BTL: added to fix circular dlopen definition if SOLARIS_THREADS defined */
   # if defined(GC_must_restore_redefined_dlopen)
   #   define dlopen GC_dlopen
   # endif
   
 #if defined(SUNOS4) && !defined(USE_PROC_FOR_LIBRARIES)  #if defined(SUNOS4) && !defined(USE_PROC_FOR_LIBRARIES)
   
 #ifdef LINT  #ifdef LINT
Line 161  static ptr_t GC_first_common()
Line 169  static ptr_t GC_first_common()
   
 #endif  /* SUNOS4 ... */  #endif  /* SUNOS4 ... */
   
 # if defined(LINUX_THREADS) || defined(SOLARIS_THREADS) \  
      || defined(HPUX_THREADS) || defined(IRIX_THREADS)  
   /* Make sure we're not in the middle of a collection, and make        */  
   /* sure we don't start any.   Returns previous value of GC_dont_gc.   */  
   /* This is invoked prior to a dlopen call to avoid synchronization    */  
   /* issues.  We can't just acquire the allocation lock, since startup  */  
   /* code in dlopen may try to allocate.                                */  
   /* This solution risks heap growth in the presence of many dlopen     */  
   /* calls in either a multithreaded environment, or if the library     */  
   /* initialization code allocates substantial amounts of GC'ed memory. */  
   /* But I don't know of a better solution.                             */  
   /* This can still deadlock if the client explicitly starts a GC       */  
   /* during the dlopen.  He shouldn't do that.                          */  
   static GC_bool disable_gc_for_dlopen()  
   {  
     GC_bool result;  
     LOCK();  
     result = GC_dont_gc;  
     while (GC_incremental && GC_collection_in_progress()) {  
         GC_collect_a_little_inner(1000);  
     }  
     GC_dont_gc = TRUE;  
     UNLOCK();  
     return(result);  
   }  
   
   /* Redefine dlopen to guarantee mutual exclusion with */  
   /* GC_register_dynamic_libraries.                     */  
   /* Should probably happen for other operating systems, too. */  
   
 #include <dlfcn.h>  
   
 #ifdef USE_LD_WRAP  
   void * __wrap_dlopen(const char *path, int mode)  
 #else  
   void * GC_dlopen(path, mode)  
   GC_CONST char * path;  
   int mode;  
 #endif  
 {  
     void * result;  
     GC_bool dont_gc_save;  
   
 #   ifndef USE_PROC_FOR_LIBRARIES  
       dont_gc_save = disable_gc_for_dlopen();  
 #   endif  
 #   ifdef USE_LD_WRAP  
       result = __real_dlopen(path, mode);  
 #   else  
       result = dlopen(path, mode);  
 #   endif  
 #   ifndef USE_PROC_FOR_LIBRARIES  
       GC_dont_gc = dont_gc_save;  
 #   endif  
     return(result);  
 }  
 # endif  /* SOLARIS_THREADS */  
   
 /* BTL: added to fix circular dlopen definition if SOLARIS_THREADS defined */  
 # if defined(GC_must_restore_redefined_dlopen)  
 #   define dlopen GC_dlopen  
 # endif  
   
 # if defined(SUNOS4) || defined(SUNOS5DL)  # if defined(SUNOS4) || defined(SUNOS5DL)
 /* Add dynamic library data sections to the root set.           */  /* Add dynamic library data sections to the root set.           */
 # if !defined(PCR) && !defined(SOLARIS_THREADS) && defined(THREADS)  # if !defined(PCR) && !defined(SOLARIS_THREADS) && defined(THREADS)
Line 297  void GC_register_dynamic_libraries()
Line 242  void GC_register_dynamic_libraries()
 # endif /* !USE_PROC ... */  # endif /* !USE_PROC ... */
 # endif /* SUNOS */  # endif /* SUNOS */
   
 #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF)  #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
       (defined(NETBSD) && defined(__ELF__))
   
   
   #ifdef USE_PROC_FOR_LIBRARIES
   
   #include <string.h>
   
   #include <sys/stat.h>
   #include <fcntl.h>
   #include <unistd.h>
   
   #define MAPS_BUF_SIZE (32*1024)
   
   extern ssize_t GC_repeat_read(int fd, char *buf, size_t count);
           /* Repeatedly read until buffer is filled, or EOF is encountered */
           /* Defined in os_dep.c.                                          */
   
   static char *parse_map_entry(char *buf_ptr, word *start, word *end,
                                char *prot_buf, unsigned int *maj_dev);
   
   void GC_register_dynamic_libraries()
   {
       int f;
       int result;
       char prot_buf[5];
       int maps_size;
       char maps_temp[32768];
       char *maps_buf;
       char *buf_ptr;
       int count;
       word start, end;
       unsigned int maj_dev, min_dev;
       word least_ha, greatest_ha;
       unsigned i;
       word datastart = (word)(DATASTART);
   
       /* Read /proc/self/maps     */
           /* Note that we may not allocate, and thus can't use stdio.     */
           f = open("/proc/self/maps", O_RDONLY);
           if (-1 == f) ABORT("Couldn't open /proc/self/maps");
           /* stat() doesn't work for /proc/self/maps, so we have to
              read it to find out how large it is... */
           maps_size = 0;
           do {
               result = GC_repeat_read(f, maps_temp, sizeof(maps_temp));
               if (result <= 0) ABORT("Couldn't read /proc/self/maps");
               maps_size += result;
           } while (result == sizeof(maps_temp));
   
           if (maps_size > sizeof(maps_temp)) {
               /* If larger than our buffer, close and re-read it. */
               close(f);
               f = open("/proc/self/maps", O_RDONLY);
               if (-1 == f) ABORT("Couldn't open /proc/self/maps");
               maps_buf = alloca(maps_size);
               if (NULL == maps_buf) ABORT("/proc/self/maps alloca failed");
               result = GC_repeat_read(f, maps_buf, maps_size);
               if (result <= 0) ABORT("Couldn't read /proc/self/maps");
           } else {
               /* Otherwise use the fixed size buffer */
               maps_buf = maps_temp;
           }
   
           close(f);
           maps_buf[result] = '\0';
           buf_ptr = maps_buf;
       /* Compute heap bounds. Should be done by add_to_heap?      */
           least_ha = (word)(-1);
           greatest_ha = 0;
           for (i = 0; i < GC_n_heap_sects; ++i) {
               word sect_start = (word)GC_heap_sects[i].hs_start;
               word sect_end = sect_start + GC_heap_sects[i].hs_bytes;
               if (sect_start < least_ha) least_ha = sect_start;
               if (sect_end > greatest_ha) greatest_ha = sect_end;
           }
           if (greatest_ha < (word)GC_scratch_last_end_ptr)
               greatest_ha = (word)GC_scratch_last_end_ptr;
       for (;;) {
   
           buf_ptr = parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev);
           if (buf_ptr == NULL) return;
   
           if (prot_buf[1] == 'w') {
               /* This is a writable mapping.  Add it to           */
               /* the root set unless it is already otherwise      */
               /* accounted for.                                   */
               if (start <= (word)GC_stackbottom && end >= (word)GC_stackbottom) {
                   /* Stack mapping; discard       */
                   continue;
               }
               if (start <= datastart && end > datastart && maj_dev != 0) {
                   /* Main data segment; discard   */
                   continue;
               }
   #           ifdef THREADS
                 if (GC_segment_is_thread_stack(start, end)) continue;
   #           endif
               /* The rest of this assumes that there is no mapping        */
               /* spanning the beginning of the data segment, or extending */
               /* beyond the entire heap at both ends.                     */
               /* Empirically these assumptions hold.                      */
   
               if (start < (word)DATAEND && end > (word)DATAEND) {
                   /* Rld may use space at the end of the main data        */
                   /* segment.  Thus we add that in.                       */
                   start = (word)DATAEND;
               }
               if (start < least_ha && end > least_ha) {
                   end = least_ha;
               }
               if (start < greatest_ha && end > greatest_ha) {
                   start = greatest_ha;
               }
               if (start >= least_ha && end <= greatest_ha) continue;
               GC_add_roots_inner((char *)start, (char *)end, TRUE);
           }
        }
   }
   
   //
   //  parse_map_entry parses an entry from /proc/self/maps so we can
   //  locate all writable data segments that belong to shared libraries.
   //  The format of one of these entries and the fields we care about
   //  is as follows:
   //  XXXXXXXX-XXXXXXXX r-xp 00000000 30:05 260537     name of mapping...\n
   //  ^^^^^^^^ ^^^^^^^^ ^^^^          ^^
   //  start    end      prot          maj_dev
   //  0        9        18            32
   //
   //  The parser is called with a pointer to the entry and the return value
   //  is either NULL or is advanced to the next entry(the byte after the
   //  trailing '\n'.)
   //
   #define OFFSET_MAP_START   0
   #define OFFSET_MAP_END     9
   #define OFFSET_MAP_PROT   18
   #define OFFSET_MAP_MAJDEV 32
   
   static char *parse_map_entry(char *buf_ptr, word *start, word *end,
                                char *prot_buf, unsigned int *maj_dev)
   {
       int i;
       unsigned int val;
       char *tok;
   
       if (buf_ptr == NULL || *buf_ptr == '\0') {
           return NULL;
       }
   
       memcpy(prot_buf, buf_ptr+OFFSET_MAP_PROT, 4); // do the protections first
       prot_buf[4] = '\0';
   
       if (prot_buf[1] == 'w') { // we can skip all of this if it's not writable
   
           tok = buf_ptr;
           buf_ptr[OFFSET_MAP_START+8] = '\0';
           *start = strtoul(tok, NULL, 16);
   
           tok = buf_ptr+OFFSET_MAP_END;
           buf_ptr[OFFSET_MAP_END+8] = '\0';
           *end = strtoul(tok, NULL, 16);
   
           buf_ptr += OFFSET_MAP_MAJDEV;
           tok = buf_ptr;
           while (*buf_ptr != ':') buf_ptr++;
           *buf_ptr++ = '\0';
           *maj_dev = strtoul(tok, NULL, 16);
       }
   
       while (*buf_ptr && *buf_ptr++ != '\n');
   
       return buf_ptr;
   }
   
   #else /* !USE_PROC_FOR_LIBRARIES */
   
 /* Dynamic loading code for Linux running ELF. Somewhat tested on  /* Dynamic loading code for Linux running ELF. Somewhat tested on
  * Linux/x86, untested but hopefully should work on Linux/Alpha.   * Linux/x86, untested but hopefully should work on Linux/Alpha.
  * This code was derived from the Solaris/ELF support. Thanks to   * This code was derived from the Solaris/ELF support. Thanks to
  * whatever kind soul wrote that.  - Patrick Bridges */   * whatever kind soul wrote that.  - Patrick Bridges */
   
 #include <elf.h>  #if defined(NETBSD)
   #  include <sys/exec_elf.h>
   #else
   #  include <elf.h>
   #endif
 #include <link.h>  #include <link.h>
   
 /* Newer versions of Linux/Alpha and Linux/x86 define this macro.  We  /* Newer versions of Linux/Alpha and Linux/x86 define this macro.  We
Line 379  void GC_register_dynamic_libraries()
Line 503  void GC_register_dynamic_libraries()
     }      }
 }  }
   
 #endif  #endif /* !USE_PROC_FOR_LIBRARIES */
   
 #if defined(IRIX5) || defined(USE_PROC_FOR_LIBRARIES)  #endif /* LINUX */
   
   #if defined(IRIX5) || (defined(USE_PROC_FOR_LIBRARIES) && !defined(LINUX))
   
 #include <sys/procfs.h>  #include <sys/procfs.h>
 #include <sys/stat.h>  #include <sys/stat.h>
 #include <fcntl.h>  #include <fcntl.h>
Line 393  extern void * GC_roots_present();
Line 519  extern void * GC_roots_present();
         /* The type is a lie, since the real type doesn't make sense here, */          /* The type is a lie, since the real type doesn't make sense here, */
         /* and we only test for NULL.                                      */          /* and we only test for NULL.                                      */
   
 #ifndef GC_scratch_last_end_ptr   /* Never an extern any more? */  
 extern ptr_t GC_scratch_last_end_ptr; /* End of GC_scratch_alloc arena  */  
 #endif  
   
 /* We use /proc to track down all parts of the address space that are   */  /* We use /proc to track down all parts of the address space that are   */
 /* mapped by the process, and throw out regions we know we shouldn't    */  /* mapped by the process, and throw out regions we know we shouldn't    */
Line 504  void GC_register_dynamic_libraries()
Line 627  void GC_register_dynamic_libraries()
   
 # endif /* USE_PROC || IRIX5 */  # endif /* USE_PROC || IRIX5 */
   
 # ifdef MSWIN32  # if defined(MSWIN32) || defined(MSWINCE)
   
 # define WIN32_LEAN_AND_MEAN  # define WIN32_LEAN_AND_MEAN
 # define NOSERVICE  # define NOSERVICE
Line 513  void GC_register_dynamic_libraries()
Line 636  void GC_register_dynamic_libraries()
   
   /* We traverse the entire address space and register all segments     */    /* We traverse the entire address space and register all segments     */
   /* that could possibly have been written to.                          */    /* that could possibly have been written to.                          */
   DWORD GC_allocation_granularity;  
   
   extern GC_bool GC_is_heap_base (ptr_t p);    extern GC_bool GC_is_heap_base (ptr_t p);
   
 # ifdef WIN32_THREADS  # ifdef WIN32_THREADS
     extern void GC_get_next_stack(char *start, char **lo, char **hi);      extern void GC_get_next_stack(char *start, char **lo, char **hi);
 # endif      void GC_cond_add_roots(char *base, char * limit)
   
   void GC_cond_add_roots(char *base, char * limit)  
   {  
     char dummy;  
     char * stack_top  
            = (char *) ((word)(&dummy) & ~(GC_allocation_granularity-1));  
     if (base == limit) return;  
 #   ifdef WIN32_THREADS  
     {      {
         char * curr_base = base;        char * curr_base = base;
         char * next_stack_lo;        char * next_stack_lo;
         char * next_stack_hi;        char * next_stack_hi;
   
         for(;;) {        if (base == limit) return;
             GC_get_next_stack(curr_base, &next_stack_lo, &next_stack_hi);        for(;;) {
             if (next_stack_lo >= limit) break;            GC_get_next_stack(curr_base, &next_stack_lo, &next_stack_hi);
             GC_add_roots_inner(curr_base, next_stack_lo, TRUE);            if (next_stack_lo >= limit) break;
             curr_base = next_stack_hi;            GC_add_roots_inner(curr_base, next_stack_lo, TRUE);
         }            curr_base = next_stack_hi;
         if (curr_base < limit) GC_add_roots_inner(curr_base, limit, TRUE);        }
         if (curr_base < limit) GC_add_roots_inner(curr_base, limit, TRUE);
     }      }
 #   else  # else
         if (limit > stack_top && base < GC_stackbottom) {      void GC_cond_add_roots(char *base, char * limit)
             /* Part of the stack; ignore it. */      {
             return;        char dummy;
         }        char * stack_top
         GC_add_roots_inner(base, limit, TRUE);           = (char *) ((word)(&dummy) & ~(GC_sysinfo.dwAllocationGranularity-1));
 #   endif        if (base == limit) return;
   }        if (limit > stack_top && base < GC_stackbottom) {
             /* Part of the stack; ignore it. */
             return;
         }
         GC_add_roots_inner(base, limit, TRUE);
       }
   # endif
   
   # ifndef MSWINCE
   extern GC_bool GC_win32s;    extern GC_bool GC_win32s;
   # endif
   
   void GC_register_dynamic_libraries()    void GC_register_dynamic_libraries()
   {    {
     MEMORY_BASIC_INFORMATION buf;      MEMORY_BASIC_INFORMATION buf;
     SYSTEM_INFO sysinfo;  
     DWORD result;      DWORD result;
     DWORD protect;      DWORD protect;
     LPVOID p;      LPVOID p;
     char * base;      char * base;
     char * limit, * new_limit;      char * limit, * new_limit;
   
     if (GC_win32s) return;  #   ifdef MSWIN32
     GetSystemInfo(&sysinfo);        if (GC_win32s) return;
     base = limit = p = sysinfo.lpMinimumApplicationAddress;  #   endif
     GC_allocation_granularity = sysinfo.dwAllocationGranularity;      base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;
     while (p < sysinfo.lpMaximumApplicationAddress) {  #   if defined(MSWINCE) && !defined(_WIN32_WCE_EMULATION)
       /* Only the first 32 MB of address space belongs to the current process */
       while (p < (LPVOID)0x02000000) {
         result = VirtualQuery(p, &buf, sizeof(buf));          result = VirtualQuery(p, &buf, sizeof(buf));
         if (result != sizeof(buf)) {          if (result == 0) {
             ABORT("Weird VirtualQuery result");              /* Page is free; advance to the next possible allocation base */
         }              new_limit = (char *)
         new_limit = (char *)p + buf.RegionSize;                  (((DWORD) p + GC_sysinfo.dwAllocationGranularity)
         protect = buf.Protect;                   & ~(GC_sysinfo.dwAllocationGranularity-1));
         if (buf.State == MEM_COMMIT          } else
             && (protect == PAGE_EXECUTE_READWRITE  #   else
                 || protect == PAGE_READWRITE)      while (p < GC_sysinfo.lpMaximumApplicationAddress) {
             && !GC_is_heap_base(buf.AllocationBase)) {          result = VirtualQuery(p, &buf, sizeof(buf));
             if ((char *)p == limit) {  #   endif
                 limit = new_limit;          {
             } else {              if (result != sizeof(buf)) {
                 GC_cond_add_roots(base, limit);                  ABORT("Weird VirtualQuery result");
                 base = p;              }
                 limit = new_limit;              new_limit = (char *)p + buf.RegionSize;
             }              protect = buf.Protect;
         }              if (buf.State == MEM_COMMIT
                   && (protect == PAGE_EXECUTE_READWRITE
                       || protect == PAGE_READWRITE)
                   && !GC_is_heap_base(buf.AllocationBase)) {
                   if ((char *)p != limit) {
                       GC_cond_add_roots(base, limit);
                       base = p;
                   }
                   limit = new_limit;
               }
           }
         if (p > (LPVOID)new_limit /* overflow */) break;          if (p > (LPVOID)new_limit /* overflow */) break;
         p = (LPVOID)new_limit;          p = (LPVOID)new_limit;
     }      }
     GC_cond_add_roots(base, limit);      GC_cond_add_roots(base, limit);
   }    }
   
 #endif /* MSWIN32 */  #endif /* MSWIN32 || MSWINCE */
   
 #if defined(ALPHA) && defined(OSF1)  #if defined(ALPHA) && defined(OSF1)
   
 #include <loader.h>  #include <loader.h>

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>