Advertisement
filashkov

Untitled

Jan 5th, 2022
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define INT_TYPE 8
  5. #define FLOAT_TYPE -8
  6.  
  7. struct ValueAndType
  8. {
  9.     unsigned long long value;
  10.     unsigned long long another_value;
  11.     long long type;
  12. };
  13.  
  14. void
  15. my_test(const char* s, int arg1, int arg2, char arg3, short arg4, int arg5, int arg6)
  16. {
  17.     puts(s);
  18.     printf("%d\n", arg1);
  19.     printf("%d\n", arg2);
  20.     printf("%c\n", arg3);
  21.     printf("%d\n", arg4);
  22.     printf("%d\n", arg5);
  23.     printf("%d\n", arg6);
  24. }
  25.  
  26. int
  27. test_double(const char* s, double arg1, double arg2, double arg3, double arg4, double arg5, double arg6, double arg7, double arg8)
  28. {
  29.     //puts(s);
  30.    
  31.     printf("%lf\n", arg1);
  32.     /*printf("%lf\n", arg2);
  33.     printf("%lf\n", arg3);
  34.     printf("%lf\n", arg4);
  35.     printf("%lf\n", arg5);
  36.     printf("%lf\n", arg6);
  37.     printf("%lf\n", arg7);
  38.     printf("%lf\n", arg8);
  39.     */
  40.     return arg1 + arg8;
  41. }
  42.  
  43. int
  44. old_main()
  45. {
  46.     unsigned long long rax_value = 0;
  47.     //unsigned long long args[] = { (unsigned long long)printf, (unsigned long long)"Hi! %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 48 };
  48.     //unsigned long long args[] = { (unsigned long long)printf, (unsigned long long)"Hi! %d %d %d %d %d %lf\n", 1, 2, 3, 4, 5, (unsigned long long)((double)5.5) };
  49.     //unsigned long long args[] = { (unsigned long long)my_test, (unsigned long long)"Hi! %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 48 };
  50.     //unsigned long long args[] = { (unsigned long long)printf, (unsigned long long)"Hi! %d %d %d %d %d 1: %lf 2: %lf 3: %lf 4: %lf 5: %lf 6: %lf 7: %lf 8: %lf %lf %lf %lf %lf\n", 1, 2, 3, 4, 5, (unsigned long long)((double)5.5) };
  51.     //unsigned long long args[] = { (unsigned long long)test_double, (unsigned long long)"Hi! %d %d %d %d %d 1: %lf 2: %lf 3: %lf 4: %lf 5: %lf 6: %lf 7: %lf 8: %lf %lf %lf %lf %lf\n", 1, 2, 3, 4, 5, (unsigned long long)((double)5.5) };
  52.     //unsigned long long args[] = { 0, (unsigned long long)printf, (unsigned long long)"Hi! %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 48 };
  53.     //unsigned long long args[] = { 0, (unsigned long long)printf, (unsigned long long)"Hi! %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 100, 77, 75, 48, 56, 64, 12, 75, 77 };
  54.     //unsigned long long args[] = { 0, (unsigned long long)printf, (unsigned long long)"Hi! %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n", 1, 2, 3, 4, 5, 100, 77, 75, 48, 56, 64, 12, 75, 77 };
  55.     unsigned long long args_row[] = { 4684295950315513862, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  56.         0, (unsigned long long)printf, (unsigned long long)"Hi! double = %lf %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 111, 100, 77, 75, 48, 56, 64, 12, 75, 77 };
  57.     unsigned long long* args = args_row + 2 * 8 + 1;
  58.     long long additional_args_quantity = sizeof(args_row) / sizeof(*args_row) - 2 * 8 - 1 - 1 - 1 - 6;
  59.     if (additional_args_quantity < 0) {
  60.         additional_args_quantity = 0;
  61.     }
  62.     args[0] = additional_args_quantity; // Количество дополнительных аргументов, которые пойдут на стек
  63.     printf("args[0] = %llu\n", args[0]);
  64.     asm volatile(
  65.         "movsd -136(%%rax), %%xmm0;"
  66.         "movsd -120(%%rax), %%xmm1;"
  67.         "movsd -104(%%rax), %%xmm2;"
  68.         "movsd -88(%%rax), %%xmm3;"
  69.         "movsd -72(%%rax), %%xmm4;"
  70.         "movsd -56(%%rax), %%xmm5;"
  71.         "movsd -40(%%rax), %%xmm6;"
  72.         "movsd -24(%%rax), %%xmm7;"
  73.         "mov $0, %%rdi;"
  74.         //"pushq %%rax;"
  75.         "for_begin_label:"
  76.             "cmpq %%rdi, (%%rax);"
  77.                 "jna for_end_label;"
  78.                     "pushq 64(%%rax, %%rdi, 8);"
  79.                      //"pushq $42;"
  80.                     "add $1, %%rdi;"
  81.             "jmp for_begin_label;"
  82.         "for_end_label:"
  83.         //"mov %%rsp, %%rsi;"
  84.         "mov 16(%%rax), %%rdi;"
  85.         "movq 24(%%rax), %%rsi;"
  86.         "movq 32(%%rax), %%rdx;"
  87.         "movq 40(%%rax), %%rcx;"
  88.         "movq 48(%%rax), %%r8;"
  89.         "movq 56(%%rax), %%r9;"
  90.         "movq %%rax, %%rbx;"
  91.         "addq $8, %%rbx;"
  92.         "mov -8(%%rax), %%rax;"
  93.         /*"notq %%mm0;"*/
  94.         /*"pcmpeqd %%xmm0, %%xmm0;"*/
  95.         "call *(%%rbx);"
  96.         "subq $8, %%rbx;"
  97.         "movq (%%rbx), %%rbx;"
  98.         "shlq $3, %%rbx;"
  99.         "add %%rbx, %%rsp;"
  100.         //"popq %%rbx;"
  101.         : "=a"(rax_value)
  102.         : "a"(args)
  103.         : "rbx"
  104.     );
  105.     printf("Out: %lld\n", rax_value);
  106.     return 0;
  107. }
  108.  
  109. unsigned long long*
  110. argswt2args(unsigned long long func_address, struct ValueAndType* args_with_types, size_t len)
  111. {
  112.     int int_type_counter = 0;
  113.     int float_type_counter = 0;
  114.  
  115.     for (int i = 0; i < len; i++) {
  116.         if (args_with_types[i].type > 0) {
  117.             int_type_counter += 1;
  118.         } else {
  119.             float_type_counter += 1;
  120.             printf("%d ", i);
  121.         }
  122.     }
  123.     puts("");
  124.  
  125.     int on_stack_int_type_counter = (int_type_counter - 6 >= 0) ? int_type_counter - 6 : 0;
  126.     int on_stack_float_type_counter = (float_type_counter - 8 >= 0) ? float_type_counter - 8 : 0;
  127.     int on_stack_alignment = (on_stack_int_type_counter + on_stack_float_type_counter) % 2;
  128.  
  129.     /*
  130.     printf("int_type_counter = %d \n", int_type_counter);
  131.     printf("float_type_counter = %d \n", float_type_counter);
  132.     printf("on_stack_int_type_counter = %d \n", on_stack_int_type_counter);
  133.     printf("on_stack_float_type_counter = %d \n", on_stack_float_type_counter);
  134.     */
  135.  
  136.     size_t total_size = 2 * 8 + 1 + 1 + 1 + 6 + on_stack_alignment + on_stack_int_type_counter + on_stack_float_type_counter;
  137.     unsigned long long* result_row = malloc(total_size * (sizeof(*result_row)));
  138.     unsigned long long* result = result_row + 2 * 8 + 1;
  139.    
  140.     result[-1] = float_type_counter;
  141.     result[0] = on_stack_int_type_counter + on_stack_float_type_counter + on_stack_alignment;
  142.     result[1] = func_address;
  143.  
  144.     int current_int_type_counter = 0;
  145.     int current_float_type_counter = 0;
  146.     int current_stack_position = total_size - 2 * 8 - 1 - 1;
  147.     for (int i = 0; i < len; i++) {
  148.         if (args_with_types[i].type > 0) {
  149.             if (current_int_type_counter < 6) {
  150.                 result[2 + current_int_type_counter] = args_with_types[i].value;
  151.             } else {
  152.                 result[current_stack_position] = args_with_types[i].value;
  153.                 current_stack_position -= 1;
  154.             }
  155.             current_int_type_counter += 1;
  156.         } else {
  157.             if (current_float_type_counter < 8) {
  158.                 result_row[2 * current_float_type_counter] = args_with_types[i].value;
  159.             } else {
  160.                 result[current_stack_position] = args_with_types[i].value;
  161.                 current_stack_position -= 1;
  162.             }
  163.             current_float_type_counter += 1;
  164.         }
  165.     }
  166.    
  167.     return result;
  168. }
  169.  
  170. int
  171. main()
  172. {
  173.     struct ValueAndType args_with_types[] = {
  174.         { (unsigned long long)"Shalom! %d %d %d %d %d %d %d %d %d %d\n", 0, INT_TYPE },
  175.         { 1, 0, INT_TYPE },
  176.         { 2, 0, INT_TYPE },
  177.         { 3, 0, INT_TYPE },
  178.         { 4, 0, INT_TYPE },
  179.         { 5, 0, INT_TYPE },
  180.         { 6, 0, INT_TYPE },
  181.         { 7, 0, INT_TYPE },
  182.         { 8, 0, INT_TYPE },
  183.         { 9, 0, INT_TYPE },
  184.         { 10, 0, INT_TYPE },
  185.         { 111, 0, FLOAT_TYPE },
  186.         { 112, 0, FLOAT_TYPE },
  187.         { 113, 0, FLOAT_TYPE },
  188.         { 114, 0, FLOAT_TYPE },
  189.         { 115, 0, FLOAT_TYPE },
  190.         { 116, 0, FLOAT_TYPE },
  191.         { 117, 0, FLOAT_TYPE },
  192.         { 118, 0, FLOAT_TYPE },
  193.         { 119, 0, FLOAT_TYPE }
  194.     };
  195.     size_t len = sizeof(args_with_types) / sizeof(*args_with_types);
  196.     unsigned long long* result = argswt2args((unsigned long long)printf, args_with_types, len);
  197.     int result_len = 2 * 8 + 1 + 1 + 1 + 6 + result[0] + result[0] % 2;
  198.     result -= 2 * 8 + 1;
  199.     for (int i = 0; i < result_len; i++) {
  200.         printf("%lld ", result[i]);
  201.     }
  202.     puts("");
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement