Skip to content

Commit 91e33e2

Browse files
tiedaoxiaotubiesebastianpoeplau
authored andcommitted
add test_strlen() wrapper
1 parent 6df681f commit 91e33e2

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

runtime/LibcWrappers.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,4 +659,39 @@ int SYM(strncmp)(const char *a, const char *b, size_t n) {
659659
reinterpret_cast<uintptr_t>(SYM(strncmp)));
660660
return result;
661661
}
662+
663+
uint32_t SYM(strlen)(const char *s) {
664+
tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen));
665+
666+
// HACK! we regard strlen as a special strchr(s, '\0')
667+
auto *result = strchr(s, 0);
668+
_sym_set_return_expression(nullptr);
669+
670+
if (isConcrete(s, result != nullptr ? (result - s) : strlen(s)))
671+
return (result - s);
672+
673+
// We force set the value of c to \x00, it should be a concrete value
674+
auto *cExpr = _sym_build_integer(0, 8);
675+
676+
size_t length = result != nullptr ? (result - s) : strlen(s);
677+
auto shadow = ReadOnlyShadow(s, length);
678+
auto shadowIt = shadow.begin();
679+
for (size_t i = 0; i < length; i++) {
680+
_sym_push_path_constraint(
681+
_sym_build_not_equal(
682+
(*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(s[i], 8),
683+
cExpr),
684+
/*taken*/ 1, reinterpret_cast<uintptr_t>(SYM(strchr)));
685+
++shadowIt;
686+
}
687+
688+
// HACK! The last byte must be \x00!
689+
_sym_push_path_constraint(
690+
_sym_build_equal(
691+
(*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(0, 8),
692+
cExpr),
693+
/*taken*/ 1, reinterpret_cast<uintptr_t>(SYM(strchr)));
694+
695+
return (result - s);
696+
}
662697
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdarg.h>
4+
#include <stdlib.h>
5+
#include <stdint.h>
6+
#include <unistd.h>
7+
int main(int argc, char *argv[]) {
8+
9+
char buf[1024];
10+
ssize_t i;
11+
if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0;
12+
buf[i] = 0;
13+
if (buf[0] != 'A') return 0;
14+
if (buf[1] != 'B') return 0;
15+
if (buf[2] != 'C') return 0;
16+
if (buf[3] != 'D') return 0;
17+
if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0;
18+
if (strlen(buf + 12) == 5) {
19+
printf("The string length after buf + 12 is: %lu\n", strlen(buf+12));
20+
printf("HIT!\n");
21+
} else {
22+
printf("The string length after buf + 12 is: %lu\n", strlen(buf+12));
23+
printf("strchr(buf+12) is: %s\n", strchr(buf+12, '\0'));
24+
printf("NOT HIT!\n");
25+
}
26+
27+
return 0;
28+
29+
}

0 commit comments

Comments
 (0)