From patchwork Thu Aug 5 13:13:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 44584 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 172F839AF4F9 for ; Thu, 5 Aug 2021 13:15:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 172F839AF4F9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628169331; bh=G2mVBU3OHqn9CDFu+4B69np+VD5MPSOqxzu8oZVl+c4=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=JOBjbsKZv/6T4Xq58vyTrx2lM6Bjuej22Qu/r22ATYeWl4ZO3WZWAA2Mij5cHcjag lRN7AfOrKpKDy/9wiXNqp2KnURiwaJQMecciQydFHdWhSqUQV/9Ok5SwV1boVW2tST E1Qo+8VxbHdFTfejNLYSDG5R3FMSvVPBRUNf32KM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by sourceware.org (Postfix) with ESMTPS id 86E0C39DC4C3 for ; Thu, 5 Aug 2021 13:14:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 86E0C39DC4C3 Received: by mail-pj1-x1029.google.com with SMTP id o44-20020a17090a0a2fb0290176ca3e5a2fso9316208pjo.1 for ; Thu, 05 Aug 2021 06:14:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=G2mVBU3OHqn9CDFu+4B69np+VD5MPSOqxzu8oZVl+c4=; b=h9xJ5xGMuLJWOYZSz2Q3Ye4KuWHfiFEfVNiI+OJcOSHQ9k7OiHQ6NTHZevihwb8ZYR XkUHlOxAM1zRrYxQtPNpN1wfcjl7MlemT8uCvwQXWI+Wm1h461ar8uGfDunxOqMrLqfV VuykYjWgqLEHJVRx0ywVK0SBJHk2g6rfmvB+TFKzGZclluhngTdsxZGVhONiLM/2khlE ZKE9BGUClaJY5OZirMxr2aKAqUqb2UWAd4MXfrtD42KspsAbaseP6Aan5U+BCy7vwiM4 5pKoFzvFEblAfkaSFovRsmEBhWYDC5qRYdyGlmTNrB96ap2AAd9oA6wu5sGW0Exe46PZ V/gQ== X-Gm-Message-State: AOAM5308X9Eeg2YYTHZ56Iqci9ULIsOjaGhg1bAFC4iBYD8ZidYkJodJ ZXYUFvJKAmWrv2RfE5Svdqs= X-Google-Smtp-Source: ABdhPJxSKf1++C5CJQ/9fHcYP66+spWCn7adbm2tiQfea3NqBFB3+YZuNsS9EOqH5kwqj4LPAuZHDw== X-Received: by 2002:aa7:868c:0:b029:3bc:e2ca:4e2f with SMTP id d12-20020aa7868c0000b02903bce2ca4e2fmr5374823pfo.73.1628169240221; Thu, 05 Aug 2021 06:14:00 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.58.38.240]) by smtp.gmail.com with ESMTPSA id t26sm6338407pfq.75.2021.08.05.06.13.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Aug 2021 06:13:59 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 38492C032C; Thu, 5 Aug 2021 06:13:58 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v5 1/1] : An API for tagged address Date: Thu, 5 Aug 2021 06:13:58 -0700 Message-Id: <20210805131358.300475-2-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210805131358.300475-1-hjl.tools@gmail.com> References: <20210805131358.300475-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3033.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: Florian Weimer , Szabolcs Nagy , Joseph Myers , "Kirill A . Shutemov" Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" By default, the number of the address bits used in address translation is the number of address bits. But it can be changed by ARM Top-byte Ignore (TBI) or Intel Linear Address Masking (LAM). provides an API for tagged address manipulation with restrictions: 1. All bits between 0 and N - 1, where N is the number of tagged address bits, are used in address translation. 2. All pointers participating in a pointer arithmetic operation should have the same tag if they point to the same memory object so that pointer equality operation can be performed on tagged pointers. /* Get the current address bits used in address translation. */ extern unsigned int get_tagged_address_bits (void); /* Get the current mask for address bits used in address translation. */ extern uintptr_t get_tagged_address_mask (void); /* Set the mask for address bits used in address translation. Return 0 on success. Return -1 on error. */ extern int set_tagged_address_mask (uintptr_t __mask); /* Return the tagged address of __ADDR with the tag value __TAG. */ extern void *tag_address (void *__addr, unsigned int __tag); /* Return the untagged address of __ADDR. */ extern void *untag_address (void *__addr); /* TRUE if constant address BITS is a valid tagged address bits. */ #define TAGGED_ADDRESS_VALID_BITS(BITS) /* A mask for constant address BITS used in address translation. */ #define TAGGED_ADDRESS_MASK(BITS) 1. set_tagged_address_mask should be called as early as possible. 2. set_tagged_address_mask shouldn't be allowed after main is called. 3. After set_tagged_address_mask is called, don't allow it to be called again to change the tagged address mask. Otherwise, the previously tagged addresses may not work anymore. --- NEWS | 2 +- bits/tagged-address-mask.h | 47 +++++++++++ bits/tagged-address-valid-bits.h | 28 +++++++ csu/libc-start.c | 3 + elf/dl-support.c | 5 ++ include/sys/tagged-address.h | 9 +++ manual/Makefile | 3 +- manual/ctype.texi | 2 +- manual/memory.texi | 2 +- manual/tagged-address.texi | 80 +++++++++++++++++++ misc/Makefile | 35 ++++++-- misc/Versions | 7 ++ misc/set-tagged-address-mask.c | 41 ++++++++++ misc/sys/tagged-address.h | 45 +++++++++++ misc/tagged-address.c | 55 +++++++++++++ misc/tst-tagged-address-1-static.c | 1 + misc/tst-tagged-address-1.c | 53 ++++++++++++ misc/tst-tagged-address-2-static.c | 1 + misc/tst-tagged-address-2.c | 44 ++++++++++ misc/tst-tagged-address-3-static.c | 1 + misc/tst-tagged-address-3.c | 48 +++++++++++ misc/tst-tagged-address-4-static.c | 1 + misc/tst-tagged-address-4.c | 35 ++++++++ misc/tst-tagged-address-5.c | 25 ++++++ misc/tst-tagged-address-6.c | 34 ++++++++ misc/tst-tagged-address-7.c | 40 ++++++++++ misc/tst-tagged-address-mod-5.c | 45 +++++++++++ misc/tst-tagged-address-mod-6.c | 34 ++++++++ misc/tst-tagged-address-mod-7.c | 34 ++++++++ sysdeps/generic/inline-tagged-address.h | 43 ++++++++++ sysdeps/generic/ldsodefs.h | 4 + sysdeps/unix/sysv/linux/i386/libc.abilist | 5 ++ .../unix/sysv/linux/x86_64/64/libc.abilist | 5 ++ .../unix/sysv/linux/x86_64/x32/libc.abilist | 5 ++ 34 files changed, 813 insertions(+), 9 deletions(-) create mode 100644 bits/tagged-address-mask.h create mode 100644 bits/tagged-address-valid-bits.h create mode 100644 include/sys/tagged-address.h create mode 100644 manual/tagged-address.texi create mode 100644 misc/set-tagged-address-mask.c create mode 100644 misc/sys/tagged-address.h create mode 100644 misc/tagged-address.c create mode 100644 misc/tst-tagged-address-1-static.c create mode 100644 misc/tst-tagged-address-1.c create mode 100644 misc/tst-tagged-address-2-static.c create mode 100644 misc/tst-tagged-address-2.c create mode 100644 misc/tst-tagged-address-3-static.c create mode 100644 misc/tst-tagged-address-3.c create mode 100644 misc/tst-tagged-address-4-static.c create mode 100644 misc/tst-tagged-address-4.c create mode 100644 misc/tst-tagged-address-5.c create mode 100644 misc/tst-tagged-address-6.c create mode 100644 misc/tst-tagged-address-7.c create mode 100644 misc/tst-tagged-address-mod-5.c create mode 100644 misc/tst-tagged-address-mod-6.c create mode 100644 misc/tst-tagged-address-mod-7.c create mode 100644 sysdeps/generic/inline-tagged-address.h diff --git a/NEWS b/NEWS index 79c895e382..4f80340f9f 100644 --- a/NEWS +++ b/NEWS @@ -9,7 +9,7 @@ Version 2.35 Major new features: - [Add new features here] +* Add to provide an API for tagged address. Deprecated and removed features, and other changes affecting compatibility: diff --git a/bits/tagged-address-mask.h b/bits/tagged-address-mask.h new file mode 100644 index 0000000000..649e05b1b7 --- /dev/null +++ b/bits/tagged-address-mask.h @@ -0,0 +1,47 @@ +/* Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _BITS_TAGGED_ADDRESS_MASK_H +#define _BITS_TAGGED_ADDRESS_MASK_H 1 + +#ifndef _SYS_TAGGED_ADDRESS_H +# error "Never include this file directly. Use instead" +#endif + +#include + +#ifdef __GNUC__ +# if defined __USE_ISOC11 || defined __USE_ISOCXX11 +# ifdef __USE_ISOCXX11 +# define TAGGED_ADDRESS_ASSERT static_assert +# else +# define TAGGED_ADDRESS_ASSERT _Static_assert +# endif +# else +# define TAGGED_ADDRESS_ASSERT(expr, msg) +# endif +/* A mask for constant address BITS used in address translation. */ +# define TAGGED_ADDRESS_MASK(BITS) \ + (__extension__ \ + ({ \ + TAGGED_ADDRESS_ASSERT (TAGGED_ADDRESS_VALID_BITS (BITS), \ + "Tagged address bits must be valid"); \ + (((uintptr_t) 1) << (BITS)) - 1; \ + })) +#endif + +#endif /* */ diff --git a/bits/tagged-address-valid-bits.h b/bits/tagged-address-valid-bits.h new file mode 100644 index 0000000000..38b6c12b01 --- /dev/null +++ b/bits/tagged-address-valid-bits.h @@ -0,0 +1,28 @@ +/* Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _BITS_TAGGED_ADDRESS_VALID_BITS_H +#define _BITS_TAGGED_ADDRESS_VALID_BITS_H 1 + +#ifndef _SYS_TAGGED_ADDRESS_H +# error "Never include this file directly. Use instead" +#endif + +/* Non-zero if constant address BITS is a valid tagged address bits. */ +#define TAGGED_ADDRESS_VALID_BITS(BITS) 0 + +#endif /* */ diff --git a/csu/libc-start.c b/csu/libc-start.c index 0350b006fd..984659a394 100644 --- a/csu/libc-start.c +++ b/csu/libc-start.c @@ -402,6 +402,9 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), GLRO(dl_debug_printf) ("\ntransferring control: %s\n\n", argv[0]); #endif + /* set_tagged_address_mask can only be called before main. */ + GL(dl_tagged_address_mask_locked) = 1; + #ifndef SHARED _dl_debug_initialize (0, LM_ID_BASE); #endif diff --git a/elf/dl-support.c b/elf/dl-support.c index 0155718175..e8cbe8fa81 100644 --- a/elf/dl-support.c +++ b/elf/dl-support.c @@ -213,6 +213,11 @@ struct link_map *_dl_sysinfo_map; # include "get-dynamic-info.h" #endif + +/* If nonzero, the mask for address bits used in address translation + is locked. */ +int _dl_tagged_address_mask_locked; + #include "setup-vdso.h" /* Define the vDSO function pointers. */ #include diff --git a/include/sys/tagged-address.h b/include/sys/tagged-address.h new file mode 100644 index 0000000000..2e902f72b2 --- /dev/null +++ b/include/sys/tagged-address.h @@ -0,0 +1,9 @@ +#include + +#ifndef _ISOMAC +# include +# define get_tagged_address_bits() __get_tagged_address_bits () +# define get_tagged_address_mask() __get_tagged_address_mask () +# define tag_address(addr, tag) __tag_address ((addr), (tag)) +# define untag_address(addr) __untag_address ((addr)) +#endif diff --git a/manual/Makefile b/manual/Makefile index e83444341e..08df2daa6b 100644 --- a/manual/Makefile +++ b/manual/Makefile @@ -34,7 +34,8 @@ info: $(objpfx)libc.info endif chapters = $(addsuffix .texi, \ - intro errno memory ctype string charset locale \ + intro errno memory tagged-address ctype string \ + charset locale \ message search pattern io stdio llio filesys \ pipe socket terminal syslog math arith time \ resource setjmp signal startup process ipc job \ diff --git a/manual/ctype.texi b/manual/ctype.texi index d0618c5c38..28af73ff0e 100644 --- a/manual/ctype.texi +++ b/manual/ctype.texi @@ -1,4 +1,4 @@ -@node Character Handling, String and Array Utilities, Memory, Top +@node Character Handling, String and Array Utilities, Tagged Address, Top @c %MENU% Character testing and conversion functions @chapter Character Handling diff --git a/manual/memory.texi b/manual/memory.texi index 0b2b9c8795..2e283e41f9 100644 --- a/manual/memory.texi +++ b/manual/memory.texi @@ -1,4 +1,4 @@ -@node Memory, Character Handling, Error Reporting, Top +@node Memory, Tagged Address, Error Reporting, Top @chapter Virtual Memory Allocation And Paging @c %MENU% Allocating virtual memory and controlling paging @cindex memory allocation diff --git a/manual/tagged-address.texi b/manual/tagged-address.texi new file mode 100644 index 0000000000..a3929a0eb7 --- /dev/null +++ b/manual/tagged-address.texi @@ -0,0 +1,80 @@ +@node Tagged Address, Character Handling, Memory, Top +@c %MENU% Tagged address functions and macros +@chapter Tagged Address + +By default, the number of the address bits used in address translation +is the number of address bits. But it can be changed by ARM Top-byte +Ignore (TBI) or Intel Linear Address Masking (LAM). + +@Theglibc{} provides several functions and macros in the header file +@file{sys/tagged-address.h} to manipulate tagged address bits, which is +the number of the address bits used in address translation, with +restrictions: + +@itemize @bullet +@item +All bits between 0 and N - 1, where N is the number of tagged address +bits, are used in address translation. + +@item +All pointers participating in a pointer arithmetic operation should have +the same tag if they point to the same memory object so that pointer +equality operation can be performed on tagged pointers. +@end itemize + +@pindex sys/tagged-address.h + +@deftypefun {unsigned int} get_tagged_address_bits (void) +@standards{GNU, sys/tagged-address.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} +Get the current address bits used in address translation. The return +value is @code{0} if tag bits are not the highest bits in addresses. The +return value is the number of address bits when tagged address is +unsupported. +@end deftypefun + +@deftypefun uintptr_t get_tagged_address_mask (void) +@standards{GNU, sys/tagged-address.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} +Get the current mask for address bits used in address translation. If a +bit is set in the return value, this bit is used in address translation. +The return value is @code{-1} when all bits are used in address +translation. +@end deftypefun + +@deftypefun int set_tagged_address_mask (uintptr_t @var{mask}) +@standards{GNU, sys/tagged-address.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} +Set the mask for address bits used in address translation to @var{mask}. +Only bits set in @var{mask} will be used in address translation. The +return value is @code{0} on success and @code{-1} on failure. This +function can be called only once before @code{main}. The possible +@code{errno} error conditions are @code{ENODEV}, @code{EPERM}, +@code{EINVAL}, and @code{ENOSYS}. +@end deftypefun + +@deftypefun {void *} tag_address (void *@var{addr}, unsigned int @var{tag}) +@standards{GNU, sys/tagged-address.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} +Return the address of @var{addr} with the tag value @var{tag} stored +in the untranslated bits. Overflow of @var{tag} in the untranslated +bits are ignored. +@end deftypefun + +@deftypefun {void *} untag_address (void *@var{addr}) +@standards{GNU, sys/tagged-address.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} +Return the address of @var{addr} with all zero untranslated bits. +@end deftypefun + +@deftypefn Macro int TAGGED_ADDRESS_VALID_BITS (@var{bits}) +This macro returns a nonzero value (true) if @var{bits} is a valid +constant number of the lower address bits which can be used in address +translation. +@end deftypefn + +@deftypefn Macro {const uintptr_t} TAGGED_ADDRESS_MASK (@var{bits}) +This macro returns a nonzero value which can be passed to +@code{set_tagged_address_mask} to specify the lower address @var{bits} +for address translation. +@end deftypefn diff --git a/misc/Makefile b/misc/Makefile index 1083ba3bfc..77fa2cd6df 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -38,7 +38,10 @@ headers := sys/uio.h bits/uio-ext.h bits/uio_lim.h \ bits/select2.h bits/hwcap.h sys/auxv.h \ sys/sysmacros.h bits/sysmacros.h bits/types/struct_iovec.h \ bits/err-ldbl.h bits/error-ldbl.h \ - sys/single_threaded.h + sys/single_threaded.h \ + sys/tagged-address.h \ + bits/tagged-address-mask.h \ + bits/tagged-address-valid-bits.h routines := brk sbrk sstk ioctl \ readv writev preadv preadv64 pwritev pwritev64 \ @@ -73,7 +76,8 @@ routines := brk sbrk sstk ioctl \ fgetxattr flistxattr fremovexattr fsetxattr getxattr \ listxattr lgetxattr llistxattr lremovexattr lsetxattr \ removexattr setxattr getauxval ifunc-impl-list makedev \ - allocate_once fd_to_filename single_threaded unwind-link + allocate_once fd_to_filename single_threaded unwind-link \ + tagged-address set-tagged-address-mask generated += tst-error1.mtrace tst-error1-mem.out \ tst-allocate_once.mtrace tst-allocate_once-mem.out @@ -89,7 +93,11 @@ tests := tst-dirname tst-tsearch tst-fdset tst-mntent tst-hsearch \ tst-preadvwritev2 tst-preadvwritev64v2 tst-warn-wide \ tst-ldbl-warn tst-ldbl-error tst-dbl-efgcvt tst-ldbl-efgcvt \ tst-mntent-autofs tst-syscalls tst-mntent-escape tst-select \ - tst-ioctl + tst-ioctl \ + tst-tagged-address-1 tst-tagged-address-1-static \ + tst-tagged-address-2 tst-tagged-address-2-static \ + tst-tagged-address-3 tst-tagged-address-3-static \ + tst-tagged-address-4 tst-tagged-address-4-static tests-time64 := \ tst-select-time64 \ @@ -99,11 +107,22 @@ tests-time64 := \ # Tests which need libdl. ifeq (yes,$(build-shared)) -tests += tst-gethostid +tests += tst-gethostid \ + tst-tagged-address-5 \ + tst-tagged-address-6 \ + tst-tagged-address-7 +modules-names += \ + tst-tagged-address-mod-5 \ + tst-tagged-address-mod-6 \ + tst-tagged-address-mod-7 endif tests-internal := tst-atomic tst-atomic-long tst-allocate_once -tests-static := tst-empty +tests-static := tst-empty \ + tst-tagged-address-1-static \ + tst-tagged-address-2-static \ + tst-tagged-address-3-static \ + tst-tagged-address-4-static # Test for the internal, non-exported __fd_to_filename function. tests-internal += tst-fd_to_filename @@ -172,3 +191,9 @@ $(objpfx)tst-select: $(librt) $(objpfx)tst-select-time64: $(librt) $(objpfx)tst-pselect: $(librt) $(objpfx)tst-pselect-time64: $(librt) + +$(objpfx)tst-tagged-address-2: $(shared-thread-library) +$(objpfx)tst-tagged-address-2-static: $(static-thread-library) +$(objpfx)tst-tagged-address-7: $(libdl) +$(objpfx)tst-tagged-address-7.out: $(objpfx)tst-tagged-address-mod-7.so +$(objpfx)tst-tagged-address-mod-7.so: $(libsupport) diff --git a/misc/Versions b/misc/Versions index d5b348e83a..6ab64586eb 100644 --- a/misc/Versions +++ b/misc/Versions @@ -164,6 +164,13 @@ libc { GLIBC_2.32 { __libc_single_threaded; } + GLIBC_2.35 { + get_tagged_address_bits; + get_tagged_address_mask; + set_tagged_address_mask; + tag_address; + untag_address; + } GLIBC_PRIVATE { __madvise; __mktemp; diff --git a/misc/set-tagged-address-mask.c b/misc/set-tagged-address-mask.c new file mode 100644 index 0000000000..2e1e2f2343 --- /dev/null +++ b/misc/set-tagged-address-mask.c @@ -0,0 +1,41 @@ +/* Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +/* Set the mask for address bits used in address translation. Return 0 + on success. Return -1 on error. */ + +int +set_tagged_address_mask (uintptr_t mask) +{ + if (GL(dl_tagged_address_mask_locked) != 0) + __set_errno (EPERM); + else + { + /* set_tagged_address_mask can only be called once. */ + GL(dl_tagged_address_mask_locked) = 1; + + /* Only NOP is allowed. */ + if (mask == __get_tagged_address_mask ()) + return 0; + + __set_errno (ENOSYS); + } + return -1; +} diff --git a/misc/sys/tagged-address.h b/misc/sys/tagged-address.h new file mode 100644 index 0000000000..025a333cac --- /dev/null +++ b/misc/sys/tagged-address.h @@ -0,0 +1,45 @@ +/* Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SYS_TAGGED_ADDRESS_H +#define _SYS_TAGGED_ADDRESS_H 1 + +#include +#include +#include + +__BEGIN_DECLS + +/* Get the current address bits used in address translation. */ +extern unsigned int get_tagged_address_bits (void); + +/* Get the current mask for address bits used in address translation. */ +extern uintptr_t get_tagged_address_mask (void); + +/* Set the mask for address bits used in address translation. Return 0 + on success. Return -1 on error. */ +extern int set_tagged_address_mask (uintptr_t __mask); + +/* Return the tagged address of __ADDR with the tag value __TAG. */ +extern void *tag_address (void *__addr, unsigned int __tag); + +/* Return the untagged address of __ADDR. */ +extern void *untag_address (void *__addr); + +__END_DECLS + +#endif /* */ diff --git a/misc/tagged-address.c b/misc/tagged-address.c new file mode 100644 index 0000000000..df474f3d0b --- /dev/null +++ b/misc/tagged-address.c @@ -0,0 +1,55 @@ +/* Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#undef get_tagged_address_bits +#undef get_tagged_address_mask +#undef tag_address +#undef untag_address + +/* Get the current address bits used in address translation. */ + +unsigned int +get_tagged_address_bits (void) +{ + return __get_tagged_address_bits (); +} + +/* Get the current mask for address bits used in address translation. */ + +uintptr_t +get_tagged_address_mask (void) +{ + return __get_tagged_address_mask (); +} + +/* Return the tagged address of ADDR with the tag value TAG. */ + +void * +tag_address (void *addr, unsigned int tag) +{ + return __tag_address (addr, tag); +} + +/* Return the untagged address of ADDR. */ + +void * +untag_address (void *addr) +{ + return __untag_address (addr); +} diff --git a/misc/tst-tagged-address-1-static.c b/misc/tst-tagged-address-1-static.c new file mode 100644 index 0000000000..3ed71a66c2 --- /dev/null +++ b/misc/tst-tagged-address-1-static.c @@ -0,0 +1 @@ +#include diff --git a/misc/tst-tagged-address-1.c b/misc/tst-tagged-address-1.c new file mode 100644 index 0000000000..0bbc214a2f --- /dev/null +++ b/misc/tst-tagged-address-1.c @@ -0,0 +1,53 @@ +/* Tests for set_tagged_address_mask. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +static void +init_0 (void) +{ + if (set_tagged_address_mask (get_tagged_address_mask ()) != 0) + FAIL_EXIT1 ("set_tagged_address_mask failed: %m"); +} + +static void +init_1 (void) +{ + if (set_tagged_address_mask (get_tagged_address_mask ()) == 0) + FAIL_EXIT1 ("set_tagged_address_mask should fail"); + + if (errno != EPERM) + FAIL_EXIT1 ("set_tagged_address_mask: errno (%m) != EPERM"); +} + +static void (*init_array []) (void) + __attribute__ ((used, section (".init_array"), aligned (sizeof (void *)))) = +{ + &init_0, + &init_1 +}; + +static int +do_test (void) +{ + return 0; +} + +#include diff --git a/misc/tst-tagged-address-2-static.c b/misc/tst-tagged-address-2-static.c new file mode 100644 index 0000000000..ac0d95e6c0 --- /dev/null +++ b/misc/tst-tagged-address-2-static.c @@ -0,0 +1 @@ +#include diff --git a/misc/tst-tagged-address-2.c b/misc/tst-tagged-address-2.c new file mode 100644 index 0000000000..76caae4053 --- /dev/null +++ b/misc/tst-tagged-address-2.c @@ -0,0 +1,44 @@ +/* Test for set_tagged_address_mask with pthread. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +static void * +tf (void *arg) +{ + if (set_tagged_address_mask (get_tagged_address_mask ()) == 0) + FAIL_EXIT1 ("set_tagged_address_mask should fail"); + + if (errno != EPERM) + FAIL_EXIT1 ("set_tagged_address_mask: errno (%m) != EPERM"); + + return NULL; +} + +static int +do_test (void) +{ + pthread_t thr = xpthread_create (NULL, tf, NULL); + xpthread_join (thr); + return 0; +} + +#include diff --git a/misc/tst-tagged-address-3-static.c b/misc/tst-tagged-address-3-static.c new file mode 100644 index 0000000000..1e3591e0d6 --- /dev/null +++ b/misc/tst-tagged-address-3-static.c @@ -0,0 +1 @@ +#include diff --git a/misc/tst-tagged-address-3.c b/misc/tst-tagged-address-3.c new file mode 100644 index 0000000000..e241950959 --- /dev/null +++ b/misc/tst-tagged-address-3.c @@ -0,0 +1,48 @@ +/* Tests for set_tagged_address_mask to turn off tagged address. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +static void +init (void) +{ + uintptr_t mask = (uintptr_t) -1; + if (set_tagged_address_mask (mask) != 0) + FAIL_EXIT1 ("set_tagged_address_mask failed: %m"); +} + +static void (*init_array []) (void) + __attribute__ ((used, section (".init_array"), aligned (sizeof (void *)))) = +{ + &init +}; + +static int +do_test (void) +{ + uintptr_t mask = (uintptr_t) -1; + uintptr_t curent_mask = get_tagged_address_mask (); + if (curent_mask != mask) + FAIL_EXIT1 ("get_tagged_address_mask failed: %p != %p", + (void *) curent_mask, (void *) mask); + + return 0; +} + +#include diff --git a/misc/tst-tagged-address-4-static.c b/misc/tst-tagged-address-4-static.c new file mode 100644 index 0000000000..b0681f6f9b --- /dev/null +++ b/misc/tst-tagged-address-4-static.c @@ -0,0 +1 @@ +#include diff --git a/misc/tst-tagged-address-4.c b/misc/tst-tagged-address-4.c new file mode 100644 index 0000000000..9843f780af --- /dev/null +++ b/misc/tst-tagged-address-4.c @@ -0,0 +1,35 @@ +/* Tests for set_tagged_address_mask. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +static int +do_test (void) +{ + if (set_tagged_address_mask (get_tagged_address_mask ()) == 0) + FAIL_EXIT1 ("set_tagged_address_mask should fail"); + + if (errno != EPERM) + FAIL_EXIT1 ("set_tagged_address_mask: errno (%m) != EPERM"); + + return 0; +} + +#include diff --git a/misc/tst-tagged-address-5.c b/misc/tst-tagged-address-5.c new file mode 100644 index 0000000000..e00679a2cb --- /dev/null +++ b/misc/tst-tagged-address-5.c @@ -0,0 +1,25 @@ +/* Tests for set_tagged_address_mask. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +static int +do_test (void) +{ + return 0; +} + +#include diff --git a/misc/tst-tagged-address-6.c b/misc/tst-tagged-address-6.c new file mode 100644 index 0000000000..5fdceb1fb4 --- /dev/null +++ b/misc/tst-tagged-address-6.c @@ -0,0 +1,34 @@ +/* Tests for set_tagged_address_mask to turn off tagged address. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +static int +do_test (void) +{ + uintptr_t mask = (uintptr_t) -1; + uintptr_t curent_mask = get_tagged_address_mask (); + if (curent_mask != mask) + FAIL_EXIT1 ("get_tagged_address_mask failed: %p != %p", + (void *) curent_mask, (void *) mask); + + return 0; +} + +#include diff --git a/misc/tst-tagged-address-7.c b/misc/tst-tagged-address-7.c new file mode 100644 index 0000000000..f0a078188d --- /dev/null +++ b/misc/tst-tagged-address-7.c @@ -0,0 +1,40 @@ +/* Tests for set_tagged_address_mask. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +static int +do_test (void) +{ + const char *modname = "$ORIGIN/tst-tagged-address-mod-7.so"; + void *h = dlopen (modname, RTLD_LAZY); + + if (h == NULL) + { + const char *err = dlerror (); + FAIL_EXIT1 ("cannot open '%s': %s\n", modname, err); + } + + dlclose (h); + + return 0; +} + +#include diff --git a/misc/tst-tagged-address-mod-5.c b/misc/tst-tagged-address-mod-5.c new file mode 100644 index 0000000000..275ecc406d --- /dev/null +++ b/misc/tst-tagged-address-mod-5.c @@ -0,0 +1,45 @@ +/* Tests for set_tagged_address_mask. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +static void +init_0 (void) +{ + if (set_tagged_address_mask (get_tagged_address_mask ()) != 0) + FAIL_EXIT1 ("set_tagged_address_mask failed: %m"); +} + +static void +init_1 (void) +{ + if (set_tagged_address_mask (get_tagged_address_mask ()) == 0) + FAIL_EXIT1 ("set_tagged_address_mask should fail"); + + if (errno != EPERM) + FAIL_EXIT1 ("set_tagged_address_mask: errno (%m) != EPERM"); +} + +static void (*init_array []) (void) + __attribute__ ((used, section (".init_array"), aligned (sizeof (void *)))) = +{ + &init_0, + &init_1 +}; diff --git a/misc/tst-tagged-address-mod-6.c b/misc/tst-tagged-address-mod-6.c new file mode 100644 index 0000000000..75dfe8e8d2 --- /dev/null +++ b/misc/tst-tagged-address-mod-6.c @@ -0,0 +1,34 @@ +/* Tests for set_tagged_address_mask to turn off tagged address. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +static void +init (void) +{ + uintptr_t mask = (uintptr_t) -1; + if (set_tagged_address_mask (mask) != 0) + FAIL_EXIT1 ("set_tagged_address_mask failed: %m"); +} + +static void (*init_array []) (void) + __attribute__ ((used, section (".init_array"), aligned (sizeof (void *)))) = +{ + &init +}; diff --git a/misc/tst-tagged-address-mod-7.c b/misc/tst-tagged-address-mod-7.c new file mode 100644 index 0000000000..c902a4e55e --- /dev/null +++ b/misc/tst-tagged-address-mod-7.c @@ -0,0 +1,34 @@ +/* Tests for set_tagged_address_mask to turn off tagged address. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +static void +init (void) +{ + uintptr_t mask = (uintptr_t) -1; + if (set_tagged_address_mask (mask) == 0) + FAIL_EXIT1 ("set_tagged_address_mask should have failed"); +} + +static void (*init_array []) (void) + __attribute__ ((used, section (".init_array"), aligned (sizeof (void *)))) = +{ + &init +}; diff --git a/sysdeps/generic/inline-tagged-address.h b/sysdeps/generic/inline-tagged-address.h new file mode 100644 index 0000000000..a016b16f21 --- /dev/null +++ b/sysdeps/generic/inline-tagged-address.h @@ -0,0 +1,43 @@ +/* Inline tagged address functions. Generic version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +static inline unsigned int +__get_tagged_address_bits (void) +{ + return sizeof (uintptr_t) * 8; +} + +static inline uintptr_t +__get_tagged_address_mask (void) +{ + return (uintptr_t) -1; +} + +static inline void * +__tag_address (void *addr, unsigned int tag) +{ + return addr; +} + +static inline void * +__untag_address (void *addr) +{ + return addr; +} diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 9c15259236..ea64fed6e6 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -489,6 +489,10 @@ struct rtld_global #else EXTERN int _dl_thread_gscope_count; #endif + + /* If nonzero, the mask for address bits used in address translation + is locked. */ + EXTERN int _dl_tagged_address_mask_locked; #ifdef SHARED }; # define __rtld_global_attribute__ diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist index 2e7603d9ed..3c87dab94a 100644 --- a/sysdeps/unix/sysv/linux/i386/libc.abilist +++ b/sysdeps/unix/sysv/linux/i386/libc.abilist @@ -2780,6 +2780,11 @@ GLIBC_2.34 tss_create F GLIBC_2.34 tss_delete F GLIBC_2.34 tss_get F GLIBC_2.34 tss_set F +GLIBC_2.35 get_tagged_address_bits F +GLIBC_2.35 get_tagged_address_mask F +GLIBC_2.35 set_tagged_address_mask F +GLIBC_2.35 tag_address F +GLIBC_2.35 untag_address F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist index 095e914b73..cc4edb86bd 100644 --- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist @@ -2521,6 +2521,11 @@ GLIBC_2.34 tss_create F GLIBC_2.34 tss_delete F GLIBC_2.34 tss_get F GLIBC_2.34 tss_set F +GLIBC_2.35 get_tagged_address_bits F +GLIBC_2.35 get_tagged_address_mask F +GLIBC_2.35 set_tagged_address_mask F +GLIBC_2.35 tag_address F +GLIBC_2.35 untag_address F GLIBC_2.4 __confstr_chk F GLIBC_2.4 __fgets_chk F GLIBC_2.4 __fgets_unlocked_chk F diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist index dd910f7fe9..f8c889ba87 100644 --- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist @@ -2627,3 +2627,8 @@ GLIBC_2.34 tss_create F GLIBC_2.34 tss_delete F GLIBC_2.34 tss_get F GLIBC_2.34 tss_set F +GLIBC_2.35 get_tagged_address_bits F +GLIBC_2.35 get_tagged_address_mask F +GLIBC_2.35 set_tagged_address_mask F +GLIBC_2.35 tag_address F +GLIBC_2.35 untag_address F