From: Fred Morcos Date: Tue, 8 Aug 2023 13:14:30 +0000 (+0200) Subject: Meson: Use builtin feature to handle sanitizers X-Git-Tag: rec-5.1.0-alpha1~80^2~289 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9b9410ed4dda910a3724809246f371f02ef11d34;p=thirdparty%2Fpdns.git Meson: Use builtin feature to handle sanitizers --- diff --git a/meson.build b/meson.build index 31a8b09165..859a92f25d 100644 --- a/meson.build +++ b/meson.build @@ -68,8 +68,7 @@ subdir('meson/ixfrdist') # Ixfrdist subdir('meson/systemd') # Systemd and unit file handling subdir('meson/code-coverage') # Code coverage subdir('meson/auto-var-init') # Automatic Variable Initialization -# TODO Use meson's -Db_sanitize option? -# subdir('meson/sanitizers') # Sanitizers +subdir('meson/sanitizers') # Sanitizers subdir('meson/malloc-trace') # Malloc-trace # TODO Use meson's -Db_lto* options? subdir('meson/lto') # Link-time Optimization diff --git a/meson/sanitizers/address-sanitizer-fiber-switching/meson.build b/meson/sanitizers/address-sanitizer-fiber-switching/meson.build new file mode 100644 index 0000000000..35cbb31999 --- /dev/null +++ b/meson/sanitizers/address-sanitizer-fiber-switching/meson.build @@ -0,0 +1,48 @@ +# Address Sanitizer Fiber Switching +# Inputs: conf + +prog_single_pointer = ''' +#include + +int main() { + __sanitizer_finish_switch_fiber(nullptr); + return 0; +} +''' + +prog_three_pointers = ''' +#include + +int main() { + __sanitizer_finish_switch_fiber(nullptr, nullptr, nullptr); + return 0; +} +''' + +single_pointer = false +three_pointers = false + +if cxx.check_header('sanitizer/common_interface_defs.h', required: false) + if cxx.has_header_symbol('sanitizer/common_interface_defs.h', '__sanitizer_start_switch_fiber', required: false) + if cxx.compiles(prog_single_pointer, name: '__sanitizer_finish_switch_fiber with a single pointer') + single_pointer = true + endif + + if cxx.compiles(prog_three_pointers, name: '__sanitizer_finish_switch_fiber with three pointers') + three_pointers = true + endif + else + warning('Address Sanitizer fiber switching is not available') + endif + +else + warning('Address Sanitizer requested but `sanitizer/common_interface_defs.h` is invalid or cannot be found. Address Sanitizer fiber switching is not available') +endif + +if not single_pointer and not three_pointers + warning('Address Sanitizer fiber switching is not available due to an unknown API version') +endif + +conf.set10('HAVE_FIBER_SANITIZER', single_pointer or three_pointers, description: 'Address Sanitizer fiber annotation interface is available') +conf.set10('HAVE_SANITIZER_FINISH_SWITCH_FIBER_SINGLE_PTR', single_pointer, description: 'Address Sanitizer: __sanitizer_finish_switch_fiber takes only a pointer') +conf.set10('HAVE_SANITIZER_FINISH_SWITCH_FIBER_THREE_PTRS', three_pointers, description: 'Address Sanitizer: __sanitizer_finish_switch_fiber takes three pointers') diff --git a/meson/sanitizers/address-sanitizer/meson.build b/meson/sanitizers/address-sanitizer/meson.build deleted file mode 100644 index bf3d30a7d6..0000000000 --- a/meson/sanitizers/address-sanitizer/meson.build +++ /dev/null @@ -1,60 +0,0 @@ -# Address Sanitizer -# Inputs: conf - -opt_address_sanitizer = get_option('sanitizer-address') - -if not opt_address_sanitizer - subdir_done() -endif - -if not cxx.has_argument('-fsanitize=address') - error('Address Sanitizer requested but compiler does not support `-fsanitize=address`') -endif - -if not cxx.check_header('sanitizer/common_interface_defs.h') - error('Address Sanitizer requested but `sanitizer/common_interface_defs.h` is invalid or cannot be found') -endif - -if not cxx.has_header_symbol('sanitizer/common_interface_defs.h', '__sanitizer_start_switch_fiber', required: false) - warning('Address Sanitizer fiber switching is not available') -endif - -single_pointer = false -prog_single_pointer = ''' -#include - -int main() { - __sanitizer_finish_switch_fiber(nullptr); - return 0; -} -''' - -three_pointers = false -prog_three_pointers = ''' -#include - -int main() { - __sanitizer_finish_switch_fiber(nullptr, nullptr, nullptr); - return 0; -} -''' - -if cxx.compiles(prog_single_pointer, name: '__sanitizer_finish_switch_fiber with a single pointer') - single_pointer = true -endif - -if cxx.compiles(prog_three_pointers, name: '__sanitizer_finish_switch_fiber with three pointers') - three_pointers = true -endif - -if not single_pointer and not three_pointers - warning('Address Sanitizer fiber switching is not available due to an unknown API version') -endif - -conf.set10('HAVE_FIBER_SANITIZER', single_pointer or three_pointers, description: 'Address Sanitizer fiber annotation interface is available') -conf.set10('HAVE_SANITIZER_FINISH_SWITCH_FIBER_SINGLE_PTR', single_pointer, description: 'Address Sanitizer: __sanitizer_finish_switch_fiber takes only a pointer') -conf.set10('HAVE_SANITIZER_FINISH_SWITCH_FIBER_THREE_PTRS', three_pointers, description: 'Address Sanitizer: __sanitizer_finish_switch_fiber takes three pointers') - -add_global_arguments('-fsanitize=address', language: ['cpp']) - -summary('address', opt_address_sanitizer, bool_yn: true, section: 'Sanitizers') diff --git a/meson/sanitizers/meson.build b/meson/sanitizers/meson.build index 3850783e42..b207ccf032 100644 --- a/meson/sanitizers/meson.build +++ b/meson/sanitizers/meson.build @@ -1,46 +1,13 @@ # Sanitizers -subdir('address-sanitizer') +opt_sanitize = get_option('b_sanitize') -sanitizers = { - 'memory': 'Memory', - 'thread': 'Thread', - 'leak': 'Leak', - 'undefined': 'Undefined Behavior', -} - -foreach sanitizer, name: sanitizers - opt_name = 'opt_' + sanitizer + '_sanitizer' - set_variable(opt_name, get_option('sanitizer-' + sanitizer)) - - if not get_variable(opt_name) - continue - endif - - compiler_opt = '-fsanitize=' + sanitizer - if not cxx.has_argument(compiler_opt) - error(name + ' Sanitizer requested but compiler does not support `' + compiler_opt + '`') - endif - add_global_arguments(compiler_opt, language: ['cpp']) - summary(name, get_variable(opt_name), bool_yn: true, section: 'Sanitizers') -endforeach - -if opt_address_sanitizer and opt_thread_sanitizer - error('Address Sanitizer is not compatible with Thread Sanitizer') +if opt_sanitize == 'address' or opt_sanitize == 'address,undefined' + subdir('address-sanitizer-fiber-switching') endif -if opt_memory_sanitizer and opt_address_sanitizer - error('Memory Sanitizer is not compatible with Address Sanitizer') +if opt_sanitize != 'none' + add_global_arguments('-fno-omit-frame-pointer', language: ['c', 'cpp']) endif -if opt_memory_sanitizer and opt_leak_sanitizer - error('Memory Sanitizer is not compatible with Leak Sanitizer') -endif - -if opt_memory_sanitizer and opt_thread_sanitizer - error('Memory Sanitizer is not compatible with Thread Sanitizer') -endif - -if opt_address_sanitizer or opt_thread_sanitizer or opt_leak_sanitizer or opt_undefined_behavior_sanitizer or opt_memory_sanitizer - add_global_arguments('-fno-omit-frame-pointer', language: ['cpp']) -endif +summary('Sanitizers', opt_sanitize, bool_yn: true, section: 'Configuration') diff --git a/meson_options.txt b/meson_options.txt index 63de43de53..d4f14a7a1b 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -47,12 +47,6 @@ option('lua-records', type: 'boolean', value: true, description: 'Support Lua re option('systemd-service-user', type: 'string', value: 'pdns', description: 'Systemd service user (setuid and unit file; user is not created)') option('systemd-service-group', type: 'string', value: 'pdns', description: 'Systemd service group (setgid and unit file; group is not created)') option('auto-var-init', type: 'combo', value: 'disabled', choices: ['zero', 'pattern', 'disabled'], description: 'Enable initialization of automatic variables') -# TODO Use meson's -Db_sanitize option? -# option('sanitizer-address', type: 'boolean', value: false, description: 'Enable the Address Sanitizer') -# option('sanitizer-memory', type: 'boolean', value: false, description: 'Enable the Memory Sanitizer') -# option('sanitizer-thread', type: 'boolean', value: false, description: 'Enable the Thread Sanitizer') -# option('sanitizer-leak', type: 'boolean', value: false, description: 'Enable the Leak Sanitizer') -# option('sanitizer-undefined', type: 'boolean', value: false, description: 'Enable the Undefined Behavior Sanitizer') option('malloc-trace', type: 'boolean', value: false, description: 'Enable malloc-trace') # TODO Use meson's -Db_lto* options? option('lto', type: 'combo', choices: ['yes', 'no', 'thin', 'auto'], value: 'no', description: 'Enable link-time optimization')