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
}
&funcwill reference the memory you can read.memcpywill work with this reference on your platform as function pointers are not data pointers.uint64_tis not the type which is guaranteed to hold the reference to the function converted to this typeBut if it works, then your code copies the variable address and next bytes which follow it (and it invokes Undefined Behaviour)
This code invoked UB too but it might work on some platforms.