Skip to content

Commit 7a64fbb

Browse files
author
Simon Tooke
committed
8338851: Hoist os::Posix::realpath() to os::realpath() and implement on Windows
Reviewed-by: dholmes, stuefe, jwaters
1 parent 8862ca0 commit 7a64fbb

File tree

11 files changed

+123
-24
lines changed

11 files changed

+123
-24
lines changed

src/hotspot/os/aix/os_aix.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ void os::jvm_path(char *buf, jint buflen) {
12931293
Dl_info dlinfo;
12941294
int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo);
12951295
assert(ret != 0, "cannot locate libjvm");
1296-
char* rp = os::Posix::realpath((char *)dlinfo.dli_fname, buf, buflen);
1296+
char* rp = os::realpath((char *)dlinfo.dli_fname, buf, buflen);
12971297
assert(rp != nullptr, "error in realpath(): maybe the 'path' argument is too long?");
12981298

12991299
if (Arguments::sun_java_launcher_is_altjvm()) {
@@ -1324,7 +1324,7 @@ void os::jvm_path(char *buf, jint buflen) {
13241324
}
13251325
assert(strstr(p, "/libjvm") == p, "invalid library name");
13261326

1327-
rp = os::Posix::realpath(java_home_var, buf, buflen);
1327+
rp = os::realpath(java_home_var, buf, buflen);
13281328
if (rp == nullptr) {
13291329
return;
13301330
}
@@ -1345,7 +1345,7 @@ void os::jvm_path(char *buf, jint buflen) {
13451345
snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
13461346
} else {
13471347
// Go back to path of .so
1348-
rp = os::Posix::realpath((char *)dlinfo.dli_fname, buf, buflen);
1348+
rp = os::realpath((char *)dlinfo.dli_fname, buf, buflen);
13491349
if (rp == nullptr) {
13501350
return;
13511351
}

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ void os::jvm_path(char *buf, jint buflen) {
15091509
assert(ret, "cannot locate libjvm");
15101510
char *rp = nullptr;
15111511
if (ret && dli_fname[0] != '\0') {
1512-
rp = os::Posix::realpath(dli_fname, buf, buflen);
1512+
rp = os::realpath(dli_fname, buf, buflen);
15131513
}
15141514
if (rp == nullptr) {
15151515
return;
@@ -1541,7 +1541,7 @@ void os::jvm_path(char *buf, jint buflen) {
15411541
p = strrchr(buf, '/');
15421542
assert(strstr(p, "/libjvm") == p, "invalid library name");
15431543

1544-
rp = os::Posix::realpath(java_home_var, buf, buflen);
1544+
rp = os::realpath(java_home_var, buf, buflen);
15451545
if (rp == nullptr) {
15461546
return;
15471547
}
@@ -1575,7 +1575,7 @@ void os::jvm_path(char *buf, jint buflen) {
15751575
snprintf(buf + len, buflen-len, "/libjvm%s", JNI_LIB_SUFFIX);
15761576
} else {
15771577
// Fall back to path of current library
1578-
rp = os::Posix::realpath(dli_fname, buf, buflen);
1578+
rp = os::realpath(dli_fname, buf, buflen);
15791579
if (rp == nullptr) {
15801580
return;
15811581
}

src/hotspot/os/linux/os_linux.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,7 +2763,7 @@ void os::jvm_path(char *buf, jint buflen) {
27632763
assert(ret, "cannot locate libjvm");
27642764
char *rp = nullptr;
27652765
if (ret && dli_fname[0] != '\0') {
2766-
rp = os::Posix::realpath(dli_fname, buf, buflen);
2766+
rp = os::realpath(dli_fname, buf, buflen);
27672767
}
27682768
if (rp == nullptr) {
27692769
return;
@@ -2797,7 +2797,7 @@ void os::jvm_path(char *buf, jint buflen) {
27972797
}
27982798
assert(strstr(p, "/libjvm") == p, "invalid library name");
27992799

2800-
rp = os::Posix::realpath(java_home_var, buf, buflen);
2800+
rp = os::realpath(java_home_var, buf, buflen);
28012801
if (rp == nullptr) {
28022802
return;
28032803
}
@@ -2818,7 +2818,7 @@ void os::jvm_path(char *buf, jint buflen) {
28182818
snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
28192819
} else {
28202820
// Go back to path of .so
2821-
rp = os::Posix::realpath(dli_fname, buf, buflen);
2821+
rp = os::realpath(dli_fname, buf, buflen);
28222822
if (rp == nullptr) {
28232823
return;
28242824
}

src/hotspot/os/linux/os_perf_linux.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ char* SystemProcessInterface::SystemProcesses::ProcessIterator::get_exe_path() {
789789

790790
jio_snprintf(buffer, PATH_MAX, "/proc/%s/exe", _entry->d_name);
791791
buffer[PATH_MAX - 1] = '\0';
792-
return os::Posix::realpath(buffer, _exePath, PATH_MAX);
792+
return os::realpath(buffer, _exePath, PATH_MAX);
793793
}
794794

795795
char* SystemProcessInterface::SystemProcesses::ProcessIterator::allocate_string(const char* str) const {

src/hotspot/os/posix/os_posix.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,10 +1029,10 @@ char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_a
10291029
return buf;
10301030
}
10311031

1032-
char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen) {
1032+
char* os::realpath(const char* filename, char* outbuf, size_t outbuflen) {
10331033

10341034
if (filename == nullptr || outbuf == nullptr || outbuflen < 1) {
1035-
assert(false, "os::Posix::realpath: invalid arguments.");
1035+
assert(false, "os::realpath: invalid arguments.");
10361036
errno = EINVAL;
10371037
return nullptr;
10381038
}
@@ -1067,7 +1067,6 @@ char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen)
10671067
}
10681068
}
10691069
return result;
1070-
10711070
}
10721071

10731072
int os::stat(const char *path, struct stat *sbuf) {

src/hotspot/os/posix/os_posix.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,6 @@ class os::Posix {
7373
// to buf with len buflen; buf is returned.
7474
static char* describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr);
7575

76-
// A safe implementation of realpath which will not cause a buffer overflow if the resolved path
77-
// is longer than PATH_MAX.
78-
// On success, returns 'outbuf', which now contains the path.
79-
// On error, it will return null and set errno. The content of 'outbuf' is undefined.
80-
// On truncation error ('outbuf' too small), it will return null and set errno to ENAMETOOLONG.
81-
static char* realpath(const char* filename, char* outbuf, size_t outbuflen);
82-
8376
// Returns true if given uid is root.
8477
static bool is_root(uid_t uid);
8578

src/hotspot/os/windows/os_windows.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5397,6 +5397,28 @@ void os::funlockfile(FILE* fp) {
53975397
_unlock_file(fp);
53985398
}
53995399

5400+
char* os::realpath(const char* filename, char* outbuf, size_t outbuflen) {
5401+
5402+
if (filename == nullptr || outbuf == nullptr || outbuflen < 1) {
5403+
assert(false, "os::realpath: invalid arguments.");
5404+
errno = EINVAL;
5405+
return nullptr;
5406+
}
5407+
5408+
char* result = nullptr;
5409+
ALLOW_C_FUNCTION(::_fullpath, char* p = ::_fullpath(nullptr, filename, 0);)
5410+
if (p != nullptr) {
5411+
if (strlen(p) < outbuflen) {
5412+
strcpy(outbuf, p);
5413+
result = outbuf;
5414+
} else {
5415+
errno = ENAMETOOLONG;
5416+
}
5417+
ALLOW_C_FUNCTION(::free, ::free(p);) // *not* os::free
5418+
}
5419+
return result;
5420+
}
5421+
54005422
// Map a block of memory.
54015423
char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
54025424
char *addr, size_t bytes, bool read_only,

src/hotspot/share/runtime/os.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,13 @@ class os: AllStatic {
668668
static void flockfile(FILE* fp);
669669
static void funlockfile(FILE* fp);
670670

671+
// A safe implementation of realpath which will not cause a buffer overflow if the resolved path
672+
// is longer than PATH_MAX.
673+
// On success, returns 'outbuf', which now contains the path.
674+
// On error, it will return null and set errno. The content of 'outbuf' is undefined.
675+
// On truncation error ('outbuf' too small), it will return null and set errno to ENAMETOOLONG.
676+
static char* realpath(const char* filename, char* outbuf, size_t outbuflen);
677+
671678
static int compare_file_modified_times(const char* file1, const char* file2);
672679

673680
static bool same_files(const char* file1, const char* file2);

src/hotspot/share/services/diagnosticCommand.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,12 +1202,10 @@ void SystemDumpMapDCmd::execute(DCmdSource source, TRAPS) {
12021202
output()->print_cr("(NMT is disabled, will not annotate mappings).");
12031203
}
12041204
MemMapPrinter::print_all_mappings(&fs);
1205-
#ifndef _WIN64
12061205
// For the readers convenience, resolve path name.
12071206
char tmp[JVM_MAXPATHLEN];
1208-
const char* absname = os::Posix::realpath(name, tmp, sizeof(tmp));
1207+
const char* absname = os::realpath(name, tmp, sizeof(tmp));
12091208
name = absname != nullptr ? absname : name;
1210-
#endif
12111209
output()->print_cr("Memory map dumped to \"%s\".", name);
12121210
} else {
12131211
output()->print_cr("Failed to open \"%s\" for writing (%s).", name, os::strerror(errno));

src/hotspot/share/utilities/globalDefinitions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ FORBID_C_FUNCTION(char* strdup(const char *s), "use os::strdup");
210210
FORBID_C_FUNCTION(char* strndup(const char *s, size_t n), "don't use");
211211
FORBID_C_FUNCTION(int posix_memalign(void **memptr, size_t alignment, size_t size), "don't use");
212212
FORBID_C_FUNCTION(void* aligned_alloc(size_t alignment, size_t size), "don't use");
213-
FORBID_C_FUNCTION(char* realpath(const char* path, char* resolved_path), "use os::Posix::realpath");
213+
FORBID_C_FUNCTION(char* realpath(const char* path, char* resolved_path), "use os::realpath");
214214
FORBID_C_FUNCTION(char* get_current_dir_name(void), "use os::get_current_directory()");
215215
FORBID_C_FUNCTION(char* getwd(char *buf), "use os::get_current_directory()");
216216
FORBID_C_FUNCTION(wchar_t* wcsdup(const wchar_t *s), "don't use");

0 commit comments

Comments
 (0)