[plt-scheme] FFI error leads to memory corruption

From: Noel Welsh (noelwelsh at gmail.com)
Date: Fri Sep 11 06:04:14 EDT 2009

Hi all,

I'm seeing a consistent and persistent SIGSEGV only when using the
FFI. Running under valgrind, the C code is absolutely fine, but writes
to pointers to double leads to complaints like

  ==19454== Invalid write of size 4

I attach a small test program that stimulates the problem. Run

  valgrind mzscheme -j -e '(require "testc.ss")'

and you'll see the error. Platform:

> uname -a
Linux hpc-005 2.6.18-128.7.1.el5 #1 SMP Mon Aug 24 08:20:55 EDT 2009
i686 i686 i386 GNU/Linux

> mzscheme
Welcome to MzScheme v4.2.1.3 [3m], Copyright (c) 2004-2009 PLT Scheme Inc

To reiterate, it appears that the FFI code generated by (_ptr o
_double) is causing the problem.

N.


test.c:

#include <math.h>

// gcc -std=gnu99 -O -Wall -fPIC -msse2 -march=pentium4 -mfpmath=sse
-lm -shared -o test.so test.c

void foo(double x, double y, double *z)
{
  *z = sin(x) + cos(y);
  return;
}


testc.ss:

#lang scheme

(require
 scheme/foreign)

(unsafe!)

(define foo
  (get-ffi-obj
   "foo"
   (ffi-lib "test")
   (_fun _double _double (out : (_ptr o _double)) -> _void -> out)))

(foo 1.0 2.0)

(provide
 foo)


Posted on the users mailing list.