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