Home | History | Annotate | Line # | Download | only in kern
subr_asan.c revision 1.27
      1  1.27    martin /*	$NetBSD: subr_asan.c,v 1.27 2020/12/18 15:33:34 martin Exp $	*/
      2   1.1      maxv 
      3   1.1      maxv /*
      4  1.26      maxv  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
      5   1.1      maxv  * All rights reserved.
      6   1.1      maxv  *
      7  1.26      maxv  * This code is part of the KASAN subsystem of the NetBSD kernel.
      8   1.1      maxv  *
      9   1.1      maxv  * Redistribution and use in source and binary forms, with or without
     10   1.1      maxv  * modification, are permitted provided that the following conditions
     11   1.1      maxv  * are met:
     12   1.1      maxv  * 1. Redistributions of source code must retain the above copyright
     13   1.1      maxv  *    notice, this list of conditions and the following disclaimer.
     14   1.1      maxv  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1      maxv  *    notice, this list of conditions and the following disclaimer in the
     16   1.1      maxv  *    documentation and/or other materials provided with the distribution.
     17   1.1      maxv  *
     18  1.26      maxv  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.26      maxv  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.26      maxv  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.26      maxv  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.26      maxv  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  1.26      maxv  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  1.26      maxv  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  1.26      maxv  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  1.26      maxv  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.26      maxv  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.26      maxv  * SUCH DAMAGE.
     29   1.1      maxv  */
     30   1.1      maxv 
     31   1.1      maxv #include <sys/cdefs.h>
     32  1.27    martin __KERNEL_RCSID(0, "$NetBSD: subr_asan.c,v 1.27 2020/12/18 15:33:34 martin Exp $");
     33   1.1      maxv 
     34   1.1      maxv #include <sys/param.h>
     35   1.1      maxv #include <sys/device.h>
     36   1.1      maxv #include <sys/kernel.h>
     37   1.1      maxv #include <sys/param.h>
     38   1.1      maxv #include <sys/conf.h>
     39   1.1      maxv #include <sys/systm.h>
     40   1.1      maxv #include <sys/types.h>
     41   1.1      maxv #include <sys/asan.h>
     42   1.1      maxv 
     43  1.25  riastrad #include <uvm/uvm_extern.h>
     44   1.1      maxv 
     45  1.27    martin #ifdef DDB
     46  1.27    martin #include <machine/db_machdep.h>
     47  1.27    martin #include <ddb/db_extern.h>
     48  1.27    martin #endif
     49  1.27    martin 
     50  1.10      maxv #ifdef KASAN_PANIC
     51  1.10      maxv #define REPORT panic
     52  1.10      maxv #else
     53  1.10      maxv #define REPORT printf
     54  1.10      maxv #endif
     55  1.10      maxv 
     56   1.1      maxv /* ASAN constants. Part of the compiler ABI. */
     57   1.1      maxv #define KASAN_SHADOW_SCALE_SIZE		(1UL << KASAN_SHADOW_SCALE_SHIFT)
     58   1.1      maxv #define KASAN_SHADOW_MASK		(KASAN_SHADOW_SCALE_SIZE - 1)
     59  1.19      maxv #define KASAN_ALLOCA_SCALE_SIZE		32
     60   1.1      maxv 
     61   1.1      maxv /* The MD code. */
     62   1.1      maxv #include <machine/asan.h>
     63   1.1      maxv 
     64   1.1      maxv /* ASAN ABI version. */
     65   1.1      maxv #if defined(__clang__) && (__clang_major__ - 0 >= 6)
     66   1.1      maxv #define ASAN_ABI_VERSION	8
     67   1.1      maxv #elif __GNUC_PREREQ__(7, 1) && !defined(__clang__)
     68   1.1      maxv #define ASAN_ABI_VERSION	8
     69   1.1      maxv #elif __GNUC_PREREQ__(6, 1) && !defined(__clang__)
     70   1.1      maxv #define ASAN_ABI_VERSION	6
     71   1.1      maxv #else
     72   1.1      maxv #error "Unsupported compiler version"
     73   1.1      maxv #endif
     74   1.1      maxv 
     75   1.1      maxv #define __RET_ADDR	(unsigned long)__builtin_return_address(0)
     76   1.1      maxv 
     77   1.1      maxv /* Global variable descriptor. Part of the compiler ABI.  */
     78   1.1      maxv struct __asan_global_source_location {
     79   1.1      maxv 	const char *filename;
     80   1.1      maxv 	int line_no;
     81   1.1      maxv 	int column_no;
     82   1.1      maxv };
     83   1.1      maxv struct __asan_global {
     84   1.1      maxv 	const void *beg;		/* address of the global variable */
     85   1.1      maxv 	size_t size;			/* size of the global variable */
     86   1.1      maxv 	size_t size_with_redzone;	/* size with the redzone */
     87   1.1      maxv 	const void *name;		/* name of the variable */
     88   1.1      maxv 	const void *module_name;	/* name of the module where the var is declared */
     89   1.1      maxv 	unsigned long has_dynamic_init;	/* the var has dyn initializer (c++) */
     90   1.1      maxv 	struct __asan_global_source_location *location;
     91   1.1      maxv #if ASAN_ABI_VERSION >= 7
     92   1.1      maxv 	uintptr_t odr_indicator;	/* the address of the ODR indicator symbol */
     93   1.1      maxv #endif
     94   1.1      maxv };
     95   1.1      maxv 
     96   1.1      maxv static bool kasan_enabled __read_mostly = false;
     97   1.1      maxv 
     98   1.1      maxv /* -------------------------------------------------------------------------- */
     99   1.1      maxv 
    100   1.1      maxv void
    101   1.1      maxv kasan_shadow_map(void *addr, size_t size)
    102   1.1      maxv {
    103   1.1      maxv 	size_t sz, npages, i;
    104   1.1      maxv 	vaddr_t sva, eva;
    105   1.1      maxv 
    106   1.1      maxv 	KASSERT((vaddr_t)addr % KASAN_SHADOW_SCALE_SIZE == 0);
    107   1.1      maxv 
    108   1.1      maxv 	sz = roundup(size, KASAN_SHADOW_SCALE_SIZE) / KASAN_SHADOW_SCALE_SIZE;
    109   1.1      maxv 
    110   1.1      maxv 	sva = (vaddr_t)kasan_md_addr_to_shad(addr);
    111   1.1      maxv 	eva = (vaddr_t)kasan_md_addr_to_shad(addr) + sz;
    112   1.1      maxv 
    113   1.1      maxv 	sva = rounddown(sva, PAGE_SIZE);
    114   1.1      maxv 	eva = roundup(eva, PAGE_SIZE);
    115   1.1      maxv 
    116   1.1      maxv 	npages = (eva - sva) / PAGE_SIZE;
    117   1.1      maxv 
    118   1.1      maxv 	KASSERT(sva >= KASAN_MD_SHADOW_START && eva < KASAN_MD_SHADOW_END);
    119   1.1      maxv 
    120   1.1      maxv 	for (i = 0; i < npages; i++) {
    121   1.1      maxv 		kasan_md_shadow_map_page(sva + i * PAGE_SIZE);
    122   1.1      maxv 	}
    123   1.1      maxv }
    124   1.1      maxv 
    125   1.1      maxv static void
    126   1.1      maxv kasan_ctors(void)
    127   1.1      maxv {
    128  1.23     skrll 	extern Elf_Addr __CTOR_LIST__, __CTOR_END__;
    129   1.1      maxv 	size_t nentries, i;
    130  1.23     skrll 	Elf_Addr *ptr;
    131   1.1      maxv 
    132   1.1      maxv 	nentries = ((size_t)&__CTOR_END__ - (size_t)&__CTOR_LIST__) /
    133   1.1      maxv 	    sizeof(uintptr_t);
    134   1.1      maxv 
    135   1.1      maxv 	ptr = &__CTOR_LIST__;
    136   1.1      maxv 	for (i = 0; i < nentries; i++) {
    137   1.1      maxv 		void (*func)(void);
    138   1.1      maxv 
    139   1.1      maxv 		func = (void *)(*ptr);
    140   1.1      maxv 		(*func)();
    141   1.1      maxv 
    142   1.1      maxv 		ptr++;
    143   1.1      maxv 	}
    144   1.1      maxv }
    145   1.1      maxv 
    146   1.1      maxv void
    147   1.1      maxv kasan_early_init(void *stack)
    148   1.1      maxv {
    149   1.1      maxv 	kasan_md_early_init(stack);
    150   1.1      maxv }
    151   1.1      maxv 
    152   1.1      maxv void
    153   1.1      maxv kasan_init(void)
    154   1.1      maxv {
    155   1.1      maxv 	/* MD initialization. */
    156   1.1      maxv 	kasan_md_init();
    157   1.1      maxv 
    158   1.1      maxv 	/* Now officially enabled. */
    159   1.1      maxv 	kasan_enabled = true;
    160   1.1      maxv 
    161   1.1      maxv 	/* Call the ASAN constructors. */
    162   1.1      maxv 	kasan_ctors();
    163   1.1      maxv }
    164   1.1      maxv 
    165   1.5      maxv static inline const char *
    166   1.5      maxv kasan_code_name(uint8_t code)
    167   1.5      maxv {
    168   1.5      maxv 	switch (code) {
    169   1.6      maxv 	case KASAN_GENERIC_REDZONE:
    170   1.6      maxv 		return "GenericRedZone";
    171   1.6      maxv 	case KASAN_MALLOC_REDZONE:
    172   1.6      maxv 		return "MallocRedZone";
    173   1.6      maxv 	case KASAN_KMEM_REDZONE:
    174   1.6      maxv 		return "KmemRedZone";
    175   1.6      maxv 	case KASAN_POOL_REDZONE:
    176   1.6      maxv 		return "PoolRedZone";
    177   1.6      maxv 	case KASAN_POOL_FREED:
    178   1.6      maxv 		return "PoolUseAfterFree";
    179   1.5      maxv 	case 1 ... 7:
    180   1.5      maxv 		return "RedZonePartial";
    181   1.5      maxv 	case KASAN_STACK_LEFT:
    182   1.5      maxv 		return "StackLeft";
    183  1.18      maxv 	case KASAN_STACK_MID:
    184  1.18      maxv 		return "StackMiddle";
    185   1.5      maxv 	case KASAN_STACK_RIGHT:
    186   1.5      maxv 		return "StackRight";
    187  1.18      maxv 	case KASAN_USE_AFTER_RET:
    188  1.18      maxv 		return "UseAfterRet";
    189   1.5      maxv 	case KASAN_USE_AFTER_SCOPE:
    190   1.5      maxv 		return "UseAfterScope";
    191   1.5      maxv 	default:
    192   1.5      maxv 		return "Unknown";
    193   1.5      maxv 	}
    194   1.5      maxv }
    195   1.5      maxv 
    196   1.1      maxv static void
    197   1.5      maxv kasan_report(unsigned long addr, size_t size, bool write, unsigned long pc,
    198   1.5      maxv     uint8_t code)
    199   1.1      maxv {
    200  1.10      maxv 	REPORT("ASan: Unauthorized Access In %p: Addr %p [%zu byte%s, %s,"
    201   1.5      maxv 	    " %s]\n",
    202   1.1      maxv 	    (void *)pc, (void *)addr, size, (size > 1 ? "s" : ""),
    203   1.5      maxv 	    (write ? "write" : "read"), kasan_code_name(code));
    204   1.1      maxv 	kasan_md_unwind();
    205   1.1      maxv }
    206   1.1      maxv 
    207   1.1      maxv static __always_inline void
    208   1.1      maxv kasan_shadow_1byte_markvalid(unsigned long addr)
    209   1.1      maxv {
    210   1.1      maxv 	int8_t *byte = kasan_md_addr_to_shad((void *)addr);
    211   1.1      maxv 	int8_t last = (addr & KASAN_SHADOW_MASK) + 1;
    212   1.1      maxv 
    213   1.1      maxv 	*byte = last;
    214   1.1      maxv }
    215   1.1      maxv 
    216   1.1      maxv static __always_inline void
    217   1.4      maxv kasan_shadow_Nbyte_markvalid(const void *addr, size_t size)
    218   1.4      maxv {
    219   1.4      maxv 	size_t i;
    220   1.4      maxv 
    221   1.4      maxv 	for (i = 0; i < size; i++) {
    222   1.4      maxv 		kasan_shadow_1byte_markvalid((unsigned long)addr+i);
    223   1.4      maxv 	}
    224   1.4      maxv }
    225   1.4      maxv 
    226   1.4      maxv static __always_inline void
    227   1.6      maxv kasan_shadow_Nbyte_fill(const void *addr, size_t size, uint8_t code)
    228   1.1      maxv {
    229   1.1      maxv 	void *shad;
    230   1.1      maxv 
    231   1.1      maxv 	if (__predict_false(size == 0))
    232   1.1      maxv 		return;
    233   1.1      maxv 	if (__predict_false(kasan_md_unsupported((vaddr_t)addr)))
    234   1.1      maxv 		return;
    235   1.1      maxv 
    236   1.1      maxv 	KASSERT((vaddr_t)addr % KASAN_SHADOW_SCALE_SIZE == 0);
    237   1.1      maxv 	KASSERT(size % KASAN_SHADOW_SCALE_SIZE == 0);
    238   1.1      maxv 
    239   1.1      maxv 	shad = (void *)kasan_md_addr_to_shad(addr);
    240   1.1      maxv 	size = size >> KASAN_SHADOW_SCALE_SHIFT;
    241   1.1      maxv 
    242   1.6      maxv 	__builtin_memset(shad, code, size);
    243   1.1      maxv }
    244   1.1      maxv 
    245   1.1      maxv void
    246   1.1      maxv kasan_add_redzone(size_t *size)
    247   1.1      maxv {
    248   1.1      maxv 	*size = roundup(*size, KASAN_SHADOW_SCALE_SIZE);
    249   1.1      maxv 	*size += KASAN_SHADOW_SCALE_SIZE;
    250   1.1      maxv }
    251   1.1      maxv 
    252   1.1      maxv void
    253   1.1      maxv kasan_softint(struct lwp *l)
    254   1.1      maxv {
    255   1.1      maxv 	const void *stk = (const void *)uvm_lwp_getuarea(l);
    256   1.1      maxv 
    257   1.1      maxv 	kasan_shadow_Nbyte_fill(stk, USPACE, 0);
    258   1.1      maxv }
    259   1.1      maxv 
    260   1.2      maxv /*
    261   1.2      maxv  * In an area of size 'sz_with_redz', mark the 'size' first bytes as valid,
    262   1.2      maxv  * and the rest as invalid. There are generally two use cases:
    263   1.2      maxv  *
    264   1.6      maxv  *  o kasan_mark(addr, origsize, size, code), with origsize < size. This marks
    265   1.6      maxv  *    the redzone at the end of the buffer as invalid.
    266   1.2      maxv  *
    267   1.6      maxv  *  o kasan_mark(addr, size, size, 0). This marks the entire buffer as valid.
    268   1.2      maxv  */
    269   1.1      maxv void
    270   1.6      maxv kasan_mark(const void *addr, size_t size, size_t sz_with_redz, uint8_t code)
    271   1.1      maxv {
    272   1.9      maxv 	size_t i, n, redz;
    273   1.9      maxv 	int8_t *shad;
    274   1.9      maxv 
    275   1.9      maxv 	KASSERT((vaddr_t)addr % KASAN_SHADOW_SCALE_SIZE == 0);
    276   1.9      maxv 	redz = sz_with_redz - roundup(size, KASAN_SHADOW_SCALE_SIZE);
    277   1.9      maxv 	KASSERT(redz % KASAN_SHADOW_SCALE_SIZE == 0);
    278   1.9      maxv 	shad = kasan_md_addr_to_shad(addr);
    279   1.9      maxv 
    280   1.9      maxv 	/* Chunks of 8 bytes, valid. */
    281   1.9      maxv 	n = size / KASAN_SHADOW_SCALE_SIZE;
    282   1.9      maxv 	for (i = 0; i < n; i++) {
    283   1.9      maxv 		*shad++ = 0;
    284   1.9      maxv 	}
    285   1.9      maxv 
    286   1.9      maxv 	/* Possibly one chunk, mid. */
    287   1.9      maxv 	if ((size & KASAN_SHADOW_MASK) != 0) {
    288   1.9      maxv 		*shad++ = (size & KASAN_SHADOW_MASK);
    289   1.9      maxv 	}
    290   1.9      maxv 
    291   1.9      maxv 	/* Chunks of 8 bytes, invalid. */
    292   1.9      maxv 	n = redz / KASAN_SHADOW_SCALE_SIZE;
    293   1.9      maxv 	for (i = 0; i < n; i++) {
    294   1.9      maxv 		*shad++ = code;
    295   1.9      maxv 	}
    296   1.1      maxv }
    297   1.1      maxv 
    298   1.1      maxv /* -------------------------------------------------------------------------- */
    299   1.1      maxv 
    300   1.1      maxv #define ADDR_CROSSES_SCALE_BOUNDARY(addr, size) 		\
    301   1.1      maxv 	(addr >> KASAN_SHADOW_SCALE_SHIFT) !=			\
    302   1.1      maxv 	    ((addr + size - 1) >> KASAN_SHADOW_SCALE_SHIFT)
    303   1.1      maxv 
    304   1.1      maxv static __always_inline bool
    305   1.5      maxv kasan_shadow_1byte_isvalid(unsigned long addr, uint8_t *code)
    306   1.1      maxv {
    307   1.1      maxv 	int8_t *byte = kasan_md_addr_to_shad((void *)addr);
    308   1.1      maxv 	int8_t last = (addr & KASAN_SHADOW_MASK) + 1;
    309   1.1      maxv 
    310   1.5      maxv 	if (__predict_true(*byte == 0 || last <= *byte)) {
    311   1.5      maxv 		return true;
    312   1.5      maxv 	}
    313   1.5      maxv 	*code = *byte;
    314   1.5      maxv 	return false;
    315   1.1      maxv }
    316   1.1      maxv 
    317   1.1      maxv static __always_inline bool
    318   1.5      maxv kasan_shadow_2byte_isvalid(unsigned long addr, uint8_t *code)
    319   1.1      maxv {
    320   1.1      maxv 	int8_t *byte, last;
    321   1.1      maxv 
    322   1.1      maxv 	if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 2)) {
    323   1.5      maxv 		return (kasan_shadow_1byte_isvalid(addr, code) &&
    324   1.5      maxv 		    kasan_shadow_1byte_isvalid(addr+1, code));
    325   1.1      maxv 	}
    326   1.1      maxv 
    327   1.1      maxv 	byte = kasan_md_addr_to_shad((void *)addr);
    328   1.1      maxv 	last = ((addr + 1) & KASAN_SHADOW_MASK) + 1;
    329   1.1      maxv 
    330   1.5      maxv 	if (__predict_true(*byte == 0 || last <= *byte)) {
    331   1.5      maxv 		return true;
    332   1.5      maxv 	}
    333   1.5      maxv 	*code = *byte;
    334   1.5      maxv 	return false;
    335   1.1      maxv }
    336   1.1      maxv 
    337   1.1      maxv static __always_inline bool
    338   1.5      maxv kasan_shadow_4byte_isvalid(unsigned long addr, uint8_t *code)
    339   1.1      maxv {
    340   1.1      maxv 	int8_t *byte, last;
    341   1.1      maxv 
    342   1.1      maxv 	if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 4)) {
    343   1.5      maxv 		return (kasan_shadow_2byte_isvalid(addr, code) &&
    344   1.5      maxv 		    kasan_shadow_2byte_isvalid(addr+2, code));
    345   1.1      maxv 	}
    346   1.1      maxv 
    347   1.1      maxv 	byte = kasan_md_addr_to_shad((void *)addr);
    348   1.1      maxv 	last = ((addr + 3) & KASAN_SHADOW_MASK) + 1;
    349   1.1      maxv 
    350   1.5      maxv 	if (__predict_true(*byte == 0 || last <= *byte)) {
    351   1.5      maxv 		return true;
    352   1.5      maxv 	}
    353   1.5      maxv 	*code = *byte;
    354   1.5      maxv 	return false;
    355   1.1      maxv }
    356   1.1      maxv 
    357   1.1      maxv static __always_inline bool
    358   1.5      maxv kasan_shadow_8byte_isvalid(unsigned long addr, uint8_t *code)
    359   1.1      maxv {
    360   1.1      maxv 	int8_t *byte, last;
    361   1.1      maxv 
    362   1.1      maxv 	if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 8)) {
    363   1.5      maxv 		return (kasan_shadow_4byte_isvalid(addr, code) &&
    364   1.5      maxv 		    kasan_shadow_4byte_isvalid(addr+4, code));
    365   1.1      maxv 	}
    366   1.1      maxv 
    367   1.1      maxv 	byte = kasan_md_addr_to_shad((void *)addr);
    368   1.1      maxv 	last = ((addr + 7) & KASAN_SHADOW_MASK) + 1;
    369   1.1      maxv 
    370   1.5      maxv 	if (__predict_true(*byte == 0 || last <= *byte)) {
    371   1.5      maxv 		return true;
    372   1.5      maxv 	}
    373   1.5      maxv 	*code = *byte;
    374   1.5      maxv 	return false;
    375   1.1      maxv }
    376   1.1      maxv 
    377   1.1      maxv static __always_inline bool
    378   1.5      maxv kasan_shadow_Nbyte_isvalid(unsigned long addr, size_t size, uint8_t *code)
    379   1.1      maxv {
    380   1.1      maxv 	size_t i;
    381   1.1      maxv 
    382   1.1      maxv 	for (i = 0; i < size; i++) {
    383   1.5      maxv 		if (!kasan_shadow_1byte_isvalid(addr+i, code))
    384   1.1      maxv 			return false;
    385   1.1      maxv 	}
    386   1.1      maxv 
    387   1.1      maxv 	return true;
    388   1.1      maxv }
    389   1.1      maxv 
    390   1.1      maxv static __always_inline void
    391   1.1      maxv kasan_shadow_check(unsigned long addr, size_t size, bool write,
    392   1.1      maxv     unsigned long retaddr)
    393   1.1      maxv {
    394   1.5      maxv 	uint8_t code;
    395   1.1      maxv 	bool valid;
    396   1.1      maxv 
    397   1.1      maxv 	if (__predict_false(!kasan_enabled))
    398   1.1      maxv 		return;
    399  1.27    martin #ifdef DDB
    400  1.27    martin 	if (__predict_false(db_recover != NULL))
    401  1.27    martin 		return;
    402  1.27    martin #endif
    403   1.1      maxv 	if (__predict_false(size == 0))
    404   1.1      maxv 		return;
    405   1.1      maxv 	if (__predict_false(kasan_md_unsupported(addr)))
    406   1.1      maxv 		return;
    407   1.1      maxv 
    408   1.1      maxv 	if (__builtin_constant_p(size)) {
    409   1.1      maxv 		switch (size) {
    410   1.1      maxv 		case 1:
    411   1.5      maxv 			valid = kasan_shadow_1byte_isvalid(addr, &code);
    412   1.1      maxv 			break;
    413   1.1      maxv 		case 2:
    414   1.5      maxv 			valid = kasan_shadow_2byte_isvalid(addr, &code);
    415   1.1      maxv 			break;
    416   1.1      maxv 		case 4:
    417   1.5      maxv 			valid = kasan_shadow_4byte_isvalid(addr, &code);
    418   1.1      maxv 			break;
    419   1.1      maxv 		case 8:
    420   1.5      maxv 			valid = kasan_shadow_8byte_isvalid(addr, &code);
    421   1.1      maxv 			break;
    422   1.1      maxv 		default:
    423   1.5      maxv 			valid = kasan_shadow_Nbyte_isvalid(addr, size, &code);
    424   1.1      maxv 			break;
    425   1.1      maxv 		}
    426   1.1      maxv 	} else {
    427   1.5      maxv 		valid = kasan_shadow_Nbyte_isvalid(addr, size, &code);
    428   1.1      maxv 	}
    429   1.1      maxv 
    430   1.1      maxv 	if (__predict_false(!valid)) {
    431   1.5      maxv 		kasan_report(addr, size, write, retaddr, code);
    432   1.1      maxv 	}
    433   1.1      maxv }
    434   1.1      maxv 
    435   1.1      maxv /* -------------------------------------------------------------------------- */
    436   1.1      maxv 
    437   1.1      maxv void *
    438   1.1      maxv kasan_memcpy(void *dst, const void *src, size_t len)
    439   1.1      maxv {
    440   1.1      maxv 	kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR);
    441   1.1      maxv 	kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR);
    442   1.1      maxv 	return __builtin_memcpy(dst, src, len);
    443   1.1      maxv }
    444   1.1      maxv 
    445   1.1      maxv int
    446   1.1      maxv kasan_memcmp(const void *b1, const void *b2, size_t len)
    447   1.1      maxv {
    448   1.1      maxv 	kasan_shadow_check((unsigned long)b1, len, false, __RET_ADDR);
    449   1.1      maxv 	kasan_shadow_check((unsigned long)b2, len, false, __RET_ADDR);
    450   1.1      maxv 	return __builtin_memcmp(b1, b2, len);
    451   1.1      maxv }
    452   1.1      maxv 
    453   1.1      maxv void *
    454   1.1      maxv kasan_memset(void *b, int c, size_t len)
    455   1.1      maxv {
    456   1.1      maxv 	kasan_shadow_check((unsigned long)b, len, true, __RET_ADDR);
    457   1.1      maxv 	return __builtin_memset(b, c, len);
    458   1.1      maxv }
    459   1.1      maxv 
    460  1.12      maxv void *
    461  1.12      maxv kasan_memmove(void *dst, const void *src, size_t len)
    462  1.12      maxv {
    463  1.12      maxv 	kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR);
    464  1.12      maxv 	kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR);
    465  1.12      maxv 	return __builtin_memmove(dst, src, len);
    466  1.12      maxv }
    467  1.12      maxv 
    468   1.1      maxv char *
    469   1.1      maxv kasan_strcpy(char *dst, const char *src)
    470   1.1      maxv {
    471   1.1      maxv 	char *save = dst;
    472   1.1      maxv 
    473   1.1      maxv 	while (1) {
    474   1.1      maxv 		kasan_shadow_check((unsigned long)src, 1, false, __RET_ADDR);
    475   1.1      maxv 		kasan_shadow_check((unsigned long)dst, 1, true, __RET_ADDR);
    476   1.1      maxv 		*dst = *src;
    477   1.1      maxv 		if (*src == '\0')
    478   1.1      maxv 			break;
    479   1.1      maxv 		src++, dst++;
    480   1.1      maxv 	}
    481   1.1      maxv 
    482   1.1      maxv 	return save;
    483   1.1      maxv }
    484   1.1      maxv 
    485   1.1      maxv int
    486   1.1      maxv kasan_strcmp(const char *s1, const char *s2)
    487   1.1      maxv {
    488   1.1      maxv 	while (1) {
    489   1.1      maxv 		kasan_shadow_check((unsigned long)s1, 1, false, __RET_ADDR);
    490   1.1      maxv 		kasan_shadow_check((unsigned long)s2, 1, false, __RET_ADDR);
    491   1.1      maxv 		if (*s1 != *s2)
    492   1.1      maxv 			break;
    493   1.1      maxv 		if (*s1 == '\0')
    494   1.1      maxv 			return 0;
    495   1.1      maxv 		s1++, s2++;
    496   1.1      maxv 	}
    497   1.1      maxv 
    498   1.1      maxv 	return (*(const unsigned char *)s1 - *(const unsigned char *)s2);
    499   1.1      maxv }
    500   1.1      maxv 
    501   1.1      maxv size_t
    502   1.1      maxv kasan_strlen(const char *str)
    503   1.1      maxv {
    504   1.1      maxv 	const char *s;
    505   1.1      maxv 
    506   1.1      maxv 	s = str;
    507   1.1      maxv 	while (1) {
    508   1.1      maxv 		kasan_shadow_check((unsigned long)s, 1, false, __RET_ADDR);
    509   1.1      maxv 		if (*s == '\0')
    510   1.1      maxv 			break;
    511   1.1      maxv 		s++;
    512   1.1      maxv 	}
    513   1.1      maxv 
    514   1.1      maxv 	return (s - str);
    515   1.1      maxv }
    516   1.1      maxv 
    517  1.20      maxv char *
    518  1.20      maxv kasan_strcat(char *dst, const char *src)
    519  1.20      maxv {
    520  1.20      maxv 	size_t ldst, lsrc;
    521  1.20      maxv 
    522  1.20      maxv 	ldst = __builtin_strlen(dst);
    523  1.20      maxv 	lsrc = __builtin_strlen(src);
    524  1.20      maxv 	kasan_shadow_check((unsigned long)dst, ldst + lsrc + 1, true,
    525  1.20      maxv 	    __RET_ADDR);
    526  1.20      maxv 	kasan_shadow_check((unsigned long)src, lsrc + 1, false,
    527  1.20      maxv 	    __RET_ADDR);
    528  1.20      maxv 
    529  1.20      maxv 	return __builtin_strcat(dst, src);
    530  1.20      maxv }
    531  1.20      maxv 
    532  1.20      maxv char *
    533  1.20      maxv kasan_strchr(const char *s, int c)
    534  1.20      maxv {
    535  1.20      maxv 	kasan_shadow_check((unsigned long)s, __builtin_strlen(s) + 1, false,
    536  1.20      maxv 	    __RET_ADDR);
    537  1.20      maxv 	return __builtin_strchr(s, c);
    538  1.20      maxv }
    539  1.20      maxv 
    540  1.20      maxv char *
    541  1.20      maxv kasan_strrchr(const char *s, int c)
    542  1.20      maxv {
    543  1.20      maxv 	kasan_shadow_check((unsigned long)s, __builtin_strlen(s) + 1, false,
    544  1.20      maxv 	    __RET_ADDR);
    545  1.20      maxv 	return __builtin_strrchr(s, c);
    546  1.20      maxv }
    547  1.20      maxv 
    548   1.8      maxv #undef kcopy
    549   1.7      maxv #undef copyinstr
    550   1.7      maxv #undef copyoutstr
    551   1.7      maxv #undef copyin
    552   1.7      maxv 
    553   1.8      maxv int	kasan_kcopy(const void *, void *, size_t);
    554   1.7      maxv int	kasan_copyinstr(const void *, void *, size_t, size_t *);
    555   1.7      maxv int	kasan_copyoutstr(const void *, void *, size_t, size_t *);
    556   1.7      maxv int	kasan_copyin(const void *, void *, size_t);
    557   1.8      maxv int	kcopy(const void *, void *, size_t);
    558   1.7      maxv int	copyinstr(const void *, void *, size_t, size_t *);
    559   1.7      maxv int	copyoutstr(const void *, void *, size_t, size_t *);
    560   1.7      maxv int	copyin(const void *, void *, size_t);
    561   1.7      maxv 
    562   1.7      maxv int
    563   1.8      maxv kasan_kcopy(const void *src, void *dst, size_t len)
    564   1.8      maxv {
    565   1.8      maxv 	kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR);
    566   1.8      maxv 	kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR);
    567   1.8      maxv 	return kcopy(src, dst, len);
    568   1.8      maxv }
    569   1.8      maxv 
    570   1.8      maxv int
    571   1.7      maxv kasan_copyin(const void *uaddr, void *kaddr, size_t len)
    572   1.7      maxv {
    573   1.7      maxv 	kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR);
    574   1.7      maxv 	return copyin(uaddr, kaddr, len);
    575   1.7      maxv }
    576   1.7      maxv 
    577   1.7      maxv int
    578   1.7      maxv kasan_copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
    579   1.7      maxv {
    580   1.7      maxv 	kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR);
    581   1.7      maxv 	return copyinstr(uaddr, kaddr, len, done);
    582   1.7      maxv }
    583   1.7      maxv 
    584   1.7      maxv int
    585   1.7      maxv kasan_copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done)
    586   1.7      maxv {
    587   1.7      maxv 	kasan_shadow_check((unsigned long)kaddr, len, false, __RET_ADDR);
    588   1.7      maxv 	return copyoutstr(kaddr, uaddr, len, done);
    589   1.7      maxv }
    590   1.7      maxv 
    591   1.1      maxv /* -------------------------------------------------------------------------- */
    592   1.1      maxv 
    593  1.16      maxv #undef _ucas_32
    594  1.16      maxv #undef _ucas_32_mp
    595  1.16      maxv #undef _ucas_64
    596  1.16      maxv #undef _ucas_64_mp
    597  1.16      maxv #undef _ufetch_8
    598  1.16      maxv #undef _ufetch_16
    599  1.16      maxv #undef _ufetch_32
    600  1.16      maxv #undef _ufetch_64
    601  1.16      maxv 
    602  1.16      maxv int _ucas_32(volatile uint32_t *, uint32_t, uint32_t, uint32_t *);
    603  1.16      maxv int kasan__ucas_32(volatile uint32_t *, uint32_t, uint32_t, uint32_t *);
    604  1.16      maxv int
    605  1.16      maxv kasan__ucas_32(volatile uint32_t *uaddr, uint32_t old, uint32_t new,
    606  1.16      maxv     uint32_t *ret)
    607  1.16      maxv {
    608  1.16      maxv 	kasan_shadow_check((unsigned long)ret, sizeof(*ret), true,
    609  1.16      maxv 	    __RET_ADDR);
    610  1.16      maxv 	return _ucas_32(uaddr, old, new, ret);
    611  1.16      maxv }
    612  1.16      maxv 
    613  1.16      maxv #ifdef __HAVE_UCAS_MP
    614  1.16      maxv int _ucas_32_mp(volatile uint32_t *, uint32_t, uint32_t, uint32_t *);
    615  1.16      maxv int kasan__ucas_32_mp(volatile uint32_t *, uint32_t, uint32_t, uint32_t *);
    616  1.16      maxv int
    617  1.16      maxv kasan__ucas_32_mp(volatile uint32_t *uaddr, uint32_t old, uint32_t new,
    618  1.16      maxv     uint32_t *ret)
    619  1.16      maxv {
    620  1.16      maxv 	kasan_shadow_check((unsigned long)ret, sizeof(*ret), true,
    621  1.16      maxv 	    __RET_ADDR);
    622  1.16      maxv 	return _ucas_32_mp(uaddr, old, new, ret);
    623  1.16      maxv }
    624  1.16      maxv #endif
    625  1.16      maxv 
    626  1.16      maxv #ifdef _LP64
    627  1.16      maxv int _ucas_64(volatile uint64_t *, uint64_t, uint64_t, uint64_t *);
    628  1.16      maxv int kasan__ucas_64(volatile uint64_t *, uint64_t, uint64_t, uint64_t *);
    629  1.16      maxv int
    630  1.16      maxv kasan__ucas_64(volatile uint64_t *uaddr, uint64_t old, uint64_t new,
    631  1.16      maxv     uint64_t *ret)
    632  1.16      maxv {
    633  1.16      maxv 	kasan_shadow_check((unsigned long)ret, sizeof(*ret), true,
    634  1.16      maxv 	    __RET_ADDR);
    635  1.16      maxv 	return _ucas_64(uaddr, old, new, ret);
    636  1.16      maxv }
    637  1.16      maxv 
    638  1.16      maxv #ifdef __HAVE_UCAS_MP
    639  1.16      maxv int _ucas_64_mp(volatile uint64_t *, uint64_t, uint64_t, uint64_t *);
    640  1.16      maxv int kasan__ucas_64_mp(volatile uint64_t *, uint64_t, uint64_t, uint64_t *);
    641  1.16      maxv int
    642  1.16      maxv kasan__ucas_64_mp(volatile uint64_t *uaddr, uint64_t old, uint64_t new,
    643  1.16      maxv     uint64_t *ret)
    644  1.16      maxv {
    645  1.16      maxv 	kasan_shadow_check((unsigned long)ret, sizeof(*ret), true,
    646  1.16      maxv 	    __RET_ADDR);
    647  1.16      maxv 	return _ucas_64_mp(uaddr, old, new, ret);
    648  1.16      maxv }
    649  1.16      maxv #endif
    650  1.16      maxv #endif
    651  1.16      maxv 
    652  1.16      maxv int _ufetch_8(const uint8_t *, uint8_t *);
    653  1.16      maxv int kasan__ufetch_8(const uint8_t *, uint8_t *);
    654  1.16      maxv int
    655  1.16      maxv kasan__ufetch_8(const uint8_t *uaddr, uint8_t *valp)
    656  1.16      maxv {
    657  1.16      maxv 	kasan_shadow_check((unsigned long)valp, sizeof(*valp), true,
    658  1.16      maxv 	    __RET_ADDR);
    659  1.16      maxv 	return _ufetch_8(uaddr, valp);
    660  1.16      maxv }
    661  1.16      maxv 
    662  1.16      maxv int _ufetch_16(const uint16_t *, uint16_t *);
    663  1.16      maxv int kasan__ufetch_16(const uint16_t *, uint16_t *);
    664  1.16      maxv int
    665  1.16      maxv kasan__ufetch_16(const uint16_t *uaddr, uint16_t *valp)
    666  1.16      maxv {
    667  1.16      maxv 	kasan_shadow_check((unsigned long)valp, sizeof(*valp), true,
    668  1.16      maxv 	    __RET_ADDR);
    669  1.16      maxv 	return _ufetch_16(uaddr, valp);
    670  1.16      maxv }
    671  1.16      maxv 
    672  1.16      maxv int _ufetch_32(const uint32_t *, uint32_t *);
    673  1.16      maxv int kasan__ufetch_32(const uint32_t *, uint32_t *);
    674  1.16      maxv int
    675  1.16      maxv kasan__ufetch_32(const uint32_t *uaddr, uint32_t *valp)
    676  1.16      maxv {
    677  1.16      maxv 	kasan_shadow_check((unsigned long)valp, sizeof(*valp), true,
    678  1.16      maxv 	    __RET_ADDR);
    679  1.16      maxv 	return _ufetch_32(uaddr, valp);
    680  1.16      maxv }
    681  1.16      maxv 
    682  1.16      maxv #ifdef _LP64
    683  1.16      maxv int _ufetch_64(const uint64_t *, uint64_t *);
    684  1.16      maxv int kasan__ufetch_64(const uint64_t *, uint64_t *);
    685  1.16      maxv int
    686  1.16      maxv kasan__ufetch_64(const uint64_t *uaddr, uint64_t *valp)
    687  1.16      maxv {
    688  1.16      maxv 	kasan_shadow_check((unsigned long)valp, sizeof(*valp), true,
    689  1.16      maxv 	    __RET_ADDR);
    690  1.16      maxv 	return _ufetch_64(uaddr, valp);
    691  1.16      maxv }
    692  1.16      maxv #endif
    693  1.16      maxv 
    694  1.16      maxv /* -------------------------------------------------------------------------- */
    695  1.16      maxv 
    696  1.11      maxv #undef atomic_add_32
    697  1.11      maxv #undef atomic_add_int
    698  1.11      maxv #undef atomic_add_long
    699  1.11      maxv #undef atomic_add_ptr
    700  1.11      maxv #undef atomic_add_64
    701  1.11      maxv #undef atomic_add_32_nv
    702  1.11      maxv #undef atomic_add_int_nv
    703  1.11      maxv #undef atomic_add_long_nv
    704  1.11      maxv #undef atomic_add_ptr_nv
    705  1.11      maxv #undef atomic_add_64_nv
    706  1.11      maxv #undef atomic_and_32
    707  1.11      maxv #undef atomic_and_uint
    708  1.11      maxv #undef atomic_and_ulong
    709  1.11      maxv #undef atomic_and_64
    710  1.11      maxv #undef atomic_and_32_nv
    711  1.11      maxv #undef atomic_and_uint_nv
    712  1.11      maxv #undef atomic_and_ulong_nv
    713  1.11      maxv #undef atomic_and_64_nv
    714  1.11      maxv #undef atomic_or_32
    715  1.11      maxv #undef atomic_or_uint
    716  1.11      maxv #undef atomic_or_ulong
    717  1.11      maxv #undef atomic_or_64
    718  1.11      maxv #undef atomic_or_32_nv
    719  1.11      maxv #undef atomic_or_uint_nv
    720  1.11      maxv #undef atomic_or_ulong_nv
    721  1.11      maxv #undef atomic_or_64_nv
    722  1.11      maxv #undef atomic_cas_32
    723  1.11      maxv #undef atomic_cas_uint
    724  1.11      maxv #undef atomic_cas_ulong
    725  1.11      maxv #undef atomic_cas_ptr
    726  1.11      maxv #undef atomic_cas_64
    727  1.11      maxv #undef atomic_cas_32_ni
    728  1.11      maxv #undef atomic_cas_uint_ni
    729  1.11      maxv #undef atomic_cas_ulong_ni
    730  1.11      maxv #undef atomic_cas_ptr_ni
    731  1.11      maxv #undef atomic_cas_64_ni
    732  1.11      maxv #undef atomic_swap_32
    733  1.11      maxv #undef atomic_swap_uint
    734  1.11      maxv #undef atomic_swap_ulong
    735  1.11      maxv #undef atomic_swap_ptr
    736  1.11      maxv #undef atomic_swap_64
    737  1.11      maxv #undef atomic_dec_32
    738  1.11      maxv #undef atomic_dec_uint
    739  1.11      maxv #undef atomic_dec_ulong
    740  1.11      maxv #undef atomic_dec_ptr
    741  1.11      maxv #undef atomic_dec_64
    742  1.11      maxv #undef atomic_dec_32_nv
    743  1.11      maxv #undef atomic_dec_uint_nv
    744  1.11      maxv #undef atomic_dec_ulong_nv
    745  1.11      maxv #undef atomic_dec_ptr_nv
    746  1.11      maxv #undef atomic_dec_64_nv
    747  1.11      maxv #undef atomic_inc_32
    748  1.11      maxv #undef atomic_inc_uint
    749  1.11      maxv #undef atomic_inc_ulong
    750  1.11      maxv #undef atomic_inc_ptr
    751  1.11      maxv #undef atomic_inc_64
    752  1.11      maxv #undef atomic_inc_32_nv
    753  1.11      maxv #undef atomic_inc_uint_nv
    754  1.11      maxv #undef atomic_inc_ulong_nv
    755  1.11      maxv #undef atomic_inc_ptr_nv
    756  1.11      maxv #undef atomic_inc_64_nv
    757  1.11      maxv 
    758  1.11      maxv #define ASAN_ATOMIC_FUNC_ADD(name, tret, targ1, targ2) \
    759  1.11      maxv 	void atomic_add_##name(volatile targ1 *, targ2); \
    760  1.11      maxv 	void kasan_atomic_add_##name(volatile targ1 *, targ2); \
    761  1.11      maxv 	void kasan_atomic_add_##name(volatile targ1 *ptr, targ2 val) \
    762  1.11      maxv 	{ \
    763  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    764  1.11      maxv 		    __RET_ADDR); \
    765  1.11      maxv 		atomic_add_##name(ptr, val); \
    766  1.11      maxv 	} \
    767  1.11      maxv 	tret atomic_add_##name##_nv(volatile targ1 *, targ2); \
    768  1.11      maxv 	tret kasan_atomic_add_##name##_nv(volatile targ1 *, targ2); \
    769  1.11      maxv 	tret kasan_atomic_add_##name##_nv(volatile targ1 *ptr, targ2 val) \
    770  1.11      maxv 	{ \
    771  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    772  1.11      maxv 		    __RET_ADDR); \
    773  1.11      maxv 		return atomic_add_##name##_nv(ptr, val); \
    774  1.11      maxv 	}
    775  1.11      maxv 
    776  1.11      maxv #define ASAN_ATOMIC_FUNC_AND(name, tret, targ1, targ2) \
    777  1.11      maxv 	void atomic_and_##name(volatile targ1 *, targ2); \
    778  1.11      maxv 	void kasan_atomic_and_##name(volatile targ1 *, targ2); \
    779  1.11      maxv 	void kasan_atomic_and_##name(volatile targ1 *ptr, targ2 val) \
    780  1.11      maxv 	{ \
    781  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    782  1.11      maxv 		    __RET_ADDR); \
    783  1.11      maxv 		atomic_and_##name(ptr, val); \
    784  1.11      maxv 	} \
    785  1.11      maxv 	tret atomic_and_##name##_nv(volatile targ1 *, targ2); \
    786  1.11      maxv 	tret kasan_atomic_and_##name##_nv(volatile targ1 *, targ2); \
    787  1.11      maxv 	tret kasan_atomic_and_##name##_nv(volatile targ1 *ptr, targ2 val) \
    788  1.11      maxv 	{ \
    789  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    790  1.11      maxv 		    __RET_ADDR); \
    791  1.11      maxv 		return atomic_and_##name##_nv(ptr, val); \
    792  1.11      maxv 	}
    793  1.11      maxv 
    794  1.11      maxv #define ASAN_ATOMIC_FUNC_OR(name, tret, targ1, targ2) \
    795  1.11      maxv 	void atomic_or_##name(volatile targ1 *, targ2); \
    796  1.11      maxv 	void kasan_atomic_or_##name(volatile targ1 *, targ2); \
    797  1.11      maxv 	void kasan_atomic_or_##name(volatile targ1 *ptr, targ2 val) \
    798  1.11      maxv 	{ \
    799  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    800  1.11      maxv 		    __RET_ADDR); \
    801  1.11      maxv 		atomic_or_##name(ptr, val); \
    802  1.11      maxv 	} \
    803  1.11      maxv 	tret atomic_or_##name##_nv(volatile targ1 *, targ2); \
    804  1.11      maxv 	tret kasan_atomic_or_##name##_nv(volatile targ1 *, targ2); \
    805  1.11      maxv 	tret kasan_atomic_or_##name##_nv(volatile targ1 *ptr, targ2 val) \
    806  1.11      maxv 	{ \
    807  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    808  1.11      maxv 		    __RET_ADDR); \
    809  1.11      maxv 		return atomic_or_##name##_nv(ptr, val); \
    810  1.11      maxv 	}
    811  1.11      maxv 
    812  1.11      maxv #define ASAN_ATOMIC_FUNC_CAS(name, tret, targ1, targ2) \
    813  1.11      maxv 	tret atomic_cas_##name(volatile targ1 *, targ2, targ2); \
    814  1.11      maxv 	tret kasan_atomic_cas_##name(volatile targ1 *, targ2, targ2); \
    815  1.11      maxv 	tret kasan_atomic_cas_##name(volatile targ1 *ptr, targ2 exp, targ2 new) \
    816  1.11      maxv 	{ \
    817  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    818  1.11      maxv 		    __RET_ADDR); \
    819  1.11      maxv 		return atomic_cas_##name(ptr, exp, new); \
    820  1.11      maxv 	} \
    821  1.11      maxv 	tret atomic_cas_##name##_ni(volatile targ1 *, targ2, targ2); \
    822  1.11      maxv 	tret kasan_atomic_cas_##name##_ni(volatile targ1 *, targ2, targ2); \
    823  1.11      maxv 	tret kasan_atomic_cas_##name##_ni(volatile targ1 *ptr, targ2 exp, targ2 new) \
    824  1.11      maxv 	{ \
    825  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    826  1.11      maxv 		    __RET_ADDR); \
    827  1.11      maxv 		return atomic_cas_##name##_ni(ptr, exp, new); \
    828  1.11      maxv 	}
    829  1.11      maxv 
    830  1.11      maxv #define ASAN_ATOMIC_FUNC_SWAP(name, tret, targ1, targ2) \
    831  1.11      maxv 	tret atomic_swap_##name(volatile targ1 *, targ2); \
    832  1.11      maxv 	tret kasan_atomic_swap_##name(volatile targ1 *, targ2); \
    833  1.11      maxv 	tret kasan_atomic_swap_##name(volatile targ1 *ptr, targ2 val) \
    834  1.11      maxv 	{ \
    835  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    836  1.11      maxv 		    __RET_ADDR); \
    837  1.11      maxv 		return atomic_swap_##name(ptr, val); \
    838  1.11      maxv 	}
    839  1.11      maxv 
    840  1.11      maxv #define ASAN_ATOMIC_FUNC_DEC(name, tret, targ1) \
    841  1.11      maxv 	void atomic_dec_##name(volatile targ1 *); \
    842  1.11      maxv 	void kasan_atomic_dec_##name(volatile targ1 *); \
    843  1.11      maxv 	void kasan_atomic_dec_##name(volatile targ1 *ptr) \
    844  1.11      maxv 	{ \
    845  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    846  1.11      maxv 		    __RET_ADDR); \
    847  1.11      maxv 		atomic_dec_##name(ptr); \
    848  1.11      maxv 	} \
    849  1.11      maxv 	tret atomic_dec_##name##_nv(volatile targ1 *); \
    850  1.11      maxv 	tret kasan_atomic_dec_##name##_nv(volatile targ1 *); \
    851  1.11      maxv 	tret kasan_atomic_dec_##name##_nv(volatile targ1 *ptr) \
    852  1.11      maxv 	{ \
    853  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    854  1.11      maxv 		    __RET_ADDR); \
    855  1.11      maxv 		return atomic_dec_##name##_nv(ptr); \
    856  1.11      maxv 	}
    857  1.11      maxv 
    858  1.11      maxv #define ASAN_ATOMIC_FUNC_INC(name, tret, targ1) \
    859  1.11      maxv 	void atomic_inc_##name(volatile targ1 *); \
    860  1.11      maxv 	void kasan_atomic_inc_##name(volatile targ1 *); \
    861  1.11      maxv 	void kasan_atomic_inc_##name(volatile targ1 *ptr) \
    862  1.11      maxv 	{ \
    863  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    864  1.11      maxv 		    __RET_ADDR); \
    865  1.11      maxv 		atomic_inc_##name(ptr); \
    866  1.11      maxv 	} \
    867  1.11      maxv 	tret atomic_inc_##name##_nv(volatile targ1 *); \
    868  1.11      maxv 	tret kasan_atomic_inc_##name##_nv(volatile targ1 *); \
    869  1.11      maxv 	tret kasan_atomic_inc_##name##_nv(volatile targ1 *ptr) \
    870  1.11      maxv 	{ \
    871  1.11      maxv 		kasan_shadow_check((uintptr_t)ptr, sizeof(tret), true, \
    872  1.11      maxv 		    __RET_ADDR); \
    873  1.11      maxv 		return atomic_inc_##name##_nv(ptr); \
    874  1.11      maxv 	}
    875  1.11      maxv 
    876  1.11      maxv ASAN_ATOMIC_FUNC_ADD(32, uint32_t, uint32_t, int32_t);
    877  1.11      maxv ASAN_ATOMIC_FUNC_ADD(64, uint64_t, uint64_t, int64_t);
    878  1.11      maxv ASAN_ATOMIC_FUNC_ADD(int, unsigned int, unsigned int, int);
    879  1.11      maxv ASAN_ATOMIC_FUNC_ADD(long, unsigned long, unsigned long, long);
    880  1.11      maxv ASAN_ATOMIC_FUNC_ADD(ptr, void *, void, ssize_t);
    881  1.11      maxv 
    882  1.11      maxv ASAN_ATOMIC_FUNC_AND(32, uint32_t, uint32_t, uint32_t);
    883  1.11      maxv ASAN_ATOMIC_FUNC_AND(64, uint64_t, uint64_t, uint64_t);
    884  1.11      maxv ASAN_ATOMIC_FUNC_AND(uint, unsigned int, unsigned int, unsigned int);
    885  1.11      maxv ASAN_ATOMIC_FUNC_AND(ulong, unsigned long, unsigned long, unsigned long);
    886  1.11      maxv 
    887  1.11      maxv ASAN_ATOMIC_FUNC_OR(32, uint32_t, uint32_t, uint32_t);
    888  1.11      maxv ASAN_ATOMIC_FUNC_OR(64, uint64_t, uint64_t, uint64_t);
    889  1.11      maxv ASAN_ATOMIC_FUNC_OR(uint, unsigned int, unsigned int, unsigned int);
    890  1.11      maxv ASAN_ATOMIC_FUNC_OR(ulong, unsigned long, unsigned long, unsigned long);
    891  1.11      maxv 
    892  1.11      maxv ASAN_ATOMIC_FUNC_CAS(32, uint32_t, uint32_t, uint32_t);
    893  1.11      maxv ASAN_ATOMIC_FUNC_CAS(64, uint64_t, uint64_t, uint64_t);
    894  1.11      maxv ASAN_ATOMIC_FUNC_CAS(uint, unsigned int, unsigned int, unsigned int);
    895  1.11      maxv ASAN_ATOMIC_FUNC_CAS(ulong, unsigned long, unsigned long, unsigned long);
    896  1.11      maxv ASAN_ATOMIC_FUNC_CAS(ptr, void *, void, void *);
    897  1.11      maxv 
    898  1.11      maxv ASAN_ATOMIC_FUNC_SWAP(32, uint32_t, uint32_t, uint32_t);
    899  1.11      maxv ASAN_ATOMIC_FUNC_SWAP(64, uint64_t, uint64_t, uint64_t);
    900  1.11      maxv ASAN_ATOMIC_FUNC_SWAP(uint, unsigned int, unsigned int, unsigned int);
    901  1.11      maxv ASAN_ATOMIC_FUNC_SWAP(ulong, unsigned long, unsigned long, unsigned long);
    902  1.11      maxv ASAN_ATOMIC_FUNC_SWAP(ptr, void *, void, void *);
    903  1.11      maxv 
    904  1.11      maxv ASAN_ATOMIC_FUNC_DEC(32, uint32_t, uint32_t)
    905  1.11      maxv ASAN_ATOMIC_FUNC_DEC(64, uint64_t, uint64_t)
    906  1.11      maxv ASAN_ATOMIC_FUNC_DEC(uint, unsigned int, unsigned int);
    907  1.11      maxv ASAN_ATOMIC_FUNC_DEC(ulong, unsigned long, unsigned long);
    908  1.11      maxv ASAN_ATOMIC_FUNC_DEC(ptr, void *, void);
    909  1.11      maxv 
    910  1.11      maxv ASAN_ATOMIC_FUNC_INC(32, uint32_t, uint32_t)
    911  1.11      maxv ASAN_ATOMIC_FUNC_INC(64, uint64_t, uint64_t)
    912  1.11      maxv ASAN_ATOMIC_FUNC_INC(uint, unsigned int, unsigned int);
    913  1.11      maxv ASAN_ATOMIC_FUNC_INC(ulong, unsigned long, unsigned long);
    914  1.11      maxv ASAN_ATOMIC_FUNC_INC(ptr, void *, void);
    915  1.11      maxv 
    916  1.11      maxv /* -------------------------------------------------------------------------- */
    917  1.11      maxv 
    918  1.14      maxv #ifdef __HAVE_KASAN_INSTR_BUS
    919  1.14      maxv 
    920  1.13      maxv #include <sys/bus.h>
    921  1.13      maxv 
    922  1.13      maxv #undef bus_space_read_multi_1
    923  1.13      maxv #undef bus_space_read_multi_2
    924  1.13      maxv #undef bus_space_read_multi_4
    925  1.13      maxv #undef bus_space_read_multi_8
    926  1.13      maxv #undef bus_space_read_multi_stream_1
    927  1.13      maxv #undef bus_space_read_multi_stream_2
    928  1.13      maxv #undef bus_space_read_multi_stream_4
    929  1.13      maxv #undef bus_space_read_multi_stream_8
    930  1.13      maxv #undef bus_space_read_region_1
    931  1.13      maxv #undef bus_space_read_region_2
    932  1.13      maxv #undef bus_space_read_region_4
    933  1.13      maxv #undef bus_space_read_region_8
    934  1.13      maxv #undef bus_space_read_region_stream_1
    935  1.13      maxv #undef bus_space_read_region_stream_2
    936  1.13      maxv #undef bus_space_read_region_stream_4
    937  1.13      maxv #undef bus_space_read_region_stream_8
    938  1.13      maxv #undef bus_space_write_multi_1
    939  1.13      maxv #undef bus_space_write_multi_2
    940  1.13      maxv #undef bus_space_write_multi_4
    941  1.13      maxv #undef bus_space_write_multi_8
    942  1.13      maxv #undef bus_space_write_multi_stream_1
    943  1.13      maxv #undef bus_space_write_multi_stream_2
    944  1.13      maxv #undef bus_space_write_multi_stream_4
    945  1.13      maxv #undef bus_space_write_multi_stream_8
    946  1.13      maxv #undef bus_space_write_region_1
    947  1.13      maxv #undef bus_space_write_region_2
    948  1.13      maxv #undef bus_space_write_region_4
    949  1.13      maxv #undef bus_space_write_region_8
    950  1.13      maxv #undef bus_space_write_region_stream_1
    951  1.13      maxv #undef bus_space_write_region_stream_2
    952  1.13      maxv #undef bus_space_write_region_stream_4
    953  1.13      maxv #undef bus_space_write_region_stream_8
    954  1.13      maxv 
    955  1.13      maxv #define ASAN_BUS_READ_FUNC(bytes, bits) \
    956  1.13      maxv 	void bus_space_read_multi_##bytes(bus_space_tag_t, bus_space_handle_t,	\
    957  1.13      maxv 	    bus_size_t, uint##bits##_t *, bus_size_t);				\
    958  1.13      maxv 	void kasan_bus_space_read_multi_##bytes(bus_space_tag_t,		\
    959  1.13      maxv 	    bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t);	\
    960  1.13      maxv 	void kasan_bus_space_read_multi_##bytes(bus_space_tag_t tag,		\
    961  1.13      maxv 	    bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf,	\
    962  1.13      maxv 	    bus_size_t count)							\
    963  1.13      maxv 	{									\
    964  1.13      maxv 		kasan_shadow_check((uintptr_t)buf,				\
    965  1.13      maxv 		    sizeof(uint##bits##_t) * count, false, __RET_ADDR);		\
    966  1.13      maxv 		bus_space_read_multi_##bytes(tag, hnd, size, buf, count);	\
    967  1.13      maxv 	}									\
    968  1.13      maxv 	void bus_space_read_multi_stream_##bytes(bus_space_tag_t,		\
    969  1.13      maxv 	    bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t);	\
    970  1.13      maxv 	void kasan_bus_space_read_multi_stream_##bytes(bus_space_tag_t,		\
    971  1.13      maxv 	    bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t);	\
    972  1.13      maxv 	void kasan_bus_space_read_multi_stream_##bytes(bus_space_tag_t tag,	\
    973  1.13      maxv 	    bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf,	\
    974  1.13      maxv 	    bus_size_t count)							\
    975  1.13      maxv 	{									\
    976  1.13      maxv 		kasan_shadow_check((uintptr_t)buf,				\
    977  1.13      maxv 		    sizeof(uint##bits##_t) * count, false, __RET_ADDR);		\
    978  1.13      maxv 		bus_space_read_multi_stream_##bytes(tag, hnd, size, buf, count);\
    979  1.13      maxv 	}									\
    980  1.13      maxv 	void bus_space_read_region_##bytes(bus_space_tag_t, bus_space_handle_t,	\
    981  1.13      maxv 	    bus_size_t, uint##bits##_t *, bus_size_t);				\
    982  1.13      maxv 	void kasan_bus_space_read_region_##bytes(bus_space_tag_t,		\
    983  1.13      maxv 	    bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t);	\
    984  1.13      maxv 	void kasan_bus_space_read_region_##bytes(bus_space_tag_t tag,		\
    985  1.13      maxv 	    bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf,	\
    986  1.13      maxv 	    bus_size_t count)							\
    987  1.13      maxv 	{									\
    988  1.13      maxv 		kasan_shadow_check((uintptr_t)buf,				\
    989  1.13      maxv 		    sizeof(uint##bits##_t) * count, false, __RET_ADDR);		\
    990  1.13      maxv 		bus_space_read_region_##bytes(tag, hnd, size, buf, count);	\
    991  1.13      maxv 	}									\
    992  1.13      maxv 	void bus_space_read_region_stream_##bytes(bus_space_tag_t,		\
    993  1.13      maxv 	    bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t);	\
    994  1.13      maxv 	void kasan_bus_space_read_region_stream_##bytes(bus_space_tag_t,	\
    995  1.13      maxv 	    bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t);	\
    996  1.13      maxv 	void kasan_bus_space_read_region_stream_##bytes(bus_space_tag_t tag,	\
    997  1.13      maxv 	    bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf,	\
    998  1.13      maxv 	    bus_size_t count)							\
    999  1.13      maxv 	{									\
   1000  1.13      maxv 		kasan_shadow_check((uintptr_t)buf,				\
   1001  1.13      maxv 		    sizeof(uint##bits##_t) * count, false, __RET_ADDR);		\
   1002  1.13      maxv 		bus_space_read_region_stream_##bytes(tag, hnd, size, buf, count);\
   1003  1.13      maxv 	}
   1004  1.13      maxv 
   1005  1.13      maxv #define ASAN_BUS_WRITE_FUNC(bytes, bits) \
   1006  1.13      maxv 	void bus_space_write_multi_##bytes(bus_space_tag_t, bus_space_handle_t,	\
   1007  1.13      maxv 	    bus_size_t, const uint##bits##_t *, bus_size_t);			\
   1008  1.13      maxv 	void kasan_bus_space_write_multi_##bytes(bus_space_tag_t,		\
   1009  1.13      maxv 	    bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
   1010  1.13      maxv 	void kasan_bus_space_write_multi_##bytes(bus_space_tag_t tag,		\
   1011  1.13      maxv 	    bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf,	\
   1012  1.13      maxv 	    bus_size_t count)							\
   1013  1.13      maxv 	{									\
   1014  1.13      maxv 		kasan_shadow_check((uintptr_t)buf,				\
   1015  1.13      maxv 		    sizeof(uint##bits##_t) * count, true, __RET_ADDR);		\
   1016  1.13      maxv 		bus_space_write_multi_##bytes(tag, hnd, size, buf, count);	\
   1017  1.13      maxv 	}									\
   1018  1.13      maxv 	void bus_space_write_multi_stream_##bytes(bus_space_tag_t,		\
   1019  1.13      maxv 	    bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
   1020  1.13      maxv 	void kasan_bus_space_write_multi_stream_##bytes(bus_space_tag_t,	\
   1021  1.13      maxv 	    bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
   1022  1.13      maxv 	void kasan_bus_space_write_multi_stream_##bytes(bus_space_tag_t tag,	\
   1023  1.13      maxv 	    bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf,	\
   1024  1.13      maxv 	    bus_size_t count)							\
   1025  1.13      maxv 	{									\
   1026  1.13      maxv 		kasan_shadow_check((uintptr_t)buf,				\
   1027  1.13      maxv 		    sizeof(uint##bits##_t) * count, true, __RET_ADDR);		\
   1028  1.13      maxv 		bus_space_write_multi_stream_##bytes(tag, hnd, size, buf, count);\
   1029  1.13      maxv 	}									\
   1030  1.13      maxv 	void bus_space_write_region_##bytes(bus_space_tag_t, bus_space_handle_t,\
   1031  1.13      maxv 	    bus_size_t, const uint##bits##_t *, bus_size_t);			\
   1032  1.13      maxv 	void kasan_bus_space_write_region_##bytes(bus_space_tag_t,		\
   1033  1.13      maxv 	    bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
   1034  1.13      maxv 	void kasan_bus_space_write_region_##bytes(bus_space_tag_t tag,		\
   1035  1.13      maxv 	    bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf,	\
   1036  1.13      maxv 	    bus_size_t count)							\
   1037  1.13      maxv 	{									\
   1038  1.13      maxv 		kasan_shadow_check((uintptr_t)buf,				\
   1039  1.13      maxv 		    sizeof(uint##bits##_t) * count, true, __RET_ADDR);		\
   1040  1.13      maxv 		bus_space_write_region_##bytes(tag, hnd, size, buf, count);	\
   1041  1.13      maxv 	}									\
   1042  1.13      maxv 	void bus_space_write_region_stream_##bytes(bus_space_tag_t,		\
   1043  1.13      maxv 	    bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
   1044  1.13      maxv 	void kasan_bus_space_write_region_stream_##bytes(bus_space_tag_t,	\
   1045  1.13      maxv 	    bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
   1046  1.13      maxv 	void kasan_bus_space_write_region_stream_##bytes(bus_space_tag_t tag,	\
   1047  1.13      maxv 	    bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf,	\
   1048  1.13      maxv 	    bus_size_t count)							\
   1049  1.13      maxv 	{									\
   1050  1.13      maxv 		kasan_shadow_check((uintptr_t)buf,				\
   1051  1.13      maxv 		    sizeof(uint##bits##_t) * count, true, __RET_ADDR);		\
   1052  1.13      maxv 		bus_space_write_region_stream_##bytes(tag, hnd, size, buf, count);\
   1053  1.13      maxv 	}
   1054  1.13      maxv 
   1055  1.13      maxv ASAN_BUS_READ_FUNC(1, 8)
   1056  1.13      maxv ASAN_BUS_READ_FUNC(2, 16)
   1057  1.13      maxv ASAN_BUS_READ_FUNC(4, 32)
   1058  1.13      maxv ASAN_BUS_READ_FUNC(8, 64)
   1059  1.13      maxv 
   1060  1.13      maxv ASAN_BUS_WRITE_FUNC(1, 8)
   1061  1.13      maxv ASAN_BUS_WRITE_FUNC(2, 16)
   1062  1.13      maxv ASAN_BUS_WRITE_FUNC(4, 32)
   1063  1.13      maxv ASAN_BUS_WRITE_FUNC(8, 64)
   1064  1.13      maxv 
   1065  1.14      maxv #endif /* __HAVE_KASAN_INSTR_BUS */
   1066  1.14      maxv 
   1067  1.13      maxv /* -------------------------------------------------------------------------- */
   1068  1.13      maxv 
   1069  1.15      maxv #include <sys/mbuf.h>
   1070  1.15      maxv 
   1071  1.15      maxv static void
   1072  1.15      maxv kasan_dma_sync_linear(uint8_t *buf, bus_addr_t offset, bus_size_t len,
   1073  1.15      maxv     bool write, uintptr_t pc)
   1074  1.15      maxv {
   1075  1.15      maxv 	kasan_shadow_check((uintptr_t)(buf + offset), len, write, pc);
   1076  1.15      maxv }
   1077  1.15      maxv 
   1078  1.15      maxv static void
   1079  1.15      maxv kasan_dma_sync_mbuf(struct mbuf *m, bus_addr_t offset, bus_size_t len,
   1080  1.15      maxv     bool write, uintptr_t pc)
   1081  1.15      maxv {
   1082  1.15      maxv 	bus_addr_t minlen;
   1083  1.15      maxv 
   1084  1.15      maxv 	for (; m != NULL && len != 0; m = m->m_next) {
   1085  1.15      maxv 		kasan_shadow_check((uintptr_t)m, sizeof(*m), false, pc);
   1086  1.15      maxv 
   1087  1.15      maxv 		if (offset >= m->m_len) {
   1088  1.15      maxv 			offset -= m->m_len;
   1089  1.15      maxv 			continue;
   1090  1.15      maxv 		}
   1091  1.15      maxv 
   1092  1.15      maxv 		minlen = MIN(len, m->m_len - offset);
   1093  1.15      maxv 		kasan_shadow_check((uintptr_t)(mtod(m, char *) + offset),
   1094  1.15      maxv 		    minlen, write, pc);
   1095  1.15      maxv 
   1096  1.15      maxv 		offset = 0;
   1097  1.15      maxv 		len -= minlen;
   1098  1.15      maxv 	}
   1099  1.15      maxv }
   1100  1.15      maxv 
   1101  1.15      maxv static void
   1102  1.15      maxv kasan_dma_sync_uio(struct uio *uio, bus_addr_t offset, bus_size_t len,
   1103  1.15      maxv     bool write, uintptr_t pc)
   1104  1.15      maxv {
   1105  1.15      maxv 	bus_size_t minlen, resid;
   1106  1.15      maxv 	struct iovec *iov;
   1107  1.15      maxv 	int i;
   1108  1.15      maxv 
   1109  1.17      maxv 	kasan_shadow_check((uintptr_t)uio, sizeof(struct uio), false, pc);
   1110  1.17      maxv 
   1111  1.17      maxv 	if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace))
   1112  1.15      maxv 		return;
   1113  1.15      maxv 
   1114  1.15      maxv 	resid = uio->uio_resid;
   1115  1.15      maxv 	iov = uio->uio_iov;
   1116  1.15      maxv 
   1117  1.15      maxv 	for (i = 0; i < uio->uio_iovcnt && resid != 0; i++) {
   1118  1.15      maxv 		kasan_shadow_check((uintptr_t)&iov[i], sizeof(iov[i]),
   1119  1.15      maxv 		    false, pc);
   1120  1.15      maxv 		minlen = MIN(resid, iov[i].iov_len);
   1121  1.15      maxv 		kasan_shadow_check((uintptr_t)iov[i].iov_base, minlen,
   1122  1.15      maxv 		    write, pc);
   1123  1.15      maxv 		resid -= minlen;
   1124  1.15      maxv 	}
   1125  1.15      maxv }
   1126  1.15      maxv 
   1127  1.15      maxv void
   1128  1.15      maxv kasan_dma_sync(bus_dmamap_t map, bus_addr_t offset, bus_size_t len, int ops)
   1129  1.15      maxv {
   1130  1.15      maxv 	bool write = (ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTWRITE)) != 0;
   1131  1.15      maxv 
   1132  1.15      maxv 	switch (map->dm_buftype) {
   1133  1.15      maxv 	case KASAN_DMA_LINEAR:
   1134  1.15      maxv 		kasan_dma_sync_linear(map->dm_buf, offset, len, write,
   1135  1.15      maxv 		    __RET_ADDR);
   1136  1.15      maxv 		break;
   1137  1.15      maxv 	case KASAN_DMA_MBUF:
   1138  1.15      maxv 		kasan_dma_sync_mbuf(map->dm_buf, offset, len, write,
   1139  1.15      maxv 		    __RET_ADDR);
   1140  1.15      maxv 		break;
   1141  1.15      maxv 	case KASAN_DMA_UIO:
   1142  1.15      maxv 		kasan_dma_sync_uio(map->dm_buf, offset, len, write,
   1143  1.15      maxv 		    __RET_ADDR);
   1144  1.15      maxv 		break;
   1145  1.15      maxv 	case KASAN_DMA_RAW:
   1146  1.15      maxv 		break;
   1147  1.15      maxv 	default:
   1148  1.15      maxv 		panic("%s: impossible", __func__);
   1149  1.15      maxv 	}
   1150  1.15      maxv }
   1151  1.15      maxv 
   1152  1.15      maxv void
   1153  1.15      maxv kasan_dma_load(bus_dmamap_t map, void *buf, bus_size_t buflen, int type)
   1154  1.15      maxv {
   1155  1.15      maxv 	map->dm_buf = buf;
   1156  1.15      maxv 	map->dm_buflen = buflen;
   1157  1.15      maxv 	map->dm_buftype = type;
   1158  1.15      maxv }
   1159  1.15      maxv 
   1160  1.15      maxv /* -------------------------------------------------------------------------- */
   1161  1.15      maxv 
   1162   1.1      maxv void __asan_register_globals(struct __asan_global *, size_t);
   1163   1.1      maxv void __asan_unregister_globals(struct __asan_global *, size_t);
   1164   1.1      maxv 
   1165   1.1      maxv void
   1166   1.1      maxv __asan_register_globals(struct __asan_global *globals, size_t n)
   1167   1.1      maxv {
   1168   1.1      maxv 	size_t i;
   1169   1.1      maxv 
   1170   1.1      maxv 	for (i = 0; i < n; i++) {
   1171   1.2      maxv 		kasan_mark(globals[i].beg, globals[i].size,
   1172   1.6      maxv 		    globals[i].size_with_redzone, KASAN_GENERIC_REDZONE);
   1173   1.1      maxv 	}
   1174   1.1      maxv }
   1175   1.1      maxv 
   1176   1.1      maxv void
   1177   1.1      maxv __asan_unregister_globals(struct __asan_global *globals, size_t n)
   1178   1.1      maxv {
   1179   1.1      maxv 	/* never called */
   1180   1.1      maxv }
   1181   1.1      maxv 
   1182   1.1      maxv #define ASAN_LOAD_STORE(size)					\
   1183   1.1      maxv 	void __asan_load##size(unsigned long);			\
   1184   1.1      maxv 	void __asan_load##size(unsigned long addr)		\
   1185   1.1      maxv 	{							\
   1186   1.1      maxv 		kasan_shadow_check(addr, size, false, __RET_ADDR);\
   1187   1.1      maxv 	} 							\
   1188   1.1      maxv 	void __asan_load##size##_noabort(unsigned long);	\
   1189   1.1      maxv 	void __asan_load##size##_noabort(unsigned long addr)	\
   1190   1.1      maxv 	{							\
   1191   1.1      maxv 		kasan_shadow_check(addr, size, false, __RET_ADDR);\
   1192   1.1      maxv 	}							\
   1193   1.1      maxv 	void __asan_store##size(unsigned long);			\
   1194   1.1      maxv 	void __asan_store##size(unsigned long addr)		\
   1195   1.1      maxv 	{							\
   1196   1.1      maxv 		kasan_shadow_check(addr, size, true, __RET_ADDR);\
   1197   1.1      maxv 	}							\
   1198   1.1      maxv 	void __asan_store##size##_noabort(unsigned long);	\
   1199   1.1      maxv 	void __asan_store##size##_noabort(unsigned long addr)	\
   1200   1.1      maxv 	{							\
   1201   1.1      maxv 		kasan_shadow_check(addr, size, true, __RET_ADDR);\
   1202   1.1      maxv 	}
   1203   1.1      maxv 
   1204   1.1      maxv ASAN_LOAD_STORE(1);
   1205   1.1      maxv ASAN_LOAD_STORE(2);
   1206   1.1      maxv ASAN_LOAD_STORE(4);
   1207   1.1      maxv ASAN_LOAD_STORE(8);
   1208   1.1      maxv ASAN_LOAD_STORE(16);
   1209   1.1      maxv 
   1210   1.1      maxv void __asan_loadN(unsigned long, size_t);
   1211   1.1      maxv void __asan_loadN_noabort(unsigned long, size_t);
   1212   1.1      maxv void __asan_storeN(unsigned long, size_t);
   1213   1.1      maxv void __asan_storeN_noabort(unsigned long, size_t);
   1214   1.1      maxv void __asan_handle_no_return(void);
   1215   1.1      maxv 
   1216   1.1      maxv void
   1217   1.1      maxv __asan_loadN(unsigned long addr, size_t size)
   1218   1.1      maxv {
   1219   1.1      maxv 	kasan_shadow_check(addr, size, false, __RET_ADDR);
   1220   1.1      maxv }
   1221   1.1      maxv 
   1222   1.1      maxv void
   1223   1.1      maxv __asan_loadN_noabort(unsigned long addr, size_t size)
   1224   1.1      maxv {
   1225   1.1      maxv 	kasan_shadow_check(addr, size, false, __RET_ADDR);
   1226   1.1      maxv }
   1227   1.1      maxv 
   1228   1.1      maxv void
   1229   1.1      maxv __asan_storeN(unsigned long addr, size_t size)
   1230   1.1      maxv {
   1231   1.1      maxv 	kasan_shadow_check(addr, size, true, __RET_ADDR);
   1232   1.1      maxv }
   1233   1.1      maxv 
   1234   1.1      maxv void
   1235   1.1      maxv __asan_storeN_noabort(unsigned long addr, size_t size)
   1236   1.1      maxv {
   1237   1.1      maxv 	kasan_shadow_check(addr, size, true, __RET_ADDR);
   1238   1.1      maxv }
   1239   1.1      maxv 
   1240   1.1      maxv void
   1241   1.1      maxv __asan_handle_no_return(void)
   1242   1.1      maxv {
   1243   1.1      maxv 	/* nothing */
   1244   1.1      maxv }
   1245   1.1      maxv 
   1246   1.1      maxv #define ASAN_SET_SHADOW(byte) \
   1247   1.1      maxv 	void __asan_set_shadow_##byte(void *, size_t);			\
   1248   1.1      maxv 	void __asan_set_shadow_##byte(void *addr, size_t size)		\
   1249   1.1      maxv 	{								\
   1250   1.1      maxv 		__builtin_memset((void *)addr, 0x##byte, size);		\
   1251   1.1      maxv 	}
   1252   1.1      maxv 
   1253   1.1      maxv ASAN_SET_SHADOW(00);
   1254   1.1      maxv ASAN_SET_SHADOW(f1);
   1255   1.1      maxv ASAN_SET_SHADOW(f2);
   1256   1.1      maxv ASAN_SET_SHADOW(f3);
   1257   1.1      maxv ASAN_SET_SHADOW(f5);
   1258   1.1      maxv ASAN_SET_SHADOW(f8);
   1259   1.4      maxv 
   1260   1.4      maxv void __asan_poison_stack_memory(const void *, size_t);
   1261   1.4      maxv void __asan_unpoison_stack_memory(const void *, size_t);
   1262   1.4      maxv 
   1263  1.17      maxv void
   1264  1.17      maxv __asan_poison_stack_memory(const void *addr, size_t size)
   1265   1.4      maxv {
   1266   1.4      maxv 	size = roundup(size, KASAN_SHADOW_SCALE_SIZE);
   1267   1.4      maxv 	kasan_shadow_Nbyte_fill(addr, size, KASAN_USE_AFTER_SCOPE);
   1268   1.4      maxv }
   1269   1.4      maxv 
   1270  1.17      maxv void
   1271  1.17      maxv __asan_unpoison_stack_memory(const void *addr, size_t size)
   1272   1.4      maxv {
   1273   1.4      maxv 	kasan_shadow_Nbyte_markvalid(addr, size);
   1274   1.4      maxv }
   1275  1.19      maxv 
   1276  1.19      maxv void __asan_alloca_poison(const void *, size_t);
   1277  1.19      maxv void __asan_allocas_unpoison(const void *, const void *);
   1278  1.19      maxv 
   1279  1.19      maxv void __asan_alloca_poison(const void *addr, size_t size)
   1280  1.19      maxv {
   1281  1.19      maxv 	const void *l, *r;
   1282  1.19      maxv 
   1283  1.19      maxv 	KASSERT((vaddr_t)addr % KASAN_ALLOCA_SCALE_SIZE == 0);
   1284  1.19      maxv 
   1285  1.19      maxv 	l = (const uint8_t *)addr - KASAN_ALLOCA_SCALE_SIZE;
   1286  1.19      maxv 	r = (const uint8_t *)addr + roundup(size, KASAN_ALLOCA_SCALE_SIZE);
   1287  1.19      maxv 
   1288  1.19      maxv 	kasan_shadow_Nbyte_fill(l, KASAN_ALLOCA_SCALE_SIZE, KASAN_STACK_LEFT);
   1289  1.19      maxv 	kasan_mark(addr, size, roundup(size, KASAN_ALLOCA_SCALE_SIZE),
   1290  1.19      maxv 	    KASAN_STACK_MID);
   1291  1.19      maxv 	kasan_shadow_Nbyte_fill(r, KASAN_ALLOCA_SCALE_SIZE, KASAN_STACK_RIGHT);
   1292  1.19      maxv }
   1293  1.19      maxv 
   1294  1.19      maxv void __asan_allocas_unpoison(const void *stkbegin, const void *stkend)
   1295  1.19      maxv {
   1296  1.19      maxv 	size_t size;
   1297  1.19      maxv 
   1298  1.19      maxv 	if (__predict_false(!stkbegin))
   1299  1.19      maxv 		return;
   1300  1.19      maxv 	if (__predict_false((uintptr_t)stkbegin > (uintptr_t)stkend))
   1301  1.19      maxv 		return;
   1302  1.19      maxv 	size = (uintptr_t)stkend - (uintptr_t)stkbegin;
   1303  1.19      maxv 
   1304  1.19      maxv 	kasan_shadow_Nbyte_fill(stkbegin, size, 0);
   1305  1.19      maxv }
   1306