about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorFrank Busse <bb0xfb@gmail.com>2018-10-03 22:23:49 +0100
committerCristian Cadar <c.cadar@imperial.ac.uk>2018-10-08 16:12:29 +0100
commitf3d3cca63d1e2500764279f7b4fc302fd8106676 (patch)
treeff3997a39bda7a15122ccb102f773066532413cc
parentdc6cba8a1524fdad87660c780c9c12dcb0b38b24 (diff)
downloadklee-f3d3cca63d1e2500764279f7b4fc302fd8106676.tar.gz
add support for klee-replay on OSX
* also adds klee-replay as dependency for systemtests
-rw-r--r--CMakeLists.txt27
-rw-r--r--include/klee/Config/config.h.cmin9
-rw-r--r--runtime/POSIX/fd.h19
-rw-r--r--test/CMakeLists.txt2
-rw-r--r--tools/klee-replay/CMakeLists.txt16
-rw-r--r--tools/klee-replay/file-creator.c26
-rw-r--r--tools/klee-replay/klee-replay.c10
-rw-r--r--tools/klee-replay/klee-replay.h1
8 files changed, 81 insertions, 29 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 961ab19d..2e894aec 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -42,6 +42,9 @@ set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX
   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cxx_flags_override.cmake")
 project(KLEE CXX C)
 
+include(CheckFunctionExists)
+include(CheckLibraryExists)
+
 ###############################################################################
 # Project version
 ###############################################################################
@@ -429,15 +432,19 @@ endif()
 ################################################################################
 # Detect libutil
 ################################################################################
-check_include_file("pty.h" HAVE_PTY_H)
-if (HAVE_PTY_H)
-  find_library(LIBUTIL_LIBRARIES
-    NAMES util
-    DOC "libutil library. Typically part of glibc")
-  if (NOT LIBUTIL_LIBRARIES)
-    message(FATAL_ERROR "Found \"pty.h\" but could not find libutil")
-  endif()
-endif()
+check_include_file(pty.h HAVE_PTY_H)
+check_include_file(util.h HAVE_UTIL_H)
+if (HAVE_PTY_H OR HAVE_UTIL_H)
+  check_function_exists(openpty openpty_in_libc)
+  if (NOT openpty_in_libc)
+    check_library_exists(util openpty "" openpty_in_libutil)
+    if (openpty_in_libutil)
+      set(LIBUTIL_LIBRARIES util)
+    else ()
+      message(FATAL_ERROR "Could not find libutil")
+    endif (openpty_in_libutil)
+  endif (NOT openpty_in_libc)
+endif (HAVE_PTY_H OR HAVE_UTIL_H)
 
 ################################################################################
 # Miscellaneous header file detection
@@ -446,6 +453,8 @@ check_cxx_symbol_exists(__ctype_b_loc ctype.h HAVE_CTYPE_EXTERNALS)
 check_cxx_symbol_exists(mallinfo malloc.h HAVE_MALLINFO)
 check_cxx_symbol_exists(malloc_zone_statistics malloc/malloc.h HAVE_MALLOC_ZONE_STATISTICS)
 
+check_include_file(sys/statfs.h HAVE_SYSSTATFS_H)
+
 # FIXME: This is needed by the runtime not KLEE itself so we are testing the wrong
 # compiler.
 check_include_file("selinux/selinux.h" HAVE_SELINUX_SELINUX_H)
diff --git a/include/klee/Config/config.h.cmin b/include/klee/Config/config.h.cmin
index b2b9692a..0a5811b7 100644
--- a/include/klee/Config/config.h.cmin
+++ b/include/klee/Config/config.h.cmin
@@ -25,6 +25,15 @@
 /* Define to 1 if you have the `malloc_zone_statistics' function. */
 #cmakedefine HAVE_MALLOC_ZONE_STATISTICS @HAVE_MALLOC_ZONE_STATISTICS@
 
+/* Define to 1 if you have the <pty.h> header file. */
+#cmakedefine HAVE_PTY_H @HAVE_PTY_H@
+
+/* Define to 1 if you have the <util.h> header file. */
+#cmakedefine HAVE_UTIL_H @HAVE_UTIL_H@
+
+/* Define to 1 if you have the <sys/statfs.h> header file. */
+#cmakedefine HAVE_SYSSTATFS_H @HAVE_SYSSTATFS_H@
+
 /* Define to 1 if you have the <selinux/selinux.h> header file. */
 #cmakedefine HAVE_SELINUX_SELINUX_H @HAVE_SELINUX_SELINUX_H@
 
diff --git a/runtime/POSIX/fd.h b/runtime/POSIX/fd.h
index 4a6fbc15..4c07ac9a 100644
--- a/runtime/POSIX/fd.h
+++ b/runtime/POSIX/fd.h
@@ -10,14 +10,29 @@
 #ifndef __EXE_FD__
 #define __EXE_FD__
 
+#include "klee/Config/config.h"
+
 #ifndef _LARGEFILE64_SOURCE
 #error "_LARGEFILE64_SOURCE should be defined"
 #endif
+
+#include <dirent.h>
 #include <sys/types.h>
+
+#ifdef HAVE_SYSSTATFS_H
 #include <sys/statfs.h>
-#include <dirent.h>
+#endif
 
-typedef struct {  
+#if defined(__APPLE__)
+#include <sys/dtrace.h>
+#include <sys/mount.h>
+#include <sys/param.h>
+#if !defined(dirent64)
+#define dirent64 dirent
+#endif
+#endif
+
+typedef struct {
   unsigned size;  /* in bytes */
   char* contents;
   struct stat64* stat;
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index ac405da1..0e4ef6f3 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -129,7 +129,7 @@ configure_file(lit.site.cfg.in
 
 add_custom_target(systemtests
   COMMAND "${LIT_TOOL}" ${LIT_ARGS} "${CMAKE_CURRENT_BINARY_DIR}"
-  DEPENDS klee kleaver kleeRuntest
+  DEPENDS klee kleaver klee-replay kleeRuntest
   COMMENT "Running system tests"
   ${ADD_CUSTOM_COMMAND_USES_TERMINAL_ARG}
 )
diff --git a/tools/klee-replay/CMakeLists.txt b/tools/klee-replay/CMakeLists.txt
index e0eb8d07..6388df50 100644
--- a/tools/klee-replay/CMakeLists.txt
+++ b/tools/klee-replay/CMakeLists.txt
@@ -6,7 +6,7 @@
 # License. See LICENSE.TXT for details.
 #
 #===------------------------------------------------------------------------===#
-if (HAVE_PTY_H)
+if (HAVE_PTY_H OR HAVE_UTIL_H)
   add_executable(klee-replay
     fd_init.c
     file-creator.c
@@ -15,16 +15,10 @@ if (HAVE_PTY_H)
   )
 
   target_link_libraries(klee-replay PRIVATE kleeBasic ${LIBCAP_LIBRARIES})
-  # FIXME: This isn't quite right. This is actually an implementation detail
-  # of glibc not Linux. Really we need to test if we can use `openpty()`
-  # with/without an additional library.
-  if ("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
-    if (NOT LIBUTIL_LIBRARIES)
-      message(FATAL_ERROR "klee-replay needs libutil under Linux")
-    endif()
+  if (openpty_in_libutil)
     target_link_libraries(klee-replay PRIVATE ${LIBUTIL_LIBRARIES})
-  endif()
-install(TARGETS klee-replay RUNTIME DESTINATION bin)
+  endif (openpty_in_libutil)
+  install(TARGETS klee-replay RUNTIME DESTINATION bin)
 else()
-  message(WARNING "Not building klee-replay due to missing \"pty.h\" header file")
+  message(WARNING "Not building klee-replay due to missing library for pty functions.")
 endif()
diff --git a/tools/klee-replay/file-creator.c b/tools/klee-replay/file-creator.c
index 505fb8d7..714a1112 100644
--- a/tools/klee-replay/file-creator.c
+++ b/tools/klee-replay/file-creator.c
@@ -17,13 +17,23 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <termios.h>
-#include <pty.h>
 #include <time.h>
 #include <sys/wait.h>
 #include <sys/time.h>
 #include <assert.h>
 
+#ifdef HAVE_PTY_H
+#include <pty.h>
+#elif defined(HAVE_UTIL_H)
+#include <util.h>
+#endif
+
+#if defined(__APPLE__)
+#include <sys/termios.h>
+#else
+#include <termios.h>
+#endif
+
 static void create_file(int target_fd, 
                        const char *target_name, 
                        exe_disk_file_t *dfile,
@@ -105,7 +115,9 @@ static int create_char_dev(const char *fname, exe_disk_file_t *dfile,
   ts->c_oflag = 5;
   ts->c_cflag = 1215;
   ts->c_lflag = 35287;
+#ifndef __APPLE__
   ts->c_line = 0;
+#endif
   ts->c_cc[0] = '\x03';
   ts->c_cc[1] = '\x1c';
   ts->c_cc[2] = '\x7f';
@@ -152,16 +164,20 @@ static int create_char_dev(const char *fname, exe_disk_file_t *dfile,
 
       fprintf(stderr, "note: pty slave: setting raw mode\n");
       {
-        struct termio mode;
+        struct termios mode;
         
-        int res = ioctl(aslave, TCGETA, &mode);
+        int res = tcgetattr(aslave, &mode);
         assert(!res);
         mode.c_iflag = IGNBRK;
+#if defined(__APPLE__)
+        mode.c_oflag &= ~(ONLCR | OCRNL | ONLRET);
+#else
         mode.c_oflag &= ~(OLCUC | ONLCR | OCRNL | ONLRET);
+#endif
         mode.c_lflag = 0;
         mode.c_cc[VMIN] = 1;
         mode.c_cc[VTIME] = 0;
-        res = ioctl(aslave, TCSETA, &mode);
+        res = tcsetattr(aslave, TCSANOW, &mode);
         assert(res == 0);
       }
 
diff --git a/tools/klee-replay/klee-replay.c b/tools/klee-replay/klee-replay.c
index 6b4fb8f4..150a82e8 100644
--- a/tools/klee-replay/klee-replay.c
+++ b/tools/klee-replay/klee-replay.c
@@ -22,9 +22,17 @@
 #include <errno.h>
 #include <time.h>
 #include <unistd.h>
-#include <sys/signal.h>
+
 #include <sys/wait.h>
 
+#if defined(__APPLE__)
+#include <signal.h>
+#define fgetc_unlocked(x) fgetc (x)
+#define fputc_unlocked(x,y) fputc (x,y)
+#else
+#include <sys/signal.h>
+#endif
+
 #ifdef HAVE_SYS_CAPABILITY_H
 #include <sys/capability.h>
 #endif
diff --git a/tools/klee-replay/klee-replay.h b/tools/klee-replay/klee-replay.h
index 668b5e3e..09cb6603 100644
--- a/tools/klee-replay/klee-replay.h
+++ b/tools/klee-replay/klee-replay.h
@@ -13,6 +13,7 @@
 #define _LARGEFILE64_SOURCE
 #define _FILE_OFFSET_BITS 64
 
+#include "klee/Config/config.h"
 // FIXME: This is a hack.
 #include "../../runtime/POSIX/fd.h"
 #include <sys/time.h>