| 1 |
#define DXX_VG_DECLARE_EXTERN_C(F) \ |
| 2 |
decltype(F) __real_##F, __wrap_##F |
| 3 |
|
| 4 |
#define DXX_VG_DEFINE_READ(F,V) \ |
| 5 |
__attribute__((__used__,__noinline__,__noclone__)) \ |
| 6 |
int __wrap_##F(PHYSFS_File *const file, V *const val) { \ |
| 7 |
dxx_vg_wrap_poison_value(*val); \ |
| 8 |
return __real_##F(file, val); \ |
| 9 |
} |
| 10 |
|
| 11 |
#define DXX_VG_DEFINE_WRITE(F,V) \ |
| 12 |
__attribute__((__used__,__noinline__,__noclone__)) \ |
| 13 |
int __wrap_##F(PHYSFS_File *const file, V val) { \ |
| 14 |
dxx_vg_wrap_check_value(__builtin_return_address(0), &val, sizeof(val)); \ |
| 15 |
return __real_##F(file, val); \ |
| 16 |
} |
| 17 |
|
| 18 |
#define DXX_VG_DECLARE_READ_HELPER \ |
| 19 |
static void dxx_vg_wrap_poison_value(auto &); |
| 20 |
|
| 21 |
#define DXX_VG_DECLARE_WRITE_HELPER \ |
| 22 |
static void dxx_vg_wrap_check_value(const void *, const void *, unsigned long); |
| 23 |
|
| 24 |
#include "vg-wrap-physfs.h" |
| 25 |
|
| 26 |
/* |
| 27 |
* This pattern is required due to a limitation in the PCH scanning |
| 28 |
* logic. That logic will copy preprocessor guard directives along with |
| 29 |
* the include lines, but does not copy other lines. Thus, writing: |
| 30 |
* |
| 31 |
* ``` |
| 32 |
* #ifndef PREPROCESSOR_SYMBOL |
| 33 |
* #define PREPROCESSOR_SYMBOL 1 |
| 34 |
* #endif |
| 35 |
* |
| 36 |
* #if PREPROCESSOR_SYMBOL |
| 37 |
* #include <header> |
| 38 |
* #endif |
| 39 |
* ``` |
| 40 |
* |
| 41 |
* will cause it to copy only the second block into the generated PCH |
| 42 |
* CPP file. Since the generated CPP will not set a value for |
| 43 |
* PREPROCESSOR_SYMBOL, the #if generates an error. |
| 44 |
* |
| 45 |
* This limitation is often hidden by a special-case that drops the |
| 46 |
* guards if an unguarded include exists. In this case, this file |
| 47 |
* includes a header (`<cstring>`) not included unguarded in any source |
| 48 |
* file, so the special case does not apply. |
| 49 |
* |
| 50 |
* Compensate for this limitation by using only defined() checks, not |
| 51 |
* value checks, for the header includes. This causes the guarded |
| 52 |
* headers not to be included in the PCH. Omission from the PCH |
| 53 |
* should never affect correctness. It can affect performance, but only |
| 54 |
* if the omission causes multiple source files to pay the performance |
| 55 |
* penalty of parsing the file individually instead of using a parsed |
| 56 |
* copy from the PCH. In this instance, no other source files include |
| 57 |
* the headers without a guard, so no other files would benefit from |
| 58 |
* including it in the PCH. |
| 59 |
*/ |
| 60 |
#if defined(DXX_ENABLE_wrap_PHYSFS_read) && !DXX_ENABLE_wrap_PHYSFS_read |
| 61 |
#undef DXX_ENABLE_wrap_PHYSFS_read |
| 62 |
#endif |
| 63 |
|
| 64 |
#if defined(DXX_ENABLE_wrap_PHYSFS_write) && !DXX_ENABLE_wrap_PHYSFS_write |
| 65 |
#undef DXX_ENABLE_wrap_PHYSFS_write |
| 66 |
#endif |
| 67 |
|
| 68 |
#if defined(DXX_ENABLE_wrap_PHYSFS_read) || defined(DXX_ENABLE_wrap_PHYSFS_write) |
| 69 |
#include "console.h" |
| 70 |
#include "dxxerror.h" |
| 71 |
#include "compiler-poison.h" |
| 72 |
|
| 73 |
#ifdef DXX_ENABLE_wrap_PHYSFS_read |
| 74 |
#include <cstring> |
| 75 |
|
| 76 |
static void dxx_vg_wrap_poison_value(auto &val) |
| 77 |
{ |
| 78 |
DXX_POISON_VAR(val, 0xbd); |
| 79 |
} |
| 80 |
|
| 81 |
__attribute__((__used__,__noinline__,__noclone__)) |
| 82 |
PHYSFS_sint64 __wrap_PHYSFS_readBytes(PHYSFS_File *const handle, void *const buffer, const PHYSFS_uint64 len) |
| 83 |
{ |
| 84 |
{ |
| 85 |
auto p = reinterpret_cast<uint8_t *>(buffer); |
| 86 |
#if DXX_HAVE_POISON_OVERWRITE |
| 87 |
std::memset(p, 0xbd, len); |
| 88 |
#endif |
| 89 |
DXX_MAKE_MEM_UNDEFINED(std::span(p, len)); |
| 90 |
} |
| 91 |
return __real_PHYSFS_readBytes(handle, buffer, len); |
| 92 |
} |
| 93 |
#endif |
| 94 |
|
| 95 |
#ifdef DXX_ENABLE_wrap_PHYSFS_write |
| 96 |
static void dxx_vg_wrap_check_value(const void *const ret, const void *const val, const unsigned long vlen) |
| 97 |
{ |
| 98 |
if (const auto o = VALGRIND_CHECK_MEM_IS_DEFINED(val, vlen)) |
| 99 |
con_printf(CON_URGENT, DXX_STRINGIZE_FL(__FILE__, __LINE__, "BUG: ret=%p VALGRIND_CHECK_MEM_IS_DEFINED(%p, %lu)=%p (offset=%p)"), ret, val, vlen, reinterpret_cast<void *>(o), reinterpret_cast<void *>(o - reinterpret_cast<uintptr_t>(val))); |
| 100 |
} |
| 101 |
|
| 102 |
__attribute__((__used__,__noinline__,__noclone__)) |
| 103 |
PHYSFS_sint64 __wrap_PHYSFS_writeBytes(PHYSFS_File *const handle, const void *const buffer, const PHYSFS_uint64 len) |
| 104 |
{ |
| 105 |
auto p = reinterpret_cast<const uint8_t *>(buffer); |
| 106 |
if (const auto o{VALGRIND_CHECK_MEM_IS_DEFINED(p, len)}) |
| 107 |
con_printf(CON_URGENT, DXX_STRINGIZE_FL(__FILE__, __LINE__, "BUG: ret=%p buffer=%p VALGRIND_CHECK_MEM_IS_DEFINED(%p, %" DXX_PRI_size_type ")=%p (offset=%p)"), __builtin_return_address(0), buffer, p, std::size_t{len}, reinterpret_cast<void *>(o), reinterpret_cast<void *>(o - reinterpret_cast<uintptr_t>(p))); |
| 108 |
return __real_PHYSFS_writeBytes(handle, buffer, len); |
| 109 |
} |
| 110 |
#endif |
| 111 |
#endif |