谷歌v8新的Linux共享库失败引发“段错误”

时间:2016-08-17 09:12:05

标签: c++ linux v8

我在linux中使用Google V8时遇到问题。如果我在共享库中创建V8实例,则会出现段错误。相同的代码在Windows DLL和Linux可执行文件中工作正常。

我的代码:

extern "C" void InitV8ExtensionInterFace(){
    v8::V8::InitializeICU();
    v8::V8::Initialize();
    v8::Isolate* isolate = v8::Isolate::New(); **//error occur** 
    threadfunc(argc, args);
}

gdb堆栈跟踪:

#0  0x0000000000000000 in ?? ()
#1  0x00007ffff3cb86d5 in v8::internal::Builtins::SetUp (this=0x7fffffffb9e0, isolate=0x235abb0, create_heap_objects=false) at ../src/builtins.cc:1567
#2  0x00007ffff3e271cf in v8::internal::Isolate::Init (this=0x235abb0, des=0x0) at ../src/isolate.cc:2115
#3  0x00007ffff3c96049 in v8::Isolate::New (params=...) at ../src/api.cc:6861
#4  0x00007ffff3b78d40 in InitV8ExtensionInterFace () at ../Framework/ExPublic.cpp:107
#5  0x00000000004729db in myTest1 () at arangod/RestServer/arangod.cpp:106
#6  0x0000000000472a50 in main (argc=1, argv=0x7fffffffe118) at arangod/RestServer/arangod.cpp:126

似乎在V8函数void Builtins::SetUp(Isolate* isolate, bool create_heap_objects)中,数组functions为空。如果我初始化v8::Platform,则错误将出现在代码V8::InitializePlatform(platform)中:

extern "C" void InitV8ExtensionInterFace(){
    v8::V8::InitializeICU();
    v8::Platform* platform = v8::platform::CreateDefaultPlatform();
    v8::V8::InitializePlatform(platform); **//error occur** 
    v8::V8::Initialize();
    v8::Isolate* isolate = v8::Isolate::New(); 
    threadfunc(argc, args);
}

gdb堆栈跟踪:

 1: V8_Fatal
 2: v8::internal::V8::InitializePlatform(v8::Platform*)
 3: InitV8ExtensionInterFace
 4: 0x4aab60
 5: 0x4aacf1
 6: 0x5f4532
 7: 0x47c32b
 8: 0x474498
 9: 0x472ae1
10: __libc_start_main
11: 0x4726f9

Thread 1 received signal SIGABRT, Aborted.
0x00007ffff66595e5 in raise () from /lib64/libc.so.6
(gdb) where
#0  0x00007ffff66595e5 in raise () from /lib64/libc.so.6
#1  0x00007ffff665adc5 in abort () from /lib64/libc.so.6
#2  0x00007fffc6e7d9c9 in v8::base::OS::Abort () at ../src/base/platform/platform-posix.cc:233
#3  0x00007fffc6e7b586 in V8_Fatal (file=0x7fffc703f535 "../src/v8.cc", line=107, format=0x7fffc6ffe77a "Check failed: %s.") at ../src/base/logging.cc:116
#4  0x00007fffc6cbd909 in v8::internal::V8::InitializePlatform (platform=0x267d840) at ../src/v8.cc:107
#5  0x00007fffc690bc8b in InitV8ExtensionInterFace () at ../Framework/ExPublic.cpp:98
#6  0x00000000004aab60 in myTest () at arangod/V8Server/ApplicationV8.cpp:1068
#7  0x00000000004aacf1 in triagens::arango::ApplicationV8::prepare2 (this=0x2378310) at arangod/V8Server/ApplicationV8.cpp:1093
#8  0x00000000005f4532 in triagens::rest::ApplicationServer::prepare2 (this=0x2377000) at arangod/ApplicationServer/ApplicationServer.cpp:525
#9  0x000000000047c32b in triagens::arango::ArangoServer::startupServer (this=0x2375330) at arangod/RestServer/ArangoServer.cpp:1009
#10 0x0000000000474498 in triagens::rest::AnyServer::start (this=0x2375330) at arangod/Rest/AnyServer.cpp:347
#11 0x0000000000472ae1 in main (argc=1, argv=0x7fffffffe118) at arangod/RestServer/arangod.cpp:139

我在运行时使用v8::V8::GetVersion得到“4.3.61”。

这个问题困扰了我好几天,非常希望有人能给我帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

您错过了对V8::InitializeExternalStartupData()的来电,请参阅Get Started教程。

int main(int argc, char* argv[])
{
    // Initialize V8.
    V8::InitializeICU();
    V8::InitializeExternalStartupData(argv[0]);
    Platform* platform = platform::CreateDefaultPlatform();
    V8::InitializePlatform(platform);
    V8::Initialize();

    // ...
}

您需要将natives_blob.binsnapshot_blob.bin与可执行文件一起复制。他们应该在V8二进制文件的某个地方。

通过更好的堆栈跟踪查看编辑,您将遇到两个截然不同的问题。您的第一次崩溃(空functions数组)是因为您没有调用CreateDefaultPlatform(),这是强制性的。

第二次崩溃发生在InitializePlatform()这一行内:

void V8::InitializePlatform(v8::Platform* platform) {
    CHECK(!platform_);     // <- here
    CHECK(platform);
    platform_ = platform;
}

此检查是为了确保只创建一次默认平台。您似乎已经两次致电InitializePlatform()了。您可以尝试在其中放置一个断点,以确定从哪里调用它。