From patchwork Sat May 16 22:23:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1291990 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=jQ9EkEEs; 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 49PfvR44n5z9sTD for ; Sun, 17 May 2020 08:24:01 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 89BDB386F454; Sat, 16 May 2020 22:23:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 89BDB386F454 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1589667835; bh=Q9T0tlJdZ36Eg3uKKbEu869UYf4IzNVlSr1m3lR6YIg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jQ9EkEEsK9FRx4y702RdDJZpLhg9pwC6Ju5npU1Om4Y87GRWXjTA2Ker0ZJNKPhot +htaq8u+838npqZTT0MAtMQvu+Kt2CiAVPhWasOr0Nps9NBP/q+8/JHwCXtFPmcPpC 5IlYnodspcbYfPTNU13tJIEk1XEYzgzNOhCgRv7o= 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 786543851C0C for ; Sat, 16 May 2020 22:23:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 786543851C0C 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 49PfvB0g0mzKmVC for ; Sun, 17 May 2020 00:23:50 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id WE7oKGUbrLIR for ; Sun, 17 May 2020 00:23:43 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed][GCC9] d: Fix wrong vtable offset in virtual function call Date: Sun, 17 May 2020 00:23:42 +0200 Message-Id: <20200516222342.29585-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: D1AAE1673 X-Rspamd-Score: 0.28 / 15.00 / 15.00 X-Spam-Status: No, score=-19.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 fixes PR95155, which prevented the D front-end in gcc-9 from being able to bootstrap a self-hosted D compiler. The Semantic (pass 1) analysis for classes is handled by ClassDeclaration::semantic. For a given class, this method may be ran multiple times in order to resolve forward references. The method incrementally tries to resolve the types referred to by the members of the class. The subsequent calls to this method are short-circuited if the class members have been fully analyzed. For this the code tests that it is not the first/main call to the method (semanticRun == PASS.init else branch), scx is not set, and that the this->symtab is already set. If all these conditions are met, the method returns. But before returning, the method was setting this->semanticRun to PASSsemanticdone. It should not set semanticRun since the class has not been fully analyzed yet. The base class analysis for this class could be pending and as a result vtable may not have been fully created. This fake setting of semanticRun results in the semantic analyzer to believe that the class has been fully analyzed. As exposed by the issues in upstream, it may result in compile time errors when a derived type class is getting analyzed and because of this fake semanticdone on the base class, the semantic analysis construes that an overriden method is not defined in the base class. PR95155 exposes anoter scenario where a buggy vtable may be created and a call to a class method may result in execution of some adhoc code. Backported from r10-1131. Regression tested on x86_64-linux-gnu, committed to gcc-9 branch. Regards Iain --- gcc/d/ChangeLog: PR d/95155 * dmd/dclass.c (ClassDeclaration::semantic): Don't prematurely set done on semantic analysis. gcc/testsuite/ChangeLog: PR d/95155 * gdc.test/compilable/imports/pr9471a.d: New test. * gdc.test/compilable/imports/pr9471b.d: New test. * gdc.test/compilable/imports/pr9471c.d: New test. * gdc.test/compilable/imports/pr9471d.d: New test. * gdc.test/compilable/pr9471.d: New test. --- gcc/d/dmd/dclass.c | 1 - .../gdc.test/compilable/imports/pr9471a.d | 2 ++ .../gdc.test/compilable/imports/pr9471b.d | 5 +++++ .../gdc.test/compilable/imports/pr9471c.d | 18 ++++++++++++++++++ .../gdc.test/compilable/imports/pr9471d.d | 1 + gcc/testsuite/gdc.test/compilable/pr9471.d | 6 ++++++ 6 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gdc.test/compilable/imports/pr9471a.d create mode 100644 gcc/testsuite/gdc.test/compilable/imports/pr9471b.d create mode 100644 gcc/testsuite/gdc.test/compilable/imports/pr9471c.d create mode 100644 gcc/testsuite/gdc.test/compilable/imports/pr9471d.d create mode 100644 gcc/testsuite/gdc.test/compilable/pr9471.d diff --git a/gcc/d/dmd/dclass.c b/gcc/d/dmd/dclass.c index 572b3e24387..66869361dcb 100644 --- a/gcc/d/dmd/dclass.c +++ b/gcc/d/dmd/dclass.c @@ -395,7 +395,6 @@ void ClassDeclaration::semantic(Scope *sc) } else if (symtab && !scx) { - semanticRun = PASSsemanticdone; return; } semanticRun = PASSsemantic; diff --git a/gcc/testsuite/gdc.test/compilable/imports/pr9471a.d b/gcc/testsuite/gdc.test/compilable/imports/pr9471a.d new file mode 100644 index 00000000000..79b78e1e52a --- /dev/null +++ b/gcc/testsuite/gdc.test/compilable/imports/pr9471a.d @@ -0,0 +1,2 @@ +import imports.pr9471c; +class AggregateDeclaration : ScopeDsymbol { } diff --git a/gcc/testsuite/gdc.test/compilable/imports/pr9471b.d b/gcc/testsuite/gdc.test/compilable/imports/pr9471b.d new file mode 100644 index 00000000000..a46a12c496f --- /dev/null +++ b/gcc/testsuite/gdc.test/compilable/imports/pr9471b.d @@ -0,0 +1,5 @@ +import imports.pr9471a; +class ClassDeclaration : AggregateDeclaration +{ + void isBaseOf(); +} diff --git a/gcc/testsuite/gdc.test/compilable/imports/pr9471c.d b/gcc/testsuite/gdc.test/compilable/imports/pr9471c.d new file mode 100644 index 00000000000..d80a61480ce --- /dev/null +++ b/gcc/testsuite/gdc.test/compilable/imports/pr9471c.d @@ -0,0 +1,18 @@ +import imports.pr9471b; + +struct Array(T) +{ + static if (is(typeof(T.opCmp))) { } +} +alias ClassDeclarations = Array!ClassDeclaration; + +class Dsymbol +{ + void addObjcSymbols(ClassDeclarations); +} + +class ScopeDsymbol : Dsymbol +{ + import imports.pr9471d; + void importScope(); +} diff --git a/gcc/testsuite/gdc.test/compilable/imports/pr9471d.d b/gcc/testsuite/gdc.test/compilable/imports/pr9471d.d new file mode 100644 index 00000000000..187b9083294 --- /dev/null +++ b/gcc/testsuite/gdc.test/compilable/imports/pr9471d.d @@ -0,0 +1 @@ +// Module needs to be imported to trigger bug. diff --git a/gcc/testsuite/gdc.test/compilable/pr9471.d b/gcc/testsuite/gdc.test/compilable/pr9471.d new file mode 100644 index 00000000000..37ff32e4957 --- /dev/null +++ b/gcc/testsuite/gdc.test/compilable/pr9471.d @@ -0,0 +1,6 @@ +// PERMUTE_ARGS: +// EXTRA_FILES: imports/pr9471a.d imports/pr9471b.d imports/pr9471c.d imports/pr9471d.d +import imports.pr9471a; +import imports.pr9471b; + +static assert (__traits(getVirtualIndex, ClassDeclaration.isBaseOf) == 7);