Truncation of zipfs file results in valgrind 0 byte allocation errors
Closed / Fixed23dd83ce7ce0aad9db5584c6b30cdb420b403e0eCreated 3 years agoUpdated 3 years ago
Descriptionmarkdown2023-09-26 07:58:29
Valgrind shows following errors when a file contained within a mounted archive is truncated (close [open FILE w])
==12978== Invalid free() / delete / delete[] / realloc()
==12978== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==12978== by 0x488EB73: TclpFree (tclAlloc.c:722)
==12978== by 0x4A506B5: ZipChannelClose (tclZipfs.c:4234)
==12978== by 0x49B614F: ChanClose (tclIO.c:384)
==12978== by 0x49B9116: CloseChannel (tclIO.c:3149)
==12978== by 0x49B8E6A: FlushChannel (tclIO.c:3026)
==12978== by 0x49B9A59: Tcl_Close (tclIO.c:3593)
==12978== by 0x49B714A: Tcl_UnregisterChannel (tclIO.c:1275)
==12978== by 0x49C65F2: Tcl_CloseObjCmd (tclIOCmd.c:713)
==12978== by 0x489E153: Dispatch (tclBasic.c:4920)
==12978== by 0x489E1DD: TclNRRunCallbacks (tclBasic.c:4960)
==12978== by 0x489DA8F: Tcl_EvalObjv (tclBasic.c:4679)
==12978== by 0x48A034C: TclEvalEx (tclBasic.c:5837)
==12978== by 0x49D5C59: Tcl_FSEvalFileEx (tclIOUtil.c:1782)
==12978== by 0x49E7195: Tcl_MainEx (tclMain.c:405)
==12978== by 0x10A618: main (tclAppInit.c:94)
==12978== Address 0x7b7a090 is 0 bytes inside a block of size 2,097,152 free'd
==12978== at 0x483C98F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==12978== by 0x483E027: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==12978== by 0x488EB9B: TclpRealloc (tclAlloc.c:747)
==12978== by 0x4A5058F: ZipChannelClose (tclZipfs.c:4211)
==12978== by 0x49B614F: ChanClose (tclIO.c:384)
==12978== by 0x49B9116: CloseChannel (tclIO.c:3149)
==12978== by 0x49B8E6A: FlushChannel (tclIO.c:3026)
==12978== by 0x49B9A59: Tcl_Close (tclIO.c:3593)
==12978== by 0x49B714A: Tcl_UnregisterChannel (tclIO.c:1275)
==12978== by 0x49C65F2: Tcl_CloseObjCmd (tclIOCmd.c:713)
==12978== by 0x489E153: Dispatch (tclBasic.c:4920)
==12978== by 0x489E1DD: TclNRRunCallbacks (tclBasic.c:4960)
==12978== by 0x489DA8F: Tcl_EvalObjv (tclBasic.c:4679)
==12978== by 0x48A034C: TclEvalEx (tclBasic.c:5837)
==12978== by 0x49D5C59: Tcl_FSEvalFileEx (tclIOUtil.c:1782)
==12978== by 0x49E7195: Tcl_MainEx (tclMain.c:405)
==12978== by 0x10A618: main (tclAppInit.c:94)
Referenced by5
7e51dd06e2Merge [a7bd45f8711fd374]: Fix [00018ec7a0], [f91ee30d33], [23dd83ce7c] - zipfs r+, w+ modes, zero byte allocation. check-in2024-08-22 15:10:15a7bd45f871Merge 8.7 - Fix [00018ec7a0], [f91ee30d33], [23dd83ce7c] - zipfs r+, w+ modes, zero byte allocation check-in2023-09-27 11:44:06a6c30811c9Fix [00018ec7a0], [f91ee30d33], [23dd83ce7c] - zipfs r+, w+ modes, zero byte allocation check-in2023-09-27 11:11:53c7f1974db7Finish up [23dd83ce7c] tests and fixes. check-in2023-09-27 03:29:39f0558adee8Fix [23dd83ce7c]. Still need to check other ckallocs for 0 length check-in2023-09-26 17:06:52
apnadkarniplain2023-09-27 11:49:42
Fixed in [a6c30811c9]
chwplain2023-09-27 13:35:49
Regarding the comment "TODO - there seems to be no locking here ..." in the diff closing this ticket [a6c30811c9] you might want to move the WriteLock() call after if (info->isWriting) before the if block. This would solve the issue since before committing to the global ZipEntry table that WriteLock() ensures exclusive access. Since other threads require a ReadLock() to obtain an open channel they'll have to wait until after that commit.
apnadkarniplain2023-09-27 15:10:49
Yes. I just want to review all of the code for multithreading issues first though.
chrstphrchvzwiki2023-09-27 21:54:00
Regarding 64 * 1024 * 1024 “magic”, that looks like a 64MiB limit. It was upstreamed from AndroWish, and I did not find any history for it beyond it appearing in the initial check-in: https://www.androwish.org/home/blame?filename=jni/tcl/generic/zipfs.c&checkin=3ba628a7f908a348e9b0
chwplain2023-09-28 05:06:56
As far as I remember (it's 10 years later now) the reason was that on Android there is/was a size limit of 64 MByte per APK (installer package) thus I've chosen this boundary. Could certainly be lifted but having an upper limit for the potential bootstrap filesystem is not a totally bad idea.
apnadkarniplain2023-09-28 11:00:59
Yes, I figured there must be some motivation behind the limit but couldn't figure where it came from as the pkware spec does not mention any. Didn't think of APK's. Easy enough to change at some point...
Log in (anonymous is fine) to add a comment.