xkbcomp crashes with segmentation fault in non-standard build
Hello. I am trying to build Xvfb
+xkbcomp
with cmake to use it in cross-compiled environment.
Everything is fine when xkbcomp
is built as executable. But it throws segmentation fault when I am trying to build it as shared library.
I am replacing system execl
in Xvfb with my own implementation which starts xkbcomp's main (xkbcomp_main
, renamed using -Dmain=xkbcomp_main
).
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <sys/signal.h>
extern int xkbcomp_main(int argc, char *argv[]);
char** parse_arguments(const char* line, int* argc) {
char* cmdline = strdup(line);
char** argv = (char**)malloc(strlen(cmdline));
int arg_index = 0;
int in_quotes = 0;
const char* token_start = cmdline;
for(char* p = cmdline;;p++) {
if ((*p == ' ' && !in_quotes) || *p == '\0') {
if (*token_start == '"')
token_start++;
long token_len = p - token_start;
if (token_start[token_len-1] == '"')
token_len--;
if (token_len > 0) {
argv[arg_index] = (char *) malloc(sizeof(char) * (token_len + 1));
memcpy(argv[arg_index], token_start, token_len);
argv[arg_index][token_len] = '\0';
arg_index++;
token_start = p + 1;
}
if (*p == '\0') {
argv[arg_index] = NULL;
break;
}
} else if (*p == '"' && *(p - 1) != '\\') {
in_quotes = !in_quotes;
}
}
if (argc)
*argc = arg_index;
free((void*) cmdline);
return argv;
}
int execl(const char * path, const char * arg, ...) {
size_t argv_max = 1024;
const char **argv = alloca(argv_max * sizeof(const char *));
unsigned int i;
va_list args;
va_start(args, arg);
argv[0] = arg;
i = 0;
while (argv[i++] != NULL) {
if (i == argv_max) {
const char **nptr = alloca((argv_max *= 2) * sizeof(const char *));
if ((char *) argv + i == (char *) nptr)
argv_max += i;
else
argv = (const char **) memcpy(nptr, argv, i * sizeof(const char *));
}
argv[i] = va_arg(args, const char *);
}
va_end(args);
assert(!strcmp(path, "/bin/sh"));
assert(!strcmp(argv[0], "sh"));
assert(!strcmp(argv[1], "-c"));
assert(argv[2] != NULL);
assert(argv[3] == NULL);
int argc;
char** new_args = parse_arguments(argv[2], &argc);
// signal(SIGINT, SIG_DFL);
// signal(SIGILL, SIG_DFL);
// signal(SIGABRT, SIG_DFL);
// signal(SIGFPE, SIG_DFL);
// signal(SIGSEGV, SIG_DFL);
// signal(SIGTERM, SIG_DFL);
// signal(SIGHUP, SIG_DFL);
// signal(SIGQUIT, SIG_DFL);
// signal(SIGTRAP, SIG_DFL);
// signal(SIGPIPE, SIG_DFL);
// signal(SIGALRM, SIG_DFL);
// signal(SIGIO, SIG_DFL);
// signal(SIGIOT, SIG_DFL);
// signal(SIGCLD, SIG_DFL);
sleep(1);
xkbcomp_main(argc, new_args);
exit(0);
}
I am getting some unclear message
(EE)
(EE) Backtrace:
(EE) 0: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x1bceb2) [0x55b68c299eb2]
(EE) 1: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x1c1f4e) [0x55b68c29ef4e]
(EE) 2: /lib/x86_64-linux-gnu/libc.so.6 (0x7f5286e00000+0x3bcf0) [0x7f5286e3bcf0]
(EE) 3: /lib/x86_64-linux-gnu/libc.so.6 (0x7f5286e00000+0x17364d) [0x7f5286f7364d]
(EE) 4: /home/twaik/Xvfb_building/cmake-build-debug/libxkbcomp.so (0x7f5287265000+0x1ed84) [0x7f5287283d84]
(EE) 5: /home/twaik/Xvfb_building/cmake-build-debug/libxkbcomp.so (0x7f5287265000+0x1f381) [0x7f5287284381]
(EE) 6: /home/twaik/Xvfb_building/cmake-build-debug/libxkbcomp.so (0x7f5287265000+0x1e93c) [0x7f528728393c]
(EE) 7: /home/twaik/Xvfb_building/cmake-build-debug/libxkbcomp.so (0x7f5287265000+0x1f2de) [0x7f52872842de]
(EE) 8: /home/twaik/Xvfb_building/cmake-build-debug/libxkbcomp.so (CompileKeycodes+0x79) [0x7f5287284516]
(EE) 9: /home/twaik/Xvfb_building/cmake-build-debug/libxkbcomp.so (CompileKeymap+0x375) [0x7f5287284b70]
(EE) 10: /home/twaik/Xvfb_building/cmake-build-debug/libxkbcomp.so (xkbcomp_main+0x598) [0x7f5287296f42]
(EE) 11: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (execl+0x482) [0x55b68c2b4d46]
(EE) 12: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x1c64f0) [0x55b68c2a34f0]
(EE) 13: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x1a380e) [0x55b68c28080e]
(EE) 14: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x1a3996) [0x55b68c280996]
(EE) 15: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x1a3f48) [0x55b68c280f48]
(EE) 16: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x1a4337) [0x55b68c281337]
(EE) 17: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x1a44b5) [0x55b68c2814b5]
(EE) 18: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x193cab) [0x55b68c270cab]
(EE) 19: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x194221) [0x55b68c271221]
(EE) 20: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x701cc) [0x55b68c14d1cc]
(EE) 21: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x7000d) [0x55b68c14d00d]
(EE) 22: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x70559) [0x55b68c14d559]
(EE) 23: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x86355) [0x55b68c163355]
(EE) 24: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x23d15) [0x55b68c100d15]
(EE) 25: /lib/x86_64-linux-gnu/libc.so.6 (0x7f5286e00000+0x23510) [0x7f5286e23510]
(EE) 26: /lib/x86_64-linux-gnu/libc.so.6 (__libc_start_main+0x89) [0x7f5286e235c9]
(EE) 27: /home/twaik/Xvfb_building/cmake-build-debug/Xvfb (0x55b68c0dd000+0x20bb5) [0x55b68c0fdbb5]
(EE)
(EE) Segmentation fault at address 0x0
(EE)
I know it is xserver's backtrace, but I am running it as a shared library and it is started in forked process without execl
.
When I am trying to debug this with gdb segmentation fault is not appearing so I can not debug this.
I am begging you for help.
Thank you for your great job.