From patchwork Tue May 2 14:45:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Monakov X-Patchwork-Id: 89397 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp669590vqo; Tue, 2 May 2023 07:45:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5sy5eAIPFtU+YnW2oBFPIEx6APYfx6OOPqmvMq+9r9wHHuhBhhK3YFLdyPcSfV2I/niz6G X-Received: by 2002:a05:6402:515c:b0:506:92d7:6dce with SMTP id n28-20020a056402515c00b0050692d76dcemr9996790edd.15.1683038752355; Tue, 02 May 2023 07:45:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683038752; cv=none; d=google.com; s=arc-20160816; b=oGunCKBiKtbprgyDRKMuIYG3f4b1whzCK1Gjw2qBmNrA7oeDzCsqAQA1waPN+/ftkz mvBruPhKBEpQecyN9q6YbxFq+TzUNmnePxozdN06FPHBWc2mMbeaxIlMlIu+MGFcqxe7 4gf6BAE7mt2UyokSXZxwkCh6buuX/qBv4YRPqY4iUo5hBE7QaCc6iLpr5pjkuQ6V+Uef mFMn3HpWZCDKHIfTMlKiAgc110kU7vVvt/Sn2yL9eAL/2jfgCF1dKennpu5i67pQ+aPZ OswnENEGMyydRRt4KVWf6r2KHtuOD7I6Hip9VhlqpHLrukrOD9A8bDegTBzKzGL+57sz E/8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dkim-filter:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=hVqydw264bu1rE26AR+8XSWD5l9KATKS6jHATI4wox4=; b=xhz+rvtnX65E6bNpcyy0IcwhhXlMgzGyfDgiSfu4yIYpeV29esmhxsEGde8dagEkKg 7FYau7Fz62snrxiH6F0wxbyoSzXgiXBMgrWZEecze4HbSr3AE9HV9tPuYe0YHcZi0a3D 5SrHFzxAP8/m4Q7gP1XTRaftJZ4TVOJ5xHFeOVQUlIiZqWCSQ2bDkZWDO3gyPVrVGjfP HO8a/h338n67eyRBiCdtF0XNK6wBtynlPKkdPTknt6IHOGD+I9pQLpc/NGr8K5TS1zvD Lce50QXi7BCA42JeKyCWL6YJlQYQzIkS59DuoyYwdduKlpxCQdQxNPQaD4dmskDHAkaA Yksg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=MWnht6yn; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y17-20020a50e611000000b00506b37e7d30si22420507edm.417.2023.05.02.07.45.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 07:45:52 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=MWnht6yn; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3F2BA3858421 for ; Tue, 2 May 2023 14:45:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F2BA3858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683038751; bh=hVqydw264bu1rE26AR+8XSWD5l9KATKS6jHATI4wox4=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=MWnht6ynFSQAa0EfkJelxQfglctNtXqhfEOQw3h4zjwy9ZS2KuY8ef9zXG14W9Or7 OplPsV0eoxnjkCoOWkSkghR7qfK4d7pgCIMI3qjhSbVhNX03lJK5Y8EDWHExNLXOF2 O7D/Fp3hfQWzFG580qgDoT7cthasdgYGMGEtW7X4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by sourceware.org (Postfix) with ESMTPS id AB40B3858D1E for ; Tue, 2 May 2023 14:45:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB40B3858D1E Received: from localhost.intra.ispras.ru (unknown [10.10.3.121]) by mail.ispras.ru (Postfix) with ESMTP id E108140737D5; Tue, 2 May 2023 14:45:05 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru E108140737D5 To: gcc-patches@gcc.gnu.org Cc: Alexander Monakov Subject: [PATCH] do not tailcall __sanitizer_cov_trace_pc [PR90746] Date: Tue, 2 May 2023 17:45:04 +0300 Message-Id: <20230502144504.14654-1-amonakov@ispras.ru> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Alexander Monakov via Gcc-patches From: Alexander Monakov Reply-To: Alexander Monakov Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764794043042947495?= X-GMAIL-MSGID: =?utf-8?q?1764794043042947495?= When instrumentation is requested via -fsanitize-coverage=trace-pc, GCC emits calls to __sanitizer_cov_trace_pc callback into each basic block. This callback is supposed to be implemented by the user, and should be able to identify the containing basic block by inspecting its return address. Tailcalling the callback prevents that, so disallow it. gcc/ChangeLog: PR sanitizer/90746 * calls.cc (can_implement_as_sibling_call_p): Reject calls to __sanitizer_cov_trace_pc. gcc/testsuite/ChangeLog: PR sanitizer/90746 * gcc.dg/sancov/basic0.c: Verify absence of tailcall. --- gcc/calls.cc | 10 ++++++++++ gcc/testsuite/gcc.dg/sancov/basic0.c | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gcc/calls.cc b/gcc/calls.cc index 4d7f6c3d2..c6ed2f189 100644 --- a/gcc/calls.cc +++ b/gcc/calls.cc @@ -2541,6 +2541,16 @@ can_implement_as_sibling_call_p (tree exp, return false; } + /* __sanitizer_cov_trace_pc is supposed to inspect its return address + to identify the caller, and therefore should not be tailcalled. */ + if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL + && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SANITIZER_COV_TRACE_PC) + { + /* No need for maybe_complain_about_tail_call here: the call + is synthesized by the compiler. */ + return false; + } + /* If the called function is nested in the current one, it might access some of the caller's arguments, but could clobber them beforehand if the argument areas are shared. */ diff --git a/gcc/testsuite/gcc.dg/sancov/basic0.c b/gcc/testsuite/gcc.dg/sancov/basic0.c index af69b2d12..dfdaea848 100644 --- a/gcc/testsuite/gcc.dg/sancov/basic0.c +++ b/gcc/testsuite/gcc.dg/sancov/basic0.c @@ -1,9 +1,11 @@ /* Basic test on number of inserted callbacks. */ /* { dg-do compile } */ -/* { dg-options "-fsanitize-coverage=trace-pc -fdump-tree-optimized" } */ +/* { dg-options "-fsanitize-coverage=trace-pc -fdump-tree-optimized -fdump-rtl-expand" } */ void foo(void) { } /* { dg-final { scan-tree-dump-times "__builtin___sanitizer_cov_trace_pc \\(\\)" 1 "optimized" } } */ +/* The built-in should not be tail-called: */ +/* { dg-final { scan-rtl-dump-not "call_insn/j" "expand" } } */