diff --git a/staticlibs/native/bpf_headers/include/bpf/BpfMap.h b/staticlibs/native/bpf_headers/include/bpf/BpfMap.h index 297b20017b..47256fa098 100644 --- a/staticlibs/native/bpf_headers/include/bpf/BpfMap.h +++ b/staticlibs/native/bpf_headers/include/bpf/BpfMap.h @@ -50,10 +50,8 @@ class BpfMap { // (later on, for testing, we still make available a copy assignment operator) BpfMap(const BpfMap&) = delete; - protected: - // flag must be within BPF_OBJ_FLAG_MASK, ie. 0, BPF_F_RDONLY, BPF_F_WRONLY - BpfMap(const char* pathname, uint32_t flags) { - mMapFd.reset(mapRetrieve(pathname, flags)); + private: + void abortOnKeyOrValueSizeMismatch() { if (!mMapFd.ok()) abort(); if (isAtLeastKernelVersion(4, 14, 0)) { 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(const char* pathname, uint32_t flags) { + mMapFd.reset(mapRetrieve(pathname, flags)); + abortOnKeyOrValueSizeMismatch(); + } + public: explicit BpfMap(const char* pathname) : BpfMap(pathname, 0) {} @@ -117,14 +122,11 @@ class BpfMap { if (!mMapFd.ok()) { 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, - // 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. - // Crashes are much more likely to be noticed than logs and missing functionality. - if (bpfGetFdKeySize(mMapFd) != sizeof(Key)) abort(); - if (bpfGetFdValueSize(mMapFd) != sizeof(Value)) 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) + // 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. + abortOnKeyOrValueSizeMismatch(); return {}; } @@ -202,11 +204,7 @@ class BpfMap { // check BpfMap.isValid() and look at errno and see why systemcall() failed. [[clang::reinitializes]] void reset(int fd) { mMapFd.reset(fd); - if ((fd >= 0) && isAtLeastKernelVersion(4, 14, 0)) { - if (bpfGetFdKeySize(mMapFd) != sizeof(Key)) abort(); - if (bpfGetFdValueSize(mMapFd) != sizeof(Value)) abort(); - if (bpfGetFdMapFlags(mMapFd) != 0) abort(); // TODO: fix for BpfMapRO - } + if (mMapFd.ok()) abortOnKeyOrValueSizeMismatch(); } #endif