From patchwork Wed Jun 24 15:53:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Earnshaw X-Patchwork-Id: 39787 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 D3092398786F; Wed, 24 Jun 2020 15:54:02 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id CD8183851C3B for ; Wed, 24 Jun 2020 15:53:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CD8183851C3B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rearnsha@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6C4FA31B; Wed, 24 Jun 2020 08:53:57 -0700 (PDT) Received: from eagle.buzzard.freeserve.co.uk (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id ED8333F73C; Wed, 24 Jun 2020 08:53:56 -0700 (PDT) From: Richard Earnshaw To: libc-alpha@sourceware.org Subject: [PATCH v2 1/6] config: Allow memory tagging to be enabled when configuring glibc Date: Wed, 24 Jun 2020 16:53:44 +0100 Message-Id: <20200624155349.12134-2-rearnsha@arm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200624155349.12134-1-rearnsha@arm.com> References: <20200624155349.12134-1-rearnsha@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: Richard Earnshaw Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" This patch adds the configuration machinery to allow memory tagging to be enabled from the command line via the configure option --enable-memory-tagging. The current default is off, though in time we may change that once the API is more stable. --- INSTALL | 14 ++++++++++++++ config.h.in | 3 +++ config.make.in | 2 ++ configure | 17 +++++++++++++++++ configure.ac | 10 ++++++++++ manual/install.texi | 13 +++++++++++++ 6 files changed, 59 insertions(+) diff --git a/INSTALL b/INSTALL index 62e78725f5..37c98ca015 100644 --- a/INSTALL +++ b/INSTALL @@ -142,6 +142,20 @@ if 'CFLAGS' is specified it must enable optimization. For example: non-CET processors. '--enable-cet' has been tested for i686, x86_64 and x32 on CET processors. +'--enable-memory-tagging' + Enable memory tagging support on architectures that support it. + When the GNU C Library is built with this option then the resulting + library will be able to control the use of tagged memory when + hardware support is present by use of the tunable + 'glibc.memtag.enable'. This includes the generation of tagged + memory when using the 'malloc' APIs. + + At present only AArch64 platforms with MTE provide this + functionality, although the library will still operate (without + memory tagging) on older versions of the architecture. + + The default is to disable support for memory tagging. + '--disable-profile' Don't build libraries with profiling information. You may want to use this option if you don't plan to do profiling. diff --git a/config.h.in b/config.h.in index 831eca2fe1..134f4257cd 100644 --- a/config.h.in +++ b/config.h.in @@ -148,6 +148,9 @@ /* Define if __stack_chk_guard canary should be randomized at program startup. */ #undef ENABLE_STACKGUARD_RANDOMIZE +/* Define if memory tagging support should be enabled. */ +#undef _LIBC_MTAG + /* Package description. */ #undef PKGVERSION diff --git a/config.make.in b/config.make.in index 2fed3da773..b3be8e0381 100644 --- a/config.make.in +++ b/config.make.in @@ -84,6 +84,8 @@ mach-interface-list = @mach_interface_list@ experimental-malloc = @experimental_malloc@ +memory-tagging = @memory_tagging@ + nss-crypt = @libc_cv_nss_crypt@ static-nss-crypt = @libc_cv_static_nss_crypt@ diff --git a/configure b/configure index b8ef8c7a51..528112aabf 100755 --- a/configure +++ b/configure @@ -678,6 +678,7 @@ link_obsolete_rpc libc_cv_static_nss_crypt libc_cv_nss_crypt build_crypt +memory_tagging experimental_malloc enable_werror all_warnings @@ -783,6 +784,7 @@ enable_all_warnings enable_werror enable_multi_arch enable_experimental_malloc +enable_memory_tagging enable_crypt enable_nss_crypt enable_obsolete_rpc @@ -1454,6 +1456,8 @@ Optional Features: architectures --disable-experimental-malloc disable experimental malloc features + --enable-memory-tagging enable memory tagging if supported by the + architecture [default=no] --disable-crypt do not build nor install the passphrase hashing library, libcrypt --enable-nss-crypt enable libcrypt to use nss @@ -3527,6 +3531,19 @@ fi +# Check whether --enable-memory-tagging was given. +if test "${enable_memory_tagging+set}" = set; then : + enableval=$enable_memory_tagging; memory_tagging=$enableval +else + memory_tagging=no +fi + +if test "$memory_tagging" = yes; then + $as_echo "#define _LIBC_MTAG 1" >>confdefs.h + +fi + + # Check whether --enable-crypt was given. if test "${enable_crypt+set}" = set; then : enableval=$enable_crypt; build_crypt=$enableval diff --git a/configure.ac b/configure.ac index ff00591fd4..ebcc931a8e 100644 --- a/configure.ac +++ b/configure.ac @@ -311,6 +311,16 @@ AC_ARG_ENABLE([experimental-malloc], [experimental_malloc=yes]) AC_SUBST(experimental_malloc) +AC_ARG_ENABLE([memory-tagging], + AC_HELP_STRING([--enable-memory-tagging], + [enable memory tagging if supported by the architecture @<:@default=no@:>@]), + [memory_tagging=$enableval], + [memory_tagging=no]) +if test "$memory_tagging" = yes; then + AC_DEFINE(_LIBC_MTAG) +fi +AC_SUBST(memory_tagging) + AC_ARG_ENABLE([crypt], AC_HELP_STRING([--disable-crypt], [do not build nor install the passphrase hashing library, libcrypt]), diff --git a/manual/install.texi b/manual/install.texi index c1e49a94fe..6cd87326e3 100644 --- a/manual/install.texi +++ b/manual/install.texi @@ -171,6 +171,19 @@ NOTE: @option{--enable-cet} has been tested for i686, x86_64 and x32 on non-CET processors. @option{--enable-cet} has been tested for i686, x86_64 and x32 on CET processors. +@item --enable-memory-tagging +Enable memory tagging support on architectures that support it. When +@theglibc{} is built with this option then the resulting library will +be able to control the use of tagged memory when hardware support is +present by use of the tunable @samp{glibc.memtag.enable}. This includes +the generation of tagged memory when using the @code{malloc} APIs. + +At present only AArch64 platforms with MTE provide this functionality, +although the library will still operate (without memory tagging) on +older versions of the architecture. + +The default is to disable support for memory tagging. + @item --disable-profile Don't build libraries with profiling information. You may want to use this option if you don't plan to do profiling. From patchwork Wed Jun 24 15:53:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Earnshaw X-Patchwork-Id: 39785 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 9AB093851C3B; Wed, 24 Jun 2020 15:54:00 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 7BA65386F037 for ; Wed, 24 Jun 2020 15:53:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7BA65386F037 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rearnsha@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 21837101E; Wed, 24 Jun 2020 08:53:58 -0700 (PDT) Received: from eagle.buzzard.freeserve.co.uk (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A32433F73C; Wed, 24 Jun 2020 08:53:57 -0700 (PDT) From: Richard Earnshaw To: libc-alpha@sourceware.org Subject: [PATCH v2 2/6] elf: Add a tunable to control use of tagged memory Date: Wed, 24 Jun 2020 16:53:45 +0100 Message-Id: <20200624155349.12134-3-rearnsha@arm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200624155349.12134-1-rearnsha@arm.com> References: <20200624155349.12134-1-rearnsha@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-15.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: Richard Earnshaw Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Add a new glibc tunable: mtag.enable, bound to the environment variable _MTAG_ENABLE. This is a decimal constant in the range 0-255 but used as a bit-field. Bit 0 enables use of tagged memory in the malloc family of functions. Bit 1 enables precise faulting of tag failure on platforms where this can be controlled. Other bits are currently unused, but if set will cause memory tag checking for the current process to be enabled in the kernel. --- elf/dl-tunables.list | 9 +++++++++ manual/tunables.texi | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 0d398dd251..92b6f21b44 100644 --- a/elf/dl-tunables.list +++ b/elf/dl-tunables.list @@ -126,4 +126,13 @@ glibc { default: 3 } } + + memtag { + enable { + type: INT_32 + minval: 0 + maxval: 255 + env_alias: _MTAG_ENABLE + } + } } diff --git a/manual/tunables.texi b/manual/tunables.texi index ec18b10834..428a0918e6 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -35,6 +35,8 @@ their own namespace. * POSIX Thread Tunables:: Tunables in the POSIX thread subsystem * Hardware Capability Tunables:: Tunables that modify the hardware capabilities seen by @theglibc{} +* Memory Tagging Tunables:: Tunables that control the use of hardware + memory tagging @end menu @node Tunable names @@ -423,3 +425,32 @@ instead. This tunable is specific to i386 and x86-64. @end deftp + +@node Memory Tagging Tunables +@section Memory Tagging Tunables +@cindex memory tagging tunables + +@deftp {Tunable namespace} glibc.memtag +If the hardware supports memory tagging, these tunables can be used to +control the way @theglibc{} uses this feature. Currently, only AArch64 +supports this feature. +@end deftp + +@deftp Tunable glibc.memtag.enable +This tunable takes a value between 0 and 255 and acts as a bitmask +that enables various capabilities. + +Bit 0 (the least significant bit) causes the malloc subsystem to allocate +tagged memory, with each allocation being assigned a random tag. + +Bit 1 enables precise faulting mode for tag violations on systems that +support deferred tag violation reporting. This may cause programs +to run more slowly. + +Other bits are currently reserved. + +@Theglibc{} startup code will automatically enable memory tagging +support in the kernel if this tunable has any non-zero value. + +The default value is @samp{0}, which disables all memory tagging. +@end deftp From patchwork Wed Jun 24 15:53:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Earnshaw X-Patchwork-Id: 39789 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 928013987875; Wed, 24 Jun 2020 15:54:05 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 68963386F837 for ; Wed, 24 Jun 2020 15:53:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 68963386F837 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rearnsha@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0E3E41045; Wed, 24 Jun 2020 08:53:59 -0700 (PDT) Received: from eagle.buzzard.freeserve.co.uk (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 57E3C3F73C; Wed, 24 Jun 2020 08:53:58 -0700 (PDT) From: Richard Earnshaw To: libc-alpha@sourceware.org Subject: [PATCH v2 3/6] malloc: Basic support for memory tagging in the malloc() family Date: Wed, 24 Jun 2020 16:53:46 +0100 Message-Id: <20200624155349.12134-4-rearnsha@arm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200624155349.12134-1-rearnsha@arm.com> References: <20200624155349.12134-1-rearnsha@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: Richard Earnshaw Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" This patch adds the basic support for memory tagging. Various flavours are supported, particularly being able to turn on tagged memory at run-time: this allows the same code to be used on systems where memory tagging support is not present without neededing a separate build of glibc. Also, depending on whether the kernel supports it, the code will use mmap for the default arena if morecore does not, or cannot support tagged memory (on AArch64 it is not available). All the hooks use function pointers to allow this to work without needing ifuncs. malloc: allow memory tagging to be controlled from a tunable --- malloc/arena.c | 43 ++++++++- malloc/hooks.c | 6 ++ malloc/malloc.c | 186 ++++++++++++++++++++++++++++-------- malloc/malloc.h | 7 ++ sysdeps/generic/libc-mtag.h | 52 ++++++++++ 5 files changed, 252 insertions(+), 42 deletions(-) create mode 100644 sysdeps/generic/libc-mtag.h diff --git a/malloc/arena.c b/malloc/arena.c index cecdb7f4c4..eab4fb3f45 100644 --- a/malloc/arena.c +++ b/malloc/arena.c @@ -274,17 +274,36 @@ next_env_entry (char ***position) #endif -#ifdef SHARED +#if defined(SHARED) || defined(_LIBC_MTAG) static void * __failing_morecore (ptrdiff_t d) { return (void *) MORECORE_FAILURE; } +#endif +#ifdef SHARED extern struct dl_open_hook *_dl_open_hook; libc_hidden_proto (_dl_open_hook); #endif +#ifdef _LIBC_MTAG +static void * +__mtag_tag_new_usable (void *ptr) +{ + if (ptr) + ptr = __libc_mtag_tag_region (__libc_mtag_new_tag (ptr), + __malloc_usable_size (ptr)); + return ptr; +} + +static void * +__mtag_tag_new_memset (void *ptr, int val, size_t size) +{ + return __libc_mtag_memset_with_tag (__libc_mtag_new_tag (ptr), val, size); +} +#endif + static void ptmalloc_init (void) { @@ -293,6 +312,24 @@ ptmalloc_init (void) __malloc_initialized = 0; +#ifdef _LIBC_MTAG + if ((TUNABLE_GET_FULL (glibc, memtag, enable, int32_t, NULL) & 1) != 0) + { + /* If the environment says that we should be using tagged memory + and that morecore does not support tagged regions, then + disable it. */ + if (__MTAG_SBRK_UNTAGGED) + __morecore = __failing_morecore; + + __mtag_mmap_flags = __MTAG_MMAP_FLAGS; + __tag_new_memset = __mtag_tag_new_memset; + __tag_region = __libc_mtag_tag_region; + __tag_new_usable = __mtag_tag_new_usable; + __tag_at = __libc_mtag_address_get_tag; + __mtag_granule_mask = ~(size_t)(__MTAG_GRANULE_SIZE - 1); + } +#endif + #ifdef SHARED /* In case this libc copy is in a non-default namespace, never use brk. Likewise if dlopened from statically linked program. */ @@ -512,7 +549,7 @@ new_heap (size_t size, size_t top_pad) } } } - if (__mprotect (p2, size, PROT_READ | PROT_WRITE) != 0) + if (__mprotect (p2, size, MTAG_MMAP_FLAGS | PROT_READ | PROT_WRITE) != 0) { __munmap (p2, HEAP_MAX_SIZE); return 0; @@ -542,7 +579,7 @@ grow_heap (heap_info *h, long diff) { if (__mprotect ((char *) h + h->mprotect_size, (unsigned long) new_size - h->mprotect_size, - PROT_READ | PROT_WRITE) != 0) + MTAG_MMAP_FLAGS | PROT_READ | PROT_WRITE) != 0) return -2; h->mprotect_size = new_size; diff --git a/malloc/hooks.c b/malloc/hooks.c index a2b93e5446..52bb3863cd 100644 --- a/malloc/hooks.c +++ b/malloc/hooks.c @@ -56,6 +56,12 @@ static int using_malloc_checking; void __malloc_check_init (void) { +#if HAVE_TUNABLES && defined (_LIBC_MTAG) + /* If memory tagging is enabled, there is no need for the boundary + checking cookie as well. */ + if ((TUNABLE_GET_FULL (glibc, memtag, enable, int32_t, NULL) & 1) != 0) + return; +#endif using_malloc_checking = 1; __malloc_hook = malloc_check; __free_hook = free_check; diff --git a/malloc/malloc.c b/malloc/malloc.c index ee87ddbbf9..d4adb0bf81 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -242,6 +242,9 @@ /* For DIAG_PUSH/POP_NEEDS_COMMENT et al. */ #include +/* For memory tagging. */ +#include + #include /* For SINGLE_THREAD_P. */ @@ -378,6 +381,52 @@ __malloc_assert (const char *assertion, const char *file, unsigned int line, void * __default_morecore (ptrdiff_t); void *(*__morecore)(ptrdiff_t) = __default_morecore; +#ifdef _LIBC_MTAG +static void * +__default_tag_region (void *ptr, size_t size) +{ + return ptr; +} + +static void * +__default_tag_nop (void *ptr) +{ + return ptr; +} + +static int __mtag_mmap_flags = 0; +static size_t __mtag_granule_mask = ~(size_t)0; + +static void *(*__tag_new_memset)(void *, int, size_t) = memset; +static void *(*__tag_region)(void *, size_t) = __default_tag_region; +static void *(*__tag_new_usable)(void *) = __default_tag_nop; +static void *(*__tag_at)(void *) = __default_tag_nop; + +# define TAG_NEW_MEMSET(ptr, val, size) __tag_new_memset (ptr, val, size) +# define TAG_REGION(ptr, size) __tag_region (ptr, size) +# define TAG_NEW_USABLE(ptr) __tag_new_usable (ptr) +# define TAG_AT(ptr) __tag_at (ptr) +#else +# define TAG_NEW_MEMSET(ptr, val, size) memset (ptr, val, size) +# define TAG_REGION(ptr, size) (ptr) +# define TAG_NEW_USABLE(ptr) (ptr) +# define TAG_AT(ptr) (ptr) +#endif + +/* When using tagged memory, we cannot share the end of the user block + with the header for the next chunk, so ensure that we allocate + blocks that are rounded up to the granule size. Take care not to + overflow from close to MAX_SIZE_T to a small number. */ +static inline size_t +ROUND_UP_ALLOCATION_SIZE(size_t bytes) +{ +#ifdef _LIBC_MTAG + return (bytes + ~__mtag_granule_mask) & __mtag_granule_mask; +#else + return bytes; +#endif +} + #include @@ -1184,8 +1233,9 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /* conversion from malloc headers to user pointers, and back */ -#define chunk2mem(p) ((void*)((char*)(p) + 2*SIZE_SZ)) -#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ)) +#define chunk2mem(p) ((void*)TAG_AT (((char*)(p) + 2*SIZE_SZ))) +#define chunk2rawmem(p) ((void*)((char*)(p) + 2*SIZE_SZ)) +#define mem2chunk(mem) ((mchunkptr)TAG_AT (((char*)(mem) - 2*SIZE_SZ))) /* The smallest possible chunk */ #define MIN_CHUNK_SIZE (offsetof(struct malloc_chunk, fd_nextsize)) @@ -1219,6 +1269,7 @@ checked_request2size (size_t req, size_t *sz) __nonnull (1) { if (__glibc_unlikely (req > PTRDIFF_MAX)) return false; + req = ROUND_UP_ALLOCATION_SIZE (req); *sz = request2size (req); return true; } @@ -1964,7 +2015,7 @@ do_check_chunk (mstate av, mchunkptr p) /* chunk is page-aligned */ assert (((prev_size (p) + sz) & (GLRO (dl_pagesize) - 1)) == 0); /* mem is aligned */ - assert (aligned_OK (chunk2mem (p))); + assert (aligned_OK (chunk2rawmem (p))); } } @@ -1988,7 +2039,7 @@ do_check_free_chunk (mstate av, mchunkptr p) if ((unsigned long) (sz) >= MINSIZE) { assert ((sz & MALLOC_ALIGN_MASK) == 0); - assert (aligned_OK (chunk2mem (p))); + assert (aligned_OK (chunk2rawmem (p))); /* ... matching footer field */ assert (prev_size (next_chunk (p)) == sz); /* ... and is fully consolidated */ @@ -2067,7 +2118,7 @@ do_check_remalloced_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T s) assert ((sz & MALLOC_ALIGN_MASK) == 0); assert ((unsigned long) (sz) >= MINSIZE); /* ... and alignment */ - assert (aligned_OK (chunk2mem (p))); + assert (aligned_OK (chunk2rawmem (p))); /* chunk is less than MINSIZE more than request */ assert ((long) (sz) - (long) (s) >= 0); assert ((long) (sz) - (long) (s + MINSIZE) < 0); @@ -2322,7 +2373,8 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av) /* Don't try if size wraps around 0 */ if ((unsigned long) (size) > (unsigned long) (nb)) { - mm = (char *) (MMAP (0, size, PROT_READ | PROT_WRITE, 0)); + mm = (char *) (MMAP (0, size, + MTAG_MMAP_FLAGS | PROT_READ | PROT_WRITE, 0)); if (mm != MAP_FAILED) { @@ -2336,14 +2388,14 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av) if (MALLOC_ALIGNMENT == 2 * SIZE_SZ) { - /* For glibc, chunk2mem increases the address by 2*SIZE_SZ and + /* For glibc, chunk2rawmem increases the address by 2*SIZE_SZ and MALLOC_ALIGN_MASK is 2*SIZE_SZ-1. Each mmap'ed area is page aligned and therefore definitely MALLOC_ALIGN_MASK-aligned. */ - assert (((INTERNAL_SIZE_T) chunk2mem (mm) & MALLOC_ALIGN_MASK) == 0); + assert (((INTERNAL_SIZE_T) chunk2rawmem (mm) & MALLOC_ALIGN_MASK) == 0); front_misalign = 0; } else - front_misalign = (INTERNAL_SIZE_T) chunk2mem (mm) & MALLOC_ALIGN_MASK; + front_misalign = (INTERNAL_SIZE_T) chunk2rawmem (mm) & MALLOC_ALIGN_MASK; if (front_misalign > 0) { correction = MALLOC_ALIGNMENT - front_misalign; @@ -2515,7 +2567,9 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av) /* Don't try if size wraps around 0 */ if ((unsigned long) (size) > (unsigned long) (nb)) { - char *mbrk = (char *) (MMAP (0, size, PROT_READ | PROT_WRITE, 0)); + char *mbrk = (char *) (MMAP (0, size, + MTAG_MMAP_FLAGS | PROT_READ | PROT_WRITE, + 0)); if (mbrk != MAP_FAILED) { @@ -2586,7 +2640,7 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av) /* Guarantee alignment of first new chunk made from this space */ - front_misalign = (INTERNAL_SIZE_T) chunk2mem (brk) & MALLOC_ALIGN_MASK; + front_misalign = (INTERNAL_SIZE_T) chunk2rawmem (brk) & MALLOC_ALIGN_MASK; if (front_misalign > 0) { /* @@ -2644,10 +2698,10 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av) { if (MALLOC_ALIGNMENT == 2 * SIZE_SZ) /* MORECORE/mmap must correctly align */ - assert (((unsigned long) chunk2mem (brk) & MALLOC_ALIGN_MASK) == 0); + assert (((unsigned long) chunk2rawmem (brk) & MALLOC_ALIGN_MASK) == 0); else { - front_misalign = (INTERNAL_SIZE_T) chunk2mem (brk) & MALLOC_ALIGN_MASK; + front_misalign = (INTERNAL_SIZE_T) chunk2rawmem (brk) & MALLOC_ALIGN_MASK; if (front_misalign > 0) { /* @@ -2832,7 +2886,7 @@ munmap_chunk (mchunkptr p) if (DUMPED_MAIN_ARENA_CHUNK (p)) return; - uintptr_t mem = (uintptr_t) chunk2mem (p); + uintptr_t mem = (uintptr_t) chunk2rawmem (p); uintptr_t block = (uintptr_t) p - prev_size (p); size_t total_size = prev_size (p) + size; /* Unfortunately we have to do the compilers job by hand here. Normally @@ -2887,7 +2941,7 @@ mremap_chunk (mchunkptr p, size_t new_size) p = (mchunkptr) (cp + offset); - assert (aligned_OK (chunk2mem (p))); + assert (aligned_OK (chunk2rawmem (p))); assert (prev_size (p) == offset); set_head (p, (new_size - offset) | IS_MMAPPED); @@ -3068,14 +3122,15 @@ __libc_malloc (size_t bytes) && tcache && tcache->counts[tc_idx] > 0) { - return tcache_get (tc_idx); + victim = tcache_get (tc_idx); + return TAG_NEW_USABLE (victim); } DIAG_POP_NEEDS_COMMENT; #endif if (SINGLE_THREAD_P) { - victim = _int_malloc (&main_arena, bytes); + victim = TAG_NEW_USABLE (_int_malloc (&main_arena, bytes)); assert (!victim || chunk_is_mmapped (mem2chunk (victim)) || &main_arena == arena_for_chunk (mem2chunk (victim))); return victim; @@ -3096,6 +3151,8 @@ __libc_malloc (size_t bytes) if (ar_ptr != NULL) __libc_lock_unlock (ar_ptr->mutex); + victim = TAG_NEW_USABLE (victim); + assert (!victim || chunk_is_mmapped (mem2chunk (victim)) || ar_ptr == arena_for_chunk (mem2chunk (victim))); return victim; @@ -3119,8 +3176,17 @@ __libc_free (void *mem) if (mem == 0) /* free(0) has no effect */ return; +#ifdef _LIBC_MTAG + /* Quickly check that the freed pointer matches the tag for the memory. + This gives a useful double-free detection. */ + *(volatile char *)mem; +#endif + p = mem2chunk (mem); + /* Mark the chunk as belonging to the library again. */ + (void)TAG_REGION (chunk2rawmem (p), __malloc_usable_size (mem)); + if (chunk_is_mmapped (p)) /* release mmapped memory. */ { /* See if the dynamic brk/mmap threshold needs adjusting. @@ -3170,6 +3236,12 @@ __libc_realloc (void *oldmem, size_t bytes) if (oldmem == 0) return __libc_malloc (bytes); +#ifdef _LIBC_MTAG + /* Perform a quick check to ensure that the pointer's tag matches the + memory's tag. */ + *(volatile char*) oldmem; +#endif + /* chunk corresponding to oldmem */ const mchunkptr oldp = mem2chunk (oldmem); /* its size */ @@ -3225,7 +3297,15 @@ __libc_realloc (void *oldmem, size_t bytes) #if HAVE_MREMAP newp = mremap_chunk (oldp, nb); if (newp) - return chunk2mem (newp); + { + void *newmem = chunk2rawmem (newp); + /* Give the new block a different tag. This helps to ensure + that stale handles to the previous mapping are not + reused. There's a performance hit for both us and the + caller for doing this, so we might want to + reconsider. */ + return TAG_NEW_USABLE (newmem); + } #endif /* Note the extra SIZE_SZ overhead. */ if (oldsize - SIZE_SZ >= nb) @@ -3308,7 +3388,6 @@ _mid_memalign (size_t alignment, size_t bytes, void *address) return 0; } - /* Make sure alignment is power of 2. */ if (!powerof2 (alignment)) { @@ -3323,8 +3402,7 @@ _mid_memalign (size_t alignment, size_t bytes, void *address) p = _int_memalign (&main_arena, alignment, bytes); assert (!p || chunk_is_mmapped (mem2chunk (p)) || &main_arena == arena_for_chunk (mem2chunk (p))); - - return p; + return TAG_NEW_USABLE (p); } arena_get (ar_ptr, bytes + alignment + MINSIZE); @@ -3342,7 +3420,7 @@ _mid_memalign (size_t alignment, size_t bytes, void *address) assert (!p || chunk_is_mmapped (mem2chunk (p)) || ar_ptr == arena_for_chunk (mem2chunk (p))); - return p; + return TAG_NEW_USABLE (p); } /* For ISO C11. */ weak_alias (__libc_memalign, aligned_alloc) @@ -3351,17 +3429,22 @@ libc_hidden_def (__libc_memalign) void * __libc_valloc (size_t bytes) { + void *p; + if (__malloc_initialized < 0) ptmalloc_init (); void *address = RETURN_ADDRESS (0); size_t pagesize = GLRO (dl_pagesize); - return _mid_memalign (pagesize, bytes, address); + p = _mid_memalign (pagesize, bytes, address); + return TAG_NEW_USABLE (p); } void * __libc_pvalloc (size_t bytes) { + void *p; + if (__malloc_initialized < 0) ptmalloc_init (); @@ -3378,19 +3461,22 @@ __libc_pvalloc (size_t bytes) } rounded_bytes = rounded_bytes & -(pagesize - 1); - return _mid_memalign (pagesize, rounded_bytes, address); + p = _mid_memalign (pagesize, rounded_bytes, address); + return TAG_NEW_USABLE (p); } void * __libc_calloc (size_t n, size_t elem_size) { mstate av; - mchunkptr oldtop, p; - INTERNAL_SIZE_T sz, csz, oldtopsize; + mchunkptr oldtop; + INTERNAL_SIZE_T sz, oldtopsize; void *mem; +#ifndef _LIBC_MTAG unsigned long clearsize; unsigned long nclears; INTERNAL_SIZE_T *d; +#endif ptrdiff_t bytes; if (__glibc_unlikely (__builtin_mul_overflow (n, elem_size, &bytes))) @@ -3398,6 +3484,7 @@ __libc_calloc (size_t n, size_t elem_size) __set_errno (ENOMEM); return NULL; } + sz = bytes; void *(*hook) (size_t, const void *) = @@ -3467,7 +3554,14 @@ __libc_calloc (size_t n, size_t elem_size) if (mem == 0) return 0; - p = mem2chunk (mem); + /* If we are using memory tagging, then we need to set the tags + regardless of MORECORE_CLEARS, so we zero the whole block while + doing so. */ +#ifdef _LIBC_MTAG + return TAG_NEW_MEMSET (mem, 0, __malloc_usable_size (mem)); +#else + mchunkptr p = mem2chunk (mem); + INTERNAL_SIZE_T csz = chunksize (p); /* Two optional cases in which clearing not necessary */ if (chunk_is_mmapped (p)) @@ -3478,8 +3572,6 @@ __libc_calloc (size_t n, size_t elem_size) return mem; } - csz = chunksize (p); - #if MORECORE_CLEARS if (perturb_byte == 0 && (p == oldtop && csz > oldtopsize)) { @@ -3522,6 +3614,7 @@ __libc_calloc (size_t n, size_t elem_size) } return mem; +#endif } /* @@ -4618,7 +4711,7 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize, av->top = chunk_at_offset (oldp, nb); set_head (av->top, (newsize - nb) | PREV_INUSE); check_inuse_chunk (av, oldp); - return chunk2mem (oldp); + return TAG_NEW_USABLE (chunk2rawmem (oldp)); } /* Try to expand forward into next chunk; split off remainder below */ @@ -4651,7 +4744,10 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize, } else { - memcpy (newmem, chunk2mem (oldp), oldsize - SIZE_SZ); + void *oldmem = chunk2mem (oldp); + newmem = TAG_NEW_USABLE (newmem); + memcpy (newmem, oldmem, __malloc_usable_size (oldmem)); + (void) TAG_REGION (chunk2rawmem (oldp), oldsize); _int_free (av, oldp, 1); check_inuse_chunk (av, newp); return chunk2mem (newp); @@ -4673,6 +4769,8 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize, else /* split remainder */ { remainder = chunk_at_offset (newp, nb); + /* Clear any user-space tags before writing the header. */ + remainder = TAG_REGION (remainder, remainder_size); set_head_size (newp, nb | (av != &main_arena ? NON_MAIN_ARENA : 0)); set_head (remainder, remainder_size | PREV_INUSE | (av != &main_arena ? NON_MAIN_ARENA : 0)); @@ -4682,8 +4780,8 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize, } check_inuse_chunk (av, newp); - return chunk2mem (newp); -} + return TAG_NEW_USABLE (chunk2rawmem (newp)); + } /* ------------------------------ memalign ------------------------------ @@ -4760,7 +4858,7 @@ _int_memalign (mstate av, size_t alignment, size_t bytes) p = newp; assert (newsize >= nb && - (((unsigned long) (chunk2mem (p))) % alignment) == 0); + (((unsigned long) (chunk2rawmem (p))) % alignment) == 0); } /* Also give back spare room at the end */ @@ -4814,7 +4912,7 @@ mtrim (mstate av, size_t pad) + sizeof (struct malloc_chunk) + psm1) & ~psm1); - assert ((char *) chunk2mem (p) + 4 * SIZE_SZ <= paligned_mem); + assert ((char *) chunk2rawmem (p) + 4 * SIZE_SZ <= paligned_mem); assert ((char *) p + size > paligned_mem); /* This is the size we could potentially free. */ @@ -4877,20 +4975,30 @@ musable (void *mem) mchunkptr p; if (mem != 0) { + size_t result = 0; + p = mem2chunk (mem); if (__builtin_expect (using_malloc_checking == 1, 0)) - return malloc_check_get_size (p); + return malloc_check_get_size (p); if (chunk_is_mmapped (p)) { if (DUMPED_MAIN_ARENA_CHUNK (p)) - return chunksize (p) - SIZE_SZ; + result = chunksize (p) - SIZE_SZ; else - return chunksize (p) - 2 * SIZE_SZ; + result = chunksize (p) - 2 * SIZE_SZ; } else if (inuse (p)) - return chunksize (p) - SIZE_SZ; + result = chunksize (p) - SIZE_SZ; + +#ifdef _LIBC_MTAG + /* The usable space may be reduced if memory tagging is needed, + since we cannot share the user-space data with malloc's internal + data structure. */ + result &= __mtag_granule_mask; +#endif + return result; } return 0; } diff --git a/malloc/malloc.h b/malloc/malloc.h index a6903fdd54..d012da9a9f 100644 --- a/malloc/malloc.h +++ b/malloc/malloc.h @@ -77,6 +77,13 @@ extern void *pvalloc (size_t __size) __THROW __attribute_malloc__ __wur; contiguous pieces of memory. */ extern void *(*__morecore) (ptrdiff_t __size); +#ifdef _LIBC_MTAG +extern int __mtag_mmap_flags; +#define MTAG_MMAP_FLAGS __mtag_mmap_flags +#else +#define MTAG_MMAP_FLAGS 0 +#endif + /* Default value of `__morecore'. */ extern void *__default_morecore (ptrdiff_t __size) __THROW __attribute_malloc__; diff --git a/sysdeps/generic/libc-mtag.h b/sysdeps/generic/libc-mtag.h new file mode 100644 index 0000000000..3e9885451c --- /dev/null +++ b/sysdeps/generic/libc-mtag.h @@ -0,0 +1,52 @@ +/* libc-internal interface for tagged (colored) memory support. + Copyright (C) 2019 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 _GENERIC_LIBC_MTAG_H +#define _GENERIC_LIBC_MTAG_H 1 + +/* Generic bindings for systems that do not support memory tagging. */ + +/* Used to ensure additional alignment when objects need to have distinct + tags. */ +#define __MTAG_GRANULE_SIZE 1 + +/* Non-zero if memory obtained via morecore (sbrk) is not tagged. */ +#define __MTAG_SBRK_UNTAGGED 0 + +/* Extra flags to pass to mmap() to request a tagged region of memory. */ +#define __MTAG_MMAP_FLAGS 0 + +/* Set the tags for a region of memory, which must have size and alignment + that are multiples of __MTAG_GRANULE_SIZE. Size cannot be zero. + void *__libc_mtag_tag_region (const void *, size_t) */ +#define __libc_mtag_tag_region(p, s) (p) + +/* Optimized equivalent to __libc_mtag_tag_region followed by memset. */ +#define __libc_mtag_memset_with_tag memset + +/* Convert address P to a pointer that is tagged correctly for that + location. + void *__libc_mtag_address_get_tag (void*) */ +#define __libc_mtag_address_get_tag(p) (p) + +/* Assign a new (random) tag to a pointer P (does not adjust the tag on + the memory addressed). + void *__libc_mtag_new_tag (void*) */ +#define __libc_mtag_new_tag(p) (p) + +#endif /* _GENERIC_LIBC_MTAG_H */ From patchwork Wed Jun 24 15:53:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Earnshaw X-Patchwork-Id: 39788 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 34CA43987870; Wed, 24 Jun 2020 15:54:05 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 0D8B73840C21 for ; Wed, 24 Jun 2020 15:54:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0D8B73840C21 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rearnsha@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B7A56101E; Wed, 24 Jun 2020 08:53:59 -0700 (PDT) Received: from eagle.buzzard.freeserve.co.uk (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 440A63F73C; Wed, 24 Jun 2020 08:53:59 -0700 (PDT) From: Richard Earnshaw To: libc-alpha@sourceware.org Subject: [PATCH v2 4/6] linux: Add compatibility definitions to sys/prctl.h for MTE Date: Wed, 24 Jun 2020 16:53:47 +0100 Message-Id: <20200624155349.12134-5-rearnsha@arm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200624155349.12134-1-rearnsha@arm.com> References: <20200624155349.12134-1-rearnsha@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: Richard Earnshaw Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Older versions of the Linux kernel headers obviously lack support for memory tagging, but we still want to be able to build in support when using those (obviously it can't be enabled on such systems). The linux kernel extensions are made to the platform-independent header (linux/prctl.h), so this patch takes a similar approach. --- sysdeps/unix/sysv/linux/sys/prctl.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/sysdeps/unix/sysv/linux/sys/prctl.h b/sysdeps/unix/sysv/linux/sys/prctl.h index 7f748ebeeb..4d01379c23 100644 --- a/sysdeps/unix/sysv/linux/sys/prctl.h +++ b/sysdeps/unix/sysv/linux/sys/prctl.h @@ -21,6 +21,24 @@ #include #include /* The magic values come from here */ +/* Recent extensions to linux which may post-date the kernel headers + we're picking up... */ + +/* Memory tagging control operations (for AArch64). */ +#ifndef PR_TAGGED_ADDR_ENABLE +# define PR_TAGGED_ADDR_ENABLE (1UL << 8) +#endif + +#ifndef PR_MTE_TCF_SHIFT +# define PR_MTE_TCF_SHIFT 1 +# define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TAG_SHIFT 3 +# define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT) +#endif + __BEGIN_DECLS /* Control process execution. */ From patchwork Wed Jun 24 15:53:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Earnshaw X-Patchwork-Id: 39790 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 E75303987876; Wed, 24 Jun 2020 15:54:05 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 081BE3987859 for ; Wed, 24 Jun 2020 15:54:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 081BE3987859 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rearnsha@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 740E61FB; Wed, 24 Jun 2020 08:54:00 -0700 (PDT) Received: from eagle.buzzard.freeserve.co.uk (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F31833F73C; Wed, 24 Jun 2020 08:53:59 -0700 (PDT) From: Richard Earnshaw To: libc-alpha@sourceware.org Subject: [PATCH v2 5/6] aarch64: Add sysv specific enabling code for memory tagging Date: Wed, 24 Jun 2020 16:53:48 +0100 Message-Id: <20200624155349.12134-6-rearnsha@arm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200624155349.12134-1-rearnsha@arm.com> References: <20200624155349.12134-1-rearnsha@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: Richard Earnshaw Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Add various defines and stubs for enabling MTE on AArch64 sysv-like systems such as Linux. The HWCAP feature bit is copied over in the same way as other feature bits. Similarly we add a new wrapper header for mman.h to define the PROT_MTE flag that can be used with mmap and related functions. We add a new field to struct cpu_features that can be used, for example, to check whether or not certain ifunc'd routines should be bound to MTE-safe versions. Finally, if we detect that MTE should be enabled (ie via the glibc tunable); we enable MTE during startup as required. --- sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h | 1 + sysdeps/unix/sysv/linux/aarch64/bits/mman.h | 32 +++++++++++++++++++ .../unix/sysv/linux/aarch64/cpu-features.c | 28 ++++++++++++++++ .../unix/sysv/linux/aarch64/cpu-features.h | 1 + 4 files changed, 62 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/aarch64/bits/mman.h diff --git a/sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h b/sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h index 4ee14b4208..ee078ee534 100644 --- a/sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h +++ b/sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h @@ -72,3 +72,4 @@ #define HWCAP2_BF16 (1 << 14) #define HWCAP2_DGH (1 << 15) #define HWCAP2_RNG (1 << 16) +#define HWCAP2_MTE (1 << 18) diff --git a/sysdeps/unix/sysv/linux/aarch64/bits/mman.h b/sysdeps/unix/sysv/linux/aarch64/bits/mman.h new file mode 100644 index 0000000000..fa3f3a31f4 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/bits/mman.h @@ -0,0 +1,32 @@ +/* Definitions for POSIX memory map interface. Linux/aarch64 version. + Copyright (C) 2020 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_MMAN_H +# error "Never use directly; include instead." +#endif + +/* The following definitions basically come from the kernel headers. + But the kernel header is not namespace clean. */ + +/* Other flags. */ +#define PROT_MTE 0x20 /* Normal Tagged mapping. */ + +#include + +/* Include generic Linux declarations. */ +#include diff --git a/sysdeps/unix/sysv/linux/aarch64/cpu-features.c b/sysdeps/unix/sysv/linux/aarch64/cpu-features.c index 896c588fee..6f91edd345 100644 --- a/sysdeps/unix/sysv/linux/aarch64/cpu-features.c +++ b/sysdeps/unix/sysv/linux/aarch64/cpu-features.c @@ -19,6 +19,7 @@ #include #include #include +#include #define DCZID_DZP_MASK (1 << 4) #define DCZID_BS_MASK (0xf) @@ -83,4 +84,31 @@ init_cpu_features (struct cpu_features *cpu_features) if ((dczid & DCZID_DZP_MASK) == 0) cpu_features->zva_size = 4 << (dczid & DCZID_BS_MASK); + + /* Setup memory tagging support if the HW and kernel support it, and if + the user has requested it. */ + cpu_features->mte_state = 0; + +#ifdef _LIBC_MTAG +# if HAVE_TUNABLES + int mte_state = TUNABLE_GET (glibc, memtag, enable, unsigned, 0); + cpu_features->mte_state = (GLRO (dl_hwcap2) & HWCAP2_MTE) ? mte_state : 0; + /* If we lack the MTE feature, disable the tunable, since it will + otherwise cause instructions that won't run on this CPU to be used. */ + TUNABLE_SET (glibc, memtag, enable, unsigned, cpu_features->mte_state); +# endif + + /* For now, disallow tag 0, so that we can clearly see when tagged + addresses are being allocated. */ + if (cpu_features->mte_state & 2) + __prctl (PR_SET_TAGGED_ADDR_CTRL, + (PR_TAGGED_ADDR_ENABLE | PR_MTE_TCF_SYNC + | (0xfffe << PR_MTE_TAG_SHIFT)), + 0, 0, 0); + else if (cpu_features->mte_state) + __prctl (PR_SET_TAGGED_ADDR_CTRL, + (PR_TAGGED_ADDR_ENABLE | PR_MTE_TCF_ASYNC + | (0xfffe << PR_MTE_TAG_SHIFT)), + 0, 0, 0); +#endif } diff --git a/sysdeps/unix/sysv/linux/aarch64/cpu-features.h b/sysdeps/unix/sysv/linux/aarch64/cpu-features.h index 1389cea1b3..604de27c88 100644 --- a/sysdeps/unix/sysv/linux/aarch64/cpu-features.h +++ b/sysdeps/unix/sysv/linux/aarch64/cpu-features.h @@ -64,6 +64,7 @@ struct cpu_features { uint64_t midr_el1; unsigned zva_size; + unsigned mte_state; }; #endif /* _CPU_FEATURES_AARCH64_H */ From patchwork Wed Jun 24 15:53:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Earnshaw X-Patchwork-Id: 39791 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 487A13987889; Wed, 24 Jun 2020 15:54:06 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 9CCE63987870 for ; Wed, 24 Jun 2020 15:54:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9CCE63987870 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rearnsha@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3B08231B; Wed, 24 Jun 2020 08:54:01 -0700 (PDT) Received: from eagle.buzzard.freeserve.co.uk (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id AA59B3F73C; Wed, 24 Jun 2020 08:54:00 -0700 (PDT) From: Richard Earnshaw To: libc-alpha@sourceware.org Subject: [PATCH v2 6/6] aarch64: Add aarch64-specific files for memory tagging support Date: Wed, 24 Jun 2020 16:53:49 +0100 Message-Id: <20200624155349.12134-7-rearnsha@arm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200624155349.12134-1-rearnsha@arm.com> References: <20200624155349.12134-1-rearnsha@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: Richard Earnshaw Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" This final patch provides the architecture-specific implementation of the memory-tagging support hooks for aarch64. --- sysdeps/aarch64/Makefile | 5 +++ sysdeps/aarch64/__mtag_address_get_tag.S | 31 +++++++++++++ sysdeps/aarch64/__mtag_memset_tag.S | 46 +++++++++++++++++++ sysdeps/aarch64/__mtag_new_tag.S | 38 ++++++++++++++++ sysdeps/aarch64/__mtag_tag_region.S | 44 ++++++++++++++++++ sysdeps/aarch64/libc-mtag.h | 57 ++++++++++++++++++++++++ 6 files changed, 221 insertions(+) create mode 100644 sysdeps/aarch64/__mtag_address_get_tag.S create mode 100644 sysdeps/aarch64/__mtag_memset_tag.S create mode 100644 sysdeps/aarch64/__mtag_new_tag.S create mode 100644 sysdeps/aarch64/__mtag_tag_region.S create mode 100644 sysdeps/aarch64/libc-mtag.h diff --git a/sysdeps/aarch64/Makefile b/sysdeps/aarch64/Makefile index 9cb141004d..34b5aa7f6e 100644 --- a/sysdeps/aarch64/Makefile +++ b/sysdeps/aarch64/Makefile @@ -21,4 +21,9 @@ endif ifeq ($(subdir),misc) sysdep_headers += sys/ifunc.h +sysdep_routines += __mtag_tag_region __mtag_new_tag __mtag_address_get_tag +endif + +ifeq ($(subdir),string) +sysdep_routines += __mtag_memset_tag endif diff --git a/sysdeps/aarch64/__mtag_address_get_tag.S b/sysdeps/aarch64/__mtag_address_get_tag.S new file mode 100644 index 0000000000..654c9d660c --- /dev/null +++ b/sysdeps/aarch64/__mtag_address_get_tag.S @@ -0,0 +1,31 @@ +/* Copyright (C) 2020 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 + +#define ptr x0 + + .arch armv8.5-a + .arch_extension memtag + +ENTRY (__libc_mtag_address_get_tag) + + ldg ptr, [ptr] + ret +END (__libc_mtag_address_get_tag) +libc_hidden_builtin_def (__libc_mtag_address_get_tag) diff --git a/sysdeps/aarch64/__mtag_memset_tag.S b/sysdeps/aarch64/__mtag_memset_tag.S new file mode 100644 index 0000000000..bc98dc49d2 --- /dev/null +++ b/sysdeps/aarch64/__mtag_memset_tag.S @@ -0,0 +1,46 @@ +/* Copyright (C) 2020 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 +/* Use the same register names and assignments as memset. */ +#include "memset-reg.h" + + .arch armv8.5-a + .arch_extension memtag + +/* NB, only supported on variants with 64-bit pointers. */ + +/* FIXME: This is a minimal implementation. We could do much better than + this for large values of COUNT. */ + +ENTRY_ALIGN(__libc_mtag_memset_with_tag, 6) + + and valw, valw, 255 + orr valw, valw, valw, lsl 8 + orr valw, valw, valw, lsl 16 + orr val, val, val, lsl 32 + mov dst, dstin + +L(loop): + stgp val, val, [dst], #16 + subs count, count, 16 + bne L(loop) + ldg dstin, [dstin] // Recover the tag created (might be untagged). + ret +END (__libc_mtag_memset_with_tag) +libc_hidden_builtin_def (__libc_mtag_memset_with_tag) diff --git a/sysdeps/aarch64/__mtag_new_tag.S b/sysdeps/aarch64/__mtag_new_tag.S new file mode 100644 index 0000000000..3a22995e9f --- /dev/null +++ b/sysdeps/aarch64/__mtag_new_tag.S @@ -0,0 +1,38 @@ +/* Copyright (C) 2020 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 + + .arch armv8.5-a + .arch_extension memtag + +/* NB, only supported on variants with 64-bit pointers. */ + +/* FIXME: This is a minimal implementation. We could do better than + this for larger values of COUNT. */ + +#define ptr x0 +#define xset x1 + +ENTRY(__libc_mtag_new_tag) + // Guarantee that the new tag is not the same as now. + gmi xset, ptr, xzr + irg ptr, ptr, xset + ret +END (__libc_mtag_new_tag) +libc_hidden_builtin_def (__libc_mtag_new_tag) diff --git a/sysdeps/aarch64/__mtag_tag_region.S b/sysdeps/aarch64/__mtag_tag_region.S new file mode 100644 index 0000000000..41019781d0 --- /dev/null +++ b/sysdeps/aarch64/__mtag_tag_region.S @@ -0,0 +1,44 @@ +/* Copyright (C) 2020 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 +/* Use the same register names and assignments as memset. */ + + .arch armv8.5-a + .arch_extension memtag + +/* NB, only supported on variants with 64-bit pointers. */ + +/* FIXME: This is a minimal implementation. We could do better than + this for larger values of COUNT. */ + +#define dstin x0 +#define count x1 +#define dst x2 + +ENTRY_ALIGN(__libc_mtag_tag_region, 6) + + mov dst, dstin +L(loop): + stg dst, [dst], #16 + subs count, count, 16 + bne L(loop) + ldg dstin, [dstin] // Recover the tag created (might be untagged). + ret +END (__libc_mtag_tag_region) +libc_hidden_builtin_def (__libc_mtag_tag_region) diff --git a/sysdeps/aarch64/libc-mtag.h b/sysdeps/aarch64/libc-mtag.h new file mode 100644 index 0000000000..9c7d00c541 --- /dev/null +++ b/sysdeps/aarch64/libc-mtag.h @@ -0,0 +1,57 @@ +/* libc-internal interface for tagged (colored) memory support. + Copyright (C) 2020 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 _AARCH64_LIBC_MTAG_H +#define _AARCH64_LIBC_MTAG_H 1 + +#ifndef _LIBC_MTAG +/* Generic bindings for systems that do not support memory tagging. */ +#include_next "libc-mtag.h" +#else + +/* Used to ensure additional alignment when objects need to have distinct + tags. */ +#define __MTAG_GRANULE_SIZE 16 + +/* Non-zero if memory obtained via morecore (sbrk) is not tagged. */ +#define __MTAG_SBRK_UNTAGGED 1 + +/* Extra flags to pass to mmap to get tagged pages. */ +#define __MTAG_MMAP_FLAGS PROT_MTE + +/* Set the tags for a region of memory, which must have size and alignment + that are multiples of __MTAG_GRANULE_SIZE. Size cannot be zero. + void *__libc_mtag_tag_region (const void *, size_t) */ +void *__libc_mtag_tag_region (void *, size_t); + +/* Optimized equivalent to __libc_mtag_tag_region followed by memset. */ +void *__libc_mtag_memset_with_tag(void *, int, size_t); + +/* Convert address P to a pointer that is tagged correctly for that + location. + void *__libc_mtag_address_get_tag (void*) */ +void *__libc_mtag_address_get_tag(void *); + +/* Assign a new (random) tag to a pointer P (does not adjust the tag on + the memory addressed). + void *__libc_mtag_new_tag (void*) */ +void *__libc_mtag_new_tag(void *); + +#endif /* _LIBC_MTAG */ + +#endif /* _AARCH64_LIBC_MTAG_H */