From 55afe9646f9324a4603ab4f117b544e2db8f5fc1 Mon Sep 17 00:00:00 2001 From: tevador Date: Fri, 21 Dec 2018 21:09:55 +0100 Subject: [PATCH] Debuggable assembly generator --- src/CompiledVirtualMachine.cpp | 1 + src/CompiledVirtualMachine.hpp | 2 +- src/common.hpp | 2 +- src/executeProgram-win64.asm | 15 +++++++++------ src/main.cpp | 22 ++++++++++++++++++++++ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/CompiledVirtualMachine.cpp b/src/CompiledVirtualMachine.cpp index 35e4ba5..5ef3cd7 100644 --- a/src/CompiledVirtualMachine.cpp +++ b/src/CompiledVirtualMachine.cpp @@ -49,6 +49,7 @@ namespace RandomX { } void CompiledVirtualMachine::execute() { + //executeProgram(reg, mem, scratchpad, readDataset); compiler.getProgramFunc()(reg, mem, scratchpad); #ifdef TRACE for (int32_t i = InstructionCount - 1; i >= 0; --i) { diff --git a/src/CompiledVirtualMachine.hpp b/src/CompiledVirtualMachine.hpp index c2e108d..e7e9299 100644 --- a/src/CompiledVirtualMachine.hpp +++ b/src/CompiledVirtualMachine.hpp @@ -34,9 +34,9 @@ namespace RandomX { return compiler.getCode(); } private: - JitCompilerX86 compiler; #ifdef TRACE convertible_t tracepad[InstructionCount]; #endif + JitCompilerX86 compiler; }; } \ No newline at end of file diff --git a/src/common.hpp b/src/common.hpp index 04333a5..32743dd 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -106,6 +106,6 @@ namespace RandomX { typedef void(*ProgramFunc)(RegisterFile&, MemoryRegisters&, convertible_t*); extern "C" { - void executeProgram(RegisterFile& registerFile, MemoryRegisters& memory, convertible_t* scratchpad); + void executeProgram(RegisterFile&, MemoryRegisters&, convertible_t*, DatasetReadFunc); } } \ No newline at end of file diff --git a/src/executeProgram-win64.asm b/src/executeProgram-win64.asm index a125409..0d92b60 100644 --- a/src/executeProgram-win64.asm +++ b/src/executeProgram-win64.asm @@ -71,7 +71,7 @@ executeProgram PROC push r13 push r14 push r15 - sub rsp, 72 + sub rsp, 64 movdqu xmmword ptr [rsp+48], xmm6 movdqu xmmword ptr [rsp+32], xmm7 movdqu xmmword ptr [rsp+16], xmm8 @@ -81,6 +81,7 @@ executeProgram PROC push rcx ; RegisterFile& registerFile mov rbx, rdx ; MemoryRegisters& memory mov rsi, r8 ; convertible_t& scratchpad + push r9 mov rbp, rsp ; beginning of VM stack mov rdi, 1048576 ; number of VM instructions to execute @@ -115,6 +116,7 @@ rx_finish: ; save VM register values pop rcx + pop rcx mov qword ptr [rcx+0], r8 mov qword ptr [rcx+8], r9 mov qword ptr [rcx+16], r10 @@ -137,7 +139,7 @@ rx_finish: movdqu xmm8, xmmword ptr [rsp+16] movdqu xmm7, xmmword ptr [rsp+32] movdqu xmm6, xmmword ptr [rsp+48] - add rsp, 72 + add rsp, 64 pop r15 pop r14 pop r13 @@ -150,11 +152,12 @@ rx_finish: ; return ret 0 -rx_read_dataset_light: - push rdx +rx_read_dataset: + push r8 push r9 push r10 push r11 + mov rdx, rbx movd qword ptr [rsp - 8], xmm1 movd qword ptr [rsp - 16], xmm2 sub rsp, 48 @@ -165,10 +168,10 @@ rx_read_dataset_light: pop r11 pop r10 pop r9 - pop rdx + pop r8 ret 0 -rx_read_dataset: +rx_read_dataset_full: mov edx, dword ptr [rbx] ; ma mov rax, qword ptr [rbx+8] ; dataset mov rax, qword ptr [rax+rdx] diff --git a/src/main.cpp b/src/main.cpp index b486c1a..fed4739 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -110,6 +110,22 @@ private: std::atomic hash[4]; }; +void generateAsm(int nonce) { + uint64_t hash[4]; + unsigned char blockTemplate[] = { + 0x07, 0x07, 0xf7, 0xa4, 0xf0, 0xd6, 0x05, 0xb3, 0x03, 0x26, 0x08, 0x16, 0xba, 0x3f, 0x10, 0x90, 0x2e, 0x1a, 0x14, + 0x5a, 0xc5, 0xfa, 0xd3, 0xaa, 0x3a, 0xf6, 0xea, 0x44, 0xc1, 0x18, 0x69, 0xdc, 0x4f, 0x85, 0x3f, 0x00, 0x2b, 0x2e, + 0xea, 0x00, 0x00, 0x00, 0x00, 0x77, 0xb2, 0x06, 0xa0, 0x2c, 0xa5, 0xb1, 0xd4, 0xce, 0x6b, 0xbf, 0xdf, 0x0a, 0xca, + 0xc3, 0x8b, 0xde, 0xd3, 0x4d, 0x2d, 0xcd, 0xee, 0xf9, 0x5c, 0xd2, 0x0c, 0xef, 0xc1, 0x2f, 0x61, 0xd5, 0x61, 0x09 + }; + int* noncePtr = (int*)(blockTemplate + 39); + *noncePtr = nonce; + blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0); + RandomX::AssemblyGeneratorX86 asmX86; + asmX86.generateProgram(hash); + asmX86.printCode(std::cout); +} + void mine(RandomX::VirtualMachine* vm, std::atomic& atomicNonce, AtomicHash& result, int noncesCount, int thread) { uint64_t hash[4]; unsigned char blockTemplate[] = { @@ -128,6 +144,7 @@ void mine(RandomX::VirtualMachine* vm, std::atomic& atomicNonce, AtomicHash int spIndex = ((uint8_t*)hash)[24] | ((((uint8_t*)hash)[25] & 63) << 8); vm->initializeScratchpad(spIndex); vm->initializeProgram(hash); + //dump((char*)((RandomX::CompiledVirtualMachine*)vm)->getProgram(), RandomX::CodeSize, "code-1337-jmp.txt"); vm->execute(); vm->getResult(hash); result.xorWith(hash); @@ -150,6 +167,11 @@ int main(int argc, char** argv) { readIntOption("--threads", argc, argv, threadCount, 1); readIntOption("--nonces", argc, argv, programCount, 1000); + if (genAsm) { + generateAsm(programCount); + return 0; + } + std::atomic atomicNonce(0); AtomicHash result; std::vector vms;