Skip to content

Commit 63d8e64

Browse files
committed
ruff reformatting
1 parent 4d7580e commit 63d8e64

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

src/patcherex2/targets/elf_x86_64_linux.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ def detect_target(binary_path):
2323
): # EM_X86_64
2424
return True
2525
return False
26-
26+
2727
def get_assembler(self, assembler):
28-
assembler = assembler or "keystone"
29-
if assembler == "keystone":
30-
return Keystone(
31-
self.p, keystone.KS_ARCH_X86, keystone.KS_MODE_LITTLE_ENDIAN + keystone.KS_MODE_64
32-
)
33-
raise NotImplementedError()
28+
assembler = assembler or "keystone"
29+
if assembler == "keystone":
30+
return Keystone(
31+
self.p,
32+
keystone.KS_ARCH_X86,
33+
keystone.KS_MODE_LITTLE_ENDIAN + keystone.KS_MODE_64,
34+
)
35+
raise NotImplementedError()
3436

3537
def get_allocation_manager(self, allocation_manager):
3638
allocation_manager = allocation_manager or "default"
@@ -47,7 +49,10 @@ def get_compiler(self, compiler):
4749
def get_disassembler(self, disassembler):
4850
disassembler = disassembler or "capstone"
4951
if disassembler == "capstone":
50-
return Capstone(capstone.CS_ARCH_X86, capstone.CS_MODE_LITTLE_ENDIAN + capstone.CS_MODE_64)
52+
return Capstone(
53+
capstone.CS_ARCH_X86,
54+
capstone.CS_MODE_LITTLE_ENDIAN + capstone.CS_MODE_64,
55+
)
5156
raise NotImplementedError()
5257

5358
def get_binfmt_tool(self, binfmt_tool):

tests/test_x86_64.py

+44-44
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def test_modify_instruction_patch(self):
4343
self.run_one(
4444
"printf_nopie",
4545
[
46-
ModifyInstructionPatch(0x40113e, "lea rax, [0x402007]"),
46+
ModifyInstructionPatch(0x40113E, "lea rax, [0x402007]"),
4747
ModifyInstructionPatch(0x401145, "mov rsi, rax"),
4848
],
4949
expected_output=b"%s",
5050
expected_returnCode=0,
5151
)
52-
52+
5353
def test_insert_instruction_patch(self):
5454
instrs = """
5555
mov rax, 0x1
@@ -60,7 +60,7 @@ def test_insert_instruction_patch(self):
6060
"""
6161
self.run_one(
6262
"printf_nopie",
63-
[InsertInstructionPatch(0x40115c, instrs)],
63+
[InsertInstructionPatch(0x40115C, instrs)],
6464
expected_output=b"Hi\x00Hi",
6565
expected_returnCode=0,
6666
)
@@ -75,7 +75,7 @@ def test_insert_instruction_patch_2(self):
7575
"printf_nopie",
7676
[
7777
InsertInstructionPatch("return_0x32", instrs),
78-
ModifyInstructionPatch(0x40115c, "jmp {return_0x32}"),
78+
ModifyInstructionPatch(0x40115C, "jmp {return_0x32}"),
7979
],
8080
expected_returnCode=0x32,
8181
)
@@ -107,7 +107,7 @@ def test_insert_data_patch(self, tlen=5):
107107
mov rdx, %s
108108
syscall
109109
""" % hex(tlen)
110-
p2 = InsertInstructionPatch(0x40115c, instrs)
110+
p2 = InsertInstructionPatch(0x40115C, instrs)
111111
self.run_one(
112112
"printf_nopie",
113113
[p1, p2],
@@ -160,45 +160,45 @@ def test_replace_function_patch_with_function_reference_and_rodata(self):
160160
)
161161

162162
def run_one(
163-
self,
164-
filename,
165-
patches,
166-
set_oep=None,
167-
inputvalue=None,
168-
expected_output=None,
169-
expected_returnCode=None,
170-
):
171-
filepath = os.path.join(self.bin_location, filename)
172-
pipe = subprocess.PIPE
173-
174-
with tempfile.TemporaryDirectory() as td:
175-
tmp_file = os.path.join(td, "patched")
176-
p = Patcherex(filepath)
177-
for patch in patches:
178-
p.patches.append(patch)
179-
p.apply_patches()
180-
p.binfmt_tool.save_binary(tmp_file)
181-
# os.system(f"readelf -hlS {tmp_file}")
182-
183-
p = subprocess.Popen(
184-
[tmp_file],
185-
stdin=pipe,
186-
stdout=pipe,
187-
stderr=pipe,
188-
)
189-
res = p.communicate(inputvalue)
190-
if expected_output:
191-
if res[0] != expected_output:
192-
self.fail(
193-
f"AssertionError: {res[0]} != {expected_output}, binary dumped: {self.dump_file(tmp_file)}"
194-
)
195-
# self.assertEqual(res[0], expected_output)
196-
if expected_returnCode:
197-
if p.returncode != expected_returnCode:
198-
self.fail(
199-
f"AssertionError: {p.returncode} != {expected_returnCode}, binary dumped: {self.dump_file(tmp_file)}"
200-
)
201-
# self.assertEqual(p.returncode, expected_returnCode)
163+
self,
164+
filename,
165+
patches,
166+
set_oep=None,
167+
inputvalue=None,
168+
expected_output=None,
169+
expected_returnCode=None,
170+
):
171+
filepath = os.path.join(self.bin_location, filename)
172+
pipe = subprocess.PIPE
173+
174+
with tempfile.TemporaryDirectory() as td:
175+
tmp_file = os.path.join(td, "patched")
176+
p = Patcherex(filepath)
177+
for patch in patches:
178+
p.patches.append(patch)
179+
p.apply_patches()
180+
p.binfmt_tool.save_binary(tmp_file)
181+
# os.system(f"readelf -hlS {tmp_file}")
182+
183+
p = subprocess.Popen(
184+
[tmp_file],
185+
stdin=pipe,
186+
stdout=pipe,
187+
stderr=pipe,
188+
)
189+
res = p.communicate(inputvalue)
190+
if expected_output:
191+
if res[0] != expected_output:
192+
self.fail(
193+
f"AssertionError: {res[0]} != {expected_output}, binary dumped: {self.dump_file(tmp_file)}"
194+
)
195+
# self.assertEqual(res[0], expected_output)
196+
if expected_returnCode:
197+
if p.returncode != expected_returnCode:
198+
self.fail(
199+
f"AssertionError: {p.returncode} != {expected_returnCode}, binary dumped: {self.dump_file(tmp_file)}"
200+
)
201+
# self.assertEqual(p.returncode, expected_returnCode)
202202

203203
def dump_file(self, file):
204204
shutil.copy(file, "/tmp/patcherex_failed_binary")

0 commit comments

Comments
 (0)