了解“损坏的大小与prev_size”glibc错误

时间:2018-04-03 11:11:19

标签: c++ malloc free jna glibc

我已经为FDK-AAC实现了一个JNA桥。源代码可以在here

中找到

在对代码进行基准测试时,我可以在同一输入上获得数百次成功运行,然后偶尔会发生C级崩溃,导致整个进程被终止,从而导致生成核心转储:

查看核心转储,看起来像这样:

#1  0x00007f3e92e00f5d in __GI_abort () at abort.c:90
#2  0x00007f3e92e4928d in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7f3e92f70528 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:181
#3  0x00007f3e92e5064a in malloc_printerr (action=<optimized out>, str=0x7f3e92f6cdee "corrupted size vs. prev_size", ptr=<optimized out>, ar_ptr=<optimized out>) at malloc.c:5426
#4  0x00007f3e92e5304a in _int_free (av=0x7f3de0000020, p=<optimized out>, have_lock=0) at malloc.c:4337
#5  0x00007f3e92e5744e in __GI___libc_free (mem=<optimized out>) at malloc.c:3145
#6  0x00007f3e113921e9 in FDKfree (ptr=0x7f3de009df60) at libSYS/src/genericStds.cpp:233
#7  0x00007f3e1130d7d3 in Free_AacEncoder (p=0x7f3de0115740) at libAACenc/src/aacenc_lib.cpp:407
#8  0x00007f3e1130fbb3 in aacEncClose (phAacEncoder=0x7f3de0115740) at libAACenc/src/aacenc_lib.cpp:1395

如果我运行足够多次重复基准测试,这个返回/堆栈跟踪错误是可重现的,尽管我很难理解这样的错误可能是什么原因?分配给指针0x7f3de009df60的内存也分配在CPP / C代码中,我可以保证释放已分配的相同实例。当然,基准是单线程。

阅读完这些内容后:

security checks&amp;&amp; internal functions

我仍然很难理解 - 什么可能是真实的(非剥削,而非错误))场景导致我得到上述错误?为什么它几乎不会发生?

目前的怀疑

运行详细的回溯,我得到了这个输入:

#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
        set = {__val = {4, 6378670679680, 645636045657660056, 90523359816, 139904561311072, 292199584, 139903730612120, 139903730611784, 139904561311088, 1460617926600, 47573685816, 4119199860131166208, 
            139904593745464, 139904553224483, 139904561311136, 288245657}}
        pid = <optimized out>
        tid = <optimized out>
#1  0x00007f3e92e00f5d in __GI_abort () at abort.c:90
        save_stage = 2
        act = {__sigaction_handler = {sa_handler = 0x7f3de026db10, sa_sigaction = 0x7f3de026db10}, sa_mask = {__val = {139903730540556, 19, 30064771092, 812522497172832284, 139903728706672, 1887866374039011357, 
              139900298780168, 3775732748407067896, 763430436865, 35180077121538, 4119199860131166208, 139904561311552, 139904553065676, 1, 139904561311584, 139904561312192}}, sa_flags = 4096, 
          sa_restorer = 0x14}
        sigs = {__val = {32, 0 <repeats 15 times>}}
#2  0x00007f3e92e4928d in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7f3e92f70528 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:181
        ap = {{gp_offset = 40, fp_offset = 32574, overflow_arg_area = 0x7f3e11adf1d0, reg_save_area = 0x7f3e11adf160}}
        fd = <optimized out>
        list = <optimized out>
        nlist = <optimized out>
        cp = <optimized out>
        written = <optimized out>
#3  0x00007f3e92e5064a in malloc_printerr (action=<optimized out>, str=0x7f3e92f6cdee "corrupted size vs. prev_size", ptr=<optimized out>, ar_ptr=<optimized out>) at malloc.c:5426
        buf = "00007f3de009e9f0"
        cp = <optimized out>
        ar_ptr = <optimized out>
        ptr = <optimized out>
        str = 0x7f3e92f6cdee "corrupted size vs. prev_size"
        action = <optimized out>
#4  0x00007f3e92e5304a in _int_free (av=0x7f3de0000020, p=<optimized out>, have_lock=0) at malloc.c:4337
        size = 2720
        fb = <optimized out>
        nextchunk = 0x7f3de009e9f0
        nextsize = 736
        nextinuse = <optimized out>
        prevsize = <optimized out>
        bck = <optimized out>
        fwd = <optimized out>
        errstr = 0x0
        locked = <optimized out>
#5  0x00007f3e92e5744e in __GI___libc_free (mem=<optimized out>) at malloc.c:3145
        ar_ptr = <optimized out>
        p = <optimized out>
        hook = <optimized out>
#6  0x00007f3e113921e9 in FDKfree (ptr=0x7f3de009df60) at libSYS/src/genericStds.cpp:233
No locals.
#7  0x00007f3e1130d7d3 in Free_AacEncoder (p=0x7f3de0115740) at libAACenc/src/aacenc_lib.cpp:407
No locals.
#8  0x00007f3e1130fbb3 in aacEncClose (phAacEncoder=0x7f3de0115740) at libAACenc/src/aacenc_lib.cpp:1395
        hAacEncoder = 0x7f3de009df60
        err = AACENC_OK
  • #6 框架中,您可以看到问题中的指针为0x7f3de009df60
  • #4 框架中,您可以看到大小为2720,这确实是要发布的结构的预期大小。
  • nextchunk的地址为0x7f3de009e9f0,在当前指针释放后仅为2704个字节。
  • 我可以确认错误再现时总是如此。
  • 这可能是我所面临的错误的强烈迹象吗?

1 个答案:

答案 0 :(得分:14)

好的,所以我设法克服了这个问题。

首先 - “大小与prev_size损坏”的实际原因非常简单 - 由于代码的越界访问,相邻后续块中的内存块控制结构字段被覆盖。如果你为指针x分配p个字节但是关于同一个指针的写入超过x,你可能会得到这个错误,表明当前的内存分配(块)大小不是与下一个块控制结构中的内容相同(由于它被覆盖)。

至于内存泄漏的原因 - 在Java / JNA层中完成的结构映射暗示了与编译的dll / so相关的不同#pragma相关填充/对齐。这反过来又导致数据被写入超出分配的结构边界。禁用该对齐使问题消失。 (成千上万的处决没有一次崩溃!)。