From e2713fc5575b170cb881b38079a6773c9fe30912 Mon Sep 17 00:00:00 2001 From: Daniel Romano <108014683+daniel-romano-DD@users.noreply.github.com> Date: Fri, 12 Jan 2024 11:13:51 +0100 Subject: [PATCH] Removed Activator calls for performance reasons --- .../AspectsDefinitionsGenerator.cs | 2 +- .../AspectsDefinitions/Helpers.cs | 42 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/tracer/src/Datadog.Trace.SourceGenerators/AspectsDefinitions/AspectsDefinitionsGenerator.cs b/tracer/src/Datadog.Trace.SourceGenerators/AspectsDefinitions/AspectsDefinitionsGenerator.cs index 2ca8ffbdfea1..c565e0926446 100644 --- a/tracer/src/Datadog.Trace.SourceGenerators/AspectsDefinitions/AspectsDefinitionsGenerator.cs +++ b/tracer/src/Datadog.Trace.SourceGenerators/AspectsDefinitions/AspectsDefinitionsGenerator.cs @@ -158,7 +158,7 @@ private static IReadOnlyList GetAspectSources(Compilation compilation, I var arguments = attribute.ConstructorArguments.Select(a => GetArgument(a)).ToArray(); var type = Resolve(attribute); if (type == null) { return null; } - var res = Activator.CreateInstance(type, arguments); + var res = Helpers.CreateAttributeInstance(type, arguments); // Activator.CreateInstance(type, arguments); return res; } catch (Exception err) diff --git a/tracer/src/Datadog.Trace.SourceGenerators/AspectsDefinitions/Helpers.cs b/tracer/src/Datadog.Trace.SourceGenerators/AspectsDefinitions/Helpers.cs index 35e3afb73842..0fa21a2e84f8 100644 --- a/tracer/src/Datadog.Trace.SourceGenerators/AspectsDefinitions/Helpers.cs +++ b/tracer/src/Datadog.Trace.SourceGenerators/AspectsDefinitions/Helpers.cs @@ -7,7 +7,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; +using Datadog.Trace.Iast; +using Datadog.Trace.Iast.Dataflow; namespace Datadog.Trace.SourceGenerators.AspectsDefinitions { @@ -23,5 +24,44 @@ internal static class Helpers Array.Copy(array, destinationArray, array.Length); return destinationArray; } + + internal static object? CreateAttributeInstance(Type attributeType, object?[] arguments) + { + int x = 0; + if (attributeType == typeof(AspectClassAttribute)) + { + if (arguments == null || arguments.Length == 0) { return new AspectClassAttribute(); } + else if (arguments.Length == 1) { return new AspectClassAttribute((string)arguments[x++]!); } + else if (arguments.Length == 3) { return new AspectClassAttribute((string)arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); } + else if (arguments.Length == 4 && arguments[1]?.GetType() == typeof(AspectFilter)) { return new AspectClassAttribute((string)arguments[x++]!, (AspectFilter)arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); } + else if (arguments.Length == 4) { return new AspectClassAttribute((string)arguments[x++]!, (AspectFilter[])arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); } + } + else if (attributeType == typeof(AspectMethodReplaceAttribute)) + { + if (arguments.Length == 1) { return new AspectMethodReplaceAttribute((string)arguments[x++]!); } + else if (arguments.Length == 2) { return new AspectMethodReplaceAttribute((string)arguments[x++]!, (AspectFilter[])arguments[x++]!); } + else if (arguments.Length == 3 && arguments[1]?.GetType() == typeof(string)) { return new AspectMethodReplaceAttribute((string)arguments[x++]!, (string)arguments[x++]!, (AspectFilter[])arguments[x++]!); } + else if (arguments.Length == 3) { return new AspectMethodReplaceAttribute((string)arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); } + } + else if (attributeType == typeof(AspectCtorReplaceAttribute)) + { + if (arguments.Length == 1) { return new AspectCtorReplaceAttribute((string)arguments[x++]!); } + else if (arguments.Length == 2) { return new AspectCtorReplaceAttribute((string)arguments[x++]!, (AspectFilter[])arguments[x++]!); } + else if (arguments.Length == 3) { return new AspectCtorReplaceAttribute((string)arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); } + } + else if (attributeType == typeof(AspectMethodInsertAfterAttribute)) + { + if (arguments.Length == 1) { return new AspectMethodInsertAfterAttribute((string)arguments[x++]!); } + else if (arguments.Length == 3) { return new AspectMethodInsertAfterAttribute((string)arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); } + } + else if (attributeType == typeof(AspectMethodInsertBeforeAttribute)) + { + if (arguments.Length == 2) { return new AspectMethodInsertBeforeAttribute((string)arguments[x++]!, (int[])arguments[x++]!); } + else if (arguments.Length == 3 && arguments[1]?.GetType() == typeof(int)) { return new AspectMethodInsertBeforeAttribute((string)arguments[x++]!, (int)arguments[x++]!, (bool)arguments[x++]!); } + else if (arguments.Length == 3) { return new AspectMethodInsertBeforeAttribute((string)arguments[x++]!, (int[])arguments[x++]!, (bool[])arguments[x++]!); } + } + + throw new ArgumentException($"No constructor helper for {attributeType} and {arguments.Length} arguments", "attributeType"); + } } }