I have changed the code in the above method such that meth->insns is
pointed to a new array containing the SAME bytes.
(just to see if I can actually play around with it later)
if(strcmp(meth->name, "call_hello") == 0)
{
DexCode* methodDexCode = (DexCode*) dvmGetMethodCode(meth);
//make code read-write
dvmLinearReadWrite(meth->clazz->classLoader, methodDexCode);
//inject additional bytecode
meth->registersSize = pDexCode->registersSize;
meth->insSize = pDexCode->insSize;
meth->outsSize = pDexCode->outsSize;
/* pointer to code area */
memcpy(newInst, pDexCode->insns, 8 * sizeof(unsigned short));
meth->insns = newInst;
//print out the instruction bytes
for(i = 0; i < 8; i++)
LOGI("--EARL-- modded, %4x ", meth->insns[i]);
//make code read-only
dvmLinearReadOnly(meth->clazz->classLoader, methodDexCode);
}
The target program in question is a simple hell world which has one
method "call_hello" which just sysouts a string.
This is the dexdump of the call_hello method
#1 : (in Lhello;)
name : 'call_hello'
type : '()V'
access : 0x0008 (STATIC)
code -
registers : 2
ins : 0
outs : 2
insns size : 8 16-bit code units
000154: |[000154]
hello.call_hello:()V
000164: 6200 0000 |0000: sget-object v0,
Ljava/lang/System;.out:Ljava/io/PrintStream; // field@0000
000168: 1a01 0a00 |0002: const-string v1,
"call_hello invoked" // string@000a
00016c: 6e20 0300 1000 |0004: invoke-virtual
{v0, v1}, Ljava/io/PrintStream;.println:(Ljava/lang/String;)V //
method@0003
000172: 0e00 |0007: return-void
catches : (none)
positions :
0x0000 line=10
0x0007 line=11
locals :
This is the tombstone when a seg fault occurs:
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'unknown'
pid: 275, tid: 275 >>> dalvikvm <<<
signal 11 (SIGSEGV), fault addr 4104c234
r0 00000000 r1 00000000 r2 000000a5 r3 ffffaca5
r4 beaf9a2c r5 4104bfa0 r6 beaf9c28 r7 0000aed9
r8 aca0fb80 r9 000000ae 10 4104bf8c fp 00000000
ip 000000d9 sp beaf9be8 lr aca62991 pc aca131cc cpsr 40000010
#00 pc 000131cc /system/lib/libdvm.so
#01 pc 0001a944 /system/lib/libdvm.so (dvmMterpStd)
#02 pc 00019810 /system/lib/libdvm.so (dvmInterpret)
#03 pc 00050014 /system/lib/libdvm.so (dvmCallMethodV)
#04 pc 0003d108 /system/lib/libdvm.so
#05 pc 00008918 /system/bin/dalvikvm
#06 pc 0000d066 /system/lib/libc.so (__libc_init)
code around pc:
aca131ac e1a00000 e1a00000 e1a00000 e1a00000
aca131bc e1a00000 e1d430f2 e1a09427 e20320ff
aca131cc e7950102 e1b01443 e1f470b4 e0600001
aca131dc e207c0ff e7850109 e088f30c e1a00000
aca131ec e1a00000 e1a00000 e1a00000 e1a00000
code around lr:
aca62970 42be2001 2000dc00 bdf0b005 2200b510
aca62980 ff6cf7ff 46c0bd10 2201b510 ff66f7ff
aca62990 46c0bd10 e000b510 78033001 d0fb2b5b
aca629a0 061b3b42 2a180e1a 4909d810 2201161b
aca629b0 1c13409a 420a3001 055ad109 213bd506
stack:
beaf9ba8 beaf9bc8 [stack]
beaf9bac beaf9c28 [stack]
beaf9bb0 0006e180 [heap]
beaf9bb4 aca5cc21 /system/lib/libdvm.so
beaf9bb8 4104bfb4
beaf9bbc aca83fd0 /system/lib/libdvm.so
beaf9bc0 00000002
beaf9bc4 00000001
beaf9bc8 00071328 [heap]
beaf9bcc 00000000
beaf9bd0 000164b0 [heap]
beaf9bd4 429bf1ac /data/dalvik-cache/
sdc...@hello.jar@classes.dex
beaf9bd8 4104bfbc
beaf9bdc beaf9c28 [stack]
beaf9be0 00000071
beaf9be4 aca117ec /system/lib/libdvm.so
#00 beaf9be8 00000000
beaf9bec beaf9c28 [stack]
beaf9bf0 0006e180 [heap]
beaf9bf4 beaf9c28 [stack]
beaf9bf8 beaf9cb8 [stack]
beaf9bfc 00000000
beaf9c00 00000000
beaf9c04 00000000
beaf9c08 00000000
beaf9c0c aca1a948 /system/lib/libdvm.so
#01 beaf9c10 0000c320 [heap]
beaf9c14 aca1a8f0 /system/lib/libdvm.so
beaf9c18 beaf9c28 [stack]
beaf9c1c aca19814 /system/lib/libdvm.so
#02 beaf9c20 0000c320 [heap]
beaf9c24 beaf9bc8 [stack]
beaf9c28 429bf1ac /data/dalvik-cache/
sdc...@hello.jar@classes.dex
beaf9c2c 4104bfbc
beaf9c30 aca8505e
beaf9c34 aca850a0
beaf9c38 4109356c /dev/ashmem/dalvik-LinearAlloc (deleted)
beaf9c3c 0006e180 [heap]
beaf9c40 0000c320 [heap]
beaf9c44 beaf9be8 [stack]
beaf9c48 41049300
beaf9c4c 0000c328 [heap]
beaf9c50 aca8505e
beaf9c54 aca850a0
beaf9c58 00000000
beaf9c5c 00000000
beaf9c60 40024401 /dev/ashmem/mspace/dalvik-heap/0 (deleted)
beaf9c64 aca4fd49 /system/lib/libdvm.so
beaf9c68 00000000
beaf9c6c 00000009
beaf9c70 410935a0 /dev/ashmem/dalvik-LinearAlloc (deleted)
beaf9c74 0000c320 [heap]
beaf9c78 429bf22c /data/dalvik-cache/
sdc...@hello.jar@classes.dex
beaf9c7c aca50017 /system/lib/libdvm.so
#03 beaf9c80 aca83fd0 /system/lib/libdvm.so
beaf9c84 429bf22c /data/dalvik-cache/
sdc...@hello.jar@classes.dex
beaf9c88 beaf9c90 [stack]
beaf9c8c 00000001
beaf9c90 00000001
beaf9c94 00000007
beaf9c98 40024428 /dev/ashmem/mspace/dalvik-heap/0 (deleted)
beaf9c9c beaf9cdc [stack]
beaf9ca0 0000c320 [heap]
beaf9ca4 410935a0 /dev/ashmem/dalvik-LinearAlloc (deleted)
beaf9ca8 40024428 /dev/ashmem/mspace/dalvik-heap/0 (deleted)
beaf9cac aca3d10d /system/lib/libdvm.so
#04 beaf9cb0 beaf9cb8 [stack]
beaf9cb4 beaf9cdc [stack]
beaf9cb8 aca3b645 /system/lib/libdvm.so
beaf9cbc 00008747 /system/bin/dalvikvm
beaf9cc0 0000c320 [heap]
beaf9cc4 beaf9cdc [stack]
beaf9cc8 aca3d0e5 /system/lib/libdvm.so
beaf9ccc 00009118 /system/bin/dalvikvm
beaf9cd0 0000a180 [heap]
beaf9cd4 0000891b /system/bin/dalvikvm
beaf9cd8 410935a0 /dev/ashmem/dalvik-LinearAlloc (deleted)
beaf9cdc 4001dcb8 /dev/ashmem/mspace/dalvik-heap/0 (deleted)
#05 beaf9ce0 beaf9d70 [stack]
beaf9ce4 410935a0 /dev/ashmem/dalvik-LinearAlloc (deleted)
beaf9ce8 00000003
beaf9cec 0000a120 [heap]
beaf9cf0 4001dcb8 /dev/ashmem/mspace/dalvik-heap/0 (deleted)
beaf9cf4 00008b59 /system/bin/dalvikvm
beaf9cf8 00010004 [heap]
beaf9cfc 00000002
beaf9d00 0000a120 [heap]
beaf9d04 00000000
beaf9d08 0000a190 [heap]
beaf9d0c 0000a168 [heap]
beaf9d10 00000000
beaf9d14 00000000
beaf9d18 00000000
beaf9d1c 00000000
beaf9d20 00000000
beaf9d24 afd0d069 /system/lib/libc.so
#06 beaf9d28 00000000
beaf9d2c b000293b /system/bin/linker
beaf9d30 00000004
beaf9d34 beaf9e1e [stack]
beaf9d38 beaf9e27 [stack]
beaf9d3c beaf9e2b [stack]
beaf9d40 beaf9e3d [stack]
beaf9d44 00000000
beaf9d48 beaf9e43 [stack]
beaf9d4c beaf9e58 [stack]
beaf9d50 beaf9e74 [stack]
beaf9d54 beaf9ea5 [stack]
beaf9d58 beaf9ebf [stack]
beaf9d5c beaf9f66 [stack]
beaf9d60 beaf9f79 [stack]
beaf9d64 beaf9f94 [stack]
beaf9d68 beaf9fb1 [stack]
beaf9d6c beaf9fc4 [stack]
and these are the bytes that are printed on the logger (I see that
they are not the same as those from dexdump)
I/dalvikvm( 275): --EARL--, 62
I/dalvikvm( 275): --EARL--, 0
I/dalvikvm( 275): --EARL--, 11a
I/dalvikvm( 275): --EARL--, a
I/dalvikvm( 275): --EARL--, 20f8
I/dalvikvm( 275): --EARL--, 2b
I/dalvikvm( 275): --EARL--, 10
I/dalvikvm( 275): --EARL--, e