2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
5 * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
17 # include "private/gc_priv.h"
19 # if defined(LINUX) && !defined(POWERPC)
20 # include <linux/version.h>
21 # if (LINUX_VERSION_CODE <= 0x10400)
22 /* Ugly hack to get struct sigcontext_struct definition. Required */
23 /* for some early 1.3.X releases. Will hopefully go away soon. */
24 /* in some later Linux releases, asm/sigcontext.h may have to */
25 /* be included instead. */
27 # include <asm/signal.h>
30 /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
31 /* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
32 /* prototypes, so we have to include the top-level sigcontext.h to */
33 /* make sure the former gets defined to be the latter if appropriate. */
34 # include <features.h>
36 # if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
37 /* glibc 2.1 no longer has sigcontext.h. But signal.h */
38 /* has the right declaration for glibc 2.1. */
39 # include <sigcontext.h>
40 # endif /* 0 == __GLIBC_MINOR__ */
41 # else /* not 2 <= __GLIBC__ */
42 /* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
43 /* one. Check LINUX_VERSION_CODE to see which we should reference. */
44 # include <asm/sigcontext.h>
45 # endif /* 2 <= __GLIBC__ */
48 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS) \
50 # include <sys/types.h>
51 # if !defined(MSWIN32) && !defined(SUNOS4)
58 # define SIGSEGV 0 /* value is irrelevant */
63 #if defined(LINUX) || defined(LINUX_STACKBOTTOM)
67 /* Blatantly OS dependent routines, except for those that are related */
68 /* to dynamic loading. */
70 # if defined(HEURISTIC2) || defined(SEARCH_FOR_DATA_START)
71 # define NEED_FIND_LIMIT
74 # if !defined(STACKBOTTOM) && defined(HEURISTIC2)
75 # define NEED_FIND_LIMIT
78 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
79 # define NEED_FIND_LIMIT
82 # if (defined(SVR4) || defined(AUX) || defined(DGUX) \
83 || (defined(LINUX) && defined(SPARC))) && !defined(PCR)
84 # define NEED_FIND_LIMIT
87 #if defined(FREEBSD) && (defined(I386) || defined(X86_64) || defined(powerpc) || defined(__powerpc__))
88 # include <machine/trap.h>
90 # define NEED_FIND_LIMIT
94 #if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__) \
95 && !defined(NEED_FIND_LIMIT)
96 /* Used by GC_init_netbsd_elf() below. */
97 # define NEED_FIND_LIMIT
100 #ifdef NEED_FIND_LIMIT
105 # define GC_AMIGA_DEF
106 # include "AmigaOS.c"
110 #if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
111 # define WIN32_LEAN_AND_MEAN
113 # include <windows.h>
117 # include <Processes.h>
121 # include <sys/uio.h>
122 # include <malloc.h> /* for locking */
124 #if defined(USE_MMAP) || defined(USE_MUNMAP)
126 --> USE_MUNMAP requires USE_MMAP
128 # include <sys/types.h>
129 # include <sys/mman.h>
130 # include <sys/stat.h>
136 # if defined(SUNOS5SIGS) && !defined(FREEBSD)
137 # include <sys/siginfo.h>
139 /* Define SETJMP and friends to be the version that restores */
140 /* the signal mask. */
141 # define SETJMP(env) sigsetjmp(env, 1)
142 # define LONGJMP(env, val) siglongjmp(env, val)
143 # define JMP_BUF sigjmp_buf
145 # define SETJMP(env) setjmp(env)
146 # define LONGJMP(env, val) longjmp(env, val)
147 # define JMP_BUF jmp_buf
151 /* for get_etext and friends */
152 #include <mach-o/getsect.h>
156 /* Apparently necessary for djgpp 2.01. May cause problems with */
157 /* other versions. */
158 typedef long unsigned int caddr_t
;
162 # include "il/PCR_IL.h"
163 # include "th/PCR_ThCtl.h"
164 # include "mm/PCR_MM.h"
167 #if !defined(NO_EXECUTE_PERMISSION)
168 # define OPT_PROT_EXEC PROT_EXEC
170 # define OPT_PROT_EXEC 0
173 #if defined(LINUX) && \
174 (defined(USE_PROC_FOR_LIBRARIES) || defined(IA64) || !defined(SMALL_CONFIG))
176 /* We need to parse /proc/self/maps, either to find dynamic libraries, */
177 /* and/or to find the register backing store base (IA64). Do it once */
182 /* Repeatedly perform a read call until the buffer is filled or */
183 /* we encounter EOF. */
184 ssize_t
GC_repeat_read(int fd
, char *buf
, size_t count
)
186 ssize_t num_read
= 0;
189 while (num_read
< count
) {
190 result
= READ(fd
, buf
+ num_read
, count
- num_read
);
191 if (result
< 0) return result
;
192 if (result
== 0) break;
199 * Apply fn to a buffer containing the contents of /proc/self/maps.
200 * Return the result of fn or, if we failed, 0.
201 * We currently do nothing to /proc/self/maps other than simply read
202 * it. This code could be simplified if we could determine its size
206 word
GC_apply_to_maps(word (*fn
)(char *))
210 size_t maps_size
= 4000; /* Initial guess. */
211 static char init_buf
[1];
212 static char *maps_buf
= init_buf
;
213 static size_t maps_buf_sz
= 1;
215 /* Read /proc/self/maps, growing maps_buf as necessary. */
216 /* Note that we may not allocate conventionally, and */
217 /* thus can't use stdio. */
219 if (maps_size
>= maps_buf_sz
) {
220 /* Grow only by powers of 2, since we leak "too small" buffers. */
221 while (maps_size
>= maps_buf_sz
) maps_buf_sz
*= 2;
222 maps_buf
= GC_scratch_alloc(maps_buf_sz
);
223 if (maps_buf
== 0) return 0;
225 f
= open("/proc/self/maps", O_RDONLY
);
226 if (-1 == f
) return 0;
229 result
= GC_repeat_read(f
, maps_buf
, maps_buf_sz
-1);
230 if (result
<= 0) return 0;
232 } while (result
== maps_buf_sz
-1);
234 } while (maps_size
>= maps_buf_sz
);
235 maps_buf
[maps_size
] = '\0';
237 /* Apply fn to result. */
241 #endif /* Need GC_apply_to_maps */
243 #if defined(LINUX) && (defined(USE_PROC_FOR_LIBRARIES) || defined(IA64))
245 // GC_parse_map_entry parses an entry from /proc/self/maps so we can
246 // locate all writable data segments that belong to shared libraries.
247 // The format of one of these entries and the fields we care about
249 // XXXXXXXX-XXXXXXXX r-xp 00000000 30:05 260537 name of mapping...\n
250 // ^^^^^^^^ ^^^^^^^^ ^^^^ ^^
251 // start end prot maj_dev
253 // Note that since about auguat 2003 kernels, the columns no longer have
254 // fixed offsets on 64-bit kernels. Hence we no longer rely on fixed offsets
255 // anywhere, which is safer anyway.
259 * Assign various fields of the first line in buf_ptr to *start, *end,
260 * *prot_buf and *maj_dev. Only *prot_buf may be set for unwritable maps.
262 char *GC_parse_map_entry(char *buf_ptr
, word
*start
, word
*end
,
263 char *prot_buf
, unsigned int *maj_dev
)
265 char *start_start
, *end_start
, *prot_start
, *maj_dev_start
;
269 if (buf_ptr
== NULL
|| *buf_ptr
== '\0') {
274 while (isspace(*p
)) ++p
;
276 GC_ASSERT(isxdigit(*start_start
));
277 *start
= strtoul(start_start
, &endp
, 16); p
= endp
;
282 GC_ASSERT(isxdigit(*end_start
));
283 *end
= strtoul(end_start
, &endp
, 16); p
= endp
;
284 GC_ASSERT(isspace(*p
));
286 while (isspace(*p
)) ++p
;
288 GC_ASSERT(*prot_start
== 'r' || *prot_start
== '-');
289 memcpy(prot_buf
, prot_start
, 4);
291 if (prot_buf
[1] == 'w') {/* we can skip the rest if it's not writable. */
292 /* Skip past protection field to offset field */
293 while (!isspace(*p
)) ++p
; while (isspace(*p
)) ++p
;
294 GC_ASSERT(isxdigit(*p
));
295 /* Skip past offset field, which we ignore */
296 while (!isspace(*p
)) ++p
; while (isspace(*p
)) ++p
;
298 GC_ASSERT(isxdigit(*maj_dev_start
));
299 *maj_dev
= strtoul(maj_dev_start
, NULL
, 16);
302 while (*p
&& *p
++ != '\n');
307 #endif /* Need to parse /proc/self/maps. */
309 #if defined(SEARCH_FOR_DATA_START)
310 /* The I386 case can be handled without a search. The Alpha case */
311 /* used to be handled differently as well, but the rules changed */
312 /* for recent Linux versions. This seems to be the easiest way to */
313 /* cover all versions. */
315 # if defined(LINUX) || defined(HURD)
316 /* Some Linux distributions arrange to define __data_start. Some */
317 /* define data_start as a weak symbol. The latter is technically */
318 /* broken, since the user program may define data_start, in which */
319 /* case we lose. Nonetheless, we try both, prefering __data_start. */
320 /* We assume gcc-compatible pragmas. */
321 # pragma weak __data_start
322 extern int __data_start
[];
323 # pragma weak data_start
324 extern int data_start
[];
330 void GC_init_linux_data_start()
332 extern ptr_t
GC_find_limit();
334 # if defined(LINUX) || defined(HURD)
335 /* Try the easy approaches first: */
336 if ((ptr_t
)__data_start
!= 0) {
337 GC_data_start
= (ptr_t
)(__data_start
);
340 if ((ptr_t
)data_start
!= 0) {
341 GC_data_start
= (ptr_t
)(data_start
);
345 GC_data_start
= GC_find_limit((ptr_t
)(_end
), FALSE
);
351 # ifndef ECOS_GC_MEMORY_SIZE
352 # define ECOS_GC_MEMORY_SIZE (448 * 1024)
353 # endif /* ECOS_GC_MEMORY_SIZE */
355 // setjmp() function, as described in ANSI para 7.6.1.1
357 #define SETJMP( __env__ ) hal_setjmp( __env__ )
359 // FIXME: This is a simple way of allocating memory which is
360 // compatible with ECOS early releases. Later releases use a more
361 // sophisticated means of allocating memory than this simple static
362 // allocator, but this method is at least bound to work.
363 static char memory
[ECOS_GC_MEMORY_SIZE
];
364 static char *brk
= memory
;
366 static void *tiny_sbrk(ptrdiff_t increment
)
372 if (brk
> memory
+ sizeof memory
)
380 #define sbrk tiny_sbrk
383 #if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__)
386 void GC_init_netbsd_elf()
388 extern ptr_t
GC_find_limit();
389 extern char **environ
;
390 /* This may need to be environ, without the underscore, for */
392 GC_data_start
= GC_find_limit((ptr_t
)&environ
, FALSE
);
400 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
403 unsigned short magic_number
;
404 unsigned short padding
[29];
408 #define E_MAGIC(x) (x).magic_number
409 #define EMAGIC 0x5A4D
410 #define E_LFANEW(x) (x).new_exe_offset
413 unsigned char magic_number
[2];
414 unsigned char byte_order
;
415 unsigned char word_order
;
416 unsigned long exe_format_level
;
419 unsigned long padding1
[13];
420 unsigned long object_table_offset
;
421 unsigned long object_count
;
422 unsigned long padding2
[31];
425 #define E32_MAGIC1(x) (x).magic_number[0]
426 #define E32MAGIC1 'L'
427 #define E32_MAGIC2(x) (x).magic_number[1]
428 #define E32MAGIC2 'X'
429 #define E32_BORDER(x) (x).byte_order
431 #define E32_WORDER(x) (x).word_order
433 #define E32_CPU(x) (x).cpu
435 #define E32_OBJTAB(x) (x).object_table_offset
436 #define E32_OBJCNT(x) (x).object_count
442 unsigned long pagemap
;
443 unsigned long mapsize
;
444 unsigned long reserved
;
447 #define O32_FLAGS(x) (x).flags
448 #define OBJREAD 0x0001L
449 #define OBJWRITE 0x0002L
450 #define OBJINVALID 0x0080L
451 #define O32_SIZE(x) (x).size
452 #define O32_BASE(x) (x).base
454 # else /* IBM's compiler */
456 /* A kludge to get around what appears to be a header file bug */
458 # define WORD unsigned short
461 # define DWORD unsigned long
468 # endif /* __IBMC__ */
470 # define INCL_DOSEXCEPTIONS
471 # define INCL_DOSPROCESS
472 # define INCL_DOSERRORS
473 # define INCL_DOSMODULEMGR
474 # define INCL_DOSMEMMGR
478 /* Disable and enable signals during nontrivial allocations */
480 void GC_disable_signals(void)
484 DosEnterMustComplete(&nest
);
485 if (nest
!= 1) ABORT("nested GC_disable_signals");
488 void GC_enable_signals(void)
492 DosExitMustComplete(&nest
);
493 if (nest
!= 0) ABORT("GC_enable_signals");
499 # if !defined(PCR) && !defined(AMIGA) && !defined(MSWIN32) \
500 && !defined(MSWINCE) \
501 && !defined(MACOS) && !defined(DJGPP) && !defined(DOS4GW) \
502 && !defined(NOSYS) && !defined(ECOS)
504 # if defined(SIG_BLOCK)
505 /* Use POSIX/SYSV interface */
506 # define SIGSET_T sigset_t
507 # define SIG_DEL(set, signal) sigdelset(&(set), (signal))
508 # define SIG_FILL(set) sigfillset(&set)
509 # define SIGSETMASK(old, new) sigprocmask(SIG_SETMASK, &(new), &(old))
510 # elif defined(sigmask) && !defined(UTS4) && !defined(HURD)
511 /* Use the traditional BSD interface */
512 # define SIGSET_T int
513 # define SIG_DEL(set, signal) (set) &= ~(sigmask(signal))
514 # define SIG_FILL(set) (set) = 0x7fffffff
515 /* Setting the leading bit appears to provoke a bug in some */
516 /* longjmp implementations. Most systems appear not to have */
518 # define SIGSETMASK(old, new) (old) = sigsetmask(new)
520 # error undetectable signal API
523 static GC_bool mask_initialized
= FALSE
;
525 static SIGSET_T new_mask
;
527 static SIGSET_T old_mask
;
529 static SIGSET_T dummy
;
531 #if defined(PRINTSTATS) && !defined(THREADS)
532 # define CHECK_SIGNALS
533 int GC_sig_disabled
= 0;
536 void GC_disable_signals()
538 if (!mask_initialized
) {
541 SIG_DEL(new_mask
, SIGSEGV
);
542 SIG_DEL(new_mask
, SIGILL
);
543 SIG_DEL(new_mask
, SIGQUIT
);
545 SIG_DEL(new_mask
, SIGBUS
);
548 SIG_DEL(new_mask
, SIGIOT
);
551 SIG_DEL(new_mask
, SIGEMT
);
554 SIG_DEL(new_mask
, SIGTRAP
);
556 mask_initialized
= TRUE
;
558 # ifdef CHECK_SIGNALS
559 if (GC_sig_disabled
!= 0) ABORT("Nested disables");
562 SIGSETMASK(old_mask
,new_mask
);
565 void GC_enable_signals()
567 # ifdef CHECK_SIGNALS
568 if (GC_sig_disabled
!= 1) ABORT("Unmatched enable");
571 SIGSETMASK(dummy
,old_mask
);
578 /* Ivan Demakov: simplest way (to me) */
580 void GC_disable_signals() { }
581 void GC_enable_signals() { }
584 /* Find the page size */
587 # if defined(MSWIN32) || defined(MSWINCE) || defined (CYGWIN32)
588 void GC_setpagesize()
590 GetSystemInfo(&GC_sysinfo
);
591 GC_page_size
= GC_sysinfo
.dwPageSize
;
595 # if defined(MPROTECT_VDB) || defined(PROC_VDB) || defined(USE_MMAP) \
596 || defined(USE_MUNMAP)
597 void GC_setpagesize()
599 GC_page_size
= GETPAGESIZE();
602 /* It's acceptable to fake it. */
603 void GC_setpagesize()
605 GC_page_size
= HBLKSIZE
;
611 * Find the base of the stack.
612 * Used only in single-threaded environment.
613 * With threads, GC_mark_roots needs to know how to do this.
614 * Called with allocator lock held.
616 # if defined(MSWIN32) || defined(MSWINCE)
617 # define is_writable(prot) ((prot) == PAGE_READWRITE \
618 || (prot) == PAGE_WRITECOPY \
619 || (prot) == PAGE_EXECUTE_READWRITE \
620 || (prot) == PAGE_EXECUTE_WRITECOPY)
621 /* Return the number of bytes that are writable starting at p. */
622 /* The pointer p is assumed to be page aligned. */
623 /* If base is not 0, *base becomes the beginning of the */
624 /* allocation region containing p. */
625 word
GC_get_writable_length(ptr_t p
, ptr_t
*base
)
627 MEMORY_BASIC_INFORMATION buf
;
631 result
= VirtualQuery(p
, &buf
, sizeof(buf
));
632 if (result
!= sizeof(buf
)) ABORT("Weird VirtualQuery result");
633 if (base
!= 0) *base
= (ptr_t
)(buf
.AllocationBase
);
634 protect
= (buf
.Protect
& ~(PAGE_GUARD
| PAGE_NOCACHE
));
635 if (!is_writable(protect
)) {
638 if (buf
.State
!= MEM_COMMIT
) return(0);
639 return(buf
.RegionSize
);
642 ptr_t
GC_get_stack_base()
645 ptr_t sp
= (ptr_t
)(&dummy
);
646 ptr_t trunc_sp
= (ptr_t
)((word
)sp
& ~(GC_page_size
- 1));
647 word size
= GC_get_writable_length(trunc_sp
, 0);
649 return(trunc_sp
+ size
);
653 # endif /* MS Windows */
656 # include <kernel/OS.h>
657 ptr_t
GC_get_stack_base(){
659 get_thread_info(find_thread(NULL
),&th
);
667 ptr_t
GC_get_stack_base()
672 if (DosGetInfoBlocks(&ptib
, &ppib
) != NO_ERROR
) {
673 GC_err_printf0("DosGetInfoBlocks failed\n");
674 ABORT("DosGetInfoBlocks failed\n");
676 return((ptr_t
)(ptib
-> tib_pstacklimit
));
683 # include "AmigaOS.c"
687 # if defined(NEED_FIND_LIMIT) || defined(UNIX_LIKE)
690 typedef void (*handler
)(int);
692 typedef void (*handler
)();
695 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1) \
696 || defined(HURD) || defined(NETBSD)
697 static struct sigaction old_segv_act
;
698 # if defined(IRIX5) || defined(HPUX) \
699 || defined(HURD) || defined(NETBSD)
700 static struct sigaction old_bus_act
;
703 static handler old_segv_handler
, old_bus_handler
;
707 void GC_set_and_save_fault_handler(handler h
)
709 void GC_set_and_save_fault_handler(h
)
713 # if defined(SUNOS5SIGS) || defined(IRIX5) \
714 || defined(OSF1) || defined(HURD) || defined(NETBSD)
715 struct sigaction act
;
718 # if 0 /* Was necessary for Solaris 2.3 and very temporary */
720 act
.sa_flags
= SA_RESTART
| SA_NODEFER
;
722 act
.sa_flags
= SA_RESTART
;
725 (void) sigemptyset(&act
.sa_mask
);
726 # ifdef GC_IRIX_THREADS
727 /* Older versions have a bug related to retrieving and */
728 /* and setting a handler at the same time. */
729 (void) sigaction(SIGSEGV
, 0, &old_segv_act
);
730 (void) sigaction(SIGSEGV
, &act
, 0);
731 (void) sigaction(SIGBUS
, 0, &old_bus_act
);
732 (void) sigaction(SIGBUS
, &act
, 0);
734 (void) sigaction(SIGSEGV
, &act
, &old_segv_act
);
735 # if defined(IRIX5) \
736 || defined(HPUX) || defined(HURD) || defined(NETBSD)
737 /* Under Irix 5.x or HP/UX, we may get SIGBUS. */
738 /* Pthreads doesn't exist under Irix 5.x, so we */
739 /* don't have to worry in the threads case. */
740 (void) sigaction(SIGBUS
, &act
, &old_bus_act
);
742 # endif /* GC_IRIX_THREADS */
744 old_segv_handler
= signal(SIGSEGV
, h
);
746 old_bus_handler
= signal(SIGBUS
, h
);
750 # endif /* NEED_FIND_LIMIT || UNIX_LIKE */
752 # ifdef NEED_FIND_LIMIT
753 /* Some tools to implement HEURISTIC2 */
754 # define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
755 /* static */ JMP_BUF GC_jmp_buf
;
758 void GC_fault_handler(sig
)
761 LONGJMP(GC_jmp_buf
, 1);
764 void GC_setup_temporary_fault_handler()
766 GC_set_and_save_fault_handler(GC_fault_handler
);
769 void GC_reset_fault_handler()
771 # if defined(SUNOS5SIGS) || defined(IRIX5) \
772 || defined(OSF1) || defined(HURD) || defined(NETBSD)
773 (void) sigaction(SIGSEGV
, &old_segv_act
, 0);
774 # if defined(IRIX5) \
775 || defined(HPUX) || defined(HURD) || defined(NETBSD)
776 (void) sigaction(SIGBUS
, &old_bus_act
, 0);
779 (void) signal(SIGSEGV
, old_segv_handler
);
781 (void) signal(SIGBUS
, old_bus_handler
);
786 /* Return the first nonaddressible location > p (up) or */
787 /* the smallest location q s.t. [q,p) is addressable (!up). */
788 /* We assume that p (up) or p-1 (!up) is addressable. */
789 ptr_t
GC_find_limit(p
, up
)
793 static VOLATILE ptr_t result
;
794 /* Needs to be static, since otherwise it may not be */
795 /* preserved across the longjmp. Can safely be */
796 /* static since it's only called once, with the */
797 /* allocation lock held. */
800 GC_setup_temporary_fault_handler();
801 if (SETJMP(GC_jmp_buf
) == 0) {
802 result
= (ptr_t
)(((word
)(p
))
803 & ~(MIN_PAGE_SIZE
-1));
806 result
+= MIN_PAGE_SIZE
;
808 result
-= MIN_PAGE_SIZE
;
810 GC_noop1((word
)(*result
));
813 GC_reset_fault_handler();
815 result
+= MIN_PAGE_SIZE
;
821 #if defined(ECOS) || defined(NOSYS)
822 ptr_t
GC_get_stack_base()
828 #ifdef HPUX_STACKBOTTOM
830 #include <sys/param.h>
831 #include <sys/pstat.h>
833 ptr_t
GC_get_register_stack_base(void)
835 struct pst_vm_status vm_status
;
838 while (pstat_getprocvm(&vm_status
, sizeof(vm_status
), 0, i
++) == 1) {
839 if (vm_status
.pst_type
== PS_RSESTACK
) {
840 return (ptr_t
) vm_status
.pst_vaddr
;
844 /* old way to get the register stackbottom */
845 return (ptr_t
)(((word
)GC_stackbottom
- BACKING_STORE_DISPLACEMENT
- 1)
846 & ~(BACKING_STORE_ALIGNMENT
- 1));
849 #endif /* HPUX_STACK_BOTTOM */
851 #ifdef LINUX_STACKBOTTOM
853 #include <sys/types.h>
854 #include <sys/stat.h>
856 # define STAT_SKIP 27 /* Number of fields preceding startstack */
857 /* field in /proc/self/stat */
859 #ifdef USE_LIBC_PRIVATES
860 # pragma weak __libc_stack_end
861 extern ptr_t __libc_stack_end
;
865 /* Try to read the backing store base from /proc/self/maps. */
866 /* We look for the writable mapping with a 0 major device, */
867 /* which is as close to our frame as possible, but below it.*/
868 static word
backing_store_base_from_maps(char *maps
)
871 char *buf_ptr
= maps
;
873 unsigned int maj_dev
;
874 word current_best
= 0;
878 buf_ptr
= GC_parse_map_entry(buf_ptr
, &start
, &end
, prot_buf
, &maj_dev
);
879 if (buf_ptr
== NULL
) return current_best
;
880 if (prot_buf
[1] == 'w' && maj_dev
== 0) {
881 if (end
< (word
)(&dummy
) && start
> current_best
) current_best
= start
;
887 static word
backing_store_base_from_proc(void)
889 return GC_apply_to_maps(backing_store_base_from_maps
);
892 # ifdef USE_LIBC_PRIVATES
893 # pragma weak __libc_ia64_register_backing_store_base
894 extern ptr_t __libc_ia64_register_backing_store_base
;
897 ptr_t
GC_get_register_stack_base(void)
899 # ifdef USE_LIBC_PRIVATES
900 if (0 != &__libc_ia64_register_backing_store_base
901 && 0 != __libc_ia64_register_backing_store_base
) {
902 /* Glibc 2.2.4 has a bug such that for dynamically linked */
903 /* executables __libc_ia64_register_backing_store_base is */
904 /* defined but uninitialized during constructor calls. */
905 /* Hence we check for both nonzero address and value. */
906 return __libc_ia64_register_backing_store_base
;
909 word result
= backing_store_base_from_proc();
911 /* Use dumb heuristics. Works only for default configuration. */
912 result
= (word
)GC_stackbottom
- BACKING_STORE_DISPLACEMENT
;
913 result
+= BACKING_STORE_ALIGNMENT
- 1;
914 result
&= ~(BACKING_STORE_ALIGNMENT
- 1);
915 /* Verify that it's at least readable. If not, we goofed. */
916 GC_noop1(*(word
*)result
);
918 return (ptr_t
)result
;
922 ptr_t
GC_linux_stack_base(void)
924 /* We read the stack base value from /proc/self/stat. We do this */
925 /* using direct I/O system calls in order to avoid calling malloc */
926 /* in case REDIRECT_MALLOC is defined. */
927 # define STAT_BUF_SIZE 4096
928 # define STAT_READ read
929 /* Should probably call the real read, if read is wrapped. */
930 char stat_buf
[STAT_BUF_SIZE
];
934 size_t i
, buf_offset
= 0;
936 /* First try the easy way. This should work for glibc 2.2 */
937 /* This fails in a prelinked ("prelink" command) executable */
938 /* since the correct value of __libc_stack_end never */
939 /* becomes visible to us. The second test works around */
941 # ifdef USE_LIBC_PRIVATES
942 if (0 != &__libc_stack_end
&& 0 != __libc_stack_end
) {
944 /* Some versions of glibc set the address 16 bytes too */
945 /* low while the initialization code is running. */
946 if (((word
)__libc_stack_end
& 0xfff) + 0x10 < 0x1000) {
947 return __libc_stack_end
+ 0x10;
948 } /* Otherwise it's not safe to add 16 bytes and we fall */
949 /* back to using /proc. */
952 /* Older versions of glibc for 64-bit Sparc do not set
953 * this variable correctly, it gets set to either zero
956 if (__libc_stack_end
!= (ptr_t
) (unsigned long)0x1)
957 return __libc_stack_end
;
959 return __libc_stack_end
;
964 f
= open("/proc/self/stat", O_RDONLY
);
965 if (f
< 0 || STAT_READ(f
, stat_buf
, STAT_BUF_SIZE
) < 2 * STAT_SKIP
) {
966 ABORT("Couldn't read /proc/self/stat");
968 c
= stat_buf
[buf_offset
++];
969 /* Skip the required number of fields. This number is hopefully */
970 /* constant across all Linux implementations. */
971 for (i
= 0; i
< STAT_SKIP
; ++i
) {
972 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
973 while (!isspace(c
)) c
= stat_buf
[buf_offset
++];
975 while (isspace(c
)) c
= stat_buf
[buf_offset
++];
979 c
= stat_buf
[buf_offset
++];
982 if (result
< 0x10000000) ABORT("Absurd stack bottom value");
983 return (ptr_t
)result
;
986 #endif /* LINUX_STACKBOTTOM */
988 #ifdef FREEBSD_STACKBOTTOM
990 /* This uses an undocumented sysctl call, but at least one expert */
991 /* believes it will stay. */
994 #include <sys/types.h>
995 #include <sys/sysctl.h>
997 ptr_t
GC_freebsd_stack_base(void)
999 int nm
[2] = {CTL_KERN
, KERN_USRSTACK
};
1001 size_t len
= sizeof(ptr_t
);
1002 int r
= sysctl(nm
, 2, &base
, &len
, NULL
, 0);
1004 if (r
) ABORT("Error getting stack base");
1009 #endif /* FREEBSD_STACKBOTTOM */
1011 #ifdef SOLARIS_STACKBOTTOM
1013 # include <thread.h>
1014 # include <signal.h>
1015 # include <pthread.h>
1017 /* These variables are used to cache ss_sp value for the primordial */
1018 /* thread (it's better not to call thr_stksegment() twice for this */
1019 /* thread - see JDK bug #4352906). */
1020 static pthread_t stackbase_main_self
= 0;
1021 /* 0 means stackbase_main_ss_sp value is unset. */
1022 static void *stackbase_main_ss_sp
= NULL
;
1024 ptr_t
GC_solaris_stack_base(void)
1027 pthread_t self
= pthread_self();
1028 if (self
== stackbase_main_self
)
1030 /* If the client calls GC_get_stack_base() from the main thread */
1031 /* then just return the cached value. */
1032 GC_ASSERT(stackbase_main_ss_sp
!= NULL
);
1033 return stackbase_main_ss_sp
;
1036 if (thr_stksegment(&s
)) {
1037 /* According to the manual, the only failure error code returned */
1038 /* is EAGAIN meaning "the information is not available due to the */
1039 /* thread is not yet completely initialized or it is an internal */
1040 /* thread" - this shouldn't happen here. */
1041 ABORT("thr_stksegment failed");
1043 /* s.ss_sp holds the pointer to the stack bottom. */
1044 GC_ASSERT((void *)&s HOTTER_THAN s
.ss_sp
);
1046 if (!stackbase_main_self
)
1048 /* Cache the stack base value for the primordial thread (this */
1049 /* is done during GC_init, so there is no race). */
1050 stackbase_main_ss_sp
= s
.ss_sp
;
1051 stackbase_main_self
= self
;
1057 #endif /* GC_SOLARIS_THREADS */
1059 #if !defined(BEOS) && !defined(AMIGA) && !defined(MSWIN32) \
1060 && !defined(MSWINCE) && !defined(OS2) && !defined(NOSYS) && !defined(ECOS)
1062 ptr_t
GC_get_stack_base()
1064 # if defined(HEURISTIC1) || defined(HEURISTIC2) || \
1065 defined(LINUX_STACKBOTTOM) || defined(FREEBSD_STACKBOTTOM) || \
1066 defined(SOLARIS_STACKBOTTOM)
1071 # define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1074 return(STACKBOTTOM
);
1077 # ifdef STACK_GROWS_DOWN
1078 result
= (ptr_t
)((((word
)(&dummy
))
1079 + STACKBOTTOM_ALIGNMENT_M1
)
1080 & ~STACKBOTTOM_ALIGNMENT_M1
);
1082 result
= (ptr_t
)(((word
)(&dummy
))
1083 & ~STACKBOTTOM_ALIGNMENT_M1
);
1085 # endif /* HEURISTIC1 */
1086 # ifdef LINUX_STACKBOTTOM
1087 result
= GC_linux_stack_base();
1089 # ifdef FREEBSD_STACKBOTTOM
1090 result
= GC_freebsd_stack_base();
1092 # ifdef SOLARIS_STACKBOTTOM
1093 result
= GC_solaris_stack_base();
1096 # ifdef STACK_GROWS_DOWN
1097 result
= GC_find_limit((ptr_t
)(&dummy
), TRUE
);
1098 # ifdef HEURISTIC2_LIMIT
1099 if (result
> HEURISTIC2_LIMIT
1100 && (ptr_t
)(&dummy
) < HEURISTIC2_LIMIT
) {
1101 result
= HEURISTIC2_LIMIT
;
1105 result
= GC_find_limit((ptr_t
)(&dummy
), FALSE
);
1106 # ifdef HEURISTIC2_LIMIT
1107 if (result
< HEURISTIC2_LIMIT
1108 && (ptr_t
)(&dummy
) > HEURISTIC2_LIMIT
) {
1109 result
= HEURISTIC2_LIMIT
;
1114 # endif /* HEURISTIC2 */
1115 # ifdef STACK_GROWS_DOWN
1116 if (result
== 0) result
= (ptr_t
)(signed_word
)(-sizeof(ptr_t
));
1119 # endif /* STACKBOTTOM */
1122 # endif /* ! AMIGA, !OS 2, ! MS Windows, !BEOS, !NOSYS, !ECOS */
1125 * Register static data segment(s) as roots.
1126 * If more data segments are added later then they need to be registered
1127 * add that point (as we do with SunOS dynamic loading),
1128 * or GC_mark_roots needs to check for them (as we do with PCR).
1129 * Called with allocator lock held.
1134 void GC_register_data_segments()
1138 HMODULE module_handle
;
1139 # define PBUFSIZ 512
1140 UCHAR path
[PBUFSIZ
];
1142 struct exe_hdr hdrdos
; /* MSDOS header. */
1143 struct e32_exe hdr386
; /* Real header for my executable */
1144 struct o32_obj seg
; /* Currrent segment */
1148 if (DosGetInfoBlocks(&ptib
, &ppib
) != NO_ERROR
) {
1149 GC_err_printf0("DosGetInfoBlocks failed\n");
1150 ABORT("DosGetInfoBlocks failed\n");
1152 module_handle
= ppib
-> pib_hmte
;
1153 if (DosQueryModuleName(module_handle
, PBUFSIZ
, path
) != NO_ERROR
) {
1154 GC_err_printf0("DosQueryModuleName failed\n");
1155 ABORT("DosGetInfoBlocks failed\n");
1157 myexefile
= fopen(path
, "rb");
1158 if (myexefile
== 0) {
1159 GC_err_puts("Couldn't open executable ");
1160 GC_err_puts(path
); GC_err_puts("\n");
1161 ABORT("Failed to open executable\n");
1163 if (fread((char *)(&hdrdos
), 1, sizeof hdrdos
, myexefile
) < sizeof hdrdos
) {
1164 GC_err_puts("Couldn't read MSDOS header from ");
1165 GC_err_puts(path
); GC_err_puts("\n");
1166 ABORT("Couldn't read MSDOS header");
1168 if (E_MAGIC(hdrdos
) != EMAGIC
) {
1169 GC_err_puts("Executable has wrong DOS magic number: ");
1170 GC_err_puts(path
); GC_err_puts("\n");
1171 ABORT("Bad DOS magic number");
1173 if (fseek(myexefile
, E_LFANEW(hdrdos
), SEEK_SET
) != 0) {
1174 GC_err_puts("Seek to new header failed in ");
1175 GC_err_puts(path
); GC_err_puts("\n");
1176 ABORT("Bad DOS magic number");
1178 if (fread((char *)(&hdr386
), 1, sizeof hdr386
, myexefile
) < sizeof hdr386
) {
1179 GC_err_puts("Couldn't read MSDOS header from ");
1180 GC_err_puts(path
); GC_err_puts("\n");
1181 ABORT("Couldn't read OS/2 header");
1183 if (E32_MAGIC1(hdr386
) != E32MAGIC1
|| E32_MAGIC2(hdr386
) != E32MAGIC2
) {
1184 GC_err_puts("Executable has wrong OS/2 magic number:");
1185 GC_err_puts(path
); GC_err_puts("\n");
1186 ABORT("Bad OS/2 magic number");
1188 if ( E32_BORDER(hdr386
) != E32LEBO
|| E32_WORDER(hdr386
) != E32LEWO
) {
1189 GC_err_puts("Executable %s has wrong byte order: ");
1190 GC_err_puts(path
); GC_err_puts("\n");
1191 ABORT("Bad byte order");
1193 if ( E32_CPU(hdr386
) == E32CPU286
) {
1194 GC_err_puts("GC can't handle 80286 executables: ");
1195 GC_err_puts(path
); GC_err_puts("\n");
1198 if (fseek(myexefile
, E_LFANEW(hdrdos
) + E32_OBJTAB(hdr386
),
1200 GC_err_puts("Seek to object table failed: ");
1201 GC_err_puts(path
); GC_err_puts("\n");
1202 ABORT("Seek to object table failed");
1204 for (nsegs
= E32_OBJCNT(hdr386
); nsegs
> 0; nsegs
--) {
1206 if (fread((char *)(&seg
), 1, sizeof seg
, myexefile
) < sizeof seg
) {
1207 GC_err_puts("Couldn't read obj table entry from ");
1208 GC_err_puts(path
); GC_err_puts("\n");
1209 ABORT("Couldn't read obj table entry");
1211 flags
= O32_FLAGS(seg
);
1212 if (!(flags
& OBJWRITE
)) continue;
1213 if (!(flags
& OBJREAD
)) continue;
1214 if (flags
& OBJINVALID
) {
1215 GC_err_printf0("Object with invalid pages?\n");
1218 GC_add_roots_inner(O32_BASE(seg
), O32_BASE(seg
)+O32_SIZE(seg
), FALSE
);
1224 # if defined(MSWIN32) || defined(MSWINCE) || defined (CYGWIN32)
1227 # define GC_no_win32_dlls (FALSE)
1231 /* Unfortunately, we have to handle win32s very differently from NT, */
1232 /* Since VirtualQuery has very different semantics. In particular, */
1233 /* under win32s a VirtualQuery call on an unmapped page returns an */
1234 /* invalid result. Under NT, GC_register_data_segments is a noop and */
1235 /* all real work is done by GC_register_dynamic_libraries. Under */
1236 /* win32s, we cannot find the data segments associated with dll's. */
1237 /* We register the main data segment here. */
1238 GC_bool GC_no_win32_dlls
= FALSE
;
1239 /* This used to be set for gcc, to avoid dealing with */
1240 /* the structured exception handling issues. But we now have */
1241 /* assembly code to do that right. */
1242 GC_bool GC_wnt
= FALSE
;
1243 /* This is a Windows NT derivative, i.e. NT, W2K, XP or later. */
1245 void GC_init_win32()
1247 /* if we're running under win32s, assume that no DLLs will be loaded */
1248 DWORD v
= GetVersion();
1249 GC_wnt
= !(v
& 0x80000000);
1250 GC_no_win32_dlls
|= ((!GC_wnt
) && (v
& 0xff) <= 3);
1253 /* Return the smallest address a such that VirtualQuery */
1254 /* returns correct results for all addresses between a and start. */
1255 /* Assumes VirtualQuery returns correct information for start. */
1256 ptr_t
GC_least_described_address(ptr_t start
)
1258 MEMORY_BASIC_INFORMATION buf
;
1264 limit
= GC_sysinfo
.lpMinimumApplicationAddress
;
1265 p
= (ptr_t
)((word
)start
& ~(GC_page_size
- 1));
1267 q
= (LPVOID
)(p
- GC_page_size
);
1268 if ((ptr_t
)q
> (ptr_t
)p
/* underflow */ || q
< limit
) break;
1269 result
= VirtualQuery(q
, &buf
, sizeof(buf
));
1270 if (result
!= sizeof(buf
) || buf
.AllocationBase
== 0) break;
1271 p
= (ptr_t
)(buf
.AllocationBase
);
1277 # ifndef REDIRECT_MALLOC
1278 /* We maintain a linked list of AllocationBase values that we know */
1279 /* correspond to malloc heap sections. Currently this is only called */
1280 /* during a GC. But there is some hope that for long running */
1281 /* programs we will eventually see most heap sections. */
1283 /* In the long run, it would be more reliable to occasionally walk */
1284 /* the malloc heap with HeapWalk on the default heap. But that */
1285 /* apparently works only for NT-based Windows. */
1287 /* In the long run, a better data structure would also be nice ... */
1288 struct GC_malloc_heap_list
{
1289 void * allocation_base
;
1290 struct GC_malloc_heap_list
*next
;
1291 } *GC_malloc_heap_l
= 0;
1293 /* Is p the base of one of the malloc heap sections we already know */
1295 GC_bool
GC_is_malloc_heap_base(ptr_t p
)
1297 struct GC_malloc_heap_list
*q
= GC_malloc_heap_l
;
1300 if (q
-> allocation_base
== p
) return TRUE
;
1306 void *GC_get_allocation_base(void *p
)
1308 MEMORY_BASIC_INFORMATION buf
;
1309 DWORD result
= VirtualQuery(p
, &buf
, sizeof(buf
));
1310 if (result
!= sizeof(buf
)) {
1311 ABORT("Weird VirtualQuery result");
1313 return buf
.AllocationBase
;
1316 size_t GC_max_root_size
= 100000; /* Appr. largest root size. */
1318 void GC_add_current_malloc_heap()
1320 struct GC_malloc_heap_list
*new_l
=
1321 malloc(sizeof(struct GC_malloc_heap_list
));
1322 void * candidate
= GC_get_allocation_base(new_l
);
1324 if (new_l
== 0) return;
1325 if (GC_is_malloc_heap_base(candidate
)) {
1326 /* Try a little harder to find malloc heap. */
1327 size_t req_size
= 10000;
1329 void *p
= malloc(req_size
);
1330 if (0 == p
) { free(new_l
); return; }
1331 candidate
= GC_get_allocation_base(p
);
1334 } while (GC_is_malloc_heap_base(candidate
)
1335 && req_size
< GC_max_root_size
/10 && req_size
< 500000);
1336 if (GC_is_malloc_heap_base(candidate
)) {
1337 free(new_l
); return;
1342 GC_printf1("Found new system malloc AllocationBase at 0x%lx\n",
1345 new_l
-> allocation_base
= candidate
;
1346 new_l
-> next
= GC_malloc_heap_l
;
1347 GC_malloc_heap_l
= new_l
;
1349 # endif /* REDIRECT_MALLOC */
1351 /* Is p the start of either the malloc heap, or of one of our */
1352 /* heap sections? */
1353 GC_bool
GC_is_heap_base (ptr_t p
)
1358 # ifndef REDIRECT_MALLOC
1359 static word last_gc_no
= -1;
1361 if (last_gc_no
!= GC_gc_no
) {
1362 GC_add_current_malloc_heap();
1363 last_gc_no
= GC_gc_no
;
1365 if (GC_root_size
> GC_max_root_size
) GC_max_root_size
= GC_root_size
;
1366 if (GC_is_malloc_heap_base(p
)) return TRUE
;
1368 for (i
= 0; i
< GC_n_heap_bases
; i
++) {
1369 if (GC_heap_bases
[i
] == p
) return TRUE
;
1375 void GC_register_root_section(ptr_t static_root
)
1377 MEMORY_BASIC_INFORMATION buf
;
1382 char * limit
, * new_limit
;
1384 if (!GC_no_win32_dlls
) return;
1385 p
= base
= limit
= GC_least_described_address(static_root
);
1386 while (p
< GC_sysinfo
.lpMaximumApplicationAddress
) {
1387 result
= VirtualQuery(p
, &buf
, sizeof(buf
));
1388 if (result
!= sizeof(buf
) || buf
.AllocationBase
== 0
1389 || GC_is_heap_base(buf
.AllocationBase
)) break;
1390 new_limit
= (char *)p
+ buf
.RegionSize
;
1391 protect
= buf
.Protect
;
1392 if (buf
.State
== MEM_COMMIT
1393 && is_writable(protect
)) {
1394 if ((char *)p
== limit
) {
1397 if (base
!= limit
) GC_add_roots_inner(base
, limit
, FALSE
);
1402 if (p
> (LPVOID
)new_limit
/* overflow */) break;
1403 p
= (LPVOID
)new_limit
;
1405 if (base
!= limit
) GC_add_roots_inner(base
, limit
, FALSE
);
1409 void GC_register_data_segments()
1413 GC_register_root_section((ptr_t
)(&dummy
));
1417 # else /* !OS2 && !Windows */
1419 # if (defined(SVR4) || defined(AUX) || defined(DGUX) \
1420 || (defined(LINUX) && defined(SPARC))) && !defined(PCR)
1421 ptr_t
GC_SysVGetDataStart(max_page_size
, etext_addr
)
1425 word text_end
= ((word
)(etext_addr
) + sizeof(word
) - 1)
1426 & ~(sizeof(word
) - 1);
1427 /* etext rounded to word boundary */
1428 word next_page
= ((text_end
+ (word
)max_page_size
- 1)
1429 & ~((word
)max_page_size
- 1));
1430 word page_offset
= (text_end
& ((word
)max_page_size
- 1));
1431 VOLATILE
char * result
= (char *)(next_page
+ page_offset
);
1432 /* Note that this isnt equivalent to just adding */
1433 /* max_page_size to &etext if &etext is at a page boundary */
1435 GC_setup_temporary_fault_handler();
1436 if (SETJMP(GC_jmp_buf
) == 0) {
1437 /* Try writing to the address. */
1439 GC_reset_fault_handler();
1441 GC_reset_fault_handler();
1442 /* We got here via a longjmp. The address is not readable. */
1443 /* This is known to happen under Solaris 2.4 + gcc, which place */
1444 /* string constants in the text segment, but after etext. */
1445 /* Use plan B. Note that we now know there is a gap between */
1446 /* text and data segments, so plan A bought us something. */
1447 result
= (char *)GC_find_limit((ptr_t
)(DATAEND
), FALSE
);
1449 return((ptr_t
)result
);
1453 # if defined(FREEBSD) && (defined(I386) || defined(X86_64) || defined(powerpc) || defined(__powerpc__)) && !defined(PCR)
1454 /* Its unclear whether this should be identical to the above, or */
1455 /* whether it should apply to non-X86 architectures. */
1456 /* For now we don't assume that there is always an empty page after */
1457 /* etext. But in some cases there actually seems to be slightly more. */
1458 /* This also deals with holes between read-only data and writable data. */
1459 ptr_t
GC_FreeBSDGetDataStart(max_page_size
, etext_addr
)
1463 word text_end
= ((word
)(etext_addr
) + sizeof(word
) - 1)
1464 & ~(sizeof(word
) - 1);
1465 /* etext rounded to word boundary */
1466 VOLATILE word next_page
= (text_end
+ (word
)max_page_size
- 1)
1467 & ~((word
)max_page_size
- 1);
1468 VOLATILE ptr_t result
= (ptr_t
)text_end
;
1469 GC_setup_temporary_fault_handler();
1470 if (SETJMP(GC_jmp_buf
) == 0) {
1471 /* Try reading at the address. */
1472 /* This should happen before there is another thread. */
1473 for (; next_page
< (word
)(DATAEND
); next_page
+= (word
)max_page_size
)
1474 *(VOLATILE
char *)next_page
;
1475 GC_reset_fault_handler();
1477 GC_reset_fault_handler();
1478 /* As above, we go to plan B */
1479 result
= GC_find_limit((ptr_t
)(DATAEND
), FALSE
);
1489 # define GC_AMIGA_DS
1490 # include "AmigaOS.c"
1493 #else /* !OS2 && !Windows && !AMIGA */
1495 void GC_register_data_segments()
1497 # if !defined(PCR) && !defined(SRC_M3) && !defined(MACOS)
1498 # if defined(REDIRECT_MALLOC) && defined(GC_SOLARIS_THREADS)
1499 /* As of Solaris 2.3, the Solaris threads implementation */
1500 /* allocates the data structure for the initial thread with */
1501 /* sbrk at process startup. It needs to be scanned, so that */
1502 /* we don't lose some malloc allocated data structures */
1503 /* hanging from it. We're on thin ice here ... */
1504 extern caddr_t
sbrk();
1506 GC_add_roots_inner(DATASTART
, (char *)sbrk(0), FALSE
);
1508 GC_add_roots_inner(DATASTART
, (char *)(DATAEND
), FALSE
);
1509 # if defined(DATASTART2)
1510 GC_add_roots_inner(DATASTART2
, (char *)(DATAEND2
), FALSE
);
1516 # if defined(THINK_C)
1517 extern void* GC_MacGetDataStart(void);
1518 /* globals begin above stack and end at a5. */
1519 GC_add_roots_inner((ptr_t
)GC_MacGetDataStart(),
1520 (ptr_t
)LMGetCurrentA5(), FALSE
);
1522 # if defined(__MWERKS__)
1524 extern void* GC_MacGetDataStart(void);
1525 /* MATTHEW: Function to handle Far Globals (CW Pro 3) */
1526 # if __option(far_data)
1527 extern void* GC_MacGetDataEnd(void);
1529 /* globals begin above stack and end at a5. */
1530 GC_add_roots_inner((ptr_t
)GC_MacGetDataStart(),
1531 (ptr_t
)LMGetCurrentA5(), FALSE
);
1532 /* MATTHEW: Handle Far Globals */
1533 # if __option(far_data)
1534 /* Far globals follow he QD globals: */
1535 GC_add_roots_inner((ptr_t
)LMGetCurrentA5(),
1536 (ptr_t
)GC_MacGetDataEnd(), FALSE
);
1539 extern char __data_start__
[], __data_end__
[];
1540 GC_add_roots_inner((ptr_t
)&__data_start__
,
1541 (ptr_t
)&__data_end__
, FALSE
);
1542 # endif /* __POWERPC__ */
1543 # endif /* __MWERKS__ */
1544 # endif /* !THINK_C */
1548 /* Dynamic libraries are added at every collection, since they may */
1552 # endif /* ! AMIGA */
1553 # endif /* ! MSWIN32 && ! MSWINCE*/
1557 * Auxiliary routines for obtaining memory from OS.
1560 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) \
1561 && !defined(MSWIN32) && !defined(MSWINCE) \
1562 && !defined(MACOS) && !defined(DOS4GW)
1565 extern caddr_t
sbrk();
1568 # define SBRK_ARG_T ptrdiff_t
1570 # define SBRK_ARG_T int
1574 # if 0 && defined(RS6000) /* We now use mmap */
1575 /* The compiler seems to generate speculative reads one past the end of */
1576 /* an allocated object. Hence we need to make sure that the page */
1577 /* following the last heap page is also mapped. */
1578 ptr_t
GC_unix_get_mem(bytes
)
1581 caddr_t cur_brk
= (caddr_t
)sbrk(0);
1583 SBRK_ARG_T lsbs
= (word
)cur_brk
& (GC_page_size
-1);
1584 static caddr_t my_brk_val
= 0;
1586 if ((SBRK_ARG_T
)bytes
< 0) return(0); /* too big */
1588 if((caddr_t
)(sbrk(GC_page_size
- lsbs
)) == (caddr_t
)(-1)) return(0);
1590 if (cur_brk
== my_brk_val
) {
1591 /* Use the extra block we allocated last time. */
1592 result
= (ptr_t
)sbrk((SBRK_ARG_T
)bytes
);
1593 if (result
== (caddr_t
)(-1)) return(0);
1594 result
-= GC_page_size
;
1596 result
= (ptr_t
)sbrk(GC_page_size
+ (SBRK_ARG_T
)bytes
);
1597 if (result
== (caddr_t
)(-1)) return(0);
1599 my_brk_val
= result
+ bytes
+ GC_page_size
; /* Always page aligned */
1600 return((ptr_t
)result
);
1603 #else /* Not RS6000 */
1605 #if defined(USE_MMAP) || defined(USE_MUNMAP)
1607 #ifdef USE_MMAP_FIXED
1608 # define GC_MMAP_FLAGS MAP_FIXED | MAP_PRIVATE
1609 /* Seems to yield better performance on Solaris 2, but can */
1610 /* be unreliable if something is already mapped at the address. */
1612 # define GC_MMAP_FLAGS MAP_PRIVATE
1615 #ifdef USE_MMAP_ANON
1617 # if defined(MAP_ANONYMOUS)
1618 # define OPT_MAP_ANON MAP_ANONYMOUS
1620 # define OPT_MAP_ANON MAP_ANON
1624 # define OPT_MAP_ANON 0
1627 #endif /* defined(USE_MMAP) || defined(USE_MUNMAP) */
1629 #if defined(USE_MMAP)
1630 /* Tested only under Linux, IRIX5 and Solaris 2 */
1633 # define HEAP_START 0
1636 ptr_t
GC_unix_get_mem(bytes
)
1640 static ptr_t last_addr
= HEAP_START
;
1642 # ifndef USE_MMAP_ANON
1643 static GC_bool initialized
= FALSE
;
1646 zero_fd
= open("/dev/zero", O_RDONLY
);
1647 fcntl(zero_fd
, F_SETFD
, FD_CLOEXEC
);
1652 if (bytes
& (GC_page_size
-1)) ABORT("Bad GET_MEM arg");
1653 result
= mmap(last_addr
, bytes
, PROT_READ
| PROT_WRITE
| OPT_PROT_EXEC
,
1654 GC_MMAP_FLAGS
| OPT_MAP_ANON
, zero_fd
, 0/* offset */);
1655 if (result
== MAP_FAILED
) return(0);
1656 last_addr
= (ptr_t
)result
+ bytes
+ GC_page_size
- 1;
1657 last_addr
= (ptr_t
)((word
)last_addr
& ~(GC_page_size
- 1));
1658 # if !defined(LINUX)
1659 if (last_addr
== 0) {
1660 /* Oops. We got the end of the address space. This isn't */
1661 /* usable by arbitrary C code, since one-past-end pointers */
1662 /* don't work, so we discard it and try again. */
1663 munmap(result
, (size_t)(-GC_page_size
) - (size_t)result
);
1664 /* Leave last page mapped, so we can't repeat. */
1665 return GC_unix_get_mem(bytes
);
1668 GC_ASSERT(last_addr
!= 0);
1670 return((ptr_t
)result
);
1673 #else /* Not RS6000, not USE_MMAP */
1674 ptr_t
GC_unix_get_mem(bytes
)
1679 /* Bare sbrk isn't thread safe. Play by malloc rules. */
1680 /* The equivalent may be needed on other systems as well. */
1684 ptr_t cur_brk
= (ptr_t
)sbrk(0);
1685 SBRK_ARG_T lsbs
= (word
)cur_brk
& (GC_page_size
-1);
1687 if ((SBRK_ARG_T
)bytes
< 0) return(0); /* too big */
1689 if((ptr_t
)sbrk(GC_page_size
- lsbs
) == (ptr_t
)(-1)) return(0);
1691 result
= (ptr_t
)sbrk((SBRK_ARG_T
)bytes
);
1692 if (result
== (ptr_t
)(-1)) result
= 0;
1700 #endif /* Not USE_MMAP */
1701 #endif /* Not RS6000 */
1707 void * os2_alloc(size_t bytes
)
1711 if (DosAllocMem(&result
, bytes
, PAG_EXECUTE
| PAG_READ
|
1712 PAG_WRITE
| PAG_COMMIT
)
1716 if (result
== 0) return(os2_alloc(bytes
));
1723 # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
1724 SYSTEM_INFO GC_sysinfo
;
1727 # if defined(MSWIN32) || defined(CYGWIN32)
1729 word GC_n_heap_bases
= 0;
1731 # ifdef USE_GLOBAL_ALLOC
1732 # define GLOBAL_ALLOC_TEST 1
1734 # define GLOBAL_ALLOC_TEST GC_no_win32_dlls
1737 ptr_t
GC_win32_get_mem(bytes
)
1743 result
= GC_unix_get_mem (bytes
);
1745 if (GLOBAL_ALLOC_TEST
) {
1746 /* VirtualAlloc doesn't like PAGE_EXECUTE_READWRITE. */
1747 /* There are also unconfirmed rumors of other */
1748 /* problems, so we dodge the issue. */
1749 result
= (ptr_t
) GlobalAlloc(0, bytes
+ HBLKSIZE
);
1750 result
= (ptr_t
)(((word
)result
+ HBLKSIZE
) & ~(HBLKSIZE
-1));
1752 /* VirtualProtect only works on regions returned by a */
1753 /* single VirtualAlloc call. Thus we allocate one */
1754 /* extra page, which will prevent merging of blocks */
1755 /* in separate regions, and eliminate any temptation */
1756 /* to call VirtualProtect on a range spanning regions. */
1757 /* This wastes a small amount of memory, and risks */
1758 /* increased fragmentation. But better alternatives */
1759 /* would require effort. */
1760 result
= (ptr_t
) VirtualAlloc(NULL
, bytes
+ 1,
1761 MEM_COMMIT
| MEM_RESERVE
,
1762 PAGE_EXECUTE_READWRITE
);
1765 if (HBLKDISPL(result
) != 0) ABORT("Bad VirtualAlloc result");
1766 /* If I read the documentation correctly, this can */
1767 /* only happen if HBLKSIZE > 64k or not a power of 2. */
1768 if (GC_n_heap_bases
>= MAX_HEAP_SECTS
) ABORT("Too many heap sections");
1769 GC_heap_bases
[GC_n_heap_bases
++] = result
;
1773 void GC_win32_free_heap ()
1775 if (GC_no_win32_dlls
) {
1776 while (GC_n_heap_bases
> 0) {
1778 free (GC_heap_bases
[--GC_n_heap_bases
]);
1780 GlobalFree (GC_heap_bases
[--GC_n_heap_bases
]);
1782 GC_heap_bases
[GC_n_heap_bases
] = 0;
1789 # define GC_AMIGA_AM
1790 # include "AmigaOS.c"
1796 word GC_n_heap_bases
= 0;
1798 ptr_t
GC_wince_get_mem(bytes
)
1804 /* Round up allocation size to multiple of page size */
1805 bytes
= (bytes
+ GC_page_size
-1) & ~(GC_page_size
-1);
1807 /* Try to find reserved, uncommitted pages */
1808 for (i
= 0; i
< GC_n_heap_bases
; i
++) {
1809 if (((word
)(-(signed_word
)GC_heap_lengths
[i
])
1810 & (GC_sysinfo
.dwAllocationGranularity
-1))
1812 result
= GC_heap_bases
[i
] + GC_heap_lengths
[i
];
1817 if (i
== GC_n_heap_bases
) {
1818 /* Reserve more pages */
1819 word res_bytes
= (bytes
+ GC_sysinfo
.dwAllocationGranularity
-1)
1820 & ~(GC_sysinfo
.dwAllocationGranularity
-1);
1821 /* If we ever support MPROTECT_VDB here, we will probably need to */
1822 /* ensure that res_bytes is strictly > bytes, so that VirtualProtect */
1823 /* never spans regions. It seems to be OK for a VirtualFree argument */
1824 /* to span regions, so we should be OK for now. */
1825 result
= (ptr_t
) VirtualAlloc(NULL
, res_bytes
,
1826 MEM_RESERVE
| MEM_TOP_DOWN
,
1827 PAGE_EXECUTE_READWRITE
);
1828 if (HBLKDISPL(result
) != 0) ABORT("Bad VirtualAlloc result");
1829 /* If I read the documentation correctly, this can */
1830 /* only happen if HBLKSIZE > 64k or not a power of 2. */
1831 if (GC_n_heap_bases
>= MAX_HEAP_SECTS
) ABORT("Too many heap sections");
1832 GC_heap_bases
[GC_n_heap_bases
] = result
;
1833 GC_heap_lengths
[GC_n_heap_bases
] = 0;
1838 result
= (ptr_t
) VirtualAlloc(result
, bytes
,
1840 PAGE_EXECUTE_READWRITE
);
1841 if (result
!= NULL
) {
1842 if (HBLKDISPL(result
) != 0) ABORT("Bad VirtualAlloc result");
1843 GC_heap_lengths
[i
] += bytes
;
1852 /* For now, this only works on Win32/WinCE and some Unix-like */
1853 /* systems. If you have something else, don't define */
1855 /* We assume ANSI C to support this feature. */
1857 #if !defined(MSWIN32) && !defined(MSWINCE)
1860 #include <sys/mman.h>
1861 #include <sys/stat.h>
1862 #include <sys/types.h>
1866 /* Compute a page aligned starting address for the unmap */
1867 /* operation on a block of size bytes starting at start. */
1868 /* Return 0 if the block is too small to make this feasible. */
1869 ptr_t
GC_unmap_start(ptr_t start
, word bytes
)
1871 ptr_t result
= start
;
1872 /* Round start to next page boundary. */
1873 result
+= GC_page_size
- 1;
1874 result
= (ptr_t
)((word
)result
& ~(GC_page_size
- 1));
1875 if (result
+ GC_page_size
> start
+ bytes
) return 0;
1879 /* Compute end address for an unmap operation on the indicated */
1881 ptr_t
GC_unmap_end(ptr_t start
, word bytes
)
1883 ptr_t end_addr
= start
+ bytes
;
1884 end_addr
= (ptr_t
)((word
)end_addr
& ~(GC_page_size
- 1));
1888 /* Under Win32/WinCE we commit (map) and decommit (unmap) */
1889 /* memory using VirtualAlloc and VirtualFree. These functions */
1890 /* work on individual allocations of virtual memory, made */
1891 /* previously using VirtualAlloc with the MEM_RESERVE flag. */
1892 /* The ranges we need to (de)commit may span several of these */
1893 /* allocations; therefore we use VirtualQuery to check */
1894 /* allocation lengths, and split up the range as necessary. */
1896 /* We assume that GC_remap is called on exactly the same range */
1897 /* as a previous call to GC_unmap. It is safe to consistently */
1898 /* round the endpoints in both places. */
1899 void GC_unmap(ptr_t start
, word bytes
)
1901 ptr_t start_addr
= GC_unmap_start(start
, bytes
);
1902 ptr_t end_addr
= GC_unmap_end(start
, bytes
);
1903 word len
= end_addr
- start_addr
;
1904 if (0 == start_addr
) return;
1905 # if defined(MSWIN32) || defined(MSWINCE)
1907 MEMORY_BASIC_INFORMATION mem_info
;
1909 if (VirtualQuery(start_addr
, &mem_info
, sizeof(mem_info
))
1910 != sizeof(mem_info
))
1911 ABORT("Weird VirtualQuery result");
1912 free_len
= (len
< mem_info
.RegionSize
) ? len
: mem_info
.RegionSize
;
1913 if (!VirtualFree(start_addr
, free_len
, MEM_DECOMMIT
))
1914 ABORT("VirtualFree failed");
1915 GC_unmapped_bytes
+= free_len
;
1916 start_addr
+= free_len
;
1920 /* We immediately remap it to prevent an intervening mmap from */
1921 /* accidentally grabbing the same address space. */
1924 result
= mmap(start_addr
, len
, PROT_NONE
,
1925 MAP_PRIVATE
| MAP_FIXED
| OPT_MAP_ANON
,
1926 zero_fd
, 0/* offset */);
1927 if (result
!= (void *)start_addr
) ABORT("mmap(...PROT_NONE...) failed");
1929 GC_unmapped_bytes
+= len
;
1934 void GC_remap(ptr_t start
, word bytes
)
1936 ptr_t start_addr
= GC_unmap_start(start
, bytes
);
1937 ptr_t end_addr
= GC_unmap_end(start
, bytes
);
1938 word len
= end_addr
- start_addr
;
1940 # if defined(MSWIN32) || defined(MSWINCE)
1943 if (0 == start_addr
) return;
1945 MEMORY_BASIC_INFORMATION mem_info
;
1947 if (VirtualQuery(start_addr
, &mem_info
, sizeof(mem_info
))
1948 != sizeof(mem_info
))
1949 ABORT("Weird VirtualQuery result");
1950 alloc_len
= (len
< mem_info
.RegionSize
) ? len
: mem_info
.RegionSize
;
1951 result
= VirtualAlloc(start_addr
, alloc_len
,
1953 PAGE_EXECUTE_READWRITE
);
1954 if (result
!= start_addr
) {
1955 ABORT("VirtualAlloc remapping failed");
1957 GC_unmapped_bytes
-= alloc_len
;
1958 start_addr
+= alloc_len
;
1962 /* It was already remapped with PROT_NONE. */
1965 if (0 == start_addr
) return;
1966 result
= mprotect(start_addr
, len
,
1967 PROT_READ
| PROT_WRITE
| OPT_PROT_EXEC
);
1970 "Mprotect failed at 0x%lx (length %ld) with errno %ld\n",
1971 start_addr
, len
, errno
);
1972 ABORT("Mprotect remapping failed");
1974 GC_unmapped_bytes
-= len
;
1978 /* Two adjacent blocks have already been unmapped and are about to */
1979 /* be merged. Unmap the whole block. This typically requires */
1980 /* that we unmap a small section in the middle that was not previously */
1981 /* unmapped due to alignment constraints. */
1982 void GC_unmap_gap(ptr_t start1
, word bytes1
, ptr_t start2
, word bytes2
)
1984 ptr_t start1_addr
= GC_unmap_start(start1
, bytes1
);
1985 ptr_t end1_addr
= GC_unmap_end(start1
, bytes1
);
1986 ptr_t start2_addr
= GC_unmap_start(start2
, bytes2
);
1987 ptr_t end2_addr
= GC_unmap_end(start2
, bytes2
);
1988 ptr_t start_addr
= end1_addr
;
1989 ptr_t end_addr
= start2_addr
;
1991 GC_ASSERT(start1
+ bytes1
== start2
);
1992 if (0 == start1_addr
) start_addr
= GC_unmap_start(start1
, bytes1
+ bytes2
);
1993 if (0 == start2_addr
) end_addr
= GC_unmap_end(start1
, bytes1
+ bytes2
);
1994 if (0 == start_addr
) return;
1995 len
= end_addr
- start_addr
;
1996 # if defined(MSWIN32) || defined(MSWINCE)
1998 MEMORY_BASIC_INFORMATION mem_info
;
2000 if (VirtualQuery(start_addr
, &mem_info
, sizeof(mem_info
))
2001 != sizeof(mem_info
))
2002 ABORT("Weird VirtualQuery result");
2003 free_len
= (len
< mem_info
.RegionSize
) ? len
: mem_info
.RegionSize
;
2004 if (!VirtualFree(start_addr
, free_len
, MEM_DECOMMIT
))
2005 ABORT("VirtualFree failed");
2006 GC_unmapped_bytes
+= free_len
;
2007 start_addr
+= free_len
;
2011 if (len
!= 0 && munmap(start_addr
, len
) != 0) ABORT("munmap failed");
2012 GC_unmapped_bytes
+= len
;
2016 #endif /* USE_MUNMAP */
2018 /* Routine for pushing any additional roots. In THREADS */
2019 /* environment, this is also responsible for marking from */
2020 /* thread stacks. */
2022 void (*GC_push_other_roots
)() = 0;
2026 PCR_ERes
GC_push_thread_stack(PCR_Th_T
*t
, PCR_Any dummy
)
2028 struct PCR_ThCtl_TInfoRep info
;
2031 info
.ti_stkLow
= info
.ti_stkHi
= 0;
2032 result
= PCR_ThCtl_GetInfo(t
, &info
);
2033 GC_push_all_stack((ptr_t
)(info
.ti_stkLow
), (ptr_t
)(info
.ti_stkHi
));
2037 /* Push the contents of an old object. We treat this as stack */
2038 /* data only becasue that makes it robust against mark stack */
2040 PCR_ERes
GC_push_old_obj(void *p
, size_t size
, PCR_Any data
)
2042 GC_push_all_stack((ptr_t
)p
, (ptr_t
)p
+ size
);
2043 return(PCR_ERes_okay
);
2047 void GC_default_push_other_roots
GC_PROTO((void))
2049 /* Traverse data allocated by previous memory managers. */
2051 extern struct PCR_MM_ProcsRep
* GC_old_allocator
;
2053 if ((*(GC_old_allocator
->mmp_enumerate
))(PCR_Bool_false
,
2056 ABORT("Old object enumeration failed");
2059 /* Traverse all thread stacks. */
2061 PCR_ThCtl_ApplyToAllOtherThreads(GC_push_thread_stack
,0))
2062 || PCR_ERes_IsErr(GC_push_thread_stack(PCR_Th_CurrThread(), 0))) {
2063 ABORT("Thread stack marking failed\n");
2071 # ifdef ALL_INTERIOR_POINTERS
2075 void GC_push_thread_structures
GC_PROTO((void))
2077 /* Not our responsibibility. */
2080 extern void ThreadF__ProcessStacks();
2082 void GC_push_thread_stack(start
, stop
)
2085 GC_push_all_stack((ptr_t
)start
, (ptr_t
)stop
+ sizeof(word
));
2088 /* Push routine with M3 specific calling convention. */
2089 GC_m3_push_root(dummy1
, p
, dummy2
, dummy3
)
2091 ptr_t dummy1
, dummy2
;
2096 GC_PUSH_ONE_STACK(q
, p
);
2099 /* M3 set equivalent to RTHeap.TracedRefTypes */
2100 typedef struct { int elts
[1]; } RefTypeSet
;
2101 RefTypeSet GC_TracedRefTypes
= {{0x1}};
2103 void GC_default_push_other_roots
GC_PROTO((void))
2105 /* Use the M3 provided routine for finding static roots. */
2106 /* This is a bit dubious, since it presumes no C roots. */
2107 /* We handle the collector roots explicitly in GC_push_roots */
2108 RTMain__GlobalMapProc(GC_m3_push_root
, 0, GC_TracedRefTypes
);
2109 if (GC_words_allocd
> 0) {
2110 ThreadF__ProcessStacks(GC_push_thread_stack
);
2112 /* Otherwise this isn't absolutely necessary, and we have */
2113 /* startup ordering problems. */
2116 # endif /* SRC_M3 */
2118 # if defined(GC_SOLARIS_THREADS) || defined(GC_PTHREADS) || \
2119 defined(GC_WIN32_THREADS)
2121 extern void GC_push_all_stacks();
2123 void GC_default_push_other_roots
GC_PROTO((void))
2125 GC_push_all_stacks();
2128 # endif /* GC_SOLARIS_THREADS || GC_PTHREADS */
2130 void (*GC_push_other_roots
) GC_PROTO((void)) = GC_default_push_other_roots
;
2132 #endif /* THREADS */
2135 * Routines for accessing dirty bits on virtual pages.
2136 * We plan to eventually implement four strategies for doing so:
2137 * DEFAULT_VDB: A simple dummy implementation that treats every page
2138 * as possibly dirty. This makes incremental collection
2139 * useless, but the implementation is still correct.
2140 * PCR_VDB: Use PPCRs virtual dirty bit facility.
2141 * PROC_VDB: Use the /proc facility for reading dirty bits. Only
2142 * works under some SVR4 variants. Even then, it may be
2143 * too slow to be entirely satisfactory. Requires reading
2144 * dirty bits for entire address space. Implementations tend
2145 * to assume that the client is a (slow) debugger.
2146 * MPROTECT_VDB:Protect pages and then catch the faults to keep track of
2147 * dirtied pages. The implementation (and implementability)
2148 * is highly system dependent. This usually fails when system
2149 * calls write to a protected page. We prevent the read system
2150 * call from doing so. It is the clients responsibility to
2151 * make sure that other system calls are similarly protected
2152 * or write only to the stack.
2154 GC_bool GC_dirty_maintained
= FALSE
;
2158 /* All of the following assume the allocation lock is held, and */
2159 /* signals are disabled. */
2161 /* The client asserts that unallocated pages in the heap are never */
2164 /* Initialize virtual dirty bit implementation. */
2165 void GC_dirty_init()
2168 GC_printf0("Initializing DEFAULT_VDB...\n");
2170 GC_dirty_maintained
= TRUE
;
2173 /* Retrieve system dirty bits for heap to a local buffer. */
2174 /* Restore the systems notion of which pages are dirty. */
2175 void GC_read_dirty()
2178 /* Is the HBLKSIZE sized page at h marked dirty in the local buffer? */
2179 /* If the actual page size is different, this returns TRUE if any */
2180 /* of the pages overlapping h are dirty. This routine may err on the */
2181 /* side of labelling pages as dirty (and this implementation does). */
2183 GC_bool
GC_page_was_dirty(h
)
2190 * The following two routines are typically less crucial. They matter
2191 * most with large dynamic libraries, or if we can't accurately identify
2192 * stacks, e.g. under Solaris 2.X. Otherwise the following default
2193 * versions are adequate.
2196 /* Could any valid GC heap pointer ever have been written to this page? */
2198 GC_bool
GC_page_was_ever_dirty(h
)
2204 /* Reset the n pages starting at h to "was never dirty" status. */
2205 void GC_is_fresh(h
, n
)
2212 /* I) hints that [h, h+nblocks) is about to be written. */
2213 /* II) guarantees that protection is removed. */
2214 /* (I) may speed up some dirty bit implementations. */
2215 /* (II) may be essential if we need to ensure that */
2216 /* pointer-free system call buffers in the heap are */
2217 /* not protected. */
2219 void GC_remove_protection(h
, nblocks
, is_ptrfree
)
2226 # endif /* DEFAULT_VDB */
2229 # ifdef MPROTECT_VDB
2232 * See DEFAULT_VDB for interface descriptions.
2236 * This implementation maintains dirty bits itself by catching write
2237 * faults and keeping track of them. We assume nobody else catches
2238 * SIGBUS or SIGSEGV. We assume no write faults occur in system calls.
2239 * This means that clients must ensure that system calls don't write
2240 * to the write-protected heap. Probably the best way to do this is to
2241 * ensure that system calls write at most to POINTERFREE objects in the
2242 * heap, and do even that only if we are on a platform on which those
2243 * are not protected. Another alternative is to wrap system calls
2244 * (see example for read below), but the current implementation holds
2245 * a lock across blocking calls, making it problematic for multithreaded
2247 * We assume the page size is a multiple of HBLKSIZE.
2248 * We prefer them to be the same. We avoid protecting POINTERFREE
2249 * objects only if they are the same.
2252 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(DARWIN)
2254 # include <sys/mman.h>
2255 # include <signal.h>
2256 # include <sys/syscall.h>
2258 # define PROTECT(addr, len) \
2259 if (mprotect((caddr_t)(addr), (size_t)(len), \
2260 PROT_READ | OPT_PROT_EXEC) < 0) { \
2261 ABORT("mprotect failed"); \
2263 # define UNPROTECT(addr, len) \
2264 if (mprotect((caddr_t)(addr), (size_t)(len), \
2265 PROT_WRITE | PROT_READ | OPT_PROT_EXEC ) < 0) { \
2266 ABORT("un-mprotect failed"); \
2272 /* Using vm_protect (mach syscall) over mprotect (BSD syscall) seems to
2273 decrease the likelihood of some of the problems described below. */
2274 #include <mach/vm_map.h>
2275 static mach_port_t GC_task_self
;
2276 #define PROTECT(addr,len) \
2277 if(vm_protect(GC_task_self,(vm_address_t)(addr),(vm_size_t)(len), \
2278 FALSE,VM_PROT_READ) != KERN_SUCCESS) { \
2279 ABORT("vm_portect failed"); \
2281 #define UNPROTECT(addr,len) \
2282 if(vm_protect(GC_task_self,(vm_address_t)(addr),(vm_size_t)(len), \
2283 FALSE,VM_PROT_READ|VM_PROT_WRITE) != KERN_SUCCESS) { \
2284 ABORT("vm_portect failed"); \
2289 # include <signal.h>
2292 static DWORD protect_junk
;
2293 # define PROTECT(addr, len) \
2294 if (!VirtualProtect((addr), (len), PAGE_EXECUTE_READ, \
2296 DWORD last_error = GetLastError(); \
2297 GC_printf1("Last error code: %lx\n", last_error); \
2298 ABORT("VirtualProtect failed"); \
2300 # define UNPROTECT(addr, len) \
2301 if (!VirtualProtect((addr), (len), PAGE_EXECUTE_READWRITE, \
2303 ABORT("un-VirtualProtect failed"); \
2305 # endif /* !DARWIN */
2306 # endif /* MSWIN32 || MSWINCE || DARWIN */
2308 #if defined(SUNOS4) || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2309 typedef void (* SIG_PF
)();
2310 #endif /* SUNOS4 || (FREEBSD && !SUNOS5SIGS) */
2312 #if defined(SUNOS5SIGS) || defined(OSF1) || defined(LINUX) \
2315 typedef void (* SIG_PF
)(int);
2317 typedef void (* SIG_PF
)();
2319 #endif /* SUNOS5SIGS || OSF1 || LINUX || HURD */
2321 #if defined(MSWIN32)
2322 typedef LPTOP_LEVEL_EXCEPTION_FILTER SIG_PF
;
2324 # define SIG_DFL (LPTOP_LEVEL_EXCEPTION_FILTER) (-1)
2326 #if defined(MSWINCE)
2327 typedef LONG (WINAPI
*SIG_PF
)(struct _EXCEPTION_POINTERS
*);
2329 # define SIG_DFL (SIG_PF) (-1)
2332 #if defined(IRIX5) || defined(OSF1) || defined(HURD)
2333 typedef void (* REAL_SIG_PF
)(int, int, struct sigcontext
*);
2334 #endif /* IRIX5 || OSF1 || HURD */
2336 #if defined(SUNOS5SIGS)
2337 # if defined(HPUX) || defined(FREEBSD)
2338 # define SIGINFO_T siginfo_t
2340 # define SIGINFO_T struct siginfo
2343 typedef void (* REAL_SIG_PF
)(int, SIGINFO_T
*, void *);
2345 typedef void (* REAL_SIG_PF
)();
2347 #endif /* SUNOS5SIGS */
2350 # if __GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 2
2351 typedef struct sigcontext s_c
;
2352 # else /* glibc < 2.2 */
2353 # include <linux/version.h>
2354 # if (LINUX_VERSION_CODE >= 0x20100) && !defined(M68K) || defined(ALPHA) || defined(ARM32)
2355 typedef struct sigcontext s_c
;
2357 typedef struct sigcontext_struct s_c
;
2359 # endif /* glibc < 2.2 */
2360 # if defined(ALPHA) || defined(M68K)
2361 typedef void (* REAL_SIG_PF
)(int, int, s_c
*);
2363 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2364 typedef void (* REAL_SIG_PF
)(int, siginfo_t
*, s_c
*);
2366 /* According to SUSV3, the last argument should have type */
2367 /* void * or ucontext_t * */
2369 typedef void (* REAL_SIG_PF
)(int, s_c
);
2373 /* Retrieve fault address from sigcontext structure by decoding */
2375 char * get_fault_addr(s_c
*sc
) {
2379 instr
= *((unsigned *)(sc
->sc_pc
));
2380 faultaddr
= sc
->sc_regs
[(instr
>> 16) & 0x1f];
2381 faultaddr
+= (word
) (((int)instr
<< 16) >> 16);
2382 return (char *)faultaddr
;
2384 # endif /* !ALPHA */
2388 SIG_PF GC_old_bus_handler
;
2389 SIG_PF GC_old_segv_handler
; /* Also old MSWIN32 ACCESS_VIOLATION filter */
2390 #endif /* !DARWIN */
2392 #if defined(THREADS)
2393 /* We need to lock around the bitmap update in the write fault handler */
2394 /* in order to avoid the risk of losing a bit. We do this with a */
2395 /* test-and-set spin lock if we know how to do that. Otherwise we */
2396 /* check whether we are already in the handler and use the dumb but */
2397 /* safe fallback algorithm of setting all bits in the word. */
2398 /* Contention should be very rare, so we do the minimum to handle it */
2400 #ifdef GC_TEST_AND_SET_DEFINED
2401 static VOLATILE
unsigned int fault_handler_lock
= 0;
2402 void async_set_pht_entry_from_index(VOLATILE page_hash_table db
, int index
) {
2403 while (GC_test_and_set(&fault_handler_lock
)) {}
2404 /* Could also revert to set_pht_entry_from_index_safe if initial */
2405 /* GC_test_and_set fails. */
2406 set_pht_entry_from_index(db
, index
);
2407 GC_clear(&fault_handler_lock
);
2409 #else /* !GC_TEST_AND_SET_DEFINED */
2410 /* THIS IS INCORRECT! The dirty bit vector may be temporarily wrong, */
2411 /* just before we notice the conflict and correct it. We may end up */
2412 /* looking at it while it's wrong. But this requires contention */
2413 /* exactly when a GC is triggered, which seems far less likely to */
2414 /* fail than the old code, which had no reported failures. Thus we */
2415 /* leave it this way while we think of something better, or support */
2416 /* GC_test_and_set on the remaining platforms. */
2417 static VOLATILE word currently_updating
= 0;
2418 void async_set_pht_entry_from_index(VOLATILE page_hash_table db
, int index
) {
2419 unsigned int update_dummy
;
2420 currently_updating
= (word
)(&update_dummy
);
2421 set_pht_entry_from_index(db
, index
);
2422 /* If we get contention in the 10 or so instruction window here, */
2423 /* and we get stopped by a GC between the two updates, we lose! */
2424 if (currently_updating
!= (word
)(&update_dummy
)) {
2425 set_pht_entry_from_index_safe(db
, index
);
2426 /* We claim that if two threads concurrently try to update the */
2427 /* dirty bit vector, the first one to execute UPDATE_START */
2428 /* will see it changed when UPDATE_END is executed. (Note that */
2429 /* &update_dummy must differ in two distinct threads.) It */
2430 /* will then execute set_pht_entry_from_index_safe, thus */
2431 /* returning us to a safe state, though not soon enough. */
2434 #endif /* !GC_TEST_AND_SET_DEFINED */
2435 #else /* !THREADS */
2436 # define async_set_pht_entry_from_index(db, index) \
2437 set_pht_entry_from_index(db, index)
2438 #endif /* !THREADS */
2441 #if !defined(DARWIN)
2442 # if defined (SUNOS4) || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2443 void GC_write_fault_handler(sig
, code
, scp
, addr
)
2445 struct sigcontext
*scp
;
2448 # define SIG_OK (sig == SIGSEGV || sig == SIGBUS)
2449 # define CODE_OK (FC_CODE(code) == FC_PROT \
2450 || (FC_CODE(code) == FC_OBJERR \
2451 && FC_ERRNO(code) == FC_PROT))
2454 # define SIG_OK (sig == SIGBUS)
2455 # define CODE_OK TRUE
2457 # endif /* SUNOS4 || (FREEBSD && !SUNOS5SIGS) */
2459 # if defined(IRIX5) || defined(OSF1) || defined(HURD)
2461 void GC_write_fault_handler(int sig
, int code
, struct sigcontext
*scp
)
2463 # define SIG_OK (sig == SIGSEGV)
2464 # define CODE_OK (code == 2 /* experimentally determined */)
2467 # define SIG_OK (sig == SIGSEGV)
2468 # define CODE_OK (code == EACCES)
2471 # define SIG_OK (sig == SIGBUS || sig == SIGSEGV)
2472 # define CODE_OK TRUE
2474 # endif /* IRIX5 || OSF1 || HURD */
2477 # if defined(ALPHA) || defined(M68K)
2478 void GC_write_fault_handler(int sig
, int code
, s_c
* sc
)
2480 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2481 void GC_write_fault_handler(int sig
, siginfo_t
* si
, s_c
* scp
)
2484 void GC_write_fault_handler(int sig
, int a2
, int a3
, int a4
, s_c sc
)
2486 void GC_write_fault_handler(int sig
, s_c sc
)
2490 # define SIG_OK (sig == SIGSEGV)
2491 # define CODE_OK TRUE
2492 /* Empirically c.trapno == 14, on IA32, but is that useful? */
2493 /* Should probably consider alignment issues on other */
2494 /* architectures. */
2497 # if defined(SUNOS5SIGS)
2499 void GC_write_fault_handler(int sig
, SIGINFO_T
*scp
, void * context
)
2501 void GC_write_fault_handler(sig
, scp
, context
)
2507 # define SIG_OK (sig == SIGSEGV || sig == SIGBUS)
2508 # define CODE_OK (scp -> si_code == SEGV_ACCERR) \
2509 || (scp -> si_code == BUS_ADRERR) \
2510 || (scp -> si_code == BUS_UNKNOWN) \
2511 || (scp -> si_code == SEGV_UNKNOWN) \
2512 || (scp -> si_code == BUS_OBJERR)
2515 # define SIG_OK (sig == SIGBUS)
2516 # define CODE_OK (scp -> si_code == BUS_PAGE_FAULT)
2518 # define SIG_OK (sig == SIGSEGV)
2519 # define CODE_OK (scp -> si_code == SEGV_ACCERR)
2522 # endif /* SUNOS5SIGS */
2524 # if defined(MSWIN32) || defined(MSWINCE)
2525 LONG WINAPI
GC_write_fault_handler(struct _EXCEPTION_POINTERS
*exc_info
)
2526 # define SIG_OK (exc_info -> ExceptionRecord -> ExceptionCode == \
2527 STATUS_ACCESS_VIOLATION)
2528 # define CODE_OK (exc_info -> ExceptionRecord -> ExceptionInformation[0] == 1)
2530 # endif /* MSWIN32 || MSWINCE */
2532 register unsigned i
;
2534 char *addr
= (char *) code
;
2537 char * addr
= (char *) (size_t) (scp
-> sc_badvaddr
);
2539 # if defined(OSF1) && defined(ALPHA)
2540 char * addr
= (char *) (scp
-> sc_traparg_a0
);
2543 char * addr
= (char *) (scp
-> si_addr
);
2547 char * addr
= (char *) (sc
.cr2
);
2552 struct sigcontext
*scp
= (struct sigcontext
*)(sc
);
2554 int format
= (scp
->sc_formatvec
>> 12) & 0xf;
2555 unsigned long *framedata
= (unsigned long *)(scp
+ 1);
2558 if (format
== 0xa || format
== 0xb) {
2561 } else if (format
== 7) {
2564 if (framedata
[1] & 0x08000000) {
2565 /* correct addr on misaligned access */
2566 ea
= (ea
+4095)&(~4095);
2568 } else if (format
== 4) {
2571 if (framedata
[1] & 0x08000000) {
2572 /* correct addr on misaligned access */
2573 ea
= (ea
+4095)&(~4095);
2579 char * addr
= get_fault_addr(sc
);
2581 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2582 char * addr
= si
-> si_addr
;
2583 /* I believe this is claimed to work on all platforms for */
2584 /* Linux 2.3.47 and later. Hopefully we don't have to */
2585 /* worry about earlier kernels on IA64. */
2587 # if defined(POWERPC)
2588 char * addr
= (char *) (sc
.regs
->dar
);
2591 char * addr
= (char *)sc
.fault_address
;
2594 char * addr
= (char *)sc
.regs
.csraddr
;
2596 --> architecture
not supported
2605 # if defined(MSWIN32) || defined(MSWINCE)
2606 char * addr
= (char *) (exc_info
-> ExceptionRecord
2607 -> ExceptionInformation
[1]);
2608 # define sig SIGSEGV
2611 if (SIG_OK
&& CODE_OK
) {
2612 register struct hblk
* h
=
2613 (struct hblk
*)((word
)addr
& ~(GC_page_size
-1));
2614 GC_bool in_allocd_block
;
2617 /* Address is only within the correct physical page. */
2618 in_allocd_block
= FALSE
;
2619 for (i
= 0; i
< divHBLKSZ(GC_page_size
); i
++) {
2620 if (HDR(h
+i
) != 0) {
2621 in_allocd_block
= TRUE
;
2625 in_allocd_block
= (HDR(addr
) != 0);
2627 if (!in_allocd_block
) {
2628 /* FIXME - We should make sure that we invoke the */
2629 /* old handler with the appropriate calling */
2630 /* sequence, which often depends on SA_SIGINFO. */
2632 /* Heap blocks now begin and end on page boundaries */
2635 if (sig
== SIGSEGV
) {
2636 old_handler
= GC_old_segv_handler
;
2638 old_handler
= GC_old_bus_handler
;
2640 if (old_handler
== SIG_DFL
) {
2641 # if !defined(MSWIN32) && !defined(MSWINCE)
2642 GC_err_printf1("Segfault at 0x%lx\n", addr
);
2643 ABORT("Unexpected bus error or segmentation fault");
2645 return(EXCEPTION_CONTINUE_SEARCH
);
2648 # if defined (SUNOS4) \
2649 || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2650 (*old_handler
) (sig
, code
, scp
, addr
);
2653 # if defined (SUNOS5SIGS)
2655 * FIXME: For FreeBSD, this code should check if the
2656 * old signal handler used the traditional BSD style and
2657 * if so call it using that style.
2659 (*(REAL_SIG_PF
)old_handler
) (sig
, scp
, context
);
2662 # if defined (LINUX)
2663 # if defined(ALPHA) || defined(M68K)
2664 (*(REAL_SIG_PF
)old_handler
) (sig
, code
, sc
);
2666 # if defined(IA64) || defined(HP_PA) || defined(X86_64)
2667 (*(REAL_SIG_PF
)old_handler
) (sig
, si
, scp
);
2669 (*(REAL_SIG_PF
)old_handler
) (sig
, sc
);
2674 # if defined (IRIX5) || defined(OSF1) || defined(HURD)
2675 (*(REAL_SIG_PF
)old_handler
) (sig
, code
, scp
);
2679 return((*old_handler
)(exc_info
));
2683 UNPROTECT(h
, GC_page_size
);
2684 /* We need to make sure that no collection occurs between */
2685 /* the UNPROTECT and the setting of the dirty bit. Otherwise */
2686 /* a write by a third thread might go unnoticed. Reversing */
2687 /* the order is just as bad, since we would end up unprotecting */
2688 /* a page in a GC cycle during which it's not marked. */
2689 /* Currently we do this by disabling the thread stopping */
2690 /* signals while this handler is running. An alternative might */
2691 /* be to record the fact that we're about to unprotect, or */
2692 /* have just unprotected a page in the GC's thread structure, */
2693 /* and then to have the thread stopping code set the dirty */
2694 /* flag, if necessary. */
2695 for (i
= 0; i
< divHBLKSZ(GC_page_size
); i
++) {
2696 register int index
= PHT_HASH(h
+i
);
2698 async_set_pht_entry_from_index(GC_dirty_pages
, index
);
2701 /* These reset the signal handler each time by default. */
2702 signal(SIGSEGV
, (SIG_PF
) GC_write_fault_handler
);
2704 /* The write may not take place before dirty bits are read. */
2705 /* But then we'll fault again ... */
2706 # if defined(MSWIN32) || defined(MSWINCE)
2707 return(EXCEPTION_CONTINUE_EXECUTION
);
2712 #if defined(MSWIN32) || defined(MSWINCE)
2713 return EXCEPTION_CONTINUE_SEARCH
;
2715 GC_err_printf1("Segfault at 0x%lx\n", addr
);
2716 ABORT("Unexpected bus error or segmentation fault");
2719 #endif /* !DARWIN */
2722 * We hold the allocation lock. We expect block h to be written
2723 * shortly. Ensure that all pages containing any part of the n hblks
2724 * starting at h are no longer protected. If is_ptrfree is false,
2725 * also ensure that they will subsequently appear to be dirty.
2727 void GC_remove_protection(h
, nblocks
, is_ptrfree
)
2732 struct hblk
* h_trunc
; /* Truncated to page boundary */
2733 struct hblk
* h_end
; /* Page boundary following block end */
2734 struct hblk
* current
;
2735 GC_bool found_clean
;
2737 if (!GC_dirty_maintained
) return;
2738 h_trunc
= (struct hblk
*)((word
)h
& ~(GC_page_size
-1));
2739 h_end
= (struct hblk
*)(((word
)(h
+ nblocks
) + GC_page_size
-1)
2740 & ~(GC_page_size
-1));
2741 found_clean
= FALSE
;
2742 for (current
= h_trunc
; current
< h_end
; ++current
) {
2743 int index
= PHT_HASH(current
);
2745 if (!is_ptrfree
|| current
< h
|| current
>= h
+ nblocks
) {
2746 async_set_pht_entry_from_index(GC_dirty_pages
, index
);
2749 UNPROTECT(h_trunc
, (ptr_t
)h_end
- (ptr_t
)h_trunc
);
2752 #if !defined(DARWIN)
2753 void GC_dirty_init()
2755 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(LINUX) || \
2756 defined(OSF1) || defined(HURD)
2757 struct sigaction act
, oldact
;
2758 /* We should probably specify SA_SIGINFO for Linux, and handle */
2759 /* the different architectures more uniformly. */
2760 # if defined(IRIX5) || defined(LINUX) && !defined(X86_64) \
2761 || defined(OSF1) || defined(HURD)
2762 act
.sa_flags
= SA_RESTART
;
2763 act
.sa_handler
= (SIG_PF
)GC_write_fault_handler
;
2765 act
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
2766 act
.sa_sigaction
= GC_write_fault_handler
;
2768 (void)sigemptyset(&act
.sa_mask
);
2770 /* Arrange to postpone SIG_SUSPEND while we're in a write fault */
2771 /* handler. This effectively makes the handler atomic w.r.t. */
2772 /* stopping the world for GC. */
2773 (void)sigaddset(&act
.sa_mask
, SIG_SUSPEND
);
2774 # endif /* SIG_SUSPEND */
2777 GC_printf0("Inititalizing mprotect virtual dirty bit implementation\n");
2779 GC_dirty_maintained
= TRUE
;
2780 if (GC_page_size
% HBLKSIZE
!= 0) {
2781 GC_err_printf0("Page size not multiple of HBLKSIZE\n");
2782 ABORT("Page size not multiple of HBLKSIZE");
2784 # if defined(SUNOS4) || (defined(FREEBSD) && !defined(SUNOS5SIGS))
2785 GC_old_bus_handler
= signal(SIGBUS
, GC_write_fault_handler
);
2786 if (GC_old_bus_handler
== SIG_IGN
) {
2787 GC_err_printf0("Previously ignored bus error!?");
2788 GC_old_bus_handler
= SIG_DFL
;
2790 if (GC_old_bus_handler
!= SIG_DFL
) {
2792 GC_err_printf0("Replaced other SIGBUS handler\n");
2796 # if defined(SUNOS4)
2797 GC_old_segv_handler
= signal(SIGSEGV
, (SIG_PF
)GC_write_fault_handler
);
2798 if (GC_old_segv_handler
== SIG_IGN
) {
2799 GC_err_printf0("Previously ignored segmentation violation!?");
2800 GC_old_segv_handler
= SIG_DFL
;
2802 if (GC_old_segv_handler
!= SIG_DFL
) {
2804 GC_err_printf0("Replaced other SIGSEGV handler\n");
2808 # if (defined(SUNOS5SIGS) && !defined(FREEBSD)) || defined(IRIX5) \
2809 || defined(LINUX) || defined(OSF1) || defined(HURD)
2810 /* SUNOS5SIGS includes HPUX */
2811 # if defined(GC_IRIX_THREADS)
2812 sigaction(SIGSEGV
, 0, &oldact
);
2813 sigaction(SIGSEGV
, &act
, 0);
2816 int res
= sigaction(SIGSEGV
, &act
, &oldact
);
2817 if (res
!= 0) ABORT("Sigaction failed");
2820 # if defined(_sigargs) || defined(HURD) || !defined(SA_SIGINFO)
2821 /* This is Irix 5.x, not 6.x. Irix 5.x does not have */
2823 GC_old_segv_handler
= oldact
.sa_handler
;
2824 # else /* Irix 6.x or SUNOS5SIGS or LINUX */
2825 if (oldact
.sa_flags
& SA_SIGINFO
) {
2826 GC_old_segv_handler
= (SIG_PF
)(oldact
.sa_sigaction
);
2828 GC_old_segv_handler
= oldact
.sa_handler
;
2831 if (GC_old_segv_handler
== SIG_IGN
) {
2832 GC_err_printf0("Previously ignored segmentation violation!?");
2833 GC_old_segv_handler
= SIG_DFL
;
2835 if (GC_old_segv_handler
!= SIG_DFL
) {
2837 GC_err_printf0("Replaced other SIGSEGV handler\n");
2840 # endif /* (SUNOS5SIGS && !FREEBSD) || IRIX5 || LINUX || OSF1 || HURD */
2841 # if defined(HPUX) || defined(LINUX) || defined(HURD) \
2842 || (defined(FREEBSD) && defined(SUNOS5SIGS))
2843 sigaction(SIGBUS
, &act
, &oldact
);
2844 GC_old_bus_handler
= oldact
.sa_handler
;
2845 if (GC_old_bus_handler
== SIG_IGN
) {
2846 GC_err_printf0("Previously ignored bus error!?");
2847 GC_old_bus_handler
= SIG_DFL
;
2849 if (GC_old_bus_handler
!= SIG_DFL
) {
2851 GC_err_printf0("Replaced other SIGBUS handler\n");
2854 # endif /* HPUX || LINUX || HURD || (FREEBSD && SUNOS5SIGS) */
2855 # if defined(MSWIN32)
2856 GC_old_segv_handler
= SetUnhandledExceptionFilter(GC_write_fault_handler
);
2857 if (GC_old_segv_handler
!= NULL
) {
2859 GC_err_printf0("Replaced other UnhandledExceptionFilter\n");
2862 GC_old_segv_handler
= SIG_DFL
;
2866 #endif /* !DARWIN */
2868 int GC_incremental_protection_needs()
2870 if (GC_page_size
== HBLKSIZE
) {
2871 return GC_PROTECTS_POINTER_HEAP
;
2873 return GC_PROTECTS_POINTER_HEAP
| GC_PROTECTS_PTRFREE_HEAP
;
2877 #define HAVE_INCREMENTAL_PROTECTION_NEEDS
2879 #define IS_PTRFREE(hhdr) ((hhdr)->hb_descr == 0)
2881 #define PAGE_ALIGNED(x) !((word)(x) & (GC_page_size - 1))
2882 void GC_protect_heap()
2886 struct hblk
* current
;
2887 struct hblk
* current_start
; /* Start of block to be protected. */
2888 struct hblk
* limit
;
2890 GC_bool protect_all
=
2891 (0 != (GC_incremental_protection_needs() & GC_PROTECTS_PTRFREE_HEAP
));
2892 for (i
= 0; i
< GC_n_heap_sects
; i
++) {
2893 start
= GC_heap_sects
[i
].hs_start
;
2894 len
= GC_heap_sects
[i
].hs_bytes
;
2896 PROTECT(start
, len
);
2898 GC_ASSERT(PAGE_ALIGNED(len
))
2899 GC_ASSERT(PAGE_ALIGNED(start
))
2900 current_start
= current
= (struct hblk
*)start
;
2901 limit
= (struct hblk
*)(start
+ len
);
2902 while (current
< limit
) {
2907 GC_ASSERT(PAGE_ALIGNED(current
));
2908 GET_HDR(current
, hhdr
);
2909 if (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
2910 /* This can happen only if we're at the beginning of a */
2911 /* heap segment, and a block spans heap segments. */
2912 /* We will handle that block as part of the preceding */
2914 GC_ASSERT(current_start
== current
);
2915 current_start
= ++current
;
2918 if (HBLK_IS_FREE(hhdr
)) {
2919 GC_ASSERT(PAGE_ALIGNED(hhdr
-> hb_sz
));
2920 nhblks
= divHBLKSZ(hhdr
-> hb_sz
);
2921 is_ptrfree
= TRUE
; /* dirty on alloc */
2923 nhblks
= OBJ_SZ_TO_BLOCKS(hhdr
-> hb_sz
);
2924 is_ptrfree
= IS_PTRFREE(hhdr
);
2927 if (current_start
< current
) {
2928 PROTECT(current_start
, (ptr_t
)current
- (ptr_t
)current_start
);
2930 current_start
= (current
+= nhblks
);
2935 if (current_start
< current
) {
2936 PROTECT(current_start
, (ptr_t
)current
- (ptr_t
)current_start
);
2942 /* We assume that either the world is stopped or its OK to lose dirty */
2943 /* bits while this is happenning (as in GC_enable_incremental). */
2944 void GC_read_dirty()
2946 BCOPY((word
*)GC_dirty_pages
, GC_grungy_pages
,
2947 (sizeof GC_dirty_pages
));
2948 BZERO((word
*)GC_dirty_pages
, (sizeof GC_dirty_pages
));
2952 GC_bool
GC_page_was_dirty(h
)
2955 register word index
= PHT_HASH(h
);
2957 return(HDR(h
) == 0 || get_pht_entry_from_index(GC_grungy_pages
, index
));
2961 * Acquiring the allocation lock here is dangerous, since this
2962 * can be called from within GC_call_with_alloc_lock, and the cord
2963 * package does so. On systems that allow nested lock acquisition, this
2965 * On other systems, SET_LOCK_HOLDER and friends must be suitably defined.
2968 static GC_bool syscall_acquired_lock
= FALSE
; /* Protected by GC lock. */
2970 void GC_begin_syscall()
2972 if (!I_HOLD_LOCK()) {
2974 syscall_acquired_lock
= TRUE
;
2978 void GC_end_syscall()
2980 if (syscall_acquired_lock
) {
2981 syscall_acquired_lock
= FALSE
;
2986 void GC_unprotect_range(addr
, len
)
2990 struct hblk
* start_block
;
2991 struct hblk
* end_block
;
2992 register struct hblk
*h
;
2995 if (!GC_dirty_maintained
) return;
2996 obj_start
= GC_base(addr
);
2997 if (obj_start
== 0) return;
2998 if (GC_base(addr
+ len
- 1) != obj_start
) {
2999 ABORT("GC_unprotect_range(range bigger than object)");
3001 start_block
= (struct hblk
*)((word
)addr
& ~(GC_page_size
- 1));
3002 end_block
= (struct hblk
*)((word
)(addr
+ len
- 1) & ~(GC_page_size
- 1));
3003 end_block
+= GC_page_size
/HBLKSIZE
- 1;
3004 for (h
= start_block
; h
<= end_block
; h
++) {
3005 register word index
= PHT_HASH(h
);
3007 async_set_pht_entry_from_index(GC_dirty_pages
, index
);
3009 UNPROTECT(start_block
,
3010 ((ptr_t
)end_block
- (ptr_t
)start_block
) + HBLKSIZE
);
3015 /* We no longer wrap read by default, since that was causing too many */
3016 /* problems. It is preferred that the client instead avoids writing */
3017 /* to the write-protected heap with a system call. */
3018 /* This still serves as sample code if you do want to wrap system calls.*/
3020 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(GC_USE_LD_WRAP)
3021 /* Replacement for UNIX system call. */
3022 /* Other calls that write to the heap should be handled similarly. */
3023 /* Note that this doesn't work well for blocking reads: It will hold */
3024 /* the allocation lock for the entire duration of the call. Multithreaded */
3025 /* clients should really ensure that it won't block, either by setting */
3026 /* the descriptor nonblocking, or by calling select or poll first, to */
3027 /* make sure that input is available. */
3028 /* Another, preferred alternative is to ensure that system calls never */
3029 /* write to the protected heap (see above). */
3030 # if defined(__STDC__) && !defined(SUNOS4)
3031 # include <unistd.h>
3032 # include <sys/uio.h>
3033 ssize_t
read(int fd
, void *buf
, size_t nbyte
)
3036 int read(fd
, buf
, nbyte
)
3038 int GC_read(fd
, buf
, nbyte
)
3048 GC_unprotect_range(buf
, (word
)nbyte
);
3049 # if defined(IRIX5) || defined(GC_LINUX_THREADS)
3050 /* Indirect system call may not always be easily available. */
3051 /* We could call _read, but that would interfere with the */
3052 /* libpthread interception of read. */
3053 /* On Linux, we have to be careful with the linuxthreads */
3054 /* read interception. */
3059 iov
.iov_len
= nbyte
;
3060 result
= readv(fd
, &iov
, 1);
3064 result
= __read(fd
, buf
, nbyte
);
3066 /* The two zero args at the end of this list are because one
3067 IA-64 syscall() implementation actually requires six args
3068 to be passed, even though they aren't always used. */
3069 result
= syscall(SYS_read
, fd
, buf
, nbyte
, 0, 0);
3075 #endif /* !MSWIN32 && !MSWINCE && !GC_LINUX_THREADS */
3077 #if defined(GC_USE_LD_WRAP) && !defined(THREADS)
3078 /* We use the GNU ld call wrapping facility. */
3079 /* This requires that the linker be invoked with "--wrap read". */
3080 /* This can be done by passing -Wl,"--wrap read" to gcc. */
3081 /* I'm not sure that this actually wraps whatever version of read */
3082 /* is called by stdio. That code also mentions __read. */
3083 # include <unistd.h>
3084 ssize_t
__wrap_read(int fd
, void *buf
, size_t nbyte
)
3089 GC_unprotect_range(buf
, (word
)nbyte
);
3090 result
= __real_read(fd
, buf
, nbyte
);
3095 /* We should probably also do this for __read, or whatever stdio */
3096 /* actually calls. */
3102 GC_bool
GC_page_was_ever_dirty(h
)
3108 /* Reset the n pages starting at h to "was never dirty" status. */
3110 void GC_is_fresh(h
, n
)
3116 # endif /* MPROTECT_VDB */
3121 * See DEFAULT_VDB for interface descriptions.
3125 * This implementaion assumes a Solaris 2.X like /proc pseudo-file-system
3126 * from which we can read page modified bits. This facility is far from
3127 * optimal (e.g. we would like to get the info for only some of the
3128 * address space), but it avoids intercepting system calls.
3132 #include <sys/types.h>
3133 #include <sys/signal.h>
3134 #include <sys/fault.h>
3135 #include <sys/syscall.h>
3136 #include <sys/procfs.h>
3137 #include <sys/stat.h>
3139 #define INITIAL_BUF_SZ 16384
3140 word GC_proc_buf_size
= INITIAL_BUF_SZ
;
3143 #ifdef GC_SOLARIS_THREADS
3144 /* We don't have exact sp values for threads. So we count on */
3145 /* occasionally declaring stack pages to be fresh. Thus we */
3146 /* need a real implementation of GC_is_fresh. We can't clear */
3147 /* entries in GC_written_pages, since that would declare all */
3148 /* pages with the given hash address to be fresh. */
3149 # define MAX_FRESH_PAGES 8*1024 /* Must be power of 2 */
3150 struct hblk
** GC_fresh_pages
; /* A direct mapped cache. */
3151 /* Collisions are dropped. */
3153 # define FRESH_PAGE_SLOT(h) (divHBLKSZ((word)(h)) & (MAX_FRESH_PAGES-1))
3154 # define ADD_FRESH_PAGE(h) \
3155 GC_fresh_pages[FRESH_PAGE_SLOT(h)] = (h)
3156 # define PAGE_IS_FRESH(h) \
3157 (GC_fresh_pages[FRESH_PAGE_SLOT(h)] == (h) && (h) != 0)
3160 /* Add all pages in pht2 to pht1 */
3161 void GC_or_pages(pht1
, pht2
)
3162 page_hash_table pht1
, pht2
;
3166 for (i
= 0; i
< PHT_SIZE
; i
++) pht1
[i
] |= pht2
[i
];
3171 void GC_dirty_init()
3176 GC_dirty_maintained
= TRUE
;
3177 if (GC_words_allocd
!= 0 || GC_words_allocd_before_gc
!= 0) {
3180 for (i
= 0; i
< PHT_SIZE
; i
++) GC_written_pages
[i
] = (word
)(-1);
3182 GC_printf1("Allocated words:%lu:all pages may have been written\n",
3184 (GC_words_allocd
+ GC_words_allocd_before_gc
));
3187 sprintf(buf
, "/proc/%d/pagedata", getpid());
3188 GC_proc_fd
= open(buf
, O_RDONLY
);
3189 if (GC_proc_fd
< 0) {
3190 ABORT("/proc open failed");
3192 syscall(SYS_fcntl
, GC_proc_fd
, F_SETFD
, FD_CLOEXEC
);
3193 if (GC_proc_fd
< 0) {
3194 ABORT("/proc ioctl failed");
3196 GC_proc_buf
= GC_scratch_alloc(GC_proc_buf_size
);
3197 # ifdef GC_SOLARIS_THREADS
3198 GC_fresh_pages
= (struct hblk
**)
3199 GC_scratch_alloc(MAX_FRESH_PAGES
* sizeof (struct hblk
*));
3200 if (GC_fresh_pages
== 0) {
3201 GC_err_printf0("No space for fresh pages\n");
3204 BZERO(GC_fresh_pages
, MAX_FRESH_PAGES
* sizeof (struct hblk
*));
3208 /* Ignore write hints. They don't help us here. */
3210 void GC_remove_protection(h
, nblocks
, is_ptrfree
)
3217 #ifdef GC_SOLARIS_THREADS
3218 # define READ(fd,buf,nbytes) syscall(SYS_read, fd, buf, nbytes)
3220 # define READ(fd,buf,nbytes) read(fd, buf, nbytes)
3223 void GC_read_dirty()
3225 unsigned long ps
, np
;
3228 struct prasmap
* map
;
3230 ptr_t current_addr
, limit
;
3234 BZERO(GC_grungy_pages
, (sizeof GC_grungy_pages
));
3237 if (READ(GC_proc_fd
, bufp
, GC_proc_buf_size
) <= 0) {
3239 GC_printf1("/proc read failed: GC_proc_buf_size = %lu\n",
3243 /* Retry with larger buffer. */
3244 word new_size
= 2 * GC_proc_buf_size
;
3245 char * new_buf
= GC_scratch_alloc(new_size
);
3248 GC_proc_buf
= bufp
= new_buf
;
3249 GC_proc_buf_size
= new_size
;
3251 if (READ(GC_proc_fd
, bufp
, GC_proc_buf_size
) <= 0) {
3252 WARN("Insufficient space for /proc read\n", 0);
3254 memset(GC_grungy_pages
, 0xff, sizeof (page_hash_table
));
3255 memset(GC_written_pages
, 0xff, sizeof(page_hash_table
));
3256 # ifdef GC_SOLARIS_THREADS
3257 BZERO(GC_fresh_pages
,
3258 MAX_FRESH_PAGES
* sizeof (struct hblk
*));
3264 /* Copy dirty bits into GC_grungy_pages */
3265 nmaps
= ((struct prpageheader
*)bufp
) -> pr_nmap
;
3266 /* printf( "nmaps = %d, PG_REFERENCED = %d, PG_MODIFIED = %d\n",
3267 nmaps, PG_REFERENCED, PG_MODIFIED); */
3268 bufp
= bufp
+ sizeof(struct prpageheader
);
3269 for (i
= 0; i
< nmaps
; i
++) {
3270 map
= (struct prasmap
*)bufp
;
3271 vaddr
= (ptr_t
)(map
-> pr_vaddr
);
3272 ps
= map
-> pr_pagesize
;
3273 np
= map
-> pr_npage
;
3274 /* printf("vaddr = 0x%X, ps = 0x%X, np = 0x%X\n", vaddr, ps, np); */
3275 limit
= vaddr
+ ps
* np
;
3276 bufp
+= sizeof (struct prasmap
);
3277 for (current_addr
= vaddr
;
3278 current_addr
< limit
; current_addr
+= ps
){
3279 if ((*bufp
++) & PG_MODIFIED
) {
3280 register struct hblk
* h
= (struct hblk
*) current_addr
;
3282 while ((ptr_t
)h
< current_addr
+ ps
) {
3283 register word index
= PHT_HASH(h
);
3285 set_pht_entry_from_index(GC_grungy_pages
, index
);
3286 # ifdef GC_SOLARIS_THREADS
3288 register int slot
= FRESH_PAGE_SLOT(h
);
3290 if (GC_fresh_pages
[slot
] == h
) {
3291 GC_fresh_pages
[slot
] = 0;
3299 bufp
+= sizeof(long) - 1;
3300 bufp
= (char *)((unsigned long)bufp
& ~(sizeof(long)-1));
3302 /* Update GC_written_pages. */
3303 GC_or_pages(GC_written_pages
, GC_grungy_pages
);
3304 # ifdef GC_SOLARIS_THREADS
3305 /* Make sure that old stacks are considered completely clean */
3306 /* unless written again. */
3307 GC_old_stacks_are_fresh();
3313 GC_bool
GC_page_was_dirty(h
)
3316 register word index
= PHT_HASH(h
);
3317 register GC_bool result
;
3319 result
= get_pht_entry_from_index(GC_grungy_pages
, index
);
3320 # ifdef GC_SOLARIS_THREADS
3321 if (result
&& PAGE_IS_FRESH(h
)) result
= FALSE
;
3322 /* This happens only if page was declared fresh since */
3323 /* the read_dirty call, e.g. because it's in an unused */
3324 /* thread stack. It's OK to treat it as clean, in */
3325 /* that case. And it's consistent with */
3326 /* GC_page_was_ever_dirty. */
3331 GC_bool
GC_page_was_ever_dirty(h
)
3334 register word index
= PHT_HASH(h
);
3335 register GC_bool result
;
3337 result
= get_pht_entry_from_index(GC_written_pages
, index
);
3338 # ifdef GC_SOLARIS_THREADS
3339 if (result
&& PAGE_IS_FRESH(h
)) result
= FALSE
;
3344 /* Caller holds allocation lock. */
3345 void GC_is_fresh(h
, n
)
3350 register word index
;
3352 # ifdef GC_SOLARIS_THREADS
3355 if (GC_fresh_pages
!= 0) {
3356 for (i
= 0; i
< n
; i
++) {
3357 ADD_FRESH_PAGE(h
+ i
);
3363 # endif /* PROC_VDB */
3368 # include "vd/PCR_VD.h"
3370 # define NPAGES (32*1024) /* 128 MB */
3372 PCR_VD_DB GC_grungy_bits
[NPAGES
];
3374 ptr_t GC_vd_base
; /* Address corresponding to GC_grungy_bits[0] */
3375 /* HBLKSIZE aligned. */
3377 void GC_dirty_init()
3379 GC_dirty_maintained
= TRUE
;
3380 /* For the time being, we assume the heap generally grows up */
3381 GC_vd_base
= GC_heap_sects
[0].hs_start
;
3382 if (GC_vd_base
== 0) {
3383 ABORT("Bad initial heap segment");
3385 if (PCR_VD_Start(HBLKSIZE
, GC_vd_base
, NPAGES
*HBLKSIZE
)
3387 ABORT("dirty bit initialization failed");
3391 void GC_read_dirty()
3393 /* lazily enable dirty bits on newly added heap sects */
3395 static int onhs
= 0;
3396 int nhs
= GC_n_heap_sects
;
3397 for( ; onhs
< nhs
; onhs
++ ) {
3398 PCR_VD_WriteProtectEnable(
3399 GC_heap_sects
[onhs
].hs_start
,
3400 GC_heap_sects
[onhs
].hs_bytes
);
3405 if (PCR_VD_Clear(GC_vd_base
, NPAGES
*HBLKSIZE
, GC_grungy_bits
)
3407 ABORT("dirty bit read failed");
3411 GC_bool
GC_page_was_dirty(h
)
3414 if((ptr_t
)h
< GC_vd_base
|| (ptr_t
)h
>= GC_vd_base
+ NPAGES
*HBLKSIZE
) {
3417 return(GC_grungy_bits
[h
- (struct hblk
*)GC_vd_base
] & PCR_VD_DB_dirtyBit
);
3421 void GC_remove_protection(h
, nblocks
, is_ptrfree
)
3426 PCR_VD_WriteProtectDisable(h
, nblocks
*HBLKSIZE
);
3427 PCR_VD_WriteProtectEnable(h
, nblocks
*HBLKSIZE
);
3430 # endif /* PCR_VDB */
3432 #if defined(MPROTECT_VDB) && defined(DARWIN)
3433 /* The following sources were used as a *reference* for this exception handling
3435 1. Apple's mach/xnu documentation
3436 2. Timothy J. Wood's "Mach Exception Handlers 101" post to the
3437 omnigroup's macosx-dev list.
3438 www.omnigroup.com/mailman/archive/macosx-dev/2000-June/014178.html
3439 3. macosx-nat.c from Apple's GDB source code.
3442 /* The bug that caused all this trouble should now be fixed. This should
3443 eventually be removed if all goes well. */
3444 /* define BROKEN_EXCEPTION_HANDLING */
3446 #include <mach/mach.h>
3447 #include <mach/mach_error.h>
3448 #include <mach/thread_status.h>
3449 #include <mach/exception.h>
3450 #include <mach/task.h>
3451 #include <pthread.h>
3453 /* These are not defined in any header, although they are documented */
3454 extern boolean_t
exc_server(mach_msg_header_t
*,mach_msg_header_t
*);
3455 extern kern_return_t
exception_raise(
3456 mach_port_t
,mach_port_t
,mach_port_t
,
3457 exception_type_t
,exception_data_t
,mach_msg_type_number_t
);
3458 extern kern_return_t
exception_raise_state(
3459 mach_port_t
,mach_port_t
,mach_port_t
,
3460 exception_type_t
,exception_data_t
,mach_msg_type_number_t
,
3461 thread_state_flavor_t
*,thread_state_t
,mach_msg_type_number_t
,
3462 thread_state_t
,mach_msg_type_number_t
*);
3463 extern kern_return_t
exception_raise_state_identity(
3464 mach_port_t
,mach_port_t
,mach_port_t
,
3465 exception_type_t
,exception_data_t
,mach_msg_type_number_t
,
3466 thread_state_flavor_t
*,thread_state_t
,mach_msg_type_number_t
,
3467 thread_state_t
,mach_msg_type_number_t
*);
3470 #define MAX_EXCEPTION_PORTS 16
3473 mach_msg_type_number_t count
;
3474 exception_mask_t masks
[MAX_EXCEPTION_PORTS
];
3475 exception_handler_t ports
[MAX_EXCEPTION_PORTS
];
3476 exception_behavior_t behaviors
[MAX_EXCEPTION_PORTS
];
3477 thread_state_flavor_t flavors
[MAX_EXCEPTION_PORTS
];
3481 mach_port_t exception
;
3482 #if defined(THREADS)
3488 mach_msg_header_t head
;
3492 GC_MP_NORMAL
, GC_MP_DISCARDING
, GC_MP_STOPPED
3493 } GC_mprotect_state_t
;
3495 /* FIXME: 1 and 2 seem to be safe to use in the msgh_id field,
3496 but it isn't documented. Use the source and see if they
3501 /* These values are only used on the reply port */
3504 #if defined(THREADS)
3506 GC_mprotect_state_t GC_mprotect_state
;
3508 /* The following should ONLY be called when the world is stopped */
3509 static void GC_mprotect_thread_notify(mach_msg_id_t id
) {
3512 mach_msg_trailer_t trailer
;
3514 mach_msg_return_t r
;
3516 buf
.msg
.head
.msgh_bits
=
3517 MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND
,0);
3518 buf
.msg
.head
.msgh_size
= sizeof(buf
.msg
);
3519 buf
.msg
.head
.msgh_remote_port
= GC_ports
.exception
;
3520 buf
.msg
.head
.msgh_local_port
= MACH_PORT_NULL
;
3521 buf
.msg
.head
.msgh_id
= id
;
3525 MACH_SEND_MSG
|MACH_RCV_MSG
|MACH_RCV_LARGE
,
3529 MACH_MSG_TIMEOUT_NONE
,
3531 if(r
!= MACH_MSG_SUCCESS
)
3532 ABORT("mach_msg failed in GC_mprotect_thread_notify");
3533 if(buf
.msg
.head
.msgh_id
!= ID_ACK
)
3534 ABORT("invalid ack in GC_mprotect_thread_notify");
3537 /* Should only be called by the mprotect thread */
3538 static void GC_mprotect_thread_reply() {
3540 mach_msg_return_t r
;
3542 msg
.head
.msgh_bits
=
3543 MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND
,0);
3544 msg
.head
.msgh_size
= sizeof(msg
);
3545 msg
.head
.msgh_remote_port
= GC_ports
.reply
;
3546 msg
.head
.msgh_local_port
= MACH_PORT_NULL
;
3547 msg
.head
.msgh_id
= ID_ACK
;
3555 MACH_MSG_TIMEOUT_NONE
,
3557 if(r
!= MACH_MSG_SUCCESS
)
3558 ABORT("mach_msg failed in GC_mprotect_thread_reply");
3561 void GC_mprotect_stop() {
3562 GC_mprotect_thread_notify(ID_STOP
);
3564 void GC_mprotect_resume() {
3565 GC_mprotect_thread_notify(ID_RESUME
);
3568 #else /* !THREADS */
3569 /* The compiler should optimize away any GC_mprotect_state computations */
3570 #define GC_mprotect_state GC_MP_NORMAL
3573 static void *GC_mprotect_thread(void *arg
) {
3574 mach_msg_return_t r
;
3575 /* These two structures contain some private kernel data. We don't need to
3576 access any of it so we don't bother defining a proper struct. The
3577 correct definitions are in the xnu source code. */
3579 mach_msg_header_t head
;
3583 mach_msg_header_t head
;
3584 mach_msg_body_t msgh_body
;
3590 GC_darwin_register_mach_handler_thread(mach_thread_self());
3595 MACH_RCV_MSG
|MACH_RCV_LARGE
|
3596 (GC_mprotect_state
== GC_MP_DISCARDING
? MACH_RCV_TIMEOUT
: 0),
3600 GC_mprotect_state
== GC_MP_DISCARDING
? 0 : MACH_MSG_TIMEOUT_NONE
,
3603 id
= r
== MACH_MSG_SUCCESS
? msg
.head
.msgh_id
: -1;
3605 #if defined(THREADS)
3606 if(GC_mprotect_state
== GC_MP_DISCARDING
) {
3607 if(r
== MACH_RCV_TIMED_OUT
) {
3608 GC_mprotect_state
= GC_MP_STOPPED
;
3609 GC_mprotect_thread_reply();
3612 if(r
== MACH_MSG_SUCCESS
&& (id
== ID_STOP
|| id
== ID_RESUME
))
3613 ABORT("out of order mprotect thread request");
3617 if(r
!= MACH_MSG_SUCCESS
) {
3618 GC_err_printf2("mach_msg failed with %d %s\n",
3619 (int)r
,mach_error_string(r
));
3620 ABORT("mach_msg failed");
3624 #if defined(THREADS)
3626 if(GC_mprotect_state
!= GC_MP_NORMAL
)
3627 ABORT("Called mprotect_stop when state wasn't normal");
3628 GC_mprotect_state
= GC_MP_DISCARDING
;
3631 if(GC_mprotect_state
!= GC_MP_STOPPED
)
3632 ABORT("Called mprotect_resume when state wasn't stopped");
3633 GC_mprotect_state
= GC_MP_NORMAL
;
3634 GC_mprotect_thread_reply();
3636 #endif /* THREADS */
3638 /* Handle the message (calls catch_exception_raise) */
3639 if(!exc_server(&msg
.head
,&reply
.head
))
3640 ABORT("exc_server failed");
3641 /* Send the reply */
3645 reply
.head
.msgh_size
,
3648 MACH_MSG_TIMEOUT_NONE
,
3650 if(r
!= MACH_MSG_SUCCESS
) {
3651 /* This will fail if the thread dies, but the thread shouldn't
3653 #ifdef BROKEN_EXCEPTION_HANDLING
3655 "mach_msg failed with %d %s while sending exc reply\n",
3656 (int)r
,mach_error_string(r
));
3658 ABORT("mach_msg failed while sending exception reply");
3667 /* All this SIGBUS code shouldn't be necessary. All protection faults should
3668 be going throught the mach exception handler. However, it seems a SIGBUS is
3669 occasionally sent for some unknown reason. Even more odd, it seems to be
3670 meaningless and safe to ignore. */
3671 #ifdef BROKEN_EXCEPTION_HANDLING
3673 typedef void (* SIG_PF
)();
3674 static SIG_PF GC_old_bus_handler
;
3676 /* Updates to this aren't atomic, but the SIGBUSs seem pretty rare.
3677 Even if this doesn't get updated property, it isn't really a problem */
3678 static int GC_sigbus_count
;
3680 static void GC_darwin_sigbus(int num
,siginfo_t
*sip
,void *context
) {
3681 if(num
!= SIGBUS
) ABORT("Got a non-sigbus signal in the sigbus handler");
3683 /* Ugh... some seem safe to ignore, but too many in a row probably means
3684 trouble. GC_sigbus_count is reset for each mach exception that is
3686 if(GC_sigbus_count
>= 8) {
3687 ABORT("Got more than 8 SIGBUSs in a row!");
3690 GC_err_printf0("GC: WARNING: Ignoring SIGBUS.\n");
3693 #endif /* BROKEN_EXCEPTION_HANDLING */
3695 void GC_dirty_init() {
3699 pthread_attr_t attr
;
3700 exception_mask_t mask
;
3703 GC_printf0("Inititalizing mach/darwin mprotect virtual dirty bit "
3704 "implementation\n");
3706 # ifdef BROKEN_EXCEPTION_HANDLING
3707 GC_err_printf0("GC: WARNING: Enabling workarounds for various darwin "
3708 "exception handling bugs.\n");
3710 GC_dirty_maintained
= TRUE
;
3711 if (GC_page_size
% HBLKSIZE
!= 0) {
3712 GC_err_printf0("Page size not multiple of HBLKSIZE\n");
3713 ABORT("Page size not multiple of HBLKSIZE");
3716 GC_task_self
= me
= mach_task_self();
3718 r
= mach_port_allocate(me
,MACH_PORT_RIGHT_RECEIVE
,&GC_ports
.exception
);
3719 if(r
!= KERN_SUCCESS
) ABORT("mach_port_allocate failed (exception port)");
3721 r
= mach_port_insert_right(me
,GC_ports
.exception
,GC_ports
.exception
,
3722 MACH_MSG_TYPE_MAKE_SEND
);
3723 if(r
!= KERN_SUCCESS
)
3724 ABORT("mach_port_insert_right failed (exception port)");
3726 #if defined(THREADS)
3727 r
= mach_port_allocate(me
,MACH_PORT_RIGHT_RECEIVE
,&GC_ports
.reply
);
3728 if(r
!= KERN_SUCCESS
) ABORT("mach_port_allocate failed (reply port)");
3731 /* The exceptions we want to catch */
3732 mask
= EXC_MASK_BAD_ACCESS
;
3734 r
= task_get_exception_ports(
3737 GC_old_exc_ports
.masks
,
3738 &GC_old_exc_ports
.count
,
3739 GC_old_exc_ports
.ports
,
3740 GC_old_exc_ports
.behaviors
,
3741 GC_old_exc_ports
.flavors
3743 if(r
!= KERN_SUCCESS
) ABORT("task_get_exception_ports failed");
3745 r
= task_set_exception_ports(
3750 GC_MACH_THREAD_STATE
3752 if(r
!= KERN_SUCCESS
) ABORT("task_set_exception_ports failed");
3754 if(pthread_attr_init(&attr
) != 0) ABORT("pthread_attr_init failed");
3755 if(pthread_attr_setdetachstate(&attr
,PTHREAD_CREATE_DETACHED
) != 0)
3756 ABORT("pthread_attr_setdetachedstate failed");
3758 # undef pthread_create
3759 /* This will call the real pthread function, not our wrapper */
3760 if(pthread_create(&thread
,&attr
,GC_mprotect_thread
,NULL
) != 0)
3761 ABORT("pthread_create failed");
3762 pthread_attr_destroy(&attr
);
3764 /* Setup the sigbus handler for ignoring the meaningless SIGBUSs */
3765 #ifdef BROKEN_EXCEPTION_HANDLING
3767 struct sigaction sa
, oldsa
;
3768 sa
.sa_handler
= (SIG_PF
)GC_darwin_sigbus
;
3769 sigemptyset(&sa
.sa_mask
);
3770 sa
.sa_flags
= SA_RESTART
|SA_SIGINFO
;
3771 if(sigaction(SIGBUS
,&sa
,&oldsa
) < 0) ABORT("sigaction");
3772 GC_old_bus_handler
= (SIG_PF
)oldsa
.sa_handler
;
3773 if (GC_old_bus_handler
!= SIG_DFL
) {
3775 GC_err_printf0("Replaced other SIGBUS handler\n");
3779 #endif /* BROKEN_EXCEPTION_HANDLING */
3782 /* The source code for Apple's GDB was used as a reference for the exception
3783 forwarding code. This code is similar to be GDB code only because there is
3784 only one way to do it. */
3785 static kern_return_t
GC_forward_exception(
3788 exception_type_t exception
,
3789 exception_data_t data
,
3790 mach_msg_type_number_t data_count
3795 exception_behavior_t behavior
;
3796 thread_state_flavor_t flavor
;
3798 thread_state_t thread_state
;
3799 mach_msg_type_number_t thread_state_count
= THREAD_STATE_MAX
;
3801 for(i
=0;i
<GC_old_exc_ports
.count
;i
++)
3802 if(GC_old_exc_ports
.masks
[i
] & (1 << exception
))
3804 if(i
==GC_old_exc_ports
.count
) ABORT("No handler for exception!");
3806 port
= GC_old_exc_ports
.ports
[i
];
3807 behavior
= GC_old_exc_ports
.behaviors
[i
];
3808 flavor
= GC_old_exc_ports
.flavors
[i
];
3810 if(behavior
!= EXCEPTION_DEFAULT
) {
3811 r
= thread_get_state(thread
,flavor
,thread_state
,&thread_state_count
);
3812 if(r
!= KERN_SUCCESS
)
3813 ABORT("thread_get_state failed in forward_exception");
3817 case EXCEPTION_DEFAULT
:
3818 r
= exception_raise(port
,thread
,task
,exception
,data
,data_count
);
3820 case EXCEPTION_STATE
:
3821 r
= exception_raise_state(port
,thread
,task
,exception
,data
,
3822 data_count
,&flavor
,thread_state
,thread_state_count
,
3823 thread_state
,&thread_state_count
);
3825 case EXCEPTION_STATE_IDENTITY
:
3826 r
= exception_raise_state_identity(port
,thread
,task
,exception
,data
,
3827 data_count
,&flavor
,thread_state
,thread_state_count
,
3828 thread_state
,&thread_state_count
);
3831 r
= KERN_FAILURE
; /* make gcc happy */
3832 ABORT("forward_exception: unknown behavior");
3836 if(behavior
!= EXCEPTION_DEFAULT
) {
3837 r
= thread_set_state(thread
,flavor
,thread_state
,thread_state_count
);
3838 if(r
!= KERN_SUCCESS
)
3839 ABORT("thread_set_state failed in forward_exception");
3845 #define FWD() GC_forward_exception(thread,task,exception,code,code_count)
3847 /* This violates the namespace rules but there isn't anything that can be done
3848 about it. The exception handling stuff is hard coded to call this */
3850 catch_exception_raise(
3851 mach_port_t exception_port
,mach_port_t thread
,mach_port_t task
,
3852 exception_type_t exception
,exception_data_t code
,
3853 mach_msg_type_number_t code_count
3859 # if defined(POWERPC)
3860 # if CPP_WORDSZ == 32
3861 thread_state_flavor_t flavor
= PPC_EXCEPTION_STATE
;
3862 mach_msg_type_number_t exc_state_count
= PPC_EXCEPTION_STATE_COUNT
;
3863 ppc_exception_state_t exc_state
;
3865 thread_state_flavor_t flavor
= PPC_EXCEPTION_STATE64
;
3866 mach_msg_type_number_t exc_state_count
= PPC_EXCEPTION_STATE64_COUNT
;
3867 ppc_exception_state64_t exc_state
;
3869 # elif defined(I386) || defined(X86_64)
3870 # if CPP_WORDSZ == 32
3871 thread_state_flavor_t flavor
= x86_EXCEPTION_STATE32
;
3872 mach_msg_type_number_t exc_state_count
= x86_EXCEPTION_STATE32_COUNT
;
3873 x86_exception_state32_t exc_state
;
3875 thread_state_flavor_t flavor
= x86_EXCEPTION_STATE64
;
3876 mach_msg_type_number_t exc_state_count
= x86_EXCEPTION_STATE64_COUNT
;
3877 x86_exception_state64_t exc_state
;
3880 # error FIXME for non-ppc darwin
3884 if(exception
!= EXC_BAD_ACCESS
|| code
[0] != KERN_PROTECTION_FAILURE
) {
3885 #ifdef DEBUG_EXCEPTION_HANDLING
3886 /* We aren't interested, pass it on to the old handler */
3887 GC_printf3("Exception: 0x%x Code: 0x%x 0x%x in catch....\n",
3889 code_count
> 0 ? code
[0] : -1,
3890 code_count
> 1 ? code
[1] : -1);
3895 r
= thread_get_state(thread
,flavor
,
3896 (natural_t
*)&exc_state
,&exc_state_count
);
3897 if(r
!= KERN_SUCCESS
) {
3898 /* The thread is supposed to be suspended while the exception handler
3899 is called. This shouldn't fail. */
3900 #ifdef BROKEN_EXCEPTION_HANDLING
3901 GC_err_printf0("thread_get_state failed in "
3902 "catch_exception_raise\n");
3903 return KERN_SUCCESS
;
3905 ABORT("thread_get_state failed in catch_exception_raise");
3909 /* This is the address that caused the fault */
3910 #if defined(POWERPC)
3911 addr
= (char*) exc_state
. THREAD_FLD(dar
);
3912 #elif defined (I386) || defined (X86_64)
3913 addr
= (char*) exc_state
. THREAD_FLD(faultvaddr
);
3915 # error FIXME for non POWERPC/I386
3918 if((HDR(addr
)) == 0) {
3919 /* Ugh... just like the SIGBUS problem above, it seems we get a bogus
3920 KERN_PROTECTION_FAILURE every once and a while. We wait till we get
3921 a bunch in a row before doing anything about it. If a "real" fault
3922 ever occurres it'll just keep faulting over and over and we'll hit
3923 the limit pretty quickly. */
3924 #ifdef BROKEN_EXCEPTION_HANDLING
3925 static char *last_fault
;
3926 static int last_fault_count
;
3928 if(addr
!= last_fault
) {
3930 last_fault_count
= 0;
3932 if(++last_fault_count
< 32) {
3933 if(last_fault_count
== 1)
3935 "GC: WARNING: Ignoring KERN_PROTECTION_FAILURE at %p\n",
3937 return KERN_SUCCESS
;
3940 GC_err_printf1("Unexpected KERN_PROTECTION_FAILURE at %p\n",addr
);
3941 /* Can't pass it along to the signal handler because that is
3942 ignoring SIGBUS signals. We also shouldn't call ABORT here as
3943 signals don't always work too well from the exception handler. */
3944 GC_err_printf0("Aborting\n");
3946 #else /* BROKEN_EXCEPTION_HANDLING */
3947 /* Pass it along to the next exception handler
3948 (which should call SIGBUS/SIGSEGV) */
3950 #endif /* !BROKEN_EXCEPTION_HANDLING */
3953 #ifdef BROKEN_EXCEPTION_HANDLING
3954 /* Reset the number of consecutive SIGBUSs */
3955 GC_sigbus_count
= 0;
3958 if(GC_mprotect_state
== GC_MP_NORMAL
) { /* common case */
3959 h
= (struct hblk
*)((word
)addr
& ~(GC_page_size
-1));
3960 UNPROTECT(h
, GC_page_size
);
3961 for (i
= 0; i
< divHBLKSZ(GC_page_size
); i
++) {
3962 register int index
= PHT_HASH(h
+i
);
3963 async_set_pht_entry_from_index(GC_dirty_pages
, index
);
3965 } else if(GC_mprotect_state
== GC_MP_DISCARDING
) {
3966 /* Lie to the thread for now. No sense UNPROTECT()ing the memory
3967 when we're just going to PROTECT() it again later. The thread
3968 will just fault again once it resumes */
3970 /* Shouldn't happen, i don't think */
3971 GC_printf0("KERN_PROTECTION_FAILURE while world is stopped\n");
3974 return KERN_SUCCESS
;
3978 /* These should never be called, but just in case... */
3979 kern_return_t
catch_exception_raise_state(mach_port_name_t exception_port
,
3980 int exception
, exception_data_t code
, mach_msg_type_number_t codeCnt
,
3981 int flavor
, thread_state_t old_state
, int old_stateCnt
,
3982 thread_state_t new_state
, int new_stateCnt
)
3984 ABORT("catch_exception_raise_state");
3985 return(KERN_INVALID_ARGUMENT
);
3987 kern_return_t
catch_exception_raise_state_identity(
3988 mach_port_name_t exception_port
, mach_port_t thread
, mach_port_t task
,
3989 int exception
, exception_data_t code
, mach_msg_type_number_t codeCnt
,
3990 int flavor
, thread_state_t old_state
, int old_stateCnt
,
3991 thread_state_t new_state
, int new_stateCnt
)
3993 ABORT("catch_exception_raise_state_identity");
3994 return(KERN_INVALID_ARGUMENT
);
3998 #endif /* DARWIN && MPROTECT_VDB */
4000 # ifndef HAVE_INCREMENTAL_PROTECTION_NEEDS
4001 int GC_incremental_protection_needs()
4003 return GC_PROTECTS_NONE
;
4005 # endif /* !HAVE_INCREMENTAL_PROTECTION_NEEDS */
4008 * Call stack save code for debugging.
4009 * Should probably be in mach_dep.c, but that requires reorganization.
4012 /* I suspect the following works for most X86 *nix variants, so */
4013 /* long as the frame pointer is explicitly stored. In the case of gcc, */
4014 /* compiler flags (e.g. -fomit-frame-pointer) determine whether it is. */
4015 #if defined(I386) && defined(LINUX) && defined(SAVE_CALL_CHAIN)
4016 # include <features.h>
4019 struct frame
*fr_savfp
;
4021 long fr_arg
[NARGS
]; /* All the arguments go here. */
4027 # include <features.h>
4032 struct frame
*fr_savfp
;
4041 # if defined(SUNOS4)
4042 # include <machine/frame.h>
4044 # if defined (DRSNX)
4045 # include <sys/sparc/frame.h>
4047 # if defined(OPENBSD)
4050 # if defined(FREEBSD) || defined(NETBSD)
4051 # include <machine/frame.h>
4053 # include <sys/frame.h>
4060 --> We only know how to to get the first
6 arguments
4064 #ifdef NEED_CALLINFO
4065 /* Fill in the pc and argument information for up to NFRAMES of my */
4066 /* callers. Ignore my frame and my callers frame. */
4069 # include <unistd.h>
4072 #endif /* NEED_CALLINFO */
4074 #if defined(GC_HAVE_BUILTIN_BACKTRACE)
4075 # include <execinfo.h>
4078 #ifdef SAVE_CALL_CHAIN
4080 #if NARGS == 0 && NFRAMES % 2 == 0 /* No padding */ \
4081 && defined(GC_HAVE_BUILTIN_BACKTRACE)
4083 #ifdef REDIRECT_MALLOC
4084 /* Deal with possible malloc calls in backtrace by omitting */
4085 /* the infinitely recursing backtrace. */
4087 __thread
/* If your compiler doesn't understand this */
4088 /* you could use something like pthread_getspecific. */
4090 GC_in_save_callers
= FALSE
;
4093 void GC_save_callers (info
)
4094 struct callinfo info
[NFRAMES
];
4096 void * tmp_info
[NFRAMES
+ 1];
4098 # define IGNORE_FRAMES 1
4100 /* We retrieve NFRAMES+1 pc values, but discard the first, since it */
4101 /* points to our own frame. */
4102 # ifdef REDIRECT_MALLOC
4103 if (GC_in_save_callers
) {
4104 info
[0].ci_pc
= (word
)(&GC_save_callers
);
4105 for (i
= 1; i
< NFRAMES
; ++i
) info
[i
].ci_pc
= 0;
4108 GC_in_save_callers
= TRUE
;
4110 GC_ASSERT(sizeof(struct callinfo
) == sizeof(void *));
4111 npcs
= backtrace((void **)tmp_info
, NFRAMES
+ IGNORE_FRAMES
);
4112 BCOPY(tmp_info
+IGNORE_FRAMES
, info
, (npcs
- IGNORE_FRAMES
) * sizeof(void *));
4113 for (i
= npcs
- IGNORE_FRAMES
; i
< NFRAMES
; ++i
) info
[i
].ci_pc
= 0;
4114 # ifdef REDIRECT_MALLOC
4115 GC_in_save_callers
= FALSE
;
4119 #else /* No builtin backtrace; do it ourselves */
4121 #if (defined(OPENBSD) || defined(NETBSD) || defined(FREEBSD)) && defined(SPARC)
4122 # define FR_SAVFP fr_fp
4123 # define FR_SAVPC fr_pc
4125 # define FR_SAVFP fr_savfp
4126 # define FR_SAVPC fr_savpc
4129 #if defined(SPARC) && (defined(__arch64__) || defined(__sparcv9))
4135 void GC_save_callers (info
)
4136 struct callinfo info
[NFRAMES
];
4138 struct frame
*frame
;
4142 /* We assume this is turned on only with gcc as the compiler. */
4143 asm("movl %%ebp,%0" : "=r"(frame
));
4146 frame
= (struct frame
*) GC_save_regs_in_stack ();
4147 fp
= (struct frame
*)((long) frame
-> FR_SAVFP
+ BIAS
);
4150 for (; (!(fp HOTTER_THAN frame
) && !(GC_stackbottom
HOTTER_THAN (ptr_t
)fp
)
4151 && (nframes
< NFRAMES
));
4152 fp
= (struct frame
*)((long) fp
-> FR_SAVFP
+ BIAS
), nframes
++) {
4155 info
[nframes
].ci_pc
= fp
->FR_SAVPC
;
4157 for (i
= 0; i
< NARGS
; i
++) {
4158 info
[nframes
].ci_arg
[i
] = ~(fp
->fr_arg
[i
]);
4160 # endif /* NARGS > 0 */
4162 if (nframes
< NFRAMES
) info
[nframes
].ci_pc
= 0;
4165 #endif /* No builtin backtrace */
4167 #endif /* SAVE_CALL_CHAIN */
4169 #ifdef NEED_CALLINFO
4171 /* Print info to stderr. We do NOT hold the allocation lock */
4172 void GC_print_callers (info
)
4173 struct callinfo info
[NFRAMES
];
4176 static int reentry_count
= 0;
4177 GC_bool stop
= FALSE
;
4179 /* FIXME: This should probably use a different lock, so that we */
4180 /* become callable with or without the allocation lock. */
4186 GC_err_printf0("\tCaller at allocation:\n");
4188 GC_err_printf0("\tCall chain at allocation:\n");
4190 for (i
= 0; i
< NFRAMES
&& !stop
; i
++) {
4191 if (info
[i
].ci_pc
== 0) break;
4196 GC_err_printf0("\t\targs: ");
4197 for (j
= 0; j
< NARGS
; j
++) {
4198 if (j
!= 0) GC_err_printf0(", ");
4199 GC_err_printf2("%d (0x%X)", ~(info
[i
].ci_arg
[j
]),
4200 ~(info
[i
].ci_arg
[j
]));
4202 GC_err_printf0("\n");
4205 if (reentry_count
> 1) {
4206 /* We were called during an allocation during */
4207 /* a previous GC_print_callers call; punt. */
4208 GC_err_printf1("\t\t##PC##= 0x%lx\n", info
[i
].ci_pc
);
4215 # if defined(GC_HAVE_BUILTIN_BACKTRACE) \
4216 && !defined(GC_BACKTRACE_SYMBOLS_BROKEN)
4218 backtrace_symbols((void **)(&(info
[i
].ci_pc
)), 1);
4219 char *name
= sym_name
[0];
4223 sprintf(buf
, "##PC##= 0x%lx", info
[i
].ci_pc
);
4225 # if defined(LINUX) && !defined(SMALL_CONFIG)
4226 /* Try for a line number. */
4229 static char exe_name
[EXE_SZ
];
4231 char cmd_buf
[CMD_SZ
];
4232 # define RESULT_SZ 200
4233 static char result_buf
[RESULT_SZ
];
4236 # define PRELOAD_SZ 200
4237 char preload_buf
[PRELOAD_SZ
];
4238 static GC_bool found_exe_name
= FALSE
;
4239 static GC_bool will_fail
= FALSE
;
4241 /* Try to get it via a hairy and expensive scheme. */
4242 /* First we get the name of the executable: */
4243 if (will_fail
) goto out
;
4244 if (!found_exe_name
) {
4245 ret_code
= readlink("/proc/self/exe", exe_name
, EXE_SZ
);
4246 if (ret_code
< 0 || ret_code
>= EXE_SZ
4247 || exe_name
[0] != '/') {
4248 will_fail
= TRUE
; /* Dont try again. */
4251 exe_name
[ret_code
] = '\0';
4252 found_exe_name
= TRUE
;
4254 /* Then we use popen to start addr2line -e <exe> <addr> */
4255 /* There are faster ways to do this, but hopefully this */
4256 /* isn't time critical. */
4257 sprintf(cmd_buf
, "/usr/bin/addr2line -f -e %s 0x%lx", exe_name
,
4258 (unsigned long)info
[i
].ci_pc
);
4259 old_preload
= getenv ("LD_PRELOAD");
4260 if (0 != old_preload
) {
4261 if (strlen (old_preload
) >= PRELOAD_SZ
) {
4265 strcpy (preload_buf
, old_preload
);
4266 unsetenv ("LD_PRELOAD");
4268 pipe
= popen(cmd_buf
, "r");
4269 if (0 != old_preload
4270 && 0 != setenv ("LD_PRELOAD", preload_buf
, 0)) {
4271 WARN("Failed to reset LD_PRELOAD\n", 0);
4274 || (result_len
= fread(result_buf
, 1, RESULT_SZ
- 1, pipe
))
4276 if (pipe
!= NULL
) pclose(pipe
);
4280 if (result_buf
[result_len
- 1] == '\n') --result_len
;
4281 result_buf
[result_len
] = 0;
4282 if (result_buf
[0] == '?'
4283 || result_buf
[result_len
-2] == ':'
4284 && result_buf
[result_len
-1] == '0') {
4288 /* Get rid of embedded newline, if any. Test for "main" */
4290 char * nl
= strchr(result_buf
, '\n');
4291 if (nl
!= NULL
&& nl
< result_buf
+ result_len
) {
4294 if (strncmp(result_buf
, "main", nl
- result_buf
) == 0) {
4298 if (result_len
< RESULT_SZ
- 25) {
4299 /* Add in hex address */
4300 sprintf(result_buf
+ result_len
, " [0x%lx]",
4301 (unsigned long)info
[i
].ci_pc
);
4308 GC_err_printf1("\t\t%s\n", name
);
4309 # if defined(GC_HAVE_BUILTIN_BACKTRACE) \
4310 && !defined(GC_BACKTRACE_SYMBOLS_BROKEN)
4311 free(sym_name
); /* May call GC_free; that's OK */
4320 #endif /* NEED_CALLINFO */
4324 #if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
4326 /* Dump /proc/self/maps to GC_stderr, to enable looking up names for
4327 addresses in FIND_LEAK output. */
4329 static word
dump_maps(char *maps
)
4331 GC_err_write(maps
, strlen(maps
));
4335 void GC_print_address_map()
4337 GC_err_printf0("---------- Begin address map ----------\n");
4338 GC_apply_to_maps(dump_maps
);
4339 GC_err_printf0("---------- End address map ----------\n");