1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/controller/javascript_call_stack_collector.h"
#include "third_party/blink/public/common/permissions_policy/document_policy_features.h"
#include "third_party/blink/public/common/scheme_registry.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/platform/bindings/dom_wrapper_world.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/public/main_thread.h"
#include "third_party/blink/renderer/platform/scheduler/public/main_thread_scheduler.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h"
namespace WTF {
template <>
struct CrossThreadCopier<std::optional<blink::LocalFrameToken>>
: public CrossThreadCopierPassThrough<
std::optional<blink::LocalFrameToken>> {};
} // namespace WTF
namespace blink {
namespace {
// Determines whether a script frame should be included in the call stack.
// frames whose URL protocol matches an extension scheme are excluded.
// Returns true to include the frame (i.e. not redacted) and false to exclude
// it.
bool IsScriptFrameAllowed(v8::Isolate* isolate,
v8::Local<v8::String> script_name) {
String script_url =
ToCoreStringWithUndefinedOrNullCheck(isolate, script_name);
if (script_url.empty()) {
return true;
}
KURL url(script_url);
if (!url.IsValid()) {
return true;
}
return !CommonSchemeRegistry::IsExtensionScheme(url.Protocol().Ascii());
}
// Gathers and formats the call stack in a format that's
// consistent with Error.stack If extension frames are detected,
// we replace them with <redacted> to protect privacy.
// The function respects the stack trace limit set in the isolate.
void CollectFilteredCallStack(v8::Isolate* isolate, StringBuilder& builder) {
std::ostringstream oss;
v8::Message::PrintCurrentStackTrace(isolate, oss, &IsScriptFrameAllowed);
const std::string stack_trace = oss.str();
std::istringstream iss(stack_trace);
std::string line;
int processed_frames = 0;
const int stack_trace_limit = isolate->GetStackTraceLimit();
while (std::getline(iss, line) && processed_frames < stack_trace_limit) {
builder.Append(kStackFramePrefix);
builder.Append(base::as_byte_span(line));
processed_frames++;
}
}
void PostHandleCollectedCallStackTask(
JavaScriptCallStackCollector* collector,
WTF::StringBuilder& builder,
std::optional<LocalFrameToken> frame_token = std::nullopt) {
DCHECK(Platform::Current());
PostCrossThreadTask(
*Platform::Current()->GetIOTaskRunner(), FROM_HERE,
WTF::CrossThreadBindOnce(
&JavaScriptCallStackCollector::HandleCallStackCollected,
WTF::CrossThreadUnretained(collector), builder.ReleaseString(),
frame_token));
}
void GenerateJavaScriptCallStack(v8::Isolate* isolate, void* data) {
CHECK(IsMainThread());
auto* collector = static_cast<JavaScriptCallStackCollector*>(data);
v8::HandleScope handle_scope(isolate);
WTF::StringBuilder builder;
if (!isolate->InContext()) {
PostHandleCollectedCallStackTask(collector, builder);
return;
}
v8::Local<v8::Context> context = isolate->GetCurrentContext();
ScriptState* script_state = ScriptState::MaybeFrom(isolate, context);
if (!script_state) {
PostHandleCollectedCallStackTask(collector, builder);
return;
}
ExecutionContext* execution_context = ToExecutionContext(script_state);
if (!RuntimeEnabledFeatures::
DocumentPolicyIncludeJSCallStacksInCrashReportsEnabled(
execution_context)) {
PostHandleCollectedCallStackTask(collector, builder);
return;
}
DOMWrapperWorld& world = script_state->World();
auto* execution_dom_window = DynamicTo<LocalDOMWindow>(execution_context);
LocalFrame* frame =
execution_dom_window ? execution_dom_window->GetFrame() : nullptr;
std::optional<LocalFrameToken> frame_token;
if (frame && world.IsMainWorld()) {
frame_token = frame->GetLocalFrameToken();
if (!execution_context->IsFeatureEnabled(
mojom::blink::DocumentPolicyFeature::
kIncludeJSCallStacksInCrashReports)) {
builder.Append(kWebsiteOwnerNotOptedInMessage);
} else {
UseCounter::Count(
execution_context,
WebFeature::kDocumentPolicyIncludeJSCallStacksInCrashReports);
CollectFilteredCallStack(isolate, builder);
}
}
PostHandleCollectedCallStackTask(collector, builder, frame_token);
}
} // namespace
void JavaScriptCallStackCollector::InterruptIsolateAndCollectCallStack(
v8::Isolate* isolate) {
if (has_interrupted_isolate_) {
return;
}
has_interrupted_isolate_ = true;
isolate->RequestInterrupt(&GenerateJavaScriptCallStack,
static_cast<void*>(this));
}
void JavaScriptCallStackCollector::HandleCallStackCollected(
const String& call_stack,
const std::optional<LocalFrameToken> frame_token) {
DCHECK(result_callback_);
std::move(result_callback_).Run(call_stack, frame_token);
DCHECK(finished_callback_);
std::move(finished_callback_).Run(this);
}
void JavaScriptCallStackCollector::CollectJavaScriptCallStack() {
Thread::MainThread()
->Scheduler()
->ToMainThreadScheduler()
->ForEachMainThreadIsolate(WTF::BindRepeating(
&JavaScriptCallStackCollector::InterruptIsolateAndCollectCallStack,
WTF::Unretained(this)));
}
} // namespace blink
|