A pre-compiled binary from ubuntu gets seg fault on alpine due to duplocale()

61 Views Asked by At

I have code I compiled on my Ubuntu 22.04:

#include <locale.h>
#include <stdio.h>

int main() {
        printf("1\n");
        locale_t oldlocale = uselocale(NULL);
        printf("2\n");
        locale_t duploc = duplocale(oldlocale);
        printf("3\n");
        return 0;
}

Running it on the same Ubuntu works:

$ gcc -g c.c && ./a.out 
1
2
3

But after copying it into an alpine container, I get a seg fault:

# ./a.out 
1
2
Segmentation fault (core dumped)

As you can see, it happens due to the function duplocale().
I have installed on that alpine container couple of stuff, such as libc6-compat.
When compiling the same piece of code on alpine and then running it, it works. But this isn't the case and I cannot use that approach. I need to be able to compile on Ubuntu. The original issue is having libjson-c which uses this duplocale() and gets seg fault. Even when dynamically linking with -ljson-c and then using alpine's json-c I still get this seg fault.

Is there a way to work around it?
Thanks


Update: backtrace - there's not much of it.

  • Through alpine:
process 340 is executing new program: /lib/ld-musl-x86_64.so.1
1
2

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f46aa7 in ?? ()
(gdb) bt
#0  0x00007ffff7f46aa7 in ?? ()
#1  0x0000000000000001 in ?? ()
#2  0x00007ffff7f521cd in ?? ()
#3  0xffffffffffffffff in ?? ()
#4  0x00007ffff7fc1600 in __libc_start_init () at ldso/dynlink.c:1621
#5  0x00007ffff7f786d1 in libc_start_main_stage2 (main=0x7ffff7f52189, argc=-1, argv=0x7fffffffebb0)
    at src/env/__libc_start_main.c:95
#6  0x00007ffff7f520c5 in ?? ()
#7  0x00007fffffffebf8 in ?? ()
#8  0x00007ffff7f520a0 in ?? ()
#9  0x0000000000000001 in ?? ()
#10 0x00007fffffffee50 in ?? ()
#11 0x0000000000000000 in ?? ()
  • Taking the core back to Ubuntu and checking it there:
Core was generated by `ld-linux-x86-64.so.2 --argv0 ./a.out --preload /lib/libgcompat.so.0  -- /dm/res'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f1c76999aa7 in ?? ()
(gdb) bt
#0  0x00007f1c76999aa7 in ?? ()
#1  0x0000000000000001 in ?? ()
#2  0x00007f1c769a51cd in ?? ()
#3  0xffffffffffffffff in ?? ()
#4  0x00007f1c76a0e600 in ?? ()
#5  0x00007ffc8621ad98 in ?? ()
#6  0x00007f1c769c56d1 in ?? ()
#7  0x0000000000000000 in ?? ()

Note, the file is with debug_info, not stripped.


I just found out this, maybe it's related https://github.com/json-c/json-c/blob/master/CMakeLists.txt#L178C11-L178C24:

# uClibc *intentionally* crashes in duplocale(), at least as of:
# https://github.com/ffainelli/uClibc/blob/266bdc1/libc/misc/locale/locale.c#L1322
# So, if it looks like we're compiling for a system like that just disable
# locale handling entirely.
0

There are 0 best solutions below