Merge pull request #11031 from poettering/gcc-attr-cleanup

various gcc attribute clean-ups
This commit is contained in:
Lennart Poettering 2018-12-03 21:59:00 +01:00 committed by GitHub
commit 63e688cc3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 120 additions and 52 deletions

View File

@ -276,7 +276,7 @@ static const struct hashmap_type_info hashmap_type_info[_HASHMAP_TYPE_MAX] = {
};
#if VALGRIND
__attribute__((destructor)) static void cleanup_pools(void) {
_destructor_ static void cleanup_pools(void) {
_cleanup_free_ char *t = NULL;
int r;

View File

@ -8,27 +8,31 @@
#include <sys/sysmacros.h>
#include <sys/types.h>
#define _printf_(a, b) __attribute__ ((__format__(printf, a, b)))
#define _printf_(a, b) __attribute__((__format__(printf, a, b)))
#ifdef __clang__
# define _alloc_(...)
#else
# define _alloc_(...) __attribute__ ((__alloc_size__(__VA_ARGS__)))
# define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__)))
#endif
#define _sentinel_ __attribute__ ((__sentinel__))
#define _unused_ __attribute__ ((__unused__))
#define _destructor_ __attribute__ ((__destructor__))
#define _pure_ __attribute__ ((__pure__))
#define _const_ __attribute__ ((__const__))
#define _deprecated_ __attribute__ ((__deprecated__))
#define _packed_ __attribute__ ((__packed__))
#define _malloc_ __attribute__ ((__malloc__))
#define _weak_ __attribute__ ((__weak__))
#define _sentinel_ __attribute__((__sentinel__))
#define _section_(x) __attribute__((__section__(x)))
#define _used_ __attribute__((__used__))
#define _unused_ __attribute__((__unused__))
#define _destructor_ __attribute__((__destructor__))
#define _pure_ __attribute__((__pure__))
#define _const_ __attribute__((__const__))
#define _deprecated_ __attribute__((__deprecated__))
#define _packed_ __attribute__((__packed__))
#define _malloc_ __attribute__((__malloc__))
#define _weak_ __attribute__((__weak__))
#define _likely_(x) (__builtin_expect(!!(x), 1))
#define _unlikely_(x) (__builtin_expect(!!(x), 0))
#define _public_ __attribute__ ((__visibility__("default")))
#define _hidden_ __attribute__ ((__visibility__("hidden")))
#define _public_ __attribute__((__visibility__("default")))
#define _hidden_ __attribute__((__visibility__("hidden")))
#define _weakref_(x) __attribute__((__weakref__(#x)))
#define _align_(x) __attribute__((__aligned__(x)))
#define _alignas_(x) __attribute__((__aligned__(__alignof(x))))
#define _alignptr_ __attribute__((__aligned__(sizeof(void*))))
#define _cleanup_(x) __attribute__((__cleanup__(x)))
#if __GNUC__ >= 7
#define _fallthrough_ __attribute__((__fallthrough__))
@ -56,6 +60,29 @@
# endif
#endif
#if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
# ifdef __SANITIZE_ADDRESS__
# define HAS_FEATURE_ADDRESS_SANITIZER 1
# elif defined(__has_feature)
# if __has_feature(address_sanitizer)
# define HAS_FEATURE_ADDRESS_SANITIZER 1
# endif
# endif
# if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
# define HAS_FEATURE_ADDRESS_SANITIZER 0
# endif
#endif
/* Note: on GCC "no_sanitize_address" is a function attribute only, on llvm it may also be applied to global
* variables. We define a specific macro which knows this. Note that on GCC we don't need this decorator so much, since
* our primary usecase for this attribute is registration structures placed in named ELF sections which shall not be
* padded, but GCC doesn't pad those anyway if AddressSanitizer is enabled. */
#if HAS_FEATURE_ADDRESS_SANITIZER && defined(__clang__)
#define _variable_no_sanitize_address_ __attribute__((__no_sanitize_address__))
#else
#define _variable_no_sanitize_address_
#endif
/* Temporarily disable some warnings */
#define DISABLE_WARNING_FORMAT_NONLITERAL \
_Pragma("GCC diagnostic push"); \

View File

@ -1172,7 +1172,7 @@ void reset_cached_pid(void) {
* headers. __register_atfork() is mostly equivalent to pthread_atfork(), but doesn't require us to link against
* libpthread, as it is part of glibc anyway. */
extern int __register_atfork(void (*prepare) (void), void (*parent) (void), void (*child) (void), void *dso_handle);
extern void* __dso_handle __attribute__ ((__weak__));
extern void* __dso_handle _weak_;
pid_t getpid_cached(void) {
static bool installed = false;

View File

@ -22,10 +22,14 @@ typedef struct StaticDestructor {
typeof(variable) *q = p; \
func(q); \
} \
/* The actual destructor structure */ \
__attribute__ ((__section__("SYSTEMD_STATIC_DESTRUCT"))) \
__attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__))) \
__attribute__ ((__used__)) \
/* The actual destructor structure we place in a special section to find it */ \
_section_("SYSTEMD_STATIC_DESTRUCT") \
/* We pick pointer alignment, since that is apparently what gcc does for static variables */ \
_alignptr_ \
/* Make sure this is not dropped from the image because not explicitly referenced */ \
_used_ \
/* Make sure that AddressSanitizer doesn't pad this variable: we want everything in this section packed next to each other so that we can enumerate it. */ \
_variable_no_sanitize_address_ \
static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \
.data = &(variable), \
.destroy = UNIQ_T(static_destructor_wrapper, uq), \
@ -33,8 +37,8 @@ typedef struct StaticDestructor {
/* Beginning and end of our section listing the destructors. We define these as weak as we want this to work even if
* there's not a single destructor is defined in which case the section will be missing. */
extern const struct StaticDestructor __attribute__((__weak__)) __start_SYSTEMD_STATIC_DESTRUCT[];
extern const struct StaticDestructor __attribute__((__weak__)) __stop_SYSTEMD_STATIC_DESTRUCT[];
extern const struct StaticDestructor _weak_ __start_SYSTEMD_STATIC_DESTRUCT[];
extern const struct StaticDestructor _weak_ __stop_SYSTEMD_STATIC_DESTRUCT[];
/* The function to destroy everything. (Note that this must be static inline, as it's key that it remains in the same
* linking unit as the variables we want to destroy. */
@ -44,9 +48,9 @@ static inline void static_destruct(void) {
if (!__start_SYSTEMD_STATIC_DESTRUCT)
return;
d = ALIGN_TO_PTR(__start_SYSTEMD_STATIC_DESTRUCT, __BIGGEST_ALIGNMENT__);
d = ALIGN_TO_PTR(__start_SYSTEMD_STATIC_DESTRUCT, sizeof(void*));
while (d < __stop_SYSTEMD_STATIC_DESTRUCT) {
d->destroy(d->data);
d = ALIGN_TO_PTR(d + 1, __BIGGEST_ALIGNMENT__);
d = ALIGN_TO_PTR(d + 1, sizeof(void*));
}
}

View File

@ -173,7 +173,7 @@ static inline void _reset_errno_(int *saved_errno) {
}
#define PROTECT_ERRNO \
_cleanup_(_reset_errno_) __attribute__((__unused__)) int _saved_errno_ = errno
_cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
static inline int negative_errno(void) {
/* This helper should be used to shut up gcc if you know 'errno' is

View File

@ -132,13 +132,13 @@ typedef struct {
UINT16 HeaderVersion;
UINT32 PCRIndex;
UINT32 EventType;
} __attribute__ ((packed)) EFI_TCG2_EVENT_HEADER;
} __attribute__((packed)) EFI_TCG2_EVENT_HEADER;
typedef struct tdEFI_TCG2_EVENT {
UINT32 Size;
EFI_TCG2_EVENT_HEADER Header;
UINT8 Event[1];
} __attribute__ ((packed)) EFI_TCG2_EVENT;
} __attribute__((packed)) EFI_TCG2_EVENT;
typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_CAPABILITY) (IN EFI_TCG2_PROTOCOL * This,
IN OUT EFI_TCG2_BOOT_SERVICE_CAPABILITY * ProtocolCapability);

View File

@ -319,7 +319,7 @@ uint32_t jenkins_hashlittle( const void *key, size_t length, uint32_t initval)
* still catch it and complain. The masking trick does make the hash
* noticeably faster for short strings (like English words).
*/
#if !VALGRIND && !defined(__SANITIZE_ADDRESS__)
#if !VALGRIND && !HAS_FEATURE_ADDRESS_SANITIZER
switch(length)
{
@ -504,7 +504,7 @@ void jenkins_hashlittle2(
* still catch it and complain. The masking trick does make the hash
* noticeably faster for short strings (like English words).
*/
#if !VALGRIND && !defined(__SANITIZE_ADDRESS__)
#if !VALGRIND && !HAS_FEATURE_ADDRESS_SANITIZER
switch(length)
{
@ -680,7 +680,7 @@ uint32_t jenkins_hashbig( const void *key, size_t length, uint32_t initval)
* still catch it and complain. The masking trick does make the hash
* noticeably faster for short strings (like English words).
*/
#if !VALGRIND && !defined(__SANITIZE_ADDRESS__)
#if !VALGRIND && !HAS_FEATURE_ADDRESS_SANITIZER
switch(length)
{

View File

@ -54,8 +54,8 @@ BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_standard_errors[] = {
};
/* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section */
extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
extern const sd_bus_error_map __start_SYSTEMD_BUS_ERROR_MAP[];
extern const sd_bus_error_map __stop_SYSTEMD_BUS_ERROR_MAP[];
/* Additional maps registered with sd_bus_error_add_map() are in this
* NULL terminated array */
@ -89,9 +89,8 @@ static int bus_error_name_to_errno(const char *name) {
return m->code;
}
m = __start_BUS_ERROR_MAP;
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
while (m < __stop_BUS_ERROR_MAP) {
m = ALIGN_TO_PTR(__start_SYSTEMD_BUS_ERROR_MAP, sizeof(void*));
while (m < __stop_SYSTEMD_BUS_ERROR_MAP) {
/* For magic ELF error maps, the end marker might
* appear in the middle of things, since multiple maps
* might appear in the same section. Hence, let's skip
@ -99,7 +98,7 @@ static int bus_error_name_to_errno(const char *name) {
* boundary, which is the selected alignment for the
* arrays. */
if (m->code == BUS_ERROR_MAP_END_MARKER) {
m = ALIGN8_PTR(m+1);
m = ALIGN_TO_PTR(m + 1, sizeof(void*));
continue;
}
@ -108,7 +107,6 @@ static int bus_error_name_to_errno(const char *name) {
m++;
}
#endif
return EIO;
}

View File

@ -31,13 +31,14 @@ int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_lis
*/
#define BUS_ERROR_MAP_ELF_REGISTER \
__attribute__ ((__section__("BUS_ERROR_MAP"))) \
__attribute__ ((__used__)) \
__attribute__ ((__aligned__(8)))
_section_("SYSTEMD_BUS_ERROR_MAP") \
_used_ \
_alignptr_ \
_variable_no_sanitize_address_
#define BUS_ERROR_MAP_ELF_USE(errors) \
extern const sd_bus_error_map errors[]; \
__attribute__ ((__used__)) \
_used_ \
static const sd_bus_error_map * const CONCATENATE(errors ## _copy_, __COUNTER__) = errors;
/* We use something exotic as end marker, to ensure people build the

View File

@ -113,18 +113,18 @@ static void test_error(void) {
assert_se(!sd_bus_error_is_set(&error));
}
extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
extern const sd_bus_error_map __start_SYSTEMD_BUS_ERROR_MAP[];
extern const sd_bus_error_map __stop_SYSTEMD_BUS_ERROR_MAP[];
static void dump_mapping_table(void) {
const sd_bus_error_map *m;
printf("----- errno mappings ------\n");
m = __start_BUS_ERROR_MAP;
while (m < __stop_BUS_ERROR_MAP) {
m = ALIGN_TO_PTR(__start_SYSTEMD_BUS_ERROR_MAP, sizeof(void*));
while (m < __stop_SYSTEMD_BUS_ERROR_MAP) {
if (m->code == BUS_ERROR_MAP_END_MARKER) {
m = ALIGN8_PTR(m+1);
m = ALIGN_TO_PTR(m + 1, sizeof(void*));
continue;
}

View File

@ -202,7 +202,7 @@ int main(int argc, char *argv[]) {
log_info("message size = %zu, contents =\n%s", sz, h);
#if HAVE_GLIB
#ifndef __SANITIZE_ADDRESS__
#if !HAS_FEATURE_ADDRESS_SANITIZER
{
GDBusMessage *g;
char *p;

View File

@ -274,9 +274,9 @@ int json_log_internal(JsonVariant *variant, int level, int error, const char *fi
#define JSON_VARIANT_STRING_CONST(x) _JSON_VARIANT_STRING_CONST(UNIQ, (x))
#define _JSON_VARIANT_STRING_CONST(xq, x) \
#define _JSON_VARIANT_STRING_CONST(xq, x) \
({ \
__attribute__((__aligned__(2))) static const char UNIQ_T(json_string_const, xq)[] = (x); \
_align_(2) static const char UNIQ_T(json_string_const, xq)[] = (x); \
assert((((uintptr_t) UNIQ_T(json_string_const, xq)) & 1) == 0); \
(JsonVariant*) ((uintptr_t) UNIQ_T(json_string_const, xq) + 1); \
})

View File

@ -27,7 +27,7 @@ typedef void (*_sd_destroy_t)(void *userdata);
#ifndef _sd_printf_
# if __GNUC__ >= 4
# define _sd_printf_(a,b) __attribute__ ((__format__(printf, a, b)))
# define _sd_printf_(a,b) __attribute__((__format__(printf, a, b)))
# else
# define _sd_printf_(a,b)
# endif

View File

@ -177,6 +177,10 @@ tests += [
[],
[]],
[['src/test/test-static-destruct.c'],
[],
[]],
[['src/test/test-sigbus.c'],
[],
[]],

View File

@ -20,7 +20,7 @@
static uid_t test_uid = -1;
static gid_t test_gid = -1;
#ifdef __SANITIZE_ADDRESS__
#if HAS_FEATURE_ADDRESS_SANITIZER
/* Keep CAP_SYS_PTRACE when running under Address Sanitizer */
static const uint64_t test_flags = UINT64_C(1) << CAP_SYS_PTRACE;
#else

View File

@ -759,7 +759,7 @@ int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
#ifdef __SANITIZE_ADDRESS__
#if HAS_FEATURE_ADDRESS_SANITIZER
if (is_run_on_travis_ci()) {
log_notice("Running on TravisCI under ASan, skipping, see https://github.com/systemd/systemd/issues/10696");
return EXIT_TEST_SKIP;

View File

@ -19,7 +19,7 @@ int main(int argc, char *argv[]) {
test_setup_logging(LOG_INFO);
#ifdef __SANITIZE_ADDRESS__
#if HAS_FEATURE_ADDRESS_SANITIZER
return log_tests_skipped("address-sanitizer is enabled");
#endif
#if HAVE_VALGRIND_VALGRIND_H

View File

@ -0,0 +1,34 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "alloc-util.h"
#include "static-destruct.h"
#include "tests.h"
static int foo = 0;
static int bar = 0;
static int baz = 0;
static char* memory = NULL;
static void test_destroy(int *b) {
(*b)++;
}
STATIC_DESTRUCTOR_REGISTER(foo, test_destroy);
STATIC_DESTRUCTOR_REGISTER(bar, test_destroy);
STATIC_DESTRUCTOR_REGISTER(bar, test_destroy);
STATIC_DESTRUCTOR_REGISTER(baz, test_destroy);
STATIC_DESTRUCTOR_REGISTER(baz, test_destroy);
STATIC_DESTRUCTOR_REGISTER(baz, test_destroy);
STATIC_DESTRUCTOR_REGISTER(memory, freep);
int main(int argc, char *argv[]) {
test_setup_logging(LOG_INFO);
assert_se(memory = strdup("hallo"));
assert_se(foo == 0 && bar == 0 && baz == 0);
static_destruct();
assert_se(foo == 1 && bar == 2 && baz == 3);
return EXIT_SUCCESS;
}