public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "quae at daurnimator dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/66826] New: Unused dlsym return results in segfault
Date: Fri, 10 Jul 2015 06:13:00 -0000	[thread overview]
Message-ID: <bug-66826-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826

            Bug ID: 66826
           Summary: Unused dlsym return results in segfault
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: quae at daurnimator dot com
  Target Milestone: ---

Having a weird issue here: if I dlsym() something in a constructor, but don't
use the result; the shared library segfaults at load.

bug-main.c:

    #define _GNU_SOURCE
    #include <dlfcn.h>

    int main() {
        void *foo = dlopen("./shared.so", RTLD_NOW);
        void (*some_exported_function)() = dlsym(foo,
"some_exported_function");
        some_exported_function();
        return 0;
    }

bug-shared.c:

    #define _GNU_SOURCE
    #include <dlfcn.h>
    #include <stdio.h>

    static int (*pointer) ();
    static int stub () { return 0; }
    __attribute__((constructor)) static void some_init() {
        if ((pointer = dlsym(RTLD_DEFAULT, "anything")) == NULL) {
            pointer = stub;
        }
    }

    void some_exported_function() {
        printf("in some_exported_function\n");
        printf("%p\n", pointer);
    }


Compile with:

    gcc -shared -O2 -fPIC bug-shared.c -ldl -o shared.so
    gcc -O2 -fPIC bug-main.c -ldl


Now comment out the `printf` of the pointer in bug-shared.c; and it'll get a
segfault.

Backtrace:
#0  0x00007ffff7de4b77 in _dl_lookup_symbol_x () from
/lib64/ld-linux-x86-64.so.2
#1  0x00007ffff7951b91 in do_sym () from /usr/lib/libc.so.6
#2  0x00007ffff7bd80f4 in ?? () from /usr/lib/libdl.so.2
#3  0x00007ffff7de9f94 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#4  0x00007ffff7bd85e1 in ?? () from /usr/lib/libdl.so.2
#5  0x00007ffff7bd8148 in dlsym () from /usr/lib/libdl.so.2
#6  0x00007ffff7dea0ea in call_init.part () from /lib64/ld-linux-x86-64.so.2
#7  0x00007ffff7dea1fb in _dl_init () from /lib64/ld-linux-x86-64.so.2
#8  0x00007ffff7dee627 in dl_open_worker () from /lib64/ld-linux-x86-64.so.2
#9  0x00007ffff7de9f94 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#10 0x00007ffff7dede01 in _dl_open () from /lib64/ld-linux-x86-64.so.2
#11 0x00007ffff7bd7fc9 in ?? () from /usr/lib/libdl.so.2
#12 0x00007ffff7de9f94 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#13 0x00007ffff7bd85e1 in ?? () from /usr/lib/libdl.so.2
#14 0x00007ffff7bd8061 in dlopen () from /usr/lib/libdl.so.2
#15 0x0000000000400622 in main ()

$ gcc -dumpversion
5.1.0

Works fine with -O0
Works fine with clang


             reply	other threads:[~2015-07-10  6:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-10  6:13 quae at daurnimator dot com [this message]
2015-07-10 13:11 ` [Bug c/66826] Unused result from dlsym in constructor results in a segfault quae at daurnimator dot com
2015-07-12 14:44 ` quae at daurnimator dot com
2021-09-02  5:37 ` [Bug tree-optimization/66826] " pinskia at gcc dot gnu.org

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=bug-66826-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).