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/UnitTest/Test.h"
15 
16 #include <fenv.h>
17 #include <signal.h>
18 
19 // This test enables an exception and verifies that raising that exception
20 // triggers SIGFPE.
21 TEST(LlvmLibcExceptionStatusTest, RaiseAndCrash) {
22   // TODO: Install a floating point exception handler and verify that the
23   // the expected exception was raised. One will have to longjmp back from
24   // that exception handler, so such a testing can be done after we have
25   // longjmp implemented.
26 
27   int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
28                    FE_UNDERFLOW};
29 
30   for (int e : excepts) {
31     ASSERT_DEATH(
32         [=] {
33           __llvm_libc::fputil::disableExcept(FE_ALL_EXCEPT);
34           __llvm_libc::fputil::enableExcept(e);
35           ASSERT_EQ(__llvm_libc::feclearexcept(FE_ALL_EXCEPT), 0);
36           // Raising all exceptions except |e| should not call the
37           // SIGFPE handler. They should set the exception flag though,
38           // so we verify that.
39           int others = FE_ALL_EXCEPT & ~e;
40           ASSERT_EQ(__llvm_libc::feraiseexcept(others), 0);
41           ASSERT_EQ(__llvm_libc::fetestexcept(others), others);
42 
43           // We don't check the return value when raising |e| as
44           // feraiseexcept will not return when it raises an enabled
45           // exception.
46           __llvm_libc::feraiseexcept(e);
47         },
48         SIGFPE);
49   }
50 }
51