unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: divoplade <d@divoplade.fr>
To: guile-user <guile-user@gnu.org>
Subject: Re: Calling back scheme from C
Date: Tue, 22 Sep 2020 17:48:37 +0200	[thread overview]
Message-ID: <2702bcfe265858200b4d5c049c333aafafc0bf43.camel@divoplade.fr> (raw)
In-Reply-To: <3a0ccd31a195f1544038b4ccc5ff1d15667a8e0d.camel@divoplade.fr>

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




      parent reply	other threads:[~2020-09-22 15:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-22  6:25 Calling back scheme from C divoplade
2020-09-22  9:50 ` Chris Vine
2020-09-22 10:36   ` divoplade
2020-09-22 14:47     ` Chris Vine
2020-09-22 15:48 ` divoplade [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2702bcfe265858200b4d5c049c333aafafc0bf43.camel@divoplade.fr \
    --to=d@divoplade.fr \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).