Beispiel #1
0
        public void AsyncAwaitDeepPatch()
        {
            var snippetSource   = File.ReadAllText(@"..\..\Tests\AsyncAwait.cs");
            var compiledSnippet = Pumpkin.SnippetCompiler.CompileWithCSC(snippetSource, Path.GetTempPath());

            Assert.IsTrue(compiledSnippet.success);
            var patchedAssemblyBytes = Pumpkin.SnippetCompiler.PatchAssembly(compiledSnippet.assemblyBytes, "Snippets.AsyncAwait");

            File.WriteAllBytes(Path.Combine(Path.GetTempPath(), "AsyncAwait-patched.dll"), patchedAssemblyBytes);

            var clientAssembly = AppDomain.CurrentDomain.Load(patchedAssemblyBytes);

            var type = clientAssembly.GetTypes().Where(t => t.FullName == "Snippets.AsyncAwait").SingleOrDefault();

            Assert.IsNotNull(type);


            var monitorField = type.GetField(Pumpkin.Monitor.MonitorFieldName, BindingFlags.Public | BindingFlags.Static);

            Assert.IsNotNull(monitorField);

            var resultOutput = new List <string>();
            var monitor      = new Pumpkin.Monitor(resultOutput);

            monitorField.SetValue(null, monitor);


            var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
            var target       = type.GetMethod("SnippetMain", bindingFlags);

            Assert.IsNotNull(target);

            //Now invoke the method.
            target.Invoke(null, null);

            Assert.AreEqual(resultOutput[0], "Hello Foo");
        }
Beispiel #2
0
        // This kind of customization (provide a custom HostSecurityManager, with custom DomainPolicy etc.)
        // is obsolete in .NET4. Setting the PermissionSet on the AppDomain (as we do here) is the right thing to do
        // See http://msdn.microsoft.com/en-us/library/ee191568(VS.100).aspx
        // and http://msdn.microsoft.com/en-us/library/bb763046(v=vs.100).aspx
        //public override HostSecurityManager HostSecurityManager {
        //   get { return new SimpleHostSecurityManager(); }
        //}

        //[SecuritySafeCritical]
        internal SnippetResult InternalRun(AppDomain appDomain, byte[] assembly, string mainTypeName, string methodName,
                                           bool runningInSandbox)
        {
            // Here we already are on the new domain
            SnippetResult result = new SnippetResult();

            try {
                // Use this "trick" to go through the standard loader path.
                // This MAY be something we want, or something to avoid

                // When loading the assembly, we need at least FileIOPermission.
                // Calling it with a full-trust stack. TODO: only what is needed
                (new PermissionSet(PermissionState.Unrestricted)).Assert();
                var clientAssembly = appDomain.Load(assembly);
                //var clientAssembly = Assembly.Load(assembly, null, SecurityContextSource.CurrentAppDomain);
                //AssemblyName an = AssemblyName.GetAssemblyName(assemblyFileName);
                //var clientAssembly = appDomain.Load(an);
                CodeAccessPermission.RevertAssert();

                //Load the MethodInfo for a method in the new Assembly. This might be a method you know, or
                //you can use Assembly.EntryPoint to get to the main function in an executable.
                var type = clientAssembly.GetTypes().Where(t => t.Name == mainTypeName || t.FullName == mainTypeName).SingleOrDefault();
                if (type == null)
                {
                    result.status = SnippetStatus.InitializationError;
                    return(result);
                }


                var monitorField = type.GetField(Pumpkin.Monitor.MonitorFieldName, BindingFlags.Public | BindingFlags.Static);
                if (monitorField != null)
                {
                    result.output = new List <string>();
                    var monitor = new Pumpkin.Monitor(result.output);
                    monitorField.SetValue(null, monitor);
                }

                // To allow code to invoke any nonpublic member: Your code must be granted ReflectionPermission with the ReflectionPermissionFlag.MemberAccess flag
                // To allow code to invoke any nonpublic member, as long as the grant set of the assembly that contains the invoked member is
                // the same as, or a subset of, the grant set of the assembly that contains the invoking code:
                // Your code must be granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag.
                // See http://msdn.microsoft.com/en-us/library/stfy7tfc%28v=vs.110%29.aspx
                var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
                if (runningInSandbox)
                {
                    bindingFlags = BindingFlags.Public | BindingFlags.Static;
                }

                var target = type.GetMethod(methodName, bindingFlags);
                if (target == null)
                {
                    result.status = SnippetStatus.InitializationError;
                    return(result);
                }

                //Now invoke the method.
                target.Invoke(null, null);
                result.status = SnippetStatus.Success;
            }
            catch (TargetInvocationException ex) {
                result.exception = (ex.InnerException == null ? ex.Message : ex.InnerException.Message);

                // When we print informations from a SecurityException extra information can be printed if we are
                //calling it with a full-trust stack.
                (new PermissionSet(PermissionState.Unrestricted)).Assert();
                System.Diagnostics.Debug.WriteLine("Exception caught: =================" +
                                                   result.exception.ToString() +
                                                   "===================================");
                CodeAccessPermission.RevertAssert();

                result.status = SnippetStatus.ExecutionError;
            }
            catch (Exception ex) {
                (new PermissionSet(PermissionState.Unrestricted)).Assert();
                System.Diagnostics.Debug.WriteLine("Exception caught: =================" +
                                                   ex.ToString() +
                                                   "===================================");
                CodeAccessPermission.RevertAssert();
                result.exception = ex.Message;
                result.status    = SnippetStatus.ExecutionError;
            }
            return(result);
        }
 public static void SnippetMain(Pumpkin.Monitor monitor)
 {
     Console.WriteLine(f(10).ToString());
 }