Skip to content

Deadlock when using LD_PRELOAD libraries that hook the open() system call #443

Closed

Description

@kspinka

In commit: c2f970c pages.c introduces a conditionally compiled os_overcommits_proc() function that opens a /proc file to query the overcommit policy. The trouble is that this is done holding the malloc init lock. If you have an LD_PRELOAD library in place, or anything that needs to init (and expects to be able to use malloc) in order to hook the open() system call, you will end up with a recursive acquire of the malloc init lock.

Please consider acquiring the lock after querying the commit policy via /proc.

#ifdef JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY
static bool
os_overcommits_proc(void)
{
    int fd;
    char buf[1];
    ssize_t nread;

    fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY);
    if (fd == -1)
        return (false); /* Error. */

    nread = read(fd, &buf, sizeof(buf));
    if (nread < 1)
        return (false); /* Error. */
    /*
     * /proc/sys/vm/overcommit_memory meanings:
     * 0: Heuristic overcommit.
     * 1: Always overcommit.
     * 2: Never overcommit.
     */
    return (buf[0] == '0' || buf[0] == '1');
}
#endif

Example:

__lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
135 ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S: No such file or directory.
(gdb) where
#0  __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1  0x00007ffff7727f3e in __GI___pthread_mutex_lock (mutex=mutex@entry=0x1680680 ) at ../nptl/pthread_mutex_lock.c:135
#2  0x0000000000f86940 in je_malloc_mutex_lock (tsdn=0x0, mutex=0x1680680 ) at include/jemalloc/internal/mutex.h:97
#3  malloc_init_hard () at src/jemalloc.c:1315
#4  0x0000000000f8a60d in malloc_init () at src/jemalloc.c:291
#5  ialloc_body (slow_path=true, usize=, tsdn=, zero=true, size=) at src/jemalloc.c:1413
#6  calloc (num=num@entry=1, size=size@entry=32) at src/jemalloc.c:1648
#7  0x00007ffff751b627 in _dlerror_run (operate=operate@entry=0x7ffff751b020 , args=args@entry=0x7fffffffe660) at dlerror.c:141
#8  0x00007ffff751b088 in __dlsym (handle=handle@entry=0xffffffffffffffff, name=name@entry=0x7ffff7bbbd05 "socket") at dlsym.c:70
#9  0x00007ffff7b4b877 in citp_find_all_sys_calls () at ../../../../../src/include/onload/declare_syscalls.h.tmpl:40
#10 citp_syscall_init () at ../../../../../src/lib/transport/unix/sys.c:103
#11 0x00007ffff7b4b24c in citp_do_init (max_init_level=max_init_level@entry=2) at ../../../../../src/lib/transport/unix/startup.c:650
#12 0x00007ffff7b516a2 in onload_open (pathname=pathname@entry=0x1257458 "/proc/sys/vm/overcommit_memory", flags=flags@entry=0)
    at ../../../../../src/lib/transport/unix/sockcall_intercept.c:2092
#13 0x0000000000fad5fa in open (__oflag=0, __path=0x1257458 "/proc/sys/vm/overcommit_memory") at /usr/include/x86_64-linux-gnu/bits/fcntl2.h:53
#14 os_overcommits_proc () at src/pages.c:217
#15 je_pages_boot () at src/pages.c:245
#16 0x0000000000f86852 in malloc_init_hard_a0_locked () at src/jemalloc.c:1194
#17 0x0000000000f869b4 in malloc_init_hard () at src/jemalloc.c:1322
#18 0x0000000000f8a60d in malloc_init () at src/jemalloc.c:291
#19 ialloc_body (slow_path=true, usize=, tsdn=, zero=true, size=) at src/jemalloc.c:1413
#20 calloc (num=num@entry=1, size=size@entry=32) at src/jemalloc.c:1648
#21 0x00007ffff751b627 in _dlerror_run (operate=operate@entry=0x7ffff751b020 , args=args@entry=0x7fffffffe840) at dlerror.c:141
#22 0x00007ffff751b088 in __dlsym (handle=handle@entry=0xffffffffffffffff, name=name@entry=0x7ffff7bbbd05 "socket") at dlsym.c:70
#23 0x00007ffff7b4b877 in citp_find_all_sys_calls () at ../../../../../src/include/onload/declare_syscalls.h.tmpl:40
#24 citp_syscall_init () at ../../../../../src/lib/transport/unix/sys.c:103
#25 0x00007ffff7b4b24c in citp_do_init (max_init_level=max_init_level@entry=10) at ../../../../../src/lib/transport/unix/startup.c:650
#26 0x00007ffff7b4b3de in _init () at ../../../../../src/lib/transport/unix/startup.c:666
#27 0x00007ffff7de74aa in call_init (l=0x7ffff7ff7620, argc=argc@entry=1, argv=argv@entry=0x7fffffffe948, env=env@entry=0x7fffffffe958) at dl-init.c:58
#28 0x00007ffff7de75fb in call_init (env=0x7fffffffe958, argv=0x7fffffffe948, argc=1, l=) at dl-init.c:30
#29 _dl_init (main_map=0x7ffff7ffe168, argc=1, argv=0x7fffffffe948, env=0x7fffffffe958) at dl-init.c:120
#30 0x00007ffff7dd7cfa in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#31 0x0000000000000001 in ?? ()
#32 0x00007fffffffeb7d in ?? ()
#33 0x0000000000000000 in ?? ()

Activity

  1. changed the title [-]Deadlock when using LD_PRELOAD libraries that hook the open system call[/-] [+]Deadlock when using LD_PRELOAD libraries that hook the open() system call[/+] on Aug 12, 2016
  2. kspinka commented on Aug 12, 2016

    @kspinka
    Author

    Indexing tags: futex, hang, preload.

  3. added a commit that references this issue on Aug 14, 2016
    dc328e7
  4. Whissi commented on Aug 23, 2016

    @Whissi

    @kspinka: I tried your patch however jemalloc test suite doesn't pass... no error but test suite hangs (the test suite runs in the same sandbox where I experienced the problem first). So it doesn't looked fixed for me. Last test lines:

    === test/unit/tsd ===
    test_tsd_main_thread: pass
    test_tsd_sub_thread: pass
    --- pass: 2/2, skip: 0/2, fail: 0/2 ---
    
    === test/unit/util ===
    test_pow2_ceil_u64: pass
    test_pow2_ceil_u32: pass
    test_pow2_ceil_zu: pass
    test_malloc_strtoumax_no_endptr: pass
    test_malloc_strtoumax: pass
    test_malloc_snprintf_truncated: pass
    test_malloc_snprintf: pass
    --- pass: 7/7, skip: 0/7, fail: 0/7 ---
    
    === test/unit/witness ===
    test_witness:test/unit/witness.c:65: Test skipped: (!config_debug)
    test_witness: skip
    test_witness_comp:test/unit/witness.c:93: Test skipped: (!config_debug)
    test_witness_comp: skip
    test_witness_reversal:test/unit/witness.c:143: Test skipped: (!config_debug)
    test_witness_reversal: skip
    test_witness_recursive:test/unit/witness.c:175: Test skipped: (!config_debug)
    test_witness_recursive: skip
    test_witness_unlock_not_owned:test/unit/witness.c:213: Test skipped: (!config_debug)
    test_witness_unlock_not_owned: skip
    test_witness_lockful:test/unit/witness.c:240: Test skipped: (!config_debug)
    test_witness_lockful: skip
    --- pass: 0/6, skip: 6/6, fail: 0/6 ---
    
    === test/unit/zero ===
    test_zero_small: pass
    test_zero_large: pass
    test_zero_huge: pass
    --- pass: 3/3, skip: 0/3, fail: 0/3 ---
    
    Test suite summary: pass: 30/40, skip: 10/40, fail: 0/40
    
  5. kspinka commented on Aug 24, 2016

    @kspinka
    Author

    @Whissi, just to be sure we're dealing with the same issue, try building without my patch, but with JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY undefined to omit the section of code I referenced above.

  6. Whissi commented on Aug 24, 2016

    @Whissi

    I patched configure.ac like

    diff --git a/configure.ac b/configure.ac
    index 7f19715..fdfe024 100644
    --- a/configure.ac
    +++ b/configure.ac
    @@ -330,7 +330,6 @@ case "${host}" in
            CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
            abi="elf"
            AC_DEFINE([JEMALLOC_HAS_ALLOCA_H])
    -       AC_DEFINE([JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY], [ ])
            AC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED], [ ])
            AC_DEFINE([JEMALLOC_THREADED_INIT], [ ])
            AC_DEFINE([JEMALLOC_USE_CXX_THROW], [ ])

    and re-generated configure. Now jemalloc-4.2.1 compiles and passes test suite. If JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY is defined it doesn't pass.

    So yes, I think we are dealing with the same issue, not?

  7. vsarunas commented on Aug 30, 2016

    @vsarunas

    We have solved the same Onload issue by using syscall() instead of open() to get overcommit_memory policy.

  8. Whissi commented on Aug 30, 2016

    @Whissi

    @vsarunas: Who is "we"? A downstream project (a software project which uses jemalloc)? Is the source available? Can you link the commit which added the workaround for you?

  9. vsarunas commented on Aug 30, 2016

    @vsarunas
    -   fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY);
    +   fd = syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY);
        if (fd == -1)
            return (false); /* Error. */
    
    -   nread = read(fd, &buf, sizeof(buf));
    +   nread = syscall(SYS_read, fd, &buf, sizeof(buf));
    +   syscall(SYS_close, fd);
  10. Whissi commented on Aug 30, 2016

    @Whissi

    Confirmed, thanks for sharing! With

    diff --git a/src/pages.c b/src/pages.c
    index 2a9b7e3..a33e7d7 100644
    --- a/src/pages.c
    +++ b/src/pages.c
    @@ -214,11 +214,14 @@ os_overcommits_proc(void)
            char buf[1];
            ssize_t nread;
    
    -       fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY);
    +       fd = syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY);
            if (fd == -1)
                    return (false); /* Error. */
    
    -       nread = read(fd, &buf, sizeof(buf));
    +       nread = syscall(SYS_read, fd, &buf, sizeof(buf));
    +
    +       syscall(SYS_close, fd);
    +
            if (nread < 1)
                    return (false); /* Error. */
            /*

    jemalloc-4.2.1 passes its own test suite and jemalloc consumsers like rsyslog or percona-server no longer show the problem reported in #430 when running using sandbox.

  11. jasone commented on Sep 7, 2016

    @jasone
    Member

    I see this as a bootstrapping issue in the code that hooks open(), rather than something that the memory allocator should accommodate. As mentioned elsewhere, I've seen this same issue for hooked mmap(). It's a constant struggle to keep jemalloc bootstrapping operational without general support for reentrant allocation, but thus far the extra cost of a general mechanism does not appear to be warranted.

  12. added this to the 4.3.0 milestone on Oct 28, 2016
  13. jasone commented on Oct 28, 2016

    @jasone
    Member

    @kspinka convinced me to reopen this issue. I'm planning to integrate a patch based on the patch @Whissi pasted (needs an explanatory comment, as well as a conditional #include <sys/syscall.h>). The broader problem of supporting other libraries hooking arbitrary syscalls (especially mmap()) remains something I have no enthusiasm for.

  14. reopened this on Oct 29, 2016
  15. sjnewbury commented on Oct 29, 2016

    @sjnewbury

    The patch on here is sufficient for the portage sandbox, but sandboxes are in general becoming increasingly important, a general solution is probably warranted.

  16. added a commit that references this issue on Oct 30, 2016
    c443b67
  17. leelamv commented on Feb 25, 2017

    @leelamv

    I could workaround this problem by calling _dl_sym (libc private function) instead of dlsym().

    So, why can't we use "dlsym" ?
    libdl's dlsym() allocate memory for its internal purposes(actually, for dlerror support). Allocation using malloc/calloc interfaces can put us into deadlock when trying to resolve the libc functions.

    Use of "_dl_sym" and other details:
    a) "_dl_sym" is unpublished libc function which is functionally equivalent to libdl's dlsym.
    b) Internally, dlsym calls libc's "_dl_sym" but with below benefits implemented in the libdl layer.

    • Protection against concurrent loads and unloads.
    • dlerror support
      c) It has a third argument which is return address of the caller.
      d) libraries using GLIBC_PRIVATE will run into rpm packaging issues if you don't relax those checks.

    When is it okay to relax on the benefits and use _dl_sym.
    a) Your library should be loaded as a LD_PRELOAD or linked as one of the first libraries.

    • That is, your library should be loaded only when program is in single-threaded env.
      b) These are known/published functions which are expected to be found.
    • Remember, we don't have dlerror support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions