[pypy-commit] stmgc c7-refactor: move shadow stack init/done to setup.c

Remi Meier noreply at buildbot.pypy.org
Wed Feb 26 13:40:44 CET 2014


Author: Remi Meier
Branch: c7-refactor
Changeset: r866:22f8313b374c
Date: 2014-02-26 13:40 +0100
http://bitbucket.org/pypy/stmgc/changeset/22f8313b374c/

Log:	move shadow stack init/done to setup.c

diff --git a/c7/demo/demo2.c b/c7/demo/demo2.c
--- a/c7/demo/demo2.c
+++ b/c7/demo/demo2.c
@@ -21,24 +21,6 @@
 
 __thread stm_thread_local_t stm_thread_local;
 
-#define PUSH_ROOT(p)   (*stm_thread_local.shadowstack++ = (object_t *)(p))
-#define POP_ROOT(p)    ((p) = (typeof(p))*--stm_thread_local.shadowstack)
-
-void init_shadow_stack(void)
-{
-    object_t **s = (object_t **)malloc(1000 * sizeof(object_t *));
-    assert(s);
-    stm_thread_local.shadowstack = s;
-    stm_thread_local.shadowstack_base = s;
-}
-
-void done_shadow_stack(void)
-{
-    free(stm_thread_local.shadowstack_base);
-    stm_thread_local.shadowstack = NULL;
-    stm_thread_local.shadowstack_base = NULL;
-}
-
 
 ssize_t stmcb_size_rounded_up(struct object_s *ob)
 {
@@ -157,14 +139,14 @@
     global_chained_list->value = -1;
     global_chained_list->next = NULL;
 
-    PUSH_ROOT(global_chained_list);
+    STM_PUSH_ROOT(stm_thread_local, global_chained_list);
 
     w_prev = global_chained_list;
     for (i = 0; i < LIST_LENGTH; i++) {
-        PUSH_ROOT(w_prev);
+        STM_PUSH_ROOT(stm_thread_local, w_prev);
         w_newnode = (nodeptr_t)stm_allocate(sizeof(struct node_s));
 
-        POP_ROOT(w_prev);
+        STM_POP_ROOT(stm_thread_local, w_prev);
         w_newnode->value = LIST_LENGTH - i;
         w_newnode->next = NULL;
 
@@ -173,16 +155,16 @@
         w_prev = w_newnode;
     }
 
-    POP_ROOT(global_chained_list);   /* update value */
+    STM_POP_ROOT(stm_thread_local, global_chained_list);   /* update value */
     assert(global_chained_list->value == -1);
-    PUSH_ROOT(global_chained_list);
+    STM_PUSH_ROOT(stm_thread_local, global_chained_list);
 
     stm_commit_transaction();
 
     stm_start_inevitable_transaction(&stm_thread_local);
-    POP_ROOT(global_chained_list);   /* update value */
+    STM_POP_ROOT(stm_thread_local, global_chained_list);   /* update value */
     assert(global_chained_list->value == -1);
-    PUSH_ROOT(global_chained_list);  /* remains forever in the shadow stack */
+    STM_PUSH_ROOT(stm_thread_local, global_chained_list);  /* remains forever in the shadow stack */
     stm_commit_transaction();
 
     printf("setup ok\n");
@@ -196,16 +178,16 @@
 {
     int status;
     stm_register_thread_local(&stm_thread_local);
-    init_shadow_stack();
-    PUSH_ROOT(global_chained_list);  /* remains forever in the shadow stack */
+
+    STM_PUSH_ROOT(stm_thread_local, global_chained_list);  /* remains forever in the shadow stack */
 
     while (check_sorted() == -1) {
         bubble_run();
     }
 
-    POP_ROOT(global_chained_list);
+    STM_POP_ROOT(stm_thread_local, global_chained_list);
     assert(stm_thread_local.shadowstack == stm_thread_local.shadowstack_base);
-    done_shadow_stack();
+
     stm_unregister_thread_local(&stm_thread_local);
     status = sem_post(&done); assert(status == 0);
     return NULL;
@@ -216,9 +198,9 @@
     long sum;
 
     printf("final check\n");
-    
+
     sum = check_sorted();
-    
+
     // little Gauss:
     if (sum == (1 + LIST_LENGTH) * (LIST_LENGTH / 2))
         printf("check ok\n");
@@ -247,7 +229,7 @@
 
     stm_setup();
     stm_register_thread_local(&stm_thread_local);
-    init_shadow_stack();
+
 
     setup_list();
 
@@ -259,7 +241,7 @@
 
     final_check();
 
-    done_shadow_stack();
+
     stm_unregister_thread_local(&stm_thread_local);
     stm_teardown();
 
diff --git a/c7/demo/demo_random.c b/c7/demo/demo_random.c
--- a/c7/demo/demo_random.c
+++ b/c7/demo/demo_random.c
@@ -44,25 +44,6 @@
 __thread struct thread_data td;
 
 
-#define PUSH_ROOT(p)   (*(stm_thread_local.shadowstack++) = (object_t *)(p))
-#define POP_ROOT(p)    ((p) = (typeof(p))*(--stm_thread_local.shadowstack))
-
-void init_shadow_stack(void)
-{
-    object_t **s = (object_t **)malloc(1000 * sizeof(object_t *));
-    assert(s);
-    stm_thread_local.shadowstack = s;
-    stm_thread_local.shadowstack_base = s;
-}
-
-void done_shadow_stack(void)
-{
-    free(stm_thread_local.shadowstack_base);
-    stm_thread_local.shadowstack = NULL;
-    stm_thread_local.shadowstack_base = NULL;
-}
-
-
 ssize_t stmcb_size_rounded_up(struct object_s *ob)
 {
     return ((struct node_s*)ob)->my_size;
@@ -89,7 +70,7 @@
 {
     int i;
     for (i = 0; i < SHARED_ROOTS; i++) {
-        PUSH_ROOT(shared_roots[i]);
+        STM_PUSH_ROOT(stm_thread_local, shared_roots[i]);
     }
 }
 
@@ -97,7 +78,7 @@
 {
     int i;
     for (i = 0; i < SHARED_ROOTS; i++) {
-        POP_ROOT(shared_roots[SHARED_ROOTS - i - 1]);
+        STM_POP_ROOT(stm_thread_local, shared_roots[SHARED_ROOTS - i - 1]);
     }
 }
 
@@ -127,12 +108,12 @@
     assert(td.num_roots == td.num_roots_at_transaction_start);
     for (i = td.num_roots_at_transaction_start - 1; i >= 0; i--) {
         if (td.roots[i])
-            POP_ROOT(td.roots[i]);
+            STM_POP_ROOT(stm_thread_local, td.roots[i]);
     }
 
     for (i = 0; i < td.num_roots_at_transaction_start; i++) {
         if (td.roots[i])
-            PUSH_ROOT(td.roots[i]);
+            STM_PUSH_ROOT(stm_thread_local, td.roots[i]);
     }
 }
 
@@ -141,7 +122,7 @@
     int i;
     for (i = td.num_roots_at_transaction_start; i < td.num_roots; i++) {
         if (td.roots[i])
-            PUSH_ROOT(td.roots[i]);
+            STM_PUSH_ROOT(stm_thread_local, td.roots[i]);
     }
 }
 
@@ -150,7 +131,7 @@
     int i;
     for (i = td.num_roots - 1; i >= td.num_roots_at_transaction_start; i--) {
         if (td.roots[i])
-            POP_ROOT(td.roots[i]);
+            STM_POP_ROOT(stm_thread_local, td.roots[i]);
     }
 }
 
@@ -304,7 +285,6 @@
 {
     int status;
     stm_register_thread_local(&stm_thread_local);
-    init_shadow_stack();
 
     /* forever on the shadowstack: */
     _push_shared_roots();
@@ -342,7 +322,6 @@
     }
     stm_commit_transaction();
 
-    done_shadow_stack();
     stm_unregister_thread_local(&stm_thread_local);
 
     status = sem_post(&done); assert(status == 0);
@@ -368,7 +347,7 @@
     for (i = 0; i < SHARED_ROOTS; i++) {
         shared_roots[i] = stm_allocate(sizeof(struct node_s));
         ((nodeptr_t)shared_roots[i])->my_size = sizeof(struct node_s);
-        PUSH_ROOT(shared_roots[i]);
+        STM_PUSH_ROOT(stm_thread_local, shared_roots[i]);
     }
     stm_commit_transaction();
 
@@ -398,7 +377,6 @@
 
     stm_setup();
     stm_register_thread_local(&stm_thread_local);
-    init_shadow_stack();
 
     setup_globals();
 
@@ -421,7 +399,6 @@
     printf("Test OK!\n");
 
     _pop_shared_roots();
-    done_shadow_stack();
     stm_unregister_thread_local(&stm_thread_local);
     stm_teardown();
 
diff --git a/c7/stm/core.h b/c7/stm/core.h
--- a/c7/stm/core.h
+++ b/c7/stm/core.h
@@ -29,6 +29,7 @@
 #define WRITELOCK_START       ((END_NURSERY_PAGE * 4096UL) >> 4)
 #define WRITELOCK_END         READMARKER_END
 
+#define SHADOW_STACK_SIZE     1000
 
 enum /* stm_flags */ {
     /* This flag is set on non-nursery objects.  It forces stm_write()
diff --git a/c7/stm/setup.c b/c7/stm/setup.c
--- a/c7/stm/setup.c
+++ b/c7/stm/setup.c
@@ -103,6 +103,22 @@
     teardown_nursery();
 }
 
+void _init_shadow_stack(stm_thread_local_t *tl)
+{
+    object_t **s = (object_t **)malloc(SHADOW_STACK_SIZE * sizeof(object_t *));
+    assert(s);
+    tl->shadowstack = s;
+    tl->shadowstack_base = s;
+}
+
+void _done_shadow_stack(stm_thread_local_t *tl)
+{
+    free(tl->shadowstack_base);
+    tl->shadowstack = NULL;
+    tl->shadowstack_base = NULL;
+}
+
+
 void stm_register_thread_local(stm_thread_local_t *tl)
 {
     int num;
@@ -123,12 +139,14 @@
        numbers automatically. */
     num = num % NB_SEGMENTS;
     tl->associated_segment_num = num;
+    _init_shadow_stack(tl);
     set_gs_register(get_segment_base(num));
 }
 
 void stm_unregister_thread_local(stm_thread_local_t *tl)
 {
     assert(tl->next != NULL);
+    _done_shadow_stack(tl);
     if (tl == stm_all_thread_locals) {
         stm_all_thread_locals = stm_all_thread_locals->next;
         if (tl == stm_all_thread_locals) {
diff --git a/c7/stmgc.h b/c7/stmgc.h
--- a/c7/stmgc.h
+++ b/c7/stmgc.h
@@ -191,6 +191,12 @@
 void stm_setup(void);
 void stm_teardown(void);
 
+/* Push and pop roots from/to the shadow stack. Only allowed inside
+   transaction. */
+#define STM_PUSH_ROOT(tl, p)   (*((tl).shadowstack++) = (object_t *)(p))
+#define STM_POP_ROOT(tl, p)    ((p) = (typeof(p))*(--(tl).shadowstack))
+
+
 /* Every thread needs to have a corresponding stm_thread_local_t
    structure.  It may be a "__thread" global variable or something else.
    Use the following functions at the start and at the end of a thread.


More information about the pypy-commit mailing list