<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - MemorySanitizer false positives in optimized code"
   href="https://llvm.org/bugs/show_bug.cgi?id=28054">28054</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>MemorySanitizer false positives in optimized code
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Loop Optimizer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>eugeni.stepanov@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>$ cat 1.cc
#include <stdio.h>
#include <sanitizer/msan_interface.h>

struct Map {
  unsigned Small;

  long Buckets;
  long *Ptr;
  unsigned NumBuckets;

  Map() {
    Small = true;
    Buckets = 42;
    __msan_allocated_memory(&NumBuckets, sizeof(NumBuckets));
  }

  long *getBuckets() {
    return Small ? &Buckets : Ptr;
  }

  unsigned getNumBuckets() {
    return Small ? 1 : NumBuckets;
  }
};

Map M;

int main() {
  for (int i = 0; i < 1; ++i) {
    if (M.getNumBuckets() != 0) {
      if (42 == *M.getBuckets())
        return 1;
    }
  }

  if (!M.Small)
    printf("zz\n");
}

$ clang++ 1.cc  -std=c++14  -g -O3 -fsanitize=memory && ./a.out
WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x4879c3 in main 1.cc:30:9
    #1 0x7f12aeff9f44 in __libc_start_main
    #2 0x419b11 in _start

Looking at the bitcode, the first thing the program does is compare NumBuckets
with 0 and branch on the result. That's what MSan is complaining about. The
source code reads NumBuckets only when Small == 0, which is impossible.

This happens only at -O3.

$ clang++ 1.cc  -std=c++14  -g -O3 -S -emit-llvm -o -
...
%struct.Map = type <{ i32, [4 x i8], i64, i64*, i32, [4 x i8] }>
...
define i32 @main() #0 {
entry:
  %0 = load i32, i32* getelementptr inbounds (%struct.Map, %struct.Map* @M, i64
0, i32 0), align 8
  %tobool.i20 = icmp eq i32 %0, 0
  %1 = load i32, i32* getelementptr inbounds (%struct.Map, %struct.Map* @M, i64
0, i32 4), align 8
  %cmp1 = icmp eq i32 %1, 0
  %2 = load i64*, i64** getelementptr inbounds (%struct.Map, %struct.Map* @M,
i64 0, i32 3), align 8
  br i1 %cmp1, label %entry.split.us, label %entry.split</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>