BUG: recv() Causes Data Corruption of Large Buffer

Last reviewed: January 15, 1998
Article ID: Q168187
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows 95

SYMPTOMS

Data buffers passed to the Windows Sockets recv() function have the potential to lose the data they contain. This problem only affects buffers passed to recv(). Data buffers manipulated solely by the application or by API's other than recv() are not affected by this problem.

CAUSE

The recv() function accepts a buffer to be filled with data received over a socket connection. For memory management purposes, the system may discard portions of such a buffer to allow other memory activity of the system to proceed successfully. When the system does this, it first checks the memory page's "dirty" bit to determine if the page should be written to the paging file before being discarded. When the memory is later needed, it is retrieved from the paging file intact.

The memory pages touched by recv() are supposed to be marked "dirty" when modified so that the pages will be properly written to the system paging file before the memory is discarded. In Windows 95, the pages are not marked "dirty." This causes the pages to be discarded without first being written to the paging file. When the data is later needed by the application, it is treated like newly-allocated zero-initialized memory. Such data received over the socket connection earlier is now lost.

RESOLUTION

There is no absolutely effective workaround to this problem.

One thing that may work is to use small buffers with the recv() function. This reduces the risk that some portion of the buffer will need to be swapped out and subsequently lost. Note that this reduces the risk but doesn't completely alleviate it.

Alternatively, a program may dirty each page manually just before or after calling recv(). A program can dirty each page by writing data to one of the bytes in each page. The system page size on Windows 95 is 4096 (4K) bytes so a program only needs to touch a single byte in each 4K section of the buffer. This causes the pages to be properly written to the paging file before the memory is discarded, if necessary. The only drawback to this method is that there is no way to determine when a page will be paged out. If a page is paged out at an inopportune time, then the data it contains may be lost.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.


Additional query words: winsock bse base
Keywords : BseMm NtwkWinsock kbbuglist
Version : 95
Platform : NT WINDOWS
Issue type : kbbug


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 15, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.