guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calling back scheme from C


From: divoplade
Subject: Re: Calling back scheme from C
Date: Tue, 22 Sep 2020 17:48:37 +0200
User-agent: Evolution 3.34.2

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]