Skip to content

Commit 28b18e4

Browse files
authored
Fix BadImageFormatException that occurred when trace annotations were used in conjunction with DD_TRACE_DEBUG (#4045)
* Fix memory corruption that happened due to a buffer that went out of scope prematurely * Enable DD_TRACE_DEBUG in Trace Annotations integration tests
1 parent 2666037 commit 28b18e4

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

tracer/src/Datadog.Tracer.Native/method_rewriter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ HRESULT TracerMethodRewriter::Rewrite(RejitHandlerModule* moduleHandler, RejitHa
111111
bool isStatic = !(caller->method_signature.CallingConvention() & IMAGE_CEE_CS_CALLCONV_HASTHIS);
112112
std::vector<trace::TypeSignature> methodArguments = caller->method_signature.GetMethodArguments();
113113
std::vector<trace::TypeSignature> traceAnnotationArguments;
114+
115+
// DO NOT move the definition of these buffers into an inner scope. It will cause memory corruption since they are referenced in a TypeSignature that is used later in this function.
116+
COR_SIGNATURE runtimeMethodHandleBuffer[10];
117+
COR_SIGNATURE runtimeTypeHandleBuffer[10];
118+
114119
int numArgs = caller->method_signature.NumberOfArguments();
115120
auto metaEmit = module_metadata.metadata_emit;
116121
auto metaImport = module_metadata.metadata_import;
@@ -307,9 +312,6 @@ HRESULT TracerMethodRewriter::Rewrite(RejitHandlerModule* moduleHandler, RejitHa
307312
}
308313
else
309314
{
310-
COR_SIGNATURE runtimeMethodHandleBuffer[10];
311-
COR_SIGNATURE runtimeTypeHandleBuffer[10];
312-
313315
// Load the methodDef token to produce a RuntimeMethodHandle on the stack
314316
reWriterWrapper.LoadToken(caller->id);
315317

tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/TraceAnnotationsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public async Task SubmitTraces()
8686
ddTraceMethodsString += ";Samples.TraceAnnotations.ExtensionMethods[ExtensionMethodForTestType,ExtensionMethodForTestTypeGeneric,ExtensionMethodForTestTypeTypeStruct];System.Net.Http.HttpRequestMessage[set_Method]";
8787

8888
SetEnvironmentVariable("DD_TRACE_METHODS", ddTraceMethodsString);
89-
89+
SetEnvironmentVariable("DD_TRACE_DEBUG", "1");
9090
// Don't bother with telemetry when two assemblies are loaded because we could get unreliable results
9191
MockTelemetryAgent<TelemetryData> telemetry = _twoAssembliesLoaded ? null : this.ConfigureTelemetry();
9292
using (var agent = EnvironmentHelper.GetMockAgent())

0 commit comments

Comments
 (0)