Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Reduce the number of KASAN ifdefs, suggested by Christos...



details:   https://anonhg.NetBSD.org/src/rev/41132833124f
branches:  trunk
changeset: 364043:41132833124f
user:      maxv <maxv%NetBSD.org@localhost>
date:      Wed Aug 22 09:38:21 2018 +0000

description:
Reduce the number of KASAN ifdefs, suggested by Christos/Taylor.

diffstat:

 sys/kern/kern_malloc.c |  17 +++--------------
 sys/kern/subr_kmem.c   |  25 +++++--------------------
 sys/sys/asan.h         |  23 ++++++++++++++++++++++-
 3 files changed, 30 insertions(+), 35 deletions(-)

diffs (194 lines):

diff -r ba61c1433ca0 -r 41132833124f sys/kern/kern_malloc.c
--- a/sys/kern/kern_malloc.c    Wed Aug 22 09:11:47 2018 +0000
+++ b/sys/kern/kern_malloc.c    Wed Aug 22 09:38:21 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_malloc.c,v 1.150 2018/08/21 07:56:53 maxv Exp $   */
+/*     $NetBSD: kern_malloc.c,v 1.151 2018/08/22 09:38:21 maxv Exp $   */
 
 /*
  * Copyright (c) 1987, 1991, 1993
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.150 2018/08/21 07:56:53 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.151 2018/08/22 09:38:21 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kasan.h"
@@ -79,10 +79,7 @@
 #include <sys/param.h>
 #include <sys/malloc.h>
 #include <sys/kmem.h>
-
-#ifdef KASAN
 #include <sys/asan.h>
-#endif
 
 /*
  * Built-in malloc types.  Note: ought to be removed.
@@ -111,16 +108,12 @@
 kern_malloc(unsigned long size, int flags)
 {
        const int kmflags = (flags & M_NOWAIT) ? KM_NOSLEEP : KM_SLEEP;
-#ifdef KASAN
-       size_t origsize = size;
-#endif
+       const size_t origsize = size;
        size_t allocsize, hdroffset;
        struct malloc_header *mh;
        void *p;
 
-#ifdef KASAN
        kasan_add_redzone(&size);
-#endif
 
        if (size >= PAGE_SIZE) {
                if (size > (ULONG_MAX-PAGE_SIZE))
@@ -147,9 +140,7 @@
 #endif
        mh++;
 
-#ifdef KASAN
        kasan_alloc(mh, origsize, size);
-#endif
 
        return mh;
 }
@@ -162,9 +153,7 @@
        mh = addr;
        mh--;
 
-#ifdef KASAN
        kasan_free(addr, mh->mh_size);
-#endif
 
        if (mh->mh_size >= PAGE_SIZE + sizeof(struct malloc_header))
                kmem_intr_free((char *)addr - PAGE_SIZE,
diff -r ba61c1433ca0 -r 41132833124f sys/kern/subr_kmem.c
--- a/sys/kern/subr_kmem.c      Wed Aug 22 09:11:47 2018 +0000
+++ b/sys/kern/subr_kmem.c      Wed Aug 22 09:38:21 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_kmem.c,v 1.69 2018/08/20 15:04:52 maxv Exp $      */
+/*     $NetBSD: subr_kmem.c,v 1.70 2018/08/22 09:38:21 maxv Exp $      */
 
 /*-
  * Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
@@ -92,7 +92,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.69 2018/08/20 15:04:52 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.70 2018/08/22 09:38:21 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kmem.h"
@@ -106,10 +106,7 @@
 #include <sys/debug.h>
 #include <sys/lockdebug.h>
 #include <sys/cpu.h>
-
-#ifdef KASAN
 #include <sys/asan.h>
-#endif
 
 #include <uvm/uvm_extern.h>
 #include <uvm/uvm_map.h>
@@ -227,9 +224,7 @@
 void *
 kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
 {
-#ifdef KASAN
-       size_t origsize = requested_size;
-#endif
+       const size_t origsize = requested_size;
        size_t allocsz, index;
        size_t size;
        pool_cache_t pc;
@@ -247,10 +242,7 @@
        }
 #endif
 
-#ifdef KASAN
        kasan_add_redzone(&requested_size);
-#endif
-
        size = kmem_roundup_size(requested_size);
        allocsz = size + SIZE_SIZE;
 
@@ -278,9 +270,7 @@
                FREECHECK_OUT(&kmem_freecheck, p);
                kmem_size_set(p, requested_size);
                p += SIZE_SIZE;
-#ifdef KASAN
                kasan_alloc(p, origsize, size);
-#endif
                return p;
        }
        return p;
@@ -323,17 +313,10 @@
        }
 #endif
 
-#ifdef KASAN
        kasan_add_redzone(&requested_size);
-#endif
-
        size = kmem_roundup_size(requested_size);
        allocsz = size + SIZE_SIZE;
 
-#ifdef KASAN
-       kasan_free(p, size);
-#endif
-
        if ((index = ((allocsz -1) >> KMEM_SHIFT))
            < kmem_cache_maxidx) {
                pc = kmem_cache[index];
@@ -347,6 +330,8 @@
                return;
        }
 
+       kasan_free(p, size);
+
        p = (uint8_t *)p - SIZE_SIZE;
        kmem_size_check(p, requested_size);
        FREECHECK_IN(&kmem_freecheck, p);
diff -r ba61c1433ca0 -r 41132833124f sys/sys/asan.h
--- a/sys/sys/asan.h    Wed Aug 22 09:11:47 2018 +0000
+++ b/sys/sys/asan.h    Wed Aug 22 09:38:21 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: asan.h,v 1.1 2018/08/20 15:04:52 maxv Exp $    */
+/*     $NetBSD: asan.h,v 1.2 2018/08/22 09:38:21 maxv Exp $    */
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -34,8 +34,29 @@
 
 #include <sys/types.h>
 
+#ifdef KASAN
 void kasan_add_redzone(size_t *);
 void kasan_alloc(const void *, size_t, size_t);
 void kasan_free(const void *, size_t);
+#else
+static void __always_inline
+kasan_add_redzone(size_t *size __unused)
+{
+       /* nothing */
+}
+
+static void __always_inline
+kasan_alloc(const void *addr __unused, size_t size __unused,
+    size_t sz_with_redz __unused)
+{
+       /* nothing */
+}
+
+static void __always_inline
+kasan_free(const void *addr __unused, size_t sz_with_redz __unused)
+{
+       /* nothing */
+}
+#endif
 
 #endif /* !_SYS_ASAN_H_ */



Home | Main Index | Thread Index | Old Index