From 004e3fd03b9d0bf259b51d678866aa2e02487c81 Mon Sep 17 00:00:00 2001 From: Vitor de Lima Date: Wed, 23 Jul 2014 13:36:59 +0000 Subject: [PATCH] PowerPC64 support in safe_address function Add a ppc64-specific implementation of the safe_address function with overflow checking. --- Zend/zend_alloc.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 9467059aa9df2..c5d913e7613c8 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -2170,6 +2170,30 @@ static inline size_t safe_address(size_t nmemb, size_t size, size_t offset) return res; } +#elif defined(__GNUC__) && defined(__powerpc64__) + +static inline size_t safe_address(size_t nmemb, size_t size, size_t offset) +{ + size_t res; + unsigned long overflow; + + __asm__ ("mulld %0,%2,%3\n\t" + "mulhdu %1,%2,%3\n\t" + "addc %0,%0,%4\n\t" + "addze %1,%1\n" + : "=r"(res), "=r"(overflow) + : "r"(nmemb), + "r"(size), + "r"(offset)); + + if (UNEXPECTED(overflow)) { + zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); + return 0; + } + + return res; +} + #elif SIZEOF_SIZE_T == 4 && defined(HAVE_ZEND_LONG64) static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)