TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / external / backward-cpp / test / suicide.cpp
1 /*
2  * test/suicide.cpp
3  * Copyright 2013 Google Inc. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23
24 #include "backward.hpp"
25
26 #include <cstdio>
27 #include <sys/resource.h>
28 #include "test/test.hpp"
29
30 using namespace backward;
31
32 void badass_function()
33 {
34         char* ptr = (char*)42;
35         *ptr = 42;
36 }
37
38 TEST_SEGFAULT (invalid_write)
39 {
40         badass_function();
41 }
42
43 int you_shall_not_pass()
44 {
45         char* ptr = (char*)42;
46         int v = *ptr;
47         return v;
48 }
49
50 TEST_SEGFAULT(invalid_read)
51 {
52         int v = you_shall_not_pass();
53         std::cout << "v=" << v << std::endl;
54 }
55
56 void abort_abort_I_repeat_abort_abort()
57 {
58         std::cout << "Jumping off the boat!" << std::endl;
59         abort();
60 }
61
62 TEST_ABORT (calling_abort)
63 {
64         abort_abort_I_repeat_abort_abort();
65 }
66
67 // aarch64 does not trap Division by zero
68 #ifndef __aarch64__
69 volatile int zero = 0;
70
71 int divide_by_zero()
72 {
73         std::cout << "And the wild black hole appears..." << std::endl;
74         int v = 42 / zero;
75         return v;
76 }
77
78 TEST_DIVZERO (divide_by_zero)
79 {
80         int v = divide_by_zero();
81         std::cout << "v=" << v << std::endl;
82 }
83 #endif
84
85 // Darwin does not allow RLIMIT_STACK to be reduced
86 #ifndef __APPLE__
87 int bye_bye_stack(int i) {
88         return bye_bye_stack(i + 1) + bye_bye_stack(i * 2);
89 }
90
91 TEST_SEGFAULT(stackoverflow)
92 {
93         struct rlimit limit;
94         limit.rlim_max = 8096;
95         setrlimit(RLIMIT_STACK, &limit);
96         int r = bye_bye_stack(42);
97         std::cout << "r=" << r << std::endl;
98 }
99 #endif