using Microsoft.CodeAnalysis;
using Semmle.Extraction.Entities;
using Semmle.Util.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
namespace Semmle.Extraction
{
///
/// State that needs to be available throughout the extraction process.
/// There is one Context object per trap output file.
///
public class Context
{
///
/// Access various extraction functions, e.g. logger, trap writer.
///
public Extractor Extractor { get; }
///
/// Access to the trap file.
///
public TrapWriter TrapWriter { get; }
///
/// Holds if assembly information should be prefixed to TRAP labels.
///
public bool ShouldAddAssemblyTrapPrefix { get; }
public IList TrapStackSuffix { get; } = new List();
private int GetNewId() => TrapWriter.IdCounter++;
// A recursion guard against writing to the trap file whilst writing an id to the trap file.
private bool writingLabel = false;
private readonly Queue labelQueue = new();
protected void DefineLabel(IEntity entity)
{
if (writingLabel)
{
// Don't define a label whilst writing a label.
labelQueue.Enqueue(entity);
}
else
{
try
{
writingLabel = true;
entity.DefineLabel(TrapWriter.Writer, Extractor);
}
finally
{
writingLabel = false;
if (labelQueue.Any())
{
DefineLabel(labelQueue.Dequeue());
}
}
}
}
#if DEBUG_LABELS
private void CheckEntityHasUniqueLabel(string id, CachedEntity entity)
{
if (idLabelCache.ContainsKey(id))
{
this.Extractor.Message(new Message("Label collision for " + id, entity.Label.ToString(), CreateLocation(entity.ReportingLocation), "", Severity.Warning));
}
else
{
idLabelCache[id] = entity;
}
}
#endif
protected Label GetNewLabel() => new Label(GetNewId());
internal TEntity CreateEntity(CachedEntityFactory factory, object cacheKey, TInit init)
where TEntity : CachedEntity =>
cacheKey is ISymbol s ? CreateEntity(factory, s, init, symbolEntityCache) : CreateEntity(factory, cacheKey, init, objectEntityCache);
internal TEntity CreateEntityFromSymbol(CachedEntityFactory factory, TSymbol init)
where TSymbol : ISymbol
where TEntity : CachedEntity => CreateEntity(factory, init, init, symbolEntityCache);
///
/// Creates and populates a new entity, or returns the existing one from the cache.
///
/// The entity factory.
/// The key used for caching.
/// The initializer for the entity.
/// The dictionary to use for caching.
/// The new/existing entity.
private TEntity CreateEntity(CachedEntityFactory factory, TCacheKey cacheKey, TInit init, IDictionary dictionary)
where TCacheKey : notnull
where TEntity : CachedEntity
{
if (dictionary.TryGetValue(cacheKey, out var cached))
return (TEntity)cached;
using (StackGuard)
{
var label = GetNewLabel();
var entity = factory.Create(this, init);
entity.Label = label;
dictionary[cacheKey] = entity;
DefineLabel(entity);
if (entity.NeedsPopulation)
Populate(init as ISymbol, entity);
#if DEBUG_LABELS
using var id = new EscapingTextWriter();
entity.WriteQuotedId(id);
CheckEntityHasUniqueLabel(id.ToString(), entity);
#endif
return entity;
}
}
///
/// Creates a fresh label with ID "*", and set it on the
/// supplied object.
///
internal void AddFreshLabel(Entity entity)
{
entity.Label = GetNewLabel();
entity.DefineFreshLabel(TrapWriter.Writer);
}
#if DEBUG_LABELS
private readonly Dictionary idLabelCache = new Dictionary();
#endif
private readonly IDictionary objectEntityCache = new Dictionary();
private readonly IDictionary symbolEntityCache = new Dictionary(10000, SymbolEqualityComparer.Default);
///
/// Queue of items to populate later.
/// The only reason for this is so that the call stack does not
/// grow indefinitely, causing a potential stack overflow.
///
private readonly Queue populateQueue = new Queue();
///
/// Enqueue the given action to be performed later.
///
/// The action to run.
public void PopulateLater(Action a)
{
var key = GetCurrentTagStackKey();
if (key is not null)
{
// If we are currently executing with a duplication guard, then the same
// guard must be used for the deferred action
populateQueue.Enqueue(() => WithDuplicationGuard(key, a));
}
else
{
populateQueue.Enqueue(a);
}
}
///
/// Runs the main populate loop until there's nothing left to populate.
///
public void PopulateAll()
{
while (populateQueue.Any())
{
try
{
populateQueue.Dequeue()();
}
catch (InternalError ex)
{
ExtractionError(new Message(ex.Text, ex.EntityText, CreateLocation(ex.Location), ex.StackTrace));
}
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
ExtractionError($"Uncaught exception. {ex.Message}", null, CreateLocation(), ex.StackTrace);
}
}
}
protected Context(Extractor extractor, TrapWriter trapWriter, bool shouldAddAssemblyTrapPrefix = false)
{
Extractor = extractor;
TrapWriter = trapWriter;
ShouldAddAssemblyTrapPrefix = shouldAddAssemblyTrapPrefix;
}
private int currentRecursiveDepth = 0;
private const int maxRecursiveDepth = 150;
private void EnterScope()
{
if (currentRecursiveDepth >= maxRecursiveDepth)
throw new StackOverflowException(string.Format("Maximum nesting depth of {0} exceeded", maxRecursiveDepth));
++currentRecursiveDepth;
}
private void ExitScope()
{
--currentRecursiveDepth;
}
public IDisposable StackGuard => new ScopeGuard(this);
private sealed class ScopeGuard : IDisposable
{
private readonly Context cx;
public ScopeGuard(Context c)
{
cx = c;
cx.EnterScope();
}
public void Dispose()
{
cx.ExitScope();
}
}
private class PushEmitter : ITrapEmitter
{
private readonly Key key;
public PushEmitter(Key key)
{
this.key = key;
}
public void EmitTrap(TextWriter trapFile)
{
trapFile.Write(".push ");
key.AppendTo(trapFile);
trapFile.WriteLine();
}
}
private class PopEmitter : ITrapEmitter
{
public void EmitTrap(TextWriter trapFile)
{
trapFile.WriteLine(".pop");
}
}
private readonly Stack tagStack = new Stack();
///
/// Populates an entity, handling the tag stack appropriately
///
/// Symbol for reporting errors.
/// The entity to populate.
/// Thrown on invalid trap stack behaviour.
private void Populate(ISymbol? optionalSymbol, CachedEntity entity)
{
if (writingLabel)
{
// Don't write tuples etc if we're currently defining a label
PopulateLater(() => Populate(optionalSymbol, entity));
return;
}
bool duplicationGuard, deferred;
switch (entity.TrapStackBehaviour)
{
case TrapStackBehaviour.NeedsLabel:
if (!tagStack.Any())
ExtractionError("TagStack unexpectedly empty", optionalSymbol, entity);
duplicationGuard = false;
deferred = false;
break;
case TrapStackBehaviour.NoLabel:
duplicationGuard = false;
deferred = tagStack.Any();
break;
case TrapStackBehaviour.OptionalLabel:
duplicationGuard = false;
deferred = false;
break;
case TrapStackBehaviour.PushesLabel:
duplicationGuard = true;
deferred = duplicationGuard && tagStack.Any();
break;
default:
throw new InternalError("Unexpected TrapStackBehaviour");
}
var a = duplicationGuard && IsEntityDuplicationGuarded(entity, out var loc)
? (() =>
{
var args = new object[TrapStackSuffix.Count + 2];
args[0] = entity;
args[1] = loc;
for (var i = 0; i < TrapStackSuffix.Count; i++)
{
args[i + 2] = TrapStackSuffix[i];
}
WithDuplicationGuard(new Key(args), () => entity.Populate(TrapWriter.Writer));
})
: (Action)(() => this.Try(null, optionalSymbol, () => entity.Populate(TrapWriter.Writer)));
if (deferred)
populateQueue.Enqueue(a);
else
a();
}
protected virtual bool IsEntityDuplicationGuarded(IEntity entity, [NotNullWhen(returnValue: true)] out Entities.Location? loc)
{
loc = null;
return false;
}
///
/// Runs the given action , guarding for trap duplication
/// based on key .
///
public virtual void WithDuplicationGuard(Key key, Action a)
{
tagStack.Push(key);
TrapWriter.Emit(new PushEmitter(key));
try
{
a();
}
finally
{
TrapWriter.Emit(new PopEmitter());
tagStack.Pop();
}
}
protected Key? GetCurrentTagStackKey() => tagStack.Count > 0
? tagStack.Peek()
: null;
///
/// Log an extraction error.
///
/// The error message.
/// A textual representation of the failed entity.
/// The location of the error.
/// An optional stack trace of the error, or null.
/// The severity of the error.
public void ExtractionError(string message, string? entityText, Entities.Location? location, string? stackTrace = null, Severity severity = Severity.Error)
{
var msg = new Message(message, entityText, location, stackTrace, severity);
ExtractionError(msg);
}
///
/// Log an extraction error.
///
/// The text of the message.
/// The symbol of the error, or null.
/// The entity of the error, or null.
private void ExtractionError(string message, ISymbol? optionalSymbol, Entity optionalEntity)
{
if (!(optionalSymbol is null))
{
ExtractionError(message, optionalSymbol.ToDisplayString(), CreateLocation(optionalSymbol.Locations.FirstOrDefault()));
}
else if (!(optionalEntity is null))
{
ExtractionError(message, optionalEntity.Label.ToString(), CreateLocation(optionalEntity.ReportingLocation));
}
else
{
ExtractionError(message, null, CreateLocation());
}
}
///
/// Log an extraction message.
///
/// The message to log.
private void ExtractionError(Message msg)
{
new ExtractionMessage(this, msg);
Extractor.Message(msg);
}
private void ExtractionError(InternalError error)
{
ExtractionError(new Message(error.Message, error.EntityText, CreateLocation(error.Location), error.StackTrace, Severity.Error));
}
private void ReportError(InternalError error)
{
if (!Extractor.Mode.HasFlag(ExtractorMode.Standalone))
throw error;
ExtractionError(error);
}
///
/// Signal an error in the program model.
///
/// The syntax node causing the failure.
/// The error message.
public void ModelError(SyntaxNode node, string msg)
{
ReportError(new InternalError(node, msg));
}
///
/// Signal an error in the program model.
///
/// Symbol causing the error.
/// The error message.
public void ModelError(ISymbol symbol, string msg)
{
ReportError(new InternalError(symbol, msg));
}
///
/// Signal an error in the program model.
///
/// The location of the error.
/// The error message.
public void ModelError(Entities.Location loc, string msg)
{
ReportError(new InternalError(loc.ReportingLocation, msg));
}
///
/// Signal an error in the program model.
///
/// The error message.
public void ModelError(string msg)
{
ReportError(new InternalError(msg));
}
///
/// Tries the supplied action , and logs an uncaught
/// exception error if the action fails.
///
/// Optional syntax node for error reporting.
/// Optional symbol for error reporting.
/// The action to perform.
public void Try(SyntaxNode? node, ISymbol? symbol, Action a)
{
try
{
a();
}
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
Message message;
if (node is not null)
{
message = Message.Create(this, ex.Message, node, ex.StackTrace);
}
else if (symbol is not null)
{
message = Message.Create(this, ex.Message, symbol, ex.StackTrace);
}
else if (ex is InternalError ie)
{
message = new Message(ie.Text, ie.EntityText, CreateLocation(ie.Location), ex.StackTrace);
}
else
{
message = new Message($"Uncaught exception. {ex.Message}", null, CreateLocation(), ex.StackTrace);
}
ExtractionError(message);
}
}
public virtual Entities.Location CreateLocation() =>
GeneratedLocation.Create(this);
public virtual Entities.Location CreateLocation(Microsoft.CodeAnalysis.Location? location) =>
CreateLocation();
}
}