Bug #103613 -DWITH_ASAN=ON cmake option does not add -fsanitize=address compiler flag
Submitted: 6 May 2021 13:14 Modified: 10 May 2021 14:43
Reporter: Yura Sorokin (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Compiling Severity:S3 (Non-critical)
Version:5.7.35 OS:Any
Assigned to: Tor Didriksen CPU Architecture:Any

[6 May 2021 13:14] Yura Sorokin
Description:
Sand-alone -DWITH_ASAN=ON cmake configuration option does not add -fsanitize=address compiler flag to CMAKE_C_FLAGS / CMAKE_CXX_FLAGS.

The only way to enable Address Sanitizer now is to supply both -DWITH_ASAN=ON and -DWITH_ASAN_SCOPE=ON, in which case -fsanitize-address-use-after-scope will be added to the compiler options implicitly triggering -fsanitize=address.

The issue exists for both GCC and Clang compilers.

How to repeat:
Run
cmake -DWITH_ASAN=ON ...

and check
CMAKE_C_FLAGS / CMAKE_CXX_FLAGS in the summary report

Suggested fix:
Add
    STRING_APPEND(CMAKE_C_FLAGS   " -fsanitize=address")
    STRING_APPEND(CMAKE_CXX_FLAGS " -fsanitize=address")
after
    SET(HAVE_ASAN 1)
Here
https://github.com/mysql/mysql-server/blob/mysql-5.7.34/CMakeLists.txt#L355

Also, consider removing WITH_ASAN_SCOPE completely - both recent GCC and Clang have sanitize-address-use-after-scope enabled by default when -fsanitize=address is specified.
[6 May 2021 13:54] Yura Sorokin
5.7.34 fix

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: bug103613_5_7_34_fix.diff (application/octet-stream, text), 522 bytes.

[6 May 2021 14:04] Yura Sorokin
One more argument for supporting WITH_ASAN_SCOPE removal - starting from Clang 11, -fsanitize-address-use-after-scope is no longer a valid command line option.
[7 May 2021 8:11] Tor Didriksen
MySQL 5.7 entered feature freeze several years ago. The set of supported platforms is here:

https://www.mysql.com/support/supportedplatforms/database.html

We usually build with "native" compilers, which means we build with gcc versions
ranging from 4.4.7 to 8.3.0. Old versions of Visual Studio on win, and old versions of Oracle Studio on Solaris.

You will notice that clang is not the native compiler on any of the platforms supported (FreeBSD and Mac support has been dropped).

After this patch:
Bug #101996 	backport the patch for Bug #27874068 ADD SUPPORT FOR -DCMAKE_BUILD_TYPE=RELEASE

you should be able to specify whatever CMAKE_CXX_FLAGS you like, without having them be clobbered by whatever defaults we have for our own builds.
So if you want to run Clang11 ASAN, I suggest you specify your own CMAKE_CXX_FLAGS.
[7 May 2021 8:45] MySQL Verification Team
Hello Yura Sorokin,

Thank you for the report and feedback.

regards,
Umesh
[7 May 2021 9:48] Tor Didriksen
Ah, I see now.There was a glitch in the patch for
        
Bug #32287863 BACKPORT THE PATCH FOR BUG #27874068 ADD SUPPORT FOR -DCMAKE_BUILD_TYPE=RELEASE

Will fix, thant you.
[10 May 2021 14:43] Paul DuBois
Posted by developer:
 
Fixed in 5.7.35, 8.0.26.

Configuration cleanup. No changelog entry required.