Skip to content

[SIL Diagnostics] Fix undefined behavior due to bit shifting in ConstantFolding.cpp #23184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/SILOptimizer/Utils/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -993,11 +993,14 @@ bool isLossyUnderflow(int srcExponent, uint64_t srcSignificand,
: srcSignificand;

// Compute the significand bits lost due to subnormal form. Note that the
// integer part: 1 will use up a significand bit in denormal form.
// integer part: 1 will use up a significand bit in subnormal form.
unsigned additionalLoss = destSem.minExponent - srcExponent + 1;
// Lost bits cannot exceed Double.minExponent - Double.significandWidth = 53.
// This can happen when truncating from Float80 to Double.
assert(additionalLoss <= 53);

// Check whether a set LSB is lost due to subnormal representation.
unsigned lostLSBBitMask = (1 << additionalLoss) - 1;
uint64_t lostLSBBitMask = ((uint64_t)1 << additionalLoss) - 1;
return (truncSignificand & lostLSBBitMask);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ func testHexFloatImprecision() {
// Smallest non-zero number representable in Double.
let d2: Double = 0x0.0000000000001p-1022
_blackHole(d2)
let d3: Double = 0x1p-1074
_blackHole(d3)

// Test the case where conversion results in subnormality in the destination.
let d4: Float = 0x1p-149
_blackHole(d4)
let d5: Float = 0x1.8p-149 // expected-warning {{'0x1.8p-149' loses precision during conversion to 'Float}}
_blackHole(d5)

// All warnings are disabled during explict conversions.
_blackHole(Float(0x1.000002p-126))
Expand Down
13 changes: 13 additions & 0 deletions test/SILOptimizer/diagnostic_constant_propagation_floats_x86.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,24 @@ func testFloatConvertUnderflow() {
let d4: Double = 5E-324
_blackHole(d4)

let e4: Float80 = 0x1p-16445
_blackHole(e4)

// FIXME: if a number is so tiny that it underflows even Float80,
// nothing is reported
let e1: Float80 = 0x1p-16446
_blackHole(e1)

// Test the case where conversion results in subnormality in the destination.
let e2: Double = 0x1p-1074
_blackHole(e2)
// FIXME: there should be a warning in the following line but it is suppressed
// conservatively as ConstantFolder thinks that the truncation here is
// explicitly requested in the code. A future enhancement to the AST may
// produce a warning here.
let e3: Double = 0x11p-1074
_blackHole(e3)

// All warnings are disabled during explict conversions
_blackHole(Float(1E-400))
_blackHole(Double(1E-309))
Expand Down