Debian Codegraph @

src/crypto/CryptonightR_JIT.c

raw symbols SCIP
1 #include <assert.h>
2 #include <stddef.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <unistd.h>
7 #if !(defined(_MSC_VER) || defined(__MINGW32__))
8 #include <sys/mman.h>
9 #endif
10
11 #include "int-util.h"
12 #include "hash-ops.h"
13 #include "variant4_random_math.h"
14 #include "CryptonightR_JIT.h"
15 #include "CryptonightR_template.h"
16
17 static const uint8_t prologue[] = {
18 #if defined __i386 || defined __x86_64__
19 0x4C, 0x8B, 0xD7, // mov r10, rdi
20 0x53, // push rbx
21 0x55, // push rbp
22 0x41, 0x57, // push r15
23 0x4C, 0x8B, 0xDC, // mov r11, rsp
24 0x41, 0x8B, 0x1A, // mov ebx, DWORD PTR [r10]
25 0x41, 0x8B, 0x72, 0x04, // mov esi, DWORD PTR [r10+4]
26 0x41, 0x8B, 0x7A, 0x08, // mov edi, DWORD PTR [r10+8]
27 0x41, 0x8B, 0x6A, 0x0C, // mov ebp, DWORD PTR [r10+12]
28 0x41, 0x8B, 0x62, 0x10, // mov esp, DWORD PTR [r10+16]
29 0x45, 0x8B, 0x7A, 0x14, // mov r15d, DWORD PTR [r10+20]
30 0x41, 0x8B, 0x42, 0x18, // mov eax, DWORD PTR [r10+24]
31 0x41, 0x8B, 0x52, 0x1C, // mov edx, DWORD PTR [r10+28]
32 0x45, 0x8B, 0x4A, 0x20, // mov r9d, DWORD PTR [r10+32]
33 #endif
34 };
35
36 static const uint8_t epilogue[] = {
37 #if defined __i386 || defined __x86_64__
38 0x49, 0x8B, 0xE3, // mov rsp, r11
39 0x41, 0x89, 0x1A, // mov DWORD PTR [r10], ebx
40 0x41, 0x89, 0x72, 0x04, // mov DWORD PTR [r10+4], esi
41 0x41, 0x89, 0x7A, 0x08, // mov DWORD PTR [r10+8], edi
42 0x41, 0x89, 0x6A, 0x0C, // mov DWORD PTR [r10+12], ebp
43 0x41, 0x5F, // pop r15
44 0x5D, // pop rbp
45 0x5B, // pop rbx
46 0xC3, // ret
47 #endif
48 };
49
50 #define APPEND_CODE(src, size) \
51 do { \
52 if (JIT_code + (size) > JIT_code_end) \
53 return -1; \
54 memcpy(JIT_code, (src), (size)); \
55 JIT_code += (size); \
56 } while (0)
57
58 int v4_generate_JIT_code(const struct V4_Instruction* code, v4_random_math_JIT_func buf, const size_t buf_size)
59 {
60 #if defined __i386 || defined __x86_64__
61 uint8_t* JIT_code = (uint8_t*) buf;
62 const uint8_t* JIT_code_end = JIT_code + buf_size;
63
64 #if !(defined(_MSC_VER) || defined(__MINGW32__))
65 if (mprotect((void*)buf, buf_size, PROT_READ | PROT_WRITE))
66 return -1;
67 #endif
68
69 APPEND_CODE(prologue, sizeof(prologue));
70
71 uint32_t prev_rot_src = 0xFFFFFFFFU;
72
73 for (int i = 0;; ++i)
74 {
75 const struct V4_Instruction inst = code[i];
76 if (inst.opcode == RET)
77 break;
78
79 const uint8_t opcode = (inst.opcode == MUL) ? inst.opcode : (inst.opcode + 2);
80
81 const uint32_t a = inst.dst_index;
82 const uint32_t b = inst.src_index;
83 const uint8_t c = opcode | (inst.dst_index << V4_OPCODE_BITS) | (((inst.src_index == 8) ? inst.dst_index : inst.src_index) << (V4_OPCODE_BITS + V4_DST_INDEX_BITS));
84
85 switch (inst.opcode)
86 {
87 case ROR:
88 case ROL:
89 if (b != prev_rot_src)
90 {
91 prev_rot_src = b;
92 const uint8_t* p1 = (const uint8_t*) instructions_mov[c];
93 const uint8_t* p2 = (const uint8_t*) instructions_mov[c + 1];
94 APPEND_CODE(p1, p2 - p1);
95 }
96 break;
97 }
98
99 if (a == prev_rot_src)
100 prev_rot_src = 0xFFFFFFFFU;
101
102 const uint8_t* p1 = (const uint8_t*) instructions[c];
103 const uint8_t* p2 = (const uint8_t*) instructions[c + 1];
104 APPEND_CODE(p1, p2 - p1);
105
106 if (inst.opcode == ADD)
107 *(uint32_t*)(JIT_code - 4) = inst.C;
108 }
109
110 APPEND_CODE(epilogue, sizeof(epilogue));
111
112 #if !(defined(_MSC_VER) || defined(__MINGW32__))
113 if (mprotect((void*)buf, buf_size, PROT_READ | PROT_EXEC))
114 return -1;
115 #endif
116
117 __builtin___clear_cache((char*)buf, (char*)JIT_code);
118
119 return 0;
120 #else
121 return -1;
122 #endif
123 }