<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
self-modifying	B-Application
code	I-Application
(	O
SMC	O
)	O
is	O
code	O
that	O
alters	O
its	O
own	O
instructions	O
while	O
it	O
is	O
executing	B-General_Concept
–	O
usually	O
to	O
reduce	O
the	O
instruction	B-General_Concept
path	I-General_Concept
length	I-General_Concept
and	O
improve	O
performance	O
or	O
simply	O
to	O
reduce	O
otherwise	O
repetitively	O
similar	O
code	O
,	O
thus	O
simplifying	O
maintenance	O
.	O
</s>
<s>
The	O
term	O
is	O
usually	O
only	O
applied	O
to	O
code	O
where	O
the	O
self-modification	B-Application
is	O
intentional	O
,	O
not	O
in	O
situations	O
where	O
code	O
accidentally	O
modifies	O
itself	O
due	O
to	O
an	O
error	O
such	O
as	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
can	O
involve	O
overwriting	O
existing	O
instructions	O
or	O
generating	O
new	O
code	O
at	O
run	B-Library
time	I-Library
and	O
transferring	O
control	O
to	O
that	O
code	O
.	O
</s>
<s>
Self-modification	B-Application
can	O
be	O
used	O
as	O
an	O
alternative	O
to	O
the	O
method	O
of	O
"	O
flag	O
setting	O
"	O
and	O
conditional	B-Language
program	O
branching	O
,	O
used	O
primarily	O
to	O
reduce	O
the	O
number	O
of	O
times	O
a	O
condition	O
needs	O
to	O
be	O
tested	O
.	O
</s>
<s>
The	O
method	O
is	O
frequently	O
used	O
for	O
conditionally	O
invoking	O
test/debugging	O
code	O
without	O
requiring	O
additional	O
computational	O
overhead	O
for	O
every	O
input/output	B-General_Concept
cycle	O
.	O
</s>
<s>
only	O
during	O
initialization	O
–	O
based	O
on	O
input	O
parameters	O
(	O
when	O
the	O
process	O
is	O
more	O
commonly	O
described	O
as	O
software	O
'	O
configuration	B-General_Concept
 '	O
and	O
is	O
somewhat	O
analogous	O
,	O
in	O
hardware	O
terms	O
,	O
to	O
setting	O
jumpers	B-Device
for	O
printed	O
circuit	O
boards	O
)	O
.	O
</s>
<s>
Alteration	O
of	O
program	O
entry	O
pointers	O
is	O
an	O
equivalent	O
indirect	O
method	O
of	O
self-modification	B-Application
,	O
but	O
requiring	O
the	O
co-existence	O
of	O
one	O
or	O
more	O
alternative	O
instruction	O
paths	O
,	O
increasing	O
the	O
program	O
size	O
.	O
</s>
<s>
In	O
either	O
case	O
,	O
the	O
modifications	O
may	O
be	O
performed	O
directly	O
to	O
the	O
machine	B-Language
code	I-Language
instructions	O
themselves	O
,	O
by	O
overlaying	O
new	O
instructions	O
over	O
the	O
existing	O
ones	O
(	O
for	O
example	O
:	O
altering	O
a	O
compare	O
and	O
branch	O
to	O
an	O
unconditional	B-General_Concept
branch	I-General_Concept
or	O
alternatively	O
a	O
'	O
NOP	B-Language
 '	O
)	O
.	O
</s>
<s>
In	O
the	O
IBM	B-Device
System/360	I-Device
architecture	I-Device
,	O
and	O
its	O
successors	O
up	O
to	O
z/Architecture	B-Device
,	O
an	O
EXECUTE	O
(	O
EX	O
)	O
instruction	O
logically	O
overlays	O
the	O
second	O
byte	O
of	O
its	O
target	O
instruction	O
with	O
the	O
low-order	O
8	O
bits	O
of	O
register	O
1	O
.	O
</s>
<s>
This	O
provides	O
the	O
effect	O
of	O
self-modification	B-Application
although	O
the	O
actual	O
instruction	O
in	O
storage	O
is	O
not	O
altered	O
.	O
</s>
<s>
Self-modification	B-Application
can	O
be	O
accomplished	O
in	O
a	O
variety	O
of	O
ways	O
depending	O
upon	O
the	O
programming	O
language	O
and	O
its	O
support	O
for	O
pointers	O
and/or	O
access	O
to	O
dynamic	O
compiler	O
or	O
interpreter	B-Application
'	O
engines	O
 '	O
:	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
is	O
quite	O
straightforward	O
to	O
implement	O
when	O
using	O
assembly	B-Language
language	I-Language
.	O
</s>
<s>
Instructions	O
can	O
be	O
dynamically	O
created	O
in	O
memory	O
(	O
or	O
else	O
overlaid	O
over	O
existing	O
code	O
in	O
non-protected	O
program	O
storage	O
)	O
,	O
in	O
a	O
sequence	O
equivalent	O
to	O
the	O
ones	O
that	O
a	O
standard	O
compiler	O
may	O
generate	O
as	O
the	O
object	B-Language
code	I-Language
.	O
</s>
<s>
With	O
modern	O
processors	O
,	O
there	O
can	O
be	O
unintended	O
side	O
effects	O
on	O
the	O
CPU	B-General_Concept
cache	I-General_Concept
that	O
must	O
be	O
considered	O
.	O
</s>
<s>
The	O
method	O
was	O
frequently	O
used	O
for	O
testing	O
'	O
first	O
time	O
 '	O
conditions	O
,	O
as	O
in	O
this	O
suitably	O
commented	O
IBM/360	B-Application
assembler	B-Language
example	O
.	O
</s>
<s>
It	O
uses	O
instruction	O
overlay	O
to	O
reduce	O
the	O
instruction	B-General_Concept
path	I-General_Concept
length	I-General_Concept
by	O
(	O
N×1	O
)	O
−1	O
where	O
N	O
is	O
the	O
number	O
of	O
records	O
on	O
the	O
file	O
( −1	O
being	O
the	O
overhead	O
to	O
perform	O
the	O
overlay	O
)	O
.	O
</s>
<s>
SUBRTN	O
NOP	B-Language
OPENED	O
FIRST	O
TIME	O
HERE	O
?	O
</s>
<s>
OI	O
SUBRTN+1	O
,	O
X'F0'	O
YES	O
,	O
CHANGE	O
NOP	B-Language
TO	O
UNCONDITIONAL	B-General_Concept
BRANCH	I-General_Concept
(	O
47F0	O
...	O
)	O
</s>
<s>
The	O
unconditional	B-General_Concept
branch	I-General_Concept
is	O
slightly	O
faster	O
than	O
a	O
compare	O
instruction	O
,	O
as	O
well	O
as	O
reducing	O
the	O
overall	O
path	O
length	O
.	O
</s>
<s>
In	O
later	O
operating	B-General_Concept
systems	I-General_Concept
for	O
programs	O
residing	O
in	O
protected	B-General_Concept
storage	I-General_Concept
this	O
technique	O
could	O
not	O
be	O
used	O
and	O
so	O
changing	O
the	O
pointer	O
to	O
the	O
subroutine	O
would	O
be	O
used	O
instead	O
.	O
</s>
<s>
The	O
pointer	O
would	O
reside	O
in	O
dynamic	B-General_Concept
storage	I-General_Concept
and	O
could	O
be	O
altered	O
at	O
will	O
after	O
the	O
first	O
pass	O
to	O
bypass	O
the	O
OPEN	O
(	O
having	O
to	O
load	O
a	O
pointer	O
first	O
instead	O
of	O
a	O
direct	O
branch	O
&	O
link	O
to	O
the	O
subroutine	O
would	O
add	O
N	O
instructions	O
to	O
the	O
path	O
length	O
–	O
but	O
there	O
would	O
be	O
a	O
corresponding	O
reduction	O
of	O
N	O
for	O
the	O
unconditional	B-General_Concept
branch	I-General_Concept
that	O
would	O
no	O
longer	O
be	O
required	O
)	O
.	O
</s>
<s>
Below	O
is	O
an	O
example	O
in	O
Zilog	B-General_Concept
Z80	I-General_Concept
assembly	B-Language
language	I-Language
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
is	O
sometimes	O
used	O
to	O
overcome	O
limitations	O
in	O
a	O
machine	O
's	O
instruction	B-General_Concept
set	I-General_Concept
.	O
</s>
<s>
For	O
example	O
,	O
in	O
the	O
Intel	B-General_Concept
8080	I-General_Concept
instruction	B-General_Concept
set	I-General_Concept
,	O
one	O
cannot	O
input	O
a	O
byte	O
from	O
an	O
input	O
port	O
that	O
is	O
specified	O
in	O
a	O
register	O
.	O
</s>
<s>
Using	O
self-modifying	B-Application
code	I-Application
,	O
it	O
is	O
possible	O
to	O
store	O
a	O
register	O
's	O
contents	O
into	O
the	O
second	O
byte	O
of	O
the	O
instruction	O
,	O
then	O
execute	O
the	O
modified	O
instruction	O
in	O
order	O
to	O
achieve	O
the	O
desired	O
effect	O
.	O
</s>
<s>
Some	O
compiled	O
languages	O
explicitly	O
permit	O
self-modifying	B-Application
code	I-Application
.	O
</s>
<s>
For	O
example	O
,	O
the	O
ALTER	O
verb	O
in	O
COBOL	B-Application
may	O
be	O
implemented	O
as	O
a	O
branch	B-General_Concept
instruction	I-General_Concept
that	O
is	O
modified	O
during	O
execution	O
.	O
</s>
<s>
Some	O
batch	O
programming	O
techniques	O
involve	O
the	O
use	O
of	O
self-modifying	B-Application
code	I-Application
.	O
</s>
<s>
Clipper	B-Application
and	O
SPITBOL	B-Application
also	O
provide	O
facilities	O
for	O
explicit	O
self-modification	B-Application
.	O
</s>
<s>
The	O
Algol	O
compiler	O
on	O
B6700	B-Device
systems	I-Device
offered	O
an	O
interface	O
to	O
the	O
operating	B-General_Concept
system	I-General_Concept
whereby	O
executing	B-General_Concept
code	O
could	O
pass	O
a	O
text	O
string	O
or	O
a	O
named	O
disc	O
file	O
to	O
the	O
Algol	O
compiler	O
and	O
was	O
then	O
able	O
to	O
invoke	O
the	O
new	O
version	O
of	O
a	O
procedure	O
.	O
</s>
<s>
With	O
interpreted	B-Application
languages	I-Application
,	O
the	O
"	O
machine	B-Language
code	I-Language
"	O
is	O
the	O
source	O
text	O
and	O
may	O
be	O
susceptible	O
to	O
editing	O
on-the-fly	O
:	O
in	O
SNOBOL	B-Application
the	O
source	O
statements	O
being	O
executed	O
are	O
elements	O
of	O
a	O
text	O
array	O
.	O
</s>
<s>
Other	O
languages	O
,	O
such	O
as	O
Perl	B-Language
and	O
Python	B-Language
,	O
allow	O
programs	O
to	O
create	O
new	O
code	O
at	O
run-time	B-Library
and	O
execute	O
it	O
using	O
an	O
eval	O
function	O
,	O
but	O
do	O
not	O
allow	O
existing	O
code	O
to	O
be	O
mutated	O
.	O
</s>
<s>
The	O
illusion	O
of	O
modification	O
(	O
even	O
though	O
no	O
machine	B-Language
code	I-Language
is	O
really	O
being	O
overwritten	O
)	O
is	O
achieved	O
by	O
modifying	O
function	B-Language
pointers	I-Language
,	O
as	O
in	O
this	O
JavaScript	O
example	O
:	O
</s>
<s>
Lisp	O
macros	O
also	O
allow	O
runtime	B-Application
code	I-Application
generation	I-Application
without	O
parsing	O
a	O
string	O
containing	O
program	O
code	O
.	O
</s>
<s>
The	O
Push	O
programming	O
language	O
is	O
a	O
genetic	B-Algorithm
programming	I-Algorithm
system	O
that	O
is	O
explicitly	O
designed	O
for	O
creating	O
self-modifying	B-Application
programs	I-Application
.	O
</s>
<s>
While	O
not	O
a	O
high	O
level	O
language	O
,	O
it	O
is	O
not	O
as	O
low	O
level	O
as	O
assembly	B-Language
language	I-Language
.	O
</s>
<s>
Because	O
the	O
DOS	O
command	O
interpreter	B-Application
does	O
not	O
compile	O
a	O
script	O
file	O
and	O
then	O
execute	O
it	O
,	O
nor	O
does	O
it	O
read	O
the	O
entire	O
file	O
into	O
memory	O
before	O
starting	O
execution	O
,	O
nor	O
yet	O
rely	O
on	O
the	O
content	O
of	O
a	O
record	O
buffer	O
,	O
when	O
SHOWMENU	O
exits	O
,	O
the	O
command	O
interpreter	B-Application
finds	O
a	O
new	O
command	O
to	O
execute	O
(	O
it	O
is	O
to	O
invoke	O
the	O
script	O
file	O
SOMENAME	O
,	O
in	O
a	O
directory	O
location	O
and	O
via	O
a	O
protocol	O
known	O
to	O
SHOWMENU	O
)	O
,	O
and	O
after	O
that	O
command	O
completes	O
,	O
it	O
goes	O
back	O
to	O
the	O
start	O
of	O
the	O
script	O
file	O
and	O
reactivates	O
SHOWMENU	O
ready	O
for	O
the	O
next	O
selection	O
.	O
</s>
<s>
Although	O
this	O
starting	O
state	O
has	O
no	O
use	O
for	O
the	O
label	O
,	O
it	O
,	O
or	O
an	O
equivalent	O
amount	O
of	O
text	O
is	O
required	O
,	O
because	O
the	O
DOS	O
command	O
interpreter	B-Application
recalls	O
the	O
byte	O
position	O
of	O
the	O
next	O
command	O
when	O
it	O
is	O
to	O
start	O
the	O
next	O
command	O
,	O
thus	O
the	O
re-written	O
file	O
must	O
maintain	O
alignment	O
for	O
the	O
next	O
command	O
start	O
point	O
to	O
indeed	O
be	O
the	O
start	O
of	O
the	O
next	O
command	O
.	O
</s>
<s>
Control	B-Application
table	I-Application
interpreters	B-Application
can	O
be	O
considered	O
to	O
be	O
,	O
in	O
one	O
sense	O
,	O
'	O
self-modified	O
'	O
by	O
data	O
values	O
extracted	O
from	O
the	O
table	O
entries	O
(	O
rather	O
than	O
specifically	O
hand	O
coded	O
in	O
conditional	B-Language
statements	O
of	O
the	O
form	O
"	O
IF	O
inputx	O
=	O
'	O
yyy	O
 '	O
"	O
)	O
.	O
</s>
<s>
Some	O
IBM	O
access	B-Application
methods	I-Application
traditionally	O
used	O
self-modifying	O
channel	O
programs	O
,	O
where	O
a	O
value	O
,	O
such	O
as	O
a	O
disk	O
address	O
,	O
is	O
read	O
into	O
an	O
area	O
referenced	O
by	O
a	O
channel	O
program	O
,	O
where	O
it	O
is	O
used	O
by	O
a	O
later	O
channel	O
command	O
to	O
access	O
the	O
disk	O
.	O
</s>
<s>
The	O
IBM	B-Device
SSEC	I-Device
,	O
demonstrated	O
in	O
January	O
1948	O
,	O
had	O
the	O
ability	O
to	O
modify	O
its	O
instructions	O
or	O
otherwise	O
treat	O
them	O
exactly	O
like	O
data	O
.	O
</s>
<s>
In	O
the	O
early	O
days	O
of	O
computers	O
,	O
self-modifying	B-Application
code	I-Application
was	O
often	O
used	O
to	O
reduce	O
use	O
of	O
limited	O
memory	O
,	O
or	O
improve	O
performance	O
,	O
or	O
both	O
.	O
</s>
<s>
It	O
was	O
also	O
sometimes	O
used	O
to	O
implement	O
subroutine	O
calls	O
and	O
returns	O
when	O
the	O
instruction	B-General_Concept
set	I-General_Concept
only	O
provided	O
simple	O
branching	O
or	O
skipping	O
instructions	O
to	O
vary	O
the	O
control	O
flow	O
.	O
</s>
<s>
This	O
use	O
is	O
still	O
relevant	O
in	O
certain	O
ultra-RISC	O
architectures	O
,	O
at	O
least	O
theoretically	O
;	O
see	O
for	O
example	O
one	B-Application
instruction	I-Application
set	I-Application
computer	I-Application
.	O
</s>
<s>
Donald	O
Knuth	O
's	O
MIX	O
architecture	O
also	O
used	O
self-modifying	B-Application
code	I-Application
to	O
implement	O
subroutine	O
calls	O
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
can	O
be	O
used	O
for	O
various	O
purposes	O
:	O
</s>
<s>
Dynamic	O
in-place	O
code	O
optimization	B-Application
for	O
speed	O
depending	O
on	O
load	O
environment	O
.	O
</s>
<s>
Run-time	B-Application
code	I-Application
generation	I-Application
,	O
or	O
specialization	O
of	O
an	O
algorithm	O
in	O
runtime	B-Library
or	O
loadtime	O
(	O
which	O
is	O
popular	O
,	O
for	O
example	O
,	O
in	O
the	O
domain	O
of	O
real-time	O
graphics	O
)	O
such	O
as	O
a	O
general	O
sort	O
utility	O
–	O
preparing	O
code	O
to	O
perform	O
the	O
key	O
comparison	O
described	O
in	O
a	O
specific	O
invocation	O
.	O
</s>
<s>
Altering	O
of	O
inlined	O
state	O
of	O
an	O
object	O
,	O
or	O
simulating	O
the	O
high-level	O
construction	O
of	O
closures	B-Language
.	O
</s>
<s>
Patching	O
of	O
subroutine	O
(	O
pointer	O
)	O
address	O
calling	O
,	O
usually	O
as	O
performed	O
at	O
load/initialization	O
time	O
of	O
dynamic	B-Application
libraries	I-Application
,	O
or	O
else	O
on	O
each	O
invocation	O
,	O
patching	O
the	O
subroutine	O
's	O
internal	O
references	O
to	O
its	O
parameters	O
so	O
as	O
to	O
use	O
their	O
actual	O
addresses	O
(	O
i.e.	O
</s>
<s>
indirect	B-Application
self-modification	I-Application
)	O
.	O
</s>
<s>
Evolutionary	O
computing	O
systems	O
such	O
as	O
neuroevolution	B-Algorithm
,	O
genetic	B-Algorithm
programming	I-Algorithm
and	O
other	O
evolutionary	B-Algorithm
algorithms	I-Algorithm
.	O
</s>
<s>
Hiding	O
of	O
code	O
to	O
prevent	O
reverse	O
engineering	O
(	O
by	O
use	O
of	O
a	O
disassembler	O
or	O
debugger	B-Application
)	O
or	O
to	O
evade	O
detection	O
by	O
virus/spyware	O
scanning	O
software	O
and	O
the	O
like	O
.	O
</s>
<s>
Filling	O
100%	O
of	O
memory	O
(	O
in	O
some	O
architectures	O
)	O
with	O
a	O
rolling	O
pattern	O
of	O
repeating	O
opcodes	B-Language
,	O
to	O
erase	O
all	O
programs	O
and	O
data	O
,	O
or	O
to	O
burn-in	O
hardware	O
or	O
perform	O
RAM	B-Application
tests	I-Application
.	O
</s>
<s>
Compressing	B-Application
code	O
to	O
be	O
decompressed	O
and	O
executed	O
at	B-Library
runtime	I-Library
,	O
e.g.	O
,	O
when	O
memory	O
or	O
disk	O
space	O
is	O
limited	O
.	O
</s>
<s>
Some	O
very	O
limited	O
instruction	B-General_Concept
sets	I-General_Concept
leave	O
no	O
option	O
but	O
to	O
use	O
self-modifying	B-Application
code	I-Application
to	O
perform	O
certain	O
functions	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
one	B-Application
instruction	I-Application
set	I-Application
computer	I-Application
(	O
OISC	O
)	O
machine	O
that	O
uses	O
only	O
the	O
subtract-and-branch-if-negative	O
"	O
instruction	O
"	O
cannot	O
do	O
an	O
indirect	O
copy	O
(	O
something	O
like	O
the	O
equivalent	O
of	O
"	O
*	O
a	O
=	O
**	O
b	O
"	O
in	O
the	O
C	B-Language
language	I-Language
)	O
without	O
using	O
self-modifying	B-Application
code	I-Application
.	O
</s>
<s>
Booting	B-Operating_System
.	O
</s>
<s>
Early	O
microcomputers	B-Architecture
often	O
used	O
self-modifying	B-Application
code	I-Application
in	O
their	O
bootloaders	B-Application
.	O
</s>
<s>
Since	O
the	O
bootloader	B-Application
was	O
keyed	O
in	O
via	O
the	O
front	O
panel	O
at	O
every	O
power-on	O
,	O
it	O
did	O
not	O
matter	O
if	O
the	O
bootloader	B-Application
modified	O
itself	O
.	O
</s>
<s>
However	O
,	O
even	O
today	O
many	O
bootstrap	B-Operating_System
loaders	I-Operating_System
are	O
self-relocating	O
,	O
and	O
a	O
few	O
are	O
even	O
self-modifying	O
.	O
</s>
<s>
Pseudocode	B-Language
example	O
:	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
,	O
in	O
this	O
case	O
,	O
would	O
simply	O
be	O
a	O
matter	O
of	O
rewriting	O
the	O
loop	O
like	O
this	O
:	O
</s>
<s>
Note	O
that	O
two-state	O
replacement	O
of	O
the	O
opcode	B-Language
can	O
be	O
easily	O
written	O
as	O
'	O
xor	B-Application
var	O
at	O
address	O
with	O
the	O
value	O
"	O
opcodeOf(Inc )	O
xor	B-Application
opcodeOf(dec )	O
"	O
 '	O
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
is	O
more	O
complex	O
to	O
analyze	O
than	O
standard	O
code	O
and	O
can	O
therefore	O
be	O
used	O
as	O
a	O
protection	O
against	O
reverse	O
engineering	O
and	O
software	B-Application
cracking	I-Application
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
was	O
used	O
to	O
hide	O
copy	O
protection	O
instructions	O
in	O
1980s	O
disk-based	O
programs	O
for	O
platforms	O
such	O
as	O
IBM	B-Device
PC	I-Device
and	O
Apple	B-Device
II	I-Device
.	O
</s>
<s>
For	O
example	O
,	O
on	O
an	O
IBM	B-Device
PC	I-Device
(	O
or	O
compatible	O
)	O
,	O
the	O
floppy	B-Device
disk	I-Device
drive	I-Device
access	O
instruction	O
int	B-Device
0x13	I-Device
would	O
not	O
appear	O
in	O
the	O
executable	O
program	O
's	O
image	O
but	O
it	O
would	O
be	O
written	O
into	O
the	O
executable	O
's	O
memory	O
image	O
after	O
the	O
program	O
started	O
executing	B-General_Concept
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
is	O
also	O
sometimes	O
used	O
by	O
programs	O
that	O
do	O
not	O
want	O
to	O
reveal	O
their	O
presence	O
,	O
such	O
as	O
computer	O
viruses	O
and	O
some	O
shellcodes	O
.	O
</s>
<s>
Viruses	O
and	O
shellcodes	O
that	O
use	O
self-modifying	B-Application
code	I-Application
mostly	O
do	O
this	O
in	O
combination	O
with	O
polymorphic	B-Application
code	O
.	O
</s>
<s>
Modifying	O
a	O
piece	O
of	O
running	O
code	O
is	O
also	O
used	O
in	O
certain	O
attacks	O
,	O
such	O
as	O
buffer	B-General_Concept
overflows	I-General_Concept
.	O
</s>
<s>
They	O
avoid	O
the	O
danger	O
of	O
catastrophic	O
self-rewrites	O
by	O
making	O
sure	O
that	O
self-modifications	B-Application
will	O
survive	O
only	O
if	O
they	O
are	O
useful	O
according	O
to	O
a	O
user-given	O
fitness	O
,	O
error	O
or	O
reward	O
function	O
.	O
</s>
<s>
The	O
Linux	B-Operating_System
kernel	I-Operating_System
notably	O
makes	O
wide	O
use	O
of	O
self-modifying	B-Application
code	I-Application
;	O
it	O
does	O
so	O
to	O
be	O
able	O
to	O
distribute	O
a	O
single	O
binary	O
image	O
for	O
each	O
major	O
architecture	O
(	O
e.g.	O
</s>
<s>
IA-32	B-Device
,	O
x86-64	B-Device
,	O
32-bit	O
ARM	B-Architecture
,	O
ARM64	B-Architecture
...	O
)	O
while	O
adapting	O
the	O
kernel	B-Operating_System
code	O
in	O
memory	O
during	O
boot	B-Operating_System
depending	O
on	O
the	O
specific	O
CPU	O
model	O
detected	O
,	O
e.g.	O
</s>
<s>
to	O
be	O
able	O
to	O
take	O
advantage	O
of	O
new	O
CPU	B-Language
instructions	I-Language
or	O
to	O
work	O
around	O
hardware	O
bugs	O
.	O
</s>
<s>
Regardless	O
,	O
at	O
a	O
meta-level	O
,	O
programs	O
can	O
still	O
modify	O
their	O
own	O
behavior	O
by	O
changing	O
data	O
stored	O
elsewhere	O
(	O
see	O
metaprogramming	B-Application
)	O
or	O
via	O
use	O
of	O
polymorphism	B-Application
.	O
</s>
<s>
The	O
Synthesis	O
kernel	B-Operating_System
presented	O
in	O
Alexia	O
Massalin	O
's	O
Ph.D.	O
thesis	O
is	O
a	O
tiny	O
Unix	B-Application
kernel	B-Operating_System
that	O
takes	O
a	O
structured	B-Language
,	O
or	O
even	O
object	B-Language
oriented	I-Language
,	O
approach	O
to	O
self-modifying	B-Application
code	I-Application
,	O
where	O
code	O
is	O
created	O
for	O
individual	O
quajects	B-Operating_System
,	O
like	O
filehandles	O
.	O
</s>
<s>
Generating	O
code	O
for	O
specific	O
tasks	O
allows	O
the	O
Synthesis	O
kernel	B-Operating_System
to	O
(	O
as	O
a	O
JIT	O
interpreter	B-Application
might	O
)	O
apply	O
a	O
number	O
of	O
optimizations	O
such	O
as	O
constant	O
folding	O
or	O
common	O
subexpression	O
elimination	O
.	O
</s>
<s>
The	O
Synthesis	O
kernel	B-Operating_System
was	O
very	O
fast	O
,	O
but	O
was	O
written	O
entirely	O
in	O
assembly	O
.	O
</s>
<s>
The	O
resulting	O
lack	O
of	O
portability	O
has	O
prevented	O
Massalin	O
's	O
optimization	B-Application
ideas	O
from	O
being	O
adopted	O
by	O
any	O
production	O
kernel	B-Operating_System
.	O
</s>
<s>
Such	O
a	O
language	O
and	O
compiler	O
could	O
allow	O
development	O
of	O
faster	O
operating	B-General_Concept
systems	I-General_Concept
and	O
applications	O
.	O
</s>
<s>
Paul	O
Haeberli	O
and	O
Bruce	O
Karsh	O
have	O
objected	O
to	O
the	O
"	O
marginalization	O
"	O
of	O
self-modifying	B-Application
code	I-Application
,	O
and	O
optimization	B-Application
in	O
general	O
,	O
in	O
favor	O
of	O
reduced	O
development	O
costs	O
.	O
</s>
<s>
On	O
architectures	O
without	O
coupled	O
data	O
and	O
instruction	O
cache	O
(	O
for	O
example	O
,	O
some	O
SPARC	B-Architecture
,	O
ARM	B-Architecture
,	O
and	O
MIPS	B-Device
cores	O
)	O
the	O
cache	O
synchronization	O
must	O
be	O
explicitly	O
performed	O
by	O
the	O
modifying	O
code	O
(	O
flush	O
data	B-General_Concept
cache	I-General_Concept
and	O
invalidate	O
instruction	O
cache	O
for	O
the	O
modified	O
memory	O
area	O
)	O
.	O
</s>
<s>
In	O
some	O
cases	O
short	O
sections	O
of	O
self-modifying	B-Application
code	I-Application
execute	O
more	O
slowly	O
on	O
modern	O
processors	O
.	O
</s>
<s>
The	O
cache	O
invalidation	O
issue	O
on	O
modern	O
processors	O
usually	O
means	O
that	O
self-modifying	B-Application
code	I-Application
would	O
still	O
be	O
faster	O
only	O
when	O
the	O
modification	O
will	O
occur	O
rarely	O
,	O
such	O
as	O
in	O
the	O
case	O
of	O
a	O
state	O
switching	O
inside	O
an	O
inner	O
loop	O
.	O
</s>
<s>
Most	O
modern	O
processors	O
load	O
the	O
machine	B-Language
code	I-Language
before	O
they	O
execute	O
it	O
,	O
which	O
means	O
that	O
if	O
an	O
instruction	O
that	O
is	O
too	O
near	O
the	O
instruction	B-General_Concept
pointer	I-General_Concept
is	O
modified	O
,	O
the	O
processor	O
will	O
not	O
notice	O
,	O
but	O
instead	O
execute	O
the	O
code	O
as	O
it	O
was	O
before	O
it	O
was	O
modified	O
.	O
</s>
<s>
See	O
prefetch	B-General_Concept
input	I-General_Concept
queue	I-General_Concept
(	O
PIQ	O
)	O
.	O
</s>
<s>
PC	O
processors	O
must	O
handle	O
self-modifying	B-Application
code	I-Application
correctly	O
for	O
backwards	B-General_Concept
compatibility	I-General_Concept
reasons	O
but	O
they	O
are	O
far	O
from	O
efficient	O
at	O
doing	O
so	O
.	O
</s>
<s>
Because	O
of	O
the	O
security	O
implications	O
of	O
self-modifying	B-Application
code	I-Application
,	O
all	O
of	O
the	O
major	O
operating	B-General_Concept
systems	I-General_Concept
are	O
careful	O
to	O
remove	O
such	O
vulnerabilities	O
as	O
they	O
become	O
known	O
.	O
</s>
<s>
One	O
mechanism	O
for	O
preventing	O
malicious	O
code	O
modification	O
is	O
an	O
operating	B-General_Concept
system	I-General_Concept
feature	O
called	O
W^X	O
(	O
for	O
"	O
write	O
xor	B-Application
execute	O
"	O
)	O
.	O
</s>
<s>
On	O
Linux	B-Operating_System
,	O
one	O
may	O
use	O
an	O
undocumented	O
SysV	O
shared	O
memory	O
flag	O
to	O
get	O
executable	O
shared	O
memory	O
without	O
needing	O
to	O
create	O
a	O
file	O
.	O
</s>
<s>
Fast	B-General_Concept
paths	I-General_Concept
can	O
be	O
established	O
for	O
a	O
program	O
's	O
execution	O
,	O
reducing	O
some	O
otherwise	O
repetitive	O
conditional	B-General_Concept
branches	I-General_Concept
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
can	O
improve	O
algorithmic	B-General_Concept
efficiency	I-General_Concept
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
is	O
harder	O
to	O
read	O
and	O
maintain	O
because	O
the	O
instructions	O
in	O
the	O
source	O
program	O
listing	O
are	O
not	O
necessarily	O
the	O
instructions	O
that	O
will	O
be	O
executed	O
.	O
</s>
<s>
Self-modification	B-Application
that	O
consists	O
of	O
substitution	O
of	O
function	B-Language
pointers	I-Language
might	O
not	O
be	O
as	O
cryptic	O
,	O
if	O
it	O
is	O
clear	O
that	O
the	O
names	O
of	O
functions	O
to	O
be	O
called	O
are	O
placeholders	O
for	O
functions	O
to	O
be	O
identified	O
later	O
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
can	O
be	O
rewritten	O
as	O
code	O
that	O
tests	O
a	O
flag	O
and	O
branches	O
to	O
alternative	O
sequences	O
based	O
on	O
the	O
outcome	O
of	O
the	O
test	O
,	O
but	O
self-modifying	B-Application
code	I-Application
typically	O
runs	O
faster	O
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
conflicts	O
with	O
authentication	O
of	O
the	O
code	O
and	O
may	O
require	O
exceptions	O
to	O
policies	O
requiring	O
that	O
all	O
code	O
running	O
on	O
a	O
system	O
be	O
signed	O
.	O
</s>
<s>
Modified	O
code	O
must	O
be	O
stored	O
separately	O
from	O
its	O
original	O
form	O
,	O
conflicting	O
with	O
memory	B-General_Concept
management	I-General_Concept
solutions	O
that	O
normally	O
discard	O
the	O
code	O
in	O
RAM	B-Architecture
and	O
reload	O
it	O
from	O
the	O
executable	O
file	O
as	O
needed	O
.	O
</s>
<s>
On	O
modern	O
processors	O
with	O
an	O
instruction	B-General_Concept
pipeline	I-General_Concept
,	O
code	O
that	O
modifies	O
itself	O
frequently	O
may	O
run	O
more	O
slowly	O
,	O
if	O
it	O
modifies	O
instructions	O
that	O
the	O
processor	O
has	O
already	O
read	O
from	O
memory	O
into	O
the	O
pipeline	O
.	O
</s>
<s>
Self-modifying	B-Application
code	I-Application
cannot	O
be	O
used	O
at	O
all	O
in	O
some	O
environments	O
,	O
such	O
as	O
the	O
following	O
:	O
</s>
<s>
Application	O
software	O
running	O
under	O
an	O
operating	B-General_Concept
system	I-General_Concept
with	O
strict	O
W^X	O
security	O
cannot	O
execute	O
instructions	O
in	O
pages	O
it	O
is	O
allowed	O
to	O
write	O
to	O
—	O
only	O
the	O
operating	B-General_Concept
system	I-General_Concept
is	O
allowed	O
to	O
both	O
write	O
instructions	O
to	O
memory	O
and	O
later	O
execute	O
those	O
instructions	O
.	O
</s>
<s>
Many	O
Harvard	B-Architecture
architecture	I-Architecture
microcontrollers	B-Architecture
cannot	O
execute	O
instructions	O
in	O
read-write	O
memory	O
,	O
but	O
only	O
instructions	O
in	O
memory	O
that	O
it	O
cannot	O
write	O
to	O
,	O
ROM	B-Device
or	O
non-self-programmable	O
flash	B-Device
memory	I-Device
.	O
</s>
<s>
A	O
multithreaded	O
application	O
may	O
have	O
several	O
threads	O
executing	B-General_Concept
the	O
same	O
section	O
of	O
self-modifying	B-Application
code	I-Application
,	O
possibly	O
resulting	O
in	O
computation	O
errors	O
and	O
application	O
failures	O
.	O
</s>
