sandbox: Unprotect DATA regions in bus tests
authorSimon Glass <sjg@chromium.org>
Tue, 2 Oct 2018 03:12:32 +0000 (21:12 -0600)
committerSimon Glass <sjg@chromium.org>
Mon, 8 Oct 2018 13:34:34 +0000 (07:34 -0600)
On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>
arch/sandbox/cpu/os.c
include/os.h
test/dm/bus.c

index 9fbcb9ef92f77dce62ded400b549d79d9145d491..d4d6d78dc746f721db177fc323e98b31655cbbdc 100644 (file)
@@ -636,3 +636,14 @@ void os_abort(void)
 {
        abort();
 }
+
+int os_mprotect_allow(void *start, size_t len)
+{
+       int page_size = getpagesize();
+
+       /* Move start to the start of a page, len to the end */
+       start = (void *)(((ulong)start) & ~(page_size - 1));
+       len = (len + page_size * 2) & ~(page_size - 1);
+
+       return mprotect(start, len, PROT_READ | PROT_WRITE);
+}
index 5c797212c25de1bb7450faaf7f8e217d932bf2a9..7116f8757802523e6fa80b73bc514e088299bf9b 100644 (file)
@@ -334,4 +334,16 @@ void os_localtime(struct rtc_time *rt);
  * os_abort() - Raise SIGABRT to exit sandbox (e.g. to debugger)
  */
 void os_abort(void);
+
+/**
+ * os_mprotect_allow() - Remove write-protection on a region of memory
+ *
+ * The start and length will be page-aligned before use.
+ *
+ * @start:     Region start
+ * @len:       Region length in bytes
+ * @return 0 if OK, -1 on error from mprotect()
+ */
+int os_mprotect_allow(void *start, size_t len);
+
 #endif
index e9a4028f04746e955d6d98b229ba58224f8b614b..08137a2216a29babddd709a49b613d2e57df50c7 100644 (file)
@@ -4,6 +4,9 @@
  */
 
 #include <common.h>
+#ifdef CONFIG_SANDBOX
+#include <os.h>
+#endif
 #include <dm.h>
 #include <dm/device-internal.h>
 #include <dm/test.h>
@@ -297,6 +300,11 @@ static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
        ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
        drv = (struct driver *)bus->driver;
        size = drv->per_child_auto_alloc_size;
+
+#ifdef CONFIG_SANDBOX
+       os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
+       os_mprotect_allow(drv, sizeof(*drv));
+#endif
        bus->uclass->uc_drv->per_child_auto_alloc_size = size;
        drv->per_child_auto_alloc_size = 0;
        ret = test_bus_parent_data(uts);
@@ -440,6 +448,10 @@ static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
        ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
        drv = (struct driver *)bus->driver;
        size = drv->per_child_platdata_auto_alloc_size;
+#ifdef CONFIG_SANDBOX
+       os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
+       os_mprotect_allow(drv, sizeof(*drv));
+#endif
        bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
        drv->per_child_platdata_auto_alloc_size = 0;
        ret = test_bus_parent_platdata(uts);