all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Andy Wingo <wingo@igalia.com>
To: "Ludovic Courtès" <ludo@gnu.org>
Cc: 27476@debbugs.gnu.org
Subject: bug#27476: libguile/memoize.c is not thread safe, so syntax parameter expansion is not thread-safe
Date: Wed, 06 Feb 2019 17:14:37 +0100	[thread overview]
Message-ID: <87o97o99n6.fsf@igalia.com> (raw)
In-Reply-To: <87lg2t7z1o.fsf@gnu.org> ("Ludovic Courtès"'s message of "Wed, 06 Feb 2019 15:48:51 +0100")

Hi!

On Wed 06 Feb 2019 15:48, Ludovic Courtès <ludo@gnu.org> writes:

> I drew the conclusion that our syntax parameter is redefined when we
> compile or when we load (guix monads), so there’s a chance that we get
> to see the wrong value when we expand (guix monads) (I’m not entirely
> sure about the exact sequence of events.)

You are a wizard!!!!

To be clear, here's the series of events.  Firstly, know that defining a
syntax parameter is like:

  (define name
    (make-syntax-transformer 'name 'syntax-parameter (list f)))

So at the top level you end up with an association between a name and a
"syntax transformer" object (see macros.[ch]).  The syntax transformer
object itself consists of its name (for debugging), its syntax type, and
its syntax binding.

For syntax parameters, the binding is a list containing a single
element, the syntax transformer.  This list is later used as a key into
a compile-time environment, as it's a unique object associated with the
syntax parameter.

When (syntax-parameterize ((name f*)) exp) is seen, psyntax will look up
`name' in the current expansion-time environment.  It asserts that the
name is bound to a syntax transformer and that the syntax transformer is
indeed a syntax parameter, and extracts the associated binding `b'.
Keep in bind that `b' is the single-element list containing the
"default" syntax transformer `f'.

syntax-parameterize then does something weird: it adds an association
between the binding value `b' and `f*' to the expand-time environment.
It does this because the `b' is just a fresh object, so it's a unique
key that's usable for associations.  (The way this works is my fault
FWIW.)  To be clear, it doesn't add a new definition of `name'; it
instead establishes a new lexical binding for the unique object `b'.

Then when a use of `name' is seen within `exp', Guile finds that `name'
is a syntax parameter, extracts the binding from the syntax transformer
object, then does a second lookup of that binding.  If it finds
something bound, it uses that, otherwise it uses the default binding.

I think you see the race here.  For an initial state of

        (define P (stx-param (list F)))

we have:

           thread A                       thread B
                            time
    resolve P                 |
    extract B                 |
    associate B and F*        |
                              |   define P (stx-param (list F**))
    resolve P                 |
    extract B (!)             |
    resolve B (!)             |
    see F** instead of F* (!) |
                              v

> So I came up with ‘define-syntax-parameter-once’, which is like
> ‘define-once’ but for syntax parameters (note that we can’t use
> ‘define-once’ in ‘define-syntax-parameter-once’ because it expands to a
> reference to NAME, which doesn’t work for a macro):

Your fix is good!  But, it prevents redefinition of syntax parameters.

I would like to work on a solution that instead of using this
double-lookup, simply adds an association between P and F* in the
environment, instead of doing the double-lookup thing.  Probably that
will be 3.0-only.

For 2.2, we can probably update the compiler to trampoline through some
kind of "redefine-syntax" or something that will do (set-car! B F**)
instead of (define P (stx-param B*)).  I.e. redefinition keeps the
unique key there.

Andy

  reply	other threads:[~2019-02-06 16:15 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-22  8:38 guix pull fails on powerful server Ricardo Wurmus
2017-09-22 14:10 ` bug#27476: " Ludovic Courtès
2017-09-25  7:27   ` Andy Wingo
2017-09-25 13:03     ` Ludovic Courtès
2017-09-25 14:02       ` bug#27476: " Ricardo Wurmus
2017-09-30  7:59         ` Ricardo Wurmus
2017-09-30  7:59         ` Ricardo Wurmus
2017-10-03 20:29           ` Marius Bakke
2017-10-04 13:15             ` bug#27476: " Ricardo Wurmus
2017-10-04 13:15             ` Ricardo Wurmus
2017-10-03 20:29           ` bug#27476: " Marius Bakke
2017-10-04 15:09           ` Clément Lassieur
2017-10-04 16:17             ` bug#27476: " Ricardo Wurmus
2017-10-04 15:09           ` Clément Lassieur
2017-10-07 15:11           ` Ludovic Courtès
2017-10-10  7:17             ` Ricardo Wurmus
2017-10-10 11:32               ` Ludovic Courtès
2017-10-10 11:32               ` bug#27476: " Ludovic Courtès
2017-10-10  7:17             ` Ricardo Wurmus
2017-10-07 15:11           ` Ludovic Courtès
2017-10-12 13:37           ` Ludovic Courtès
2017-10-13 20:29             ` Ricardo Wurmus
2017-10-13 20:29             ` Ricardo Wurmus
2017-10-13 21:04             ` Ricardo Wurmus
2017-10-13 21:10               ` Ricardo Wurmus
2017-10-12 13:37           ` Ludovic Courtès
2017-11-07 10:57           ` Ludovic Courtès
2018-04-30 21:19             ` Ludovic Courtès
2018-04-30 21:39               ` bug#27476: libguile/memoize.c is not thread safe, so syntax parameter expansion is not thread-safe Ludovic Courtès
2018-05-09  8:41                 ` Andy Wingo
2018-05-09  9:23                   ` Ludovic Courtès
2018-05-09 10:18                     ` Andy Wingo
2019-02-06 14:48                       ` Ludovic Courtès
2019-02-06 16:14                         ` Andy Wingo [this message]
2019-02-06 22:09                           ` Ludovic Courtès
2019-01-29 10:07               ` bug#27476: guix pull fails on powerful server Ricardo Wurmus
2019-01-29 10:07               ` Ricardo Wurmus
2017-11-07 10:57           ` Ludovic Courtès
2017-09-25 13:03     ` Ludovic Courtès
2017-09-25  8:43 ` Clément Lassieur
2017-09-25  8:43 ` bug#27476: " Clément Lassieur

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

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

  git send-email \
    --in-reply-to=87o97o99n6.fsf@igalia.com \
    --to=wingo@igalia.com \
    --cc=27476@debbugs.gnu.org \
    --cc=ludo@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.