Debian Codegraph @

gcc/testsuite/gdc.test/runnable/testptrref.d

raw
1
2 version(CRuntime_Microsoft)
3 {
4 extern(C)
5 {
6 extern __gshared void* __ImageBase;
7 extern __gshared uint _DP_beg;
8 extern __gshared uint _DP_end;
9 extern __gshared uint _TP_beg;
10 extern __gshared uint _TP_end;
11 }
12 alias _DPbegin = _DP_beg;
13 alias _DPend = _DP_end;
14 alias _TPbegin = _TP_beg;
15 alias _TPend = _TP_end;
16
17 __gshared void[] dataSection;
18 shared static this()
19 {
20 import core.internal.traits : externDFunc;
21 alias findImageSection = externDFunc!("rt.sections_win64.findImageSection",
22 void[] function(void* handle, string name) nothrow @nogc);
23 dataSection = findImageSection(&__ImageBase, ".data");
24 }
25
26 void[] tlsRange;
27 static this()
28 {
29 import core.internal.traits : externDFunc;
30 alias initTLSRanges = externDFunc!("rt.sections_win64.initTLSRanges",
31 void[] function() nothrow @nogc);
32 tlsRange = initTLSRanges();
33 }
34
35 version = ptrref_supported;
36 }
37 else version(Win32)
38 {
39 extern(C)
40 {
41 extern __gshared void* _DPbegin;
42 extern __gshared void* _DPend;
43 extern __gshared uint _TPbegin;
44 extern __gshared uint _TPend;
45 extern int _tlsstart;
46 extern int _tlsend;
47 }
48
49 void[] tlsRange;
50 static this()
51 {
52 tlsRange = (cast(void*)&_tlsstart)[0.. cast(void*)&_tlsend - cast(void*)&_tlsstart];
53 }
54
55 version = ptrref_supported;
56 }
57
58 struct Struct
59 {
60 int x;
61 Struct* next;
62 }
63
64 class Class
65 {
66 void* ptr;
67 }
68
69 struct Struc(T)
70 {
71 static T vtls;
72 static __gshared T vgshared;
73 }
74
75 __gshared Struct* gsharedStrctPtr2 = new Struct(7, new Struct(8, null));
76
77 int tlsInt;
78 void* tlsVar;
79
80 shared int sharedInt;
81 shared void* sharedVar;
82 __gshared void* gsharedVar;
83 __gshared void* gsharedVar2;
84 immutable int[] arr = [1, 2, 3];
85 string tlsStr;
86
87 __gshared Struct gsharedStrct;
88 Struct[3] tlsStrcArr;
89 Class tlsClss;
90
91 // expression initializers
92 string[] strArr = [ "a", "b" ];
93 __gshared Class gsharedClss = new Class;
94 __gshared Struct* gsharedStrctPtr = new Struct(7, new Struct(8, null));
95
96 debug(PRINT) import core.stdc.stdio;
97
98 void main()
99 {
100 version(ptrref_supported)
101 testRefPtr();
102 }
103
104 version(ptrref_supported):
105
106 bool findTlsPtr(const(void)* ptr)
107 {
108 debug(PRINT) printf("findTlsPtr %p\n", ptr);
109 for (uint* p = &_TPbegin; p < &_TPend; p++)
110 {
111 void* addr = tlsRange.ptr + *p;
112 debug(PRINT) printf(" try %p\n", addr);
113 assert(*p < tlsRange.length);
114 if (addr == ptr)
115 return true;
116 }
117 return false;
118 }
119
120 bool findDataPtr(const(void)* ptr)
121 {
122 debug(PRINT) printf("findDataPtr %p\n", ptr);
123 for (auto p = &_DPbegin; p < &_DPend; p++)
124 {
125 debug(PRINT) printf(" try %p\n", cast(void*) cast(size_t) *p);
126 version(CRuntime_Microsoft)
127 void* addr = dataSection.ptr + *p;
128 else
129 void* addr = *p;
130
131 if (addr == ptr)
132 return true;
133 }
134 return false;
135 }
136
137 void testRefPtr()
138 {
139 debug(PRINT) printf("&_DPbegin %p\n", &_DPbegin);
140 debug(PRINT) printf("&_DPend %p\n", &_DPend);
141
142 debug(PRINT) printf("&_TPbegin %p\n", &_TPbegin);
143 debug(PRINT) printf("&_TPend %p\n", &_TPend);
144
145 assert(!findDataPtr(cast(void*)&sharedInt));
146 assert(!findTlsPtr(&tlsInt));
147
148 assert(findDataPtr(cast(void*)&sharedVar));
149 assert(findDataPtr(&gsharedVar));
150 assert(findDataPtr(&gsharedStrct.next));
151 assert(findDataPtr(&(gsharedClss)));
152 assert(findDataPtr(&(gsharedClss.ptr)));
153
154 assert(findTlsPtr(&tlsVar));
155 assert(findTlsPtr(&tlsClss));
156 assert(findTlsPtr(&tlsStrcArr[0].next));
157 assert(findTlsPtr(&tlsStrcArr[1].next));
158 assert(findTlsPtr(&tlsStrcArr[2].next));
159
160 assert(!findTlsPtr(cast(size_t*)&tlsStr)); // length
161 assert(findTlsPtr(cast(size_t*)&tlsStr + 1)); // ptr
162
163 // monitor is manually managed
164 assert(!findDataPtr(cast(size_t*)cast(void*)Class.classinfo + 1));
165 assert(!findDataPtr(cast(size_t*)cast(void*)Class.classinfo + 1));
166
167 assert(!findDataPtr(&arr));
168 assert(!findTlsPtr(&arr));
169 assert(!findDataPtr(cast(size_t*)&arr + 1));
170 assert(!findTlsPtr(cast(size_t*)&arr + 1));
171
172 assert(findDataPtr(cast(size_t*)&strArr[0] + 1)); // ptr in _DATA!
173 assert(findDataPtr(cast(size_t*)&strArr[1] + 1)); // ptr in _DATA!
174 strArr[1] = "c";
175
176 assert(findDataPtr(&gsharedStrctPtr));
177 assert(findDataPtr(&gsharedStrctPtr.next));
178 assert(findDataPtr(&gsharedStrctPtr.next.next));
179
180 assert(findDataPtr(&(Struc!(int*).vgshared)));
181 assert(!findDataPtr(&(Struc!(int).vgshared)));
182 assert(findTlsPtr(&(Struc!(int*).vtls)));
183 assert(!findTlsPtr(&(Struc!(int).vtls)));
184 }