Quantcast

[GHC] #5254: usb library fails on Windows

classic Classic list List threaded Threaded
7 messages Options
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

[GHC] #5254: usb library fails on Windows

GHC
#5254: usb library fails on Windows
---------------------------------+------------------------------------------
    Reporter:  basvandijk        |       Owner:                
        Type:  bug               |      Status:  new          
    Priority:  normal            |   Component:  Compiler (FFI)
     Version:  7.0.3             |    Keywords:                
    Testcase:                    |   Blockedby:                
          Os:  Windows           |    Blocking:                
Architecture:  Unknown/Multiple  |     Failure:  Runtime crash
---------------------------------+------------------------------------------
 I'm trying to get my Haskell `usb` library to work on Windows. I currently
 get a weird error. Please follow the steps below to reproduce the error:

  * Download
 [http://www.libusb.org/wiki/windows_backend#LatestBinarySnapshots libusb
 for Windows] and extract it somewhere e.g. `C:\Program
 Files\libusb\libusb1`.

  (Important build-time files: `libusb-1.0\include\libusb.h` and
 `MinGW32\dll\libusb-1.0.dll.a`)

  (Important run-time files: `MinGW32\dll\libusb-1.0.dll`)

  * Download, unpack and cabal install the, as of yet unreleased,
 [https://bitbucket.org/mauricio/bindings-dsl/get/1.0.12.tar.gz bindings-
 DSL-1.0.12]
  (This version lets you use the right calling convention on Windows
 (`stdcall` instead of `ccall`) by configuring cabal with
  `cc-options: -DBINDINGS_STDCALLCONV`)

  * Install `bindings-libusb`:
 {{{
 git clone git://github.com/basvandijk/bindings-libusb.git
 cd bindings-libusb
 }}}

  Make sure to checkout the `windows` branch and let cabal know where to
 find `libusb`:
 {{{
 git checkout windows
 cabal install --extra-include-dirs="C:\Program
 Files\libusb\libusb1\include\libusb-1.0"
               --extra-lib-dirs="C:\Program
 Files\libusb\libusb1\MinGW32\dll"
 }}}

  * Clone the `usb` repository:
 {{{
 git clone git://github.com/basvandijk/usb.git
 cd usb
 }}}
  There's no need to install the library. I included an example program
 that will demonstrate the error:
 {{{
 cabal configure --flags="example -library"
 cabal build
 }}}

 The example should read some bytes of an attached USB mouse (change the
 VID and PID to match your mouse). However it gives two errors:

 {{{
 dist\build\example\example.exe
 example.exe: NotFoundException
 Segmentation fault/access violation in generated code
 }}}

 This bug report is about the segmentation fault.

 The `NotFoundException` is thrown by `c'libusb_get_config_descriptor`
 which is indirectly called by `getDevices`. According to the
 [http://libusb.sourceforge.net/api-1.0/group__desc.html#gaa635d9aec77de4895dd0896ccf001532
 libusb docs]
 this error is thrown when the specified configuration doesn't
 exists. Since I only call this function on existing configurations
 this seems like a bug in libusb. I will dive in the libusb source code to
 see what is going on.

 The `Segmentation fault/access violation in generated code` is caused
 by the finalizer in `newCtx`:

 {{{
 newCtx ∷ IO Ctx
 newCtx = newCtxNoEventManager Ctx

 libusb_init ∷ IO (Ptr C'libusb_context)
 libusb_init = alloca $ \ctxPtrPtr → do
                handleUSBException $ c'libusb_init ctxPtrPtr
                peek ctxPtrPtr

 newCtxNoEventManager ∷ (ForeignPtr  C'libusb_context → Ctx) → IO Ctx
 newCtxNoEventManager ctx = mask_ $ do
                             ctxPtr ← libusb_init
                             ctx <$> newForeignPtr p'libusb_exit ctxPtr
 }}}

 When `p'libusb_exit` is called the segmentation fault occurs.

 '''Note that the error disappears when I change that last line with:

 `ctx <$> Foreign.Concurrent.newForeignPtr ctxPtr (c'libusb_exit
 ctxPtr)`'''

 I previously got the exact same segmentation fault when calling other
 `bindings-libusb` functions. This was caused by using the wrong calling
 convention on Windows. I used `ccall` but had to use `stdcall`. So I
 assume the current segmentation fault has something to do with the calling
 convention of `FunPtrs`.

 Any ideas what is causing this? I would really like to solve it. It's the
 last step before releasing `usb-1.0`.

 Thanks!

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5254>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5254: usb library fails on Windows

GHC
#5254: usb library fails on Windows
---------------------------------+------------------------------------------
    Reporter:  basvandijk        |        Owner:              
        Type:  bug               |       Status:  new          
    Priority:  normal            |    Milestone:  7.4.1        
   Component:  Compiler (FFI)    |      Version:  7.0.3        
    Keywords:                    |     Testcase:              
   Blockedby:                    |   Difficulty:              
          Os:  Windows           |     Blocking:              
Architecture:  Unknown/Multiple  |      Failure:  Runtime crash
---------------------------------+------------------------------------------
Changes (by igloo):

  * milestone:  => 7.4.1


--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5254#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5254: usb library fails on Windows

GHC
In reply to this post by GHC
#5254: usb library fails on Windows
---------------------------------+------------------------------------------
    Reporter:  basvandijk        |       Owner:              
        Type:  bug               |      Status:  new          
    Priority:  low               |   Milestone:  7.6.1        
   Component:  Compiler (FFI)    |     Version:  7.0.3        
    Keywords:                    |          Os:  Windows      
Architecture:  Unknown/Multiple  |     Failure:  Runtime crash
  Difficulty:                    |    Testcase:              
   Blockedby:                    |    Blocking:              
     Related:                    |  
---------------------------------+------------------------------------------

Comment(by basvandijk):

 Before I forget, I did some more investigating in
 [http://www.haskell.org/pipermail/glasgow-haskell-
 users/2012-June/022454.html this thread].

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5254#comment:3>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5254: usb library fails on Windows

GHC
In reply to this post by GHC
#5254: usb library fails on Windows
---------------------------------+------------------------------------------
    Reporter:  basvandijk        |       Owner:              
        Type:  bug               |      Status:  new          
    Priority:  low               |   Milestone:  7.6.1        
   Component:  Compiler (FFI)    |     Version:  7.0.3        
    Keywords:                    |          Os:  Windows      
Architecture:  Unknown/Multiple  |     Failure:  Runtime crash
  Difficulty:                    |    Testcase:              
   Blockedby:                    |    Blocking:              
     Related:                    |  
---------------------------------+------------------------------------------

Comment(by basvandijk):

 Summarizing: executing the following isolated program with the argument
 "fp" (for !ForeignPtr) gives an error and without it I get no error:

 {{{
 {-# LANGUAGE ForeignFunctionInterface #-}

 module Main where

 import Foreign
 import Foreign.C.Types
 import Control.Concurrent
 import System.Environment

 main :: IO ()
 main = do
   ctxPtr <- alloca $ \ctxPtrPtr -> do
               _ <- c'libusb_init ctxPtrPtr
               peek ctxPtrPtr

   args <- getArgs
   case args of
     ["fp"] -> do
       fp <- newForeignPtr p'libusb_exit ctxPtr
       threadDelay 1000000
       print $ fp == fp

     _ -> c'libusb_exit ctxPtr

 data C'libusb_context = C'libusb_context

 foreign import stdcall "libusb_init" c'libusb_init
   :: Ptr (Ptr C'libusb_context) -> IO CInt

 foreign import stdcall "&libusb_exit" p'libusb_exit
   :: FunPtr (Ptr C'libusb_context -> IO ())

 foreign import stdcall "libusb_exit" c'libusb_exit
   :: Ptr C'libusb_context -> IO ()
 }}}

 Note that building the program with the new `win64_alpha1` GHC doesn't
 produce the error. I only get warnings that the `stdcall` calling
 convention is not supported. So I guess it then falls back to the `ccall`
 calling convention which does work.

 Make sure to give these flags to cabal when building the program:

  * `--extra-include-dirs="...\libusb\include\libusb-1.0"`
  * `--extra-lib-dirs="...\libusb\MinGW32\dll"` or: `--extra-lib-
 dirs="...\libusb\MinGW64\dll"` when building with the new `win64_alpha1`
 GHC.

 and make sure the `libusb-1.0.dll` is in your working directory when
 running the program.

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5254#comment:4>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5254: usb library fails on Windows

GHC
In reply to this post by GHC
#5254: usb library fails on Windows
---------------------------------+------------------------------------------
    Reporter:  basvandijk        |       Owner:  simonmar    
        Type:  bug               |      Status:  new          
    Priority:  high              |   Milestone:  7.6.1        
   Component:  Compiler (FFI)    |     Version:  7.0.3        
    Keywords:                    |          Os:  Windows      
Architecture:  Unknown/Multiple  |     Failure:  Runtime crash
  Difficulty:  Unknown           |    Testcase:              
   Blockedby:                    |    Blocking:              
     Related:                    |  
---------------------------------+------------------------------------------
Changes (by simonmar):

  * owner:  => simonmar
  * difficulty:  => Unknown
  * priority:  low => high


Comment:

 The `FunPtr` that you pass to `mkForeignPtr` must use the `ccall` calling
 convention.  This should be documented - I'll fix that.

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5254#comment:5>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5254: usb library fails on Windows

GHC
In reply to this post by GHC
#5254: usb library fails on Windows
---------------------------------+------------------------------------------
    Reporter:  basvandijk        |       Owner:  simonmar    
        Type:  bug               |      Status:  new          
    Priority:  high              |   Milestone:  7.6.1        
   Component:  Compiler (FFI)    |     Version:  7.0.3        
    Keywords:                    |          Os:  Windows      
Architecture:  Unknown/Multiple  |     Failure:  Runtime crash
  Difficulty:  Unknown           |    Testcase:              
   Blockedby:                    |    Blocking:              
     Related:                    |  
---------------------------------+------------------------------------------

Comment(by basvandijk):

 Thanks Simon!

 Maurício: I guess the best way to solve this in `bindings-DSL` is to fix
 the calling convention for `FunPtr`s to `ccall` (regardless of
 `BINDINGS_STDCALLCONV`).

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5254#comment:6>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5254: usb library fails on Windows

GHC
In reply to this post by GHC
#5254: usb library fails on Windows
-----------------------------+----------------------------------------------
  Reporter:  basvandijk      |          Owner:  simonmar        
      Type:  bug             |         Status:  closed          
  Priority:  high            |      Milestone:  7.6.1          
 Component:  Compiler (FFI)  |        Version:  7.0.3          
Resolution:  fixed           |       Keywords:                  
        Os:  Windows         |   Architecture:  Unknown/Multiple
   Failure:  Runtime crash   |     Difficulty:  Unknown        
  Testcase:                  |      Blockedby:                  
  Blocking:                  |        Related:                  
-----------------------------+----------------------------------------------
Changes (by simonmar):

  * status:  new => closed
  * resolution:  => fixed


Comment:

 Fixed:

 {{{
 commit d7332cf3731be55edb4e3ad7e8b272a210a8d210
 Author: Simon Marlow <[hidden email]>
 Date:   Thu Aug 2 11:40:17 2012 +0100

     Document that a FinalizerPtr is a pointer to a ccall function (#5254)

     It can't be any other calling convention, e.g. stdcall.
 }}}

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5254#comment:7>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
Loading...