FFI questions

Gary Morris trevion at gmail.com
Mon Jun 6 04:03:36 EDT 2005

Hello everyone,

Apologies in advance if I'm asking an incredibly simple question, but

I'm using GHC 6.4 on Windows XP SP2.  I was recently playing with
implementing the Kocher attack on RSA in Haskell, and along the way I
needed a reasonable way to time operations -- the built-in Windows
functions don't do such a good job.  I wrote a very simple function
that uses the rdtsc instruction:

--- begin: AccuTime.c

#include <windows.h>

__declspec(dllexport) ULONGLONG accuticks() {
	ULONG lo, hi;
	__asm {
		mov hi, edx
		mov lo, eax
	i.HighPart = hi;
	i.LowPart = lo;
	return i.QuadPart;

--- end

--- begin: AccuTime.h

#ifndef __accutime_h
#define __acutime_h

#include <windows.h>

ULONGLONG accuticks();

#endif /* __accutime_h */

--- end

and compiled it to AccuTime.dll.  I then attempted to piece together
the file I'd need to import this function into Haskell:

--- begin: AccuTime.hs

module AccuTime where

import Foreign.C.Types

foreign import stdcall unsafe "accutime.h accuticks" c_accuticks :: IO CULLong

accuticks :: IO Integer
accuticks = do ull <- c_accuticks
               return (fromIntegral ull)

--- end

And then, after passing the correct -L and -l flags to ghci, attempted
to load that module.  Unfortunately, I got:

   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.4, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base-1.0 ... linking ... done.
Loading object (dynamic) AccuTime ... done
final link ... done
Prelude> :l AccuTime.hs
Compiling AccuTime         ( AccuTime.hs, interpreted )
ghc.exe: panic! (the `impossible' happened, GHC version 6.4):
        ByteCodeFFI.mkMarshalCode_wrk(x86) L_

Please report it as a compiler bug to glasgow-haskell-bugs at haskell.org,
or http://sourceforge.net/projects/ghc/.


At this point, I'm quite confused.  Does anyone else know what I'm
doing to cause this panic?



More information about the Glasgow-haskell-users mailing list