Skip to content

Commit d12c973

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

File tree

10 files changed

+368
-3
lines changed

10 files changed

+368
-3
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ jobs:
4444
4545
- name: Patch V8
4646
run: |
47-
cd v8
48-
# placeholder
47+
./patcher.ps1
4948
5049
- name: Build D8
5150
id: build
@@ -72,7 +71,7 @@ jobs:
7271
gn gen $release
7372
ninja -C $release d8
7473
75-
$artifact = "d8-$env:V8_BRANCH"
74+
$artifact = "d8-with-disassembler-$env:V8_BRANCH"
7675
Add-Content -Path $env:GITHUB_OUTPUT -Value "artifact=$artifact"
7776
7877
- name: Cleanup Artifact

patcher.ps1

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

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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
this->GetActiveBytecodeArray().Disassemble(os);
15+
os << "\nEnd BytecodeArray\n";
16+
os << std::flush;
17+
"@
18+
return $Body
19+
}
20+
21+
$Content = Edit-FunctionBody -Content $Content `
22+
-FunctionName "void HeapObject::HeapObjectShortPrint" `
23+
-Converter {
24+
param($Body)
25+
$Body = Add-LineBelow -Content $Body `
26+
-Patterns @('\s*switch \(map\(cage_base\)->instance_type\(\)\) {') `
27+
-Insert @"
28+
case ASM_WASM_DATA_TYPE: {
29+
os << "<ArrayBoilerplateDescription> ";
30+
ArrayBoilerplateDescription::cast(*this)
31+
.constant_elements()
32+
.HeapObjectShortPrint(os);
33+
break;
34+
}
35+
"@
36+
$Body = Add-LineBelow -Content $Body `
37+
-Patterns @('case FIXED_ARRAY_TYPE:', ';') `
38+
-Insert @"
39+
os << "\nStart FixedArray\n";
40+
FixedArray::cast(*this).FixedArrayPrint(os);
41+
os << "\nEnd FixedArray\n";
42+
"@
43+
$Body = Add-LineBelow -Content $Body `
44+
-Patterns @('case OBJECT_BOILERPLATE_DESCRIPTION_TYPE:', ';') `
45+
-Insert @"
46+
os << "\nStart ObjectBoilerplateDescription\n";
47+
ObjectBoilerplateDescription::cast(*this)
48+
.ObjectBoilerplateDescriptionPrint(os);
49+
os << "\nEnd ObjectBoilerplateDescription\n";
50+
"@
51+
$Body = Add-LineBelow -Content $Body `
52+
-Patterns @('case FIXED_DOUBLE_ARRAY_TYPE:', ';') `
53+
-Insert @"
54+
os << "\nStart FixedDoubleArray\n";
55+
FixedDoubleArray::cast(*this).FixedDoubleArrayPrint(os);
56+
os << "\nEnd FixedDoubleArray\n";
57+
"@
58+
$Body = Add-LineBelow -Content $Body `
59+
-Patterns @('case SHARED_FUNCTION_INFO_TYPE:', 'else', '}') `
60+
-Insert @"
61+
os << "\nStart SharedFunctionInfo\n";
62+
shared.SharedFunctionInfoPrint(os);
63+
os << "\nEnd SharedFunctionInfo\n";
64+
"@
65+
return $Body
66+
}
67+
68+
return $Content
69+
}

patches/objects/string.cc.psm1

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 ".+<SharedFunctionInfo> CodeSerializer::Deserialize" `
15+
-Converter {
16+
param($Body)
17+
$Body = Add-LineBelow -Content $Body `
18+
-Patterns @('\[Deserializing failed\]', '\s*}$') `
19+
-Insert @"
20+
std::cout << "\nStart SharedFunctionInfo\n";
21+
result->SharedFunctionInfoPrint(std::cout);
22+
std::cout << "\nEnd SharedFunctionInfo\n";
23+
std::cout << std::flush;
24+
"@
25+
return $Body
26+
}
27+
28+
return $Content
29+
}
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+
}

patches/utils.psm1

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
function Edit-FunctionBody {
2+
param(
3+
[Parameter(Mandatory)][string]$Content,
4+
[Parameter(Mandatory)][string]$FunctionName,
5+
[Parameter(Mandatory)][ScriptBlock]$Converter
6+
)
7+
8+
$methodPattern = "$FunctionName\s*\([^)]*\)\s*(const\s*)?{"
9+
10+
$match = [regex]::Match($Content, $methodPattern)
11+
if (-not $match.Success) {
12+
Write-Warning "Editing function '$FunctionName' is not found."
13+
return $Content
14+
}
15+
16+
$startIndex = $match.Index + $match.Length - 1
17+
$braceCount = 1
18+
$i = $startIndex + 1
19+
while ($i -lt $Content.Length -and $braceCount -gt 0) {
20+
if ($Content[$i] -eq '{') { $braceCount++ }
21+
elseif ($Content[$i] -eq '}') { $braceCount-- }
22+
$i++
23+
}
24+
$endIndex = $i
25+
26+
$originalBody = $Content.Substring($startIndex + 1, $endIndex - $startIndex - 2).Trim()
27+
$newBody = & $Converter $originalBody
28+
$newContent = $Content.Substring(0, $startIndex + 1) + "`n$newBody`n" + $Content.Substring($endIndex - 1)
29+
return $newContent
30+
}
31+
32+
function Add-BeforeReturn {
33+
param(
34+
[Parameter(Mandatory)][string]$Body,
35+
[Parameter(Mandatory)][string]$Insert
36+
)
37+
38+
$lines = $Body -split "`r?`n"
39+
$newLines = @()
40+
41+
foreach ($line in $lines) {
42+
if ($line -match '^\s*return\s+.+;$') {
43+
$newLines += $Insert
44+
}
45+
$newLines += $line
46+
}
47+
48+
return ($newLines -join "`n")
49+
}
50+
51+
function Add-LineBelow {
52+
[CmdletBinding()]
53+
param(
54+
[Parameter(Mandatory)][string]$Content,
55+
[Parameter(Mandatory)][string[]]$Patterns,
56+
[Parameter(Mandatory)][string]$Insert
57+
)
58+
59+
$eol = if ($Content -match "`r`n") { "`r`n" } else { "`n" }
60+
$lines = $Content -split "`r?`n"
61+
62+
$searchStart = 0
63+
$lastMatchIndex = -1
64+
foreach ($pattern in $Patterns) {
65+
$found = $false
66+
for ($i = $searchStart; $i -lt $lines.Count; $i++) {
67+
if ($lines[$i] -match $pattern) {
68+
$lastMatchIndex = $i
69+
$searchStart = $i + 1
70+
$found = $true
71+
break
72+
}
73+
}
74+
if (-not $found) {
75+
return $Content
76+
}
77+
}
78+
79+
$insertAt = $lastMatchIndex + 1
80+
$before = if ($lastMatchIndex -ge 0) { $lines[0..$lastMatchIndex] } else { @() }
81+
$after = if ($insertAt -lt $lines.Count) { $lines[$insertAt..($lines.Count-1)] } else { @() }
82+
$insertLines = $Insert -split "`r?`n"
83+
84+
$newLines = @()
85+
$newLines += $before
86+
$newLines += $insertLines
87+
$newLines += $after
88+
89+
return ($newLines -join $eol)
90+
}
91+
92+
function Set-CommentLine {
93+
[CmdletBinding()]
94+
param(
95+
[Parameter(Mandatory)][string]$Content,
96+
[Parameter(Mandatory)][string]$Pattern
97+
)
98+
99+
$eol = if ($Content -match "`r`n") { "`r`n" } else { "`n" }
100+
$lines = $Content -split "`r?`n"
101+
for ($i = 0; $i -lt $lines.Count; $i++) {
102+
if ($lines[$i] -match $Pattern) {
103+
$lines[$i] = $lines[$i] -replace '^( *)', '${1}// '
104+
break
105+
}
106+
}
107+
return ($lines -join $eol)
108+
}

0 commit comments

Comments
 (0)