Edward O'Callaghan submitted this change.

View Change

Approvals: build bot (Jenkins): Verified Angel Pons: Looks good to me, approved Edward O'Callaghan: Looks good to me, approved
tests: Wrap strdup to help cmocka recognise memory allocation

This is a known issue in cmocka (see
https://github.com/clibs/cmocka/issues/17) where cmocka does not
recognise memory allocation happening inside strdup, and then later
throws an error when the memory is freed. If the issue is fixed at
some point, this workaround can be removed.

Given that cmocka already overrides malloc, calloc, realloc, free,
adding strdup there seems fine.

Existing tests now can (and have to) free the memory they allocated
by strdup, and this is in the same patch.

BUG=b:193584590
TEST=ninja test

Change-Id: I56aef6b342752d80995c36ab075b12198fc101d9
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/56323
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
---
M tests/init_shutdown.c
M tests/meson.build
M tests/tests.c
3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/tests/init_shutdown.c b/tests/init_shutdown.c
index 8469902..79918e6 100644
--- a/tests/init_shutdown.c
+++ b/tests/init_shutdown.c
@@ -23,13 +23,17 @@
{
(void) state; /* unused */

+ char *param_dup = strdup(param);
+
printf("Testing programmer_init for programmer=%s ...\n", prog->name);
- assert_int_equal(0, programmer_init(prog, strdup(param)));
+ assert_int_equal(0, programmer_init(prog, param_dup));
printf("... programmer_init for programmer=%s successful\n", prog->name);

printf("Testing programmer_shutdown for programmer=%s ...\n", prog->name);
assert_int_equal(0, programmer_shutdown());
printf("... programmer_shutdown for programmer=%s successful\n", prog->name);
+
+ free(param_dup);
}

void dummy_init_and_shutdown_test_success(void **state)
diff --git a/tests/meson.build b/tests/meson.build
index a18bc45..0bc0560 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -22,6 +22,7 @@
]

mocks = [
+ '-Wl,--wrap=strdup',
'-Wl,--wrap=physunmap',
'-Wl,--wrap=physmap',
'-Wl,--wrap=spi_send_command',
diff --git a/tests/tests.c b/tests/tests.c
index cde696f..592d896 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -18,6 +18,7 @@
#include "tests.h"

#include <stdio.h>
+#include <string.h>
#include <stdint.h>

/* redefinitions/wrapping */
@@ -31,6 +32,16 @@
current_io = io;
}

+/* Workaround for https://github.com/clibs/cmocka/issues/17 */
+char *__wrap_strdup(const char *s)
+{
+ size_t len = strlen(s) + 1;
+ void *new = malloc(len);
+ if (new == NULL)
+ return NULL;
+ return (char *)memcpy(new, s, len);
+}
+
void __wrap_physunmap(void *virt_addr, size_t len)
{
LOG_ME;

To view, visit change 56323. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I56aef6b342752d80995c36ab075b12198fc101d9
Gerrit-Change-Number: 56323
Gerrit-PatchSet: 4
Gerrit-Owner: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-Reviewer: Angel Pons <th3fanbus@gmail.com>
Gerrit-Reviewer: Edward O'Callaghan <quasisec@chromium.org>
Gerrit-Reviewer: Nico Huber <nico.h@gmx.de>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-CC: Paul Menzel <paulepanter@mailbox.org>
Gerrit-MessageType: merged