|
2025-06-25
| ||
| 16:41 | Merge 9.0 - Fix [40b1814b93] and [7c2716733a] Window handle use-after-frees. check-in: 54eb90319d user: apnadkarni tags: trunk, main | |
| 16:24 | • Closed ticket [7c2716733a]: App Verifier shows use-after-free on event handle fo... artifact: 4f217b01c0 user: apnadkarni | |
| 16:23 | Fix [7c2716733a] - use after free on Windows pipe handles check-in: a9351d85f3 user: apnadkarni tags: core-9-0-branch | |
|
2025-06-14
| ||
| 14:31 | • Ticket [7c2716733a] App Verifier shows use-after-free on event handle for Window... artifact: e6d35a9384 user: apnadkarni | |
| 13:59 | Bug [7c2716733a] - use after free on Windows event handle check-in: e85c44529a user: apnadkarni tags: bug-7c2716733a | |
|
2025-06-13
| ||
| 11:48 | • Ticket [7c2716733a] App Verifier shows use-after-free on event handle for Window... artifact: 7490cd239f user: apnadkarni | |
| 11:13 | • New ticket [7c2716733a]. artifact: a510689999 user: apnadkarni | |
App Verifier shows use-after-free on event handle for Windows pipes
Running the Windows application verifier on trunk shows a use after free on a Windows event handle used for exec pipes. The offending line is here.
The SetEvent generally returns an invalid handle error in this case and we happily ignore the error so it is undetected and life goes on. However, if by chance (however small) the handle value were to be reused by Windows for a different handle, unexpected behaviour would occur as it would not be treated as an invalid handle.
This should be afforded the same level of importance as a user-after-free detected by valgrind.
54eb90319dMerge 9.0 - Fix [40b1814b93] and [7c2716733a] Window handle use-after-frees. check-in2025-06-25 16:41:27a9351d85f3Fix [7c2716733a] - use after free on Windows pipe handles check-in2025-06-25 16:23:23e85c44529aBug [7c2716733a] - use after free on Windows event handle check-in2025-06-14 13:59:53
Looking at the source, the event handle seems to have been already closed here.
It seems to me that the immediately preceding TclPipeThreadStop wakes up the worker thread, transitioning it to the STOP state where it invokes SetEvent(). In the meanwhile, the code after TclPipeThreadStop immediately closes that event handle before the worker actually runs. Hence the invalid handle error.
I don't know what the synchronization protocol is between the main and worker threads so leaving investigation and fix to those who do.
As an aside, seems like this extends way back to 8.6.
Proposed fix is here.
Looking more closely at the code, I did not see why that event handle was needed at all. In summary,
the handle in question is
TclPipeThreadInfo.evWakeup. This is initialized from thereadableorwritableevent handles from the channel structure.the worker thread however signal read or write completions directly using the
readable/writablehandles from the channel structure, not evWakeup.the only time
TclPipeThreadInfo.evWakeupis signalled is on worker thread exit which is when it has already been deallocated by the main thread causing the use-after-free error!
The proposed fix simply expunges all references to the TclPipeThreadInfo.evWakeup. The test suite passes and Application Verifier no longer complains. But someone should review before merge.
Fixed in [7c2716733a].