summaryrefslogtreecommitdiff
path: root/tests/dwfl-bug-fd-leak.c
blob: 170a61a7fe8b753f069586ea2520cd40bca2d2c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Test program for libdwfl file decriptors leakage.
   Copyright (C) 2007, 2008 Red Hat, Inc.
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <locale.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <error.h>
#include <unistd.h>
#include <dwarf.h>
#include <sys/resource.h>
#include ELFUTILS_HEADER(dwfl)


static Dwfl *
elfutils_open (pid_t pid, Dwarf_Addr address)
{
  static char *debuginfo_path;
  static const Dwfl_Callbacks proc_callbacks =
    {
      .find_debuginfo = dwfl_standard_find_debuginfo,
      .debuginfo_path = &debuginfo_path,

      .find_elf = dwfl_linux_proc_find_elf,
    };
  Dwfl *dwfl = dwfl_begin (&proc_callbacks);
  if (dwfl == NULL)
    error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));

  int result = dwfl_linux_proc_report (dwfl, pid);
  if (result < 0)
    error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
  else if (result > 0)
    error (2, result, "dwfl_linux_proc_report");

  if (dwfl_report_end (dwfl, NULL, NULL) != 0)
    error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));

  Dwarf_Addr bias;
  Dwarf *dbg = dwfl_addrdwarf (dwfl, address, &bias);
  if (dbg != NULL)
    {
      Elf *elf = dwarf_getelf (dbg);
      if (elf == NULL)
	error (2, 0, "dwarf_getelf: %s", dwarf_errmsg (-1));
    }
  else
    {
      Elf *elf = dwfl_module_getelf (dwfl_addrmodule (dwfl, address), &bias);
      if (elf == NULL)
	error (2, 0, "dwfl_module_getelf: %s", dwfl_errmsg (-1));
    }

  return dwfl;
}

static void
elfutils_close (Dwfl *dwfl)
{
  dwfl_end (dwfl);
}

int
main (void)
{
  /* We use no threads here which can interfere with handling a stream.  */
  (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);

  /* Set locale.  */
  (void) setlocale (LC_ALL, "");

  struct rlimit fd_limit = { .rlim_cur = 32, .rlim_max = 32 };
  if (setrlimit (RLIMIT_NOFILE, &fd_limit) < 0)
    error (2, errno, "setrlimit");

  for (int i = 0; i < 5000; ++i)
    {
      Dwfl *dwfl = elfutils_open (getpid (), (Dwarf_Addr) (uintptr_t) &main);
      elfutils_close (dwfl);
    }

  return 0;
}