| 1 | /* Copyright (c) 2009, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <signal.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #ifndef _WIN32 |
| 30 | #include <sys/resource.h> |
| 31 | #include <sys/time.h> |
| 32 | #endif |
| 33 | #include <sys/types.h> |
| 34 | |
| 35 | #include "gunit_test_main.h" |
| 36 | |
| 37 | #include "my_getopt.h" |
| 38 | #include "my_inttypes.h" |
| 39 | #include "my_stacktrace.h" |
| 40 | #include "my_sys.h" |
| 41 | #include "my_thread_local.h" |
| 42 | #include "mysql/psi/mysql_mutex.h" |
| 43 | |
| 44 | class Cost_constant_cache; |
| 45 | CHARSET_INFO *system_charset_info = nullptr; |
| 46 | class THD; |
| 47 | |
| 48 | #ifdef _WIN32 |
| 49 | #define SIGNAL_FMT "exception 0x%x" |
| 50 | #else |
| 51 | #define SIGNAL_FMT "signal %d" |
| 52 | #endif |
| 53 | |
| 54 | static void signal_handler(int sig) { |
| 55 | my_safe_printf_stderr("unit test got " SIGNAL_FMT "\n", sig); |
| 56 | |
| 57 | // Alpine Linux does not have backtrace. |
| 58 | #ifdef HAVE_STACKTRACE |
| 59 | my_print_stacktrace(nullptr, 0); |
| 60 | #endif |
| 61 | |
| 62 | exit(EXIT_FAILURE); |
| 63 | } |
| 64 | |
| 65 | #ifdef _WIN32 |
| 66 | |
| 67 | LONG WINAPI exception_filter(EXCEPTION_POINTERS *exp) { |
| 68 | __try { |
| 69 | my_set_exception_pointers(exp); |
| 70 | signal_handler(exp->ExceptionRecord->ExceptionCode); |
| 71 | } __except (EXCEPTION_EXECUTE_HANDLER) { |
| 72 | fputs("Got exception in exception handler!\n", stderr); |
| 73 | } |
| 74 | |
| 75 | return EXCEPTION_CONTINUE_SEARCH; |
| 76 | } |
| 77 | |
| 78 | static void init_signal_handling() { |
| 79 | /* Set output destination of messages to the standard error stream. */ |
| 80 | _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); |
| 81 | _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); |
| 82 | _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE); |
| 83 | _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); |
| 84 | _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE); |
| 85 | _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); |
| 86 | |
| 87 | /* Do not not display the a error message box. */ |
| 88 | UINT mode = SetErrorMode(0) | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX; |
| 89 | SetErrorMode(mode); |
| 90 | |
| 91 | SetUnhandledExceptionFilter(exception_filter); |
| 92 | } |
| 93 | |
| 94 | #else |
| 95 | |
| 96 | static void init_signal_handling() { |
| 97 | // Alpine Linux does not have backtrace. |
| 98 | #ifdef HAVE_STACKTRACE |
| 99 | my_init_stacktrace(); |
| 100 | #endif |
| 101 | |
| 102 | struct sigaction sa; |
| 103 | sa.sa_flags = SA_RESETHAND | SA_NODEFER; |
| 104 | sigemptyset(&sa.sa_mask); |
| 105 | sigprocmask(SIG_SETMASK, &sa.sa_mask, nullptr); |
| 106 | |
| 107 | sa.sa_handler = signal_handler; |
| 108 | // Treat these as fatal and handle them. |
| 109 | sigaction(SIGABRT, &sa, nullptr); |
| 110 | sigaction(SIGFPE, &sa, nullptr); |
| 111 | // Handle these as well, except for ASAN/UBSAN builds: |
| 112 | // we let sanitizer runtime handle them instead. |
| 113 | #if defined(HANDLE_FATAL_SIGNALS) |
| 114 | sigaction(SIGBUS, &sa, nullptr); |
| 115 | sigaction(SIGILL, &sa, nullptr); |
| 116 | sigaction(SIGSEGV, &sa, nullptr); |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | #endif |
| 121 | |
| 122 | // Some globals needed for "small" tests. |
| 123 | mysql_mutex_t LOCK_open; |
| 124 | uint opt_debug_sync_timeout = 0; |
| 125 | thread_local MEM_ROOT **THR_MALLOC = nullptr; |
| 126 | thread_local THD *current_thd = nullptr; |
| 127 | size_t malloc_chunk_size = 1024; |
| 128 | |
| 129 | extern "C" void sql_alloc_error_handler(void) { ADD_FAILURE(); } |
| 130 | |
| 131 | int compare_malloc_chunks(void *a, void *b, size_t sz) { |
| 132 | return memcmp(a, b, sz); |
| 133 | } |
| 134 | |
| 135 | int main(int argc, char **argv) { |
| 136 | ::testing::InitGoogleTest(&argc, argv); |
| 137 | ::testing::InitGoogleMock(&argc, argv); |
| 138 | MY_INIT(argv[0]); |
| 139 | |
| 140 | mysql_mutex_init(PSI_NOT_INSTRUMENTED, &LOCK_open, MY_MUTEX_INIT_FAST); |
| 141 | |
| 142 | #ifndef _WIN32 |
| 143 | rlimit core_limit; |
| 144 | core_limit.rlim_cur = 0; |
| 145 | core_limit.rlim_max = 0; |
| 146 | setrlimit(RLIMIT_CORE, &core_limit); |
| 147 | #endif |
| 148 | |
| 149 | init_signal_handling(); |
| 150 | |
| 151 | const int retval = RUN_ALL_TESTS(); |
| 152 | mysql_mutex_destroy(&LOCK_open); |
| 153 | my_end(0); |
| 154 | return retval; |
| 155 | } |