From patchwork Fri Dec 28 14:13:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 30892 Received: (qmail 116044 invoked by alias); 28 Dec 2018 14:13:25 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 116034 invoked by uid 89); 28 Dec 2018 14:13:25 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=scratch X-HELO: mail-qk1-f194.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id; bh=BYIVfGE1wQTDR1PsOUin+Nls3l0W0MWbnhxGF3+UU6o=; b=fN+El9ovFxZgrH5KRm+GWbXmGU3oDr4n55rhdCmteRA9ijg54B8i5ThEYFHf78Fiku I4BNUkqxxoJJdx6qWwrfzh+dSAlYsuGWhkzAXAQ3X8wj49JV0Nja3P3Jn7lXD/0pBVo5 QUFxnjjYhu2OB6imw4yzWEp+c2IbsvjETEIOQ= Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 1/2] Replace check_mul_overflow_size_t with __builtin_mul_overflow Date: Fri, 28 Dec 2018 12:13:15 -0200 Message-Id: <20181228141316.25952-1-adhemerval.zanella@linaro.org> Changes from previous version: - Replace INT_MULTIPLY_WRAPV with check_mul_overflow_size_t. --- Checked on x86_64-linux-gnu and i686-linux-gnu. * malloc/alloc_buffer_alloc_array.c (__libc_alloc_buffer_alloc_array): Use __builtin_mul_overflow in place of check_mul_overflow_size_t. * malloc/dynarray_emplace_enlarge.c (__libc_dynarray_emplace_enlarge): Likewise. * malloc/dynarray_resize.c (__libc_dynarray_resize): Likewise. * malloc/reallocarray.c (__libc_reallocarray): Likewise. * malloc/malloc-internal.h (check_mul_overflow_size_t): Remove function. * support/blob_repeat.c (check_mul_overflow_size_t, (minimum_stride_size, support_blob_repeat_allocate): Likewise. --- ChangeLog | 13 +++++++++++++ malloc/alloc_buffer_alloc_array.c | 3 +-- malloc/dynarray_emplace_enlarge.c | 3 +-- malloc/dynarray_resize.c | 3 +-- malloc/malloc-internal.h | 20 -------------------- malloc/reallocarray.c | 7 +++---- support/blob_repeat.c | 26 +++----------------------- 7 files changed, 22 insertions(+), 53 deletions(-) diff --git a/malloc/alloc_buffer_alloc_array.c b/malloc/alloc_buffer_alloc_array.c index 1dd098a8fc..7505422b43 100644 --- a/malloc/alloc_buffer_alloc_array.c +++ b/malloc/alloc_buffer_alloc_array.c @@ -17,7 +17,6 @@ . */ #include -#include #include void * @@ -28,7 +27,7 @@ __libc_alloc_buffer_alloc_array (struct alloc_buffer *buf, size_t element_size, /* The caller asserts that align is a power of two. */ size_t aligned = ALIGN_UP (current, align); size_t size; - bool overflow = check_mul_overflow_size_t (element_size, count, &size); + bool overflow = __builtin_mul_overflow (element_size, count, &size); size_t new_current = aligned + size; if (!overflow /* Multiplication did not overflow. */ && aligned >= current /* No overflow in align step. */ diff --git a/malloc/dynarray_emplace_enlarge.c b/malloc/dynarray_emplace_enlarge.c index 0408271e27..aa8f5fae3f 100644 --- a/malloc/dynarray_emplace_enlarge.c +++ b/malloc/dynarray_emplace_enlarge.c @@ -18,7 +18,6 @@ #include #include -#include #include #include @@ -52,7 +51,7 @@ __libc_dynarray_emplace_enlarge (struct dynarray_header *list, } size_t new_size; - if (check_mul_overflow_size_t (new_allocated, element_size, &new_size)) + if (__builtin_mul_overflow (new_allocated, element_size, &new_size)) return false; void *new_array; if (list->array == scratch) diff --git a/malloc/dynarray_resize.c b/malloc/dynarray_resize.c index 0bfca1ba4b..0205cf7ab2 100644 --- a/malloc/dynarray_resize.c +++ b/malloc/dynarray_resize.c @@ -18,7 +18,6 @@ #include #include -#include #include #include @@ -38,7 +37,7 @@ __libc_dynarray_resize (struct dynarray_header *list, size_t size, over-allocation here. */ size_t new_size_bytes; - if (check_mul_overflow_size_t (size, element_size, &new_size_bytes)) + if (__builtin_mul_overflow (size, element_size, &new_size_bytes)) { /* Overflow. */ __set_errno (ENOMEM); diff --git a/malloc/malloc-internal.h b/malloc/malloc-internal.h index 9cee0fb2d7..70d5b38504 100644 --- a/malloc/malloc-internal.h +++ b/malloc/malloc-internal.h @@ -74,24 +74,4 @@ void __malloc_fork_unlock_child (void) attribute_hidden; /* Called as part of the thread shutdown sequence. */ void __malloc_arena_thread_freeres (void) attribute_hidden; -/* Set *RESULT to LEFT * RIGHT. Return true if the multiplication - overflowed. */ -static inline bool -check_mul_overflow_size_t (size_t left, size_t right, size_t *result) -{ -#if __GNUC__ >= 5 - return __builtin_mul_overflow (left, right, result); -#else - /* size_t is unsigned so the behavior on overflow is defined. */ - *result = left * right; - size_t half_size_t = ((size_t) 1) << (8 * sizeof (size_t) / 2); - if (__glibc_unlikely ((left | right) >= half_size_t)) - { - if (__glibc_unlikely (right != 0 && *result / right != left)) - return true; - } - return false; -#endif -} - #endif /* _MALLOC_INTERNAL_H */ diff --git a/malloc/reallocarray.c b/malloc/reallocarray.c index 319eccd21f..4905dc6e4b 100644 --- a/malloc/reallocarray.c +++ b/malloc/reallocarray.c @@ -18,19 +18,18 @@ #include #include -#include +#include void * __libc_reallocarray (void *optr, size_t nmemb, size_t elem_size) { size_t bytes; - if (check_mul_overflow_size_t (nmemb, elem_size, &bytes)) + if (__builtin_mul_overflow (nmemb, elem_size, &bytes)) { __set_errno (ENOMEM); return 0; } - else - return realloc (optr, bytes); + return realloc (optr, bytes); } libc_hidden_def (__libc_reallocarray) diff --git a/support/blob_repeat.c b/support/blob_repeat.c index 718846d81d..daa1b7fd96 100644 --- a/support/blob_repeat.c +++ b/support/blob_repeat.c @@ -34,26 +34,6 @@ optimization because mappings carry a lot of overhead. */ static const size_t maximum_small_size = 4 * 1024 * 1024; -/* Set *RESULT to LEFT * RIGHT. Return true if the multiplication - overflowed. See . */ -static inline bool -check_mul_overflow_size_t (size_t left, size_t right, size_t *result) -{ -#if __GNUC__ >= 5 - return __builtin_mul_overflow (left, right, result); -#else - /* size_t is unsigned so the behavior on overflow is defined. */ - *result = left * right; - size_t half_size_t = ((size_t) 1) << (8 * sizeof (size_t) / 2); - if (__glibc_unlikely ((left | right) >= half_size_t)) - { - if (__glibc_unlikely (right != 0 && *result / right != left)) - return true; - } - return false; -#endif -} - /* Internal helper for fill. */ static void fill0 (char *target, const char *element, size_t element_size, @@ -138,8 +118,8 @@ minimum_stride_size (size_t page_size, size_t element_size) common multiple, it appears only once. Therefore, shift one factor. */ size_t multiple; - if (check_mul_overflow_size_t (page_size >> common_zeros, element_size, - &multiple)) + if (__builtin_mul_overflow (page_size >> common_zeros, element_size, + &multiple)) return 0; return multiple; } @@ -275,7 +255,7 @@ support_blob_repeat_allocate (const void *element, size_t element_size, size_t count) { size_t total_size; - if (check_mul_overflow_size_t (element_size, count, &total_size)) + if (__builtin_mul_overflow (element_size, count, &total_size)) { errno = EOVERFLOW; return (struct support_blob_repeat) { 0 };