Bug 1885334 - Change the signature of FdPrintf to use a platform-dependently-sized argument type r=glandium

That way we avoid some spurious casts

Differential Revision: https://phabricator.services.mozilla.com/D204648
This commit is contained in:
serge-sans-paille 2024-04-17 09:36:13 +00:00
parent 78ac6a6ccf
commit 469e6b7b31
8 changed files with 47 additions and 29 deletions

View file

@ -89,7 +89,7 @@ static void WriteDigits(CheckedIncrement<char*>& b, size_t i,
} while (x > 0);
}
void FdPrintf(intptr_t aFd, const char* aFormat, ...) {
void FdPrintf(platform_handle_t aFd, const char* aFormat, ...) {
if (aFd == 0) {
return;
}
@ -192,7 +192,7 @@ out:
#ifdef _WIN32
// See comment in FdPrintf.h as to why WriteFile is used.
DWORD written;
WriteFile(reinterpret_cast<HANDLE>(aFd), buf, b - buf, &written, nullptr);
WriteFile(aFd, buf, b - buf, &written, nullptr);
#else
MOZ_UNUSED(write(aFd, buf, b - buf));
#endif

View file

@ -7,6 +7,12 @@
#ifndef __FdPrintf_h__
#define __FdPrintf_h__
#ifdef _WIN32
typedef void* platform_handle_t;
#else
typedef int platform_handle_t;
#endif
/* We can't use libc's (f)printf because it would reenter in replace_malloc,
* So use a custom and simplified version. Only %p, %zu, %s and %% are
* supported, %zu, %s, support width specifiers.
@ -18,7 +24,7 @@
* APIs is that they don't support O_APPEND in a multi-process-safe way,
* while CreateFile does.
*/
void FdPrintf(intptr_t aFd, const char* aFormat, ...)
void FdPrintf(platform_handle_t aFd, const char* aFormat, ...)
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)))
#endif

View file

@ -48,6 +48,12 @@ struct ReplaceMallocBridge;
#include "mozilla/Types.h"
#ifdef _WIN32
typedef void* platform_handle_t;
#else
typedef int platform_handle_t;
#endif
MOZ_BEGIN_EXTERN_C
#ifndef REPLACE_MALLOC_IMPL
@ -124,9 +130,9 @@ class AddrInfo;
// Callbacks to register debug file handles for Poison IO interpose.
// See Mozilla(|Un)RegisterDebugHandle in xpcom/build/PoisonIOInterposer.h
struct DebugFdRegistry {
virtual void RegisterHandle(intptr_t aFd);
virtual void RegisterHandle(platform_handle_t aFd);
virtual void UnRegisterHandle(intptr_t aFd);
virtual void UnRegisterHandle(platform_handle_t aFd);
};
} // namespace mozilla

View file

@ -21,7 +21,7 @@
#include "Mutex.h"
static malloc_table_t sFuncs;
static intptr_t sFd = 0;
static platform_handle_t sFd = 0;
static bool sStdoutOrStderr = false;
static Mutex sMutex MOZ_UNANNOTATED;
@ -169,7 +169,7 @@ void replace_init(malloc_table_t* aTable, ReplaceMallocBridge** aBridge) {
nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
}
if (handle != INVALID_HANDLE_VALUE) {
sFd = reinterpret_cast<intptr_t>(handle);
sFd = handle;
}
#else
if (fd == -1) {

View file

@ -458,7 +458,7 @@ class Distribution {
mTotalRequests++;
}
void printDist(intptr_t std_err) {
void printDist(platform_handle_t std_err) {
MOZ_ASSERT(mMaxSize);
// The translation to turn a slot index into a memory request size.
@ -533,7 +533,7 @@ class SMapsReader : private FdReader {
return Some(SMapsReader(FdReader(fd, true)));
}
Maybe<MemoryMap> readMap(intptr_t aStdErr) {
Maybe<MemoryMap> readMap(platform_handle_t aStdErr) {
// This is not very tolerant of format changes because things like
// parseNumber will crash if they get a bad value. TODO: make this
// soft-fail.
@ -615,7 +615,7 @@ class Replay {
Replay() {
#ifdef _WIN32
// See comment in FdPrintf.h as to why native win32 handles are used.
mStdErr = reinterpret_cast<intptr_t>(GetStdHandle(STD_ERROR_HANDLE));
mStdErr = GetStdHandle(STD_ERROR_HANDLE);
#else
mStdErr = fileno(stderr);
#endif
@ -1044,7 +1044,7 @@ class Replay {
}
}
intptr_t mStdErr;
platform_handle_t mStdErr;
size_t mOps = 0;
// The number of slots that have been used. It is used to iterate over slots

View file

@ -10,6 +10,12 @@
#include "mozilla/Types.h"
#include <stdio.h>
#ifdef _WIN32
typedef void* platform_handle_t;
#else
typedef int platform_handle_t;
#endif
MOZ_BEGIN_EXTERN_C
/** Register file handle to be ignored by poisoning IO interposer. This function
@ -18,7 +24,7 @@ MOZ_BEGIN_EXTERN_C
* when one of them links the static CRT). In such cases, giving file
* descriptors or FILEs
* doesn't work because _get_osfhandle fails with "invalid parameter". */
void MozillaRegisterDebugHandle(intptr_t aHandle);
void MozillaRegisterDebugHandle(platform_handle_t aHandle);
/** Register file descriptor to be ignored by poisoning IO interposer */
void MozillaRegisterDebugFD(int aFd);
@ -27,7 +33,7 @@ void MozillaRegisterDebugFD(int aFd);
void MozillaRegisterDebugFILE(FILE* aFile);
/** Unregister file handle from being ignored by poisoning IO interposer */
void MozillaUnRegisterDebugHandle(intptr_t aHandle);
void MozillaUnRegisterDebugHandle(platform_handle_t aHandle);
/** Unregister file descriptor from being ignored by poisoning IO interposer */
void MozillaUnRegisterDebugFD(int aFd);
@ -45,7 +51,7 @@ namespace mozilla {
/**
* Check if a file is registered as a debug file.
*/
bool IsDebugFile(intptr_t aFileID);
bool IsDebugFile(platform_handle_t aFileID);
/**
* Initialize IO poisoning, this is only safe to do on the main-thread when no
@ -79,7 +85,7 @@ void ClearPoisonIOInterposer();
# ifdef __cplusplus
namespace mozilla {
inline bool IsDebugFile(intptr_t aFileID) { return true; }
inline bool IsDebugFile(platform_handle_t aFileID) { return true; }
inline void InitPoisonIOInterposer() {}
inline void ClearPoisonIOInterposer() {}
# ifdef XP_MACOSX

View file

@ -21,17 +21,18 @@
// Auxiliary method to convert file descriptors to ids
#if defined(XP_WIN)
# include <io.h>
inline mozilla::Maybe<intptr_t> FileDescriptorToHandle(int aFd) {
inline mozilla::Maybe<platform_handle_t> FileDescriptorToHandle(int aFd) {
intptr_t handle = _get_osfhandle(aFd);
if ((handle == -1) || (handle == -2)) {
// -1: Invalid handle. -2: stdin/out/err not associated with a stream.
return mozilla::Nothing();
}
return mozilla::Some(handle);
return mozilla::Some<platform_handle_t>(
reinterpret_cast<platform_handle_t>(handle));
}
#else
inline mozilla::Maybe<intptr_t> FileDescriptorToHandle(int aFd) {
return mozilla::Some<intptr_t>(aFd);
inline mozilla::Maybe<platform_handle_t> FileDescriptorToHandle(int aFd) {
return mozilla::Some<platform_handle_t>(static_cast<platform_handle_t>(aFd));
}
#endif /* if not XP_WIN */
@ -161,7 +162,7 @@ class ChunkedList {
}
};
typedef ChunkedList<intptr_t> FdList;
typedef ChunkedList<platform_handle_t> FdList;
// Return a list used to hold the IDs of the current debug files. On unix
// an ID is a file descriptor. On Windows it is a file HANDLE.
@ -176,7 +177,7 @@ namespace mozilla {
// Auxiliary Method to test if a file descriptor is registered to be ignored
// by the poisoning IO interposer
bool IsDebugFile(intptr_t aFileID) {
bool IsDebugFile(platform_handle_t aFileID) {
return getDebugFileIDs().Contains(aFileID);
}
@ -184,7 +185,7 @@ bool IsDebugFile(intptr_t aFileID) {
extern "C" {
void MozillaRegisterDebugHandle(intptr_t aHandle) {
void MozillaRegisterDebugHandle(platform_handle_t aHandle) {
DebugFilesAutoLock lockedScope;
FdList& DebugFileIDs = getDebugFileIDs();
MOZ_ASSERT(!DebugFileIDs.Contains(aHandle));
@ -192,7 +193,7 @@ void MozillaRegisterDebugHandle(intptr_t aHandle) {
}
void MozillaRegisterDebugFD(int aFd) {
mozilla::Maybe<intptr_t> handle = FileDescriptorToHandle(aFd);
mozilla::Maybe<platform_handle_t> handle = FileDescriptorToHandle(aFd);
if (!handle.isSome()) {
return;
}
@ -207,7 +208,7 @@ void MozillaRegisterDebugFILE(FILE* aFile) {
MozillaRegisterDebugFD(fd);
}
void MozillaUnRegisterDebugHandle(intptr_t aHandle) {
void MozillaUnRegisterDebugHandle(platform_handle_t aHandle) {
DebugFilesAutoLock lockedScope;
FdList& DebugFileIDs = getDebugFileIDs();
MOZ_ASSERT(DebugFileIDs.Contains(aHandle));
@ -215,7 +216,7 @@ void MozillaUnRegisterDebugHandle(intptr_t aHandle) {
}
void MozillaUnRegisterDebugFD(int aFd) {
mozilla::Maybe<intptr_t> handle = FileDescriptorToHandle(aFd);
mozilla::Maybe<platform_handle_t> handle = FileDescriptorToHandle(aFd);
if (!handle.isSome()) {
return;
}
@ -234,11 +235,11 @@ void MozillaUnRegisterDebugFILE(FILE* aFile) {
} // extern "C"
#ifdef MOZ_REPLACE_MALLOC
void mozilla::DebugFdRegistry::RegisterHandle(intptr_t aHandle) {
void mozilla::DebugFdRegistry::RegisterHandle(platform_handle_t aHandle) {
MozillaRegisterDebugHandle(aHandle);
}
void mozilla::DebugFdRegistry::UnRegisterHandle(intptr_t aHandle) {
void mozilla::DebugFdRegistry::UnRegisterHandle(platform_handle_t aHandle) {
MozillaUnRegisterDebugHandle(aHandle);
}
#endif

View file

@ -127,8 +127,7 @@ class WinIOAutoObservation : public mozilla::IOInterposeObserver::Observation {
WinIOAutoObservation(mozilla::IOInterposeObserver::Operation aOp,
HANDLE aFileHandle, const LARGE_INTEGER* aOffset)
: mozilla::IOInterposeObserver::Observation(
aOp, sReference,
!mozilla::IsDebugFile(reinterpret_cast<intptr_t>(aFileHandle))),
aOp, sReference, !mozilla::IsDebugFile(aFileHandle)),
mFileHandle(aFileHandle),
mFileHandleType(GetFileType(aFileHandle)),
mHasQueriedFilename(false) {