Debian Codegraph @

tools/setjmp_t.c

raw
1 /*
2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 /* Check whether setjmp actually saves registers in jmp_buf. */
15 /* If it doesn't, the generic mark_regs code won't work. */
16 /* Compilers vary as to whether they will put x in a */
17 /* (callee-save) register without -O. The code is */
18 /* contrived such that any decent compiler should put x in */
19 /* a callee-save register with -O. Thus it is */
20 /* recommended that this be run optimized. (If the machine */
21 /* has no callee-save registers, then the generic code is */
22 /* safe, but this will not be noticed by this piece of */
23 /* code.) This test appears to be far from perfect. */
24 #include <stdio.h>
25 #include <setjmp.h>
26 #include <string.h>
27
28 #define NOT_GCBUILD
29 #include "private/gc_priv.h"
30
31 #ifdef OS2
32 /* GETPAGESIZE() is set to getpagesize() by default, but that */
33 /* doesn't really exist, and the collector doesn't need it. */
34 #define INCL_DOSFILEMGR
35 #define INCL_DOSMISC
36 #define INCL_DOSERRORS
37 #include <os2.h>
38
39 int getpagesize(void)
40 {
41 ULONG result[1];
42
43 if (DosQuerySysInfo(QSV_PAGE_SIZE, QSV_PAGE_SIZE,
44 (void *)result, sizeof(ULONG)) != NO_ERROR) {
45 fprintf(stderr, "DosQuerySysInfo failed\n");
46 result[0] = 4096;
47 }
48 return((int)(result[0]));
49 }
50
51 #elif defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
52 int getpagesize(void)
53 {
54 SYSTEM_INFO sysinfo;
55 GetSystemInfo(&sysinfo);
56 return sysinfo.dwPageSize;
57 }
58 #endif
59
60 struct a_s {
61 char a_a;
62 char * a_b;
63 } a;
64
65 GC_ATTR_NOINLINE
66 word nested_sp(void)
67 {
68 # if GC_GNUC_PREREQ(4, 0)
69 return (word)__builtin_frame_address(0);
70 # else
71 volatile word sp;
72 sp = (word)(&sp);
73 return sp;
74 # endif
75 }
76
77 /* To prevent nested_sp inlining. */
78 word (*volatile nested_sp_fn)(void) = nested_sp;
79
80 int g(int x);
81
82 #if defined(CPPCHECK) || !defined(__cplusplus)
83 const char *a_str = "a";
84 #else
85 # define a_str "a"
86 #endif
87
88 int main(void)
89 {
90 volatile word sp;
91 unsigned ps = GETPAGESIZE();
92 JMP_BUF b;
93 # if (!defined(__cplusplus) || __cplusplus < 201703L /* before c++17 */) \
94 && (!defined(__GNUC__) || defined(__OPTIMIZE__))
95 register
96 # endif
97 int x = (int)strlen(a_str); /* 1, slightly disguised */
98 static volatile int y = 0;
99
100 sp = (word)(&sp);
101 printf("This appears to be a %s running %s\n", MACH_TYPE, OS_TYPE);
102 # if defined(CPPCHECK)
103 (void)nested_sp(); /* to workaround a bug in cppcheck */
104 # endif
105 if (nested_sp_fn() < sp) {
106 printf("Stack appears to grow down, which is the default.\n");
107 printf("A good guess for STACKBOTTOM on this machine is 0x%lx.\n",
108 ((unsigned long)sp + ps) & ~(unsigned long)(ps-1));
109 } else {
110 printf("Stack appears to grow up.\n");
111 printf("Define STACK_GROWS_UP in gc_priv.h\n");
112 printf("A good guess for STACKBOTTOM on this machine is 0x%lx.\n",
113 (unsigned long)sp & ~(unsigned long)(ps-1)); /* round down */
114 }
115 printf("Note that this may vary between machines of ostensibly\n");
116 printf("the same architecture (e.g. Sun 3/50s and 3/80s).\n");
117 printf("On many machines the value is not fixed.\n");
118 printf("A good guess for ALIGNMENT on this machine is %lu.\n",
119 (unsigned long)((word)(&(a.a_b)) - (word)(&a)));
120
121 printf("The following is a very dubious test of one root marking"
122 " strategy.\n");
123 printf("Results may not be accurate/useful:\n");
124 /* Encourage the compiler to keep x in a callee-save register */
125 x = 2*x-1;
126 printf("\n");
127 x = 2*x-1;
128 (void)SETJMP(b);
129 if (y == 1) {
130 if (x == 2) {
131 printf("Setjmp-based generic code to push registers probably\n"
132 "will not work. But we rarely try that anymore.\n"
133 "If you have getcontext(), this probably does not matter.\n");
134 } else if (x == 1) {
135 printf("Setjmp-based register marking code may work.\n");
136 } else {
137 printf("Very strange setjmp implementation.\n");
138 }
139 }
140 y++;
141 x = 2;
142 if (y == 1) LONGJMP(b, 1);
143 printf("Some GC internal configuration stuff: \n");
144 printf("\tWORDSZ = %lu, ALIGNMENT = %d, GC_GRANULE_BYTES = %d\n",
145 (unsigned long)WORDSZ, ALIGNMENT, GC_GRANULE_BYTES);
146 printf("\tUsing one mark ");
147 # if defined(USE_MARK_BYTES)
148 printf("byte");
149 # else
150 printf("bit");
151 # endif
152 printf(" per ");
153 # if defined(MARK_BIT_PER_OBJ)
154 printf("object.\n");
155 # elif defined(MARK_BIT_PER_GRANULE)
156 printf("granule.\n");
157 # endif
158 # ifdef THREAD_LOCAL_ALLOC
159 printf("Thread local allocation enabled.\n");
160 # endif
161 # ifdef PARALLEL_MARK
162 printf("Parallel marking enabled.\n");
163 # endif
164 (void)g(x);
165 return(0);
166 }
167
168 int g(int x)
169 {
170 return(x);
171 }