Skip to content

Commit

Permalink
xen/xmalloc: XMEM_POOL_POISON improvements
Browse files Browse the repository at this point in the history
When in use, the spew:

  (XEN) Assertion '!memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE, (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE)' failed at common/xmalloc_tlsf.c:246

is unweidly and likely meaningless to non-Xen developers.  Therefore:

 * Switch to IS_ENABLED().  There's no need for full #ifdef-ary.
 * Pull memchr_inv() out into the if(), and provide an error message which
   clearly states that corruption has been found.
 * XMEM_POOL_POISON can be enabled in release builds.  Use printk()+BUG() so
   it doesn't silently stop working when assertions are compiled out.

Signed-off-by: Andrew Cooper <[email protected]>
Reviewed-by: Julien Grall <[email protected]>
  • Loading branch information
andyhhp committed Dec 21, 2023
1 parent 3909fb4 commit b74c735
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions xen/common/xmalloc_tlsf.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,23 +249,25 @@ static inline void EXTRACT_BLOCK(struct bhdr *b, struct xmem_pool *p, int fl,
}
b->ptr.free_ptr = (struct free_ptr) {NULL, NULL};

#ifdef CONFIG_XMEM_POOL_POISON
if ( (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE )
ASSERT(!memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE,
(b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE));
#endif /* CONFIG_XMEM_POOL_POISON */
if ( IS_ENABLED(CONFIG_XMEM_POOL_POISON) &&
(b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE &&
memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE,
(b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE) )
{
printk(XENLOG_ERR "XMEM Pool corruption found");
BUG();
}
}

/**
* Insert block(b) in free list with indexes (fl, sl)
*/
static inline void INSERT_BLOCK(struct bhdr *b, struct xmem_pool *p, int fl, int sl)
{
#ifdef CONFIG_XMEM_POOL_POISON
if ( (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE )
if ( IS_ENABLED(CONFIG_XMEM_POOL_POISON) &&
(b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE )
memset(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE,
(b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE);
#endif /* CONFIG_XMEM_POOL_POISON */

b->ptr.free_ptr = (struct free_ptr) {NULL, p->matrix[fl][sl]};
if ( p->matrix[fl][sl] )
Expand Down

0 comments on commit b74c735

Please sign in to comment.