Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix two GlobalsEncryption pass bugs #59

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

mrh929
Copy link
Contributor

@mrh929 mrh929 commented Dec 17, 2023

Fix bugs:

  1. LLVM pass crashes during compilation due to modification of LLVM's built-in global variables. GlobalsEncryption 在编译 protobuf 时由于 llvm.ctros 没有正确处理而 crash #55
  2. LLVM pass crashes during processing double variables. GlobalsEncryption 在编译 protobuf 时由于浮点型变量没有了正确处理而 crash #58

* Fix bug: LLVM pass crashes during compilation due to modification of LLVM's built-in global variables.
@@ -28,19 +28,25 @@ bool GlobalsEncryption::runOnModule(Module &M) {
INIT_CONTEXT(M);
vector<GlobalVariable *> GVs;
for (GlobalVariable &GV : M.getGlobalList()) {
// only process non llvm-generated IRs
if(GV.getName().contains("llvm"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们把 Module 内的所有 Globals 筛选出来的时候,没有对全局变量做特判,这会导致有一部分由 llvm 生成的全局变量(这里代称为 llvm-global)也放到列表中来。

其实本来这也没什么问题的,因为后续我们有语句去把这些无关的变量过滤掉。

但是,我们的 appendToGlobalCtors 函数会删除 llvm-global,再添加新的 llvm-global,这会导致之前 vector 中的 llvm-global 变成无意义指针,后续的 pass 在处理变量的时候会 crash 掉,所以我们一开始就不应该把这些变量筛选进来。

所以把有 llvm 字样的所有全局变量忽略掉就好了。

}
if(GV->getValueType()->isArrayTy()){ // the value can be array
ArrayType *ArrTy = dyn_cast<ArrayType>(GV->getValueType());
if(!ArrTy->getElementType()->isIntegerTy()){ // but the array must be integerty
Copy link
Contributor Author

@mrh929 mrh929 Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们对数组变量做筛选的时候,没有判断数组内元素是否为浮点,正好 IR 没办法拿 double、float 来做异或,所以遇到这种数,就需要跳过,不做处理。
如果后续有对浮点的加密方式,需要重新写相关逻辑。

@mrh929 mrh929 marked this pull request as ready for review December 17, 2023 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant