Extend base context in CIL project

This commit is contained in:
Tamas Vajk
2021-02-17 09:51:00 +01:00
parent 5fca946678
commit 539fdf952a
11 changed files with 28 additions and 33 deletions

View File

@@ -27,10 +27,10 @@ namespace Semmle.Extraction.CIL
}
else
{
e.Label = Cx.GetNewLabel();
Cx.DefineLabel(e, Cx.TrapWriter.Writer, Cx.Extractor);
e.Label = GetNewLabel();
DefineLabel(e, TrapWriter.Writer, Extractor);
ids.Add(e, e.Label);
Cx.PopulateLater(() =>
PopulateLater(() =>
{
foreach (var c in e.Contents)
c.Extract(this);
@@ -42,7 +42,7 @@ namespace Semmle.Extraction.CIL
if (debugLabels.TryGetValue(id, out var previousEntity))
{
Cx.Extractor.Message(new Message("Duplicate trap ID", id, null, severity: Util.Logging.Severity.Warning));
Extractor.Message(new Message("Duplicate trap ID", id, null, severity: Util.Logging.Severity.Warning));
}
else
{
@@ -74,9 +74,9 @@ namespace Semmle.Extraction.CIL
{
e = new PrimitiveType(this, code)
{
Label = Cx.GetNewLabel()
Label = GetNewLabel()
};
Cx.DefineLabel(e, Cx.TrapWriter.Writer, Cx.Extractor);
DefineLabel(e, TrapWriter.Writer, Extractor);
primitiveTypes[(int)code] = e;
}

View File

@@ -10,12 +10,10 @@ namespace Semmle.Extraction.CIL
/// Adds additional context that is specific for CIL extraction.
/// One context = one DLL/EXE.
/// </summary>
public sealed partial class Context : IDisposable
public sealed partial class Context : Extraction.Context, IDisposable
{
private readonly FileStream stream;
private Entities.Assembly? assemblyNull;
public Extraction.Context Cx { get; }
public MetadataReader MdReader { get; }
public PEReader PeReader { get; }
public string AssemblyPath { get; }
@@ -26,9 +24,9 @@ namespace Semmle.Extraction.CIL
}
public PDB.IPdb? Pdb { get; }
public Context(Extraction.Context cx, string assemblyPath, bool extractPdbs)
public Context(Extractor extractor, TrapWriter trapWriter, string assemblyPath, bool extractPdbs)
: base(extractor, trapWriter)
{
this.Cx = cx;
this.AssemblyPath = assemblyPath;
stream = File.OpenRead(assemblyPath);
PeReader = new PEReader(stream, PEStreamOptions.PrefetchEntireImage);
@@ -51,7 +49,7 @@ namespace Semmle.Extraction.CIL
Pdb = PDB.PdbReader.Create(assemblyPath, PeReader);
if (Pdb != null)
{
cx.Extractor.Logger.Log(Util.Logging.Severity.Info, string.Format("Found PDB information for {0}", assemblyPath));
Extractor.Logger.Log(Util.Logging.Severity.Info, string.Format("Found PDB information for {0}", assemblyPath));
}
}
}

View File

@@ -76,7 +76,7 @@ namespace Semmle.Extraction.CIL.Entities
}
catch (InternalError e)
{
Cx.Cx.ExtractionError("Error processing type definition", e.Message, GeneratedLocation.Create(Cx.Cx), e.StackTrace);
Cx.ExtractionError("Error processing type definition", e.Message, GeneratedLocation.Create(Cx), e.StackTrace);
}
// Limitation of C#: Cannot yield return inside a try-catch.
@@ -93,7 +93,7 @@ namespace Semmle.Extraction.CIL.Entities
}
catch (InternalError e)
{
Cx.Cx.ExtractionError("Error processing bytecode", e.Message, GeneratedLocation.Create(Cx.Cx), e.StackTrace);
Cx.ExtractionError("Error processing bytecode", e.Message, GeneratedLocation.Create(Cx), e.StackTrace);
}
if (product != null)
@@ -102,11 +102,11 @@ namespace Semmle.Extraction.CIL.Entities
}
}
private static void ExtractCIL(Extraction.Context cx, string assemblyPath, bool extractPdbs)
private static void ExtractCIL(Extractor extractor, TrapWriter trapWriter, bool extractPdbs)
{
using var cilContext = new Context(cx, assemblyPath, extractPdbs);
using var cilContext = new Context(extractor, trapWriter, extractor.OutputPath, extractPdbs);
cilContext.Populate(new Assembly(cilContext));
cilContext.Cx.PopulateAll();
cilContext.PopulateAll();
}
/// <summary>
@@ -135,8 +135,7 @@ namespace Semmle.Extraction.CIL.Entities
trapFile = trapWriter.TrapFile;
if (nocache || !System.IO.File.Exists(trapFile))
{
var cx = new Extraction.Context(extractor, trapWriter);
ExtractCIL(cx, assemblyPath, extractPdbs);
ExtractCIL(extractor, trapWriter, extractPdbs);
extracted = true;
}
}

View File

@@ -45,7 +45,7 @@ namespace Semmle.Extraction.CIL.Entities
}
catch
{
Cx.Cx.Extractor.Logger.Log(Util.Logging.Severity.Info,
Cx.Extractor.Logger.Log(Util.Logging.Severity.Info,
$"Attribute decoding is partial. Decoding attribute {constructor.DeclaringType.GetQualifiedName()} failed on {@object}.");
yield break;
}

View File

@@ -11,11 +11,10 @@ namespace Semmle.Extraction.CIL
public abstract class LabelledEntity : Extraction.LabelledEntity, IExtractedEntity
{
// todo: with .NET 5 this can override the base context, and change the return type.
public Context Cx { get; }
public Context Cx => (Context)base.Context;
protected LabelledEntity(Context cx) : base(cx.Cx)
protected LabelledEntity(Context cx) : base(cx)
{
this.Cx = cx;
}
public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException();

View File

@@ -14,7 +14,7 @@ namespace Semmle.Extraction.CIL
public void Extract(Context cx)
{
cx.Cx.TrapWriter.Emit(tuple);
cx.TrapWriter.Emit(tuple);
}
public override string ToString() => tuple.ToString();

View File

@@ -10,11 +10,10 @@ namespace Semmle.Extraction.CIL
public abstract class UnlabelledEntity : Extraction.UnlabelledEntity, IExtractedEntity
{
// todo: with .NET 5 this can override the base context, and change the return type.
public Context Cx { get; }
public Context Cx => (Context)base.Context;
protected UnlabelledEntity(Context cx) : base(cx.Cx)
protected UnlabelledEntity(Context cx) : base(cx)
{
Cx = cx;
}
public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException();

View File

@@ -40,11 +40,11 @@ namespace Semmle.Extraction.CIL.Entities
if (wellKnownEnums.TryGetValue(name, out var code))
{
cx.Cx.Extractor.Logger.Log(Util.Logging.Severity.Debug, $"Using hard coded underlying enum type for {name}");
cx.Extractor.Logger.Log(Util.Logging.Severity.Debug, $"Using hard coded underlying enum type for {name}");
return code;
}
cx.Cx.Extractor.Logger.Log(Util.Logging.Severity.Info, $"Couldn't get underlying enum type for {name}");
cx.Extractor.Logger.Log(Util.Logging.Severity.Info, $"Couldn't get underlying enum type for {name}");
// We can't fall back to Int32, because the type returned here defines how many bytes are read from the
// stream and how those bytes are interpreted.

View File

@@ -11,7 +11,7 @@ namespace Semmle.Extraction.CIL.Entities
public File(Context cx, string path) : base(cx)
{
this.OriginalPath = path;
TransformedPath = cx.Cx.Extractor.PathTransformer.Transform(OriginalPath);
TransformedPath = Cx.Extractor.PathTransformer.Transform(OriginalPath);
}
public override void WriteId(TextWriter trapFile)

View File

@@ -476,7 +476,7 @@ namespace Semmle.Extraction.CIL.Entities
// TODO: Find a solution to this.
// For now, just log the error
Cx.Cx.ExtractionError("A CIL instruction jumps outside the current method", null, Extraction.Entities.GeneratedLocation.Create(Cx.Cx), "", Util.Logging.Severity.Warning);
Cx.ExtractionError("A CIL instruction jumps outside the current method", null, Extraction.Entities.GeneratedLocation.Create(Cx), "", Util.Logging.Severity.Warning);
}
}
}

View File

@@ -21,9 +21,9 @@ namespace Semmle.Extraction.CIL.Entities
var text = file.Contents;
if (text == null)
Cx.Cx.Extractor.Logger.Log(Util.Logging.Severity.Warning, string.Format("PDB source file {0} could not be found", OriginalPath));
Cx.Extractor.Logger.Log(Util.Logging.Severity.Warning, string.Format("PDB source file {0} could not be found", OriginalPath));
else
Cx.Cx.TrapWriter.Archive(TransformedPath, text);
Cx.TrapWriter.Archive(TransformedPath, text);
yield return Tuples.file_extraction_mode(this, 2);
}