-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathexpects
More file actions
121 lines (101 loc) · 3.73 KB
/
expects
File metadata and controls
121 lines (101 loc) · 3.73 KB
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
// -*- C++ -*-
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#ifndef INCLUDEOS_EXPECTS_HEADER
#define INCLUDEOS_EXPECTS_HEADER
// LIKELY/UNLIKELY
#include <likely>
#include <cstdlib>
#undef Expects
#undef Ensures
#ifdef INCLUDEOS_SMP_ENABLE
#include <smp>
#endif
#include <os.hpp>
#include <serial>
inline bool __expect_is_recursive() {
#ifndef UNITTESTS
static volatile bool in_failure = false;
if (in_failure) return true;
in_failure = true;
return false;
#else
return false;
#endif
}
inline void __expect_emit_failure(std::string_view msg, std::string_view panic_text) {
#ifndef UNITTESTS
#ifdef INCLUDEOS_SMP_ENABLE
SMP::global_lock();
#endif
std::fprintf(stderr, "%.*s\n", int(msg.size()), msg.data());
fflush(NULL);
#ifdef INCLUDEOS_SMP_ENABLE
SMP::global_unlock();
#endif
os::panic(std::string(panic_text).c_str());
#else // UNITTESTS
(void) panic_text;
// throw here to allow tests to capture the error
#include <stdexcept>
throw std::runtime_error(std::string(msg));
#endif
}
template <class... Args>
inline void __expect_failf(const char *err_prefix, const char * cond, const char *file, int line, const char *func, std::format_string<Args...> fmt, Args&&... args){
if (__expect_is_recursive()) {
kprint("Fatal Expects/Ensures recursion. (libc not initialized?)\n");
kprint("Condition was '");
kprint(cond); kprint("' @ ");
kprint(file); kprint(":");
kprint(func); kprint(" with fmt=");
kprint(fmt.get().data()); kprint("\n");
// os::shutdown();
__arch_poweroff();
}
auto reason_msg = std::format(fmt, std::forward<Args>(args)...);
auto error_msg = std::format("{}:{}:{}: {}: {}", file, line, func, err_prefix, reason_msg);
__expect_emit_failure(error_msg, reason_msg);
}
inline void __expect_failf(const char *err_prefix, const char *cond, const char *file, int line, const char *func){
if (__expect_is_recursive()) {
kprint("Fatal Expects/Ensures recursion (libc not initialized?)\n");
kprint("Condition was '");
kprint(cond); kprint("' @ ");
kprint(file); kprint(":");
kprint(func); kprint("\n");
// os::shutdown();
__arch_poweroff();
}
auto reason_msg = std::format("{}: {}", err_prefix, cond);
auto error_msg = std::format("{}:{}:{}: {}", file, line, func, err_prefix);
__expect_emit_failure(error_msg, reason_msg);
}
#define Expects(cond) ((void)((cond) || (__expect_failf("Expects failed", #cond, __FILE__, __LINE__, __func__),0)))
#define Ensures(cond) ((void)((cond) || (__expect_failf("Ensures failed", #cond, __FILE__, __LINE__, __func__),0)))
#define Expectsf(cond, fmt, ...) ((void)((cond) || (__expect_failf("Expects failed", #cond, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__),0)))
#define Ensuresf(cond, fmt, ...) ((void)((cond) || (__expect_failf("Ensures failed", #cond, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__),0)))
namespace os {
// parameter for noexcept specifier when bypassing noexcept for testing
#if defined(TEST)
constexpr bool hard_noexcept = false;
#else
constexpr bool hard_noexcept = true;
#endif
}
#endif //< INCLUDEOS_EXPECTS_HEADER