Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I can't run this code, what's problem?
C
#include <stdlib.h>
#include <stdio.h>
 
typedef unsigned long int uint32;
 
void dest()
{
    printf("Made it to dest()!\n");
    exit(0);
}

void func(uint32 a,uint32 b)
{
    uint32 *local;
    local = (uint32*)((uint32)&local + 0x08);
    *local = (uint32)&local + 0x0C;
}

int main()
{
    uint32 a = ((uint32)&dest << 8);
    a += 0x68;
 
    uint32 b = ((uint32)&dest >> 24);
    b += 0xC300;
 
    func(a,b);
    return 0;
}
Posted
Updated 8-Jul-11 5:27am
v2
Comments
Richard MacCutchan 8-Jul-11 11:30am    
No idea. what is it supposed to do, and what happens when you try?
NoorKia 8-Jul-11 11:36am    
it's a Self-modifying code,
i wanna just run it.

about Self-modifying:
http://en.wikipedia.org/wiki/Self-modifying_code
Richard MacCutchan 9-Jul-11 6:03am    
it's a Self-modifying code, i wanna just run it.
OK, go ahead, what is stopping you?
Smithers-Jones 8-Jul-11 13:00pm    
"Who can run this code in C?" - Chuck Norris probably can, though I am not 100 percent sure...

First, this code is not portable at all and very dependent on compiler options and such.

Second, the compiler might do some optimizations that prevent the code from running as you would expect.

Finaly even though the compiler might generate the appropriate code, the CPU might cache the code thus it might not be effective without some special OS function call.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Jul-11 21:41pm    
Did you pay attention that the code tries to self-modify?

This is essentially a re-post about self-modified code. John, Griff and myself offered OP pretty detailed answers (please see); but maybe OP ignored or failed to understand them?

Self-modified code will not work on "good" OS. Please see my answer.
--SA
Sergey Alexandrovich Kryukov 10-Jul-11 15:14pm    
By the way, I agree with your statements, my 5.
--SA
Self-modified code is not guaranteed to run. I explained it in my answer to previous question: How can I write a Self-Modifying code ic C?[^]; see also all the answers you got.

This question is different, but essentially this is re-post, as you should have used "Improve question" on the page of your previous question. May be you did not realize that writing self-modified code makes no sense? Well, not you're dealing with the consequences.

It totally depends on operating system; and good operating systems will use the features of CPU to prevent self-modified code. It can be absolutely prevented. What OS do you use trying to run this code? Why do you try to do it? Not really fruitful exercise, I would say. :-)

—SA
 
Share this answer
 
Comments
Espen Harlinn 10-Jul-11 14:19pm    
Good point, my 5 - not sure I would call this self modifying code though, just fooling around with a pointer to the function.
This may be of interest:http://migeel.sk/blog/2007/08/02/advanced-self-modifying-code/
Sergey Alexandrovich Kryukov 10-Jul-11 15:13pm    
Thank you, Espen.

Good point about the code presented by OP.
As I stated in my previous answer (you saw it), I'm not 100% sure modern Windows and Linux systems are totally guarded from modification of the executable code in memory or not, but this is absolutely possible and must be done.

Do you have any information of judgment about real situation with the OSs?
--SA
Espen Harlinn 10-Jul-11 15:53pm    
No, after the troubles I ran into with services, I've not spent anytime tinkering with this - but I do remember patting myself on the back when I initially implenteded it - at that time, it was the only way to do it, but I should have upgraded the code as soon as the customer moved to Windows 2000 - it was after all a "hack".
Sergey Alexandrovich Kryukov 11-Jul-11 1:57am    
Me too. I've done self-modified code in DOS, but only in cases where the task itself is nearly a hack -- remember Terminate-and-stay-resident? Self-modifying code is a compact technique to install and uninstall them to/from memory (a skill to make them self-removing them was pretty rare).
--SA
I don't see dest declared anywhere...
 
Share this answer
 
Comments
fjdiewornncalwe 8-Jul-11 12:14pm    
dest is a method, not a variable...
Sergey Alexandrovich Kryukov 9-Jul-11 21:40pm    
This is essentially a re-post about self-modified code. John, Griff and myself offered OP pretty detailed answers (please see); but maybe OP ignored or failed to understand them?

Self-modified code will not work on "good" OS. Please see my answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900