Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove C FILE PAL wrappers #98238

Merged
merged 8 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions src/coreclr/debug/createdump/crashinfounix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ CrashInfo::EnumerateMemoryRegions()
printf_error("snprintf failed building /proc/<pid>/maps\n");
return false;
}
FILE* mapsFile = fopen(mapPath, "r");
FILE* mapsFile = fopen(mapPath, "rb");
if (mapsFile == nullptr)
{
printf_error("Problem reading maps file: fopen(%s) FAILED %s (%d)\n", mapPath, strerror(errno), errno);
Expand Down Expand Up @@ -554,7 +554,7 @@ GetStatus(pid_t pid, pid_t* ppid, pid_t* tgid, std::string* name)
return false;
}

FILE *statusFile = fopen(statusPath, "r");
FILE *statusFile = fopen(statusPath, "rb");
if (statusFile == nullptr)
{
printf_error("GetStatus fopen(%s) FAILED %s (%d)\n", statusPath, strerror(errno), errno);
Expand Down
20 changes: 0 additions & 20 deletions src/coreclr/debug/createdump/createdumppal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,26 +230,6 @@ size_t u16_strlen(const WCHAR* str)
//

#ifdef _DEBUG

PAL_FILE *
__cdecl
PAL_get_stderr(int caller)
{
return (PAL_FILE*)stderr;
}

int
__cdecl
PAL_fprintf(PAL_FILE* stream, const char* format, ...)
{
va_list args;
va_start(args, format);
int result = vfprintf((FILE*)stream, format, args);
fflush((FILE*)stream);
va_end(args);
return result;
}

DWORD
PALAPI
GetCurrentProcessId()
Expand Down
10 changes: 2 additions & 8 deletions src/coreclr/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ nativeStringResourceTable_mscorrc
#PAL_bsearch
#PAL_CopyModuleData
#PAL_errno
#PAL_fflush
#PAL__flushall
#PAL_free
#PAL_GetLogicalCpuCountFromOS
#PAL_GetTotalCpuCount
#PAL_GetUnwindInfoSize
#PAL_get_stdout
#PAL_get_stderr
#PAL_stdout
#PAL_stderr
#PAL_GetApplicationGroupId
#PAL_GetTransportName
#PAL_GetCurrentThread
Expand All @@ -52,7 +50,6 @@ nativeStringResourceTable_mscorrc
#PAL_malloc
#PAL_realloc
#PAL_qsort
#PAL_fprintf
#PAL__wcstoui64
#PAL_wcstoul
#PAL_wcstod
Expand All @@ -65,9 +62,6 @@ nativeStringResourceTable_mscorrc
#PAL_wcschr
#PAL_wcscat
#PAL_wcsstr
#PAL__open
#PAL__pread
#PAL__close

#_wcsicmp
#sprintf_s
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/ilasm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
else
{
InvalidOption:
fprintf(stderr, "Error : Invalid Option: %LS\n", argv[i]);
MAKE_UTF8PTR_FROMWIDE_NOTHROW(invalidOpt, argv[i]);
fprintf(stderr, "Error : Invalid Option: %s\n", invalidOpt);
goto ErrorExit;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/ildasm/dasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7352,9 +7352,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"));
#else
FILE* pfile = NULL;
_wfopen_s(&pfile,wzFileName,W("w"));
#endif

if(pfile)
{
Expand Down
9 changes: 7 additions & 2 deletions src/coreclr/ildasm/dis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,14 +1113,19 @@ BOOL Disassemble(IMDInternalImport *pImport, BYTE *ILHeader, void *GUICookie, md
{
if(pFile) fclose(pFile);
pFile = NULL;
if(fopen_s(&pFile,szFileName,"rt") != 0)
#ifdef HOST_WINDOWS
const char* const mode = "rt";
#else
const char* const mode = "r";
#endif
if(fopen_s(&pFile,szFileName, mode) != 0)
{
char* pch = strrchr(szFileName, DIRECTORY_SEPARATOR_CHAR_A);
#ifdef HOST_WINDOWS
if(pch == NULL) pch = strrchr(szFileName,':');
#endif
pFile = NULL;
if(pch) fopen_s(&pFile,pch+1,"rt");
if(pch) fopen_s(&pFile,pch+1, mode);
}
if(bIsNewFile)
{
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9088,7 +9088,7 @@ void JitTimer::PrintCsvMethodStats(Compiler* comp)
{
totCycles += m_info.m_cyclesByPhase[i];
}
fprintf(s_csvFile, "%llu,", m_info.m_cyclesByPhase[i]);
fprintf(s_csvFile, "%llu,", (unsigned long long)m_info.m_cyclesByPhase[i]);

if ((JitConfig.JitMeasureIR() != 0) && PhaseReportsIRSize[i])
{
Expand All @@ -9101,7 +9101,7 @@ void JitTimer::PrintCsvMethodStats(Compiler* comp)
fprintf(s_csvFile, "%u,", comp->info.compNativeCodeSize);
fprintf(s_csvFile, "%zu,", comp->compInfoBlkSize);
fprintf(s_csvFile, "%zu,", comp->compGetArenaAllocator()->getTotalBytesAllocated());
fprintf(s_csvFile, "%llu,", m_info.m_totalCycles);
fprintf(s_csvFile, "%llu,", (unsigned long long)m_info.m_totalCycles);
fprintf(s_csvFile, "%f\n", CachedCyclesPerSecond());

fflush(s_csvFile);
Expand Down
158 changes: 66 additions & 92 deletions src/coreclr/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3982,32 +3982,13 @@ PAL_GetCurrentThreadAffinitySet(SIZE_T size, UINT_PTR* data);
#ifndef PAL_STDCPP_COMPAT
#define exit PAL_exit
#define realloc PAL_realloc
#define fopen PAL_fopen
#define fprintf PAL_fprintf
#define vfprintf PAL_vfprintf
#define rand PAL_rand
#define time PAL_time
#define getenv PAL_getenv
#define fgets PAL_fgets
#define qsort PAL_qsort
#define bsearch PAL_bsearch
#define ferror PAL_ferror
#define fread PAL_fread
#define fwrite PAL_fwrite
#define ftell PAL_ftell
#define fclose PAL_fclose
#define fflush PAL_fflush
#define fputs PAL_fputs
#define fseek PAL_fseek
#define fgetpos PAL_fgetpos
#define fsetpos PAL_fsetpos
#define setvbuf PAL_setvbuf
#define malloc PAL_malloc
#define free PAL_free
#define _open PAL__open
#define _pread PAL__pread
#define _close PAL__close
#define _flushall PAL__flushall

#ifdef HOST_AMD64
#define _mm_getcsr PAL__mm_getcsr
Expand Down Expand Up @@ -4086,6 +4067,68 @@ PALIMPORT int __cdecl iswxdigit(wint_t);
PALIMPORT wint_t __cdecl towupper(wint_t);
PALIMPORT wint_t __cdecl towlower(wint_t);
PALIMPORT int remove(const char*);

#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2

/* Locale categories */
#define LC_ALL 0
#define LC_COLLATE 1
#define LC_CTYPE 2
#define LC_MONETARY 3
#define LC_NUMERIC 4
#define LC_TIME 5

#define _IOFBF 0 /* setvbuf should set fully buffered */
#define _IOLBF 1 /* setvbuf should set line buffered */
#define _IONBF 2 /* setvbuf should set unbuffered */

struct _FILE;

#ifdef DEFINE_DUMMY_FILE_TYPE
#define FILE _PAL_FILE
struct _PAL_FILE;
#else
typedef _FILE FILE;
#endif // DEFINE_DUMMY_FILE_TYPE

PALIMPORT int __cdecl fclose(FILE *);
PALIMPORT int __cdecl fflush(FILE *);
PALIMPORT size_t __cdecl fwrite(const void *, size_t, size_t, FILE *);
PALIMPORT size_t __cdecl fread(void *, size_t, size_t, FILE *);
PALIMPORT char * __cdecl fgets(char *, int, FILE *);
PALIMPORT int __cdecl fputs(const char *, FILE *);
PALIMPORT int __cdecl fprintf(FILE *, const char *, ...);
PALIMPORT int __cdecl vfprintf(FILE *, const char *, va_list);
PALIMPORT int __cdecl fseek(FILE *, LONG, int);
PALIMPORT LONG __cdecl ftell(FILE *);
PALIMPORT int __cdecl ferror(FILE *);
PALIMPORT FILE * __cdecl fopen(const char *, const char *);
PALIMPORT int __cdecl setvbuf(FILE *stream, char *, int, size_t);

// We need a PAL shim for errno and the standard streams as it's not possible to replicate these definition from the standard library
// in all cases. Instead, we shim it and implement the PAL function where we can include the standard headers.
// When we allow people to include the standard headers, then we can remove this.

PALIMPORT DLLEXPORT int * __cdecl PAL_errno();
#define errno (*PAL_errno())

// Only provide a prototype for the PAL forwarders for the standard streams if we are not including the standard headers.
#ifndef DEFINE_DUMMY_FILE_TYPE

extern "C" PALIMPORT DLLEXPORT FILE* __cdecl PAL_stdout();
extern "C" PALIMPORT DLLEXPORT FILE* __cdecl PAL_stdin();
extern "C" PALIMPORT DLLEXPORT FILE* __cdecl PAL_stderr();
#define stdout PAL_stdout()
#define stdin PAL_stdin()
#define stderr PAL_stderr()

#endif

#ifdef DEFINE_DUMMY_FILE_TYPE
#undef FILE
#endif
#endif // PAL_STDCPP_COMPAT

/* _TRUNCATE */
Expand Down Expand Up @@ -4125,6 +4168,10 @@ PALIMPORT errno_t __cdecl _wcslwr_s(WCHAR *, size_t sz);
PALIMPORT DLLEXPORT errno_t __cdecl _i64tow_s(long long, WCHAR *, size_t, int);
PALIMPORT int __cdecl _wtoi(const WCHAR *);

#ifndef DEFINE_DUMMY_FILE_TYPE
PALIMPORT FILE * __cdecl _wfopen(const WCHAR *, const WCHAR *);
#endif

inline int _stricmp(const char* a, const char* b)
{
return strcasecmp(a, b);
Expand Down Expand Up @@ -4321,56 +4368,6 @@ PALIMPORT time_t __cdecl time(time_t *);

#endif // !PAL_STDCPP_COMPAT

PALIMPORT DLLEXPORT int __cdecl _open(const char *szPath, int nFlags, ...);
PALIMPORT DLLEXPORT size_t __cdecl _pread(int fd, void *buf, size_t nbytes, ULONG64 offset);
PALIMPORT DLLEXPORT int __cdecl _close(int);
PALIMPORT DLLEXPORT int __cdecl _flushall();

#ifdef PAL_STDCPP_COMPAT

struct _PAL_FILE;
typedef struct _PAL_FILE PAL_FILE;

#else // PAL_STDCPP_COMPAT

struct _FILE;
typedef struct _FILE FILE;
typedef struct _FILE PAL_FILE;

#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2

/* Locale categories */
#define LC_ALL 0
#define LC_COLLATE 1
#define LC_CTYPE 2
#define LC_MONETARY 3
#define LC_NUMERIC 4
#define LC_TIME 5

#define _IOFBF 0 /* setvbuf should set fully buffered */
#define _IOLBF 1 /* setvbuf should set line buffered */
#define _IONBF 2 /* setvbuf should set unbuffered */

#endif // PAL_STDCPP_COMPAT

PALIMPORT int __cdecl PAL_fclose(PAL_FILE *);
PALIMPORT DLLEXPORT int __cdecl PAL_fflush(PAL_FILE *);
PALIMPORT size_t __cdecl PAL_fwrite(const void *, size_t, size_t, PAL_FILE *);
PALIMPORT size_t __cdecl PAL_fread(void *, size_t, size_t, PAL_FILE *);
PALIMPORT char * __cdecl PAL_fgets(char *, int, PAL_FILE *);
PALIMPORT int __cdecl PAL_fputs(const char *, PAL_FILE *);
PALIMPORT DLLEXPORT int __cdecl PAL_fprintf(PAL_FILE *, const char *, ...);
PALIMPORT int __cdecl PAL_vfprintf(PAL_FILE *, const char *, va_list);
PALIMPORT int __cdecl PAL_fseek(PAL_FILE *, LONG, int);
PALIMPORT LONG __cdecl PAL_ftell(PAL_FILE *);
PALIMPORT int __cdecl PAL_ferror(PAL_FILE *);
PALIMPORT PAL_FILE * __cdecl PAL_fopen(const char *, const char *);
PALIMPORT int __cdecl PAL_setvbuf(PAL_FILE *stream, char *, int, size_t);

PALIMPORT PAL_FILE * __cdecl _wfopen(const WCHAR *, const WCHAR *);

/* Maximum value that can be returned by the rand function. */

#ifndef PAL_STDCPP_COMPAT
Expand All @@ -4380,29 +4377,6 @@ PALIMPORT PAL_FILE * __cdecl _wfopen(const WCHAR *, const WCHAR *);
PALIMPORT int __cdecl rand(void);
PALIMPORT void __cdecl srand(unsigned int);

#ifdef _MSC_VER
#define PAL_get_caller _MSC_VER
#else
#define PAL_get_caller 0
#endif

PALIMPORT DLLEXPORT PAL_FILE * __cdecl PAL_get_stdout(int caller);
PALIMPORT PAL_FILE * __cdecl PAL_get_stdin(int caller);
PALIMPORT DLLEXPORT PAL_FILE * __cdecl PAL_get_stderr(int caller);
PALIMPORT DLLEXPORT int * __cdecl PAL_errno(int caller);

#ifdef PAL_STDCPP_COMPAT
#define PAL_stdout (PAL_get_stdout(PAL_get_caller))
#define PAL_stdin (PAL_get_stdin(PAL_get_caller))
#define PAL_stderr (PAL_get_stderr(PAL_get_caller))
#define PAL_errno (*PAL_errno(PAL_get_caller))
#else // PAL_STDCPP_COMPAT
#define stdout (PAL_get_stdout(PAL_get_caller))
#define stdin (PAL_get_stdin(PAL_get_caller))
#define stderr (PAL_get_stderr(PAL_get_caller))
#define errno (*PAL_errno(PAL_get_caller))
#endif // PAL_STDCPP_COMPAT

PALIMPORT DLLEXPORT char * __cdecl getenv(const char *);
PALIMPORT DLLEXPORT int __cdecl _putenv(const char *);

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/pal/inc/pal_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern "C" {
#if defined(_DEBUG)
#define _ASSERTE(e) do { \
if (!(e)) { \
PAL_fprintf (PAL_get_stderr(PAL_get_caller), \
fprintf (stderr, \
"ASSERT FAILED\n" \
"\tExpression: %s\n" \
"\tLocation: line %d in %s\n" \
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/pal/inc/rt/palrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,9 @@ inline int __cdecl _vscprintf_unsafe(const char *_Format, va_list _ArgList)
}
}

inline errno_t __cdecl _wfopen_unsafe(PAL_FILE * *ff, const WCHAR *fileName, const WCHAR *mode)
inline errno_t __cdecl _wfopen_unsafe(FILE * *ff, const WCHAR *fileName, const WCHAR *mode)
{
PAL_FILE *result = _wfopen(fileName, mode);
FILE *result = _wfopen(fileName, mode);
if(result == 0) {
return 1;
} else {
Expand All @@ -692,9 +692,9 @@ inline errno_t __cdecl _wfopen_unsafe(PAL_FILE * *ff, const WCHAR *fileName, con
}
}

inline errno_t __cdecl _fopen_unsafe(PAL_FILE * *ff, const char *fileName, const char *mode)
inline errno_t __cdecl _fopen_unsafe(FILE * *ff, const char *fileName, const char *mode)
{
PAL_FILE *result = PAL_fopen(fileName, mode);
FILE *result = fopen(fileName, mode);
if(result == 0) {
return 1;
} else {
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,8 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND (CLR_CMAKE_HOST_ARCH_AMD64 OR CLR_CM
endif()

set(SOURCES
cruntime/file.cpp
cruntime/filecrt.cpp
cruntime/malloc.cpp
cruntime/misc.cpp
cruntime/printfcpp.cpp
cruntime/thread.cpp
cruntime/wchar.cpp
debug/debug.cpp
Expand Down
Loading