File: exhaust.impure.lisp

package info (click to toggle)
sbcl 1%3A0.9.16.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 19,960 kB
  • ctags: 16,537
  • sloc: lisp: 231,164; ansic: 19,558; asm: 2,539; sh: 1,925; makefile: 308
file content (73 lines) | stat: -rw-r--r-- 2,542 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;;;; tests of the system's ability to catch resource exhaustion problems

;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;;
;;;; This software is in the public domain and is provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for
;;;; more information.

(cl:in-package :cl-user)

;;; Prior to sbcl-0.7.1.38, doing something like (RECURSE), even in
;;; safe code, would crash the entire Lisp process. Then the soft
;;; stack checking was introduced, which checked (in safe code) for
;;; stack exhaustion at each lambda.

;;; Post 0.7.6.1, this was rewritten to use mprotect()-based stack
;;; protection which does not require lisp code to check anything,
;;; and works at all optimization settings.  However, it now signals a
;;; STORAGE-CONDITION instead of an ERROR.

(defun recurse ()
  (recurse)
  (recurse))

(defvar *count* 100)

;;; Base-case: detecting exhaustion
(assert (eq :exhausted
            (handler-case
                (recurse)
              (storage-condition (c)
                (declare (ignore c))
                :exhausted))))

;;; Check that non-local control transfers restore the stack
;;; exhaustion checking after unwinding -- and that previous test
;;; didn't break it.
(let ((exhaust-count 0)
      (recurse-count 0))
  (tagbody
     :retry
     (handler-bind ((storage-condition (lambda (c)
                                         (declare (ignore c))
                                         (if (= *count* (incf exhaust-count))
                                             (go :stop)
                                             (go :retry)))))
       (incf recurse-count)
       (recurse))
     :stop)
  (assert (= exhaust-count recurse-count *count*)))

;;; Check that we can safely use user-provided restarts to
;;; unwind.
(let ((exhaust-count 0)
      (recurse-count 0))
  (block nil
   (handler-bind ((storage-condition (lambda (c)
                                       (declare (ignore c))
                                       (if (= *count* (incf exhaust-count))
                                           (return)
                                           (invoke-restart (find-restart 'ok))))))
      (loop
       (with-simple-restart (ok "ok")
         (incf recurse-count)
         (recurse)))))
  (assert (= exhaust-count recurse-count *count*)))

;;; OK!