《计算机应用》唯一官方网站 ›› 2024, Vol. 44 ›› Issue (7): 2151-2159.DOI: 10.11772/j.issn.1001-9081.2023070968
收稿日期:
2023-07-19
修回日期:
2023-09-25
接受日期:
2023-09-25
发布日期:
2023-10-26
出版日期:
2024-07-10
通讯作者:
冯温迪
作者简介:
孙蒴(1999—),男,河北任丘人,硕士研究生,主要研究方向:操作系统内核、计算机网络;基金资助:
Shuo SUN1, Wei ZHANG1,2,3, Wendi FENG1(), Yuwei ZHANG4
Received:
2023-07-19
Revised:
2023-09-25
Accepted:
2023-09-25
Online:
2023-10-26
Published:
2024-07-10
Contact:
Wendi FENG
About author:
SUN Shuo, born in 1999, M. S. candidate. His research interests include operating system kernels, computer networks.Supported by:
摘要:
外部函数接口(FFI)是解决一种编程语言调用其他语言函数库的主要方法。针对使用FFI技术时需要大量人工编码的问题,提出自动化外部函数接口生成(AFIG)方法。该方法利用基于抽象语法树的源码逆向分析技术,从被封装的库文件中精准提取出用于描述函数接口信息的多语言融合的统一表示。基于此统一表示,不同平台的代码生成器可利用多语言转换规则矩阵,全自动化地生成不同平台的FFI相关代码。为解决FFI代码生成中的效率低下问题,设计了一种基于依赖分析的任务聚合策略,通过把存在依赖的任务聚合为新的任务,有效消除了FFI代码任务在并行下的阻塞与死锁,从而实现任务在多核系统下的可扩展与负载均衡。实验结果表明:与人工编码相比,AFIG方法减少了FFI开发中98.14%的开发编码量以及41.95%的测试编码量;与现有的SWIG (Simplified Wrapper and Interface Generator)方法相比,在同等任务下可减少61.27%的开发成本;且生成效率随着计算资源的增加呈线性增长。
中图分类号:
孙蒴, 张伟, 冯温迪, 张俞炜. 基于源码分析的自动化外部函数接口生成方法[J]. 计算机应用, 2024, 44(7): 2151-2159.
Shuo SUN, Wei ZHANG, Wendi FENG, Yuwei ZHANG. Automatic foreign function interface generation method based on source code analysis[J]. Journal of Computer Applications, 2024, 44(7): 2151-2159.
方法 | 接口分析 | 分析对象 | 自动化完成 | ||
---|---|---|---|---|---|
编码 | 封装 | 测试 | |||
SWIG[ | × | 单个 | ○ | × | △ |
JACAW[ | △ | 单个 | △ | × | × |
ctypesgen[ | △ | 单个 | △ | × | × |
AFIG | ○ | 多个 | ○ | ○ | △ |
表1 AFIG与现有方法的对比
Tab. 1 Comparison between AFIG and existing methods
方法 | 接口分析 | 分析对象 | 自动化完成 | ||
---|---|---|---|---|---|
编码 | 封装 | 测试 | |||
SWIG[ | × | 单个 | ○ | × | △ |
JACAW[ | △ | 单个 | △ | × | × |
ctypesgen[ | △ | 单个 | △ | × | × |
AFIG | ○ | 多个 | ○ | ○ | △ |
统一表示 | JNA | Ctypes | cgo |
---|---|---|---|
int | int | c_int | C.int |
char | byte | c_wchar | C.char |
short | short | c_short | C.short |
long | long | c_long | C.long |
long long | long | c_longlong | C.longlong |
unsigned char | char | c_ubyte | C.uchar |
unsigned short | short | c_ushort | C.ushort |
unsigned int | int | c_uint | C.uint |
unsigned long | long | c_ulong | C.ulong |
int* | Pointer | c_int_p | *C.int |
char* | String/byte[] | c_char_p | *C.char |
void* | Pointer | c_void_p | unsafe.Poniter |
表2 FFI规则集中基本变量类型的映射关系
Tab. 2 Mapping relationships of basic variable types in FFI rule set
统一表示 | JNA | Ctypes | cgo |
---|---|---|---|
int | int | c_int | C.int |
char | byte | c_wchar | C.char |
short | short | c_short | C.short |
long | long | c_long | C.long |
long long | long | c_longlong | C.longlong |
unsigned char | char | c_ubyte | C.uchar |
unsigned short | short | c_ushort | C.ushort |
unsigned int | int | c_uint | C.uint |
unsigned long | long | c_ulong | C.ulong |
int* | Pointer | c_int_p | *C.int |
char* | String/byte[] | c_char_p | *C.char |
void* | Pointer | c_void_p | unsafe.Poniter |
外部接口 | 结构体数 | 宏与用户定义类型行数 | 函数量 |
---|---|---|---|
SDF | 8 | 106 | 50 |
Tesseract | 10 | 169 | 114 |
表3 数据集信息统计
Tab. 3 Dataset information statistics
外部接口 | 结构体数 | 宏与用户定义类型行数 | 函数量 |
---|---|---|---|
SDF | 8 | 106 | 50 |
Tesseract | 10 | 169 | 114 |
外部接口 | 开发完成度近似值 | 测试完成度近似值 | |||
---|---|---|---|---|---|
SWIG | AFIG | 人工 | SWIG | AFIG | |
Tesseract | 16.97 | 96.39 | 57.98 | 0 | 40.78 |
SDF | 27.31 | 98.14 | 68.52 | 0 | 41.95 |
表4 不同开发方法的代码完成度比较 ( %)
Tab. 4 Code completion comparison among different development methods
外部接口 | 开发完成度近似值 | 测试完成度近似值 | |||
---|---|---|---|---|---|
SWIG | AFIG | 人工 | SWIG | AFIG | |
Tesseract | 16.97 | 96.39 | 57.98 | 0 | 40.78 |
SDF | 27.31 | 98.14 | 68.52 | 0 | 41.95 |
项目名称 | 覆盖率 | |||
---|---|---|---|---|
路径 | 语句 | 分支 | 函数 | |
Tess4J(人工编写) | 42.4 | 72.58 | 43.75 | 73.17 |
Tess4J(AFIG生成) | 35.6 | 65.23 | 42.42 | 100.00 |
表5 测试覆盖率统计 ( %)
Tab. 5 Test coverage statistics
项目名称 | 覆盖率 | |||
---|---|---|---|---|
路径 | 语句 | 分支 | 函数 | |
Tess4J(人工编写) | 42.4 | 72.58 | 43.75 | 73.17 |
Tess4J(AFIG生成) | 35.6 | 65.23 | 42.42 | 100.00 |
图12 不同数据规模下原始策略和任务聚合与负载均衡策略的生成时间对比
Fig. 12 Generation time comparison of original strategy and task aggregation with load balancing strategy across various scales
图13 任务聚合与负载均衡策略在不同核心数与不同代码行数(LOC)下的生成时间对比
Fig. 13 Generation time comparison of task aggregation with load balancing strategy with different cores and Line of Code (LOC)
1 | WYNN D C, ECKERT C M. Perspectives on iteration in design and development [J]. Research in Engineering Design, 2017, 28(2): 153-184. |
2 | FOWLER M. Refactoring: Improving the Design of Existing Code [M]. [S.l.]: Addison-Wesley Professional, 2018: 328-332. |
3 | YALLOP J, SHEETS D, MADHAVAPEDDY A. A modular foreign function interface [J]. Science of Computer Programming, 2018, 164: 82-97. |
4 | YAN Y, GROSSMAN M, SARKAR V. JCUDA: a programmer-friendly interface for accelerating Java programs with CUDA [C]// Proceedings of the 15th International Euro-Par Conference on Parallel Processing. Berlin: Springer, 2009: 887-899. |
5 | AMATO G, SCOZZARI F. JGMP: Java bindings and wrappers for the GMP library [J]. SoftwareX, 2023, 23: 101428. |
6 | PARK J, LEE S, HONG J, et al. Static analysis of JNI programs via binary decompilation [J]. IEEE Transactions on Software Engineering, 2023, 49(5): 3089-3105. |
7 | HWANG S, LEE S, KIM J, et al. JustGen: effective test generation for unspecified JNI behaviors on JVMs [C]// Proceedings of the 2021 IEEE/ACM 43rd International Conference on Software Engineering. Piscataway: IEEE, 2021: 1708-1718. |
8 | HANSEN L T. FFIGEN Manifesto and overview [EB/OL]. (1996-02-06) [2023-08-26]. . |
9 | BEAZLEY D M. Simplified wrapper and interface generator [EB/OL]. (1996-02-26) [2023-08-26]. . |
10 | WILLIAM S F, BETTS O, NADLINGER D, et al. SWIG (Simplified Wrapper and Interface Generator) [CP/OL]. (2023-08-18) [2023-08-25]. . |
11 | ROBERTSON A. Python wrapper generator for ctypes [CP/OL]. (2022-10-19) [2023-04-19]. . |
12 | 聂鹏,耿技,秦志光.软件测试用例自动生成算法综述[J].计算机应用研究, 2012, 29(2): 401-405. |
NIE P, GENG J, QIN Z G. Survey on automatic test case generation algorithms for software testing [J]. Application Research of Computers, 2012, 29(2): 401-405. | |
13 | 顾荣,罗义力,仇伶玮,等.跨语言用户态文件系统框架读写性能优化[J].电子学报, 2023, 51(6): 1590-1606. |
GU R, LUO Y L, QIU L W, et al. Reading and writing performance optimization of cross-language FUSE framework [J]. Acta Electronica Sinica, 2023, 51(6): 1590-1606. | |
14 | DOUBROVKINE D, WALL T, GREEN A, et al. Java native library: jna [CP/OL]. (2023-03-02) [2023-04-19]. . |
15 | HELLER T. ctypeslib — useful additions to the ctypes FFI library [CP/OL]. (2021-12-17) [2023-04-19]. . |
16 | LU H, JIN C J, HELU X H, et al. AutoD: intelligent blockchain application unpacking based on JNI layer deception call [J]. IEEE Network, 2021, 35(2): 215-221. |
17 | GRICHI M, ABIDI M, JAAFAR F, et al. On the impact of inter-language dependencies in multilanguage systems empirical case study on Java Native Interface applications (JNI) [J]. IEEE Transactions on Reliability, 2021, 70(1): 428-440. |
18 | ABIDI M, RAHMAN M S, OPENJA M, et al. Are multi-language design smells fault-prone? an empirical study [J]. ACM Transactions on Software Engineering and Methodology, 2021, 30(3): No. 29. |
19 | LEE S, LEE H, RYU S. Broadening horizons of multilingual static analysis: semantic summary extraction from C code for JNI program analysis [C]// Proceedings of the 2020 35th IEEE/ACM International Conference on Automated Software Engineering. Piscataway: IEEE, 2020: 127-137. |
20 | LEE S. JNI program analysis with automatically extracted C semantic summary [C]// Proceedings of the 28th ACM SIGSOFT International Symposium on Software Testing and Analysis. New York: ACM, 2019: 448-451. |
21 | KONDOH G, ONODERA T. Finding bugs in Java native interface programs [C]// Proceedings of the 2008 International Symposium on Software Testing and Analysis. New York: ACM, 2008: 109-118. |
22 | LI S, TAN G. JET: exception checking in the Java native interface [J]. ACM SIGPLAN Notices, 2011, 46(10): 345-358. |
23 | BEAZLEY D M. Automated scientific software scripting with SWIG [J]. Future Generation Computer Systems, 2003, 19(5): 599-609. |
24 | HUANG Y, WALKER D W. JACAW: a Java-C automatic wrapper [R]. Wales, UK: Cardiff University, Department of Computer Science, 2002: 1-9. |
25 | VAIRALE V S, HONWADKAR K N. Wrapper generator using Java Native interface [J]. International Journal of Computer Science and Information Technology, 2010, 2(2): 125-139. |
26 | 单锦辉,姜瑛,孙萍.软件测试研究进展[J].北京大学学报(自然科学版), 2005, 41(1): 134-145. |
SHAN J H, JIANG Y, SUN P. Research progress in software testing [J]. Acta Scientiarum Naturalium Universitatis Pekinensis, 2005, 41(1): 134-145. | |
27 | 张海藩,吕云翔.软件工程[M].北京:人民邮电出版社, 2013: 149-175. |
ZHANG H F, LYU Y X. Software Engineering [M]. Beijing: Posts & Telecom Press, 2013: 149-175. | |
28 | LI J, ZHANG Z, YANG H. A grid oriented approach to reusing legacy code in ICENI framework [C]// Proceedings of the 2005 IEEE International Conference on Information Reuse and Integration. Piscataway: IEEE, 2005: 464-469. |
29 | BUBAK M, KURZYNIEC D, LUSZCZEK P. Creating Java to native code interfaces with Janet [J]. Scientific Programming, 2001, 9(1): 39-50. |
30 | 国家市场监督管理总局. ,信息安全技术密码设备应用接口规范 [S].北京:中国国家标准化管理委员会, 2018: 1-44. |
State Administration for Market Regulation. , Information security technology: Cryptographic device application interface specifications [S]. Beijing: Standardization Administration of the People's Republic of China, 2018: 1-44. | |
31 | 宣兆新,马旭.商用密码模块接口统一调用技术研究[J].信息安全与通信保密, 2021(10): 75-81. |
XUAN Z X, MA X. Study on interface unification of commercial cryptographic modules [J]. Information Security and Communications Privacy, 2021(10): 75-81. | |
32 | HOFFMANN M R, JANICZAK B, MANDRIKOV E. JaCoCo Java code coverage library [CP/OL]. (2023-04-18) [2023-04-19]. . |
[1] | 孙鉴, 马宝全, 吴隹伟, 杨晓焕, 武涛, 陈攀. 地震场景下无人机群路径规划与任务分配均衡联合优化[J]. 《计算机应用》唯一官方网站, 2024, 44(10): 3232-3239. |
[2] | 郎大鹏, 丁巍, 姜昊辰, 陈志远. 基于多特征融合的恶意代码分类算法[J]. 计算机应用, 2019, 39(8): 2333-2338. |
[3] | 卜同同, 曹天杰. 基于权限的Android应用风险评估方法[J]. 计算机应用, 2019, 39(1): 131-135. |
[4] | 周敏, 周安民, 刘亮, 贾鹏, 谭翠江. 组件拒绝服务漏洞自动挖掘技术[J]. 计算机应用, 2017, 37(11): 3288-3293. |
[5] | 蒋建春, 陈慧玲, 邓露, 赵建朋. 基于多核实时操作系统的配置工具设计[J]. 计算机应用, 2016, 36(3): 765-769. |
[6] | 汤杨, 曾凡平, 王健康, 黄心依. 基于静态分析的Android GUI遍历方法[J]. 计算机应用, 2016, 36(10): 2811-2815. |
[7] | 胡征宇, 沈备军. 一种移动平台儿童教育游戏的最终用户编程语言[J]. 计算机应用, 2015, 35(2): 540-544. |
[8] | 胡持, 杨庚, 杨倍思, 闵兆娥. 基于MapReduce的并行同态加密算法[J]. 计算机应用, 2015, 35(12): 3408-3412. |
[9] | 林碧英 王艳萍. 基于Hadoop的电力地理信息系统数据管理[J]. 计算机应用, 2014, 34(10): 2806-2811. |
[10] | 杜双枝 王勇 陶晓玲. 基于多片FPGA的双优先级动态调度算法[J]. 计算机应用, 2013, 33(03): 862-865. |
[11] | 汪竹 梅林 李磊 赵太银 胡光岷. 适应大规模数据处理的动态服务私有云系统[J]. 计算机应用, 2012, 32(04): 1009-1012. |
[12] | 张学锋 徐胜超. 因特网上基于节点角色的计算资源共享平台——RB-CRSP[J]. 计算机应用, 2011, 31(03): 834-838. |
[13] | 张正 刘景泰 王鸿鹏. 基于排队网络的网络服务器性能分析与优化[J]. 计算机应用, 2010, 30(12): 3148-3150. |
[14] | 吴莲贵 李肯立 易瑜. 基于CUDA的地震数据相干体并行算法[J]. 计算机应用, 2009, 29(3): 912-914. |
[15] | 黄友文. AVS解码器流水线控制机制的一种改进设计[J]. 计算机应用, 2009, 29(11): 3135-3138. |
阅读次数 | ||||||
全文 |
|
|||||
摘要 |
|
|||||