“非法指令:4”出现在OS X Lion系统中。

时间:2022-04-30 08:44:49

Some C++ application compiled and run seamlessly in OS X Snow Leopard, but I changed recently to OS X Lion, and here, although there is no compilation error, when I try to run it I get the error "Illegal instruction: 4", I have no clue, what could be the reason?

一些c++应用程序在OS X Snow Leopard中进行了无缝的编译和运行,但是我最近修改了OS X Lion,在这里,虽然没有编译错误,但是当我尝试运行它时,我得到了错误的“非法指令:4”,我不知道,原因是什么?

PS:

PS:

These are the linking flags I use

这些是我使用的链接标志。

-Wl,-stack_size,0x10000000,-stack_addr,0xc0000000 

This is the output I get when I do sudo truss executable

这是我做sudo truss可执行文件时得到的输出。

setrlimit returned result = -1
    SYSCALL(args)        = return
getpid(0x0, 0x0, 0x0)        = 32993 0
__sysctl(0xBFFFF5EC, 0x3, 0xBFFFF5E8)        = 0 0
issetugid(0xBFFFF5EC, 0x3, 0xBFFFF5E8)       = 0 0
csops(0x0, 0x0, 0xBFFFF65C)      = 0 0
shared_region_check_np(0xBFFFD5E0, 0x0, 0xBFFFF65C)      = 0 0
stat64("/usr/lib/dtrace/libdtrace_dyld.dylib\0", 0xBFFFE830, 0xBFFFF65C)         = 0 0
open("/usr/lib/dtrace/libdtrace_dyld.dylib\0", 0x0, 0x0)         = 3 0
pread(0x3, "\312\376\272\276\0", 0x1000, 0x0)        = 4096 0
pread(0x3, "\316\372\355\376\a\0", 0x1000, 0x6000)       = 4096 0
mmap(0x4D3000, 0x2000, 0x1, 0x12, 0x3, 0x3)      = 0x4D3000 0
mmap(0x4D5000, 0x1000, 0x3, 0x12, 0x3, 0x3)      = 0x4D5000 0
mmap(0x4D6000, 0x1EF0, 0x1, 0x12, 0x3, 0x3)      = 0x4D6000 0
close(0x3)       = 0 0
stat64("/usr/lib/libstdc++.6.dylib\0", 0xBFFFE690, 0x1)      = 0 0
stat64("/usr/lib/libgcc_s.1.dylib\0", 0xBFFFE690, 0x1)       = 0 0
stat64("/usr/lib/libSystem.B.dylib\0", 0xBFFFE560, 0x1)      = 0 0
stat64("/usr/lib/libc++abi.dylib\0", 0xBFFFE5D0, 0x1)        = 0 0
stat64("/usr/lib/system/libcache.dylib\0", 0xBFFFE360, 0x1)      = 0 0
stat64("/usr/lib/system/libcommonCrypto.dylib\0", 0xBFFFE360, 0x1)       = 0 0
stat64("/usr/lib/system/libcompiler_rt.dylib\0", 0xBFFFE360, 0x1)        = 0 0
stat64("/usr/lib/system/libcopyfile.dylib\0", 0xBFFFE360, 0x1)       = 0 0
stat64("/usr/lib/system/libdispatch.dylib\0", 0xBFFFE360, 0x1)       = 0 0
stat64("/usr/lib/system/libdnsinfo.dylib\0", 0xBFFFE360, 0x1)        = 0 0
stat64("/usr/lib/system/libdyld.dylib\0", 0xBFFFE360, 0x1)       = 0 0
stat64("/usr/lib/system/libkeymgr.dylib\0", 0xBFFFE360, 0x1)         = 0 0
stat64("/usr/lib/system/liblaunch.dylib\0", 0xBFFFE360, 0x1)         = 0 0
stat64("/usr/lib/system/libmacho.dylib\0", 0xBFFFE360, 0x1)      = 0 0
stat64("/usr/lib/system/libmathCommon.A.dylib\0", 0xBFFFE360, 0x1)       = 0 0
stat64("/usr/lib/system/libquarantine.dylib\0", 0xBFFFE360, 0x1)         = 0 0
stat64("/usr/lib/system/libremovefile.dylib\0", 0xBFFFE360, 0x1)         = 0 0
stat64("/usr/lib/system/libsystem_blocks.dylib\0", 0xBFFFE360, 0x1)      = 0 0
stat64("/usr/lib/system/libsystem_c.dylib\0", 0xBFFFE360, 0x1)       = 0 0
stat64("/usr/lib/system/libsystem_dnssd.dylib\0", 0xBFFFE360, 0x1)       = 0 0
stat64("/usr/lib/system/libsystem_info.dylib\0", 0xBFFFE360, 0x1)        = 0 0
stat64("/usr/lib/system/libsystem_kernel.dylib\0", 0xBFFFE360, 0x1)      = 0 0
stat64("/usr/lib/system/libsystem_network.dylib\0", 0xBFFFE360, 0x1)         = 0 0
stat64("/usr/lib/system/libsystem_notify.dylib\0", 0xBFFFE360, 0x1)      = 0 0
stat64("/usr/lib/system/libsystem_sandbox.dylib\0", 0xBFFFE360, 0x1)         = 0 0
stat64("/usr/lib/system/libunc.dylib\0", 0xBFFFE360, 0x1)        = 0 0
stat64("/usr/lib/system/libunwind.dylib\0", 0xBFFFE360, 0x1)         = 0 0
stat64("/usr/lib/system/libxpc.dylib\0", 0xBFFFE360, 0x1)        = 0 0
open("/dev/dtracehelper\0", 0x2, 0xBFFFF5B0)         = 3 0
ioctl(0x3, 0x80086804, 0xBFFFF540)       = 0 0
close(0x3)       = 0 0
__sysctl(0xBFFFF1FC, 0x2, 0xBFFFF1F4)        = 0 0
bsdthread_register(0x92C9F6BC, 0x92C9F6E0, 0x1000)       = 0 0
thread_selfid(0x92C9F6BC, 0x92C9F6E0, 0x1000)        = 2500945 0
mmap(0x0, 0x2000, 0x3, 0x1002, 0x1000000, 0xAC308375)        = 0x4D8000 0
mprotect(0x4D8000, 0x44, 0x1)        = 0 0
mmap(0x0, 0xD000, 0x3, 0x1002, 0x1000000, 0x4D8034)      = 0x4DA000 0
mprotect(0x4DA000, 0x1000, 0x0)      = 0 0
mprotect(0x4E6000, 0x1000, 0x0)      = 0 0
mmap(0x0, 0xD000, 0x3, 0x1002, 0x1000000, 0x4DB000)      = 0x4E7000 0
mprotect(0x4E7000, 0x1000, 0x0)      = 0 0
mprotect(0x4F3000, 0x1000, 0x0)      = 0 0
mmap(0x0, 0x1000, 0x3, 0x1002, 0x1000000, 0x4E8000)      = 0x4F4000 0
mprotect(0x4F4000, 0x1000, 0x1)      = 0 0
mprotect(0x4D8000, 0x44, 0x3)        = 0 0
mmap(0x0, 0x200000, 0x3, 0x1002, 0x7000000, 0x4F4000)        = 0x4F5000 0
munmap(0x4F5000, 0xB000)         = 0 0
munmap(0x600000, 0xF5000)        = 0 0
mprotect(0x4D8000, 0x44, 0x1)        = 0 0
getpid(0x4D8000, 0x44, 0x1)      = 32993 0
__mac_syscall(0x973E8E8E, 0x2, 0xBFFFF0C8)       = 0 0
stat64("/AppleInternal\0", 0xBFFFF130, 0xBFFFF0C8)       = -1 Err#2
audit_session_self(0x92C1F4B6, 0xBFFFF130, 0xBFFFF0C8)       = 5635 0
geteuid(0x92C1F4B6, 0xBFFFF130, 0xBFFFF0C8)      = 0 0
getegid(0x92C1F4B6, 0xBFFFF130, 0xBFFFF0C8)      = 0 0
getaudit_addr(0xBFFFF0A8, 0x30, 0xBFFFF0C8)      = 0 0
csops(0x80E1, 0x7, 0xBFFFECF8)       = 0 0
mmap(0x0, 0x2000, 0x3, 0x1002, 0x1000000, 0xACA5EB00)        = 0x4F5000 0
mprotect(0x4F5000, 0x44, 0x1)        = 0 0
mmap(0x0, 0xD000, 0x3, 0x1002, 0x1000000, 0x4F5034)      = 0x600000 0
mprotect(0x600000, 0x1000, 0x0)      = 0 0
mprotect(0x60C000, 0x1000, 0x0)      = 0 0
mmap(0x0, 0xD000, 0x3, 0x1002, 0x1000000, 0x601000)      = 0x60D000 0
mprotect(0x60D000, 0x1000, 0x0)      = 0 0
mprotect(0x619000, 0x1000, 0x0)      = 0 0
mprotect(0x4F4000, 0x1000, 0x3)      = 0 0
mprotect(0x4F4000, 0x1000, 0x1)      = 0 0
mprotect(0x4F5000, 0x44, 0x3)        = 0 0
mmap(0x0, 0x200000, 0x3, 0x1002, 0x7000000, 0x4F4004)        = 0x61A000 0
munmap(0x61A000, 0xE6000)        = 0 0
munmap(0x800000, 0x1A000)        = 0 0
mprotect(0x4F5000, 0x44, 0x1)        = 0 0
getrlimit(0x1003, 0xBFFFF8DC, 0x1)       = 0 0
setrlimit(0x1003, 0xBFFFF8DC, 0x1)       = -1 Err#22
getrlimit(0x1008, 0xBF835C60, 0x1)       = 0 0
fstat64(0x1, 0xBF836090, 0x1F)       = 0 0
mmap(0x0, 0x1000000, 0x3, 0x1002, 0x2000000, 0xACA5B3E0)         = 0x800000 0
munmap(0x1000000, 0x800000)      = 0 0

PS2: If I remove the previously mentioned linking flag, the program runs, with no error. But when I input real data to the program, then I get

PS2:如果我删除前面提到的链接标志,程序运行,没有错误。但是当我把真实的数据输入到程序中时,我就得到了。

Segmentation fault: 11

I remember that this program had a problem with the stack, so it had to be increased. In Linux I did this and works

我记得这个程序在堆栈上有问题,所以它必须增加。在Linux中,我这样做了。

       const rlim_t kStackSize = 256L * 1024L * 1024L;   // min stack size = 64 Mb
    struct rlimit rl;
    int result;

    result = getrlimit(RLIMIT_STACK, &rl);
    if (result == 0)
    {
            if (rl.rlim_cur < kStackSize)
            {
                    rl.rlim_cur = kStackSize;
                    result = setrlimit(RLIMIT_STACK, &rl);
                    if (result != 0)
                    {
                            fprintf(stderr, "setrlimit returned result = %d\n", result);
                    }
            }
    }

but in OS X, since that did not work, I used the previously mentioned linking flag, and had no problem is OS X Snow Leopard, so it seems I still have the stack overflow problem in OS X Lion but the linking flag does not solve this. What could I do?

但是在OS X中,由于没有工作,我使用了之前提到的链接标志,并且没有问题是OS X Snow Leopard,所以看起来我在OS X Lion中仍然有堆栈溢出问题,但是链接标志并没有解决这个问题。我能做些什么呢?

3 个解决方案

#1


19  

I encountered this issue when building a product on Mountain Lion (10.8,) and then running on Lion. (10.7). The cause was that I made some changes to my build environment.

我在Mountain Lion(10.8)上构建产品时遇到了这个问题,然后在Lion上运行。(10.7)。原因是我对我的构建环境做了一些修改。

(I'm using mkbundle to ship a product that uses Mono.)

(我使用mkbundle来发送一个使用Mono的产品。)

The fix was very simple, I had to tell clang that generated binaries need to work on OSX 10.6. I added the following argument to clang:

修复非常简单,我必须告诉clang生成的二进制文件需要在osx10.6上工作。我在铿锵声中加入了以下论点:

-mmacosx-version-min=10.6

Problem solved!

问题解决了!

#2


8  

In OS X Lion (but also 10.5) the stack size hard limit is 65532 kbytes (just under 64 MiB). This can be seen with:

在OS X Lion(但也10.5)中,栈大小硬限制为65532 kbytes(略低于64 MiB)。可以看到:

bswift$ ulimit -Hs
65532

Even as root, I couldn't increase this value.

即使是根,我也不能增加这个值。

The soft limit defaults to only 8 MiB:

软限制默认为8 MiB:

bswift$ ulimit -Ss
8192

Try raising the value to this maximum before starting you application:

在开始应用程序之前,尝试将值提高到这个最大值:

bswift$ ulimit -Ss unlimited
bswift$ ulimit -Ss
65532

Note: segmentation fault (SIGSEGV) (number 11) you observed is the signal sent to the process when the stack limit is exceeded according to man setrlimit

注意:您所观察到的分割错误(SIGSEGV)(第11号)是当堆栈限制超出了man setrlimit时发送给进程的信号。

Note: Since the ulimit command needs to be a shell builtin, you will find it documented in man bash

注意:由于ulimit命令需要是一个shell builtin,您将在man bash中找到它的文档。

#3


0  

This could be a permissions issue.

这可能是一个权限问题。

To diagnose further, run your program from the terminal with sudo dtruss prefixed. See which syscall it runs before throwing the error.

要进一步诊断,请使用sudo dtruss在终端运行您的程序。在抛出错误之前,请查看它运行的是哪个syscall。

Example: sudo dtruss /path/to/application

例如:sudo dtruss /道路/ /应用程序

You can also diagnose it with the Xcode or GDB debuggers.

您还可以使用Xcode或GDB调试器来诊断它。

#1


19  

I encountered this issue when building a product on Mountain Lion (10.8,) and then running on Lion. (10.7). The cause was that I made some changes to my build environment.

我在Mountain Lion(10.8)上构建产品时遇到了这个问题,然后在Lion上运行。(10.7)。原因是我对我的构建环境做了一些修改。

(I'm using mkbundle to ship a product that uses Mono.)

(我使用mkbundle来发送一个使用Mono的产品。)

The fix was very simple, I had to tell clang that generated binaries need to work on OSX 10.6. I added the following argument to clang:

修复非常简单,我必须告诉clang生成的二进制文件需要在osx10.6上工作。我在铿锵声中加入了以下论点:

-mmacosx-version-min=10.6

Problem solved!

问题解决了!

#2


8  

In OS X Lion (but also 10.5) the stack size hard limit is 65532 kbytes (just under 64 MiB). This can be seen with:

在OS X Lion(但也10.5)中,栈大小硬限制为65532 kbytes(略低于64 MiB)。可以看到:

bswift$ ulimit -Hs
65532

Even as root, I couldn't increase this value.

即使是根,我也不能增加这个值。

The soft limit defaults to only 8 MiB:

软限制默认为8 MiB:

bswift$ ulimit -Ss
8192

Try raising the value to this maximum before starting you application:

在开始应用程序之前,尝试将值提高到这个最大值:

bswift$ ulimit -Ss unlimited
bswift$ ulimit -Ss
65532

Note: segmentation fault (SIGSEGV) (number 11) you observed is the signal sent to the process when the stack limit is exceeded according to man setrlimit

注意:您所观察到的分割错误(SIGSEGV)(第11号)是当堆栈限制超出了man setrlimit时发送给进程的信号。

Note: Since the ulimit command needs to be a shell builtin, you will find it documented in man bash

注意:由于ulimit命令需要是一个shell builtin,您将在man bash中找到它的文档。

#3


0  

This could be a permissions issue.

这可能是一个权限问题。

To diagnose further, run your program from the terminal with sudo dtruss prefixed. See which syscall it runs before throwing the error.

要进一步诊断,请使用sudo dtruss在终端运行您的程序。在抛出错误之前,请查看它运行的是哪个syscall。

Example: sudo dtruss /path/to/application

例如:sudo dtruss /道路/ /应用程序

You can also diagnose it with the Xcode or GDB debuggers.

您还可以使用Xcode或GDB调试器来诊断它。