可信编程 – Rust语言开发的实践和愿景

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Yijun Yu"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可信编程首席专家"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"华为可信软件工程与开源实验室"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"华为爱尔兰研究所"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Amanieu d’Antras"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rust 高级专家"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"华为可信软件工程与开源实验室"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"华为爱尔兰研究所"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":"center","level":2},"content":[{"type":"text","text":"Rust 带来的创新"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"StackOverflow 的调查表明, 自 2015 年以来,Rust 一直是开发者最爱的编程语言。"}]},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/66\/fb\/66af4faa9b5bd9f285e9335084ayybfb.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"学术界对于 Rust 也越来越重视,在编程语言和软件工程顶会上发表的关于 Rust 的论文正逐年增加。"}]},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/79\/c3\/792deb9bfaa2833c67c7d6b9ff45bac3.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不仅如此,《自然》杂志 2020 年尾的文章《Why Scientists are Turning to Rust》中也强调:科学家极为推崇 Rust。"}]},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/6e\/6c\/6ed7d2792feee58434ce03eceeeb826c.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":"center","level":2},"content":[{"type":"text","text":"Rust 在华为的初步推进"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"华为的目标是引领通信系统软件向安全可信演进,其中 Rust 语言正在发挥很大的作用。例如,我们希望通过部分C\/C++代码向Rust的迁移,在保证高性能的同时,拥有更高的安全性。在此过程中, 我们为开发者提供一套自动化工具支持:基于开源的 C2Rust 转译工具, 首先从 C 代码生成 Rust 代码, 然后通过源到源变换工具自动重构。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"华为内部还基于 actor 的并发编程模式开发了 Rust 库,例如async, await,方便程序员充分利用 Rust 的语言特性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"华为的通信系统软件开发以 C\/C++ 代码为主, 在需要的时候,这些 Rust 库将使 C\/C++ 到 Rust 的迁移更加顺畅。作为业界领先公司和 Rust 基金会创始成员,华为致力于推进 Rust 在通信软件行业的发展,并将持续为 Rust 社区做出贡献。"}]},{"type":"heading","attrs":{"align":"center","level":2},"content":[{"type":"text","text":"华为对 Rust 社区的贡献"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们为 Rust 社区贡献了许多重要的功能特性。例如,我们最近为 Rust 编译器提交了一系列代码,使得 Rust 编译目标可以支持 ARM AArch64 32 位大端变体 ILP32 芯片组, 以便用于我们的通信产品中。这些改进使得我们和友商可以在这些常用网络硬件架构上执行 Rust 原生程序。这些代码已经通过我们的 Rust 专家 Amanieu d’Antras 提交给了 LLVM 编译器, libc 库, 以及 Rust 编译器等开源项目。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这些对 Rust 编译器的更改引入了新的端到端交叉编译目标,基于此针对定制硬件构建 Rust 产品变得更容易, 只需要简单的命令,比如:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"cargo build --target aarch64_be-unknown-linux-gnucargo build --target aarch64-unknown-linux-gnu_ilp32cargo build --target aarch64_be-unknown-linux-gnu_ilp32"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"华为在中国 Rust 社区贡献方面也走在前列。去年 12 月 26 日至 27 日,在深圳战略赞助了Rust China Conf 2020 ,并推行多项社区活动,包括为中国的开发者提供 Rust 教程和 Rust 编码规范。"}]},{"type":"heading","attrs":{"align":"center","level":2},"content":[{"type":"text","text":"配置华为的端到端 Rust 工具链"}]},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/9d\/61\/9dbb0de6yy631016d693c9c08fb97d61.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","text":"(C、C++、Rust 代码在 Fuchsia 项目的占比)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rust 社区中有几种端到端的工具,我们已经开始从开发人员和工具的交互中获取信息。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里有一些例子"}]},{"type":"heading","attrs":{"align":"center","level":3},"content":[{"type":"text","text":"tokei"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由于可信编程项目通常涉及多个编程语言,我们采用了 tokei 作为多语言代码复杂性度量工具,可识别多达 200 种编程语言。例如,开源的 Fuchsia 项目涉及了多种编程语言,下面的统计信息显示有多少行不同语言的代码:"}]},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/52\/9f\/52cf6633e5f6555e06d7fcf2416f3c9f.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另外,为了在大型项目中满足处理多种编程语言的场景需求,我们为tokei 提供新特性,使其支持识别编程语言的批处理。"}]},{"type":"heading","attrs":{"align":"center","level":3},"content":[{"type":"text","text":"cargo-geiger"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为了提高安全性,我们经常想知道有多少代码已经被 Rust 编译器检查过。幸运的是,通过统计带有“unsafe”关键字的fn、expr,struct、impl、trait 及其在各相关库, cargo-geiger 几乎做到了这点。"}]},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/c6\/a5\/c6fd5bb0c023ebbaabcb80bbb70c48a5.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不过,统计数字中并没有反映安全性,所以没办法展现 Rust 项目总体上取得了多少进展的比例。因此,我们提交了代码,在改进的 cargo-geiger 计数器报告中提供 Rust 项目的安全检查比率。这些代码被采纳后,我们的研发团队现在每天都在使用这个工具,这份典型的报告能够很容易看出哪些代码库还没被 Rust 编译器完全检查到。"}]},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/4d\/fb\/4d114b16abc33a41936a2682471502fb.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/ec\/8f\/ecc59855246c49737c844b75d8fcff8f.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":"center","level":2},"content":[{"type":"text","text":"通过深度代码学习研究 Rust"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"随着 Rust 开源社区代码的发展和革新,初学者需要学习掌握 Rust 最佳的实践,其包括但不限于 Rust 语言本身。把统计机器学习的方法应用到源代码数据上,也称为Big Code,正被全世界的软件工程研究团队关注:类似于图像处理和自然语言处理中的机器学习问题,这些问题都需要通过深度神经网络 (deep neural networks DNN) 提取大量的特征,Big Code 可能同样需要去训练 DNN 来反映程序的统计特性,所以也称为“深度代码学习”。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在这方面,华为与英国开放大学和新加坡管理大学进行技术合作,在现在最先进的“跨语言”深度代码学习基础上进行优化研究。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如,最初的深度代码学习方法应用于北京大学编程课程收集到的 104 个算法类的 5.2 万个 C\/C++ 程序。对此数据集,树基卷积神经网络 (TBCNN) 算法分类准确率达到 94%(AAAI’16)。最近的 SOTA 在语句级使用抽象语法树 (ICSE ’19) 准确率达到 98%。近期我们同英国开放大学和新加坡管理大学在树基胶囊网络的合作研究进展推动了 SOTA 进一步提高,达到 98.4% 的准确率 (AAAI’21)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"早些时候我们已经使用跨语言的数据集证明,对一种编程语言的深度代码学习模型也适用于另一种编程语言。例如,从 GitHub 爬取的数据集 Rosetta Code,从 Java 到 C 语言,可以获得 86% 的算法分类准确度 (SANER’19),在 Java 到 C# 的跨语言 API 映射问题中也能发挥重要作用 (ESEC\/FSE’19)。这些统计语言模型在软件工程中可以应用于很多方面,比如代码分类、代码搜索、代码推荐、代码摘要、方法名称预测、代码克隆检测等等 (ICSE’21)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为了进一步研究分析 Rust 项目,我们向 Rust 解析器项目 tree-sitter 和 XML 序列化 quick-xml 等项目提交了代码,通过 Rust 程序的抽象语法树来训练深度代码学习模型。研究的初步结果显示,算法检测任务在 Rust 代码上的精度高达 85.5%。随着工具链的改进,这个比例还有望进一步提升。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Visual Studio Code IDE 上,我们开发扩展插件,使得程序员可以得到合适的算法推荐和可解释性的帮助。"}]},{"type":"heading","attrs":{"align":"center","level":2},"content":[{"type":"text","text":"结      论"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"综上所述,华为可信软件工程与开源实验室正在开展的 Rust 工作为程序员提供智能化端到端 IDE 工具链,以期最大限度地提高代码的安全性和性能。走向可信编程远景的旅程刚刚开始,我们希望与 Rust 社区和 Rust 基金会深度合作,引领电信软件产业的可信革新。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"英文原文链接"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"https:\/\/trusted-programming.github.io\/2021-02-07\/index.html"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章