root/mtcp/NOTES-x86_64/example.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test_mmap
  2. main

   1 // FIX __set_errno BELOW, AND 6 ARG CALL FOR i386
   2 //    mtcp_sy.h:  static int mtcp_sy_errno;
   3 //    and for inline, will be available in same file only --- okay for typical use
   4 // Doesn't handle __PIC__ (position independent code).
   5 // LOOK AT INTERNAL_SYSCALL  AND DEFINE MISSING ARGS:  EXTRAVAR_6, LOADARGS_6, etc.
   6 //
   7 // TESTING:  gcc -pie -fpie example.c
   8 // TESTING:  gcc -DSHARED=1 -shared -o shared.so -pie -fpie example.c
   9 //           gcc -DEXECUTABLE=1 -shared -o shared.so -pie -fpie example.c
  10 
  11 #include <sys/types.h>
  12 #include <unistd.h>
  13 #include <errno.h>
  14 
  15 #include <stdio.h> /* for printf */
  16 #include <sys/mman.h>
  17 
  18 // Rename it for cosmetic reasons.
  19 #define mtcp_inline_syscall(name, num_args, args...) \
  20                                         INLINE_SYSCALL(name, num_args, args)
  21 
  22 // sysdep-x86_64.h:
  23 //   From glibc-2.5/sysdeps/unix/sysv/linux/x86_64/sysdep.h :  (define INLINE_SYSCALL)
  24 // sysdep-i386.h:
  25 //   Or glibc-2.5/sysdeps/unix/sysv/linux/i386/sysdep.h :  (define INLINE_SYSCALL)
  26 // But all further includes from sysdep-XXX.h have been commented out.
  27 #ifdef __i386__
  28 // THIS CASE fOR i386 NEEDS PATCHING FOR 6 ARGUMENT CASE, SUCH AS MMAP.
  29 // IT ONLY TRIES TO HANDLE UP TO 5 ARGS.
  30 # include "sysdep-i386.h"
  31 
  32 #ifdef __PIC__
  33 # define EXTRAVAR_6 int _xv;
  34 # define ASMFMT_6(arg1, arg2, arg3, arg4, arg5) \
  35         , "0" (arg1), "m" (_xv), "c" (arg2), "d" (arg3), "S" (arg4), \
  36         "D" (arg5), "0" (arg6)
  37 # if defined I386_USE_SYSENTER && defined SHARED
  38 #  define LOADARGS_6 \
  39     "movl %%ebx, %4\n\t"  \
  40     "movl %3, %%ebx\n\t" \
  41     "push %%ebp\n\t" "movl %%eax,%%ebp\n\t"
  42 #  define RESTOREARGS_6 \
  43     "movl %4, %%ebx" \
  44     "pop %%ebp\n\t"
  45 # else
  46 #  define LOADARGS_6 \
  47     "movl %%ebx, %3\n\t" \
  48     "movl %2, %%ebx\n\t" \
  49     "push %%ebp\n\t" "movl %%eax,%%ebp\n\t"
  50 #  define RESTOREARGS_6 \
  51     "movl %3, %%ebx" \
  52     "pop %%ebp\n\t"
  53 # endif
  54 #else
  55 # define EXTRAVAR_6
  56 # define LOADARGS_6 "push %%ebp\n\t" "movl %%eax,%%ebp\n\t"
  57 # define RESTOREARGS_6  "pop %%ebp\n\t"
  58 # define ASMFMT_6(arg1, arg2, arg3, arg4, arg5, arg6) \
  59        , "b" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5), "0" (arg6)
  60 #endif
  61 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6)             \
  62   long int __arg6 = (long) (a6);                        \
  63   LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  64 #define LOAD_REGS_6                                     \
  65   register long int _a6 asm ("r9") = __arg6;            \
  66   LOAD_REGS_5
  67 #define ASM_ARGS_6      ASM_ARGS_5, "r" (_a6)
  68 #endif
  69 #ifdef __x86_64__
  70 # include "sysdep-x86_64.h"
  71 #endif
  72 // #include <sysdeps/unix/x86_64/sysdep.h>
  73 #include <asm/unistd.h> /* translate __NR_getpid to syscall # using i386 or x86_64 */
  74 # define __set_errno(Val) errno = (Val)
  75 
  76 #ifdef SHARED
  77 void test_mmap() {
  78   FILE *stream = fopen("/etc/passwd", "r");
  79   void * ptr = (void *)INLINE_SYSCALL(mmap, 6,
  80                0, 1024, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, fileno(stream), 0);
  81   if (ptr == MAP_FAILED) { printf("mmap: %s\n", strerror(errno)); exit(1); }
  82   fclose(stream);
  83 
  84   printf("mmap pointer: %x\n", ptr);
  85 }
  86 #endif
  87 
  88 #ifndef SHARED
  89 int main() {
  90   pid_t x = INLINE_SYSCALL (getpid, 0);
  91   FILE *stream = fopen("/etc/passwd", "r");
  92   void * ptr = (void *)INLINE_SYSCALL(mmap, 6,
  93                0, 1024, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, fileno(stream), 0);
  94   fclose(stream);
  95   if (ptr == MAP_FAILED) { printf("mmap: %s\n", strerror(errno)); exit(1); }
  96 
  97   printf("mmap pointer: %x\n", ptr);
  98   printf("pid: %d\n", x);
  99   printf("pid: %d\n", mtcp_inline_syscall(getpid, 0));
 100   // printf("pid: %d\n", getpid());
 101 
 102   return 0;
 103 }
 104 #endif

/* [<][>][^][v][top][bottom][index][help] */