pose
pose copied to clipboard
System.InvalidProgramException when trying to shim DateTime.Now
Write the following program:
using System;
using Pose;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
var dt = DateTime.Now;
Shim dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 4, 4));
PoseContext.Isolate(() =>
{
var time = DateTime.Now;
Console.WriteLine(time);
},
dateTimeShim);
}
}
}
Compile it with netcoreapp2.0
and run with .NET Core ⇒ throws an exception:
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: Common Language
Runtime detected an invalid program.
at dynamic_System.Text.Encoding_.ctor(Encoding , Int32 )
at stub_ctor_System.Text.Encoding_.ctor(Encoding , Int32 , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Text.OSEncoding_.ctor(OSEncoding , Int32 )
at stub_ctor_System.Text.OSEncoding_.ctor(Int32 , RuntimeMethodHandle , RuntimeTypeHandle )
at stub_System.Text.EncodingHelper_GetSupportedConsoleEncoding(Int32 , RuntimeMethodHandle , RuntimeTypeHandle )
at stub_System.ConsolePal_get_OutputEncoding(RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Threading.LazyInitializer_EnsureInitializedCore(Encoding& , Object& , Func`1 )
at stub_System.Threading.LazyInitializer_EnsureInitializedCore(Encoding& , Object& , Func`1 , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Threading.LazyInitializer_EnsureInitialized(Encoding& , Object& , Func`1 )
at stub_System.Threading.LazyInitializer_EnsureInitialized(Encoding& , Object& , Func`1 , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Console_EnsureInitialized(Encoding& , Func`1 )
at stub_System.Console_EnsureInitialized(Encoding& , Func`1 , RuntimeMethodHandle , RuntimeTypeHandle )
at stub_System.Console_get_OutputEncoding(RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.ConsolePal_GetUseFileAPIs(Int32 )
at stub_System.ConsolePal_GetUseFileAPIs(Int32 , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.ConsolePal_GetStandardFile(Int32 , FileAccess )
at stub_System.ConsolePal_GetStandardFile(Int32 , FileAccess , RuntimeMethodHandle , RuntimeTypeHandle )
at stub_System.ConsolePal_OpenStandardOutput(RuntimeMethodHandle , RuntimeTypeHandle )
at stub_System.Console_OpenStandardOutput(RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Console+<>c_<get_Out>b__25_0(<>c )
at dynamic_System.Threading.LazyInitializer_EnsureInitializedCore(TextWriter& , Object& , Func`1 )
at stub_System.Threading.LazyInitializer_EnsureInitializedCore(TextWriter& , Object& , Func`1 , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Threading.LazyInitializer_EnsureInitialized(TextWriter& , Object& , Func`1 )
at stub_System.Threading.LazyInitializer_EnsureInitialized(TextWriter& , Object& , Func`1 , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Console_EnsureInitialized(TextWriter& , Func`1 )
at stub_System.Console_EnsureInitialized(TextWriter& , Func`1 , RuntimeMethodHandle , RuntimeTypeHandle )
at stub_System.Console_get_Out(RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Console_WriteLine(Object )
at stub_System.Console_WriteLine(Object , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_ConsoleApp4.Program+<>c_<Main>b__0_2(<>c )
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at Pose.PoseContext.Isolate(Action entryPoint, Shim[] shims)
at ConsoleApp4.Program.Main(String[] args) in T:\Temp\ConsoleApp4\ConsoleApp4\Program.cs:line 12
Have the same with a couple of other tests i am trying. any updates?
Still working out the intricacies of the new implementation especially around performance. Expression trees allow for more features but are a bit harder to translate IL to
👍 to expression trees being a PITA to debug and deal with when they do not work. Really cool stuff though!
Do you have any ETA on when this will be fixed?
Pose is so useful, but this issue limits its main functionality.
Compiling this with .NET 4.7.1 and the latest Github version of POSE also results in the same exception. Is this as expected?
Hi @crushjz, can't give a definite ETA at the moment. I've been a bit swamped with work and all, I believe I can fix most of the InvalidProgramException
without resorting to a re-implementation with Expression Trees. This will be my focus for the coming weeks
This is awesome news, thank you so much @tonerdo for your incredible effort!
Hi @tonerdo, any updates on this yet? :smile:
@devedse been improving the debugging experience around the generated IL to help figure out exactly where the problem is. Just cleaning that up and will make a release soon
Thanks, keep us up to date :D
I've been able to fix the InvalidProgramException
errors. Turned out I wasn't handling the presence of the constrained
opcode. However, I'm encountering situations where the entire runtime seg faults without any error message, so I'm currently debugging that
Any workaround or previous version that doesn't have the issue?
Guys, I'm also having the same issue. Any advances or previous versions that could work? Congratulations for the nice software!
Same issue when trying to mock static method. Updates?
This issue is still present in the latest NuGet package (version 1.2.1). I was able to run my tests by reverting to version 1.1.0
Same for me even when I revert to 1.1.0. Pity.
This defect still persists.
Is there any update on this? Would love to see it fixed as I only need to Shim a LoggingService in a 3rd party dll
any plan to resolve it?
Given that as of today, this bug has effectively been ignored for over 5 years, I think its safe to say that if you run into this defect, that means Prose is no longer an option that can be trusted.
Its important to call out that even the maintainer doesn't want to fix this, as shown by the lack of a fix in 5 years of people waiting, so its probably best to find other options.
@ThaDaVos @motcke Please see #80