Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2020-05-29
20:23 • Pending ticket [75d525d37c]: Memory corruption transferring server (listening) s... artifact: 7e56fd6cd2 user: sebres
20:00
fixes [75d525d37c]: segfault by server socket transfer (accept proc executed in wrong interpreter); ... leaf check-in: 455c680c3a user: sebres tags: bug-75d525d37c-sf-chan-transfer
19:40
test illustrating [75d525d37c]: segfault by server socket transfer (accept proc executed in wrong in... check-in: 0e2fa18bb9 user: sebres tags: bug-75d525d37c-sf-chan-transfer
13:22 • Ticket [75d525d37c] Memory corruption transferring server (listening) sockets be... artifact: e8be9b5267 user: sebres
2020-01-31
12:54 • Add attachment socketservertransfer.tcl to ticket [75d525d37c931812|75d... artifact: 60a39ad4a4 user: apnadkarni
12:24 • Ticket [75d525d37c] Memory corruption transferring server (listening) sockets be... artifact: d7a93ece31 user: apnadkarni
12:23 • New ticket [75d525d37c]. artifact: 79731ca478 user: apnadkarni

Memory corruption transferring server (listening) sockets between threads

Pending75d525d37c931812e9b00a80f6f43ac062598d54Created 7 years agoUpdated 7 years ago
Descriptionwiki2020-01-31 12:23:59
Working on a generic IOCP framework for Windows, I noticed the following issue while trying to grok the Windows socket code. Note it appears the same issue will also exist on Unix.

The socket implementation stores the user supplied script argument to [socket -server] in the AcceptCallback structure along with a pointer to the current interp. If the socket is then transferred to a different thread using thread::transfer, when a connection comes in the callback is run in the context of the owning thread but using the original interp belonging to the original thread with obviously catastrophic results.

There needs to be mechanism to either switch the interp stored in the AcceptCallback structure to the appropriate one in the receiving thread (this seems hard to me given the current implementation), or disallow transfer of server sockets.

I'll try to provide a proof script later.

Referenced by2
  • 455c680c3afixes [75d525d37c]: segfault by server socket transfer (accept proc executed in wrong interpreter); no interp-attach action or callback at the moment, but since accept proc is not sharable between multiple interpreters of same thread, after transfer to another thread we can simply obtain new interp handle from statePtr->interp (set by register interp now) check-in2020-05-29 20:00:35
  • 0e2fa18bb9test illustrating [75d525d37c]: segfault by server socket transfer (accept proc executed in wrong interpreter) check-in2020-05-29 19:40:36
apnadkarni Ticket [75d525d37c]: 3 changes [History diff]2020-01-31 12:24:39
sebreswiki2020-05-29 13:22:25

Confirmed (current 8.6 but also 8.5).
The issue is very similar to [f583715154], and the fix can be almost the same as [532506ef71] but for server side - proper reaction on switch of managing thread, either in event handler or in action proc handler (or in both).

WiP

sebreswiki2020-05-29 20:23:04

Although it is fixed in branch [bug-75d525d37c-sf-chan-transfer] now (platform-unrelated), the fix is a bit tricky: since as Ashok correctly said, Tcl-API doesn't provide any possibility to inform socket that this going attached to some interpreter.
So one could reset interp in accept-data by thread (interp) detach, but it is impossible to be informed that it is attached again.

Check-in [455c680c3a] provides now such information (interp set in statePtr of the channel now), that allows to use this in every socket handler, with prerequisite that socket would be not shared in multiple interpreter of same thread, or (like accept handler of server socket) it does not intended to be used in all interpreters.

This also allows extremely simplify the (un)registering mechanism of socket handlers (like accept-proc and its client-data) and it could be protected by (statePtr->interp == NULL) inside handler and/or simple ref-counter now.

Test, similar to attached by apn, that illustrating the bug, is provided also (just the segfault is not always reproducible).

To use it in modules or extensions either internal structures of tclIO.h are expected, or we must implement new function like "Tcl_GetChannelInterp" (for the single or main interpreter mode only).

Log in (anonymous is fine) to add a comment.

Attachments: