-
Notifications
You must be signed in to change notification settings - Fork 185
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
mrh929
wants to merge
7
commits into
bluesadi:main
Choose a base branch
from
mrh929:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
74fc85e
Fix bug (#1)
mrh929 a838b89
fix bug: float value is wrongly processed (#2)
mrh929 09775c2
ignore functions that contain exception handling and phi instructions…
mrh929 aa75362
Apply llvm flat only on functions whose terminators are br and ret.
mrh929 bbafdc1
Merge pull request #3 from mrh929/dev
mrh929 6bb1c37
ignore intdata in GlobalsEncryption(bug)
mrh929 fcaa2ba
Merge pull request #4 from mrh929/dev
mrh929 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")) | ||
continue; | ||
GVs.push_back(&GV); | ||
} | ||
for (int i = 0; i < ObfuTimes; i++) { | ||
for (GlobalVariable *GV : GVs) { | ||
// Only encrypt globals of integer and array | ||
if (!GV->getValueType()->isIntegerTy() && | ||
!GV->getValueType()->isArrayTy()) { | ||
continue; | ||
} | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我们对数组变量做筛选的时候,没有判断数组内元素是否为浮点,正好 IR 没办法拿 double、float 来做异或,所以遇到这种数,就需要跳过,不做处理。 |
||
continue; | ||
} | ||
} | ||
else if (!GV->getValueType()->isIntegerTy()){ // or, the value must be integerty | ||
continue; | ||
} | ||
|
||
if (GV->hasInitializer() && GV->getInitializer() && | ||
(GV->getName().contains(".str") || !OnlyStr) | ||
// Do not encrypt globals having a section named "llvm.metadata" | ||
&& !GV->getSection().equals("llvm.metadata")) { | ||
(GV->getName().contains(".str") || !OnlyStr)) { | ||
Constant *initializer = GV->getInitializer(); | ||
ConstantInt *intData = dyn_cast<ConstantInt>(initializer); | ||
ConstantDataArray *arrData = dyn_cast<ConstantDataArray>(initializer); | ||
|
@@ -61,6 +67,7 @@ bool GlobalsEncryption::runOnModule(Module &M) { | |
GV->setConstant(false); | ||
insertArrayDecryption(M, {GV, key, eleNum}); | ||
} else if (intData) { | ||
continue; // TODO: fix the bug | ||
uint64_t key = cryptoutils->get_uint64_t(); | ||
ConstantInt *enc = | ||
CONST(intData->getType(), key ^ intData->getZExtValue()); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 字样的所有全局变量忽略掉就好了。