From 2636452396544b740b156b275377a7ee5e2aae3e Mon Sep 17 00:00:00 2001 From: tiedaoxiaotubie <617021914@qq.com> Date: Mon, 13 Sep 2021 15:55:10 +0800 Subject: [PATCH 1/7] add strcmp() wrapper --- runtime/LibcWrappers.cpp | 25 +++++++++++++++++++++++++ runtime/test_wrappers/test_strcmp.c | 26 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 runtime/test_wrappers/test_strcmp.c diff --git a/runtime/LibcWrappers.cpp b/runtime/LibcWrappers.cpp index cfe55525..f9377d36 100644 --- a/runtime/LibcWrappers.cpp +++ b/runtime/LibcWrappers.cpp @@ -608,4 +608,29 @@ uint32_t SYM(ntohl)(uint32_t netlong) { return result; } + +int SYM(strcmp)(const char *a, const char *b) { + tryAlternative(a, _sym_get_parameter_expression(0), SYM(strcmp)); + tryAlternative(b, _sym_get_parameter_expression(1), SYM(strcmp)); + + auto result = strcmp(a, b); + _sym_set_return_expression(nullptr); + + if (isConcrete(a, strlen(a)) && isConcrete(b, strlen(b))) + return result; + + auto aShadowIt = ReadOnlyShadow(a, strlen(a)).begin_non_null(); + auto bShadowIt = ReadOnlyShadow(b, strlen(b)).begin_non_null(); + auto *allEqual = _sym_build_equal(*aShadowIt, *bShadowIt); + for (size_t i = 1; i < strlen(a); i++) { + ++aShadowIt; + ++bShadowIt; + allEqual = + _sym_build_bool_and(allEqual, _sym_build_equal(*aShadowIt, *bShadowIt)); + } + + _sym_push_path_constraint(allEqual, result == 0, + reinterpret_cast(SYM(strcmp))); + return result; +} } diff --git a/runtime/test_wrappers/test_strcmp.c b/runtime/test_wrappers/test_strcmp.c new file mode 100644 index 00000000..3ff4292d --- /dev/null +++ b/runtime/test_wrappers/test_strcmp.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) { + + char buf[1024]; + ssize_t i; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + buf[i] = 0; + if (buf[0] != 'A') return 0; + if (buf[1] != 'B') return 0; + if (buf[2] != 'C') return 0; + if (buf[3] != 'D') return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + if (strcmp(buf + 12, "AAAA") == 0) { + printf("HIT!\n"); + } else { + printf("NOT HIT!\n"); + } + + return 0; + +} From 6df681ffb63d31029f26a88bd5162066b2c531b4 Mon Sep 17 00:00:00 2001 From: tiedaoxiaotubie <617021914@qq.com> Date: Mon, 13 Sep 2021 15:56:36 +0800 Subject: [PATCH 2/7] add strncmp() wrapper --- runtime/LibcWrappers.cpp | 26 ++++++++++++++++++++++++++ runtime/test_wrappers/test_strncmp.c | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 runtime/test_wrappers/test_strncmp.c diff --git a/runtime/LibcWrappers.cpp b/runtime/LibcWrappers.cpp index f9377d36..2f6e9f90 100644 --- a/runtime/LibcWrappers.cpp +++ b/runtime/LibcWrappers.cpp @@ -633,4 +633,30 @@ int SYM(strcmp)(const char *a, const char *b) { reinterpret_cast(SYM(strcmp))); return result; } + +int SYM(strncmp)(const char *a, const char *b, size_t n) { + tryAlternative(a, _sym_get_parameter_expression(0), SYM(strncmp)); + tryAlternative(b, _sym_get_parameter_expression(1), SYM(strncmp)); + tryAlternative(n, _sym_get_parameter_expression(2), SYM(strncmp)); + + auto result = strncmp(a, b, n); + _sym_set_return_expression(nullptr); + + if (isConcrete(a, n) && isConcrete(b, n)) + return result; + + auto aShadowIt = ReadOnlyShadow(a, n).begin_non_null(); + auto bShadowIt = ReadOnlyShadow(b, n).begin_non_null(); + auto *allEqual = _sym_build_equal(*aShadowIt, *bShadowIt); + for (size_t i = 1; i < n; i++) { + ++aShadowIt; + ++bShadowIt; + allEqual = + _sym_build_bool_and(allEqual, _sym_build_equal(*aShadowIt, *bShadowIt)); + } + + _sym_push_path_constraint(allEqual, result == 0, + reinterpret_cast(SYM(strncmp))); + return result; +} } diff --git a/runtime/test_wrappers/test_strncmp.c b/runtime/test_wrappers/test_strncmp.c new file mode 100644 index 00000000..961f17ce --- /dev/null +++ b/runtime/test_wrappers/test_strncmp.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) { + + char buf[1024]; + ssize_t i; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + buf[i] = 0; + if (buf[0] != 'A') return 0; + if (buf[1] != 'B') return 0; + if (buf[2] != 'C') return 0; + if (buf[3] != 'D') return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + if (strncmp(buf + 12, "AAAA", 4) == 0){ + printf("HIT!\n"); + } else { + printf("NOT HIT!\n"); + } + + return 0; + +} From 91e33e29f6fb447c0566459a77f29209e42b23ed Mon Sep 17 00:00:00 2001 From: tiedaoxiaotubie <617021914@qq.com> Date: Mon, 13 Sep 2021 15:58:27 +0800 Subject: [PATCH 3/7] add test_strlen() wrapper --- runtime/LibcWrappers.cpp | 35 +++++++++++++++++++++++++++++ runtime/test_wrappers/test_strlen.c | 29 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 runtime/test_wrappers/test_strlen.c diff --git a/runtime/LibcWrappers.cpp b/runtime/LibcWrappers.cpp index 2f6e9f90..8b5a12c6 100644 --- a/runtime/LibcWrappers.cpp +++ b/runtime/LibcWrappers.cpp @@ -659,4 +659,39 @@ int SYM(strncmp)(const char *a, const char *b, size_t n) { reinterpret_cast(SYM(strncmp))); return result; } + +uint32_t SYM(strlen)(const char *s) { + tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen)); + + // HACK! we regard strlen as a special strchr(s, '\0') + auto *result = strchr(s, 0); + _sym_set_return_expression(nullptr); + + if (isConcrete(s, result != nullptr ? (result - s) : strlen(s))) + return (result - s); + + // We force set the value of c to \x00, it should be a concrete value + auto *cExpr = _sym_build_integer(0, 8); + + size_t length = result != nullptr ? (result - s) : strlen(s); + auto shadow = ReadOnlyShadow(s, length); + auto shadowIt = shadow.begin(); + for (size_t i = 0; i < length; i++) { + _sym_push_path_constraint( + _sym_build_not_equal( + (*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(s[i], 8), + cExpr), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + ++shadowIt; + } + + // HACK! The last byte must be \x00! + _sym_push_path_constraint( + _sym_build_equal( + (*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(0, 8), + cExpr), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + + return (result - s); +} } diff --git a/runtime/test_wrappers/test_strlen.c b/runtime/test_wrappers/test_strlen.c new file mode 100644 index 00000000..86fac6b2 --- /dev/null +++ b/runtime/test_wrappers/test_strlen.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) { + + char buf[1024]; + ssize_t i; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + buf[i] = 0; + if (buf[0] != 'A') return 0; + if (buf[1] != 'B') return 0; + if (buf[2] != 'C') return 0; + if (buf[3] != 'D') return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + if (strlen(buf + 12) == 5) { + printf("The string length after buf + 12 is: %lu\n", strlen(buf+12)); + printf("HIT!\n"); + } else { + printf("The string length after buf + 12 is: %lu\n", strlen(buf+12)); + printf("strchr(buf+12) is: %s\n", strchr(buf+12, '\0')); + printf("NOT HIT!\n"); + } + + return 0; + +} From bab79d2bde2b4d69a9b12c09bfd1e87666ddd45c Mon Sep 17 00:00:00 2001 From: tiedaoxiaotubie <617021914@qq.com> Date: Mon, 13 Sep 2021 16:04:27 +0800 Subject: [PATCH 4/7] add atoi() and atol() wrappers --- runtime/LibcWrappers.cpp | 164 ++++++++++++++++++++++++++++++ runtime/test_wrappers/test_atoi.c | 28 +++++ runtime/test_wrappers/test_atol.c | 28 +++++ 3 files changed, 220 insertions(+) create mode 100644 runtime/test_wrappers/test_atoi.c create mode 100644 runtime/test_wrappers/test_atol.c diff --git a/runtime/LibcWrappers.cpp b/runtime/LibcWrappers.cpp index 8b5a12c6..c00d81ed 100644 --- a/runtime/LibcWrappers.cpp +++ b/runtime/LibcWrappers.cpp @@ -694,4 +694,168 @@ uint32_t SYM(strlen)(const char *s) { return (result - s); } + +int SYM(atoi)(const char *s) { + tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen)); + + // HACK! we mimic the libc function summary in klee + /* int atoi(const char *str) { + * return (int)strtol(str, (char **)NULL, 10); + * } + * */ + auto result = strtol(s, (char **)NULL, 10); + _sym_set_return_expression(nullptr); + + if (isConcrete(s, strlen(s))) + return result; + + size_t length = strlen(s); + size_t num_len = 0; + for (size_t i = 0; i < length; i++) { + if ('0' <= (char)s[i] && (char)s[i] <= '9') { + num_len++; + } else { + break; + } + } + auto shadow = ReadOnlyShadow(s, length); + auto shadowIt = shadow.begin(); + for (size_t i = 0; i < num_len; i++) { + int res = (char)s[i]; + auto *cExpr = _sym_build_integer(res, 8); + _sym_push_path_constraint( + _sym_build_equal( + (*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(s[i], 8), + cExpr), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + ++shadowIt; + } + + // The value of tail must be non-num + auto *tailExpr_0 = _sym_build_integer(48, 8); // '0' + auto *tailExpr_1 = _sym_build_integer(49, 8); // '1' + auto *tailExpr_2 = _sym_build_integer(50, 8); // '2' + auto *tailExpr_3 = _sym_build_integer(51, 8); // '3' + auto *tailExpr_4 = _sym_build_integer(52, 8); // '4' + auto *tailExpr_5 = _sym_build_integer(53, 8); // '5' + auto *tailExpr_6 = _sym_build_integer(54, 8); // '6' + auto *tailExpr_7 = _sym_build_integer(55, 8); // '7' + auto *tailExpr_8 = _sym_build_integer(56, 8); // '8' + auto *tailExpr_9 = _sym_build_integer(57, 8); // '9' + + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_0), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_1), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_2), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_3), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_4), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_5), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_6), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_7), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_8), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_9), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + + return result; +} + +long int SYM(atol)(const char *s) { + tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen)); + + // HACK! we mimic the libc function summary in klee + /* int atoi(const char *str) { + * return (int)strtol(str, (char **)NULL, 10); + * } + * */ + auto result = strtol(s, (char **)NULL, 10); + _sym_set_return_expression(nullptr); + + if (isConcrete(s, strlen(s))) + return result; + + size_t length = strlen(s); + size_t num_len = 0; + for (size_t i = 0; i < length; i++) { + if ('0' <= (char)s[i] && (char)s[i] <= '9') { + num_len++; + } else { + break; + } + } + auto shadow = ReadOnlyShadow(s, length); + auto shadowIt = shadow.begin(); + for (size_t i = 0; i < num_len; i++) { + int res = (char)s[i]; + auto *cExpr = _sym_build_integer(res, 8); + _sym_push_path_constraint( + _sym_build_equal( + (*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(s[i], 8), + cExpr), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + ++shadowIt; + } + + // The value of tail must be non-num + auto *tailExpr_0 = _sym_build_integer(48, 8); // '0' + auto *tailExpr_1 = _sym_build_integer(49, 8); // '1' + auto *tailExpr_2 = _sym_build_integer(50, 8); // '2' + auto *tailExpr_3 = _sym_build_integer(51, 8); // '3' + auto *tailExpr_4 = _sym_build_integer(52, 8); // '4' + auto *tailExpr_5 = _sym_build_integer(53, 8); // '5' + auto *tailExpr_6 = _sym_build_integer(54, 8); // '6' + auto *tailExpr_7 = _sym_build_integer(55, 8); // '7' + auto *tailExpr_8 = _sym_build_integer(56, 8); // '8' + auto *tailExpr_9 = _sym_build_integer(57, 8); // '9' + + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_0), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_1), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_2), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_3), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_4), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_5), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_6), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_7), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_8), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint( + _sym_build_not_equal(*shadowIt, tailExpr_9), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + + return result; +} } diff --git a/runtime/test_wrappers/test_atoi.c b/runtime/test_wrappers/test_atoi.c new file mode 100644 index 00000000..b48bb947 --- /dev/null +++ b/runtime/test_wrappers/test_atoi.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) { + + char buf[1024]; + ssize_t i; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + buf[i] = 0; + if (buf[0] != 'A') return 0; + if (buf[1] != 'B') return 0; + if (buf[2] != 'C') return 0; + if (buf[3] != 'D') return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + if (atoi(buf + 12) == 678) { + printf("The result of atoi(buf+12) is: %lu\n", atoi(buf+12)); + printf("HIT!\n"); + } else { + printf("The result of atoi(buf+12) is: %lu\n", atoi(buf+12)); + printf("NOT HIT!\n"); + } + + return 0; + +} diff --git a/runtime/test_wrappers/test_atol.c b/runtime/test_wrappers/test_atol.c new file mode 100644 index 00000000..b397528e --- /dev/null +++ b/runtime/test_wrappers/test_atol.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) { + + char buf[1024]; + ssize_t i; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + buf[i] = 0; + if (buf[0] != 'A') return 0; + if (buf[1] != 'B') return 0; + if (buf[2] != 'C') return 0; + if (buf[3] != 'D') return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + if (atol(buf + 12) == 99999999) { + printf("The result of atoi(buf+12) is: %lu\n", atol(buf+12)); + printf("HIT!\n"); + } else { + printf("The result of atoi(buf+12) is: %lu\n", atol(buf+12)); + printf("NOT HIT!\n"); + } + + return 0; + +} From 496dac17aa935c259a89e07d021baa147c3e708f Mon Sep 17 00:00:00 2001 From: tiedaoxiaotubie <617021914@qq.com> Date: Mon, 13 Sep 2021 16:06:18 +0800 Subject: [PATCH 5/7] add strcpy() wrapper --- runtime/LibcWrappers.cpp | 19 ++++++++++++++++++ runtime/test_wrappers/test_strcpy.c | 31 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 runtime/test_wrappers/test_strcpy.c diff --git a/runtime/LibcWrappers.cpp b/runtime/LibcWrappers.cpp index c00d81ed..99993a05 100644 --- a/runtime/LibcWrappers.cpp +++ b/runtime/LibcWrappers.cpp @@ -858,4 +858,23 @@ long int SYM(atol)(const char *s) { return result; } + +char *SYM(strcpy)(char *dest, const char *src) { + tryAlternative(dest, _sym_get_parameter_expression(0), SYM(strcpy)); + tryAlternative(src, _sym_get_parameter_expression(1), SYM(strcpy)); + + auto *result = strcpy(dest, src); + _sym_set_return_expression(nullptr); + + size_t cpyLen = strlen(src); + if (isConcrete(src, cpyLen) && isConcrete(dest, cpyLen)) + return result; + + auto srcShadow = ReadOnlyShadow(src, cpyLen); + auto destShadow = ReadWriteShadow(dest, cpyLen); + + std::copy(srcShadow.begin(), srcShadow.end(), destShadow.begin()); + + return result; +} } diff --git a/runtime/test_wrappers/test_strcpy.c b/runtime/test_wrappers/test_strcpy.c new file mode 100644 index 00000000..53bffc13 --- /dev/null +++ b/runtime/test_wrappers/test_strcpy.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) { + + char buf[1024]; + ssize_t i; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + buf[i] = 0; + if (buf[0] != 'A') return 0; + if (buf[1] != 'B') return 0; + if (buf[2] != 'C') return 0; + if (buf[3] != 'D') return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + + char buf_2[4]; + strcpy(buf_2, buf+12); + if (memcmp(buf_2, "NICE", 4)){ + printf("NOT HIT!\n"); + return 0; + } else { + printf("HIT!\n"); + + } + + return 0; + +} From 99a7410a3854a1906abe62e5f5a9f4f727cc9b84 Mon Sep 17 00:00:00 2001 From: Sebastian Poeplau Date: Wed, 22 Feb 2023 11:37:22 +0000 Subject: [PATCH 6/7] Fix formatting of the new libc wrappers --- runtime/LibcWrappers.cpp | 453 +++++++++++++-------------- runtime/test_wrappers/test_atoi.c | 31 +- runtime/test_wrappers/test_atol.c | 31 +- runtime/test_wrappers/test_strcmp.c | 27 +- runtime/test_wrappers/test_strcpy.c | 32 +- runtime/test_wrappers/test_strlen.c | 33 +- runtime/test_wrappers/test_strncmp.c | 29 +- 7 files changed, 332 insertions(+), 304 deletions(-) diff --git a/runtime/LibcWrappers.cpp b/runtime/LibcWrappers.cpp index 99993a05..51e860f7 100644 --- a/runtime/LibcWrappers.cpp +++ b/runtime/LibcWrappers.cpp @@ -610,271 +610,270 @@ uint32_t SYM(ntohl)(uint32_t netlong) { } int SYM(strcmp)(const char *a, const char *b) { - tryAlternative(a, _sym_get_parameter_expression(0), SYM(strcmp)); - tryAlternative(b, _sym_get_parameter_expression(1), SYM(strcmp)); + tryAlternative(a, _sym_get_parameter_expression(0), SYM(strcmp)); + tryAlternative(b, _sym_get_parameter_expression(1), SYM(strcmp)); - auto result = strcmp(a, b); - _sym_set_return_expression(nullptr); - - if (isConcrete(a, strlen(a)) && isConcrete(b, strlen(b))) - return result; - - auto aShadowIt = ReadOnlyShadow(a, strlen(a)).begin_non_null(); - auto bShadowIt = ReadOnlyShadow(b, strlen(b)).begin_non_null(); - auto *allEqual = _sym_build_equal(*aShadowIt, *bShadowIt); - for (size_t i = 1; i < strlen(a); i++) { - ++aShadowIt; - ++bShadowIt; - allEqual = - _sym_build_bool_and(allEqual, _sym_build_equal(*aShadowIt, *bShadowIt)); - } + auto result = strcmp(a, b); + _sym_set_return_expression(nullptr); - _sym_push_path_constraint(allEqual, result == 0, - reinterpret_cast(SYM(strcmp))); + if (isConcrete(a, strlen(a)) && isConcrete(b, strlen(b))) return result; + + auto aShadowIt = ReadOnlyShadow(a, strlen(a)).begin_non_null(); + auto bShadowIt = ReadOnlyShadow(b, strlen(b)).begin_non_null(); + auto *allEqual = _sym_build_equal(*aShadowIt, *bShadowIt); + for (size_t i = 1; i < strlen(a); i++) { + ++aShadowIt; + ++bShadowIt; + allEqual = + _sym_build_bool_and(allEqual, _sym_build_equal(*aShadowIt, *bShadowIt)); + } + + _sym_push_path_constraint(allEqual, result == 0, + reinterpret_cast(SYM(strcmp))); + return result; } int SYM(strncmp)(const char *a, const char *b, size_t n) { - tryAlternative(a, _sym_get_parameter_expression(0), SYM(strncmp)); - tryAlternative(b, _sym_get_parameter_expression(1), SYM(strncmp)); - tryAlternative(n, _sym_get_parameter_expression(2), SYM(strncmp)); + tryAlternative(a, _sym_get_parameter_expression(0), SYM(strncmp)); + tryAlternative(b, _sym_get_parameter_expression(1), SYM(strncmp)); + tryAlternative(n, _sym_get_parameter_expression(2), SYM(strncmp)); - auto result = strncmp(a, b, n); - _sym_set_return_expression(nullptr); - - if (isConcrete(a, n) && isConcrete(b, n)) - return result; - - auto aShadowIt = ReadOnlyShadow(a, n).begin_non_null(); - auto bShadowIt = ReadOnlyShadow(b, n).begin_non_null(); - auto *allEqual = _sym_build_equal(*aShadowIt, *bShadowIt); - for (size_t i = 1; i < n; i++) { - ++aShadowIt; - ++bShadowIt; - allEqual = - _sym_build_bool_and(allEqual, _sym_build_equal(*aShadowIt, *bShadowIt)); - } + auto result = strncmp(a, b, n); + _sym_set_return_expression(nullptr); - _sym_push_path_constraint(allEqual, result == 0, - reinterpret_cast(SYM(strncmp))); + if (isConcrete(a, n) && isConcrete(b, n)) return result; + + auto aShadowIt = ReadOnlyShadow(a, n).begin_non_null(); + auto bShadowIt = ReadOnlyShadow(b, n).begin_non_null(); + auto *allEqual = _sym_build_equal(*aShadowIt, *bShadowIt); + for (size_t i = 1; i < n; i++) { + ++aShadowIt; + ++bShadowIt; + allEqual = + _sym_build_bool_and(allEqual, _sym_build_equal(*aShadowIt, *bShadowIt)); + } + + _sym_push_path_constraint(allEqual, result == 0, + reinterpret_cast(SYM(strncmp))); + return result; } uint32_t SYM(strlen)(const char *s) { - tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen)); + tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen)); - // HACK! we regard strlen as a special strchr(s, '\0') - auto *result = strchr(s, 0); - _sym_set_return_expression(nullptr); + // HACK! we regard strlen as a special strchr(s, '\0') + auto *result = strchr(s, 0); + _sym_set_return_expression(nullptr); - if (isConcrete(s, result != nullptr ? (result - s) : strlen(s))) - return (result - s); - - // We force set the value of c to \x00, it should be a concrete value - auto *cExpr = _sym_build_integer(0, 8); - - size_t length = result != nullptr ? (result - s) : strlen(s); - auto shadow = ReadOnlyShadow(s, length); - auto shadowIt = shadow.begin(); - for (size_t i = 0; i < length; i++) { - _sym_push_path_constraint( - _sym_build_not_equal( - (*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(s[i], 8), - cExpr), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - ++shadowIt; - } + if (isConcrete(s, result != nullptr ? (result - s) : strlen(s))) + return (result - s); - // HACK! The last byte must be \x00! + // We force set the value of c to \x00, it should be a concrete value + auto *cExpr = _sym_build_integer(0, 8); + + size_t length = result != nullptr ? (result - s) : strlen(s); + auto shadow = ReadOnlyShadow(s, length); + auto shadowIt = shadow.begin(); + for (size_t i = 0; i < length; i++) { _sym_push_path_constraint( - _sym_build_equal( - (*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(0, 8), - cExpr), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_build_not_equal( + (*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(s[i], 8), + cExpr), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + ++shadowIt; + } - return (result - s); + // HACK! The last byte must be \x00! + _sym_push_path_constraint( + _sym_build_equal( + (*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(0, 8), cExpr), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + + return (result - s); } int SYM(atoi)(const char *s) { - tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen)); - - // HACK! we mimic the libc function summary in klee - /* int atoi(const char *str) { - * return (int)strtol(str, (char **)NULL, 10); - * } - * */ - auto result = strtol(s, (char **)NULL, 10); - _sym_set_return_expression(nullptr); - - if (isConcrete(s, strlen(s))) - return result; - - size_t length = strlen(s); - size_t num_len = 0; - for (size_t i = 0; i < length; i++) { - if ('0' <= (char)s[i] && (char)s[i] <= '9') { - num_len++; - } else { - break; - } - } - auto shadow = ReadOnlyShadow(s, length); - auto shadowIt = shadow.begin(); - for (size_t i = 0; i < num_len; i++) { - int res = (char)s[i]; - auto *cExpr = _sym_build_integer(res, 8); - _sym_push_path_constraint( - _sym_build_equal( - (*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(s[i], 8), - cExpr), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - ++shadowIt; - } + tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen)); + + // HACK! we mimic the libc function summary in klee + /* int atoi(const char *str) { + * return (int)strtol(str, (char **)NULL, 10); + * } + * */ + auto result = strtol(s, (char **)NULL, 10); + _sym_set_return_expression(nullptr); - // The value of tail must be non-num - auto *tailExpr_0 = _sym_build_integer(48, 8); // '0' - auto *tailExpr_1 = _sym_build_integer(49, 8); // '1' - auto *tailExpr_2 = _sym_build_integer(50, 8); // '2' - auto *tailExpr_3 = _sym_build_integer(51, 8); // '3' - auto *tailExpr_4 = _sym_build_integer(52, 8); // '4' - auto *tailExpr_5 = _sym_build_integer(53, 8); // '5' - auto *tailExpr_6 = _sym_build_integer(54, 8); // '6' - auto *tailExpr_7 = _sym_build_integer(55, 8); // '7' - auto *tailExpr_8 = _sym_build_integer(56, 8); // '8' - auto *tailExpr_9 = _sym_build_integer(57, 8); // '9' + if (isConcrete(s, strlen(s))) + return result; + size_t length = strlen(s); + size_t num_len = 0; + for (size_t i = 0; i < length; i++) { + if ('0' <= (char)s[i] && (char)s[i] <= '9') { + num_len++; + } else { + break; + } + } + auto shadow = ReadOnlyShadow(s, length); + auto shadowIt = shadow.begin(); + for (size_t i = 0; i < num_len; i++) { + int res = (char)s[i]; + auto *cExpr = _sym_build_integer(res, 8); _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_0), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_1), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_2), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_3), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_4), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_5), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_6), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_7), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_8), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_9), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_build_equal((*shadowIt != nullptr) ? *shadowIt + : _sym_build_integer(s[i], 8), + cExpr), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + ++shadowIt; + } - return result; + // The value of tail must be non-num + auto *tailExpr_0 = _sym_build_integer(48, 8); // '0' + auto *tailExpr_1 = _sym_build_integer(49, 8); // '1' + auto *tailExpr_2 = _sym_build_integer(50, 8); // '2' + auto *tailExpr_3 = _sym_build_integer(51, 8); // '3' + auto *tailExpr_4 = _sym_build_integer(52, 8); // '4' + auto *tailExpr_5 = _sym_build_integer(53, 8); // '5' + auto *tailExpr_6 = _sym_build_integer(54, 8); // '6' + auto *tailExpr_7 = _sym_build_integer(55, 8); // '7' + auto *tailExpr_8 = _sym_build_integer(56, 8); // '8' + auto *tailExpr_9 = _sym_build_integer(57, 8); // '9' + + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_0), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_1), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_2), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_3), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_4), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_5), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_6), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_7), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_8), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_9), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + + return result; } long int SYM(atol)(const char *s) { - tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen)); - - // HACK! we mimic the libc function summary in klee - /* int atoi(const char *str) { - * return (int)strtol(str, (char **)NULL, 10); - * } - * */ - auto result = strtol(s, (char **)NULL, 10); - _sym_set_return_expression(nullptr); - - if (isConcrete(s, strlen(s))) - return result; - - size_t length = strlen(s); - size_t num_len = 0; - for (size_t i = 0; i < length; i++) { - if ('0' <= (char)s[i] && (char)s[i] <= '9') { - num_len++; - } else { - break; - } - } - auto shadow = ReadOnlyShadow(s, length); - auto shadowIt = shadow.begin(); - for (size_t i = 0; i < num_len; i++) { - int res = (char)s[i]; - auto *cExpr = _sym_build_integer(res, 8); - _sym_push_path_constraint( - _sym_build_equal( - (*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(s[i], 8), - cExpr), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - ++shadowIt; - } + tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen)); + + // HACK! we mimic the libc function summary in klee + /* int atoi(const char *str) { + * return (int)strtol(str, (char **)NULL, 10); + * } + * */ + auto result = strtol(s, (char **)NULL, 10); + _sym_set_return_expression(nullptr); - // The value of tail must be non-num - auto *tailExpr_0 = _sym_build_integer(48, 8); // '0' - auto *tailExpr_1 = _sym_build_integer(49, 8); // '1' - auto *tailExpr_2 = _sym_build_integer(50, 8); // '2' - auto *tailExpr_3 = _sym_build_integer(51, 8); // '3' - auto *tailExpr_4 = _sym_build_integer(52, 8); // '4' - auto *tailExpr_5 = _sym_build_integer(53, 8); // '5' - auto *tailExpr_6 = _sym_build_integer(54, 8); // '6' - auto *tailExpr_7 = _sym_build_integer(55, 8); // '7' - auto *tailExpr_8 = _sym_build_integer(56, 8); // '8' - auto *tailExpr_9 = _sym_build_integer(57, 8); // '9' + if (isConcrete(s, strlen(s))) + return result; + size_t length = strlen(s); + size_t num_len = 0; + for (size_t i = 0; i < length; i++) { + if ('0' <= (char)s[i] && (char)s[i] <= '9') { + num_len++; + } else { + break; + } + } + auto shadow = ReadOnlyShadow(s, length); + auto shadowIt = shadow.begin(); + for (size_t i = 0; i < num_len; i++) { + int res = (char)s[i]; + auto *cExpr = _sym_build_integer(res, 8); _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_0), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_1), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_2), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_3), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_4), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_5), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_6), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_7), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_8), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); - _sym_push_path_constraint( - _sym_build_not_equal(*shadowIt, tailExpr_9), - /*taken*/ 1, reinterpret_cast(SYM(strchr))); + _sym_build_equal((*shadowIt != nullptr) ? *shadowIt + : _sym_build_integer(s[i], 8), + cExpr), + /*taken*/ 1, reinterpret_cast(SYM(strchr))); + ++shadowIt; + } - return result; + // The value of tail must be non-num + auto *tailExpr_0 = _sym_build_integer(48, 8); // '0' + auto *tailExpr_1 = _sym_build_integer(49, 8); // '1' + auto *tailExpr_2 = _sym_build_integer(50, 8); // '2' + auto *tailExpr_3 = _sym_build_integer(51, 8); // '3' + auto *tailExpr_4 = _sym_build_integer(52, 8); // '4' + auto *tailExpr_5 = _sym_build_integer(53, 8); // '5' + auto *tailExpr_6 = _sym_build_integer(54, 8); // '6' + auto *tailExpr_7 = _sym_build_integer(55, 8); // '7' + auto *tailExpr_8 = _sym_build_integer(56, 8); // '8' + auto *tailExpr_9 = _sym_build_integer(57, 8); // '9' + + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_0), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_1), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_2), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_3), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_4), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_5), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_6), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_7), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_8), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + _sym_push_path_constraint(_sym_build_not_equal(*shadowIt, tailExpr_9), + /*taken*/ 1, + reinterpret_cast(SYM(strchr))); + + return result; } char *SYM(strcpy)(char *dest, const char *src) { - tryAlternative(dest, _sym_get_parameter_expression(0), SYM(strcpy)); - tryAlternative(src, _sym_get_parameter_expression(1), SYM(strcpy)); + tryAlternative(dest, _sym_get_parameter_expression(0), SYM(strcpy)); + tryAlternative(src, _sym_get_parameter_expression(1), SYM(strcpy)); - auto *result = strcpy(dest, src); - _sym_set_return_expression(nullptr); + auto *result = strcpy(dest, src); + _sym_set_return_expression(nullptr); - size_t cpyLen = strlen(src); - if (isConcrete(src, cpyLen) && isConcrete(dest, cpyLen)) - return result; + size_t cpyLen = strlen(src); + if (isConcrete(src, cpyLen) && isConcrete(dest, cpyLen)) + return result; - auto srcShadow = ReadOnlyShadow(src, cpyLen); - auto destShadow = ReadWriteShadow(dest, cpyLen); + auto srcShadow = ReadOnlyShadow(src, cpyLen); + auto destShadow = ReadWriteShadow(dest, cpyLen); - std::copy(srcShadow.begin(), srcShadow.end(), destShadow.begin()); + std::copy(srcShadow.begin(), srcShadow.end(), destShadow.begin()); - return result; + return result; } } diff --git a/runtime/test_wrappers/test_atoi.c b/runtime/test_wrappers/test_atoi.c index b48bb947..86bb7589 100644 --- a/runtime/test_wrappers/test_atoi.c +++ b/runtime/test_wrappers/test_atoi.c @@ -1,28 +1,33 @@ -#include -#include #include -#include #include +#include +#include +#include #include int main(int argc, char *argv[]) { - char buf[1024]; + char buf[1024]; ssize_t i; - if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) + return 0; buf[i] = 0; - if (buf[0] != 'A') return 0; - if (buf[1] != 'B') return 0; - if (buf[2] != 'C') return 0; - if (buf[3] != 'D') return 0; - if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + if (buf[0] != 'A') + return 0; + if (buf[1] != 'B') + return 0; + if (buf[2] != 'C') + return 0; + if (buf[3] != 'D') + return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) + return 0; if (atoi(buf + 12) == 678) { - printf("The result of atoi(buf+12) is: %lu\n", atoi(buf+12)); + printf("The result of atoi(buf+12) is: %lu\n", atoi(buf + 12)); printf("HIT!\n"); } else { - printf("The result of atoi(buf+12) is: %lu\n", atoi(buf+12)); + printf("The result of atoi(buf+12) is: %lu\n", atoi(buf + 12)); printf("NOT HIT!\n"); } return 0; - } diff --git a/runtime/test_wrappers/test_atol.c b/runtime/test_wrappers/test_atol.c index b397528e..4498e797 100644 --- a/runtime/test_wrappers/test_atol.c +++ b/runtime/test_wrappers/test_atol.c @@ -1,28 +1,33 @@ -#include -#include #include -#include #include +#include +#include +#include #include int main(int argc, char *argv[]) { - char buf[1024]; + char buf[1024]; ssize_t i; - if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) + return 0; buf[i] = 0; - if (buf[0] != 'A') return 0; - if (buf[1] != 'B') return 0; - if (buf[2] != 'C') return 0; - if (buf[3] != 'D') return 0; - if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + if (buf[0] != 'A') + return 0; + if (buf[1] != 'B') + return 0; + if (buf[2] != 'C') + return 0; + if (buf[3] != 'D') + return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) + return 0; if (atol(buf + 12) == 99999999) { - printf("The result of atoi(buf+12) is: %lu\n", atol(buf+12)); + printf("The result of atoi(buf+12) is: %lu\n", atol(buf + 12)); printf("HIT!\n"); } else { - printf("The result of atoi(buf+12) is: %lu\n", atol(buf+12)); + printf("The result of atoi(buf+12) is: %lu\n", atol(buf + 12)); printf("NOT HIT!\n"); } return 0; - } diff --git a/runtime/test_wrappers/test_strcmp.c b/runtime/test_wrappers/test_strcmp.c index 3ff4292d..6786f57b 100644 --- a/runtime/test_wrappers/test_strcmp.c +++ b/runtime/test_wrappers/test_strcmp.c @@ -1,20 +1,26 @@ -#include -#include #include -#include #include +#include +#include +#include #include int main(int argc, char *argv[]) { - char buf[1024]; + char buf[1024]; ssize_t i; - if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) + return 0; buf[i] = 0; - if (buf[0] != 'A') return 0; - if (buf[1] != 'B') return 0; - if (buf[2] != 'C') return 0; - if (buf[3] != 'D') return 0; - if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + if (buf[0] != 'A') + return 0; + if (buf[1] != 'B') + return 0; + if (buf[2] != 'C') + return 0; + if (buf[3] != 'D') + return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) + return 0; if (strcmp(buf + 12, "AAAA") == 0) { printf("HIT!\n"); } else { @@ -22,5 +28,4 @@ int main(int argc, char *argv[]) { } return 0; - } diff --git a/runtime/test_wrappers/test_strcpy.c b/runtime/test_wrappers/test_strcpy.c index 53bffc13..f60851d3 100644 --- a/runtime/test_wrappers/test_strcpy.c +++ b/runtime/test_wrappers/test_strcpy.c @@ -1,31 +1,35 @@ -#include -#include #include -#include #include +#include +#include +#include #include int main(int argc, char *argv[]) { - char buf[1024]; + char buf[1024]; ssize_t i; - if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) + return 0; buf[i] = 0; - if (buf[0] != 'A') return 0; - if (buf[1] != 'B') return 0; - if (buf[2] != 'C') return 0; - if (buf[3] != 'D') return 0; - if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + if (buf[0] != 'A') + return 0; + if (buf[1] != 'B') + return 0; + if (buf[2] != 'C') + return 0; + if (buf[3] != 'D') + return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) + return 0; char buf_2[4]; - strcpy(buf_2, buf+12); - if (memcmp(buf_2, "NICE", 4)){ + strcpy(buf_2, buf + 12); + if (memcmp(buf_2, "NICE", 4)) { printf("NOT HIT!\n"); return 0; } else { printf("HIT!\n"); - } return 0; - } diff --git a/runtime/test_wrappers/test_strlen.c b/runtime/test_wrappers/test_strlen.c index 86fac6b2..4a512536 100644 --- a/runtime/test_wrappers/test_strlen.c +++ b/runtime/test_wrappers/test_strlen.c @@ -1,29 +1,34 @@ -#include -#include #include -#include #include +#include +#include +#include #include int main(int argc, char *argv[]) { - char buf[1024]; + char buf[1024]; ssize_t i; - if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) + return 0; buf[i] = 0; - if (buf[0] != 'A') return 0; - if (buf[1] != 'B') return 0; - if (buf[2] != 'C') return 0; - if (buf[3] != 'D') return 0; - if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; + if (buf[0] != 'A') + return 0; + if (buf[1] != 'B') + return 0; + if (buf[2] != 'C') + return 0; + if (buf[3] != 'D') + return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) + return 0; if (strlen(buf + 12) == 5) { - printf("The string length after buf + 12 is: %lu\n", strlen(buf+12)); + printf("The string length after buf + 12 is: %lu\n", strlen(buf + 12)); printf("HIT!\n"); } else { - printf("The string length after buf + 12 is: %lu\n", strlen(buf+12)); - printf("strchr(buf+12) is: %s\n", strchr(buf+12, '\0')); + printf("The string length after buf + 12 is: %lu\n", strlen(buf + 12)); + printf("strchr(buf+12) is: %s\n", strchr(buf + 12, '\0')); printf("NOT HIT!\n"); } return 0; - } diff --git a/runtime/test_wrappers/test_strncmp.c b/runtime/test_wrappers/test_strncmp.c index 961f17ce..500c83e5 100644 --- a/runtime/test_wrappers/test_strncmp.c +++ b/runtime/test_wrappers/test_strncmp.c @@ -1,26 +1,31 @@ -#include -#include #include -#include #include +#include +#include +#include #include int main(int argc, char *argv[]) { - char buf[1024]; + char buf[1024]; ssize_t i; - if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0; + if ((i = read(0, buf, sizeof(buf) - 1)) < 24) + return 0; buf[i] = 0; - if (buf[0] != 'A') return 0; - if (buf[1] != 'B') return 0; - if (buf[2] != 'C') return 0; - if (buf[3] != 'D') return 0; - if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0; - if (strncmp(buf + 12, "AAAA", 4) == 0){ + if (buf[0] != 'A') + return 0; + if (buf[1] != 'B') + return 0; + if (buf[2] != 'C') + return 0; + if (buf[3] != 'D') + return 0; + if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) + return 0; + if (strncmp(buf + 12, "AAAA", 4) == 0) { printf("HIT!\n"); } else { printf("NOT HIT!\n"); } return 0; - } From 85396712151a303faec66c9d38af3a06cfdb60fc Mon Sep 17 00:00:00 2001 From: Sebastian Poeplau Date: Wed, 22 Feb 2023 12:03:11 +0000 Subject: [PATCH 7/7] Move the new libc wrapper tests to the test directory --- runtime/test_wrappers/test_atoi.c => test/atoi.c | 0 runtime/test_wrappers/test_atol.c => test/atol.c | 0 runtime/test_wrappers/test_strcmp.c => test/strcmp.c | 0 runtime/test_wrappers/test_strcpy.c => test/strcpy.c | 0 runtime/test_wrappers/test_strlen.c => test/strlen.c | 0 runtime/test_wrappers/test_strncmp.c => test/strncmp.c | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename runtime/test_wrappers/test_atoi.c => test/atoi.c (100%) rename runtime/test_wrappers/test_atol.c => test/atol.c (100%) rename runtime/test_wrappers/test_strcmp.c => test/strcmp.c (100%) rename runtime/test_wrappers/test_strcpy.c => test/strcpy.c (100%) rename runtime/test_wrappers/test_strlen.c => test/strlen.c (100%) rename runtime/test_wrappers/test_strncmp.c => test/strncmp.c (100%) diff --git a/runtime/test_wrappers/test_atoi.c b/test/atoi.c similarity index 100% rename from runtime/test_wrappers/test_atoi.c rename to test/atoi.c diff --git a/runtime/test_wrappers/test_atol.c b/test/atol.c similarity index 100% rename from runtime/test_wrappers/test_atol.c rename to test/atol.c diff --git a/runtime/test_wrappers/test_strcmp.c b/test/strcmp.c similarity index 100% rename from runtime/test_wrappers/test_strcmp.c rename to test/strcmp.c diff --git a/runtime/test_wrappers/test_strcpy.c b/test/strcpy.c similarity index 100% rename from runtime/test_wrappers/test_strcpy.c rename to test/strcpy.c diff --git a/runtime/test_wrappers/test_strlen.c b/test/strlen.c similarity index 100% rename from runtime/test_wrappers/test_strlen.c rename to test/strlen.c diff --git a/runtime/test_wrappers/test_strncmp.c b/test/strncmp.c similarity index 100% rename from runtime/test_wrappers/test_strncmp.c rename to test/strncmp.c