xref: /aosp_15_r20/external/elfutils/tests/dwfl-bug-fd-leak.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker /* Test program for libdwfl file descriptors leakage.
2*7304104dSAndroid Build Coastguard Worker    Copyright (C) 2007, 2008 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker    This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker 
5*7304104dSAndroid Build Coastguard Worker    This file is free software; you can redistribute it and/or modify
6*7304104dSAndroid Build Coastguard Worker    it under the terms of the GNU General Public License as published by
7*7304104dSAndroid Build Coastguard Worker    the Free Software Foundation; either version 3 of the License, or
8*7304104dSAndroid Build Coastguard Worker    (at your option) any later version.
9*7304104dSAndroid Build Coastguard Worker 
10*7304104dSAndroid Build Coastguard Worker    elfutils is distributed in the hope that it will be useful, but
11*7304104dSAndroid Build Coastguard Worker    WITHOUT ANY WARRANTY; without even the implied warranty of
12*7304104dSAndroid Build Coastguard Worker    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*7304104dSAndroid Build Coastguard Worker    GNU General Public License for more details.
14*7304104dSAndroid Build Coastguard Worker 
15*7304104dSAndroid Build Coastguard Worker    You should have received a copy of the GNU General Public License
16*7304104dSAndroid Build Coastguard Worker    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17*7304104dSAndroid Build Coastguard Worker 
18*7304104dSAndroid Build Coastguard Worker #include <config.h>
19*7304104dSAndroid Build Coastguard Worker #include <assert.h>
20*7304104dSAndroid Build Coastguard Worker #include <inttypes.h>
21*7304104dSAndroid Build Coastguard Worker #include <stdio.h>
22*7304104dSAndroid Build Coastguard Worker #include <stdio_ext.h>
23*7304104dSAndroid Build Coastguard Worker #include <locale.h>
24*7304104dSAndroid Build Coastguard Worker #include <dirent.h>
25*7304104dSAndroid Build Coastguard Worker #include <stdlib.h>
26*7304104dSAndroid Build Coastguard Worker #include <errno.h>
27*7304104dSAndroid Build Coastguard Worker #include <unistd.h>
28*7304104dSAndroid Build Coastguard Worker #include <dwarf.h>
29*7304104dSAndroid Build Coastguard Worker #include "system.h"
30*7304104dSAndroid Build Coastguard Worker 
31*7304104dSAndroid Build Coastguard Worker #ifndef __linux__
32*7304104dSAndroid Build Coastguard Worker int
main(void)33*7304104dSAndroid Build Coastguard Worker main (void)
34*7304104dSAndroid Build Coastguard Worker {
35*7304104dSAndroid Build Coastguard Worker   return 77; /* dwfl_linux_proc_report is linux specific.  */
36*7304104dSAndroid Build Coastguard Worker }
37*7304104dSAndroid Build Coastguard Worker #else
38*7304104dSAndroid Build Coastguard Worker 
39*7304104dSAndroid Build Coastguard Worker #include <sys/resource.h>
40*7304104dSAndroid Build Coastguard Worker #include ELFUTILS_HEADER(dwfl)
41*7304104dSAndroid Build Coastguard Worker 
42*7304104dSAndroid Build Coastguard Worker 
43*7304104dSAndroid Build Coastguard Worker static Dwfl *
elfutils_open(pid_t pid,Dwarf_Addr address)44*7304104dSAndroid Build Coastguard Worker elfutils_open (pid_t pid, Dwarf_Addr address)
45*7304104dSAndroid Build Coastguard Worker {
46*7304104dSAndroid Build Coastguard Worker   static char *debuginfo_path;
47*7304104dSAndroid Build Coastguard Worker   static const Dwfl_Callbacks proc_callbacks =
48*7304104dSAndroid Build Coastguard Worker     {
49*7304104dSAndroid Build Coastguard Worker       .find_debuginfo = dwfl_standard_find_debuginfo,
50*7304104dSAndroid Build Coastguard Worker       .debuginfo_path = &debuginfo_path,
51*7304104dSAndroid Build Coastguard Worker 
52*7304104dSAndroid Build Coastguard Worker       .find_elf = dwfl_linux_proc_find_elf,
53*7304104dSAndroid Build Coastguard Worker     };
54*7304104dSAndroid Build Coastguard Worker   Dwfl *dwfl = dwfl_begin (&proc_callbacks);
55*7304104dSAndroid Build Coastguard Worker   if (dwfl == NULL)
56*7304104dSAndroid Build Coastguard Worker     error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
57*7304104dSAndroid Build Coastguard Worker 
58*7304104dSAndroid Build Coastguard Worker   int result = dwfl_linux_proc_report (dwfl, pid);
59*7304104dSAndroid Build Coastguard Worker   if (result < 0)
60*7304104dSAndroid Build Coastguard Worker     error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
61*7304104dSAndroid Build Coastguard Worker   else if (result > 0)
62*7304104dSAndroid Build Coastguard Worker     error (2, result, "dwfl_linux_proc_report");
63*7304104dSAndroid Build Coastguard Worker 
64*7304104dSAndroid Build Coastguard Worker   if (dwfl_report_end (dwfl, NULL, NULL) != 0)
65*7304104dSAndroid Build Coastguard Worker     error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
66*7304104dSAndroid Build Coastguard Worker 
67*7304104dSAndroid Build Coastguard Worker   Dwarf_Addr bias;
68*7304104dSAndroid Build Coastguard Worker   Dwarf *dbg = dwfl_addrdwarf (dwfl, address, &bias);
69*7304104dSAndroid Build Coastguard Worker   if (dbg != NULL)
70*7304104dSAndroid Build Coastguard Worker     {
71*7304104dSAndroid Build Coastguard Worker       Elf *elf = dwarf_getelf (dbg);
72*7304104dSAndroid Build Coastguard Worker       if (elf == NULL)
73*7304104dSAndroid Build Coastguard Worker 	error (2, 0, "dwarf_getelf: %s", dwarf_errmsg (-1));
74*7304104dSAndroid Build Coastguard Worker     }
75*7304104dSAndroid Build Coastguard Worker   else
76*7304104dSAndroid Build Coastguard Worker     {
77*7304104dSAndroid Build Coastguard Worker       Dwfl_Module *module = dwfl_addrmodule (dwfl, address);
78*7304104dSAndroid Build Coastguard Worker       if (module == NULL)
79*7304104dSAndroid Build Coastguard Worker 	error (2, 0, "dwfl_addrmodule: no module available for 0x%" PRIx64 "",
80*7304104dSAndroid Build Coastguard Worker 	       address);
81*7304104dSAndroid Build Coastguard Worker       Elf *elf = dwfl_module_getelf (module, &bias);
82*7304104dSAndroid Build Coastguard Worker       if (elf == NULL)
83*7304104dSAndroid Build Coastguard Worker 	error (2, 0, "dwfl_module_getelf: %s", dwfl_errmsg (-1));
84*7304104dSAndroid Build Coastguard Worker     }
85*7304104dSAndroid Build Coastguard Worker 
86*7304104dSAndroid Build Coastguard Worker   return dwfl;
87*7304104dSAndroid Build Coastguard Worker }
88*7304104dSAndroid Build Coastguard Worker 
89*7304104dSAndroid Build Coastguard Worker static void
elfutils_close(Dwfl * dwfl)90*7304104dSAndroid Build Coastguard Worker elfutils_close (Dwfl *dwfl)
91*7304104dSAndroid Build Coastguard Worker {
92*7304104dSAndroid Build Coastguard Worker   dwfl_end (dwfl);
93*7304104dSAndroid Build Coastguard Worker }
94*7304104dSAndroid Build Coastguard Worker 
95*7304104dSAndroid Build Coastguard Worker int
main(void)96*7304104dSAndroid Build Coastguard Worker main (void)
97*7304104dSAndroid Build Coastguard Worker {
98*7304104dSAndroid Build Coastguard Worker   /* We use no threads here which can interfere with handling a stream.  */
99*7304104dSAndroid Build Coastguard Worker   (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
100*7304104dSAndroid Build Coastguard Worker 
101*7304104dSAndroid Build Coastguard Worker   /* Set locale.  */
102*7304104dSAndroid Build Coastguard Worker   (void) setlocale (LC_ALL, "");
103*7304104dSAndroid Build Coastguard Worker 
104*7304104dSAndroid Build Coastguard Worker   /* Get both the soft and hard limits first. The soft limit is what
105*7304104dSAndroid Build Coastguard Worker      will be enforced against the process.  The hard limit is the max
106*7304104dSAndroid Build Coastguard Worker      that can be set for the soft limit.  We don't want to lower it
107*7304104dSAndroid Build Coastguard Worker      because we might not be able to (under valgrind).  */
108*7304104dSAndroid Build Coastguard Worker   struct rlimit fd_limit;
109*7304104dSAndroid Build Coastguard Worker   if (getrlimit (RLIMIT_NOFILE, &fd_limit) < 0)
110*7304104dSAndroid Build Coastguard Worker     error (2, errno, "getrlimit");
111*7304104dSAndroid Build Coastguard Worker   fd_limit.rlim_cur = 32;
112*7304104dSAndroid Build Coastguard Worker   if (setrlimit (RLIMIT_NOFILE, &fd_limit) < 0)
113*7304104dSAndroid Build Coastguard Worker     error (2, errno, "setrlimit");
114*7304104dSAndroid Build Coastguard Worker 
115*7304104dSAndroid Build Coastguard Worker   for (int i = 0; i < 5000; ++i)
116*7304104dSAndroid Build Coastguard Worker     {
117*7304104dSAndroid Build Coastguard Worker       Dwfl *dwfl = elfutils_open (getpid (), (Dwarf_Addr) (uintptr_t) &main);
118*7304104dSAndroid Build Coastguard Worker       elfutils_close (dwfl);
119*7304104dSAndroid Build Coastguard Worker     }
120*7304104dSAndroid Build Coastguard Worker 
121*7304104dSAndroid Build Coastguard Worker   return 0;
122*7304104dSAndroid Build Coastguard Worker }
123*7304104dSAndroid Build Coastguard Worker #endif
124