[v3] libio: do not unbuffer legacy standard files in compatibility mode [BZ #24228]

Message ID 20190619174649.GB25165@altlinux.org
State Superseded
Headers

Commit Message

Dmitry V. Levin June 19, 2019, 5:46 p.m. UTC
  On Wed, Jun 19, 2019 at 06:15:06PM +0200, Florian Weimer wrote:
> * Dmitry V. Levin:
> > On Wed, Jun 19, 2019 at 03:10:14PM +0200, Florian Weimer wrote:
> >> * Dmitry V. Levin:
> >> 
> >> > diff --git a/libio/genops.c b/libio/genops.c
> >> > index 2a0d9b81df..aa92d61b6b 100644
> >> > --- a/libio/genops.c
> >> > +++ b/libio/genops.c
> >> > @@ -789,6 +789,10 @@ _IO_unbuffer_all (void)
> >> >  
> >> >    for (fp = (FILE *) _IO_list_all; fp; fp = fp->_chain)
> >> >      {
> >> > +#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
> >> > +      if (__glibc_unlikely (&_IO_stdin_used == NULL) && _IO_legacy_file (fp))
> >> > +	continue;
> >> > +#endif
> >> >        if (! (fp->_flags & _IO_UNBUFFERED)
> >> >  	  /* Iff stream is un-orientated, it wasn't used. */
> >> >  	  && fp->_mode != 0)
> >> 
> >> I believe a better fix would be this, in case an old-style file showed
> >> up for a different reason:
> >> 
> >> #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
> >>           bool potentially_wide_stream = _IO_vtable_offset (fp) != 0;
> >> #else
> >>           bool potentially_wide_stream = true;
> >> #endif
> >> 	  if (potentially_wide_stream && fp->_mode > 0)
> >> 	    _IO_wsetb (fp, NULL, NULL, 0);
> >> 
> >> This is _IO_new_fclose handles this situation.
> >
> > Yes, this approach seems to work, too:
> >
> > diff --git a/libio/genops.c b/libio/genops.c
> > index 2a0d9b81df..575f0e6584 100644
> > --- a/libio/genops.c
> > +++ b/libio/genops.c
> > @@ -789,6 +789,10 @@ _IO_unbuffer_all (void)
> >  
> >    for (fp = (FILE *) _IO_list_all; fp; fp = fp->_chain)
> >      {
> > +#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
> > +      if (__glibc_unlikely (_IO_vtable_offset (fp) != 0))
> > +	continue;
> > +#endif
> >        if (! (fp->_flags & _IO_UNBUFFERED)
> >  	  /* Iff stream is un-orientated, it wasn't used. */
> >  	  && fp->_mode != 0)
> 
> Hmm, right there's an early access to fp->_mode that I had missed.
> Should we still flush buffers in old binaries?
> 
> I think we could do this instead:
> 
>   int mode;
> #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
>   if (__glibc_unlikely (_IO_vtable_offset (fp) != 0))
>     mode = 1; /* Old streams are never wide.  */
>   else
>     mode = fp->_mode;
> #else
>   mode  = fp->_mode;
> #endif
> 
> And then use mode instead of fp->_mode below.  Does this make sense?

_mode is not the only member that is not available in legacy libio,
_freeres_list and _freeres_buf also should be avoided.
Would you prefer the following approach?
  

Comments

Florian Weimer June 19, 2019, 7:04 p.m. UTC | #1
* Dmitry V. Levin:

> _mode is not the only member that is not available in legacy libio,
> _freeres_list and _freeres_buf also should be avoided.
> Would you prefer the following approach?
>
> diff --git a/libio/genops.c b/libio/genops.c
> index 2a0d9b81df..11a15549e8 100644
> --- a/libio/genops.c
> +++ b/libio/genops.c
> @@ -789,9 +789,16 @@ _IO_unbuffer_all (void)
>  
>    for (fp = (FILE *) _IO_list_all; fp; fp = fp->_chain)
>      {
> +      int legacy = 0;
> +
> +#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
> +      if (__glibc_unlikely (_IO_vtable_offset (fp) != 0))
> +	legacy = 1;
> +#endif

This approach looks reasonable to me.  It's certainly much better than
what we have today.  If you post a full patch, I'll test it against
one of the problematic OpenJDK 8 builds.
  

Patch

diff --git a/libio/genops.c b/libio/genops.c
index 2a0d9b81df..11a15549e8 100644
--- a/libio/genops.c
+++ b/libio/genops.c
@@ -789,9 +789,16 @@  _IO_unbuffer_all (void)
 
   for (fp = (FILE *) _IO_list_all; fp; fp = fp->_chain)
     {
+      int legacy = 0;
+
+#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
+      if (__glibc_unlikely (_IO_vtable_offset (fp) != 0))
+	legacy = 1;
+#endif
+
       if (! (fp->_flags & _IO_UNBUFFERED)
 	  /* Iff stream is un-orientated, it wasn't used. */
-	  && fp->_mode != 0)
+	  && (legacy || fp->_mode != 0))
 	{
 #ifdef _IO_MTSAFE_IO
 	  int cnt;
@@ -805,7 +812,7 @@  _IO_unbuffer_all (void)
 	      __sched_yield ();
 #endif
 
-	  if (! dealloc_buffers && !(fp->_flags & _IO_USER_BUF))
+	  if (! legacy && ! dealloc_buffers && !(fp->_flags & _IO_USER_BUF))
 	    {
 	      fp->_flags |= _IO_USER_BUF;
 
@@ -816,7 +823,7 @@  _IO_unbuffer_all (void)
 
 	  _IO_SETBUF (fp, NULL, 0);
 
-	  if (fp->_mode > 0)
+	  if (! legacy && fp->_mode > 0)
 	    _IO_wsetb (fp, NULL, NULL, 0);
 
 #ifdef _IO_MTSAFE_IO
@@ -827,7 +834,8 @@  _IO_unbuffer_all (void)
 
       /* Make sure that never again the wide char functions can be
 	 used.  */
-      fp->_mode = -1;
+      if (! legacy)
+	fp->_mode = -1;
     }
 
 #ifdef _IO_MTSAFE_IO