From patchwork Thu Jan 11 20:34:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Drozodv X-Patchwork-Id: 187487 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1708214dyi; Thu, 11 Jan 2024 12:35:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEFrCUGprRdpDN79LTQzmEIR4wV/fCo/4zG8t18xeB7Pj0+Oy6p7Irb1lYoTbGzFTL6OKgF X-Received: by 2002:a05:6870:64ac:b0:204:45d7:2e86 with SMTP id cz44-20020a05687064ac00b0020445d72e86mr368100oab.110.1705005328526; Thu, 11 Jan 2024 12:35:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705005328; cv=pass; d=google.com; s=arc-20160816; b=D/mRpprLrHkEcw4LbWHTj4JtzJ22BNEd8P6OoVySLPqQE3jSGjPnUjjLVZaba9G9k1 3DRuA6K9kJUOL0O8NA83lm9riW7InIKhsrDlZfsSe6PSsosoTRnjEhP7N9o7hYHGXlP+ CHiInA6GyvGh2Kqq4ZDkPvjEvEJ/jfxI4DPHidFh262Vh7YsCCWfje1OcSwyI27KUOS1 4DsaE9dSEl+vuvDrQ06NjfgqCI6R9fSOw32g3RddlYgJm8hMbesD5Gt40CYBR2pyzFxx YH72yBHjwJEspvHTdk0ioX9yAiongMiZg2o4cEmbqQbucCEKoPDTT1LHWbhdtbTeG5jT u6yg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:cc:to:subject:message-id:date :from:mime-version:dkim-signature:arc-filter:dmarc-filter :delivered-to; bh=UlQTp+9Zvn02A6tNy8qitVFkDyJv1RKWm4ZeJcq3+7k=; fh=MAobXbnKNmKc8RGpN2Bx5dqwBbaj4ITN0VQh44seonM=; b=EMzR8mshR200ZbBA4mDVfq86zUMcmO5OKRDEkc4ZKcwi0tlVBpwL05FdYTz6gzYk6V C4+HWXHO9X6V9AgsGc3CI85IPYgxEJIIwGg2fAVR0ef6damtMrlfhPrhnbl2A+1h45kH RCPb43ZNhINsq1UyZtETsQF+0cVDCk1fjeg8Gr7lS5qEEwQHtSEPbexK9czNyUDjHLmm ZFp5jTgF83Xx2h93oQ79bVqOke7xKjPG0igCS3g0AoNh/nw3uliji9SaXB95UYSiJ+gZ eCQTowow+gpEmITDNxAMFsqg4L3q2ifk6Ldp/k/XcTF8gz+9sU581GuE1RmK1gvUeEz8 P8ow== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=GULBsZtg; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id t9-20020a05620a0b0900b007833f1d19a8si1506234qkg.569.2024.01.11.12.35.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 12:35:28 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=GULBsZtg; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 37B5B385803D for ; Thu, 11 Jan 2024 20:35:28 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id B6E453858439 for ; Thu, 11 Jan 2024 20:34:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B6E453858439 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B6E453858439 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::22f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705005284; cv=none; b=Op8ZQ7hvxXQzlOk0v8fXlLTF3SSfoT7acMAnTVf9UeFTsEDdtu5r5iCNy9lRmh69nt7I9bGEQBgiTEueGLL065AbVh5ft2vHQhuONmFWpBvJg0aFMJkZnpiWoh3cf5jrMBNy8tUYfNAYpIU78m7ZwBZNxpXDDN3bHROnBzujG7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705005284; c=relaxed/simple; bh=ADoZYW1SxgOE35xqmE9BFSSDr/vtxPEiMx6uXhMWUQM=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=ja8uiXYMBXGWB7jjg5vlyo27BZ6+V3YtIrw+U0ZgFFZvByXj7O58VbrgA99jkzbPtEi4MmzE4r9CQsBL4Jwl+S45DjslFKQfDxwoQyOKetcdURvLbrRVnUsA5gstbDEyCjdPFEu3CpIUl0FtYpuLQDpjtdRXuXTGDo47ZQDSm4A= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lj1-x22f.google.com with SMTP id 38308e7fff4ca-2cd8b661cb8so9697621fa.0 for ; Thu, 11 Jan 2024 12:34:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705005281; x=1705610081; darn=gcc.gnu.org; h=cc:to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=UlQTp+9Zvn02A6tNy8qitVFkDyJv1RKWm4ZeJcq3+7k=; b=GULBsZtgnFEY9zzpLJJkNtba4lsXscVAdVoO2CXsJtykSnT8rrUn5+mbAxsf6PYRvV WQl+bMBaBKj/5BOxeOin3ziL9XJwpr+GjDgby/8kKzE92og/O9AjRKPyDAB741THojW7 G2PjarvTAugOnyhZPzzlih/5u+CP80e1mm311C2hnAV232O+lHYpxRql9Qu4M0iKkTf5 ZP8s/5rlz+8ejKycyit+sEgTXeCZ7wygl7zW7vckws9hZXSXeuzRy/SbJursO0pV/ksZ M8HVO73FNSqEqg281QmB5sWphHcz2IgeMXe9RZa3BUZxOzeTcVGjzZgZhAoE8VnzKzoK 7vBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705005281; x=1705610081; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=UlQTp+9Zvn02A6tNy8qitVFkDyJv1RKWm4ZeJcq3+7k=; b=UpSvjSqZ4ceeLiNpEjK4KPcOLieFC8y2Z4zsUoy8uURlOt3/21Yo3MyAt8GMImBVdU T1QcYjGIc481zk2rJHa3z6CFpTKJSqgb3GthX9lkXY4W2F9RCVI/HCUzBOQfQNxeRQkf 9wJUmJhbv8zImLNHFKP7WCX+DdoMURDQO7DRbR0i2xtc+d1kL0pzsc4gwHoZirhxslKh Rzw6GNtanJ5e7vbr5KSrZiZbCEW+GmUMSq5UN5hwUlRLjiO521e4Rj/jM7IGingj3lEb GDJilRZY4PZ4U/tZrjgDjMfGUspzMftVCCnieKvmhASv3Gsq6QTnPSBE4H86lK+HS8Jt YW5Q== X-Gm-Message-State: AOJu0YxNiIU/z9VHkNzrYESgF6BanSvjm3ja+0zaIEkHxp9J3DyeiELf mJYim4evvagCaNIPzfn0jOQAWWpCeT8Dk6m2bUmbsunlSrOoHA== X-Received: by 2002:a05:6512:38c7:b0:50d:1c5e:7982 with SMTP id p7-20020a05651238c700b0050d1c5e7982mr66825lft.182.1705005280911; Thu, 11 Jan 2024 12:34:40 -0800 (PST) MIME-Version: 1.0 From: Dmitry Drozodv Date: Thu, 11 Jan 2024 23:34:33 +0300 Message-ID: Subject: [PATCH v2] c++: side effect in nullptr_t conversion fix To: jakub@redhat.com Cc: gcc-patches@gcc.gnu.org X-Spam-Status: No, score=-0.4 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, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPAM_FROM, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787827667722904079 X-GMAIL-MSGID: 1787827667722904079 Hello, You are absolutely right, we can't throw all side-effects away. Example: ```c++ auto __attribute__ ((noinline)) bar() { volatile int* b = (int *)0xff; *b = 10; volatile auto n = nullptr; return n; // Problem (1) } int* foo_2() { volatile auto a = bar(); // Problem (2) return a; } ``` There are 2 additional places, where std::nullptr_t causes side effects. (1) nullptr_t identical conversion produces: mov QWORD PTR [rsp-8], 0 mov rax, QWORD PTR [rsp-8] // load n ret (2) Initialization by not constant expression: call bar() mov QWORD PTR [rsp+8], rax mov rax, QWORD PTR [rsp+8] xor eax, eax diff --git a/gcc/cp/cvt.cc b/gcc/cp/cvt.cc index cbed847b343..24c1ceb4404 100644 --- a/gcc/cp/cvt.cc +++ b/gcc/cp/cvt.cc @@ -218,8 +218,11 @@ cp_convert_to_pointer (tree type, tree expr, bool dofold, ? build_int_cst_type (type, -1) : build_int_cst (type, 0)); - return (TREE_SIDE_EFFECTS (expr) - ? build2 (COMPOUND_EXPR, type, expr, val) : val); + /* C++ [conv.lval]p3: + If T is cv std::nullptr_t, the result is a null pointer constant. */ + return ((TREE_SIDE_EFFECTS (expr) && !CONVERT_EXPR_P (expr)) + ? build2 (COMPOUND_EXPR, type, expr, val) + : val); } else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form)) { @@ -743,8 +746,9 @@ ocp_convert (tree type, tree expr, int convtype, int flags, { if (complain & tf_warning) maybe_warn_zero_as_null_pointer_constant (e, loc); - - if (!TREE_SIDE_EFFECTS (e)) + /* C++ [conv.lval]p3: + Convert expr from nullptr to nullptr doesn't have side effect. */ + if (!TREE_SIDE_EFFECTS (e) || CONVERT_EXPR_P (expr)) return nullptr_node; } diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index ac0fefa24f2..9c7866d319f 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -770,6 +770,15 @@ split_nonconstant_init (tree dest, tree init) && array_of_runtime_bound_p (TREE_TYPE (dest))) code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false, /*from array*/1, tf_warning_or_error); + else if (TREE_CODE (TREE_TYPE(init)) == NULLPTR_TYPE) + { + /* C++ [conv.lval]p3: + If T is cv std::nullptr_t, the result is a null pointer constant. */ + tree val = build_int_cst(TREE_TYPE(dest), 0); + tree ie = cp_build_init_expr(dest, val); + // remain expr with side effect + code = add_stmt_to_compound(init, ie); + } else code = cp_build_init_expr (dest, init);