Nginx Valgrind 内存检测问题

618 views
Skip to first unread message

syz

unread,
Sep 22, 2015, 3:51:14 AM9/22/15
to OpenResty Mail list
使用Valgrind对nginx进行内存泄露检测 怎么会报Lua vm的错误呢, 使用的是luajit-2.0版本
各位大侠帮忙看看哇

以下是执行输出
% uname -a                                                                                                                                    1 ↵
Linux Node-02 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

% sudo valgrind --tool=memcheck --leak-check=full /usr/local/nginx-1.4.2/sbin/nginx
==41400== Memcheck, a memory error detector
==41400== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==41400== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==41400== Command: /usr/local/nginx-1.4.2/sbin/nginx
==41400==
==41400== Warning: set address range perms: large range [0x957a040, 0x2e45f7e2) (undefined)
==41400== Warning: set address range perms: large range [0x957b040, 0x2e45f040) (defined)
nginx: [error] failed to initialize Lua VM in /usr/local/nginx-1.4.2/conf/nginx.conf:125
==41400==
==41400== HEAP SUMMARY:
==41400==     in use at exit: 619,703,912 bytes in 3,285 blocks
==41400==   total heap usage: 20,476 allocs, 17,191 frees, 626,783,730 bytes allocated
==41400==
==41400== 128 bytes in 1 blocks are possibly lost in loss record 583 of 650
==41400==    at 0x4C2ABA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==41400==    by 0x431D0A: ngx_alloc (ngx_alloc.c:22)
==41400==    by 0x4222CE: ngx_crc32_table_init (ngx_crc32.c:117)
==41400==    by 0x41A20B: main (nginx.c:320)
==41400==
==41400== 1,024 bytes in 1 blocks are definitely lost in loss record 626 of 650
==41400==    at 0x4C2D136: memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==41400==    by 0x4C2D251: posix_memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==41400==    by 0x431DBB: ngx_memalign (ngx_alloc.c:57)
==41400==    by 0x41B5C2: ngx_create_pool (ngx_palloc.c:21)
==41400==    by 0x419F52: main (nginx.c:299)
==41400==
==41400== LEAK SUMMARY:
==41400==    definitely lost: 1,024 bytes in 1 blocks
==41400==    indirectly lost: 0 bytes in 0 blocks
==41400==      possibly lost: 128 bytes in 1 blocks
==41400==    still reachable: 619,702,760 bytes in 3,283 blocks
==41400==         suppressed: 0 bytes in 0 blocks
==41400== Reachable blocks (those to which a pointer was found) are not shown.
==41400== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==41400==
==41400== For counts of detected and suppressed errors, rerun with: -v
==41400== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

Yichun Zhang (agentzh)

unread,
Sep 22, 2015, 4:33:19 AM9/22/15
to openresty
Hello!

On Tue, Sep 22, 2015 at 3:50 PM, syz wrote:
> 使用Valgrind对nginx进行内存泄露检测 怎么会报Lua vm的错误呢, 使用的是luajit-2.0版本
> 各位大侠帮忙看看哇
>

这是因为 LuaJIT 自己的内存分配器在 Linux x86_64 上面会使用 MAP_32BIT 这个标志来调用 mmap()
系统调用。而 Valgrind 从 3.9.0 开始不再支持 mmap 的 MAP_32BIT 这个标志位,所以才会让 LuaJIT
初始化失败。

解决的办法是,重新编译一个特殊版本的 LuaJIT,强制它使用系统的分配器,即在编译 LuaJIT 时使用类似下面的命令:

make CCDEBUG=-g Q= XCFLAGS='-DLUAJIT_USE_VALGRIND -DLUAJIT_USE_SYSMALLOC'

这里最重要的是 LUAJIT_USE_SYSMALLOC 这个宏。当然,为了最好的效果,还应同时指定下面的 C 编译器选项:

-DLUA_USE_APICHECK -DLUA_USE_ASSERT

这些选项可以帮助发现更多的问题。当然,这些选项会引入额外的开销,而且 LUAJIT_USE_SYSMALLOC 在非 Valgrind 环境也会出问题。

我在开发过程中一般会准备两种 LuaJIT,一是带这些选项的,一是不带这些选项的。

值得一提的是,为了最好的效果,建议使用 openresty 的 luajit 仓库的 v2.1-agentzh 分支:

https://github.com/openresty/luajit2/tree/v2.1-agentzh

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages