Ejemplo n.º 1
0
        public Context(Extractor extractor, TrapWriter trapWriter, string assemblyPath, bool extractPdbs)
            : base(extractor, trapWriter)
        {
            this.AssemblyPath    = assemblyPath;
            stream               = File.OpenRead(assemblyPath);
            PeReader             = new PEReader(stream, PEStreamOptions.PrefetchEntireImage);
            MdReader             = PeReader.GetMetadataReader();
            TypeSignatureDecoder = new Entities.TypeSignatureDecoder(this);

            globalNamespace            = new Lazy <Entities.Namespace>(() => Populate(new Entities.Namespace(this, "", null)));
            systemNamespace            = new Lazy <Entities.Namespace>(() => Populate(new Entities.Namespace(this, "System")));
            genericHandleFactory       = new CachedFunction <IGenericContext, Handle, IExtractedEntity>(CreateGenericHandle);
            namespaceFactory           = new CachedFunction <StringHandle, Entities.Namespace>(n => CreateNamespace(MdReader.GetString(n)));
            namespaceDefinitionFactory = new CachedFunction <NamespaceDefinitionHandle, Entities.Namespace>(CreateNamespace);
            sourceFiles     = new CachedFunction <PDB.ISourceFile, Entities.PdbSourceFile>(path => new Entities.PdbSourceFile(this, path));
            folders         = new CachedFunction <PathTransformer.ITransformedPath, Entities.Folder>(path => new Entities.Folder(this, path));
            sourceLocations = new CachedFunction <PDB.Location, Entities.PdbSourceLocation>(location => new Entities.PdbSourceLocation(this, location));

            defaultGenericContext = new EmptyContext(this);

            if (extractPdbs)
            {
                Pdb = PDB.PdbReader.Create(assemblyPath, PeReader);
                if (Pdb is not null)
                {
                    Extractor.Logger.Log(Util.Logging.Severity.Info, string.Format("Found PDB information for {0}", assemblyPath));
                }
            }
        }
Ejemplo n.º 2
0
        public void NestedPaths()
        {
            string tempDir = System.IO.Path.GetTempPath();
            string root1, root2, root3;

            if (Win32.IsWindows())
            {
                root1 = "E:";
                root2 = "e:";
                root3 = @"\";
            }
            else
            {
                root1 = "/E_";
                root2 = "/e_";
                root3 = "/";
            }

            using var logger = new LoggerMock();

            Assert.Equal($@"C:\Temp\source_archive\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", "def.cs").Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", "def.cs").Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\E_\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root1}\source\def.cs").Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\e_\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root2}\source\def.cs").Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}source\def.cs").Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}source\def.cs").Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\diskstation\share\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}{root3}diskstation\share\source\def.cs").Replace('/', '\\'));
        }
Ejemplo n.º 3
0
        public void TestDefaultLayout()
        {
            var layout  = new Semmle.Extraction.Layout(null, null, null);
            var project = layout.LookupProjectOrNull("foo.cs");

            // All files are mapped when there's no layout file.
            Assert.True(layout.FileInLayout("foo.cs"));

            // Test trap filename
            var tmpDir = Path.GetTempPath();

            Directory.SetCurrentDirectory(tmpDir);
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                // `Directory.SetCurrentDirectory()` doesn't seem to work on macOS,
                // so disable this test on macOS, for now
                Assert.NotEqual(Directory.GetCurrentDirectory(), tmpDir);
                return;
            }
            var f1 = project.GetTrapPath(Logger, "foo.cs");
            var g1 = TrapWriter.NestPaths(Logger, tmpDir, "foo.cs.trap.gz", TrapWriter.InnerPathComputation.ABSOLUTE);

            Assert.Equal(f1, g1);

            // Test trap file generation
            var trapwriterFilename = project.GetTrapPath(Logger, "foo.cs");

            using (var trapwriter = project.CreateTrapWriter(Logger, "foo.cs", false))
            {
                trapwriter.Emit("1=*");
                Assert.False(File.Exists(trapwriterFilename));
            }
            Assert.True(File.Exists(trapwriterFilename));
            File.Delete(trapwriterFilename);
        }
Ejemplo n.º 4
0
 public void TestTrapOverridesLayout()
 {
     // When you specify both a trap file and a layout, use the trap file.
     var layout = new Semmle.Extraction.Layout(Path.GetFullPath("snapshot\\trap"), null, "something.txt");
     Assert.True(layout.FileInLayout(new TransformedPathStub("bar.cs")));
     var subProject = layout.LookupProjectOrNull(new TransformedPathStub("foo.cs"));
     Assert.NotNull(subProject);
     var f1 = subProject!.GetTrapPath(logger, new TransformedPathStub("foo.cs"), TrapWriter.CompressionMode.Gzip);
     var g1 = TrapWriter.NestPaths(logger, Path.GetFullPath("snapshot\\trap"), "foo.cs.trap.gz");
     Assert.Equal(f1, g1);
 }
Ejemplo n.º 5
0
        public void TestTrapOverridesLayout()
        {
            // When you specify both a trap file and a layout, use the trap file.
            var layout = new Semmle.Extraction.Layout(Path.GetFullPath("snapshot\\trap"), null, "something.txt");

            Assert.True(layout.FileInLayout("bar.cs"));
            var f1 = layout.LookupProjectOrNull("foo.cs").GetTrapPath(Logger, "foo.cs");
            var g1 = TrapWriter.NestPaths(Logger, Path.GetFullPath("snapshot\\trap"), "foo.cs.trap.gz", TrapWriter.InnerPathComputation.ABSOLUTE);

            Assert.Equal(f1, g1);
        }
Ejemplo n.º 6
0
        public void TestMultipleSections()
        {
            File.WriteAllLines("layout.txt", new string[]
            {
                "# Section 1",
                "TRAP_FOLDER=" + Path.GetFullPath("snapshot\\trap1"),
                "ODASA_DB=snapshot\\db-csharp",
                "SOURCE_ARCHIVE=" + Path.GetFullPath("snapshot\\archive1"),
                "ODASA_BUILD_ERROR_DIR=snapshot\build-errors",
                "foo.cs",
                "# Section 2",
                "TRAP_FOLDER=" + Path.GetFullPath("snapshot\\trap2"),
                "ODASA_DB=snapshot\\db-csharp",
                "SOURCE_ARCHIVE=" + Path.GetFullPath("snapshot\\archive2"),
                "ODASA_BUILD_ERROR_DIR=snapshot\build-errors",
                "bar.cs",
            });

            var layout = new Semmle.Extraction.Layout(null, null, "layout.txt");

            // Use Section 2
            Assert.True(layout.FileInLayout(new TransformedPathStub("bar.cs")));
            var subProject = layout.LookupProjectOrNull(new TransformedPathStub("bar.cs"));
            Assert.NotNull(subProject);
            var f1 = subProject!.GetTrapPath(logger, new TransformedPathStub("bar.cs"), TrapWriter.CompressionMode.Gzip);
            var g1 = TrapWriter.NestPaths(logger, Path.GetFullPath("snapshot\\trap2"), "bar.cs.trap.gz");
            Assert.Equal(f1, g1);

            // Use Section 1
            Assert.True(layout.FileInLayout(new TransformedPathStub("foo.cs")));
            subProject = layout.LookupProjectOrNull(new TransformedPathStub("foo.cs"));
            Assert.NotNull(subProject);
            var f2 = subProject!.GetTrapPath(logger, new TransformedPathStub("foo.cs"), TrapWriter.CompressionMode.Gzip);
            var g2 = TrapWriter.NestPaths(logger, Path.GetFullPath("snapshot\\trap1"), "foo.cs.trap.gz");
            Assert.Equal(f2, g2);

            // boo.dll is not in the layout, so use layout from first section.
            Assert.False(layout.FileInLayout(new TransformedPathStub("boo.dll")));
            var f3 = layout.LookupProjectOrDefault(new TransformedPathStub("boo.dll")).GetTrapPath(logger, new TransformedPathStub("boo.dll"), TrapWriter.CompressionMode.Gzip);
            var g3 = TrapWriter.NestPaths(logger, Path.GetFullPath("snapshot\\trap1"), "boo.dll.trap.gz");
            Assert.Equal(f3, g3);

            // boo.cs is not in the layout, so return null
            Assert.False(layout.FileInLayout(new TransformedPathStub("boo.cs")));
            Assert.Null(layout.LookupProjectOrNull(new TransformedPathStub("boo.cs")));
        }
Ejemplo n.º 7
0
        public void NestedPaths()
        {
            string tempDir = System.IO.Path.GetTempPath();
            string root1, root2, root3;

            if (Win32.IsWindows())
            {
                root1 = "E:";
                root2 = "e:";
                root3 = @"\";
            }
            else
            {
                root1 = "/E_";
                root2 = "/e_";
                root3 = "/";
            }

            string formattedTempDir = tempDir.Replace('/', '\\').Replace(':', '_').Trim('\\');

            var logger = new LoggerMock();

            System.IO.Directory.SetCurrentDirectory(tempDir);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                // `Directory.SetCurrentDirectory()` doesn't seem to work on macOS,
                // so disable this test on macOS, for now
                Assert.NotEqual(Directory.GetCurrentDirectory(), tempDir);
                return;
            }

            Assert.Equal($@"C:\Temp\source_archive\{formattedTempDir}\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", "def.cs", TrapWriter.InnerPathComputation.ABSOLUTE).Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", "def.cs", TrapWriter.InnerPathComputation.RELATIVE).Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\E_\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root1}\source\def.cs", TrapWriter.InnerPathComputation.ABSOLUTE).Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\e_\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root2}\source\def.cs", TrapWriter.InnerPathComputation.RELATIVE).Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}source\def.cs", TrapWriter.InnerPathComputation.ABSOLUTE).Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}source\def.cs", TrapWriter.InnerPathComputation.RELATIVE).Replace('/', '\\'));

            Assert.Equal(@"C:\Temp\source_archive\diskstation\share\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}{root3}diskstation\share\source\def.cs", TrapWriter.InnerPathComputation.ABSOLUTE).Replace('/', '\\'));
        }
Ejemplo n.º 8
0
        public void TestLayoutFile()
        {
            File.WriteAllLines("layout.txt", new string[]
            {
                "# Section",
                "TRAP_FOLDER=" + Path.GetFullPath("snapshot\\trap"),
                "ODASA_DB=snapshot\\db-csharp",
                "SOURCE_ARCHIVE=" + Path.GetFullPath("snapshot\\archive"),
                "ODASA_BUILD_ERROR_DIR=snapshot\build-errors",
                "-foo.cs",
                "bar.cs",
                "-excluded",
                "excluded/foo.cs",
                "included"
            });

            var layout = new Semmle.Extraction.Layout(null, null, "layout.txt");

            // Test general pattern matching
            Assert.True(layout.FileInLayout("bar.cs"));
            Assert.False(layout.FileInLayout("foo.cs"));
            Assert.False(layout.FileInLayout("goo.cs"));
            Assert.False(layout.FileInLayout("excluded/bar.cs"));
            Assert.True(layout.FileInLayout("excluded/foo.cs"));
            Assert.True(layout.FileInLayout("included/foo.cs"));

            // Test the trap file
            var project            = layout.LookupProjectOrNull("bar.cs");
            var trapwriterFilename = project.GetTrapPath(Logger, "bar.cs");

            Assert.Equal(TrapWriter.NestPaths(Logger, Path.GetFullPath("snapshot\\trap"), "bar.cs.trap.gz", TrapWriter.InnerPathComputation.ABSOLUTE),
                         trapwriterFilename);

            // Test the source archive
            var trapWriter = project.CreateTrapWriter(Logger, "bar.cs", false);

            trapWriter.Archive("layout.txt", System.Text.Encoding.ASCII);
            var writtenFile = TrapWriter.NestPaths(Logger, Path.GetFullPath("snapshot\\archive"), "layout.txt", TrapWriter.InnerPathComputation.ABSOLUTE);

            Assert.True(File.Exists(writtenFile));
            File.Delete("layout.txt");
        }
Ejemplo n.º 9
0
        public void TestDefaultLayout()
        {
            var layout = new Semmle.Extraction.Layout(null, null, null);
            var project = layout.LookupProjectOrNull(new TransformedPathStub("foo.cs"));

            Assert.NotNull(project);

            // All files are mapped when there's no layout file.
            Assert.True(layout.FileInLayout(new TransformedPathStub("foo.cs")));

            // Test trap filename
            var tmpDir = Path.GetTempPath();
            Directory.SetCurrentDirectory(tmpDir);
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                // `Directory.SetCurrentDirectory()` seems to slightly change the path on macOS,
                // so adjusting it:
                Assert.NotEqual(Directory.GetCurrentDirectory(), tmpDir);
                tmpDir = "/private" + tmpDir;
                // Remove trailing slash:
                Assert.Equal('/', tmpDir[tmpDir.Length - 1]);
                tmpDir = tmpDir.Substring(0, tmpDir.Length - 1);
                Assert.Equal(Directory.GetCurrentDirectory(), tmpDir);
            }
            var f1 = project!.GetTrapPath(logger, new TransformedPathStub("foo.cs"), TrapWriter.CompressionMode.Gzip);
            var g1 = TrapWriter.NestPaths(logger, tmpDir, "foo.cs.trap.gz");
            Assert.Equal(f1, g1);

            // Test trap file generation
            var trapwriterFilename = project.GetTrapPath(logger, new TransformedPathStub("foo.cs"), TrapWriter.CompressionMode.Gzip);
            using (var trapwriter = project.CreateTrapWriter(logger, new TransformedPathStub("foo.cs"), TrapWriter.CompressionMode.Gzip, discardDuplicates: false))
            {
                trapwriter.Emit("1=*");
                Assert.False(File.Exists(trapwriterFilename));
            }
            Assert.True(File.Exists(trapwriterFilename));
            File.Delete(trapwriterFilename);
        }
Ejemplo n.º 10
0
 public static void Emit(this TrapWriter trapFile, string s)
 {
     trapFile.Emit(new StringTrapEmitter(s));
 }
Ejemplo n.º 11
0
 public static void Emit(this TrapWriter tw, string s)
 {
     tw.Emit(new StringTrapEmitter(s));
 }
Ejemplo n.º 12
0
 public Context(Extraction.Extractor e, Compilation c, TrapWriter trapWriter, IExtractionScope scope, bool addAssemblyTrapPrefix)
     : base(e, trapWriter, addAssemblyTrapPrefix)
 {
     Compilation = c;
     this.scope  = scope;
 }
Ejemplo n.º 13
0
 private static void ExtractCIL(TracingExtractor extractor, TrapWriter trapWriter, bool extractPdbs)
 {
     using var cilContext = new Context(extractor, trapWriter, extractor.OutputPath, extractPdbs);
     cilContext.Populate(new Assembly(cilContext));
     cilContext.PopulateAll();
 }