[PATCH] D15553: PR25717: fatal IO error writing large outputs to console on Windows

Yunzhong Gao via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 17 11:41:29 PST 2015


ygao updated this revision to Diff 43164.
ygao added a comment.

Addressed review comments and rebased to trunk.


http://reviews.llvm.org/D15553

Files:
  lib/Support/raw_ostream.cpp

Index: lib/Support/raw_ostream.cpp
===================================================================
--- lib/Support/raw_ostream.cpp
+++ lib/Support/raw_ostream.cpp
@@ -57,6 +57,11 @@
 #endif
 #endif
 
+#ifdef LLVM_ON_WIN32
+#define NOMINMAX
+#include <windows.h>
+#endif
+
 using namespace llvm;
 
 raw_ostream::~raw_ostream() {
@@ -562,13 +567,34 @@
     report_fatal_error("IO failure on output stream.", /*GenCrashDiag=*/false);
 }
 
+#ifdef LLVM_ON_WIN32
+static bool IsWindows7OrEarlier() {
+  unsigned Version = ::GetVersion();
+  unsigned MajorVersion = (Version & 0xFF);
+  unsigned MinorVersion = ((Version >> 8) & 0xFF);
+  return MajorVersion < 6 || MinorVersion < 2;
+}
+#endif
 
 void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
   assert(FD >= 0 && "File already closed.");
   pos += Size;
 
+#ifndef LLVM_ON_WIN32
+  bool ShouldWriteInChunks = false;
+#else
+  // Writing a large size of output to Windows console returns ENOMEM. It seems
+  // that, prior to Windows 8, WriteFile() is redirecting to WriteConsole(), and
+  // the latter has a size limit (66000 bytes or less, depending on heap usage).
+  bool ShouldWriteInChunks = !!::_isatty(FD) && IsWindows7OrEarlier();
+#endif
+
   do {
-    ssize_t ret = ::write(FD, Ptr, Size);
+    size_t ChunkSize = Size;
+    if (ChunkSize > 32767 && ShouldWriteInChunks)
+        ChunkSize = 32767;
+
+    ssize_t ret = ::write(FD, Ptr, ChunkSize);
 
     if (ret < 0) {
       // If it's a recoverable error, swallow it and retry the write.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15553.43164.patch
Type: text/x-patch
Size: 1521 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151217/eb131bb6/attachment.bin>


More information about the llvm-commits mailing list