Why isn't this code directly storing the assembly instructions of func into the buffer? The value of the buffer is junk and doesn't change.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char buffer[1000];
uint64_t* addr;
void func() {
// here should be what the function really do
addr = (uint64_t*)&func;
memcpy(buffer, &addr, 1000); // im expecting that the instructions of func will be stored in the buffer
strcpy(secret, hash(secret, buffer)); // secret is previous hash of the function before it so i can make hash chain to verify the control flow integrity
// also the 1000 is not the actual size for the function, i just used it here for clarification
}
&addris the address of the variableaddr. You want justaddr.addr/&addrmixup, but there are multiple levels of undefinedness here.gpregister, you still run into problems when the compiler splits the function into multiple pieces and scatters them into different parts of memory. (For example, unlikely code paths like exception handling get moved out of hot pages.) All modern compilers do this.