Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
squash! do not print internal stack frames
  • Loading branch information
bnoordhuis committed Aug 26, 2016
commit dc8e0e672aa746662d1d177ac9742b4ab3383963
75 changes: 36 additions & 39 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#endif

#include <stdio.h>
#include <algorithm>

namespace node {

using v8::Context;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Local;
using v8::Message;
using v8::StackFrame;
using v8::StackTrace;

Expand Down Expand Up @@ -108,47 +108,44 @@ void Environment::StopProfilerIdleNotifier() {
uv_check_stop(&idle_check_handle_);
}

void Environment::PrintSyncTrace() const {
if (!trace_sync_io_)
return;
bool Environment::IsInternalScriptId(int script_id) const {
auto ids = internal_script_ids_;
return ids.end() != std::find(ids.begin(), ids.end(), script_id);
}

void Environment::PrintStackTrace(FILE* stream, const char* prefix,
PrintStackTraceMode mode) const {
HandleScope handle_scope(isolate());
Local<v8::StackTrace> stack =
StackTrace::CurrentStackTrace(isolate(), 10, StackTrace::kDetailed);

fprintf(stderr, "(node:%d) WARNING: Detected use of sync API\n", getpid());

for (int i = 0; i < stack->GetFrameCount() - 1; i++) {
Local<StackFrame> stack_frame = stack->GetFrame(i);
node::Utf8Value fn_name_s(isolate(), stack_frame->GetFunctionName());
node::Utf8Value script_name(isolate(), stack_frame->GetScriptName());
const int line_number = stack_frame->GetLineNumber();
const int column = stack_frame->GetColumn();

if (stack_frame->IsEval()) {
if (stack_frame->GetScriptId() == Message::kNoScriptIdInfo) {
fprintf(stderr, " at [eval]:%i:%i\n", line_number, column);
} else {
fprintf(stderr,
" at [eval] (%s:%i:%i)\n",
*script_name,
line_number,
column);
}
break;
}

if (fn_name_s.length() == 0) {
fprintf(stderr, " at %s:%i:%i\n", *script_name, line_number, column);
} else {
fprintf(stderr,
" at %s (%s:%i:%i)\n",
*fn_name_s,
*script_name,
line_number,
column);
}
// kExposeFramesAcrossSecurityOrigins is currently implied but let's be
// explicit so it won't break after a V8 upgrade.
// If you include kColumnOffset here, you should ensure that the offset of
// the first line for non-eval'd code compensates for the module wrapper.
using O = StackTrace::StackTraceOptions;
auto options = static_cast<O>(StackTrace::kExposeFramesAcrossSecurityOrigins |
StackTrace::kFunctionName |
StackTrace::kLineNumber |
StackTrace::kScriptId |
StackTrace::kScriptName);
auto stack_trace = StackTrace::CurrentStackTrace(isolate(), 12, options);
for (int i = 0, n = stack_trace->GetFrameCount(); i < n; i += 1) {
Local<v8::StackFrame> frame = stack_trace->GetFrame(i);
if (mode == kNoInternalScripts && IsInternalScriptId(frame->GetScriptId()))
continue;
node::Utf8Value function_name(isolate(), frame->GetFunctionName());
node::Utf8Value script_name(isolate(), frame->GetScriptName());
fprintf(stream, "%s at %s (%s:%d)\n", prefix,
**function_name ? *function_name : "<anonymous>",
*script_name, frame->GetLineNumber());
}
}

void Environment::PrintSyncTrace() const {
if (!trace_sync_io_)
return;
char prefix[32];
snprintf(prefix, sizeof(prefix), "(node:%d) ", getpid());
fprintf(stderr, "%sWARNING: Detected use of sync API\n", prefix);
PrintStackTrace(stderr, prefix);
fflush(stderr);
}

Expand Down
8 changes: 8 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "v8.h"

#include <stdint.h>
#include <stdio.h>
#include <vector>

// Caveat emptor: we're going slightly crazy with macros here but the end
Expand Down Expand Up @@ -511,6 +512,13 @@ class Environment {

inline v8::Local<v8::Object> NewInternalFieldObject();

bool IsInternalScriptId(int script_id) const;

enum PrintStackTraceMode { kInternalScripts, kNoInternalScripts };

void PrintStackTrace(FILE* stream, const char* prefix = "",
PrintStackTraceMode mode = kInternalScripts) const;

// Strings and private symbols are shared across shared contexts
// The getters simply proxy to the per-isolate primitive.
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName, StringValue)
Expand Down
26 changes: 6 additions & 20 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
#include <string.h>
#include <sys/types.h>

#include <algorithm>
#include <string>
#include <vector>

Expand Down Expand Up @@ -2525,12 +2524,6 @@ int GetCallerScriptId(v8::Isolate* isolate) {
}


inline bool IsInternalScriptId(Environment* env, int script_id) {
auto ids = env->internal_script_ids();
return ids->end() != std::find(ids->begin(), ids->end(), script_id);
}


static void Binding(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand Down Expand Up @@ -2570,21 +2563,14 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
cache->Set(module, exports);
} else if (!strcmp(*module_v, "natives")) {
auto caller_script_id = GetCallerScriptId(env->isolate());
if (!IsInternalScriptId(env, caller_script_id)) {
if (!env->IsInternalScriptId(caller_script_id)) {
// graceful-fs < 4 evals process.binding('natives').fs, which prohibits
// using internal modules in that module. Encourage people to upgrade.
Copy link
Copy Markdown
Member

@ChALkeR ChALkeR Aug 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which prohibits using internal modules in that module.

This is not accurate any more, they evaded that, and it now reevalutes internal modules with more dark magic, internal API usage, and relying on implementation details.

auto pid = getpid();
fprintf(stderr,
"(node:%d) process.binding('natives') is deprecated.\n", pid);
fprintf(stderr,
"(node:%d) If you use graceful-fs < 4, please update.\n", pid);
auto stack_trace = StackTrace::CurrentStackTrace(env->isolate(), 12);
for (int i = 0, n = stack_trace->GetFrameCount(); i < n; i += 1) {
Local<v8::StackFrame> frame = stack_trace->GetFrame(i);
node::Utf8Value name(env->isolate(), frame->GetScriptName());
fprintf(stderr,
"(node:%d) at %s:%d\n", pid, *name, frame->GetLineNumber());
}
char prefix[32];
snprintf(prefix, sizeof(prefix), "(node:%d) ", getpid());
fprintf(stderr, "%sprocess.binding('natives') is deprecated.\n", prefix);
fprintf(stderr, "%sIf you use graceful-fs < 4, please update.\n", prefix);
env->PrintStackTrace(stderr, prefix, Environment::kNoInternalScripts);
fflush(stderr);
}
exports = Object::New(env->isolate());
Expand Down