From c86077010a8f6094fdedca082a524eec656589b1 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Wed, 26 Jun 2019 14:31:10 +0200 Subject: [PATCH] Try to align the native code to the next 8 byte boundary --- src/runtime/typemanager.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/runtime/typemanager.cs b/src/runtime/typemanager.cs index a260e8dfa..0326a3b9b 100644 --- a/src/runtime/typemanager.cs +++ b/src/runtime/typemanager.cs @@ -504,7 +504,7 @@ public static NativeCode Active { get { - switch(Runtime.Machine) + switch (Runtime.Machine) { case Runtime.MachineType.i386: return I386; @@ -696,8 +696,21 @@ internal static void InitializeNativeCodePage() // to be executable. IMemoryMapper mapper = CreateMemoryMapper(); int codeLength = NativeCode.Active.Code.Length; - NativeCodePage = mapper.MapWriteable(codeLength); - Marshal.Copy(NativeCode.Active.Code, 0, NativeCodePage, codeLength); + + // Request 7 bytes more than required such that we can align to an 8 byte boundary on ARM + NativeCodePage = mapper.MapWriteable(codeLength + 7); + + // Round to 8 byte boundary + var start = new IntPtr(((NativeCodePage.ToInt64() + 7) >> 3) << 3); + var diff = start.ToInt64()- NativeCodePage.ToInt64(); + + Marshal.Copy(NativeCode.Active.Code, 0, start, codeLength); + // Fill the remainder up with 0 + for (int i = 0; i < diff; ++i) + Marshal.WriteByte(NativeCodePage, i, 0); + for (int i = 0; i < 7 - diff; ++i) + Marshal.WriteByte(NativeCodePage + codeLength, i, 0); + mapper.SetReadExec(NativeCodePage, codeLength); } #endregion