From patchwork Tue Mar 12 09:10:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1910907 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=g181B7Sa; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tv7DC6dJYz1yWt for ; Tue, 12 Mar 2024 20:11:07 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D7EBB38582BD for ; Tue, 12 Mar 2024 09:11:05 +0000 (GMT) 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 BB160385841E for ; Tue, 12 Mar 2024 09:10:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BB160385841E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BB160385841E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710234639; cv=none; b=X3/IYd836N1Ml1whnlQLuwh+od2fd3s4/+OB5Ck4Zy2gQYyRh98jpc/lZnvIyB3KeYyxXnMLxN9hAdX6faukrNVbRNWJ9+4h5UAKHXwI8yQ58QEZgHaIVkoifGH7XytiqqOQ+uNJn7kb8lsxYIXrH4UW/bZKHCH1ZWsikn1UEpM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710234639; c=relaxed/simple; bh=RTvVlr19HE0maLg/Hn+6pObZ3nYmEU5uRoaeJbS1WRY=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=b0w3bYiGhrMjMFl71d+JlDDW7w4/RcuuDF5Nx9pDbVTuyk+30qj1cJVcamd6s0bDz64AlVFCWu5NQiWmDDzHxOaWzbd/57uR6Tzg/zzRmE+8TjEemekftDVn5lJq/lcPDklrRRvOmCFm4in4le9w598quGnyPZPKIae0edGTCtc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710234637; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=aF0I8Emp9/rvHKDkSRLnrLQLc3/+ufQg9oPRGg6WCTU=; b=g181B7SaMyeadAIcDPOuG1d5/rJoMQ6dU9yqDByqovSqQuDYE0oo64wvnpx84caBAj/JFj VF7dnK/0ghzTjQiUEjQv3S2bOiIwpqvsAizmnwe/3AKoXQLihRIlfKLd2XEgsHqcgOUkt0 aURdPW/emjF7chZWXTgYvycTdnZGUrA= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-219-kR_jp4A-OWCkNjJZUBNGgA-1; Tue, 12 Mar 2024 05:10:33 -0400 X-MC-Unique: kR_jp4A-OWCkNjJZUBNGgA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 946851C0215C; Tue, 12 Mar 2024 09:10:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.225.36]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 599C12166B4F; Tue, 12 Mar 2024 09:10:32 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 42C9AUAs3184538 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 12 Mar 2024 10:10:30 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 42C9AUYT3184537; Tue, 12 Mar 2024 10:10:30 +0100 Date: Tue, 12 Mar 2024 10:10:30 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] asan: Fix ICE during instrumentation of returns_twice calls [PR112709] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The following patch on top of the previously posted ubsan/gimple-iterator one handles asan the same. While the case of returning by hidden reference is handled differently because of the first recently posted asan patch, this deals with instrumentation of the aggregates returned in registers case as well as instrumentation of loads from aggregate memory in the function arguments of returns_twice calls. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-03-12 Jakub Jelinek PR sanitizer/112709 * asan.cc (asan_insert_before): New function. (maybe_create_ssa_name, maybe_cast_to_ptrmode, build_check_stmt, maybe_instrument_call, asan_expand_mark_ifn): Use it instead of gsi_insert_before. * gcc.dg/asan/pr112709-2.c: New test. Jakub --- gcc/asan.cc.jj 2024-03-11 13:49:58.931045179 +0100 +++ gcc/asan.cc 2024-03-11 18:38:29.047330489 +0100 @@ -2561,6 +2561,21 @@ build_shadow_mem_access (gimple_stmt_ite return gimple_assign_lhs (g); } +/* Insert G stmt before ITER. If ITER is a returns_twice call, + insert it on an appropriate edge instead. */ + +static void +asan_insert_before (gimple_stmt_iterator *iter, gimple *g) +{ + gimple *stmt = gsi_stmt (*iter); + if (stmt + && is_gimple_call (stmt) + && (gimple_call_flags (stmt) & ECF_RETURNS_TWICE) != 0) + gsi_insert_before_returns_twice_call (gsi_bb (*iter), g); + else + gsi_insert_before (iter, g, GSI_SAME_STMT); +} + /* BASE can already be an SSA_NAME; in that case, do not create a new SSA_NAME for it. */ @@ -2574,7 +2589,7 @@ maybe_create_ssa_name (location_t loc, t gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (base)), base); gimple_set_location (g, loc); if (before_p) - gsi_insert_before (iter, g, GSI_SAME_STMT); + asan_insert_before (iter, g); else gsi_insert_after (iter, g, GSI_NEW_STMT); return gimple_assign_lhs (g); @@ -2593,7 +2608,7 @@ maybe_cast_to_ptrmode (location_t loc, t NOP_EXPR, len); gimple_set_location (g, loc); if (before_p) - gsi_insert_before (iter, g, GSI_SAME_STMT); + asan_insert_before (iter, g); else gsi_insert_after (iter, g, GSI_NEW_STMT); return gimple_assign_lhs (g); @@ -2684,7 +2699,7 @@ build_check_stmt (location_t loc, tree b align / BITS_PER_UNIT)); gimple_set_location (g, loc); if (before_p) - gsi_insert_before (&gsi, g, GSI_SAME_STMT); + asan_insert_before (&gsi, g); else { gsi_insert_after (&gsi, g, GSI_NEW_STMT); @@ -3025,7 +3040,7 @@ maybe_instrument_call (gimple_stmt_itera tree decl = builtin_decl_implicit (BUILT_IN_ASAN_HANDLE_NO_RETURN); gimple *g = gimple_build_call (decl, 0); gimple_set_location (g, gimple_location (stmt)); - gsi_insert_before (iter, g, GSI_SAME_STMT); + asan_insert_before (iter, g); } } @@ -3852,7 +3867,7 @@ asan_expand_mark_ifn (gimple_stmt_iterat g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), NOP_EXPR, len); gimple_set_location (g, loc); - gsi_insert_before (iter, g, GSI_SAME_STMT); + asan_insert_before (iter, g); tree sz_arg = gimple_assign_lhs (g); tree fun --- gcc/testsuite/gcc.dg/asan/pr112709-2.c.jj 2024-03-11 18:30:59.813488200 +0100 +++ gcc/testsuite/gcc.dg/asan/pr112709-2.c 2024-03-11 18:31:06.506396462 +0100 @@ -0,0 +1,50 @@ +/* PR sanitizer/112709 */ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=address -O2" } */ + +struct S { char c[1024]; } *p; +int foo (int); + +__attribute__((returns_twice, noipa)) int +bar (struct S x) +{ + (void) x.c[0]; + return 0; +} + +void +baz (int *y) +{ + foo (1); + *y = bar (*p); +} + +void +qux (int x, int *y) +{ + if (x == 25) + x = foo (2); + else if (x == 42) + x = foo (foo (3)); + *y = bar (*p); +} + +void +corge (int x, int *y) +{ + void *q[] = { &&l1, &&l2, &&l3, &&l3 }; + if (x == 25) + { + l1: + x = foo (2); + } + else if (x == 42) + { + l2: + x = foo (foo (3)); + } +l3: + *y = bar (*p); + if (x < 4) + goto *q[x & 3]; +}