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: 46 47 97.9 %
Date: 2019-02-23 22:23:05 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
12497
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
105
 public:
106
4410
  inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
107
108
  void* Allocate(size_t size) override;  // Defined in src/node.cc
109
6172
  void* AllocateUninitialized(size_t size) override
110
6172
    { return node::UncheckedMalloc(size); }
111
1366275
  void Free(void* data, size_t) override { free(data); }
112
113
 private:
114
  uint32_t zero_fill_field_ = 1;  // Boolean but exposed as uint32 to JS land.
115
};
116
117
namespace Buffer {
118
v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
119
v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
120
// Takes ownership of |data|.
121
v8::MaybeLocal<v8::Object> New(Environment* env,
122
                               char* data,
123
                               size_t length,
124
                               void (*callback)(char* data, void* hint),
125
                               void* hint);
126
// Takes ownership of |data|.  Must allocate |data| with malloc() or realloc()
127
// because ArrayBufferAllocator::Free() deallocates it again with free().
128
// Mixing operator new and free() is undefined behavior so don't do that.
129
v8::MaybeLocal<v8::Object> New(Environment* env, char* data, size_t length);
130
131
// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
132
// Utf8Value and TwoByteValue).
133
// If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
134
// changed.
135
// If |buf| contains actual data, this method takes ownership of |buf|'s
136
// underlying buffer. However, |buf| itself can be reused even after this call,
137
// but its capacity, if increased through AllocateSufficientStorage, is not
138
// guaranteed to stay the same.
139
template <typename T>
140
534
static v8::MaybeLocal<v8::Object> New(Environment* env,
141
                                      MaybeStackBuffer<T>* buf) {
142
  v8::MaybeLocal<v8::Object> ret;
143
534
  char* src = reinterpret_cast<char*>(buf->out());
144
534
  const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
145
146

534
  if (buf->IsAllocated())
147
2
    ret = New(env, src, len_in_bytes);
148

532
  else if (!buf->IsInvalidated())
149
532
    ret = Copy(env, src, len_in_bytes);
150
151

534
  if (ret.IsEmpty())
152
    return ret;
153
154

534
  if (buf->IsAllocated())
155
2
    buf->Release();
156
157
534
  return ret;
158
}
159
}  // namespace Buffer
160
161
v8::MaybeLocal<v8::Value> InternalMakeCallback(
162
    Environment* env,
163
    v8::Local<v8::Object> recv,
164
    const v8::Local<v8::Function> callback,
165
    int argc,
166
    v8::Local<v8::Value> argv[],
167
    async_context asyncContext);
168
169
class InternalCallbackScope {
170
 public:
171
  // Tell the constructor whether its `object` parameter may be empty or not.
172
  enum ResourceExpectation { kRequireResource, kAllowEmptyResource };
173
  InternalCallbackScope(Environment* env,
174
                        v8::Local<v8::Object> object,
175
                        const async_context& asyncContext,
176
                        ResourceExpectation expect = kRequireResource);
177
  // Utility that can be used by AsyncWrap classes.
178
  explicit InternalCallbackScope(AsyncWrap* async_wrap);
179
  ~InternalCallbackScope();
180
  void Close();
181
182
2386213
  inline bool Failed() const { return failed_; }
183
586
  inline void MarkAsFailed() { failed_ = true; }
184
185
 private:
186
  Environment* env_;
187
  async_context async_context_;
188
  v8::Local<v8::Object> object_;
189
  Environment::AsyncCallbackScope callback_scope_;
190
  bool failed_ = false;
191
  bool pushed_ids_ = false;
192
  bool closed_ = false;
193
};
194
195
class ThreadPoolWork {
196
 public:
197
2439
  explicit inline ThreadPoolWork(Environment* env) : env_(env) {
198
2439
    CHECK_NOT_NULL(env);
199
2439
  }
200
2334
  inline virtual ~ThreadPoolWork() = default;
201
202
  inline void ScheduleWork();
203
  inline int CancelWork();
204
205
  virtual void DoThreadPoolWork() = 0;
206
  virtual void AfterThreadPoolWork(int status) = 0;
207
208
 private:
209
  Environment* env_;
210
  uv_work_t work_req_;
211
};
212
213
9087
void ThreadPoolWork::ScheduleWork() {
214
9087
  env_->IncreaseWaitingRequestCounter();
215
  int status = uv_queue_work(
216
      env_->event_loop(),
217
      &work_req_,
218
27259
      [](uv_work_t* req) {
219
9086
        ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req);
220
9086
        self->DoThreadPoolWork();
221
27257
      },
222
27261
      [](uv_work_t* req, int status) {
223
9087
        ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req);
224
9087
        self->env_->DecreaseWaitingRequestCounter();
225
9087
        self->AfterThreadPoolWork(status);
226
36347
      });
227
9087
  CHECK_EQ(status, 0);
228
9087
}
229
230
1
int ThreadPoolWork::CancelWork() {
231
1
  return uv_cancel(reinterpret_cast<uv_req_t*>(&work_req_));
232
}
233
234
#define TRACING_CATEGORY_NODE "node"
235
#define TRACING_CATEGORY_NODE1(one)                                           \
236
    TRACING_CATEGORY_NODE ","                                                 \
237
    TRACING_CATEGORY_NODE "." #one
238
#define TRACING_CATEGORY_NODE2(one, two)                                      \
239
    TRACING_CATEGORY_NODE ","                                                 \
240
    TRACING_CATEGORY_NODE "." #one ","                                        \
241
    TRACING_CATEGORY_NODE "." #one "." #two
242
243
// Functions defined in node.cc that are exposed via the bootstrapper object
244
245
#if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
246
#define NODE_IMPLEMENTS_POSIX_CREDENTIALS 1
247
#endif  // __POSIX__ && !defined(__ANDROID__) && !defined(__CloudABI__)
248
249
namespace credentials {
250
bool SafeGetenv(const char* key, std::string* text);
251
}  // namespace credentials
252
253
void DefineZlibConstants(v8::Local<v8::Object> target);
254
255
v8::MaybeLocal<v8::Value> RunBootstrapping(Environment* env);
256
v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
257
                                         const char* main_script_id);
258
namespace coverage {
259
bool StartCoverageCollection(Environment* env);
260
}
261
}  // namespace node
262
263
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
264
265
#endif  // SRC_NODE_INTERNALS_H_