Skip to content

Commit

Permalink
src: protect global state with mutexes
Browse files Browse the repository at this point in the history
PR-URL: ayojs#58
  • Loading branch information
addaleax committed Sep 20, 2017
1 parent cc0ce96 commit c32775c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
2 changes: 0 additions & 2 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@
'src/signal_wrap.cc',
'src/spawn_sync.cc',
'src/string_bytes.cc',
'src/string_search.cc',
'src/stream_base.cc',
'src/stream_wrap.cc',
'src/tcp_wrap.cc',
Expand Down Expand Up @@ -679,7 +678,6 @@
'<(OBJ_PATH)<(OBJ_SEPARATOR)node_url.<(OBJ_SUFFIX)',
'<(OBJ_PATH)<(OBJ_SEPARATOR)util.<(OBJ_SUFFIX)',
'<(OBJ_PATH)<(OBJ_SEPARATOR)string_bytes.<(OBJ_SUFFIX)',
'<(OBJ_PATH)<(OBJ_SEPARATOR)string_search.<(OBJ_SUFFIX)',
'<(OBJ_PATH)<(OBJ_SEPARATOR)stream_base.<(OBJ_SUFFIX)',
'<(OBJ_PATH)<(OBJ_SEPARATOR)node_constants.<(OBJ_SUFFIX)',
'<(OBJ_TRACING_PATH)<(OBJ_SEPARATOR)agent.<(OBJ_SUFFIX)',
Expand Down
13 changes: 13 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ using v8::Value;

using AsyncHooks = node::Environment::AsyncHooks;

static Mutex process_mutex;
static Mutex environ_mutex;

static bool print_eval = false;
static bool force_repl = false;
static bool syntax_check_only = false;
Expand Down Expand Up @@ -1839,6 +1842,7 @@ void AppendExceptionLine(Environment* env,
if (!can_set_arrow || (mode == FATAL_ERROR && !err_obj->IsNativeError())) {
if (env->printed_error())
return;
Mutex::ScopedLock lock(process_mutex);
env->set_printed_error(true);

uv_tty_reset_mode();
Expand Down Expand Up @@ -3007,6 +3011,7 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {

static void ProcessTitleGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Mutex::ScopedLock lock(process_mutex);
char buffer[512];
uv_get_process_title(buffer, sizeof(buffer));
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buffer));
Expand All @@ -3016,6 +3021,7 @@ static void ProcessTitleGetter(Local<Name> property,
static void ProcessTitleSetter(Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<void>& info) {
Mutex::ScopedLock lock(process_mutex);
node::Utf8Value title(info.GetIsolate(), value);
// TODO(piscisaureus): protect with a lock
uv_set_process_title(*title);
Expand All @@ -3024,6 +3030,7 @@ static void ProcessTitleSetter(Local<Name> property,

static void EnvGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Mutex::ScopedLock lock(environ_mutex);
Isolate* isolate = info.GetIsolate();
if (property->IsSymbol()) {
return info.GetReturnValue().SetUndefined();
Expand Down Expand Up @@ -3056,6 +3063,7 @@ static void EnvGetter(Local<Name> property,
static void EnvSetter(Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<Value>& info) {
Mutex::ScopedLock lock(environ_mutex);
#ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property);
node::Utf8Value val(info.GetIsolate(), value);
Expand All @@ -3076,6 +3084,7 @@ static void EnvSetter(Local<Name> property,

static void EnvQuery(Local<Name> property,
const PropertyCallbackInfo<Integer>& info) {
Mutex::ScopedLock lock(environ_mutex);
int32_t rc = -1; // Not found unless proven otherwise.
if (property->IsString()) {
#ifdef __POSIX__
Expand Down Expand Up @@ -3104,6 +3113,7 @@ static void EnvQuery(Local<Name> property,

static void EnvDeleter(Local<Name> property,
const PropertyCallbackInfo<Boolean>& info) {
Mutex::ScopedLock lock(environ_mutex);
if (property->IsString()) {
#ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property);
Expand All @@ -3122,6 +3132,7 @@ static void EnvDeleter(Local<Name> property,


static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
Mutex::ScopedLock lock(environ_mutex);
Environment* env = Environment::GetCurrent(info);
Isolate* isolate = env->isolate();
Local<Context> ctx = env->context();
Expand Down Expand Up @@ -3245,6 +3256,7 @@ static Local<Object> GetFeatures(Environment* env) {

static void DebugPortGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Mutex::ScopedLock lock(process_mutex);
int port = debug_options.port();
#if HAVE_INSPECTOR
if (port == 0) {
Expand All @@ -3260,6 +3272,7 @@ static void DebugPortGetter(Local<Name> property,
static void DebugPortSetter(Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<void>& info) {
Mutex::ScopedLock lock(process_mutex);
debug_options.set_port(value->Int32Value());
}

Expand Down
3 changes: 3 additions & 0 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,9 @@ static int X509_up_ref(X509* cert) {

static X509_STORE* NewRootCertStore() {
static std::vector<X509*> root_certs_vector;
static Mutex root_certs_vector_mutex;
Mutex::ScopedLock lock(root_certs_vector_mutex);

if (root_certs_vector.empty()) {
for (size_t i = 0; i < arraysize(root_certs); i++) {
BIO* bp = NodeBIO::NewFixed(root_certs[i], strlen(root_certs[i]));
Expand Down
2 changes: 1 addition & 1 deletion src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Freelist<nghttp2_header_list, FREELIST_MAX> header_free_list;
Freelist<nghttp2_data_chunks_t, FREELIST_MAX>
data_chunks_free_list;

Nghttp2Session::Callbacks Nghttp2Session::callback_struct_saved[2] = {
const Nghttp2Session::Callbacks Nghttp2Session::callback_struct_saved[2] = {
Callbacks(false),
Callbacks(true)};

Expand Down
2 changes: 1 addition & 1 deletion src/node_http2_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class Nghttp2Session {
};

/* Use callback_struct_saved[kHasGetPaddingCallback ? 1 : 0] */
static Callbacks callback_struct_saved[2];
static const Callbacks callback_struct_saved[2];

nghttp2_session* session_;
uv_loop_t* loop_;
Expand Down
11 changes: 0 additions & 11 deletions src/string_search.cc

This file was deleted.

6 changes: 3 additions & 3 deletions src/string_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ class StringSearchBase {
static const int kBMMinPatternLength = 8;

// Store for the BoyerMoore(Horspool) bad char shift table.
static int kBadCharShiftTable[kUC16AlphabetSize];
int kBadCharShiftTable[kUC16AlphabetSize];
// Store for the BoyerMoore good suffix shift table.
static int kGoodSuffixShiftTable[kBMMaxShift + 1];
int kGoodSuffixShiftTable[kBMMaxShift + 1];
// Table used temporarily while building the BoyerMoore good suffix
// shift table.
static int kSuffixTable[kBMMaxShift + 1];
int kSuffixTable[kBMMaxShift + 1];
};

template <typename Char>
Expand Down

0 comments on commit c32775c

Please sign in to comment.