Coverage Report

Created: 2025-09-04 06:34

/src/hermes/lib/Support/ErrorHandling.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
#include "hermes/Support/ErrorHandling.h"
9
10
#include "llvh/ADT/Twine.h"
11
12
namespace hermes {
13
14
0
const std::error_category &oom_category() {
15
0
  class OOMErrorCategory final : public std::error_category {
16
0
    const char *name() const noexcept override {
17
0
      return "vm_allocate_category";
18
0
    }
19
20
0
    std::string message(int condition) const override {
21
0
      switch (static_cast<OOMError>(condition)) {
22
0
        case OOMError::None:
23
0
          return "No error";
24
0
        case OOMError::MaxHeapReached:
25
0
          return "Max heap size was exceeded";
26
0
        case OOMError::MaxStorageReached:
27
0
          return "Number of storages requested exceeded the limit";
28
0
        case OOMError::Effective:
29
0
          return "Effective OOM";
30
0
        case OOMError::SuperSegmentAlloc:
31
0
          return "Allocation occurred that was larger than a heap segment";
32
0
        case OOMError::CopyableVectorCapacityIntegerOverflow:
33
0
          return "CopyableVector capacity integer overflow";
34
0
        case OOMError::TestVMLimitReached:
35
0
          return "A test set a limit for virtual memory that was exceeded";
36
0
      }
37
0
      return "Unknown";
38
0
    }
39
0
  };
40
41
0
  static OOMErrorCategory category;
42
0
  return category;
43
0
}
44
45
0
std::error_code make_error_code(OOMError err) {
46
0
  return std::error_code(static_cast<int>(err), oom_category());
47
0
}
48
49
0
std::string convert_error_to_message(std::error_code code) {
50
0
  return (llvh::Twine("error_code(value = ") + llvh::Twine(code.value()) +
51
0
          ", category = " + code.category().name() +
52
0
          ", message = " + code.message() + ")")
53
0
      .str();
54
0
}
55
56
0
LLVM_ATTRIBUTE_NORETURN void hermes_fatal(const char *msg) {
57
0
  llvh::report_fatal_error(msg);
58
0
}
59
60
0
LLVM_ATTRIBUTE_NORETURN void hermes_fatal(const std::string &msg) {
61
0
  llvh::report_fatal_error(msg.c_str());
62
0
}
63
64
LLVM_ATTRIBUTE_NORETURN void hermes_fatal(
65
    llvh::StringRef prefix,
66
0
    std::error_code code) {
67
0
  llvh::report_fatal_error(
68
0
      (llvh::Twine(prefix) + ": " + convert_error_to_message(code)).str());
69
0
}
70
71
} // namespace hermes