summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/util/testsuite_hooks.cc
blob: 6d77d57672febc7653d50f4429caeffd20b9e631 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// -*- C++ -*-

// Utility subroutines for the C++ library testsuite. 
//
// Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
// Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library 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, or (at your option)
// any later version.
//
// This library 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 library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.
//

#include <testsuite_hooks.h>

#ifdef _GLIBCXX_RES_LIMITS
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#endif

#include <list>
#include <string>
#include <stdexcept>
#include <cstddef>
#include <clocale>
#include <cstdlib>
#include <locale>
#include <cxxabi.h>

// If we have <sys/types.h>, <sys/ipc.h>, and <sys/sem.h>, then assume
// that System V semaphores are available.
#if defined(_GLIBCXX_HAVE_SYS_TYPES_H)		\
    && defined(_GLIBCXX_HAVE_SYS_IPC_H)		\
    && defined(_GLIBCXX_HAVE_SYS_SEM_H)
#define _GLIBCXX_SYSV_SEM
#endif

#ifdef _GLIBCXX_SYSV_SEM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#endif

namespace __gnu_test
{
#ifdef _GLIBCXX_RES_LIMITS
  void 
  set_memory_limits(float size)
  {
    struct rlimit r;
    // Cater to the absence of rlim_t.
    __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size * 1048576);

    // Heap size, seems to be common.
#if _GLIBCXX_HAVE_LIMIT_DATA
    getrlimit(RLIMIT_DATA, &r);
    r.rlim_cur = limit;
    setrlimit(RLIMIT_DATA, &r);
#endif

    // Resident set size.
#if _GLIBCXX_HAVE_LIMIT_RSS
    getrlimit(RLIMIT_RSS, &r);
    r.rlim_cur = limit;
    setrlimit(RLIMIT_RSS, &r);
#endif

    // Mapped memory (brk + mmap).
#if _GLIBCXX_HAVE_LIMIT_VMEM
    getrlimit(RLIMIT_VMEM, &r);
    r.rlim_cur = limit;
    setrlimit(RLIMIT_VMEM, &r);
#endif

    // Virtual memory.  On x86_64-linux, the default is -z
    // max-page-size=0x200000 which means up to 2MB of address space
    // are accounted for PROT_NONE mappings between text and data
    // segments of each shared library.  There are 4 shared libs
    // involved in addition to the dynamic linker, maybe 5 if libgomp
    // is being used as well.  Use at least 20MB address space limit.
#if defined(__x86_64__) && defined(__linux__)
    if (limit < 20971520)
      limit = 20971520;
#endif

    // On HP-UX 11.23, a trivial C++ program that sets RLIMIT_AS to
    // anything less than 128MB cannot "malloc" even 1K of memory.
    // Therefore, we skip RLIMIT_AS on HP-UX.
#if _GLIBCXX_HAVE_LIMIT_AS && !defined(__hpux__)
    getrlimit(RLIMIT_AS, &r);
    r.rlim_cur = limit;
    setrlimit(RLIMIT_AS, &r);
#endif
  }

#else
  void
  set_memory_limits(float) { }
#endif 

#ifdef _GLIBCXX_RES_LIMITS
  void 
  set_file_limit(unsigned long size)
  {
#if _GLIBCXX_HAVE_LIMIT_FSIZE
    struct rlimit r;
    // Cater to the absence of rlim_t.
    __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size);

    getrlimit(RLIMIT_FSIZE, &r);
    r.rlim_cur = limit;
    setrlimit(RLIMIT_FSIZE, &r);
#endif
  }

#else
  void
  set_file_limit(unsigned long) { }
#endif 

  void 
  verify_demangle(const char* mangled, const char* wanted)
  {
    int status = 0;
    const char* s = abi::__cxa_demangle(mangled, 0, 0, &status);
    if (!s)
      {
	switch (status)
	  {
	  case 0:
	    s = "error code = 0: success";
	    break;
	  case -1:
	    s = "error code = -1: memory allocation failure";
	    break;
	  case -2:
	    s = "error code = -2: invalid mangled name";
	    break;
	  case -3:
	    s = "error code = -3: invalid arguments";
	    break;
	  default:
	    s = "error code unknown - who knows what happened";
	  }
      }

    std::string w(wanted);
    if (w != s)
      std::__throw_runtime_error(s);
  }

  void 
  run_tests_wrapped_locale(const char* name, const func_callback& l)
  {
    using namespace std;
    
    // Set the global locale. 
    locale loc_name = locale(name);
    locale orig = locale::global(loc_name);

    const char* res = setlocale(LC_ALL, name);
    if (res != NULL)
      {
	string preLC_ALL = res;
	const func_callback::test_type* tests = l.tests();
	for (int i = 0; i < l.size(); ++i)
	  (*tests[i])();
	string postLC_ALL= setlocale(LC_ALL, NULL);
	VERIFY( preLC_ALL == postLC_ALL );
      }
    else
      {
	string s("LC_ALL for ");
	s += name;
	__throw_runtime_error(s.c_str());
      }
  }
  
  void 
  run_tests_wrapped_env(const char* name, const char* env,
			const func_callback& l)
  {
    using namespace std;
    
#ifdef _GLIBCXX_HAVE_SETENV 
    // Set the global locale. 
    locale loc_name = locale(name);
    locale orig = locale::global(loc_name);

    // Set environment variable env to value in name. 
    const char* oldENV = getenv(env);
    if (!setenv(env, name, 1))
      {
	const func_callback::test_type* tests = l.tests();
	for (int i = 0; i < l.size(); ++i)
	  (*tests[i])();
	setenv(env, oldENV ? oldENV : "", 1);
      }
    else
      {
	string s(env);
	s += string(" to ");
	s += string(name);
	__throw_runtime_error(s.c_str());
      }
#endif
  }

  object_counter::size_type  object_counter::count = 0;
  unsigned int copy_constructor::count_ = 0;
  unsigned int copy_constructor::throw_on_ = 0;
  unsigned int assignment_operator::count_ = 0;
  unsigned int assignment_operator::throw_on_ = 0;
  unsigned int destructor::_M_count = 0;
  int copy_tracker::next_id_ = 0;

#ifdef _GLIBCXX_SYSV_SEM
  // This union is not declared in system headers.  Instead, it must
  // be defined by user programs.
  union semun 
  {
    int val;
    struct semid_ds *buf;
    unsigned short *array;
  };
#endif

  semaphore::semaphore() 
  {
#ifdef _GLIBCXX_SYSV_SEM
    // Remeber the PID for the process that created the semaphore set
    // so that only one process will destroy the set.
    pid_ = getpid();

    // GLIBC does not define SEM_R and SEM_A.
#ifndef SEM_R
#define SEM_R 0400
#endif
    
#ifndef SEM_A
#define SEM_A 0200
#endif

    // Get a semaphore set with one semaphore.
    sem_set_ = semget(IPC_PRIVATE, 1, SEM_R | SEM_A);
    if (sem_set_ == -1)
      std::__throw_runtime_error("could not obtain semaphore set");

    // Initialize the semaphore.
    union semun val;
    val.val = 0;
    if (semctl(sem_set_, 0, SETVAL, val) == -1)
      std::__throw_runtime_error("could not initialize semaphore");
#else
    // There are no semaphores on this system.  We have no way to mark
    // a test as "unsupported" at runtime, so we just exit, pretending
    // that the test passed.
    exit(0);
#endif
  }

  semaphore::~semaphore() 
  {
#ifdef _GLIBCXX_SYSV_SEM
    union semun val;
    val.val = 0; // Avoid uninitialized variable warning.
    // Destroy the semaphore set only in the process that created it. 
    if (pid_ == getpid())
      semctl(sem_set_, 0, IPC_RMID, val);
#endif
  }

  void
  semaphore::signal() 
  {
#ifdef _GLIBCXX_SYSV_SEM
    struct sembuf op[1] = 
      {
	{ 0, 1, 0 }
      };
    if (semop(sem_set_, op, 1) == -1)
      std::__throw_runtime_error("could not signal semaphore");
#endif
  }

  void
  semaphore::wait() 
  {
#ifdef _GLIBCXX_SYSV_SEM
    struct sembuf op[1] = 
      {
	{ 0, -1, SEM_UNDO }
      };
    if (semop(sem_set_, op, 1) == -1)
      std::__throw_runtime_error("could not wait for semaphore");
#endif    
  }

  // For use in 22_locale/time_get and time_put.
  std::tm
  test_tm(int sec, int min, int hour, int mday, int mon,
	  int year, int wday, int yday, int isdst)
  {
    static std::tm tmp;
    tmp.tm_sec = sec;
    tmp.tm_min = min;
    tmp.tm_hour = hour;
    tmp.tm_mday = mday;
    tmp.tm_mon = mon;
    tmp.tm_year = year;
    tmp.tm_wday = wday;
    tmp.tm_yday = yday;
    tmp.tm_isdst = isdst;
    return tmp;
  }
} // namespace __gnu_test