1.0.25.50: detect binding and alien stack exhaustion
[sbcl/llvm.git] / tests / exhaust.impure.lisp
blobef21e7c0351ced9b9272bce6929ebe13ef282681
1 ;;;; tests of the system's ability to catch resource exhaustion problems
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (cl:in-package :cl-user)
16 ;;; Prior to sbcl-0.7.1.38, doing something like (RECURSE), even in
17 ;;; safe code, would crash the entire Lisp process. Then the soft
18 ;;; stack checking was introduced, which checked (in safe code) for
19 ;;; stack exhaustion at each lambda.
21 ;;; Post 0.7.6.1, this was rewritten to use mprotect()-based stack
22 ;;; protection which does not require lisp code to check anything,
23 ;;; and works at all optimization settings. However, it now signals a
24 ;;; STORAGE-CONDITION instead of an ERROR.
26 (defun recurse ()
27 (recurse)
28 (recurse))
30 (defvar *count* 100)
32 ;;; Base-case: detecting exhaustion
33 (assert (eq :exhausted
34 (handler-case
35 (recurse)
36 (storage-condition (c)
37 (declare (ignore c))
38 :exhausted))))
40 ;;; Check that non-local control transfers restore the stack
41 ;;; exhaustion checking after unwinding -- and that previous test
42 ;;; didn't break it.
43 (let ((exhaust-count 0)
44 (recurse-count 0))
45 (tagbody
46 :retry
47 (handler-bind ((storage-condition (lambda (c)
48 (declare (ignore c))
49 (if (= *count* (incf exhaust-count))
50 (go :stop)
51 (go :retry)))))
52 (incf recurse-count)
53 (recurse))
54 :stop)
55 (assert (= exhaust-count recurse-count *count*)))
57 ;;; Check that we can safely use user-provided restarts to
58 ;;; unwind.
59 (let ((exhaust-count 0)
60 (recurse-count 0))
61 (block nil
62 (handler-bind ((storage-condition (lambda (c)
63 (declare (ignore c))
64 (if (= *count* (incf exhaust-count))
65 (return)
66 (invoke-restart (find-restart 'ok))))))
67 (loop
68 (with-simple-restart (ok "ok")
69 (incf recurse-count)
70 (recurse)))))
71 (assert (= exhaust-count recurse-count *count*)))
73 (with-test (:name (:exhaust :binding-stack))
74 (let ((ok nil)
75 (symbols (loop repeat 1024 collect (gensym)))
76 (values (loop repeat 1024 collect nil)))
77 (gc :full t)
78 (labels ((exhaust-binding-stack (i)
79 (progv symbols values
80 (exhaust-binding-stack (1+ i)))))
81 (handler-case
82 (exhaust-binding-stack 0)
83 (sb-kernel::binding-stack-exhausted ()
84 (setq ok t)))
85 (assert ok))))
87 #+c-stack-is-control-stack
88 (with-test (:name (:exhaust :alien-stack))
89 (let ((ok nil))
90 (labels ((exhaust-alien-stack (i)
91 (with-alien ((integer-array (array int 500)))
92 (+ (deref integer-array 0)
93 (exhaust-alien-stack (1+ i))))))
94 (handler-case
95 (exhaust-alien-stack 0)
96 (sb-kernel::alien-stack-exhausted ()
97 (setq ok t)))
98 (assert ok))))
100 ;;; OK!