Commit 3aedafbb authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[wasm] Reorganize source position lookup

Instead of two copies of the lookup code in frames.cc and wasm-debug.cc,
put one lookup method on the WasmCode. This is where it belongs really,
since the WasmCode is the main input to the function (besides the
offset).

Also refactor how source positions are computed in WasmCompiledFrame.
Avoid going through the summary, which is unneccessarily complex. This
also adds another {byte_offset} accessor which can be used for
debugging.

Bug: v8:10235
Change-Id: I5c545ee302754b86009f09bedc5ff6e39ba664f6
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2135726Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66991}
parent df204288
......@@ -1454,21 +1454,8 @@ uint32_t FrameSummary::WasmCompiledFrameSummary::function_index() const {
return code()->index();
}
int FrameSummary::WasmCompiledFrameSummary::GetWasmSourcePosition(
const wasm::WasmCode* code, int offset) {
int position = 0;
// Subtract one because the current PC is one instruction after the call site.
offset--;
for (SourcePositionTableIterator iterator(code->source_positions());
!iterator.done() && iterator.code_offset() <= offset;
iterator.Advance()) {
position = iterator.source_position().ScriptOffset();
}
return position;
}
int FrameSummary::WasmCompiledFrameSummary::byte_offset() const {
return GetWasmSourcePosition(code_, code_offset());
return code_->GetSourcePositionBefore(code_offset());
}
FrameSummary::WasmInterpretedFrameSummary::WasmInterpretedFrameSummary(
......@@ -1922,7 +1909,16 @@ uint32_t WasmCompiledFrame::function_index() const {
Script WasmCompiledFrame::script() const { return module_object().script(); }
int WasmCompiledFrame::position() const {
return FrameSummary::GetSingle(this).SourcePosition();
wasm::WasmCodeRefScope code_ref_scope;
const wasm::WasmModule* module = wasm_instance().module_object().module();
return GetSourcePosition(module, function_index(), byte_offset(),
at_to_number_conversion());
}
int WasmCompiledFrame::byte_offset() const {
wasm::WasmCode* code = wasm_code();
int offset = static_cast<int>(pc() - code->instruction_start());
return code->GetSourcePositionBefore(offset);
}
Object WasmCompiledFrame::context() const {
......@@ -1951,11 +1947,11 @@ bool WasmCompiledFrame::at_to_number_conversion() const {
: nullptr;
if (!code || code->kind() != wasm::WasmCode::kWasmToJsWrapper) return false;
int offset = static_cast<int>(callee_pc() - code->instruction_start());
int pos = FrameSummary::WasmCompiledFrameSummary::GetWasmSourcePosition(
code, offset);
DCHECK(pos == 0 || pos == 1);
int pos = code->GetSourcePositionBefore(offset);
// The imported call has position 0, ToNumber has position 1.
return !!pos;
// If there is no source position available, this is also not a ToNumber call.
DCHECK(pos == wasm::kNoCodePosition || pos == 0 || pos == 1);
return pos == 1;
}
int WasmCompiledFrame::LookupExceptionHandlerInTable() {
......
......@@ -546,7 +546,6 @@ class V8_EXPORT_PRIVATE FrameSummary {
wasm::WasmCode* code() const { return code_; }
int code_offset() const { return code_offset_; }
int byte_offset() const;
static int GetWasmSourcePosition(const wasm::WasmCode* code, int offset);
private:
wasm::WasmCode* const code_;
......@@ -961,9 +960,12 @@ class WasmCompiledFrame : public StandardFrame {
wasm::WasmCode* wasm_code() const;
uint32_t function_index() const;
Script script() const override;
// Byte position in the module, or asm.js source position.
int position() const override;
Object context() const override;
bool at_to_number_conversion() const;
// Byte offset in the function.
int byte_offset() const;
void Summarize(std::vector<FrameSummary>* frames) const override;
......
......@@ -2112,8 +2112,7 @@ bool Isolate::ComputeLocationFromStackTrace(MessageLocation* target,
Managed<wasm::GlobalWasmCodeRef>::cast(elements->WasmCodeObject(i))
.get()
->code();
offset = FrameSummary::WasmCompiledFrameSummary::GetWasmSourcePosition(
code, offset);
offset = code->GetSourcePositionBefore(offset);
}
Handle<WasmInstanceObject> instance(elements->WasmInstance(i), this);
const wasm::WasmModule* module = elements->WasmInstance(i).module();
......
......@@ -601,10 +601,7 @@ Handle<Object> WasmStackFrame::GetWasmModuleName() {
Handle<Object> WasmStackFrame::GetWasmInstance() { return wasm_instance_; }
int WasmStackFrame::GetPosition() const {
return IsInterpreted()
? offset_
: FrameSummary::WasmCompiledFrameSummary::GetWasmSourcePosition(
code_, offset_);
return IsInterpreted() ? offset_ : code_->GetSourcePositionBefore(offset_);
}
int WasmStackFrame::GetColumnNumber() { return GetModuleOffset(); }
......@@ -657,9 +654,7 @@ Handle<Object> AsmJsWasmStackFrame::GetScriptNameOrSourceUrl() {
int AsmJsWasmStackFrame::GetPosition() const {
DCHECK_LE(0, offset_);
int byte_offset =
FrameSummary::WasmCompiledFrameSummary::GetWasmSourcePosition(code_,
offset_);
int byte_offset = code_->GetSourcePositionBefore(offset_);
const wasm::WasmModule* module = wasm_instance_->module();
return GetSourcePosition(module, wasm_func_index_, byte_offset,
is_at_number_conversion_);
......
......@@ -444,6 +444,16 @@ void WasmCode::DecrementRefCount(Vector<WasmCode* const> code_vec) {
if (engine) engine->FreeDeadCode(dead_code);
}
int WasmCode::GetSourcePositionBefore(int offset) {
int position = kNoSourcePosition;
for (SourcePositionTableIterator iterator(source_positions());
!iterator.done() && iterator.code_offset() < offset;
iterator.Advance()) {
position = iterator.source_position().ScriptOffset();
}
return position;
}
WasmCodeAllocator::OptionalLock::~OptionalLock() {
if (allocator_) allocator_->mutex_.Unlock();
}
......
......@@ -219,6 +219,9 @@ class V8_EXPORT_PRIVATE WasmCode final {
// belonging to different {NativeModule}s. Dead code will be deleted.
static void DecrementRefCount(Vector<WasmCode* const>);
// Returns the last source position before {offset}.
int GetSourcePositionBefore(int offset);
enum FlushICache : bool { kFlushICache = true, kNoFlushICache = false };
private:
......
......@@ -461,17 +461,6 @@ class InterpreterHandle {
DISALLOW_COPY_AND_ASSIGN(InterpreterHandle);
};
int FindByteOffset(int pc_offset, WasmCode* wasm_code) {
int position = 0;
SourcePositionTableIterator iterator(wasm_code->source_positions());
for (SourcePositionTableIterator iterator(wasm_code->source_positions());
!iterator.done() && iterator.code_offset() < pc_offset;
iterator.Advance()) {
position = iterator.source_position().ScriptOffset();
}
return position;
}
// Generate a sorted and deduplicated list of byte offsets for this function's
// current positions on the stack.
std::vector<int> StackFramePositions(int func_index, Isolate* isolate) {
......@@ -483,10 +472,7 @@ std::vector<int> StackFramePositions(int func_index, Isolate* isolate) {
if (static_cast<int>(frame->function_index()) != func_index) continue;
WasmCode* wasm_code = frame->wasm_code();
if (!wasm_code->is_liftoff()) continue;
int pc_offset =
static_cast<int>(frame->pc() - wasm_code->instruction_start());
int byte_offset = FindByteOffset(pc_offset, wasm_code);
byte_offsets.push_back(byte_offset);
byte_offsets.push_back(frame->byte_offset());
}
std::sort(byte_offsets.begin(), byte_offsets.end());
auto last = std::unique(byte_offsets.begin(), byte_offsets.end());
......@@ -925,12 +911,9 @@ class DebugInfoImpl {
WasmCompiledFrame* frame = WasmCompiledFrame::cast(it.frame());
if (frame->native_module() != new_code->native_module()) continue;
if (frame->function_index() != new_code->index()) continue;
WasmCode* old_code = frame->wasm_code();
if (!old_code->is_liftoff()) continue;
int pc_offset =
static_cast<int>(frame->pc() - old_code->instruction_start());
if (!frame->wasm_code()->is_liftoff()) continue;
int position = frame->position();
int byte_offset = FindByteOffset(pc_offset, old_code);
int byte_offset = frame->byte_offset();
Address new_pc = FindNewPC(new_code, byte_offset, return_location);
PointerAuthentication::ReplacePC(frame->pc_address(), new_pc,
kSystemPointerSize);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment