I've made a pin tool to dump CreatFile win32 calls (in my case CreateFileW) and its return values. It looks like this:
/* ... */
VOID Image(IMG img, VOID *v)
{
RTN cfwRtn = RTN_FindByName(img, "CreateFileW");
if (RTN_Valid(cfwRtn))
{
RTN_Open(cfwRtn);
RTN_InsertCall(cfwRtn, IPOINT_BEFORE, (AFUNPTR)CreateFileWArg,
IARG_ADDRINT, "CreateFileW",
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_END);
RTN_InsertCall(cfwRtn, IPOINT_AFTER, (AFUNPTR)CreateFileWafter,
IARG_FUNCRET_EXITPOINT_VALUE, IARG_END);
RTN_Close(cfwRtn);
}
}
/* ... */
VOID CreateFileWArg(CHAR * name, wchar_t * filename)
{
TraceFile << name << "(" << filename << ")" << endl;
}
VOID CreateFileWafter(ADDRINT ret)
{
TraceFile << "\tReturned handle: " << ret << endl;
}
It gives interesting results. For instance, on a small program that just opens an existing file and does nothing else, it gives:
CreateFileW(file.txt)
Returned handle: 0
CreateFileW(file.txt)
Returned handle: 0x74
Returned handle: 0x74
Lots of anomalies.
- Why are there two calls?
- If i'm not mistaken CreateFile should never ever return 0.
- After the second call, it returns twice (?)
I also tried to instrument a simple c++ program, that directly calls CreateFileW once, the result:
CreateFileW(file.txt)
Returned handle: 0
CreateFileW(file.txt)
Returned handle: 0xffffffff
Returned handle: 0xffffffff
The file i tried to open did not exist, so the return value (-1 == INVALID_HANDLE_VALUE) is correct at least.
Any ideas? Thanks in advance!