GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_internals.h Lines: 50 52 96.2 %
Date: 2019-02-26 22:23:30 Branches: 24 36 66.7 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_NODE_INTERNALS_H_
23
#define SRC_NODE_INTERNALS_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "env-inl.h"
28
#include "node.h"
29
#include "node_binding.h"
30
#include "node_mutex.h"
31
#include "node_persistent.h"
32
#include "tracing/trace_event.h"
33
#include "util-inl.h"
34
#include "uv.h"
35
#include "v8.h"
36
37
#include <stdint.h>
38
#include <stdlib.h>
39
40
#include <string>
41
#include <vector>
42
43
// Custom constants used by both node_constants.cc and node_zlib.cc
44
#define Z_MIN_WINDOWBITS 8
45
#define Z_MAX_WINDOWBITS 15
46
#define Z_DEFAULT_WINDOWBITS 15
47
48
struct sockaddr;
49
50
namespace node {
51
52
namespace native_module {
53
class NativeModuleLoader;
54
}
55
56
namespace per_process {
57
extern Mutex env_var_mutex;
58
extern uint64_t node_start_time;
59
extern bool v8_is_profiling;
60
}  // namespace per_process
61
62
// Forward declaration
63
class Environment;
64
65
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
66
// Sets address and port properties on the info object and returns it.
67
// If |info| is omitted, a new object is returned.
68
v8::Local<v8::Object> AddressToJS(
69
    Environment* env,
70
    const sockaddr* addr,
71
    v8::Local<v8::Object> info = v8::Local<v8::Object>());
72
73
template <typename T, int (*F)(const typename T::HandleType*, sockaddr*, int*)>
74
3553
void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
75
  T* wrap;
76

7106
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
77
                          args.Holder(),
78
                          args.GetReturnValue().Set(UV_EBADF));
79

7106
  CHECK(args[0]->IsObject());
80
  sockaddr_storage storage;
81
3553
  int addrlen = sizeof(storage);
82
3553
  sockaddr* const addr = reinterpret_cast<sockaddr*>(&storage);
83
3553
  const int err = F(&wrap->handle_, addr, &addrlen);
84

3553
  if (err == 0)
85
7096
    AddressToJS(wrap->env(), addr, args[0].As<v8::Object>());
86
7106
  args.GetReturnValue().Set(err);
87
}
88
89
void WaitForInspectorDisconnect(Environment* env);
90
void SignalExit(int signo);
91
#ifdef __POSIX__
92
void RegisterSignalHandler(int signal,
93
                           void (*handler)(int signal),
94
                           bool reset_handler = false);
95
#endif
96
97
std::string GetHumanReadableProcessName();
98
void GetHumanReadableProcessName(char (*name)[1024]);
99
100
namespace task_queue {
101
void PromiseRejectCallback(v8::PromiseRejectMessage message);
102
}  // namespace task_queue
103
104
12484
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
105
 public:
106
4400
  inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
107
108
  void* Allocate(size_t size) override;  // Defined in src/node.cc
109
473262
  void* AllocateUninitialized(size_t size) override
110
473262
    { return node::UncheckedMalloc(size); }
111
1396441
  void Free(void* data, size_t) override { free(data); }
112
398987
  virtual void* Reallocate(void* data, size_t old_size, size_t size) {
113
    return static_cast<void*>(
114
398987
        UncheckedRealloc<char>(static_cast<char*>(data), size));
115
  }
116
2
  virtual void RegisterPointer(void* data, size_t size) {}
117
3
  virtual void UnregisterPointer(void* data, size_t size) {}
118
119
 private:
120
  uint32_t zero_fill_field_ = 1;  // Boolean but exposed as uint32 to JS land.
121
};
122
123
class DebuggingArrayBufferAllocator final : public ArrayBufferAllocator {
124
 public:
125
  ~DebuggingArrayBufferAllocator() override;
126
  void* Allocate(size_t size) override;
127
  void* AllocateUninitialized(size_t size) override;
128
  void Free(void* data, size_t size) override;
129
  void* Reallocate(void* data, size_t old_size, size_t size) override;
130
  void RegisterPointer(void* data, size_t size) override;
131
  void UnregisterPointer(void* data, size_t size) override;
132
133
 private:
134
  void RegisterPointerInternal(void* data, size_t size);
135
  void UnregisterPointerInternal(void* data, size_t size);
136
  Mutex mutex_;
137
  std::unordered_map<void*, size_t> allocations_;
138
};
139
140
namespace Buffer {
141
v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
142
v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
143
// Takes ownership of |data|.
144
v8::MaybeLocal<v8::Object> New(Environment* env,
145
                               char* data,
146
                               size_t length,
147
                               void (*callback)(char* data, void* hint),
148
                               void* hint);
149
// Takes ownership of |data|.  Must allocate |data| with the current Isolate's
150
// ArrayBuffer::Allocator().
151
v8::MaybeLocal<v8::Object> New(Environment* env,
152
                               char* data,
153
                               size_t length,
154
                               bool uses_malloc);
155
156
// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
157
// Utf8Value and TwoByteValue).
158
// If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
159
// changed.
160
// If |buf| contains actual data, this method takes ownership of |buf|'s
161
// underlying buffer. However, |buf| itself can be reused even after this call,
162
// but its capacity, if increased through AllocateSufficientStorage, is not
163
// guaranteed to stay the same.
164
template <typename T>
165
534
static v8::MaybeLocal<v8::Object> New(Environment* env,
166
                                      MaybeStackBuffer<T>* buf) {
167
  v8::MaybeLocal<v8::Object> ret;
168
534
  char* src = reinterpret_cast<char*>(buf->out());
169
534
  const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
170
171

534
  if (buf->IsAllocated())
172
2
    ret = New(env, src, len_in_bytes, true);
173

532
  else if (!buf->IsInvalidated())
174
532
    ret = Copy(env, src, len_in_bytes);
175
176

534
  if (ret.IsEmpty())
177
    return ret;
178
179

534
  if (buf->IsAllocated())
180
2
    buf->Release();
181
182
534
  return ret;
183
}
184
}  // namespace Buffer
185
186
v8::MaybeLocal<v8::Value> InternalMakeCallback(
187
    Environment* env,
188
    v8::Local<v8::Object> recv,
189
    const v8::Local<v8::Function> callback,
190
    int argc,
191
    v8::Local<v8::Value> argv[],
192
    async_context asyncContext);
193
194
class InternalCallbackScope {
195
 public:
196
  // Tell the constructor whether its `object` parameter may be empty or not.
197
  enum ResourceExpectation { kRequireResource, kAllowEmptyResource };
198
  InternalCallbackScope(Environment* env,
199
                        v8::Local<v8::Object> object,
200
                        const async_context& asyncContext,
201
                        ResourceExpectation expect = kRequireResource);
202
  // Utility that can be used by AsyncWrap classes.
203
  explicit InternalCallbackScope(AsyncWrap* async_wrap);
204
  ~InternalCallbackScope();
205
  void Close();
206
207
2384573
  inline bool Failed() const { return failed_; }
208
591
  inline void MarkAsFailed() { failed_ = true; }
209
210
 private:
211
  Environment* env_;
212
  async_context async_context_;
213
  v8::Local<v8::Object> object_;
214
  Environment::AsyncCallbackScope callback_scope_;
215
  bool failed_ = false;
216
  bool pushed_ids_ = false;
217
  bool closed_ = false;
218
};
219
220
class ThreadPoolWork {
221
 public:
222
2434
  explicit inline ThreadPoolWork(Environment* env) : env_(env) {
223
2434
    CHECK_NOT_NULL(env);
224
2434
  }
225
2329
  inline virtual ~ThreadPoolWork() = default;
226
227
  inline void ScheduleWork();
228
  inline int CancelWork();
229
230
  virtual void DoThreadPoolWork() = 0;
231
  virtual void AfterThreadPoolWork(int status) = 0;
232
233
 private:
234
  Environment* env_;
235
  uv_work_t work_req_;
236
};
237
238
9076
void ThreadPoolWork::ScheduleWork() {
239
9076
  env_->IncreaseWaitingRequestCounter();
240
  int status = uv_queue_work(
241
      env_->event_loop(),
242
      &work_req_,
243
27226
      [](uv_work_t* req) {
244
9075
        ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req);
245
9075
        self->DoThreadPoolWork();
246
27225
      },
247
27228
      [](uv_work_t* req, int status) {
248
9076
        ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req);
249
9076
        self->env_->DecreaseWaitingRequestCounter();
250
9076
        self->AfterThreadPoolWork(status);
251
36303
      });
252
9076
  CHECK_EQ(status, 0);
253
9076
}
254
255
1
int ThreadPoolWork::CancelWork() {
256
1
  return uv_cancel(reinterpret_cast<uv_req_t*>(&work_req_));
257
}
258
259
#define TRACING_CATEGORY_NODE "node"
260
#define TRACING_CATEGORY_NODE1(one)                                           \
261
    TRACING_CATEGORY_NODE ","                                                 \
262
    TRACING_CATEGORY_NODE "." #one
263
#define TRACING_CATEGORY_NODE2(one, two)                                      \
264
    TRACING_CATEGORY_NODE ","                                                 \
265
    TRACING_CATEGORY_NODE "." #one ","                                        \
266
    TRACING_CATEGORY_NODE "." #one "." #two
267
268
// Functions defined in node.cc that are exposed via the bootstrapper object
269
270
#if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
271
#define NODE_IMPLEMENTS_POSIX_CREDENTIALS 1
272
#endif  // __POSIX__ && !defined(__ANDROID__) && !defined(__CloudABI__)
273
274
namespace credentials {
275
bool SafeGetenv(const char* key, std::string* text);
276
}  // namespace credentials
277
278
void DefineZlibConstants(v8::Local<v8::Object> target);
279
280
v8::MaybeLocal<v8::Value> RunBootstrapping(Environment* env);
281
v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
282
                                         const char* main_script_id);
283
namespace coverage {
284
bool StartCoverageCollection(Environment* env);
285
}
286
}  // namespace node
287
288
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
289
290
#endif  // SRC_NODE_INTERNALS_H_