From patchwork Mon Jun 13 19:38:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1642970 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=oFWmR2gG; dkim-atps=neutral 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+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) 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 (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LMMM15Vt5z9s0r for ; Tue, 14 Jun 2022 05:38:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9373D385040D for ; Mon, 13 Jun 2022 19:38:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9373D385040D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1655149129; bh=aYun5S0DxHLz5fHAXSCuwEIL4cariPbdlqne6nTndu8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=oFWmR2gGEO2O++MBCrWnMmAqpd4nC/RzUPcRSivS5spj7Mqjjzhjzcj7ThLghoGzs U4eNsPXENms6EqFv2Q2rFsbpbaSR0AAzQfbNXDz6tA03o54oQgJVHXk+wRioIsOcNH KmqqNcERZNy1ew/X/nHuwZyw9l8vwpbsXJNcJe7c= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 8170138582AD for ; Mon, 13 Jun 2022 19:38:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8170138582AD Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-647-Q5NylrLLOk-D_tbcAYtVcw-1; Mon, 13 Jun 2022 15:38:27 -0400 X-MC-Unique: Q5NylrLLOk-D_tbcAYtVcw-1 Received: by mail-qt1-f199.google.com with SMTP id f5-20020ac87f05000000b003052b88f1deso4018837qtk.0 for ; Mon, 13 Jun 2022 12:38:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=aYun5S0DxHLz5fHAXSCuwEIL4cariPbdlqne6nTndu8=; b=dlN2DWPl9uDPwHr3KoFUlZnxv+IXPzV2SfmyCsJ5ocuX4FMZ7QTGlLKGeY5WTMxpQx yTAOLt/VmjRh0Eer8HWQUu5hiXexwIEBw/PqZXD6k/un9/hsXoaVK0REdyO+ZdsP1HeN cIlIJPv5SLVP3EAuU6kuT3dvEPFRokx9RKifbyB+Y8kfOIY1H9uL7NPF7+r5ve4ec4mX npK5Y/ukojaA9pmKDu0cwg8EKJGhj0ko4s/UyrtRQIy8mTPCCTdBGjQb9AvjxQJdY5bi 5mrvPAjDzPaSUcHIikTf0aO6yMn3ZwOs9id3bj84UoE6XYJWaN1Nhb+Yg/wgPjkSU+ha IubA== X-Gm-Message-State: AJIora9vkhUMhvMvAyJIVNDb73kxlpZJGgqkJ3/eor4Op6VBTip52Cyc cGhWr/LUnLao6ZKtv4PeMj5rtN0QDOOIfRBsNr5/6GpJteyNbxPqgpxOyrDBdTI7ZE7Pi/y3QVm 0iSXGOSvxGYeTkOJjyRem04B/I9xOYgbL8H0nrnzAIzepJDCKgSegWNErwj/bU7IkSA== X-Received: by 2002:ad4:5c87:0:b0:464:5127:6b56 with SMTP id o7-20020ad45c87000000b0046451276b56mr625995qvh.66.1655149106373; Mon, 13 Jun 2022 12:38:26 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t5pUKjm8USENj6mjkUcB1wfcn66vDra4Zcl9YSSJgb7AvbF68WwZVudnT6XpecfGFW6c0PJw== X-Received: by 2002:ad4:5c87:0:b0:464:5127:6b56 with SMTP id o7-20020ad45c87000000b0046451276b56mr625968qvh.66.1655149105910; Mon, 13 Jun 2022 12:38:25 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id s19-20020a05620a29d300b006a6d3fa430csm7858560qkp.58.2022.06.13.12.38.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jun 2022 12:38:25 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH RFA] ubsan: -Wreturn-type and ubsan trap-on-error Date: Mon, 13 Jun 2022 15:38:23 -0400 Message-Id: <20220613193823.3233868-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" I noticed that -fsanitize=undefined -fsanitize-undefined-trap-on-error was omitting the usual -Wreturn-type warning for control flowing off the end of a function. This was because the warning code was looking for calls either to __builtin_unreachable or the UBSan function, but these flags produce a call to __builtin_trap instead. Tested x86_64-pc-linux-gnu, OK for trunk? gcc/c-family/ChangeLog: * c-ubsan.cc (ubsan_instrument_return): Use BUILTINS_LOCATION. gcc/ChangeLog: * tree-cfg.cc (pass_warn_function_return::execute): Also check BUILT_IN_TRAP. gcc/testsuite/ChangeLog: * g++.dg/ubsan/return-8.C: New test. --- gcc/c-family/c-ubsan.cc | 4 +++- gcc/testsuite/g++.dg/ubsan/return-8.C | 9 +++++++++ gcc/tree-cfg.cc | 5 +++-- 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ubsan/return-8.C base-commit: 13ea4a6e830da1f245136601e636dec62e74d1a7 diff --git a/gcc/c-family/c-ubsan.cc b/gcc/c-family/c-ubsan.cc index 48f948745f8..a2cd8fb3262 100644 --- a/gcc/c-family/c-ubsan.cc +++ b/gcc/c-family/c-ubsan.cc @@ -308,7 +308,9 @@ tree ubsan_instrument_return (location_t loc) { if (flag_sanitize_undefined_trap_on_error) - return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0); + return build_call_expr_loc + /* pass_warn_function_return checks for BUILTINS_LOCATION. */ + (BUILTINS_LOCATION, builtin_decl_explicit (BUILT_IN_TRAP), 0); tree data = ubsan_create_data ("__ubsan_missing_return_data", 1, &loc, NULL_TREE, NULL_TREE); diff --git a/gcc/testsuite/g++.dg/ubsan/return-8.C b/gcc/testsuite/g++.dg/ubsan/return-8.C new file mode 100644 index 00000000000..354c96098d2 --- /dev/null +++ b/gcc/testsuite/g++.dg/ubsan/return-8.C @@ -0,0 +1,9 @@ +// { dg-additional-options "-fsanitize=undefined -fsanitize-undefined-trap-on-error" } + +bool b; + +int f() { + if (b) return 42; +} // { dg-warning "-Wreturn-type" } + +int main() { f(); } diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc index 9e5d84a9805..c67c278dad0 100644 --- a/gcc/tree-cfg.cc +++ b/gcc/tree-cfg.cc @@ -9543,7 +9543,7 @@ pass_warn_function_return::execute (function *fun) } /* The C++ FE turns fallthrough from the end of non-void function into __builtin_unreachable () call with BUILTINS_LOCATION. - Recognize those too. */ + Recognize those as well as calls from ubsan_instrument_return. */ basic_block bb; if (!warning_suppressed_p (fun->decl, OPT_Wreturn_type)) FOR_EACH_BB_FN (bb, fun) @@ -9555,7 +9555,8 @@ pass_warn_function_return::execute (function *fun) if (last && ((LOCATION_LOCUS (gimple_location (last)) == BUILTINS_LOCATION - && gimple_call_builtin_p (last, BUILT_IN_UNREACHABLE)) + && (gimple_call_builtin_p (last, BUILT_IN_UNREACHABLE) + || gimple_call_builtin_p (last, BUILT_IN_TRAP))) || gimple_call_builtin_p (last, ubsan_missing_ret))) { gimple_stmt_iterator gsi = gsi_for_stmt (last);