Skip to content

Commit f674de3

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

File tree

10 files changed

+396
-3
lines changed

10 files changed

+396
-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(isolate)->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-LineBefore -Content $Body `
26+
-Pattern '\s*switch \(map\(cage_base\)->instance_type\(\)\) {' `
27+
-Insert @"
28+
if (map(cage_base).instance_type() == ASM_WASM_DATA_TYPE) {
29+
os << "<ArrayBoilerplateDescription> ";
30+
Cast<ArrayBoilerplateDescription>(*this)
31+
->constant_elements()
32+
->HeapObjectShortPrint(os);
33+
return;
34+
}
35+
"@
36+
$Body = Add-LineBelow -Content $Body `
37+
-Patterns @('case FIXED_ARRAY_TYPE:', ';') `
38+
-Insert @"
39+
os << "\nStart FixedArray\n";
40+
Cast<FixedArray>(*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+
Cast<ObjectBoilerplateDescription>(*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+
Cast<FixedDoubleArray>(*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: 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)