pose icon indicating copy to clipboard operation
pose copied to clipboard

System.InvalidProgramException when trying to shim DateTime.Now

Open ForNeVeR opened this issue 6 years ago • 21 comments

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

ForNeVeR avatar Jan 29 '18 14:01 ForNeVeR

Have the same with a couple of other tests i am trying. any updates?

alexzaytsev-newsroomly avatar Apr 09 '18 23:04 alexzaytsev-newsroomly

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

tonerdo avatar Apr 11 '18 08:04 tonerdo

👍 to expression trees being a PITA to debug and deal with when they do not work. Really cool stuff though!

Stoom avatar Jun 04 '18 06:06 Stoom

Do you have any ETA on when this will be fixed?

Pose is so useful, but this issue limits its main functionality.

crushjz avatar Jun 12 '18 15:06 crushjz

Compiling this with .NET 4.7.1 and the latest Github version of POSE also results in the same exception. Is this as expected?

devedse avatar Jul 05 '18 18:07 devedse

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

tonerdo avatar Jul 10 '18 10:07 tonerdo

This is awesome news, thank you so much @tonerdo for your incredible effort!

crushjz avatar Jul 10 '18 10:07 crushjz

Hi @tonerdo, any updates on this yet? :smile:

devedse avatar Aug 09 '18 23:08 devedse

@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

tonerdo avatar Aug 11 '18 15:08 tonerdo

Thanks, keep us up to date :D

devedse avatar Aug 11 '18 16:08 devedse

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

tonerdo avatar Aug 12 '18 01:08 tonerdo

Any workaround or previous version that doesn't have the issue?

SamerAdra avatar Sep 05 '18 19:09 SamerAdra

Guys, I'm also having the same issue. Any advances or previous versions that could work? Congratulations for the nice software!

igormcoelho avatar Nov 27 '18 14:11 igormcoelho

Same issue when trying to mock static method. Updates?

achobanov avatar Jul 25 '19 10:07 achobanov

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

rstarritt avatar Dec 31 '19 21:12 rstarritt

Same for me even when I revert to 1.1.0. Pity.

vjrossi avatar May 21 '20 10:05 vjrossi

This defect still persists.

duaneking avatar Mar 09 '23 22:03 duaneking

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

ThaDaVos avatar Nov 09 '23 13:11 ThaDaVos

any plan to resolve it?

motcke avatar Nov 16 '23 12:11 motcke

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.

duaneking avatar Nov 21 '23 00:11 duaneking

@ThaDaVos @motcke Please see #80

Miista avatar Jan 25 '24 18:01 Miista