Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 0 additions & 117 deletions .github/CODEOWNERS

This file was deleted.

2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@

<!-- Packaging -->
<PropertyGroup>
<GitHubRepositoryName>runtime</GitHubRepositoryName>
<GitHubRepositoryName>runtimelab</GitHubRepositoryName>
<RepositoryUrl>https://github.com/dotnet/$(GitHubRepositoryName)</RepositoryUrl>
<PackageProjectUrl>https://dot.net</PackageProjectUrl>
<Owners>microsoft,dotnetframework</Owners>
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageVersionNet8>8.0.$([MSBuild]::Add($([System.Version]::Parse('$(PackageVersionNet9)').Build),11))</PackageVersionNet8>
<PackageVersionNet7>7.0.20</PackageVersionNet7>
<PackageVersionNet6>6.0.36</PackageVersionNet6>
<PreReleaseVersionLabel>rc</PreReleaseVersionLabel>
<PreReleaseVersionLabel>unsafe</PreReleaseVersionLabel>
<PreReleaseVersionIteration>1</PreReleaseVersionIteration>
<!-- Enable to remove prerelease label. -->
<StabilizePackageVersion Condition="'$(StabilizePackageVersion)' == ''">false</StabilizePackageVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<FeaturePortableThreadPool Condition="'$(TargetsBrowser)' != 'true'">true</FeaturePortableThreadPool>
<FeaturePortableTimer Condition="'$(TargetsBrowser)' != 'true'">true</FeaturePortableTimer>
<FeatureSingleThread Condition="'$(TargetsBrowser)' == 'true'">true</FeatureSingleThread>
<EnableUnsafeAnalyzer>true</EnableUnsafeAnalyzer>
</PropertyGroup>

<ItemGroup>
Expand Down
21 changes: 14 additions & 7 deletions src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ internal IEnumerator<T> GetEnumerator<T>()
{
// ! Warning: "this" is an array, not an SZArrayHelper. See comments above
// ! or you may introduce a security hole!
T[] @this = Unsafe.As<T[]>(this);
T[] @this;
unsafe { @this = Unsafe.As<T[]>(this); }
int length = @this.Length;
return length == 0 ? SZGenericArrayEnumerator<T>.Empty : new SZGenericArrayEnumerator<T>(@this, length);
}
Expand All @@ -411,23 +412,26 @@ private void CopyTo<T>(T[] array, int index)
// ! Warning: "this" is an array, not an SZArrayHelper. See comments above
// ! or you may introduce a security hole!

T[] @this = Unsafe.As<T[]>(this);
T[] @this;
unsafe { @this = Unsafe.As<T[]>(this); }
Array.Copy(@this, 0, array, index, @this.Length);
}

internal int get_Count<T>()
{
// ! Warning: "this" is an array, not an SZArrayHelper. See comments above
// ! or you may introduce a security hole!
T[] @this = Unsafe.As<T[]>(this);
T[] @this;
unsafe { @this = Unsafe.As<T[]>(this); }
return @this.Length;
}

internal T get_Item<T>(int index)
{
// ! Warning: "this" is an array, not an SZArrayHelper. See comments above
// ! or you may introduce a security hole!
T[] @this = Unsafe.As<T[]>(this);
T[] @this;
unsafe { @this = Unsafe.As<T[]>(this); }
if ((uint)index >= (uint)@this.Length)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException();
Expand All @@ -440,7 +444,8 @@ internal void set_Item<T>(int index, T value)
{
// ! Warning: "this" is an array, not an SZArrayHelper. See comments above
// ! or you may introduce a security hole!
T[] @this = Unsafe.As<T[]>(this);
T[] @this;
unsafe { @this = Unsafe.As<T[]>(this); }
if ((uint)index >= (uint)@this.Length)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException();
Expand All @@ -459,7 +464,8 @@ private bool Contains<T>(T value)
{
// ! Warning: "this" is an array, not an SZArrayHelper. See comments above
// ! or you may introduce a security hole!
T[] @this = Unsafe.As<T[]>(this);
T[] @this;
unsafe { @this = Unsafe.As<T[]>(this); }
return Array.IndexOf(@this, value, 0, @this.Length) >= 0;
}

Expand All @@ -480,7 +486,8 @@ private int IndexOf<T>(T value)
{
// ! Warning: "this" is an array, not an SZArrayHelper. See comments above
// ! or you may introduce a security hole!
T[] @this = Unsafe.As<T[]>(this);
T[] @this;
unsafe { @this = Unsafe.As<T[]>(this); }
return Array.IndexOf(@this, value, 0, @this.Length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ private bool BindToMethodInfo(object? target, IRuntimeMethodInfo method, Runtime
private static MulticastDelegate InternalAlloc(RuntimeType type)
{
Debug.Assert(type.IsAssignableTo(typeof(MulticastDelegate)));
return Unsafe.As<MulticastDelegate>(RuntimeTypeHandle.InternalAlloc(type));
MulticastDelegate result;
unsafe { result = Unsafe.As<MulticastDelegate>(RuntimeTypeHandle.InternalAlloc(type)); }
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public sealed override bool Equals([NotNullWhen(true)] object? obj)
// the types are the same, obj should also be a
// MulticastDelegate
Debug.Assert(obj is MulticastDelegate, "Shouldn't have failed here since we already checked the types are the same!");
MulticastDelegate d = Unsafe.As<MulticastDelegate>(obj);
MulticastDelegate d;
unsafe { d = Unsafe.As<MulticastDelegate>(obj); }

if (_invocationCount != 0)
{
Expand Down Expand Up @@ -168,7 +169,11 @@ private static bool TrySetSlot(object?[] a, int index, object o)
private unsafe MulticastDelegate NewMulticastDelegate(object[] invocationList, int invocationCount, bool thisIsMultiCastAlready)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove unsafe from this method since we have an unsafe block.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit c0df804. Removed unsafe modifier from the method signature.

{
// First, allocate a new multicast delegate just like this one, i.e. same type as the this object
MulticastDelegate result = Unsafe.As<MulticastDelegate>(RuntimeTypeHandle.InternalAllocNoChecks(RuntimeHelpers.GetMethodTable(this)));
MulticastDelegate result;
unsafe
{
result = Unsafe.As<MulticastDelegate>(RuntimeTypeHandle.InternalAllocNoChecks(RuntimeHelpers.GetMethodTable(this)));
}

// Performance optimization - if this already points to a true multicast delegate,
// copy _methodPtr and _methodPtrAux fields rather than calling into the EE to get them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2448,7 +2448,12 @@ private RuntimeTypeCache? CacheIfExists
{
object? cache = GCHandle.InternalGet(m_cache);
Debug.Assert(cache == null || cache is RuntimeTypeCache);
return Unsafe.As<RuntimeTypeCache>(cache);
RuntimeTypeCache? result;
unsafe
{
result = Unsafe.As<RuntimeTypeCache>(cache);
}
return result;
}
return null;
}
Expand All @@ -2465,7 +2470,12 @@ private RuntimeTypeCache Cache
if (cache != null)
{
Debug.Assert(cache is RuntimeTypeCache);
return Unsafe.As<RuntimeTypeCache>(cache);
RuntimeTypeCache result;
unsafe
{
result = Unsafe.As<RuntimeTypeCache>(cache);
}
return result;
}
}
return InitializeCache();
Expand Down
1 change: 1 addition & 0 deletions src/libraries/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Nullable Condition="'$(Nullable)' == '' and '$(IsTestProject)' == 'true'">annotations</Nullable>
<!-- AOT compatibility is enabled by default for src/ref projects. -->
<IsAotCompatible Condition="'$(IsAotCompatible)' == '' and ('$(IsSourceProject)' == 'true' or '$(IsReferenceAssemblyProject)' == 'true')">true</IsAotCompatible>
<EnableUnsafeAnalyzer>true</EnableUnsafeAnalyzer>
</PropertyGroup>

<!-- Set up common paths -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresAssemblyFilesAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresUnsafeAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\SetsRequiredMembersAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\StringSyntaxAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\SuppressMessageAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public override T[] Rent(int minimumLength)
SharedArrayPoolThreadLocalArray[]? tlsBuckets = t_tlsBuckets;
if (tlsBuckets is not null && (uint)bucketIndex < (uint)tlsBuckets.Length)
{
buffer = Unsafe.As<T[]>(tlsBuckets[bucketIndex].Array);
unsafe
{
buffer = Unsafe.As<T[]>(tlsBuckets[bucketIndex].Array);
}
if (buffer is not null)
{
tlsBuckets[bucketIndex].Array = null;
Expand All @@ -79,7 +82,10 @@ public override T[] Rent(int minimumLength)
SharedArrayPoolPartitions? b = perCoreBuckets[bucketIndex];
if (b is not null)
{
buffer = Unsafe.As<T[]>(b.TryPop());
unsafe
{
buffer = Unsafe.As<T[]>(b.TryPop());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryPop must always be a T[] so please make sure that any of the input methods to SharedArrayPoolPartitions are unsafe and have a comment about that requirement.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit c0df804. Made TryPush method unsafe with comment "Array parameter must be a T[]".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mark the TryPush method RequiresUnsafe instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit b73ffce. Changed TryPush method from unsafe to [RequiresUnsafe].

}
if (buffer is not null)
{
if (log.IsEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2321,7 +2321,9 @@ private static IAlternateEqualityComparer<TAlternateKey, TKey> GetAlternateCompa
where TAlternateKey : notnull, allows ref struct
{
Debug.Assert(IsCompatibleKey<TAlternateKey>(tables));
return Unsafe.As<IAlternateEqualityComparer<TAlternateKey, TKey>>(tables._comparer!);
IAlternateEqualityComparer<TAlternateKey, TKey> result;
unsafe { result = Unsafe.As<IAlternateEqualityComparer<TAlternateKey, TKey>>(tables._comparer!); }
return result;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,12 @@ internal static bool IsCompatibleKey(Dictionary<TKey, TValue> dictionary)
internal static IAlternateEqualityComparer<TAlternateKey, TKey> GetAlternateComparer(Dictionary<TKey, TValue> dictionary)
{
Debug.Assert(IsCompatibleKey(dictionary));
return Unsafe.As<IAlternateEqualityComparer<TAlternateKey, TKey>>(dictionary._comparer)!;
IAlternateEqualityComparer<TAlternateKey, TKey> result;
unsafe
{
result = Unsafe.As<IAlternateEqualityComparer<TAlternateKey, TKey>>(dictionary._comparer)!;
}
return result;
}

/// <summary>Gets the value associated with the specified alternate key.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ internal static bool IsCompatibleItem(HashSet<T> set)
internal static IAlternateEqualityComparer<TAlternate, T> GetAlternateComparer(HashSet<T> set)
{
Debug.Assert(IsCompatibleItem(set));
return Unsafe.As<IAlternateEqualityComparer<TAlternate, T>>(set._comparer)!;
IAlternateEqualityComparer<TAlternate, T> result;
unsafe { result = Unsafe.As<IAlternateEqualityComparer<TAlternate, T>>(set._comparer)!; }
return result;
}

/// <summary>Adds the specified element to a set.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void SetTarget(object? target, ComInfo? comInfo)
if (_comInfo != null)
{
// Check if the target is still null
target = Unsafe.As<T>(GCHandle.InternalGet(_weakHandle));
unsafe { target = Unsafe.As<T>(GCHandle.InternalGet(_weakHandle)); }
if (target == null)
{
// Resolve and reset. Perform runtime cast to catch bugs
Expand Down Expand Up @@ -146,22 +146,32 @@ private static ComAwareWeakReference EnsureComAwareReference(ref nint taggedHand
GC.SuppressFinalize(newRef);
}

return Unsafe.As<ComAwareWeakReference>(GCHandle.InternalGet(taggedHandle & ~HandleTagBits)!);
ComAwareWeakReference result;
unsafe { result = Unsafe.As<ComAwareWeakReference>(GCHandle.InternalGet(taggedHandle & ~HandleTagBits)!); }
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ComAwareWeakReference GetFromTaggedReference(nint taggedHandle)
{
Debug.Assert((taggedHandle & ComAwareBit) != 0);
return Unsafe.As<ComAwareWeakReference>(GCHandle.InternalGet(taggedHandle & ~HandleTagBits)!);
ComAwareWeakReference result;
unsafe { result = Unsafe.As<ComAwareWeakReference>(GCHandle.InternalGet(taggedHandle & ~HandleTagBits)!); }
return result;
}

[MethodImpl(MethodImplOptions.NoInlining)]
internal static void SetTarget(ref nint taggedHandle, object? target, ComInfo? comInfo)
{
ComAwareWeakReference comAwareRef = comInfo != null ?
EnsureComAwareReference(ref taggedHandle) :
Unsafe.As<ComAwareWeakReference>(GCHandle.InternalGet(taggedHandle & ~HandleTagBits)!);
ComAwareWeakReference comAwareRef;
if (comInfo != null)
{
comAwareRef = EnsureComAwareReference(ref taggedHandle);
}
else
{
unsafe { comAwareRef = Unsafe.As<ComAwareWeakReference>(GCHandle.InternalGet(taggedHandle & ~HandleTagBits)!); }
}

comAwareRef.SetTarget(target, comInfo);
}
Expand Down
Loading
Loading