Bug 21660 - GCC fails to compile a formula with tgmath.h
Summary: GCC fails to compile a formula with tgmath.h
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: math (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: 2.27
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-06-23 05:13 UTC by Daren
Modified: 2017-11-15 02:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2017-06-23 00:00:00
fweimer: security-


Attachments
C file that fails to compile with GCC (241 bytes, text/x-csrc)
2017-06-23 05:13 UTC, Daren
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Daren 2017-06-23 05:13:18 UTC
Created attachment 10214 [details]
C file that fails to compile with GCC

Hello, I reported a bug on the GCC bugtracker (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81156) and it might have something to do with glibc, I am gonna copy & paste the original post here:

Hey, I ran into some problems compiling code that contained a specific formula. I am using the type generic tgmath.h header because I am switching between float and double for performance testing reasons.

If I include the tgmath.h header, the code doesn't compile, GCC ends up filling up all my RAM and doesn't succeed to compile the file. It compiles using the math.h header without a problem.

Here's the code in question (also in the attachment):
/* memory_leak.c */
#include <stdio.h>
#include <stdlib.h>
#include <tgmath.h>

#ifdef TEST_SP
typedef float test_float;
#else
typedef double test_float;
#endif

int main(int argc, char *argv[]) {
	int b = -1;
	test_float a, c, d, e, f, g, h, i;
	a = c = d = e = f = g = h = i = 1;

	a = 2*b/abs(b)*sqrt(pow(sqrt(pow(c, 2) - pow(d, 2)) - e*f*cos(g)*cos(h)/abs(b)*(i - 1), 2) + pow(d/2, 2));

	return 0;
}
/* END */

Compile with:
gcc -o memory_leak -DTEST_SP -std=c99 memory_leak.c -lm

It ends up eating all the memory there is.
Doesn't work on current GCC 7.1.1 on Arch nor on an RHEL6 distro with GCC 4.4.7.
Works with GCC 5.3 in Cygwin.
Clang and ICC compile without problems as well on Linux.
Comment 1 Florian Weimer 2017-06-23 09:07:50 UTC
See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81156#c1 for Joseph's analysis (combination explosion due to nested macros and repeated expansion).
Comment 2 cvs-commit@gcc.gnu.org 2017-08-22 17:56:37 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, master has been updated
       via  d9bef9c0a41bc3ca4036542cbf2a7d2d0cf60237 (commit)
      from  988f991b5069f09d793c887618e84a196b100416 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=d9bef9c0a41bc3ca4036542cbf2a7d2d0cf60237

commit d9bef9c0a41bc3ca4036542cbf2a7d2d0cf60237
Author: Joseph Myers <joseph@codesourcery.com>
Date:   Tue Aug 22 17:55:42 2017 +0000

    Fix tgmath.h handling of complex integers (bug 21684).
    
    The tgmath.h macros return a real type not a complex type when an
    argument is of complex integer type (a GNU extension) and there are no
    arguments of complex floating type.  It seems clear that just as real
    integers are mapped to double for tgmath.h, so complex integers should
    be mapped to _Complex double.
    
    This patch implements such a mapping.  The main complication in fixing
    this bug is that the tgmath.h macros expand their arguments a large
    number of times, resulting in exponential blowup of the size of the
    expansion when calls to tgmath.h macros are used in the arguments of
    such macros; it would be unfortunate for fixing a bug with a fairly
    obscure extension to make the macros expand their arguments even more
    times.  Thus, this patch optimizes the definitions of the relevant
    macros.  __tgmath_real_type previously expanded its argument 7 times
    and now expands it 3 times.  __tgmath_complex_type, used in place of
    __tgmath_real_type only for functions that might return either real or
    complex types, not for complex functions that always return real types
    or always return complex types, expands its argument 5 times.  So the
    sizes of the macro expansions from nested macro calls are
    correspondingly reduced (remembering that each tgmath.h macro expands
    __tgmath_real_type, or sometimes now __tgmath_complex_type, several
    times).
    
    Sometimes the real return type resulted from calling a complex
    function and converting the result to a real type; sometimes it
    resulted from calling a real function, because the logic for
    determining whether arguments were real or complex, based on sizeof,
    was confused by integer promotions applying to e.g. short int but not
    _Complex short int.  The relevant tests are converted to use a new
    macro __expr_is_real, which, by calling __builtin_classify_type rather
    than comparing the results of two calls to sizeof, also reduces the
    number of times macros expand their arguments.
    
    Although there are reductions in the number of times macros expand
    their arguments, I do not consider this to fix bug 21660, since a
    proper fix means each macro expanding its arguments only once (via
    using new compiler features designed for that purpose).
    
    Tested for x86_64.
    
    	[BZ #21684]
    	* math/tgmath.h (__floating_type): Simplify definitions.
    	(__real_integer_type): New macro.
    	(__complex_integer_type): Likewise.
    	(__expr_is_real): Likewise.
    	(__tgmath_real_type_sub): Update comment to describe handling of
    	complex types.
    	(__tgmath_complex_type_sub): New macro.
    	(__tgmath_complex_type): Likewise.
    	[__HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)]
    	(__TGMATH_CF128): Use __expr_is_real.
    	(__TGMATH_UNARY_REAL_IMAG): Use __tgmath_complex_type and
    	__expr_is_real.
    	(__TGMATH_BINARY_REAL_IMAG): Likewise.
    	(__TGMATH_UNARY_REAL_IMAG_RET_REAL): Use __expr_is_real.
    	* math/gen-tgmath-tests.py (Type.create_type): Create complex
    	integer types.

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog                |   20 +++++++
 math/gen-tgmath-tests.py |    4 +-
 math/tgmath.h            |  135 ++++++++++++++++++++++++++++++----------------
 3 files changed, 110 insertions(+), 49 deletions(-)
Comment 3 cvs-commit@gcc.gnu.org 2017-11-15 02:09:53 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, master has been updated
       via  be3a79a3cc8d8111811a52b0fe243b6f4dd03844 (commit)
      from  2e64ec9c9eac3aeb70f7cfa2392846c87c28068e (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=be3a79a3cc8d8111811a52b0fe243b6f4dd03844

commit be3a79a3cc8d8111811a52b0fe243b6f4dd03844
Author: Joseph Myers <joseph@codesourcery.com>
Date:   Wed Nov 15 02:08:56 2017 +0000

    Use __builtin_tgmath in tgmath.h with GCC 8 (bug 21660).
    
    GCC mainline now supports __builtin_tgmath to allow <tgmath.h> macro
    implementations that expand their arguments only once, so avoiding
    exponential blowup in the size of macro expansions when calls to those
    macros are nested in arguments to those macros.
    
    This patch makes glibc's tgmath.h support using __builtin_tgmath, as a
    much simpler and more efficient alternative to the existing
    implementation.  (As a side effect, the new feature would make it much
    more practical to support decimal floating point in <tgmath.h> with
    new compilers; currently, libdfp does not provide a <tgmath.h>
    implementation, and making decimal arguments cause integer arguments
    to be considered of type _Decimal64 instead of double would have been
    very problematic in the old implementation.)
    
    Tested for x86_64 (with GCC mainline).
    
    	[BZ #21660]
    	* math/tgmath.h (__HAVE_BUILTIN_TGMATH): New macro.
    	[__HAVE_BUILTIN_TGMATH] (__TG_F16_ARG): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TG_F32_ARG): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TG_F64_ARG): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TG_F128_ARG): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TG_F32X_ARG): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TG_F64X_ARG): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TG_F128X_ARG): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_FUNCS): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_RCFUNCS): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_1): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_2): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_2STD): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_3): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_1C): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_2C): Likewise.
    	(__tgml): Make conditional on [!__HAVE_BUILTIN_TGMATH].
    	(__floating_type): Likewise.
    	(__real_integer_type): Likewise.
    	(__complex_integer_type): Likewise.
    	(__expr_is_real): Likewise.
    	(__tgmath_real_type_sub): Likewise.
    	(__tgmath_real_type): Likewise.
    	(__tgmath_complex_type_sub): Likewise.
    	(__tgmath_complex_type): Likewise.
    	(__TGMATH_F128): Likewise.
    	(__TGMATH_CF128): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_UNARY_REAL_ONLY): Define using
    	new macros.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_UNARY_REAL_RET_ONLY): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_BINARY_FIRST_REAL_ONLY):
    	Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_BINARY_FIRST_REAL_STD_ONLY):
    	Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_BINARY_REAL_ONLY): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_BINARY_REAL_STD_ONLY): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_BINARY_REAL_RET_ONLY): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY):
    	Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_TERNARY_REAL_ONLY): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_TERNARY_FIRST_REAL_RET_ONLY):
    	Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_UNARY_REAL_IMAG): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_UNARY_IMAG): Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_UNARY_REAL_IMAG_RET_REAL):
    	Likewise.
    	[__HAVE_BUILTIN_TGMATH] (__TGMATH_BINARY_REAL_IMAG): Likewise.
    	(__TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME): New macro.
    	(carg): Use __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME.
    	(cimag): Likewise.
    	(creal): Likewise.

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog     |   55 +++++++++++++++
 math/tgmath.h |  204 +++++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 202 insertions(+), 57 deletions(-)
Comment 4 Joseph Myers 2017-11-15 02:10:38 UTC
Fixed for 2.27 (when used with GCC 8 or later).