public inbox for glibc-bugs@sourceware.org help / color / mirror / Atom feed
From: "keyid.w at qq dot com" <sourceware-bugzilla@sourceware.org> To: glibc-bugs@sourceware.org Subject: [Bug malloc/26969] New: A common malloc pattern can make memory not given back to OS Date: Sat, 28 Nov 2020 13:58:35 +0000 [thread overview] Message-ID: <bug-26969-131@http.sourceware.org/bugzilla/> (raw) https://sourceware.org/bugzilla/show_bug.cgi?id=26969 Bug ID: 26969 Summary: A common malloc pattern can make memory not given back to OS Product: glibc Version: 2.27 Status: UNCONFIRMED Severity: enhancement Priority: P2 Component: malloc Assignee: unassigned at sourceware dot org Reporter: keyid.w at qq dot com Target Milestone: --- Some malloc patterns can easily make almost all freed memory not given back to OS before program exits. An example is alternatively malloc-ing small-sized memory(67th line of the following codes) and large-sized memory(71st line of the following codes) and then free-ing them. You can try to run the program with arguments "1000 30 3 20" or "1000 30 0 20". The third number stands for thread number. You can also run with others arguments if you read the detail of program and know the meaning of other arguments. I used gdb to check the heap's state, and I believe this is caused by tcache/fast bins since free chunks in these bins are still considered "in use". Some tcache/fase bins stayed close to the heap's top so nearly all chunks couldn't be released. Though I know this is closer to a "corner case" of design than a bug, I think this malloc pattern is very common(alternatively small and large pieces) so programs' memory can easily be not released. Jemalloc performs much better in this case. Would you consider to improve this? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <malloc.h> #define MAX_TICK_NUM 4800 #define MAX_FIELD_NUM 30 #define MAX_IID_NUM 4000 #define MAX_FIELD_NAME_LEN 20 #define MAX_THREAD_NUM 32 typedef struct tick_data_s { char *names[MAX_FIELD_NUM]; double *values[MAX_FIELD_NUM]; int size; } tick_data_t; typedef struct thread_args_s { int n_ticks; int n_fields; int field_name_len; tick_data_t *ticks; } thread_args_t; void init_thread_args(thread_args_t *t, int n_ticks, int n_fields, int field_name_len) { t->n_ticks = n_ticks; t->n_fields = n_fields; t->field_name_len = field_name_len; t->ticks = NULL; } void _free_tick_data(thread_args_t *t) { for (int i = 0; i < t->n_ticks; ++i) { for (int j = 0; j < t->ticks[i].size; ++j) { free(t->ticks[i].names[j]); free(t->ticks[i].values[j]); } } } void free_thread_args(thread_args_t *t) { if (t->ticks) { _free_tick_data(t); free(t->ticks); t->ticks = NULL; } } void _fill_tick_data(thread_args_t *t) { for (int i = 0; i < t->n_ticks; ++i) { for (int j = 0; j < t->n_fields; ++j) { if (t->field_name_len > 0) { char *name = (char *)malloc(t->field_name_len); name[0] = (char)j; t->ticks[i].names[j] = name; } double *value = (double *)malloc(t->n_ticks * sizeof(double)); for (int k = 0; k < t->n_ticks; ++k) { value[k] = 1.0 * (i * k + j) / t->n_ticks; } t->ticks[i].values[j] = value; } t->ticks[i].size = t->n_fields; } } void *thread_main_func(void *args) { int thread_id = (int)pthread_self(); printf("thread created 0x%x\n", thread_id); thread_args_t *t = (thread_args_t *)args; t->ticks = (tick_data_t *)malloc(t->n_ticks * sizeof(tick_data_t)); _fill_tick_data(t); printf("thread done 0x%x\n", thread_id); return NULL; } int main(int argc, char *argv[]) { if (argc != 5) { printf("Usage: glibc_tcache_exploit <tick_num> <field_num> <thread_num> <field_name_len>\n"); return 0; } //mallopt(M_MXFAST, 0); //mallopt(M_MMAP_THRESHOLD, 4096); int i = 0; int tick_num = atoi(argv[++i]); int field_num = atoi(argv[++i]); int thread_num = atoi(argv[++i]); int field_name_len = atoi(argv[++i]); printf("tick_num:%d field_num:%d thread_num:%d\n", tick_num, field_num, thread_num); printf("field_name_len:%d\n", field_name_len); if (thread_num > 0) { pthread_t threads[MAX_THREAD_NUM]; thread_args_t thread_args[MAX_THREAD_NUM]; for (int i = 0; i < thread_num; ++i) { init_thread_args(&thread_args[i], tick_num, field_num, field_name_len); pthread_create(&threads[i], NULL, thread_main_func, &thread_args[i]); } printf("before join "); getchar(); for (int i = 0; i < thread_num; ++i) { pthread_join(threads[i], NULL); free_thread_args(&thread_args[i]); } } else { thread_args_t thread_arg; init_thread_args(&thread_arg, tick_num, field_num, field_name_len); thread_main_func(&thread_arg); free_thread_args(&thread_arg); } printf("before return "); getchar(); return 0; } -- You are receiving this mail because: You are on the CC list for the bug.
next reply other threads:[~2020-11-28 13:58 UTC|newest] Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-11-28 13:58 keyid.w at qq dot com [this message] 2020-11-28 13:59 ` [Bug malloc/26969] " keyid.w at qq dot com 2020-11-29 3:24 ` keyid.w at qq dot com 2020-12-01 1:03 ` uwydoc at gmail dot com 2020-12-01 2:51 ` carlos at redhat dot com 2020-12-01 8:43 ` keyid.w at qq dot com 2021-01-29 16:08 ` dimahabr at gmail dot com 2021-02-01 8:52 ` keyid.w at qq dot com 2022-06-29 16:34 ` romash at rbbn dot com
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-26969-131@http.sourceware.org/bugzilla/ \ --to=sourceware-bugzilla@sourceware.org \ --cc=glibc-bugs@sourceware.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).