Skip to content

Commit f33367f

Browse files
committed
Fixed a LOT of memory leaks in mariabackup
This was generally good to get done but also needed to be able to run mariabackup test under asan. Things freed: - Allocated variables (mysql_tmpdir_list, opt_passwd etc) - InnoDB variables - Results from SQL queries (A lot of sql queries did not free their result) - Allocated sys_vars - Server variables (mysql_server_end()) - Memory allocated by plugins (encryption) - Free variables allocated by my_default. (Old code had a bug that caused these to not be freed) Other things: - Moved freeing of mysql_tmpdir_list to main, as the old code did not free the last mysqltmp_dir allocation. Now we also initialize the variable only once. - Fixed a serious, potentially 'crashing at end' bug where we called free_defaults() with wrong pointers. - Fixed a bug related to update_malloc_size() where we did not take into account the it was not changed. - Fixed a bug in Sys_var_charptr_base where we did not allocate default values. This could lead to trying to free not allocated values in xtrabackup. - Added sf_have_memory_leak() to be able to easily check if there was a memory leak when using safemalloc() - sf_report_leaked_memory() now returns 1 if a memory leak was found.
1 parent f65dda6 commit f33367f

File tree

8 files changed

+84
-36
lines changed

8 files changed

+84
-36
lines changed

extra/mariabackup/backup_mysql.cc

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ bool have_lock_wait_timeout = false;
7777
bool have_galera_enabled = false;
7878
bool have_multi_threaded_slave = false;
7979
bool have_gtid_slave = false;
80+
bool innobase_data_file_path_allocated= false;
8081

8182
/* Kill long selects */
8283
static mysql_mutex_t kill_query_thread_mutex;
@@ -498,21 +499,19 @@ bool get_mysql_vars(MYSQL *connection)
498499
}
499500

500501
if (innodb_data_file_path_var && *innodb_data_file_path_var)
501-
innobase_data_file_path= my_strdup(PSI_NOT_INSTRUMENTED,
502-
innodb_data_file_path_var, MYF(MY_FAE));
502+
innobase_data_file_path= my_once_strdup(innodb_data_file_path_var,
503+
MYF(MY_FAE));
503504

504505
if (innodb_data_home_dir_var)
505-
innobase_data_home_dir= my_strdup(PSI_NOT_INSTRUMENTED,
506-
innodb_data_home_dir_var, MYF(MY_FAE));
506+
innobase_data_home_dir= my_once_strdup(innodb_data_home_dir_var,
507+
MYF(MY_FAE));
507508

508509
if (innodb_log_group_home_dir_var && *innodb_log_group_home_dir_var)
509-
srv_log_group_home_dir= my_strdup(PSI_NOT_INSTRUMENTED,
510-
innodb_log_group_home_dir_var,
511-
MYF(MY_FAE));
510+
srv_log_group_home_dir= my_once_strdup(innodb_log_group_home_dir_var,
511+
MYF(MY_FAE));
512512

513513
if (innodb_undo_directory_var && *innodb_undo_directory_var)
514-
srv_undo_dir= my_strdup(PSI_NOT_INSTRUMENTED, innodb_undo_directory_var,
515-
MYF(MY_FAE));
514+
srv_undo_dir= my_once_strdup(innodb_undo_directory_var, MYF(MY_FAE));
516515

517516
if (innodb_log_file_size_var)
518517
{
@@ -534,10 +533,7 @@ bool get_mysql_vars(MYSQL *connection)
534533
}
535534

536535
if (aria_log_dir_path_var)
537-
{
538-
aria_log_dir_path= my_strdup(PSI_NOT_INSTRUMENTED,
539-
aria_log_dir_path_var, MYF(MY_FAE));
540-
}
536+
aria_log_dir_path= my_once_strdup(aria_log_dir_path_var, MYF(MY_FAE));
541537

542538
if (page_zip_level_var != NULL)
543539
{
@@ -550,11 +546,11 @@ bool get_mysql_vars(MYSQL *connection)
550546
xb_load_list_string(ignore_db_dirs, ",", register_ignore_db_dirs_filter);
551547

552548
out:
553-
free_mysql_variables(mysql_vars);
554549

555550
return (ret);
556551
}
557552

553+
558554
static
559555
bool
560556
select_incremental_lsn_from_history(lsn_t *incremental_lsn)
@@ -930,7 +926,7 @@ lock_for_backup_stage_flush(MYSQL *connection) {
930926
if (opt_kill_long_queries_timeout) {
931927
start_query_killer();
932928
}
933-
xb_mysql_query(connection, "BACKUP STAGE FLUSH", true);
929+
xb_mysql_query(connection, "BACKUP STAGE FLUSH", false);
934930
if (opt_kill_long_queries_timeout) {
935931
stop_query_killer();
936932
}
@@ -942,7 +938,7 @@ lock_for_backup_stage_block_ddl(MYSQL *connection) {
942938
if (opt_kill_long_queries_timeout) {
943939
start_query_killer();
944940
}
945-
xb_mysql_query(connection, "BACKUP STAGE BLOCK_DDL", true);
941+
xb_mysql_query(connection, "BACKUP STAGE BLOCK_DDL", false);
946942
DBUG_MARIABACKUP_EVENT("after_backup_stage_block_ddl", {});
947943
if (opt_kill_long_queries_timeout) {
948944
stop_query_killer();
@@ -955,7 +951,7 @@ lock_for_backup_stage_commit(MYSQL *connection) {
955951
if (opt_kill_long_queries_timeout) {
956952
start_query_killer();
957953
}
958-
xb_mysql_query(connection, "BACKUP STAGE BLOCK_COMMIT", true);
954+
xb_mysql_query(connection, "BACKUP STAGE BLOCK_COMMIT", false);
959955
DBUG_MARIABACKUP_EVENT("after_backup_stage_block_commit", {});
960956
if (opt_kill_long_queries_timeout) {
961957
stop_query_killer();
@@ -966,12 +962,12 @@ lock_for_backup_stage_commit(MYSQL *connection) {
966962
bool backup_lock(MYSQL *con, const char *table_name) {
967963
static const std::string backup_lock_prefix("BACKUP LOCK ");
968964
std::string backup_lock_query = backup_lock_prefix + table_name;
969-
xb_mysql_query(con, backup_lock_query.c_str(), true);
965+
xb_mysql_query(con, backup_lock_query.c_str(), false);
970966
return true;
971967
}
972968

973969
bool backup_unlock(MYSQL *con) {
974-
xb_mysql_query(con, "BACKUP UNLOCK", true);
970+
xb_mysql_query(con, "BACKUP UNLOCK", false);
975971
return true;
976972
}
977973

@@ -985,6 +981,8 @@ get_tables_in_use(MYSQL *con) {
985981
msg("Table %s is in use", tk.c_str());
986982
result.insert(std::move(tk));
987983
}
984+
if (q_res)
985+
mysql_free_result(q_res);
988986
return result;
989987
}
990988

extra/mariabackup/backup_mysql.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,4 @@ bool
9797
write_slave_info(ds_ctxt *datasink, MYSQL *connection);
9898

9999
ulonglong get_current_lsn(MYSQL *connection);
100-
101100
#endif

extra/mariabackup/xtrabackup.cc

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ int sd_notifyf() { return 0; }
129129
}
130130

131131
int sys_var_init();
132+
void sys_var_end();
132133

133134
extern const char* fts_common_tables[];
134135
extern const fts_index_selector_t fts_index_selector[];
@@ -396,6 +397,7 @@ char *opt_incremental_history_uuid;
396397

397398
char *opt_user;
398399
const char *opt_password;
400+
bool free_opt_password;
399401
char *opt_host;
400402
char *opt_defaults_group;
401403
char *opt_socket;
@@ -2456,19 +2458,23 @@ xb_get_one_option(const struct my_option *opt,
24562458

24572459
static bool innodb_init_param()
24582460
{
2461+
static bool mysql_tmpdir_list_set= 0;
24592462
if (!ut_is_2pow(log_sys.write_size)) {
24602463
msg("InnoDB: innodb_log_write_ahead_size=%u"
24612464
" is not a power of two", log_sys.write_size);
24622465
return true;
24632466
}
24642467
srv_is_being_started = TRUE;
24652468
/* === some variables from mysqld === */
2466-
memset((G_PTR) &mysql_tmpdir_list, 0, sizeof(mysql_tmpdir_list));
2467-
2468-
if (init_tmpdir(&mysql_tmpdir_list, opt_mysql_tmpdir)) {
2469-
msg("init_tmpdir() failed");
2470-
return true;
2471-
}
2469+
if (!mysql_tmpdir_list_set)
2470+
{
2471+
mysql_tmpdir_list_set= 1;
2472+
memset((G_PTR) &mysql_tmpdir_list, 0, sizeof(mysql_tmpdir_list));
2473+
if (init_tmpdir(&mysql_tmpdir_list, opt_mysql_tmpdir)) {
2474+
msg("init_tmpdir() failed");
2475+
return true;
2476+
}
2477+
}
24722478
xtrabackup_tmpdir = my_tmpdir(&mysql_tmpdir_list);
24732479
/* dummy for initialize all_charsets[] */
24742480
get_charset_name(0);
@@ -6628,7 +6634,6 @@ void
66286634
innodb_free_param()
66296635
{
66306636
srv_sys_space.shutdown();
6631-
free_tmpdir(&mysql_tmpdir_list);
66326637
}
66336638

66346639

@@ -7283,6 +7288,8 @@ void setup_error_messages()
72837288
void handle_options(int argc, char **argv, char ***argv_server,
72847289
char ***argv_client, char ***argv_backup)
72857290
{
7291+
char **save_argv_server, **save_argv_client, **save_argv_backup;
7292+
72867293
/* Setup some variables for Innodb.*/
72877294
srv_operation = SRV_OPERATION_RESTORE;
72887295

@@ -7400,6 +7407,7 @@ void handle_options(int argc, char **argv, char ***argv_server,
74007407

74017408
load_defaults_or_exit(conf_file, &server_default_groups[0],
74027409
&argc_server, argv_server);
7410+
save_argv_server= *argv_server;
74037411

74047412
int n;
74057413
for (n = 0; (*argv_server)[n]; n++) {};
@@ -7445,6 +7453,7 @@ void handle_options(int argc, char **argv, char ***argv_server,
74457453

74467454
load_defaults_or_exit(conf_file, xb_client_default_groups,
74477455
&argc_client, argv_client);
7456+
save_argv_client= *argv_client;
74487457

74497458
for (n = 0; (*argv_client)[n]; n++) {};
74507459
argc_client = n;
@@ -7465,6 +7474,8 @@ void handle_options(int argc, char **argv, char ***argv_server,
74657474

74667475
load_defaults_or_exit(conf_file, backup_default_groups, &argc_backup,
74677476
argv_backup);
7477+
save_argv_backup= *argv_backup;
7478+
74687479
for (n= 0; (*argv_backup)[n]; n++)
74697480
{
74707481
};
@@ -7498,6 +7509,7 @@ void handle_options(int argc, char **argv, char ***argv_server,
74987509
char *start= (char*) opt_password;
74997510
opt_password= my_strdup(PSI_NOT_INSTRUMENTED, opt_password,
75007511
MYF(MY_FAE));
7512+
free_opt_password= 1;
75017513
while (*argument)
75027514
*argument++= 'x'; // Destroy argument
75037515
if (*start)
@@ -7543,6 +7555,14 @@ void handle_options(int argc, char **argv, char ***argv_server,
75437555
}
75447556
}
75457557
}
7558+
/*
7559+
Restore load defaults argument to the value after
7560+
load_defaults_or_exit(). This is needed for caller
7561+
when calling free_defaults()
7562+
*/
7563+
*argv_server= save_argv_server;
7564+
*argv_client= save_argv_client;
7565+
*argv_backup= save_argv_backup;
75467566
}
75477567

75487568
static int main_low(char** argv);
@@ -7641,10 +7661,21 @@ int main(int argc, char **argv)
76417661
cleanup_errmsgs();
76427662
free_error_messages();
76437663
mysql_mutex_destroy(&LOCK_error_log);
7664+
free_tmpdir(&mysql_tmpdir_list);
7665+
if (free_opt_password)
7666+
my_free((char*) opt_password);
7667+
plugin_shutdown();
7668+
free_list(opt_plugin_load_list_ptr);
7669+
mysql_server_end();
7670+
sys_var_end();
76447671

76457672
if (status == EXIT_SUCCESS) {
7646-
msg("completed OK!");
7673+
msg("completed OK!");
76477674
}
7675+
my_end(MY_CHECK_ERROR);
7676+
sf_leaking_memory= 0;
7677+
if (SAFEMALLOC_HAVE_MEMORY_LEAK)
7678+
status= EXIT_FAILURE;
76487679

76497680
return status;
76507681
}

include/my_sys.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,15 @@ char *guess_malloc_library();
152152

153153
/* If we have our own safemalloc (for debugging) */
154154
#if defined(SAFEMALLOC)
155-
void sf_report_leaked_memory(my_thread_id id);
155+
my_bool sf_report_leaked_memory(my_thread_id id);
156156
int sf_sanity();
157+
my_bool sf_have_memory_leak();
157158
extern my_thread_id (*sf_malloc_dbug_id)(void);
158159
#define SAFEMALLOC_REPORT_MEMORY(X) if (!sf_leaking_memory) sf_report_leaked_memory(X)
160+
#define SAFEMALLOC_HAVE_MEMORY_LEAK sf_have_memory_leak()
159161
#else
160162
#define SAFEMALLOC_REPORT_MEMORY(X) do {} while(0)
163+
#define SAFEMALLOC_HAVE_MEMORY_LEAK 0
161164
#endif
162165

163166
typedef void (*MALLOC_SIZE_CB) (long long size, my_bool is_thread_specific);

mysys/my_malloc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void *my_malloc(PSI_memory_key key, size_t size, myf my_flags)
107107
int flag= MY_TEST(my_flags & MY_THREAD_SPECIFIC);
108108
mh->m_size= size | flag;
109109
mh->m_key= PSI_CALL_memory_alloc(key, size, & mh->m_owner);
110-
if (update_malloc_size)
110+
if (update_malloc_size != dummy)
111111
{
112112
mh->m_size|=2;
113113
update_malloc_size(size + HEADER_SIZE, flag);
@@ -176,7 +176,7 @@ void *my_realloc(PSI_memory_key key, void *old_point, size_t size, myf my_flags)
176176
{
177177
mh->m_size= size | old_flags;
178178
mh->m_key= PSI_CALL_memory_realloc(key, old_size, size, & mh->m_owner);
179-
if (update_malloc_size && (old_flags & 2))
179+
if (update_malloc_size != dummy && (old_flags & 2))
180180
update_malloc_size((longlong)size - (longlong)old_size, old_flags & 1);
181181
point= HEADER_TO_USER(mh);
182182
}
@@ -207,7 +207,7 @@ void my_free(void *ptr)
207207
old_flags= mh->m_size & 3;
208208
PSI_CALL_memory_free(mh->m_key, old_size, mh->m_owner);
209209

210-
if (update_malloc_size && (old_flags & 2))
210+
if (update_malloc_size != dummy && (old_flags & 2))
211211
update_malloc_size(- (longlong) old_size - HEADER_SIZE, old_flags & 1);
212212

213213
#ifndef SAFEMALLOC

mysys/safemalloc.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ int sf_sanity()
372372
if (count || irem)
373373
{
374374
warn("Error: Safemalloc link list destroyed");
375-
flag= 1;
375+
flag++;
376376
}
377377
return flag;
378378
}
@@ -383,7 +383,7 @@ int sf_sanity()
383383
@param id Id of thread to report. 0 if all
384384
*/
385385

386-
void sf_report_leaked_memory(my_thread_id id)
386+
my_bool sf_report_leaked_memory(my_thread_id id)
387387
{
388388
size_t total= 0;
389389
struct st_irem *irem;
@@ -411,7 +411,7 @@ void sf_report_leaked_memory(my_thread_id id)
411411
if (total)
412412
fprintf(stderr, "Memory lost: %lu bytes in %u chunks of %u total chunks\n",
413413
(ulong) total, chunks, sf_malloc_count);
414-
return;
414+
return total != 0;
415415
}
416416

417417
static void sf_terminate()
@@ -422,4 +422,9 @@ static void sf_terminate()
422422
pthread_mutex_destroy(&sf_mutex);
423423
}
424424

425+
my_bool sf_have_memory_leak()
426+
{
427+
return sf_malloc_root != 0;
428+
}
429+
425430
#endif

sql/sys_vars.inl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,10 @@ public:
520520
(think of an exit because of an error right after my_getopt)
521521
*/
522522
option.var_type|= (flags & ALLOCATED) ? GET_STR_ALLOC : GET_STR;
523-
global_var(const char*)= def_val;
523+
if (def_val && (flags & ALLOCATED))
524+
global_var(const char*)= my_strdup(PSI_INSTRUMENT_ME, def_val, MYF(0));
525+
else
526+
global_var(const char*)= def_val;
524527
}
525528
void cleanup() override
526529
{

sql/wsrep_var.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ ulong wsrep_reject_queries;
3333

3434
int wsrep_init_vars()
3535
{
36+
my_free((char*) wsrep_provider);
37+
my_free((char*) wsrep_provider_options);
38+
my_free((char*) wsrep_cluster_address);
39+
my_free((char*) wsrep_cluster_name);
40+
my_free((char*) wsrep_node_name);
41+
my_free((char*) wsrep_node_address);
42+
my_free((char*) wsrep_node_incoming_address);
43+
my_free((char*) wsrep_start_position);
44+
3645
wsrep_provider = my_strdup(PSI_INSTRUMENT_ME, WSREP_NONE, MYF(MY_WME));
3746
wsrep_provider_options= my_strdup(PSI_INSTRUMENT_ME, "", MYF(MY_WME));
3847
wsrep_cluster_address = my_strdup(PSI_INSTRUMENT_ME, "", MYF(MY_WME));

0 commit comments

Comments
 (0)