Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arm64: define the KASAN_SHADOW_SCALE_SHIFT macro #2518

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to
- [#2545](https://github.com/iovisor/bpftrace/pull/2545)
- Treat str() builtin's len parameter as int64
- [#2546](https://github.com/iovisor/bpftrace/pull/2546)
- arm64: define the KASAN_SHADOW_SCALE_SHIFT macro
- [#2518](https://github.com/iovisor/bpftrace/pull/2518)
#### Docs
#### Tools

Expand Down
1 change: 1 addition & 0 deletions src/bpftrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class BPFtrace
std::map<libbpf::bpf_func_id, location> helper_use_loc_;
// mapping traceable functions to modules (or "vmlinux") that they appear in
FuncsModulesMap traceable_funcs_;
KConfig kconfig;
std::vector<std::unique_ptr<AttachedProbe>> attached_probes_;

std::map<std::string, std::unique_ptr<PCAPwriter>> pcap_writers;
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ static std::optional<struct timespec> get_boottime()
kobj = std::get<1>(kdirs);

if (ksrc != "")
extra_flags = get_kernel_cflags(utsname.machine, ksrc, kobj);
extra_flags = get_kernel_cflags(
utsname.machine, ksrc, kobj, bpftrace.kconfig);
}
extra_flags.push_back("-include");
extra_flags.push_back(CLANG_WORKAROUNDS_H);
Expand Down
72 changes: 68 additions & 4 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <bcc/bcc_syms.h>
#include <bcc/bcc_usdt.h>
#include <elf.h>
#include <zlib.h>

#include <linux/version.h>

Expand Down Expand Up @@ -178,6 +179,55 @@ StdioSilencer::~StdioSilencer()
}
}

KConfig::KConfig()
{
std::vector<std::string> config_locs;

// Try to get the config from BPFTRACE_KCONFIG_TEST env
// If not set, use the set of default locations
const char *path_env = std::getenv("BPFTRACE_KCONFIG_TEST");
if (path_env)
config_locs = { std::string(path_env) };
else
{
struct utsname utsname;
if (uname(&utsname) < 0)
return;
config_locs = {
"/proc/config.gz",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe order procfs option first. The /boot/.. configs can suffer from skew when installing new kernels.

"/boot/config-" + std::string(utsname.release),
};
}

for (auto &path : config_locs)
{
// gzopen/gzgets handle both uncompressed and compressed files
gzFile file = gzopen(path.c_str(), "r");
if (!file)
continue;

char buf[4096];
while (gzgets(file, buf, sizeof(buf)))
{
std::string option(buf);
if (option.find("CONFIG_") == 0)
{
// trim trailing '\n'
if (option[option.length() - 1] == '\n')
option = option.substr(0, option.length() - 1);

auto split = option.find("=");
if (split == std::string::npos)
continue;

config.emplace(option.substr(0, split), option.substr(split + 1));
}
}
gzclose(file);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a break after this? If both locations are available, probably best not to duplicate effort.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course, thanks.

break;
}
}

bool get_uint64_env_var(const std::string &str, uint64_t &dest)
{
if (const char* env_p = std::getenv(str.c_str()))
Expand Down Expand Up @@ -314,10 +364,10 @@ std::vector<int> get_possible_cpus()
return read_cpu_range("/sys/devices/system/cpu/possible");
}

std::vector<std::string> get_kernel_cflags(
const char* uname_machine,
const std::string& ksrc,
const std::string& kobj)
std::vector<std::string> get_kernel_cflags(const char *uname_machine,
const std::string &ksrc,
const std::string &kobj,
const KConfig &kconfig)
{
std::vector<std::string> cflags;
std::string arch = uname_machine;
Expand Down Expand Up @@ -383,6 +433,20 @@ std::vector<std::string> get_kernel_cflags(
cflags.push_back("-D__LINUX_ARM_ARCH__=7");
}

if (arch == "arm64")
{
// arm64 defines KASAN_SHADOW_SCALE_SHIFT in a Makefile instead of defining
// it in a header file. Since we're not executing make, we need to set the
// value manually (values are taken from arch/arm64/Makefile).
if (kconfig.has_value("CONFIG_KASAN", "y"))
{
if (kconfig.has_value("CONFIG_KASAN_SW_TAGS", "y"))
cflags.push_back("-DKASAN_SHADOW_SCALE_SHIFT=4");
else
cflags.push_back("-DKASAN_SHADOW_SCALE_SHIFT=3");
}
}

return cflags;
}

Expand Down
15 changes: 14 additions & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ struct DeprecatedName
typedef std::unordered_map<std::string, std::unordered_set<std::string>>
FuncsModulesMap;

struct KConfig
{
KConfig();
bool has_value(const std::string &name, const std::string &value) const
{
auto c = config.find(name);
return c != config.end() && c->second == value;
}

std::unordered_map<std::string, std::string> config;
};

static std::vector<DeprecatedName> DEPRECATED_LIST =
{
};
Expand Down Expand Up @@ -166,7 +178,8 @@ std::tuple<std::string, std::string> get_kernel_dirs(
bool unpack_kheaders);
std::vector<std::string> get_kernel_cflags(const char *uname_machine,
const std::string &ksrc,
const std::string &kobj);
const std::string &kobj,
const KConfig &kconfig);
std::string get_cgroup_path_in_hierarchy(uint64_t cgroupid,
std::string base_path);
std::vector<std::pair<std::string, std::string>> get_cgroup_hierarchy_roots();
Expand Down
22 changes: 22 additions & 0 deletions tests/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,28 @@ TEST(utils, get_cgroup_path_in_hierarchy)
}
}

TEST(utils, parse_kconfig)
{
char path[] = "/tmp/configXXXXXX";
int fd = mkstemp(path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to unlink this after use

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and resolved the changelog conflict.

const std::string config = "# Intro comment\n"
"CONFIG_YES=y\n"
"CONFIG_MOD=m\n"
"CONFIG_VAL=42\n"
"# CONFIG_NO is not set";
EXPECT_EQ(write(fd, config.c_str(), config.length()), config.length());
setenv("BPFTRACE_KCONFIG_TEST", path, true);
close(fd);

KConfig kconfig;
ASSERT_TRUE(kconfig.has_value("CONFIG_YES", "y"));
ASSERT_TRUE(kconfig.has_value("CONFIG_MOD", "m"));
ASSERT_TRUE(kconfig.has_value("CONFIG_VAL", "42"));
ASSERT_EQ(kconfig.config.find("CONFIG_NO"), kconfig.config.end());

unlink(path);
}

} // namespace utils
} // namespace test
} // namespace bpftrace