Some kinds of integer abuse (bugs and vulnerabilities) that can be found in programs.
修订版 | f1f90523b1c49f05a4a8215ba1de23dda9bcd43b (tree) |
---|---|
时间 | 2020-11-13 20:24:59 |
作者 | Joel Matthew Rees <joel.rees@gmai...> |
Commiter | Joel Matthew Rees |
intermediate state adding byte order simulation functions
@@ -1,234 +1,298 @@ | ||
1 | -/* A little program to demonstrate a few of the dangers | |
2 | -** of aliasing pointers to different types of integers. | |
3 | -** | |
4 | -** To fully appreciate the effects, | |
5 | -** compile and run this code on both LSB1st and MSB1st CPUs. | |
6 | -** | |
7 | -** Written in response to a thread on the TRS-80 Color Computer FB group. | |
8 | -** | |
9 | -** Joel Rees, Amagasaki, Japan, November 2020. | |
10 | -*/ | |
11 | - | |
12 | - | |
13 | -#include <stdlib.h> | |
14 | -#include <limits.h> | |
15 | -#include <stdio.h> | |
16 | - | |
17 | - | |
18 | -typedef char * byteptr_t; | |
19 | -typedef short * shortptr_t; | |
20 | -typedef long * longptr_t; | |
21 | - | |
22 | - | |
23 | -int main( int argc, char *argv[] ) | |
24 | -{ | |
25 | - short int shorttest[ 3 ] = { 0, 0, 0 }; | |
26 | - long int longtest[ 3 ] = { 0, 0, 0 }; | |
27 | - shortptr_t shortptr; | |
28 | - longptr_t longptr; | |
29 | - | |
30 | - printf( "Note that both\n" | |
31 | - "least significant first and most significant first\n" | |
32 | - "give unfortanate results.\n" | |
33 | - "Different, but both unfortunate.\n" | |
34 | - "Now that you know they do, see if you can figure out why.\n\n" ); | |
35 | - | |
36 | - printf( "Let's start with referring to a long as a short, all target bits clear.\n\n" ); | |
37 | - | |
38 | - shortptr = (short *) &longtest[ 1 ]; | |
39 | - | |
40 | - printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
41 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
42 | - printf( "Test: ( * shortptr ) = 1 \n" ); | |
43 | - | |
44 | - ( * shortptr ) = 1; | |
45 | - | |
46 | - printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
47 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
48 | - printf( "We thought we wrote: %d. We actually wrote %ld between %ld and %ld.\n", | |
49 | - * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
50 | - printf( "In hexadecimal,\n" ); | |
51 | - printf( "We thought we wrote: %x. We actually wrote %lx between %lx and %lx.\n", | |
52 | - * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
53 | - | |
54 | - printf( "\nAgain, setting all target bits,\n" ); | |
55 | - | |
56 | - longtest[ 0 ] = longtest[ 1 ] = longtest[ 2 ] = -1; | |
57 | - | |
58 | - printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
59 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
60 | - printf( "Test: ( * shortptr ) = 1 \n" ); | |
61 | - | |
62 | - ( * shortptr ) = 1; | |
63 | - | |
64 | - printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
65 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
66 | - printf( "We thought we wrote: %d. We actually wrote %ld between %ld and %ld.\n", | |
67 | - * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
68 | - printf( "In hexadecimal,\n" ); | |
69 | - printf( "We thought we wrote: %x. We actually wrote %lx between %lx and %lx.\n", | |
70 | - * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
71 | - | |
72 | - printf( "\n\nClearing all target bits,\n" ); | |
73 | - | |
74 | - longtest[ 0 ] = longtest[ 1 ] = longtest[ 2 ] = 0; | |
75 | - | |
76 | - printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
77 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
78 | - printf( "Test: ( * shortptr ) = -1 \n" ); | |
79 | - | |
80 | - ( * shortptr ) = -1; | |
81 | - | |
82 | - printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
83 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
84 | - printf( "We thought we wrote: %d. We actually wrote %ld between %ld and %ld.\n", | |
85 | - * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
86 | - printf( "In hexadecimal,\n" ); | |
87 | - printf( "We thought we wrote: %x. We actually wrote %lx between %lx and %lx.\n", | |
88 | - * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
89 | - | |
90 | - printf( "\nAgain, setting all target bits,\n" ); | |
91 | - | |
92 | - longtest[ 0 ] = longtest[ 1 ] = longtest[ 2 ] = -1; | |
93 | - | |
94 | - printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
95 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
96 | - printf( "Test: ( * shortptr ) = -1 \n" ); | |
97 | - | |
98 | - ( * shortptr ) = -1; | |
99 | - | |
100 | - printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
101 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
102 | - printf( "We thought we wrote: %d. We actually wrote %ld between %ld and %ld.\n", | |
103 | - * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
104 | - printf( "In hexadecimal,\n" ); | |
105 | - printf( "We thought we wrote: %x. We actually wrote %lx between %lx and %lx.\n", | |
106 | - * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
107 | - | |
108 | - printf( "\n\nTime for arithmetic on small integers.\n" ); | |
109 | - printf( "Clearing ..." ); | |
110 | - longtest[ 0 ] = longtest[ 1 ] = longtest[ 2 ] = 0; | |
111 | - printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
112 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
113 | - printf( "Test: ( * shortptr ) -= 1 (subtract 1)\n" ); | |
114 | - ( * shortptr ) -= 1; | |
115 | - printf( "* shortptr sees %d (%x)\n", * shortptr, *shortptr ); | |
116 | - printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
117 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
118 | - | |
119 | - printf( "\nClearing ..." ); | |
120 | - longtest[ 0 ] = longtest[ 1 ] = longtest[ 2 ] = 0; | |
121 | - printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
122 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
123 | - printf( "Test: ( * shortptr ) += 1 (add 1)\n" ); | |
124 | - ( * shortptr ) += 1; | |
125 | - printf( "* shortptr sees %d (%x)\n", * shortptr, *shortptr ); | |
126 | - printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
127 | - longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
128 | - | |
129 | - | |
130 | -/********/ | |
131 | - | |
132 | - printf( "\n\n\nNow let's refer to a short as a long.\n\n" ); | |
133 | - | |
134 | - longptr = (long *) &shorttest[ 1 ]; | |
135 | - | |
136 | - printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
137 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
138 | - printf( "Test: ( * longptr ) = 1 \n" ); | |
139 | - | |
140 | - ( * longptr ) = 1; | |
141 | - | |
142 | - printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
143 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
144 | - printf( "We thought we wrote: %ld. We actually wrote %d between %d and %d.\n", | |
145 | - * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
146 | - printf( "In hexadecimal,\n" ); | |
147 | - printf( "We thought we wrote: %lx. We actually wrote %x between %x and %x.\n", | |
148 | - * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
149 | - | |
150 | - printf( "\nAgain, setting all target bits,\n" ); | |
151 | - | |
152 | - shorttest[ 0 ] = shorttest[ 1 ] = shorttest[ 2 ] = -1; | |
153 | - | |
154 | - printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
155 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
156 | - printf( "Test: ( * longptr ) = 1 \n" ); | |
157 | - | |
158 | - ( * longptr ) = 1; | |
159 | - | |
160 | - printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
161 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
162 | - printf( "We thought we wrote: %ld. We actually wrote %d between %d and %d.\n", | |
163 | - * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
164 | - printf( "In hexadecimal,\n" ); | |
165 | - printf( "We thought we wrote: %lx. We actually wrote %x between %x and %x.\n", | |
166 | - * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
167 | - | |
168 | - | |
169 | - printf( "\n\nClearing all target bits,\n" ); | |
170 | - | |
171 | - shorttest[ 0 ] = shorttest[ 1 ] = shorttest[ 2 ] = 0; | |
172 | - | |
173 | - printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
174 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
175 | - printf( "Test: ( * longptr ) = -1 \n" ); | |
176 | - | |
177 | - ( * longptr ) = -1; | |
178 | - | |
179 | - printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
180 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
181 | - printf( "We thought we wrote: %ld. We actually wrote %d between %d and %d.\n", | |
182 | - * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
183 | - printf( "In hexadecimal,\n" ); | |
184 | - printf( "We thought we wrote: %lx. We actually wrote %x between %x and %x.\n", | |
185 | - * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
186 | - | |
187 | - printf( "\nAgain, setting all target bits,\n" ); | |
188 | - | |
189 | - shorttest[ 0 ] = shorttest[ 1 ] = shorttest[ 2 ] = -1; | |
190 | - | |
191 | - printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
192 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
193 | - printf( "Test: ( * longptr ) = -1 \n" ); | |
194 | - | |
195 | - ( * longptr ) = -1; | |
196 | - | |
197 | - printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
198 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
199 | - printf( "We thought we wrote: %ld. We actually wrote %d between %d and %d.\n", | |
200 | - * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
201 | - printf( "In hexadecimal,\n" ); | |
202 | - printf( "We thought we wrote: %lx. We actually wrote %x between %x and %x.\n", | |
203 | - * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
204 | - | |
205 | - printf( "\n\nTime for arithmetic on small integers.\n" ); | |
206 | - printf( "Clearing ..." ); | |
207 | - shorttest[ 0 ] = shorttest[ 1 ] = shorttest[ 2 ] = 0; | |
208 | - printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
209 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
210 | - printf( "Test: ( * longptr ) -= 1 (subtract 1)\n" ); | |
211 | - ( * longptr ) -= 1; | |
212 | - printf( "* longptr sees %d (%x)\n", * shortptr, *shortptr ); | |
213 | - printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
214 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
215 | - | |
216 | - printf( "\nClearing ..." ); | |
217 | - shorttest[ 0 ] = shorttest[ 1 ] = shorttest[ 2 ] = 0; | |
218 | - printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
219 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
220 | - printf( "Test: ( * longptr ) += 1 (add 1)\n" ); | |
221 | - ( * longptr ) += 1; | |
222 | - printf( "* longptr sees %d (%x)\n", * shortptr, *shortptr ); | |
223 | - printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
224 | - shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
225 | - | |
226 | - printf( "To get the full effect of this,\n" | |
227 | - "be sure to compile and run on both least significant first\n" | |
228 | - "and most significant first architectures.\n" | |
229 | - "Ask yourself, \n" | |
230 | - "on which architecture will you notice the effects more quickly?\n\n" ); | |
231 | - | |
232 | - | |
233 | - return EXIT_SUCCESS; | |
234 | -} | |
1 | +/* A little program to demonstrate a few of the dangers | |
2 | +** of aliasing pointers to different types of integers. | |
3 | +** | |
4 | +** To fully appreciate the effects, | |
5 | +** compile and run this code on both LSB1st and MSB1st CPUs. | |
6 | +** | |
7 | +** Written in response to a thread on the TRS-80 Color Computer FB group. | |
8 | +** | |
9 | +** Joel Rees, Amagasaki, Japan, November 2020. | |
10 | +*/ | |
11 | + | |
12 | + | |
13 | +#include <stdlib.h> | |
14 | +#include <limits.h> | |
15 | +#include <stdio.h> | |
16 | + | |
17 | + | |
18 | +typedef unsigned char byte_t; | |
19 | +typedef byte_t * byteptr_t; | |
20 | +typedef short * shortptr_t; | |
21 | +typedef long * longptr_t; | |
22 | + | |
23 | + | |
24 | +#define writeMSB1st( type, val, ptr ) \ | |
25 | +void write##type##MSB1st( type val; type##ptr_t ptr ) \ | |
26 | +{ \ | |
27 | + byteptr_t bytes = ( (byteptr_t) ptr ) + sizeof val; \ | |
28 | + for ( ;; ) \ | |
29 | + { \ | |
30 | + * --bytes = (byte_t) ( val & UCHAR_MAX ); \ | |
31 | + if ( bytes <= (byteptr_t) ptr ) break; \ | |
32 | + val >>= CHAR_BIT; \ | |
33 | + } \ | |
34 | +} | |
35 | + | |
36 | + | |
37 | +#define writeLSB1st( type ) \ | |
38 | +void write##type##LSB1st( type value; type##ptr_t memptr ) \ | |
39 | +{ \ | |
40 | + byteptr_t bytes = ( (byteptr_t) memptr ); \ | |
41 | + byteptr_t bound = bytes + sizeof value; \ | |
42 | + for ( ;; ) \ | |
43 | + { \ | |
44 | + * bytes++ = (byte_t) ( value & UCHAR_MAX ); \ | |
45 | + if ( bytes >= bound ) break; \ | |
46 | + value >>= CHAR_BIT; \ | |
47 | + } \ | |
48 | +} | |
49 | + | |
50 | + | |
51 | +long readlongMSB1st( longptr_t memptr ) | |
52 | +{ | |
53 | + long value = 0; | |
54 | + byteptr_t bytes = ( (byteptr_t) memptr ) + sizeof value; | |
55 | + while ( bytes >= (byteptr_t) memptr ) | |
56 | + { | |
57 | + value |= * --bytes; | |
58 | + value <<= CHAR_BIT; | |
59 | + } | |
60 | +} | |
61 | + | |
62 | + | |
63 | +long readlongLSB1st( longptr_t memptr ) | |
64 | +{ | |
65 | + long value = 0; | |
66 | + byteptr_t bytes = (byteptr_t) memptr; | |
67 | + byteptr_t bound = bytes + sizeof value; | |
68 | + while ( bytes < bound ) | |
69 | + { | |
70 | + value |= * bytes++; | |
71 | + value <<= CHAR_BIT; | |
72 | + } | |
73 | +} | |
74 | + | |
75 | + | |
76 | +writeMSB1st( long, value, memptr ) | |
77 | + | |
78 | +writeMSB1st( short ) | |
79 | + | |
80 | +writeLSB1st( long ) | |
81 | + | |
82 | +writeLSB1st( short ) | |
83 | + | |
84 | + | |
85 | + | |
86 | +int main( int argc, char *argv[] ) | |
87 | +{ | |
88 | + short int shorttest[ 3 ] = { 0, 0, 0 }; | |
89 | + long int longtest[ 3 ] = { 0, 0, 0 }; | |
90 | + shortptr_t shortptr; | |
91 | + longptr_t longptr; | |
92 | + | |
93 | + printf( "Note that both\n" | |
94 | + "least significant first and most significant first\n" | |
95 | + "give unfortanate results.\n" | |
96 | + "Different, but both unfortunate.\n" | |
97 | + "Now that you know they do, see if you can figure out why.\n\n" ); | |
98 | + | |
99 | + printf( "Let's start with referring to a long as a short, all target bits clear.\n\n" ); | |
100 | + | |
101 | + shortptr = (short *) &longtest[ 1 ]; | |
102 | + | |
103 | + printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
104 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
105 | + printf( "Test: ( * shortptr ) = 1 \n" ); | |
106 | + | |
107 | + ( * shortptr ) = 1; | |
108 | + | |
109 | + printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
110 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
111 | + printf( "We thought we wrote: %d. We actually wrote %ld between %ld and %ld.\n", | |
112 | + * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
113 | + printf( "In hexadecimal,\n" ); | |
114 | + printf( "We thought we wrote: %x. We actually wrote %lx between %lx and %lx.\n", | |
115 | + * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
116 | + | |
117 | + printf( "\nAgain, setting all target bits,\n" ); | |
118 | + | |
119 | + longtest[ 0 ] = longtest[ 1 ] = longtest[ 2 ] = -1; | |
120 | + | |
121 | + printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
122 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
123 | + printf( "Test: ( * shortptr ) = 1 \n" ); | |
124 | + | |
125 | + ( * shortptr ) = 1; | |
126 | + | |
127 | + printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
128 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
129 | + printf( "We thought we wrote: %d. We actually wrote %ld between %ld and %ld.\n", | |
130 | + * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
131 | + printf( "In hexadecimal,\n" ); | |
132 | + printf( "We thought we wrote: %x. We actually wrote %lx between %lx and %lx.\n", | |
133 | + * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
134 | + | |
135 | + printf( "\n\nClearing all target bits,\n" ); | |
136 | + | |
137 | + longtest[ 0 ] = longtest[ 1 ] = longtest[ 2 ] = 0; | |
138 | + | |
139 | + printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
140 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
141 | + printf( "Test: ( * shortptr ) = -1 \n" ); | |
142 | + | |
143 | + ( * shortptr ) = -1; | |
144 | + | |
145 | + printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
146 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
147 | + printf( "We thought we wrote: %d. We actually wrote %ld between %ld and %ld.\n", | |
148 | + * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
149 | + printf( "In hexadecimal,\n" ); | |
150 | + printf( "We thought we wrote: %x. We actually wrote %lx between %lx and %lx.\n", | |
151 | + * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
152 | + | |
153 | + printf( "\nAgain, setting all target bits,\n" ); | |
154 | + | |
155 | + longtest[ 0 ] = longtest[ 1 ] = longtest[ 2 ] = -1; | |
156 | + | |
157 | + printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
158 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
159 | + printf( "Test: ( * shortptr ) = -1 \n" ); | |
160 | + | |
161 | + ( * shortptr ) = -1; | |
162 | + | |
163 | + printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
164 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
165 | + printf( "We thought we wrote: %d. We actually wrote %ld between %ld and %ld.\n", | |
166 | + * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
167 | + printf( "In hexadecimal,\n" ); | |
168 | + printf( "We thought we wrote: %x. We actually wrote %lx between %lx and %lx.\n", | |
169 | + * shortptr, longtest[ 1 ], longtest[ 0 ], longtest[ 2 ] ); | |
170 | + | |
171 | + printf( "\n\nTime for arithmetic on small integers.\n" ); | |
172 | + printf( "Clearing ..." ); | |
173 | + longtest[ 0 ] = longtest[ 1 ] = longtest[ 2 ] = 0; | |
174 | + printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
175 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
176 | + printf( "Test: ( * shortptr ) -= 1 (subtract 1)\n" ); | |
177 | + ( * shortptr ) -= 1; | |
178 | + printf( "* shortptr sees %d (%x)\n", * shortptr, *shortptr ); | |
179 | + printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
180 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
181 | + | |
182 | + printf( "\nClearing ..." ); | |
183 | + longtest[ 0 ] = longtest[ 1 ] = longtest[ 2 ] = 0; | |
184 | + printf( "longtest before: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
185 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
186 | + printf( "Test: ( * shortptr ) += 1 (add 1)\n" ); | |
187 | + ( * shortptr ) += 1; | |
188 | + printf( "* shortptr sees %d (%x)\n", * shortptr, *shortptr ); | |
189 | + printf( "longtest after: %ld %ld %ld (hex: %lx %lx %lx)\n", | |
190 | + longtest[ 0 ], longtest[ 1 ], longtest[ 2 ], longtest[ 0 ], longtest[ 1 ], longtest[ 2 ] ); | |
191 | + | |
192 | + | |
193 | +/********/ | |
194 | + | |
195 | + printf( "\n\n\nNow let's refer to a short as a long.\n\n" ); | |
196 | + | |
197 | + longptr = (long *) &shorttest[ 1 ]; | |
198 | + | |
199 | + printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
200 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
201 | + printf( "Test: ( * longptr ) = 1 \n" ); | |
202 | + | |
203 | + ( * longptr ) = 1; | |
204 | + | |
205 | + printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
206 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
207 | + printf( "We thought we wrote: %ld. We actually wrote %d between %d and %d.\n", | |
208 | + * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
209 | + printf( "In hexadecimal,\n" ); | |
210 | + printf( "We thought we wrote: %lx. We actually wrote %x between %x and %x.\n", | |
211 | + * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
212 | + | |
213 | + printf( "\nAgain, setting all target bits,\n" ); | |
214 | + | |
215 | + shorttest[ 0 ] = shorttest[ 1 ] = shorttest[ 2 ] = -1; | |
216 | + | |
217 | + printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
218 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
219 | + printf( "Test: ( * longptr ) = 1 \n" ); | |
220 | + | |
221 | + ( * longptr ) = 1; | |
222 | + | |
223 | + printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
224 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
225 | + printf( "We thought we wrote: %ld. We actually wrote %d between %d and %d.\n", | |
226 | + * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
227 | + printf( "In hexadecimal,\n" ); | |
228 | + printf( "We thought we wrote: %lx. We actually wrote %x between %x and %x.\n", | |
229 | + * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
230 | + | |
231 | + | |
232 | + printf( "\n\nClearing all target bits,\n" ); | |
233 | + | |
234 | + shorttest[ 0 ] = shorttest[ 1 ] = shorttest[ 2 ] = 0; | |
235 | + | |
236 | + printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
237 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
238 | + printf( "Test: ( * longptr ) = -1 \n" ); | |
239 | + | |
240 | + ( * longptr ) = -1; | |
241 | + | |
242 | + printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
243 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
244 | + printf( "We thought we wrote: %ld. We actually wrote %d between %d and %d.\n", | |
245 | + * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
246 | + printf( "In hexadecimal,\n" ); | |
247 | + printf( "We thought we wrote: %lx. We actually wrote %x between %x and %x.\n", | |
248 | + * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
249 | + | |
250 | + printf( "\nAgain, setting all target bits,\n" ); | |
251 | + | |
252 | + shorttest[ 0 ] = shorttest[ 1 ] = shorttest[ 2 ] = -1; | |
253 | + | |
254 | + printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
255 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
256 | + printf( "Test: ( * longptr ) = -1 \n" ); | |
257 | + | |
258 | + ( * longptr ) = -1; | |
259 | + | |
260 | + printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
261 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
262 | + printf( "We thought we wrote: %ld. We actually wrote %d between %d and %d.\n", | |
263 | + * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
264 | + printf( "In hexadecimal,\n" ); | |
265 | + printf( "We thought we wrote: %lx. We actually wrote %x between %x and %x.\n", | |
266 | + * longptr, shorttest[ 1 ], shorttest[ 0 ], shorttest[ 2 ] ); | |
267 | + | |
268 | + printf( "\n\nTime for arithmetic on small integers.\n" ); | |
269 | + printf( "Clearing ..." ); | |
270 | + shorttest[ 0 ] = shorttest[ 1 ] = shorttest[ 2 ] = 0; | |
271 | + printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
272 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
273 | + printf( "Test: ( * longptr ) -= 1 (subtract 1)\n" ); | |
274 | + ( * longptr ) -= 1; | |
275 | + printf( "* longptr sees %d (%x)\n", * shortptr, *shortptr ); | |
276 | + printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
277 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
278 | + | |
279 | + printf( "\nClearing ..." ); | |
280 | + shorttest[ 0 ] = shorttest[ 1 ] = shorttest[ 2 ] = 0; | |
281 | + printf( "shorttest before: %d %d %d (hex: %x %x %x)\n", | |
282 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
283 | + printf( "Test: ( * longptr ) += 1 (add 1)\n" ); | |
284 | + ( * longptr ) += 1; | |
285 | + printf( "* longptr sees %d (%x)\n", * shortptr, *shortptr ); | |
286 | + printf( "shorttest after: %d %d %d (hex: %x %x %x)\n", | |
287 | + shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ], shorttest[ 0 ], shorttest[ 1 ], shorttest[ 2 ] ); | |
288 | + | |
289 | + printf( "To get the full effect of this,\n" | |
290 | + "be sure to compile and run on both least significant first\n" | |
291 | + "and most significant first architectures.\n" | |
292 | + "Ask yourself, \n" | |
293 | + "on which architecture will you notice the effects more quickly?\n\n" ); | |
294 | + | |
295 | + | |
296 | + return EXIT_SUCCESS; | |
297 | +} | |
298 | + |