Re: FW: [Haskell-cafe] hsc2hs and Storable (especially) unsafe

Stephen Paul Weber <[email protected]> Wed, 2 Jan 2013 10:24:47 -0500
Newsgroups gmane.comp.lang.haskell.cvs.ghc
Message-ID <20130102152447.GB1845@singpolyma-svelti>
Somebody claiming to be Simon Peyton-Jones wrote:
>| It so happens that on 64 bit OS X, haskell's Int is 64 bits, while C's
>| int is
>| 32 bits.  hsc2hs's #poke does no typechecking at all, so if you have
>|
>| (#poke Struct, int_field) structp int -- where int :: Int

One should not do this.

That said, the tool should not allow this.

>| It's probably not even correct to
>| assume Double = CDouble and Float = CFloat, though it seems likely to be
>| always true.

base has a configure check for these, but I have been unable to determine 
what it does.  It fails on my QNXNTO cross-compiler and so I've been 
manually hacking this assumption in there, but ... yeah ... probably not 
safe.

>| I carefully vetted all my #pokes but I'm annoyed that hsc2hs and Foreign
>| let me get away with this.  If Storable is for marshalling types to and
>| from C, then non-C types should not be Storable!

Well, except that you might just be round-tripping some Haskell data through 
(void*) in C, and so the interpretation on the C side does not matter in 
that case.

>| While we're at it, fromIntegral is not a safe way to convert a haskell
>| type to a C one.  But it sure is a tempting one

It is the one I always use, but I'm very aware that the behaviour is likely 
to be bad.  That said, the behaviour of `fromIntegral` moving to a smaller 
type is always bad.  This should probably exist:

fromIntegralSafe :: (Integral a, Ord a, Bounded b, Integral b) => a -> Maybe b
fromIntegralSafe x
	| toInteger x > toInteger (maxBound `asTypeOf` v) = Nothing
	| toInteger x < toInteger (minBound `asTypeOf` v) = Nothing
	| otherwise = Just v
	where
	v = fromIntegral x

>| Or maybe I shouldn't be using hsc2hs in the first place.

Hard to say.  I've used it a couple times, mostly when there are C enums 
involved.  Usually in my FFI work I just write the imports by hand (though 
this also allows for dangerously writing `Int` when you mean `CInt`, etc).

-- 
Stephen Paul Weber, @singpolyma
See <http://singpolyma.net> for how I prefer to be contacted
edition right joseph

_______________________________________________
Cvs-ghc mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/cvs-ghc
signature.asc (application/pgp-signature, 836 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iQIcBAEBCAAGBQJQ5FE+AAoJENEcKRHOUZzeFCwP/R7o7yEEmOwiYfIzeZN+EfHz
I4XdAYUNJ4nVJQ/sM1knK04kZWLXyvl6pJjy+G5tZvo2i88WHOb1tND6p5I2WKw2
2yhPhM2WRIhqvhTM+y4l8xU+5Y21Xarm6pWDErRJ82B3JRXVZ/BNpbShCGutxSbi
vaeIq0RXpNQ3t8F+btDzQcGsLIBWv3RYKE2rN3abWbvc6cxTWNmdVCt/l9YSMz79
5IFCVeoRHjkIK2+mLsV466wA2X1ALmtstbzRL0Fve7juHbEEeTrC6ZZhb+BzqngS
FnB6MlxAeMhF4X0RXbt8ylez7FouHKd+PODdwISwpLbgZYEWfiu/TOktf0a+kCT2
EdRBzSy9z+vkdjg6O0jRQSvZ0iD9OAi7I3D525iyepJTjr6zF8cH7Xf6D/R7QI2I
+ljT54Kxys5YQIIHrXHFd1PITaT8nqUxTe5JcLKIulISTCAE2NLJ8iNsdNoKz17h
nbt5cYb027BMtg2U87fnOE2PoJfA9tjluZnesEDKMZhPtFgN0RWi1wGQNC8Bb17M
6pfJIjlHOLsco9f6QndYc2AGoRQxAxiIE0xCwozfitL+7vBFXPA6krqO9yTGrHpD
L8tIQ/avMUqAgcmCX2+BShKR4DvO4qGh1uwOiDLAXWhGTNW12Tx4rx1wplJArd5i
OuiErEUVMis4ofReVh88
=oQwR
-----END PGP SIGNATURE-----