[PATCH] [UBsan] Skip -fsanitize=vptr instrumentations when the pointer value is null

Byoungyoung Lee byoungyoung at chromium.org
Mon Jul 14 10:19:58 PDT 2014


http://reviews.llvm.org/D4412

Files:
  lib/CodeGen/CGExpr.cpp

Index: lib/CodeGen/CGExpr.cpp
===================================================================
--- lib/CodeGen/CGExpr.cpp
+++ lib/CodeGen/CGExpr.cpp
@@ -547,6 +547,23 @@
 
     // Blacklist based on the mangled type.
     if (!CGM.getSanitizerBlacklist().isBlacklistedType(Out.str())) {
+      llvm::BasicBlock *VptrNullBlock = nullptr;
+      llvm::BasicBlock *VptrNotNullBlock = nullptr;
+
+      // Skip Vptr checks when the pointer value is null. This is redundant if
+      // -fsanitize=null is used.
+      // FIXME: Add optional flags.
+      if (TCK_DowncastPointer && !SanOpts->Null) {
+        Cond = Builder.CreateICmpNE(
+          Address, llvm::Constant::getNullValue(Address->getType()));
+
+        VptrNullBlock = createBasicBlock("vptr_null");
+        VptrNotNullBlock = createBasicBlock("not.vptr_null");
+
+        Builder.CreateCondBr(Cond, VptrNotNullBlock, VptrNullBlock);
+        EmitBlock(VptrNotNullBlock);
+      }
+
       llvm::hash_code TypeHash = hash_value(Out.str());
 
       // Load the vptr, and compute hash_16_bytes(TypeHash, vptr).
@@ -585,6 +602,11 @@
       EmitCheck(Builder.CreateICmpEQ(CacheVal, Hash),
                 "dynamic_type_cache_miss", StaticData, DynamicData,
                 CRK_AlwaysRecoverable);
+
+      if (VptrNullBlock) {
+        Builder.CreateBr(VptrNullBlock);
+        EmitBlock(VptrNullBlock);
+      }
     }
   }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4412.11395.patch
Type: text/x-patch
Size: 1393 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140714/277371c0/attachment.bin>


More information about the cfe-commits mailing list