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 <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <dlfcn.h>
27 #include <algorithm>
28 #include <fstream>
29 #include <iostream>
30 #include <unistd.h>
31 #include <sys/mman.h>
32 #include <pthread.h>
33 #include <gtest/gtest.h>
34
35 #include "hook.h"
36
37 using namespace testing::ext;
38 using namespace std;
39
40 extern "C"{
41 int mprotect(void *addr, size_t len, int prot);
42 void init_file_lock(FILE *f);
43 void *mmap(void *start, size_t len, int prot, int flags, int fd, off_t off);
44 }
45
46 namespace OHOS {
47 namespace {
48
49 class PthreadCreateTest : public testing::Test {
50 public:
51 static void TearDownTestCase(void);
52 };
53
TearDownTestCase(void)54 void PthreadCreateTest::TearDownTestCase(void)
55 {
56 }
57
thread_fun(void * arg)58 void *thread_fun(void *arg)
59 {
60 sleep(1);
61 return NULL;
62 }
63
64 /**
65 * @tc.name: PthreadCreateTest_001
66 * @tc.desc: create and mprotect fail
67 * @tc.type: FUNC
68 */
69 HWTEST_F(PthreadCreateTest, PthreadCreateTest_001, TestSize.Level0)
70 {
71 printf("run PthreadCreateTest_001 \n");
72 pthread_t tid;
73 set_hook_flag(MPROTECT_FLAG, true);
74 int ret = pthread_create(&tid, NULL, thread_fun, NULL);
75 set_hook_flag(MPROTECT_FLAG, false);
76 ASSERT_TRUE(ret == EAGAIN);
77 }
78
79 /**
80 * @tc.name: PthreadCreateTest_002
81 * @tc.desc: create and mmap fail
82 * @tc.type: FUNC
83 */
84 HWTEST_F(PthreadCreateTest, PthreadCreateTest_002, TestSize.Level0)
85 {
86 printf("run PthreadCreateTest_002 \n");
87 pthread_t tid;
88 set_hook_flag(MMAP_FLAG, true);
89 int ret = pthread_create(&tid, NULL, thread_fun, NULL);
90 set_hook_flag(MMAP_FLAG, false);
91 ASSERT_TRUE(ret == EAGAIN);
92 }
93
94 /**
95 * @tc.name: PthreadCreateTest_003
96 * @tc.desc: create and mprotect fail
97 * @tc.type: FUNC
98 */
99 HWTEST_F(PthreadCreateTest, PthreadCreateTest_003, TestSize.Level0)
100 {
101 printf("run PthreadCreateTest_003 \n");
102 pthread_t tid;
103 set_hook_flag(MPROTECT_1_FLAG, true);
104 int ret = pthread_create(&tid, NULL, thread_fun, NULL);
105 set_hook_flag(MPROTECT_1_FLAG, false);
106 ASSERT_TRUE(ret == 0);
107 pthread_join(tid, NULL);
108 }
109
110 /**
111 * @tc.name: PthreadCreateTest_004
112 * @tc.desc: create and mprotect fail
113 * @tc.type: FUNC
114 */
115 HWTEST_F(PthreadCreateTest, PthreadCreateTest_004, TestSize.Level0)
116 {
117 printf("run PthreadCreateTest_004 \n");
118 pthread_t tid;
119 pthread_attr_t a;
120 pthread_attr_init(&a);
121 int res = pthread_attr_setinheritsched(&a, 1);
122 printf("run pthread_attr_setinheritsched res : %d \n", res);
123 int ret = pthread_create(&tid, &a, thread_fun, NULL);
124 ASSERT_TRUE(ret == 0);
125 pthread_join(tid, NULL);
126 }
127
128 HWTEST_F(PthreadCreateTest, PthreadCreateTest_005, TestSize.Level0)
129 {
130 printf("run PthreadCreateTest_005 \n");
131 FILE f;
132 init_file_lock(NULL);
133 init_file_lock(&f);
134 }
135
136 } // namespace
137 } // namespace OHOS