<div dir="ltr">Windows doesn't have strncasecmp. I have a hack to make the test pass, but maybe it would be better to split the test into strncmp and strncasecmp and XFAIL the strncasecmp one.</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Nov 11, 2016 at 8:32 PM, Vitaly Buka via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: vitalybuka<br>
Date: Fri Nov 11 22:32:31 2016<br>
New Revision: 286708<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=286708&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=286708&view=rev</a><br>
Log:<br>
[asan] Fix strncmp and strncasecmp interceptors<br>
<br>
Summary:<br>
In non-strict mode we will check memory access for both strings from beginning<br>
to either:<br>
1. 0-char<br>
2. size<br>
3. different chars<br>
<br>
In strict mode we will check from beginning to either:<br>
1. 0-char<br>
2. size<br>
<br>
Previously in strict mode we always checked up to the 0-char.<br>
<br>
Reviewers: kcc, eugenis<br>
<br>
Subscribers: llvm-commits, kubabrecka<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D26574" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D26574</a><br>
<br>
Modified:<br>
compiler-rt/trunk/lib/<wbr>sanitizer_common/sanitizer_<wbr>common_interceptors.inc<br>
compiler-rt/trunk/test/asan/<wbr>TestCases/strncmp_strict.c<br>
<br>
Modified: compiler-rt/trunk/lib/<wbr>sanitizer_common/sanitizer_<wbr>common_interceptors.inc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=286708&r1=286707&r2=286708&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/compiler-rt/trunk/lib/<wbr>sanitizer_common/sanitizer_<wbr>common_interceptors.inc?rev=<wbr>286708&r1=286707&r2=286708&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- compiler-rt/trunk/lib/<wbr>sanitizer_common/sanitizer_<wbr>common_interceptors.inc (original)<br>
+++ compiler-rt/trunk/lib/<wbr>sanitizer_common/sanitizer_<wbr>common_interceptors.inc Fri Nov 11 22:32:31 2016<br>
@@ -304,8 +304,14 @@ INTERCEPTOR(int, strncmp, const char *s1<br>
c2 = (unsigned char)s2[i];<br>
if (c1 != c2 || c1 == '\0') break;<br>
}<br>
- COMMON_INTERCEPTOR_READ_<wbr>STRING(ctx, s1, Min(i + 1, size));<br>
- COMMON_INTERCEPTOR_READ_<wbr>STRING(ctx, s2, Min(i + 1, size));<br>
+ uptr i1 = i;<br>
+ uptr i2 = i;<br>
+ if (common_flags()->strict_<wbr>string_checks) {<br>
+ for (; i1 < size && s1[i1]; i1++) {}<br>
+ for (; i2 < size && s2[i2]; i2++) {}<br>
+ }<br>
+ COMMON_INTERCEPTOR_READ_RANGE(<wbr>(ctx), (s1), Min(i1 + 1, size));<br>
+ COMMON_INTERCEPTOR_READ_RANGE(<wbr>(ctx), (s2), Min(i2 + 1, size));<br>
int result = CharCmpX(c1, c2);<br>
CALL_WEAK_INTERCEPTOR_HOOK(__<wbr>sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,<br>
s2, size, result);<br>
@@ -348,24 +354,30 @@ INTERCEPTOR(int, strcasecmp, const char<br>
}<br>
<br>
DECLARE_WEAK_INTERCEPTOR_HOOK(<wbr>__sanitizer_weak_hook_<wbr>strncasecmp, uptr called_pc,<br>
- const char *s1, const char *s2, uptr n,<br>
+ const char *s1, const char *s2, uptr size,<br>
int result)<br>
<br>
-INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) {<br>
+INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {<br>
void *ctx;<br>
- COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);<br>
+ COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);<br>
unsigned char c1 = 0, c2 = 0;<br>
uptr i;<br>
- for (i = 0; i < n; i++) {<br>
+ for (i = 0; i < size; i++) {<br>
c1 = (unsigned char)s1[i];<br>
c2 = (unsigned char)s2[i];<br>
if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;<br>
}<br>
- COMMON_INTERCEPTOR_READ_RANGE(<wbr>ctx, s1, Min(i + 1, n));<br>
- COMMON_INTERCEPTOR_READ_RANGE(<wbr>ctx, s2, Min(i + 1, n));<br>
+ uptr i1 = i;<br>
+ uptr i2 = i;<br>
+ if (common_flags()->strict_<wbr>string_checks) {<br>
+ for (; i1 < size && s1[i1]; i1++) {}<br>
+ for (; i2 < size && s2[i2]; i2++) {}<br>
+ }<br>
+ COMMON_INTERCEPTOR_READ_RANGE(<wbr>(ctx), (s1), Min(i1 + 1, size));<br>
+ COMMON_INTERCEPTOR_READ_RANGE(<wbr>(ctx), (s2), Min(i2 + 1, size));<br>
int result = CharCaseCmp(c1, c2);<br>
CALL_WEAK_INTERCEPTOR_HOOK(__<wbr>sanitizer_weak_hook_<wbr>strncasecmp, GET_CALLER_PC(),<br>
- s1, s2, n, result);<br>
+ s1, s2, size, result);<br>
return result;<br>
}<br>
<br>
<br>
Modified: compiler-rt/trunk/test/asan/<wbr>TestCases/strncmp_strict.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/strncmp_strict.c?rev=286708&r1=286707&r2=286708&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/compiler-rt/trunk/<wbr>test/asan/TestCases/strncmp_<wbr>strict.c?rev=286708&r1=286707&<wbr>r2=286708&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- compiler-rt/trunk/test/asan/<wbr>TestCases/strncmp_strict.c (original)<br>
+++ compiler-rt/trunk/test/asan/<wbr>TestCases/strncmp_strict.c Fri Nov 11 22:32:31 2016<br>
@@ -1,26 +1,81 @@<br>
// Test strict_string_checks option in strncmp function<br>
-// RUN: %clang_asan %s -o %t && %run %t 2>&1<br>
-// RUN: %env_asan_opts=strict_string_<wbr>checks=false %run %t 2>&1<br>
-// RUN: %env_asan_opts=strict_string_<wbr>checks=true not %run %t 2>&1 | FileCheck %s<br>
+// RUN: %clang_asan %s -o %t<br>
+<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=false %run %t a 2>&1<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=true %run %t a 2>&1<br>
+// RUN: not %run %t b 2>&1 | FileCheck %s<br>
+// RUN: not %run %t c 2>&1 | FileCheck %s<br>
+// RUN: not %run %t d 2>&1 | FileCheck %s<br>
+// RUN: not %run %t e 2>&1 | FileCheck %s<br>
+// RUN: not %run %t f 2>&1 | FileCheck %s<br>
+// RUN: not %run %t g 2>&1 | FileCheck %s<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=false %run %t h 2>&1<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=true not %run %t h 2>&1 | FileCheck %s<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=false %run %t i 2>&1<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=true not %run %t i 2>&1 | FileCheck %s<br>
+<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=false %run %t A 2>&1<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=true %run %t A 2>&1<br>
+// RUN: not %run %t B 2>&1 | FileCheck %s<br>
+// RUN: not %run %t C 2>&1 | FileCheck %s<br>
+// RUN: not %run %t D 2>&1 | FileCheck %s<br>
+// RUN: not %run %t E 2>&1 | FileCheck %s<br>
+// RUN: not %run %t F 2>&1 | FileCheck %s<br>
+// RUN: not %run %t G 2>&1 | FileCheck %s<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=false %run %t H 2>&1<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=true not %run %t H 2>&1 | FileCheck %s<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=false %run %t I 2>&1<br>
+// RUN: %env_asan_opts=strict_string_<wbr>checks=true not %run %t I 2>&1 | FileCheck %s<br>
<br>
#include <assert.h><br>
#include <stdlib.h><br>
+#include <stdio.h><br>
#include <string.h><br>
+#include <ctype.h><br>
<br>
int main(int argc, char **argv) {<br>
- size_t size = 100;<br>
+ assert(argc >= 2);<br>
+ const size_t size = 100;<br>
char fill = 'o';<br>
- char *s1 = (char*)malloc(size);<br>
+ char s1[size];<br>
+ char s2[size];<br>
memset(s1, fill, size);<br>
- char *s2 = (char*)malloc(size);<br>
memset(s2, fill, size);<br>
- s1[size - 1] = 'z';<br>
- s2[size - 1] = 'x';<br>
- int r = strncmp(s1, s2, size + 1);<br>
- // CHECK: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}}<br>
- // CHECK: READ of size 101<br>
- assert(r == 1);<br>
- free(s1);<br>
- free(s2);<br>
+<br>
+ int (*cmp_fn)(const char *, const char *, size_t);<br>
+ cmp_fn = islower(argv[1][0]) ? &strncmp : &strncasecmp;<br>
+<br>
+ switch(tolower(argv[1][0])) {<br>
+ case 'a':<br>
+ s1[size - 1] = 'z';<br>
+ s2[size - 1] = 'x';<br>
+ for (int i = 0; i <= size; ++i)<br>
+ assert((cmp_fn(s1, s2, i) == 0) == (i < size));<br>
+ s1[size - 1] = '\0';<br>
+ s2[size - 1] = '\0';<br>
+ assert(cmp_fn(s1, s2, 2*size) == 0);<br>
+ break;<br>
+ case 'b':<br>
+ return cmp_fn(s1-1, s2, 1);<br>
+ case 'c':<br>
+ return cmp_fn(s1, s2-1, 1);<br>
+ case 'd':<br>
+ return cmp_fn(s1+size, s2, 1);<br>
+ case 'e':<br>
+ return cmp_fn(s1, s2+size, 1);<br>
+ case 'f':<br>
+ return cmp_fn(s1+1, s2, size);<br>
+ case 'g':<br>
+ return cmp_fn(s1, s2+1, size);<br>
+ case 'h':<br>
+ s1[size - 1] = '\0';<br>
+ assert(cmp_fn(s1, s2, 2*size) != 0);<br>
+ break;<br>
+ case 'i':<br>
+ s2[size - 1] = '\0';<br>
+ assert(cmp_fn(s1, s2, 2*size) != 0);<br>
+ break;<br>
+ // CHECK: {{.*}}ERROR: AddressSanitizer: stack-buffer-{{ov|und}}erflow on address<br>
+ }<br>
return 0;<br>
}<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>