Signal floating-point-overflow from bignum-to-float.
[sbcl.git] / tests / gc.impure.lisp
blob6a15305e98e65a97e2d19274351778717c267d27
1 ;;;; gc tests
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 ;;;; absoluely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (in-package :cl-user)
16 ;;; Make sure MAP-REFERENCING-OBJECTS doesn't spuriously treat raw bits as
17 ;;; potential pointers. Also make sure it sees the SYMBOL-INFO slot.
18 (defstruct afoo (slot nil :type sb-ext:word))
19 (defvar *afoo* (make-afoo :slot (sb-kernel:get-lisp-obj-address '*posix-argv*)))
20 (with-test (:name :map-referencing-objs)
21 (sb-vm::map-referencing-objects (lambda (x) (assert (not (typep x 'afoo))))
22 :dynamic '*posix-argv*)
23 (let ((v (sb-kernel:symbol-info 'satisfies)) referers)
24 (sb-vm::map-referencing-objects (lambda (referer) (push referer referers))
25 #+gencgc :dynamic #-gencgc :static v)
26 #+immobile-space
27 (sb-vm::map-referencing-objects (lambda (referer) (push referer referers))
28 :immobile v)
29 (assert (member 'satisfies referers))))
31 (defparameter *x* ())
33 (defun cons-madly ()
34 (loop repeat 10000 do
35 (setq *x* (make-string 100000))))
37 ;; check that WITHOUT-INTERRUPTS doesn't block the gc trigger
38 (sb-sys:without-interrupts (cons-madly))
40 ;; check that WITHOUT-INTERRUPTS doesn't block SIG_STOP_FOR_GC
41 #+sb-thread
42 (sb-sys:without-interrupts
43 (let ((thread (sb-thread:make-thread (lambda () (sb-ext:gc)))))
44 (loop while (sb-thread:thread-alive-p thread))))
46 (let ((gc-happend nil))
47 (push (lambda () (setq gc-happend t)) sb-ext:*after-gc-hooks*)
49 ;; check that WITHOUT-GCING defers explicit gc
50 (sb-sys:without-gcing
51 (gc)
52 (assert (not gc-happend)))
53 (assert gc-happend)
55 ;; check that WITHOUT-GCING defers SIG_STOP_FOR_GC
56 #+sb-thread
57 (let ((in-without-gcing nil))
58 (setq gc-happend nil)
59 (sb-thread:make-thread (lambda ()
60 (loop while (not in-without-gcing))
61 (sb-ext:gc)))
62 (sb-sys:without-gcing
63 (setq in-without-gcing t)
64 (sleep 3)
65 (assert (not gc-happend)))
66 ;; give the hook time to run
67 (sleep 1)
68 (assert gc-happend)))
70 ;;; SB-EXT:GENERATION-* accessors returned bogus values for generation > 0
71 (with-test (:name :bug-529014 :skipped-on (not :gencgc))
72 (loop for i from 0 to sb-vm:+pseudo-static-generation+
73 do (assert (= (sb-ext:generation-bytes-consed-between-gcs i)
74 (truncate (sb-ext:bytes-consed-between-gcs)
75 sb-vm:+highest-normal-generation+)))
76 ;; FIXME: These parameters are a) tunable in the source and b)
77 ;; duplicated multiple times there and now here. It would be good to
78 ;; OAOO-ify them (probably to src/compiler/generic/params.lisp).
79 (assert (= (sb-ext:generation-minimum-age-before-gc i) 0.75))
80 (assert (= (sb-ext:generation-number-of-gcs-before-promotion i) 1))))
82 (defun stress-gc ()
83 ;; Kludge or not? I don't know whether the smaller allocation size
84 ;; for sb-safepoint is a legitimate correction to the test case, or
85 ;; rather hides the actual bug this test is checking for... It's also
86 ;; not clear to me whether the issue is actually safepoint-specific.
87 ;; But the main problem safepoint-related bugs tend to introduce is a
88 ;; delay in the GC triggering -- and if bug-936304 fails, it also
89 ;; causes bug-981106 to fail, even though there is a full GC in
90 ;; between, which makes it seem unlikely to me that the problem is
91 ;; delay- (and hence safepoint-) related. --DFL
92 (let* ((x (make-array (truncate #-sb-safepoint (* 0.2 (dynamic-space-size))
93 #+sb-safepoint (* 0.1 (dynamic-space-size))
94 sb-vm:n-word-bytes))))
95 (elt x 0)))
97 (with-test (:name :bug-936304)
98 (gc :full t)
99 (assert (eq :ok (handler-case
100 (progn
101 (loop repeat 50 do (stress-gc))
102 :ok)
103 (storage-condition ()
104 :oom)))))
106 (with-test (:name :bug-981106)
107 (gc :full t)
108 (assert (eq :ok
109 (handler-case
110 (dotimes (runs 100 :ok)
111 (let* ((n (truncate (dynamic-space-size) 1200))
112 (len (length
113 (with-output-to-string (string)
114 (dotimes (i n)
115 (write-sequence "hi there!" string))))))
116 (assert (eql len (* n (length "hi there!"))))))
117 (storage-condition ()
118 :oom)))))
120 (with-test (:name :gc-logfile :skipped-on (not :gencgc))
121 (assert (not (gc-logfile)))
122 (let ((p #p"gc.log"))
123 (assert (not (probe-file p)))
124 (assert (equal p (setf (gc-logfile) p)))
125 (gc)
126 (let ((p2 (gc-logfile)))
127 (assert (equal (truename p2) (truename p))))
128 (assert (not (setf (gc-logfile) nil)))
129 (assert (not (gc-logfile)))
130 (delete-file p)))
132 #+immobile-code
133 (with-test (:name (sb-kernel::order-by-in-degree :uninterned-function-names))
134 ;; This creates two functions whose names are uninterned symbols and
135 ;; that are both referenced once, resulting in a tie
136 ;; w.r.t. ORDER-BY-IN-DEGREE. Uninterned symbols used to cause an
137 ;; error in the tie-breaker.
138 (let* ((sb-c::*compile-to-memory-space* :immobile)
139 (f (eval `(defun ,(gensym) ())))
140 (g (eval `(defun ,(gensym) ()))))
141 (eval `(defun h () (,f) (,g))))
142 (sb-kernel::order-by-in-degree))
144 (defparameter *pin-test-object* nil)
145 (defparameter *pin-test-object-address* nil)
147 (with-test (:name (sb-sys:with-pinned-objects :actually-pins-objects)
148 :skipped-on :cheneygc)
149 ;; The interpreters (both sb-eval and sb-fasteval) special-case
150 ;; WITH-PINNED-OBJECTS as a "special form", because the x86oid
151 ;; version of WITH-PINNED-OBJECTS uses black magic that isn't
152 ;; supportable outside of the compiler. The non-x86oid versions of
153 ;; WITH-PINNED-OBJECTS don't use black magic, but are overridden
154 ;; anyway. But the special-case logic was, historically broken, and
155 ;; this affects all gencgc targets (cheneygc isn't affected because
156 ;; cheneygc WITH-PINNED-OBJECTS devolves to WITHOUT-GCING).
158 ;; Our basic approach is to allocate some kind of object and stuff
159 ;; it where it doesn't need to be on the control stack. We then pin
160 ;; the object, take its address and store that somewhere as well,
161 ;; force a full GC, re-take the address, and see if it moved.
162 (locally (declare (notinline make-string)) ;; force full call
163 (setf *pin-test-object* (make-string 100)))
164 (sb-sys:with-pinned-objects (*pin-test-object*)
165 (setf *pin-test-object-address*
166 (sb-kernel:get-lisp-obj-address *pin-test-object*))
167 (gc :full t)
168 (assert (= (sb-kernel:get-lisp-obj-address *pin-test-object*)
169 *pin-test-object-address*))))
171 (defun ensure-code/data-separation ()
172 (let* ((n-bits (+ sb-vm:last-free-page 10))
173 (code-bits (make-array n-bits :element-type 'bit))
174 (data-bits (make-array n-bits :element-type 'bit))
175 (total-code-size 0))
176 (sb-vm::map-allocated-objects
177 (lambda (obj type size)
178 (declare ((and fixnum (integer 1)) size))
179 ;; M-A-O disables GC, therefore GET-LISP-OBJ-ADDRESS is safe
180 (let ((obj-addr (sb-kernel:get-lisp-obj-address obj))
181 (array (cond ((= type sb-vm:code-header-widetag)
182 (incf total-code-size size)
183 code-bits)
185 data-bits))))
186 ;; This is not the most efficient way to update the bit arrays,
187 ;; but the simplest and clearest for sure. (The loop could avoided
188 ;; if the current page is the same as the previously seen page)
189 (loop for index from (sb-vm::find-page-index obj-addr)
190 to (sb-vm::find-page-index (truly-the word
191 (+ (logandc2 obj-addr sb-vm:lowtag-mask)
192 (1- size))))
193 do (setf (sbit array index) 1))))
194 :dynamic)
195 (assert (not (find 1 (bit-and code-bits data-bits))))
196 (let* ((code-bytes-consumed
197 (* (count 1 code-bits) sb-vm:gencgc-card-bytes))
198 (waste
199 (- total-code-size code-bytes-consumed)))
200 ;; This should be true for all platforms.
201 ;; Some have as little as .5% space wasted.
202 (assert (<= waste (* 3/100 code-bytes-consumed))))))
204 (compile 'ensure-code/data-separation)
206 (with-test (:name :code/data-separation
207 :skipped-on (:not :gencgc))
208 (ensure-code/data-separation))
210 #+immobile-space
211 (with-test (:name :immobile-space-addr-p)
212 ;; Upper bound should be exclusive
213 (assert (not (sb-kernel:immobile-space-addr-p
214 (+ sb-vm:fixedobj-space-start
215 sb-vm:fixedobj-space-size
216 sb-vm:varyobj-space-size)))))
218 ;;; After each iteration of FOO there are a few pinned conses.
219 ;;; On alternate GC cycles, those get promoted to generation 1.
220 ;;; When the logic for page-spanning-object zeroing incorrectly decreased
221 ;;; the upper bound on bytes used for partially pinned pages, it caused
222 ;;; an accumulation of pages in generation 1 each with 2 objects' worth
223 ;;; of bytes, and the remainder waste. Because the waste was not accounted
224 ;;; for, it did not trigger GC enough to avoid heap exhaustion.
225 (with-test (:name :smallobj-auto-gc-trigger)
226 ;; Ensure that these are compiled functions because the interpreter
227 ;; would make lots of objects of various sizes which is insufficient
228 ;; to provoke the bug.
229 (setf (symbol-function 'foo)
230 (compile nil '(lambda () (list 1 2))))
231 ;; 500 million iterations of this loop seems to be reliable enough
232 ;; to show that GC happens.
233 (setf (symbol-function 'callfoo)
234 (compile nil '(lambda () (loop repeat 500000000 do (foo)))))
235 (funcall 'callfoo))