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)
BpfMap<Key, Value>(const BpfMap<Key, Value>&) = delete;
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));
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<Key, Value>(const char* pathname, uint32_t flags) {
mMapFd.reset(mapRetrieve(pathname, flags));
abortOnKeyOrValueSizeMismatch();
}
public:
explicit BpfMap<Key, Value>(const char* pathname) : BpfMap<Key, Value>(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();
}
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