-
-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add the "regen-frozen" makefile target that regenerates the code for the frozen __hello__ module.
- Loading branch information
Showing
6 changed files
with
82 additions
and
21 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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Build/2020-01-24-12-54-22.bpo-39448.k4pv14.rst
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 @@ | ||
Add the "regen-frozen" makefile target that regenerates the code for the | ||
frozen ``__hello__`` module. |
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,13 @@ | ||
/* Generated with Tools/freeze/regen_frozen.py */ | ||
static unsigned char M___hello__[] = { | ||
227,0,0,0,0,0,0,0,0,0,0,0,0, | ||
0,0,0,0,2,0,0,0,64,0,0,0,115, | ||
16,0,0,0,100,0,90,0,101,1,100,1,131, | ||
1,1,0,100,2,83,0,41,3,84,122,12,72, | ||
101,108,108,111,32,119,111,114,108,100,33,78,41, | ||
2,90,11,105,110,105,116,105,97,108,105,122,101, | ||
100,218,5,112,114,105,110,116,169,0,114,2,0, | ||
0,0,114,2,0,0,0,218,4,110,111,110,101, | ||
218,8,60,109,111,100,117,108,101,62,1,0,0, | ||
0,115,2,0,0,0,4,1, | ||
}; |
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,51 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
import os | ||
import marshal | ||
|
||
|
||
DIR = os.path.dirname(sys.argv[0]) | ||
# source code for module to freeze | ||
FILE = os.path.join(DIR, 'flag.py') | ||
# C symbol to use for array holding frozen bytes | ||
SYMBOL = 'M___hello__' | ||
|
||
|
||
def get_module_code(filename): | ||
"""Compile 'filename' and return the module code as a marshalled byte | ||
string. | ||
""" | ||
with open(filename, 'r') as fp: | ||
src = fp.read() | ||
co = compile(src, 'none', 'exec') | ||
co_bytes = marshal.dumps(co) | ||
return co_bytes | ||
|
||
|
||
def gen_c_code(fp, co_bytes): | ||
"""Generate C code for the module code in 'co_bytes', write it to 'fp'. | ||
""" | ||
def write(*args, **kwargs): | ||
print(*args, **kwargs, file=fp) | ||
write('/* Generated with Tools/freeze/regen_frozen.py */') | ||
write('static unsigned char %s[] = {' % SYMBOL, end='') | ||
bytes_per_row = 13 | ||
for i, opcode in enumerate(co_bytes): | ||
if (i % bytes_per_row) == 0: | ||
# start a new row | ||
write() | ||
write(' ', end='') | ||
write('%d,' % opcode, end='') | ||
write() | ||
write('};') | ||
|
||
|
||
def main(): | ||
out_filename = sys.argv[1] | ||
co_bytes = get_module_code(FILE) | ||
with open(out_filename, 'w') as fp: | ||
gen_c_code(fp, co_bytes) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |