Skip to content

Commit e0afbb6

Browse files
add test_strlen() wrapper
1 parent f366ace commit e0afbb6

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
@@ -436,4 +436,39 @@ int SYM(strncmp)(const char *a, const char *b, size_t n) {
436436
reinterpret_cast<uintptr_t>(SYM(strncmp)));
437437
return result;
438438
}
439+
440+
uint32_t SYM(strlen)(const char *s) {
441+
tryAlternative(s, _sym_get_parameter_expression(0), SYM(strlen));
442+
443+
// HACK! we regard strlen as a special strchr(s, '\0')
444+
auto *result = strchr(s, 0);
445+
_sym_set_return_expression(nullptr);
446+
447+
if (isConcrete(s, result != nullptr ? (result - s) : strlen(s)))
448+
return (result - s);
449+
450+
// We force set the value of c to \x00, it should be a concrete value
451+
auto *cExpr = _sym_build_integer(0, 8);
452+
453+
size_t length = result != nullptr ? (result - s) : strlen(s);
454+
auto shadow = ReadOnlyShadow(s, length);
455+
auto shadowIt = shadow.begin();
456+
for (size_t i = 0; i < length; i++) {
457+
_sym_push_path_constraint(
458+
_sym_build_not_equal(
459+
(*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(s[i], 8),
460+
cExpr),
461+
/*taken*/ 1, reinterpret_cast<uintptr_t>(SYM(strchr)));
462+
++shadowIt;
463+
}
464+
465+
// HACK! The last byte must be \x00!
466+
_sym_push_path_constraint(
467+
_sym_build_equal(
468+
(*shadowIt != nullptr) ? *shadowIt : _sym_build_integer(0, 8),
469+
cExpr),
470+
/*taken*/ 1, reinterpret_cast<uintptr_t>(SYM(strchr)));
471+
472+
return (result - s);
473+
}
439474
}
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)