-
Notifications
You must be signed in to change notification settings - Fork 751
/
Copy pathjvmfree.c
392 lines (329 loc) · 13.4 KB
/
jvmfree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*******************************************************************************
* Copyright (c) 1991, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include <string.h>
#include "j9.h"
#include "j9consts.h"
#include "omrlinkedlist.h"
#include "j9protos.h"
#include "j9port.h"
#include "omrthread.h"
#include "j9user.h"
#include "jimage.h"
#include "jni.h"
#include "../util/ut_module.h"
#include "jvmstackusage.h"
#include "omrutilbase.h"
#undef UT_MODULE_LOADED
#undef UT_MODULE_UNLOADED
#include "ut_j9vm.h"
#include "vm_internal.h"
#include "vmaccess.h"
#include "vmhook_internal.h"
#if defined(J9VM_INTERP_VERBOSE)
/* verbose messages for displaying stack info */
#include "verbosenls.h"
#endif
static void trcModulesFreeJ9ModuleEntry(J9JavaVM *javaVM, J9Module *j9module);
void
freeClassLoaderEntries(J9VMThread * vmThread, J9ClassPathEntry * entries, UDATA count)
{
/* free memory allocated to class path entries */
J9JavaVM *vm = vmThread->javaVM;
J9TranslationBufferSet *dynLoadBuffers = vm->dynamicLoadBuffers;
U_32 i = 0;
J9ClassPathEntry *cpEntry = entries;
PORT_ACCESS_FROM_VMC(vmThread);
Trc_VM_freeClassLoaderEntries_Entry(vmThread, entries, count);
for (i = 0; i < count; i++) {
if (NULL != cpEntry->extraInfo) {
switch(cpEntry->type) {
#if defined(J9VM_OPT_ZIP_SUPPORT) && defined(J9VM_OPT_DYNAMIC_LOAD_SUPPORT)
/* If there is a J9ZipFile allocated -- free it too */
case CPE_TYPE_JAR:
dynLoadBuffers->closeZipFileFunction(&vm->vmInterface, (void *) (cpEntry->extraInfo));
j9mem_free_memory(cpEntry->extraInfo);
break;
#endif
case CPE_TYPE_JIMAGE:
vm->jimageIntf->jimageClose(vm->jimageIntf, (UDATA)cpEntry->extraInfo);
break;
default:
/* Do nothing */
break;
}
cpEntry->extraInfo = NULL;
}
cpEntry->path = NULL;
cpEntry->pathLength = 0;
cpEntry++;
}
j9mem_free_memory(entries);
Trc_VM_freeClassLoaderEntries_Exit(vmThread);
}
/**
* For every non-system class loaders' classpath entries,
* an entry is added to shared cache's CPPool to be able to reuse it.
* When classloader is freed, this function is being called to remove the CPPool entry
* from shared cache associated with the freed classloader's classpath entries.
*/
void
freeSharedCacheCLEntries(J9VMThread * vmThread, J9ClassLoader * classloader)
{
/* free memory allocated to class path entries */
J9JavaVM * vm = vmThread->javaVM;
J9SharedClassConfig *sharedClassConfig = vm->sharedClassConfig;
J9Pool* cpCachePool;
PORT_ACCESS_FROM_VMC(vmThread);
Trc_VM_freeSharedCacheCLEntries_Entry(vmThread, classloader);
omrthread_monitor_enter(sharedClassConfig->jclCacheMutex);
cpCachePool = sharedClassConfig->jclClasspathCache;
if (cpCachePool) {
J9GenericByID *cachePoolItem = (J9GenericByID *)classloader->classPathEntries->extraInfo;
if (NULL != cachePoolItem->cpData) {
sharedClassConfig->freeClasspathData(vm, cachePoolItem->cpData);
}
pool_removeElement(cpCachePool, (void *)cachePoolItem);
}
j9mem_free_memory(classloader->classPathEntries);
classloader->classPathEntries = NULL;
omrthread_monitor_exit(sharedClassConfig->jclCacheMutex);
Trc_VM_freeSharedCacheCLEntries_Exit(vmThread);
}
static void
recycleVMThread(J9VMThread * vmThread)
{
J9JavaVM * vm = vmThread->javaVM;
/* Preserve J9VMThread->startOfMemoryBlock and J9VMThread->J9RIParameters */
void *startOfMemoryBlock = vmThread->startOfMemoryBlock;
#if defined(J9VM_PORT_RUNTIME_INSTRUMENTATION)
J9RIParameters *riParameters = vmThread->riParameters;
#endif /* defined(J9VM_PORT_RUNTIME_INSTRUMENTATION) */
/* Determine the region of the vmThread to preserve: from publicFlagsMutex to threadObject */
size_t startRegion = offsetof(J9VMThread, publicFlagsMutex);
size_t endRegion = offsetof(J9VMThread, threadObject);
/* Indicate that the vmThread is dying */
vmThread->threadObject = NULL;
issueWriteBarrier();
/* Selectively clear the vmThread */
memset((U_8 *) vmThread, 0, startRegion);
memset(((U_8 *) vmThread) + endRegion, 0, J9_VMTHREAD_SEGREGATED_ALLOCATION_CACHE_OFFSET + vm->segregatedAllocationCacheSize - endRegion);
/* Restore J9VMThread->startOfMemoryBlock and J9VMThread->J9RIParameters */
vmThread->startOfMemoryBlock = startOfMemoryBlock;
#if defined(J9VM_PORT_RUNTIME_INSTRUMENTATION)
vmThread->riParameters = riParameters;
memset(vmThread->riParameters, 0, sizeof(J9RIParameters));
#endif /* defined(J9VM_PORT_RUNTIME_INSTRUMENTATION) */
/* Clear the public flags except for those related to halting */
clearEventFlag(vmThread, ~(UDATA)J9_PUBLIC_FLAGS_HALT_THREAD_INSPECTION);
/* dead threads are stored in "halted for inspection mode" */
omrthread_monitor_enter(vmThread->publicFlagsMutex);
if (++vmThread->inspectionSuspendCount == 1) {
setHaltFlag(vmThread, J9_PUBLIC_FLAGS_HALT_THREAD_INSPECTION);
}
omrthread_monitor_exit(vmThread->publicFlagsMutex);
J9_LINKED_LIST_ADD_LAST(vm->deadThreadList, vmThread);
}
void
deallocateVMThread(J9VMThread * vmThread, UDATA decrementZombieCount, UDATA sendThreadDestroyEvent)
{
J9JavaVM * vm = vmThread->javaVM;
J9PortLibrary * portLibrary = vm->portLibrary;
J9JavaStack * currentStack;
PORT_ACCESS_FROM_PORT(portLibrary);
/* If any exclusive access is in progress, do not let this thread die,
* as it may have stored its pointer into the exclusiveAccessStats (which verbose
* GC may read). As soon as the state is NONE, the exclusiveAccessStats are invalid,
* so we can be sure that this thread will not (validly) be read from them.
*/
omrthread_monitor_enter(vm->exclusiveAccessMutex);
while (J9_XACCESS_NONE != vm->exclusiveAccessState) {
omrthread_monitor_wait(vm->exclusiveAccessMutex);
}
omrthread_monitor_exit(vm->exclusiveAccessMutex);
/* If this thread is being inspected, do not allow it to die */
omrthread_monitor_enter(vm->vmThreadListMutex);
while (vmThread->inspectorCount != 0) {
omrthread_monitor_wait(vm->vmThreadListMutex);
}
/* Unlink the thread from the list */
J9_LINKED_LIST_REMOVE(vm->mainThread, vmThread);
/* This must be called before the GC cleans up, as the cleanup deletes the gc extensions. The
* extensions are used by the RT vm's when calling getVMThreadName because it must go through
* the access barrier.
*/
#if defined(J9VM_INTERP_VERBOSE)
if ((vm->runtimeFlags & J9_RUNTIME_REPORT_STACK_USE) && vmThread->stackObject) {
print_verbose_stackUsage(vmThread, FALSE);
}
#endif
/* vm->memoryManagerFunctions will be NULL if we failed to load the gc dll */
if (NULL != vm->memoryManagerFunctions) {
/* Make sure the memory manager does anything needed before shutting down */
/* Holding the vmThreadListMutex ensures that no heap walking will occur, ergo heap manipulation is safe */
vm->memoryManagerFunctions->cleanupMutatorModelJava(vmThread);
}
/* Call destroy hook if requested */
if (sendThreadDestroyEvent) {
TRIGGER_J9HOOK_VM_THREAD_DESTROY(vm->hookInterface, vmThread);
}
/* freeing the per thread buffers in the portlibrary */
j9port_tls_free();
if (vmThread->stackObject) {
/* Free all stacks that were used by this thread */
currentStack = vmThread->stackObject;
do {
J9JavaStack * previous = currentStack->previous;
freeJavaStack(vm, currentStack);
currentStack = previous;
} while (currentStack);
}
if (vmThread->privateFlags & J9_PRIVATE_FLAGS_DAEMON_THREAD) {
--(vm->daemonThreadCount);
}
if (vmThread->jniLocalReferences && ((J9JNIReferenceFrame*)vmThread->jniLocalReferences)->references) {
pool_kill(((J9JNIReferenceFrame*)vmThread->jniLocalReferences)->references);
}
#if defined(J9VM_GC_JNI_ARRAY_CACHE)
cleanupVMThreadJniArrayCache(vmThread);
#endif
if (vmThread->jniReferenceFrames) {
pool_kill(vmThread->jniReferenceFrames);
}
if (NULL != vmThread->monitorEnterRecordPool) {
pool_kill(vmThread->monitorEnterRecordPool);
}
j9mem_free_memory(vmThread->lastDecompilation);
#if defined(J9VM_JIT_DYNAMIC_LOOP_TRANSFER)
if (vmThread->dltBlock.temps != vmThread->dltBlock.inlineTempsBuffer) {
j9mem_free_memory(vmThread->dltBlock.temps);
}
#endif
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
if (NULL != vm->javaOffloadSwitchOffWithReasonFunc) {
vmThread->javaOffloadState = 0;
vm->javaOffloadSwitchOffWithReasonFunc(vmThread, J9_JNI_OFFLOAD_SWITCH_DEALLOCATE_VM_THREAD);
}
#endif
/* Detach the thread from OMR */
setOMRVMThreadNameWithFlagNoLock(vmThread->omrVMThread, NULL, 0);
detachVMThreadFromOMR(vm, vmThread);
recycleVMThread(vmThread); /* Make sure there are no references to vmThread after this line! */
--(vm->totalThreadCount);
/* If this thread was not forked by the VM (i.e. it was attached), then decrement the zombie count as deallocating the vmThread is as far as we can track this thread */
if (decrementZombieCount) {
--(vm->zombieThreadCount);
}
omrthread_monitor_notify_all(vm->vmThreadListMutex);
omrthread_monitor_exit(vm->vmThreadListMutex);
}
static void
trcModulesFreeJ9ModuleEntry(J9JavaVM *javaVM, J9Module *j9module)
{
J9VMThread *currentThread = javaVM->mainThread;
PORT_ACCESS_FROM_VMC(currentThread);
char moduleNameBuf[J9VM_PACKAGE_NAME_BUFFER_LENGTH];
char *moduleNameUTF = copyStringToUTF8WithMemAlloc(
currentThread, j9module->moduleName, J9_STR_NULL_TERMINATE_RESULT, "", 0, moduleNameBuf, J9VM_PACKAGE_NAME_BUFFER_LENGTH, NULL);
if (NULL != moduleNameUTF) {
Trc_MODULE_freeJ9ModuleV2_entry(currentThread, moduleNameUTF, j9module);
if (moduleNameBuf != moduleNameUTF) {
j9mem_free_memory(moduleNameUTF);
}
}
}
void
freeJ9Module(J9JavaVM *javaVM, J9Module *j9module) {
/* Removed the module from all other modules readAccessHashTable and removeAccessHashtables */
J9HashTableState walkState;
if (TrcEnabled_Trc_MODULE_freeJ9ModuleV2_entry) {
trcModulesFreeJ9ModuleEntry(javaVM, j9module);
}
if (NULL != j9module->removeAccessHashTable) {
J9Module **modulePtr = (J9Module**)hashTableStartDo(j9module->removeAccessHashTable, &walkState);
while (NULL != modulePtr) {
hashTableRemove((*modulePtr)->readAccessHashTable, &j9module);
modulePtr = (J9Module**)hashTableNextDo(&walkState);
}
hashTableFree(j9module->removeAccessHashTable);
}
if (NULL != j9module->readAccessHashTable) {
J9Module **modulePtr = (J9Module**)hashTableStartDo(j9module->readAccessHashTable, &walkState);
while (NULL != modulePtr) {
if (NULL != (*modulePtr)->removeAccessHashTable) {
hashTableRemove((*modulePtr)->removeAccessHashTable, &j9module);
}
modulePtr = (J9Module**)hashTableNextDo(&walkState);
}
hashTableFree(j9module->readAccessHashTable);
}
if (NULL != j9module->removeExportsHashTable) {
J9Package **packagePtr = (J9Package**)hashTableStartDo(j9module->removeExportsHashTable, &walkState);
while (NULL != packagePtr) {
hashTableRemove((*packagePtr)->exportsHashTable, &j9module);
packagePtr = (J9Package**)hashTableNextDo(&walkState);
}
hashTableFree(j9module->removeExportsHashTable);
}
pool_removeElement(javaVM->modularityPool, j9module);
Trc_MODULE_freeJ9Module_exit(j9module);
}
#if (defined(J9VM_GC_DYNAMIC_CLASS_UNLOADING))
/**
* Perform classloader-specific cleanup. The current thread has exclusive access.
* J9HOOK_VM_CLASS_LOADER_UNLOAD is triggered.
*
* @note The classLoader's classLoaderObject, classHashTable and classPathEntries are all NULL upon return of this function.
*
* @param classLoader the classloader to cleanup
*/
void
cleanUpClassLoader(J9VMThread *vmThread, J9ClassLoader* classLoader)
{
J9JavaVM *javaVM = vmThread->javaVM;
Trc_VM_cleanUpClassLoaders_Entry(vmThread, classLoader);
Trc_VM_triggerClassLoaderUnloadHook_Entry(vmThread, classLoader);
TRIGGER_J9HOOK_VM_CLASS_LOADER_UNLOAD(javaVM->hookInterface, vmThread, classLoader);
Trc_VM_triggerClassLoaderUnloadHook_Exit(vmThread);
/* NULL the object out to avoid confusion */
classLoader->classLoaderObject = NULL;
/* Free the class table */
if (NULL != classLoader->classHashTable) {
hashClassTableFree(classLoader);
}
/* Free the rom class orphans class table */
if (NULL != classLoader->romClassOrphansHashTable) {
hashTableFree(classLoader->romClassOrphansHashTable);
classLoader->romClassOrphansHashTable = NULL;
}
if (NULL != classLoader->classPathEntries) {
if (classLoader == javaVM->systemClassLoader) {
/* Free the class path entries in system class loader */
freeClassLoaderEntries(vmThread, classLoader->classPathEntries, classLoader->classPathEntryCount);
} else {
/* Free the class path entries in non-system class loaders*/
freeSharedCacheCLEntries(vmThread, classLoader);
}
classLoader->classPathEntries = NULL;
}
Trc_VM_cleanUpClassLoaders_Exit(vmThread);
}
#endif /* J9VM_GC_DYNAMIC_CLASS_UNLOADING */