在使用Radare2的API r2pipe和python中的多重处理Pool.map函数时遇到问题。我面临的问题是应用程序挂在pool.join()上。
我希望通过multiprocessing.dummy类使用多线程,以便通过r2pipe快速评估函数。我尝试使用Manager类将r2pipe对象作为名称空间传递。我也尝试过使用事件,但是这些似乎都不起作用。
class Test:
def __init__(self, filename=None):
if filename:
self.r2 = r2pipe.open(filename)
else:
self.r2 = r2pipe.open()
self.r2.cmd('aaa')
def t_func(self, args):
f = args[0]
r2_ns = args[1]
print('afbj @ {}'.format(f['name']))
try:
bb = r2_ns.cmdj('afbj @ {}'.format(f['name']))
if bb:
return bb[0]['addr']
else:
return None
except Exception as e:
print(e)
return None
def thread(self):
funcs = self.r2.cmdj('aflj')
mgr = ThreadMgr()
ns = mgr.Namespace()
ns.r2 = self.r2
pool = ThreadPool(2)
results = pool.map(self.t_func, product(funcs, [ns.r2]))
pool.close()
pool.join()
print(list(results))
这是我正在使用的课程。我在主函数中调用了Test.thread函数。
我希望应用程序打印出它将在r2pipe afbj @ entry0
等中运行的命令,然后打印出包含第一个基本块地址[40000, 50000, ...]
的结果列表。
应用程序确实打印出将要运行的命令,但是在打印出结果之前挂起。
答案 0 :(得分:0)
环境
解决方案
r2 = r2pipe.open('filename')
r2.cmd('aaa')
示例
更多示例
import r2pipe
R2: r2pipe.open_sync = r2pipe.open('/bin/ls')
R2.cmd("aaaa")
FUNCS: list = R2.cmd('afbj @@f').split("\n")[:-1]
RESULTS: list = []
for func in FUNCS:
basic_block_info: list = eval(func)
first_block: dict = basic_block_info[0]
address_first_block: int = first_block['addr']
RESULTS.append(hex(address_first_block))
print(RESULTS)
'''
['0x4a56', '0x1636c', '0x3758', '0x15690', '0x15420', '0x154f0', '0x15420',
'0x154f0', '0x3780', '0x3790', '0x37a0', '0x37b0', '0x37c0', '0x37d0', '0x0',
...,
'0x3e90', '0x6210', '0x62f0', '0x8f60', '0x99e0', '0xa860', '0xc640', '0x3e70',
'0xd200', '0xd220', '0x133a0', '0x14480', '0x144e0', '0x145e0', '0x14840', '0x15cf0']
'''
更短的例子
import r2pipe
R2 = r2pipe.open('/bin/ls')
R2.cmd("aaaa")
print([hex(eval(func)[0]['addr']) for func in R2.cmd('afbj @@f').split("\n")[:-1]])