From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Zhu Zihao Newsgroups: gmane.lisp.guile.bugs Subject: bug#58498: Foreign callback returned by procedure->pointer cannot be executed in foreign thread. Date: Fri, 14 Oct 2022 00:01:08 +0800 Message-ID: <86bkqfiu7s.fsf@163.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31320"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.8.9; emacs 29.0.50 To: 58498@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Thu Oct 13 18:55:36 2022 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oj1Uc-0007yO-W2 for guile-bugs@m.gmane-mx.org; Thu, 13 Oct 2022 18:55:35 +0200 Original-Received: from localhost ([::1]:45166 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oj1Ub-0000mR-UW for guile-bugs@m.gmane-mx.org; Thu, 13 Oct 2022 12:55:33 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55282) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oj0z8-0004Z4-SZ for bug-guile@gnu.org; Thu, 13 Oct 2022 12:23:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:35653) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oj0z8-0004AV-Ie for bug-guile@gnu.org; Thu, 13 Oct 2022 12:23:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oj0z8-0006OJ-Cp for bug-guile@gnu.org; Thu, 13 Oct 2022 12:23:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Zhu Zihao Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Thu, 13 Oct 2022 16:23:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 58498 X-GNU-PR-Package: guile X-Debbugs-Original-To: bug-guile@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.166567814724498 (code B ref -1); Thu, 13 Oct 2022 16:23:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 13 Oct 2022 16:22:27 +0000 Original-Received: from localhost ([127.0.0.1]:34727 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oj0yZ-0006N3-4Q for submit@debbugs.gnu.org; Thu, 13 Oct 2022 12:22:27 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:58256) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oj0yO-0006MZ-Fm for submit@debbugs.gnu.org; Thu, 13 Oct 2022 12:22:25 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37522) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oj0yO-0003VF-8v for bug-guile@gnu.org; Thu, 13 Oct 2022 12:22:16 -0400 Original-Received: from mail-m974.mail.163.com ([123.126.97.4]:54185) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oj0yI-00044z-3W for bug-guile@gnu.org; Thu, 13 Oct 2022 12:22:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version; bh=pG/Pc FDseFCJju06lHoAI1SCoBY8gojs1WvROK/3DYI=; b=QM2a4sKODCW5ljbpkicOl IGIn0Z1tp78s4h3HOcsYvn8QeaiBIPZx9QDKNXjaOk4pJ2JgClWytr4uPpVjT+x4 xxScjnHz1lE3zJv3XjIc+G5IZxb92mZPK4PJ5R8WJKyis87Yc8h3xvlS5is92l9P k1igLSi3LRVedNdMddPJsI= Original-Received: from asus-laptop (unknown [163.125.249.49]) by smtp4 (Coremail) with SMTP id HNxpCgBXqIiTN0hjSBadkg--.76S2; Fri, 14 Oct 2022 00:06:46 +0800 (CST) X-CM-TRANSID: HNxpCgBXqIiTN0hjSBadkg--.76S2 X-Coremail-Antispam: 1Uf129KBjvdXoW7Jr1ftw4fGr13JrWrurWrGrg_yoWkWrc_WF WfAr4UJr47CF43Gw4akr1DAF4fWwnxZw1fXrW8tryxtry8K39xXFWv9rsFka13Wrn2vr98 CrW09FWY93WxWjkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7IUjqXd5UUUUU== X-Originating-IP: [163.125.249.49] X-CM-SenderInfo: pdoosuxxwbztlvw6il2tof0z/xtbBoQeZr2I0VKkzRgABsP Received-SPF: pass client-ip=123.126.97.4; envelope-from=all_but_last@163.com; helo=mail-m974.mail.163.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.io gmane.lisp.guile.bugs:10401 Archived-At: --=-=-= Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" --==-=-= Content-Type: text/plain The foreign function pointer returned by "procedure->pointer" can only executed in a thread which Scheme context is already initialized. This may not so convenient because foreign library may create its own thread and run our callback in foreign thread. To show what will happen, this is a small example using Guile FFI & pthread API to create a foreign thread and run foreign callback in it. ``` (begin (use-modules (rnrs bytevectors) (system foreign) (system foreign-library)) (define libc (load-foreign-library #f)) (define pthread_create (foreign-library-function libc "pthread_create" #:return-type int #:arg-types `(* * * *))) (define pthread-handle-ret (make-bytevector 4)) (define thread-runner (procedure->pointer '* (lambda (_) (display "I' m from a foreign thread!\n") %null-pointer) `(*))) (pthread_create (bytevector->pointer pthread-handle-ret) %null-pointer thread-runner %null-pointer)) ``` In Guile 3.0.8. This will cause segfault and crash. My patch to fix this problem is attached below. It wraps the real execution of foreign callback in "scm_with_guile". --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iIsEARYIADMWIQT4UAIrVkIEZilSHr2K2nJqP6LM8gUCY0g3hxUcYWxsX2J1dF9s YXN0QDE2My5jb20ACgkQitpyaj+izPJ16wD8Didfhvla24rMdHSM3v01gSu7J48D t0TjU/iHLs3LJJ4BAKqDTTEirF0eGZhG/THxjcCvftF17NWFegBnJFDUdVUA =Tob6 -----END PGP SIGNATURE----- --==-=-=-- --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Allow-closure-returned-by-procedure-pointer-executed.patch >From c6ede90552019a5851e4ba0de41fa9dfd3269b38 Mon Sep 17 00:00:00 2001 From: Zhu Zihao Date: Fri, 14 Oct 2022 00:00:32 +0800 Subject: [PATCH] Allow closure returned by procedure->pointer executed in foreign thread. * libguile/foreign.c (invoke_closure): Move the core logci to "do_invoke_closure". Wrap it by "scm_with_guile". (do_invoke_closure): New function. --- libguile/foreign.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/libguile/foreign.c b/libguile/foreign.c index 1f594b0e4..2f9a8469a 100644 --- a/libguile/foreign.c +++ b/libguile/foreign.c @@ -1148,13 +1148,19 @@ scm_i_foreign_call (SCM cif_scm, SCM pointer_scm, int *errno_ret, #ifdef FFI_CLOSURES -/* Trampoline to invoke a libffi closure that wraps a Scheme - procedure. */ -static void -invoke_closure (ffi_cif *cif, void *ret, void **args, void *data) +static void * +do_invoke_closure (void *outer_data) { + ffi_cif *cif; + void *ret, **args, *data; size_t i; SCM proc, *argv, result; + void ** outer_args = (void **) outer_data; + + cif = outer_args[0]; + ret = outer_args[1]; + args = outer_args[2]; + data = outer_args[3]; proc = SCM_PACK_POINTER (data); @@ -1167,6 +1173,21 @@ invoke_closure (ffi_cif *cif, void *ret, void **args, void *data) result = scm_call_n (proc, argv, cif->nargs); unpack (cif->rtype, ret, result, 1); + + return NULL; +} + +/* Trampoline to invoke a libffi closure that wraps a Scheme + procedure. */ +static void +invoke_closure (ffi_cif *cif, void *ret, void **args, void *data) +{ + void *outer_args[4] = { cif, ret, args, data }; + + /* Foreign code may call this Scheme closure in a context which Guile is not + initialized. */ + scm_with_guile (do_invoke_closure, outer_args); + } SCM_DEFINE (scm_procedure_to_pointer, "procedure->pointer", 3, 0, 0, -- 2.38.0 --=-=-= Content-Type: text/plain -- Retrieve my PGP public key: gpg --recv-keys B3EBC086AB0EBC0F45E0B4D433DB374BCEE4D9DC Zihao --=-=-=--