Skip to content

Remove AllowUnsafeBlocks, MemoryMarshal and Unsafe #7324

@martincostello

Description

@martincostello

Context: #7307 (comment)

<AllowUnsafeBlocks Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == '$(NetFrameworkMinimumSupportedVersion)'">true</AllowUnsafeBlocks>

=> ref (span.Length != 0) ? ref Unsafe.AsRef(in MemoryMarshal.GetReference(span)) : ref Unsafe.AsRef<T>((void*)1);

The OTLP exporter uses unsafe for .NET Standard and .NET Framework for performance reasons.

We also use MemoryMarshal and Unsafe in two places in OpenTelemetry for netX.0 targets:

ref var ours = ref MemoryMarshal.GetArrayDataReference(ourKvps);
ref var theirs = ref MemoryMarshal.GetArrayDataReference(theirKvps);

ours = ref Unsafe.Add(ref ours, 1);
theirs = ref Unsafe.Add(ref theirs, 1);

C# 15 16 in the .NET 11 12 timeframe is looking to make changes to unsafe code to reduce its usage for security reasons, see Unsafe Evolution for further context.

To improve the security posture of the OTLP exporter, we should remove the remaining usages of unsafe code even in the face of potential performance degradation.

A similar attempt was made previously in the core library for the same reason: #6594

This may take a while to achieve as if the degradation is large, we may have to make other changes elsewhere to compensate.

The MemoryMarshal and Unsafe usages may get surfaced in later previews of .NET 11 in #6899.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions