《计算机应用》唯一官方网站

• •    下一篇

基于源码分析的自动化外部函数接口生成方法

孙蒴1,张伟1,冯温迪1,张俞炜2   

  1. 1. 北京信息科技大学
    2. 北京大学
  • 收稿日期:2023-07-19 修回日期:2023-09-19 发布日期:2023-10-26 出版日期:2023-10-26
  • 通讯作者: 孙蒴

Automatic foreign function interface generation method based on source code analysis

  • Received:2023-07-19 Revised:2023-09-19 Online:2023-10-26 Published:2023-10-26

摘要: 外部函数接口(FFI)是解决一种编程语言调用其他语言函数库的主要方法。针对使用FFI技术时需要大量人工编码的问题,提出了自动化外部函数接口生成方法(AFIG)。该方法利用基于抽象语法树的源码逆向分析技术,从被封装的库文件中精准提取出用于描述函数接口信息的多语言融合的统一表示。基于此统一表示,不同平台的代码生成器可利用多语言转换规则矩阵,全自动化地生成不同平台的FFI相关代码。为解决FFI代码生成中的效率低下问题,设计了一种基于依赖分析的任务聚合策略,通过把存在依赖的任务聚合为新的任务,有效消除了FFI代码任务在并行下的阻塞与死锁,从而实现任务在多核下的可扩展与负载均衡。实验结果表明:AFIG减少了FFI开发中98.14%的开发编码量以及41.95%的测试编码量;与现有的SWIG方法相比,在同等任务下可减少61.27%的开发成本,且使其生成效率随着计算资源的增加呈线性增长。

Abstract: Foreign Function Interface (FFI) is a fundamental method to invoke interfaces provided in another programming languages. Focusing on the issue that huge amount of manual coding was required when using FFI, Automatic a Foreign function Interface Generation (AFIG) was proposed. A reverse source code analysis technique based on the abstract syntax tree was employed by AFIG to accurately retrieve the multilingual intermediate representation from library binaries, in which function interface information was described. Based on the representation, the multilingual conversion rule matrix could be utilized by different platform code generators to automatically generate FFI codes for various platforms without handcrafting. To further reduce generation time usage, a dependency analysis-based task aggregation strategy was proposed, by which tasks with dependencies were consolidated as monolithic ones. Hence, blocking and deadlocks were efficiently eliminated, and load balancing and scalability on multi-core systems were achieved, accordingly. Experimental results indicate that a reduction of 98.14% for FFI manual codes and 41.95% for testing codes are achieved. Compared to SWIG, AFIG can further reduce development cost by 61.27% under the same task. Besides, the code generation mechanism is proven to be scalable because experimental results indicate a linear time reduction as the number of central processing unit cores increases.

中图分类号: