From patchwork Mon Sep 5 12:14:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuri Gribov X-Patchwork-Id: 990 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp147885wrt; Mon, 5 Sep 2022 05:15:06 -0700 (PDT) X-Google-Smtp-Source: AA6agR51ZIri9fj+9jG6ntBP+ly49MW0aQIPEC4pZW6TW6jDLq2J0CZUpXFjBigwNtCmV9wTvdqJ X-Received: by 2002:a17:906:cc4d:b0:73d:f594:159b with SMTP id mm13-20020a170906cc4d00b0073df594159bmr31487065ejb.651.1662380106356; Mon, 05 Sep 2022 05:15:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662380106; cv=none; d=google.com; s=arc-20160816; b=O2xbxrdyPTBJ6JUoSaWUza9WKr9PwjdaQnzbd8613WVs70WaQDf1ZxCKwHsOgSRUCi tmZ3IQzfQYmMmp4aaqChkLGZYLJaSHVIo9C7ifq3kQ2okbCkYZHtfKkfJVXlAgsLZWuN jBZqL+1mXX0ItpPr64d2/HUyq5wKwK9m7X6bVkbJfnUd1WL5PMlYWpHhsTvSZSt08Fdt /MdrClgPYUuEUeOhaT+mgyCoQdvzkM4xsAepVcpvg2xoMvwzgwJ0UZxUjXg78ayowWwN ptEfq7Q647+HWR8CnxWRQtDMTXLQvTUanho5xMa2q4iMarPlFjFdn5gIGoEaKv5W/THO o17g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:to :subject:message-id:date:mime-version:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=HYrv0YtfTRbHW4zyNLw0lf2+V1uPqMS6TTuRYGuejHA=; b=A8vKHlwz4TCY7ogzsDTCqnZ2my5eQ1WMlVshaDwYy6xjYk8TpNjI3x+cv4ZuW0+Hi5 1pptwXyPOXXBTC9Lz+5+7mFwXidqLoDWtBkKX2neSdjm3gpBhHvSJbHADpdXHfvEVkB2 SLGAt4HxENMF8FC+VGknQ+dkdo2PNnqEXh38uBaQOUpKO93DmRAmVwgKhSucHcPGt02c F9I5tsPQk0atSHUT7sbYZz+uk52mbLN9EFgxMFmb6ibLXhGMt3zMDKBmSV/SAHatlgHT t7aVLH5Ej3yT503I8BLR/5dljhyalbLOu8vm+VvUsaB+3Jjb6HH2poAqEpUeMHh8qs8I 0l3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=lzttcg+u; 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 k5-20020a50cb85000000b004484fbd4d33si7048346edi.117.2022.09.05.05.15.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Sep 2022 05:15:06 -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=lzttcg+u; 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 093533857016 for ; Mon, 5 Sep 2022 12:15:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 093533857016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662380105; bh=HYrv0YtfTRbHW4zyNLw0lf2+V1uPqMS6TTuRYGuejHA=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=lzttcg+uRY4mzgVJ2DCrvRHjs633owAv0/YJpXSDau+9lqB49DvDE41VXNwwwk7Bc 2Qe/pJ2FXkCDDddGKwMO8mi6G7VudrAJjpzTA47PN37dWrHL/tHzs+OtETKf/6Jy17 EaZLomhVLLSSozSTKgI4Sth2uBNR690axySlitSg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by sourceware.org (Postfix) with ESMTPS id F1A543857016 for ; Mon, 5 Sep 2022 12:14:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F1A543857016 Received: by mail-il1-x134.google.com with SMTP id b17so4596002ilh.0 for ; Mon, 05 Sep 2022 05:14:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date; bh=HYrv0YtfTRbHW4zyNLw0lf2+V1uPqMS6TTuRYGuejHA=; b=M9gVRsXKR30FdkiXtZhKw4j3qG87TzAs7rV1nirC0GjO4r9iOiDcIhi3rCYueuHah1 EWFCtrIrJ60tTyVfmFY2n4x1YnQPIReLOf/rnHs0PY6L72Ia2agEfmPw8B0L8Ggmcr/l sW4Q/8s/WtPBfIpDHZrDKLxPfv6D75XWy5GRsx7X2PUp1SHZD+CqwOuzZ+M0UGULvj43 /ylbSlQu5MWbTALmbjb16Cl7AGv2YRFXL/BG8fvPipjsXANg9plkBysoffjKzdGJdST9 Tj6m9vQ2oxrfkZN7NYoCinnzbAXNwDMm+Bct9nYq1gyF1VQDWvPeruvdd0SBS6Rm/9fa LbGw== X-Gm-Message-State: ACgBeo2ySUNCTHTjWS+Hq+V7YhNJ3u5X28IHtDZ8lqUPsKppLcUMGvOp 1VWcq0auJrc4CzqaKUpH8iFQ2D0bgT9+DpaFeQ4hiAG3/eU= X-Received: by 2002:a92:dcc2:0:b0:2e4:38f2:a9f6 with SMTP id b2-20020a92dcc2000000b002e438f2a9f6mr25866682ilr.130.1662380061233; Mon, 05 Sep 2022 05:14:21 -0700 (PDT) MIME-Version: 1.0 Date: Mon, 5 Sep 2022 15:14:10 +0300 Message-ID: Subject: [PATCH][sanitizer/106558] asan: fix unsafe optimization of Asan checks. To: GCC Patches X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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: Yuri Gribov via Gcc-patches From: Yuri Gribov Reply-To: Yuri Gribov Cc: Jakub Jelinek 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?1743131882291820303?= X-GMAIL-MSGID: =?utf-8?q?1743131882291820303?= Hi, This patch fixes incorrect Asan optimization in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106558 . It successfully passes bootstrap-asan, regular bootstrap and regression testing (on x86_64). With this patch number of optimizations has reduced only slightly (146062 -> 145824 on bootstrap-asan) so I decided to skip the more complicated alias oracle-based approach that was suggested by Jakub in the PR. Best regards, Yuri From 3aebd2adc30e164065327c7d3820ad98fe59cad8 Mon Sep 17 00:00:00 2001 From: Yuri Gribov Date: Sun, 14 Aug 2022 08:42:44 +0300 Subject: [PATCH] asan: fix unsafe optimization of Asan checks. gcc/ PR sanitizer/106558 * sanopt.c: Do not optimize out checks for non-SSA addresses. gcc/testsuite/ PR sanitizer/106558 * c-c++-common/asan/pr106558.c: New test. --- gcc/sanopt.cc | 40 +++++++++++++++++----- gcc/testsuite/c-c++-common/asan/pr106558.c | 23 +++++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/asan/pr106558.c diff --git a/gcc/sanopt.cc b/gcc/sanopt.cc index e9d188d7889..13942a0b1da 100644 --- a/gcc/sanopt.cc +++ b/gcc/sanopt.cc @@ -80,16 +80,16 @@ struct sanopt_info /* If T has a single definition of form T = T2, return T2. */ -static tree +static gimple * maybe_get_single_definition (tree t) { if (TREE_CODE (t) == SSA_NAME) { gimple *g = SSA_NAME_DEF_STMT (t); if (gimple_assign_single_p (g)) - return gimple_assign_rhs1 (g); + return g; } - return NULL_TREE; + return NULL; } /* Tree triplet for vptr_check_map. */ @@ -618,11 +618,30 @@ maybe_optimize_ubsan_vptr_ifn (class sanopt_ctx *ctx, gimple *stmt) return true; } +/* Checks whether value of T in CHECK and USE is the same. */ + +static bool same_value_p (gimple *check, gimple *use, tree t) +{ + tree check_vuse = gimple_vuse (check); + tree use_vuse = gimple_vuse (use); + + if (TREE_CODE (t) == SSA_NAME + || is_gimple_min_invariant (t) + || ! use_vuse) + return true; + + if (check_vuse == use_vuse) + return true; + + return false; +} + /* Returns TRUE if ASan check of length LEN in block BB can be removed if preceded by checks in V. */ static bool -can_remove_asan_check (auto_vec &v, tree len, basic_block bb) +can_remove_asan_check (auto_vec &v, tree len, basic_block bb, + gimple *base_stmt, tree base_addr) { unsigned int i; gimple *g; @@ -674,8 +693,10 @@ can_remove_asan_check (auto_vec &v, tree len, basic_block bb) last_bb = imm; } - if (last_bb == gbb) - remove = true; + if (last_bb != gbb) + break; + // In case of base_addr residing in memory we also need to check aliasing + remove = ! base_addr || same_value_p (g, base_stmt, base_addr); break; } @@ -718,7 +739,8 @@ maybe_optimize_asan_check_ifn (class sanopt_ctx *ctx, gimple *stmt) auto_vec *ptr_checks = &ctx->asan_check_map.get_or_insert (ptr); - tree base_addr = maybe_get_single_definition (ptr); + gimple *base_stmt = maybe_get_single_definition (ptr); + tree base_addr = base_stmt ? gimple_assign_rhs1 (base_stmt) : NULL_TREE; auto_vec *base_checks = NULL; if (base_addr) { @@ -747,11 +769,11 @@ maybe_optimize_asan_check_ifn (class sanopt_ctx *ctx, gimple *stmt) bool remove = false; if (ptr_checks) - remove = can_remove_asan_check (*ptr_checks, len, bb); + remove = can_remove_asan_check (*ptr_checks, len, bb, NULL, NULL); if (!remove && base_checks) /* Try with base address as well. */ - remove = can_remove_asan_check (*base_checks, len, bb); + remove = can_remove_asan_check (*base_checks, len, bb, base_stmt, base_addr); if (!remove) { diff --git a/gcc/testsuite/c-c++-common/asan/pr106558.c b/gcc/testsuite/c-c++-common/asan/pr106558.c new file mode 100644 index 00000000000..d82b2dc7a83 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/pr106558.c @@ -0,0 +1,23 @@ +/* { dg-do run } */ +/* { dg-options "-w -fpermissive" } */ +/* { dg-shouldfail "asan" } */ + +int a; +int *b = &a; +int **c = &b; +int d[1]; +int *e = &d[1]; + +static int f(int *g) { + *b = e; + *c = e; + *b = 2; + *g = 2; +} + +int main() { + f(b); + return *b; +} + +/* { dg-output "AddressSanitizer: global-buffer-overflow on address" } */ -- 2.17.1