Discussion:
[Larceny-users] error peeking short values from raw memory
Marco Maggi
2009-07-03 14:13:59 UTC
Permalink
Ciao,

Running the following program on a i686-pc-linux-gnu under
Larceny revision 6303 (Larceny v0.97a4 (alpha test) (Jun 26
2009 16:14:03, precise:Linux:unified)):

(import (rnrs)
(primitives
foreign-procedure
%peek8 %peek8u %peek16 %peek16u %peek32 %peek32u %peek-pointer
%poke8 %poke8u %poke16 %poke16u %poke32 %poke32u %poke-pointer
void*-double-set! void*-double-ref void*-float-set! void*-float-ref
void*? void*-rt record-constructor void*->address))

(define platform-malloc
(foreign-procedure "malloc" '(unsigned) 'void*))

(define p (platform-malloc 4096))
(define a (void*->address p))

(let ((v (- (expt 2 7) 1)))
(%poke8 a v)
(write (list v (%peek8 a)))
(newline))

(let ((v (- (expt 2 15) 1)))
(%poke16 a v)
(write (list v (%peek16 a)))
(newline))

(let ((v (- (expt 2 31) 1)))
(%poke32 a v)
(write (list v (%peek32 a)))
(newline))

(newline)

(let ((v (- (expt 2 8) 1)))
(%poke8u a v)
(write (list v (%peek8u a)))
(newline))

(let ((v (- (expt 2 16) 1)))
(%poke16u a v)
(write (list v (%peek16u a)))
(newline))

(let ((v (- (expt 2 32) 1)))
(%poke32u a v)
(write (list v (%peek32u a)))
(newline))

(newline)

(let ((v (- (expt 2 15) 2)))
(%poke16 a v)
(write (list v (%peek16 a)))
(newline))

prints:

(127 127)
(32767 -32769)
(2147483647 2147483647)

(255 255)
(65535 65535)
(4294967295 4294967295)

(32766 32766)

while I expected (32767 32767) as output for %peek16 and
%poke16. This gives me a feeling of deja vu, but,
unfortunately, right now I cannot access the archive of the
mailing list (no answer from server) to verify if I had
reported it before. However, searching the tickets database
for "peek" or "poke" shows nothing relevant.
--
Marco Maggi
Felix Klock
2009-07-03 16:51:35 UTC
Permalink
Marco (cc'ing larceny-users)-
Post by Marco Maggi
(let ((v (- (expt 2 7) 1)))
(%poke8 a v)
(write (list v (%peek8 a)))
(newline))
(let ((v (- (expt 2 15) 1)))
(%poke16 a v)
(write (list v (%peek16 a)))
(newline))
(let ((v (- (expt 2 31) 1)))
(%poke32 a v)
(write (list v (%peek32 a)))
(newline))
(newline)
(let ((v (- (expt 2 8) 1)))
(%poke8u a v)
(write (list v (%peek8u a)))
(newline))
(let ((v (- (expt 2 16) 1)))
(%poke16u a v)
(write (list v (%peek16u a)))
(newline))
(let ((v (- (expt 2 32) 1)))
(%poke32u a v)
(write (list v (%peek32u a)))
(newline))
(newline)
(let ((v (- (expt 2 15) 2)))
(%poke16 a v)
(write (list v (%peek16 a)))
(newline))
Why are you subtracting 2 here when you've been subtracting 1 in all
the other cases?

----

In other words, I do not yet understand what bug you are reporting; (-
(expt 2 15) 2) evaluates to 32766, no peek or poke involved.

-Felix
Marco Maggi
2009-07-03 18:28:28 UTC
Permalink
Why are you subtracting 2 here when you've been
subtracting 1 in all the other cases?
(let ((v (- (expt 2 15) 1)))
(%poke16 a v)
(write (list v (%peek16 a)))
(newline))
which prints (32767 -32769) where I expected (32767 32767).
(let ((v (- (expt 2 15) 2)))
(%poke16 a v)
(write (list v (%peek16 a)))
(newline))
correctly prints (32766 32766), I included it to show that
the problem is only with 2^15-1.
--
Marco Maggi
Felix Klock
2009-07-03 18:46:23 UTC
Permalink
Marco-
Post by Marco Maggi
Why are you subtracting 2 here when you've been
subtracting 1 in all the other cases?
(let ((v (- (expt 2 15) 1)))
(%poke16 a v)
(write (list v (%peek16 a)))
(newline))
which prints (32767 -32769) where I expected (32767 32767).
(let ((v (- (expt 2 15) 2)))
(%poke16 a v)
(write (list v (%peek16 a)))
(newline))
correctly prints (32766 32766), I included it to show that
the problem is only with 2^15-1.
Ah, I understand now. I did not read all the output you had included
carefully enough.

This looks like a fence-post error in the definition of %get16 in lib/
Base/std-ffi.sch to me, especially if one compares it to %get32
immediately following...

-Felix
Felix Klock
2009-07-08 21:52:19 UTC
Permalink
Marco (cc'ing larceny-users)-
Post by Felix Klock
This looks like a fence-post error in the definition of %get16 in
lib/Base/std-ffi.sch to me, especially if one compares it to %get32
immediately following...
Filed as Ticket #646 and fixed by changeset:6344.

Thanks so much for the bug report; clearly the FFI has not been
exercised as much as I would like, so feedback like this is much
appreciated!

-Felix
Marco Maggi
2009-07-09 13:12:36 UTC
Permalink
Post by Felix Klock
Filed as Ticket #646 and fixed by changeset:6344.
Confirmed, it is fixed.
--
Marco Maggi
Loading...