blob: 49b0885b1765622492a8ed125a41cc52a6c84aa9 (
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
|
/********************* */
/*! \file memory.h
** \verbatim
** Top contributors (to current version):
** Tim King, Morgan Deters, Andres Noetzli
** This file is part of the CVC4 project.
** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
** in the top-level source directory) and their institutional affiliations.
** All rights reserved. See the file COPYING in the top-level source
** directory for licensing information.\endverbatim
**
** \brief Utility class to help testing out-of-memory conditions.
**
** Utility class to help testing out-of-memory conditions.
**
** Use it like this (for example):
**
** CVC4::test::WithLimitedMemory wlm(amount);
** TS_ASSERT_THROWS( foo(), bad_alloc );
**
** The WithLimitedMemory destructor will re-establish the previous limit.
**
** This class does not exist in CVC4_MEMORY_LIMITING_DISABLED is defined.
** This can be disabled for a variety of reasons.
** If this is disabled, there will be a function:
** void WarnWithLimitedMemoryDisabledReason()
** uses TS_WARN to alert users that these tests are disabled.
**/
#include <cxxtest/TestSuite.h>
#ifndef __CVC4__TEST__MEMORY_H
#define __CVC4__TEST__MEMORY_H
#include <string>
#include <sys/resource.h>
#include <sys/time.h>
#include "base/configuration_private.h"
#include "base/cvc4_assert.h"
// Conditionally define CVC4_MEMORY_LIMITING_DISABLED.
#ifdef __APPLE__
# define CVC4_MEMORY_LIMITING_DISABLED 1
# define CVC4_MEMORY_LIMITING_DISABLED_REASON "setrlimit() is broken on Mac."
#else /* __APPLE__ */
// Tests cannot expect bad_alloc to be thrown due to limit memory using
// setrlimit when ASAN is enable. ASAN instead aborts on mmap failures.
# if IS_ASAN_BUILD
# define CVC4_MEMORY_LIMITING_DISABLED 1
# define CVC4_MEMORY_LIMITING_DISABLED_REASON "ASAN's mmap failures abort."
# endif
#endif
namespace CVC4 {
namespace test {
#ifdef CVC4_MEMORY_LIMITING_DISABLED
inline void WarnWithLimitedMemoryDisabledReason() {
const std::string reason = std::string("WithLimitedMemory is disabled: ") +
std::string(CVC4_MEMORY_LIMITING_DISABLED_REASON);
TS_WARN(reason.c_str());
}
#else /* CVC4_MEMORY_LIMITING_DISABLED */
class WithLimitedMemory {
public:
WithLimitedMemory() { remember(); }
WithLimitedMemory(rlim_t amount) {
remember();
set(amount);
}
~WithLimitedMemory() { set(d_prevAmount); }
void set(rlim_t amount) {
struct rlimit rlim;
rlim.rlim_cur = amount;
rlim.rlim_max = RLIM_INFINITY;
TS_ASSERT_EQUALS(setrlimit(RLIMIT_AS, &rlim), 0);
}
private:
void remember() {
struct rlimit rlim;
TS_ASSERT_EQUALS(getrlimit(RLIMIT_AS, &rlim), 0);
d_prevAmount = rlim.rlim_cur;
}
rlim_t d_prevAmount;
}; /* class WithLimitedMemory */
#endif /* CVC4_MEMORY_LIMITING_DISABLED */
} /* CVC4::test namespace */
} /* CVC4 namespace */
// Remove CVC4_MEMORY_LIMITING_DISABLED_REASON if it is defined.
#ifdef CVC4_MEMORY_LIMITING_DISABLED_REASON
#undef CVC4_MEMORY_LIMITING_DISABLED_REASON
#endif /* CVC4_MEMORY_LIMITING_DISABLED_REASON */
#endif /* __CVC4__TEST__MEMORY_H */
|