flintlib / arb

Arb has been merged into FLINT -- use https://github.com/flintlib/flint/ instead
http://arblib.org/
GNU Lesser General Public License v2.1
457 stars 137 forks source link

Segmentation Fault ( Memory Error ) Error in Fortran-C Interoperability #448

Closed AkhilAkkapelli closed 1 year ago

AkhilAkkapelli commented 1 year ago

I wrote a simple code for Complex Integration :

PROGRAM ComplexIntegral
USE farblib
IMPLICIT NONE

TYPE(fmag_t) :: tol
TYPE(facb_t) :: s, a, b
INTEGER(c_long) :: num_threads, prec, goal
TYPE(facb_calc_integrate_opt_t) :: options
TYPE(c_ptr) :: params

CALL facb_calc_integrate_opt_init(options)

prec = 64_c_long
goal = prec
params = c_null_ptr

CALL facb_init(a)
CALL facb_init(b)
CALL facb_init(s)
CALL fmag_init(tol)

CALL fmag_set_ui_2exp_si(tol, 1_c_long, -prec)

CALL facb_set_d(a, 0.0_c_double)
CALL facb_set_d(b, 100.0_c_double)
CALL facb_calc_integrate(s, fone, params, a, b, goal, tol, options, prec)

PRINT*, "Solution:"
CALL facb_print(s)
PRINT*, ""
CALL facb_clear(a)
CALL facb_clear(b)
CALL facb_clear(s)
CALL fmag_clear(tol)

CALL fflint_cleanup_master()    

END PROGRAM ComplexIntegral

FARBLIB Module:

MODULE farblib
USE, INTRINSIC :: iso_c_binding
IMPLICIT NONE

TYPE, PUBLIC, BIND(c) :: facb_t
  PRIVATE  
  TYPE(c_ptr) :: acb_t = c_null_ptr
END TYPE facb_t

TYPE, PUBLIC, BIND(c) :: fmag_t
  PRIVATE  
  TYPE(c_ptr) :: mag_t = c_null_ptr
END TYPE fmag_t

TYPE, PUBLIC, BIND(c) :: facb_calc_integrate_opt_struct
  INTEGER(c_long) :: deg_limit;
  INTEGER(c_long) :: eval_limit;
  INTEGER(c_long) :: depth_limit;  
  INTEGER(c_int) :: use_heap
  INTEGER(c_int) :: verbose
END TYPE facb_calc_integrate_opt_struct

TYPE, PUBLIC, BIND(c) :: facb_calc_integrate_opt_t
  TYPE(facb_calc_integrate_opt_struct) :: acb_calc_integrate_opt_t
END TYPE facb_calc_integrate_opt_t

TYPE, PUBLIC, BIND(c) :: facb_calc_func_t
  TYPE(c_funptr) :: acb_calc_func_t = c_null_funptr
END TYPE facb_calc_func_t

INTERFACE

SUBROUTINE acb_calc_integrate_opt_init(options) BIND(c)
  IMPORT :: facb_calc_integrate_opt_struct
  TYPE(facb_calc_integrate_opt_struct) :: options
END SUBROUTINE acb_calc_integrate_opt_init

SUBROUTINE acb_init(x) BIND(c)
  IMPORT :: c_ptr
  TYPE(c_ptr) :: x
END SUBROUTINE acb_init

SUBROUTINE mag_init(x) BIND(c)
  IMPORT :: c_ptr
  TYPE(c_ptr) :: x
END SUBROUTINE mag_init

SUBROUTINE acb_print(x) BIND(c)
  IMPORT :: c_ptr
  TYPE(c_ptr) :: x
END SUBROUTINE acb_print

SUBROUTINE mag_print(x) BIND(c)
  IMPORT :: c_ptr
  TYPE(c_ptr) :: x
END SUBROUTINE mag_print

SUBROUTINE mag_set_ui_2exp_si(res, x, y) BIND(c)
  IMPORT :: c_ptr, c_long
  TYPE(c_ptr)     :: res
  INTEGER(c_long) :: x
  INTEGER(c_long) :: y  
END SUBROUTINE mag_set_ui_2exp_si

SUBROUTINE acb_set_d(z, c) BIND(c)
  IMPORT :: c_ptr, c_double
  TYPE(c_ptr) :: z
  REAL(c_double), VALUE :: c
END SUBROUTINE acb_set_d

SUBROUTINE acb_calc_integrate(res, f, param, a, b, goal, tol, options, prec) BIND(c)
  IMPORT :: c_ptr, c_null_ptr, c_funptr, c_long, facb_calc_integrate_opt_struct
  TYPE(c_ptr) :: res
  TYPE(c_funptr), VALUE :: f
  TYPE(c_ptr), VALUE :: param
  TYPE(c_ptr) :: a
  TYPE(c_ptr) :: b
  INTEGER(c_long), VALUE :: goal
  TYPE(c_ptr) :: tol
  TYPE(facb_calc_integrate_opt_struct) :: options
  INTEGER(c_long), VALUE :: prec 

END SUBROUTINE acb_calc_integrate

SUBROUTINE acb_clear(x) BIND(c)
  IMPORT :: c_ptr
  TYPE(c_ptr) :: x
END SUBROUTINE acb_clear

SUBROUTINE mag_clear(x) BIND(c)
  IMPORT :: c_ptr
  TYPE(c_ptr) :: x
END SUBROUTINE mag_clear

SUBROUTINE flint_cleanup_master() BIND(c)
END SUBROUTINE flint_cleanup_master

SUBROUTINE acb_one(z) BIND(c)
  IMPORT :: c_ptr
  TYPE(c_ptr) :: z
END SUBROUTINE acb_one

END INTERFACE

CONTAINS

SUBROUTINE facb_calc_integrate_opt_init(options)
  TYPE(facb_calc_integrate_opt_t) :: options

  CALL acb_calc_integrate_opt_init(options%acb_calc_integrate_opt_t)
END SUBROUTINE facb_calc_integrate_opt_init

SUBROUTINE facb_init(x)
  TYPE(facb_t) :: x

  CALL acb_init(x%acb_t)
END SUBROUTINE facb_init

SUBROUTINE fmag_init(x)
  TYPE(fmag_t) :: x

  CALL mag_init(x%mag_t)
END SUBROUTINE fmag_init

SUBROUTINE facb_print(x)
  TYPE(facb_t) :: x

  CALL acb_print(x%acb_t)
END SUBROUTINE facb_print

SUBROUTINE fmag_set_ui_2exp_si(res, x, y) BIND(C)
  TYPE(fmag_t)   :: res
  INTEGER(c_long) :: x
  INTEGER(c_long) :: y  

  CALL mag_set_ui_2exp_si(res%mag_t, x, y)
END SUBROUTINE fmag_set_ui_2exp_si

SUBROUTINE facb_set_d(z, c) BIND(C)
  TYPE(facb_t), INTENT(INOUT) :: z
  REAL(c_double), INTENT(IN) :: c 

  CALL acb_set_d(z%acb_t, c)
END SUBROUTINE facb_set_d

SUBROUTINE facb_calc_integrate(res, f, param, a, b, goal, tol, options, prec) BIND(C)
  TYPE(facb_t), INTENT(INOUT) :: res
  INTERFACE 
    FUNCTION f(output, input, param, order, prec) BIND(C)
    IMPORT :: c_ptr, c_long, c_int
    TYPE(c_ptr) ::  output
    TYPE(c_ptr) :: input
    TYPE(c_ptr) :: param
    INTEGER(c_long) :: order
    INTEGER(c_long) :: prec
    INTEGER(c_int) :: f
    END FUNCTION f
  END INTERFACE
  TYPE(facb_calc_func_t) :: func
  TYPE(c_ptr), INTENT(IN) :: param
  TYPE(facb_t), INTENT(IN)  :: a
  TYPE(facb_t), INTENT(IN)  :: b    
  INTEGER(c_long), INTENT(IN) :: goal  
  TYPE(fmag_t), INTENT(IN) :: tol
  TYPE(facb_calc_integrate_opt_t), INTENT(IN) :: options   
  INTEGER(c_long), INTENT(IN) :: prec 

  func%acb_calc_func_t = c_funloc(f)
  CALL acb_calc_integrate(res%acb_t, func%acb_calc_func_t, param, a%acb_t, b%acb_t, goal, &
                                            tol%mag_t, options%acb_calc_integrate_opt_t, prec)
END SUBROUTINE facb_calc_integrate

SUBROUTINE facb_clear(x)
  TYPE(facb_t) :: x

  CALL acb_clear(x%acb_t)
END SUBROUTINE facb_clear

SUBROUTINE fmag_clear(x)
  TYPE(fmag_t) :: x

  CALL mag_clear(x%mag_t)
END SUBROUTINE fmag_clear

SUBROUTINE fflint_cleanup_master()

  CALL flint_cleanup_master()
END SUBROUTINE fflint_cleanup_master

SUBROUTINE facb_one(z) BIND(C)
  TYPE(c_ptr) :: z

  CALL acb_one(z)
END SUBROUTINE facb_one

SUBROUTINE facb_mul_2exp_si(z, x, e) BIND(c)
  TYPE(facb_t) :: z
  TYPE(facb_t) :: x
  INTEGER(c_long) :: e

  CALL acb_mul_2exp_si(z%acb_t, x%acb_t, e)
END SUBROUTINE facb_mul_2exp_si

FUNCTION fone(res, z, params, order, prec) BIND(C)

TYPE(c_ptr) :: res
TYPE(c_ptr) :: z
TYPE(c_ptr) :: params
INTEGER(c_long) :: order
INTEGER(c_long) :: prec

INTEGER :: fone

!IF(order > 1)  PRINT*, "flint_abort"

CALL facb_one(res)

fone = 1

END FUNCTION fone

END MODULE farblib

I compiled the program using gfortran:

gfortran farblib.F90 farb.F90 -larb -lflint -lm -g 
./a.out

I am getting the following Error :

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7fcbd5c87ad0 in ???
#1  0x7fcbd5c86c35 in ???
#2  0x7fcbd5a7c51f in ???
    at ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0
#3  0x7fcbd6b07dab in arf_set
    at /home/akhil/Arb/arb-2.23.0/arf.h:398
#4  0x7fcbd6b07dab in arb_set
    at /home/akhil/Arb/arb-2.23.0/arb/set.c:17
#5  0x7fcbd6c32249 in acb_set
    at /home/akhil/Arb/arb-2.23.0/acb.h:131
#6  0x7fcbd6c32249 in acb_calc_integrate
    at /home/akhil/Arb/arb-2.23.0/acb_calc/integrate.c:182
#7  0x55afc7de79ef in facb_calc_integrate
    at /home/akhil/Arb/FortranCInteroperability/farb/farblib.F90:476
#8  0x55afc7de7d7c in complexintegral
    at /home/akhil/Arb/FortranCInteroperability/farb/farb.F90:144
#9  0x55afc7de7eeb in main
    at /home/akhil/Arb/FortranCInteroperability/farb/farb.F90:2
Segmentation fault (core dumped)

Using Valgrind ( valgrind ./a.out ):

==69262== Memcheck, a memory error detector
==69262== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==69262== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==69262== Command: ./a.out
==69262== 
==69262== Invalid read of size 8
==69262==    at 0x4926DAB: arf_set (arf.h:398)
==69262==    by 0x4926DAB: arb_set (set.c:17)
==69262==    by 0x4A51249: acb_set (acb.h:131)
==69262==    by 0x4A51249: acb_calc_integrate (integrate.c:182)
==69262==    by 0x10A9EF: facb_calc_integrate (farblib.F90:476)
==69262==    by 0x10AD7C: MAIN__ (farb.F90:144)
==69262==    by 0x10AEEB: main (farb.F90:2)
==69262==  Address 0xc800000000000000 is not stack'd, malloc'd or (recently) free'd
==69262== 

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x5761ad0 in ???
#1  0x5760c35 in ???
#2  0x5a5b51f in ???
    at ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0
#3  0x4926dab in arf_set
    at /home/akhil/Arb/arb-2.23.0/arf.h:398
#4  0x4926dab in arb_set
    at /home/akhil/Arb/arb-2.23.0/arb/set.c:17
#5  0x4a51249 in acb_set
    at /home/akhil/Arb/arb-2.23.0/acb.h:131
#6  0x4a51249 in acb_calc_integrate
    at /home/akhil/Arb/arb-2.23.0/acb_calc/integrate.c:182
#7  0x10a9ef in facb_calc_integrate
    at /home/akhil/Arb/FortranCInteroperability/farb/farblib.F90:476
#8  0x10ad7c in complexintegral
    at /home/akhil/Arb/FortranCInteroperability/farb/farb.F90:144
#9  0x10aeeb in main
    at /home/akhil/Arb/FortranCInteroperability/farb/farb.F90:2
==69262== 
==69262== Process terminating with default action of signal 11 (SIGSEGV)
==69262==    at 0x5AAFA7C: __pthread_kill_implementation (pthread_kill.c:44)
==69262==    by 0x5AAFA7C: __pthread_kill_internal (pthread_kill.c:78)
==69262==    by 0x5AAFA7C: pthread_kill@@GLIBC_2.34 (pthread_kill.c:89)
==69262==    by 0x5A5B475: raise (raise.c:26)
==69262==    by 0x5A5B51F: ??? (in /usr/lib/x86_64-linux-gnu/libc.so.6)
==69262==    by 0x4926DAA: arf_set (arf.h:398)
==69262==    by 0x4926DAA: arb_set (set.c:17)
==69262==    by 0x4A51249: acb_set (acb.h:131)
==69262==    by 0x4A51249: acb_calc_integrate (integrate.c:182)
==69262==    by 0x10A9EF: facb_calc_integrate (farblib.F90:476)
==69262==    by 0x10AD7C: MAIN__ (farb.F90:144)
==69262==    by 0x10AEEB: main (farb.F90:2)
==69262== 
==69262== HEAP SUMMARY:
==69262==     in use at exit: 16,040 bytes in 26 blocks
==69262==   total heap usage: 26 allocs, 0 frees, 16,040 bytes allocated
==69262== 
==69262== LEAK SUMMARY:
==69262==    definitely lost: 0 bytes in 0 blocks
==69262==    indirectly lost: 0 bytes in 0 blocks
==69262==      possibly lost: 0 bytes in 0 blocks
==69262==    still reachable: 16,040 bytes in 26 blocks
==69262==         suppressed: 0 bytes in 0 blocks
==69262== Rerun with --leak-check=full to see details of leaked memory
==69262== 
==69262== For lists of detected and suppressed errors, rerun with: -s
==69262== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)