2022-12-19 14:37:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2023-04-26 16:20:53 +01:00
|
|
|
#include "oi/OILibraryImpl.h"
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <glog/logging.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
#include <fstream>
|
|
|
|
|
2023-05-18 14:00:10 +01:00
|
|
|
#include "oi/Headers.h"
|
2023-04-26 16:20:53 +01:00
|
|
|
#include "oi/OIParser.h"
|
|
|
|
#include "oi/OIUtils.h"
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <libelf.h>
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ObjectIntrospection {
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
OILibraryImpl::OILibraryImpl(OILibrary* self, void* TemplateFunc)
|
2022-12-19 14:37:51 +00:00
|
|
|
: _self(self), _TemplateFunc(TemplateFunc) {
|
|
|
|
if (_self->opts.debugLevel != 0) {
|
|
|
|
google::LogToStderr();
|
|
|
|
google::SetStderrLogging(0);
|
|
|
|
google::SetVLOGLevel("*", _self->opts.debugLevel);
|
|
|
|
// Upstream glog defines `GLOG_INFO` as 0 https://fburl.com/ydjajhz0,
|
|
|
|
// but internally it's defined as 1 https://fburl.com/code/9fwams75
|
|
|
|
//
|
|
|
|
// We don't want to link gflags in OIL, so setting it via the flags rather
|
|
|
|
// than with gflags::SetCommandLineOption
|
|
|
|
FLAGS_minloglevel = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OILibraryImpl::~OILibraryImpl() {
|
|
|
|
unmapSegment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OILibraryImpl::mapSegment() {
|
2023-03-22 16:16:17 +00:00
|
|
|
void* textSeg =
|
2022-12-19 14:37:51 +00:00
|
|
|
mmap(NULL, segConfig.textSegSize, PROT_EXEC | PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
if (textSeg == MAP_FAILED) {
|
|
|
|
PLOG(ERROR) << "error mapping text segment";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
segConfig.textSegBase = textSeg;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OILibraryImpl::unmapSegment() {
|
|
|
|
if (segConfig.textSegBase != nullptr &&
|
|
|
|
munmap(segConfig.textSegBase, segConfig.textSegSize) != 0) {
|
|
|
|
PLOG(ERROR) << "error unmapping text segment";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OILibraryImpl::initCompiler() {
|
|
|
|
symbols = std::make_shared<SymbolService>(getpid());
|
|
|
|
|
|
|
|
generatorConfig.useDataSegment = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OILibraryImpl::processConfigFile() {
|
2023-04-17 19:13:59 +01:00
|
|
|
auto features = OIUtils::processConfigFile(
|
|
|
|
_self->opts.configFilePath,
|
|
|
|
{
|
|
|
|
{Feature::ChaseRawPointers, _self->opts.chaseRawPointers},
|
|
|
|
{Feature::PackStructs, true},
|
2023-06-26 13:06:09 +01:00
|
|
|
{Feature::GenJitDebug, _self->opts.generateJitDebugInfo},
|
2023-04-17 19:13:59 +01:00
|
|
|
},
|
|
|
|
compilerConfig, generatorConfig);
|
|
|
|
if (!features) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
generatorConfig.features = *features;
|
2023-06-26 13:06:09 +01:00
|
|
|
compilerConfig.features = *features;
|
2023-04-17 19:13:59 +01:00
|
|
|
return true;
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class F>
|
|
|
|
class Cleanup {
|
|
|
|
T resource;
|
|
|
|
F cleanupFunc;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Cleanup(T _resource, F _cleanupFunc)
|
|
|
|
: resource{_resource}, cleanupFunc{_cleanupFunc} {};
|
|
|
|
~Cleanup() {
|
|
|
|
cleanupFunc(resource);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
void close_file(std::FILE* fp) {
|
2022-12-19 14:37:51 +00:00
|
|
|
std::fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int OILibraryImpl::compileCode() {
|
|
|
|
OICompiler compiler{symbols, compilerConfig};
|
|
|
|
|
|
|
|
int objectMemfd = memfd_create("oil_object_code", 0);
|
|
|
|
if (!objectMemfd) {
|
|
|
|
PLOG(ERROR) << "failed to create memfd for object code";
|
|
|
|
return Response::OIL_COMPILATION_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
using unique_file_t = std::unique_ptr<std::FILE, decltype(&close_file)>;
|
|
|
|
unique_file_t objectStream(fdopen(objectMemfd, "w+"), &close_file);
|
|
|
|
if (!objectStream) {
|
|
|
|
PLOG(ERROR) << "failed to convert memfd to stream";
|
|
|
|
// This only needs to be cleaned up in the error case, as the fclose
|
|
|
|
// on the unique_file_t will clean up the underlying fd if it was
|
|
|
|
// created successfully.
|
|
|
|
close(objectMemfd);
|
|
|
|
return Response::OIL_COMPILATION_FAILURE;
|
|
|
|
}
|
|
|
|
auto objectPath =
|
|
|
|
fs::path((boost::format("/dev/fd/%1%") % objectMemfd).str());
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
struct drgn_program* prog = symbols->getDrgnProgram();
|
2023-03-16 13:09:18 +00:00
|
|
|
if (!prog) {
|
|
|
|
return Response::OIL_COMPILATION_FAILURE;
|
|
|
|
}
|
2023-03-22 16:16:17 +00:00
|
|
|
struct drgn_symbol* sym;
|
2023-03-16 13:09:18 +00:00
|
|
|
if (auto err = drgn_program_find_symbol_by_address(
|
|
|
|
prog, (uintptr_t)_TemplateFunc, &sym)) {
|
|
|
|
LOG(ERROR) << "Error when finding symbol by address " << err->code << " "
|
|
|
|
<< err->message;
|
|
|
|
drgn_error_destroy(err);
|
|
|
|
return Response::OIL_COMPILATION_FAILURE;
|
|
|
|
}
|
2023-03-22 16:16:17 +00:00
|
|
|
const char* name = drgn_symbol_name(sym);
|
2023-03-16 13:09:18 +00:00
|
|
|
drgn_symbol_destroy(sym);
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-03-16 13:09:18 +00:00
|
|
|
// TODO: change this to the new drgn interface from symbol -> type
|
|
|
|
auto rootType = symbols->getRootType(irequest{"entry", name, "arg0"});
|
|
|
|
if (!rootType.has_value()) {
|
|
|
|
LOG(ERROR) << "Failed to get type of probe argument";
|
|
|
|
return Response::OIL_COMPILATION_FAILURE;
|
|
|
|
}
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-06-29 13:07:46 +01:00
|
|
|
std::string code(headers::oi_OITraceCode_cpp);
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-03-16 13:09:18 +00:00
|
|
|
auto codegen = OICodeGen::buildFromConfig(generatorConfig, *symbols);
|
|
|
|
if (!codegen) {
|
|
|
|
return OIL_COMPILATION_FAILURE;
|
|
|
|
}
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-03-16 13:09:18 +00:00
|
|
|
codegen->setRootType(rootType->type);
|
|
|
|
if (!codegen->generate(code)) {
|
|
|
|
return Response::OIL_COMPILATION_FAILURE;
|
|
|
|
}
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-03-16 13:09:18 +00:00
|
|
|
std::string sourcePath = _self->opts.sourceFileDumpPath;
|
|
|
|
if (_self->opts.sourceFileDumpPath.empty()) {
|
|
|
|
// This is the path Clang acts as if it has compiled from e.g. for debug
|
|
|
|
// information. It does not need to exist.
|
|
|
|
sourcePath = "oil_jit.cpp";
|
2022-12-19 14:37:51 +00:00
|
|
|
} else {
|
2023-03-16 13:09:18 +00:00
|
|
|
std::ofstream outputFile(sourcePath);
|
|
|
|
outputFile << code;
|
|
|
|
}
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-03-16 13:09:18 +00:00
|
|
|
if (!compiler.compile(code, sourcePath, objectPath)) {
|
|
|
|
return Response::OIL_COMPILATION_FAILURE;
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto relocRes = compiler.applyRelocs(
|
|
|
|
reinterpret_cast<uint64_t>(segConfig.textSegBase), {objectPath}, {});
|
|
|
|
if (!relocRes.has_value()) {
|
|
|
|
return Response::OIL_RELOCATION_FAILURE;
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
const auto& [_, segments, jitSymbols] = relocRes.value();
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
// Locate the probe's entry point
|
|
|
|
_self->fp = nullptr;
|
2023-03-22 16:16:17 +00:00
|
|
|
for (const auto& [symName, symAddr] : jitSymbols) {
|
2022-12-19 14:37:51 +00:00
|
|
|
if (symName.starts_with("_Z7getSize")) {
|
2023-03-22 16:16:17 +00:00
|
|
|
_self->fp = (size_t(*)(const void*))symAddr;
|
2022-12-19 14:37:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 13:09:18 +00:00
|
|
|
if (!_self->fp) {
|
2022-12-19 14:37:51 +00:00
|
|
|
return Response::OIL_RELOCATION_FAILURE;
|
2023-03-16 13:09:18 +00:00
|
|
|
}
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
// Copy relocated segments in their final destination
|
2023-03-22 16:16:17 +00:00
|
|
|
for (const auto& [BaseAddr, RelocAddr, Size] : segments)
|
|
|
|
memcpy((void*)RelocAddr, (void*)BaseAddr, Size);
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
return Response::OIL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ObjectIntrospection
|