提交 07820e42 编写于 作者: O olefirenque 提交者: Maxim Polyakov

fix: get rid of critical bugs and some serious warnings

Signed-off-by: NMaxim Polyakov <polyakov.maksim@huawei.com>
上级 aa1f46f7
......@@ -7,6 +7,7 @@
#include "test-malloc-api-common.h"
#define BARRIER_HEIGHT 2
#define ALLOCATIONS_NUMBER 8
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
......@@ -32,8 +33,7 @@ static const size_t allocs_sizes[ALLOCATIONS_NUMBER] = {
2 * 1024 * 1024,
8 * 1024 * 1024,
16 * 1024 * 1024,
32 * 1024 * 1024
};
32 * 1024 * 1024};
void iterate_callback(void *base, size_t size, void *data)
{
......@@ -152,8 +152,8 @@ int test_iterate_another_thread(void)
{
int ret;
iterate_arg_t iterate_arg_routine = {{0}, {0}, {0}, {0}};
pthread_barrier_init(&routine_allocated, NULL, 2);
pthread_barrier_init(&routine_iterated, NULL, 2);
pthread_barrier_init(&routine_allocated, NULL, BARRIER_HEIGHT);
pthread_barrier_init(&routine_iterated, NULL, BARRIER_HEIGHT);
pthread_t thread_id;
pthread_create(&thread_id, NULL, allocate_routine, (void *)&iterate_arg_routine);
pthread_barrier_wait(&routine_allocated);
......
......@@ -8,6 +8,8 @@
#include "test-malloc-api-common.h"
#define BARRIER_HEIGHT 2
pthread_barrier_t routine_disabled;
pthread_barrier_t routine_allocated;
......@@ -26,8 +28,8 @@ void *disable_routine(void *vargp)
int test_malloc_while_disabled(void)
{
int ret = 0;
pthread_barrier_init(&routine_disabled, NULL, 2);
pthread_barrier_init(&routine_allocated, NULL, 2);
pthread_barrier_init(&routine_disabled, NULL, BARRIER_HEIGHT);
pthread_barrier_init(&routine_allocated, NULL, BARRIER_HEIGHT);
pthread_t thread_id;
pthread_create(&thread_id, NULL, disable_routine, NULL);
pthread_barrier_wait(&routine_disabled);
......
......@@ -2127,8 +2127,6 @@ musl_src_porting_file = [
"src/ldso/arm/dlvsym.s",
"src/ldso/riscv64/dlvsym.s",
"src/ldso/x86_64/dlvsym.s",
"src/thread/pthread_getspecific.c",
"src/thread/pthread_setspecific.c",
]
musl_inc_hook_files = [
......
......@@ -11,7 +11,6 @@
#include "malloc_impl.h"
#include "pthread_impl.h"
extern pthread_key_t occupied_bin_key;
static void dummy(void) {}
weak_alias(dummy, _init);
......@@ -104,7 +103,7 @@ static int libc_start_main_stage2(int (*main)(int,char **,char **), int argc, ch
__init_occupied_bin_key_once();
occupied_bin_t *occupied_bin = internal_calloc(sizeof(occupied_bin_t), 1);
if (occupied_bin == NULL) return ENOMEM;
pthread_setspecific(occupied_bin_key, occupied_bin);
pthread_setspecific(__get_occupied_bin_key(), occupied_bin);
#endif
libc.initialized = 1;
......
......@@ -2,6 +2,7 @@
#define MALLOC_IMPL_H
#include <sys/mman.h>
#include "pthread.h"
#include "malloc_config.h"
hidden void *__expand_heap(size_t *);
......@@ -49,8 +50,13 @@ hidden void __merge_bin_chunks(occupied_bin_t *target_bin, occupied_bin_t *sourc
hidden void __init_occupied_bin_key_once(void);
hidden void __push_chunk(struct chunk *c);
hidden void __pop_chunk(struct chunk *c);
hidden occupied_bin_t *__get_detached_occupied_bin(void);
hidden pthread_key_t __get_occupied_bin_key(void);
hidden size_t __get_total_heap_space(void);
#endif
#define BINS_COUNT 64
#define SIZE_MASK (-SIZE_ALIGN)
#ifdef MUSL_ITERATE_AND_STATS_API
......@@ -134,4 +140,4 @@ hidden void chunk_checksum_set(struct chunk *c);
hidden int chunk_checksum_check(struct chunk *c);
#endif
#endif
\ No newline at end of file
#endif
......@@ -20,6 +20,23 @@ pthread_key_t occupied_bin_key;
occupied_bin_t detached_occupied_bin;
/* Usable memory only, excluding overhead for chunks */
size_t total_heap_space = 0;
volatile int total_heap_space_inc_lock[2];
volatile int pop_merge_lock[2];
occupied_bin_t *__get_detached_occupied_bin(void) {
return &detached_occupied_bin;
}
pthread_key_t __get_detached_occupied_bin_key(void) {
return occupied_bin_key;
}
size_t __get_total_heap_space(void) {
return total_heap_space;
}
static pthread_once_t occupied_bin_key_is_initialized = PTHREAD_ONCE_INIT;
static void occupied_bin_destructor(void *occupied_bin)
......@@ -48,10 +65,9 @@ occupied_bin_t *__get_current_occupied_bin()
return __get_occupied_bin(__pthread_self());
}
/* Usable memory only, excluding overhead for chunks */
size_t total_heap_space = 0;
volatile int total_heap_space_inc_lock[2];
volatile int pop_merge_lock[2];
pthread_key_t __get_occupied_bin_key() {
return occupied_bin_key;
}
#endif
#ifdef HOOK_ENABLE
......@@ -61,14 +77,14 @@ void __libc_free(void *p);
static struct {
volatile uint64_t binmap;
struct bin bins[64];
struct bin bins[BINS_COUNT];
volatile int free_lock[2];
#ifdef MALLOC_FREELIST_QUARANTINE
struct bin quarantine[QUARANTINE_NUM];
size_t quarantined_count[QUARANTINE_NUM];
size_t quarantined_size[QUARANTINE_NUM];
#ifdef MALLOC_RED_ZONE
char poison[64];
char poison[BINS_COUNT];
volatile int poison_lock[2];
int poison_count_down;
#endif
......@@ -191,7 +207,7 @@ void malloc_disable(void)
#ifdef MUSL_ITERATE_AND_STATS_API
lock(mal.free_lock);
lock(total_heap_space_inc_lock);
for (size_t i = 0; i < 64; ++i) {
for (size_t i = 0; i < BINS_COUNT; ++i) {
lock(mal.bins[i].lock);
}
__tl_lock();
......@@ -218,7 +234,7 @@ void malloc_enable(void)
} while (it != self);
unlock(detached_occupied_bin.lock);
__tl_unlock();
for (size_t i = 0; i < 64; ++i) {
for (size_t i = 0; i < BINS_COUNT; ++i) {
unlock(mal.bins[i].lock);
}
unlock(total_heap_space_inc_lock);
......@@ -434,7 +450,7 @@ void __dump_heap(int x)
c, CHUNK_SIZE(c), bin_index(CHUNK_SIZE(c)),
c->csize & 15,
NEXT_CHUNK(c)->psize & 15);
for (i=0; i<64; i++) {
for (i=0; i<BINS_COUNT; i++) {
if (mal.bins[i].head != BIN_TO_CHUNK(i) && mal.bins[i].head) {
fprintf(stderr, "bin %d: %p\n", i, mal.bins[i].head);
if (!(mal.binmap & 1ULL<<i))
......@@ -1330,4 +1346,4 @@ int mallopt(int param, int value)
ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count)
{
return 0;
}
\ No newline at end of file
}
......@@ -7,6 +7,7 @@
#ifdef MUSL_ITERATE_AND_STATS_API
#define STAT_PRINTF_MAX_LEN 255
#define ALLOCATOR_VERSION 1
#define SEPARATOR_REPEATS 7
typedef void (write_cb_fun)(void *, const char *);
......@@ -21,10 +22,6 @@ typedef struct {
size_t total_allocated_heap_space;
} malloc_stats_t;
extern size_t total_heap_space;
extern occupied_bin_t detached_occupied_bin;
static void stat_printf(write_cb_fun *write_cb, void *write_cb_arg, const char *fmt, ...)
{
va_list args;
......@@ -135,7 +132,7 @@ static void print_abandoned_stats_xml(write_cb_fun *write_cb, void *write_cb_arg
static size_t print_abandoned(write_cb_fun *write_cb, void *write_cb_arg, print_mode mode)
{
malloc_stats_t stats = add_up_chunks(&detached_occupied_bin);
malloc_stats_t stats = add_up_chunks(__get_detached_occupied_bin());
if (mode == TABLE) {
print_abandoned_stats_table(write_cb, write_cb_arg, &stats);
} else {
......@@ -153,7 +150,7 @@ static void print_total_free_heap_space(
{
if (mode == TABLE) {
stat_printf(write_cb, write_cb_arg, "\n");
for (size_t i = 0; i < 7; i++) {
for (size_t i = 0; i < SEPARATOR_REPEATS; i++) {
stat_printf(
write_cb,
write_cb_arg,
......@@ -164,14 +161,14 @@ static void print_total_free_heap_space(
write_cb,
write_cb_arg,
"\ntotal free heap space: %zu\n",
total_heap_space - total_allocated_heap_space
__get_total_heap_space() - total_allocated_heap_space
);
} else {
print_amount_xml(
write_cb,
write_cb_arg,
"total_free_heap_space",
total_heap_space - total_allocated_heap_space
__get_total_heap_space() - total_allocated_heap_space
);
}
}
......@@ -243,14 +240,14 @@ struct mallinfo2 mallinfo2(void)
add_stats(&shared_stats, &stats);
it = it->next;
} while (it != self);
malloc_stats_t abandoned_stats = add_up_chunks(&detached_occupied_bin);
malloc_stats_t abandoned_stats = add_up_chunks(__get_detached_occupied_bin());
add_stats(&shared_stats, &abandoned_stats);
struct mallinfo2 res = {
.hblks = shared_stats.mmapped_regions,
.hblkhd = shared_stats.total_mmapped_memory,
.uordblks = shared_stats.total_allocated_memory,
.fordblks = total_heap_space - shared_stats.total_allocated_heap_space
.fordblks = __get_total_heap_space() - shared_stats.total_allocated_heap_space
};
malloc_enable();
return res;
......@@ -267,4 +264,4 @@ struct mallinfo mallinfo(void)
.uordblks = (int) mallinfo2_res.uordblks,
.fordblks = (int) mallinfo2_res.fordblks,
};
}
\ No newline at end of file
}
......@@ -11,11 +11,6 @@
#include <stddef.h>
#include <stdarg.h>
#ifdef MUSL_ITERATE_AND_STATS_API
extern pthread_key_t occupied_bin_key;
extern occupied_bin_t detached_occupied_bin;
#endif
void log_print(const char* info,...)
{
va_list ap;
......@@ -164,7 +159,7 @@ _Noreturn void __pthread_exit(void *result)
#ifdef MUSL_ITERATE_AND_STATS_API
occupied_bin_t *self_tsd = __get_occupied_bin(self);
__merge_bin_chunks(&detached_occupied_bin, self_tsd);
__merge_bin_chunks(__get_detached_occupied_bin(), self_tsd);
#endif
__pthread_tsd_run_dtors();
......@@ -416,7 +411,7 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
__init_occupied_bin_key_once();
occupied_bin_t *occupied_bin = internal_calloc(sizeof(occupied_bin_t), 1);
if (occupied_bin == NULL) goto fail;
new->tsd[occupied_bin_key] = occupied_bin;
new->tsd[__get_occupied_bin_key()] = occupied_bin;
new->tsd_used = 1;
#endif
......@@ -492,7 +487,7 @@ weak_alias(__pthread_create, pthread_create);
struct pthread* __pthread_list_find(pthread_t thread_id, const char* info)
{
struct pthread *thread = (struct pthread *)thread_id;
struct pthread *thread = (struct pthread *)thread_id;
if (NULL == thread) {
log_print("invalid pthread_t (0) passed to %s\n", info);
return NULL;
......@@ -508,7 +503,7 @@ struct pthread* __pthread_list_find(pthread_t thread_id, const char* info)
if (t == thread) return thread;
t = t->next ;
}
log_print("invalid pthread_t %p passed to %s\n", thread, info);
log_print("invalid pthread_t %p passed to %s\n", thread, info);
return NULL;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册