libcint icon indicating copy to clipboard operation
libcint copied to clipboard

Calling 4c1e integrals - segfault/double free

Open duykhoidang opened this issue 2 years ago • 1 comments

          I am trying to call cint4c1e using `cint4c1e_sph(buf, shls, atm, natm, bas, nbas, env)` and `cint4c1e_sph(buf, shls, atm, natm, bas, nbas, env, NULL)`, but both versions of this call result in a segfault or double-free error. Replacing `cint4c1e_sph` with `cint2e_sph` results in working code with no segfault and the correct 2e-integrals, so I do not believe the loop structure or setup of the `buf`, `shls`, `atm`, `natm`, `bas`, `nbas`, `env` variables are causing the issue. It seems like the 4c1e overlap integral code is possibly writing memory out of bounds. Do you have this error?

Originally posted by @duykhoidang in https://github.com/sunqm/libcint/issues/89#issuecomment-1492082590

I am trying to call cint4c1e using cint4c1e_sph(buf, shls, atm, natm, bas, nbas, env) and cint4c1e_sph(buf, shls, atm, natm, bas, nbas, env, NULL), but both versions of this call result in a segfault or double-free error. Replacing cint4c1e_sph with cint2e_sph results in working code with no segfault and the correct 2e-integrals, so I do not believe the loop structure or setup of the buf, shls, atm, natm, bas, nbas, env variables are causing the issue. It seems like the 4c1e overlap integral code is possibly writing memory out of bounds. Do you have this error?

duykhoidang avatar Mar 31 '23 15:03 duykhoidang

I also had this error. My guess is that it seems like the cache_size variable value in the CINT4c1e_drv() function of cint4c1e.c should be bigger. This value is defined as https://github.com/sunqm/libcint/blob/708695674f908fc6d4f6b0d8ffac63553313a434/src/cint4c1e.c#L282-L283 Changing this definition (as well as L273) to

size_t cache_size = MAX(leng+len0+nc*n_comp*3 + pdata_size,
                                      nc*n_comp+envs->nf*32*OF_CMPLX);

seems to resolve the segfault for me, at least for the test cases obtained by adding the following lines

from pyscf import gto
import sys
mol =  gto.M(atom="H 0 0 1; H 1 0 0; O 0 1 0", basis=str(sys.argv[1]))
natm = mol.natm; nbas = mol.nbas; atm = mol._atm; bas = mol._bas; env = mol._env
natm = ctypes.c_int(natm)
nbas = ctypes.c_int(nbas)
c_atm = atm.ctypes.data_as(ctypes.c_void_p)
c_bas = bas.ctypes.data_as(ctypes.c_void_p)
c_env = env.ctypes.data_as(ctypes.c_void_p)

somewhere around line 148 (just before the first test function) of the file test_cint4c1e.py in the test directory libcint/testsuite. The segfault did not appear in my test cases for basis sets sto-3g, cc-pvdz, aug-cc-pvdz, aug-cc-pvtz.

Furthermore, I believe in PySCF 2.3.0 the molecular integral mol.intor("int4c1e_sph", comp=1) for the water molecule and the forementioned basis can be obtained with no segfault error by declaring the variables eri and out as double * instead of double complex * in pyscf/lib/gto/fill_r_4c.c lines 29 and 73. The gto.mole.getints4c function has also been evoked with NULL optimizer cintopt = lib.c_null_ptr() because otherwise I obtained an optimizer not found error.

ilygatsika avatar Sep 11 '23 09:09 ilygatsika