Skip to content

Commit 2faecaf

Browse files
committed
feat: patcher for d8 with disassembler
1 parent ddfeb67 commit 2faecaf

File tree

10 files changed

+452
-3
lines changed

10 files changed

+452
-3
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,27 @@ jobs:
3131
uses: ilammy/msvc-dev-cmd@v1
3232

3333
- name: Install depot_tools
34+
# language=PowerShell
3435
run: |
3536
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git C:\depot_tools
3637
echo "C:\depot_tools" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
3738
3839
- name: Fetch V8
40+
# language=PowerShell
3941
run: |
4042
fetch v8
4143
cd v8
4244
git checkout $env:V8_BRANCH
4345
gclient sync -v
4446
4547
- name: Patch V8
48+
# language=PowerShell
4649
run: |
47-
cd v8
48-
# placeholder
50+
./patcher.ps1
4951
5052
- name: Build D8
5153
id: build
54+
# language=PowerShell
5255
run: |
5356
cd v8
5457
$release = "out.gn\x64.release"
@@ -72,10 +75,11 @@ jobs:
7275
gn gen $release
7376
ninja -C $release d8
7477
75-
$artifact = "d8-$env:V8_BRANCH"
78+
$artifact = "d8-with-disassembler-$env:V8_BRANCH"
7679
Add-Content -Path $env:GITHUB_OUTPUT -Value "artifact=$artifact"
7780
7881
- name: Cleanup Artifact
82+
# language=PowerShell
7983
run: |
8084
cd v8\out.gn\x64.release
8185
Get-ChildItem

patcher.ps1

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
param (
2+
[switch]$Restore
3+
)
4+
5+
$patchesRoot = Join-Path $PSScriptRoot "patches"
6+
$sourceRoot = Join-Path $PSScriptRoot "v8\src"
7+
8+
function Restore-PatchedFile {
9+
param (
10+
[string]$SourcePath,
11+
[string]$SourcePathRelative,
12+
[string]$BackupPath
13+
)
14+
15+
if (Test-Path $BackupPath) {
16+
Copy-Item -Path $BackupPath -Destination $SourcePath -Force
17+
Remove-Item -Path $BackupPath -Force
18+
Write-Host "Restored '$SourcePathRelative'."
19+
}
20+
else
21+
{
22+
Write-Warning "Backup file '$BackupPath' does not exist for source '$SourcePath'."
23+
}
24+
}
25+
26+
function Out-PatchedFile {
27+
param (
28+
[string]$ModulePath,
29+
[string]$SourcePath,
30+
[string]$SourcePathRelative,
31+
[string]$BackupPath
32+
)
33+
34+
if (Test-Path $BackupPath)
35+
{
36+
Write-Warning "Source file '$SourcePath' has already patched."
37+
}
38+
elseif (Test-Path $SourcePath)
39+
{
40+
Copy-Item -Path $SourcePath -Destination $BackupPath -Force
41+
42+
Import-Module $ModulePath -Force
43+
$content = Get-Content $SourcePath -Raw
44+
$content = Patch $content
45+
Set-Content -Path $SourcePath -Value $content
46+
Write-Host "Patched '$SourcePathRelative'."
47+
}
48+
else
49+
{
50+
Write-Warning "Source file '$SourcePath' does not exist for patch '$ModulePath'."
51+
}
52+
}
53+
54+
Get-ChildItem -Path $patchesRoot -Recurse -Filter "*.psm1" |
55+
Where-Object { $_.DirectoryName -ne $patchesRoot } |
56+
ForEach-Object {
57+
$modulePath = $_.FullName
58+
$sourcePathRelative = $_.FullName.Substring($patchesRoot.Length + 1)
59+
$sourcePathRelative = [System.IO.Path]::ChangeExtension($sourcePathRelative, $null)
60+
$sourcePathRelative = $sourcePathRelative.Substring(0, $sourcePathRelative.Length - 1)
61+
$sourcePath = Join-Path $sourceRoot $sourcePathRelative
62+
$backupPath = "$sourcePath.bak"
63+
64+
if ($Restore)
65+
{
66+
Restore-PatchedFile -SourcePath $sourcePath `
67+
-SourcePathRelative $sourcePathRelative `
68+
-BackupPath $backupPath
69+
}
70+
else
71+
{
72+
Out-PatchedFile -ModulePath $modulePath `
73+
-SourcePath $sourcePath `
74+
-SourcePathRelative $sourcePathRelative `
75+
-BackupPath $backupPath
76+
}
77+
}

patches/d8/d8.cc.psm1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Import-Module (Join-Path $PSScriptRoot "..\utils.psm1")
2+
3+
function Patch {
4+
param([string]$Content)
5+
6+
$Content = Edit-FunctionBody -Content $Content `
7+
-FunctionName "Local<ObjectTemplate> Shell::CreateGlobalTemplate" `
8+
-Converter {
9+
param($Body)
10+
$Body = Add-BeforeReturn -Body $Body `
11+
-Insert @"
12+
global_template->Set(isolate, "loadBytecode",
13+
FunctionTemplate::New(isolate, LoadBytecode));
14+
"@
15+
return $Body
16+
}
17+
18+
$disassemble = Join-Path $PSScriptRoot "disassemble.cc"
19+
$disassemble = Get-Content -Path $disassemble -Raw
20+
$Content = Add-LineBelow -Content $Content `
21+
-Patterns @('void Shell::Print\(', '^}$') `
22+
-Insert $disassemble
23+
24+
return $Content
25+
}

patches/d8/d8.h.psm1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Import-Module (Join-Path $PSScriptRoot "..\utils.psm1")
2+
3+
function Patch {
4+
param([string]$Content)
5+
6+
$Content = Add-LineBelow -Content $Content `
7+
-Patterns @('class Shell .+', '.*public:\s*$') `
8+
-Insert @"
9+
static void LoadBytecode(const v8::FunctionCallbackInfo<v8::Value>& info);
10+
"@
11+
12+
return $Content
13+
}

patches/d8/disassemble.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
void Shell::LoadBytecode(const v8::FunctionCallbackInfo<v8::Value>& info) {
2+
auto isolate = info.GetIsolate();
3+
auto isolateInternal = reinterpret_cast<v8::internal::Isolate*>(isolate);
4+
5+
if (info.Length() < 1) {
6+
isolate->ThrowException(v8::Exception::Error(
7+
v8::String::NewFromUtf8(isolate, "No args found.").ToLocalChecked()));
8+
return;
9+
}
10+
11+
v8::String::Utf8Value filename(isolate, info[0]);
12+
if (*filename == NULL) {
13+
isolate->ThrowException(v8::Exception::Error(
14+
v8::String::NewFromUtf8(isolate, "Error creating filename.").ToLocalChecked()));
15+
return;
16+
}
17+
18+
int length = 0;
19+
auto filedata = reinterpret_cast<uint8_t*>(ReadChars(*filename, &length));
20+
if (filedata == NULL) {
21+
isolate->ThrowException(v8::Exception::Error(
22+
v8::String::NewFromUtf8(isolate, "Error reading file.").ToLocalChecked()));
23+
return;
24+
}
25+
26+
v8::internal::AlignedCachedData cached_data(filedata, length);
27+
auto source = isolateInternal->factory()
28+
->NewStringFromUtf8(base::CStrVector("source"))
29+
.ToHandleChecked();
30+
v8::internal::ScriptDetails script_details;
31+
32+
printf("===== START DESERIALIZE BYTECODE =====\n");
33+
v8::internal::CodeSerializer::Deserialize(isolateInternal, &cached_data, source, script_details);
34+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Import-Module (Join-Path $PSScriptRoot "..\utils.psm1")
2+
3+
function Patch {
4+
param([string]$Content)
5+
6+
$Content = Edit-FunctionBody -Content $Content `
7+
-FunctionName "void SharedFunctionInfo::SharedFunctionInfoPrint" `
8+
-Converter {
9+
param($Body)
10+
$Body = Set-CommentLine -Content $Body -Pattern "\s*PrintSourceCode\(os\);"
11+
$Body += "`n"
12+
$Body += @"
13+
os << "\nStart BytecodeArray\n";
14+
// TODO: isolate may be invalid
15+
this->GetActiveBytecodeArray(isolate)->Disassemble(os);
16+
os << "\nEnd BytecodeArray\n";
17+
os << std::flush;
18+
"@
19+
return $Body
20+
}
21+
22+
$Content = Edit-FunctionBody -Content $Content `
23+
-FunctionName "void HeapObject::HeapObjectShortPrint" `
24+
-Converter {
25+
param($Body)
26+
$Body = Add-LineBelow -Content $Body `
27+
-Patterns @('cage_base =') `
28+
-Insert @"
29+
Isolate* isolate = nullptr;
30+
if (!GetIsolateFromHeapObject(*this, &isolate) || isolate == nullptr) {
31+
os << "[!!! Corrupted HeapObject (cannot get Isolate) at "
32+
<< reinterpret_cast<void*>(this->ptr()) << " !!!]";
33+
return;
34+
}
35+
ReadOnlyRoots roots(isolate);
36+
Tagged<Map> map_of_this_object = this->map(cage_base);
37+
if (map_of_this_object.ptr() == kNullAddress) {
38+
os << "[!!! Corrupted HeapObject (null map pointer) at "
39+
<< reinterpret_cast<void*>(this->ptr()) << " !!!]";
40+
return;
41+
}
42+
if (map_of_this_object->map(cage_base) != roots.meta_map()) {
43+
os << "[!!! Corrupted HeapObject (invalid map) at "
44+
<< reinterpret_cast<void*>(this->ptr()) << " !!!]";
45+
return;
46+
}
47+
"@
48+
$Body = Add-LineBefore -Content $Body `
49+
-Pattern '\s*switch \(map\(cage_base\)->instance_type\(\)\) {' `
50+
-Insert @"
51+
if (map(cage_base)->instance_type() == ASM_WASM_DATA_TYPE) {
52+
os << "<ArrayBoilerplateDescription> ";
53+
Cast<ArrayBoilerplateDescription>(*this)
54+
->constant_elements()
55+
.GetHeapObject()
56+
->HeapObjectShortPrint(os);
57+
return;
58+
}
59+
"@
60+
$Body = Add-LineBelow -Content $Body `
61+
-Patterns @('case FIXED_ARRAY_TYPE:', ';') `
62+
-Insert @"
63+
os << "\nStart FixedArray\n";
64+
Cast<FixedArray>(*this)->FixedArrayPrint(os);
65+
os << "\nEnd FixedArray\n";
66+
"@
67+
$Body = Add-LineBelow -Content $Body `
68+
-Patterns @('case OBJECT_BOILERPLATE_DESCRIPTION_TYPE:', ';') `
69+
-Insert @"
70+
os << "\nStart ObjectBoilerplateDescription\n";
71+
Cast<ObjectBoilerplateDescription>(*this)
72+
->ObjectBoilerplateDescriptionPrint(os);
73+
os << "\nEnd ObjectBoilerplateDescription\n";
74+
"@
75+
$Body = Add-LineBelow -Content $Body `
76+
-Patterns @('case FIXED_DOUBLE_ARRAY_TYPE:', ';') `
77+
-Insert @"
78+
os << "\nStart FixedDoubleArray\n";
79+
Cast<FixedDoubleArray>(*this)->FixedDoubleArrayPrint(os);
80+
os << "\nEnd FixedDoubleArray\n";
81+
"@
82+
$Body = Add-LineBelow -Content $Body `
83+
-Patterns @('case SHARED_FUNCTION_INFO_TYPE:', 'else', '}') `
84+
-Insert @"
85+
os << "\nStart SharedFunctionInfo\n";
86+
shared->SharedFunctionInfoPrint(os);
87+
os << "\nEnd SharedFunctionInfo\n";
88+
"@
89+
return $Body
90+
}
91+
92+
return $Content
93+
}

patches/objects/string.cc.psm1.bak

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Import-Module (Join-Path $PSScriptRoot "..\utils.psm1")
2+
3+
function Patch {
4+
param([string]$Content)
5+
6+
$Content = Edit-FunctionBody -Content $Content `
7+
-FunctionName "void String::StringShortPrint" `
8+
-Converter {
9+
param($Body)
10+
$ifCondition = "len > kMaxShortPrintLength"
11+
$Body = Set-CommentLine -Content $Body `
12+
-Pattern $ifCondition
13+
$Body = Add-LineBelow -Content $Body `
14+
-Patterns @($ifCondition) `
15+
-Insert " /*"
16+
$Body = Add-LineBelow -Content $Body `
17+
-Patterns @($ifCondition, '}') `
18+
-Insert " */"
19+
return $Body
20+
}
21+
22+
return $Content
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Import-Module (Join-Path $PSScriptRoot "..\utils.psm1")
2+
3+
function Patch {
4+
param([string]$Content)
5+
6+
$Content = Edit-FunctionBody -Content $Content `
7+
-FunctionName "SerializedCodeSanityCheckResult SerializedCodeData::SanityCheck" `
8+
-Converter {
9+
param($Body)
10+
return " return SerializedCodeSanityCheckResult::kSuccess;"
11+
}
12+
13+
$Content = Edit-FunctionBody -Content $Content `
14+
-FunctionName "SerializedCodeSanityCheckResult SerializedCodeData::SanityCheckWithoutSource" `
15+
-Converter {
16+
param($Body)
17+
return " return SerializedCodeSanityCheckResult::kSuccess;"
18+
}
19+
20+
# $Content = Edit-FunctionBody -Content $Content `
21+
# -FunctionName ".+<SharedFunctionInfo> CodeSerializer::Deserialize" `
22+
# -Converter {
23+
# param($Body)
24+
# $Body = Add-LineBelow -Content $Body `
25+
# -Patterns @('\[Deserializing failed\]', '\s*}$') `
26+
# -Insert @"
27+
# std::cout << "\nStart SharedFunctionInfo\n";
28+
# result->SharedFunctionInfoPrint(std::cout);
29+
# std::cout << "\nEnd SharedFunctionInfo\n";
30+
# std::cout << std::flush;
31+
#"@
32+
# return $Body
33+
# }
34+
35+
return $Content
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Import-Module (Join-Path $PSScriptRoot "..\utils.psm1")
2+
3+
function Patch {
4+
param([string]$Content)
5+
6+
$deserializerSignature = "Deserializer<IsolateT>::Deserializer"
7+
$Content = Add-LineBelow -Content $Content `
8+
-Patterns @($deserializerSignature, '#endif') `
9+
-Insert " /*"
10+
$Content = Add-LineBelow -Content $Content `
11+
-Patterns @($deserializerSignature, 'CHECK_EQ') `
12+
-Insert " */"
13+
14+
return $Content
15+
}

0 commit comments

Comments
 (0)