From: Paul Floyd Date: Tue, 28 Feb 2023 20:21:05 +0000 (+0100) Subject: Change behaviour of posix_memalign for Solaris X-Git-Tag: VALGRIND_3_21_0~158 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=33ce1bf1cb01b49a441e43a11f5c2de21712e60b;p=thirdparty%2Fvalgrind.git Change behaviour of posix_memalign for Solaris It returns NULL and 0 status whilst most other platforms allocatae some undefined amount of memory (which is allowed by posix). Update the posix_memalign test as well. Finally remove some clang warnings about alignment. --- diff --git a/coregrind/m_replacemalloc/vg_replace_malloc.c b/coregrind/m_replacemalloc/vg_replace_malloc.c index 5ded69e969..cc38d665bf 100644 --- a/coregrind/m_replacemalloc/vg_replace_malloc.c +++ b/coregrind/m_replacemalloc/vg_replace_malloc.c @@ -1817,6 +1817,12 @@ extern int *___errno (void) __attribute__((weak)); /*---------------------- posix_memalign ----------------------*/ +#if defined(VGO_solaris) +#define VG_POSIX_MEMALIGN_SIZE_0_RETURN_NULL 1 +#else +#define VG_POSIX_MEMALIGN_SIZE_0_RETURN_NULL 0 +#endif + #define POSIX_MEMALIGN(soname, fnname) \ \ int VG_REPLACE_FUNCTION_EZU(10160,soname,fnname) \ @@ -1837,6 +1843,12 @@ extern int *___errno (void) __attribute__((weak)); || (alignment & (alignment - 1)) != 0) { \ return VKI_EINVAL; \ } \ + if (VG_POSIX_MEMALIGN_SIZE_0_RETURN_NULL && \ + size == 0U) { \ + /* no allocation for zro size on Solaris/Illumos */ \ + *memptr = NULL; \ + return 0; \ + } \ /* Round up to minimum alignment if necessary. */ \ if (alignment < VG_MIN_MALLOC_SZB) \ alignment = VG_MIN_MALLOC_SZB; \ @@ -1861,7 +1873,9 @@ extern int *___errno (void) __attribute__((weak)); POSIX_MEMALIGN(SO_SYN_MALLOC, posix_memalign); #elif defined(VGO_darwin) - //POSIX_MEMALIGN(VG_Z_LIBC_SONAME, posix_memalign); +#if (DARWIN_VERSIO >= DARWIN_10_6) + POSIX_MEMALIGN(VG_Z_LIBC_SONAME, posix_memalign); +#endif #elif defined(VGO_solaris) POSIX_MEMALIGN(VG_Z_LIBC_SONAME, posix_memalign); diff --git a/memcheck/tests/freebsd/Makefile.am b/memcheck/tests/freebsd/Makefile.am index 631d0efe9e..2259e1efb8 100644 --- a/memcheck/tests/freebsd/Makefile.am +++ b/memcheck/tests/freebsd/Makefile.am @@ -134,3 +134,5 @@ scalar_CFLAGS = ${AM_CFLAGS} -g errno_aligned_allocs_CFLAGS = ${AM_CFLAGS} @FLAG_W_NO_NON_POWER_OF_TWO_ALIGNMENT@ extattr_CFLAGS = ${AM_CFLAGS} @FLAG_W_NO_UNUSED_BUT_SET_VARIABLE@ + +memalign_CFLAGS = ${AM_CFLAGS} @FLAG_W_NO_NON_POWER_OF_TWO_ALIGNMENT@ diff --git a/memcheck/tests/posix_memalign.c b/memcheck/tests/posix_memalign.c index 415c6c025b..219df2ffd8 100644 --- a/memcheck/tests/posix_memalign.c +++ b/memcheck/tests/posix_memalign.c @@ -28,25 +28,33 @@ int main ( void ) # define PM(a,b,c) posix_memalign((void**)a, b, c) + // test for size 0 + res = PM(&p, 64, 0); +#if defined(VGO_solaris) + assert(NULL == p); +#else + assert(0 == res && p && 0 == (long)p % 64); +#endif + res = PM(&p, -1,100); assert(EINVAL == res); res = PM(&p, 0, 100); assert(EINVAL == res); res = PM(&p, 1, 100); assert(EINVAL == res); res = PM(&p, 2, 100); assert(EINVAL == res); res = PM(&p, 3, 100); assert(EINVAL == res); res = PM(&p, sizeof(void*), 100); - assert(0 == res && 0 == (long)p % sizeof(void*)); + assert(0 == res && p && 0 == (long)p % sizeof(void*)); res = PM(&p, 31, 100); assert(EINVAL == res); - res = PM(&p, 32, 100); assert(0 == res && 0 == (long)p % 32); + res = PM(&p, 32, 100); assert(0 == res && p && 0 == (long)p % 32); res = PM(&p, 33, 100); assert(EINVAL == res); res = PM(&p, 4095, 100); assert(EINVAL == res); - res = PM(&p, 4096, 100); assert(0 == res && 0 == (long)p % 4096); + res = PM(&p, 4096, 100); assert(0 == res && p && 0 == (long)p % 4096); res = PM(&p, 4097, 100); assert(EINVAL == res); - res = PM(&p, 4 * 1024 * 1024, 100); assert(0 == res + res = PM(&p, 4 * 1024 * 1024, 100); assert(0 == res && p && 0 == (long)p % (4 * 1024 * 1024)); - res = PM(&p, 16 * 1024 * 1024, 100); assert(0 == res + res = PM(&p, 16 * 1024 * 1024, 100); assert(0 == res &&p && 0 == (long)p % (16 * 1024 * 1024)); #endif }