From 73067e5c9af6583bfc1b2da36e8162ac35035478 Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Tue, 11 Dec 2018 21:27:37 +0100 Subject: [PATCH] allocation fail fuzzer: use a static library and objcopy oss-fuzz can not handle shared libraries. Do not build it by default. --- tests/libfuzzer/alloc_fail.c | 6 ++---- tests/meson.build | 37 ++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/tests/libfuzzer/alloc_fail.c b/tests/libfuzzer/alloc_fail.c index 515e8ee..50b2c4b 100644 --- a/tests/libfuzzer/alloc_fail.c +++ b/tests/libfuzzer/alloc_fail.c @@ -44,23 +44,21 @@ void dav1d_setup_alloc_fail(unsigned seed, unsigned probability) { fail_probability = probability; } -void * __real_malloc(size_t); void * __wrap_malloc(size_t); void * __wrap_malloc(size_t sz) { if (rand() < fail_probability) return NULL; - return __real_malloc(sz); + return malloc(sz); } #if defined(HAVE_POSIX_MEMALIGN) -int __real_posix_memalign(void **memptr, size_t alignment, size_t size); int __wrap_posix_memalign(void **memptr, size_t alignment, size_t size); int __wrap_posix_memalign(void **memptr, size_t alignment, size_t size) { if (rand() < fail_probability) return ENOMEM; - return __real_posix_memalign(memptr, alignment, size); + return posix_memalign(memptr, alignment, size); } #else #error "HAVE_POSIX_MEMALIGN required" diff --git a/tests/meson.build b/tests/meson.build index 9d8b304..16da420 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -124,28 +124,29 @@ dav1d_fuzzer_mt = executable('dav1d_fuzzer_mt', dependencies : [thread_dependency], ) -if (cc.has_function('posix_memalign', prefix : '#include ', args : test_args) and - cc.has_link_argument('-Wl,-wrap,malloc') and not get_option('b_lto')) - - alloc_fail = shared_library('alloc_fail', - files('libfuzzer/alloc_fail.c'), - libdav1d_nasm_objs_if_needed, - objects: [ - libdav1d.extract_all_objects(recursive: true), - ], - include_directories: dav1d_inc_dirs, - c_args : [stackalign_flag], - link_args: ['-Wl,-wrap,malloc', '-Wl,-wrap,posix_memalign'], - dependencies : [thread_dependency], - ) +objcopy = find_program('objcopy', + required: false) +if (objcopy.found() and + not get_option('b_lto') and + get_option('default_library') == 'static' and + cc.has_function('posix_memalign', prefix : '#include ', args : test_args)) + + libdav1d_af = custom_target('libdav1d_af', + input: libdav1d, + output: 'libdav1d_af.a', + depends: libdav1d, + command: [objcopy, + '--redefine-sym', 'malloc=__wrap_malloc', + '--redefine-sym', 'posix_memalign=__wrap_posix_memalign', + '@INPUT@', '@OUTPUT@']) dav1d_fuzzer_mem = executable('dav1d_fuzzer_mem', - dav1d_fuzzer_sources, + dav1d_fuzzer_sources + ['libfuzzer/alloc_fail.c'], include_directories: dav1d_inc_dirs, c_args: [stackalign_flag, stackrealign_flag, '-DDAV1D_ALLOC_FAIL'], - link_args: fuzzer_ldflags, - link_with : [alloc_fail], - build_by_default: true, + link_args: fuzzer_ldflags + [join_paths(libdav1d_af.full_path())], + link_depends: libdav1d_af, + build_by_default: false, dependencies : [thread_dependency], ) endif -- 2.18.1