How to use IsTaskType method of Microsoft.Coyote.Rewriting.InterAssemblyInvocationRewritingPass class

Best Coyote code snippet using Microsoft.Coyote.Rewriting.InterAssemblyInvocationRewritingPass.IsTaskType

InterAssemblyInvocationRewritingPass.cs

Source:InterAssemblyInvocationRewritingPass.cs Github

copy

Full Screen

...38 instruction.Operand is MethodReference methodReference &&39 this.IsForeignType(methodReference.DeclaringType))40 {41 TypeDefinition resolvedReturnType = methodReference.ReturnType.Resolve();42 if (IsTaskType(resolvedReturnType, NameCache.TaskName, NameCache.SystemTasksNamespace))43 {44 string methodName = GetFullyQualifiedMethodName(methodReference);45 Instruction nextInstruction = instruction.Next;46 MethodReference interceptionMethod = this.CreateInterceptionMethod(47 typeof(ExceptionProvider), methodReference,48 nameof(ExceptionProvider.ThrowIfReturnedTaskNotControlled));49 TypeReference interceptedReturnType = this.CreateInterceptedReturnType(methodReference);50 var instructions = this.CreateInterceptionMethodCallInstructions(51 interceptionMethod, nextInstruction, interceptedReturnType, methodName);52 if (instructions.Count > 0)53 {54 Debug.WriteLine($"............. [+] uncontrolled task assertion when invoking '{methodName}'");55 instructions.ForEach(i => this.Processor.InsertBefore(nextInstruction, i));56 this.IsMethodBodyModified = true;57 }58 }59 else if (IsTaskType(resolvedReturnType, NameCache.ValueTaskName, NameCache.SystemTasksNamespace))60 {61 string methodName = GetFullyQualifiedMethodName(methodReference);62 Instruction nextInstruction = instruction.Next;63 MethodReference interceptionMethod = this.CreateInterceptionMethod(64 typeof(ExceptionProvider), methodReference,65 nameof(ExceptionProvider.ThrowIfReturnedValueTaskNotControlled));66 TypeReference interceptedReturnType = this.CreateInterceptedReturnType(methodReference);67 var instructions = this.CreateInterceptionMethodCallInstructions(68 interceptionMethod, nextInstruction, interceptedReturnType, methodName);69 if (instructions.Count > 0)70 {71 Debug.WriteLine($"............. [+] uncontrolled value task assertion when invoking '{methodName}'");72 instructions.ForEach(i => this.Processor.InsertBefore(nextInstruction, i));73 this.IsMethodBodyModified = true;74 }75 }76 else if (methodReference.Name is "GetAwaiter" && IsTaskType(resolvedReturnType,77 NameCache.TaskAwaiterName, NameCache.SystemCompilerNamespace))78 {79 MethodReference interceptionMethod = this.CreateInterceptionMethod(80 typeof(TaskAwaiter), methodReference,81 nameof(TaskAwaiter.Wrap));82 Instruction newInstruction = Instruction.Create(OpCodes.Call, interceptionMethod);83 Debug.WriteLine($"............. [+] {newInstruction}");84 this.Processor.InsertAfter(instruction, newInstruction);85 this.IsMethodBodyModified = true;86 }87 else if (methodReference.Name is "GetAwaiter" && IsTaskType(resolvedReturnType,88 NameCache.ValueTaskAwaiterName, NameCache.SystemCompilerNamespace))89 {90 MethodReference interceptionMethod = this.CreateInterceptionMethod(91 typeof(ValueTaskAwaiter), methodReference,92 nameof(ValueTaskAwaiter.Wrap));93 Instruction newInstruction = Instruction.Create(OpCodes.Call, interceptionMethod);94 Debug.WriteLine($"............. [+] {newInstruction}");95 this.Processor.InsertAfter(instruction, newInstruction);96 this.IsMethodBodyModified = true;97 }98#if NET || NETCOREAPP3_199 else if (IsSystemType(resolvedReturnType) && resolvedReturnType.FullName == NameCache.HttpClient)100 {101 MethodReference interceptionMethod = this.CreateInterceptionMethod(102 typeof(HttpClient), methodReference, nameof(HttpClient.Control));103 Instruction newInstruction = Instruction.Create(OpCodes.Call, interceptionMethod);104 Debug.WriteLine($"............. [+] {newInstruction}");105 this.Processor.InsertAfter(instruction, newInstruction);106 this.IsMethodBodyModified = true;107 }108#endif109 }110 return instruction;111 }112 /// <summary>113 /// Creates the IL instructions for invoking the specified interception method.114 /// </summary>115 private List<Instruction> CreateInterceptionMethodCallInstructions(MethodReference interceptionMethod,116 Instruction nextInstruction, TypeReference returnType, string methodName)117 {118 var instructions = new List<Instruction>();119 bool isParamByReference = interceptionMethod.Parameters[0].ParameterType.IsByReference;120 if (nextInstruction.OpCode == OpCodes.Stsfld &&121 nextInstruction.Operand is FieldReference fieldReference)122 {123 OpCode loadOpCode = isParamByReference ? OpCodes.Ldsflda : OpCodes.Ldsfld;124 instructions.Add(this.Processor.Create(nextInstruction.OpCode, fieldReference));125 instructions.Add(this.Processor.Create(loadOpCode, fieldReference));126 }127 else if (nextInstruction.OpCode == OpCodes.Starg_S &&128 nextInstruction.Operand is ParameterDefinition parameterDefinition)129 {130 OpCode loadOpCode = isParamByReference ? OpCodes.Ldarga_S :131 parameterDefinition.Index is 0 ? OpCodes.Ldarg_0 :132 parameterDefinition.Index is 1 ? OpCodes.Ldarg_1 :133 parameterDefinition.Index is 2 ? OpCodes.Ldarg_2 :134 parameterDefinition.Index is 3 ? OpCodes.Ldarg_3 :135 OpCodes.Ldarg_S;136 instructions.Add(this.Processor.Create(nextInstruction.OpCode, parameterDefinition));137 instructions.Add(loadOpCode == OpCodes.Ldarga_S || loadOpCode == OpCodes.Ldarg_S ?138 this.Processor.Create(loadOpCode, parameterDefinition) :139 this.Processor.Create(loadOpCode));140 }141 else if (nextInstruction.OpCode == OpCodes.Stloc_S &&142 nextInstruction.Operand is VariableDefinition variableDefinition)143 {144 OpCode loadOpCode = isParamByReference ? OpCodes.Ldloca_S : OpCodes.Ldloc_S;145 instructions.Add(this.Processor.Create(nextInstruction.OpCode, variableDefinition));146 instructions.Add(this.Processor.Create(loadOpCode, variableDefinition));147 }148 else if (nextInstruction.OpCode == OpCodes.Stloc_0)149 {150 OpCode loadOpCode = isParamByReference ? OpCodes.Ldloca_S : OpCodes.Ldloc_0;151 instructions.Add(this.Processor.Create(nextInstruction.OpCode));152 instructions.Add(isParamByReference ? this.Processor.Create(loadOpCode, (byte)0) :153 this.Processor.Create(loadOpCode));154 }155 else if (nextInstruction.OpCode == OpCodes.Stloc_1)156 {157 OpCode loadOpCode = isParamByReference ? OpCodes.Ldloca_S : OpCodes.Ldloc_1;158 instructions.Add(this.Processor.Create(nextInstruction.OpCode));159 instructions.Add(isParamByReference ? this.Processor.Create(loadOpCode, (byte)1) :160 this.Processor.Create(loadOpCode));161 }162 else if (nextInstruction.OpCode == OpCodes.Stloc_2)163 {164 OpCode loadOpCode = isParamByReference ? OpCodes.Ldloca_S : OpCodes.Ldloc_2;165 instructions.Add(this.Processor.Create(nextInstruction.OpCode));166 instructions.Add(isParamByReference ? this.Processor.Create(loadOpCode, (byte)2) :167 this.Processor.Create(loadOpCode));168 }169 else if (nextInstruction.OpCode == OpCodes.Stloc_3)170 {171 OpCode loadOpCode = isParamByReference ? OpCodes.Ldloca_S : OpCodes.Ldloc_3;172 instructions.Add(this.Processor.Create(nextInstruction.OpCode));173 instructions.Add(isParamByReference ? this.Processor.Create(loadOpCode, (byte)3) :174 this.Processor.Create(loadOpCode));175 }176 else177 {178 instructions.Add(this.Processor.Create(OpCodes.Ldstr, methodName));179 instructions.Add(this.Processor.Create(OpCodes.Call, interceptionMethod));180 return instructions;181 }182 instructions.Add(this.Processor.Create(OpCodes.Ldstr, methodName));183 instructions.Add(this.Processor.Create(OpCodes.Call, interceptionMethod));184 if (interceptionMethod.ReturnType.IsByReference)185 {186 instructions.Add(this.Processor.Create(OpCodes.Ldobj, returnType));187 }188 return instructions;189 }190 /// <summary>191 /// Creates an interception method from the specified method and type.192 /// </summary>193 private MethodReference CreateInterceptionMethod(Type type, MethodReference methodReference,194 string interceptionMethodName)195 {196 var returnType = methodReference.ReturnType;197 TypeDefinition providerType = this.Module.ImportReference(type).Resolve();198 MethodReference wrapMethod = null;199 if (returnType is GenericInstanceType genericType)200 {201 GenericInstanceType resolvedType = ResolveGenericTypeArguments(genericType, methodReference);202 TypeReference argType = resolvedType.HasGenericArguments ?203 resolvedType.GenericArguments.FirstOrDefault() :204 resolvedType;205 MethodDefinition genericMethod = providerType.Methods.FirstOrDefault(206 m => m.Name == interceptionMethodName && m.HasGenericParameters);207 MethodReference wrapReference = this.Module.ImportReference(genericMethod);208 wrapMethod = MakeGenericMethod(wrapReference, argType);209 }210 else211 {212 wrapMethod = providerType.Methods.FirstOrDefault(m => m.Name == interceptionMethodName);213 }214 return this.Module.ImportReference(wrapMethod);215 }216 /// <summary>217 /// Creates an intercepted return type from the specified method.218 /// </summary>219 private TypeReference CreateInterceptedReturnType(MethodReference methodReference)220 {221 var returnType = methodReference.ReturnType;222 if (returnType is GenericInstanceType genericType)223 {224 GenericInstanceType resolvedType = ResolveGenericTypeArguments(genericType, methodReference);225 TypeReference argType = resolvedType.HasGenericArguments ?226 resolvedType.GenericArguments.FirstOrDefault() :227 resolvedType;228 returnType = MakeGenericType(genericType.ElementType, argType);229 returnType = this.Module.ImportReference(returnType);230 }231 return returnType;232 }233 /// <summary>234 /// Resolves the generic arguments of the specified type using the given method reference.235 /// </summary>236 private static GenericInstanceType ResolveGenericTypeArguments(GenericInstanceType genericType,237 MethodReference methodReference)238 {239 GenericInstanceType resolvedType = new GenericInstanceType(genericType.ElementType);240 foreach (var genericArgument in genericType.GenericArguments)241 {242 TypeReference argType = genericArgument;243 if (argType is GenericParameter gp)244 {245 if (gp.Type is GenericParameterType.Type &&246 methodReference.DeclaringType is GenericInstanceType dgt)247 {248 argType = dgt.GenericArguments[gp.Position];249 }250 else if (gp.Type is GenericParameterType.Method &&251 methodReference is GenericInstanceMethod gim)252 {253 argType = gim.GenericArguments[gp.Position];254 }255 }256 else if (argType is GenericInstanceType git)257 {258 argType = ResolveGenericTypeArguments(git, methodReference);259 }260 resolvedType.GenericArguments.Add(argType);261 }262 return resolvedType;263 }264 /// <summary>265 /// Checks if the specified type is the expected task type.266 /// </summary>267 private static bool IsTaskType(TypeDefinition type, string expectedName, string expectedNamespace)268 {269 if (type != null)270 {271 if (IsSystemType(type) && type.Namespace == expectedNamespace &&272 (type.Name == expectedName || type.Name.StartsWith(expectedName + "`")))273 {274 return true;275 }276 }277 return false;278 }279 /// <summary>280 /// Checks if the specified type is a task-like type.281 /// </summary>...

Full Screen

Full Screen

IsTaskType

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Tasks;6{7 {8 public static void Main(string[] args)9 {10 var isTask = CoyoteRuntime.IsTaskType(typeof(Task));11 var isActor = CoyoteRuntime.IsActorType(typeof(Actor));12 var isActorTask = CoyoteRuntime.IsActorTaskType(typeof(ActorTask));13 }14 }15}

Full Screen

Full Screen

IsTaskType

Using AI Code Generation

copy

Full Screen

1using System;2using Microsoft.Coyote;3using Microsoft.Coyote.Tasks;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine(InterAssemblyInvocationRewritingPass.IsTaskType(typeof(Task)));9 Console.WriteLine(InterAssemblyInvocationRewritingPass.IsTaskType(typeof(int)));10 }11 }12}

Full Screen

Full Screen

IsTaskType

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Rewriting;2using System;3using System.Threading.Tasks;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 Console.WriteLine(InterAssemblyInvocationRewritingPass.IsTaskType(typeof(Task)));10 }11 }12}

Full Screen

Full Screen

IsTaskType

Using AI Code Generation

copy

Full Screen

1{2 {3 public static bool IsTaskType(Type t)4 {5 return t.IsTaskType();6 }7 }8}

Full Screen

Full Screen

IsTaskType

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Rewriting;2using System;3using System.Threading.Tasks;4{5 {6 public static void Main()7 {8 Console.WriteLine("Hello World!");9 Console.WriteLine(IsTaskType(typeof(Task)));10 }11 }12}

Full Screen

Full Screen

IsTaskType

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3{4 {5 static void Main(string[] args)6 {7 var task = new Task(() => { });8 var isTaskType = Microsoft.Coyote.Rewriting.InterAssemblyInvocationRewritingPass.IsTaskType(task.GetType());9 Console.WriteLine("IsTaskType: " + isTaskType);10 }11 }12}

Full Screen

Full Screen

IsTaskType

Using AI Code Generation

copy

Full Screen

1{2 public static void Main()3 {4 Console.WriteLine("Hello World!");5 Console.WriteLine(InterAssemblyInvocationRewritingPass.IsTaskType(typeof(Program).GetMethod("Main")));6 }7}8{9 public static void Main()10 {11 Console.WriteLine("Hello World!");12 Console.WriteLine(Microsoft.Coyote.Rewriting.InterAssemblyInvocationRewritingPass.IsTaskType(typeof(Program).GetMethod("Main")));13 }14}15{16 public static void Main()17 {18 Console.WriteLine("Hello World!");19 Console.WriteLine(Microsoft.Coyote.Rewriting.InterAssemblyInvocationRewritingPass.IsTaskType(typeof(Program).GetMethod("Main")));20 }21}22{23 public static void Main()24 {25 Console.WriteLine("Hello World!");26 Console.WriteLine(Microsoft.Coyote.Rewriting.InterAssemblyInvocationRewritingPass.IsTaskType(typeof(Program).GetMethod("Main")));27 }28}29{30 public static void Main()31 {32 Console.WriteLine("Hello World!");33 Console.WriteLine(Microsoft.Coyote.Rewriting.InterAssemblyInvocationRewritingPass.IsTaskType(typeof(Program).GetMethod("Main")));34 }35}36{37 public static void Main()38 {39 Console.WriteLine("Hello World!");

Full Screen

Full Screen

IsTaskType

Using AI Code Generation

copy

Full Screen

1{2 static void Main(string[] args)3 {4 Console.WriteLine("Hello World!");5 var task = new Task();6 task.Run();7 }8}9{10 static void Main(string[] args)11 {12 Console.WriteLine("Hello World!");13 var task = new Task();14 task.Run();15 }16}17{18 static void Main(string[] args)19 {20 Console.WriteLine("Hello World!");21 var task = new Task();22 task.Run();23 }24}25{26 static void Main(string[] args)27 {28 Console.WriteLine("Hello World!");29 var task = new Task();30 task.Run();31 }32}33{34 static void Main(string[] args)35 {36 Console.WriteLine("Hello World!");37 var task = new Task();38 task.Run();39 }40}41{42 static void Main(string[] args)43 {44 Console.WriteLine("Hello World!");45 var task = new Task();46 task.Run();47 }48}49{50 static void Main(string[] args

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Coyote automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful