Getting Seg Fault when I try to dynamically load a custom library (.so) which is compiled with webkit2gtk library

266 views Asked by At

I have created a shared library which has a function displaywebview that launches a GTK window and loads the URL into it using webkit2gtk.

Now I am writing a caller program which loads this library using dlopen, gets the method displaywebview using dlsym and calls this function.

I get a seg fault inside displaywebview at the point where I call webkit_web_view_new(). Could someone help me out on why this is happening?

webkit_main.so

#include <gtk/gtk.h>
#include <webkit2/webkit2.h>

extern "C"
{
    int displayWebView();
}

int displayWebView()
{

    printf("Entered in displayWebView\n");
    // Initialize GTK+
    gtk_init(NULL, NULL);

    // Create an 800x600 window that will contain the browser instance
    GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(main_window), 800, 600);

    WebKitWebView *webView = (WebKitWebView*)webkit_web_view_new();

    // webkit_web_view_new();

    // // Put the browser area into the main window
    gtk_container_add(GTK_CONTAINER(main_window), GTK_WIDGET(webView));

    // Set up callbacks so that if either the main window or the browser instance is
    // closed, the program will exit
    g_signal_connect(main_window, "destroy", G_CALLBACK(destroyWindowCb), NULL);
    g_signal_connect(webView, "close", G_CALLBACK(closeWebViewCb), main_window);

    // // Load a web page into the browser instance
    webkit_web_view_load_uri(webView, "http://www.gmail.com");

    // // Make sure that when the browser area becomes visible, it will get mouse
    // // and keyboard events
    gtk_widget_grab_focus(GTK_WIDGET(webView));

    // // Make sure the main window and all its contents are visible
    gtk_widget_show_all(main_window);

    // // Run the main GTK+ event loop
    gtk_main();

    return 0;
}

caller.cpp

#include <unistd.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>

typedef int (*PDISPLAYWEBVIEW)();

int main(){
    void* hnd = dlopen("/home/radix/Desktop/webkit_socket/webkit_main.so", RTLD_LAZY);
    // sleep(10);
    if(hnd!=NULL){

        PDISPLAYWEBVIEW pdisplayWebView = (PDISPLAYWEBVIEW)dlsym(hnd,"displayWebView");
        if(pdisplayWebView == NULL){
            printf("dlsym error %s", dlerror());
        }
        else{
            printf("Everything okay, launch the function\n");

            (*pdisplayWebView)();

        }

        dlclose(hnd);

    }
    else{
        printf("The error is %s", dlerror());
    }
}

BACKTRACE details:

Thread 1 "caller" received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff790d165 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007ffff2009168 in bmalloc::Scavenger::Scavenger(std::lock_guard<bmalloc::Mutex>&) () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#3  0x00007ffff1d12f61 in bmalloc::PerProcess<bmalloc::Scavenger>::getSlowCase() () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#4  0x00007ffff2001fcc in bmalloc::Heap::Heap(bmalloc::HeapKind, std::lock_guard<bmalloc::Mutex>&) () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#5  0x00007ffff1fff1ff in bmalloc::PerProcess<bmalloc::PerHeapKind<bmalloc::Heap> >::getSlowCase() () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#6  0x00007ffff1ffee99 in bmalloc::Cache::Cache(bmalloc::HeapKind) () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#7  0x00007ffff1fff311 in bmalloc::PerThread<bmalloc::PerHeapKind<bmalloc::Cache> >::getSlowCase() () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#8  0x00007ffff1ffef0d in bmalloc::Cache::allocateSlowCaseNullCache(bmalloc::HeapKind, unsigned long) () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#9  0x00007ffff1af22a2 in JSC::ExecutableAllocator::initializeAllocator() () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#10 0x00007ffff1d0bf25 in ?? () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#11 0x00007ffff0926739 in __pthread_once_slow (once_control=0x7ffff22a6ff0, init_routine=0x7ffff790c120 <__once_proxy>) at pthread_once.c:116
#12 0x00007ffff1d0d90d in JSC::initializeThreading() () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#13 0x00007ffff49beb29 in ?? () from /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37
#14 0x00007ffff4aa4add in ?? () from /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37
#15 0x00007ffff4b0eb00 in ?? () from /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37
#16 0x00007ffff0e5e777 in ?? () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#17 0x00007ffff0e5fc0d in g_object_newv () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#18 0x00007ffff0e603c4 in g_object_new () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#19 0x00007ffff4b0ab7d in ?? () from /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37
#20 0x00007ffff0ba74a5 in g_once_impl () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#21 0x00007ffff4b31cc9 in webkit_web_view_new () from /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37
#22 0x00007ffff6d96f0f in displayWebView () at webkit_main.cpp:81
#23 0x00005555555548df in main () at caller.cpp:20

Note:

  1. When I use the same program with webkitgtk-1.0 it runs absolutely fine. With webkit2gtk-4.0 it gives this issue.

  2. When I compile caller.cpp with libwebkit2gtk-4.0 it strangely doesn't give seg fault anymore.

Could someone help me out why this is happening?

I am using debian 9 with webkit2gtk-4.0-37 of version: 2.22.2-1~bpo9+1

1

There are 1 answers

0
Lorinczy Zsigmond On BEST ANSWER

There are some compiler options that have to be the same within every components (object modules, libraries, shared objects) so that they could work together. Other than -pthread, large-file-support (in 32-bit) comes to my mind.

Another possible source of problems if different versions of a component are linked together.