Hello fellow programmers :) I have a problem with the function call in the middle of the other functions that were previously copied. I'm not sure if my problem is understandable, so I've included the code and comments.
void DeleteP2(int num){
printf("NUM: %d\n",num);
asm("leave");//I don't want to return to Delete(), but to main()
}
void Delete(){
int num = 50;
DeleteP2(num); //<==It's crashing
printf("ERROR\n"); //<==If I comment DeleteP2(num), this one is crashing too
}
/* Assembly code of Delete();
0x00401521 <+0>: push ebp
0x00401522 <+1>: mov ebp,esp
0x00401524 <+3>: sub esp,0x28
0x00401527 <+6>: mov DWORD PTR [ebp-0xc],0x32
0x0040152e <+13>: mov eax,DWORD PTR [ebp-0xc]
0x00401531 <+16>: mov DWORD PTR [esp],eax
0x00401534 <+19>: call 0x401500 <DeleteP2>
0x00401539 <+24>: mov DWORD PTR [esp],0x404009
0x00401540 <+31>: call 0x4026e0 <puts>
0x00401545 <+36>: leave
0x00401546 <+37>: ret
*/
typedef void (*DelFunc)(void);
DelFunc Create(){//Make a copy of Delete() function
unsigned char *code = VirtualAlloc(NULL,38,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
unsigned char *func = (unsigned char*)Delete;
for(int i=0;i<38;++i)code[i]=func[i];
return (DelFunc)code;
}
int main(int argc, char *argv[]) {
DelFunc f = Create();
Delete();//<== That one is not crashing
f();//<<== Stack trace error
VirtualFree(f,0,MEM_RELEASE);
return 0;
}
Could this be a problem with offsets? I mean that f() may need different addresses for the functions, than Delete() But I am not sure if thats the case. I would be happy from short explanation and maybe article where I would be able to learn how it works.
Delete()(not a disassembly, just a memory dump), do you see0x4026e0around location401534, or do you see an offset to it instead? Based on stackoverflow.com/questions/9438544/… I think it's an offset, which is no longer valid with your relocated copy of the function. I think this question is effectively a duplicate of the one I linked.