Boinst
Boinst

Reputation: 3505

V8 "Hello World" Segfault on "InitializeBuiltinTypedArrays"

This code is working fine on Windows/Visual Studio, but segfaulting on Ubuntu/GCC. Why?

I've compiled V8 4.5.8 to a shared library. I've put the following code in a CPPUNIT test.

/**
* A simple test for basic V8 functionality, just running the "Hello World" example from
* from <a href="https://developers.google.com/v8/get_started">the getting started guide</a>.
**/
CPPUNIT_QUICKTEST(V8Basics, GettingStartedGuide)
{
    v8::V8::InitializeICU();
    v8::Platform * platform = v8::platform::CreateDefaultPlatform();
    v8::V8::InitializePlatform(platform);
    v8::V8::Initialize();

    // Create a new Isolate and make it the current one.
    Isolate* isolate = Isolate::New();
    {
        Isolate::Scope isolate_scope(isolate);

        // Create a stack-allocated handle scope.
        HandleScope handle_scope(isolate);

        // Create a new context.
        Local<Context> context = Context::New(isolate);

        // Enter the context for compiling and running the hello world script.
        Context::Scope context_scope(context);

        // Create a string containing the JavaScript source code.
        Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");

        // Compile the source code.
        Local<Script> script = Script::Compile(source);

        // Run the script to get the result.
        Local<Value> result = script->Run();

        // Convert the result to an UTF8 string and print it.
        String::Utf8Value utf8(result);

        CPPUNIT_ASSERT_STRING_EQUAL("Hello, World!", std::string(*utf8));
    }

    // Dispose the isolate and tear down V8.
    isolate->Dispose();

    v8::V8::Dispose();
    v8::V8::ShutdownPlatform();
    delete platform;
}

This test segfaults with the following stack. Most of this stack is CppUnit and the enclosing test executable.

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f75cabf8ce9 in v8::internal::Genesis::InitializeBuiltinTypedArrays() ()
   from /my-working-dir/Debug/bin/../lib/libv8.so
(gdb) bt
#0  0x00007f75cabf8ce9 in v8::internal::Genesis::InitializeBuiltinTypedArrays() ()
   from /my-working-dir/Debug/bin/../lib/libv8.so
#1  0x00007f75cabfe743 in v8::internal::Genesis::Genesis(v8::internal::Isolate*, v8::internal::MaybeHandle<v8::internal::JSGlobalProxy>, v8::Local<v8::ObjectTemplate>, v8::ExtensionConfiguration*) () from /my-working-dir/Debug/bin/../lib/libv8.so
#2  0x00007f75cabed179 in v8::internal::Bootstrapper::CreateEnvironment(v8::internal::MaybeHandle<v8::internal::JSGlobalProxy>, v8::Local<v8::ObjectTemplate>, v8::ExtensionConfiguration*) () from /my-working-dir/Debug/bin/../lib/libv8.so
#3  0x00007f75cabb4e50 in v8::Context::New(v8::Isolate*, v8::ExtensionConfiguration*, v8::Local<v8::ObjectTemplate>, v8::Local<v8::Value>) ()
   from /my-working-dir/Debug/bin/../lib/libv8.so
#4  0x00007f75ccc34fd0 in V8Basics_GettingStartedGuide::TestBody (this=0x25fda50) at v8core_v8basics_tests.cpp:31
#5  0x00007f75ccc374e2 in CppUnit::TestCaller<V8Basics_GettingStartedGuide>::runTest (this=0x2600a80)
    at ../../libs/3rdParty/include/cppunit/TestCaller.h:166
#6  0x00007f75cc2b8012 in CppUnit::TestCaseMethodFunctor::operator() (this=<optimised out>) at TestCase.cpp:32
#7  0x00007f75cd4d6793 in testsuite::ExceptionProtector::protect (this=0x25fef10, functor=..., context=...) at testsuite_exceptionprotector.cpp:27
#8  0x00007f75cc2b535d in CppUnit::ProtectorChain::protect (this=0x25ffef0, functor=..., context=...) at ProtectorChain.cpp:77
#9  0x00007f75cc2bdb7a in CppUnit::TestResult::protect (this=this@entry=0x7ffc69f1ace0, functor=..., test=test@entry=0x2600a80, shortDescription=...)
    at TestResult.cpp:178
#10 0x00007f75cc2b7d1a in CppUnit::TestCase::run (this=0x2600a80, result=0x7ffc69f1ace0) at TestCase.cpp:92
#11 0x00007f75cc2b8363 in CppUnit::TestComposite::doRunChildTests (this=0x2600600, controller=0x7ffc69f1ace0) at TestComposite.cpp:64
#12 0x00007f75cc2b827e in CppUnit::TestComposite::run (this=0x2600600, result=0x7ffc69f1ace0) at TestComposite.cpp:23
#13 0x00007f75cc2b8363 in CppUnit::TestComposite::doRunChildTests (this=0x26005a0, controller=0x7ffc69f1ace0) at TestComposite.cpp:64
#14 0x00007f75cc2b827e in CppUnit::TestComposite::run (this=0x26005a0, result=0x7ffc69f1ace0) at TestComposite.cpp:23
#15 0x00007f75cd4d9739 in testsuite::TestRunner::Impl::ExecuteTests (this=0x25c3a50, suites=..., registry=0x26005a0, controller=..., result=...)
    at testsuite_testrunner.cpp:217
#16 0x00007f75cd4d9158 in testsuite::TestRunner::Run (this=0x7ffc69f1af08) at testsuite_testrunner.cpp:187
#17 0x0000000000426ad0 in testsuite::TestSuiteApplication::ExecuteTests (this=0x7ffc69f1aef0) at testsuite_testsuiteapplication.cpp:308
#18 0x0000000000426a2b in testsuite::TestSuiteApplication::Run (this=0x7ffc69f1aef0) at testsuite_testsuiteapplication.cpp:288
#19 0x0000000000422741 in main (argc=3, argv=0x7ffc69f1b018, env=0x7ffc69f1b038) at testsuite_main.cpp:64

Why am I seg faulting and how can I fix it?

My successful Windows build uses V8 4.5.8, Windows 8.1, Visual Studio 2013. My unsuccessful Ubuntu build uses GCC 4.8.2, Ubuntu 14.04.2.

Upvotes: 1

Views: 629

Answers (1)

Fabian
Fabian

Reputation: 86

i had the same issue. Try to create your Isolate like in the samples(hello-world). It works for me :)

class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
    virtual void* Allocate(size_t length) {
        void* data = AllocateUninitialized(length);
        return data == NULL ? data : memset(data, 0, length);
    }
    virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
    virtual void Free(void* data, size_t) { free(data); }
};

And the Isolate:

// Create a new Isolate and make it the current one.
ArrayBufferAllocator allocator;
Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &allocator;
Isolate* isolate = Isolate::New(create_params);

Upvotes: 3

Related Questions