Skip to content

Commit bf9e084

Browse files
committed
更新构建脚本以修复补丁文件路径和源文件路径
新增 v8dasm.cpp 文件以支持字节码加载和反汇编功能
1 parent 889bdd7 commit bf9e084

6 files changed

Lines changed: 217 additions & 8 deletions

File tree

Disassembler/v8.patch

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
diff --git a/src/diagnostics/objects-printer.cc b/src/diagnostics/objects-printer.cc
2+
index 5b546e29926..45d7b50b7f5 100644
3+
--- a/src/diagnostics/objects-printer.cc
4+
+++ b/src/diagnostics/objects-printer.cc
5+
@@ -1687,7 +1687,6 @@ void SharedFunctionInfo::SharedFunctionInfoPrint(std::ostream& os) {
6+
os << "\n - data: " << Brief(function_data(kAcquireLoad));
7+
os << "\n - code (from data): ";
8+
os << Brief(GetCode());
9+
- PrintSourceCode(os);
10+
// Script files are often large, thus only print their {Brief} representation.
11+
os << "\n - script: " << Brief(script());
12+
os << "\n - function token position: " << function_token_position();
13+
@@ -1710,6 +1709,10 @@ void SharedFunctionInfo::SharedFunctionInfoPrint(std::ostream& os) {
14+
os << "<none>";
15+
}
16+
os << "\n";
17+
+ os << "\nStart BytecodeArray\n";
18+
+ this->GetActiveBytecodeArray().Disassemble(os);
19+
+ os << "\nEnd BytecodeArray\n";
20+
+ os << std::flush;
21+
}
22+
23+
void JSGlobalProxy::JSGlobalProxyPrint(std::ostream& os) {
24+
diff --git a/src/objects/objects.cc b/src/objects/objects.cc
25+
index 559160358c1..420b3da2b66 100644
26+
--- a/src/objects/objects.cc
27+
+++ b/src/objects/objects.cc
28+
@@ -1850,6 +1850,16 @@ void HeapObject::HeapObjectShortPrint(std::ostream& os) {
29+
os << accumulator.ToCString().get();
30+
return;
31+
}
32+
+
33+
+ // Print array literal members instead of only "<AsmWasmData>"
34+
+ if (map(cage_base).instance_type() == ASM_WASM_DATA_TYPE) {
35+
+ os << "<ArrayBoilerplateDescription> ";
36+
+ ArrayBoilerplateDescription::cast(*this)
37+
+ .constant_elements()
38+
+ .HeapObjectShortPrint(os);
39+
+ return;
40+
+ }
41+
+
42+
switch (map(cage_base).instance_type()) {
43+
case MAP_TYPE: {
44+
os << "<Map";
45+
@@ -1936,14 +1946,24 @@ void HeapObject::HeapObjectShortPrint(std::ostream& os) {
46+
break;
47+
case FIXED_ARRAY_TYPE:
48+
os << "<FixedArray[" << FixedArray::cast(*this).length() << "]>";
49+
+ os << "\nStart FixedArray\n";
50+
+ FixedArray::cast(*this).FixedArrayPrint(os);
51+
+ os << "\nEnd FixedArray\n";
52+
break;
53+
case OBJECT_BOILERPLATE_DESCRIPTION_TYPE:
54+
os << "<ObjectBoilerplateDescription[" << FixedArray::cast(*this).length()
55+
<< "]>";
56+
+ os << "\nStart ObjectBoilerplateDescription\n";
57+
+ ObjectBoilerplateDescription::cast(*this)
58+
+ .ObjectBoilerplateDescriptionPrint(os);
59+
+ os << "\nEnd ObjectBoilerplateDescription\n";
60+
break;
61+
case FIXED_DOUBLE_ARRAY_TYPE:
62+
os << "<FixedDoubleArray[" << FixedDoubleArray::cast(*this).length()
63+
<< "]>";
64+
+ os << "\nStart FixedDoubleArray\n";
65+
+ FixedDoubleArray::cast(*this).FixedDoubleArrayPrint(os);
66+
+ os << "\nEnd FixedDoubleArray\n";
67+
break;
68+
case BYTE_ARRAY_TYPE:
69+
os << "<ByteArray[" << ByteArray::cast(*this).length() << "]>";
70+
@@ -2022,6 +2042,9 @@ void HeapObject::HeapObjectShortPrint(std::ostream& os) {
71+
} else {
72+
os << "<SharedFunctionInfo>";
73+
}
74+
+ os << "\nStart SharedFunctionInfo\n";
75+
+ shared.SharedFunctionInfoPrint(os);
76+
+ os << "\nEnd SharedFunctionInfo\n";
77+
break;
78+
}
79+
case JS_MESSAGE_OBJECT_TYPE:
80+
diff --git a/src/objects/string.cc b/src/objects/string.cc
81+
index cd134f84055..285d55fda5e 100644
82+
--- a/src/objects/string.cc
83+
+++ b/src/objects/string.cc
84+
@@ -477,13 +477,6 @@ void String::StringShortPrint(StringStream* accumulator) {
85+
accumulator->Add("<String[%u]: ", len);
86+
accumulator->Add(PrefixForDebugPrint());
87+
88+
- if (len > kMaxShortPrintLength) {
89+
- accumulator->Add("...<truncated>>");
90+
- accumulator->Add(SuffixForDebugPrint());
91+
- accumulator->Put('>');
92+
- return;
93+
- }
94+
-
95+
PrintUC16(accumulator, 0, len);
96+
accumulator->Add(SuffixForDebugPrint());
97+
accumulator->Put('>');
98+
diff --git a/src/snapshot/code-serializer.cc b/src/snapshot/code-serializer.cc
99+
index 3f380e6a2ff..1f55cb9009a 100644
100+
--- a/src/snapshot/code-serializer.cc
101+
+++ b/src/snapshot/code-serializer.cc
102+
@@ -466,6 +466,12 @@ MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize(
103+
if (FLAG_profile_deserialization) PrintF("[Deserializing failed]\n");
104+
return MaybeHandle<SharedFunctionInfo>();
105+
}
106+
+
107+
+ std::cout << "\nStart SharedFunctionInfo\n";
108+
+ result->SharedFunctionInfoPrint(std::cout);
109+
+ std::cout << "\nEnd SharedFunctionInfo\n";
110+
+ std::cout << std::flush;
111+
+
112+
BaselineBatchCompileIfSparkplugCompiled(isolate,
113+
Script::cast(result->script()));
114+
if (FLAG_profile_deserialization) {
115+
@@ -651,9 +657,7 @@ SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload,
116+
117+
SerializedCodeSanityCheckResult SerializedCodeData::SanityCheck(
118+
uint32_t expected_source_hash) const {
119+
- SerializedCodeSanityCheckResult result = SanityCheckWithoutSource();
120+
- if (result != SerializedCodeSanityCheckResult::kSuccess) return result;
121+
- return SanityCheckJustSource(expected_source_hash);
122+
+ return SerializedCodeSanityCheckResult::kSuccess;
123+
}
124+
125+
SerializedCodeSanityCheckResult SerializedCodeData::SanityCheckJustSource(
126+
diff --git a/src/snapshot/deserializer.cc b/src/snapshot/deserializer.cc
127+
index 09fffbbed37..8ce3b2680ef 100644
128+
--- a/src/snapshot/deserializer.cc
129+
+++ b/src/snapshot/deserializer.cc
130+
@@ -210,7 +210,6 @@ Deserializer<IsolateT>::Deserializer(IsolateT* isolate,
131+
#ifdef DEBUG
132+
num_api_references_ = GetNumApiReferences(isolate);
133+
#endif // DEBUG
134+
- CHECK_EQ(magic_number_, SerializedData::kMagicNumber);
135+
}
136+
137+
template <typename IsolateT>

Disassembler/v8dasm.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <fstream>
2+
#include <iostream>
3+
#include <string>
4+
5+
#include "include/v8.h"
6+
#include "include/libplatform/libplatform.h"
7+
8+
using namespace v8;
9+
10+
static Isolate* isolate = nullptr;
11+
12+
// Compatibility with v8 versions that have different ScriptOrigin constructors
13+
template <typename... Args>
14+
ScriptOrigin CreateScriptOrigin(Args&&... args) {
15+
if constexpr (std::is_constructible_v<ScriptOrigin, Isolate*, Local<String>>) {
16+
return ScriptOrigin(isolate, std::forward<Args>(args)...);
17+
} else {
18+
return ScriptOrigin(std::forward<Args>(args)...);
19+
}
20+
}
21+
22+
static void loadBytecode(uint8_t* bytecodeBuffer, int length) {
23+
// Load code into code cache.
24+
ScriptCompiler::CachedData* cached_data =
25+
new ScriptCompiler::CachedData(bytecodeBuffer, length);
26+
27+
// Create dummy source.
28+
ScriptOrigin origin = CreateScriptOrigin(String::NewFromUtf8Literal(isolate, "code.jsc"));
29+
30+
ScriptCompiler::Source source(String::NewFromUtf8Literal(isolate, "\"ಠ_ಠ\""),
31+
origin, cached_data);
32+
33+
// Compile code from code cache to print disassembly.
34+
MaybeLocal<UnboundScript> script = ScriptCompiler::CompileUnboundScript(
35+
isolate, &source, ScriptCompiler::kConsumeCodeCache);
36+
}
37+
38+
static void readAllBytes(const std::string& file, std::vector<char>& buffer) {
39+
std::ifstream infile(file, std::ios::binary);
40+
41+
infile.seekg(0, infile.end);
42+
size_t length = infile.tellg();
43+
infile.seekg(0, infile.beg);
44+
45+
if (length > 0) {
46+
buffer.resize(length);
47+
infile.read(&buffer[0], length);
48+
}
49+
}
50+
51+
int main(int argc, char* argv[]) {
52+
V8::SetFlagsFromString("--no-lazy --no-flush-bytecode");
53+
54+
V8::InitializeICU();
55+
std::unique_ptr<Platform> platform = platform::NewDefaultPlatform();
56+
V8::InitializePlatform(platform.get());
57+
V8::Initialize();
58+
59+
Isolate::CreateParams create_params;
60+
create_params.array_buffer_allocator =
61+
ArrayBuffer::Allocator::NewDefaultAllocator();
62+
63+
isolate = Isolate::New(create_params);
64+
Isolate::Scope isolate_scope(isolate);
65+
HandleScope handle_scope(isolate);
66+
Local<v8::Context> context = Context::New(isolate);
67+
Context::Scope context_scope(context);
68+
69+
std::vector<char> data;
70+
readAllBytes(argv[1], data);
71+
loadBytecode((uint8_t*)data.data(), data.size());
72+
}

scripts/v8dasm-builders/build-linux.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ gclient sync
8181

8282
# 应用补丁
8383
echo "=====[ Applying v8.patch ]====="
84-
PATCH_FILE="$WORKSPACE_DIR/view8-jsc-decode/Disassembler/v8.patch"
84+
PATCH_FILE="$WORKSPACE_DIR/Disassembler/v8.patch"
8585

8686
# 检查补丁是否已应用
8787
if git apply --check $PATCH_FILE 2>/dev/null; then
@@ -125,7 +125,7 @@ ninja -C out.gn/x64.release v8_monolith
125125

126126
# 编译 v8dasm
127127
echo "=====[ Compiling v8dasm ]====="
128-
DASM_SOURCE="$WORKSPACE_DIR/view8-jsc-decode/Disassembler/v8dasm.cpp"
128+
DASM_SOURCE="$WORKSPACE_DIR/Disassembler/v8dasm.cpp"
129129
OUTPUT_NAME="v8dasm-$V8_VERSION"
130130

131131
clang++ $DASM_SOURCE \

scripts/v8dasm-builders/build-macos-arm.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ gclient sync
6464

6565
# 应用补丁
6666
echo "=====[ Applying v8.patch ]====="
67-
PATCH_FILE="$WORKSPACE_DIR/view8-jsc-decode/Disassembler/v8.patch"
67+
PATCH_FILE="$WORKSPACE_DIR/Disassembler/v8.patch"
6868

6969
if git apply --check $PATCH_FILE 2>/dev/null; then
7070
git apply --verbose $PATCH_FILE
@@ -100,7 +100,7 @@ ninja -C out.gn/arm64.release v8_monolith
100100

101101
# 编译 v8dasm
102102
echo "=====[ Compiling v8dasm ]====="
103-
DASM_SOURCE="$WORKSPACE_DIR/view8-jsc-decode/Disassembler/v8dasm.cpp"
103+
DASM_SOURCE="$WORKSPACE_DIR/Disassembler/v8dasm.cpp"
104104
OUTPUT_NAME="v8dasm-$V8_VERSION"
105105

106106
clang++ $DASM_SOURCE \

scripts/v8dasm-builders/build-macos-intel.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ gclient sync
6464

6565
# 应用补丁
6666
echo "=====[ Applying v8.patch ]====="
67-
PATCH_FILE="$WORKSPACE_DIR/view8-jsc-decode/Disassembler/v8.patch"
67+
PATCH_FILE="$WORKSPACE_DIR/Disassembler/v8.patch"
6868

6969
if git apply --check $PATCH_FILE 2>/dev/null; then
7070
git apply --verbose $PATCH_FILE
@@ -100,7 +100,7 @@ ninja -C out.gn/x64.release v8_monolith
100100

101101
# 编译 v8dasm
102102
echo "=====[ Compiling v8dasm ]====="
103-
DASM_SOURCE="$WORKSPACE_DIR/view8-jsc-decode/Disassembler/v8dasm.cpp"
103+
DASM_SOURCE="$WORKSPACE_DIR/Disassembler/v8dasm.cpp"
104104
OUTPUT_NAME="v8dasm-$V8_VERSION"
105105

106106
clang++ $DASM_SOURCE \

scripts/v8dasm-builders/build-windows.cmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ call gclient sync
6565

6666
REM 应用补丁
6767
echo =====[ Applying v8.patch ]=====
68-
set PATCH_FILE=%WORKSPACE_DIR%\view8-jsc-decode\Disassembler\v8.patch
68+
set PATCH_FILE=%WORKSPACE_DIR%\Disassembler\v8.patch
6969

7070
git apply --check %PATCH_FILE% >nul 2>&1
7171
if %errorlevel% equ 0 (
@@ -94,7 +94,7 @@ call ninja -C out.gn\x64.release v8_monolith
9494

9595
REM 编译 v8dasm
9696
echo =====[ Compiling v8dasm ]=====
97-
set DASM_SOURCE=%WORKSPACE_DIR%\view8-jsc-decode\Disassembler\v8dasm.cpp
97+
set DASM_SOURCE=%WORKSPACE_DIR%\Disassembler\v8dasm.cpp
9898
set OUTPUT_NAME=v8dasm-%V8_VERSION%.exe
9999

100100
clang++ %DASM_SOURCE% ^

0 commit comments

Comments
 (0)