Update tests for threaded windows builds
[sbcl.git] / tests / debug.impure.lisp
1 ;;;; This file is for testing debugging functionality, using
2 ;;;; test machinery which might have side effects (e.g.
3 ;;;; executing DEFUN).
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; While most of SBCL is derived from the CMU CL system, the test
9 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; from CMU CL.
11 ;;;;
12 ;;;; This software is in the public domain and is provided with
13 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
14 ;;;; more information.
15
16 (cl:in-package :cl-user)
17
18 ;;; The debugger doesn't have any native knowledge of the interpreter
19 (when (eq sb-ext:*evaluator-mode* :interpret)
20   (sb-ext:exit :code 104))
21
22 \f
23 ;;;; Check that we get debug arglists right.
24
25 (defvar *p* (namestring *load-truename*))
26
27 ;;; FIXME: This should use some get-argslist like functionality that
28 ;;; we actually export.
29 ;;;
30 ;;; Return the debug arglist of the function object FUN as a list, or
31 ;;; punt with :UNKNOWN.
32 (defun get-arglist (fun)
33   (declare (type function fun))
34   ;; The Lisp-level type FUNCTION can conceal a multitude of sins..
35   (case (sb-kernel:widetag-of fun)
36     (#.sb-vm:simple-fun-header-widetag
37       (sb-kernel:%simple-fun-arglist fun))
38     (#.sb-vm:closure-header-widetag (get-arglist
39                                      (sb-kernel:%closure-fun fun)))
40     ;; In code/describe.lisp, ll. 227 (%describe-fun), we use a scheme
41     ;; like above, and it seems to work. -- MNA 2001-06-12
42     ;;
43     ;; (There might be other cases with arglist info also.
44     ;; SIMPLE-FUN-HEADER-WIDETAG and CLOSURE-HEADER-WIDETAG just
45     ;; happen to be the two case that I had my nose rubbed in when
46     ;; debugging a GC problem caused by applying %SIMPLE-FUN-ARGLIST to
47     ;; a closure. -- WHN 2001-06-05)
48     (t
49      #+sb-eval
50      (if (typep fun 'sb-eval::interpreted-function)
51          (sb-eval::interpreted-function-lambda-list fun)
52          :unknown)
53      #-sb-eval
54      :unknown)))
55
56 (defun zoop (zeep &key beep)
57   blurp)
58 (assert (equal (get-arglist #'zoop) '(zeep &key beep)))
59
60 ;;; Check some predefined functions too.
61 ;;;
62 ;;; (We don't know exactly what the arguments are, e.g. the first
63 ;;; argument of PRINT might be SB-IMPL::OBJECT or SB-KERNEL::OBJ or
64 ;;; whatever. But we do know the general structure that a correct
65 ;;; answer should have, so we can safely do a lot of checks.)
66 (with-test (:name :predefined-functions-1)
67   (destructuring-bind (object-sym &optional-sym stream-sym) (get-arglist #'print)
68     (assert (symbolp object-sym))
69     (assert (eql &optional-sym '&optional))
70     (assert (symbolp stream-sym))))
71 (with-test (:name :predefined-functions-2)
72   (destructuring-bind (dest-sym control-sym &rest-sym format-args-sym)
73       (get-arglist #'format)
74     (assert (symbolp dest-sym))
75     (assert (symbolp control-sym))
76     (assert (eql &rest-sym '&rest))
77     (assert (symbolp format-args-sym))))
78
79 ;;; Check for backtraces generally being correct.  Ensure that the
80 ;;; actual backtrace finishes (doesn't signal any errors on its own),
81 ;;; and that it contains the frames we expect, doesn't contain any
82 ;;; "bogus stack frame"s, and contains the appropriate toplevel call
83 ;;; and hasn't been cut off anywhere.
84 (defun verify-backtrace (test-function frame-specs &key (allow-stunted nil))
85   (labels ((args-equal (want real)
86              (cond ((eq '&rest (car want))
87                     t)
88                    ((endp want)
89                     (endp real))
90                    ((or (eq '? (car want)) (equal (car want) (car real)))
91                     (args-equal (cdr want) (cdr real)))
92                    (t
93                     nil))))
94     (let ((result nil))
95       (block outer-handler
96         (handler-bind
97             ((error (lambda (condition)
98                       ;; find the part of the backtrace we're interested in
99                       (let* ((full-backtrace (sb-debug:backtrace-as-list))
100                              (backtrace (member (caar frame-specs) full-backtrace
101                                                 :key #'car
102                                                 :test #'equal)))
103
104                         (setf result condition)
105
106                         (unless backtrace
107                           (format t "~&//~S not in backtrace:~%   ~S~%"
108                                   (caar frame-specs)
109                                   full-backtrace)
110                           (setf result nil))
111                         ;; check that we have all the frames we wanted
112                         (mapcar
113                          (lambda (spec frame)
114                            (unless (or (not spec)
115                                        (and (equal (car spec) (car frame))
116                                             (args-equal (cdr spec)
117                                                         (cdr frame))))
118                              (print (list :wanted spec :got frame))
119                              (setf result nil)))
120                          frame-specs
121                          backtrace)
122
123                         ;; Make sure the backtrace isn't stunted in
124                         ;; any way.  (Depends on running in the main
125                         ;; thread.) FIXME: On Windows we get two
126                         ;; extra foreign frames below regular frames.
127                         (unless (find '(sb-impl::toplevel-init) backtrace
128                                       :test #'equal)
129                           (print (list :backtrace-stunted backtrace))
130                           (setf result nil))
131                         (return-from outer-handler)))))
132           (funcall test-function)))
133       result)))
134
135 (defvar *undefined-function-frame*
136   ;; bug 353
137   '("undefined function"))
138
139 ;;; Test for "undefined function" (undefined_tramp) working properly.
140 ;;; Try it with and without tail call elimination, since they can have
141 ;;; different effects.  (Specifically, if undefined_tramp is incorrect
142 ;;; a stunted stack can result from the tail call variant.)
143 (flet ((optimized ()
144          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
145          (#:undefined-function 42))
146        (not-optimized ()
147          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
148          (#:undefined-function 42))
149        (test (fun)
150          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
151          (funcall fun)))
152
153   (with-test (:name (:undefined-function :bug-346)
154                     ;; Failures on ALPHA, SPARC, MIPS, and probably
155                     ;; HPPA are due to not having a full and valid
156                     ;; stack frame for the undefined function frame.
157                     ;; See PPC undefined_tramp for details.
158               :fails-on '(or :alpha :sparc :mips
159                           (and :x86-64 :freebsd)))
160     (assert (verify-backtrace
161              (lambda () (test #'optimized))
162              (list *undefined-function-frame*
163                    (list `(flet test :in ,*p*) #'optimized)))))
164
165   ;; bug 353: This test fails at least most of the time for x86/linux
166   ;; ca. 0.8.20.16. -- WHN
167   (with-test (:name (:undefined-function :bug-353))
168     (assert (verify-backtrace
169              (lambda () (test #'not-optimized))
170              (list *undefined-function-frame*
171                    (list `(flet not-optimized :in ,*p*))
172                    (list `(flet test :in ,*p*) #'not-optimized))))))
173
174 (with-test (:name :backtrace-interrupted-condition-wait
175             :skipped-on '(not :sb-thread)
176                   ;; For some unfathomable reason the backtrace becomes
177                   ;; stunted, ending at _sigtramp, when we add :TIMEOUT NIL to
178                   ;; the frame we expect. If we leave it out, the backtrace is
179                   ;; fine -- but the test fails. I can only boggle right now.
180             :fails-on '(or (and :x86 :linux)
181                            (and :win32 :sb-thread)))
182   (let ((m (sb-thread:make-mutex))
183         (q (sb-thread:make-waitqueue)))
184     (assert (verify-backtrace
185             (lambda ()
186               (sb-thread:with-mutex (m)
187                 (handler-bind ((timeout (lambda (c)
188                                           (error "foo"))))
189                   (with-timeout 0.1
190                     (sb-thread:condition-wait q m)))))
191             `((sb-thread:condition-wait ,q ,m :timeout nil))))))
192
193 ;;; Division by zero was a common error on PPC. It depended on the
194 ;;; return function either being before INTEGER-/-INTEGER in memory,
195 ;;; or more than MOST-POSITIVE-FIXNUM bytes ahead. It also depends on
196 ;;; INTEGER-/-INTEGER calling SIGNED-TRUNCATE. I believe Raymond Toy
197 ;;; says that the Sparc backend (at least for CMUCL) inlines this, so
198 ;;; if SBCL does the same this test is probably not good for the
199 ;;; Sparc.
200 ;;;
201 ;;; Disabling tail call elimination on this will probably ensure that
202 ;;; the return value (to the flet or the enclosing top level form) is
203 ;;; more than MOST-POSITIVE-FIXNUM with the current spaces on OS X.
204 ;;; Enabling it might catch other problems, so do it anyway.
205 (flet ((optimized ()
206          (declare (optimize (speed 2) (debug 1))) ; tail call elimination
207          (/ 42 0))
208        (not-optimized ()
209          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
210          (/ 42 0))
211        (test (fun)
212          (declare (optimize (speed 1) (debug 2))) ; no tail call elimination
213          (funcall fun)))
214   (with-test (:name (:divide-by-zero :bug-346)
215               :fails-on :alpha)  ; bug 346
216     (assert (verify-backtrace (lambda () (test #'optimized))
217                               (list '(/ 42 &rest)
218                                     (list `(flet test :in ,*p*) #'optimized)))))
219   (with-test (:name (:divide-by-zero :bug-356)
220               :fails-on :alpha)  ; bug 356
221     (assert (verify-backtrace (lambda () (test #'not-optimized))
222                               (list '(/ 42 &rest)
223                                     `((flet not-optimized :in ,*p*))
224                                     (list `(flet test :in ,*p*) #'not-optimized))))))
225
226 (with-test (:name (:throw :no-such-tag)
227             :fails-on '(or
228                         (and :sparc :linux)
229                         :alpha
230                         :mips))
231   (progn
232     (defun throw-test ()
233       (throw 'no-such-tag t))
234     (assert (verify-backtrace #'throw-test '((throw-test))))))
235
236 (defun bug-308926 (x)
237   (let ((v "foo"))
238     (flet ((bar (z)
239              (oops v z)
240              (oops z v)))
241       (bar x)
242       (bar v))))
243
244 (with-test (:name :bug-308926)
245   (assert (verify-backtrace (lambda () (bug-308926 13))
246                             '(((flet bar :in bug-308926) 13)
247                               (bug-308926 &rest t)))))
248
249 ;;; test entry point handling in backtraces
250
251 (defun oops ()
252   (error "oops"))
253
254 (with-test (:name :xep-too-many-arguments)
255   (assert (verify-backtrace (lambda () (oops 1 2 3 4 5 6))
256                             '((oops ? ? ? ? ? ?)))))
257
258 (defmacro defbt (n ll &body body)
259   ;; WTF is this? This is a way to make these tests not depend so much on the
260   ;; details of LOAD/EVAL. Around 1.0.57 we changed %SIMPLE-EVAL to be
261   ;; slightly smarter, which meant that things which used to have xeps
262   ;; suddently had tl-xeps, etc. This takes care of that.
263   `(funcall
264     (compile nil
265              '(lambda ()
266                (progn
267                  ;; normal debug info
268                  (defun ,(intern (format nil "BT.~A.1" n)) ,ll
269                    ,@body)
270                  ;; no arguments saved
271                  (defun ,(intern (format nil "BT.~A.2" n)) ,ll
272                    (declare (optimize (debug 1) (speed 3)))
273                    ,@body)
274                  ;; no lambda-list saved
275                  (defun ,(intern (format nil "BT.~A.3" n)) ,ll
276                    (declare (optimize (debug 0)))
277                    ,@body))))))
278
279 (defbt 1 (&key key)
280   (list key))
281
282 (defbt 2 (x)
283   (list x))
284
285 (defbt 3 (&key (key (oops)))
286   (list key))
287
288 ;;; ERROR instead of OOPS so that tail call elimination doesn't happen
289 (defbt 4 (&optional opt)
290   (list (error "error")))
291
292 (defbt 5 (&optional (opt (oops)))
293   (list opt))
294
295 (defmacro with-details (bool &body body)
296   `(let ((sb-debug:*show-entry-point-details* ,bool))
297      ,@body))
298
299 (defun bug-354 (x)
300   (error "XEPs in backtraces: ~S" x))
301
302 (with-test (:name :bug-354)
303   (with-details t
304     (assert (not (verify-backtrace (lambda () (bug-354 354))
305                                    '((bug-354 &rest)
306                                      ((sb-c::tl-xep bug-354) &rest))))))
307   (assert (verify-backtrace (lambda () (bug-354 354)) '((bug-354 354)))))
308
309 ;;; FIXME: This test really should be broken into smaller pieces
310 (with-test (:name (:backtrace :tl-xep))
311   (with-details t
312     (assert (verify-backtrace #'namestring
313                               '(((sb-c::tl-xep namestring) 0 ?)))))
314   (with-details nil
315     (assert (verify-backtrace #'namestring
316                               '((namestring))))))
317
318 (with-test (:name (:backtrace :more-processor))
319   (with-details t
320     (assert (verify-backtrace (lambda () (bt.1.1 :key))
321                               '(((sb-c::&more-processor bt.1.1) &rest))))
322     (assert (verify-backtrace (lambda () (bt.1.2 :key))
323                               '(((sb-c::&more-processor bt.1.2) &rest))))
324     (assert (verify-backtrace (lambda () (bt.1.3 :key))
325                               '(((sb-c::&more-processor bt.1.3) &rest)))))
326   (with-details nil
327     (assert (verify-backtrace (lambda () (bt.1.1 :key))
328                               '((bt.1.1 :key))))
329     (assert (verify-backtrace (lambda () (bt.1.2 :key))
330                               '((bt.1.2 &rest))))
331     (assert (verify-backtrace (lambda () (bt.1.3 :key))
332                               '((bt.1.3 &rest))))))
333
334 (with-test (:name (:backtrace :xep))
335   (with-details t
336     (assert (verify-backtrace #'bt.2.1
337                               '(((sb-c::xep bt.2.1) 0 ?))))
338     (assert (verify-backtrace #'bt.2.2
339                               '(((sb-c::xep bt.2.2) &rest))))
340     (assert (verify-backtrace #'bt.2.3
341                               '(((sb-c::xep bt.2.3) &rest)))))
342   (with-details nil
343     (assert (verify-backtrace #'bt.2.1
344                               '((bt.2.1))))
345     (assert (verify-backtrace #'bt.2.2
346                               '((bt.2.2 &rest))))
347     (assert (verify-backtrace #'bt.2.3
348                               '((bt.2.3 &rest))))))
349
350 ;;; This test is somewhat deceptively named. Due to confusion in debug naming
351 ;;; these functions used to have sb-c::varargs-entry debug names for their
352 ;;; main lambda.
353 (with-test (:name (:backtrace :varargs-entry))
354   (with-details t
355     (assert (verify-backtrace #'bt.3.1
356                               '((bt.3.1 :key nil))))
357     (assert (verify-backtrace #'bt.3.2
358                               '((bt.3.2 :key ?))))
359     (assert (verify-backtrace #'bt.3.3
360                               '((bt.3.3 &rest)))))
361   (with-details nil
362     (assert (verify-backtrace #'bt.3.1
363                               '((bt.3.1 :key nil))))
364     (assert (verify-backtrace #'bt.3.2
365                               '((bt.3.2 :key ?))))
366     (assert (verify-backtrace #'bt.3.3
367                               '((bt.3.3 &rest))))))
368
369 ;;; This test is somewhat deceptively named. Due to confusion in debug naming
370 ;;; these functions used to have sb-c::hairy-args-processor debug names for
371 ;;; their main lambda.
372 (with-test (:name (:backtrace :hairy-args-processor))
373   (with-details t
374     (assert (verify-backtrace #'bt.4.1
375                               '((bt.4.1 ?))))
376     (assert (verify-backtrace #'bt.4.2
377                               '((bt.4.2 ?))))
378     (assert (verify-backtrace #'bt.4.3
379                               '((bt.4.3 &rest)))))
380   (with-details nil
381     (assert (verify-backtrace #'bt.4.1
382                               '((bt.4.1 ?))))
383     (assert (verify-backtrace #'bt.4.2
384                               '((bt.4.2 ?))))
385     (assert (verify-backtrace #'bt.4.3
386                               '((bt.4.3 &rest))))))
387
388
389 (with-test (:name (:backtrace :optional-processor))
390   (with-details t
391     (assert (verify-backtrace #'bt.5.1
392                               '(((sb-c::&optional-processor bt.5.1)))))
393     (assert (verify-backtrace #'bt.5.2
394                               '(((sb-c::&optional-processor bt.5.2) &rest))))
395     (assert (verify-backtrace #'bt.5.3
396                               '(((sb-c::&optional-processor bt.5.3) &rest)))))
397   (with-details nil
398     (assert (verify-backtrace #'bt.5.1
399                               '((bt.5.1))))
400     (assert (verify-backtrace #'bt.5.2
401                               '((bt.5.2 &rest))))
402     (assert (verify-backtrace #'bt.5.3
403                               '((bt.5.3 &rest))))))
404
405 (write-line "//compile nil")
406 (defvar *compile-nil-error* (compile nil '(lambda (x) (cons (when x (error "oops")) nil))))
407 (defvar *compile-nil-non-tc* (compile nil '(lambda (y) (cons (funcall *compile-nil-error* y) nil))))
408 (with-test (:name (:compile nil))
409   (assert (verify-backtrace (lambda () (funcall *compile-nil-non-tc* 13))
410                             `(((lambda (x) :in ,*p*) 13)
411                               ((lambda (y) :in ,*p*) 13)))))
412
413 (with-test (:name :clos-slot-typecheckfun-named)
414   (assert
415    (verify-backtrace
416     (lambda ()
417       (eval `(locally (declare (optimize safety))
418                (defclass clos-typecheck-test ()
419                  ((slot :type fixnum)))
420                (setf (slot-value (make-instance 'clos-typecheck-test) 'slot) t))))
421     '(((sb-pcl::slot-typecheck fixnum) t)))))
422
423 (with-test (:name :clos-emf-named)
424   (assert
425    (verify-backtrace
426     (lambda ()
427       (eval `(progn
428                (defmethod clos-emf-named-test ((x symbol)) x)
429                (defmethod clos-emf-named-test :before (x) (assert x))
430                (clos-emf-named-test nil))))
431     '(((sb-pcl::emf clos-emf-named-test) ? ? nil)))))
432
433 (with-test (:name :bug-310173)
434   (flet ((make-fun (n)
435            (let* ((names '(a b))
436                   (req (loop repeat n collect (pop names))))
437              (compile nil
438                       `(lambda (,@req &rest rest)
439                          (let ((* *)) ; no tail-call
440                            (apply '/ ,@req rest)))))))
441     (assert
442      (verify-backtrace (lambda ()
443                          (funcall (make-fun 0) 10 11 0))
444                        `((sb-kernel:two-arg-/ 10/11 0)
445                          (/ 10 11 0)
446                          ((lambda (&rest rest) :in ,*p*) 10 11 0))))
447     (assert
448      (verify-backtrace (lambda ()
449                          (funcall (make-fun 1) 10 11 0))
450                        `((sb-kernel:two-arg-/ 10/11 0)
451                          (/ 10 11 0)
452                          ((lambda (a &rest rest) :in ,*p*) 10 11 0))))
453     (assert
454      (verify-backtrace (lambda ()
455                          (funcall (make-fun 2) 10 11 0))
456                        `((sb-kernel:two-arg-/ 10/11 0)
457                          (/ 10 11 0)
458                          ((lambda (a b &rest rest) :in ,*p*) 10 11 0))))))
459
460 ;;;; test TRACE
461
462 (defun trace-this ()
463   'ok)
464
465 (defun trace-fact (n)
466   (if (zerop n)
467       1
468       (* n (trace-fact (1- n)))))
469
470 (with-test (:name (trace :simple))
471   (let ((out (with-output-to-string (*trace-output*)
472                (trace trace-this)
473                (assert (eq 'ok (trace-this)))
474                (untrace))))
475     (assert (search "TRACE-THIS" out))
476     (assert (search "returned OK" out))))
477
478 ;;; bug 379
479 ;;; This is not a WITH-TEST :FAILS-ON PPC DARWIN since there are
480 ;;; suspicions that the breakpoint trace might corrupt the whole image
481 ;;; on that platform.
482 (with-test (:name (trace :encapsulate nil)
483             :fails-on '(or (and :ppc (not :linux)) :sparc :mips)
484             :broken-on '(or :darwin :sunos))
485   (let ((out (with-output-to-string (*trace-output*)
486                (trace trace-this :encapsulate nil)
487                (assert (eq 'ok (trace-this)))
488                (untrace))))
489     (assert (search "TRACE-THIS" out))
490     (assert (search "returned OK" out))))
491
492 (with-test (:name (trace-recursive :encapsulate nil)
493             :fails-on '(or (and :ppc (not :linux)) :sparc :mips :sunos)
494             :broken-on '(or :darwin (and :x86 :sunos)))
495   (let ((out (with-output-to-string (*trace-output*)
496                (trace trace-fact :encapsulate nil)
497                (assert (= 120 (trace-fact 5)))
498                (untrace))))
499     (assert (search "TRACE-FACT" out))
500     (assert (search "returned 1" out))
501     (assert (search "returned 120" out))))
502
503 (defun trace-and-fmakunbound-this (x)
504   x)
505
506 (with-test (:name :bug-667657)
507   (trace trace-and-fmakunbound-this)
508   (fmakunbound 'trace-and-fmakunbound-this)
509   (untrace)
510   (assert (not (trace))))
511
512 (with-test (:name :bug-414)
513   (handler-bind ((warning #'error))
514     (load (compile-file "bug-414.lisp"))
515     (disassemble 'bug-414)))
516
517 (with-test (:name :bug-310175 :fails-on '(not :stack-allocatable-lists))
518   ;; KLUDGE: Not all DX-enabled platforms DX CONS, and the compiler
519   ;; transforms two-arg-LIST* (and one-arg-LIST) to CONS.  Therefore,
520   ;; use two-arg-LIST, which should get through to VOP LIST, and thus
521   ;; stack-allocate on a predictable set of machines.
522   (let ((dx-arg (list t t)))
523     (declare (dynamic-extent dx-arg))
524     (flet ((dx-arg-backtrace (x)
525              (declare (optimize (debug 2)))
526              (prog1 (sb-debug:backtrace-as-list 10)
527                (assert (sb-debug::stack-allocated-p x)))))
528       (declare (notinline dx-arg-backtrace))
529       (assert (member-if (lambda (frame)
530                            (and (consp frame)
531                                 (consp (car frame))
532                                 (equal '(flet dx-arg-backtrace :in) (butlast (car frame)))
533                                 (notany #'sb-debug::stack-allocated-p (cdr frame))))
534                          (dx-arg-backtrace dx-arg))))))
535
536 (with-test (:name :bug-795245)
537   (assert
538    (eq :ok
539        (catch 'done
540          (handler-bind
541              ((error (lambda (e)
542                        (declare (ignore e))
543                        (handler-case
544                            (sb-debug:backtrace 100 (make-broadcast-stream))
545                          (error ()
546                            (throw 'done :error))
547                          (:no-error ()
548                            (throw 'done :ok))))))
549            (apply '/= nil 1 2 nil))))))
550
551 ;;;; test infinite error protection
552
553 (defmacro nest-errors (n-levels error-form)
554   (if (< 0 n-levels)
555       `(handler-bind ((error (lambda (condition)
556                                (declare (ignore condition))
557                                ,error-form)))
558         (nest-errors ,(1- n-levels) ,error-form))
559       error-form))
560
561 (defun erroring-debugger-hook (condition old-debugger-hook)
562   (let ((*debugger-hook* old-debugger-hook))
563     (format t "recursive condition: ~A~%" condition) (force-output)
564     (error "recursive condition: ~A" condition)))
565
566 (defun test-inifinite-error-protection ()
567   ;; after 50 successful throws to SB-IMPL::TOPLEVEL-CATCHER sbcl used
568   ;; to halt, it produces so much garbage that's hard to suppress that
569   ;; it is tested only once
570   (write-line "--HARMLESS BUT ALARMING BACKTRACE COMING UP--")
571   (let ((*debugger-hook* #'erroring-debugger-hook))
572     (loop repeat 1 do
573           (let ((error-counter 0)
574                 (*terminal-io* (make-broadcast-stream)))
575             (assert
576              (not (eq
577                    :normal-exit
578                    (catch 'sb-impl::toplevel-catcher
579                      (nest-errors 20 (error "infinite error ~s"
580                                             (incf error-counter)))
581                      :normal-exit)))))))
582   (write-line "--END OF H-B-A-B--"))
583
584 (with-test (:name infinite-error-protection)
585   (enable-debugger)
586   (test-inifinite-error-protection))
587
588 (with-test (:name (infinite-error-protection :thread)
589                   :skipped-on '(not :sb-thread))
590   (enable-debugger)
591   (let ((thread (sb-thread:make-thread #'test-inifinite-error-protection)))
592     (loop while (sb-thread:thread-alive-p thread))))
593
594 ;; unconditional, in case either previous left it enabled
595 (disable-debugger)
596 \f
597 ;;;; test some limitations of MAKE-LISP-OBJ
598
599 ;;; Older GENCGC systems had a bug in the pointer validation used by
600 ;;; MAKE-LISP-OBJ that made SIMPLE-FUN objects always fail to
601 ;;; validate.
602 (with-test (:name (make-lisp-obj :simple-funs))
603   (sb-sys:without-gcing
604     (assert (eq #'identity
605                 (sb-kernel:make-lisp-obj
606                  (sb-kernel:get-lisp-obj-address
607                   #'identity))))))
608
609 ;;; Older CHENEYGC systems didn't perform any real pointer validity
610 ;;; checks beyond "is this pointer to somewhere in heap space".
611 (with-test (:name (make-lisp-obj :pointer-validation))
612   ;; Fun and games: We need to test MAKE-LISP-OBJ with a known-bogus
613   ;; address, but we also need the GC to not pitch a fit if it sees an
614   ;; object with said bogus address.  Thus, construct our known-bogus
615   ;; object within an area of unboxed storage (a vector) in static
616   ;; space.  We'll make it a simple object, (CONS 0 0), which has an
617   ;; in-memory representation of two consecutive zero words.  We
618   ;; allocate a three-word vector so that we can guarantee a
619   ;; double-word aligned double-word of zeros no matter what happens
620   ;; with the vector-data-offset (currently double-word aligned).
621   (let* ((memory (sb-int:make-static-vector 3 :element-type `(unsigned-byte ,sb-vm:n-word-bits)
622                                             :initial-element 0))
623          (vector-data-address (sb-sys:sap-int (sb-kernel::vector-sap memory)))
624          (object-base-address (logandc2 (+ vector-data-address sb-vm:lowtag-mask) sb-vm:lowtag-mask))
625          (object-tagged-address (+ object-base-address sb-vm:list-pointer-lowtag)))
626     (multiple-value-bind (object valid-p)
627         (sb-kernel:make-lisp-obj object-tagged-address nil)
628       (declare (ignore object))
629       (assert (not valid-p)))))
630
631 (defun test-debugger (control form &rest targets)
632   (let ((out (make-string-output-stream))
633         (oops t))
634     (unwind-protect
635          (progn
636            (with-simple-restart (debugger-test-done! "Debugger Test Done!")
637              (let* ((*debug-io* (make-two-way-stream
638                                  (make-string-input-stream control)
639                                  (make-broadcast-stream out #+nil *standard-output*)))
640                     ;; Initial announcement goes to *ERROR-OUTPUT*
641                     (*error-output* *debug-io*)
642                     (*invoke-debugger-hook* nil))
643                (handler-bind ((error #'invoke-debugger))
644                  (eval form))))
645            (setf oops nil))
646       (when oops
647         (error "Uncontrolled unwind from debugger test.")))
648     ;; For sanity's sake this is outside the *debug-io* rebinding -- otherwise
649     ;; it could swallow our asserts!
650     (with-input-from-string (s (get-output-stream-string out))
651       (loop for line = (read-line s nil)
652             while line
653             do (assert targets)
654                #+nil
655                (format *error-output* "Got: ~A~%" line)
656                (let ((match (pop targets)))
657                  (if (eq '* match)
658                      ;; Whatever, till the next line matches.
659                      (let ((text (pop targets)))
660                        #+nil
661                        (format *error-output* "Looking for: ~A~%" text)
662                        (unless (search text line)
663                          (push text targets)
664                          (push match targets)))
665                      (unless (search match line)
666                        (format *error-output* "~&Wanted: ~S~%   Got: ~S~%" match line)
667                        (setf oops t))))))
668     ;; Check that we saw everything we wanted
669     (when targets
670       (error "Missed: ~S" targets))
671     (assert (not oops))))
672
673 (with-test (:name (:debugger :source 1))
674   (test-debugger
675    "d
676     source 0
677     debugger-test-done!"
678    `(progn
679       (defun this-will-break (x)
680                (declare (optimize debug))
681                (let* ((y (- x x))
682                       (z (/ x y)))
683                  (+ x z)))
684       (this-will-break 1))
685    '*
686    "debugger invoked"
687    '*
688    "DIVISION-BY-ZERO"
689    "operands (1 0)"
690    '*
691    "INTEGER-/-INTEGER"
692    "(THIS-WILL-BREAK 1)"
693    "1]"
694    "(/ X Y)"
695    "1]"))
696
697 (with-test (:name (:debugger :source 2))
698   (test-debugger
699    "d
700     source 0
701     debugger-test-done!"
702    `(locally (declare (optimize (speed 0) (safety 3) (debug 3)))
703       (let ((f #'(lambda (x cont)
704                    (print x (make-broadcast-stream))
705                    (if (zerop x)
706                        (error "~%foo")
707                        (funcall cont (1- x) cont)))))
708         (funcall f 10 f)))
709    '*
710    "debugger"
711    '*
712    "foo"
713    '*
714    "source: (ERROR \"~%foo\")"
715    '*
716    "(LAMBDA (X CONT)"
717    '*
718    "(FUNCALL CONT (1- X) CONT)"
719    "1]"))
720
721 (with-test (:name (disassemble :high-debug-eval))
722   (eval `(defun this-will-be-disassembled (x)
723            (declare (optimize debug))
724            (+ x x)))
725   (let* ((oopses (make-string-output-stream))
726          (disassembly
727            (let ((*error-output* oopses))
728              (with-output-to-string (*standard-output*)
729                (disassemble 'this-will-be-disassembled)))))
730     (with-input-from-string (s disassembly)
731       (assert (search "; disassembly for THIS-WILL-BE-DISASSEMBLED"
732                       (read-line s))))
733     (let ((problems (get-output-stream-string oopses)))
734       (unless (zerop (length problems))
735         (error problems)))))
736
737 (defun this-too-will-be-disasssembled (x)
738   (declare (optimize debug))
739   (+ x x))
740
741 (with-test (:name (disassemble :high-debug-load))
742   (let* ((oopses (make-string-output-stream))
743          (disassembly
744            (let ((*error-output* oopses))
745              (with-output-to-string (*standard-output*)
746                (disassemble 'this-too-will-be-disasssembled)))))
747     (with-input-from-string (s disassembly)
748       (assert (equal "; disassembly for THIS-TOO-WILL-BE-DISASSSEMBLED"
749                      (read-line s))))
750     (let ((problems (get-output-stream-string oopses)))
751       (unless (zerop (length problems))
752         (error problems)))))
753
754 (write-line "/debug.impure.lisp done")