Skip to content

Fix GH-11992: utf_encodings.phpt fails on Windows 32-bit #12726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 16 additions & 5 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -4637,6 +4637,14 @@ MBSTRING_API bool php_mb_check_encoding(const char *input, size_t length, const
return true;
}

/* MSVC 32-bit has issues with 64-bit intrinsics.
* (Bad 7/8-byte UTF-8 strings would be wrongly passed through as 'valid')
* It seems this is caused by a bug in MS Visual C++
* Ref: https://stackoverflow.com/questions/37509129/potential-bug-in-visual-studio-c-compiler-or-in-intel-intrinsics-avx2-mm256-s */
#if defined(PHP_WIN32) && !defined(__clang__) && defined(_MSC_VER) && defined(_M_IX86)
# define MBSTRING_BROKEN_X86_MSVC_INTRINSICS
#endif

/* If we are building an AVX2-only binary, don't compile the next function */
#ifndef ZEND_INTRIN_AVX2_NATIVE

Expand Down Expand Up @@ -4802,7 +4810,11 @@ static bool mb_fast_check_utf8_default(zend_string *str)
goto check_operand;
case 7:
case 8:
#ifdef MBSTRING_BROKEN_X86_MSVC_INTRINSICS
operand = _mm_set_epi32(0, 0, ((int32_t*)p)[1], ((int32_t*)p)[0]);
#else
operand = _mm_set_epi64x(0, *((uint64_t*)p));
#endif
goto check_operand;
case 9:
operand = _mm_srli_si128(_mm_loadu_si128((__m128i*)(p - 6)), 6);
Expand Down Expand Up @@ -5195,12 +5207,11 @@ static bool mb_fast_check_utf8_avx2(zend_string *str)
goto check_operand;
case 7:
case 8:
/* This was originally: operand = _mm256_set_epi64x(0, 0, 0, *((int64_t*)p));
* However, that caused test failures on 32-bit MS Windows
* (Bad 7/8-byte UTF-8 strings would be wrongly passed through as 'valid')
* It seems this is caused by a bug in MS Visual C++
* Ref: https://stackoverflow.com/questions/37509129/potential-bug-in-visual-studio-c-compiler-or-in-intel-intrinsics-avx2-mm256-s */
#ifdef MBSTRING_BROKEN_X86_MSVC_INTRINSICS
operand = _mm256_set_epi32(0, 0, 0, 0, 0, 0, ((int32_t*)p)[1], ((int32_t*)p)[0]);
#else
operand = _mm256_set_epi64x(0, 0, 0, *((int64_t*)p));
#endif
goto check_operand;
case 9:
operand = _mm256_set_m128i(_mm_setzero_si128(), _mm_srli_si128(_mm_loadu_si128((__m128i*)(p - 6)), 6));
Expand Down
3 changes: 0 additions & 3 deletions ext/mbstring/tests/utf_encodings.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ mbstring
--SKIPIF--
<?php
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
if (substr(PHP_OS, 0, 3) === 'WIN' && PHP_INT_SIZE === 4) {
die('xfail Fails on 32-bit Windows');
}
?>
--FILE--
<?php
Expand Down