From owner-freebsd-bugs@FreeBSD.ORG Thu Apr 10 15:10:01 2014 Return-Path: Delivered-To: freebsd-bugs@smarthost.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id CE1EA967 for ; Thu, 10 Apr 2014 15:10:01 +0000 (UTC) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:1900:2254:206c::16:87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id AADA11D35 for ; Thu, 10 Apr 2014 15:10:01 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.8/8.14.8) with ESMTP id s3AFA1lR016301 for ; Thu, 10 Apr 2014 15:10:01 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.8/8.14.8/Submit) id s3AFA1dB016300; Thu, 10 Apr 2014 15:10:01 GMT (envelope-from gnats) Resent-Date: Thu, 10 Apr 2014 15:10:01 GMT Resent-Message-Id: <201404101510.s3AFA1dB016300@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Alex Crichton Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 75EDE8C4 for ; Thu, 10 Apr 2014 15:07:05 +0000 (UTC) Received: from cgiserv.freebsd.org (cgiserv.freebsd.org [IPv6:2001:1900:2254:206a::50:4]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 48C241D10 for ; Thu, 10 Apr 2014 15:07:05 +0000 (UTC) Received: from cgiserv.freebsd.org ([127.0.1.6]) by cgiserv.freebsd.org (8.14.8/8.14.8) with ESMTP id s3AF74cP041673 for ; Thu, 10 Apr 2014 15:07:04 GMT (envelope-from nobody@cgiserv.freebsd.org) Received: (from nobody@localhost) by cgiserv.freebsd.org (8.14.8/8.14.8/Submit) id s3AF7476041659; Thu, 10 Apr 2014 15:07:04 GMT (envelope-from nobody) Message-Id: <201404101507.s3AF7476041659@cgiserv.freebsd.org> Date: Thu, 10 Apr 2014 15:07:04 GMT From: Alex Crichton To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Subject: misc/188425: Frequent segfault with multithreaded fork X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Apr 2014 15:10:02 -0000 >Number: 188425 >Category: misc >Synopsis: Frequent segfault with multithreaded fork >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Thu Apr 10 15:10:01 UTC 2014 >Closed-Date: >Last-Modified: >Originator: Alex Crichton >Release: 10.0-RELEASE-p1 >Organization: Mozilla >Environment: FreeBSD freebsd-vm 10.0-RELEASE-p1 FreeBSD 10.0-RELEASE-p1 #0: Tue Apr 8 06:45:06 UTC 2014 root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC amd64 >Description: Recently we've found that FreeBSD is segfaulting or hanging frequently when dealing with what we suspect is a multithreaded forking situation. I've written a small program (attached) which exhibits the bug. This program will segfault or trip an assertion in libthr frequently, but not always. >How-To-Repeat: 1. Download attached bug.c 2. clang -o bug bug.c -lthr 3. Run the program repeatedly until it segfaults, normally requiring less than 500 runs. When running the program, I've been using this script: for i in {1..1000}; do; echo $i; ./bug || break; done >Fix: Patch attached with submission follows: #include #include #include #include #include #include #include static pthread_mutex_t lock; static pthread_cond_t cond; static volatile int cnt = 0; static void test() { int p = fork(); assert(p >= 0); if (p == 0) { _exit(0); } int a = 1; assert(waitpid(p, &a, 0) == p); assert(WEXITSTATUS(a) == 0); } static void *child(void *arg) { test(); assert(pthread_mutex_lock(&lock) == 0); cnt -= 1; if (cnt == 0) { assert(pthread_cond_signal(&cond) == 0); } assert(pthread_mutex_unlock(&lock) == 0); return arg; } int main() { assert(pthread_mutex_init(&lock, NULL) == 0); assert(pthread_cond_init(&cond, NULL) == 0); pthread_t c1, c2; cnt = 2; assert(pthread_create(&c1, NULL, child, NULL) == 0); assert(pthread_create(&c2, NULL, child, NULL) == 0); assert(pthread_mutex_lock(&lock) == 0); while (cnt != 0) { assert(pthread_cond_wait(&cond, &lock) == 0); } assert(pthread_mutex_unlock(&lock) == 0); pthread_join(c1, NULL); pthread_join(c2, NULL); } >Release-Note: >Audit-Trail: >Unformatted: