From 7c14dc7f77705f79ba49e7f0b2879986fea70fea Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 6 Sep 2022 20:26:59 -0600 Subject: test: Fix missing livetree test runs At present the live tree tests are not run on sandbox. This bug is in two parts, with a duplicate flag value and incorrect logic in the test runner. This was not noticed because the bug was fixed in a later commit and does not cause test failures. Fix this. Fixes: 7b1dfc9fd7e ("dm: core: Prepare for updating the device tree with ofnode") Signed-off-by: Simon Glass --- test/test-main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'test/test-main.c') diff --git a/test/test-main.c b/test/test-main.c index ae34002a3d4..90a324bf70c 100644 --- a/test/test-main.c +++ b/test/test-main.c @@ -343,8 +343,7 @@ static int ut_run_test_live_flat(struct unit_test_state *uts, /* Run with the live tree if possible */ runs = 0; if (CONFIG_IS_ENABLED(OF_LIVE)) { - if (!(test->flags & - (UT_TESTF_FLAT_TREE | UT_TESTF_LIVE_OR_FLAT))) { + if (!(test->flags & UT_TESTF_FLAT_TREE)) { uts->of_live = true; ut_assertok(ut_run_test(uts, test, test->name)); runs++; -- cgit v1.2.3 From 62d638386c17d17b929ad10956c7f60825335a4e Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 6 Sep 2022 20:27:00 -0600 Subject: test: Support testing malloc() failures It is helpful to test that out-of-memory checks work correctly in code that calls malloc(). Add a simple way to force failure after a given number of malloc() calls. Fix a header guard to avoid a build error on sandbox_vpl. Signed-off-by: Simon Glass Reviewed-by: Sean Anderson --- test/test-main.c | 1 + 1 file changed, 1 insertion(+) (limited to 'test/test-main.c') diff --git a/test/test-main.c b/test/test-main.c index 90a324bf70c..7a3871624cb 100644 --- a/test/test-main.c +++ b/test/test-main.c @@ -47,6 +47,7 @@ static int dm_test_pre_run(struct unit_test_state *uts) uts->force_fail_alloc = false; uts->skip_post_probe = false; gd->dm_root = NULL; + malloc_disable_testing(); if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA)) memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count)); arch_reset_for_test(); -- cgit v1.2.3 From 0e4b697f884d1f2190a9972b662abc8498159333 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 6 Sep 2022 20:27:05 -0600 Subject: test: Make a copy of the device tree before running a test When the flat device tree changes it can mess up the live tree since that uses the flat tree for its strings. This affects only a few sandbox tests which modify the device tree, but the number will grow as ofnode support for writing improves. While the control FDT is not intended to change while U-Boot is running, some tests do so. For example, the ofnode interface only supports modifying properties in the control FDT, so tests must use that. To solve this problem, keep a copy of the FDT and restore it as needed when the test is finished. The copy only happens on sandbox (except SPL builds), to reduce memory usage and because these tests are not useful on other boards. For other boards, a checksum is taken to ensure that nothing changes. It would be possible to always checksum the FDT on sandbox and only restore it if needed, but this is slightly slower than restoring it every time, at least with crc8. Move the code which checks for success to the very end, for clarity. Signed-off-by: Simon Glass --- test/test-main.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'test/test-main.c') diff --git a/test/test-main.c b/test/test-main.c index 7a3871624cb..082821ef3e0 100644 --- a/test/test-main.c +++ b/test/test-main.c @@ -9,14 +9,51 @@ #include #include #include +#include #include #include #include #include #include +#include DECLARE_GLOBAL_DATA_PTR; +/** + * enum fdtchk_t - what to do with the device tree (gd->fdt_blob) + * + * This affects what happens with the device tree before and after a test + * + * @FDTCHK_NONE: Do nothing + * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and + * compare it afterwards to detect any changes + * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards + */ +enum fdtchk_t { + FDTCHK_NONE, + FDTCHK_CHECKSUM, + FDTCHK_COPY, +}; + +/** + * fdt_action() - get the required action for the FDT + * + * @return the action that should be taken for this build + */ +static enum fdtchk_t fdt_action(void) +{ + /* Do a copy for sandbox (but only the U-Boot build, not SPL) */ + if (CONFIG_IS_ENABLED(SANDBOX)) + return FDTCHK_COPY; + + /* For sandbox SPL builds, do nothing */ + if (IS_ENABLED(CONFIG_SANDBOX)) + return FDTCHK_NONE; + + /* For all other boards, do a checksum */ + return FDTCHK_CHECKSUM; +} + /* This is valid when a test is running, NULL otherwise */ static struct unit_test_state *cur_test_state; @@ -46,6 +83,9 @@ static int dm_test_pre_run(struct unit_test_state *uts) uts->testdev = NULL; uts->force_fail_alloc = false; uts->skip_post_probe = false; + if (fdt_action() == FDTCHK_CHECKSUM) + uts->fdt_chksum = crc8(0, gd->fdt_blob, + fdt_totalsize(gd->fdt_blob)); gd->dm_root = NULL; malloc_disable_testing(); if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA)) @@ -64,6 +104,25 @@ static int dm_test_post_run(struct unit_test_state *uts) { int id; + if (gd->fdt_blob) { + switch (fdt_action()) { + case FDTCHK_COPY: + memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size); + break; + case FDTCHK_CHECKSUM: { + uint chksum; + + chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob)); + + if (chksum != uts->fdt_chksum) + printf("Device tree changed: cannot run live tree tests\n"); + break; + } + case FDTCHK_NONE: + break; + } + } + /* * With of-platdata-inst the uclasses are created at build time. If we * destroy them we cannot get them back since uclass_add() is not @@ -443,8 +502,23 @@ int ut_run_list(const char *category, const char *prefix, uts.of_root = gd_of_root(); uts.runs_per_test = runs_per_test; + if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) { + uts.fdt_size = fdt_totalsize(gd->fdt_blob); + uts.fdt_copy = os_malloc(uts.fdt_size); + if (!uts.fdt_copy) { + printf("Out of memory for device tree copy\n"); + return -ENOMEM; + } + memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size); + } ret = ut_run_tests(&uts, prefix, tests, count, select_name); + /* Best efforts only...ignore errors */ + if (has_dm_tests) + dm_test_restore(uts.of_root); + if (IS_ENABLED(CONFIG_SANDBOX)) + os_free(uts.fdt_copy); + if (ret == -ENOENT) printf("Test '%s' not found\n", select_name); else -- cgit v1.2.3 From eb6e903a569d35032ca3772d93c26fb1071ab3ae Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 6 Sep 2022 20:27:06 -0600 Subject: test: Detect a change in the device tree If the device tree changes during a test and we cannot restore it, mark it as such so that future tests which need the live tree are skipped. Signed-off-by: Simon Glass --- test/test-main.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'test/test-main.c') diff --git a/test/test-main.c b/test/test-main.c index 082821ef3e0..c12027ce680 100644 --- a/test/test-main.c +++ b/test/test-main.c @@ -79,6 +79,10 @@ static int dm_test_pre_run(struct unit_test_state *uts) { bool of_live = uts->of_live; + if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) { + printf("Cannot run live tree test as device tree changed\n"); + return -EFAULT; + } uts->root = NULL; uts->testdev = NULL; uts->force_fail_alloc = false; @@ -113,9 +117,17 @@ static int dm_test_post_run(struct unit_test_state *uts) uint chksum; chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob)); - - if (chksum != uts->fdt_chksum) + if (chksum != uts->fdt_chksum) { + /* + * We cannot run any more tests that need the + * live tree, since its strings point into the + * flat tree, which has changed. This likely + * means that at least some of the pointers from + * the live tree point to different things + */ printf("Device tree changed: cannot run live tree tests\n"); + gd->flags |= GD_FLG_FDT_CHANGED; + } break; } case FDTCHK_NONE: @@ -415,7 +427,8 @@ static int ut_run_test_live_flat(struct unit_test_state *uts, * or it is a core test. */ if (!(test->flags & UT_TESTF_LIVE_TREE) && - (!runs || ut_test_run_on_flattree(test))) { + (!runs || ut_test_run_on_flattree(test)) && + !(gd->flags & GD_FLG_FDT_CHANGED)) { uts->of_live = false; ut_assertok(ut_run_test(uts, test, test->name)); runs++; -- cgit v1.2.3 From 8d468a188f69424b3461ba681a26b95bb9679f53 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 6 Sep 2022 20:27:11 -0600 Subject: sandbox: test: Provide an easy way to use the other FDT Add a test flag which indicates that the 'other' FDT should be set up ready for use. Handle this by copying in the FDT, unflattening it for livetree tests. Free the structures when the tests have run. We cannot use the other FDT unless we are using live tree or OFNODE_MULTI_TREE is enabled, since only one tree is supported by the ofnode interface in that case. Add this condition into ut_run_test_live_flat() and update the comments. Signed-off-by: Simon Glass --- test/test-main.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'test/test-main.c') diff --git a/test/test-main.c b/test/test-main.c index c12027ce680..1fcbae3cd4e 100644 --- a/test/test-main.c +++ b/test/test-main.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -314,6 +315,20 @@ static int test_pre_run(struct unit_test_state *uts, struct unit_test *test) (test->flags & UT_TESTF_SCAN_FDT)) ut_assertok(dm_extended_scan(false)); + if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UT_TESTF_OTHER_FDT)) { + /* make sure the other FDT is available */ + ut_assertok(test_load_other_fdt(uts)); + + /* + * create a new live tree with it for every test, in case a + * test modifies the tree + */ + if (of_live_active()) { + ut_assertok(unflatten_device_tree(uts->other_fdt, + &uts->of_other)); + } + } + if (test->flags & UT_TESTF_CONSOLE_REC) { int ret = console_record_reset_enable(); @@ -342,6 +357,9 @@ static int test_post_run(struct unit_test_state *uts, struct unit_test *test) ut_assertok(cyclic_uninit()); ut_assertok(event_uninit()); + free(uts->of_other); + uts->of_other = NULL; + return 0; } @@ -412,6 +430,9 @@ static int ut_run_test_live_flat(struct unit_test_state *uts, { int runs; + if ((test->flags & UT_TESTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX)) + return -EAGAIN; + /* Run with the live tree if possible */ runs = 0; if (CONFIG_IS_ENABLED(OF_LIVE)) { @@ -423,10 +444,20 @@ static int ut_run_test_live_flat(struct unit_test_state *uts, } /* - * Run with the flat tree if we couldn't run it with live tree, - * or it is a core test. + * Run with the flat tree if: + * - it is not marked for live tree only + * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is + * not enabled (since flat tree can only support a single FDT in that + * case + * - we couldn't run it with live tree, + * - it is a core test (dm tests except video) + * - the FDT is still valid and has not been updated by an earlier test + * (for sandbox we handle this by copying the tree, but not for other + * boards) */ if (!(test->flags & UT_TESTF_LIVE_TREE) && + (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) || + !(test->flags & UT_TESTF_OTHER_FDT)) && (!runs || ut_test_run_on_flattree(test)) && !(gd->flags & GD_FLG_FDT_CHANGED)) { uts->of_live = false; @@ -529,8 +560,10 @@ int ut_run_list(const char *category, const char *prefix, /* Best efforts only...ignore errors */ if (has_dm_tests) dm_test_restore(uts.of_root); - if (IS_ENABLED(CONFIG_SANDBOX)) + if (IS_ENABLED(CONFIG_SANDBOX)) { os_free(uts.fdt_copy); + os_free(uts.other_fdt); + } if (ret == -ENOENT) printf("Test '%s' not found\n", select_name); -- cgit v1.2.3 From ee88ba71acce3455aadef18c725de715e6c9af21 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 6 Sep 2022 20:27:19 -0600 Subject: dm: core: Provide a way to reset the device tree At present there is only one device tree used by the ofnode functions, except for some esoteric use of live tree. In preparation for supporting more than one, add a way to reset the list of device trees. For now this does nothing. Signed-off-by: Simon Glass --- test/test-main.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test/test-main.c') diff --git a/test/test-main.c b/test/test-main.c index 1fcbae3cd4e..d74df297c43 100644 --- a/test/test-main.c +++ b/test/test-main.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -99,6 +100,7 @@ static int dm_test_pre_run(struct unit_test_state *uts) /* Determine whether to make the live tree available */ gd_set_of_root(of_live ? uts->of_root : NULL); + oftree_reset(); ut_assertok(dm_init(of_live)); uts->root = dm_root(); -- cgit v1.2.3