-1

我已经使用 Sun 编译器将代码从 Solaris/Sparc/x86 移植到使用 GCC 4.4.6 的 Red Hat Linux 6.2,并且在 valgrind 和核心转储下报告内存损坏的代码存在问题。

相同的源代码在 Solaris(Sparc 和 x86)上运行时没有错误。

发生错误的区域在我调用以检索系统配置的共享库中。

它抱怨的内存是共享库中的本地堆栈变量。Valgrind 没有帮助我找到发生故障的实际区域。

带有“-d check -b -p 1”选项的 DBGMEM 内存调试器没有检测到我可以在报告中看到的任何内存,程序正常退出。

带有“-v --leak-check=yes --read-var-info=yes”选项的 valgrind 内存调试器失败,报告无效写入。

来自 valgrind 日志:

==22043== Invalid write of size 8
==22043==    at 0x51DF74D: setLockingMode (db_support_funcs.c:258)
==22043==    by 0x40BF4E: GetAmaHdrParameters (AmaFileWriter.c:3707)
==22043==    by 0x40490E: MakeAMADNSFile (AmaFileWriter.c:899)
==22043==    by 0x403109: main (MakeAmaFile.c:775)
==22043==  Address 0x7fefebea8 is not stack'd, malloc'd or (recently) free'd
==22043== 
==22043== Invalid write of size 8
==22043==    at 0x51DF348: logconfig (db_support_funcs.c:228)
==22043==    by 0x51DF790: setLockingMode (db_support_funcs.c:257)
==22043==    by 0x40BF4E: GetAmaHdrParameters (AmaFileWriter.c:3707)
==22043==    by 0x40490E: MakeAMADNSFile (AmaFileWriter.c:899)
==22043==    by 0x403109: main (MakeAmaFile.c:775)
==22043==  Address 0x7fefebd68 is not stack'd, malloc'd or (recently) free'd
==22043== 
==22043== Invalid write of size 8
==22043==    at 0x51DF36A: logconfig (db_support_funcs.c:176)
==22043==    by 0x51DF790: setLockingMode (db_support_funcs.c:257)
==22043==    by 0x40BF4E: GetAmaHdrParameters (AmaFileWriter.c:3707)
==22043==    by 0x40490E: MakeAMADNSFile (AmaFileWriter.c:899)
==22043==    by 0x403109: main (MakeAmaFile.c:775)
==22043==  Address 0x7fefebcc8 is not stack'd, malloc'd or (recently) free'd
==22043== 
==22043== Invalid write of size 8
==22043==    at 0x51DF396: logconfig (db_support_funcs.c:177)
==22043==    by 0x51DF790: setLockingMode (db_support_funcs.c:257)
==22043==    by 0x40BF4E: GetAmaHdrParameters (AmaFileWriter.c:3707)
==22043==    by 0x40490E: MakeAMADNSFile (AmaFileWriter.c:899)
==22043==    by 0x403109: main (MakeAmaFile.c:775)
==22043==  Address 0x7fefeacb8 is not stack'd, malloc'd or (recently) free'd
==22043== 
==22043== 
==22043== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==22043==  Access not within mapped region at address 0x7FEFEACB8
==22043==    at 0x51DF396: logconfig (db_support_funcs.c:177)
==22043==    by 0x51DF790: setLockingMode (db_support_funcs.c:257)
==22043==    by 0x40BF4E: GetAmaHdrParameters (AmaFileWriter.c:3707)
==22043==    by 0x40490E: MakeAMADNSFile (AmaFileWriter.c:899)
==22043==    by 0x403109: main (MakeAmaFile.c:775)
==22043==  If you believe this happened as a result of a stack
==22043==  overflow in your program's main thread (unlikely but
==22043==  possible), you can try to increase the size of the
==22043==  main thread stack using the --main-stacksize= flag.
==22043==  The main thread stack size used in this run was 10485760.

db_support_funcs.c 中调用源的函数是:

-- line 255 -- void setLockingMode( short locking )
-- line 256 -- {
-- line 257 --    logconfig(0, LOG_DEBUG1, "DEBUG1:[%s:%d]:setLockingMode(locking=%d), currently %d", __FILE__,__LINE__, locking, disable_lock);
-- line 258 --    disable_lock = locking;
-- line 259 -- }

-- line 168 -- void logconfig( int errnoflag, int level, const char *fmt, ... )
-- line 169 -- {
-- line 170 --    va_list     ap;
-- line 171 --    int         errno_save = errno; /* Value caller might want printed */
-- line 172 --    long        n;
-- line 173 --    time_t      curr_time;
-- line 174 --    struct tm   *curr_tm,
-- line 175 --                *stat_tm;
-- line 176 --    struct stat stat_buff;
-- line 177 --    char        buff[BIG_BUFFER_LEN];
-- line 178 --    static char pid_str[MAX_PATH_LEN];
-- line 179 --    static int  first_time = 1;

代码使用以下开关选项编译: Shared Lib: -D_LINUX_SOURCE -DLINUX -D_REENTRANT -DDEBUG_ENABLED -Wall -Wwrite-strings -DDEBUG -DTHREAD_SAFE -g -fstack-check -Wmissing-prototypes -Wpointer-arith -Wcast-align - DNOPROTX -pthread -D__LITTLE_ENDIAN=1234 -D_LITTLEENDIAN -DL_ENDIAN -fPIC -DTHREAD_SAFE -shared -fPIC

应用程序:-D_LINUX_SOURCE -DLINUX -D_REENTRANT -DDEBUG_ENABLED -Wall -Wwrite-strings -DDEBUG -g -fstack-check -Wmissing-prototypes -Wpointer-arith -Wcast-align -DNOPROTX -pthread -g -D__LITTLE_ENDIAN=1234 -D_LITTLEENDIAN -DL_ENDIAN -fPIC

我正在使用 gcc 版本 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)

4

1 回答 1

0

由于错误主要是针对 log 函数的局部变量报告的,因此我推断您的程序超出了线程的堆栈。报告的底部暗示了这一点:

==22043==  If you believe this happened as a result of a stack
==22043==  overflow in your program's main thread (unlikely but
==22043==  possible), you can try to increase the size of the
==22043==  main thread stack using the --main-stacksize= flag.
==22043==  The main thread stack size used in this run was 10485760.

valgrind主线程堆栈使用 10MB。valgrind将此堆栈大小与错误报告中调用链中的每个函数在堆栈上使用的预期内存进行比较。

于 2013-07-26T19:36:04.867 回答