-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement -reserved-code-size=<size> option to reserve code space tha…
…t can be patched in the binary
- Loading branch information
Showing
14 changed files
with
156 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
def RESERVED_CODE_SIZE = 4096; | ||
|
||
var result = 0; | ||
|
||
def main(args: Array<string>) -> int { | ||
if (args.length > 0) do_patch(args[0]); | ||
else do_run(); | ||
return result; | ||
} | ||
|
||
def do_patch(fileName: string) { | ||
begin("loading ", fileName); | ||
|
||
var data = System.fileLoad(fileName); | ||
if (data == null || data.length == 0) return fail("could not load"); | ||
|
||
ok(); | ||
|
||
begin("code size check", null); | ||
var gotSize = int.!(CiRuntime.RESERVED_CODE_END - CiRuntime.RESERVED_CODE_START); | ||
if (RESERVED_CODE_SIZE == gotSize) ok(); | ||
else return fail("size does not match"); | ||
|
||
var offset = CiRuntime.RESERVED_CODE_FILE_OFFSET - Pointer.NULL; | ||
var region = data[offset ..+ RESERVED_CODE_SIZE]; | ||
|
||
begin("patching", null); | ||
write(region, getMachineCode()); | ||
ok(); | ||
|
||
begin("writing", null); | ||
var fd = System.fileOpen(fileName, false); | ||
if (fd <= 0) return fail("failed to open for writing"); | ||
|
||
System.write(fd, data); | ||
System.fileClose(fd); | ||
ok(); | ||
} | ||
|
||
def write(region: Range<byte>, data: Range<byte>) { | ||
for (i < data.length) region[i] = data[i]; | ||
} | ||
|
||
def begin(str1: string, str2: string) { | ||
System.puts("##+"); | ||
System.puts(str1); | ||
if (str2 != null) System.puts(str2); | ||
System.ln(); | ||
} | ||
|
||
def ok() { | ||
System.puts("##-ok\n"); | ||
} | ||
|
||
def fail(msg: string) { | ||
System.puts("##-fail: "); | ||
if (msg != null) System.puts(msg); | ||
System.ln(); | ||
result |= 1; | ||
} | ||
|
||
def do_run() { | ||
var region = CiRuntime.forgeRange<byte>(CiRuntime.RESERVED_CODE_START, | ||
int.!(CiRuntime.RESERVED_CODE_END - CiRuntime.RESERVED_CODE_START)); | ||
|
||
begin("checking code", null); | ||
var expected = getMachineCode(); | ||
if (region.length == 0) return fail("no reserved code region"); | ||
if (region.length < expected.length) return fail("reserved code region too small"); | ||
for (i < expected.length) { | ||
if (region[i] != expected[i]) return fail("machine code does not match expectation"); | ||
} | ||
ok(); | ||
|
||
begin("running", null); | ||
var codePtr = CiRuntime.RESERVED_CODE_START; | ||
var f: void -> int = CiRuntime.forgeClosure<void, void, int>(codePtr, ()); | ||
var got = f(); | ||
if (got == 42) ok(); | ||
else fail("got wrong value"); | ||
} | ||
|
||
//========================================================================================== | ||
//== Target-specific configuration ========================================================= | ||
//========================================================================================== | ||
|
||
// Retarget this test by passing -redef-field=TARGET_<target>=true | ||
def TARGET_x86_linux = false; | ||
def TARGET_x86_darwin = false; | ||
def TARGET_x86_64_linux = false; | ||
def TARGET_x86_64_darwin = false; | ||
|
||
def TARGET_x86 = TARGET_x86_linux || TARGET_x86_darwin; | ||
def TARGET_x86_64 = TARGET_x86_64_linux || TARGET_x86_64_darwin; | ||
|
||
def getMachineCode() -> Array<byte> { | ||
if (TARGET_x86 || TARGET_x86_64) return [ | ||
0xB8, 0x2A, 0x00, 0x00, 0x00, // mov eax, 42 | ||
0xc3 // ret | ||
]; | ||
var x = 1/0; | ||
return null; // no other targets supported now | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
##+checking code | ||
##-fail: machine code does not match expectation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-reserved-code-size=4K |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters