From 8df36761038cf817c0410f0b808711137369d07b Mon Sep 17 00:00:00 2001 From: jmg Date: Thu, 25 Jun 2015 20:44:46 +0000 Subject: [PATCH] If INVARIANTS is specified, add ctor/dtor to junk memory if they are unspecified... Submitted by: Suresh Gumpula at Netapp Differential Revision: https://reviews.freebsd.org/D2725 --- sys/vm/uma_core.c | 14 ++++++++++++++ sys/vm/uma_dbg.c | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/sys/vm/uma_core.c b/sys/vm/uma_core.c index 6780bad1ecac..04e000e5dff9 100644 --- a/sys/vm/uma_core.c +++ b/sys/vm/uma_core.c @@ -1940,6 +1940,20 @@ uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, args.dtor = dtor; args.uminit = uminit; args.fini = fini; +#ifdef INVARIANTS + /* + * If a zone is being created with an empty constructor and + * destructor, pass UMA constructor/destructor which checks for + * memory use after free. + */ + if ((!(flags & UMA_ZONE_ZINIT)) && ctor == NULL && dtor == NULL && + uminit == NULL && fini == NULL) { + args.ctor = trash_ctor; + args.dtor = trash_dtor; + args.uminit = trash_init; + args.fini = trash_fini; + } +#endif args.align = align; args.flags = flags; args.keg = NULL; diff --git a/sys/vm/uma_dbg.c b/sys/vm/uma_dbg.c index 48a5e6d472f5..3fbd29b16dea 100644 --- a/sys/vm/uma_dbg.c +++ b/sys/vm/uma_dbg.c @@ -69,8 +69,13 @@ trash_ctor(void *mem, int size, void *arg, int flags) for (p = mem; cnt > 0; cnt--, p++) if (*p != uma_junk) { +#ifdef INVARIANTS + panic("Memory modified after free %p(%d) val=%x @ %p\n", + mem, size, *p, p); +#else printf("Memory modified after free %p(%d) val=%x @ %p\n", mem, size, *p, p); +#endif return (0); } return (0);