|
|
a35d80 |
Enhance setuid-tunables test
|
|
|
a35d80 |
|
|
|
a35d80 |
Instead of passing GLIBC_TUNABLES via the environment, pass the
|
|
|
a35d80 |
environment variable from parent to child. This allows us to test
|
|
|
a35d80 |
multiple variables to ensure better coverage.
|
|
|
a35d80 |
|
|
|
a35d80 |
The test list currently only includes the case that's already being
|
|
|
a35d80 |
tested. More tests will be added later.
|
|
|
a35d80 |
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
|
|
|
a35d80 |
|
|
|
a35d80 |
(cherry picked from commit 061fe3f8add46a89b7453e87eabb9c4695005ced)
|
|
|
a35d80 |
|
|
|
a35d80 |
Also add intprops.h from 2.29 from commit 8e6fd2bdb21efe2cc1ae7571ff8fb2599db6a05a
|
|
|
a35d80 |
|
|
|
a35d80 |
diff --git a/elf/Makefile b/elf/Makefile
|
|
|
a35d80 |
index fc9c685b9d23bb6c..2093cefa7e73349e 100644
|
|
|
a35d80 |
--- a/elf/Makefile
|
|
|
a35d80 |
+++ b/elf/Makefile
|
|
|
a35d80 |
@@ -1597,8 +1597,6 @@ $(objpfx)tst-nodelete-dlclose.out: $(objpfx)tst-nodelete-dlclose-dso.so \
|
|
|
a35d80 |
|
|
|
a35d80 |
tst-env-setuid-ENV = MALLOC_CHECK_=2 MALLOC_MMAP_THRESHOLD_=4096 \
|
|
|
a35d80 |
LD_HWCAP_MASK=0x1
|
|
|
a35d80 |
-tst-env-setuid-tunables-ENV = \
|
|
|
a35d80 |
- GLIBC_TUNABLES=glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096
|
|
|
a35d80 |
|
|
|
a35d80 |
$(objpfx)tst-debug1: $(libdl)
|
|
|
a35d80 |
$(objpfx)tst-debug1.out: $(objpfx)tst-debug1mod1.so
|
|
|
a35d80 |
diff --git a/elf/tst-env-setuid-tunables.c b/elf/tst-env-setuid-tunables.c
|
|
|
a35d80 |
index d7c4f0d5742cd526..a48281b175af6920 100644
|
|
|
a35d80 |
--- a/elf/tst-env-setuid-tunables.c
|
|
|
a35d80 |
+++ b/elf/tst-env-setuid-tunables.c
|
|
|
a35d80 |
@@ -25,35 +25,50 @@
|
|
|
a35d80 |
#include "config.h"
|
|
|
a35d80 |
#undef _LIBC
|
|
|
a35d80 |
|
|
|
a35d80 |
-#define test_parent test_parent_tunables
|
|
|
a35d80 |
-#define test_child test_child_tunables
|
|
|
a35d80 |
-
|
|
|
a35d80 |
-static int test_child_tunables (void);
|
|
|
a35d80 |
-static int test_parent_tunables (void);
|
|
|
a35d80 |
-
|
|
|
a35d80 |
-#include "tst-env-setuid.c"
|
|
|
a35d80 |
+#include <errno.h>
|
|
|
a35d80 |
+#include <fcntl.h>
|
|
|
a35d80 |
+#include <stdlib.h>
|
|
|
a35d80 |
+#include <stdint.h>
|
|
|
a35d80 |
+#include <stdio.h>
|
|
|
a35d80 |
+#include <string.h>
|
|
|
a35d80 |
+#include <sys/stat.h>
|
|
|
a35d80 |
+#include <sys/wait.h>
|
|
|
a35d80 |
+#include <unistd.h>
|
|
|
a35d80 |
+#include <intprops.h>
|
|
|
a35d80 |
+#include <array_length.h>
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+#include <support/check.h>
|
|
|
a35d80 |
+#include <support/support.h>
|
|
|
a35d80 |
+#include <support/test-driver.h>
|
|
|
a35d80 |
+#include <support/capture_subprocess.h>
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+const char *teststrings[] =
|
|
|
a35d80 |
+{
|
|
|
a35d80 |
+ "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
|
|
|
a35d80 |
+};
|
|
|
a35d80 |
|
|
|
a35d80 |
-#define CHILD_VALSTRING_VALUE "glibc.malloc.mmap_threshold=4096"
|
|
|
a35d80 |
-#define PARENT_VALSTRING_VALUE \
|
|
|
a35d80 |
- "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096"
|
|
|
a35d80 |
+const char *resultstrings[] =
|
|
|
a35d80 |
+{
|
|
|
a35d80 |
+ "glibc.malloc.mmap_threshold=4096",
|
|
|
a35d80 |
+};
|
|
|
a35d80 |
|
|
|
a35d80 |
static int
|
|
|
a35d80 |
-test_child_tunables (void)
|
|
|
a35d80 |
+test_child (int off)
|
|
|
a35d80 |
{
|
|
|
a35d80 |
const char *val = getenv ("GLIBC_TUNABLES");
|
|
|
a35d80 |
|
|
|
a35d80 |
#if HAVE_TUNABLES
|
|
|
a35d80 |
- if (val != NULL && strcmp (val, CHILD_VALSTRING_VALUE) == 0)
|
|
|
a35d80 |
+ if (val != NULL && strcmp (val, resultstrings[off]) == 0)
|
|
|
a35d80 |
return 0;
|
|
|
a35d80 |
|
|
|
a35d80 |
if (val != NULL)
|
|
|
a35d80 |
- printf ("Unexpected GLIBC_TUNABLES VALUE %s\n", val);
|
|
|
a35d80 |
+ printf ("[%d] Unexpected GLIBC_TUNABLES VALUE %s\n", off, val);
|
|
|
a35d80 |
|
|
|
a35d80 |
return 1;
|
|
|
a35d80 |
#else
|
|
|
a35d80 |
if (val != NULL)
|
|
|
a35d80 |
{
|
|
|
a35d80 |
- printf ("GLIBC_TUNABLES not cleared\n");
|
|
|
a35d80 |
+ printf ("[%d] GLIBC_TUNABLES not cleared\n", off);
|
|
|
a35d80 |
return 1;
|
|
|
a35d80 |
}
|
|
|
a35d80 |
return 0;
|
|
|
a35d80 |
@@ -61,15 +76,48 @@ test_child_tunables (void)
|
|
|
a35d80 |
}
|
|
|
a35d80 |
|
|
|
a35d80 |
static int
|
|
|
a35d80 |
-test_parent_tunables (void)
|
|
|
a35d80 |
+do_test (int argc, char **argv)
|
|
|
a35d80 |
{
|
|
|
a35d80 |
- const char *val = getenv ("GLIBC_TUNABLES");
|
|
|
a35d80 |
+ /* Setgid child process. */
|
|
|
a35d80 |
+ if (argc == 2)
|
|
|
a35d80 |
+ {
|
|
|
a35d80 |
+ if (getgid () == getegid ())
|
|
|
a35d80 |
+ /* This can happen if the file system is mounted nosuid. */
|
|
|
a35d80 |
+ FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n",
|
|
|
a35d80 |
+ (intmax_t) getgid ());
|
|
|
a35d80 |
|
|
|
a35d80 |
- if (val != NULL && strcmp (val, PARENT_VALSTRING_VALUE) == 0)
|
|
|
a35d80 |
- return 0;
|
|
|
a35d80 |
+ int ret = test_child (atoi (argv[1]));
|
|
|
a35d80 |
|
|
|
a35d80 |
- if (val != NULL)
|
|
|
a35d80 |
- printf ("Unexpected GLIBC_TUNABLES VALUE %s\n", val);
|
|
|
a35d80 |
+ if (ret != 0)
|
|
|
a35d80 |
+ exit (1);
|
|
|
a35d80 |
|
|
|
a35d80 |
- return 1;
|
|
|
a35d80 |
+ exit (EXIT_SUCCESS);
|
|
|
a35d80 |
+ }
|
|
|
a35d80 |
+ else
|
|
|
a35d80 |
+ {
|
|
|
a35d80 |
+ int ret = 0;
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ /* Spawn tests. */
|
|
|
a35d80 |
+ for (int i = 0; i < array_length (teststrings); i++)
|
|
|
a35d80 |
+ {
|
|
|
a35d80 |
+ char buf[INT_BUFSIZE_BOUND (int)];
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ printf ("Spawned test for %s (%d)\n", teststrings[i], i);
|
|
|
a35d80 |
+ snprintf (buf, sizeof (buf), "%d\n", i);
|
|
|
a35d80 |
+ if (setenv ("GLIBC_TUNABLES", teststrings[i], 1) != 0)
|
|
|
a35d80 |
+ exit (1);
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ int status = support_capture_subprogram_self_sgid (buf);
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ /* Bail out early if unsupported. */
|
|
|
a35d80 |
+ if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
|
|
|
a35d80 |
+ return EXIT_UNSUPPORTED;
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ ret |= status;
|
|
|
a35d80 |
+ }
|
|
|
a35d80 |
+ return ret;
|
|
|
a35d80 |
+ }
|
|
|
a35d80 |
}
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+#define TEST_FUNCTION_ARGV do_test
|
|
|
a35d80 |
+#include <support/test-driver.c>
|
|
|
a35d80 |
diff --git a/include/intprops.h b/include/intprops.h
|
|
|
a35d80 |
new file mode 100644
|
|
|
a35d80 |
index 0000000000000000..9702aec4c6e3c80a
|
|
|
a35d80 |
--- /dev/null
|
|
|
a35d80 |
+++ b/include/intprops.h
|
|
|
a35d80 |
@@ -0,0 +1,455 @@
|
|
|
a35d80 |
+/* intprops.h -- properties of integer types
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ Copyright (C) 2001-2018 Free Software Foundation, Inc.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ This program is free software: you can redistribute it and/or modify it
|
|
|
a35d80 |
+ under the terms of the GNU Lesser General Public License as published
|
|
|
a35d80 |
+ by the Free Software Foundation; either version 2.1 of the License, or
|
|
|
a35d80 |
+ (at your option) any later version.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ This program is distributed in the hope that it will be useful,
|
|
|
a35d80 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
a35d80 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
a35d80 |
+ GNU Lesser General Public License for more details.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ You should have received a copy of the GNU Lesser General Public License
|
|
|
a35d80 |
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Written by Paul Eggert. */
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+#ifndef _GL_INTPROPS_H
|
|
|
a35d80 |
+#define _GL_INTPROPS_H
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+#include <limits.h>
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return a value with the common real type of E and V and the value of V.
|
|
|
a35d80 |
+ Do not evaluate E. */
|
|
|
a35d80 |
+#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
|
|
|
a35d80 |
+ <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
|
|
|
a35d80 |
+#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* The extra casts in the following macros work around compiler bugs,
|
|
|
a35d80 |
+ e.g., in Cray C 5.0.3.0. */
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* True if the arithmetic type T is an integer type. bool counts as
|
|
|
a35d80 |
+ an integer. */
|
|
|
a35d80 |
+#define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* True if the real type T is signed. */
|
|
|
a35d80 |
+#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return 1 if the real expression E, after promotion, has a
|
|
|
a35d80 |
+ signed or floating type. Do not evaluate E. */
|
|
|
a35d80 |
+#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Minimum and maximum values for integer types and expressions. */
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* The width in bits of the integer type or expression T.
|
|
|
a35d80 |
+ Do not evaluate T.
|
|
|
a35d80 |
+ Padding bits are not supported; this is checked at compile-time below. */
|
|
|
a35d80 |
+#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* The maximum and minimum values for the integer type T. */
|
|
|
a35d80 |
+#define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
|
|
|
a35d80 |
+#define TYPE_MAXIMUM(t) \
|
|
|
a35d80 |
+ ((t) (! TYPE_SIGNED (t) \
|
|
|
a35d80 |
+ ? (t) -1 \
|
|
|
a35d80 |
+ : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* The maximum and minimum values for the type of the expression E,
|
|
|
a35d80 |
+ after integer promotion. E is not evaluated. */
|
|
|
a35d80 |
+#define _GL_INT_MINIMUM(e) \
|
|
|
a35d80 |
+ (EXPR_SIGNED (e) \
|
|
|
a35d80 |
+ ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
|
|
|
a35d80 |
+ : _GL_INT_CONVERT (e, 0))
|
|
|
a35d80 |
+#define _GL_INT_MAXIMUM(e) \
|
|
|
a35d80 |
+ (EXPR_SIGNED (e) \
|
|
|
a35d80 |
+ ? _GL_SIGNED_INT_MAXIMUM (e) \
|
|
|
a35d80 |
+ : _GL_INT_NEGATE_CONVERT (e, 1))
|
|
|
a35d80 |
+#define _GL_SIGNED_INT_MAXIMUM(e) \
|
|
|
a35d80 |
+ (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Work around OpenVMS incompatibility with C99. */
|
|
|
a35d80 |
+#if !defined LLONG_MAX && defined __INT64_MAX
|
|
|
a35d80 |
+# define LLONG_MAX __INT64_MAX
|
|
|
a35d80 |
+# define LLONG_MIN __INT64_MIN
|
|
|
a35d80 |
+#endif
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* This include file assumes that signed types are two's complement without
|
|
|
a35d80 |
+ padding bits; the above macros have undefined behavior otherwise.
|
|
|
a35d80 |
+ If this is a problem for you, please let us know how to fix it for your host.
|
|
|
a35d80 |
+ This assumption is tested by the intprops-tests module. */
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Does the __typeof__ keyword work? This could be done by
|
|
|
a35d80 |
+ 'configure', but for now it's easier to do it by hand. */
|
|
|
a35d80 |
+#if (2 <= __GNUC__ \
|
|
|
a35d80 |
+ || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
|
|
|
a35d80 |
+ || (0x5110 <= __SUNPRO_C && !__STDC__))
|
|
|
a35d80 |
+# define _GL_HAVE___TYPEOF__ 1
|
|
|
a35d80 |
+#else
|
|
|
a35d80 |
+# define _GL_HAVE___TYPEOF__ 0
|
|
|
a35d80 |
+#endif
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return 1 if the integer type or expression T might be signed. Return 0
|
|
|
a35d80 |
+ if it is definitely unsigned. This macro does not evaluate its argument,
|
|
|
a35d80 |
+ and expands to an integer constant expression. */
|
|
|
a35d80 |
+#if _GL_HAVE___TYPEOF__
|
|
|
a35d80 |
+# define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
|
|
|
a35d80 |
+#else
|
|
|
a35d80 |
+# define _GL_SIGNED_TYPE_OR_EXPR(t) 1
|
|
|
a35d80 |
+#endif
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Bound on length of the string representing an unsigned integer
|
|
|
a35d80 |
+ value representable in B bits. log10 (2.0) < 146/485. The
|
|
|
a35d80 |
+ smallest value of B where this bound is not tight is 2621. */
|
|
|
a35d80 |
+#define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Bound on length of the string representing an integer type or expression T.
|
|
|
a35d80 |
+ Subtract 1 for the sign bit if T is signed, and then add 1 more for
|
|
|
a35d80 |
+ a minus sign if needed.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is
|
|
|
a35d80 |
+ signed, this macro may overestimate the true bound by one byte when
|
|
|
a35d80 |
+ applied to unsigned types of size 2, 4, 16, ... bytes. */
|
|
|
a35d80 |
+#define INT_STRLEN_BOUND(t) \
|
|
|
a35d80 |
+ (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
|
|
|
a35d80 |
+ + _GL_SIGNED_TYPE_OR_EXPR (t))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Bound on buffer size needed to represent an integer type or expression T,
|
|
|
a35d80 |
+ including the terminating null. */
|
|
|
a35d80 |
+#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Range overflow checks.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
|
|
|
a35d80 |
+ operators might not yield numerically correct answers due to
|
|
|
a35d80 |
+ arithmetic overflow. They do not rely on undefined or
|
|
|
a35d80 |
+ implementation-defined behavior. Their implementations are simple
|
|
|
a35d80 |
+ and straightforward, but they are a bit harder to use than the
|
|
|
a35d80 |
+ INT_<op>_OVERFLOW macros described below.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ Example usage:
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ long int i = ...;
|
|
|
a35d80 |
+ long int j = ...;
|
|
|
a35d80 |
+ if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
|
|
|
a35d80 |
+ printf ("multiply would overflow");
|
|
|
a35d80 |
+ else
|
|
|
a35d80 |
+ printf ("product is %ld", i * j);
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ Restrictions on *_RANGE_OVERFLOW macros:
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ These macros do not check for all possible numerical problems or
|
|
|
a35d80 |
+ undefined or unspecified behavior: they do not check for division
|
|
|
a35d80 |
+ by zero, for bad shift counts, or for shifting negative numbers.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ These macros may evaluate their arguments zero or multiple times,
|
|
|
a35d80 |
+ so the arguments should not have side effects. The arithmetic
|
|
|
a35d80 |
+ arguments (including the MIN and MAX arguments) must be of the same
|
|
|
a35d80 |
+ integer type after the usual arithmetic conversions, and the type
|
|
|
a35d80 |
+ must have minimum value MIN and maximum MAX. Unsigned types should
|
|
|
a35d80 |
+ use a zero MIN of the proper type.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ These macros are tuned for constant MIN and MAX. For commutative
|
|
|
a35d80 |
+ operations such as A + B, they are also tuned for constant B. */
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
|
|
|
a35d80 |
+ See above for restrictions. */
|
|
|
a35d80 |
+#define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ ((b) < 0 \
|
|
|
a35d80 |
+ ? (a) < (min) - (b) \
|
|
|
a35d80 |
+ : (max) - (b) < (a))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
|
|
|
a35d80 |
+ See above for restrictions. */
|
|
|
a35d80 |
+#define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ ((b) < 0 \
|
|
|
a35d80 |
+ ? (max) + (b) < (a) \
|
|
|
a35d80 |
+ : (a) < (min) + (b))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
|
|
|
a35d80 |
+ See above for restrictions. */
|
|
|
a35d80 |
+#define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
|
|
|
a35d80 |
+ ((min) < 0 \
|
|
|
a35d80 |
+ ? (a) < - (max) \
|
|
|
a35d80 |
+ : 0 < (a))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
|
|
|
a35d80 |
+ See above for restrictions. Avoid && and || as they tickle
|
|
|
a35d80 |
+ bugs in Sun C 5.11 2010/08/13 and other compilers; see
|
|
|
a35d80 |
+ <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
|
|
|
a35d80 |
+#define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ ((b) < 0 \
|
|
|
a35d80 |
+ ? ((a) < 0 \
|
|
|
a35d80 |
+ ? (a) < (max) / (b) \
|
|
|
a35d80 |
+ : (b) == -1 \
|
|
|
a35d80 |
+ ? 0 \
|
|
|
a35d80 |
+ : (min) / (b) < (a)) \
|
|
|
a35d80 |
+ : (b) == 0 \
|
|
|
a35d80 |
+ ? 0 \
|
|
|
a35d80 |
+ : ((a) < 0 \
|
|
|
a35d80 |
+ ? (a) < (min) / (b) \
|
|
|
a35d80 |
+ : (max) / (b) < (a)))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
|
|
|
a35d80 |
+ See above for restrictions. Do not check for division by zero. */
|
|
|
a35d80 |
+#define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ ((min) < 0 && (b) == -1 && (a) < - (max))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
|
|
|
a35d80 |
+ See above for restrictions. Do not check for division by zero.
|
|
|
a35d80 |
+ Mathematically, % should never overflow, but on x86-like hosts
|
|
|
a35d80 |
+ INT_MIN % -1 traps, and the C standard permits this, so treat this
|
|
|
a35d80 |
+ as an overflow too. */
|
|
|
a35d80 |
+#define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
|
|
|
a35d80 |
+ See above for restrictions. Here, MIN and MAX are for A only, and B need
|
|
|
a35d80 |
+ not be of the same type as the other arguments. The C standard says that
|
|
|
a35d80 |
+ behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
|
|
|
a35d80 |
+ A is negative then A << B has undefined behavior and A >> B has
|
|
|
a35d80 |
+ implementation-defined behavior, but do not check these other
|
|
|
a35d80 |
+ restrictions. */
|
|
|
a35d80 |
+#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ ((a) < 0 \
|
|
|
a35d80 |
+ ? (a) < (min) >> (b) \
|
|
|
a35d80 |
+ : (max) >> (b) < (a))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
|
|
|
a35d80 |
+#if 5 <= __GNUC__ && !defined __ICC
|
|
|
a35d80 |
+# define _GL_HAS_BUILTIN_OVERFLOW 1
|
|
|
a35d80 |
+#else
|
|
|
a35d80 |
+# define _GL_HAS_BUILTIN_OVERFLOW 0
|
|
|
a35d80 |
+#endif
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* True if __builtin_add_overflow_p (A, B, C) works. */
|
|
|
a35d80 |
+#define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* The _GL*_OVERFLOW macros have the same restrictions as the
|
|
|
a35d80 |
+ *_RANGE_OVERFLOW macros, except that they do not assume that operands
|
|
|
a35d80 |
+ (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
|
|
|
a35d80 |
+ that the result (e.g., A + B) has that type. */
|
|
|
a35d80 |
+#if _GL_HAS_BUILTIN_OVERFLOW_P
|
|
|
a35d80 |
+# define _GL_ADD_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
|
|
|
a35d80 |
+# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
|
|
|
a35d80 |
+# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
|
|
|
a35d80 |
+#else
|
|
|
a35d80 |
+# define _GL_ADD_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
|
|
|
a35d80 |
+ : (a) < 0 ? (b) <= (a) + (b) \
|
|
|
a35d80 |
+ : (b) < 0 ? (a) <= (a) + (b) \
|
|
|
a35d80 |
+ : (a) + (b) < (b))
|
|
|
a35d80 |
+# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
|
|
|
a35d80 |
+ : (a) < 0 ? 1 \
|
|
|
a35d80 |
+ : (b) < 0 ? (a) - (b) <= (a) \
|
|
|
a35d80 |
+ : (a) < (b))
|
|
|
a35d80 |
+# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
|
|
|
a35d80 |
+ || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
|
|
|
a35d80 |
+#endif
|
|
|
a35d80 |
+#define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
|
|
|
a35d80 |
+ : (a) < 0 ? (b) <= (a) + (b) - 1 \
|
|
|
a35d80 |
+ : (b) < 0 && (a) + (b) <= (a))
|
|
|
a35d80 |
+#define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
|
|
|
a35d80 |
+ ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
|
|
|
a35d80 |
+ : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
|
|
|
a35d80 |
+ : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return a nonzero value if A is a mathematical multiple of B, where
|
|
|
a35d80 |
+ A is unsigned, B is negative, and MAX is the maximum value of A's
|
|
|
a35d80 |
+ type. A's type must be the same as (A % B)'s type. Normally (A %
|
|
|
a35d80 |
+ -B == 0) suffices, but things get tricky if -B would overflow. */
|
|
|
a35d80 |
+#define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
|
|
|
a35d80 |
+ (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
|
|
|
a35d80 |
+ ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
|
|
|
a35d80 |
+ ? (a) \
|
|
|
a35d80 |
+ : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
|
|
|
a35d80 |
+ : (a) % - (b)) \
|
|
|
a35d80 |
+ == 0)
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Check for integer overflow, and report low order bits of answer.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
|
|
|
a35d80 |
+ might not yield numerically correct answers due to arithmetic overflow.
|
|
|
a35d80 |
+ The INT_<op>_WRAPV macros also store the low-order bits of the answer.
|
|
|
a35d80 |
+ These macros work correctly on all known practical hosts, and do not rely
|
|
|
a35d80 |
+ on undefined behavior due to signed arithmetic overflow.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ Example usage, assuming A and B are long int:
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ if (INT_MULTIPLY_OVERFLOW (a, b))
|
|
|
a35d80 |
+ printf ("result would overflow\n");
|
|
|
a35d80 |
+ else
|
|
|
a35d80 |
+ printf ("result is %ld (no overflow)\n", a * b);
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ Example usage with WRAPV flavor:
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ long int result;
|
|
|
a35d80 |
+ bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
|
|
|
a35d80 |
+ printf ("result is %ld (%s)\n", result,
|
|
|
a35d80 |
+ overflow ? "after overflow" : "no overflow");
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ Restrictions on these macros:
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ These macros do not check for all possible numerical problems or
|
|
|
a35d80 |
+ undefined or unspecified behavior: they do not check for division
|
|
|
a35d80 |
+ by zero, for bad shift counts, or for shifting negative numbers.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ These macros may evaluate their arguments zero or multiple times, so the
|
|
|
a35d80 |
+ arguments should not have side effects.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ The WRAPV macros are not constant expressions. They support only
|
|
|
a35d80 |
+ +, binary -, and *. The result type must be signed.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ These macros are tuned for their last argument being a constant.
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
|
|
|
a35d80 |
+ A % B, and A << B would overflow, respectively. */
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+#define INT_ADD_OVERFLOW(a, b) \
|
|
|
a35d80 |
+ _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
|
|
|
a35d80 |
+#define INT_SUBTRACT_OVERFLOW(a, b) \
|
|
|
a35d80 |
+ _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
|
|
|
a35d80 |
+#if _GL_HAS_BUILTIN_OVERFLOW_P
|
|
|
a35d80 |
+# define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
|
|
|
a35d80 |
+#else
|
|
|
a35d80 |
+# define INT_NEGATE_OVERFLOW(a) \
|
|
|
a35d80 |
+ INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
|
|
|
a35d80 |
+#endif
|
|
|
a35d80 |
+#define INT_MULTIPLY_OVERFLOW(a, b) \
|
|
|
a35d80 |
+ _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
|
|
|
a35d80 |
+#define INT_DIVIDE_OVERFLOW(a, b) \
|
|
|
a35d80 |
+ _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
|
|
|
a35d80 |
+#define INT_REMAINDER_OVERFLOW(a, b) \
|
|
|
a35d80 |
+ _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
|
|
|
a35d80 |
+#define INT_LEFT_SHIFT_OVERFLOW(a, b) \
|
|
|
a35d80 |
+ INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
|
|
|
a35d80 |
+ _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return 1 if the expression A <op> B would overflow,
|
|
|
a35d80 |
+ where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
|
|
|
a35d80 |
+ assuming MIN and MAX are the minimum and maximum for the result type.
|
|
|
a35d80 |
+ Arguments should be free of side effects. */
|
|
|
a35d80 |
+#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
|
|
|
a35d80 |
+ op_result_overflow (a, b, \
|
|
|
a35d80 |
+ _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
|
|
|
a35d80 |
+ _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
|
|
|
a35d80 |
+ Return 1 if the result overflows. See above for restrictions. */
|
|
|
a35d80 |
+#define INT_ADD_WRAPV(a, b, r) \
|
|
|
a35d80 |
+ _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, INT_ADD_OVERFLOW)
|
|
|
a35d80 |
+#define INT_SUBTRACT_WRAPV(a, b, r) \
|
|
|
a35d80 |
+ _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW)
|
|
|
a35d80 |
+#define INT_MULTIPLY_WRAPV(a, b, r) \
|
|
|
a35d80 |
+ _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
|
|
|
a35d80 |
+ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
|
|
|
a35d80 |
+ https://llvm.org/bugs/show_bug.cgi?id=25390
|
|
|
a35d80 |
+ For now, assume all versions of GCC-like compilers generate bogus
|
|
|
a35d80 |
+ warnings for _Generic. This matters only for older compilers that
|
|
|
a35d80 |
+ lack __builtin_add_overflow. */
|
|
|
a35d80 |
+#if __GNUC__
|
|
|
a35d80 |
+# define _GL__GENERIC_BOGUS 1
|
|
|
a35d80 |
+#else
|
|
|
a35d80 |
+# define _GL__GENERIC_BOGUS 0
|
|
|
a35d80 |
+#endif
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Store the low-order bits of A <op> B into *R, where OP specifies
|
|
|
a35d80 |
+ the operation. BUILTIN is the builtin operation, and OVERFLOW the
|
|
|
a35d80 |
+ overflow predicate. Return 1 if the result overflows. See above
|
|
|
a35d80 |
+ for restrictions. */
|
|
|
a35d80 |
+#if _GL_HAS_BUILTIN_OVERFLOW
|
|
|
a35d80 |
+# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
|
|
|
a35d80 |
+#elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
|
|
|
a35d80 |
+# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
|
|
|
a35d80 |
+ (_Generic \
|
|
|
a35d80 |
+ (*(r), \
|
|
|
a35d80 |
+ signed char: \
|
|
|
a35d80 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
a35d80 |
+ signed char, SCHAR_MIN, SCHAR_MAX), \
|
|
|
a35d80 |
+ short int: \
|
|
|
a35d80 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
a35d80 |
+ short int, SHRT_MIN, SHRT_MAX), \
|
|
|
a35d80 |
+ int: \
|
|
|
a35d80 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
a35d80 |
+ int, INT_MIN, INT_MAX), \
|
|
|
a35d80 |
+ long int: \
|
|
|
a35d80 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
|
|
|
a35d80 |
+ long int, LONG_MIN, LONG_MAX), \
|
|
|
a35d80 |
+ long long int: \
|
|
|
a35d80 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
|
|
|
a35d80 |
+ long long int, LLONG_MIN, LLONG_MAX)))
|
|
|
a35d80 |
+#else
|
|
|
a35d80 |
+# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
|
|
|
a35d80 |
+ (sizeof *(r) == sizeof (signed char) \
|
|
|
a35d80 |
+ ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
a35d80 |
+ signed char, SCHAR_MIN, SCHAR_MAX) \
|
|
|
a35d80 |
+ : sizeof *(r) == sizeof (short int) \
|
|
|
a35d80 |
+ ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
a35d80 |
+ short int, SHRT_MIN, SHRT_MAX) \
|
|
|
a35d80 |
+ : sizeof *(r) == sizeof (int) \
|
|
|
a35d80 |
+ ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
a35d80 |
+ int, INT_MIN, INT_MAX) \
|
|
|
a35d80 |
+ : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
|
|
|
a35d80 |
+# ifdef LLONG_MAX
|
|
|
a35d80 |
+# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
|
|
|
a35d80 |
+ (sizeof *(r) == sizeof (long int) \
|
|
|
a35d80 |
+ ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
|
|
|
a35d80 |
+ long int, LONG_MIN, LONG_MAX) \
|
|
|
a35d80 |
+ : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
|
|
|
a35d80 |
+ long long int, LLONG_MIN, LLONG_MAX))
|
|
|
a35d80 |
+# else
|
|
|
a35d80 |
+# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
|
|
|
a35d80 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
|
|
|
a35d80 |
+ long int, LONG_MIN, LONG_MAX)
|
|
|
a35d80 |
+# endif
|
|
|
a35d80 |
+#endif
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Store the low-order bits of A <op> B into *R, where the operation
|
|
|
a35d80 |
+ is given by OP. Use the unsigned type UT for calculation to avoid
|
|
|
a35d80 |
+ overflow problems. *R's type is T, with extrema TMIN and TMAX.
|
|
|
a35d80 |
+ T must be a signed integer type. Return 1 if the result overflows. */
|
|
|
a35d80 |
+#define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
|
|
|
a35d80 |
+ (sizeof ((a) op (b)) < sizeof (t) \
|
|
|
a35d80 |
+ ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
|
|
|
a35d80 |
+ : _GL_INT_OP_CALC1 (a, b, r, op, overflow, ut, t, tmin, tmax))
|
|
|
a35d80 |
+#define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \
|
|
|
a35d80 |
+ ((overflow (a, b) \
|
|
|
a35d80 |
+ || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
|
|
|
a35d80 |
+ || (tmax) < ((a) op (b))) \
|
|
|
a35d80 |
+ ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
|
|
|
a35d80 |
+ : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+/* Return the low-order bits of A <op> B, where the operation is given
|
|
|
a35d80 |
+ by OP. Use the unsigned type UT for calculation to avoid undefined
|
|
|
a35d80 |
+ behavior on signed integer overflow, and convert the result to type T.
|
|
|
a35d80 |
+ UT is at least as wide as T and is no narrower than unsigned int,
|
|
|
a35d80 |
+ T is two's complement, and there is no padding or trap representations.
|
|
|
a35d80 |
+ Assume that converting UT to T yields the low-order bits, as is
|
|
|
a35d80 |
+ done in all known two's-complement C compilers. E.g., see:
|
|
|
a35d80 |
+ https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ According to the C standard, converting UT to T yields an
|
|
|
a35d80 |
+ implementation-defined result or signal for values outside T's
|
|
|
a35d80 |
+ range. However, code that works around this theoretical problem
|
|
|
a35d80 |
+ runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
|
|
|
a35d80 |
+ https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
|
|
|
a35d80 |
+ As the compiler bug is real, don't try to work around the
|
|
|
a35d80 |
+ theoretical problem. */
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
|
|
|
a35d80 |
+ ((t) ((ut) (a) op (ut) (b)))
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+#endif /* _GL_INTPROPS_H */
|