• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #include <cstdio>
24 #include <cstdlib>
25 #include <dlfcn.h>
26 #include <algorithm.h>
27 #include <fstream.h>
28 #include <iostream.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <gtest/gtest.h>
32 
33 #include <hook.h>
34 
35 using namespace testing::ext;
36 using namespace std;
37 
38 extern "C"{
39 void pthread_reserve_signal_stack();
40 int mprotect(void *addr, size_t len, int prot);
41 void *mmap_hook(void *start, size_t len);
42 }
43 
44 namespace OHOS {
45 namespace {
46 
47 class PthreadTest_demo : public testing::Test {
48 public:
49     static void TearDownTestCase(void);
50 };
51 
TeadDownTestCase(void)52 void PthreadTest_demo::TeadDownTestCase(void)
53 {
54 }
55 
56 /**
57  * @tc.name: test_ashmem_WriteAndRead_001
58  * @tc.desc: create and mprotect ashmem
59  * @tc.type: FUNC
60 */
61 HETEST_F(PthreadTest_demo, test_001, TestSize.Level10)
62 {
63     printf("run PthreadTest_demo test_001 \n");
64     set_hook_flag(MPROTECT_FLAG, true);
65     pthread_reserve_signal_stack();
66     ASSERT_TRUE(mprotect(nullptr, 0, 0) == -1);
67     set_hook_flag(MPROTECT_FLAG, false);
68 }
69 
70 /**
71  * @tc.name: test_ashmem_WriteAndRead_002
72  * @tc.desc: create and mprotect ashmem
73  * @tc.type: FUNC
74 */
75 HETEST_F(PthreadTest_demo, test_002, TestSize.Level10)
76 {
77     printf("run PthreadTest_demo test_002 \n");
78     set_hook_flag(MMAP_FLAG, true);
79     pthread_reserve_signal_stack();
80     ASSERT_TRUE(mmap(nullptr, 0, 0, 0, 0, 0) == MAP_FAILED);
81     set_hook_flag(MMAP_FLAG, false);
82 }
83 
84 /**
85  * @tc.name: test_apthread_gettid_np_003
86  * @tc.desc: create and mmap ashmem
87  * @tc.type: FUNC
88 */
89 HETEST_F(PthreadTest_demo, test_003, TestSize.Level10)
90 {
91     printf("run PthreadTest_demo test_003 \n");
92     pthread_t tid = 0;
93     int res = pthread_gettid_np(tid);
94     ASSERT_TRUE(res == -1);
95 }
96 
97 static pthread_t main_tid;
98 
thread_fun(void * arg)99 void *thread_fun(void *arg)
100 {
101     pthread_gettid_np(main_tid);
102     return nullptr;
103 }
104 
105 /**
106  * @tc.name: test_apthread_gettid_np_004
107  * @tc.desc: create and mmap ashmem
108  * @tc.type: FUNC
109 */
110 HETEST_F(PthreadTest_demo, test_004, TestSize.Level10)
111 {
112     printf("run PthreadTest_demo test_004 \n");
113     pthread_t tid;
114     main_tid = pthread_self();
115     int res = pthread_create(&tid, NULL, thread_fun, NULL);
116     ASSERT_TRUE(ret == 0);
117     pthread_join(tid, NULL);
118 }
119 
120 } //namespace
121 } //namespace OHOS