GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_errors.h Lines: 24 40 60.0 %
Date: 2021-05-01 04:12:26 Branches: 0 0 - %

Line Branch Exec Source
1
#ifndef SRC_NODE_ERRORS_H_
2
#define SRC_NODE_ERRORS_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "debug_utils-inl.h"
7
#include "env.h"
8
#include "v8.h"
9
10
// Use ostringstream to print exact-width integer types
11
// because the format specifiers are not available on AIX.
12
#include <sstream>
13
14
namespace node {
15
16
enum ErrorHandlingMode { CONTEXTIFY_ERROR, FATAL_ERROR, MODULE_ERROR };
17
void AppendExceptionLine(Environment* env,
18
                         v8::Local<v8::Value> er,
19
                         v8::Local<v8::Message> message,
20
                         enum ErrorHandlingMode mode);
21
22
[[noreturn]] void FatalError(const char* location, const char* message);
23
void OnFatalError(const char* location, const char* message);
24
25
// Helpers to construct errors similar to the ones provided by
26
// lib/internal/errors.js.
27
// Example: with `V(ERR_INVALID_ARG_TYPE, TypeError)`, there will be
28
// `node::ERR_INVALID_ARG_TYPE(isolate, "message")` returning
29
// a `Local<Value>` containing the TypeError with proper code and message
30
31
#define ERRORS_WITH_CODE(V)                                                    \
32
  V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error)                                   \
33
  V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError)                                      \
34
  V(ERR_BUFFER_TOO_LARGE, Error)                                               \
35
  V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError)                                    \
36
  V(ERR_CONSTRUCT_CALL_INVALID, TypeError)                                     \
37
  V(ERR_CRYPTO_INITIALIZATION_FAILED, Error)                                   \
38
  V(ERR_CRYPTO_INVALID_AUTH_TAG, TypeError)                                    \
39
  V(ERR_CRYPTO_INVALID_COUNTER, TypeError)                                     \
40
  V(ERR_CRYPTO_INVALID_CURVE, TypeError)                                       \
41
  V(ERR_CRYPTO_INVALID_DIGEST, TypeError)                                      \
42
  V(ERR_CRYPTO_INVALID_IV, TypeError)                                          \
43
  V(ERR_CRYPTO_INVALID_JWK, TypeError)                                         \
44
  V(ERR_CRYPTO_INVALID_KEYLEN, RangeError)                                     \
45
  V(ERR_CRYPTO_INVALID_KEYPAIR, RangeError)                                    \
46
  V(ERR_CRYPTO_INVALID_KEYTYPE, RangeError)                                    \
47
  V(ERR_CRYPTO_INVALID_MESSAGELEN, RangeError)                                 \
48
  V(ERR_CRYPTO_INVALID_SCRYPT_PARAMS, RangeError)                              \
49
  V(ERR_CRYPTO_INVALID_STATE, Error)                                           \
50
  V(ERR_CRYPTO_INVALID_TAG_LENGTH, RangeError)                                 \
51
  V(ERR_CRYPTO_OPERATION_FAILED, Error)                                        \
52
  V(ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, RangeError)                           \
53
  V(ERR_CRYPTO_UNKNOWN_CIPHER, Error)                                          \
54
  V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error)                                        \
55
  V(ERR_CRYPTO_UNSUPPORTED_OPERATION, Error)                                   \
56
  V(ERR_CRYPTO_JOB_INIT_FAILED, Error)                                         \
57
  V(ERR_DLOPEN_FAILED, Error)                                                  \
58
  V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error)                            \
59
  V(ERR_INVALID_ADDRESS, Error)                                                \
60
  V(ERR_INVALID_ARG_VALUE, TypeError)                                          \
61
  V(ERR_OSSL_EVP_INVALID_DIGEST, Error)                                        \
62
  V(ERR_INVALID_ARG_TYPE, TypeError)                                           \
63
  V(ERR_INVALID_MODULE, Error)                                                 \
64
  V(ERR_INVALID_THIS, TypeError)                                               \
65
  V(ERR_INVALID_TRANSFER_OBJECT, TypeError)                                    \
66
  V(ERR_MEMORY_ALLOCATION_FAILED, Error)                                       \
67
  V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, Error)                             \
68
  V(ERR_MISSING_ARGS, TypeError)                                               \
69
  V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, TypeError)                      \
70
  V(ERR_MISSING_PASSPHRASE, TypeError)                                         \
71
  V(ERR_MISSING_PLATFORM_FOR_WORKER, Error)                                    \
72
  V(ERR_NON_CONTEXT_AWARE_DISABLED, Error)                                     \
73
  V(ERR_OUT_OF_RANGE, RangeError)                                              \
74
  V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error)                                   \
75
  V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error)                                       \
76
  V(ERR_STRING_TOO_LONG, Error)                                                \
77
  V(ERR_TLS_INVALID_PROTOCOL_METHOD, TypeError)                                \
78
  V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, Error)                                \
79
  V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error)                                 \
80
  V(ERR_VM_MODULE_LINK_FAILURE, Error)                                         \
81
  V(ERR_WASI_NOT_STARTED, Error)                                               \
82
  V(ERR_WORKER_INIT_FAILED, Error)                                             \
83
  V(ERR_PROTO_ACCESS, Error)
84
85
#define V(code, type)                                                          \
86
  template <typename... Args>                                                  \
87
  inline v8::Local<v8::Value> code(                                            \
88
      v8::Isolate* isolate, const char* format, Args&&... args) {              \
89
    std::string message = SPrintF(format, std::forward<Args>(args)...);        \
90
    v8::Local<v8::String> js_code = OneByteString(isolate, #code);             \
91
    v8::Local<v8::String> js_msg =                                             \
92
        OneByteString(isolate, message.c_str(), message.length());             \
93
    v8::Local<v8::Object> e = v8::Exception::type(js_msg)                      \
94
                                  ->ToObject(isolate->GetCurrentContext())     \
95
                                  .ToLocalChecked();                           \
96
    e->Set(isolate->GetCurrentContext(),                                       \
97
           OneByteString(isolate, "code"),                                     \
98
           js_code)                                                            \
99
        .Check();                                                              \
100
    return e;                                                                  \
101
  }                                                                            \
102
  template <typename... Args>                                                  \
103
  inline void THROW_##code(                                                    \
104
      v8::Isolate* isolate, const char* format, Args&&... args) {              \
105
    isolate->ThrowException(                                                   \
106
        code(isolate, format, std::forward<Args>(args)...));                   \
107
  }                                                                            \
108
  template <typename... Args>                                                  \
109
  inline void THROW_##code(                                                    \
110
      Environment* env, const char* format, Args&&... args) {                  \
111
    THROW_##code(env->isolate(), format, std::forward<Args>(args)...);         \
112
  }
113
7585
ERRORS_WITH_CODE(V)
114
2
#undef V
115
2
116
4
// Errors with predefined static messages
117
7
118
#define PREDEFINED_ERROR_MESSAGES(V)                                           \
119
  V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE,                                          \
120
    "Buffer is not available for the current Context")                         \
121
  V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called")                \
122
  V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`")      \
123
  V(ERR_CRYPTO_INITIALIZATION_FAILED, "Initialization failed")                 \
124
  V(ERR_CRYPTO_INVALID_AUTH_TAG, "Invalid authentication tag")                 \
125
  V(ERR_CRYPTO_INVALID_COUNTER, "Invalid counter")                             \
126
  V(ERR_CRYPTO_INVALID_CURVE, "Invalid EC curve name")                         \
127
  V(ERR_CRYPTO_INVALID_DIGEST, "Invalid digest")                               \
128
  V(ERR_CRYPTO_INVALID_IV, "Invalid initialization vector")                    \
129
  V(ERR_CRYPTO_INVALID_JWK, "Invalid JWK format")                              \
130
  V(ERR_CRYPTO_INVALID_KEYLEN, "Invalid key length")                           \
131
  V(ERR_CRYPTO_INVALID_KEYPAIR, "Invalid key pair")                            \
132
  V(ERR_CRYPTO_INVALID_KEYTYPE, "Invalid key type")                            \
133
  V(ERR_CRYPTO_INVALID_MESSAGELEN, "Invalid message length")                   \
134
  V(ERR_CRYPTO_INVALID_SCRYPT_PARAMS, "Invalid scrypt params")                 \
135
  V(ERR_CRYPTO_INVALID_STATE, "Invalid state")                                 \
136
  V(ERR_CRYPTO_INVALID_TAG_LENGTH, "Invalid taglength")                        \
137
  V(ERR_CRYPTO_OPERATION_FAILED, "Operation failed")                           \
138
3
  V(ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH,                                       \
139
6
    "Input buffers must have the same byte length")                            \
140
3
  V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher")                               \
141
21
  V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group")                           \
142
8
  V(ERR_CRYPTO_UNSUPPORTED_OPERATION, "Unsupported crypto operation")          \
143
2
  V(ERR_CRYPTO_JOB_INIT_FAILED, "Failed to initialize crypto job config")      \
144
4
  V(ERR_DLOPEN_FAILED, "DLOpen failed")                                        \
145
  V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE,                                   \
146
    "Context not associated with Node.js environment")                         \
147
  V(ERR_INVALID_ADDRESS, "Invalid socket address")                             \
148
  V(ERR_INVALID_MODULE, "No such module")                                      \
149
  V(ERR_INVALID_THIS, "Value of \"this\" is the wrong type")                   \
150
  V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList")       \
151
  V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory")                 \
152
  V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used")                        \
153
  V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE,                                    \
154
    "A message object could not be deserialized successfully in the target "   \
155
    "vm.Context")                                                              \
156
  V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST,                                 \
157
    "Object that needs transfer was found in message but not listed "          \
158
    "in transferList")                                                         \
159
  V(ERR_MISSING_PLATFORM_FOR_WORKER,                                           \
160
    "The V8 platform used by this instance of Node does not support "          \
161
    "creating Workers")                                                        \
162
  V(ERR_NON_CONTEXT_AWARE_DISABLED,                                            \
163
    "Loading non context-aware native modules has been disabled")              \
164
  V(ERR_SCRIPT_EXECUTION_INTERRUPTED,                                          \
165
    "Script execution was interrupted by `SIGINT`")                            \
166
  V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint")    \
167
  V(ERR_WASI_NOT_STARTED, "wasi.start() has not been called")                  \
168
  V(ERR_WORKER_INIT_FAILED, "Worker initialization failure")                   \
169
  V(ERR_PROTO_ACCESS,                                                          \
170
    "Accessing Object.prototype.__proto__ has been "                           \
171
    "disallowed with --disable-proto=throw")
172
173
#define V(code, message)                                                     \
174
  inline v8::Local<v8::Value> code(v8::Isolate* isolate) {                   \
175
    return code(isolate, message);                                           \
176
  }                                                                          \
177
  inline void THROW_ ## code(v8::Isolate* isolate) {                         \
178
    isolate->ThrowException(code(isolate, message));                         \
179
  }                                                                          \
180
  inline void THROW_ ## code(Environment* env) {                             \
181
    THROW_ ## code(env->isolate());                                          \
182
  }
183
1396
  PREDEFINED_ERROR_MESSAGES(V)
184
#undef V
185
186
// Errors with predefined non-static messages
187
13
inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env,
188
                                               int64_t timeout) {
189
26
  std::ostringstream message;
190
13
  message << "Script execution timed out after ";
191
13
  message << timeout << "ms";
192
13
  THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, message.str().c_str());
193
13
}
194
195
inline v8::Local<v8::Value> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) {
196
  char message[128];
197
  snprintf(message, sizeof(message),
198
      "Cannot create a Buffer larger than 0x%zx bytes",
199
      v8::TypedArray::kMaxLength);
200
  return ERR_BUFFER_TOO_LARGE(isolate, message);
201
}
202
203
8
inline v8::Local<v8::Value> ERR_STRING_TOO_LONG(v8::Isolate* isolate) {
204
  char message[128];
205
  snprintf(message, sizeof(message),
206
      "Cannot create a string longer than 0x%x characters",
207
8
      v8::String::kMaxLength);
208
8
  return ERR_STRING_TOO_LONG(isolate, message);
209
}
210
211
#define THROW_AND_RETURN_IF_NOT_BUFFER(env, val, prefix)                     \
212
  do {                                                                       \
213
    if (!Buffer::HasInstance(val))                                           \
214
      return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
215
                                              prefix " must be a buffer");   \
216
  } while (0)
217
218
#define THROW_AND_RETURN_IF_NOT_STRING(env, val, prefix)                     \
219
  do {                                                                       \
220
    if (!val->IsString())                                                    \
221
      return node::THROW_ERR_INVALID_ARG_TYPE(env,                           \
222
                                              prefix " must be a string");   \
223
  } while (0)
224
225
namespace errors {
226
227
class TryCatchScope : public v8::TryCatch {
228
 public:
229
  enum class CatchMode { kNormal, kFatal };
230
231
885789
  explicit TryCatchScope(Environment* env, CatchMode mode = CatchMode::kNormal)
232
885789
      : v8::TryCatch(env->isolate()), env_(env), mode_(mode) {}
233
  ~TryCatchScope();
234
235
  // Since the dtor is not virtual we need to make sure no one creates
236
  // object of it in the free store that might be held by polymorphic pointers.
237
  void* operator new(std::size_t count) = delete;
238
  void* operator new[](std::size_t count) = delete;
239
  TryCatchScope(TryCatchScope&) = delete;
240
  TryCatchScope(TryCatchScope&&) = delete;
241
  TryCatchScope operator=(TryCatchScope&) = delete;
242
  TryCatchScope operator=(TryCatchScope&&) = delete;
243
244
 private:
245
  Environment* env_;
246
  CatchMode mode_;
247
};
248
249
// Trigger the global uncaught exception handler `process._fatalException`
250
// in JS land (which emits the 'uncaughtException' event). If that returns
251
// true, continue program execution, otherwise exit the process.
252
void TriggerUncaughtException(v8::Isolate* isolate,
253
                              const v8::TryCatch& try_catch);
254
void TriggerUncaughtException(v8::Isolate* isolate,
255
                              v8::Local<v8::Value> error,
256
                              v8::Local<v8::Message> message,
257
                              bool from_promise = false);
258
259
const char* errno_string(int errorno);
260
void PerIsolateMessageListener(v8::Local<v8::Message> message,
261
                               v8::Local<v8::Value> error);
262
263
void DecorateErrorStack(Environment* env,
264
                        const errors::TryCatchScope& try_catch);
265
}  // namespace errors
266
267
}  // namespace node
268
269
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
270
271
#endif  // SRC_NODE_ERRORS_H_