From patchwork Fri Jul 2 19:21:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1500280 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=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) 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=bmjg2ct2; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (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 4GGlRR13Rsz9sVb for ; Sat, 3 Jul 2021 05:25:39 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CAB793985008 for ; Fri, 2 Jul 2021 19:25:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CAB793985008 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1625253936; bh=FGVXVgwBaD81wui5H2MrJ5rWnOP8NeGIMEiSiH0PMaE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=bmjg2ct2v4XRkDvBMVoatmDHkUMgGG1GR01div9H+9pU2r+tLi/5UrkE0L05vro0Q cBZIVZgTECGPvGRVRUG3Sy/11CB3Meb3b6kiua4CRkgDOEG938burflOSVzwa5QyXp 6fdA6mV4VT0D6UfCr9eEsV1h+WtaV7tfSXjhNuIw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by sourceware.org (Postfix) with ESMTPS id 7CF833985032 for ; Fri, 2 Jul 2021 19:21:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7CF833985032 Received: by mail-oi1-x235.google.com with SMTP id u11so12551741oiv.1 for ; Fri, 02 Jul 2021 12:21:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=FGVXVgwBaD81wui5H2MrJ5rWnOP8NeGIMEiSiH0PMaE=; b=aJGZeXSb71WJec64RXZjO+AZkh6k2VXEIRMPt+/geIDGt+ZArt8uMhE22HNjfdxrOm cAQsl4ojieG07LyMSNUV3PXIDiBs2/SqSN6WfOKv+gAch0tG8JUMXLpvRh8nP/Hfv9kJ rUze86dxdH67sj+2ZHg/dc/8J5U0FfqXroQZWL7zn/n4MJN11uwpFqF1e+74FytqDiWX p5nCpgfqpaYAkDnYXEFWF19FDJIueAV9xy/xbYiXdpv0H9siYWWnLwKiptk7hm6FR5nd oexcF0KK0FmSB0ntHkRK01mc99lwd6bsjZ4LuLcUKTckIl6Fs/xFu1kz5BPqQ+w7ASFX RPyA== X-Gm-Message-State: AOAM532Rfxe5c0y0OvWW7J9MbXI7ipj08eY4u2i+yj0Crot3OapVX+EY v72ycGl/SO9LzJ1fTrNq3m/SlDbbxTg= X-Google-Smtp-Source: ABdhPJxU9zr2SyKJEievEB8F5e5LrKlXamUynMsr3wNgVs/cGl9Ci6hqkdgkucK8BDiq2qmPjbm2fg== X-Received: by 2002:aca:3902:: with SMTP id g2mr850513oia.162.1625253671900; Fri, 02 Jul 2021 12:21:11 -0700 (PDT) Received: from [192.168.0.41] (75-166-102-22.hlrn.qwest.net. [75.166.102.22]) by smtp.gmail.com with ESMTPSA id f3sm775693ote.74.2021.07.02.12.21.11 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 Jul 2021 12:21:11 -0700 (PDT) To: gcc-patches Subject: [PATCH] handle sanitizer built-ins in -Wuninitialized (PR 101300) Message-ID: <44fabafb-9174-1739-9818-53122ef499f9@gmail.com> Date: Fri, 2 Jul 2021 13:21:10 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Martin Sebor via Gcc-patches From: Martin Sebor Reply-To: Martin Sebor Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" To avoid a class of false negatives for sanitized code -Wuninitialized recognizes the ASAN_MARK internal function doesn't modify its argument. But the warning code doesn't do the same for any sanitizer built-ins even though they don't modify user-supplied arguments either. This leaves another class of false negatives unresolved. The attached fix enhances the warning logic to recognize all sanitizer built-ins as well and treat them as non-modifying. Tested on x86_64-linux. Martin PR middle-end/101300 - -fsanitize=undefined suppresses -Wuninitialized for a VLA read at -O0 gcc/ChangeLog: * tree-ssa-uninit.c (check_defs): Handle UBSAN built-ins. gcc/testsuite/ChangeLog: * gcc.dg/uninit-pr101300.c: New test. diff --git a/gcc/tree-ssa-uninit.c b/gcc/tree-ssa-uninit.c index 99442d7f975..dfcb7aba7c1 100644 --- a/gcc/tree-ssa-uninit.c +++ b/gcc/tree-ssa-uninit.c @@ -228,9 +228,26 @@ check_defs (ao_ref *ref, tree vdef, void *data_) gimple *def_stmt = SSA_NAME_DEF_STMT (vdef); /* The ASAN_MARK intrinsic doesn't modify the variable. */ - if (is_gimple_call (def_stmt) - && gimple_call_internal_p (def_stmt, IFN_ASAN_MARK)) - return false; + if (is_gimple_call (def_stmt)) + { + if (gimple_call_internal_p (def_stmt) + && gimple_call_internal_fn (def_stmt) == IFN_ASAN_MARK) + return false; + + if (tree fndecl = gimple_call_fndecl (def_stmt)) + { + /* Some sanitizer calls pass integer arguments to built-ins + that expect pointets. Avoid using gimple_call_builtin_p() + which fails for such calls. */ + if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) + { + built_in_function fncode = DECL_FUNCTION_CODE (fndecl); + if (fncode > BEGIN_SANITIZER_BUILTINS + && fncode < END_SANITIZER_BUILTINS) + return false; + } + } + } /* End of VLA scope is not a kill. */ if (gimple_call_builtin_p (def_stmt, BUILT_IN_STACK_RESTORE)) diff --git a/gcc/testsuite/gcc.dg/uninit-pr101300.c b/gcc/testsuite/gcc.dg/uninit-pr101300.c new file mode 100644 index 00000000000..4392e8bae4f --- /dev/null +++ b/gcc/testsuite/gcc.dg/uninit-pr101300.c @@ -0,0 +1,53 @@ +/* PR middle-end/101300 - -fsanitize=undefined suppresses -Wuninitialized + for a VLA read at -O0 + { dg-do compile } + { dg-options "-O0 -Wall -fsanitize=undefined" } */ + +int warn_vla_rd0 (int n) +{ + char a[n]; + return a[0]; // { dg-warning "\\\[-Wuninitialized]" } +} + +int warn_vla_rd1 (int n) +{ + char a[n]; + return a[1]; // { dg-warning "\\\[-Wuninitialized]" } +} + +int warn_vla_rdi (int n, int i) +{ + char a[n]; + return a[i]; // { dg-warning "\\\[-Wuninitialized]" } +} + + +int warn_vla_wr0_rd2_1_0 (int n) +{ + char a[n]; + a[0] = __LINE__; + int x = a[2]; // { dg-warning "\\\[-Wuninitialized]" } + int y = a[1]; // { dg-warning "\\\[-Wuninitialized]" } + int z = a[0]; + return x + y + z; +} + +int warn_vla_wr1_rd2_1_0 (int n) +{ + char a[n]; + a[1] = __LINE__; + int x = a[2]; // { dg-warning "\\\[-Wuninitialized]" } + int y = a[1]; + int z = a[0]; // { dg-warning "\\\[-Wuninitialized]" } + return x + y + z; +} + +int warn_vla_wr2_rd2_1_0 (int n) +{ + char a[n]; + a[2] = __LINE__; + int x = a[2]; + int y = a[1]; // { dg-warning "\\\[-Wuninitialized]" } + int z = a[0]; // { dg-warning "\\\[-Wuninitialized]" } + return x + y + z; +}