aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2024-11-14 16:00:44 +0100
committerDmitry Vyukov <dvyukov@google.com>2024-11-14 17:05:16 +0000
commit6e87f9b3bb544587f270c029f6ec5f971c5fe3a7 (patch)
tree7be3878a1ad892de0a7c294a578ca8ae9f4c5126 /tools
parent33bffb43ead1bb279ee4dfd33fbf50e0281d7726 (diff)
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.
Diffstat (limited to 'tools')
-rw-r--r--tools/syz-declextract/syz-declextract.cpp125
1 files changed, 63 insertions, 62 deletions
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<int>(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");
}