1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.Runtime
{
using System.Diagnostics;
using System.Runtime.Diagnostics;
using System.Security;
using System.Threading;
abstract class ActionItem
{
#if FEATURE_COMPRESSEDSTACK
[Fx.Tag.SecurityNote(Critical = "Stores the security context, used later in binding back into")]
[SecurityCritical]
SecurityContext context;
#endif
bool isScheduled;
bool lowPriority;
protected ActionItem()
{
}
public bool LowPriority
{
get
{
return this.lowPriority;
}
protected set
{
this.lowPriority = value;
}
}
public static void Schedule(Action<object> callback, object state)
{
Schedule(callback, state, false);
}
[Fx.Tag.SecurityNote(Critical = "Calls into critical method ScheduleCallback",
Safe = "Schedule invoke of the given delegate under the current context")]
[SecuritySafeCritical]
public static void Schedule(Action<object> callback, object state, bool lowPriority)
{
Fx.Assert(callback != null, "A null callback was passed for Schedule!");
if (PartialTrustHelpers.ShouldFlowSecurityContext ||
WaitCallbackActionItem.ShouldUseActivity ||
Fx.Trace.IsEnd2EndActivityTracingEnabled)
{
new DefaultActionItem(callback, state, lowPriority).Schedule();
}
else
{
ScheduleCallback(callback, state, lowPriority);
}
}
[Fx.Tag.SecurityNote(Critical = "Called after applying the user context on the stack or (potentially) " +
"without any user context on the stack")]
[SecurityCritical]
protected abstract void Invoke();
[Fx.Tag.SecurityNote(Critical = "Access critical field context and critical property " +
"CallbackHelper.InvokeWithContextCallback, calls into critical method " +
"PartialTrustHelpers.CaptureSecurityContextNoIdentityFlow, calls into critical method ScheduleCallback; " +
"since the invoked method and the capturing of the security contex are de-coupled, can't " +
"be treated as safe")]
[SecurityCritical]
protected void Schedule()
{
if (isScheduled)
{
throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.ActionItemIsAlreadyScheduled));
}
this.isScheduled = true;
#if FEATURE_COMPRESSEDSTACK
if (PartialTrustHelpers.ShouldFlowSecurityContext)
{
this.context = PartialTrustHelpers.CaptureSecurityContextNoIdentityFlow();
}
if (this.context != null)
{
ScheduleCallback(CallbackHelper.InvokeWithContextCallback);
}
else
#endif
{
ScheduleCallback(CallbackHelper.InvokeWithoutContextCallback);
}
}
#if FEATURE_COMPRESSEDSTACK
[Fx.Tag.SecurityNote(Critical = "Access critical field context and critical property " +
"CallbackHelper.InvokeWithContextCallback, calls into critical method ScheduleCallback; " +
"since nothing is known about the given context, can't be treated as safe")]
[SecurityCritical]
protected void ScheduleWithContext(SecurityContext context)
{
if (context == null)
{
throw Fx.Exception.ArgumentNull("context");
}
if (isScheduled)
{
throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.ActionItemIsAlreadyScheduled));
}
this.isScheduled = true;
this.context = context.CreateCopy();
ScheduleCallback(CallbackHelper.InvokeWithContextCallback);
}
#endif
[Fx.Tag.SecurityNote(Critical = "Access critical property CallbackHelper.InvokeWithoutContextCallback, " +
"Calls into critical method ScheduleCallback; not bound to a security context")]
[SecurityCritical]
protected void ScheduleWithoutContext()
{
if (isScheduled)
{
throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.ActionItemIsAlreadyScheduled));
}
this.isScheduled = true;
ScheduleCallback(CallbackHelper.InvokeWithoutContextCallback);
}
[Fx.Tag.SecurityNote(Critical = "Calls into critical methods IOThreadScheduler.ScheduleCallbackNoFlow, " +
"IOThreadScheduler.ScheduleCallbackLowPriNoFlow")]
[SecurityCritical]
static void ScheduleCallback(Action<object> callback, object state, bool lowPriority)
{
Fx.Assert(callback != null, "Cannot schedule a null callback");
if (lowPriority)
{
IOThreadScheduler.ScheduleCallbackLowPriNoFlow(callback, state);
}
else
{
IOThreadScheduler.ScheduleCallbackNoFlow(callback, state);
}
}
#if FEATURE_COMPRESSEDSTACK
[Fx.Tag.SecurityNote(Critical = "Extract the security context stored and reset the critical field")]
[SecurityCritical]
SecurityContext ExtractContext()
{
Fx.Assert(this.context != null, "Cannot bind to a null context; context should have been set by now");
Fx.Assert(this.isScheduled, "Context is extracted only while the object is scheduled");
SecurityContext result = this.context;
this.context = null;
return result;
}
#endif
[Fx.Tag.SecurityNote(Critical = "Calls into critical static method ScheduleCallback")]
[SecurityCritical]
void ScheduleCallback(Action<object> callback)
{
ScheduleCallback(callback, this, this.lowPriority);
}
[SecurityCritical]
static class CallbackHelper
{
#if FEATURE_COMPRESSEDSTACK
[Fx.Tag.SecurityNote(Critical = "Stores a delegate to a critical method")]
static Action<object> invokeWithContextCallback;
#endif
[Fx.Tag.SecurityNote(Critical = "Stores a delegate to a critical method")]
static Action<object> invokeWithoutContextCallback;
[Fx.Tag.SecurityNote(Critical = "Stores a delegate to a critical method")]
static ContextCallback onContextAppliedCallback;
#if FEATURE_COMPRESSEDSTACK
[Fx.Tag.SecurityNote(Critical = "Provides access to a critical field; Initialize it with " +
"a delegate to a critical method")]
public static Action<object> InvokeWithContextCallback
{
get
{
if (invokeWithContextCallback == null)
{
invokeWithContextCallback = new Action<object>(InvokeWithContext);
}
return invokeWithContextCallback;
}
}
#endif
[Fx.Tag.SecurityNote(Critical = "Provides access to a critical field; Initialize it with " +
"a delegate to a critical method")]
public static Action<object> InvokeWithoutContextCallback
{
get
{
if (invokeWithoutContextCallback == null)
{
invokeWithoutContextCallback = new Action<object>(InvokeWithoutContext);
}
return invokeWithoutContextCallback;
}
}
[Fx.Tag.SecurityNote(Critical = "Provides access to a critical field; Initialize it with " +
"a delegate to a critical method")]
public static ContextCallback OnContextAppliedCallback
{
get
{
if (onContextAppliedCallback == null)
{
onContextAppliedCallback = new ContextCallback(OnContextApplied);
}
return onContextAppliedCallback;
}
}
#if FEATURE_COMPRESSEDSTACK
[Fx.Tag.SecurityNote(Critical = "Called by the scheduler without any user context on the stack")]
static void InvokeWithContext(object state)
{
SecurityContext context = ((ActionItem)state).ExtractContext();
SecurityContext.Run(context, OnContextAppliedCallback, state);
}
#endif
[Fx.Tag.SecurityNote(Critical = "Called by the scheduler without any user context on the stack")]
static void InvokeWithoutContext(object state)
{
((ActionItem)state).Invoke();
((ActionItem)state).isScheduled = false;
}
[Fx.Tag.SecurityNote(Critical = "Called after applying the user context on the stack")]
static void OnContextApplied(object o)
{
((ActionItem)o).Invoke();
((ActionItem)o).isScheduled = false;
}
}
class DefaultActionItem : ActionItem
{
[Fx.Tag.SecurityNote(Critical = "Stores a delegate that will be called later, at a particular context")]
[SecurityCritical]
Action<object> callback;
[Fx.Tag.SecurityNote(Critical = "Stores an object that will be passed to the delegate that will be " +
"called later, at a particular context")]
[SecurityCritical]
object state;
bool flowLegacyActivityId;
Guid activityId;
EventTraceActivity eventTraceActivity;
[Fx.Tag.SecurityNote(Critical = "Access critical fields callback and state",
Safe = "Doesn't leak information or resources")]
[SecuritySafeCritical]
public DefaultActionItem(Action<object> callback, object state, bool isLowPriority)
{
Fx.Assert(callback != null, "Shouldn't instantiate an object to wrap a null callback");
base.LowPriority = isLowPriority;
this.callback = callback;
this.state = state;
if (WaitCallbackActionItem.ShouldUseActivity)
{
this.flowLegacyActivityId = true;
this.activityId = EtwDiagnosticTrace.ActivityId;
}
if (Fx.Trace.IsEnd2EndActivityTracingEnabled)
{
this.eventTraceActivity = EventTraceActivity.GetFromThreadOrCreate();
if (TraceCore.ActionItemScheduledIsEnabled(Fx.Trace))
{
TraceCore.ActionItemScheduled(Fx.Trace, this.eventTraceActivity);
}
}
}
[Fx.Tag.SecurityNote(Critical = "Implements a the critical abstract ActionItem.Invoke method, " +
"Access critical fields callback and state")]
[SecurityCritical]
protected override void Invoke()
{
if (this.flowLegacyActivityId || Fx.Trace.IsEnd2EndActivityTracingEnabled)
{
TraceAndInvoke();
}
else
{
this.callback(this.state);
}
}
[Fx.Tag.SecurityNote(Critical = "Implements a the critical abstract Trace method, " +
"Access critical fields callback and state")]
[SecurityCritical]
void TraceAndInvoke()
{
//
if (this.flowLegacyActivityId)
{
Guid currentActivityId = EtwDiagnosticTrace.ActivityId;
try
{
EtwDiagnosticTrace.ActivityId = this.activityId;
this.callback(this.state);
}
finally
{
EtwDiagnosticTrace.ActivityId = currentActivityId;
}
}
else
{
Guid previous = Guid.Empty;
bool restoreActivityId = false;
try
{
if (this.eventTraceActivity != null)
{
previous = Trace.CorrelationManager.ActivityId;
restoreActivityId = true;
Trace.CorrelationManager.ActivityId = this.eventTraceActivity.ActivityId;
if (TraceCore.ActionItemCallbackInvokedIsEnabled(Fx.Trace))
{
TraceCore.ActionItemCallbackInvoked(Fx.Trace, this.eventTraceActivity);
}
}
this.callback(this.state);
}
finally
{
if (restoreActivityId)
{
Trace.CorrelationManager.ActivityId = previous;
}
}
}
}
}
}
}
|