Skip to content

Commit 4acd106

Browse files
authored
[mono][jit] Simplify method_needs_stack_walk (). (#98230)
Instead of hardcoding a set of classes, check that the method has a DynamicSecurityMethod attribute, eliminating false positives.
1 parent fc08df1 commit 4acd106

File tree

2 files changed

+5
-16
lines changed

2 files changed

+5
-16
lines changed

src/mono/System.Private.CoreLib/src/System/Reflection/Assembly.Mono.cs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static Assembly GetExecutingAssembly()
4343
internal static extern RuntimeAssembly GetExecutingAssembly(ref StackCrawlMark stackMark);
4444

4545
[MethodImplAttribute(MethodImplOptions.InternalCall)]
46+
[System.Security.DynamicSecurityMethod] // Methods doing stack walks has to be marked DynamicSecurityMethod
4647
public static extern Assembly GetCallingAssembly();
4748

4849
[MethodImplAttribute(MethodImplOptions.InternalCall)]

src/mono/mono/mini/method-to-ir.c

+4-16
Original file line numberDiff line numberDiff line change
@@ -3547,23 +3547,11 @@ method_needs_stack_walk (MonoCompile *cfg, MonoMethod *cmethod)
35473547
}
35483548

35493549
/*
3550-
* In corelib code, methods which need to do a stack walk declare a StackCrawlMark local and pass it as an
3551-
* arguments until it reaches an icall. Its hard to detect which methods do that especially with
3552-
* StackCrawlMark.LookForMyCallersCaller, so for now, just hardcode the classes which contain the public
3553-
* methods whose caller is needed.
3550+
* Methods which do stack walks are marked with [System.Security.DynamicSecurityMethod] in the bcl.
3551+
* This check won't work for StackCrawlMark.LookForMyCallersCaller, but thats not currently by the
3552+
* stack walk code anyway.
35543553
*/
3555-
if (mono_is_corlib_image (m_class_get_image (cmethod->klass))) {
3556-
const char *cname = m_class_get_name (cmethod->klass);
3557-
if (!strcmp (cname, "Assembly") ||
3558-
!strcmp (cname, "AssemblyLoadContext") ||
3559-
(!strcmp (cname, "Activator"))) {
3560-
if (!strcmp (cmethod->name, "op_Equality"))
3561-
return FALSE;
3562-
return TRUE;
3563-
}
3564-
}
3565-
3566-
return FALSE;
3554+
return (cmethod->flags & METHOD_ATTRIBUTE_REQSECOBJ) != 0;
35673555
}
35683556

35693557
G_GNUC_UNUSED MonoInst*

0 commit comments

Comments
 (0)