Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 97f7294489d61cd6822d37aa1596d4414af42661adc44593345fd970093fdd1f
Ticket: b0f84119c8f040ed2cb4910e2aea33fc2ddc2d94
TEBCresume(): undefined behavior for INST_LSHIFT
User & Date: chrstphrchvz on 2021-12-16 17:22:45
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    Yet another case where I do not observe Tcl behaving unexpectedly despite complaints from [https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks|UBSan].
    
    Example to trigger complaint from <tt>-fsanitize=shift-base</tt> (left shift by <tt>CHAR_BIT*sizeof(long)-1</tt> causes signed integer overflow) and <tt>-fsanitize=signed-integer-overflow</tt> (2’s complement negation of <tt>LONG_MIN</tt> causes signed integer overflow) for <tt>sizeof(long)==4</tt>:
    
    <pre>
    % expr {1<<0}
    <span style="font-weight:bold">tcl/generic/tclExecute.c:6384:9: <span style="color:red">runtime error:</span> left shift of 1 by 31 places cannot be represented in type 'long int'</span>
    <span style="font-weight:bold">tcl/generic/tclExecute.c:6384:5: <span style="color:red">runtime error:</span> negation of -2147483648 cannot be represented in type 'long int'; cast to an unsigned type to negate this value to itself</span>
    1
    </pre>
    
    Likewise for <tt>sizeof(long)==8</tt>:
    
    <pre>
    % expr {1<<0}
    <span style="font-weight:bold">tcl/generic/tclExecute.c:6384:9: <span style="color:red">runtime error:</span> left shift of 1 by 63 places cannot be represented in type 'long'</span>
    <span style="font-weight:bold">tcl/generic/tclExecute.c:6384:5: <span style="color:red">runtime error:</span> negation of -9223372036854775808 cannot be represented in type 'long'; cast to an unsigned type to negate this value to itself</span>
    1
    </pre>
    
    Another example to trigger complaint from <tt>-fsanitize=shift-base</tt> (left-shifting negative signed integer is undefined behavior):
    
    <pre>
    % expr {-1<<1}
    <span style="font-weight:bold">tcl/generic/tclExecute.c:6385:17: <span style="color:red">runtime error:</span> left shift of negative value -1</span>
    -2
    </pre>
    
    Suggested fix for both of these is to use <tt>unsigned long</tt> literals and casts in a few places (this should avoid undefined behavior without changing actual behavior):
    
    <pre>
    diff --git generic/tclExecute.c generic/tclExecute.c
    index 8963472e5..19b0565 100644
    --- generic/tclExecute.c
    +++ generic/tclExecute.c
    @@ -6381,8 +6381,8 @@ TEBCresume(
    <span> </span>
     		    if ((size_t) shift < CHAR_BIT*sizeof(long) && (l1 != 0)
     			    && !((l1>0 ? l1 : ~l1) &
    <span style="color:red">-				-(1L<<(CHAR_BIT*sizeof(long) - 1 - shift)))) {
    -			lResult = l1 << shift;</span>
    <span style="color:green">+				-(1UL<<(CHAR_BIT*sizeof(long) - 1 - shift)))) {
    +			lResult = (unsigned long) l1 << shift;</span>
     			goto longResultOfArithmetic;
     		    }
     		}
    </pre>
    
    (I might also point out that <tt>l1 != 0</tt> in this condition is always true because of the <tt>l1 == 0</tt> check on line 6353, though I’m not sure whether removing it makes more sense than keeping it.)
    
  5. foundin changed to: "8.6.12"
  6. is_private changed to: "0"
  7. login: "chrstphrchvz"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Minor"
  11. status changed to: "Open"
  12. submitter changed to: "chrstphrchvz"
  13. subsystem changed to: "16. Commands A-H"
  14. title changed to: "TEBCresume(): undefined behavior for INST_LSHIFT"
  15. type changed to: "Patch"