Codegen review changes (#18)

Co-authored-by: Jon Haslam <jonhaslam@meta.com>
This commit is contained in:
Jon Haslam 2023-01-04 17:54:07 +00:00 committed by GitHub
parent 1c3ee5bf6b
commit 3f2ae5e56f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 264 additions and 273 deletions

File diff suppressed because it is too large Load Diff

View File

@ -38,7 +38,7 @@ extern "C" {
namespace fs = std::filesystem; namespace fs = std::filesystem;
struct ParentMember { struct ParentMember {
struct drgn_type *type; drgn_type *type;
uint64_t bit_offset; uint64_t bit_offset;
bool operator<(const ParentMember &parent) const { bool operator<(const ParentMember &parent) const {
@ -89,84 +89,77 @@ class OICodeGen {
// TODO: remove me once all the callsites are gone // TODO: remove me once all the callsites are gone
static void initializeCodeGen(); static void initializeCodeGen();
struct drgn_qualified_type getRootType(); drgn_qualified_type getRootType();
void setRootType(struct drgn_qualified_type rt); void setRootType(drgn_qualified_type rt);
void setLinkageName(std::string name) { void setLinkageName(std::string name) {
linkageName = name; linkageName = name;
}; };
TypeHierarchy getTypeHierarchy(); TypeHierarchy getTypeHierarchy();
std::map<std::string, PaddingInfo> getPaddingInfo(); std::map<std::string, PaddingInfo> getPaddingInfo();
static void getDrgnArrayElementType(struct drgn_type *type, static void getDrgnArrayElementType(drgn_type *type, drgn_type **outElemType,
struct drgn_type **outElemType,
size_t &outNumElems); size_t &outNumElems);
bool isContainer(struct drgn_type *type); bool isContainer(drgn_type *type);
static struct drgn_type *drgnUnderlyingType(struct drgn_type *type); static drgn_type *drgnUnderlyingType(drgn_type *type);
bool buildName(struct drgn_type *type, std::string &text, bool buildName(drgn_type *type, std::string &text, std::string &outName);
std::string &outName);
std::string typeToTransformedName(struct drgn_type *type); std::string typeToTransformedName(drgn_type *type);
static std::string typeToName(struct drgn_type *type); static std::string typeToName(drgn_type *type);
bool enumerateTypesRecurse(struct drgn_type *type); bool enumerateTypesRecurse(drgn_type *type);
static std::string_view drgnKindStr(struct drgn_type *type); static std::string_view drgnKindStr(drgn_type *type);
std::set<struct drgn_type *> processedTypes; std::set<drgn_type *> processedTypes;
private: private:
Config config{}; Config config{};
FuncGen funcGen; FuncGen funcGen;
using ContainerTypeMap = using ContainerTypeMap =
std::pair<ContainerInfo, std::vector<struct drgn_qualified_type>>; std::pair<ContainerInfo, std::vector<drgn_qualified_type>>;
using TemplateParamList = using TemplateParamList =
std::vector<std::pair<struct drgn_qualified_type, std::string>>; std::vector<std::pair<drgn_qualified_type, std::string>>;
using SortedTypeDefMap = using SortedTypeDefMap = std::vector<std::pair<drgn_type *, drgn_type *>>;
std::vector<std::pair<struct drgn_type *, struct drgn_type *>>;
std::string rootTypeStr; std::string rootTypeStr;
std::string linkageName; std::string linkageName;
std::map<struct drgn_type *, std::string> unnamedUnion; std::map<drgn_type *, std::string> unnamedUnion;
std::map<std::string, size_t> sizeMap; std::map<std::string, size_t> sizeMap;
std::map<struct drgn_type *, ContainerTypeMap> containerTypeMapDrgn; std::map<drgn_type *, ContainerTypeMap> containerTypeMapDrgn;
std::vector<std::unique_ptr<ContainerInfo>> containerInfoList; std::vector<std::unique_ptr<ContainerInfo>> containerInfoList;
std::vector<struct drgn_type *> enumTypes; std::vector<drgn_type *> enumTypes;
std::vector<struct drgn_type *> typePath; std::vector<drgn_type *> typePath;
std::vector<std::string> knownTypes; std::vector<std::string> knownTypes;
struct drgn_qualified_type rootType; drgn_qualified_type rootType;
struct drgn_qualified_type rootTypeToIntrospect; drgn_qualified_type rootTypeToIntrospect;
std::map<std::string, std::string> typedefMap; std::map<std::string, std::string> typedefMap;
std::map<struct drgn_type *, std::vector<ParentMember>> parentClasses; std::map<drgn_type *, std::vector<ParentMember>> parentClasses;
size_t pad_index = 0; size_t pad_index = 0;
std::unordered_map<struct drgn_type *, std::pair<size_t, size_t>> std::unordered_map<drgn_type *, std::pair<size_t, size_t>> paddingIndexMap;
paddingIndexMap;
std::map<struct drgn_type *, struct drgn_type *> typedefTypes; std::map<drgn_type *, drgn_type *> typedefTypes;
std::map<struct drgn_type *, std::vector<DrgnClassMemberInfo>> std::map<drgn_type *, std::vector<DrgnClassMemberInfo>> classMembersMap;
classMembersMap; std::map<drgn_type *, std::vector<DrgnClassMemberInfo>> classMembersMapCopy;
std::map<struct drgn_type *, std::vector<DrgnClassMemberInfo>> std::map<drgn_type *, std::string> typeToNameMap;
classMembersMapCopy; std::map<std::string, drgn_type *> nameToTypeMap;
std::map<struct drgn_type *, std::string> typeToNameMap; std::set<drgn_type *> funcDefTypeList;
std::map<std::string, struct drgn_type *> nameToTypeMap; std::vector<drgn_type *> structDefType;
std::set<struct drgn_type *> funcDefTypeList; std::set<drgn_type *> knownDummyTypeList;
std::vector<struct drgn_type *> structDefType; std::map<drgn_type *, drgn_type *> pointerToTypeMap;
std::set<struct drgn_type *> knownDummyTypeList; std::set<drgn_type *> thriftIssetStructTypes;
std::map<struct drgn_type *, struct drgn_type *> pointerToTypeMap; std::vector<drgn_type *> topoSortedStructTypes;
std::set<struct drgn_type *> thriftIssetStructTypes;
std::vector<struct drgn_type *> topoSortedStructTypes;
std::set<ContainerInfo> containerTypesFuncDef; std::set<ContainerInfo> containerTypesFuncDef;
std::map<std::string, PaddingInfo> paddedStructs; std::map<std::string, PaddingInfo> paddedStructs;
std::map<struct drgn_type *, std::vector<DrgnClassMemberInfo>> std::map<drgn_type *, std::vector<DrgnClassMemberInfo>> &getClassMembersMap();
&getClassMembersMap();
class DrgnString { class DrgnString {
struct FreeDeleter { struct FreeDeleter {
@ -193,54 +186,52 @@ class OICodeGen {
const std::string &fullyQualifiedname); const std::string &fullyQualifiedname);
static void removeTemplateParamAtIndex(std::vector<std::string> &params, static void removeTemplateParamAtIndex(std::vector<std::string> &params,
const size_t index); const size_t index);
std::unordered_map<struct drgn_type *, DrgnString> fullyQualifiedNames; std::unordered_map<drgn_type *, DrgnString> fullyQualifiedNames;
std::optional<const std::string_view> fullyQualifiedName( std::optional<const std::string_view> fullyQualifiedName(drgn_type *type);
struct drgn_type *type);
static SortedTypeDefMap getSortedTypeDefMap( static SortedTypeDefMap getSortedTypeDefMap(
const std::map<struct drgn_type *, struct drgn_type *> &typedefTypeMap); const std::map<drgn_type *, drgn_type *> &typedefTypeMap);
std::optional<ContainerInfo> getContainerInfo(struct drgn_type *type); std::optional<ContainerInfo> getContainerInfo(drgn_type *type);
void printAllTypes(); void printAllTypes();
void printAllTypeNames(); void printAllTypeNames();
void printTypePath(); void printTypePath();
static void addPaddingForBaseClass(struct drgn_type *type, static void addPaddingForBaseClass(drgn_type *type,
std::vector<std::string> &def); std::vector<std::string> &def);
void addTypeToName(struct drgn_type *type, std::string name); void addTypeToName(drgn_type *type, std::string name);
bool generateNamesForTypes(); bool generateNamesForTypes();
bool generateJitCode(std::string &code); bool generateJitCode(std::string &code);
bool generateStructDefs(std::string &code); bool generateStructDefs(std::string &code);
bool generateStructDef(struct drgn_type *e, std::string &code); bool generateStructDef(drgn_type *e, std::string &code);
bool getDrgnTypeName(struct drgn_type *type, std::string &outName); bool getDrgnTypeName(drgn_type *type, std::string &outName);
bool getDrgnTypeNameInt(struct drgn_type *type, std::string &outName); bool getDrgnTypeNameInt(drgn_type *type, std::string &outName);
bool populateDefsAndDecls(); bool populateDefsAndDecls();
static void memberTransformName( static void memberTransformName(
std::map<std::string, std::string> &templateTransformMap, std::map<std::string, std::string> &templateTransformMap,
std::string &typeName); std::string &typeName);
bool getMemberDefinition(struct drgn_type *type); bool getMemberDefinition(drgn_type *type);
bool isKnownType(const std::string &type); bool isKnownType(const std::string &type);
bool isKnownType(const std::string &type, std::string &matched); bool isKnownType(const std::string &type, std::string &matched);
static bool getTemplateParams( static bool getTemplateParams(
struct drgn_type *type, size_t numTemplateParams, drgn_type *type, size_t numTemplateParams,
std::vector<std::pair<struct drgn_qualified_type, std::string>> &v); std::vector<std::pair<drgn_qualified_type, std::string>> &v);
bool enumerateTemplateParamIdxs(struct drgn_type *type, bool enumerateTemplateParamIdxs(drgn_type *type,
const ContainerInfo &containerInfo, const ContainerInfo &containerInfo,
const std::vector<size_t> &paramIdxs, const std::vector<size_t> &paramIdxs,
bool &ifStub); bool &ifStub);
bool getContainerTemplateParams(struct drgn_type *type, bool &ifStub); bool getContainerTemplateParams(drgn_type *type, bool &ifStub);
void getFuncDefinitionStr(std::string &code, struct drgn_type *type, void getFuncDefinitionStr(std::string &code, drgn_type *type,
const std::string &typeName); const std::string &typeName);
std::optional<uint64_t> getDrgnTypeSize(struct drgn_type *type); std::optional<uint64_t> getDrgnTypeSize(drgn_type *type);
std::optional<std::string> getNameForType(struct drgn_type *type); std::optional<std::string> getNameForType(drgn_type *type);
static std::string preProcessUniquePtr(struct drgn_type *type, static std::string preProcessUniquePtr(drgn_type *type, std::string name);
std::string name); std::string transformTypeName(drgn_type *type, std::string &text);
std::string transformTypeName(struct drgn_type *type, std::string &text);
static std::string templateTransformType(const std::string &typeName); static std::string templateTransformType(const std::string &typeName);
static std::string structNameTransformType(const std::string &typeName); static std::string structNameTransformType(const std::string &typeName);
@ -252,81 +243,76 @@ class OICodeGen {
const DrgnClassMemberInfo &m, const DrgnClassMemberInfo &m,
std::unordered_map<std::string, int> &memberNames, std::unordered_map<std::string, int> &memberNames,
uint64_t currOffsetBits, std::string &code, bool isInUnion); uint64_t currOffsetBits, std::string &code, bool isInUnion);
bool generateParent(struct drgn_type *p, bool generateParent(drgn_type *p,
std::unordered_map<std::string, int> &memberNames, std::unordered_map<std::string, int> &memberNames,
uint64_t &currOffsetBits, std::string &code, uint64_t &currOffsetBits, std::string &code,
size_t offsetToNextMember); size_t offsetToNextMember);
std::optional<uint64_t> getAlignmentRequirements(struct drgn_type *e); std::optional<uint64_t> getAlignmentRequirements(drgn_type *e);
bool generateStructMembers(struct drgn_type *e, bool generateStructMembers(drgn_type *e,
std::unordered_map<std::string, int> &memberNames, std::unordered_map<std::string, int> &memberNames,
std::string &code, uint64_t &out_offset_bits, std::string &code, uint64_t &out_offset_bits,
PaddingInfo &paddingInfo, PaddingInfo &paddingInfo,
bool &violatesAlignmentRequirement, bool &violatesAlignmentRequirement,
size_t offsetToNextMember); size_t offsetToNextMember);
void getFuncDefClassMembers(std::string &code, struct drgn_type *type, void getFuncDefClassMembers(std::string &code, drgn_type *type,
std::unordered_map<std::string, int> &memberNames, std::unordered_map<std::string, int> &memberNames,
bool skipPadding = false); bool skipPadding = false);
bool isDrgnSizeComplete(struct drgn_type *type); bool isDrgnSizeComplete(drgn_type *type);
static bool getEnumUnderlyingTypeStr(struct drgn_type *e, static bool getEnumUnderlyingTypeStr(drgn_type *e,
std::string &enumUnderlyingTypeStr); std::string &enumUnderlyingTypeStr);
bool ifEnumerateClass(const std::string &typeName); bool ifEnumerateClass(const std::string &typeName);
bool enumerateClassParents(struct drgn_type *type, bool enumerateClassParents(drgn_type *type, const std::string &typeName);
const std::string &typeName); bool enumerateClassMembers(drgn_type *type, const std::string &typeName,
bool enumerateClassMembers(struct drgn_type *type, bool &isStubbed);
const std::string &typeName, bool &isStubbed); bool enumerateClassTemplateParams(drgn_type *type,
bool enumerateClassTemplateParams(struct drgn_type *type,
const std::string &typeName, const std::string &typeName,
bool &isStubbed); bool &isStubbed);
bool ifGenerateMemberDefinition(const std::string &typeName); bool ifGenerateMemberDefinition(const std::string &typeName);
bool generateMemberDefinition(struct drgn_type *type, std::string &typeName); bool generateMemberDefinition(drgn_type *type, std::string &typeName);
std::optional<std::pair<std::string_view, std::string_view>> isMemberToStub( std::optional<std::pair<std::string_view, std::string_view>> isMemberToStub(
const std::string &type, const std::string &member); const std::string &type, const std::string &member);
std::optional<std::string_view> isTypeToStub(const std::string &typeName); std::optional<std::string_view> isTypeToStub(const std::string &typeName);
bool isTypeToStub(struct drgn_type *type, const std::string &typeName); bool isTypeToStub(drgn_type *type, const std::string &typeName);
bool isEmptyClassOrFunctionType(struct drgn_type *type, bool isEmptyClassOrFunctionType(drgn_type *type, const std::string &typeName);
const std::string &typeName); bool enumerateClassType(drgn_type *type);
bool enumerateClassType(struct drgn_type *type); bool enumerateTypeDefType(drgn_type *type);
bool enumerateTypeDefType(struct drgn_type *type); bool enumerateEnumType(drgn_type *type);
bool enumerateEnumType(struct drgn_type *type); bool enumeratePointerType(drgn_type *type);
bool enumeratePointerType(struct drgn_type *type); bool enumeratePrimitiveType(drgn_type *type);
bool enumeratePrimitiveType(struct drgn_type *type); bool enumerateArrayType(drgn_type *type);
bool enumerateArrayType(struct drgn_type *type);
bool isUnnamedStruct(struct drgn_type *type); bool isUnnamedStruct(drgn_type *type);
std::string getAnonName(struct drgn_type *, const char *); std::string getAnonName(drgn_type *, const char *);
std::string getStructName(struct drgn_type *type) { std::string getStructName(drgn_type *type) {
return getAnonName(type, "__anon_struct_"); return getAnonName(type, "__anon_struct_");
} }
std::string getUnionName(struct drgn_type *type) { std::string getUnionName(drgn_type *type) {
return getAnonName(type, "__anon_union_"); return getAnonName(type, "__anon_union_");
} }
static void declareThriftStruct(std::string &code, std::string_view name); static void declareThriftStruct(std::string &code, std::string_view name);
bool isNumMemberGreaterThanZero(struct drgn_type *type); bool isNumMemberGreaterThanZero(drgn_type *type);
void getClassMembersIncludingParent(struct drgn_type *type, void getClassMembersIncludingParent(drgn_type *type,
std::vector<DrgnClassMemberInfo> &out); std::vector<DrgnClassMemberInfo> &out);
bool staticAssertMemberOffsets( bool staticAssertMemberOffsets(
const std::string &struct_name, struct drgn_type *struct_type, const std::string &struct_name, drgn_type *struct_type,
std::string &assert_str, std::string &assert_str,
std::unordered_map<std::string, int> &member_names, std::unordered_map<std::string, int> &member_names,
uint64_t base_offset = 0); uint64_t base_offset = 0);
bool addStaticAssertsForType(struct drgn_type *type, bool addStaticAssertsForType(drgn_type *type, bool generateAssertsForOffsets,
bool generateAssertsForOffsets,
std::string &code); std::string &code);
bool buildNameInt(struct drgn_type *type, std::string &nameWithoutTemplate, bool buildNameInt(drgn_type *type, std::string &nameWithoutTemplate,
std::string &outName); std::string &outName);
void replaceTemplateOperator( void replaceTemplateOperator(
std::vector<std::pair<struct drgn_qualified_type, std::string>> std::vector<std::pair<drgn_qualified_type, std::string>> &template_params,
&template_params,
std::vector<std::string> &template_params_strings, size_t index); std::vector<std::string> &template_params_strings, size_t index);
void replaceTemplateParameters( void replaceTemplateParameters(
struct drgn_type *type, drgn_type *type,
std::vector<std::pair<struct drgn_qualified_type, std::string>> std::vector<std::pair<drgn_qualified_type, std::string>> &template_params,
&template_params,
std::vector<std::string> &template_params_strings, std::vector<std::string> &template_params_strings,
const std::string &nameWithoutTemplate); const std::string &nameWithoutTemplate);
}; };