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);
        }
Esempio n. 3
0
            static LibSystem()
            {
                typeof(LibSystem).ResolveDynDllImports(new Dictionary <string, List <DynDllMapping> >
                {
                    ["libSystem"] = new List <DynDllMapping>
                    {
                        "/usr/lib/libSystem.dylib"                         // OSX POSIX
                    }
                });

                var libsystem = DynDll.OpenLibrary("/usr/lib/libSystem.dylib");

                TaskSelf = libsystem.GetFunction("mach_task_self_");                 // This isn't a function but rather an exported symbol
            }
        public DetourNativeMonoPlatform(IDetourNativePlatform inner, string libmono)
        {
            Inner = inner;

            Dictionary <string, List <DynDllMapping> > mappings = new Dictionary <string, List <DynDllMapping> >();

            if (!string.IsNullOrEmpty(libmono))
            {
                mappings.Add("mono", new List <DynDllMapping>()
                {
                    libmono
                });
            }
            DynDll.ResolveDynDllImports(this, mappings);

            _Pagesize = mono_pagesize();
        }
        public DetourNativeMonoPlatform(IDetourNativePlatform inner, string libmono)
        {
            Inner = inner;

#if MONOMOD_RUNTIMEDETOUR
            Dictionary <string, DynDllMapping> mappings = new Dictionary <string, DynDllMapping>();
            if (!string.IsNullOrEmpty(libmono))
            {
                mappings.Add("mono", new DynDllMapping {
                    ResolveAs = libmono
                });
            }
            DynDll.ResolveDynDllImports(this, mappings);
#endif

            _Pagesize = mono_pagesize();
        }
        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. 7
0
        private void TestNativeDetoursWindows(dt_rand d_not_rand, IntPtr ptr_not_rand)
        {
            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(
                DynDll.OpenLibrary($"msvcrt.{PlatformHelper.LibrarySuffix}").GetFunction("rand"),
                ptr_not_rand
                );

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

            d.Dispose();

            DidNothing = true;
            msvcrt_rand();
            Assert.True(DidNothing);
        }
Esempio n. 8
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>();
                }
            }
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            CultureInfo.DefaultThreadCurrentCulture   = CultureInfo.InvariantCulture;
            CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;

            Args = args;

            if (!Directory.Exists(InstallDir))
            {
                Directory.CreateDirectory(InstallDir);
            }
            if (!Directory.Exists(ConfigDir))
            {
                Directory.CreateDirectory(ConfigDir);
            }

            Environment.SetEnvironmentVariable("LOCAL_LUA_DEBUGGER_VSCODE", "0");
            Environment.CurrentDirectory = InstallDir;

            if (File.Exists(LogPath))
            {
                File.Delete(LogPath);
            }

            using (Stream fileStream = new FileStream(LogPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete))
                using (StreamWriter fileWriter = new StreamWriter(fileStream, Console.OutputEncoding))
                    using (LogWriter logWriter = new LogWriter {
                        STDOUT = Console.Out,
                        File = fileWriter
                    }) {
                        try {
                            Console.SetOut(logWriter);

                            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;

                            Console.WriteLine($"Olympus Launcher {typeof(Program).Assembly.GetName().Version}");

                            if (StartMain())
                            {
                                Console.WriteLine("Quitting early");
                                return;
                            }

                            Console.WriteLine("Starting up form");

                            // Application.SetHighDpiMode(HighDpiMode.SystemAware);
                            DynDll.ResolveDynDllImports(typeof(DpiUtils));
                            if (DpiUtils.SetProcessDpiAwarenessContext != null)
                            {
                                DpiUtils.SetProcessDpiAwarenessContext(DpiUtils.DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
                            }
                            else if (DpiUtils.SetProcessDpiAwareness != null)
                            {
                                DpiUtils.SetProcessDpiAwareness(DpiUtils.PROCESS_DPI_AWARENESS.Process_Per_Monitor_DPI_Aware);
                            }
                            else if (DpiUtils.SetProcessDPIAware != null)
                            {
                                DpiUtils.SetProcessDPIAware();
                            }

                            Application.EnableVisualStyles();
                            Application.SetCompatibleTextRenderingDefault(false);
                            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                            Application.Run(new OlympusForm());
                        } finally {
                            if (logWriter.STDOUT != null)
                            {
                                Console.SetOut(logWriter.STDOUT);
                                logWriter.STDOUT = null;
                            }
                        }
                    }
        }