1 //===-- Unittests for feholdexcept with exceptions enabled ----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/fenv/feholdexcept.h" 10 11 #include "src/__support/FPUtil/FEnvImpl.h" 12 #include "src/__support/architectures.h" 13 #include "utils/UnitTest/FPExceptMatcher.h" 14 #include "utils/UnitTest/Test.h" 15 16 #include <fenv.h> 17 18 TEST(LlvmLibcFEnvTest, RaiseAndCrash) { 19 #if defined(LLVM_LIBC_ARCH_AARCH64) 20 // Few aarch64 HW implementations do not trap exceptions. We skip this test 21 // completely on such HW. 22 // 23 // Whether HW supports trapping exceptions or not is deduced by enabling an 24 // exception and reading back to see if the exception got enabled. If the 25 // exception did not get enabled, then it means that the HW does not support 26 // trapping exceptions. 27 __llvm_libc::fputil::disable_except(FE_ALL_EXCEPT); 28 __llvm_libc::fputil::enable_except(FE_DIVBYZERO); 29 if (__llvm_libc::fputil::get_except() == 0) 30 return; 31 #endif // defined(LLVM_LIBC_ARCH_AARCH64) 32 33 int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW, 34 FE_UNDERFLOW}; 35 36 for (int e : excepts) { 37 fenv_t env; 38 __llvm_libc::fputil::disable_except(FE_ALL_EXCEPT); 39 __llvm_libc::fputil::enable_except(e); 40 ASSERT_EQ(__llvm_libc::fputil::clear_except(FE_ALL_EXCEPT), 0); 41 ASSERT_EQ(__llvm_libc::feholdexcept(&env), 0); 42 // feholdexcept should disable all excepts so raising an exception 43 // should not crash/invoke the exception handler. 44 ASSERT_EQ(__llvm_libc::fputil::raise_except(e), 0); 45 46 ASSERT_RAISES_FP_EXCEPT([=] { 47 // When we put back the saved env, which has the exception enabled, it 48 // should crash with SIGFPE. Note that we set the old environment 49 // back inside this closure because in some test frameworks like Fuchsia's 50 // zxtest, this test translates to a death test in which this closure is 51 // run in a different thread. So, we set the old environment inside 52 // this closure so that the exception gets enabled for the thread running 53 // this closure. 54 __llvm_libc::fputil::set_env(&env); 55 __llvm_libc::fputil::raise_except(e); 56 }); 57 58 // Cleanup 59 __llvm_libc::fputil::disable_except(FE_ALL_EXCEPT); 60 ASSERT_EQ(__llvm_libc::fputil::clear_except(FE_ALL_EXCEPT), 0); 61 } 62 } 63