From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12599 Path: news.gmane.org!.POSTED!not-for-mail From: Bracken Dawson Newsgroups: gmane.linux.lib.musl.general Subject: Program with constructor function segfaults frequently with musl Date: Thu, 15 Mar 2018 10:38:31 +0000 Message-ID: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a1140bfb8c87484056771175f" X-Trace: blaine.gmane.org 1521110206 6627 195.159.176.226 (15 Mar 2018 10:36:46 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 15 Mar 2018 10:36:46 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-12613-gllmg-musl=m.gmane.org@lists.openwall.com Thu Mar 15 11:36:42 2018 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1ewQFV-0001dg-3x for gllmg-musl@m.gmane.org; Thu, 15 Mar 2018 11:36:41 +0100 Original-Received: (qmail 3184 invoked by uid 550); 15 Mar 2018 10:38:44 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 3148 invoked from network); 15 Mar 2018 10:38:43 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=1qq/JwhtrL8qZ/cH2Rt4DgnR51Ud/h5GgE4DNQf98uM=; b=puo2B2AmLB01IIzot5AqX15DvzhAlWyOwwRx7mkaMJKujq2tYAnPWdhaEwtgXCk/dU bU8B9PjDQ4h+OY4L07YNJiRcgf1efT+QYXgx8JXavcbq0Y8ws4x/2FoyHTrZAdaCrlxV GhIOXjnS5hawmNFAVi8JxLL6edNu++fNdLXCftbHisW7bEFw1VQeqViqvqNPVsxZpJn0 UPgJPMp+abpizWrKglzC6dXTTNsct3vkd+9ElkiTUtGVIBZCZGT170uxWGWMARzxm+Hx bp6NL6xKKdNYSC5iaA4CQs/weYNYgRF1MBliV+kSmp+q303NWLRMNYVhBemenc0VENcu sVow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=1qq/JwhtrL8qZ/cH2Rt4DgnR51Ud/h5GgE4DNQf98uM=; b=n8jii7xc7QMXJ+NOY+JR2yHD9aPhDzOyPBtDvUSUmsNwKd+5eoAnOPrGCkUqgxLTmq kDmu05PzTYyRbKRCo/lfKd74zrfyHNhmwK6kntOLCA8eX/OJ45ucp8xTyCSq7rDe2Qqi WdlTZdciC5AhLp864r7KV5+HwNLObPtPXoqwfI2q8zURRxZ7d6GxrEpVJBz06z88MB9k urqFxRVzdHFejBFUvfg9sHRAs/KG21Bp2siszBn2MizmwIIGpHuYf5kqYpGQ3zNmbjAh XPm2j+ytHnUvvVKU3ptz7GG7L3awTAiOWEuxvSXi+wWjKmkqusuexXIPQUM+h8z/aqju KjIg== X-Gm-Message-State: AElRT7EHfGQR1U8Jr9512P7U5BQfrDCoBkwmU8lC/N/DZHKEZbGFBHdf Xefo/TSSjclG6G2NHFXWiCnBvPRPIddLFQIpDAc3Fk9P X-Google-Smtp-Source: AG47ELv8vF22ln9ZIbLorbiYYOMFs/F3UX9c6pJ7ILA5gpep+RZ02MoKiJKK+NfqwbWfU3QhuJYHIacmhYvcILlQ14c= X-Received: by 10.107.32.79 with SMTP id g76mr8251580iog.101.1521110311529; Thu, 15 Mar 2018 03:38:31 -0700 (PDT) Xref: news.gmane.org gmane.linux.lib.musl.general:12599 Archived-At: --001a1140bfb8c87484056771175f Content-Type: text/plain; charset="UTF-8" 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 #include #include __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 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: . Find the GDB manual and other documentation resources online at: . 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 --001a1140bfb8c87484056771175f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I have been having troubl= e getting a cgo program to run with musl, it has been segfaulting frequentl= y and with 'No stack' when run under gdb.

I have managed to reproduce such a failure in p= ure c with a very small example:
=C2= =A0
```
#include <stdio.h>
#include <stdlib.h>
#include= <getopt.h>
=C2=A0
__attribute__((constructor)) void enter_namespace(int arg= c, char *argv[]) {
=C2=A0 =C2=A0 struct option lo= ng_options[] =3D {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 {&= quot;some-option", required_argument, 0, 's'},
=C2=A0 =C2=A0 =C2=A0 =C2=A0 {0,0,0,0}
= =C2=A0 =C2=A0 };
=C2=A0 =C2=A0 int option_= index, c, pid;
=C2=A0 =C2=A0 while ((c =3D getopt= _long_only(argc, argv, "m:", long_options, &option_index)) != =3D -1) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 switch (c) = {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 case = 's':
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 pid =3D atoi(optarg);
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (pid < 1) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 fprintf(stderr, "Invalid some-option: %s\n", optarg= );
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 exit(1);
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 case 0:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break;<= br style=3D"outline:none">=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 }
}
=C2=A0
int main(void) {
=C2=A0 return 0;
}
```
=C2=A0
Run with or without options will segfault frequently:
```
~ # gcc -g test= .c=C2=A0
~ # ./a.out=C2=A0
Segmentation fault
~ # ./a.out=C2=A0
~ # ./a.out=C2=A0
~ # ./a.out= =C2=A0
~ # ./a.out=C2=A0
Segmentation fault
~ #=C2=A0
~ # 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 i= t.
There is NO WARRANTY, to the extent permitted = by law.=C2=A0 Type "show copying"
and &= quot;show warranty" for details.
This GDB wa= s 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/documentatio= n/>.
For help, type "help".
Type "apropos word" to search for commands = related to "word"...
Reading symbols fr= om ./a.out...done.
(gdb) r
Starting program: /root/a.out=C2=A0
warning: = Error disabling address space randomization: Operation not permitted
During startup program terminated with signal SIGSEGV, = Segmentation fault.
(gdb) bt
No stack.
(gdb)=C2=A0
```
It appears that having any code i= n a constructor function leads to this problem, having the same code in the= =C2=A0main function does not segfault.

I'm not sure how to take this any further without a b= acktrace.

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

Reg= ards,
Bracken Dawson.

:wq
--001a1140bfb8c87484056771175f--