Skip to content

Commit 0d9393d

Browse files
[APM] Made native definition lists temporary (#6417)
## Summary of changes Move native definitions from a permanent list to a temporary one that gets created and freed on demand ## Reason for change Some crashes were reported in shutdown and it points to definition lists cleanup ## Implementation details Instead of storing the lists as static fields, they get created in the call to GetCallSites and GetCallTargets and destroyed right after they've been read by the profiler ## Test coverage ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. -->
1 parent 0e50622 commit 0d9393d

File tree

13 files changed

+1018
-1038
lines changed

13 files changed

+1018
-1038
lines changed

tracer/build/_build/CodeGenerators/CallSitesGenerator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,13 @@ internal static void GenerateFile(Dictionary<string, AspectClass> aspectClasses,
258258
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
259259
// </copyright>
260260
// <auto-generated/>
261-
#pragma once
262261
#include "generated_definitions.h"
263262
264263
namespace trace
265264
{
266-
267-
std::vector<WCHAR*> g_callSites=
265+
int GeneratedDefinitions::InitCallSites(UINT32 enabledCategories, UINT32 platform)
266+
{
267+
std::vector<WCHAR*> callSites =
268268
{
269269
""");
270270

@@ -298,12 +298,14 @@ namespace trace
298298

299299
sb.AppendLine("""
300300
};
301+
return trace::profiler->RegisterIastAspects((WCHAR**) callSites.data(), callSites.size(), enabledCategories, platform);
302+
}
301303
}
302304
""");
303305

304306

305307
if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); }
306-
var fileName = outputPath / "generated_callsites.g.h";
308+
var fileName = outputPath / "generated_callsites.g.cpp";
307309
File.WriteAllText(fileName, sb.ToString());
308310

309311
Logger.Information("CallSite definitions File saved: {File}", fileName);

tracer/build/_build/CodeGenerators/CallTargetsGenerator.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,12 @@ internal static void GenerateNativeFile(Dictionary<CallTargetDefinitionSource, T
510510
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
511511
// </copyright>
512512
// <auto-generated/>
513-
#pragma once
514513
#include "generated_definitions.h"
515514
516515
namespace trace
517516
{
517+
int GeneratedDefinitions::InitCallTargets(UINT32 enabledCategories, UINT32 platform)
518+
{
518519
""");
519520

520521
var assemblyName = $"Datadog.Trace, Version={version}.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb";
@@ -548,7 +549,9 @@ namespace trace
548549
//Write all CallTargets
549550
bool inWin32Section = false;
550551
sb.AppendLine();
551-
sb.AppendLine("std::vector<CallTargetDefinition3> g_callTargets=");
552+
sb.AppendLine("""
553+
std::vector<CallTargetDefinition3> callTargets =
554+
""");
552555
sb.AppendLine("{");
553556
int x = 0;
554557
foreach (var definition in definitions
@@ -580,12 +583,14 @@ namespace trace
580583

581584
sb.AppendLine("""
582585
};
586+
return profiler->RegisterCallTargetDefinitions((WCHAR*) WStr("Tracing"), callTargets.data(), callTargets.size(), enabledCategories, platform);
587+
}
583588
}
584589
""");
585590

586591

587592
if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); }
588-
var fileName = outputPath / "generated_calltargets.g.h";
593+
var fileName = outputPath / "generated_calltargets.g.cpp";
589594
File.WriteAllText(fileName, sb.ToString());
590595

591596
Logger.Information("CallTarget definitions File saved: {File}", fileName);
@@ -603,7 +608,7 @@ static string GetSignature(CallTargetDefinitionSource definition)
603608

604609
static string GetSignatureName(int index)
605610
{
606-
return $"g_callTargets_Sig_{index:000}";
611+
return $"sig{index:000}";
607612
}
608613

609614
static string GetSignatureField(string signatureName, string signature)

tracer/src/Datadog.Tracer.Native/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ add_library("Datadog.Tracer.Native.static" STATIC
208208
tracer_integration_definition.cpp
209209
tracer_method_rewriter.cpp
210210
tracer_rejit_preprocessor.cpp
211-
Generated/generated_definitions.cpp
211+
Generated/generated_callsites.g.cpp
212+
Generated/generated_calltargets.g.cpp
212213
)
213214

214215
set_target_properties("Datadog.Tracer.Native.static" PROPERTIES PREFIX "")

tracer/src/Datadog.Tracer.Native/Datadog.Tracer.Native.vcxproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@
283283
<ClInclude Include="fault_tolerant_rewriter.h" />
284284
<ClInclude Include="fault_tolerant_tracker.h" />
285285
<ClInclude Include="function_control_wrapper.h" />
286-
<ClInclude Include="Generated\generated_callsites.g.h" />
287-
<ClInclude Include="Generated\generated_calltargets.g.h" />
286+
<ClCompile Include="Generated\generated_callsites.g.cpp" />
287+
<ClCompile Include="Generated\generated_calltargets.g.cpp" />
288288
<ClInclude Include="Generated\generated_definitions.h" />
289289
<ClInclude Include="iast\hardcoded_secrets_method_analyzer.h" />
290290
<ClInclude Include="iast\app_domain_info.h" />
@@ -369,7 +369,6 @@
369369
<ClCompile Include="fault_tolerant_rewriter.cpp" />
370370
<ClCompile Include="fault_tolerant_tracker.cpp" />
371371
<ClCompile Include="function_control_wrapper.cpp" />
372-
<ClCompile Include="Generated\generated_definitions.cpp" />
373372
<ClCompile Include="iast\hardcoded_secrets_method_analyzer.cpp" />
374373
<ClCompile Include="iast\aspect.cpp" />
375374
<ClCompile Include="iast\aspect_filter.cpp" />

tracer/src/Datadog.Tracer.Native/Datadog.Tracer.Native.vcxproj.filters

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@
116116
<ClCompile Include="iast\hardcoded_secrets_method_analyzer.cpp">
117117
<Filter>Iast</Filter>
118118
</ClCompile>
119-
<ClCompile Include="Generated\generated_definitions.cpp" />
120119
<ClCompile Include="signature_builder.cpp" />
120+
<ClCompile Include="Generated\generated_callsites.g.cpp" />
121+
<ClCompile Include="Generated\generated_calltargets.g.cpp" />
121122
</ItemGroup>
122123
<ItemGroup>
123124
<ClInclude Include="calltarget_tokens.h" />
@@ -269,8 +270,6 @@
269270
</ClInclude>
270271
<ClInclude Include="..\..\..\shared\src\native-src\version.h" />
271272
<ClInclude Include="Generated\generated_definitions.h" />
272-
<ClInclude Include="Generated\generated_callsites.g.h" />
273-
<ClInclude Include="Generated\generated_calltargets.g.h" />
274273
<ClInclude Include="signature_builder.h" />
275274
</ItemGroup>
276275
<ItemGroup>

tracer/src/Datadog.Tracer.Native/Generated/generated_callsites.g.h renamed to tracer/src/Datadog.Tracer.Native/Generated/generated_callsites.g.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
55
// <auto-generated/>
6-
#pragma once
76
#include "generated_definitions.h"
87

98
namespace trace
109
{
11-
12-
std::vector<WCHAR*> g_callSites=
10+
int GeneratedDefinitions::InitCallSites(UINT32 enabledCategories, UINT32 platform)
11+
{
12+
std::vector<WCHAR*> callSites =
1313
{
1414
(WCHAR*)WStr("[AspectClass(\"EntityFramework\",[None],Sink,[SqlInjection])] Datadog.Trace.Iast.Aspects.EntityCommandAspect 12"),
1515
(WCHAR*)WStr(" [AspectMethodInsertBefore(\"System.Data.Entity.Core.EntityClient.EntityCommand::ExecuteReader(System.Data.CommandBehavior)\",\"\",[1],[False],[None],Default,[])] ReviewSqlCommand(System.Object) 15"),
@@ -707,4 +707,6 @@ std::vector<WCHAR*> g_callSites=
707707
(WCHAR*)WStr(" [AspectMethodInsertBefore(\"System.Xml.XPath.Extensions::XPathSelectElements(System.Xml.Linq.XNode,System.String)\",\"\",[0],[False],[None],Default,[])] ReviewPath(System.String) 15"),
708708
(WCHAR*)WStr(" [AspectMethodInsertBefore(\"System.Xml.XPath.Extensions::XPathSelectElements(System.Xml.Linq.XNode,System.String,System.Xml.IXmlNamespaceResolver)\",\"\",[1],[False],[None],Default,[])] ReviewPath(System.String) 15"),
709709
};
710+
return trace::profiler->RegisterIastAspects((WCHAR**) callSites.data(), callSites.size(), enabledCategories, platform);
711+
}
710712
}

0 commit comments

Comments
 (0)