{"action":"opened","issue":{"url":"https://api.github.com/repos/jemalloc/jemalloc/issues/443","repository_url":"https://api.github.com/repos/jemalloc/jemalloc","labels_url":"https://api.github.com/repos/jemalloc/jemalloc/issues/443/labels{/name}","comments_url":"https://api.github.com/repos/jemalloc/jemalloc/issues/443/comments","events_url":"https://api.github.com/repos/jemalloc/jemalloc/issues/443/events","html_url":"https://github.com/jemalloc/jemalloc/issues/443","id":170783160,"number":443,"title":"Deadlock when using LD_PRELOAD libraries that hook the open system call","user":{"login":"kspinka","id":1490618,"avatar_url":"https://avatars.githubusercontent.com/u/1490618?v=3","gravatar_id":"","url":"https://api.github.com/users/kspinka","html_url":"https://github.com/kspinka","followers_url":"https://api.github.com/users/kspinka/followers","following_url":"https://api.github.com/users/kspinka/following{/other_user}","gists_url":"https://api.github.com/users/kspinka/gists{/gist_id}","starred_url":"https://api.github.com/users/kspinka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kspinka/subscriptions","organizations_url":"https://api.github.com/users/kspinka/orgs","repos_url":"https://api.github.com/users/kspinka/repos","events_url":"https://api.github.com/users/kspinka/events{/privacy}","received_events_url":"https://api.github.com/users/kspinka/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-08-12T01:35:39Z","updated_at":"2016-08-12T01:35:39Z","closed_at":null,"body":"In commit: c2f970c32b527660a33fa513a76d913c812dcf7c 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.\r\n\r\nPlease consider acquiring the lock after querying the commit policy via /proc.\r\n\r\n```c\r\n#ifdef JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY\r\nstatic bool\r\nos_overcommits_proc(void)\r\n{\r\n\tint fd;\r\n\tchar buf[1];\r\n\tssize_t nread;\r\n\r\n\tfd = open(\"/proc/sys/vm/overcommit_memory\", O_RDONLY);\r\n\tif (fd == -1)\r\n\t\treturn (false); /* Error. */\r\n\r\n\tnread = read(fd, &buf, sizeof(buf));\r\n\tif (nread < 1)\r\n\t\treturn (false); /* Error. */\r\n\t/*\r\n\t * /proc/sys/vm/overcommit_memory meanings:\r\n\t * 0: Heuristic overcommit.\r\n\t * 1: Always overcommit.\r\n\t * 2: Never overcommit.\r\n\t */\r\n\treturn (buf[0] == '0' || buf[0] == '1');\r\n}\r\n#endif\r\n```\r\n\r\nExample:\r\n<pre>\r\n__lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135\r\n135\t../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S: No such file or directory.\r\n(gdb) where\r\n#0  __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135\r\n#1  0x00007ffff7727f3e in __GI___pthread_mutex_lock (mutex=mutex@entry=0x1680680 <init_lock>) at ../nptl/pthread_mutex_lock.c:135\r\n#2  0x0000000000f86940 in je_malloc_mutex_lock (tsdn=0x0, mutex=0x1680680 <init_lock>) at include/jemalloc/internal/mutex.h:97\r\n#3  malloc_init_hard () at src/jemalloc.c:1315\r\n<b>#4  0x0000000000f8a60d in malloc_init () at src/jemalloc.c:291</b>\r\n#5  ialloc_body (slow_path=true, usize=<synthetic pointer>, tsdn=<synthetic pointer>, zero=true, size=<optimized out>) at src/jemalloc.c:1413\r\n#6  calloc (num=num@entry=1, size=size@entry=32) at src/jemalloc.c:1648\r\n#7  0x00007ffff751b627 in _dlerror_run (operate=operate@entry=0x7ffff751b020 <dlsym_doit>, args=args@entry=0x7fffffffe660) at dlerror.c:141\r\n#8  0x00007ffff751b088 in __dlsym (handle=handle@entry=0xffffffffffffffff, name=name@entry=0x7ffff7bbbd05 \"socket\") at dlsym.c:70\r\n#9  0x00007ffff7b4b877 in citp_find_all_sys_calls () at ../../../../../src/include/onload/declare_syscalls.h.tmpl:40\r\n#10 citp_syscall_init () at ../../../../../src/lib/transport/unix/sys.c:103\r\n#11 0x00007ffff7b4b24c in citp_do_init (max_init_level=max_init_level@entry=2) at ../../../../../src/lib/transport/unix/startup.c:650\r\n#12 0x00007ffff7b516a2 in onload_open (pathname=pathname@entry=0x1257458 \"/proc/sys/vm/overcommit_memory\", flags=flags@entry=0)\r\n    at ../../../../../src/lib/transport/unix/sockcall_intercept.c:2092\r\n#13 0x0000000000fad5fa in open (__oflag=0, __path=0x1257458 \"/proc/sys/vm/overcommit_memory\") at /usr/include/x86_64-linux-gnu/bits/fcntl2.h:53\r\n#14 os_overcommits_proc () at src/pages.c:217\r\n#15 je_pages_boot () at src/pages.c:245\r\n#16 0x0000000000f86852 in malloc_init_hard_a0_locked () at src/jemalloc.c:1194\r\n#17 0x0000000000f869b4 in malloc_init_hard () at src/jemalloc.c:1322\r\n<b>#18 0x0000000000f8a60d in malloc_init () at src/jemalloc.c:291</b>\r\n#19 ialloc_body (slow_path=true, usize=<synthetic pointer>, tsdn=<synthetic pointer>, zero=true, size=<optimized out>) at src/jemalloc.c:1413\r\n#20 calloc (num=num@entry=1, size=size@entry=32) at src/jemalloc.c:1648\r\n#21 0x00007ffff751b627 in _dlerror_run (operate=operate@entry=0x7ffff751b020 <dlsym_doit>, args=args@entry=0x7fffffffe840) at dlerror.c:141\r\n#22 0x00007ffff751b088 in __dlsym (handle=handle@entry=0xffffffffffffffff, name=name@entry=0x7ffff7bbbd05 \"socket\") at dlsym.c:70\r\n#23 0x00007ffff7b4b877 in citp_find_all_sys_calls () at ../../../../../src/include/onload/declare_syscalls.h.tmpl:40\r\n#24 citp_syscall_init () at ../../../../../src/lib/transport/unix/sys.c:103\r\n#25 0x00007ffff7b4b24c in citp_do_init (max_init_level=max_init_level@entry=10) at ../../../../../src/lib/transport/unix/startup.c:650\r\n#26 0x00007ffff7b4b3de in _init () at ../../../../../src/lib/transport/unix/startup.c:666\r\n#27 0x00007ffff7de74aa in call_init (l=0x7ffff7ff7620, argc=argc@entry=1, argv=argv@entry=0x7fffffffe948, env=env@entry=0x7fffffffe958) at dl-init.c:58\r\n#28 0x00007ffff7de75fb in call_init (env=0x7fffffffe958, argv=0x7fffffffe948, argc=1, l=<optimized out>) at dl-init.c:30\r\n#29 _dl_init (main_map=0x7ffff7ffe168, argc=1, argv=0x7fffffffe948, env=0x7fffffffe958) at dl-init.c:120\r\n#30 0x00007ffff7dd7cfa in _dl_start_user () from /lib64/ld-linux-x86-64.so.2\r\n#31 0x0000000000000001 in ?? ()\r\n#32 0x00007fffffffeb7d in ?? ()\r\n#33 0x0000000000000000 in ?? ()\r\n</pre>"}}
{"actor":{"display_login":"kspinka"}}