Skip to content

Commit de7ab39

Browse files
acomminosCommit Bot
authored andcommitted
[cpu-profiler] Expose whether or not a script is shared cross origin in a CpuProfileNode
Enable cross-origin frame filtering by exposing this bit from ScriptOriginOptions. Bug: v8:8956 Change-Id: I109eec9db8b3d42d68d32abc5edd437b1c91a9b8 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1493294 Commit-Queue: Yang Guo <yangguo@chromium.org> Reviewed-by: Alexei Filippov <alph@chromium.org> Reviewed-by: Peter Marshall <petermarshall@chromium.org> Reviewed-by: Yang Guo <yangguo@chromium.org> Cr-Commit-Position: refs/heads/master@{#60205}
1 parent f8bbb58 commit de7ab39

11 files changed

Lines changed: 96 additions & 77 deletions

File tree

include/v8-profiler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ class V8_EXPORT CpuProfileNode {
152152
*/
153153
const char* GetScriptResourceNameStr() const;
154154

155+
/**
156+
* Return true if the script from where the function originates is flagged as
157+
* being shared cross-origin.
158+
*/
159+
bool IsScriptSharedCrossOrigin() const;
160+
155161
/**
156162
* Returns the number, 1-based, of the line where the function originates.
157163
* kNoLineNumberInfo if no line number information is available.

src/api.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9981,6 +9981,11 @@ const char* CpuProfileNode::GetScriptResourceNameStr() const {
99819981
return node->entry()->resource_name();
99829982
}
99839983

9984+
bool CpuProfileNode::IsScriptSharedCrossOrigin() const {
9985+
const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9986+
return node->entry()->is_shared_cross_origin();
9987+
}
9988+
99849989
int CpuProfileNode::GetLineNumber() const {
99859990
return reinterpret_cast<const i::ProfileNode*>(this)->line_number();
99869991
}

src/profiler/profile-generator-inl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ CodeEntry::CodeEntry(CodeEventListener::LogEventsAndTags tag, const char* name,
1414
const char* resource_name, int line_number,
1515
int column_number,
1616
std::unique_ptr<SourcePositionTable> line_info,
17-
Address instruction_start)
17+
Address instruction_start, bool is_shared_cross_origin)
1818
: bit_field_(TagField::encode(tag) |
19-
BuiltinIdField::encode(Builtins::builtin_count)),
19+
BuiltinIdField::encode(Builtins::builtin_count) |
20+
SharedCrossOriginField::encode(is_shared_cross_origin)),
2021
name_(name),
2122
resource_name_(resource_name),
2223
line_number_(line_number),

src/profiler/profile-generator.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class CodeEntry {
6565
int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
6666
int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
6767
std::unique_ptr<SourcePositionTable> line_info = nullptr,
68-
Address instruction_start = kNullAddress);
68+
Address instruction_start = kNullAddress,
69+
bool is_shared_cross_origin = false);
6970

7071
const char* name() const { return name_; }
7172
const char* resource_name() const { return resource_name_; }
@@ -106,6 +107,10 @@ class CodeEntry {
106107
return BuiltinIdField::decode(bit_field_);
107108
}
108109

110+
bool is_shared_cross_origin() const {
111+
return SharedCrossOriginField::decode(bit_field_);
112+
}
113+
109114
uint32_t GetHash() const;
110115
bool IsSameFunctionAs(const CodeEntry* entry) const;
111116

@@ -197,8 +202,11 @@ class CodeEntry {
197202
kUnresolvedEntry;
198203

199204
using TagField = BitField<CodeEventListener::LogEventsAndTags, 0, 8>;
200-
using BuiltinIdField = BitField<Builtins::Name, 8, 23>;
201-
using UsedField = BitField<bool, 31, 1>;
205+
using BuiltinIdField = BitField<Builtins::Name, 8, 22>;
206+
static_assert(Builtins::builtin_count <= BuiltinIdField::kNumValues,
207+
"builtin_count exceeds size of bitfield");
208+
using UsedField = BitField<bool, 30, 1>;
209+
using SharedCrossOriginField = BitField<bool, 31, 1>;
202210

203211
uint32_t bit_field_;
204212
const char* name_;

src/profiler/profiler-listener.cc

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void ProfilerListener::CallbackEvent(Name name, Address entry_point) {
2626
CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
2727
CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
2828
rec->instruction_start = entry_point;
29-
rec->entry = NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetName(name));
29+
rec->entry = new CodeEntry(CodeEventListener::CALLBACK_TAG, GetName(name));
3030
rec->instruction_size = 1;
3131
DispatchCodeEvent(evt_rec);
3232
}
@@ -36,10 +36,10 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
3636
CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
3737
CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
3838
rec->instruction_start = code->InstructionStart();
39-
rec->entry = NewCodeEntry(tag, GetName(name), CodeEntry::kEmptyResourceName,
40-
CpuProfileNode::kNoLineNumberInfo,
41-
CpuProfileNode::kNoColumnNumberInfo, nullptr,
42-
code->InstructionStart());
39+
rec->entry = new CodeEntry(tag, GetName(name), CodeEntry::kEmptyResourceName,
40+
CpuProfileNode::kNoLineNumberInfo,
41+
CpuProfileNode::kNoColumnNumberInfo, nullptr,
42+
code->InstructionStart());
4343
rec->instruction_size = code->InstructionSize();
4444
DispatchCodeEvent(evt_rec);
4545
}
@@ -49,10 +49,10 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
4949
CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
5050
CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
5151
rec->instruction_start = code->InstructionStart();
52-
rec->entry = NewCodeEntry(tag, GetName(name), CodeEntry::kEmptyResourceName,
53-
CpuProfileNode::kNoLineNumberInfo,
54-
CpuProfileNode::kNoColumnNumberInfo, nullptr,
55-
code->InstructionStart());
52+
rec->entry = new CodeEntry(tag, GetName(name), CodeEntry::kEmptyResourceName,
53+
CpuProfileNode::kNoLineNumberInfo,
54+
CpuProfileNode::kNoColumnNumberInfo, nullptr,
55+
code->InstructionStart());
5656
rec->instruction_size = code->InstructionSize();
5757
DispatchCodeEvent(evt_rec);
5858
}
@@ -64,11 +64,11 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
6464
CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
6565
CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
6666
rec->instruction_start = code->InstructionStart();
67-
rec->entry = NewCodeEntry(tag, GetName(shared->DebugName()),
68-
GetName(InferScriptName(script_name, shared)),
69-
CpuProfileNode::kNoLineNumberInfo,
70-
CpuProfileNode::kNoColumnNumberInfo, nullptr,
71-
code->InstructionStart());
67+
rec->entry = new CodeEntry(tag, GetName(shared->DebugName()),
68+
GetName(InferScriptName(script_name, shared)),
69+
CpuProfileNode::kNoLineNumberInfo,
70+
CpuProfileNode::kNoColumnNumberInfo, nullptr,
71+
code->InstructionStart());
7272
DCHECK(!code->IsCode());
7373
rec->entry->FillFunctionInfo(shared);
7474
rec->instruction_size = code->InstructionSize();
@@ -102,11 +102,14 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
102102
std::unordered_set<std::unique_ptr<CodeEntry>, CodeEntry::Hasher,
103103
CodeEntry::Equals>
104104
cached_inline_entries;
105+
bool is_shared_cross_origin = false;
105106
if (shared->script()->IsScript()) {
106107
Script script = Script::cast(shared->script());
107108
line_table.reset(new SourcePositionTable());
108109
HandleScope scope(isolate_);
109110

111+
is_shared_cross_origin = script.origin_options().IsSharedCrossOrigin();
112+
110113
// Add each position to the source position table and store inlining stacks
111114
// for inline positions. We store almost the same information in the
112115
// profiler as is stored on the code object, except that we transform source
@@ -140,6 +143,9 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
140143
? GetName(Name::cast(pos_info.script->name()))
141144
: CodeEntry::kEmptyResourceName;
142145

146+
bool inline_is_shared_cross_origin =
147+
pos_info.script->origin_options().IsSharedCrossOrigin();
148+
143149
// We need the start line number and column number of the function for
144150
// kLeafNodeLineNumbers mode. Creating a SourcePositionInfo is a handy
145151
// way of getting both easily.
@@ -151,7 +157,7 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
151157
base::make_unique<CodeEntry>(
152158
tag, GetName(pos_info.shared->DebugName()), resource_name,
153159
start_pos_info.line + 1, start_pos_info.column + 1, nullptr,
154-
code->InstructionStart());
160+
code->InstructionStart(), inline_is_shared_cross_origin);
155161
inline_entry->FillFunctionInfo(*pos_info.shared);
156162

157163
// Create a canonical CodeEntry for each inlined frame and then re-use
@@ -168,9 +174,10 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
168174
}
169175
}
170176
rec->entry =
171-
NewCodeEntry(tag, GetName(shared->DebugName()),
172-
GetName(InferScriptName(script_name, shared)), line, column,
173-
std::move(line_table), abstract_code->InstructionStart());
177+
new CodeEntry(tag, GetName(shared->DebugName()),
178+
GetName(InferScriptName(script_name, shared)), line, column,
179+
std::move(line_table), abstract_code->InstructionStart(),
180+
is_shared_cross_origin);
174181
if (!inline_stacks.empty()) {
175182
rec->entry->SetInlineStacks(std::move(cached_inline_entries),
176183
std::move(inline_stacks));
@@ -187,10 +194,10 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
187194
CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
188195
CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
189196
rec->instruction_start = code->instruction_start();
190-
rec->entry = NewCodeEntry(
197+
rec->entry = new CodeEntry(
191198
tag, GetName(name.start()), CodeEntry::kWasmResourceNamePrefix,
192199
CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo,
193-
nullptr, code->instruction_start());
200+
nullptr, code->instruction_start(), true);
194201
rec->instruction_size = code->instructions().length();
195202
DispatchCodeEvent(evt_rec);
196203
}
@@ -234,7 +241,7 @@ void ProfilerListener::GetterCallbackEvent(Name name, Address entry_point) {
234241
CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
235242
rec->instruction_start = entry_point;
236243
rec->entry =
237-
NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetConsName("get ", name));
244+
new CodeEntry(CodeEventListener::CALLBACK_TAG, GetConsName("get ", name));
238245
rec->instruction_size = 1;
239246
DispatchCodeEvent(evt_rec);
240247
}
@@ -243,7 +250,7 @@ void ProfilerListener::RegExpCodeCreateEvent(AbstractCode code, String source) {
243250
CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
244251
CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
245252
rec->instruction_start = code->InstructionStart();
246-
rec->entry = NewCodeEntry(
253+
rec->entry = new CodeEntry(
247254
CodeEventListener::REG_EXP_TAG, GetConsName("RegExp: ", source),
248255
CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
249256
CpuProfileNode::kNoColumnNumberInfo, nullptr, code->InstructionStart());
@@ -256,7 +263,7 @@ void ProfilerListener::SetterCallbackEvent(Name name, Address entry_point) {
256263
CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
257264
rec->instruction_start = entry_point;
258265
rec->entry =
259-
NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetConsName("set ", name));
266+
new CodeEntry(CodeEventListener::CALLBACK_TAG, GetConsName("set ", name));
260267
rec->instruction_size = 1;
261268
DispatchCodeEvent(evt_rec);
262269
}
@@ -317,13 +324,5 @@ void ProfilerListener::AttachDeoptInlinedFrames(Code code,
317324
}
318325
}
319326

320-
CodeEntry* ProfilerListener::NewCodeEntry(
321-
CodeEventListener::LogEventsAndTags tag, const char* name,
322-
const char* resource_name, int line_number, int column_number,
323-
std::unique_ptr<SourcePositionTable> line_info, Address instruction_start) {
324-
return new CodeEntry(tag, name, resource_name, line_number, column_number,
325-
std::move(line_info), instruction_start);
326-
}
327-
328327
} // namespace internal
329328
} // namespace v8

src/profiler/profiler-listener.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ class ProfilerListener : public CodeEventListener {
5454
void SetterCallbackEvent(Name name, Address entry_point) override;
5555
void SharedFunctionInfoMoveEvent(Address from, Address to) override {}
5656

57-
CodeEntry* NewCodeEntry(
58-
CodeEventListener::LogEventsAndTags tag, const char* name,
59-
const char* resource_name = CodeEntry::kEmptyResourceName,
60-
int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
61-
int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
62-
std::unique_ptr<SourcePositionTable> line_info = nullptr,
63-
Address instruction_start = kNullAddress);
64-
6557
const char* GetName(Name name) {
6658
return function_and_resource_names_.GetName(name);
6759
}

test/cctest/cctest.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ static inline i::Handle<T> GetGlobal(const char* name) {
339339
return i::Handle<T>::cast(value);
340340
}
341341

342+
static inline v8::Local<v8::Boolean> v8_bool(bool val) {
343+
return v8::Boolean::New(v8::Isolate::GetCurrent(), val);
344+
}
345+
342346
static inline v8::Local<v8::Value> v8_num(double x) {
343347
return v8::Number::New(v8::Isolate::GetCurrent(), x);
344348
}
@@ -386,29 +390,30 @@ static inline int32_t v8_run_int32value(v8::Local<v8::Script> script) {
386390
return script->Run(context).ToLocalChecked()->Int32Value(context).FromJust();
387391
}
388392

389-
390393
static inline v8::Local<v8::Script> CompileWithOrigin(
391-
v8::Local<v8::String> source, v8::Local<v8::String> origin_url) {
392-
v8::ScriptOrigin origin(origin_url);
394+
v8::Local<v8::String> source, v8::Local<v8::String> origin_url,
395+
v8::Local<v8::Boolean> is_shared_cross_origin) {
396+
v8::ScriptOrigin origin(origin_url, v8::Local<v8::Integer>(),
397+
v8::Local<v8::Integer>(), is_shared_cross_origin);
393398
v8::ScriptCompiler::Source script_source(source, origin);
394399
return v8::ScriptCompiler::Compile(
395400
v8::Isolate::GetCurrent()->GetCurrentContext(), &script_source)
396401
.ToLocalChecked();
397402
}
398403

399-
400404
static inline v8::Local<v8::Script> CompileWithOrigin(
401-
v8::Local<v8::String> source, const char* origin_url) {
402-
return CompileWithOrigin(source, v8_str(origin_url));
405+
v8::Local<v8::String> source, const char* origin_url,
406+
bool is_shared_cross_origin) {
407+
return CompileWithOrigin(source, v8_str(origin_url),
408+
v8_bool(is_shared_cross_origin));
403409
}
404410

405-
406-
static inline v8::Local<v8::Script> CompileWithOrigin(const char* source,
407-
const char* origin_url) {
408-
return CompileWithOrigin(v8_str(source), v8_str(origin_url));
411+
static inline v8::Local<v8::Script> CompileWithOrigin(
412+
const char* source, const char* origin_url, bool is_shared_cross_origin) {
413+
return CompileWithOrigin(v8_str(source), v8_str(origin_url),
414+
v8_bool(is_shared_cross_origin));
409415
}
410416

411-
412417
// Helper functions that compile and run the source.
413418
static inline v8::MaybeLocal<v8::Value> CompileRun(
414419
v8::Local<v8::Context> context, const char* source) {

test/cctest/test-api.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4853,7 +4853,8 @@ THREADED_TEST(MessageHandler0) {
48534853
CHECK(!message_received);
48544854
LocalContext context;
48554855
CcTest::isolate()->AddMessageListener(check_message_0, v8_num(5.76));
4856-
v8::Local<v8::Script> script = CompileWithOrigin("throw 'error'", "6.75");
4856+
v8::Local<v8::Script> script =
4857+
CompileWithOrigin("throw 'error'", "6.75", false);
48574858
CHECK(script->Run(context.local()).IsEmpty());
48584859
CHECK(message_received);
48594860
// clear out the message listener
@@ -14303,7 +14304,7 @@ THREADED_TEST(TryCatchSourceInfo) {
1430314304
const char* resource_name;
1430414305
v8::Local<v8::Script> script;
1430514306
resource_name = "test.js";
14306-
script = CompileWithOrigin(source, resource_name);
14307+
script = CompileWithOrigin(source, resource_name, false);
1430714308
CheckTryCatchSourceInfo(script, resource_name, 0);
1430814309

1430914310
resource_name = "test1.js";
@@ -14338,8 +14339,8 @@ THREADED_TEST(CompilationCache) {
1433814339
v8::HandleScope scope(context->GetIsolate());
1433914340
v8::Local<v8::String> source0 = v8_str("1234");
1434014341
v8::Local<v8::String> source1 = v8_str("1234");
14341-
v8::Local<v8::Script> script0 = CompileWithOrigin(source0, "test.js");
14342-
v8::Local<v8::Script> script1 = CompileWithOrigin(source1, "test.js");
14342+
v8::Local<v8::Script> script0 = CompileWithOrigin(source0, "test.js", false);
14343+
v8::Local<v8::Script> script1 = CompileWithOrigin(source1, "test.js", false);
1434314344
v8::Local<v8::Script> script2 = v8::Script::Compile(context.local(), source0)
1434414345
.ToLocalChecked(); // different origin
1434514346
CHECK_EQ(1234, script0->Run(context.local())
@@ -16755,7 +16756,7 @@ TEST(ErrorLevelWarning) {
1675516756
v8::HandleScope scope(isolate);
1675616757

1675716758
const char* source = "fake = 1;";
16758-
v8::Local<v8::Script> lscript = CompileWithOrigin(source, "test");
16759+
v8::Local<v8::Script> lscript = CompileWithOrigin(source, "test", false);
1675916760
i::Handle<i::SharedFunctionInfo> obj = i::Handle<i::SharedFunctionInfo>::cast(
1676016761
v8::Utils::OpenHandle(*lscript->GetUnboundScript()));
1676116762
CHECK(obj->script()->IsScript());
@@ -17629,7 +17630,7 @@ TEST(ScriptIdInStackTrace) {
1762917630
" AnalyzeScriptIdInStack();"
1763017631
"}\n"
1763117632
"foo();\n");
17632-
v8::Local<v8::Script> script = CompileWithOrigin(scriptSource, "test");
17633+
v8::Local<v8::Script> script = CompileWithOrigin(scriptSource, "test", false);
1763317634
script->Run(context.local()).ToLocalChecked();
1763417635
for (int i = 0; i < 2; i++) {
1763517636
CHECK_NE(scriptIdInStack[i], v8::Message::kNoScriptIdInfo);

0 commit comments

Comments
 (0)