diff options
author | Tom Hughes <tomhughes@chromium.org> | 2023-03-27 15:44:52 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-04-07 22:33:28 +0000 |
commit | 4dcdae2474cdab1cf7dffc2828d73336a9b4260a (patch) | |
tree | 04aa5f5d7def491ece3021061e9e729933c661a4 | |
parent | b5eb344ca4965a798b8c849a9294f2f5798fcc3d (diff) | |
download | chrome-ec-4dcdae2474cdab1cf7dffc2828d73336a9b4260a.tar.gz |
test: Add a test for malloc/free
BRANCH=none
BUG=b:274737509
TEST=./test/run_device_tests.py --board bloonchipper -t malloc
=> PASS
TEST=./test/run_device_tests.py --board dartmonkey -t malloc
=> PASS
TEST=make run-malloc
Change-Id: Ida58335dcacbeefad56804246897810936c98546
Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4390609
Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
-rw-r--r-- | board/hatch_fp/build.mk | 1 | ||||
-rw-r--r-- | board/nocturne_fp/build.mk | 1 | ||||
-rw-r--r-- | test/build.mk | 2 | ||||
-rw-r--r-- | test/malloc.c | 94 | ||||
-rw-r--r-- | test/malloc.tasklist | 9 | ||||
-rwxr-xr-x | test/run_device_tests.py | 1 |
6 files changed, 108 insertions, 0 deletions
diff --git a/board/hatch_fp/build.mk b/board/hatch_fp/build.mk index a13ec273d2..7ded3aeb6a 100644 --- a/board/hatch_fp/build.mk +++ b/board/hatch_fp/build.mk @@ -44,6 +44,7 @@ test-list-y=\ global_initialization \ libc_printf \ libcxx \ + malloc \ mpu \ mutex \ panic \ diff --git a/board/nocturne_fp/build.mk b/board/nocturne_fp/build.mk index aa906b8cf1..dc99c72f85 100644 --- a/board/nocturne_fp/build.mk +++ b/board/nocturne_fp/build.mk @@ -44,6 +44,7 @@ test-list-y=\ global_initialization \ libc_printf \ libcxx \ + malloc \ mpu \ mutex \ panic \ diff --git a/test/build.mk b/test/build.mk index 2c5ff67b69..d03ae345cb 100644 --- a/test/build.mk +++ b/test/build.mk @@ -71,6 +71,7 @@ test-list-host += kb_scan_strict test-list-host += lid_sw test-list-host += lightbar test-list-host += mag_cal +test-list-host += malloc test-list-host += math_util test-list-host += motion_angle test-list-host += motion_angle_tablet @@ -221,6 +222,7 @@ kb_scan_strict-y=kb_scan.o lid_sw-y=lid_sw.o lightbar-y=lightbar.o mag_cal-y=mag_cal.o +malloc-y=malloc.o math_util-y=math_util.o motion_angle-y=motion_angle.o motion_angle_data_literals.o motion_common.o motion_angle_tablet-y=motion_angle_tablet.o motion_angle_data_literals_tablet.o motion_common.o diff --git a/test/malloc.c b/test/malloc.c new file mode 100644 index 0000000000..37c566b2ec --- /dev/null +++ b/test/malloc.c @@ -0,0 +1,94 @@ +/* Copyright 2023 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "common.h" +#include "shared_mem.h" +#include "stdlib.h" +#include "test_util.h" + +#include <malloc.h> + +struct malloc_data { + uint32_t size; + uint8_t val; + uint8_t *data; +}; + +static struct malloc_data test_data[] = { + { .size = 15, .val = 1, .data = NULL }, + { .size = 1024, .val = 2, .data = NULL }, + { .size = 86096, .val = 3, .data = NULL } +}; + +test_static int test_malloc_different_sizes(void) +{ + /* Trim to make sure that previous tests haven't fragmented the heap. */ + malloc_trim(0); + + for (int i = 0; i < ARRAY_SIZE(test_data); i++) { + uint8_t *volatile ptr = malloc(test_data[i].size); + TEST_NE(ptr, NULL, "%p"); + test_data[i].data = ptr; + for (int j = 0; j < test_data[i].size; j++) { + ptr[j] = test_data[i].val; + } + } + + for (int i = 0; i < ARRAY_SIZE(test_data); i++) { + uint8_t *ptr = test_data[i].data; + /* Using TEST_EQ results in too much logging. */ + for (int j = 0; j < test_data[i].size; j++) { + if (ptr[j] != test_data[i].val) { + TEST_ASSERT(false); + } + } + } + + for (int i = 0; i < ARRAY_SIZE(test_data); i++) { + free(test_data[i].data); + } + + return EC_SUCCESS; +} + +test_static int test_free_null(void) +{ + free(NULL); + return EC_SUCCESS; +} + +test_static int test_malloc_large(void) +{ + /* Trim to make sure that previous tests haven't fragmented the heap. */ + malloc_trim(0); + uint8_t *volatile ptr = malloc(shared_mem_size() * 0.8); + TEST_NE(ptr, NULL, "%p"); + free(ptr); + return EC_SUCCESS; +} + +test_static int test_malloc_too_large(void) +{ + /* Trim to make sure that previous tests haven't fragmented the heap. */ + malloc_trim(0); + uint8_t *volatile ptr = malloc(shared_mem_size() + 1); + TEST_EQ(ptr, NULL, "%p"); + free(ptr); + return EC_SUCCESS; +} + +void run_test(int argc, const char **argv) +{ + test_reset(); + + RUN_TEST(test_free_null); + RUN_TEST(test_malloc_different_sizes); + RUN_TEST(test_malloc_large); + + if (!IS_ENABLED(BOARD_HOST)) + RUN_TEST(test_malloc_too_large); + + test_print_result(); +} diff --git a/test/malloc.tasklist b/test/malloc.tasklist new file mode 100644 index 0000000000..d1920322a9 --- /dev/null +++ b/test/malloc.tasklist @@ -0,0 +1,9 @@ +/* Copyright 2023 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * See CONFIG_TASK_LIST in config.h for details. + */ +#define CONFIG_TEST_TASK_LIST diff --git a/test/run_device_tests.py b/test/run_device_tests.py index 65241177b2..f385441f6d 100755 --- a/test/run_device_tests.py +++ b/test/run_device_tests.py @@ -275,6 +275,7 @@ class AllTests: ), TestConfig(test_name="global_initialization"), TestConfig(test_name="libcxx"), + TestConfig(test_name="malloc", imagetype_to_use=ImageType.RO), TestConfig( config_name="mpu_ro", test_name="mpu", |