| 1 | /* Copyright (c) 2013, 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 | /** |
| 25 | This is a unit test written to demonstrate a race issue in debug_sync |
| 26 | functionality which resulted in wait thread getting timed out. This |
| 27 | happens because we use a single global variable to identify the thread |
| 28 | that has signalled. Before the wait could process the signal from the |
| 29 | signal thread, another signal thread can overwrite this global signal |
| 30 | value thus making the wait thread to lose this signal and timeout |
| 31 | waiting for a signal that it has already lost. |
| 32 | */ |
| 33 | |
| 34 | #include <gtest/gtest.h> |
| 35 | #include <string> |
| 36 | |
| 37 | #include "my_dbug.h" |
| 38 | #include "my_inttypes.h" |
| 39 | #include "sql/debug_sync.h" |
| 40 | #include "unittest/gunit/test_utils.h" |
| 41 | #include "unittest/gunit/thread_utils.h" |
| 42 | |
| 43 | #if defined(ENABLED_DEBUG_SYNC) |
| 44 | extern uchar *debug_sync_value_ptr(THD *thd); |
| 45 | |
| 46 | namespace debug_sync_unittest { |
| 47 | |
| 48 | using my_testing::Mock_error_handler; |
| 49 | using my_testing::Server_initializer; |
| 50 | using thread::Notification; |
| 51 | using thread::Thread; |
| 52 | |
| 53 | class DebugSyncTest : public ::testing::Test { |
| 54 | protected: |
| 55 | DebugSyncTest() = default; |
| 56 | |
| 57 | void SetUp() override { |
| 58 | // set debug sync timeout of 60 seconds. |
| 59 | opt_debug_sync_timeout = 60; |
| 60 | debug_sync_init(); |
| 61 | } |
| 62 | |
| 63 | void TearDown() override { |
| 64 | debug_sync_end(); |
| 65 | opt_debug_sync_timeout = 0; |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | Set up a debug sync action thread. Depending on type of action, the thread |
| 71 | serves different purpose. In the case where it involves wait_for action, it |
| 72 | serves the purpose of a waiting thread that wait at a 'sync_point' in it's |
| 73 | thread of control for named signal event. The debug sync action could |
| 74 | be a signal event, in which case it assumes the role of signalling thread |
| 75 | that signals an event identified by signal name to wake a waiting thread. |
| 76 | */ |
| 77 | class DebugSyncThread : public Thread { |
| 78 | public: |
| 79 | DebugSyncThread(Notification *go, bool wait_thread, std::string action) |
| 80 | : m_go(go), m_wait_thread(wait_thread), m_action(action) {} |
| 81 | |
| 82 | virtual void run() { |
| 83 | m_initializer.SetUp(); |
| 84 | m_thd = m_initializer.thd(); |
| 85 | m_go->wait_for_notification(); |
| 86 | |
| 87 | if (m_wait_thread) { |
| 88 | std::string sync_point("sync_point "); |
| 89 | std::string action_str = sync_point + m_action; |
| 90 | Mock_error_handler error_handler(m_thd, 0); |
| 91 | |
| 92 | debug_sync_set_action(m_thd, action_str.c_str(), action_str.length()); |
| 93 | wait_point(); |
| 94 | |
| 95 | // The above should not generate any warnings. |
| 96 | EXPECT_EQ(0, error_handler.handle_called()); |
| 97 | } else { |
| 98 | debug_sync_set_action(m_thd, m_action.c_str(), m_action.length()); |
| 99 | } |
| 100 | |
| 101 | m_initializer.TearDown(); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | Notification *m_go; |
| 106 | Server_initializer m_initializer; |
| 107 | THD *m_thd; // THD context required to hold debug_sync context. |
| 108 | bool m_wait_thread; // indicate if it is a wait thread. |
| 109 | std::string m_action; // indicate type of action WAIT_FOR or SIGNAL. |
| 110 | |
| 111 | void wait_point() { DEBUG_SYNC(m_thd, "sync_point"); } |
| 112 | }; |
| 113 | |
| 114 | /* |
| 115 | Start a wait thread waiting for signal X1 and two signal threads |
| 116 | one signalling event X1 and other signalling an event X2. Notify |
| 117 | all of them to start at once. |
| 118 | */ |
| 119 | TEST_F(DebugSyncTest, DebugSyncSignalWaitTests) { |
| 120 | Notification go[3]; |
| 121 | |
| 122 | /* wait thread */ |
| 123 | DebugSyncThread wait_thread(&go[0], true, std::string("WAIT_FOR X1")); |
| 124 | /* signal thread */ |
| 125 | DebugSyncThread signal_thread1(&go[1], false, std::string("now signal X1")); |
| 126 | DebugSyncThread signal_thread2(&go[2], false, std::string("now signal X2")); |
| 127 | |
| 128 | wait_thread.start(); |
| 129 | signal_thread1.start(); |
| 130 | signal_thread2.start(); |
| 131 | |
| 132 | // Notify all threads |
| 133 | go[0].notify(); |
| 134 | go[1].notify(); |
| 135 | go[2].notify(); |
| 136 | |
| 137 | // Wait for all threads to finish. |
| 138 | wait_thread.join(); |
| 139 | signal_thread1.join(); |
| 140 | signal_thread2.join(); |
| 141 | } |
| 142 | |
| 143 | // Test debug_sync_value_ptr |
| 144 | TEST_F(DebugSyncTest, DebugSyncValuesTest) { |
| 145 | std::string action; |
| 146 | Server_initializer server_initializer; |
| 147 | THD *thd; |
| 148 | |
| 149 | server_initializer.SetUp(); |
| 150 | thd = server_initializer.thd(); |
| 151 | |
| 152 | // Ensure that we have a empty signal list at startup. |
| 153 | EXPECT_STREQ("ON - signals: ''", |
| 154 | reinterpret_cast<char *>(debug_sync_value_ptr(thd))); |
| 155 | |
| 156 | // Set up signalling actions and ensure the signals list reflect it. |
| 157 | action = "now signal x1"; |
| 158 | debug_sync_set_action(thd, action.c_str(), action.length()); |
| 159 | EXPECT_STREQ("ON - signals: 'x1'", |
| 160 | reinterpret_cast<char *>(debug_sync_value_ptr(thd))); |
| 161 | |
| 162 | action = "now signal x2"; |
| 163 | debug_sync_set_action(thd, action.c_str(), action.length()); |
| 164 | EXPECT_STREQ("ON - signals: 'x1,x2'", |
| 165 | reinterpret_cast<char *>(debug_sync_value_ptr(thd))); |
| 166 | |
| 167 | action = "now signal x3"; |
| 168 | debug_sync_set_action(thd, action.c_str(), action.length()); |
| 169 | EXPECT_STREQ("ON - signals: 'x1,x2,x3'", |
| 170 | reinterpret_cast<char *>(debug_sync_value_ptr(thd))); |
| 171 | |
| 172 | // Ensure the signal list is empty after reset. |
| 173 | action = "reset"; |
| 174 | debug_sync_set_action(thd, action.c_str(), action.length()); |
| 175 | EXPECT_STREQ("ON - signals: ''", |
| 176 | reinterpret_cast<char *>(debug_sync_value_ptr(thd))); |
| 177 | |
| 178 | server_initializer.TearDown(); |
| 179 | } |
| 180 | |
| 181 | } // namespace debug_sync_unittest |
| 182 | #endif |