Skip to content

Commit 53d6465

Browse files
addaleaxjasnell
authored andcommitted
src: protect global state with mutexes
Original PR: ayojs/ayo#82 > PR-URL: ayojs/ayo#82 > Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent a4e8e9e commit 53d6465

7 files changed

+21
-18
lines changed

node.gyp

-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@
223223
'src/signal_wrap.cc',
224224
'src/spawn_sync.cc',
225225
'src/string_bytes.cc',
226-
'src/string_search.cc',
227226
'src/stream_base.cc',
228227
'src/stream_wrap.cc',
229228
'src/tcp_wrap.cc',
@@ -685,7 +684,6 @@
685684
'<(OBJ_PATH)<(OBJ_SEPARATOR)node_url.<(OBJ_SUFFIX)',
686685
'<(OBJ_PATH)<(OBJ_SEPARATOR)util.<(OBJ_SUFFIX)',
687686
'<(OBJ_PATH)<(OBJ_SEPARATOR)string_bytes.<(OBJ_SUFFIX)',
688-
'<(OBJ_PATH)<(OBJ_SEPARATOR)string_search.<(OBJ_SUFFIX)',
689687
'<(OBJ_PATH)<(OBJ_SEPARATOR)stream_base.<(OBJ_SUFFIX)',
690688
'<(OBJ_PATH)<(OBJ_SEPARATOR)node_constants.<(OBJ_SUFFIX)',
691689
'<(OBJ_TRACING_PATH)<(OBJ_SEPARATOR)agent.<(OBJ_SUFFIX)',

src/node.cc

+13
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ using v8::Value;
167167

168168
using AsyncHooks = node::Environment::AsyncHooks;
169169

170+
static Mutex process_mutex;
171+
static Mutex environ_mutex;
172+
170173
static bool print_eval = false;
171174
static bool force_repl = false;
172175
static bool syntax_check_only = false;
@@ -1789,6 +1792,7 @@ void AppendExceptionLine(Environment* env,
17891792
if (!can_set_arrow || (mode == FATAL_ERROR && !err_obj->IsNativeError())) {
17901793
if (env->printed_error())
17911794
return;
1795+
Mutex::ScopedLock lock(process_mutex);
17921796
env->set_printed_error(true);
17931797

17941798
uv_tty_reset_mode();
@@ -2952,6 +2956,7 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
29522956

29532957
static void ProcessTitleGetter(Local<Name> property,
29542958
const PropertyCallbackInfo<Value>& info) {
2959+
Mutex::ScopedLock lock(process_mutex);
29552960
char buffer[512];
29562961
uv_get_process_title(buffer, sizeof(buffer));
29572962
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buffer));
@@ -2961,6 +2966,7 @@ static void ProcessTitleGetter(Local<Name> property,
29612966
static void ProcessTitleSetter(Local<Name> property,
29622967
Local<Value> value,
29632968
const PropertyCallbackInfo<void>& info) {
2969+
Mutex::ScopedLock lock(process_mutex);
29642970
node::Utf8Value title(info.GetIsolate(), value);
29652971
// TODO(piscisaureus): protect with a lock
29662972
uv_set_process_title(*title);
@@ -2969,6 +2975,7 @@ static void ProcessTitleSetter(Local<Name> property,
29692975

29702976
static void EnvGetter(Local<Name> property,
29712977
const PropertyCallbackInfo<Value>& info) {
2978+
Mutex::ScopedLock lock(environ_mutex);
29722979
Isolate* isolate = info.GetIsolate();
29732980
if (property->IsSymbol()) {
29742981
return info.GetReturnValue().SetUndefined();
@@ -3001,6 +3008,7 @@ static void EnvGetter(Local<Name> property,
30013008
static void EnvSetter(Local<Name> property,
30023009
Local<Value> value,
30033010
const PropertyCallbackInfo<Value>& info) {
3011+
Mutex::ScopedLock lock(environ_mutex);
30043012
#ifdef __POSIX__
30053013
node::Utf8Value key(info.GetIsolate(), property);
30063014
node::Utf8Value val(info.GetIsolate(), value);
@@ -3021,6 +3029,7 @@ static void EnvSetter(Local<Name> property,
30213029

30223030
static void EnvQuery(Local<Name> property,
30233031
const PropertyCallbackInfo<Integer>& info) {
3032+
Mutex::ScopedLock lock(environ_mutex);
30243033
int32_t rc = -1; // Not found unless proven otherwise.
30253034
if (property->IsString()) {
30263035
#ifdef __POSIX__
@@ -3049,6 +3058,7 @@ static void EnvQuery(Local<Name> property,
30493058

30503059
static void EnvDeleter(Local<Name> property,
30513060
const PropertyCallbackInfo<Boolean>& info) {
3061+
Mutex::ScopedLock lock(environ_mutex);
30523062
if (property->IsString()) {
30533063
#ifdef __POSIX__
30543064
node::Utf8Value key(info.GetIsolate(), property);
@@ -3067,6 +3077,7 @@ static void EnvDeleter(Local<Name> property,
30673077

30683078

30693079
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
3080+
Mutex::ScopedLock lock(environ_mutex);
30703081
Environment* env = Environment::GetCurrent(info);
30713082
Isolate* isolate = env->isolate();
30723083
Local<Context> ctx = env->context();
@@ -3190,6 +3201,7 @@ static Local<Object> GetFeatures(Environment* env) {
31903201

31913202
static void DebugPortGetter(Local<Name> property,
31923203
const PropertyCallbackInfo<Value>& info) {
3204+
Mutex::ScopedLock lock(process_mutex);
31933205
int port = debug_options.port();
31943206
#if HAVE_INSPECTOR
31953207
if (port == 0) {
@@ -3205,6 +3217,7 @@ static void DebugPortGetter(Local<Name> property,
32053217
static void DebugPortSetter(Local<Name> property,
32063218
Local<Value> value,
32073219
const PropertyCallbackInfo<void>& info) {
3220+
Mutex::ScopedLock lock(process_mutex);
32083221
debug_options.set_port(value->Int32Value());
32093222
}
32103223

src/node_crypto.cc

+3
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,9 @@ static int X509_up_ref(X509* cert) {
788788

789789
static X509_STORE* NewRootCertStore() {
790790
static std::vector<X509*> root_certs_vector;
791+
static Mutex root_certs_vector_mutex;
792+
Mutex::ScopedLock lock(root_certs_vector_mutex);
793+
791794
if (root_certs_vector.empty()) {
792795
for (size_t i = 0; i < arraysize(root_certs); i++) {
793796
BIO* bp = NodeBIO::NewFixed(root_certs[i], strlen(root_certs[i]));

src/node_http2.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Freelist<nghttp2_header_list, FREELIST_MAX> header_free_list;
2828
Freelist<nghttp2_data_chunks_t, FREELIST_MAX>
2929
data_chunks_free_list;
3030

31-
Nghttp2Session::Callbacks Nghttp2Session::callback_struct_saved[2] = {
31+
const Nghttp2Session::Callbacks Nghttp2Session::callback_struct_saved[2] = {
3232
Callbacks(false),
3333
Callbacks(true)};
3434

src/node_http2_core.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class Nghttp2Session {
279279
};
280280

281281
/* Use callback_struct_saved[kHasGetPaddingCallback ? 1 : 0] */
282-
static Callbacks callback_struct_saved[2];
282+
static const Callbacks callback_struct_saved[2];
283283

284284
nghttp2_session* session_;
285285
uv_loop_t* loop_;

src/string_search.cc

-11
This file was deleted.

src/string_search.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ class StringSearchBase {
8585
static const int kBMMinPatternLength = 8;
8686

8787
// Store for the BoyerMoore(Horspool) bad char shift table.
88-
static int kBadCharShiftTable[kUC16AlphabetSize];
88+
int kBadCharShiftTable[kUC16AlphabetSize];
8989
// Store for the BoyerMoore good suffix shift table.
90-
static int kGoodSuffixShiftTable[kBMMaxShift + 1];
90+
int kGoodSuffixShiftTable[kBMMaxShift + 1];
9191
// Table used temporarily while building the BoyerMoore good suffix
9292
// shift table.
93-
static int kSuffixTable[kBMMaxShift + 1];
93+
int kSuffixTable[kBMMaxShift + 1];
9494
};
9595

9696
template <typename Char>

0 commit comments

Comments
 (0)