asherah 3.0.2 → 3.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/asherah.cc +82 -46
- package/src/asherah_async_worker.h +3 -3
- package/src/cobhan_buffer.h +1 -1
- package/src/logging.cc +7 -12
- package/src/logging.h +2 -5
- package/src/logging_napi.cc +25 -70
- package/src/logging_napi.h +1 -3
- package/src/napi_utils.h +14 -16
- package/src/scoped_allocate.h +6 -11
package/package.json
CHANGED
package/src/asherah.cc
CHANGED
|
@@ -82,9 +82,13 @@ private:
|
|
|
82
82
|
|
|
83
83
|
// extern GoInt32 SetupJson(void* configJson);
|
|
84
84
|
GoInt32 result = SetupJson(config);
|
|
85
|
-
EndSetupAsherah(result, product_id_length, service_name_length);
|
|
85
|
+
EndSetupAsherah(env, result, product_id_length, service_name_length);
|
|
86
|
+
} catch (Napi::Error &e) {
|
|
87
|
+
e.ThrowAsJavaScriptException();
|
|
88
|
+
return;
|
|
86
89
|
} catch (const std::exception &e) {
|
|
87
|
-
|
|
90
|
+
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
91
|
+
return;
|
|
88
92
|
}
|
|
89
93
|
}
|
|
90
94
|
|
|
@@ -105,8 +109,12 @@ private:
|
|
|
105
109
|
service_name_length);
|
|
106
110
|
worker->Queue();
|
|
107
111
|
return worker->Promise();
|
|
112
|
+
} catch (Napi::Error &e) {
|
|
113
|
+
e.ThrowAsJavaScriptException();
|
|
114
|
+
return env.Undefined();
|
|
108
115
|
} catch (const std::exception &e) {
|
|
109
|
-
|
|
116
|
+
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
117
|
+
return env.Undefined();
|
|
110
118
|
}
|
|
111
119
|
}
|
|
112
120
|
|
|
@@ -116,9 +124,13 @@ private:
|
|
|
116
124
|
try {
|
|
117
125
|
BeginShutdownAsherah(env, __func__, info);
|
|
118
126
|
Shutdown();
|
|
119
|
-
EndShutdownAsherah();
|
|
127
|
+
EndShutdownAsherah(env);
|
|
128
|
+
} catch (Napi::Error &e) {
|
|
129
|
+
e.ThrowAsJavaScriptException();
|
|
130
|
+
return;
|
|
120
131
|
} catch (const std::exception &e) {
|
|
121
|
-
|
|
132
|
+
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
133
|
+
return;
|
|
122
134
|
}
|
|
123
135
|
}
|
|
124
136
|
|
|
@@ -130,8 +142,12 @@ private:
|
|
|
130
142
|
auto worker = new ShutdownAsherahWorker(env, this);
|
|
131
143
|
worker->Queue();
|
|
132
144
|
return worker->Promise();
|
|
145
|
+
} catch (Napi::Error &e) {
|
|
146
|
+
e.ThrowAsJavaScriptException();
|
|
147
|
+
return env.Undefined();
|
|
133
148
|
} catch (const std::exception &e) {
|
|
134
|
-
|
|
149
|
+
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
150
|
+
return env.Undefined();
|
|
135
151
|
}
|
|
136
152
|
}
|
|
137
153
|
|
|
@@ -189,9 +205,12 @@ private:
|
|
|
189
205
|
GoInt32 result = EncryptToJson(partition_id, input, output);
|
|
190
206
|
|
|
191
207
|
EndEncryptToJson(env, output, result, output_string);
|
|
192
|
-
|
|
208
|
+
} catch (Napi::Error &e) {
|
|
209
|
+
e.ThrowAsJavaScriptException();
|
|
210
|
+
return env.Undefined();
|
|
193
211
|
} catch (const std::exception &e) {
|
|
194
|
-
|
|
212
|
+
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
213
|
+
return env.Undefined();
|
|
195
214
|
}
|
|
196
215
|
|
|
197
216
|
return output_string;
|
|
@@ -220,8 +239,12 @@ private:
|
|
|
220
239
|
new EncryptAsherahWorker(env, this, partition_id, input, output);
|
|
221
240
|
worker->Queue();
|
|
222
241
|
return worker->Promise();
|
|
242
|
+
} catch (Napi::Error &e) {
|
|
243
|
+
e.ThrowAsJavaScriptException();
|
|
244
|
+
return env.Undefined();
|
|
223
245
|
} catch (const std::exception &e) {
|
|
224
|
-
|
|
246
|
+
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
247
|
+
return env.Undefined();
|
|
225
248
|
}
|
|
226
249
|
}
|
|
227
250
|
|
|
@@ -272,12 +295,17 @@ private:
|
|
|
272
295
|
// void* dataPtr);
|
|
273
296
|
GoInt32 result = DecryptFromJson(partition_id, input, output);
|
|
274
297
|
|
|
275
|
-
CheckResult(result);
|
|
298
|
+
CheckResult(env, result);
|
|
276
299
|
|
|
277
300
|
output_value = output.ToBuffer(env); // NOLINT(*-slicing)
|
|
301
|
+
} catch (Napi::Error &e) {
|
|
302
|
+
e.ThrowAsJavaScriptException();
|
|
303
|
+
return env.Undefined();
|
|
278
304
|
} catch (const std::exception &e) {
|
|
279
|
-
|
|
305
|
+
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
306
|
+
return env.Undefined();
|
|
280
307
|
}
|
|
308
|
+
|
|
281
309
|
return output_value;
|
|
282
310
|
}
|
|
283
311
|
|
|
@@ -298,8 +326,12 @@ private:
|
|
|
298
326
|
env, this, partition_id, input, output);
|
|
299
327
|
worker->Queue();
|
|
300
328
|
return worker->Promise();
|
|
329
|
+
} catch (Napi::Error &e) {
|
|
330
|
+
e.ThrowAsJavaScriptException();
|
|
331
|
+
return env.Undefined();
|
|
301
332
|
} catch (const std::exception &e) {
|
|
302
|
-
|
|
333
|
+
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
334
|
+
return env.Undefined();
|
|
303
335
|
}
|
|
304
336
|
}
|
|
305
337
|
|
|
@@ -310,10 +342,6 @@ private:
|
|
|
310
342
|
try {
|
|
311
343
|
NapiUtils::RequireParameterCount(info, 2);
|
|
312
344
|
|
|
313
|
-
if (unlikely(!info[0].IsString() || !info[1].IsString())) {
|
|
314
|
-
logger.log_error_and_throw(__func__, "Wrong argument types");
|
|
315
|
-
}
|
|
316
|
-
|
|
317
345
|
Napi::String partition_id_string;
|
|
318
346
|
Napi::Value input_value;
|
|
319
347
|
BeginDecryptFromJson(env, __func__, info, partition_id_string,
|
|
@@ -349,8 +377,12 @@ private:
|
|
|
349
377
|
GoInt32 result = DecryptFromJson(partition_id, input, output);
|
|
350
378
|
|
|
351
379
|
EndDecryptFromJson(env, output, result, output_string);
|
|
380
|
+
} catch (Napi::Error &e) {
|
|
381
|
+
e.ThrowAsJavaScriptException();
|
|
382
|
+
return env.Undefined();
|
|
352
383
|
} catch (const std::exception &e) {
|
|
353
|
-
|
|
384
|
+
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
385
|
+
return env.Undefined();
|
|
354
386
|
}
|
|
355
387
|
return output_string;
|
|
356
388
|
}
|
|
@@ -361,10 +393,6 @@ private:
|
|
|
361
393
|
try {
|
|
362
394
|
NapiUtils::RequireParameterCount(info, 2);
|
|
363
395
|
|
|
364
|
-
if (unlikely(!info[0].IsString() || !info[1].IsString())) {
|
|
365
|
-
logger.log_error_and_throw(__func__, "Wrong argument types");
|
|
366
|
-
}
|
|
367
|
-
|
|
368
396
|
Napi::String partition_id_string;
|
|
369
397
|
Napi::Value input_value;
|
|
370
398
|
BeginDecryptFromJson(env, __func__, info, partition_id_string,
|
|
@@ -380,8 +408,12 @@ private:
|
|
|
380
408
|
worker->Queue();
|
|
381
409
|
|
|
382
410
|
return worker->Promise();
|
|
411
|
+
} catch (Napi::Error &e) {
|
|
412
|
+
e.ThrowAsJavaScriptException();
|
|
413
|
+
return env.Undefined();
|
|
383
414
|
} catch (const std::exception &e) {
|
|
384
|
-
|
|
415
|
+
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
416
|
+
return env.Undefined();
|
|
385
417
|
}
|
|
386
418
|
}
|
|
387
419
|
|
|
@@ -392,6 +424,7 @@ private:
|
|
|
392
424
|
auto new_size = (size_t)item_size.Int32Value();
|
|
393
425
|
|
|
394
426
|
maximum_stack_alloc_size = new_size;
|
|
427
|
+
//TODO: This needs exception handling consistent with other methods
|
|
395
428
|
}
|
|
396
429
|
|
|
397
430
|
void SetSafetyPaddingOverhead(const Napi::CallbackInfo &info) {
|
|
@@ -408,23 +441,25 @@ private:
|
|
|
408
441
|
&info) { // NOLINT(*-convert-member-functions-to-static)
|
|
409
442
|
int32_t setup_status = setup_state.load(std::memory_order_acquire);
|
|
410
443
|
return Napi::Boolean::New(info.Env(), setup_status != 0);
|
|
444
|
+
//TODO: This needs exception handling consistent with other methods
|
|
411
445
|
}
|
|
412
446
|
|
|
413
447
|
void SetLogHook(const Napi::CallbackInfo &info) {
|
|
414
448
|
NapiUtils::RequireParameterCount(info, 1);
|
|
415
449
|
|
|
416
450
|
if (unlikely(!info[0].IsFunction())) {
|
|
417
|
-
|
|
451
|
+
NapiUtils::ThrowException(info.Env(), "Expected a function");
|
|
418
452
|
}
|
|
419
453
|
|
|
420
454
|
logger.set_log_hook(info[0].As<Napi::Function>());
|
|
455
|
+
//TODO: This needs exception handling consistent with other methods
|
|
421
456
|
}
|
|
422
457
|
|
|
423
458
|
void BeginSetupAsherah(const Napi::Env &env, const char *func_name,
|
|
424
459
|
const Napi::CallbackInfo &info,
|
|
425
460
|
Napi::String &config_string, size_t &product_id_length,
|
|
426
461
|
size_t &service_name_length) {
|
|
427
|
-
RequireAsherahNotSetup(func_name);
|
|
462
|
+
RequireAsherahNotSetup(env, func_name);
|
|
428
463
|
|
|
429
464
|
NapiUtils::RequireParameterCount(info, 1);
|
|
430
465
|
|
|
@@ -444,22 +479,22 @@ private:
|
|
|
444
479
|
verbose_flag = verbose;
|
|
445
480
|
}
|
|
446
481
|
|
|
447
|
-
void EndSetupAsherah(
|
|
448
|
-
size_t service_name_length) {
|
|
449
|
-
CheckResult(result);
|
|
482
|
+
void EndSetupAsherah(const Napi::Env &env, GoInt32 result,
|
|
483
|
+
size_t product_id_length, size_t service_name_length) {
|
|
484
|
+
CheckResult(env, result);
|
|
450
485
|
|
|
451
486
|
est_intermediate_key_overhead = product_id_length + service_name_length;
|
|
452
487
|
|
|
453
488
|
auto old_setup_state = setup_state.exchange(1, std::memory_order_acq_rel);
|
|
454
489
|
if (unlikely(old_setup_state != 0)) {
|
|
455
|
-
|
|
490
|
+
NapiUtils::ThrowException(env, "EndSetupAsherah: lost race to mark setup_state?!");
|
|
456
491
|
}
|
|
457
492
|
}
|
|
458
493
|
|
|
459
494
|
void BeginEncryptToJson(const Napi::Env &env, const char *func_name,
|
|
460
495
|
const Napi::CallbackInfo &info,
|
|
461
496
|
Napi::String &partition_id, Napi::Value &input) {
|
|
462
|
-
RequireAsherahSetup(func_name);
|
|
497
|
+
RequireAsherahSetup(env, func_name);
|
|
463
498
|
|
|
464
499
|
NapiUtils::RequireParameterCount(info, 2);
|
|
465
500
|
|
|
@@ -469,14 +504,14 @@ private:
|
|
|
469
504
|
|
|
470
505
|
void EndEncryptToJson(Napi::Env env, CobhanBufferNapi &output, GoInt32 result,
|
|
471
506
|
Napi::String &output_string) {
|
|
472
|
-
CheckResult(result);
|
|
507
|
+
CheckResult(env, result);
|
|
473
508
|
|
|
474
509
|
output_string = output.ToString(env);
|
|
475
510
|
}
|
|
476
511
|
|
|
477
512
|
void EndEncryptToJson(Napi::Env env, CobhanBufferNapi &output, GoInt32 result,
|
|
478
513
|
Napi::Buffer<unsigned char> &output_buffer) {
|
|
479
|
-
CheckResult(result);
|
|
514
|
+
CheckResult(env, result);
|
|
480
515
|
|
|
481
516
|
output_buffer = output.ToBuffer(env);
|
|
482
517
|
}
|
|
@@ -484,7 +519,7 @@ private:
|
|
|
484
519
|
void BeginDecryptFromJson(const Napi::Env &env, const char *func_name,
|
|
485
520
|
const Napi::CallbackInfo &info,
|
|
486
521
|
Napi::String &partition_id, Napi::Value &input) {
|
|
487
|
-
RequireAsherahSetup(func_name);
|
|
522
|
+
RequireAsherahSetup(env, func_name);
|
|
488
523
|
|
|
489
524
|
NapiUtils::RequireParameterCount(info, 2);
|
|
490
525
|
|
|
@@ -495,28 +530,28 @@ private:
|
|
|
495
530
|
void EndDecryptFromJson(Napi::Env &env, CobhanBufferNapi &output,
|
|
496
531
|
GoInt32 result,
|
|
497
532
|
Napi::Buffer<unsigned char> &output_buffer) {
|
|
498
|
-
CheckResult(result);
|
|
533
|
+
CheckResult(env, result);
|
|
499
534
|
|
|
500
535
|
output_buffer = output.ToBuffer(env);
|
|
501
536
|
}
|
|
502
537
|
|
|
503
538
|
void EndDecryptFromJson(Napi::Env &env, CobhanBufferNapi &output,
|
|
504
539
|
GoInt32 result, Napi::String &output_string) {
|
|
505
|
-
CheckResult(result);
|
|
540
|
+
CheckResult(env, result);
|
|
506
541
|
|
|
507
542
|
output_string = output.ToString(env);
|
|
508
543
|
}
|
|
509
544
|
|
|
510
|
-
void BeginShutdownAsherah(const Napi::Env
|
|
545
|
+
void BeginShutdownAsherah(const Napi::Env &env, const char *func_name,
|
|
511
546
|
const Napi::CallbackInfo &info) {
|
|
512
|
-
RequireAsherahSetup(func_name);
|
|
547
|
+
RequireAsherahSetup(env, func_name);
|
|
513
548
|
NapiUtils::RequireParameterCount(info, 0);
|
|
514
549
|
}
|
|
515
550
|
|
|
516
|
-
void EndShutdownAsherah() {
|
|
551
|
+
void EndShutdownAsherah(Napi::Env &env) {
|
|
517
552
|
auto old_setup_state = setup_state.exchange(0, std::memory_order_acq_rel);
|
|
518
553
|
if (unlikely(old_setup_state == 0)) {
|
|
519
|
-
|
|
554
|
+
NapiUtils::ThrowException(env, "EndSetupAsherah: lost race to mark setup_state?!");
|
|
520
555
|
}
|
|
521
556
|
}
|
|
522
557
|
|
|
@@ -536,7 +571,8 @@ private:
|
|
|
536
571
|
GoInt32 ExecuteTask() override { return SetupJson(config); }
|
|
537
572
|
|
|
538
573
|
Napi::Value OnOKTask(Napi::Env &env) override {
|
|
539
|
-
asherah->EndSetupAsherah(result, product_id_length,
|
|
574
|
+
asherah->EndSetupAsherah(env, result, product_id_length,
|
|
575
|
+
service_name_length);
|
|
540
576
|
return env.Undefined();
|
|
541
577
|
}
|
|
542
578
|
|
|
@@ -610,7 +646,7 @@ private:
|
|
|
610
646
|
}
|
|
611
647
|
|
|
612
648
|
Napi::Value OnOKTask(Napi::Env &env) override {
|
|
613
|
-
asherah->EndShutdownAsherah();
|
|
649
|
+
asherah->EndShutdownAsherah(env);
|
|
614
650
|
return env.Undefined();
|
|
615
651
|
}
|
|
616
652
|
};
|
|
@@ -619,21 +655,21 @@ private:
|
|
|
619
655
|
|
|
620
656
|
#pragma region Helpers
|
|
621
657
|
|
|
622
|
-
void CheckResult(GoInt32 result) {
|
|
658
|
+
void CheckResult(const Napi::Env &env, GoInt32 result) {
|
|
623
659
|
if (unlikely(result < 0)) {
|
|
624
|
-
|
|
660
|
+
NapiUtils::ThrowException(env, AsherahCobhanErrorToString(result));
|
|
625
661
|
}
|
|
626
662
|
}
|
|
627
663
|
|
|
628
|
-
void RequireAsherahSetup(const char *func_name) {
|
|
664
|
+
void RequireAsherahSetup(const Napi::Env &env, const char *func_name) {
|
|
629
665
|
if (unlikely(setup_state.load(std::memory_order_acquire) == 0)) {
|
|
630
|
-
|
|
666
|
+
NapiUtils::ThrowException(env, "RequireAsherahSetup: setup() not called");
|
|
631
667
|
}
|
|
632
668
|
}
|
|
633
669
|
|
|
634
|
-
void RequireAsherahNotSetup(const char *func_name) {
|
|
670
|
+
void RequireAsherahNotSetup(const Napi::Env &env, const char *func_name) {
|
|
635
671
|
if (unlikely(setup_state.load(std::memory_order_acquire) != 0)) {
|
|
636
|
-
|
|
672
|
+
NapiUtils::ThrowException(env, "RequireAsherahNotSetup: setup() already called");
|
|
637
673
|
}
|
|
638
674
|
}
|
|
639
675
|
|
|
@@ -32,7 +32,7 @@ protected:
|
|
|
32
32
|
private:
|
|
33
33
|
Napi::Promise::Deferred deferred;
|
|
34
34
|
|
|
35
|
-
void Execute()
|
|
35
|
+
void Execute() final {
|
|
36
36
|
try {
|
|
37
37
|
result = ExecuteTask();
|
|
38
38
|
} catch (const std::exception &ex) {
|
|
@@ -40,7 +40,7 @@ private:
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
void OnOK()
|
|
43
|
+
void OnOK() final {
|
|
44
44
|
Napi::Env env = Env();
|
|
45
45
|
Napi::HandleScope scope(env);
|
|
46
46
|
try {
|
|
@@ -51,7 +51,7 @@ private:
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
void OnError(Napi::Error const &error)
|
|
54
|
+
void OnError(Napi::Error const &error) final {
|
|
55
55
|
Napi::Env env = Env();
|
|
56
56
|
Napi::HandleScope scope(Env());
|
|
57
57
|
try {
|
package/src/cobhan_buffer.h
CHANGED
|
@@ -61,7 +61,7 @@ public:
|
|
|
61
61
|
cleanup();
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
__attribute__((unused)) std::string DebugPrintStdErr() const {
|
|
65
65
|
auto debug_string = std::string(get_data_ptr(), get_data_len_bytes());
|
|
66
66
|
std::ostringstream debug_output;
|
|
67
67
|
debug_output << "CobhanBuffer { " << std::endl
|
package/src/logging.cc
CHANGED
|
@@ -4,12 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
void Logger::set_verbose_flag(int32_t verbose) { verbose_flag = verbose != 0; }
|
|
6
6
|
|
|
7
|
-
Logger::Logger(std::string system_name) : system_name(system_name) {
|
|
7
|
+
Logger::Logger(const std::string &system_name) : system_name(system_name) {
|
|
8
8
|
std::cerr << "Created logger for " << system_name << std::endl << std::flush;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
Logger::stderr_debug_log(const char *function_name, const char *message) const {
|
|
11
|
+
void Logger::stderr_debug_log(const char *function_name, const char *message) const {
|
|
13
12
|
if (unlikely(verbose_flag)) {
|
|
14
13
|
std::cerr << system_name << ": [DEBUG] " << function_name << ": " << message
|
|
15
14
|
<< std::endl
|
|
@@ -17,8 +16,7 @@ Logger::stderr_debug_log(const char *function_name, const char *message) const {
|
|
|
17
16
|
}
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
Logger::stderr_debug_log(const char *function_name,
|
|
19
|
+
void Logger::stderr_debug_log(const char *function_name,
|
|
22
20
|
const std::string &message) const {
|
|
23
21
|
if (unlikely(verbose_flag)) {
|
|
24
22
|
std::cerr << system_name << ": [DEBUG] " << function_name << ": " << message
|
|
@@ -27,7 +25,7 @@ Logger::stderr_debug_log(const char *function_name,
|
|
|
27
25
|
}
|
|
28
26
|
}
|
|
29
27
|
|
|
30
|
-
|
|
28
|
+
void Logger::stderr_debug_log_alloca(
|
|
31
29
|
const char *function_name, const char *variable_name, size_t length) const {
|
|
32
30
|
if (unlikely(verbose_flag)) {
|
|
33
31
|
std::cerr << system_name << ": [DEBUG] " << function_name
|
|
@@ -37,8 +35,7 @@ __attribute__((always_inline)) inline void Logger::stderr_debug_log_alloca(
|
|
|
37
35
|
}
|
|
38
36
|
}
|
|
39
37
|
|
|
40
|
-
|
|
41
|
-
Logger::stderr_debug_log_new(const char *function_name,
|
|
38
|
+
void Logger::stderr_debug_log_new(const char *function_name,
|
|
42
39
|
const char *variable_name, size_t length) const {
|
|
43
40
|
if (unlikely(verbose_flag)) {
|
|
44
41
|
std::cerr << system_name << ": [DEBUG] " << function_name
|
|
@@ -48,15 +45,13 @@ Logger::stderr_debug_log_new(const char *function_name,
|
|
|
48
45
|
}
|
|
49
46
|
}
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
Logger::stderr_error_log(const char *function_name, const char *message) const {
|
|
48
|
+
void Logger::stderr_error_log(const char *function_name, const char *message) const {
|
|
53
49
|
std::cerr << system_name << ": [ERROR] " << function_name << ": " << message
|
|
54
50
|
<< std::endl
|
|
55
51
|
<< std::flush;
|
|
56
52
|
}
|
|
57
53
|
|
|
58
|
-
|
|
59
|
-
Logger::stderr_error_log(const char *function_name,
|
|
54
|
+
void Logger::stderr_error_log(const char *function_name,
|
|
60
55
|
const std::string &message) const {
|
|
61
56
|
std::cerr << system_name << ": [ERROR] " << function_name << ": " << message
|
|
62
57
|
<< std::endl
|
package/src/logging.h
CHANGED
|
@@ -25,9 +25,6 @@ public:
|
|
|
25
25
|
const char *message) const = 0;
|
|
26
26
|
virtual void error_log(const char *function_name,
|
|
27
27
|
const std::string &message) const = 0;
|
|
28
|
-
[[noreturn]] virtual void
|
|
29
|
-
log_error_and_throw(const char *function_name,
|
|
30
|
-
const std::string &error_msg) const = 0;
|
|
31
28
|
|
|
32
29
|
__attribute__((always_inline)) inline static std::string
|
|
33
30
|
format_ptr(const void *ptr) {
|
|
@@ -37,9 +34,9 @@ public:
|
|
|
37
34
|
}
|
|
38
35
|
|
|
39
36
|
protected:
|
|
40
|
-
Logger(std::string system_name);
|
|
37
|
+
explicit Logger(const std::string &system_name);
|
|
41
38
|
|
|
42
|
-
bool verbose_flag =
|
|
39
|
+
bool verbose_flag = false;
|
|
43
40
|
std::string system_name;
|
|
44
41
|
|
|
45
42
|
void stderr_debug_log(const char *function_name, const char *message) const;
|
package/src/logging_napi.cc
CHANGED
|
@@ -8,7 +8,7 @@ LoggerNapi::LoggerNapi(Napi::Env &env, std::string system_name,
|
|
|
8
8
|
Napi::Function new_log_hook)
|
|
9
9
|
: Logger(system_name), env(env) {
|
|
10
10
|
if (unlikely(new_log_hook.IsEmpty())) {
|
|
11
|
-
NapiUtils::ThrowException(env, "new_log_hook cannot be nullptr");
|
|
11
|
+
NapiUtils::ThrowException(env, system_name + ": new_log_hook cannot be nullptr");
|
|
12
12
|
}
|
|
13
13
|
log_hook = Napi::Persistent(new_log_hook);
|
|
14
14
|
}
|
|
@@ -22,7 +22,7 @@ LoggerNapi::~LoggerNapi() {
|
|
|
22
22
|
|
|
23
23
|
void LoggerNapi::set_log_hook(Napi::Function new_log_hook) {
|
|
24
24
|
if (unlikely(new_log_hook.IsUndefined() || new_log_hook.IsEmpty())) {
|
|
25
|
-
NapiUtils::ThrowException(env, "new_log_hook cannot be empty");
|
|
25
|
+
NapiUtils::ThrowException(env, system_name + ": new_log_hook cannot be empty");
|
|
26
26
|
}
|
|
27
27
|
auto old_log_hook = std::exchange(log_hook, Napi::Persistent(new_log_hook));
|
|
28
28
|
if (!old_log_hook.IsEmpty()) {
|
|
@@ -30,19 +30,23 @@ void LoggerNapi::set_log_hook(Napi::Function new_log_hook) {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
void LoggerNapi::call_log_hook(int level, const std::string &message) const {
|
|
34
|
+
if (unlikely(log_hook.IsEmpty())) {
|
|
35
|
+
NapiUtils::ThrowException(env, system_name + ": log_hook cannot be empty");
|
|
36
|
+
}
|
|
37
|
+
Napi::HandleScope scope(env);
|
|
38
|
+
Napi::Function log_hook_function = log_hook.Value();
|
|
39
|
+
log_hook_function.Call({Napi::Number::New(env, level),
|
|
40
|
+
Napi::String::New(env, system_name + ": " + message)});
|
|
41
|
+
}
|
|
42
|
+
|
|
33
43
|
void LoggerNapi::debug_log(const char *function_name,
|
|
34
44
|
const char *message) const {
|
|
35
45
|
if (unlikely(verbose_flag)) {
|
|
36
46
|
if (unlikely(log_hook.IsEmpty())) {
|
|
37
47
|
stderr_debug_log(function_name, message);
|
|
38
48
|
} else {
|
|
39
|
-
|
|
40
|
-
Napi::HandleScope scope(env);
|
|
41
|
-
Napi::Function log_hook_function = log_hook.Value();
|
|
42
|
-
log_hook_function.Call(
|
|
43
|
-
{Napi::Number::New(env, posix_log_level_debug),
|
|
44
|
-
Napi::String::New(env,
|
|
45
|
-
system_name + function_name + ": " + message)});
|
|
49
|
+
call_log_hook(posix_log_level_debug, system_name + ": " + function_name + ": " + message);
|
|
46
50
|
}
|
|
47
51
|
}
|
|
48
52
|
}
|
|
@@ -53,13 +57,7 @@ void LoggerNapi::debug_log(const char *function_name,
|
|
|
53
57
|
if (unlikely(log_hook.IsEmpty())) {
|
|
54
58
|
stderr_debug_log(function_name, message);
|
|
55
59
|
} else {
|
|
56
|
-
|
|
57
|
-
Napi::HandleScope scope(env);
|
|
58
|
-
Napi::Function log_hook_function = log_hook.Value();
|
|
59
|
-
log_hook_function.Call(
|
|
60
|
-
{Napi::Number::New(env, posix_log_level_debug),
|
|
61
|
-
Napi::String::New(env,
|
|
62
|
-
system_name + function_name + ": " + message)});
|
|
60
|
+
call_log_hook(posix_log_level_debug, system_name + ": " + function_name + ": " + message);
|
|
63
61
|
}
|
|
64
62
|
}
|
|
65
63
|
}
|
|
@@ -71,15 +69,10 @@ void LoggerNapi::debug_log_alloca(const char *function_name,
|
|
|
71
69
|
if (unlikely(log_hook.IsEmpty())) {
|
|
72
70
|
stderr_debug_log_alloca(function_name, variable_name, length);
|
|
73
71
|
} else {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
{Napi::Number::New(env, posix_log_level_debug),
|
|
79
|
-
Napi::String::New(env, system_name + function_name +
|
|
80
|
-
": Calling alloca(" +
|
|
81
|
-
std::to_string(length) +
|
|
82
|
-
") (stack) for " + variable_name)});
|
|
72
|
+
call_log_hook(posix_log_level_debug, system_name + ": " + function_name +
|
|
73
|
+
": Calling alloca(" +
|
|
74
|
+
std::to_string(length) +
|
|
75
|
+
") (stack) for " + variable_name);
|
|
83
76
|
}
|
|
84
77
|
}
|
|
85
78
|
}
|
|
@@ -90,62 +83,24 @@ void LoggerNapi::debug_log_new(const char *function_name,
|
|
|
90
83
|
if (unlikely(log_hook.IsEmpty())) {
|
|
91
84
|
stderr_debug_log_new(function_name, variable_name, length);
|
|
92
85
|
} else {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
{Napi::Number::New(env, posix_log_level_debug),
|
|
98
|
-
Napi::String::New(env, system_name + function_name +
|
|
99
|
-
": Calling new[" +
|
|
100
|
-
std::to_string(length) + "] (heap) for " +
|
|
101
|
-
variable_name)});
|
|
86
|
+
call_log_hook(posix_log_level_debug, system_name + ": " + function_name +
|
|
87
|
+
": Calling new[" +
|
|
88
|
+
std::to_string(length) + "] (heap) for " +
|
|
89
|
+
variable_name);
|
|
102
90
|
}
|
|
103
91
|
}
|
|
104
92
|
}
|
|
105
93
|
|
|
106
94
|
void LoggerNapi::error_log(const char *function_name,
|
|
107
95
|
const char *message) const {
|
|
108
|
-
if (
|
|
109
|
-
|
|
110
|
-
stderr_error_log(function_name, message);
|
|
111
|
-
} else {
|
|
112
|
-
stderr_error_log(function_name, message);
|
|
113
|
-
Napi::Env env = log_hook.Env();
|
|
114
|
-
Napi::HandleScope scope(env);
|
|
115
|
-
Napi::Function log_hook_function = log_hook.Value();
|
|
116
|
-
log_hook_function.Call(
|
|
117
|
-
{Napi::Number::New(env, posix_log_level_error),
|
|
118
|
-
Napi::String::New(env,
|
|
119
|
-
system_name + function_name + ": " + message)});
|
|
120
|
-
}
|
|
96
|
+
if (likely(!log_hook.IsEmpty())) {
|
|
97
|
+
call_log_hook(posix_log_level_error, system_name + ": " + function_name + ": " + message);
|
|
121
98
|
}
|
|
122
99
|
}
|
|
123
100
|
|
|
124
101
|
void LoggerNapi::error_log(const char *function_name,
|
|
125
102
|
const std::string &message) const {
|
|
126
|
-
// Unconditionally log errors to stderr
|
|
127
|
-
stderr_error_log(function_name, message);
|
|
128
|
-
|
|
129
103
|
if (likely(!log_hook.IsEmpty())) {
|
|
130
|
-
|
|
131
|
-
Napi::HandleScope scope(env);
|
|
132
|
-
Napi::Function log_hook_function = log_hook.Value();
|
|
133
|
-
log_hook_function.Call(
|
|
134
|
-
{Napi::Number::New(env, posix_log_level_error),
|
|
135
|
-
Napi::String::New(env, system_name + function_name + ": " + message)});
|
|
104
|
+
call_log_hook(posix_log_level_error, system_name + ": " + function_name + ": " + message);
|
|
136
105
|
}
|
|
137
106
|
}
|
|
138
|
-
|
|
139
|
-
__attribute__((always_inline, noreturn)) inline void
|
|
140
|
-
LoggerNapi::log_error_and_throw(const char *function_name,
|
|
141
|
-
const std::string &error_msg) const {
|
|
142
|
-
std::string final_error_msg =
|
|
143
|
-
system_name + ": [EXCEPTION] " + function_name + (": " + error_msg);
|
|
144
|
-
|
|
145
|
-
// Unconditionally log errors to stderr
|
|
146
|
-
stderr_error_log(function_name, final_error_msg);
|
|
147
|
-
|
|
148
|
-
error_log(function_name, final_error_msg);
|
|
149
|
-
|
|
150
|
-
NapiUtils::ThrowException(env, final_error_msg);
|
|
151
|
-
}
|
package/src/logging_napi.h
CHANGED
|
@@ -29,11 +29,9 @@ public:
|
|
|
29
29
|
void error_log(const char *function_name, const char *message) const override;
|
|
30
30
|
void error_log(const char *function_name,
|
|
31
31
|
const std::string &message) const override;
|
|
32
|
-
[[noreturn]] void
|
|
33
|
-
log_error_and_throw(const char *function_name,
|
|
34
|
-
const std::string &error_msg) const override;
|
|
35
32
|
|
|
36
33
|
private:
|
|
34
|
+
void call_log_hook(int level, const std::string &message) const;
|
|
37
35
|
Napi::FunctionReference log_hook;
|
|
38
36
|
Napi::Env env;
|
|
39
37
|
const int posix_log_level_error = 3;
|
package/src/napi_utils.h
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
#include "hints.h"
|
|
5
5
|
#include <napi.h>
|
|
6
6
|
#include <stdexcept>
|
|
7
|
-
|
|
7
|
+
#include <string>
|
|
8
8
|
#ifndef NAPI_CPP_EXCEPTIONS
|
|
9
9
|
#error Support for C++ exceptions is required
|
|
10
10
|
#endif
|
|
@@ -26,10 +26,8 @@ public:
|
|
|
26
26
|
|
|
27
27
|
[[noreturn]] static void ThrowException(const Napi::Env &env,
|
|
28
28
|
const std::string &message) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
// If the JavaScript exception isn't thrown, fallback to a C++ exception
|
|
32
|
-
throw error;
|
|
29
|
+
//throw std::runtime_error(message);
|
|
30
|
+
throw Napi::Error::New(env, message);
|
|
33
31
|
}
|
|
34
32
|
|
|
35
33
|
static void AsJsonObjectAndString(const Napi::Env &env,
|
|
@@ -116,25 +114,25 @@ public:
|
|
|
116
114
|
if (likely(value.IsString())) {
|
|
117
115
|
return value.As<Napi::String>();
|
|
118
116
|
} else if (unlikely(value.IsUndefined())) {
|
|
119
|
-
ThrowException(env, "Expected String but received undefined");
|
|
117
|
+
ThrowException(env, std::string(func_name) + ": Expected String but received undefined");
|
|
120
118
|
} else if (unlikely(value.IsNull())) {
|
|
121
|
-
ThrowException(env, "Expected String but received null");
|
|
119
|
+
ThrowException(env, std::string(func_name) + ": Expected String but received null");
|
|
122
120
|
} else {
|
|
123
|
-
ThrowException(env, "Expected String but received unknown type");
|
|
121
|
+
ThrowException(env, std::string(func_name) + ": Expected String but received unknown type");
|
|
124
122
|
}
|
|
125
123
|
}
|
|
126
124
|
|
|
127
|
-
|
|
125
|
+
__attribute__((unused)) static Napi::Buffer<unsigned char>
|
|
128
126
|
RequireParameterBuffer(const Napi::Env &env, const char *func_name,
|
|
129
127
|
Napi::Value value) {
|
|
130
128
|
if (likely(value.IsBuffer())) {
|
|
131
129
|
return value.As<Napi::Buffer<unsigned char>>();
|
|
132
130
|
} else if (unlikely(value.IsUndefined())) {
|
|
133
|
-
ThrowException(env, "Expected String but received undefined");
|
|
131
|
+
ThrowException(env, std::string(func_name) + ": Expected String but received undefined");
|
|
134
132
|
} else if (unlikely(value.IsNull())) {
|
|
135
|
-
ThrowException(env, "Expected String but received null");
|
|
133
|
+
ThrowException(env, std::string(func_name) + ": Expected String but received null");
|
|
136
134
|
} else {
|
|
137
|
-
ThrowException(env, "Expected String but received unknown type");
|
|
135
|
+
ThrowException(env, std::string(func_name) + ": Expected String but received unknown type");
|
|
138
136
|
}
|
|
139
137
|
}
|
|
140
138
|
|
|
@@ -144,13 +142,13 @@ public:
|
|
|
144
142
|
if (value.IsString()) {
|
|
145
143
|
return value.As<Napi::String>();
|
|
146
144
|
} else if (likely(value.IsBuffer())) {
|
|
147
|
-
return value.As<Napi::Buffer<unsigned char>>();
|
|
145
|
+
return value.As<Napi::Buffer<unsigned char>>(); // NOLINT(*-slicing)
|
|
148
146
|
} else if (unlikely(value.IsUndefined())) {
|
|
149
|
-
ThrowException(env, "Expected String but received undefined");
|
|
147
|
+
ThrowException(env, std::string(func_name) + ": Expected String but received undefined");
|
|
150
148
|
} else if (unlikely(value.IsNull())) {
|
|
151
|
-
ThrowException(env, "Expected String but received null");
|
|
149
|
+
ThrowException(env, std::string(func_name) + ": Expected String but received null");
|
|
152
150
|
} else {
|
|
153
|
-
ThrowException(env, "Expected String but received unknown type");
|
|
151
|
+
ThrowException(env, std::string(func_name) + ": Expected String but received unknown type");
|
|
154
152
|
}
|
|
155
153
|
}
|
|
156
154
|
|
package/src/scoped_allocate.h
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
#ifndef SCOPED_ALLOCATE_H
|
|
2
2
|
#define SCOPED_ALLOCATE_H
|
|
3
3
|
|
|
4
|
-
#include "logging.h"
|
|
5
|
-
|
|
6
4
|
/*
|
|
7
5
|
This macro allows us to allocate a buffer either on the stack or on the heap.
|
|
8
6
|
If the requested buffer size is less than max_stack_alloc_size, we create the
|
|
@@ -24,23 +22,20 @@
|
|
|
24
22
|
/* If the buffer is small enough, allocate it on the stack */ \
|
|
25
23
|
logger.debug_log_alloca(function_name, #buffer, buffer_size); \
|
|
26
24
|
buffer = (char *)alloca(buffer_size); \
|
|
25
|
+
throw std::runtime_error("alloca(" + std::to_string(buffer_size) + \
|
|
26
|
+
") returned null"); \
|
|
27
27
|
} else { \
|
|
28
28
|
/* Otherwise, allocate it on the heap */ \
|
|
29
29
|
logger.debug_log_new(function_name, #buffer, buffer_size); \
|
|
30
30
|
buffer = new (std::nothrow) char[buffer_size]; \
|
|
31
31
|
if (unlikely(buffer == nullptr)) { \
|
|
32
|
-
std::string error_msg =
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
std::string error_msg = std::string(function_name) + "new[" + \
|
|
33
|
+
std::to_string(buffer_size) + \
|
|
34
|
+
"] returned null"; \
|
|
35
|
+
throw std::runtime_error(error_msg); \
|
|
36
36
|
} \
|
|
37
37
|
unique_ptr.reset(buffer); \
|
|
38
38
|
} \
|
|
39
|
-
if (unlikely(buffer == nullptr)) { \
|
|
40
|
-
logger.log_error_and_throw(function_name, \
|
|
41
|
-
"Failed to allocate " #buffer); \
|
|
42
|
-
break; \
|
|
43
|
-
} \
|
|
44
39
|
} while (0)
|
|
45
40
|
|
|
46
41
|
#define SCOPED_ALLOCATE_BUFFER(logger, buffer, buffer_size, \
|