From patchwork Wed Apr 5 14:02:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 1765415 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.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=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256 header.s=google header.b=Fj4eayMf; dkim-atps=neutral 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 ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Ps5xx5hR2z1yZT for ; Thu, 6 Apr 2023 00:05:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 36ABC3854143 for ; Wed, 5 Apr 2023 14:05:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 3C82C3858C2C for ; Wed, 5 Apr 2023 14:05:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3C82C3858C2C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wm1-x333.google.com with SMTP id d11-20020a05600c3acb00b003ef6e6754c5so18370637wms.5 for ; Wed, 05 Apr 2023 07:05:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703520; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=FP0FHKjiKLOOhKCwIKB/P6DVVdwJ963gOaP8iS2bk6I=; b=Fj4eayMfqZd7abUGWOTz9M4Bgs4lzRMIjg9DdqrM6Qh8g3opg5SA8mTR6JF1TvH++e KZoB453DVUH8ZgAWjTqgTrEDT/6FLF57sP5+CHv6aM/wDZMf37ZP0neE+Liftw62sX0Z cjVmPoDCqdRjDACfvPwsqLV9vTmbTsBK0U9G0CoYE7xqJPgMMn62LYtwH1zzPtQ2FyfP jzMvYPo1E7Lf0gbKaZi4XJvMnE3ZE+5rqSn08r5uiR9KNdGYGz+/kncM44hMmsKuY0FE ZDdf6pCbdc9Hwse2U/qAEQ+HSIXDwYgIiew87eOcWYWFLLhJPARwIPEZGoOuta4+AXq3 IWbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703520; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=FP0FHKjiKLOOhKCwIKB/P6DVVdwJ963gOaP8iS2bk6I=; b=QWim909xNbjtxTkm6uNFkNa5iuH3ZfT0qBrOMejKeaT6B5OOBtRRLegUts+uMsZTxv 86JL73fLSfS+8AUZFUYp5ZSPADhbuHR0rr2nd1GQXqAbooh/l3uGBCHEuLld4a8pm1kv k3P1J4q1cWiDW/tA04mQn8YeN4WfprJ2zsYYUczycrvraD3VBQrplvZ1A7K5nGXEFoP7 KrDalmEmPxyvjfQroO7TwuquduRGClOkMpo7U2XpktTPPEzquCMsZ0sCMroQq09Hzm9a T6RI7gnrYJQJQNSNPFo4NAaCTfOWAcP64zusMijkC7B5PMC3fI4fk8puUvszrFpZYzsf CFLQ== X-Gm-Message-State: AAQBX9cNcYef15N3RcB/Xy5GZlDHsqQQHwMGc6uIgKA2WWM2XZjtvx61 kmxE73Fb573bvi8FDx8PuALO7tbPWH+bnM+uGQ== X-Google-Smtp-Source: AKy350Y8E633UZ0A7mrlbviq7c6shzqvTT9G/SPn4AF1+z8v2AvZuXx9VCqS3WskMHjapcBg5C9u5g== X-Received: by 2002:a1c:f206:0:b0:3f0:5fda:c95f with SMTP id s6-20020a1cf206000000b003f05fdac95fmr3773962wmc.26.1680703520701; Wed, 05 Apr 2023 07:05:20 -0700 (PDT) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b003edddae1068sm2330150wmb.9.2023.04.05.07.05.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:05:20 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Prajwal S N Subject: [committed 02/88] gccrs: unsafe: check use of `target_feature` attribute Date: Wed, 5 Apr 2023 16:02:45 +0200 Message-Id: <20230405140411.3016563-3-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405140411.3016563-1-arthur.cohen@embecosm.com> References: <20230405140411.3016563-1-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable 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: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" From: Prajwal S N The `target_feature` attribute is for conditional compilation and may or may not compile on all platforms. Using it requires an unsafe function or block. gcc/rust/ChangeLog: * checks/errors/rust-unsafe-checker.cc (check_target_attr): New function. (UnsafeChecker::check_function_attr): Call into `check_target_attr`. (UnsafeChecker::visit): Check for target_feature attributes. * checks/errors/rust-unsafe-checker.h: Add declarations. * util/rust-attributes.cc: Add attribute. gcc/testsuite/ChangeLog: * rust/compile/unsafe11.rs: New test. Signed-off-by: Prajwal S N --- gcc/rust/checks/errors/rust-unsafe-checker.cc | 29 ++++++++++++++++++- gcc/rust/checks/errors/rust-unsafe-checker.h | 5 ++++ gcc/rust/util/rust-attributes.cc | 3 ++ gcc/testsuite/rust/compile/unsafe11.rs | 8 +++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/rust/compile/unsafe11.rs diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index 94808632c04..3c369a2a81a 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -179,6 +179,31 @@ UnsafeChecker::check_function_call (HirId node_id, Location locus) locus); } +static void +check_target_attr (HIR::Function *fn, Location locus) +{ + if (std::any_of (fn->get_outer_attrs ().begin (), + fn->get_outer_attrs ().end (), + [] (const AST::Attribute &attr) { + return attr.get_path ().as_string () == "target_feature"; + })) + rust_error_at (locus, + "call to function with %<#[target_feature]%> requires " + "unsafe function or block"); +} + +void +UnsafeChecker::check_function_attr (HirId node_id, Location locus) +{ + if (unsafe_context.is_in_context ()) + return; + + auto maybe_fn = mappings.lookup_hir_item (node_id); + + if (maybe_fn && maybe_fn->get_item_kind () == Item::ItemKind::Function) + check_target_attr (static_cast (maybe_fn), locus); +} + void UnsafeChecker::visit (Lifetime &) {} @@ -398,11 +423,13 @@ UnsafeChecker::visit (CallExpr &expr) rust_assert (mappings.lookup_node_to_hir (ref_node_id, &definition_id)); - // At this point we have the function's HIR Id. There are two checks we + // At this point we have the function's HIR Id. There are three checks we // must perform: // 1. The function is an unsafe one // 2. The function is an extern one + // 3. The function is marked with a target_feature attribute check_function_call (definition_id, expr.get_locus ()); + check_function_attr (definition_id, expr.get_locus ()); if (expr.has_params ()) for (auto &arg : expr.get_arguments ()) diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.h b/gcc/rust/checks/errors/rust-unsafe-checker.h index 9df44db06a9..2abd3a62351 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.h +++ b/gcc/rust/checks/errors/rust-unsafe-checker.h @@ -46,6 +46,11 @@ private: */ void check_function_call (HirId node_id, Location locus); + /** + * Check if any unsafe attributes are present on a function + */ + void check_function_attr (HirId node_id, Location locus); + StackedContexts unsafe_context; Resolver::TypeCheckContext &context; diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index 33f2c932d09..0458f6902b7 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -41,6 +41,9 @@ static const BuiltinAttrDefinition __definitions[] {"repr", CODE_GENERATION}, {"path", EXPANSION}, {"macro_use", NAME_RESOLUTION}, + // FIXME: This is not implemented yet, see + // https://github.com/Rust-GCC/gccrs/issues/1475 + {"target_feature", CODE_GENERATION}, // From now on, these are reserved by the compiler and gated through // #![feature(rustc_attrs)] {"rustc_inherit_overflow_checks", CODE_GENERATION}}; diff --git a/gcc/testsuite/rust/compile/unsafe11.rs b/gcc/testsuite/rust/compile/unsafe11.rs new file mode 100644 index 00000000000..c87902fcd5f --- /dev/null +++ b/gcc/testsuite/rust/compile/unsafe11.rs @@ -0,0 +1,8 @@ +#[target_feature(sse)] +fn foo() { + let a: usize = 0; +} + +fn main() { + foo() // { dg-error "requires unsafe function or block" } +}