<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 --- - [ms] clang-cl emits code that causes link.exe /incremental to write broken binaries, related to function pointers"
   href="https://llvm.org/bugs/show_bug.cgi?id=25891">25891</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[ms] clang-cl emits code that causes link.exe /incremental to write broken binaries, related to function pointers
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>nicolasweber@gmx.de
          </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>First, create these 3 files:

C:\src\chrome\src\net>type s3.h
typedef struct ssl_st SSL;
struct ssl_st {
  void* f[21];  // <- !
  int (*handshake_func)(SSL *);
  void (*info_callback)();
};

C:\src\chrome\src\net>type s3_clnt.c
#include "s3.h"

#include <stddef.h>

void f();

#define FOO0 f(); f();
#define FOO1 FOO0 FOO0
#define FOO2 FOO1 FOO1
#define FOO3 FOO2 FOO2
#define FOO4 FOO3 FOO3
#define FOO5 FOO4 FOO4
#define FOO6 FOO5 FOO5
#define FOO7 FOO6 FOO6
#define FOO8 FOO7 FOO7
#define FOO9 FOO8 FOO8
#define FOO10 FOO9 FOO9
#define FOO11 FOO10 FOO10

int ssl3_connect(SSL *s) {
  if (s->info_callback != NULL)
    s->info_callback();

  // Bloat code size a bit so it's more likely link.exe writes a new version
  // of this function.
  FOO11
  return 0;
}

C:\src\chrome\src\net>type s3_lib.c
#include <stdlib.h>
#include <string.h>

#include "s3.h"

int ssl3_connect(SSL *s);

void f() {}

int main() {
  SSL *s = (SSL *)malloc(sizeof(SSL));
  memset(s, 0, sizeof(SSL));
  s->handshake_func = ssl3_connect;
  s->handshake_func(s);
}


Then build them like so:
C:\src\chrome\src\net>"..\third_party\llvm-build\Release+Asserts\bin\clang-cl.exe"
/c s3_clnt.c /O2

C:\src\chrome\src\net>"..\third_party\llvm-build\Release+Asserts\bin\clang-cl.exe"
/c s3_lib.c /O2

C:\src\chrome\src\net>link /incremental s3_clnt.obj s3_lib.obj
/out:net_repro.exe
Microsoft (R) Incremental Linker Version 12.00.31101.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Now run net_repro.exe. It should exit fine.

Now increment the number in the line marked "// <- !" by one, rerun the three
build commands, and run the executable again. You my need to do this 2-5 times,
but sometimes once is enough. Eventually, the executable will crash with a
stack overflow. It looks like `s->handshake_func(s)` jumps to an old version of
ssl3_connect() where the offset is 8 bytes too low, so that `s->info_callback`
isn't NULL but points ot `s->handshake_func` again.</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>