From e1fb22721027deb97a20b3c656506e2cb084b2aa Mon Sep 17 00:00:00 2001 From: Dejice Jacob Date: Sun, 20 Oct 2024 00:34:55 +0300 Subject: [PATCH] Provide correct GC_uintptr_t, CPP_PTRSZ, GC_GRANULE_PTRS for CHERI (a cherry-pick of commits 8120c8b, 4f431ac partly from capablevms/bdwgc) Issue #627 (bdwgc). Changes specific to CHERI capability systems are conditionally compiled using the macro __CHERI_PURE_CAPABILITY__. * include/gc/gc.h [_UINTPTR_T_DECLARED && !__MSYS__] (GC_uintptr_t): Define to uintptr_t. * include/gc/gc.h [__GNUC__ && !__INTEL_COMPILER && __CHERI_PURE_CAPABILITY__] (GC_reachable_here): Use "g" constraint instead of "X". * include/gc/gc_config_macros.h [__GNUC__ && __CHERI_PURE_CAPABILITY__]: Include stdint.h; reformat comment. * include/gc/gc_tiny_fl.h [!GC_GRANULE_BYTES && __CHERI_PURE_CAPABILITY__] (GC_GRANULE_PTRS): Define to 1 (instead of 2). * include/private/gcconfig.h [__CHERI_PURE_CAPABILITY__ && USE_MMAP] (USE_MUNMAP): Undefine; add TODO item. * include/private/gcconfig.h [!CPP_PTRSZ && __CHERI_PURE_CAPABILITY__] (CPP_PTRSZ): Define to __SIZEOF_POINTER__*8 instead of CPP_WORDSZ. Co-authored-by: Ivan Maidanski --- include/gc/gc.h | 8 +++++--- include/gc/gc_config_macros.h | 10 +++++----- include/gc/gc_tiny_fl.h | 6 +++++- include/private/gcconfig.h | 11 ++++++++++- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/include/gc/gc.h b/include/gc/gc.h index f17e1e3be..c1716d73f 100644 --- a/include/gc/gc.h +++ b/include/gc/gc.h @@ -58,8 +58,10 @@ typedef GC_SIGNEDWORD GC_signed_word; #undef GC_SIGNEDWORD #undef GC_UNSIGNEDWORD -#if (defined(_UINTPTR_T) || defined(_UINTPTR_T_DEFINED)) && !defined(__MSYS__) -/* Note: MSYS2 might provide __uintptr_t instead of uintptr_t. */ +#if (defined(_UINTPTR_T) || defined(_UINTPTR_T_DECLARED) \ + || defined(_UINTPTR_T_DEFINED)) \ + && !defined(__MSYS__) +/* Note: MSYS2 might provide __uintptr_t but not uintptr_t. */ typedef uintptr_t GC_uintptr_t; #else typedef GC_word GC_uintptr_t; @@ -1476,7 +1478,7 @@ GC_API int GC_CALL GC_invoke_finalizers(void); #if defined(__GNUC__) && !defined(__INTEL_COMPILER) \ && !(defined(__APPLE__) && defined(__arm__) && defined(__TINYC__)) /* TCC (as of v0.9.28rc) does not support asm on macOS/arm. */ -# if defined(__TINYC__) +# if defined(__CHERI_PURE_CAPABILITY__) || defined(__TINYC__) # define GC_reachable_here(ptr) \ __asm__ __volatile__(" " : : "g"(ptr) : "memory") # elif defined(__e2k__) diff --git a/include/gc/gc_config_macros.h b/include/gc/gc_config_macros.h index 9baf0f219..20080e6df 100644 --- a/include/gc/gc_config_macros.h +++ b/include/gc/gc_config_macros.h @@ -189,13 +189,13 @@ # define __GC # if !defined(_WIN32_WCE) || defined(__GNUC__) # include -# if defined(__MINGW32__) && !defined(_WIN32_WCE) +# if defined(__MINGW32__) && !defined(_WIN32_WCE) \ + || defined(__CHERI_PURE_CAPABILITY__) # include -/* We mention uintptr_t. */ -/* Perhaps this should be included in pure msft environments */ -/* as well? */ +/* We mention uintptr_t. Perhaps this should be included in pure */ +/* msft environments as well. */ # endif -# else /* _WIN32_WCE */ +# else /* Yet more kludges for WinCE. */ # include /* size_t is defined here */ # ifndef _PTRDIFF_T_DEFINED diff --git a/include/gc/gc_tiny_fl.h b/include/gc/gc_tiny_fl.h index 0bcf32e2b..35229c938 100644 --- a/include/gc/gc_tiny_fl.h +++ b/include/gc/gc_tiny_fl.h @@ -48,7 +48,11 @@ #ifdef GC_GRANULE_BYTES # define GC_GRANULE_PTRS (GC_GRANULE_BYTES / GC_SIZEOF_PTR) #else -# define GC_GRANULE_PTRS 2 /* in pointers */ +# if defined(__CHERI_PURE_CAPABILITY__) +# define GC_GRANULE_PTRS 1 +# else +# define GC_GRANULE_PTRS 2 /* in pointers */ +# endif # define GC_GRANULE_BYTES (GC_GRANULE_PTRS * GC_SIZEOF_PTR) #endif /* !GC_GRANULE_BYTES */ diff --git a/include/private/gcconfig.h b/include/private/gcconfig.h index dd5f0656d..5ae0edf27 100644 --- a/include/private/gcconfig.h +++ b/include/private/gcconfig.h @@ -2488,6 +2488,11 @@ extern char __global_base, __heap_base; # define USE_MMAP_ANON #endif +#if defined(__CHERI_PURE_CAPABILITY__) && defined(USE_MMAP) +/* TODO: currently turned off to avoid downgrading permissions on CHERI */ +# undef USE_MUNMAP +#endif + #if defined(REDIRECT_MALLOC) && defined(THREADS) \ && (defined(LINUX) || defined(NACL)) /* TODO: Unclear if NaCl really needs this. */ @@ -2674,7 +2679,11 @@ EXTERN_C_BEGIN #endif #ifndef CPP_PTRSZ -# define CPP_PTRSZ CPP_WORDSZ +# if defined(__CHERI_PURE_CAPABILITY__) +# define CPP_PTRSZ (__SIZEOF_POINTER__ * 8) +# else +# define CPP_PTRSZ CPP_WORDSZ +# endif #endif #ifndef CPPCHECK