1 //===-- Unittests for feraiseexcept 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/feclearexcept.h" 10 #include "src/fenv/feraiseexcept.h" 11 #include "src/fenv/fetestexcept.h" 12 13 #include "src/__support/FPUtil/FEnvUtils.h" 14 #include "src/__support/FPUtil/FPExceptMatcher.h" 15 #include "utils/UnitTest/Test.h" 16 17 #include <fenv.h> 18 #include <signal.h> 19 20 // This test enables an exception and verifies that raising that exception 21 // triggers SIGFPE. 22 TEST(LlvmLibcExceptionStatusTest, RaiseAndCrash) { 23 // TODO: Install a floating point exception handler and verify that the 24 // the expected exception was raised. One will have to longjmp back from 25 // that exception handler, so such a testing can be done after we have 26 // longjmp implemented. 27 28 int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW, 29 FE_UNDERFLOW}; 30 31 // We '|' the individual exception flags instead of using FE_ALL_EXCEPT 32 // as it can include non-standard extensions. Note that we should be able 33 // to compile this file with headers from other libcs as well. 34 constexpr int allExcepts = 35 FE_DIVBYZERO | FE_INVALID | FE_INEXACT | FE_OVERFLOW | FE_UNDERFLOW; 36 37 for (int e : excepts) { 38 __llvm_libc::fputil::disableExcept(FE_ALL_EXCEPT); 39 __llvm_libc::fputil::enableExcept(e); 40 ASSERT_EQ(__llvm_libc::feclearexcept(FE_ALL_EXCEPT), 0); 41 // Raising all exceptions except |e| should not call the 42 // SIGFPE handler. They should set the exception flag though, 43 // so we verify that. Since other exceptions like FE_DIVBYZERO 44 // can raise FE_INEXACT as well, we don't verify the other 45 // exception flags when FE_INEXACT is enabled. 46 if (e != FE_INEXACT) { 47 int others = allExcepts & ~e; 48 ASSERT_EQ(__llvm_libc::feraiseexcept(others), 0); 49 ASSERT_EQ(__llvm_libc::fetestexcept(others), others); 50 } 51 52 ASSERT_RAISES_FP_EXCEPT([=] { 53 // In test frameworks like Fuchsia's zxtest, this translates to 54 // a death test which runs this closure in a different thread. So, 55 // we enable the exception again inside this closure so that the 56 // exception gets enabled for the thread running this closure. 57 __llvm_libc::fputil::enableExcept(e); 58 __llvm_libc::feraiseexcept(e); 59 }); 60 61 // Cleanup. 62 __llvm_libc::fputil::disableExcept(FE_ALL_EXCEPT); 63 ASSERT_EQ(__llvm_libc::feclearexcept(FE_ALL_EXCEPT), 0); 64 } 65 } 66