Esempio n. 1
0
        private void TestNativeDetoursUnix(dt_rand d_not_rand, IntPtr ptr_not_rand)
        {
            DidNothing = true;
            libc_rand();
            Assert.True(DidNothing);

            DidNothing = true;
            Assert.Equal(-1, d_not_rand());
            Assert.False(DidNothing);

            using (new NativeDetour(
                       typeof(NativeDetourTest).GetMethod("libc_rand"),
                       typeof(NativeDetourTest).GetMethod("not_rand")
                       )) {
                DidNothing = true;
                Assert.Equal(-1, libc_rand());
                Assert.False(DidNothing);
            }

            DidNothing = true;
            libc_rand();
            Assert.True(DidNothing);

            /* dl's dlopen doesn't follow ld, meaning that we need to...
             * - use libc.so.6 on Linux and hope that everyone is using glibc.
             * - use /usr/lib/libc.dylib on macOS because macOS is macOS.
             * If libc cannot be dlopened, skip the native -> managed detour test.
             * - ade
             */
            if (!(PlatformHelper.Is(Platform.Linux) && DynDll.TryOpenLibrary("libc.so.6", out IntPtr libc)) &&
                !(PlatformHelper.Is(Platform.MacOS) && DynDll.TryOpenLibrary("/usr/lib/libc.dylib", out libc)) &&
                !DynDll.TryOpenLibrary($"libc.{PlatformHelper.LibrarySuffix}", out libc))
            {
                return;
            }

            NativeDetour d = new NativeDetour(
                libc.GetFunction("rand"),
                ptr_not_rand
                );

            DidNothing = true;
            Assert.Equal(-1, libc_rand());
            Assert.False(DidNothing);

            d.Dispose();

            DidNothing = true;
            libc_rand();
            Assert.True(DidNothing);
        }
Esempio n. 2
0
        private void TestNativeDetoursWindows(dt_rand d_not_rand, IntPtr ptr_not_rand)
        {
            if (!DynDll.TryOpenLibrary($"msvcrt.{PlatformHelper.LibrarySuffix}", out IntPtr msvcrt))
            {
                Console.WriteLine("NativeDetourTest skipped - msvcrt not found!");
                return;
            }

            if (!msvcrt.TryGetFunction("rand", out IntPtr ptr_msvcrt_rand))
            {
                Console.WriteLine("NativeDetourTest skipped - rand not found in msvcrt!");
                return;
            }

            DidNothing = true;
            msvcrt_rand();
            Assert.True(DidNothing);

            DidNothing = true;
            Assert.Equal(-1, d_not_rand());
            Assert.False(DidNothing);

            using (new NativeDetour(
                       typeof(NativeDetourTest).GetMethod("msvcrt_rand"),
                       typeof(NativeDetourTest).GetMethod("not_rand")
                       )) {
                DidNothing = true;
                Assert.Equal(-1, msvcrt_rand());
                Assert.False(DidNothing);
            }

            DidNothing = true;
            msvcrt_rand();
            Assert.True(DidNothing);

            NativeDetour d = new NativeDetour(
                ptr_msvcrt_rand,
                ptr_not_rand
                );

            DidNothing = true;
            Assert.Equal(-1, msvcrt_rand());
            Assert.False(DidNothing);

            d.Dispose();

            DidNothing = true;
            msvcrt_rand();
            Assert.True(DidNothing);
        }
        protected static IntPtr GetJitObject()
        {
            if (getJit == null)
            {
                // To make sure we get the right clrjit, we enumerate the process's modules and find the one
                //   with the name we care about, then use its full path to gat a handle and load symbols.
                Process       currentProc  = Process.GetCurrentProcess();
                ProcessModule clrjitModule = currentProc.Modules.Cast <ProcessModule>()
                                             .FirstOrDefault(m => Path.GetFileNameWithoutExtension(m.FileName).EndsWith("clrjit"));
                if (clrjitModule == null)
                {
                    throw new PlatformNotSupportedException();
                }

                if (!DynDll.TryOpenLibrary(clrjitModule.FileName, out IntPtr clrjitPtr))
                {
                    throw new PlatformNotSupportedException();
                }

                if (PlatformHelper.Is(Platform.Windows))
                {
                    // we can use this check only on Windows, because only Windows actually has FilveVersionInfo
                    // this is preferred because it checks the version of the JIT, rather than the runtime library
                    //   which is what actually determines the layout.
                    isNet5Jit = clrjitModule.FileVersionInfo.ProductMajorPart >= 5;
                }
                else
                {
                    // this gets System.Private.CorLib's major version, which *should* match the runtime version
                    // it is the only method we have at the moment to detect it on non-Windows platforms
                    isNet5Jit = typeof(object).Assembly.GetName().Version.Major >= 5;
                }

                try {
                    getJit = clrjitPtr.GetFunction(nameof(getJit)).AsDelegate <d_getJit>();
                } catch {
                    DynDll.CloseLibrary(clrjitPtr);
                    throw;
                }
            }

            return(getJit());
        }
Esempio n. 4
0
        static DpiUtils()
        {
            if (DynDll.TryOpenLibrary("user32.dll", out IntPtr p_user32))
            {
                if (p_user32.TryGetFunction("SetProcessDpiAwarenessContext", out IntPtr p_SetProcessDpiAwarenessContext))
                {
                    SetProcessDpiAwarenessContext = p_SetProcessDpiAwarenessContext.AsDelegate <d_SetProcessDpiAwarenessContext>();
                }

                if (p_user32.TryGetFunction("SetProcessDPIAware", out IntPtr p_SetProcessDPIAware))
                {
                    SetProcessDPIAware = p_SetProcessDPIAware.AsDelegate <d_SetProcessDPIAware>();
                }
            }

            if (DynDll.TryOpenLibrary("SHCore.dll", out IntPtr p_SHCore))
            {
                if (p_SHCore.TryGetFunction("SetProcessDpiAwareness", out IntPtr p_SetProcessDpiAwareness))
                {
                    SetProcessDpiAwareness = p_SetProcessDpiAwareness.AsDelegate <d_SetProcessDpiAwareness>();
                }
            }
        }