Merge "refactor common logic into abortOnKeyOrValueSizeMismatch"

This commit is contained in:
Treehugger Robot
2022-06-23 19:53:41 +00:00
committed by Gerrit Code Review

View File

@@ -50,10 +50,8 @@ class BpfMap {
// (later on, for testing, we still make available a copy assignment operator) // (later on, for testing, we still make available a copy assignment operator)
BpfMap<Key, Value>(const BpfMap<Key, Value>&) = delete; BpfMap<Key, Value>(const BpfMap<Key, Value>&) = delete;
protected: private:
// flag must be within BPF_OBJ_FLAG_MASK, ie. 0, BPF_F_RDONLY, BPF_F_WRONLY void abortOnKeyOrValueSizeMismatch() {
BpfMap<Key, Value>(const char* pathname, uint32_t flags) {
mMapFd.reset(mapRetrieve(pathname, flags));
if (!mMapFd.ok()) abort(); if (!mMapFd.ok()) abort();
if (isAtLeastKernelVersion(4, 14, 0)) { if (isAtLeastKernelVersion(4, 14, 0)) {
if (bpfGetFdKeySize(mMapFd) != sizeof(Key)) abort(); if (bpfGetFdKeySize(mMapFd) != sizeof(Key)) abort();
@@ -61,6 +59,13 @@ class BpfMap {
} }
} }
protected:
// flag must be within BPF_OBJ_FLAG_MASK, ie. 0, BPF_F_RDONLY, BPF_F_WRONLY
BpfMap<Key, Value>(const char* pathname, uint32_t flags) {
mMapFd.reset(mapRetrieve(pathname, flags));
abortOnKeyOrValueSizeMismatch();
}
public: public:
explicit BpfMap<Key, Value>(const char* pathname) : BpfMap<Key, Value>(pathname, 0) {} explicit BpfMap<Key, Value>(const char* pathname) : BpfMap<Key, Value>(pathname, 0) {}
@@ -117,14 +122,11 @@ class BpfMap {
if (!mMapFd.ok()) { if (!mMapFd.ok()) {
return ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path); return ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path);
} }
if (isAtLeastKernelVersion(4, 14, 0)) { // Normally we should return an error here instead of calling abort,
// Normally we should return an error here instead of calling abort, // but this cannot happen at runtime without a massive code bug (K/V type mismatch)
// but this cannot happen at runtime without a massive code bug (K/V type mismatch) // and as such it's better to just blow the system up and let the developer fix it.
// and as such it's better to just blow the system up and let the developer fix it. // Crashes are much more likely to be noticed than logs and missing functionality.
// Crashes are much more likely to be noticed than logs and missing functionality. abortOnKeyOrValueSizeMismatch();
if (bpfGetFdKeySize(mMapFd) != sizeof(Key)) abort();
if (bpfGetFdValueSize(mMapFd) != sizeof(Value)) abort();
}
return {}; return {};
} }
@@ -202,11 +204,7 @@ class BpfMap {
// check BpfMap.isValid() and look at errno and see why systemcall() failed. // check BpfMap.isValid() and look at errno and see why systemcall() failed.
[[clang::reinitializes]] void reset(int fd) { [[clang::reinitializes]] void reset(int fd) {
mMapFd.reset(fd); mMapFd.reset(fd);
if ((fd >= 0) && isAtLeastKernelVersion(4, 14, 0)) { if (mMapFd.ok()) abortOnKeyOrValueSizeMismatch();
if (bpfGetFdKeySize(mMapFd) != sizeof(Key)) abort();
if (bpfGetFdValueSize(mMapFd) != sizeof(Value)) abort();
if (bpfGetFdMapFlags(mMapFd) != 0) abort(); // TODO: fix for BpfMapRO
}
} }
#endif #endif