Fix PR41465 - Use __builtin_mul_overflow instead of hand-rolled check.

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
This commit is contained in:
Eric Fiselier 2019-04-11 17:16:35 +00:00
parent 339594e4dc
commit f32463848b
2 changed files with 31 additions and 4 deletions

View 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)

View File

@ -11,12 +11,17 @@
//===----------------------------------------------------------------------===//
#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;