From a93b6c0a653a4667d3e95b0bc6c4cb03bf39d4a2 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Mon, 9 Mar 2020 15:55:22 +0800 Subject: [PATCH] Use _SSE2_ instrs to to sanitize name --- tests/issue297.phpt | 11 ++++++++--- yaf_loader.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/tests/issue297.phpt b/tests/issue297.phpt index a3437a5c..003358f5 100644 --- a/tests/issue297.phpt +++ b/tests/issue297.phpt @@ -10,15 +10,20 @@ yaf.lowcase_path=0 yaf.throw_exception=0 yaf.catch_exception=1 yaf.use_namespace=1 +yaf.use_spl_autoload=0 --FILE-- autoload("Test\Test")); +var_dump(new \Test\Test\Test\Test\Test\Test\T\Test\Test); //for __SSE2__ ?> --EXPECTF-- -Warning: Yaf\Loader::autoload(): Failed opening script %s/Test/Test.php: No such file or directory in %sissue297.php on line %d +Warning: Yaf\Loader::autoload(): Failed opening script %s%cTest/Test.php: No such file or directory in %sissue297.php on line %d +bool(true) -Fatal error: Uncaught Error: Class 'Test\Test' not found in %sissue297.php:%d +Warning: Yaf\Loader::autoload(): Failed opening script %s/Test/Test/Test/Test/Test/Test/T/Test/Test.php: No such file or directory in %sissue297.php on line %d + +Fatal error: Uncaught Error: Class 'Test\Test\Test\Test\Test\Test\T\Test\Test' not found in %sissue297.php:%d Stack trace: #0 {main} thrown in %sissue297.php on line %d diff --git a/yaf_loader.c b/yaf_loader.c index 402fdc48..f021a1b7 100644 --- a/yaf_loader.c +++ b/yaf_loader.c @@ -21,6 +21,10 @@ #include "php.h" #include "zend_smart_str.h" /* for smart_str */ +#ifdef __SSE2__ +#include +#endif + #include "php_yaf.h" #include "yaf_application.h" #include "yaf_namespace.h" @@ -221,7 +225,31 @@ static inline char* yaf_loader_sanitize_name(char *name, size_t len) /* {{{ */ { /* replace all '\' to '_' */ sanitized_name = estrndup(name, len); pos = sanitized_name + (pos - name); - while ((*pos = '_', pos = memchr(pos, '\\', len - (pos - sanitized_name)))); +#ifdef __SSE2__ + do { + const __m128i slash = _mm_set1_epi8('\\'); + const __m128i delta = _mm_set1_epi8('_' - '\\'); + len -= (pos - sanitized_name); + while (len >= 16) { + __m128i op = _mm_loadu_si128((__m128i *)pos); + __m128i eq = _mm_cmpeq_epi8(op, slash); + if (_mm_movemask_epi8(eq)) { + eq = _mm_and_si128(eq, delta); + op = _mm_add_epi8(op, eq); + _mm_storeu_si128((__m128i*)pos, op); + } + len -= 16; + pos += 16; + } + } while (0); + + name = pos; + while ((pos = memchr(pos, '\\', len - (pos - name)))) { + *pos++ = '_'; + } +#else + while ((*pos++ = '_', pos = memchr(pos, '\\', len - (pos - sanitized_name)))); +#endif } return sanitized_name;