From patchwork Mon Feb 28 17:42:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 1599034 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=aqg5s1xT; 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 (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4K6nlC1YHYz9sFv for ; Tue, 1 Mar 2022 04:42:29 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 95A923858C20 for ; Mon, 28 Feb 2022 17:42:27 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qv1-xf35.google.com (mail-qv1-xf35.google.com [IPv6:2607:f8b0:4864:20::f35]) by sourceware.org (Postfix) with ESMTPS id 347473858C83 for ; Mon, 28 Feb 2022 17:42:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 347473858C83 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=acm.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qv1-xf35.google.com with SMTP id j11so13867747qvy.0 for ; Mon, 28 Feb 2022 09:42:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:message-id:date:mime-version:user-agent:content-language:to :cc:from:subject; bh=SQhWR4idRiJlgfou6bXVEppfwVdutyPLjwoP4qkJIqM=; b=aqg5s1xTB8W1IAXBcJdiwCFxtNoOmSViYqmvPkjUTwmKBQHpIbNGKjMhQcXuDub1lv 4GthLsEc4Vh3RhKdgPWM+7d0EC3wvQz0N9JGO+ND9rnGqpdAR9XGRwVMxPfUknG0a+4Z DYwPj7v7BK07ra2fPicuFeKiU7c1oBaCXmrXCEH7nL/wsfrghn50k/77C9oHmCYz3kN3 FYel1p3fUC7QNIwrg4SDumqzsnraEeKpC4lL4+imH1xW/uTCoZPL0lNMV/mjeVw50QjS a2qCP1oWlnuw51dGIuhdRCLE6o9SiQiE5S7i6LKszWJEngiyX2n6F0lMN0Dy7syY2LRP QbCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:message-id:date:mime-version:user-agent :content-language:to:cc:from:subject; bh=SQhWR4idRiJlgfou6bXVEppfwVdutyPLjwoP4qkJIqM=; b=dAzcHYoaG8k55SMv+VhJZoClPHNsssZobJAaRZayKvIiIJ+ExslV0KCQSRhVes6Jgs NLCcPuSzh7APfBO9bMnN86KN2BSoC59GuuxJN8BXzPJJmlkqV1hWGH8p1A2oPc/4bu5g aj8hnOp8p31Vn+qczrbi5Mb95UkeBDhZFCpy7CRKqDoigpLuIYwNa9un8ZuKsOQkjHlj RPiqn47Nh7S0Z3veS+Eh7/diyHI8xkIUwpTwVi39tILO42XU3KdBtyKveJAsmi2sW91U HhCDXKKLXRBN0JBMAPOqj2/yX2FbAvq9iD1K1iQsjYDL/U6ceGfjz4m/YytJBhMoMJ+k bTmg== X-Gm-Message-State: AOAM531hpLnUL+NjNsbBpXXBPKjU4Jtsvj8BzQ4LknKjh/Mjkh7Trk2a 6lOOB+zx+ZRO02usFuNfLwI= X-Google-Smtp-Source: ABdhPJxjBcemZF7pOk0bDw93/xyavvdoBgy5KSErKlEXF7jcIMZKxLPrxujgLKirUPCxM28uUf7KKQ== X-Received: by 2002:ac8:59c8:0:b0:2de:5f4:7e84 with SMTP id f8-20020ac859c8000000b002de05f47e84mr17636277qtf.97.1646070124528; Mon, 28 Feb 2022 09:42:04 -0800 (PST) Received: from ?IPV6:2620:10d:c0a3:1407:7a37:3822:e32e:7ad3? ([2620:10d:c091:500::2:bc71]) by smtp.googlemail.com with ESMTPSA id y188-20020a3764c5000000b00609d2c739a6sm5250468qkb.49.2022.02.28.09.42.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 28 Feb 2022 09:42:03 -0800 (PST) Message-ID: <406a8452-08f5-19ac-8071-06ca842b2dea@acm.org> Date: Mon, 28 Feb 2022 12:42:02 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Content-Language: en-US To: Iain Sandoe From: Nathan Sidwell Subject: [coroutines] member slicing fix X-Spam-Status: No, score=-3039.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: , Cc: GCC Patches Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Iain, this is the first of 2 patches I needed on top of your github WIP series. It is against GCC-10, but AFAICT will also be needed on trunk. We were not descending into the object operand of COMPONENT_REF exprs, and end up bitcopying a field out of a temporary, leading to badness. The breaking out of replace_unary_preamble is merely coincidental simplification. I also think one needs to descend the second operand of COMPOUND_EXPR, but when I did that badness happens, probably because of the artificial COMPOUND_EXPRs that get generated, which you mentioned? The bug manifested itself in Folly, and it was too difficult to extract a testcase. nathan Summary: The ported coroutine patches still contained a bug. It could cause object slicing and bit copying when the compiler got confused about the lifetime of a member access. It needs to look at the object being accessed, not just the data member. diff --git a/9.x/src/gcc-10.x/gcc/cp/coroutines.cc b/9.x/src/gcc-10.x/gcc/cp/coroutines.cc index d0f292f2a6..ee004d5b98 100644 --- a/9.x/src/gcc-10.x/gcc/cp/coroutines.cc +++ b/9.x/src/gcc-10.x/gcc/cp/coroutines.cc @@ -3007,6 +3007,7 @@ struct coro_flattened_statement { static tree *find_unary_preamble (tree, bool&); static bool expr_result_needs_frame_temp (tree); + static tree replace_unary_preamble (tree unary, tree replace); tree get_flattened_statement (tree); void build_flattened_statement (); @@ -3565,15 +3566,15 @@ tree * coro_flattened_statement::find_unary_preamble (tree expr, bool& addr_taken) { tree *non_u_ptr = NULL; - bool done = false; /* Find the first non-unary operation in an expression. */ - while (!done) + for (;;) if (UNARY_CLASS_P (expr) - || (EXPRESSION_CLASS_P (expr) - && TREE_CODE_LENGTH (TREE_CODE (expr)) == 1) || TREE_CODE (expr) == INDIRECT_REF - || TREE_CODE (expr) == VIEW_CONVERT_EXPR) + || TREE_CODE (expr) == VIEW_CONVERT_EXPR + || TREE_CODE (expr) == COMPONENT_REF + || (EXPRESSION_CLASS_P (expr) + && TREE_CODE_LENGTH (TREE_CODE (expr)) == 1)) { if (TREE_CODE (expr) == STMT_EXPR || TREE_CODE (expr) == CLEANUP_POINT_EXPR) @@ -3582,14 +3583,24 @@ coro_flattened_statement::find_unary_preamble (tree expr, bool& addr_taken) if (TREE_CODE (expr) == ADDR_EXPR) addr_taken = true; non_u_ptr = &TREE_OPERAND (expr, 0); - expr = TREE_OPERAND (expr, 0); + expr = *non_u_ptr; } else - done = true; + break; return non_u_ptr; } +tree coro_flattened_statement::replace_unary_preamble (tree unary, tree replace) +{ + tree *slot = &unary; + while (*slot) + slot = &TREE_OPERAND (*slot, 0); + *slot = replace; + + return unary; +} + /* EXPR is an expression with a result that must persist across one or more suspension points, does this need a frame var? */ @@ -3639,15 +3650,7 @@ coro_flattened_statement::flatten_aggr_init (var_nest_node *t, tree expr, handle_call_param (t, &AGGR_INIT_EXPR_ARG (expr, p_num), "AI"); /* [A = ] revised_call. */ - if (unary_preamble) - { - gcc_checking_assert (!VOID_TYPE_P (TREE_TYPE (expr))); - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = expr; - expr = unary_preamble; - } + expr = replace_unary_preamble (unary_preamble, expr); if (!discarded) expr = build2_loc (loc, expr_code, TREE_TYPE (t->var), t->var, expr); @@ -3710,14 +3713,7 @@ coro_flattened_statement::flatten_await (var_nest_node *t, tree expr, flatten_await_inner (t, expr); /* [A = ] expr. */ - if (unary_preamble) - { - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = expr; - expr = unary_preamble; - } + expr = replace_unary_preamble (unary_preamble, expr); if (!discarded) expr = build2_loc (loc, expr_code, TREE_TYPE (t->var), t->var, expr); @@ -3790,14 +3786,7 @@ coro_flattened_statement::flatten_binary (var_nest_node *t, tree expr, flatten_expression (ins); /* Recurse into the second sub-expr... */ } - if (unary_preamble) - { - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = expr; - expr = unary_preamble; - } + expr = replace_unary_preamble (unary_preamble, expr); if (!discarded) expr = build2_loc (loc, expr_code, TREE_TYPE (t->var), t->var, expr); @@ -3890,14 +3879,7 @@ coro_flattened_statement::flatten_call (var_nest_node *t, tree expr, handle_call_param (t, &CALL_EXPR_ARG(expr, p_num), "CT"); /* [A = ] revised_call. */ - if (unary_preamble) - { - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = expr; - expr = unary_preamble; - } + expr = replace_unary_preamble (unary_preamble, expr); if (!discarded) expr = build2_loc (loc, expr_code, TREE_TYPE (t->var), t->var, expr); @@ -3933,16 +3915,7 @@ coro_flattened_statement::flatten_cleanup (var_nest_node *t, tree expr, flatten_expression (t->then_cl); /* Recurse into the sub-expr... */ /* [A = ] tmp. */ - if (unary_preamble) - { - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = tmp; - expr = unary_preamble; - } - else - expr = tmp; + expr = replace_unary_preamble (unary_preamble, tmp); if (!discarded) expr = build2_loc (loc, expr_code, TREE_TYPE (t->var), t->var, tmp); t->init = expr; @@ -3988,14 +3961,7 @@ coro_flattened_statement::flatten_compound (var_nest_node *t, tree expr, /* The second operand of the compound becomes the new expression. */ tree second = TREE_OPERAND (expr, 1); - if (unary_preamble) - { - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = second; - second = unary_preamble; - } + second = replace_unary_preamble (unary_preamble, second); if (!discarded) second = build2_loc (loc, expr_code, TREE_TYPE (t->var), t->var, second); @@ -4031,14 +3997,7 @@ coro_flattened_statement::flatten_constructor (var_nest_node *t, tree expr, } /* Reassemble the original. */ - if (unary_preamble) - { - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = expr; - expr = unary_preamble; - } + expr = replace_unary_preamble (unary_preamble, expr); if (!discarded) expr = build2_loc (loc, expr_code, TREE_TYPE (t->var), t->var, expr); @@ -4107,10 +4066,8 @@ coro_flattened_statement::flatten_target (var_nest_node *t, tree expr, /* There must be an await expression in here, so recurse with a 'bare' target expr. */ flatten_expression (ins); - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = var; + gcc_assert (unary_preamble); + replace_unary_preamble (unary_preamble, var); expr = unary_preamble; if (!discarded) expr = build2_loc (loc, expr_code, TREE_TYPE (t->var), t->var, expr); @@ -4225,21 +4182,10 @@ coro_flattened_statement::flatten_ternary (var_nest_node *t, tree expr, ? copy_node (unary_preamble) : unary_preamble; if (apply_uops_then) - { - tree x_op = t_op; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = then_; - then_ = t_op; - } + then_ = replace_unary_preamble (t_op, then_); + if (apply_uops_else) - { - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = else_; - else_ = unary_preamble; - } + else_ = replace_unary_preamble (unary_preamble, else_); } if (!discarded && apply_uops_then) @@ -4270,14 +4216,7 @@ coro_flattened_statement::flatten_ternary (var_nest_node *t, tree expr, { /* The only replacement is the condition, so that we preserve the ternary and any unary preamble. */ - if (unary_preamble) - { - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = expr; - expr = unary_preamble; - } + expr = replace_unary_preamble (unary_preamble, expr); if (!discarded) expr = build2_loc (loc, expr_code, res_type, t->var, expr); } @@ -4361,10 +4300,7 @@ coro_flattened_statement::flatten_truth_if (var_nest_node *t, tree expr, ins->then_cl = then_cl; ins->synthesized_var_p = synthetic; /* x. */ - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = var; + replace_unary_preamble (unary_preamble, var); if (!discarded) init = build2_loc (loc, expr_code, expr_type, t->var, unary_preamble); else @@ -4454,11 +4390,7 @@ coro_flattened_statement::flatten_expression (var_nest_node *t) else if (unary_preamble) { /* Undo the split. */ - tree x_op = unary_preamble; - while (TREE_OPERAND (x_op, 0)) - x_op = TREE_OPERAND (x_op, 0); - TREE_OPERAND (x_op, 0) = expr; - expr = unary_preamble; + expr = replace_unary_preamble (unary_preamble, expr); } if (!discarded) expr = build2_loc (loc, expr_code, TREE_TYPE (t->var), t->var, expr); -- 2.30.2