Skip to content

Commit

Permalink
src: pass only Isolate* and env_vars to EnabledDebugList::Parse()
Browse files Browse the repository at this point in the history
The function doesn't require access to the entire Environment object, so
this change just passes what it needs.

Addresses this TODO -
https://github.com/nodejs/node/blob/71691e53d601a4cf3eab3b33cebe4cc9f50c8470/src/env.cc#L372-L374

Signed-off-by: Darshan Sen <[email protected]>

PR-URL: nodejs#43668
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
RaisinTen authored and targos committed Jul 31, 2022
1 parent acc68c9 commit 1073bee
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 49 deletions.
6 changes: 4 additions & 2 deletions src/debug_utils.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "debug_utils-inl.h" // NOLINT(build/include)
#include "env-inl.h"
#include "node_internals.h"
#include "util.h"

#ifdef __POSIX__
#if defined(__linux__)
Expand Down Expand Up @@ -58,9 +59,10 @@ namespace per_process {
EnabledDebugList enabled_debug_list;
}

void EnabledDebugList::Parse(Environment* env) {
void EnabledDebugList::Parse(std::shared_ptr<KVStore> env_vars,
v8::Isolate* isolate) {
std::string cats;
credentials::SafeGetenv("NODE_DEBUG_NATIVE", &cats, env);
credentials::SafeGetenv("NODE_DEBUG_NATIVE", &cats, env_vars, isolate);
Parse(cats, true);
}

Expand Down
10 changes: 6 additions & 4 deletions src/debug_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "async_wrap.h"
#include "util.h"

#include <algorithm>
#include <sstream>
Expand Down Expand Up @@ -66,10 +67,11 @@ class NODE_EXTERN_PRIVATE EnabledDebugList {
return enabled_[static_cast<int>(category)];
}

// Uses NODE_DEBUG_NATIVE to initialize the categories. When env is not a
// nullptr, the environment variables set in the Environment are used.
// Otherwise the system environment variables are used.
void Parse(Environment* env);
// Uses NODE_DEBUG_NATIVE to initialize the categories. The env_vars variable
// is parsed if it is not a nullptr, otherwise the system environment
// variables are parsed.
void Parse(std::shared_ptr<KVStore> env_vars = nullptr,
v8::Isolate* isolate = nullptr);

private:
// Set all categories matching cats to the value of enabled.
Expand Down
5 changes: 1 addition & 4 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -760,10 +760,7 @@ Environment::Environment(IsolateData* isolate_data,
}

set_env_vars(per_process::system_environment);
// TODO(joyeecheung): pass Isolate* and env_vars to it instead of the entire
// env, when the recursive dependency inclusion in "debug-utils.h" is
// resolved.
enabled_debug_list_.Parse(this);
enabled_debug_list_.Parse(env_vars(), isolate);

// We create new copies of the per-Environment option sets, so that it is
// easier to modify them after Environment creation. The defaults are
Expand Down
28 changes: 0 additions & 28 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,34 +656,6 @@ struct ContextInfo {

class EnabledDebugList;

class KVStore {
public:
KVStore() = default;
virtual ~KVStore() = default;
KVStore(const KVStore&) = delete;
KVStore& operator=(const KVStore&) = delete;
KVStore(KVStore&&) = delete;
KVStore& operator=(KVStore&&) = delete;

virtual v8::MaybeLocal<v8::String> Get(v8::Isolate* isolate,
v8::Local<v8::String> key) const = 0;
virtual v8::Maybe<std::string> Get(const char* key) const = 0;
virtual void Set(v8::Isolate* isolate,
v8::Local<v8::String> key,
v8::Local<v8::String> value) = 0;
virtual int32_t Query(v8::Isolate* isolate,
v8::Local<v8::String> key) const = 0;
virtual int32_t Query(const char* key) const = 0;
virtual void Delete(v8::Isolate* isolate, v8::Local<v8::String> key) = 0;
virtual v8::Local<v8::Array> Enumerate(v8::Isolate* isolate) const = 0;

virtual std::shared_ptr<KVStore> Clone(v8::Isolate* isolate) const;
virtual v8::Maybe<bool> AssignFromObject(v8::Local<v8::Context> context,
v8::Local<v8::Object> entries);

static std::shared_ptr<KVStore> CreateMapKVStore();
};

namespace per_process {
extern std::shared_ptr<KVStore> system_environment;
}
Expand Down
2 changes: 1 addition & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ InitializationResult InitializeOncePerProcess(

// Initialized the enabled list for Debug() calls with system
// environment variables.
per_process::enabled_debug_list.Parse(nullptr);
per_process::enabled_debug_list.Parse();

atexit(ResetStdio);

Expand Down
21 changes: 12 additions & 9 deletions src/node_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,25 @@ bool linux_at_secure = false;
namespace credentials {

// Look up environment variable unless running as setuid root.
bool SafeGetenv(const char* key, std::string* text, Environment* env) {
bool SafeGetenv(const char* key,
std::string* text,
std::shared_ptr<KVStore> env_vars,
v8::Isolate* isolate) {
#if !defined(__CloudABI__) && !defined(_WIN32)
if (per_process::linux_at_secure || getuid() != geteuid() ||
getgid() != getegid())
goto fail;
#endif

if (env != nullptr) {
HandleScope handle_scope(env->isolate());
TryCatch ignore_errors(env->isolate());
MaybeLocal<String> maybe_value = env->env_vars()->Get(
env->isolate(),
String::NewFromUtf8(env->isolate(), key).ToLocalChecked());
if (env_vars != nullptr) {
DCHECK_NOT_NULL(isolate);
HandleScope handle_scope(isolate);
TryCatch ignore_errors(isolate);
MaybeLocal<String> maybe_value = env_vars->Get(
isolate, String::NewFromUtf8(isolate, key).ToLocalChecked());
Local<String> value;
if (!maybe_value.ToLocal(&value)) goto fail;
String::Utf8Value utf8_value(env->isolate(), value);
String::Utf8Value utf8_value(isolate, value);
if (*utf8_value == nullptr) goto fail;
*text = std::string(*utf8_value, utf8_value.length());
return true;
Expand Down Expand Up @@ -86,7 +89,7 @@ static void SafeGetenv(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = env->isolate();
Utf8Value strenvtag(isolate, args[0]);
std::string text;
if (!SafeGetenv(*strenvtag, &text, env)) return;
if (!SafeGetenv(*strenvtag, &text, env->env_vars(), isolate)) return;
Local<Value> result =
ToV8Value(isolate->GetCurrentContext(), text).ToLocalChecked();
args.GetReturnValue().Set(result);
Expand Down
5 changes: 4 additions & 1 deletion src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ class ThreadPoolWork {
#endif // defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)

namespace credentials {
bool SafeGetenv(const char* key, std::string* text, Environment* env = nullptr);
bool SafeGetenv(const char* key,
std::string* text,
std::shared_ptr<KVStore> env_vars = nullptr,
v8::Isolate* isolate = nullptr);
} // namespace credentials

void DefineZlibConstants(v8::Local<v8::Object> target);
Expand Down
28 changes: 28 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,34 @@ template <typename Inner, typename Outer>
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
Inner* pointer);

class KVStore {
public:
KVStore() = default;
virtual ~KVStore() = default;
KVStore(const KVStore&) = delete;
KVStore& operator=(const KVStore&) = delete;
KVStore(KVStore&&) = delete;
KVStore& operator=(KVStore&&) = delete;

virtual v8::MaybeLocal<v8::String> Get(v8::Isolate* isolate,
v8::Local<v8::String> key) const = 0;
virtual v8::Maybe<std::string> Get(const char* key) const = 0;
virtual void Set(v8::Isolate* isolate,
v8::Local<v8::String> key,
v8::Local<v8::String> value) = 0;
virtual int32_t Query(v8::Isolate* isolate,
v8::Local<v8::String> key) const = 0;
virtual int32_t Query(const char* key) const = 0;
virtual void Delete(v8::Isolate* isolate, v8::Local<v8::String> key) = 0;
virtual v8::Local<v8::Array> Enumerate(v8::Isolate* isolate) const = 0;

virtual std::shared_ptr<KVStore> Clone(v8::Isolate* isolate) const;
virtual v8::Maybe<bool> AssignFromObject(v8::Local<v8::Context> context,
v8::Local<v8::Object> entries);

static std::shared_ptr<KVStore> CreateMapKVStore();
};

// Convenience wrapper around v8::String::NewFromOneByte().
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
const char* data,
Expand Down

0 comments on commit 1073bee

Please sign in to comment.