Skip to content

Commit

Permalink
Make sure the cache file path is shorter than the OS limit
Browse files Browse the repository at this point in the history
1. Remove duplicated define of J9SH_MAXPATH in j9sharedhelper.h
2. Make sure the length of non-persistent cache mutex name is less than
_MAX_PATH on Windows.
3. Make sure the cache file path is shorter than J9SH_MAXPATH before
starting up the shared cache.

closes eclipse-openj9#4351

Signed-off-by: hangshao <[email protected]>
  • Loading branch information
hangshao0 committed Feb 5, 2019
1 parent f83e933 commit 38b04a0
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 72 deletions.
10 changes: 9 additions & 1 deletion runtime/nls/shrc/j9shr.nls
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2000, 2018 IBM Corp. and others
# Copyright (c) 2000, 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
Expand Down Expand Up @@ -6694,3 +6694,11 @@ J9NLS_SHRC_CM_PRINTSTATS_SUMMARY_NUM_STARTUP_HINTS.explanation=NOTAG
J9NLS_SHRC_CM_PRINTSTATS_SUMMARY_NUM_STARTUP_HINTS.system_action=
J9NLS_SHRC_CM_PRINTSTATS_SUMMARY_NUM_STARTUP_HINTS.user_response=
# END NON-TRANSLATABLE

J9NLS_SHRC_SHRINIT_CACHEFILEPATH_BUFFER_OVERFLOW=The length of shared cache file path should be less than %d.
# START NON-TRANSLATABLE
J9NLS_SHRC_SHRINIT_CACHEFILEPATH_BUFFER_OVERFLOW.sample_input_1=259
J9NLS_SHRC_SHRINIT_CACHEFILEPATH_BUFFER_OVERFLOW.explanation=The cache file path exceeds the maximum length allowed.
J9NLS_SHRC_SHRINIT_CACHEFILEPATH_BUFFER_OVERFLOW.system_action=The JVM terminates.
J9NLS_SHRC_SHRINIT_CACHEFILEPATH_BUFFER_OVERFLOW.user_response=Specify a shorter string to option(s) "cacheDir=" and/or "cacheName=".
# END NON-TRANSLATABLE
4 changes: 3 additions & 1 deletion runtime/oti/j9port.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@
#define J9PORT_SHSEM_MODE_NOWAIT ((uintptr_t) 2)
/** @} */

/* The following defines are used by j9shmem and j9shsem */
/* The following defines are used by j9shmem.c, j9shsem.c and j9shsem_deprecated.c */
#define J9SH_MAXPATH EsMaxPath
#define J9SH_MUTEX_STR "_mutex"
#define J9SH_SET_STR "_set"

#define J9SH_MEMORY_ID "_memory_"
#define J9SH_SEMAPHORE_ID "_semaphore_"
Expand Down
3 changes: 1 addition & 2 deletions runtime/port/sysvipc/j9sharedhelper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2014 IBM Corp. and others
* 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
Expand Down Expand Up @@ -40,7 +40,6 @@ BOOLEAN unlinkControlFile(struct J9PortLibrary* portLibrary, const char *control
#define J9SH_FILE_DOES_NOT_EXIST -4

/* The following defines are used by j9shmem and j9shsem */
#define J9SH_MAXPATH EsMaxPath
#define J9SH_VERSION (EsVersionMajor*100 + EsVersionMinor)

/* Macros dealing with control file modlevel.
Expand Down
6 changes: 3 additions & 3 deletions runtime/port/win32/j9shsem.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2018 IBM Corp. and others
* 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
Expand Down Expand Up @@ -398,7 +398,7 @@ createSemaphore(struct J9PortLibrary* portLibrary, struct j9shsem_handle* shsem_

for (i = 0; i < shsem_handle->setSize; i++) {
HANDLE debugHandle;
omrstr_printf(semaphoreName, J9SH_MAXPATH, "%s_set%d", shsem_handle->rootName, i);
omrstr_printf(semaphoreName, J9SH_MAXPATH, "%s"J9SH_SET_STR"%d", shsem_handle->rootName, i);
Trc_PRT_shsem_j9shsem_createsemaphore_creatingset(i, semaphoreName);

unicodeSemaphoreName = port_convertFromUTF8(OMRPORTLIB, semaphoreName, unicodeBuffer, UNICODE_BUFFER_SIZE);
Expand Down Expand Up @@ -431,7 +431,7 @@ openSemaphore(struct J9PortLibrary* portLibrary, struct j9shsem_handle* shsem_ha
Trc_PRT_shsem_j9shsem_opensemaphore_entered(shsem_handle->rootName);

for (i = 0; i < shsem_handle->setSize; i++) {
omrstr_printf(semaphoreName, J9SH_MAXPATH, "%s_set%d", shsem_handle->rootName, i);
omrstr_printf(semaphoreName, J9SH_MAXPATH, "%s"J9SH_SET_STR"%d", shsem_handle->rootName, i);
Trc_PRT_shsem_j9shsem_opensemaphore_openingset(i, semaphoreName);

/*convert the name to unicode*/
Expand Down
8 changes: 4 additions & 4 deletions runtime/port/win32/j9shsem_deprecated.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2018 IBM Corp. and others
* 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
Expand Down Expand Up @@ -122,7 +122,7 @@ j9shsem_deprecated_open (struct J9PortLibrary *portLibrary, const char* cacheDir
}

sharedMemoryPathAndFileName = getSharedMemoryPathandFileName(portLibrary, cacheDirName, semname);
omrstr_printf(baseFile, J9SH_MAXPATH, "%s_mutex", sharedMemoryPathAndFileName);
omrstr_printf(baseFile, J9SH_MAXPATH, "%s"J9SH_MUTEX_STR, sharedMemoryPathAndFileName);
omrmem_free_memory(sharedMemoryPathAndFileName);
Trc_PRT_shsem_j9shsem_open_builtsemname(baseFile);
for (i = 0; baseFile[i] != '\0' && i < J9SH_MAXPATH; i++) {
Expand Down Expand Up @@ -416,7 +416,7 @@ createMutex(struct J9PortLibrary* portLibrary, struct j9shsem_handle* shsem_hand

for (i = 0; i < shsem_handle->setSize; i++) {
HANDLE debugHandle;
omrstr_printf(mutexName, J9SH_MAXPATH, "%s_set%d", shsem_handle->rootName, i);
omrstr_printf(mutexName, J9SH_MAXPATH, "%s"J9SH_SET_STR"%d", shsem_handle->rootName, i);
Trc_PRT_shsem_j9shsem_createmutex_creatingset(i, mutexName);

unicodeMutexName = port_convertFromUTF8(OMRPORTLIB, mutexName, unicodeBuffer, UNICODE_BUFFER_SIZE);
Expand Down Expand Up @@ -450,7 +450,7 @@ openMutex(struct J9PortLibrary* portLibrary, struct j9shsem_handle* shsem_handle
Trc_PRT_shsem_j9shsem_openmutex_entered(shsem_handle->rootName);

for (i = 0; i < shsem_handle->setSize; i++) {
omrstr_printf(mutexName, J9SH_MAXPATH, "%s_set%d", shsem_handle->rootName, i);
omrstr_printf(mutexName, J9SH_MAXPATH, "%s"J9SH_SET_STR"%d", shsem_handle->rootName, i);
Trc_PRT_shsem_j9shsem_openmutex_openingset(i, mutexName);

/*convert the name to unicode*/
Expand Down
80 changes: 41 additions & 39 deletions runtime/shared_common/CacheLifecycleManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2001, 2018 IBM Corp. and others
* Copyright (c) 2001, 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
Expand Down Expand Up @@ -987,55 +987,57 @@ j9shr_destroy_snapshot(struct J9JavaVM* vm, const char* ctrlDirName, UDATA verbo
/* try to remove older generation cache snapshot first */
for (UDATA i = generationStart; i <= lastGeneration; i++) {
SH_OSCache::getCacheVersionAndGen(PORTLIB, vm, nameWithVGen, CACHE_ROOT_MAXLEN, snapshotName, versionData, i, false);
/* No check for the return value of getCachePathName() as it always returns 0 */
SH_OSCache::getCachePathName(PORTLIB, cacheDirName, pathFileName, J9SH_MAXPATH, nameWithVGen);
if (EsIsFile == j9file_attr(pathFileName)) {
IDATA rc = deleteSnapshot(vm, verboseFlags, pathFileName);

if (0 == rc) {
noSnapshotExists = false;
snapshotStatus = J9SH_DESTROYED_OLDER_GEN_SNAPSHOT;
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_REMOVED_OLDER_GEN, snapshotName);
} else if (-2 == rc) {
noSnapshotExists = false;
snapshotStatus = J9SH_DESTROY_FAILED_OLDER_GEN_SNAPSHOT;
returnVal = snapshotStatus;
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_FAILED_REMOVED_OLDER_GEN, snapshotName);
if (0 == SH_OSCache::getCachePathName(PORTLIB, cacheDirName, pathFileName, J9SH_MAXPATH, nameWithVGen)) {
if (EsIsFile == j9file_attr(pathFileName)) {
IDATA rc = deleteSnapshot(vm, verboseFlags, pathFileName);

if (0 == rc) {
noSnapshotExists = false;
snapshotStatus = J9SH_DESTROYED_OLDER_GEN_SNAPSHOT;
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_REMOVED_OLDER_GEN, snapshotName);
} else if (-2 == rc) {
noSnapshotExists = false;
snapshotStatus = J9SH_DESTROY_FAILED_OLDER_GEN_SNAPSHOT;
returnVal = snapshotStatus;
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_FAILED_REMOVED_OLDER_GEN, snapshotName);
}
/* rc is -1 means J9PORT_ERROR_FILE_NOENT occurred while deleting the snapshot file, silently ignore this case*/
}
/* rc is -1 means J9PORT_ERROR_FILE_NOENT occurred while deleting the snapshot file, silently ignore this case*/
}
/* silently ignore if getCachePathName() does not return 0 */
}

/* try to remove current generation cache snapshot */
if (OSCACHE_CURRENT_CACHE_GEN == generationEnd) {
SH_OSCache::getCacheVersionAndGen(PORTLIB, vm, nameWithVGen, CACHE_ROOT_MAXLEN, snapshotName, versionData, OSCACHE_CURRENT_CACHE_GEN, false);
/* No check for the return value of getCachePathName() as it always returns 0 */
SH_OSCache::getCachePathName(PORTLIB, cacheDirName, pathFileName, J9SH_MAXPATH, nameWithVGen);
if (EsIsFile == j9file_attr(pathFileName)) {
IDATA rc = deleteSnapshot(vm, verboseFlags, pathFileName);

if (0 == rc) {
/* Destroy the snapshot successfully */
J9PortShcVersion versionData;

noSnapshotExists = false;
memset(&versionData, 0, sizeof(J9PortShcVersion));
/* Do not care about the getValuesFromShcFilePrefix() return value */
getValuesFromShcFilePrefix(PORTLIB, nameWithVGen, &versionData);
if (J9SH_FEATURE_COMPRESSED_POINTERS == versionData.feature) {
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_DESTROYED_CR, snapshotName);
} else if (J9SH_FEATURE_NON_COMPRESSED_POINTERS == versionData.feature) {
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_DESTROYED_NONCR, snapshotName);
} else {
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_DESTROYED, snapshotName);
if (0 == SH_OSCache::getCachePathName(PORTLIB, cacheDirName, pathFileName, J9SH_MAXPATH, nameWithVGen)) {
if (EsIsFile == j9file_attr(pathFileName)) {
IDATA rc = deleteSnapshot(vm, verboseFlags, pathFileName);

if (0 == rc) {
/* Destroy the snapshot successfully */
J9PortShcVersion versionData;

noSnapshotExists = false;
memset(&versionData, 0, sizeof(J9PortShcVersion));
/* Do not care about the getValuesFromShcFilePrefix() return value */
getValuesFromShcFilePrefix(PORTLIB, nameWithVGen, &versionData);
if (J9SH_FEATURE_COMPRESSED_POINTERS == versionData.feature) {
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_DESTROYED_CR, snapshotName);
} else if (J9SH_FEATURE_NON_COMPRESSED_POINTERS == versionData.feature) {
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_DESTROYED_NONCR, snapshotName);
} else {
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_DESTROYED, snapshotName);
}
} else if (-2 == rc) {
noSnapshotExists = false;
returnVal = J9SH_DESTROY_FAILED_CURRENT_GEN_SNAPSHOT;
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_FAILED_REMOVED_CURRENT_GEN, snapshotName);
}
} else if (-2 == rc) {
noSnapshotExists = false;
returnVal = J9SH_DESTROY_FAILED_CURRENT_GEN_SNAPSHOT;
CLM_TRACE1(J9NLS_SHRC_CLCM_SNAPSHOT_FAILED_REMOVED_CURRENT_GEN, snapshotName);
}
/* rc is -1 means J9PORT_ERROR_FILE_NOENT occurred while deleting the snapshot file, silently ignore this case*/
}
/* silently ignore if getCachePathName() does not return 0 */
}
if (true == noSnapshotExists) {
CLM_ERR_TRACE(J9NLS_SHRC_CLCM_SNAPSHOT_NOT_EXIST);
Expand Down
15 changes: 12 additions & 3 deletions runtime/shared_common/OSCache.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2001, 2018 IBM Corp. and others
* Copyright (c) 2001, 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
Expand Down Expand Up @@ -246,14 +246,20 @@ SH_OSCache::createCacheDir(J9PortLibrary* portLibrary, char* cacheDirName, UDATA
IDATA
SH_OSCache::getCachePathName(J9PortLibrary* portLibrary, const char* cacheDirName, char* buffer, UDATA bufferSize, const char* cacheNameWithVGen)
{
IDATA rc = 0;
UDATA pathLen = 0;
PORT_ACCESS_FROM_PORT(portLibrary);

Trc_SHR_OSC_getCachePathName_Entry(cacheNameWithVGen);

j9str_printf(PORTLIB, buffer, bufferSize, "%s%s", cacheDirName, cacheNameWithVGen);
pathLen = j9str_printf(PORTLIB, buffer, bufferSize, "%s%s", cacheDirName, cacheNameWithVGen);
if (pathLen >= (bufferSize - 1)) {
Trc_SHR_OSC_getCachePathName_BufferOverFlow(cacheDirName, cacheNameWithVGen, bufferSize - 1);
rc = -1;
}

Trc_SHR_OSC_getCachePathName_Exit();
return 0;
return rc;
}

/**
Expand Down Expand Up @@ -399,6 +405,9 @@ SH_OSCache::commonStartup(J9JavaVM* vm, const char* ctrlDirName, UDATA cacheDirP
OSC_ERR_TRACE(J9NLS_SHRC_OSCACHE_ALLOC_FAILED);
return -1;
}
} else {
Trc_SHR_Assert_ShouldNeverHappen();
return -1;
}

_doCheckBuildID = ((openMode & J9OSCACHE_OPEN_MODE_CHECKBUILDID) != 0);
Expand Down
38 changes: 23 additions & 15 deletions runtime/shared_common/OSCachesysv.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2001, 2018 IBM Corp. and others
* Copyright (c) 2001, 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
Expand Down Expand Up @@ -281,20 +281,24 @@ SH_OSCachesysv::startup(J9JavaVM* vm, const char* ctrlDirName, UDATA cacheDirPer
break;
}

getCachePathName(PORTLIB, _cacheDirName, pathFileName, J9SH_MAXPATH, _semFileName);
/* No check for return value of getCachePathName() as it always return 0 */
memset(&statBuf, 0, sizeof(statBuf));
if (0 == j9file_stat(pathFileName, 0, &statBuf)) {
if (1 != statBuf.perm.isGroupReadable) {
/* Control file needs to be group readable */
Trc_SHR_OSC_startup_setGroupAccessFailed(pathFileName);
OSC_WARNING_TRACE1(J9NLS_SHRC_OSCACHE_SEM_CONTROL_FILE_SET_GROUPACCESS_FAILED, pathFileName);
if (0 == getCachePathName(PORTLIB, _cacheDirName, pathFileName, J9SH_MAXPATH, _semFileName)) {
memset(&statBuf, 0, sizeof(statBuf));
if (0 == j9file_stat(pathFileName, 0, &statBuf)) {
if (1 != statBuf.perm.isGroupReadable) {
/* Control file needs to be group readable */
Trc_SHR_OSC_startup_setGroupAccessFailed(pathFileName);
OSC_WARNING_TRACE1(J9NLS_SHRC_OSCACHE_SEM_CONTROL_FILE_SET_GROUPACCESS_FAILED, pathFileName);
}
} else {
Trc_SHR_OSC_startup_badFileStat(pathFileName);
lastErrorInfo.lastErrorCode = j9error_last_error_number();
lastErrorInfo.lastErrorMsg = j9error_last_error_message();
errorHandler(J9NLS_SHRC_OSCACHE_ERROR_FILE_STAT, &lastErrorInfo);
rc = OSCACHESYSV_FAILURE;
break;
}
} else {
Trc_SHR_OSC_startup_badFileStat(pathFileName);
lastErrorInfo.lastErrorCode = j9error_last_error_number();
lastErrorInfo.lastErrorMsg = j9error_last_error_message();
errorHandler(J9NLS_SHRC_OSCACHE_ERROR_FILE_STAT, &lastErrorInfo);
Trc_SHR_Assert_ShouldNeverHappen();
rc = OSCACHESYSV_FAILURE;
break;
}
Expand Down Expand Up @@ -2673,8 +2677,12 @@ SH_OSCachesysv::restoreFromSnapshot(J9JavaVM* vm, const char* cacheName, UDATA n
}

SH_OSCache::getCacheVersionAndGen(PORTLIB, vm, nameWithVGen, CACHE_ROOT_MAXLEN, cacheName, &versionData, OSCACHE_CURRENT_CACHE_GEN, false);
/* No check for the return value of getCachePathName() as it always returns 0 */
SH_OSCache::getCachePathName(PORTLIB, cacheDirName, pathFileName, J9SH_MAXPATH, nameWithVGen);

if (0 != SH_OSCache::getCachePathName(PORTLIB, cacheDirName, pathFileName, J9SH_MAXPATH, nameWithVGen)) {
Trc_SHR_Assert_ShouldNeverHappen();
rc = -1;
goto done;
}

fd = j9file_open(pathFileName, EsOpenRead | EsOpenWrite, 0);
if (-1 == fd) {
Expand Down
3 changes: 2 additions & 1 deletion runtime/shared_common/j9shr.tdf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//*******************************************************************************
// Copyright (c) 2006, 2018 IBM Corp. and others
// Copyright (c) 2006, 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
Expand Down Expand Up @@ -2935,3 +2935,4 @@ TraceEvent=Trc_SHR_INIT_j9shr_findGCHints_Event_Found Overhead=1 Level=4 Templat
TraceEvent=Trc_SHR_CM_storeSharedData_OverwriteExisting Overhead=1 Level=4 Template="CM storeSharedData: Existing data in the shared cache has been overwritten (result %p, data->address %p, foundDatalen %zu)."
TraceEvent=Trc_SHR_CM_updateLocalHintsData_OverwriteHeapSizes Overhead=1 Level=4 Template="CM updateLocalHintsData: Existing hints (heapSize1=%zu, heapSize2=%zu) in the shared cache will be overwritten to (heapSize1=%zu, heapSize2=%zu)."
TraceEvent=Trc_SHR_CM_updateLocalHintsData_WriteHeapSizes Overhead=1 Level=4 Template="CM updateLocalHintsData: Will write hints (heapSize1=%zu, heapSize2=%zu) to shared cache."
TraceException=Trc_SHR_OSC_getCachePathName_BufferOverFlow NoEnv Overhead=1 Level=1 Template="OSCache:: getCachePathName: The length of cacheDir %s and cacheNameWithVGen %s should be less than buffer size %zu"
Loading

0 comments on commit 38b04a0

Please sign in to comment.