Code breaks with '-O', any ideas.

My program 'harchive' http://www.davidb.org/darcs/harchive/ breaks if compiled with '-O'. Just wondering if anyone has suggestions on how to figure out what might be causing this. Is is possible to build some files with '-O' and others without? I'm not using unsafePerformIO. Thanks, David

On Mon, Mar 05, 2007 at 10:51:46AM -0800, David Brown wrote:
My program 'harchive' http://www.davidb.org/darcs/harchive/ breaks if compiled with '-O'. Just wondering if anyone has suggestions on how to figure out what might be causing this.
What do you mean by "breaks"?
Is is possible to build some files with '-O' and others without?
Yes. Thanks Ian

Ian Lynagh wrote:
On Mon, Mar 05, 2007 at 10:51:46AM -0800, David Brown wrote:
My program 'harchive' http://www.davidb.org/darcs/harchive/ breaks if compiled with '-O'. Just wondering if anyone has suggestions on how to figure out what might be causing this.
What do you mean by "breaks"?
:-). It guess it would be best to describe it as behaving differently. I've seen to case. One has to do with my binding to sqlite3. Sometimes the queries don't return the right results or gibberish is entered into the database. This suggests my binding has problems (like pointers living past a GC). In another instance, a populated map was failing to find an entry in it. I added a debug message to print out the map, and then it worked fine. I'm now rebuilding individual parts with -O to see if I can isolate the problem(s). Thanks, David

David Brown wrote:
Ian Lynagh wrote:
On Mon, Mar 05, 2007 at 10:51:46AM -0800, David Brown wrote:
My program 'harchive' http://www.davidb.org/darcs/harchive/ breaks if compiled with '-O'. Just wondering if anyone has suggestions on how to figure out what might be causing this. What do you mean by "breaks"?
I'm now rebuilding individual parts with -O to see if I can isolate the problem(s).
Well, that helped. I found the problem. I had something like: import qualified Data.ByteString as B let hash = B.pack $ replicate (#const SHA_DIGEST_LENGTH) 0 ... B.useAsCStringLen hash $ \ (hdata, _) -> do c_sha1Final hash ctx return hash But this seems to be causing the hash to be garbage, and possibly have c_sha1Final write a 20-byte hash into some garbled piece of memory. Ireplaced this with let hashLen = (#const SHA_DIGEST_LENGTH) hashData <- mallocForeignPtrBytes hashLen withForeignPtr hashData $ \hashP -> do c_sha1Final hashP ctx B.copyCStringLen (hashP, hashLen) and the code works both with and without optimization. Dave

haskell2:
David Brown wrote:
Ian Lynagh wrote:
On Mon, Mar 05, 2007 at 10:51:46AM -0800, David Brown wrote:
My program 'harchive' http://www.davidb.org/darcs/harchive/ breaks if compiled with '-O'. Just wondering if anyone has suggestions on how to figure out what might be causing this. What do you mean by "breaks"?
I'm now rebuilding individual parts with -O to see if I can isolate the problem(s).
Well, that helped. I found the problem. I had something like:
import qualified Data.ByteString as B
let hash = B.pack $ replicate (#const SHA_DIGEST_LENGTH) 0 ... B.useAsCStringLen hash $ \ (hdata, _) -> do c_sha1Final hash ctx return hash
But this seems to be causing the hash to be garbage, and possibly have c_sha1Final write a 20-byte hash into some garbled piece of memory.
Ireplaced this with
let hashLen = (#const SHA_DIGEST_LENGTH) hashData <- mallocForeignPtrBytes hashLen withForeignPtr hashData $ \hashP -> do c_sha1Final hashP ctx B.copyCStringLen (hashP, hashLen)
and the code works both with and without optimization.
Dave
Ah, this is my fault! The old api used 'useAsCSTringLen' unsafely. Its a zero-copying version. The default is now copyCStringLen. See this thread for the issues involved: http://www.haskell.org/pipermail/libraries/2007-January/006783.html and for how it has been resolved: http://www.haskell.org/pipermail/libraries/2007-January/006801.html So yes, using 'copyCStringlen' is the appropriate thing to do, if you don't want to guarantee the C side of things. -- Don
participants (3)
-
David Brown
-
dons@cse.unsw.edu.au
-
Ian Lynagh