diff options
| author | Nathan Moinvaziri <nathan@nathanm.com> | 2026-03-09 18:27:58 -0700 |
|---|---|---|
| committer | Hans Kristian Rosbach <hk-github@circlestorm.org> | 2026-03-11 00:56:35 +0100 |
| commit | 2f12c030c888608175e0b343ef0b30003fae4ac6 (patch) | |
| tree | 4fdc27b8ed9c640cdb964b528ef798ea13fc8e1a | |
| parent | 4c621b14a0cd3c4654ff469a02ac10e3356005b4 (diff) | |
| download | Project-Tick-2f12c030c888608175e0b343ef0b30003fae4ac6.tar.gz Project-Tick-2f12c030c888608175e0b343ef0b30003fae4ac6.zip | |
Move ASAN/MSAN instrumentation out of zbuild.h
Create zsanitizer.h with all sanitizer detection, declaration
stubs, and instrument_read/write/read_write macros. Include it
only in the chunkset, inflate, and dfltcc files that perform
deliberate out-of-bounds reads for performance.
| -rw-r--r-- | arch/arm/chunkset_neon.c | 1 | ||||
| -rw-r--r-- | arch/loongarch/chunkset_lasx.c | 1 | ||||
| -rw-r--r-- | arch/loongarch/chunkset_lsx.c | 1 | ||||
| -rw-r--r-- | arch/s390/dfltcc_detail.h | 1 | ||||
| -rw-r--r-- | arch/x86/chunkset_avx2.c | 1 | ||||
| -rw-r--r-- | arch/x86/chunkset_ssse3.c | 1 | ||||
| -rw-r--r-- | inflate.c | 1 | ||||
| -rw-r--r-- | zbuild.h | 60 | ||||
| -rw-r--r-- | zsanitizer.h | 68 |
9 files changed, 75 insertions, 60 deletions
diff --git a/arch/arm/chunkset_neon.c b/arch/arm/chunkset_neon.c index 0a06122ae1..a891f10fa5 100644 --- a/arch/arm/chunkset_neon.c +++ b/arch/arm/chunkset_neon.c @@ -5,6 +5,7 @@ #ifdef ARM_NEON #include "zbuild.h" +#include "zsanitizer.h" #include "zmemory.h" #include "neon_intrins.h" #include "arch/generic/chunk_128bit_perm_idx_lut.h" diff --git a/arch/loongarch/chunkset_lasx.c b/arch/loongarch/chunkset_lasx.c index a85c07d094..905704172d 100644 --- a/arch/loongarch/chunkset_lasx.c +++ b/arch/loongarch/chunkset_lasx.c @@ -6,6 +6,7 @@ #ifdef LOONGARCH_LASX #include "zbuild.h" +#include "zsanitizer.h" #include "zmemory.h" #include <lasxintrin.h> diff --git a/arch/loongarch/chunkset_lsx.c b/arch/loongarch/chunkset_lsx.c index e626157f40..23dabfba51 100644 --- a/arch/loongarch/chunkset_lsx.c +++ b/arch/loongarch/chunkset_lsx.c @@ -6,6 +6,7 @@ #ifdef LOONGARCH_LSX #include "zbuild.h" +#include "zsanitizer.h" #include "zmemory.h" #include <lsxintrin.h> diff --git a/arch/s390/dfltcc_detail.h b/arch/s390/dfltcc_detail.h index b367f42f37..f790735ab4 100644 --- a/arch/s390/dfltcc_detail.h +++ b/arch/s390/dfltcc_detail.h @@ -1,4 +1,5 @@ #include "zbuild.h" +#include "zsanitizer.h" #include <stdio.h> #ifdef HAVE_SYS_SDT_H diff --git a/arch/x86/chunkset_avx2.c b/arch/x86/chunkset_avx2.c index 953d3b05e2..3e69a7bf66 100644 --- a/arch/x86/chunkset_avx2.c +++ b/arch/x86/chunkset_avx2.c @@ -5,6 +5,7 @@ #ifdef X86_AVX2 #include "zbuild.h" +#include "zsanitizer.h" #include "zmemory.h" #include "arch/generic/chunk_256bit_perm_idx_lut.h" diff --git a/arch/x86/chunkset_ssse3.c b/arch/x86/chunkset_ssse3.c index 3085f57a35..0bef7de811 100644 --- a/arch/x86/chunkset_ssse3.c +++ b/arch/x86/chunkset_ssse3.c @@ -5,6 +5,7 @@ #ifdef X86_SSSE3 #include "zbuild.h" +#include "zsanitizer.h" #include "zmemory.h" #include <immintrin.h> @@ -4,6 +4,7 @@ */ #include "zbuild.h" +#include "zsanitizer.h" #include "zutil.h" #include "inftrees.h" #include "inflate.h" @@ -349,64 +349,4 @@ # define OPTIMAL_CMP 16 #endif -#if defined(__has_feature) -# if __has_feature(address_sanitizer) -# define Z_ADDRESS_SANITIZER 1 -# endif -#elif defined(__SANITIZE_ADDRESS__) -# define Z_ADDRESS_SANITIZER 1 -#endif - -/* - * __asan_loadN() and __asan_storeN() calls are inserted by compilers in order to check memory accesses. - * They can be called manually too, with the following caveats: - * gcc says: "warning: implicit declaration of function '...'" - * g++ says: "error: new declaration '...' ambiguates built-in declaration '...'" - * Accommodate both. - */ -#ifdef Z_ADDRESS_SANITIZER -#ifndef __cplusplus -void __asan_loadN(void *, long); -void __asan_storeN(void *, long); -#endif -#else -# define __asan_loadN(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) -# define __asan_storeN(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) -#endif - -#if defined(__has_feature) -# if __has_feature(memory_sanitizer) -# define Z_MEMORY_SANITIZER 1 -# include <sanitizer/msan_interface.h> -# endif -#endif - -#ifndef Z_MEMORY_SANITIZER -# define __msan_check_mem_is_initialized(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) -# define __msan_unpoison(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) -#endif - -/* Notify sanitizer runtime about an upcoming read access. */ -#define instrument_read(a, size) do { \ - void *__a = (void *)(a); \ - long __size = size; \ - __asan_loadN(__a, __size); \ - __msan_check_mem_is_initialized(__a, __size); \ -} while (0) - -/* Notify sanitizer runtime about an upcoming write access. */ -#define instrument_write(a, size) do { \ - void *__a = (void *)(a); \ - long __size = size; \ - __asan_storeN(__a, __size); \ -} while (0) - -/* Notify sanitizer runtime about an upcoming read/write access. */ -#define instrument_read_write(a, size) do { \ - void *__a = (void *)(a); \ - long __size = size; \ - __asan_storeN(__a, __size); \ - __msan_check_mem_is_initialized(__a, __size); \ -} while (0) - #endif diff --git a/zsanitizer.h b/zsanitizer.h new file mode 100644 index 0000000000..d77456d9da --- /dev/null +++ b/zsanitizer.h @@ -0,0 +1,68 @@ +/* zsanitizer.h -- sanitizer instrumentation for deliberate OOB and uninitialized memory access. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#ifndef ZSANITIZER_H +#define ZSANITIZER_H + +#if defined(__has_feature) +# if __has_feature(address_sanitizer) +# define Z_ADDRESS_SANITIZER 1 +# endif +#elif defined(__SANITIZE_ADDRESS__) +# define Z_ADDRESS_SANITIZER 1 +#endif + +/* + * __asan_loadN() and __asan_storeN() calls are inserted by compilers in order to check memory accesses. + * They can be called manually too, with the following caveats: + * gcc says: "warning: implicit declaration of function '...'" + * g++ says: "error: new declaration '...' ambiguates built-in declaration '...'" + * Accommodate both. + */ +#ifdef Z_ADDRESS_SANITIZER +# ifndef __cplusplus +void __asan_loadN(void *, long); +void __asan_storeN(void *, long); +# endif +#else +# define __asan_loadN(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) +# define __asan_storeN(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) +#endif + +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) +# define Z_MEMORY_SANITIZER 1 +# include <sanitizer/msan_interface.h> +# endif +#endif + +#ifndef Z_MEMORY_SANITIZER +# define __msan_check_mem_is_initialized(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) +# define __msan_unpoison(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0) +#endif + +/* Notify sanitizer runtime about an upcoming read access. */ +#define instrument_read(a, size) do { \ + void *__a = (void *)(a); \ + long __size = size; \ + __asan_loadN(__a, __size); \ + __msan_check_mem_is_initialized(__a, __size); \ +} while (0) + +/* Notify sanitizer runtime about an upcoming write access. */ +#define instrument_write(a, size) do { \ + void *__a = (void *)(a); \ + long __size = size; \ + __asan_storeN(__a, __size); \ +} while (0) + +/* Notify sanitizer runtime about an upcoming read/write access. */ +#define instrument_read_write(a, size) do { \ + void *__a = (void *)(a); \ + long __size = size; \ + __asan_storeN(__a, __size); \ + __msan_check_mem_is_initialized(__a, __size); \ +} while (0) + +#endif |
