--- old/src/hotspot/os/windows/os_windows.cpp 2017-10-25 11:01:40.092591235 -0700 +++ new/src/hotspot/os/windows/os_windows.cpp 2017-10-25 11:01:39.725556744 -0700 @@ -4060,15 +4060,85 @@ } } +// combine the high and low DWORD into a ULONGLONG +static ULONGLONG eval_find_data(DWORD high_word, DWORD low_word) { + ULONGLONG value = high_word; + value <<= sizeof(high_word) * 8; + value |= low_word; + return value; +} + +// setup the struct stat using the data from WIN32_FIND_DATAW structure +static void setup_stat(struct stat* sbuf, WIN32_FIND_DATAW find_data) { + sbuf->st_size = (_off_t)eval_find_data(find_data.nFileSizeHigh, find_data.nFileSizeLow); + sbuf->st_mtime = eval_find_data(find_data.ftLastWriteTime.dwHighDateTime, + find_data.ftLastWriteTime.dwLowDateTime); + sbuf->st_ctime = eval_find_data(find_data.ftCreationTime.dwHighDateTime, + find_data.ftCreationTime.dwLowDateTime); + sbuf->st_atime = eval_find_data(find_data.ftLastAccessTime.dwHighDateTime, + find_data.ftLastAccessTime.dwLowDateTime); + if (find_data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { + sbuf->st_mode |= S_IFDIR; + } + if (find_data.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) { + sbuf->st_mode |= S_IFREG; + } +} + +// The following function is adapted from java.base/windows/native/libjava/canonicalize_md.c +// copy \\?\ or \\?\UNC\ to the front of path +static void prefixed_path(WCHAR* wpath, size_t pathlen) { + if (wpath[0] == L'\\' && wpath[1] == L'\\') { + /* if it already has a \\?\ don't do the prefix */ + if (!(wpath[2] == L'?' && wpath[3] == L'\\')) { + /* only UNC pathname includes double slashes here */ + ::wmemmove(wpath + 7, wpath + 1, pathlen - 1); + ::wmemcpy_s(wpath, pathlen, L"\\\\?\\UNC", 7); + wpath[pathlen + 6] = L'\0'; + } + } + else { + ::wmemmove(wpath + 4, wpath, pathlen); + ::wmemcpy_s(wpath, pathlen, L"\\\\?\\", 4); + wpath[pathlen + 4] = L'\0'; + } +} int os::stat(const char *path, struct stat *sbuf) { - char pathbuf[MAX_PATH]; - if (strlen(path) > MAX_PATH - 1) { - errno = ENAMETOOLONG; + size_t path_len = strlen(path) + 1; + char* pathbuf = (char*)os::malloc(path_len * sizeof(char), mtInternal); + if (pathbuf == NULL) { + errno = ENOMEM; return -1; } os::native_path(strcpy(pathbuf, path)); - int ret = ::stat(pathbuf, sbuf); + int ret; + if (strlen(path) < MAX_PATH) { + ret = ::stat(pathbuf, sbuf); + } else { + WCHAR* wpathbuf = (WCHAR*)os::malloc((path_len + 10) * sizeof (WCHAR), mtInternal); + if (wpathbuf == NULL) { + os::free(pathbuf); + errno = ENOMEM; + return -1; + } + size_t converted_chars = 0; + ::mbstowcs_s(&converted_chars, wpathbuf, path_len + 10, pathbuf, strlen(path)); + prefixed_path(wpathbuf, strlen(path)); + WIN32_FIND_DATAW find_data; + HANDLE h_find; + h_find = ::FindFirstFileW(wpathbuf, &find_data); + if (h_find == INVALID_HANDLE_VALUE) { + errno = ::GetLastError(); + os::free(wpathbuf); + ret = -1; + } else { + setup_stat(sbuf, find_data); + ret = 0; + ::FindClose(h_find); + } + os::free(wpathbuf); + } if (sbuf != NULL && UseUTCFileTimestamp) { // Fix for 6539723. st_mtime returned from stat() is dependent on // the system timezone and so can return different values for the @@ -4092,9 +4162,10 @@ TIME_ZONE_INFORMATION tz; DWORD tzid = GetTimeZoneInformation(&tz); int daylightBias = - (tzid == TIME_ZONE_ID_DAYLIGHT) ? tz.DaylightBias : tz.StandardBias; + (tzid == TIME_ZONE_ID_DAYLIGHT) ? tz.DaylightBias : tz.StandardBias; sbuf->st_mtime += (tz.Bias + daylightBias) * 60; } + os::free(pathbuf); return ret; } @@ -4207,14 +4278,34 @@ // from src/windows/hpi/src/sys_api_md.c int os::open(const char *path, int oflag, int mode) { - char pathbuf[MAX_PATH]; - - if (strlen(path) > MAX_PATH - 1) { - errno = ENAMETOOLONG; + size_t path_len = strlen(path) + 1; + char* pathbuf = (char*)os::malloc(path_len * sizeof(char), mtInternal); + if (pathbuf == NULL) { + errno = ENOMEM; return -1; } os::native_path(strcpy(pathbuf, path)); - return ::open(pathbuf, oflag | O_BINARY | O_NOINHERIT, mode); + int ret; + if (strlen(path) < MAX_PATH) { + ret = ::open(pathbuf, oflag | O_BINARY | O_NOINHERIT, mode); + } else { + WCHAR* wpathbuf = (WCHAR*)os::malloc((path_len + 10) * sizeof (WCHAR), mtInternal); + if (wpathbuf == NULL) { + os::free(pathbuf); + errno = ENOMEM; + return -1; + } + size_t converted_chars = 0; + ::mbstowcs_s(&converted_chars, wpathbuf, path_len + 10, pathbuf, strlen(path)); + prefixed_path(wpathbuf, strlen(path)); + ret = ::_wopen(wpathbuf, oflag | O_BINARY | O_NOINHERIT, mode); + if (ret == -1) { + errno = ::GetLastError(); + } + os::free(wpathbuf); + } + os::free(pathbuf); + return ret; } FILE* os::open(int fd, const char* mode) { --- old/src/hotspot/share/classfile/classLoader.cpp 2017-10-25 11:01:41.418715854 -0700 +++ new/src/hotspot/share/classfile/classLoader.cpp 2017-10-25 11:01:41.003676852 -0700 @@ -262,11 +262,10 @@ ClassFileStream* ClassPathDirEntry::open_stream(const char* name, TRAPS) { // construct full path name - char* path = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, JVM_MAXPATHLEN); - if (jio_snprintf(path, JVM_MAXPATHLEN, "%s%s%s", _dir, os::file_separator(), name) == -1) { - FREE_RESOURCE_ARRAY(char, path, JVM_MAXPATHLEN); - return NULL; - } + size_t path_len = strlen(_dir) + strlen(name) + strlen(os::file_separator()) + 1; + char* path = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, path_len); + int len = jio_snprintf(path, path_len, "%s%s%s", _dir, os::file_separator(), name); + assert(len == (int)(path_len - 1), "sanity"); // check if file exists struct stat st; if (os::stat(path, &st) == 0) { @@ -291,7 +290,7 @@ if (UsePerfData) { ClassLoader::perf_sys_classfile_bytes_read()->inc(num_read); } - FREE_RESOURCE_ARRAY(char, path, JVM_MAXPATHLEN); + FREE_RESOURCE_ARRAY(char, path, path_len); // Resource allocated return new ClassFileStream(buffer, st.st_size, @@ -300,7 +299,7 @@ } } } - FREE_RESOURCE_ARRAY(char, path, JVM_MAXPATHLEN); + FREE_RESOURCE_ARRAY(char, path, path_len); return NULL; } @@ -381,9 +380,13 @@ if (is_multi_ver) { int n; - char* entry_name = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, JVM_MAXPATHLEN); + const char* version_entry = "META-INF/versions/"; + // 10 is the max length of a decimal 32-bit non-negative number + // 2 includes the '/' and trailing zero + size_t entry_name_len = strlen(version_entry) + 10 + strlen(name) + 2; + char* entry_name = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, entry_name_len); if (version > 0) { - n = jio_snprintf(entry_name, JVM_MAXPATHLEN, "META-INF/versions/%d/%s", version, name); + n = jio_snprintf(entry_name, entry_name_len, "%s%d/%s", version_entry, version, name); entry_name[n] = '\0'; buffer = open_entry((const char*)entry_name, filesize, false, CHECK_NULL); if (buffer == NULL) { @@ -392,7 +395,7 @@ } if (buffer == NULL) { for (int i = cur_ver; i >= base_version; i--) { - n = jio_snprintf(entry_name, JVM_MAXPATHLEN, "META-INF/versions/%d/%s", i, name); + n = jio_snprintf(entry_name, entry_name_len, "%s%d/%s", version_entry, i, name); entry_name[n] = '\0'; buffer = open_entry((const char*)entry_name, filesize, false, CHECK_NULL); if (buffer != NULL) { @@ -400,7 +403,7 @@ } } } - FREE_RESOURCE_ARRAY(char, entry_name, JVM_MAXPATHLEN); + FREE_RESOURCE_ARRAY(char, entry_name, entry_name_len); } } return buffer; --- /dev/null 2017-09-20 01:43:30.052953571 -0700 +++ new/test/hotspot/jtreg/runtime/LoadClass/LongPath.java 2017-10-25 11:01:42.103780231 -0700 @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary JVM should be able to handle full path (directory path plus + * class name) or directory path longer than MAX_PATH on windows. + * @library /test/lib + * @modules java.base/jdk.internal.misc + * java.management + * @run main LongPath + */ + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import jdk.test.lib.compiler.CompilerUtils; +import jdk.test.lib.process.ProcessTools; +import jdk.test.lib.process.OutputAnalyzer; + +public class LongPath { + + private static final int MAX_PATH = 260; + + public static void main(String args[]) throws Exception { + Path sourceDir = Paths.get(System.getProperty("test.src"), "test-classes"); + Path classDir = Paths.get(System.getProperty("test.classes")); + Path destDir = classDir; + + // create a sub-path so that the destDir length is almost MAX_PATH + // so that the full path (with the class name) will exceed MAX_PATH + int subDirLen = MAX_PATH - classDir.toString().length() - 2; + if (subDirLen > 0) { + char[] chars = new char[subDirLen]; + Arrays.fill(chars, 'x'); + String subPath = new String(chars); + destDir = Paths.get(System.getProperty("test.classes"), subPath); + } + + CompilerUtils.compile(sourceDir, destDir); + + String bootCP = "-Xbootclasspath/a:" + destDir.toString(); + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( + bootCP, "Hello"); + + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + output.shouldContain("Hello World") + .shouldHaveExitValue(0); + + // increase the length of destDir to slightly over MAX_PATH + destDir = Paths.get(destDir.toString(), "xxxxx"); + CompilerUtils.compile(sourceDir, destDir); + + bootCP = "-Xbootclasspath/a:" + destDir.toString(); + pb = ProcessTools.createJavaProcessBuilder( + bootCP, "Hello"); + + output = new OutputAnalyzer(pb.start()); + output.shouldContain("Hello World") + .shouldHaveExitValue(0); + } +} --- /dev/null 2017-09-20 01:43:30.052953571 -0700 +++ new/test/hotspot/jtreg/runtime/LoadClass/test-classes/Hello.java 2017-10-25 11:01:42.970861712 -0700 @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. + */ + +public class Hello { + public static void main(String args[]) { + System.out.println("Hello World"); + } +}