@@ -75,6 +75,7 @@ private unsafe byte[] GenerateMetadata(
75
75
// level : 4 bytes
76
76
// parameterCount : 4 bytes
77
77
uint metadataLength = 24 + ( ( uint ) eventName . Length + 1 ) * 2 ;
78
+ uint defaultMetadataLength = metadataLength ;
78
79
79
80
// Check for an empty payload.
80
81
// Write<T> calls with no arguments by convention have a parameter of
@@ -87,7 +88,16 @@ private unsafe byte[] GenerateMetadata(
87
88
// Increase the metadataLength for parameters.
88
89
foreach ( var parameter in parameters )
89
90
{
90
- metadataLength = metadataLength + parameter . GetMetadataLength ( ) ;
91
+ int pMetadataLength = parameter . GetMetadataLength ( ) ;
92
+ // The call above may return -1 which means we failed to get the metadata length.
93
+ // We then return a default metadata blob (with parameterCount of 0) to prevent it from generating malformed metadata.
94
+ if ( pMetadataLength < 0 )
95
+ {
96
+ parameters = Array . Empty < EventParameterInfo > ( ) ;
97
+ metadataLength = defaultMetadataLength ;
98
+ break ;
99
+ }
100
+ metadataLength += ( uint ) pMetadataLength ;
91
101
}
92
102
93
103
metadata = new byte [ metadataLength ] ;
@@ -107,7 +117,11 @@ private unsafe byte[] GenerateMetadata(
107
117
WriteToBuffer ( pMetadata , metadataLength , ref offset , ( uint ) parameters . Length ) ;
108
118
foreach ( var parameter in parameters )
109
119
{
110
- parameter . GenerateMetadata ( pMetadata , ref offset , metadataLength ) ;
120
+ if ( ! parameter . GenerateMetadata ( pMetadata , ref offset , metadataLength ) )
121
+ {
122
+ // If we fail to generate metadata for any parameter, we should return the "default" metadata without any parameters
123
+ return GenerateMetadata ( eventId , eventName , keywords , level , version , Array . Empty < EventParameterInfo > ( ) ) ;
124
+ }
111
125
}
112
126
Debug . Assert ( metadataLength == offset ) ;
113
127
}
@@ -174,7 +188,7 @@ internal void SetInfo(string name, Type type, TraceLoggingTypeInfo typeInfo = nu
174
188
TypeInfo = typeInfo ;
175
189
}
176
190
177
- internal unsafe void GenerateMetadata ( byte * pMetadataBlob , ref uint offset , uint blobSize )
191
+ internal unsafe bool GenerateMetadata ( byte * pMetadataBlob , ref uint offset , uint blobSize )
178
192
{
179
193
TypeCode typeCode = GetTypeCodeExtended ( ParameterType ) ;
180
194
if ( typeCode == TypeCode . Object )
@@ -189,7 +203,7 @@ internal unsafe void GenerateMetadata(byte* pMetadataBlob, ref uint offset, uint
189
203
InvokeTypeInfo invokeTypeInfo = TypeInfo as InvokeTypeInfo ;
190
204
if ( invokeTypeInfo == null )
191
205
{
192
- throw new NotSupportedException ( ) ;
206
+ return false ;
193
207
}
194
208
195
209
// Get the set of properties to be serialized.
@@ -201,7 +215,10 @@ internal unsafe void GenerateMetadata(byte* pMetadataBlob, ref uint offset, uint
201
215
202
216
foreach ( PropertyAnalysis prop in properties )
203
217
{
204
- GenerateMetadataForProperty ( prop , pMetadataBlob , ref offset , blobSize ) ;
218
+ if ( ! GenerateMetadataForProperty ( prop , pMetadataBlob , ref offset , blobSize ) )
219
+ {
220
+ return false ;
221
+ }
205
222
}
206
223
}
207
224
else
@@ -225,9 +242,10 @@ internal unsafe void GenerateMetadata(byte* pMetadataBlob, ref uint offset, uint
225
242
EventPipeMetadataGenerator . WriteToBuffer ( pMetadataBlob , blobSize , ref offset , ( byte * ) pParameterName , ( ( uint ) ParameterName . Length + 1 ) * 2 ) ;
226
243
}
227
244
}
245
+ return true ;
228
246
}
229
247
230
- private static unsafe void GenerateMetadataForProperty ( PropertyAnalysis property , byte * pMetadataBlob , ref uint offset , uint blobSize )
248
+ private static unsafe bool GenerateMetadataForProperty ( PropertyAnalysis property , byte * pMetadataBlob , ref uint offset , uint blobSize )
231
249
{
232
250
Debug . Assert ( property != null ) ;
233
251
Debug . Assert ( pMetadataBlob != null ) ;
@@ -252,7 +270,10 @@ private static unsafe void GenerateMetadataForProperty(PropertyAnalysis property
252
270
253
271
foreach ( PropertyAnalysis prop in properties )
254
272
{
255
- GenerateMetadataForProperty ( prop , pMetadataBlob , ref offset , blobSize ) ;
273
+ if ( ! GenerateMetadataForProperty ( prop , pMetadataBlob , ref offset , blobSize ) )
274
+ {
275
+ return false ;
276
+ }
256
277
}
257
278
}
258
279
else
@@ -274,10 +295,10 @@ private static unsafe void GenerateMetadataForProperty(PropertyAnalysis property
274
295
// PropertyName : NULL-terminated string
275
296
TypeCode typeCode = GetTypeCodeExtended ( property . typeInfo . DataType ) ;
276
297
277
- // EventPipe does not support this type. Throw , which will cause no metadata to be registered for this event.
298
+ // EventPipe does not support this type. Return false , which will cause no metadata to be registered for this event.
278
299
if ( typeCode == TypeCode . Object )
279
300
{
280
- throw new NotSupportedException ( ) ;
301
+ return false ;
281
302
}
282
303
283
304
// Write the type code.
@@ -289,20 +310,21 @@ private static unsafe void GenerateMetadataForProperty(PropertyAnalysis property
289
310
EventPipeMetadataGenerator . WriteToBuffer ( pMetadataBlob , blobSize , ref offset , ( byte * ) pPropertyName , ( ( uint ) property . name . Length + 1 ) * 2 ) ;
290
311
}
291
312
}
313
+ return true ;
292
314
}
293
315
294
316
295
- internal unsafe uint GetMetadataLength ( )
317
+ internal unsafe int GetMetadataLength ( )
296
318
{
297
- uint ret = 0 ;
319
+ int ret = 0 ;
298
320
299
321
TypeCode typeCode = GetTypeCodeExtended ( ParameterType ) ;
300
322
if ( typeCode == TypeCode . Object )
301
323
{
302
324
InvokeTypeInfo typeInfo = TypeInfo as InvokeTypeInfo ;
303
325
if ( typeInfo == null )
304
326
{
305
- throw new NotSupportedException ( ) ;
327
+ return - 1 ;
306
328
}
307
329
308
330
// Each nested struct is serialized as:
@@ -319,7 +341,7 @@ internal unsafe uint GetMetadataLength()
319
341
{
320
342
foreach ( PropertyAnalysis prop in properties )
321
343
{
322
- ret += GetMetadataLengthForProperty ( prop ) ;
344
+ ret += ( int ) GetMetadataLengthForProperty ( prop ) ;
323
345
}
324
346
}
325
347
@@ -330,7 +352,7 @@ internal unsafe uint GetMetadataLength()
330
352
}
331
353
else
332
354
{
333
- ret += ( uint ) ( sizeof ( uint ) + ( ( ParameterName . Length + 1 ) * 2 ) ) ;
355
+ ret += ( int ) ( sizeof ( uint ) + ( ( ParameterName . Length + 1 ) * 2 ) ) ;
334
356
}
335
357
336
358
return ret ;
0 commit comments