使用Clang编译V8示例代码时v8::internal::Isolate::Exit()触发段错误
Clang编译V8官方嵌入式示例触发段错误
问题现象
使用GCC编译V8官方的hello-world.cc示例时程序运行正常:
$ g++ -I/path/to/v8/include hello-world.cc -o hello_world -fno-rtti -lv8_monolith -lv8_libbase -lv8_libplatform -ldl -L/path/to/v8/out.gn/x64.release.sample/obj/ -pthread -std=c++20 -DV8_COMPRESS_POINTERS -DV8_ENABLE_SANDBOX -ggdb $ ./hello_world Hello, World! 3 + 4 = 7
但使用Clang编译时,程序执行完业务逻辑后,在v8::Isolate::Scope对象析构阶段触发段错误:
$ clang++ -I/path/to/v8/include hello-world.cc -o hello_world -fno-rtti -lv8_monolith -lv8_libbase -lv8_libplatform -ldl -L/path/to/v8/out.gn/x64.release.sample/obj/ -pthread -std=c++20 -DV8_COMPRESS_POINTERS -DV8_ENABLE_SANDBOX -ggdb $ ./hello_world Hello, World! 3 + 4 = 7 [1] 684462 segmentation fault (core dumped) ./hello_world
调试信息
栈跟踪显示段错误发生在v8::internal::Isolate::Exit()调用流程中:
#0 v8::internal::Isolate::Exit() () at ../../build/linux/debian_bullseye_amd64-sysroot/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/atomic_base.h:747 #1 0x0000555555698c68 in v8::Isolate::Scope::~Scope (this=0x7fffffffd250) at /path/to/v8/include/v8-isolate.h:312 #2 0x00005555556987ff in main (argc=0x1, argv=0x7fffffffd658) at hello-world.cc:96
此时寄存器$rdi指向无效地址0x555500000001,直接导致内存解引用操作失败:
0x55555594514a <v8::internal::Isolate::Exit()+10> push rbx 0x55555594514b <v8::internal::Isolate::Exit()+11> sub rsp, 0x10 0x55555594514f <v8::internal::Isolate::Exit()+15> mov r14, rdi → 0x555555945152 <v8::internal::Isolate::Exit()+18> mov rbx, QWORD PTR [rdi+0xf380] 0x555555945159 <v8::internal::Isolate::Exit()+25> test rbx, rbx 0x55555594515c <v8::internal::Isolate::Exit()+28> je 0x55555594523e <_ZN2v88internal7Isolate4ExitEv+254> 0x555555945162 <v8::internal::Isolate::Exit()+34> mov rax, QWORD PTR [rbx+0x8] 0x555555945166 <v8::internal::Isolate::Exit()+38> test rax, rax 0x555555945169 <v8::internal::Isolate::Exit()+41> je 0x55555594517d <_ZN2v88internal7Isolate4ExitEv+61>
对比GCC编译版本,调试时$rdi指向有效内存地址0x00555559b21000。
测试环境
- V8版本:v12.9、HEAD版本(781c2056824)
- 编译器:Clang 18.1.8、GCC 14.2.1
- 硬件架构:x86_64
- V8 GN构建参数:
dcheck_always_on = false is_component_build = false is_debug = true target_cpu = "x64" use_custom_libcxx = false v8_monolithic = true v8_use_external_startup_data = false
推测这可能是V8的bug,但不确定,寻求可行的解决方案。
内容的提问来源于stack exchange,提问作者captainGeech
相关产品推荐
相关产品推荐

