Warwick-Plasma / epoch

Particle-in-cell code for plasma physics simulations
https://epochpic.github.io
GNU General Public License v3.0
184 stars 58 forks source link

`Invalid write of size 1` when reading grid using Python/C library #693

Closed jthistle closed 2 months ago

jthistle commented 3 months ago

Valgrind picks up on an invalid write, which I think is leading to a segfault that I'm experiencing:

==156608== Invalid write of size 1
==156608==    at 0x4850B5D: mempcpy (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==156608==    by 0x4A023BD: _IO_file_xsgetn (fileops.c:1304)
==156608==    by 0x49F4FAE: fread (iofread.c:38)
==156608==    by 0x587D5A5: fread (stdio2.h:211)
==156608==    by 0x587D5A5: sdf_helper_read_array (sdf_input_cartesian.c:565)
==156608==    by 0x5879E73: sdf_read_array (sdf_input.c:1080)
==156608==    by 0x5879E73: sdf_read_data (sdf_input.c:445)
==156608==    by 0x589419A: sdf_callback_cpu_mesh (sdf_derived.c:1355)
==156608==    by 0x5879D8C: sdf_read_data (sdf_input.c:431)
==156608==    by 0x58689D6: Block_getdata (in /home/[--]/opt/anaconda3/envs/[--]/lib/python3.12/site-packages/sdf.cpython-312-x86_64-linux-gnu.so)
==156608==    by 0x33150D: _PyObject_GenericGetAttrWithDict (object.c:1430)
==156608==    by 0x317110: UnknownInlinedFun (object.c:1521)
==156608==    by 0x317110: PyObject_GetAttr (object.c:1044)
==156608==    by 0x220DF3: _PyEval_EvalFrameDefault.cold (bytecodes.c:1802)
==156608==    by 0x3CF6FD: PyEval_EvalCode (ceval.c:578)
==156608==  Address 0x22e5963f is 3,007 bytes inside an unallocated block of size 1,029,472 in arena "client"

The offending code is in sdf_helper_read_array at sdf_input_cartesian.c:565, and it looks like

FSEEKO(h->filehandle, offset, SEEK_SET);
if (!fread(read_ptr, 1, length, h->filehandle))
    return (offset - h->current_location);

I tried to debug this, but the SDF C codebase is pretty impenetrable to me. Can provide a file from which to reproduce if necessary.

keithbennett commented 3 months ago

Hi. Please provide the file from which to reproduce. I am on holiday until the 29th, but will take a look when I am back at work.

jthistle commented 3 months ago

I've sent the relevant files and further details by email.

keithbennett commented 2 months ago

This should now be fixed upstream with PR Warwick-Plasma/SDF_C#9 I will create a PR to update the SDF library in EPOCH later today

jthistle commented 2 months ago

Thanks Keith.