Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Zend/Zend.m4
Original file line number Diff line number Diff line change
Expand Up @@ -382,18 +382,15 @@ AC_DEFUN([ZEND_CHECK_ALIGNMENT],
[AC_RUN_IFELSE([AC_LANG_SOURCE([
#include <stdio.h>
#include <stdlib.h>
#include <stdalign.h>

typedef union _mm_align_test {
void *ptr;
double dbl;
long lng;
} mm_align_test;

#if (defined (__GNUC__) && __GNUC__ >= 2)
#define ZEND_MM_ALIGNMENT (__alignof__ (mm_align_test))
#else
#define ZEND_MM_ALIGNMENT (sizeof(mm_align_test))
#endif
#define ZEND_MM_ALIGNMENT (alignof(mm_align_test))

int main(void)
{
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ZEND_API zend_string *zend_one_char_string[256];
ZEND_API zend_string **zend_known_strings = NULL;

/* this is read-only, so it's ok */
ZEND_SET_ALIGNED(16, static const char zend_hexconvtab[]) = "0123456789abcdef";
alignas(16) static const char zend_hexconvtab[] = "0123456789abcdef";

static zend_always_inline void zend_bin2hex_impl(char *out, const unsigned char *in, size_t in_len, const char *hexconvtab)
{
Expand Down
13 changes: 0 additions & 13 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -493,19 +493,6 @@ PHP_CHECK_AVX512_SUPPORTS
dnl Check AVX512 VBMI
PHP_CHECK_AVX512_VBMI_SUPPORTS

dnl Check for __alignof__ support in the compiler
AC_CACHE_CHECK([whether the compiler supports __alignof__],
[php_cv_have_alignof],
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [
int align = __alignof__(int);
(void)align;
])],
[php_cv_have_alignof=yes],
[php_cv_have_alignof=no])])
AS_VAR_IF([php_cv_have_alignof], [yes],
[AC_DEFINE([HAVE_ALIGNOF], [1],
[Define to 1 if the compiler supports '__alignof__'.])])

dnl Check for structure members.
AC_CHECK_MEMBERS([struct tm.tm_gmtoff],,,[#include <time.h>])
AC_CHECK_MEMBERS(m4_normalize([
Expand Down
18 changes: 5 additions & 13 deletions ext/hash/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,10 @@
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "zend_smart_str.h"
#include <stdalign.h>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we include this in zend_portability.h instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already is included in zend_portability


#include "hash_arginfo.h"

#ifdef PHP_WIN32
# define __alignof__ __alignof
#else
# ifndef HAVE_ALIGNOF
# include <stddef.h>
# define __alignof__(type) offsetof (struct { char c; type member;}, member)
# endif
#endif

static HashTable php_hash_hashtable;
zend_class_entry *php_hashcontext_ce;
static zend_object_handlers php_hashcontext_handlers;
Expand Down Expand Up @@ -138,16 +130,16 @@ static size_t parse_serialize_spec(
/* parse size */
if (*spec == 's' || *spec == 'S') {
*sz = 2;
alignment = __alignof__(uint16_t); /* usually 2 */
alignment = alignof(uint16_t); /* usually 2 */
} else if (*spec == 'l' || *spec == 'L') {
*sz = 4;
alignment = __alignof__(uint32_t); /* usually 4 */
alignment = alignof(uint32_t); /* usually 4 */
} else if (*spec == 'q' || *spec == 'Q') {
*sz = 8;
alignment = __alignof__(uint64_t); /* usually 8 */
alignment = alignof(uint64_t); /* usually 8 */
} else if (*spec == 'i' || *spec == 'I') {
*sz = sizeof(int);
alignment = __alignof__(int); /* usually 4 */
alignment = alignof(int); /* usually 4 */
} else {
ZEND_ASSERT(*spec == 'b' || *spec == 'B');
*sz = 1;
Expand Down
2 changes: 1 addition & 1 deletion ext/opcache/ZendAccelerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ typedef struct _zend_accel_shared_globals {
const void **jit_exit_groups;

/* Interned Strings Support (must be the last element) */
ZEND_SET_ALIGNED(ZEND_STRING_TABLE_POS_ALIGNMENT, zend_string_table interned_strings);
alignas(ZEND_STRING_TABLE_POS_ALIGNMENT) zend_string_table interned_strings;
} zend_accel_shared_globals;

#ifdef ZEND_WIN32
Expand Down
2 changes: 1 addition & 1 deletion ext/random/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ PHPAPI void *php_random_default_status(void)
/* }}} */

/* this is read-only, so it's ok */
ZEND_SET_ALIGNED(16, static const char hexconvtab[]) = "0123456789abcdef";
alignas(16) static const char hexconvtab[] = "0123456789abcdef";

/* {{{ php_random_bin2hex_le */
/* stolen from standard/string.c */
Expand Down
40 changes: 10 additions & 30 deletions ext/standard/crypt_sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,9 @@
#include <errno.h>
#include <limits.h>

#ifdef PHP_WIN32
# define __alignof__ __alignof
#else
# ifndef HAVE_ALIGNOF
# include <stddef.h>
# define __alignof__(type) offsetof (struct { char c; type member;}, member)
# endif
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdalign.h>

#ifdef PHP_WIN32
# include <string.h>
Expand Down Expand Up @@ -260,14 +252,7 @@ static void sha256_process_bytes(const void *buffer, size_t len, struct sha256_c

/* Process available complete blocks. */
if (len >= 64) {
/* To check alignment gcc has an appropriate operator. Other
compilers don't. */
#if __GNUC__ >= 2
# define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0)
#else
# define UNALIGNED_P(p) (((uintptr_t) p) % sizeof (uint32_t) != 0)
#endif
if (UNALIGNED_P (buffer))
if ((uintptr_t)buffer % alignof(uint32_t) != 0)
while (len > 64) {
sha256_process_block(memcpy(ctx->buffer, buffer, 64), 64, ctx);
buffer = (const char *) buffer + 64;
Expand Down Expand Up @@ -317,13 +302,8 @@ static const char b64t[64] ZEND_NONSTRING =

char * php_sha256_crypt_r(const char *key, const char *salt, char *buffer, int buflen)
{
#ifdef PHP_WIN32
ZEND_SET_ALIGNED(32, unsigned char alt_result[32]);
ZEND_SET_ALIGNED(32, unsigned char temp_result[32]);
#else
ZEND_SET_ALIGNED(__alignof__ (uint32_t), unsigned char alt_result[32]);
ZEND_SET_ALIGNED(__alignof__ (uint32_t), unsigned char temp_result[32]);
#endif
alignas(uint32_t) unsigned char alt_result[32];
alignas(uint32_t) unsigned char temp_result[32];

struct sha256_ctx ctx;
struct sha256_ctx alt_ctx;
Expand Down Expand Up @@ -371,15 +351,15 @@ char * php_sha256_crypt_r(const char *key, const char *salt, char *buffer, int b
SET_ALLOCA_FLAG(use_heap_key);
SET_ALLOCA_FLAG(use_heap_salt);

if ((uintptr_t)key % __alignof__ (uint32_t) != 0) {
tmp_key = (char *) do_alloca(key_len + __alignof__(uint32_t), use_heap_key);
key = copied_key = memcpy(tmp_key + __alignof__(uint32_t) - (uintptr_t)tmp_key % __alignof__(uint32_t), key, key_len);
if ((uintptr_t)key % alignof(uint32_t) != 0) {
tmp_key = (char *) do_alloca(key_len + alignof(uint32_t), use_heap_key);
key = copied_key = memcpy(tmp_key + alignof(uint32_t) - (uintptr_t)tmp_key % alignof(uint32_t), key, key_len);
}

if ((uintptr_t)salt % __alignof__(uint32_t) != 0) {
tmp_salt = (char *) do_alloca(salt_len + 1 + __alignof__(uint32_t), use_heap_salt);
if ((uintptr_t)salt % alignof(uint32_t) != 0) {
tmp_salt = (char *) do_alloca(salt_len + 1 + alignof(uint32_t), use_heap_salt);
salt = copied_salt =
memcpy(tmp_salt + __alignof__(uint32_t) - (uintptr_t)tmp_salt % __alignof__ (uint32_t), salt, salt_len);
memcpy(tmp_salt + alignof(uint32_t) - (uintptr_t)tmp_salt % alignof(uint32_t), salt, salt_len);
copied_salt[salt_len] = 0;
}

Expand Down
39 changes: 10 additions & 29 deletions ext/standard/crypt_sha512.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@

#include <errno.h>
#include <limits.h>
#ifdef PHP_WIN32
# define __alignof__ __alignof
#else
# ifndef HAVE_ALIGNOF
# include <stddef.h>
# define __alignof__(type) offsetof (struct { char c; type member;}, member)
# endif
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdalign.h>

#ifdef PHP_WIN32
# include <string.h>
Expand Down Expand Up @@ -294,14 +287,7 @@ sha512_process_bytes(const void *buffer, size_t len, struct sha512_ctx *ctx) {
/* Process available complete blocks. */
if (len >= 128) {
#ifndef _STRING_ARCH_unaligned
/* To check alignment gcc has an appropriate operator. Other
compilers don't. */
# if __GNUC__ >= 2
# define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint64_t) != 0)
# else
# define UNALIGNED_P(p) (((uintptr_t) p) % sizeof(uint64_t) != 0)
# endif
if (UNALIGNED_P(buffer))
if ((uintptr_t)buffer % alignof(uint64_t) != 0)
while (len > 128) {
sha512_process_block(memcpy(ctx->buffer, buffer, 128), 128, ctx);
buffer = (const char *) buffer + 128;
Expand Down Expand Up @@ -355,13 +341,8 @@ static const char b64t[64] ZEND_NONSTRING =

char *
php_sha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen) {
#ifdef PHP_WIN32
ZEND_SET_ALIGNED(64, unsigned char alt_result[64]);
ZEND_SET_ALIGNED(64, unsigned char temp_result[64]);
#else
ZEND_SET_ALIGNED(__alignof__ (uint64_t), unsigned char alt_result[64]);
ZEND_SET_ALIGNED(__alignof__ (uint64_t), unsigned char temp_result[64]);
#endif
alignas(uint64_t) unsigned char alt_result[64];
alignas(uint64_t) unsigned char temp_result[64];
struct sha512_ctx ctx;
struct sha512_ctx alt_ctx;
size_t salt_len;
Expand Down Expand Up @@ -409,15 +390,15 @@ php_sha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen)
SET_ALLOCA_FLAG(use_heap_key);
SET_ALLOCA_FLAG(use_heap_salt);

if ((uintptr_t)key % __alignof__ (uint64_t) != 0) {
tmp_key = (char *) do_alloca(key_len + __alignof__ (uint64_t), use_heap_key);
if ((uintptr_t)key % alignof(uint64_t) != 0) {
tmp_key = (char *) do_alloca(key_len + alignof(uint64_t), use_heap_key);
key = copied_key =
memcpy(tmp_key + __alignof__(uint64_t) - (uintptr_t)tmp_key % __alignof__(uint64_t), key, key_len);
memcpy(tmp_key + alignof(uint64_t) - (uintptr_t)tmp_key % alignof(uint64_t), key, key_len);
}

if ((uintptr_t)salt % __alignof__ (uint64_t) != 0) {
tmp_salt = (char *) do_alloca(salt_len + 1 + __alignof__(uint64_t), use_heap_salt);
salt = copied_salt = memcpy(tmp_salt + __alignof__(uint64_t) - (uintptr_t)tmp_salt % __alignof__(uint64_t), salt, salt_len);
if ((uintptr_t)salt % alignof(uint64_t) != 0) {
tmp_salt = (char *) do_alloca(salt_len + 1 + alignof(uint64_t), use_heap_salt);
salt = copied_salt = memcpy(tmp_salt + alignof(uint64_t) - (uintptr_t)tmp_salt % alignof(uint64_t), salt, salt_len);
copied_salt[salt_len] = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -4076,7 +4076,7 @@ PHPAPI zend_string *php_addslashes(zend_string *str) /* {{{ */
zend_string *php_addslashes_sse42(zend_string *str)
# endif
{
ZEND_SET_ALIGNED(16, static const char slashchars[16]) = "\'\"\\\0";
alignas(16) static const char slashchars[16] = "\'\"\\\0";
__m128i w128, s128;
uint32_t res = 0;
/* maximum string length, worst case situation */
Expand Down
Loading