Android: workaround absence of getcontext/setcontext (dotnet/coreclr#9725)
authorFrederik Carlier <frederik.carlier@quamotion.mobi>
Thu, 23 Feb 2017 10:09:24 +0000 (02:09 -0800)
committerJan Vorlicek <janvorli@microsoft.com>
Thu, 23 Feb 2017 10:09:24 +0000 (11:09 +0100)
* Android: workaround absence of getcontext/setcontext
use RtlCaptureContext and RtlRestoreContext

Commit migrated from https://github.com/dotnet/coreclr/commit/08581d8e9515ef1a348e7509785ea29195a9de37

src/coreclr/src/pal/src/exception/signal.cpp
src/coreclr/src/pal/src/include/pal/context.h

index d442afa..ee7f7ce 100644 (file)
@@ -47,6 +47,33 @@ SET_DEFAULT_DEBUG_CHANNEL(EXCEPT); // some headers have code with asserts, so do
 
 #include "pal/context.h"
 
+#ifdef __ANDROID__
+// getcontext and setcontext are not available natively on Android
+int getcontext(ucontext_t *ucp)
+{
+    CONTEXT context;
+    RtlCaptureContext(&context);
+    CONTEXTToNativeContext(&context, ucp);
+
+    return 0;
+}
+
+int setcontext(const ucontext_t *ucp)
+{
+    CONTEXT context;
+    ULONG contextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT;
+
+#if defined(_AMD64_)
+    contextFlags |= CONTEXT_XSTATE;
+#endif
+
+    CONTEXTFromNativeContext(ucp, &context, contextFlags);
+    RtlRestoreContext(&context, NULL);
+
+    return 0;
+}
+#endif
+
 using namespace CorUnix;
 
 #ifdef SIGRTMIN
index db6d695..782a51b 100644 (file)
@@ -29,6 +29,12 @@ extern "C"
 #include <signal.h>
 #include <pthread.h>
 
+#ifdef __ANDROID__
+// getcontext and setcontext are not available natively on Android
+int setcontext(const ucontext_t *ucp);
+int getcontext(ucontext_t* ucp);
+#endif
+
 #if !HAVE_MACH_EXCEPTIONS
 /* A type to wrap the native context type, which is ucontext_t on some
  * platforms and another type elsewhere. */