Page 1 of 1
How to debug self-modifying code in Windows?
Posted: Thu Feb 24, 2011 4:32 am
by xuancong
I have debuggers like Visual C++ 2008, Visual C++ 6, IDA Pro 5.2, etc.
However, when I debug self-modifying exe files instruction-by-instruction, the results I obtained is different from when I press run&continue. Apparantly, the debugger is not aware of any code change caused by the program itself. So does anyone knows any tool which can debug self-modifying exe files properly?
How about in Linux?
Re: How to debug self-modifying code in Windows?
Posted: Thu Feb 24, 2011 6:38 am
by Tosi
As far as I know, there is no way to debug self-modifying code.
If you want to be able to debug, don't write self-modifying code.
If you want self-modifying code, then don't expect to be able to debug.
Re: How to debug self-modifying code in Windows?
Posted: Thu Feb 24, 2011 6:39 am
by Solar
Tosi wrote:If you want to be able to debug, don't write self-modifying code.
I.e., don't write self-modifying code.
Re: How to debug self-modifying code in Windows?
Posted: Thu Feb 24, 2011 12:13 pm
by davidv1992
Then what would you call what V8 does, cause that sure looks like self modifying code to me and that is one of the fastest javascript implementations available today.
Re: How to debug self-modifying code in Windows?
Posted: Thu Feb 24, 2011 12:17 pm
by Tosi
It's dynamic code generation. It is basically a just-in-time Javascript compiler. The generated machine code, I assume, does not modify itself during executed, which makes it not self-modifying code. This is typically the fastest way to run an interpreted language, in the same way that dynamic recompilation speeds up an emulator more than simple interpretation of opcodes does. Since the generated code does not modify itself, it avoids many of the problems of self-modifying code, such as cache problems.
Re: How to debug self-modifying code in Windows?
Posted: Thu Feb 24, 2011 1:30 pm
by qw
Is self-modifying code even possible on Windows? Didn't know that.
Re: How to debug self-modifying code in Windows?
Posted: Thu Feb 24, 2011 1:48 pm
by Tosi
I know it's possible with 32-bit Windows, but I don't know about 64-bit.
Re: How to debug self-modifying code in Windows?
Posted: Thu Feb 24, 2011 2:48 pm
by a5498828
windows use flat address space. Linear addresses are accessible using equal offsets using data or code selectors. Why you brought segment issue here? Im missing something about self mod~ code? Its just code that write where it execute, so instructions change dynamicly.
I would use trap flag to debug it. x86 allows it, windows - i dont think so. trap flag is very nice for debugging because your handler is executed after each instruction. things like popf you can emulate to hide trap flag state from program. And debugging ring3-1 code there is no way to escape this (is things like rdtsc are locked).
Re: How to debug self-modifying code in Windows?
Posted: Thu Feb 24, 2011 2:53 pm
by Tosi
Segmentation doesn't magically go away with protected mode. With 32-bit segmentation you can mark blocks of memory as "non-writeable" or "executable." PE files have separate sections such as .text/.code (executable, read-only), and data (read/write). I don't know which versions, if any, of Windows make use of the GDT or LDT in such a manner.
Re: How to debug self-modifying code in Windows?
Posted: Thu Feb 24, 2011 2:54 pm
by a5498828
windows use flat address space.
With 32-bit segmentation you can mark blocks of memory as "non-writeable" or "executable."
Rather readable (code) or writeable (data). Only 32 bit segmentation? I bet 16 bit one also works the same (with 24bit base and no granuality).
The only nonflat thing about windows is TIB. Each thread = diffrent base, FS stays same on cswitch, only its base changes.