I learned about function pointers recently in class and I was wondering if you could assign a function pointer to a block of memory allocated by a program, fill the memory block with assembly commands (hex values of op codes), then call the memory block using the function pointer.
I don't know much about function pointers, but I'm guessing you can't assign them wherever you want in memory, they need to point to a function. If that's true, how can you create a function in memory to be called? Is that even possible?
Here's some code I typed up to show the concept. I used various opcode values to see if anything would work, and 0x90 (NOP) did not break it sometimes (but it did other times), and 0xC3 (ret) always broke it.
#include <stdlib.h> //for malloc
#include <cstring> //for memcpy
int main() //program entry
{
void(*test)() = NULL; //create function pointer, initialize to NULL
void* hold_address = (void*)malloc(100); //allocate memory, save the address it returns in a dummy pointer
int asm_commands[] = {0x90}; //create array of assembly commands, hex values
memcpy(hold_address, asm_commands, sizeof(asm_commands)); //copy the array into the reserved memory
test = (void(*)())hold_address; //set the function pointer to start of the allocated memory
test(); //call the function, crashes here
return 0; //exit the program
}