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 "utils/FPUtil/FEnv.h" 14 #include "utils/FPUtil/TestHelpers.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. 44 int others = allExcepts & ~e; 45 ASSERT_EQ(__llvm_libc::feraiseexcept(others), 0); 46 ASSERT_EQ(__llvm_libc::fetestexcept(others), others); 47 48 ASSERT_RAISES_FP_EXCEPT([=] { __llvm_libc::feraiseexcept(e); }); 49 } 50 } 51