Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/xlint lint: remove custom memory allocator
details: https://anonhg.NetBSD.org/src/rev/b64ec91753ae
branches: trunk
changeset: 373026:b64ec91753ae
user: rillig <rillig%NetBSD.org@localhost>
date: Fri Jan 13 19:41:50 2023 +0000
description:
lint: remove custom memory allocator
Besides adding complexity, the custom memory allocator didn't invalidate
freed memory, which made it harder to find possible use-after-free bugs.
diffstat:
usr.bin/xlint/common/externs.h | 3 +-
usr.bin/xlint/common/mem.c | 16 +----
usr.bin/xlint/lint1/externs1.h | 8 +-
usr.bin/xlint/lint1/init.c | 9 +-
usr.bin/xlint/lint1/lint1.h | 16 ++++-
usr.bin/xlint/lint1/main1.c | 5 +-
usr.bin/xlint/lint1/mem1.c | 132 ++++++++++++++++------------------------
usr.bin/xlint/lint1/tree.c | 6 +-
usr.bin/xlint/lint2/externs2.h | 3 +-
usr.bin/xlint/lint2/main2.c | 6 +-
usr.bin/xlint/lint2/mem2.c | 42 +-----------
11 files changed, 90 insertions(+), 156 deletions(-)
diffs (truncated from 534 to 300 lines):
diff -r 80e7d55b305a -r b64ec91753ae usr.bin/xlint/common/externs.h
--- a/usr.bin/xlint/common/externs.h Fri Jan 13 18:43:42 2023 +0000
+++ b/usr.bin/xlint/common/externs.h Fri Jan 13 19:41:50 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: externs.h,v 1.24 2021/09/04 14:48:27 rillig Exp $ */
+/* $NetBSD: externs.h,v 1.25 2023/01/13 19:41:50 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -45,7 +45,6 @@
/*
* mem.c
*/
-extern size_t mem_block_size(void);
extern void *xmalloc(size_t);
extern void *xcalloc(size_t, size_t);
extern void *xrealloc(void *, size_t);
diff -r 80e7d55b305a -r b64ec91753ae usr.bin/xlint/common/mem.c
--- a/usr.bin/xlint/common/mem.c Fri Jan 13 18:43:42 2023 +0000
+++ b/usr.bin/xlint/common/mem.c Fri Jan 13 19:41:50 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: mem.c,v 1.20 2022/05/20 21:18:54 rillig Exp $ */
+/* $NetBSD: mem.c,v 1.21 2023/01/13 19:41:50 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,27 +37,15 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
-__RCSID("$NetBSD: mem.c,v 1.20 2022/05/20 21:18:54 rillig Exp $");
+__RCSID("$NetBSD: mem.c,v 1.21 2023/01/13 19:41:50 rillig Exp $");
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
#include "lint.h"
-#if defined(IS_LINT1) || defined(IS_LINT2)
-size_t
-mem_block_size(void)
-{
- unsigned int pagesize;
-
- pagesize = (unsigned int)getpagesize();
- return (MBLKSIZ + pagesize - 1) / pagesize * pagesize;
-}
-#endif
-
static void *
not_null(void *ptr)
{
diff -r 80e7d55b305a -r b64ec91753ae usr.bin/xlint/lint1/externs1.h
--- a/usr.bin/xlint/lint1/externs1.h Fri Jan 13 18:43:42 2023 +0000
+++ b/usr.bin/xlint/lint1/externs1.h Fri Jan 13 19:41:50 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: externs1.h,v 1.170 2022/10/01 09:59:40 rillig Exp $ */
+/* $NetBSD: externs1.h,v 1.171 2023/01/13 19:41:50 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -99,8 +99,6 @@
extern void add_directory_replacement(char *);
extern const char *transform_filename(const char *, size_t);
-extern void initmem(void);
-
extern void *block_zero_alloc(size_t);
extern void *level_zero_alloc(size_t, size_t);
extern void level_free_all(size_t);
@@ -108,8 +106,8 @@
extern void *expr_zero_alloc(size_t);
extern tnode_t *expr_alloc_tnode(void);
extern void expr_free_all(void);
-extern struct memory_block *expr_save_memory(void);
-extern void expr_restore_memory(struct memory_block *);
+extern memory_pool expr_save_memory(void);
+extern void expr_restore_memory(memory_pool);
/*
* debug.c
diff -r 80e7d55b305a -r b64ec91753ae usr.bin/xlint/lint1/init.c
--- a/usr.bin/xlint/lint1/init.c Fri Jan 13 18:43:42 2023 +0000
+++ b/usr.bin/xlint/lint1/init.c Fri Jan 13 19:41:50 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: init.c,v 1.237 2022/08/28 12:04:47 rillig Exp $ */
+/* $NetBSD: init.c,v 1.238 2023/01/13 19:41:50 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -38,7 +38,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
-__RCSID("$NetBSD: init.c,v 1.237 2022/08/28 12:04:47 rillig Exp $");
+__RCSID("$NetBSD: init.c,v 1.238 2023/01/13 19:41:50 rillig Exp $");
#endif
#include <stdlib.h>
@@ -309,7 +309,6 @@
tnode_t *ln;
type_t *lutp;
tspec_t lt, rt;
- struct memory_block *tmem;
lutp = expr_unqualified_type(ltp);
@@ -334,9 +333,9 @@
* Preserve the tree memory. This is necessary because otherwise
* expr() would free it.
*/
- tmem = expr_save_memory();
+ memory_pool saved_mem = expr_save_memory();
expr(rn, true, false, true, false);
- expr_restore_memory(tmem);
+ expr_restore_memory(saved_mem);
check_bit_field_init(ln, lt, rt);
diff -r 80e7d55b305a -r b64ec91753ae usr.bin/xlint/lint1/lint1.h
--- a/usr.bin/xlint/lint1/lint1.h Fri Jan 13 18:43:42 2023 +0000
+++ b/usr.bin/xlint/lint1/lint1.h Fri Jan 13 19:41:50 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.158 2022/10/01 09:42:40 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.159 2023/01/13 19:41:50 rillig Exp $ */
/*
* Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
@@ -36,6 +36,18 @@
#include "err-msgs.h"
#include "op.h"
+/*
+ * A memory pool collects allocated objects that must be available until:
+ * - the end of a block,
+ * - the end of an expression, or
+ * - the end of the translation unit.
+ */
+typedef struct memory_pool {
+ void **items;
+ size_t len;
+ size_t cap;
+} memory_pool;
+
/* See saved_lwarn in cgram.y. */
#define LWARN_ALL (-2)
#define LWARN_NONE (-1)
@@ -426,7 +438,7 @@
tnode_t *c_switch_expr;
case_label_t *c_case_labels; /* list of case values */
- struct memory_block *c_for_expr3_mem; /* saved memory for end of loop
+ memory_pool c_for_expr3_mem; /* saved memory for end of loop
* expression in for() */
tnode_t *c_for_expr3; /* end of loop expr in for() */
pos_t c_for_expr3_pos; /* position of end of loop expr */
diff -r 80e7d55b305a -r b64ec91753ae usr.bin/xlint/lint1/main1.c
--- a/usr.bin/xlint/lint1/main1.c Fri Jan 13 18:43:42 2023 +0000
+++ b/usr.bin/xlint/lint1/main1.c Fri Jan 13 19:41:50 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: main1.c,v 1.65 2022/07/05 22:50:41 rillig Exp $ */
+/* $NetBSD: main1.c,v 1.66 2023/01/13 19:41:50 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
-__RCSID("$NetBSD: main1.c,v 1.65 2022/07/05 22:50:41 rillig Exp $");
+__RCSID("$NetBSD: main1.c,v 1.66 2023/01/13 19:41:50 rillig Exp $");
#endif
#include <sys/types.h>
@@ -259,7 +259,6 @@
#endif
(void)signal(SIGFPE, sigfpe);
- initmem();
initdecl();
initscan();
diff -r 80e7d55b305a -r b64ec91753ae usr.bin/xlint/lint1/mem1.c
--- a/usr.bin/xlint/lint1/mem1.c Fri Jan 13 18:43:42 2023 +0000
+++ b/usr.bin/xlint/lint1/mem1.c Fri Jan 13 19:41:50 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: mem1.c,v 1.63 2022/05/20 21:18:55 rillig Exp $ */
+/* $NetBSD: mem1.c,v 1.64 2023/01/13 19:41:50 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
-__RCSID("$NetBSD: mem1.c,v 1.63 2022/05/20 21:18:55 rillig Exp $");
+__RCSID("$NetBSD: mem1.c,v 1.64 2023/01/13 19:41:50 rillig Exp $");
#endif
#include <sys/param.h>
@@ -160,74 +160,60 @@
return fn->fn_id;
}
-/*
- * Memory for declarations and other things that must be available
- * until the end of a block (or the end of the translation unit)
- * is associated with the corresponding mem_block_level, which may be 0.
- * Because this memory is allocated in large blocks associated with
- * a given level it can be freed easily at the end of a block.
- */
-typedef struct memory_block {
- void *start; /* beginning of memory block */
- void *first_free; /* first free byte */
- size_t nfree; /* # of free bytes */
- struct memory_block *next;
-} memory_block;
+/* Array of memory pools, indexed by mem_block_level. */
+typedef struct memory_pools {
+ struct memory_pool *pools;
+ size_t cap;
+} memory_pools;
-
-static size_t mblk_size; /* size of newly allocated memory blocks */
+static memory_pools mpools;
-/* Array of lists of memory blocks, indexed by mem_block_level. */
-static memory_block **mblks;
-static size_t nmblks; /* number of elements in *mblks */
-#define ML_INC ((size_t)32) /* Increment for length of *mblks */
-
+/* The pool for the current expression is independent of any block level. */
+static memory_pool expr_pool;
-/* Allocate new memory, initialized with zero. */
-static void *
-xgetblk(memory_block **mbp, size_t s)
+static void
+mpool_add(memory_pool *pool, void *item)
{
- memory_block *mb;
- void *p;
-
- size_t worst_align = 2 * sizeof(long) - 1;
- s = (s + worst_align) & ~worst_align;
- if ((mb = *mbp) == NULL || mb->nfree < s) {
- size_t block_size = s > mblk_size ? s : mblk_size;
- mb = xmalloc(sizeof(*mb));
- mb->start = xmalloc(block_size);
- mb->first_free = mb->start;
- mb->nfree = block_size;
- mb->next = *mbp;
- *mbp = mb;
+ if (pool->len >= pool->cap) {
+ pool->cap = 2 * pool->len + 16;
+ pool->items = xrealloc(pool->items,
+ sizeof(*pool->items) * pool->cap);
}
-
- p = mb->first_free;
- mb->first_free = (char *)mb->first_free + s;
- mb->nfree -= s;
- (void)memset(p, 0, s);
- return p;
+ pool->items[pool->len++] = item;
}
-/* Free all blocks from list *fmbp. */
static void
-xfreeblk(memory_block **fmbp)
+mpool_free(memory_pool *pool)
{
- memory_block *mb;
- while ((mb = *fmbp) != NULL) {
- *fmbp = mb->next;
- free(mb);
- }
+ for (; pool->len > 0; pool->len--)
+ free(pool->items[pool->len - 1]);
}
-void
-initmem(void)
+static void *
+mpool_zero_alloc(memory_pool *pool, size_t size)
{
- mblk_size = mem_block_size();
- mblks = xcalloc(nmblks = ML_INC, sizeof(*mblks));
+ void *mem = xmalloc(size);
+ memset(mem, 0, size);
+ mpool_add(pool, mem);
+ return mem;
+}
Home |
Main Index |
Thread Index |
Old Index