[pypy-commit] pypy shadowstack-again: A variant not using r15 at all, but an extra argument. Awful Hack Hack

arigo noreply at buildbot.pypy.org
Sun May 25 21:05:03 CEST 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: shadowstack-again
Changeset: r71728:1254999ae792
Date: 2014-05-25 21:04 +0200
http://bitbucket.org/pypy/pypy/changeset/1254999ae792/

Log:	A variant not using r15 at all, but an extra argument. Awful Hack
	Hack Hack for now.

diff --git a/rpython/memory/gctransform/shadowstack.py b/rpython/memory/gctransform/shadowstack.py
--- a/rpython/memory/gctransform/shadowstack.py
+++ b/rpython/memory/gctransform/shadowstack.py
@@ -21,6 +21,12 @@
         lltype.Struct('rpy_shadowstack_s',
                       hints={"external": "C", "c_name": "rpy_shadowstack_s"}))
 
+    def gct_gc_stack_bottom(self, hop):
+        self.ensure_ss_graph_marker(0)
+        block = self._transforming_graph.startblock
+        op = SpaceOperation('gc_stack_bottom', [], varoftype(lltype.Void))
+        block.operations.insert(0, op)
+
     def build_root_walker(self):
         return ShadowStackRootWalker(self)
 
diff --git a/rpython/rtyper/lltypesystem/lloperation.py b/rpython/rtyper/lltypesystem/lloperation.py
--- a/rpython/rtyper/lltypesystem/lloperation.py
+++ b/rpython/rtyper/lltypesystem/lloperation.py
@@ -438,9 +438,6 @@
     'gc_gettypeptr_group':  LLOp(canfold=True),
     'get_member_index':     LLOp(canfold=True),
 
-    'getfield_exc_type':    LLOp(sideeffects=False),
-    'setfield_exc_type':    LLOp(),
-
     # __________ used by the JIT ________
 
     'jit_marker':           LLOp(),
diff --git a/rpython/translator/c/database.py b/rpython/translator/c/database.py
--- a/rpython/translator/c/database.py
+++ b/rpython/translator/c/database.py
@@ -118,7 +118,7 @@
             return node.gettype()
         elif isinstance(T, FuncType):
             resulttype = self.gettype(T.RESULT)
-            argtypes = []
+            argtypes = ['struct rpy_shadowstack_s *rpy_shadowstack']
             for i in range(len(T.ARGS)):
                 if T.ARGS[i] is not Void:
                     argtype = self.gettype(T.ARGS[i])
diff --git a/rpython/translator/c/funcgen.py b/rpython/translator/c/funcgen.py
--- a/rpython/translator/c/funcgen.py
+++ b/rpython/translator/c/funcgen.py
@@ -407,7 +407,8 @@
         r = self.expr(op.result)
         return 'OP_CALL_ARGS((%s), %s);' % (', '.join(args), r)
 
-    def generic_call(self, FUNC, fnexpr, args_v, v_result, targets=None):
+    def generic_call(self, FUNC, fnexpr, args_v, v_result, targets=None,
+                     to_internal=True):
         args = []
         assert len(args_v) == len(FUNC.TO.ARGS)
         for v, ARGTYPE in zip(args_v, FUNC.TO.ARGS):
@@ -418,6 +419,8 @@
             # XXX is this still needed now that rctypes is gone
             if isinstance(ARGTYPE, ContainerType):
                 args[-1] = '*%s' % (args[-1],)
+        if to_internal:
+            args.insert(0, 'rpy_shadowstack')
 
         line = '%s(%s);' % (fnexpr, ', '.join(args))
         if self.lltypemap(v_result) is not Void:
@@ -438,7 +441,8 @@
         except AttributeError:
             targets = None
         return self.generic_call(fn.concretetype, self.expr(fn),
-                                 op.args[1:], op.result, targets)
+                                 op.args[1:], op.result, targets,
+                                 to_internal = targets is not None)
 
     def OP_INDIRECT_CALL(self, op):
         fn = op.args[0]
diff --git a/rpython/translator/c/gc.py b/rpython/translator/c/gc.py
--- a/rpython/translator/c/gc.py
+++ b/rpython/translator/c/gc.py
@@ -431,18 +431,22 @@
             raise AssertionError(subopnum)
         return ' '.join(parts)
 
-    def OP_GC_STACK_BOTTOM(self, funcgen, op):
-        return 'pypy_asm_stack_bottom();'
-
-    def OP_GC_STACK_TOP(self, funcgen, op):
-        return 'pypy_asm_stack_top();'
-
 class ShadowStackFrameworkGcPolicy(BasicFrameworkGcPolicy):
 
     def gettransformer(self):
         from rpython.memory.gctransform import shadowstack
         return shadowstack.ShadowStackFrameworkGCTransformer(self.db.translator)
 
+    def gc_startup_code(self):
+        fnptr = self.db.gctransformer.frameworkgc_setup_ptr.value
+        yield '%s(NULL);' % (self.db.get(fnptr),)
+
+    def OP_GC_STACK_BOTTOM(self, funcgen, op):
+        return 'RPY_SS_STACK_BOTTOM();'
+
+    def OP_GC_STACK_TOP(self, funcgen, op):
+        return 'RPY_SS_STACK_TOP();'
+
     def OP_GC_SS_GRAPH_MARKER(self, funcgen, op):
         marker = funcgen.expr(op.result)
         count = op.args[0].value
diff --git a/rpython/translator/c/genc.py b/rpython/translator/c/genc.py
--- a/rpython/translator/c/genc.py
+++ b/rpython/translator/c/genc.py
@@ -782,6 +782,7 @@
     incfilename = targetdir.join('common_header.h')
     fi = incfilename.open('w')
     fi.write('#ifndef _PY_COMMON_HEADER_H\n#define _PY_COMMON_HEADER_H\n')
+    fi.write('struct rpy_shadowstack_s;\n')
 
     #
     # Header
diff --git a/rpython/translator/c/src/debug_traceback.c b/rpython/translator/c/src/debug_traceback.c
--- a/rpython/translator/c/src/debug_traceback.c
+++ b/rpython/translator/c/src/debug_traceback.c
@@ -13,7 +13,7 @@
 {
   int i;
   int skipping;
-  void *my_etype = RPyFetchExceptionType();
+  void *my_etype = RPyFetchExceptionType(NULL);
   struct pypydtpos_s *location;
   void *etype;
   int has_loc;
@@ -67,6 +67,6 @@
 {
   pypy_debug_traceback_print();
   fprintf(stderr, "Fatal RPython error: %s\n",
-          RPyFetchExceptionType()->ov_name->items);
+          RPyFetchExceptionType(NULL)->ov_name->items);
   abort();
 }
diff --git a/rpython/translator/c/src/entrypoint.c b/rpython/translator/c/src/entrypoint.c
--- a/rpython/translator/c/src/entrypoint.c
+++ b/rpython/translator/c/src/entrypoint.c
@@ -31,6 +31,7 @@
     char *errmsg;
     int i, exitcode;
     RPyListOfString *list;
+    struct rpy_shadowstack_s *rpy_shadowstack;
 
 #ifdef PYPY_USE_ASMGCC
     pypy_g_rpython_rtyper_lltypesystem_rffi_StackCounter.sc_inst_stacks_counter++;
@@ -49,23 +50,20 @@
     errmsg = RPython_StartupCode();
     if (errmsg) goto error;
 
-    pypy_asm_stack_bottom();
-    list = _RPyListOfString_New(argc);
-    if (RPyExceptionOccurred()) goto memory_out;
+    RPY_SS_STACK_BOTTOM();
+    list = _RPyListOfString_New(rpy_shadowstack, argc);
+    if (RPyExceptionOccurred(rpy_shadowstack)) goto memory_out;
     for (i=0; i<argc; i++) {
-        pypy_asm_stack_bottom();
-        RPyString *s = RPyString_FromString(argv[i]);
-        if (RPyExceptionOccurred()) goto memory_out;
-        pypy_asm_stack_bottom();
-        _RPyListOfString_SetItem(list, i, s);
+        RPyString *s = RPyString_FromString(rpy_shadowstack, argv[i]);
+        if (RPyExceptionOccurred(rpy_shadowstack)) goto memory_out;
+        _RPyListOfString_SetItem(rpy_shadowstack, list, i, s);
     }
 
-    pypy_asm_stack_bottom();
-    exitcode = STANDALONE_ENTRY_POINT(list);
+    exitcode = STANDALONE_ENTRY_POINT(rpy_shadowstack, list);
 
     pypy_debug_alloc_results();
 
-    if (RPyExceptionOccurred()) {
+    if (RPyExceptionOccurred(rpy_shadowstack)) {
         /* print the RPython traceback */
         pypy_debug_catch_fatal_exception();
     }
diff --git a/rpython/translator/c/src/exception.c b/rpython/translator/c/src/exception.c
--- a/rpython/translator/c/src/exception.c
+++ b/rpython/translator/c/src/exception.c
@@ -36,7 +36,7 @@
 void _RPyRaiseSimpleException(RPYTHON_EXCEPTION rexc)
 {
 	/* XXX msg is ignored */
-	RPyRaiseException(RPYTHON_TYPE_OF_EXC_INST(rexc), rexc);
+        RPyRaiseException(NULL, RPYTHON_TYPE_OF_EXC_INST(NULL, rexc), rexc);
 }
 
 
diff --git a/rpython/translator/c/src/mem.h b/rpython/translator/c/src/mem.h
--- a/rpython/translator/c/src/mem.h
+++ b/rpython/translator/c/src/mem.h
@@ -201,16 +201,7 @@
 /* The "shadowstack" root finder */
 /*********************************/
 
-#if defined(__GNUC__) && defined(__amd64__)
-#  define RPY_SHADOWSTACK_REG  "r15"
-#endif
-
 struct rpy_shadowstack_s { void *s; };
-#ifdef RPY_SHADOWSTACK_REG
-register void *rpy_shadowstack asm(RPY_SHADOWSTACK_REG);
-#else
-extern void *rpy_shadowstack;
-#endif
 
 #define RPY_SS_GRAPH_MARKER(marker, count)              \
     ;                                                   \
@@ -222,28 +213,13 @@
     rpy_shadowstack = count > 0 ? (char *)(marker + count) - 2  \
                                 : marker[0].s
 
-static inline void pypy_asm_stack_bottom(void)
-{
-    void *s = pypy_g_rpython_memory_gctypelayout_GCData.gcd_inst_root_stack_top;
-    rpy_shadowstack = s;
-}
+#define RPY_SS_STACK_BOTTOM()                                           \
+    rpy_shadowstack =                                                   \
+        pypy_g_rpython_memory_gctypelayout_GCData.gcd_inst_root_stack_top
 
-static inline void pypy_asm_stack_top(void)
-{
-    void *s = rpy_shadowstack;
-    pypy_g_rpython_memory_gctypelayout_GCData.gcd_inst_root_stack_top = s;
-}
-
-#define OP_GETFIELD_EXC_TYPE(r)                                            \
-    if (__builtin_expect(((Signed)rpy_shadowstack) & 1, 0)) {              \
-        r = (struct pypy_object_vtable0 *)(((char *)rpy_shadowstack) - 1); \
-        if (!r) __builtin_unreachable();                                   \
-    }                                                                      \
-    else {                                                                 \
-        r = NULL;                                                          \
-    }
-#define OP_SETFIELD_EXC_TYPE(x, r)                      \
-    rpy_shadowstack = (x) ? ((char *)(x)) + 1 : NULL
+#define RPY_SS_STACK_TOP()                                              \
+    pypy_g_rpython_memory_gctypelayout_GCData.gcd_inst_root_stack_top = \
+        rpy_shadowstack
 
 
 #endif
diff --git a/rpython/translator/c/src/rtyper.c b/rpython/translator/c/src/rtyper.c
--- a/rpython/translator/c/src/rtyper.c
+++ b/rpython/translator/c/src/rtyper.c
@@ -37,10 +37,10 @@
 	}
 }
 
-RPyString *RPyString_FromString(char *buf)
+RPyString *RPyString_FromString(struct rpy_shadowstack_s *rpy_shadowstack, char *buf)
 {
 	int length = strlen(buf);
-	RPyString *rps = RPyString_New(length);
+	RPyString *rps = RPyString_New(rpy_shadowstack, length);
 	memcpy(rps->rs_chars.items, buf, length);
 	return rps;
 }
diff --git a/rpython/translator/c/src/rtyper.h b/rpython/translator/c/src/rtyper.h
--- a/rpython/translator/c/src/rtyper.h
+++ b/rpython/translator/c/src/rtyper.h
@@ -11,4 +11,4 @@
 
 char *RPyString_AsCharP(RPyString *rps);
 void RPyString_FreeCache(void);
-RPyString *RPyString_FromString(char *buf);
+RPyString *RPyString_FromString(struct rpy_shadowstack_s *rpy_shadowstack, char *buf);
diff --git a/rpython/translator/exceptiontransform.py b/rpython/translator/exceptiontransform.py
--- a/rpython/translator/exceptiontransform.py
+++ b/rpython/translator/exceptiontransform.py
@@ -67,19 +67,17 @@
         self.c_n_i_error_ll_exc_type = constant_value(n_i_error_ll_exc_type)
 
         def rpyexc_occured():
-            exc_type = lloperation.llop.getfield_exc_type(
-                self.lltype_of_exception_type)
+            exc_type = exc_data.exc_type
             return bool(exc_type)
 
         def rpyexc_fetch_type():
-            return lloperation.llop.getfield_exc_type(
-                self.lltype_of_exception_type)
+            return exc_data.exc_type
 
         def rpyexc_fetch_value():
             return exc_data.exc_value
 
         def rpyexc_clear():
-            lloperation.llop.setfield_exc_type(lltype.Void, null_type)
+            exc_data.exc_type = null_type
             exc_data.exc_value = null_value
 
         def rpyexc_raise(etype, evalue):
@@ -92,12 +90,12 @@
             # us to see at least part of the traceback for them.
             ll_assert(etype != assertion_error_ll_exc_type, "AssertionError")
             ll_assert(etype != n_i_error_ll_exc_type, "NotImplementedError")
-            lloperation.llop.setfield_exc_type(lltype.Void, etype)
+            exc_data.exc_type = etype
             exc_data.exc_value = evalue
             lloperation.llop.debug_start_traceback(lltype.Void, etype)
 
         def rpyexc_reraise(etype, evalue):
-            lloperation.llop.setfield_exc_type(lltype.Void, etype)
+            exc_data.exc_type = etype
             exc_data.exc_value = evalue
             lloperation.llop.debug_reraise_traceback(lltype.Void, etype)
 
@@ -108,8 +106,7 @@
 
         def rpyexc_restore_exception(evalue):
             if evalue:
-                lloperation.llop.setfield_exc_type(lltype.Void,
-                                                   ll_inst_type(evalue))
+                exc_data.exc_type = ll_inst_type(evalue)
                 exc_data.exc_value = evalue
 
         self.rpyexc_occured_ptr = self.build_func(
@@ -146,15 +143,15 @@
             lltype.Void,
             jitcallkind='rpyexc_raise') # for the JIT
 
-        #self.rpyexc_fetch_exception_ptr = self.build_func(
-        #    "RPyFetchException",
-        #    rpyexc_fetch_exception,
-        #    [], self.lltype_of_exception_value)
+        self.rpyexc_fetch_exception_ptr = self.build_func(
+            "RPyFetchException",
+            rpyexc_fetch_exception,
+            [], self.lltype_of_exception_value)
 
-        #self.rpyexc_restore_exception_ptr = self.build_func(
-        #    "RPyRestoreException",
-        #    self.noinline(rpyexc_restore_exception),
-        #    [self.lltype_of_exception_value], lltype.Void)
+        self.rpyexc_restore_exception_ptr = self.build_func(
+            "RPyRestoreException",
+            self.noinline(rpyexc_restore_exception),
+            [self.lltype_of_exception_value], lltype.Void)
 
         self.build_extra_funcs()
 
@@ -464,6 +461,7 @@
 
     def setup_excdata(self):
         EXCDATA = lltype.Struct('ExcData',
+            ('exc_type',  self.lltype_of_exception_type),
             ('exc_value', self.lltype_of_exception_value))
         self.EXCDATA = EXCDATA
 
@@ -484,17 +482,11 @@
         return Constant(fn_ptr, lltype.Ptr(FUNC_TYPE))
 
     def gen_getfield(self, name, llops):
-        if name == 'exc_type':
-            return llops.genop('getfield_exc_type', [],
-                               resulttype = self.lltype_of_exception_type)
         c_name = inputconst(lltype.Void, name)
         return llops.genop('getfield', [self.cexcdata, c_name],
                            resulttype = getattr(self.EXCDATA, name))
 
     def gen_setfield(self, name, v_value, llops):
-        if name == 'exc_type':
-            llops.genop('setfield_exc_type', [v_value])
-            return
         c_name = inputconst(lltype.Void, name)
         llops.genop('setfield', [self.cexcdata, c_name, v_value])
 
@@ -523,7 +515,6 @@
         exc_data = self.exc_data_ptr
 
         def rpyexc_get_exception_addr():
-            raise NotImplementedError
             return (llmemory.cast_ptr_to_adr(exc_data) +
                     llmemory.offsetof(EXCDATA, 'exc_type'))
 


More information about the pypy-commit mailing list