segfault calling SSE enabled library from ctypes

Olivier Grisel olivier.grisel at ensta.org
Mon Nov 24 19:14:32 EST 2008


Hello,

It seems that I am able to reproduce the same problem as reported
earlier on this list by someone else:

  http://mail.python.org/pipermail/python-list/2008-October/511794.html

Similar setup: python 2.5.2 / gcc (Ubuntu 4.3.2-1ubuntu11) from
Intrepid on 32bit intel Core 2 Duo.  I can confirm this is not related
to any alignment problem from data passed from python, I took care of
that and I could even reproduce the problem with the following minimal
test case that does not use any external data (you can fetch the
following source here
http://www.bitbucket.org/ogrisel/ctypes_sse/get/tip.gz )

<sample>

=== dummysse.c ===

#include <stdio.h>
#include <emmintrin.h>

void dummy_sse(void)
{
	// allocate an alligned vector of 128 bits
	__m128 myvector;

	printf("[dummy_sse] before calling setzero\n");
	fflush(stdout);

	// initialize it to 4 32 bits float valued to zeros
	myvector = _mm_setzero_ps();

	printf("[dummysse] after calling setzero\n");
	fflush(stdout);

	// display the content of the vector
	float* part = (float*) &myvector;
	printf("[dummysse] myvector = {%f, %f, %f, %f}\n",
			part[0], part[1], part[2], part[3]);
}

int main()
{
	dummy_sse();
	return 0;
}

=== dummysse.py ===

from ctypes import cdll

lib = cdll.LoadLibrary('./dummysse.so')
lib.dummy_sse()


=== Makefile ===
CC = gcc
CFLAGS = -Wall -g -O0 -msse2

all: dummysse dummysse.so

dummysse:
	$(CC) $(CFLAGS) $(LIBS) -o dummysse dummysse.c
#	./dummysse

dummysse.so:
	$(CC) $(CFLAGS) $(LIBS) -shared -o dummysse.so dummysse.c
#	python dummysse.py

clean:
	rm -f dummysse dummysse.so

</sample>

By running the main of the C program I get the expected behavior:

  gcc -Wall -g -O0 -msse2  -o dummysse dummysse.c
  ./dummysse
  [dummy_sse] before calling setzero
  [dummysse] after calling setzero
  [dummysse] myvector = {0.000000, 0.000000, 0.000000, 0.000000}

Running from python, the call to the _mm_setzero_ps() segfaults:

  gcc -Wall -g -O0 -msse2  -shared -o dummysse.so dummysse.c
  python dummysse.py
  [dummy_sse] before calling setzero
  Segmentation fault

Is this to be expected? The result to a call to "valgrind python
dummysse.py" is available here :

  http://www.bitbucket.org/ogrisel/ctypes_sse/src/tip/valgrind.log

I am not familiar with python internal at all so I cannot understand
what's wrong. You can notice that valgrind make the program run till
the end and display the correct results (4 zeros) on stdout while
logging a bunch of errors (most of those are not related to our
problem since they appear when launching python on an empty script).

-- 
Olivier



More information about the Python-list mailing list