GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: api/hooks.cc Lines: 102 113 90.3 %
Date: 2022-12-31 04:22:30 Branches: 26 38 68.4 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node_internals.h"
3
#include "node_process-inl.h"
4
#include "async_wrap.h"
5
6
namespace node {
7
8
using v8::Context;
9
using v8::HandleScope;
10
using v8::Integer;
11
using v8::Isolate;
12
using v8::Just;
13
using v8::Local;
14
using v8::Maybe;
15
using v8::NewStringType;
16
using v8::Nothing;
17
using v8::Object;
18
using v8::String;
19
20
6604
void RunAtExit(Environment* env) {
21
6604
  env->RunAtExitCallbacks();
22
6604
}
23
24
13057
void AtExit(Environment* env, void (*cb)(void* arg), void* arg) {
25
13057
  CHECK_NOT_NULL(env);
26
13057
  env->AtExit(cb, arg);
27
13057
}
28
29
void EmitBeforeExit(Environment* env) {
30
  USE(EmitProcessBeforeExit(env));
31
}
32
33
5577
Maybe<bool> EmitProcessBeforeExit(Environment* env) {
34

16274
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "BeforeExit");
35
5577
  if (!env->destroy_async_id_list()->empty())
36
284
    AsyncWrap::DestroyAsyncIdsCallback(env);
37
38
5577
  Isolate* isolate = env->isolate();
39
11152
  HandleScope handle_scope(isolate);
40
5577
  Context::Scope context_scope(env->context());
41
42
5577
  if (!env->can_call_into_js()) {
43
7
    return Nothing<bool>();
44
  }
45
46
  Local<Integer> exit_code = Integer::New(
47
5570
      isolate, static_cast<int32_t>(env->exit_code(ExitCode::kNoFailure)));
48
49
5570
  return ProcessEmit(env, "beforeExit", exit_code).IsEmpty() ?
50
5568
      Nothing<bool>() : Just(true);
51
}
52
53
static ExitCode EmitExitInternal(Environment* env) {
54
  return EmitProcessExitInternal(env).FromMaybe(ExitCode::kGenericUserError);
55
}
56
57
int EmitExit(Environment* env) {
58
  return static_cast<int>(EmitExitInternal(env));
59
}
60
61
5543
Maybe<ExitCode> EmitProcessExitInternal(Environment* env) {
62
  // process.emit('exit')
63
5543
  Isolate* isolate = env->isolate();
64
11076
  HandleScope handle_scope(isolate);
65
5543
  Context::Scope context_scope(env->context());
66
67
5543
  env->set_exiting(true);
68
69
5543
  if (!env->can_call_into_js()) {
70
1
    return Nothing<ExitCode>();
71
  }
72
73
  Local<Integer> exit_code = Integer::New(
74
5542
      isolate, static_cast<int32_t>(env->exit_code(ExitCode::kNoFailure)));
75
76
11074
  if (ProcessEmit(env, "exit", exit_code).IsEmpty()) {
77
1
    return Nothing<ExitCode>();
78
  }
79
  // Reload exit code, it may be changed by `emit('exit')`
80
5531
  return Just(env->exit_code(ExitCode::kNoFailure));
81
}
82
83
Maybe<int> EmitProcessExit(Environment* env) {
84
  Maybe<ExitCode> result = EmitProcessExitInternal(env);
85
  if (result.IsNothing()) {
86
    return Nothing<int>();
87
  }
88
  return Just(static_cast<int>(result.FromJust()));
89
}
90
91
typedef void (*CleanupHook)(void* arg);
92
typedef void (*AsyncCleanupHook)(void* arg, void(*)(void*), void*);
93
94
struct AsyncCleanupHookInfo final {
95
  Environment* env;
96
  AsyncCleanupHook fun;
97
  void* arg;
98
  bool started = false;
99
  // Use a self-reference to make sure the storage is kept alive while the
100
  // cleanup hook is registered but not yet finished.
101
  std::shared_ptr<AsyncCleanupHookInfo> self;
102
};
103
104
// Opaque type that is basically an alias for `shared_ptr<AsyncCleanupHookInfo>`
105
// (but not publicly so for easier ABI/API changes). In particular,
106
// std::shared_ptr does not generally maintain a consistent ABI even on a
107
// specific platform.
108
struct ACHHandle final {
109
  std::shared_ptr<AsyncCleanupHookInfo> info;
110
};
111
// This is implemented as an operator on a struct because otherwise you can't
112
// default-initialize AsyncCleanupHookHandle, because in C++ for a
113
// std::unique_ptr to be default-initializable the deleter type also needs
114
// to be default-initializable; in particular, function types don't satisfy
115
// this.
116
10
void DeleteACHHandle::operator ()(ACHHandle* handle) const { delete handle; }
117
118
41
void AddEnvironmentCleanupHook(Isolate* isolate,
119
                               CleanupHook fun,
120
                               void* arg) {
121
41
  Environment* env = Environment::GetCurrent(isolate);
122
41
  CHECK_NOT_NULL(env);
123
41
  env->AddCleanupHook(fun, arg);
124
41
}
125
126
27
void RemoveEnvironmentCleanupHook(Isolate* isolate,
127
                                  CleanupHook fun,
128
                                  void* arg) {
129
27
  Environment* env = Environment::GetCurrent(isolate);
130
27
  CHECK_NOT_NULL(env);
131
27
  env->RemoveCleanupHook(fun, arg);
132
27
}
133
134
6
static void FinishAsyncCleanupHook(void* arg) {
135
6
  AsyncCleanupHookInfo* info = static_cast<AsyncCleanupHookInfo*>(arg);
136
12
  std::shared_ptr<AsyncCleanupHookInfo> keep_alive = info->self;
137
138
6
  info->env->DecreaseWaitingRequestCounter();
139
6
  info->self.reset();
140
6
}
141
142
6
static void RunAsyncCleanupHook(void* arg) {
143
6
  AsyncCleanupHookInfo* info = static_cast<AsyncCleanupHookInfo*>(arg);
144
6
  info->env->IncreaseWaitingRequestCounter();
145
6
  info->started = true;
146
6
  info->fun(info->arg, FinishAsyncCleanupHook, info);
147
6
}
148
149
10
ACHHandle* AddEnvironmentCleanupHookInternal(
150
    Isolate* isolate,
151
    AsyncCleanupHook fun,
152
    void* arg) {
153
10
  Environment* env = Environment::GetCurrent(isolate);
154
10
  CHECK_NOT_NULL(env);
155
10
  auto info = std::make_shared<AsyncCleanupHookInfo>();
156
10
  info->env = env;
157
10
  info->fun = fun;
158
10
  info->arg = arg;
159
10
  info->self = info;
160
10
  env->AddCleanupHook(RunAsyncCleanupHook, info.get());
161
10
  return new ACHHandle { info };
162
}
163
164
10
void RemoveEnvironmentCleanupHookInternal(
165
    ACHHandle* handle) {
166
10
  if (handle->info->started) return;
167
4
  handle->info->self.reset();
168
4
  handle->info->env->RemoveCleanupHook(RunAsyncCleanupHook, handle->info.get());
169
}
170
171
3
void RequestInterrupt(Environment* env, void (*fun)(void* arg), void* arg) {
172
3
  env->RequestInterrupt([fun, arg](Environment* env) {
173
    // Disallow JavaScript execution during interrupt.
174
    Isolate::DisallowJavascriptExecutionScope scope(
175
        env->isolate(),
176
4
        Isolate::DisallowJavascriptExecutionScope::CRASH_ON_FAILURE);
177
3
    fun(arg);
178
1
  });
179
3
}
180
181
2
async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
182
2
  Environment* env = Environment::GetCurrent(isolate);
183
2
  if (env == nullptr) return -1;
184
2
  return env->execution_async_id();
185
}
186
187
2
async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
188
2
  Environment* env = Environment::GetCurrent(isolate);
189
2
  if (env == nullptr) return -1;
190
2
  return env->trigger_async_id();
191
}
192
193
194
542
async_context EmitAsyncInit(Isolate* isolate,
195
                            Local<Object> resource,
196
                            const char* name,
197
                            async_id trigger_async_id) {
198
1084
  HandleScope handle_scope(isolate);
199
  Local<String> type =
200
542
      String::NewFromUtf8(isolate, name, NewStringType::kInternalized)
201
542
          .ToLocalChecked();
202
542
  return EmitAsyncInit(isolate, resource, type, trigger_async_id);
203
}
204
205
542
async_context EmitAsyncInit(Isolate* isolate,
206
                            Local<Object> resource,
207
                            Local<String> name,
208
                            async_id trigger_async_id) {
209
542
  DebugSealHandleScope handle_scope(isolate);
210
542
  Environment* env = Environment::GetCurrent(isolate);
211
542
  CHECK_NOT_NULL(env);
212
213
  // Initialize async context struct
214
542
  if (trigger_async_id == -1)
215
539
    trigger_async_id = env->get_default_trigger_async_id();
216
217
  async_context context = {
218
542
    env->new_async_id(),  // async_id_
219
    trigger_async_id  // trigger_async_id_
220
542
  };
221
222
  // Run init hooks
223
542
  AsyncWrap::EmitAsyncInit(env, resource, name, context.async_id,
224
                           context.trigger_async_id);
225
226
542
  return context;
227
}
228
229
3
void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {
230
3
  EmitAsyncDestroy(Environment::GetCurrent(isolate), asyncContext);
231
3
}
232
233
538
void EmitAsyncDestroy(Environment* env, async_context asyncContext) {
234
538
  AsyncWrap::EmitDestroy(env, asyncContext.async_id);
235
538
}
236
237
}  // namespace node