Example #1
0
            public Delegate(IntPtr instancePtr, OSPlatform osPlatform)
            {
                Ptr    = IntPtr.Zero;
                Invoke = null;

                Symbol = typeof(T).Name;
                const string delegatePrefix = "_delegate";

                if (Symbol.EndsWith(delegatePrefix, StringComparison.OrdinalIgnoreCase))
                {
                    Symbol = Symbol.Substring(0, Symbol.Length - delegatePrefix.Length);
                }

                if (!osPlatform.IsSupportedOsVersion())
                {
                    throw new PlatformNotSupportedException();
                }

                if (osPlatform == OSPlatform.Windows)
                {
                    Ptr = NativeMethodsWindows.dlsym(instancePtr, Symbol);
                }
                else if (osPlatform == OSPlatform.Linux)
                {
                    Ptr = NativeMethodsUnix.dlsym(instancePtr, Symbol);
                }

                if (Ptr == IntPtr.Zero)
                {
                    throw new Exception(
                              $"Failed load function: {Symbol}. Last error: {Marshal.GetLastWin32Error()}.");
                }

                Invoke = Marshal.GetDelegateForFunctionPointer <T>(Ptr);
            }
Example #2
0
        public AssemblyDefinition OptimizeSnapDllForPackageArchive([NotNull] AssemblyDefinition assemblyDefinition, OSPlatform osPlatform)
        {
            if (assemblyDefinition == null)
            {
                throw new ArgumentNullException(nameof(assemblyDefinition));
            }

            if (!osPlatform.IsSupportedOsVersion())
            {
                throw new PlatformNotSupportedException();
            }

            var cecilReflector         = new CecilAssemblyReflector(assemblyDefinition);
            var cecilResourceReflector = cecilReflector.GetResourceReflector();

            cecilResourceReflector.RemoveAllOrThrow(typeof(SnapEmbeddedResourcesTypeRoot).Namespace);

            cecilReflector.RewriteOrThrow <SnapEmbeddedResources>(x => x.IsOptimized, (typedDefinition, getterName, setterName, propertyDefinition) =>
            {
                var getIlProcessor = propertyDefinition.GetMethod.Body.GetILProcessor();
                getIlProcessor.Body.Instructions.Clear();
                getIlProcessor.Emit(OpCodes.Ldc_I4_1);
                getIlProcessor.Emit(OpCodes.Ret);
            });

            return(assemblyDefinition);
        }
Example #3
0
        public CoreRunLib([JetBrains.Annotations.NotNull] ISnapFilesystem filesystem, OSPlatform osPlatform, [JetBrains.Annotations.NotNull] string workingDirectory)
        {
            if (filesystem == null)
            {
                throw new ArgumentNullException(nameof(filesystem));
            }
            if (workingDirectory == null)
            {
                throw new ArgumentNullException(nameof(workingDirectory));
            }

            if (!osPlatform.IsSupportedOsVersion())
            {
                throw new PlatformNotSupportedException();
            }

            _osPlatform = osPlatform;

            var filename = filesystem.PathCombine(workingDirectory, "libcorerun-");

            #if SNAP_BOOTSTRAP
            return;
            #endif

            var rid = osPlatform.BuildRid();
            if (osPlatform == OSPlatform.Windows)
            {
                filename += $"{rid}.dll";
                _libPtr   = NativeMethodsWindows.dlopen(filename);
            }
            else if (osPlatform == OSPlatform.Linux)
            {
                filename += $"{rid}.so";
                _libPtr   = NativeMethodsUnix.dlopen(filename, NativeMethodsUnix.libdl_RTLD_NOW | NativeMethodsUnix.libdl_RTLD_LOCAL);
            }

            if (_libPtr == IntPtr.Zero)
            {
                throw new FileNotFoundException($"Failed to load corerun: {filename}. " +
                                                $"OS: {osPlatform}. " +
                                                $"64-bit OS: {Environment.Is64BitOperatingSystem}. " +
                                                $"Last error: {Marshal.GetLastWin32Error()}.");
            }

            // generic
            pal_is_elevated = new Delegate <pal_is_elevated_delegate>(_libPtr, osPlatform);
            pal_set_icon    = new Delegate <pal_set_icon_delegate>(_libPtr, osPlatform);

            // filesystem
            pal_fs_chmod       = new Delegate <pal_fs_chmod_delegate>(_libPtr, osPlatform);
            pal_fs_file_exists = new Delegate <pal_fs_file_exists_delegate>(_libPtr, osPlatform);
        }