当前,对于C语言,JAVA,Python等程序语言分析的需求越来越大,我们更多的是用这些语言来处理问题,但其实每种语言都有自己的不足之处,对程序语言的不足之处进行分析,或者说对目前已有的程序漏洞进行分析的需求也已经越来越多。
本问题,我们继python提取文件中指定的代码行中所述的需要继续做分析,我们要分析solidity语言中的函数依赖,但是与存在的程序控制流图又不是完全一样,我们需要从call.value出发,找出函数的调用关系。
函数分割
根据solidity定义函数的特点,我们通过"function"来分割每个函数,并将之存储到二维数组中。
# 函数分割
def split_function(filepath): # filepath:输入需要处理的文件路径f = open(filepath, 'r')lines = f.readlines()f.close()flag = -1 # 作为二维数组下标使用for line in lines:text = line.strip() # strip是trim掉字符串两边的空格。if len(text) > 0 and text != "\n":if text.split()[0] == "function":function_list.append([text])flag += 1elif len(function_list) > 0 and "function" in function_list[flag][0]:function_list[flag].append(text)return function_list
接着,我们定义函数找出call.value所在的位置。
# 定位文件中call.value的位置
def find_location(filepath):functionList = split_function(filepath)S_count = 0for i in range(len(functionList)):for j in range(len(functionList[i])):text = functionList[i][j]if 'call.value' in text:location_i, location_j = i, j # call.value 所处的位置print("S_location: ", functionList[location_i][location_j])print("S_location_function: ", functionList[location_i])node_list.append("S" + str(S_count))S_count += 1print(node_list)
后续的,我们将继续完善函数的调用关系,主要是达到自动生成图(graph)的效果 。