pthread_create() 似乎泄漏内存

时间:2021-07-24 21:30:59

标签: c++ linux pthreads

我在 wrapper.socalloc() 上编写了一个简单的 free() 来监控内存调用,结果显示 pthreads_create() 正在泄漏内存。

在使用 calloc(17, 16) 进行初始分配后(大部分时间为 calloc(18, 16)),似乎正在尝试释放内存,但是 nullptr 被传递给 { {1}}。

这里发生了什么?

free()
// test.cpp

#include <pthread.h>
#include <cassert>
#include <cstdlib>

void* p_dummy(void*)
{
    return nullptr;
}

int main(void)
{
    void* ptr = calloc(11, 11);
    free(ptr);

    pthread_t thread;
    assert(pthread_create(&thread, nullptr, p_dummy, nullptr) == 0); 
    assert(pthread_join(thread, nullptr) == 0);

    return 0;
}
// wrapper.cpp

#ifndef _GNU_SOURCE
    #define _GNU_SOURCE
#endif

#include <dlfcn.h>
#include <cstdio>

static void  (*real_free)(void* ptr)                   = NULL;
static void* (*real_calloc)(size_t nmemb, size_t size) = NULL;

static bool initializing = false;

static void init()
{
    initializing = true;

    fprintf(stderr,"init()\n");

    real_free = (void (*)(void*))dlsym(RTLD_NEXT, "free");
    real_calloc = (void* (*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc");

    if (!real_free or !real_calloc) {
        fprintf(stderr,"Error in `dlsym()`: %s\n", dlerror());
    }

    initializing = false;
}

extern "C" void free(void *ptr)
{
    fprintf(stderr,"free(%p)\n", ptr);

    if (!real_free) {
        init();
    }

    real_free(ptr);
}

extern "C" void* calloc(size_t nmemb, size_t size)
{
    static char memory[32] { 0 }; // Hack to provide memory to dlsym()

    if (initializing) {
        fprintf(stderr,"calloc(%lu, %lu): %p\n", nmemb, size, &memory);
        return memory;
    }

    if (!real_calloc) {
        init();
    }

    void* ptr = real_calloc(nmemb, size);

    fprintf(stderr,"calloc(%lu, %lu): %p\n", nmemb, size, ptr);

    return ptr;
}

输出:

# Makefile

CC = g++
CFLAGS = -std=c++17 -Wall

all: test

test: test.cpp wrapper.so 
    $(CC) $(CFLAGS) -pthread -o test test.cpp -ldl

wrapper.o: wrapper.cpp
    $(CC) $(CFLAGS) -c -fPIC -o wrapper.o wrapper.cpp

wrapper.so: wrapper.o
    $(CC) $(CFLAGS) -shared -o wrapper.so wrapper.o -ldl

clean:
    rm -f *.o *.so test

2 个答案:

答案 0 :(得分:4)

有问题的内存区域是 DTV(动态线程向量),它在程序终止之前不能被释放。

如果您在 calloc 休息,您可以在 GDB 中看到它:

(gdb) bt
#0  __libc_calloc (n=17, elem_size=16) at malloc.c:3366
#1  0x00007ffff7fc52fa in calloc (nmemb=17, size=16) at wrapper.cpp:56
#2  0x00007ffff7fe39cb in allocate_dtv (result=0x7ffff7d86700) at ../elf/dl-tls.c:286
#3  __GI__dl_allocate_tls (mem=mem@entry=0x7ffff7d86700) at ../elf/dl-tls.c:532
#4  0x00007ffff7f8b323 in allocate_stack (stack=<synthetic pointer>, pdp=<synthetic pointer>, attr=0x7fffffffe300) at allocatestack.c:622
#5  __pthread_create_2_1 (newthread=<optimized out>, attr=<optimized out>, start_routine=<optimized out>, arg=<optimized out>) at pthread_create.c:660
#6  0x0000555555555273 in main () at test.cpp:19

这是设计使然(更多详细信息here,如果您真的很好奇)。

报告了许多关于 allocate_dtv 中的“泄漏”的错误,所有这些错误都被拒绝(例如 12)。

free(nullptr) 的调用与此无关,它是从线程清理函数 (__res_thread_freeres) 调用的。

答案 1 :(得分:3)

我补充说:

extern "C" void free(void *ptr)
{
    if (ptr == NULL) {
        raise(SIGTRAP);
    }

     ....

然后启动调试器:

gdb --args env LD_PRELOAD=./wrapper.so ./test

run 它和 bt 显示:

free((nil))

Thread 2 "test" received signal SIGTRAP, Trace/breakpoint trap.
[Switching to Thread 0x7ffff7a25640 (LWP 418295)]
0x00007ffff7c0a702 in raise () from /usr/lib/libpthread.so.0
(gdb) bt
#0  0x00007ffff7c0a702 in raise () from /usr/lib/libpthread.so.0
#1  0x00007ffff7fc1252 in free (ptr=0x0) at wrapper.cpp:39
#2  0x00007ffff7ab8027 in __libc_thread_freeres () from /usr/lib/libc.so.6
#3  0x00007ffff7c0027f in start_thread () from /usr/lib/libpthread.so.0
#4  0x00007ffff7b275e3 in clone () from /usr/lib/libc.so.6
(gdb) 

一个简单的谷歌搜索导致 __libc_thread_freeres 调用 __strerror_thread_freeres 调用 free(last_value)last_value 用作 strerror_l 分配的内存的线程本地值。它是 NULL,从未调用过 strerror