Skip to content

Commit 71f514d

Browse files
committed
src: protect global state with mutexes
PR-URL: ayojs#82
1 parent 5c6aa32 commit 71f514d

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',
@@ -687,7 +686,6 @@
687686
'<(OBJ_PATH)<(OBJ_SEPARATOR)node_url.<(OBJ_SUFFIX)',
688687
'<(OBJ_PATH)<(OBJ_SEPARATOR)util.<(OBJ_SUFFIX)',
689688
'<(OBJ_PATH)<(OBJ_SEPARATOR)string_bytes.<(OBJ_SUFFIX)',
690-
'<(OBJ_PATH)<(OBJ_SEPARATOR)string_search.<(OBJ_SUFFIX)',
691689
'<(OBJ_PATH)<(OBJ_SEPARATOR)stream_base.<(OBJ_SUFFIX)',
692690
'<(OBJ_PATH)<(OBJ_SEPARATOR)node_constants.<(OBJ_SUFFIX)',
693691
'<(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;
@@ -1792,6 +1795,7 @@ void AppendExceptionLine(Environment* env,
17921795
if (!can_set_arrow || (mode == FATAL_ERROR && !err_obj->IsNativeError())) {
17931796
if (env->printed_error())
17941797
return;
1798+
Mutex::ScopedLock lock(process_mutex);
17951799
env->set_printed_error(true);
17961800

17971801
uv_tty_reset_mode();
@@ -2955,6 +2959,7 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
29552959

29562960
static void ProcessTitleGetter(Local<Name> property,
29572961
const PropertyCallbackInfo<Value>& info) {
2962+
Mutex::ScopedLock lock(process_mutex);
29582963
char buffer[512];
29592964
uv_get_process_title(buffer, sizeof(buffer));
29602965
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buffer));
@@ -2964,6 +2969,7 @@ static void ProcessTitleGetter(Local<Name> property,
29642969
static void ProcessTitleSetter(Local<Name> property,
29652970
Local<Value> value,
29662971
const PropertyCallbackInfo<void>& info) {
2972+
Mutex::ScopedLock lock(process_mutex);
29672973
node::Utf8Value title(info.GetIsolate(), value);
29682974
// TODO(piscisaureus): protect with a lock
29692975
uv_set_process_title(*title);
@@ -2972,6 +2978,7 @@ static void ProcessTitleSetter(Local<Name> property,
29722978

29732979
static void EnvGetter(Local<Name> property,
29742980
const PropertyCallbackInfo<Value>& info) {
2981+
Mutex::ScopedLock lock(environ_mutex);
29752982
Isolate* isolate = info.GetIsolate();
29762983
if (property->IsSymbol()) {
29772984
return info.GetReturnValue().SetUndefined();
@@ -3004,6 +3011,7 @@ static void EnvGetter(Local<Name> property,
30043011
static void EnvSetter(Local<Name> property,
30053012
Local<Value> value,
30063013
const PropertyCallbackInfo<Value>& info) {
3014+
Mutex::ScopedLock lock(environ_mutex);
30073015
#ifdef __POSIX__
30083016
node::Utf8Value key(info.GetIsolate(), property);
30093017
node::Utf8Value val(info.GetIsolate(), value);
@@ -3024,6 +3032,7 @@ static void EnvSetter(Local<Name> property,
30243032

30253033
static void EnvQuery(Local<Name> property,
30263034
const PropertyCallbackInfo<Integer>& info) {
3035+
Mutex::ScopedLock lock(environ_mutex);
30273036
int32_t rc = -1; // Not found unless proven otherwise.
30283037
if (property->IsString()) {
30293038
#ifdef __POSIX__
@@ -3052,6 +3061,7 @@ static void EnvQuery(Local<Name> property,
30523061

30533062
static void EnvDeleter(Local<Name> property,
30543063
const PropertyCallbackInfo<Boolean>& info) {
3064+
Mutex::ScopedLock lock(environ_mutex);
30553065
if (property->IsString()) {
30563066
#ifdef __POSIX__
30573067
node::Utf8Value key(info.GetIsolate(), property);
@@ -3070,6 +3080,7 @@ static void EnvDeleter(Local<Name> property,
30703080

30713081

30723082
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
3083+
Mutex::ScopedLock lock(environ_mutex);
30733084
Environment* env = Environment::GetCurrent(info);
30743085
Isolate* isolate = env->isolate();
30753086
Local<Context> ctx = env->context();
@@ -3193,6 +3204,7 @@ static Local<Object> GetFeatures(Environment* env) {
31933204

31943205
static void DebugPortGetter(Local<Name> property,
31953206
const PropertyCallbackInfo<Value>& info) {
3207+
Mutex::ScopedLock lock(process_mutex);
31963208
int port = debug_options.port();
31973209
#if HAVE_INSPECTOR
31983210
if (port == 0) {
@@ -3208,6 +3220,7 @@ static void DebugPortGetter(Local<Name> property,
32083220
static void DebugPortSetter(Local<Name> property,
32093221
Local<Value> value,
32103222
const PropertyCallbackInfo<void>& info) {
3223+
Mutex::ScopedLock lock(process_mutex);
32113224
debug_options.set_port(value->Int32Value());
32123225
}
32133226

src/node_crypto.cc

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

737737
static X509_STORE* NewRootCertStore() {
738738
static std::vector<X509*> root_certs_vector;
739+
static Mutex root_certs_vector_mutex;
740+
Mutex::ScopedLock lock(root_certs_vector_mutex);
741+
739742
if (root_certs_vector.empty()) {
740743
for (size_t i = 0; i < arraysize(root_certs); i++) {
741744
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)