Skip to content

Commit 0cba00f

Browse files
authored
Fix Windows context to Unix context translation on AMD64 (dotnet/coreclr#6027)
There was a bug in the context translation between the Windows context and Unix context on AMD64 caused by the fact that the Unix context gregs array contains CS, GS and FS in a single field (REG_CSGSFS) and the MCREG_SegCs accessor macro was incorrectly written to use the whole field as CS. So writing the CS into the Unix context also cleared the GS, FS and the topmost 16 bits described as padding. This issue was exposed on the Linux kernel >= 4.6.0 where the padding for some reason was not zero, probably used by the kernel for some internal purposes. I have fixed it by changing the accessor to modify only the 16 bits corresponding to the CS. I have also changed the code in the inject_activation_handler to save cycles and not to copy the Windows context back to the Unix one in case the activation function was not called and so the context was not possibly changed. Commit migrated from dotnet/coreclr@56ab756
1 parent 7bb3708 commit 0cba00f

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

src/coreclr/src/pal/src/exception/signal.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,9 @@ static void inject_activation_handler(int code, siginfo_t *siginfo, void *contex
464464
if (g_safeActivationCheckFunction(CONTEXTGetPC(&winContext), /* checkingCurrentThread */ TRUE))
465465
{
466466
g_activationFunction(&winContext);
467+
// Activation function may have modified the context, so update it.
468+
CONTEXTToNativeContext(&winContext, ucontext);
467469
}
468-
469-
// Activation function may have modified the context, so update it.
470-
CONTEXTToNativeContext(&winContext, ucontext);
471470
}
472471
else if (g_previous_activation.sa_sigaction != NULL)
473472
{

src/coreclr/src/pal/src/include/pal/context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ typedef ucontext_t native_context_t;
111111
#define MCREG_Rax(mc) ((mc).gregs[REG_RAX])
112112
#define MCREG_Rip(mc) ((mc).gregs[REG_RIP])
113113
#define MCREG_Rsp(mc) ((mc).gregs[REG_RSP])
114-
#define MCREG_SegCs(mc) ((mc).gregs[REG_CSGSFS])
114+
#define MCREG_SegCs(mc) (*(WORD*)&((mc).gregs[REG_CSGSFS]))
115115
#define MCREG_R8(mc) ((mc).gregs[REG_R8])
116116
#define MCREG_R9(mc) ((mc).gregs[REG_R9])
117117
#define MCREG_R10(mc) ((mc).gregs[REG_R10])

0 commit comments

Comments
 (0)