Skip to content

Commit 2636452

Browse files
tiedaoxiaotubiesebastianpoeplau
authored andcommitted
add strcmp() wrapper
1 parent de3e888 commit 2636452

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
@@ -608,4 +608,29 @@ uint32_t SYM(ntohl)(uint32_t netlong) {
608608

609609
return result;
610610
}
611+
612+
int SYM(strcmp)(const char *a, const char *b) {
613+
tryAlternative(a, _sym_get_parameter_expression(0), SYM(strcmp));
614+
tryAlternative(b, _sym_get_parameter_expression(1), SYM(strcmp));
615+
616+
auto result = strcmp(a, b);
617+
_sym_set_return_expression(nullptr);
618+
619+
if (isConcrete(a, strlen(a)) && isConcrete(b, strlen(b)))
620+
return result;
621+
622+
auto aShadowIt = ReadOnlyShadow(a, strlen(a)).begin_non_null();
623+
auto bShadowIt = ReadOnlyShadow(b, strlen(b)).begin_non_null();
624+
auto *allEqual = _sym_build_equal(*aShadowIt, *bShadowIt);
625+
for (size_t i = 1; i < strlen(a); i++) {
626+
++aShadowIt;
627+
++bShadowIt;
628+
allEqual =
629+
_sym_build_bool_and(allEqual, _sym_build_equal(*aShadowIt, *bShadowIt));
630+
}
631+
632+
_sym_push_path_constraint(allEqual, result == 0,
633+
reinterpret_cast<uintptr_t>(SYM(strcmp)));
634+
return result;
635+
}
611636
}
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)