Skip to content

Use UnsafeAccessorType in System.Private.CoreLib and the BCL #115583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
11e9cd1
Use UnsafeAccessorType in StackFrameHelper
jkoritzinsky May 13, 2025
6166c51
Convert ComActivator to use UnsafeAccessorType
jkoritzinsky May 14, 2025
df05d6b
ConvertDefaultValueAttribute to use UnsafeAccessorType
jkoritzinsky May 14, 2025
22cd4be
Move AppDomain to use UnsafeAccessorType
jkoritzinsky May 14, 2025
7cee5e3
Convert ResourceReader to use UnsafeAccessorType
jkoritzinsky May 14, 2025
7b696e7
Convert X509ResourceClient to use UnsafeAccessor
jkoritzinsky May 14, 2025
15d985f
Convert TypeDescriptor to use UnsafeAccessorType
jkoritzinsky May 14, 2025
77ba50d
Convert DelegateHelpers to use UnsafeAccessorType
jkoritzinsky May 14, 2025
fe47df6
Convert HttpWebRequest to use UnsafeAccessorType
jkoritzinsky May 14, 2025
d71b334
Convert XmlKeyHelper to use UnsafeAccessorType
jkoritzinsky May 14, 2025
a1f3bc7
Update XmlKeyHelper.cs
jkoritzinsky May 15, 2025
e26d7fa
PR feedback
jkoritzinsky May 15, 2025
84b0be2
Fix trimming test failures
jkoritzinsky May 15, 2025
8973fc5
PR feedback
jkoritzinsky May 16, 2025
a2db757
Migrate over cases where the type name is on a separate line
jkoritzinsky May 16, 2025
bcc5d8d
Convert HttpClientHandler's calls up to various native platform handl…
jkoritzinsky May 16, 2025
7a7bb75
Various fixes
jkoritzinsky May 16, 2025
6945bc8
Fix out param type
jkoritzinsky May 16, 2025
6cfd878
Fix trimming tests
jkoritzinsky May 16, 2025
314b22b
Update AssemblyName.cs
jkoritzinsky May 17, 2025
870775e
Rewrite BinarySerializer path to ensure we still trim out System.Runt…
jkoritzinsky May 20, 2025
4bf9826
suppress warning from bug
jkoritzinsky May 20, 2025
1691219
Remove tests that test removed reflection infra and add librarybuild …
jkoritzinsky May 20, 2025
eb866c9
Fix exception for WindowsPrincipal
jkoritzinsky May 20, 2025
337d504
Fix GetStatusCode return
jkoritzinsky May 21, 2025
e86536f
PR feedback
jkoritzinsky May 23, 2025
9c474cd
PR Feedback
jkoritzinsky May 23, 2025
15a327a
PR feedback
jkoritzinsky May 29, 2025
2faaecf
Merge branch 'main' into unsafe-accessor-type-usage
jkoritzinsky May 30, 2025
b792af2
Fix typos
jkoritzinsky Jun 4, 2025
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

namespace System.Diagnostics
Expand Down Expand Up @@ -38,11 +39,18 @@ internal sealed class StackFrameHelper
private int iFrameCount;
#pragma warning restore 414

private delegate void GetSourceLineInfoDelegate(Assembly? assembly, string assemblyPath, IntPtr loadedPeAddress,
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
[return: UnsafeAccessorType("System.Diagnostics.StackTraceSymbols, System.Diagnostics.StackTrace, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
private static extern object CreateStackTraceSymbols();

[UnsafeAccessor(UnsafeAccessorKind.Method)]
private static extern void GetSourceLineInfo(
[UnsafeAccessorType("System.Diagnostics.StackTraceSymbols, System.Diagnostics.StackTrace, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] object target,
Assembly? assembly, string assemblyPath, IntPtr loadedPeAddress,
int loadedPeSize, bool isFileLayout, IntPtr inMemoryPdbAddress, int inMemoryPdbSize, int methodToken, int ilOffset,
out string? sourceFile, out int sourceLine, out int sourceColumn);

private static GetSourceLineInfoDelegate? s_getSourceLineInfo;
private static object? s_stackTraceSymbolsCache;

[ThreadStatic]
private static int t_reentrancy;
Expand Down Expand Up @@ -97,38 +105,11 @@ internal void InitializeSourceInfo(bool fNeedFileInfo, Exception? exception)
t_reentrancy++;
try
{
if (s_getSourceLineInfo == null)
if (s_stackTraceSymbolsCache == null)
{
Type? symbolsType = Type.GetType(
"System.Diagnostics.StackTraceSymbols, System.Diagnostics.StackTrace, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
throwOnError: false);

if (symbolsType == null)
{
return;
}

Type[] parameterTypes =
[
typeof(Assembly), typeof(string), typeof(IntPtr), typeof(int), typeof(bool), typeof(IntPtr),
typeof(int), typeof(int), typeof(int),
typeof(string).MakeByRefType(), typeof(int).MakeByRefType(), typeof(int).MakeByRefType()
];
MethodInfo? symbolsMethodInfo = symbolsType.GetMethod("GetSourceLineInfo", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, parameterTypes, null);
if (symbolsMethodInfo == null)
{
return;
}

// Create an instance of System.Diagnostics.Stacktrace.Symbols
object? target = Activator.CreateInstance(symbolsType);

// Create an instance delegate for the GetSourceLineInfo method
GetSourceLineInfoDelegate getSourceLineInfo = symbolsMethodInfo.CreateDelegate<GetSourceLineInfoDelegate>(target);

// We could race with another thread. It doesn't matter if we win or lose, the losing instance will be GC'ed and all threads including this one will
// use the winning instance
Interlocked.CompareExchange(ref s_getSourceLineInfo, getSourceLineInfo, null);
Interlocked.CompareExchange(ref s_stackTraceSymbolsCache, CreateStackTraceSymbols(), null);
}

for (int index = 0; index < iFrameCount; index++)
Expand All @@ -137,7 +118,7 @@ internal void InitializeSourceInfo(bool fNeedFileInfo, Exception? exception)
// ENC or the source/line info was already retrieved, the method token is 0.
if (rgiMethodToken![index] != 0)
{
s_getSourceLineInfo!(rgAssembly![index], rgAssemblyPath![index]!, rgLoadedPeAddress![index], rgiLoadedPeSize![index], rgiIsFileLayout![index],
GetSourceLineInfo(s_stackTraceSymbolsCache!, rgAssembly![index], rgAssemblyPath![index]!, rgLoadedPeAddress![index], rgiLoadedPeSize![index], rgiIsFileLayout![index],
rgInMemoryPdbAddress![index], rgiInMemoryPdbSize![index], rgiMethodToken![index],
rgiILOffset![index], out rgFilename![index], out rgiLineNumber![index], out rgiColumnNumber![index]);
}
Expand Down
Loading
Loading