Skip to content

Commit

Permalink
hw/sparse-mem: clear memory on reset
Browse files Browse the repository at this point in the history
We use sparse-mem for fuzzing. For long-running fuzzing processes, we
eventually end up with many allocated sparse-mem pages. To avoid this,
clear the allocated pages on system-reset.

Signed-off-by: Alexander Bulekov <alxndr@bu.edu>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
  • Loading branch information
a1xndr committed Feb 17, 2023
1 parent 6dffbe3 commit 66169c3
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion hw/mem/sparse-mem.c
Expand Up @@ -77,6 +77,13 @@ static void sparse_mem_write(void *opaque, hwaddr addr, uint64_t v,

}

static void sparse_mem_enter_reset(Object *obj, ResetType type)
{
SparseMemState *s = SPARSE_MEM(obj);
g_hash_table_remove_all(s->mapped);
return;
}

static const MemoryRegionOps sparse_mem_ops = {
.read = sparse_mem_read,
.write = sparse_mem_write,
Expand Down Expand Up @@ -123,20 +130,24 @@ static void sparse_mem_realize(DeviceState *dev, Error **errp)

assert(s->baseaddr + s->length > s->baseaddr);

s->mapped = g_hash_table_new(NULL, NULL);
s->mapped = g_hash_table_new_full(NULL, NULL, NULL,
(GDestroyNotify)g_free);
memory_region_init_io(&s->mmio, OBJECT(s), &sparse_mem_ops, s,
"sparse-mem", s->length);
sysbus_init_mmio(sbd, &s->mmio);
}

static void sparse_mem_class_init(ObjectClass *klass, void *data)
{
ResettableClass *rc = RESETTABLE_CLASS(klass);
DeviceClass *dc = DEVICE_CLASS(klass);

device_class_set_props(dc, sparse_mem_properties);

dc->desc = "Sparse Memory Device";
dc->realize = sparse_mem_realize;

rc->phases.enter = sparse_mem_enter_reset;
}

static const TypeInfo sparse_mem_types[] = {
Expand Down

0 comments on commit 66169c3

Please sign in to comment.