Mar 27, 2009

boost asio for windows pipe bug -- ERROR_MORE_DATA

I encountered another asio bug today.
I created a pipe server and used a pipe client to send data to the server.
The server read 128 bytes for each read, but the client sends 65536 bytes for each write. When the server read the data, ::ReadFile returns ERROR_MORE_DATA.
Then it just crashed.

The root cause is at function "async_read_some_at" in the file "win_iocp_handle_service.hpp".


DWORD bytes_transferred = 0;
ptr.get()->Offset = offset & 0xFFFFFFFF;
ptr.get()->OffsetHigh = (offset >> 32) & 0xFFFFFFFF;
BOOL ok = ::ReadFile(impl.handle_,
boost::asio::buffer_cast(buffer),
static_cast(boost::asio::buffer_size(buffer)),
&bytes_transferred, ptr.get());
DWORD last_error = ::GetLastError();
if (!ok && last_error != ERROR_IO_PENDING)
{
boost::asio::io_service::work work(this->get_io_service());
ptr.reset();
boost::system::error_code ec(last_error,
boost::asio::error::get_system_category());
iocp_service_.post(bind_handler(handler, ec, bytes_transferred));
}
else
{
ptr.release();
}
}


In the above code,

if (!ok && last_error != ERROR_IO_PENDING)
{
boost::asio::io_service::work work(this->get_io_service());
ptr.reset();
boost::system::error_code ec(last_error,
boost::asio::error::get_system_category());
iocp_service_.post(bind_handler(handler, ec, bytes_transferred));
}


Checks for any error. When last_error != ERROR_IO_PENDING, the function assumes that the call to ::ReadFile is failed, so it release ptr immediately. But when last_error is ERROR_MORE_DATA, the call is successful and the ReadComplete event will still be put in the complete queue. Since the ptr has been released, the LPOVERLAPPED returned by ::GetQueuedCompletionStatus points to invalid location and is causes crash.

The fix is simple, just change the code to

if (!ok && (last_error != ERROR_IO_PENDING && last_error != ERROR_MORE_DATA) )
{
boost::asio::io_service::work work(this->get_io_service());
ptr.reset();
boost::system::error_code ec(last_error,
boost::asio::error::get_system_category());
iocp_service_.post(bind_handler(handler, ec, bytes_transferred));
}

Mar 24, 2009

Microsoft Usbser.sys hot fix

Here is the list of Microsoft Hotfix of the notorious usbser.sys.

5.1.2600.3226: When you use a USB-to-Serial converter to connect a device to a Windows XP-based computer, the data transfer from the computer to the device is slower than expected. (KB943198)

5.1.2600.2930: The Usbser.sys driver may not load when a USB device uses an IAD to define a function that has multiple interfaces, and this function uses the Usbser.sys driver file in Windows XP. (KB918365)

5.1.2600.1330: A memory leak may occur when you repeatedly start and stop the Usbser.sys driver on a Windows XP-based computer. (KB831480)

Mar 11, 2009

Useful USB status code

Some useful USB status code which you may see when dealing with USB monitor

1.target stalled
USBD_STATUS_STALL_PID 0xC0000004

2.operation is cancelled by application
USBD_STATUS_CANCELED 0xC0010000

Mar 1, 2009

PDF management

Recently I tried to manage the pdf I have got so far. It's very handy if I had a tool like iPhoto which can automatically add all of my pdfs to a large library with automatic indexs, comments or description.

Delicious library on MAC can find book description, but it cannot import pdfs automatically. Yep on MAC is a good way to go, but I just wonder that if there is a similiar software on PC?

 
TEMPLATE HACKS AND TWEAKS BY [ METAMUSE ] BLACKCAT 1.1
/scripts/shBrushJScript.js'/>