Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
bc0aeaa
fopen_u16
huoyaoyuan May 30, 2025
f21899c
Return errno
huoyaoyuan May 30, 2025
91a65d8
Add fgetsize
huoyaoyuan May 30, 2025
da5e47d
General ReadFile
huoyaoyuan May 30, 2025
8d87a78
General WriteFile
huoyaoyuan May 30, 2025
fbe663c
Better error code
huoyaoyuan May 30, 2025
b8a70b5
CFileStream
huoyaoyuan May 30, 2025
727b55b
Use stdio in superpmi
huoyaoyuan May 30, 2025
67943e6
superpmi batch 2
huoyaoyuan May 31, 2025
dee1622
Use fstream for string/number extracting
huoyaoyuan May 31, 2025
3f9913d
Superpmi batch 3
huoyaoyuan Jun 1, 2025
605b939
Fix missing fflush
huoyaoyuan Jun 2, 2025
970a294
Use fprintf instead
huoyaoyuan Jun 2, 2025
8192513
Use 64bit seek/tell
huoyaoyuan Jun 2, 2025
de36695
Fix build
huoyaoyuan Jun 2, 2025
c481f02
Wrap long path
huoyaoyuan Jun 2, 2025
d44f02f
Fix gcc build
huoyaoyuan Jun 3, 2025
926a46e
Merge branch 'main' into pal/stdio
huoyaoyuan Jun 5, 2025
b7f56c1
Fix msvc build
huoyaoyuan Jun 5, 2025
e929448
Cleanup CFileStream usage
huoyaoyuan Jun 5, 2025
492473d
Port aot implementation of eventpipe file operations
huoyaoyuan Jun 5, 2025
6218277
Cleanup fstream
huoyaoyuan Jun 5, 2025
7a7950a
Delete outdated comment in pewriter
huoyaoyuan Jun 5, 2025
c1aab56
Remove PerfData in ildasm
huoyaoyuan Jun 5, 2025
3cfb4b3
Revert changes in superpmi
huoyaoyuan Jun 5, 2025
4894d4c
Fix build
huoyaoyuan Jun 5, 2025
791551d
Fix missing fclose
huoyaoyuan Jun 6, 2025
e0071ef
Fix string terminator
huoyaoyuan Jun 6, 2025
02b95d7
Merge branch 'main'
huoyaoyuan Jul 2, 2025
8ad7cf7
Remove unnecessary cast
huoyaoyuan Jul 2, 2025
78f8ffc
Preserve errno before delete[]
huoyaoyuan Jul 2, 2025
4cef9b5
Merge branch 'main'
huoyaoyuan Jul 30, 2025
f8c78b9
Merge branch 'main' into pal/stdio
huoyaoyuan Sep 30, 2025
2f25e28
Update ep-rt definition
huoyaoyuan Sep 30, 2025
9629fe3
Add contract to wrapper
huoyaoyuan Sep 30, 2025
b9db604
Update naming convention
huoyaoyuan Sep 30, 2025
a35bfc5
Additional usage of WszCreateFile
huoyaoyuan Sep 30, 2025
e90ff73
Guard fgetsize usage
huoyaoyuan Sep 30, 2025
be683e3
_wfopen_s
huoyaoyuan Oct 1, 2025
a2b92b0
Merge branch 'main' into pal/stdio
huoyaoyuan Oct 1, 2025
a12016a
Handle allocation throwing
huoyaoyuan Oct 1, 2025
1b5c996
Use E_FAIL in multicorejit
huoyaoyuan Oct 3, 2025
f96648d
Preserve hr from MAKE_UTF8PTR_FROMWIDE
huoyaoyuan Oct 3, 2025
b105300
Use HRESULTFromErrno
huoyaoyuan Oct 5, 2025
bec996e
Merge branch 'main' into pal/stdio
huoyaoyuan Nov 5, 2025
61a7c7e
Use _ftelli64
huoyaoyuan Nov 5, 2025
e78dd1e
Make error code signed
huoyaoyuan Nov 8, 2025
f2663b9
Also update m_dwMResSize when failure
huoyaoyuan Nov 8, 2025
f862119
Always check return code of fopen_lp
huoyaoyuan Nov 13, 2025
1b32f49
Eliminate raw usages of u16_fopen_s
huoyaoyuan Nov 13, 2025
3056b9d
Merge branch 'main' into pal/stdio
huoyaoyuan Nov 13, 2025
e0584f0
Fix building
huoyaoyuan Nov 13, 2025
6553e8c
Don't rely on GetLastError on Windows
huoyaoyuan Nov 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 28 additions & 37 deletions src/coreclr/dlls/mscorpe/pewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "blobfetcher.h"
#include "pedecoder.h"
#include <dn-stdio.h>

#ifdef _DEBUG
#define LOGGING
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -626,32 +627,26 @@ 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;
for(idx = 0; idx <= m_nIndexUsed; idx ++) {
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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -1379,21 +1369,21 @@ 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;
}

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);
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/dlls/mscorpe/pewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};

Expand Down
55 changes: 27 additions & 28 deletions src/coreclr/ilasm/asmman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "assembler.h"
#include "strongnameinternal.h"
#include <limits.h>
#include <dn-stdio.h>

extern WCHAR* pwzInputFiles[];

Expand Down Expand Up @@ -404,52 +405,48 @@ 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;
}

m_sStrongName.m_pbPublicKey = new BYTE[m_sStrongName.m_cbPublicKey];
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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -1003,7 +1002,7 @@ HRESULT AsmMan::EmitManifest()
}
}

CloseHandle(hFile);
fclose(fp);
}
}
if(fOK || ((Assembler*)m_pAssembler)->OnErrGo)
Expand Down
Loading
Loading