From patchwork Thu Jan 11 05:57:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guo Ren X-Patchwork-Id: 858806 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=uclibc-ng.org (client-ip=2a00:1828:2000:679::23; helo=helium.openadk.org; envelope-from=devel-bounces@uclibc-ng.org; receiver=) Received: from helium.openadk.org (helium.openadk.org [IPv6:2a00:1828:2000:679::23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zHFXk6s6fz9rxj for ; Thu, 11 Jan 2018 16:57:57 +1100 (AEDT) Received: from helium.openadk.org (localhost [IPv6:::1]) by helium.openadk.org (Postfix) with ESMTP id 253421013D; Thu, 11 Jan 2018 06:57:53 +0100 (CET) X-Original-To: devel@uclibc-ng.org Delivered-To: devel@helium.openadk.org Received: from smtp2200-217.mail.aliyun.com (smtp2200-217.mail.aliyun.com [121.197.200.217]) by helium.openadk.org (Postfix) with ESMTPS id 2108A10133; Thu, 11 Jan 2018 06:57:41 +0100 (CET) X-Alimail-AntiSpam: AC=CONTINUE; BC=0.0744509|-1; CH=green; FP=0|0|0|0|0|-1|-1|-1; HT=e01l01425; MF=ren_guo@c-sky.com; NM=1; PH=DS; RN=3; RT=3; SR=0; TI=SMTPD_---.AD-2Ck0_1515650253; Received: from localhost(mailfrom:ren_guo@c-sky.com fp:223.93.147.148) by smtp.aliyun-inc.com(10.147.41.158); Thu, 11 Jan 2018 13:57:33 +0800 From: Guo Ren To: devel@uclibc-ng.org, wbx@uclibc-ng.org Date: Thu, 11 Jan 2018 13:57:32 +0800 Message-Id: <1515650252-2936-1-git-send-email-ren_guo@c-sky.com> X-Mailer: git-send-email 2.7.4 Cc: Guo Ren Subject: [uclibc-ng-devel] [PATCH] libpthread/nptl: bugfix malloc segfault in race conditions. X-BeenThere: devel@uclibc-ng.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: uClibc-ng Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: devel-bounces@uclibc-ng.org Sender: "devel" In libc/sysdeps/linux/common/bits/uClibc_pthread.h: extern void weak_function _pthread_cleanup_push_defer(...) This *weak_function* declaration will cause nptl/cleanup_defer_compat.c: strong_alias (...) !!!FAIL!!!, because it include pthreadP.h->pthread.h ->uClibc_pthread.h That means: Readelf -s libpthread/nptl/cleanup_defer_compat.o you will get: 18: 00000000 198 FUNC WEAK DEFAULT 1 _pthread_cleanup_push_def Readelf -s libc/misc/internals/__uClibc_main.o you will also get: 32: 00000038 58 FUNC WEAK DEFAULT 1 _pthread_cleanup_push_def Final: gcc malloc_pthread_test.c -lpthread The libc/stdlib/malloc-standard/malloc.c:839 (__MALLOC_LOCK-> _pthread_cleanup_push_def) will use the one in __uClibc_main.o !!!not in cleanup_defer_compat.o!!!, becasue two cleanup_defer_compat in libc.a with the same weak declarations and the __uClibc_main.o is close to front. ==== All of malloc/free will failed in muti-threads' race conditions probabilistic. As it happens, uClibc-0.9.33.2 is OK, Becasue: It's seperated in libpthread.a and libc.a, and the libc.a is the last lib for ld internal-cmd, and malloc will get right cleanup_defer_compat from libpthread.a. This BUG is from 2016-09-24 to now: >>> commit 29ff9055c80efe77a7130767a9fcb3ab8c67e8ce Author: Waldemar Brodkorb Date: Sat Sep 24 02:55:31 2016 +0200 use a single libc and deduplicate threading code Similar to musl libc a single libc has many benefits and solves some open issues with uClibc-ng. - no pthread_mutex_* weak symbols exported anymore - applications no longer failing to link when either -lrt or -lpthread are missing for dynamic and static linking mode - smaller C library - slightly better runtime performance <<< Perharps we need carefully check all of the impact about that commit. Signed-off-by: Guo Ren --- libpthread/nptl/sysdeps/pthread/pthread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libpthread/nptl/sysdeps/pthread/pthread.h b/libpthread/nptl/sysdeps/pthread/pthread.h index b2897b3..1fba7fc 100644 --- a/libpthread/nptl/sysdeps/pthread/pthread.h +++ b/libpthread/nptl/sysdeps/pthread/pthread.h @@ -29,7 +29,7 @@ #include #include #include -#if defined _LIBC && ( defined IS_IN_libc || defined NOT_IN_libc ) +#if defined _LIBC && ( defined IS_IN_libc || !defined NOT_IN_libc ) #include #endif