From 6e87f9b3bb544587f270c029f6ec5f971c5fe3a7 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 14 Nov 2024 16:00:44 +0100 Subject: tools/syz-declextract: refactor int type conversion Allow to specialize "intptr". Pass isSyscallParam to int*Subtype b/c if they need to return const/flags, they will need isSyscallParam. Move makeConst/Flags/etc to before int*Subtype b/c they may need to use them. --- tools/syz-declextract/syz-declextract.cpp | 125 +++++++++++++++--------------- 1 file changed, 63 insertions(+), 62 deletions(-) (limited to 'tools') diff --git a/tools/syz-declextract/syz-declextract.cpp b/tools/syz-declextract/syz-declextract.cpp index 4dd73f2e4..bc5665dec 100644 --- a/tools/syz-declextract/syz-declextract.cpp +++ b/tools/syz-declextract/syz-declextract.cpp @@ -150,16 +150,51 @@ bool beginsWith(const std::string_view &str, const std::string_view begin) { bool contains(const std::string_view &str, const std::string_view sub) { return str.find(sub) != std::string::npos; } -std::string int8Subtype(const std::string &name) { return "int8"; } +std::string makeArray(const std::string &type, const size_t min = 0, const size_t max = -1) { + if (max != size_t(-1)) { + return "array[" + type + ", " + std::to_string(min) + ":" + std::to_string(max) + "]"; + } + if (min == 1) { + return type; + } + if (min) { + return "array[" + type + ", " + std::to_string(min) + "]"; + } + return "array[" + type + "]"; +} + +std::string makePtr(const std::string &dir, const std::string &type, bool isOpt = false) { + std::string ptr = "ptr[" + dir + ", " + type; + if (isOpt) { + return ptr + ", opt]"; + } + return ptr + "]"; +} -std::string int16Subtype(const std::string &name) { +std::string makeConst(bool isSyscallArg, const std::string &type, const std::string &val) { + if (isSyscallArg) { + return "const[" + val + "]"; + } + return "const[" + val + ", " + type + "]"; +} + +std::string makeFlags(bool isSyscallArg, const std::string &type, const std::string &flags) { + if (isSyscallArg) { + return "flags[" + flags + "]"; + } + return "flags[" + flags + ", " + type + "]"; +} + +std::string int8Subtype(const std::string &name, const bool isSyscallParam) { return "int8"; } + +std::string int16Subtype(const std::string &name, const bool isSyscallParam) { if (contains(name, "port")) { return "sock_port"; } return "int16"; } -std::string int32Subtype(const std::string &name) { +std::string int32Subtype(const std::string &name, const bool isSyscallParam) { if (contains(name, "ipv4")) { return "ipv4_addr"; } @@ -190,6 +225,10 @@ std::string int32Subtype(const std::string &name) { return "int32"; } +std::string int64Subtype(const std::string &name, const bool isSyscallParam) { return "int64"; } + +std::string intptrSubtype(const std::string &name, const bool isSyscallParam) { return "intptr"; } + std::string stringSubtype(const std::string &name, const char *defaultName = "string") { if (contains(name, "ifname") || endsWith(name, "dev_name")) { return "devname"; @@ -201,43 +240,6 @@ std::string stringSubtype(const std::string &name, const char *defaultName = "st return defaultName; } -std::string int64Subtype(const std::string &name) { return "int64"; } - -std::string makeArray(const std::string &type, const size_t min = 0, const size_t max = -1) { - if (max != size_t(-1)) { - return "array[" + type + ", " + std::to_string(min) + ":" + std::to_string(max) + "]"; - } - if (min == 1) { - return type; - } - if (min) { - return "array[" + type + ", " + std::to_string(min) + "]"; - } - return "array[" + type + "]"; -} - -std::string makePtr(const std::string &dir, const std::string &type, bool isOpt = false) { - std::string ptr = "ptr[" + dir + ", " + type; - if (isOpt) { - return ptr + ", opt]"; - } - return ptr + "]"; -} - -std::string makeConst(bool isSyscallArg, const std::string &type, const std::string &val) { - if (isSyscallArg) { - return "const[" + val + "]"; - } - return "const[" + val + ", " + type + "]"; -} - -std::string makeFlags(bool isSyscallArg, const std::string &type, const std::string &flags) { - if (isSyscallArg) { - return "flags[" + flags + "]"; - } - return "flags[" + flags + ", " + type + "]"; -} - enum IntType { INVALID_INT = 0, INT_8 = 1, @@ -268,44 +270,43 @@ IntType getIntType(const std::string &ctype, const bool isSyscallParam) { exit(1); } -const std::string intNCommonSubtype(const std::string &name, const IntType len) { - const auto &byteLen = std::to_string(len * 8); - if (endsWith(name, "enabled") || endsWith(name, "enable")) { - return "bool" + byteLen; - } - return "int" + byteLen; -} - -const std::string intNSubtype(const std::string &name, const IntType len) { +const std::string intNSubtype(const std::string &name, const IntType len, const bool isSyscallParam) { switch (len) { case INT_8: - return int8Subtype(name); + return int8Subtype(name, isSyscallParam); case INT_16: - return int16Subtype(name); + return int16Subtype(name, isSyscallParam); case INT_32: - return int32Subtype(name); + return int32Subtype(name, isSyscallParam); case INT_64: - return int64Subtype(name); + return int64Subtype(name, isSyscallParam); + case INT_PTR: + return intptrSubtype(name, isSyscallParam); default: fprintf(stderr, "invalid int type: %d\n", static_cast(len)); exit(1); } } -bool isIntN(const std::string &syztype) { - return !syztype.compare(0, 3, "int") && std::all_of(syztype.begin() + 3, syztype.end(), ::isDigit); +bool isIntN(const std::string &type) { + return (!type.compare(0, 3, "int") && std::all_of(type.begin() + 3, type.end(), ::isDigit)) || (type == "intptr"); } -const std::string intSubtype(const std::string &name, const IntType len) { +const std::string intSubtype(const std::string &name, const IntType len, const bool isSyscallParam = false) { if (len == INVALID_INT) { fprintf(stderr, "Invalid int type\n"); exit(1); - } else if (len == INT_PTR) { - return "intptr"; } - const std::string &subType = intNSubtype(name, len); - return isIntN(subType) ? intNCommonSubtype(name, len) : subType; + const std::string subType = intNSubtype(name, len, isSyscallParam); + if (!isIntN(subType)) { + return subType; + } + if (endsWith(name, "enabled") || endsWith(name, "enable")) { + // Replace "int" with "bool". + return "bool" + subType.substr(3); + } + return subType; } const std::string getSyzType(const std::string &ctype, std::string name, const bool isSyscallParam, @@ -325,10 +326,10 @@ const std::string getSyzType(const std::string &ctype, std::string name, const b type += ":" + std::to_string(bitFieldWidth); } } else { - type = intSubtype(name, len); + type = intSubtype(name, len, isSyscallParam); } - if (isBitField || type == "intptr" || isIntN(type)) { + if (isBitField || isIntN(type)) { if (name.empty() || contains(name, "pad") || contains(name, "unused") || contains(name, "_reserved")) { return makeConst(isSyscallParam, type, "0"); } -- cgit mrf-deployment