<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 --- - clang-cl miscompilation of Firefox's netwerk/base/nsSocketTransportService2.cpp"
   href="https://llvm.org/bugs/show_bug.cgi?id=30293">30293</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang-cl miscompilation of Firefox's netwerk/base/nsSocketTransportService2.cpp
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

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

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>froydnj@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>Created <span class=""><a href="attachment.cgi?id=17216" name="attach_17216" title="preprocessed source and runscript">attachment 17216</a> <a href="attachment.cgi?id=17216&action=edit" title="preprocessed source and runscript">[details]</a></span>
preprocessed source and runscript

While debugging why Firefox crashes when compiled with clang-cl, I ran across
this miscompilation.  In nsSocketTransport2.cpp, we have:

nsSocketTransportService* gSocketTransportService = nullptr;
...
void
nsSocketTransportService::OnKeepaliveEnabledPrefChange()
{
    // Dispatch to socket thread if we're not executing there.
    if (PR_GetCurrentThread() != gSocketThread) {
        gSocketTransportService->Dispatch(
            NewRunnableMethod(
                this, &nsSocketTransportService::OnKeepaliveEnabledPrefChange),
            nsIEventTarget::DISPATCH_NORMAL);
        return;
    }
    ...
}

The preprocessed source file, along with the command-line flags used to compile
it, are provided in the attached tarball.  What I see happening in the debugger
running Firefox is:

1. At the start of OnKeepaliveEnabledPrefChange, ecx contains |this|, and so
does gSocketTransportService.

2. When we load from gSocketTransportService, we adjust its value by 4.

3. When we're preparing to call Dispatch, I think the code is assembling a
member function pointer to be used with some kind of thunk.  Whatever it's
doing, the adjusted value we constructed in step (2) winds up in 0(%ecx)

3. When Dispatch() is invoked, we go through what looks like a thunk, but none
of the arguments are massaged in any way; the thunk jumps directly to the
Dispatch() implementation.  (I think this is a thunk, anyway; it doesn't appear
in llvm-objdump -d output, but it's definitely there in the debugger, for
reasons I do not understand.)

4. The real Dispatch implementation receives the adjusted pointer from step (3)
in 0(%ecx), acts on it as though it's the actual |this| pointer and things go
south from there.  We call nsSocketTransportService::GetThreadSafely() with the
wrong |this| value and it loads a null pointer instead of the actual pointer
it's supposed to load.</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>