0

I have a program that utilizes libdispatch in Ubuntu linux. At the end of my main function, I have the dispatch_main() to keep the main thread hanging. The rest of my program runs as expected but for some reason once dispatch_main is called at the end, it exits with SIGILL. Running GDB, here is the output below. Backtrace at the end shows it's failing at dispatch_release.

program:

int main(int argc, const char *argv[]) {
    char *error_message = NULL;

    // Process command line args.
    int option;
    while ((option = getopt_long(argc, (char * const *)argv, "i:", longopts, NULL)) != -1) {
        switch (option) {
            case 'i':
                device_path = optarg;
                break;
        }
    }       

    // prompt has its own "read" function, so instead of using a
    // DispatchSource to read from STDIN_FILENO a global queue is used to loop
    // while it receives input.
//    dispatch_queue_t input_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_queue_t input_queue = dispatch_queue_create("serialctl.prompt_input", NULL);
    dispatch_async(input_queue, ^{
        char *line;
        while((line = prompt_read()) != NULL) {
            interpret_command(line);
            free(line);
        }
    });


    char *home_id_str = create_home_id_str(device->home_id);
    prompt_printf("Initialized SerialAPI device at '%s'\n", device->path);
    prompt_printf("Library '0x%02x' - Version '%.2Lf'\n", device->library, device->version);
    prompt_printf("Home ID '%s' - Node ID '%03d'\n", home_id_str, device->node_id);
    free(home_id_str);



    dispatch_main();
}

gdb debug:

 (gdb) r
    Starting program: /home/jia/libzwsapi/cmake-build-debug/serialapictl -i /dev/ttyACM2
    warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    [New Thread 0x7ffff6ba8700 (LWP 2813)]
    [New Thread 0x7ffff63a7700 (LWP 2814)]
    serialAPI> [New Thread 0x7ffff5ba6700 (LWP 2815)]
                                                     [New Thread 0x7ffff53a5700 (LWP 2816)]
                                                                                           [New Thread 0x7ffff4ba4700 (LWP 2817)]
                                                                                                                                 [New Thread 0x7fffe7fff700 (LWP 2818)]
                                       [New Thread 0x7fffe77fe700 (LWP 2819)]
    Initialized SerialAPI device at '/dev/ttyACM2'
    Library '0x07' - Version '4.33'
    Home ID 'dbf54165' - Node ID '001'
    serialAPI> 
               Thread 1 "serialapictl" received signal SIGILL, Illegal instruction.
                                                                                   0x00007ffff79c6b73 in _dispatch_release () from /usr/lib/libdispatch.so.0
    (gdb) bt
    #0  0x00007ffff79c6b73 in _dispatch_release () from /usr/lib/libdispatch.so.0
    #1  0x00007ffff79c77bb in dispatch_main () from /usr/lib/libdispatch.so.0
    #2  0x0000000000402e69 in main (argc=3, argv=0x7fffffffe368) at /home/jia/libzwsapi/serialapictl/main.c:138
    (gdb) 
Jia Li
  • 5
  • 5
  • Possible lead: the stack might get corrupted at some point. Buffer overflow with an off-by-one error for instance would overwrite the last byte of the return address with garbage, causing the return to go to an unexpected place, yet still in the code area, and attempt to run what's there. – spectras Sep 02 '17 at 02:20
  • Now, pictures of stuff are discouraged here, as they are not searchable and will go away at some point. Please copy the output as actual text here (use the `{}` formatting icon to make it fixed-width font). And adding the actual code would help if you want a useful answer. – spectras Sep 02 '17 at 02:22
  • @spectras removed the image and added the code – Jia Li Sep 03 '17 at 16:28

0 Answers0