Subject: Re: bug#75754: styled_format stack usage/GC protection

From: Eli Zaretskii <[email-masked]>
Date: Fri, 24 Jan 2025 17:28:53 +0200

> Date: Fri, 24 Jan 2025 15:02:20 +0000
> From: Pip Cet <[email-masked]>
> Cc: [email-masked], [email-masked], [email-masked]
> 
> > If you mean under what circumstances some internal function calls Lisp
> > (or can otherwise GC), then no, we don't document that anywhere.  I
> > just looked at the source code to answer your question.
> 
> (This is an honest question: do you believe Emacs would be worse off if
> we added an ASSUME_NO_GC macro like
> 
>   ASSUME_NO_GC (true);
>   p = SDATA (str);
> 
>   532 lines of code here
> 
>   use p for the last time
> 
>   ASSUME_NO_GC (false);

No, I don't think Emacs will be worse.  But I hope we will only have
to do this in not too-many places.  Although I'm unsure how to find
them and how to discern between them and the rest.  We'd need some
rules where to place these.

> >> >> feature/igc avoids all of these problems by keeping SDATA pointers
> >> >> valid, even when the string is resized.
> >> >
> >> > Unless the string is GC'ed, right?
> >>
> >> Even if it is GC'd.
> >>
> >> The SDATA will stay around while there are live pointers to it.  The
> >> SDATA doesn't keep the string alive, but there's no way to get a
> >> reference to the now-dead Lisp_String structure from the SDATA, so this
> >> won't cause bugs.
> >
> > Even if the code modifies the string after GC?
> 
> If the GC free'd the string, but not the SDATA structure, we cannot
> regenerate the string to call resize_string_data.  So, no, this cannot
> happen.
> 
> Note that resizing a live string while you have a live SDATA pointer to
> it might result in the SDATA pointer referring to the pre-modification
> SDATA, not the current one.

Who said anything about resizing?  I thought about changing a string
with SSET, for example, or Faset.

IOW, the scenario is:

  . we take a pointer to a string's SDATA
  . GC happens
  . we use SSET or Faset to modify the string
  . we use the above pointer

With the old GC, this is a no-no, but you seem to be saying that on
the igc branch it's okay to do that?