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   // We '|' the individual exception flags instead of using FE_ALL_EXCEPT
31   // as it can include non-standard extensions. Note that we should be able
32   // to compile this file with headers from other libcs as well.
33   constexpr int allExcepts =
34       FE_DIVBYZERO | FE_INVALID | FE_INEXACT | FE_OVERFLOW | FE_UNDERFLOW;
35 
36   for (int e : excepts) {
37     ASSERT_DEATH(
38         [=] {
39           __llvm_libc::fputil::disableExcept(FE_ALL_EXCEPT);
40           __llvm_libc::fputil::enableExcept(e);
41           ASSERT_EQ(__llvm_libc::feclearexcept(FE_ALL_EXCEPT), 0);
42           // Raising all exceptions except |e| should not call the
43           // SIGFPE handler. They should set the exception flag though,
44           // so we verify that.
45           int others = allExcepts & ~e;
46           ASSERT_EQ(__llvm_libc::feraiseexcept(others), 0);
47           ASSERT_EQ(__llvm_libc::fetestexcept(others), others);
48 
49           // We don't check the return value when raising |e| as
50           // feraiseexcept will not return when it raises an enabled
51           // exception.
52           __llvm_libc::feraiseexcept(e);
53         },
54         WITH_SIGNAL(SIGFPE));
55   }
56 }
57