2018-12-17 13:47:54 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-01-14 15:13:20 -08:00
|
|
|
#include <android-base/file.h>
|
2018-12-17 13:47:54 -08:00
|
|
|
#include <android-base/macros.h>
|
|
|
|
|
#include <gtest/gtest.h>
|
2023-06-12 23:10:52 -07:00
|
|
|
#include <libbpf.h>
|
2018-12-17 13:47:54 -08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <iostream>
|
2021-10-25 21:11:22 +08:00
|
|
|
#include "bpf/BpfMap.h"
|
|
|
|
|
#include "bpf/BpfUtils.h"
|
2018-12-17 13:47:54 -08:00
|
|
|
#include "include/libbpf_android.h"
|
|
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
namespace bpf {
|
|
|
|
|
|
2024-08-02 10:34:20 -07:00
|
|
|
class BpfLoadTest : public ::testing::Test {
|
2018-12-17 13:47:54 -08:00
|
|
|
protected:
|
|
|
|
|
BpfLoadTest() {}
|
2020-01-26 20:17:58 -08:00
|
|
|
int mProgFd;
|
2022-01-14 15:13:20 -08:00
|
|
|
std::string mTpProgPath;
|
2022-01-14 00:18:11 -08:00
|
|
|
std::string mTpNeverLoadProgPath;
|
2024-04-09 17:04:25 +00:00
|
|
|
std::string mTpMapPath;
|
2018-12-17 13:47:54 -08:00
|
|
|
|
|
|
|
|
void SetUp() {
|
2024-04-09 17:04:25 +00:00
|
|
|
/*
|
|
|
|
|
* b/326156952
|
|
|
|
|
*
|
|
|
|
|
* Kernels prior to 5.11 used rlimit memlock accounting for bpf memory
|
|
|
|
|
* allocations, and therefore require increasing the rlimit of this
|
|
|
|
|
* process for the maps to be created successfully.
|
|
|
|
|
*
|
|
|
|
|
* 5.11 introduces cgroup-based accounting as discussed here:
|
|
|
|
|
* https://lore.kernel.org/bpf/20201201215900.3569844-1-guro@fb.com/
|
|
|
|
|
*/
|
|
|
|
|
if (!isAtLeastKernelVersion(5, 11, 0)) EXPECT_EQ(setrlimitForTest(), 0);
|
|
|
|
|
|
2024-08-02 10:34:20 -07:00
|
|
|
mTpProgPath = "/sys/fs/bpf/prog_bpfLoadTpProg_tracepoint_sched_sched_switch";
|
2022-01-14 15:13:20 -08:00
|
|
|
unlink(mTpProgPath.c_str());
|
|
|
|
|
|
2024-08-02 10:34:20 -07:00
|
|
|
mTpNeverLoadProgPath = "/sys/fs/bpf/prog_bpfLoadTpProg_tracepoint_sched_sched_wakeup";
|
2022-01-14 00:18:11 -08:00
|
|
|
unlink(mTpNeverLoadProgPath.c_str());
|
|
|
|
|
|
2024-08-02 10:34:20 -07:00
|
|
|
mTpMapPath = "/sys/fs/bpf/map_bpfLoadTpProg_cpu_pid_map";
|
2022-01-14 15:13:20 -08:00
|
|
|
unlink(mTpMapPath.c_str());
|
2018-12-17 13:47:54 -08:00
|
|
|
|
2024-08-02 10:34:20 -07:00
|
|
|
auto progPath = android::base::GetExecutableDirectory() + "/bpfLoadTpProg.o";
|
2020-06-14 19:27:33 -07:00
|
|
|
bool critical = true;
|
2019-12-12 14:22:34 -08:00
|
|
|
|
|
|
|
|
bpf_prog_type kAllowed[] = {
|
|
|
|
|
BPF_PROG_TYPE_UNSPEC,
|
|
|
|
|
};
|
2022-12-01 20:08:17 -08:00
|
|
|
|
|
|
|
|
Location loc = {
|
|
|
|
|
.dir = "",
|
|
|
|
|
.prefix = "",
|
|
|
|
|
.allowedProgTypes = kAllowed,
|
|
|
|
|
.allowedProgTypesLength = arraysize(kAllowed),
|
|
|
|
|
};
|
|
|
|
|
EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical, loc), -1);
|
2019-12-12 14:22:34 -08:00
|
|
|
|
2024-09-17 00:03:56 +00:00
|
|
|
ASSERT_EQ(android::bpf::loadProg(progPath.c_str(), &critical), 0);
|
2020-06-14 19:27:33 -07:00
|
|
|
EXPECT_EQ(false, critical);
|
2018-12-17 13:47:54 -08:00
|
|
|
|
2023-06-12 23:10:52 -07:00
|
|
|
mProgFd = retrieveProgram(mTpProgPath.c_str());
|
2024-09-17 00:03:56 +00:00
|
|
|
ASSERT_GT(mProgFd, 0);
|
2018-12-17 13:47:54 -08:00
|
|
|
|
|
|
|
|
int ret = bpf_attach_tracepoint(mProgFd, "sched", "sched_switch");
|
|
|
|
|
EXPECT_NE(ret, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TearDown() {
|
|
|
|
|
close(mProgFd);
|
2022-01-14 15:13:20 -08:00
|
|
|
unlink(mTpProgPath.c_str());
|
|
|
|
|
unlink(mTpMapPath.c_str());
|
2018-12-17 13:47:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void checkMapNonZero() {
|
|
|
|
|
// The test program installs a tracepoint on sched:sched_switch
|
|
|
|
|
// and expects the kernel to populate a PID corresponding to CPU
|
2022-01-14 15:13:20 -08:00
|
|
|
android::bpf::BpfMap<uint32_t, uint32_t> m(mTpMapPath.c_str());
|
2018-12-17 13:47:54 -08:00
|
|
|
|
|
|
|
|
// Wait for program to run a little
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
|
|
int non_zero = 0;
|
|
|
|
|
const auto iterFunc = [&non_zero](const uint32_t& key, const uint32_t& val,
|
|
|
|
|
BpfMap<uint32_t, uint32_t>& map) {
|
|
|
|
|
if (val && !non_zero) {
|
|
|
|
|
non_zero = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UNUSED(key);
|
|
|
|
|
UNUSED(map);
|
2020-01-10 17:49:35 -08:00
|
|
|
return base::Result<void>();
|
2018-12-17 13:47:54 -08:00
|
|
|
};
|
|
|
|
|
|
2020-02-10 07:25:16 +09:00
|
|
|
EXPECT_RESULT_OK(m.iterateWithValue(iterFunc));
|
2018-12-17 13:47:54 -08:00
|
|
|
EXPECT_EQ(non_zero, 1);
|
|
|
|
|
}
|
2022-01-18 21:41:16 -08:00
|
|
|
|
2022-01-14 00:18:11 -08:00
|
|
|
void checkKernelVersionEnforced() {
|
2024-09-17 00:03:56 +00:00
|
|
|
ASSERT_EQ(retrieveProgram(mTpNeverLoadProgPath.c_str()), -1);
|
|
|
|
|
ASSERT_EQ(errno, ENOENT);
|
2022-01-14 00:18:11 -08:00
|
|
|
}
|
2018-12-17 13:47:54 -08:00
|
|
|
};
|
|
|
|
|
|
2024-08-02 10:34:20 -07:00
|
|
|
TEST_F(BpfLoadTest, bpfCheckMap) {
|
2018-12-17 13:47:54 -08:00
|
|
|
checkMapNonZero();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 10:34:20 -07:00
|
|
|
TEST_F(BpfLoadTest, bpfCheckMinKernelVersionEnforced) {
|
2022-01-14 00:18:11 -08:00
|
|
|
checkKernelVersionEnforced();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 13:47:54 -08:00
|
|
|
} // namespace bpf
|
|
|
|
|
} // namespace android
|