Skip to content
Tcl Source Code View Ticket
Login

View Ticket

select() on wrong fd

Closed / Fixed1602208fffffffffffffffffffffffffffffffffCreated 20 years agoUpdated 20 years ago
Descriptionplain2006-11-24 10:52:54
I suspect a rather big problem in TclUnixWaitForFile() from tclUnixChan.c:

...
    int index, bit, numFound, result = 0;
...
    memset((void *) readyMasks, 0, 3*MASK_SIZE*sizeof(fd_mask));
    index = fd/(NBBY*sizeof(fd_mask));
    bit = 1 << (fd%(NBBY*sizeof(fd_mask)));
...

I don't see how the code above can handle descriptors above 31, bit being a 32 bit integer. 

This type of bug was fixed in tclUnixNotfy.c (see 1071807).
Would you please notify me as soon as a patch becomes available so I can test it, as I suspect my HA monitoring daemon is sometimes hanging because of it.
jfontain Ticket [1602208fff]: 1 change [History diff]2006-11-24 17:53:31
dkfplain2006-11-24 18:11:53
Logged In: YES 
user_id=79902
Originator: NO

Assuming that 'fd_mask' is a 32-bit type, it works just fine, and it allows up to MASK_SIZE*NBBY*sizeof(fd_mask) descriptors, which will usually be 256 or more (if my understanding of tclUnixPort.h is right).

The bit variable is the mask to use within a value of type fd_mask, and the index variable indexes into an array (actually three arrays concatenated) of them.
        if (mask & TCL_READABLE) {
            readyMasks[index] |= bit;
        }
        if (mask & TCL_WRITABLE) {
            (readyMasks+MASK_SIZE)[index] |= bit;
        }
        if (mask & TCL_EXCEPTION) {
            (readyMasks+2*(MASK_SIZE))[index] |= bit;
        }

Thus, suppose we're talking about fd=35. In that case (with normal constant sizes) bit==3 and index==1.

(It probably ought to be rewritten to use the same code as tclUnixNotfy.c uses though. That's more "officially correct". Assigning to the person who did that change for their comments.)
dkfplain2006-11-24 18:19:12
Logged In: YES 
user_id=79902
Originator: NO

BTW, who uses TclUnixWaitForFile() anyway? In the core, it's only touched by the test suite...
dkf Ticket [1602208fff]: 1 change [History diff]2006-11-24 18:19:28
jfontainplain2006-11-24 19:33:47
Logged In: YES 
user_id=376942
Originator: YES

On my 64 bit Fedora Core 6 box, I got bit=2**20 with fd=52.
TclUnixWaitForFile() is used by smtp::sendmessage (socket command I presume).
jfontainplain2006-11-24 19:39:36
Logged In: YES 
user_id=376942
Originator: YES

I got bit=2**20 with fd=52: will have value for index next week, but strace clearly shows a select() on 20 instead of 52.
dkfplain2006-11-24 20:30:12
Logged In: YES 
user_id=79902
Originator: NO

More digging indicates that it is used by TcpInputProc() in tclUnixChan.c (via WaitForConnect).

We need a simpler test case than the whole tcllib/mime/smtp mess. Test case will need to open at least $tcl_platform(wordSize)*8 async sockets to exhibit the problem.
jfontainplain2006-11-27 16:31:20
Logged In: YES 
user_id=376942
Originator: YES

#define NBBY 8
typedef unsigned long fd_mask;
int main(){
    int fd;
    int index;
    int bit;
    fd = 52;
    index = fd/(NBBY*sizeof(fd_mask));
    bit = 1 << (fd%(NBBY*sizeof(fd_mask)));
    printf("%d,%d,%d,%lu\n", fd, index, bit, sizeof(fd_mask));
}
gives: 52,0,1048576,8 (bit=2**20)

whereas:

int main(){
    int fd;
    int index;
    long bit;
    fd = 52;
    index = fd/(NBBY*sizeof(fd_mask));
    bit = 1L << (fd%(NBBY*sizeof(fd_mask)));
    printf("%d,%d,%ld,%lu\n", fd, index, bit, sizeof(fd_mask));
}
gives: 52,0,4503599627370496,8 (bit=2**52, which is correct)

which clearly demonstrate the problem on 64 bit platforms.
kennykbplain2006-11-28 03:19:25
Logged In: YES 
user_id=99768
Originator: NO

Fixed on HEAD and backported to core-8-4-branch
jfontainplain2006-11-28 13:20:20
Logged In: YES 
user_id=376942
Originator: YES

Unfortunately, that is not quite enough:

int main(){
    int fd;
    int index;
    fd_mask bit;
    fd = 52;
    index = fd/(NBBY*sizeof(fd_mask));
    bit = 1 << (fd%(NBBY*sizeof(fd_mask)));
    printf("%d,%d,%ld,%lu\n", fd, index, bit, sizeof(fd_mask));
    return 0;
}
still gives: 52,0,1048576,8 (bit=2**20)

    bit = 1L << (fd%(NBBY*sizeof(fd_mask)));

or maybe better, if compatible with all compilers:

    bit = (fd_mask)1 << (fd%(NBBY*sizeof(fd_mask)));

solves the problem.
dgpplain2006-11-28 21:53:55
Logged In: YES 
user_id=80530
Originator: NO

committed improvement.
kennykbplain2006-11-28 23:30:10
Logged In: YES 
user_id=99768
Originator: NO

Fix backported to the 8.4 branch.
Log in (anonymous is fine) to add a comment.