Ticket #3516 (closed bug: fixed)

Opened 4 years ago

Last modified 3 years ago

[PATCH] ppc64: broken 'foreign import wrapper'

Reported by: slyfox Owned by:
Priority: high Milestone: 6.12.2
Component: Compiler (FFI) Version: 7.1
Keywords: ffi, wrapper, patch Cc: pho@…
Operating System: Unknown/Multiple Architecture: powerpc64
Type of failure: Incorrect result at runtime Difficulty: Unknown
Test Case: yet Blocked By:
Blocking: Related Tickets:

Description

Attaching simple testcase failing horribly on ppc64:

amd64 test output:

$ ./dist/build/fct/fct
C call:[result = 105]

C call with registered C callback function:C(72)C(33)[result = 735]

C call with registered Hs-C callback function:H(72)H(33)[result = 735]

TEST PASSED

ppc64 test output:

$ dist/build/fct/fct
C call:[result = 105]

C call with registered C callback function:C(72)C(33)[result = 735]

C call with registered Hs-C callback function:[result = 105]

Segmentation fault

As you see C call with registered Hs-C callback function called not a registered function, but something strange. There is no even registered callback (glo_cb == 0).

Attachments

wrapper-ppc64-fault-0.1.tar.gz Download (1.5 KB) - added by slyfox 4 years ago.
wrapper-ppc64-fault-0.1.tar.gz: Contains small 'cabal build'able project.
missing-dependency-on-ffitarget-h.patch Download (167.1 KB) - added by PHO 3 years ago.

Change History

Changed 4 years ago by slyfox

wrapper-ppc64-fault-0.1.tar.gz: Contains small 'cabal build'able project.

Changed 4 years ago by slyfox

  • component changed from Compiler to Compiler (FFI)

ghc-6.10.4 was built as unregisterised:  http://code.haskell.org/~slyfox/ghc-ppc64/ghc-bin-6.10.4-ppc64.tbz2

Bug was originally spotted in terminfo package after digging why darcs-2.3.0 SIGSEGVs there.

Changed 4 years ago by igloo

  • priority changed from normal to low
  • difficulty set to Unknown
  • milestone set to 6.12 branch

Thanks for the report. This is not a tier 1 arch, so needs a volunteer to look into it.

Changed 3 years ago by slyfox

  • keywords wrapper, patch added; wrapper removed
  • failure set to Incorrect result at runtime
  • summary changed from ppc64: broken 'foreign import wrapper' to [PATCH] ppc64: broken 'foreign import wrapper'

I think I've tracked down the problem. The problem is incorrect assumption about libFFI closure return value ABI.

The idea of the patch is to change _stub.c generator in such a way: --- - removed lines +++ - added lines

#define IN_STG_CODE 0
#include "Rts.h"
#include "Stg.h"

+++ #include "ffi.h"

#ifdef __cplusplus
extern "C" {
#endif
 
void Main_dSe(void *cif STG_UNUSED, void* resp, void** args, void* the_stableptr)
{
Capability *cap;
HaskellObj ret;
HsInt32 cret;
cap = rts_lock();
cap=rts_evalIO(cap,rts_apply(cap,(HaskellObj)runIO_closure,rts_apply(cap,(StgClosure*)deRefStablePtr(the_stableptr),rts_mkPtr(c
ap,*(HsPtr*)args[0]))) ,&ret);
rts_checkSchedStatus("Main_dSe",cap);
cret=rts_getInt32(ret);
rts_unlock(cap);

--- *(HsInt32*)resp = cret;
+++ *(ffi_arg*)resp = cret;
}
#ifdef __cplusplus
}
#endif

NOTE: HsFloat? and HsDouble? were not affected.

As we see resp ABI is not just a pointer to user specified type, but a pointer to largest available integral value (or something like that) of type ffi_arg.

I tested this patch on attached sample on LIBFFI aware x86_64 and on powerpc64 host. Both worked correctly.

The patch is available here(and in cvs-ghc ML):  http://code.haskell.org/~slyfox/ppc64-broken-foreign-import-wrapper.dpatch

I also tried to start discussion in view of this issue:  http://sourceware.org/ml/libffi-discuss/2010/msg00063.html

Sorry, I failed to attach this patch directly (firefox SIGSEGVs when I click 'Attach file')

Changed 3 years ago by slyfox

Forgot to mention: this patch is against ghc-STABLE branch (aka ghc-6.12) as -HEAD branch is hard to build on ppc64. But I think it should not be hard to pull this patch to -HEAD with help of darcs.

Changed 3 years ago by simonmar

  • milestone changed from 6.12 branch to 6.12.2

Changed 3 years ago by simonmar

  • priority changed from low to high

Changed 3 years ago by simonmar

  • owner set to simonmar
  • status changed from new to assigned

Changed 3 years ago by simonmar

  • owner changed from simonmar to igloo
  • status changed from assigned to new
  • type changed from bug to merge

Fixed:

Mon Mar 29 14:57:34 BST 2010  Simon Marlow <marlowsd@gmail.com>
  * libffi: install 'ffitarget.h' header as sole 'ffi.h' is unusable
  Submitted by: Sergei Trofimovich <slyfox@community.haskell.org>
  Re-recorded against HEAD.

Mon Mar 29 16:42:20 BST 2010  Simon Marlow <marlowsd@gmail.com>
  * fix return type cast in f.i.wrapper when using libffi (#3516)
  
  Original fix submitted by 
    Sergei Trofimovich <slyfox@community.haskell.org>
  modified by me:
   - exclude 64-bit types
   - compare uniques, not strings
   - #include "ffi.h" is conditional

Changed 3 years ago by igloo

  • status changed from new to closed
  • resolution set to fixed

Both merged.

Changed 3 years ago by PHO

  • cc pho@… added

Changed 3 years ago by PHO

Changed 3 years ago by PHO

  • owner igloo deleted
  • status changed from closed to new
  • version changed from 6.10.4 to 7.1
  • resolution fixed deleted
  • type changed from merge to bug

On the darcs HEAD:

"inplace/bin/ghc-stage1"   -H32m -O    -package-name base-4.3.0.0 -hide-all-packages -i -ilibraries/base/. -ilibraries/base/dist-install/build -ilibraries/base/dist-install/build/autogen -Ilibraries/base/dist-install/build -Ilibraries/base/dist-install/build/autogen -Ilibraries/base/include   -optP-DOPTIMISE_INTEGER_GCD_LCM -optP-include -optPlibraries/base/dist-install/build/autogen/cabal_macros.h -package ghc-prim-0.2.0.0 -package integer-gmp-0.2.0.2 -package rts-1.0  -package-name base -XMagicHash -XExistentialQuantification -XRank2Types -XScopedTypeVariables -XUnboxedTuples -XForeignFunctionInterface -XUnliftedFFITypes -XDeriveDataTypeable -XGeneralizedNewtypeDeriving -XFlexibleInstances -XStandaloneDeriving -XPatternGuards -XEmptyDataDecls -XNoImplicitPrelude -XCPP -no-user-package-conf -rtsopts -O2 -XGenerics     -odir libraries/base/dist-install/build -hidir libraries/base/dist-install/build -stubdir libraries/base/dist-install/build -hisuf hi -osuf  o -hcsuf hc -c libraries/base/./Control/Concurrent.hs -o libraries/base/dist-install/build/Control/Concurrent.o

In file included from libraries/base/dist-install/build/Control/Concurrent_stub.c:4:0:


/Users/admin/sandbox/_haskell/ghc/libffi/dist-install/build/ffi.h:64:23:
     error: ffitarget.h: No such file or directory

The attached missing-dependency-on-ffitarget-h.patch solves this.

Changed 3 years ago by igloo

  • status changed from new to patch

Changed 3 years ago by igloo

  • status changed from patch to closed
  • resolution set to fixed

Thanks; applied to HEAD and 7.0 branch.

Note: See TracTickets for help on using tickets.