From ef25f978ffb49540cc2f7166aaf2b4ee0ce45be1 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 1 Oct 2021 18:23:20 +0100 Subject: [PATCH] Added attrs to internal-exported C API header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove -fanalyzer: Too many false positives Fortune for cpprng's current commit: Middle blessing − 中吉 --- Makefile | 2 +- include/common.h | 6 ++++-- include/mem.h | 2 +- src/rng/crand.c | 15 ++++++++------- src/rng/crand.h | 4 ++-- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index be77b62..65a1329 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ LDFLAGS += STRIP=strip RELEASE_COMMON_FLAGS+= # -Werror -DEBUG_COMMON_FLAGS+= -fanalyzer +DEBUG_COMMON_FLAGS+= ifneq ($(TARGET_SPEC_FLAGS),no) RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) diff --git a/include/common.h b/include/common.h index 7c4fb64..3b8a089 100644 --- a/include/common.h +++ b/include/common.h @@ -9,8 +9,10 @@ extern "C" { #include #include -#define IFUNC_IMPL(name, ver) _impl__ ## name ## __ ## ver -#define IFUNC_RESOLVER(name) (*_ifun__ ## name (void)) +#define IFUNC_NAME(name, ver) _impl__ ## name ## __ ## ver +#define IFUNC_IMPL(name, ver) __attribute__((copy(name))) IFUNC_NAME(name, ver) +#define IFUNC_RESOLVER_A(attr, name) __attribute__((returns_nonnull)) (* __attribute__(attr) _ifun__ ## name (void)) // When the ifunc resolver wants to return a function pointer that has attributes on it, the attribute inner list (e.g. `(returns_nonnull, const, nonnull)') can be provided as the first argument +#define IFUNC_RESOLVER(name) IFUNC_RESOLVER_A((copy(name)), name) #define IFUNC_DEF(name, params) name params __attribute__((__ifunc__("_ifun__" #name))) #define _export(kind) __attribute__((visibility(#kind))) diff --git a/include/mem.h b/include/mem.h index 2fea0bb..5e68557 100644 --- a/include/mem.h +++ b/include/mem.h @@ -93,7 +93,7 @@ namespace mem return (*this = other.clone()); } inline aligned_ptr(const aligned_ptr& copy) : ptr(aligned_ptr_util::make(copy.get())){} - T* const ptr; + T* __restrict__ const ptr; }; template diff --git a/src/rng/crand.c b/src/rng/crand.c index 0f5173f..a5a46c4 100644 --- a/src/rng/crand.c +++ b/src/rng/crand.c @@ -32,26 +32,26 @@ typedef __typeof(((struct jr_state*)NULL)->st) jr_xsub_t; _Static_assert( sizeof(uint48_t) == (sizeof(uint16_t) * 3), "bad uint48 (ushort[3])"); _Static_assert( sizeof(((struct jr_state*)NULL)->st) == sizeof(uint64_t), "bad uint64 (union st)"); - __attribute__((nonnull, returns_nonnull)) +static unsigned short* IFUNC_DEF(_jr_st_resolv, (jr_xsub_t* restrict state) + __attribute__((const, nonnull, returns_nonnull))); + static unsigned short* IFUNC_IMPL(_jr_st_resolv, low) (jr_xsub_t* restrict state) { return state->xsubi; } - __attribute__((nonnull, returns_nonnull)) static unsigned short* IFUNC_IMPL(_jr_st_resolv, high) (jr_xsub_t* restrict state) { return state->xsubi+1; } -__attribute__((returns_nonnull)) +__attribute__((const)) static unsigned short* IFUNC_RESOLVER(_jr_st_resolv) (jr_xsub_t* restrict state) { struct jr_state chk = {0}; chk.st.xsubh = JR_MAX; return chk.st._xsub - ? & IFUNC_IMPL(_jr_st_resolv, high) - : & IFUNC_IMPL(_jr_st_resolv, low); + ? & IFUNC_NAME(_jr_st_resolv, high) + : & IFUNC_NAME(_jr_st_resolv, low); } -static unsigned short* IFUNC_DEF(_jr_st_resolv, (jr_xsub_t* restrict state) __attribute__((nonnull, returns_nonnull))); internal void _jr_seed(struct jr_state* restrict state, unsigned long with) { @@ -71,6 +71,7 @@ internal double _jr_procf(struct jr_state* restrict state) return state->fresult; } +__attribute__((malloc(_jr_free))) internal struct jr_state* _jr_alloc() { struct jr_state* bx = aligned_alloc(_Alignof(struct jr_state), sizeof(struct jr_state)); @@ -78,7 +79,7 @@ internal struct jr_state* _jr_alloc() return bx; } - +__attribute__((malloc(_jr_free))) internal struct jr_state* _jr_new(unsigned long with) { struct jr_state* state = _jr_alloc(); diff --git a/src/rng/crand.h b/src/rng/crand.h index 9667b34..d13d57b 100644 --- a/src/rng/crand.h +++ b/src/rng/crand.h @@ -14,10 +14,10 @@ struct jr_state; void _jr_seed(struct jr_state* restrict state, unsigned long with) _export(internal); long _jr_proc(struct jr_state* restrict state) _export(internal); double _jr_procf(struct jr_state* restrict state) _export(internal); -struct jr_state* _jr_alloc() _export(internal); void _jr_free(struct jr_state* restrict state) _export(internal); +struct jr_state* _jr_alloc() _export(internal) __attribute__((malloc(_jr_free))); -struct jr_state* _jr_new(unsigned long with) _export(internal); +struct jr_state* _jr_new(unsigned long with) _export(internal) __attribute__((malloc(_jr_free))); #ifdef __cplusplus }