@@ -172,14 +172,12 @@ char CmpLogRoutines::ID = 0;
172
172
#endif
173
173
174
174
bool CmpLogRoutines::hookRtns (Module &M) {
175
- std::vector<CallInst *> calls, llvmStdStd, llvmStdC, gccStdStd, gccStdC,
176
- Memcmp, Strcmp, Strncmp;
177
- LLVMContext &C = M.getContext ();
175
+ std::vector<CallInst *> calls, llvmStdStd, llvmStdC, gccStdStd, gccStdC;
176
+ LLVMContext &C = M.getContext ();
178
177
179
178
Type *VoidTy = Type::getVoidTy (C);
180
179
// PointerType *VoidPtrTy = PointerType::get(VoidTy, 0);
181
180
IntegerType *Int8Ty = IntegerType::getInt8Ty (C);
182
- IntegerType *Int64Ty = IntegerType::getInt64Ty (C);
183
181
PointerType *i8PtrTy = PointerType::get (Int8Ty, 0 );
184
182
185
183
#if LLVM_VERSION_MAJOR < 9
@@ -271,60 +269,6 @@ bool CmpLogRoutines::hookRtns(Module &M) {
271
269
FunctionCallee cmplogGccStdC = c4;
272
270
#endif
273
271
274
- #if LLVM_VERSION_MAJOR >= 9
275
- FunctionCallee
276
- #else
277
- Constant *
278
- #endif
279
- c5 = M.getOrInsertFunction (" __cmplog_rtn_hook_n" , VoidTy, i8PtrTy,
280
- i8PtrTy, Int64Ty
281
- #if LLVM_VERSION_MAJOR < 5
282
- ,
283
- NULL
284
- #endif
285
- );
286
- #if LLVM_VERSION_MAJOR >= 9
287
- FunctionCallee cmplogHookFnN = c5;
288
- #else
289
- Function *cmplogHookFnN = cast<Function>(c5);
290
- #endif
291
-
292
- #if LLVM_VERSION_MAJOR >= 9
293
- FunctionCallee
294
- #else
295
- Constant *
296
- #endif
297
- c6 = M.getOrInsertFunction (" __cmplog_rtn_hook_strn" , VoidTy, i8PtrTy,
298
- i8PtrTy, Int64Ty
299
- #if LLVM_VERSION_MAJOR < 5
300
- ,
301
- NULL
302
- #endif
303
- );
304
- #if LLVM_VERSION_MAJOR >= 9
305
- FunctionCallee cmplogHookFnStrN = c6;
306
- #else
307
- Function *cmplogHookFnStrN = cast<Function>(c6);
308
- #endif
309
-
310
- #if LLVM_VERSION_MAJOR >= 9
311
- FunctionCallee
312
- #else
313
- Constant *
314
- #endif
315
- c7 = M.getOrInsertFunction (" __cmplog_rtn_hook_str" , VoidTy, i8PtrTy,
316
- i8PtrTy
317
- #if LLVM_VERSION_MAJOR < 5
318
- ,
319
- NULL
320
- #endif
321
- );
322
- #if LLVM_VERSION_MAJOR >= 9
323
- FunctionCallee cmplogHookFnStr = c7;
324
- #else
325
- Function *cmplogHookFnStr = cast<Function>(c7);
326
- #endif
327
-
328
272
/* iterate over all functions, bbs and instruction and add suitable calls */
329
273
for (auto &F : M) {
330
274
if (isIgnoreFunction (&F)) { continue ; }
@@ -339,87 +283,12 @@ bool CmpLogRoutines::hookRtns(Module &M) {
339
283
if (callInst->getCallingConv () != llvm::CallingConv::C) { continue ; }
340
284
341
285
FunctionType *FT = Callee->getFunctionType ();
342
- std::string FuncName = Callee->getName ().str ();
343
286
344
287
bool isPtrRtn = FT->getNumParams () >= 2 &&
345
288
!FT->getReturnType ()->isVoidTy () &&
346
289
FT->getParamType (0 ) == FT->getParamType (1 ) &&
347
290
FT->getParamType (0 )->isPointerTy ();
348
291
349
- bool isPtrRtnN = FT->getNumParams () >= 3 &&
350
- !FT->getReturnType ()->isVoidTy () &&
351
- FT->getParamType (0 ) == FT->getParamType (1 ) &&
352
- FT->getParamType (0 )->isPointerTy () &&
353
- FT->getParamType (2 )->isIntegerTy ();
354
- if (isPtrRtnN) {
355
- auto intTyOp =
356
- dyn_cast<IntegerType>(callInst->getArgOperand (2 )->getType ());
357
- if (intTyOp) {
358
- if (intTyOp->getBitWidth () != 32 &&
359
- intTyOp->getBitWidth () != 64 ) {
360
- isPtrRtnN = false ;
361
- }
362
- }
363
- }
364
-
365
- bool isMemcmp =
366
- (!FuncName.compare (" memcmp" ) || !FuncName.compare (" bcmp" ) ||
367
- !FuncName.compare (" CRYPTO_memcmp" ) ||
368
- !FuncName.compare (" OPENSSL_memcmp" ) ||
369
- !FuncName.compare (" memcmp_const_time" ) ||
370
- !FuncName.compare (" memcmpct" ));
371
- isMemcmp &= FT->getNumParams () == 3 &&
372
- FT->getReturnType ()->isIntegerTy (32 ) &&
373
- FT->getParamType (0 )->isPointerTy () &&
374
- FT->getParamType (1 )->isPointerTy () &&
375
- FT->getParamType (2 )->isIntegerTy ();
376
-
377
- bool isStrcmp =
378
- (!FuncName.compare (" strcmp" ) || !FuncName.compare (" xmlStrcmp" ) ||
379
- !FuncName.compare (" xmlStrEqual" ) ||
380
- !FuncName.compare (" g_strcmp0" ) ||
381
- !FuncName.compare (" curl_strequal" ) ||
382
- !FuncName.compare (" strcsequal" ) ||
383
- !FuncName.compare (" strcasecmp" ) ||
384
- !FuncName.compare (" stricmp" ) ||
385
- !FuncName.compare (" ap_cstr_casecmp" ) ||
386
- !FuncName.compare (" OPENSSL_strcasecmp" ) ||
387
- !FuncName.compare (" xmlStrcasecmp" ) ||
388
- !FuncName.compare (" g_strcasecmp" ) ||
389
- !FuncName.compare (" g_ascii_strcasecmp" ) ||
390
- !FuncName.compare (" Curl_strcasecompare" ) ||
391
- !FuncName.compare (" Curl_safe_strcasecompare" ) ||
392
- !FuncName.compare (" cmsstrcasecmp" ) ||
393
- !FuncName.compare (" strstr" ) ||
394
- !FuncName.compare (" g_strstr_len" ) ||
395
- !FuncName.compare (" ap_strcasestr" ) ||
396
- !FuncName.compare (" xmlStrstr" ) ||
397
- !FuncName.compare (" xmlStrcasestr" ) ||
398
- !FuncName.compare (" g_str_has_prefix" ) ||
399
- !FuncName.compare (" g_str_has_suffix" ));
400
- isStrcmp &=
401
- FT->getNumParams () == 2 && FT->getReturnType ()->isIntegerTy (32 ) &&
402
- FT->getParamType (0 ) == FT->getParamType (1 ) &&
403
- FT->getParamType (0 ) == IntegerType::getInt8PtrTy (M.getContext ());
404
-
405
- bool isStrncmp = (!FuncName.compare (" strncmp" ) ||
406
- !FuncName.compare (" xmlStrncmp" ) ||
407
- !FuncName.compare (" curl_strnequal" ) ||
408
- !FuncName.compare (" strncasecmp" ) ||
409
- !FuncName.compare (" strnicmp" ) ||
410
- !FuncName.compare (" ap_cstr_casecmpn" ) ||
411
- !FuncName.compare (" OPENSSL_strncasecmp" ) ||
412
- !FuncName.compare (" xmlStrncasecmp" ) ||
413
- !FuncName.compare (" g_ascii_strncasecmp" ) ||
414
- !FuncName.compare (" Curl_strncasecompare" ) ||
415
- !FuncName.compare (" g_strncasecmp" ));
416
- isStrncmp &= FT->getNumParams () == 3 &&
417
- FT->getReturnType ()->isIntegerTy (32 ) &&
418
- FT->getParamType (0 ) == FT->getParamType (1 ) &&
419
- FT->getParamType (0 ) ==
420
- IntegerType::getInt8PtrTy (M.getContext ()) &&
421
- FT->getParamType (2 )->isIntegerTy ();
422
-
423
292
bool isGccStdStringStdString =
424
293
Callee->getName ().find (" __is_charIT_EE7__value" ) !=
425
294
std::string::npos &&
@@ -467,13 +336,10 @@ bool CmpLogRoutines::hookRtns(Module &M) {
467
336
*/
468
337
469
338
if (isGccStdStringCString || isGccStdStringStdString ||
470
- isLlvmStdStringStdString || isLlvmStdStringCString || isMemcmp ||
471
- isStrcmp || isStrncmp) {
472
- isPtrRtnN = isPtrRtn = false ;
339
+ isLlvmStdStringStdString || isLlvmStdStringCString) {
340
+ isPtrRtn = false ;
473
341
}
474
342
475
- if (isPtrRtnN) { isPtrRtn = false ; }
476
-
477
343
if (isPtrRtn) { calls.push_back (callInst); }
478
344
if (isGccStdStringStdString) { gccStdStd.push_back (callInst); }
479
345
if (isGccStdStringCString) { gccStdC.push_back (callInst); }
@@ -485,9 +351,9 @@ bool CmpLogRoutines::hookRtns(Module &M) {
485
351
}
486
352
487
353
if (!calls.size () && !gccStdStd.size () && !gccStdC.size () &&
488
- !llvmStdStd.size () && !llvmStdC.size () && !Memcmp.size () &&
489
- Strcmp.size () && Strncmp.size ())
354
+ !llvmStdStd.size () && !llvmStdC.size ()) {
490
355
return false ;
356
+ }
491
357
492
358
for (auto &callInst : calls) {
493
359
Value *v1P = callInst->getArgOperand (0 ), *v2P = callInst->getArgOperand (1 );
@@ -506,64 +372,6 @@ bool CmpLogRoutines::hookRtns(Module &M) {
506
372
// errs() << callInst->getCalledFunction()->getName() << "\n";
507
373
}
508
374
509
- for (auto &callInst : Memcmp) {
510
- Value *v1P = callInst->getArgOperand (0 ), *v2P = callInst->getArgOperand (1 ),
511
- *v3P = callInst->getArgOperand (2 );
512
-
513
- IRBuilder<> IRB (callInst->getParent ());
514
-
515
- std::vector<Value *> args;
516
- Value *v1Pcasted = IRB.CreatePointerCast (v1P, i8PtrTy);
517
- Value *v2Pcasted = IRB.CreatePointerCast (v2P, i8PtrTy);
518
- Value *v3Pbitcast = IRB.CreateBitCast (
519
- v3P, IntegerType::get (C, v3P->getType ()->getPrimitiveSizeInBits ()));
520
- Value *v3Pcasted =
521
- IRB.CreateIntCast (v3Pbitcast, IntegerType::get (C, 64 ), false );
522
- args.push_back (v1Pcasted);
523
- args.push_back (v2Pcasted);
524
- args.push_back (v3Pcasted);
525
-
526
- IRB.CreateCall (cmplogHookFnN, args);
527
-
528
- // errs() << callInst->getCalledFunction()->getName() << "\n";
529
- }
530
-
531
- for (auto &callInst : Strcmp) {
532
- Value *v1P = callInst->getArgOperand (0 ), *v2P = callInst->getArgOperand (1 );
533
-
534
- IRBuilder<> IRB (callInst->getParent ());
535
- std::vector<Value *> args;
536
- Value *v1Pcasted = IRB.CreatePointerCast (v1P, i8PtrTy);
537
- Value *v2Pcasted = IRB.CreatePointerCast (v2P, i8PtrTy);
538
- args.push_back (v1Pcasted);
539
- args.push_back (v2Pcasted);
540
-
541
- IRB.CreateCall (cmplogHookFnStr, args);
542
-
543
- // errs() << callInst->getCalledFunction()->getName() << "\n";
544
- }
545
-
546
- for (auto &callInst : Strncmp) {
547
- Value *v1P = callInst->getArgOperand (0 ), *v2P = callInst->getArgOperand (1 ),
548
- *v3P = callInst->getArgOperand (2 );
549
-
550
- IRBuilder<> IRB (callInst->getParent ());
551
- std::vector<Value *> args;
552
- Value *v1Pcasted = IRB.CreatePointerCast (v1P, i8PtrTy);
553
- Value *v2Pcasted = IRB.CreatePointerCast (v2P, i8PtrTy);
554
- Value *v3Pbitcast = IRB.CreateBitCast (
555
- v3P, IntegerType::get (C, v3P->getType ()->getPrimitiveSizeInBits ()));
556
- Value *v3Pcasted =
557
- IRB.CreateIntCast (v3Pbitcast, IntegerType::get (C, 64 ), false );
558
- args.push_back (v1Pcasted);
559
- args.push_back (v2Pcasted);
560
- args.push_back (v3Pcasted);
561
-
562
- IRB.CreateCall (cmplogHookFnStrN, args);
563
-
564
- // errs() << callInst->getCalledFunction()->getName() << "\n";
565
- }
566
-
567
375
for (auto &callInst : gccStdStd) {
568
376
Value *v1P = callInst->getArgOperand (0 ), *v2P = callInst->getArgOperand (1 );
569
377
0 commit comments