Thread: Run machine-code in a non-executable

  1. #1
    Registered User AeroHammer's Avatar
    Join Date
    Sep 2002
    Posts
    12

    Question Run machine-code in a non-executable

    I want to load actual proccesor-specific machine code (as in
    IBM-compatible, not as in 486 10 MHz only) and run it.

    I plan to follow this order of actions:
    1) malloc() memory and load the entire file into it,
    2) set a pointer to a class to point to the first bit of file (may
    change to header info @ begining of file)
    3) do a pointer->function() type of operation to call code.

    The file format currently stands at:

    Code:
    something function(int target, int *data){
                              switch (target){
                                  case 1: called_function_1( data );
                                  case 2: called_function_2( data );
                                  ...
                              }
                              return ( value );
        };
    
        void called_function_1( int *data ){
                   ...
        };
    
        void called_function_2( int *data ){
                   ...
        };
    This isn't really code, just something to carry the concept across,
    all variables except for target and data in the first function would
    be in memory the file code itself malloc()'ed. target and data
    would be at locations at the end of the file in the file set aside
    for that purpose.

    This is intended to be used to incorporate plug-and-play type
    functionality in a format that I understand, and in a format that I
    choose (in case I need to change the format of the file later to
    add a header or some-such thing). Unfortunatly, I don't know if
    this will work, or even if I will need to have the program modify
    the plug-in according to it's memory location.

    May seem a bit of a doozy, but I really want to know the
    answer (I figure it should be useful for a mechanism to port
    programs from one OS to another on the same platform also).

    Thanks.

  2. #2
    Registered User AeroHammer's Avatar
    Join Date
    Sep 2002
    Posts
    12
    Sorry about some of the funkiness of my wording, I
    had intended to click preview, guess what I did
    instead.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    the Code Segment and the Data Segment are seperated. your program and stack are allocated by the operating system and run through before being unallocated and returned to the system.

    unfortunately, the code segment is read-only. this means that running a program like you want to would require some sort of emulator to read in the commands and execute them.

  4. #4
    Registered User AeroHammer's Avatar
    Join Date
    Sep 2002
    Posts
    12
    There's probably some work-around that can be used, does anyone know how poly-morphic programs do it? They mentioned it in the FAQ or tutorials area but I never saw a link to the info. If I had some OS or poly-morphic code experience I could probably do this in my sleep.

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    there's this "feature" (read: bug) in windows that allows programs to do this. it was done for the purpose of debugging, but it has been exploited to do other things, cheats in games among them. you can probably find more information on google. try searching for how to make programs like this.

    you might be able to overlap the code segment with the data segment, but i think this results in an segmentation fault. on older, pre i386 machines, it might work.

    some old 8086's had a bug which allowed programs to change the code stack pointer at will. this is probably worthless for your applications, though.


    try researching into debugging. gdb is open source and probably has source code which allows you to do this.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I did kind of what your doing once........but its WINAPI dependant

    My idea was to get an area of space and fill it with the machine code nessasary to use the MessageBox API in windows.....(Not the most useful proggie in the world, but I seem to enjoy doing stupid stuff like this )

    I created a global structure.......filled it with the binary instructions........changed the operands of the instructions to point to the right data and function pointers (use GetProcAddress() to find the APIs I needed - MessageBoxA & ExitThread)........Then I created a stalled thread.......obtained the context of this thread with GetThreadContext.........altered the thread's instruction pointer to start at the beginning of my global structure.......then I reset the context........resumed the thread.....and off it went.....

    In my main thread I called WaitForSingleObject to let my created code finish...then I shutdown the program.....

    When I planned this app, I honestly thought I would have to change the attributes of the memory I wrote the code into...I thought it would be real tricky (maybe having to add structured exception handling to the code to stop it crashing & burning - and this isnt nice to do in assembler!), but I was sure there was an API to allow me to change attributes as I needed (Hell the system does it!)..........so when I got to the point above, I ran the app expecting some sort of access violation....but when no exception was produced and the code ran as it should, I stopped working on it......

    The code is definately not perfect.......It works on WinXP & Win2000........have a look - here

  7. #7
    Registered User AeroHammer's Avatar
    Join Date
    Sep 2002
    Posts
    12

    Talking

    Thanks. That last suggestion, especially, could do more than I was hoping, and the dlopen tip sounds like exactly what I had in mind. I'll have to look up those bugs, too, might be useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i get an error after trying to run this code
    By Hanz in forum C Programming
    Replies: 6
    Last Post: 09-07-2005, 09:06 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. easy question about machine code
    By Jaguar in forum Tech Board
    Replies: 3
    Last Post: 10-07-2003, 09:11 AM
  4. How to run executable?
    By hugo in forum C++ Programming
    Replies: 5
    Last Post: 03-20-2002, 04:05 PM
  5. Load and run machine code at runtime
    By ninebit in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2002, 10:26 AM