[compiler-rt] r350002 - [Sanitizer] Enable POSIX regex api on FreeBSD.

David Carlier via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 22 03:17:27 PST 2018


Author: devnexen
Date: Sat Dec 22 03:17:27 2018
New Revision: 350002

URL: http://llvm.org/viewvc/llvm-project?rev=350002&view=rev
Log:
[Sanitizer] Enable POSIX regex api on FreeBSD.

Reviewers: krytarowski

Reviewed By: krytarowski

Differential Revision: https://reviews.llvm.org/D56009

M    lib/sanitizer_common/sanitizer_common_interceptors.inc
M    lib/sanitizer_common/sanitizer_platform_interceptors.h
M    lib/sanitizer_common/sanitizer_platform_limits_freebsd.cc
M    lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
D    test/sanitizer_common/TestCases/NetBSD/regex.cc
A  + test/sanitizer_common/TestCases/Posix/regex.cc

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/regex.cc
      - copied, changed from r350001, compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/regex.cc
Removed:
    compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/regex.cc
Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=350002&r1=350001&r2=350002&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Sat Dec 22 03:17:27 2018
@@ -7421,6 +7421,16 @@ INTERCEPTOR(void, regfree, const void *p
     COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
   REAL(regfree)(preg);
 }
+#define INIT_REGEX                                                             \
+  COMMON_INTERCEPT_FUNCTION(regcomp);                                          \
+  COMMON_INTERCEPT_FUNCTION(regexec);                                          \
+  COMMON_INTERCEPT_FUNCTION(regerror);                                         \
+  COMMON_INTERCEPT_FUNCTION(regfree);
+#else
+#define INIT_REGEX
+#endif
+
+#if SANITIZER_INTERCEPT_REGEXSUB
 INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
             const struct __sanitizer_regmatch *rm, const char *str) {
   void *ctx;
@@ -7455,15 +7465,12 @@ INTERCEPTOR(SSIZE_T, regasub, char **buf
   }
   return res;
 }
-#define INIT_REGEX                                                             \
-  COMMON_INTERCEPT_FUNCTION(regcomp);                                          \
-  COMMON_INTERCEPT_FUNCTION(regexec);                                          \
-  COMMON_INTERCEPT_FUNCTION(regerror);                                         \
-  COMMON_INTERCEPT_FUNCTION(regfree);                                          \
+
+#define INIT_REGEXSUB                                                          \
   COMMON_INTERCEPT_FUNCTION(regnsub);                                          \
   COMMON_INTERCEPT_FUNCTION(regasub);
 #else
-#define INIT_REGEX
+#define INIT_REGEXSUB
 #endif
 
 #if SANITIZER_INTERCEPT_FTS
@@ -9297,6 +9304,7 @@ static void InitializeCommonInterceptors
   INIT_SETVBUF;
   INIT_GETVFSSTAT;
   INIT_REGEX;
+  INIT_REGEXSUB;
   INIT_FTS;
   INIT_SYSCTL;
   INIT_ASYSCTL;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h?rev=350002&r1=350001&r2=350002&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Sat Dec 22 03:17:27 2018
@@ -522,7 +522,8 @@
 #define SANITIZER_INTERCEPT_GETMNTINFO (SI_NETBSD || SI_FREEBSD || SI_MAC)
 #define SANITIZER_INTERCEPT_MI_VECTOR_HASH SI_NETBSD
 #define SANITIZER_INTERCEPT_GETVFSSTAT SI_NETBSD
-#define SANITIZER_INTERCEPT_REGEX SI_NETBSD
+#define SANITIZER_INTERCEPT_REGEX (SI_NETBSD || SI_FREEBSD)
+#define SANITIZER_INTERCEPT_REGEXSUB SI_NETBSD
 #define SANITIZER_INTERCEPT_FTS SI_NETBSD
 #define SANITIZER_INTERCEPT_SYSCTL (SI_NETBSD || SI_FREEBSD || SI_MAC)
 #define SANITIZER_INTERCEPT_ASYSCTL SI_NETBSD

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cc?rev=350002&r1=350001&r2=350002&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cc Sat Dec 22 03:17:27 2018
@@ -25,6 +25,7 @@
 #include <poll.h>
 #include <pthread.h>
 #include <pwd.h>
+#include <regex.h>
 #include <signal.h>
 #include <stddef.h>
 #include <sys/mman.h>
@@ -125,6 +126,8 @@ namespace __sanitizer {
   unsigned struct_statvfs_sz = sizeof(struct statvfs);
   unsigned struct_shminfo_sz = sizeof(struct shminfo);
   unsigned struct_shm_info_sz = sizeof(struct shm_info);
+  unsigned struct_regmatch_sz = sizeof(regmatch_t);
+  unsigned struct_regex_sz = sizeof(regex_t);
 
   const uptr sig_ign = (uptr)SIG_IGN;
   const uptr sig_dfl = (uptr)SIG_DFL;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h?rev=350002&r1=350001&r2=350002&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h Sat Dec 22 03:17:27 2018
@@ -62,6 +62,8 @@ namespace __sanitizer {
   extern unsigned struct_rlimit_sz;
   extern unsigned struct_utimbuf_sz;
   extern unsigned struct_timespec_sz;
+  extern unsigned struct_regmatch_sz;
+  extern unsigned struct_regex_sz;
   extern const int unvis_valid;
   extern const int unvis_validpush;
 

Removed: compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/regex.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/regex.cc?rev=350001&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/regex.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/regex.cc (removed)
@@ -1,101 +0,0 @@
-// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
-
-#include <assert.h>
-#include <regex.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-void test_matched(const regex_t *preg, const char *string) {
-  int rv = regexec(preg, string, 0, NULL, 0);
-  if (!rv)
-    printf("%s: matched\n", string);
-  else if (rv == REG_NOMATCH)
-    printf("%s: not-matched\n", string);
-  else
-    abort();
-}
-
-void test_print_matches(const regex_t *preg, const char *string) {
-  regmatch_t rm[10];
-  int rv = regexec(preg, string, __arraycount(rm), rm, 0);
-  if (!rv) {
-    for (size_t i = 0; i < __arraycount(rm); i++) {
-      // This condition shall be simplified, but verify that the data fields
-      // are accessible.
-      if (rm[i].rm_so == -1 && rm[i].rm_eo == -1)
-        continue;
-      printf("matched[%zu]='%.*s'\n", i, (int)(rm[i].rm_eo - rm[i].rm_so),
-             string + rm[i].rm_so);
-    }
-  } else if (rv == REG_NOMATCH)
-    printf("%s: not-matched\n", string);
-  else
-    abort();
-}
-
-void test_nsub(const regex_t *preg, const char *string) {
-  regmatch_t rm[10];
-  int rv = regexec(preg, string, __arraycount(rm), rm, 0);
-  if (!rv) {
-    char buf[1024];
-    ssize_t ss = regnsub(buf, __arraycount(buf), "\\1xyz", rm, string);
-    assert(ss != -1);
-
-    printf("'%s' -> '%s'\n", string, buf);
-  } else if (rv == REG_NOMATCH)
-    printf("%s: not-matched\n", string);
-  else
-    abort();
-}
-
-void test_asub(const regex_t *preg, const char *string) {
-  regmatch_t rm[10];
-  int rv = regexec(preg, string, __arraycount(rm), rm, 0);
-  if (!rv) {
-    char *buf;
-    ssize_t ss = regasub(&buf, "\\1xyz", rm, string);
-    assert(ss != -1);
-
-    printf("'%s' -> '%s'\n", string, buf);
-    free(buf);
-  } else if (rv == REG_NOMATCH)
-    printf("%s: not-matched\n", string);
-  else
-    abort();
-}
-
-int main(void) {
-  printf("regex\n");
-
-  regex_t regex;
-  int rv = regcomp(&regex, "[[:upper:]]\\([[:upper:]]\\)", 0);
-  assert(!rv);
-
-  test_matched(&regex, "abc");
-  test_matched(&regex, "ABC");
-
-  test_print_matches(&regex, "ABC");
-
-  test_nsub(&regex, "ABC DEF");
-  test_asub(&regex, "GHI JKL");
-
-  regfree(&regex);
-
-  rv = regcomp(&regex, "[[:upp:]]", 0);
-  assert(rv);
-
-  char errbuf[1024];
-  regerror(rv, &regex, errbuf, sizeof errbuf);
-  printf("error: %s\n", errbuf);
-
-  // CHECK: regex
-  // CHECK: abc: not-matched
-  // CHECK: ABC: matched
-  // CHECK: matched[0]='AB'
-  // CHECK: matched[1]='B'
-  // CHECK: 'ABC DEF' -> 'Bxyz'
-  // CHECK: 'GHI JKL' -> 'Hxyz'
-  // CHECK: error:{{.*}}
-
-  return 0;
-}

Copied: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/regex.cc (from r350001, compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/regex.cc)
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/regex.cc?p2=compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/regex.cc&p1=compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/regex.cc&r1=350001&r2=350002&rev=350002&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/regex.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/regex.cc Sat Dec 22 03:17:27 2018
@@ -1,10 +1,16 @@
 // RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+//
+// UNSUPPORTED: linux, darwin, solaris
 
 #include <assert.h>
 #include <regex.h>
 #include <stdio.h>
 #include <stdlib.h>
 
+#ifndef __arraycount
+#define __arraycount(a) ((sizeof(a) / sizeof(a[0])))
+#endif
+
 void test_matched(const regex_t *preg, const char *string) {
   int rv = regexec(preg, string, 0, NULL, 0);
   if (!rv)
@@ -33,37 +39,6 @@ void test_print_matches(const regex_t *p
     abort();
 }
 
-void test_nsub(const regex_t *preg, const char *string) {
-  regmatch_t rm[10];
-  int rv = regexec(preg, string, __arraycount(rm), rm, 0);
-  if (!rv) {
-    char buf[1024];
-    ssize_t ss = regnsub(buf, __arraycount(buf), "\\1xyz", rm, string);
-    assert(ss != -1);
-
-    printf("'%s' -> '%s'\n", string, buf);
-  } else if (rv == REG_NOMATCH)
-    printf("%s: not-matched\n", string);
-  else
-    abort();
-}
-
-void test_asub(const regex_t *preg, const char *string) {
-  regmatch_t rm[10];
-  int rv = regexec(preg, string, __arraycount(rm), rm, 0);
-  if (!rv) {
-    char *buf;
-    ssize_t ss = regasub(&buf, "\\1xyz", rm, string);
-    assert(ss != -1);
-
-    printf("'%s' -> '%s'\n", string, buf);
-    free(buf);
-  } else if (rv == REG_NOMATCH)
-    printf("%s: not-matched\n", string);
-  else
-    abort();
-}
-
 int main(void) {
   printf("regex\n");
 
@@ -76,9 +51,6 @@ int main(void) {
 
   test_print_matches(&regex, "ABC");
 
-  test_nsub(&regex, "ABC DEF");
-  test_asub(&regex, "GHI JKL");
-
   regfree(&regex);
 
   rv = regcomp(&regex, "[[:upp:]]", 0);
@@ -93,8 +65,6 @@ int main(void) {
   // CHECK: ABC: matched
   // CHECK: matched[0]='AB'
   // CHECK: matched[1]='B'
-  // CHECK: 'ABC DEF' -> 'Bxyz'
-  // CHECK: 'GHI JKL' -> 'Hxyz'
   // CHECK: error:{{.*}}
 
   return 0;




More information about the llvm-commits mailing list