Tcl Source Code

View Ticket
Login
Ticket UUID: 90612089d8423b75a50197df1cdd29d5aef2f062
Title: signed integer overflow in ExprRandFunc()
Type: Patch Created on: 2021-12-15 09:46:14
Submitter: chrstphrchvz Assigned to: jan.nijtmans
Subsystem: 16. Commands A-H Severity: Minor
Priority: 5 Medium Last modified: 2021-12-18 12:20:51
Status: Closed Closed by: chrstphrchvz
Resolution: Fixed Closed on: 2021-12-18 12:20:51
Version: 8.6.12
Description:

There is signed integer overflow happening in ExprRandFunc() (generic/tclBasic.c). UBSan (-fsanitize=shift-base) complaint:

% puts [expr {rand()}]
tcl/generic/tclBasic.c:7784:69: runtime error: left shift of 404780544 by 12 places cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclBasic.c:7784:69 in 
0.6984060833688853

The signed integer overflow can be avoided by instead using PTR2UINT():

diff --git generic/tclBasic.c generic/tclBasic.c
index fb8524180..4a2fc04b3 100644
--- generic/tclBasic.c
+++ generic/tclBasic.c
@@ -7781,7 +7781,7 @@ ExprRandFunc(
 	 * take into consideration the thread this interp is running in.
 	 */
 
-	iPtr->randSeed = TclpGetClicks() + (PTR2INT(Tcl_GetCurrentThread())<<12);
+	iPtr->randSeed = TclpGetClicks() + (PTR2UINT(Tcl_GetCurrentThread())<<12);
 
 	/*
 	 * Make sure 1 <= randSeed <= (2^31) - 2. See below.

Though I wonder if it is intentional to have Tcl_GetCurrentThread() contribute at most (32-12)-1=19 bits (assuming sizeof(int)==4) to the seed (even fewer in practice due to lower order bits tending to be identical between threads, depending on the alignment of the struct underlying pthread_t, and/or spacing between pthread_t instances being a multiple of not very small powers of 2: I observe that for glibc NPTL on Linux i386, it is 2**12—i.e. 4kib page size—so as few as ((32-12)-12)-1=7 useful bits are incorporated in the seed). If this statement should be changed to incorporate more useful bits from Tcl_GetCurrentThread(), can the behavior of this statement be changed in 8.6?

User Comments:
jan.nijtmans added on 2021-12-15 17:06:01:

Suggested fix [b0fd5cb70eed5212|here]. In stead of multiplying by 2**12 (= 4096) we could also multiply with the first prime-number below that (= 4093). Then all bits of randomness in thread-id are kept.

Thanks for the suggestion!


chrstphrchvz added on 2021-12-15 22:59:36:

Multiplying by 4093 does preserve more useful bits, but it is done after the result of Tcl_GetCurrentThread() is temporarily narrowed to an unsigned int. Would it be useful to introduce a PTR2ULONG() macro, so that the result of Tcl_GetCurrentThread isn’t narrowed when sizeof(Tcl_ThreadId) == sizeof(unsigned long) > sizeof(unsigned int)?


jan.nijtmans added on 2021-12-16 08:29:43:

> Would it be useful to introduce a PTR2ULONG() macro ...

In Tcl 8.7, PTR2UINT() no longer narrows to unsigned int. Since it's just an internal macro, it's not worth to change that in 8.6: 8.7 still has some more time for possible bugs arising from that change to be found and fixed (although I don't know of any)

Further on, the (unsigned long) cast doesn't do anything if the value is already an unsigned type. A few lines after that:

    iPtr->randSeed &= 0x7FFFFFFFL
so any bits higher than 30 will be lost anyway.


chrstphrchvz added on 2021-12-18 12:20:51:

A few lines after that:
    iPtr->randSeed &= 0x7FFFFFFFL
so any bits higher than 30 will be lost anyway.

It seems that for a moment I forgot how (x * y) mod z = ((x mod z) * (y mod z)) mod z (in this case z = 0x7FFFFFFF+1); thanks for the clarification.