From patchwork Mon Aug 3 10:00:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1340259 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=BXl7B+ge; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BKtgX3mLsz9sR4 for ; Mon, 3 Aug 2020 20:00:55 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D57893857C54; Mon, 3 Aug 2020 10:00:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D57893857C54 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1596448851; bh=jwc/34dR/lDYQnkzzSy/wUVSGM5qoceqMVuWiUySu3E=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=BXl7B+geGX0dK/Hg5HSjO0CdRx/LGj+v3K9G2IPG9HIEcQFHUISCnHkizOsQOsZ1b sfvWKKmXx3P42jlIFGh1q2t1qZ/s+UdSOXeXf2UAGNZ/B1BT/7Rjt1PHwmG7jPXjwS KmBEwNKrGkWvi/yPycasMtdPZeQHQxAt1H5EqgpE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [IPv6:2001:67c:2050::465:102]) by sourceware.org (Postfix) with ESMTPS id 5A3023858D35 for ; Mon, 3 Aug 2020 10:00:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5A3023858D35 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4BKtgK6JcFzKmbb; Mon, 3 Aug 2020 12:00:45 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter05.heinlein-hosting.de (spamfilter05.heinlein-hosting.de [80.241.56.123]) (amavisd-new, port 10030) with ESMTP id AQpPby2-kc5y; Mon, 3 Aug 2020 12:00:42 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Fix ICE using non-local variable: internal compiler error: Segmentation fault Date: Mon, 3 Aug 2020 12:00:40 +0200 Message-Id: <20200803100040.1317013-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: 1 X-Rspamd-Score: 0.21 / 15.00 / 15.00 X-Rspamd-Queue-Id: 72A4717B0 X-Rspamd-UID: aa97b0 X-Spam-Status: No, score=-16.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi, This patch moves no frame access error to own function, adding use of it for both when get_framedecl() cannot find a path to the outer function frame, and guarding get_decl_tree() from recursively calling itself. Bootstrapped and regression tested on x86_64-linux-gnu with multilib configurations -m32 and -mx32. Committed to mainline. Regards Iain --- gcc/d/ChangeLog: PR d/96254 * d-codegen.cc (error_no_frame_access): New. (get_frame_for_symbol): Use fdparent name in error message. (get_framedecl): Replace call to assert with error. * d-tree.h (error_no_frame_access): Declare. * decl.cc (get_decl_tree): Detect recursion and error. gcc/testsuite/ChangeLog: PR d/96254 * gdc.dg/pr96254a.d: New test. * gdc.dg/pr96254b.d: New test. --- gcc/d/d-codegen.cc | 53 +++++++++++++++++++-------------- gcc/d/d-tree.h | 1 + gcc/d/decl.cc | 5 ++++ gcc/testsuite/gdc.dg/pr96254a.d | 28 +++++++++++++++++ gcc/testsuite/gdc.dg/pr96254b.d | 24 +++++++++++++++ 5 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/pr96254a.d create mode 100644 gcc/testsuite/gdc.dg/pr96254b.d diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc index 2dce09d7187..296d62a91a4 100644 --- a/gcc/d/d-codegen.cc +++ b/gcc/d/d-codegen.cc @@ -2127,6 +2127,17 @@ build_vthis_function (tree basetype, tree type) return fntype; } +/* Raise an error at that the context pointer of the function or object SYM is + not accessible from the current scope. */ + +tree +error_no_frame_access (Dsymbol *sym) +{ + error_at (input_location, "cannot get frame pointer to %qs", + sym->toPrettyChars ()); + return null_pointer_node; +} + /* If SYM is a nested function, return the static chain to be used when calling that function from the current function. @@ -2191,7 +2202,7 @@ get_frame_for_symbol (Dsymbol *sym) { error_at (make_location_t (sym->loc), "%qs is a nested function and cannot be accessed from %qs", - fd->toPrettyChars (), thisfd->toPrettyChars ()); + fdparent->toPrettyChars (), thisfd->toPrettyChars ()); return null_pointer_node; } @@ -2202,39 +2213,35 @@ get_frame_for_symbol (Dsymbol *sym) while (fd != dsym) { /* Check if enclosing function is a function. */ - FuncDeclaration *fd = dsym->isFuncDeclaration (); + FuncDeclaration *fdp = dsym->isFuncDeclaration (); + Dsymbol *parent = dsym->toParent2 (); - if (fd != NULL) + if (fdp != NULL) { - if (fdparent == fd->toParent2 ()) + if (fdparent == parent) break; - gcc_assert (fd->isNested () || fd->vthis); - dsym = dsym->toParent2 (); + gcc_assert (fdp->isNested () || fdp->vthis); + dsym = parent; continue; } /* Check if enclosed by an aggregate. That means the current function must be a member function of that aggregate. */ - AggregateDeclaration *ad = dsym->isAggregateDeclaration (); + AggregateDeclaration *adp = dsym->isAggregateDeclaration (); - if (ad == NULL) - goto Lnoframe; - if (ad->isClassDeclaration () && fdparent == ad->toParent2 ()) - break; - if (ad->isStructDeclaration () && fdparent == ad->toParent2 ()) - break; - - if (!ad->isNested () || !ad->vthis) + if (adp != NULL) { - Lnoframe: - error_at (make_location_t (thisfd->loc), - "cannot get frame pointer to %qs", - sym->toPrettyChars ()); - return null_pointer_node; + if ((adp->isClassDeclaration () || adp->isStructDeclaration ()) + && fdparent == parent) + break; } - dsym = dsym->toParent2 (); + /* No frame to outer function found. */ + if (!adp || !adp->isNested () || !adp->vthis) + return error_no_frame_access (sym); + + dsym = parent; } } @@ -2724,8 +2731,10 @@ get_framedecl (FuncDeclaration *inner, FuncDeclaration *outer) break; } + if (fd != outer) + return error_no_frame_access (outer); + /* Go get our frame record. */ - gcc_assert (fd == outer); tree frame_type = FRAMEINFO_TYPE (get_frameinfo (outer)); if (frame_type != NULL_TREE) diff --git a/gcc/d/d-tree.h b/gcc/d/d-tree.h index df317d557eb..2be80dd1867 100644 --- a/gcc/d/d-tree.h +++ b/gcc/d/d-tree.h @@ -575,6 +575,7 @@ extern tree d_build_call (TypeFunction *, tree, tree, Expressions *); extern tree d_assert_call (const Loc &, libcall_fn, tree = NULL_TREE); extern tree build_float_modulus (tree, tree, tree); extern tree build_vthis_function (tree, tree); +extern tree error_no_frame_access (Dsymbol *); extern tree get_frame_for_symbol (Dsymbol *); extern tree build_vthis (AggregateDeclaration *); extern void build_closure (FuncDeclaration *); diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index 15eb9a43399..72c8a8cff06 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -1480,6 +1480,11 @@ get_decl_tree (Declaration *decl) AggregateDeclaration *ad = fd->isThis (); gcc_assert (ad != NULL); + /* The parent function is for the same `this' declaration we are + building a chain to. Non-local declaration is inaccessible. */ + if (fd->vthis == vd) + return error_no_frame_access (fd); + t = get_decl_tree (fd->vthis); Dsymbol *outer = fd; diff --git a/gcc/testsuite/gdc.dg/pr96254a.d b/gcc/testsuite/gdc.dg/pr96254a.d new file mode 100644 index 00000000000..e5dd1244b94 --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr96254a.d @@ -0,0 +1,28 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96254 +// { dg-do compile } +struct map(alias fun) +{ + @property run() + { + } +} + +struct Task(Args) +{ + Args _args; +} + +template reduce(functions...) +{ + auto reduce(Args)(Args args) + { + alias RTask = Task!(typeof(args)); + auto task = RTask(); + } +} + +void main() // { dg-error "'D main' is a nested function and cannot be accessed" } +{ + immutable delta = 1; + reduce!"a + b"(map!({ immutable x = delta; })()); +} diff --git a/gcc/testsuite/gdc.dg/pr96254b.d b/gcc/testsuite/gdc.dg/pr96254b.d new file mode 100644 index 00000000000..02e3c484d26 --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr96254b.d @@ -0,0 +1,24 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96254 +// { dg-do compile } +mixin template test() +{ + int next; +} + +void foo(alias l)() +{ + l.next = 0; // { dg-error "cannot get frame pointer to 'D main'" } +} + +void bar(alias l, alias t)() +{ + l.next = 0; // { dg-error "cannot get frame pointer to 'D main'" } +} + +void main() +{ + mixin test l1; + mixin test l2; + foo!(l1); + bar!(l1,l2); +}