Commit 9aaf6642 authored by Ng Zhi An's avatar Ng Zhi An Committed by V8 LUCI CQ

Reland "[wasm][diagnostics] Support WasmCode in gdb JIT integration"

This is a reland of a3b2c4ec

The fix is in PS3, for UBSan. We use WriteUnalignedValue for
potentially unaligned memory writes.

Original change's description:
> [wasm][diagnostics] Support WasmCode in gdb JIT integration
>
> - Add new enum WASM_CODE to JitCodeEvent::CodeType
> - Use AddressRegion instead of AddressRange (remove the latter)
> - Change CodeDescription constructor to take an AddressRegion,
>   both JIT_CODE and WASM_CODE use this
> - Add a simple mjsunit test that sets --gdbjit to check that
>   we don't crash.
> - Add a api test for adding WASM_CODE
>
> Bug: v8:11908
> Change-Id: I6e87fadc2df67978144d78caf9800c3982bc3705
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3067754
> Reviewed-by: Adam Klein <adamk@chromium.org>
> Reviewed-by: Clemens Backes <clemensb@chromium.org>
> Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
> Commit-Queue: Zhi An Ng <zhin@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#76271}

Bug: v8:11908
Change-Id: I5ded6d01cff40803b2f70525163f760edcf97165
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3093506Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Reviewed-by: 's avatarClemens Backes <clemensb@chromium.org>
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#76327}
parent 150d1bd3
...@@ -7848,13 +7848,13 @@ struct JitCodeEvent { ...@@ -7848,13 +7848,13 @@ struct JitCodeEvent {
// statement, and is used to indicate possible break locations. // statement, and is used to indicate possible break locations.
enum PositionType { POSITION, STATEMENT_POSITION }; enum PositionType { POSITION, STATEMENT_POSITION };
// There are two different kinds of JitCodeEvents, one for JIT code generated // There are three different kinds of CodeType, one for JIT code generated
// by the optimizing compiler, and one for byte code generated for the // by the optimizing compiler, one for byte code generated for the
// interpreter. For JIT_CODE events, the |code_start| member of the event // interpreter, and one for code generated from Wasm. For JIT_CODE and
// points to the beginning of jitted assembly code, while for BYTE_CODE // WASM_CODE, |code_start| points to the beginning of jitted assembly code,
// events, |code_start| points to the first bytecode of the interpreted // while for BYTE_CODE events, |code_start| points to the first bytecode of
// function. // the interpreted function.
enum CodeType { BYTE_CODE, JIT_CODE }; enum CodeType { BYTE_CODE, JIT_CODE, WASM_CODE };
// Type of event. // Type of event.
EventType type; EventType type;
......
This diff is collapsed.
...@@ -732,7 +732,7 @@ void JitLogger::LogRecordedBuffer(const wasm::WasmCode* code, const char* name, ...@@ -732,7 +732,7 @@ void JitLogger::LogRecordedBuffer(const wasm::WasmCode* code, const char* name,
int length) { int length) {
JitCodeEvent event = {}; JitCodeEvent event = {};
event.type = JitCodeEvent::CODE_ADDED; event.type = JitCodeEvent::CODE_ADDED;
event.code_type = JitCodeEvent::JIT_CODE; event.code_type = JitCodeEvent::WASM_CODE;
event.code_start = code->instructions().begin(); event.code_start = code->instructions().begin();
event.code_len = code->instructions().length(); event.code_len = code->instructions().length();
event.name.str = name; event.name.str = name;
...@@ -1560,12 +1560,14 @@ void Logger::CodeLinePosInfoRecordEvent(Address code_start, ...@@ -1560,12 +1560,14 @@ void Logger::CodeLinePosInfoRecordEvent(Address code_start,
CodeLinePosEvent(*jit_logger_, code_start, iter, code_type); CodeLinePosEvent(*jit_logger_, code_start, iter, code_type);
} }
void Logger::CodeLinePosInfoRecordEvent( #if V8_ENABLE_WEBASSEMBLY
void Logger::WasmCodeLinePosInfoRecordEvent(
Address code_start, base::Vector<const byte> source_position_table) { Address code_start, base::Vector<const byte> source_position_table) {
if (!jit_logger_) return; if (!jit_logger_) return;
SourcePositionTableIterator iter(source_position_table); SourcePositionTableIterator iter(source_position_table);
CodeLinePosEvent(*jit_logger_, code_start, iter, JitCodeEvent::JIT_CODE); CodeLinePosEvent(*jit_logger_, code_start, iter, JitCodeEvent::WASM_CODE);
} }
#endif // V8_ENABLE_WEBASSEMBLY
void Logger::CodeNameEvent(Address addr, int pos, const char* code_name) { void Logger::CodeNameEvent(Address addr, int pos, const char* code_name) {
if (code_name == nullptr) return; // Not a code object. if (code_name == nullptr) return; // Not a code object.
......
...@@ -241,8 +241,10 @@ class Logger : public CodeEventListener { ...@@ -241,8 +241,10 @@ class Logger : public CodeEventListener {
void CodeLinePosInfoRecordEvent(Address code_start, void CodeLinePosInfoRecordEvent(Address code_start,
ByteArray source_position_table, ByteArray source_position_table,
JitCodeEvent::CodeType code_type); JitCodeEvent::CodeType code_type);
void CodeLinePosInfoRecordEvent( #if V8_ENABLE_WEBASSEMBLY
void WasmCodeLinePosInfoRecordEvent(
Address code_start, base::Vector<const byte> source_position_table); Address code_start, base::Vector<const byte> source_position_table);
#endif // V8_ENABLE_WEBASSEMBLY
void CodeNameEvent(Address addr, int pos, const char* code_name); void CodeNameEvent(Address addr, int pos, const char* code_name);
......
...@@ -267,14 +267,17 @@ void WasmCode::LogCode(Isolate* isolate, const char* source_url, ...@@ -267,14 +267,17 @@ void WasmCode::LogCode(Isolate* isolate, const char* source_url,
"wasm-function[%d]", index())); "wasm-function[%d]", index()));
name = base::VectorOf(name_buffer); name = base::VectorOf(name_buffer);
} }
int code_offset = module->functions[index_].code.offset();
PROFILE(isolate, CodeCreateEvent(CodeEventListener::FUNCTION_TAG, this, name,
source_url, code_offset, script_id));
// Record source positions before adding code, otherwise when code is added,
// there are no source positions to associate with the added code.
if (!source_positions().empty()) { if (!source_positions().empty()) {
LOG_CODE_EVENT(isolate, CodeLinePosInfoRecordEvent(instruction_start(), LOG_CODE_EVENT(isolate, WasmCodeLinePosInfoRecordEvent(instruction_start(),
source_positions())); source_positions()));
} }
int code_offset = module->functions[index_].code.offset();
PROFILE(isolate, CodeCreateEvent(CodeEventListener::FUNCTION_TAG, this, name,
source_url, code_offset, script_id));
} }
void WasmCode::Validate() const { void WasmCode::Validate() const {
......
...@@ -589,6 +589,7 @@ ...@@ -589,6 +589,7 @@
'test-api/TurboAsmDisablesDetach': [SKIP], 'test-api/TurboAsmDisablesDetach': [SKIP],
'test-api/WasmI32AtomicWaitCallback': [SKIP], 'test-api/WasmI32AtomicWaitCallback': [SKIP],
'test-api/WasmI64AtomicWaitCallback': [SKIP], 'test-api/WasmI64AtomicWaitCallback': [SKIP],
'test-api/WasmSetJitCodeEventHandler': [SKIP],
'test-api-wasm/WasmStreaming*': [SKIP], 'test-api-wasm/WasmStreaming*': [SKIP],
'test-backing-store/Run_WasmModule_Buffer_Externalized_Regression_UseAfterFree': [SKIP], 'test-backing-store/Run_WasmModule_Buffer_Externalized_Regression_UseAfterFree': [SKIP],
'test-c-wasm-entry/*': [SKIP], 'test-c-wasm-entry/*': [SKIP],
......
...@@ -76,6 +76,7 @@ ...@@ -76,6 +76,7 @@
#if V8_ENABLE_WEBASSEMBLY #if V8_ENABLE_WEBASSEMBLY
#include "test/cctest/wasm/wasm-run-utils.h" #include "test/cctest/wasm/wasm-run-utils.h"
#include "test/common/wasm/test-signatures.h"
#include "test/common/wasm/wasm-macro-gen.h" #include "test/common/wasm/wasm-macro-gen.h"
#endif // V8_ENABLE_WEBASSEMBLY #endif // V8_ENABLE_WEBASSEMBLY
...@@ -13738,16 +13739,14 @@ static v8::base::HashMap* jitcode_line_info = nullptr; ...@@ -13738,16 +13739,14 @@ static v8::base::HashMap* jitcode_line_info = nullptr;
static int saw_bar = 0; static int saw_bar = 0;
static int move_events = 0; static int move_events = 0;
static bool FunctionNameIs(const char* expected, static bool FunctionNameIs(const char* expected,
const v8::JitCodeEvent* event) { const v8::JitCodeEvent* event) {
// Log lines for functions are of the general form: // Log lines for functions are of the general form:
// "LazyCompile:<type><function_name>" or Function:<type><function_name>, // "LazyCompile:<type><function_name>" or Function:<type><function_name>,
// where the type is one of "*", "~" or "". // where the type is one of "*", "~" or "".
static const char* kPreamble; static const char* kPreamble = "Function:";
if (!i::FLAG_lazy) { if (i::FLAG_lazy &&
kPreamble = "Function:"; event->code_type != v8::JitCodeEvent::CodeType::WASM_CODE) {
} else {
kPreamble = "LazyCompile:"; kPreamble = "LazyCompile:";
} }
static size_t kPreambleLen = strlen(kPreamble); static size_t kPreambleLen = strlen(kPreamble);
...@@ -13780,7 +13779,6 @@ static bool FunctionNameIs(const char* expected, ...@@ -13780,7 +13779,6 @@ static bool FunctionNameIs(const char* expected,
return strncmp(tail, expected, expected_len) == 0; return strncmp(tail, expected, expected_len) == 0;
} }
static void event_handler(const v8::JitCodeEvent* event) { static void event_handler(const v8::JitCodeEvent* event) {
CHECK_NOT_NULL(event); CHECK_NOT_NULL(event);
CHECK_NOT_NULL(code_map); CHECK_NOT_NULL(code_map);
...@@ -13873,7 +13871,6 @@ static void event_handler(const v8::JitCodeEvent* event) { ...@@ -13873,7 +13871,6 @@ static void event_handler(const v8::JitCodeEvent* event) {
} }
} }
UNINITIALIZED_TEST(SetJitCodeEventHandler) { UNINITIALIZED_TEST(SetJitCodeEventHandler) {
i::FLAG_stress_compaction = true; i::FLAG_stress_compaction = true;
i::FLAG_incremental_marking = false; i::FLAG_incremental_marking = false;
...@@ -13998,6 +13995,77 @@ UNINITIALIZED_TEST(SetJitCodeEventHandler) { ...@@ -13998,6 +13995,77 @@ UNINITIALIZED_TEST(SetJitCodeEventHandler) {
isolate->Dispose(); isolate->Dispose();
} }
#if V8_ENABLE_WEBASSEMBLY
static bool saw_wasm_main = false;
static void wasm_event_handler(const v8::JitCodeEvent* event) {
switch (event->type) {
case v8::JitCodeEvent::CODE_ADDED: {
if (FunctionNameIs("main-0-turbofan", event)) {
saw_wasm_main = true;
// Make sure main function has line info.
auto* entry = jitcode_line_info->Lookup(
event->code_start, i::ComputePointerHash(event->code_start));
CHECK_NOT_NULL(entry);
}
break;
}
case v8::JitCodeEvent::CODE_END_LINE_INFO_RECORDING: {
jitcode_line_info->LookupOrInsert(
event->code_start, i::ComputePointerHash(event->code_start));
break;
}
case v8::JitCodeEvent::CODE_ADD_LINE_POS_INFO: {
break;
}
default: {
// Ignore all other events;
}
}
}
namespace v8 {
namespace internal {
namespace wasm {
TEST(WasmSetJitCodeEventHandler) {
v8::base::HashMap code;
code_map = &code;
v8::base::HashMap lineinfo;
jitcode_line_info = &lineinfo;
WasmRunner<int32_t, int32_t, int32_t> r(TestExecutionTier::kTurbofan);
i::Isolate* isolate = r.main_isolate();
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
v8_isolate->SetJitCodeEventHandler(v8::kJitCodeEventDefault,
wasm_event_handler);
TestSignatures sigs;
auto& f = r.NewFunction(sigs.i_i(), "f");
BUILD(f, WASM_I32_ADD(WASM_LOCAL_GET(0), WASM_LOCAL_GET(0)));
LocalContext env;
BUILD(r,
WASM_I32_ADD(WASM_LOCAL_GET(0), WASM_CALL_FUNCTION(f.function_index(),
WASM_LOCAL_GET(1))));
Handle<JSFunction> func = r.builder().WrapCode(0);
CHECK(env->Global()
->Set(env.local(), v8_str("func"), v8::Utils::ToLocal(func))
.FromJust());
const char* script = R"(
func(1, 2);
)";
CompileRun(script);
CHECK(saw_wasm_main);
saw_wasm_main = false;
}
} // namespace wasm
} // namespace internal
} // namespace v8
#endif // V8_ENABLE_WEBASSEMBLY
TEST(ExternalAllocatedMemory) { TEST(ExternalAllocatedMemory) {
v8::Isolate* isolate = CcTest::isolate(); v8::Isolate* isolate = CcTest::isolate();
v8::HandleScope outer(isolate); v8::HandleScope outer(isolate);
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --gdbjit
d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
// A simple test to ensure that passing the --gdbjit flag doesn't crash.
(function testGdbJitFlag() {
const builder = new WasmModuleBuilder();
builder.addFunction('i32_add', kSig_i_ii)
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprI32Add])
.exportFunc();
const module = new WebAssembly.Module(builder.toBuffer());
const instance = new WebAssembly.Instance(module);
assertEquals(instance.exports.i32_add(1, 2), 3);
}());
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