Execute a piece of code from the data-section
Asked Answered
C

1

2

I want to take a piece of code, copy it into a global array and execute it from there.

In other words, I am trying to to copy a bunch of instructions from the code-section into the data-section, and then set the program-counter to continue the execution of the program from the data-section.

Here is my code:

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

typedef void(*func)();

static void code_section_func()
{
    printf("hello");
}

#define CODE_SIZE 73
// I verified this size in the disassembly of 'code_section_func'

static long long data[(CODE_SIZE-1)/sizeof(long long)+1];
// I am using 'long long' in order to obtain the maximum alignment

int main()
{
    func data_section_func = (func)data;
    memcpy((void*)data_section_func,(void*)code_section_func,CODE_SIZE);
    data_section_func();
    return 0;
}

I might have been naive thinking it could work, so I'd be happy to get an explanation why it didn't.

For example, after a program is loaded into memory, does the MMU restrict instruction-fetching to a specific area within the memory address space of the process (i.e., the code-section of the program)?

For the protocol, I have tested this with VS2013 compiler over a 64-bit OS and an x64-based processor.

Thanks

Cirrose answered 14/8, 2014 at 15:26 Comment(7)
Is this relevant to your question?Chloral
Another - related to heap space this timeChloral
This might also be relevantChloral
@Chloral assuming OP is running on Windows (based on usage of VS2013), none of those are completely relevant, as they all deal with unix/linuxBoiler
@roelofs: A little bit, because OP asks a similar thing (for 'stack' instead of 'data-section'). But the accepted answer refers only to the problem of how to compute the "size" of the function, whereas in my question I have computed that size and added it hard-coded. So my question relates strictly to why have I failed executing the code from the data-section (to which the other answer does not refer at all).Cirrose
The second answer on the third link (20 upvotes, but not the accepted one) deals with write and execute permissions - it sounds like that might be related?Chloral
@roelofs: Yes, that one is much closer to my question. Except for the fact that it does not explain how to do it for a statically allocated global array (or if it's even possible).Cirrose
B
4

Windows (and many other modern OSes) by default sets the data section as read/write/no-execute, so attempting to "call" a data object will fail.

Instead, you should VirtualAlloc a chunk of memory with the PAGE_EXECUTE_READWRITE protection. Note, it may be necessary to use FlushInstructionCache to ensure the newly-copied code is executed.

Boiler answered 14/8, 2014 at 15:43 Comment(3)
Thanks. Are these Win API routines?Cirrose
It seems he wants to execute from the stack, rather than the heap - is this solution still applicable?Chloral
@roelofs: data-section, not stack (but yes - statically allocated memory if that's what you meant).Cirrose

© 2022 - 2024 — McMap. All rights reserved.