[PATCH] D30283: [ubsan] Reduce alignment checking of C++ object pointers

Vedant Kumar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 22 16:18:20 PDT 2017


vsk updated this revision to Diff 92732.
vsk added a comment.

Per Eli's comment: test that we don't regress alignment-checking for extern globals which aren't arrays. I verified that for this case, there is not functional change. However, there *is* a somewhat surprising IR change even at -O0, that is worth calling out.

Before this patch, here's what happens when loading a "long long" from a global struct:

  define i64 @load_extern_S1()() #0 {
    br i1 true, label %2, label %1, !prof !2, !nosanitize !3 ;; The frontend appears to be doing some early optimizations here...
  
  ; <label>:1:                                      ; preds = %0
    call void @__ubsan_handle_type_mismatch(i8* bitcast ({ { [99 long long i8]*, i32, i32 }, { i16, i16, [12 long long i8] }*, i64, i8 }* @1 to i8*), i64 ptrtoint (%struct.S1* @g_S1 to i64)) #2, !nosanitize !3
    br label %2, !nosanitize !3
  
  ; <label>:2:                                      ; preds = %1, %0
    %3 = load i64, i64* getelementptr inbounds (%struct.S1, %struct.S1* @g_S1, i32 0, i32 0), align 8
    ret i64 %3
  }

Because this patch skips alignment checking when the base of a MemberExpr is a DeclRefExpr, the IR changes, even though the behavior doesn't:

  define i64 @load_extern_S1()() #0 {
    %0 = load i64, i64* getelementptr inbounds (%struct.S1, %struct.S1* @g_S1, i32 0, i32 0), align 8
    ret i64 %0
  }

IMO this is an acceptable change which won't lead to missed diagnostics. But I'm calling it out in case anyone thinks otherwise.


https://reviews.llvm.org/D30283

Files:
  lib/CodeGen/CGExpr.cpp
  lib/CodeGen/CGExprCXX.cpp
  lib/CodeGen/CodeGenFunction.cpp
  test/CodeGen/catch-undef-behavior.c
  test/CodeGen/sanitize-recover.c
  test/CodeGenCXX/ubsan-global-alignment.cpp
  test/CodeGenCXX/ubsan-suppress-checks.cpp
  test/CodeGenCXX/ubsan-suppress-null-checks.cpp
  test/CodeGenCXX/ubsan-type-checks.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30283.92732.patch
Type: text/x-patch
Size: 18726 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170322/2cccb2c6/attachment-0001.bin>


More information about the cfe-commits mailing list