diff --git a/examples/execve3.py b/examples/execve3.py index eadfd47..1989704 100644 --- a/examples/execve3.py +++ b/examples/execve3.py @@ -30,11 +30,11 @@ def hello_again(ctx: c_void_p) -> c_int64: # if delta < 1000000000: # print("execve called within last second") # last().delete(key) - x = False x = True + x = False if x: print("we prevailed") - if x: + else: print("we did not prevail") ts = ktime() last().update(key, ts) @@ -52,4 +52,5 @@ def hello_again(ctx: c_void_p) -> c_int64: def LICENSE() -> str: return "GPL" + compile() diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index 16d5dd1..29a7e92 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -161,11 +161,18 @@ def handle_if(func, module, builder, stmt, map_sym_tab, local_sym_tab): start = builder.block.parent then_block = func.append_basic_block(name="if.then") merge_block = func.append_basic_block(name="if.end") + if stmt.orelse: + else_block = func.append_basic_block(name="if.else") + else: + else_block = None cond = handle_cond(func, module, builder, stmt.test, local_sym_tab, map_sym_tab) + if else_block: + builder.cbranch(cond, then_block, else_block) + else: + builder.cbranch(cond, then_block, merge_block) - builder.cbranch(cond, then_block, merge_block) builder.position_at_end(then_block) for s in stmt.body: process_stmt(func, module, builder, s, @@ -173,6 +180,14 @@ def handle_if(func, module, builder, stmt, map_sym_tab, local_sym_tab): if not builder.block.is_terminated: builder.branch(merge_block) + if else_block: + builder.position_at_end(else_block) + for s in stmt.orelse: + process_stmt(func, module, builder, s, + local_sym_tab, map_sym_tab, False) + if not builder.block.is_terminated: + builder.branch(merge_block) + builder.position_at_end(merge_block)