This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: master: Build failure in malloc with GCC 7
- From: Florian Weimer <fweimer at redhat dot com>
- To: Andreas Schwab <schwab at suse dot de>
- Cc: GNU C Library <libc-alpha at sourceware dot org>, DJ Delorie <dj at redhat dot com>, Carlos O'Donell <carlos at redhat dot com>
- Date: Wed, 12 Jul 2017 14:58:11 +0200
- Subject: Re: master: Build failure in malloc with GCC 7
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=fweimer at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 28FC580C1D
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 28FC580C1D
- References: <53780a28-ff3e-0c60-f9b2-f16f9580667d@redhat.com> <mvm60ey9gyx.fsf@suse.de> <8a31493c-6778-ca10-7469-77fff10b8b42@redhat.com> <a0e2feeb-4f0e-29f5-f0e2-c44f848ac3a5@redhat.com> <mvmd19596u2.fsf@suse.de>
On 07/12/2017 02:29 PM, Andreas Schwab wrote:
> On Jul 12 2017, Florian Weimer <fweimer@redhat.com> wrote:
>
>> The attached patch adds an assert which reveals to the GCC optimizers
>> that the global_max_fast can never MAX_FAST_SIZE, so this particular
>> issue goes away. However, there is a cost in terms of code size because
>> it affects many places in malloc.
>
> Can __builtin_unreachable avoid the runtime overhead?
Interesting idea. I have never used __builtin_unreachable before, I think.
Current master:
text data bss dec hex filename
64592 2392 104 67088 10610 /root/build/malloc/malloc.o
My second patch:
text data bss dec hex filename
64424 2392 104 66920 10568 /root/build/malloc/malloc.o
Attached patch:
text data bss dec hex filename
64504 2392 104 67000 105b8 /root/build/malloc/malloc.o
Looking at the GIMPLE dumps, the code is somewhat improved over my
second patch.
Thanks,
Florian
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 54e406b..da7876a 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1658,6 +1658,9 @@ typedef struct malloc_chunk *mfastbinptr;
#define arena_is_corrupt(A) (((A)->flags & ARENA_CORRUPTION_BIT))
#define set_arena_corrupt(A) ((A)->flags |= ARENA_CORRUPTION_BIT)
+/* Maximum size of memory handled in fastbins. */
+static INTERNAL_SIZE_T global_max_fast;
+
/*
Set value of max_fast.
Use impossibly small value if 0.
@@ -1668,8 +1671,20 @@ typedef struct malloc_chunk *mfastbinptr;
#define set_max_fast(s) \
global_max_fast = (((s) == 0) \
? SMALLBIN_WIDTH : ((s + SIZE_SZ) & ~MALLOC_ALIGN_MASK))
-#define get_max_fast() global_max_fast
+static inline INTERNAL_SIZE_T
+get_max_fast (void)
+{
+ /* Tell the glibc optimizers that global_max_fast is never larger
+ than MAX_FAST_SIZE. This avoids out-of-bounds array accesses in
+ _int_malloc after constant propagation of the size parameter.
+ (The code never executes because malloc preserves the
+ global_max_fast invariant, but the optimizers may not recognize
+ this.) */
+ if (global_max_fast > MAX_FAST_SIZE)
+ __builtin_unreachable ();
+ return global_max_fast;
+}
/*
----------- Internal state representation and initialization -----------
@@ -1797,9 +1812,6 @@ static struct malloc_par mp_ =
#endif
};
-/* Maximum size of memory handled in fastbins. */
-static INTERNAL_SIZE_T global_max_fast;
-
/*
Initialize a malloc_state struct.