[Openmcl-devel] odd behaviour of #_malloc and length?

Gary Byers gb at clozure.com
Thu Jan 12 08:56:02 PST 2006



On Thu, 12 Jan 2006, rs wrote:

> i know there is ccl::malloc, but...
> when i use length on a local variable containing a string in a
> #_malloc call it errors out with:
>   value NIL is not of the expected type (UNSIGNED-BYTE 32).
>
> (let* ((a "a string")
>           (mem (#_malloc (+ (length a) 1))))
>   (format t "mem is ~s" mem)
>   (when mem
>     (#_free mem)))
>
> -> value NIL is not of the expected type (UNSIGNED-BYTE 32).
>


It's a compiler bug; if the value of a foreign argument of certain
types (including :UNSIGNED-FULLWORD) is obtained from a fixnum
arithmetic operation that may overflow (and the overflow handling
is done out of line), the compiler neglects to store the result
of that operation in the right register before trying to initialize
the foreign argument from that register.  (Yes, that's the short
version ...)

> all of the following forms are ok (no error)

Some of the cases below avoid the bug because the LENGTH operation
gets constant-folded.  Some of the other cases may not error,
but may still suffer from the same bug: the register that doesn't
get used correctly might have -some- value of type (UNSIGNED-BYTE 32)
in it; it might or might not be the -correct- value at that point
in time.

This is pretty nasty (and I think that it's been lurking around for
a fairly long time.)  Let me try to do a little testing (not an
entirely foreign concept ...) and I'll try to check in fixes to
both the main and bleeding-edge CVS trees later today.

>
> (let* ((mem (#_malloc (+ (length "a string") 1))))
>
> (let* ((mem (#_malloc (+ (length "a string") 1))))
>
> (let* ((a "a string")
>           (mem (#_malloc (+ (array-total-size a) 1))))
>
> (let* ((a "a string")
>        (mem (#_malloc (+ (ccl::uv-size a) 1))))
>
> (let* ((a '(1 2 3 4))
>           (mem (#_malloc (+ (length a) 1))))
>
> (let* ((a "a string")
>           (len (+ (length a) 1))
>           (mem (#_malloc len)))
>
>
>
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/mailman/listinfo/openmcl-devel
>
>



More information about the Openmcl-devel mailing list