Warwick-Plasma/epoch

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

jthistle opened this issue · 4 comments

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.

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.

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

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

Thanks Keith.