Browse Source

[LibOS/regression] Add spinlock tests

borysp 5 years ago
parent
commit
e064b2bd8c

+ 1 - 0
LibOS/shim/test/regression/Makefile

@@ -23,6 +23,7 @@ CFLAGS-multi_pthread = -pthread
 CFLAGS-exit_group = -pthread
 CFLAGS-abort_multithread = -pthread
 CFLAGS-eventfd = -pthread
+CFLAGS-spinlock += -I$(PALDIR)/../lib -pthread
 
 %: %.c
 	$(call cmd,csingle)

+ 66 - 0
LibOS/shim/test/regression/spinlock.c

@@ -0,0 +1,66 @@
+/* Poor man's spinlock test */
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "spinlock.h"
+
+#define TEST_TIMES 1000
+
+static spinlock_t guard = INIT_SPINLOCK_UNLOCKED;
+static spinlock_t go = INIT_SPINLOCK_UNLOCKED;
+static volatile int x = 0;
+
+static int set_expect(int s, int e) {
+    int ret = 0;
+
+    spinlock_lock(&guard);
+    spinlock_unlock(&go);
+    if (x != e) {
+        ret = 1;
+        goto out;
+    }
+    x = s;
+out:
+    spinlock_unlock(&guard);
+    return ret;
+}
+
+static void* thr(void* unused) {
+    spinlock_lock(&go);
+    if (set_expect(0, -1)) {
+        return (void*)1;
+    }
+    return (void*)0;
+}
+
+static void do_test(void) {
+    pthread_t th;
+    int ret_val = 0;
+
+    spinlock_lock(&go);
+
+    pthread_create(&th, NULL, thr, NULL);
+
+    if (set_expect(-1, 0)) {
+        puts("Test failed!");
+        exit(1);
+    }
+
+    pthread_join(th, (void **)&ret_val);
+    if (ret_val) {
+        puts("Test failed!");
+        exit(1);
+    }
+}
+
+int main(void) {
+    setbuf(stdout, NULL);
+
+    for (int i = 0; i < TEST_TIMES; ++i) {
+        do_test();
+    }
+
+    puts("Test successful!");
+    return 0;
+}

+ 9 - 3
LibOS/shim/test/regression/test_libos.py

@@ -12,7 +12,13 @@ from regression import (
     expectedFailureIf,
 )
 
-class TC_00_Bootstrap(RegressionTestCase):
+class TC_00_Unittests(RegressionTestCase):
+    def test_000_spinlock(self):
+        stdout, stderr = self.run_binary(['spinlock'])
+
+        self.assertIn('Test successful!', stdout)
+
+class TC_01_Bootstrap(RegressionTestCase):
     def test_100_basic_bootstrapping(self):
         stdout, stderr = self.run_binary(['bootstrap'])
 
@@ -151,7 +157,7 @@ class TC_00_Bootstrap(RegressionTestCase):
     'This test is only meaningful on SGX PAL because only SGX catches raw '
     'syscalls and redirects to Graphene\'s LibOS. If we will add seccomp to '
     'Linux PAL, then we should allow this test on Linux PAL as well.')
-class TC_01_OpenMP(RegressionTestCase):
+class TC_02_OpenMP(RegressionTestCase):
     def test_000_simple_for_loop(self):
         stdout, stderr = self.run_binary(['openmp'])
 
@@ -161,7 +167,7 @@ class TC_01_OpenMP(RegressionTestCase):
 @unittest.skipUnless(HAS_SGX,
     'This test is only meaningful on SGX PAL because file-check-policy is '
     'only relevant to SGX.')
-class TC_02_FileCheckPolicy(RegressionTestCase):
+class TC_03_FileCheckPolicy(RegressionTestCase):
     def test_000_strict_success(self):
         manifest = self.get_manifest('file_check_policy_strict')
         stdout, stderr = self.run_binary([manifest, 'trusted_testfile'])