【发布时间】:2012-11-29 19:55:40
【问题描述】:
编辑:找到解决方案(见底部)
我正在尝试在 Mac OS X 10.8 x86_64 系统上构建软件包 (owfs)。这个软件包主要是在/for linux上开发的,但是我已经得到了一个旧版本来编译和正常运行,并且互联网上有很多人在运行它的各种版本,所以我希望它是可能的.我已经能够成功完成编译过程,但是当我尝试运行它时程序会出现段错误。我在 GDB 中进行了调查,结果如下:
有问题的代码:
void FS_dir_entry_aliased(void (*dirfunc) (void *, const struct parsedname *), void *v, const struct parsedname *pn)
{
if ( ( pn->state & ePS_unaliased ) == 0 ) {
// Want alias substituted
struct parsedname s_pn_copy ;
struct parsedname * pn_copy = & s_pn_copy ;
ASCII path[PATH_MAX+3] ;
ASCII * path_pointer = path ; // current location in original path
// Shallow copy
memcpy( pn_copy, pn, sizeof(struct parsedname) ) ;
pn_copy->path[0] = '\0' ;
// path copy to use for separation
strcpy( path, pn->path ) ;
// copy segments of path (delimitted by "/") to copy
while( path_pointer != NULL ) {
ASCII * path_segment = NULL;
path_segment = strsep( &path_pointer, "/" ) ;
BYTE sn[SERIAL_NUMBER_SIZE] ;
if ( PATH_MAX < strlen(pn_copy->path) + strlen(path_segment) ) {
在if 块中对strlen(path_segment) 的调用失败,因为path_segment 是一个越界地址。
逐步了解 GDB,这就是我的发现。进入用strsep初始化path_segment的调用,gdb给了我:
path_pointer = (ASCII *) 0x7fff5fbf99a5 "/uncached/bus.0/interface"
在我执行下一行之后,path_pointer 已经前进了,正如我所料:
path_pointer = (ASCII *) 0x7fff5fbf99a6 "uncached/bus.0/interface"
但 gdb 报告:
path_segment = (ASCII *) 0x5fbf99a5 <Address 0x5fbf99a5 out of bounds>
这是地址的正确“开始”,但它是一个 32 位指针地址。由于我的系统是 64 位系统,所以当 strlen 被调用时,我得到一个 EXC_BAD_ACCESS。
我知道这里发生了很多事情,如果问题是深埋在包构建过程中的问题,我没有提供足够的信息。在我看来,这可能有一个简单的解决方案,因为该函数基本上可以正常工作,但只是返回了错误的大小指针。不过,我对 64 位与 32 位系统的复杂性几乎没有什么经验,所以我想知道是否有人能发现一些明显的东西,或者为调试此问题的后续步骤提供说明。有趣的是,当我在 gdb (p (ASCII *) strsep (&path_pointer, "/")) 中手动运行 strsep 命令时,它们似乎工作正常,为我提供了符合我期望的 64 位指针。
最后,如果它有用,我相信是 strsep 调用的装配线:
0x0000000100036eee <FS_dir_entry_aliased+318>: callq 0x1000a56a0 <dyld_stub_strsep>
0x0000000100036ef3 <FS_dir_entry_aliased+323>: mov %eax,%ecx
0x0000000100036ef5 <FS_dir_entry_aliased+325>: movslq %ecx,%rcx
0x0000000100036ef8 <FS_dir_entry_aliased+328>: mov %rcx,-0x1d10(%rbp)
以及该 callq 地址处的 strsep 的反汇编:
0x00007fff915c0fc7 <strsep+0>: push %rbp
0x00007fff915c0fc8 <strsep+1>: mov %rsp,%rbp
0x00007fff915c0fcb <strsep+4>: mov (%rdi),%r8
0x00007fff915c0fce <strsep+7>: xor %eax,%eax
0x00007fff915c0fd0 <strsep+9>: test %r8,%r8
0x00007fff915c0fd3 <strsep+12>: je 0x7fff915c1007 <strsep+64>
0x00007fff915c0fd5 <strsep+14>: mov %r8,%r10
0x00007fff915c0fd8 <strsep+17>: jmp 0x7fff915c0fe4 <strsep+29>
0x00007fff915c0fda <strsep+19>: inc %rdx
0x00007fff915c0fdd <strsep+22>: test %al,%al
0x00007fff915c0fdf <strsep+24>: jne 0x7fff915c0fee <strsep+39>
0x00007fff915c0fe1 <strsep+26>: mov %r9,%r10
0x00007fff915c0fe4 <strsep+29>: mov (%r10),%cl
0x00007fff915c0fe7 <strsep+32>: lea 0x1(%r10),%r9
0x00007fff915c0feb <strsep+36>: mov %rsi,%rdx
0x00007fff915c0fee <strsep+39>: mov (%rdx),%al
0x00007fff915c0ff0 <strsep+41>: cmp %cl,%al
0x00007fff915c0ff2 <strsep+43>: jne 0x7fff915c0fda <strsep+19>
0x00007fff915c0ff4 <strsep+45>: xor %edx,%edx
0x00007fff915c0ff6 <strsep+47>: test %cl,%cl
0x00007fff915c0ff8 <strsep+49>: je 0x7fff915c1001 <strsep+58>
0x00007fff915c0ffa <strsep+51>: movb $0x0,(%r10)
0x00007fff915c0ffe <strsep+55>: mov %r9,%rdx
0x00007fff915c1001 <strsep+58>: mov %rdx,(%rdi)
0x00007fff915c1004 <strsep+61>: mov %r8,%rax
0x00007fff915c1007 <strsep+64>: pop %rbp
0x00007fff915c1008 <strsep+65>: retq
编辑:
string.h 肯定被包含在内,有一个系统范围的包含文件包含它,但只是为了确保我尝试将它添加到这个特定文件。 -D_BSD_SOURCE=1 是自动添加的编译器选项之一,因此正在发生。默认情况下还有一个编译器选项-D_ISOC99_SOURCE=1。我尝试添加-std=gnu99(我的编译器llvm-gcc 4.2,不喜欢-std=gnu90),但它没有修复它。我收到以下编译器错误:
以下是我针对此文件收到的编译器警告:
ow_alias.c: In function 'ReadAliasFile':
ow_alias.c:40: warning: implicit declaration of function 'getline'
ow_alias.c:48: warning: implicit declaration of function 'strsep'
ow_alias.c:48: warning: assignment makes pointer from integer without a cast
ow_alias.c:64: warning: assignment makes pointer from integer without a cast
ow_alias.c: In function 'FS_dir_entry_aliased':
ow_alias.c:177: warning: initialization makes pointer from integer without a cast
第二次编辑:
在进行了更多挖掘之后(感谢gcc -E | grep strsep 提示!),我发现问题出在编译器标志-D_POSIX_C_SOURCE=200112L 上。此编译器标志阻止了 strsep 的定义,使编译器进行了隐式声明。我从配置脚本中删除了它,一切似乎都运行良好。感谢您的帮助!
【问题讨论】:
-
有问题的编译单元是
#include <string.h>吗?如果是这样,cc -E | grep strsep显示strsep要声明为什么? -
使用
gcc -I./module/owlib/src/include -I/opt/local/include -I./src/include -E module/owlib/src/c/ow_alias.c | grep strsep,我将strsep 声明为char *strsep(char **, const char *);,这似乎是正确的。但是在完整的制作过程中,我在上面的编辑中收到了编译器警告......
标签: c macos assembly 32bit-64bit x86-64