Esempio n. 1
0
        public void HandlesAsyncMethods()
        {
            var m = TestHelper.RewriteAndGetMethodWrappedInScope(@"
                using System.Threading.Tasks;

                class Program {
                    void M() => Main().Wait(200);

                    async Task Main() {
                        await Task.Delay(100);
                    }
                }
            ", "Program", "M", new AssemblyGuardSettings {
                ApiPolicy = ApiPolicy.SafeDefault()
                            .Namespace("System.Runtime.CompilerServices", ApiAccess.Allowed)
                            .Namespace("System.Threading.Tasks", ApiAccess.Allowed)
            },
                                                                 new RuntimeGuardSettings {
                TimeLimit = TimeSpan.FromMilliseconds(100),
            });
            var ex = Assert.Throws <TargetInvocationException>(() => m());

            Assert.IsType <AggregateException>(ex.InnerException);
            Assert.IsType <TimeGuardException>(ex.InnerException?.InnerException);
        }
Esempio n. 2
0
        public void ThrowsGuardException_ForAsyncMethodWithTry()
        {
            var compiled = TestHelper.Compile(@"
                using System;
                using System.Threading.Tasks;

                class Program {
                    async Task Main() {
                        Console.WriteLine();

                        try {
                            await Task.Yield();
                        } catch { }
                    }
                }
            ");
            var ex       = Assert.Throws <AssemblyGuardException>(
                () => AssemblyGuard.Rewrite(compiled, Stream.Null, new AssemblyGuardSettings {
                ApiPolicy = ApiPolicy.SafeDefault()
                            .Namespace("System.Runtime.CompilerServices", ApiAccess.Allowed)
                            .Namespace("System.Threading.Tasks", ApiAccess.Allowed)
            })
                );

            Assert.Contains("System.Console", ex.Message);
        }
Esempio n. 3
0
 public static ApiPolicy CreatePolicy() => ApiPolicy.SafeDefault()
 .Namespace("System", Neutral, SetupSystem)
 .Namespace("System.Collections.Concurrent", Neutral, SetupSystemCollectionsConcurrent)
 .Namespace("System.Collections.Specialized", Neutral, SetupSystemCollectionsSpecialized)
 .Namespace("System.Diagnostics", Neutral, SetupSystemDiagnostics)
 .Namespace("System.Globalization", Neutral, SetupSystemGlobalization)
 .Namespace("System.IO", Neutral, SetupSystemIO)
 .Namespace("System.Linq.Expressions", Neutral, SetupSystemLinqExpressions)
 .Namespace("System.Net", Neutral, SetupSystemNet)
 .Namespace("System.Numerics", Neutral, SetupSystemNumerics)
 .Namespace("System.Reflection", Neutral, SetupSystemReflection)
 .Namespace("System.Runtime.InteropServices", Neutral, SetupSystemRuntimeInteropServices)
 .Namespace("System.Security.Cryptography", Neutral, SetupSystemSecurityCryptography)
 .Namespace("System.Web", Neutral, SetupSystemWeb)
 .Namespace("SharpLab.Runtime.Internal", Neutral,
            n => n.Type(typeof(Flow), Neutral,
                        t => t.Member(nameof(Flow.ReportException), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportLineStart), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportValue), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportRefValue), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportSpanValue), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportRefSpanValue), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportReadOnlySpanValue), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportRefReadOnlySpanValue), Allowed, NoGuardRewriter.Default)
                        ).Type(typeof(MemoryGraphArgumentNames), Neutral,
                               t => t.Member(nameof(MemoryGraphArgumentNames.AllocateNext), Allowed, CountArgumentRewriter.Default)
                               .Member(nameof(MemoryGraphArgumentNames.AddToNext), Allowed, NoGuardRewriter.Default)
                               )
            )
 .Namespace("", Neutral,
            n => n.Type(typeof(SharpLabObjectExtensions), Allowed)
            .Type(typeof(Inspect), Allowed)
            )
     #if !NETCOREAPP
 .Namespace("Microsoft.FSharp.Core", Neutral, SetupFSharpCore)
 .Namespace("Microsoft.FSharp.Collections", Neutral,
            n => n.Type(typeof(FSharpList <>), Allowed)
            .Type(typeof(ListModule), Neutral,
                  t => t.Member(nameof(ListModule.Iterate), Allowed)
                  )
            .Type(typeof(SeqModule), Neutral,
                  t => t.Member(nameof(SeqModule.ToArray), Allowed, CollectedEnumerableArgumentRewriter.Default)
                  .Member(nameof(SeqModule.ToList), Allowed, CollectedEnumerableArgumentRewriter.Default)
                  )
            )
     #endif
 .Namespace("Microsoft.VisualBasic", Neutral, SetupMicrosoftVisualBasic)
 .Namespace("Microsoft.VisualBasic.CompilerServices", Neutral,
            n => n.Type(typeof(Conversions), Allowed,
                        t => t.Member(nameof(Conversions.FromCharAndCount), Allowed, new CountArgumentRewriter("Count"))
                        // Those need extra review
                        .Member(nameof(Conversions.ChangeType), Denied)
                        .Member(nameof(Conversions.FallbackUserDefinedConversion), Denied)
                        .Member(nameof(Conversions.FromCharArray), Denied)
                        .Member(nameof(Conversions.FromCharArraySubset), Denied)
                        .Member(nameof(Conversions.ToCharArrayRankOne), Denied)
                        )
            .Type(typeof(StandardModuleAttribute), Allowed)
            );
Esempio n. 4
0
        public void Type_CanAddTypeInEmptyNamespace()
        {
            var policy = ApiPolicy.SafeDefault();

            policy.Namespace("", ApiAccess.Neutral, n => {
                n.Type(typeof(TestClassWithoutNamespace), ApiAccess.Allowed);
            });

            Assert.Equal(ApiAccess.Allowed, policy.Namespaces[""].Types[nameof(TestClassWithoutNamespace)].Access);
        }
Esempio n. 5
0
 public static ApiPolicy CreatePolicy() => ApiPolicy.SafeDefault()
 .Namespace("System", Neutral, SetupSystem)
 .Namespace("System.Threading", Neutral, SetupThreading)
 .Namespace("System.Collections.Concurrent", Neutral, SetupSystemCollectionsConcurrent)
 .Namespace("System.Collections.Specialized", Neutral, SetupSystemCollectionsSpecialized)
 //.Namespace("System.Globalization", Neutral, SetupSystemGlobalization)
 //.Namespace("System.IO", Neutral, SetupSystemIO)
 //.Namespace("System.Linq.Expressions", Neutral, SetupSystemLinqExpressions)
 //.Namespace("System.Net", Neutral, SetupSystemNet)
 .Namespace("System.Numerics", Neutral, SetupSystemNumerics);
Esempio n. 6
0
 private AssemblyGuardSettings GetGuardSettingsForTestDisposable()
 {
     return(new AssemblyGuardSettings {
         ApiPolicy = ApiPolicy.SafeDefault().Namespace(
             "Unbreakable.Tests", ApiAccess.Neutral,
             n => n.Type(
                 typeof(TestDisposable), ApiAccess.Allowed,
                 t => t.Constructor(ApiAccess.Allowed, new DisposableReturnRewriter())
                 .Member(nameof(TestDisposable.GetSelfToRewrite), ApiAccess.Allowed, new DisposableReturnRewriter())
                 )
             )
     });
 }
Esempio n. 7
0
 public static ApiPolicy CreatePolicy() => ApiPolicy.SafeDefault()
 .Namespace("System", Neutral,
            n => n.Type(typeof(Console), Neutral,
                        t => t.Member(nameof(Console.Write), Allowed)
                        .Member(nameof(Console.WriteLine), Allowed)
                        // required by F#'s printf
                        .Getter(nameof(Console.Out), Allowed)
                        ).Type(typeof(STAThreadAttribute), Allowed)
            .Type(typeof(NotImplementedException), Neutral, t => t.Constructor(Allowed))
            .Type(typeof(Type), Neutral, SetupSystemType)
            )
 .Namespace("System.Diagnostics", Neutral, SetupSystemDiagnostics)
 .Namespace("System.Reflection", Neutral, SetupSystemReflection)
 .Namespace("System.Linq.Expressions", Neutral, SetupSystemLinqExpressions)
 .Namespace("System.IO", Neutral,
            // required by F#'s printf
            n => n.Type(typeof(TextWriter), Neutral)
            )
 .Namespace("SharpLab.Runtime.Internal", Neutral,
            n => n.Type(typeof(Flow), Neutral,
                        t => t.Member(nameof(Flow.ReportException), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportLineStart), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportValue), Allowed, NoGuardRewriter.Default)
                        )
            )
 .Namespace("", Neutral,
            n => n.Type(typeof(SharpLabObjectExtensions), Allowed)
            )
 .Namespace("Microsoft.FSharp.Core", Neutral, SetupFSharpCore)
 .Namespace("Microsoft.FSharp.Collections", Neutral,
            n => n.Type(typeof(FSharpList <>), Allowed)
            .Type(typeof(ListModule), Neutral,
                  t => t.Member(nameof(ListModule.Iterate), Allowed)
                  )
            .Type(typeof(SeqModule), Neutral,
                  t => t.Member(nameof(SeqModule.ToArray), Allowed, CollectedEnumerableArgumentRewriter.Default)
                  .Member(nameof(SeqModule.ToList), Allowed, CollectedEnumerableArgumentRewriter.Default)
                  )
            )
 .Namespace("Microsoft.VisualBasic.CompilerServices", Neutral,
            n => n.Type(typeof(Conversions), Allowed,
                        t => t.Member(nameof(Conversions.FromCharAndCount), Allowed, new CountArgumentRewriter("Count"))
                        // Those need extra review
                        .Member(nameof(Conversions.ChangeType), Denied)
                        .Member(nameof(Conversions.FallbackUserDefinedConversion), Denied)
                        .Member(nameof(Conversions.FromCharArray), Denied)
                        .Member(nameof(Conversions.FromCharArraySubset), Denied)
                        .Member(nameof(Conversions.ToCharArrayRankOne), Denied)
                        )
            .Type(typeof(StandardModuleAttribute), Allowed)
            );
Esempio n. 8
0
        public void HandlesGenericReferenceTypes(string code, int expected)
        {
            var m = TestHelper.RewriteAndGetMethodWrappedInScope(@"
                using System;
                using Unbreakable.Tests.Internal;
                class C {
                    " + code + @"
                }
            ", "C", "M", new AssemblyGuardSettings {
                ApiPolicy = ApiPolicy.SafeDefault().Namespace("Unbreakable.Tests.Internal", ApiAccess.Allowed)
            });

            Assert.Equal(expected, m());
        }
Esempio n. 9
0
        public void AllowsTypesInEmptyNamespace_IfAllowed()
        {
            var m = TestHelper.RewriteAndGetMethodWrappedInScope(@"
                using System;
                class C {
                    public object M() {
                        return new " + nameof(TestClassWithoutNamespace) + @"();
                    }
                }
            ", "C", "M", new AssemblyGuardSettings {
                ApiPolicy = ApiPolicy.SafeDefault().Namespace("", ApiAccess.Allowed)
            });

            Assert.NotNull(m());
        }
Esempio n. 10
0
        public void ThrowsGuardException_ForPointerTypes(string code)
        {
            // pointers, available in local functions due to a Roslyn bug, reported by Andy Gocke‏ (@andygocke)
            var policy = ApiPolicy.SafeDefault()
                         .Namespace("System.Security", ApiAccess.Neutral, n => n.Type(typeof(UnverifiableCodeAttribute), ApiAccess.Allowed));
            var compiled = TestHelper.Compile(@"
                class X {
                    unsafe void M(int a) { " + code + @" }
                }
            ", allowUnsafe: true);

            Assert.Throws <AssemblyGuardException>(
                () => AssemblyGuard.Rewrite(compiled, new MemoryStream(), new AssemblyGuardSettings {
                ApiPolicy = policy
            })
                );
        }
Esempio n. 11
0
        public void ThrowsGuardException_ForDelegatesCreatedFromMethodsWithRewriters(string code)
        {
            var compiled = TestHelper.Compile(@"
                using System;
                using System.Linq.Expressions;
                class C {
                    void M() { " + code + @" }
                }"
                                              );
            var policy = ApiPolicy.SafeDefault()
                         .Namespace("System.Linq.Expressions", ApiAccess.Allowed)
                         .Namespace("System.Reflection", ApiAccess.Allowed);

            Assert.Throws <AssemblyGuardException>(
                () => AssemblyGuard.Rewrite(compiled, new MemoryStream(), new AssemblyGuardSettings {
                ApiPolicy = policy
            })
                );
        }
Esempio n. 12
0
        public void DoesNotEnforceApiPolicy_ForUserCode()
        {
            var compiled = TestHelper.Compile(@"
                namespace N {
                    class C1 {
                        void M(C2 c2) { c2.M(); }
                    }

                    class C2 {
                        public void M() {}
                    }
                }
            ");

            var exception = Record.Exception(() => AssemblyGuard.Rewrite(compiled, new MemoryStream(), new AssemblyGuardSettings {
                ApiPolicy = ApiPolicy.SafeDefault().Namespace("N", ApiAccess.Denied)
            }));

            Assert.Null(exception);
        }
Esempio n. 13
0
        public void HandlesAsyncMethods()
        {
            var m = TestHelper.RewriteAndGetMethodWrappedInScope(@"
                using System.Threading.Tasks;

                class Program {
                    bool M() => Main().Wait(100);

                    async Task Main() {
                        await Task.Yield();
                    }
                }
            ", "Program", "M", new AssemblyGuardSettings {
                ApiPolicy = ApiPolicy.SafeDefault()
                            .Namespace("System.Runtime.CompilerServices", ApiAccess.Allowed)
                            .Namespace("System.Threading.Tasks", ApiAccess.Allowed)
            });

            Assert.True(m() is true);
        }
Esempio n. 14
0
 public static ApiPolicy CreatePolicy() => ApiPolicy.SafeDefault()
 .Namespace("System", Neutral,
            n => n.Type(typeof(Console), Neutral,
                        t => t.Member(nameof(Console.Write), Allowed)
                        .Member(nameof(Console.WriteLine), Allowed)
                        // required by F#'s printf
                        .Getter(nameof(Console.Out), Allowed)
                        ).Type(typeof(STAThreadAttribute), Allowed)
            .Type(typeof(NotImplementedException), Neutral, t => t.Constructor(Allowed))
            .Type(typeof(Type), Neutral, SetupSystemType)
            )
 .Namespace("System.Diagnostics", Neutral, SetupSystemDiagnostics)
 .Namespace("System.Reflection", Neutral, SetupSystemReflection)
 .Namespace("System.Linq.Expressions", Neutral, SetupSystemLinqExpressions)
 .Namespace("System.IO", Neutral,
            // required by F#'s printf
            n => n.Type(typeof(TextWriter), Neutral)
            )
 .Namespace("SharpLab.Runtime.Internal", Neutral,
            n => n.Type(typeof(Flow), Neutral,
                        t => t.Member(nameof(Flow.ReportException), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportLineStart), Allowed, NoGuardRewriter.Default)
                        .Member(nameof(Flow.ReportValue), Allowed, NoGuardRewriter.Default)
                        )
            )
 .Namespace("", Neutral,
            n => n.Type(typeof(SharpLabObjectExtensions), Allowed)
            )
 .Namespace("Microsoft.FSharp.Core", Neutral,
            n => n.Type(typeof(CompilationArgumentCountsAttribute), Allowed)
            .Type(typeof(CompilationMappingAttribute), Allowed)
            .Type(typeof(EntryPointAttribute), Allowed)
            .Type(typeof(ExtraTopLevelOperators), Neutral,
                  t => t.Member(nameof(ExtraTopLevelOperators.CreateDictionary), Allowed, CollectedEnumerableArgumentRewriter.Default)
                  .Member(nameof(ExtraTopLevelOperators.CreateSet), Allowed, CollectedEnumerableArgumentRewriter.Default)
                  .Member(nameof(ExtraTopLevelOperators.LazyPattern), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.PrintFormat), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.PrintFormatLine), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.PrintFormatToTextWriter), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.PrintFormatLineToTextWriter), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.PrintFormatToString), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.SpliceExpression), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.SpliceUntypedExpression), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.ToByte), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.ToDouble), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.ToSByte), Allowed)
                  .Member(nameof(ExtraTopLevelOperators.ToSingle), Allowed)
                  )
            .Type(typeof(ExtraTopLevelOperators.Checked), Allowed)
            .Type(typeof(FSharpChoice <,>), Allowed)
            .Type(typeof(FSharpFunc <,>), Allowed)
            .Type(typeof(FSharpOption <>), Allowed)
            .Type(typeof(LanguagePrimitives), Neutral,
                  t => t.Getter(nameof(LanguagePrimitives.GenericComparer), Allowed)
                  .Getter(nameof(LanguagePrimitives.GenericEqualityComparer), Allowed)
                  .Getter(nameof(LanguagePrimitives.GenericEqualityERComparer), Allowed)
                  )
            .Type(typeof(OptimizedClosures.FSharpFunc <, ,>), Allowed)
            .Type(typeof(OptimizedClosures.FSharpFunc <, , ,>), Allowed)
            .Type(typeof(OptimizedClosures.FSharpFunc <, , , ,>), Allowed)
            .Type(typeof(Microsoft.FSharp.Core.Operators), Allowed,
                  t => t.Member("ConsoleError", Denied)
                  .Member("ConsoleIn", Denied)
                  .Member("ConsoleOut", Denied)
                  .Member("Lock", Denied)
                  )
            .Type(typeof(PrintfFormat <, , ,>), Allowed)
            .Type(typeof(PrintfFormat <, , , ,>), Allowed)
            .Type(typeof(PrintfModule), Neutral,
                  t => t.Member(nameof(PrintfModule.PrintFormat), Allowed)
                  .Member(nameof(PrintfModule.PrintFormatLine), Allowed)
                  .Member(nameof(PrintfModule.PrintFormatToTextWriter), Allowed)
                  .Member(nameof(PrintfModule.PrintFormatLineToTextWriter), Allowed)
                  )
            .Type(typeof(Unit), Allowed)
            )
 .Namespace("Microsoft.FSharp.Collections", Neutral,
            n => n.Type(typeof(FSharpList <>), Allowed)
            )
 .Namespace("Microsoft.VisualBasic.CompilerServices", Neutral,
            n => n.Type(typeof(Conversions), Allowed,
                        t => t.Member(nameof(Conversions.FromCharAndCount), Allowed, new CountArgumentRewriter("Count"))
                        // Those need extra review
                        .Member(nameof(Conversions.ChangeType), Denied)
                        .Member(nameof(Conversions.FallbackUserDefinedConversion), Denied)
                        .Member(nameof(Conversions.FromCharArray), Denied)
                        .Member(nameof(Conversions.FromCharArraySubset), Denied)
                        .Member(nameof(Conversions.ToCharArrayRankOne), Denied)
                        )
            .Type(typeof(StandardModuleAttribute), Allowed)
            );