Skip to content

Commit 4d75359

Browse files
committed
Fix GH-9068: Conditional jump or move depends on uninitialised value(s)
This patch preserves the scratch registers of the SysV x86-64 ABI by storing them to the stack and restoring them later. We need to do this to prevent the registers of the caller from being corrupted. The reason these get corrupted is because the compiler is unaware of the Valgrind replacement function and thus makes assumptions about the original function regarding registers which are not true for the replacement function. For implementation I used a GCC and Clang attribute. A more general approach would be to use inline assembly but that's also less portable and quite hacky. This attributes is supported since GCC 7.x, but the target option is only supported since 11.x. For Clang the target option does not matter.
1 parent fbf5216 commit 4d75359

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Zend/zend_string.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,26 @@ ZEND_API void zend_interned_strings_switch_storage(bool request)
372372
# define I_REPLACE_SONAME_FNNAME_ZU(soname, fnname) _vgr00000ZU_ ## soname ## _ ## fnname
373373
#endif
374374

375-
ZEND_API bool ZEND_FASTCALL I_REPLACE_SONAME_FNNAME_ZU(NONE,zend_string_equal_val)(zend_string *s1, zend_string *s2)
375+
/* See GH-9068 */
376+
#if defined(__GNUC__) && (__GNUC__ >= 11 || defined(__clang__)) && __has_attribute(no_caller_saved_registers)
377+
# define NO_CALLER_SAVED_REGISTERS __attribute__((no_caller_saved_registers))
378+
# ifndef __clang__
379+
# pragma GCC push_options
380+
# pragma GCC target ("general-regs-only")
381+
# endif
382+
#else
383+
# define NO_CALLER_SAVED_REGISTERS
384+
#endif
385+
386+
ZEND_API bool ZEND_FASTCALL NO_CALLER_SAVED_REGISTERS I_REPLACE_SONAME_FNNAME_ZU(NONE,zend_string_equal_val)(zend_string *s1, zend_string *s2)
376387
{
377388
return !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1));
378389
}
379390

391+
#if defined(__GNUC__) && __GNUC__ >= 11 && !defined(__clang__) && __has_attribute(no_caller_saved_registers)
392+
# pragma GCC pop_options
393+
#endif
394+
380395
#if defined(__GNUC__) && defined(__i386__)
381396
ZEND_API bool ZEND_FASTCALL zend_string_equal_val(zend_string *s1, zend_string *s2)
382397
{

0 commit comments

Comments
 (0)