When compiling the embedded v8 hello-world.cc
(https://chromium.googlesource.com/v8/v8/+/main/samples/hello-world.cc) code sample using gcc, it works properly:
$ 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
However, when I use clang, it segfaults when the v8::Isolate::Scope objects deconstructs:
$ 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
Stack trace:
#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 isn't a valid pointer (0x555500000001
), so it fails on this dereference:
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>
When debugging the gcc version, $rdi was 0x00555559b21000
, a valid pointer.
I've tried this with v12.9 and HEAD (781c2056824) on v8, and am using clang version 18.1.8
and gcc version 14.2.1 20240805 (GCC)
on x86_64. These are my build arguments via gn args
:
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
My gut take is this may be a bug in v8, but I am not sure.
This is probably what was reported here: if you compile V8 with is_debug = true
in your GN args, then you need to set -DV8_ENABLE_CHECKS
in your own compilation units that include V8 headers.
(That requirement should definitely be documented or eliminated or at the very least fail in a more understandable manner; it seems to be pretty new, or at least I only heard of it very recently. I would appreciate it if you could briefly report back whether this fixes your issue.)