rocksdb/utilities
Peter Dillinger 54cb9c77d9 Prefer static_cast in place of most reinterpret_cast (#12308)
Summary:
The following are risks associated with pointer-to-pointer reinterpret_cast:
* Can produce the "wrong result" (crash or memory corruption). IIRC, in theory this can happen for any up-cast or down-cast for a non-standard-layout type, though in practice would only happen for multiple inheritance cases (where the base class pointer might be "inside" the derived object). We don't use multiple inheritance a lot, but we do.
* Can mask useful compiler errors upon code change, including converting between unrelated pointer types that you are expecting to be related, and converting between pointer and scalar types unintentionally.

I can only think of some obscure cases where static_cast could be troublesome when it compiles as a replacement:
* Going through `void*` could plausibly cause unnecessary or broken pointer arithmetic. Suppose we have
`struct Derived: public Base1, public Base2`.  If we have `Derived*` -> `void*` -> `Base2*` -> `Derived*` through reinterpret casts, this could plausibly work (though technical UB) assuming the `Base2*` is not dereferenced. Changing to static cast could introduce breaking pointer arithmetic.
* Unnecessary (but safe) pointer arithmetic could arise in a case like `Derived*` -> `Base2*` -> `Derived*` where before the Base2 pointer might not have been dereferenced. This could potentially affect performance.

With some light scripting, I tried replacing pointer-to-pointer reinterpret_casts with static_cast and kept the cases that still compile. Most occurrences of reinterpret_cast have successfully been changed (except for java/ and third-party/). 294 changed, 257 remain.

A couple of related interventions included here:
* Previously Cache::Handle was not actually derived from in the implementations and just used as a `void*` stand-in with reinterpret_cast. Now there is a relationship to allow static_cast. In theory, this could introduce pointer arithmetic (as described above) but is unlikely without multiple inheritance AND non-empty Cache::Handle.
* Remove some unnecessary casts to void* as this is allowed to be implicit (for better or worse).

Most of the remaining reinterpret_casts are for converting to/from raw bytes of objects. We could consider better idioms for these patterns in follow-up work.

I wish there were a way to implement a template variant of static_cast that would only compile if no pointer arithmetic is generated, but best I can tell, this is not possible. AFAIK the best you could do is a dynamic check that the void* conversion after the static cast is unchanged.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/12308

Test Plan: existing tests, CI

Reviewed By: ltamasi

Differential Revision: D53204947

Pulled By: pdillinger

fbshipit-source-id: 9de23e618263b0d5b9820f4e15966876888a16e2
2024-02-07 10:44:11 -08:00
..
agg_merge Remove unnecessary, confusing 'extern' (#12300) 2024-01-29 10:38:08 -08:00
backup Prefer static_cast in place of most reinterpret_cast (#12308) 2024-02-07 10:44:11 -08:00
blob_db Prefer static_cast in place of most reinterpret_cast (#12308) 2024-02-07 10:44:11 -08:00
cassandra Remove 'virtual' when implied by 'override' (#12319) 2024-01-31 13:14:42 -08:00
checkpoint Remove the default force behavior for `EnableFileDeletion` API (#12001) 2023-11-10 14:35:54 -08:00
compaction_filters Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
convenience Run clang-format on utilities/ (except utilities/transactions/) (#10853) 2022-10-24 16:38:09 -07:00
leveldb_options Put Cache and CacheWrapper in new public header (#11192) 2023-02-09 12:12:02 -08:00
memory Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
merge_operators Remove 'virtual' when implied by 'override' (#12319) 2024-01-31 13:14:42 -08:00
option_change_migration Enforce status checking after Valid() returns false for IteratorWrapper (#11975) 2023-10-18 09:38:38 -07:00
options Group SST write in flush, compaction and db open with new stats (#11910) 2023-12-29 15:29:23 -08:00
persistent_cache Prefer static_cast in place of most reinterpret_cast (#12308) 2024-02-07 10:44:11 -08:00
simulator_cache Group SST write in flush, compaction and db open with new stats (#11910) 2023-12-29 15:29:23 -08:00
table_properties_collectors Remove 'virtual' when implied by 'override' (#12319) 2024-01-31 13:14:42 -08:00
trace Prefer static_cast in place of most reinterpret_cast (#12308) 2024-02-07 10:44:11 -08:00
transactions Prefer static_cast in place of most reinterpret_cast (#12308) 2024-02-07 10:44:11 -08:00
ttl Remove 'virtual' when implied by 'override' (#12319) 2024-01-31 13:14:42 -08:00
write_batch_with_index Some further cleanup in WriteBatchWithIndex::MultiGetFromBatchAndDB (#12143) 2023-12-13 17:34:18 -08:00
cache_dump_load.cc Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
cache_dump_load_impl.cc Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
cache_dump_load_impl.h Remove 'virtual' when implied by 'override' (#12319) 2024-01-31 13:14:42 -08:00
compaction_filters.cc Remove FactoryFunc from LoadXXXObject (#11203) 2023-02-17 12:54:07 -08:00
counted_fs.cc Fix serious FSDirectory use-after-Close bug (missing fsync) (#10460) 2022-08-02 10:54:32 -07:00
counted_fs.h Explicitly closing all directory file descriptors (#10049) 2022-06-01 18:03:34 -07:00
debug.cc Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
env_mirror.cc Remove extra semi colon from internal_repo_rocksdb/repo/utilities/env_mirror.cc (#12271) 2024-01-24 07:37:31 -08:00
env_mirror_test.cc Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
env_timed.cc Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
env_timed.h Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
env_timed_test.cc Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
fault_injection_env.cc Explicitly closing all directory file descriptors (#10049) 2022-06-01 18:03:34 -07:00
fault_injection_env.h Remove 'virtual' when implied by 'override' (#12319) 2024-01-31 13:14:42 -08:00
fault_injection_fs.cc Fix potential incorrect result for duplicate key in MultiGet (#12295) 2024-02-02 11:48:35 -08:00
fault_injection_fs.h Remove 'virtual' when implied by 'override' (#12319) 2024-01-31 13:14:42 -08:00
fault_injection_secondary_cache.cc Add some compressed and tiered secondary cache stats (#12150) 2023-12-15 11:34:08 -08:00
fault_injection_secondary_cache.h Remove 'virtual' when implied by 'override' (#12319) 2024-01-31 13:14:42 -08:00
memory_allocators.h Major Cache refactoring, CPU efficiency improvement (#10975) 2023-01-11 14:20:40 -08:00
merge_operators.cc Remove FactoryFunc from LoadXXXObject (#11203) 2023-02-17 12:54:07 -08:00
merge_operators.h Run clang-format on utilities/ (except utilities/transactions/) (#10853) 2022-10-24 16:38:09 -07:00
object_registry.cc Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
object_registry_test.cc Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
util_merge_operators_test.cc Print stack traces on frozen tests in CI (#10828) 2022-10-18 00:35:35 -07:00
wal_filter.cc Remove FactoryFunc from LoadXXXObject (#11203) 2023-02-17 12:54:07 -08:00