Skip to content

Commit

Permalink
Merge pull request #911 from Microsoft/v2-to-v1-stack-stackFrame
Browse files Browse the repository at this point in the history
Transform v2 -> v1 stack, stackFrame, region
  • Loading branch information
easyrhino-gh authored Jun 1, 2018
2 parents d7ca04a + 0fc5b3c commit 3c58e00
Show file tree
Hide file tree
Showing 2 changed files with 322 additions and 1 deletion.
228 changes: 228 additions & 0 deletions src/Sarif.UnitTests/SarifCurrentToVersionOneTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,5 +667,233 @@ public void SarifTransformerTests_ToVersionOne_OneRunWithInvocationAndNotificati

VerifyCurrentToVersionOneTransformation(V2LogText, V1LogExpectedText);
}

[Fact]
public void SarifTransformerTests_ToVersionOne_NotificationExceptionWithStack()
{
const string V2LogText =
@"{
""$schema"": ""http://json.schemastore.org/sarif-2.0.0"",
""version"": ""2.0.0"",
""runs"": [
{
""tool"": {
""name"": ""CodeScanner"",
""semanticVersion"": ""2.1.0""
},
""invocations"": [
{
""toolNotifications"": [
{
""id"": ""CTN0001"",
""message"": {
""text"": ""Unhandled exception.""
},
""level"": ""error"",
""exception"": {
""kind"": ""ExecutionEngine.RuleFailureException"",
""message"": ""Unhandled exception during rule evaluation."",
""stack"": {
""message"": {
""text"": ""This is the stack messasge.""
},
""frames"": [
{
""location"": {
""physicalLocation"": {
""fileLocation"": {
""uri"": ""file:///C:/src/main.cs""
},
""region"": {
""startLine"": 15,
""startColumn"": 9
}
},
""fullyQualifiedLogicalName"": ""Rules.SecureHashAlgorithmRule.Evaluate"",
""message"": {
""text"": ""Exception thrown""
}
},
""module"": ""RuleLibrary"",
""threadId"": 52,
""address"": 10092852
},
{
""location"": {
""physicalLocation"": {
""fileLocation"": {
""uri"": ""file:///C:/src/main.cs""
}
},
""fullyQualifiedLogicalName"": ""Rules.SecureHashAlgorithmRule.Register-0""
},
""module"": ""RuleLibrary"",
""threadId"": 52,
""address"": 1002485
},
{
""location"": {
""physicalLocation"": {
""fileLocation"": {
""uri"": ""file:///C:/src/utils.cs""
}
},
""fullyQualifiedLogicalName"": ""ExecutionEngine.Engine.EvaluateRule""
},
""module"": ""ExecutionEngine"",
""threadId"": 52,
""address"": 10073356,
""offset"": 10475
},
{
""location"": {
""physicalLocation"": {
""fileLocation"": {
""uri"": ""file:///C:/src/foobar.cs""
}
},
""fullyQualifiedLogicalName"": ""ExecutionEngine.Engine.FooBar""
},
""module"": ""ExecutionEngine"",
""threadId"": 52,
""address"": 10073356,
""offset"": 10475
}
]
},
""innerExceptions"": [
{
""kind"": ""System.ArgumentException"",
""message"": ""length is < 0""
}
]
}
}
]
}
],
""logicalLocations"": {
""Rules.SecureHashAlgorithmRule.Evaluate"": {
""name"": ""Evaluate"",
""kind"": ""some kind""
},
""Rules.SecureHashAlgorithmRule.Register"": {
""name"": ""InvalidName""
},
""ExecutionEngine.Engine.FooBar"": {
""name"": ""EvaluateRule"",
""fullyQualifiedName"": ""ExecutionEngine.Engine.EvaluateRule"",
""kind"": ""another kind""
},
""Rules.SecureHashAlgorithmRule.Register-0"": {
""name"": ""Register"",
""fullyQualifiedName"": ""Rules.SecureHashAlgorithmRule.Register""
},
""ExecutionEngine.Engine.EvaluateRule"": {
""name"": ""EvaluateRule""
}
},
""results"": []
}
]
}";

const string V1LogExpectedText =
@"{
""$schema"": ""http://json.schemastore.org/sarif-1.0.0"",
""version"": ""1.0.0"",
""runs"": [
{
""tool"": {
""name"": ""CodeScanner"",
""semanticVersion"": ""2.1.0""
},
""invocation"": {},
""logicalLocations"": {
""Rules.SecureHashAlgorithmRule.Evaluate"": {
""name"": ""Evaluate"",
""kind"": ""some kind""
},
""Rules.SecureHashAlgorithmRule.Register"": {
""name"": ""InvalidName""
},
""ExecutionEngine.Engine.FooBar"": {
""name"": ""EvaluateRule"",
""kind"": ""another kind""
},
""Rules.SecureHashAlgorithmRule.Register-0"": {
""name"": ""Register""
},
""ExecutionEngine.Engine.EvaluateRule"": {
""name"": ""EvaluateRule""
}
},
""results"": [],
""toolNotifications"": [
{
""id"": ""CTN0001"",
""message"": ""Unhandled exception."",
""level"": ""error"",
""exception"": {
""kind"": ""ExecutionEngine.RuleFailureException"",
""message"": ""Unhandled exception during rule evaluation."",
""stack"": {
""message"": ""This is the stack messasge."",
""frames"": [
{
""message"": ""Exception thrown"",
""uri"": ""file:///C:/src/main.cs"",
""line"": 15,
""column"": 9,
""module"": ""RuleLibrary"",
""threadId"": 52,
""fullyQualifiedLogicalName"": ""Rules.SecureHashAlgorithmRule.Evaluate"",
""address"": 10092852
},
{
""uri"": ""file:///C:/src/main.cs"",
""module"": ""RuleLibrary"",
""threadId"": 52,
""fullyQualifiedLogicalName"": ""Rules.SecureHashAlgorithmRule.Register"",
""logicalLocationKey"": ""Rules.SecureHashAlgorithmRule.Register-0"",
""address"": 1002485
},
{
""uri"": ""file:///C:/src/utils.cs"",
""module"": ""ExecutionEngine"",
""threadId"": 52,
""fullyQualifiedLogicalName"": ""ExecutionEngine.Engine.EvaluateRule"",
""address"": 10073356,
""offset"": 10475
},
{
""uri"": ""file:///C:/src/foobar.cs"",
""module"": ""ExecutionEngine"",
""threadId"": 52,
""fullyQualifiedLogicalName"": ""ExecutionEngine.Engine.EvaluateRule"",
""logicalLocationKey"": ""ExecutionEngine.Engine.FooBar"",
""address"": 10073356,
""offset"": 10475
}
]
},
""innerExceptions"": [
{
""kind"": ""System.ArgumentException"",
""message"": ""length is < 0""
}
]
}
}
],
""properties"": {
""sarifv2/run"": {""tool"":{""name"":""CodeScanner"",""semanticVersion"":""2.1.0""},""invocations"":[{""toolNotifications"":[{""id"":""CTN0001"",""message"":{""text"":""Unhandled exception.""},""level"":""error"",""exception"":{""kind"":""ExecutionEngine.RuleFailureException"",""message"":""Unhandled exception during rule evaluation."",""stack"":{""message"":{""text"":""This is the stack messasge.""},""frames"":[{""location"":{""physicalLocation"":{""fileLocation"":{""uri"":""file:///C:/src/main.cs""},""region"":{""startLine"":15,""startColumn"":9}},""fullyQualifiedLogicalName"":""Rules.SecureHashAlgorithmRule.Evaluate"",""message"":{""text"":""Exception thrown""}},""module"":""RuleLibrary"",""threadId"":52,""address"":10092852},{""location"":{""physicalLocation"":{""fileLocation"":{""uri"":""file:///C:/src/main.cs""}},""fullyQualifiedLogicalName"":""Rules.SecureHashAlgorithmRule.Register-0""},""module"":""RuleLibrary"",""threadId"":52,""address"":1002485},{""location"":{""physicalLocation"":{""fileLocation"":{""uri"":""file:///C:/src/utils.cs""}},""fullyQualifiedLogicalName"":""ExecutionEngine.Engine.EvaluateRule""},""module"":""ExecutionEngine"",""threadId"":52,""address"":10073356,""offset"":10475},{""location"":{""physicalLocation"":{""fileLocation"":{""uri"":""file:///C:/src/foobar.cs""}},""fullyQualifiedLogicalName"":""ExecutionEngine.Engine.FooBar""},""module"":""ExecutionEngine"",""threadId"":52,""address"":10073356,""offset"":10475}]},""innerExceptions"":[{""kind"":""System.ArgumentException"",""message"":""length is < 0""}]}}]}],""logicalLocations"":{""Rules.SecureHashAlgorithmRule.Evaluate"":{""name"":""Evaluate"",""kind"":""some kind""},""Rules.SecureHashAlgorithmRule.Register"":{""name"":""InvalidName""},""ExecutionEngine.Engine.FooBar"":{""name"":""EvaluateRule"",""fullyQualifiedName"":""ExecutionEngine.Engine.EvaluateRule"",""kind"":""another kind""},""Rules.SecureHashAlgorithmRule.Register-0"":{""name"":""Register"",""fullyQualifiedName"":""Rules.SecureHashAlgorithmRule.Register""},""ExecutionEngine.Engine.EvaluateRule"":{""name"":""EvaluateRule""}},""results"":[]}
}
}
]
}";

VerifyCurrentToVersionOneTransformation(V2LogText, V1LogExpectedText);
}
}
}
95 changes: 94 additions & 1 deletion src/Sarif/Visitors/SarifCurrentToVersionOneVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ internal ExceptionDataVersionOne CreateExceptionData(ExceptionData v2ExceptionDa
{
InnerExceptions = v2ExceptionData.InnerExceptions?.Select(CreateExceptionData).ToList(),
Kind = v2ExceptionData.Kind,
Message = v2ExceptionData.Message
Message = v2ExceptionData.Message,
Stack = CreateStack(v2ExceptionData.Stack)
};
}

Expand Down Expand Up @@ -196,6 +197,7 @@ internal PhysicalLocationVersionOne CreatePhysicalLocation(PhysicalLocation v2Ph
{
physicalLocation = new PhysicalLocationVersionOne
{
Region = CreateRegion(v2PhysicalLocation.Region),
Uri = v2PhysicalLocation.FileLocation?.Uri,
UriBaseId = v2PhysicalLocation.FileLocation?.UriBaseId
};
Expand All @@ -204,6 +206,31 @@ internal PhysicalLocationVersionOne CreatePhysicalLocation(PhysicalLocation v2Ph
return physicalLocation;
}

internal RegionVersionOne CreateRegion(Region v2Region)
{
RegionVersionOne region = null;

if (v2Region != null && (v2Region.StartColumn > 0 ||
v2Region.StartLine > 0 ||
v2Region.EndColumn > 0 ||
v2Region.EndLine > 0 ||
v2Region.Length > 0 ||
v2Region.Offset > 0))
{
region = new RegionVersionOne
{
EndColumn = v2Region.EndColumn,
EndLine = v2Region.EndLine,
Length = v2Region.Length,
Offset = v2Region.Offset,
StartColumn = v2Region.StartColumn,
StartLine = v2Region.StartLine
};
}

return region;
}

internal Dictionary<string, string> CreateResponseFilesDictionary(IList<FileLocation> v2ResponseFilesList)
{
Dictionary<string, string> responseFiles = null;
Expand Down Expand Up @@ -299,6 +326,72 @@ internal RunVersionOne CreateRun(Run v2Run)
return run;
}

internal StackVersionOne CreateStack(Stack v2Stack)
{
StackVersionOne stack = null;

if (v2Stack != null)
{
stack = new StackVersionOne
{
Message = v2Stack.Message?.Text,
Properties = v2Stack.Properties,
Frames = v2Stack.Frames?.Select(CreateStackFrame).ToList()
};
}

return stack;
}

internal StackFrameVersionOne CreateStackFrame(StackFrame v2StackFrame)
{
StackFrameVersionOne stackFrame = null;

if (v2StackFrame != null)
{
stackFrame = new StackFrameVersionOne
{
Address = v2StackFrame.Address,
Module = v2StackFrame.Module,
Offset = v2StackFrame.Offset,
Parameters = v2StackFrame.Parameters,
Properties = v2StackFrame.Properties,
ThreadId = v2StackFrame.ThreadId
};

Location location = v2StackFrame.Location;
if (location != null)
{
string fqln = location.FullyQualifiedLogicalName;

if (_currentV2Run.LogicalLocations != null &&
_currentV2Run.LogicalLocations.ContainsKey(fqln) &&
!string.IsNullOrWhiteSpace(_currentV2Run.LogicalLocations[fqln].FullyQualifiedName))
{
stackFrame.FullyQualifiedLogicalName = _currentV2Run.LogicalLocations[fqln].FullyQualifiedName;
stackFrame.LogicalLocationKey = fqln;
}
else
{
stackFrame.FullyQualifiedLogicalName = fqln;
}

stackFrame.Message = location.Message?.Text;

PhysicalLocation physicalLocation = location.PhysicalLocation;
if (physicalLocation != null)
{
stackFrame.Column = physicalLocation.Region?.StartColumn ?? 0;
stackFrame.Line = physicalLocation.Region?.StartLine ?? 0;
stackFrame.Uri = physicalLocation.FileLocation?.Uri;
stackFrame.UriBaseId = physicalLocation.FileLocation?.UriBaseId;
}
}
}

return stackFrame;
}

internal ToolVersionOne CreateTool(Tool v2Tool)
{
ToolVersionOne tool = null;
Expand Down

0 comments on commit 3c58e00

Please sign in to comment.