Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2018-06-05
09:14 • Closed ticket [92564326a9]: Open file descriptor leak: better align dir operatio... artifact: 0d1d43aca4 user: sebres
07:45 • Ticket [92564326a9]: 3 changes artifact: c2b03341a1 user: jan.nijtmans
2018-06-04
14:10 • Ticket [92564326a9]: 3 changes artifact: 5dd5945b66 user: sebres
13:49
fixes [92564326a9] if compiled on some x86 systems (with dirent64 but without DIR64, partially cherr... check-in: 32babd8ff2 user: sebres tags: core-8-5-branch
11:22 • Ticket [92564326a9] Open file descriptor leak: better align dir operations(opend... artifact: 2ae53e4dac user: chw
10:38 • Ticket [92564326a9]: 3 changes artifact: b390ae42fc user: sebres
2018-06-02
16:27 • Ticket [92564326a9]: 3 changes artifact: 005b2b7138 user: bll
11:26 • Pending ticket [92564326a9]. artifact: d78504d17c user: chw
2018-05-11
05:28 • Closed ticket [92564326a9]. artifact: d0193745af user: wrenashe
2018-05-10
01:51 • Pending ticket [92564326a9]. artifact: c6bbe0bb44 user: wrenashe
2018-05-09
10:37 • Closed ticket [92564326a9]. artifact: 169103fbc5 user: sebres
02:12 • Pending ticket [92564326a9]. artifact: 21241ce9a0 user: wrenashe
2018-05-08
10:02 • Closed ticket [92564326a9]. artifact: 372693da50 user: sebres
09:56
merge fix for [92564326a98b5510] from 8.5 check-in: b6d467a174 user: sebres tags: core-8-6-branch
09:49
fixes [92564326a98b5510]: wrong x64-aligned handle from readdir64 by opendir/rewinddir/closedir, if ... check-in: 85bcf84100 user: sebres tags: core-8-5-branch
07:02 • New ticket [92564326a9] Open file descriptor leak: better align dir operations(o... artifact: 917ce6fcd7 user: wrenashe

Open file descriptor leak: better align dir operations(opendir/readdir/closedir) all to either 64bit or 32bit ones.

Closed / Fixed92564326a98b551099d3e61bbe88e3b5f9720c15Created 9 years agoUpdated 8 years ago
Descriptionplain2018-05-08 07:02:43
We recently got "file operator leaks" on AIX from Tcl 8.5.19/8.6.3 which was built by ourselves on AIX for 32-bit Tcl target. 

build@aixbuild 8>xlC_r -qversion
IBM XL C/C++ for AIX, V13.1.3 (5725-C72, 5765-J07)
Version: 13.01.0003.0000

The sympton was like,
<-libc.a:opendir() = 3603f978		0.000000
495.8273:        getdirent(161, 0x360ADD78, 4096) = 1024
496.6565:        getdirent(161, 0x360ADD78, 4096) = 0
->libc.a:closedir(0x3603f978)
<-libc.a:closedir() = 0		0.000000
->libc.a:opendir(0x35caf13c)
498.2069:        getdirent(161, 0x360ADD78, 4096) = 116
<-libc.a:opendir() = 3603f978		0.000000
498.2097:        getdirent64(161, 0x360ADD78, 4096) = 160
498.2100:        getdirent64(161, 0x360ADD78, 4096) = 0
->libc.a:closedir(0x3603f978)
<-libc.a:closedir() = ffffffffffffffff		0.000000

You can see closedir() fails at the end and then the file operator is leaked then.

The problem is caused by closedir() call after readdir64() which is enabled by "-DHAVE_STRUCT_DIRENT64=1" instead of readdir() during the build.

The warning is issued during the build.
"/work/app/external/tcl8.5.19/unix/../unix/tclUnixFile.c", line 392.35: 1506-280 (W) Function argument assignment between types "struct dirdesc64*" and "struct dirdesc*" is not allowed.

The suggestion is to align all the DIR operations in either 64-bit or 32-bit ones to avoid build warning and file operator leak in the runtime. Tcl now only has TclOSreaddir defined for readdir/readdir64().

Tao
Referenced by1
  • 32babd8ff2fixes [92564326a9] if compiled on some x86 systems (with dirent64 but without DIR64, partially cherry-picked from https://www.androwish.org/index.html/info/6119b8ac2aee8411). check-in2018-06-04 13:49:38
sebreswiki2018-05-08 10:02:04

Fixed in [85bcf84100] for both branches (8.5 / 8.6).

Thanks Tao. Could you test it please for your AIX-system?

wrenasheplain2018-05-09 02:12:28
As the comments to "sebres added on 2018-05-08 10:02:04:"

Sebres,

Thanks for the prompt reply. I thought your fix was not completed one, you missed DIR/DIR64. I attached the partial patch based on my local 8.5.19 for your reference here(DIR to Tcl_Dir), the other two tclUnixFile.c and tclUnixFCmd.c files need a light update as well on where DIR is placed. I tag the bug with "rejected" and "pending" to drive the workflow.

The changes to my local is built good on AIX and testing is passed.

Index: tclUnixPort.h
===================================================================
--- tclUnixPort.h	(revision 88561)
+++ tclUnixPort.h	(working copy)
@@ -59,10 +59,18 @@
 
 #ifdef HAVE_STRUCT_DIRENT64
 typedef struct dirent64	Tcl_DirEntry;
+typedef DIR64			Tcl_Dir;
 #   define TclOSreaddir		readdir64
+#   define TclOSopendir		opendir64
+#   define TclOSrewinddir	rewinddir64
+#   define TclOSclosedir	closedir64
 #else
 typedef struct dirent	Tcl_DirEntry;
+typedef DIR		Tcl_Dir;
 #   define TclOSreaddir		readdir
+#   define TclOSopendir		opendir
+#   define TclOSrewinddir	rewinddir
+#   define TclOSclosedir	closedir
 #endif
 
 #ifdef HAVE_TYPE_OFF64_T
sebresplain2018-05-09 10:37:16
Done.
Thx again.
wrenasheplain2018-05-10 01:51:01
To be more clear,

 Modified generic/tclIntPlatDecls.h from [8993284278] to [1333869f36].

    12     12   
    13     13   #ifndef _TCLINTPLATDECLS
    14     14   #define _TCLINTPLATDECLS
    15     15   
    16     16   #ifdef __WIN32__
    17     17   #   define Tcl_DirEntry void
    18     18   #   define DIR void
           19  +#   define Tcl_Dir void
    19     20   #endif
    20     21   
    21     22   #undef TCL_STORAGE_CLASS

That "18   #   define DIR void" is suggested to removed as well.
wrenashe Closed ticket [92564326a9]. [History diff]2018-05-11 05:28:45
chwplain2018-06-02 11:26:26
The introduction of "typedef DIR64 TclDIR" et.al. broke my builds on
CentOS 5 x86 (and possibly others on 32 bit platforms, which usually do
not have a DIR64 data type).

A slightly modified autogoo helped, as can be seen in check-in

  https://www.androwish.org/index.html/info/6119b8ac2aee8411

Still broader verification is required on different 32 bit POSIX platforms.
bllplain2018-06-02 16:27:52
Related: 
    http://core.tcl.tk/tcl/info/02b58d5d040821ba
sebresplain2018-06-04 10:38:05
I don't think such deviation between dirent64/DIR64 (as in mentioned androwish commit now) is good idea at all.

Because the documentation says about the declaration of "readdir64" pretty clear:
<code><pre>
struct dirent64 *readdir64 (DIR64 *DirectoryPointer);
</pre></code>

So how it may be possible at all, that HAVE_STRUCT_DIRENT64 is 1 and HAVE_DIR64 is not?
How it is declared on your system?
chwplain2018-06-04 11:22:57
Linux/glibc x86_64 for example:

  /usr/include/dirent.h:
  ...
  #ifdef __USE_LARGEFILE64
  extern struct dirent64 *readdir64 (DIR *__dirp) __nonnull ((1));
  #endif

There simply is no DIR64 data type.
sebreswiki2018-06-04 14:10:00

You are right, thanks.

I tried to find all possible constellations (as regards dirent.h, __USE_FILE_OFFSET64, __USE_LARGEFILE64 etc.), so I've rewritten it a bit (in opposite to the current fix from androwish):

- Tcl_DirEntry and TclOSreaddir are dependent on HAVE_STRUCT_DIRENT64;

- TclDIR, TclOSopendir, TclOSrewinddir and TclOSclosedir on HAVE_DIR64;

If I can assess the configure rules correctly, HAVE_DIR64 will set only if supported (and HAVE_STRUCT_DIRENT64 will set also);

Thus fixed in [32babd8ff27c1141] for 8.5 (and merged into 8.6th).

@wrenashe: Tao, could you please test the latest version on your system (as regards the subject, I mean the leaks).

jan.nijtmanswiki2018-06-05 07:45:42

Thanks, sebres, for taking this! For the 8.x line I agree with your approach. The reason: Using -D_LARGEFILE_SOURCE -D`$(getconf LFS_CFLAGS)` would affect all usage of off_t, the ones in 'struct stat' as well. So, it could lead to binary incompatibility on some platforms (however subtile). For 9.0 that's OK (even better) but for 8.x I'm a little bit reluctant to make that step already.

So - indeed - all those #ifdef's make it a little bit complicated, but it fully preserves binary compatibility with earlier 8.x releases. Good work!

So, +1 for merging it further (core-8-branch and trunk). We already have ticket [02b58d5d04] for cleaning up the code for 9.0, doing it the 'right' way (implementation not 100% finished yet, no TIP needed because it's implementation change only)

sebresplain2018-06-05 09:14:55
Thus merged and I close the ticket. Thx, Jan.
Log in (anonymous is fine) to add a comment.