|
| 1 | +# Symbol Visibility Implementation for RocksDB |
| 2 | + |
| 3 | +### What I've Accomplished |
| 4 | + |
| 5 | +#### 1. Created the Visibility Macro (port_defs.h) |
| 6 | +Added `ROCKSDB_API` macro that works across platforms: |
| 7 | +- Linux/Unix: Uses GCC/Clang's `__attribute__((visibility("default")))` |
| 8 | +- Windows: Uses `__declspec(dllexport/dllimport)` |
| 9 | +- Static builds: No overhead (expands to nothing) |
| 10 | + |
| 11 | +This lets us mark which classes/functions should be exported. |
| 12 | + |
| 13 | +#### 2. Updated Build Systems |
| 14 | +**CMake**: Added `HIDE_PRIVATE_SYMBOLS` option that: |
| 15 | +- Sets compiler to hide all symbols by default |
| 16 | +- Only exports what we explicitly mark with `ROCKSDB_API` |
| 17 | +- Default is OFF (backward compatible) |
| 18 | + |
| 19 | +**Makefile**: Added support via `HIDE_PRIVATE_SYMBOLS=1` environment variable |
| 20 | + |
| 21 | +#### 3. Started Marking Public APIs |
| 22 | +Applied `ROCKSDB_API` to the main `DB` class as a proof of concept. |
| 23 | + |
| 24 | +#### 4. Fixed a Bug |
| 25 | +Found and fixed a Clang compatibility issue with `using enum` - changed to fully qualified enum names in `db.h`. |
| 26 | + |
| 27 | +### Testing Done |
| 28 | + |
| 29 | +**Task 1: Checked compiler options** - Visibility flags working correctly |
| 30 | +**Task 2: Build without breaking** - Both default and hidden modes compile fine |
| 31 | +**Task 3: Tested with Clang** - Works with both GCC and Clang |
| 32 | + |
| 33 | +**Important**: This doesn't affect functionality at all. When disabled (default), it's like the code changes don't exist. When enabled, it only affects which symbols are visible in the symbol table - the actual code behavior is identical. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## Overview |
| 38 | +This implementation addresses GitHub issue #12929: "RocksDB library leaks private symbols" |
| 39 | + |
| 40 | +## The Problem We're Solving |
| 41 | + |
| 42 | +Right now, RocksDB exports everything - all internal classes, templates, helper functions, etc. This causes: |
| 43 | +- Thousands of symbols leaking (seen in Debian package builds) |
| 44 | +- ABI breaks when we change internal code |
| 45 | +- Larger libraries and slower linking |
| 46 | +- Conflicts with other libraries |
| 47 | + |
| 48 | +## What Needs to Happen Next |
| 49 | + |
| 50 | +### Phase 1: Infrastructure (Pretty Done) |
| 51 | +The big job is going through all the public API headers and marking classes with `ROCKSDB_API`. Here's what's left: |
| 52 | + |
| 53 | +### Must Mark (High Priority - ~20 classes) |
| 54 | +These are the core APIs that users interact with: |
| 55 | +- `Options`, `DBOptions`, `ColumnFamilyOptions` (options.h) |
| 56 | +- `ReadOptions`, `WriteOptions` (options.h) |
| 57 | +- `Status`, `IOStatus` (status.h, io_status.h) |
| 58 | +- `Iterator`, `WriteBatch` (iterator.h, write_batch.h) |
| 59 | +- `Snapshot`, `Cache` (snapshot.h, cache.h) |
| 60 | +- `Env`, `FileSystem` (env.h, file_system.h) |
| 61 | +- `Logger`, `Statistics` (env.h, statistics.h) |
| 62 | +- `ColumnFamilyHandle` (db.h) |
| 63 | +- `Slice`, `PinnableSlice` (slice.h) |
| 64 | +- `Comparator`, `MergeOperator` (comparator.h, merge_operator.h) |
| 65 | + |
| 66 | +### Should Mark (Medium Priority - ~30-40 classes) |
| 67 | +- Everything in `include/rocksdb/utilities/*.h` |
| 68 | +- Listener/callback classes |
| 69 | +- Filter and table factory classes |
| 70 | +- SST file readers/writers |
| 71 | + |
| 72 | +### Can Skip (Internal/Private) |
| 73 | +- Anything not in `include/` directory |
| 74 | +- Implementation details in `db/`, `util/`, etc. |
| 75 | + |
| 76 | +## Usage Examples |
| 77 | + |
| 78 | +### For Users (Default Behavior - Backward Compatible) |
| 79 | +```bash |
| 80 | +# No changes needed - works as before |
| 81 | +make shared_lib |
| 82 | +cmake .. && make |
| 83 | +``` |
| 84 | + |
| 85 | +### For Packagers (Enable Symbol Hiding) |
| 86 | +```bash |
| 87 | +# CMake |
| 88 | +cmake -DHIDE_PRIVATE_SYMBOLS=ON .. |
| 89 | + |
| 90 | +# Makefile |
| 91 | +HIDE_PRIVATE_SYMBOLS=1 make shared_lib |
| 92 | +``` |
| 93 | + |
| 94 | +### For Developers (Marking New Public APIs) |
| 95 | +```cpp |
| 96 | +// In public header include/rocksdb/my_new_class.h |
| 97 | +#include "rocksdb/port_defs.h" |
| 98 | + |
| 99 | +namespace ROCKSDB_NAMESPACE { |
| 100 | + |
| 101 | +// Export this class in shared library |
| 102 | +class ROCKSDB_API MyPublicClass { |
| 103 | + public: |
| 104 | + ROCKSDB_API void PublicMethod(); |
| 105 | + |
| 106 | + private: |
| 107 | + void PrivateMethod(); // Not exported |
| 108 | +}; |
| 109 | + |
| 110 | +} // namespace ROCKSDB_NAMESPACE |
| 111 | +``` |
| 112 | +
|
| 113 | +## Benefits |
| 114 | +
|
| 115 | +### When Fully Implemented |
| 116 | +1. **ABI Stability**: Only intentional API changes break compatibility |
| 117 | +2. **Smaller Binaries**: Less symbols to export/resolve |
| 118 | +3. **Faster Linking**: Dynamic linker has less work |
| 119 | +4. **Better Encapsulation**: Internal changes invisible to users |
| 120 | +5. **Package Maintainer Friendly**: Clean symbol files for Debian, etc. |
| 121 | +
|
| 122 | +### Backward Compatibility |
| 123 | +- **Default behavior unchanged**: All symbols still exported by default |
| 124 | +- **Opt-in feature**: Must explicitly enable `HIDE_PRIVATE_SYMBOLS` |
| 125 | +- **No source-level changes** required for existing code |
| 126 | +- **Binary compatibility**: Only affects shared library symbol table |
| 127 | +
|
| 128 | +## References |
| 129 | +
|
| 130 | +- **GitHub Issue**: https://github.com/facebook/rocksdb/issues/12929 |
| 131 | +- **Related PR**: https://github.com/facebook/rocksdb/pull/12944 |
| 132 | +- **GCC Visibility**: https://gcc.gnu.org/wiki/Visibility |
| 133 | +- **Clang Visibility**: https://clang.llvm.org/docs/AttributeReference.html#visibility |
| 134 | +
|
| 135 | +## Rollout Plan |
| 136 | +
|
| 137 | +### Phase 1 (Done) |
| 138 | +- Infrastructure implementation |
| 139 | +- Feature flag protection |
| 140 | +- CMake and Makefile support |
| 141 | +- Initial API marking (DB class) |
| 142 | +
|
| 143 | +### Phase 2 (TODO) |
| 144 | +- Mark all public API classes (~50-70 classes) |
| 145 | +- Comprehensive testing |
| 146 | +- Documentation updates |
| 147 | +
|
| 148 | +### Phase 3 (FUTURE) |
| 149 | +- Default to ON in RocksDB 10.0 |
| 150 | +- Deprecation warnings for old behavior |
| 151 | +- Remove feature flag in 11.0 |
| 152 | +
|
| 153 | +## Known Limitations |
| 154 | +
|
| 155 | +1. **JNI**: Needs testing to ensure Java bindings still work |
| 156 | +2. **Plugins**: Plugin API must be explicitly exported |
| 157 | +3. **Custom Implementations**: Users with custom Env, Cache, etc. may need updates |
| 158 | +4. **Template Instantiations**: Some might need explicit instantiation |
| 159 | +
|
| 160 | +## How to Use This |
| 161 | +
|
| 162 | +### Default (Unchanged Behavior) |
| 163 | +Just build normally - nothing changes: |
| 164 | +```bash |
| 165 | +make shared_lib |
| 166 | +# or |
| 167 | +cmake .. && make |
| 168 | +``` |
| 169 | + |
| 170 | +### Enable Symbol Hiding (For Testing/Packaging) |
| 171 | +```bash |
| 172 | +# With Make: |
| 173 | +HIDE_PRIVATE_SYMBOLS=1 make shared_lib |
| 174 | + |
| 175 | +# With CMake: |
| 176 | +cmake -DHIDE_PRIVATE_SYMBOLS=ON .. |
| 177 | +make |
| 178 | +``` |
| 179 | + |
| 180 | +## Files I Changed |
| 181 | + |
| 182 | +``` |
| 183 | +include/rocksdb/port_defs.h - Added ROCKSDB_API macro |
| 184 | +include/rocksdb/db.h - Fixed Clang enum issue, marked DB class |
| 185 | +include/rocksdb/options.h - Added port_defs.h include |
| 186 | +CMakeLists.txt - Added visibility preset logic |
| 187 | +build_tools/build_detect_platform - Added visibility flags for Make |
| 188 | +``` |
| 189 | + |
| 190 | +--- |
0 commit comments