Memory 使用Linux RHL 6.2进行共享库调用时堆损坏

Memory 使用Linux RHL 6.2进行共享库调用时堆损坏,memory,corruption,Memory,Corruption,我已经使用Sun编译器将代码从Solaris/Sparc/x86移植到使用GCC4.4.6的Red Hat Linux 6.2,并且我在valgrind和core转储下报告内存损坏的代码方面遇到了问题 相同的源代码在Solaris(Sparc和x86)上运行时没有错误 发生错误的区域位于我调用以检索系统配置的共享库中 它抱怨的内存是共享库中的本地堆栈变量。Valgrind并没有帮助我找到故障发生的实际区域 带有“-d check-b-p1”选项的DBGMEM内存调试器未检测到我在报告中看到的任何

我已经使用Sun编译器将代码从Solaris/Sparc/x86移植到使用GCC4.4.6的Red Hat Linux 6.2,并且我在valgrind和core转储下报告内存损坏的代码方面遇到了问题

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

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

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

带有“-d check-b-p1”选项的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;
代码是使用以下开关选项编译的: 共享库: -D_LINUX_SOURCE-DLINUX-D_REENTRANT-DDEBUG_ENABLED-Wall-Wwrite strings-DDEBUG-DTHREAD_SAFE-g-fstack check-Wmissing prototype-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 prototype-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)

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

==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
错误报告中调用链中的每个函数在堆栈上使用的预期内存进行比较。

我尝试将堆栈大小增加4倍以进行快速检查,结果没有差异。==29969==如果您认为这是由于程序主线程中的堆栈==29969==溢出造成的(不太可能,但==29969==可能),您可以尝试使用--main stacksize=标志增加==29969==主线程堆栈的大小。==29969==此运行中使用的主线程堆栈大小为41943040。大缓冲区有多大?崩溃时您没有给我堆栈的预期大小。