mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Bracken Dawson <abdawson@gmail.com>
To: musl@lists.openwall.com
Subject: Program with constructor function segfaults frequently with musl
Date: Thu, 15 Mar 2018 10:38:31 +0000	[thread overview]
Message-ID: <CAE-X3sfga=KTHwAKZpKzwEXfFUpDt8mw-_93aMong8_Q=Rnrnw@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2436 bytes --]

I have been having trouble getting a cgo program to run with musl, it has
been segfaulting frequently and with 'No stack' when run under gdb.

I have managed to reproduce such a failure in pure c with a very small
example:

```
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

__attribute__((constructor)) void enter_namespace(int argc, char *argv[]) {
    struct option long_options[] = {
        {"some-option", required_argument, 0, 's'},
        {0,0,0,0}
    };
    int option_index, c, pid;
    while ((c = getopt_long_only(argc, argv, "m:", long_options,
&option_index)) != -1) {
        switch (c) {
            case 's':
                pid = atoi(optarg);
                if (pid < 1) {
                    fprintf(stderr, "Invalid some-option: %s\n", optarg);
                    exit(1);
                }
                break;
            case 0:
                break;
        }
    }
}

int main(void) {
  return 0;
}
```

Run with or without options will segfault frequently:
```
~ # gcc -g test.c
~ # ./a.out
Segmentation fault
~ # ./a.out
~ # ./a.out
~ # ./a.out
~ # ./a.out
Segmentation fault
~ #
~ # gdb ./a.out
GNU gdb (GDB) 7.12.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html
>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-alpine-linux-musl".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
(gdb) r
Starting program: /root/a.out
warning: Error disabling address space randomization: Operation not
permitted
During startup program terminated with signal SIGSEGV, Segmentation fault.
(gdb) bt
No stack.
(gdb)
```
It appears that having any code in a constructor function leads to this
problem, having the same code in the main function does not segfault.

I'm not sure how to take this any further without a backtrace.

I am using musl 1.1.16-r14 on alpine 3.6.0. The code above works with glibc.

Regards,
Bracken Dawson.

:wq

[-- Attachment #2: Type: text/html, Size: 6654 bytes --]

             reply	other threads:[~2018-03-15 10:38 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-15 10:38 Bracken Dawson [this message]
2018-03-15 11:01 ` Szabolcs Nagy
2018-03-15 11:17   ` Szabolcs Nagy
2018-03-15 11:28     ` Bracken Dawson
2018-03-15 12:12       ` Szabolcs Nagy
2018-03-15 15:35       ` Rich Felker
2018-03-15 20:34       ` Markus Wichmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAE-X3sfga=KTHwAKZpKzwEXfFUpDt8mw-_93aMong8_Q=Rnrnw@mail.gmail.com' \
    --to=abdawson@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).