Artifacts Associated With Ticket 90612089d8
Ticket change [81c1a49514] (rid 196811) by chrstphrchvz on 2021-12-15 09:46:14:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-wiki"
- comment initialized to:
There is signed integer overflow happening in <tt>ExprRandFunc()</tt> (generic/tclBasic.c). UBSan (<tt>-fsanitize=shift-base</tt>) complaint: <pre> % puts [expr {rand()}] <span style="font-weight:bold">tcl/generic/tclBasic.c:7784:69: <span style="color:red">runtime error:</span> left shift of 404780544 by 12 places cannot be represented in type 'int'</span> SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclBasic.c:7784:69 in 0.6984060833688853 </pre> The signed integer overflow can be avoided by instead using <tt>PTR2UINT()</tt>: <pre> 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. */ <span> </span> <span style="color:red">- iPtr->randSeed = TclpGetClicks() + (PTR2INT(Tcl_GetCurrentThread())<<12);</span> <span style="color:green">+ iPtr->randSeed = TclpGetClicks() + (PTR2UINT(Tcl_GetCurrentThread())<<12);</span> <span> </span> /* * Make sure 1 <= randSeed <= (2^31) - 2. See below. </pre> Though I wonder if it is intentional to have <tt>Tcl_GetCurrentThread()</tt> contribute at most (32-12)-1=19 bits (assuming <tt>sizeof(int)==4</tt>) 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 <tt>pthread_t</tt>, and/or spacing between <tt>pthread_t</tt> 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 <tt>Tcl_GetCurrentThread()</tt>, can the behavior of this statement be changed in 8.6? - foundin initialized to: "8.6.12"
- is_private initialized to: "0"
- login: "chrstphrchvz"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "chrstphrchvz"
- subsystem initialized to: "16. Commands A-H"
- title initialized to: "signed integer overflow in ExprRandFunc()"
- type initialized to: "Patch"
Ticket change [132796b134] (rid 196818) by jan.nijtmans on 2021-12-15 17:06:01:
- assignee changed to: "jan.nijtmans"
- icomment:
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!
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Pending"
Ticket change [302d9d264c] (rid 196820) by chrstphrchvz on 2021-12-15 22:59:36:
- icomment:
Multiplying by 4093 does preserve more useful bits, but it is done after the result of <tt>Tcl_GetCurrentThread()</tt> is temporarily narrowed to an <tt>unsigned int</tt>. Would it be useful to introduce a <tt>PTR2ULONG()</tt> macro, so that the result of <tt>Tcl_GetCurrentThread</tt> isn’t narrowed when <tt>sizeof(Tcl_ThreadId) == sizeof(unsigned long) > sizeof(unsigned int)</tt>?
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [9e1e37de8a] (rid 196828) by jan.nijtmans on 2021-12-16 08:29:43:
- icomment:
> 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: <pre> iPtr->randSeed &= 0x7FFFFFFFL </pre> so any bits higher than 30 will be lost anyway. - login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [b1fd4b5708] (rid 196829) by jan.nijtmans on 2021-12-16 08:29:54:
- closedate changed to: "2459564.85409833"
- closer changed to: "jan.nijtmans"
- login: "jan.nijtmans"
- mimetype: "text/plain"
- status changed to: "Closed"
Ticket change [adafff04a3] (rid 196863) by chrstphrchvz on 2021-12-18 12:20:51:
- closedate changed to: "2459567.01448206"
- closer changed to: "chrstphrchvz"
- icomment:
<blockquote>A few lines after that: <pre> iPtr->randSeed &= 0x7FFFFFFFL </pre> so any bits higher than 30 will be lost anyway.</blockquote> 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. - login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"