From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: divoplade Newsgroups: gmane.lisp.guile.user Subject: Re: Calling back scheme from C Date: Tue, 22 Sep 2020 17:48:37 +0200 Message-ID: <2702bcfe265858200b4d5c049c333aafafc0bf43.camel@divoplade.fr> References: <3a0ccd31a195f1544038b4ccc5ff1d15667a8e0d.camel@divoplade.fr> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31751"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Evolution 3.34.2 To: guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Tue Sep 22 17:51:27 2020 Return-path: Envelope-to: guile-user@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 1kKkZi-00087c-Br for guile-user@m.gmane-mx.org; Tue, 22 Sep 2020 17:51:26 +0200 Original-Received: from localhost ([::1]:53928 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKkZh-00010z-0t for guile-user@m.gmane-mx.org; Tue, 22 Sep 2020 11:51:25 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:41788) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKkX7-00083z-B7 for guile-user@gnu.org; Tue, 22 Sep 2020 11:48:45 -0400 Original-Received: from relay5-d.mail.gandi.net ([217.70.183.197]:40503) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKkX4-0001r7-P2 for guile-user@gnu.org; Tue, 22 Sep 2020 11:48:45 -0400 X-Originating-IP: 86.194.83.229 Original-Received: from divoplade.home (lfbn-lyo-1-1007-229.w86-194.abo.wanadoo.fr [86.194.83.229]) (Authenticated sender: d@divoplade.fr) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id BB9A71C000D for ; Tue, 22 Sep 2020 15:48:38 +0000 (UTC) In-Reply-To: <3a0ccd31a195f1544038b4ccc5ff1d15667a8e0d.camel@divoplade.fr> Received-SPF: pass client-ip=217.70.183.197; envelope-from=d@divoplade.fr; helo=relay5-d.mail.gandi.net X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 09:55:05 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:16949 Archived-At: Hello all, It seems that I'm making progress. scm_call_* seems to corrupt something if the value is a lambda calling (use-modules) I tested with srfi srfi-1 or rnrs bytevectors. Without any of them, calling the function and discarding its results does not crash the program. With any or both of them, calling the function (still discarding the result, it does not matter) corrupts some values (=> assert failure in foreign library code). I will make further tests... divoplade Le mardi 22 septembre 2020 à 08:25 +0200, divoplade a écrit : > Hello guile, > > I am having a hard time understanding what I do wrong when trying to > pass a guile function as a C callback (from C). > > You should be able to trigger the bug by saving the 3 attached files > and running: > > guix build -L . pkg-with-the-bug > > The bug disappears when ignoring the scheme callback, so I suspect it > has something to do with the callback and not further functions: > > guix build -L . pkg-without-the-bug > > Does anyone see an error? > > You will find the C code under libguile-nettle.c. guile-nettle.c is > just an ad-hoc interpreter with the primitives built in, to run the > test. The test is example-rsa-sign.in: it defines a callback that > will > return a bytevector of length its argument, and that bytevector will > be > processed to generate an RSA key pair with nettle. The function with- > rsa-generated-key-pair takes the callback plus other arguments and a > function, and it will call that function with the public key and the > private key. > > (define (random-with-guile length) > (use-modules (srfi srfi-1)) > (use-modules (rnrs bytevectors)) > (u8-list->bytevector > (unfold (lambda (i) (>= i length)) > (lambda (i) (random 256)) > 1+ > 0))) > > (define (generate-rsa-keypair nbits random progress) > (with-rsa-generated-key-pair random progress nbits 16 > (lambda (public private) > (rsa-public-key-n public)))) > > (define keypair > (generate-rsa-keypair 2048 random-with-guile #f)) > > (if keypair > (format #t "The key pair has been generated.\n") > (begin > (format (current-error-port) > "The key pair could not be generated.\n") > (exit 1))) > > Best regards, > > divoplade