xen/rangeset: one bugfix and unit testing
cover xen/rangeset: one bugfix and unit testing
Hello,
Rangesets are extensively used inside of Xen, yet we had no unit tests to ensure it's correctness. rangeset_remove_range() is in fact bogus and will generate an incorrect output rangeset depending on the inputs.
Patch 1 fixes the bug, patch 2 adds some basic unit testing to ensure the fix done by patch 1 is correct. The unit testing should likely be expanded (in a different series) to further assert the correctness of other rangeset operations.
Thanks, Roger.
Roger Pau Monne (2):
xen/rangeset: fix incorrect subtraction tootls/tests: introduce unit tests for rangesets
tools/tests/Makefile | 1 + tools/tests/rangeset/Makefile | 45 ++++++ tools/tests/rangeset/harness.h | 71 +++++++++ tools/tests/rangeset/test-rangeset.c | 228 +++++++++++++++++++++++++++ xen/common/rangeset.c | 3 +- 5 files changed, 347 insertions(+), 1 deletion(-) create mode 100644 tools/tests/rangeset/Makefile create mode 100644 tools/tests/rangeset/harness.h create mode 100644 tools/tests/rangeset/test-rangeset.c
Signature · 1 line
2.48.1
PATCH 1/2 xen/rangeset: fix incorrect subtraction
Given the following rangset operation:
{ [0, 1], [4, 5] } - { [3, 4] }
The current rangeset logic will output a rangeset:
{ [0, 2], [5, 5] }
This is incorrect, and also has the undesirable property of being bogus in a way that the resulting rangeset is expanded.
Fix this by making sure the bounds are correctly checked before modifying the previous range.
--- xen/common/rangeset.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
xen/common/rangeset.c +2 -1
@@ -227,7 +227,8 @@ int rangeset_remove_range(if ( x->s < s ) { - x->e = s -1 ; +if ( x->e >= s ) + x->e = s -1 ; x =next_range (r, x); }
Signature · 1 line
2.48.1
Re: [PATCH 1/2] xen/rangeset: fix incorrect subtraction
Quoted 8 lines · Roger Pau Monne
Given the following rangset operation:
{ [0, 1], [4, 5] } - { [3, 4] }
The current rangeset logic will output a rangeset:
{ [0, 2], [5, 5] }
Quoted 8 lines
This is incorrect, and also has the undesirable property of being bogus in a way that the resulting rangeset is expanded.
Fix this by making sure the bounds are correctly checked before modifying the previous range.
Fixes 484a058c4828 ('Add auto-destructing per-domain rangeset data structure...')Signed-off-by Roger Pau Monné <[email protected]>
PATCH 2/2 tootls/tests: introduce unit tests for rangesets
In reply to Roger Pau MonneIntroduce some basic infrastructure for doing rangeset unit tests, and add a few tests that ensure correctness of rangeset subtraction.
--- tools/tests/Makefile | 1 + tools/tests/rangeset/Makefile | 45 ++++++ tools/tests/rangeset/harness.h | 71 +++++++++ tools/tests/rangeset/test-rangeset.c | 228 +++++++++++++++++++++++++++ 4 files changed, 345 insertions(+) create mode 100644 tools/tests/rangeset/Makefile create mode 100644 tools/tests/rangeset/harness.h create mode 100644 tools/tests/rangeset/test-rangeset.c
tools/tests/Makefile +1 -0
@@ -10,6 +10,7 @@ SUBDIRS-$(CONFIG_X86) += x86_emulator endif SUBDIRS-y += xenstore SUBDIRS-y += depriv +SUBDIRS-y += rangeset SUBDIRS-y += vpci SUBDIRS-y += paging-mempool
tools/tests/rangeset/Makefile new file +45 -0
45 lines not on this page · raw message
tools/tests/rangeset/harness.h new file +71 -0
71 lines not on this page · raw message
tools/tests/rangeset/test-rangeset.c new file +228 -0
228 lines not on this page · raw message
Signature · 1 line
2.48.1
Re: [PATCH 2/2] tootls/tests: introduce unit tests for rangesets
Quoted 4 lines · Roger Pau Monne
Introduce some basic infrastructure for doing rangeset unit tests, and add a few tests that ensure correctness of rangeset subtraction.
Signed-off-by Roger Pau Monné <[email protected]>
It's okay as is, so:
Nevertheless a couple of remarks.
Quoted 35 lines
tools/tests/rangeset/Makefile new file +32
-0@@ -0,0 +1,45 @@ +XEN_ROOT=$(CURDIR)/../../.. +include $(XEN_ROOT)/tools/Rules.mk + +TARGET := test-rangeset + +.PHONY: all +all: $(TARGET) + +.PHONY: run +run: $(TARGET) + ./$< + +.PHONY: clean +clean: + $(RM) -- *.o $(TARGET) $(DEPS_RM) + +.PHONY: distclean +distclean: clean + $(RM) -- *~ + +.PHONY: install +install: all + $(INSTALL_DIR) $(DESTDIR)$(LIBEXEC_BIN) + $(INSTALL_PROG) $(TARGET) $(DESTDIR)$(LIBEXEC_BIN) + +.PHONY: uninstall +uninstall: + $(RM) -- $(addprefix $(DESTDIR)$(LIBEXEC_BIN)/,$(TARGET)) + +list.h: $(XEN_ROOT)/xen/include/xen/list.h +rangeset.h: $(XEN_ROOT)/xen/include/xen/rangeset.h +list.h rangeset.h:
tools/tests/rangeset/Makefile line 42
Quoted 10 lines
tools/tests/rangeset/Makefile new file +10
-0+ sed -e '/#include/d' <$< >$@ + +rangeset.c: $(XEN_ROOT)/xen/common/rangeset.c list.h rangeset.h + # Remove includes and add the test harness header + sed -e '/#include/d' -e '1s/^/#include "harness.h"/' <$< >$@ + +CFLAGS += -D__XEN_TOOLS__ +CFLAGS += $(APPEND_CFLAGS) +CFLAGS += $(CFLAGS_xeninclude) +LDFLAGS += $(APPEND_LDFLAGS)
The mix of padding ahead of the += is a little odd here.
Quoted 26 lines
tools/tests/rangeset/harness.h new file +23
-0@@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Unit tests for rangesets. +* +* Copyright (C) 2025 Cloud Software Group +*/ + +#ifndef _TEST_HARNESS_ +#define _TEST_HARNESS_ + +#include <assert.h> +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <xen-tools/common-macros.h> + +#define smp_wmb () +#define __must_check __attribute__((__warn_unused_result__))
tools/tests/rangeset/harness.h line 33
Quoted 10 lines
tools/tests/rangeset/harness.h new file +10
-0+#define cf_check + +#define BUG_ON (x) assert(!(x)) +#define ASSERT (x) assert(x) + +#include "list.h" +#include "rangeset.h" + +typedef bool rwlock_t ; +typedef bool spinlock_t ;
Are spinlocks really required for the rangeset code?
Quoted 111 lines
tools/tests/rangeset/test-rangeset.c new file +108
-0@@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Unit tests for rangesets. +* +* Copyright (C) 2025 Cloud Software Group +*/ + +#include "harness.h" + +struct range { +unsigned long start, end; +}; + +struct action { +enum { +ADD , +REMOVE , + } action; +struct range r; +}; + +#define DECLARE_ACTIONS (nr) static const struct action actions ## nr [] +#define DECLARE_RESULTS (nr) static const struct range results ## nr [] + +/* +* Subtract range with tail overlap on existing range: +* +* { 0, 1, 4, 5 } - { 3, 4 } = { 0, 1, 5, 5 } +*/ +DECLARE_ACTIONS(0) = { + { ADD, { 0, 1 } }, + { ADD, { 4, 5 } }, + { REMOVE, { 3, 4 } }, +}; +DECLARE_RESULTS(0) = { + { 0, 1 }, { 5, 5 }, +}; + +/* +* Subtract range with complete and tail overlap on existing ranges: +* +* { 0, 1, 4, 5, 7, 8 } - { 3, 4, 5, 6, 7 } = { 0, 1, 8 } +*/ +DECLARE_ACTIONS(1) = { + { ADD, { 0, 1 } }, + { ADD, { 4, 5 } }, + { ADD, { 7, 8 } }, + { REMOVE, { 3, 7 } }, +}; +DECLARE_RESULTS(1) = { + { 0, 1 }, { 8, 8 }, +}; + +/* +* Subtract range with no overlap: +* +* { 0, 1, 4, 5 } - { 2, 3 } = { 0, 1, 4, 5 } +*/ +DECLARE_ACTIONS(2) = { + { ADD, { 0, 1 } }, + { ADD, { 4, 5 } }, + { REMOVE, { 2, 3 } }, +}; +DECLARE_RESULTS(2) = { + { 0, 1 }, { 4, 5 }, +}; + +/* +* Subtract range with partial overlap on two existing ranges: +* +* { 0, 1, 4, 5 } - { 1, 4 } = { 0, 5 } +*/ +DECLARE_ACTIONS(3) = { + { ADD, { 0, 1 } }, + { ADD, { 4, 5 } }, + { REMOVE, { 1, 4 } }, +}; +DECLARE_RESULTS(3) = { + { 0, 0 }, { 5, 5 }, +}; + +static const struct test { + unsigned int nr_actions, nr_results; + const struct action *actions; + const struct range *result; +} tests[] = { +#define DECLARE_TEST(nr) \ + { \ + .actions = actions ## nr, \ + .nr_actions = ARRAY_SIZE(actions ## nr), \ + .result = results ## nr, \ + .nr_results = ARRAY_SIZE(results ## nr), \ + } + +DECLARE_TEST (0 ), +DECLARE_TEST (1 ), +DECLARE_TEST (2 ), +DECLARE_TEST (3 ), + +#undef DECLARE_TEST +}; + +static int print_range (unsigned long s,unsigned long e,void *data) +{ +printf ("[%ld, %ld]\n" , s, e); + +return 0 ; +}
tools/tests/rangeset/test-rangeset.c line 118
Quoted 10 lines
tools/tests/rangeset/test-rangeset.c new file +10
-0+ +static int count_ranges (unsigned long s,unsigned long e,void *data) +{ +unsigned int *nr = data; + + ++*nr; +return 0 ; +} + +const struct range *expected;
static?
Jan
Re: [PATCH 2/2] tootls/tests: introduce unit tests for rangesets
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Roger Pau Monne
tools/tests/rangeset/harness.h new file +27
-0@@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Unit tests for rangesets. +* +* Copyright (C) 2025 Cloud Software Group +*/ + +#ifndef _TEST_HARNESS_ +#define _TEST_HARNESS_ + +#include <assert.h> +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <xen-tools/common-macros.h> + +#define smp_wmb () +#define __must_check __attribute__((__warn_unused_result__)) +#define cf_check + +#define BUG_ON (x) assert(!(x)) +#define ASSERT (x) assert(x)
Quoted 8 lines
tools/tests/rangeset/harness.h line 33
Quoted 6 lines
tools/tests/rangeset/harness.h new file +6
-0+ +#include "list.h" +#include "rangeset.h" + +typedef bool rwlock_t ; +typedef bool spinlock_t ;Are spinlocks really required for the rangeset code?
It's noted a bit below in this same file:
/* For rangeset_domain_{initialize,printk}() */
Otherwise rangeset.c won't build.
Will adjust the rest of your comments before commit, thanks!
Roger.