Thread: Copying WHOLE Functions

  1. #1
    Speedy5
    Guest

    Question Copying WHOLE Functions

    I need help! What I want to do is to copy and paste the code of fucntions into an array of some sort and execute, through function pointers, the new code inside the array. (I'm not a hacker, btw). I also want to remove the return statements when copying the functions so I can add others. For example:

    void a()
    {
    printf("hello");
    }
    voit b()
    {
    print(" world\n");
    }

    I want in a char array:
    { code for a minus the return statement, code for b normally }

    Then with a function pointer to that array, I can hopefully execute the new combined code.

    So say we have:

    void m()
    {
    printf("HELLO!\n");
    }
    void f() { }

    .....
    // no casting is done here for reading's sake
    char *s=new char[1000];
    memcpy(s, &m, &f-&m);
    void (*go)(void)=s;
    go();
    // prints out "HELLO"
    ...


    I would want to do all this in a class too (like the functions being member functions).

    HELP ME! HOW TO DO?

    I bet you advanced coders know how to do all this, LOL!

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Copying WHOLE Functions

    Originally posted by Speedy5
    I need help! What I want to do is to copy and paste the code of fucntions into an array of some sort and execute, through function pointers, the new code inside the array. (I'm not a hacker, btw). I also want to remove the return statements when copying the functions so I can add others.
    There is no way to "copy and paste" function bodies or change the return type of a function. If you want to do either of those things then you have a major problem with your logic in the program.

  3. #3
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    >>I'm not a hacker, btw

    no kidding

    wtf is the purpose of this program anyway? doesn't make any sense to me.
    PHP and XML
    Let's talk about SAX

  4. #4
    Speedy5
    Guest
    I want to clone the function, take it's code at runtime through pointers and copy it all into an array. And by trimming off X-number of bytes at the end (the assembler 'ret'), we can prevent the copied function from returning so I can paste another afterwards. I just want to have some help on this.

  5. #5
    Speedy5
    Guest
    I'm not a hacker, I'm not stupid. But please help!

    Its for a VERY VERY primitive form of a JIT Compiler.

    (This board responds fast, lol)

  6. #6
    Speedy5
    Guest
    basically, i just want to test myself on this

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    There is no way to do that in C++. You'd have to know the size of the function, which can not be calculated, as well as many other implementation dependant things.

    If you find that you think you have to do this, then you have a huge flaw in your program's logic. It should never be necissary.

  8. #8
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    WHY ARE YOU DOING THIS!?

    There is NO point in this (besides the fact that it's impossible)!

    Why not just call the function again??

    [edit]
    if you had the skill to make a JIT compiler you wouldn't be trying to do what you're doing, second of all there isn't a JIT for C++ for a reason....go use java if you want a JIT
    [/edit]
    Last edited by Waldo2k2; 01-19-2003 at 08:23 PM.
    PHP and XML
    Let's talk about SAX

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Slow down, speedy. To put it mildly, you are confused about how things work.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Speedy5
    Guest
    I can know the size of the function. Test it out. I put the f() function as a barrior. I take the address of f() and minus it from m(). That gives me, usually (works on Retail build), the correct size of the function.

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    That's not valid. You can not be guaranteed that the functions are right next to each other in memory.

  12. #12
    Speedy5
    Guest
    Where i got this idea:

    POSTED ON PLANET SOURCE CODE C++ SECTION




    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    /* This programs shows how to copy the bytecodes of a function to a variable
    then execute this chunk of code (dynamic linking?). Not sure if this works
    with all compilers and all machines/OSes
    */

    int func1 (int a, int b){ /* this is the function that will be copied */
    int i;
    for (i=0;i<=100;i++){ /*some silly code.. doesnt have to do with the purpose of this program */
    a+=i + b;
    }
    return a;
    }
    void func2(){ /*this useless function is declared to mark a boundary */
    } /* its address is just after the code for func1 */

    int (* f) (int a, int b); /*a function pointer, must have the same args of the copy*/

    int main(){
    /*try
    {*/
    int *start,len,i,*end;
    char * funccopy;
    start = (int* ) &func1 ; /*where to start copying? */
    end = (int* ) &func2; /* to determine length of char array to copy*/
    len = end-start; /* */
    funccopy = (char *)malloc (len);
    printf ("length of bytecode buffer %d\n",len); /* if you change the code within func1 you will see this number change accordingly */
    memcpy (funccopy,(void *)start,len); /*^ an easy way to determine the speed of your functions! */
    f = (int(__cdecl*)(int,int)) funccopy; /*now f will point to our copy */
    printf ("Value %d\n", f (3,4)); /* test it... does it work? */
    /*}
    catch (...)
    {
    printf("DAMN");
    }*/

    system("pause");
    return 0;
    }

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    *sigh* search the board...this topic covered just a few days ago...
    ...but it won't work quite like that...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Speedy5
    Guest
    You are right, I can't be sure. BUT, as I said, on the Retail build, it works, (the function size). I am SURE of this.

  15. #15
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    This programs shows how to copy the bytecodes of a function to a variable
    then execute this chunk of code (dynamic linking?).
    That's not dynamic linking. Dynamic linking makes the function pointers used in calling functions in one program in memory point to functions in memory at another location. There is no copying of functions going on in dynamic linking.

    The person who wrote the code had a vague idea, tried to implement it, and falsely concluded that it does what he thinks it's doing. It's interesting to test and toy with, but you can't conclude anything from it or guarantee portable results.
    Last edited by Polymorphic OOP; 01-19-2003 at 08:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM