Link with glib2 break emulated tls

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Link with glib2 break emulated tls

zh_jq
>Synopsis: Emulated tls is broken if the binary use glib2 functions
>Category: library
>Environment:
        System      : OpenBSD 6.5
        Details     : OpenBSD 6.5 (GENERIC.MP) #3: Sat Apr 13 14:48:43 MDT 2019
                         [hidden email]:/usr/src/sys/arch/amd64/compile/GENERIC.MP

        Architecture: OpenBSD.amd64
        Machine     : amd64
>Description:
        A .so export a thread local variable, and has a function to modify its value.
        Then call that function in main, and also call glib2 function there, this
        cause the variable valus seen by main differ from expected.
>How-To-Repeat:
        Source code to repeat:
        ### foo.h ###
        ----
#include <signal.h>
extern _Thread_local volatile sig_atomic_t foo_thread_events;
extern void set_thread_events(int bit);
        ----
        ### foo.c ###
        ----
#include "foo.h"

_Thread_local volatile sig_atomic_t foo_thread_events = 0;

void set_thread_events(int bit)
{
        foo_thread_events |= bit;
}
        ----
        ### bar.c ###
        ----
#include "foo.h"

#include <stdio.h>

#include <glib.h>

int main(void)
{
        g_log("test", G_LOG_LEVEL_CRITICAL, "...");
        set_thread_events(0x01);
        if (foo_thread_events & 0x01) {
                fprintf(stdout, "set OK\n");
        } else {
                fprintf(stdout, "set Failed\n");
        }

        return 0;
}
        ----
        ### Commands to compile ###
        ----
        cc -shared -fPIC -o foo.so foo.c
        cc -o bar bar.c foo.so `pkg-config --cflags --libs glib-2.0`
        export LD_LIBRARY_PATH=.
        ./bar
        ----
>Fix: