GCC Code Coverage Report
Directory: . Exec Total Coverage
File: test/unit/memory.h Lines: 12 12 100.0 %
Date: 2021-03-22 Branches: 6 28 21.4 %

Line Exec Source
1
/*********************                                                        */
2
/*! \file memory.h
3
 ** \verbatim
4
 ** Top contributors (to current version):
5
 **   Tim King, Morgan Deters, Aina Niemetz
6
 ** This file is part of the CVC4 project.
7
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 ** in the top-level source directory and their institutional affiliations.
9
 ** All rights reserved.  See the file COPYING in the top-level source
10
 ** directory for licensing information.\endverbatim
11
 **
12
 ** \brief Utility class to help testing out-of-memory conditions.
13
 **
14
 ** Utility class to help testing out-of-memory conditions.
15
 **
16
 ** Use it like this (for example):
17
 **
18
 **   CVC4::test::WithLimitedMemory wlm(amount);
19
 **   TS_ASSERT_THROWS( foo(), bad_alloc );
20
 **
21
 ** The WithLimitedMemory destructor will re-establish the previous limit.
22
 **
23
 ** This class does not exist in CVC4_MEMORY_LIMITING_DISABLED is defined.
24
 ** This can be disabled for a variety of reasons.
25
 ** If this is disabled, there will be a function:
26
 **   void WarnWithLimitedMemoryDisabledReason()
27
 ** uses TS_WARN to alert users that these tests are disabled.
28
 **/
29
30
#include "test.h"
31
32
#ifndef __CVC4__TEST__UNIT__MEMORY_H
33
#define __CVC4__TEST__UNIT__MEMORY_H
34
35
#include <string>
36
#include <sys/resource.h>
37
#include <sys/time.h>
38
39
#include "base/check.h"
40
#include "base/configuration_private.h"
41
42
// Conditionally define CVC4_MEMORY_LIMITING_DISABLED.
43
#ifdef __APPLE__
44
#  define CVC4_MEMORY_LIMITING_DISABLED 1
45
#  define CVC4_MEMORY_LIMITING_DISABLED_REASON "setrlimit() is broken on Mac."
46
#else /* __APPLE__ */
47
48
// Tests cannot expect bad_alloc to be thrown due to limit memory using
49
// setrlimit when ASAN is enable. ASAN instead aborts on mmap failures.
50
#  if IS_ASAN_BUILD
51
#    define CVC4_MEMORY_LIMITING_DISABLED 1
52
#    define CVC4_MEMORY_LIMITING_DISABLED_REASON "ASAN's mmap failures abort."
53
#  endif
54
#endif
55
56
namespace CVC4 {
57
namespace test {
58
59
#ifndef CVC4_MEMORY_LIMITING_DISABLED
60
class WithLimitedMemory {
61
 public:
62
  WithLimitedMemory() { remember(); }
63
64
2
  WithLimitedMemory(rlim_t amount) {
65
2
    remember();
66
2
    set(amount);
67
2
  }
68
69
2
  ~WithLimitedMemory() { set(d_prevAmount); }
70
71
4
  void set(rlim_t amount) {
72
    struct rlimit rlim;
73
4
    rlim.rlim_cur = amount;
74
4
    rlim.rlim_max = RLIM_INFINITY;
75
4
    ASSERT_EQ(setrlimit(RLIMIT_AS, &rlim), 0);
76
  }
77
78
 private:
79
2
  void remember() {
80
    struct rlimit rlim;
81
2
    ASSERT_EQ(getrlimit(RLIMIT_AS, &rlim), 0);
82
2
    d_prevAmount = rlim.rlim_cur;
83
  }
84
85
  rlim_t d_prevAmount;
86
}; /* class WithLimitedMemory */
87
#endif
88
89
} /* CVC4::test namespace */
90
} /* CVC4 namespace */
91
92
93
// Remove CVC4_MEMORY_LIMITING_DISABLED_REASON if it is defined.
94
#ifdef CVC4_MEMORY_LIMITING_DISABLED_REASON
95
#undef CVC4_MEMORY_LIMITING_DISABLED_REASON
96
#endif /* CVC4_MEMORY_LIMITING_DISABLED_REASON */
97
98
#endif /* __CVC4__TEST__MEMORY_H */