[all-commits] [llvm/llvm-project] 1896ee: [libc] Fix undefined behavior for nan functions. (...

lntue via All-commits all-commits at lists.llvm.org
Wed Sep 11 11:13:53 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 1896ee38898a73ea9c2894e848884c8999884ab1
      https://github.com/llvm/llvm-project/commit/1896ee38898a73ea9c2894e848884c8999884ab1
  Author: lntue <35648136+lntue at users.noreply.github.com>
  Date:   2024-09-11 (Wed, 11 Sep 2024)

  Changed paths:
    M libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
    M libc/config/config.json
    M libc/docs/configure.rst
    M libc/src/__support/CMakeLists.txt
    M libc/src/__support/macros/CMakeLists.txt
    A libc/src/__support/macros/null_check.h
    M libc/src/__support/macros/sanitizer.h
    M libc/src/__support/str_to_float.h
    M libc/test/src/compiler/CMakeLists.txt
    M libc/test/src/compiler/stack_chk_guard_test.cpp
    M libc/test/src/math/smoke/CMakeLists.txt
    M libc/test/src/math/smoke/nan_test.cpp
    M libc/test/src/math/smoke/nanf128_test.cpp
    M libc/test/src/math/smoke/nanf16_test.cpp
    M libc/test/src/math/smoke/nanf_test.cpp
    M libc/test/src/math/smoke/nanl_test.cpp
    M utils/bazel/llvm-project-overlay/libc/BUILD.bazel

  Log Message:
  -----------
  [libc] Fix undefined behavior for nan functions. (#106468)

Currently the nan* functions use nullptr dereferencing to crash with
SIGSEGV if the input is nullptr. Both `nan(nullptr)` and `nullptr`
dereferencing are undefined behaviors according to the C standard.
Employing `nullptr` dereference in the `nan` function implementation is
ok if users only linked against the pre-built library, but it might be
completely removed by the compilers' optimizations if it is built from
source together with the users' code.

See for instance:  https://godbolt.org/z/fd8KcM9bx

This PR uses volatile load to prevent the undefined behavior if libc is
built without sanitizers, and leave the current undefined behavior if
libc is built with sanitizers, so that the undefined behavior can be
caught for users' codes.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list