From patchwork Tue May 22 16:57:41 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 160690 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 F0623B6FA4 for ; Wed, 23 May 2012 02:58:09 +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=1338310690; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:From:To:Subject:Date:Message-ID:User-Agent: MIME-Version:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=tctdBcMrcbhOtWCU3ysJGSKt1Ho=; b=QjLbNlxnM2VgQQq 2JLY1ukVTWMVuZXko1Os5F42y9MzpVqdrGOeqolQzFHDTYSep70frOdIDDXxdBcO U0QvGWCSTak8zS36OYGkv6HR8YOOPwYi4ij5c243gDM0S1dAQNOvZvZBsO4C7h0b ZE2P7Hmi+E9Lhb6NgQuCgpX2UkFU= 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:X-Google-DKIM-Signature:Received:Received:Received:From:To:Subject:Date:Message-ID:User-Agent:MIME-Version:Content-Type:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=soj6mhKgX9OVSm8GkFp025czzOMKaUf7uVvW8VpCKAyu4i3uBObBXy9sCBD4tK k3yxM/eUd/1FCX0V0/lA3wLNaenKC+wAonUBohxBGcF/KkoViHjEl4Nb6vtfDgZ+ V63IksG2mgezJMqJnpF+IEs9abboeJ2QylH3z07BTNqAA=; Received: (qmail 19647 invoked by alias); 22 May 2012 16:58:01 -0000 Received: (qmail 19615 invoked by uid 22791); 22 May 2012 16:57:58 -0000 X-SWARE-Spam-Status: No, hits=-4.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, TW_TJ, T_RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from mail-pb0-f47.google.com (HELO mail-pb0-f47.google.com) (209.85.160.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 22 May 2012 16:57:45 +0000 Received: by pbbrq2 with SMTP id rq2so8475073pbb.20 for ; Tue, 22 May 2012 09:57:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id:user-agent:mime-version :content-type:x-gm-message-state; bh=oq0fdYVhl8tZBcBen2MELb6L7zFLGI9r6keP0pl2Sdo=; b=CaWmVagE6OonNtBdanokgWZqkzfjMOe5/JN08pT22mzM5k/rudhW5cP8xZwCAkNmsd BuVRkQilL5ONmuvsqXQPs6NgDqVUkjW1dnNMqeZCGjRor+MnZpSfz9YTx362e1nI6jKs 3wpzyKf4gI3UEDzh0Nzpb8cm47ODMtGEXfG3VmOqNrrC5vBp1WFJPwKgv4LBjjoOT2cy r7z7T1FR29kSMKqXVEzD2KNZSu1bt3ZZ7AxcEubcuqol+AZVOiGe4uHINtn/ARAxLukK cYZ0XZPGvnSqRzf9cb8cpCvQh0vfDbzNATtpuPgnwLTKfVsC6dAxnd+FlN3L+mY0gMiR Bprg== Received: by 10.68.197.166 with SMTP id iv6mr697656pbc.40.1337705864479; Tue, 22 May 2012 09:57:44 -0700 (PDT) Received: by 10.68.197.166 with SMTP id iv6mr697636pbc.40.1337705864348; Tue, 22 May 2012 09:57:44 -0700 (PDT) Received: from coign.google.com ([2620:0:1000:fd2a:224:d7ff:fe8f:f634]) by mx.google.com with ESMTPS id gq5sm25452805pbc.65.2012.05.22.09.57.42 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 22 May 2012 09:57:42 -0700 (PDT) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: libgo patch committed: Use getcontext, not setjmp, to save regs for GC Date: Tue, 22 May 2012 09:57:41 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-Gm-Message-State: ALoCoQmCHZb8KrQX8gn5/uPmvI2ASzpUrm/lW0VhLq1TT6Gyxn6j8VRRqWt4OqXvYBSZQdAi1tXNmT6HjI7gn7/Zl4iH9kJHAXMmOM43LGlwOVSL4EH58CIahrTBzt9ydLVm046HzyRdiUtOFbLbDNJXrog/zcKGig== X-IsSubscribed: yes 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 When starting a system call, the libgo library needs to save the registers in a place where the garbage collector can see them, so that if the registers happen to be pointers the objects to which they point will not be collected. I thought that I could simply use setjmp for that. Unfortunately, I just managed to uncover the fact that setjmp mangles the register values, so that the garbage collector does not understand them. This means that a particularly poorly timed garbage collection can discard values used by a system call. This patch changes libgo to use getcontext as well, which should work at least until getcontext also gets pointer mangling. It appears that in the long run we will need processor-specific context save and restore routines in libgo. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline and 4.7 branch. Ian diff -r edb476bbcabd libgo/runtime/proc.c --- a/libgo/runtime/proc.c Fri May 18 13:14:19 2012 -0700 +++ b/libgo/runtime/proc.c Tue May 22 09:51:13 2012 -0700 @@ -1239,9 +1239,7 @@ // Save the registers in the g structure so that any pointers // held in registers will be seen by the garbage collector. - // We could use getcontext here, but setjmp is more efficient - // because it doesn't need to save the signal mask. - setjmp(g->gcregs); + getcontext(&g->gcregs); g->status = Gsyscall; @@ -1299,7 +1297,7 @@ gp->gcstack = nil; #endif gp->gcnext_sp = nil; - runtime_memclr(gp->gcregs, sizeof gp->gcregs); + runtime_memclr(&gp->gcregs, sizeof gp->gcregs); if(m->profilehz > 0) runtime_setprof(true); @@ -1328,7 +1326,7 @@ gp->gcstack = nil; #endif gp->gcnext_sp = nil; - runtime_memclr(gp->gcregs, sizeof gp->gcregs); + runtime_memclr(&gp->gcregs, sizeof gp->gcregs); } // Allocate a new g, with a stack big enough for stacksize bytes. diff -r edb476bbcabd libgo/runtime/runtime.h --- a/libgo/runtime/runtime.h Fri May 18 13:14:19 2012 -0700 +++ b/libgo/runtime/runtime.h Tue May 22 09:51:13 2012 -0700 @@ -7,7 +7,6 @@ #include "config.h" #include "go-assert.h" -#include #include #include #include @@ -128,7 +127,7 @@ void* gcnext_segment; void* gcnext_sp; void* gcinitial_sp; - jmp_buf gcregs; + ucontext_t gcregs; byte* entry; // initial function G* alllink; // on allg void* param; // passed parameter on wakeup