首页
社区
课程
招聘
[讨论]关于LLVM pass的字符串替换问题。
发表于: 2015-2-13 15:54 15084

[讨论]关于LLVM pass的字符串替换问题。

2015-2-13 15:54
15084
想写个简单的pass看看,发现好蛋疼,想对程序中的字符串加密,然后有了下文:

"Hello,world"    该字符串是要替换成加密字符串的原字符串。
好吧,我首先转成ConstantDataSequential,然后调用getAsString()函数得到该字符串,但是得到的长度是12.(不应该是11?)然后我进行xor处理,再使用replaceAllUsesWith函数全局替换。
出现第一个问题:type不匹配,后来我发现是长度的问题,不是12,改成11就好了(那为什么getAsString()得到的长度是12?)。

然后我替换后,出现第二个问题:
While deleting: [13 x i8]* %.encstr0_3064045518
Use still stuck around after Def is destroyed:@.encstr0_3064045518 = private constant [13 x i8] <null operand!>
Assertion failed: (use_empty() && "Uses remain when a value is destroyed!"), function ~Value,

encstr0_3064045518这个是我替换后的加密字符串变量名,为何会莫名的被删除?后面有引用肯定是不能删除的。

部分代码如下:
GlobalVariable* GV = *it;
  //get clear text string
  std::string clearstr = getGlobalStringValue(GV);//通过getAsString获取的
  GlobalVariable::LinkageTypes lt = GV->getLinkage();
  //encrypt current string
  std::string encryptedString = stringEncryption(clearstr); //简单xor不改变长度
  //create new global string with the encrypted string
  std::ostringstream oss;
  oss << ".encstr" << encryptedStringCounter << "_" << sys::Process::GetRandomNumber();
  Constant *cryptedStr = ConstantDataArray::getString(M.getContext(), encryptedString, true);
  GlobalVariable* gCryptedStr = new GlobalVariable(M, cryptedStr->getType(), true, GV->getLinkage(), cryptedStr, oss.str());
  StringMapGlobalVars[oss.str()] = gCryptedStr;
   //replace use of clear string with encrypted string
  GV->replaceAllUsesWith(gCryptedStr);
  GV->eraseFromParent();

感觉里面的各种类不知道指的哪个结构
有弄过的大牛快来指点下下

[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 0
支持
分享
最新回复 (8)
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
Value::~Value() {
00057   // Notify all ValueHandles (if present) that this value is going away.
00058   if (HasValueHandle)
00059     ValueHandleBase::ValueIsDeleted(this);
00060
00061 #ifndef NDEBUG      // Only in -g mode...
00062   // Check to make sure that there are no uses of this value that are still
00063   // around when the value is destroyed.  If there are, then we have a dangling
00064   // reference and something is wrong.  This code is here to print out what is
00065   // still being referenced.  The value in question should be printed as
00066   // a <badref>
00067   //
00068   if (!use_empty()) {
00069     dbgs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
00070     for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
00071       dbgs() << "Use still stuck around after Def is destroyed:"
00072            << **I << "\n";
00073   }
00074 #endif
00075   assert(use_empty() && "Uses remain when a value is destroyed!");
00076
00077   // If this value is named, destroy the name.  This should not be in a symtab
00078   // at this point.
00079   if (Name)
00080     Name->Destroy();
00081
00082   // There should be no uses of this object anymore, remove it.
00083   LeakDetector::removeGarbageObject(this);
00084 }
00085

设置NDEBUG看看?
2015-2-14 06:51
0
雪    币: 628
活跃值: (358)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
3
这个只是为了调试方便的打印信息,设不设置都一样
2015-2-14 09:17
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
打印出来看看哪里还use
2015-2-15 15:38
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
字符串的长度不匹配原因是ConstantDataArray::getString(),此函数第三个参数bool为ture时会自动添加00字节导致长度改变,楼主将此Constant *cryptedStr = ConstantDataArray::getString(M.getContext(), encryptedString, true)换成Constant *cryptedStr = ConstantDataArray::getString(M.getContext(), encryptedString, flase)即可,希望对楼主有帮助
2016-1-25 17:10
0
雪    币: 3
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
楼主,问题解决了吗?最近我也在弄这个,遇到类似的问题。
2016-4-7 16:46
0
雪    币: 3
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
@逆羊羊 第二个问题是否有解决方案?感谢!
While deleting: [13 x i8]* %.encstr0_3064045518
Use still stuck around after Def is destroyed:@.encstr0_3064045518 = private constant [13 x i8] <null operand!>
Assertion failed: (use_empty() && "Uses remain when a value is destroyed!"), function ~Value,
2016-4-26 21:16
0
雪    币: 94
活跃值: (2197)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
8
感谢分享~刚遇到了这个问题!
2016-8-16 10:19
0
雪    币: 410
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
9
请问问题解决了么? 我在 clone function的时候遇到的
2023-7-6 17:44
0
游客
登录 | 注册 方可回帖
返回
//