Skip to content

Clang on Windows: undefined behavior? #17462

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
cmb69 opened this issue Jan 13, 2025 · 3 comments
Closed

Clang on Windows: undefined behavior? #17462

cmb69 opened this issue Jan 13, 2025 · 3 comments

Comments

@cmb69
Copy link
Member

cmb69 commented Jan 13, 2025

Description

I came back to working with Clang on Windows, and did a minimal build with ASan and UBSan support (using the VS 2022 supplied clang 18.1.8):

configure --with-toolset=clang --disable-all --enable-cli --enable-sanitizer --enable-debug-pack

Now the following happens:

$ x64\Release_TS\php -v
TSRM\TSRM.c:265:7: runtime error: call to function sapi_globals_ctor through pointer to incorrect function type 'void (*)(void *)'
C:\php-sdk\phpdev\vs17\x64\php-src\main\SAPI.c:53: note: sapi_globals_ctor defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior TSRM\TSRM.c:265:7
Zend/zend_ini_parser.y:377:4: runtime error: call to function php_ini_parser_cb through pointer to incorrect function type 'void (*)(struct _zval_struct *, struct _zval_struct *, struct _zval_struct *, int, void *)'
C:\php-sdk\phpdev\vs17\x64\php-src\main\php_ini.c:184: note: php_ini_parser_cb defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior Zend/zend_ini_parser.y:377:4
PHP 8.5.0-dev (cli) (built: Jan 13 2025 13:48:08) (ZTS clang version 18.1.8  x64)
Copyright (c) The PHP Group
Zend Engine v4.5.0-dev, Copyright (c) Zend Technologies
TSRM\TSRM.c:560:8: runtime error: call to function basic_globals_dtor through pointer to incorrect function type 'void (*)(void *)'
C:\php-sdk\phpdev\vs17\x64\php-src\ext\standard\basic_functions.c:248: note: basic_globals_dtor defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior TSRM\TSRM.c:560:8
TSRM\TSRM.c:170:5: runtime error: call to function file_globals_dtor through pointer to incorrect function type 'void (*)(void *)'
C:\php-sdk\phpdev\vs17\x64\php-src\ext\standard\file.c:138: note: file_globals_dtor defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior TSRM\TSRM.c:170:5

I've stumbled upon this earlier, and back then considered these bogus diagnostics. I'm not sure any longer, though, but still suprised that this hasn't been caught so far (or are there some UBSan suppressions in place?) Maybe someone can clarify whether this is a proper diagnostic, or some false positive.

Note that applying

 Zend/zend_ini.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Zend/zend_ini.h b/Zend/zend_ini.h
index 1939d7b89e..3a72deb12d 100644
--- a/Zend/zend_ini.h
+++ b/Zend/zend_ini.h
@@ -234,7 +234,7 @@ END_EXTERN_C()
 #define ZEND_INI_STAGE_IN_REQUEST   (ZEND_INI_STAGE_ACTIVATE|ZEND_INI_STAGE_DEACTIVATE|ZEND_INI_STAGE_RUNTIME|ZEND_INI_STAGE_HTACCESS)
 
 /* INI parsing engine */
-typedef void (*zend_ini_parser_cb_t)(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg);
+typedef void (*zend_ini_parser_cb_t)(zval *arg1, zval *arg2, zval *arg3, int callback_type, HashTable *arg);
 BEGIN_EXTERN_C()
 ZEND_API zend_result zend_parse_ini_file(zend_file_handle *fh, bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg);
 ZEND_API zend_result zend_parse_ini_string(const char *str, bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg);

fixes the php_ini_parser_cb() warning.

PHP Version

master (but likely irrelevant)

Operating System

Windows

@arnaud-lb
Copy link
Member

We can find all such cases with -Wcast-function-type-strict. I think they are not harmful, but this could break some security hardening features such as CFI.

Currently --enable-undefined-sanitizer disables these checks explicitly because fixing these would require a lot of changes, but ideally we should avoid introducing new cases.

@cmb69
Copy link
Member Author

cmb69 commented Jan 13, 2025

Currently --enable-undefined-sanitizer disables these checks explicitly because fixing these would require a lot of changes, […]

Ah, thank you! (Additionally passing -fno-sanitize=function indeed suppresses the warnings for me.)

I think they are not harmful, but this could break some security hardening features such as CFI.

Yeah, not nice.

@nielsdos
Copy link
Member

I looked into this a few weeks ago, and the work that needs to be done is huge indeed. It took a while to get the test runner working without errors, but even then many warnings popped up. It goes as deep as the rc_dtor_func even...

cmb69 added a commit to cmb69/php-src that referenced this issue Jan 14, 2025
Like for POSIX systems, we pass `-fno-sanitize=function`.

Closes phpGH-17462.
@cmb69 cmb69 self-assigned this Jan 14, 2025
@cmb69 cmb69 closed this as completed in 7dbfacb Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants