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 "functionalext.h"
24
25 #define MAX_BUF 256
26 const char* g_libPath_1 = "/data/local/tmp/libc-test-lib/libldso_debug_test_lib_6.so";
27 const char* g_libPath_2 = "/data/local/tmp/libc-test-lib/libldso_debug_test_lib_8.so";
28 const char* g_libPath_3 = "/data/local/tmp/libc-test-lib/libldso_debug_test_lib_9.so";
29
check_loaded(char * so)30 int check_loaded(char* so)
31 {
32 int pid = getpid();
33 char path[MAX_BUF] = { 0 };
34 sprintf(path, "/proc/%d/maps", pid);
35 FILE* fp = fopen(path, "r");
36 if (fp == NULL) {
37 return 0;
38 }
39
40 char buffer[MAX_BUF] = { 0 };
41 while (fgets(buffer, MAX_BUF, fp) != NULL) {
42 if (strstr(buffer, so) != NULL) {
43 fclose(fp);
44 return 1;
45 }
46 }
47 fclose(fp);
48 return 0;
49 }
50
51 /**
52 * @tc.name : ldso_memleak_check_0100
53 * @tc.desc : dlopen so file libldso_debug_test_lib_6.so which has another DTNEEDED so file
54 * libldso_debug_test_lib_7.so. but the libldso_debug_test_lib_7.so is not in device,
55 * so the dlopen will fail, and we will check there is no memleak for so libldso_debug_test_lib_6.so
56 * @tc.level : Level 0
57 */
ldso_memleak_check_0100(void)58 void ldso_memleak_check_0100(void)
59 {
60 void* handle = dlopen(g_libPath_1, RTLD_NOW);
61 if (handle != NULL) {
62 t_error("dlopen(name=%s, mode=%d) should fail\n", g_libPath_1, RTLD_NOW);
63 }
64 if (strstr(dlerror(), "libldso_debug_test_lib_7.so") == NULL) {
65 t_error("libldso_debug_test_lib_7.so should be not found, %s\n", dlerror());
66 }
67 if (check_loaded((char*)g_libPath_1)) {
68 t_error("This so file should not exist in maps, %s\n", (char*)g_libPath_1);
69 }
70 }
71
72 /**
73 * @tc.name : ldso_memleak_check_0200
74 * @tc.desc : dlopen so file libldso_debug_test_lib_8.so has DTNEEDED so file libldso_debug_test_lib_9.so.
75 * libldso_debug_test_lib_9.so is dependent on libldso_debug_test_lib_10.so,
76 * but the libldso_debug_test_lib_10.so is not in device, so the dlopen will fail,
77 * and we will check there is no memleak for so libldso_debug_test_lib_8.so
78 * and libldso_debug_test_lib_9.so
79 * @tc.level : Level 0
80 */
ldso_memleak_check_0200(void)81 void ldso_memleak_check_0200(void)
82 {
83 void* handle = dlopen(g_libPath_2, RTLD_NOW);
84 if (handle != NULL) {
85 t_error("dlopen(name=%s, mode=%d) should fail\n", g_libPath_2, RTLD_NOW);
86 }
87 if (check_loaded((char*)g_libPath_2)) {
88 t_error("This so file should not exist in maps, %s\n", (char*)g_libPath_2);
89 }
90 if (check_loaded((char*)g_libPath_3)) {
91 t_error("This so file should not exist in maps, %s\n", (char*)g_libPath_3);
92 }
93 }
94
main(int argc,char * argv[])95 int main(int argc, char* argv[])
96 {
97 ldso_memleak_check_0100();
98 ldso_memleak_check_0200();
99 return t_status;
100 }