diff --git a/src/coreclr/dlls/mscorpe/pewriter.cpp b/src/coreclr/dlls/mscorpe/pewriter.cpp index c6ff21de3ddc76..e509412abdabd4 100644 --- a/src/coreclr/dlls/mscorpe/pewriter.cpp +++ b/src/coreclr/dlls/mscorpe/pewriter.cpp @@ -4,6 +4,7 @@ #include "blobfetcher.h" #include "pedecoder.h" +#include #ifdef _DEBUG #define LOGGING @@ -547,7 +548,7 @@ HRESULT PEWriter::Init(PESectionMan *pFrom, DWORD createFlags) headers = NULL; headersEnd = NULL; - m_file = INVALID_HANDLE_VALUE; + m_file = NULL; return S_OK; } @@ -626,21 +627,15 @@ HRESULT PEWriter::setDirectoryEntry(PEWriterSection *section, ULONG entry, ULONG return S_OK; } -//----------------------------------------------------------------------------- -// These 2 write functions must be implemented here so that they're in the same -// .obj file as whoever creates the FILE struct. We can't pass a FILE struct -// across a dll boundary and use it. -//----------------------------------------------------------------------------- - -HRESULT PEWriterSection::write(HANDLE file) +HRESULT PEWriterSection::write(FILE* file) { return m_blobFetcher.Write(file); } //----------------------------------------------------------------------------- -// Write out the section to the stream +// Write out the section to the file //----------------------------------------------------------------------------- -HRESULT CBlobFetcher::Write(HANDLE file) +HRESULT CBlobFetcher::Write(FILE* file) { // Must write out each pillar (including idx = m_nIndexUsed), one after the other unsigned idx; @@ -648,10 +643,10 @@ HRESULT CBlobFetcher::Write(HANDLE file) if (m_pIndex[idx].GetDataLen() > 0) { ULONG length = m_pIndex[idx].GetDataLen(); - DWORD dwWritten = 0; - if (!WriteFile(file, m_pIndex[idx].GetRawDataStart(), length, &dwWritten, NULL)) + size_t dwWritten = 0; + if ((dwWritten = fwrite(m_pIndex[idx].GetRawDataStart(), 1, length, file)) <= 0) { - return HRESULT_FROM_GetLastError(); + return HRESULTFromErrno(); } _ASSERTE(dwWritten == length); } @@ -1336,37 +1331,32 @@ HRESULT PEWriter::fixup(CeeGenTokenMapper *pMapper) HRESULT PEWriter::Open(_In_ LPCWSTR fileName) { - _ASSERTE(m_file == INVALID_HANDLE_VALUE); + _ASSERTE(m_file == NULL); HRESULT hr = NOERROR; - m_file = WszCreateFile(fileName, - GENERIC_WRITE, - 0, // No sharing. Was: FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, - CREATE_ALWAYS, - FILE_ATTRIBUTE_NORMAL, - NULL ); - if (m_file == INVALID_HANDLE_VALUE) - hr = HRESULT_FROM_GetLastErrorNA(); + int err = fopen_lp(&m_file, fileName, W("wb")); + + if (err != 0) + hr = HRESULTFromErrno(); return hr; } HRESULT PEWriter::Seek(int offset) { - _ASSERTE(m_file != INVALID_HANDLE_VALUE); - if (SetFilePointer(m_file, offset, 0, FILE_BEGIN)) + _ASSERTE(m_file != NULL); + if (fseek(m_file, offset, SEEK_SET) == 0) return S_OK; else - return HRESULT_FROM_GetLastError(); + return HRESULTFromErrno(); } HRESULT PEWriter::Write(const void *data, int size) { - _ASSERTE(m_file != INVALID_HANDLE_VALUE); + _ASSERTE(m_file != NULL); HRESULT hr = S_OK; - DWORD dwWritten = 0; + size_t dwWritten = 0; if (size) { CQuickBytes zero; @@ -1379,13 +1369,13 @@ HRESULT PEWriter::Write(const void *data, int size) data = zero.Ptr(); } } - - if (WriteFile(m_file, data, size, &dwWritten, NULL)) + _ASSERTE(data != NULL); + if ((dwWritten = fwrite(data, 1, size, m_file)) >= 0) { - _ASSERTE(dwWritten == (DWORD)size); + _ASSERTE(dwWritten == (size_t)size); } else - hr = HRESULT_FROM_GetLastError(); + hr = HRESULTFromErrno(); } return hr; @@ -1393,7 +1383,7 @@ HRESULT PEWriter::Write(const void *data, int size) HRESULT PEWriter::Pad(int align) { - DWORD offset = SetFilePointer(m_file, 0, NULL, FILE_CURRENT); + DWORD offset = (DWORD)ftell(m_file); int pad = padLen(offset, align); if (pad > 0) return Write(NULL, pad); @@ -1403,16 +1393,17 @@ HRESULT PEWriter::Pad(int align) HRESULT PEWriter::Close() { - if (m_file == INVALID_HANDLE_VALUE) + if (m_file == NULL) return S_OK; + int err = fclose(m_file); HRESULT hr; - if (CloseHandle(m_file)) + if (err == 0) hr = S_OK; else - hr = HRESULT_FROM_GetLastError(); + hr = HRESULTFromErrno(); - m_file = INVALID_HANDLE_VALUE; + m_file = NULL; return hr; } diff --git a/src/coreclr/dlls/mscorpe/pewriter.h b/src/coreclr/dlls/mscorpe/pewriter.h index 31b4ddcb726eb8..5016c3a8ac305a 100644 --- a/src/coreclr/dlls/mscorpe/pewriter.h +++ b/src/coreclr/dlls/mscorpe/pewriter.h @@ -111,7 +111,7 @@ class PEWriter : public PESectionMan ULONG m_codeRvaBase; DWORD m_peFileTimeStamp; - HANDLE m_file; + FILE* m_file; PEWriterSection **getSectStart() { return (PEWriterSection**)sectStart; @@ -203,7 +203,7 @@ class PEWriterSection : public PESection { DWORD dataRvaBase, DWORD textRvaBase); - virtual HRESULT write (HANDLE file); + virtual HRESULT write (FILE* file); virtual unsigned writeMem (void ** pMem); }; diff --git a/src/coreclr/ilasm/asmman.cpp b/src/coreclr/ilasm/asmman.cpp index 8f42b83bdcd7b7..1f343066c5a36a 100644 --- a/src/coreclr/ilasm/asmman.cpp +++ b/src/coreclr/ilasm/asmman.cpp @@ -10,6 +10,7 @@ #include "assembler.h" #include "strongnameinternal.h" #include +#include extern WCHAR* pwzInputFiles[]; @@ -404,28 +405,23 @@ void AsmMan::EndAssembly() else { // Read public key or key pair from file. - HANDLE hFile = WszCreateFile(((Assembler*)m_pAssembler)->m_wzKeySourceName, - GENERIC_READ, - FILE_SHARE_READ, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, - NULL); - if(hFile == INVALID_HANDLE_VALUE) + FILE* fp; + int err = fopen_lp(&fp, ((Assembler*)m_pAssembler)->m_wzKeySourceName, W("rb")); + if (err != 0) { - hr = GetLastError(); MAKE_UTF8PTR_FROMWIDE(keySourceNameUtf8, ((Assembler*)m_pAssembler)->m_wzKeySourceName); - report->error("Failed to open key file '%s': 0x%08X\n",keySourceNameUtf8,hr); + report->error("Failed to open key file '%s': 0x%08X\n",keySourceNameUtf8,err); m_pCurAsmRef = NULL; return; } // Determine file size and allocate an appropriate buffer. - m_sStrongName.m_cbPublicKey = SafeGetFileSize(hFile, NULL); - if (m_sStrongName.m_cbPublicKey == 0xffffffff) { + int64_t fSize = fgetsize(fp); + m_sStrongName.m_cbPublicKey = (DWORD)fSize; + if (fSize > UINT32_MAX) { report->error("File size too large\n"); m_pCurAsmRef = NULL; - CloseHandle(hFile); + fclose(fp); return; } @@ -433,23 +429,24 @@ void AsmMan::EndAssembly() if (m_sStrongName.m_pbPublicKey == NULL) { report->error("Failed to allocate key buffer\n"); m_pCurAsmRef = NULL; - CloseHandle(hFile); + fclose(fp); return; } m_sStrongName.m_dwPublicKeyAllocated = AsmManStrongName::AllocatedByNew; // Read the file into the buffer. - DWORD dwBytesRead; - if (!ReadFile(hFile, m_sStrongName.m_pbPublicKey, m_sStrongName.m_cbPublicKey, &dwBytesRead, NULL)) { - hr = GetLastError(); + size_t dwBytesRead; + + if ((dwBytesRead = fread(m_sStrongName.m_pbPublicKey, 1, m_sStrongName.m_cbPublicKey, fp)) <= m_sStrongName.m_cbPublicKey) { + HRESULT hr = HRESULTFromErrno(); MAKE_UTF8PTR_FROMWIDE(keySourceNameUtf8, ((Assembler*)m_pAssembler)->m_wzKeySourceName); - report->error("Failed to read key file '%s': 0x%08X\n",keySourceNameUtf8,hr); + report->error("Failed to read key file '%s': 0x%d\n",keySourceNameUtf8,hr); m_pCurAsmRef = NULL; - CloseHandle(hFile); + fclose(fp); return; } - CloseHandle(hFile); + fclose(fp); // Guess whether we're full or delay signing based on // whether the blob passed to us looks like a public @@ -954,13 +951,13 @@ HRESULT AsmMan::EmitManifest() } else // embedded mgd.resource, go after the file { - HANDLE hFile = INVALID_HANDLE_VALUE; + FILE* fp = NULL; int j; WCHAR wzFileName[2048]; WCHAR* pwz; pManRes->ulOffset = m_dwMResSizeTotal; - for(j=0; (hFile == INVALID_HANDLE_VALUE)&&(pwzInputFiles[j] != NULL); j++) + for(j=0; (fp == NULL)&&(pwzInputFiles[j] != NULL); j++) { wcscpy_s(wzFileName,2048,pwzInputFiles[j]); pwz = (WCHAR*)u16_strrchr(wzFileName,DIRECTORY_SEPARATOR_CHAR_A); @@ -970,10 +967,10 @@ HRESULT AsmMan::EmitManifest() if(pwz == NULL) pwz = &wzFileName[0]; else pwz++; wcscpy_s(pwz,2048-(pwz-wzFileName),wzUniBuf); - hFile = WszCreateFile(wzFileName, GENERIC_READ, FILE_SHARE_READ, - 0, OPEN_EXISTING, 0, 0); + if (fopen_lp(&fp, wzFileName, W("rb")) != 0) + fp = NULL; } - if (hFile == INVALID_HANDLE_VALUE) + if (fp == NULL) { report->error("Failed to open managed resource file '%s'\n",pManRes->szAlias); fOK = FALSE; @@ -987,14 +984,16 @@ HRESULT AsmMan::EmitManifest() } else { - m_dwMResSize[m_dwMResNum] = SafeGetFileSize(hFile,NULL); - if(m_dwMResSize[m_dwMResNum] == 0xFFFFFFFF) + uint64_t fSize = fgetsize(fp); + if(fSize >= 0xFFFFFFFF) { + m_dwMResSize[m_dwMResNum] = 0xFFFFFFFF; report->error("Failed to get size of managed resource file '%s'\n",pManRes->szAlias); fOK = FALSE; } else { + m_dwMResSize[m_dwMResNum] = (DWORD)fSize; m_dwMResSizeTotal += m_dwMResSize[m_dwMResNum]+sizeof(DWORD); m_wzMResName[m_dwMResNum] = new WCHAR[u16_strlen(wzFileName)+1]; wcscpy_s(m_wzMResName[m_dwMResNum],u16_strlen(wzFileName)+1,wzFileName); @@ -1003,7 +1002,7 @@ HRESULT AsmMan::EmitManifest() } } - CloseHandle(hFile); + fclose(fp); } } if(fOK || ((Assembler*)m_pAssembler)->OnErrGo) diff --git a/src/coreclr/ildasm/dasm.cpp b/src/coreclr/ildasm/dasm.cpp index 2a151638dfa180..09038e782172b7 100644 --- a/src/coreclr/ildasm/dasm.cpp +++ b/src/coreclr/ildasm/dasm.cpp @@ -80,9 +80,6 @@ BOOL g_fShowCA = TRUE; BOOL g_fCAVerbal = FALSE; BOOL g_fShowRefs = FALSE; -BOOL g_fDumpToPerfWriter = FALSE; -HANDLE g_PerfDataFilePtr = NULL; - BOOL g_fDumpClassList = FALSE; BOOL g_fDumpTypeList = FALSE; BOOL g_fDumpSummary = FALSE; @@ -5793,65 +5790,6 @@ void DumpHeaderDetails(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) printLine(GUICookie,""); } - -void WritePerfData(const char *KeyDesc, const char *KeyName, const char *UnitDesc, const char *UnitName, void* Value, BOOL IsInt) -{ - - DWORD BytesWritten; - - if(!g_fDumpToPerfWriter) return; - - if (!g_PerfDataFilePtr) - { - if((g_PerfDataFilePtr = WszCreateFile(W("c:\\temp\\perfdata.dat"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL) ) == INVALID_HANDLE_VALUE) - { - printLine(NULL,"PefTimer::LogStoppedTime(): Unable to open the FullPath file. No performance data will be generated"); - g_fDumpToPerfWriter = FALSE; - return; - } - WriteFile(g_PerfDataFilePtr,"ExecTime=0\r\n",13,&BytesWritten,NULL); - WriteFile(g_PerfDataFilePtr,"ExecUnit=bytes\r\n",17,&BytesWritten,NULL); - WriteFile(g_PerfDataFilePtr,"ExecUnitDescr=File Size\r\n",26,&BytesWritten,NULL); - WriteFile(g_PerfDataFilePtr,"ExeciDirection=False\r\n",23,&BytesWritten,NULL); - } - - char ValueStr[10]; - char TmpStr[201]; - - if (IsInt) - { - sprintf_s(ValueStr,10,"%d",(int)*(int*)Value); - } - else - { - sprintf_s(ValueStr,10,"%5.2f",(float)*(float*)Value); - } - sprintf_s(TmpStr, 201, "%s=%s\r\n", KeyName, ValueStr); - WriteFile(g_PerfDataFilePtr, TmpStr, (DWORD)strlen(TmpStr), &BytesWritten, NULL); - - sprintf_s(TmpStr, 201, "%s Descr=%s\r\n", KeyName, KeyDesc); - WriteFile(g_PerfDataFilePtr, TmpStr, (DWORD)strlen(TmpStr), &BytesWritten, NULL); - - sprintf_s(TmpStr, 201, "%s Unit=%s\r\n", KeyName, UnitName); - WriteFile(g_PerfDataFilePtr, TmpStr, (DWORD)strlen(TmpStr), &BytesWritten, NULL); - - sprintf_s(TmpStr, 201, "%s Unit Descr=%s\r\n", KeyName, UnitDesc); - WriteFile(g_PerfDataFilePtr, TmpStr, (DWORD)strlen(TmpStr), &BytesWritten, NULL); - - sprintf_s(TmpStr, 201, "%s IDirection=%s\r\n", KeyName, "False"); - WriteFile(g_PerfDataFilePtr, TmpStr, (DWORD)strlen(TmpStr), &BytesWritten, NULL); -} - -void WritePerfDataInt(const char *KeyDesc, const char *KeyName, const char *UnitDesc, const char *UnitName, int Value) -{ - WritePerfData(KeyDesc,KeyName,UnitDesc,UnitName, (void*)&Value, TRUE); -} -void WritePerfDataFloat(const char *KeyDesc, const char *KeyName, const char *UnitDesc, const char *UnitName, float Value) -{ - WritePerfData(KeyDesc,KeyName,UnitDesc,UnitName, (void*)&Value, FALSE); -} - - IMetaDataTables *pITables = NULL; //ULONG sizeRec, count; //int size, size2; @@ -5879,8 +5817,6 @@ void DumpTable(unsigned long Table, const char *TableName, void* GUICookie) if(count > 0) { metaSize += size = count * sizeRec; - WritePerfDataInt(TableName,TableName,"count","count",count); - WritePerfDataInt(TableName,TableName,"bytes","bytes",size); sprintf_s(szString,SZSTRING_SIZE,"// %-14s- %4d (%d bytes)", TableName, count, size); printLine(GUICookie,szStr); } @@ -5904,8 +5840,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) sprintf_s(szString,SZSTRING_SIZE,"// File size : %d", fileSize = SafeGetFileSize(g_pPELoader->getHFile(), NULL)); printLine(GUICookie,szStr); - WritePerfDataInt("FileSize","FileSize","standard byte","bytes",fileSize); - if (g_pPELoader->IsPE32()) { size = VAL32(((IMAGE_DOS_HEADER*) g_pPELoader->getHModule())->e_lfanew) + @@ -5928,10 +5862,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) if (g_pPELoader->IsPE32()) { sizeOfHeaders = VAL32(g_pPELoader->ntHeaders32()->OptionalHeader.SizeOfHeaders); - - WritePerfDataInt("PE header size", "PE header size", "standard byte", "bytes", sizeOfHeaders); - WritePerfDataInt("PE header size used", "PE header size used", "standard byte", "bytes", size); - WritePerfDataFloat("PE header size", "PE header size", "percentage", "percentage", (float)((sizeOfHeaders * 100) / fileSize)); sprintf_s(szString,SZSTRING_SIZE,"// PE header size : %d (%d used) (%5.2f%%)", sizeOfHeaders, size, (double) (sizeOfHeaders * 100) / fileSize); @@ -5947,11 +5877,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) else { sizeOfHeaders = VAL32(g_pPELoader->ntHeaders64()->OptionalHeader.SizeOfHeaders); - - WritePerfDataInt("PE+ header size", "PE header size", "standard byte", "bytes", sizeOfHeaders); - WritePerfDataInt("PE+ header size used", "PE header size used", "standard byte", "bytes", size); - WritePerfDataFloat("PE+ header size", "PE header size", "percentage", "percentage", (float)((sizeOfHeaders * 100) / fileSize)); - sprintf_s(szString,SZSTRING_SIZE,"// PE header size : %d (%d used) (%5.2f%%)", sizeOfHeaders, size, (double) (sizeOfHeaders * 100) / fileSize); @@ -5965,9 +5890,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) } } - WritePerfDataInt("PE additional info", "PE additional info", "standard byte", "bytes",miscPESize); - WritePerfDataFloat("PE additional info", "PE additional info", "percentage", "percent", (float) ((miscPESize * 100) / fileSize)); - sprintf_s(buf, MAX_MEMBER_LENGTH, "PE additional info : %d", miscPESize); sprintf_s(szString,SZSTRING_SIZE,"// %-40s (%5.2f%%)", buf, (double) (miscPESize * 100) / fileSize); printLine(GUICookie,szStr); @@ -5982,21 +5904,15 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) numberOfSections = VAL16(g_pPELoader->ntHeaders64()->FileHeader.NumberOfSections); } - WritePerfDataInt("Num.of PE sections", "Num.of PE sections", "Nbr of sections", "sections",numberOfSections); sprintf_s(szString,SZSTRING_SIZE,"// Num.of PE sections : %d", numberOfSections); printLine(GUICookie,szStr); - WritePerfDataInt("CLR header size", "CLR header size", "byte", "bytes",VAL32(CORHeader->cb)); - WritePerfDataFloat("CLR header size", "CLR header size", "percentage", "percent",(float) ((VAL32(CORHeader->cb) * 100) / fileSize)); - sprintf_s(buf, MAX_MEMBER_LENGTH, "CLR header size : %d", VAL32(CORHeader->cb)); sprintf_s(szString,SZSTRING_SIZE,"// %-40s (%5.2f%%)", buf, (double) (VAL32(CORHeader->cb) * 100) / fileSize); printLine(GUICookie,szStr); DWORD dwMetaSize = g_cbMetaData; - WritePerfDataInt("CLR meta-data size", "CLR meta-data size", "bytes", "bytes",dwMetaSize); - WritePerfDataFloat("CLR meta-data size", "CLR meta-data size", "percentage", "percent",(float) ((dwMetaSize * 100) / fileSize)); sprintf_s(buf, MAX_MEMBER_LENGTH, "CLR meta-data size : %d", dwMetaSize); sprintf_s(szString,SZSTRING_SIZE,"// %-40s (%5.2f%%)", buf, (double) (dwMetaSize * 100) / fileSize); @@ -6011,9 +5927,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) pFirst++; } - WritePerfDataInt("CLR Additional info", "CLR Additional info", "bytes", "bytes",miscCOMPlusSize); - WritePerfDataFloat("CLR Additional info", "CLR Additional info", "percentage", "percent",(float) ((miscCOMPlusSize * 100) / fileSize)); - sprintf_s(buf, MAX_MEMBER_LENGTH, "CLR additional info : %d", miscCOMPlusSize); sprintf_s(szString,SZSTRING_SIZE,"// %-40s (%5.2f%%)", buf, (double) (miscCOMPlusSize * 100) / fileSize); printLine(GUICookie,szStr); @@ -6083,17 +5996,10 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) } } - - WritePerfDataInt("CLR method headers", "CLR method headers", "bytes", "bytes",methodHeaderSize); - WritePerfDataFloat("CLR method headers", "CLR method headers", "percentage", "percent",(float) ((methodHeaderSize * 100) / fileSize)); - sprintf_s(buf, MAX_MEMBER_LENGTH, "CLR method headers : %d", methodHeaderSize); sprintf_s(szString,SZSTRING_SIZE,"// %-40s (%5.2f%%)", buf, (double) (methodHeaderSize * 100) / fileSize); printLine(GUICookie,szStr); - WritePerfDataInt("Managed code", "Managed code", "bytes", "bytes",methodBodySize); - WritePerfDataFloat("Managed code", "Managed code", "percentage", "percent",(float) ((methodBodySize * 100) / fileSize)); - sprintf_s(buf, MAX_MEMBER_LENGTH, "Managed code : %d", methodBodySize); sprintf_s(szString,SZSTRING_SIZE,"// %-40s (%5.2f%%)", buf, (double) (methodBodySize * 100) / fileSize); printLine(GUICookie,szStr); @@ -6102,9 +6008,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) { DWORD sizeOfInitializedData = VAL32(g_pPELoader->ntHeaders32()->OptionalHeader.SizeOfInitializedData); - WritePerfDataInt("Data", "Data", "bytes", "bytes",sizeOfInitializedData); - WritePerfDataFloat("Data", "Data", "percentage", "percent",(float) ((sizeOfInitializedData * 100) / fileSize)); - sprintf_s(buf, MAX_MEMBER_LENGTH, "Data : %d", sizeOfInitializedData); sprintf_s(szString,SZSTRING_SIZE,"// %-40s (%5.2f%%)", buf, (double) (sizeOfInitializedData * 100) / fileSize); printLine(GUICookie,szStr); @@ -6118,9 +6021,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) { DWORD sizeOfInitializedData = VAL32(g_pPELoader->ntHeaders64()->OptionalHeader.SizeOfInitializedData); - WritePerfDataInt("Data", "Data", "bytes", "bytes",sizeOfInitializedData); - WritePerfDataFloat("Data", "Data", "percentage", "percent",(float) ((sizeOfInitializedData * 100) / fileSize)); - sprintf_s(buf, MAX_MEMBER_LENGTH, "Data : %d", sizeOfInitializedData); sprintf_s(szString,SZSTRING_SIZE,"// %-40s (%5.2f%%)", buf, (double) (sizeOfInitializedData * 100) / fileSize); printLine(GUICookie,szStr); @@ -6131,9 +6031,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) methodHeaderSize - methodBodySize; } - WritePerfDataInt("Unaccounted", "Unaccounted", "bytes", "bytes",size); - WritePerfDataFloat("Unaccounted", "Unaccounted", "percentage", "percent",(float) ((size * 100) / fileSize)); - sprintf_s(buf, MAX_MEMBER_LENGTH, "Unaccounted : %d", size); sprintf_s(szString,SZSTRING_SIZE,"// %-40s (%5.2f%%)", buf, (double) (size * 100) / fileSize); printLine(GUICookie,szStr); @@ -6144,7 +6041,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) { numberOfSections = VAL16(g_pPELoader->ntHeaders32()->FileHeader.NumberOfSections); - WritePerfDataInt("Num.of PE sections", "Num.of PE sections", "bytes", "bytes",numberOfSections); printLine(GUICookie,""); sprintf_s(szString,SZSTRING_SIZE,"// Num.of PE sections : %d", numberOfSections); printLine(GUICookie,szStr); @@ -6153,7 +6049,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) for (i=0; i < numberOfSections; ++i) { - WritePerfDataInt((char*)pSecHdr->Name,(char*)pSecHdr->Name, "bytes", "bytes",VAL32(pSecHdr->SizeOfRawData)); sprintf_s(szString,SZSTRING_SIZE,"// %-8s - %d", pSecHdr->Name, VAL32(pSecHdr->SizeOfRawData)); printLine(GUICookie,szStr); ++pSecHdr; @@ -6163,7 +6058,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) { numberOfSections = VAL16(g_pPELoader->ntHeaders64()->FileHeader.NumberOfSections); - WritePerfDataInt("Num.of PE sections", "Num.of PE sections", "bytes", "bytes",numberOfSections); printLine(GUICookie,""); sprintf_s(szString,SZSTRING_SIZE,"// Num.of PE sections : %d", numberOfSections); printLine(GUICookie,szStr); @@ -6172,7 +6066,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) for (i=0; i < numberOfSections; ++i) { - WritePerfDataInt((char*)pSecHdr->Name,(char*)pSecHdr->Name, "bytes", "bytes",pSecHdr->SizeOfRawData); sprintf_s(szString,SZSTRING_SIZE,"// %-8s - %d", pSecHdr->Name, pSecHdr->SizeOfRawData); printLine(GUICookie,szStr); ++pSecHdr; @@ -6194,7 +6087,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) else { DWORD Size = g_cbMetaData; - WritePerfDataInt("CLR meta-data size", "CLR meta-data size", "bytes", "bytes",Size); printLine(GUICookie,""); sprintf_s(szString,SZSTRING_SIZE,"// CLR meta-data size : %d", Size); printLine(GUICookie,szStr); @@ -6203,8 +6095,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) pITables->GetTableInfo(TBL_Module, &sizeRec, &count, NULL, NULL, NULL); TableSeen(TBL_Module); metaSize += size = count * sizeRec; \ - WritePerfDataInt("Module (count)", "Module (count)", "count", "count",count); - WritePerfDataInt("Module (bytes)", "Module (bytes)", "bytes", "bytes",size); sprintf_s(szString,SZSTRING_SIZE,"// %-14s- %4d (%d bytes)", "Module", count, size); \ printLine(GUICookie,szStr); @@ -6225,11 +6115,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) TableSeen(TBL_TypeDef); metaSize += size = count * sizeRec; - WritePerfDataInt("TypeDef (count)", "TypeDef (count)", "count", "count", count); - WritePerfDataInt("TypeDef (bytes)", "TypeDef (bytes)", "bytes", "bytes", size); - WritePerfDataInt("interfaces", "interfaces", "count", "count", interfaces); - WritePerfDataInt("explicitLayout", "explicitLayout", "count", "count", explicitLayout); - sprintf_s(buf, MAX_MEMBER_LENGTH, " TypeDef - %4d (%d bytes)", count, size); sprintf_s(szString,SZSTRING_SIZE,"// %-38s %d interfaces, %d explicit layout", buf, interfaces, explicitLayout); printLine(GUICookie,szStr); @@ -6241,8 +6126,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) if (count > 0) { metaSize += size = count * sizeRec; \ - WritePerfDataInt("TypeRef (count)", "TypeRef (count)", "count", "count", count); - WritePerfDataInt("TypeRef (bytes)", "TypeRef (bytes)", "bytes", "bytes", size); sprintf_s(szString,SZSTRING_SIZE,"// %-14s- %4d (%d bytes)", "TypeRef", count, size); \ printLine(GUICookie,szStr); } @@ -6266,12 +6149,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) { metaSize += size = count * sizeRec; - WritePerfDataInt("MethodDef (count)", "MethodDef (count)", "count", "count", count); - WritePerfDataInt("MethodDef (bytes)", "MethodDef (bytes)", "bytes", "bytes", size); - WritePerfDataInt("abstract", "abstract", "count", "count", abstract); - WritePerfDataInt("native", "native", "count", "count", native); - WritePerfDataInt("methodBodies", "methodBodies", "count", "count", methodBodies); - sprintf_s(buf, MAX_MEMBER_LENGTH, " MethodDef - %4d (%d bytes)", count, size); sprintf_s(szString,SZSTRING_SIZE,"// %-38s %d abstract, %d native, %d bodies", buf, abstract, native, methodBodies); printLine(GUICookie,szStr); @@ -6295,10 +6172,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) pITables->GetTableInfo(TBL_Field, &sizeRec, NULL, NULL, NULL, NULL); metaSize += size = count * sizeRec; - WritePerfDataInt("FieldDef (count)", "FieldDef (count)", "count", "count", count); - WritePerfDataInt("FieldDef (bytes)", "FieldDef (bytes)", "bytes", "bytes", size); - WritePerfDataInt("constant", "constant", "count", "count", constants); - sprintf_s(buf, MAX_MEMBER_LENGTH, " FieldDef - %4d (%d bytes)", count, size); sprintf_s(szString,SZSTRING_SIZE,"// %-38s %d constant", buf, constants); printLine(GUICookie,szStr); @@ -6345,7 +6218,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) if (sizeRec > 0) { metaSize += sizeRec; - WritePerfDataInt("Strings", "Strings", "bytes", "bytes",sizeRec); sprintf_s(szString,SZSTRING_SIZE,"// Strings - %5d bytes", sizeRec); printLine(GUICookie,szStr); } @@ -6354,7 +6226,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) if (sizeRec > 0) { metaSize += sizeRec; - WritePerfDataInt("Blobs", "Blobs", "bytes", "bytes",sizeRec); sprintf_s(szString,SZSTRING_SIZE,"// Blobs - %5d bytes", sizeRec); printLine(GUICookie,szStr); } @@ -6363,7 +6234,6 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) if (sizeRec > 0) { metaSize += sizeRec; - WritePerfDataInt("UserStrings", "UserStrings", "bytes", "bytes",sizeRec); sprintf_s(szString,SZSTRING_SIZE,"// UserStrings - %5d bytes", sizeRec); printLine(GUICookie,szStr); } @@ -6372,92 +6242,74 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie) if (sizeRec > 0) { metaSize += sizeRec; - WritePerfDataInt("Guids", "Guids", "bytes", "bytes", sizeRec); sprintf_s(szString,SZSTRING_SIZE,"// Guids - %5d bytes", sizeRec); printLine(GUICookie,szStr); } if (g_cbMetaData - metaSize > 0) { - WritePerfDataInt("Uncategorized", "Uncategorized", "bytes", "bytes",g_cbMetaData - metaSize); sprintf_s(szString,SZSTRING_SIZE,"// Uncategorized - %5d bytes", g_cbMetaData - metaSize); printLine(GUICookie,szStr); } if (miscCOMPlusSize != 0) { - WritePerfDataInt("CLR additional info", "CLR additional info", "bytes", "bytes", miscCOMPlusSize); sprintf_s(szString,SZSTRING_SIZE,"// CLR additional info : %d", miscCOMPlusSize); printLine(GUICookie,""); printLine(GUICookie,szStr); if (CORHeader->CodeManagerTable.Size != 0) { - WritePerfDataInt("CodeManagerTable", "CodeManagerTable", "bytes", "bytes", VAL32(CORHeader->CodeManagerTable.Size)); sprintf_s(szString,SZSTRING_SIZE,"// CodeManagerTable - %d", VAL32(CORHeader->CodeManagerTable.Size)); printLine(GUICookie,szStr); } if (CORHeader->VTableFixups.Size != 0) { - WritePerfDataInt("VTableFixups", "VTableFixups", "bytes", "bytes", VAL32(CORHeader->VTableFixups.Size)); sprintf_s(szString,SZSTRING_SIZE,"// VTableFixups - %d", VAL32(CORHeader->VTableFixups.Size)); printLine(GUICookie,szStr); } if (CORHeader->Resources.Size != 0) { - WritePerfDataInt("Resources", "Resources", "bytes", "bytes", VAL32(CORHeader->Resources.Size)); sprintf_s(szString,SZSTRING_SIZE,"// Resources - %d", VAL32(CORHeader->Resources.Size)); printLine(GUICookie,szStr); } } - WritePerfDataInt("CLR method headers", "CLR method headers", "count", "count", methodHeaderSize); sprintf_s(szString,SZSTRING_SIZE,"// CLR method headers : %d", methodHeaderSize); printLine(GUICookie,""); printLine(GUICookie,szStr); - WritePerfDataInt("Num.of method bodies", "Num.of method bodies", "count", "count",methodBodies); sprintf_s(szString,SZSTRING_SIZE,"// Num.of method bodies - %d", methodBodies); printLine(GUICookie,szStr); - WritePerfDataInt("Num.of fat headers", "Num.of fat headers", "count", "count", fatHeaders); sprintf_s(szString,SZSTRING_SIZE,"// Num.of fat headers - %d", fatHeaders); printLine(GUICookie,szStr); - WritePerfDataInt("Num.of tiny headers", "Num.of tiny headers", "count", "count", tinyHeaders); sprintf_s(szString,SZSTRING_SIZE,"// Num.of tiny headers - %d", tinyHeaders); printLine(GUICookie,szStr); if (deprecatedHeaders > 0) { - WritePerfDataInt("Num.of old headers", "Num.of old headers", "count", "count", deprecatedHeaders); sprintf_s(szString,SZSTRING_SIZE,"// Num.of old headers - %d", deprecatedHeaders); printLine(GUICookie,szStr); } if (fatSections != 0 || smallSections != 0) { - WritePerfDataInt("Num.of fat sections", "Num.of fat sections", "count", "count", fatSections); sprintf_s(szString,SZSTRING_SIZE,"// Num.of fat sections - %d", fatSections); printLine(GUICookie,szStr); - WritePerfDataInt("Num.of small section", "Num.of small section", "count", "count", smallSections); sprintf_s(szString,SZSTRING_SIZE,"// Num.of small sections - %d", smallSections); printLine(GUICookie,szStr); } - WritePerfDataInt("Managed code", "Managed code", "bytes", "bytes", methodBodySize); sprintf_s(szString,SZSTRING_SIZE,"// Managed code : %d", methodBodySize); printLine(GUICookie,""); printLine(GUICookie,szStr); if (methodBodies != 0) { - WritePerfDataInt("Ave method size", "Ave method size", "bytes", "bytes", methodBodySize / methodBodies); sprintf_s(szString,SZSTRING_SIZE,"// Ave method size - %d", methodBodySize / methodBodies); printLine(GUICookie,szStr); } if (pITables) pITables->Release(); - - if(g_fDumpToPerfWriter) - CloseHandle((char*) g_PerfDataFilePtr); } void DumpHexbytes(__inout __nullterminated char* szptr,BYTE *pb, DWORD fromPtr, DWORD toPtr, DWORD limPtr) @@ -7313,16 +7165,14 @@ void CloseNamespace(__inout __nullterminated char* szString) FILE* OpenOutput(_In_ __nullterminated const WCHAR* wzFileName) { -#ifdef HOST_WINDOWS FILE* pfile = NULL; - if(g_uCodePage == 0xFFFFFFFF) _wfopen_s(&pfile,wzFileName,W("wb")); - else _wfopen_s(&pfile,wzFileName,W("wt")); +#ifdef HOST_WINDOWS + int err = fopen_lp(&pfile,wzFileName, (g_uCodePage == 0xFFFFFFFF) ? W("wb") : W("wt")); #else - FILE* pfile = NULL; - _wfopen_s(&pfile,wzFileName,W("w")); + int err = fopen_lp(&pfile,wzFileName,W("w")); #endif - if(pfile) + if(err == 0) { if(g_uCodePage == CP_UTF8) fwrite("\357\273\277",3,1,pfile); else if(g_uCodePage == 0xFFFFFFFF) fwrite("\377\376",2,1,pfile); diff --git a/src/coreclr/ildasm/dman.cpp b/src/coreclr/ildasm/dman.cpp index d6409243d161ec..240f4a42e6f2aa 100644 --- a/src/coreclr/ildasm/dman.cpp +++ b/src/coreclr/ildasm/dman.cpp @@ -716,8 +716,7 @@ static void DumpResourceFile(void *GUICookie, BYTE *pRes, DWORD dwOffset, LPCWST if (g_pFile != NULL) // embedded resource -- dump as .resources file { FILE *pF = NULL; - _wfopen_s(&pF, pParam->wzFileName, W("wb")); - if (pF) + if (fopen_lp(&pF, pParam->wzFileName, W("wb")) == 0) { struct Param { diff --git a/src/coreclr/ildasm/dres.cpp b/src/coreclr/ildasm/dres.cpp index e1612130ca6065..0e1747b9327198 100644 --- a/src/coreclr/ildasm/dres.cpp +++ b/src/coreclr/ildasm/dres.cpp @@ -240,8 +240,7 @@ DWORD DumpResourceToFile(_In_ __nullterminated WCHAR* wzFileName) ret = 1; #ifdef RES_FILE_DUMP_ENABLED - _wfopen_s(&pF,wzFileName,L"wb"); - if(pF) + if(fopen_lp(&pF,wzFileName,L"wb") == 0) { // Dump them to pF // Write dummy header diff --git a/src/coreclr/ildasm/ildasmpch.h b/src/coreclr/ildasm/ildasmpch.h index 5bb192dd14e10e..af7295f12a21aa 100644 --- a/src/coreclr/ildasm/ildasmpch.h +++ b/src/coreclr/ildasm/ildasmpch.h @@ -13,6 +13,7 @@ #include #include #include +#include "dn-stdio.h" using std::min; using std::max; diff --git a/src/coreclr/ildasm/windasm.cpp b/src/coreclr/ildasm/windasm.cpp index 31e44efda73808..4bf36bb6c08962 100644 --- a/src/coreclr/ildasm/windasm.cpp +++ b/src/coreclr/ildasm/windasm.cpp @@ -38,8 +38,6 @@ extern BOOL g_fDumpSummary; extern BOOL g_fDecompile; // still in progress extern BOOL g_fShowRefs; -extern BOOL g_fDumpToPerfWriter; - extern BOOL g_fShowBytes; extern BOOL g_fShowSource; extern BOOL g_fInsertSourceLines; @@ -230,10 +228,6 @@ int ProcessOneArg(_In_ __nullterminated char* szArg, _Out_ char** ppszObjFileNam { g_fDumpSummary = TRUE; } - else if (_stricmp(szOpt, "per") == 0) - { - g_fDumpToPerfWriter = TRUE; - } else if (_stricmp(szOpt, "for") == 0) { g_fForwardDecl = TRUE; diff --git a/src/coreclr/inc/blobfetcher.h b/src/coreclr/inc/blobfetcher.h index ca9f420c0b9374..0402f8bfc6c887 100644 --- a/src/coreclr/inc/blobfetcher.h +++ b/src/coreclr/inc/blobfetcher.h @@ -17,6 +17,7 @@ #define __BLOB_FETCHER_H_ #include +#include class CBlobFetcher @@ -84,7 +85,7 @@ class CBlobFetcher unsigned ComputeOffset(_In_ char *ptr) const; // Write out the section to the stream - HRESULT Write(HANDLE file); + HRESULT Write(FILE* file); // Write out the section to memory HRESULT WriteMem(void ** pMem); diff --git a/src/coreclr/inc/fstream.h b/src/coreclr/inc/fstream.h deleted file mode 100644 index 4b43d95228698c..00000000000000 --- a/src/coreclr/inc/fstream.h +++ /dev/null @@ -1,49 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - - -#ifndef __FSTREAM_H_INCLUDED__ -#define __FSTREAM_H_INCLUDED__ - -#include - -class CFileStream : public IStream -{ - public: - CFileStream(); - virtual ~CFileStream(); - - HRESULT OpenForRead(LPCWSTR wzFilePath); - HRESULT OpenForWrite(LPCWSTR wzFilePath); - - // IUnknown methods: - STDMETHODIMP_(ULONG) AddRef(); - STDMETHODIMP_(ULONG) Release(); - STDMETHODIMP QueryInterface(REFIID riid, LPVOID *ppvObj); - - // ISequentialStream methods: - STDMETHODIMP Read(void *pv, ULONG cb, ULONG *pcbRead); - STDMETHODIMP Write(void const *pv, ULONG cb, ULONG *pcbWritten); - - // IStream methods: - STDMETHODIMP Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition); - STDMETHODIMP SetSize(ULARGE_INTEGER libNewSize); - STDMETHODIMP CopyTo(IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten); - STDMETHODIMP Commit(DWORD grfCommitFlags); - STDMETHODIMP Revert(); - STDMETHODIMP LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType); - STDMETHODIMP UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType); - STDMETHODIMP Stat(STATSTG *pstatstg, DWORD grfStatFlag); - STDMETHODIMP Clone(IStream **ppIStream); - - private: - BOOL Close(); - - private: - LONG _cRef; - HANDLE _hFile; - -}; - -#endif - diff --git a/src/coreclr/inc/longfilepathwrappers.h b/src/coreclr/inc/longfilepathwrappers.h index 88c22095d764e3..ca028bf6d625a1 100644 --- a/src/coreclr/inc/longfilepathwrappers.h +++ b/src/coreclr/inc/longfilepathwrappers.h @@ -25,6 +25,8 @@ CreateFileWrapper( _In_opt_ HANDLE hTemplateFile ); +int u16_fopen_wrapper(FILE** stream, const WCHAR* filename, const WCHAR* mode); + BOOL CopyFileExWrapper( _In_ LPCWSTR lpExistingFileName, diff --git a/src/coreclr/inc/winwrap.h b/src/coreclr/inc/winwrap.h index 4f5569343cbfac..2e8946aa50205f 100644 --- a/src/coreclr/inc/winwrap.h +++ b/src/coreclr/inc/winwrap.h @@ -42,9 +42,11 @@ #ifdef HOST_WINDOWS #define WszLoadLibrary LoadLibraryExWrapper #define WszCreateFile CreateFileWrapper +#define fopen_lp u16_fopen_wrapper #else // HOST_WINDOWS #define WszLoadLibrary LoadLibraryExW #define WszCreateFile CreateFileW +#define fopen_lp u16_fopen_s #endif // HOST_WINDOWS //APIS which have a buffer as an out parameter diff --git a/src/coreclr/minipal/Unix/CMakeLists.txt b/src/coreclr/minipal/Unix/CMakeLists.txt index 31528782153f54..7e1c1a4e9698b9 100644 --- a/src/coreclr/minipal/Unix/CMakeLists.txt +++ b/src/coreclr/minipal/Unix/CMakeLists.txt @@ -1,6 +1,7 @@ set(SOURCES doublemapping.cpp dn-u16.cpp + dn-stdio.cpp ) add_library(coreclrminipal_objects diff --git a/src/coreclr/minipal/Unix/dn-stdio.cpp b/src/coreclr/minipal/Unix/dn-stdio.cpp new file mode 100644 index 00000000000000..249b93d427bc6f --- /dev/null +++ b/src/coreclr/minipal/Unix/dn-stdio.cpp @@ -0,0 +1,149 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include + +typedef char16_t WCHAR; +typedef int32_t HRESULT; + +#include +#include +#include +#include +#include + +int u16_fopen_s(FILE** stream, const WCHAR* path, const WCHAR* mode) +{ + size_t pathLen = u16_strlen(path); + size_t pathU8Len = minipal_get_length_utf16_to_utf8((CHAR16_T*)path, pathLen, 0); + char* pathU8 = new char[pathU8Len + 1]; + size_t ret = minipal_convert_utf16_to_utf8((CHAR16_T*)path, pathLen, pathU8, pathU8Len, 0); + pathU8[ret] = '\0'; + + size_t modeLen = u16_strlen(mode); + size_t modeU8Len = minipal_get_length_utf16_to_utf8((CHAR16_T*)mode, modeLen, 0); + char* modeU8 = new char[modeU8Len + 1]; + ret = minipal_convert_utf16_to_utf8((CHAR16_T*)mode, modeLen, modeU8, modeU8Len, 0); + modeU8[ret] = '\0'; + + FILE* result = fopen(pathU8, modeU8); + int err = errno; + + delete[] pathU8; + delete[] modeU8; + + if (result) + { + *stream = result; + return 0; + } + else + { + *stream = NULL; + return err; + } +} + +int64_t fgetsize(FILE* stream) +{ + fpos_t current; + fgetpos(stream, ¤t); + fseek(stream, 0, SEEK_END); + int64_t length = ftell(stream); + fsetpos(stream, ¤t); + return length; +} + +int64_t ftell_64(FILE* stream) +{ + return ftell(stream); +} + +int fsetpos_64(FILE* stream, int64_t pos) +{ + return fseek(stream, pos, SEEK_SET); +} + +#define FACILITY_WIN32 7 +#define HRESULT_FROM_WIN32(x) ((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : ((HRESULT) (((x) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000))) + +#define ERROR_SUCCESS 0L +#define ERROR_FILE_NOT_FOUND 2L +#define ERROR_PATH_NOT_FOUND 3L +#define ERROR_TOO_MANY_OPEN_FILES 4L +#define ERROR_ACCESS_DENIED 5L +#define ERROR_INVALID_HANDLE 6L +#define ERROR_NOT_ENOUGH_MEMORY 8L +#define ERROR_WRITE_FAULT 29L +#define ERROR_GEN_FAILURE 31L +#define ERROR_DISK_FULL 112L +#define ERROR_DIR_NOT_EMPTY 145L +#define ERROR_BAD_PATHNAME 161L +#define ERROR_BUSY 170L +#define ERROR_ALREADY_EXISTS 183L +#define ERROR_FILENAME_EXCED_RANGE 206L + +HRESULT HRESULTFromErrno() +{ + // maps the common I/O errors + // based on FILEGetLastErrorFromErrno + + int32_t win32Err; + + switch(errno) + { + case 0: + win32Err = ERROR_SUCCESS; + break; + case ENAMETOOLONG: + win32Err = ERROR_FILENAME_EXCED_RANGE; + break; + case ENOTDIR: + win32Err = ERROR_PATH_NOT_FOUND; + break; + case ENOENT: + win32Err = ERROR_FILE_NOT_FOUND; + break; + case EACCES: + case EPERM: + case EROFS: + case EISDIR: + win32Err = ERROR_ACCESS_DENIED; + break; + case EEXIST: + win32Err = ERROR_ALREADY_EXISTS; + break; + case ENOTEMPTY: + win32Err = ERROR_DIR_NOT_EMPTY; + break; + case EBADF: + win32Err = ERROR_INVALID_HANDLE; + break; + case ENOMEM: + win32Err = ERROR_NOT_ENOUGH_MEMORY; + break; + case EBUSY: + win32Err = ERROR_BUSY; + break; + case ENOSPC: + case EDQUOT: + win32Err = ERROR_DISK_FULL; + break; + case ELOOP: + win32Err = ERROR_BAD_PATHNAME; + break; + case EIO: + win32Err = ERROR_WRITE_FAULT; + break; + case EMFILE: + win32Err = ERROR_TOO_MANY_OPEN_FILES; + break; + case ERANGE: + win32Err = ERROR_BAD_PATHNAME; + break; + default: + win32Err = ERROR_GEN_FAILURE; + } + + return HRESULT_FROM_WIN32(win32Err); +} diff --git a/src/coreclr/minipal/Windows/CMakeLists.txt b/src/coreclr/minipal/Windows/CMakeLists.txt index 90ed6ddf4ea954..ac0af0399d72b4 100644 --- a/src/coreclr/minipal/Windows/CMakeLists.txt +++ b/src/coreclr/minipal/Windows/CMakeLists.txt @@ -1,6 +1,7 @@ set(SOURCES doublemapping.cpp dn-u16.cpp + dn-stdio.cpp ) add_library(coreclrminipal STATIC diff --git a/src/coreclr/minipal/Windows/dn-stdio.cpp b/src/coreclr/minipal/Windows/dn-stdio.cpp new file mode 100644 index 00000000000000..9f43dbe4c7bafd --- /dev/null +++ b/src/coreclr/minipal/Windows/dn-stdio.cpp @@ -0,0 +1,98 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include +#include + +int u16_fopen_s(FILE** stream, const WCHAR* path, const WCHAR* mode) +{ + return _wfopen_s(stream, path, mode); +} + +int64_t fgetsize(FILE* stream) +{ + fpos_t current; + fgetpos(stream, ¤t); + fseek(stream, 0, SEEK_END); + int64_t length = _ftelli64(stream); + fsetpos(stream, ¤t); + return length; +} + +int64_t ftell_64(FILE* stream) +{ + return _ftelli64(stream); +} + +int fsetpos_64(FILE* stream, int64_t pos) +{ + fpos_t fpos = pos; + return fsetpos(stream, &fpos); +} + +HRESULT HRESULTFromErrno() +{ + // maps the common I/O errors + // based on FILEGetLastErrorFromErrno + + // stdio functions aren't guaranteed to preserve GetLastError. + // errno should be used as source of truth. + + DWORD win32Err; + + switch(errno) + { + case 0: + win32Err = ERROR_SUCCESS; + break; + case ENAMETOOLONG: + win32Err = ERROR_FILENAME_EXCED_RANGE; + break; + case ENOTDIR: + win32Err = ERROR_PATH_NOT_FOUND; + break; + case ENOENT: + win32Err = ERROR_FILE_NOT_FOUND; + break; + case EACCES: + case EPERM: + case EROFS: + case EISDIR: + win32Err = ERROR_ACCESS_DENIED; + break; + case EEXIST: + win32Err = ERROR_ALREADY_EXISTS; + break; + case ENOTEMPTY: + win32Err = ERROR_DIR_NOT_EMPTY; + break; + case EBADF: + win32Err = ERROR_INVALID_HANDLE; + break; + case ENOMEM: + win32Err = ERROR_NOT_ENOUGH_MEMORY; + break; + case EBUSY: + win32Err = ERROR_BUSY; + break; + case ENOSPC: + win32Err = ERROR_DISK_FULL; + break; + case ELOOP: + win32Err = ERROR_BAD_PATHNAME; + break; + case EIO: + win32Err = ERROR_WRITE_FAULT; + break; + case EMFILE: + win32Err = ERROR_TOO_MANY_OPEN_FILES; + break; + case ERANGE: + win32Err = ERROR_BAD_PATHNAME; + break; + default: + win32Err = ERROR_GEN_FAILURE; + } + + return HRESULT_FROM_WIN32(win32Err); +} diff --git a/src/coreclr/minipal/dn-stdio.h b/src/coreclr/minipal/dn-stdio.h new file mode 100644 index 00000000000000..41464d2069c8e4 --- /dev/null +++ b/src/coreclr/minipal/dn-stdio.h @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include +#include + +// +// Wrappers for stdio with UTF-16 path. +// + +int u16_fopen_s(FILE** stream, const WCHAR* path, const WCHAR* mode); +int64_t fgetsize(FILE* stream); +int64_t ftell_64(FILE* stream); +int fsetpos_64(FILE* stream, int64_t pos); +HRESULT HRESULTFromErrno(); diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index 397c0dc4f7a792..88e67e10c71700 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -3625,7 +3625,6 @@ PALIMPORT DLLEXPORT double __cdecl PAL_wcstod(const WCHAR *, WCHAR **); PALIMPORT errno_t __cdecl _wcslwr_s(WCHAR *, size_t sz); PALIMPORT int __cdecl _wtoi(const WCHAR *); -PALIMPORT FILE * __cdecl _wfopen(const WCHAR *, const WCHAR *); inline int _stricmp(const char* a, const char* b) { diff --git a/src/coreclr/pal/inc/rt/palrt.h b/src/coreclr/pal/inc/rt/palrt.h index 746f219a57c4b0..829a75cd185909 100644 --- a/src/coreclr/pal/inc/rt/palrt.h +++ b/src/coreclr/pal/inc/rt/palrt.h @@ -584,7 +584,6 @@ The wrappers below are simple implementations that may not be as robust as compl Remember to fix the errcode definition in safecrt.h. */ -#define _wfopen_s _wfopen_unsafe #define fopen_s _fopen_unsafe #define _vscprintf _vscprintf_unsafe @@ -616,17 +615,6 @@ inline int __cdecl _vscprintf_unsafe(const char *_Format, va_list _ArgList) } } -inline errno_t __cdecl _wfopen_unsafe(FILE * *ff, const WCHAR *fileName, const WCHAR *mode) -{ - FILE *result = _wfopen(fileName, mode); - if(result == 0) { - return 1; - } else { - *ff = result; - return 0; - } -} - inline errno_t __cdecl _fopen_unsafe(FILE * *ff, const char *fileName, const char *mode) { FILE *result = fopen(fileName, mode); diff --git a/src/coreclr/pal/src/cruntime/wchar.cpp b/src/coreclr/pal/src/cruntime/wchar.cpp index 06e2e9940c4019..959ebd6f576fb5 100644 --- a/src/coreclr/pal/src/cruntime/wchar.cpp +++ b/src/coreclr/pal/src/cruntime/wchar.cpp @@ -943,51 +943,3 @@ PAL_wcstod( const wchar_16 * nptr, wchar_16 **endptr ) PERF_EXIT(wcstod); return RetVal; } - -/*++ -Function: - _wfopen - -see MSDN doc. - ---*/ -extern "C" -FILE * -__cdecl -_wfopen( - const wchar_16 *fileName, - const wchar_16 *mode) -{ - CHAR mbFileName[ MAX_PATH ]; - CHAR mbMode[ 10 ]; - FILE * filePtr = NULL; - - PERF_ENTRY(_wfopen); - ENTRY("_wfopen(fileName:%p (%S), mode:%p (%S))\n", fileName, fileName, mode, mode); - - _ASSERTE(fileName != NULL); - _ASSERTE(mode != NULL); - - /* Convert the parameters to ASCII and defer to PAL_fopen */ - if ( WideCharToMultiByte( CP_ACP, 0, fileName, -1, mbFileName, - sizeof mbFileName, NULL, NULL ) != 0 ) - { - if ( WideCharToMultiByte( CP_ACP, 0, mode, -1, mbMode, - sizeof mbMode, NULL, NULL ) != 0 ) - { - filePtr = fopen(mbFileName, mbMode); - } - else - { - ERROR( "An error occurred while converting mode to ANSI.\n" ); - } - } - else - { - ERROR( "An error occurred while converting" - " fileName to ANSI string.\n" ); - } - LOGEXIT("_wfopen returning FILE* %p\n", filePtr); - PERF_EXIT(_wfopen); - return filePtr; -} diff --git a/src/coreclr/pal/tests/palsuite/CMakeLists.txt b/src/coreclr/pal/tests/palsuite/CMakeLists.txt index c61121186f831c..6f1251ed42e4a2 100644 --- a/src/coreclr/pal/tests/palsuite/CMakeLists.txt +++ b/src/coreclr/pal/tests/palsuite/CMakeLists.txt @@ -144,13 +144,6 @@ add_executable_clr(paltests c_runtime/_wcsicmp/test1/test1.cpp c_runtime/_wcslwr_s/test1/test1.cpp c_runtime/_wcsnicmp/test1/test1.cpp - c_runtime/_wfopen/test1/test1.cpp - c_runtime/_wfopen/test2/test2.cpp - c_runtime/_wfopen/test3/test3.cpp - c_runtime/_wfopen/test4/test4.cpp - c_runtime/_wfopen/test5/test5.cpp - c_runtime/_wfopen/test6/test6.cpp - c_runtime/_wfopen/test7/test7.cpp c_runtime/_wtoi/test1/test1.cpp #debug_api/DebugBreak/test1/test1.cpp debug_api/OutputDebugStringA/test1/helper.cpp diff --git a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test1/test1.cpp b/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test1/test1.cpp deleted file mode 100644 index 7b96770bc54a9c..00000000000000 --- a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test1/test1.cpp +++ /dev/null @@ -1,78 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/*===================================================================== -** -** Source: test1.c -** -** Purpose: Tests the PAL implementation of the _wfopen function. -** This test simply attempts to open a number of files with -** different modes. It checks to ensure a valid file -** pointer is returned. It doesn't do any checking to -** ensure the mode is really what it claims. -** - -** -**===================================================================*/ - - -#define UNICODE -#include - -struct testCase -{ - int CorrectResult; - WCHAR mode[20]; -}; - -PALTEST(c_runtime__wfopen_test1_paltest_wfopen_test1, "c_runtime/_wfopen/test1/paltest_wfopen_test1") -{ - - FILE *fp; - WCHAR name[128]; - WCHAR base[] = {'t','e','s','t','f','i','l','e','s','\0'}; - char * PrintResult; - int i; - - struct testCase testCases[] = - { - {0, {'r','\0' }}, {1, {'w','\0'}}, {1, {'a','\0'}}, - {0, {'r','+','\0'}}, {1, {'w','+','\0'}}, {1, {'a','+','\0'}}, - {1, {'w','t','\0'}}, {1, {'w','b','\0'}}, {1, {'w','S','\0'}}, - {1, {'w','c','\0'}}, {1, {'w','n','\0'}}, {1, {'w', 'R','\0'}}, - {1, {'w','T','\0'}}, {0, {'t','w','\0'}}, {0, {'.','\0'}} - }; - - if (PAL_Initialize(argc, argv)) - { - return FAIL; - } - - - - for(i = 0; i < sizeof(testCases) / sizeof(struct testCase); i++) - { - wcscpy(name,base); - wcscat(name,testCases[i].mode); - - fp = _wfopen(name,testCases[i].mode); - - if ((fp == 0 && testCases[i].CorrectResult != 0) || - (testCases[i].CorrectResult == 0 && fp != 0) ) - { - PrintResult = convertC(testCases[i].mode); - Fail("ERROR: fopen returned incorrectly " - "opening a file in %s mode. Perhaps it opened a " - "read only file which didn't exist and returned a correct " - "pointer?",PrintResult); - free(PrintResult); - } - - memset(name, '\0', 128 * sizeof(name[0])); - } - - PAL_Terminate(); - return PASS; -} - - diff --git a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test2/test2.cpp b/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test2/test2.cpp deleted file mode 100644 index ccc1a105502e37..00000000000000 --- a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test2/test2.cpp +++ /dev/null @@ -1,67 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/*===================================================================== -** -** Source: test2.c -** -** Purpose: Tests the PAL implementation of the _wfopen function. -** Test to ensure that you can write to a 'w' mode file. -** And that you can't read from a 'w' mode file. -** -** Depends: -** fprintf -** fseek -** fgets -** - -** -**===================================================================*/ - -#define UNICODE -#include - -PALTEST(c_runtime__wfopen_test2_paltest_wfopen_test2, "c_runtime/_wfopen/test2/paltest_wfopen_test2") -{ - - FILE *fp; - char buffer[128]; - WCHAR filename[] = {'t','e','s','t','f','i','l','e','\0'}; - WCHAR write[] = {'w','\0'}; - - if (PAL_Initialize(argc, argv)) - { - return FAIL; - } - - - if( (fp = _wfopen( filename, write )) == NULL ) - { - Fail( "ERROR: The file failed to open with 'w' mode.\n" ); - } - - /* Test that you can write */ - if(fprintf(fp,"%s","some text") <= 0) - { - Fail("ERROR: Attempted to WRITE to a file opened with 'w' mode " - "but fprintf failed. Either fopen or fprintf have problems."); - } - - if(fseek(fp, 0, SEEK_SET)) - { - Fail("ERROR: fseek failed, and this test depends on it."); - } - - /* Test that you can't read */ - if(fgets(buffer,10,fp) != NULL) - { - Fail("ERROR: Tried to READ from a file with only 'w' mode set. " - "This should fail, but fgets didn't return NULL. " - "Either fgets or fopen is broken."); - } - - PAL_Terminate(); - return PASS; -} - - diff --git a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test3/test3.cpp b/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test3/test3.cpp deleted file mode 100644 index 081f9d9bb964cc..00000000000000 --- a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test3/test3.cpp +++ /dev/null @@ -1,69 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/*===================================================================== -** -** Source: test3.c -** -** Purpose: Tests the PAL implementation of the _wfopen function. -** Test to ensure that you can write to a 'w+' mode file. -** And that you can read from a 'w+' mode file. -** -** Depends: -** fprintf -** fseek -** fgets -** - -** -**===================================================================*/ - -#define UNICODE - -#include - -PALTEST(c_runtime__wfopen_test3_paltest_wfopen_test3, "c_runtime/_wfopen/test3/paltest_wfopen_test3") -{ - - FILE *fp; - char buffer[128]; - WCHAR filename[] = {'t','e','s','t','f','i','l','e','\0'}; - WCHAR writeplus[] = {'w','+','\0'}; - - if (PAL_Initialize(argc, argv)) - { - return FAIL; - } - - - /* Open a file with 'w+' mode */ - if( (fp = _wfopen( filename, writeplus )) == NULL ) - { - Fail( "ERROR: The file failed to open with 'w+' mode.\n" ); - } - - /* Write some text to the file */ - if(fprintf(fp,"%s","some text") <= 0) - { - Fail("ERROR: Attempted to WRITE to a file opened with 'w+' mode " - "but fprintf failed. Either fopen or fprintf have problems."); - } - - if(fseek(fp, 0, SEEK_SET)) - { - Fail("ERROR: fseek failed, and this test depends on it."); - } - - /* Attempt to read from the 'w+' only file, should pass */ - if(fgets(buffer,10,fp) == NULL) - { - Fail("ERROR: Tried to READ from a file with 'w+' mode set. " - "This should succeed, but fgets returned NULL. Either fgets " - "or fopen is broken."); - } - - PAL_Terminate(); - return PASS; -} - - diff --git a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test4/test4.cpp b/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test4/test4.cpp deleted file mode 100644 index 93f137f0a9a8fd..00000000000000 --- a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test4/test4.cpp +++ /dev/null @@ -1,86 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/*===================================================================== -** -** Source: test4.c -** -** Purpose: Tests the PAL implementation of the _wfopen function. -** Test to ensure that you can't write to a 'r' mode file. -** And that you can read from a 'r' mode file. -** -** Depends: -** fprintf -** fclose -** fgets -** - -** -**===================================================================*/ - -#define UNICODE - -#include - -PALTEST(c_runtime__wfopen_test4_paltest_wfopen_test4, "c_runtime/_wfopen/test4/paltest_wfopen_test4") -{ - - FILE *fp; - char buffer[128]; - WCHAR filename[] = {'t','e','s','t','f','i','l','e','\0'}; - WCHAR write[] = {'w','\0'}; - WCHAR read[] = {'r','\0'}; - - if (PAL_Initialize(argc, argv)) - { - return FAIL; - } - - - /* Open a file with 'w' mode */ - if( (fp = _wfopen( filename, write )) == NULL ) - { - Fail( "ERROR: The file failed to open with 'w' mode.\n" ); - } - - /* Write some text to the file */ - if(fprintf(fp,"%s","some text") <= 0) - { - Fail("ERROR: Attempted to WRITE to a file opened with 'w' mode " - "but fprintf failed. Either fopen or fprintf have problems."); - } - - if(fclose(fp)) - { - Fail("ERROR: Attempted to close a file, but fclose failed. " - "This test depends upon it."); - } - - /* Open a file with 'r' mode */ - if( (fp = _wfopen( filename, read )) == NULL ) - { - Fail( "ERROR: The file failed to open with 'r' mode.\n" ); - } - - /* Attempt to read from the 'r' only file, should pass */ - if(fgets(buffer,10,fp) == NULL) - { - Fail("ERROR: Tried to READ from a file with 'r' mode set. " - "This should succeed, but fgets returned NULL. Either fgets " - "or fopen is broken."); - } - - /* Write some text to the file */ - if(fprintf(fp,"%s","some text") > 0) - { - Fail("ERROR: Attempted to WRITE to a file opened with 'r' mode " - "but fprintf succeeded It should have failed. " - "Either fopen or fprintf have problems."); - } - - - PAL_Terminate(); - return PASS; -} - - diff --git a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test5/test5.cpp b/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test5/test5.cpp deleted file mode 100644 index 10839d1c6c9fce..00000000000000 --- a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test5/test5.cpp +++ /dev/null @@ -1,81 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/*===================================================================== -** -** Source: test5.c -** -** Purpose: Tests the PAL implementation of the _wfopen function. -** Test to ensure that you can write to a 'r+' mode file. -** And that you can read from a 'r+' mode file. -** -** Depends: -** fprintf -** fclose -** fgets -** fseek -** - -** -**===================================================================*/ -#define UNICODE - -#include - -PALTEST(c_runtime__wfopen_test5_paltest_wfopen_test5, "c_runtime/_wfopen/test5/paltest_wfopen_test5") -{ - - FILE *fp; - char buffer[128]; - WCHAR filename[] = {'t','e','s','t','f','i','l','e','\0'}; - WCHAR write[] = {'w','\0'}; - WCHAR readplus[] = {'r','+','\0'}; - - if (PAL_Initialize(argc, argv)) - { - return FAIL; - } - - - /* Open a file with 'w' mode */ - if( (fp = _wfopen( filename,write )) == NULL ) - { - Fail( "ERROR: The file failed to open with 'w' mode.\n" ); - } - - if(fclose(fp)) - { - Fail("ERROR: Attempted to close a file, but fclose failed. " - "This test depends upon it."); - } - - if( (fp = _wfopen( filename, readplus )) == NULL ) - { - Fail( "ERROR: The file failed to open with 'r+' mode.\n" ); - } - - /* Write some text to the file */ - if(fprintf(fp,"%s","some text") <= 0) - { - Fail("ERROR: Attempted to WRITE to a file opened with 'r+' mode " - "but fprintf failed. Either fopen or fprintf have problems."); - } - - if(fseek(fp, 0, SEEK_SET)) - { - Fail("ERROR: fseek failed, and this test depends on it."); - } - - /* Attempt to read from the 'r+' only file, should pass */ - if(fgets(buffer,10,fp) == NULL) - { - Fail("ERROR: Tried to READ from a file with 'r+' mode set. " - "This should succeed, but fgets returned NULL. Either fgets " - "or fopen is broken."); - } - - PAL_Terminate(); - return PASS; -} - - diff --git a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test6/test6.cpp b/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test6/test6.cpp deleted file mode 100644 index 269ca1f5cc1a18..00000000000000 --- a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test6/test6.cpp +++ /dev/null @@ -1,151 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/*===================================================================== -** -** Source: test6.c -** -** Purpose: Tests the PAL implementation of the _wfopen function. -** Test to ensure that you can write to an 'a' mode file. -** And that you can't read from a 'a' mode file. -** -** Depends: -** fprintf -** fgets -** fseek -** - -** -**===================================================================*/ - -#include - -PALTEST(c_runtime__wfopen_test6_paltest_wfopen_test6, "c_runtime/_wfopen/test6/paltest_wfopen_test6") -{ - FILE *fp; - char buffer[128]; - WCHAR filename[] = {'t','e','s','t','f','i','l','e','\0'}; - WCHAR filename2[] = {'t','e','s','t','f','i','l','e','2','\0'}; - WCHAR append[] = {'a','\0'}; - - if (PAL_Initialize(argc, argv)) - { - return FAIL; - } - - /* Open a file with 'a' mode */ - if( (fp = _wfopen( filename, append )) == NULL ) - { - Fail( "ERROR: file failed to open with 'a' mode.\n" ); - } - - /* Write some text to the file */ - if(fprintf(fp,"%s","some text") <= 0) - { - Fail("ERROR: Attempted to WRITE to a file opened with 'a' mode " - "but fprintf failed. Either fopen or fprintf have problems."); - } - - if(fseek(fp, 0, SEEK_SET)) - { - Fail("ERROR: fseek failed, and this test depends on it."); - } - - /* Attempt to read from the 'a' only file, should fail */ - if(fgets(buffer,10,fp) != NULL) - { - Fail("ERROR: Tried to READ from a file with 'a' mode set. " - "This should fail, but fgets returned success. Either fgets " - "or fopen is broken."); - } - - // Delete the file now that we're done with it. - if (fclose(fp) != 0) - { - Fail("ERROR: fclose failed to close \"testfile\".\n"); - } - - if (!DeleteFileA("testfile")) - { - Fail("ERROR: Failed to delete \"testfile\".\n" - " Error is %d\n", - GetLastError()); - } - - /* Attempt to write to a file after using 'a' and fseek */ - fp = _wfopen(filename2, append); - if(fp == NULL) - { - Fail("ERROR: _wfopen failed to be created with 'a' mode.\n"); - } - - /* write text to the file initially */ - if(fprintf(fp,"%s","abcd") <= 0) - { - Fail("ERROR: Attempted to WRITE to a file opened with 'a' mode " - "but fprintf failed. Either fopen or fprintf have problems.\n"); - } - - /* set the pointer to the front of the file */ - if(fseek(fp, 0, SEEK_SET)) - { - Fail("ERROR: fseek failed, and this test depends on it.\n"); - } - - /* using 'a' should still write to the end of the file, not the front */ - if(fputs("efgh", fp) < 0) - { - Fail("ERROR: Attempt to WRITE with fputs to the beginning of a file " - "opened with 'a' mode succeeded.\n"); - } - - /* set the pointer to the front of the file */ - if(fseek(fp, 0, SEEK_SET)) - { - Fail("ERROR: fseek failed, and this test depends on it.\n"); - } - - /* a file with 'a' mode can only write, so close the file before reading */ - if(fclose(fp)) - { - Fail("ERROR: fclose failed when it should have succeeded.\n"); - } - - /* open the file again to read */ - fp = fopen("testfile2","r"); - if(fp == NULL) - { - Fail("ERROR: fopen failed to open the file using 'r' mode"); - } - - /* Attempt to read from the 'a' only file, should succeed */ - if(fgets(buffer,10,fp) == NULL) - { - Fail("ERROR: Tried to READ from a file with 'a' mode set. " - "This should pass, but fgets returned failure. Either fgets " - "or fopen is broken.\n"); - } - - /* Compare what was read and what should have been in the file */ - if(memcmp(buffer,"abcdefgh",8)) - { - Fail("ERROR: The string read should have equaled 'abcdefgh' " - "but instead it is %s\n", buffer); - } - - // Delete the file now that we're done with it. - if (fclose(fp) != 0) - { - Fail("ERROR: fclose failed to close \"testfile\".\n"); - } - - if (!DeleteFileA("testfile2")) - { - Fail("ERROR: Failed to delete \"testfile2\".\n"); - } - - PAL_Terminate(); - return PASS; -} - - diff --git a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test7/test7.cpp b/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test7/test7.cpp deleted file mode 100644 index 419707a13acdd8..00000000000000 --- a/src/coreclr/pal/tests/palsuite/c_runtime/_wfopen/test7/test7.cpp +++ /dev/null @@ -1,118 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/*===================================================================== -** -** Source: test7.c -** -** Purpose: Tests the PAL implementation of the _wfopen function. -** Test to ensure that you can write to an 'a+' mode file. -** And that you can read from a 'a+' mode file. -** -** Depends: -** fprintf -** fgets -** fseek -** - -** -**===================================================================*/ -#define UNICODE - -#include - -PALTEST(c_runtime__wfopen_test7_paltest_wfopen_test7, "c_runtime/_wfopen/test7/paltest_wfopen_test7") -{ - - FILE *fp; - char buffer[128]; - WCHAR filename[] = {'t','e','s','t','f','i','l','e','\0'}; - WCHAR filename2[] = {'t','e','s','t','f','i','l','e','2','\0'}; - WCHAR appendplus[] = {'a','+','\0'}; - - if (PAL_Initialize(argc, argv)) - { - return FAIL; - } - - - /* Open a file with 'a+' mode */ - if( (fp = _wfopen( filename, appendplus )) == NULL ) - { - Fail( "ERROR: The file failed to open with 'a+' mode.\n" ); - } - - /* Write some text to the file */ - if(fprintf(fp,"%s","some text") <= 0) - { - Fail("ERROR: Attempted to WRITE to a file opened with 'a+' mode " - "but fprintf failed. Either fopen or fprintf have problems."); - } - - if(fseek(fp, 0, SEEK_SET)) - { - Fail("ERROR: fseek failed, and this test depends on it."); - } - - /* Attempt to read from the 'a+' only file, should succeed */ - if(fgets(buffer,10,fp) == NULL) - { - Fail("ERROR: Tried to READ from a file with 'a+' mode set. " - "This should pass, but fgets returned failure. Either fgets " - "or fopen is broken."); - } - - - /* Attempt to write to a file after using 'a+' and fseek */ - fp = _wfopen(filename2, appendplus); - if(fp == NULL) - { - Fail("ERROR: _wfopen failed to be created with 'a+' mode.\n"); - } - - /* write text to the file initially */ - if(fprintf(fp,"%s","abcd") <= 0) - { - Fail("ERROR: Attempted to WRITE to a file opened with 'a+' mode " - "but fprintf failed. Either fopen or fprintf have problems.\n"); - } - - /* set the pointer to the front of the file */ - if(fseek(fp, 0, SEEK_SET)) - { - Fail("ERROR: fseek failed, and this test depends on it.\n"); - } - - /* using 'a+' should still write to the end of the file, not the front */ - if(fputs("efgh",fp) < 0) - { - Fail("ERROR: Attempted to WRITE to a file opened with 'a+' mode " - "but fputs failed.\n"); - } - - /* set the pointer to the front of the file */ - if(fseek(fp, 0, SEEK_SET)) - { - Fail("ERROR: fseek failed, and this test depends on it.\n"); - } - - /* Attempt to read from the 'a+' only file, should succeed */ - if(fgets(buffer,10,fp) == NULL) - { - Fail("ERROR: Tried to READ from a file with 'a+' mode set. " - "This should pass, but fgets returned failure. Either fgets " - "or fopen is broken.\n"); - } - - /* Compare what was read and what should have been in the file */ - if(memcmp(buffer,"abcdefgh",8)) - { - Fail("ERROR: The string read should have equaled 'abcdefgh' " - "but instead it is %s\n", buffer); - } - - PAL_Terminate(); - return PASS; -} - - diff --git a/src/coreclr/pal/tests/palsuite/compilableTests.txt b/src/coreclr/pal/tests/palsuite/compilableTests.txt index c9de088fd78892..8e61d00fa7a0a3 100644 --- a/src/coreclr/pal/tests/palsuite/compilableTests.txt +++ b/src/coreclr/pal/tests/palsuite/compilableTests.txt @@ -91,13 +91,6 @@ c_runtime/_vsnprintf_s/test9/paltest_vsnprintf_test9 c_runtime/_wcsicmp/test1/paltest_wcsicmp_test1 c_runtime/_wcslwr_s/test1/paltest_wcslwr_s_test1 c_runtime/_wcsnicmp/test1/paltest_wcsnicmp_test1 -c_runtime/_wfopen/test1/paltest_wfopen_test1 -c_runtime/_wfopen/test2/paltest_wfopen_test2 -c_runtime/_wfopen/test3/paltest_wfopen_test3 -c_runtime/_wfopen/test4/paltest_wfopen_test4 -c_runtime/_wfopen/test5/paltest_wfopen_test5 -c_runtime/_wfopen/test6/paltest_wfopen_test6 -c_runtime/_wfopen/test7/paltest_wfopen_test7 c_runtime/_wtoi/test1/paltest_wtoi_test1 debug_api/OutputDebugStringA/test1/paltest_outputdebugstringa_test1 debug_api/OutputDebugStringW/test1/paltest_outputdebugstringw_test1 diff --git a/src/coreclr/pal/tests/palsuite/paltestlist.txt b/src/coreclr/pal/tests/palsuite/paltestlist.txt index 035f8b33a14875..f4f0293ba02efd 100644 --- a/src/coreclr/pal/tests/palsuite/paltestlist.txt +++ b/src/coreclr/pal/tests/palsuite/paltestlist.txt @@ -90,13 +90,6 @@ c_runtime/_vsnprintf_s/test9/paltest_vsnprintf_test9 c_runtime/_wcsicmp/test1/paltest_wcsicmp_test1 c_runtime/_wcslwr_s/test1/paltest_wcslwr_s_test1 c_runtime/_wcsnicmp/test1/paltest_wcsnicmp_test1 -c_runtime/_wfopen/test1/paltest_wfopen_test1 -c_runtime/_wfopen/test2/paltest_wfopen_test2 -c_runtime/_wfopen/test3/paltest_wfopen_test3 -c_runtime/_wfopen/test4/paltest_wfopen_test4 -c_runtime/_wfopen/test5/paltest_wfopen_test5 -c_runtime/_wfopen/test6/paltest_wfopen_test6 -c_runtime/_wfopen/test7/paltest_wfopen_test7 c_runtime/_wtoi/test1/paltest_wtoi_test1 debug_api/OutputDebugStringW/test1/paltest_outputdebugstringw_test1 exception_handling/RaiseException/test1/paltest_raiseexception_test1 diff --git a/src/coreclr/utilcode/CMakeLists.txt b/src/coreclr/utilcode/CMakeLists.txt index fd7b7d04399709..fa88c2146ca6a3 100644 --- a/src/coreclr/utilcode/CMakeLists.txt +++ b/src/coreclr/utilcode/CMakeLists.txt @@ -14,7 +14,6 @@ set(UTILCODE_COMMON_SOURCES configuration.cpp collections.cpp posterror.cpp - fstream.cpp clrhelpers.cpp stgpool.cpp stgpooli.cpp diff --git a/src/coreclr/utilcode/fstream.cpp b/src/coreclr/utilcode/fstream.cpp deleted file mode 100644 index 82821186923192..00000000000000 --- a/src/coreclr/utilcode/fstream.cpp +++ /dev/null @@ -1,253 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - - -#include "stdafx.h" // Precompiled header key. -#include "fstream.h" - -CFileStream::CFileStream() -: _cRef(1) -, _hFile(INVALID_HANDLE_VALUE) -{ -} - -CFileStream::~CFileStream() -{ - Close(); -} - -HRESULT CFileStream::OpenForRead(LPCWSTR wzFilePath) -{ - HRESULT hr = S_OK; - DWORD dwShareMode = FILE_SHARE_READ; - - dwShareMode |= FILE_SHARE_DELETE; - - _ASSERTE(_hFile == INVALID_HANDLE_VALUE && wzFilePath); - if (_hFile != INVALID_HANDLE_VALUE || !wzFilePath) { - hr = E_INVALIDARG; - goto Exit; - } - - _hFile = WszCreateFile(wzFilePath, GENERIC_READ, - dwShareMode, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (_hFile == INVALID_HANDLE_VALUE) { - hr = HRESULT_FROM_WIN32(GetLastError()); - goto Exit; - } - -Exit: - return hr; -} - -HRESULT CFileStream::OpenForWrite(LPCWSTR wzFilePath) -{ - HRESULT hr = S_OK; - - _ASSERTE(_hFile == INVALID_HANDLE_VALUE && wzFilePath); - if (_hFile != INVALID_HANDLE_VALUE || !wzFilePath) { - hr = E_INVALIDARG; - goto Exit; - } - - _hFile = WszCreateFile(wzFilePath, GENERIC_WRITE, - FILE_SHARE_READ, NULL, - CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - - if (_hFile == INVALID_HANDLE_VALUE) - { - hr = HRESULT_FROM_WIN32(GetLastError()); - goto Exit; - } - -Exit: - return hr; -} - -HRESULT CFileStream::QueryInterface(REFIID riid, void **ppv) -{ - HRESULT hr = S_OK; - - if (!ppv) - return E_POINTER; - - *ppv = NULL; - - if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IStream)) { - *ppv = static_cast(this); - } - else { - hr = E_NOINTERFACE; - } - - if (*ppv) { - AddRef(); - } - - return hr; -} - -STDMETHODIMP_(ULONG) CFileStream::AddRef() -{ - return InterlockedIncrement(&_cRef); -} - -STDMETHODIMP_(ULONG) CFileStream::Release() -{ - ULONG ulRef = InterlockedDecrement(&_cRef); - - if (!ulRef) { - delete this; - } - - return ulRef; -} - -HRESULT CFileStream::Read(void *pv, ULONG cb, ULONG *pcbRead) -{ - HRESULT hr = S_OK; - ULONG cbRead = 0; - - if (pcbRead != NULL) { - *pcbRead = 0; - } - - _ASSERTE(_hFile != INVALID_HANDLE_VALUE); - if (_hFile == INVALID_HANDLE_VALUE) { - hr = E_UNEXPECTED; - goto Exit; - } - - if (!::ReadFile(_hFile, pv, cb, &cbRead, NULL)) { - hr = HRESULT_FROM_WIN32(::GetLastError()); - goto Exit; - } - - if (cbRead == 0) { - hr = S_FALSE; - } - else { - hr = NOERROR; - } - - if (pcbRead != NULL) { - *pcbRead = cbRead; - } - -Exit: - return hr; -} - -HRESULT CFileStream::Write(void const *pv, ULONG cb, ULONG *pcbWritten) -{ - HRESULT hr = S_OK; - ULONG cbWritten = 0; - - if (pcbWritten != NULL) { - *pcbWritten = 0; - } - - _ASSERTE(_hFile != INVALID_HANDLE_VALUE); - if (_hFile == INVALID_HANDLE_VALUE) { - hr = E_UNEXPECTED; - goto Exit; - } - - if (!::WriteFile(_hFile, pv, cb, &cbWritten, NULL)) { - hr = HRESULT_FROM_WIN32(::GetLastError()); - goto Exit; - } - - if (cbWritten == 0) { - hr = S_FALSE; - } - else { - hr = S_OK; - } - - if (pcbWritten != NULL) { - *pcbWritten = cbWritten; - } - -Exit: - return hr; -} - -HRESULT CFileStream::Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition) -{ - return E_NOTIMPL; -} - -HRESULT CFileStream::SetSize(ULARGE_INTEGER libNewSize) -{ - return E_NOTIMPL; -} - -HRESULT CFileStream::CopyTo(IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten) -{ - return E_NOTIMPL; -} - -HRESULT CFileStream::Commit(DWORD grfCommitFlags) -{ - HRESULT hr = S_OK; - - if (grfCommitFlags != 0) { - hr = E_INVALIDARG; - goto Exit; - } - - if (!Close()) { - hr = HRESULT_FROM_WIN32(GetLastError()); - } - -Exit: - return hr; -} - -HRESULT CFileStream::Revert() -{ - return E_NOTIMPL; -} - -HRESULT CFileStream::LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) -{ - return E_NOTIMPL; -} - -HRESULT CFileStream::UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) -{ - return E_NOTIMPL; -} - -HRESULT CFileStream::Stat(STATSTG *pstatstg, DWORD grfStatFlag) -{ - return E_NOTIMPL; -} - -HRESULT CFileStream::Clone(IStream **ppIStream) -{ - return E_NOTIMPL; -} - - -BOOL CFileStream::Close() -{ - BOOL fSuccess = FALSE; - - if (_hFile != INVALID_HANDLE_VALUE) { - if (!::CloseHandle(_hFile)) { - _hFile = INVALID_HANDLE_VALUE; - goto Exit; - } - - _hFile = INVALID_HANDLE_VALUE; - } - - fSuccess = TRUE; - -Exit: - return fSuccess; -} - diff --git a/src/coreclr/utilcode/log.cpp b/src/coreclr/utilcode/log.cpp index 296757812e6b23..2f3073831e6cf4 100644 --- a/src/coreclr/utilcode/log.cpp +++ b/src/coreclr/utilcode/log.cpp @@ -17,6 +17,7 @@ #include "log.h" #include "utilcode.h" +#include #ifdef LOGGING @@ -32,7 +33,7 @@ static DWORD LogFlags = 0; static CQuickWSTR szLogFileName; -static HANDLE LogFileHandle = INVALID_HANDLE_VALUE; +static FILE* LogFileHandle = NULL; static minipal_mutex* volatile LogFileMutex = nullptr; static DWORD LogFacilityMask = LF_ALL; static DWORD LogFacilityMask2 = 0; @@ -85,20 +86,13 @@ VOID InitLogging() if ((LogFlags & LOG_ENABLE) && (LogFlags & LOG_ENABLE_FILE_LOGGING) && (szLogFileName.Size() > 0) && - (LogFileHandle == INVALID_HANDLE_VALUE)) + (LogFileHandle == NULL)) { - DWORD fdwCreate = (LogFlags & LOG_ENABLE_APPEND_FILE) ? OPEN_ALWAYS : CREATE_ALWAYS; - LogFileHandle = WszCreateFile( - szLogFileName.Ptr(), - GENERIC_WRITE, - FILE_SHARE_READ, - NULL, - fdwCreate, - FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN | ((LogFlags & LOG_ENABLE_FLUSH_FILE) ? FILE_FLAG_WRITE_THROUGH : 0), - NULL); + const WCHAR* mode = (LogFlags & LOG_ENABLE_APPEND_FILE) ? W("ab+") : W("wb+"); + int err = fopen_lp(&LogFileHandle, szLogFileName.Ptr(), mode); // Some other logging may be going on, try again with another file name - if (LogFileHandle == INVALID_HANDLE_VALUE && u16_strlen(szLogFileName.Ptr()) + 3 <= szLogFileName.Size()) + if ((err != 0) && u16_strlen(szLogFileName.Ptr()) + 3 <= szLogFileName.Size()) { WCHAR* ptr = szLogFileName.Ptr() + u16_strlen(szLogFileName.Ptr()) + 1; ptr[-1] = W('.'); @@ -107,41 +101,19 @@ VOID InitLogging() for(int i = 0; i < 10; i++) { - LogFileHandle = WszCreateFile( - szLogFileName.Ptr(), - GENERIC_WRITE, - FILE_SHARE_READ, - NULL, - fdwCreate, - FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN | ((LogFlags & LOG_ENABLE_FLUSH_FILE) ? FILE_FLAG_WRITE_THROUGH : 0), - NULL); - if (LogFileHandle != INVALID_HANDLE_VALUE) + err = fopen_lp(&LogFileHandle, szLogFileName.Ptr(), mode); + + if (err == 0) break; *ptr = *ptr + 1; } - if (LogFileHandle == INVALID_HANDLE_VALUE) { - int ret = WideCharToMultiByte(CP_ACP, 0, szLogFileName.Ptr(), -1, NULL, 0, NULL, NULL); - const char *msg = "Could not open log file, logging to "; - DWORD msgLen = (DWORD)strlen(msg); - CQuickSTR buff; - if (SUCCEEDED(buff.ReSizeNoThrow(ret + msgLen))) - { - strcpy_s(buff.Ptr(), buff.Size(), msg); - WideCharToMultiByte(CP_ACP, 0, szLogFileName.Ptr(), -1, buff.Ptr() + msgLen, ret, NULL, NULL); - msg = buff.Ptr(); - } - else - { - msg = "Could not open log file"; - } - DWORD written; - WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msg, (DWORD)strlen(msg), &written, 0); + if (err != 0) { + MAKE_UTF8PTR_FROMWIDE_NOTHROW(u8LogFileName, szLogFileName.Ptr()); + printf("Could not open log file, logging to %s\n", u8LogFileName); } } - if (LogFileHandle != INVALID_HANDLE_VALUE) + if (LogFileHandle != NULL) { - if (LogFlags & LOG_ENABLE_APPEND_FILE) - SetFilePointer(LogFileHandle, 0, NULL, FILE_END); LogSpew( LF_ALWAYS, FATALERROR, "************************ New Output *****************\n" ); } } @@ -203,12 +175,12 @@ VOID InitializeLogging() VOID FlushLogging() { STATIC_CONTRACT_NOTHROW; - if (LogFileHandle != INVALID_HANDLE_VALUE) + if (LogFileHandle != NULL) { // We must take the lock, as an OS deadlock can occur between - // FlushFileBuffers and WriteFile. + // fflush and fwrite. EnterLogLock(); - FlushFileBuffers( LogFileHandle ); + fflush( LogFileHandle ); LeaveLogLock(); } } @@ -217,11 +189,11 @@ VOID ShutdownLogging() { STATIC_CONTRACT_NOTHROW; - if (LogFileHandle != INVALID_HANDLE_VALUE) { + if (LogFileHandle != NULL) { LogSpew( LF_ALWAYS, FATALERROR, "Logging shutting down\n"); - CloseHandle( LogFileHandle ); + fclose( LogFileHandle ); } - LogFileHandle = INVALID_HANDLE_VALUE; + LogFileHandle = NULL; bLoggingInitialized = false; } @@ -314,7 +286,6 @@ VOID LogSpewAlwaysValist(const char *fmt, va_list args) char * pBuffer = &rgchBuffer[0]; DWORD buflen = 0; - DWORD written; static bool needsPrefix = true; @@ -358,11 +329,11 @@ VOID LogSpewAlwaysValist(const char *fmt, va_list args) pBuffer = pBuffer2; #endif // TARGET_UNIX - if (LogFlags & LOG_ENABLE_FILE_LOGGING && LogFileHandle != INVALID_HANDLE_VALUE) + if (LogFlags & LOG_ENABLE_FILE_LOGGING && LogFileHandle != NULL) { - WriteFile(LogFileHandle, pBuffer, buflen, &written, NULL); + fwrite(pBuffer, 1, buflen, LogFileHandle); if (LogFlags & LOG_ENABLE_FLUSH_FILE) { - FlushFileBuffers( LogFileHandle ); + fflush(LogFileHandle); } } diff --git a/src/coreclr/utilcode/longfilepathwrappers.cpp b/src/coreclr/utilcode/longfilepathwrappers.cpp index 2ea72de5383877..a0586a0b3ec97b 100644 --- a/src/coreclr/utilcode/longfilepathwrappers.cpp +++ b/src/coreclr/utilcode/longfilepathwrappers.cpp @@ -6,6 +6,7 @@ #include "longfilepathwrappers.h" #include "sstring.h" #include "ex.h" +#include #ifdef HOST_WINDOWS class LongFile @@ -342,6 +343,32 @@ CreateFileWrapper( return ret; } +int u16_fopen_wrapper(FILE** stream, const WCHAR* filename, const WCHAR* mode) +{ + CONTRACTL + { + NOTHROW; + } + CONTRACTL_END; + + EX_TRY + { + LongPathString path(LongPathString::Literal, filename); + + if (SUCCEEDED(LongFile::NormalizePath(path))) + { + return u16_fopen_s(stream, path.GetUnicode(), mode); + } + } + EX_CATCH + { + return -1; + } + EX_END_CATCH + + return -1; +} + BOOL CopyFileExWrapper( _In_ LPCWSTR lpExistingFileName, diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index 9b30b3123c47e6..fb0c5498a0325b 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -13,10 +13,18 @@ #include #include #include -#include "fstream.h" #include "typestring.h" #include "clrversion.h" #include "hostinformation.h" + +#ifdef HOST_WINDOWS +#include +#else // !HOST_WINDOWS +#include +#include +#include +#endif // HOST_WINDOWS + #include #include #include @@ -1117,17 +1125,25 @@ ep_rt_file_open_write (const ep_char8_t *path) { STATIC_CONTRACT_NOTHROW; - ep_char16_t *path_utf16 = ep_rt_utf8_to_utf16le_string (path); - ep_return_null_if_nok (path_utf16 != NULL); + if (!path) + return INVALID_HANDLE_VALUE; - CFileStream *file_stream = new (nothrow) CFileStream (); - if (file_stream && FAILED (file_stream->OpenForWrite (reinterpret_cast(path_utf16)))) { - delete file_stream; - file_stream = NULL; - } +#ifdef HOST_WINDOWS + ep_char16_t *path_utf16 = ep_rt_utf8_to_utf16le_string (path); + if (!path_utf16) + return INVALID_HANDLE_VALUE; + + HANDLE res = ::CreateFileW (reinterpret_cast(path_utf16), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + ep_rt_utf16_string_free (path_utf16); + return static_cast(res); +#else // !HOST_WINDOWS + mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; + int fd = creat (path, perms); + if (fd == -1) + return INVALID_HANDLE_VALUE; - ep_rt_utf16_string_free (path_utf16); - return static_cast(file_stream); + return (ep_rt_file_handle_t)(ptrdiff_t)fd; +#endif // HOST_WINDOWS } static @@ -1137,10 +1153,13 @@ ep_rt_file_close (ep_rt_file_handle_t file_handle) { STATIC_CONTRACT_NOTHROW; - // Closed in destructor. - if (file_handle) - delete file_handle; - return true; +#ifdef HOST_WINDOWS + return ::CloseHandle (file_handle) != FALSE; +#else // !HOST_WINDOWS + int fd = (int)(ptrdiff_t)file_handle; + close (fd); + return true; +#endif // HOST_WINDOWS } static @@ -1157,10 +1176,28 @@ ep_rt_file_write ( ep_return_false_if_nok (file_handle != NULL); - ULONG out_count; - HRESULT result = reinterpret_cast(file_handle)->Write (buffer, bytes_to_write, &out_count); - *bytes_written = static_cast(out_count); - return result == S_OK; +#ifdef HOST_WINDOWS + return ::WriteFile (file_handle, buffer, bytes_to_write, reinterpret_cast(bytes_written), NULL) != FALSE; +#else // !HOST_WINDOWS + int fd = (int)(ptrdiff_t)file_handle; + int ret; + do { + ret = write (fd, buffer, bytes_to_write); + } while (ret == -1 && errno == EINTR); + + if (ret == -1) { + if (bytes_written != NULL) { + *bytes_written = 0; + } + + return false; + } + + if (bytes_written != NULL) + *bytes_written = ret; + + return true; +#endif // HOST_WINDOWS } static diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-types-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-types-coreclr.h index 9661e9a4ae2c36..da11d90f8c8d7c 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-types-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-types-coreclr.h @@ -56,7 +56,7 @@ typedef class MethodDesc ep_rt_method_desc_t; */ #undef ep_rt_file_handle_t -typedef class CFileStream * ep_rt_file_handle_t; +typedef void* ep_rt_file_handle_t; #undef ep_rt_wait_event_handle_t typedef struct _rt_coreclr_event_internal_t ep_rt_wait_event_handle_t; diff --git a/src/coreclr/vm/finalizerthread.cpp b/src/coreclr/vm/finalizerthread.cpp index fe468252c1cfbe..159653b209cd04 100644 --- a/src/coreclr/vm/finalizerthread.cpp +++ b/src/coreclr/vm/finalizerthread.cpp @@ -9,6 +9,7 @@ #include "jithost.h" #include "genanalysis.h" #include "eventpipeadapter.h" +#include "dn-stdio.h" #ifdef FEATURE_COMINTEROP #include "runtimecallablewrapper.h" @@ -448,7 +449,11 @@ VOID FinalizerThread::FinalizerThreadWorker(void *args) // Writing an empty file to indicate completion WCHAR outputPath[MAX_PATH]; ReplacePid(GENAWARE_COMPLETION_FILE_NAME, outputPath, MAX_PATH); - fclose(_wfopen(outputPath, W("w+"))); + FILE* fp = NULL; + if (fopen_lp(&fp, outputPath, W("w+")) == 0) + { + fclose(fp); + } } if (!bPriorityBoosted) diff --git a/src/coreclr/vm/multicorejit.cpp b/src/coreclr/vm/multicorejit.cpp index 17307691593037..a9d1c7b3879ff9 100644 --- a/src/coreclr/vm/multicorejit.cpp +++ b/src/coreclr/vm/multicorejit.cpp @@ -18,7 +18,6 @@ #include "stubgen.h" #include "eventtrace.h" #include "array.h" -#include "fstream.h" #include "hash.h" #include "minipal/time.h" @@ -28,6 +27,7 @@ #include "eventtracebase.h" #include "multicorejit.h" #include "multicorejitimpl.h" +#include void MulticoreJitFireEtw(const WCHAR * pAction, const WCHAR * pTarget, int p1, int p2, int p3) { @@ -154,11 +154,12 @@ HRESULT MulticoreJitRecorder::WriteOutput() EX_TRY { - CFileStream fileStream; + FILE* fp; - if (SUCCEEDED(hr = fileStream.OpenForWrite(m_fullFileName.GetUnicode()))) + if (fopen_lp(&fp, m_fullFileName.GetUnicode(), W("wb")) == 0) { - hr = WriteOutput(& fileStream); + hr = WriteOutput(fp); + fclose(fp); } } EX_CATCH @@ -169,7 +170,7 @@ HRESULT MulticoreJitRecorder::WriteOutput() } -HRESULT WriteData(IStream * pStream, const void * pData, unsigned len) +HRESULT WriteData(FILE * fp, const void * pData, unsigned len) { CONTRACTL { @@ -179,11 +180,11 @@ HRESULT WriteData(IStream * pStream, const void * pData, unsigned len) } CONTRACTL_END - ULONG cbWritten; + size_t cbWritten = fwrite(pData, 1, len, fp); - HRESULT hr = pStream->Write(pData, len, & cbWritten); + HRESULT hr = S_OK; - if (SUCCEEDED(hr) && (cbWritten != len)) + if (cbWritten != len) { hr = E_FAIL; } @@ -192,7 +193,7 @@ HRESULT WriteData(IStream * pStream, const void * pData, unsigned len) } // Write string, round to DWORD alignment -HRESULT WriteString(const void * pString, unsigned len, IStream * pStream) +HRESULT WriteString(const void * pString, unsigned len, FILE * fp) { CONTRACTL { @@ -202,25 +203,23 @@ HRESULT WriteString(const void * pString, unsigned len, IStream * pStream) } CONTRACTL_END; - ULONG cbWritten = 0; - - HRESULT hr; - - hr = pStream->Write(pString, len, & cbWritten); + size_t cbWritten = fwrite(pString, 1, len, fp); - if (SUCCEEDED(hr)) + if (cbWritten == (size_t)len) { len = RoundUp(len) - len; if (len != 0) { - cbWritten = 0; + uint32_t temp = 0; + cbWritten = fwrite(&temp, 1, len, fp); - hr = pStream->Write(& cbWritten, len, & cbWritten); + if (cbWritten == (size_t)len) + return S_OK; } } - return hr; + return E_FAIL; } @@ -329,7 +328,7 @@ bool RecorderModuleInfo::SetModule(Module * pMod) // ///////////////////////////////////////////////////// -HRESULT MulticoreJitRecorder::WriteModuleRecord(IStream * pStream, const RecorderModuleInfo & module) +HRESULT MulticoreJitRecorder::WriteModuleRecord(FILE * fp, const RecorderModuleInfo & module) { CONTRACTL { @@ -355,15 +354,15 @@ HRESULT MulticoreJitRecorder::WriteModuleRecord(IStream * pStream, const Recorde mod.wLoadLevel = (unsigned short) module.loadLevel; mod.flags = module.flags; - hr = WriteData(pStream, & mod, sizeof(mod)); + hr = WriteData(fp, & mod, sizeof(mod)); if (SUCCEEDED(hr)) { - hr = WriteString(pModuleName, lenModuleName, pStream); + hr = WriteString(pModuleName, lenModuleName, fp); if (SUCCEEDED(hr)) { - hr = WriteString(pAssemblyName, lenAssemblyName, pStream); + hr = WriteString(pAssemblyName, lenAssemblyName, fp); } } @@ -371,7 +370,7 @@ HRESULT MulticoreJitRecorder::WriteModuleRecord(IStream * pStream, const Recorde } -HRESULT MulticoreJitRecorder::WriteOutput(IStream * pStream) +HRESULT MulticoreJitRecorder::WriteOutput(FILE * fp) { CONTRACTL { @@ -486,14 +485,14 @@ HRESULT MulticoreJitRecorder::WriteOutput(IStream * pStream) _ASSERTE((sizeof(header) % sizeof(unsigned)) == 0); - hr = WriteData(pStream, & header, sizeof(header)); + hr = WriteData(fp, & header, sizeof(header)); } DWORD dwData = 0; for (unsigned i = 0; SUCCEEDED(hr) && (i < m_ModuleCount); i ++) { - hr = WriteModuleRecord(pStream, m_ModuleList[i]); + hr = WriteModuleRecord(fp, m_ModuleList[i]); } for (LONG i = 0 ; i < m_JitInfoCount && SUCCEEDED(hr); i++) @@ -504,7 +503,7 @@ HRESULT MulticoreJitRecorder::WriteOutput(IStream * pStream) _ASSERTE(m_JitInfoArray[i].IsFullyInitialized()); DWORD data1 = m_JitInfoArray[i].GetRawModuleData(); - hr = WriteData(pStream, &data1, sizeof(data1)); + hr = WriteData(fp, &data1, sizeof(data1)); } else if (m_JitInfoArray[i].IsGenericMethodInfo()) { @@ -522,19 +521,19 @@ HRESULT MulticoreJitRecorder::WriteOutput(IStream * pStream) DWORD sigSize = m_JitInfoArray[i].GetMethodSignatureSize(); DWORD paddingSize = m_JitInfoArray[i].GetMethodRecordPaddingSize(); - hr = WriteData(pStream, &data1, sizeof(data1)); + hr = WriteData(fp, &data1, sizeof(data1)); if (SUCCEEDED(hr)) { - hr = WriteData(pStream, &data2, sizeof(data2)); + hr = WriteData(fp, &data2, sizeof(data2)); } if (SUCCEEDED(hr)) { - hr = WriteData(pStream, pSignature, sigSize); + hr = WriteData(fp, pSignature, sigSize); } if (SUCCEEDED(hr) && paddingSize > 0) { DWORD tmp = 0; - hr = WriteData(pStream, &tmp, paddingSize); + hr = WriteData(fp, &tmp, paddingSize); } } else @@ -545,10 +544,10 @@ HRESULT MulticoreJitRecorder::WriteOutput(IStream * pStream) DWORD data1 = m_JitInfoArray[i].GetRawMethodData1(); unsigned data2 = m_JitInfoArray[i].GetRawMethodData2NonGeneric(); - hr = WriteData(pStream, &data1, sizeof(data1)); + hr = WriteData(fp, &data1, sizeof(data1)); if (SUCCEEDED(hr)) { - hr = WriteData(pStream, &data2, sizeof(data2)); + hr = WriteData(fp, &data2, sizeof(data2)); } } } diff --git a/src/coreclr/vm/multicorejitimpl.h b/src/coreclr/vm/multicorejitimpl.h index 9aeaa7d9bde5ed..81663650e907e6 100644 --- a/src/coreclr/vm/multicorejitimpl.h +++ b/src/coreclr/vm/multicorejitimpl.h @@ -626,13 +626,13 @@ class MulticoreJitRecorder unsigned FindModule(Module * pModule); unsigned GetOrAddModuleIndex(Module * pModule); - HRESULT WriteModuleRecord(IStream * pStream, const RecorderModuleInfo & module); + HRESULT WriteModuleRecord(FILE * fp, const RecorderModuleInfo & module); void RecordMethodInfo(unsigned moduleIndex, MethodDesc * pMethod, bool application); unsigned RecordModuleInfo(Module * pModule); void RecordOrUpdateModuleInfo(FileLoadLevel needLevel, unsigned moduleIndex); - HRESULT WriteOutput(IStream * pStream); + HRESULT WriteOutput(FILE * fp); HRESULT WriteOutput(); diff --git a/src/coreclr/vm/multicorejitplayer.cpp b/src/coreclr/vm/multicorejitplayer.cpp index 78d7a53bda3aa5..33d4bab0d81a24 100644 --- a/src/coreclr/vm/multicorejitplayer.cpp +++ b/src/coreclr/vm/multicorejitplayer.cpp @@ -18,10 +18,10 @@ #include "stubgen.h" #include "eventtrace.h" #include "array.h" -#include "fstream.h" #include "hash.h" #include "clrex.h" #include "minipal/time.h" +#include #include "appdomain.hpp" @@ -1029,22 +1029,17 @@ HRESULT MulticoreJitProfilePlayer::ReadCheckFile(const WCHAR * pFileName) HRESULT hr = S_OK; { - HANDLE hFile = WszCreateFile(pFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if (hFile == INVALID_HANDLE_VALUE) + FILE* fp; + if (fopen_lp(&fp, pFileName, W("rb")) != 0) { return COR_E_FILENOTFOUND; } HeaderRecord header; - DWORD cbRead = 0; + size_t cbRead = fread(&header, sizeof(header), 1, fp); - if (! ::ReadFile(hFile, & header, sizeof(header), &cbRead, NULL)) - { - hr = COR_E_BADIMAGEFORMAT; - } - else if (cbRead != sizeof(header)) + if (cbRead != sizeof(header)) { hr = COR_E_BADIMAGEFORMAT; } @@ -1072,7 +1067,8 @@ HRESULT MulticoreJitProfilePlayer::ReadCheckFile(const WCHAR * pFileName) if (SUCCEEDED(hr)) { - m_nFileSize = SafeGetFileSize(hFile, 0); + uint64_t fSize = fgetsize(fp); + m_nFileSize = fSize > UINT32_MAX ? UINT32_MAX : (unsigned int)fSize; if (m_nFileSize > sizeof(header)) { @@ -1084,7 +1080,7 @@ HRESULT MulticoreJitProfilePlayer::ReadCheckFile(const WCHAR * pFileName) { hr = E_OUTOFMEMORY; } - else if (::ReadFile(hFile, m_pFileBuffer, m_nFileSize, & cbRead, NULL)) + else if ((cbRead = fread(m_pFileBuffer, 1, m_nFileSize, fp)) > 0) { if (cbRead != m_nFileSize) { @@ -1102,7 +1098,7 @@ HRESULT MulticoreJitProfilePlayer::ReadCheckFile(const WCHAR * pFileName) } } - CloseHandle(hFile); + fclose(fp); _FireEtwMulticoreJit(W("PLAYER"), W("Header"), hr, m_headerModuleCount, header.methodCount); } diff --git a/src/coreclr/vm/perfmap.cpp b/src/coreclr/vm/perfmap.cpp index df5635dabcccca..74be61ed435527 100644 --- a/src/coreclr/vm/perfmap.cpp +++ b/src/coreclr/vm/perfmap.cpp @@ -10,6 +10,7 @@ #include #include "perfmap.h" #include "pal.h" +#include // The code addresses are actually native image offsets during crossgen. Print @@ -221,8 +222,8 @@ PerfMap::~PerfMap() { LIMITED_METHOD_CONTRACT; - delete m_FileStream; - m_FileStream = nullptr; + fclose(m_fp); + m_fp = nullptr; } void PerfMap::OpenFileForPid(int pid, const char* basePath) @@ -240,16 +241,8 @@ void PerfMap::OpenFile(SString& path) STANDARD_VM_CONTRACT; // Open the file stream. - m_FileStream = new (nothrow) CFileStream(); - if(m_FileStream != nullptr) - { - HRESULT hr = m_FileStream->OpenForWrite(path.GetUnicode()); - if(FAILED(hr)) - { - delete m_FileStream; - m_FileStream = nullptr; - } - } + if (fopen_lp(&m_fp, path.GetUnicode(), W("w")) != 0) + m_fp = nullptr; } // Write a line to the map file. @@ -260,7 +253,7 @@ void PerfMap::WriteLine(SString& line) _ASSERTE(s_csPerfMap.OwnedByCurrentThread()); #endif - if (m_FileStream == nullptr || m_ErrorEncountered) + if (m_fp == nullptr || m_ErrorEncountered) { return; } @@ -268,13 +261,8 @@ void PerfMap::WriteLine(SString& line) EX_TRY { // Write the line. - // The PAL already takes a lock when writing, so we don't need to do so here. - const char * strLine = line.GetUTF8(); - ULONG inCount = line.GetCount(); - ULONG outCount; - m_FileStream->Write(strLine, inCount, &outCount); - if (inCount != outCount) + if (fprintf(m_fp, "%s", line.GetUTF8()) != 0) { // This will cause us to stop writing to the file. // The file will still remain open until shutdown so that we don't have to take a lock at this level when we touch the file stream. diff --git a/src/coreclr/vm/perfmap.h b/src/coreclr/vm/perfmap.h index 6079d1fbc2e15d..5268c4b0d56aef 100644 --- a/src/coreclr/vm/perfmap.h +++ b/src/coreclr/vm/perfmap.h @@ -7,8 +7,8 @@ #define PERFPID_H #include "sstring.h" -#include "fstream.h" #include "volatile.h" +#include // Generates a perfmap file. @@ -40,7 +40,7 @@ class PerfMap static CrstStatic s_csPerfMap; // The file stream to write the map to. - CFileStream * m_FileStream; + FILE * m_fp; // Set to true if an error is encountered when writing to the file. bool m_ErrorEncountered; diff --git a/src/coreclr/vm/pgo.cpp b/src/coreclr/vm/pgo.cpp index 40a8882baaffe8..d91ed7dfea521f 100644 --- a/src/coreclr/vm/pgo.cpp +++ b/src/coreclr/vm/pgo.cpp @@ -7,6 +7,7 @@ #include "versionresilienthashcode.h" #include "typestring.h" #include "pgo_formatprocessing.h" +#include "dn-stdio.h" #ifdef FEATURE_PGO @@ -205,9 +206,9 @@ void PgoManager::WritePgoData() return; } - FILE* const pgoDataFile = _wfopen(fileName, W("wb")); + FILE* pgoDataFile = NULL; - if (pgoDataFile == NULL) + if (fopen_lp(&pgoDataFile, fileName, W("wb")) != 0) { return; } @@ -366,10 +367,10 @@ void PgoManager::ReadPgoData() { return; } + + FILE* pgoDataFile = NULL; - FILE* const pgoDataFile = _wfopen(fileName, W("rb")); - - if (pgoDataFile == NULL) + if (fopen_lp(&pgoDataFile, fileName, W("wb")) != 0) { return; } diff --git a/src/coreclr/vm/readytoruninfo.cpp b/src/coreclr/vm/readytoruninfo.cpp index 39ad5fafc6f70e..34103df26c9b60 100644 --- a/src/coreclr/vm/readytoruninfo.cpp +++ b/src/coreclr/vm/readytoruninfo.cpp @@ -16,6 +16,7 @@ #include "method.hpp" #include "wellknownattributes.h" #include "nativeimage.h" +#include "dn-stdio.h" #ifdef FEATURE_PERFMAP #include "perfmap.h" @@ -429,7 +430,8 @@ static void LogR2r(const char *msg, PEAssembly *pPEAssembly) DWORD pid = GetCurrentProcessId(); FormatInteger(pidSuffix + 1, ARRAY_SIZE(pidSuffix) - 1, "%u", pid); fullname.Append(pidSuffix); - r2rLogFile = _wfopen(fullname.GetUnicode(), W("w")); + if (fopen_lp(&r2rLogFile, fullname.GetUnicode(), W("w")) != 0) + r2rLogFile = NULL; } else r2rLogFile = NULL; diff --git a/src/coreclr/vm/virtualcallstub.cpp b/src/coreclr/vm/virtualcallstub.cpp index db08933e87793b..5ddf8e6438403e 100644 --- a/src/coreclr/vm/virtualcallstub.cpp +++ b/src/coreclr/vm/virtualcallstub.cpp @@ -8,6 +8,7 @@ #include "CachedInterfaceDispatchPal.h" #include "CachedInterfaceDispatch.h" #include "comdelegate.h" +#include #ifdef FEATURE_PERFMAP #include "perfmap.h" @@ -148,7 +149,7 @@ UINT32 g_dumpLogIncr; #endif // STUB_LOGGING //@TODO: use the existing logging mechanisms. for now we write to a file. -HANDLE g_hStubLogFile; +FILE* g_hStubLogFile; void VirtualCallStubManager::StartupLogging() { @@ -167,22 +168,13 @@ void VirtualCallStubManager::StartupLogging() FAULT_NOT_FATAL(); // We handle filecreation problems locally SString str; str.Printf("StubLog_%d.log", GetCurrentProcessId()); - g_hStubLogFile = WszCreateFile (str.GetUnicode(), - GENERIC_WRITE, - 0, - 0, - CREATE_ALWAYS, - FILE_ATTRIBUTE_NORMAL, - 0); + if (fopen_lp(&g_hStubLogFile, str.GetUnicode(), W("wb")) != 0) + g_hStubLogFile = NULL; } EX_CATCH { } EX_END_CATCH - - if (g_hStubLogFile == INVALID_HANDLE_VALUE) { - g_hStubLogFile = NULL; - } } #define OUTPUT_FORMAT_INT "\t%-30s %d\r\n" @@ -212,226 +204,155 @@ void VirtualCallStubManager::LoggingDump() g_resolveCache->LogStats(); #endif // FEATURE_VIRTUAL_STUB_DISPATCH - // Temp space to use for formatting the output. - static const int FMT_STR_SIZE = 160; - char szPrintStr[FMT_STR_SIZE]; - DWORD dwWriteByte; - if(g_hStubLogFile) { #ifdef STUB_LOGGING - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nstub tuning parameters\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "\r\nstub tuning parameters\r\n"); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d (0x%02x)\r\n", "STUB_MISS_COUNT_VALUE", + fprintf(g_hStubLogFile, "\t%-30s %3d (0x%02x)\r\n", "STUB_MISS_COUNT_VALUE", STUB_MISS_COUNT_VALUE, STUB_MISS_COUNT_VALUE); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "STUB_COLLIDE_WRITE_PCT", + fprintf(g_hStubLogFile, "\t%-30s %3d%% (0x%02x)\r\n", "STUB_COLLIDE_WRITE_PCT", STUB_COLLIDE_WRITE_PCT, STUB_COLLIDE_WRITE_PCT); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "STUB_COLLIDE_MONO_PCT", + fprintf(g_hStubLogFile, "\t%-30s %3d%% (0x%02x)\r\n", "STUB_COLLIDE_MONO_PCT", STUB_COLLIDE_MONO_PCT, STUB_COLLIDE_MONO_PCT); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "DumpLogCounter", + fprintf(g_hStubLogFile, "\t%-30s %3d%% (0x%02x)\r\n", "DumpLogCounter", g_dumpLogCounter, g_dumpLogCounter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "DumpLogIncr", + fprintf(g_hStubLogFile, "\t%-30s %3d%% (0x%02x)\r\n", "DumpLogIncr", g_dumpLogCounter, g_dumpLogIncr); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "ResetCacheCounter", + fprintf(g_hStubLogFile, "\t%-30s %3d%% (0x%02x)\r\n", "ResetCacheCounter", g_resetCacheCounter, g_resetCacheCounter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "ResetCacheIncr", + fprintf(g_hStubLogFile, "\t%-30s %3d%% (0x%02x)\r\n", "ResetCacheIncr", g_resetCacheCounter, g_resetCacheIncr); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); #endif // STUB_LOGGING - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nsite data\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "\r\nsite data\r\n"); //output counters - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_counter", g_site_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_write", g_site_write); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_write_mono", g_site_write_mono); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_write_poly", g_site_write_poly); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %d\r\n", "reclaim_counter", g_reclaim_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nstub data\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_lookup_counter", g_stub_lookup_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_mono_counter", g_stub_mono_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_poly_counter", g_stub_poly_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_vtable_counter", g_stub_vtable_counter); - WriteFile(g_hStubLogFile, szPrintStr, (DWORD)strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_space", g_stub_space); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "site_counter", g_site_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "site_write", g_site_write); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "site_write_mono", g_site_write_mono); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "site_write_poly", g_site_write_poly); + + fprintf(g_hStubLogFile, "\r\n%-30s %d\r\n", "reclaim_counter", g_reclaim_counter); + + fprintf(g_hStubLogFile, "\r\nstub data\r\n"); + + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "stub_lookup_counter", g_stub_lookup_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "stub_mono_counter", g_stub_mono_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "stub_poly_counter", g_stub_poly_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "stub_vtable_counter", g_stub_vtable_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "stub_space", g_stub_space); #ifdef STUB_LOGGING - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nlookup stub data\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "\r\nlookup stub data\r\n"); UINT32 total_calls = g_mono_call_counter + g_poly_call_counter; - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "lookup_call_counter", g_call_lookup_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "lookup_call_counter", g_call_lookup_counter); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %d\r\n", "total stub dispatch calls", total_calls); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "\r\n%-30s %d\r\n", "total stub dispatch calls", total_calls); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %#5.2f%%\r\n", "mono stub data", + fprintf(g_hStubLogFile, "\r\n%-30s %#5.2f%%\r\n", "mono stub data", 100.0 * double(g_mono_call_counter)/double(total_calls)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "mono_call_counter", g_mono_call_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "mono_miss_counter", g_mono_miss_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_PCT, "miss percent", + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "mono_call_counter", g_mono_call_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "mono_miss_counter", g_mono_miss_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_PCT, "miss percent", 100.0 * double(g_mono_miss_counter)/double(g_mono_call_counter)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %#5.2f%%\r\n", "poly stub data", + fprintf(g_hStubLogFile, "\r\n%-30s %#5.2f%%\r\n", "poly stub data", 100.0 * double(g_poly_call_counter)/double(total_calls)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "poly_call_counter", g_poly_call_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "poly_miss_counter", g_poly_miss_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_PCT, "miss percent", + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "poly_call_counter", g_poly_call_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "poly_miss_counter", g_poly_miss_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_PCT, "miss percent", 100.0 * double(g_poly_miss_counter)/double(g_poly_call_counter)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); #endif // STUB_LOGGING #ifdef CHAIN_LOOKUP - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nchain lookup data\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "\r\nchain lookup data\r\n"); #ifdef STUB_LOGGING - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "chained_lookup_call_counter", g_chained_lookup_call_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "chained_lookup_miss_counter", g_chained_lookup_miss_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_PCT, "miss percent", + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "chained_lookup_call_counter", g_chained_lookup_call_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "chained_lookup_miss_counter", g_chained_lookup_miss_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_PCT, "miss percent", 100.0 * double(g_chained_lookup_miss_counter)/double(g_chained_lookup_call_counter)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "chained_lookup_external_call_counter", g_chained_lookup_external_call_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "chained_lookup_external_miss_counter", g_chained_lookup_external_miss_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_PCT, "miss percent", + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "chained_lookup_external_call_counter", g_chained_lookup_external_call_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "chained_lookup_external_miss_counter", g_chained_lookup_external_miss_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_PCT, "miss percent", 100.0 * double(g_chained_lookup_external_miss_counter)/double(g_chained_lookup_external_call_counter)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); #endif // STUB_LOGGING - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "chained_entry_promoted", g_chained_entry_promoted); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "chained_entry_promoted", g_chained_entry_promoted); #endif // CHAIN_LOOKUP #ifdef STUB_LOGGING - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %#5.2f%%\r\n", "worker (slow resolver) data", + fprintf(g_hStubLogFile, "\r\n%-30s %#5.2f%%\r\n", "worker (slow resolver) data", 100.0 * double(g_worker_call)/double(total_calls)); #else // !STUB_LOGGING - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nworker (slow resolver) data\r\n"); + fprintf(g_hStubLogFile, "\r\nworker (slow resolver) data\r\n"); #endif // !STUB_LOGGING - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "worker_call", g_worker_call); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "worker_call_no_patch", g_worker_call_no_patch); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "external_call", g_external_call); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "external_call_no_patch", g_external_call_no_patch); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "worker_collide_to_mono", g_worker_collide_to_mono); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "worker_call", g_worker_call); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "worker_call_no_patch", g_worker_call_no_patch); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "external_call", g_external_call); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "external_call_no_patch", g_external_call_no_patch); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "worker_collide_to_mono", g_worker_collide_to_mono); UINT32 total_inserts = g_insert_cache_external + g_insert_cache_shared + g_insert_cache_dispatch + g_insert_cache_resolve; - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %d\r\n", "insert cache data", total_inserts); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "\r\n%-30s %d\r\n", "insert cache data", total_inserts); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_external", g_insert_cache_external, + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT_PCT, "insert_cache_external", g_insert_cache_external, 100.0 * double(g_insert_cache_external)/double(total_inserts)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_shared", g_insert_cache_shared, + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT_PCT, "insert_cache_shared", g_insert_cache_shared, 100.0 * double(g_insert_cache_shared)/double(total_inserts)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_dispatch", g_insert_cache_dispatch, + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT_PCT, "insert_cache_dispatch", g_insert_cache_dispatch, 100.0 * double(g_insert_cache_dispatch)/double(total_inserts)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_resolve", g_insert_cache_resolve, + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT_PCT, "insert_cache_resolve", g_insert_cache_resolve, 100.0 * double(g_insert_cache_resolve)/double(total_inserts)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_hit", g_insert_cache_hit, + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT_PCT, "insert_cache_hit", g_insert_cache_hit, 100.0 * double(g_insert_cache_hit)/double(total_inserts)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_miss", g_insert_cache_miss, + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT_PCT, "insert_cache_miss", g_insert_cache_miss, 100.0 * double(g_insert_cache_miss)/double(total_inserts)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_collide", g_insert_cache_collide, + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT_PCT, "insert_cache_collide", g_insert_cache_collide, 100.0 * double(g_insert_cache_collide)/double(total_inserts)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_write", g_insert_cache_write, + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT_PCT, "insert_cache_write", g_insert_cache_write, 100.0 * double(g_insert_cache_write)/double(total_inserts)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\ncache data\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "\r\ncache data\r\n"); #ifdef FEATURE_VIRTUAL_STUB_DISPATCH size_t total, used; g_resolveCache->GetLoadFactor(&total, &used); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_SIZE, "cache_entry_used", used); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "cache_entry_counter", g_cache_entry_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "cache_entry_space", g_cache_entry_space); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nstub hash table data\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "bucket_space", g_bucket_space); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "bucket_space_dead", g_bucket_space_dead); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\ncache_load:\t%zu used, %zu total, utilization %#5.2f%%\r\n", + fprintf(g_hStubLogFile, OUTPUT_FORMAT_SIZE, "cache_entry_used", used); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "cache_entry_counter", g_cache_entry_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "cache_entry_space", g_cache_entry_space); + + fprintf(g_hStubLogFile, "\r\nstub hash table data\r\n"); + + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "bucket_space", g_bucket_space); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "bucket_space_dead", g_bucket_space_dead); + + fprintf(g_hStubLogFile, "\r\ncache_load:\t%zu used, %zu total, utilization %#5.2f%%\r\n", used, total, 100.0 * double(used) / double(total)); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); #ifdef STUB_LOGGING - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\ncache entry write counts\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "\r\ncache entry write counts\r\n"); DispatchCache::CacheEntryData *rgCacheData = g_resolveCache->cacheData; for (UINT16 i = 0; i < CALL_STUB_CACHE_SIZE; i++) { - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), " %4d", rgCacheData[i]); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, " %4d", rgCacheData[i]); if (i % 16 == 15) { - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "\r\n"); } } - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "\r\n"); #endif // STUB_LOGGING #endif // FEATURE_VIRTUAL_STUB_DISPATCH @@ -440,28 +361,21 @@ void VirtualCallStubManager::LoggingDump() { if (ContractImplMap::deltasDescs[i] != 0) { - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "deltasDescs[%d]\t%d\r\n", i, ContractImplMap::deltasDescs[i]); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "deltasDescs[%d]\t%d\r\n", i, ContractImplMap::deltasDescs[i]); } } for (unsigned i = 0; i < ContractImplMap::max_delta_count; i++) { if (ContractImplMap::deltasSlots[i] != 0) { - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "deltasSlots[%d]\t%d\r\n", i, ContractImplMap::deltasSlots[i]); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "deltasSlots[%d]\t%d\r\n", i, ContractImplMap::deltasSlots[i]); } } - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "cout of maps:\t%d\r\n", ContractImplMap::countMaps); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "count of interfaces:\t%d\r\n", ContractImplMap::countInterfaces); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "count of deltas:\t%d\r\n", ContractImplMap::countDelta); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "total delta for descs:\t%d\r\n", ContractImplMap::totalDeltaDescs); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "total delta for slots:\t%d\r\n", ContractImplMap::totalDeltaSlots); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, "cout of maps:\t%d\r\n", ContractImplMap::countMaps); + fprintf(g_hStubLogFile, "count of interfaces:\t%d\r\n", ContractImplMap::countInterfaces); + fprintf(g_hStubLogFile, "count of deltas:\t%d\r\n", ContractImplMap::countDelta); + fprintf(g_hStubLogFile, "total delta for descs:\t%d\r\n", ContractImplMap::totalDeltaDescs); + fprintf(g_hStubLogFile, "total delta for slots:\t%d\r\n", ContractImplMap::totalDeltaSlots); #endif // 0 } @@ -473,7 +387,7 @@ void VirtualCallStubManager::FinishLogging() if(g_hStubLogFile) { - CloseHandle(g_hStubLogFile); + fclose(g_hStubLogFile); } g_hStubLogFile = NULL; } @@ -3175,50 +3089,32 @@ void VirtualCallStubManager::LogStats() return; } - // Temp space to use for formatting the output. - static const int FMT_STR_SIZE = 160; - char szPrintStr[FMT_STR_SIZE]; - DWORD dwWriteByte; - if (g_hStubLogFile && (stats.site_write != 0)) { //output counters - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_counter", stats.site_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_write", stats.site_write); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_write_mono", stats.site_write_mono); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_write_poly", stats.site_write_poly); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nstub data\r\n"); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_lookup_counter", stats.stub_lookup_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_mono_counter", stats.stub_mono_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_poly_counter", stats.stub_poly_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_space", stats.stub_space); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "site_counter", stats.site_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "site_write", stats.site_write); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "site_write_mono", stats.site_write_mono); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "site_write_poly", stats.site_write_poly); + + fprintf(g_hStubLogFile, "\r\nstub data\r\n"); + + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "stub_lookup_counter", stats.stub_lookup_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "stub_mono_counter", stats.stub_mono_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "stub_poly_counter", stats.stub_poly_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "stub_space", stats.stub_space); #ifdef FEATURE_VIRTUAL_STUB_DISPATCH size_t total, used; g_resolveCache->GetLoadFactor(&total, &used); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_SIZE, "cache_entry_used", used); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "cache_entry_counter", stats.cache_entry_counter); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "cache_entry_space", stats.cache_entry_space); - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_SIZE, "cache_entry_used", used); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "cache_entry_counter", stats.cache_entry_counter); + fprintf(g_hStubLogFile, OUTPUT_FORMAT_INT, "cache_entry_space", stats.cache_entry_space); - sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\ncache_load:\t%zu used, %zu total, utilization %#5.2f%%\r\n", + fprintf(g_hStubLogFile, "\r\ncache_load:\t%zu used, %zu total, utilization %#5.2f%%\r\n", used, total, 100.0 * double(used) / double(total)); #endif // FEATURE_VIRTUAL_STUB_DISPATCH - WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL); } #ifdef FEATURE_VIRTUAL_STUB_DISPATCH