Esempio n. 1
0
        public static void CopyProperties <TSource>(TSource Source, object LinqDestination)
        {
            Type DestinationType = LinqDestination.GetType();
            Type SourceType      = typeof(TSource);

            System.Reflection.FieldInfo[] SourceFields = SourceType.GetFields();
            foreach (System.Reflection.FieldInfo SourceField in SourceFields)
            {
                //bool DataIgnore = false;
                object[] attr = SourceField.GetCustomAttributes(typeof(DataIgnoreAttrib), true);
                if (attr.Length > 0)
                {
                    continue;                 //DataIgnore = true;
                }
                string DestPropName = SourceField.Name;
                attr = SourceField.GetCustomAttributes(typeof(DataNameAttrib), true);
                if (attr.Length > 0)
                {
                    DestPropName = ((DataNameAttrib)attr[0]).DataName;
                }

                System.Reflection.PropertyInfo DestProp = DestinationType.GetProperty(DestPropName);
                if (DestProp == null)
                {
                    continue;
                }
                //if ((!DestProp.PropertyType.FullName.StartsWith("System.")) && DataIgnore) continue;
                object v = SourceField.GetValue(Source);
                if (v != null)
                {
                    v = Convert.ChangeType(v, DestProp.PropertyType);
                }
                DestProp.SetValue(LinqDestination, v, null);
            }
        }
Esempio n. 2
0
        protected override void onCreate(Bundle savedInstanceState)
        {
            var activity = this;

            base.onCreate(savedInstanceState);

            var sv = new ScrollView(this);
            var ll = new LinearLayout(this);

            ll.setOrientation(LinearLayout.VERTICAL);
            sv.addView(ll);



            var t = typeof(MemoryFile);

            //var m = Activator.CreateInstance(t);

            //E/AndroidRuntime( 4217): Caused by: java.lang.InstantiationException: class android.os.MemoryFile has no zero argument constructor
            //E/AndroidRuntime( 4217):        at java.lang.Class.newInstance(Class.java:1641)
            //E/AndroidRuntime( 4217):        at ScriptCoreLibJava.BCLImplementation.System.__Activator.CreateInstance(__Activator.java:27)
            //E/AndroidRuntime( 4217):        ... 14 more
            //E/AndroidRuntime( 4217): Caused by: java.lang.NoSuchMethodException: <init> []
            //E/AndroidRuntime( 4217):        at java.lang.Class.getConstructor(Class.java:531)
            //E/AndroidRuntime( 4217):        at java.lang.Class.getDeclaredConstructor(Class.java:510)
            //E/AndroidRuntime( 4217):        at java.lang.Class.newInstance(Class.java:1639)
            //E/AndroidRuntime( 4217):        ... 15 more

            var m_descriptor = 0;
            var m_fd         = default(java.io.FileDescriptor);
            var pid          = android.os.Process.myPid();
            var uid          = android.os.Process.myUid();

            //try { m = new MemoryFile(default(string), 0); }
            try { m = new MemoryFile("name1", 0x07); }
            catch { throw; }

            try
            {
                m.writeBytes(
                    new byte[] { 7, 6, 5, 4, 3, 2, 1 }, 0, 0, 0x07
                    );
            }
            catch { }

            var buffer = new byte[0x07];

            try
            {
                m.readBytes(buffer, 0, 0, 0x07);
            }
            catch { }
            var buffer0 = buffer[0];

            //new Button(this).AttachTo(ll).WithText(new { t, m, buffer0 }.ToString());
            new Button(this).AttachTo(ll).WithText(new { buffer0 }.ToString());

            #region fields
            var fields = t.GetFields(
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
                );

            new Button(this).AttachTo(ll).WithText("fields: " + new { fields.Length }.ToString());

            fields.WithEach(
                SourceField =>
            {
                //E/AndroidRuntime( 9919): Caused by: java.lang.IllegalAccessException: Cannot access field: java.io.FileDescriptor android.os.MemoryFile.mFD
                //E/AndroidRuntime( 9919):        at java.lang.reflect.Field.get(Native Method)
                //E/AndroidRuntime( 9919):        at java.lang.reflect.Field.get(Field.java:279)
                //E/AndroidRuntime( 9919):        at ScriptCoreLibJava.BCLImplementation.System.Reflection.__FieldInfo.GetValue(__FieldInfo.java:46)

                var value = SourceField.GetValue(m);

                var xFileDescriptor = value as java.io.FileDescriptor;
                if (xFileDescriptor != null)
                {
                    m_fd = xFileDescriptor;

                    var xfields = typeof(java.io.FileDescriptor).GetFields(
                        System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
                        );

                    xfields.WithEach(
                        xFileDescriptor_SourceField =>
                    {
                        var xvalue = xFileDescriptor_SourceField.GetValue(value);

                        //if (xFileDescriptor_SourceField.FieldType == typeof(int))
                        if (xFileDescriptor_SourceField.Name == "descriptor")
                        {
                            m_descriptor = (int)xvalue;
                        }

                        new Button(this).AttachTo(ll).WithText(xFileDescriptor_SourceField + new { xvalue }.ToString());
                    }
                        );
                }
                else
                {
                    new Button(this).AttachTo(ll).WithText(new { SourceField, value }.ToString());
                }
            }
                );
            #endregion

            //var pfd = default(android.os.ParcelFileDescriptor);

            //try
            //{
            //    pfd = android.os.ParcelFileDescriptor.dup(
            //        m_fd
            //    );
            //}
            //catch
            //{
            //}

            new Button(activity).WithText("Next \n " + new
            {
                pid,
                uid
                //,
                //pfd = pfd.getFd(),
                //size = pfd.getStatSize()
            }).AttachTo(ll).AtClick(
                delegate
            {
                Intent intent = new Intent(activity, typeof(SecondaryActivity).ToClass());
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                // share scope
                intent.putExtra("m_descriptor", m_descriptor);
                intent.putExtra("pid", pid);

                //intent.putExtra("pfd", pfd);

                //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

                try
                {
                    Console.WriteLine("new LocalServerSocket");
                    var ss = new LocalServerSocket("MemoryFileDescriptor0");

                    // cached backgroun process?
                    // switching to another process.. easy...
                    activity.startActivity(intent);

                    Console.WriteLine("before LocalServerSocket accept");


                    // http://alvinalexander.com/java/jwarehouse/android/core/tests/coretests/src/android/net/LocalSocketTest.java.shtml
                    var ls = ss.accept();
                    Console.WriteLine("after LocalServerSocket accept");

                    ls.setFileDescriptorsForSend(new[] { m_fd });
                    ls.getOutputStream().write(42);
                }
                catch
                {
                    throw;
                }
            }
                );

            this.setContentView(sv);

            // https://developer.android.com/training/run-background-service/create-service.html
        }
Esempio n. 3
0
        protected override void onCreate(Bundle savedInstanceState)
        {
            // http://unix.stackexchange.com/questions/10050/proc-pid-fd-x-link-number
            // http://stackoverflow.com/questions/21955273/sharing-shared-memory-file-descriptors-across-android-app-processes-using-binder
            // http://permalink.gmane.org/gmane.comp.handhelds.android.porting/10904

            // http://www.mkyong.com/android/android-activity-from-one-screen-to-another-screen/
            // https://groups.google.com/forum/#!topic/android-ndk/sjIiMsLkHCM

            base.onCreate(savedInstanceState);


            var sv = new ScrollView(this);
            var ll = new LinearLayout(this);

            ll.setOrientation(LinearLayout.VERTICAL);
            sv.addView(ll);

            var activity = this;

            // does it work for us?
            var fs = default(java.io.FileDescriptor);

            try
            {
                var ls = new LocalSocket();
                ls.connect(new LocalSocketAddress("MemoryFileDescriptor0"));

                var i8 = ls.getInputStream().read();

                fs = ls.getAncillaryFileDescriptors().FirstOrDefault();

                new Button(this).AttachTo(ll).WithText(new { i8, fs }.ToString());


                //var memory0 = new FileInputStream(ls_fd).read();

                //new Button(this).AttachTo(ll).WithText(new { memory0 }.ToString());
            }
            catch { }

            var m = default(MemoryFile);

            //try { m = new MemoryFile(default(string), 0); }
            try { m = new MemoryFile("name1", 0x07); }
            catch { throw; }
            var m_descriptor = this.getIntent().getIntExtra("m_descriptor", 0);
            var parentpid    = this.getIntent().getIntExtra("pid", 0);

            //var pfd = (ParcelFileDescriptor)this.getIntent().getParcelableExtra("pfd");

            ////var fs = new java.io.FileDescriptor { };

            ////var xfields = typeof(java.io.FileDescriptor).GetFields(
            ////    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
            ////);

            ////xfields.WithEach(
            ////    xFileDescriptor_SourceField =>
            ////    {
            ////        var xvalue = xFileDescriptor_SourceField.GetValue(fs);

            ////        //if (xFileDescriptor_SourceField.FieldType == typeof(int))
            ////        if (xFileDescriptor_SourceField.Name == "descriptor")
            ////        {
            ////            xFileDescriptor_SourceField.SetValue(fs, m_descriptor);

            ////        }
            ////    }
            ////);



            // need to call native_mmap

            var t = typeof(MemoryFile);

            // internal static int native_mmap(java.io.FileDescriptor fd, int length, int mode);
            //var native_mmap = t.GetMethod("native_mmap", new[] {
            //        typeof(java.io.FileDescriptor),
            //        typeof(int),
            //        typeof(int)
            //    }
            //);

            //var methods = t.GetMethods(
            //    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static
            //);

            //var native_mmap = methods.FirstOrDefault(x => x.Name == "native_mmap");

            //#region time to patch it?

            //#endregion

            //var available = 0;
            var buffer = new byte[0x07];
            //X:\jsc.svn\examples\java\android\Test\TestNewByteArray7\TestNewByteArray7\Class1.cs
            //try { m.readBytes(buffer, 0, 0, 0x07); }
            //catch { }

            //try
            //{
            //    //buffer0 = new java.io.FileInputStream(fs).read();
            //    available = new java.io.FileInputStream(fs).available();
            //}
            //catch
            //{ }


            //var parentpidfd = "/proc/" + parentpid + "/fd/" + m_descriptor;

            //var parentpidfdx = global::System.IO.File.Exists(parentpidfd);

            //var parentpidf = new java.io.File(parentpidfd);


            //new Button(this).AttachTo(ll).WithText(new { parentpidfdx }.ToString());
            //new Button(this).AttachTo(ll).WithText(new { parentpidfd }.ToString());
            //new Button(this).AttachTo(ll).WithText(new { size = parentpidf.length() }.ToString());


            //var parentfd = ParcelFileDescriptor.open(parentpidf, ParcelFileDescriptor.MODE_READ_WRITE);

            var pid = android.os.Process.myPid();
            var uid = android.os.Process.myUid();

            this.setTitle(
                new
            {
                pid,
                uid

                //m_descriptor,
                //parentpid,
                ////pfg = pfd.getFd(),
                ////size = pfd.getStatSize()
                //parentpidfdx,
                //parentpidfd
            }.ToString()
                );


            //new Button(this).AttachTo(ll).WithText("methods: " + new
            //{
            //    methods.Length
            //    ,
            //    native_mmap
            //}.ToString());


            //methods.WithEach(
            //    SourceMethod =>
            //    {
            //        new Button(this).AttachTo(ll).WithText(new { SourceMethod }.ToString());
            //    }
            //);

            Action patch = delegate { };



            #region fields
            var fields = t.GetFields(
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
                );

            new Button(this).AttachTo(ll).WithText("fields: " + new { fields.Length }.ToString());

            fields.WithEach(
                SourceField =>
            {
                //E/AndroidRuntime( 9919): Caused by: java.lang.IllegalAccessException: Cannot access field: java.io.FileDescriptor android.os.MemoryFile.mFD
                //E/AndroidRuntime( 9919):        at java.lang.reflect.Field.get(Native Method)
                //E/AndroidRuntime( 9919):        at java.lang.reflect.Field.get(Field.java:279)
                //E/AndroidRuntime( 9919):        at ScriptCoreLibJava.BCLImplementation.System.Reflection.__FieldInfo.GetValue(__FieldInfo.java:46)

                var value = SourceField.GetValue(m);

                var xFileDescriptor = value as java.io.FileDescriptor;
                if (xFileDescriptor != null)
                {
                    var xfields = typeof(java.io.FileDescriptor).GetFields(
                        System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
                        );

                    xfields.WithEach(
                        xFileDescriptor_SourceField =>
                    {
                        var xvalue = xFileDescriptor_SourceField.GetValue(value);

                        //if (xFileDescriptor_SourceField.FieldType == typeof(int))
                        if (xFileDescriptor_SourceField.Name == "descriptor")
                        {
                            //m_descriptor = (int)xvalue;

                            patch = delegate
                            {
                                Console.WriteLine("enter patch " + new { SourceField });

                                //mAddress = native_mmap(mFD, length, modeToProt(mode));
                                //mOwnsRegion = false;

                                SourceField.SetValue(m, fs);
                                //xFileDescriptor_SourceField.SetValue(value, m_descriptor);
                                value  = SourceField.GetValue(m);
                                xvalue = xFileDescriptor_SourceField.GetValue(value);

                                var field_mAddress = fields.FirstOrDefault(xx => xx.Name == "mAddress");

                                Console.WriteLine("enter patch " + new { xvalue } +" invoke mmap");


                                var PROT_READ = 0x1;

                                //E/AndroidRuntime( 8047): Caused by: java.lang.IllegalAccessException: access to method denied
                                //E/AndroidRuntime( 8047):        at java.lang.reflect.Method.invokeNative(Native Method)
                                //E/AndroidRuntime( 8047):        at java.lang.reflect.Method.invoke(Method.java:507)
                                //E/AndroidRuntime( 8047):        at ScriptCoreLibJava.BCLImplementation.System.Reflection.__MethodInfo.InternalInvoke(__MethodInfo.java:93)

                                //I/System.Console( 8648): 21c8:0001 enter patch { field_mAddress = int mAddress } invoke native_mmap

                                //I/System.Console( 8936): 22e8:0001 GetFields { Length = 5, IsPublic = false, IsNonPublic = true, IsStatic = fal
                                //I/System.Console( 8936): 22e8:0001 enter patch
                                //I/System.Console( 8936): 22e8:0001 enter patch { field_mAddress = int mAddress, m_descriptor = 39 } invoke mmap
                                //I/System.Console( 8936): 22e8:0001 lib: libs/armeabi_v7a/libTestNDKAsAsset.so
                                //I/System.Console( 8936): 22e8:0001 loadLibrary: TestNDKAsAsset
                                //I/System.Console( 8936): 22e8:0001 exit patch { mAddress = -1 }

                                //var z = ScriptCoreLibNative.SystemHeaders.sys.mman_h.mmap(
                                //    null,
                                //    length,
                                //    PROT_READ | PROT_WRITE,
                                //    MAP_SHARED,
                                //    fd,
                                //    0
                                //    );

                                //                                        I/System.Console( 8016): 1f50:0001 enter patch { SourceField = java.io.FileDescriptor mFD }
                                //I/System.Console( 8016): 1f50:0001 enter patch { field_mAddress = long mAddress, xvalue = 32 } invoke mmap
                                //I/System.Console( 8016): 1f50:0001 lib: libs/armeabi_v7a/libTestNDKAsAsset.so
                                //I/System.Console( 8016): 1f50:0001 loadLibrary: TestNDKAsAsset
                                //I/System.Console( 8016): 1f50:0001 exit patch { mAddress = -1 }


                                //I/System.Console( 9792): 2640:0001 enter patch { SourceField = java.io.FileDescriptor mFD }
                                //I/System.Console( 9792): 2640:0001 enter patch { xvalue = 32 } invoke mmap
                                //I/xNativeActivity( 9792): x:\jsc.svn\examples\c\android\Test\TestNDKAsAsset\TestNDKAsAsset\Program.cs:139 mmap -1 errno: 13 Permission denied
                                //I/System.Console( 9792): 2640:0001 exit patch { mAddress = -1 }
                                //E/audit   ( 5152): type=1400 msg=audit(1433328685.131:566): avc:  denied  { mmap_zero } for  pid=9792 comm="Activities:foo1" scontext=u:r:untrusted_app:s0 tcontext=u:r:untrusted_app:s0 tclass=memprotect permissive=0
                                //E/audit   ( 5152):  SEPF_SM-G925F_5.0.2_0009
                                //E/audit   ( 5152): type=1300 msg=audit(1433328685.131:566): arch=40000028 syscall=192 success=no exit=-13 a0=0 a1=7 a2=3 a3=10 items=0 ppid=2962 ppcomm=main pid=9792 auid=4294967295 uid=10315 gid=10315 euid=10315 suid=10315 fsuid=10315 egid=10315 sgid=10315 fsgid=10315 ses=4294967295 tty=(none) comm="Activities:foo1" exe="/system/bin/app_process32" subj=u:r:untrusted_app:s0 key=(null)
                                //E/audit   ( 5152): type=1320 msg=audit(1433328685.131:566):
                                //D/SSRM:n  ( 3468): SIOP:: AP = 290, PST = 300, CP = 395, CUR = 268

                                // https://github.com/realm/realm-java/issues/1037

                                var mAddress = (int)
                                               // ???
                                               TestNDKAsAsset.xActivity.mmap(
                                    (int)xvalue,
                                    0x07
                                    );

                                // https://android.googlesource.com/platform/development/+/858086e/ndk/sources/android/libportable/arch-mips/mmap.c
                                // https://groups.google.com/forum/#!msg/android-ndk/tNYpTsHNQEY/8S7VS2j8f_8J

                                // jint result = (jint)mmap(NULL, length, prot, MAP_SHARED, fd, 0);

                                //native_mmap.Invoke(null,
                                //    new object[]
                                //                {
                                //                    value,
                                //                    0x07,
                                //                    PROT_READ
                                //                }
                                //);

                                Console.WriteLine("exit patch " + new { mAddress });

                                field_mAddress.SetValue(m, mAddress);

                                // http://stackoverflow.com/questions/8165216/what-is-the-use-of-memoryfile-in-android
                                // http://osdir.com/ml/Android-Developers/2013-01/msg00793.html
                            };
                        }

                        new Button(this).AttachTo(ll).WithText(xFileDescriptor_SourceField + new { xvalue }.ToString());
                    }
                        );
                }
                else
                {
                    new Button(this).AttachTo(ll).WithText(new { SourceField, value }.ToString());
                }
            }
                );
            #endregion

            new Button(activity).WithText("Patch n Read!").AttachTo(ll).AtClick(
                delegate
            {
                patch();
                //return;

                //this.finish();

                //E/AndroidRuntime( 7526): Caused by: java.lang.NoSuchMethodError: android.app.Activity.finishAndRemoveTask
                //E/AndroidRuntime( 7526):        at TestMultiProcMemoryFile.Activities.SecondaryActivity._onCreate_b__0(SecondaryActivity.java:66)

                //this.finishAndRemoveTask();

                //try { m.readBytes(buffer, 0, 0, 0x07); }
                //catch { }
                //var buffer0 = buffer[0];


                //this.setTitle(
                //     new { m_descriptor, buffer0 }.ToString()
                //);
            }
                );

            this.setContentView(sv);
        }
Esempio n. 4
0
        static Program()
        {
            // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2015/201503/20150328

            Console.WriteLine(typeof(object) + " Program prep for " + typeof(SharedProgram) + new { Thread.CurrentThread.ManagedThreadId });

            HopToCLR.VirtualOnCompleted =
                (Action continuation) =>
            {
                Console.WriteLine(typeof(object) + " enter HopToCLR " + new { Thread.CurrentThread.ManagedThreadId });

                //Task.Run(continuation);

                // we will never complete?
                // if we do, se have to jump? can we jump?

                // do we know how to pass forward our state?
                // continuation = {Method = {Void Run()}}

                //-		((System.Delegate)(continuation))._target	{System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner}	object {System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner}
                //+		m_context	{System.Threading.ExecutionContext}	System.Threading.ExecutionContext
                //-		m_stateMachine	{JVMCLRSwitchToCLRContextAsync.SharedProgram.Invoke}	System.Runtime.CompilerServices.IAsyncStateMachine {JVMCLRSwitchToCLRContextAsync.SharedProgram.Invoke}
                //-		[JVMCLRSwitchToCLRContextAsync.SharedProgram.Invoke]	{JVMCLRSwitchToCLRContextAsync.SharedProgram.Invoke}	JVMCLRSwitchToCLRContextAsync.SharedProgram.Invoke
                //        e	"hi"	string

                Console.WriteLine(new { continuation });
                Console.WriteLine(new { continuation.Method });
                Console.WriteLine(new { continuation.Target });

                // inspect target. can we reactivate it?

                //0001 0200000e JVMCLRSwitchToCLRContextAsync__i__d.jvm::JVMCLRSwitchToCLRContextAsync.SharedProgram+<Invoke>d__0



                var f = continuation.Target.GetType().GetFields(
                    System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
                    );


                var AsyncStateMachineSource = default(IAsyncStateMachine);
                var AsyncStateMachineType   = default(Type);
                var AsyncStateMachineFields = default(FieldInfo[]);

                var AsyncStateMachineStateField = default(FieldInfo);


                f.WithEach(
                    SourceField =>
                {
                    var SourceField_value = SourceField.GetValue(continuation.Target);
                    Console.WriteLine(new { SourceField, value = SourceField_value });

                    var m_stateMachine = SourceField_value as IAsyncStateMachine;
                    if (m_stateMachine != null)
                    {
                        AsyncStateMachineSource = m_stateMachine;
                        AsyncStateMachineType   = m_stateMachine.GetType();

                        AsyncStateMachineFields = AsyncStateMachineType.GetFields(
                            System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
                            );

                        AsyncStateMachineFields.WithEach(
                            AsyncStateMachineSourceField =>
                        {
                            var value = AsyncStateMachineSourceField.GetValue(m_stateMachine);

                            if (AsyncStateMachineSourceField.Name.EndsWith("1__state"))
                            {
                                AsyncStateMachineStateField = AsyncStateMachineSourceField;
                            }

                            Console.WriteLine(new { AsyncStateMachineSourceField, value });
                        }
                            );
                    }
                }
                    );

                // System.Object Program prep for JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
                //System.Object CLRProgram prep for JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
                //System.Object enter JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
                //System.Object enter HopToCLR { ManagedThreadId = 1 }
                //{ continuation = System.Action }
                //{ Method = Void Run() }
                //{ Target = System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner }
                //{ SourceField = System.Threading.ExecutionContext m_context, value = System.Threading.ExecutionContext }
                //{ SourceField = System.Runtime.CompilerServices.IAsyncStateMachine m_stateMachine, value = JVMCLRSwitchToCLRContextAsync.SharedProgram+<Invoke>d__0 }
                //{ AsyncStateMachineSourceField = Int32 <>1__state, value = 0 }
                //{ AsyncStateMachineSourceField = System.Runtime.CompilerServices.AsyncTaskMethodBuilder <>t__builder, value = System.Runtime.CompilerServices.AsyncTaskMethodBuilder }
                //{ AsyncStateMachineSourceField = System.String e, value = hi }
                //{ AsyncStateMachineSourceField = JVMCLRSwitchToCLRContextAsync.HopToCLR <>u__$awaiter1, value = JVMCLRSwitchToCLRContextAsync.HopToCLR }
                //{ AsyncStateMachineSourceField = System.Object <>t__stack, value =  }
                //{ AsyncStateMachineSourceField = JVMCLRSwitchToCLRContextAsync.HopToThreadPoolAwaitable <>u__$awaiter2, value = JVMCLRSwitchToCLRContextAsync.HopToThreadPoolAwaitable }
                //System.Object CLRInvoke { ManagedThreadId = 1 }

                //			System.Object Program prep for JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
                //System.Object CLRProgram prep for JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
                //System.Object enter JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
                //System.Object enter HopToCLR { ManagedThreadId = 1 }
                //{ continuation = System.Action }
                //{ Method = Void Run() }
                //{ Target = System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner }
                //{ SourceField = System.Threading.ExecutionContext m_context, value = System.Threading.ExecutionContext }
                //{ SourceField = System.Runtime.CompilerServices.IAsyncStateMachine m_stateMachine, value = JVMCLRSwitchToCLRContextAsync.SharedProgram+<Invoke>d__0 }
                //{ AsyncStateMachineSourceField = Int32 <>1__state, value = 0 }
                //{ AsyncStateMachineSourceField = System.Runtime.CompilerServices.AsyncTaskMethodBuilder <>t__builder, value = System.Runtime.CompilerServices.AsyncTaskMethodBuilder }
                //{ AsyncStateMachineSourceField = System.String e, value = hi }
                //{ AsyncStateMachineSourceField = JVMCLRSwitchToCLRContextAsync.HopToCLR <>u__1, value = JVMCLRSwitchToCLRContextAsync.HopToCLR }
                //{ AsyncStateMachineSourceField = JVMCLRSwitchToCLRContextAsync.HopToThreadPoolAwaitable <>u__2, value = JVMCLRSwitchToCLRContextAsync.HopToThreadPoolAwaitable }
                //System.Object CLRInvoke { ManagedThreadId = 1 }


                //-Activator.CreateInstance(AsyncStateMachineType) { JVMCLRSwitchToCLRContextAsync.SharedProgram.< Invoke > d__0}
                //object { JVMCLRSwitchToCLRContextAsync.SharedProgram.< Invoke > d__0}
                //e   null    string

                // how can we send the type ref over the wire? encypt it?


                //var NewStateMachine = Activator.CreateInstance(AsyncStateMachineType);
                //var NewStateMachineI = NewStateMachine as IAsyncStateMachine;

                // this will take step into next await.
                //NewStateMachineI.MoveNext();


                //CLRProgram.CLRInvoke(
                var ShadowIAsyncStateMachine = Program.CLRInvoke(
                    new ShadowIAsyncStateMachine
                {
                    TypeName = AsyncStateMachineType.FullName,
                    state    = (int)AsyncStateMachineStateField.GetValue(AsyncStateMachineSource)
                }
                    );

                Console.WriteLine("can we skip a step? " + new { AsyncStateMachineStateField, ShadowIAsyncStateMachine.state, AsyncStateMachineSource });

                //time to jump back? yes
                //{ continuation = System.Action }
                //{ Method = Void Run() }
                //{ Target = System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner }
                //{ SourceField = System.Threading.ExecutionContext m_context, value = System.Threading.ExecutionContext }
                //{ SourceField = System.Runtime.CompilerServices.IAsyncStateMachine m_stateMachine, value = JVMCLRSwitchToCLRContextAsync.SharedProgram+<Invoke>d__0 }
                //{ AsyncStateMachineSourceField = Int32 <>1__state, value = 1 }
                //{ AsyncStateMachineSourceField = System.Runtime.CompilerServices.AsyncTaskMethodBuilder <>t__builder, value = System.Runtime.CompilerServices.AsyncTaskMethodBuilder }
                //{ AsyncStateMachineSourceField = System.String e, value =  }
                //{ AsyncStateMachineSourceField = JVMCLRSwitchToCLRContextAsync.HopToCLR <>u__$awaiter1, value = JVMCLRSwitchToCLRContextAsync.HopToCLR }
                //{ AsyncStateMachineSourceField = System.Object <>t__stack, value =  }
                //{ AsyncStateMachineSourceField = JVMCLRSwitchToCLRContextAsync.HopToJVM <>u__$awaiter2, value = JVMCLRSwitchToCLRContextAsync.HopToJVM }
                //time to jump back? { state = 1 }
                //                   time to jump back? { state = 1 }
                //can we skip a step? { AsyncStateMachineStateField = int __1__state, state = 1, AsyncStateMachineSource = JVMCLRSwitchToCLRContextAsync.SharedProgram__Invoke_d__0@4437c4 }
                // enter catch { mname = <01a8> ldloca.s.try } ClauseCatchLocal:

                //__AsyncTaskMethodBuilder.SetException { exception =  }
                //enter catch { mname = <00a8> ldarg.0.try } ClauseCatchLocal:

                //__AsyncTaskMethodBuilder.SetException { exception =  }
                //{ Message = System.Diagnostics.Debugger.Break, StackTrace = java.lang.RuntimeException: System.Diagnostics.Debugger.Break
                //        at ScriptCoreLibJava.BCLImplementation.System.Diagnostics.__Debugger.Break(__Debugger.java:32)
                //        at ScriptCoreLib.Shared.BCLImplementation.System.Runtime.CompilerServices.__AsyncTaskMethodBuilder.SetException(__AsyncTaskMethodBuilder.java:58)
                //        at JVMCLRSwitchToCLRContextAsync.SharedProgram__Invoke_d__0__MoveNext_06000055._0214__stloc_1(SharedProgram__Invoke_d__0__MoveNext_06000055.java:253)
                //        at JVMCLRSwitchToCLRContextAsync.SharedProgram__Invoke_d__0__MoveNext_06000055._00a8__ldarg_0(SharedProgram__Invoke_d__0__MoveNext_06000055.java:425)
                //        at JVMCLRSwitchToCLRContextAsync.SharedProgram__Invoke_d__0__MoveNext_06000055.__workflow(SharedProgram__Invoke_d__0__MoveNext_06000055.java:81)
                //        at JVMCLRSwitchToCLRContextAsync.SharedProgram__Invoke_d__0__MoveNext_06000055.__forwardref(SharedProgram__Invoke_d__0__MoveNext_06000055.java:49)
                //        at JVMCLRSwitchToCLRContextAsync.SharedProgram__Invoke_d__0.MoveNext(SharedProgram__Invoke_d__0.java:34)
                //        at JVMCLRSwitchToCLRContextAsync.SharedProgram__Invoke_d__0.System_Runtime_CompilerServices_IAsyncStateMachine_MoveNext(SharedProgram__Invoke_d__0.java:51)
                //        at ScriptCoreLib.Shared.BCLImplementation.System.Runtime.CompilerServices.__AsyncTaskMethodBuilder.Start(__AsyncTaskMethodBuilder.java:43)
                //        at JVMCLRSwitchToCLRContextAsync.SharedProgram.Invoke(SharedProgram.java:36)
                //        at JVMCLRSwitchToCLRContextAsync.Program.main(Program.java:128)
                // }



                ////var NewStateMachine = Activator.CreateInstance(AsyncStateMachineType);
                ////var NewStateMachineI = NewStateMachine as IAsyncStateMachine;


                ////AsyncStateMachineStateField.SetValue(NewStateMachine, ShadowIAsyncStateMachine.state);

                //////this will take step into next await.
                ////NewStateMachineI.MoveNext();

                AsyncStateMachineStateField.SetValue(AsyncStateMachineSource, ShadowIAsyncStateMachine.state);
                AsyncStateMachineSource.MoveNext();

                // will it be the same in roslyn?
                // can we jump back in the future?


                //java.lang.Object Program prep for JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
                //System.Object CLRProgram prep for JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 3 }
                //java.lang.Object enter JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
                //java.lang.Object enter HopToCLR { ManagedThreadId = 1 }
                //{ continuation = ScriptCoreLib.Shared.BCLImplementation.System.__Action@123ec81 }
                //{ Method = void _AwaitUnsafeOnCompleted_b__1() }
                //{ Target = ScriptCoreLib.Shared.BCLImplementation.System.Runtime.CompilerServices.__AsyncTaskMethodBuilder___c__DisplayClass2_2@f87f48 }
                //{ SourceField = ScriptCoreLib.Shared.BCLImplementation.System.Runtime.CompilerServices.__IAsyncStateMachine zstateMachine, value = JVMCLRSwitchToCLRContextAsync.SharedProgram__Invoke_d__0@1405ef7 }
                //{ AsyncStateMachineSourceField = java.lang.String e, value = hi }
                //{ AsyncStateMachineSourceField = ScriptCoreLib.Shared.BCLImplementation.System.Runtime.CompilerServices.__AsyncTaskMethodBuilder __t__builder, value = ScriptCoreLib.Shared.BCLImplementation.System.Runtime.CompilerServices.__AsyncTaskMethodBuilder@1bba400 }
                //{ AsyncStateMachineSourceField = int __1__state, value = 0 }
                //{ AsyncStateMachineSourceField = JVMCLRSwitchToCLRContextAsync.HopToCLR __u___awaiter1, value = JVMCLRSwitchToCLRContextAsync.HopToCLR@19d3b25 }
                //{ AsyncStateMachineSourceField = java.lang.Object __t__stack, value =  }
                //{ AsyncStateMachineSourceField = JVMCLRSwitchToCLRContextAsync.HopToThreadPoolAwaitable __u___awaiter2, value =  }
                //{ SourceField = ScriptCoreLib.Shared.BCLImplementation.System.__Action yield, value = ScriptCoreLib.Shared.BCLImplementation.System.__Action@14989ff }
                //java.lang.Object CLRProgram prep for JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
                //java.lang.Object CLRInvoke { ManagedThreadId = 1 }



                // whats the _AwaitUnsafeOnCompleted_b__1 ?

                // both have __IAsyncStateMachine, but CLR has ExecutionContext, while others have .__Action yield
                // shall we test on roslyn, then inspect that __IAsyncStateMachine?

                //CLRProgram.CLRInvoke();
            };
        }
Esempio n. 5
0
        public static ShadowIAsyncStateMachine CLRInvoke(
            ShadowIAsyncStateMachine that
            )
        {
            var xAsyncStateMachineTypeName = that.TypeName;

            Console.WriteLine(typeof(object) + " CLRInvoke "
                              //+ typeof(SharedProgram)
                              + new
            {
                Thread.CurrentThread.ManagedThreadId,

                that.TypeName,
                that.state
            });

            // do we have the type mentioned loaded?

            //var NewStateMachine = Activator.CreateInstance(AsyncStateMachineType);
            //var NewStateMachineI = NewStateMachine as IAsyncStateMachine;

            //// this will take step into next await.
            ////NewStateMachineI.MoveNext();


            //java.lang.Object Program prep for JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
            //java.lang.Object enter JVMCLRSwitchToCLRContextAsync.SharedProgram{ ManagedThreadId = 1 }
            //java.lang.Object enter HopToCLR { ManagedThreadId = 1 }
            //{ continuation = ScriptCoreLib.Shared.BCLImplementation.System.__Action@123ec81 }
            //{ Method = void _AwaitUnsafeOnCompleted_b__1() }
            //{ Target = ScriptCoreLib.Shared.BCLImplementation.System.Runtime.CompilerServices.__AsyncTaskMethodBuilder___c__DisplayClass2_2@f87f48 }
            //{ SourceField = ScriptCoreLib.Shared.BCLImplementation.System.Runtime.CompilerServices.__IAsyncStateMachine zstateMachine, value = JVMCLRSwitchToCLRContextAsync.SharedProgram__Invoke_d__0@1405ef7 }
            //{ AsyncStateMachineSourceField = java.lang.String e, value = hi }
            //{ AsyncStateMachineSourceField = ScriptCoreLib.Shared.BCLImplementation.System.Runtime.CompilerServices.__AsyncTaskMethodBuilder __t__builder, value = ScriptCoreLib.Shared.BCLImplementation.System.Runtime.CompilerServices.__AsyncTaskMethodBuilder@1bba400 }
            //{ AsyncStateMachineSourceField = int __1__state, value = 0 }
            //{ AsyncStateMachineSourceField = JVMCLRSwitchToCLRContextAsync.HopToCLR __u___awaiter1, value = JVMCLRSwitchToCLRContextAsync.HopToCLR@19d3b25 }
            //{ AsyncStateMachineSourceField = java.lang.Object __t__stack, value =  }
            //{ AsyncStateMachineSourceField = JVMCLRSwitchToCLRContextAsync.HopToThreadPoolAwaitable __u___awaiter2, value =  }
            //{ SourceField = ScriptCoreLib.Shared.BCLImplementation.System.__Action yield, value = ScriptCoreLib.Shared.BCLImplementation.System.__Action@14989ff }

            //System.Object CLRInvoke { ManagedThreadId = 3, AsyncStateMachineTypeName = JVMCLRSwitchToCLRContextAsync.SharedProgram__Invoke_d__0 }

            //{ SourceField = System.Runtime.CompilerServices.IAsyncStateMachine m_stateMachine, value = JVMCLRSwitchToCLRContextAsync.SharedProgram+<Invoke>d__0 }
            // should java keep the actual type name as an attribute, like we have displayName

            Console.WriteLine("looking for the type...");

            var xAsyncStateMachineType = typeof(CLRProgram).Assembly.GetTypes().FirstOrDefault(
                x =>
            {
                //Console.WriteLine(new { x.FullName });

                return(x.FullName.Replace("+", "_").Replace("<", "_").Replace(">", "_")
                       == xAsyncStateMachineTypeName.Replace("+", "_").Replace("<", "_").Replace(">", "_"));
            }
                );

            //looking for the type...
            //{ FullName = JVMCLRSwitchToCLRContextAsync.Program }
            //{ FullName = <module>.SHA12fc19b38aba0098236ba7fd654b62facbfa4969b@1331799119 }
            //{ FullName = ScriptCoreLib.Desktop.JVM.JVMLauncher }
            //{ FullName = <module>.SHA1e4eb4631b3cf3610d48914d2f4deba4581c74282@275784628$00000006 }
            //{ FullName =  .  }
            //{ FullName =  .  }
            //{ FullName =  .  }
            //{ FullName = ScriptCoreLib.Desktop.JVM.__JVMLauncherInvoke }
            //{ FullName = ScriptCoreLib.Desktop.JVM.__InternalGetEntryPoint }
            //{ FullName = ScriptCoreLib.Desktop.JVM.__InternalGetEntryPoint+    }
            //{ FullName =  .  }
            //{ FullName =  .  }
            //{ FullName =  .? }
            //{ FullName =  .   }
            //{ FullName =  .   }
            //{ FullName =  .   }
            //{ FullName =  .   }
            //{ FullName =  .   }
            //{ FullName =  .   }
            //{ FullName =  .   }
            //{ FullName =  .?  }
            //{ FullName =  .   }
            //{ FullName =  .   }
            //{ FullName = JVMCLRSwitchToCLRContextAsync__i.<02000018>\\\\\\\+export }
            //{ FullName = <module>.SHA14908e43b60b15906983f15d5126d55d794b08a03@516876350$0000001e }
            //{ FullName = JVMCLRSwitchToCLRContextAsync.<02000006>\\\\\\\\\\\\\\\+<interfaceexport> }
            //{ FullName = JVMCLRSwitchToCLRContextAsync.CLRProgram }
            //{ FullName = JVMCLRSwitchToCLRContextAsync.CLRProgram+<>c__DisplayClass1 }
            //{ FullName = <>f__AnonymousType$34$$30$$29$$28$7`1 }
            //{ FullName = ScriptCoreLib.Desktop.AppDomainAssemblyResolve }
            //{ FullName = <module>.SHA1ece49800bcc87751341b0db5b5d43bab4e1973d0@1209331862$00000025 }
            //{ FullName = ScriptCoreLib.Library.StringConversions }
            //{ FullName = <module>.SHA17c927d0ef51414d5913d986a2a0ad52032747944@1023654678$00000027 }
            //{ FullName = ScriptCoreLib.Interop.IntPtrInfo }
            //{ FullName = <module>.SHA19ab5a45e1768fb14172603268fe7f44ab10839ae@1329949876$0000002a$0000002e$00000029 }
            //{ FullName = <>f__AnonymousType$42$$53$$67$$65$6`2 }
            //{ FullName = <>f__AnonymousType$45$$65$$70$$68$8`2 }
            //{ FullName = <module>.SHA111aec3ca07ebcd71f8efa6a7eb37c94e05d294eb@2139898218$00000047$00000030 }
            //{ FullName = <module>.SHA1d04c4f386a94a00ca9ac06112eefc7d4456dc8c4@30829592 }
            //{ FullName = <ExportDirectoryBridge> }

            Console.WriteLine(typeof(object) + " CLRInvoke " + new
            {
                Thread.CurrentThread.ManagedThreadId,

                xAsyncStateMachineType
            });



            // how can we send the type ref over the wire? encypt it?
            var NewStateMachine  = Activator.CreateInstance(xAsyncStateMachineType);
            var NewStateMachineI = NewStateMachine as IAsyncStateMachine;

            #region 1__state
            xAsyncStateMachineType.GetFields(
                System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
                ).WithEach(
                AsyncStateMachineSourceField =>
            {
                if (AsyncStateMachineSourceField.Name.EndsWith("1__state"))
                {
                    AsyncStateMachineSourceField.SetValue(
                        NewStateMachineI,
                        that.state
                        );
                }
            }
                );
            #endregion



            var ShadowIAsyncStateMachine = new ShadowIAsyncStateMachine();

            var reset = new AutoResetEvent(false);

            HopToJVM.VirtualOnCompleted =
                (Action continuation) =>
            {
                Console.WriteLine("time to jump back? yes");

                Console.WriteLine(new { continuation });
                Console.WriteLine(new { continuation.Method });
                Console.WriteLine(new { continuation.Target });

                // inspect target. can we reactivate it?

                //0001 0200000e JVMCLRSwitchToCLRContextAsync__i__d.jvm::JVMCLRSwitchToCLRContextAsync.SharedProgram+<Invoke>d__0



                var f = continuation.Target.GetType().GetFields(
                    System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
                    );


                var AsyncStateMachineSource = default(IAsyncStateMachine);
                var AsyncStateMachineType   = default(Type);
                var AsyncStateMachineFields = default(FieldInfo[]);

                // { AsyncStateMachineSourceField = Int32 <>1__state, value = 1 }

                f.WithEach(
                    SourceField =>
                {
                    var SourceField_value = SourceField.GetValue(continuation.Target);
                    Console.WriteLine(new { SourceField, value = SourceField_value });

                    var m_stateMachine = SourceField_value as IAsyncStateMachine;
                    if (m_stateMachine != null)
                    {
                        AsyncStateMachineSource = m_stateMachine;
                        AsyncStateMachineType   = m_stateMachine.GetType();

                        AsyncStateMachineFields = AsyncStateMachineType.GetFields(
                            System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
                            );

                        AsyncStateMachineFields.WithEach(
                            AsyncStateMachineSourceField =>
                        {
                            var value = AsyncStateMachineSourceField.GetValue(m_stateMachine);

                            if (AsyncStateMachineSourceField.Name.EndsWith("1__state"))
                            {
                                ShadowIAsyncStateMachine.state = (int)value;
                            }

                            Console.WriteLine(new { AsyncStateMachineSourceField, value });
                        }
                            );
                    }
                }
                    );

                reset.Set();
            };

            new Thread(
                delegate()
            {
                // this will take step into next await.
                NewStateMachineI.MoveNext();
            }
                ).Start();


            reset.WaitOne();

            Console.WriteLine("time to jump back? " + new { ShadowIAsyncStateMachine.state });

            return(ShadowIAsyncStateMachine);
        }