C and Machine Code horror
-
/\*Generate mreadmem\*/ blockpoint2=BLOCKS+1; codeblockpos=0; addbyte(0x89); addbyte(0xFA); /\*MOVL %edi,%edx\*/ addbyte(0xC1); addbyte(0xEA); addbyte(12); /\*SHR $12,%edx\*/ addbyte(0x8B); addbyte(0x0C); addbyte(0x95); /\*MOV vraddrl(,%edx,4),%ecx\*/ addlong(vraddrl); addbyte(0xF6); addbyte(0xC1); addbyte(1); /\*TST %cl,1\*/ addbyte(0x75); addbyte(4); /\*JNZ notinbuffer\*/ addbyte(0x8B); addbyte(0x14); addbyte(0x39); /\*MOVL (%ecx,%edi),%edx\*/ addbyte(0xC3); /\*RET\*/ addbyte(0x57); /\*PUSH %edi\*/ addbyte(0xE8); /\*CALL\*/ addlong(readmemfl-(uint32\_t)(&rcodeblock\[blockpoint2\]\[codeblockpos+4\])); addbyte(0x89); addbyte(0xF9); /\*MOVL %edi,%ecx\*/ addbyte(0xC1); addbyte(0xE9); addbyte(12); /\*SHR $12,%ecx\*/ addbyte(0x83); addbyte(0xC4); addbyte(0x04); /\*ADDL $4,%esp\*/ addbyte(0x89); addbyte(0xC2); /\*MOVL %eax,%edx\*/ addbyte(0x8B); addbyte(0x0C); addbyte(0x8D); addlong(vraddrl); /\*MOV vraddrl(,%ecx,4),%ecx\*/ addbyte(0xC3); /\*RET\*/
There is written to memory and executed there. The only way I can think of making this worse is to remove the comments.
-
/\*Generate mreadmem\*/ blockpoint2=BLOCKS+1; codeblockpos=0; addbyte(0x89); addbyte(0xFA); /\*MOVL %edi,%edx\*/ addbyte(0xC1); addbyte(0xEA); addbyte(12); /\*SHR $12,%edx\*/ addbyte(0x8B); addbyte(0x0C); addbyte(0x95); /\*MOV vraddrl(,%edx,4),%ecx\*/ addlong(vraddrl); addbyte(0xF6); addbyte(0xC1); addbyte(1); /\*TST %cl,1\*/ addbyte(0x75); addbyte(4); /\*JNZ notinbuffer\*/ addbyte(0x8B); addbyte(0x14); addbyte(0x39); /\*MOVL (%ecx,%edi),%edx\*/ addbyte(0xC3); /\*RET\*/ addbyte(0x57); /\*PUSH %edi\*/ addbyte(0xE8); /\*CALL\*/ addlong(readmemfl-(uint32\_t)(&rcodeblock\[blockpoint2\]\[codeblockpos+4\])); addbyte(0x89); addbyte(0xF9); /\*MOVL %edi,%ecx\*/ addbyte(0xC1); addbyte(0xE9); addbyte(12); /\*SHR $12,%ecx\*/ addbyte(0x83); addbyte(0xC4); addbyte(0x04); /\*ADDL $4,%esp\*/ addbyte(0x89); addbyte(0xC2); /\*MOVL %eax,%edx\*/ addbyte(0x8B); addbyte(0x0C); addbyte(0x8D); addlong(vraddrl); /\*MOV vraddrl(,%ecx,4),%ecx\*/ addbyte(0xC3); /\*RET\*/
There is written to memory and executed there. The only way I can think of making this worse is to remove the comments.
Timothy Baldwin wrote:
The only way I can think of making this worse is to remove the comments.
I agree. It would serve as potential job security though.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
-
/\*Generate mreadmem\*/ blockpoint2=BLOCKS+1; codeblockpos=0; addbyte(0x89); addbyte(0xFA); /\*MOVL %edi,%edx\*/ addbyte(0xC1); addbyte(0xEA); addbyte(12); /\*SHR $12,%edx\*/ addbyte(0x8B); addbyte(0x0C); addbyte(0x95); /\*MOV vraddrl(,%edx,4),%ecx\*/ addlong(vraddrl); addbyte(0xF6); addbyte(0xC1); addbyte(1); /\*TST %cl,1\*/ addbyte(0x75); addbyte(4); /\*JNZ notinbuffer\*/ addbyte(0x8B); addbyte(0x14); addbyte(0x39); /\*MOVL (%ecx,%edi),%edx\*/ addbyte(0xC3); /\*RET\*/ addbyte(0x57); /\*PUSH %edi\*/ addbyte(0xE8); /\*CALL\*/ addlong(readmemfl-(uint32\_t)(&rcodeblock\[blockpoint2\]\[codeblockpos+4\])); addbyte(0x89); addbyte(0xF9); /\*MOVL %edi,%ecx\*/ addbyte(0xC1); addbyte(0xE9); addbyte(12); /\*SHR $12,%ecx\*/ addbyte(0x83); addbyte(0xC4); addbyte(0x04); /\*ADDL $4,%esp\*/ addbyte(0x89); addbyte(0xC2); /\*MOVL %eax,%edx\*/ addbyte(0x8B); addbyte(0x0C); addbyte(0x8D); addlong(vraddrl); /\*MOV vraddrl(,%ecx,4),%ecx\*/ addbyte(0xC3); /\*RET\*/
There is written to memory and executed there. The only way I can think of making this worse is to remove the comments.
Ohhh... so that's how you do that...
-
/\*Generate mreadmem\*/ blockpoint2=BLOCKS+1; codeblockpos=0; addbyte(0x89); addbyte(0xFA); /\*MOVL %edi,%edx\*/ addbyte(0xC1); addbyte(0xEA); addbyte(12); /\*SHR $12,%edx\*/ addbyte(0x8B); addbyte(0x0C); addbyte(0x95); /\*MOV vraddrl(,%edx,4),%ecx\*/ addlong(vraddrl); addbyte(0xF6); addbyte(0xC1); addbyte(1); /\*TST %cl,1\*/ addbyte(0x75); addbyte(4); /\*JNZ notinbuffer\*/ addbyte(0x8B); addbyte(0x14); addbyte(0x39); /\*MOVL (%ecx,%edi),%edx\*/ addbyte(0xC3); /\*RET\*/ addbyte(0x57); /\*PUSH %edi\*/ addbyte(0xE8); /\*CALL\*/ addlong(readmemfl-(uint32\_t)(&rcodeblock\[blockpoint2\]\[codeblockpos+4\])); addbyte(0x89); addbyte(0xF9); /\*MOVL %edi,%ecx\*/ addbyte(0xC1); addbyte(0xE9); addbyte(12); /\*SHR $12,%ecx\*/ addbyte(0x83); addbyte(0xC4); addbyte(0x04); /\*ADDL $4,%esp\*/ addbyte(0x89); addbyte(0xC2); /\*MOVL %eax,%edx\*/ addbyte(0x8B); addbyte(0x0C); addbyte(0x8D); addlong(vraddrl); /\*MOV vraddrl(,%ecx,4),%ecx\*/ addbyte(0xC3); /\*RET\*/
There is written to memory and executed there. The only way I can think of making this worse is to remove the comments.
There are cases where I would consider such code appropriate. If the routine in question will represent 90% of the running time of the program in which it resides, writing the routine as indicated will reduce its own running time by 60%, the running time of the program will be significant, and there is no other practical way to achieve such a speedup, then such code could be reasonable. Such code may also be reasonable in cases where one wishes to thwart disassembly. In such cases, one may have to tolerate some messiness in the source to obfuscate the machine code. If obfuscation of the machine code is a bona fide and legitimate goal, the nasty source code may be an acceptable price to pay. The above code doesn't seem to contain any loops, so it wouldn't have much use as a speedup method. It might be designed to discourage reverse-engineering, though there would be better ways of accomplishing that.
-
/\*Generate mreadmem\*/ blockpoint2=BLOCKS+1; codeblockpos=0; addbyte(0x89); addbyte(0xFA); /\*MOVL %edi,%edx\*/ addbyte(0xC1); addbyte(0xEA); addbyte(12); /\*SHR $12,%edx\*/ addbyte(0x8B); addbyte(0x0C); addbyte(0x95); /\*MOV vraddrl(,%edx,4),%ecx\*/ addlong(vraddrl); addbyte(0xF6); addbyte(0xC1); addbyte(1); /\*TST %cl,1\*/ addbyte(0x75); addbyte(4); /\*JNZ notinbuffer\*/ addbyte(0x8B); addbyte(0x14); addbyte(0x39); /\*MOVL (%ecx,%edi),%edx\*/ addbyte(0xC3); /\*RET\*/ addbyte(0x57); /\*PUSH %edi\*/ addbyte(0xE8); /\*CALL\*/ addlong(readmemfl-(uint32\_t)(&rcodeblock\[blockpoint2\]\[codeblockpos+4\])); addbyte(0x89); addbyte(0xF9); /\*MOVL %edi,%ecx\*/ addbyte(0xC1); addbyte(0xE9); addbyte(12); /\*SHR $12,%ecx\*/ addbyte(0x83); addbyte(0xC4); addbyte(0x04); /\*ADDL $4,%esp\*/ addbyte(0x89); addbyte(0xC2); /\*MOVL %eax,%edx\*/ addbyte(0x8B); addbyte(0x0C); addbyte(0x8D); addlong(vraddrl); /\*MOV vraddrl(,%ecx,4),%ecx\*/ addbyte(0xC3); /\*RET\*/
There is written to memory and executed there. The only way I can think of making this worse is to remove the comments.
Too much code, too much writing. It could be done in a simple and write-only way like this:
= {0x89,0xFA,0xC1,0xEA,0x0C,0x8B,...,0xC3 };
addbytes(mreadmem);Sometimes if your code is crap it is better to asm it down to make others thinking "Uh, better not touch that".
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
There are cases where I would consider such code appropriate. If the routine in question will represent 90% of the running time of the program in which it resides, writing the routine as indicated will reduce its own running time by 60%, the running time of the program will be significant, and there is no other practical way to achieve such a speedup, then such code could be reasonable. Such code may also be reasonable in cases where one wishes to thwart disassembly. In such cases, one may have to tolerate some messiness in the source to obfuscate the machine code. If obfuscation of the machine code is a bona fide and legitimate goal, the nasty source code may be an acceptable price to pay. The above code doesn't seem to contain any loops, so it wouldn't have much use as a speedup method. It might be designed to discourage reverse-engineering, though there would be better ways of accomplishing that.
The C code only executes once. It would have been better to use an assembler! However most of this file is a JIT complier written in a similar style - a complete absence of symbolic constants. And it is slow. As for discouraging reverse-engineering, this code is published under the GNU General Public Licence.
-
The C code only executes once. It would have been better to use an assembler! However most of this file is a JIT complier written in a similar style - a complete absence of symbolic constants. And it is slow. As for discouraging reverse-engineering, this code is published under the GNU General Public Licence.
Timothy Baldwin wrote:
As for discouraging reverse-engineering, this code is published under the GNU General Public Licence
If it weren't for the comments, that might explain it. BTW, would the GPL require the release of source code for things like p-code which is interpreted by the rest of the program? What if the source code for the p-code never existed (because it was hand-generated)?