Guest User

Untitled

a guest
Jul 10th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. //#include "guess_max_fn.c"
  5.  
  6. FILE* fd;
  7. int max_id = 1;
  8. int guess_max(double x, int N, int count) {
  9.     size_t* rbp = NULL;
  10.     asm ("mov %%rbp, %0"
  11.              : "=r" (rbp)
  12.              :);
  13.  
  14.     // lets do it only with first loop iteration
  15.     if(count == 1) {
  16.  
  17.         // We need to iterate between ques_max stack frame and main stack frame,
  18.         // and find file descriptor in local variables.
  19.         //
  20.         // How stacks would be looks in this situation on 32 bits platform? Something like this:
  21.         // [ebp+00] main_ebp(pointer to stack frame of main function)
  22.         // [ebp+04] return address           //----------
  23.         // there starts arguments            // ↑
  24.         // [ebp+08] double x                 // i should skip this but whatever
  25.         // [ebp+0C] int N                    // ↓
  26.         // [ebp+10] int count                //----------
  27.         // local variables start here
  28.         // [ebp+14]   int N ????
  29.         // ...
  30.         // [ebp+1C+x] FILE* f;               //<--- we need to find this
  31.         // ...
  32.         // [main_ebp]
  33.         //
  34.         // How to check if variable is a file descriptor?
  35.         // Lets see the _IO_FILE declaration in libio.h:
  36.         // struct _IO_FILE {
  37.         //   int _flags;                     /* High-order word is _IO_MAGIC; rest is flags. */
  38.         //   ...
  39.         // };
  40.         // here we go, all what we need to do is check first word in _flags.
  41.  
  42.         size_t* guess_max_rbp = rbp;
  43.         size_t* main_rbp = (size_t*)rbp[0];
  44.  
  45.         // local_var is increasing by sizeof(void*) because stack should be alignment of pointer size
  46.         for(size_t* local_var = guess_max_rbp; local_var < main_rbp; local_var += sizeof(void*)) {
  47.             // local_var is a valid address? (dirty way for !IsBadCodePtr() from WinAPI)
  48.             if(write(1, local_var, 1) == 1) {
  49.                 if((*local_var & 0xFFFF0000) == _IO_MAGIC) {
  50.                     fd = (FILE*)local_var;
  51.                 }
  52.             }
  53.         }
  54.  
  55.         if(fd) {
  56.             //save stream position
  57.             long stream_position = ftell(fd);
  58.  
  59.             //C&P from main
  60.             double real_max = x;
  61.             for(int count = 2; count <= N; count++) { //skip first value
  62.                 double x;
  63.                 fscanf(fd, "%lf", &x);
  64.                 if (real_max < x) {
  65.                     real_max = x;
  66.                     max_id = count;
  67.                 }
  68.             }
  69.             //restore stream position
  70.             fseek(fd, stream_position, SEEK_SET);
  71.         }
  72.  
  73.     }
  74.  
  75.     if(count == max_id)
  76.         return 1;
  77.  
  78.     return 0;
  79. }
Add Comment
Please, Sign In to add comment