JDK-8202353 : os::readdir should use readdir instead of readdir_r
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 11
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2018-04-26
  • Updated: 2020-02-18
  • Resolved: 2018-07-17
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 12 Other
11.0.4Fixed 12 b03Fixed openjdk8u232Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
POSIX implementations of os::readdir are using readdir_r. However, glibc (>= 2.24) has deprecated readdir_r; see JDK-8179887. FreeBSD has also deprecated it, for similar reasons. The rationale for that deprecation include: (1) correctly determining the size for the dirent* argument is hard, (2) readdir_r doesn't receive that size, so can't check whether it was correctly allocated, and (3) readdir can be (and is, in these implementations) reetrant so long as multiple threads don't operate on the same DIR*, making readdir_r unnecessary.

Linux platforms should use readdir because glibc (>= 2.24) deprecated readdir_r; see JDK-8179887.

AIX  documentation is clear that readdir has the desired reentrancy:
https://www.ibm.com/support/knowledgecenter/en/ssw_aix_71/com.ibm.aix.basetrf2/readdir_r.htm
The readdir subroutine is reentrant when an application program uses different DirectoryPointer parameter values (returned from the opendir subroutine). Use the readdir_r subroutine when multiple threads use the same directory pointer.

Solaris documentation is clear that readdir has the desired reentrancy:
https://docs.oracle.com/cd/E36784_01/html/E36874/readdir-r-3c.html
It is safe to use readdir() in a threaded application, so long as only one thread reads from the directory stream at any given time. The readdir() function is generally preferred over the readdir_r() function.

Windows os::readdir uses a Windows-specific mechanism, and ignores the dirent* second argument that is needed for readdir_r support.

I haven't figured out the state of things on MacOS. None of the various BSD-variant documentation I've looked at even mention a thread-safety problem for readdir.

There is some discussion of possible POSIX deprecation of readdir_r here:
http://austingroupbugs.net/view.php?id=696 

Comments
8u RFR: https://mail.openjdk.java.net/pipermail/jdk8u-dev/2019-May/009511.html
04-06-2019

Fix Request One of a set of 4 backports needed to compile against glibc versions > 2.24: JDK-8202353, JDK-8202794, JDK-8207340, and JDK-8207748. As a side effect, also fixes JDK-8202835. Patch applies cleanly net of line numbers.
29-03-2019

[~bsrbnd] glibc < 2.24 doesn't make readdir_r necessary, it just means we wouldn't get a deprecation warning for it. The glibc implementation of readdir didn't need to change for that deprecation, and readdir_r seems to have been pretty much a mistake from the get-go.
09-05-2018

Does the test environment use glibc >= 2.24 or an older version? With glibc < 2.24, readdir_r is probably necessary.
09-05-2018

The OS-dependent fix above is superseded by JDK-8202794
09-05-2018

The fix for JDK-8179887 has caused breakage: JDK-8202835
09-05-2018

Suggested core-libs fix to solve the build failure on Linux with glibc >= 2.24: http://mail.openjdk.java.net/pipermail/core-libs-dev/2018-May/052976.html
05-05-2018

There are also some uses of readdir_r (or readdir64_r) in java.base/unix/native.
02-05-2018

The second argument to os::readdir can be dropped as part of this. And os::readdir_buf_size() is no longer needed.
27-04-2018