| 1 | /* Copyright (c) 2011, 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 "storage/perfschema/unittest/pfs_unit_test_conf.h" |
| 25 | |
| 26 | #include <string.h> /* memset */ |
| 27 | |
| 28 | #include "my_thread.h" |
| 29 | #include "storage/perfschema/pfs_buffer_container.h" |
| 30 | #include "storage/perfschema/pfs_global.h" |
| 31 | #include "storage/perfschema/pfs_instr.h" |
| 32 | #include "storage/perfschema/pfs_stat.h" |
| 33 | #include "storage/perfschema/pfs_user.h" |
| 34 | #include "storage/perfschema/unittest/stub_digest.h" |
| 35 | #include "storage/perfschema/unittest/stub_pfs_global.h" |
| 36 | #include "storage/perfschema/unittest/stub_pfs_plugin_table.h" |
| 37 | #include "storage/perfschema/unittest/stub_pfs_tls_channel.h" |
| 38 | #include "storage/perfschema/unittest/stub_server_telemetry.h" |
| 39 | #include "unittest/mytap/tap.h" |
| 40 | |
| 41 | static void test_oom() { |
| 42 | int rc; |
| 43 | PFS_global_param param; |
| 44 | PSI_thread_service_t *thread_service; |
| 45 | PSI_thread_bootstrap *thread_boot; |
| 46 | PSI_mutex_bootstrap *mutex_boot; |
| 47 | PSI_rwlock_bootstrap *rwlock_boot; |
| 48 | PSI_cond_bootstrap *cond_boot; |
| 49 | PSI_file_bootstrap *file_boot; |
| 50 | PSI_socket_bootstrap *socket_boot; |
| 51 | PSI_table_bootstrap *table_boot; |
| 52 | PSI_mdl_bootstrap *mdl_boot; |
| 53 | PSI_idle_bootstrap *idle_boot; |
| 54 | PSI_stage_bootstrap *stage_boot; |
| 55 | PSI_statement_bootstrap *statement_boot; |
| 56 | PSI_transaction_bootstrap *transaction_boot; |
| 57 | PSI_memory_bootstrap *memory_boot; |
| 58 | PSI_error_bootstrap *error_boot; |
| 59 | PSI_data_lock_bootstrap *data_lock_boot; |
| 60 | PSI_system_bootstrap *system_boot; |
| 61 | PSI_tls_channel_bootstrap *tls_channel_boot; |
| 62 | |
| 63 | memset(¶m, 0xFF, sizeof(param)); |
| 64 | param.m_enabled = true; |
| 65 | param.m_mutex_class_sizing = 0; |
| 66 | param.m_rwlock_class_sizing = 0; |
| 67 | param.m_cond_class_sizing = 0; |
| 68 | param.m_thread_class_sizing = 10; |
| 69 | param.m_table_share_sizing = 0; |
| 70 | param.m_file_class_sizing = 0; |
| 71 | param.m_socket_class_sizing = 0; |
| 72 | param.m_mutex_sizing = 0; |
| 73 | param.m_rwlock_sizing = 0; |
| 74 | param.m_cond_sizing = 0; |
| 75 | param.m_thread_sizing = 1000; |
| 76 | param.m_table_sizing = 0; |
| 77 | param.m_file_sizing = 0; |
| 78 | param.m_file_handle_sizing = 0; |
| 79 | param.m_socket_sizing = 0; |
| 80 | param.m_events_waits_history_sizing = 10; |
| 81 | param.m_events_waits_history_long_sizing = 0; |
| 82 | param.m_setup_actor_sizing = 0; |
| 83 | param.m_setup_object_sizing = 0; |
| 84 | param.m_host_sizing = 0; |
| 85 | param.m_user_sizing = 1000; |
| 86 | param.m_account_sizing = 0; |
| 87 | param.m_stage_class_sizing = 50; |
| 88 | param.m_events_stages_history_sizing = 0; |
| 89 | param.m_events_stages_history_long_sizing = 0; |
| 90 | param.m_statement_class_sizing = 50; |
| 91 | param.m_events_statements_history_sizing = 0; |
| 92 | param.m_events_statements_history_long_sizing = 0; |
| 93 | param.m_events_transactions_history_sizing = 0; |
| 94 | param.m_events_transactions_history_long_sizing = 0; |
| 95 | param.m_digest_sizing = 0; |
| 96 | param.m_session_connect_attrs_sizing = 0; |
| 97 | param.m_program_sizing = 0; |
| 98 | param.m_statement_stack_sizing = 0; |
| 99 | param.m_memory_class_sizing = 10; |
| 100 | param.m_metadata_lock_sizing = 0; |
| 101 | param.m_max_digest_length = 0; |
| 102 | param.m_max_sql_text_length = 0; |
| 103 | param.m_error_sizing = 0; |
| 104 | param.m_consumer_events_stages_current_enabled = false; |
| 105 | param.m_consumer_events_stages_history_enabled = false; |
| 106 | param.m_consumer_events_stages_history_long_enabled = false; |
| 107 | param.m_consumer_events_statements_cpu_enabled = false; |
| 108 | param.m_consumer_events_statements_current_enabled = false; |
| 109 | param.m_consumer_events_statements_history_enabled = false; |
| 110 | param.m_consumer_events_statements_history_long_enabled = false; |
| 111 | param.m_consumer_events_transactions_current_enabled = false; |
| 112 | param.m_consumer_events_transactions_history_enabled = false; |
| 113 | param.m_consumer_events_transactions_history_long_enabled = false; |
| 114 | param.m_consumer_events_waits_current_enabled = false; |
| 115 | param.m_consumer_events_waits_history_enabled = false; |
| 116 | param.m_consumer_events_waits_history_long_enabled = false; |
| 117 | param.m_consumer_global_instrumentation_enabled = false; |
| 118 | param.m_consumer_thread_instrumentation_enabled = false; |
| 119 | param.m_consumer_statement_digest_enabled = false; |
| 120 | |
| 121 | /* Setup */ |
| 122 | |
| 123 | stub_alloc_always_fails = false; |
| 124 | stub_alloc_fails_after_count = 1000; |
| 125 | |
| 126 | pre_initialize_performance_schema(); |
| 127 | rc = initialize_performance_schema( |
| 128 | ¶m, &thread_boot, &mutex_boot, &rwlock_boot, &cond_boot, &file_boot, |
| 129 | &socket_boot, &table_boot, &mdl_boot, &idle_boot, &stage_boot, |
| 130 | &statement_boot, &transaction_boot, &memory_boot, &error_boot, |
| 131 | &data_lock_boot, &system_boot, &tls_channel_boot); |
| 132 | ok(rc == 0, "init ok"); |
| 133 | thread_service = (PSI_thread_service_t *)thread_boot->get_interface( |
| 134 | PSI_CURRENT_THREAD_VERSION); |
| 135 | |
| 136 | PSI_thread_key thread_key_1; |
| 137 | PSI_thread_info all_thread[] = {{&thread_key_1, "T-1", "T-1", 0, 0, ""}}; |
| 138 | thread_service->register_thread("test", all_thread, 1); |
| 139 | |
| 140 | PSI_thread *thread_1 = |
| 141 | thread_service->new_thread(thread_key_1, 0, nullptr, 0); |
| 142 | thread_service->set_thread(thread_1); |
| 143 | |
| 144 | /* Tests */ |
| 145 | |
| 146 | int first_fail = 1; |
| 147 | stub_alloc_fails_after_count = first_fail; |
| 148 | thread_service->set_thread_account("user1", 5, "", 0); |
| 149 | ok(global_user_container.m_lost == 1, "oom (user)"); |
| 150 | |
| 151 | stub_alloc_fails_after_count = first_fail + 1; |
| 152 | thread_service->set_thread_account("user2", 5, "", 0); |
| 153 | ok(global_user_container.m_lost == 2, "oom (user waits)"); |
| 154 | |
| 155 | stub_alloc_fails_after_count = first_fail + 2; |
| 156 | thread_service->set_thread_account("user3", 5, "", 0); |
| 157 | ok(global_user_container.m_lost == 3, "oom (user stages)"); |
| 158 | |
| 159 | stub_alloc_fails_after_count = first_fail + 3; |
| 160 | thread_service->set_thread_account("user4", 5, "", 0); |
| 161 | ok(global_user_container.m_lost == 4, "oom (user statements)"); |
| 162 | |
| 163 | stub_alloc_fails_after_count = first_fail + 4; |
| 164 | thread_service->set_thread_account("user5", 5, "", 0); |
| 165 | ok(global_user_container.m_lost == 5, "oom (user transactions)"); |
| 166 | |
| 167 | stub_alloc_fails_after_count = first_fail + 5; |
| 168 | thread_service->set_thread_account("user6", 5, "", 0); |
| 169 | ok(global_user_container.m_lost == 6, "oom (user memory)"); |
| 170 | |
| 171 | shutdown_performance_schema(); |
| 172 | } |
| 173 | |
| 174 | static void do_all_tests() { test_oom(); } |
| 175 | |
| 176 | int main(int, char **) { |
| 177 | plan(7); |
| 178 | MY_INIT("pfs_user-oom-t"); |
| 179 | do_all_tests(); |
| 180 | my_end(0); |
| 181 | return (exit_status()); |
| 182 | } |