File indexing completed on 2025-02-16 05:12:10
0001 /* 0002 * test/suicide.cpp 0003 * Copyright 2013 Google Inc. All Rights Reserved. 0004 * 0005 * Permission is hereby granted, free of charge, to any person obtaining a copy 0006 * of this software and associated documentation files (the "Software"), to deal 0007 * in the Software without restriction, including without limitation the rights 0008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0009 * copies of the Software, and to permit persons to whom the Software is 0010 * furnished to do so, subject to the following conditions: 0011 * 0012 * The above copyright notice and this permission notice shall be included in 0013 * all copies or substantial portions of the Software. 0014 * 0015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0021 * SOFTWARE. 0022 */ 0023 0024 #include "backward.hpp" 0025 0026 #include "test/test.hpp" 0027 #include <cstdio> 0028 0029 #ifndef _WIN32 0030 #include <sys/resource.h> 0031 #endif 0032 0033 using namespace backward; 0034 0035 void badass_function() { 0036 char *ptr = (char *)42; 0037 *ptr = 42; 0038 } 0039 0040 TEST_SEGFAULT(invalid_write) { badass_function(); } 0041 0042 int you_shall_not_pass() { 0043 char *ptr = (char *)42; 0044 int v = *ptr; 0045 return v; 0046 } 0047 0048 TEST_SEGFAULT(invalid_read) { 0049 int v = you_shall_not_pass(); 0050 std::cout << "v=" << v << std::endl; 0051 } 0052 0053 void abort_abort_I_repeat_abort_abort() { 0054 std::cout << "Jumping off the boat!" << std::endl; 0055 abort(); 0056 } 0057 0058 TEST_ABORT(calling_abort) { abort_abort_I_repeat_abort_abort(); } 0059 0060 // aarch64, mips, PowerPC and RISC-V do not trap Division by zero 0061 #if !defined(__aarch64__) && !defined(__mips__) && !defined (__powerpc__) && !defined (__riscv) 0062 volatile int zero = 0; 0063 0064 int divide_by_zero() { 0065 std::cout << "And the wild black hole appears..." << std::endl; 0066 int v = 42 / zero; 0067 return v; 0068 } 0069 0070 TEST_DIVZERO(divide_by_zero) { 0071 int v = divide_by_zero(); 0072 std::cout << "v=" << v << std::endl; 0073 } 0074 #endif 0075 0076 // Darwin does not allow RLIMIT_STACK to be reduced 0077 #ifndef __APPLE__ 0078 int bye_bye_stack(int i) { return bye_bye_stack(i + 1) + bye_bye_stack(i * 2); } 0079 0080 TEST_SEGFAULT(stackoverflow) { 0081 #ifndef _WIN32 0082 struct rlimit limit; 0083 limit.rlim_max = 8096; 0084 setrlimit(RLIMIT_STACK, &limit); 0085 #endif 0086 int r = bye_bye_stack(42); 0087 std::cout << "r=" << r << std::endl; 0088 } 0089 #endif