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/OIGenerator.h"
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
2023-02-02 11:15:11 +00:00
|
|
|
#include <boost/core/demangle.hpp>
|
2022-12-19 14:37:51 +00:00
|
|
|
#include <fstream>
|
2023-03-07 17:45:00 +00:00
|
|
|
#include <iostream>
|
2023-02-02 11:15:11 +00:00
|
|
|
#include <unordered_map>
|
2022-12-19 14:37:51 +00:00
|
|
|
#include <variant>
|
|
|
|
|
2023-08-29 15:08:43 +01:00
|
|
|
#include "oi/CodeGen.h"
|
2023-04-26 16:20:53 +01:00
|
|
|
#include "oi/DrgnUtils.h"
|
2023-05-18 14:00:10 +01:00
|
|
|
#include "oi/Headers.h"
|
2023-04-26 16:20:53 +01:00
|
|
|
#include "oi/OIUtils.h"
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-07-26 15:31:53 +01:00
|
|
|
namespace oi::detail {
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-02-02 11:15:11 +00:00
|
|
|
std::unordered_map<std::string, std::string>
|
|
|
|
OIGenerator::oilStrongToWeakSymbolsMap(drgnplusplus::program& prog) {
|
|
|
|
static constexpr std::string_view strongSymbolPrefix =
|
2023-08-29 15:08:43 +01:00
|
|
|
"oi::IntrospectionResult oi::introspect<";
|
2023-02-02 11:15:11 +00:00
|
|
|
static constexpr std::string_view weakSymbolPrefix =
|
2023-08-29 15:08:43 +01:00
|
|
|
"oi::IntrospectionResult oi::introspectImpl<";
|
2023-02-02 11:15:11 +00:00
|
|
|
|
|
|
|
std::unordered_map<std::string, std::pair<std::string, std::string>>
|
|
|
|
templateArgsToSymbolsMap;
|
|
|
|
|
2023-05-19 16:18:04 +01:00
|
|
|
auto symbols = prog.find_all_symbols();
|
2023-02-02 11:15:11 +00:00
|
|
|
for (drgn_symbol* sym : *symbols) {
|
|
|
|
auto symName = drgnplusplus::symbol::name(sym);
|
|
|
|
auto demangled = boost::core::demangle(symName);
|
|
|
|
|
|
|
|
if (demangled.starts_with(strongSymbolPrefix)) {
|
|
|
|
auto& matchedSyms = templateArgsToSymbolsMap[demangled.substr(
|
|
|
|
strongSymbolPrefix.length())];
|
|
|
|
if (!matchedSyms.first.empty()) {
|
|
|
|
LOG(WARNING) << "non-unique symbols found: `" << matchedSyms.first
|
2023-08-29 15:08:43 +01:00
|
|
|
<< "` and `" << symName << '`';
|
2023-02-02 11:15:11 +00:00
|
|
|
}
|
|
|
|
matchedSyms.first = symName;
|
|
|
|
} else if (demangled.starts_with(weakSymbolPrefix)) {
|
|
|
|
auto& matchedSyms =
|
|
|
|
templateArgsToSymbolsMap[demangled.substr(weakSymbolPrefix.length())];
|
|
|
|
if (!matchedSyms.second.empty()) {
|
2023-08-29 15:08:43 +01:00
|
|
|
LOG(WARNING) << "non-unique symbols found: `" << matchedSyms.second
|
2023-02-02 11:15:11 +00:00
|
|
|
<< "` and `" << symName << "`";
|
|
|
|
}
|
|
|
|
matchedSyms.second = symName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::string> strongToWeakSymbols;
|
|
|
|
for (auto& [_, val] : templateArgsToSymbolsMap) {
|
|
|
|
if (val.first.empty() || val.second.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strongToWeakSymbols[std::move(val.first)] = std::move(val.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return strongToWeakSymbols;
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:08:43 +01:00
|
|
|
std::unordered_map<std::string, drgn_qualified_type>
|
2022-12-19 14:37:51 +00:00
|
|
|
OIGenerator::findOilTypesAndNames(drgnplusplus::program& prog) {
|
2023-02-02 11:15:11 +00:00
|
|
|
auto strongToWeakSymbols = oilStrongToWeakSymbolsMap(prog);
|
|
|
|
|
2023-08-29 15:08:43 +01:00
|
|
|
std::unordered_map<std::string, drgn_qualified_type> out;
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-08-29 15:08:43 +01:00
|
|
|
for (drgn_qualified_type& func : drgnplusplus::func_iterator(prog)) {
|
2023-02-02 11:15:11 +00:00
|
|
|
std::string strongLinkageName;
|
2022-12-19 14:37:51 +00:00
|
|
|
{
|
2023-04-03 16:44:07 +01:00
|
|
|
const char* linkageNameCstr;
|
2023-02-02 11:15:11 +00:00
|
|
|
if (auto err = drgnplusplus::error(
|
|
|
|
drgn_type_linkage_name(func.type, &linkageNameCstr))) {
|
|
|
|
// throw err;
|
|
|
|
continue;
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
2023-02-02 11:15:11 +00:00
|
|
|
strongLinkageName = linkageNameCstr;
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 11:15:11 +00:00
|
|
|
std::string weakLinkageName;
|
|
|
|
if (auto search = strongToWeakSymbols.find(strongLinkageName);
|
|
|
|
search != strongToWeakSymbols.end()) {
|
|
|
|
weakLinkageName = search->second;
|
|
|
|
} else {
|
|
|
|
continue; // not an oil strong symbol
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 15:08:43 +01:00
|
|
|
// IntrospectionResult (*)(const T&)
|
|
|
|
CHECK(drgn_type_has_parameters(func.type)) << "functions have parameters";
|
|
|
|
CHECK(drgn_type_num_parameters(func.type) == 1)
|
|
|
|
<< "introspection func has one parameter";
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-08-29 15:08:43 +01:00
|
|
|
auto* params = drgn_type_parameters(func.type);
|
|
|
|
drgn_qualified_type tType;
|
|
|
|
if (auto err =
|
|
|
|
drgnplusplus::error(drgn_parameter_type(¶ms[0], &tType))) {
|
2022-12-19 14:37:51 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:08:43 +01:00
|
|
|
if (drgn_type_has_name(tType.type)) {
|
|
|
|
LOG(INFO) << "found OIL type: " << drgn_type_name(tType.type);
|
2023-02-22 18:33:18 +00:00
|
|
|
} else {
|
|
|
|
LOG(INFO) << "found OIL type: (no name)";
|
|
|
|
}
|
2023-08-29 15:08:43 +01:00
|
|
|
out.emplace(std::move(weakLinkageName), tType);
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-03-07 17:45:00 +00:00
|
|
|
fs::path OIGenerator::generateForType(const OICodeGen::Config& generatorConfig,
|
|
|
|
const OICompiler::Config& compilerConfig,
|
|
|
|
const drgn_qualified_type& type,
|
|
|
|
const std::string& linkageName,
|
|
|
|
SymbolService& symbols) {
|
2023-08-29 15:08:43 +01:00
|
|
|
CodeGen codegen{generatorConfig, symbols};
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-08-29 15:08:43 +01:00
|
|
|
std::string code;
|
|
|
|
if (!codegen.codegenFromDrgn(type.type, linkageName, code)) {
|
|
|
|
LOG(ERROR) << "codegen failed!";
|
2023-03-07 17:45:00 +00:00
|
|
|
return {};
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string sourcePath = sourceFileDumpPath;
|
|
|
|
if (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";
|
|
|
|
} else {
|
|
|
|
std::ofstream outputFile(sourcePath);
|
|
|
|
outputFile << code;
|
|
|
|
}
|
|
|
|
|
|
|
|
OICompiler compiler{{}, compilerConfig};
|
2023-03-07 17:45:00 +00:00
|
|
|
|
|
|
|
// TODO: Revert to outputPath and remove printing when typegraph is done.
|
|
|
|
fs::path tmpObject = outputPath;
|
2023-09-15 19:26:51 +01:00
|
|
|
tmpObject.replace_extension(
|
|
|
|
"." + std::to_string(std::hash<std::string>{}(linkageName)) + ".o");
|
2023-03-07 17:45:00 +00:00
|
|
|
|
|
|
|
if (!compiler.compile(code, sourcePath, tmpObject)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return tmpObject;
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 14:37:16 +00:00
|
|
|
int OIGenerator::generate(fs::path& primaryObject, SymbolService& symbols) {
|
2022-12-19 14:37:51 +00:00
|
|
|
drgnplusplus::program prog;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::array<const char*, 1> objectPaths = {{primaryObject.c_str()}};
|
|
|
|
if (auto err = drgnplusplus::error(drgn_program_load_debug_info(
|
|
|
|
prog.get(), std::data(objectPaths), std::size(objectPaths), false,
|
|
|
|
false))) {
|
|
|
|
LOG(ERROR) << "error loading debug info program: " << err;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:08:43 +01:00
|
|
|
auto oilTypes = findOilTypesAndNames(prog);
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-04-17 19:13:59 +01:00
|
|
|
std::map<Feature, bool> featuresMap = {
|
2023-08-29 15:08:43 +01:00
|
|
|
{Feature::TypeGraph, true},
|
|
|
|
{Feature::TypedDataSegment, true},
|
|
|
|
{Feature::TreeBuilderTypeChecking, true},
|
|
|
|
{Feature::TreeBuilderV2, true},
|
|
|
|
{Feature::Library, true},
|
2023-04-17 19:13:59 +01:00
|
|
|
{Feature::PackStructs, true},
|
2023-07-25 18:02:53 +01:00
|
|
|
{Feature::PruneTypeGraph, true},
|
2023-04-17 19:13:59 +01:00
|
|
|
};
|
|
|
|
|
2022-12-19 14:37:51 +00:00
|
|
|
OICodeGen::Config generatorConfig{};
|
|
|
|
OICompiler::Config compilerConfig{};
|
2023-09-14 11:40:11 +01:00
|
|
|
compilerConfig.usePIC = pic;
|
2023-04-17 19:13:59 +01:00
|
|
|
|
2023-07-26 15:31:53 +01:00
|
|
|
auto features = utils::processConfigFile(configFilePath, featuresMap,
|
|
|
|
compilerConfig, generatorConfig);
|
2023-04-17 19:13:59 +01:00
|
|
|
if (!features) {
|
2022-12-19 14:37:51 +00:00
|
|
|
LOG(ERROR) << "failed to process config file";
|
|
|
|
return -1;
|
|
|
|
}
|
2023-04-17 19:13:59 +01:00
|
|
|
generatorConfig.features = *features;
|
2023-08-29 15:08:43 +01:00
|
|
|
compilerConfig.features = *features;
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
size_t failures = 0;
|
2023-08-29 15:08:43 +01:00
|
|
|
for (const auto& [linkageName, type] : oilTypes) {
|
2023-03-07 17:45:00 +00:00
|
|
|
if (auto obj = generateForType(generatorConfig, compilerConfig, type,
|
|
|
|
linkageName, symbols);
|
|
|
|
!obj.empty()) {
|
|
|
|
std::cout << obj.string() << std::endl;
|
|
|
|
} else {
|
2022-12-19 14:37:51 +00:00
|
|
|
LOG(WARNING) << "failed to generate for symbol `" << linkageName
|
|
|
|
<< "`. this is non-fatal but the call will not work.";
|
|
|
|
failures++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t successes = oilTypes.size() - failures;
|
|
|
|
LOG(INFO) << "object introspection generation complete. " << successes
|
|
|
|
<< " successes and " << failures << " failures.";
|
2023-01-25 15:27:33 +00:00
|
|
|
|
|
|
|
if (failures > 0 || (failIfNothingGenerated && successes == 0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:31:53 +01:00
|
|
|
} // namespace oi::detail
|