Skip to content

Change check for "shift > Type::BITS" to avoid offset greater than total bits. #133016

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
merged 1 commit into from
Mar 26, 2025

Conversation

amykhuang
Copy link
Collaborator

nexttowardf16_test is resulting in calling shift and for some reason not meeting the invariant where offset is less than bits. Change the if statement to directly check if shift - 1 meets the conditions.

@llvmbot
Copy link
Member

llvmbot commented Mar 25, 2025

@llvm/pr-subscribers-libc

Author: Amy Huang (amykhuang)

Changes

nexttowardf16_test is resulting in calling shift and for some reason not meeting the invariant where offset is less than bits. Change the if statement to directly check if shift - 1 meets the conditions.


Full diff: https://github.com/llvm/llvm-project/pull/133016.diff

1 Files Affected:

  • (modified) libc/src/__support/FPUtil/dyadic_float.h (+1-1)
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index 16b690be08612..2378b1e594c44 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -335,7 +335,7 @@ template <size_t Bits> struct DyadicFloat {
                  .get_val();
 
     MantissaType round_mask =
-        shift > MantissaType::BITS ? 0 : MantissaType(1) << (shift - 1);
+        shift - 1 >= MantissaType::BITS ? 0 : MantissaType(1) << (shift - 1);
     MantissaType sticky_mask = round_mask - MantissaType(1);
 
     bool round_bit = !(mantissa & round_mask).is_zero();

@lntue
Copy link
Contributor

lntue commented Mar 26, 2025

It looks like some false-positive from the sanitizer? Shall we create an issue for them?

@jhuber6
Copy link
Contributor

jhuber6 commented Mar 26, 2025

x << (y - 1) can underflow if it's zero leading to an out of bounds shift. By making the check y - 1 >= bits we guarantee that it's not zero because 0 - 1 is UINTMAX.

@lntue lntue merged commit 6927483 into llvm:main Mar 26, 2025
18 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Mar 26, 2025

LLVM Buildbot has detected a new failure on builder libc-riscv32-qemu-yocto-fullbuild-dbg running on rv32gc-qemu-system while building libc at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/196/builds/6413

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure) (timed out)
...
[       OK ] LlvmLibcSqrtTest.NormalRange (342759 ms)
Ran 2 tests.  PASS: 2  FAIL: 0
[1920/1922] Running unit test libc.test.src.math.sqrtl_test.__unit__
sh: line 1: /timer.7576: Permission denied
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcSqrtTest.DenormalValues
[       OK ] LlvmLibcSqrtTest.DenormalValues (877336 ms)
[ RUN      ] LlvmLibcSqrtTest.NormalRange
[       OK ] LlvmLibcSqrtTest.NormalRange (411781 ms)
Ran 2 tests.  PASS: 2  FAIL: 0
command timed out: 1200 seconds without output running [b'python', b'../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py', b'--debug'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=3767.273608
Step 8 (libc-unit-tests) failure: libc-unit-tests (failure)
...
[ RUN      ] LlvmLibcAddTestFloat.NormalRange
[       OK ] LlvmLibcAddTestFloat.NormalRange (13432 ms)
[ RUN      ] LlvmLibcAddTestLongDouble.SubnormalRange
[       OK ] LlvmLibcAddTestLongDouble.SubnormalRange (64793 ms)
[ RUN      ] LlvmLibcAddTestLongDouble.NormalRange
[       OK ] LlvmLibcAddTestLongDouble.NormalRange (81285 ms)
[ RUN      ] LlvmLibcAddTestFloat128.SubnormalRange
[       OK ] LlvmLibcAddTestFloat128.SubnormalRange (65192 ms)
[ RUN      ] LlvmLibcAddTestFloat128.NormalRange
[       OK ] LlvmLibcAddTestFloat128.NormalRange (70704 ms)
Ran 8 tests.  PASS: 8  FAIL: 0
[1917/1922] Running unit test libc.test.src.math.sqrtf128_test.__unit__
sh: line 1: /timer.7616: Permission denied
[==========] Running 3 tests from 1 test suite.
[ RUN      ] LlvmLibcSqrtTest.DenormalValues
[       OK ] LlvmLibcSqrtTest.DenormalValues (293505 ms)
[ RUN      ] LlvmLibcSqrtTest.NormalRange
[       OK ] LlvmLibcSqrtTest.NormalRange (130686 ms)
[ RUN      ] LlvmLibcSqrtTest.SpecialInputs
[       OK ] LlvmLibcSqrtTest.SpecialInputs (27 ms)
Ran 3 tests.  PASS: 3  FAIL: 0
[1918/1922] Running unit test libc.test.src.math.fsqrtl_test.__unit__
sh: line 1: /timer.8073: Permission denied
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcSqrtTest.DenormalValues
[       OK ] LlvmLibcSqrtTest.DenormalValues (702184 ms)
[ RUN      ] LlvmLibcSqrtTest.NormalRange
[       OK ] LlvmLibcSqrtTest.NormalRange (327919 ms)
Ran 2 tests.  PASS: 2  FAIL: 0
[1919/1922] Running unit test libc.test.src.math.dsqrtl_test.__unit__
sh: line 1: /timer.8092: Permission denied
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcSqrtTest.DenormalValues
[       OK ] LlvmLibcSqrtTest.DenormalValues (730296 ms)
[ RUN      ] LlvmLibcSqrtTest.NormalRange
[       OK ] LlvmLibcSqrtTest.NormalRange (342759 ms)
Ran 2 tests.  PASS: 2  FAIL: 0
[1920/1922] Running unit test libc.test.src.math.sqrtl_test.__unit__
sh: line 1: /timer.7576: Permission denied
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcSqrtTest.DenormalValues
[       OK ] LlvmLibcSqrtTest.DenormalValues (877336 ms)
[ RUN      ] LlvmLibcSqrtTest.NormalRange
[       OK ] LlvmLibcSqrtTest.NormalRange (411781 ms)
Ran 2 tests.  PASS: 2  FAIL: 0

command timed out: 1200 seconds without output running [b'python', b'../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py', b'--debug'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=3767.273608

@amykhuang amykhuang deleted the libc-undefined-shift branch March 26, 2025 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants