xref: /aosp_15_r20/external/llvm-libc/test/integration/startup/linux/tls_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Loader test to check if tls size is read correctly ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/errno/libc_errno.h"
10 #include "src/sys/mman/mmap.h"
11 #include "test/IntegrationTest/test.h"
12 
13 #include <sys/mman.h>
14 
15 constexpr int threadLocalDataSize = 101;
16 _Thread_local int a[threadLocalDataSize] = {123};
17 
TEST_MAIN(int argc,char ** argv,char ** envp)18 TEST_MAIN(int argc, char **argv, char **envp) {
19   ASSERT_TRUE(a[0] == 123);
20 
21   for (int i = 1; i < threadLocalDataSize; ++i)
22     a[i] = i;
23   for (int i = 1; i < threadLocalDataSize; ++i)
24     ASSERT_TRUE(a[i] == i);
25 
26   // Call mmap with bad params so that an error value is
27   // set in errno. Since errno is implemented using a thread
28   // local var, this helps us test setting of errno and
29   // reading it back.
30   ASSERT_ERRNO_SUCCESS();
31   void *addr = LIBC_NAMESPACE::mmap(nullptr, 0, PROT_READ,
32                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
33   ASSERT_TRUE(addr == MAP_FAILED);
34   ASSERT_ERRNO_EQ(EINVAL);
35 
36   return 0;
37 }
38