From patchwork Fri Dec 21 18:38:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 154418 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp1248416ljp; Fri, 21 Dec 2018 10:38:41 -0800 (PST) X-Google-Smtp-Source: AFSGD/U/T/rfSlpjAE9TOxf4jmfvogGXZNr5r68KiE+khFqNoDLtLc2fSpLmC9VE95EdbR2zs1kq X-Received: by 2002:a62:7e93:: with SMTP id z141mr3569211pfc.239.1545417521086; Fri, 21 Dec 2018 10:38:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545417521; cv=none; d=google.com; s=arc-20160816; b=jXRttaL4ajAnWvc0HKMKqBWwx9AnQHvQmqhrhKZDfDaIfAfyqSaACSzoejZRiu61nO Q2Djj282ybajjAdAJPcB33QmUzHSuFuvR4Rk9btjplS53dvLXhIP/3ti29Bb1nIMht7h GMZL7I8QrZB7irNVYMfZiogTQzcEumdjpsoRViXlNNzrCjFr4lsA6Dkrgp7f3+zYiToV tVm9QqAbBZGmJ9dYQC9fs/7CRutoC67ZCuI7Rp9CNQTfHX++LvtZ7IcAlTfG3X8ecNhU E9Vkl9RTbojS9YxYJhxAEcfw/zi3Ajpt+2gcOd4DvAklsiQT1LFjAyoDeNfm2T1bjpQe juaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:to:from:dkim-signature:delivered-to:sender :list-help:list-post:list-archive:list-subscribe:list-unsubscribe :list-id:precedence:mailing-list:dkim-signature:domainkey-signature; bh=dLNZV6/QWR157oOMzPeShfwU2o/vVR9pZbkUX91TLQk=; b=KD83lSQr2Yd8xR+PvKW4brW76LPCwV4+fr82iY899ZP9JTjJ58lrPqfzGiM229huVx 7z2gjiRBXR7K5NHI1aA0Fm+ewjJ+UdEhqbeKwqPO9w4DnCrasii7s86EA8VmZyU5gnFL ZBpRghMhcYO/s6gyCC/FRNifEQwtWU6jNf8/Ar2x+GDN81UuiGhOu4lk1SlOZUAWFqCp olBWfCsdrcGrhmjUPoDJwMkb5NMFAP1X0RbIY+3asExXgjks8dnDeI2+CfoT0nrpLNRK Oxu0KhjEl+xjclGOvaCgr3D9yONU2ZNqV3zcYPbjhxzD1gIb9KWizi5PbiWBdtkbO18O 9OFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=VYztuylc; dkim=pass header.i=@linaro.org header.s=google header.b="gH/qCv88"; spf=pass (google.com: domain of libc-alpha-return-98718-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-98718-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id p3si3468167plk.424.2018.12.21.10.38.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Dec 2018 10:38:41 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-98718-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=VYztuylc; dkim=pass header.i=@linaro.org header.s=google header.b="gH/qCv88"; spf=pass (google.com: domain of libc-alpha-return-98718-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-98718-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; q=dns; s= default; b=ah6k8TRXJVFdBhEeOdtGiE8k8wAsmtsdGucL/MKmY8vGxdJFf2XRL 44DeBQrhuiVtxPjPUvvQE0JK7Wk9Munc/JGENvr3u/DhLmm92/s27G8PTrQO5Er3 FWku83UlyRILr9vrh3wFGKFDlFElNbovMu8D+zg74HsNsow/DkYtHU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; s=default; bh=27/vRfWPFUUyYB20kc0P4RAHWR8=; b=VYztuylcR4tvLn2Rims09TOfB6H8 iV04f53+uea0iLtU95M+aAbjkt5nZiQCa+oz42zPz0+pVnGn2x1EH7M0S/zZm+L4 Yqe5vROwgrbfFSmKKhOuKRHcfoBblPqtHMW6zV/qw3Twut4fwI0IsUPmtuSMoYIi qd6nJ9VA9qLvw50= Received: (qmail 10685 invoked by alias); 21 Dec 2018 18:38:31 -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 10671 invoked by uid 89); 21 Dec 2018 18:38:31 -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=fill, shutdown, 387, 1024 X-HELO: mail-qt1-f196.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=dLNZV6/QWR157oOMzPeShfwU2o/vVR9pZbkUX91TLQk=; b=gH/qCv8859vH42U70S8dITXt3VxJo8o9KAZddEz7XDK5jFX1+9HTgI4UBACSsPF9YE MvmL28lu04QrunCaVZ8rxIN46sUaEGJ6FBMj8kE1nWlJkKuksCP2xzvTs/lCIRglGjDb caUxx1JwHteBmgWlnB3Jf+yXw90v4fAMxKBgc= Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 1/2] Replace check_mul_overflow_size_t with INT_MULTIPLY_WRAPV Date: Fri, 21 Dec 2018 16:38:12 -0200 Message-Id: <20181221183813.16245-1-adhemerval.zanella@linaro.org> Checked on x86_64-linux-gnu and i686-linux-gnu. * malloc/alloc_buffer_alloc_array.c (__libc_alloc_buffer_alloc_array): Use INT_MULTIPLY_WRAPV 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 | 4 ++-- malloc/dynarray_emplace_enlarge.c | 4 ++-- malloc/dynarray_resize.c | 4 ++-- malloc/malloc-internal.h | 20 -------------------- malloc/reallocarray.c | 7 +++---- support/blob_repeat.c | 27 ++++----------------------- 7 files changed, 26 insertions(+), 53 deletions(-) -- 2.17.1 diff --git a/malloc/alloc_buffer_alloc_array.c b/malloc/alloc_buffer_alloc_array.c index 1dd098a8fc..35de7115e2 100644 --- a/malloc/alloc_buffer_alloc_array.c +++ b/malloc/alloc_buffer_alloc_array.c @@ -17,7 +17,7 @@ . */ #include -#include +#include #include void * @@ -28,7 +28,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 = INT_MULTIPLY_WRAPV (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..7538cbe4c5 100644 --- a/malloc/dynarray_emplace_enlarge.c +++ b/malloc/dynarray_emplace_enlarge.c @@ -18,7 +18,7 @@ #include #include -#include +#include #include #include @@ -52,7 +52,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 (INT_MULTIPLY_WRAPV (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..4d766605ff 100644 --- a/malloc/dynarray_resize.c +++ b/malloc/dynarray_resize.c @@ -18,7 +18,7 @@ #include #include -#include +#include #include #include @@ -38,7 +38,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 (INT_MULTIPLY_WRAPV (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..f3a83abc91 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 (INT_MULTIPLY_WRAPV (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..e4260872fd 100644 --- a/support/blob_repeat.c +++ b/support/blob_repeat.c @@ -29,31 +29,12 @@ #include #include #include +#include /* Small allocations should use malloc directly instead of the mmap 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 +119,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 (INT_MULTIPLY_WRAPV (page_size >> common_zeros, element_size, + &multiple)) return 0; return multiple; } @@ -275,7 +256,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 (INT_MULTIPLY_WRAPV (element_size, count, &total_size)) { errno = EOVERFLOW; return (struct support_blob_repeat) { 0 };