Zbigniew Jędrzejewski-Szmek a142c8
From e998c9d7c1a52ab02ff6e9c363c1cfe0b76cd6f4 Mon Sep 17 00:00:00 2001
Zbigniew Jędrzejewski-Szmek a142c8
From: Siddhesh Poyarekar <siddhesh@gotplt.org>
Zbigniew Jędrzejewski-Szmek a142c8
Date: Sat, 7 Jan 2023 19:30:32 -0500
Zbigniew Jędrzejewski-Szmek a142c8
Subject: [PATCH 5/5] alloc-util: Disallow inlining of expand_to_usable
Zbigniew Jędrzejewski-Szmek a142c8
Zbigniew Jędrzejewski-Szmek a142c8
Explicitly set __attribute__ ((noinline)) so that the compiler does not
Zbigniew Jędrzejewski-Szmek a142c8
attempt to inline expand_to_usable, even with LTO.
Zbigniew Jędrzejewski-Szmek a142c8
Zbigniew Jędrzejewski-Szmek a142c8
(cherry picked from commit 4f79f545b3c46c358666c9f5f2b384fe50aac4b4)
Zbigniew Jędrzejewski-Szmek a142c8
---
Zbigniew Jędrzejewski-Szmek a142c8
 src/basic/alloc-util.h              | 7 ++++---
Zbigniew Jędrzejewski-Szmek a142c8
 src/fundamental/macro-fundamental.h | 1 +
Zbigniew Jędrzejewski-Szmek a142c8
 2 files changed, 5 insertions(+), 3 deletions(-)
Zbigniew Jędrzejewski-Szmek a142c8
Zbigniew Jędrzejewski-Szmek a142c8
diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h
Zbigniew Jędrzejewski-Szmek a142c8
index eb53aae6f3..bf783b15a2 100644
Zbigniew Jędrzejewski-Szmek a142c8
--- a/src/basic/alloc-util.h
Zbigniew Jędrzejewski-Szmek a142c8
+++ b/src/basic/alloc-util.h
Zbigniew Jędrzejewski-Szmek a142c8
@@ -186,10 +186,11 @@ void* greedy_realloc0(void **p, size_t need, size_t size);
Zbigniew Jędrzejewski-Szmek a142c8
 #endif
Zbigniew Jędrzejewski-Szmek a142c8
 
Zbigniew Jędrzejewski-Szmek a142c8
 /* Dummy allocator to tell the compiler that the new size of p is newsize. The implementation returns the
Zbigniew Jędrzejewski-Szmek a142c8
- * pointer as is; the only reason for its existence is as a conduit for the _alloc_ attribute. This cannot be
Zbigniew Jędrzejewski-Szmek a142c8
- * a static inline because gcc then loses the attributes on the function.
Zbigniew Jędrzejewski-Szmek a142c8
+ * pointer as is; the only reason for its existence is as a conduit for the _alloc_ attribute.  This must not
Zbigniew Jędrzejewski-Szmek a142c8
+ * be inlined (hence a non-static function with _noinline_ because LTO otherwise tries to inline it) because
Zbigniew Jędrzejewski-Szmek a142c8
+ * gcc then loses the attributes on the function.
Zbigniew Jędrzejewski-Szmek a142c8
  * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503 */
Zbigniew Jędrzejewski-Szmek a142c8
-void *expand_to_usable(void *p, size_t newsize) _alloc_(2) _returns_nonnull_;
Zbigniew Jędrzejewski-Szmek a142c8
+void *expand_to_usable(void *p, size_t newsize) _alloc_(2) _returns_nonnull_ _noinline_;
Zbigniew Jędrzejewski-Szmek a142c8
 
Zbigniew Jędrzejewski-Szmek a142c8
 static inline size_t malloc_sizeof_safe(void **xp) {
Zbigniew Jędrzejewski-Szmek a142c8
         if (_unlikely_(!xp || !*xp))
Zbigniew Jędrzejewski-Szmek a142c8
diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h
Zbigniew Jędrzejewski-Szmek a142c8
index c11a5b15f4..e73174a593 100644
Zbigniew Jędrzejewski-Szmek a142c8
--- a/src/fundamental/macro-fundamental.h
Zbigniew Jędrzejewski-Szmek a142c8
+++ b/src/fundamental/macro-fundamental.h
Zbigniew Jędrzejewski-Szmek a142c8
@@ -20,6 +20,7 @@
Zbigniew Jędrzejewski-Szmek a142c8
 #define _hidden_ __attribute__((__visibility__("hidden")))
Zbigniew Jędrzejewski-Szmek a142c8
 #define _likely_(x) (__builtin_expect(!!(x), 1))
Zbigniew Jędrzejewski-Szmek a142c8
 #define _malloc_ __attribute__((__malloc__))
Zbigniew Jędrzejewski-Szmek a142c8
+#define _noinline_ __attribute__((noinline))
Zbigniew Jędrzejewski-Szmek a142c8
 #define _noreturn_ _Noreturn
Zbigniew Jędrzejewski-Szmek a142c8
 #define _packed_ __attribute__((__packed__))
Zbigniew Jędrzejewski-Szmek a142c8
 #define _printf_(a, b) __attribute__((__format__(printf, a, b)))
Zbigniew Jędrzejewski-Szmek a142c8
-- 
Zbigniew Jędrzejewski-Szmek a142c8
2.39.1
Zbigniew Jędrzejewski-Szmek a142c8