sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /* Test Heimdall's ability to spot writes to code which has been |
| 3 | translated, and discard the out-of-date translations. |
| 4 | |
| 5 | CORRECT output is |
| 6 | |
| 7 | in p 0 |
| 8 | in q 1 |
| 9 | in p 2 |
| 10 | in q 3 |
| 11 | in p 4 |
| 12 | in q 5 |
| 13 | in p 6 |
| 14 | in q 7 |
| 15 | in p 8 |
| 16 | in q 9 |
| 17 | |
| 18 | WRONG output (if you fail to spot code-writes to code[0 .. 4]) is |
| 19 | |
| 20 | in p 0 |
| 21 | in p 1 |
| 22 | in p 2 |
| 23 | in p 3 |
| 24 | in p 4 |
| 25 | in p 5 |
| 26 | in p 6 |
| 27 | in p 7 |
| 28 | in p 8 |
| 29 | in p 9 |
| 30 | */ |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | |
| 34 | typedef unsigned int Addr; |
| 35 | typedef unsigned char UChar; |
| 36 | |
| 37 | void q ( int n ) |
| 38 | { |
| 39 | printf("in q %d\n", n); |
| 40 | } |
| 41 | |
| 42 | void p ( int n ) |
| 43 | { |
| 44 | printf("in p %d\n", n); |
| 45 | } |
| 46 | |
| 47 | UChar code[100]; |
| 48 | |
| 49 | /* Make `code' be JMP-32 dest */ |
| 50 | void set_dest ( Addr dest ) |
| 51 | { |
| 52 | unsigned int delta; |
| 53 | delta = dest - ((Addr)(&code[0])); |
| 54 | delta -= 5; |
| 55 | |
| 56 | code[0] = 0xE9; /* JMP d32 */ |
| 57 | code[1] = (delta & 0xFF); |
| 58 | code[2] = ((delta >> 8) & 0xFF); |
| 59 | code[3] = ((delta >> 16) & 0xFF); |
| 60 | code[4] = ((delta >> 24) & 0xFF); |
| 61 | } |
| 62 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 63 | int main ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 64 | { |
| 65 | int i; |
| 66 | for (i = 0; i < 10; i += 2) { |
| 67 | set_dest ( (Addr)&p ); |
| 68 | ( (void (*)(int)) (&code[0]) ) (i); |
| 69 | set_dest ( (Addr)&q ); |
| 70 | ( (void (*)(int)) (&code[0]) ) (i+1); |
| 71 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 72 | return 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 73 | } |