Occasional segfault in Cython

17 views
Skip to first unread message

Noon Chen

unread,
May 18, 2021, 2:47:14 AM5/18/21
to cython-users
Greetings!

I am testing a C hashmap implementation that was found in GitHub and noticing a weird situation, the Cython version of the test code gives me segfault occasionally (sometimes malloc error). However, I write a similar C version test code with the same logic and it just works without any error.

The C hashmap implementation and cython pxd file is located in : https://gist.github.com/noonchen/82007b4a33f7aa173355f98ef96bab8d.

Could it be a bug in Cython?

C test code:

#include "hashmap.h"
#include <assert.h>
#include <stdio.h>

int main() {
int err, a1, a2, a3, a4;
map_t testDict;
testDict = hashmap_new(1);

a1 = 1;
a2 = 100;
a3 = 56;
a4 = 9765;

err = hashmap_put(testDict, 1, a1);
assert (err==MAP_OK);
err = hashmap_put(testDict, 10000, a2);
assert (err==MAP_OK);
err = hashmap_put(testDict, 234, a3);
assert (err==MAP_OK);
err = hashmap_put(testDict, 0, a4);
assert (err==MAP_OK);

assert (hashmap_contains(testDict, 0) != 0);
assert (hashmap_contains(testDict, 234) != 0);
assert (hashmap_contains(testDict, 1) != 0);
assert (hashmap_contains(testDict, 10000) != 0);
assert (hashmap_contains(testDict, 555) == 0);

for (int i=0; i<10000; i++) {
err = hashmap_put(testDict, i, i);
assert (err==MAP_OK);
}

hashmap_free(testDict);
printf("all passed\n");
return 0;
}

Cython test code:

from libc.stdint cimport uint64_t
from hashmap_libc cimport *

cdef map_t testDict
cdef int err
cdef int a1, a2, a3, a4

testDict = hashmap_new(1)

a1 = 1
a2 = 100
a3 = 56
a4 = 9765

err = hashmap_put(testDict, 1, a1)
assert (err==MAP_OK)
err = hashmap_put(testDict, 10000, a2)
assert (err==MAP_OK)
err = hashmap_put(testDict, 234, a3)
assert (err==MAP_OK)
err = hashmap_put(testDict, 0, a4)
assert (err==MAP_OK)

assert (hashmap_contains(testDict, 0) != 0)
assert (hashmap_contains(testDict, 234) != 0)
assert (hashmap_contains(testDict, 1) != 0)
assert (hashmap_contains(testDict, 10000) != 0)
assert (hashmap_contains(testDict, 555) == 0)

for i in range(10000):
err = hashmap_put(testDict, i, i)
assert (err==MAP_OK)

hashmap_free(testDict)
print("all passed!")

Noon Chen

unread,
May 18, 2021, 3:45:21 AM5/18/21
to cython-users
FYI, the environment is:
Python 3.9.5
Cython 3.0a6

compile C code via: 
```
gcc --version

Apple clang version 12.0.5 (clang-1205.0.22.9)

Target: x86_64-apple-darwin20.4.0

Thread model: posix

InstalledDir: /Library/Developer/CommandLineTools/usr/bin

```

Stefan Behnel

unread,
May 18, 2021, 5:14:18 PM5/18/21
to cython...@googlegroups.com
Hi!

Noon Chen schrieb am 18.05.21 um 05:06:
> Greetings!
>
> I am testing a C hashmap implementation that was found in GitHub and
> noticing a weird situation, the Cython version of the test code gives me
> segfault occasionally (sometimes malloc error). However, I write a similar
> C version test code with the same logic and it just works without any error.
>
> The C hashmap implementation and cython pxd file is located in :
> https://gist.github.com/noonchen/82007b4a33f7aa173355f98ef96bab8d.

At a quick glance, I didn't see anything obviously wrong.


> Could it be a bug in Cython?

Never impossible, but also not very likely.

Try running it in a debugger like gdb to see where it crashes.

Stefan

Dan Stromberg

unread,
May 18, 2021, 7:40:27 PM5/18/21
to cython...@googlegroups.com


It's about how weird things can get when you reference undefined regions of memory.  It uses FORTRAN for its examples, but the same thing applies to C and Cython.


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/b1f4dd3a-1ae6-0097-8577-7637b6a0bab2%40behnel.de.


--
Dan Stromberg

Noon Chen

unread,
May 19, 2021, 4:22:50 AM5/19/21
to cython-users
Hey!

Thanks for your suggestions!

I am so embarrassed😂... There is a typo in the c function `hashmap_new`, the table size should be `size` instead of INITIAL_SIZE, now everything works as expected.

guess I am really lucky that I haven't encountered a single error in the C version code ;)

Besides, the setup of Cython gdb seems kind of hard in mac, so I am sticking to the basic printf debugging method...

Best Regards!

Reply all
Reply to author
Forward
0 new messages