Skip to content

Commit 3ebd00e

Browse files
add strcmp() wrapper
1 parent b66fa69 commit 3ebd00e

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

runtime/LibcWrappers.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,29 @@ int SYM(memcmp)(const void *a, const void *b, size_t n) {
385385
reinterpret_cast<uintptr_t>(SYM(memcmp)));
386386
return result;
387387
}
388+
389+
int SYM(strcmp)(const char *a, const char *b) {
390+
tryAlternative(a, _sym_get_parameter_expression(0), SYM(strcmp));
391+
tryAlternative(b, _sym_get_parameter_expression(1), SYM(strcmp));
392+
393+
auto result = strcmp(a, b);
394+
_sym_set_return_expression(nullptr);
395+
396+
if (isConcrete(a, strlen(a)) && isConcrete(b, strlen(b)))
397+
return result;
398+
399+
auto aShadowIt = ReadOnlyShadow(a, strlen(a)).begin_non_null();
400+
auto bShadowIt = ReadOnlyShadow(b, strlen(b)).begin_non_null();
401+
auto *allEqual = _sym_build_equal(*aShadowIt, *bShadowIt);
402+
for (size_t i = 1; i < strlen(a); i++) {
403+
++aShadowIt;
404+
++bShadowIt;
405+
allEqual =
406+
_sym_build_bool_and(allEqual, _sym_build_equal(*aShadowIt, *bShadowIt));
407+
}
408+
409+
_sym_push_path_constraint(allEqual, result == 0,
410+
reinterpret_cast<uintptr_t>(SYM(strcmp)));
411+
return result;
412+
}
388413
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 (strcmp(buf + 12, "AAAA") == 0) {
19+
printf("HIT!\n");
20+
} else {
21+
printf("NOT HIT!\n");
22+
}
23+
24+
return 0;
25+
26+
}

0 commit comments

Comments
 (0)