升级至macOS 15.4后D语言程序触发Segmentation fault:11崩溃
macOS 15.4升级后D语言可执行文件Segmentation Fault问题分析
将macOS从15.3.1升级至15.4后,D语言可执行文件出现Segmentation fault: 11崩溃,使用lldb调试得到以下信息:
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT) frame #0: 0x00007ff80c4978c1 libsystem_pthread.dylib`pthread_getspecific libsystem_pthread.dylib`pthread_getspecific: -> 0x7ff80c4978c1 <+0>: movq %gs:(,%rdi,8), %rax 0x7ff80c4978ca <+9>: retq libsystem_pthread.dylib`pthread_setspecific: 0x7ff80c4978cb <+0>: leaq -0xa(%rdi), %rcx 0x7ff80c4978cf <+4>: movl $0x16, %eax Target 0: (test) stopped. (lldb) bt * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT) * frame #0: 0x00007ff80c4978c1 libsystem_pthread.dylib`pthread_getspecific frame #1: 0x0000000100091df3 test`_D2rt18sections_darwin_647tlvInfoFNbNixPS4core3sys6darwin4mach6loader14mach_header_64ZSQDhQDh13dyld_tlv_info(header=0x0000000100000000) at sections_darwin_64.d:95:5 frame #2: 0x0000000100091d72 test`_D2rt18sections_darwin_6411getTLSRangeFNbNixPvZAv(tlsSymbol="\U00000001") at sections_darwin_64.d:43:9 frame #3: 0x0000000100092416 test`_D2rt19sections_elf_shared3DSO8tlsRangeMxFNbNiZAv(this=<unavailable>) at sections_elf_shared.d:201:13 frame #4: 0x0000000100092957 test`_d_dso_registry(arg="\U00000001") at sections_elf_shared.d:606:13 frame #5: 0x0000000100088d63 test`_D2rt3dso12register_dsoFZv at dso.d:51:9 frame #6: 0x00007ff80c10c5a2 dyld`invocation function for block in dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const + 290 frame #7: 0x00007ff80c13de9a dyld`invocation function for block in dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const + 234 frame #8: 0x00007ff80c16c45c dyld`invocation function for block in mach_o::Header::forEachSection(void (mach_o::Header::SectionInfo const&, bool&) block_pointer) const + 271 frame #9: 0x00007ff80c16985d dyld`mach_o::Header::forEachLoadCommand(void (load_command const*, bool&) block_pointer) const + 191 frame #10: 0x00007ff80c16adb2 dyld`mach_o::Header::forEachSection(void (mach_o::Header::SectionInfo const&, bool&) block_pointer) const + 116 frame #11: 0x00007ff80c13da7c dyld`dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const + 470 frame #12: 0x00007ff80c10c40a dyld`dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const + 150 frame #13: 0x00007ff80c112bdb dyld`dyld4::JustInTimeLoader::runInitializers(dyld4::RuntimeState&) const + 21 frame #14: 0x00007ff80c10c77c dyld`dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&, dyld3::Array<dyld4::Loader const*>&) const + 276 frame #15: 0x00007ff80c1101c9 dyld`dyld4::Loader::runInitializersBottomUpPlusUpwardLinks(dyld4::RuntimeState&) const::$_0::operator()() const + 147 frame #16: 0x00007ff80c10c825 dyld`dyld4::Loader::runInitializersBottomUpPlusUpwardLinks(dyld4::RuntimeState&) const + 115 frame #17: 0x00007ff80c1267cb dyld`dyld4::APIs::runAllInitializersForMain() + 269 frame #18: 0x00007ff80c0f8634 dyld`dyld4::prepare(dyld4::APIs&, mach_o::Header const*) + 3587 frame #19: 0x00007ff80c0f781f dyld`dyld4::start(dyld4::KernelArgs*, void*, void*)::$_0::operator()() const + 239 frame #20: 0x00007ff80c0f74da dyld`start + 2970
调试信息解析
- 崩溃核心:在系统库
libsystem_pthread.dylib的pthread_getspecific函数中触发EXC_BAD_ACCESS(非法内存访问),错误码EXC_I386_GPFLT表示通用保护错误,本质是尝试访问了无效的内存地址。具体指令movq %gs:(,%rdi,8), %rax中,传入的参数%rdi(即pthread_getspecific的第一个参数)无效,导致访问GS段寄存器指向的内存失败。 - 调用链逻辑:崩溃发生在程序启动阶段的初始化流程中:
- dyld(macOS动态链接器)负责加载并运行可执行文件的初始化器(frame 6-20);
- D语言运行时(DRT)执行DSO(动态共享对象)注册逻辑,在获取TLS(线程本地存储)范围时出现错误(frame 1-5);
- 最终调用系统
pthread_getspecific函数时触发崩溃。
可能原因与解决方向
- 该问题属于macOS 15.4升级后,D语言运行时的TLS处理逻辑与新系统的pthread API不兼容导致的兼容性问题。
- 解决建议:
- 更新D语言编译器(如dmd、ldc)至最新稳定版本,官方通常会针对新系统版本修复兼容性问题;
- 使用更新后的编译器重新编译可执行文件,确保生成的二进制适配macOS 15.4;
- 检查代码中是否存在自定义TLS操作,确认是否符合新系统的API规范。
内容的提问来源于stack exchange,提问作者Cornelius Roemer
相关产品推荐
相关产品推荐

