Xref: utzoo comp.lang.c:11290 comp.arch:5455 Path: utzoo!utgpu!water!watmath!clyde!att!pacbell!ames!ubvax!vsi1!altnet!uunet!mcvax!cernvax!hjm From: hjm@cernvax.UUCP (hjm) Newsgroups: comp.lang.c,comp.arch Subject: Re: Self-modifying code Summary: What is self-modifying code anyway? Keywords: self-modifying code Message-ID: <752@cernvax.UUCP> Date: 13 Jul 88 09:44:20 GMT References: <3353@cognos.UUCP> <619@goofy.megatest.UUCP> <429@uwovax.uwo.ca> <5254@june.cs.washington.edu> <12357@ut-sally.UUCP> <5262@june.cs.washington.edu> Reply-To: hjm@cernvax.UUCP () Organization: CERN European Laboratory for Particle Physics, CH-1211 Geneva, Switzerland Lines: 36 I have been mulling over the idea of self-modifying code (SMC) for a while and I've come to the conclusion that there is no good definition of SMC. For example, if treating code as data is the definition, then does passing a procedure as a parameter in PASCAL, or a pointer to a function in C count? Probably not. OK, what about a jump table. Consider an array of pointers to functions in C. Does changing the pointers count as SMC? Again, I don't think so. So, changing a pointer by assigning to it is not SMC, but putting a new jump instruction in (e.g. jmp #somewhere_else) in place of an existing instruction is SMC. Does one level of indirection really make that much difference? Of course, if you want to be really horrid in C, you can try something like this: char codearray[] = { 0x03, /* one byte instruction for something */ 0xc9 /* one byte return instruction */ } and then 'call' this function using a cast to turn the pointer codearray into a pointer to a function. (Who needs #asm anyway!) Then you can modify the code as much as you want. This _is_ SMC without a doubt, because you can overwrite code. So, I propose a very weak definition for SMC as code that writes over other code. As a final note, why is it 'clean' to alter a jump table and 'unclean' to alter an inline constant (e.g. jmp @offset(r0) uses a value in memory as the address but mov (pc)+,#1234 which loads an immediate does so too)? Why the subtle difference? Any thoughts on the subject? Hubert Matthews (I don't consider LISP or PROLOG programs that create code on the fly to be SMC. Does anyone disagree?)