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

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Tim King, Morgan Deters, Aina Niemetz
4
 *
5
 * This file is part of the cvc5 project.
6
 *
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.
11
 * ****************************************************************************
12
 *
13
 * Utility class to help testing out-of-memory conditions.
14
 *
15
 * Use it like this (for example):
16
 *
17
 *   cvc5::test::WithLimitedMemory wlm(amount);
18
 *   ASSERT_THROW( foo(), bad_alloc );
19
 *
20
 * The WithLimitedMemory destructor will re-establish the previous limit.
21
 *
22
 * This class does not exist in CVC5_MEMORY_LIMITING_DISABLED is defined.
23
 * This can be disabled for a variety of reasons.
24
 */
25
26
#include "test.h"
27
28
#ifndef __CVC5__TEST__UNIT__MEMORY_H
29
#define __CVC5__TEST__UNIT__MEMORY_H
30
31
#include <string>
32
#include <sys/resource.h>
33
#include <sys/time.h>
34
35
#include "base/check.h"
36
#include "base/configuration_private.h"
37
38
// Conditionally define CVC5_MEMORY_LIMITING_DISABLED.
39
#ifdef __APPLE__
40
#define CVC5_MEMORY_LIMITING_DISABLED 1
41
#define CVC5_MEMORY_LIMITING_DISABLED_REASON "setrlimit() is broken on Mac."
42
#else /* __APPLE__ */
43
44
// Tests cannot expect bad_alloc to be thrown due to limit memory using
45
// setrlimit when ASAN is enable. ASAN instead aborts on mmap failures.
46
#  if IS_ASAN_BUILD
47
#define CVC5_MEMORY_LIMITING_DISABLED 1
48
#define CVC5_MEMORY_LIMITING_DISABLED_REASON "ASAN's mmap failures abort."
49
#  endif
50
#endif
51
52
namespace cvc5 {
53
namespace test {
54
55
#ifndef CVC5_MEMORY_LIMITING_DISABLED
56
class WithLimitedMemory {
57
 public:
58
  WithLimitedMemory() { remember(); }
59
60
2
  WithLimitedMemory(rlim_t amount) {
61
2
    remember();
62
2
    set(amount);
63
2
  }
64
65
2
  ~WithLimitedMemory() { set(d_prevAmount); }
66
67
4
  void set(rlim_t amount) {
68
    struct rlimit rlim;
69
4
    rlim.rlim_cur = amount;
70
4
    rlim.rlim_max = RLIM_INFINITY;
71
4
    ASSERT_EQ(setrlimit(RLIMIT_AS, &rlim), 0);
72
  }
73
74
 private:
75
2
  void remember() {
76
    struct rlimit rlim;
77
2
    ASSERT_EQ(getrlimit(RLIMIT_AS, &rlim), 0);
78
2
    d_prevAmount = rlim.rlim_cur;
79
  }
80
81
  rlim_t d_prevAmount;
82
}; /* class WithLimitedMemory */
83
#endif
84
85
}  // namespace test
86
}  // namespace cvc5
87
88
// Remove CVC5_MEMORY_LIMITING_DISABLED_REASON if it is defined.
89
#ifdef CVC5_MEMORY_LIMITING_DISABLED_REASON
90
#undef CVC5_MEMORY_LIMITING_DISABLED_REASON
91
#endif /* CVC5_MEMORY_LIMITING_DISABLED_REASON */
92
93
#endif /* __CVC5__TEST__MEMORY_H */