From patchwork Wed Nov 30 05:21:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: liuhongt X-Patchwork-Id: 27596 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp737820wrr; Tue, 29 Nov 2022 21:24:17 -0800 (PST) X-Google-Smtp-Source: AA0mqf5Q7O24U0cpyD9evO+6Jvq8CNobrXWVZ7OAkRLxSCvAl4ElDrnAZstXy20owSwJvPxDnktW X-Received: by 2002:a05:6402:d6:b0:458:b42e:46e6 with SMTP id i22-20020a05640200d600b00458b42e46e6mr54769352edu.375.1669785856960; Tue, 29 Nov 2022 21:24:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669785856; cv=none; d=google.com; s=arc-20160816; b=gvd1eBdCb8RDZ6lBepIjVnwq9kXQBIMpE7ufzLpv/JLokm+odWj5a6Absehjl3oeXi jy1HIbyopcrdV+q4DM7qDSmipfzT2kMlQlgNHsApneQz4Z2TIXUv+Z1B0xSxTe4Uocwc kIpRhDvWi67z6d4x72dqxdaCz8mQgvP2jfrKllpVw4SYmhNoLbA2rfVt9d6sS99ny8Qp zfGddYi3JFeVfUEa5UZnESPyihB7c80sPTRHSUxn518SrrFmDUjZyQfK+aZYvvzn9QJm Jp3rjW91H8zbv5/xPMhsKonWUj4J6KT4HMpJEOvVCprXtI5JQHMD44nIegurFr5wMctd EgRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=qR4dQX1udDOKqbetjd4Uz3W+9rM7mX7WKQ8YfJdIVkw=; b=ZdSqlD3ArBFvFB3bQiQfTFwMIh0BwmkpYTYjS5xtw0VeMQ9RIYAGIYM2WSHTtGj+FJ Nlu3GrqMEMlSeYafN2mOTfXWotflVXojS9I8CEoTZSt0q9Z8n7fsv1uUjUhrsaLHasZd Q60NNwAv8/1bH0w8xMwmOudl2I8WAaeflb/ZOqRW3iDbTuwPFdW9rzsscsvS8MiLPe1h 5AW4aSkZEQ3Ech2Q6hrTVMd6qiql5lxfxvu0BV3CP82zqqcxHOPnPUNXzM+jbBW1sxos s+3/SCzwlbxPcYMuu1RTI3f0a2VdqCvMZ9JGC1M39vTywejyadtA3O5spJRTxOQ5mFMq sHmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=rTz9ZQIL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id nb38-20020a1709071ca600b00791a67e4c00si578311ejc.835.2022.11.29.21.24.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 21:24:16 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=rTz9ZQIL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 381403858017 for ; Wed, 30 Nov 2022 05:24:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 381403858017 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669785853; bh=qR4dQX1udDOKqbetjd4Uz3W+9rM7mX7WKQ8YfJdIVkw=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=rTz9ZQILJRdEg9Qy6e9pMQe9ulaCTNOROP9OlFazgaqfknpEe4V6T6288Yts04u88 GbuvvH45DnM1WvE66dE4OPKIh3Pxp8NQTT3m1j212vKNLNAbOrDFStIHPI0v4kBHWR NeL4QVXJjXLA+5vxWspwuZ8GbMfxTOfT3W4dsufE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by sourceware.org (Postfix) with ESMTPS id 7C30A3858D35 for ; Wed, 30 Nov 2022 05:23:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7C30A3858D35 X-IronPort-AV: E=McAfee;i="6500,9779,10546"; a="302912669" X-IronPort-AV: E=Sophos;i="5.96,205,1665471600"; d="scan'208";a="302912669" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2022 21:23:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10546"; a="889148946" X-IronPort-AV: E=Sophos;i="5.96,205,1665471600"; d="scan'208";a="889148946" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by fmsmga006.fm.intel.com with ESMTP; 29 Nov 2022 21:23:14 -0800 Received: from shliclel320.sh.intel.com (shliclel320.sh.intel.com [10.239.240.127]) by shvmail03.sh.intel.com (Postfix) with ESMTP id 34CF01005638; Wed, 30 Nov 2022 13:23:14 +0800 (CST) To: gcc-patches@gcc.gnu.org Cc: crazylht@gmail.com, hjl.tools@gmail.com, ubizjak@gmail.com Subject: [PATCH 1/2 V2] Implement hwasan target_hook. Date: Wed, 30 Nov 2022 13:21:14 +0800 Message-Id: <20221130052114.10229-1-hongtao.liu@intel.com> X-Mailer: git-send-email 2.18.1 In-Reply-To: <20221111012631.76776-2-hongtao.liu@intel.com> References: <20221111012631.76776-2-hongtao.liu@intel.com> X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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: liuhongt via Gcc-patches From: liuhongt Reply-To: liuhongt Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749161176648272723?= X-GMAIL-MSGID: =?utf-8?q?1750897374546068141?= Update in V2: Add documentation for -mlam={none,u48,u57} to x86 options in invoke.texi. gcc/ChangeLog: * doc/invoke.texi (x86 options): Document -mlam={none,u48,u57}. * config/i386/i386-opts.h (enum lam_type): New enum. * config/i386/i386.c (ix86_memtag_can_tag_addresses): New. (ix86_memtag_set_tag): Ditto. (ix86_memtag_extract_tag): Ditto. (ix86_memtag_add_tag): Ditto. (ix86_memtag_tag_size): Ditto. (ix86_memtag_untagged_pointer): Ditto. (TARGET_MEMTAG_CAN_TAG_ADDRESSES): New. (TARGET_MEMTAG_ADD_TAG): Ditto. (TARGET_MEMTAG_SET_TAG): Ditto. (TARGET_MEMTAG_EXTRACT_TAG): Ditto. (TARGET_MEMTAG_UNTAGGED_POINTER): Ditto. (TARGET_MEMTAG_TAG_SIZE): Ditto. (IX86_HWASAN_SHIFT): Ditto. (IX86_HWASAN_TAG_SIZE): Ditto. * config/i386/i386-expand.c (ix86_expand_call): Untag code pointer. * config/i386/i386-options.c (ix86_option_override_internal): Error when enable -mlam=[u48|u57] for 32-bit code. * config/i386/i386.opt: Add -mlam=[none|u48|u57]. * config/i386/i386-protos.h (ix86_memtag_untagged_pointer): Declare. (ix86_memtag_can_tag_addresses): Ditto. --- gcc/config/i386/i386-expand.cc | 12 ++++ gcc/config/i386/i386-options.cc | 3 + gcc/config/i386/i386-opts.h | 6 ++ gcc/config/i386/i386-protos.h | 2 + gcc/config/i386/i386.cc | 123 ++++++++++++++++++++++++++++++++ gcc/config/i386/i386.opt | 16 +++++ gcc/doc/invoke.texi | 9 ++- 7 files changed, 170 insertions(+), 1 deletion(-) diff --git a/gcc/config/i386/i386-expand.cc b/gcc/config/i386/i386-expand.cc index d26e7e41445..0e94782165a 100644 --- a/gcc/config/i386/i386-expand.cc +++ b/gcc/config/i386/i386-expand.cc @@ -92,6 +92,7 @@ along with GCC; see the file COPYING3. If not see #include "i386-options.h" #include "i386-builtins.h" #include "i386-expand.h" +#include "asan.h" /* Split one or more double-mode RTL references into pairs of half-mode references. The RTL can be REG, offsettable MEM, integer constant, or @@ -9438,6 +9439,17 @@ ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1, fnaddr = gen_rtx_MEM (QImode, copy_to_mode_reg (word_mode, fnaddr)); } + /* PR100665: Hwasan may tag code pointer which is not supported by LAM, + mask off code pointers here. + TODO: also need to handle indirect jump. */ + if (ix86_memtag_can_tag_addresses () && !fndecl + && sanitize_flags_p (SANITIZE_HWADDRESS)) + { + rtx untagged_addr = ix86_memtag_untagged_pointer (XEXP (fnaddr, 0), + NULL_RTX); + fnaddr = gen_rtx_MEM (QImode, untagged_addr); + } + call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1); if (retval) diff --git a/gcc/config/i386/i386-options.cc b/gcc/config/i386/i386-options.cc index 44dcccb0a73..25f21ac2a49 100644 --- a/gcc/config/i386/i386-options.cc +++ b/gcc/config/i386/i386-options.cc @@ -2033,6 +2033,9 @@ ix86_option_override_internal (bool main_args_p, if (TARGET_UINTR && !TARGET_64BIT) error ("%<-muintr%> not supported for 32-bit code"); + if (ix86_lam_type && !TARGET_LP64) + error ("%<-mlam=%> option: [u48|u57] not supported for 32-bit code"); + if (!opts->x_ix86_arch_string) opts->x_ix86_arch_string = TARGET_64BIT_P (opts->x_ix86_isa_flags) diff --git a/gcc/config/i386/i386-opts.h b/gcc/config/i386/i386-opts.h index 8f71e89fa9a..d3bfeed0af2 100644 --- a/gcc/config/i386/i386-opts.h +++ b/gcc/config/i386/i386-opts.h @@ -128,4 +128,10 @@ enum harden_sls { harden_sls_all = harden_sls_return | harden_sls_indirect_jmp }; +enum lam_type { + lam_none = 0, + lam_u48 = 1, + lam_u57 +}; + #endif diff --git a/gcc/config/i386/i386-protos.h b/gcc/config/i386/i386-protos.h index e136f6ec175..abd123c9efc 100644 --- a/gcc/config/i386/i386-protos.h +++ b/gcc/config/i386/i386-protos.h @@ -228,6 +228,8 @@ extern void ix86_expand_atomic_fetch_op_loop (rtx, rtx, rtx, enum rtx_code, extern void ix86_expand_cmpxchg_loop (rtx *, rtx, rtx, rtx, rtx, rtx, bool, rtx_code_label *); extern rtx ix86_expand_fast_convert_bf_to_sf (rtx); +extern rtx ix86_memtag_untagged_pointer (rtx, rtx); +extern bool ix86_memtag_can_tag_addresses (void); #ifdef TREE_CODE extern void init_cumulative_args (CUMULATIVE_ARGS *, tree, rtx, tree, int); diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index 95babd93c9d..518cc9ffd1f 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -24274,6 +24274,111 @@ ix86_push_rounding (poly_int64 bytes) return ROUND_UP (bytes, UNITS_PER_WORD); } +/* Use 8 bits metadata start from bit48 for LAM_U48, + 6 bits metadat start from bit57 for LAM_U57. */ +#define IX86_HWASAN_SHIFT (ix86_lam_type == lam_u48 \ + ? 48 \ + : (ix86_lam_type == lam_u57 ? 57 : 0)) +#define IX86_HWASAN_TAG_SIZE (ix86_lam_type == lam_u48 \ + ? 8 \ + : (ix86_lam_type == lam_u57 ? 6 : 0)) + +/* Implement TARGET_MEMTAG_CAN_TAG_ADDRESSES. */ +bool +ix86_memtag_can_tag_addresses () +{ + return ix86_lam_type != lam_none && TARGET_LP64; +} + +/* Implement TARGET_MEMTAG_TAG_SIZE. */ +unsigned char +ix86_memtag_tag_size () +{ + return IX86_HWASAN_TAG_SIZE; +} + +/* Implement TARGET_MEMTAG_SET_TAG. */ +rtx +ix86_memtag_set_tag (rtx untagged, rtx tag, rtx target) +{ + /* default_memtag_insert_random_tag may + generate tag with value more than 6 bits. */ + if (ix86_lam_type == lam_u57) + { + unsigned HOST_WIDE_INT and_imm + = (HOST_WIDE_INT_1U << IX86_HWASAN_TAG_SIZE) - 1; + + emit_insn (gen_andqi3 (tag, tag, GEN_INT (and_imm))); + } + tag = expand_simple_binop (Pmode, ASHIFT, tag, + GEN_INT (IX86_HWASAN_SHIFT), NULL_RTX, + /* unsignedp = */1, OPTAB_WIDEN); + rtx ret = expand_simple_binop (Pmode, IOR, untagged, tag, target, + /* unsignedp = */1, OPTAB_DIRECT); + return ret; +} + +/* Implement TARGET_MEMTAG_EXTRACT_TAG. */ +rtx +ix86_memtag_extract_tag (rtx tagged_pointer, rtx target) +{ + rtx tag = expand_simple_binop (Pmode, LSHIFTRT, tagged_pointer, + GEN_INT (IX86_HWASAN_SHIFT), target, + /* unsignedp = */0, + OPTAB_DIRECT); + rtx ret = gen_reg_rtx (QImode); + /* Mask off bit63 when LAM_U57. */ + if (ix86_lam_type == lam_u57) + { + unsigned HOST_WIDE_INT and_imm + = (HOST_WIDE_INT_1U << IX86_HWASAN_TAG_SIZE) - 1; + emit_insn (gen_andqi3 (ret, gen_lowpart (QImode, tag), + gen_int_mode (and_imm, QImode))); + } + else + emit_move_insn (ret, gen_lowpart (QImode, tag)); + return ret; +} + +/* The default implementation of TARGET_MEMTAG_UNTAGGED_POINTER. */ +rtx +ix86_memtag_untagged_pointer (rtx tagged_pointer, rtx target) +{ + /* Leave bit63 alone. */ + rtx tag_mask = gen_int_mode (((HOST_WIDE_INT_1U << IX86_HWASAN_SHIFT) + + (HOST_WIDE_INT_1U << 63) - 1), + Pmode); + rtx untagged_base = expand_simple_binop (Pmode, AND, tagged_pointer, + tag_mask, target, true, + OPTAB_DIRECT); + gcc_assert (untagged_base); + return untagged_base; +} + +/* Implement TARGET_MEMTAG_ADD_TAG. */ +rtx +ix86_memtag_add_tag (rtx base, poly_int64 offset, unsigned char tag_offset) +{ + rtx base_tag = gen_reg_rtx (QImode); + rtx base_addr = gen_reg_rtx (Pmode); + rtx tagged_addr = gen_reg_rtx (Pmode); + rtx new_tag = gen_reg_rtx (QImode); + unsigned HOST_WIDE_INT and_imm + = (HOST_WIDE_INT_1U << IX86_HWASAN_SHIFT) - 1; + + /* When there's "overflow" in tag adding, + need to mask the most significant bit off. */ + emit_move_insn (base_tag, ix86_memtag_extract_tag (base, NULL_RTX)); + emit_move_insn (base_addr, + ix86_memtag_untagged_pointer (base, NULL_RTX)); + emit_insn (gen_add2_insn (base_tag, gen_int_mode (tag_offset, QImode))); + emit_move_insn (new_tag, base_tag); + emit_insn (gen_andqi3 (new_tag, new_tag, gen_int_mode (and_imm, QImode))); + emit_move_insn (tagged_addr, + ix86_memtag_set_tag (base_addr, new_tag, NULL_RTX)); + return plus_constant (Pmode, tagged_addr, offset); +} + /* Target-specific selftests. */ #if CHECKING_P @@ -25068,6 +25173,24 @@ ix86_libgcc_floating_mode_supported_p # define TARGET_ASM_RELOC_RW_MASK ix86_reloc_rw_mask #endif +#undef TARGET_MEMTAG_CAN_TAG_ADDRESSES +#define TARGET_MEMTAG_CAN_TAG_ADDRESSES ix86_memtag_can_tag_addresses + +#undef TARGET_MEMTAG_ADD_TAG +#define TARGET_MEMTAG_ADD_TAG ix86_memtag_add_tag + +#undef TARGET_MEMTAG_SET_TAG +#define TARGET_MEMTAG_SET_TAG ix86_memtag_set_tag + +#undef TARGET_MEMTAG_EXTRACT_TAG +#define TARGET_MEMTAG_EXTRACT_TAG ix86_memtag_extract_tag + +#undef TARGET_MEMTAG_UNTAGGED_POINTER +#define TARGET_MEMTAG_UNTAGGED_POINTER ix86_memtag_untagged_pointer + +#undef TARGET_MEMTAG_TAG_SIZE +#define TARGET_MEMTAG_TAG_SIZE ix86_memtag_tag_size + static bool ix86_libc_has_fast_function (int fcode ATTRIBUTE_UNUSED) { #ifdef OPTION_GLIBC diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt index aa8574e6b71..fb4e57ada7c 100644 --- a/gcc/config/i386/i386.opt +++ b/gcc/config/i386/i386.opt @@ -1250,3 +1250,19 @@ Support RAOINT built-in functions and code generation. munroll-only-small-loops Target Var(ix86_unroll_only_small_loops) Init(0) Save Enable conservative small loop unrolling. + +mlam= +Target RejectNegative Joined Enum(lam_type) Var(ix86_lam_type) Init(lam_none) +-mlam=[none|u48|u57] Instrument meta data position in user data pointers. + +Enum +Name(lam_type) Type(enum lam_type) UnknownError(unknown lam type %qs) + +EnumValue +Enum(lam_type) String(none) Value(lam_none) + +EnumValue +Enum(lam_type) String(u48) Value(lam_u48) + +EnumValue +Enum(lam_type) String(u57) Value(lam_u57) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index e312b5cef3d..dcecf6849b0 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -1448,7 +1448,7 @@ See RS/6000 and PowerPC Options. -mindirect-branch=@var{choice} -mfunction-return=@var{choice} @gol -mindirect-branch-register -mharden-sls=@var{choice} @gol -mindirect-branch-cs-prefix -mneeded -mno-direct-extern-access @gol --munroll-only-small-loops} +-munroll-only-small-loops -mlam=@var{choice}} @emph{x86 Windows Options} @gccoptlist{-mconsole -mcygwin -mno-cygwin -mdll @gol @@ -33857,6 +33857,13 @@ Controls conservative small loop unrolling. It is default enabled by O2, and unrolls loop with less than 4 insns by 1 time. Explicit -f[no-]unroll-[all-]loops would disable this flag to avoid any unintended unrolling behavior that user does not want. + +@item -mlam=@var{choice} +@opindex mlam +LAM(linear-address masking) allows special bits in the pointer to be used +for metadata. The default is @samp{none}. With @samp{u48}, pointer bits in +positions 62:48 can be used for metadata; With @samp{u57}, pointer bits in +positions 62:57 can be used for metadata. @end table @node x86 Windows Options