public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Carlos O'Donell <carlos@redhat.com>
To: Florian Weimer <fweimer@redhat.com>, libc-alpha@sourceware.org
Subject: Re: [PATCH 08/19] nptl: Use __pthread_attr_copy in pthread_getattr_default_np (bug 25999)
Date: Wed, 20 May 2020 10:42:56 -0400	[thread overview]
Message-ID: <986cb140-5a63-51ac-95a4-526a20d0e959@redhat.com> (raw)
In-Reply-To: <f4bd12d279f8f49e69cb845b0aac431f97fa6331.1589884403.git.fweimer@redhat.com>

On 5/19/20 6:44 AM, Florian Weimer via Libc-alpha wrote:
> pthread_getattr_default_np needs to make a deep copy.

Agreed, this seems like an oversight regarding the affinity.
I expect not enough programs ever use the kind of setup you run in
the test, but it's still wrong, we should be doing a deep copy.

Tested clean on x86_64 and i686.

OK for master.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Tested-by: Carlos O'Donell <carlos@redhat.com>

> ---
>  nptl/Makefile                       |  1 +
>  nptl/pthread_getattr_default_np.c   | 12 ++---
>  nptl/tst-pthread-defaultattr-free.c | 78 +++++++++++++++++++++++++++++
>  3 files changed, 82 insertions(+), 9 deletions(-)
>  create mode 100644 nptl/tst-pthread-defaultattr-free.c
> 
> diff --git a/nptl/Makefile b/nptl/Makefile
> index b4aaad0f20..e5686b20ac 100644
> --- a/nptl/Makefile
> +++ b/nptl/Makefile
> @@ -328,6 +328,7 @@ tests = tst-attr2 tst-attr3 tst-default-attr \
>  	tst-thread-affinity-pthread \
>  	tst-thread-affinity-pthread2 \
>  	tst-thread-affinity-sched \
> +	tst-pthread-defaultattr-free \

OK.

>  
>  
>  tests-container =  tst-pthread-getattr
> diff --git a/nptl/pthread_getattr_default_np.c b/nptl/pthread_getattr_default_np.c
> index cce20cbe94..a9665c5df7 100644
> --- a/nptl/pthread_getattr_default_np.c
> +++ b/nptl/pthread_getattr_default_np.c
> @@ -16,20 +16,14 @@
>     License along with the GNU C Library; if not, see
>     <https://www.gnu.org/licenses/>.  */
>  
> -#include <errno.h>
> -#include <stdlib.h>
>  #include <pthreadP.h>
>  
>  int
>  pthread_getattr_default_np (pthread_attr_t *out)
>  {
> -  struct pthread_attr *real_out;
> -
> -  real_out = (struct pthread_attr *) out;
> -
>    lll_lock (__default_pthread_attr_lock, LLL_PRIVATE);
> -  *real_out = __default_pthread_attr;
> +  int ret = __pthread_attr_copy (out,
> +                                 (pthread_attr_t *) &__default_pthread_attr);

OK. Locks kept in place, and call to __pthread_attr_copy owns the struct.

>    lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
> -
> -  return 0;
> +  return ret;
>  }
> diff --git a/nptl/tst-pthread-defaultattr-free.c b/nptl/tst-pthread-defaultattr-free.c
> new file mode 100644
> index 0000000000..5a84302380
> --- /dev/null
> +++ b/nptl/tst-pthread-defaultattr-free.c
> @@ -0,0 +1,78 @@
> +/* Test for user-after-free bug in pthread_getattr_default_np (bug 25999).

OK.

> +   Copyright (C) 2020 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <pthread.h>
> +#include <sched.h>
> +#include <support/check.h>
> +#include <support/xthread.h>
> +
> +static int
> +do_test (void)
> +{
> +  /* This is a typical affinity size.  */
> +  enum { cpu_count = 128 };
> +  cpu_set_t *set = CPU_ALLOC (cpu_count);
> +  size_t set_size = CPU_ALLOC_SIZE (cpu_count);
> +  CPU_ZERO_S (set_size, set);
> +  CPU_SET (1, set);
> +  CPU_SET (3, set);

OK.

> +
> +  /* Apply the affinity mask to the default attribute.  */
> +  pthread_attr_t attr;
> +  xpthread_attr_init (&attr);
> +  TEST_COMPARE (pthread_attr_setaffinity_np (&attr, set_size, set), 0);
> +  TEST_COMPARE (pthread_setattr_default_np (&attr), 0);

OK. Set the default.

> +  xpthread_attr_destroy (&attr);

OK.

> +
> +  /* Read back the default attribute and check affinity mask.  */
> +  pthread_getattr_default_np (&attr);
> +  CPU_ZERO_S (set_size, set);
> +  TEST_COMPARE (pthread_attr_getaffinity_np (&attr, set_size, set), 0);
> +  for (int i = 0; i < cpu_count; ++i)
> +    TEST_COMPARE (!!CPU_ISSET (i, set), i == 1 || i == 3);

OK.

> +
> +
> +  /* Apply a larger CPU affinity mask to the default attribute, to
> +     trigger reallocation.  */
> +  {
> +    cpu_set_t *large_set = CPU_ALLOC (4 * cpu_count);
> +    size_t large_set_size = CPU_ALLOC_SIZE (4 * cpu_count);
> +    CPU_ZERO_S (large_set_size, large_set);
> +    pthread_attr_t large_attr;
> +    xpthread_attr_init (&large_attr);
> +    TEST_COMPARE (pthread_attr_setaffinity_np (&large_attr,
> +                                               large_set_size, large_set), 0);
> +    TEST_COMPARE (pthread_setattr_default_np (&large_attr), 0);

OK.

> +    xpthread_attr_destroy (&large_attr);
> +    CPU_FREE (large_set);
> +  }
> +
> +  /* Read back the default attribute and check affinity mask.  */
> +  CPU_ZERO_S (set_size, set);
> +  TEST_COMPARE (pthread_attr_getaffinity_np (&attr, set_size, set), 0);
> +  for (int i = 0; i < cpu_count; ++i)
> +    TEST_COMPARE (!!CPU_ISSET (i, set), i == 1 || i == 3);
> +  /* Due to bug 25999, the following caused a double-free abort.  */
> +  xpthread_attr_destroy (&attr);

OK.

> +
> +  CPU_FREE (set);
> +
> +  return 0;
> +}
> +
> +#include <support/test-driver.c>
> 


-- 
Cheers,
Carlos.


  reply	other threads:[~2020-05-20 14:43 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-19 10:43 [PATCH 00/19] Signal mask for timer helper thread Florian Weimer
2020-05-19 10:44 ` [PATCH 01/19] manual: Add missing section and node for clockid_t wait functions Florian Weimer
2020-05-20 13:12   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 02/19] nptl: Replace some stubs with the Linux implementation Florian Weimer
2020-05-20 13:27   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 03/19] nptl: Move pthread_attr_setaffinity_np into libc Florian Weimer
2020-05-20 13:31   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 04/19] nptl: Move pthread_getaffinity_np " Florian Weimer
2020-05-20 13:52   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 05/19] nptl: Move pthread_gettattr_np " Florian Weimer
2020-05-20 13:57   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 06/19] nptl: Make __pthread_attr_init, __pthread_attr_destroy available internally Florian Weimer
2020-05-20 13:59   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 07/19] nptl: Add __pthread_attr_copy for copying pthread_attr_t objects Florian Weimer
2020-05-20 14:10   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 08/19] nptl: Use __pthread_attr_copy in pthread_getattr_default_np (bug 25999) Florian Weimer
2020-05-20 14:42   ` Carlos O'Donell [this message]
2020-05-19 10:44 ` [PATCH 09/19] nptl: Use __pthread_attr_copy in pthread_setattr_default_np Florian Weimer
2020-05-20 14:48   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 10/19] <libc-symbols.h>: Add libpthread hidden alias support Florian Weimer
2020-05-20 14:51   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 11/19] nptl: Add internal alias __pthread_getattr_default_np Florian Weimer
2020-06-02  3:28   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 12/19] nptl: Use __pthread_getattr_default_np in pthread_create Florian Weimer
2020-06-02  3:34   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 13/19] nptl: Use __pthread_attr_setaffinity_np in pthread_getattr_np Florian Weimer
2020-06-02  3:36   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 14/19] nptl: Change type of __default_pthread_attr Florian Weimer
2020-06-02  3:39   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 15/19] nptl: Destroy the default thread attribute as part of freeres Florian Weimer
2020-06-02  3:41   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 16/19] nptl: Make pthread_attr_t dynamically extensible Florian Weimer
2020-06-02  3:47   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 17/19] nptl: Add pthread_attr_setsigmask_np, pthread_attr_getsigmask_np Florian Weimer
2020-06-02  4:01   ` Carlos O'Donell
2020-05-19 10:45 ` [PATCH 18/19] manual: " Florian Weimer
2020-05-20  7:39   ` Michael Kerrisk
2020-06-03  9:26     ` Florian Weimer
2020-06-02  4:05   ` Carlos O'Donell
2020-05-19 10:45 ` [PATCH 19/19] Linux: Use __pthread_attr_setsigmask_internal for timer helper thread Florian Weimer
2020-06-02  4:07   ` Carlos O'Donell
2020-05-20 13:11 ` [PATCH 00/19] Signal mask " Carlos O'Donell

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=986cb140-5a63-51ac-95a4-526a20d0e959@redhat.com \
    --to=carlos@redhat.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.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).