From patchwork Fri Oct 5 14:21:49 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 189489 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 923022C031D for ; Sat, 6 Oct 2012 00:22:06 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1350051726; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Date:From:To:Subject:Message-ID:MIME-Version: Content-Type:Content-Disposition:User-Agent:Mailing-List: Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:Sender:Delivered-To; bh=iKNbDpP/zPrwvAp+0guS0MLff28=; b=YQ5ETQXOEo/di13lCT64b1F5wGjx2XijFzezVD0eHuxpEB7GVcA/vYNxQ6wHpJ C3xRvxdBg/IlhmI8GWupeMSxPZWbzjVy0xrtfWUnKfRST7YoRZKmTmfWaO+WFrqL ON+wSob+/jUPtYCb7+JiEqZxYhKPdeOHZDiI80DYA2GqQ= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Date:From:To:Subject:Message-ID:MIME-Version:Content-Type:Content-Disposition:User-Agent:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=ldegpPd/BYjP3OC49dqgZX3aVgGVI564kHgBZanfW6I9ETSR1V27tTYfx7mvIt rFLdCdikHqUbDUYV6A2Ue4YoOsuA8UCnH1wM9HJM4kSoN1HR49NijgiLwndeyUuy dvcbvv7yjxQnXx3XQqPg7em+qXz4fbdOCSK1tML9ZaQ7M=; Received: (qmail 1082 invoked by alias); 5 Oct 2012 14:21:55 -0000 Received: (qmail 924 invoked by uid 22791); 5 Oct 2012 14:21:55 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_NO X-Spam-Check-By: sourceware.org Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 05 Oct 2012 14:21:51 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 0F9481C7A2A for ; Fri, 5 Oct 2012 10:21:50 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id Uo8TDZH8jZYa for ; Fri, 5 Oct 2012 10:21:49 -0400 (EDT) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id E58281C7994 for ; Fri, 5 Oct 2012 10:21:49 -0400 (EDT) Received: by kwai.gnat.com (Postfix, from userid 4192) id E3DC3919E3; Fri, 5 Oct 2012 10:21:49 -0400 (EDT) Date: Fri, 5 Oct 2012 10:21:49 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Subject: [Ada] Fix race condition in freeing tasks Message-ID: <20121005142149.GA5858@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This patch fixes a subtle race condition where the master task and the child task may free the child task concurrently. Found by code reading. Tested on x86_64-pc-linux-gnu, committed on trunk 2012-10-05 Arnaud Charlet * s-tassta.adb: Update comments. (Vulnerable_Complete_Master): If Free_On_Termination is set, do nothing, and let the task free itself if not already done. Index: s-tassta.adb =================================================================== --- s-tassta.adb (revision 192066) +++ s-tassta.adb (working copy) @@ -1905,7 +1905,16 @@ C := All_Tasks_List; P := null; while C /= null loop - if C.Common.Parent = Self_ID and then C.Master_of_Task >= CM then + -- If Free_On_Termination is set, do nothing here, and let + -- the task free itself if not already done, otherwise we + -- risk a race condition where Vulnerable_Free_Task is called + -- in the loop below, while the task calls Free_Task itself, + -- in Terminate_Task. + + if C.Common.Parent = Self_ID + and then C.Master_of_Task >= CM + and then not C.Free_On_Termination + then if P /= null then P.Common.All_Tasks_Link := C.Common.All_Tasks_Link; else @@ -2088,9 +2097,7 @@ -- is called from Expunge_Unactivated_Tasks. -- For tasks created by elaboration of task object declarations it is - -- called from the finalization code of the Task_Wrapper procedure. It is - -- also called from Ada.Unchecked_Deallocation, for objects that are or - -- contain tasks. + -- called from the finalization code of the Task_Wrapper procedure. procedure Vulnerable_Free_Task (T : Task_Id) is begin