Name | Executed | Routines | % | Executed | Lines | % | Unexecuted |
/home/matt/eu/rds/include/std/safe.e | 33 | 40 | 82.50% | 300 | 362 | 82.87% | 62 |
Routine | Executed | Lines | Unexecuted | |
show_block() | 34 | 43 | 79.07% | 9 |
info() | 0 | 6 | 0.00% | 6 |
memory_used() | 0 | 6 | 0.00% | 6 |
free_code() | 13 | 17 | 76.47% | 4 |
safe_address() | 31 | 35 | 88.57% | 4 |
dep_works() | 0 | 3 | 0.00% | 3 |
unregister_block() | 7 | 10 | 70.00% | 3 |
allocations() | 0 | 2 | 0.00% | 2 |
check_all_blocks() | 10 | 12 | 83.33% | 2 |
deallocate() | 20 | 22 | 90.91% | 2 |
far_addr() | 0 | 2 | 0.00% | 2 |
low_machine_addr() | 0 | 2 | 0.00% | 2 |
mem_copy() | 5 | 7 | 71.43% | 2 |
permits() | 0 | 2 | 0.00% | 2 |
call() | 6 | 7 | 85.71% | 1 |
die() | 8 | 9 | 88.89% | 1 |
mem_set() | 4 | 5 | 80.00% | 1 |
peek() | 8 | 9 | 88.89% | 1 |
peek2s() | 8 | 9 | 88.89% | 1 |
peek2u() | 8 | 9 | 88.89% | 1 |
peek4s() | 8 | 9 | 88.89% | 1 |
peek4u() | 8 | 9 | 88.89% | 1 |
peek_string() | 10 | 11 | 90.91% | 1 |
peeks() | 8 | 9 | 88.89% | 1 |
poke2() | 7 | 8 | 87.50% | 1 |
poke4() | 7 | 8 | 87.50% | 1 |
prepare_block() | 8 | 9 | 88.89% | 1 |
bad_address() | 2 | 2 | 100.00% | 0 |
bordered_address() | 7 | 7 | 100.00% | 0 |
c_func() | 5 | 5 | 100.00% | 0 |
c_proc() | 5 | 5 | 100.00% | 0 |
does_not_permit() | 8 | 8 | 100.00% | 0 |
ext_addr() | 2 | 2 | 100.00% | 0 |
int_addr() | 2 | 2 | 100.00% | 0 |
machine_addr() | 2 | 2 | 100.00% | 0 |
natural() | 2 | 2 | 100.00% | 0 |
poke() | 8 | 8 | 100.00% | 0 |
positive_int() | 2 | 2 | 100.00% | 0 |
register_block() | 8 | 8 | 100.00% | 0 |
show_byte() | 11 | 11 | 100.00% | 0 |
# | Executed | |
1 | -- (c) Copyright - See License.txt | |
2 | -- | |
3 | -- Euphoria 4.0 | |
4 | -- Machine Level Programming (386/486/Pentium) | |
5 | ||
6 | --**** | |
7 | -- === safe.e | |
8 | -- | |
9 | -- This is a slower DEBUGGING VERSION of machine.e | |
10 | -- | |
11 | -- How To Use This File: | |
12 | -- | |
13 | -- 1. If your program doesn't already include machine.e add: | |
14 | -- include std/machine.e | |
15 | -- to your main .ex[w][u] file at the top. | |
16 | -- | |
17 | -- 2. To turn debug version on, issue | |
18 | -- | |
19 | -- with define SAFE | |
20 | -- | |
21 | -- in your main program, before the statement including machine.e. | |
22 | -- | |
23 | -- 3. If necessary, call register_block(address, length, memory_protection) to add additional | |
24 | -- "external" blocks of memory to the safe_address_list. These are blocks | |
25 | -- of memory that are safe to use but which you did not acquire | |
26 | -- through Euphoria's allocate(), allocate_data(), allocate_code() or memory_protect(). Call | |
27 | -- unregister_block(address) when you want to prevent further access to | |
28 | -- an external block. | |
29 | -- | |
30 | -- 4. Run your program. It might be 10x slower than normal but it's | |
31 | -- worth it to catch a nasty bug. | |
32 | -- | |
33 | -- 5. If a bug is caught, you will hear some "beep" sounds. | |
34 | -- Press Enter to clear the screen and see the error message. | |
35 | -- There will be a "divide by zero" traceback in ex.err | |
36 | -- so you can find the statement that is making the illegal memory access. | |
37 | -- | |
38 | -- 6. To switch between normal and debug versions, simply comment in or out the | |
39 | -- "with define SAFE" directive. In means debugging and out means normal. | |
40 | -- Alternatively, you can use -D SAFE as a switch on the command line (debug) or not (normal). | |
41 | -- | |
42 | -- 7. The older method of switching files and renaming them //**no longer works**//. machine.e conditionally includes safe.e. | |
43 | -- | |
44 | -- This file is equivalent to machine.e, but it overrides the built-in | |
45 | -- routines: | |
46 | -- poke, peek, poke4, peek4s, peek4u, call, mem_copy, and mem_set | |
47 | -- and it provides alternate versions of: | |
48 | -- allocate, free | |
49 | -- | |
50 | -- Your program will only be allowed to read/write areas of memory | |
51 | -- that it allocated (and hasn't freed), as well as areas in low memory | |
52 | -- that you list below, or add dynamically via register_block(). | |
53 | ||
54 | namespace safe | |
55 | include std/error.e | |
56 | 8 | ifdef WINDOWS then |
57 | include std/win32/sounds.e | |
58 | end ifdef | |
59 | public include std/memconst.e | |
60 | 8 | ifdef DATA_EXECUTE then |
61 | public include std/machine.e as machine | |
62 | end ifdef | |
63 | ||
64 | ||
65 | ||
66 | -- Some parameters you may wish to change: | |
67 | ||
68 | --** | |
69 | -- Define block checking policy. | |
70 | -- | |
71 | -- Comments: | |
72 | -- | |
73 | -- If this integer is 1, (the default), check all blocks for edge corruption after each | |
74 | -- [[:call]](), [[:c_proc]]() or [[:c_func]](). | |
75 | -- To save time, your program can turn off this checking by setting check_calls to 0. | |
76 | ||
77 | 8 | public integer check_calls = 1 |
78 | ||
79 | --** | |
80 | -- Determine whether to flag accesses to remote memory areas. | |
81 | -- | |
82 | -- Comments: | |
83 | -- | |
84 | -- If this integer is 1 (the default under //WIN32//), only check for references to the | |
85 | -- leader or trailer areas just outside each registered block, and don't complain about | |
86 | -- addresses that are far out of bounds (it's probably a legitimate block from another source) | |
87 | -- | |
88 | -- For a stronger check, set this to 0 if your program will never read/write an | |
89 | -- unregistered block of memory. | |
90 | -- | |
91 | -- On //WIN32// people often use unregistered blocks. | |
92 | 8 | public integer edges_only = (platform()=2) |
93 | ||
94 | -- Constants that tell us what we are about to try to do: read, write or execute memory. | |
95 | -- They should be distinct from PERM_EXEC, etc... | |
96 | -- These are not permission constants. | |
97 | ||
98 | -- Internal types for understanding more than type-checking: | |
99 | ||
100 | -- internal address | |
101 | -- addresses used for passing to and getting from low level machine_func calls and to a few | |
102 | -- local only routines. | |
103 | 46 | |
104 | 46 | return 1 |
105 | end type | |
106 | ||
107 | -- external address | |
108 | -- addresses used for passing to and getting from high level functions in machine.e and | |
109 | -- public functions declared here. | |
110 | 76 | |
111 | 76 | return 1 |
112 | end type | |
113 | ||
114 | ||
115 | -- Include the starting address and length of any | |
116 | -- acceptable areas of memory for peek/poke here. | |
117 | -- Set allocation number to 0. | |
118 | -- This symbol is *only* available from std/machine.e when SAFE is defined. | |
119 | 8 | public sequence safe_address_list = {} |
120 | ||
121 | 8 | enum BLOCK_ADDRESS, BLOCK_LENGTH, ALLOC_NUMBER, BLOCK_PROT |
122 | ||
123 | with type_check | |
124 | ||
125 | 8 | constant OK = 1, BAD = 0 |
126 | 8 | constant M_ALLOC = 16, |
127 | 8 | M_FREE = 17, |
128 | 8 | M_SLEEP = 64 |
129 | ||
130 | 8 | puts(1, "\n\t\tUsing Debug Version of machine.e\n") |
131 | -- machine_proc(M_SLEEP, 3) | |
132 | ||
133 | -- biggest address on a 32-bit machine | |
134 | 8 | constant MAX_ADDR = power(2, 32)-1 |
135 | ||
136 | -- biggest address accessible to 16-bit real mode | |
137 | 8 | constant LOW_ADDR = power(2, 20)-1 |
138 | ||
139 | 197 | |
140 | 197 | return x >= 1 |
141 | end type | |
142 | ||
143 | 109 | |
144 | 109 | return x >= 0 |
145 | end type | |
146 | ||
147 | 263 | |
148 | -- a 32-bit non-null machine address | |
149 | 263 | return a > 0 and a <= MAX_ADDR and floor(a) = a |
150 | end type | |
151 | ||
152 | 0 | |
153 | -- protected mode far address {seg, offset} | |
154 | 0 | return length(a) = 2 and integer(a[1]) and machine_addr(a[2]) |
155 | end type | |
156 | ||
157 | 0 | |
158 | -- a legal low machine address | |
159 | 0 | return a > 0 and a <= LOW_ADDR and floor(a) = a |
160 | end type | |
161 | ||
162 | 8 | export constant BORDER_SPACE = 40 |
163 | 8 | export constant leader = repeat('@', BORDER_SPACE) |
164 | 8 | export constant trailer = repeat('%', BORDER_SPACE) |
165 | ||
166 | 20 | |
167 | sequence l | |
168 | 20 | for i = 1 to length(safe_address_list) do |
169 | 33 | if safe_address_list[i][BLOCK_ADDRESS] = addr then |
170 | 18 | l = eu:peek( {addr - BORDER_SPACE, BORDER_SPACE} ) |
171 | 18 | return equal(l, leader) |
172 | end if | |
173 | 15 | end for |
174 | 2 | return 0 |
175 | end type | |
176 | ||
177 | -- Return true if /action/ is not allowed when memory has /protection/ | |
178 | 56 | |
179 | 56 | if action = A_READ and not test_read( protection ) then |
180 | 1 | return 1 |
181 | 55 | elsif action = A_WRITE and not test_write( protection ) then |
182 | 4 | return 1 |
183 | 51 | elsif action = A_EXECUTE and not test_exec( protection ) then |
184 | 4 | return 1 |
185 | else | |
186 | 47 | return 0 |
187 | end if | |
188 | end function | |
189 | ||
190 | -- Return true if /action/ is allowed when memory has /protection/ | |
191 | 0 | |
192 | 0 | return not does_not_permit(protection,action) |
193 | end function | |
194 | ||
195 | 67 | |
196 | -- is it ok to read/write all addresses from start to start+len-1? | |
197 | -- Note: This routine is available from std/machine.e *only* when SAFE | |
198 | -- is defined. | |
199 | atom block_start, block_upper, upper | |
200 | sequence block | |
201 | ||
202 | 67 | if len = 0 then |
203 | 1 | return OK |
204 | end if | |
205 | ||
206 | 66 | upper = start + len |
207 | -- search the list of safe memory blocks: | |
208 | 66 | for i = 1 to length(safe_address_list) do |
209 | 372 | block = safe_address_list[i] |
210 | 372 | block_start = block[BLOCK_ADDRESS] |
211 | 372 | if edges_only then |
212 | -- addresses are considered safe as long as | |
213 | -- they aren't in any block's border zone and | |
214 | -- if they are in a block, the action is permitted | |
215 | -- for that block's protection | |
216 | 331 | if start <= 3 then |
217 | 1 | return BAD -- null pointer (or very small address) |
218 | end if | |
219 | 330 | if block[ALLOC_NUMBER] >= 1 then |
220 | -- an allocated block with a border area | |
221 | 330 | block_upper = block_start + block[BLOCK_LENGTH] |
222 | 330 | if (start >= block_start - BORDER_SPACE and |
223 | start < block_start) or | |
224 | (start >= block_upper and | |
225 | start < block_upper + BORDER_SPACE) then | |
226 | 1 | return BAD |
227 | ||
228 | 329 | elsif (upper > block_start - BORDER_SPACE and |
229 | upper <= block_start) or | |
230 | (upper > block_upper and | |
231 | upper < block_upper + BORDER_SPACE) then | |
232 | 1 | return BAD |
233 | ||
234 | 328 | elsif start < block_start - BORDER_SPACE and |
235 | upper > block_upper + BORDER_SPACE then | |
236 | 1 | return BAD |
237 | end if | |
238 | end if | |
239 | 327 | if ( (block_start <= start and start <= block_upper) or |
240 | (block_start <= upper and upper <= block_upper) ) | |
241 | and | |
242 | does_not_permit( block[BLOCK_PROT], action ) | |
243 | then | |
244 | 1 | return BAD |
245 | end if | |
246 | else | |
247 | -- addresses are considered safe as long as | |
248 | -- they are inside an allocated or registered block | |
249 | -- whose protection permits the current action. | |
250 | 41 | if start >= block_start then |
251 | 41 | block_upper = block_start + block[BLOCK_LENGTH] |
252 | 41 | if upper <= block_upper then |
253 | 17 | if i > 1 then |
254 | -- move block i to the top and move 1..i-1 down | |
255 | 0 | if i = 2 then |
256 | -- common case, subscript is faster than slice: | |
257 | 0 | safe_address_list[2] = safe_address_list[1] |
258 | else | |
259 | 0 | safe_address_list[2..i] = safe_address_list[1..i-1] |
260 | end if | |
261 | 0 | safe_address_list[1] = block |
262 | end if | |
263 | 17 | if does_not_permit( block[BLOCK_PROT], action ) then |
264 | 8 | return BAD |
265 | else | |
266 | 9 | return OK |
267 | end if | |
268 | end if | |
269 | end if | |
270 | end if | |
271 | 350 | end for |
272 | 44 | if edges_only then |
273 | 40 | return OK -- not found in any border zone |
274 | else | |
275 | 4 | return BAD -- not found in any safe block |
276 | end if | |
277 | end function | |
278 | ||
279 | 6 | |
280 | -- Terminate with a message. | |
281 | -- makes warning beeps first so you can see what's happening on the screen | |
282 | 6 | for i = 1 to 3 do |
283 | 18 | ifdef WINDOWS then |
284 | sound() | |
285 | end ifdef | |
286 | 18 | machine_func(M_SLEEP,0.1) |
287 | 18 | ifdef WINDOWS then |
288 | sound(0) | |
289 | end ifdef | |
290 | 18 | machine_func(M_SLEEP,0.1) |
291 | 18 | end for |
292 | 6 | crash(msg) |
293 | 0 | end procedure |
294 | ||
295 | 1 | |
296 | -- show address in decimal and hex | |
297 | 1 | return sprintf(" ADDRESS!!!! %d (#%08x)", {a, a}) |
298 | end function | |
299 | ||
300 | without warning &= (override) | |
301 | ||
302 | 5 | |
303 | -- safe version of peek | |
304 | integer len | |
305 | atom a | |
306 | ||
307 | 5 | if atom(x) then |
308 | 1 | len = 1 |
309 | 1 | a = x |
310 | else | |
311 | 4 | len = x[2] |
312 | 4 | a = x[1] |
313 | end if | |
314 | 5 | if safe_address(a, len, A_READ) then |
315 | 5 | return eu:peek(x) |
316 | else | |
317 | 0 | die("BAD PEEK" & bad_address(a)) |
318 | end if | |
319 | end function | |
320 | ||
321 | 2 | |
322 | -- safe version of peeks | |
323 | integer len | |
324 | atom a | |
325 | ||
326 | 2 | if atom(x) then |
327 | 1 | len = 1 |
328 | 1 | a = x |
329 | else | |
330 | 1 | len = x[2] |
331 | 1 | a = x[1] |
332 | end if | |
333 | 2 | if safe_address(a, len, A_READ) then |
334 | 2 | return eu:peeks(x) |
335 | else | |
336 | 0 | die("BAD PEEK" & bad_address(a)) |
337 | end if | |
338 | end function | |
339 | ||
340 | 2 | |
341 | -- safe version of peek | |
342 | integer len | |
343 | atom a | |
344 | ||
345 | 2 | if atom(x) then |
346 | 1 | len = 2 |
347 | 1 | a = x |
348 | else | |
349 | 1 | len = x[2] * 2 |
350 | 1 | a = x[1] |
351 | end if | |
352 | 2 | if safe_address(a, len, A_READ) then |
353 | 2 | return eu:peek2u(x) |
354 | else | |
355 | 0 | die("BAD PEEK2U" & bad_address(a)) |
356 | end if | |
357 | end function | |
358 | ||
359 | 2 | |
360 | -- safe version of peek | |
361 | integer len | |
362 | atom a | |
363 | ||
364 | 2 | if atom(x) then |
365 | 1 | len = 2 |
366 | 1 | a = x |
367 | else | |
368 | 1 | len = x[2] * 2 |
369 | 1 | a = x[1] |
370 | end if | |
371 | 2 | if safe_address(a, len, A_READ) then |
372 | 2 | return eu:peek2s(x) |
373 | else | |
374 | 0 | die("BAD PEEK2S" & bad_address(a)) |
375 | end if | |
376 | end function | |
377 | ||
378 | 2 | |
379 | -- safe version of peek4s | |
380 | integer len | |
381 | atom a | |
382 | ||
383 | 2 | if atom(x) then |
384 | 1 | len = 4 |
385 | 1 | a = x |
386 | else | |
387 | 1 | len = x[2]*4 |
388 | 1 | a = x[1] |
389 | end if | |
390 | 2 | if safe_address(a, len, A_READ) then |
391 | 2 | return eu:peek4s(x) |
392 | else | |
393 | 0 | die("BAD PEEK4S" & bad_address(a)) |
394 | end if | |
395 | end function | |
396 | ||
397 | 2 | |
398 | -- safe version of peek4u | |
399 | integer len | |
400 | atom a | |
401 | ||
402 | 2 | if atom(x) then |
403 | 1 | len = 4 |
404 | 1 | a = x |
405 | else | |
406 | 1 | len = x[2]*4 |
407 | 1 | a = x[1] |
408 | end if | |
409 | 2 | if safe_address(a, len, A_READ) then |
410 | 2 | return eu:peek4u(x) |
411 | else | |
412 | 0 | die("BAD PEEK4U" & bad_address(a)) |
413 | end if | |
414 | end function | |
415 | ||
416 | ||
417 | 1 | |
418 | -- safe version of peek_string | |
419 | integer len | |
420 | 1 | atom a = x |
421 | ||
422 | 1 | len = 1 |
423 | 1 | while 1 do |
424 | 7 | if safe_address( a, len, A_READ ) then |
425 | 7 | if not eu:peek( a + len - 1 ) then |
426 | 1 | exit |
427 | else | |
428 | 6 | len += 1 |
429 | end if | |
430 | else | |
431 | 0 | die("BAD PEEK_STRING" & bad_address(a)) |
432 | end if | |
433 | 6 | end while |
434 | 1 | return eu:peek_string(x) |
435 | end function | |
436 | ||
437 | ||
438 | 7 | |
439 | -- safe version of poke | |
440 | integer len | |
441 | ||
442 | 7 | if atom(v) then |
443 | 2 | len = 1 |
444 | else | |
445 | 5 | len = length(v) |
446 | end if | |
447 | 7 | if safe_address(a, len, A_WRITE) then |
448 | 6 | eu:poke(a, v) |
449 | else | |
450 | 1 | die("BAD POKE" & bad_address(a)) |
451 | end if | |
452 | 6 | end procedure |
453 | ||
454 | 4 | |
455 | -- safe version of poke2 | |
456 | integer len | |
457 | ||
458 | 4 | if atom(v) then |
459 | 2 | len = 2 |
460 | else | |
461 | 2 | len = length(v) * 2 |
462 | end if | |
463 | 4 | if safe_address(a, len, A_WRITE) then |
464 | 4 | eu:poke2(a, v) |
465 | else | |
466 | 0 | die("BAD POKE" & bad_address(a)) |
467 | end if | |
468 | 4 | end procedure |
469 | ||
470 | 4 | |
471 | -- safe version of poke4 | |
472 | integer len | |
473 | ||
474 | 4 | if atom(v) then |
475 | 2 | len = 4 |
476 | else | |
477 | 2 | len = length(v)*4 |
478 | end if | |
479 | 4 | if safe_address(a, len, A_WRITE) then |
480 | 4 | eu:poke4(a, v) |
481 | else | |
482 | 0 | die("BAD POKE4" & bad_address(a)) |
483 | end if | |
484 | 4 | end procedure |
485 | ||
486 | 1 | |
487 | -- safe mem_copy | |
488 | 1 | if not safe_address(target, len, A_WRITE) then |
489 | 0 | die("BAD MEM_COPY TARGET" & bad_address(target)) |
490 | 1 | elsif not safe_address(source, len, A_READ) then |
491 | 0 | die("BAD MEM_COPY SOURCE" & bad_address(source)) |
492 | else | |
493 | 1 | eu:mem_copy(target, source, len) |
494 | end if | |
495 | 1 | end procedure |
496 | ||
497 | 1 | |
498 | -- safe mem_set | |
499 | 1 | if safe_address(target, len, A_WRITE) then |
500 | 1 | eu:mem_set(target, value, len) |
501 | else | |
502 | 0 | die("BAD MEM_SET" & bad_address(target)) |
503 | end if | |
504 | 1 | end procedure |
505 | ||
506 | atom allocation_num | |
507 | 8 | allocation_num = 0 |
508 | ||
509 | 49 | |
510 | -- display byte at memory location m | |
511 | integer c | |
512 | ||
513 | 49 | c = eu:peek(m) |
514 | 49 | if c <= 9 then |
515 | 2 | printf(1, "%d", c) |
516 | 47 | elsif c < 32 or c > 127 then |
517 | 5 | printf(1, "%d #%02x", {c, c}) |
518 | else | |
519 | 42 | if c = leader[1] or c = trailer[1] then |
520 | 39 | printf(1, "%s", c) |
521 | else | |
522 | 3 | printf(1, "%d #%02x '%s'", {c, c, c}) |
523 | end if | |
524 | end if | |
525 | 49 | puts(1, ", ") |
526 | 49 | end procedure |
527 | ||
528 | 2 | |
529 | -- display a corrupted block and die | |
530 | integer len, id, bad, p | |
531 | atom start | |
532 | ||
533 | 2 | start = block_info[1] |
534 | 2 | len = block_info[2] |
535 | 2 | id = block_info[3] |
536 | 2 | printf(1, "BLOCK# %d, START: #%x, SIZE %d\n", {id, start, len}) |
537 | -- check pre-block | |
538 | 2 | bad = 0 |
539 | 2 | for i = start-BORDER_SPACE to start-1 do |
540 | 80 | p = eu:peek(i) |
541 | 80 | if p != leader[1] or bad then |
542 | 1 | bad += 1 |
543 | 1 | if bad = 1 then |
544 | 1 | puts(1, "DATA WAS STORED ILLEGALLY, JUST BEFORE THIS BLOCK:\n") |
545 | 1 | puts(1, "(" & leader[1] & " characters are OK)\n") |
546 | 1 | printf(1, "#%x: ", i) |
547 | end if | |
548 | 1 | show_byte(i) |
549 | end if | |
550 | 80 | end for |
551 | 2 | puts(1, "\nDATA WITHIN THE BLOCK:\n") |
552 | 2 | printf(1, "#%x: ", start) |
553 | 2 | if len <= 30 then |
554 | -- show whole block | |
555 | 2 | for i = start to start+len-1 do |
556 | 8 | show_byte(i) |
557 | 8 | end for |
558 | else | |
559 | -- first part of block | |
560 | 0 | for i = start to start+14 do |
561 | 0 | show_byte(i) |
562 | 0 | end for |
563 | -- last part of block | |
564 | 0 | puts(1, "\n ...\n") |
565 | 0 | printf(1, "#%x: ", start+len-15) |
566 | 0 | for i = start+len-15 to start+len-1 do |
567 | 0 | show_byte(i) |
568 | 0 | end for |
569 | end if | |
570 | 2 | bad = 0 |
571 | -- check post-block | |
572 | 2 | for i = start+len to start+len+BORDER_SPACE-1 do |
573 | 80 | p = eu:peek(i) |
574 | 80 | if p != trailer[1] or bad then |
575 | 40 | bad += 1 |
576 | 40 | if bad = 1 then |
577 | 1 | puts(1, "\nDATA WAS STORED ILLEGALLY, JUST AFTER THIS BLOCK:\n") |
578 | 1 | puts(1, "(" & trailer[1] & " characters are OK)\n") |
579 | 1 | printf(1, "#%x: ", i) |
580 | end if | |
581 | 40 | show_byte(i) |
582 | end if | |
583 | 80 | end for |
584 | 2 | die("safe.e: show_block()") |
585 | 0 | end procedure |
586 | ||
587 | 10 | |
588 | -- Check all allocated blocks for corruption of the leader and trailer areas. | |
589 | integer n | |
590 | ext_addr a | |
591 | sequence block | |
592 | ||
593 | 10 | for i = 1 to length(safe_address_list) do |
594 | 16 | block = safe_address_list[i] |
595 | 16 | if block[3] >= 1 then |
596 | -- a block that we allocated | |
597 | 16 | a = block[1] |
598 | 16 | n = block[2] |
599 | 16 | if not equal(leader, |
600 | eu:peek({a-BORDER_SPACE, BORDER_SPACE})) then | |
601 | 0 | show_block(block) |
602 | 16 | elsif not equal(trailer, |
603 | eu:peek({a+n, BORDER_SPACE})) then | |
604 | 0 | show_block(block) |
605 | end if | |
606 | end if | |
607 | 16 | end for |
608 | 10 | end procedure |
609 | ||
610 | 1 | |
611 | -- safe call - machine code must start in block that we own | |
612 | 1 | if safe_address(addr, 1, A_EXECUTE) then |
613 | 1 | eu:call(addr) |
614 | 1 | if check_calls then |
615 | 1 | check_all_blocks() -- check for any corruption |
616 | end if | |
617 | else | |
618 | 0 | die(sprintf("BAD CALL ADDRESS!!!! %d\n\n", addr)) |
619 | end if | |
620 | 1 | end procedure |
621 | ||
622 | 1 | |
623 | 1 | eu:c_proc(i, s) |
624 | 1 | if check_calls then |
625 | 1 | check_all_blocks() |
626 | end if | |
627 | 1 | end procedure |
628 | ||
629 | 8 | |
630 | object r | |
631 | ||
632 | 8 | r = eu:c_func(i, s) |
633 | 8 | if check_calls then |
634 | 8 | check_all_blocks() |
635 | end if | |
636 | 8 | return r |
637 | end function | |
638 | ||
639 | 2 | |
640 | -- register an externally-acquired block of memory as being safe to use | |
641 | 2 | allocation_num += 1 |
642 | 2 | for i = 1 to length(safe_address_list) do |
643 | 8 | if safe_address_list[i][BLOCK_ADDRESS] = block_addr then |
644 | 1 | die("ATTEMPT TO REGISTER A NON-EXTERNAL BLOCK.") |
645 | end if | |
646 | 7 | end for |
647 | 1 | safe_address_list = prepend(safe_address_list, {block_addr, block_len, |
648 | -allocation_num,memory_protection}) | |
649 | 1 | end procedure |
650 | ||
651 | 2 | |
652 | -- remove an external block of memory from the safe address list | |
653 | 2 | for i = 1 to length(safe_address_list) do |
654 | 2 | if safe_address_list[i][BLOCK_ADDRESS] = block_addr then |
655 | 2 | if safe_address_list[i][ALLOC_NUMBER] >= 0 then |
656 | 1 | die("ATTEMPT TO UNREGISTER A NON-EXTERNAL BLOCK") |
657 | end if | |
658 | 1 | safe_address_list = safe_address_list[1..i-1] & |
659 | safe_address_list[i+1..$] | |
660 | 1 | return |
661 | end if | |
662 | 0 | end for |
663 | 0 | die("ATTEMPT TO UNREGISTER A BLOCK THAT WAS NOT REGISTERED!") |
664 | 0 | end procedure |
665 | ||
666 | 40 | |
667 | ext_addr eaddr | |
668 | -- set up an allocated block so we can check it for corruption | |
669 | 40 | if iaddr = 0 then |
670 | 0 | die("OUT OF MEMORY!") |
671 | end if | |
672 | 40 | eu:poke(iaddr, leader) |
673 | 40 | eaddr = iaddr + BORDER_SPACE |
674 | 40 | eu:poke(eaddr+n, trailer) |
675 | 40 | allocation_num += 1 |
676 | 40 | safe_address_list = prepend(safe_address_list, {eaddr, n, allocation_num, protection}) |
677 | 40 | return eaddr |
678 | end function | |
679 | ||
680 | -- Internal use of the library only. free() calls this. It works with | |
681 | -- only atoms and in the unSAFE implementation is different. | |
682 | 8 | |
683 | -- free address a - make sure it was allocated | |
684 | int_addr ia | |
685 | ||
686 | 8 | for i = 1 to length(safe_address_list) do |
687 | 23 | if safe_address_list[i][BLOCK_ADDRESS] = a then |
688 | -- check pre and post block areas | |
689 | 6 | integer n |
690 | 6 | ia = a-BORDER_SPACE |
691 | 6 | if safe_address_list[i][ALLOC_NUMBER] <= 0 then |
692 | 0 | die("ATTEMPT TO FREE A BLOCK THAT WAS NOT ALLOCATED!") |
693 | end if | |
694 | 6 | n = safe_address_list[i][BLOCK_LENGTH] |
695 | 6 | if not equal(leader, eu:peek({ia, BORDER_SPACE})) then |
696 | 1 | show_block(safe_address_list[i]) |
697 | 5 | elsif not equal(trailer, eu:peek({a+n, BORDER_SPACE})) then |
698 | 1 | show_block(safe_address_list[i]) |
699 | end if | |
700 | 4 | ifdef DATA_EXECUTE then |
701 | ifdef WINDOWS then | |
702 | if dep_works() then | |
703 | eu:c_func( VirtualFree_rid, { ia, n, MEM_RELEASE } ) | |
704 | else | |
705 | eu:machine_proc(M_FREE, ia) | |
706 | end if | |
707 | elsedef | |
708 | eu:machine_proc(M_FREE, ia) | |
709 | end ifdef | |
710 | elsedef | |
711 | 4 | if safe_address_list[i][BLOCK_PROT] != PAGE_READ_WRITE then |
712 | 1 | die("ATTEMPT TO FREE WITH free() A BLOCK " & |
713 | "THAT WAS ALLOCATED BY allocate_protect()!") | |
714 | end if | |
715 | 3 | eu:machine_proc(M_FREE, ia) |
716 | end ifdef | |
717 | -- remove it from list | |
718 | 3 | safe_address_list = remove(safe_address_list, i) |
719 | 3 | return |
720 | end if | |
721 | 17 | end for |
722 | 2 | if bordered_address( a ) then |
723 | 0 | die("ATTEMPT TO FREE USING AN ILLEGAL ADDRESS!") |
724 | end if | |
725 | 2 | end procedure |
726 | 8 | FREE_RID = routine_id("deallocate") |
727 | ||
728 | -- Returns 1 if the DEP executing data only memory would cause an exception | |
729 | 0 | |
730 | 0 | ifdef WINDOWS then |
731 | return DEP_really_works | |
732 | elsedef | |
733 | 0 | return 0 |
734 | end ifdef | |
735 | end function | |
736 | ||
737 | export atom VirtualFree_rid | |
738 | ||
739 | 2 | |
740 | integer free_succeeded | |
741 | sequence block | |
742 | ||
743 | 2 | for i = 1 to length(safe_address_list) do |
744 | 2 | block = safe_address_list[i] |
745 | 2 | if block[BLOCK_ADDRESS] = addr then |
746 | 2 | if safe_address_list[i][ALLOC_NUMBER] <= 0 then |
747 | 0 | die("ATTEMPT TO FREE A BLOCK THAT WAS NOT ALLOCATED!") |
748 | end if | |
749 | 2 | integer n = safe_address_list[i][BLOCK_LENGTH] |
750 | 2 | if not equal(leader, eu:peek({addr-BORDER_SPACE, BORDER_SPACE})) then |
751 | 0 | show_block(safe_address_list[i]) |
752 | 2 | elsif not equal(trailer, eu:peek({addr+n, BORDER_SPACE})) then |
753 | 0 | show_block(safe_address_list[i]) |
754 | end if | |
755 | 2 | safe_address_list = remove( safe_address_list, i ) |
756 | 2 | exit |
757 | end if | |
758 | 0 | end for |
759 | ||
760 | 2 | ifdef WINDOWS then |
761 | if dep_works() then | |
762 | free_succeeded = c_func( VirtualFree_rid, | |
763 | { addr-BORDER_SPACE, size*wordsize, MEM_RELEASE } ) | |
764 | return | |
765 | end if | |
766 | end ifdef | |
767 | 2 | machine_proc(M_FREE, addr-BORDER_SPACE) |
768 | 2 | end procedure |
769 | ||
770 | -- Shawn's custom stuff: | |
771 | 0 | |
772 | 0 | integer tm = 0 |
773 | 0 | for i = 1 to length( safe_address_list ) do |
774 | 0 | tm += safe_address_list[i][BLOCK_LENGTH] |
775 | 0 | end for |
776 | 0 | return sprintf(""" |
777 | Total memory allocations %10d | |
778 | Total memory allocated %10dB""", | |
779 | { length(safe_address_list), tm } ) | |
780 | end function | |
781 | ||
782 | 0 | |
783 | 0 | integer tm = 0 |
784 | 0 | for i = 1 to length( safe_address_list ) do |
785 | 0 | tm += safe_address_list[i][BLOCK_LENGTH] |
786 | 0 | end for |
787 | 0 | return tm |
788 | end function | |
789 | ||
790 | 0 | |
791 | 0 | return length( safe_address_list ) |
792 | end function |