From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2495 invoked by alias); 24 Apr 2012 19:41:21 -0000 Received: (qmail 2396 invoked by uid 22791); 24 Apr 2012 19:41:20 -0000 X-SWARE-Spam-Status: No, hits=-0.2 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_THREADED,RCVD_IN_DNSWL_LOW,RCVD_IN_HOSTKARMA_YE,TW_BF X-Spam-Check-By: sourceware.org Received: from mail-lpp01m010-f41.google.com (HELO mail-lpp01m010-f41.google.com) (209.85.215.41) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 24 Apr 2012 19:41:06 +0000 Received: by lagz14 with SMTP id z14so959589lag.0 for ; Tue, 24 Apr 2012 12:41:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding:x-gm-message-state; bh=LLtcAufpfEwkJ0mEECSKskx53Wk3ajW9mVo5FjT6pkk=; b=C0qsIm4JZ29gEEm9xOPhzd0X5eI5i4YiS3fb5OozK8XBsGAXv4oHY9sLsbn52ZhB9A GH+JcriYW9UdRRt6wPU/Rn6uWWm/boaMIv2ANi00gQo8dgXWYFXusb42Lgx4uwSobgW0 xM6xm6kKv5HX75TsGOpXhGILizshASrcwMIoaq0JRFst/y8pJIFr4URkHd6gnS0+3/bn PA3jbSwayisH5QrEiLzpH1EqZlBdtacch0xMjkiwsEb18uHAYZv9qjr7eMCzlTSlpyKc rNCLH3kY17a3/6+QlLSm7fQH6FwKOyMWGd1Z/vAapo35U4RChrcSLx7AEbbssiySk+Jw qjQw== Received: by 10.152.103.134 with SMTP id fw6mr20340243lab.20.1335296464467; Tue, 24 Apr 2012 12:41:04 -0700 (PDT) MIME-Version: 1.0 Received: by 10.112.23.68 with HTTP; Tue, 24 Apr 2012 12:40:44 -0700 (PDT) In-Reply-To: References: From: Gaash Hazan Date: Tue, 24 Apr 2012 19:41:00 -0000 Message-ID: Subject: libffi & fork To: libffi-discuss@sourceware.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQlnAwQauQ4IPsy8maBqtbwjWPljKElkzMOzp05S5JvGeBe7Qop1PR0VctebEYb0U4XEEDbN X-IsSubscribed: yes Mailing-List: contact libffi-discuss-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libffi-discuss-owner@sourceware.org X-SW-Source: 2012/txt/msg00180.txt.bz2 Hi experts, I am new to libffi, so forgive me for my ignorance. I have a python crash involving ctypes and functions callbacks (read closures) and fork. The crash happens when process is forked, some callbacks are modified and a function callback is made. I suspect that the problem could be related to libffi closures and fork. dlmmap() at libffi/closures.c allocates memory block that has to be both read-write and read-execute. To achieve that, libffi creates a temp file and performs two mmap-s to the that file, the first read-write and the 2nd read-execute. 7fe27d64f000-7fe27d650000 rw-s 00000000 08:02 1840563 =A0 /tmp/ffiHf53x5 (deleted) 7fe27d650000-7fe27d651000 r-xs 00000000 08:02 1840563 =A0 /tmp/ffiHf53x5 (deleted) The thing is that those maps are marked as shared. Hence when the process is forked the allocated(mapped) memory block is *shared* between the processes (and not copied-on-write). So, when father process changes something in that memory block, the child process will be affected as well, creating unplanned shared-memory block between the two processes. I suspect that this is not the intended behavior and that it leads to the crash. snips from /proc/XXX/maps: father PID=3D7975, child PID=3D7976 ---------------------------------------------------------------------------= ------------- $cat /proc/7975/maps=A0 | grep /tmp/ 7fc537d49000-7fc537d4a000 rw-s 00000000 08:02 1840370 =A0 /tmp/ffiqyLKFR (deleted) 7fc537d4a000-7fc537d4b000 r-xs 00000000 08:02 1840370 =A0 /tmp/ffiqyLKFR (deleted) $cat /proc/7976/maps=A0 | grep /tmp/ 7fc537d49000-7fc537d4a000 rw-s 00000000 08:02 1840370 =A0 /tmp/ffiqyLKFR (deleted) 7fc537d4a000-7fc537d4b000 r-xs 00000000 08:02 1840370 =A0 /tmp/ffiqyLKFR (deleted) test .py: ----------- import os,time import ctypes os.fork() print str(os.getpid()) time.sleep( 1000) system info: centos 6.0, pyhton 2.6.5, selinux disabled, x86_64. Also tested with python 2.7.3 Thanks, Gaash