<html>
    <head>
      <base href="http://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 --- - Windows Rewriter/MemoryBuffer errors with 16KB large files"
   href="http://llvm.org/bugs/show_bug.cgi?id=17960">17960</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Windows Rewriter/MemoryBuffer errors with 16KB large files
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </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>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>alp@nuanti.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Various parts of tooling, the modernizers and migrators are mis-using the
Rewriter interface and causing Windows errors or crashes when rewriting large
files.

A typical error:

"Error while writing file: Error opening output file \'1.cpp\': The requested
operation cannot be performed on a file with a user-mapped section open"

The problematic pattern appearing frequently in clang's codebase is:

      llvm::raw_fd_ostream FileStream(FileName.c_str(), ErrorInfo,
                                      llvm::sys::fs::F_Binary);
      Rewrite.getEditBuffer(ID).write(FileStream);

The fix is often simple as just replacing with:

      Rewrite.overwriteChangedFiles()

overwriteChangedFiles() knows how to write to a temporary file and move into
place without writing over the mmaped source MemoryBuffer.

My analysis of the underlying problem:

    The previous implementation was attempting to stream back directly to the
    original file and failing if it was already memory mapped by MemoryBuffer,
    an operation unsupported by Windows.

    MemoryBuffer generally mmaps files larger than the physical page.

I've so far fixed two instances of the problem:

 * r193594: RefactoringTool
 * r194250: clang-format (PR17216)

There are a others yet to be fixed, some more involved, which I'm filing this
bug to track.

The clang lit tests have mostly missed this so far because there's a tendency
to keep test inputs checked into version control small, but in practice this
gets hit often when running the rewrite-based tools on Windows with real code.</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>