|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | + |
| 8 | +public unsafe class Runtime_65937 |
| 9 | +{ |
| 10 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 11 | + public static int Main() |
| 12 | + { |
| 13 | + if (!OperatingSystem.IsLinux()) |
| 14 | + { |
| 15 | + return 100; |
| 16 | + } |
| 17 | + |
| 18 | + const int PROT_NONE = 0x0; |
| 19 | + const int PROT_READ = 0x1; |
| 20 | + const int PROT_WRITE = 0x2; |
| 21 | + const int MAP_PRIVATE = 0x02; |
| 22 | + const int MAP_ANONYMOUS = 0x20; |
| 23 | + const int PAGE_SIZE = 0x1000; |
| 24 | + |
| 25 | + byte* pages = (byte*)mmap(null, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 26 | + |
| 27 | + if (pages == (byte*)-1) |
| 28 | + { |
| 29 | + Console.WriteLine("Failed to allocate two pages, errno is {0}, giving up on the test", Marshal.GetLastSystemError()); |
| 30 | + return 100; |
| 31 | + } |
| 32 | + |
| 33 | + if (mprotect(pages + PAGE_SIZE, PAGE_SIZE, PROT_NONE) != 0) |
| 34 | + { |
| 35 | + Console.WriteLine("Failed to protect the second page, errno is {0}, giving up on the test", Marshal.GetLastSystemError()); |
| 36 | + munmap(pages, 2 * PAGE_SIZE); |
| 37 | + return 100; |
| 38 | + } |
| 39 | + |
| 40 | + CallWithStkArg(0, 0, 0, 0, 0, 0, *(StructWithNineBytes*)(pages + PAGE_SIZE - sizeof(StructWithNineBytes))); |
| 41 | + |
| 42 | + munmap(pages, 2 * PAGE_SIZE); |
| 43 | + |
| 44 | + return 100; |
| 45 | + } |
| 46 | + |
| 47 | + struct StructWithNineBytes |
| 48 | + { |
| 49 | + byte ByteOne; |
| 50 | + byte ByteTwo; |
| 51 | + byte ByteThree; |
| 52 | + byte ByteFour; |
| 53 | + byte ByteFive; |
| 54 | + byte ByteSix; |
| 55 | + byte ByteSeven; |
| 56 | + byte ByteEight; |
| 57 | + byte ByteNine; |
| 58 | + } |
| 59 | + |
| 60 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 61 | + private static void CallWithStkArg(int a, int b, int c, int d, int e, int f, StructWithNineBytes stkArg) { } |
| 62 | + |
| 63 | + [DllImport("libc")] |
| 64 | + private static extern void* mmap(void* addr, nuint length, int prot, int flags, int fd, nuint offset); |
| 65 | + |
| 66 | + [DllImport("libc")] |
| 67 | + private static extern int mprotect(void* addr, nuint len, int prot); |
| 68 | + |
| 69 | + [DllImport("libc")] |
| 70 | + private static extern int munmap(void* addr, nuint length); |
| 71 | +} |
0 commit comments