| //===----------------------------------------------------------------------===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| /// | |
| /// \file | |
| /// Unit tests for pthread_key_* and tss_* key validation. | |
| /// | |
| //===----------------------------------------------------------------------===// | |
| #include <pthread.h> | |
| #include <threads.h> | |
| #include "hdr/errno_macros.h" | |
| #include "hdr/limits_macros.h" | |
| #include "src/__support/CPP/scope.h" | |
| #include "src/pthread/pthread_getspecific.h" | |
| #include "src/pthread/pthread_key_create.h" | |
| #include "src/pthread/pthread_key_delete.h" | |
| #include "src/pthread/pthread_setspecific.h" | |
| #include "src/threads/tss_create.h" | |
| #include "src/threads/tss_delete.h" | |
| #include "src/threads/tss_get.h" | |
| #include "src/threads/tss_set.h" | |
| #include "test/UnitTest/ErrnoCheckingTest.h" | |
| #include "test/UnitTest/Test.h" | |
| using LlvmLibcPThreadTSSTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; | |
| TEST_F(LlvmLibcPThreadTSSTest, KeyLifecycleAndUpperLimitCheck) { | |
| int value = 42; | |
| pthread_key_t pkey = 0; | |
| ASSERT_EQ(LIBC_NAMESPACE::pthread_key_create(&pkey, nullptr), 0); | |
| LIBC_NAMESPACE::cpp::scope_exit cleanup_pkey( | |
| [&] { LIBC_NAMESPACE::pthread_key_delete(pkey); }); | |
| EXPECT_EQ(LIBC_NAMESPACE::pthread_setspecific(pkey, &value), 0); | |
| EXPECT_EQ(LIBC_NAMESPACE::pthread_getspecific(pkey), | |
| static_cast<void *>(&value)); | |
| cleanup_pkey.release(); | |
| ASSERT_EQ(LIBC_NAMESPACE::pthread_key_delete(pkey), 0); | |
| tss_t tkey = 0; | |
| ASSERT_EQ(LIBC_NAMESPACE::tss_create(&tkey, nullptr), | |
| static_cast<int>(thrd_success)); | |
| LIBC_NAMESPACE::cpp::scope_exit cleanup_tkey( | |
| [&] { LIBC_NAMESPACE::tss_delete(tkey); }); | |
| EXPECT_EQ(LIBC_NAMESPACE::tss_set(tkey, &value), | |
| static_cast<int>(thrd_success)); | |
| EXPECT_EQ(LIBC_NAMESPACE::tss_get(tkey), static_cast<void *>(&value)); | |
| cleanup_tkey.release(); | |
| LIBC_NAMESPACE::tss_delete(tkey); | |
| // TSS_KEY_COUNT in src/__support/threads/thread.cpp is 1024. | |
| constexpr unsigned int KEY_LIMIT = 1024U; | |
| EXPECT_EQ(LIBC_NAMESPACE::pthread_setspecific(KEY_LIMIT, &value), EINVAL); | |
| EXPECT_EQ(LIBC_NAMESPACE::pthread_getspecific(KEY_LIMIT), nullptr); | |
| EXPECT_EQ(LIBC_NAMESPACE::pthread_key_delete(KEY_LIMIT), EINVAL); | |
| EXPECT_EQ(LIBC_NAMESPACE::tss_set(KEY_LIMIT, &value), | |
| static_cast<int>(thrd_error)); | |
| EXPECT_EQ(LIBC_NAMESPACE::tss_get(KEY_LIMIT), nullptr); | |
| EXPECT_EQ(LIBC_NAMESPACE::pthread_setspecific(UINT_MAX, &value), EINVAL); | |
| EXPECT_EQ(LIBC_NAMESPACE::pthread_getspecific(UINT_MAX), nullptr); | |
| EXPECT_EQ(LIBC_NAMESPACE::pthread_key_delete(UINT_MAX), EINVAL); | |
| EXPECT_EQ(LIBC_NAMESPACE::tss_set(UINT_MAX, &value), | |
| static_cast<int>(thrd_error)); | |
| EXPECT_EQ(LIBC_NAMESPACE::tss_get(UINT_MAX), nullptr); | |
| } |