Skip to contentSkip to conversation

lib/assoc_array: fix root-shortcut split lookup regression, add KUnit tests

PATCH New William Chuang 3 participants 5 messages Nov 16

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover lib/assoc_array: fix root-shortcut split lookup regression, add KUnit tests

This series adds a KUnit test suite for lib/assoc_array.c and fixes a lookup bug that suite exposes.

When a root shortcut is split at the first level-segment boundary, the replacement node is installed one segment too shallow. Lookups then walk the tree at the wrong depth, so a fraction of previously inserted keys can no longer be found, even though they remain in the array.

Patch 1 adds the test suite; it passes on mainline. Patch 2 fixes the split path in assoc_array_insert() and adds a regression test that fails without the fix, keeping the series bisection-clean.

The fix was checked by exhaustive fan-out enumeration, randomized stress runs, and KASAN; the patch 2 commit message describes the mechanism in detail.

William Chuang (2):
lib/assoc_array: add KUnit test suite lib/assoc_array: fix lookups after splitting a root shortcut

 lib/Kconfig.debug             |  17 +
 lib/assoc_array.c             |   3 +-
 lib/tests/Makefile            |   1 +
 lib/tests/assoc_array_kunit.c | 991 ++++++++++++++++++++++++++++++++++
 4 files changed, 1011 insertions(+), 1 deletion(-)
 create mode 100644 lib/tests/assoc_array_kunit.c
Signature · 1 line
2.39.1
#1 lore raw

PATCH 1/2 lib/assoc_array: add KUnit test suite

lib/assoc_array.c has had no tests since it was added in 2013, though it indexes the keys held in every keyring and has needed several correctness fixes over the years. Eric Biggers noted the gap while reviewing one such fix and observed that the code runs easily in userspace, so KUnit is a good fit.

Add a suite that drives an associative array through insertion, replacement, deletion, clearance, iteration and garbage collection while keeping a parallel model of the expected contents. After each operation it checks that every present key is found, every absent key is not, iteration visits each object exactly once, and nr_leaves_on_tree matches the model. Objects are reference counted the way keyrings count keys, so a leak, double free or premature free shows up as a non-zero live count or a refcount warning; the suite passes under KASAN.

Keys are generated in several layouts (dense integers, uniformly random, high-bit-only and sparse clusters) to drive the tree into wide nodes, deep clusters and shortcuts, and the index-key space is kept small enough that near-miss probes exercise shortcut matching. Some cases rebuild the tree shapes behind earlier fixes and name the commit they guard: the node split of commit ea6789980fda ("assoc_array: Fix a buggy node-splitting case", CVE-2017-12193), shortcut creation of commit bb2ba2d75a2d ("assoc_array: Fix shortcut creation"), the leaf/meta comparison of commit 8d4a2ec1e0b4 ("assoc_array: don't call compare_object() on a node") and the final-word trim of commit a82c8a05e86f ("assoc_array: trim the final shortcut word using the current chunk end"). Reverting any of those four fixes makes the suite fail.

Run with:

./tools/testing/kunit/kunit.py run assoc_array

---
 lib/Kconfig.debug             |  17 +
 lib/tests/Makefile            |   1 +
 lib/tests/assoc_array_kunit.c | 963 ++++++++++++++++++++++++++++++++++
 3 files changed, 981 insertions(+)
 create mode 100644 lib/tests/assoc_array_kunit.c
lib/Kconfig.debug +17 -0
index 2d22638ec..d68f29764 100644
@@ -2830,6 +2830,23 @@ config SYSCTL_KUNIT_TEST
 
 	  If unsure, say N.
 
+config ASSOC_ARRAY_KUNIT_TEST
+	tristate "KUnit test for the generic associative array" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	select ASSOCIATIVE_ARRAY
+	default KUNIT_ALL_TESTS
+	help
+	  This builds the KUnit test suite for the generic associative array
+	  (lib/assoc_array.c), which indexes the keys held in keyrings.
+	  It checks insertion, deletion, garbage collection and lookup
+	  against a model of the expected contents, including the tree
+	  shapes behind earlier fixes to the implementation.
+
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 config ERRSEQ_KUNIT_TEST
 	tristate "KUnit test for errseq" if !KUNIT_ALL_TESTS
 	depends on KUNIT
lib/tests/Makefile +1 -0
index df0537e1b..3e9c4a67d 100644
@@ -5,6 +5,7 @@
 # KUnit tests
 CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN)
 obj-$(CONFIG_842_DECOMPRESS_KUNIT_TEST) += 842_decompress_kunit.o
+obj-$(CONFIG_ASSOC_ARRAY_KUNIT_TEST) += assoc_array_kunit.o
 obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o
 obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o
 obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
lib/tests/assoc_array_kunit.c new file +963 -0

963 lines not on this page · raw message

Signature · 1 line
2.39.1
#2 lore raw

Re: [PATCH 1/2] lib/assoc_array: add KUnit test suite

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] lib/assoc_array: unexported symbols break modular ASSOC_ARRAY_KUNIT_TEST=m build --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] lib/assoc_array: unexported symbols break modular ASSOC_ARRAY_KUNIT_TEST=m build
--

commit 07438606f16e23d96f51f8a13d17b392d4d4bc67
Author: William Chuang <[email protected]>

lib/assoc_array: add KUnit test suite

This commit adds a KUnit test suite for the generic associative array in lib/assoc_array.c, testing insertion, replacement, deletion, clearance, iteration, and garbage collection against an expected model.

lib/Kconfig.debug config SYSCTL_KUNIT_TEST · line 2863

Quoted 13 lines
lib/Kconfig.debug +5 -0
index 1dac2674a9838..157f350eee5dc 100644
@@ -2856,6 +2856,23 @@ config SYSCTL_KUNIT_TEST
 
 	  If unsure, say N.
 
+config ASSOC_ARRAY_KUNIT_TEST
+	tristate "KUnit test for the generic associative array" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	select ASSOCIATIVE_ARRAY
+	default KUNIT_ALL_TESTS

[Severity: Medium]
Does building with CONFIG_ASSOC_ARRAY_KUNIT_TEST=m (such as under allmodconfig) fail at modpost?

CONFIG_ASSOCIATIVE_ARRAY is a bool option that compiles lib/assoc_array.o into vmlinux, and lib/assoc_array.c does not export any of the assoc_array_*() functions with EXPORT_SYMBOL(). Either ASSOC_ARRAY_KUNIT_TEST needs to be bool with "depends on KUNIT=y", or the assoc_array_*() functions need to be exported.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH 1/2] lib/assoc_array: add KUnit test suite

In reply to William Chuang
kernel test robot noticed the following build errors:

Hi William,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-nonmm-unstable]
[also build test ERROR on akpm-mm/mm-everything linus/master v7.4-rc3 next-20261113] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/William-Chuang/lib-assoc_array-add-KUnit-test-suite/20261116-054121
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-nonmm-unstable
patch link:    https://lore.kernel.org/r/20261116054112.39078-2-founder%40logarcheon.com
patch subject: [PATCH 1/2] lib/assoc_array: add KUnit test suite
config: i386-buildonly-randconfig-1001-20261116 (https://download.01.org/0day-ci/archive/20261117/[email protected]/config)
compiler: gcc-12 (Debian 12.4.0-5) 12.4.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261117/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: lib/tests/assoc_array_kunit.ko: symbol 'assoc_array_destroy' undefined!
>> ERROR: modpost: lib/tests/assoc_array_kunit.ko: symbol 'assoc_array_delete' undefined!
>> ERROR: modpost: lib/tests/assoc_array_kunit.ko: symbol 'assoc_array_apply_edit' undefined!
>> ERROR: modpost: lib/tests/assoc_array_kunit.ko: symbol 'assoc_array_gc' undefined!
>> ERROR: modpost: lib/tests/assoc_array_kunit.ko: symbol 'assoc_array_find' undefined!
>> ERROR: modpost: lib/tests/assoc_array_kunit.ko: symbol 'assoc_array_iterate' undefined!
>> ERROR: modpost: lib/tests/assoc_array_kunit.ko: symbol 'assoc_array_insert' undefined!
>> ERROR: modpost: lib/tests/assoc_array_kunit.ko: symbol 'assoc_array_clear' undefined!
>> ERROR: modpost: lib/tests/assoc_array_kunit.ko: symbol 'assoc_array_insert_set_object' undefined!
>> ERROR: modpost: lib/tests/assoc_array_kunit.ko: symbol 'assoc_array_cancel_edit' undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

PATCH 2/2 lib/assoc_array: fix lookups after splitting a root shortcut

In reply to William Chuang

assoc_array_insert_mid_shortcut() splits a shortcut when a newly inserted leaf diverges from it partway along. When the divergence point (diff) lies exactly one segment past the level at which the shortcut was entered, the function takes its "no pre-shortcut" path and attaches the new node directly in the shortcut's old place.

For a shortcut that hangs off a parent node that is correct: the parent already consumes the entering segment, so the new node ends up indexed at the right level. For a shortcut at the root of the tree (back_pointer == NULL) there is no parent to consume that segment, so the new node is installed as the root and walked from level 0 even though it was built to be indexed at level 'diff'. Every node below it is then walked one segment too shallow and assoc_array_find() can no longer reach most of the leaves. The leaves are still present -- assoc_array_iterate() visits them all and nr_leaves_on_tree stays correct -- so the data is intact but becomes unfindable.

This is not seen in normal use because index keys are recommended to begin with a hash, which scatters them and makes a shared multi-segment prefix with an earlier-diverging sibling very unlikely. With poorly scattered keys, which the documentation says must still work correctly, as few as 18 insertions can lose lookups.

Create the pre-shortcut in the root case as well, so the leading segment(s) that the shortcut skipped stay covered and the new node is indexed at the correct level. Non-root shortcuts are unaffected: the added condition is guarded by back_pointer == NULL.

Add a KUnit regression case, aa_test_root_shortcut_split, that inserts 18 poorly scattered keys and checks every one is still found; it fails before this change and passes after.

Fixes 3cb989501c26 ("Add a generic associative array implementation.")
---
 lib/assoc_array.c             |  3 ++-
 lib/tests/assoc_array_kunit.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)
lib/assoc_array.c +2 -1
index b6c9723e1..272aeeedd 100644
@@ -844,7 +844,8 @@ static bool assoc_array_insert_mid_shortcut(struct assoc_array_edit *edit,
 	 * parent.
 	 */
 	level += ASSOC_ARRAY_LEVEL_STEP;
-	if (diff > level) {
+	if (diff > level ||
+	    (!shortcut->back_pointer && diff > level - ASSOC_ARRAY_LEVEL_STEP)) {
 		pr_devel("pre-shortcut %d...%d\n", level, diff);
 		keylen = round_up(diff, ASSOC_ARRAY_KEY_CHUNK_SIZE);
 		keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
lib/tests/assoc_array_kunit.c +28 -0
index bd253b6f6..427c71d37 100644
@@ -936,6 +936,33 @@ static void aa_test_random_ops(struct kunit *test)
 	}
 }
 
+/*
+ * Poorly scattered keys: 17 keys share an 8-bit prefix and cluster behind a
+ * shortcut at the root of the tree, then one key diverges a nibble earlier and
+ * splits that root shortcut.  The documentation permits poor scattering (it only
+ * widens and deepens the tree), so every key must still be found.
+ */
+static void aa_test_root_shortcut_split(struct kunit *test)
+{
+	struct aa_ctx *ctx = aa_ctx_new(test, 18, 900);
+	unsigned int i;
+
+	for (i = 0; i < ctx->nr_slots; i++) {
+		struct aa_key *key = &ctx->slots[i].key;
+
+		memset(key, 0, sizeof(*key));
+		aa_key_set_nibble(key, 4, i < 17 ? 0 : 1);	/* nibble 1 */
+		aa_key_set_nibble(key, 8, i & 0xf);		/* nibble 2 */
+		aa_key_set_nibble(key, 12, (i >> 4) & 0xf);	/* nibble 3 */
+		key->w[AA_KEY_WORDS - 1] = 0x100 + i;		/* globally unique */
+	}
+	for (i = 0; i < ctx->nr_slots; i++)
+		aa_insert(ctx, i);
+	aa_check(ctx);
+	aa_check_near_misses(ctx, 200);
+	aa_ctx_finish(ctx);
+}
+
 static struct kunit_case assoc_array_test_cases[] = {
 	KUNIT_CASE(aa_test_empty),
 	KUNIT_CASE(aa_test_replace),
@@ -948,6 +975,7 @@ static struct kunit_case assoc_array_test_cases[] = {
 	KUNIT_CASE_PARAM(aa_test_shortcut, aa_shortcut_gen_params),
 	KUNIT_CASE(aa_test_gc_fold),
 	KUNIT_CASE(aa_test_gc_all_or_nothing),
+	KUNIT_CASE(aa_test_root_shortcut_split),
 	KUNIT_CASE_SLOW(aa_test_random_ops),
 	{}
 };
Signature · 1 line
2.39.1