Fix PR41465 - Use __builtin_mul_overflow instead of hand-rolled check.
authorEric Fiselier <eric@efcs.ca>
Thu, 11 Apr 2019 17:16:35 +0000 (17:16 +0000)
committerEric Fiselier <eric@efcs.ca>
Thu, 11 Apr 2019 17:16:35 +0000 (17:16 +0000)
On ARM the hand-rolled check causes a call to __aeabi_uidiv,
which we may not have a definition for.

Using the builtin avoids the generation of any library call.

llvm-svn: 358195

libcxxabi/include/__cxxabi_config.h
libcxxabi/src/cxa_vector.cpp

index fb24730..1e0edca 100644 (file)
@@ -61,6 +61,8 @@
 
 #if defined(__clang__)
 #define _LIBCXXABI_COMPILER_CLANG
+#elif defined(__GNUC__)
+#define _LIBCXXABI_COMPILER_GCC
 #endif
 
 #if __has_attribute(__no_sanitize__) && defined(_LIBCXXABI_COMPILER_CLANG)
index f53a956..3237b1d 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "cxxabi.h"
+#include "__cxxabi_config.h"
 
 #include <exception>        // for std::terminate
 #include <new>              // for std::bad_alloc
 
 #include "abort_message.h"
 
+#ifndef __has_builtin
+#define __has_builtin(x) 0
+#endif
+
 namespace __cxxabiv1 {
 
 #if 0
@@ -120,15 +125,35 @@ void throw_bad_array_new_length() {
 #endif
 }
 
+bool mul_overflow(size_t x, size_t y, size_t *res) {
+#if (defined(_LIBCXXABI_COMPILER_CLANG) && __has_builtin(__builtin_mul_overflow)) \
+    || defined(_LIBCXXABI_COMPILER_GCC)
+    return __builtin_mul_overflow(x, y, res);
+#else
+    *res = x * y;
+    return x && ((*res / x) != y);
+#endif
+}
+
+bool add_overflow(size_t x, size_t y, size_t *res) {
+#if (defined(_LIBCXXABI_COMPILER_CLANG) && __has_builtin(__builtin_add_overflow)) \
+    || defined(_LIBCXXABI_COMPILER_GCC)
+  return __builtin_add_overflow(x, y, res);
+#else
+  *res = x + y;
+  return *res < y;
+#endif
+}
+
 size_t calculate_allocation_size_or_throw(size_t element_count,
                                           size_t element_size,
                                           size_t padding_size) {
-  const size_t element_heap_size = element_count * element_size;
-  if (element_heap_size / element_count != element_size)
+  size_t element_heap_size;
+  if (mul_overflow(element_count, element_size, &element_heap_size))
     throw_bad_array_new_length();
 
-  const size_t allocation_size = element_heap_size + padding_size;
-  if (allocation_size < element_heap_size)
+  size_t allocation_size;
+  if (add_overflow(element_heap_size, padding_size, &allocation_size))
     throw_bad_array_new_length();
 
   return allocation_size;