@@ -90,32 +90,15 @@ public class LinuxImageHeapProvider extends AbstractImageHeapProvider {
90
90
private static final SignedWord UNASSIGNED_FD = signed (-1 );
91
91
private static final SignedWord CANNOT_OPEN_FD = signed (-2 );
92
92
private static final CGlobalData <WordPointer > CACHED_IMAGE_FD = CGlobalDataFactory .createWord (UNASSIGNED_FD );
93
- private static final CGlobalData <WordPointer > CACHED_IMAGE_HEAP_OFFSET = CGlobalDataFactory .createWord ();
93
+ private static final CGlobalData <WordPointer > CACHED_IMAGE_HEAP_OFFSET_IN_FILE = CGlobalDataFactory .createWord ();
94
+
95
+ private static final int MAX_PATHLEN = 4096 ;
94
96
95
97
@ Override
96
98
public boolean guaranteesHeapPreferredAddressSpaceAlignment () {
97
99
return true ;
98
100
}
99
101
100
- @ Override
101
- @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
102
- protected UnsignedWord getImageHeapAddressSpaceSize () {
103
- int imageHeapOffset = Heap .getHeap ().getImageHeapOffsetInAddressSpace ();
104
- assert imageHeapOffset >= 0 ;
105
- UnsignedWord size = WordFactory .unsigned (imageHeapOffset );
106
- size = size .add (getImageHeapSizeInFile (IMAGE_HEAP_BEGIN .get (), IMAGE_HEAP_END .get ()));
107
- return size ;
108
- }
109
-
110
- @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
111
- protected UnsignedWord getTotalRequiredAddressSpaceSize () {
112
- UnsignedWord size = getImageHeapAddressSpaceSize ();
113
- if (DynamicMethodAddressResolutionHeapSupport .isEnabled ()) {
114
- size = size .add (DynamicMethodAddressResolutionHeapSupport .get ().getDynamicMethodAddressResolverPreHeapMemoryBytes ());
115
- }
116
- return size ;
117
- }
118
-
119
102
@ Override
120
103
@ Uninterruptible (reason = "Called during isolate initialization." )
121
104
public int initialize (Pointer reservedAddressSpace , UnsignedWord reservedSize , WordPointer basePointer , WordPointer endPointer ) {
@@ -135,15 +118,14 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
135
118
Pointer heapBase ;
136
119
Pointer selfReservedHeapBase ;
137
120
if (DynamicMethodAddressResolutionHeapSupport .isEnabled ()) {
138
- UnsignedWord preHeapRequiredBytes = DynamicMethodAddressResolutionHeapSupport . get (). getDynamicMethodAddressResolverPreHeapMemoryBytes ();
121
+ UnsignedWord preHeapRequiredBytes = getPreHeapAlignedSizeForDynamicMethodAddressResolver ();
139
122
if (selfReservedMemory .isNonNull ()) {
140
123
selfReservedHeapBase = selfReservedMemory .add (preHeapRequiredBytes );
141
124
heapBase = selfReservedHeapBase ;
142
125
} else {
143
126
heapBase = reservedAddressSpace .add (preHeapRequiredBytes );
144
127
selfReservedHeapBase = WordFactory .nullPointer ();
145
128
}
146
- assert heapBase .isNonNull ();
147
129
remainingSize = remainingSize .subtract (preHeapRequiredBytes );
148
130
149
131
int error = DynamicMethodAddressResolutionHeapSupport .get ().initialize ();
@@ -152,8 +134,7 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
152
134
return error ;
153
135
}
154
136
155
- Pointer installOffset = heapBase .subtract (DynamicMethodAddressResolutionHeapSupport .get ().getRequiredPreHeapMemoryInBytes ());
156
- error = DynamicMethodAddressResolutionHeapSupport .get ().install (installOffset );
137
+ error = DynamicMethodAddressResolutionHeapSupport .get ().install (heapBase );
157
138
if (error != CEntryPointErrors .NO_ERROR ) {
158
139
freeImageHeap (selfReservedHeapBase );
159
140
return error ;
@@ -165,10 +146,10 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
165
146
166
147
int imageHeapOffsetInAddressSpace = Heap .getHeap ().getImageHeapOffsetInAddressSpace ();
167
148
basePointer .write (heapBase );
168
- Pointer firstBase = heapBase .add (imageHeapOffsetInAddressSpace );
149
+ Pointer imageHeapStart = heapBase .add (imageHeapOffsetInAddressSpace );
169
150
remainingSize = remainingSize .subtract (imageHeapOffsetInAddressSpace );
170
- int result = initializeImageHeap (firstBase , remainingSize , endPointer ,
171
- CACHED_IMAGE_FD .get (), CACHED_IMAGE_HEAP_OFFSET .get (), MAGIC .get (),
151
+ int result = initializeImageHeap (imageHeapStart , remainingSize , endPointer ,
152
+ CACHED_IMAGE_FD .get (), CACHED_IMAGE_HEAP_OFFSET_IN_FILE .get (), MAGIC .get (),
172
153
IMAGE_HEAP_BEGIN .get (), IMAGE_HEAP_END .get (),
173
154
IMAGE_HEAP_RELOCATABLE_BEGIN .get (), IMAGE_HEAP_A_RELOCATABLE_POINTER .get (), IMAGE_HEAP_RELOCATABLE_END .get (),
174
155
IMAGE_HEAP_WRITABLE_BEGIN .get (), IMAGE_HEAP_WRITABLE_END .get ());
@@ -179,7 +160,7 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
179
160
}
180
161
181
162
@ Uninterruptible (reason = "Called during isolate initialization." )
182
- private int initializeImageHeap (Pointer imageHeap , UnsignedWord reservedSize , WordPointer endPointer , WordPointer cachedFd , WordPointer cachedOffset ,
163
+ private int initializeImageHeap (Pointer imageHeap , UnsignedWord reservedSize , WordPointer endPointer , WordPointer cachedFd , WordPointer cachedOffsetInFile ,
183
164
Pointer magicAddress , Word heapBeginSym , Word heapEndSym , Word heapRelocsSym , Pointer heapAnyRelocPointer , Word heapRelocsEndSym , Word heapWritableSym , Word heapWritableEndSym ) {
184
165
assert heapBeginSym .belowOrEqual (heapWritableSym ) && heapWritableSym .belowOrEqual (heapWritableEndSym ) && heapWritableEndSym .belowOrEqual (heapEndSym );
185
166
assert heapBeginSym .belowOrEqual (heapRelocsSym ) && heapRelocsSym .belowOrEqual (heapRelocsEndSym ) && heapRelocsEndSym .belowOrEqual (heapEndSym );
@@ -193,7 +174,7 @@ private int initializeImageHeap(Pointer imageHeap, UnsignedWord reservedSize, Wo
193
174
* step to avoid stalling threads.
194
175
*/
195
176
if (fd .equal (UNASSIGNED_FD )) {
196
- int opened = openImageFile (cachedOffset , heapBeginSym , heapRelocsSym , magicAddress );
177
+ int opened = openImageFile (heapBeginSym , magicAddress , cachedOffsetInFile );
197
178
SignedWord previous = ((Pointer ) cachedFd ).compareAndSwapWord (0 , fd , signed (opened ), LocationIdentity .ANY_LOCATION );
198
179
if (previous .equal (fd )) {
199
180
fd = signed (opened );
@@ -221,7 +202,7 @@ private int initializeImageHeap(Pointer imageHeap, UnsignedWord reservedSize, Wo
221
202
}
222
203
223
204
// Create memory mappings from the image file.
224
- UnsignedWord fileOffset = cachedOffset .read ();
205
+ UnsignedWord fileOffset = cachedOffsetInFile .read ();
225
206
imageHeap = VirtualMemoryProvider .get ().mapFile (imageHeap , imageHeapSize , fd , fileOffset , Access .READ );
226
207
if (imageHeap .isNull ()) {
227
208
return CEntryPointErrors .MAP_HEAP_FAILED ;
@@ -232,8 +213,8 @@ private int initializeImageHeap(Pointer imageHeap, UnsignedWord reservedSize, Wo
232
213
ComparableWord mappedValue = imageHeap .readWord (heapAnyRelocPointer .subtract (heapBeginSym ));
233
214
if (relocatedValue .notEqual (mappedValue )) {
234
215
/*
235
- * Addresses were relocated by dynamic linker, so copy them, but first remap the
236
- * pages to avoid swapping them in from disk.
216
+ * Addresses were relocated by the dynamic linker, so copy them, but first remap the
217
+ * pages as anonymous memory to avoid swapping in part of the image from disk.
237
218
*/
238
219
Pointer relocsBegin = imageHeap .add (heapRelocsSym .subtract (heapBeginSym ));
239
220
UnsignedWord relocsSize = heapRelocsEndSym .subtract (heapRelocsSym );
@@ -283,27 +264,24 @@ private static int initializeImageHeapByCopying(Pointer imageHeap, Word heapBegi
283
264
284
265
/**
285
266
* Locate our image file, containing the image heap. Unfortunately we must open it by its path.
286
- *
287
- * NOTE: we look for the relocatables partition of the linker-mapped heap because it always
288
- * stays mapped, while the rest of the linker-mapped heap can be unmapped after tearing down the
289
- * first isolate. We do not use /proc/self/exe because it breaks with some tools like Valgrind.
267
+ * We do not use /proc/self/exe because it breaks with some tools like Valgrind.
290
268
*/
291
269
@ Uninterruptible (reason = "Called during isolate initialization." )
292
- private static int openImageFile (WordPointer cachedOffset , Word heapBeginSym , Word heapRelocsSym , Pointer magicAddress ) {
270
+ private static int openImageFile (Word heapBeginSym , Pointer magicAddress , WordPointer cachedImageHeapOffsetInFile ) {
293
271
final int failfd = (int ) CANNOT_OPEN_FD .rawValue ();
294
272
int mapfd = Fcntl .NoTransitions .open (PROC_SELF_MAPS .get (), Fcntl .O_RDONLY (), 0 );
295
273
if (mapfd == -1 ) {
296
274
return failfd ;
297
275
}
298
- final int bufferSize = 4096 ; // assumed MAX_PATHLEN
276
+ final int bufferSize = MAX_PATHLEN ;
299
277
CCharPointer buffer = StackValue .get (bufferSize );
300
278
301
279
// The relocatables partition might stretch over two adjacent mappings due to permission
302
280
// differences, so only locate the mapping for the first page of relocatables
303
281
UnsignedWord pageSize = VirtualMemoryProvider .get ().getGranularity ();
304
- WordPointer relocsMappingStart = StackValue .get (WordPointer .class );
305
- WordPointer relocsMappingFileOffset = StackValue .get (WordPointer .class );
306
- boolean found = findMapping (mapfd , buffer , bufferSize , heapRelocsSym , heapRelocsSym .add (pageSize ), relocsMappingStart , relocsMappingFileOffset , true );
282
+ WordPointer imageHeapMappingStart = StackValue .get (WordPointer .class );
283
+ WordPointer imageHeapMappingFileOffset = StackValue .get (WordPointer .class );
284
+ boolean found = findMapping (mapfd , buffer , bufferSize , heapBeginSym , heapBeginSym .add (pageSize ), imageHeapMappingStart , imageHeapMappingFileOffset , true );
307
285
if (!found ) {
308
286
Unistd .NoTransitions .close (mapfd );
309
287
return failfd ;
@@ -321,10 +299,9 @@ private static int openImageFile(WordPointer cachedOffset, Word heapBeginSym, Wo
321
299
return failfd ;
322
300
}
323
301
324
- Word imageHeapRelocsOffset = heapRelocsSym .subtract (heapBeginSym );
325
- Word imageHeapOffset = heapRelocsSym .subtract (relocsMappingStart .read ()).subtract (imageHeapRelocsOffset );
326
- UnsignedWord fileOffset = imageHeapOffset .add (relocsMappingFileOffset .read ());
327
- cachedOffset .write (fileOffset );
302
+ Word imageHeapOffsetInMapping = heapBeginSym .subtract (imageHeapMappingStart .read ());
303
+ UnsignedWord imageHeapOffsetInFile = imageHeapOffsetInMapping .add (imageHeapMappingFileOffset .read ());
304
+ cachedImageHeapOffsetInFile .write (imageHeapOffsetInFile );
328
305
return opened ;
329
306
}
330
307
@@ -368,17 +345,12 @@ public int freeImageHeap(PointerBase heapBase) {
368
345
UnsignedWord totalAddressSpaceSize = getTotalRequiredAddressSpaceSize ();
369
346
Pointer addressSpaceStart = (Pointer ) heapBase ;
370
347
if (DynamicMethodAddressResolutionHeapSupport .isEnabled ()) {
371
- UnsignedWord preHeapRequiredBytes = DynamicMethodAddressResolutionHeapSupport . get (). getDynamicMethodAddressResolverPreHeapMemoryBytes ();
348
+ UnsignedWord preHeapRequiredBytes = getPreHeapAlignedSizeForDynamicMethodAddressResolver ();
372
349
addressSpaceStart = addressSpaceStart .subtract (preHeapRequiredBytes );
373
350
}
374
351
if (VirtualMemoryProvider .get ().free (addressSpaceStart , totalAddressSpaceSize ) != 0 ) {
375
352
return CEntryPointErrors .FREE_IMAGE_HEAP_FAILED ;
376
353
}
377
354
return CEntryPointErrors .NO_ERROR ;
378
355
}
379
-
380
- @ Override
381
- protected UnsignedWord getImageHeapSizeInFile () {
382
- throw VMError .shouldNotReachHere ("use the variant that takes pointers" );
383
- }
384
356
}
0 commit comments