Warning: Trying to access array offset on value of type null in phar://.../vb/vb.phar/bbcode/url.php on line 2 Reflection.Emit - Flash Flash Revolution

Reflection.Emit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xXOpkillerXx
    Forever OP
    FFR Simfile Author
    • Dec 2008
    • 4207

    #1

    Reflection.Emit

    Give me your thoughts on it please. I'm currently studying it on my own for a project of mine and just wanted to hear more about what people think of it.

    OpCodes is giving me some trouble since there is so many damn options and I'm not so good at memory management and such low-level skills.

    Also, I've heard Expression.Compile is great too but not as versatile and it's limited by lambda usage. Yes/no ? In what context to pick one or the other ?

    Thanks
  • kommisar
    Dark Chancellor
    FFR Simfile Author
    FFR Music Producer
    • Jun 2005
    • 7324

    #2
    Re: Reflection.Emit

    are there more versatile or useful classes you can use for the same function or is this as good as it gets
    Last edited by kommisar; 10-15-2016, 04:41 PM.

    Comment

    • Dinglesberry
      longing
      • Dec 2007
      • 2679

      #3
      Re: Reflection.Emit

      Wow I was writing a post and my ipad deleted it before I submitted :(

      I'm not too familiar with it in c#, but reflection is pretty useful in other languages.. Basically, it allows for you to dynamically create functions, objects etc, and create them or change them during execution, and the program will be able to interact with them as though they were native parts of the code. It also allows you to expose certain elements to external code. A pretty solid practical example of this is runescape bots - imagine rewriting the method that displays the game itself to also output data on objects that are appearing in the world, such as their coordinates.

      On the topic of expression compile.. I am predicting one benefit of not using it is because of how arbitrary it could be.. You wouldn't want someone to be able to arbitrarily add code, so perhaps using other methods allows for things such as ensuring it is valid, ensuring it comes from a legit source etc..

      I'm reading more on this IS stuff.. It seems like the idea is to have code that is system and code independant, hence why you'd be writing op codes and manually managing the stack, that way it's irrelevant which language executes your IS packag or where it's run.

      Regarding the op code stuff, you are basically manually managing a stack.. For example if you want to print letters, you can't just call a print statement, as that would be platform dependant.. Instead, you need to manually place the characters on the stack, then pop them to the output buffer etc.. How familiar are you with data structures and basic assembly stuff (things like registers, subroutines, traps etc).. I'd recommend researching some assembly, how it works in the CPU etc... I'm imagine since you are trying reflection you are familiar with this stuff already though :p

      From what I can see, reflection.emit is just an abstraction of writing this op code stuff manually, essentially it converts to machine code anyways... Or maybe I'm just talking out my ass I know literally nothing ;D

      Comment

      • xXOpkillerXx
        Forever OP
        FFR Simfile Author
        • Dec 2008
        • 4207

        #4
        Re: Reflection.Emit

        Thanks for the reply!

        Well I'm definitely thinking about writing some kind of utility class that will ensure the validity of the stack manipulations cuz I don't see any way to make sure you always use the right opcodes -.-
        I'm not sure how you'd use reflection (.emit at least) to manipulate runescape UI unless you actually have some kind of access to its source code or something (Idk runescape much lol), since that'd be cross-domain level and well there's most likely some security there. In any case I'm not that good at it yet.

        I was thinking about starting some program that'd basically take text (that you'd write on UI) and convert that into code, then run it live. Ofc I would only be implementing the basic structures such as simple types (primitives only first), no delegates, maybe a few simple events (??), etc.

        Comment

        • Soundwave-
          Carry your failures proud
          • Sep 2015
          • 644

          #5
          Re: Reflection.Emit

          I think the main concern with Reflection.Emit is that it has a narrow use case.

          If you need to write IL code for some reason, with the only use pretty much being a compiler or generator of some description, then Reflection.Emit is great, because it lowers the complexity required to write IL code dynamically.

          The other conceivable use case of writing IL code reflects the use of similar constructs in Java: modifying a third-party method at runtime. Unfortunately, C# makes this range from "you really, REALLY, shouldn't be doing this what so ever" to impossible, depending on the exact use case. A shame really.

          Expression.Compile seems to be (I probably should have mentioned I'm not qualified to speak definitively on this at all) a shortcut for writing IL should you already have C# expressions. Examples of when this would be the case are writing a C# compiler in C#, or when you are generating code based on some higher level parameters, and it'd be easier to just write it in C# expressions as compared to writing the IL yourself.

          In terms of Expression.Compile vs Reflection.Emit, there doesn't seem to be anything indicating to me that either one can generate something that is functionally different from the other, but Expression.Compile guarantees something that functions a certain way where as Reflection.Emit guarantees something that *is* a certain way, which if you're writing IL code for say, performance reasons, could be of interest.
          Originally posted by [11:38 PM] Hakulyte
          only person who can legit tilt me is like YoshL
          Originally posted by スンファンさん
          右に3回回らない限り間違います。

          Comment

          • Dinglesberry
            longing
            • Dec 2007
            • 2679

            #6
            Re: Reflection.Emit

            Op, if I'm correct from what I can see, this seems like its somewhat similar to what you want to do https://github.com/Ineedajob/RSBot/t...bot/loader/asm

            It sounds like you are trying to write an interpreter for plaintext to code? Better start reading about abstract syntax trees :p

            The bots work because regardless of the source code or not, java runs on its own virtual machine which I'd imagine is being analyzed, unsure exactly how it works though :(
            Last edited by Dinglesberry; 10-15-2016, 05:40 PM.

            Comment

            • xXOpkillerXx
              Forever OP
              FFR Simfile Author
              • Dec 2008
              • 4207

              #7
              Re: Reflection.Emit

              Originally posted by Soundwave-
              Unfortunately, C# makes this range from "you really, REALLY, shouldn't be doing this what so ever" to impossible, depending on the exact use case. A shame really.
              Thanks for the reply,

              I'm curious to see if you'd have any examples to support this quote ? If that's really the case then I might give it a try in Java since I have no problem using it.

              Comment

              • Dinglesberry
                longing
                • Dec 2007
                • 2679

                #8
                Re: Reflection.Emit

                Might be interesting reading, seems like it is more general than about a specific language

                Comment

                • xXOpkillerXx
                  Forever OP
                  FFR Simfile Author
                  • Dec 2008
                  • 4207

                  #9
                  Re: Reflection.Emit

                  Originally posted by Dinglesberry
                  Op, if I'm correct from what I can see, this seems like its somewhat similar to what you want to do https://github.com/Ineedajob/RSBot/t...bot/loader/asm

                  It sounds like you are trying to write an interpreter for plaintext to code? Better start reading about abstract syntax trees :p

                  The bots work because regardless of the source code or not, java runs on its own virtual machine which I'd imagine is being analyzed, unsure exactly how it works though :(
                  Hmmm yeah, after a quick search it seems like there are programs/libraries to intercept the JVM's instructions. Pretty neat (I guess haha)

                  As for the git repo I might read that a little bit but I'm sticking with c# if there's a possibility I can get it done with that. :)

                  Originally posted by Dinglesberry
                  Might be interesting reading, seems like it is more general than about a specific language

                  https://ruslanspivak.com/lsbasi-part1/
                  Thanks but no haha, I'm not writting a full on interpreter, got no time to think about all those syntax rules. I know what that requires and I'm not so comfortable with the tasks to accomplish. :p
                  Last edited by xXOpkillerXx; 10-15-2016, 05:55 PM.

                  Comment

                  • Soundwave-
                    Carry your failures proud
                    • Sep 2015
                    • 644

                    #10
                    Re: Reflection.Emit

                    Originally posted by xXOpkillerXx
                    Thanks for the reply,

                    I'm curious to see if you'd have any examples to support this quote ? If that's really the case then I might give it a try in Java since I have no problem using it.
                    See:

                    What I want to do is change how a C# method executes when it is called, so that I can write something like this: [Distributed] public DTask<bool> Solve(int n, DEvent<bool> callback) { ...


                    Just by doing a bit of pointer work you can replace a "vanilla" method with another "vanilla" method, and there's a link there to really hack it up for other cases. Purportedly it works but it involves native level stuff, and only works on .NET. I couldn't follow it very well myself.

                    On the other hand, Java lets you really dig into the internals of code loading, making dynamic modification portable and (relatively) easy.

                    EDIT: It should be noted that both C# and Java suffer from a similar problem in this regard: A launch point. Any loaded code in both cases will not be updated. In C# this is a massive problem because there's no easy way to reload the code that's already loaded, or ensure that you can modify the code before it's loaded. In Java you have multiple options. One is to launch a victim program from another Java program, choosing your custom loading code along the way, or modify the victim program with a class that when loaded, all it does is reload the entire program using custom loading code on a new thread, and kills the current thread.
                    Last edited by Soundwave-; 10-15-2016, 09:15 PM.
                    Originally posted by [11:38 PM] Hakulyte
                    only person who can legit tilt me is like YoshL
                    Originally posted by スンファンさん
                    右に3回回らない限り間違います。

                    Comment

                    • xXOpkillerXx
                      Forever OP
                      FFR Simfile Author
                      • Dec 2008
                      • 4207

                      #11
                      Re: Reflection.Emit

                      ?? Not sure if I'm just lacking the knowledge here or not fully understanding the issue you're pointing out, but you can inject a Main(args) easily and run it. Please clarify/explain to me if I'm not getting what you mean. ^^

                      Comment

                      • Soundwave-
                        Carry your failures proud
                        • Sep 2015
                        • 644

                        #12
                        Re: Reflection.Emit

                        If you're modifying a third-party assembly then your code needs to be ran at some point, requiring that you either be entirely in control of the execution of the assembly or that your code has already been incorporated somehow.
                        Originally posted by [11:38 PM] Hakulyte
                        only person who can legit tilt me is like YoshL
                        Originally posted by スンファンさん
                        右に3回回らない限り間違います。

                        Comment

                        • xXOpkillerXx
                          Forever OP
                          FFR Simfile Author
                          • Dec 2008
                          • 4207

                          #13
                          Re: Reflection.Emit

                          Originally posted by Soundwave-
                          If you're modifying a third-party assembly then your code needs to be ran at some point, requiring that you either be entirely in control of the execution of the assembly or that your code has already been incorporated somehow.
                          Oh yeah absolutely. But that's no flaw to me; if a third party code can be easily modified at runtime it's just a major security flaw if you ask me lmao. But yeah, good thing I only want to modify my own code with reflection. I'll spend some time on it today, I'll let you guys know if I get anything nice ^^

                          Comment

                          • Soundwave-
                            Carry your failures proud
                            • Sep 2015
                            • 644

                            #14
                            Re: Reflection.Emit

                            Originally posted by xXOpkillerXx
                            Oh yeah absolutely. But that's no flaw to me; if a third party code can be easily modified at runtime it's just a major security flaw if you ask me lmao. But yeah, good thing I only want to modify my own code with reflection. I'll spend some time on it today, I'll let you guys know if I get anything nice ^^
                            Well, if you design your code to be modified, both Java and C# have great options for dynamic code. Reflection.Emit allows you to emit to dynamic reflection objects, where as Java let's you be the middle-man in loading, letting you put whatever you want in as well, although .NET seems to have more framework already laid out for you, if you don't want to learn JBC.
                            Originally posted by [11:38 PM] Hakulyte
                            only person who can legit tilt me is like YoshL
                            Originally posted by スンファンさん
                            右に3回回らない限り間違います。

                            Comment

                            • xXOpkillerXx
                              Forever OP
                              FFR Simfile Author
                              • Dec 2008
                              • 4207

                              #15
                              Re: Reflection.Emit

                              Originally posted by Soundwave-
                              Well, if you design your code to be modified, both Java and C# have great options for dynamic code. Reflection.Emit allows you to emit to dynamic reflection objects, where as Java let's you be the middle-man in loading, letting you put whatever you want in as well, although .NET seems to have more framework already laid out for you, if you don't want to learn JBC.
                              Yeah I'm using ILDASM and it makes it feel like a piece of cake. The long work will be to either map disassembled class dumps words to OpCodes/Builders or actually do some interpreter work (Sadly I don't think I can fully avoid/automate parsing of new written code). Owell, got work on my hands, job+school are time consuming so I might not progress quickly. Will keep this updated when I do.

                              Comment

                              Working...
                                Notice: Function utf8_encode() is deprecated in phar://.../vb/vb.phar/mail/transport/legacy.php on line 2 Notice: Function utf8_encode() is deprecated in phar://.../vb/vb.phar/mail/transport/legacy.php on line 2 Warning: Undefined variable $username in phar://.../vb/vb.phar/mail/transport/legacy.php on line 2 Notice: Function utf8_encode() is deprecated in phar://.../vb/vb.phar/mail/transport/legacy.php on line 2 Notice: utf8_encode(): Passing null to parameter #1 ($string) of type string is deprecated in phar://.../vb/vb.phar/mail/transport/legacy.php on line 2 Notice: Function utf8_encode() is deprecated in phar://.../vb/vb.phar/mail/transport/legacy.php on line 2