[plt-scheme] bug: segfault on ephemeron/weak hash table usage

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Tue Jun 19 21:27:18 EDT 2007

Hi everyone,

(http://bugs.plt-scheme.org doesn't appear to be working?)


I ran into an ugly bug in mzscheme 370.  Here's the code that exhibits the 
problem:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module hash-cons mzscheme
   ;; Code pretty much inspired from:
   ;;
   ;; http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/hashcons.scm
   ;;
   ;; I'm trying to take care to use ephemerons to keep leaks from
   ;; happening.

   (define the-elt (cons #f #f))
   (define ht (make-hash-table 'weak 'equal))
   (define sema (make-semaphore 1))

   (provide hash-cons test)

   (define (hash-cons first rest)
     (call-with-semaphore
      sema
      (lambda ()
        (set-car! the-elt first)
        (set-cdr! the-elt rest)
        (let ([v (hash-table-get ht the-elt #f)])
          (cond
            [v
             (let ([v (ephemeron-value v)])
               (cond
                 [v (unbox v)]
                 [else
                  (add-to-hash! first rest)]))]
            [else
             (add-to-hash! first rest)])))))


   (define (add-to-hash! first rest)
     (let ([new-elt (cons first rest)])
       (hash-table-put! ht new-elt (make-ephemeron new-elt (box new-elt)))
       new-elt))


   ;; sample test code
   (define (test)
     (define x (hash-cons 1 2))
     (define y (hash-cons 1 2))
     (display (eq? x y))
     (display (hash-table-map ht (lambda (k v) k)))
     (collect-garbage)
     (display (hash-table-map ht (lambda (k v) k)))
     (set! x #f)
     (set! y #f)
     (collect-garbage)
     (display (hash-table-map ht (lambda (k v) k)))
     (eq? (hash-cons 4 5) (hash-cons 5 6))
     (display (hash-table-map ht (lambda (k v) k)))
     (collect-garbage)
     (display (hash-table-map ht (lambda (k v) k)))
     (newline)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


The test code is a little silly, but oh well.  I'm seeing segfaults after 
trying to run 'test'.  On my Mac OS X (10.4.9) system (Intel):

###################################
mithril:~/work/projects/plt-misc/hash-cons dyoo$ /Applications/PLT\ 
Scheme\ v370/bin/mzscheme
Welcome to MzScheme v370 [3m], Copyright (c) 2004-2007 PLT Scheme Inc.
> (require "hash-cons.ss")
> (test)
Seg fault (internal error) at 0x0
Bus error
###################################


This is also an issue from svn:

###################################
mithril:~/work/projects/plt-misc/hash-cons dyoo$ mzscheme
Welcome to MzScheme v370.2 [3m], Copyright (c) 2004-2007 PLT Scheme Inc.
> (require "hash-cons.ss")
> (test)
Seg fault (internal error) at 0x0
Bus error
###################################



I do not see this crashing behavior on v360:
##########################################
mithril:~/work/projects/plt-misc/hash-cons dyoo$ /Applications/PLT\ 
Scheme\ v360/bin/mzscheme
Welcome to MzScheme version 360, Copyright (c) 2004-2006 PLT Scheme Inc.
> (require "hash-cons.ss")
> (test)
#t((1 . 2))((1 . 2))((1 . 2))((5 . 6) (4 . 5) (1 . 2))((5 . 6) (4 . 5) (1 
. 2))
##########################################



I hope this helps!


Posted on the users mailing list.