Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure should probe for __builtin_add_overflow #16063

Closed
p5pRT opened this issue Jul 2, 2017 · 8 comments
Closed

Configure should probe for __builtin_add_overflow #16063

p5pRT opened this issue Jul 2, 2017 · 8 comments

Comments

@p5pRT
Copy link

p5pRT commented Jul 2, 2017

Migrated from rt.perl.org#131689 (status was 'resolved')

Searchable as RT131689$

@p5pRT
Copy link
Author

p5pRT commented Jul 2, 2017

From @mauke

Created by @mauke

I'd like to be able __builtin_add_overflow, __builtin_sub_overflow,
__builtin_mul_overflow (safe +, -, * with overflow checks) in core code.

It would be nice if Configure had checks for those (it already checks for
__builtin_choose_expr and __builtin_expect).

Perl Info

Flags:
    category=core
    severity=wishlist

Site configuration information for perl 5.26.0:

Configured by mauke at Tue May 30 23:06:36 CEST 2017.

Summary of my perl5 (revision 5 version 26 subversion 0) configuration:
   
  Platform:
    osname=linux
    osvers=4.10.11-1-arch
    archname=i686-linux
    uname='linux simplicio 4.10.11-1-arch #1 smp preempt tue apr 18 09:00:04 cest 2017 i686 gnulinux '
    config_args=''
    hint=recommended
    useposix=true
    d_sigaction=define
    useithreads=undef
    usemultiplicity=undef
    use64bitint=undef
    use64bitall=undef
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
    bincompat5005=undef
  Compiler:
    cc='cc'
    ccflags ='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'
    optimize='-O2 -march=native'
    cppflags='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include'
    ccversion=''
    gccversion='7.1.1 20170516'
    gccosandvers=''
    intsize=4
    longsize=4
    ptrsize=4
    doublesize=8
    byteorder=1234
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=12
    longdblkind=3
    ivtype='long'
    ivsize=4
    nvtype='double'
    nvsize=8
    Off_t='off_t'
    lseeksize=8
    alignbytes=4
    prototype=define
  Linker and Libraries:
    ld='cc'
    ldflags ='-fstack-protector-strong -L/usr/local/lib'
    libpth=/usr/local/lib /usr/lib/gcc/i686-pc-linux-gnu/7.1.1/include-fixed /usr/lib /lib
    libs=-lpthread -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
    perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
    libc=libc-2.25.so
    so=so
    useshrplib=false
    libperl=libperl.a
    gnulibc_version='2.25'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs
    dlext=so
    d_dlsymun=undef
    ccdlflags='-Wl,-E'
    cccdlflags='-fPIC'
    lddlflags='-shared -O2 -march=native -L/usr/local/lib -fstack-protector-strong'



@INC for perl 5.26.0:
    /home/mauke/usr/lib/perl5/site_perl/5.26.0/i686-linux
    /home/mauke/usr/lib/perl5/site_perl/5.26.0
    /home/mauke/usr/lib/perl5/5.26.0/i686-linux
    /home/mauke/usr/lib/perl5/5.26.0


Environment for perl 5.26.0:
    HOME=/home/mauke
    LANG=en_US.UTF-8
    LANGUAGE=en_US
    LC_COLLATE=C
    LC_MONETARY=de_DE.UTF-8
    LC_TIME=de_DE.UTF-8
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/home/mauke/perl5/perlbrew/bin:/home/mauke/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
    PERLBREW_BASHRC_VERSION=0.73
    PERLBREW_HOME=/home/mauke/.perlbrew
    PERLBREW_ROOT=/home/mauke/perl5/perlbrew
    PERL_BADLANG (unset)
    PERL_UNICODE=SAL
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Jul 2, 2017

From @mauke

On Sun, 02 Jul 2017 01​:14​:12 -0700, mauke- wrote​:

I'd like to be able __builtin_add_overflow, __builtin_sub_overflow,
__builtin_mul_overflow (safe +, -, * with overflow checks) in core
code.

It would be nice if Configure had checks for those (it already checks
for
__builtin_choose_expr and __builtin_expect).

I've attached patches to do that, but I'm not sure if this is the right way to do it (see bug #131688).

The first patch extends Configure.

The second patch extends config_h.SH, which contains a comment saying "THIS IS A GENERATED FILE. DO NOT HAND-EDIT. See Porting/config_h.pl", but I saw Porting/config_h.pl and it contains no useful information, so I hand-edited anyway.

@p5pRT
Copy link
Author

p5pRT commented Jul 2, 2017

From @mauke

0001-Configure-check-for-__builtin_add_overflow-and-frien.patch
From 27f6a544755754ae61bcf02dfbff843a94c6eeb0 Mon Sep 17 00:00:00 2001
From: Lukas Mai <l.mai@web.de>
Date: Sun, 2 Jul 2017 09:35:59 +0200
Subject: [PATCH 1/2] Configure: check for __builtin_add_overflow and friends

---
 Configure | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 117 insertions(+)

diff --git Configure Configure
index 3259249500..4d151d97de 100755
--- Configure
+++ Configure
@@ -398,6 +398,9 @@ d_bcmp=''
 d_bcopy=''
 d_builtin_choose_expr=''
 d_builtin_expect=''
+d_builtin_add_overflow=''
+d_builtin_sub_overflow=''
+d_builtin_mul_overflow=''
 d_bzero=''
 d_c99_variadic_macros=''
 d_casti32=''
@@ -11562,6 +11565,117 @@ set d_builtin_expect
 eval $setvar
 $rm_try
 
+: Look for GCC-style __builtin_add_overflow
+case "$d_builtin_add_overflow" in
+'')
+    echo " "
+    echo "Checking whether your compiler can handle __builtin_add_overflow ..." >&4
+    $cat >try.c <<'EOCP'
+int main(void) {
+    const unsigned int uint_max = ~0u;
+    int target_int = 0;
+    if (__builtin_add_overflow(1, 2, &target_int) || target_int != 3) {
+        return 1;
+    }
+    if (!__builtin_add_overflow((int)(uint_max >> 1), 1, &target_int)) {
+        return 1;
+    }
+    if (!__builtin_add_overflow(uint_max, -1, &target_int)) {
+        return 1;
+    }
+    return 0;
+}
+EOCP
+    set try
+    if eval $compile && $run ./try; then
+	echo "Your C compiler supports __builtin_add_overflow."
+	val="$define"
+    else
+	echo "Your C compiler doesn't seem to understand __builtin_add_overflow."
+	val="$undef"
+    fi
+    ;;
+*) val="$d_builtin_add_overflow" ;;
+esac
+
+set d_builtin_add_overflow
+eval $setvar
+$rm_try
+
+: Look for GCC-style __builtin_sub_overflow
+case "$d_builtin_sub_overflow" in
+'')
+    echo " "
+    echo "Checking whether your compiler can handle __builtin_sub_overflow ..." >&4
+    $cat >try.c <<'EOCP'
+int main(void) {
+    const unsigned int uint_max = ~0u;
+    int target_int = 0;
+    if (__builtin_sub_overflow(1, -2, &target_int) || target_int != 3) {
+        return 1;
+    }
+    if (!__builtin_sub_overflow(-(int)(uint_max >> 1), 2, &target_int)) {
+        return 1;
+    }
+    if (!__builtin_sub_overflow(uint_max, 1, &target_int)) {
+        return 1;
+    }
+    return 0;
+}
+EOCP
+    set try
+    if eval $compile && $run ./try; then
+	echo "Your C compiler supports __builtin_sub_overflow."
+	val="$define"
+    else
+	echo "Your C compiler doesn't seem to understand __builtin_sub_overflow."
+	val="$undef"
+    fi
+    ;;
+*) val="$d_builtin_sub_overflow" ;;
+esac
+
+set d_builtin_sub_overflow
+eval $setvar
+$rm_try
+
+: Look for GCC-style __builtin_mul_overflow
+case "$d_builtin_mul_overflow" in
+'')
+    echo " "
+    echo "Checking whether your compiler can handle __builtin_mul_overflow ..." >&4
+    $cat >try.c <<'EOCP'
+int main(void) {
+    const unsigned int uint_max = ~0u;
+    int target_int = 0;
+    if (__builtin_mul_overflow(2, 3, &target_int) || target_int != 6) {
+        return 1;
+    }
+    if (!__builtin_mul_overflow((int)(uint_max >> 1), 2, &target_int)) {
+        return 1;
+    }
+    if (!__builtin_mul_overflow(uint_max, 1, &target_int)) {
+        return 1;
+    }
+    return 0;
+}
+EOCP
+    set try
+    if eval $compile && $run ./try; then
+	echo "Your C compiler supports __builtin_mul_overflow."
+	val="$define"
+    else
+	echo "Your C compiler doesn't seem to understand __builtin_mul_overflow."
+	val="$undef"
+    fi
+    ;;
+*) val="$d_builtin_mul_overflow" ;;
+esac
+
+set d_builtin_mul_overflow
+eval $setvar
+$rm_try
+
 : see if bzero exists
 set bzero d_bzero
 eval $inlibc
@@ -24463,6 +24577,9 @@ d_bsdgetpgrp='$d_bsdgetpgrp'
 d_bsdsetpgrp='$d_bsdsetpgrp'
 d_builtin_choose_expr='$d_builtin_choose_expr'
 d_builtin_expect='$d_builtin_expect'
+d_builtin_add_overflow='$d_builtin_add_overflow'
+d_builtin_sub_overflow='$d_builtin_sub_overflow'
+d_builtin_mul_overflow='$d_builtin_mul_overflow'
 d_bzero='$d_bzero'
 d_c99_variadic_macros='$d_c99_variadic_macros'
 d_casti32='$d_casti32'
-- 
2.13.2

@p5pRT
Copy link
Author

p5pRT commented Jul 2, 2017

From @mauke

0002-provide-guard-macros-for-__builtin_add_overflow-and-.patch
From 0c07092cb3bd0b47e475f702566aaab2ffbd257b Mon Sep 17 00:00:00 2001
From: Lukas Mai <l.mai@web.de>
Date: Sun, 2 Jul 2017 09:51:57 +0200
Subject: [PATCH 2/2] provide guard macros for __builtin_add_overflow and
 friends

---
 config_h.SH | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git config_h.SH config_h.SH
index e9b144f02b..d35163f0e0 100755
--- config_h.SH
+++ config_h.SH
@@ -2586,6 +2586,24 @@ sed <<!GROK!THIS! >$CONFIG_H -e 's!^#undef\(.*/\)\*!/\*#define\1 \*!' -e 's!^#un
 #$d_builtin_expect HAS_BUILTIN_EXPECT	/**/
 #$d_builtin_choose_expr HAS_BUILTIN_CHOOSE_EXPR	/**/
 
+/* HAS_BUILTIN_ADD_OVERFLOW:
+ *	This symbol, if defined, indicates that the compiler supports
+ *	__builtin_add_overflow for adding integers with overflow checks.
+ */
+#$d_builtin_add_overflow HAS_BUILTIN_ADD_OVERFLOW	/**/
+
+/* HAS_BUILTIN_SUB_OVERFLOW:
+ *	This symbol, if defined, indicates that the compiler supports
+ *	__builtin_sub_overflow for subtracting integers with overflow checks.
+ */
+#$d_builtin_sub_overflow HAS_BUILTIN_SUB_OVERFLOW	/**/
+
+/* HAS_BUILTIN_MUL_OVERFLOW:
+ *	This symbol, if defined, indicates that the compiler supports
+ *	__builtin_mul_overflow for multiplying integers with overflow checks.
+ */
+#$d_builtin_mul_overflow HAS_BUILTIN_MUL_OVERFLOW	/**/
+
 /* HAS_C99_VARIADIC_MACROS:
  *	If defined, the compiler supports C99 variadic macros.
  */
-- 
2.13.2

@p5pRT
Copy link
Author

p5pRT commented Oct 28, 2017

From @jkeenan

On Sun, 02 Jul 2017 08​:19​:31 GMT, mauke- wrote​:

On Sun, 02 Jul 2017 01​:14​:12 -0700, mauke- wrote​:

I'd like to be able __builtin_add_overflow, __builtin_sub_overflow,
__builtin_mul_overflow (safe +, -, * with overflow checks) in core
code.

It would be nice if Configure had checks for those (it already checks
for
__builtin_choose_expr and __builtin_expect).

I've attached patches to do that, but I'm not sure if this is the
right way to do it (see bug #131688).

The first patch extends Configure.

The second patch extends config_h.SH, which contains a comment saying
"THIS IS A GENERATED FILE. DO NOT HAND-EDIT. See
Porting/config_h.pl", but I saw Porting/config_h.pl and it contains no
useful information, so I hand-edited anyway.

Now that metaconfig has its repository on github.com, should this RT be re-formulated as an issue at that location?

https://github.com/perl5-metaconfig/metaconfig/issues?q=is%3Aopen+is%3Aissue

Thank you very much.

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Oct 28, 2017

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Oct 29, 2017

From @Tux

On Sat, 28 Oct 2017 13​:46​:58 -0700, "James E Keenan via RT"
<perlbug-followup@​perl.org> wrote​:

On Sun, 02 Jul 2017 08​:19​:31 GMT, mauke- wrote​:

On Sun, 02 Jul 2017 01​:14​:12 -0700, mauke- wrote​:

I'd like to be able __builtin_add_overflow, __builtin_sub_overflow,
__builtin_mul_overflow (safe +, -, * with overflow checks) in core
code.

It would be nice if Configure had checks for those (it already checks
for
__builtin_choose_expr and __builtin_expect).

I've attached patches to do that, but I'm not sure if this is the
right way to do it (see bug #131688).

The first patch extends Configure.

The second patch extends config_h.SH, which contains a comment saying
"THIS IS A GENERATED FILE. DO NOT HAND-EDIT. See
Porting/config_h.pl", but I saw Porting/config_h.pl and it contains no
useful information, so I hand-edited anyway.

Now that metaconfig has its repository on github.com, should this RT
be re-formulated as an issue at that location?

As these have already been merged into Configure, this ticket should
simply be closed.

https://github.com/perl5-metaconfig/metaconfig/issues?q=is%3Aopen+is%3Aissue

--
H.Merijn Brand http​://tux.nl Perl Monger http​://amsterdam.pm.org/
using perl5.00307 .. 5.27 porting perl5 on HP-UX, AIX, and openSUSE
http​://mirrors.develooper.com/hpux/ http​://www.test-smoke.org/
http​://qa.perl.org http​://www.goldmark.org/jeff/stupid-disclaimers/

@p5pRT
Copy link
Author

p5pRT commented Oct 29, 2017

@jkeenan - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant