1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <dlfcn.h>
16 #include <link.h>
17 #include <pthread.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/ioctl.h>
24 #include <sys/wait.h>
25 #include <sys/prctl.h>
26
27 #include "test.h"
28 typedef void(*notify_call)(uintptr_t dso_base, size_t dso_len, const char *dso_path);
29 #define MAX_BUF 256
30 #define THREAD_NUMBER 3
31 #define HEX_TERM 16
32 #define SET_PROC_TYPE_CMD _IOW('E', 0x18, uint32_t)
33 #define ASSIGN_ENCAPS_CMD _IOW('E', 0x1A, char *)
34
35 const char* g_libPath1 = "/data/local/tmp/libc-test-lib/libdlopen_register_a.so";
36 const char* g_libPath2 = "/data/local/tmp/libc-test-lib/libdlopen_register_b.so";
37 static int test_value1 = 0;
38 static int test_value2 = 0;
39 static int test_value3 = 0;
40 static int test_value4 = 0;
41 static void *register_ptr = NULL;
42 static int so_nums = 0;
43
check_loaded(const char * so,uintptr_t dso_base,size_t dso_len)44 int check_loaded(const char *so, uintptr_t dso_base, size_t dso_len)
45 {
46 int pid = getpid();
47 char path[MAX_BUF] = { 0 };
48 if (sprintf(path, "/proc/%d/maps", pid) < 0) {
49 t_error("Failed in sprintf: %s\n", strerror(errno));
50 }
51 FILE* fp = fopen(path, "r");
52 if (fp == NULL) {
53 return 0;
54 }
55 if (strchr(so, '/')) {
56 so = strrchr(so, '/') + 1;
57 }
58
59 char buffer[MAX_BUF] = { 0 };
60 unsigned long start = 0;
61 unsigned long end = 0;
62 unsigned long base = 0;
63 unsigned long size = 0;
64 int has_check = 0;
65 while (fgets(buffer, MAX_BUF, fp) != NULL) {
66 if (strstr(buffer, so) != NULL) {
67 char *end_ptr;
68 start = strtoul(buffer, &end_ptr, HEX_TERM);
69 end = strtoul(end_ptr + 1, &end_ptr, HEX_TERM);
70 size += end - start;
71 if (base == 0) {
72 base = start;
73 has_check = 1;
74 }
75 } else if (has_check) {
76 break;
77 }
78 }
79 fclose(fp);
80 if (dso_base == base && size == dso_len) {
81 return 1;
82 }
83 return 0;
84 }
85
callback1(uintptr_t dso_base,size_t dso_len,const char * dso_path)86 void callback1(uintptr_t dso_base, size_t dso_len, const char *dso_path)
87 {
88 test_value1++;
89 int ret = check_loaded(dso_path, dso_base, dso_len);
90 if (!ret) {
91 t_error("Failed check in callback1 for so: %s\n", dso_path);
92 }
93 }
94
callback2(uintptr_t dso_base,size_t dso_len,const char * dso_path)95 void callback2(uintptr_t dso_base, size_t dso_len, const char *dso_path)
96 {
97 test_value2++;
98 int ret = check_loaded(dso_path, dso_base, dso_len);
99 if (!ret) {
100 t_error("Failed check in callback2 for so: %s\n", dso_path);
101 }
102 }
103
callback3(uintptr_t dso_base,size_t dso_len,const char * dso_path)104 void callback3(uintptr_t dso_base, size_t dso_len, const char *dso_path)
105 {
106 test_value3++;
107 int ret = check_loaded(dso_path, dso_base, dso_len);
108 if (!ret) {
109 t_error("Failed check in callback3 for so: %s\n", dso_path);
110 }
111 }
112
callback4(uintptr_t dso_base,size_t dso_len,const char * dso_path)113 void callback4(uintptr_t dso_base, size_t dso_len, const char *dso_path)
114 {
115 test_value4++;
116 int ret = check_loaded(dso_path, dso_base, dso_len);
117 if (!ret) {
118 t_error("Failed check in callback4 for so: %s\n", dso_path);
119 }
120 }
121
header_handler(struct dl_phdr_info * info,size_t size,void * data)122 static int header_handler(struct dl_phdr_info *info, size_t size, void *data)
123 {
124 if (strlen(info->dlpi_name) == 0) {
125 return 0;
126 }
127 so_nums++;
128 return 0;
129 }
130
131 static notify_call func_list[THREAD_NUMBER] = {callback1, callback2, callback3};
132
133 struct callback_param {
134 int (*register_func)(notify_call);
135 notify_call callback;
136 };
137
add_callback(void * arg)138 void *add_callback(void *arg)
139 {
140 struct callback_param *info = (struct callback_param *)arg;
141 info->register_func(info->callback);
142 return arg;
143 }
144
add_callback_concurrently(void)145 void add_callback_concurrently(void)
146 {
147 pthread_t *threads = (pthread_t *) malloc(sizeof(pthread_t) * THREAD_NUMBER);
148 if (threads == NULL) {
149 t_error("Failed to allocate memory: %s\n", strerror(errno));
150 return;
151 }
152 register_ptr = dlsym(RTLD_DEFAULT, "register_ldso_func_for_add_dso");
153 if (register_ptr == NULL) {
154 t_error("Failed to find register function in libc.so\n");
155 return;
156 }
157 size_t index = 0;
158 struct callback_param arg[THREAD_NUMBER] = {0};
159 while (index < THREAD_NUMBER) {
160 arg[index].register_func = register_ptr;
161 arg[index].callback = func_list[index];
162 if (pthread_create(&(threads[index]), NULL, add_callback, (void*)(&arg[index]))) {
163 t_error("Failed to create thread: %s\n", strerror(errno));
164 break;
165 }
166 index++;
167 }
168 for (size_t i = 0; i < index; i++) {
169 if (pthread_join(threads[i], NULL)) {
170 t_error("Failed to join thread: %s\n", strerror(errno));
171 }
172 }
173
174 free(threads);
175 }
176
do_iterate_check(void)177 int do_iterate_check(void)
178 {
179 so_nums = 0;
180 if (dl_iterate_phdr(header_handler, NULL)) {
181 t_error("Failed to run dl_iterate_phdr\n");
182 return -1;
183 }
184
185 if (so_nums != test_value1 || so_nums != test_value2 || so_nums != test_value3) {
186 t_error("callback check failed, so_nums: %d, test_value1: %d, test_value2: %d, test_value3: %d\n",
187 so_nums, test_value1, test_value2, test_value3);
188 return -1;
189 }
190
191 if (test_value4 != 0 && so_nums != test_value4) {
192 t_error("callback second check failed, so_nums: %d, test_value4: %d\n",
193 so_nums, test_value4);
194 return -1;
195 }
196 return 0;
197 }
198
dlopen_thread1(void * arg)199 void *dlopen_thread1(void *arg)
200 {
201 void* handle = dlopen(g_libPath1, RTLD_NOW);
202 if (!handle) {
203 t_error("dlopen(name=%s, mode=%d) failed: %s\n", g_libPath1, RTLD_NOW, dlerror());
204 }
205 return arg;
206 }
207
dlopen_thread2(void * arg)208 void *dlopen_thread2(void *arg)
209 {
210 void* handle = dlopen(g_libPath2, RTLD_LAZY);
211 if (!handle) {
212 t_error("dlopen(name=%s, mode=%d) failed: %s\n", g_libPath2, RTLD_LAZY, dlerror());
213 }
214 return arg;
215 }
216
dlopen_concurrently(void)217 void dlopen_concurrently(void)
218 {
219 pthread_t td1, td2, td3;
220 struct callback_param arg = {register_ptr, callback4};
221 pthread_create(&td1, NULL, dlopen_thread1, NULL);
222 pthread_create(&td2, NULL, dlopen_thread2, NULL);
223 pthread_create(&td3, NULL, add_callback, (void *)(&arg));
224 pthread_join(td1, NULL);
225 pthread_join(td2, NULL);
226 pthread_join(td3, NULL);
227 }
228
set_proc_type(void)229 void set_proc_type(void)
230 {
231 int fd = open("/dev/encaps", O_RDWR);
232 if (fd < 0) {
233 t_error("open /dev/encaps failed, errno: %d\n", errno);
234 return;
235 } else {
236 uint32_t type = 4;
237 ioctl(fd, SET_PROC_TYPE_CMD, &type);
238 close(fd);
239 }
240 }
241
assign_encaps(void)242 void assign_encaps(void)
243 {
244 int fd = open("/dev/encaps", O_RDWR);
245 char str[] = "{\"encaps\":{\"ohos.encaps.count\":1, \"ohos.permission.kernel.DISABLE_GOTPLT_RO_PROTECTION\":1}}";
246 if (fd < 0) {
247 t_error("open /dev/encaps failed, errno: %d\n", errno);
248 return;
249 } else {
250 ioctl(fd, ASSIGN_ENCAPS_CMD, str);
251 close(fd);
252 }
253 }
254
main(int argc,char * argv[])255 int main(int argc, char* argv[])
256 {
257 set_proc_type();
258 assign_encaps();
259 add_callback_concurrently();
260 if (do_iterate_check() < 0) {
261 return t_status;
262 }
263 dlopen_concurrently();
264 do_iterate_check();
265 return t_status;
266 }