From 5601c7a672aa79ee0e5ef23a85dd1521168ade23 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 31 Aug 2017 02:28:33 +0300 Subject: [PATCH 001/389] Added Httpd class and API config options. --- CMakeLists.txt | 15 ++++++-- cmake/FindMHD.cmake | 39 ++++++++++++++++++++ src/App.cpp | 14 ++++++- src/App.h | 2 + src/Options.cpp | 90 ++++++++++++++++++++++++++++++++------------- src/Options.h | 6 +++ src/api/Httpd.cpp | 69 ++++++++++++++++++++++++++++++++++ src/api/Httpd.h | 49 ++++++++++++++++++++++++ src/config.json | 7 +++- 9 files changed, 260 insertions(+), 31 deletions(-) create mode 100644 cmake/FindMHD.cmake create mode 100644 src/api/Httpd.cpp create mode 100644 src/api/Httpd.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 021ffc30..2afcf70a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project(xmrig) option(WITH_LIBCPUID "Use Libcpuid" ON) option(WITH_AEON "CryptoNight-Lite support" ON) - +option(WITH_HTTPD "HTTP REST API" ON) include (CheckIncludeFile) @@ -191,6 +191,15 @@ if (HAVE_SYSLOG_H) set(SOURCES_SYSLOG src/log/SysLog.h src/log/SysLog.cpp) endif() +find_package(MHD REQUIRED) +if (WITH_HTTPD AND MHD_FOUND) + include_directories(${MHD_INCLUDE_DIRS}) + + set(HTTPD_SOURCES src/api/Httpd.h src/api/Httpd.cpp) +else() + add_definitions(/DXMRIG_NO_HTTPD) +endif() + include_directories(src) include_directories(src/3rdparty) include_directories(src/3rdparty/jansson) @@ -198,5 +207,5 @@ include_directories(${UV_INCLUDE_DIR}) add_subdirectory(src/3rdparty/jansson) -add_executable(xmrig ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG}) -target_link_libraries(xmrig jansson ${UV_LIBRARIES} ${EXTRA_LIBS} ${CPUID_LIB}) +add_executable(xmrig ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES}) +target_link_libraries(xmrig jansson ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) diff --git a/cmake/FindMHD.cmake b/cmake/FindMHD.cmake new file mode 100644 index 00000000..23e31c44 --- /dev/null +++ b/cmake/FindMHD.cmake @@ -0,0 +1,39 @@ +# - Try to find MHD +# Once done this will define +# +# MHD_FOUND - system has MHD +# MHD_INCLUDE_DIRS - the MHD include directory +# MHD_LIBRARY - Link these to use MHD + +find_path( + MHD_INCLUDE_DIR + NAMES microhttpd.h + DOC "microhttpd include dir" +) + +find_library( + MHD_LIBRARY + NAMES microhttpd microhttpd-10 libmicrohttpd libmicrohttpd-dll + DOC "microhttpd library" +) + +set(MHD_INCLUDE_DIRS ${MHD_INCLUDE_DIR}) +set(MHD_LIBRARIES ${MHD_LIBRARY}) + +# debug library on windows +# same naming convention as in qt (appending debug library with d) +# boost is using the same "hack" as us with "optimized" and "debug" +# official MHD project actually uses _d suffix +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + find_library( + MHD_LIBRARY_DEBUG + NAMES microhttpd_d microhttpd-10_d libmicrohttpd_d libmicrohttpd-dll_d + DOC "mhd debug library" + ) + set(MHD_LIBRARIES optimized ${MHD_LIBRARIES} debug ${MHD_LIBRARY_DEBUG}) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(mhd DEFAULT_MSG MHD_INCLUDE_DIR MHD_LIBRARY) +mark_as_advanced(MHD_INCLUDE_DIR MHD_LIBRARY) + diff --git a/src/App.cpp b/src/App.cpp index c172c045..a944fe84 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -46,6 +46,10 @@ # include "log/SysLog.h" #endif +#ifndef XMRIG_NO_HTTPD +# include "api/Httpd.h" +#endif + App *App::m_self = nullptr; @@ -53,6 +57,7 @@ App *App::m_self = nullptr; App::App(int argc, char **argv) : m_console(nullptr), + m_httpd(nullptr), m_network(nullptr), m_options(nullptr) { @@ -92,6 +97,9 @@ App::App(int argc, char **argv) : App::~App() { + uv_tty_reset_mode(); + + delete m_httpd; delete m_console; } @@ -116,13 +124,17 @@ int App::exec() Mem::allocate(m_options->algo(), m_options->threads(), m_options->doubleHash(), m_options->hugePages()); Summary::print(); +# ifndef XMRIG_NO_HTTPD + m_httpd = new Httpd(m_options->apiPort(), m_options->apiToken()); + m_httpd->start(); +# endif + Workers::start(m_options->affinity(), m_options->priority()); m_network->connect(); const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); uv_loop_close(uv_default_loop()); - uv_tty_reset_mode(); delete m_network; diff --git a/src/App.h b/src/App.h index 77bf973b..781f78f2 100644 --- a/src/App.h +++ b/src/App.h @@ -32,6 +32,7 @@ class Console; +class Httpd; class Network; class Options; @@ -56,6 +57,7 @@ private: static App *m_self; Console *m_console; + Httpd *m_httpd; Network *m_network; Options *m_options; uv_signal_t m_signal; diff --git a/src/Options.cpp b/src/Options.cpp index 36d570b4..120f58bf 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -90,32 +90,35 @@ static char const short_options[] = "a:c:khBp:Px:r:R:s:t:T:o:u:O:v:Vl:S"; static struct option const options[] = { - { "algo", 1, nullptr, 'a' }, - { "av", 1, nullptr, 'v' }, - { "background", 0, nullptr, 'B' }, - { "config", 1, nullptr, 'c' }, - { "cpu-affinity", 1, nullptr, 1020 }, - { "cpu-priority", 1, nullptr, 1021 }, - { "donate-level", 1, nullptr, 1003 }, - { "help", 0, nullptr, 'h' }, - { "keepalive", 0, nullptr ,'k' }, - { "log-file", 1, nullptr, 'l' }, - { "max-cpu-usage", 1, nullptr, 1004 }, - { "nicehash", 0, nullptr, 1006 }, - { "no-color", 0, nullptr, 1002 }, - { "no-huge-pages", 0, nullptr, 1009 }, - { "pass", 1, nullptr, 'p' }, - { "print-time", 1, nullptr, 1007 }, - { "retries", 1, nullptr, 'r' }, - { "retry-pause", 1, nullptr, 'R' }, - { "safe", 0, nullptr, 1005 }, - { "syslog", 0, nullptr, 'S' }, - { "threads", 1, nullptr, 't' }, - { "url", 1, nullptr, 'o' }, - { "user", 1, nullptr, 'u' }, - { "user-agent", 1, nullptr, 1008 }, - { "userpass", 1, nullptr, 'O' }, - { "version", 0, nullptr, 'V' }, + { "algo", 1, nullptr, 'a' }, + { "av", 1, nullptr, 'v' }, + { "background", 0, nullptr, 'B' }, + { "config", 1, nullptr, 'c' }, + { "cpu-affinity", 1, nullptr, 1020 }, + { "cpu-priority", 1, nullptr, 1021 }, + { "donate-level", 1, nullptr, 1003 }, + { "help", 0, nullptr, 'h' }, + { "keepalive", 0, nullptr ,'k' }, + { "log-file", 1, nullptr, 'l' }, + { "max-cpu-usage", 1, nullptr, 1004 }, + { "nicehash", 0, nullptr, 1006 }, + { "no-color", 0, nullptr, 1002 }, + { "no-huge-pages", 0, nullptr, 1009 }, + { "pass", 1, nullptr, 'p' }, + { "print-time", 1, nullptr, 1007 }, + { "retries", 1, nullptr, 'r' }, + { "retry-pause", 1, nullptr, 'R' }, + { "safe", 0, nullptr, 1005 }, + { "syslog", 0, nullptr, 'S' }, + { "threads", 1, nullptr, 't' }, + { "url", 1, nullptr, 'o' }, + { "user", 1, nullptr, 'u' }, + { "user-agent", 1, nullptr, 1008 }, + { "userpass", 1, nullptr, 'O' }, + { "version", 0, nullptr, 'V' }, + { "api-port", 1, nullptr, 3000 }, + { "api-access-token", 1, nullptr, 3001 }, + { "api-worker-id", 1, nullptr, 3002 }, { 0, 0, 0, 0 } }; @@ -153,6 +156,14 @@ static struct option const pool_options[] = { }; +static struct option const api_options[] = { + { "port", 1, nullptr, 3000 }, + { "access-token", 1, nullptr, 3001 }, + { "worker-id", 1, nullptr, 3002 }, + { 0, 0, 0, 0 } +}; + + static const char *algo_names[] = { "cryptonight", # ifndef XMRIG_NO_AEON @@ -188,10 +199,13 @@ Options::Options(int argc, char **argv) : m_ready(false), m_safe(false), m_syslog(false), + m_apiToken(nullptr), + m_apiWorkerId(nullptr), m_logFile(nullptr), m_userAgent(nullptr), m_algo(0), m_algoVariant(0), + m_apiPort(0), m_donateLevel(kDonateLevel), m_maxCpuUsage(75), m_printTime(60), @@ -302,6 +316,16 @@ bool Options::parseArg(int key, const char *arg) m_colors = false; break; + case 3001: /* --access-token */ + free(m_apiToken); + m_apiToken = strdup(arg); + break; + + case 3002: /* --worker-id */ + free(m_apiWorkerId); + m_apiWorkerId = strdup(arg); + break; + case 'r': /* --retries */ case 'R': /* --retry-pause */ case 't': /* --threads */ @@ -310,6 +334,7 @@ bool Options::parseArg(int key, const char *arg) case 1004: /* --max-cpu-usage */ case 1007: /* --print-time */ case 1021: /* --cpu-priority */ + case 3000: /* --api-port */ return parseArg(key, strtol(arg, nullptr, 10)); case 'B': /* --background */ @@ -432,6 +457,12 @@ bool Options::parseArg(int key, uint64_t arg) } break; + case 3000: /* --api-port */ + if (arg <= 65536) { + m_apiPort = (int) arg; + } + break; + default: break; } @@ -548,6 +579,13 @@ void Options::parseConfig(const char *fileName) } } + json_t *api = json_object_get(config, "api"); + if (json_is_object(api)) { + for (size_t i = 0; i < ARRAY_SIZE(api_options); i++) { + parseJSON(&api_options[i], api); + } + } + json_decref(config); } diff --git a/src/Options.h b/src/Options.h index e85441d9..582f393f 100644 --- a/src/Options.h +++ b/src/Options.h @@ -54,11 +54,14 @@ public: static inline Options* i() { return m_self; } static Options *parse(int argc, char **argv); + inline apiPort() const { return m_apiPort; } inline bool background() const { return m_background; } inline bool colors() const { return m_colors; } inline bool doubleHash() const { return m_doubleHash; } inline bool hugePages() const { return m_hugePages; } inline bool syslog() const { return m_syslog; } + inline const char *apiToken() const { return m_apiToken; } + inline const char *apiWorkerId() const { return m_apiWorkerId; } inline const char *logFile() const { return m_logFile; } inline const char *userAgent() const { return m_userAgent; } inline const std::vector &pools() const { return m_pools; } @@ -107,10 +110,13 @@ private: bool m_ready; bool m_safe; bool m_syslog; + char *m_apiToken; + char *m_apiWorkerId; char *m_logFile; char *m_userAgent; int m_algo; int m_algoVariant; + int m_apiPort; int m_donateLevel; int m_maxCpuUsage; int m_printTime; diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp new file mode 100644 index 00000000..9161d082 --- /dev/null +++ b/src/api/Httpd.cpp @@ -0,0 +1,69 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include + + +#include "api/Httpd.h" +#include "log/Log.h" + + +static const char kNotFound [] = "{\"error\":\"NOT_FOUND\"}"; +static const size_t kNotFoundSize = sizeof(kNotFound) - 1; + + +Httpd::Httpd(int port, const char *accessToken) : + m_accessToken(accessToken), + m_port(port), + m_daemon(nullptr) +{ +} + + +bool Httpd::start() +{ + if (!m_port) { + return false; + } + + m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, 4455, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); + if (!m_daemon) { + LOG_ERR("HTTP Daemon failed to start."); + return false; + } + + return true; +} + + +int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) +{ + struct MHD_Response *rsp = MHD_create_response_from_buffer(kNotFoundSize, (void*)kNotFound, MHD_RESPMEM_PERSISTENT); + + MHD_add_response_header(rsp, "Content-Type", "application/json"); + + const int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, rsp); + MHD_destroy_response(rsp); + return ret; +} diff --git a/src/api/Httpd.h b/src/api/Httpd.h new file mode 100644 index 00000000..32750beb --- /dev/null +++ b/src/api/Httpd.h @@ -0,0 +1,49 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __HTTPD_H__ +#define __HTTPD_H__ + + +#include + + +struct MHD_Daemon; +struct MHD_Connection; + + +class Httpd +{ +public: + Httpd(int port, const char *accessToken); + bool start(); + +private: + static int handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); + + const char *m_accessToken; + const int m_port; + MHD_Daemon *m_daemon; +}; + +#endif /* __HTTPD_H__ */ diff --git a/src/config.json b/src/config.json index afc2936b..5a727e48 100644 --- a/src/config.json +++ b/src/config.json @@ -22,5 +22,10 @@ "keepalive": true, "nicehash": false } - ] + ], + "api": { + "port": 0, + "access-token": null, + "worker-id": null + } } \ No newline at end of file From 71f06530df5fa6fb49dbf2622b1f27b666269694 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 31 Aug 2017 04:30:59 +0300 Subject: [PATCH 002/389] Added classes Api and ApiState. --- CMakeLists.txt | 4 +++ src/App.cpp | 3 ++ src/api/Api.cpp | 69 ++++++++++++++++++++++++++++++++++++++ src/api/Api.h | 48 +++++++++++++++++++++++++++ src/api/ApiState.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++ src/api/ApiState.h | 46 ++++++++++++++++++++++++++ src/api/Httpd.cpp | 24 +++++++++++--- src/version.h | 1 + 8 files changed, 270 insertions(+), 4 deletions(-) create mode 100644 src/api/Api.cpp create mode 100644 src/api/Api.h create mode 100644 src/api/ApiState.cpp create mode 100644 src/api/ApiState.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2afcf70a..817cdcd6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,8 @@ include (CheckIncludeFile) set(HEADERS src/3rdparty/align.h + src/api/Api.h + src/api/ApiState.h src/App.h src/Console.h src/Cpu.h @@ -60,6 +62,8 @@ set(HEADERS_CRYPTO ) set(SOURCES + src/api/Api.cpp + src/api/ApiState.cpp src/App.cpp src/Console.cpp src/log/ConsoleLog.cpp diff --git a/src/App.cpp b/src/App.cpp index a944fe84..a016d6b8 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -26,6 +26,7 @@ #include +#include "api/Api.h" #include "App.h" #include "Console.h" #include "Cpu.h" @@ -124,6 +125,8 @@ int App::exec() Mem::allocate(m_options->algo(), m_options->threads(), m_options->doubleHash(), m_options->hugePages()); Summary::print(); + Api::start(); + # ifndef XMRIG_NO_HTTPD m_httpd = new Httpd(m_options->apiPort(), m_options->apiToken()); m_httpd->start(); diff --git a/src/api/Api.cpp b/src/api/Api.cpp new file mode 100644 index 00000000..6fe1a838 --- /dev/null +++ b/src/api/Api.cpp @@ -0,0 +1,69 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "api/Api.h" +#include "api/ApiState.h" + + +ApiState *Api::m_state = nullptr; +char Api::m_buf[4096]; +uv_mutex_t Api::m_mutex; + + +bool Api::start() +{ + uv_mutex_init(&m_mutex); + m_state = new ApiState(); + + return true; +} + + +void Api::release() +{ + delete m_state; +} + + +const char *Api::get(const char *url, size_t *size, int *status) +{ + if (!m_state) { + *size = 0; + return nullptr; + } + + uv_mutex_lock(&m_mutex); + + const char *buf = m_state->get(url, size); + if (*size) { + memcpy(m_buf, buf, *size); + } + else { + *status = 500; + } + + uv_mutex_unlock(&m_mutex); + + return m_buf; +} diff --git a/src/api/Api.h b/src/api/Api.h new file mode 100644 index 00000000..db053139 --- /dev/null +++ b/src/api/Api.h @@ -0,0 +1,48 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __API_H__ +#define __API_H__ + + +#include + + +class ApiState; + + +class Api +{ +public: + static bool start(); + static void release(); + + static const char *get(const char *url, size_t *size, int *status); + +private: + static ApiState *m_state; + static char m_buf[4096]; + static uv_mutex_t m_mutex; +}; + +#endif /* __API_H__ */ diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp new file mode 100644 index 00000000..69044b14 --- /dev/null +++ b/src/api/ApiState.cpp @@ -0,0 +1,79 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + + +#include "api/ApiState.h" +#include "Cpu.h" +#include "Mem.h" +#include "Options.h" +#include "Platform.h" +#include "version.h" + + +ApiState::ApiState() +{ +} + + +ApiState::~ApiState() +{ +} + + +const char *ApiState::get(const char *url, size_t *size) const +{ + json_t *reply = json_object(); + + getMiner(reply); + + return finalize(reply, size); +} + + +const char *ApiState::finalize(json_t *reply, size_t *size) const +{ + *size = json_dumpb(reply, m_buf, sizeof(m_buf) - 1, JSON_INDENT(4)); + + json_decref(reply); + return m_buf; +} + + +void ApiState::getMiner(json_t *reply) const +{ + json_t *cpu = json_object(); + json_object_set(reply, "version", json_string(APP_VERSION)); + json_object_set(reply, "kind", json_string(APP_KIND)); + json_object_set(reply, "ua", json_string(Platform::userAgent())); + json_object_set(reply, "cpu", cpu); + json_object_set(reply, "algo", json_string(Options::i()->algoName())); + json_object_set(reply, "hugepages", json_boolean(Mem::isHugepagesEnabled())); + json_object_set(reply, "donate", json_integer(Options::i()->donateLevel())); + + json_object_set(cpu, "brand", json_string(Cpu::brand())); + json_object_set(cpu, "aes", json_boolean(Cpu::hasAES())); + json_object_set(cpu, "x64", json_boolean(Cpu::isX64())); + json_object_set(cpu, "sockets", json_integer(Cpu::sockets())); +} diff --git a/src/api/ApiState.h b/src/api/ApiState.h new file mode 100644 index 00000000..8b2a5089 --- /dev/null +++ b/src/api/ApiState.h @@ -0,0 +1,46 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __APISTATE_H__ +#define __APISTATE_H__ + + +#include "jansson.h" + + +class ApiState +{ +public: + ApiState(); + ~ApiState(); + + const char *get(const char *url, size_t *size) const; + +private: + const char *finalize(json_t *reply, size_t *size) const; + void getMiner(json_t *reply) const; + + mutable char m_buf[4096]; +}; + +#endif /* __APISTATE_H__ */ diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 9161d082..29baa2f5 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -25,12 +25,13 @@ #include +#include "api/Api.h" #include "api/Httpd.h" #include "log/Log.h" -static const char kNotFound [] = "{\"error\":\"NOT_FOUND\"}"; -static const size_t kNotFoundSize = sizeof(kNotFound) - 1; +static const char k500 [] = "{\"error\":\"INTERNAL_SERVER_ERROR\"}"; +static const size_t k500Size = sizeof(k500) - 1; Httpd::Httpd(int port, const char *accessToken) : @@ -59,11 +60,26 @@ bool Httpd::start() int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { - struct MHD_Response *rsp = MHD_create_response_from_buffer(kNotFoundSize, (void*)kNotFound, MHD_RESPMEM_PERSISTENT); + if (strcmp(method, "GET") != 0) { + return MHD_NO; + } + + struct MHD_Response *rsp; + + size_t size = 0; + int status = MHD_HTTP_OK; + const char *buf = Api::get(url, &size, &status); + + if (size) { + rsp = MHD_create_response_from_buffer(size, (void*) buf, MHD_RESPMEM_PERSISTENT); + } + else { + rsp = MHD_create_response_from_buffer(k500Size, (void*) k500, MHD_RESPMEM_PERSISTENT); + } MHD_add_response_header(rsp, "Content-Type", "application/json"); - const int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, rsp); + const int ret = MHD_queue_response(connection, status, rsp); MHD_destroy_response(rsp); return ret; } diff --git a/src/version.h b/src/version.h index b272e7b9..f1bf79cf 100644 --- a/src/version.h +++ b/src/version.h @@ -31,6 +31,7 @@ #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com" +#define APP_KIND "cpu" #define APP_VER_MAJOR 2 #define APP_VER_MINOR 3 From 30dd7d6fe43cd592aa863f6e9497d96106eca30a Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 31 Aug 2017 06:31:49 +0300 Subject: [PATCH 003/389] Added support for id and worker_id in API. --- src/api/ApiState.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++ src/api/ApiState.h | 5 ++++ 2 files changed, 61 insertions(+) diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp index 69044b14..32681842 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiState.cpp @@ -22,18 +22,41 @@ */ #include +#include + +#if _WIN32 +# include "winsock2.h" +#else +# include "unistd.h" +#endif #include "api/ApiState.h" #include "Cpu.h" #include "Mem.h" +#include "net/Job.h" #include "Options.h" #include "Platform.h" #include "version.h" +extern "C" +{ +#include "crypto/c_keccak.h" +} + + ApiState::ApiState() { + memset(m_workerId, 0, sizeof(m_workerId)); + if (Options::i()->apiWorkerId()) { + strncpy(m_workerId, Options::i()->apiWorkerId(), sizeof(m_workerId) - 1); + } + else { + gethostname(m_workerId, sizeof(m_workerId) - 1); + } + + genId(); } @@ -46,6 +69,7 @@ const char *ApiState::get(const char *url, size_t *size) const { json_t *reply = json_object(); + getIdentify(reply); getMiner(reply); return finalize(reply, size); @@ -61,6 +85,38 @@ const char *ApiState::finalize(json_t *reply, size_t *size) const } +void ApiState::genId() +{ + memset(m_id, 0, sizeof(m_id)); + + uv_interface_address_t *interfaces; + int count = 0; + + if (uv_interface_addresses(&interfaces, &count) < 0) { + return; + } + + for (int i = 0; i < count; i++) { + if (!interfaces[i].is_internal && interfaces[i].address.address4.sin_family == AF_INET) { + uint8_t hash[200]; + + keccak(reinterpret_cast(interfaces[i].phys_addr), static_cast(sizeof(interfaces[i].phys_addr)), hash, sizeof(hash)); + Job::toHex(hash, 8, m_id); + break; + } + } + + uv_free_interface_addresses(interfaces, count); +} + + +void ApiState::getIdentify(json_t *reply) const +{ + json_object_set(reply, "id", json_string(m_id)); + json_object_set(reply, "worker_id", json_string(m_workerId)); +} + + void ApiState::getMiner(json_t *reply) const { json_t *cpu = json_object(); diff --git a/src/api/ApiState.h b/src/api/ApiState.h index 8b2a5089..3876aa6c 100644 --- a/src/api/ApiState.h +++ b/src/api/ApiState.h @@ -38,8 +38,13 @@ public: private: const char *finalize(json_t *reply, size_t *size) const; + void genId(); + void getIdentify(json_t *reply) const; void getMiner(json_t *reply) const; + + char m_id[17]; + char m_workerId[128]; mutable char m_buf[4096]; }; From 1651b041de063738cc12d127dc5804fdf4474fb3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 1 Sep 2017 03:45:08 +0300 Subject: [PATCH 004/389] Added hashrate to reports. --- src/api/Api.cpp | 12 ++++++++ src/api/Api.h | 2 ++ src/api/ApiState.cpp | 62 +++++++++++++++++++++++++++++++++++++++- src/api/ApiState.h | 10 ++++++- src/workers/Hashrate.cpp | 10 +++---- src/workers/Hashrate.h | 7 +++++ src/workers/Workers.cpp | 3 ++ 7 files changed, 99 insertions(+), 7 deletions(-) diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 6fe1a838..a1d6e8ab 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -67,3 +67,15 @@ const char *Api::get(const char *url, size_t *size, int *status) return m_buf; } + + +void Api::tick(const Hashrate *hashrate) +{ + if (!m_state) { + return; + } + + uv_mutex_lock(&m_mutex); + m_state->tick(hashrate); + uv_mutex_unlock(&m_mutex); +} diff --git a/src/api/Api.h b/src/api/Api.h index db053139..ba36a992 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -29,6 +29,7 @@ class ApiState; +class Hashrate; class Api @@ -38,6 +39,7 @@ public: static void release(); static const char *get(const char *url, size_t *size, int *status); + static void tick(const Hashrate *hashrate); private: static ApiState *m_state; diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp index 32681842..8c745d9b 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiState.cpp @@ -21,6 +21,7 @@ * along with this program. If not, see . */ +#include #include #include @@ -38,6 +39,7 @@ #include "Options.h" #include "Platform.h" #include "version.h" +#include "workers/Hashrate.h" extern "C" @@ -46,9 +48,24 @@ extern "C" } +static inline double normalizeHs(double hashrate) +{ + if (!std::isnormal(hashrate)) { + return 0.0; + } + + return std::floor(hashrate * 10.0) / 10.0; +} + + ApiState::ApiState() { + m_threads = Options::i()->threads(); + m_hashrate = new double[m_threads * 3](); + + memset(m_totalHashrate, 0, sizeof(m_totalHashrate)); memset(m_workerId, 0, sizeof(m_workerId)); + if (Options::i()->apiWorkerId()) { strncpy(m_workerId, Options::i()->apiWorkerId(), sizeof(m_workerId) - 1); } @@ -62,6 +79,7 @@ ApiState::ApiState() ApiState::~ApiState() { + delete [] m_hashrate; } @@ -71,14 +89,30 @@ const char *ApiState::get(const char *url, size_t *size) const getIdentify(reply); getMiner(reply); + getHashrate(reply); return finalize(reply, size); } +void ApiState::tick(const Hashrate *hashrate) +{ + for (int i = 0; i < m_threads; ++i) { + m_hashrate[i * 3] = normalizeHs(hashrate->calc((size_t) i, Hashrate::ShortInterval)); + m_hashrate[i * 3 + 1] = normalizeHs(hashrate->calc((size_t) i, Hashrate::MediumInterval)); + m_hashrate[i * 3 + 2] = normalizeHs(hashrate->calc((size_t) i, Hashrate::LargeInterval)); + } + + m_totalHashrate[0] = normalizeHs(hashrate->calc(Hashrate::ShortInterval)); + m_totalHashrate[1] = normalizeHs(hashrate->calc(Hashrate::MediumInterval)); + m_totalHashrate[2] = normalizeHs(hashrate->calc(Hashrate::LargeInterval)); + m_highestHashrate = normalizeHs(hashrate->highest()); +} + + const char *ApiState::finalize(json_t *reply, size_t *size) const { - *size = json_dumpb(reply, m_buf, sizeof(m_buf) - 1, JSON_INDENT(4)); + *size = json_dumpb(reply, m_buf, sizeof(m_buf) - 1, JSON_INDENT(4) | JSON_REAL_PRECISION(15)); json_decref(reply); return m_buf; @@ -110,6 +144,32 @@ void ApiState::genId() } +void ApiState::getHashrate(json_t *reply) const +{ + json_t *hashrate = json_object(); + json_t *threads = json_array(); + json_t *total = json_array(); + + json_object_set(reply, "hashrate", hashrate); + json_object_set(hashrate, "total", total); + json_object_set(hashrate, "highest", json_real(m_highestHashrate)); + json_object_set(hashrate, "threads", threads); + + for (int i = 0; i < m_threads * 3; i += 3) { + json_t *thread = json_array(); + json_array_append(thread, json_real(m_hashrate[i])); + json_array_append(thread, json_real(m_hashrate[i + 1])); + json_array_append(thread, json_real(m_hashrate[i + 2])); + + json_array_append(threads, thread); + } + + for (int i = 0; i < 3; ++i) { + json_array_append(total, json_real(m_totalHashrate[i])); + } +} + + void ApiState::getIdentify(json_t *reply) const { json_object_set(reply, "id", json_string(m_id)); diff --git a/src/api/ApiState.h b/src/api/ApiState.h index 3876aa6c..3405d49d 100644 --- a/src/api/ApiState.h +++ b/src/api/ApiState.h @@ -28,6 +28,9 @@ #include "jansson.h" +class Hashrate; + + class ApiState { public: @@ -35,16 +38,21 @@ public: ~ApiState(); const char *get(const char *url, size_t *size) const; + void tick(const Hashrate *hashrate); private: const char *finalize(json_t *reply, size_t *size) const; void genId(); + void getHashrate(json_t *reply) const; void getIdentify(json_t *reply) const; void getMiner(json_t *reply) const; - char m_id[17]; char m_workerId[128]; + double *m_hashrate; + double m_highestHashrate; + double m_totalHashrate[3]; + int m_threads; mutable char m_buf[4096]; }; diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index 5bc65698..5c20c247 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -153,10 +153,10 @@ void Hashrate::print() char num4[8]; LOG_INFO(Options::i()->colors() ? "\x1B[01;37mspeed\x1B[0m 2.5s/60s/15m \x1B[01;36m%s \x1B[22;36m%s %s \x1B[01;36mH/s\x1B[0m max: \x1B[01;36m%s H/s" : "speed 2.5s/60s/15m %s %s %s H/s max: %s H/s", - format(calc(2500), num1, sizeof(num1)), - format(calc(60000), num2, sizeof(num2)), - format(calc(900000), num3, sizeof(num3)), - format(m_highest, num4, sizeof(num4)) + format(calc(ShortInterval), num1, sizeof(num1)), + format(calc(MediumInterval), num2, sizeof(num2)), + format(calc(LargeInterval), num3, sizeof(num3)), + format(m_highest, num4, sizeof(num4)) ); } @@ -169,7 +169,7 @@ void Hashrate::stop() void Hashrate::updateHighest() { - double highest = calc(2500); + double highest = calc(ShortInterval); if (std::isnormal(highest) && highest > m_highest) { m_highest = highest; } diff --git a/src/workers/Hashrate.h b/src/workers/Hashrate.h index ca894dcb..026c0cdf 100644 --- a/src/workers/Hashrate.h +++ b/src/workers/Hashrate.h @@ -32,6 +32,12 @@ class Hashrate { public: + enum Intervals { + ShortInterval = 2500, + MediumInterval = 60000, + LargeInterval = 900000 + }; + Hashrate(int threads); double calc(size_t ms) const; double calc(size_t threadId, size_t ms) const; @@ -41,6 +47,7 @@ public: void updateHighest(); inline double highest() const { return m_highest; } + inline int threads() const { return m_threads; } private: static void onReport(uv_timer_t *handle); diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index e51f5d22..4ba28693 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -24,6 +24,7 @@ #include +#include "api/Api.h" #include "interfaces/IJobResultListener.h" #include "Mem.h" #include "Options.h" @@ -192,4 +193,6 @@ void Workers::onTick(uv_timer_t *handle) if ((m_ticks++ & 0xF) == 0) { m_hashrate->updateHighest(); } + + Api::tick(m_hashrate); } From 9e9cddedc515ce3a69e86038d41f35205a8eff44 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 1 Sep 2017 08:02:56 +0300 Subject: [PATCH 005/389] Added results statistics to API. --- CMakeLists.txt | 3 ++ src/api/Api.cpp | 12 +++++ src/api/Api.h | 2 + src/api/ApiState.cpp | 64 +++++++++++++++++------ src/api/ApiState.h | 5 ++ src/api/Results.cpp | 47 +++++++++++++++++ src/api/Results.h | 54 +++++++++++++++++++ src/interfaces/IClientListener.h | 9 ++-- src/interfaces/IStrategyListener.h | 9 ++-- src/net/Client.cpp | 8 +-- src/net/JobResult.h | 6 +++ src/net/Network.cpp | 22 ++++---- src/net/Network.h | 6 +-- src/net/SubmitResult.cpp | 44 ++++++++++++++++ src/net/SubmitResult.h | 13 ++--- src/net/strategies/DonateStrategy.cpp | 4 +- src/net/strategies/DonateStrategy.h | 2 +- src/net/strategies/FailoverStrategy.cpp | 4 +- src/net/strategies/FailoverStrategy.h | 2 +- src/net/strategies/SinglePoolStrategy.cpp | 4 +- src/net/strategies/SinglePoolStrategy.h | 2 +- 21 files changed, 265 insertions(+), 57 deletions(-) create mode 100644 src/api/Results.cpp create mode 100644 src/api/Results.h create mode 100644 src/net/SubmitResult.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 817cdcd6..f5df7718 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ set(HEADERS src/3rdparty/align.h src/api/Api.h src/api/ApiState.h + src/api/Results.h src/App.h src/Console.h src/Cpu.h @@ -64,6 +65,7 @@ set(HEADERS_CRYPTO set(SOURCES src/api/Api.cpp src/api/ApiState.cpp + src/api/Results.cpp src/App.cpp src/Console.cpp src/log/ConsoleLog.cpp @@ -76,6 +78,7 @@ set(SOURCES src/net/strategies/DonateStrategy.cpp src/net/strategies/FailoverStrategy.cpp src/net/strategies/SinglePoolStrategy.cpp + src/net/SubmitResult.cpp src/net/Url.cpp src/Options.cpp src/Platform.cpp diff --git a/src/api/Api.cpp b/src/api/Api.cpp index a1d6e8ab..dd120408 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -79,3 +79,15 @@ void Api::tick(const Hashrate *hashrate) m_state->tick(hashrate); uv_mutex_unlock(&m_mutex); } + + +void Api::tick(const Results &results) +{ + if (!m_state) { + return; + } + + uv_mutex_lock(&m_mutex); + m_state->tick(results); + uv_mutex_unlock(&m_mutex); +} diff --git a/src/api/Api.h b/src/api/Api.h index ba36a992..d228fb38 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -30,6 +30,7 @@ class ApiState; class Hashrate; +class Results; class Api @@ -40,6 +41,7 @@ public: static const char *get(const char *url, size_t *size, int *status); static void tick(const Hashrate *hashrate); + static void tick(const Results &results); private: static ApiState *m_state; diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp index 8c745d9b..af51f6dc 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiState.cpp @@ -48,13 +48,13 @@ extern "C" } -static inline double normalizeHs(double hashrate) +static inline double normalize(double d) { - if (!std::isnormal(hashrate)) { + if (!std::isnormal(d)) { return 0.0; } - return std::floor(hashrate * 10.0) / 10.0; + return std::floor(d * 10.0) / 10.0; } @@ -90,6 +90,8 @@ const char *ApiState::get(const char *url, size_t *size) const getIdentify(reply); getMiner(reply); getHashrate(reply); + getResults(reply); + getConnection(reply); return finalize(reply, size); } @@ -98,15 +100,21 @@ const char *ApiState::get(const char *url, size_t *size) const void ApiState::tick(const Hashrate *hashrate) { for (int i = 0; i < m_threads; ++i) { - m_hashrate[i * 3] = normalizeHs(hashrate->calc((size_t) i, Hashrate::ShortInterval)); - m_hashrate[i * 3 + 1] = normalizeHs(hashrate->calc((size_t) i, Hashrate::MediumInterval)); - m_hashrate[i * 3 + 2] = normalizeHs(hashrate->calc((size_t) i, Hashrate::LargeInterval)); + m_hashrate[i * 3] = hashrate->calc((size_t) i, Hashrate::ShortInterval); + m_hashrate[i * 3 + 1] = hashrate->calc((size_t) i, Hashrate::MediumInterval); + m_hashrate[i * 3 + 2] = hashrate->calc((size_t) i, Hashrate::LargeInterval); } - m_totalHashrate[0] = normalizeHs(hashrate->calc(Hashrate::ShortInterval)); - m_totalHashrate[1] = normalizeHs(hashrate->calc(Hashrate::MediumInterval)); - m_totalHashrate[2] = normalizeHs(hashrate->calc(Hashrate::LargeInterval)); - m_highestHashrate = normalizeHs(hashrate->highest()); + m_totalHashrate[0] = hashrate->calc(Hashrate::ShortInterval); + m_totalHashrate[1] = hashrate->calc(Hashrate::MediumInterval); + m_totalHashrate[2] = hashrate->calc(Hashrate::LargeInterval); + m_highestHashrate = hashrate->highest(); +} + + +void ApiState::tick(const Results &results) +{ + m_results = results; } @@ -144,6 +152,14 @@ void ApiState::genId() } +void ApiState::getConnection(json_t *reply) const +{ + json_t *connection = json_object(); + + json_object_set(reply, "connection", connection); +} + + void ApiState::getHashrate(json_t *reply) const { json_t *hashrate = json_object(); @@ -152,20 +168,20 @@ void ApiState::getHashrate(json_t *reply) const json_object_set(reply, "hashrate", hashrate); json_object_set(hashrate, "total", total); - json_object_set(hashrate, "highest", json_real(m_highestHashrate)); + json_object_set(hashrate, "highest", json_real(normalize(m_highestHashrate))); json_object_set(hashrate, "threads", threads); for (int i = 0; i < m_threads * 3; i += 3) { json_t *thread = json_array(); - json_array_append(thread, json_real(m_hashrate[i])); - json_array_append(thread, json_real(m_hashrate[i + 1])); - json_array_append(thread, json_real(m_hashrate[i + 2])); + json_array_append(thread, json_real(normalize(m_hashrate[i]))); + json_array_append(thread, json_real(normalize(m_hashrate[i + 1]))); + json_array_append(thread, json_real(normalize(m_hashrate[i + 2]))); json_array_append(threads, thread); } for (int i = 0; i < 3; ++i) { - json_array_append(total, json_real(m_totalHashrate[i])); + json_array_append(total, json_real(normalize(m_totalHashrate[i]))); } } @@ -193,3 +209,21 @@ void ApiState::getMiner(json_t *reply) const json_object_set(cpu, "x64", json_boolean(Cpu::isX64())); json_object_set(cpu, "sockets", json_integer(Cpu::sockets())); } + + +void ApiState::getResults(json_t *reply) const +{ + json_t *results = json_object(); + json_t *best = json_array(); + + json_object_set(reply, "results", results); + json_object_set(results, "diff_current", json_integer(m_results.diff)); + json_object_set(results, "shares_good", json_integer(m_results.accepted)); + json_object_set(results, "shares_total", json_integer(m_results.accepted + m_results.rejected)); + json_object_set(results, "hashes_total", json_integer(m_results.total)); + json_object_set(results, "best", best); + + for (size_t i = 0; i < m_results.topDiff.size(); ++i) { + json_array_append(best, json_integer(m_results.topDiff[i])); + } +} diff --git a/src/api/ApiState.h b/src/api/ApiState.h index 3405d49d..e4c4740b 100644 --- a/src/api/ApiState.h +++ b/src/api/ApiState.h @@ -25,6 +25,7 @@ #define __APISTATE_H__ +#include "api/Results.h" #include "jansson.h" @@ -39,13 +40,16 @@ public: const char *get(const char *url, size_t *size) const; void tick(const Hashrate *hashrate); + void tick(const Results &results); private: const char *finalize(json_t *reply, size_t *size) const; void genId(); + void getConnection(json_t *reply) const; void getHashrate(json_t *reply) const; void getIdentify(json_t *reply) const; void getMiner(json_t *reply) const; + void getResults(json_t *reply) const; char m_id[17]; char m_workerId[128]; @@ -54,6 +58,7 @@ private: double m_totalHashrate[3]; int m_threads; mutable char m_buf[4096]; + Results m_results; }; #endif /* __APISTATE_H__ */ diff --git a/src/api/Results.cpp b/src/api/Results.cpp new file mode 100644 index 00000000..80f721d4 --- /dev/null +++ b/src/api/Results.cpp @@ -0,0 +1,47 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include + + +#include "api/Results.h" +#include "net/SubmitResult.h" + + +void Results::add(const SubmitResult &result, const char *error) +{ + if (error) { + rejected++; + return; + } + + accepted++; + total += result.diff; + + const size_t ln = topDiff.size() - 1; + if (result.actualDiff > topDiff[ln]) { + topDiff[ln] = result.actualDiff; + std::sort(topDiff.rbegin(), topDiff.rend()); + } +} diff --git a/src/api/Results.h b/src/api/Results.h new file mode 100644 index 00000000..3f3296ef --- /dev/null +++ b/src/api/Results.h @@ -0,0 +1,54 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __RESULTS_H__ +#define __RESULTS_H__ + + +#include +#include + + +class SubmitResult; + + +class Results +{ +public: + inline Results() : + diff(0), + accepted(0), + rejected(0), + total(0) + {} + + void add(const SubmitResult &result, const char *error); + + std::array topDiff { { } }; + uint32_t diff; + uint64_t accepted; + uint64_t rejected; + uint64_t total; +}; + +#endif /* __RESULTS_H__ */ diff --git a/src/interfaces/IClientListener.h b/src/interfaces/IClientListener.h index b7c866de..f6e7fd3c 100644 --- a/src/interfaces/IClientListener.h +++ b/src/interfaces/IClientListener.h @@ -30,6 +30,7 @@ class Client; class Job; +class SubmitResult; class IClientListener @@ -37,10 +38,10 @@ class IClientListener public: virtual ~IClientListener() {} - virtual void onClose(Client *client, int failures) = 0; - virtual void onJobReceived(Client *client, const Job &job) = 0; - virtual void onLoginSuccess(Client *client) = 0; - virtual void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) = 0; + virtual void onClose(Client *client, int failures) = 0; + virtual void onJobReceived(Client *client, const Job &job) = 0; + virtual void onLoginSuccess(Client *client) = 0; + virtual void onResultAccepted(Client *client, const SubmitResult &result, const char *error) = 0; }; diff --git a/src/interfaces/IStrategyListener.h b/src/interfaces/IStrategyListener.h index e71b2529..60f95734 100644 --- a/src/interfaces/IStrategyListener.h +++ b/src/interfaces/IStrategyListener.h @@ -31,6 +31,7 @@ class Client; class IStrategy; class Job; +class SubmitResult; class IStrategyListener @@ -38,10 +39,10 @@ class IStrategyListener public: virtual ~IStrategyListener() {} - virtual void onActive(Client *client) = 0; - virtual void onJob(Client *client, const Job &job) = 0; - virtual void onPause(IStrategy *strategy) = 0; - virtual void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) = 0; + virtual void onActive(Client *client) = 0; + virtual void onJob(Client *client, const Job &job) = 0; + virtual void onPause(IStrategy *strategy) = 0; + virtual void onResultAccepted(Client *client, const SubmitResult &result, const char *error) = 0; }; diff --git a/src/net/Client.cpp b/src/net/Client.cpp index c2518be9..41bfda47 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -194,7 +194,7 @@ int64_t Client::submit(const JobResult &result) snprintf(req, 345, "{\"id\":%" PRIu64 ",\"jsonrpc\":\"2.0\",\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"}}\n", m_sequence, m_rpcId, result.jobId, nonce, data); - m_results[m_sequence] = SubmitResult(m_sequence, result.diff); + m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff()); return send(req); } @@ -420,7 +420,8 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error auto it = m_results.find(id); if (it != m_results.end()) { - m_listener->onResultAccepted(this, it->second.seq, it->second.diff, it->second.elapsed(), message); + it->second.done(); + m_listener->onResultAccepted(this, it->second, message); m_results.erase(it); } else if (!m_quiet) { @@ -456,7 +457,8 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error auto it = m_results.find(id); if (it != m_results.end()) { - m_listener->onResultAccepted(this, it->second.seq, it->second.diff, it->second.elapsed(), nullptr); + it->second.done(); + m_listener->onResultAccepted(this, it->second, nullptr); m_results.erase(it); } } diff --git a/src/net/JobResult.h b/src/net/JobResult.h index de3b17ad..3f69992a 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -61,6 +61,12 @@ public: } + inline uint64_t actualDiff() const + { + return Job::toDiff(reinterpret_cast(result)[3]); + } + + char jobId[64]; int poolId; uint32_t diff; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index d732c774..9f83ee6d 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -30,12 +30,14 @@ #include +#include "api/Api.h" #include "log/Log.h" #include "net/Client.h" #include "net/Network.h" #include "net/strategies/DonateStrategy.h" #include "net/strategies/FailoverStrategy.h" #include "net/strategies/SinglePoolStrategy.h" +#include "net/SubmitResult.h" #include "net/Url.h" #include "Options.h" #include "Platform.h" @@ -44,9 +46,7 @@ Network::Network(const Options *options) : m_options(options), - m_donate(nullptr), - m_accepted(0), - m_rejected(0) + m_donate(nullptr) { srand(time(0) ^ (uintptr_t) this); @@ -139,21 +139,19 @@ void Network::onPause(IStrategy *strategy) } -void Network::onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) +void Network::onResultAccepted(Client *client, const SubmitResult &result, const char *error) { - if (error) { - m_rejected++; + m_results.add(result, error); + if (error) { LOG_INFO(m_options->colors() ? "\x1B[01;31mrejected\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[01;37m%u\x1B[0m \x1B[31m\"%s\"\x1B[0m \x1B[01;30m(%" PRIu64 " ms)" : "rejected (%" PRId64 "/%" PRId64 ") diff %u \"%s\" (%" PRIu64 " ms)", - m_accepted, m_rejected, diff, error, ms); + m_results.accepted, m_results.rejected, result.diff, error, result.elapsed); } else { - m_accepted++; - LOG_INFO(m_options->colors() ? "\x1B[01;32maccepted\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[01;37m%u\x1B[0m \x1B[01;30m(%" PRIu64 " ms)" : "accepted (%" PRId64 "/%" PRId64 ") diff %u (%" PRIu64 " ms)", - m_accepted, m_rejected, diff, ms); + m_results.accepted, m_results.rejected, result.diff, result.elapsed); } } @@ -162,12 +160,12 @@ void Network::setJob(Client *client, const Job &job) { if (m_options->colors()) { LOG_INFO("\x1B[01;35mnew job\x1B[0m from \x1B[01;37m%s:%d\x1B[0m diff \x1B[01;37m%d", client->host(), client->port(), job.diff()); - } else { LOG_INFO("new job from %s:%d diff %d", client->host(), client->port(), job.diff()); } + m_results.diff = job.diff(); Workers::setJob(job); } @@ -181,6 +179,8 @@ void Network::tick() if (m_donate) { m_donate->tick(now); } + + Api::tick(m_results); } diff --git a/src/net/Network.h b/src/net/Network.h index 33806f63..54a4f8d6 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -29,6 +29,7 @@ #include +#include "api/Results.h" #include "interfaces/IJobResultListener.h" #include "interfaces/IStrategyListener.h" @@ -52,7 +53,7 @@ protected: void onJob(Client *client, const Job &job) override; void onJobResult(const JobResult &result) override; void onPause(IStrategy *strategy) override; - void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) override; + void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; private: constexpr static int kTickInterval = 1 * 1000; @@ -65,8 +66,7 @@ private: const Options *m_options; IStrategy *m_donate; IStrategy *m_strategy; - uint64_t m_accepted; - uint64_t m_rejected; + Results m_results; uv_timer_t m_timer; }; diff --git a/src/net/SubmitResult.cpp b/src/net/SubmitResult.cpp new file mode 100644 index 00000000..2e81017c --- /dev/null +++ b/src/net/SubmitResult.cpp @@ -0,0 +1,44 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include + + +#include "net/SubmitResult.h" + + +SubmitResult::SubmitResult(int64_t seq, uint32_t diff, uint64_t actualDiff) : + seq(seq), + diff(diff), + actualDiff(actualDiff), + elapsed(0) +{ + start = uv_hrtime(); +} + + +void SubmitResult::done() +{ + elapsed = (uv_hrtime() - start) / 1000000; +} diff --git a/src/net/SubmitResult.h b/src/net/SubmitResult.h index 71a9572b..63f5e883 100644 --- a/src/net/SubmitResult.h +++ b/src/net/SubmitResult.h @@ -31,18 +31,15 @@ class SubmitResult { public: - inline SubmitResult() : seq(0), diff(0), start(0) {} - inline SubmitResult(int64_t seq, uint32_t diff) : - seq(seq), - diff(diff) - { - start = uv_hrtime(); - } + inline SubmitResult() : seq(0), diff(0), actualDiff(0), elapsed(0), start(0) {} + SubmitResult(int64_t seq, uint32_t diff, uint64_t actualDiff); - inline uint64_t elapsed() const { return (uv_hrtime() - start) / 1000000; } + void done(); int64_t seq; uint32_t diff; + uint64_t actualDiff; + uint64_t elapsed; uint64_t start; }; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 0f981451..d7c721c6 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -111,9 +111,9 @@ void DonateStrategy::onLoginSuccess(Client *client) } -void DonateStrategy::onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) +void DonateStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) { - m_listener->onResultAccepted(client, seq, diff, ms, error); + m_listener->onResultAccepted(client, result, error); } diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index b54b0b17..302de292 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -55,7 +55,7 @@ protected: void onClose(Client *client, int failures) override; void onJobReceived(Client *client, const Job &job) override; void onLoginSuccess(Client *client) override; - void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) override; + void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; private: void idle(); diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index e25b8c58..47d390b0 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -132,9 +132,9 @@ void FailoverStrategy::onLoginSuccess(Client *client) } -void FailoverStrategy::onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) +void FailoverStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) { - m_listener->onResultAccepted(client, seq, diff, ms, error); + m_listener->onResultAccepted(client, result, error); } diff --git a/src/net/strategies/FailoverStrategy.h b/src/net/strategies/FailoverStrategy.h index 616a08d7..963d3157 100644 --- a/src/net/strategies/FailoverStrategy.h +++ b/src/net/strategies/FailoverStrategy.h @@ -55,7 +55,7 @@ protected: void onClose(Client *client, int failures) override; void onJobReceived(Client *client, const Job &job) override; void onLoginSuccess(Client *client) override; - void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) override; + void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; private: void add(const Url *url, const char *agent); diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index f38405f4..997dc00b 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -96,7 +96,7 @@ void SinglePoolStrategy::onLoginSuccess(Client *client) } -void SinglePoolStrategy::onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) +void SinglePoolStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) { - m_listener->onResultAccepted(client, seq, diff, ms, error); + m_listener->onResultAccepted(client, result, error); } diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h index c09d0305..95e21547 100644 --- a/src/net/strategies/SinglePoolStrategy.h +++ b/src/net/strategies/SinglePoolStrategy.h @@ -52,7 +52,7 @@ protected: void onClose(Client *client, int failures) override; void onJobReceived(Client *client, const Job &job) override; void onLoginSuccess(Client *client) override; - void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) override; + void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; private: bool m_active; From 8e08df2fd2ab19d5349a45c2d1d371ce1eaeb662 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 1 Sep 2017 15:35:00 +0300 Subject: [PATCH 006/389] Rename Results => NetworkState. --- CMakeLists.txt | 4 ++-- src/api/Api.cpp | 4 ++-- src/api/Api.h | 4 ++-- src/api/ApiState.cpp | 21 +++++++++++---------- src/api/ApiState.h | 6 +++--- src/api/{Results.cpp => NetworkState.cpp} | 4 ++-- src/api/{Results.h => NetworkState.h} | 10 +++++----- src/net/Network.cpp | 10 +++++----- src/net/Network.h | 4 ++-- 9 files changed, 34 insertions(+), 33 deletions(-) rename src/api/{Results.cpp => NetworkState.cpp} (93%) rename src/api/{Results.h => NetworkState.h} (91%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5df7718..6cc9eb89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ set(HEADERS src/3rdparty/align.h src/api/Api.h src/api/ApiState.h - src/api/Results.h + src/api/NetworkState.h src/App.h src/Console.h src/Cpu.h @@ -65,7 +65,7 @@ set(HEADERS_CRYPTO set(SOURCES src/api/Api.cpp src/api/ApiState.cpp - src/api/Results.cpp + src/api/NetworkState.cpp src/App.cpp src/Console.cpp src/log/ConsoleLog.cpp diff --git a/src/api/Api.cpp b/src/api/Api.cpp index dd120408..20882133 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -81,13 +81,13 @@ void Api::tick(const Hashrate *hashrate) } -void Api::tick(const Results &results) +void Api::tick(const NetworkState &network) { if (!m_state) { return; } uv_mutex_lock(&m_mutex); - m_state->tick(results); + m_state->tick(network); uv_mutex_unlock(&m_mutex); } diff --git a/src/api/Api.h b/src/api/Api.h index d228fb38..dc5a09e0 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -30,7 +30,7 @@ class ApiState; class Hashrate; -class Results; +class NetworkState; class Api @@ -41,7 +41,7 @@ public: static const char *get(const char *url, size_t *size, int *status); static void tick(const Hashrate *hashrate); - static void tick(const Results &results); + static void tick(const NetworkState &results); private: static ApiState *m_state; diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp index af51f6dc..66af86bf 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiState.cpp @@ -112,9 +112,9 @@ void ApiState::tick(const Hashrate *hashrate) } -void ApiState::tick(const Results &results) +void ApiState::tick(const NetworkState &network) { - m_results = results; + m_network = network; } @@ -216,14 +216,15 @@ void ApiState::getResults(json_t *reply) const json_t *results = json_object(); json_t *best = json_array(); - json_object_set(reply, "results", results); - json_object_set(results, "diff_current", json_integer(m_results.diff)); - json_object_set(results, "shares_good", json_integer(m_results.accepted)); - json_object_set(results, "shares_total", json_integer(m_results.accepted + m_results.rejected)); - json_object_set(results, "hashes_total", json_integer(m_results.total)); - json_object_set(results, "best", best); + json_object_set(reply, "results", results); + json_object_set(results, "diff_current", json_integer(m_network.diff)); + json_object_set(results, "shares_good", json_integer(m_network.accepted)); + json_object_set(results, "shares_total", json_integer(m_network.accepted + m_network.rejected)); + json_object_set(results, "hashes_total", json_integer(m_network.total)); + json_object_set(results, "best", best); + json_object_set(results, "error_log", json_array()); - for (size_t i = 0; i < m_results.topDiff.size(); ++i) { - json_array_append(best, json_integer(m_results.topDiff[i])); + for (size_t i = 0; i < m_network.topDiff.size(); ++i) { + json_array_append(best, json_integer(m_network.topDiff[i])); } } diff --git a/src/api/ApiState.h b/src/api/ApiState.h index e4c4740b..14c4c135 100644 --- a/src/api/ApiState.h +++ b/src/api/ApiState.h @@ -25,7 +25,7 @@ #define __APISTATE_H__ -#include "api/Results.h" +#include "api/NetworkState.h" #include "jansson.h" @@ -40,7 +40,7 @@ public: const char *get(const char *url, size_t *size) const; void tick(const Hashrate *hashrate); - void tick(const Results &results); + void tick(const NetworkState &results); private: const char *finalize(json_t *reply, size_t *size) const; @@ -58,7 +58,7 @@ private: double m_totalHashrate[3]; int m_threads; mutable char m_buf[4096]; - Results m_results; + NetworkState m_network; }; #endif /* __APISTATE_H__ */ diff --git a/src/api/Results.cpp b/src/api/NetworkState.cpp similarity index 93% rename from src/api/Results.cpp rename to src/api/NetworkState.cpp index 80f721d4..08c64c21 100644 --- a/src/api/Results.cpp +++ b/src/api/NetworkState.cpp @@ -25,11 +25,11 @@ #include -#include "api/Results.h" +#include "api/NetworkState.h" #include "net/SubmitResult.h" -void Results::add(const SubmitResult &result, const char *error) +void NetworkState::add(const SubmitResult &result, const char *error) { if (error) { rejected++; diff --git a/src/api/Results.h b/src/api/NetworkState.h similarity index 91% rename from src/api/Results.h rename to src/api/NetworkState.h index 3f3296ef..6832f534 100644 --- a/src/api/Results.h +++ b/src/api/NetworkState.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __RESULTS_H__ -#define __RESULTS_H__ +#ifndef __NETWORKSTATE_H__ +#define __NETWORKSTATE_H__ #include @@ -32,10 +32,10 @@ class SubmitResult; -class Results +class NetworkState { public: - inline Results() : + inline NetworkState() : diff(0), accepted(0), rejected(0), @@ -51,4 +51,4 @@ public: uint64_t total; }; -#endif /* __RESULTS_H__ */ +#endif /* __NETWORKSTATE_H__ */ diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 9f83ee6d..840a77f8 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -141,17 +141,17 @@ void Network::onPause(IStrategy *strategy) void Network::onResultAccepted(Client *client, const SubmitResult &result, const char *error) { - m_results.add(result, error); + m_state.add(result, error); if (error) { LOG_INFO(m_options->colors() ? "\x1B[01;31mrejected\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[01;37m%u\x1B[0m \x1B[31m\"%s\"\x1B[0m \x1B[01;30m(%" PRIu64 " ms)" : "rejected (%" PRId64 "/%" PRId64 ") diff %u \"%s\" (%" PRIu64 " ms)", - m_results.accepted, m_results.rejected, result.diff, error, result.elapsed); + m_state.accepted, m_state.rejected, result.diff, error, result.elapsed); } else { LOG_INFO(m_options->colors() ? "\x1B[01;32maccepted\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[01;37m%u\x1B[0m \x1B[01;30m(%" PRIu64 " ms)" : "accepted (%" PRId64 "/%" PRId64 ") diff %u (%" PRIu64 " ms)", - m_results.accepted, m_results.rejected, result.diff, result.elapsed); + m_state.accepted, m_state.rejected, result.diff, result.elapsed); } } @@ -165,7 +165,7 @@ void Network::setJob(Client *client, const Job &job) LOG_INFO("new job from %s:%d diff %d", client->host(), client->port(), job.diff()); } - m_results.diff = job.diff(); + m_state.diff = job.diff(); Workers::setJob(job); } @@ -180,7 +180,7 @@ void Network::tick() m_donate->tick(now); } - Api::tick(m_results); + Api::tick(m_state); } diff --git a/src/net/Network.h b/src/net/Network.h index 54a4f8d6..fe13d9b7 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -29,7 +29,7 @@ #include -#include "api/Results.h" +#include "api/NetworkState.h" #include "interfaces/IJobResultListener.h" #include "interfaces/IStrategyListener.h" @@ -66,7 +66,7 @@ private: const Options *m_options; IStrategy *m_donate; IStrategy *m_strategy; - Results m_results; + NetworkState m_state; uv_timer_t m_timer; }; From 7798f09dc78cdc683757905449e26b705e21923e Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 2 Sep 2017 05:47:30 +0300 Subject: [PATCH 007/389] Added connection statistics to API. --- src/api/ApiState.cpp | 16 ++++++++-- src/api/NetworkState.cpp | 65 ++++++++++++++++++++++++++++++++++++++++ src/api/NetworkState.h | 21 ++++++++----- src/net/Network.cpp | 3 ++ 4 files changed, 96 insertions(+), 9 deletions(-) diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp index 66af86bf..e05afb53 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiState.cpp @@ -141,8 +141,14 @@ void ApiState::genId() for (int i = 0; i < count; i++) { if (!interfaces[i].is_internal && interfaces[i].address.address4.sin_family == AF_INET) { uint8_t hash[200]; + const size_t addrSize = sizeof(interfaces[i].phys_addr); + const size_t inSize = strlen(APP_KIND) + addrSize; - keccak(reinterpret_cast(interfaces[i].phys_addr), static_cast(sizeof(interfaces[i].phys_addr)), hash, sizeof(hash)); + uint8_t *input = new uint8_t[inSize](); + memcpy(input, interfaces[i].phys_addr, addrSize); + memcpy(input + addrSize, APP_KIND, strlen(APP_KIND)); + + keccak(input, static_cast(inSize), hash, sizeof(hash)); Job::toHex(hash, 8, m_id); break; } @@ -156,7 +162,12 @@ void ApiState::getConnection(json_t *reply) const { json_t *connection = json_object(); - json_object_set(reply, "connection", connection); + json_object_set(reply, "connection", connection); + json_object_set(connection, "pool", json_string(m_network.pool)); + json_object_set(connection, "uptime", json_integer(m_network.connectionTime())); + json_object_set(connection, "ping", json_integer(m_network.latency())); + json_object_set(connection, "failures", json_integer(m_network.failures)); + json_object_set(connection, "error_log", json_array()); } @@ -220,6 +231,7 @@ void ApiState::getResults(json_t *reply) const json_object_set(results, "diff_current", json_integer(m_network.diff)); json_object_set(results, "shares_good", json_integer(m_network.accepted)); json_object_set(results, "shares_total", json_integer(m_network.accepted + m_network.rejected)); + json_object_set(results, "avg_time", json_integer(m_network.avgTime())); json_object_set(results, "hashes_total", json_integer(m_network.total)); json_object_set(results, "best", best); json_object_set(results, "error_log", json_array()); diff --git a/src/api/NetworkState.cpp b/src/api/NetworkState.cpp index 08c64c21..2222b475 100644 --- a/src/api/NetworkState.cpp +++ b/src/api/NetworkState.cpp @@ -23,12 +23,56 @@ #include +#include + #include "api/NetworkState.h" #include "net/SubmitResult.h" +NetworkState::NetworkState() : + diff(0), + accepted(0), + failures(0), + rejected(0), + total(0), + m_active(false) +{ + memset(pool, 0, sizeof(pool)); +} + + +int NetworkState::connectionTime() const +{ + return m_active ? ((uv_now(uv_default_loop()) - m_connectionTime) / 1000) : 0; +} + + +uint32_t NetworkState::avgTime() const +{ + if (m_latency.empty()) { + return 0; + } + + return (uint32_t) connectionTime() / m_latency.size(); +} + + +uint32_t NetworkState::latency() const +{ + const size_t calls = m_latency.size(); + if (calls == 0) { + return 0; + } + + auto v = m_latency; + std::nth_element(v.begin(), v.begin() + calls / 2, v.end()); + + return v[calls / 2]; +} + + void NetworkState::add(const SubmitResult &result, const char *error) { if (error) { @@ -44,4 +88,25 @@ void NetworkState::add(const SubmitResult &result, const char *error) topDiff[ln] = result.actualDiff; std::sort(topDiff.rbegin(), topDiff.rend()); } + + m_latency.push_back(result.elapsed > 0xFFFF ? 0xFFFF : (uint16_t) result.elapsed); +} + + +void NetworkState::setPool(const char *host, int port, const char *ip) +{ + snprintf(pool, sizeof(pool) - 1, "%s:%d", host, port); + + m_active = true; + m_connectionTime = uv_now(uv_default_loop()); +} + + +void NetworkState::stop() +{ + m_active = false; + diff = 0; + + failures++; + m_latency.clear(); } diff --git a/src/api/NetworkState.h b/src/api/NetworkState.h index 6832f534..d0998074 100644 --- a/src/api/NetworkState.h +++ b/src/api/NetworkState.h @@ -25,8 +25,8 @@ #define __NETWORKSTATE_H__ -#include #include +#include class SubmitResult; @@ -35,20 +35,27 @@ class SubmitResult; class NetworkState { public: - inline NetworkState() : - diff(0), - accepted(0), - rejected(0), - total(0) - {} + NetworkState(); + int connectionTime() const; + uint32_t avgTime() const; + uint32_t latency() const; void add(const SubmitResult &result, const char *error); + void setPool(const char *host, int port, const char *ip); + void stop(); + char pool[256]; std::array topDiff { { } }; uint32_t diff; uint64_t accepted; + uint64_t failures; uint64_t rejected; uint64_t total; + +private: + bool m_active; + std::vector m_latency; + uint64_t m_connectionTime; }; #endif /* __NETWORKSTATE_H__ */ diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 840a77f8..23b24a04 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -100,6 +100,8 @@ void Network::onActive(Client *client) return; } + m_state.setPool(client->host(), client->port(), client->ip()); + LOG_INFO(m_options->colors() ? "\x1B[01;37muse pool \x1B[01;36m%s:%d \x1B[01;30m%s" : "use pool %s:%d %s", client->host(), client->port(), client->ip()); } @@ -134,6 +136,7 @@ void Network::onPause(IStrategy *strategy) if (!m_strategy->isActive()) { LOG_ERR("no active pools, stop mining"); + m_state.stop(); return Workers::pause(); } } From 325e11662436abf060ecdb42b5a5b34c26abe2de Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 2 Sep 2017 08:18:24 +0300 Subject: [PATCH 008/389] Added API authorization via Bearer token. --- src/api/Httpd.cpp | 62 +++++++++++++++++++++++++++++++--------- src/api/Httpd.h | 8 ++++-- src/api/NetworkState.cpp | 1 - 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 29baa2f5..6a61ac74 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -58,28 +58,64 @@ bool Httpd::start() } -int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) +int Httpd::auth(const char *header) { - if (strcmp(method, "GET") != 0) { - return MHD_NO; + if (!m_accessToken) { + return MHD_HTTP_OK; } - struct MHD_Response *rsp; - - size_t size = 0; - int status = MHD_HTTP_OK; - const char *buf = Api::get(url, &size, &status); - - if (size) { - rsp = MHD_create_response_from_buffer(size, (void*) buf, MHD_RESPMEM_PERSISTENT); + if (m_accessToken && !header) { + return MHD_HTTP_UNAUTHORIZED; } - else { - rsp = MHD_create_response_from_buffer(k500Size, (void*) k500, MHD_RESPMEM_PERSISTENT); + + const size_t size = strlen(header); + if (size < 8 || strlen(m_accessToken) != size - 7 || memcmp("Bearer ", header, 7) != 0) { + return MHD_HTTP_FORBIDDEN; + } + + return strncmp(m_accessToken, header + 7, strlen(m_accessToken)) == 0 ? MHD_HTTP_OK : MHD_HTTP_FORBIDDEN; +} + + +int Httpd::done(MHD_Connection *connection, int status, MHD_Response *rsp) +{ + if (!rsp) { + rsp = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT); } MHD_add_response_header(rsp, "Content-Type", "application/json"); + MHD_add_response_header(rsp, "Access-Control-Allow-Origin", "*"); + MHD_add_response_header(rsp, "Access-Control-Allow-Methods", "GET"); + MHD_add_response_header(rsp, "Access-Control-Allow-Headers", "Authorization"); const int ret = MHD_queue_response(connection, status, rsp); MHD_destroy_response(rsp); return ret; } + + +int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) +{ + if (strcmp(method, "OPTIONS") == 0) { + return done(connection, MHD_HTTP_OK, nullptr); + } + + if (strcmp(method, "GET") != 0) { + return MHD_NO; + } + + int status = static_cast(cls)->auth(MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Authorization")); + if (status != MHD_HTTP_OK) { + return done(connection, status, nullptr); + } + + MHD_Response *rsp = nullptr; + size_t size = 0; + const char *buf = Api::get(url, &size, &status); + + if (size) { + rsp = MHD_create_response_from_buffer(size, (void*) buf, MHD_RESPMEM_PERSISTENT); + } + + return done(connection, status, rsp); +} diff --git a/src/api/Httpd.h b/src/api/Httpd.h index 32750beb..7a4dd932 100644 --- a/src/api/Httpd.h +++ b/src/api/Httpd.h @@ -28,8 +28,9 @@ #include -struct MHD_Daemon; struct MHD_Connection; +struct MHD_Daemon; +struct MHD_Response; class Httpd @@ -39,7 +40,10 @@ public: bool start(); private: - static int handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); + int auth(const char *header); + + static int done(MHD_Connection *connection, int status, MHD_Response *rsp); + static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); const char *m_accessToken; const int m_port; diff --git a/src/api/NetworkState.cpp b/src/api/NetworkState.cpp index 2222b475..e3c76ec1 100644 --- a/src/api/NetworkState.cpp +++ b/src/api/NetworkState.cpp @@ -26,7 +26,6 @@ #include - #include "api/NetworkState.h" #include "net/SubmitResult.h" From fd98dc236a82ecd88d310541845b4696f6e82100 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 2 Sep 2017 12:12:40 +0300 Subject: [PATCH 009/389] Added support to build without API. --- src/App.cpp | 5 +++++ src/net/Network.cpp | 2 ++ src/workers/Workers.cpp | 2 ++ 3 files changed, 9 insertions(+) diff --git a/src/App.cpp b/src/App.cpp index a016d6b8..1aae7ae0 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -100,7 +100,10 @@ App::~App() { uv_tty_reset_mode(); +# ifndef XMRIG_NO_HTTPD delete m_httpd; +# endif + delete m_console; } @@ -125,7 +128,9 @@ int App::exec() Mem::allocate(m_options->algo(), m_options->threads(), m_options->doubleHash(), m_options->hugePages()); Summary::print(); +# ifndef XMRIG_NO_API Api::start(); +# endif # ifndef XMRIG_NO_HTTPD m_httpd = new Httpd(m_options->apiPort(), m_options->apiToken()); diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 23b24a04..e0f65497 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -183,7 +183,9 @@ void Network::tick() m_donate->tick(now); } +# ifndef XMRIG_NO_API Api::tick(m_state); +# endif } diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 4ba28693..039a1793 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -194,5 +194,7 @@ void Workers::onTick(uv_timer_t *handle) m_hashrate->updateHighest(); } +# ifndef XMRIG_NO_API Api::tick(m_hashrate); +# endif } From 55916660699f7dc7b95fe57aad897739aac3d454 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 2 Sep 2017 14:14:10 +0300 Subject: [PATCH 010/389] Fixed msvc build. --- cmake/FindMHD.cmake | 2 +- src/Options.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/FindMHD.cmake b/cmake/FindMHD.cmake index 23e31c44..f6665892 100644 --- a/cmake/FindMHD.cmake +++ b/cmake/FindMHD.cmake @@ -24,7 +24,7 @@ set(MHD_LIBRARIES ${MHD_LIBRARY}) # same naming convention as in qt (appending debug library with d) # boost is using the same "hack" as us with "optimized" and "debug" # official MHD project actually uses _d suffix -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL MSVC) find_library( MHD_LIBRARY_DEBUG NAMES microhttpd_d microhttpd-10_d libmicrohttpd_d libmicrohttpd-dll_d diff --git a/src/Options.h b/src/Options.h index 582f393f..01aab3ea 100644 --- a/src/Options.h +++ b/src/Options.h @@ -54,7 +54,6 @@ public: static inline Options* i() { return m_self; } static Options *parse(int argc, char **argv); - inline apiPort() const { return m_apiPort; } inline bool background() const { return m_background; } inline bool colors() const { return m_colors; } inline bool doubleHash() const { return m_doubleHash; } @@ -67,6 +66,7 @@ public: inline const std::vector &pools() const { return m_pools; } inline int algo() const { return m_algo; } inline int algoVariant() const { return m_algoVariant; } + inline int apiPort() const { return m_apiPort; } inline int donateLevel() const { return m_donateLevel; } inline int printTime() const { return m_printTime; } inline int priority() const { return m_priority; } From d52aa5a72b6ca2abac3ee5557d6f8f65aa13d725 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 2 Sep 2017 14:25:54 +0300 Subject: [PATCH 011/389] v2.4.0-dev --- src/version.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/version.h b/src/version.h index f1bf79cf..ba908482 100644 --- a/src/version.h +++ b/src/version.h @@ -27,15 +27,15 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.3.1" +#define APP_VERSION "2.4.0-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com" #define APP_KIND "cpu" #define APP_VER_MAJOR 2 -#define APP_VER_MINOR 3 -#define APP_VER_BUILD 1 +#define APP_VER_MINOR 4 +#define APP_VER_BUILD 0 #define APP_VER_REV 0 #ifdef _MSC_VER From 38e93ed6e356a6cc2faa1371498fa15e957f00ab Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 2 Sep 2017 14:33:53 +0300 Subject: [PATCH 012/389] Fixed Linux build. --- src/api/Api.cpp | 2 ++ src/api/Httpd.cpp | 1 + src/api/NetworkState.cpp | 1 + 3 files changed, 4 insertions(+) diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 20882133..a455290a 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -21,6 +21,8 @@ * along with this program. If not, see . */ +#include + #include "api/Api.h" #include "api/ApiState.h" diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 6a61ac74..cefb8192 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -23,6 +23,7 @@ #include +#include #include "api/Api.h" diff --git a/src/api/NetworkState.cpp b/src/api/NetworkState.cpp index e3c76ec1..997f52df 100644 --- a/src/api/NetworkState.cpp +++ b/src/api/NetworkState.cpp @@ -23,6 +23,7 @@ #include +#include #include From d0db4770ed0990ea6862e6ed638f234ae3e614be Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 2 Sep 2017 15:50:27 +0300 Subject: [PATCH 013/389] Added API port to summary. --- CMakeLists.txt | 1 + src/Summary.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cc9eb89..c6a291f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -205,6 +205,7 @@ if (WITH_HTTPD AND MHD_FOUND) set(HTTPD_SOURCES src/api/Httpd.h src/api/Httpd.cpp) else() add_definitions(/DXMRIG_NO_HTTPD) + add_definitions(/DXMRIG_NO_API) endif() include_directories(src) diff --git a/src/Summary.cpp b/src/Summary.cpp index b7376b8e..c6c53341 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -127,6 +127,18 @@ static void print_pools() } +#ifndef XMRIG_NO_API +static void print_api() +{ + if (Options::i()->apiPort() == 0) { + return; + } + + Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mAPI PORT: \x1B[01;36m%d" : " * API PORT: %d", Options::i()->apiPort()); +} +#endif + + static void print_commands() { if (Options::i()->colors()) { @@ -145,6 +157,11 @@ void Summary::print() print_cpu(); print_threads(); print_pools(); + +# ifndef XMRIG_NO_API + print_api(); +# endif + print_commands(); } From 4928b6d3e3d4cfd5fef62e5e8219d19e83386a9b Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 2 Sep 2017 19:04:40 +0300 Subject: [PATCH 014/389] Sync changes with xmrig-nvidia --- CMakeLists.txt | 6 +++++- cmake/FindMHD.cmake | 2 +- src/Options.cpp | 20 ++++++++++---------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c6a291f6..3f87f92d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -198,7 +198,11 @@ if (HAVE_SYSLOG_H) set(SOURCES_SYSLOG src/log/SysLog.h src/log/SysLog.cpp) endif() -find_package(MHD REQUIRED) +if (NOT WITH_AEON) + add_definitions(/DXMRIG_NO_AEON) +endif() + +find_package(MHD) if (WITH_HTTPD AND MHD_FOUND) include_directories(${MHD_INCLUDE_DIRS}) diff --git a/cmake/FindMHD.cmake b/cmake/FindMHD.cmake index f6665892..8505b337 100644 --- a/cmake/FindMHD.cmake +++ b/cmake/FindMHD.cmake @@ -24,7 +24,7 @@ set(MHD_LIBRARIES ${MHD_LIBRARY}) # same naming convention as in qt (appending debug library with d) # boost is using the same "hack" as us with "optimized" and "debug" # official MHD project actually uses _d suffix -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL MSVC) +if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) find_library( MHD_LIBRARY_DEBUG NAMES microhttpd_d microhttpd-10_d libmicrohttpd_d libmicrohttpd-dll_d diff --git a/src/Options.cpp b/src/Options.cpp index 120f58bf..d799b83e 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -116,9 +116,9 @@ static struct option const options[] = { { "user-agent", 1, nullptr, 1008 }, { "userpass", 1, nullptr, 'O' }, { "version", 0, nullptr, 'V' }, - { "api-port", 1, nullptr, 3000 }, - { "api-access-token", 1, nullptr, 3001 }, - { "api-worker-id", 1, nullptr, 3002 }, + { "api-port", 1, nullptr, 4000 }, + { "api-access-token", 1, nullptr, 4001 }, + { "api-worker-id", 1, nullptr, 4002 }, { 0, 0, 0, 0 } }; @@ -157,9 +157,9 @@ static struct option const pool_options[] = { static struct option const api_options[] = { - { "port", 1, nullptr, 3000 }, - { "access-token", 1, nullptr, 3001 }, - { "worker-id", 1, nullptr, 3002 }, + { "port", 1, nullptr, 4000 }, + { "access-token", 1, nullptr, 4001 }, + { "worker-id", 1, nullptr, 4002 }, { 0, 0, 0, 0 } }; @@ -316,12 +316,12 @@ bool Options::parseArg(int key, const char *arg) m_colors = false; break; - case 3001: /* --access-token */ + case 4001: /* --access-token */ free(m_apiToken); m_apiToken = strdup(arg); break; - case 3002: /* --worker-id */ + case 4002: /* --worker-id */ free(m_apiWorkerId); m_apiWorkerId = strdup(arg); break; @@ -334,7 +334,7 @@ bool Options::parseArg(int key, const char *arg) case 1004: /* --max-cpu-usage */ case 1007: /* --print-time */ case 1021: /* --cpu-priority */ - case 3000: /* --api-port */ + case 4000: /* --api-port */ return parseArg(key, strtol(arg, nullptr, 10)); case 'B': /* --background */ @@ -457,7 +457,7 @@ bool Options::parseArg(int key, uint64_t arg) } break; - case 3000: /* --api-port */ + case 4000: /* --api-port */ if (arg <= 65536) { m_apiPort = (int) arg; } From e9a16912c3b8d067a8df0340282ef6e8af9d5e39 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 2 Sep 2017 19:24:06 +0300 Subject: [PATCH 015/389] Fix typo. --- src/api/Httpd.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index cefb8192..f6836c92 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -49,7 +49,7 @@ bool Httpd::start() return false; } - m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, 4455, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); + m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); if (!m_daemon) { LOG_ERR("HTTP Daemon failed to start."); return false; From 80b0ac2492ef7c26304526fa08b3e063de0e28ef Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 4 Sep 2017 06:12:45 +0300 Subject: [PATCH 016/389] #87 Use CMAKE_CXX_STANDARD. --- CMakeLists.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f87f92d..cba41204 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,13 +145,18 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "") endif() +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 11) + + # https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html if (CMAKE_CXX_COMPILER_ID MATCHES GNU) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall -Wno-strict-aliasing") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -std=c++14 -fno-exceptions -fno-rtti") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") if (WIN32) @@ -176,7 +181,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -std=c++14 -fno-exceptions -fno-rtti") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") endif() From 729ee08631233f35a341f21c083de3b10be1a403 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 8 Sep 2017 14:54:43 +0300 Subject: [PATCH 017/389] #101 Fix MSVC 2017 (15.3) version detection. --- src/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index ba908482..551bfbb9 100644 --- a/src/version.h +++ b/src/version.h @@ -39,7 +39,7 @@ #define APP_VER_REV 0 #ifdef _MSC_VER -# if _MSC_VER == 1910 +# if (_MSC_VER == 1910 || _MSC_VER == 1911) # define MSVC_VERSION 2017 # elif _MSC_VER == 1900 # define MSVC_VERSION 2015 From 3a7bb24b401e2230e9e09522f32ef0f9ef53e3f4 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 13 Sep 2017 18:01:28 +0300 Subject: [PATCH 018/389] #108 Silently ignore invalid values for donate-level option. --- src/Options.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Options.cpp b/src/Options.cpp index d799b83e..66c34447 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -420,8 +420,7 @@ bool Options::parseArg(int key, uint64_t arg) case 1003: /* --donate-level */ if (arg < 1 || arg > 99) { - showUsage(1); - return false; + return true; } m_donateLevel = (int) arg; From 4cf3bb9930f623c5f8b695e6a93abc57d9ebcfa4 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 16 Sep 2017 15:20:33 +0300 Subject: [PATCH 019/389] #111 Fixed build without AEON support. --- src/crypto/CryptoNight.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index a463c9f9..c4e56678 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -140,5 +140,9 @@ bool CryptoNight::selfTest(int algo) { _mm_free(ctx->memory); _mm_free(ctx); +# ifndef XMRIG_NO_AEON return memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; +# else + return memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; +# endif } From af51513614b61ce234f443b4b6cda69218e02a43 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 4 Oct 2017 23:33:30 +0300 Subject: [PATCH 020/389] libjansson replaced to rapidjson. Sync changes with proxy. --- CMakeLists.txt | 18 +- src/3rdparty/jansson/CMakeLists.txt | 25 - src/3rdparty/jansson/LICENSE | 19 - src/3rdparty/jansson/dump.c | 506 ---- src/3rdparty/jansson/error.c | 63 - src/3rdparty/jansson/hashtable.c | 360 --- src/3rdparty/jansson/hashtable.h | 176 -- src/3rdparty/jansson/hashtable_seed.c | 277 -- src/3rdparty/jansson/jansson.h | 315 -- src/3rdparty/jansson/jansson_config.h | 43 - src/3rdparty/jansson/jansson_private.h | 109 - src/3rdparty/jansson/jansson_private_config.h | 173 -- src/3rdparty/jansson/load.c | 1158 -------- src/3rdparty/jansson/lookup3.h | 381 --- src/3rdparty/jansson/memory.c | 69 - src/3rdparty/jansson/pack_unpack.c | 871 ------ src/3rdparty/jansson/strbuffer.c | 111 - src/3rdparty/jansson/strbuffer.h | 34 - src/3rdparty/jansson/strconv.c | 145 - src/3rdparty/jansson/utf.c | 187 -- src/3rdparty/jansson/utf.h | 27 - src/3rdparty/jansson/value.c | 1045 ------- src/3rdparty/rapidjson/allocators.h | 271 ++ src/3rdparty/rapidjson/document.h | 2575 +++++++++++++++++ src/3rdparty/rapidjson/encodedstream.h | 299 ++ src/3rdparty/rapidjson/encodings.h | 716 +++++ src/3rdparty/rapidjson/error/en.h | 74 + src/3rdparty/rapidjson/error/error.h | 155 + src/3rdparty/rapidjson/filereadstream.h | 99 + src/3rdparty/rapidjson/filewritestream.h | 104 + src/3rdparty/rapidjson/fwd.h | 151 + src/3rdparty/rapidjson/internal/biginteger.h | 290 ++ src/3rdparty/rapidjson/internal/diyfp.h | 258 ++ src/3rdparty/rapidjson/internal/dtoa.h | 245 ++ src/3rdparty/rapidjson/internal/ieee754.h | 78 + src/3rdparty/rapidjson/internal/itoa.h | 304 ++ src/3rdparty/rapidjson/internal/meta.h | 181 ++ src/3rdparty/rapidjson/internal/pow10.h | 55 + src/3rdparty/rapidjson/internal/regex.h | 701 +++++ src/3rdparty/rapidjson/internal/stack.h | 230 ++ src/3rdparty/rapidjson/internal/strfunc.h | 55 + src/3rdparty/rapidjson/internal/strtod.h | 269 ++ src/3rdparty/rapidjson/internal/swap.h | 46 + src/3rdparty/rapidjson/istreamwrapper.h | 115 + src/3rdparty/rapidjson/memorybuffer.h | 70 + src/3rdparty/rapidjson/memorystream.h | 71 + src/3rdparty/rapidjson/msinttypes/inttypes.h | 316 ++ src/3rdparty/rapidjson/msinttypes/stdint.h | 300 ++ src/3rdparty/rapidjson/ostreamwrapper.h | 81 + src/3rdparty/rapidjson/pointer.h | 1358 +++++++++ src/3rdparty/rapidjson/prettywriter.h | 255 ++ src/3rdparty/rapidjson/rapidjson.h | 614 ++++ src/3rdparty/rapidjson/reader.h | 1879 ++++++++++++ src/3rdparty/rapidjson/schema.h | 2006 +++++++++++++ src/3rdparty/rapidjson/stream.h | 179 ++ src/3rdparty/rapidjson/stringbuffer.h | 117 + src/3rdparty/rapidjson/writer.h | 610 ++++ src/Options.cpp | 110 +- src/Options.h | 7 +- src/api/Api.cpp | 16 +- src/api/Api.h | 3 +- src/api/ApiState.cpp | 150 +- src/api/ApiState.h | 16 +- src/api/Httpd.cpp | 10 +- src/log/ConsoleLog.cpp | 58 +- src/log/ConsoleLog.h | 5 +- src/log/FileLog.cpp | 9 +- src/log/Log.cpp | 1 + src/log/Log.h | 1 + src/net/Client.cpp | 191 +- src/net/Client.h | 16 +- src/net/Job.cpp | 21 +- src/net/Job.h | 15 +- src/net/JobId.h | 83 + src/net/JobResult.h | 13 +- src/net/SubmitResult.h | 2 + src/workers/DoubleWorker.cpp | 2 +- src/workers/SingleWorker.cpp | 2 +- 78 files changed, 15550 insertions(+), 6420 deletions(-) delete mode 100644 src/3rdparty/jansson/CMakeLists.txt delete mode 100644 src/3rdparty/jansson/LICENSE delete mode 100644 src/3rdparty/jansson/dump.c delete mode 100644 src/3rdparty/jansson/error.c delete mode 100644 src/3rdparty/jansson/hashtable.c delete mode 100644 src/3rdparty/jansson/hashtable.h delete mode 100644 src/3rdparty/jansson/hashtable_seed.c delete mode 100644 src/3rdparty/jansson/jansson.h delete mode 100644 src/3rdparty/jansson/jansson_config.h delete mode 100644 src/3rdparty/jansson/jansson_private.h delete mode 100644 src/3rdparty/jansson/jansson_private_config.h delete mode 100644 src/3rdparty/jansson/load.c delete mode 100644 src/3rdparty/jansson/lookup3.h delete mode 100644 src/3rdparty/jansson/memory.c delete mode 100644 src/3rdparty/jansson/pack_unpack.c delete mode 100644 src/3rdparty/jansson/strbuffer.c delete mode 100644 src/3rdparty/jansson/strbuffer.h delete mode 100644 src/3rdparty/jansson/strconv.c delete mode 100644 src/3rdparty/jansson/utf.c delete mode 100644 src/3rdparty/jansson/utf.h delete mode 100644 src/3rdparty/jansson/value.c create mode 100644 src/3rdparty/rapidjson/allocators.h create mode 100644 src/3rdparty/rapidjson/document.h create mode 100644 src/3rdparty/rapidjson/encodedstream.h create mode 100644 src/3rdparty/rapidjson/encodings.h create mode 100644 src/3rdparty/rapidjson/error/en.h create mode 100644 src/3rdparty/rapidjson/error/error.h create mode 100644 src/3rdparty/rapidjson/filereadstream.h create mode 100644 src/3rdparty/rapidjson/filewritestream.h create mode 100644 src/3rdparty/rapidjson/fwd.h create mode 100644 src/3rdparty/rapidjson/internal/biginteger.h create mode 100644 src/3rdparty/rapidjson/internal/diyfp.h create mode 100644 src/3rdparty/rapidjson/internal/dtoa.h create mode 100644 src/3rdparty/rapidjson/internal/ieee754.h create mode 100644 src/3rdparty/rapidjson/internal/itoa.h create mode 100644 src/3rdparty/rapidjson/internal/meta.h create mode 100644 src/3rdparty/rapidjson/internal/pow10.h create mode 100644 src/3rdparty/rapidjson/internal/regex.h create mode 100644 src/3rdparty/rapidjson/internal/stack.h create mode 100644 src/3rdparty/rapidjson/internal/strfunc.h create mode 100644 src/3rdparty/rapidjson/internal/strtod.h create mode 100644 src/3rdparty/rapidjson/internal/swap.h create mode 100644 src/3rdparty/rapidjson/istreamwrapper.h create mode 100644 src/3rdparty/rapidjson/memorybuffer.h create mode 100644 src/3rdparty/rapidjson/memorystream.h create mode 100644 src/3rdparty/rapidjson/msinttypes/inttypes.h create mode 100644 src/3rdparty/rapidjson/msinttypes/stdint.h create mode 100644 src/3rdparty/rapidjson/ostreamwrapper.h create mode 100644 src/3rdparty/rapidjson/pointer.h create mode 100644 src/3rdparty/rapidjson/prettywriter.h create mode 100644 src/3rdparty/rapidjson/rapidjson.h create mode 100644 src/3rdparty/rapidjson/reader.h create mode 100644 src/3rdparty/rapidjson/schema.h create mode 100644 src/3rdparty/rapidjson/stream.h create mode 100644 src/3rdparty/rapidjson/stringbuffer.h create mode 100644 src/3rdparty/rapidjson/writer.h create mode 100644 src/net/JobId.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cba41204..1705cc00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ set(HEADERS src/Mem.h src/net/Client.h src/net/Job.h + src/net/JobId.h src/net/JobResult.h src/net/Network.h src/net/strategies/DonateStrategy.h @@ -207,11 +208,15 @@ if (NOT WITH_AEON) add_definitions(/DXMRIG_NO_AEON) endif() -find_package(MHD) -if (WITH_HTTPD AND MHD_FOUND) - include_directories(${MHD_INCLUDE_DIRS}) +if (WITH_HTTPD) + find_package(MHD) - set(HTTPD_SOURCES src/api/Httpd.h src/api/Httpd.cpp) + if (MHD_FOUND) + include_directories(${MHD_INCLUDE_DIRS}) + set(HTTPD_SOURCES src/api/Httpd.h src/api/Httpd.cpp) + else() + message(FATAL_ERROR "microhttpd NOT found: use `-DWITH_HTTPD=OFF` to build without http deamon support") + endif() else() add_definitions(/DXMRIG_NO_HTTPD) add_definitions(/DXMRIG_NO_API) @@ -219,10 +224,7 @@ endif() include_directories(src) include_directories(src/3rdparty) -include_directories(src/3rdparty/jansson) include_directories(${UV_INCLUDE_DIR}) -add_subdirectory(src/3rdparty/jansson) - add_executable(xmrig ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES}) -target_link_libraries(xmrig jansson ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) +target_link_libraries(xmrig ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) diff --git a/src/3rdparty/jansson/CMakeLists.txt b/src/3rdparty/jansson/CMakeLists.txt deleted file mode 100644 index 7bd74c67..00000000 --- a/src/3rdparty/jansson/CMakeLists.txt +++ /dev/null @@ -1,25 +0,0 @@ -cmake_minimum_required (VERSION 2.8) -project (jansson C) - -add_definitions(-DHAVE_CONFIG_H) - -# Add the lib sources. -file(GLOB JANSSON_SRC *.c) - -set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Os") - -set(JANSSON_HDR_PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/hashtable.h - ${CMAKE_CURRENT_SOURCE_DIR}/jansson_private.h - ${CMAKE_CURRENT_SOURCE_DIR}/strbuffer.h - ${CMAKE_CURRENT_SOURCE_DIR}/utf.h - ${CMAKE_CURRENT_SOURCE_DIR}/jansson_private_config.h) - -set(JANSSON_HDR_PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/jansson_config.h - ${CMAKE_CURRENT_SOURCE_DIR}/jansson.h) - -add_library(jansson STATIC - ${JANSSON_SRC} - ${JANSSON_HDR_PRIVATE} - ${JANSSON_HDR_PUBLIC}) diff --git a/src/3rdparty/jansson/LICENSE b/src/3rdparty/jansson/LICENSE deleted file mode 100644 index 8ae43e02..00000000 --- a/src/3rdparty/jansson/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -Copyright (c) 2009-2014 Petri Lehtinen - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/src/3rdparty/jansson/dump.c b/src/3rdparty/jansson/dump.c deleted file mode 100644 index 0f8996e7..00000000 --- a/src/3rdparty/jansson/dump.c +++ /dev/null @@ -1,506 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifdef _MSC_VER -#pragma warning(disable:4090) -#endif - -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - -#include "jansson_private.h" - -#include -#include -#include -#include - -#if defined(HAVE_UNISTD_H) -# include -#elif defined(_MSC_VER) -# include -#endif - -#include "jansson.h" -#include "strbuffer.h" -#include "utf.h" - -#define MAX_INTEGER_STR_LENGTH 100 -#define MAX_REAL_STR_LENGTH 100 - -#define FLAGS_TO_INDENT(f) ((f) & 0x1F) -#define FLAGS_TO_PRECISION(f) (((f) >> 11) & 0x1F) - -struct buffer { - const size_t size; - size_t used; - char *data; -}; - -static int dump_to_strbuffer(const char *buffer, size_t size, void *data) -{ - return strbuffer_append_bytes((strbuffer_t *)data, buffer, size); -} - -static int dump_to_buffer(const char *buffer, size_t size, void *data) -{ - struct buffer *buf = (struct buffer *)data; - - if(buf->used + size <= buf->size) - memcpy(&buf->data[buf->used], buffer, size); - - buf->used += size; - return 0; -} - -static int dump_to_file(const char *buffer, size_t size, void *data) -{ - FILE *dest = (FILE *)data; - if(fwrite(buffer, size, 1, dest) != 1) - return -1; - return 0; -} - -static int dump_to_fd(const char *buffer, size_t size, void *data) -{ - int *dest = (int *)data; -# if defined(HAVE_UNISTD_H) - if(write(*dest, buffer, size) == (ssize_t)size) - return 0; -# elif (defined(_MSC_VER)) - if (write(*dest, buffer, (unsigned int) size) == (int) size) - return 0; -# endif - return -1; -} - -/* 32 spaces (the maximum indentation size) */ -static const char whitespace[] = " "; - -static int dump_indent(size_t flags, int depth, int space, json_dump_callback_t dump, void *data) -{ - if(FLAGS_TO_INDENT(flags) > 0) - { - unsigned int ws_count = FLAGS_TO_INDENT(flags), n_spaces = depth * ws_count; - - if(dump("\n", 1, data)) - return -1; - - while(n_spaces > 0) - { - int cur_n = n_spaces < sizeof whitespace - 1 ? n_spaces : sizeof whitespace - 1; - - if(dump(whitespace, cur_n, data)) - return -1; - - n_spaces -= cur_n; - } - } - else if(space && !(flags & JSON_COMPACT)) - { - return dump(" ", 1, data); - } - return 0; -} - -static int dump_string(const char *str, size_t len, json_dump_callback_t dump, void *data, size_t flags) -{ - const char *pos, *end, *lim; - int32_t codepoint; - - if(dump("\"", 1, data)) - return -1; - - end = pos = str; - lim = str + len; - while(1) - { - const char *text; - char seq[13]; - int length; - - while(end < lim) - { - end = utf8_iterate(pos, lim - pos, &codepoint); - if(!end) - return -1; - - /* mandatory escape or control char */ - if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20) - break; - - /* slash */ - if((flags & JSON_ESCAPE_SLASH) && codepoint == '/') - break; - - /* non-ASCII */ - if((flags & JSON_ENSURE_ASCII) && codepoint > 0x7F) - break; - - pos = end; - } - - if(pos != str) { - if(dump(str, pos - str, data)) - return -1; - } - - if(end == pos) - break; - - /* handle \, /, ", and control codes */ - length = 2; - switch(codepoint) - { - case '\\': text = "\\\\"; break; - case '\"': text = "\\\""; break; - case '\b': text = "\\b"; break; - case '\f': text = "\\f"; break; - case '\n': text = "\\n"; break; - case '\r': text = "\\r"; break; - case '\t': text = "\\t"; break; - case '/': text = "\\/"; break; - default: - { - /* codepoint is in BMP */ - if(codepoint < 0x10000) - { - snprintf(seq, sizeof(seq), "\\u%04X", (unsigned int)codepoint); - length = 6; - } - - /* not in BMP -> construct a UTF-16 surrogate pair */ - else - { - int32_t first, last; - - codepoint -= 0x10000; - first = 0xD800 | ((codepoint & 0xffc00) >> 10); - last = 0xDC00 | (codepoint & 0x003ff); - - snprintf(seq, sizeof(seq), "\\u%04X\\u%04X", (unsigned int)first, (unsigned int)last); - length = 12; - } - - text = seq; - break; - } - } - - if(dump(text, length, data)) - return -1; - - str = pos = end; - } - - return dump("\"", 1, data); -} - -static int compare_keys(const void *key1, const void *key2) -{ - return strcmp(*(const char **)key1, *(const char **)key2); -} - -static int do_dump(const json_t *json, size_t flags, int depth, - json_dump_callback_t dump, void *data) -{ - int embed = flags & JSON_EMBED; - - flags &= ~JSON_EMBED; - - if(!json) - return -1; - - switch(json_typeof(json)) { - case JSON_NULL: - return dump("null", 4, data); - - case JSON_TRUE: - return dump("true", 4, data); - - case JSON_FALSE: - return dump("false", 5, data); - - case JSON_INTEGER: - { - char buffer[MAX_INTEGER_STR_LENGTH]; - int size; - - size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, - "%" JSON_INTEGER_FORMAT, - json_integer_value(json)); - if(size < 0 || size >= MAX_INTEGER_STR_LENGTH) - return -1; - - return dump(buffer, size, data); - } - - case JSON_REAL: - { - char buffer[MAX_REAL_STR_LENGTH]; - int size; - double value = json_real_value(json); - - size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value, - FLAGS_TO_PRECISION(flags)); - if(size < 0) - return -1; - - return dump(buffer, size, data); - } - - case JSON_STRING: - return dump_string(json_string_value(json), json_string_length(json), dump, data, flags); - - case JSON_ARRAY: - { - size_t n; - size_t i; - - json_array_t *array; - - /* detect circular references */ - array = json_to_array(json); - if(array->visited) - goto array_error; - array->visited = 1; - - n = json_array_size(json); - - if(!embed && dump("[", 1, data)) - goto array_error; - if(n == 0) { - array->visited = 0; - return embed ? 0 : dump("]", 1, data); - } - if(dump_indent(flags, depth + 1, 0, dump, data)) - goto array_error; - - for(i = 0; i < n; ++i) { - if(do_dump(json_array_get(json, i), flags, depth + 1, - dump, data)) - goto array_error; - - if(i < n - 1) - { - if(dump(",", 1, data) || - dump_indent(flags, depth + 1, 1, dump, data)) - goto array_error; - } - else - { - if(dump_indent(flags, depth, 0, dump, data)) - goto array_error; - } - } - - array->visited = 0; - return embed ? 0 : dump("]", 1, data); - - array_error: - array->visited = 0; - return -1; - } - - case JSON_OBJECT: - { - json_object_t *object; - void *iter; - const char *separator; - int separator_length; - - if(flags & JSON_COMPACT) { - separator = ":"; - separator_length = 1; - } - else { - separator = ": "; - separator_length = 2; - } - - /* detect circular references */ - object = json_to_object(json); - if(object->visited) - goto object_error; - object->visited = 1; - - iter = json_object_iter((json_t *)json); - - if(!embed && dump("{", 1, data)) - goto object_error; - if(!iter) { - object->visited = 0; - return embed ? 0 : dump("}", 1, data); - } - if(dump_indent(flags, depth + 1, 0, dump, data)) - goto object_error; - - if(flags & JSON_SORT_KEYS) - { - const char **keys; - size_t size, i; - - size = json_object_size(json); - keys = jsonp_malloc(size * sizeof(const char *)); - if(!keys) - goto object_error; - - i = 0; - while(iter) - { - keys[i] = json_object_iter_key(iter); - iter = json_object_iter_next((json_t *)json, iter); - i++; - } - assert(i == size); - - qsort(keys, size, sizeof(const char *), compare_keys); - - for(i = 0; i < size; i++) - { - const char *key; - json_t *value; - - key = keys[i]; - value = json_object_get(json, key); - assert(value); - - dump_string(key, strlen(key), dump, data, flags); - if(dump(separator, separator_length, data) || - do_dump(value, flags, depth + 1, dump, data)) - { - jsonp_free(keys); - goto object_error; - } - - if(i < size - 1) - { - if(dump(",", 1, data) || - dump_indent(flags, depth + 1, 1, dump, data)) - { - jsonp_free(keys); - goto object_error; - } - } - else - { - if(dump_indent(flags, depth, 0, dump, data)) - { - jsonp_free(keys); - goto object_error; - } - } - } - - jsonp_free(keys); - } - else - { - /* Don't sort keys */ - - while(iter) - { - void *next = json_object_iter_next((json_t *)json, iter); - const char *key = json_object_iter_key(iter); - - dump_string(key, strlen(key), dump, data, flags); - if(dump(separator, separator_length, data) || - do_dump(json_object_iter_value(iter), flags, depth + 1, - dump, data)) - goto object_error; - - if(next) - { - if(dump(",", 1, data) || - dump_indent(flags, depth + 1, 1, dump, data)) - goto object_error; - } - else - { - if(dump_indent(flags, depth, 0, dump, data)) - goto object_error; - } - - iter = next; - } - } - - object->visited = 0; - return embed ? 0 : dump("}", 1, data); - - object_error: - object->visited = 0; - return -1; - } - - default: - /* not reached */ - return -1; - } -} - -char *json_dumps(const json_t *json, size_t flags) -{ - strbuffer_t strbuff; - char *result; - - if(strbuffer_init(&strbuff)) - return NULL; - - if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags)) - result = NULL; - else - result = jsonp_strdup(strbuffer_value(&strbuff)); - - strbuffer_close(&strbuff); - return result; -} - -size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags) -{ - struct buffer buf = { size, 0, buffer }; - - if(json_dump_callback(json, dump_to_buffer, (void *)&buf, flags)) - return 0; - - return buf.used; -} - -int json_dumpf(const json_t *json, FILE *output, size_t flags) -{ - return json_dump_callback(json, dump_to_file, (void *)output, flags); -} - -int json_dumpfd(const json_t *json, int output, size_t flags) -{ - return json_dump_callback(json, dump_to_fd, (void *)&output, flags); -} - -int json_dump_file(const json_t *json, const char *path, size_t flags) -{ - int result; - - FILE *output = fopen(path, "w"); - if(!output) - return -1; - - result = json_dumpf(json, output, flags); - - fclose(output); - return result; -} - -int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags) -{ - if(!(flags & JSON_ENCODE_ANY)) { - if(!json_is_array(json) && !json_is_object(json)) - return -1; - } - - return do_dump(json, flags, 0, callback, data); -} diff --git a/src/3rdparty/jansson/error.c b/src/3rdparty/jansson/error.c deleted file mode 100644 index 58c83790..00000000 --- a/src/3rdparty/jansson/error.c +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include "jansson_private.h" - -void jsonp_error_init(json_error_t *error, const char *source) -{ - if(error) - { - error->text[0] = '\0'; - error->line = -1; - error->column = -1; - error->position = 0; - if(source) - jsonp_error_set_source(error, source); - else - error->source[0] = '\0'; - } -} - -void jsonp_error_set_source(json_error_t *error, const char *source) -{ - size_t length; - - if(!error || !source) - return; - - length = strlen(source); - if(length < JSON_ERROR_SOURCE_LENGTH) - strncpy(error->source, source, length + 1); - else { - size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4; - strncpy(error->source, "...", 3); - strncpy(error->source + 3, source + extra, length - extra + 1); - } -} - -void jsonp_error_set(json_error_t *error, int line, int column, - size_t position, const char *msg, ...) -{ - va_list ap; - - va_start(ap, msg); - jsonp_error_vset(error, line, column, position, msg, ap); - va_end(ap); -} - -void jsonp_error_vset(json_error_t *error, int line, int column, - size_t position, const char *msg, va_list ap) -{ - if(!error) - return; - - if(error->text[0] != '\0') { - /* error already set */ - return; - } - - error->line = line; - error->column = column; - error->position = (int)position; - - vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap); - error->text[JSON_ERROR_TEXT_LENGTH - 1] = '\0'; -} diff --git a/src/3rdparty/jansson/hashtable.c b/src/3rdparty/jansson/hashtable.c deleted file mode 100644 index dcc1687b..00000000 --- a/src/3rdparty/jansson/hashtable.c +++ /dev/null @@ -1,360 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * This library is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifdef _MSC_VER -#pragma warning(disable:4334) -#endif - -#if HAVE_CONFIG_H -#include -#endif - -#include -#include - -#if HAVE_STDINT_H -#include -#endif - -#include /* for JSON_INLINE */ -#include "jansson_private.h" /* for container_of() */ -#include "hashtable.h" - -#ifndef INITIAL_HASHTABLE_ORDER -#define INITIAL_HASHTABLE_ORDER 3 -#endif - -typedef struct hashtable_list list_t; -typedef struct hashtable_pair pair_t; -typedef struct hashtable_bucket bucket_t; - -extern volatile uint32_t hashtable_seed; - -/* Implementation of the hash function */ -#include "lookup3.h" - -#define list_to_pair(list_) container_of(list_, pair_t, list) -#define ordered_list_to_pair(list_) container_of(list_, pair_t, ordered_list) -#define hash_str(key) ((size_t)hashlittle((key), strlen(key), hashtable_seed)) - -static JSON_INLINE void list_init(list_t *list) -{ - list->next = list; - list->prev = list; -} - -static JSON_INLINE void list_insert(list_t *list, list_t *node) -{ - node->next = list; - node->prev = list->prev; - list->prev->next = node; - list->prev = node; -} - -static JSON_INLINE void list_remove(list_t *list) -{ - list->prev->next = list->next; - list->next->prev = list->prev; -} - -static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket) -{ - return bucket->first == &hashtable->list && bucket->first == bucket->last; -} - -static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket, - list_t *list) -{ - if(bucket_is_empty(hashtable, bucket)) - { - list_insert(&hashtable->list, list); - bucket->first = bucket->last = list; - } - else - { - list_insert(bucket->first, list); - bucket->first = list; - } -} - -static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket, - const char *key, size_t hash) -{ - list_t *list; - pair_t *pair; - - if(bucket_is_empty(hashtable, bucket)) - return NULL; - - list = bucket->first; - while(1) - { - pair = list_to_pair(list); - if(pair->hash == hash && strcmp(pair->key, key) == 0) - return pair; - - if(list == bucket->last) - break; - - list = list->next; - } - - return NULL; -} - -/* returns 0 on success, -1 if key was not found */ -static int hashtable_do_del(hashtable_t *hashtable, - const char *key, size_t hash) -{ - pair_t *pair; - bucket_t *bucket; - size_t index; - - index = hash & hashmask(hashtable->order); - bucket = &hashtable->buckets[index]; - - pair = hashtable_find_pair(hashtable, bucket, key, hash); - if(!pair) - return -1; - - if(&pair->list == bucket->first && &pair->list == bucket->last) - bucket->first = bucket->last = &hashtable->list; - - else if(&pair->list == bucket->first) - bucket->first = pair->list.next; - - else if(&pair->list == bucket->last) - bucket->last = pair->list.prev; - - list_remove(&pair->list); - list_remove(&pair->ordered_list); - json_decref(pair->value); - - jsonp_free(pair); - hashtable->size--; - - return 0; -} - -static void hashtable_do_clear(hashtable_t *hashtable) -{ - list_t *list, *next; - pair_t *pair; - - for(list = hashtable->list.next; list != &hashtable->list; list = next) - { - next = list->next; - pair = list_to_pair(list); - json_decref(pair->value); - jsonp_free(pair); - } -} - -static int hashtable_do_rehash(hashtable_t *hashtable) -{ - list_t *list, *next; - pair_t *pair; - size_t i, index, new_size, new_order; - struct hashtable_bucket *new_buckets; - - new_order = hashtable->order + 1; - new_size = hashsize(new_order); - - new_buckets = jsonp_malloc(new_size * sizeof(bucket_t)); - if(!new_buckets) - return -1; - - jsonp_free(hashtable->buckets); - hashtable->buckets = new_buckets; - hashtable->order = new_order; - - for(i = 0; i < hashsize(hashtable->order); i++) - { - hashtable->buckets[i].first = hashtable->buckets[i].last = - &hashtable->list; - } - - list = hashtable->list.next; - list_init(&hashtable->list); - - for(; list != &hashtable->list; list = next) { - next = list->next; - pair = list_to_pair(list); - index = pair->hash % new_size; - insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list); - } - - return 0; -} - - -int hashtable_init(hashtable_t *hashtable) -{ - size_t i; - - hashtable->size = 0; - hashtable->order = INITIAL_HASHTABLE_ORDER; - hashtable->buckets = jsonp_malloc(hashsize(hashtable->order) * sizeof(bucket_t)); - if(!hashtable->buckets) - return -1; - - list_init(&hashtable->list); - list_init(&hashtable->ordered_list); - - for(i = 0; i < hashsize(hashtable->order); i++) - { - hashtable->buckets[i].first = hashtable->buckets[i].last = - &hashtable->list; - } - - return 0; -} - -void hashtable_close(hashtable_t *hashtable) -{ - hashtable_do_clear(hashtable); - jsonp_free(hashtable->buckets); -} - -int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value) -{ - pair_t *pair; - bucket_t *bucket; - size_t hash, index; - - /* rehash if the load ratio exceeds 1 */ - if(hashtable->size >= hashsize(hashtable->order)) - if(hashtable_do_rehash(hashtable)) - return -1; - - hash = hash_str(key); - index = hash & hashmask(hashtable->order); - bucket = &hashtable->buckets[index]; - pair = hashtable_find_pair(hashtable, bucket, key, hash); - - if(pair) - { - json_decref(pair->value); - pair->value = value; - } - else - { - /* offsetof(...) returns the size of pair_t without the last, - flexible member. This way, the correct amount is - allocated. */ - - size_t len = strlen(key); - if(len >= (size_t)-1 - offsetof(pair_t, key)) { - /* Avoid an overflow if the key is very long */ - return -1; - } - - pair = jsonp_malloc(offsetof(pair_t, key) + len + 1); - if(!pair) - return -1; - - pair->hash = hash; - strncpy(pair->key, key, len + 1); - pair->value = value; - list_init(&pair->list); - list_init(&pair->ordered_list); - - insert_to_bucket(hashtable, bucket, &pair->list); - list_insert(&hashtable->ordered_list, &pair->ordered_list); - - hashtable->size++; - } - return 0; -} - -void *hashtable_get(hashtable_t *hashtable, const char *key) -{ - pair_t *pair; - size_t hash; - bucket_t *bucket; - - hash = hash_str(key); - bucket = &hashtable->buckets[hash & hashmask(hashtable->order)]; - - pair = hashtable_find_pair(hashtable, bucket, key, hash); - if(!pair) - return NULL; - - return pair->value; -} - -int hashtable_del(hashtable_t *hashtable, const char *key) -{ - size_t hash = hash_str(key); - return hashtable_do_del(hashtable, key, hash); -} - -void hashtable_clear(hashtable_t *hashtable) -{ - size_t i; - - hashtable_do_clear(hashtable); - - for(i = 0; i < hashsize(hashtable->order); i++) - { - hashtable->buckets[i].first = hashtable->buckets[i].last = - &hashtable->list; - } - - list_init(&hashtable->list); - list_init(&hashtable->ordered_list); - hashtable->size = 0; -} - -void *hashtable_iter(hashtable_t *hashtable) -{ - return hashtable_iter_next(hashtable, &hashtable->ordered_list); -} - -void *hashtable_iter_at(hashtable_t *hashtable, const char *key) -{ - pair_t *pair; - size_t hash; - bucket_t *bucket; - - hash = hash_str(key); - bucket = &hashtable->buckets[hash & hashmask(hashtable->order)]; - - pair = hashtable_find_pair(hashtable, bucket, key, hash); - if(!pair) - return NULL; - - return &pair->ordered_list; -} - -void *hashtable_iter_next(hashtable_t *hashtable, void *iter) -{ - list_t *list = (list_t *)iter; - if(list->next == &hashtable->ordered_list) - return NULL; - return list->next; -} - -void *hashtable_iter_key(void *iter) -{ - pair_t *pair = ordered_list_to_pair((list_t *)iter); - return pair->key; -} - -void *hashtable_iter_value(void *iter) -{ - pair_t *pair = ordered_list_to_pair((list_t *)iter); - return pair->value; -} - -void hashtable_iter_set(void *iter, json_t *value) -{ - pair_t *pair = ordered_list_to_pair((list_t *)iter); - - json_decref(pair->value); - pair->value = value; -} diff --git a/src/3rdparty/jansson/hashtable.h b/src/3rdparty/jansson/hashtable.h deleted file mode 100644 index d4c32ae0..00000000 --- a/src/3rdparty/jansson/hashtable.h +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * This library is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef HASHTABLE_H -#define HASHTABLE_H - -#include -#include "jansson.h" - -struct hashtable_list { - struct hashtable_list *prev; - struct hashtable_list *next; -}; - -/* "pair" may be a bit confusing a name, but think of it as a - key-value pair. In this case, it just encodes some extra data, - too */ -struct hashtable_pair { - struct hashtable_list list; - struct hashtable_list ordered_list; - size_t hash; - json_t *value; - char key[1]; -}; - -struct hashtable_bucket { - struct hashtable_list *first; - struct hashtable_list *last; -}; - -typedef struct hashtable { - size_t size; - struct hashtable_bucket *buckets; - size_t order; /* hashtable has pow(2, order) buckets */ - struct hashtable_list list; - struct hashtable_list ordered_list; -} hashtable_t; - - -#define hashtable_key_to_iter(key_) \ - (&(container_of(key_, struct hashtable_pair, key)->ordered_list)) - - -/** - * hashtable_init - Initialize a hashtable object - * - * @hashtable: The (statically allocated) hashtable object - * - * Initializes a statically allocated hashtable object. The object - * should be cleared with hashtable_close when it's no longer used. - * - * Returns 0 on success, -1 on error (out of memory). - */ -int hashtable_init(hashtable_t *hashtable); - -/** - * hashtable_close - Release all resources used by a hashtable object - * - * @hashtable: The hashtable - * - * Destroys a statically allocated hashtable object. - */ -void hashtable_close(hashtable_t *hashtable); - -/** - * hashtable_set - Add/modify value in hashtable - * - * @hashtable: The hashtable object - * @key: The key - * @serial: For addition order of keys - * @value: The value - * - * If a value with the given key already exists, its value is replaced - * with the new value. Value is "stealed" in the sense that hashtable - * doesn't increment its refcount but decreases the refcount when the - * value is no longer needed. - * - * Returns 0 on success, -1 on failure (out of memory). - */ -int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value); - -/** - * hashtable_get - Get a value associated with a key - * - * @hashtable: The hashtable object - * @key: The key - * - * Returns value if it is found, or NULL otherwise. - */ -void *hashtable_get(hashtable_t *hashtable, const char *key); - -/** - * hashtable_del - Remove a value from the hashtable - * - * @hashtable: The hashtable object - * @key: The key - * - * Returns 0 on success, or -1 if the key was not found. - */ -int hashtable_del(hashtable_t *hashtable, const char *key); - -/** - * hashtable_clear - Clear hashtable - * - * @hashtable: The hashtable object - * - * Removes all items from the hashtable. - */ -void hashtable_clear(hashtable_t *hashtable); - -/** - * hashtable_iter - Iterate over hashtable - * - * @hashtable: The hashtable object - * - * Returns an opaque iterator to the first element in the hashtable. - * The iterator should be passed to hashtable_iter_* functions. - * The hashtable items are not iterated over in any particular order. - * - * There's no need to free the iterator in any way. The iterator is - * valid as long as the item that is referenced by the iterator is not - * deleted. Other values may be added or deleted. In particular, - * hashtable_iter_next() may be called on an iterator, and after that - * the key/value pair pointed by the old iterator may be deleted. - */ -void *hashtable_iter(hashtable_t *hashtable); - -/** - * hashtable_iter_at - Return an iterator at a specific key - * - * @hashtable: The hashtable object - * @key: The key that the iterator should point to - * - * Like hashtable_iter() but returns an iterator pointing to a - * specific key. - */ -void *hashtable_iter_at(hashtable_t *hashtable, const char *key); - -/** - * hashtable_iter_next - Advance an iterator - * - * @hashtable: The hashtable object - * @iter: The iterator - * - * Returns a new iterator pointing to the next element in the - * hashtable or NULL if the whole hastable has been iterated over. - */ -void *hashtable_iter_next(hashtable_t *hashtable, void *iter); - -/** - * hashtable_iter_key - Retrieve the key pointed by an iterator - * - * @iter: The iterator - */ -void *hashtable_iter_key(void *iter); - -/** - * hashtable_iter_value - Retrieve the value pointed by an iterator - * - * @iter: The iterator - */ -void *hashtable_iter_value(void *iter); - -/** - * hashtable_iter_set - Set the value pointed by an iterator - * - * @iter: The iterator - * @value: The value to set - */ -void hashtable_iter_set(void *iter, json_t *value); - -#endif diff --git a/src/3rdparty/jansson/hashtable_seed.c b/src/3rdparty/jansson/hashtable_seed.c deleted file mode 100644 index 8aed5406..00000000 --- a/src/3rdparty/jansson/hashtable_seed.c +++ /dev/null @@ -1,277 +0,0 @@ -/* Generate sizeof(uint32_t) bytes of as random data as possible to seed - the hash function. -*/ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include - -#ifdef HAVE_STDINT_H -#include -#endif - -#ifdef HAVE_FCNTL_H -#include -#endif - -#ifdef HAVE_SCHED_H -#include -#endif - -#ifdef HAVE_UNISTD_H -#include -#endif - -#ifdef HAVE_SYS_STAT_H -#include -#endif - -#ifdef HAVE_SYS_TIME_H -#include -#endif - -#ifdef HAVE_SYS_TYPES_H -#include -#endif - -#if defined(_WIN32) -/* For GetModuleHandle(), GetProcAddress() and GetCurrentProcessId() */ -#include -#endif - -#include "jansson.h" - - -static uint32_t buf_to_uint32(char *data) { - size_t i; - uint32_t result = 0; - - for (i = 0; i < sizeof(uint32_t); i++) - result = (result << 8) | (unsigned char)data[i]; - - return result; -} - - - -/* /dev/urandom */ -#if !defined(_WIN32) && defined(USE_URANDOM) -static int seed_from_urandom(uint32_t *seed) { - /* Use unbuffered I/O if we have open(), close() and read(). Otherwise - fall back to fopen() */ - - char data[sizeof(uint32_t)]; - int ok; - -#if defined(HAVE_OPEN) && defined(HAVE_CLOSE) && defined(HAVE_READ) - int urandom; - urandom = open("/dev/urandom", O_RDONLY); - if (urandom == -1) - return 1; - - ok = read(urandom, data, sizeof(uint32_t)) == sizeof(uint32_t); - close(urandom); -#else - FILE *urandom; - - urandom = fopen("/dev/urandom", "rb"); - if (!urandom) - return 1; - - ok = fread(data, 1, sizeof(uint32_t), urandom) == sizeof(uint32_t); - fclose(urandom); -#endif - - if (!ok) - return 1; - - *seed = buf_to_uint32(data); - return 0; -} -#endif - -/* Windows Crypto API */ -#if defined(_WIN32) && defined(USE_WINDOWS_CRYPTOAPI) -#include - -typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv, LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags); -typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer); -typedef BOOL (WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV hProv, DWORD dwFlags); - -static int seed_from_windows_cryptoapi(uint32_t *seed) -{ - HINSTANCE hAdvAPI32 = NULL; - CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; - CRYPTGENRANDOM pCryptGenRandom = NULL; - CRYPTRELEASECONTEXT pCryptReleaseContext = NULL; - HCRYPTPROV hCryptProv = 0; - BYTE data[sizeof(uint32_t)]; - int ok; - - hAdvAPI32 = GetModuleHandle(TEXT("advapi32.dll")); - if(hAdvAPI32 == NULL) - return 1; - - pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(hAdvAPI32, "CryptAcquireContextA"); - if (!pCryptAcquireContext) - return 1; - - pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(hAdvAPI32, "CryptGenRandom"); - if (!pCryptGenRandom) - return 1; - - pCryptReleaseContext = (CRYPTRELEASECONTEXT)GetProcAddress(hAdvAPI32, "CryptReleaseContext"); - if (!pCryptReleaseContext) - return 1; - - if (!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) - return 1; - - ok = pCryptGenRandom(hCryptProv, sizeof(uint32_t), data); - pCryptReleaseContext(hCryptProv, 0); - - if (!ok) - return 1; - - *seed = buf_to_uint32((char *)data); - return 0; -} -#endif - -/* gettimeofday() and getpid() */ -static int seed_from_timestamp_and_pid(uint32_t *seed) { -#ifdef HAVE_GETTIMEOFDAY - /* XOR of seconds and microseconds */ - struct timeval tv; - gettimeofday(&tv, NULL); - *seed = (uint32_t)tv.tv_sec ^ (uint32_t)tv.tv_usec; -#else - /* Seconds only */ - *seed = (uint32_t)time(NULL); -#endif - - /* XOR with PID for more randomness */ -#if defined(_WIN32) - *seed ^= (uint32_t)GetCurrentProcessId(); -#elif defined(HAVE_GETPID) - *seed ^= (uint32_t)getpid(); -#endif - - return 0; -} - -static uint32_t generate_seed() { - uint32_t seed; - int done = 0; - -#if !defined(_WIN32) && defined(USE_URANDOM) - if (seed_from_urandom(&seed) == 0) - done = 1; -#endif - -#if defined(_WIN32) && defined(USE_WINDOWS_CRYPTOAPI) - if (seed_from_windows_cryptoapi(&seed) == 0) - done = 1; -#endif - - if (!done) { - /* Fall back to timestamp and PID if no better randomness is - available */ - seed_from_timestamp_and_pid(&seed); - } - - /* Make sure the seed is never zero */ - if (seed == 0) - seed = 1; - - return seed; -} - - -volatile uint32_t hashtable_seed = 0; - -#if defined(HAVE_ATOMIC_BUILTINS) && (defined(HAVE_SCHED_YIELD) || !defined(_WIN32)) -static volatile char seed_initialized = 0; - -void json_object_seed(size_t seed) { - uint32_t new_seed = (uint32_t)seed; - - if (hashtable_seed == 0) { - if (__atomic_test_and_set(&seed_initialized, __ATOMIC_RELAXED) == 0) { - /* Do the seeding ourselves */ - if (new_seed == 0) - new_seed = generate_seed(); - - __atomic_store_n(&hashtable_seed, new_seed, __ATOMIC_RELEASE); - } else { - /* Wait for another thread to do the seeding */ - do { -#ifdef HAVE_SCHED_YIELD - sched_yield(); -#endif - } while(__atomic_load_n(&hashtable_seed, __ATOMIC_ACQUIRE) == 0); - } - } -} -#elif defined(HAVE_SYNC_BUILTINS) && (defined(HAVE_SCHED_YIELD) || !defined(_WIN32)) -void json_object_seed(size_t seed) { - uint32_t new_seed = (uint32_t)seed; - - if (hashtable_seed == 0) { - if (new_seed == 0) { - /* Explicit synchronization fences are not supported by the - __sync builtins, so every thread getting here has to - generate the seed value. - */ - new_seed = generate_seed(); - } - - do { - if (__sync_bool_compare_and_swap(&hashtable_seed, 0, new_seed)) { - /* We were the first to seed */ - break; - } else { - /* Wait for another thread to do the seeding */ -#ifdef HAVE_SCHED_YIELD - sched_yield(); -#endif - } - } while(hashtable_seed == 0); - } -} -#elif defined(_WIN32) -static long seed_initialized = 0; -void json_object_seed(size_t seed) { - uint32_t new_seed = (uint32_t)seed; - - if (hashtable_seed == 0) { - if (InterlockedIncrement(&seed_initialized) == 1) { - /* Do the seeding ourselves */ - if (new_seed == 0) - new_seed = generate_seed(); - - hashtable_seed = new_seed; - } else { - /* Wait for another thread to do the seeding */ - do { - SwitchToThread(); - } while (hashtable_seed == 0); - } - } -} -#else -/* Fall back to a thread-unsafe version */ -void json_object_seed(size_t seed) { - uint32_t new_seed = (uint32_t)seed; - - if (hashtable_seed == 0) { - if (new_seed == 0) - new_seed = generate_seed(); - - hashtable_seed = new_seed; - } -} -#endif diff --git a/src/3rdparty/jansson/jansson.h b/src/3rdparty/jansson/jansson.h deleted file mode 100644 index a5927bd6..00000000 --- a/src/3rdparty/jansson/jansson.h +++ /dev/null @@ -1,315 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef JANSSON_H -#define JANSSON_H - -#include -#include /* for size_t */ -#include - -#include "jansson_config.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/* version */ - -#define JANSSON_MAJOR_VERSION 2 -#define JANSSON_MINOR_VERSION 10 -#define JANSSON_MICRO_VERSION 0 - -/* Micro version is omitted if it's 0 */ -#define JANSSON_VERSION "2.10" - -/* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this - for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */ -#define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \ - (JANSSON_MINOR_VERSION << 8) | \ - (JANSSON_MICRO_VERSION << 0)) - - -/* types */ - -typedef enum { - JSON_OBJECT, - JSON_ARRAY, - JSON_STRING, - JSON_INTEGER, - JSON_REAL, - JSON_TRUE, - JSON_FALSE, - JSON_NULL -} json_type; - -typedef struct json_t { - json_type type; - size_t refcount; -} json_t; - -#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */ -#if JSON_INTEGER_IS_LONG_LONG -#ifdef _WIN32 -#define JSON_INTEGER_FORMAT "I64d" -#else -#define JSON_INTEGER_FORMAT "lld" -#endif -typedef long long json_int_t; -#else -#define JSON_INTEGER_FORMAT "ld" -typedef long json_int_t; -#endif /* JSON_INTEGER_IS_LONG_LONG */ -#endif - -#define json_typeof(json) ((json)->type) -#define json_is_object(json) ((json) && json_typeof(json) == JSON_OBJECT) -#define json_is_array(json) ((json) && json_typeof(json) == JSON_ARRAY) -#define json_is_string(json) ((json) && json_typeof(json) == JSON_STRING) -#define json_is_integer(json) ((json) && json_typeof(json) == JSON_INTEGER) -#define json_is_real(json) ((json) && json_typeof(json) == JSON_REAL) -#define json_is_number(json) (json_is_integer(json) || json_is_real(json)) -#define json_is_true(json) ((json) && json_typeof(json) == JSON_TRUE) -#define json_is_false(json) ((json) && json_typeof(json) == JSON_FALSE) -#define json_boolean_value json_is_true -#define json_is_boolean(json) (json_is_true(json) || json_is_false(json)) -#define json_is_null(json) ((json) && json_typeof(json) == JSON_NULL) - -/* construction, destruction, reference counting */ - -json_t *json_object(void); -json_t *json_array(void); -json_t *json_string(const char *value); -json_t *json_stringn(const char *value, size_t len); -json_t *json_string_nocheck(const char *value); -json_t *json_stringn_nocheck(const char *value, size_t len); -json_t *json_integer(json_int_t value); -json_t *json_real(double value); -json_t *json_true(void); -json_t *json_false(void); -#define json_boolean(val) ((val) ? json_true() : json_false()) -json_t *json_null(void); - -static JSON_INLINE -json_t *json_incref(json_t *json) -{ - if(json && json->refcount != (size_t)-1) - ++json->refcount; - return json; -} - -/* do not call json_delete directly */ -void json_delete(json_t *json); - -static JSON_INLINE -void json_decref(json_t *json) -{ - if(json && json->refcount != (size_t)-1 && --json->refcount == 0) - json_delete(json); -} - -#if defined(__GNUC__) || defined(__clang__) -static JSON_INLINE -void json_decrefp(json_t **json) -{ - if(json) { - json_decref(*json); - *json = NULL; - } -} - -#define json_auto_t json_t __attribute__((cleanup(json_decrefp))) -#endif - - -/* error reporting */ - -#define JSON_ERROR_TEXT_LENGTH 160 -#define JSON_ERROR_SOURCE_LENGTH 80 - -typedef struct { - int line; - int column; - int position; - char source[JSON_ERROR_SOURCE_LENGTH]; - char text[JSON_ERROR_TEXT_LENGTH]; -} json_error_t; - - -/* getters, setters, manipulation */ - -void json_object_seed(size_t seed); -size_t json_object_size(const json_t *object); -json_t *json_object_get(const json_t *object, const char *key); -int json_object_set_new(json_t *object, const char *key, json_t *value); -int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value); -int json_object_del(json_t *object, const char *key); -int json_object_clear(json_t *object); -int json_object_update(json_t *object, json_t *other); -int json_object_update_existing(json_t *object, json_t *other); -int json_object_update_missing(json_t *object, json_t *other); -void *json_object_iter(json_t *object); -void *json_object_iter_at(json_t *object, const char *key); -void *json_object_key_to_iter(const char *key); -void *json_object_iter_next(json_t *object, void *iter); -const char *json_object_iter_key(void *iter); -json_t *json_object_iter_value(void *iter); -int json_object_iter_set_new(json_t *object, void *iter, json_t *value); - -#define json_object_foreach(object, key, value) \ - for(key = json_object_iter_key(json_object_iter(object)); \ - key && (value = json_object_iter_value(json_object_key_to_iter(key))); \ - key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key)))) - -#define json_object_foreach_safe(object, n, key, value) \ - for(key = json_object_iter_key(json_object_iter(object)), \ - n = json_object_iter_next(object, json_object_key_to_iter(key)); \ - key && (value = json_object_iter_value(json_object_key_to_iter(key))); \ - key = json_object_iter_key(n), \ - n = json_object_iter_next(object, json_object_key_to_iter(key))) - -#define json_array_foreach(array, index, value) \ - for(index = 0; \ - index < json_array_size(array) && (value = json_array_get(array, index)); \ - index++) - -static JSON_INLINE -int json_object_set(json_t *object, const char *key, json_t *value) -{ - return json_object_set_new(object, key, json_incref(value)); -} - -static JSON_INLINE -int json_object_set_nocheck(json_t *object, const char *key, json_t *value) -{ - return json_object_set_new_nocheck(object, key, json_incref(value)); -} - -static JSON_INLINE -int json_object_iter_set(json_t *object, void *iter, json_t *value) -{ - return json_object_iter_set_new(object, iter, json_incref(value)); -} - -size_t json_array_size(const json_t *array); -json_t *json_array_get(const json_t *array, size_t index); -int json_array_set_new(json_t *array, size_t index, json_t *value); -int json_array_append_new(json_t *array, json_t *value); -int json_array_insert_new(json_t *array, size_t index, json_t *value); -int json_array_remove(json_t *array, size_t index); -int json_array_clear(json_t *array); -int json_array_extend(json_t *array, json_t *other); - -static JSON_INLINE -int json_array_set(json_t *array, size_t ind, json_t *value) -{ - return json_array_set_new(array, ind, json_incref(value)); -} - -static JSON_INLINE -int json_array_append(json_t *array, json_t *value) -{ - return json_array_append_new(array, json_incref(value)); -} - -static JSON_INLINE -int json_array_insert(json_t *array, size_t ind, json_t *value) -{ - return json_array_insert_new(array, ind, json_incref(value)); -} - -const char *json_string_value(const json_t *string); -size_t json_string_length(const json_t *string); -json_int_t json_integer_value(const json_t *integer); -double json_real_value(const json_t *real); -double json_number_value(const json_t *json); - -int json_string_set(json_t *string, const char *value); -int json_string_setn(json_t *string, const char *value, size_t len); -int json_string_set_nocheck(json_t *string, const char *value); -int json_string_setn_nocheck(json_t *string, const char *value, size_t len); -int json_integer_set(json_t *integer, json_int_t value); -int json_real_set(json_t *real, double value); - -/* pack, unpack */ - -json_t *json_pack(const char *fmt, ...); -json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...); -json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap); - -#define JSON_VALIDATE_ONLY 0x1 -#define JSON_STRICT 0x2 - -int json_unpack(json_t *root, const char *fmt, ...); -int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...); -int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap); - - -/* equality */ - -int json_equal(json_t *value1, json_t *value2); - - -/* copying */ - -json_t *json_copy(json_t *value); -json_t *json_deep_copy(const json_t *value); - - -/* decoding */ - -#define JSON_REJECT_DUPLICATES 0x1 -#define JSON_DISABLE_EOF_CHECK 0x2 -#define JSON_DECODE_ANY 0x4 -#define JSON_DECODE_INT_AS_REAL 0x8 -#define JSON_ALLOW_NUL 0x10 - -typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data); - -json_t *json_loads(const char *input, size_t flags, json_error_t *error); -json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error); -json_t *json_loadf(FILE *input, size_t flags, json_error_t *error); -json_t *json_loadfd(int input, size_t flags, json_error_t *error); -json_t *json_load_file(const char *path, size_t flags, json_error_t *error); -json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error); - - -/* encoding */ - -#define JSON_MAX_INDENT 0x1F -#define JSON_INDENT(n) ((n) & JSON_MAX_INDENT) -#define JSON_COMPACT 0x20 -#define JSON_ENSURE_ASCII 0x40 -#define JSON_SORT_KEYS 0x80 -#define JSON_PRESERVE_ORDER 0x100 -#define JSON_ENCODE_ANY 0x200 -#define JSON_ESCAPE_SLASH 0x400 -#define JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11) -#define JSON_EMBED 0x10000 - -typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data); - -char *json_dumps(const json_t *json, size_t flags); -size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags); -int json_dumpf(const json_t *json, FILE *output, size_t flags); -int json_dumpfd(const json_t *json, int output, size_t flags); -int json_dump_file(const json_t *json, const char *path, size_t flags); -int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags); - -/* custom memory allocation */ - -typedef void *(*json_malloc_t)(size_t); -typedef void (*json_free_t)(void *); - -void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn); -void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/3rdparty/jansson/jansson_config.h b/src/3rdparty/jansson/jansson_config.h deleted file mode 100644 index f1a5ddd2..00000000 --- a/src/3rdparty/jansson/jansson_config.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2010-2016 Petri Lehtinen - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - * - * - * This file specifies a part of the site-specific configuration for - * Jansson, namely those things that affect the public API in - * jansson.h. - * - * The configure script copies this file to jansson_config.h and - * replaces @var@ substitutions by values that fit your system. If you - * cannot run the configure script, you can do the value substitution - * by hand. - */ - -#ifndef JANSSON_CONFIG_H -#define JANSSON_CONFIG_H - -/* If your compiler supports the inline keyword in C, JSON_INLINE is - defined to `inline', otherwise empty. In C++, the inline is always - supported. */ -#ifdef __cplusplus -#define JSON_INLINE inline -#else -#define JSON_INLINE inline -#endif - -/* If your compiler supports the `long long` type and the strtoll() - library function, JSON_INTEGER_IS_LONG_LONG is defined to 1, - otherwise to 0. */ -#define JSON_INTEGER_IS_LONG_LONG 1 - -/* If locale.h and localeconv() are available, define to 1, - otherwise to 0. */ -#define JSON_HAVE_LOCALECONV 1 - -/* Maximum recursion depth for parsing JSON input. - This limits the depth of e.g. array-within-array constructions. */ -#define JSON_PARSER_MAX_DEPTH 2048 - -#endif diff --git a/src/3rdparty/jansson/jansson_private.h b/src/3rdparty/jansson/jansson_private.h deleted file mode 100644 index 5ed96158..00000000 --- a/src/3rdparty/jansson/jansson_private.h +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef JANSSON_PRIVATE_H -#define JANSSON_PRIVATE_H - -#include "jansson_private_config.h" -#include -#include "jansson.h" -#include "hashtable.h" -#include "strbuffer.h" - -#define container_of(ptr_, type_, member_) \ - ((type_ *)((char *)ptr_ - offsetof(type_, member_))) - -/* On some platforms, max() may already be defined */ -#ifndef max -#define max(a, b) ((a) > (b) ? (a) : (b)) -#endif - -/* va_copy is a C99 feature. In C89 implementations, it's sometimes - available as __va_copy. If not, memcpy() should do the trick. */ -#ifndef va_copy -#ifdef __va_copy -#define va_copy __va_copy -#else -#define va_copy(a, b) memcpy(&(a), &(b), sizeof(va_list)) -#endif -#endif - -typedef struct { - json_t json; - hashtable_t hashtable; - int visited; -} json_object_t; - -typedef struct { - json_t json; - size_t size; - size_t entries; - json_t **table; - int visited; -} json_array_t; - -typedef struct { - json_t json; - char *value; - size_t length; -} json_string_t; - -typedef struct { - json_t json; - double value; -} json_real_t; - -typedef struct { - json_t json; - json_int_t value; -} json_integer_t; - -#define json_to_object(json_) container_of(json_, json_object_t, json) -#define json_to_array(json_) container_of(json_, json_array_t, json) -#define json_to_string(json_) container_of(json_, json_string_t, json) -#define json_to_real(json_) container_of(json_, json_real_t, json) -#define json_to_integer(json_) container_of(json_, json_integer_t, json) - -/* Create a string by taking ownership of an existing buffer */ -json_t *jsonp_stringn_nocheck_own(const char *value, size_t len); - -/* Error message formatting */ -void jsonp_error_init(json_error_t *error, const char *source); -void jsonp_error_set_source(json_error_t *error, const char *source); -void jsonp_error_set(json_error_t *error, int line, int column, - size_t position, const char *msg, ...); -void jsonp_error_vset(json_error_t *error, int line, int column, - size_t position, const char *msg, va_list ap); - -/* Locale independent string<->double conversions */ -int jsonp_strtod(strbuffer_t *strbuffer, double *out); -int jsonp_dtostr(char *buffer, size_t size, double value, int prec); - -/* Wrappers for custom memory functions */ -void* jsonp_malloc(size_t size); -void jsonp_free(void *ptr); -char *jsonp_strndup(const char *str, size_t length); -char *jsonp_strdup(const char *str); -char *jsonp_strndup(const char *str, size_t len); - - -/* Windows compatibility */ -#if defined(_WIN32) || defined(WIN32) -# if defined(_MSC_VER) /* MS compiller */ -# if (_MSC_VER < 1900) && !defined(snprintf) /* snprintf not defined yet & not introduced */ -# define snprintf _snprintf -# endif -# if (_MSC_VER < 1500) && !defined(vsnprintf) /* vsnprintf not defined yet & not introduced */ -# define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a) -# endif -# else /* Other Windows compiller, old definition */ -# define snprintf _snprintf -# define vsnprintf _vsnprintf -# endif -#endif - -#endif diff --git a/src/3rdparty/jansson/jansson_private_config.h b/src/3rdparty/jansson/jansson_private_config.h deleted file mode 100644 index 671993d9..00000000 --- a/src/3rdparty/jansson/jansson_private_config.h +++ /dev/null @@ -1,173 +0,0 @@ -/* jansson_private_config.h. Generated from jansson_private_config.h.in by configure. */ -/* jansson_private_config.h.in. Generated from configure.ac by autoheader. */ - -/* Define to 1 if gcc's __atomic builtins are available */ -#ifndef _MSC_VER -# define HAVE_ATOMIC_BUILTINS 1 -#endif - -/* Define to 1 if you have the `close' function. */ -#define HAVE_CLOSE 1 - -/* Define to 1 if you have the header file. */ -/* #undef HAVE_DLFCN_H */ - -/* Define to 1 if you have the header file. */ -/* #undef HAVE_ENDIAN_H */ - -/* Define to 1 if you have the header file. */ -#define HAVE_FCNTL_H 1 - -/* Define to 1 if you have the `getpid' function. */ -#define HAVE_GETPID 1 - -/* Define to 1 if you have the `gettimeofday' function. */ -#ifndef _MSC_VER -# define HAVE_GETTIMEOFDAY 1 -#endif - -/* Define to 1 if you have the header file. */ -#define HAVE_INTTYPES_H 1 - -/* Define to 1 if you have the `localeconv' function. */ -#define HAVE_LOCALECONV 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_LOCALE_H 1 - -/* Define to 1 if the system has the type 'long long int'. */ -#define HAVE_LONG_LONG_INT 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_MEMORY_H 1 - -/* Define to 1 if you have the `open' function. */ -#define HAVE_OPEN 1 - -/* Define to 1 if you have the `read' function. */ -#define HAVE_READ 1 - -/* Define to 1 if you have the header file. */ -#ifndef _MSC_VER -# define HAVE_SCHED_H 1 -#endif - -/* Define to 1 if you have the `sched_yield' function. */ -#ifndef _MSC_VER -# define HAVE_SCHED_YIELD 1 -#endif - -/* Define to 1 if you have the header file. */ -#define HAVE_STDINT_H 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_STDLIB_H 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_STRINGS_H 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_STRING_H 1 - -/* Define to 1 if you have the `strtoll' function. */ -#define HAVE_STRTOLL 1 - -/* Define to 1 if gcc's __sync builtins are available */ -#define HAVE_SYNC_BUILTINS 1 - -/* Define to 1 if you have the header file. */ -#ifndef _MSC_VER -# define HAVE_SYS_PARAM_H 1 -#endif - -/* Define to 1 if you have the header file. */ -#define HAVE_SYS_STAT_H 1 - -/* Define to 1 if you have the header file. */ -#ifndef _MSC_VER -# define HAVE_SYS_TIME_H 1 -#endif - -/* Define to 1 if you have the header file. */ -#define HAVE_SYS_TYPES_H 1 - -/* Define to 1 if you have the header file. */ -#ifndef _MSC_VER -# define HAVE_UNISTD_H 1 -#endif - -/* Define to 1 if the system has the type 'unsigned long long int'. */ -#define HAVE_UNSIGNED_LONG_LONG_INT 1 - -/* Number of buckets new object hashtables contain is 2 raised to this power. - E.g. 3 -> 2^3 = 8. */ -#define INITIAL_HASHTABLE_ORDER 3 - -/* Define to the sub-directory where libtool stores uninstalled libraries. */ -#define LT_OBJDIR ".libs/" - -/* Name of package */ -#define PACKAGE "jansson" - -/* Define to the address where bug reports for this package should be sent. */ -#define PACKAGE_BUGREPORT "petri@digip.org" - -/* Define to the full name of this package. */ -#define PACKAGE_NAME "jansson" - -/* Define to the full name and version of this package. */ -#define PACKAGE_STRING "jansson 2.9" - -/* Define to the one symbol short name of this package. */ -#define PACKAGE_TARNAME "jansson" - -/* Define to the home page for this package. */ -#define PACKAGE_URL "" - -/* Define to the version of this package. */ -#define PACKAGE_VERSION "2.9" - -/* Define to 1 if you have the ANSI C header files. */ -#define STDC_HEADERS 1 - -/* Define to 1 if /dev/urandom should be used for seeding the hash function */ -#define USE_URANDOM 1 - -/* Define to 1 if CryptGenRandom should be used for seeding the hash function - */ -#define USE_WINDOWS_CRYPTOAPI 1 - -/* Version number of package */ -#define VERSION "2.9" - -/* Define for Solaris 2.5.1 so the uint32_t typedef from , - , or is not used. If the typedef were allowed, the - #define below would cause a syntax error. */ -/* #undef _UINT32_T */ - -/* Define for Solaris 2.5.1 so the uint8_t typedef from , - , or is not used. If the typedef were allowed, the - #define below would cause a syntax error. */ -/* #undef _UINT8_T */ - -/* Define to `__inline__' or `__inline' if that's what the C compiler - calls it, or to nothing if 'inline' is not supported under any name. */ -#ifndef __cplusplus -/* #undef inline */ -#endif - -/* Define to the type of a signed integer type of width exactly 32 bits if - such a type exists and the standard includes do not define it. */ -/* #undef int32_t */ - -/* Define to the type of an unsigned integer type of width exactly 16 bits if - such a type exists and the standard includes do not define it. */ -/* #undef uint16_t */ - -/* Define to the type of an unsigned integer type of width exactly 32 bits if - such a type exists and the standard includes do not define it. */ -/* #undef uint32_t */ - -/* Define to the type of an unsigned integer type of width exactly 8 bits if - such a type exists and the standard includes do not define it. */ -/* #undef uint8_t */ diff --git a/src/3rdparty/jansson/load.c b/src/3rdparty/jansson/load.c deleted file mode 100644 index d9399696..00000000 --- a/src/3rdparty/jansson/load.c +++ /dev/null @@ -1,1158 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - -#include "jansson_private.h" - -#include -#include -#include -#include -#include -#include - -#if defined(HAVE_UNISTD_H) -# include -#elif defined(_MSC_VER) -# include -# define HAVE_UNISTD_H -# define STDIN_FILENO 0 -#endif - -#include "jansson.h" -#include "strbuffer.h" -#include "utf.h" - -#define STREAM_STATE_OK 0 -#define STREAM_STATE_EOF -1 -#define STREAM_STATE_ERROR -2 - -#define TOKEN_INVALID -1 -#define TOKEN_EOF 0 -#define TOKEN_STRING 256 -#define TOKEN_INTEGER 257 -#define TOKEN_REAL 258 -#define TOKEN_TRUE 259 -#define TOKEN_FALSE 260 -#define TOKEN_NULL 261 - -/* Locale independent versions of isxxx() functions */ -#define l_isupper(c) ('A' <= (c) && (c) <= 'Z') -#define l_islower(c) ('a' <= (c) && (c) <= 'z') -#define l_isalpha(c) (l_isupper(c) || l_islower(c)) -#define l_isdigit(c) ('0' <= (c) && (c) <= '9') -#define l_isxdigit(c) \ - (l_isdigit(c) || ('A' <= (c) && (c) <= 'F') || ('a' <= (c) && (c) <= 'f')) - -/* Read one byte from stream, convert to unsigned char, then int, and - return. return EOF on end of file. This corresponds to the - behaviour of fgetc(). */ -typedef int (*get_func)(void *data); - -typedef struct { - get_func get; - void *data; - char buffer[5]; - size_t buffer_pos; - int state; - int line; - int column, last_column; - size_t position; -} stream_t; - -typedef struct { - stream_t stream; - strbuffer_t saved_text; - size_t flags; - size_t depth; - int token; - union { - struct { - char *val; - size_t len; - } string; - json_int_t integer; - double real; - } value; -} lex_t; - -#define stream_to_lex(stream) container_of(stream, lex_t, stream) - - -/*** error reporting ***/ - -static void error_set(json_error_t *error, const lex_t *lex, - const char *msg, ...) -{ - va_list ap; - char msg_text[JSON_ERROR_TEXT_LENGTH]; - char msg_with_context[JSON_ERROR_TEXT_LENGTH]; - - int line = -1, col = -1; - size_t pos = 0; - const char *result = msg_text; - - if(!error) - return; - - va_start(ap, msg); - vsnprintf(msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap); - msg_text[JSON_ERROR_TEXT_LENGTH - 1] = '\0'; - va_end(ap); - - if(lex) - { - const char *saved_text = strbuffer_value(&lex->saved_text); - - line = lex->stream.line; - col = lex->stream.column; - pos = lex->stream.position; - - if(saved_text && saved_text[0]) - { - if(lex->saved_text.length <= 20) { - snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, - "%s near '%s'", msg_text, saved_text); - msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] = '\0'; - result = msg_with_context; - } - } - else - { - if(lex->stream.state == STREAM_STATE_ERROR) { - /* No context for UTF-8 decoding errors */ - result = msg_text; - } - else { - snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, - "%s near end of file", msg_text); - msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] = '\0'; - result = msg_with_context; - } - } - } - - jsonp_error_set(error, line, col, pos, "%s", result); -} - - -/*** lexical analyzer ***/ - -static void -stream_init(stream_t *stream, get_func get, void *data) -{ - stream->get = get; - stream->data = data; - stream->buffer[0] = '\0'; - stream->buffer_pos = 0; - - stream->state = STREAM_STATE_OK; - stream->line = 1; - stream->column = 0; - stream->position = 0; -} - -static int stream_get(stream_t *stream, json_error_t *error) -{ - int c; - - if(stream->state != STREAM_STATE_OK) - return stream->state; - - if(!stream->buffer[stream->buffer_pos]) - { - c = stream->get(stream->data); - if(c == EOF) { - stream->state = STREAM_STATE_EOF; - return STREAM_STATE_EOF; - } - - stream->buffer[0] = c; - stream->buffer_pos = 0; - - if(0x80 <= c && c <= 0xFF) - { - /* multi-byte UTF-8 sequence */ - size_t i, count; - - count = utf8_check_first(c); - if(!count) - goto out; - - assert(count >= 2); - - for(i = 1; i < count; i++) - stream->buffer[i] = stream->get(stream->data); - - if(!utf8_check_full(stream->buffer, count, NULL)) - goto out; - - stream->buffer[count] = '\0'; - } - else - stream->buffer[1] = '\0'; - } - - c = stream->buffer[stream->buffer_pos++]; - - stream->position++; - if(c == '\n') { - stream->line++; - stream->last_column = stream->column; - stream->column = 0; - } - else if(utf8_check_first(c)) { - /* track the Unicode character column, so increment only if - this is the first character of a UTF-8 sequence */ - stream->column++; - } - - return c; - -out: - stream->state = STREAM_STATE_ERROR; - error_set(error, stream_to_lex(stream), "unable to decode byte 0x%x", c); - return STREAM_STATE_ERROR; -} - -static void stream_unget(stream_t *stream, int c) -{ - if(c == STREAM_STATE_EOF || c == STREAM_STATE_ERROR) - return; - - stream->position--; - if(c == '\n') { - stream->line--; - stream->column = stream->last_column; - } - else if(utf8_check_first(c)) - stream->column--; - - assert(stream->buffer_pos > 0); - stream->buffer_pos--; - assert(stream->buffer[stream->buffer_pos] == c); -} - - -static int lex_get(lex_t *lex, json_error_t *error) -{ - return stream_get(&lex->stream, error); -} - -static void lex_save(lex_t *lex, int c) -{ - strbuffer_append_byte(&lex->saved_text, c); -} - -static int lex_get_save(lex_t *lex, json_error_t *error) -{ - int c = stream_get(&lex->stream, error); - if(c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR) - lex_save(lex, c); - return c; -} - -static void lex_unget(lex_t *lex, int c) -{ - stream_unget(&lex->stream, c); -} - -static void lex_unget_unsave(lex_t *lex, int c) -{ - if(c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR) { - /* Since we treat warnings as errors, when assertions are turned - * off the "d" variable would be set but never used. Which is - * treated as an error by GCC. - */ - #ifndef NDEBUG - char d; - #endif - stream_unget(&lex->stream, c); - #ifndef NDEBUG - d = - #endif - strbuffer_pop(&lex->saved_text); - assert(c == d); - } -} - -static void lex_save_cached(lex_t *lex) -{ - while(lex->stream.buffer[lex->stream.buffer_pos] != '\0') - { - lex_save(lex, lex->stream.buffer[lex->stream.buffer_pos]); - lex->stream.buffer_pos++; - lex->stream.position++; - } -} - -static void lex_free_string(lex_t *lex) -{ - jsonp_free(lex->value.string.val); - lex->value.string.val = NULL; - lex->value.string.len = 0; -} - -/* assumes that str points to 'u' plus at least 4 valid hex digits */ -static int32_t decode_unicode_escape(const char *str) -{ - int i; - int32_t value = 0; - - assert(str[0] == 'u'); - - for(i = 1; i <= 4; i++) { - char c = str[i]; - value <<= 4; - if(l_isdigit(c)) - value += c - '0'; - else if(l_islower(c)) - value += c - 'a' + 10; - else if(l_isupper(c)) - value += c - 'A' + 10; - else - return -1; - } - - return value; -} - -static void lex_scan_string(lex_t *lex, json_error_t *error) -{ - int c; - const char *p; - char *t; - int i; - - lex->value.string.val = NULL; - lex->token = TOKEN_INVALID; - - c = lex_get_save(lex, error); - - while(c != '"') { - if(c == STREAM_STATE_ERROR) - goto out; - - else if(c == STREAM_STATE_EOF) { - error_set(error, lex, "premature end of input"); - goto out; - } - - else if(0 <= c && c <= 0x1F) { - /* control character */ - lex_unget_unsave(lex, c); - if(c == '\n') - error_set(error, lex, "unexpected newline"); - else - error_set(error, lex, "control character 0x%x", c); - goto out; - } - - else if(c == '\\') { - c = lex_get_save(lex, error); - if(c == 'u') { - c = lex_get_save(lex, error); - for(i = 0; i < 4; i++) { - if(!l_isxdigit(c)) { - error_set(error, lex, "invalid escape"); - goto out; - } - c = lex_get_save(lex, error); - } - } - else if(c == '"' || c == '\\' || c == '/' || c == 'b' || - c == 'f' || c == 'n' || c == 'r' || c == 't') - c = lex_get_save(lex, error); - else { - error_set(error, lex, "invalid escape"); - goto out; - } - } - else - c = lex_get_save(lex, error); - } - - /* the actual value is at most of the same length as the source - string, because: - - shortcut escapes (e.g. "\t") (length 2) are converted to 1 byte - - a single \uXXXX escape (length 6) is converted to at most 3 bytes - - two \uXXXX escapes (length 12) forming an UTF-16 surrogate pair - are converted to 4 bytes - */ - t = jsonp_malloc(lex->saved_text.length + 1); - if(!t) { - /* this is not very nice, since TOKEN_INVALID is returned */ - goto out; - } - lex->value.string.val = t; - - /* + 1 to skip the " */ - p = strbuffer_value(&lex->saved_text) + 1; - - while(*p != '"') { - if(*p == '\\') { - p++; - if(*p == 'u') { - size_t length; - int32_t value; - - value = decode_unicode_escape(p); - if(value < 0) { - error_set(error, lex, "invalid Unicode escape '%.6s'", p - 1); - goto out; - } - p += 5; - - if(0xD800 <= value && value <= 0xDBFF) { - /* surrogate pair */ - if(*p == '\\' && *(p + 1) == 'u') { - int32_t value2 = decode_unicode_escape(++p); - if(value2 < 0) { - error_set(error, lex, "invalid Unicode escape '%.6s'", p - 1); - goto out; - } - p += 5; - - if(0xDC00 <= value2 && value2 <= 0xDFFF) { - /* valid second surrogate */ - value = - ((value - 0xD800) << 10) + - (value2 - 0xDC00) + - 0x10000; - } - else { - /* invalid second surrogate */ - error_set(error, lex, - "invalid Unicode '\\u%04X\\u%04X'", - value, value2); - goto out; - } - } - else { - /* no second surrogate */ - error_set(error, lex, "invalid Unicode '\\u%04X'", - value); - goto out; - } - } - else if(0xDC00 <= value && value <= 0xDFFF) { - error_set(error, lex, "invalid Unicode '\\u%04X'", value); - goto out; - } - - if(utf8_encode(value, t, &length)) - assert(0); - t += length; - } - else { - switch(*p) { - case '"': case '\\': case '/': - *t = *p; break; - case 'b': *t = '\b'; break; - case 'f': *t = '\f'; break; - case 'n': *t = '\n'; break; - case 'r': *t = '\r'; break; - case 't': *t = '\t'; break; - default: assert(0); - } - t++; - p++; - } - } - else - *(t++) = *(p++); - } - *t = '\0'; - lex->value.string.len = t - lex->value.string.val; - lex->token = TOKEN_STRING; - return; - -out: - lex_free_string(lex); -} - -#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */ -#if JSON_INTEGER_IS_LONG_LONG -#ifdef _MSC_VER /* Microsoft Visual Studio */ -#define json_strtoint _strtoi64 -#else -#define json_strtoint strtoll -#endif -#else -#define json_strtoint strtol -#endif -#endif - -static int lex_scan_number(lex_t *lex, int c, json_error_t *error) -{ - const char *saved_text; - char *end; - double doubleval; - - lex->token = TOKEN_INVALID; - - if(c == '-') - c = lex_get_save(lex, error); - - if(c == '0') { - c = lex_get_save(lex, error); - if(l_isdigit(c)) { - lex_unget_unsave(lex, c); - goto out; - } - } - else if(l_isdigit(c)) { - do - c = lex_get_save(lex, error); - while(l_isdigit(c)); - } - else { - lex_unget_unsave(lex, c); - goto out; - } - - if(!(lex->flags & JSON_DECODE_INT_AS_REAL) && - c != '.' && c != 'E' && c != 'e') - { - json_int_t intval; - - lex_unget_unsave(lex, c); - - saved_text = strbuffer_value(&lex->saved_text); - - errno = 0; - intval = json_strtoint(saved_text, &end, 10); - if(errno == ERANGE) { - if(intval < 0) - error_set(error, lex, "too big negative integer"); - else - error_set(error, lex, "too big integer"); - goto out; - } - - assert(end == saved_text + lex->saved_text.length); - - lex->token = TOKEN_INTEGER; - lex->value.integer = intval; - return 0; - } - - if(c == '.') { - c = lex_get(lex, error); - if(!l_isdigit(c)) { - lex_unget(lex, c); - goto out; - } - lex_save(lex, c); - - do - c = lex_get_save(lex, error); - while(l_isdigit(c)); - } - - if(c == 'E' || c == 'e') { - c = lex_get_save(lex, error); - if(c == '+' || c == '-') - c = lex_get_save(lex, error); - - if(!l_isdigit(c)) { - lex_unget_unsave(lex, c); - goto out; - } - - do - c = lex_get_save(lex, error); - while(l_isdigit(c)); - } - - lex_unget_unsave(lex, c); - - if(jsonp_strtod(&lex->saved_text, &doubleval)) { - error_set(error, lex, "real number overflow"); - goto out; - } - - lex->token = TOKEN_REAL; - lex->value.real = doubleval; - return 0; - -out: - return -1; -} - -static int lex_scan(lex_t *lex, json_error_t *error) -{ - int c; - - strbuffer_clear(&lex->saved_text); - - if(lex->token == TOKEN_STRING) - lex_free_string(lex); - - do - c = lex_get(lex, error); - while(c == ' ' || c == '\t' || c == '\n' || c == '\r'); - - if(c == STREAM_STATE_EOF) { - lex->token = TOKEN_EOF; - goto out; - } - - if(c == STREAM_STATE_ERROR) { - lex->token = TOKEN_INVALID; - goto out; - } - - lex_save(lex, c); - - if(c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',') - lex->token = c; - - else if(c == '"') - lex_scan_string(lex, error); - - else if(l_isdigit(c) || c == '-') { - if(lex_scan_number(lex, c, error)) - goto out; - } - - else if(l_isalpha(c)) { - /* eat up the whole identifier for clearer error messages */ - const char *saved_text; - - do - c = lex_get_save(lex, error); - while(l_isalpha(c)); - lex_unget_unsave(lex, c); - - saved_text = strbuffer_value(&lex->saved_text); - - if(strcmp(saved_text, "true") == 0) - lex->token = TOKEN_TRUE; - else if(strcmp(saved_text, "false") == 0) - lex->token = TOKEN_FALSE; - else if(strcmp(saved_text, "null") == 0) - lex->token = TOKEN_NULL; - else - lex->token = TOKEN_INVALID; - } - - else { - /* save the rest of the input UTF-8 sequence to get an error - message of valid UTF-8 */ - lex_save_cached(lex); - lex->token = TOKEN_INVALID; - } - -out: - return lex->token; -} - -static char *lex_steal_string(lex_t *lex, size_t *out_len) -{ - char *result = NULL; - if(lex->token == TOKEN_STRING) { - result = lex->value.string.val; - *out_len = lex->value.string.len; - lex->value.string.val = NULL; - lex->value.string.len = 0; - } - return result; -} - -static int lex_init(lex_t *lex, get_func get, size_t flags, void *data) -{ - stream_init(&lex->stream, get, data); - if(strbuffer_init(&lex->saved_text)) - return -1; - - lex->flags = flags; - lex->token = TOKEN_INVALID; - return 0; -} - -static void lex_close(lex_t *lex) -{ - if(lex->token == TOKEN_STRING) - lex_free_string(lex); - strbuffer_close(&lex->saved_text); -} - - -/*** parser ***/ - -static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error); - -static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error) -{ - json_t *object = json_object(); - if(!object) - return NULL; - - lex_scan(lex, error); - if(lex->token == '}') - return object; - - while(1) { - char *key; - size_t len; - json_t *value; - - if(lex->token != TOKEN_STRING) { - error_set(error, lex, "string or '}' expected"); - goto error; - } - - key = lex_steal_string(lex, &len); - if(!key) - return NULL; - if (memchr(key, '\0', len)) { - jsonp_free(key); - error_set(error, lex, "NUL byte in object key not supported"); - goto error; - } - - if(flags & JSON_REJECT_DUPLICATES) { - if(json_object_get(object, key)) { - jsonp_free(key); - error_set(error, lex, "duplicate object key"); - goto error; - } - } - - lex_scan(lex, error); - if(lex->token != ':') { - jsonp_free(key); - error_set(error, lex, "':' expected"); - goto error; - } - - lex_scan(lex, error); - value = parse_value(lex, flags, error); - if(!value) { - jsonp_free(key); - goto error; - } - - if(json_object_set_nocheck(object, key, value)) { - jsonp_free(key); - json_decref(value); - goto error; - } - - json_decref(value); - jsonp_free(key); - - lex_scan(lex, error); - if(lex->token != ',') - break; - - lex_scan(lex, error); - } - - if(lex->token != '}') { - error_set(error, lex, "'}' expected"); - goto error; - } - - return object; - -error: - json_decref(object); - return NULL; -} - -static json_t *parse_array(lex_t *lex, size_t flags, json_error_t *error) -{ - json_t *array = json_array(); - if(!array) - return NULL; - - lex_scan(lex, error); - if(lex->token == ']') - return array; - - while(lex->token) { - json_t *elem = parse_value(lex, flags, error); - if(!elem) - goto error; - - if(json_array_append(array, elem)) { - json_decref(elem); - goto error; - } - json_decref(elem); - - lex_scan(lex, error); - if(lex->token != ',') - break; - - lex_scan(lex, error); - } - - if(lex->token != ']') { - error_set(error, lex, "']' expected"); - goto error; - } - - return array; - -error: - json_decref(array); - return NULL; -} - -static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error) -{ - json_t *json; - - lex->depth++; - if(lex->depth > JSON_PARSER_MAX_DEPTH) { - error_set(error, lex, "maximum parsing depth reached"); - return NULL; - } - - switch(lex->token) { - case TOKEN_STRING: { - const char *value = lex->value.string.val; - size_t len = lex->value.string.len; - - if(!(flags & JSON_ALLOW_NUL)) { - if(memchr(value, '\0', len)) { - error_set(error, lex, "\\u0000 is not allowed without JSON_ALLOW_NUL"); - return NULL; - } - } - - json = jsonp_stringn_nocheck_own(value, len); - if(json) { - lex->value.string.val = NULL; - lex->value.string.len = 0; - } - break; - } - - case TOKEN_INTEGER: { - json = json_integer(lex->value.integer); - break; - } - - case TOKEN_REAL: { - json = json_real(lex->value.real); - break; - } - - case TOKEN_TRUE: - json = json_true(); - break; - - case TOKEN_FALSE: - json = json_false(); - break; - - case TOKEN_NULL: - json = json_null(); - break; - - case '{': - json = parse_object(lex, flags, error); - break; - - case '[': - json = parse_array(lex, flags, error); - break; - - case TOKEN_INVALID: - error_set(error, lex, "invalid token"); - return NULL; - - default: - error_set(error, lex, "unexpected token"); - return NULL; - } - - if(!json) - return NULL; - - lex->depth--; - return json; -} - -static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error) -{ - json_t *result; - - lex->depth = 0; - - lex_scan(lex, error); - if(!(flags & JSON_DECODE_ANY)) { - if(lex->token != '[' && lex->token != '{') { - error_set(error, lex, "'[' or '{' expected"); - return NULL; - } - } - - result = parse_value(lex, flags, error); - if(!result) - return NULL; - - if(!(flags & JSON_DISABLE_EOF_CHECK)) { - lex_scan(lex, error); - if(lex->token != TOKEN_EOF) { - error_set(error, lex, "end of file expected"); - json_decref(result); - return NULL; - } - } - - if(error) { - /* Save the position even though there was no error */ - error->position = (int)lex->stream.position; - } - - return result; -} - -typedef struct -{ - const char *data; - size_t pos; -} string_data_t; - -static int string_get(void *data) -{ - char c; - string_data_t *stream = (string_data_t *)data; - c = stream->data[stream->pos]; - if(c == '\0') - return EOF; - else - { - stream->pos++; - return (unsigned char)c; - } -} - -json_t *json_loads(const char *string, size_t flags, json_error_t *error) -{ - lex_t lex; - json_t *result; - string_data_t stream_data; - - jsonp_error_init(error, ""); - - if (string == NULL) { - error_set(error, NULL, "wrong arguments"); - return NULL; - } - - stream_data.data = string; - stream_data.pos = 0; - - if(lex_init(&lex, string_get, flags, (void *)&stream_data)) - return NULL; - - result = parse_json(&lex, flags, error); - - lex_close(&lex); - return result; -} - -typedef struct -{ - const char *data; - size_t len; - size_t pos; -} buffer_data_t; - -static int buffer_get(void *data) -{ - char c; - buffer_data_t *stream = data; - if(stream->pos >= stream->len) - return EOF; - - c = stream->data[stream->pos]; - stream->pos++; - return (unsigned char)c; -} - -json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error) -{ - lex_t lex; - json_t *result; - buffer_data_t stream_data; - - jsonp_error_init(error, ""); - - if (buffer == NULL) { - error_set(error, NULL, "wrong arguments"); - return NULL; - } - - stream_data.data = buffer; - stream_data.pos = 0; - stream_data.len = buflen; - - if(lex_init(&lex, buffer_get, flags, (void *)&stream_data)) - return NULL; - - result = parse_json(&lex, flags, error); - - lex_close(&lex); - return result; -} - -json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) -{ - lex_t lex; - const char *source; - json_t *result; - - if(input == stdin) - source = ""; - else - source = ""; - - jsonp_error_init(error, source); - - if (input == NULL) { - error_set(error, NULL, "wrong arguments"); - return NULL; - } - - if(lex_init(&lex, (get_func)fgetc, flags, input)) - return NULL; - - result = parse_json(&lex, flags, error); - - lex_close(&lex); - return result; -} - -static int fd_get_func(int *fd) -{ -#ifdef HAVE_UNISTD_H - uint8_t c; - if (read(*fd, &c, 1) == 1) - return c; -#endif - return EOF; -} - -json_t *json_loadfd(int input, size_t flags, json_error_t *error) -{ - lex_t lex; - const char *source; - json_t *result; - -#ifdef HAVE_UNISTD_H - if(input == STDIN_FILENO) - source = ""; - else -#endif - source = ""; - - jsonp_error_init(error, source); - - if (input < 0) { - error_set(error, NULL, "wrong arguments"); - return NULL; - } - - if(lex_init(&lex, (get_func)fd_get_func, flags, &input)) - return NULL; - - result = parse_json(&lex, flags, error); - - lex_close(&lex); - return result; -} - -json_t *json_load_file(const char *path, size_t flags, json_error_t *error) -{ - json_t *result; - FILE *fp; - - jsonp_error_init(error, path); - - if (path == NULL) { - error_set(error, NULL, "wrong arguments"); - return NULL; - } - - fp = fopen(path, "rb"); - if(!fp) - { - error_set(error, NULL, "unable to open %s: %s", - path, strerror(errno)); - return NULL; - } - - result = json_loadf(fp, flags, error); - - fclose(fp); - return result; -} - -#define MAX_BUF_LEN 1024 - -typedef struct -{ - char data[MAX_BUF_LEN]; - size_t len; - size_t pos; - json_load_callback_t callback; - void *arg; -} callback_data_t; - -static int callback_get(void *data) -{ - char c; - callback_data_t *stream = data; - - if(stream->pos >= stream->len) { - stream->pos = 0; - stream->len = stream->callback(stream->data, MAX_BUF_LEN, stream->arg); - if(stream->len == 0 || stream->len == (size_t)-1) - return EOF; - } - - c = stream->data[stream->pos]; - stream->pos++; - return (unsigned char)c; -} - -json_t *json_load_callback(json_load_callback_t callback, void *arg, size_t flags, json_error_t *error) -{ - lex_t lex; - json_t *result; - - callback_data_t stream_data; - - memset(&stream_data, 0, sizeof(stream_data)); - stream_data.callback = callback; - stream_data.arg = arg; - - jsonp_error_init(error, ""); - - if (callback == NULL) { - error_set(error, NULL, "wrong arguments"); - return NULL; - } - - if(lex_init(&lex, (get_func)callback_get, flags, &stream_data)) - return NULL; - - result = parse_json(&lex, flags, error); - - lex_close(&lex); - return result; -} diff --git a/src/3rdparty/jansson/lookup3.h b/src/3rdparty/jansson/lookup3.h deleted file mode 100644 index 522a41ae..00000000 --- a/src/3rdparty/jansson/lookup3.h +++ /dev/null @@ -1,381 +0,0 @@ -/* -------------------------------------------------------------------------------- -lookup3.c, by Bob Jenkins, May 2006, Public Domain. - -These are functions for producing 32-bit hashes for hash table lookup. -hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() -are externally useful functions. Routines to test the hash are included -if SELF_TEST is defined. You can use this free for any purpose. It's in -the public domain. It has no warranty. - -You probably want to use hashlittle(). hashlittle() and hashbig() -hash byte arrays. hashlittle() is is faster than hashbig() on -little-endian machines. Intel and AMD are little-endian machines. -On second thought, you probably want hashlittle2(), which is identical to -hashlittle() except it returns two 32-bit hashes for the price of one. -You could implement hashbig2() if you wanted but I haven't bothered here. - -If you want to find a hash of, say, exactly 7 integers, do - a = i1; b = i2; c = i3; - mix(a,b,c); - a += i4; b += i5; c += i6; - mix(a,b,c); - a += i7; - final(a,b,c); -then use c as the hash value. If you have a variable length array of -4-byte integers to hash, use hashword(). If you have a byte array (like -a character string), use hashlittle(). If you have several byte arrays, or -a mix of things, see the comments above hashlittle(). - -Why is this so big? I read 12 bytes at a time into 3 4-byte integers, -then mix those integers. This is fast (you can do a lot more thorough -mixing with 12*3 instructions on 3 integers than you can with 3 instructions -on 1 byte), but shoehorning those bytes into integers efficiently is messy. -------------------------------------------------------------------------------- -*/ - -#include - -#ifdef HAVE_CONFIG_H -#include -#endif - -#ifdef HAVE_STDINT_H -#include /* defines uint32_t etc */ -#endif - -#ifdef HAVE_SYS_PARAM_H -#include /* attempt to define endianness */ -#endif - -#ifdef HAVE_ENDIAN_H -# include /* attempt to define endianness */ -#endif - -/* - * My best guess at if you are big-endian or little-endian. This may - * need adjustment. - */ -#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ - __BYTE_ORDER == __LITTLE_ENDIAN) || \ - (defined(i386) || defined(__i386__) || defined(__i486__) || \ - defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL)) -# define HASH_LITTLE_ENDIAN 1 -# define HASH_BIG_ENDIAN 0 -#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \ - __BYTE_ORDER == __BIG_ENDIAN) || \ - (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel)) -# define HASH_LITTLE_ENDIAN 0 -# define HASH_BIG_ENDIAN 1 -#else -# define HASH_LITTLE_ENDIAN 0 -# define HASH_BIG_ENDIAN 0 -#endif - -#define hashsize(n) ((uint32_t)1<<(n)) -#define hashmask(n) (hashsize(n)-1) -#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) - -/* -------------------------------------------------------------------------------- -mix -- mix 3 32-bit values reversibly. - -This is reversible, so any information in (a,b,c) before mix() is -still in (a,b,c) after mix(). - -If four pairs of (a,b,c) inputs are run through mix(), or through -mix() in reverse, there are at least 32 bits of the output that -are sometimes the same for one pair and different for another pair. -This was tested for: -* pairs that differed by one bit, by two bits, in any combination - of top bits of (a,b,c), or in any combination of bottom bits of - (a,b,c). -* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed - the output delta to a Gray code (a^(a>>1)) so a string of 1's (as - is commonly produced by subtraction) look like a single 1-bit - difference. -* the base values were pseudorandom, all zero but one bit set, or - all zero plus a counter that starts at zero. - -Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that -satisfy this are - 4 6 8 16 19 4 - 9 15 3 18 27 15 - 14 9 3 7 17 3 -Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing -for "differ" defined as + with a one-bit base and a two-bit delta. I -used http://burtleburtle.net/bob/hash/avalanche.html to choose -the operations, constants, and arrangements of the variables. - -This does not achieve avalanche. There are input bits of (a,b,c) -that fail to affect some output bits of (a,b,c), especially of a. The -most thoroughly mixed value is c, but it doesn't really even achieve -avalanche in c. - -This allows some parallelism. Read-after-writes are good at doubling -the number of bits affected, so the goal of mixing pulls in the opposite -direction as the goal of parallelism. I did what I could. Rotates -seem to cost as much as shifts on every machine I could lay my hands -on, and rotates are much kinder to the top and bottom bits, so I used -rotates. -------------------------------------------------------------------------------- -*/ -#define mix(a,b,c) \ -{ \ - a -= c; a ^= rot(c, 4); c += b; \ - b -= a; b ^= rot(a, 6); a += c; \ - c -= b; c ^= rot(b, 8); b += a; \ - a -= c; a ^= rot(c,16); c += b; \ - b -= a; b ^= rot(a,19); a += c; \ - c -= b; c ^= rot(b, 4); b += a; \ -} - -/* -------------------------------------------------------------------------------- -final -- final mixing of 3 32-bit values (a,b,c) into c - -Pairs of (a,b,c) values differing in only a few bits will usually -produce values of c that look totally different. This was tested for -* pairs that differed by one bit, by two bits, in any combination - of top bits of (a,b,c), or in any combination of bottom bits of - (a,b,c). -* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed - the output delta to a Gray code (a^(a>>1)) so a string of 1's (as - is commonly produced by subtraction) look like a single 1-bit - difference. -* the base values were pseudorandom, all zero but one bit set, or - all zero plus a counter that starts at zero. - -These constants passed: - 14 11 25 16 4 14 24 - 12 14 25 16 4 14 24 -and these came close: - 4 8 15 26 3 22 24 - 10 8 15 26 3 22 24 - 11 8 15 26 3 22 24 -------------------------------------------------------------------------------- -*/ -#define final(a,b,c) \ -{ \ - c ^= b; c -= rot(b,14); \ - a ^= c; a -= rot(c,11); \ - b ^= a; b -= rot(a,25); \ - c ^= b; c -= rot(b,16); \ - a ^= c; a -= rot(c,4); \ - b ^= a; b -= rot(a,14); \ - c ^= b; c -= rot(b,24); \ -} - -/* -------------------------------------------------------------------------------- -hashlittle() -- hash a variable-length key into a 32-bit value - k : the key (the unaligned variable-length array of bytes) - length : the length of the key, counting by bytes - initval : can be any 4-byte value -Returns a 32-bit value. Every bit of the key affects every bit of -the return value. Two keys differing by one or two bits will have -totally different hash values. - -The best hash table sizes are powers of 2. There is no need to do -mod a prime (mod is sooo slow!). If you need less than 32 bits, -use a bitmask. For example, if you need only 10 bits, do - h = (h & hashmask(10)); -In which case, the hash table should have hashsize(10) elements. - -If you are hashing n strings (uint8_t **)k, do it like this: - for (i=0, h=0; i 12) - { - a += k[0]; - b += k[1]; - c += k[2]; - mix(a,b,c); - length -= 12; - k += 3; - } - - /*----------------------------- handle the last (probably partial) block */ - /* - * "k[2]&0xffffff" actually reads beyond the end of the string, but - * then masks off the part it's not allowed to read. Because the - * string is aligned, the masked-off tail is in the same word as the - * rest of the string. Every machine with memory protection I've seen - * does it on word boundaries, so is OK with this. But VALGRIND will - * still catch it and complain. The masking trick does make the hash - * noticably faster for short strings (like English words). - */ -#ifndef NO_MASKING_TRICK - - switch(length) - { - case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; - case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; - case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; - case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; - case 8 : b+=k[1]; a+=k[0]; break; - case 7 : b+=k[1]&0xffffff; a+=k[0]; break; - case 6 : b+=k[1]&0xffff; a+=k[0]; break; - case 5 : b+=k[1]&0xff; a+=k[0]; break; - case 4 : a+=k[0]; break; - case 3 : a+=k[0]&0xffffff; break; - case 2 : a+=k[0]&0xffff; break; - case 1 : a+=k[0]&0xff; break; - case 0 : return c; /* zero length strings require no mixing */ - } - -#else /* make valgrind happy */ - - k8 = (const uint8_t *)k; - switch(length) - { - case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; - case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ - case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ - case 9 : c+=k8[8]; /* fall through */ - case 8 : b+=k[1]; a+=k[0]; break; - case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ - case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ - case 5 : b+=k8[4]; /* fall through */ - case 4 : a+=k[0]; break; - case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ - case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ - case 1 : a+=k8[0]; break; - case 0 : return c; - } - -#endif /* !valgrind */ - - } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { - const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ - const uint8_t *k8; - - /*--------------- all but last block: aligned reads and different mixing */ - while (length > 12) - { - a += k[0] + (((uint32_t)k[1])<<16); - b += k[2] + (((uint32_t)k[3])<<16); - c += k[4] + (((uint32_t)k[5])<<16); - mix(a,b,c); - length -= 12; - k += 6; - } - - /*----------------------------- handle the last (probably partial) block */ - k8 = (const uint8_t *)k; - switch(length) - { - case 12: c+=k[4]+(((uint32_t)k[5])<<16); - b+=k[2]+(((uint32_t)k[3])<<16); - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ - case 10: c+=k[4]; - b+=k[2]+(((uint32_t)k[3])<<16); - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 9 : c+=k8[8]; /* fall through */ - case 8 : b+=k[2]+(((uint32_t)k[3])<<16); - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ - case 6 : b+=k[2]; - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 5 : b+=k8[4]; /* fall through */ - case 4 : a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ - case 2 : a+=k[0]; - break; - case 1 : a+=k8[0]; - break; - case 0 : return c; /* zero length requires no mixing */ - } - - } else { /* need to read the key one byte at a time */ - const uint8_t *k = (const uint8_t *)key; - - /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ - while (length > 12) - { - a += k[0]; - a += ((uint32_t)k[1])<<8; - a += ((uint32_t)k[2])<<16; - a += ((uint32_t)k[3])<<24; - b += k[4]; - b += ((uint32_t)k[5])<<8; - b += ((uint32_t)k[6])<<16; - b += ((uint32_t)k[7])<<24; - c += k[8]; - c += ((uint32_t)k[9])<<8; - c += ((uint32_t)k[10])<<16; - c += ((uint32_t)k[11])<<24; - mix(a,b,c); - length -= 12; - k += 12; - } - - /*-------------------------------- last block: affect all 32 bits of (c) */ - switch(length) /* all the case statements fall through */ - { - case 12: c+=((uint32_t)k[11])<<24; - case 11: c+=((uint32_t)k[10])<<16; - case 10: c+=((uint32_t)k[9])<<8; - case 9 : c+=k[8]; - case 8 : b+=((uint32_t)k[7])<<24; - case 7 : b+=((uint32_t)k[6])<<16; - case 6 : b+=((uint32_t)k[5])<<8; - case 5 : b+=k[4]; - case 4 : a+=((uint32_t)k[3])<<24; - case 3 : a+=((uint32_t)k[2])<<16; - case 2 : a+=((uint32_t)k[1])<<8; - case 1 : a+=k[0]; - break; - case 0 : return c; - } - } - - final(a,b,c); - return c; -} diff --git a/src/3rdparty/jansson/memory.c b/src/3rdparty/jansson/memory.c deleted file mode 100644 index a2be5d23..00000000 --- a/src/3rdparty/jansson/memory.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * Copyright (c) 2011-2012 Basile Starynkevitch - * - * Jansson is free software; you can redistribute it and/or modify it - * under the terms of the MIT license. See LICENSE for details. - */ - -#include -#include - -#include "jansson.h" -#include "jansson_private.h" - -/* C89 allows these to be macros */ -#undef malloc -#undef free - -/* memory function pointers */ -static json_malloc_t do_malloc = malloc; -static json_free_t do_free = free; - -void *jsonp_malloc(size_t size) -{ - if(!size) - return NULL; - - return (*do_malloc)(size); -} - -void jsonp_free(void *ptr) -{ - if(!ptr) - return; - - (*do_free)(ptr); -} - -char *jsonp_strdup(const char *str) -{ - return jsonp_strndup(str, strlen(str)); -} - -char *jsonp_strndup(const char *str, size_t len) -{ - char *new_str; - - new_str = jsonp_malloc(len + 1); - if(!new_str) - return NULL; - - memcpy(new_str, str, len); - new_str[len] = '\0'; - return new_str; -} - -void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) -{ - do_malloc = malloc_fn; - do_free = free_fn; -} - -void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn) -{ - if (malloc_fn) - *malloc_fn = do_malloc; - if (free_fn) - *free_fn = do_free; -} diff --git a/src/3rdparty/jansson/pack_unpack.c b/src/3rdparty/jansson/pack_unpack.c deleted file mode 100644 index 2a2da353..00000000 --- a/src/3rdparty/jansson/pack_unpack.c +++ /dev/null @@ -1,871 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * Copyright (c) 2011-2012 Graeme Smecher - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#include -#include "jansson.h" -#include "jansson_private.h" -#include "utf.h" - -typedef struct { - int line; - int column; - size_t pos; - char token; -} token_t; - -typedef struct { - const char *start; - const char *fmt; - token_t prev_token; - token_t token; - token_t next_token; - json_error_t *error; - size_t flags; - int line; - int column; - size_t pos; -} scanner_t; - -#define token(scanner) ((scanner)->token.token) - -static const char * const type_names[] = { - "object", - "array", - "string", - "integer", - "real", - "true", - "false", - "null" -}; - -#define type_name(x) type_names[json_typeof(x)] - -static const char unpack_value_starters[] = "{[siIbfFOon"; - -static void scanner_init(scanner_t *s, json_error_t *error, - size_t flags, const char *fmt) -{ - s->error = error; - s->flags = flags; - s->fmt = s->start = fmt; - memset(&s->prev_token, 0, sizeof(token_t)); - memset(&s->token, 0, sizeof(token_t)); - memset(&s->next_token, 0, sizeof(token_t)); - s->line = 1; - s->column = 0; - s->pos = 0; -} - -static void next_token(scanner_t *s) -{ - const char *t; - s->prev_token = s->token; - - if(s->next_token.line) { - s->token = s->next_token; - s->next_token.line = 0; - return; - } - - t = s->fmt; - s->column++; - s->pos++; - - /* skip space and ignored chars */ - while(*t == ' ' || *t == '\t' || *t == '\n' || *t == ',' || *t == ':') { - if(*t == '\n') { - s->line++; - s->column = 1; - } - else - s->column++; - - s->pos++; - t++; - } - - s->token.token = *t; - s->token.line = s->line; - s->token.column = s->column; - s->token.pos = s->pos; - - t++; - s->fmt = t; -} - -static void prev_token(scanner_t *s) -{ - s->next_token = s->token; - s->token = s->prev_token; -} - -static void set_error(scanner_t *s, const char *source, const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - - jsonp_error_vset(s->error, s->token.line, s->token.column, s->token.pos, - fmt, ap); - - jsonp_error_set_source(s->error, source); - - va_end(ap); -} - -static json_t *pack(scanner_t *s, va_list *ap); - - -/* ours will be set to 1 if jsonp_free() must be called for the result - afterwards */ -static char *read_string(scanner_t *s, va_list *ap, - const char *purpose, size_t *out_len, int *ours) -{ - char t; - strbuffer_t strbuff; - const char *str; - size_t length; - - next_token(s); - t = token(s); - prev_token(s); - - if(t != '#' && t != '%' && t != '+') { - /* Optimize the simple case */ - str = va_arg(*ap, const char *); - - if(!str) { - set_error(s, "", "NULL string argument"); - return NULL; - } - - length = strlen(str); - - if(!utf8_check_string(str, length)) { - set_error(s, "", "Invalid UTF-8 %s", purpose); - return NULL; - } - - *out_len = length; - *ours = 0; - return (char *)str; - } - - strbuffer_init(&strbuff); - - while(1) { - str = va_arg(*ap, const char *); - if(!str) { - set_error(s, "", "NULL string argument"); - strbuffer_close(&strbuff); - return NULL; - } - - next_token(s); - - if(token(s) == '#') { - length = va_arg(*ap, int); - } - else if(token(s) == '%') { - length = va_arg(*ap, size_t); - } - else { - prev_token(s); - length = strlen(str); - } - - if(strbuffer_append_bytes(&strbuff, str, length) == -1) { - set_error(s, "", "Out of memory"); - strbuffer_close(&strbuff); - return NULL; - } - - next_token(s); - if(token(s) != '+') { - prev_token(s); - break; - } - } - - if(!utf8_check_string(strbuff.value, strbuff.length)) { - set_error(s, "", "Invalid UTF-8 %s", purpose); - strbuffer_close(&strbuff); - return NULL; - } - - *out_len = strbuff.length; - *ours = 1; - return strbuffer_steal_value(&strbuff); -} - -static json_t *pack_object(scanner_t *s, va_list *ap) -{ - json_t *object = json_object(); - next_token(s); - - while(token(s) != '}') { - char *key; - size_t len; - int ours; - json_t *value; - - if(!token(s)) { - set_error(s, "", "Unexpected end of format string"); - goto error; - } - - if(token(s) != 's') { - set_error(s, "", "Expected format 's', got '%c'", token(s)); - goto error; - } - - key = read_string(s, ap, "object key", &len, &ours); - if(!key) - goto error; - - next_token(s); - - value = pack(s, ap); - if(!value) { - if(ours) - jsonp_free(key); - - goto error; - } - - if(json_object_set_new_nocheck(object, key, value)) { - set_error(s, "", "Unable to add key \"%s\"", key); - if(ours) - jsonp_free(key); - - goto error; - } - - if(ours) - jsonp_free(key); - - next_token(s); - } - - return object; - -error: - json_decref(object); - return NULL; -} - -static json_t *pack_array(scanner_t *s, va_list *ap) -{ - json_t *array = json_array(); - next_token(s); - - while(token(s) != ']') { - json_t *value; - - if(!token(s)) { - set_error(s, "", "Unexpected end of format string"); - goto error; - } - - value = pack(s, ap); - if(!value) - goto error; - - if(json_array_append_new(array, value)) { - set_error(s, "", "Unable to append to array"); - goto error; - } - - next_token(s); - } - return array; - -error: - json_decref(array); - return NULL; -} - -static json_t *pack_string(scanner_t *s, va_list *ap) -{ - char *str; - size_t len; - int ours; - int nullable; - - next_token(s); - nullable = token(s) == '?'; - if (!nullable) - prev_token(s); - - str = read_string(s, ap, "string", &len, &ours); - if (!str) { - return nullable ? json_null() : NULL; - } else if (ours) { - return jsonp_stringn_nocheck_own(str, len); - } else { - return json_stringn_nocheck(str, len); - } -} - -static json_t *pack(scanner_t *s, va_list *ap) -{ - switch(token(s)) { - case '{': - return pack_object(s, ap); - - case '[': - return pack_array(s, ap); - - case 's': /* string */ - return pack_string(s, ap); - - case 'n': /* null */ - return json_null(); - - case 'b': /* boolean */ - return va_arg(*ap, int) ? json_true() : json_false(); - - case 'i': /* integer from int */ - return json_integer(va_arg(*ap, int)); - - case 'I': /* integer from json_int_t */ - return json_integer(va_arg(*ap, json_int_t)); - - case 'f': /* real */ - return json_real(va_arg(*ap, double)); - - case 'O': /* a json_t object; increments refcount */ - { - int nullable; - json_t *json; - - next_token(s); - nullable = token(s) == '?'; - if (!nullable) - prev_token(s); - - json = va_arg(*ap, json_t *); - if (!json && nullable) { - return json_null(); - } else { - return json_incref(json); - } - } - - case 'o': /* a json_t object; doesn't increment refcount */ - { - int nullable; - json_t *json; - - next_token(s); - nullable = token(s) == '?'; - if (!nullable) - prev_token(s); - - json = va_arg(*ap, json_t *); - if (!json && nullable) { - return json_null(); - } else { - return json; - } - } - - default: - set_error(s, "", "Unexpected format character '%c'", - token(s)); - return NULL; - } -} - -static int unpack(scanner_t *s, json_t *root, va_list *ap); - -static int unpack_object(scanner_t *s, json_t *root, va_list *ap) -{ - int ret = -1; - int strict = 0; - int gotopt = 0; - - /* Use a set (emulated by a hashtable) to check that all object - keys are accessed. Checking that the correct number of keys - were accessed is not enough, as the same key can be unpacked - multiple times. - */ - hashtable_t key_set; - - if(hashtable_init(&key_set)) { - set_error(s, "", "Out of memory"); - return -1; - } - - if(root && !json_is_object(root)) { - set_error(s, "", "Expected object, got %s", - type_name(root)); - goto out; - } - next_token(s); - - while(token(s) != '}') { - const char *key; - json_t *value; - int opt = 0; - - if(strict != 0) { - set_error(s, "", "Expected '}' after '%c', got '%c'", - (strict == 1 ? '!' : '*'), token(s)); - goto out; - } - - if(!token(s)) { - set_error(s, "", "Unexpected end of format string"); - goto out; - } - - if(token(s) == '!' || token(s) == '*') { - strict = (token(s) == '!' ? 1 : -1); - next_token(s); - continue; - } - - if(token(s) != 's') { - set_error(s, "", "Expected format 's', got '%c'", token(s)); - goto out; - } - - key = va_arg(*ap, const char *); - if(!key) { - set_error(s, "", "NULL object key"); - goto out; - } - - next_token(s); - - if(token(s) == '?') { - opt = gotopt = 1; - next_token(s); - } - - if(!root) { - /* skipping */ - value = NULL; - } - else { - value = json_object_get(root, key); - if(!value && !opt) { - set_error(s, "", "Object item not found: %s", key); - goto out; - } - } - - if(unpack(s, value, ap)) - goto out; - - hashtable_set(&key_set, key, json_null()); - next_token(s); - } - - if(strict == 0 && (s->flags & JSON_STRICT)) - strict = 1; - - if(root && strict == 1) { - /* We need to check that all non optional items have been parsed */ - const char *key; - int have_unrecognized_keys = 0; - strbuffer_t unrecognized_keys; - json_t *value; - long unpacked = 0; - if (gotopt) { - /* We have optional keys, we need to iter on each key */ - json_object_foreach(root, key, value) { - if(!hashtable_get(&key_set, key)) { - unpacked++; - - /* Save unrecognized keys for the error message */ - if (!have_unrecognized_keys) { - strbuffer_init(&unrecognized_keys); - have_unrecognized_keys = 1; - } else { - strbuffer_append_bytes(&unrecognized_keys, ", ", 2); - } - strbuffer_append_bytes(&unrecognized_keys, key, strlen(key)); - } - } - } else { - /* No optional keys, we can just compare the number of items */ - unpacked = (long)json_object_size(root) - (long)key_set.size; - } - if (unpacked) { - if (!gotopt) { - /* Save unrecognized keys for the error message */ - json_object_foreach(root, key, value) { - if(!hashtable_get(&key_set, key)) { - if (!have_unrecognized_keys) { - strbuffer_init(&unrecognized_keys); - have_unrecognized_keys = 1; - } else { - strbuffer_append_bytes(&unrecognized_keys, ", ", 2); - } - strbuffer_append_bytes(&unrecognized_keys, key, strlen(key)); - } - } - } - set_error(s, "", - "%li object item(s) left unpacked: %s", - unpacked, strbuffer_value(&unrecognized_keys)); - strbuffer_close(&unrecognized_keys); - goto out; - } - } - - ret = 0; - -out: - hashtable_close(&key_set); - return ret; -} - -static int unpack_array(scanner_t *s, json_t *root, va_list *ap) -{ - size_t i = 0; - int strict = 0; - - if(root && !json_is_array(root)) { - set_error(s, "", "Expected array, got %s", type_name(root)); - return -1; - } - next_token(s); - - while(token(s) != ']') { - json_t *value; - - if(strict != 0) { - set_error(s, "", "Expected ']' after '%c', got '%c'", - (strict == 1 ? '!' : '*'), - token(s)); - return -1; - } - - if(!token(s)) { - set_error(s, "", "Unexpected end of format string"); - return -1; - } - - if(token(s) == '!' || token(s) == '*') { - strict = (token(s) == '!' ? 1 : -1); - next_token(s); - continue; - } - - if(!strchr(unpack_value_starters, token(s))) { - set_error(s, "", "Unexpected format character '%c'", - token(s)); - return -1; - } - - if(!root) { - /* skipping */ - value = NULL; - } - else { - value = json_array_get(root, i); - if(!value) { - set_error(s, "", "Array index %lu out of range", - (unsigned long)i); - return -1; - } - } - - if(unpack(s, value, ap)) - return -1; - - next_token(s); - i++; - } - - if(strict == 0 && (s->flags & JSON_STRICT)) - strict = 1; - - if(root && strict == 1 && i != json_array_size(root)) { - long diff = (long)json_array_size(root) - (long)i; - set_error(s, "", "%li array item(s) left unpacked", diff); - return -1; - } - - return 0; -} - -static int unpack(scanner_t *s, json_t *root, va_list *ap) -{ - switch(token(s)) - { - case '{': - return unpack_object(s, root, ap); - - case '[': - return unpack_array(s, root, ap); - - case 's': - if(root && !json_is_string(root)) { - set_error(s, "", "Expected string, got %s", - type_name(root)); - return -1; - } - - if(!(s->flags & JSON_VALIDATE_ONLY)) { - const char **str_target; - size_t *len_target = NULL; - - str_target = va_arg(*ap, const char **); - if(!str_target) { - set_error(s, "", "NULL string argument"); - return -1; - } - - next_token(s); - - if(token(s) == '%') { - len_target = va_arg(*ap, size_t *); - if(!len_target) { - set_error(s, "", "NULL string length argument"); - return -1; - } - } - else - prev_token(s); - - if(root) { - *str_target = json_string_value(root); - if(len_target) - *len_target = json_string_length(root); - } - } - return 0; - - case 'i': - if(root && !json_is_integer(root)) { - set_error(s, "", "Expected integer, got %s", - type_name(root)); - return -1; - } - - if(!(s->flags & JSON_VALIDATE_ONLY)) { - int *target = va_arg(*ap, int*); - if(root) - *target = (int)json_integer_value(root); - } - - return 0; - - case 'I': - if(root && !json_is_integer(root)) { - set_error(s, "", "Expected integer, got %s", - type_name(root)); - return -1; - } - - if(!(s->flags & JSON_VALIDATE_ONLY)) { - json_int_t *target = va_arg(*ap, json_int_t*); - if(root) - *target = json_integer_value(root); - } - - return 0; - - case 'b': - if(root && !json_is_boolean(root)) { - set_error(s, "", "Expected true or false, got %s", - type_name(root)); - return -1; - } - - if(!(s->flags & JSON_VALIDATE_ONLY)) { - int *target = va_arg(*ap, int*); - if(root) - *target = json_is_true(root); - } - - return 0; - - case 'f': - if(root && !json_is_real(root)) { - set_error(s, "", "Expected real, got %s", - type_name(root)); - return -1; - } - - if(!(s->flags & JSON_VALIDATE_ONLY)) { - double *target = va_arg(*ap, double*); - if(root) - *target = json_real_value(root); - } - - return 0; - - case 'F': - if(root && !json_is_number(root)) { - set_error(s, "", "Expected real or integer, got %s", - type_name(root)); - return -1; - } - - if(!(s->flags & JSON_VALIDATE_ONLY)) { - double *target = va_arg(*ap, double*); - if(root) - *target = json_number_value(root); - } - - return 0; - - case 'O': - if(root && !(s->flags & JSON_VALIDATE_ONLY)) - json_incref(root); - /* Fall through */ - - case 'o': - if(!(s->flags & JSON_VALIDATE_ONLY)) { - json_t **target = va_arg(*ap, json_t**); - if(root) - *target = root; - } - - return 0; - - case 'n': - /* Never assign, just validate */ - if(root && !json_is_null(root)) { - set_error(s, "", "Expected null, got %s", - type_name(root)); - return -1; - } - return 0; - - default: - set_error(s, "", "Unexpected format character '%c'", - token(s)); - return -1; - } -} - -json_t *json_vpack_ex(json_error_t *error, size_t flags, - const char *fmt, va_list ap) -{ - scanner_t s; - va_list ap_copy; - json_t *value; - - if(!fmt || !*fmt) { - jsonp_error_init(error, ""); - jsonp_error_set(error, -1, -1, 0, "NULL or empty format string"); - return NULL; - } - jsonp_error_init(error, NULL); - - scanner_init(&s, error, flags, fmt); - next_token(&s); - - va_copy(ap_copy, ap); - value = pack(&s, &ap_copy); - va_end(ap_copy); - - if(!value) - return NULL; - - next_token(&s); - if(token(&s)) { - json_decref(value); - set_error(&s, "", "Garbage after format string"); - return NULL; - } - - return value; -} - -json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...) -{ - json_t *value; - va_list ap; - - va_start(ap, fmt); - value = json_vpack_ex(error, flags, fmt, ap); - va_end(ap); - - return value; -} - -json_t *json_pack(const char *fmt, ...) -{ - json_t *value; - va_list ap; - - va_start(ap, fmt); - value = json_vpack_ex(NULL, 0, fmt, ap); - va_end(ap); - - return value; -} - -int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, - const char *fmt, va_list ap) -{ - scanner_t s; - va_list ap_copy; - - if(!root) { - jsonp_error_init(error, ""); - jsonp_error_set(error, -1, -1, 0, "NULL root value"); - return -1; - } - - if(!fmt || !*fmt) { - jsonp_error_init(error, ""); - jsonp_error_set(error, -1, -1, 0, "NULL or empty format string"); - return -1; - } - jsonp_error_init(error, NULL); - - scanner_init(&s, error, flags, fmt); - next_token(&s); - - va_copy(ap_copy, ap); - if(unpack(&s, root, &ap_copy)) { - va_end(ap_copy); - return -1; - } - va_end(ap_copy); - - next_token(&s); - if(token(&s)) { - set_error(&s, "", "Garbage after format string"); - return -1; - } - - return 0; -} - -int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...) -{ - int ret; - va_list ap; - - va_start(ap, fmt); - ret = json_vunpack_ex(root, error, flags, fmt, ap); - va_end(ap); - - return ret; -} - -int json_unpack(json_t *root, const char *fmt, ...) -{ - int ret; - va_list ap; - - va_start(ap, fmt); - ret = json_vunpack_ex(root, NULL, 0, fmt, ap); - va_end(ap); - - return ret; -} diff --git a/src/3rdparty/jansson/strbuffer.c b/src/3rdparty/jansson/strbuffer.c deleted file mode 100644 index 5e8c0039..00000000 --- a/src/3rdparty/jansson/strbuffer.c +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - -#include -#include -#include "jansson_private.h" -#include "strbuffer.h" - -#define STRBUFFER_MIN_SIZE 16 -#define STRBUFFER_FACTOR 2 -#define STRBUFFER_SIZE_MAX ((size_t)-1) - -int strbuffer_init(strbuffer_t *strbuff) -{ - strbuff->size = STRBUFFER_MIN_SIZE; - strbuff->length = 0; - - strbuff->value = jsonp_malloc(strbuff->size); - if(!strbuff->value) - return -1; - - /* initialize to empty */ - strbuff->value[0] = '\0'; - return 0; -} - -void strbuffer_close(strbuffer_t *strbuff) -{ - if(strbuff->value) - jsonp_free(strbuff->value); - - strbuff->size = 0; - strbuff->length = 0; - strbuff->value = NULL; -} - -void strbuffer_clear(strbuffer_t *strbuff) -{ - strbuff->length = 0; - strbuff->value[0] = '\0'; -} - -const char *strbuffer_value(const strbuffer_t *strbuff) -{ - return strbuff->value; -} - -char *strbuffer_steal_value(strbuffer_t *strbuff) -{ - char *result = strbuff->value; - strbuff->value = NULL; - return result; -} - -int strbuffer_append_byte(strbuffer_t *strbuff, char byte) -{ - return strbuffer_append_bytes(strbuff, &byte, 1); -} - -int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size) -{ - if(size >= strbuff->size - strbuff->length) - { - size_t new_size; - char *new_value; - - /* avoid integer overflow */ - if (strbuff->size > STRBUFFER_SIZE_MAX / STRBUFFER_FACTOR - || size > STRBUFFER_SIZE_MAX - 1 - || strbuff->length > STRBUFFER_SIZE_MAX - 1 - size) - return -1; - - new_size = max(strbuff->size * STRBUFFER_FACTOR, - strbuff->length + size + 1); - - new_value = jsonp_malloc(new_size); - if(!new_value) - return -1; - - memcpy(new_value, strbuff->value, strbuff->length); - - jsonp_free(strbuff->value); - strbuff->value = new_value; - strbuff->size = new_size; - } - - memcpy(strbuff->value + strbuff->length, data, size); - strbuff->length += size; - strbuff->value[strbuff->length] = '\0'; - - return 0; -} - -char strbuffer_pop(strbuffer_t *strbuff) -{ - if(strbuff->length > 0) { - char c = strbuff->value[--strbuff->length]; - strbuff->value[strbuff->length] = '\0'; - return c; - } - else - return '\0'; -} diff --git a/src/3rdparty/jansson/strbuffer.h b/src/3rdparty/jansson/strbuffer.h deleted file mode 100644 index 615b7f5f..00000000 --- a/src/3rdparty/jansson/strbuffer.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef STRBUFFER_H -#define STRBUFFER_H - -#include - -typedef struct { - char *value; - size_t length; /* bytes used */ - size_t size; /* bytes allocated */ -} strbuffer_t; - -int strbuffer_init(strbuffer_t *strbuff); -void strbuffer_close(strbuffer_t *strbuff); - -void strbuffer_clear(strbuffer_t *strbuff); - -const char *strbuffer_value(const strbuffer_t *strbuff); - -/* Steal the value and close the strbuffer */ -char *strbuffer_steal_value(strbuffer_t *strbuff); - -int strbuffer_append_byte(strbuffer_t *strbuff, char byte); -int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size); - -char strbuffer_pop(strbuffer_t *strbuff); - -#endif diff --git a/src/3rdparty/jansson/strconv.c b/src/3rdparty/jansson/strconv.c deleted file mode 100644 index 8075481e..00000000 --- a/src/3rdparty/jansson/strconv.c +++ /dev/null @@ -1,145 +0,0 @@ -#include -#include -#include -#include -#include -#ifdef __MINGW32__ -#undef __NO_ISOCEXT /* ensure stdlib.h will declare prototypes for mingw own 'strtod' replacement, called '__strtod' */ -#endif -#include "jansson_private.h" -#include "strbuffer.h" - -/* need jansson_private_config.h to get the correct snprintf */ -#ifdef HAVE_CONFIG_H -#include -#endif - -#ifdef __MINGW32__ -#define strtod __strtod -#endif - -#if JSON_HAVE_LOCALECONV -#include - -/* - - This code assumes that the decimal separator is exactly one - character. - - - If setlocale() is called by another thread between the call to - localeconv() and the call to sprintf() or strtod(), the result may - be wrong. setlocale() is not thread-safe and should not be used - this way. Multi-threaded programs should use uselocale() instead. -*/ - -static void to_locale(strbuffer_t *strbuffer) -{ - const char *point; - char *pos; - - point = localeconv()->decimal_point; - if(*point == '.') { - /* No conversion needed */ - return; - } - - pos = strchr(strbuffer->value, '.'); - if(pos) - *pos = *point; -} - -static void from_locale(char *buffer) -{ - const char *point; - char *pos; - - point = localeconv()->decimal_point; - if(*point == '.') { - /* No conversion needed */ - return; - } - - pos = strchr(buffer, *point); - if(pos) - *pos = '.'; -} -#endif - -int jsonp_strtod(strbuffer_t *strbuffer, double *out) -{ - double value; - char *end; - -#if JSON_HAVE_LOCALECONV - to_locale(strbuffer); -#endif - - errno = 0; - value = strtod(strbuffer->value, &end); - assert(end == strbuffer->value + strbuffer->length); - - if((value == HUGE_VAL || value == -HUGE_VAL) && errno == ERANGE) { - /* Overflow */ - return -1; - } - - *out = value; - return 0; -} - -int jsonp_dtostr(char *buffer, size_t size, double value, int precision) -{ - int ret; - char *start, *end; - size_t length; - - if (precision == 0) - precision = 17; - - ret = snprintf(buffer, size, "%.*g", precision, value); - if(ret < 0) - return -1; - - length = (size_t)ret; - if(length >= size) - return -1; - -#if JSON_HAVE_LOCALECONV - from_locale(buffer); -#endif - - /* Make sure there's a dot or 'e' in the output. Otherwise - a real is converted to an integer when decoding */ - if(strchr(buffer, '.') == NULL && - strchr(buffer, 'e') == NULL) - { - if(length + 3 >= size) { - /* No space to append ".0" */ - return -1; - } - buffer[length] = '.'; - buffer[length + 1] = '0'; - buffer[length + 2] = '\0'; - length += 2; - } - - /* Remove leading '+' from positive exponent. Also remove leading - zeros from exponents (added by some printf() implementations) */ - start = strchr(buffer, 'e'); - if(start) { - start++; - end = start + 1; - - if(*start == '-') - start++; - - while(*end == '0') - end++; - - if(end != start) { - memmove(start, end, length - (size_t)(end - buffer)); - length -= (size_t)(end - start); - } - } - - return (int)length; -} diff --git a/src/3rdparty/jansson/utf.c b/src/3rdparty/jansson/utf.c deleted file mode 100644 index be966cbd..00000000 --- a/src/3rdparty/jansson/utf.c +++ /dev/null @@ -1,187 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#include -#include "utf.h" - -int utf8_encode(int32_t codepoint, char *buffer, size_t *size) -{ - if(codepoint < 0) - return -1; - else if(codepoint < 0x80) - { - buffer[0] = (char)codepoint; - *size = 1; - } - else if(codepoint < 0x800) - { - buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6); - buffer[1] = 0x80 + ((codepoint & 0x03F)); - *size = 2; - } - else if(codepoint < 0x10000) - { - buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12); - buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6); - buffer[2] = 0x80 + ((codepoint & 0x003F)); - *size = 3; - } - else if(codepoint <= 0x10FFFF) - { - buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18); - buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12); - buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6); - buffer[3] = 0x80 + ((codepoint & 0x00003F)); - *size = 4; - } - else - return -1; - - return 0; -} - -size_t utf8_check_first(char byte) -{ - unsigned char u = (unsigned char)byte; - - if(u < 0x80) - return 1; - - if(0x80 <= u && u <= 0xBF) { - /* second, third or fourth byte of a multi-byte - sequence, i.e. a "continuation byte" */ - return 0; - } - else if(u == 0xC0 || u == 0xC1) { - /* overlong encoding of an ASCII byte */ - return 0; - } - else if(0xC2 <= u && u <= 0xDF) { - /* 2-byte sequence */ - return 2; - } - - else if(0xE0 <= u && u <= 0xEF) { - /* 3-byte sequence */ - return 3; - } - else if(0xF0 <= u && u <= 0xF4) { - /* 4-byte sequence */ - return 4; - } - else { /* u >= 0xF5 */ - /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid - UTF-8 */ - return 0; - } -} - -size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint) -{ - size_t i; - int32_t value = 0; - unsigned char u = (unsigned char)buffer[0]; - - if(size == 2) - { - value = u & 0x1F; - } - else if(size == 3) - { - value = u & 0xF; - } - else if(size == 4) - { - value = u & 0x7; - } - else - return 0; - - for(i = 1; i < size; i++) - { - u = (unsigned char)buffer[i]; - - if(u < 0x80 || u > 0xBF) { - /* not a continuation byte */ - return 0; - } - - value = (value << 6) + (u & 0x3F); - } - - if(value > 0x10FFFF) { - /* not in Unicode range */ - return 0; - } - - else if(0xD800 <= value && value <= 0xDFFF) { - /* invalid code point (UTF-16 surrogate halves) */ - return 0; - } - - else if((size == 2 && value < 0x80) || - (size == 3 && value < 0x800) || - (size == 4 && value < 0x10000)) { - /* overlong encoding */ - return 0; - } - - if(codepoint) - *codepoint = value; - - return 1; -} - -const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint) -{ - size_t count; - int32_t value; - - if(!bufsize) - return buffer; - - count = utf8_check_first(buffer[0]); - if(count <= 0) - return NULL; - - if(count == 1) - value = (unsigned char)buffer[0]; - else - { - if(count > bufsize || !utf8_check_full(buffer, count, &value)) - return NULL; - } - - if(codepoint) - *codepoint = value; - - return buffer + count; -} - -int utf8_check_string(const char *string, size_t length) -{ - size_t i; - - for(i = 0; i < length; i++) - { - size_t count = utf8_check_first(string[i]); - if(count == 0) - return 0; - else if(count > 1) - { - if(count > length - i) - return 0; - - if(!utf8_check_full(&string[i], count, NULL)) - return 0; - - i += count - 1; - } - } - - return 1; -} diff --git a/src/3rdparty/jansson/utf.h b/src/3rdparty/jansson/utf.h deleted file mode 100644 index e182df78..00000000 --- a/src/3rdparty/jansson/utf.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef UTF_H -#define UTF_H - -#ifdef HAVE_CONFIG_H -#include -#endif - -#ifdef HAVE_STDINT_H -#include -#endif - -int utf8_encode(int32_t codepoint, char *buffer, size_t *size); - -size_t utf8_check_first(char byte); -size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint); -const char *utf8_iterate(const char *buffer, size_t size, int32_t *codepoint); - -int utf8_check_string(const char *string, size_t length); - -#endif diff --git a/src/3rdparty/jansson/value.c b/src/3rdparty/jansson/value.c deleted file mode 100644 index 82e60a5f..00000000 --- a/src/3rdparty/jansson/value.c +++ /dev/null @@ -1,1045 +0,0 @@ -/* - * Copyright (c) 2009-2016 Petri Lehtinen - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include - -#ifdef HAVE_STDINT_H -#include -#endif - -#include "jansson.h" -#include "hashtable.h" -#include "jansson_private.h" -#include "utf.h" - -/* Work around nonstandard isnan() and isinf() implementations */ -#ifndef isnan -#ifndef __sun -static JSON_INLINE int isnan(double x) { return x != x; } -#endif -#endif -#ifndef isinf -static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); } -#endif - -static JSON_INLINE void json_init(json_t *json, json_type type) -{ - json->type = type; - json->refcount = 1; -} - - -/*** object ***/ - -extern volatile uint32_t hashtable_seed; - -json_t *json_object(void) -{ - json_object_t *object = jsonp_malloc(sizeof(json_object_t)); - if(!object) - return NULL; - - if (!hashtable_seed) { - /* Autoseed */ - json_object_seed(0); - } - - json_init(&object->json, JSON_OBJECT); - - if(hashtable_init(&object->hashtable)) - { - jsonp_free(object); - return NULL; - } - - object->visited = 0; - - return &object->json; -} - -static void json_delete_object(json_object_t *object) -{ - hashtable_close(&object->hashtable); - jsonp_free(object); -} - -size_t json_object_size(const json_t *json) -{ - json_object_t *object; - - if(!json_is_object(json)) - return 0; - - object = json_to_object(json); - return object->hashtable.size; -} - -json_t *json_object_get(const json_t *json, const char *key) -{ - json_object_t *object; - - if(!key || !json_is_object(json)) - return NULL; - - object = json_to_object(json); - return hashtable_get(&object->hashtable, key); -} - -int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) -{ - json_object_t *object; - - if(!value) - return -1; - - if(!key || !json_is_object(json) || json == value) - { - json_decref(value); - return -1; - } - object = json_to_object(json); - - if(hashtable_set(&object->hashtable, key, value)) - { - json_decref(value); - return -1; - } - - return 0; -} - -int json_object_set_new(json_t *json, const char *key, json_t *value) -{ - if(!key || !utf8_check_string(key, strlen(key))) - { - json_decref(value); - return -1; - } - - return json_object_set_new_nocheck(json, key, value); -} - -int json_object_del(json_t *json, const char *key) -{ - json_object_t *object; - - if(!key || !json_is_object(json)) - return -1; - - object = json_to_object(json); - return hashtable_del(&object->hashtable, key); -} - -int json_object_clear(json_t *json) -{ - json_object_t *object; - - if(!json_is_object(json)) - return -1; - - object = json_to_object(json); - hashtable_clear(&object->hashtable); - - return 0; -} - -int json_object_update(json_t *object, json_t *other) -{ - const char *key; - json_t *value; - - if(!json_is_object(object) || !json_is_object(other)) - return -1; - - json_object_foreach(other, key, value) { - if(json_object_set_nocheck(object, key, value)) - return -1; - } - - return 0; -} - -int json_object_update_existing(json_t *object, json_t *other) -{ - const char *key; - json_t *value; - - if(!json_is_object(object) || !json_is_object(other)) - return -1; - - json_object_foreach(other, key, value) { - if(json_object_get(object, key)) - json_object_set_nocheck(object, key, value); - } - - return 0; -} - -int json_object_update_missing(json_t *object, json_t *other) -{ - const char *key; - json_t *value; - - if(!json_is_object(object) || !json_is_object(other)) - return -1; - - json_object_foreach(other, key, value) { - if(!json_object_get(object, key)) - json_object_set_nocheck(object, key, value); - } - - return 0; -} - -void *json_object_iter(json_t *json) -{ - json_object_t *object; - - if(!json_is_object(json)) - return NULL; - - object = json_to_object(json); - return hashtable_iter(&object->hashtable); -} - -void *json_object_iter_at(json_t *json, const char *key) -{ - json_object_t *object; - - if(!key || !json_is_object(json)) - return NULL; - - object = json_to_object(json); - return hashtable_iter_at(&object->hashtable, key); -} - -void *json_object_iter_next(json_t *json, void *iter) -{ - json_object_t *object; - - if(!json_is_object(json) || iter == NULL) - return NULL; - - object = json_to_object(json); - return hashtable_iter_next(&object->hashtable, iter); -} - -const char *json_object_iter_key(void *iter) -{ - if(!iter) - return NULL; - - return hashtable_iter_key(iter); -} - -json_t *json_object_iter_value(void *iter) -{ - if(!iter) - return NULL; - - return (json_t *)hashtable_iter_value(iter); -} - -int json_object_iter_set_new(json_t *json, void *iter, json_t *value) -{ - if(!json_is_object(json) || !iter || !value) - return -1; - - hashtable_iter_set(iter, value); - return 0; -} - -void *json_object_key_to_iter(const char *key) -{ - if(!key) - return NULL; - - return hashtable_key_to_iter(key); -} - -static int json_object_equal(json_t *object1, json_t *object2) -{ - const char *key; - json_t *value1, *value2; - - if(json_object_size(object1) != json_object_size(object2)) - return 0; - - json_object_foreach(object1, key, value1) { - value2 = json_object_get(object2, key); - - if(!json_equal(value1, value2)) - return 0; - } - - return 1; -} - -static json_t *json_object_copy(json_t *object) -{ - json_t *result; - - const char *key; - json_t *value; - - result = json_object(); - if(!result) - return NULL; - - json_object_foreach(object, key, value) - json_object_set_nocheck(result, key, value); - - return result; -} - -static json_t *json_object_deep_copy(const json_t *object) -{ - json_t *result; - void *iter; - - result = json_object(); - if(!result) - return NULL; - - /* Cannot use json_object_foreach because object has to be cast - non-const */ - iter = json_object_iter((json_t *)object); - while(iter) { - const char *key; - const json_t *value; - key = json_object_iter_key(iter); - value = json_object_iter_value(iter); - - json_object_set_new_nocheck(result, key, json_deep_copy(value)); - iter = json_object_iter_next((json_t *)object, iter); - } - - return result; -} - - -/*** array ***/ - -json_t *json_array(void) -{ - json_array_t *array = jsonp_malloc(sizeof(json_array_t)); - if(!array) - return NULL; - json_init(&array->json, JSON_ARRAY); - - array->entries = 0; - array->size = 8; - - array->table = jsonp_malloc(array->size * sizeof(json_t *)); - if(!array->table) { - jsonp_free(array); - return NULL; - } - - array->visited = 0; - - return &array->json; -} - -static void json_delete_array(json_array_t *array) -{ - size_t i; - - for(i = 0; i < array->entries; i++) - json_decref(array->table[i]); - - jsonp_free(array->table); - jsonp_free(array); -} - -size_t json_array_size(const json_t *json) -{ - if(!json_is_array(json)) - return 0; - - return json_to_array(json)->entries; -} - -json_t *json_array_get(const json_t *json, size_t index) -{ - json_array_t *array; - if(!json_is_array(json)) - return NULL; - array = json_to_array(json); - - if(index >= array->entries) - return NULL; - - return array->table[index]; -} - -int json_array_set_new(json_t *json, size_t index, json_t *value) -{ - json_array_t *array; - - if(!value) - return -1; - - if(!json_is_array(json) || json == value) - { - json_decref(value); - return -1; - } - array = json_to_array(json); - - if(index >= array->entries) - { - json_decref(value); - return -1; - } - - json_decref(array->table[index]); - array->table[index] = value; - - return 0; -} - -static void array_move(json_array_t *array, size_t dest, - size_t src, size_t count) -{ - memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *)); -} - -static void array_copy(json_t **dest, size_t dpos, - json_t **src, size_t spos, - size_t count) -{ - memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *)); -} - -static json_t **json_array_grow(json_array_t *array, - size_t amount, - int copy) -{ - size_t new_size; - json_t **old_table, **new_table; - - if(array->entries + amount <= array->size) - return array->table; - - old_table = array->table; - - new_size = max(array->size + amount, array->size * 2); - new_table = jsonp_malloc(new_size * sizeof(json_t *)); - if(!new_table) - return NULL; - - array->size = new_size; - array->table = new_table; - - if(copy) { - array_copy(array->table, 0, old_table, 0, array->entries); - jsonp_free(old_table); - return array->table; - } - - return old_table; -} - -int json_array_append_new(json_t *json, json_t *value) -{ - json_array_t *array; - - if(!value) - return -1; - - if(!json_is_array(json) || json == value) - { - json_decref(value); - return -1; - } - array = json_to_array(json); - - if(!json_array_grow(array, 1, 1)) { - json_decref(value); - return -1; - } - - array->table[array->entries] = value; - array->entries++; - - return 0; -} - -int json_array_insert_new(json_t *json, size_t index, json_t *value) -{ - json_array_t *array; - json_t **old_table; - - if(!value) - return -1; - - if(!json_is_array(json) || json == value) { - json_decref(value); - return -1; - } - array = json_to_array(json); - - if(index > array->entries) { - json_decref(value); - return -1; - } - - old_table = json_array_grow(array, 1, 0); - if(!old_table) { - json_decref(value); - return -1; - } - - if(old_table != array->table) { - array_copy(array->table, 0, old_table, 0, index); - array_copy(array->table, index + 1, old_table, index, - array->entries - index); - jsonp_free(old_table); - } - else - array_move(array, index + 1, index, array->entries - index); - - array->table[index] = value; - array->entries++; - - return 0; -} - -int json_array_remove(json_t *json, size_t index) -{ - json_array_t *array; - - if(!json_is_array(json)) - return -1; - array = json_to_array(json); - - if(index >= array->entries) - return -1; - - json_decref(array->table[index]); - - /* If we're removing the last element, nothing has to be moved */ - if(index < array->entries - 1) - array_move(array, index, index + 1, array->entries - index - 1); - - array->entries--; - - return 0; -} - -int json_array_clear(json_t *json) -{ - json_array_t *array; - size_t i; - - if(!json_is_array(json)) - return -1; - array = json_to_array(json); - - for(i = 0; i < array->entries; i++) - json_decref(array->table[i]); - - array->entries = 0; - return 0; -} - -int json_array_extend(json_t *json, json_t *other_json) -{ - json_array_t *array, *other; - size_t i; - - if(!json_is_array(json) || !json_is_array(other_json)) - return -1; - array = json_to_array(json); - other = json_to_array(other_json); - - if(!json_array_grow(array, other->entries, 1)) - return -1; - - for(i = 0; i < other->entries; i++) - json_incref(other->table[i]); - - array_copy(array->table, array->entries, other->table, 0, other->entries); - - array->entries += other->entries; - return 0; -} - -static int json_array_equal(json_t *array1, json_t *array2) -{ - size_t i, size; - - size = json_array_size(array1); - if(size != json_array_size(array2)) - return 0; - - for(i = 0; i < size; i++) - { - json_t *value1, *value2; - - value1 = json_array_get(array1, i); - value2 = json_array_get(array2, i); - - if(!json_equal(value1, value2)) - return 0; - } - - return 1; -} - -static json_t *json_array_copy(json_t *array) -{ - json_t *result; - size_t i; - - result = json_array(); - if(!result) - return NULL; - - for(i = 0; i < json_array_size(array); i++) - json_array_append(result, json_array_get(array, i)); - - return result; -} - -static json_t *json_array_deep_copy(const json_t *array) -{ - json_t *result; - size_t i; - - result = json_array(); - if(!result) - return NULL; - - for(i = 0; i < json_array_size(array); i++) - json_array_append_new(result, json_deep_copy(json_array_get(array, i))); - - return result; -} - -/*** string ***/ - -static json_t *string_create(const char *value, size_t len, int own) -{ - char *v; - json_string_t *string; - - if(!value) - return NULL; - - if(own) - v = (char *)value; - else { - v = jsonp_strndup(value, len); - if(!v) - return NULL; - } - - string = jsonp_malloc(sizeof(json_string_t)); - if(!string) { - if(!own) - jsonp_free(v); - return NULL; - } - json_init(&string->json, JSON_STRING); - string->value = v; - string->length = len; - - return &string->json; -} - -json_t *json_string_nocheck(const char *value) -{ - if(!value) - return NULL; - - return string_create(value, strlen(value), 0); -} - -json_t *json_stringn_nocheck(const char *value, size_t len) -{ - return string_create(value, len, 0); -} - -/* this is private; "steal" is not a public API concept */ -json_t *jsonp_stringn_nocheck_own(const char *value, size_t len) -{ - return string_create(value, len, 1); -} - -json_t *json_string(const char *value) -{ - if(!value) - return NULL; - - return json_stringn(value, strlen(value)); -} - -json_t *json_stringn(const char *value, size_t len) -{ - if(!value || !utf8_check_string(value, len)) - return NULL; - - return json_stringn_nocheck(value, len); -} - -const char *json_string_value(const json_t *json) -{ - if(!json_is_string(json)) - return NULL; - - return json_to_string(json)->value; -} - -size_t json_string_length(const json_t *json) -{ - if(!json_is_string(json)) - return 0; - - return json_to_string(json)->length; -} - -int json_string_set_nocheck(json_t *json, const char *value) -{ - if(!value) - return -1; - - return json_string_setn_nocheck(json, value, strlen(value)); -} - -int json_string_setn_nocheck(json_t *json, const char *value, size_t len) -{ - char *dup; - json_string_t *string; - - if(!json_is_string(json) || !value) - return -1; - - dup = jsonp_strndup(value, len); - if(!dup) - return -1; - - string = json_to_string(json); - jsonp_free(string->value); - string->value = dup; - string->length = len; - - return 0; -} - -int json_string_set(json_t *json, const char *value) -{ - if(!value) - return -1; - - return json_string_setn(json, value, strlen(value)); -} - -int json_string_setn(json_t *json, const char *value, size_t len) -{ - if(!value || !utf8_check_string(value, len)) - return -1; - - return json_string_setn_nocheck(json, value, len); -} - -static void json_delete_string(json_string_t *string) -{ - jsonp_free(string->value); - jsonp_free(string); -} - -static int json_string_equal(json_t *string1, json_t *string2) -{ - json_string_t *s1, *s2; - - if(!json_is_string(string1) || !json_is_string(string2)) - return 0; - - s1 = json_to_string(string1); - s2 = json_to_string(string2); - return s1->length == s2->length && !memcmp(s1->value, s2->value, s1->length); -} - -static json_t *json_string_copy(const json_t *string) -{ - json_string_t *s; - - if(!json_is_string(string)) - return NULL; - - s = json_to_string(string); - return json_stringn_nocheck(s->value, s->length); -} - - -/*** integer ***/ - -json_t *json_integer(json_int_t value) -{ - json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t)); - if(!integer) - return NULL; - json_init(&integer->json, JSON_INTEGER); - - integer->value = value; - return &integer->json; -} - -json_int_t json_integer_value(const json_t *json) -{ - if(!json_is_integer(json)) - return 0; - - return json_to_integer(json)->value; -} - -int json_integer_set(json_t *json, json_int_t value) -{ - if(!json_is_integer(json)) - return -1; - - json_to_integer(json)->value = value; - - return 0; -} - -static void json_delete_integer(json_integer_t *integer) -{ - jsonp_free(integer); -} - -static int json_integer_equal(json_t *integer1, json_t *integer2) -{ - return json_integer_value(integer1) == json_integer_value(integer2); -} - -static json_t *json_integer_copy(const json_t *integer) -{ - return json_integer(json_integer_value(integer)); -} - - -/*** real ***/ - -json_t *json_real(double value) -{ - json_real_t *real; - - if(isnan(value) || isinf(value)) - return NULL; - - real = jsonp_malloc(sizeof(json_real_t)); - if(!real) - return NULL; - json_init(&real->json, JSON_REAL); - - real->value = value; - return &real->json; -} - -double json_real_value(const json_t *json) -{ - if(!json_is_real(json)) - return 0; - - return json_to_real(json)->value; -} - -int json_real_set(json_t *json, double value) -{ - if(!json_is_real(json) || isnan(value) || isinf(value)) - return -1; - - json_to_real(json)->value = value; - - return 0; -} - -static void json_delete_real(json_real_t *real) -{ - jsonp_free(real); -} - -static int json_real_equal(json_t *real1, json_t *real2) -{ - return json_real_value(real1) == json_real_value(real2); -} - -static json_t *json_real_copy(const json_t *real) -{ - return json_real(json_real_value(real)); -} - - -/*** number ***/ - -double json_number_value(const json_t *json) -{ - if(json_is_integer(json)) - return (double)json_integer_value(json); - else if(json_is_real(json)) - return json_real_value(json); - else - return 0.0; -} - - -/*** simple values ***/ - -json_t *json_true(void) -{ - static json_t the_true = {JSON_TRUE, (size_t)-1}; - return &the_true; -} - - -json_t *json_false(void) -{ - static json_t the_false = {JSON_FALSE, (size_t)-1}; - return &the_false; -} - - -json_t *json_null(void) -{ - static json_t the_null = {JSON_NULL, (size_t)-1}; - return &the_null; -} - - -/*** deletion ***/ - -void json_delete(json_t *json) -{ - if (!json) - return; - - switch(json_typeof(json)) { - case JSON_OBJECT: - json_delete_object(json_to_object(json)); - break; - case JSON_ARRAY: - json_delete_array(json_to_array(json)); - break; - case JSON_STRING: - json_delete_string(json_to_string(json)); - break; - case JSON_INTEGER: - json_delete_integer(json_to_integer(json)); - break; - case JSON_REAL: - json_delete_real(json_to_real(json)); - break; - default: - return; - } - - /* json_delete is not called for true, false or null */ -} - - -/*** equality ***/ - -int json_equal(json_t *json1, json_t *json2) -{ - if(!json1 || !json2) - return 0; - - if(json_typeof(json1) != json_typeof(json2)) - return 0; - - /* this covers true, false and null as they are singletons */ - if(json1 == json2) - return 1; - - switch(json_typeof(json1)) { - case JSON_OBJECT: - return json_object_equal(json1, json2); - case JSON_ARRAY: - return json_array_equal(json1, json2); - case JSON_STRING: - return json_string_equal(json1, json2); - case JSON_INTEGER: - return json_integer_equal(json1, json2); - case JSON_REAL: - return json_real_equal(json1, json2); - default: - return 0; - } -} - - -/*** copying ***/ - -json_t *json_copy(json_t *json) -{ - if(!json) - return NULL; - - switch(json_typeof(json)) { - case JSON_OBJECT: - return json_object_copy(json); - case JSON_ARRAY: - return json_array_copy(json); - case JSON_STRING: - return json_string_copy(json); - case JSON_INTEGER: - return json_integer_copy(json); - case JSON_REAL: - return json_real_copy(json); - case JSON_TRUE: - case JSON_FALSE: - case JSON_NULL: - return json; - default: - return NULL; - } - - return NULL; -} - -json_t *json_deep_copy(const json_t *json) -{ - if(!json) - return NULL; - - switch(json_typeof(json)) { - case JSON_OBJECT: - return json_object_deep_copy(json); - case JSON_ARRAY: - return json_array_deep_copy(json); - /* for the rest of the types, deep copying doesn't differ from - shallow copying */ - case JSON_STRING: - return json_string_copy(json); - case JSON_INTEGER: - return json_integer_copy(json); - case JSON_REAL: - return json_real_copy(json); - case JSON_TRUE: - case JSON_FALSE: - case JSON_NULL: - return (json_t *)json; - default: - return NULL; - } - - return NULL; -} diff --git a/src/3rdparty/rapidjson/allocators.h b/src/3rdparty/rapidjson/allocators.h new file mode 100644 index 00000000..98affe03 --- /dev/null +++ b/src/3rdparty/rapidjson/allocators.h @@ -0,0 +1,271 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ALLOCATORS_H_ +#define RAPIDJSON_ALLOCATORS_H_ + +#include "rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Allocator + +/*! \class rapidjson::Allocator + \brief Concept for allocating, resizing and freeing memory block. + + Note that Malloc() and Realloc() are non-static but Free() is static. + + So if an allocator need to support Free(), it needs to put its pointer in + the header of memory block. + +\code +concept Allocator { + static const bool kNeedFree; //!< Whether this allocator needs to call Free(). + + // Allocate a memory block. + // \param size of the memory block in bytes. + // \returns pointer to the memory block. + void* Malloc(size_t size); + + // Resize a memory block. + // \param originalPtr The pointer to current memory block. Null pointer is permitted. + // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.) + // \param newSize the new size in bytes. + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); + + // Free a memory block. + // \param pointer to the memory block. Null pointer is permitted. + static void Free(void *ptr); +}; +\endcode +*/ + +/////////////////////////////////////////////////////////////////////////////// +// CrtAllocator + +//! C-runtime library allocator. +/*! This class is just wrapper for standard C library memory routines. + \note implements Allocator concept +*/ +class CrtAllocator { +public: + static const bool kNeedFree = true; + void* Malloc(size_t size) { + if (size) // behavior of malloc(0) is implementation defined. + return std::malloc(size); + else + return NULL; // standardize to returning NULL. + } + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + (void)originalSize; + if (newSize == 0) { + std::free(originalPtr); + return NULL; + } + return std::realloc(originalPtr, newSize); + } + static void Free(void *ptr) { std::free(ptr); } +}; + +/////////////////////////////////////////////////////////////////////////////// +// MemoryPoolAllocator + +//! Default memory allocator used by the parser and DOM. +/*! This allocator allocate memory blocks from pre-allocated memory chunks. + + It does not free memory blocks. And Realloc() only allocate new memory. + + The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. + + User may also supply a buffer as the first chunk. + + If the user-buffer is full then additional chunks are allocated by BaseAllocator. + + The user-buffer is not deallocated by this allocator. + + \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. + \note implements Allocator concept +*/ +template +class MemoryPoolAllocator { +public: + static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) + + //! Constructor with chunkSize. + /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. + \param baseAllocator The allocator for allocating memory chunks. + */ + MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : + chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) + { + } + + //! Constructor with user-supplied buffer. + /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. + + The user buffer will not be deallocated when this allocator is destructed. + + \param buffer User supplied buffer. + \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). + \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. + \param baseAllocator The allocator for allocating memory chunks. + */ + MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : + chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0) + { + RAPIDJSON_ASSERT(buffer != 0); + RAPIDJSON_ASSERT(size > sizeof(ChunkHeader)); + chunkHead_ = reinterpret_cast(buffer); + chunkHead_->capacity = size - sizeof(ChunkHeader); + chunkHead_->size = 0; + chunkHead_->next = 0; + } + + //! Destructor. + /*! This deallocates all memory chunks, excluding the user-supplied buffer. + */ + ~MemoryPoolAllocator() { + Clear(); + RAPIDJSON_DELETE(ownBaseAllocator_); + } + + //! Deallocates all memory chunks, excluding the user-supplied buffer. + void Clear() { + while (chunkHead_ && chunkHead_ != userBuffer_) { + ChunkHeader* next = chunkHead_->next; + baseAllocator_->Free(chunkHead_); + chunkHead_ = next; + } + if (chunkHead_ && chunkHead_ == userBuffer_) + chunkHead_->size = 0; // Clear user buffer + } + + //! Computes the total capacity of allocated memory chunks. + /*! \return total capacity in bytes. + */ + size_t Capacity() const { + size_t capacity = 0; + for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) + capacity += c->capacity; + return capacity; + } + + //! Computes the memory blocks allocated. + /*! \return total used bytes. + */ + size_t Size() const { + size_t size = 0; + for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) + size += c->size; + return size; + } + + //! Allocates a memory block. (concept Allocator) + void* Malloc(size_t size) { + if (!size) + return NULL; + + size = RAPIDJSON_ALIGN(size); + if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) + if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size)) + return NULL; + + void *buffer = reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size; + chunkHead_->size += size; + return buffer; + } + + //! Resizes a memory block (concept Allocator) + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + if (originalPtr == 0) + return Malloc(newSize); + + if (newSize == 0) + return NULL; + + originalSize = RAPIDJSON_ALIGN(originalSize); + newSize = RAPIDJSON_ALIGN(newSize); + + // Do not shrink if new size is smaller than original + if (originalSize >= newSize) + return originalPtr; + + // Simply expand it if it is the last allocation and there is sufficient space + if (originalPtr == reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) { + size_t increment = static_cast(newSize - originalSize); + if (chunkHead_->size + increment <= chunkHead_->capacity) { + chunkHead_->size += increment; + return originalPtr; + } + } + + // Realloc process: allocate and copy memory, do not free original buffer. + if (void* newBuffer = Malloc(newSize)) { + if (originalSize) + std::memcpy(newBuffer, originalPtr, originalSize); + return newBuffer; + } + else + return NULL; + } + + //! Frees a memory block (concept Allocator) + static void Free(void *ptr) { (void)ptr; } // Do nothing + +private: + //! Copy constructor is not permitted. + MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; + //! Copy assignment operator is not permitted. + MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; + + //! Creates a new chunk. + /*! \param capacity Capacity of the chunk in bytes. + \return true if success. + */ + bool AddChunk(size_t capacity) { + if (!baseAllocator_) + ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); + if (ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) { + chunk->capacity = capacity; + chunk->size = 0; + chunk->next = chunkHead_; + chunkHead_ = chunk; + return true; + } + else + return false; + } + + static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. + + //! Chunk header for perpending to each chunk. + /*! Chunks are stored as a singly linked list. + */ + struct ChunkHeader { + size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). + size_t size; //!< Current size of allocated memory in bytes. + ChunkHeader *next; //!< Next chunk in the linked list. + }; + + ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation. + size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. + void *userBuffer_; //!< User supplied buffer. + BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. + BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object. +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ENCODINGS_H_ diff --git a/src/3rdparty/rapidjson/document.h b/src/3rdparty/rapidjson/document.h new file mode 100644 index 00000000..e3e20dfb --- /dev/null +++ b/src/3rdparty/rapidjson/document.h @@ -0,0 +1,2575 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_DOCUMENT_H_ +#define RAPIDJSON_DOCUMENT_H_ + +/*! \file document.h */ + +#include "reader.h" +#include "internal/meta.h" +#include "internal/strfunc.h" +#include "memorystream.h" +#include "encodedstream.h" +#include // placement new +#include + +RAPIDJSON_DIAG_PUSH +#ifdef _MSC_VER +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_OFF(effc++) +#if __GNUC__ >= 6 +RAPIDJSON_DIAG_OFF(terminate) // ignore throwing RAPIDJSON_ASSERT in RAPIDJSON_NOEXCEPT functions +#endif +#endif // __GNUC__ + +#ifndef RAPIDJSON_NOMEMBERITERATORCLASS +#include // std::iterator, std::random_access_iterator_tag +#endif + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#include // std::move +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +// Forward declaration. +template +class GenericValue; + +template +class GenericDocument; + +//! Name-value pair in a JSON object value. +/*! + This class was internal to GenericValue. It used to be a inner struct. + But a compiler (IBM XL C/C++ for AIX) have reported to have problem with that so it moved as a namespace scope struct. + https://code.google.com/p/rapidjson/issues/detail?id=64 +*/ +template +struct GenericMember { + GenericValue name; //!< name of member (must be a string) + GenericValue value; //!< value of member. +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericMemberIterator + +#ifndef RAPIDJSON_NOMEMBERITERATORCLASS + +//! (Constant) member iterator for a JSON object value +/*! + \tparam Const Is this a constant iterator? + \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) + \tparam Allocator Allocator type for allocating memory of object, array and string. + + This class implements a Random Access Iterator for GenericMember elements + of a GenericValue, see ISO/IEC 14882:2003(E) C++ standard, 24.1 [lib.iterator.requirements]. + + \note This iterator implementation is mainly intended to avoid implicit + conversions from iterator values to \c NULL, + e.g. from GenericValue::FindMember. + + \note Define \c RAPIDJSON_NOMEMBERITERATORCLASS to fall back to a + pointer-based implementation, if your platform doesn't provide + the C++ header. + + \see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator + */ +template +class GenericMemberIterator + : public std::iterator >::Type> { + + friend class GenericValue; + template friend class GenericMemberIterator; + + typedef GenericMember PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef std::iterator BaseType; + +public: + //! Iterator type itself + typedef GenericMemberIterator Iterator; + //! Constant iterator type + typedef GenericMemberIterator ConstIterator; + //! Non-constant iterator type + typedef GenericMemberIterator NonConstIterator; + + //! Pointer to (const) GenericMember + typedef typename BaseType::pointer Pointer; + //! Reference to (const) GenericMember + typedef typename BaseType::reference Reference; + //! Signed integer type (e.g. \c ptrdiff_t) + typedef typename BaseType::difference_type DifferenceType; + + //! Default constructor (singular value) + /*! Creates an iterator pointing to no element. + \note All operations, except for comparisons, are undefined on such values. + */ + GenericMemberIterator() : ptr_() {} + + //! Iterator conversions to more const + /*! + \param it (Non-const) iterator to copy from + + Allows the creation of an iterator from another GenericMemberIterator + that is "less const". Especially, creating a non-constant iterator + from a constant iterator are disabled: + \li const -> non-const (not ok) + \li const -> const (ok) + \li non-const -> const (ok) + \li non-const -> non-const (ok) + + \note If the \c Const template parameter is already \c false, this + constructor effectively defines a regular copy-constructor. + Otherwise, the copy constructor is implicitly defined. + */ + GenericMemberIterator(const NonConstIterator & it) : ptr_(it.ptr_) {} + Iterator& operator=(const NonConstIterator & it) { ptr_ = it.ptr_; return *this; } + + //! @name stepping + //@{ + Iterator& operator++(){ ++ptr_; return *this; } + Iterator& operator--(){ --ptr_; return *this; } + Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; } + Iterator operator--(int){ Iterator old(*this); --ptr_; return old; } + //@} + + //! @name increment/decrement + //@{ + Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); } + Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); } + + Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; } + Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; } + //@} + + //! @name relations + //@{ + bool operator==(ConstIterator that) const { return ptr_ == that.ptr_; } + bool operator!=(ConstIterator that) const { return ptr_ != that.ptr_; } + bool operator<=(ConstIterator that) const { return ptr_ <= that.ptr_; } + bool operator>=(ConstIterator that) const { return ptr_ >= that.ptr_; } + bool operator< (ConstIterator that) const { return ptr_ < that.ptr_; } + bool operator> (ConstIterator that) const { return ptr_ > that.ptr_; } + //@} + + //! @name dereference + //@{ + Reference operator*() const { return *ptr_; } + Pointer operator->() const { return ptr_; } + Reference operator[](DifferenceType n) const { return ptr_[n]; } + //@} + + //! Distance + DifferenceType operator-(ConstIterator that) const { return ptr_-that.ptr_; } + +private: + //! Internal constructor from plain pointer + explicit GenericMemberIterator(Pointer p) : ptr_(p) {} + + Pointer ptr_; //!< raw pointer +}; + +#else // RAPIDJSON_NOMEMBERITERATORCLASS + +// class-based member iterator implementation disabled, use plain pointers + +template +struct GenericMemberIterator; + +//! non-const GenericMemberIterator +template +struct GenericMemberIterator { + //! use plain pointer as iterator type + typedef GenericMember* Iterator; +}; +//! const GenericMemberIterator +template +struct GenericMemberIterator { + //! use plain const pointer as iterator type + typedef const GenericMember* Iterator; +}; + +#endif // RAPIDJSON_NOMEMBERITERATORCLASS + +/////////////////////////////////////////////////////////////////////////////// +// GenericStringRef + +//! Reference to a constant string (not taking a copy) +/*! + \tparam CharType character type of the string + + This helper class is used to automatically infer constant string + references for string literals, especially from \c const \b (!) + character arrays. + + The main use is for creating JSON string values without copying the + source string via an \ref Allocator. This requires that the referenced + string pointers have a sufficient lifetime, which exceeds the lifetime + of the associated GenericValue. + + \b Example + \code + Value v("foo"); // ok, no need to copy & calculate length + const char foo[] = "foo"; + v.SetString(foo); // ok + + const char* bar = foo; + // Value x(bar); // not ok, can't rely on bar's lifetime + Value x(StringRef(bar)); // lifetime explicitly guaranteed by user + Value y(StringRef(bar, 3)); // ok, explicitly pass length + \endcode + + \see StringRef, GenericValue::SetString +*/ +template +struct GenericStringRef { + typedef CharType Ch; //!< character type of the string + + //! Create string reference from \c const character array +#ifndef __clang__ // -Wdocumentation + /*! + This constructor implicitly creates a constant string reference from + a \c const character array. It has better performance than + \ref StringRef(const CharType*) by inferring the string \ref length + from the array length, and also supports strings containing null + characters. + + \tparam N length of the string, automatically inferred + + \param str Constant character array, lifetime assumed to be longer + than the use of the string in e.g. a GenericValue + + \post \ref s == str + + \note Constant complexity. + \note There is a hidden, private overload to disallow references to + non-const character arrays to be created via this constructor. + By this, e.g. function-scope arrays used to be filled via + \c snprintf are excluded from consideration. + In such cases, the referenced string should be \b copied to the + GenericValue instead. + */ +#endif + template + GenericStringRef(const CharType (&str)[N]) RAPIDJSON_NOEXCEPT + : s(str), length(N-1) {} + + //! Explicitly create string reference from \c const character pointer +#ifndef __clang__ // -Wdocumentation + /*! + This constructor can be used to \b explicitly create a reference to + a constant string pointer. + + \see StringRef(const CharType*) + + \param str Constant character pointer, lifetime assumed to be longer + than the use of the string in e.g. a GenericValue + + \post \ref s == str + + \note There is a hidden, private overload to disallow references to + non-const character arrays to be created via this constructor. + By this, e.g. function-scope arrays used to be filled via + \c snprintf are excluded from consideration. + In such cases, the referenced string should be \b copied to the + GenericValue instead. + */ +#endif + explicit GenericStringRef(const CharType* str) + : s(str), length(internal::StrLen(str)){ RAPIDJSON_ASSERT(s != 0); } + + //! Create constant string reference from pointer and length +#ifndef __clang__ // -Wdocumentation + /*! \param str constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \param len length of the string, excluding the trailing NULL terminator + + \post \ref s == str && \ref length == len + \note Constant complexity. + */ +#endif + GenericStringRef(const CharType* str, SizeType len) + : s(str), length(len) { RAPIDJSON_ASSERT(s != 0); } + + GenericStringRef(const GenericStringRef& rhs) : s(rhs.s), length(rhs.length) {} + + GenericStringRef& operator=(const GenericStringRef& rhs) { s = rhs.s; length = rhs.length; } + + //! implicit conversion to plain CharType pointer + operator const Ch *() const { return s; } + + const Ch* const s; //!< plain CharType pointer + const SizeType length; //!< length of the string (excluding the trailing NULL terminator) + +private: + //! Disallow construction from non-const array + template + GenericStringRef(CharType (&str)[N]) /* = delete */; +}; + +//! Mark a character pointer as constant string +/*! Mark a plain character pointer as a "string literal". This function + can be used to avoid copying a character string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + \tparam CharType Character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \return GenericStringRef string reference object + \relatesalso GenericStringRef + + \see GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember +*/ +template +inline GenericStringRef StringRef(const CharType* str) { + return GenericStringRef(str, internal::StrLen(str)); +} + +//! Mark a character pointer as constant string +/*! Mark a plain character pointer as a "string literal". This function + can be used to avoid copying a character string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + + This version has better performance with supplied length, and also + supports string containing null characters. + + \tparam CharType character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \param length The length of source string. + \return GenericStringRef string reference object + \relatesalso GenericStringRef +*/ +template +inline GenericStringRef StringRef(const CharType* str, size_t length) { + return GenericStringRef(str, SizeType(length)); +} + +#if RAPIDJSON_HAS_STDSTRING +//! Mark a string object as constant string +/*! Mark a string object (e.g. \c std::string) as a "string literal". + This function can be used to avoid copying a string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + + \tparam CharType character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \return GenericStringRef string reference object + \relatesalso GenericStringRef + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. +*/ +template +inline GenericStringRef StringRef(const std::basic_string& str) { + return GenericStringRef(str.data(), SizeType(str.size())); +} +#endif + +/////////////////////////////////////////////////////////////////////////////// +// GenericValue type traits +namespace internal { + +template +struct IsGenericValueImpl : FalseType {}; + +// select candidates according to nested encoding and allocator types +template struct IsGenericValueImpl::Type, typename Void::Type> + : IsBaseOf, T>::Type {}; + +// helper to match arbitrary GenericValue instantiations, including derived classes +template struct IsGenericValue : IsGenericValueImpl::Type {}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// TypeHelper + +namespace internal { + +template +struct TypeHelper {}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsBool(); } + static bool Get(const ValueType& v) { return v.GetBool(); } + static ValueType& Set(ValueType& v, bool data) { return v.SetBool(data); } + static ValueType& Set(ValueType& v, bool data, typename ValueType::AllocatorType&) { return v.SetBool(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsInt(); } + static int Get(const ValueType& v) { return v.GetInt(); } + static ValueType& Set(ValueType& v, int data) { return v.SetInt(data); } + static ValueType& Set(ValueType& v, int data, typename ValueType::AllocatorType&) { return v.SetInt(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsUint(); } + static unsigned Get(const ValueType& v) { return v.GetUint(); } + static ValueType& Set(ValueType& v, unsigned data) { return v.SetUint(data); } + static ValueType& Set(ValueType& v, unsigned data, typename ValueType::AllocatorType&) { return v.SetUint(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsInt64(); } + static int64_t Get(const ValueType& v) { return v.GetInt64(); } + static ValueType& Set(ValueType& v, int64_t data) { return v.SetInt64(data); } + static ValueType& Set(ValueType& v, int64_t data, typename ValueType::AllocatorType&) { return v.SetInt64(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsUint64(); } + static uint64_t Get(const ValueType& v) { return v.GetUint64(); } + static ValueType& Set(ValueType& v, uint64_t data) { return v.SetUint64(data); } + static ValueType& Set(ValueType& v, uint64_t data, typename ValueType::AllocatorType&) { return v.SetUint64(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsDouble(); } + static double Get(const ValueType& v) { return v.GetDouble(); } + static ValueType& Set(ValueType& v, double data) { return v.SetDouble(data); } + static ValueType& Set(ValueType& v, double data, typename ValueType::AllocatorType&) { return v.SetDouble(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsFloat(); } + static float Get(const ValueType& v) { return v.GetFloat(); } + static ValueType& Set(ValueType& v, float data) { return v.SetFloat(data); } + static ValueType& Set(ValueType& v, float data, typename ValueType::AllocatorType&) { return v.SetFloat(data); } +}; + +template +struct TypeHelper { + typedef const typename ValueType::Ch* StringType; + static bool Is(const ValueType& v) { return v.IsString(); } + static StringType Get(const ValueType& v) { return v.GetString(); } + static ValueType& Set(ValueType& v, const StringType data) { return v.SetString(typename ValueType::StringRefType(data)); } + static ValueType& Set(ValueType& v, const StringType data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); } +}; + +#if RAPIDJSON_HAS_STDSTRING +template +struct TypeHelper > { + typedef std::basic_string StringType; + static bool Is(const ValueType& v) { return v.IsString(); } + static StringType Get(const ValueType& v) { return StringType(v.GetString(), v.GetStringLength()); } + static ValueType& Set(ValueType& v, const StringType& data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); } +}; +#endif + +template +struct TypeHelper { + typedef typename ValueType::Array ArrayType; + static bool Is(const ValueType& v) { return v.IsArray(); } + static ArrayType Get(ValueType& v) { return v.GetArray(); } + static ValueType& Set(ValueType& v, ArrayType data) { return v = data; } + static ValueType& Set(ValueType& v, ArrayType data, typename ValueType::AllocatorType&) { return v = data; } +}; + +template +struct TypeHelper { + typedef typename ValueType::ConstArray ArrayType; + static bool Is(const ValueType& v) { return v.IsArray(); } + static ArrayType Get(const ValueType& v) { return v.GetArray(); } +}; + +template +struct TypeHelper { + typedef typename ValueType::Object ObjectType; + static bool Is(const ValueType& v) { return v.IsObject(); } + static ObjectType Get(ValueType& v) { return v.GetObject(); } + static ValueType& Set(ValueType& v, ObjectType data) { return v = data; } + static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { v = data; } +}; + +template +struct TypeHelper { + typedef typename ValueType::ConstObject ObjectType; + static bool Is(const ValueType& v) { return v.IsObject(); } + static ObjectType Get(const ValueType& v) { return v.GetObject(); } +}; + +} // namespace internal + +// Forward declarations +template class GenericArray; +template class GenericObject; + +/////////////////////////////////////////////////////////////////////////////// +// GenericValue + +//! Represents a JSON value. Use Value for UTF8 encoding and default allocator. +/*! + A JSON value can be one of 7 types. This class is a variant type supporting + these types. + + Use the Value if UTF8 and default allocator + + \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) + \tparam Allocator Allocator type for allocating memory of object, array and string. +*/ +template > +class GenericValue { +public: + //! Name-value pair in an object. + typedef GenericMember Member; + typedef Encoding EncodingType; //!< Encoding type from template parameter. + typedef Allocator AllocatorType; //!< Allocator type from template parameter. + typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. + typedef GenericStringRef StringRefType; //!< Reference to a constant string + typedef typename GenericMemberIterator::Iterator MemberIterator; //!< Member iterator for iterating in object. + typedef typename GenericMemberIterator::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object. + typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array. + typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array. + typedef GenericValue ValueType; //!< Value type of itself. + typedef GenericArray Array; + typedef GenericArray ConstArray; + typedef GenericObject Object; + typedef GenericObject ConstObject; + + //!@name Constructors and destructor. + //@{ + + //! Default constructor creates a null value. + GenericValue() RAPIDJSON_NOEXCEPT : data_() { data_.f.flags = kNullFlag; } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericValue(GenericValue&& rhs) RAPIDJSON_NOEXCEPT : data_(rhs.data_) { + rhs.data_.f.flags = kNullFlag; // give up contents + } +#endif + +private: + //! Copy constructor is not permitted. + GenericValue(const GenericValue& rhs); + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Moving from a GenericDocument is not permitted. + template + GenericValue(GenericDocument&& rhs); + + //! Move assignment from a GenericDocument is not permitted. + template + GenericValue& operator=(GenericDocument&& rhs); +#endif + +public: + + //! Constructor with JSON value type. + /*! This creates a Value of specified type with default content. + \param type Type of the value. + \note Default content for number is zero. + */ + explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_() { + static const uint16_t defaultFlags[7] = { + kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag, + kNumberAnyFlag + }; + RAPIDJSON_ASSERT(type <= kNumberType); + data_.f.flags = defaultFlags[type]; + + // Use ShortString to store empty string. + if (type == kStringType) + data_.ss.SetLength(0); + } + + //! Explicit copy constructor (with allocator) + /*! Creates a copy of a Value by using the given Allocator + \tparam SourceAllocator allocator of \c rhs + \param rhs Value to copy from (read-only) + \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator(). + \see CopyFrom() + */ + template< typename SourceAllocator > + GenericValue(const GenericValue& rhs, Allocator & allocator); + + //! Constructor for boolean value. + /*! \param b Boolean value + \note This constructor is limited to \em real boolean values and rejects + implicitly converted types like arbitrary pointers. Use an explicit cast + to \c bool, if you want to construct a boolean JSON value in such cases. + */ +#ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen + template + explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame))) RAPIDJSON_NOEXCEPT // See #472 +#else + explicit GenericValue(bool b) RAPIDJSON_NOEXCEPT +#endif + : data_() { + // safe-guard against failing SFINAE + RAPIDJSON_STATIC_ASSERT((internal::IsSame::Value)); + data_.f.flags = b ? kTrueFlag : kFalseFlag; + } + + //! Constructor for int value. + explicit GenericValue(int i) RAPIDJSON_NOEXCEPT : data_() { + data_.n.i64 = i; + data_.f.flags = (i >= 0) ? (kNumberIntFlag | kUintFlag | kUint64Flag) : kNumberIntFlag; + } + + //! Constructor for unsigned value. + explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_() { + data_.n.u64 = u; + data_.f.flags = (u & 0x80000000) ? kNumberUintFlag : (kNumberUintFlag | kIntFlag | kInt64Flag); + } + + //! Constructor for int64_t value. + explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_() { + data_.n.i64 = i64; + data_.f.flags = kNumberInt64Flag; + if (i64 >= 0) { + data_.f.flags |= kNumberUint64Flag; + if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) + data_.f.flags |= kUintFlag; + if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + else if (i64 >= static_cast(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + + //! Constructor for uint64_t value. + explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_() { + data_.n.u64 = u64; + data_.f.flags = kNumberUint64Flag; + if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000))) + data_.f.flags |= kInt64Flag; + if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) + data_.f.flags |= kUintFlag; + if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + + //! Constructor for double value. + explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = d; data_.f.flags = kNumberDoubleFlag; } + + //! Constructor for constant string (i.e. do not make a copy of string) + GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(StringRef(s, length)); } + + //! Constructor for constant string (i.e. do not make a copy of string) + explicit GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(s); } + + //! Constructor for copy-string (i.e. do make a copy of string) + GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_() { SetStringRaw(StringRef(s, length), allocator); } + + //! Constructor for copy-string (i.e. do make a copy of string) + GenericValue(const Ch*s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); } + +#if RAPIDJSON_HAS_STDSTRING + //! Constructor for copy-string from a string object (i.e. do make a copy of string) + /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + GenericValue(const std::basic_string& s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); } +#endif + + //! Constructor for Array. + /*! + \param a An array obtained by \c GetArray(). + \note \c Array is always pass-by-value. + \note the source array is moved into this value and the sourec array becomes empty. + */ + GenericValue(Array a) RAPIDJSON_NOEXCEPT : data_(a.value_.data_) { + a.value_.data_ = Data(); + a.value_.data_.f.flags = kArrayFlag; + } + + //! Constructor for Object. + /*! + \param o An object obtained by \c GetObject(). + \note \c Object is always pass-by-value. + \note the source object is moved into this value and the sourec object becomes empty. + */ + GenericValue(Object o) RAPIDJSON_NOEXCEPT : data_(o.value_.data_) { + o.value_.data_ = Data(); + o.value_.data_.f.flags = kObjectFlag; + } + + //! Destructor. + /*! Need to destruct elements of array, members of object, or copy-string. + */ + ~GenericValue() { + if (Allocator::kNeedFree) { // Shortcut by Allocator's trait + switch(data_.f.flags) { + case kArrayFlag: + { + GenericValue* e = GetElementsPointer(); + for (GenericValue* v = e; v != e + data_.a.size; ++v) + v->~GenericValue(); + Allocator::Free(e); + } + break; + + case kObjectFlag: + for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) + m->~Member(); + Allocator::Free(GetMembersPointer()); + break; + + case kCopyStringFlag: + Allocator::Free(const_cast(GetStringPointer())); + break; + + default: + break; // Do nothing for other types. + } + } + } + + //@} + + //!@name Assignment operators + //@{ + + //! Assignment with move semantics. + /*! \param rhs Source of the assignment. It will become a null value after assignment. + */ + GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT { + RAPIDJSON_ASSERT(this != &rhs); + this->~GenericValue(); + RawAssign(rhs); + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move assignment in C++11 + GenericValue& operator=(GenericValue&& rhs) RAPIDJSON_NOEXCEPT { + return *this = rhs.Move(); + } +#endif + + //! Assignment of constant string reference (no copy) + /*! \param str Constant string reference to be assigned + \note This overload is needed to avoid clashes with the generic primitive type assignment overload below. + \see GenericStringRef, operator=(T) + */ + GenericValue& operator=(StringRefType str) RAPIDJSON_NOEXCEPT { + GenericValue s(str); + return *this = s; + } + + //! Assignment with primitive types. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param value The value to be assigned. + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref SetString(const Ch*, Allocator&) (for copying) or + \ref StringRef() (to explicitly mark the pointer as constant) instead. + All other pointer types would implicitly convert to \c bool, + use \ref SetBool() instead. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer), (GenericValue&)) + operator=(T value) { + GenericValue v(value); + return *this = v; + } + + //! Deep-copy assignment from Value + /*! Assigns a \b copy of the Value to the current Value object + \tparam SourceAllocator Allocator type of \c rhs + \param rhs Value to copy from (read-only) + \param allocator Allocator to use for copying + */ + template + GenericValue& CopyFrom(const GenericValue& rhs, Allocator& allocator) { + RAPIDJSON_ASSERT(static_cast(this) != static_cast(&rhs)); + this->~GenericValue(); + new (this) GenericValue(rhs, allocator); + return *this; + } + + //! Exchange the contents of this value with those of other. + /*! + \param other Another value. + \note Constant complexity. + */ + GenericValue& Swap(GenericValue& other) RAPIDJSON_NOEXCEPT { + GenericValue temp; + temp.RawAssign(*this); + RawAssign(other); + other.RawAssign(temp); + return *this; + } + + //! free-standing swap function helper + /*! + Helper function to enable support for common swap implementation pattern based on \c std::swap: + \code + void swap(MyClass& a, MyClass& b) { + using std::swap; + swap(a.value, b.value); + // ... + } + \endcode + \see Swap() + */ + friend inline void swap(GenericValue& a, GenericValue& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + + //! Prepare Value for move semantics + /*! \return *this */ + GenericValue& Move() RAPIDJSON_NOEXCEPT { return *this; } + //@} + + //!@name Equal-to and not-equal-to operators + //@{ + //! Equal-to operator + /*! + \note If an object contains duplicated named member, comparing equality with any object is always \c false. + \note Linear time complexity (number of all values in the subtree and total lengths of all strings). + */ + template + bool operator==(const GenericValue& rhs) const { + typedef GenericValue RhsType; + if (GetType() != rhs.GetType()) + return false; + + switch (GetType()) { + case kObjectType: // Warning: O(n^2) inner-loop + if (data_.o.size != rhs.data_.o.size) + return false; + for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) { + typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name); + if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value) + return false; + } + return true; + + case kArrayType: + if (data_.a.size != rhs.data_.a.size) + return false; + for (SizeType i = 0; i < data_.a.size; i++) + if ((*this)[i] != rhs[i]) + return false; + return true; + + case kStringType: + return StringEqual(rhs); + + case kNumberType: + if (IsDouble() || rhs.IsDouble()) { + double a = GetDouble(); // May convert from integer to double. + double b = rhs.GetDouble(); // Ditto + return a >= b && a <= b; // Prevent -Wfloat-equal + } + else + return data_.n.u64 == rhs.data_.n.u64; + + default: + return true; + } + } + + //! Equal-to operator with const C-string pointer + bool operator==(const Ch* rhs) const { return *this == GenericValue(StringRef(rhs)); } + +#if RAPIDJSON_HAS_STDSTRING + //! Equal-to operator with string object + /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + bool operator==(const std::basic_string& rhs) const { return *this == GenericValue(StringRef(rhs)); } +#endif + + //! Equal-to operator with primitive types + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false + */ + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr,internal::IsGenericValue >), (bool)) operator==(const T& rhs) const { return *this == GenericValue(rhs); } + + //! Not-equal-to operator + /*! \return !(*this == rhs) + */ + template + bool operator!=(const GenericValue& rhs) const { return !(*this == rhs); } + + //! Not-equal-to operator with const C-string pointer + bool operator!=(const Ch* rhs) const { return !(*this == rhs); } + + //! Not-equal-to operator with arbitrary types + /*! \return !(*this == rhs) + */ + template RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& rhs) const { return !(*this == rhs); } + + //! Equal-to operator with arbitrary types (symmetric version) + /*! \return (rhs == lhs) + */ + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator==(const T& lhs, const GenericValue& rhs) { return rhs == lhs; } + + //! Not-Equal-to operator with arbitrary types (symmetric version) + /*! \return !(rhs == lhs) + */ + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& lhs, const GenericValue& rhs) { return !(rhs == lhs); } + //@} + + //!@name Type + //@{ + + Type GetType() const { return static_cast(data_.f.flags & kTypeMask); } + bool IsNull() const { return data_.f.flags == kNullFlag; } + bool IsFalse() const { return data_.f.flags == kFalseFlag; } + bool IsTrue() const { return data_.f.flags == kTrueFlag; } + bool IsBool() const { return (data_.f.flags & kBoolFlag) != 0; } + bool IsObject() const { return data_.f.flags == kObjectFlag; } + bool IsArray() const { return data_.f.flags == kArrayFlag; } + bool IsNumber() const { return (data_.f.flags & kNumberFlag) != 0; } + bool IsInt() const { return (data_.f.flags & kIntFlag) != 0; } + bool IsUint() const { return (data_.f.flags & kUintFlag) != 0; } + bool IsInt64() const { return (data_.f.flags & kInt64Flag) != 0; } + bool IsUint64() const { return (data_.f.flags & kUint64Flag) != 0; } + bool IsDouble() const { return (data_.f.flags & kDoubleFlag) != 0; } + bool IsString() const { return (data_.f.flags & kStringFlag) != 0; } + + // Checks whether a number can be losslessly converted to a double. + bool IsLosslessDouble() const { + if (!IsNumber()) return false; + if (IsUint64()) { + uint64_t u = GetUint64(); + volatile double d = static_cast(u); + return (d >= 0.0) + && (d < static_cast(std::numeric_limits::max())) + && (u == static_cast(d)); + } + if (IsInt64()) { + int64_t i = GetInt64(); + volatile double d = static_cast(i); + return (d >= static_cast(std::numeric_limits::min())) + && (d < static_cast(std::numeric_limits::max())) + && (i == static_cast(d)); + } + return true; // double, int, uint are always lossless + } + + // Checks whether a number is a float (possible lossy). + bool IsFloat() const { + if ((data_.f.flags & kDoubleFlag) == 0) + return false; + double d = GetDouble(); + return d >= -3.4028234e38 && d <= 3.4028234e38; + } + // Checks whether a number can be losslessly converted to a float. + bool IsLosslessFloat() const { + if (!IsNumber()) return false; + double a = GetDouble(); + if (a < static_cast(-std::numeric_limits::max()) + || a > static_cast(std::numeric_limits::max())) + return false; + double b = static_cast(static_cast(a)); + return a >= b && a <= b; // Prevent -Wfloat-equal + } + + //@} + + //!@name Null + //@{ + + GenericValue& SetNull() { this->~GenericValue(); new (this) GenericValue(); return *this; } + + //@} + + //!@name Bool + //@{ + + bool GetBool() const { RAPIDJSON_ASSERT(IsBool()); return data_.f.flags == kTrueFlag; } + //!< Set boolean value + /*! \post IsBool() == true */ + GenericValue& SetBool(bool b) { this->~GenericValue(); new (this) GenericValue(b); return *this; } + + //@} + + //!@name Object + //@{ + + //! Set this value as an empty object. + /*! \post IsObject() == true */ + GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; } + + //! Get the number of members in the object. + SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; } + + //! Check whether the object is empty. + bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; } + + //! Get a value from an object associated with the name. + /*! \pre IsObject() == true + \tparam T Either \c Ch or \c const \c Ch (template used for disambiguation with \ref operator[](SizeType)) + \note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7. + Since 0.2, if the name is not correct, it will assert. + If user is unsure whether a member exists, user should use HasMember() first. + A better approach is to use FindMember(). + \note Linear time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >),(GenericValue&)) operator[](T* name) { + GenericValue n(StringRef(name)); + return (*this)[n]; + } + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >),(const GenericValue&)) operator[](T* name) const { return const_cast(*this)[name]; } + + //! Get a value from an object associated with the name. + /*! \pre IsObject() == true + \tparam SourceAllocator Allocator of the \c name value + + \note Compared to \ref operator[](T*), this version is faster because it does not need a StrLen(). + And it can also handle strings with embedded null characters. + + \note Linear time complexity. + */ + template + GenericValue& operator[](const GenericValue& name) { + MemberIterator member = FindMember(name); + if (member != MemberEnd()) + return member->value; + else { + RAPIDJSON_ASSERT(false); // see above note + + // This will generate -Wexit-time-destructors in clang + // static GenericValue NullValue; + // return NullValue; + + // Use static buffer and placement-new to prevent destruction + static char buffer[sizeof(GenericValue)]; + return *new (buffer) GenericValue(); + } + } + template + const GenericValue& operator[](const GenericValue& name) const { return const_cast(*this)[name]; } + +#if RAPIDJSON_HAS_STDSTRING + //! Get a value from an object associated with name (string object). + GenericValue& operator[](const std::basic_string& name) { return (*this)[GenericValue(StringRef(name))]; } + const GenericValue& operator[](const std::basic_string& name) const { return (*this)[GenericValue(StringRef(name))]; } +#endif + + //! Const member iterator + /*! \pre IsObject() == true */ + ConstMemberIterator MemberBegin() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(GetMembersPointer()); } + //! Const \em past-the-end member iterator + /*! \pre IsObject() == true */ + ConstMemberIterator MemberEnd() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(GetMembersPointer() + data_.o.size); } + //! Member iterator + /*! \pre IsObject() == true */ + MemberIterator MemberBegin() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer()); } + //! \em Past-the-end member iterator + /*! \pre IsObject() == true */ + MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer() + data_.o.size); } + + //! Check whether a member exists in the object. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + bool HasMember(const Ch* name) const { return FindMember(name) != MemberEnd(); } + +#if RAPIDJSON_HAS_STDSTRING + //! Check whether a member exists in the object with string object. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + bool HasMember(const std::basic_string& name) const { return FindMember(name) != MemberEnd(); } +#endif + + //! Check whether a member exists in the object with GenericValue name. + /*! + This version is faster because it does not need a StrLen(). It can also handle string with null character. + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + template + bool HasMember(const GenericValue& name) const { return FindMember(name) != MemberEnd(); } + + //! Find member by name. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + + \note Earlier versions of Rapidjson returned a \c NULL pointer, in case + the requested member doesn't exist. For consistency with e.g. + \c std::map, this has been changed to MemberEnd() now. + \note Linear time complexity. + */ + MemberIterator FindMember(const Ch* name) { + GenericValue n(StringRef(name)); + return FindMember(n); + } + + ConstMemberIterator FindMember(const Ch* name) const { return const_cast(*this).FindMember(name); } + + //! Find member by name. + /*! + This version is faster because it does not need a StrLen(). It can also handle string with null character. + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + + \note Earlier versions of Rapidjson returned a \c NULL pointer, in case + the requested member doesn't exist. For consistency with e.g. + \c std::map, this has been changed to MemberEnd() now. + \note Linear time complexity. + */ + template + MemberIterator FindMember(const GenericValue& name) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(name.IsString()); + MemberIterator member = MemberBegin(); + for ( ; member != MemberEnd(); ++member) + if (name.StringEqual(member->name)) + break; + return member; + } + template ConstMemberIterator FindMember(const GenericValue& name) const { return const_cast(*this).FindMember(name); } + +#if RAPIDJSON_HAS_STDSTRING + //! Find member by string object name. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + */ + MemberIterator FindMember(const std::basic_string& name) { return FindMember(GenericValue(StringRef(name))); } + ConstMemberIterator FindMember(const std::basic_string& name) const { return FindMember(GenericValue(StringRef(name))); } +#endif + + //! Add a member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value Value of any type. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note The ownership of \c name and \c value will be transferred to this object on success. + \pre IsObject() && name.IsString() + \post name.IsNull() && value.IsNull() + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, GenericValue& value, Allocator& allocator) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(name.IsString()); + + ObjectData& o = data_.o; + if (o.size >= o.capacity) { + if (o.capacity == 0) { + o.capacity = kDefaultObjectCapacity; + SetMembersPointer(reinterpret_cast(allocator.Malloc(o.capacity * sizeof(Member)))); + } + else { + SizeType oldCapacity = o.capacity; + o.capacity += (oldCapacity + 1) / 2; // grow by factor 1.5 + SetMembersPointer(reinterpret_cast(allocator.Realloc(GetMembersPointer(), oldCapacity * sizeof(Member), o.capacity * sizeof(Member)))); + } + } + Member* members = GetMembersPointer(); + members[o.size].name.RawAssign(name); + members[o.size].value.RawAssign(value); + o.size++; + return *this; + } + + //! Add a constant string value as member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, StringRefType value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Add a string object as member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, std::basic_string& value, Allocator& allocator) { + GenericValue v(value, allocator); + return AddMember(name, v, allocator); + } +#endif + + //! Add any primitive value as member (name-value pair) to the object. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param name A string value as name of member. + \param value Value of primitive type \c T as value of member + \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref + AddMember(StringRefType, StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized Constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + AddMember(GenericValue& name, T value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericValue& AddMember(GenericValue&& name, GenericValue&& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(GenericValue&& name, GenericValue& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(GenericValue& name, GenericValue&& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(StringRefType name, GenericValue&& value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + + + //! Add a member (name-value pair) to the object. + /*! \param name A constant string reference as name of member. + \param value Value of any type. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note The ownership of \c value will be transferred to this object on success. + \pre IsObject() + \post value.IsNull() + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(StringRefType name, GenericValue& value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } + + //! Add a constant string value as member (name-value pair) to the object. + /*! \param name A constant string reference as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(StringRefType,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(StringRefType name, StringRefType value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + + //! Add any primitive value as member (name-value pair) to the object. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param name A constant string reference as name of member. + \param value Value of primitive type \c T as value of member + \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref + AddMember(StringRefType, StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized Constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + AddMember(StringRefType name, T value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } + + //! Remove all members in the object. + /*! This function do not deallocate memory in the object, i.e. the capacity is unchanged. + \note Linear time complexity. + */ + void RemoveAllMembers() { + RAPIDJSON_ASSERT(IsObject()); + for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) + m->~Member(); + data_.o.size = 0; + } + + //! Remove a member in object by its name. + /*! \param name Name of member to be removed. + \return Whether the member existed. + \note This function may reorder the object members. Use \ref + EraseMember(ConstMemberIterator) if you need to preserve the + relative order of the remaining members. + \note Linear time complexity. + */ + bool RemoveMember(const Ch* name) { + GenericValue n(StringRef(name)); + return RemoveMember(n); + } + +#if RAPIDJSON_HAS_STDSTRING + bool RemoveMember(const std::basic_string& name) { return RemoveMember(GenericValue(StringRef(name))); } +#endif + + template + bool RemoveMember(const GenericValue& name) { + MemberIterator m = FindMember(name); + if (m != MemberEnd()) { + RemoveMember(m); + return true; + } + else + return false; + } + + //! Remove a member in object by iterator. + /*! \param m member iterator (obtained by FindMember() or MemberBegin()). + \return the new iterator after removal. + \note This function may reorder the object members. Use \ref + EraseMember(ConstMemberIterator) if you need to preserve the + relative order of the remaining members. + \note Constant time complexity. + */ + MemberIterator RemoveMember(MemberIterator m) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(data_.o.size > 0); + RAPIDJSON_ASSERT(GetMembersPointer() != 0); + RAPIDJSON_ASSERT(m >= MemberBegin() && m < MemberEnd()); + + MemberIterator last(GetMembersPointer() + (data_.o.size - 1)); + if (data_.o.size > 1 && m != last) + *m = *last; // Move the last one to this place + else + m->~Member(); // Only one left, just destroy + --data_.o.size; + return m; + } + + //! Remove a member from an object by iterator. + /*! \param pos iterator to the member to remove + \pre IsObject() == true && \ref MemberBegin() <= \c pos < \ref MemberEnd() + \return Iterator following the removed element. + If the iterator \c pos refers to the last element, the \ref MemberEnd() iterator is returned. + \note This function preserves the relative order of the remaining object + members. If you do not need this, use the more efficient \ref RemoveMember(MemberIterator). + \note Linear time complexity. + */ + MemberIterator EraseMember(ConstMemberIterator pos) { + return EraseMember(pos, pos +1); + } + + //! Remove members in the range [first, last) from an object. + /*! \param first iterator to the first member to remove + \param last iterator following the last member to remove + \pre IsObject() == true && \ref MemberBegin() <= \c first <= \c last <= \ref MemberEnd() + \return Iterator following the last removed element. + \note This function preserves the relative order of the remaining object + members. + \note Linear time complexity. + */ + MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(data_.o.size > 0); + RAPIDJSON_ASSERT(GetMembersPointer() != 0); + RAPIDJSON_ASSERT(first >= MemberBegin()); + RAPIDJSON_ASSERT(first <= last); + RAPIDJSON_ASSERT(last <= MemberEnd()); + + MemberIterator pos = MemberBegin() + (first - MemberBegin()); + for (MemberIterator itr = pos; itr != last; ++itr) + itr->~Member(); + std::memmove(&*pos, &*last, static_cast(MemberEnd() - last) * sizeof(Member)); + data_.o.size -= static_cast(last - first); + return pos; + } + + //! Erase a member in object by its name. + /*! \param name Name of member to be removed. + \return Whether the member existed. + \note Linear time complexity. + */ + bool EraseMember(const Ch* name) { + GenericValue n(StringRef(name)); + return EraseMember(n); + } + +#if RAPIDJSON_HAS_STDSTRING + bool EraseMember(const std::basic_string& name) { return EraseMember(GenericValue(StringRef(name))); } +#endif + + template + bool EraseMember(const GenericValue& name) { + MemberIterator m = FindMember(name); + if (m != MemberEnd()) { + EraseMember(m); + return true; + } + else + return false; + } + + Object GetObject() { RAPIDJSON_ASSERT(IsObject()); return Object(*this); } + ConstObject GetObject() const { RAPIDJSON_ASSERT(IsObject()); return ConstObject(*this); } + + //@} + + //!@name Array + //@{ + + //! Set this value as an empty array. + /*! \post IsArray == true */ + GenericValue& SetArray() { this->~GenericValue(); new (this) GenericValue(kArrayType); return *this; } + + //! Get the number of elements in array. + SizeType Size() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size; } + + //! Get the capacity of array. + SizeType Capacity() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.capacity; } + + //! Check whether the array is empty. + bool Empty() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size == 0; } + + //! Remove all elements in the array. + /*! This function do not deallocate memory in the array, i.e. the capacity is unchanged. + \note Linear time complexity. + */ + void Clear() { + RAPIDJSON_ASSERT(IsArray()); + GenericValue* e = GetElementsPointer(); + for (GenericValue* v = e; v != e + data_.a.size; ++v) + v->~GenericValue(); + data_.a.size = 0; + } + + //! Get an element from array by index. + /*! \pre IsArray() == true + \param index Zero-based index of element. + \see operator[](T*) + */ + GenericValue& operator[](SizeType index) { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(index < data_.a.size); + return GetElementsPointer()[index]; + } + const GenericValue& operator[](SizeType index) const { return const_cast(*this)[index]; } + + //! Element iterator + /*! \pre IsArray() == true */ + ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return GetElementsPointer(); } + //! \em Past-the-end element iterator + /*! \pre IsArray() == true */ + ValueIterator End() { RAPIDJSON_ASSERT(IsArray()); return GetElementsPointer() + data_.a.size; } + //! Constant element iterator + /*! \pre IsArray() == true */ + ConstValueIterator Begin() const { return const_cast(*this).Begin(); } + //! Constant \em past-the-end element iterator + /*! \pre IsArray() == true */ + ConstValueIterator End() const { return const_cast(*this).End(); } + + //! Request the array to have enough capacity to store elements. + /*! \param newCapacity The capacity that the array at least need to have. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note Linear time complexity. + */ + GenericValue& Reserve(SizeType newCapacity, Allocator &allocator) { + RAPIDJSON_ASSERT(IsArray()); + if (newCapacity > data_.a.capacity) { + SetElementsPointer(reinterpret_cast(allocator.Realloc(GetElementsPointer(), data_.a.capacity * sizeof(GenericValue), newCapacity * sizeof(GenericValue)))); + data_.a.capacity = newCapacity; + } + return *this; + } + + //! Append a GenericValue at the end of the array. + /*! \param value Value to be appended. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \post value.IsNull() == true + \return The value itself for fluent API. + \note The ownership of \c value will be transferred to this array on success. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + \note Amortized constant time complexity. + */ + GenericValue& PushBack(GenericValue& value, Allocator& allocator) { + RAPIDJSON_ASSERT(IsArray()); + if (data_.a.size >= data_.a.capacity) + Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : (data_.a.capacity + (data_.a.capacity + 1) / 2), allocator); + GetElementsPointer()[data_.a.size++].RawAssign(value); + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericValue& PushBack(GenericValue&& value, Allocator& allocator) { + return PushBack(value, allocator); + } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + + //! Append a constant string reference at the end of the array. + /*! \param value Constant string reference to be appended. + \param allocator Allocator for reallocating memory. It must be the same one used previously. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \return The value itself for fluent API. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + \note Amortized constant time complexity. + \see GenericStringRef + */ + GenericValue& PushBack(StringRefType value, Allocator& allocator) { + return (*this).template PushBack(value, allocator); + } + + //! Append a primitive value at the end of the array. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param value Value of primitive type T to be appended. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \return The value itself for fluent API. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref PushBack(GenericValue&, Allocator&) or \ref + PushBack(StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + PushBack(T value, Allocator& allocator) { + GenericValue v(value); + return PushBack(v, allocator); + } + + //! Remove the last element in the array. + /*! + \note Constant time complexity. + */ + GenericValue& PopBack() { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(!Empty()); + GetElementsPointer()[--data_.a.size].~GenericValue(); + return *this; + } + + //! Remove an element of array by iterator. + /*! + \param pos iterator to the element to remove + \pre IsArray() == true && \ref Begin() <= \c pos < \ref End() + \return Iterator following the removed element. If the iterator pos refers to the last element, the End() iterator is returned. + \note Linear time complexity. + */ + ValueIterator Erase(ConstValueIterator pos) { + return Erase(pos, pos + 1); + } + + //! Remove elements in the range [first, last) of the array. + /*! + \param first iterator to the first element to remove + \param last iterator following the last element to remove + \pre IsArray() == true && \ref Begin() <= \c first <= \c last <= \ref End() + \return Iterator following the last removed element. + \note Linear time complexity. + */ + ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(data_.a.size > 0); + RAPIDJSON_ASSERT(GetElementsPointer() != 0); + RAPIDJSON_ASSERT(first >= Begin()); + RAPIDJSON_ASSERT(first <= last); + RAPIDJSON_ASSERT(last <= End()); + ValueIterator pos = Begin() + (first - Begin()); + for (ValueIterator itr = pos; itr != last; ++itr) + itr->~GenericValue(); + std::memmove(pos, last, static_cast(End() - last) * sizeof(GenericValue)); + data_.a.size -= static_cast(last - first); + return pos; + } + + Array GetArray() { RAPIDJSON_ASSERT(IsArray()); return Array(*this); } + ConstArray GetArray() const { RAPIDJSON_ASSERT(IsArray()); return ConstArray(*this); } + + //@} + + //!@name Number + //@{ + + int GetInt() const { RAPIDJSON_ASSERT(data_.f.flags & kIntFlag); return data_.n.i.i; } + unsigned GetUint() const { RAPIDJSON_ASSERT(data_.f.flags & kUintFlag); return data_.n.u.u; } + int64_t GetInt64() const { RAPIDJSON_ASSERT(data_.f.flags & kInt64Flag); return data_.n.i64; } + uint64_t GetUint64() const { RAPIDJSON_ASSERT(data_.f.flags & kUint64Flag); return data_.n.u64; } + + //! Get the value as double type. + /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessDouble() to check whether the converison is lossless. + */ + double GetDouble() const { + RAPIDJSON_ASSERT(IsNumber()); + if ((data_.f.flags & kDoubleFlag) != 0) return data_.n.d; // exact type, no conversion. + if ((data_.f.flags & kIntFlag) != 0) return data_.n.i.i; // int -> double + if ((data_.f.flags & kUintFlag) != 0) return data_.n.u.u; // unsigned -> double + if ((data_.f.flags & kInt64Flag) != 0) return static_cast(data_.n.i64); // int64_t -> double (may lose precision) + RAPIDJSON_ASSERT((data_.f.flags & kUint64Flag) != 0); return static_cast(data_.n.u64); // uint64_t -> double (may lose precision) + } + + //! Get the value as float type. + /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessFloat() to check whether the converison is lossless. + */ + float GetFloat() const { + return static_cast(GetDouble()); + } + + GenericValue& SetInt(int i) { this->~GenericValue(); new (this) GenericValue(i); return *this; } + GenericValue& SetUint(unsigned u) { this->~GenericValue(); new (this) GenericValue(u); return *this; } + GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; } + GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; } + GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; } + GenericValue& SetFloat(float f) { this->~GenericValue(); new (this) GenericValue(f); return *this; } + + //@} + + //!@name String + //@{ + + const Ch* GetString() const { RAPIDJSON_ASSERT(IsString()); return (data_.f.flags & kInlineStrFlag) ? data_.ss.str : GetStringPointer(); } + + //! Get the length of string. + /*! Since rapidjson permits "\\u0000" in the json string, strlen(v.GetString()) may not equal to v.GetStringLength(). + */ + SizeType GetStringLength() const { RAPIDJSON_ASSERT(IsString()); return ((data_.f.flags & kInlineStrFlag) ? (data_.ss.GetLength()) : data_.s.length); } + + //! Set this value as a string without copying source string. + /*! This version has better performance with supplied length, and also support string containing null character. + \param s source string pointer. + \param length The length of source string, excluding the trailing null terminator. + \return The value itself for fluent API. + \post IsString() == true && GetString() == s && GetStringLength() == length + \see SetString(StringRefType) + */ + GenericValue& SetString(const Ch* s, SizeType length) { return SetString(StringRef(s, length)); } + + //! Set this value as a string without copying source string. + /*! \param s source string reference + \return The value itself for fluent API. + \post IsString() == true && GetString() == s && GetStringLength() == s.length + */ + GenericValue& SetString(StringRefType s) { this->~GenericValue(); SetStringRaw(s); return *this; } + + //! Set this value as a string by copying from source string. + /*! This version has better performance with supplied length, and also support string containing null character. + \param s source string. + \param length The length of source string, excluding the trailing null terminator. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { this->~GenericValue(); SetStringRaw(StringRef(s, length), allocator); return *this; } + + //! Set this value as a string by copying from source string. + /*! \param s source string. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(s, internal::StrLen(s), allocator); } + +#if RAPIDJSON_HAS_STDSTRING + //! Set this value as a string by copying from source string. + /*! \param s source string. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s.data() && strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size() + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + GenericValue& SetString(const std::basic_string& s, Allocator& allocator) { return SetString(s.data(), SizeType(s.size()), allocator); } +#endif + + //@} + + //!@name Array + //@{ + + //! Templated version for checking whether this value is type T. + /*! + \tparam T Either \c bool, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c float, \c const \c char*, \c std::basic_string + */ + template + bool Is() const { return internal::TypeHelper::Is(*this); } + + template + T Get() const { return internal::TypeHelper::Get(*this); } + + template + T Get() { return internal::TypeHelper::Get(*this); } + + template + ValueType& Set(const T& data) { return internal::TypeHelper::Set(*this, data); } + + template + ValueType& Set(const T& data, AllocatorType& allocator) { return internal::TypeHelper::Set(*this, data, allocator); } + + //@} + + //! Generate events of this value to a Handler. + /*! This function adopts the GoF visitor pattern. + Typical usage is to output this JSON value as JSON text via Writer, which is a Handler. + It can also be used to deep clone this value via GenericDocument, which is also a Handler. + \tparam Handler type of handler. + \param handler An object implementing concept Handler. + */ + template + bool Accept(Handler& handler) const { + switch(GetType()) { + case kNullType: return handler.Null(); + case kFalseType: return handler.Bool(false); + case kTrueType: return handler.Bool(true); + + case kObjectType: + if (RAPIDJSON_UNLIKELY(!handler.StartObject())) + return false; + for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) { + RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of name by MemberIterator. + if (RAPIDJSON_UNLIKELY(!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.data_.f.flags & kCopyFlag) != 0))) + return false; + if (RAPIDJSON_UNLIKELY(!m->value.Accept(handler))) + return false; + } + return handler.EndObject(data_.o.size); + + case kArrayType: + if (RAPIDJSON_UNLIKELY(!handler.StartArray())) + return false; + for (const GenericValue* v = Begin(); v != End(); ++v) + if (RAPIDJSON_UNLIKELY(!v->Accept(handler))) + return false; + return handler.EndArray(data_.a.size); + + case kStringType: + return handler.String(GetString(), GetStringLength(), (data_.f.flags & kCopyFlag) != 0); + + default: + RAPIDJSON_ASSERT(GetType() == kNumberType); + if (IsDouble()) return handler.Double(data_.n.d); + else if (IsInt()) return handler.Int(data_.n.i.i); + else if (IsUint()) return handler.Uint(data_.n.u.u); + else if (IsInt64()) return handler.Int64(data_.n.i64); + else return handler.Uint64(data_.n.u64); + } + } + +private: + template friend class GenericValue; + template friend class GenericDocument; + + enum { + kBoolFlag = 0x0008, + kNumberFlag = 0x0010, + kIntFlag = 0x0020, + kUintFlag = 0x0040, + kInt64Flag = 0x0080, + kUint64Flag = 0x0100, + kDoubleFlag = 0x0200, + kStringFlag = 0x0400, + kCopyFlag = 0x0800, + kInlineStrFlag = 0x1000, + + // Initial flags of different types. + kNullFlag = kNullType, + kTrueFlag = kTrueType | kBoolFlag, + kFalseFlag = kFalseType | kBoolFlag, + kNumberIntFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag, + kNumberUintFlag = kNumberType | kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag, + kNumberInt64Flag = kNumberType | kNumberFlag | kInt64Flag, + kNumberUint64Flag = kNumberType | kNumberFlag | kUint64Flag, + kNumberDoubleFlag = kNumberType | kNumberFlag | kDoubleFlag, + kNumberAnyFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag, + kConstStringFlag = kStringType | kStringFlag, + kCopyStringFlag = kStringType | kStringFlag | kCopyFlag, + kShortStringFlag = kStringType | kStringFlag | kCopyFlag | kInlineStrFlag, + kObjectFlag = kObjectType, + kArrayFlag = kArrayType, + + kTypeMask = 0x07 + }; + + static const SizeType kDefaultArrayCapacity = 16; + static const SizeType kDefaultObjectCapacity = 16; + + struct Flag { +#if RAPIDJSON_48BITPOINTER_OPTIMIZATION + char payload[sizeof(SizeType) * 2 + 6]; // 2 x SizeType + lower 48-bit pointer +#elif RAPIDJSON_64BIT + char payload[sizeof(SizeType) * 2 + sizeof(void*) + 6]; // 6 padding bytes +#else + char payload[sizeof(SizeType) * 2 + sizeof(void*) + 2]; // 2 padding bytes +#endif + uint16_t flags; + }; + + struct String { + SizeType length; + SizeType hashcode; //!< reserved + const Ch* str; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // implementation detail: ShortString can represent zero-terminated strings up to MaxSize chars + // (excluding the terminating zero) and store a value to determine the length of the contained + // string in the last character str[LenPos] by storing "MaxSize - length" there. If the string + // to store has the maximal length of MaxSize then str[LenPos] will be 0 and therefore act as + // the string terminator as well. For getting the string length back from that value just use + // "MaxSize - str[LenPos]". + // This allows to store 13-chars strings in 32-bit mode, 21-chars strings in 64-bit mode, + // 13-chars strings for RAPIDJSON_48BITPOINTER_OPTIMIZATION=1 inline (for `UTF8`-encoded strings). + struct ShortString { + enum { MaxChars = sizeof(static_cast(0)->payload) / sizeof(Ch), MaxSize = MaxChars - 1, LenPos = MaxSize }; + Ch str[MaxChars]; + + inline static bool Usable(SizeType len) { return (MaxSize >= len); } + inline void SetLength(SizeType len) { str[LenPos] = static_cast(MaxSize - len); } + inline SizeType GetLength() const { return static_cast(MaxSize - str[LenPos]); } + }; // at most as many bytes as "String" above => 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // By using proper binary layout, retrieval of different integer types do not need conversions. + union Number { +#if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN + struct I { + int i; + char padding[4]; + }i; + struct U { + unsigned u; + char padding2[4]; + }u; +#else + struct I { + char padding[4]; + int i; + }i; + struct U { + char padding2[4]; + unsigned u; + }u; +#endif + int64_t i64; + uint64_t u64; + double d; + }; // 8 bytes + + struct ObjectData { + SizeType size; + SizeType capacity; + Member* members; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + struct ArrayData { + SizeType size; + SizeType capacity; + GenericValue* elements; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + union Data { + String s; + ShortString ss; + Number n; + ObjectData o; + ArrayData a; + Flag f; + }; // 16 bytes in 32-bit mode, 24 bytes in 64-bit mode, 16 bytes in 64-bit with RAPIDJSON_48BITPOINTER_OPTIMIZATION + + RAPIDJSON_FORCEINLINE const Ch* GetStringPointer() const { return RAPIDJSON_GETPOINTER(Ch, data_.s.str); } + RAPIDJSON_FORCEINLINE const Ch* SetStringPointer(const Ch* str) { return RAPIDJSON_SETPOINTER(Ch, data_.s.str, str); } + RAPIDJSON_FORCEINLINE GenericValue* GetElementsPointer() const { return RAPIDJSON_GETPOINTER(GenericValue, data_.a.elements); } + RAPIDJSON_FORCEINLINE GenericValue* SetElementsPointer(GenericValue* elements) { return RAPIDJSON_SETPOINTER(GenericValue, data_.a.elements, elements); } + RAPIDJSON_FORCEINLINE Member* GetMembersPointer() const { return RAPIDJSON_GETPOINTER(Member, data_.o.members); } + RAPIDJSON_FORCEINLINE Member* SetMembersPointer(Member* members) { return RAPIDJSON_SETPOINTER(Member, data_.o.members, members); } + + // Initialize this value as array with initial data, without calling destructor. + void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) { + data_.f.flags = kArrayFlag; + if (count) { + GenericValue* e = static_cast(allocator.Malloc(count * sizeof(GenericValue))); + SetElementsPointer(e); + std::memcpy(e, values, count * sizeof(GenericValue)); + } + else + SetElementsPointer(0); + data_.a.size = data_.a.capacity = count; + } + + //! Initialize this value as object with initial data, without calling destructor. + void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) { + data_.f.flags = kObjectFlag; + if (count) { + Member* m = static_cast(allocator.Malloc(count * sizeof(Member))); + SetMembersPointer(m); + std::memcpy(m, members, count * sizeof(Member)); + } + else + SetMembersPointer(0); + data_.o.size = data_.o.capacity = count; + } + + //! Initialize this value as constant string, without calling destructor. + void SetStringRaw(StringRefType s) RAPIDJSON_NOEXCEPT { + data_.f.flags = kConstStringFlag; + SetStringPointer(s); + data_.s.length = s.length; + } + + //! Initialize this value as copy string with initial data, without calling destructor. + void SetStringRaw(StringRefType s, Allocator& allocator) { + Ch* str = 0; + if (ShortString::Usable(s.length)) { + data_.f.flags = kShortStringFlag; + data_.ss.SetLength(s.length); + str = data_.ss.str; + } else { + data_.f.flags = kCopyStringFlag; + data_.s.length = s.length; + str = static_cast(allocator.Malloc((s.length + 1) * sizeof(Ch))); + SetStringPointer(str); + } + std::memcpy(str, s, s.length * sizeof(Ch)); + str[s.length] = '\0'; + } + + //! Assignment without calling destructor + void RawAssign(GenericValue& rhs) RAPIDJSON_NOEXCEPT { + data_ = rhs.data_; + // data_.f.flags = rhs.data_.f.flags; + rhs.data_.f.flags = kNullFlag; + } + + template + bool StringEqual(const GenericValue& rhs) const { + RAPIDJSON_ASSERT(IsString()); + RAPIDJSON_ASSERT(rhs.IsString()); + + const SizeType len1 = GetStringLength(); + const SizeType len2 = rhs.GetStringLength(); + if(len1 != len2) { return false; } + + const Ch* const str1 = GetString(); + const Ch* const str2 = rhs.GetString(); + if(str1 == str2) { return true; } // fast path for constant string + + return (std::memcmp(str1, str2, sizeof(Ch) * len1) == 0); + } + + Data data_; +}; + +//! GenericValue with UTF8 encoding +typedef GenericValue > Value; + +/////////////////////////////////////////////////////////////////////////////// +// GenericDocument + +//! A document for parsing JSON text as DOM. +/*! + \note implements Handler concept + \tparam Encoding Encoding for both parsing and string storage. + \tparam Allocator Allocator for allocating memory for the DOM + \tparam StackAllocator Allocator for allocating memory for stack during parsing. + \warning Although GenericDocument inherits from GenericValue, the API does \b not provide any virtual functions, especially no virtual destructor. To avoid memory leaks, do not \c delete a GenericDocument object via a pointer to a GenericValue. +*/ +template , typename StackAllocator = CrtAllocator> +class GenericDocument : public GenericValue { +public: + typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. + typedef GenericValue ValueType; //!< Value type of the document. + typedef Allocator AllocatorType; //!< Allocator type from template parameter. + + //! Constructor + /*! Creates an empty document of specified type. + \param type Mandatory type of object to create. + \param allocator Optional allocator for allocating memory. + \param stackCapacity Optional initial capacity of stack in bytes. + \param stackAllocator Optional allocator for allocating memory for stack. + */ + explicit GenericDocument(Type type, Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) : + GenericValue(type), allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + } + + //! Constructor + /*! Creates an empty document which type is Null. + \param allocator Optional allocator for allocating memory. + \param stackCapacity Optional initial capacity of stack in bytes. + \param stackAllocator Optional allocator for allocating memory for stack. + */ + GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) : + allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericDocument(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT + : ValueType(std::forward(rhs)), // explicit cast to avoid prohibited move from Document + allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + stack_(std::move(rhs.stack_)), + parseResult_(rhs.parseResult_) + { + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.parseResult_ = ParseResult(); + } +#endif + + ~GenericDocument() { + Destroy(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move assignment in C++11 + GenericDocument& operator=(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT + { + // The cast to ValueType is necessary here, because otherwise it would + // attempt to call GenericValue's templated assignment operator. + ValueType::operator=(std::forward(rhs)); + + // Calling the destructor here would prematurely call stack_'s destructor + Destroy(); + + allocator_ = rhs.allocator_; + ownAllocator_ = rhs.ownAllocator_; + stack_ = std::move(rhs.stack_); + parseResult_ = rhs.parseResult_; + + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.parseResult_ = ParseResult(); + + return *this; + } +#endif + + //! Exchange the contents of this document with those of another. + /*! + \param rhs Another document. + \note Constant complexity. + \see GenericValue::Swap + */ + GenericDocument& Swap(GenericDocument& rhs) RAPIDJSON_NOEXCEPT { + ValueType::Swap(rhs); + stack_.Swap(rhs.stack_); + internal::Swap(allocator_, rhs.allocator_); + internal::Swap(ownAllocator_, rhs.ownAllocator_); + internal::Swap(parseResult_, rhs.parseResult_); + return *this; + } + + //! free-standing swap function helper + /*! + Helper function to enable support for common swap implementation pattern based on \c std::swap: + \code + void swap(MyClass& a, MyClass& b) { + using std::swap; + swap(a.doc, b.doc); + // ... + } + \endcode + \see Swap() + */ + friend inline void swap(GenericDocument& a, GenericDocument& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + + //! Populate this document by a generator which produces SAX events. + /*! \tparam Generator A functor with bool f(Handler) prototype. + \param g Generator functor which sends SAX events to the parameter. + \return The document itself for fluent API. + */ + template + GenericDocument& Populate(Generator& g) { + ClearStackOnExit scope(*this); + if (g(*this)) { + RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object + ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document + } + return *this; + } + + //!@name Parse from stream + //!@{ + + //! Parse JSON text from an input stream (with Encoding conversion) + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam SourceEncoding Encoding of input stream + \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + GenericReader reader( + stack_.HasAllocator() ? &stack_.GetAllocator() : 0); + ClearStackOnExit scope(*this); + parseResult_ = reader.template Parse(is, *this); + if (parseResult_) { + RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object + ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document + } + return *this; + } + + //! Parse JSON text from an input stream + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + + //! Parse JSON text from an input stream (with \ref kParseDefaultFlags) + /*! \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + //!@} + + //!@name Parse in-place from mutable string + //!@{ + + //! Parse JSON text from a mutable string + /*! \tparam parseFlags Combination of \ref ParseFlag. + \param str Mutable zero-terminated string to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseInsitu(Ch* str) { + GenericInsituStringStream s(str); + return ParseStream(s); + } + + //! Parse JSON text from a mutable string (with \ref kParseDefaultFlags) + /*! \param str Mutable zero-terminated string to be parsed. + \return The document itself for fluent API. + */ + GenericDocument& ParseInsitu(Ch* str) { + return ParseInsitu(str); + } + //!@} + + //!@name Parse from read-only string + //!@{ + + //! Parse JSON text from a read-only string (with Encoding conversion) + /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). + \tparam SourceEncoding Transcoding from input Encoding + \param str Read-only zero-terminated string to be parsed. + */ + template + GenericDocument& Parse(const typename SourceEncoding::Ch* str) { + RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); + GenericStringStream s(str); + return ParseStream(s); + } + + //! Parse JSON text from a read-only string + /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). + \param str Read-only zero-terminated string to be parsed. + */ + template + GenericDocument& Parse(const Ch* str) { + return Parse(str); + } + + //! Parse JSON text from a read-only string (with \ref kParseDefaultFlags) + /*! \param str Read-only zero-terminated string to be parsed. + */ + GenericDocument& Parse(const Ch* str) { + return Parse(str); + } + + template + GenericDocument& Parse(const typename SourceEncoding::Ch* str, size_t length) { + RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); + MemoryStream ms(static_cast(str), length * sizeof(typename SourceEncoding::Ch)); + EncodedInputStream is(ms); + ParseStream(is); + return *this; + } + + template + GenericDocument& Parse(const Ch* str, size_t length) { + return Parse(str, length); + } + + GenericDocument& Parse(const Ch* str, size_t length) { + return Parse(str, length); + } + +#if RAPIDJSON_HAS_STDSTRING + template + GenericDocument& Parse(const std::basic_string& str) { + // c_str() is constant complexity according to standard. Should be faster than Parse(const char*, size_t) + return Parse(str.c_str()); + } + + template + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str.c_str()); + } + + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str); + } +#endif // RAPIDJSON_HAS_STDSTRING + + //!@} + + //!@name Handling parse errors + //!@{ + + //! Whether a parse error has occured in the last parsing. + bool HasParseError() const { return parseResult_.IsError(); } + + //! Get the \ref ParseErrorCode of last parsing. + ParseErrorCode GetParseError() const { return parseResult_.Code(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorOffset() const { return parseResult_.Offset(); } + + //! Implicit conversion to get the last parse result +#ifndef __clang // -Wdocumentation + /*! \return \ref ParseResult of the last parse operation + + \code + Document doc; + ParseResult ok = doc.Parse(json); + if (!ok) + printf( "JSON parse error: %s (%u)\n", GetParseError_En(ok.Code()), ok.Offset()); + \endcode + */ +#endif + operator ParseResult() const { return parseResult_; } + //!@} + + //! Get the allocator of this document. + Allocator& GetAllocator() { + RAPIDJSON_ASSERT(allocator_); + return *allocator_; + } + + //! Get the capacity of stack in bytes. + size_t GetStackCapacity() const { return stack_.GetCapacity(); } + +private: + // clear stack on any exit from ParseStream, e.g. due to exception + struct ClearStackOnExit { + explicit ClearStackOnExit(GenericDocument& d) : d_(d) {} + ~ClearStackOnExit() { d_.ClearStack(); } + private: + ClearStackOnExit(const ClearStackOnExit&); + ClearStackOnExit& operator=(const ClearStackOnExit&); + GenericDocument& d_; + }; + + // callers of the following private Handler functions + // template friend class GenericReader; // for parsing + template friend class GenericValue; // for deep copying + +public: + // Implementation of Handler + bool Null() { new (stack_.template Push()) ValueType(); return true; } + bool Bool(bool b) { new (stack_.template Push()) ValueType(b); return true; } + bool Int(int i) { new (stack_.template Push()) ValueType(i); return true; } + bool Uint(unsigned i) { new (stack_.template Push()) ValueType(i); return true; } + bool Int64(int64_t i) { new (stack_.template Push()) ValueType(i); return true; } + bool Uint64(uint64_t i) { new (stack_.template Push()) ValueType(i); return true; } + bool Double(double d) { new (stack_.template Push()) ValueType(d); return true; } + + bool RawNumber(const Ch* str, SizeType length, bool copy) { + if (copy) + new (stack_.template Push()) ValueType(str, length, GetAllocator()); + else + new (stack_.template Push()) ValueType(str, length); + return true; + } + + bool String(const Ch* str, SizeType length, bool copy) { + if (copy) + new (stack_.template Push()) ValueType(str, length, GetAllocator()); + else + new (stack_.template Push()) ValueType(str, length); + return true; + } + + bool StartObject() { new (stack_.template Push()) ValueType(kObjectType); return true; } + + bool Key(const Ch* str, SizeType length, bool copy) { return String(str, length, copy); } + + bool EndObject(SizeType memberCount) { + typename ValueType::Member* members = stack_.template Pop(memberCount); + stack_.template Top()->SetObjectRaw(members, memberCount, GetAllocator()); + return true; + } + + bool StartArray() { new (stack_.template Push()) ValueType(kArrayType); return true; } + + bool EndArray(SizeType elementCount) { + ValueType* elements = stack_.template Pop(elementCount); + stack_.template Top()->SetArrayRaw(elements, elementCount, GetAllocator()); + return true; + } + +private: + //! Prohibit copying + GenericDocument(const GenericDocument&); + //! Prohibit assignment + GenericDocument& operator=(const GenericDocument&); + + void ClearStack() { + if (Allocator::kNeedFree) + while (stack_.GetSize() > 0) // Here assumes all elements in stack array are GenericValue (Member is actually 2 GenericValue objects) + (stack_.template Pop(1))->~ValueType(); + else + stack_.Clear(); + stack_.ShrinkToFit(); + } + + void Destroy() { + RAPIDJSON_DELETE(ownAllocator_); + } + + static const size_t kDefaultStackCapacity = 1024; + Allocator* allocator_; + Allocator* ownAllocator_; + internal::Stack stack_; + ParseResult parseResult_; +}; + +//! GenericDocument with UTF8 encoding +typedef GenericDocument > Document; + +// defined here due to the dependency on GenericDocument +template +template +inline +GenericValue::GenericValue(const GenericValue& rhs, Allocator& allocator) +{ + switch (rhs.GetType()) { + case kObjectType: + case kArrayType: { // perform deep copy via SAX Handler + GenericDocument d(&allocator); + rhs.Accept(d); + RawAssign(*d.stack_.template Pop(1)); + } + break; + case kStringType: + if (rhs.data_.f.flags == kConstStringFlag) { + data_.f.flags = rhs.data_.f.flags; + data_ = *reinterpret_cast(&rhs.data_); + } else { + SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator); + } + break; + default: + data_.f.flags = rhs.data_.f.flags; + data_ = *reinterpret_cast(&rhs.data_); + break; + } +} + +//! Helper class for accessing Value of array type. +/*! + Instance of this helper class is obtained by \c GenericValue::GetArray(). + In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1. +*/ +template +class GenericArray { +public: + typedef GenericArray ConstArray; + typedef GenericArray Array; + typedef ValueT PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef ValueType* ValueIterator; // This may be const or non-const iterator + typedef const ValueT* ConstValueIterator; + typedef typename ValueType::AllocatorType AllocatorType; + typedef typename ValueType::StringRefType StringRefType; + + template + friend class GenericValue; + + GenericArray(const GenericArray& rhs) : value_(rhs.value_) {} + GenericArray& operator=(const GenericArray& rhs) { value_ = rhs.value_; return *this; } + ~GenericArray() {} + + SizeType Size() const { return value_.Size(); } + SizeType Capacity() const { return value_.Capacity(); } + bool Empty() const { return value_.Empty(); } + void Clear() const { value_.Clear(); } + ValueType& operator[](SizeType index) const { return value_[index]; } + ValueIterator Begin() const { return value_.Begin(); } + ValueIterator End() const { return value_.End(); } + GenericArray Reserve(SizeType newCapacity, AllocatorType &allocator) const { value_.Reserve(newCapacity, allocator); return *this; } + GenericArray PushBack(ValueType& value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericArray PushBack(ValueType&& value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericArray PushBack(StringRefType value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (const GenericArray&)) PushBack(T value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } + GenericArray PopBack() const { value_.PopBack(); return *this; } + ValueIterator Erase(ConstValueIterator pos) const { return value_.Erase(pos); } + ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) const { return value_.Erase(first, last); } + +#if RAPIDJSON_HAS_CXX11_RANGE_FOR + ValueIterator begin() const { return value_.Begin(); } + ValueIterator end() const { return value_.End(); } +#endif + +private: + GenericArray(); + GenericArray(ValueType& value) : value_(value) {} + ValueType& value_; +}; + +//! Helper class for accessing Value of object type. +/*! + Instance of this helper class is obtained by \c GenericValue::GetObject(). + In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1. +*/ +template +class GenericObject { +public: + typedef GenericObject ConstObject; + typedef GenericObject Object; + typedef ValueT PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef GenericMemberIterator MemberIterator; // This may be const or non-const iterator + typedef GenericMemberIterator ConstMemberIterator; + typedef typename ValueType::AllocatorType AllocatorType; + typedef typename ValueType::StringRefType StringRefType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename ValueType::Ch Ch; + + template + friend class GenericValue; + + GenericObject(const GenericObject& rhs) : value_(rhs.value_) {} + GenericObject& operator=(const GenericObject& rhs) { value_ = rhs.value_; return *this; } + ~GenericObject() {} + + SizeType MemberCount() const { return value_.MemberCount(); } + bool ObjectEmpty() const { return value_.ObjectEmpty(); } + template ValueType& operator[](T* name) const { return value_[name]; } + template ValueType& operator[](const GenericValue& name) const { return value_[name]; } +#if RAPIDJSON_HAS_STDSTRING + ValueType& operator[](const std::basic_string& name) const { return value_[name]; } +#endif + MemberIterator MemberBegin() const { return value_.MemberBegin(); } + MemberIterator MemberEnd() const { return value_.MemberEnd(); } + bool HasMember(const Ch* name) const { return value_.HasMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool HasMember(const std::basic_string& name) const { return value_.HasMember(name); } +#endif + template bool HasMember(const GenericValue& name) const { return value_.HasMember(name); } + MemberIterator FindMember(const Ch* name) const { return value_.FindMember(name); } + template MemberIterator FindMember(const GenericValue& name) const { return value_.FindMember(name); } +#if RAPIDJSON_HAS_STDSTRING + MemberIterator FindMember(const std::basic_string& name) const { return value_.FindMember(name); } +#endif + GenericObject AddMember(ValueType& name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType& name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#if RAPIDJSON_HAS_STDSTRING + GenericObject AddMember(ValueType& name, std::basic_string& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#endif + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) AddMember(ValueType& name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericObject AddMember(ValueType&& name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType&& name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType& name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(StringRefType name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericObject AddMember(StringRefType name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(StringRefType name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericObject)) AddMember(StringRefType name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + void RemoveAllMembers() { return value_.RemoveAllMembers(); } + bool RemoveMember(const Ch* name) const { return value_.RemoveMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool RemoveMember(const std::basic_string& name) const { return value_.RemoveMember(name); } +#endif + template bool RemoveMember(const GenericValue& name) const { return value_.RemoveMember(name); } + MemberIterator RemoveMember(MemberIterator m) const { return value_.RemoveMember(m); } + MemberIterator EraseMember(ConstMemberIterator pos) const { return value_.EraseMember(pos); } + MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) const { return value_.EraseMember(first, last); } + bool EraseMember(const Ch* name) const { return value_.EraseMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool EraseMember(const std::basic_string& name) const { return EraseMember(ValueType(StringRef(name))); } +#endif + template bool EraseMember(const GenericValue& name) const { return value_.EraseMember(name); } + +#if RAPIDJSON_HAS_CXX11_RANGE_FOR + MemberIterator begin() const { return value_.MemberBegin(); } + MemberIterator end() const { return value_.MemberEnd(); } +#endif + +private: + GenericObject(); + GenericObject(ValueType& value) : value_(value) {} + ValueType& value_; +}; + +RAPIDJSON_NAMESPACE_END +RAPIDJSON_DIAG_POP + +#endif // RAPIDJSON_DOCUMENT_H_ diff --git a/src/3rdparty/rapidjson/encodedstream.h b/src/3rdparty/rapidjson/encodedstream.h new file mode 100644 index 00000000..14506838 --- /dev/null +++ b/src/3rdparty/rapidjson/encodedstream.h @@ -0,0 +1,299 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ENCODEDSTREAM_H_ +#define RAPIDJSON_ENCODEDSTREAM_H_ + +#include "stream.h" +#include "memorystream.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Input byte stream wrapper with a statically bound encoding. +/*! + \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. + \tparam InputByteStream Type of input byte stream. For example, FileReadStream. +*/ +template +class EncodedInputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); +public: + typedef typename Encoding::Ch Ch; + + EncodedInputStream(InputByteStream& is) : is_(is) { + current_ = Encoding::TakeBOM(is_); + } + + Ch Peek() const { return current_; } + Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; } + size_t Tell() const { return is_.Tell(); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + EncodedInputStream(const EncodedInputStream&); + EncodedInputStream& operator=(const EncodedInputStream&); + + InputByteStream& is_; + Ch current_; +}; + +//! Specialized for UTF8 MemoryStream. +template <> +class EncodedInputStream, MemoryStream> { +public: + typedef UTF8<>::Ch Ch; + + EncodedInputStream(MemoryStream& is) : is_(is) { + if (static_cast(is_.Peek()) == 0xEFu) is_.Take(); + if (static_cast(is_.Peek()) == 0xBBu) is_.Take(); + if (static_cast(is_.Peek()) == 0xBFu) is_.Take(); + } + Ch Peek() const { return is_.Peek(); } + Ch Take() { return is_.Take(); } + size_t Tell() const { return is_.Tell(); } + + // Not implemented + void Put(Ch) {} + void Flush() {} + Ch* PutBegin() { return 0; } + size_t PutEnd(Ch*) { return 0; } + + MemoryStream& is_; + +private: + EncodedInputStream(const EncodedInputStream&); + EncodedInputStream& operator=(const EncodedInputStream&); +}; + +//! Output byte stream wrapper with statically bound encoding. +/*! + \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. + \tparam OutputByteStream Type of input byte stream. For example, FileWriteStream. +*/ +template +class EncodedOutputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); +public: + typedef typename Encoding::Ch Ch; + + EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) { + if (putBOM) + Encoding::PutBOM(os_); + } + + void Put(Ch c) { Encoding::Put(os_, c); } + void Flush() { os_.Flush(); } + + // Not implemented + Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;} + Ch Take() { RAPIDJSON_ASSERT(false); return 0;} + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + EncodedOutputStream(const EncodedOutputStream&); + EncodedOutputStream& operator=(const EncodedOutputStream&); + + OutputByteStream& os_; +}; + +#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x + +//! Input stream wrapper with dynamically bound encoding and automatic encoding detection. +/*! + \tparam CharType Type of character for reading. + \tparam InputByteStream type of input byte stream to be wrapped. +*/ +template +class AutoUTFInputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); +public: + typedef CharType Ch; + + //! Constructor. + /*! + \param is input stream to be wrapped. + \param type UTF encoding type if it is not detected from the stream. + */ + AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + DetectType(); + static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) }; + takeFunc_ = f[type_]; + current_ = takeFunc_(*is_); + } + + UTFType GetType() const { return type_; } + bool HasBOM() const { return hasBOM_; } + + Ch Peek() const { return current_; } + Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; } + size_t Tell() const { return is_->Tell(); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + AutoUTFInputStream(const AutoUTFInputStream&); + AutoUTFInputStream& operator=(const AutoUTFInputStream&); + + // Detect encoding type with BOM or RFC 4627 + void DetectType() { + // BOM (Byte Order Mark): + // 00 00 FE FF UTF-32BE + // FF FE 00 00 UTF-32LE + // FE FF UTF-16BE + // FF FE UTF-16LE + // EF BB BF UTF-8 + + const unsigned char* c = reinterpret_cast(is_->Peek4()); + if (!c) + return; + + unsigned bom = static_cast(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24)); + hasBOM_ = false; + if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } + else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } + else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); } + else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); } + else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); } + + // RFC 4627: Section 3 + // "Since the first two characters of a JSON text will always be ASCII + // characters [RFC0020], it is possible to determine whether an octet + // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking + // at the pattern of nulls in the first four octets." + // 00 00 00 xx UTF-32BE + // 00 xx 00 xx UTF-16BE + // xx 00 00 00 UTF-32LE + // xx 00 xx 00 UTF-16LE + // xx xx xx xx UTF-8 + + if (!hasBOM_) { + unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); + switch (pattern) { + case 0x08: type_ = kUTF32BE; break; + case 0x0A: type_ = kUTF16BE; break; + case 0x01: type_ = kUTF32LE; break; + case 0x05: type_ = kUTF16LE; break; + case 0x0F: type_ = kUTF8; break; + default: break; // Use type defined by user. + } + } + + // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. + if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); + if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); + } + + typedef Ch (*TakeFunc)(InputByteStream& is); + InputByteStream* is_; + UTFType type_; + Ch current_; + TakeFunc takeFunc_; + bool hasBOM_; +}; + +//! Output stream wrapper with dynamically bound encoding and automatic encoding detection. +/*! + \tparam CharType Type of character for writing. + \tparam OutputByteStream type of output byte stream to be wrapped. +*/ +template +class AutoUTFOutputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); +public: + typedef CharType Ch; + + //! Constructor. + /*! + \param os output stream to be wrapped. + \param type UTF encoding type. + \param putBOM Whether to write BOM at the beginning of the stream. + */ + AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + + // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. + if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); + if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); + + static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) }; + putFunc_ = f[type_]; + + if (putBOM) + PutBOM(); + } + + UTFType GetType() const { return type_; } + + void Put(Ch c) { putFunc_(*os_, c); } + void Flush() { os_->Flush(); } + + // Not implemented + Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;} + Ch Take() { RAPIDJSON_ASSERT(false); return 0;} + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + AutoUTFOutputStream(const AutoUTFOutputStream&); + AutoUTFOutputStream& operator=(const AutoUTFOutputStream&); + + void PutBOM() { + typedef void (*PutBOMFunc)(OutputByteStream&); + static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) }; + f[type_](*os_); + } + + typedef void (*PutFunc)(OutputByteStream&, Ch); + + OutputByteStream* os_; + UTFType type_; + PutFunc putFunc_; +}; + +#undef RAPIDJSON_ENCODINGS_FUNC + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/src/3rdparty/rapidjson/encodings.h b/src/3rdparty/rapidjson/encodings.h new file mode 100644 index 00000000..baa7c2b1 --- /dev/null +++ b/src/3rdparty/rapidjson/encodings.h @@ -0,0 +1,716 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ENCODINGS_H_ +#define RAPIDJSON_ENCODINGS_H_ + +#include "rapidjson.h" + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4244) // conversion from 'type1' to 'type2', possible loss of data +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#elif defined(__GNUC__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +RAPIDJSON_DIAG_OFF(overflow) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Encoding + +/*! \class rapidjson::Encoding + \brief Concept for encoding of Unicode characters. + +\code +concept Encoding { + typename Ch; //! Type of character. A "character" is actually a code unit in unicode's definition. + + enum { supportUnicode = 1 }; // or 0 if not supporting unicode + + //! \brief Encode a Unicode codepoint to an output stream. + //! \param os Output stream. + //! \param codepoint An unicode codepoint, ranging from 0x0 to 0x10FFFF inclusively. + template + static void Encode(OutputStream& os, unsigned codepoint); + + //! \brief Decode a Unicode codepoint from an input stream. + //! \param is Input stream. + //! \param codepoint Output of the unicode codepoint. + //! \return true if a valid codepoint can be decoded from the stream. + template + static bool Decode(InputStream& is, unsigned* codepoint); + + //! \brief Validate one Unicode codepoint from an encoded stream. + //! \param is Input stream to obtain codepoint. + //! \param os Output for copying one codepoint. + //! \return true if it is valid. + //! \note This function just validating and copying the codepoint without actually decode it. + template + static bool Validate(InputStream& is, OutputStream& os); + + // The following functions are deal with byte streams. + + //! Take a character from input byte stream, skip BOM if exist. + template + static CharType TakeBOM(InputByteStream& is); + + //! Take a character from input byte stream. + template + static Ch Take(InputByteStream& is); + + //! Put BOM to output byte stream. + template + static void PutBOM(OutputByteStream& os); + + //! Put a character to output byte stream. + template + static void Put(OutputByteStream& os, Ch c); +}; +\endcode +*/ + +/////////////////////////////////////////////////////////////////////////////// +// UTF8 + +//! UTF-8 encoding. +/*! http://en.wikipedia.org/wiki/UTF-8 + http://tools.ietf.org/html/rfc3629 + \tparam CharType Code unit for storing 8-bit UTF-8 data. Default is char. + \note implements Encoding concept +*/ +template +struct UTF8 { + typedef CharType Ch; + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + if (codepoint <= 0x7F) + os.Put(static_cast(codepoint & 0xFF)); + else if (codepoint <= 0x7FF) { + os.Put(static_cast(0xC0 | ((codepoint >> 6) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint & 0x3F)))); + } + else if (codepoint <= 0xFFFF) { + os.Put(static_cast(0xE0 | ((codepoint >> 12) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + os.Put(static_cast(0x80 | (codepoint & 0x3F))); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + os.Put(static_cast(0xF0 | ((codepoint >> 18) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint >> 12) & 0x3F))); + os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + os.Put(static_cast(0x80 | (codepoint & 0x3F))); + } + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + if (codepoint <= 0x7F) + PutUnsafe(os, static_cast(codepoint & 0xFF)); + else if (codepoint <= 0x7FF) { + PutUnsafe(os, static_cast(0xC0 | ((codepoint >> 6) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint & 0x3F)))); + } + else if (codepoint <= 0xFFFF) { + PutUnsafe(os, static_cast(0xE0 | ((codepoint >> 12) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | (codepoint & 0x3F))); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + PutUnsafe(os, static_cast(0xF0 | ((codepoint >> 18) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 12) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | (codepoint & 0x3F))); + } + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { +#define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | (static_cast(c) & 0x3Fu) +#define TRANS(mask) result &= ((GetRange(static_cast(c)) & mask) != 0) +#define TAIL() COPY(); TRANS(0x70) + typename InputStream::Ch c = is.Take(); + if (!(c & 0x80)) { + *codepoint = static_cast(c); + return true; + } + + unsigned char type = GetRange(static_cast(c)); + if (type >= 32) { + *codepoint = 0; + } else { + *codepoint = (0xFF >> type) & static_cast(c); + } + bool result = true; + switch (type) { + case 2: TAIL(); return result; + case 3: TAIL(); TAIL(); return result; + case 4: COPY(); TRANS(0x50); TAIL(); return result; + case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; + case 6: TAIL(); TAIL(); TAIL(); return result; + case 10: COPY(); TRANS(0x20); TAIL(); return result; + case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; + default: return false; + } +#undef COPY +#undef TRANS +#undef TAIL + } + + template + static bool Validate(InputStream& is, OutputStream& os) { +#define COPY() os.Put(c = is.Take()) +#define TRANS(mask) result &= ((GetRange(static_cast(c)) & mask) != 0) +#define TAIL() COPY(); TRANS(0x70) + Ch c; + COPY(); + if (!(c & 0x80)) + return true; + + bool result = true; + switch (GetRange(static_cast(c))) { + case 2: TAIL(); return result; + case 3: TAIL(); TAIL(); return result; + case 4: COPY(); TRANS(0x50); TAIL(); return result; + case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; + case 6: TAIL(); TAIL(); TAIL(); return result; + case 10: COPY(); TRANS(0x20); TAIL(); return result; + case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; + default: return false; + } +#undef COPY +#undef TRANS +#undef TAIL + } + + static unsigned char GetRange(unsigned char c) { + // Referring to DFA of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ + // With new mapping 1 -> 0x10, 7 -> 0x20, 9 -> 0x40, such that AND operation can test multiple types. + static const unsigned char type[] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, + 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, + 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, + }; + return type[c]; + } + + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + typename InputByteStream::Ch c = Take(is); + if (static_cast(c) != 0xEFu) return c; + c = is.Take(); + if (static_cast(c) != 0xBBu) return c; + c = is.Take(); + if (static_cast(c) != 0xBFu) return c; + c = is.Take(); + return c; + } + + template + static Ch Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + return static_cast(is.Take()); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xEFu)); + os.Put(static_cast(0xBBu)); + os.Put(static_cast(0xBFu)); + } + + template + static void Put(OutputByteStream& os, Ch c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// UTF16 + +//! UTF-16 encoding. +/*! http://en.wikipedia.org/wiki/UTF-16 + http://tools.ietf.org/html/rfc2781 + \tparam CharType Type for storing 16-bit UTF-16 data. Default is wchar_t. C++11 may use char16_t instead. + \note implements Encoding concept + + \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. + For streaming, use UTF16LE and UTF16BE, which handle endianness. +*/ +template +struct UTF16 { + typedef CharType Ch; + RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 2); + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + if (codepoint <= 0xFFFF) { + RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair + os.Put(static_cast(codepoint)); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + unsigned v = codepoint - 0x10000; + os.Put(static_cast((v >> 10) | 0xD800)); + os.Put((v & 0x3FF) | 0xDC00); + } + } + + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + if (codepoint <= 0xFFFF) { + RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair + PutUnsafe(os, static_cast(codepoint)); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + unsigned v = codepoint - 0x10000; + PutUnsafe(os, static_cast((v >> 10) | 0xD800)); + PutUnsafe(os, (v & 0x3FF) | 0xDC00); + } + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); + typename InputStream::Ch c = is.Take(); + if (c < 0xD800 || c > 0xDFFF) { + *codepoint = static_cast(c); + return true; + } + else if (c <= 0xDBFF) { + *codepoint = (static_cast(c) & 0x3FF) << 10; + c = is.Take(); + *codepoint |= (static_cast(c) & 0x3FF); + *codepoint += 0x10000; + return c >= 0xDC00 && c <= 0xDFFF; + } + return false; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + typename InputStream::Ch c; + os.Put(static_cast(c = is.Take())); + if (c < 0xD800 || c > 0xDFFF) + return true; + else if (c <= 0xDBFF) { + os.Put(c = is.Take()); + return c >= 0xDC00 && c <= 0xDFFF; + } + return false; + } +}; + +//! UTF-16 little endian encoding. +template +struct UTF16LE : UTF16 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0xFEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(is.Take()); + c |= static_cast(static_cast(is.Take())) << 8; + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFFu)); + os.Put(static_cast(0xFEu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(static_cast(c) & 0xFFu)); + os.Put(static_cast((static_cast(c) >> 8) & 0xFFu)); + } +}; + +//! UTF-16 big endian encoding. +template +struct UTF16BE : UTF16 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0xFEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(static_cast(is.Take())) << 8; + c |= static_cast(is.Take()); + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0xFFu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast((static_cast(c) >> 8) & 0xFFu)); + os.Put(static_cast(static_cast(c) & 0xFFu)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// UTF32 + +//! UTF-32 encoding. +/*! http://en.wikipedia.org/wiki/UTF-32 + \tparam CharType Type for storing 32-bit UTF-32 data. Default is unsigned. C++11 may use char32_t instead. + \note implements Encoding concept + + \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. + For streaming, use UTF32LE and UTF32BE, which handle endianness. +*/ +template +struct UTF32 { + typedef CharType Ch; + RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 4); + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + os.Put(codepoint); + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + PutUnsafe(os, codepoint); + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); + Ch c = is.Take(); + *codepoint = c; + return c <= 0x10FFFF; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); + Ch c; + os.Put(c = is.Take()); + return c <= 0x10FFFF; + } +}; + +//! UTF-32 little endian enocoding. +template +struct UTF32LE : UTF32 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0x0000FEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(is.Take()); + c |= static_cast(static_cast(is.Take())) << 8; + c |= static_cast(static_cast(is.Take())) << 16; + c |= static_cast(static_cast(is.Take())) << 24; + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFFu)); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0x00u)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c & 0xFFu)); + os.Put(static_cast((c >> 8) & 0xFFu)); + os.Put(static_cast((c >> 16) & 0xFFu)); + os.Put(static_cast((c >> 24) & 0xFFu)); + } +}; + +//! UTF-32 big endian encoding. +template +struct UTF32BE : UTF32 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0x0000FEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(static_cast(is.Take())) << 24; + c |= static_cast(static_cast(is.Take())) << 16; + c |= static_cast(static_cast(is.Take())) << 8; + c |= static_cast(static_cast(is.Take())); + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0xFFu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast((c >> 24) & 0xFFu)); + os.Put(static_cast((c >> 16) & 0xFFu)); + os.Put(static_cast((c >> 8) & 0xFFu)); + os.Put(static_cast(c & 0xFFu)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// ASCII + +//! ASCII encoding. +/*! http://en.wikipedia.org/wiki/ASCII + \tparam CharType Code unit for storing 7-bit ASCII data. Default is char. + \note implements Encoding concept +*/ +template +struct ASCII { + typedef CharType Ch; + + enum { supportUnicode = 0 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_ASSERT(codepoint <= 0x7F); + os.Put(static_cast(codepoint & 0xFF)); + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_ASSERT(codepoint <= 0x7F); + PutUnsafe(os, static_cast(codepoint & 0xFF)); + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + uint8_t c = static_cast(is.Take()); + *codepoint = c; + return c <= 0X7F; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + uint8_t c = static_cast(is.Take()); + os.Put(static_cast(c)); + return c <= 0x7F; + } + + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + uint8_t c = static_cast(Take(is)); + return static_cast(c); + } + + template + static Ch Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + return static_cast(is.Take()); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + (void)os; + } + + template + static void Put(OutputByteStream& os, Ch c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// AutoUTF + +//! Runtime-specified UTF encoding type of a stream. +enum UTFType { + kUTF8 = 0, //!< UTF-8. + kUTF16LE = 1, //!< UTF-16 little endian. + kUTF16BE = 2, //!< UTF-16 big endian. + kUTF32LE = 3, //!< UTF-32 little endian. + kUTF32BE = 4 //!< UTF-32 big endian. +}; + +//! Dynamically select encoding according to stream's runtime-specified UTF encoding type. +/*! \note This class can be used with AutoUTFInputtStream and AutoUTFOutputStream, which provides GetType(). +*/ +template +struct AutoUTF { + typedef CharType Ch; + + enum { supportUnicode = 1 }; + +#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x + + template + RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoint) { + typedef void (*EncodeFunc)(OutputStream&, unsigned); + static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) }; + (*f[os.GetType()])(os, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + typedef void (*EncodeFunc)(OutputStream&, unsigned); + static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(EncodeUnsafe) }; + (*f[os.GetType()])(os, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) { + typedef bool (*DecodeFunc)(InputStream&, unsigned*); + static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) }; + return (*f[is.GetType()])(is, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + typedef bool (*ValidateFunc)(InputStream&, OutputStream&); + static const ValidateFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Validate) }; + return (*f[is.GetType()])(is, os); + } + +#undef RAPIDJSON_ENCODINGS_FUNC +}; + +/////////////////////////////////////////////////////////////////////////////// +// Transcoder + +//! Encoding conversion. +template +struct Transcoder { + //! Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the output stream. + template + RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { + unsigned codepoint; + if (!SourceEncoding::Decode(is, &codepoint)) + return false; + TargetEncoding::Encode(os, codepoint); + return true; + } + + template + RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) { + unsigned codepoint; + if (!SourceEncoding::Decode(is, &codepoint)) + return false; + TargetEncoding::EncodeUnsafe(os, codepoint); + return true; + } + + //! Validate one Unicode codepoint from an encoded stream. + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + return Transcode(is, os); // Since source/target encoding is different, must transcode. + } +}; + +// Forward declaration. +template +inline void PutUnsafe(Stream& stream, typename Stream::Ch c); + +//! Specialization of Transcoder with same source and target encoding. +template +struct Transcoder { + template + RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { + os.Put(is.Take()); // Just copy one code unit. This semantic is different from primary template class. + return true; + } + + template + RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) { + PutUnsafe(os, is.Take()); // Just copy one code unit. This semantic is different from primary template class. + return true; + } + + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + return Encoding::Validate(is, os); // source/target encoding are the same + } +}; + +RAPIDJSON_NAMESPACE_END + +#if defined(__GNUC__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ENCODINGS_H_ diff --git a/src/3rdparty/rapidjson/error/en.h b/src/3rdparty/rapidjson/error/en.h new file mode 100644 index 00000000..2db838bf --- /dev/null +++ b/src/3rdparty/rapidjson/error/en.h @@ -0,0 +1,74 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ERROR_EN_H_ +#define RAPIDJSON_ERROR_EN_H_ + +#include "error.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(covered-switch-default) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Maps error code of parsing into error message. +/*! + \ingroup RAPIDJSON_ERRORS + \param parseErrorCode Error code obtained in parsing. + \return the error message. + \note User can make a copy of this function for localization. + Using switch-case is safer for future modification of error codes. +*/ +inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErrorCode) { + switch (parseErrorCode) { + case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error."); + + case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty."); + case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not be followed by other values."); + + case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value."); + + case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member."); + case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member."); + case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member."); + + case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or ']' after an array element."); + + case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string."); + case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid."); + case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string."); + case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string."); + case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoding in string."); + + case kParseErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to be stored in double."); + case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number."); + case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number."); + + case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error."); + case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error."); + + default: return RAPIDJSON_ERROR_STRING("Unknown error."); + } +} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ERROR_EN_H_ diff --git a/src/3rdparty/rapidjson/error/error.h b/src/3rdparty/rapidjson/error/error.h new file mode 100644 index 00000000..95cb31a7 --- /dev/null +++ b/src/3rdparty/rapidjson/error/error.h @@ -0,0 +1,155 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ERROR_ERROR_H_ +#define RAPIDJSON_ERROR_ERROR_H_ + +#include "../rapidjson.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +/*! \file error.h */ + +/*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */ + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ERROR_CHARTYPE + +//! Character type of error messages. +/*! \ingroup RAPIDJSON_ERRORS + The default character type is \c char. + On Windows, user can define this macro as \c TCHAR for supporting both + unicode/non-unicode settings. +*/ +#ifndef RAPIDJSON_ERROR_CHARTYPE +#define RAPIDJSON_ERROR_CHARTYPE char +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ERROR_STRING + +//! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[]. +/*! \ingroup RAPIDJSON_ERRORS + By default this conversion macro does nothing. + On Windows, user can define this macro as \c _T(x) for supporting both + unicode/non-unicode settings. +*/ +#ifndef RAPIDJSON_ERROR_STRING +#define RAPIDJSON_ERROR_STRING(x) x +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// ParseErrorCode + +//! Error code of parsing. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericReader::Parse, GenericReader::GetParseErrorCode +*/ +enum ParseErrorCode { + kParseErrorNone = 0, //!< No error. + + kParseErrorDocumentEmpty, //!< The document is empty. + kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values. + + kParseErrorValueInvalid, //!< Invalid value. + + kParseErrorObjectMissName, //!< Missing a name for object member. + kParseErrorObjectMissColon, //!< Missing a colon after a name of object member. + kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member. + + kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element. + + kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string. + kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid. + kParseErrorStringEscapeInvalid, //!< Invalid escape character in string. + kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string. + kParseErrorStringInvalidEncoding, //!< Invalid encoding in string. + + kParseErrorNumberTooBig, //!< Number too big to be stored in double. + kParseErrorNumberMissFraction, //!< Miss fraction part in number. + kParseErrorNumberMissExponent, //!< Miss exponent in number. + + kParseErrorTermination, //!< Parsing was terminated. + kParseErrorUnspecificSyntaxError //!< Unspecific syntax error. +}; + +//! Result of parsing (wraps ParseErrorCode) +/*! + \ingroup RAPIDJSON_ERRORS + \code + Document doc; + ParseResult ok = doc.Parse("[42]"); + if (!ok) { + fprintf(stderr, "JSON parse error: %s (%u)", + GetParseError_En(ok.Code()), ok.Offset()); + exit(EXIT_FAILURE); + } + \endcode + \see GenericReader::Parse, GenericDocument::Parse +*/ +struct ParseResult { +public: + //! Default constructor, no error. + ParseResult() : code_(kParseErrorNone), offset_(0) {} + //! Constructor to set an error. + ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} + + //! Get the error code. + ParseErrorCode Code() const { return code_; } + //! Get the error offset, if \ref IsError(), 0 otherwise. + size_t Offset() const { return offset_; } + + //! Conversion to \c bool, returns \c true, iff !\ref IsError(). + operator bool() const { return !IsError(); } + //! Whether the result is an error. + bool IsError() const { return code_ != kParseErrorNone; } + + bool operator==(const ParseResult& that) const { return code_ == that.code_; } + bool operator==(ParseErrorCode code) const { return code_ == code; } + friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; } + + //! Reset error code. + void Clear() { Set(kParseErrorNone); } + //! Update error code and offset. + void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } + +private: + ParseErrorCode code_; + size_t offset_; +}; + +//! Function pointer type of GetParseError(). +/*! \ingroup RAPIDJSON_ERRORS + + This is the prototype for \c GetParseError_X(), where \c X is a locale. + User can dynamically change locale in runtime, e.g.: +\code + GetParseErrorFunc GetParseError = GetParseError_En; // or whatever + const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode()); +\endcode +*/ +typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode); + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ERROR_ERROR_H_ diff --git a/src/3rdparty/rapidjson/filereadstream.h b/src/3rdparty/rapidjson/filereadstream.h new file mode 100644 index 00000000..b56ea13b --- /dev/null +++ b/src/3rdparty/rapidjson/filereadstream.h @@ -0,0 +1,99 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FILEREADSTREAM_H_ +#define RAPIDJSON_FILEREADSTREAM_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(unreachable-code) +RAPIDJSON_DIAG_OFF(missing-noreturn) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! File byte stream for input using fread(). +/*! + \note implements Stream concept +*/ +class FileReadStream { +public: + typedef char Ch; //!< Character type (byte). + + //! Constructor. + /*! + \param fp File pointer opened for read. + \param buffer user-supplied buffer. + \param bufferSize size of buffer in bytes. Must >=4 bytes. + */ + FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { + RAPIDJSON_ASSERT(fp_ != 0); + RAPIDJSON_ASSERT(bufferSize >= 4); + Read(); + } + + Ch Peek() const { return *current_; } + Ch Take() { Ch c = *current_; Read(); return c; } + size_t Tell() const { return count_ + static_cast(current_ - buffer_); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return (current_ + 4 <= bufferLast_) ? current_ : 0; + } + +private: + void Read() { + if (current_ < bufferLast_) + ++current_; + else if (!eof_) { + count_ += readCount_; + readCount_ = fread(buffer_, 1, bufferSize_, fp_); + bufferLast_ = buffer_ + readCount_ - 1; + current_ = buffer_; + + if (readCount_ < bufferSize_) { + buffer_[readCount_] = '\0'; + ++bufferLast_; + eof_ = true; + } + } + } + + std::FILE* fp_; + Ch *buffer_; + size_t bufferSize_; + Ch *bufferLast_; + Ch *current_; + size_t readCount_; + size_t count_; //!< Number of characters read + bool eof_; +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/src/3rdparty/rapidjson/filewritestream.h b/src/3rdparty/rapidjson/filewritestream.h new file mode 100644 index 00000000..6378dd60 --- /dev/null +++ b/src/3rdparty/rapidjson/filewritestream.h @@ -0,0 +1,104 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FILEWRITESTREAM_H_ +#define RAPIDJSON_FILEWRITESTREAM_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(unreachable-code) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of C file stream for input using fread(). +/*! + \note implements Stream concept +*/ +class FileWriteStream { +public: + typedef char Ch; //!< Character type. Only support char. + + FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { + RAPIDJSON_ASSERT(fp_ != 0); + } + + void Put(char c) { + if (current_ >= bufferEnd_) + Flush(); + + *current_++ = c; + } + + void PutN(char c, size_t n) { + size_t avail = static_cast(bufferEnd_ - current_); + while (n > avail) { + std::memset(current_, c, avail); + current_ += avail; + Flush(); + n -= avail; + avail = static_cast(bufferEnd_ - current_); + } + + if (n > 0) { + std::memset(current_, c, n); + current_ += n; + } + } + + void Flush() { + if (current_ != buffer_) { + size_t result = fwrite(buffer_, 1, static_cast(current_ - buffer_), fp_); + if (result < static_cast(current_ - buffer_)) { + // failure deliberately ignored at this time + // added to avoid warn_unused_result build errors + } + current_ = buffer_; + } + } + + // Not implemented + char Peek() const { RAPIDJSON_ASSERT(false); return 0; } + char Take() { RAPIDJSON_ASSERT(false); return 0; } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + // Prohibit copy constructor & assignment operator. + FileWriteStream(const FileWriteStream&); + FileWriteStream& operator=(const FileWriteStream&); + + std::FILE* fp_; + char *buffer_; + char *bufferEnd_; + char *current_; +}; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(FileWriteStream& stream, char c, size_t n) { + stream.PutN(c, n); +} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/src/3rdparty/rapidjson/fwd.h b/src/3rdparty/rapidjson/fwd.h new file mode 100644 index 00000000..e8104e84 --- /dev/null +++ b/src/3rdparty/rapidjson/fwd.h @@ -0,0 +1,151 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FWD_H_ +#define RAPIDJSON_FWD_H_ + +#include "rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN + +// encodings.h + +template struct UTF8; +template struct UTF16; +template struct UTF16BE; +template struct UTF16LE; +template struct UTF32; +template struct UTF32BE; +template struct UTF32LE; +template struct ASCII; +template struct AutoUTF; + +template +struct Transcoder; + +// allocators.h + +class CrtAllocator; + +template +class MemoryPoolAllocator; + +// stream.h + +template +struct GenericStringStream; + +typedef GenericStringStream > StringStream; + +template +struct GenericInsituStringStream; + +typedef GenericInsituStringStream > InsituStringStream; + +// stringbuffer.h + +template +class GenericStringBuffer; + +typedef GenericStringBuffer, CrtAllocator> StringBuffer; + +// filereadstream.h + +class FileReadStream; + +// filewritestream.h + +class FileWriteStream; + +// memorybuffer.h + +template +struct GenericMemoryBuffer; + +typedef GenericMemoryBuffer MemoryBuffer; + +// memorystream.h + +struct MemoryStream; + +// reader.h + +template +struct BaseReaderHandler; + +template +class GenericReader; + +typedef GenericReader, UTF8, CrtAllocator> Reader; + +// writer.h + +template +class Writer; + +// prettywriter.h + +template +class PrettyWriter; + +// document.h + +template +struct GenericMember; + +template +class GenericMemberIterator; + +template +struct GenericStringRef; + +template +class GenericValue; + +typedef GenericValue, MemoryPoolAllocator > Value; + +template +class GenericDocument; + +typedef GenericDocument, MemoryPoolAllocator, CrtAllocator> Document; + +// pointer.h + +template +class GenericPointer; + +typedef GenericPointer Pointer; + +// schema.h + +template +class IGenericRemoteSchemaDocumentProvider; + +template +class GenericSchemaDocument; + +typedef GenericSchemaDocument SchemaDocument; +typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProvider; + +template < + typename SchemaDocumentType, + typename OutputHandler, + typename StateAllocator> +class GenericSchemaValidator; + +typedef GenericSchemaValidator, void>, CrtAllocator> SchemaValidator; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_RAPIDJSONFWD_H_ diff --git a/src/3rdparty/rapidjson/internal/biginteger.h b/src/3rdparty/rapidjson/internal/biginteger.h new file mode 100644 index 00000000..9d3e88c9 --- /dev/null +++ b/src/3rdparty/rapidjson/internal/biginteger.h @@ -0,0 +1,290 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_BIGINTEGER_H_ +#define RAPIDJSON_BIGINTEGER_H_ + +#include "../rapidjson.h" + +#if defined(_MSC_VER) && defined(_M_AMD64) +#include // for _umul128 +#pragma intrinsic(_umul128) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +class BigInteger { +public: + typedef uint64_t Type; + + BigInteger(const BigInteger& rhs) : count_(rhs.count_) { + std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); + } + + explicit BigInteger(uint64_t u) : count_(1) { + digits_[0] = u; + } + + BigInteger(const char* decimals, size_t length) : count_(1) { + RAPIDJSON_ASSERT(length > 0); + digits_[0] = 0; + size_t i = 0; + const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19 + while (length >= kMaxDigitPerIteration) { + AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration); + length -= kMaxDigitPerIteration; + i += kMaxDigitPerIteration; + } + + if (length > 0) + AppendDecimal64(decimals + i, decimals + i + length); + } + + BigInteger& operator=(const BigInteger &rhs) + { + if (this != &rhs) { + count_ = rhs.count_; + std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); + } + return *this; + } + + BigInteger& operator=(uint64_t u) { + digits_[0] = u; + count_ = 1; + return *this; + } + + BigInteger& operator+=(uint64_t u) { + Type backup = digits_[0]; + digits_[0] += u; + for (size_t i = 0; i < count_ - 1; i++) { + if (digits_[i] >= backup) + return *this; // no carry + backup = digits_[i + 1]; + digits_[i + 1] += 1; + } + + // Last carry + if (digits_[count_ - 1] < backup) + PushBack(1); + + return *this; + } + + BigInteger& operator*=(uint64_t u) { + if (u == 0) return *this = 0; + if (u == 1) return *this; + if (*this == 1) return *this = u; + + uint64_t k = 0; + for (size_t i = 0; i < count_; i++) { + uint64_t hi; + digits_[i] = MulAdd64(digits_[i], u, k, &hi); + k = hi; + } + + if (k > 0) + PushBack(k); + + return *this; + } + + BigInteger& operator*=(uint32_t u) { + if (u == 0) return *this = 0; + if (u == 1) return *this; + if (*this == 1) return *this = u; + + uint64_t k = 0; + for (size_t i = 0; i < count_; i++) { + const uint64_t c = digits_[i] >> 32; + const uint64_t d = digits_[i] & 0xFFFFFFFF; + const uint64_t uc = u * c; + const uint64_t ud = u * d; + const uint64_t p0 = ud + k; + const uint64_t p1 = uc + (p0 >> 32); + digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32); + k = p1 >> 32; + } + + if (k > 0) + PushBack(k); + + return *this; + } + + BigInteger& operator<<=(size_t shift) { + if (IsZero() || shift == 0) return *this; + + size_t offset = shift / kTypeBit; + size_t interShift = shift % kTypeBit; + RAPIDJSON_ASSERT(count_ + offset <= kCapacity); + + if (interShift == 0) { + std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type)); + count_ += offset; + } + else { + digits_[count_] = 0; + for (size_t i = count_; i > 0; i--) + digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift)); + digits_[offset] = digits_[0] << interShift; + count_ += offset; + if (digits_[count_]) + count_++; + } + + std::memset(digits_, 0, offset * sizeof(Type)); + + return *this; + } + + bool operator==(const BigInteger& rhs) const { + return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0; + } + + bool operator==(const Type rhs) const { + return count_ == 1 && digits_[0] == rhs; + } + + BigInteger& MultiplyPow5(unsigned exp) { + static const uint32_t kPow5[12] = { + 5, + 5 * 5, + 5 * 5 * 5, + 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 + }; + if (exp == 0) return *this; + for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27 + for (; exp >= 13; exp -= 13) *this *= static_cast(1220703125u); // 5^13 + if (exp > 0) *this *= kPow5[exp - 1]; + return *this; + } + + // Compute absolute difference of this and rhs. + // Assume this != rhs + bool Difference(const BigInteger& rhs, BigInteger* out) const { + int cmp = Compare(rhs); + RAPIDJSON_ASSERT(cmp != 0); + const BigInteger *a, *b; // Makes a > b + bool ret; + if (cmp < 0) { a = &rhs; b = this; ret = true; } + else { a = this; b = &rhs; ret = false; } + + Type borrow = 0; + for (size_t i = 0; i < a->count_; i++) { + Type d = a->digits_[i] - borrow; + if (i < b->count_) + d -= b->digits_[i]; + borrow = (d > a->digits_[i]) ? 1 : 0; + out->digits_[i] = d; + if (d != 0) + out->count_ = i + 1; + } + + return ret; + } + + int Compare(const BigInteger& rhs) const { + if (count_ != rhs.count_) + return count_ < rhs.count_ ? -1 : 1; + + for (size_t i = count_; i-- > 0;) + if (digits_[i] != rhs.digits_[i]) + return digits_[i] < rhs.digits_[i] ? -1 : 1; + + return 0; + } + + size_t GetCount() const { return count_; } + Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; } + bool IsZero() const { return count_ == 1 && digits_[0] == 0; } + +private: + void AppendDecimal64(const char* begin, const char* end) { + uint64_t u = ParseUint64(begin, end); + if (IsZero()) + *this = u; + else { + unsigned exp = static_cast(end - begin); + (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u + } + } + + void PushBack(Type digit) { + RAPIDJSON_ASSERT(count_ < kCapacity); + digits_[count_++] = digit; + } + + static uint64_t ParseUint64(const char* begin, const char* end) { + uint64_t r = 0; + for (const char* p = begin; p != end; ++p) { + RAPIDJSON_ASSERT(*p >= '0' && *p <= '9'); + r = r * 10u + static_cast(*p - '0'); + } + return r; + } + + // Assume a * b + k < 2^128 + static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) { +#if defined(_MSC_VER) && defined(_M_AMD64) + uint64_t low = _umul128(a, b, outHigh) + k; + if (low < k) + (*outHigh)++; + return low; +#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) + __extension__ typedef unsigned __int128 uint128; + uint128 p = static_cast(a) * static_cast(b); + p += k; + *outHigh = static_cast(p >> 64); + return static_cast(p); +#else + const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32; + uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1; + x1 += (x0 >> 32); // can't give carry + x1 += x2; + if (x1 < x2) + x3 += (static_cast(1) << 32); + uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF); + uint64_t hi = x3 + (x1 >> 32); + + lo += k; + if (lo < k) + hi++; + *outHigh = hi; + return lo; +#endif + } + + static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000 + static const size_t kCapacity = kBitCount / sizeof(Type); + static const size_t kTypeBit = sizeof(Type) * 8; + + Type digits_[kCapacity]; + size_t count_; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_BIGINTEGER_H_ diff --git a/src/3rdparty/rapidjson/internal/diyfp.h b/src/3rdparty/rapidjson/internal/diyfp.h new file mode 100644 index 00000000..c9fefdc6 --- /dev/null +++ b/src/3rdparty/rapidjson/internal/diyfp.h @@ -0,0 +1,258 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +// This is a C++ header-only implementation of Grisu2 algorithm from the publication: +// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with +// integers." ACM Sigplan Notices 45.6 (2010): 233-243. + +#ifndef RAPIDJSON_DIYFP_H_ +#define RAPIDJSON_DIYFP_H_ + +#include "../rapidjson.h" + +#if defined(_MSC_VER) && defined(_M_AMD64) +#include +#pragma intrinsic(_BitScanReverse64) +#pragma intrinsic(_umul128) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +struct DiyFp { + DiyFp() : f(), e() {} + + DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {} + + explicit DiyFp(double d) { + union { + double d; + uint64_t u64; + } u = { d }; + + int biased_e = static_cast((u.u64 & kDpExponentMask) >> kDpSignificandSize); + uint64_t significand = (u.u64 & kDpSignificandMask); + if (biased_e != 0) { + f = significand + kDpHiddenBit; + e = biased_e - kDpExponentBias; + } + else { + f = significand; + e = kDpMinExponent + 1; + } + } + + DiyFp operator-(const DiyFp& rhs) const { + return DiyFp(f - rhs.f, e); + } + + DiyFp operator*(const DiyFp& rhs) const { +#if defined(_MSC_VER) && defined(_M_AMD64) + uint64_t h; + uint64_t l = _umul128(f, rhs.f, &h); + if (l & (uint64_t(1) << 63)) // rounding + h++; + return DiyFp(h, e + rhs.e + 64); +#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) + __extension__ typedef unsigned __int128 uint128; + uint128 p = static_cast(f) * static_cast(rhs.f); + uint64_t h = static_cast(p >> 64); + uint64_t l = static_cast(p); + if (l & (uint64_t(1) << 63)) // rounding + h++; + return DiyFp(h, e + rhs.e + 64); +#else + const uint64_t M32 = 0xFFFFFFFF; + const uint64_t a = f >> 32; + const uint64_t b = f & M32; + const uint64_t c = rhs.f >> 32; + const uint64_t d = rhs.f & M32; + const uint64_t ac = a * c; + const uint64_t bc = b * c; + const uint64_t ad = a * d; + const uint64_t bd = b * d; + uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32); + tmp += 1U << 31; /// mult_round + return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64); +#endif + } + + DiyFp Normalize() const { +#if defined(_MSC_VER) && defined(_M_AMD64) + unsigned long index; + _BitScanReverse64(&index, f); + return DiyFp(f << (63 - index), e - (63 - index)); +#elif defined(__GNUC__) && __GNUC__ >= 4 + int s = __builtin_clzll(f); + return DiyFp(f << s, e - s); +#else + DiyFp res = *this; + while (!(res.f & (static_cast(1) << 63))) { + res.f <<= 1; + res.e--; + } + return res; +#endif + } + + DiyFp NormalizeBoundary() const { + DiyFp res = *this; + while (!(res.f & (kDpHiddenBit << 1))) { + res.f <<= 1; + res.e--; + } + res.f <<= (kDiySignificandSize - kDpSignificandSize - 2); + res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2); + return res; + } + + void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const { + DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary(); + DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1); + mi.f <<= mi.e - pl.e; + mi.e = pl.e; + *plus = pl; + *minus = mi; + } + + double ToDouble() const { + union { + double d; + uint64_t u64; + }u; + const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 : + static_cast(e + kDpExponentBias); + u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize); + return u.d; + } + + static const int kDiySignificandSize = 64; + static const int kDpSignificandSize = 52; + static const int kDpExponentBias = 0x3FF + kDpSignificandSize; + static const int kDpMaxExponent = 0x7FF - kDpExponentBias; + static const int kDpMinExponent = -kDpExponentBias; + static const int kDpDenormalExponent = -kDpExponentBias + 1; + static const uint64_t kDpExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); + static const uint64_t kDpSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); + static const uint64_t kDpHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); + + uint64_t f; + int e; +}; + +inline DiyFp GetCachedPowerByIndex(size_t index) { + // 10^-348, 10^-340, ..., 10^340 + static const uint64_t kCachedPowers_F[] = { + RAPIDJSON_UINT64_C2(0xfa8fd5a0, 0x081c0288), RAPIDJSON_UINT64_C2(0xbaaee17f, 0xa23ebf76), + RAPIDJSON_UINT64_C2(0x8b16fb20, 0x3055ac76), RAPIDJSON_UINT64_C2(0xcf42894a, 0x5dce35ea), + RAPIDJSON_UINT64_C2(0x9a6bb0aa, 0x55653b2d), RAPIDJSON_UINT64_C2(0xe61acf03, 0x3d1a45df), + RAPIDJSON_UINT64_C2(0xab70fe17, 0xc79ac6ca), RAPIDJSON_UINT64_C2(0xff77b1fc, 0xbebcdc4f), + RAPIDJSON_UINT64_C2(0xbe5691ef, 0x416bd60c), RAPIDJSON_UINT64_C2(0x8dd01fad, 0x907ffc3c), + RAPIDJSON_UINT64_C2(0xd3515c28, 0x31559a83), RAPIDJSON_UINT64_C2(0x9d71ac8f, 0xada6c9b5), + RAPIDJSON_UINT64_C2(0xea9c2277, 0x23ee8bcb), RAPIDJSON_UINT64_C2(0xaecc4991, 0x4078536d), + RAPIDJSON_UINT64_C2(0x823c1279, 0x5db6ce57), RAPIDJSON_UINT64_C2(0xc2109436, 0x4dfb5637), + RAPIDJSON_UINT64_C2(0x9096ea6f, 0x3848984f), RAPIDJSON_UINT64_C2(0xd77485cb, 0x25823ac7), + RAPIDJSON_UINT64_C2(0xa086cfcd, 0x97bf97f4), RAPIDJSON_UINT64_C2(0xef340a98, 0x172aace5), + RAPIDJSON_UINT64_C2(0xb23867fb, 0x2a35b28e), RAPIDJSON_UINT64_C2(0x84c8d4df, 0xd2c63f3b), + RAPIDJSON_UINT64_C2(0xc5dd4427, 0x1ad3cdba), RAPIDJSON_UINT64_C2(0x936b9fce, 0xbb25c996), + RAPIDJSON_UINT64_C2(0xdbac6c24, 0x7d62a584), RAPIDJSON_UINT64_C2(0xa3ab6658, 0x0d5fdaf6), + RAPIDJSON_UINT64_C2(0xf3e2f893, 0xdec3f126), RAPIDJSON_UINT64_C2(0xb5b5ada8, 0xaaff80b8), + RAPIDJSON_UINT64_C2(0x87625f05, 0x6c7c4a8b), RAPIDJSON_UINT64_C2(0xc9bcff60, 0x34c13053), + RAPIDJSON_UINT64_C2(0x964e858c, 0x91ba2655), RAPIDJSON_UINT64_C2(0xdff97724, 0x70297ebd), + RAPIDJSON_UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), RAPIDJSON_UINT64_C2(0xf8a95fcf, 0x88747d94), + RAPIDJSON_UINT64_C2(0xb9447093, 0x8fa89bcf), RAPIDJSON_UINT64_C2(0x8a08f0f8, 0xbf0f156b), + RAPIDJSON_UINT64_C2(0xcdb02555, 0x653131b6), RAPIDJSON_UINT64_C2(0x993fe2c6, 0xd07b7fac), + RAPIDJSON_UINT64_C2(0xe45c10c4, 0x2a2b3b06), RAPIDJSON_UINT64_C2(0xaa242499, 0x697392d3), + RAPIDJSON_UINT64_C2(0xfd87b5f2, 0x8300ca0e), RAPIDJSON_UINT64_C2(0xbce50864, 0x92111aeb), + RAPIDJSON_UINT64_C2(0x8cbccc09, 0x6f5088cc), RAPIDJSON_UINT64_C2(0xd1b71758, 0xe219652c), + RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), RAPIDJSON_UINT64_C2(0xe8d4a510, 0x00000000), + RAPIDJSON_UINT64_C2(0xad78ebc5, 0xac620000), RAPIDJSON_UINT64_C2(0x813f3978, 0xf8940984), + RAPIDJSON_UINT64_C2(0xc097ce7b, 0xc90715b3), RAPIDJSON_UINT64_C2(0x8f7e32ce, 0x7bea5c70), + RAPIDJSON_UINT64_C2(0xd5d238a4, 0xabe98068), RAPIDJSON_UINT64_C2(0x9f4f2726, 0x179a2245), + RAPIDJSON_UINT64_C2(0xed63a231, 0xd4c4fb27), RAPIDJSON_UINT64_C2(0xb0de6538, 0x8cc8ada8), + RAPIDJSON_UINT64_C2(0x83c7088e, 0x1aab65db), RAPIDJSON_UINT64_C2(0xc45d1df9, 0x42711d9a), + RAPIDJSON_UINT64_C2(0x924d692c, 0xa61be758), RAPIDJSON_UINT64_C2(0xda01ee64, 0x1a708dea), + RAPIDJSON_UINT64_C2(0xa26da399, 0x9aef774a), RAPIDJSON_UINT64_C2(0xf209787b, 0xb47d6b85), + RAPIDJSON_UINT64_C2(0xb454e4a1, 0x79dd1877), RAPIDJSON_UINT64_C2(0x865b8692, 0x5b9bc5c2), + RAPIDJSON_UINT64_C2(0xc83553c5, 0xc8965d3d), RAPIDJSON_UINT64_C2(0x952ab45c, 0xfa97a0b3), + RAPIDJSON_UINT64_C2(0xde469fbd, 0x99a05fe3), RAPIDJSON_UINT64_C2(0xa59bc234, 0xdb398c25), + RAPIDJSON_UINT64_C2(0xf6c69a72, 0xa3989f5c), RAPIDJSON_UINT64_C2(0xb7dcbf53, 0x54e9bece), + RAPIDJSON_UINT64_C2(0x88fcf317, 0xf22241e2), RAPIDJSON_UINT64_C2(0xcc20ce9b, 0xd35c78a5), + RAPIDJSON_UINT64_C2(0x98165af3, 0x7b2153df), RAPIDJSON_UINT64_C2(0xe2a0b5dc, 0x971f303a), + RAPIDJSON_UINT64_C2(0xa8d9d153, 0x5ce3b396), RAPIDJSON_UINT64_C2(0xfb9b7cd9, 0xa4a7443c), + RAPIDJSON_UINT64_C2(0xbb764c4c, 0xa7a44410), RAPIDJSON_UINT64_C2(0x8bab8eef, 0xb6409c1a), + RAPIDJSON_UINT64_C2(0xd01fef10, 0xa657842c), RAPIDJSON_UINT64_C2(0x9b10a4e5, 0xe9913129), + RAPIDJSON_UINT64_C2(0xe7109bfb, 0xa19c0c9d), RAPIDJSON_UINT64_C2(0xac2820d9, 0x623bf429), + RAPIDJSON_UINT64_C2(0x80444b5e, 0x7aa7cf85), RAPIDJSON_UINT64_C2(0xbf21e440, 0x03acdd2d), + RAPIDJSON_UINT64_C2(0x8e679c2f, 0x5e44ff8f), RAPIDJSON_UINT64_C2(0xd433179d, 0x9c8cb841), + RAPIDJSON_UINT64_C2(0x9e19db92, 0xb4e31ba9), RAPIDJSON_UINT64_C2(0xeb96bf6e, 0xbadf77d9), + RAPIDJSON_UINT64_C2(0xaf87023b, 0x9bf0ee6b) + }; + static const int16_t kCachedPowers_E[] = { + -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, + -954, -927, -901, -874, -847, -821, -794, -768, -741, -715, + -688, -661, -635, -608, -582, -555, -529, -502, -475, -449, + -422, -396, -369, -343, -316, -289, -263, -236, -210, -183, + -157, -130, -103, -77, -50, -24, 3, 30, 56, 83, + 109, 136, 162, 189, 216, 242, 269, 295, 322, 348, + 375, 402, 428, 455, 481, 508, 534, 561, 588, 614, + 641, 667, 694, 720, 747, 774, 800, 827, 853, 880, + 907, 933, 960, 986, 1013, 1039, 1066 + }; + return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]); +} + +inline DiyFp GetCachedPower(int e, int* K) { + + //int k = static_cast(ceil((-61 - e) * 0.30102999566398114)) + 374; + double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive + int k = static_cast(dk); + if (dk - k > 0.0) + k++; + + unsigned index = static_cast((k >> 3) + 1); + *K = -(-348 + static_cast(index << 3)); // decimal exponent no need lookup table + + return GetCachedPowerByIndex(index); +} + +inline DiyFp GetCachedPower10(int exp, int *outExp) { + unsigned index = (static_cast(exp) + 348u) / 8u; + *outExp = -348 + static_cast(index) * 8; + return GetCachedPowerByIndex(index); + } + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +RAPIDJSON_DIAG_OFF(padded) +#endif + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_DIYFP_H_ diff --git a/src/3rdparty/rapidjson/internal/dtoa.h b/src/3rdparty/rapidjson/internal/dtoa.h new file mode 100644 index 00000000..8d6350e6 --- /dev/null +++ b/src/3rdparty/rapidjson/internal/dtoa.h @@ -0,0 +1,245 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +// This is a C++ header-only implementation of Grisu2 algorithm from the publication: +// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with +// integers." ACM Sigplan Notices 45.6 (2010): 233-243. + +#ifndef RAPIDJSON_DTOA_ +#define RAPIDJSON_DTOA_ + +#include "itoa.h" // GetDigitsLut() +#include "diyfp.h" +#include "ieee754.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124 +#endif + +inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) { + while (rest < wp_w && delta - rest >= ten_kappa && + (rest + ten_kappa < wp_w || /// closer + wp_w - rest > rest + ten_kappa - wp_w)) { + buffer[len - 1]--; + rest += ten_kappa; + } +} + +inline unsigned CountDecimalDigit32(uint32_t n) { + // Simple pure C++ implementation was faster than __builtin_clz version in this situation. + if (n < 10) return 1; + if (n < 100) return 2; + if (n < 1000) return 3; + if (n < 10000) return 4; + if (n < 100000) return 5; + if (n < 1000000) return 6; + if (n < 10000000) return 7; + if (n < 100000000) return 8; + // Will not reach 10 digits in DigitGen() + //if (n < 1000000000) return 9; + //return 10; + return 9; +} + +inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) { + static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; + const DiyFp one(uint64_t(1) << -Mp.e, Mp.e); + const DiyFp wp_w = Mp - W; + uint32_t p1 = static_cast(Mp.f >> -one.e); + uint64_t p2 = Mp.f & (one.f - 1); + unsigned kappa = CountDecimalDigit32(p1); // kappa in [0, 9] + *len = 0; + + while (kappa > 0) { + uint32_t d = 0; + switch (kappa) { + case 9: d = p1 / 100000000; p1 %= 100000000; break; + case 8: d = p1 / 10000000; p1 %= 10000000; break; + case 7: d = p1 / 1000000; p1 %= 1000000; break; + case 6: d = p1 / 100000; p1 %= 100000; break; + case 5: d = p1 / 10000; p1 %= 10000; break; + case 4: d = p1 / 1000; p1 %= 1000; break; + case 3: d = p1 / 100; p1 %= 100; break; + case 2: d = p1 / 10; p1 %= 10; break; + case 1: d = p1; p1 = 0; break; + default:; + } + if (d || *len) + buffer[(*len)++] = static_cast('0' + static_cast(d)); + kappa--; + uint64_t tmp = (static_cast(p1) << -one.e) + p2; + if (tmp <= delta) { + *K += kappa; + GrisuRound(buffer, *len, delta, tmp, static_cast(kPow10[kappa]) << -one.e, wp_w.f); + return; + } + } + + // kappa = 0 + for (;;) { + p2 *= 10; + delta *= 10; + char d = static_cast(p2 >> -one.e); + if (d || *len) + buffer[(*len)++] = static_cast('0' + d); + p2 &= one.f - 1; + kappa--; + if (p2 < delta) { + *K += kappa; + int index = -static_cast(kappa); + GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[-static_cast(kappa)] : 0)); + return; + } + } +} + +inline void Grisu2(double value, char* buffer, int* length, int* K) { + const DiyFp v(value); + DiyFp w_m, w_p; + v.NormalizedBoundaries(&w_m, &w_p); + + const DiyFp c_mk = GetCachedPower(w_p.e, K); + const DiyFp W = v.Normalize() * c_mk; + DiyFp Wp = w_p * c_mk; + DiyFp Wm = w_m * c_mk; + Wm.f++; + Wp.f--; + DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K); +} + +inline char* WriteExponent(int K, char* buffer) { + if (K < 0) { + *buffer++ = '-'; + K = -K; + } + + if (K >= 100) { + *buffer++ = static_cast('0' + static_cast(K / 100)); + K %= 100; + const char* d = GetDigitsLut() + K * 2; + *buffer++ = d[0]; + *buffer++ = d[1]; + } + else if (K >= 10) { + const char* d = GetDigitsLut() + K * 2; + *buffer++ = d[0]; + *buffer++ = d[1]; + } + else + *buffer++ = static_cast('0' + static_cast(K)); + + return buffer; +} + +inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) { + const int kk = length + k; // 10^(kk-1) <= v < 10^kk + + if (0 <= k && kk <= 21) { + // 1234e7 -> 12340000000 + for (int i = length; i < kk; i++) + buffer[i] = '0'; + buffer[kk] = '.'; + buffer[kk + 1] = '0'; + return &buffer[kk + 2]; + } + else if (0 < kk && kk <= 21) { + // 1234e-2 -> 12.34 + std::memmove(&buffer[kk + 1], &buffer[kk], static_cast(length - kk)); + buffer[kk] = '.'; + if (0 > k + maxDecimalPlaces) { + // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1 + // Remove extra trailing zeros (at least one) after truncation. + for (int i = kk + maxDecimalPlaces; i > kk + 1; i--) + if (buffer[i] != '0') + return &buffer[i + 1]; + return &buffer[kk + 2]; // Reserve one zero + } + else + return &buffer[length + 1]; + } + else if (-6 < kk && kk <= 0) { + // 1234e-6 -> 0.001234 + const int offset = 2 - kk; + std::memmove(&buffer[offset], &buffer[0], static_cast(length)); + buffer[0] = '0'; + buffer[1] = '.'; + for (int i = 2; i < offset; i++) + buffer[i] = '0'; + if (length - kk > maxDecimalPlaces) { + // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1 + // Remove extra trailing zeros (at least one) after truncation. + for (int i = maxDecimalPlaces + 1; i > 2; i--) + if (buffer[i] != '0') + return &buffer[i + 1]; + return &buffer[3]; // Reserve one zero + } + else + return &buffer[length + offset]; + } + else if (kk < -maxDecimalPlaces) { + // Truncate to zero + buffer[0] = '0'; + buffer[1] = '.'; + buffer[2] = '0'; + return &buffer[3]; + } + else if (length == 1) { + // 1e30 + buffer[1] = 'e'; + return WriteExponent(kk - 1, &buffer[2]); + } + else { + // 1234e30 -> 1.234e33 + std::memmove(&buffer[2], &buffer[1], static_cast(length - 1)); + buffer[1] = '.'; + buffer[length + 1] = 'e'; + return WriteExponent(kk - 1, &buffer[0 + length + 2]); + } +} + +inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) { + RAPIDJSON_ASSERT(maxDecimalPlaces >= 1); + Double d(value); + if (d.IsZero()) { + if (d.Sign()) + *buffer++ = '-'; // -0.0, Issue #289 + buffer[0] = '0'; + buffer[1] = '.'; + buffer[2] = '0'; + return &buffer[3]; + } + else { + if (value < 0) { + *buffer++ = '-'; + value = -value; + } + int length, K; + Grisu2(value, buffer, &length, &K); + return Prettify(buffer, length, K, maxDecimalPlaces); + } +} + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_DTOA_ diff --git a/src/3rdparty/rapidjson/internal/ieee754.h b/src/3rdparty/rapidjson/internal/ieee754.h new file mode 100644 index 00000000..82bb0b99 --- /dev/null +++ b/src/3rdparty/rapidjson/internal/ieee754.h @@ -0,0 +1,78 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_IEEE754_ +#define RAPIDJSON_IEEE754_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +class Double { +public: + Double() {} + Double(double d) : d_(d) {} + Double(uint64_t u) : u_(u) {} + + double Value() const { return d_; } + uint64_t Uint64Value() const { return u_; } + + double NextPositiveDouble() const { + RAPIDJSON_ASSERT(!Sign()); + return Double(u_ + 1).Value(); + } + + bool Sign() const { return (u_ & kSignMask) != 0; } + uint64_t Significand() const { return u_ & kSignificandMask; } + int Exponent() const { return static_cast(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); } + + bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; } + bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; } + bool IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; } + bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; } + bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; } + + uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); } + int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; } + uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; } + + static unsigned EffectiveSignificandSize(int order) { + if (order >= -1021) + return 53; + else if (order <= -1074) + return 0; + else + return static_cast(order) + 1074; + } + +private: + static const int kSignificandSize = 52; + static const int kExponentBias = 0x3FF; + static const int kDenormalExponent = 1 - kExponentBias; + static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000); + static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); + static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); + static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); + + union { + double d_; + uint64_t u_; + }; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_IEEE754_ diff --git a/src/3rdparty/rapidjson/internal/itoa.h b/src/3rdparty/rapidjson/internal/itoa.h new file mode 100644 index 00000000..01a4e7e7 --- /dev/null +++ b/src/3rdparty/rapidjson/internal/itoa.h @@ -0,0 +1,304 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ITOA_ +#define RAPIDJSON_ITOA_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline const char* GetDigitsLut() { + static const char cDigitsLut[200] = { + '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9', + '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9', + '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9', + '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9', + '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9', + '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9', + '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9', + '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9', + '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', + '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9' + }; + return cDigitsLut; +} + +inline char* u32toa(uint32_t value, char* buffer) { + const char* cDigitsLut = GetDigitsLut(); + + if (value < 10000) { + const uint32_t d1 = (value / 100) << 1; + const uint32_t d2 = (value % 100) << 1; + + if (value >= 1000) + *buffer++ = cDigitsLut[d1]; + if (value >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else if (value < 100000000) { + // value = bbbbcccc + const uint32_t b = value / 10000; + const uint32_t c = value % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + else { + // value = aabbbbcccc in decimal + + const uint32_t a = value / 100000000; // 1 to 42 + value %= 100000000; + + if (a >= 10) { + const unsigned i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else + *buffer++ = static_cast('0' + static_cast(a)); + + const uint32_t b = value / 10000; // 0 to 9999 + const uint32_t c = value % 10000; // 0 to 9999 + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + return buffer; +} + +inline char* i32toa(int32_t value, char* buffer) { + uint32_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + + return u32toa(u, buffer); +} + +inline char* u64toa(uint64_t value, char* buffer) { + const char* cDigitsLut = GetDigitsLut(); + const uint64_t kTen8 = 100000000; + const uint64_t kTen9 = kTen8 * 10; + const uint64_t kTen10 = kTen8 * 100; + const uint64_t kTen11 = kTen8 * 1000; + const uint64_t kTen12 = kTen8 * 10000; + const uint64_t kTen13 = kTen8 * 100000; + const uint64_t kTen14 = kTen8 * 1000000; + const uint64_t kTen15 = kTen8 * 10000000; + const uint64_t kTen16 = kTen8 * kTen8; + + if (value < kTen8) { + uint32_t v = static_cast(value); + if (v < 10000) { + const uint32_t d1 = (v / 100) << 1; + const uint32_t d2 = (v % 100) << 1; + + if (v >= 1000) + *buffer++ = cDigitsLut[d1]; + if (v >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (v >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else { + // value = bbbbcccc + const uint32_t b = v / 10000; + const uint32_t c = v % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + } + else if (value < kTen16) { + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + if (value >= kTen15) + *buffer++ = cDigitsLut[d1]; + if (value >= kTen14) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= kTen13) + *buffer++ = cDigitsLut[d2]; + if (value >= kTen12) + *buffer++ = cDigitsLut[d2 + 1]; + if (value >= kTen11) + *buffer++ = cDigitsLut[d3]; + if (value >= kTen10) + *buffer++ = cDigitsLut[d3 + 1]; + if (value >= kTen9) + *buffer++ = cDigitsLut[d4]; + if (value >= kTen8) + *buffer++ = cDigitsLut[d4 + 1]; + + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + else { + const uint32_t a = static_cast(value / kTen16); // 1 to 1844 + value %= kTen16; + + if (a < 10) + *buffer++ = static_cast('0' + static_cast(a)); + else if (a < 100) { + const uint32_t i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else if (a < 1000) { + *buffer++ = static_cast('0' + static_cast(a / 100)); + + const uint32_t i = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else { + const uint32_t i = (a / 100) << 1; + const uint32_t j = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + *buffer++ = cDigitsLut[j]; + *buffer++ = cDigitsLut[j + 1]; + } + + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + + return buffer; +} + +inline char* i64toa(int64_t value, char* buffer) { + uint64_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + + return u64toa(u, buffer); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ITOA_ diff --git a/src/3rdparty/rapidjson/internal/meta.h b/src/3rdparty/rapidjson/internal/meta.h new file mode 100644 index 00000000..5a9aaa42 --- /dev/null +++ b/src/3rdparty/rapidjson/internal/meta.h @@ -0,0 +1,181 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_META_H_ +#define RAPIDJSON_INTERNAL_META_H_ + +#include "../rapidjson.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif +#if defined(_MSC_VER) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(6334) +#endif + +#if RAPIDJSON_HAS_CXX11_TYPETRAITS +#include +#endif + +//@cond RAPIDJSON_INTERNAL +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +// Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching +template struct Void { typedef void Type; }; + +/////////////////////////////////////////////////////////////////////////////// +// BoolType, TrueType, FalseType +// +template struct BoolType { + static const bool Value = Cond; + typedef BoolType Type; +}; +typedef BoolType TrueType; +typedef BoolType FalseType; + + +/////////////////////////////////////////////////////////////////////////////// +// SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr +// + +template struct SelectIfImpl { template struct Apply { typedef T1 Type; }; }; +template <> struct SelectIfImpl { template struct Apply { typedef T2 Type; }; }; +template struct SelectIfCond : SelectIfImpl::template Apply {}; +template struct SelectIf : SelectIfCond {}; + +template struct AndExprCond : FalseType {}; +template <> struct AndExprCond : TrueType {}; +template struct OrExprCond : TrueType {}; +template <> struct OrExprCond : FalseType {}; + +template struct BoolExpr : SelectIf::Type {}; +template struct NotExpr : SelectIf::Type {}; +template struct AndExpr : AndExprCond::Type {}; +template struct OrExpr : OrExprCond::Type {}; + + +/////////////////////////////////////////////////////////////////////////////// +// AddConst, MaybeAddConst, RemoveConst +template struct AddConst { typedef const T Type; }; +template struct MaybeAddConst : SelectIfCond {}; +template struct RemoveConst { typedef T Type; }; +template struct RemoveConst { typedef T Type; }; + + +/////////////////////////////////////////////////////////////////////////////// +// IsSame, IsConst, IsMoreConst, IsPointer +// +template struct IsSame : FalseType {}; +template struct IsSame : TrueType {}; + +template struct IsConst : FalseType {}; +template struct IsConst : TrueType {}; + +template +struct IsMoreConst + : AndExpr::Type, typename RemoveConst::Type>, + BoolType::Value >= IsConst::Value> >::Type {}; + +template struct IsPointer : FalseType {}; +template struct IsPointer : TrueType {}; + +/////////////////////////////////////////////////////////////////////////////// +// IsBaseOf +// +#if RAPIDJSON_HAS_CXX11_TYPETRAITS + +template struct IsBaseOf + : BoolType< ::std::is_base_of::value> {}; + +#else // simplified version adopted from Boost + +template struct IsBaseOfImpl { + RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0); + RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0); + + typedef char (&Yes)[1]; + typedef char (&No) [2]; + + template + static Yes Check(const D*, T); + static No Check(const B*, int); + + struct Host { + operator const B*() const; + operator const D*(); + }; + + enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) }; +}; + +template struct IsBaseOf + : OrExpr, BoolExpr > >::Type {}; + +#endif // RAPIDJSON_HAS_CXX11_TYPETRAITS + + +////////////////////////////////////////////////////////////////////////// +// EnableIf / DisableIf +// +template struct EnableIfCond { typedef T Type; }; +template struct EnableIfCond { /* empty */ }; + +template struct DisableIfCond { typedef T Type; }; +template struct DisableIfCond { /* empty */ }; + +template +struct EnableIf : EnableIfCond {}; + +template +struct DisableIf : DisableIfCond {}; + +// SFINAE helpers +struct SfinaeTag {}; +template struct RemoveSfinaeTag; +template struct RemoveSfinaeTag { typedef T Type; }; + +#define RAPIDJSON_REMOVEFPTR_(type) \ + typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \ + < ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type + +#define RAPIDJSON_ENABLEIF(cond) \ + typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ + ::Type * = NULL + +#define RAPIDJSON_DISABLEIF(cond) \ + typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ + ::Type * = NULL + +#define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \ + typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ + ::Type + +#define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \ + typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ + ::Type + +} // namespace internal +RAPIDJSON_NAMESPACE_END +//@endcond + +#if defined(__GNUC__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_META_H_ diff --git a/src/3rdparty/rapidjson/internal/pow10.h b/src/3rdparty/rapidjson/internal/pow10.h new file mode 100644 index 00000000..02f475d7 --- /dev/null +++ b/src/3rdparty/rapidjson/internal/pow10.h @@ -0,0 +1,55 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_POW10_ +#define RAPIDJSON_POW10_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Computes integer powers of 10 in double (10.0^n). +/*! This function uses lookup table for fast and accurate results. + \param n non-negative exponent. Must <= 308. + \return 10.0^n +*/ +inline double Pow10(int n) { + static const double e[] = { // 1e-0...1e308: 309 * 8 bytes = 2472 bytes + 1e+0, + 1e+1, 1e+2, 1e+3, 1e+4, 1e+5, 1e+6, 1e+7, 1e+8, 1e+9, 1e+10, 1e+11, 1e+12, 1e+13, 1e+14, 1e+15, 1e+16, 1e+17, 1e+18, 1e+19, 1e+20, + 1e+21, 1e+22, 1e+23, 1e+24, 1e+25, 1e+26, 1e+27, 1e+28, 1e+29, 1e+30, 1e+31, 1e+32, 1e+33, 1e+34, 1e+35, 1e+36, 1e+37, 1e+38, 1e+39, 1e+40, + 1e+41, 1e+42, 1e+43, 1e+44, 1e+45, 1e+46, 1e+47, 1e+48, 1e+49, 1e+50, 1e+51, 1e+52, 1e+53, 1e+54, 1e+55, 1e+56, 1e+57, 1e+58, 1e+59, 1e+60, + 1e+61, 1e+62, 1e+63, 1e+64, 1e+65, 1e+66, 1e+67, 1e+68, 1e+69, 1e+70, 1e+71, 1e+72, 1e+73, 1e+74, 1e+75, 1e+76, 1e+77, 1e+78, 1e+79, 1e+80, + 1e+81, 1e+82, 1e+83, 1e+84, 1e+85, 1e+86, 1e+87, 1e+88, 1e+89, 1e+90, 1e+91, 1e+92, 1e+93, 1e+94, 1e+95, 1e+96, 1e+97, 1e+98, 1e+99, 1e+100, + 1e+101,1e+102,1e+103,1e+104,1e+105,1e+106,1e+107,1e+108,1e+109,1e+110,1e+111,1e+112,1e+113,1e+114,1e+115,1e+116,1e+117,1e+118,1e+119,1e+120, + 1e+121,1e+122,1e+123,1e+124,1e+125,1e+126,1e+127,1e+128,1e+129,1e+130,1e+131,1e+132,1e+133,1e+134,1e+135,1e+136,1e+137,1e+138,1e+139,1e+140, + 1e+141,1e+142,1e+143,1e+144,1e+145,1e+146,1e+147,1e+148,1e+149,1e+150,1e+151,1e+152,1e+153,1e+154,1e+155,1e+156,1e+157,1e+158,1e+159,1e+160, + 1e+161,1e+162,1e+163,1e+164,1e+165,1e+166,1e+167,1e+168,1e+169,1e+170,1e+171,1e+172,1e+173,1e+174,1e+175,1e+176,1e+177,1e+178,1e+179,1e+180, + 1e+181,1e+182,1e+183,1e+184,1e+185,1e+186,1e+187,1e+188,1e+189,1e+190,1e+191,1e+192,1e+193,1e+194,1e+195,1e+196,1e+197,1e+198,1e+199,1e+200, + 1e+201,1e+202,1e+203,1e+204,1e+205,1e+206,1e+207,1e+208,1e+209,1e+210,1e+211,1e+212,1e+213,1e+214,1e+215,1e+216,1e+217,1e+218,1e+219,1e+220, + 1e+221,1e+222,1e+223,1e+224,1e+225,1e+226,1e+227,1e+228,1e+229,1e+230,1e+231,1e+232,1e+233,1e+234,1e+235,1e+236,1e+237,1e+238,1e+239,1e+240, + 1e+241,1e+242,1e+243,1e+244,1e+245,1e+246,1e+247,1e+248,1e+249,1e+250,1e+251,1e+252,1e+253,1e+254,1e+255,1e+256,1e+257,1e+258,1e+259,1e+260, + 1e+261,1e+262,1e+263,1e+264,1e+265,1e+266,1e+267,1e+268,1e+269,1e+270,1e+271,1e+272,1e+273,1e+274,1e+275,1e+276,1e+277,1e+278,1e+279,1e+280, + 1e+281,1e+282,1e+283,1e+284,1e+285,1e+286,1e+287,1e+288,1e+289,1e+290,1e+291,1e+292,1e+293,1e+294,1e+295,1e+296,1e+297,1e+298,1e+299,1e+300, + 1e+301,1e+302,1e+303,1e+304,1e+305,1e+306,1e+307,1e+308 + }; + RAPIDJSON_ASSERT(n >= 0 && n <= 308); + return e[n]; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_POW10_ diff --git a/src/3rdparty/rapidjson/internal/regex.h b/src/3rdparty/rapidjson/internal/regex.h new file mode 100644 index 00000000..422a5240 --- /dev/null +++ b/src/3rdparty/rapidjson/internal/regex.h @@ -0,0 +1,701 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_REGEX_H_ +#define RAPIDJSON_INTERNAL_REGEX_H_ + +#include "../allocators.h" +#include "../stream.h" +#include "stack.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(implicit-fallthrough) +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +#ifndef RAPIDJSON_REGEX_VERBOSE +#define RAPIDJSON_REGEX_VERBOSE 0 +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +/////////////////////////////////////////////////////////////////////////////// +// GenericRegex + +static const SizeType kRegexInvalidState = ~SizeType(0); //!< Represents an invalid index in GenericRegex::State::out, out1 +static const SizeType kRegexInvalidRange = ~SizeType(0); + +//! Regular expression engine with subset of ECMAscript grammar. +/*! + Supported regular expression syntax: + - \c ab Concatenation + - \c a|b Alternation + - \c a? Zero or one + - \c a* Zero or more + - \c a+ One or more + - \c a{3} Exactly 3 times + - \c a{3,} At least 3 times + - \c a{3,5} 3 to 5 times + - \c (ab) Grouping + - \c ^a At the beginning + - \c a$ At the end + - \c . Any character + - \c [abc] Character classes + - \c [a-c] Character class range + - \c [a-z0-9_] Character class combination + - \c [^abc] Negated character classes + - \c [^a-c] Negated character class range + - \c [\b] Backspace (U+0008) + - \c \\| \\\\ ... Escape characters + - \c \\f Form feed (U+000C) + - \c \\n Line feed (U+000A) + - \c \\r Carriage return (U+000D) + - \c \\t Tab (U+0009) + - \c \\v Vertical tab (U+000B) + + \note This is a Thompson NFA engine, implemented with reference to + Cox, Russ. "Regular Expression Matching Can Be Simple And Fast (but is slow in Java, Perl, PHP, Python, Ruby,...).", + https://swtch.com/~rsc/regexp/regexp1.html +*/ +template +class GenericRegex { +public: + typedef typename Encoding::Ch Ch; + + GenericRegex(const Ch* source, Allocator* allocator = 0) : + states_(allocator, 256), ranges_(allocator, 256), root_(kRegexInvalidState), stateCount_(), rangeCount_(), + stateSet_(), state0_(allocator, 0), state1_(allocator, 0), anchorBegin_(), anchorEnd_() + { + GenericStringStream ss(source); + DecodedStream > ds(ss); + Parse(ds); + } + + ~GenericRegex() { + Allocator::Free(stateSet_); + } + + bool IsValid() const { + return root_ != kRegexInvalidState; + } + + template + bool Match(InputStream& is) const { + return SearchWithAnchoring(is, true, true); + } + + bool Match(const Ch* s) const { + GenericStringStream is(s); + return Match(is); + } + + template + bool Search(InputStream& is) const { + return SearchWithAnchoring(is, anchorBegin_, anchorEnd_); + } + + bool Search(const Ch* s) const { + GenericStringStream is(s); + return Search(is); + } + +private: + enum Operator { + kZeroOrOne, + kZeroOrMore, + kOneOrMore, + kConcatenation, + kAlternation, + kLeftParenthesis + }; + + static const unsigned kAnyCharacterClass = 0xFFFFFFFF; //!< For '.' + static const unsigned kRangeCharacterClass = 0xFFFFFFFE; + static const unsigned kRangeNegationFlag = 0x80000000; + + struct Range { + unsigned start; // + unsigned end; + SizeType next; + }; + + struct State { + SizeType out; //!< Equals to kInvalid for matching state + SizeType out1; //!< Equals to non-kInvalid for split + SizeType rangeStart; + unsigned codepoint; + }; + + struct Frag { + Frag(SizeType s, SizeType o, SizeType m) : start(s), out(o), minIndex(m) {} + SizeType start; + SizeType out; //!< link-list of all output states + SizeType minIndex; + }; + + template + class DecodedStream { + public: + DecodedStream(SourceStream& ss) : ss_(ss), codepoint_() { Decode(); } + unsigned Peek() { return codepoint_; } + unsigned Take() { + unsigned c = codepoint_; + if (c) // No further decoding when '\0' + Decode(); + return c; + } + + private: + void Decode() { + if (!Encoding::Decode(ss_, &codepoint_)) + codepoint_ = 0; + } + + SourceStream& ss_; + unsigned codepoint_; + }; + + State& GetState(SizeType index) { + RAPIDJSON_ASSERT(index < stateCount_); + return states_.template Bottom()[index]; + } + + const State& GetState(SizeType index) const { + RAPIDJSON_ASSERT(index < stateCount_); + return states_.template Bottom()[index]; + } + + Range& GetRange(SizeType index) { + RAPIDJSON_ASSERT(index < rangeCount_); + return ranges_.template Bottom()[index]; + } + + const Range& GetRange(SizeType index) const { + RAPIDJSON_ASSERT(index < rangeCount_); + return ranges_.template Bottom()[index]; + } + + template + void Parse(DecodedStream& ds) { + Allocator allocator; + Stack operandStack(&allocator, 256); // Frag + Stack operatorStack(&allocator, 256); // Operator + Stack atomCountStack(&allocator, 256); // unsigned (Atom per parenthesis) + + *atomCountStack.template Push() = 0; + + unsigned codepoint; + while (ds.Peek() != 0) { + switch (codepoint = ds.Take()) { + case '^': + anchorBegin_ = true; + break; + + case '$': + anchorEnd_ = true; + break; + + case '|': + while (!operatorStack.Empty() && *operatorStack.template Top() < kAlternation) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + *operatorStack.template Push() = kAlternation; + *atomCountStack.template Top() = 0; + break; + + case '(': + *operatorStack.template Push() = kLeftParenthesis; + *atomCountStack.template Push() = 0; + break; + + case ')': + while (!operatorStack.Empty() && *operatorStack.template Top() != kLeftParenthesis) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + if (operatorStack.Empty()) + return; + operatorStack.template Pop(1); + atomCountStack.template Pop(1); + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '?': + if (!Eval(operandStack, kZeroOrOne)) + return; + break; + + case '*': + if (!Eval(operandStack, kZeroOrMore)) + return; + break; + + case '+': + if (!Eval(operandStack, kOneOrMore)) + return; + break; + + case '{': + { + unsigned n, m; + if (!ParseUnsigned(ds, &n)) + return; + + if (ds.Peek() == ',') { + ds.Take(); + if (ds.Peek() == '}') + m = kInfinityQuantifier; + else if (!ParseUnsigned(ds, &m) || m < n) + return; + } + else + m = n; + + if (!EvalQuantifier(operandStack, n, m) || ds.Peek() != '}') + return; + ds.Take(); + } + break; + + case '.': + PushOperand(operandStack, kAnyCharacterClass); + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '[': + { + SizeType range; + if (!ParseRange(ds, &range)) + return; + SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, kRangeCharacterClass); + GetState(s).rangeStart = range; + *operandStack.template Push() = Frag(s, s, s); + } + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '\\': // Escape character + if (!CharacterEscape(ds, &codepoint)) + return; // Unsupported escape character + // fall through to default + + default: // Pattern character + PushOperand(operandStack, codepoint); + ImplicitConcatenation(atomCountStack, operatorStack); + } + } + + while (!operatorStack.Empty()) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + + // Link the operand to matching state. + if (operandStack.GetSize() == sizeof(Frag)) { + Frag* e = operandStack.template Pop(1); + Patch(e->out, NewState(kRegexInvalidState, kRegexInvalidState, 0)); + root_ = e->start; + +#if RAPIDJSON_REGEX_VERBOSE + printf("root: %d\n", root_); + for (SizeType i = 0; i < stateCount_ ; i++) { + State& s = GetState(i); + printf("[%2d] out: %2d out1: %2d c: '%c'\n", i, s.out, s.out1, (char)s.codepoint); + } + printf("\n"); +#endif + } + + // Preallocate buffer for SearchWithAnchoring() + RAPIDJSON_ASSERT(stateSet_ == 0); + if (stateCount_ > 0) { + stateSet_ = static_cast(states_.GetAllocator().Malloc(GetStateSetSize())); + state0_.template Reserve(stateCount_); + state1_.template Reserve(stateCount_); + } + } + + SizeType NewState(SizeType out, SizeType out1, unsigned codepoint) { + State* s = states_.template Push(); + s->out = out; + s->out1 = out1; + s->codepoint = codepoint; + s->rangeStart = kRegexInvalidRange; + return stateCount_++; + } + + void PushOperand(Stack& operandStack, unsigned codepoint) { + SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, codepoint); + *operandStack.template Push() = Frag(s, s, s); + } + + void ImplicitConcatenation(Stack& atomCountStack, Stack& operatorStack) { + if (*atomCountStack.template Top()) + *operatorStack.template Push() = kConcatenation; + (*atomCountStack.template Top())++; + } + + SizeType Append(SizeType l1, SizeType l2) { + SizeType old = l1; + while (GetState(l1).out != kRegexInvalidState) + l1 = GetState(l1).out; + GetState(l1).out = l2; + return old; + } + + void Patch(SizeType l, SizeType s) { + for (SizeType next; l != kRegexInvalidState; l = next) { + next = GetState(l).out; + GetState(l).out = s; + } + } + + bool Eval(Stack& operandStack, Operator op) { + switch (op) { + case kConcatenation: + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag) * 2); + { + Frag e2 = *operandStack.template Pop(1); + Frag e1 = *operandStack.template Pop(1); + Patch(e1.out, e2.start); + *operandStack.template Push() = Frag(e1.start, e2.out, Min(e1.minIndex, e2.minIndex)); + } + return true; + + case kAlternation: + if (operandStack.GetSize() >= sizeof(Frag) * 2) { + Frag e2 = *operandStack.template Pop(1); + Frag e1 = *operandStack.template Pop(1); + SizeType s = NewState(e1.start, e2.start, 0); + *operandStack.template Push() = Frag(s, Append(e1.out, e2.out), Min(e1.minIndex, e2.minIndex)); + return true; + } + return false; + + case kZeroOrOne: + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + *operandStack.template Push() = Frag(s, Append(e.out, s), e.minIndex); + return true; + } + return false; + + case kZeroOrMore: + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + Patch(e.out, s); + *operandStack.template Push() = Frag(s, s, e.minIndex); + return true; + } + return false; + + default: + RAPIDJSON_ASSERT(op == kOneOrMore); + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + Patch(e.out, s); + *operandStack.template Push() = Frag(e.start, s, e.minIndex); + return true; + } + return false; + } + } + + bool EvalQuantifier(Stack& operandStack, unsigned n, unsigned m) { + RAPIDJSON_ASSERT(n <= m); + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag)); + + if (n == 0) { + if (m == 0) // a{0} not support + return false; + else if (m == kInfinityQuantifier) + Eval(operandStack, kZeroOrMore); // a{0,} -> a* + else { + Eval(operandStack, kZeroOrOne); // a{0,5} -> a? + for (unsigned i = 0; i < m - 1; i++) + CloneTopOperand(operandStack); // a{0,5} -> a? a? a? a? a? + for (unsigned i = 0; i < m - 1; i++) + Eval(operandStack, kConcatenation); // a{0,5} -> a?a?a?a?a? + } + return true; + } + + for (unsigned i = 0; i < n - 1; i++) // a{3} -> a a a + CloneTopOperand(operandStack); + + if (m == kInfinityQuantifier) + Eval(operandStack, kOneOrMore); // a{3,} -> a a a+ + else if (m > n) { + CloneTopOperand(operandStack); // a{3,5} -> a a a a + Eval(operandStack, kZeroOrOne); // a{3,5} -> a a a a? + for (unsigned i = n; i < m - 1; i++) + CloneTopOperand(operandStack); // a{3,5} -> a a a a? a? + for (unsigned i = n; i < m; i++) + Eval(operandStack, kConcatenation); // a{3,5} -> a a aa?a? + } + + for (unsigned i = 0; i < n - 1; i++) + Eval(operandStack, kConcatenation); // a{3} -> aaa, a{3,} -> aaa+, a{3.5} -> aaaa?a? + + return true; + } + + static SizeType Min(SizeType a, SizeType b) { return a < b ? a : b; } + + void CloneTopOperand(Stack& operandStack) { + const Frag src = *operandStack.template Top(); // Copy constructor to prevent invalidation + SizeType count = stateCount_ - src.minIndex; // Assumes top operand contains states in [src->minIndex, stateCount_) + State* s = states_.template Push(count); + memcpy(s, &GetState(src.minIndex), count * sizeof(State)); + for (SizeType j = 0; j < count; j++) { + if (s[j].out != kRegexInvalidState) + s[j].out += count; + if (s[j].out1 != kRegexInvalidState) + s[j].out1 += count; + } + *operandStack.template Push() = Frag(src.start + count, src.out + count, src.minIndex + count); + stateCount_ += count; + } + + template + bool ParseUnsigned(DecodedStream& ds, unsigned* u) { + unsigned r = 0; + if (ds.Peek() < '0' || ds.Peek() > '9') + return false; + while (ds.Peek() >= '0' && ds.Peek() <= '9') { + if (r >= 429496729 && ds.Peek() > '5') // 2^32 - 1 = 4294967295 + return false; // overflow + r = r * 10 + (ds.Take() - '0'); + } + *u = r; + return true; + } + + template + bool ParseRange(DecodedStream& ds, SizeType* range) { + bool isBegin = true; + bool negate = false; + int step = 0; + SizeType start = kRegexInvalidRange; + SizeType current = kRegexInvalidRange; + unsigned codepoint; + while ((codepoint = ds.Take()) != 0) { + if (isBegin) { + isBegin = false; + if (codepoint == '^') { + negate = true; + continue; + } + } + + switch (codepoint) { + case ']': + if (start == kRegexInvalidRange) + return false; // Error: nothing inside [] + if (step == 2) { // Add trailing '-' + SizeType r = NewRange('-'); + RAPIDJSON_ASSERT(current != kRegexInvalidRange); + GetRange(current).next = r; + } + if (negate) + GetRange(start).start |= kRangeNegationFlag; + *range = start; + return true; + + case '\\': + if (ds.Peek() == 'b') { + ds.Take(); + codepoint = 0x0008; // Escape backspace character + } + else if (!CharacterEscape(ds, &codepoint)) + return false; + // fall through to default + + default: + switch (step) { + case 1: + if (codepoint == '-') { + step++; + break; + } + // fall through to step 0 for other characters + + case 0: + { + SizeType r = NewRange(codepoint); + if (current != kRegexInvalidRange) + GetRange(current).next = r; + if (start == kRegexInvalidRange) + start = r; + current = r; + } + step = 1; + break; + + default: + RAPIDJSON_ASSERT(step == 2); + GetRange(current).end = codepoint; + step = 0; + } + } + } + return false; + } + + SizeType NewRange(unsigned codepoint) { + Range* r = ranges_.template Push(); + r->start = r->end = codepoint; + r->next = kRegexInvalidRange; + return rangeCount_++; + } + + template + bool CharacterEscape(DecodedStream& ds, unsigned* escapedCodepoint) { + unsigned codepoint; + switch (codepoint = ds.Take()) { + case '^': + case '$': + case '|': + case '(': + case ')': + case '?': + case '*': + case '+': + case '.': + case '[': + case ']': + case '{': + case '}': + case '\\': + *escapedCodepoint = codepoint; return true; + case 'f': *escapedCodepoint = 0x000C; return true; + case 'n': *escapedCodepoint = 0x000A; return true; + case 'r': *escapedCodepoint = 0x000D; return true; + case 't': *escapedCodepoint = 0x0009; return true; + case 'v': *escapedCodepoint = 0x000B; return true; + default: + return false; // Unsupported escape character + } + } + + template + bool SearchWithAnchoring(InputStream& is, bool anchorBegin, bool anchorEnd) const { + RAPIDJSON_ASSERT(IsValid()); + DecodedStream ds(is); + + state0_.Clear(); + Stack *current = &state0_, *next = &state1_; + const size_t stateSetSize = GetStateSetSize(); + std::memset(stateSet_, 0, stateSetSize); + + bool matched = AddState(*current, root_); + unsigned codepoint; + while (!current->Empty() && (codepoint = ds.Take()) != 0) { + std::memset(stateSet_, 0, stateSetSize); + next->Clear(); + matched = false; + for (const SizeType* s = current->template Bottom(); s != current->template End(); ++s) { + const State& sr = GetState(*s); + if (sr.codepoint == codepoint || + sr.codepoint == kAnyCharacterClass || + (sr.codepoint == kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint))) + { + matched = AddState(*next, sr.out) || matched; + if (!anchorEnd && matched) + return true; + } + if (!anchorBegin) + AddState(*next, root_); + } + internal::Swap(current, next); + } + + return matched; + } + + size_t GetStateSetSize() const { + return (stateCount_ + 31) / 32 * 4; + } + + // Return whether the added states is a match state + bool AddState(Stack& l, SizeType index) const { + RAPIDJSON_ASSERT(index != kRegexInvalidState); + + const State& s = GetState(index); + if (s.out1 != kRegexInvalidState) { // Split + bool matched = AddState(l, s.out); + return AddState(l, s.out1) || matched; + } + else if (!(stateSet_[index >> 5] & (1 << (index & 31)))) { + stateSet_[index >> 5] |= (1 << (index & 31)); + *l.template PushUnsafe() = index; + } + return s.out == kRegexInvalidState; // by using PushUnsafe() above, we can ensure s is not validated due to reallocation. + } + + bool MatchRange(SizeType rangeIndex, unsigned codepoint) const { + bool yes = (GetRange(rangeIndex).start & kRangeNegationFlag) == 0; + while (rangeIndex != kRegexInvalidRange) { + const Range& r = GetRange(rangeIndex); + if (codepoint >= (r.start & ~kRangeNegationFlag) && codepoint <= r.end) + return yes; + rangeIndex = r.next; + } + return !yes; + } + + Stack states_; + Stack ranges_; + SizeType root_; + SizeType stateCount_; + SizeType rangeCount_; + + static const unsigned kInfinityQuantifier = ~0u; + + // For SearchWithAnchoring() + uint32_t* stateSet_; // allocated by states_.GetAllocator() + mutable Stack state0_; + mutable Stack state1_; + bool anchorBegin_; + bool anchorEnd_; +}; + +typedef GenericRegex > Regex; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_REGEX_H_ diff --git a/src/3rdparty/rapidjson/internal/stack.h b/src/3rdparty/rapidjson/internal/stack.h new file mode 100644 index 00000000..022c9aab --- /dev/null +++ b/src/3rdparty/rapidjson/internal/stack.h @@ -0,0 +1,230 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_STACK_H_ +#define RAPIDJSON_INTERNAL_STACK_H_ + +#include "../allocators.h" +#include "swap.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +/////////////////////////////////////////////////////////////////////////////// +// Stack + +//! A type-unsafe stack for storing different types of data. +/*! \tparam Allocator Allocator for allocating stack memory. +*/ +template +class Stack { +public: + // Optimization note: Do not allocate memory for stack_ in constructor. + // Do it lazily when first Push() -> Expand() -> Resize(). + Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) { + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Stack(Stack&& rhs) + : allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + stack_(rhs.stack_), + stackTop_(rhs.stackTop_), + stackEnd_(rhs.stackEnd_), + initialCapacity_(rhs.initialCapacity_) + { + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.stack_ = 0; + rhs.stackTop_ = 0; + rhs.stackEnd_ = 0; + rhs.initialCapacity_ = 0; + } +#endif + + ~Stack() { + Destroy(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Stack& operator=(Stack&& rhs) { + if (&rhs != this) + { + Destroy(); + + allocator_ = rhs.allocator_; + ownAllocator_ = rhs.ownAllocator_; + stack_ = rhs.stack_; + stackTop_ = rhs.stackTop_; + stackEnd_ = rhs.stackEnd_; + initialCapacity_ = rhs.initialCapacity_; + + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.stack_ = 0; + rhs.stackTop_ = 0; + rhs.stackEnd_ = 0; + rhs.initialCapacity_ = 0; + } + return *this; + } +#endif + + void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT { + internal::Swap(allocator_, rhs.allocator_); + internal::Swap(ownAllocator_, rhs.ownAllocator_); + internal::Swap(stack_, rhs.stack_); + internal::Swap(stackTop_, rhs.stackTop_); + internal::Swap(stackEnd_, rhs.stackEnd_); + internal::Swap(initialCapacity_, rhs.initialCapacity_); + } + + void Clear() { stackTop_ = stack_; } + + void ShrinkToFit() { + if (Empty()) { + // If the stack is empty, completely deallocate the memory. + Allocator::Free(stack_); + stack_ = 0; + stackTop_ = 0; + stackEnd_ = 0; + } + else + Resize(GetSize()); + } + + // Optimization note: try to minimize the size of this function for force inline. + // Expansion is run very infrequently, so it is moved to another (probably non-inline) function. + template + RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) { + // Expand the stack if needed + if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_)) + Expand(count); + } + + template + RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) { + Reserve(count); + return PushUnsafe(count); + } + + template + RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) { + RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_); + T* ret = reinterpret_cast(stackTop_); + stackTop_ += sizeof(T) * count; + return ret; + } + + template + T* Pop(size_t count) { + RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T)); + stackTop_ -= count * sizeof(T); + return reinterpret_cast(stackTop_); + } + + template + T* Top() { + RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); + return reinterpret_cast(stackTop_ - sizeof(T)); + } + + template + const T* Top() const { + RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); + return reinterpret_cast(stackTop_ - sizeof(T)); + } + + template + T* End() { return reinterpret_cast(stackTop_); } + + template + const T* End() const { return reinterpret_cast(stackTop_); } + + template + T* Bottom() { return reinterpret_cast(stack_); } + + template + const T* Bottom() const { return reinterpret_cast(stack_); } + + bool HasAllocator() const { + return allocator_ != 0; + } + + Allocator& GetAllocator() { + RAPIDJSON_ASSERT(allocator_); + return *allocator_; + } + + bool Empty() const { return stackTop_ == stack_; } + size_t GetSize() const { return static_cast(stackTop_ - stack_); } + size_t GetCapacity() const { return static_cast(stackEnd_ - stack_); } + +private: + template + void Expand(size_t count) { + // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity. + size_t newCapacity; + if (stack_ == 0) { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + newCapacity = initialCapacity_; + } else { + newCapacity = GetCapacity(); + newCapacity += (newCapacity + 1) / 2; + } + size_t newSize = GetSize() + sizeof(T) * count; + if (newCapacity < newSize) + newCapacity = newSize; + + Resize(newCapacity); + } + + void Resize(size_t newCapacity) { + const size_t size = GetSize(); // Backup the current size + stack_ = static_cast(allocator_->Realloc(stack_, GetCapacity(), newCapacity)); + stackTop_ = stack_ + size; + stackEnd_ = stack_ + newCapacity; + } + + void Destroy() { + Allocator::Free(stack_); + RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack + } + + // Prohibit copy constructor & assignment operator. + Stack(const Stack&); + Stack& operator=(const Stack&); + + Allocator* allocator_; + Allocator* ownAllocator_; + char *stack_; + char *stackTop_; + char *stackEnd_; + size_t initialCapacity_; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_STACK_H_ diff --git a/src/3rdparty/rapidjson/internal/strfunc.h b/src/3rdparty/rapidjson/internal/strfunc.h new file mode 100644 index 00000000..2edfae52 --- /dev/null +++ b/src/3rdparty/rapidjson/internal/strfunc.h @@ -0,0 +1,55 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_STRFUNC_H_ +#define RAPIDJSON_INTERNAL_STRFUNC_H_ + +#include "../stream.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Custom strlen() which works on different character types. +/*! \tparam Ch Character type (e.g. char, wchar_t, short) + \param s Null-terminated input string. + \return Number of characters in the string. + \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints. +*/ +template +inline SizeType StrLen(const Ch* s) { + const Ch* p = s; + while (*p) ++p; + return SizeType(p - s); +} + +//! Returns number of code points in a encoded string. +template +bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) { + GenericStringStream is(s); + const typename Encoding::Ch* end = s + length; + SizeType count = 0; + while (is.src_ < end) { + unsigned codepoint; + if (!Encoding::Decode(is, &codepoint)) + return false; + count++; + } + *outCount = count; + return true; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_INTERNAL_STRFUNC_H_ diff --git a/src/3rdparty/rapidjson/internal/strtod.h b/src/3rdparty/rapidjson/internal/strtod.h new file mode 100644 index 00000000..289c413b --- /dev/null +++ b/src/3rdparty/rapidjson/internal/strtod.h @@ -0,0 +1,269 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_STRTOD_ +#define RAPIDJSON_STRTOD_ + +#include "ieee754.h" +#include "biginteger.h" +#include "diyfp.h" +#include "pow10.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline double FastPath(double significand, int exp) { + if (exp < -308) + return 0.0; + else if (exp >= 0) + return significand * internal::Pow10(exp); + else + return significand / internal::Pow10(-exp); +} + +inline double StrtodNormalPrecision(double d, int p) { + if (p < -308) { + // Prevent expSum < -308, making Pow10(p) = 0 + d = FastPath(d, -308); + d = FastPath(d, p + 308); + } + else + d = FastPath(d, p); + return d; +} + +template +inline T Min3(T a, T b, T c) { + T m = a; + if (m > b) m = b; + if (m > c) m = c; + return m; +} + +inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) { + const Double db(b); + const uint64_t bInt = db.IntegerSignificand(); + const int bExp = db.IntegerExponent(); + const int hExp = bExp - 1; + + int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0; + + // Adjust for decimal exponent + if (dExp >= 0) { + dS_Exp2 += dExp; + dS_Exp5 += dExp; + } + else { + bS_Exp2 -= dExp; + bS_Exp5 -= dExp; + hS_Exp2 -= dExp; + hS_Exp5 -= dExp; + } + + // Adjust for binary exponent + if (bExp >= 0) + bS_Exp2 += bExp; + else { + dS_Exp2 -= bExp; + hS_Exp2 -= bExp; + } + + // Adjust for half ulp exponent + if (hExp >= 0) + hS_Exp2 += hExp; + else { + dS_Exp2 -= hExp; + bS_Exp2 -= hExp; + } + + // Remove common power of two factor from all three scaled values + int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2); + dS_Exp2 -= common_Exp2; + bS_Exp2 -= common_Exp2; + hS_Exp2 -= common_Exp2; + + BigInteger dS = d; + dS.MultiplyPow5(static_cast(dS_Exp5)) <<= static_cast(dS_Exp2); + + BigInteger bS(bInt); + bS.MultiplyPow5(static_cast(bS_Exp5)) <<= static_cast(bS_Exp2); + + BigInteger hS(1); + hS.MultiplyPow5(static_cast(hS_Exp5)) <<= static_cast(hS_Exp2); + + BigInteger delta(0); + dS.Difference(bS, &delta); + + return delta.Compare(hS); +} + +inline bool StrtodFast(double d, int p, double* result) { + // Use fast path for string-to-double conversion if possible + // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/ + if (p > 22 && p < 22 + 16) { + // Fast Path Cases In Disguise + d *= internal::Pow10(p - 22); + p = 22; + } + + if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1 + *result = FastPath(d, p); + return true; + } + else + return false; +} + +// Compute an approximation and see if it is within 1/2 ULP +inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosition, int exp, double* result) { + uint64_t significand = 0; + size_t i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999 + for (; i < length; i++) { + if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || + (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5')) + break; + significand = significand * 10u + static_cast(decimals[i] - '0'); + } + + if (i < length && decimals[i] >= '5') // Rounding + significand++; + + size_t remaining = length - i; + const unsigned kUlpShift = 3; + const unsigned kUlp = 1 << kUlpShift; + int64_t error = (remaining == 0) ? 0 : kUlp / 2; + + DiyFp v(significand, 0); + v = v.Normalize(); + error <<= -v.e; + + const int dExp = static_cast(decimalPosition) - static_cast(i) + exp; + + int actualExp; + DiyFp cachedPower = GetCachedPower10(dExp, &actualExp); + if (actualExp != dExp) { + static const DiyFp kPow10[] = { + DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 00000000), -60), // 10^1 + DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 00000000), -57), // 10^2 + DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 00000000), -54), // 10^3 + DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 00000000), -50), // 10^4 + DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 00000000), -47), // 10^5 + DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 00000000), -44), // 10^6 + DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 00000000), -40) // 10^7 + }; + int adjustment = dExp - actualExp - 1; + RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7); + v = v * kPow10[adjustment]; + if (length + static_cast(adjustment)> 19u) // has more digits than decimal digits in 64-bit + error += kUlp / 2; + } + + v = v * cachedPower; + + error += kUlp + (error == 0 ? 0 : 1); + + const int oldExp = v.e; + v = v.Normalize(); + error <<= oldExp - v.e; + + const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e); + unsigned precisionSize = 64 - effectiveSignificandSize; + if (precisionSize + kUlpShift >= 64) { + unsigned scaleExp = (precisionSize + kUlpShift) - 63; + v.f >>= scaleExp; + v.e += scaleExp; + error = (error >> scaleExp) + 1 + static_cast(kUlp); + precisionSize -= scaleExp; + } + + DiyFp rounded(v.f >> precisionSize, v.e + static_cast(precisionSize)); + const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp; + const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp; + if (precisionBits >= halfWay + static_cast(error)) { + rounded.f++; + if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340) + rounded.f >>= 1; + rounded.e++; + } + } + + *result = rounded.ToDouble(); + + return halfWay - static_cast(error) >= precisionBits || precisionBits >= halfWay + static_cast(error); +} + +inline double StrtodBigInteger(double approx, const char* decimals, size_t length, size_t decimalPosition, int exp) { + const BigInteger dInt(decimals, length); + const int dExp = static_cast(decimalPosition) - static_cast(length) + exp; + Double a(approx); + int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp); + if (cmp < 0) + return a.Value(); // within half ULP + else if (cmp == 0) { + // Round towards even + if (a.Significand() & 1) + return a.NextPositiveDouble(); + else + return a.Value(); + } + else // adjustment + return a.NextPositiveDouble(); +} + +inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t length, size_t decimalPosition, int exp) { + RAPIDJSON_ASSERT(d >= 0.0); + RAPIDJSON_ASSERT(length >= 1); + + double result; + if (StrtodFast(d, p, &result)) + return result; + + // Trim leading zeros + while (*decimals == '0' && length > 1) { + length--; + decimals++; + decimalPosition--; + } + + // Trim trailing zeros + while (decimals[length - 1] == '0' && length > 1) { + length--; + decimalPosition--; + exp++; + } + + // Trim right-most digits + const int kMaxDecimalDigit = 780; + if (static_cast(length) > kMaxDecimalDigit) { + int delta = (static_cast(length) - kMaxDecimalDigit); + exp += delta; + decimalPosition -= static_cast(delta); + length = kMaxDecimalDigit; + } + + // If too small, underflow to zero + if (int(length) + exp < -324) + return 0.0; + + if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result)) + return result; + + // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison + return StrtodBigInteger(result, decimals, length, decimalPosition, exp); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STRTOD_ diff --git a/src/3rdparty/rapidjson/internal/swap.h b/src/3rdparty/rapidjson/internal/swap.h new file mode 100644 index 00000000..666e49f9 --- /dev/null +++ b/src/3rdparty/rapidjson/internal/swap.h @@ -0,0 +1,46 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_SWAP_H_ +#define RAPIDJSON_INTERNAL_SWAP_H_ + +#include "../rapidjson.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Custom swap() to avoid dependency on C++ header +/*! \tparam T Type of the arguments to swap, should be instantiated with primitive C++ types only. + \note This has the same semantics as std::swap(). +*/ +template +inline void Swap(T& a, T& b) RAPIDJSON_NOEXCEPT { + T tmp = a; + a = b; + b = tmp; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_SWAP_H_ diff --git a/src/3rdparty/rapidjson/istreamwrapper.h b/src/3rdparty/rapidjson/istreamwrapper.h new file mode 100644 index 00000000..f5fe2897 --- /dev/null +++ b/src/3rdparty/rapidjson/istreamwrapper.h @@ -0,0 +1,115 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ISTREAMWRAPPER_H_ +#define RAPIDJSON_ISTREAMWRAPPER_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of \c std::basic_istream into RapidJSON's Stream concept. +/*! + The classes can be wrapped including but not limited to: + + - \c std::istringstream + - \c std::stringstream + - \c std::wistringstream + - \c std::wstringstream + - \c std::ifstream + - \c std::fstream + - \c std::wifstream + - \c std::wfstream + + \tparam StreamType Class derived from \c std::basic_istream. +*/ + +template +class BasicIStreamWrapper { +public: + typedef typename StreamType::char_type Ch; + BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {} + + Ch Peek() const { + typename StreamType::int_type c = stream_.peek(); + return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast(c) : '\0'; + } + + Ch Take() { + typename StreamType::int_type c = stream_.get(); + if (RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) { + count_++; + return static_cast(c); + } + else + return '\0'; + } + + // tellg() may return -1 when failed. So we count by ourself. + size_t Tell() const { return count_; } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream. + int i; + bool hasError = false; + for (i = 0; i < 4; ++i) { + typename StreamType::int_type c = stream_.get(); + if (c == StreamType::traits_type::eof()) { + hasError = true; + stream_.clear(); + break; + } + peekBuffer_[i] = static_cast(c); + } + for (--i; i >= 0; --i) + stream_.putback(peekBuffer_[i]); + return !hasError ? peekBuffer_ : 0; + } + +private: + BasicIStreamWrapper(const BasicIStreamWrapper&); + BasicIStreamWrapper& operator=(const BasicIStreamWrapper&); + + StreamType& stream_; + size_t count_; //!< Number of characters read. Note: + mutable Ch peekBuffer_[4]; +}; + +typedef BasicIStreamWrapper IStreamWrapper; +typedef BasicIStreamWrapper WIStreamWrapper; + +#if defined(__clang__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ISTREAMWRAPPER_H_ diff --git a/src/3rdparty/rapidjson/memorybuffer.h b/src/3rdparty/rapidjson/memorybuffer.h new file mode 100644 index 00000000..39bee1de --- /dev/null +++ b/src/3rdparty/rapidjson/memorybuffer.h @@ -0,0 +1,70 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_MEMORYBUFFER_H_ +#define RAPIDJSON_MEMORYBUFFER_H_ + +#include "stream.h" +#include "internal/stack.h" + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory output byte stream. +/*! + This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream. + + It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file. + + Differences between MemoryBuffer and StringBuffer: + 1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer. + 2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator. + + \tparam Allocator type for allocating memory buffer. + \note implements Stream concept +*/ +template +struct GenericMemoryBuffer { + typedef char Ch; // byte + + GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} + + void Put(Ch c) { *stack_.template Push() = c; } + void Flush() {} + + void Clear() { stack_.Clear(); } + void ShrinkToFit() { stack_.ShrinkToFit(); } + Ch* Push(size_t count) { return stack_.template Push(count); } + void Pop(size_t count) { stack_.template Pop(count); } + + const Ch* GetBuffer() const { + return stack_.template Bottom(); + } + + size_t GetSize() const { return stack_.GetSize(); } + + static const size_t kDefaultCapacity = 256; + mutable internal::Stack stack_; +}; + +typedef GenericMemoryBuffer<> MemoryBuffer; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) { + std::memset(memoryBuffer.stack_.Push(n), c, n * sizeof(c)); +} + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_MEMORYBUFFER_H_ diff --git a/src/3rdparty/rapidjson/memorystream.h b/src/3rdparty/rapidjson/memorystream.h new file mode 100644 index 00000000..1d71d8a4 --- /dev/null +++ b/src/3rdparty/rapidjson/memorystream.h @@ -0,0 +1,71 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_MEMORYSTREAM_H_ +#define RAPIDJSON_MEMORYSTREAM_H_ + +#include "stream.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(unreachable-code) +RAPIDJSON_DIAG_OFF(missing-noreturn) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory input byte stream. +/*! + This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream. + + It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file. + + Differences between MemoryStream and StringStream: + 1. StringStream has encoding but MemoryStream is a byte stream. + 2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source. + 3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4(). + \note implements Stream concept +*/ +struct MemoryStream { + typedef char Ch; // byte + + MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {} + + Ch Peek() const { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_; } + Ch Take() { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_++; } + size_t Tell() const { return static_cast(src_ - begin_); } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return Tell() + 4 <= size_ ? src_ : 0; + } + + const Ch* src_; //!< Current read position. + const Ch* begin_; //!< Original head of the string. + const Ch* end_; //!< End of stream. + size_t size_; //!< Size of the stream. +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_MEMORYBUFFER_H_ diff --git a/src/3rdparty/rapidjson/msinttypes/inttypes.h b/src/3rdparty/rapidjson/msinttypes/inttypes.h new file mode 100644 index 00000000..18111286 --- /dev/null +++ b/src/3rdparty/rapidjson/msinttypes/inttypes.h @@ -0,0 +1,316 @@ +// ISO C9x compliant inttypes.h for Microsoft Visual Studio +// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 +// +// Copyright (c) 2006-2013 Alexander Chemeris +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. Neither the name of the product nor the names of its contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +/////////////////////////////////////////////////////////////////////////////// + +// The above software in this distribution may have been modified by +// THL A29 Limited ("Tencent Modifications"). +// All Tencent Modifications are Copyright (C) 2015 THL A29 Limited. + +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_INTTYPES_H_ // [ +#define _MSC_INTTYPES_H_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +#include "stdint.h" + +// miloyip: VC supports inttypes.h since VC2013 +#if _MSC_VER >= 1800 +#include +#else + +// 7.8 Format conversion of integer types + +typedef struct { + intmax_t quot; + intmax_t rem; +} imaxdiv_t; + +// 7.8.1 Macros for format specifiers + +#if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) // [ See footnote 185 at page 198 + +// The fprintf macros for signed integers are: +#define PRId8 "d" +#define PRIi8 "i" +#define PRIdLEAST8 "d" +#define PRIiLEAST8 "i" +#define PRIdFAST8 "d" +#define PRIiFAST8 "i" + +#define PRId16 "hd" +#define PRIi16 "hi" +#define PRIdLEAST16 "hd" +#define PRIiLEAST16 "hi" +#define PRIdFAST16 "hd" +#define PRIiFAST16 "hi" + +#define PRId32 "I32d" +#define PRIi32 "I32i" +#define PRIdLEAST32 "I32d" +#define PRIiLEAST32 "I32i" +#define PRIdFAST32 "I32d" +#define PRIiFAST32 "I32i" + +#define PRId64 "I64d" +#define PRIi64 "I64i" +#define PRIdLEAST64 "I64d" +#define PRIiLEAST64 "I64i" +#define PRIdFAST64 "I64d" +#define PRIiFAST64 "I64i" + +#define PRIdMAX "I64d" +#define PRIiMAX "I64i" + +#define PRIdPTR "Id" +#define PRIiPTR "Ii" + +// The fprintf macros for unsigned integers are: +#define PRIo8 "o" +#define PRIu8 "u" +#define PRIx8 "x" +#define PRIX8 "X" +#define PRIoLEAST8 "o" +#define PRIuLEAST8 "u" +#define PRIxLEAST8 "x" +#define PRIXLEAST8 "X" +#define PRIoFAST8 "o" +#define PRIuFAST8 "u" +#define PRIxFAST8 "x" +#define PRIXFAST8 "X" + +#define PRIo16 "ho" +#define PRIu16 "hu" +#define PRIx16 "hx" +#define PRIX16 "hX" +#define PRIoLEAST16 "ho" +#define PRIuLEAST16 "hu" +#define PRIxLEAST16 "hx" +#define PRIXLEAST16 "hX" +#define PRIoFAST16 "ho" +#define PRIuFAST16 "hu" +#define PRIxFAST16 "hx" +#define PRIXFAST16 "hX" + +#define PRIo32 "I32o" +#define PRIu32 "I32u" +#define PRIx32 "I32x" +#define PRIX32 "I32X" +#define PRIoLEAST32 "I32o" +#define PRIuLEAST32 "I32u" +#define PRIxLEAST32 "I32x" +#define PRIXLEAST32 "I32X" +#define PRIoFAST32 "I32o" +#define PRIuFAST32 "I32u" +#define PRIxFAST32 "I32x" +#define PRIXFAST32 "I32X" + +#define PRIo64 "I64o" +#define PRIu64 "I64u" +#define PRIx64 "I64x" +#define PRIX64 "I64X" +#define PRIoLEAST64 "I64o" +#define PRIuLEAST64 "I64u" +#define PRIxLEAST64 "I64x" +#define PRIXLEAST64 "I64X" +#define PRIoFAST64 "I64o" +#define PRIuFAST64 "I64u" +#define PRIxFAST64 "I64x" +#define PRIXFAST64 "I64X" + +#define PRIoMAX "I64o" +#define PRIuMAX "I64u" +#define PRIxMAX "I64x" +#define PRIXMAX "I64X" + +#define PRIoPTR "Io" +#define PRIuPTR "Iu" +#define PRIxPTR "Ix" +#define PRIXPTR "IX" + +// The fscanf macros for signed integers are: +#define SCNd8 "d" +#define SCNi8 "i" +#define SCNdLEAST8 "d" +#define SCNiLEAST8 "i" +#define SCNdFAST8 "d" +#define SCNiFAST8 "i" + +#define SCNd16 "hd" +#define SCNi16 "hi" +#define SCNdLEAST16 "hd" +#define SCNiLEAST16 "hi" +#define SCNdFAST16 "hd" +#define SCNiFAST16 "hi" + +#define SCNd32 "ld" +#define SCNi32 "li" +#define SCNdLEAST32 "ld" +#define SCNiLEAST32 "li" +#define SCNdFAST32 "ld" +#define SCNiFAST32 "li" + +#define SCNd64 "I64d" +#define SCNi64 "I64i" +#define SCNdLEAST64 "I64d" +#define SCNiLEAST64 "I64i" +#define SCNdFAST64 "I64d" +#define SCNiFAST64 "I64i" + +#define SCNdMAX "I64d" +#define SCNiMAX "I64i" + +#ifdef _WIN64 // [ +# define SCNdPTR "I64d" +# define SCNiPTR "I64i" +#else // _WIN64 ][ +# define SCNdPTR "ld" +# define SCNiPTR "li" +#endif // _WIN64 ] + +// The fscanf macros for unsigned integers are: +#define SCNo8 "o" +#define SCNu8 "u" +#define SCNx8 "x" +#define SCNX8 "X" +#define SCNoLEAST8 "o" +#define SCNuLEAST8 "u" +#define SCNxLEAST8 "x" +#define SCNXLEAST8 "X" +#define SCNoFAST8 "o" +#define SCNuFAST8 "u" +#define SCNxFAST8 "x" +#define SCNXFAST8 "X" + +#define SCNo16 "ho" +#define SCNu16 "hu" +#define SCNx16 "hx" +#define SCNX16 "hX" +#define SCNoLEAST16 "ho" +#define SCNuLEAST16 "hu" +#define SCNxLEAST16 "hx" +#define SCNXLEAST16 "hX" +#define SCNoFAST16 "ho" +#define SCNuFAST16 "hu" +#define SCNxFAST16 "hx" +#define SCNXFAST16 "hX" + +#define SCNo32 "lo" +#define SCNu32 "lu" +#define SCNx32 "lx" +#define SCNX32 "lX" +#define SCNoLEAST32 "lo" +#define SCNuLEAST32 "lu" +#define SCNxLEAST32 "lx" +#define SCNXLEAST32 "lX" +#define SCNoFAST32 "lo" +#define SCNuFAST32 "lu" +#define SCNxFAST32 "lx" +#define SCNXFAST32 "lX" + +#define SCNo64 "I64o" +#define SCNu64 "I64u" +#define SCNx64 "I64x" +#define SCNX64 "I64X" +#define SCNoLEAST64 "I64o" +#define SCNuLEAST64 "I64u" +#define SCNxLEAST64 "I64x" +#define SCNXLEAST64 "I64X" +#define SCNoFAST64 "I64o" +#define SCNuFAST64 "I64u" +#define SCNxFAST64 "I64x" +#define SCNXFAST64 "I64X" + +#define SCNoMAX "I64o" +#define SCNuMAX "I64u" +#define SCNxMAX "I64x" +#define SCNXMAX "I64X" + +#ifdef _WIN64 // [ +# define SCNoPTR "I64o" +# define SCNuPTR "I64u" +# define SCNxPTR "I64x" +# define SCNXPTR "I64X" +#else // _WIN64 ][ +# define SCNoPTR "lo" +# define SCNuPTR "lu" +# define SCNxPTR "lx" +# define SCNXPTR "lX" +#endif // _WIN64 ] + +#endif // __STDC_FORMAT_MACROS ] + +// 7.8.2 Functions for greatest-width integer types + +// 7.8.2.1 The imaxabs function +#define imaxabs _abs64 + +// 7.8.2.2 The imaxdiv function + +// This is modified version of div() function from Microsoft's div.c found +// in %MSVC.NET%\crt\src\div.c +#ifdef STATIC_IMAXDIV // [ +static +#else // STATIC_IMAXDIV ][ +_inline +#endif // STATIC_IMAXDIV ] +imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom) +{ + imaxdiv_t result; + + result.quot = numer / denom; + result.rem = numer % denom; + + if (numer < 0 && result.rem > 0) { + // did division wrong; must fix up + ++result.quot; + result.rem -= denom; + } + + return result; +} + +// 7.8.2.3 The strtoimax and strtoumax functions +#define strtoimax _strtoi64 +#define strtoumax _strtoui64 + +// 7.8.2.4 The wcstoimax and wcstoumax functions +#define wcstoimax _wcstoi64 +#define wcstoumax _wcstoui64 + +#endif // _MSC_VER >= 1800 + +#endif // _MSC_INTTYPES_H_ ] diff --git a/src/3rdparty/rapidjson/msinttypes/stdint.h b/src/3rdparty/rapidjson/msinttypes/stdint.h new file mode 100644 index 00000000..3d4477b9 --- /dev/null +++ b/src/3rdparty/rapidjson/msinttypes/stdint.h @@ -0,0 +1,300 @@ +// ISO C9x compliant stdint.h for Microsoft Visual Studio +// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 +// +// Copyright (c) 2006-2013 Alexander Chemeris +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. Neither the name of the product nor the names of its contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +/////////////////////////////////////////////////////////////////////////////// + +// The above software in this distribution may have been modified by +// THL A29 Limited ("Tencent Modifications"). +// All Tencent Modifications are Copyright (C) 2015 THL A29 Limited. + +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_STDINT_H_ // [ +#define _MSC_STDINT_H_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +// miloyip: Originally Visual Studio 2010 uses its own stdint.h. However it generates warning with INT64_C(), so change to use this file for vs2010. +#if _MSC_VER >= 1600 // [ +#include + +#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 + +#undef INT8_C +#undef INT16_C +#undef INT32_C +#undef INT64_C +#undef UINT8_C +#undef UINT16_C +#undef UINT32_C +#undef UINT64_C + +// 7.18.4.1 Macros for minimum-width integer constants + +#define INT8_C(val) val##i8 +#define INT16_C(val) val##i16 +#define INT32_C(val) val##i32 +#define INT64_C(val) val##i64 + +#define UINT8_C(val) val##ui8 +#define UINT16_C(val) val##ui16 +#define UINT32_C(val) val##ui32 +#define UINT64_C(val) val##ui64 + +// 7.18.4.2 Macros for greatest-width integer constants +// These #ifndef's are needed to prevent collisions with . +// Check out Issue 9 for the details. +#ifndef INTMAX_C // [ +# define INTMAX_C INT64_C +#endif // INTMAX_C ] +#ifndef UINTMAX_C // [ +# define UINTMAX_C UINT64_C +#endif // UINTMAX_C ] + +#endif // __STDC_CONSTANT_MACROS ] + +#else // ] _MSC_VER >= 1700 [ + +#include + +// For Visual Studio 6 in C++ mode and for many Visual Studio versions when +// compiling for ARM we have to wrap include with 'extern "C++" {}' +// or compiler would give many errors like this: +// error C2733: second C linkage of overloaded function 'wmemchr' not allowed +#if defined(__cplusplus) && !defined(_M_ARM) +extern "C" { +#endif +# include +#if defined(__cplusplus) && !defined(_M_ARM) +} +#endif + +// Define _W64 macros to mark types changing their size, like intptr_t. +#ifndef _W64 +# if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 +# define _W64 __w64 +# else +# define _W64 +# endif +#endif + + +// 7.18.1 Integer types + +// 7.18.1.1 Exact-width integer types + +// Visual Studio 6 and Embedded Visual C++ 4 doesn't +// realize that, e.g. char has the same size as __int8 +// so we give up on __intX for them. +#if (_MSC_VER < 1300) + typedef signed char int8_t; + typedef signed short int16_t; + typedef signed int int32_t; + typedef unsigned char uint8_t; + typedef unsigned short uint16_t; + typedef unsigned int uint32_t; +#else + typedef signed __int8 int8_t; + typedef signed __int16 int16_t; + typedef signed __int32 int32_t; + typedef unsigned __int8 uint8_t; + typedef unsigned __int16 uint16_t; + typedef unsigned __int32 uint32_t; +#endif +typedef signed __int64 int64_t; +typedef unsigned __int64 uint64_t; + + +// 7.18.1.2 Minimum-width integer types +typedef int8_t int_least8_t; +typedef int16_t int_least16_t; +typedef int32_t int_least32_t; +typedef int64_t int_least64_t; +typedef uint8_t uint_least8_t; +typedef uint16_t uint_least16_t; +typedef uint32_t uint_least32_t; +typedef uint64_t uint_least64_t; + +// 7.18.1.3 Fastest minimum-width integer types +typedef int8_t int_fast8_t; +typedef int16_t int_fast16_t; +typedef int32_t int_fast32_t; +typedef int64_t int_fast64_t; +typedef uint8_t uint_fast8_t; +typedef uint16_t uint_fast16_t; +typedef uint32_t uint_fast32_t; +typedef uint64_t uint_fast64_t; + +// 7.18.1.4 Integer types capable of holding object pointers +#ifdef _WIN64 // [ + typedef signed __int64 intptr_t; + typedef unsigned __int64 uintptr_t; +#else // _WIN64 ][ + typedef _W64 signed int intptr_t; + typedef _W64 unsigned int uintptr_t; +#endif // _WIN64 ] + +// 7.18.1.5 Greatest-width integer types +typedef int64_t intmax_t; +typedef uint64_t uintmax_t; + + +// 7.18.2 Limits of specified-width integer types + +#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259 + +// 7.18.2.1 Limits of exact-width integer types +#define INT8_MIN ((int8_t)_I8_MIN) +#define INT8_MAX _I8_MAX +#define INT16_MIN ((int16_t)_I16_MIN) +#define INT16_MAX _I16_MAX +#define INT32_MIN ((int32_t)_I32_MIN) +#define INT32_MAX _I32_MAX +#define INT64_MIN ((int64_t)_I64_MIN) +#define INT64_MAX _I64_MAX +#define UINT8_MAX _UI8_MAX +#define UINT16_MAX _UI16_MAX +#define UINT32_MAX _UI32_MAX +#define UINT64_MAX _UI64_MAX + +// 7.18.2.2 Limits of minimum-width integer types +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST8_MAX INT8_MAX +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST16_MAX INT16_MAX +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST32_MAX INT32_MAX +#define INT_LEAST64_MIN INT64_MIN +#define INT_LEAST64_MAX INT64_MAX +#define UINT_LEAST8_MAX UINT8_MAX +#define UINT_LEAST16_MAX UINT16_MAX +#define UINT_LEAST32_MAX UINT32_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +// 7.18.2.3 Limits of fastest minimum-width integer types +#define INT_FAST8_MIN INT8_MIN +#define INT_FAST8_MAX INT8_MAX +#define INT_FAST16_MIN INT16_MIN +#define INT_FAST16_MAX INT16_MAX +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST32_MAX INT32_MAX +#define INT_FAST64_MIN INT64_MIN +#define INT_FAST64_MAX INT64_MAX +#define UINT_FAST8_MAX UINT8_MAX +#define UINT_FAST16_MAX UINT16_MAX +#define UINT_FAST32_MAX UINT32_MAX +#define UINT_FAST64_MAX UINT64_MAX + +// 7.18.2.4 Limits of integer types capable of holding object pointers +#ifdef _WIN64 // [ +# define INTPTR_MIN INT64_MIN +# define INTPTR_MAX INT64_MAX +# define UINTPTR_MAX UINT64_MAX +#else // _WIN64 ][ +# define INTPTR_MIN INT32_MIN +# define INTPTR_MAX INT32_MAX +# define UINTPTR_MAX UINT32_MAX +#endif // _WIN64 ] + +// 7.18.2.5 Limits of greatest-width integer types +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +// 7.18.3 Limits of other integer types + +#ifdef _WIN64 // [ +# define PTRDIFF_MIN _I64_MIN +# define PTRDIFF_MAX _I64_MAX +#else // _WIN64 ][ +# define PTRDIFF_MIN _I32_MIN +# define PTRDIFF_MAX _I32_MAX +#endif // _WIN64 ] + +#define SIG_ATOMIC_MIN INT_MIN +#define SIG_ATOMIC_MAX INT_MAX + +#ifndef SIZE_MAX // [ +# ifdef _WIN64 // [ +# define SIZE_MAX _UI64_MAX +# else // _WIN64 ][ +# define SIZE_MAX _UI32_MAX +# endif // _WIN64 ] +#endif // SIZE_MAX ] + +// WCHAR_MIN and WCHAR_MAX are also defined in +#ifndef WCHAR_MIN // [ +# define WCHAR_MIN 0 +#endif // WCHAR_MIN ] +#ifndef WCHAR_MAX // [ +# define WCHAR_MAX _UI16_MAX +#endif // WCHAR_MAX ] + +#define WINT_MIN 0 +#define WINT_MAX _UI16_MAX + +#endif // __STDC_LIMIT_MACROS ] + + +// 7.18.4 Limits of other integer types + +#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 + +// 7.18.4.1 Macros for minimum-width integer constants + +#define INT8_C(val) val##i8 +#define INT16_C(val) val##i16 +#define INT32_C(val) val##i32 +#define INT64_C(val) val##i64 + +#define UINT8_C(val) val##ui8 +#define UINT16_C(val) val##ui16 +#define UINT32_C(val) val##ui32 +#define UINT64_C(val) val##ui64 + +// 7.18.4.2 Macros for greatest-width integer constants +// These #ifndef's are needed to prevent collisions with . +// Check out Issue 9 for the details. +#ifndef INTMAX_C // [ +# define INTMAX_C INT64_C +#endif // INTMAX_C ] +#ifndef UINTMAX_C // [ +# define UINTMAX_C UINT64_C +#endif // UINTMAX_C ] + +#endif // __STDC_CONSTANT_MACROS ] + +#endif // _MSC_VER >= 1600 ] + +#endif // _MSC_STDINT_H_ ] diff --git a/src/3rdparty/rapidjson/ostreamwrapper.h b/src/3rdparty/rapidjson/ostreamwrapper.h new file mode 100644 index 00000000..6f4667c0 --- /dev/null +++ b/src/3rdparty/rapidjson/ostreamwrapper.h @@ -0,0 +1,81 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_OSTREAMWRAPPER_H_ +#define RAPIDJSON_OSTREAMWRAPPER_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of \c std::basic_ostream into RapidJSON's Stream concept. +/*! + The classes can be wrapped including but not limited to: + + - \c std::ostringstream + - \c std::stringstream + - \c std::wpstringstream + - \c std::wstringstream + - \c std::ifstream + - \c std::fstream + - \c std::wofstream + - \c std::wfstream + + \tparam StreamType Class derived from \c std::basic_ostream. +*/ + +template +class BasicOStreamWrapper { +public: + typedef typename StreamType::char_type Ch; + BasicOStreamWrapper(StreamType& stream) : stream_(stream) {} + + void Put(Ch c) { + stream_.put(c); + } + + void Flush() { + stream_.flush(); + } + + // Not implemented + char Peek() const { RAPIDJSON_ASSERT(false); return 0; } + char Take() { RAPIDJSON_ASSERT(false); return 0; } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + BasicOStreamWrapper(const BasicOStreamWrapper&); + BasicOStreamWrapper& operator=(const BasicOStreamWrapper&); + + StreamType& stream_; +}; + +typedef BasicOStreamWrapper OStreamWrapper; +typedef BasicOStreamWrapper WOStreamWrapper; + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_OSTREAMWRAPPER_H_ diff --git a/src/3rdparty/rapidjson/pointer.h b/src/3rdparty/rapidjson/pointer.h new file mode 100644 index 00000000..0206ac1c --- /dev/null +++ b/src/3rdparty/rapidjson/pointer.h @@ -0,0 +1,1358 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_POINTER_H_ +#define RAPIDJSON_POINTER_H_ + +#include "document.h" +#include "internal/itoa.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(switch-enum) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +static const SizeType kPointerInvalidIndex = ~SizeType(0); //!< Represents an invalid index in GenericPointer::Token + +//! Error code of parsing. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericPointer::GenericPointer, GenericPointer::GetParseErrorCode +*/ +enum PointerParseErrorCode { + kPointerParseErrorNone = 0, //!< The parse is successful + + kPointerParseErrorTokenMustBeginWithSolidus, //!< A token must begin with a '/' + kPointerParseErrorInvalidEscape, //!< Invalid escape + kPointerParseErrorInvalidPercentEncoding, //!< Invalid percent encoding in URI fragment + kPointerParseErrorCharacterMustPercentEncode //!< A character must percent encoded in URI fragment +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericPointer + +//! Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator. +/*! + This class implements RFC 6901 "JavaScript Object Notation (JSON) Pointer" + (https://tools.ietf.org/html/rfc6901). + + A JSON pointer is for identifying a specific value in a JSON document + (GenericDocument). It can simplify coding of DOM tree manipulation, because it + can access multiple-level depth of DOM tree with single API call. + + After it parses a string representation (e.g. "/foo/0" or URI fragment + representation (e.g. "#/foo/0") into its internal representation (tokens), + it can be used to resolve a specific value in multiple documents, or sub-tree + of documents. + + Contrary to GenericValue, Pointer can be copy constructed and copy assigned. + Apart from assignment, a Pointer cannot be modified after construction. + + Although Pointer is very convenient, please aware that constructing Pointer + involves parsing and dynamic memory allocation. A special constructor with user- + supplied tokens eliminates these. + + GenericPointer depends on GenericDocument and GenericValue. + + \tparam ValueType The value type of the DOM tree. E.g. GenericValue > + \tparam Allocator The allocator type for allocating memory for internal representation. + + \note GenericPointer uses same encoding of ValueType. + However, Allocator of GenericPointer is independent of Allocator of Value. +*/ +template +class GenericPointer { +public: + typedef typename ValueType::EncodingType EncodingType; //!< Encoding type from Value + typedef typename ValueType::Ch Ch; //!< Character type from Value + + //! A token is the basic units of internal representation. + /*! + A JSON pointer string representation "/foo/123" is parsed to two tokens: + "foo" and 123. 123 will be represented in both numeric form and string form. + They are resolved according to the actual value type (object or array). + + For token that are not numbers, or the numeric value is out of bound + (greater than limits of SizeType), they are only treated as string form + (i.e. the token's index will be equal to kPointerInvalidIndex). + + This struct is public so that user can create a Pointer without parsing and + allocation, using a special constructor. + */ + struct Token { + const Ch* name; //!< Name of the token. It has null character at the end but it can contain null character. + SizeType length; //!< Length of the name. + SizeType index; //!< A valid array index, if it is not equal to kPointerInvalidIndex. + }; + + //!@name Constructors and destructor. + //@{ + + //! Default constructor. + GenericPointer(Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {} + + //! Constructor that parses a string or URI fragment representation. + /*! + \param source A null-terminated, string or URI fragment representation of JSON pointer. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + */ + explicit GenericPointer(const Ch* source, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source, internal::StrLen(source)); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Constructor that parses a string or URI fragment representation. + /*! + \param source A string or URI fragment representation of JSON pointer. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + explicit GenericPointer(const std::basic_string& source, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source.c_str(), source.size()); + } +#endif + + //! Constructor that parses a string or URI fragment representation, with length of the source string. + /*! + \param source A string or URI fragment representation of JSON pointer. + \param length Length of source. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + \note Slightly faster than the overload without length. + */ + GenericPointer(const Ch* source, size_t length, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source, length); + } + + //! Constructor with user-supplied tokens. + /*! + This constructor let user supplies const array of tokens. + This prevents the parsing process and eliminates allocation. + This is preferred for memory constrained environments. + + \param tokens An constant array of tokens representing the JSON pointer. + \param tokenCount Number of tokens. + + \b Example + \code + #define NAME(s) { s, sizeof(s) / sizeof(s[0]) - 1, kPointerInvalidIndex } + #define INDEX(i) { #i, sizeof(#i) - 1, i } + + static const Pointer::Token kTokens[] = { NAME("foo"), INDEX(123) }; + static const Pointer p(kTokens, sizeof(kTokens) / sizeof(kTokens[0])); + // Equivalent to static const Pointer p("/foo/123"); + + #undef NAME + #undef INDEX + \endcode + */ + GenericPointer(const Token* tokens, size_t tokenCount) : allocator_(), ownAllocator_(), nameBuffer_(), tokens_(const_cast(tokens)), tokenCount_(tokenCount), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {} + + //! Copy constructor. + GenericPointer(const GenericPointer& rhs, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + *this = rhs; + } + + //! Destructor. + ~GenericPointer() { + if (nameBuffer_) // If user-supplied tokens constructor is used, nameBuffer_ is nullptr and tokens_ are not deallocated. + Allocator::Free(tokens_); + RAPIDJSON_DELETE(ownAllocator_); + } + + //! Assignment operator. + GenericPointer& operator=(const GenericPointer& rhs) { + if (this != &rhs) { + // Do not delete ownAllcator + if (nameBuffer_) + Allocator::Free(tokens_); + + tokenCount_ = rhs.tokenCount_; + parseErrorOffset_ = rhs.parseErrorOffset_; + parseErrorCode_ = rhs.parseErrorCode_; + + if (rhs.nameBuffer_) + CopyFromRaw(rhs); // Normally parsed tokens. + else { + tokens_ = rhs.tokens_; // User supplied const tokens. + nameBuffer_ = 0; + } + } + return *this; + } + + //@} + + //!@name Append token + //@{ + + //! Append a token and return a new Pointer + /*! + \param token Token to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const Token& token, Allocator* allocator = 0) const { + GenericPointer r; + r.allocator_ = allocator; + Ch *p = r.CopyFromRaw(*this, 1, token.length + 1); + std::memcpy(p, token.name, (token.length + 1) * sizeof(Ch)); + r.tokens_[tokenCount_].name = p; + r.tokens_[tokenCount_].length = token.length; + r.tokens_[tokenCount_].index = token.index; + return r; + } + + //! Append a name token with length, and return a new Pointer + /*! + \param name Name to be appended. + \param length Length of name. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const Ch* name, SizeType length, Allocator* allocator = 0) const { + Token token = { name, length, kPointerInvalidIndex }; + return Append(token, allocator); + } + + //! Append a name token without length, and return a new Pointer + /*! + \param name Name (const Ch*) to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >), (GenericPointer)) + Append(T* name, Allocator* allocator = 0) const { + return Append(name, StrLen(name), allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Append a name token, and return a new Pointer + /*! + \param name Name to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const std::basic_string& name, Allocator* allocator = 0) const { + return Append(name.c_str(), static_cast(name.size()), allocator); + } +#endif + + //! Append a index token, and return a new Pointer + /*! + \param index Index to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(SizeType index, Allocator* allocator = 0) const { + char buffer[21]; + char* end = sizeof(SizeType) == 4 ? internal::u32toa(index, buffer) : internal::u64toa(index, buffer); + SizeType length = static_cast(end - buffer); + buffer[length] = '\0'; + + if (sizeof(Ch) == 1) { + Token token = { reinterpret_cast(buffer), length, index }; + return Append(token, allocator); + } + else { + Ch name[21]; + for (size_t i = 0; i <= length; i++) + name[i] = buffer[i]; + Token token = { name, length, index }; + return Append(token, allocator); + } + } + + //! Append a token by value, and return a new Pointer + /*! + \param token token to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const ValueType& token, Allocator* allocator = 0) const { + if (token.IsString()) + return Append(token.GetString(), token.GetStringLength(), allocator); + else { + RAPIDJSON_ASSERT(token.IsUint64()); + RAPIDJSON_ASSERT(token.GetUint64() <= SizeType(~0)); + return Append(static_cast(token.GetUint64()), allocator); + } + } + + //!@name Handling Parse Error + //@{ + + //! Check whether this is a valid pointer. + bool IsValid() const { return parseErrorCode_ == kPointerParseErrorNone; } + + //! Get the parsing error offset in code unit. + size_t GetParseErrorOffset() const { return parseErrorOffset_; } + + //! Get the parsing error code. + PointerParseErrorCode GetParseErrorCode() const { return parseErrorCode_; } + + //@} + + //! Get the allocator of this pointer. + Allocator& GetAllocator() { return *allocator_; } + + //!@name Tokens + //@{ + + //! Get the token array (const version only). + const Token* GetTokens() const { return tokens_; } + + //! Get the number of tokens. + size_t GetTokenCount() const { return tokenCount_; } + + //@} + + //!@name Equality/inequality operators + //@{ + + //! Equality operator. + /*! + \note When any pointers are invalid, always returns false. + */ + bool operator==(const GenericPointer& rhs) const { + if (!IsValid() || !rhs.IsValid() || tokenCount_ != rhs.tokenCount_) + return false; + + for (size_t i = 0; i < tokenCount_; i++) { + if (tokens_[i].index != rhs.tokens_[i].index || + tokens_[i].length != rhs.tokens_[i].length || + (tokens_[i].length != 0 && std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch)* tokens_[i].length) != 0)) + { + return false; + } + } + + return true; + } + + //! Inequality operator. + /*! + \note When any pointers are invalid, always returns true. + */ + bool operator!=(const GenericPointer& rhs) const { return !(*this == rhs); } + + //@} + + //!@name Stringify + //@{ + + //! Stringify the pointer into string representation. + /*! + \tparam OutputStream Type of output stream. + \param os The output stream. + */ + template + bool Stringify(OutputStream& os) const { + return Stringify(os); + } + + //! Stringify the pointer into URI fragment representation. + /*! + \tparam OutputStream Type of output stream. + \param os The output stream. + */ + template + bool StringifyUriFragment(OutputStream& os) const { + return Stringify(os); + } + + //@} + + //!@name Create value + //@{ + + //! Create a value in a subtree. + /*! + If the value is not exist, it creates all parent values and a JSON Null value. + So it always succeed and return the newly created or existing value. + + Remind that it may change types of parents according to tokens, so it + potentially removes previously stored values. For example, if a document + was an array, and "/foo" is used to create a value, then the document + will be changed to an object, and all existing array elements are lost. + + \param root Root value of a DOM subtree to be resolved. It can be any value other than document root. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \param alreadyExist If non-null, it stores whether the resolved value is already exist. + \return The resolved newly created (a JSON Null value), or already exists value. + */ + ValueType& Create(ValueType& root, typename ValueType::AllocatorType& allocator, bool* alreadyExist = 0) const { + RAPIDJSON_ASSERT(IsValid()); + ValueType* v = &root; + bool exist = true; + for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + if (v->IsArray() && t->name[0] == '-' && t->length == 1) { + v->PushBack(ValueType().Move(), allocator); + v = &((*v)[v->Size() - 1]); + exist = false; + } + else { + if (t->index == kPointerInvalidIndex) { // must be object name + if (!v->IsObject()) + v->SetObject(); // Change to Object + } + else { // object name or array index + if (!v->IsArray() && !v->IsObject()) + v->SetArray(); // Change to Array + } + + if (v->IsArray()) { + if (t->index >= v->Size()) { + v->Reserve(t->index + 1, allocator); + while (t->index >= v->Size()) + v->PushBack(ValueType().Move(), allocator); + exist = false; + } + v = &((*v)[t->index]); + } + else { + typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); + if (m == v->MemberEnd()) { + v->AddMember(ValueType(t->name, t->length, allocator).Move(), ValueType().Move(), allocator); + v = &(--v->MemberEnd())->value; // Assumes AddMember() appends at the end + exist = false; + } + else + v = &m->value; + } + } + } + + if (alreadyExist) + *alreadyExist = exist; + + return *v; + } + + //! Creates a value in a document. + /*! + \param document A document to be resolved. + \param alreadyExist If non-null, it stores whether the resolved value is already exist. + \return The resolved newly created, or already exists value. + */ + template + ValueType& Create(GenericDocument& document, bool* alreadyExist = 0) const { + return Create(document, document.GetAllocator(), alreadyExist); + } + + //@} + + //!@name Query value + //@{ + + //! Query a value in a subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param unresolvedTokenIndex If the pointer cannot resolve a token in the pointer, this parameter can obtain the index of unresolved token. + \return Pointer to the value if it can be resolved. Otherwise null. + + \note + There are only 3 situations when a value cannot be resolved: + 1. A value in the path is not an array nor object. + 2. An object value does not contain the token. + 3. A token is out of range of an array value. + + Use unresolvedTokenIndex to retrieve the token index. + */ + ValueType* Get(ValueType& root, size_t* unresolvedTokenIndex = 0) const { + RAPIDJSON_ASSERT(IsValid()); + ValueType* v = &root; + for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + switch (v->GetType()) { + case kObjectType: + { + typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); + if (m == v->MemberEnd()) + break; + v = &m->value; + } + continue; + case kArrayType: + if (t->index == kPointerInvalidIndex || t->index >= v->Size()) + break; + v = &((*v)[t->index]); + continue; + default: + break; + } + + // Error: unresolved token + if (unresolvedTokenIndex) + *unresolvedTokenIndex = static_cast(t - tokens_); + return 0; + } + return v; + } + + //! Query a const value in a const subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \return Pointer to the value if it can be resolved. Otherwise null. + */ + const ValueType* Get(const ValueType& root, size_t* unresolvedTokenIndex = 0) const { + return Get(const_cast(root), unresolvedTokenIndex); + } + + //@} + + //!@name Query a value with default + //@{ + + //! Query a value in a subtree with default value. + /*! + Similar to Get(), but if the specified value do not exists, it creates all parents and clone the default value. + So that this function always succeed. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param defaultValue Default value to be cloned if the value was not exists. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& GetWithDefault(ValueType& root, const ValueType& defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + Value& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.CopyFrom(defaultValue, allocator); + } + + //! Query a value in a subtree with default null-terminated string. + ValueType& GetWithDefault(ValueType& root, const Ch* defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + Value& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.SetString(defaultValue, allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Query a value in a subtree with default std::basic_string. + ValueType& GetWithDefault(ValueType& root, const std::basic_string& defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + Value& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.SetString(defaultValue, allocator); + } +#endif + + //! Query a value in a subtree with default primitive value. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + GetWithDefault(ValueType& root, T defaultValue, typename ValueType::AllocatorType& allocator) const { + return GetWithDefault(root, ValueType(defaultValue).Move(), allocator); + } + + //! Query a value in a document with default value. + template + ValueType& GetWithDefault(GenericDocument& document, const ValueType& defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + + //! Query a value in a document with default null-terminated string. + template + ValueType& GetWithDefault(GenericDocument& document, const Ch* defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Query a value in a document with default std::basic_string. + template + ValueType& GetWithDefault(GenericDocument& document, const std::basic_string& defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } +#endif + + //! Query a value in a document with default primitive value. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + GetWithDefault(GenericDocument& document, T defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + + //@} + + //!@name Set a value + //@{ + + //! Set a value in a subtree, with move semantics. + /*! + It creates all parents if they are not exist or types are different to the tokens. + So this function always succeeds but potentially remove existing values. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param value Value to be set. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& Set(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = value; + } + + //! Set a value in a subtree, with copy semantics. + ValueType& Set(ValueType& root, const ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator).CopyFrom(value, allocator); + } + + //! Set a null-terminated string in a subtree. + ValueType& Set(ValueType& root, const Ch* value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value, allocator).Move(); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Set a std::basic_string in a subtree. + ValueType& Set(ValueType& root, const std::basic_string& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value, allocator).Move(); + } +#endif + + //! Set a primitive value in a subtree. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + Set(ValueType& root, T value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value).Move(); + } + + //! Set a value in a document, with move semantics. + template + ValueType& Set(GenericDocument& document, ValueType& value) const { + return Create(document) = value; + } + + //! Set a value in a document, with copy semantics. + template + ValueType& Set(GenericDocument& document, const ValueType& value) const { + return Create(document).CopyFrom(value, document.GetAllocator()); + } + + //! Set a null-terminated string in a document. + template + ValueType& Set(GenericDocument& document, const Ch* value) const { + return Create(document) = ValueType(value, document.GetAllocator()).Move(); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Sets a std::basic_string in a document. + template + ValueType& Set(GenericDocument& document, const std::basic_string& value) const { + return Create(document) = ValueType(value, document.GetAllocator()).Move(); + } +#endif + + //! Set a primitive value in a document. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + Set(GenericDocument& document, T value) const { + return Create(document) = value; + } + + //@} + + //!@name Swap a value + //@{ + + //! Swap a value with a value in a subtree. + /*! + It creates all parents if they are not exist or types are different to the tokens. + So this function always succeeds but potentially remove existing values. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param value Value to be swapped. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& Swap(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator).Swap(value); + } + + //! Swap a value with a value in a document. + template + ValueType& Swap(GenericDocument& document, ValueType& value) const { + return Create(document).Swap(value); + } + + //@} + + //! Erase a value in a subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \return Whether the resolved value is found and erased. + + \note Erasing with an empty pointer \c Pointer(""), i.e. the root, always fail and return false. + */ + bool Erase(ValueType& root) const { + RAPIDJSON_ASSERT(IsValid()); + if (tokenCount_ == 0) // Cannot erase the root + return false; + + ValueType* v = &root; + const Token* last = tokens_ + (tokenCount_ - 1); + for (const Token *t = tokens_; t != last; ++t) { + switch (v->GetType()) { + case kObjectType: + { + typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); + if (m == v->MemberEnd()) + return false; + v = &m->value; + } + break; + case kArrayType: + if (t->index == kPointerInvalidIndex || t->index >= v->Size()) + return false; + v = &((*v)[t->index]); + break; + default: + return false; + } + } + + switch (v->GetType()) { + case kObjectType: + return v->EraseMember(GenericStringRef(last->name, last->length)); + case kArrayType: + if (last->index == kPointerInvalidIndex || last->index >= v->Size()) + return false; + v->Erase(v->Begin() + last->index); + return true; + default: + return false; + } + } + +private: + //! Clone the content from rhs to this. + /*! + \param rhs Source pointer. + \param extraToken Extra tokens to be allocated. + \param extraNameBufferSize Extra name buffer size (in number of Ch) to be allocated. + \return Start of non-occupied name buffer, for storing extra names. + */ + Ch* CopyFromRaw(const GenericPointer& rhs, size_t extraToken = 0, size_t extraNameBufferSize = 0) { + if (!allocator_) // allocator is independently owned. + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + + size_t nameBufferSize = rhs.tokenCount_; // null terminators for tokens + for (Token *t = rhs.tokens_; t != rhs.tokens_ + rhs.tokenCount_; ++t) + nameBufferSize += t->length; + + tokenCount_ = rhs.tokenCount_ + extraToken; + tokens_ = static_cast(allocator_->Malloc(tokenCount_ * sizeof(Token) + (nameBufferSize + extraNameBufferSize) * sizeof(Ch))); + nameBuffer_ = reinterpret_cast(tokens_ + tokenCount_); + if (rhs.tokenCount_ > 0) { + std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ * sizeof(Token)); + } + if (nameBufferSize > 0) { + std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize * sizeof(Ch)); + } + + // Adjust pointers to name buffer + std::ptrdiff_t diff = nameBuffer_ - rhs.nameBuffer_; + for (Token *t = tokens_; t != tokens_ + rhs.tokenCount_; ++t) + t->name += diff; + + return nameBuffer_ + nameBufferSize; + } + + //! Check whether a character should be percent-encoded. + /*! + According to RFC 3986 2.3 Unreserved Characters. + \param c The character (code unit) to be tested. + */ + bool NeedPercentEncode(Ch c) const { + return !((c >= '0' && c <= '9') || (c >= 'A' && c <='Z') || (c >= 'a' && c <= 'z') || c == '-' || c == '.' || c == '_' || c =='~'); + } + + //! Parse a JSON String or its URI fragment representation into tokens. +#ifndef __clang__ // -Wdocumentation + /*! + \param source Either a JSON Pointer string, or its URI fragment representation. Not need to be null terminated. + \param length Length of the source string. + \note Source cannot be JSON String Representation of JSON Pointer, e.g. In "/\u0000", \u0000 will not be unescaped. + */ +#endif + void Parse(const Ch* source, size_t length) { + RAPIDJSON_ASSERT(source != NULL); + RAPIDJSON_ASSERT(nameBuffer_ == 0); + RAPIDJSON_ASSERT(tokens_ == 0); + + // Create own allocator if user did not supply. + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + + // Count number of '/' as tokenCount + tokenCount_ = 0; + for (const Ch* s = source; s != source + length; s++) + if (*s == '/') + tokenCount_++; + + Token* token = tokens_ = static_cast(allocator_->Malloc(tokenCount_ * sizeof(Token) + length * sizeof(Ch))); + Ch* name = nameBuffer_ = reinterpret_cast(tokens_ + tokenCount_); + size_t i = 0; + + // Detect if it is a URI fragment + bool uriFragment = false; + if (source[i] == '#') { + uriFragment = true; + i++; + } + + if (i != length && source[i] != '/') { + parseErrorCode_ = kPointerParseErrorTokenMustBeginWithSolidus; + goto error; + } + + while (i < length) { + RAPIDJSON_ASSERT(source[i] == '/'); + i++; // consumes '/' + + token->name = name; + bool isNumber = true; + + while (i < length && source[i] != '/') { + Ch c = source[i]; + if (uriFragment) { + // Decoding percent-encoding for URI fragment + if (c == '%') { + PercentDecodeStream is(&source[i], source + length); + GenericInsituStringStream os(name); + Ch* begin = os.PutBegin(); + if (!Transcoder, EncodingType>().Validate(is, os) || !is.IsValid()) { + parseErrorCode_ = kPointerParseErrorInvalidPercentEncoding; + goto error; + } + size_t len = os.PutEnd(begin); + i += is.Tell() - 1; + if (len == 1) + c = *name; + else { + name += len; + isNumber = false; + i++; + continue; + } + } + else if (NeedPercentEncode(c)) { + parseErrorCode_ = kPointerParseErrorCharacterMustPercentEncode; + goto error; + } + } + + i++; + + // Escaping "~0" -> '~', "~1" -> '/' + if (c == '~') { + if (i < length) { + c = source[i]; + if (c == '0') c = '~'; + else if (c == '1') c = '/'; + else { + parseErrorCode_ = kPointerParseErrorInvalidEscape; + goto error; + } + i++; + } + else { + parseErrorCode_ = kPointerParseErrorInvalidEscape; + goto error; + } + } + + // First check for index: all of characters are digit + if (c < '0' || c > '9') + isNumber = false; + + *name++ = c; + } + token->length = static_cast(name - token->name); + if (token->length == 0) + isNumber = false; + *name++ = '\0'; // Null terminator + + // Second check for index: more than one digit cannot have leading zero + if (isNumber && token->length > 1 && token->name[0] == '0') + isNumber = false; + + // String to SizeType conversion + SizeType n = 0; + if (isNumber) { + for (size_t j = 0; j < token->length; j++) { + SizeType m = n * 10 + static_cast(token->name[j] - '0'); + if (m < n) { // overflow detection + isNumber = false; + break; + } + n = m; + } + } + + token->index = isNumber ? n : kPointerInvalidIndex; + token++; + } + + RAPIDJSON_ASSERT(name <= nameBuffer_ + length); // Should not overflow buffer + parseErrorCode_ = kPointerParseErrorNone; + return; + + error: + Allocator::Free(tokens_); + nameBuffer_ = 0; + tokens_ = 0; + tokenCount_ = 0; + parseErrorOffset_ = i; + return; + } + + //! Stringify to string or URI fragment representation. + /*! + \tparam uriFragment True for stringifying to URI fragment representation. False for string representation. + \tparam OutputStream type of output stream. + \param os The output stream. + */ + template + bool Stringify(OutputStream& os) const { + RAPIDJSON_ASSERT(IsValid()); + + if (uriFragment) + os.Put('#'); + + for (Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + os.Put('/'); + for (size_t j = 0; j < t->length; j++) { + Ch c = t->name[j]; + if (c == '~') { + os.Put('~'); + os.Put('0'); + } + else if (c == '/') { + os.Put('~'); + os.Put('1'); + } + else if (uriFragment && NeedPercentEncode(c)) { + // Transcode to UTF8 sequence + GenericStringStream source(&t->name[j]); + PercentEncodeStream target(os); + if (!Transcoder >().Validate(source, target)) + return false; + j += source.Tell() - 1; + } + else + os.Put(c); + } + } + return true; + } + + //! A helper stream for decoding a percent-encoded sequence into code unit. + /*! + This stream decodes %XY triplet into code unit (0-255). + If it encounters invalid characters, it sets output code unit as 0 and + mark invalid, and to be checked by IsValid(). + */ + class PercentDecodeStream { + public: + typedef typename ValueType::Ch Ch; + + //! Constructor + /*! + \param source Start of the stream + \param end Past-the-end of the stream. + */ + PercentDecodeStream(const Ch* source, const Ch* end) : src_(source), head_(source), end_(end), valid_(true) {} + + Ch Take() { + if (*src_ != '%' || src_ + 3 > end_) { // %XY triplet + valid_ = false; + return 0; + } + src_++; + Ch c = 0; + for (int j = 0; j < 2; j++) { + c = static_cast(c << 4); + Ch h = *src_; + if (h >= '0' && h <= '9') c = static_cast(c + h - '0'); + else if (h >= 'A' && h <= 'F') c = static_cast(c + h - 'A' + 10); + else if (h >= 'a' && h <= 'f') c = static_cast(c + h - 'a' + 10); + else { + valid_ = false; + return 0; + } + src_++; + } + return c; + } + + size_t Tell() const { return static_cast(src_ - head_); } + bool IsValid() const { return valid_; } + + private: + const Ch* src_; //!< Current read position. + const Ch* head_; //!< Original head of the string. + const Ch* end_; //!< Past-the-end position. + bool valid_; //!< Whether the parsing is valid. + }; + + //! A helper stream to encode character (UTF-8 code unit) into percent-encoded sequence. + template + class PercentEncodeStream { + public: + PercentEncodeStream(OutputStream& os) : os_(os) {} + void Put(char c) { // UTF-8 must be byte + unsigned char u = static_cast(c); + static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + os_.Put('%'); + os_.Put(hexDigits[u >> 4]); + os_.Put(hexDigits[u & 15]); + } + private: + OutputStream& os_; + }; + + Allocator* allocator_; //!< The current allocator. It is either user-supplied or equal to ownAllocator_. + Allocator* ownAllocator_; //!< Allocator owned by this Pointer. + Ch* nameBuffer_; //!< A buffer containing all names in tokens. + Token* tokens_; //!< A list of tokens. + size_t tokenCount_; //!< Number of tokens in tokens_. + size_t parseErrorOffset_; //!< Offset in code unit when parsing fail. + PointerParseErrorCode parseErrorCode_; //!< Parsing error code. +}; + +//! GenericPointer for Value (UTF-8, default allocator). +typedef GenericPointer Pointer; + +//!@name Helper functions for GenericPointer +//@{ + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& CreateValueByPointer(T& root, const GenericPointer& pointer, typename T::AllocatorType& a) { + return pointer.Create(root, a); +} + +template +typename T::ValueType& CreateValueByPointer(T& root, const CharType(&source)[N], typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Create(root, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& CreateValueByPointer(DocumentType& document, const GenericPointer& pointer) { + return pointer.Create(document); +} + +template +typename DocumentType::ValueType& CreateValueByPointer(DocumentType& document, const CharType(&source)[N]) { + return GenericPointer(source, N - 1).Create(document); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType* GetValueByPointer(T& root, const GenericPointer& pointer, size_t* unresolvedTokenIndex = 0) { + return pointer.Get(root, unresolvedTokenIndex); +} + +template +const typename T::ValueType* GetValueByPointer(const T& root, const GenericPointer& pointer, size_t* unresolvedTokenIndex = 0) { + return pointer.Get(root, unresolvedTokenIndex); +} + +template +typename T::ValueType* GetValueByPointer(T& root, const CharType (&source)[N], size_t* unresolvedTokenIndex = 0) { + return GenericPointer(source, N - 1).Get(root, unresolvedTokenIndex); +} + +template +const typename T::ValueType* GetValueByPointer(const T& root, const CharType(&source)[N], size_t* unresolvedTokenIndex = 0) { + return GenericPointer(source, N - 1).Get(root, unresolvedTokenIndex); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const typename T::ValueType& defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const typename T::Ch* defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const std::basic_string& defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, T2 defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::ValueType& defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::Ch* defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const std::basic_string& defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +GetValueByPointerWithDefault(T& root, const CharType(&source)[N], T2 defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::ValueType& defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::Ch* defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const std::basic_string& defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, T2 defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const typename DocumentType::ValueType& defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const typename DocumentType::Ch* defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const std::basic_string& defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], T2 defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const typename T::Ch* value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const std::basic_string& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +SetValueByPointer(T& root, const GenericPointer& pointer, T2 value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const typename T::Ch* value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const std::basic_string& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +SetValueByPointer(T& root, const CharType(&source)[N], T2 value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, typename DocumentType::ValueType& value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::ValueType& value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::Ch* value) { + return pointer.Set(document, value); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const std::basic_string& value) { + return pointer.Set(document, value); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +SetValueByPointer(DocumentType& document, const GenericPointer& pointer, T2 value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const typename DocumentType::Ch* value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const std::basic_string& value) { + return GenericPointer(source, N - 1).Set(document, value); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +SetValueByPointer(DocumentType& document, const CharType(&source)[N], T2 value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& SwapValueByPointer(T& root, const GenericPointer& pointer, typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Swap(root, value, a); +} + +template +typename T::ValueType& SwapValueByPointer(T& root, const CharType(&source)[N], typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Swap(root, value, a); +} + +template +typename DocumentType::ValueType& SwapValueByPointer(DocumentType& document, const GenericPointer& pointer, typename DocumentType::ValueType& value) { + return pointer.Swap(document, value); +} + +template +typename DocumentType::ValueType& SwapValueByPointer(DocumentType& document, const CharType(&source)[N], typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Swap(document, value); +} + +////////////////////////////////////////////////////////////////////////////// + +template +bool EraseValueByPointer(T& root, const GenericPointer& pointer) { + return pointer.Erase(root); +} + +template +bool EraseValueByPointer(T& root, const CharType(&source)[N]) { + return GenericPointer(source, N - 1).Erase(root); +} + +//@} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_POINTER_H_ diff --git a/src/3rdparty/rapidjson/prettywriter.h b/src/3rdparty/rapidjson/prettywriter.h new file mode 100644 index 00000000..0dcb0fee --- /dev/null +++ b/src/3rdparty/rapidjson/prettywriter.h @@ -0,0 +1,255 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_PRETTYWRITER_H_ +#define RAPIDJSON_PRETTYWRITER_H_ + +#include "writer.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Combination of PrettyWriter format flags. +/*! \see PrettyWriter::SetFormatOptions + */ +enum PrettyFormatOptions { + kFormatDefault = 0, //!< Default pretty formatting. + kFormatSingleLineArray = 1 //!< Format arrays on a single line. +}; + +//! Writer with indentation and spacing. +/*! + \tparam OutputStream Type of ouptut os. + \tparam SourceEncoding Encoding of source string. + \tparam TargetEncoding Encoding of output stream. + \tparam StackAllocator Type of allocator for allocating memory of stack. +*/ +template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags> +class PrettyWriter : public Writer { +public: + typedef Writer Base; + typedef typename Base::Ch Ch; + + //! Constructor + /*! \param os Output stream. + \param allocator User supplied allocator. If it is null, it will create a private one. + \param levelDepth Initial capacity of stack. + */ + explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : + Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {} + + + explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : + Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} + + //! Set custom indentation. + /*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r'). + \param indentCharCount Number of indent characters for each indentation level. + \note The default indentation is 4 spaces. + */ + PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) { + RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r'); + indentChar_ = indentChar; + indentCharCount_ = indentCharCount; + return *this; + } + + //! Set pretty writer formatting options. + /*! \param options Formatting options. + */ + PrettyWriter& SetFormatOptions(PrettyFormatOptions options) { + formatOptions_ = options; + return *this; + } + + /*! @name Implementation of Handler + \see Handler + */ + //@{ + + bool Null() { PrettyPrefix(kNullType); return Base::WriteNull(); } + bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::WriteBool(b); } + bool Int(int i) { PrettyPrefix(kNumberType); return Base::WriteInt(i); } + bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::WriteUint(u); } + bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::WriteInt64(i64); } + bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::WriteUint64(u64); } + bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); } + + bool RawNumber(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + PrettyPrefix(kNumberType); + return Base::WriteString(str, length); + } + + bool String(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + PrettyPrefix(kStringType); + return Base::WriteString(str, length); + } + +#if RAPIDJSON_HAS_STDSTRING + bool String(const std::basic_string& str) { + return String(str.data(), SizeType(str.size())); + } +#endif + + bool StartObject() { + PrettyPrefix(kObjectType); + new (Base::level_stack_.template Push()) typename Base::Level(false); + return Base::WriteStartObject(); + } + + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + +#if RAPIDJSON_HAS_STDSTRING + bool Key(const std::basic_string& str) { + return Key(str.data(), SizeType(str.size())); + } +#endif + + bool EndObject(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); + RAPIDJSON_ASSERT(!Base::level_stack_.template Top()->inArray); + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; + + if (!empty) { + Base::os_->Put('\n'); + WriteIndent(); + } + bool ret = Base::WriteEndObject(); + (void)ret; + RAPIDJSON_ASSERT(ret == true); + if (Base::level_stack_.Empty()) // end of json text + Base::os_->Flush(); + return true; + } + + bool StartArray() { + PrettyPrefix(kArrayType); + new (Base::level_stack_.template Push()) typename Base::Level(true); + return Base::WriteStartArray(); + } + + bool EndArray(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); + RAPIDJSON_ASSERT(Base::level_stack_.template Top()->inArray); + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; + + if (!empty && !(formatOptions_ & kFormatSingleLineArray)) { + Base::os_->Put('\n'); + WriteIndent(); + } + bool ret = Base::WriteEndArray(); + (void)ret; + RAPIDJSON_ASSERT(ret == true); + if (Base::level_stack_.Empty()) // end of json text + Base::os_->Flush(); + return true; + } + + //@} + + /*! @name Convenience extensions */ + //@{ + + //! Simpler but slower overload. + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + + //@} + + //! Write a raw JSON value. + /*! + For user to write a stringified JSON as a value. + + \param json A well-formed JSON value. It should not contain null character within [0, length - 1] range. + \param length Length of the json. + \param type Type of the root of json. + \note When using PrettyWriter::RawValue(), the result json may not be indented correctly. + */ + bool RawValue(const Ch* json, size_t length, Type type) { PrettyPrefix(type); return Base::WriteRawValue(json, length); } + +protected: + void PrettyPrefix(Type type) { + (void)type; + if (Base::level_stack_.GetSize() != 0) { // this value is not at root + typename Base::Level* level = Base::level_stack_.template Top(); + + if (level->inArray) { + if (level->valueCount > 0) { + Base::os_->Put(','); // add comma if it is not the first element in array + if (formatOptions_ & kFormatSingleLineArray) + Base::os_->Put(' '); + } + + if (!(formatOptions_ & kFormatSingleLineArray)) { + Base::os_->Put('\n'); + WriteIndent(); + } + } + else { // in object + if (level->valueCount > 0) { + if (level->valueCount % 2 == 0) { + Base::os_->Put(','); + Base::os_->Put('\n'); + } + else { + Base::os_->Put(':'); + Base::os_->Put(' '); + } + } + else + Base::os_->Put('\n'); + + if (level->valueCount % 2 == 0) + WriteIndent(); + } + if (!level->inArray && level->valueCount % 2 == 0) + RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name + level->valueCount++; + } + else { + RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root. + Base::hasRoot_ = true; + } + } + + void WriteIndent() { + size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_; + PutN(*Base::os_, static_cast(indentChar_), count); + } + + Ch indentChar_; + unsigned indentCharCount_; + PrettyFormatOptions formatOptions_; + +private: + // Prohibit copy constructor & assignment operator. + PrettyWriter(const PrettyWriter&); + PrettyWriter& operator=(const PrettyWriter&); +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/src/3rdparty/rapidjson/rapidjson.h b/src/3rdparty/rapidjson/rapidjson.h new file mode 100644 index 00000000..2ef9bc56 --- /dev/null +++ b/src/3rdparty/rapidjson/rapidjson.h @@ -0,0 +1,614 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_RAPIDJSON_H_ +#define RAPIDJSON_RAPIDJSON_H_ + +/*!\file rapidjson.h + \brief common definitions and configuration + + \see RAPIDJSON_CONFIG + */ + +/*! \defgroup RAPIDJSON_CONFIG RapidJSON configuration + \brief Configuration macros for library features + + Some RapidJSON features are configurable to adapt the library to a wide + variety of platforms, environments and usage scenarios. Most of the + features can be configured in terms of overriden or predefined + preprocessor macros at compile-time. + + Some additional customization is available in the \ref RAPIDJSON_ERRORS APIs. + + \note These macros should be given on the compiler command-line + (where applicable) to avoid inconsistent values when compiling + different translation units of a single application. + */ + +#include // malloc(), realloc(), free(), size_t +#include // memset(), memcpy(), memmove(), memcmp() + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_VERSION_STRING +// +// ALWAYS synchronize the following 3 macros with corresponding variables in /CMakeLists.txt. +// + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +// token stringification +#define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x) +#define RAPIDJSON_DO_STRINGIFY(x) #x +//!@endcond + +/*! \def RAPIDJSON_MAJOR_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Major version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_MINOR_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Minor version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_PATCH_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Patch version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_VERSION_STRING + \ingroup RAPIDJSON_CONFIG + \brief Version of RapidJSON in ".." string format. +*/ +#define RAPIDJSON_MAJOR_VERSION 1 +#define RAPIDJSON_MINOR_VERSION 1 +#define RAPIDJSON_PATCH_VERSION 0 +#define RAPIDJSON_VERSION_STRING \ + RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION) + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NAMESPACE_(BEGIN|END) +/*! \def RAPIDJSON_NAMESPACE + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace + + In order to avoid symbol clashes and/or "One Definition Rule" errors + between multiple inclusions of (different versions of) RapidJSON in + a single binary, users can customize the name of the main RapidJSON + namespace. + + In case of a single nesting level, defining \c RAPIDJSON_NAMESPACE + to a custom name (e.g. \c MyRapidJSON) is sufficient. If multiple + levels are needed, both \ref RAPIDJSON_NAMESPACE_BEGIN and \ref + RAPIDJSON_NAMESPACE_END need to be defined as well: + + \code + // in some .cpp file + #define RAPIDJSON_NAMESPACE my::rapidjson + #define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapidjson { + #define RAPIDJSON_NAMESPACE_END } } + #include "rapidjson/..." + \endcode + + \see rapidjson + */ +/*! \def RAPIDJSON_NAMESPACE_BEGIN + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace (opening expression) + \see RAPIDJSON_NAMESPACE +*/ +/*! \def RAPIDJSON_NAMESPACE_END + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace (closing expression) + \see RAPIDJSON_NAMESPACE +*/ +#ifndef RAPIDJSON_NAMESPACE +#define RAPIDJSON_NAMESPACE rapidjson +#endif +#ifndef RAPIDJSON_NAMESPACE_BEGIN +#define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE { +#endif +#ifndef RAPIDJSON_NAMESPACE_END +#define RAPIDJSON_NAMESPACE_END } +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_HAS_STDSTRING + +#ifndef RAPIDJSON_HAS_STDSTRING +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation +#else +#define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default +#endif +/*! \def RAPIDJSON_HAS_STDSTRING + \ingroup RAPIDJSON_CONFIG + \brief Enable RapidJSON support for \c std::string + + By defining this preprocessor symbol to \c 1, several convenience functions for using + \ref rapidjson::GenericValue with \c std::string are enabled, especially + for construction and comparison. + + \hideinitializer +*/ +#endif // !defined(RAPIDJSON_HAS_STDSTRING) + +#if RAPIDJSON_HAS_STDSTRING +#include +#endif // RAPIDJSON_HAS_STDSTRING + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NO_INT64DEFINE + +/*! \def RAPIDJSON_NO_INT64DEFINE + \ingroup RAPIDJSON_CONFIG + \brief Use external 64-bit integer types. + + RapidJSON requires the 64-bit integer types \c int64_t and \c uint64_t types + to be available at global scope. + + If users have their own definition, define RAPIDJSON_NO_INT64DEFINE to + prevent RapidJSON from defining its own types. +*/ +#ifndef RAPIDJSON_NO_INT64DEFINE +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#if defined(_MSC_VER) && (_MSC_VER < 1800) // Visual Studio 2013 +#include "msinttypes/stdint.h" +#include "msinttypes/inttypes.h" +#else +// Other compilers should have this. +#include +#include +#endif +//!@endcond +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_NO_INT64DEFINE +#endif +#endif // RAPIDJSON_NO_INT64TYPEDEF + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_FORCEINLINE + +#ifndef RAPIDJSON_FORCEINLINE +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#if defined(_MSC_VER) && defined(NDEBUG) +#define RAPIDJSON_FORCEINLINE __forceinline +#elif defined(__GNUC__) && __GNUC__ >= 4 && defined(NDEBUG) +#define RAPIDJSON_FORCEINLINE __attribute__((always_inline)) +#else +#define RAPIDJSON_FORCEINLINE +#endif +//!@endcond +#endif // RAPIDJSON_FORCEINLINE + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ENDIAN +#define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine +#define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine + +//! Endianness of the machine. +/*! + \def RAPIDJSON_ENDIAN + \ingroup RAPIDJSON_CONFIG + + GCC 4.6 provided macro for detecting endianness of the target machine. But other + compilers may not have this. User can define RAPIDJSON_ENDIAN to either + \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN. + + Default detection implemented with reference to + \li https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html + \li http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp +*/ +#ifndef RAPIDJSON_ENDIAN +// Detect with GCC 4.6's macro +# ifdef __BYTE_ORDER__ +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif // __BYTE_ORDER__ +// Detect with GLIBC's endian.h +# elif defined(__GLIBC__) +# include +# if (__BYTE_ORDER == __LITTLE_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif (__BYTE_ORDER == __BIG_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif // __GLIBC__ +// Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro +# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +// Detect with architecture macros +# elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(_MSC_VER) && defined(_M_ARM) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(RAPIDJSON_DOXYGEN_RUNNING) +# define RAPIDJSON_ENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif +#endif // RAPIDJSON_ENDIAN + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_64BIT + +//! Whether using 64-bit architecture +#ifndef RAPIDJSON_64BIT +#if defined(__LP64__) || (defined(__x86_64__) && defined(__ILP32__)) || defined(_WIN64) || defined(__EMSCRIPTEN__) +#define RAPIDJSON_64BIT 1 +#else +#define RAPIDJSON_64BIT 0 +#endif +#endif // RAPIDJSON_64BIT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ALIGN + +//! Data alignment of the machine. +/*! \ingroup RAPIDJSON_CONFIG + \param x pointer to align + + Some machines require strict data alignment. Currently the default uses 4 bytes + alignment on 32-bit platforms and 8 bytes alignment for 64-bit platforms. + User can customize by defining the RAPIDJSON_ALIGN function macro. +*/ +#ifndef RAPIDJSON_ALIGN +#if RAPIDJSON_64BIT == 1 +#define RAPIDJSON_ALIGN(x) (((x) + static_cast(7u)) & ~static_cast(7u)) +#else +#define RAPIDJSON_ALIGN(x) (((x) + 3u) & ~3u) +#endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_UINT64_C2 + +//! Construct a 64-bit literal by a pair of 32-bit integer. +/*! + 64-bit literal with or without ULL suffix is prone to compiler warnings. + UINT64_C() is C macro which cause compilation problems. + Use this macro to define 64-bit constants by a pair of 32-bit integer. +*/ +#ifndef RAPIDJSON_UINT64_C2 +#define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast(high32) << 32) | static_cast(low32)) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_48BITPOINTER_OPTIMIZATION + +//! Use only lower 48-bit address for some pointers. +/*! + \ingroup RAPIDJSON_CONFIG + + This optimization uses the fact that current X86-64 architecture only implement lower 48-bit virtual address. + The higher 16-bit can be used for storing other data. + \c GenericValue uses this optimization to reduce its size form 24 bytes to 16 bytes in 64-bit architecture. +*/ +#ifndef RAPIDJSON_48BITPOINTER_OPTIMIZATION +#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64) +#define RAPIDJSON_48BITPOINTER_OPTIMIZATION 1 +#else +#define RAPIDJSON_48BITPOINTER_OPTIMIZATION 0 +#endif +#endif // RAPIDJSON_48BITPOINTER_OPTIMIZATION + +#if RAPIDJSON_48BITPOINTER_OPTIMIZATION == 1 +#if RAPIDJSON_64BIT != 1 +#error RAPIDJSON_48BITPOINTER_OPTIMIZATION can only be set to 1 when RAPIDJSON_64BIT=1 +#endif +#define RAPIDJSON_SETPOINTER(type, p, x) (p = reinterpret_cast((reinterpret_cast(p) & static_cast(RAPIDJSON_UINT64_C2(0xFFFF0000, 0x00000000))) | reinterpret_cast(reinterpret_cast(x)))) +#define RAPIDJSON_GETPOINTER(type, p) (reinterpret_cast(reinterpret_cast(p) & static_cast(RAPIDJSON_UINT64_C2(0x0000FFFF, 0xFFFFFFFF)))) +#else +#define RAPIDJSON_SETPOINTER(type, p, x) (p = (x)) +#define RAPIDJSON_GETPOINTER(type, p) (p) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD + +/*! \def RAPIDJSON_SIMD + \ingroup RAPIDJSON_CONFIG + \brief Enable SSE2/SSE4.2 optimization. + + RapidJSON supports optimized implementations for some parsing operations + based on the SSE2 or SSE4.2 SIMD extensions on modern Intel-compatible + processors. + + To enable these optimizations, two different symbols can be defined; + \code + // Enable SSE2 optimization. + #define RAPIDJSON_SSE2 + + // Enable SSE4.2 optimization. + #define RAPIDJSON_SSE42 + \endcode + + \c RAPIDJSON_SSE42 takes precedence, if both are defined. + + If any of these symbols is defined, RapidJSON defines the macro + \c RAPIDJSON_SIMD to indicate the availability of the optimized code. +*/ +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \ + || defined(RAPIDJSON_DOXYGEN_RUNNING) +#define RAPIDJSON_SIMD +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NO_SIZETYPEDEFINE + +#ifndef RAPIDJSON_NO_SIZETYPEDEFINE +/*! \def RAPIDJSON_NO_SIZETYPEDEFINE + \ingroup RAPIDJSON_CONFIG + \brief User-provided \c SizeType definition. + + In order to avoid using 32-bit size types for indexing strings and arrays, + define this preprocessor symbol and provide the type rapidjson::SizeType + before including RapidJSON: + \code + #define RAPIDJSON_NO_SIZETYPEDEFINE + namespace rapidjson { typedef ::std::size_t SizeType; } + #include "rapidjson/..." + \endcode + + \see rapidjson::SizeType +*/ +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_NO_SIZETYPEDEFINE +#endif +RAPIDJSON_NAMESPACE_BEGIN +//! Size type (for string lengths, array sizes, etc.) +/*! RapidJSON uses 32-bit array/string indices even on 64-bit platforms, + instead of using \c size_t. Users may override the SizeType by defining + \ref RAPIDJSON_NO_SIZETYPEDEFINE. +*/ +typedef unsigned SizeType; +RAPIDJSON_NAMESPACE_END +#endif + +// always import std::size_t to rapidjson namespace +RAPIDJSON_NAMESPACE_BEGIN +using std::size_t; +RAPIDJSON_NAMESPACE_END + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ASSERT + +//! Assertion. +/*! \ingroup RAPIDJSON_CONFIG + By default, rapidjson uses C \c assert() for internal assertions. + User can override it by defining RAPIDJSON_ASSERT(x) macro. + + \note Parsing errors are handled and can be customized by the + \ref RAPIDJSON_ERRORS APIs. +*/ +#ifndef RAPIDJSON_ASSERT +#define RAPIDJSON_ASSERT(x) +#endif // RAPIDJSON_ASSERT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_STATIC_ASSERT + +// Adopt from boost +#ifndef RAPIDJSON_STATIC_ASSERT +#ifndef __clang__ +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#endif +RAPIDJSON_NAMESPACE_BEGIN +template struct STATIC_ASSERTION_FAILURE; +template <> struct STATIC_ASSERTION_FAILURE { enum { value = 1 }; }; +template struct StaticAssertTest {}; +RAPIDJSON_NAMESPACE_END + +#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y) +#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y) +#define RAPIDJSON_DO_JOIN2(X, Y) X##Y + +#if defined(__GNUC__) +#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused)) +#else +#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE +#endif +#ifndef __clang__ +//!@endcond +#endif + +/*! \def RAPIDJSON_STATIC_ASSERT + \brief (Internal) macro to check for conditions at compile-time + \param x compile-time condition + \hideinitializer + */ +#define RAPIDJSON_STATIC_ASSERT(x) \ + typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest< \ + sizeof(::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE)> \ + RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_LIKELY, RAPIDJSON_UNLIKELY + +//! Compiler branching hint for expression with high probability to be true. +/*! + \ingroup RAPIDJSON_CONFIG + \param x Boolean expression likely to be true. +*/ +#ifndef RAPIDJSON_LIKELY +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_LIKELY(x) __builtin_expect(!!(x), 1) +#else +#define RAPIDJSON_LIKELY(x) (x) +#endif +#endif + +//! Compiler branching hint for expression with low probability to be true. +/*! + \ingroup RAPIDJSON_CONFIG + \param x Boolean expression unlikely to be true. +*/ +#ifndef RAPIDJSON_UNLIKELY +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +#define RAPIDJSON_UNLIKELY(x) (x) +#endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Helpers + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN + +#define RAPIDJSON_MULTILINEMACRO_BEGIN do { +#define RAPIDJSON_MULTILINEMACRO_END \ +} while((void)0, 0) + +// adopted from Boost +#define RAPIDJSON_VERSION_CODE(x,y,z) \ + (((x)*100000) + ((y)*100) + (z)) + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF + +#if defined(__GNUC__) +#define RAPIDJSON_GNUC \ + RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) +#endif + +#if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,2,0)) + +#define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x)) +#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x) +#define RAPIDJSON_DIAG_OFF(x) \ + RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x))) + +// push/pop support in Clang and GCC>=4.6 +#if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) +#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) +#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) +#else // GCC >= 4.2, < 4.6 +#define RAPIDJSON_DIAG_PUSH /* ignored */ +#define RAPIDJSON_DIAG_POP /* ignored */ +#endif + +#elif defined(_MSC_VER) + +// pragma (MSVC specific) +#define RAPIDJSON_PRAGMA(x) __pragma(x) +#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x)) + +#define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x) +#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) +#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) + +#else + +#define RAPIDJSON_DIAG_OFF(x) /* ignored */ +#define RAPIDJSON_DIAG_PUSH /* ignored */ +#define RAPIDJSON_DIAG_POP /* ignored */ + +#endif // RAPIDJSON_DIAG_* + +/////////////////////////////////////////////////////////////////////////////// +// C++11 features + +#ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS +#if defined(__clang__) +#if __has_feature(cxx_rvalue_references) && \ + (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306) +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#else +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 +#endif +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ + (defined(_MSC_VER) && _MSC_VER >= 1600) + +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#else +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 +#endif +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + +#ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT +#if defined(__clang__) +#define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept) +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) +// (defined(_MSC_VER) && _MSC_VER >= ????) // not yet supported +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 1 +#else +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 0 +#endif +#endif +#if RAPIDJSON_HAS_CXX11_NOEXCEPT +#define RAPIDJSON_NOEXCEPT noexcept +#else +#define RAPIDJSON_NOEXCEPT /* noexcept */ +#endif // RAPIDJSON_HAS_CXX11_NOEXCEPT + +// no automatic detection, yet +#ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS +#define RAPIDJSON_HAS_CXX11_TYPETRAITS 0 +#endif + +#ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR +#if defined(__clang__) +#define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for) +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ + (defined(_MSC_VER) && _MSC_VER >= 1700) +#define RAPIDJSON_HAS_CXX11_RANGE_FOR 1 +#else +#define RAPIDJSON_HAS_CXX11_RANGE_FOR 0 +#endif +#endif // RAPIDJSON_HAS_CXX11_RANGE_FOR + +//!@endcond + +/////////////////////////////////////////////////////////////////////////////// +// new/delete + +#ifndef RAPIDJSON_NEW +///! customization point for global \c new +#define RAPIDJSON_NEW(x) new x +#endif +#ifndef RAPIDJSON_DELETE +///! customization point for global \c delete +#define RAPIDJSON_DELETE(x) delete x +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Type + +/*! \namespace rapidjson + \brief main RapidJSON namespace + \see RAPIDJSON_NAMESPACE +*/ +RAPIDJSON_NAMESPACE_BEGIN + +//! Type of JSON value +enum Type { + kNullType = 0, //!< null + kFalseType = 1, //!< false + kTrueType = 2, //!< true + kObjectType = 3, //!< object + kArrayType = 4, //!< array + kStringType = 5, //!< string + kNumberType = 6 //!< number +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/src/3rdparty/rapidjson/reader.h b/src/3rdparty/rapidjson/reader.h new file mode 100644 index 00000000..303aac2e --- /dev/null +++ b/src/3rdparty/rapidjson/reader.h @@ -0,0 +1,1879 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_READER_H_ +#define RAPIDJSON_READER_H_ + +/*! \file reader.h */ + +#include "allocators.h" +#include "stream.h" +#include "encodedstream.h" +#include "internal/meta.h" +#include "internal/stack.h" +#include "internal/strtod.h" +#include + +#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) +#include +#pragma intrinsic(_BitScanForward) +#endif +#ifdef RAPIDJSON_SSE42 +#include +#elif defined(RAPIDJSON_SSE2) +#include +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(old-style-cast) +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define RAPIDJSON_NOTHING /* deliberately empty */ +#ifndef RAPIDJSON_PARSE_ERROR_EARLY_RETURN +#define RAPIDJSON_PARSE_ERROR_EARLY_RETURN(value) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + if (RAPIDJSON_UNLIKELY(HasParseError())) { return value; } \ + RAPIDJSON_MULTILINEMACRO_END +#endif +#define RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID \ + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(RAPIDJSON_NOTHING) +//!@endcond + +/*! \def RAPIDJSON_PARSE_ERROR_NORETURN + \ingroup RAPIDJSON_ERRORS + \brief Macro to indicate a parse error. + \param parseErrorCode \ref rapidjson::ParseErrorCode of the error + \param offset position of the error in JSON input (\c size_t) + + This macros can be used as a customization point for the internal + error handling mechanism of RapidJSON. + + A common usage model is to throw an exception instead of requiring the + caller to explicitly check the \ref rapidjson::GenericReader::Parse's + return value: + + \code + #define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode,offset) \ + throw ParseException(parseErrorCode, #parseErrorCode, offset) + + #include // std::runtime_error + #include "rapidjson/error/error.h" // rapidjson::ParseResult + + struct ParseException : std::runtime_error, rapidjson::ParseResult { + ParseException(rapidjson::ParseErrorCode code, const char* msg, size_t offset) + : std::runtime_error(msg), ParseResult(code, offset) {} + }; + + #include "rapidjson/reader.h" + \endcode + + \see RAPIDJSON_PARSE_ERROR, rapidjson::GenericReader::Parse + */ +#ifndef RAPIDJSON_PARSE_ERROR_NORETURN +#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + RAPIDJSON_ASSERT(!HasParseError()); /* Error can only be assigned once */ \ + SetParseError(parseErrorCode, offset); \ + RAPIDJSON_MULTILINEMACRO_END +#endif + +/*! \def RAPIDJSON_PARSE_ERROR + \ingroup RAPIDJSON_ERRORS + \brief (Internal) macro to indicate and handle a parse error. + \param parseErrorCode \ref rapidjson::ParseErrorCode of the error + \param offset position of the error in JSON input (\c size_t) + + Invokes RAPIDJSON_PARSE_ERROR_NORETURN and stops the parsing. + + \see RAPIDJSON_PARSE_ERROR_NORETURN + \hideinitializer + */ +#ifndef RAPIDJSON_PARSE_ERROR +#define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset); \ + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; \ + RAPIDJSON_MULTILINEMACRO_END +#endif + +#include "error/error.h" // ParseErrorCode, ParseResult + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// ParseFlag + +/*! \def RAPIDJSON_PARSE_DEFAULT_FLAGS + \ingroup RAPIDJSON_CONFIG + \brief User-defined kParseDefaultFlags definition. + + User can define this as any \c ParseFlag combinations. +*/ +#ifndef RAPIDJSON_PARSE_DEFAULT_FLAGS +#define RAPIDJSON_PARSE_DEFAULT_FLAGS kParseCommentsFlag | kParseTrailingCommasFlag +#endif + +//! Combination of parseFlags +/*! \see Reader::Parse, Document::Parse, Document::ParseInsitu, Document::ParseStream + */ +enum ParseFlag { + kParseNoFlags = 0, //!< No flags are set. + kParseInsituFlag = 1, //!< In-situ(destructive) parsing. + kParseValidateEncodingFlag = 2, //!< Validate encoding of JSON strings. + kParseIterativeFlag = 4, //!< Iterative(constant complexity in terms of function call stack size) parsing. + kParseStopWhenDoneFlag = 8, //!< After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate kParseErrorDocumentRootNotSingular error. + kParseFullPrecisionFlag = 16, //!< Parse number in full precision (but slower). + kParseCommentsFlag = 32, //!< Allow one-line (//) and multi-line (/**/) comments. + kParseNumbersAsStringsFlag = 64, //!< Parse all numbers (ints/doubles) as strings. + kParseTrailingCommasFlag = 128, //!< Allow trailing commas at the end of objects and arrays. + kParseNanAndInfFlag = 256, //!< Allow parsing NaN, Inf, Infinity, -Inf and -Infinity as doubles. + kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS //!< Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS +}; + +/////////////////////////////////////////////////////////////////////////////// +// Handler + +/*! \class rapidjson::Handler + \brief Concept for receiving events from GenericReader upon parsing. + The functions return true if no error occurs. If they return false, + the event publisher should terminate the process. +\code +concept Handler { + typename Ch; + + bool Null(); + bool Bool(bool b); + bool Int(int i); + bool Uint(unsigned i); + bool Int64(int64_t i); + bool Uint64(uint64_t i); + bool Double(double d); + /// enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length) + bool RawNumber(const Ch* str, SizeType length, bool copy); + bool String(const Ch* str, SizeType length, bool copy); + bool StartObject(); + bool Key(const Ch* str, SizeType length, bool copy); + bool EndObject(SizeType memberCount); + bool StartArray(); + bool EndArray(SizeType elementCount); +}; +\endcode +*/ +/////////////////////////////////////////////////////////////////////////////// +// BaseReaderHandler + +//! Default implementation of Handler. +/*! This can be used as base class of any reader handler. + \note implements Handler concept +*/ +template, typename Derived = void> +struct BaseReaderHandler { + typedef typename Encoding::Ch Ch; + + typedef typename internal::SelectIf, BaseReaderHandler, Derived>::Type Override; + + bool Default() { return true; } + bool Null() { return static_cast(*this).Default(); } + bool Bool(bool) { return static_cast(*this).Default(); } + bool Int(int) { return static_cast(*this).Default(); } + bool Uint(unsigned) { return static_cast(*this).Default(); } + bool Int64(int64_t) { return static_cast(*this).Default(); } + bool Uint64(uint64_t) { return static_cast(*this).Default(); } + bool Double(double) { return static_cast(*this).Default(); } + /// enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length) + bool RawNumber(const Ch* str, SizeType len, bool copy) { return static_cast(*this).String(str, len, copy); } + bool String(const Ch*, SizeType, bool) { return static_cast(*this).Default(); } + bool StartObject() { return static_cast(*this).Default(); } + bool Key(const Ch* str, SizeType len, bool copy) { return static_cast(*this).String(str, len, copy); } + bool EndObject(SizeType) { return static_cast(*this).Default(); } + bool StartArray() { return static_cast(*this).Default(); } + bool EndArray(SizeType) { return static_cast(*this).Default(); } +}; + +/////////////////////////////////////////////////////////////////////////////// +// StreamLocalCopy + +namespace internal { + +template::copyOptimization> +class StreamLocalCopy; + +//! Do copy optimization. +template +class StreamLocalCopy { +public: + StreamLocalCopy(Stream& original) : s(original), original_(original) {} + ~StreamLocalCopy() { original_ = s; } + + Stream s; + +private: + StreamLocalCopy& operator=(const StreamLocalCopy&) /* = delete */; + + Stream& original_; +}; + +//! Keep reference. +template +class StreamLocalCopy { +public: + StreamLocalCopy(Stream& original) : s(original) {} + + Stream& s; + +private: + StreamLocalCopy& operator=(const StreamLocalCopy&) /* = delete */; +}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// SkipWhitespace + +//! Skip the JSON white spaces in a stream. +/*! \param is A input stream for skipping white spaces. + \note This function has SSE2/SSE4.2 specialization. +*/ +template +void SkipWhitespace(InputStream& is) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + typename InputStream::Ch c; + while ((c = s.Peek()) == ' ' || c == '\n' || c == '\r' || c == '\t') + s.Take(); +} + +inline const char* SkipWhitespace(const char* p, const char* end) { + while (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + return p; +} + +#ifdef RAPIDJSON_SSE42 +//! Skip whitespace with SSE 4.2 pcmpistrm instruction, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // The rest of string using SIMD + static const char whitespace[16] = " \n\r\t"; + const __m128i w = _mm_loadu_si128(reinterpret_cast(&whitespace[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const int r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY)); + if (r != 0) { // some of characters is non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } +} + +inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { + // Fast return for single non-whitespace + if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + else + return p; + + // The middle of string using SIMD + static const char whitespace[16] = " \n\r\t"; + const __m128i w = _mm_loadu_si128(reinterpret_cast(&whitespace[0])); + + for (; p <= end - 16; p += 16) { + const __m128i s = _mm_loadu_si128(reinterpret_cast(p)); + const int r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY)); + if (r != 0) { // some of characters is non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } + + return SkipWhitespace(p, end); +} + +#elif defined(RAPIDJSON_SSE2) + +//! Skip whitespace with SSE2 instructions, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // The rest of string + #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c } + static const char whitespaces[4][16] = { C16(' '), C16('\n'), C16('\r'), C16('\t') }; + #undef C16 + + const __m128i w0 = _mm_loadu_si128(reinterpret_cast(&whitespaces[0][0])); + const __m128i w1 = _mm_loadu_si128(reinterpret_cast(&whitespaces[1][0])); + const __m128i w2 = _mm_loadu_si128(reinterpret_cast(&whitespaces[2][0])); + const __m128i w3 = _mm_loadu_si128(reinterpret_cast(&whitespaces[3][0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + __m128i x = _mm_cmpeq_epi8(s, w0); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3)); + unsigned short r = static_cast(~_mm_movemask_epi8(x)); + if (r != 0) { // some of characters may be non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } +} + +inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { + // Fast return for single non-whitespace + if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + else + return p; + + // The rest of string + #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c } + static const char whitespaces[4][16] = { C16(' '), C16('\n'), C16('\r'), C16('\t') }; + #undef C16 + + const __m128i w0 = _mm_loadu_si128(reinterpret_cast(&whitespaces[0][0])); + const __m128i w1 = _mm_loadu_si128(reinterpret_cast(&whitespaces[1][0])); + const __m128i w2 = _mm_loadu_si128(reinterpret_cast(&whitespaces[2][0])); + const __m128i w3 = _mm_loadu_si128(reinterpret_cast(&whitespaces[3][0])); + + for (; p <= end - 16; p += 16) { + const __m128i s = _mm_loadu_si128(reinterpret_cast(p)); + __m128i x = _mm_cmpeq_epi8(s, w0); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3)); + unsigned short r = static_cast(~_mm_movemask_epi8(x)); + if (r != 0) { // some of characters may be non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } + + return SkipWhitespace(p, end); +} + +#endif // RAPIDJSON_SSE2 + +#ifdef RAPIDJSON_SIMD +//! Template function specialization for InsituStringStream +template<> inline void SkipWhitespace(InsituStringStream& is) { + is.src_ = const_cast(SkipWhitespace_SIMD(is.src_)); +} + +//! Template function specialization for StringStream +template<> inline void SkipWhitespace(StringStream& is) { + is.src_ = SkipWhitespace_SIMD(is.src_); +} + +template<> inline void SkipWhitespace(EncodedInputStream, MemoryStream>& is) { + is.is_.src_ = SkipWhitespace_SIMD(is.is_.src_, is.is_.end_); +} +#endif // RAPIDJSON_SIMD + +/////////////////////////////////////////////////////////////////////////////// +// GenericReader + +//! SAX-style JSON parser. Use \ref Reader for UTF8 encoding and default allocator. +/*! GenericReader parses JSON text from a stream, and send events synchronously to an + object implementing Handler concept. + + It needs to allocate a stack for storing a single decoded string during + non-destructive parsing. + + For in-situ parsing, the decoded string is directly written to the source + text string, no temporary buffer is required. + + A GenericReader object can be reused for parsing multiple JSON text. + + \tparam SourceEncoding Encoding of the input stream. + \tparam TargetEncoding Encoding of the parse output. + \tparam StackAllocator Allocator type for stack. +*/ +template +class GenericReader { +public: + typedef typename SourceEncoding::Ch Ch; //!< SourceEncoding character type + + //! Constructor. + /*! \param stackAllocator Optional allocator for allocating stack memory. (Only use for non-destructive parsing) + \param stackCapacity stack capacity in bytes for storing a single decoded string. (Only use for non-destructive parsing) + */ + GenericReader(StackAllocator* stackAllocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(stackAllocator, stackCapacity), parseResult_() {} + + //! Parse JSON text. + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept. + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult Parse(InputStream& is, Handler& handler) { + if (parseFlags & kParseIterativeFlag) + return IterativeParse(is, handler); + + parseResult_.Clear(); + + ClearStackOnExit scope(*this); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (RAPIDJSON_UNLIKELY(is.Peek() == '\0')) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentEmpty, is.Tell()); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + else { + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (!(parseFlags & kParseStopWhenDoneFlag)) { + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (RAPIDJSON_UNLIKELY(is.Peek() != '\0')) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotSingular, is.Tell()); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + } + } + + return parseResult_; + } + + //! Parse JSON text (with \ref kParseDefaultFlags) + /*! \tparam InputStream Type of input stream, implementing Stream concept + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult Parse(InputStream& is, Handler& handler) { + return Parse(is, handler); + } + + //! Whether a parse error has occured in the last parsing. + bool HasParseError() const { return parseResult_.IsError(); } + + //! Get the \ref ParseErrorCode of last parsing. + ParseErrorCode GetParseErrorCode() const { return parseResult_.Code(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorOffset() const { return parseResult_.Offset(); } + +protected: + void SetParseError(ParseErrorCode code, size_t offset) { parseResult_.Set(code, offset); } + +private: + // Prohibit copy constructor & assignment operator. + GenericReader(const GenericReader&); + GenericReader& operator=(const GenericReader&); + + void ClearStack() { stack_.Clear(); } + + // clear stack on any exit from ParseStream, e.g. due to exception + struct ClearStackOnExit { + explicit ClearStackOnExit(GenericReader& r) : r_(r) {} + ~ClearStackOnExit() { r_.ClearStack(); } + private: + GenericReader& r_; + ClearStackOnExit(const ClearStackOnExit&); + ClearStackOnExit& operator=(const ClearStackOnExit&); + }; + + template + void SkipWhitespaceAndComments(InputStream& is) { + SkipWhitespace(is); + + if (parseFlags & kParseCommentsFlag) { + while (RAPIDJSON_UNLIKELY(Consume(is, '/'))) { + if (Consume(is, '*')) { + while (true) { + if (RAPIDJSON_UNLIKELY(is.Peek() == '\0')) + RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); + else if (Consume(is, '*')) { + if (Consume(is, '/')) + break; + } + else + is.Take(); + } + } + else if (RAPIDJSON_LIKELY(Consume(is, '/'))) + while (is.Peek() != '\0' && is.Take() != '\n'); + else + RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); + + SkipWhitespace(is); + } + } + } + + // Parse object: { string : value, ... } + template + void ParseObject(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == '{'); + is.Take(); // Skip '{' + + if (RAPIDJSON_UNLIKELY(!handler.StartObject())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, '}')) { + if (RAPIDJSON_UNLIKELY(!handler.EndObject(0))) // empty object + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + + for (SizeType memberCount = 0;;) { + if (RAPIDJSON_UNLIKELY(is.Peek() != '"')) + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); + + ParseString(is, handler, true); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (RAPIDJSON_UNLIKELY(!Consume(is, ':'))) + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ++memberCount; + + switch (is.Peek()) { + case ',': + is.Take(); + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + break; + case '}': + is.Take(); + if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + default: + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); break; // This useless break is only for making warning and coverage happy + } + + if (parseFlags & kParseTrailingCommasFlag) { + if (is.Peek() == '}') { + if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + is.Take(); + return; + } + } + } + } + + // Parse array: [ value, ... ] + template + void ParseArray(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == '['); + is.Take(); // Skip '[' + + if (RAPIDJSON_UNLIKELY(!handler.StartArray())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, ']')) { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(0))) // empty array + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + + for (SizeType elementCount = 0;;) { + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ++elementCount; + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, ',')) { + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + } + else if (Consume(is, ']')) { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); + + if (parseFlags & kParseTrailingCommasFlag) { + if (is.Peek() == ']') { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + is.Take(); + return; + } + } + } + } + + template + void ParseNull(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 'n'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'u') && Consume(is, 'l') && Consume(is, 'l'))) { + if (RAPIDJSON_UNLIKELY(!handler.Null())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + void ParseTrue(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 't'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'r') && Consume(is, 'u') && Consume(is, 'e'))) { + if (RAPIDJSON_UNLIKELY(!handler.Bool(true))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + void ParseFalse(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 'f'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'a') && Consume(is, 'l') && Consume(is, 's') && Consume(is, 'e'))) { + if (RAPIDJSON_UNLIKELY(!handler.Bool(false))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + RAPIDJSON_FORCEINLINE static bool Consume(InputStream& is, typename InputStream::Ch expect) { + if (RAPIDJSON_LIKELY(is.Peek() == expect)) { + is.Take(); + return true; + } + else + return false; + } + + // Helper function to parse four hexidecimal digits in \uXXXX in ParseString(). + template + unsigned ParseHex4(InputStream& is, size_t escapeOffset) { + unsigned codepoint = 0; + for (int i = 0; i < 4; i++) { + Ch c = is.Peek(); + codepoint <<= 4; + codepoint += static_cast(c); + if (c >= '0' && c <= '9') + codepoint -= '0'; + else if (c >= 'A' && c <= 'F') + codepoint -= 'A' - 10; + else if (c >= 'a' && c <= 'f') + codepoint -= 'a' - 10; + else { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorStringUnicodeEscapeInvalidHex, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(0); + } + is.Take(); + } + return codepoint; + } + + template + class StackStream { + public: + typedef CharType Ch; + + StackStream(internal::Stack& stack) : stack_(stack), length_(0) {} + RAPIDJSON_FORCEINLINE void Put(Ch c) { + *stack_.template Push() = c; + ++length_; + } + + RAPIDJSON_FORCEINLINE void* Push(SizeType count) { + length_ += count; + return stack_.template Push(count); + } + + size_t Length() const { return length_; } + + Ch* Pop() { + return stack_.template Pop(length_); + } + + private: + StackStream(const StackStream&); + StackStream& operator=(const StackStream&); + + internal::Stack& stack_; + SizeType length_; + }; + + // Parse string and generate String event. Different code paths for kParseInsituFlag. + template + void ParseString(InputStream& is, Handler& handler, bool isKey = false) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + RAPIDJSON_ASSERT(s.Peek() == '\"'); + s.Take(); // Skip '\"' + + bool success = false; + if (parseFlags & kParseInsituFlag) { + typename InputStream::Ch *head = s.PutBegin(); + ParseStringToStream(s, s); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + size_t length = s.PutEnd(head) - 1; + RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); + const typename TargetEncoding::Ch* const str = reinterpret_cast(head); + success = (isKey ? handler.Key(str, SizeType(length), false) : handler.String(str, SizeType(length), false)); + } + else { + StackStream stackStream(stack_); + ParseStringToStream(s, stackStream); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + SizeType length = static_cast(stackStream.Length()) - 1; + const typename TargetEncoding::Ch* const str = stackStream.Pop(); + success = (isKey ? handler.Key(str, length, true) : handler.String(str, length, true)); + } + if (RAPIDJSON_UNLIKELY(!success)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell()); + } + + // Parse string to an output is + // This function handles the prefix/suffix double quotes, escaping, and optional encoding validation. + template + RAPIDJSON_FORCEINLINE void ParseStringToStream(InputStream& is, OutputStream& os) { +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + static const char escape[256] = { + Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/', + Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, + 0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0, + 0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 + }; +#undef Z16 +//!@endcond + + for (;;) { + // Scan and copy string before "\\\"" or < 0x20. This is an optional optimzation. + if (!(parseFlags & kParseValidateEncodingFlag)) + ScanCopyUnescapedString(is, os); + + Ch c = is.Peek(); + if (RAPIDJSON_UNLIKELY(c == '\\')) { // Escape + size_t escapeOffset = is.Tell(); // For invalid escaping, report the inital '\\' as error offset + is.Take(); + Ch e = is.Peek(); + if ((sizeof(Ch) == 1 || unsigned(e) < 256) && RAPIDJSON_LIKELY(escape[static_cast(e)])) { + is.Take(); + os.Put(static_cast(escape[static_cast(e)])); + } + else if (RAPIDJSON_LIKELY(e == 'u')) { // Unicode + is.Take(); + unsigned codepoint = ParseHex4(is, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + if (RAPIDJSON_UNLIKELY(codepoint >= 0xD800 && codepoint <= 0xDBFF)) { + // Handle UTF-16 surrogate pair + if (RAPIDJSON_UNLIKELY(!Consume(is, '\\') || !Consume(is, 'u'))) + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, escapeOffset); + unsigned codepoint2 = ParseHex4(is, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + if (RAPIDJSON_UNLIKELY(codepoint2 < 0xDC00 || codepoint2 > 0xDFFF)) + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, escapeOffset); + codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000; + } + TEncoding::Encode(os, codepoint); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, escapeOffset); + } + else if (RAPIDJSON_UNLIKELY(c == '"')) { // Closing double quote + is.Take(); + os.Put('\0'); // null-terminate the string + return; + } + else if (RAPIDJSON_UNLIKELY(static_cast(c) < 0x20)) { // RFC 4627: unescaped = %x20-21 / %x23-5B / %x5D-10FFFF + if (c == '\0') + RAPIDJSON_PARSE_ERROR(kParseErrorStringMissQuotationMark, is.Tell()); + else + RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, is.Tell()); + } + else { + size_t offset = is.Tell(); + if (RAPIDJSON_UNLIKELY((parseFlags & kParseValidateEncodingFlag ? + !Transcoder::Validate(is, os) : + !Transcoder::Transcode(is, os)))) + RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, offset); + } + } + } + + template + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InputStream&, OutputStream&) { + // Do nothing for generic version + } + +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) + // StringStream -> StackStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(StringStream& is, StackStream& os) { + const char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + return; + } + else + os.Put(*p++); + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + SizeType length; + #ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; + #else + length = static_cast(__builtin_ffs(r) - 1); + #endif + char* q = reinterpret_cast(os.Push(length)); + for (size_t i = 0; i < length; i++) + q[i] = p[i]; + + p += length; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(os.Push(16)), s); + } + + is.src_ = p; + } + + // InsituStringStream -> InsituStringStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InsituStringStream& is, InsituStringStream& os) { + RAPIDJSON_ASSERT(&is == &os); + (void)os; + + if (is.src_ == is.dst_) { + SkipUnescapedString(is); + return; + } + + char* p = is.src_; + char *q = is.dst_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + is.dst_ = q; + return; + } + else + *q++ = *p++; + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16, q += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + size_t length; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; +#else + length = static_cast(__builtin_ffs(r) - 1); +#endif + for (const char* pend = p + length; p != pend; ) + *q++ = *p++; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(q), s); + } + + is.src_ = p; + is.dst_ = q; + } + + // When read/write pointers are the same for insitu stream, just skip unescaped characters + static RAPIDJSON_FORCEINLINE void SkipUnescapedString(InsituStringStream& is) { + RAPIDJSON_ASSERT(is.src_ == is.dst_); + char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + for (; p != nextAligned; p++) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = is.dst_ = p; + return; + } + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + size_t length; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; +#else + length = static_cast(__builtin_ffs(r) - 1); +#endif + p += length; + break; + } + } + + is.src_ = is.dst_ = p; + } +#endif + + template + class NumberStream; + + template + class NumberStream { + public: + typedef typename InputStream::Ch Ch; + + NumberStream(GenericReader& reader, InputStream& s) : is(s) { (void)reader; } + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch Peek() const { return is.Peek(); } + RAPIDJSON_FORCEINLINE Ch TakePush() { return is.Take(); } + RAPIDJSON_FORCEINLINE Ch Take() { return is.Take(); } + RAPIDJSON_FORCEINLINE void Push(char) {} + + size_t Tell() { return is.Tell(); } + size_t Length() { return 0; } + const char* Pop() { return 0; } + + protected: + NumberStream& operator=(const NumberStream&); + + InputStream& is; + }; + + template + class NumberStream : public NumberStream { + typedef NumberStream Base; + public: + NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is), stackStream(reader.stack_) {} + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch TakePush() { + stackStream.Put(static_cast(Base::is.Peek())); + return Base::is.Take(); + } + + RAPIDJSON_FORCEINLINE void Push(char c) { + stackStream.Put(c); + } + + size_t Length() { return stackStream.Length(); } + + const char* Pop() { + stackStream.Put('\0'); + return stackStream.Pop(); + } + + private: + StackStream stackStream; + }; + + template + class NumberStream : public NumberStream { + typedef NumberStream Base; + public: + NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is) {} + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch Take() { return Base::TakePush(); } + }; + + template + void ParseNumber(InputStream& is, Handler& handler) { + internal::StreamLocalCopy copy(is); + NumberStream s(*this, copy.s); + + size_t startOffset = s.Tell(); + double d = 0.0; + bool useNanOrInf = false; + + // Parse minus + bool minus = Consume(s, '-'); + + // Parse int: zero / ( digit1-9 *DIGIT ) + unsigned i = 0; + uint64_t i64 = 0; + bool use64bit = false; + int significandDigit = 0; + if (RAPIDJSON_UNLIKELY(s.Peek() == '0')) { + i = 0; + s.TakePush(); + } + else if (RAPIDJSON_LIKELY(s.Peek() >= '1' && s.Peek() <= '9')) { + i = static_cast(s.TakePush() - '0'); + + if (minus) + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i >= 214748364)) { // 2^31 = 2147483648 + if (RAPIDJSON_LIKELY(i != 214748364 || s.Peek() > '8')) { + i64 = i; + use64bit = true; + break; + } + } + i = i * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + else + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i >= 429496729)) { // 2^32 - 1 = 4294967295 + if (RAPIDJSON_LIKELY(i != 429496729 || s.Peek() > '5')) { + i64 = i; + use64bit = true; + break; + } + } + i = i * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + } + // Parse NaN or Infinity here + else if ((parseFlags & kParseNanAndInfFlag) && RAPIDJSON_LIKELY((s.Peek() == 'I' || s.Peek() == 'N'))) { + useNanOrInf = true; + if (RAPIDJSON_LIKELY(Consume(s, 'N') && Consume(s, 'a') && Consume(s, 'N'))) { + d = std::numeric_limits::quiet_NaN(); + } + else if (RAPIDJSON_LIKELY(Consume(s, 'I') && Consume(s, 'n') && Consume(s, 'f'))) { + d = (minus ? -std::numeric_limits::infinity() : std::numeric_limits::infinity()); + if (RAPIDJSON_UNLIKELY(s.Peek() == 'i' && !(Consume(s, 'i') && Consume(s, 'n') + && Consume(s, 'i') && Consume(s, 't') && Consume(s, 'y')))) + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + + // Parse 64bit int + bool useDouble = false; + if (use64bit) { + if (minus) + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC))) // 2^63 = 9223372036854775808 + if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC) || s.Peek() > '8')) { + d = static_cast(i64); + useDouble = true; + break; + } + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + else + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x19999999, 0x99999999))) // 2^64 - 1 = 18446744073709551615 + if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || s.Peek() > '5')) { + d = static_cast(i64); + useDouble = true; + break; + } + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + } + + // Force double for big integer + if (useDouble) { + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(d >= 1.7976931348623157e307)) // DBL_MAX / 10.0 + RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, startOffset); + d = d * 10 + (s.TakePush() - '0'); + } + } + + // Parse frac = decimal-point 1*DIGIT + int expFrac = 0; + size_t decimalPosition; + if (Consume(s, '.')) { + decimalPosition = s.Length(); + + if (RAPIDJSON_UNLIKELY(!(s.Peek() >= '0' && s.Peek() <= '9'))) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberMissFraction, s.Tell()); + + if (!useDouble) { +#if RAPIDJSON_64BIT + // Use i64 to store significand in 64-bit architecture + if (!use64bit) + i64 = i; + + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (i64 > RAPIDJSON_UINT64_C2(0x1FFFFF, 0xFFFFFFFF)) // 2^53 - 1 for fast path + break; + else { + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + --expFrac; + if (i64 != 0) + significandDigit++; + } + } + + d = static_cast(i64); +#else + // Use double to store significand in 32-bit architecture + d = static_cast(use64bit ? i64 : i); +#endif + useDouble = true; + } + + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (significandDigit < 17) { + d = d * 10.0 + (s.TakePush() - '0'); + --expFrac; + if (RAPIDJSON_LIKELY(d > 0.0)) + significandDigit++; + } + else + s.TakePush(); + } + } + else + decimalPosition = s.Length(); // decimal position at the end of integer. + + // Parse exp = e [ minus / plus ] 1*DIGIT + int exp = 0; + if (Consume(s, 'e') || Consume(s, 'E')) { + if (!useDouble) { + d = static_cast(use64bit ? i64 : i); + useDouble = true; + } + + bool expMinus = false; + if (Consume(s, '+')) + ; + else if (Consume(s, '-')) + expMinus = true; + + if (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = static_cast(s.Take() - '0'); + if (expMinus) { + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = exp * 10 + static_cast(s.Take() - '0'); + if (exp >= 214748364) { // Issue #313: prevent overflow exponent + while (RAPIDJSON_UNLIKELY(s.Peek() >= '0' && s.Peek() <= '9')) // Consume the rest of exponent + s.Take(); + } + } + } + else { // positive exp + int maxExp = 308 - expFrac; + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = exp * 10 + static_cast(s.Take() - '0'); + if (RAPIDJSON_UNLIKELY(exp > maxExp)) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, startOffset); + } + } + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorNumberMissExponent, s.Tell()); + + if (expMinus) + exp = -exp; + } + + // Finish parsing, call event according to the type of number. + bool cont = true; + + if (parseFlags & kParseNumbersAsStringsFlag) { + if (parseFlags & kParseInsituFlag) { + s.Pop(); // Pop stack no matter if it will be used or not. + typename InputStream::Ch* head = is.PutBegin(); + const size_t length = s.Tell() - startOffset; + RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); + // unable to insert the \0 character here, it will erase the comma after this number + const typename TargetEncoding::Ch* const str = reinterpret_cast(head); + cont = handler.RawNumber(str, SizeType(length), false); + } + else { + SizeType numCharsToCopy = static_cast(s.Length()); + StringStream srcStream(s.Pop()); + StackStream dstStream(stack_); + while (numCharsToCopy--) { + Transcoder, TargetEncoding>::Transcode(srcStream, dstStream); + } + dstStream.Put('\0'); + const typename TargetEncoding::Ch* str = dstStream.Pop(); + const SizeType length = static_cast(dstStream.Length()) - 1; + cont = handler.RawNumber(str, SizeType(length), true); + } + } + else { + size_t length = s.Length(); + const char* decimal = s.Pop(); // Pop stack no matter if it will be used or not. + + if (useDouble) { + int p = exp + expFrac; + if (parseFlags & kParseFullPrecisionFlag) + d = internal::StrtodFullPrecision(d, p, decimal, length, decimalPosition, exp); + else + d = internal::StrtodNormalPrecision(d, p); + + cont = handler.Double(minus ? -d : d); + } + else if (useNanOrInf) { + cont = handler.Double(d); + } + else { + if (use64bit) { + if (minus) + cont = handler.Int64(static_cast(~i64 + 1)); + else + cont = handler.Uint64(i64); + } + else { + if (minus) + cont = handler.Int(static_cast(~i + 1)); + else + cont = handler.Uint(i); + } + } + } + if (RAPIDJSON_UNLIKELY(!cont)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, startOffset); + } + + // Parse any JSON value + template + void ParseValue(InputStream& is, Handler& handler) { + switch (is.Peek()) { + case 'n': ParseNull (is, handler); break; + case 't': ParseTrue (is, handler); break; + case 'f': ParseFalse (is, handler); break; + case '"': ParseString(is, handler); break; + case '{': ParseObject(is, handler); break; + case '[': ParseArray (is, handler); break; + default : + ParseNumber(is, handler); + break; + + } + } + + // Iterative Parsing + + // States + enum IterativeParsingState { + IterativeParsingStartState = 0, + IterativeParsingFinishState, + IterativeParsingErrorState, + + // Object states + IterativeParsingObjectInitialState, + IterativeParsingMemberKeyState, + IterativeParsingKeyValueDelimiterState, + IterativeParsingMemberValueState, + IterativeParsingMemberDelimiterState, + IterativeParsingObjectFinishState, + + // Array states + IterativeParsingArrayInitialState, + IterativeParsingElementState, + IterativeParsingElementDelimiterState, + IterativeParsingArrayFinishState, + + // Single value state + IterativeParsingValueState + }; + + enum { cIterativeParsingStateCount = IterativeParsingValueState + 1 }; + + // Tokens + enum Token { + LeftBracketToken = 0, + RightBracketToken, + + LeftCurlyBracketToken, + RightCurlyBracketToken, + + CommaToken, + ColonToken, + + StringToken, + FalseToken, + TrueToken, + NullToken, + NumberToken, + + kTokenCount + }; + + RAPIDJSON_FORCEINLINE Token Tokenize(Ch c) { + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define N NumberToken +#define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N + // Maps from ASCII to Token + static const unsigned char tokenMap[256] = { + N16, // 00~0F + N16, // 10~1F + N, N, StringToken, N, N, N, N, N, N, N, N, N, CommaToken, N, N, N, // 20~2F + N, N, N, N, N, N, N, N, N, N, ColonToken, N, N, N, N, N, // 30~3F + N16, // 40~4F + N, N, N, N, N, N, N, N, N, N, N, LeftBracketToken, N, RightBracketToken, N, N, // 50~5F + N, N, N, N, N, N, FalseToken, N, N, N, N, N, N, N, NullToken, N, // 60~6F + N, N, N, N, TrueToken, N, N, N, N, N, N, LeftCurlyBracketToken, N, RightCurlyBracketToken, N, N, // 70~7F + N16, N16, N16, N16, N16, N16, N16, N16 // 80~FF + }; +#undef N +#undef N16 +//!@endcond + + if (sizeof(Ch) == 1 || static_cast(c) < 256) + return static_cast(tokenMap[static_cast(c)]); + else + return NumberToken; + } + + RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token) { + // current state x one lookahead token -> new state + static const char G[cIterativeParsingStateCount][kTokenCount] = { + // Start + { + IterativeParsingArrayInitialState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingValueState, // String + IterativeParsingValueState, // False + IterativeParsingValueState, // True + IterativeParsingValueState, // Null + IterativeParsingValueState // Number + }, + // Finish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Error(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // ObjectInitial + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // MemberKey + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingKeyValueDelimiterState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // KeyValueDelimiter + { + IterativeParsingArrayInitialState, // Left bracket(push MemberValue state) + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberValueState, // String + IterativeParsingMemberValueState, // False + IterativeParsingMemberValueState, // True + IterativeParsingMemberValueState, // Null + IterativeParsingMemberValueState // Number + }, + // MemberValue + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingMemberDelimiterState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // MemberDelimiter + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // ObjectFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // ArrayInitial + { + IterativeParsingArrayInitialState, // Left bracket(push Element state) + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push Element state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingElementState, // String + IterativeParsingElementState, // False + IterativeParsingElementState, // True + IterativeParsingElementState, // Null + IterativeParsingElementState // Number + }, + // Element + { + IterativeParsingErrorState, // Left bracket + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingElementDelimiterState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // ElementDelimiter + { + IterativeParsingArrayInitialState, // Left bracket(push Element state) + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push Element state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingElementState, // String + IterativeParsingElementState, // False + IterativeParsingElementState, // True + IterativeParsingElementState, // Null + IterativeParsingElementState // Number + }, + // ArrayFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Single Value (sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + } + }; // End of G + + return static_cast(G[state][token]); + } + + // Make an advance in the token stream and state based on the candidate destination state which was returned by Transit(). + // May return a new state on state pop. + template + RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream& is, Handler& handler) { + (void)token; + + switch (dst) { + case IterativeParsingErrorState: + return dst; + + case IterativeParsingObjectInitialState: + case IterativeParsingArrayInitialState: + { + // Push the state(Element or MemeberValue) if we are nested in another array or value of member. + // In this way we can get the correct state on ObjectFinish or ArrayFinish by frame pop. + IterativeParsingState n = src; + if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState) + n = IterativeParsingElementState; + else if (src == IterativeParsingKeyValueDelimiterState) + n = IterativeParsingMemberValueState; + // Push current state. + *stack_.template Push(1) = n; + // Initialize and push the member/element count. + *stack_.template Push(1) = 0; + // Call handler + bool hr = (dst == IterativeParsingObjectInitialState) ? handler.StartObject() : handler.StartArray(); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return dst; + } + } + + case IterativeParsingMemberKeyState: + ParseString(is, handler, true); + if (HasParseError()) + return IterativeParsingErrorState; + else + return dst; + + case IterativeParsingKeyValueDelimiterState: + RAPIDJSON_ASSERT(token == ColonToken); + is.Take(); + return dst; + + case IterativeParsingMemberValueState: + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return dst; + + case IterativeParsingElementState: + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return dst; + + case IterativeParsingMemberDelimiterState: + case IterativeParsingElementDelimiterState: + is.Take(); + // Update member/element count. + *stack_.template Top() = *stack_.template Top() + 1; + return dst; + + case IterativeParsingObjectFinishState: + { + // Transit from delimiter is only allowed when trailing commas are enabled + if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingMemberDelimiterState) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorObjectMissName, is.Tell()); + return IterativeParsingErrorState; + } + // Get member count. + SizeType c = *stack_.template Pop(1); + // If the object is not empty, count the last member. + if (src == IterativeParsingMemberValueState) + ++c; + // Restore the state. + IterativeParsingState n = static_cast(*stack_.template Pop(1)); + // Transit to Finish state if this is the topmost scope. + if (n == IterativeParsingStartState) + n = IterativeParsingFinishState; + // Call handler + bool hr = handler.EndObject(c); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return n; + } + } + + case IterativeParsingArrayFinishState: + { + // Transit from delimiter is only allowed when trailing commas are enabled + if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingElementDelimiterState) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorValueInvalid, is.Tell()); + return IterativeParsingErrorState; + } + // Get element count. + SizeType c = *stack_.template Pop(1); + // If the array is not empty, count the last element. + if (src == IterativeParsingElementState) + ++c; + // Restore the state. + IterativeParsingState n = static_cast(*stack_.template Pop(1)); + // Transit to Finish state if this is the topmost scope. + if (n == IterativeParsingStartState) + n = IterativeParsingFinishState; + // Call handler + bool hr = handler.EndArray(c); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return n; + } + } + + default: + // This branch is for IterativeParsingValueState actually. + // Use `default:` rather than + // `case IterativeParsingValueState:` is for code coverage. + + // The IterativeParsingStartState is not enumerated in this switch-case. + // It is impossible for that case. And it can be caught by following assertion. + + // The IterativeParsingFinishState is not enumerated in this switch-case either. + // It is a "derivative" state which cannot triggered from Predict() directly. + // Therefore it cannot happen here. And it can be caught by following assertion. + RAPIDJSON_ASSERT(dst == IterativeParsingValueState); + + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return IterativeParsingFinishState; + } + } + + template + void HandleError(IterativeParsingState src, InputStream& is) { + if (HasParseError()) { + // Error flag has been set. + return; + } + + switch (src) { + case IterativeParsingStartState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentEmpty, is.Tell()); return; + case IterativeParsingFinishState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentRootNotSingular, is.Tell()); return; + case IterativeParsingObjectInitialState: + case IterativeParsingMemberDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); return; + case IterativeParsingMemberKeyState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); return; + case IterativeParsingMemberValueState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); return; + case IterativeParsingKeyValueDelimiterState: + case IterativeParsingArrayInitialState: + case IterativeParsingElementDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); return; + default: RAPIDJSON_ASSERT(src == IterativeParsingElementState); RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); return; + } + } + + template + ParseResult IterativeParse(InputStream& is, Handler& handler) { + parseResult_.Clear(); + ClearStackOnExit scope(*this); + IterativeParsingState state = IterativeParsingStartState; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + while (is.Peek() != '\0') { + Token t = Tokenize(is.Peek()); + IterativeParsingState n = Predict(state, t); + IterativeParsingState d = Transit(state, t, n, is, handler); + + if (d == IterativeParsingErrorState) { + HandleError(state, is); + break; + } + + state = d; + + // Do not further consume streams if a root JSON has been parsed. + if ((parseFlags & kParseStopWhenDoneFlag) && state == IterativeParsingFinishState) + break; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + + // Handle the end of file. + if (state != IterativeParsingFinishState) + HandleError(state, is); + + return parseResult_; + } + + static const size_t kDefaultStackCapacity = 256; //!< Default stack capacity in bytes for storing a single decoded string. + internal::Stack stack_; //!< A stack for storing decoded string temporarily during non-destructive parsing. + ParseResult parseResult_; +}; // class GenericReader + +//! Reader with UTF8 encoding and default allocator. +typedef GenericReader, UTF8<> > Reader; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_READER_H_ diff --git a/src/3rdparty/rapidjson/schema.h b/src/3rdparty/rapidjson/schema.h new file mode 100644 index 00000000..b182aa27 --- /dev/null +++ b/src/3rdparty/rapidjson/schema.h @@ -0,0 +1,2006 @@ +// Tencent is pleased to support the open source community by making RapidJSON available-> +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip-> All rights reserved-> +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License-> You may obtain a copy of the License at +// +// http://opensource->org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied-> See the License for the +// specific language governing permissions and limitations under the License-> + +#ifndef RAPIDJSON_SCHEMA_H_ +#define RAPIDJSON_SCHEMA_H_ + +#include "document.h" +#include "pointer.h" +#include // abs, floor + +#if !defined(RAPIDJSON_SCHEMA_USE_INTERNALREGEX) +#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 1 +#else +#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 0 +#endif + +#if !RAPIDJSON_SCHEMA_USE_INTERNALREGEX && !defined(RAPIDJSON_SCHEMA_USE_STDREGEX) && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)) +#define RAPIDJSON_SCHEMA_USE_STDREGEX 1 +#else +#define RAPIDJSON_SCHEMA_USE_STDREGEX 0 +#endif + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX +#include "internal/regex.h" +#elif RAPIDJSON_SCHEMA_USE_STDREGEX +#include +#endif + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX || RAPIDJSON_SCHEMA_USE_STDREGEX +#define RAPIDJSON_SCHEMA_HAS_REGEX 1 +#else +#define RAPIDJSON_SCHEMA_HAS_REGEX 0 +#endif + +#ifndef RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_SCHEMA_VERBOSE 0 +#endif + +#if RAPIDJSON_SCHEMA_VERBOSE +#include "stringbuffer.h" +#endif + +RAPIDJSON_DIAG_PUSH + +#if defined(__GNUC__) +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_OFF(weak-vtables) +RAPIDJSON_DIAG_OFF(exit-time-destructors) +RAPIDJSON_DIAG_OFF(c++98-compat-pedantic) +RAPIDJSON_DIAG_OFF(variadic-macros) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Verbose Utilities + +#if RAPIDJSON_SCHEMA_VERBOSE + +namespace internal { + +inline void PrintInvalidKeyword(const char* keyword) { + printf("Fail keyword: %s\n", keyword); +} + +inline void PrintInvalidKeyword(const wchar_t* keyword) { + wprintf(L"Fail keyword: %ls\n", keyword); +} + +inline void PrintInvalidDocument(const char* document) { + printf("Fail document: %s\n\n", document); +} + +inline void PrintInvalidDocument(const wchar_t* document) { + wprintf(L"Fail document: %ls\n\n", document); +} + +inline void PrintValidatorPointers(unsigned depth, const char* s, const char* d) { + printf("S: %*s%s\nD: %*s%s\n\n", depth * 4, " ", s, depth * 4, " ", d); +} + +inline void PrintValidatorPointers(unsigned depth, const wchar_t* s, const wchar_t* d) { + wprintf(L"S: %*ls%ls\nD: %*ls%ls\n\n", depth * 4, L" ", s, depth * 4, L" ", d); +} + +} // namespace internal + +#endif // RAPIDJSON_SCHEMA_VERBOSE + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_INVALID_KEYWORD_RETURN + +#if RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword) internal::PrintInvalidKeyword(keyword) +#else +#define RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword) +#endif + +#define RAPIDJSON_INVALID_KEYWORD_RETURN(keyword)\ +RAPIDJSON_MULTILINEMACRO_BEGIN\ + context.invalidKeyword = keyword.GetString();\ + RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword.GetString());\ + return false;\ +RAPIDJSON_MULTILINEMACRO_END + +/////////////////////////////////////////////////////////////////////////////// +// Forward declarations + +template +class GenericSchemaDocument; + +namespace internal { + +template +class Schema; + +/////////////////////////////////////////////////////////////////////////////// +// ISchemaValidator + +class ISchemaValidator { +public: + virtual ~ISchemaValidator() {} + virtual bool IsValid() const = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// ISchemaStateFactory + +template +class ISchemaStateFactory { +public: + virtual ~ISchemaStateFactory() {} + virtual ISchemaValidator* CreateSchemaValidator(const SchemaType&) = 0; + virtual void DestroySchemaValidator(ISchemaValidator* validator) = 0; + virtual void* CreateHasher() = 0; + virtual uint64_t GetHashCode(void* hasher) = 0; + virtual void DestroryHasher(void* hasher) = 0; + virtual void* MallocState(size_t size) = 0; + virtual void FreeState(void* p) = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Hasher + +// For comparison of compound value +template +class Hasher { +public: + typedef typename Encoding::Ch Ch; + + Hasher(Allocator* allocator = 0, size_t stackCapacity = kDefaultSize) : stack_(allocator, stackCapacity) {} + + bool Null() { return WriteType(kNullType); } + bool Bool(bool b) { return WriteType(b ? kTrueType : kFalseType); } + bool Int(int i) { Number n; n.u.i = i; n.d = static_cast(i); return WriteNumber(n); } + bool Uint(unsigned u) { Number n; n.u.u = u; n.d = static_cast(u); return WriteNumber(n); } + bool Int64(int64_t i) { Number n; n.u.i = i; n.d = static_cast(i); return WriteNumber(n); } + bool Uint64(uint64_t u) { Number n; n.u.u = u; n.d = static_cast(u); return WriteNumber(n); } + bool Double(double d) { + Number n; + if (d < 0) n.u.i = static_cast(d); + else n.u.u = static_cast(d); + n.d = d; + return WriteNumber(n); + } + + bool RawNumber(const Ch* str, SizeType len, bool) { + WriteBuffer(kNumberType, str, len * sizeof(Ch)); + return true; + } + + bool String(const Ch* str, SizeType len, bool) { + WriteBuffer(kStringType, str, len * sizeof(Ch)); + return true; + } + + bool StartObject() { return true; } + bool Key(const Ch* str, SizeType len, bool copy) { return String(str, len, copy); } + bool EndObject(SizeType memberCount) { + uint64_t h = Hash(0, kObjectType); + uint64_t* kv = stack_.template Pop(memberCount * 2); + for (SizeType i = 0; i < memberCount; i++) + h ^= Hash(kv[i * 2], kv[i * 2 + 1]); // Use xor to achieve member order insensitive + *stack_.template Push() = h; + return true; + } + + bool StartArray() { return true; } + bool EndArray(SizeType elementCount) { + uint64_t h = Hash(0, kArrayType); + uint64_t* e = stack_.template Pop(elementCount); + for (SizeType i = 0; i < elementCount; i++) + h = Hash(h, e[i]); // Use hash to achieve element order sensitive + *stack_.template Push() = h; + return true; + } + + bool IsValid() const { return stack_.GetSize() == sizeof(uint64_t); } + + uint64_t GetHashCode() const { + RAPIDJSON_ASSERT(IsValid()); + return *stack_.template Top(); + } + +private: + static const size_t kDefaultSize = 256; + struct Number { + union U { + uint64_t u; + int64_t i; + }u; + double d; + }; + + bool WriteType(Type type) { return WriteBuffer(type, 0, 0); } + + bool WriteNumber(const Number& n) { return WriteBuffer(kNumberType, &n, sizeof(n)); } + + bool WriteBuffer(Type type, const void* data, size_t len) { + // FNV-1a from http://isthe.com/chongo/tech/comp/fnv/ + uint64_t h = Hash(RAPIDJSON_UINT64_C2(0x84222325, 0xcbf29ce4), type); + const unsigned char* d = static_cast(data); + for (size_t i = 0; i < len; i++) + h = Hash(h, d[i]); + *stack_.template Push() = h; + return true; + } + + static uint64_t Hash(uint64_t h, uint64_t d) { + static const uint64_t kPrime = RAPIDJSON_UINT64_C2(0x00000100, 0x000001b3); + h ^= d; + h *= kPrime; + return h; + } + + Stack stack_; +}; + +/////////////////////////////////////////////////////////////////////////////// +// SchemaValidationContext + +template +struct SchemaValidationContext { + typedef Schema SchemaType; + typedef ISchemaStateFactory SchemaValidatorFactoryType; + typedef typename SchemaType::ValueType ValueType; + typedef typename ValueType::Ch Ch; + + enum PatternValidatorType { + kPatternValidatorOnly, + kPatternValidatorWithProperty, + kPatternValidatorWithAdditionalProperty + }; + + SchemaValidationContext(SchemaValidatorFactoryType& f, const SchemaType* s) : + factory(f), + schema(s), + valueSchema(), + invalidKeyword(), + hasher(), + arrayElementHashCodes(), + validators(), + validatorCount(), + patternPropertiesValidators(), + patternPropertiesValidatorCount(), + patternPropertiesSchemas(), + patternPropertiesSchemaCount(), + valuePatternValidatorType(kPatternValidatorOnly), + propertyExist(), + inArray(false), + valueUniqueness(false), + arrayUniqueness(false) + { + } + + ~SchemaValidationContext() { + if (hasher) + factory.DestroryHasher(hasher); + if (validators) { + for (SizeType i = 0; i < validatorCount; i++) + factory.DestroySchemaValidator(validators[i]); + factory.FreeState(validators); + } + if (patternPropertiesValidators) { + for (SizeType i = 0; i < patternPropertiesValidatorCount; i++) + factory.DestroySchemaValidator(patternPropertiesValidators[i]); + factory.FreeState(patternPropertiesValidators); + } + if (patternPropertiesSchemas) + factory.FreeState(patternPropertiesSchemas); + if (propertyExist) + factory.FreeState(propertyExist); + } + + SchemaValidatorFactoryType& factory; + const SchemaType* schema; + const SchemaType* valueSchema; + const Ch* invalidKeyword; + void* hasher; // Only validator access + void* arrayElementHashCodes; // Only validator access this + ISchemaValidator** validators; + SizeType validatorCount; + ISchemaValidator** patternPropertiesValidators; + SizeType patternPropertiesValidatorCount; + const SchemaType** patternPropertiesSchemas; + SizeType patternPropertiesSchemaCount; + PatternValidatorType valuePatternValidatorType; + PatternValidatorType objectPatternValidatorType; + SizeType arrayElementIndex; + bool* propertyExist; + bool inArray; + bool valueUniqueness; + bool arrayUniqueness; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Schema + +template +class Schema { +public: + typedef typename SchemaDocumentType::ValueType ValueType; + typedef typename SchemaDocumentType::AllocatorType AllocatorType; + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + typedef SchemaValidationContext Context; + typedef Schema SchemaType; + typedef GenericValue SValue; + friend class GenericSchemaDocument; + + Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) : + allocator_(allocator), + enum_(), + enumCount_(), + not_(), + type_((1 << kTotalSchemaType) - 1), // typeless + validatorCount_(), + properties_(), + additionalPropertiesSchema_(), + patternProperties_(), + patternPropertyCount_(), + propertyCount_(), + minProperties_(), + maxProperties_(SizeType(~0)), + additionalProperties_(true), + hasDependencies_(), + hasRequired_(), + hasSchemaDependencies_(), + additionalItemsSchema_(), + itemsList_(), + itemsTuple_(), + itemsTupleCount_(), + minItems_(), + maxItems_(SizeType(~0)), + additionalItems_(true), + uniqueItems_(false), + pattern_(), + minLength_(0), + maxLength_(~SizeType(0)), + exclusiveMinimum_(false), + exclusiveMaximum_(false) + { + typedef typename SchemaDocumentType::ValueType ValueType; + typedef typename ValueType::ConstValueIterator ConstValueIterator; + typedef typename ValueType::ConstMemberIterator ConstMemberIterator; + + if (!value.IsObject()) + return; + + if (const ValueType* v = GetMember(value, GetTypeString())) { + type_ = 0; + if (v->IsString()) + AddType(*v); + else if (v->IsArray()) + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr) + AddType(*itr); + } + + if (const ValueType* v = GetMember(value, GetEnumString())) + if (v->IsArray() && v->Size() > 0) { + enum_ = static_cast(allocator_->Malloc(sizeof(uint64_t) * v->Size())); + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr) { + typedef Hasher > EnumHasherType; + char buffer[256 + 24]; + MemoryPoolAllocator<> hasherAllocator(buffer, sizeof(buffer)); + EnumHasherType h(&hasherAllocator, 256); + itr->Accept(h); + enum_[enumCount_++] = h.GetHashCode(); + } + } + + if (schemaDocument) { + AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document); + AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document); + AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document); + } + + if (const ValueType* v = GetMember(value, GetNotString())) { + schemaDocument->CreateSchema(¬_, p.Append(GetNotString(), allocator_), *v, document); + notValidatorIndex_ = validatorCount_; + validatorCount_++; + } + + // Object + + const ValueType* properties = GetMember(value, GetPropertiesString()); + const ValueType* required = GetMember(value, GetRequiredString()); + const ValueType* dependencies = GetMember(value, GetDependenciesString()); + { + // Gather properties from properties/required/dependencies + SValue allProperties(kArrayType); + + if (properties && properties->IsObject()) + for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) + AddUniqueElement(allProperties, itr->name); + + if (required && required->IsArray()) + for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr) + if (itr->IsString()) + AddUniqueElement(allProperties, *itr); + + if (dependencies && dependencies->IsObject()) + for (ConstMemberIterator itr = dependencies->MemberBegin(); itr != dependencies->MemberEnd(); ++itr) { + AddUniqueElement(allProperties, itr->name); + if (itr->value.IsArray()) + for (ConstValueIterator i = itr->value.Begin(); i != itr->value.End(); ++i) + if (i->IsString()) + AddUniqueElement(allProperties, *i); + } + + if (allProperties.Size() > 0) { + propertyCount_ = allProperties.Size(); + properties_ = static_cast(allocator_->Malloc(sizeof(Property) * propertyCount_)); + for (SizeType i = 0; i < propertyCount_; i++) { + new (&properties_[i]) Property(); + properties_[i].name = allProperties[i]; + properties_[i].schema = GetTypeless(); + } + } + } + + if (properties && properties->IsObject()) { + PointerType q = p.Append(GetPropertiesString(), allocator_); + for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) { + SizeType index; + if (FindPropertyIndex(itr->name, &index)) + schemaDocument->CreateSchema(&properties_[index].schema, q.Append(itr->name, allocator_), itr->value, document); + } + } + + if (const ValueType* v = GetMember(value, GetPatternPropertiesString())) { + PointerType q = p.Append(GetPatternPropertiesString(), allocator_); + patternProperties_ = static_cast(allocator_->Malloc(sizeof(PatternProperty) * v->MemberCount())); + patternPropertyCount_ = 0; + + for (ConstMemberIterator itr = v->MemberBegin(); itr != v->MemberEnd(); ++itr) { + new (&patternProperties_[patternPropertyCount_]) PatternProperty(); + patternProperties_[patternPropertyCount_].pattern = CreatePattern(itr->name); + schemaDocument->CreateSchema(&patternProperties_[patternPropertyCount_].schema, q.Append(itr->name, allocator_), itr->value, document); + patternPropertyCount_++; + } + } + + if (required && required->IsArray()) + for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr) + if (itr->IsString()) { + SizeType index; + if (FindPropertyIndex(*itr, &index)) { + properties_[index].required = true; + hasRequired_ = true; + } + } + + if (dependencies && dependencies->IsObject()) { + PointerType q = p.Append(GetDependenciesString(), allocator_); + hasDependencies_ = true; + for (ConstMemberIterator itr = dependencies->MemberBegin(); itr != dependencies->MemberEnd(); ++itr) { + SizeType sourceIndex; + if (FindPropertyIndex(itr->name, &sourceIndex)) { + if (itr->value.IsArray()) { + properties_[sourceIndex].dependencies = static_cast(allocator_->Malloc(sizeof(bool) * propertyCount_)); + std::memset(properties_[sourceIndex].dependencies, 0, sizeof(bool)* propertyCount_); + for (ConstValueIterator targetItr = itr->value.Begin(); targetItr != itr->value.End(); ++targetItr) { + SizeType targetIndex; + if (FindPropertyIndex(*targetItr, &targetIndex)) + properties_[sourceIndex].dependencies[targetIndex] = true; + } + } + else if (itr->value.IsObject()) { + hasSchemaDependencies_ = true; + schemaDocument->CreateSchema(&properties_[sourceIndex].dependenciesSchema, q.Append(itr->name, allocator_), itr->value, document); + properties_[sourceIndex].dependenciesValidatorIndex = validatorCount_; + validatorCount_++; + } + } + } + } + + if (const ValueType* v = GetMember(value, GetAdditionalPropertiesString())) { + if (v->IsBool()) + additionalProperties_ = v->GetBool(); + else if (v->IsObject()) + schemaDocument->CreateSchema(&additionalPropertiesSchema_, p.Append(GetAdditionalPropertiesString(), allocator_), *v, document); + } + + AssignIfExist(minProperties_, value, GetMinPropertiesString()); + AssignIfExist(maxProperties_, value, GetMaxPropertiesString()); + + // Array + if (const ValueType* v = GetMember(value, GetItemsString())) { + PointerType q = p.Append(GetItemsString(), allocator_); + if (v->IsObject()) // List validation + schemaDocument->CreateSchema(&itemsList_, q, *v, document); + else if (v->IsArray()) { // Tuple validation + itemsTuple_ = static_cast(allocator_->Malloc(sizeof(const Schema*) * v->Size())); + SizeType index = 0; + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr, index++) + schemaDocument->CreateSchema(&itemsTuple_[itemsTupleCount_++], q.Append(index, allocator_), *itr, document); + } + } + + AssignIfExist(minItems_, value, GetMinItemsString()); + AssignIfExist(maxItems_, value, GetMaxItemsString()); + + if (const ValueType* v = GetMember(value, GetAdditionalItemsString())) { + if (v->IsBool()) + additionalItems_ = v->GetBool(); + else if (v->IsObject()) + schemaDocument->CreateSchema(&additionalItemsSchema_, p.Append(GetAdditionalItemsString(), allocator_), *v, document); + } + + AssignIfExist(uniqueItems_, value, GetUniqueItemsString()); + + // String + AssignIfExist(minLength_, value, GetMinLengthString()); + AssignIfExist(maxLength_, value, GetMaxLengthString()); + + if (const ValueType* v = GetMember(value, GetPatternString())) + pattern_ = CreatePattern(*v); + + // Number + if (const ValueType* v = GetMember(value, GetMinimumString())) + if (v->IsNumber()) + minimum_.CopyFrom(*v, *allocator_); + + if (const ValueType* v = GetMember(value, GetMaximumString())) + if (v->IsNumber()) + maximum_.CopyFrom(*v, *allocator_); + + AssignIfExist(exclusiveMinimum_, value, GetExclusiveMinimumString()); + AssignIfExist(exclusiveMaximum_, value, GetExclusiveMaximumString()); + + if (const ValueType* v = GetMember(value, GetMultipleOfString())) + if (v->IsNumber() && v->GetDouble() > 0.0) + multipleOf_.CopyFrom(*v, *allocator_); + } + + ~Schema() { + if (allocator_) { + allocator_->Free(enum_); + } + if (properties_) { + for (SizeType i = 0; i < propertyCount_; i++) + properties_[i].~Property(); + AllocatorType::Free(properties_); + } + if (patternProperties_) { + for (SizeType i = 0; i < patternPropertyCount_; i++) + patternProperties_[i].~PatternProperty(); + AllocatorType::Free(patternProperties_); + } + AllocatorType::Free(itemsTuple_); +#if RAPIDJSON_SCHEMA_HAS_REGEX + if (pattern_) { + pattern_->~RegexType(); + allocator_->Free(pattern_); + } +#endif + } + + bool BeginValue(Context& context) const { + if (context.inArray) { + if (uniqueItems_) + context.valueUniqueness = true; + + if (itemsList_) + context.valueSchema = itemsList_; + else if (itemsTuple_) { + if (context.arrayElementIndex < itemsTupleCount_) + context.valueSchema = itemsTuple_[context.arrayElementIndex]; + else if (additionalItemsSchema_) + context.valueSchema = additionalItemsSchema_; + else if (additionalItems_) + context.valueSchema = GetTypeless(); + else + RAPIDJSON_INVALID_KEYWORD_RETURN(GetItemsString()); + } + else + context.valueSchema = GetTypeless(); + + context.arrayElementIndex++; + } + return true; + } + + RAPIDJSON_FORCEINLINE bool EndValue(Context& context) const { + if (context.patternPropertiesValidatorCount > 0) { + bool otherValid = false; + SizeType count = context.patternPropertiesValidatorCount; + if (context.objectPatternValidatorType != Context::kPatternValidatorOnly) + otherValid = context.patternPropertiesValidators[--count]->IsValid(); + + bool patternValid = true; + for (SizeType i = 0; i < count; i++) + if (!context.patternPropertiesValidators[i]->IsValid()) { + patternValid = false; + break; + } + + if (context.objectPatternValidatorType == Context::kPatternValidatorOnly) { + if (!patternValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString()); + } + else if (context.objectPatternValidatorType == Context::kPatternValidatorWithProperty) { + if (!patternValid || !otherValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString()); + } + else if (!patternValid && !otherValid) // kPatternValidatorWithAdditionalProperty) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString()); + } + + if (enum_) { + const uint64_t h = context.factory.GetHashCode(context.hasher); + for (SizeType i = 0; i < enumCount_; i++) + if (enum_[i] == h) + goto foundEnum; + RAPIDJSON_INVALID_KEYWORD_RETURN(GetEnumString()); + foundEnum:; + } + + if (allOf_.schemas) + for (SizeType i = allOf_.begin; i < allOf_.begin + allOf_.count; i++) + if (!context.validators[i]->IsValid()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetAllOfString()); + + if (anyOf_.schemas) { + for (SizeType i = anyOf_.begin; i < anyOf_.begin + anyOf_.count; i++) + if (context.validators[i]->IsValid()) + goto foundAny; + RAPIDJSON_INVALID_KEYWORD_RETURN(GetAnyOfString()); + foundAny:; + } + + if (oneOf_.schemas) { + bool oneValid = false; + for (SizeType i = oneOf_.begin; i < oneOf_.begin + oneOf_.count; i++) + if (context.validators[i]->IsValid()) { + if (oneValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetOneOfString()); + else + oneValid = true; + } + if (!oneValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetOneOfString()); + } + + if (not_ && context.validators[notValidatorIndex_]->IsValid()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetNotString()); + + return true; + } + + bool Null(Context& context) const { + if (!(type_ & (1 << kNullSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + return CreateParallelValidator(context); + } + + bool Bool(Context& context, bool) const { + if (!(type_ & (1 << kBooleanSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + return CreateParallelValidator(context); + } + + bool Int(Context& context, int i) const { + if (!CheckInt(context, i)) + return false; + return CreateParallelValidator(context); + } + + bool Uint(Context& context, unsigned u) const { + if (!CheckUint(context, u)) + return false; + return CreateParallelValidator(context); + } + + bool Int64(Context& context, int64_t i) const { + if (!CheckInt(context, i)) + return false; + return CreateParallelValidator(context); + } + + bool Uint64(Context& context, uint64_t u) const { + if (!CheckUint(context, u)) + return false; + return CreateParallelValidator(context); + } + + bool Double(Context& context, double d) const { + if (!(type_ & (1 << kNumberSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (!minimum_.IsNull() && !CheckDoubleMinimum(context, d)) + return false; + + if (!maximum_.IsNull() && !CheckDoubleMaximum(context, d)) + return false; + + if (!multipleOf_.IsNull() && !CheckDoubleMultipleOf(context, d)) + return false; + + return CreateParallelValidator(context); + } + + bool String(Context& context, const Ch* str, SizeType length, bool) const { + if (!(type_ & (1 << kStringSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (minLength_ != 0 || maxLength_ != SizeType(~0)) { + SizeType count; + if (internal::CountStringCodePoint(str, length, &count)) { + if (count < minLength_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinLengthString()); + if (count > maxLength_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxLengthString()); + } + } + + if (pattern_ && !IsPatternMatch(pattern_, str, length)) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternString()); + + return CreateParallelValidator(context); + } + + bool StartObject(Context& context) const { + if (!(type_ & (1 << kObjectSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (hasDependencies_ || hasRequired_) { + context.propertyExist = static_cast(context.factory.MallocState(sizeof(bool) * propertyCount_)); + std::memset(context.propertyExist, 0, sizeof(bool) * propertyCount_); + } + + if (patternProperties_) { // pre-allocate schema array + SizeType count = patternPropertyCount_ + 1; // extra for valuePatternValidatorType + context.patternPropertiesSchemas = static_cast(context.factory.MallocState(sizeof(const SchemaType*) * count)); + context.patternPropertiesSchemaCount = 0; + std::memset(context.patternPropertiesSchemas, 0, sizeof(SchemaType*) * count); + } + + return CreateParallelValidator(context); + } + + bool Key(Context& context, const Ch* str, SizeType len, bool) const { + if (patternProperties_) { + context.patternPropertiesSchemaCount = 0; + for (SizeType i = 0; i < patternPropertyCount_; i++) + if (patternProperties_[i].pattern && IsPatternMatch(patternProperties_[i].pattern, str, len)) + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = patternProperties_[i].schema; + } + + SizeType index; + if (FindPropertyIndex(ValueType(str, len).Move(), &index)) { + if (context.patternPropertiesSchemaCount > 0) { + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = properties_[index].schema; + context.valueSchema = GetTypeless(); + context.valuePatternValidatorType = Context::kPatternValidatorWithProperty; + } + else + context.valueSchema = properties_[index].schema; + + if (context.propertyExist) + context.propertyExist[index] = true; + + return true; + } + + if (additionalPropertiesSchema_) { + if (additionalPropertiesSchema_ && context.patternPropertiesSchemaCount > 0) { + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = additionalPropertiesSchema_; + context.valueSchema = GetTypeless(); + context.valuePatternValidatorType = Context::kPatternValidatorWithAdditionalProperty; + } + else + context.valueSchema = additionalPropertiesSchema_; + return true; + } + else if (additionalProperties_) { + context.valueSchema = GetTypeless(); + return true; + } + + if (context.patternPropertiesSchemaCount == 0) // patternProperties are not additional properties + RAPIDJSON_INVALID_KEYWORD_RETURN(GetAdditionalPropertiesString()); + + return true; + } + + bool EndObject(Context& context, SizeType memberCount) const { + if (hasRequired_) + for (SizeType index = 0; index < propertyCount_; index++) + if (properties_[index].required) + if (!context.propertyExist[index]) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString()); + + if (memberCount < minProperties_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinPropertiesString()); + + if (memberCount > maxProperties_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxPropertiesString()); + + if (hasDependencies_) { + for (SizeType sourceIndex = 0; sourceIndex < propertyCount_; sourceIndex++) + if (context.propertyExist[sourceIndex]) { + if (properties_[sourceIndex].dependencies) { + for (SizeType targetIndex = 0; targetIndex < propertyCount_; targetIndex++) + if (properties_[sourceIndex].dependencies[targetIndex] && !context.propertyExist[targetIndex]) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetDependenciesString()); + } + else if (properties_[sourceIndex].dependenciesSchema) + if (!context.validators[properties_[sourceIndex].dependenciesValidatorIndex]->IsValid()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetDependenciesString()); + } + } + + return true; + } + + bool StartArray(Context& context) const { + if (!(type_ & (1 << kArraySchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + context.arrayElementIndex = 0; + context.inArray = true; + + return CreateParallelValidator(context); + } + + bool EndArray(Context& context, SizeType elementCount) const { + context.inArray = false; + + if (elementCount < minItems_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinItemsString()); + + if (elementCount > maxItems_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxItemsString()); + + return true; + } + + // Generate functions for string literal according to Ch +#define RAPIDJSON_STRING_(name, ...) \ + static const ValueType& Get##name##String() {\ + static const Ch s[] = { __VA_ARGS__, '\0' };\ + static const ValueType v(s, sizeof(s) / sizeof(Ch) - 1);\ + return v;\ + } + + RAPIDJSON_STRING_(Null, 'n', 'u', 'l', 'l') + RAPIDJSON_STRING_(Boolean, 'b', 'o', 'o', 'l', 'e', 'a', 'n') + RAPIDJSON_STRING_(Object, 'o', 'b', 'j', 'e', 'c', 't') + RAPIDJSON_STRING_(Array, 'a', 'r', 'r', 'a', 'y') + RAPIDJSON_STRING_(String, 's', 't', 'r', 'i', 'n', 'g') + RAPIDJSON_STRING_(Number, 'n', 'u', 'm', 'b', 'e', 'r') + RAPIDJSON_STRING_(Integer, 'i', 'n', 't', 'e', 'g', 'e', 'r') + RAPIDJSON_STRING_(Type, 't', 'y', 'p', 'e') + RAPIDJSON_STRING_(Enum, 'e', 'n', 'u', 'm') + RAPIDJSON_STRING_(AllOf, 'a', 'l', 'l', 'O', 'f') + RAPIDJSON_STRING_(AnyOf, 'a', 'n', 'y', 'O', 'f') + RAPIDJSON_STRING_(OneOf, 'o', 'n', 'e', 'O', 'f') + RAPIDJSON_STRING_(Not, 'n', 'o', 't') + RAPIDJSON_STRING_(Properties, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(Required, 'r', 'e', 'q', 'u', 'i', 'r', 'e', 'd') + RAPIDJSON_STRING_(Dependencies, 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'i', 'e', 's') + RAPIDJSON_STRING_(PatternProperties, 'p', 'a', 't', 't', 'e', 'r', 'n', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(AdditionalProperties, 'a', 'd', 'd', 'i', 't', 'i', 'o', 'n', 'a', 'l', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(MinProperties, 'm', 'i', 'n', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(MaxProperties, 'm', 'a', 'x', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(Items, 'i', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MinItems, 'm', 'i', 'n', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MaxItems, 'm', 'a', 'x', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(AdditionalItems, 'a', 'd', 'd', 'i', 't', 'i', 'o', 'n', 'a', 'l', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(UniqueItems, 'u', 'n', 'i', 'q', 'u', 'e', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MinLength, 'm', 'i', 'n', 'L', 'e', 'n', 'g', 't', 'h') + RAPIDJSON_STRING_(MaxLength, 'm', 'a', 'x', 'L', 'e', 'n', 'g', 't', 'h') + RAPIDJSON_STRING_(Pattern, 'p', 'a', 't', 't', 'e', 'r', 'n') + RAPIDJSON_STRING_(Minimum, 'm', 'i', 'n', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(Maximum, 'm', 'a', 'x', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(ExclusiveMinimum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'i', 'n', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(ExclusiveMaximum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'a', 'x', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(MultipleOf, 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', 'O', 'f') + +#undef RAPIDJSON_STRING_ + +private: + enum SchemaValueType { + kNullSchemaType, + kBooleanSchemaType, + kObjectSchemaType, + kArraySchemaType, + kStringSchemaType, + kNumberSchemaType, + kIntegerSchemaType, + kTotalSchemaType + }; + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX + typedef internal::GenericRegex RegexType; +#elif RAPIDJSON_SCHEMA_USE_STDREGEX + typedef std::basic_regex RegexType; +#else + typedef char RegexType; +#endif + + struct SchemaArray { + SchemaArray() : schemas(), count() {} + ~SchemaArray() { AllocatorType::Free(schemas); } + const SchemaType** schemas; + SizeType begin; // begin index of context.validators + SizeType count; + }; + + static const SchemaType* GetTypeless() { + static SchemaType typeless(0, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0); + return &typeless; + } + + template + void AddUniqueElement(V1& a, const V2& v) { + for (typename V1::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) + if (*itr == v) + return; + V1 c(v, *allocator_); + a.PushBack(c, *allocator_); + } + + static const ValueType* GetMember(const ValueType& value, const ValueType& name) { + typename ValueType::ConstMemberIterator itr = value.FindMember(name); + return itr != value.MemberEnd() ? &(itr->value) : 0; + } + + static void AssignIfExist(bool& out, const ValueType& value, const ValueType& name) { + if (const ValueType* v = GetMember(value, name)) + if (v->IsBool()) + out = v->GetBool(); + } + + static void AssignIfExist(SizeType& out, const ValueType& value, const ValueType& name) { + if (const ValueType* v = GetMember(value, name)) + if (v->IsUint64() && v->GetUint64() <= SizeType(~0)) + out = static_cast(v->GetUint64()); + } + + void AssignIfExist(SchemaArray& out, SchemaDocumentType& schemaDocument, const PointerType& p, const ValueType& value, const ValueType& name, const ValueType& document) { + if (const ValueType* v = GetMember(value, name)) { + if (v->IsArray() && v->Size() > 0) { + PointerType q = p.Append(name, allocator_); + out.count = v->Size(); + out.schemas = static_cast(allocator_->Malloc(out.count * sizeof(const Schema*))); + memset(out.schemas, 0, sizeof(Schema*)* out.count); + for (SizeType i = 0; i < out.count; i++) + schemaDocument.CreateSchema(&out.schemas[i], q.Append(i, allocator_), (*v)[i], document); + out.begin = validatorCount_; + validatorCount_ += out.count; + } + } + } + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX + template + RegexType* CreatePattern(const ValueType& value) { + if (value.IsString()) { + RegexType* r = new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString()); + if (!r->IsValid()) { + r->~RegexType(); + AllocatorType::Free(r); + r = 0; + } + return r; + } + return 0; + } + + static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType) { + return pattern->Search(str); + } +#elif RAPIDJSON_SCHEMA_USE_STDREGEX + template + RegexType* CreatePattern(const ValueType& value) { + if (value.IsString()) + try { + return new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript); + } + catch (const std::regex_error&) { + } + return 0; + } + + static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType length) { + std::match_results r; + return std::regex_search(str, str + length, r, *pattern); + } +#else + template + RegexType* CreatePattern(const ValueType&) { return 0; } + + static bool IsPatternMatch(const RegexType*, const Ch *, SizeType) { return true; } +#endif // RAPIDJSON_SCHEMA_USE_STDREGEX + + void AddType(const ValueType& type) { + if (type == GetNullString() ) type_ |= 1 << kNullSchemaType; + else if (type == GetBooleanString()) type_ |= 1 << kBooleanSchemaType; + else if (type == GetObjectString() ) type_ |= 1 << kObjectSchemaType; + else if (type == GetArrayString() ) type_ |= 1 << kArraySchemaType; + else if (type == GetStringString() ) type_ |= 1 << kStringSchemaType; + else if (type == GetIntegerString()) type_ |= 1 << kIntegerSchemaType; + else if (type == GetNumberString() ) type_ |= (1 << kNumberSchemaType) | (1 << kIntegerSchemaType); + } + + bool CreateParallelValidator(Context& context) const { + if (enum_ || context.arrayUniqueness) + context.hasher = context.factory.CreateHasher(); + + if (validatorCount_) { + RAPIDJSON_ASSERT(context.validators == 0); + context.validators = static_cast(context.factory.MallocState(sizeof(ISchemaValidator*) * validatorCount_)); + context.validatorCount = validatorCount_; + + if (allOf_.schemas) + CreateSchemaValidators(context, allOf_); + + if (anyOf_.schemas) + CreateSchemaValidators(context, anyOf_); + + if (oneOf_.schemas) + CreateSchemaValidators(context, oneOf_); + + if (not_) + context.validators[notValidatorIndex_] = context.factory.CreateSchemaValidator(*not_); + + if (hasSchemaDependencies_) { + for (SizeType i = 0; i < propertyCount_; i++) + if (properties_[i].dependenciesSchema) + context.validators[properties_[i].dependenciesValidatorIndex] = context.factory.CreateSchemaValidator(*properties_[i].dependenciesSchema); + } + } + + return true; + } + + void CreateSchemaValidators(Context& context, const SchemaArray& schemas) const { + for (SizeType i = 0; i < schemas.count; i++) + context.validators[schemas.begin + i] = context.factory.CreateSchemaValidator(*schemas.schemas[i]); + } + + // O(n) + bool FindPropertyIndex(const ValueType& name, SizeType* outIndex) const { + SizeType len = name.GetStringLength(); + const Ch* str = name.GetString(); + for (SizeType index = 0; index < propertyCount_; index++) + if (properties_[index].name.GetStringLength() == len && + (std::memcmp(properties_[index].name.GetString(), str, sizeof(Ch) * len) == 0)) + { + *outIndex = index; + return true; + } + return false; + } + + bool CheckInt(Context& context, int64_t i) const { + if (!(type_ & ((1 << kIntegerSchemaType) | (1 << kNumberSchemaType)))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (!minimum_.IsNull()) { + if (minimum_.IsInt64()) { + if (exclusiveMinimum_ ? i <= minimum_.GetInt64() : i < minimum_.GetInt64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); + } + else if (minimum_.IsUint64()) { + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); // i <= max(int64_t) < minimum.GetUint64() + } + else if (!CheckDoubleMinimum(context, static_cast(i))) + return false; + } + + if (!maximum_.IsNull()) { + if (maximum_.IsInt64()) { + if (exclusiveMaximum_ ? i >= maximum_.GetInt64() : i > maximum_.GetInt64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); + } + else if (maximum_.IsUint64()) + /* do nothing */; // i <= max(int64_t) < maximum_.GetUint64() + else if (!CheckDoubleMaximum(context, static_cast(i))) + return false; + } + + if (!multipleOf_.IsNull()) { + if (multipleOf_.IsUint64()) { + if (static_cast(i >= 0 ? i : -i) % multipleOf_.GetUint64() != 0) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString()); + } + else if (!CheckDoubleMultipleOf(context, static_cast(i))) + return false; + } + + return true; + } + + bool CheckUint(Context& context, uint64_t i) const { + if (!(type_ & ((1 << kIntegerSchemaType) | (1 << kNumberSchemaType)))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (!minimum_.IsNull()) { + if (minimum_.IsUint64()) { + if (exclusiveMinimum_ ? i <= minimum_.GetUint64() : i < minimum_.GetUint64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); + } + else if (minimum_.IsInt64()) + /* do nothing */; // i >= 0 > minimum.Getint64() + else if (!CheckDoubleMinimum(context, static_cast(i))) + return false; + } + + if (!maximum_.IsNull()) { + if (maximum_.IsUint64()) { + if (exclusiveMaximum_ ? i >= maximum_.GetUint64() : i > maximum_.GetUint64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); + } + else if (maximum_.IsInt64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); // i >= 0 > maximum_ + else if (!CheckDoubleMaximum(context, static_cast(i))) + return false; + } + + if (!multipleOf_.IsNull()) { + if (multipleOf_.IsUint64()) { + if (i % multipleOf_.GetUint64() != 0) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString()); + } + else if (!CheckDoubleMultipleOf(context, static_cast(i))) + return false; + } + + return true; + } + + bool CheckDoubleMinimum(Context& context, double d) const { + if (exclusiveMinimum_ ? d <= minimum_.GetDouble() : d < minimum_.GetDouble()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); + return true; + } + + bool CheckDoubleMaximum(Context& context, double d) const { + if (exclusiveMaximum_ ? d >= maximum_.GetDouble() : d > maximum_.GetDouble()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); + return true; + } + + bool CheckDoubleMultipleOf(Context& context, double d) const { + double a = std::abs(d), b = std::abs(multipleOf_.GetDouble()); + double q = std::floor(a / b); + double r = a - q * b; + if (r > 0.0) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString()); + return true; + } + + struct Property { + Property() : schema(), dependenciesSchema(), dependenciesValidatorIndex(), dependencies(), required(false) {} + ~Property() { AllocatorType::Free(dependencies); } + SValue name; + const SchemaType* schema; + const SchemaType* dependenciesSchema; + SizeType dependenciesValidatorIndex; + bool* dependencies; + bool required; + }; + + struct PatternProperty { + PatternProperty() : schema(), pattern() {} + ~PatternProperty() { + if (pattern) { + pattern->~RegexType(); + AllocatorType::Free(pattern); + } + } + const SchemaType* schema; + RegexType* pattern; + }; + + AllocatorType* allocator_; + uint64_t* enum_; + SizeType enumCount_; + SchemaArray allOf_; + SchemaArray anyOf_; + SchemaArray oneOf_; + const SchemaType* not_; + unsigned type_; // bitmask of kSchemaType + SizeType validatorCount_; + SizeType notValidatorIndex_; + + Property* properties_; + const SchemaType* additionalPropertiesSchema_; + PatternProperty* patternProperties_; + SizeType patternPropertyCount_; + SizeType propertyCount_; + SizeType minProperties_; + SizeType maxProperties_; + bool additionalProperties_; + bool hasDependencies_; + bool hasRequired_; + bool hasSchemaDependencies_; + + const SchemaType* additionalItemsSchema_; + const SchemaType* itemsList_; + const SchemaType** itemsTuple_; + SizeType itemsTupleCount_; + SizeType minItems_; + SizeType maxItems_; + bool additionalItems_; + bool uniqueItems_; + + RegexType* pattern_; + SizeType minLength_; + SizeType maxLength_; + + SValue minimum_; + SValue maximum_; + SValue multipleOf_; + bool exclusiveMinimum_; + bool exclusiveMaximum_; +}; + +template +struct TokenHelper { + RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) { + *documentStack.template Push() = '/'; + char buffer[21]; + size_t length = static_cast((sizeof(SizeType) == 4 ? u32toa(index, buffer) : u64toa(index, buffer)) - buffer); + for (size_t i = 0; i < length; i++) + *documentStack.template Push() = buffer[i]; + } +}; + +// Partial specialized version for char to prevent buffer copying. +template +struct TokenHelper { + RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) { + if (sizeof(SizeType) == 4) { + char *buffer = documentStack.template Push(1 + 10); // '/' + uint + *buffer++ = '/'; + const char* end = internal::u32toa(index, buffer); + documentStack.template Pop(static_cast(10 - (end - buffer))); + } + else { + char *buffer = documentStack.template Push(1 + 20); // '/' + uint64 + *buffer++ = '/'; + const char* end = internal::u64toa(index, buffer); + documentStack.template Pop(static_cast(20 - (end - buffer))); + } + } +}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// IGenericRemoteSchemaDocumentProvider + +template +class IGenericRemoteSchemaDocumentProvider { +public: + typedef typename SchemaDocumentType::Ch Ch; + + virtual ~IGenericRemoteSchemaDocumentProvider() {} + virtual const SchemaDocumentType* GetRemoteDocument(const Ch* uri, SizeType length) = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericSchemaDocument + +//! JSON schema document. +/*! + A JSON schema document is a compiled version of a JSON schema. + It is basically a tree of internal::Schema. + + \note This is an immutable class (i.e. its instance cannot be modified after construction). + \tparam ValueT Type of JSON value (e.g. \c Value ), which also determine the encoding. + \tparam Allocator Allocator type for allocating memory of this document. +*/ +template +class GenericSchemaDocument { +public: + typedef ValueT ValueType; + typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProviderType; + typedef Allocator AllocatorType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + typedef internal::Schema SchemaType; + typedef GenericPointer PointerType; + friend class internal::Schema; + template + friend class GenericSchemaValidator; + + //! Constructor. + /*! + Compile a JSON document into schema document. + + \param document A JSON document as source. + \param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null. + \param allocator An optional allocator instance for allocating memory. Can be null. + */ + explicit GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) : + remoteProvider_(remoteProvider), + allocator_(allocator), + ownAllocator_(), + root_(), + schemaMap_(allocator, kInitialSchemaMapSize), + schemaRef_(allocator, kInitialSchemaRefSize) + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + + // Generate root schema, it will call CreateSchema() to create sub-schemas, + // And call AddRefSchema() if there are $ref. + CreateSchemaRecursive(&root_, PointerType(), document, document); + + // Resolve $ref + while (!schemaRef_.Empty()) { + SchemaRefEntry* refEntry = schemaRef_.template Pop(1); + if (const SchemaType* s = GetSchema(refEntry->target)) { + if (refEntry->schema) + *refEntry->schema = s; + + // Create entry in map if not exist + if (!GetSchema(refEntry->source)) { + new (schemaMap_.template Push()) SchemaEntry(refEntry->source, const_cast(s), false, allocator_); + } + } + refEntry->~SchemaRefEntry(); + } + + RAPIDJSON_ASSERT(root_ != 0); + + schemaRef_.ShrinkToFit(); // Deallocate all memory for ref + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericSchemaDocument(GenericSchemaDocument&& rhs) RAPIDJSON_NOEXCEPT : + remoteProvider_(rhs.remoteProvider_), + allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + root_(rhs.root_), + schemaMap_(std::move(rhs.schemaMap_)), + schemaRef_(std::move(rhs.schemaRef_)) + { + rhs.remoteProvider_ = 0; + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + } +#endif + + //! Destructor + ~GenericSchemaDocument() { + while (!schemaMap_.Empty()) + schemaMap_.template Pop(1)->~SchemaEntry(); + + RAPIDJSON_DELETE(ownAllocator_); + } + + //! Get the root schema. + const SchemaType& GetRoot() const { return *root_; } + +private: + //! Prohibit copying + GenericSchemaDocument(const GenericSchemaDocument&); + //! Prohibit assignment + GenericSchemaDocument& operator=(const GenericSchemaDocument&); + + struct SchemaRefEntry { + SchemaRefEntry(const PointerType& s, const PointerType& t, const SchemaType** outSchema, Allocator *allocator) : source(s, allocator), target(t, allocator), schema(outSchema) {} + PointerType source; + PointerType target; + const SchemaType** schema; + }; + + struct SchemaEntry { + SchemaEntry(const PointerType& p, SchemaType* s, bool o, Allocator* allocator) : pointer(p, allocator), schema(s), owned(o) {} + ~SchemaEntry() { + if (owned) { + schema->~SchemaType(); + Allocator::Free(schema); + } + } + PointerType pointer; + SchemaType* schema; + bool owned; + }; + + void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) { + if (schema) + *schema = SchemaType::GetTypeless(); + + if (v.GetType() == kObjectType) { + const SchemaType* s = GetSchema(pointer); + if (!s) + CreateSchema(schema, pointer, v, document); + + for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr) + CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document); + } + else if (v.GetType() == kArrayType) + for (SizeType i = 0; i < v.Size(); i++) + CreateSchemaRecursive(0, pointer.Append(i, allocator_), v[i], document); + } + + void CreateSchema(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) { + RAPIDJSON_ASSERT(pointer.IsValid()); + if (v.IsObject()) { + if (!HandleRefSchema(pointer, schema, v, document)) { + SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, pointer, v, document, allocator_); + new (schemaMap_.template Push()) SchemaEntry(pointer, s, true, allocator_); + if (schema) + *schema = s; + } + } + } + + bool HandleRefSchema(const PointerType& source, const SchemaType** schema, const ValueType& v, const ValueType& document) { + static const Ch kRefString[] = { '$', 'r', 'e', 'f', '\0' }; + static const ValueType kRefValue(kRefString, 4); + + typename ValueType::ConstMemberIterator itr = v.FindMember(kRefValue); + if (itr == v.MemberEnd()) + return false; + + if (itr->value.IsString()) { + SizeType len = itr->value.GetStringLength(); + if (len > 0) { + const Ch* s = itr->value.GetString(); + SizeType i = 0; + while (i < len && s[i] != '#') // Find the first # + i++; + + if (i > 0) { // Remote reference, resolve immediately + if (remoteProvider_) { + if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(s, i - 1)) { + PointerType pointer(&s[i], len - i, allocator_); + if (pointer.IsValid()) { + if (const SchemaType* sc = remoteDocument->GetSchema(pointer)) { + if (schema) + *schema = sc; + return true; + } + } + } + } + } + else if (s[i] == '#') { // Local reference, defer resolution + PointerType pointer(&s[i], len - i, allocator_); + if (pointer.IsValid()) { + if (const ValueType* nv = pointer.Get(document)) + if (HandleRefSchema(source, schema, *nv, document)) + return true; + + new (schemaRef_.template Push()) SchemaRefEntry(source, pointer, schema, allocator_); + return true; + } + } + } + } + return false; + } + + const SchemaType* GetSchema(const PointerType& pointer) const { + for (const SchemaEntry* target = schemaMap_.template Bottom(); target != schemaMap_.template End(); ++target) + if (pointer == target->pointer) + return target->schema; + return 0; + } + + PointerType GetPointer(const SchemaType* schema) const { + for (const SchemaEntry* target = schemaMap_.template Bottom(); target != schemaMap_.template End(); ++target) + if (schema == target->schema) + return target->pointer; + return PointerType(); + } + + static const size_t kInitialSchemaMapSize = 64; + static const size_t kInitialSchemaRefSize = 64; + + IRemoteSchemaDocumentProviderType* remoteProvider_; + Allocator *allocator_; + Allocator *ownAllocator_; + const SchemaType* root_; //!< Root schema. + internal::Stack schemaMap_; // Stores created Pointer -> Schemas + internal::Stack schemaRef_; // Stores Pointer from $ref and schema which holds the $ref +}; + +//! GenericSchemaDocument using Value type. +typedef GenericSchemaDocument SchemaDocument; +//! IGenericRemoteSchemaDocumentProvider using SchemaDocument. +typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProvider; + +/////////////////////////////////////////////////////////////////////////////// +// GenericSchemaValidator + +//! JSON Schema Validator. +/*! + A SAX style JSON schema validator. + It uses a \c GenericSchemaDocument to validate SAX events. + It delegates the incoming SAX events to an output handler. + The default output handler does nothing. + It can be reused multiple times by calling \c Reset(). + + \tparam SchemaDocumentType Type of schema document. + \tparam OutputHandler Type of output handler. Default handler does nothing. + \tparam StateAllocator Allocator for storing the internal validation states. +*/ +template < + typename SchemaDocumentType, + typename OutputHandler = BaseReaderHandler, + typename StateAllocator = CrtAllocator> +class GenericSchemaValidator : + public internal::ISchemaStateFactory, + public internal::ISchemaValidator +{ +public: + typedef typename SchemaDocumentType::SchemaType SchemaType; + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename SchemaType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + + //! Constructor without output handler. + /*! + \param schemaDocument The schema document to conform to. + \param allocator Optional allocator for storing internal validation states. + \param schemaStackCapacity Optional initial capacity of schema path stack. + \param documentStackCapacity Optional initial capacity of document path stack. + */ + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(schemaDocument.GetRoot()), + outputHandler_(GetNullHandler()), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + valid_(true) +#if RAPIDJSON_SCHEMA_VERBOSE + , depth_(0) +#endif + { + } + + //! Constructor with output handler. + /*! + \param schemaDocument The schema document to conform to. + \param allocator Optional allocator for storing internal validation states. + \param schemaStackCapacity Optional initial capacity of schema path stack. + \param documentStackCapacity Optional initial capacity of document path stack. + */ + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + OutputHandler& outputHandler, + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(schemaDocument.GetRoot()), + outputHandler_(outputHandler), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + valid_(true) +#if RAPIDJSON_SCHEMA_VERBOSE + , depth_(0) +#endif + { + } + + //! Destructor. + ~GenericSchemaValidator() { + Reset(); + RAPIDJSON_DELETE(ownStateAllocator_); + } + + //! Reset the internal states. + void Reset() { + while (!schemaStack_.Empty()) + PopSchema(); + documentStack_.Clear(); + valid_ = true; + } + + //! Checks whether the current state is valid. + // Implementation of ISchemaValidator + virtual bool IsValid() const { return valid_; } + + //! Gets the JSON pointer pointed to the invalid schema. + PointerType GetInvalidSchemaPointer() const { + return schemaStack_.Empty() ? PointerType() : schemaDocument_->GetPointer(&CurrentSchema()); + } + + //! Gets the keyword of invalid schema. + const Ch* GetInvalidSchemaKeyword() const { + return schemaStack_.Empty() ? 0 : CurrentContext().invalidKeyword; + } + + //! Gets the JSON pointer pointed to the invalid value. + PointerType GetInvalidDocumentPointer() const { + return documentStack_.Empty() ? PointerType() : PointerType(documentStack_.template Bottom(), documentStack_.GetSize() / sizeof(Ch)); + } + +#if RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_() \ +RAPIDJSON_MULTILINEMACRO_BEGIN\ + *documentStack_.template Push() = '\0';\ + documentStack_.template Pop(1);\ + internal::PrintInvalidDocument(documentStack_.template Bottom());\ +RAPIDJSON_MULTILINEMACRO_END +#else +#define RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_() +#endif + +#define RAPIDJSON_SCHEMA_HANDLE_BEGIN_(method, arg1)\ + if (!valid_) return false; \ + if (!BeginValue() || !CurrentSchema().method arg1) {\ + RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_();\ + return valid_ = false;\ + } + +#define RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(method, arg2)\ + for (Context* context = schemaStack_.template Bottom(); context != schemaStack_.template End(); context++) {\ + if (context->hasher)\ + static_cast(context->hasher)->method arg2;\ + if (context->validators)\ + for (SizeType i_ = 0; i_ < context->validatorCount; i_++)\ + static_cast(context->validators[i_])->method arg2;\ + if (context->patternPropertiesValidators)\ + for (SizeType i_ = 0; i_ < context->patternPropertiesValidatorCount; i_++)\ + static_cast(context->patternPropertiesValidators[i_])->method arg2;\ + } + +#define RAPIDJSON_SCHEMA_HANDLE_END_(method, arg2)\ + return valid_ = EndValue() && outputHandler_.method arg2 + +#define RAPIDJSON_SCHEMA_HANDLE_VALUE_(method, arg1, arg2) \ + RAPIDJSON_SCHEMA_HANDLE_BEGIN_ (method, arg1);\ + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(method, arg2);\ + RAPIDJSON_SCHEMA_HANDLE_END_ (method, arg2) + + bool Null() { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Null, (CurrentContext() ), ( )); } + bool Bool(bool b) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Bool, (CurrentContext(), b), (b)); } + bool Int(int i) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Int, (CurrentContext(), i), (i)); } + bool Uint(unsigned u) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Uint, (CurrentContext(), u), (u)); } + bool Int64(int64_t i) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Int64, (CurrentContext(), i), (i)); } + bool Uint64(uint64_t u) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Uint64, (CurrentContext(), u), (u)); } + bool Double(double d) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Double, (CurrentContext(), d), (d)); } + bool RawNumber(const Ch* str, SizeType length, bool copy) + { RAPIDJSON_SCHEMA_HANDLE_VALUE_(String, (CurrentContext(), str, length, copy), (str, length, copy)); } + bool String(const Ch* str, SizeType length, bool copy) + { RAPIDJSON_SCHEMA_HANDLE_VALUE_(String, (CurrentContext(), str, length, copy), (str, length, copy)); } + + bool StartObject() { + RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartObject, (CurrentContext())); + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartObject, ()); + return valid_ = outputHandler_.StartObject(); + } + + bool Key(const Ch* str, SizeType len, bool copy) { + if (!valid_) return false; + AppendToken(str, len); + if (!CurrentSchema().Key(CurrentContext(), str, len, copy)) return valid_ = false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(Key, (str, len, copy)); + return valid_ = outputHandler_.Key(str, len, copy); + } + + bool EndObject(SizeType memberCount) { + if (!valid_) return false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(EndObject, (memberCount)); + if (!CurrentSchema().EndObject(CurrentContext(), memberCount)) return valid_ = false; + RAPIDJSON_SCHEMA_HANDLE_END_(EndObject, (memberCount)); + } + + bool StartArray() { + RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartArray, (CurrentContext())); + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartArray, ()); + return valid_ = outputHandler_.StartArray(); + } + + bool EndArray(SizeType elementCount) { + if (!valid_) return false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(EndArray, (elementCount)); + if (!CurrentSchema().EndArray(CurrentContext(), elementCount)) return valid_ = false; + RAPIDJSON_SCHEMA_HANDLE_END_(EndArray, (elementCount)); + } + +#undef RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_ +#undef RAPIDJSON_SCHEMA_HANDLE_BEGIN_ +#undef RAPIDJSON_SCHEMA_HANDLE_PARALLEL_ +#undef RAPIDJSON_SCHEMA_HANDLE_VALUE_ + + // Implementation of ISchemaStateFactory + virtual ISchemaValidator* CreateSchemaValidator(const SchemaType& root) { + return new (GetStateAllocator().Malloc(sizeof(GenericSchemaValidator))) GenericSchemaValidator(*schemaDocument_, root, +#if RAPIDJSON_SCHEMA_VERBOSE + depth_ + 1, +#endif + &GetStateAllocator()); + } + + virtual void DestroySchemaValidator(ISchemaValidator* validator) { + GenericSchemaValidator* v = static_cast(validator); + v->~GenericSchemaValidator(); + StateAllocator::Free(v); + } + + virtual void* CreateHasher() { + return new (GetStateAllocator().Malloc(sizeof(HasherType))) HasherType(&GetStateAllocator()); + } + + virtual uint64_t GetHashCode(void* hasher) { + return static_cast(hasher)->GetHashCode(); + } + + virtual void DestroryHasher(void* hasher) { + HasherType* h = static_cast(hasher); + h->~HasherType(); + StateAllocator::Free(h); + } + + virtual void* MallocState(size_t size) { + return GetStateAllocator().Malloc(size); + } + + virtual void FreeState(void* p) { + return StateAllocator::Free(p); + } + +private: + typedef typename SchemaType::Context Context; + typedef GenericValue, StateAllocator> HashCodeArray; + typedef internal::Hasher HasherType; + + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + const SchemaType& root, +#if RAPIDJSON_SCHEMA_VERBOSE + unsigned depth, +#endif + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(root), + outputHandler_(GetNullHandler()), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + valid_(true) +#if RAPIDJSON_SCHEMA_VERBOSE + , depth_(depth) +#endif + { + } + + StateAllocator& GetStateAllocator() { + if (!stateAllocator_) + stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator()); + return *stateAllocator_; + } + + bool BeginValue() { + if (schemaStack_.Empty()) + PushSchema(root_); + else { + if (CurrentContext().inArray) + internal::TokenHelper, Ch>::AppendIndexToken(documentStack_, CurrentContext().arrayElementIndex); + + if (!CurrentSchema().BeginValue(CurrentContext())) + return false; + + SizeType count = CurrentContext().patternPropertiesSchemaCount; + const SchemaType** sa = CurrentContext().patternPropertiesSchemas; + typename Context::PatternValidatorType patternValidatorType = CurrentContext().valuePatternValidatorType; + bool valueUniqueness = CurrentContext().valueUniqueness; + if (CurrentContext().valueSchema) + PushSchema(*CurrentContext().valueSchema); + + if (count > 0) { + CurrentContext().objectPatternValidatorType = patternValidatorType; + ISchemaValidator**& va = CurrentContext().patternPropertiesValidators; + SizeType& validatorCount = CurrentContext().patternPropertiesValidatorCount; + va = static_cast(MallocState(sizeof(ISchemaValidator*) * count)); + for (SizeType i = 0; i < count; i++) + va[validatorCount++] = CreateSchemaValidator(*sa[i]); + } + + CurrentContext().arrayUniqueness = valueUniqueness; + } + return true; + } + + bool EndValue() { + if (!CurrentSchema().EndValue(CurrentContext())) + return false; + +#if RAPIDJSON_SCHEMA_VERBOSE + GenericStringBuffer sb; + schemaDocument_->GetPointer(&CurrentSchema()).Stringify(sb); + + *documentStack_.template Push() = '\0'; + documentStack_.template Pop(1); + internal::PrintValidatorPointers(depth_, sb.GetString(), documentStack_.template Bottom()); +#endif + + uint64_t h = CurrentContext().arrayUniqueness ? static_cast(CurrentContext().hasher)->GetHashCode() : 0; + + PopSchema(); + + if (!schemaStack_.Empty()) { + Context& context = CurrentContext(); + if (context.valueUniqueness) { + HashCodeArray* a = static_cast(context.arrayElementHashCodes); + if (!a) + CurrentContext().arrayElementHashCodes = a = new (GetStateAllocator().Malloc(sizeof(HashCodeArray))) HashCodeArray(kArrayType); + for (typename HashCodeArray::ConstValueIterator itr = a->Begin(); itr != a->End(); ++itr) + if (itr->GetUint64() == h) + RAPIDJSON_INVALID_KEYWORD_RETURN(SchemaType::GetUniqueItemsString()); + a->PushBack(h, GetStateAllocator()); + } + } + + // Remove the last token of document pointer + while (!documentStack_.Empty() && *documentStack_.template Pop(1) != '/') + ; + + return true; + } + + void AppendToken(const Ch* str, SizeType len) { + documentStack_.template Reserve(1 + len * 2); // worst case all characters are escaped as two characters + *documentStack_.template PushUnsafe() = '/'; + for (SizeType i = 0; i < len; i++) { + if (str[i] == '~') { + *documentStack_.template PushUnsafe() = '~'; + *documentStack_.template PushUnsafe() = '0'; + } + else if (str[i] == '/') { + *documentStack_.template PushUnsafe() = '~'; + *documentStack_.template PushUnsafe() = '1'; + } + else + *documentStack_.template PushUnsafe() = str[i]; + } + } + + RAPIDJSON_FORCEINLINE void PushSchema(const SchemaType& schema) { new (schemaStack_.template Push()) Context(*this, &schema); } + + RAPIDJSON_FORCEINLINE void PopSchema() { + Context* c = schemaStack_.template Pop(1); + if (HashCodeArray* a = static_cast(c->arrayElementHashCodes)) { + a->~HashCodeArray(); + StateAllocator::Free(a); + } + c->~Context(); + } + + const SchemaType& CurrentSchema() const { return *schemaStack_.template Top()->schema; } + Context& CurrentContext() { return *schemaStack_.template Top(); } + const Context& CurrentContext() const { return *schemaStack_.template Top(); } + + static OutputHandler& GetNullHandler() { + static OutputHandler nullHandler; + return nullHandler; + } + + static const size_t kDefaultSchemaStackCapacity = 1024; + static const size_t kDefaultDocumentStackCapacity = 256; + const SchemaDocumentType* schemaDocument_; + const SchemaType& root_; + OutputHandler& outputHandler_; + StateAllocator* stateAllocator_; + StateAllocator* ownStateAllocator_; + internal::Stack schemaStack_; //!< stack to store the current path of schema (BaseSchemaType *) + internal::Stack documentStack_; //!< stack to store the current path of validating document (Ch) + bool valid_; +#if RAPIDJSON_SCHEMA_VERBOSE + unsigned depth_; +#endif +}; + +typedef GenericSchemaValidator SchemaValidator; + +/////////////////////////////////////////////////////////////////////////////// +// SchemaValidatingReader + +//! A helper class for parsing with validation. +/*! + This helper class is a functor, designed as a parameter of \ref GenericDocument::Populate(). + + \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept. + \tparam SourceEncoding Encoding of the input stream. + \tparam SchemaDocumentType Type of schema document. + \tparam StackAllocator Allocator type for stack. +*/ +template < + unsigned parseFlags, + typename InputStream, + typename SourceEncoding, + typename SchemaDocumentType = SchemaDocument, + typename StackAllocator = CrtAllocator> +class SchemaValidatingReader { +public: + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename InputStream::Ch Ch; + + //! Constructor + /*! + \param is Input stream. + \param sd Schema document. + */ + SchemaValidatingReader(InputStream& is, const SchemaDocumentType& sd) : is_(is), sd_(sd), invalidSchemaKeyword_(), isValid_(true) {} + + template + bool operator()(Handler& handler) { + GenericReader reader; + GenericSchemaValidator validator(sd_, handler); + parseResult_ = reader.template Parse(is_, validator); + + isValid_ = validator.IsValid(); + if (isValid_) { + invalidSchemaPointer_ = PointerType(); + invalidSchemaKeyword_ = 0; + invalidDocumentPointer_ = PointerType(); + } + else { + invalidSchemaPointer_ = validator.GetInvalidSchemaPointer(); + invalidSchemaKeyword_ = validator.GetInvalidSchemaKeyword(); + invalidDocumentPointer_ = validator.GetInvalidDocumentPointer(); + } + + return parseResult_; + } + + const ParseResult& GetParseResult() const { return parseResult_; } + bool IsValid() const { return isValid_; } + const PointerType& GetInvalidSchemaPointer() const { return invalidSchemaPointer_; } + const Ch* GetInvalidSchemaKeyword() const { return invalidSchemaKeyword_; } + const PointerType& GetInvalidDocumentPointer() const { return invalidDocumentPointer_; } + +private: + InputStream& is_; + const SchemaDocumentType& sd_; + + ParseResult parseResult_; + PointerType invalidSchemaPointer_; + const Ch* invalidSchemaKeyword_; + PointerType invalidDocumentPointer_; + bool isValid_; +}; + +RAPIDJSON_NAMESPACE_END +RAPIDJSON_DIAG_POP + +#endif // RAPIDJSON_SCHEMA_H_ diff --git a/src/3rdparty/rapidjson/stream.h b/src/3rdparty/rapidjson/stream.h new file mode 100644 index 00000000..fef82c25 --- /dev/null +++ b/src/3rdparty/rapidjson/stream.h @@ -0,0 +1,179 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include "rapidjson.h" + +#ifndef RAPIDJSON_STREAM_H_ +#define RAPIDJSON_STREAM_H_ + +#include "encodings.h" + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Stream + +/*! \class rapidjson::Stream + \brief Concept for reading and writing characters. + + For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd(). + + For write-only stream, only need to implement Put() and Flush(). + +\code +concept Stream { + typename Ch; //!< Character type of the stream. + + //! Read the current character from stream without moving the read cursor. + Ch Peek() const; + + //! Read the current character from stream and moving the read cursor to next character. + Ch Take(); + + //! Get the current read cursor. + //! \return Number of characters read from start. + size_t Tell(); + + //! Begin writing operation at the current read pointer. + //! \return The begin writer pointer. + Ch* PutBegin(); + + //! Write a character. + void Put(Ch c); + + //! Flush the buffer. + void Flush(); + + //! End the writing operation. + //! \param begin The begin write pointer returned by PutBegin(). + //! \return Number of characters written. + size_t PutEnd(Ch* begin); +} +\endcode +*/ + +//! Provides additional information for stream. +/*! + By using traits pattern, this type provides a default configuration for stream. + For custom stream, this type can be specialized for other configuration. + See TEST(Reader, CustomStringStream) in readertest.cpp for example. +*/ +template +struct StreamTraits { + //! Whether to make local copy of stream for optimization during parsing. + /*! + By default, for safety, streams do not use local copy optimization. + Stream that can be copied fast should specialize this, like StreamTraits. + */ + enum { copyOptimization = 0 }; +}; + +//! Reserve n characters for writing to a stream. +template +inline void PutReserve(Stream& stream, size_t count) { + (void)stream; + (void)count; +} + +//! Write character to a stream, presuming buffer is reserved. +template +inline void PutUnsafe(Stream& stream, typename Stream::Ch c) { + stream.Put(c); +} + +//! Put N copies of a character to a stream. +template +inline void PutN(Stream& stream, Ch c, size_t n) { + PutReserve(stream, n); + for (size_t i = 0; i < n; i++) + PutUnsafe(stream, c); +} + +/////////////////////////////////////////////////////////////////////////////// +// StringStream + +//! Read-only string stream. +/*! \note implements Stream concept +*/ +template +struct GenericStringStream { + typedef typename Encoding::Ch Ch; + + GenericStringStream(const Ch *src) : src_(src), head_(src) {} + + Ch Peek() const { return *src_; } + Ch Take() { return *src_++; } + size_t Tell() const { return static_cast(src_ - head_); } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + const Ch* src_; //!< Current read position. + const Ch* head_; //!< Original head of the string. +}; + +template +struct StreamTraits > { + enum { copyOptimization = 1 }; +}; + +//! String stream with UTF8 encoding. +typedef GenericStringStream > StringStream; + +/////////////////////////////////////////////////////////////////////////////// +// InsituStringStream + +//! A read-write string stream. +/*! This string stream is particularly designed for in-situ parsing. + \note implements Stream concept +*/ +template +struct GenericInsituStringStream { + typedef typename Encoding::Ch Ch; + + GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {} + + // Read + Ch Peek() { return *src_; } + Ch Take() { return *src_++; } + size_t Tell() { return static_cast(src_ - head_); } + + // Write + void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; } + + Ch* PutBegin() { return dst_ = src_; } + size_t PutEnd(Ch* begin) { return static_cast(dst_ - begin); } + void Flush() {} + + Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; } + void Pop(size_t count) { dst_ -= count; } + + Ch* src_; + Ch* dst_; + Ch* head_; +}; + +template +struct StreamTraits > { + enum { copyOptimization = 1 }; +}; + +//! Insitu string stream with UTF8 encoding. +typedef GenericInsituStringStream > InsituStringStream; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STREAM_H_ diff --git a/src/3rdparty/rapidjson/stringbuffer.h b/src/3rdparty/rapidjson/stringbuffer.h new file mode 100644 index 00000000..78f34d20 --- /dev/null +++ b/src/3rdparty/rapidjson/stringbuffer.h @@ -0,0 +1,117 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_STRINGBUFFER_H_ +#define RAPIDJSON_STRINGBUFFER_H_ + +#include "stream.h" +#include "internal/stack.h" + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#include // std::move +#endif + +#include "internal/stack.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory output stream. +/*! + \tparam Encoding Encoding of the stream. + \tparam Allocator type for allocating memory buffer. + \note implements Stream concept +*/ +template +class GenericStringBuffer { +public: + typedef typename Encoding::Ch Ch; + + GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} + GenericStringBuffer& operator=(GenericStringBuffer&& rhs) { + if (&rhs != this) + stack_ = std::move(rhs.stack_); + return *this; + } +#endif + + void Put(Ch c) { *stack_.template Push() = c; } + void PutUnsafe(Ch c) { *stack_.template PushUnsafe() = c; } + void Flush() {} + + void Clear() { stack_.Clear(); } + void ShrinkToFit() { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.ShrinkToFit(); + stack_.template Pop(1); + } + + void Reserve(size_t count) { stack_.template Reserve(count); } + Ch* Push(size_t count) { return stack_.template Push(count); } + Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe(count); } + void Pop(size_t count) { stack_.template Pop(count); } + + const Ch* GetString() const { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.template Pop(1); + + return stack_.template Bottom(); + } + + size_t GetSize() const { return stack_.GetSize(); } + + static const size_t kDefaultCapacity = 256; + mutable internal::Stack stack_; + +private: + // Prohibit copy constructor & assignment operator. + GenericStringBuffer(const GenericStringBuffer&); + GenericStringBuffer& operator=(const GenericStringBuffer&); +}; + +//! String buffer with UTF8 encoding +typedef GenericStringBuffer > StringBuffer; + +template +inline void PutReserve(GenericStringBuffer& stream, size_t count) { + stream.Reserve(count); +} + +template +inline void PutUnsafe(GenericStringBuffer& stream, typename Encoding::Ch c) { + stream.PutUnsafe(c); +} + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(GenericStringBuffer >& stream, char c, size_t n) { + std::memset(stream.stack_.Push(n), c, n * sizeof(c)); +} + +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_STRINGBUFFER_H_ diff --git a/src/3rdparty/rapidjson/writer.h b/src/3rdparty/rapidjson/writer.h new file mode 100644 index 00000000..94f22dd5 --- /dev/null +++ b/src/3rdparty/rapidjson/writer.h @@ -0,0 +1,610 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_WRITER_H_ +#define RAPIDJSON_WRITER_H_ + +#include "stream.h" +#include "internal/stack.h" +#include "internal/strfunc.h" +#include "internal/dtoa.h" +#include "internal/itoa.h" +#include "stringbuffer.h" +#include // placement new + +#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) +#include +#pragma intrinsic(_BitScanForward) +#endif +#ifdef RAPIDJSON_SSE42 +#include +#elif defined(RAPIDJSON_SSE2) +#include +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(unreachable-code) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// WriteFlag + +/*! \def RAPIDJSON_WRITE_DEFAULT_FLAGS + \ingroup RAPIDJSON_CONFIG + \brief User-defined kWriteDefaultFlags definition. + + User can define this as any \c WriteFlag combinations. +*/ +#ifndef RAPIDJSON_WRITE_DEFAULT_FLAGS +#define RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNoFlags +#endif + +//! Combination of writeFlags +enum WriteFlag { + kWriteNoFlags = 0, //!< No flags are set. + kWriteValidateEncodingFlag = 1, //!< Validate encoding of JSON strings. + kWriteNanAndInfFlag = 2, //!< Allow writing of Infinity, -Infinity and NaN. + kWriteDefaultFlags = RAPIDJSON_WRITE_DEFAULT_FLAGS //!< Default write flags. Can be customized by defining RAPIDJSON_WRITE_DEFAULT_FLAGS +}; + +//! JSON writer +/*! Writer implements the concept Handler. + It generates JSON text by events to an output os. + + User may programmatically calls the functions of a writer to generate JSON text. + + On the other side, a writer can also be passed to objects that generates events, + + for example Reader::Parse() and Document::Accept(). + + \tparam OutputStream Type of output stream. + \tparam SourceEncoding Encoding of source string. + \tparam TargetEncoding Encoding of output stream. + \tparam StackAllocator Type of allocator for allocating memory of stack. + \note implements Handler concept +*/ +template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags> +class Writer { +public: + typedef typename SourceEncoding::Ch Ch; + + static const int kDefaultMaxDecimalPlaces = 324; + + //! Constructor + /*! \param os Output stream. + \param stackAllocator User supplied allocator. If it is null, it will create a private one. + \param levelDepth Initial capacity of stack. + */ + explicit + Writer(OutputStream& os, StackAllocator* stackAllocator = 0, size_t levelDepth = kDefaultLevelDepth) : + os_(&os), level_stack_(stackAllocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {} + + explicit + Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : + os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {} + + //! Reset the writer with a new stream. + /*! + This function reset the writer with a new stream and default settings, + in order to make a Writer object reusable for output multiple JSONs. + + \param os New output stream. + \code + Writer writer(os1); + writer.StartObject(); + // ... + writer.EndObject(); + + writer.Reset(os2); + writer.StartObject(); + // ... + writer.EndObject(); + \endcode + */ + void Reset(OutputStream& os) { + os_ = &os; + hasRoot_ = false; + level_stack_.Clear(); + } + + //! Checks whether the output is a complete JSON. + /*! + A complete JSON has a complete root object or array. + */ + bool IsComplete() const { + return hasRoot_ && level_stack_.Empty(); + } + + int GetMaxDecimalPlaces() const { + return maxDecimalPlaces_; + } + + //! Sets the maximum number of decimal places for double output. + /*! + This setting truncates the output with specified number of decimal places. + + For example, + + \code + writer.SetMaxDecimalPlaces(3); + writer.StartArray(); + writer.Double(0.12345); // "0.123" + writer.Double(0.0001); // "0.0" + writer.Double(1.234567890123456e30); // "1.234567890123456e30" (do not truncate significand for positive exponent) + writer.Double(1.23e-4); // "0.0" (do truncate significand for negative exponent) + writer.EndArray(); + \endcode + + The default setting does not truncate any decimal places. You can restore to this setting by calling + \code + writer.SetMaxDecimalPlaces(Writer::kDefaultMaxDecimalPlaces); + \endcode + */ + void SetMaxDecimalPlaces(int maxDecimalPlaces) { + maxDecimalPlaces_ = maxDecimalPlaces; + } + + /*!@name Implementation of Handler + \see Handler + */ + //@{ + + bool Null() { Prefix(kNullType); return EndValue(WriteNull()); } + bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return EndValue(WriteBool(b)); } + bool Int(int i) { Prefix(kNumberType); return EndValue(WriteInt(i)); } + bool Uint(unsigned u) { Prefix(kNumberType); return EndValue(WriteUint(u)); } + bool Int64(int64_t i64) { Prefix(kNumberType); return EndValue(WriteInt64(i64)); } + bool Uint64(uint64_t u64) { Prefix(kNumberType); return EndValue(WriteUint64(u64)); } + + //! Writes the given \c double value to the stream + /*! + \param d The value to be written. + \return Whether it is succeed. + */ + bool Double(double d) { Prefix(kNumberType); return EndValue(WriteDouble(d)); } + + bool RawNumber(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + Prefix(kNumberType); + return EndValue(WriteString(str, length)); + } + + bool String(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + Prefix(kStringType); + return EndValue(WriteString(str, length)); + } + +#if RAPIDJSON_HAS_STDSTRING + bool String(const std::basic_string& str) { + return String(str.data(), SizeType(str.size())); + } +#endif + + bool StartObject() { + Prefix(kObjectType); + new (level_stack_.template Push()) Level(false); + return WriteStartObject(); + } + + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + + bool EndObject(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); + RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); + level_stack_.template Pop(1); + return EndValue(WriteEndObject()); + } + + bool StartArray() { + Prefix(kArrayType); + new (level_stack_.template Push()) Level(true); + return WriteStartArray(); + } + + bool EndArray(SizeType elementCount = 0) { + (void)elementCount; + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); + RAPIDJSON_ASSERT(level_stack_.template Top()->inArray); + level_stack_.template Pop(1); + return EndValue(WriteEndArray()); + } + //@} + + /*! @name Convenience extensions */ + //@{ + + //! Simpler but slower overload. + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + + //@} + + //! Write a raw JSON value. + /*! + For user to write a stringified JSON as a value. + + \param json A well-formed JSON value. It should not contain null character within [0, length - 1] range. + \param length Length of the json. + \param type Type of the root of json. + */ + bool RawValue(const Ch* json, size_t length, Type type) { Prefix(type); return EndValue(WriteRawValue(json, length)); } + +protected: + //! Information for each nested level + struct Level { + Level(bool inArray_) : valueCount(0), inArray(inArray_) {} + size_t valueCount; //!< number of values in this level + bool inArray; //!< true if in array, otherwise in object + }; + + static const size_t kDefaultLevelDepth = 32; + + bool WriteNull() { + PutReserve(*os_, 4); + PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 'l'); return true; + } + + bool WriteBool(bool b) { + if (b) { + PutReserve(*os_, 4); + PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'r'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'e'); + } + else { + PutReserve(*os_, 5); + PutUnsafe(*os_, 'f'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 's'); PutUnsafe(*os_, 'e'); + } + return true; + } + + bool WriteInt(int i) { + char buffer[11]; + const char* end = internal::i32toa(i, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteUint(unsigned u) { + char buffer[10]; + const char* end = internal::u32toa(u, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteInt64(int64_t i64) { + char buffer[21]; + const char* end = internal::i64toa(i64, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteUint64(uint64_t u64) { + char buffer[20]; + char* end = internal::u64toa(u64, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteDouble(double d) { + if (internal::Double(d).IsNanOrInf()) { + if (!(writeFlags & kWriteNanAndInfFlag)) + return false; + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } + + char buffer[25]; + char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); + PutReserve(*os_, static_cast(end - buffer)); + for (char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteString(const Ch* str, SizeType length) { + static const typename TargetEncoding::Ch hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + static const char escape[256] = { +#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + //0 1 2 3 4 5 6 7 8 9 A B C D E F + 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00 + 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10 + 0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 + Z16, Z16, // 30~4F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, // 50 + Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF +#undef Z16 + }; + + if (TargetEncoding::supportUnicode) + PutReserve(*os_, 2 + length * 6); // "\uxxxx..." + else + PutReserve(*os_, 2 + length * 12); // "\uxxxx\uyyyy..." + + PutUnsafe(*os_, '\"'); + GenericStringStream is(str); + while (ScanWriteUnescapedString(is, length)) { + const Ch c = is.Peek(); + if (!TargetEncoding::supportUnicode && static_cast(c) >= 0x80) { + // Unicode escaping + unsigned codepoint; + if (RAPIDJSON_UNLIKELY(!SourceEncoding::Decode(is, &codepoint))) + return false; + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, 'u'); + if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { + PutUnsafe(*os_, hexDigits[(codepoint >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint ) & 15]); + } + else { + RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF); + // Surrogate pair + unsigned s = codepoint - 0x010000; + unsigned lead = (s >> 10) + 0xD800; + unsigned trail = (s & 0x3FF) + 0xDC00; + PutUnsafe(*os_, hexDigits[(lead >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(lead >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(lead >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(lead ) & 15]); + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, 'u'); + PutUnsafe(*os_, hexDigits[(trail >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(trail >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(trail >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(trail ) & 15]); + } + } + else if ((sizeof(Ch) == 1 || static_cast(c) < 256) && RAPIDJSON_UNLIKELY(escape[static_cast(c)])) { + is.Take(); + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, static_cast(escape[static_cast(c)])); + if (escape[static_cast(c)] == 'u') { + PutUnsafe(*os_, '0'); + PutUnsafe(*os_, '0'); + PutUnsafe(*os_, hexDigits[static_cast(c) >> 4]); + PutUnsafe(*os_, hexDigits[static_cast(c) & 0xF]); + } + } + else if (RAPIDJSON_UNLIKELY(!(writeFlags & kWriteValidateEncodingFlag ? + Transcoder::Validate(is, *os_) : + Transcoder::TranscodeUnsafe(is, *os_)))) + return false; + } + PutUnsafe(*os_, '\"'); + return true; + } + + bool ScanWriteUnescapedString(GenericStringStream& is, size_t length) { + return RAPIDJSON_LIKELY(is.Tell() < length); + } + + bool WriteStartObject() { os_->Put('{'); return true; } + bool WriteEndObject() { os_->Put('}'); return true; } + bool WriteStartArray() { os_->Put('['); return true; } + bool WriteEndArray() { os_->Put(']'); return true; } + + bool WriteRawValue(const Ch* json, size_t length) { + PutReserve(*os_, length); + for (size_t i = 0; i < length; i++) { + RAPIDJSON_ASSERT(json[i] != '\0'); + PutUnsafe(*os_, json[i]); + } + return true; + } + + void Prefix(Type type) { + (void)type; + if (RAPIDJSON_LIKELY(level_stack_.GetSize() != 0)) { // this value is not at root + Level* level = level_stack_.template Top(); + if (level->valueCount > 0) { + if (level->inArray) + os_->Put(','); // add comma if it is not the first element in array + else // in object + os_->Put((level->valueCount % 2 == 0) ? ',' : ':'); + } + if (!level->inArray && level->valueCount % 2 == 0) + RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name + level->valueCount++; + } + else { + RAPIDJSON_ASSERT(!hasRoot_); // Should only has one and only one root. + hasRoot_ = true; + } + } + + // Flush the value if it is the top level one. + bool EndValue(bool ret) { + if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text + os_->Flush(); + return ret; + } + + OutputStream* os_; + internal::Stack level_stack_; + int maxDecimalPlaces_; + bool hasRoot_; + +private: + // Prohibit copy constructor & assignment operator. + Writer(const Writer&); + Writer& operator=(const Writer&); +}; + +// Full specialization for StringStream to prevent memory copying + +template<> +inline bool Writer::WriteInt(int i) { + char *buffer = os_->Push(11); + const char* end = internal::i32toa(i, buffer); + os_->Pop(static_cast(11 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteUint(unsigned u) { + char *buffer = os_->Push(10); + const char* end = internal::u32toa(u, buffer); + os_->Pop(static_cast(10 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteInt64(int64_t i64) { + char *buffer = os_->Push(21); + const char* end = internal::i64toa(i64, buffer); + os_->Pop(static_cast(21 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteUint64(uint64_t u) { + char *buffer = os_->Push(20); + const char* end = internal::u64toa(u, buffer); + os_->Pop(static_cast(20 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteDouble(double d) { + if (internal::Double(d).IsNanOrInf()) { + // Note: This code path can only be reached if (RAPIDJSON_WRITE_DEFAULT_FLAGS & kWriteNanAndInfFlag). + if (!(kWriteDefaultFlags & kWriteNanAndInfFlag)) + return false; + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } + + char *buffer = os_->Push(25); + char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); + os_->Pop(static_cast(25 - (end - buffer))); + return true; +} + +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) +template<> +inline bool Writer::ScanWriteUnescapedString(StringStream& is, size_t length) { + if (length < 16) + return RAPIDJSON_LIKELY(is.Tell() < length); + + if (!RAPIDJSON_LIKELY(is.Tell() < length)) + return false; + + const char* p = is.src_; + const char* end = is.head_ + length; + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + const char* endAligned = reinterpret_cast(reinterpret_cast(end) & static_cast(~15)); + if (nextAligned > end) + return true; + + while (p != nextAligned) + if (*p < 0x20 || *p == '\"' || *p == '\\') { + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); + } + else + os_->PutUnsafe(*p++); + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (; p != endAligned; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + SizeType len; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + len = offset; +#else + len = static_cast(__builtin_ffs(r) - 1); +#endif + char* q = reinterpret_cast(os_->PushUnsafe(len)); + for (size_t i = 0; i < len; i++) + q[i] = p[i]; + + p += len; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(os_->PushUnsafe(16)), s); + } + + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); +} +#endif // defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) + +RAPIDJSON_NAMESPACE_END + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/src/Options.cpp b/src/Options.cpp index 66c34447..71c3a180 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -22,7 +22,6 @@ */ -#include #include #include @@ -39,6 +38,9 @@ #include "net/Url.h" #include "Options.h" #include "Platform.h" +#include "rapidjson/document.h" +#include "rapidjson/error/en.h" +#include "rapidjson/filereadstream.h" #include "version.h" @@ -268,6 +270,35 @@ Options::~Options() } +bool Options::getJSON(const char *fileName, rapidjson::Document &doc) +{ + uv_fs_t req; + const int fd = uv_fs_open(uv_default_loop(), &req, fileName, O_RDONLY, 0644, nullptr); + if (fd < 0) { + fprintf(stderr, "unable to open %s: %s\n", fileName, uv_strerror(fd)); + return false; + } + + uv_fs_req_cleanup(&req); + + FILE *fp = fdopen(fd, "rb"); + char buf[8192]; + rapidjson::FileReadStream is(fp, buf, sizeof(buf)); + + doc.ParseStream(is); + + uv_fs_close(uv_default_loop(), &req, fd, nullptr); + uv_fs_req_cleanup(&req); + + if (doc.HasParseError()) { + printf("%s:%d: %s\n", fileName, (int) doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError())); + return false; + } + + return doc.IsObject(); +} + + bool Options::parseArg(int key, const char *arg) { switch (key) { @@ -529,85 +560,53 @@ Url *Options::parseUrl(const char *arg) const void Options::parseConfig(const char *fileName) { - uv_fs_t req; - const int fd = uv_fs_open(uv_default_loop(), &req, fileName, O_RDONLY, 0644, nullptr); - if (fd < 0) { - fprintf(stderr, "unable to open %s: %s\n", fileName, uv_strerror(fd)); - return; - } - - uv_fs_req_cleanup(&req); - - json_error_t err; - json_t *config = json_loadfd(fd, 0, &err); - - uv_fs_close(uv_default_loop(), &req, fd, nullptr); - uv_fs_req_cleanup(&req); - - if (!json_is_object(config)) { - if (config) { - json_decref(config); - return; - } - - if (err.line < 0) { - fprintf(stderr, "%s\n", err.text); - } - else { - fprintf(stderr, "%s:%d: %s\n", fileName, err.line, err.text); - } - + rapidjson::Document doc; + if (!getJSON(fileName, doc)) { return; } for (size_t i = 0; i < ARRAY_SIZE(config_options); i++) { - parseJSON(&config_options[i], config); + parseJSON(&config_options[i], doc); } - json_t *pools = json_object_get(config, "pools"); - if (json_is_array(pools)) { - size_t index; - json_t *value; + const rapidjson::Value &pools = doc["pools"]; + if (pools.IsArray()) { + for (const rapidjson::Value &value : pools.GetArray()) { + if (!value.IsObject()) { + continue; + } - json_array_foreach(pools, index, value) { - if (json_is_object(value)) { - for (size_t i = 0; i < ARRAY_SIZE(pool_options); i++) { - parseJSON(&pool_options[i], value); - } + for (size_t i = 0; i < ARRAY_SIZE(pool_options); i++) { + parseJSON(&pool_options[i], value); } } } - json_t *api = json_object_get(config, "api"); - if (json_is_object(api)) { + const rapidjson::Value &api = doc["api"]; + if (api.IsObject()) { for (size_t i = 0; i < ARRAY_SIZE(api_options); i++) { parseJSON(&api_options[i], api); } } - - json_decref(config); } -void Options::parseJSON(const struct option *option, json_t *object) +void Options::parseJSON(const struct option *option, const rapidjson::Value &object) { - if (!option->name) { + if (!option->name || !object.HasMember(option->name)) { return; } - json_t *val = json_object_get(object, option->name); - if (!val) { - return; - } + const rapidjson::Value &value = object[option->name]; - if (option->has_arg && json_is_string(val)) { - parseArg(option->val, json_string_value(val)); + if (option->has_arg && value.IsString()) { + parseArg(option->val, value.GetString()); } - else if (option->has_arg && json_is_integer(val)) { - parseArg(option->val, json_integer_value(val)); + else if (option->has_arg && value.IsUint64()) { + parseArg(option->val, value.GetUint64()); } - else if (!option->has_arg && json_is_boolean(val)) { - parseBoolean(option->val, json_is_true(val)); + else if (!option->has_arg && value.IsBool()) { + parseBoolean(option->val, value.IsTrue()); } } @@ -652,7 +651,6 @@ void Options::showVersion() "\n"); printf("\nlibuv/%s\n", uv_version_string()); - printf("libjansson/%s\n", JANSSON_VERSION); } diff --git a/src/Options.h b/src/Options.h index 01aab3ea..06b86f38 100644 --- a/src/Options.h +++ b/src/Options.h @@ -25,11 +25,13 @@ #define __OPTIONS_H__ -#include #include #include +#include "rapidjson/fwd.h" + + class Url; struct option; @@ -87,12 +89,13 @@ private: static Options *m_self; + bool getJSON(const char *fileName, rapidjson::Document &doc); bool parseArg(int key, const char *arg); bool parseArg(int key, uint64_t arg); bool parseBoolean(int key, bool enable); Url *parseUrl(const char *arg) const; void parseConfig(const char *fileName); - void parseJSON(const struct option *option, json_t *object); + void parseJSON(const struct option *option, const rapidjson::Value &object); void showUsage(int status) const; void showVersion(void); diff --git a/src/api/Api.cpp b/src/api/Api.cpp index a455290a..729ebccd 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -29,7 +29,6 @@ ApiState *Api::m_state = nullptr; -char Api::m_buf[4096]; uv_mutex_t Api::m_mutex; @@ -48,26 +47,17 @@ void Api::release() } -const char *Api::get(const char *url, size_t *size, int *status) +char *Api::get(const char *url, int *status) { if (!m_state) { - *size = 0; return nullptr; } uv_mutex_lock(&m_mutex); - - const char *buf = m_state->get(url, size); - if (*size) { - memcpy(m_buf, buf, *size); - } - else { - *status = 500; - } - + char *buf = m_state->get(url, status); uv_mutex_unlock(&m_mutex); - return m_buf; + return buf; } diff --git a/src/api/Api.h b/src/api/Api.h index dc5a09e0..72c65c3c 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -39,13 +39,12 @@ public: static bool start(); static void release(); - static const char *get(const char *url, size_t *size, int *status); + static char *get(const char *url, int *status); static void tick(const Hashrate *hashrate); static void tick(const NetworkState &results); private: static ApiState *m_state; - static char m_buf[4096]; static uv_mutex_t m_mutex; }; diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp index e05afb53..3154307d 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiState.cpp @@ -38,6 +38,9 @@ #include "net/Job.h" #include "Options.h" #include "Platform.h" +#include "rapidjson/document.h" +#include "rapidjson/stringbuffer.h" +#include "rapidjson/prettywriter.h" #include "version.h" #include "workers/Hashrate.h" @@ -83,17 +86,18 @@ ApiState::~ApiState() } -const char *ApiState::get(const char *url, size_t *size) const +char *ApiState::get(const char *url, int *status) const { - json_t *reply = json_object(); + rapidjson::Document doc; + doc.SetObject(); - getIdentify(reply); - getMiner(reply); - getHashrate(reply); - getResults(reply); - getConnection(reply); + getIdentify(doc); + getMiner(doc); + getHashrate(doc); + getResults(doc); + getConnection(doc); - return finalize(reply, size); + return finalize(doc); } @@ -118,12 +122,14 @@ void ApiState::tick(const NetworkState &network) } -const char *ApiState::finalize(json_t *reply, size_t *size) const +char *ApiState::finalize(rapidjson::Document &doc) const { - *size = json_dumpb(reply, m_buf, sizeof(m_buf) - 1, JSON_INDENT(4) | JSON_REAL_PRECISION(15)); + rapidjson::StringBuffer buffer(0, 4096); + rapidjson::PrettyWriter writer(buffer); + writer.SetMaxDecimalPlaces(10); + doc.Accept(writer); - json_decref(reply); - return m_buf; + return strdup(buffer.GetString()); } @@ -150,6 +156,8 @@ void ApiState::genId() keccak(input, static_cast(inSize), hash, sizeof(hash)); Job::toHex(hash, 8, m_id); + + delete [] input; break; } } @@ -158,85 +166,95 @@ void ApiState::genId() } -void ApiState::getConnection(json_t *reply) const +void ApiState::getConnection(rapidjson::Document &doc) const { - json_t *connection = json_object(); + auto &allocator = doc.GetAllocator(); - json_object_set(reply, "connection", connection); - json_object_set(connection, "pool", json_string(m_network.pool)); - json_object_set(connection, "uptime", json_integer(m_network.connectionTime())); - json_object_set(connection, "ping", json_integer(m_network.latency())); - json_object_set(connection, "failures", json_integer(m_network.failures)); - json_object_set(connection, "error_log", json_array()); + rapidjson::Value connection(rapidjson::kObjectType); + connection.AddMember("pool", rapidjson::StringRef(m_network.pool), allocator); + connection.AddMember("uptime", m_network.connectionTime(), allocator); + connection.AddMember("ping", m_network.latency(), allocator); + connection.AddMember("failures", m_network.failures, allocator); + connection.AddMember("error_log", rapidjson::Value(rapidjson::kArrayType), allocator); + + doc.AddMember("connection", connection, allocator); } -void ApiState::getHashrate(json_t *reply) const +void ApiState::getHashrate(rapidjson::Document &doc) const { - json_t *hashrate = json_object(); - json_t *threads = json_array(); - json_t *total = json_array(); + auto &allocator = doc.GetAllocator(); - json_object_set(reply, "hashrate", hashrate); - json_object_set(hashrate, "total", total); - json_object_set(hashrate, "highest", json_real(normalize(m_highestHashrate))); - json_object_set(hashrate, "threads", threads); - - for (int i = 0; i < m_threads * 3; i += 3) { - json_t *thread = json_array(); - json_array_append(thread, json_real(normalize(m_hashrate[i]))); - json_array_append(thread, json_real(normalize(m_hashrate[i + 1]))); - json_array_append(thread, json_real(normalize(m_hashrate[i + 2]))); - - json_array_append(threads, thread); - } + rapidjson::Value hashrate(rapidjson::kObjectType); + rapidjson::Value total(rapidjson::kArrayType); + rapidjson::Value threads(rapidjson::kArrayType); for (int i = 0; i < 3; ++i) { - json_array_append(total, json_real(normalize(m_totalHashrate[i]))); + total.PushBack(normalize(m_totalHashrate[i]), allocator); } + + for (int i = 0; i < m_threads * 3; i += 3) { + rapidjson::Value thread(rapidjson::kArrayType); + thread.PushBack(normalize(m_hashrate[i]), allocator); + thread.PushBack(normalize(m_hashrate[i + 1]), allocator); + thread.PushBack(normalize(m_hashrate[i + 2]), allocator); + + threads.PushBack(thread, allocator); + } + + hashrate.AddMember("total", total, allocator); + hashrate.AddMember("highest", normalize(m_highestHashrate), allocator); + hashrate.AddMember("threads", threads, allocator); + doc.AddMember("hashrate", hashrate, allocator); } -void ApiState::getIdentify(json_t *reply) const +void ApiState::getIdentify(rapidjson::Document &doc) const { - json_object_set(reply, "id", json_string(m_id)); - json_object_set(reply, "worker_id", json_string(m_workerId)); + doc.AddMember("id", rapidjson::StringRef(m_id), doc.GetAllocator()); + doc.AddMember("worker_id", rapidjson::StringRef(m_workerId), doc.GetAllocator()); } -void ApiState::getMiner(json_t *reply) const +void ApiState::getMiner(rapidjson::Document &doc) const { - json_t *cpu = json_object(); - json_object_set(reply, "version", json_string(APP_VERSION)); - json_object_set(reply, "kind", json_string(APP_KIND)); - json_object_set(reply, "ua", json_string(Platform::userAgent())); - json_object_set(reply, "cpu", cpu); - json_object_set(reply, "algo", json_string(Options::i()->algoName())); - json_object_set(reply, "hugepages", json_boolean(Mem::isHugepagesEnabled())); - json_object_set(reply, "donate", json_integer(Options::i()->donateLevel())); + auto &allocator = doc.GetAllocator(); - json_object_set(cpu, "brand", json_string(Cpu::brand())); - json_object_set(cpu, "aes", json_boolean(Cpu::hasAES())); - json_object_set(cpu, "x64", json_boolean(Cpu::isX64())); - json_object_set(cpu, "sockets", json_integer(Cpu::sockets())); + rapidjson::Value cpu(rapidjson::kObjectType); + cpu.AddMember("brand", rapidjson::StringRef(Cpu::brand()), allocator); + cpu.AddMember("aes", Cpu::hasAES(), allocator); + cpu.AddMember("x64", Cpu::isX64(), allocator); + cpu.AddMember("sockets", Cpu::sockets(), allocator); + + doc.AddMember("version", APP_VERSION, allocator); + doc.AddMember("kind", APP_KIND, allocator); + doc.AddMember("ua", rapidjson::StringRef(Platform::userAgent()), allocator); + doc.AddMember("cpu", cpu, allocator); + doc.AddMember("algo", rapidjson::StringRef(Options::i()->algoName()), allocator); + doc.AddMember("hugepages", Mem::isHugepagesEnabled(), allocator); + doc.AddMember("donate_level", Options::i()->donateLevel(), allocator); } -void ApiState::getResults(json_t *reply) const +void ApiState::getResults(rapidjson::Document &doc) const { - json_t *results = json_object(); - json_t *best = json_array(); + auto &allocator = doc.GetAllocator(); - json_object_set(reply, "results", results); - json_object_set(results, "diff_current", json_integer(m_network.diff)); - json_object_set(results, "shares_good", json_integer(m_network.accepted)); - json_object_set(results, "shares_total", json_integer(m_network.accepted + m_network.rejected)); - json_object_set(results, "avg_time", json_integer(m_network.avgTime())); - json_object_set(results, "hashes_total", json_integer(m_network.total)); - json_object_set(results, "best", best); - json_object_set(results, "error_log", json_array()); + rapidjson::Value results(rapidjson::kObjectType); + results.AddMember("diff_current", m_network.diff, allocator); + results.AddMember("shares_good", m_network.accepted, allocator); + results.AddMember("shares_total", m_network.accepted + m_network.rejected, allocator); + results.AddMember("avg_time", m_network.avgTime(), allocator); + results.AddMember("hashes_total", m_network.total, allocator); + + rapidjson::Value best(rapidjson::kArrayType); for (size_t i = 0; i < m_network.topDiff.size(); ++i) { - json_array_append(best, json_integer(m_network.topDiff[i])); + best.PushBack(m_network.topDiff[i], allocator); } + + results.AddMember("best", best, allocator); + results.AddMember("error_log", rapidjson::Value(rapidjson::kArrayType), allocator); + + doc.AddMember("results", results, allocator); } diff --git a/src/api/ApiState.h b/src/api/ApiState.h index 14c4c135..72a332aa 100644 --- a/src/api/ApiState.h +++ b/src/api/ApiState.h @@ -26,7 +26,7 @@ #include "api/NetworkState.h" -#include "jansson.h" +#include "rapidjson/fwd.h" class Hashrate; @@ -38,18 +38,18 @@ public: ApiState(); ~ApiState(); - const char *get(const char *url, size_t *size) const; + char *get(const char *url, int *status) const; void tick(const Hashrate *hashrate); void tick(const NetworkState &results); private: - const char *finalize(json_t *reply, size_t *size) const; + char *finalize(rapidjson::Document &doc) const; void genId(); - void getConnection(json_t *reply) const; - void getHashrate(json_t *reply) const; - void getIdentify(json_t *reply) const; - void getMiner(json_t *reply) const; - void getResults(json_t *reply) const; + void getConnection(rapidjson::Document &doc) const; + void getHashrate(rapidjson::Document &doc) const; + void getIdentify(rapidjson::Document &doc) const; + void getMiner(rapidjson::Document &doc) const; + void getResults(rapidjson::Document &doc) const; char m_id[17]; char m_workerId[128]; diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index f6836c92..cb361114 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -110,13 +110,11 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url return done(connection, status, nullptr); } - MHD_Response *rsp = nullptr; - size_t size = 0; - const char *buf = Api::get(url, &size, &status); - - if (size) { - rsp = MHD_create_response_from_buffer(size, (void*) buf, MHD_RESPMEM_PERSISTENT); + char *buf = Api::get(url, &status); + if (buf == nullptr) { + return MHD_NO; } + MHD_Response *rsp = MHD_create_response_from_buffer(strlen(buf), (void*) buf, MHD_RESPMEM_MUST_FREE); return done(connection, status, rsp); } diff --git a/src/log/ConsoleLog.cpp b/src/log/ConsoleLog.cpp index 1ea81a39..ef8516eb 100644 --- a/src/log/ConsoleLog.cpp +++ b/src/log/ConsoleLog.cpp @@ -23,6 +23,7 @@ #include +#include #include #include #include @@ -38,13 +39,16 @@ ConsoleLog::ConsoleLog(bool colors) : - m_colors(colors) + m_colors(colors), + m_stream(nullptr) { if (uv_tty_init(uv_default_loop(), &m_tty, 1, 0) < 0) { return; } uv_tty_set_mode(&m_tty, UV_TTY_MODE_NORMAL); + m_uvBuf.base = m_buf; + m_stream = reinterpret_cast(&m_tty); # ifdef WIN32 HANDLE handle = GetStdHandle(STD_INPUT_HANDLE); @@ -99,21 +103,19 @@ void ConsoleLog::message(int level, const char* fmt, va_list args) } } - char *buf = new char[64 + strlen(fmt) + 2]; - - sprintf(buf, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s\n", - stime.tm_year + 1900, - stime.tm_mon + 1, - stime.tm_mday, - stime.tm_hour, - stime.tm_min, - stime.tm_sec, - m_colors ? color : "", - fmt, - m_colors ? Log::kCL_N : "" + snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s\n", + stime.tm_year + 1900, + stime.tm_mon + 1, + stime.tm_mday, + stime.tm_hour, + stime.tm_min, + stime.tm_sec, + m_colors ? color : "", + fmt, + m_colors ? Log::kCL_N : "" ); - print(buf, args); + print(args); } @@ -123,17 +125,15 @@ void ConsoleLog::text(const char* fmt, va_list args) return; } - char *buf = new char[64 + strlen(fmt) + 2]; + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s\n", fmt, m_colors ? Log::kCL_N : ""); - sprintf(buf, "%s%s\n", fmt, m_colors ? Log::kCL_N : ""); - - print(buf, args); + print(args); } bool ConsoleLog::isWritable() const { - if (uv_is_writable(reinterpret_cast(&m_tty)) != 1) { + if (!m_stream || uv_is_writable(m_stream) != 1) { return false; } @@ -142,20 +142,12 @@ bool ConsoleLog::isWritable() const } -void ConsoleLog::print(char *fmt, va_list args) +void ConsoleLog::print(va_list args) { - vsnprintf(m_buf, sizeof(m_buf) - 1, fmt, args); - delete [] fmt; + m_uvBuf.len = vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args); + if (m_uvBuf.len <= 0) { + return; + } - uv_buf_t buf; - buf.base = strdup(m_buf); - buf.len = strlen(buf.base); - - uv_write_t *req = new uv_write_t; - req->data = buf.base; - - uv_write(req, reinterpret_cast(&m_tty), &buf, 1, [](uv_write_t *req, int status) { - free(req->data); - delete req; - }); + uv_try_write(m_stream, &m_uvBuf, 1); } diff --git a/src/log/ConsoleLog.h b/src/log/ConsoleLog.h index e3d06e60..a04a27c5 100644 --- a/src/log/ConsoleLog.h +++ b/src/log/ConsoleLog.h @@ -41,10 +41,13 @@ public: private: bool isWritable() const; - void print(char *fmt, va_list args); + void print(va_list args); bool m_colors; char m_buf[512]; + char m_fmt[256]; + uv_buf_t m_uvBuf; + uv_stream_t *m_stream; uv_tty_t m_tty; }; diff --git a/src/log/FileLog.cpp b/src/log/FileLog.cpp index 9a8711a4..5eeb252c 100644 --- a/src/log/FileLog.cpp +++ b/src/log/FileLog.cpp @@ -23,6 +23,7 @@ #include +#include #include #include #include @@ -54,7 +55,7 @@ void FileLog::message(int level, const char* fmt, va_list args) localtime_r(&now, &stime); # endif - char *buf = static_cast(malloc(512)); + char *buf = new char[512]; int size = snprintf(buf, 23, "[%d-%02d-%02d %02d:%02d:%02d] ", stime.tm_year + 1900, stime.tm_mon + 1, @@ -79,17 +80,17 @@ void FileLog::text(const char* fmt, va_list args) void FileLog::onWrite(uv_fs_t *req) { - free(req->data); + delete [] static_cast(req->data); uv_fs_req_cleanup(req); - free(req); + delete req; } void FileLog::write(char *data, size_t size) { uv_buf_t buf = uv_buf_init(data, (unsigned int) size); - uv_fs_t *req = static_cast(malloc(sizeof(uv_fs_t))); + uv_fs_t *req = new uv_fs_t; req->data = buf.base; uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, 0, FileLog::onWrite); diff --git a/src/log/Log.cpp b/src/log/Log.cpp index 1efd486a..3e5d5671 100644 --- a/src/log/Log.cpp +++ b/src/log/Log.cpp @@ -23,6 +23,7 @@ #include +#include #include #include #include diff --git a/src/log/Log.h b/src/log/Log.h index e193125f..fd944d80 100644 --- a/src/log/Log.h +++ b/src/log/Log.h @@ -57,6 +57,7 @@ public: static inline Log* i() { return m_self; } static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); } static inline void init() { if (!m_self) { m_self = new Log();} } + static inline void release() { delete m_self; } void message(Level level, const char* fmt, ...); void text(const char* fmt, ...); diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 41bfda47..dfe202a4 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -23,13 +23,19 @@ #include #include +#include #include #include -#include "log/Log.h" + #include "interfaces/IClientListener.h" +#include "log/Log.h" #include "net/Client.h" #include "net/Url.h" +#include "rapidjson/document.h" +#include "rapidjson/error/en.h" +#include "rapidjson/stringbuffer.h" +#include "rapidjson/writer.h" #ifdef XMRIG_PROXY_PROJECT @@ -69,8 +75,8 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_hints.ai_socktype = SOCK_STREAM; m_hints.ai_protocol = IPPROTO_TCP; - m_recvBuf.base = static_cast(malloc(kRecvBufSize)); - m_recvBuf.len = kRecvBufSize; + m_recvBuf.base = m_buf; + m_recvBuf.len = sizeof(m_buf); # ifndef XMRIG_PROXY_PROJECT m_keepAliveTimer.data = this; @@ -81,36 +87,7 @@ Client::Client(int id, const char *agent, IClientListener *listener) : Client::~Client() { - free(m_recvBuf.base); - free(m_socket); -} - - -/** - * @brief Send raw data to server. - * - * @param data - */ -int64_t Client::send(char *data, size_t size) -{ - LOG_DEBUG("[%s:%u] send (%d bytes): \"%s\"", m_url.host(), m_url.port(), size ? size : strlen(data), data); - if (state() != ConnectedState || !uv_is_writable(m_stream)) { - LOG_DEBUG_ERR("[%s:%u] send failed, invalid state: %d", m_url.host(), m_url.port(), m_state); - return -1; - } - - uv_buf_t buf = uv_buf_init(data, (unsigned int) (size ? size : strlen(data))); - - uv_write_t *req = new uv_write_t; - req->data = buf.base; - - uv_write(req, m_stream, &buf, 1, [](uv_write_t *req, int status) { - free(req->data); - delete req; - }); - - m_expire = uv_now(uv_default_loop()) + kResponseTimeout; - return m_sequence++; + delete m_socket; } @@ -175,8 +152,6 @@ void Client::tick(uint64_t now) int64_t Client::submit(const JobResult &result) { - char *req = static_cast(malloc(345)); - # ifdef XMRIG_PROXY_PROJECT const char *nonce = result.nonce; const char *data = result.result; @@ -191,11 +166,11 @@ int64_t Client::submit(const JobResult &result) data[64] = '\0'; # endif - snprintf(req, 345, "{\"id\":%" PRIu64 ",\"jsonrpc\":\"2.0\",\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"}}\n", - m_sequence, m_rpcId, result.jobId, nonce, data); + const size_t size = snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRIu64 ",\"jsonrpc\":\"2.0\",\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"}}\n", + m_sequence, m_rpcId, result.jobId.data(), nonce, data); m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff()); - return send(req); + return send(size); } @@ -221,25 +196,25 @@ bool Client::isCriticalError(const char *message) } -bool Client::parseJob(const json_t *params, int *code) +bool Client::parseJob(const rapidjson::Value ¶ms, int *code) { - if (!json_is_object(params)) { + if (!params.IsObject()) { *code = 2; return false; } Job job(m_id, m_url.isNicehash()); - if (!job.setId(json_string_value(json_object_get(params, "job_id")))) { + if (!job.setId(params["job_id"].GetString())) { *code = 3; return false; } - if (!job.setBlob(json_string_value(json_object_get(params, "blob")))) { + if (!job.setBlob(params["blob"].GetString())) { *code = 4; return false; } - if (!job.setTarget(json_string_value(json_object_get(params, "target")))) { + if (!job.setTarget(params["target"].GetString())) { *code = 5; return false; } @@ -254,9 +229,9 @@ bool Client::parseJob(const json_t *params, int *code) } -bool Client::parseLogin(const json_t *result, int *code) +bool Client::parseLogin(const rapidjson::Value &result, int *code) { - const char *id = json_string_value(json_object_get(result, "id")); + const char *id = result["id"].GetString(); if (!id || strlen(id) >= sizeof(m_rpcId)) { *code = 1; return false; @@ -265,7 +240,7 @@ bool Client::parseLogin(const json_t *result, int *code) memset(m_rpcId, 0, sizeof(m_rpcId)); memcpy(m_rpcId, id, strlen(id)); - return parseJob(json_object_get(result, "job"), code); + return parseJob(result["job"], code); } @@ -292,6 +267,26 @@ int Client::resolve(const char *host) } +int64_t Client::send(size_t size) +{ + LOG_DEBUG("[%s:%u] send (%d bytes): \"%s\"", m_url.host(), m_url.port(), size, m_sendBuf); + if (state() != ConnectedState || !uv_is_writable(m_stream)) { + LOG_DEBUG_ERR("[%s:%u] send failed, invalid state: %d", m_url.host(), m_url.port(), m_state); + return -1; + } + + uv_buf_t buf = uv_buf_init(m_sendBuf, (unsigned int) size); + + if (uv_try_write(m_stream, &buf, 1) < 0) { + close(); + return -1; + } + + m_expire = uv_now(uv_default_loop()) + kResponseTimeout; + return m_sequence++; +} + + void Client::close() { if (m_state == UnconnectedState || m_state == ClosingState || !m_socket) { @@ -311,12 +306,12 @@ void Client::connect(struct sockaddr *addr) setState(ConnectingState); reinterpret_cast(addr)->sin_port = htons(m_url.port()); - free(m_socket); + delete m_socket; - uv_connect_t *req = (uv_connect_t*) malloc(sizeof(uv_connect_t)); + uv_connect_t *req = new uv_connect_t; req->data = this; - m_socket = static_cast(malloc(sizeof(uv_tcp_t))); + m_socket = new uv_tcp_t; m_socket->data = this; uv_tcp_init(uv_default_loop(), m_socket); @@ -334,26 +329,36 @@ void Client::login() { m_results.clear(); - json_t *req = json_object(); - json_object_set(req, "id", json_integer(1)); - json_object_set(req, "jsonrpc", json_string("2.0")); - json_object_set(req, "method", json_string("login")); + rapidjson::Document doc; + doc.SetObject(); - json_t *params = json_object(); - json_object_set(params, "login", json_string(m_url.user())); - json_object_set(params, "pass", json_string(m_url.password())); - json_object_set(params, "agent", json_string(m_agent)); + auto &allocator = doc.GetAllocator(); - json_object_set(req, "params", params); + doc.AddMember("id", 1, allocator); + doc.AddMember("jsonrpc", "2.0", allocator); + doc.AddMember("method", "login", allocator); - char *buf = json_dumps(req, JSON_COMPACT); - const size_t size = strlen(buf); + rapidjson::Value params(rapidjson::kObjectType); + params.AddMember("login", rapidjson::StringRef(m_url.user()), allocator); + params.AddMember("pass", rapidjson::StringRef(m_url.password()), allocator); + params.AddMember("agent", rapidjson::StringRef(m_agent), allocator); - buf[size] = '\n'; + doc.AddMember("params", params, allocator); - json_decref(req); + rapidjson::StringBuffer buffer(0, 512); + rapidjson::Writer writer(buffer); + doc.Accept(writer); - send(buf, size + 1); + const size_t size = buffer.GetSize(); + if (size > (sizeof(m_buf) - 2)) { + return; + } + + memcpy(m_sendBuf, buffer.GetString(), size); + m_sendBuf[size] = '\n'; + m_sendBuf[size + 1] = '\0'; + + send(size + 1); } @@ -365,33 +370,34 @@ void Client::parse(char *line, size_t len) LOG_DEBUG("[%s:%u] received (%d bytes): \"%s\"", m_url.host(), m_url.port(), len, line); - json_error_t err; - json_t *val = json_loads(line, 0, &err); - - if (!val) { + rapidjson::Document doc; + if (doc.ParseInsitu(line).HasParseError()) { if (!m_quiet) { - LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_url.host(), m_url.port(), err.text); + LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_url.host(), m_url.port(), rapidjson::GetParseError_En(doc.GetParseError())); } + return; } - const json_t *id = json_object_get(val, "id"); - if (json_is_integer(id)) { - parseResponse(json_integer_value(id), json_object_get(val, "result"), json_object_get(val, "error")); - } - else { - parseNotification(json_string_value(json_object_get(val, "method")), json_object_get(val, "params"), json_object_get(val, "error")); + if (!doc.IsObject()) { + return; } - json_decref(val); + const rapidjson::Value &id = doc["id"]; + if (id.IsInt64()) { + parseResponse(id.GetInt64(), doc["result"], doc["error"]); + } + else { + parseNotification(doc["method"].GetString(), doc["params"], doc["error"]); + } } -void Client::parseNotification(const char *method, const json_t *params, const json_t *error) +void Client::parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error) { - if (json_is_object(error)) { + if (error.IsObject()) { if (!m_quiet) { - LOG_ERR("[%s:%u] error: \"%s\", code: %" PRId64, m_url.host(), m_url.port(), json_string_value(json_object_get(error, "message")), json_integer_value(json_object_get(error, "code"))); + LOG_ERR("[%s:%u] error: \"%s\", code: %d", m_url.host(), m_url.port(), error["message"].GetString(), error["code"].GetInt()); } return; } @@ -413,10 +419,10 @@ void Client::parseNotification(const char *method, const json_t *params, const j } -void Client::parseResponse(int64_t id, const json_t *result, const json_t *error) +void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error) { - if (json_is_object(error)) { - const char *message = json_string_value(json_object_get(error, "message")); + if (error.IsObject()) { + const char *message = error["message"].GetString(); auto it = m_results.find(id); if (it != m_results.end()) { @@ -425,7 +431,7 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error m_results.erase(it); } else if (!m_quiet) { - LOG_ERR("[%s:%u] error: \"%s\", code: %" PRId64, m_url.host(), m_url.port(), message, json_integer_value(json_object_get(error, "code"))); + LOG_ERR("[%s:%u] error: \"%s\", code: %d", m_url.host(), m_url.port(), message, error["code"].GetInt()); } if (id == 1 || isCriticalError(message)) { @@ -435,7 +441,7 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error return; } - if (!json_is_object(result)) { + if (!result.IsObject()) { return; } @@ -466,10 +472,7 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error void Client::ping() { - char *req = static_cast(malloc(160)); - snprintf(req, 160, "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, m_rpcId); - - send(req); + send(snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, m_rpcId)); } @@ -533,7 +536,7 @@ void Client::onClose(uv_handle_t *handle) { auto client = getClient(handle->data); - free(client->m_socket); + delete client->m_socket; client->m_stream = nullptr; client->m_socket = nullptr; @@ -551,7 +554,7 @@ void Client::onConnect(uv_connect_t *req, int status) LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); } - free(req); + delete req; client->close(); return; } @@ -561,7 +564,7 @@ void Client::onConnect(uv_connect_t *req, int status) client->setState(ConnectedState); uv_read_start(client->m_stream, Client::onAllocBuffer, Client::onRead); - free(req); + delete req; client->login(); } @@ -578,14 +581,14 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) return client->close();; } - if ((size_t) nread > (kRecvBufSize - 8 - client->m_recvBufPos)) { + if ((size_t) nread > (sizeof(m_buf) - 8 - client->m_recvBufPos)) { return client->close();; } client->m_recvBufPos += nread; char* end; - char* start = client->m_recvBuf.base; + char* start = buf->base; size_t remaining = client->m_recvBufPos; while ((end = static_cast(memchr(start, '\n', remaining))) != nullptr) { @@ -602,11 +605,11 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) return; } - if (start == client->m_recvBuf.base) { + if (start == buf->base) { return; } - memcpy(client->m_recvBuf.base, start, remaining); + memcpy(buf->base, start, remaining); client->m_recvBufPos = remaining; } diff --git a/src/net/Client.h b/src/net/Client.h index f554e341..699f4903 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -25,7 +25,6 @@ #define __CLIENT_H__ -#include #include #include @@ -33,6 +32,7 @@ #include "net/Job.h" #include "net/SubmitResult.h" #include "net/Url.h" +#include "rapidjson/fwd.h" class IClientListener; @@ -56,7 +56,6 @@ public: Client(int id, const char *agent, IClientListener *listener); ~Client(); - int64_t send(char *data, size_t size = 0); int64_t submit(const JobResult &result); void connect(); void connect(const Url *url); @@ -75,18 +74,17 @@ public: inline void setRetryPause(int ms) { m_retryPause = ms; } private: - constexpr static size_t kRecvBufSize = 4096; - bool isCriticalError(const char *message); - bool parseJob(const json_t *params, int *code); - bool parseLogin(const json_t *result, int *code); + bool parseJob(const rapidjson::Value ¶ms, int *code); + bool parseLogin(const rapidjson::Value &result, int *code); int resolve(const char *host); + int64_t send(size_t size); void close(); void connect(struct sockaddr *addr); void login(); void parse(char *line, size_t len); - void parseNotification(const char *method, const json_t *params, const json_t *error); - void parseResponse(int64_t id, const json_t *result, const json_t *error); + void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error); + void parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error); void ping(); void reconnect(); void setState(SocketState state); @@ -102,8 +100,10 @@ private: addrinfo m_hints; bool m_quiet; + char m_buf[2048]; char m_ip[17]; char m_rpcId[64]; + char m_sendBuf[768]; const char *m_agent; IClientListener *m_listener; int m_id; diff --git a/src/net/Job.cpp b/src/net/Job.cpp index bce65e62..7295d943 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -25,7 +25,6 @@ #include -#include "log/Log.h" #include "net/Job.h" @@ -59,6 +58,7 @@ static inline char hf_bin2hex(unsigned char c) Job::Job(int poolId, bool nicehash) : m_nicehash(nicehash), m_poolId(poolId), + m_threadId(-1), m_size(0), m_diff(0), m_target(0) @@ -66,6 +66,11 @@ Job::Job(int poolId, bool nicehash) : } +Job::~Job() +{ +} + + bool Job::setBlob(const char *blob) { if (!blob) { @@ -99,18 +104,6 @@ bool Job::setBlob(const char *blob) } -bool Job::setId(const char *id) -{ - if (!id || strlen(id) >= sizeof(m_id)) { - return false; - } - - memset(m_id, 0, sizeof(m_id)); - memcpy(m_id, id, strlen(id)); - return true; -} - - bool Job::setTarget(const char *target) { if (!target) { @@ -178,5 +171,5 @@ void Job::toHex(const unsigned char* in, unsigned int len, char* out) bool Job::operator==(const Job &other) const { - return memcmp(m_id, other.m_id, sizeof(m_id)) == 0; + return m_id == other.m_id && memcmp(m_blob, other.m_blob, sizeof(m_blob) == 0); } diff --git a/src/net/Job.h b/src/net/Job.h index 86160584..1ba2d25d 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -25,31 +25,37 @@ #define __JOB_H__ +#include #include #include "align.h" +#include "net/JobId.h" class Job { public: Job(int poolId = -2, bool nicehash = false); + ~Job(); + bool setBlob(const char *blob); - bool setId(const char *id); bool setTarget(const char *target); inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } - inline const char *id() const { return m_id; } + inline bool setId(const char *id) { return m_id.setId(id); } + inline const JobId &id() const { return m_id; } inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } inline const uint8_t *blob() const { return m_blob; } inline int poolId() const { return m_poolId; } + inline int threadId() const { return m_threadId; } inline size_t size() const { return m_size; } inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } inline uint32_t diff() const { return (uint32_t) m_diff; } inline uint64_t target() const { return m_target; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } + inline void setThreadId(int threadId) { m_threadId = threadId; } # ifdef XMRIG_PROXY_PROJECT inline char *rawBlob() { return m_rawBlob; } @@ -66,11 +72,12 @@ public: private: bool m_nicehash; int m_poolId; - VAR_ALIGN(16, char m_id[64]); - VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. + int m_threadId; + JobId m_id; size_t m_size; uint64_t m_diff; uint64_t m_target; + VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. # ifdef XMRIG_PROXY_PROJECT VAR_ALIGN(16, char m_rawBlob[169]); diff --git a/src/net/JobId.h b/src/net/JobId.h new file mode 100644 index 00000000..e68b3953 --- /dev/null +++ b/src/net/JobId.h @@ -0,0 +1,83 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __JOBID_H__ +#define __JOBID_H__ + + +#include + + +class JobId +{ +public: + inline JobId() + { + memset(m_data, 0, sizeof(m_data)); + } + + + inline JobId(const char *id, size_t sizeFix = 0) + { + setId(id, sizeFix); + } + + + inline bool operator==(const JobId &other) const + { + return memcmp(m_data, other.m_data, sizeof(m_data)) == 0; + } + + + inline bool operator!=(const JobId &other) const + { + return memcmp(m_data, other.m_data, sizeof(m_data)) != 0; + } + + + inline bool setId(const char *id, size_t sizeFix = 0) + { + memset(m_data, 0, sizeof(m_data)); + if (!id) { + return false; + } + + const size_t size = strlen(id); + if (size < 4 || size >= sizeof(m_data)) { + return false; + } + + memcpy(m_data, id, size - sizeFix); + return true; + } + + + inline const char *data() const { return m_data; } + inline bool isValid() const { return *m_data != '\0'; } + + +private: + char m_data[64]; +}; + +#endif /* __JOBID_H__ */ diff --git a/src/net/JobResult.h b/src/net/JobResult.h index 3f69992a..520022a7 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -36,16 +36,19 @@ class JobResult { public: inline JobResult() : poolId(0), diff(0), nonce(0) {} - inline JobResult(int poolId, const char *jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : poolId(poolId), diff(diff), nonce(nonce) + inline JobResult(int poolId, const JobId &jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : + poolId(poolId), + jobId(jobId), + diff(diff), + nonce(nonce) { - memcpy(this->jobId, jobId, sizeof(this->jobId)); memcpy(this->result, result, sizeof(this->result)); } inline JobResult(const Job &job) : poolId(0), diff(0), nonce(0) { - memcpy(jobId, job.id(), sizeof(jobId)); + jobId = job.id(); poolId = job.poolId(); diff = job.diff(); nonce = *job.nonce(); @@ -53,7 +56,7 @@ public: inline JobResult &operator=(const Job &job) { - memcpy(jobId, job.id(), sizeof(jobId)); + jobId = job.id(); poolId = job.poolId(); diff = job.diff(); @@ -67,8 +70,8 @@ public: } - char jobId[64]; int poolId; + JobId jobId; uint32_t diff; uint32_t nonce; uint8_t result[32]; diff --git a/src/net/SubmitResult.h b/src/net/SubmitResult.h index 63f5e883..8eddef89 100644 --- a/src/net/SubmitResult.h +++ b/src/net/SubmitResult.h @@ -40,6 +40,8 @@ public: uint32_t diff; uint64_t actualDiff; uint64_t elapsed; + +private: uint64_t start; }; diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 794fa289..46d6f366 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -105,7 +105,7 @@ void DoubleWorker::start() bool DoubleWorker::resume(const Job &job) { - if (m_state->job.poolId() == -1 && job.poolId() >= 0 && memcmp(job.id(), m_pausedState->job.id(), 64) == 0) { + if (m_state->job.poolId() == -1 && job.poolId() >= 0 && job.id() == m_pausedState->job.id()) { *m_state = *m_pausedState; return true; } diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 34045f74..ecb566f8 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -74,7 +74,7 @@ void SingleWorker::start() bool SingleWorker::resume(const Job &job) { - if (m_job.poolId() == -1 && job.poolId() >= 0 && memcmp(job.id(), m_paused.id(), 64) == 0) { + if (m_job.poolId() == -1 && job.poolId() >= 0 && job.id() == m_paused.id()) { m_job = m_paused; m_result = m_job; m_result.nonce = *m_job.nonce(); From 3ead4eba89fdb197245e0fffc878fab50b14c02a Mon Sep 17 00:00:00 2001 From: xmrig Date: Fri, 6 Oct 2017 17:17:56 +0300 Subject: [PATCH 021/389] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eee4cf70..a218f32d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v2.4.0 + - Added [HTTP API](https://github.com/xmrig/xmrig/wiki/API). + - libjansson replaced to rapidjson. + # v2.3.1 - [#68](https://github.com/xmrig/xmrig/issues/68) Fixed compatibility with Docker containers, was nothing print on console. From 08f6c222ca343076d6c55d9cd90c654dbbd52afe Mon Sep 17 00:00:00 2001 From: xmrig Date: Fri, 6 Oct 2017 17:25:23 +0300 Subject: [PATCH 022/389] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a218f32d..ab3995d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # v2.4.0 - Added [HTTP API](https://github.com/xmrig/xmrig/wiki/API). - libjansson replaced to rapidjson. + - [#101](https://github.com/xmrig/xmrig/issues/101) Fixed MSVC 2017 (15.3) compile time version detection. + - [#108](https://github.com/xmrig/xmrig/issues/108) Silently ignore invalid values for `donate-level` option. + - [#111](https://github.com/xmrig/xmrig/issues/111) Fixed build without AEON support. # v2.3.1 - [#68](https://github.com/xmrig/xmrig/issues/68) Fixed compatibility with Docker containers, was nothing print on console. From 914fdd5f0a344996f011ab9a8da213a5268822b6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 6 Oct 2017 19:10:08 +0300 Subject: [PATCH 023/389] #97 Ignore keepalive option with minergate and nicehash.com --- src/Options.cpp | 4 ++++ src/api/ApiState.cpp | 2 +- src/net/Url.cpp | 24 ++++++++++++++++++------ src/net/Url.h | 3 ++- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Options.cpp b/src/Options.cpp index 71c3a180..bcc830d0 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -261,6 +261,10 @@ Options::Options(int argc, char **argv) : } } + for (Url *url : m_pools) { + url->applyExceptions(); + } + m_ready = true; } diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp index 3154307d..bade355a 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiState.cpp @@ -57,7 +57,7 @@ static inline double normalize(double d) return 0.0; } - return std::floor(d * 10.0) / 10.0; + return std::floor(d * 100.0) / 100.0; } diff --git a/src/net/Url.cpp b/src/net/Url.cpp index a0024d26..dcbe82af 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -24,6 +24,7 @@ #include #include +#include #include "net/Url.h" @@ -87,12 +88,6 @@ Url::~Url() } -bool Url::isNicehash() const -{ - return isValid() && (m_nicehash || strstr(m_host, ".nicehash.com")); -} - - bool Url::parse(const char *url) { const char *p = strstr(url, "://"); @@ -144,6 +139,23 @@ bool Url::setUserpass(const char *userpass) } +void Url::applyExceptions() +{ + if (!isValid()) { + return; + } + + if (strstr(m_host, ".nicehash.com")) { + m_keepAlive = false; + m_nicehash = true; + } + + if (strstr(m_host, ".minergate.com")) { + m_keepAlive = false; + } +} + + void Url::setPassword(const char *password) { if (!password) { diff --git a/src/net/Url.h b/src/net/Url.h index 43197195..a1982300 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -41,6 +41,7 @@ public: ~Url(); inline bool isKeepAlive() const { return m_keepAlive; } + inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_host && m_port > 0; } inline const char *host() const { return m_host; } inline const char *password() const { return m_password ? m_password : kDefaultPassword; } @@ -49,9 +50,9 @@ public: inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } - bool isNicehash() const; bool parse(const char *url); bool setUserpass(const char *userpass); + void applyExceptions(); void setPassword(const char *password); void setUser(const char *user); From 04d3dd6df112e8ae8f8b770ae351843ba2c51afd Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 6 Oct 2017 19:34:06 +0300 Subject: [PATCH 024/389] #130 Fixed compatibility with CMake older than 3.1.0. --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1705cc00..fde1b941 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -135,6 +135,7 @@ endif() add_definitions(/D__STDC_FORMAT_MACROS) add_definitions(/DUNICODE) +add_definitions(/DRAPIDJSON_SSE2) #add_definitions(/DAPP_DEBUG) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") @@ -168,6 +169,10 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU) add_definitions(/D_GNU_SOURCE) + if (${CMAKE_VERSION} VERSION_LESS "3.1.0") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + endif() + #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -gdwarf-2") elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC) From b43d986537642b3ffda737456029800fc5de00b3 Mon Sep 17 00:00:00 2001 From: xmrig Date: Fri, 6 Oct 2017 19:37:42 +0300 Subject: [PATCH 025/389] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab3995d4..057bf297 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # v2.4.0 - Added [HTTP API](https://github.com/xmrig/xmrig/wiki/API). - libjansson replaced to rapidjson. + - [#98](https://github.com/xmrig/xmrig/issues/98) Ignore `keepalive` option with minergate.com and nicehash.com. - [#101](https://github.com/xmrig/xmrig/issues/101) Fixed MSVC 2017 (15.3) compile time version detection. - [#108](https://github.com/xmrig/xmrig/issues/108) Silently ignore invalid values for `donate-level` option. - [#111](https://github.com/xmrig/xmrig/issues/111) Fixed build without AEON support. From 87296166985dae4d8833787e6720e8a90a1a627e Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 6 Oct 2017 23:44:25 +0300 Subject: [PATCH 026/389] Added special option -t all --- src/Options.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Options.cpp b/src/Options.cpp index bcc830d0..4dc53e61 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -363,7 +363,6 @@ bool Options::parseArg(int key, const char *arg) case 'r': /* --retries */ case 'R': /* --retry-pause */ - case 't': /* --threads */ case 'v': /* --av */ case 1003: /* --donate-level */ case 1004: /* --max-cpu-usage */ @@ -383,6 +382,14 @@ bool Options::parseArg(int key, const char *arg) case 1009: /* --no-huge-pages */ return parseBoolean(key, false); + case 't': /* --threads */ + if (strncmp(arg, "all", 3) == 0) { + m_threads = Cpu::threads(); + return true; + } + + return parseArg(key, strtol(arg, nullptr, 10)); + case 'V': /* --version */ showVersion(); return false; From fc810dc87b663acd4a870e8adef55c33b6c2e961 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 7 Oct 2017 00:06:58 +0300 Subject: [PATCH 027/389] Fix for msvc --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index fde1b941..37a00912 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -181,6 +181,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox /Ot /Oi /MT /GL") add_definitions(/D_CRT_SECURE_NO_WARNINGS) add_definitions(/D_CRT_NONSTDC_NO_WARNINGS) + add_definitions(/DNOMINMAX) elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang) From 1123c20da0fab7c08be877af4abf85ea63076f60 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 7 Oct 2017 00:49:38 +0300 Subject: [PATCH 028/389] Add comments to config file. --- src/Options.cpp | 55 ++++++++++++++++++++++++++----------------------- src/config.json | 46 ++++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/Options.cpp b/src/Options.cpp index 4dc53e61..b1197223 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -55,36 +55,39 @@ Options *Options::m_self = nullptr; static char const usage[] = "\ Usage: " APP_ID " [OPTIONS]\n\ Options:\n\ - -a, --algo=ALGO cryptonight (default) or cryptonight-lite\n\ - -o, --url=URL URL of mining server\n\ - -O, --userpass=U:P username:password pair for mining server\n\ - -u, --user=USERNAME username for mining server\n\ - -p, --pass=PASSWORD password for mining server\n\ - -t, --threads=N number of miner threads\n\ - -v, --av=N algorithm variation, 0 auto select\n\ - -k, --keepalive send keepalived for prevent timeout (need pool support)\n\ - -r, --retries=N number of times to retry before switch to backup server (default: 5)\n\ - -R, --retry-pause=N time to pause between retries (default: 5)\n\ - --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1\n\ - --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ - --no-huge-pages disable huge pages support\n\ - --no-color disable colored output\n\ - --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ - --user-agent set custom user-agent string for pool\n\ - -B, --background run the miner in the background\n\ - -c, --config=FILE load a JSON-format configuration file\n\ - -l, --log-file=FILE log all output to a file\n" + -a, --algo=ALGO cryptonight (default) or cryptonight-lite\n\ + -o, --url=URL URL of mining server\n\ + -O, --userpass=U:P username:password pair for mining server\n\ + -u, --user=USERNAME username for mining server\n\ + -p, --pass=PASSWORD password for mining server\n\ + -t, --threads=N number of miner threads\n\ + -v, --av=N algorithm variation, 0 auto select\n\ + -k, --keepalive send keepalived for prevent timeout (need pool support)\n\ + -r, --retries=N number of times to retry before switch to backup server (default: 5)\n\ + -R, --retry-pause=N time to pause between retries (default: 5)\n\ + --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1\n\ + --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ + --no-huge-pages disable huge pages support\n\ + --no-color disable colored output\n\ + --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ + --user-agent set custom user-agent string for pool\n\ + -B, --background run the miner in the background\n\ + -c, --config=FILE load a JSON-format configuration file\n\ + -l, --log-file=FILE log all output to a file\n" # ifdef HAVE_SYSLOG_H "\ - -S, --syslog use system log for output messages\n" + -S, --syslog use system log for output messages\n" # endif "\ - --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75)\n\ - --safe safe adjust threads and av settings for current CPU\n\ - --nicehash enable nicehash support\n\ - --print-time=N print hashrate report every N seconds\n\ - -h, --help display this help and exit\n\ - -V, --version output version information and exit\n\ + --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75)\n\ + --safe safe adjust threads and av settings for current CPU\n\ + --nicehash enable nicehash/xmrig-proxy support\n\ + --print-time=N print hashrate report every N seconds\n\ + --api-port=N port for the miner API\n\ + --api-access-token=T access token for API\n\ + --api-worker-id=ID custom worker-id for API\n\ + -h, --help display this help and exit\n\ + -V, --version output version information and exit\n\ "; diff --git a/src/config.json b/src/config.json index 5a727e48..7813d4b9 100644 --- a/src/config.json +++ b/src/config.json @@ -1,31 +1,31 @@ { - "algo": "cryptonight", - "av": 0, - "background": false, - "colors": true, - "cpu-affinity": null, - "cpu-priority": null, - "donate-level": 5, - "log-file": null, - "max-cpu-usage": 75, - "print-time": 60, - "retries": 5, - "retry-pause": 5, - "safe": false, - "syslog": false, - "threads": null, + "algo": "cryptonight", // cryptonight (default) or cryptonight-lite + "av": 0, // algorithm variation, 0 auto select + "background": false, // true to run the miner in the background + "colors": true, // false to disable colored output + "cpu-affinity": null, // set process affinity to CPU core(s), mask "0x3" for cores 0 and 1 + "cpu-priority": null, // set process priority (0 idle, 2 normal to 5 highest) + "donate-level": 5, // donate level, mininum 1% + "log-file": null, // log all output to a file, example: "c:/some/path/xmrig.log" + "max-cpu-usage": 75, // maximum CPU usage for automatic mode, usually limiting factor is CPU cache not this option. + "print-time": 60, // print hashrate report every N seconds + "retries": 5, // number of times to retry before switch to backup server + "retry-pause": 5, // time to pause between retries + "safe": false, // true to safe adjust threads and av settings for current CPU + "syslog": false, // use system log for output messages + "threads": null, // number of miner threads "pools": [ { - "url": "pool.minemonero.pro:5555", - "user": "", - "pass": "x", - "keepalive": true, - "nicehash": false + "url": "pool.minemonero.pro:5555", // URL of mining server + "user": "", // username for mining server + "pass": "x", // password for mining server + "keepalive": true, // send keepalived for prevent timeout (need pool support) + "nicehash": false // enable nicehash/xmrig-proxy support } ], "api": { - "port": 0, - "access-token": null, - "worker-id": null + "port": 0, // port for the miner API https://github.com/xmrig/xmrig/wiki/API + "access-token": null, // access token for API + "worker-id": null // custom worker-id for API } } \ No newline at end of file From 14cd81a7a1c6e69bc4075c6c30213fed86c6563d Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 7 Oct 2017 00:56:32 +0300 Subject: [PATCH 029/389] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 057bf297..090b4363 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # v2.4.0 - Added [HTTP API](https://github.com/xmrig/xmrig/wiki/API). + - Added comments support in config file. - libjansson replaced to rapidjson. - [#98](https://github.com/xmrig/xmrig/issues/98) Ignore `keepalive` option with minergate.com and nicehash.com. - [#101](https://github.com/xmrig/xmrig/issues/101) Fixed MSVC 2017 (15.3) compile time version detection. From 7d4bdfc91341f398244063a6c9cd614f8fac702f Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 7 Oct 2017 16:55:58 +0300 Subject: [PATCH 030/389] v2.4.0 --- src/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index 551bfbb9..7f80dea0 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.4.0-dev" +#define APP_VERSION "2.4.0" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com" From f02b98efe7b95a1ecf8e961d5b5e1836d945b647 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 7 Oct 2017 18:58:04 +0300 Subject: [PATCH 031/389] Fix clang warnings and job comparison bug. --- CMakeLists.txt | 2 +- src/api/ApiState.h | 1 - src/net/Job.cpp | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37a00912..e861f26a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,7 +186,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC) elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall") - set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants -Wno-missing-braces") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") diff --git a/src/api/ApiState.h b/src/api/ApiState.h index 72a332aa..7ecca36d 100644 --- a/src/api/ApiState.h +++ b/src/api/ApiState.h @@ -57,7 +57,6 @@ private: double m_highestHashrate; double m_totalHashrate[3]; int m_threads; - mutable char m_buf[4096]; NetworkState m_network; }; diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 7295d943..c3d33739 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -171,5 +171,5 @@ void Job::toHex(const unsigned char* in, unsigned int len, char* out) bool Job::operator==(const Job &other) const { - return m_id == other.m_id && memcmp(m_blob, other.m_blob, sizeof(m_blob) == 0); + return m_id == other.m_id && memcmp(m_blob, other.m_blob, sizeof(m_blob)) == 0; } From b46f376f3294a8277c2e4fefec8a3a84849003d3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 7 Oct 2017 19:23:46 +0300 Subject: [PATCH 032/389] Remove unused variables . --- CMakeLists.txt | 4 ++-- src/api/Httpd.cpp | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e861f26a..73c4421b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,9 +186,9 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC) elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall") - set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants -Wno-missing-braces") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti -Wno-missing-braces") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") endif() diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index cb361114..996bc007 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -31,10 +31,6 @@ #include "log/Log.h" -static const char k500 [] = "{\"error\":\"INTERNAL_SERVER_ERROR\"}"; -static const size_t k500Size = sizeof(k500) - 1; - - Httpd::Httpd(int port, const char *accessToken) : m_accessToken(accessToken), m_port(port), From a3297b9ea4154599dc6a95bb40d73008a972c95b Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 7 Oct 2017 20:36:02 +0300 Subject: [PATCH 033/389] Fixed 32 bit version crash. --- src/net/Job.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/net/Job.h b/src/net/Job.h index 1ba2d25d..bf8f8835 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -70,6 +70,8 @@ public: bool operator==(const Job &other) const; private: + VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. + bool m_nicehash; int m_poolId; int m_threadId; @@ -77,7 +79,6 @@ private: size_t m_size; uint64_t m_diff; uint64_t m_target; - VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. # ifdef XMRIG_PROXY_PROJECT VAR_ALIGN(16, char m_rawBlob[169]); From f9202c6951f63bec3d00536e9b419ae0bc722f91 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 10 Oct 2017 16:31:19 +0300 Subject: [PATCH 034/389] #147 Fixed comparability with monero-stratum. --- src/net/JobId.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/JobId.h b/src/net/JobId.h index e68b3953..06189779 100644 --- a/src/net/JobId.h +++ b/src/net/JobId.h @@ -63,7 +63,7 @@ public: } const size_t size = strlen(id); - if (size < 4 || size >= sizeof(m_data)) { + if (size >= sizeof(m_data)) { return false; } From 4138d4a178f7b65da1c6b71a9dba14165aeb39c8 Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 12 Oct 2017 22:40:20 +0300 Subject: [PATCH 035/389] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e6648bc5..311f8ca3 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ XMRig is high performance Monero (XMR) CPU miner, with the official full Windows Originally based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of legacy code, since version 1.0.0 complete rewritten from scratch on C++. * This is the CPU-mining version, there is also a [NVIDIA GPU version](https://github.com/xmrig/xmrig-nvidia). +* [Roadmap](https://github.com/xmrig/xmrig/issues/106) for next releases. From 7e4d0d83ff45a4909a7b8731f93a03603fbe0c9f Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 15 Oct 2017 03:42:45 +0300 Subject: [PATCH 036/389] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 090b4363..aa1f8cf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# v2.4.1 + - [#147](https://github.com/xmrig/xmrig/issues/147) Fixed comparability with monero-stratum. + # v2.4.0 - Added [HTTP API](https://github.com/xmrig/xmrig/wiki/API). - Added comments support in config file. From bea3bc74b2f53af187045047314c4d6c6e5b55f6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 15 Oct 2017 04:52:00 +0300 Subject: [PATCH 037/389] v2.4.1. --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index 7f80dea0..76f61596 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.4.0" +#define APP_VERSION "2.4.1" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 4 -#define APP_VER_BUILD 0 +#define APP_VER_BUILD 1 #define APP_VER_REV 0 #ifdef _MSC_VER From 1af1ba6b5fe4b3f65afec903dd80b6dfd5f07225 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 16 Oct 2017 01:26:55 +0300 Subject: [PATCH 038/389] #153 Force reconnect when duplicated job received. --- src/net/Client.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index dfe202a4..c6d683a2 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -220,7 +220,8 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) } if (m_job == job) { - LOG_WARN("[%s:%u] duplicate job received, ignore", m_url.host(), m_url.port()); + LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); + close(); return false; } From 9c0fe7310242b84fe33af56a2493603f5324296c Mon Sep 17 00:00:00 2001 From: vcambur Date: Fri, 20 Oct 2017 08:41:08 +0000 Subject: [PATCH 039/389] Some FreeBSD fixes --- CMakeLists.txt | 8 ++++++++ src/Cpu_unix.cpp | 13 +++++++++++-- src/Mem_unix.cpp | 3 ++- src/Platform_unix.cpp | 3 ++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 73c4421b..f0d81e2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,6 +122,14 @@ elseif (APPLE) src/Mem_unix.cpp src/Platform_mac.cpp ) +elseif (CMAKE_SYSTEM_NAME STREQUAL FreeBSD) + set(SOURCES_OS + src/App_unix.cpp + src/Cpu_unix.cpp + src/Mem_unix.cpp + src/Platform_unix.cpp + ) +set(EXTRA_LIBS pthread kvm) else() set(SOURCES_OS src/App_unix.cpp diff --git a/src/Cpu_unix.cpp b/src/Cpu_unix.cpp index 5d3a6d64..ca1225df 100644 --- a/src/Cpu_unix.cpp +++ b/src/Cpu_unix.cpp @@ -20,8 +20,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - - +#ifdef __FreeBSD__ +#include +#include +#include +#include +#endif #include #include #include @@ -30,6 +34,9 @@ #include "Cpu.h" +#ifdef __FreeBSD__ +typedef cpuset_t cpu_set_t; +#endif void Cpu::init() { @@ -53,7 +60,9 @@ void Cpu::setAffinity(int id, uint64_t mask) } if (id == -1) { + #ifndef __FreeBSD__ sched_setaffinity(0, sizeof(&set), &set); + #endif } else { pthread_setaffinity_np(pthread_self(), sizeof(&set), &set); } diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 7c41fd16..2cb14bc9 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -51,10 +51,11 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) # if defined(__APPLE__) m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); +# elif defined(__FreeBSD__) + m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); # else m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); # endif - if (m_memory == MAP_FAILED) { m_memory = static_cast(_mm_malloc(size, 16)); return true; diff --git a/src/Platform_unix.cpp b/src/Platform_unix.cpp index 27d8de37..b6d16d6f 100644 --- a/src/Platform_unix.cpp +++ b/src/Platform_unix.cpp @@ -21,7 +21,6 @@ * along with this program. If not, see . */ - #include #include #include @@ -116,6 +115,7 @@ void Platform::setThreadPriority(int priority) setpriority(PRIO_PROCESS, 0, prio); +#ifdef SCHED_IDLE if (priority == 0) { sched_param param; param.sched_priority = 0; @@ -124,4 +124,5 @@ void Platform::setThreadPriority(int priority) sched_setscheduler(0, SCHED_BATCH, ¶m); } } +#endif } From 10df3ec227e253b3799d4420c42ac04a08244faf Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 20 Oct 2017 19:54:18 +0300 Subject: [PATCH 040/389] Code style fixes for FreeBSD support pull request. --- CMakeLists.txt | 12 ++++-------- src/Cpu_unix.cpp | 18 ++++++++++++------ src/Platform_unix.cpp | 4 ++-- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f0d81e2f..7e102fc5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,14 +122,6 @@ elseif (APPLE) src/Mem_unix.cpp src/Platform_mac.cpp ) -elseif (CMAKE_SYSTEM_NAME STREQUAL FreeBSD) - set(SOURCES_OS - src/App_unix.cpp - src/Cpu_unix.cpp - src/Mem_unix.cpp - src/Platform_unix.cpp - ) -set(EXTRA_LIBS pthread kvm) else() set(SOURCES_OS src/App_unix.cpp @@ -141,6 +133,10 @@ else() set(EXTRA_LIBS pthread) endif() +if (CMAKE_SYSTEM_NAME STREQUAL FreeBSD) + set(EXTRA_LIBS ${EXTRA_LIBS} kvm) +endif() + add_definitions(/D__STDC_FORMAT_MACROS) add_definitions(/DUNICODE) add_definitions(/DRAPIDJSON_SSE2) diff --git a/src/Cpu_unix.cpp b/src/Cpu_unix.cpp index ca1225df..8de98c8c 100644 --- a/src/Cpu_unix.cpp +++ b/src/Cpu_unix.cpp @@ -20,12 +20,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + + #ifdef __FreeBSD__ -#include -#include -#include -#include +# include +# include +# include +# include #endif + + #include #include #include @@ -34,10 +38,12 @@ #include "Cpu.h" + #ifdef __FreeBSD__ typedef cpuset_t cpu_set_t; #endif + void Cpu::init() { # ifdef XMRIG_NO_LIBCPUID @@ -60,9 +66,9 @@ void Cpu::setAffinity(int id, uint64_t mask) } if (id == -1) { - #ifndef __FreeBSD__ +# ifndef __FreeBSD__ sched_setaffinity(0, sizeof(&set), &set); - #endif +# endif } else { pthread_setaffinity_np(pthread_self(), sizeof(&set), &set); } diff --git a/src/Platform_unix.cpp b/src/Platform_unix.cpp index b6d16d6f..ecccc49e 100644 --- a/src/Platform_unix.cpp +++ b/src/Platform_unix.cpp @@ -115,7 +115,7 @@ void Platform::setThreadPriority(int priority) setpriority(PRIO_PROCESS, 0, prio); -#ifdef SCHED_IDLE +# ifdef SCHED_IDLE if (priority == 0) { sched_param param; param.sched_priority = 0; @@ -124,5 +124,5 @@ void Platform::setThreadPriority(int priority) sched_setscheduler(0, SCHED_BATCH, ¶m); } } -#endif +# endif } From a7c0ba6d2887a32a5a714d863682cca495ff1f97 Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 23 Oct 2017 14:10:37 +0300 Subject: [PATCH 041/389] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa1f8cf2..cf4b2101 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v2.4.2 + - [#60](https://github.com/xmrig/xmrig/issues/60) Added FreeBSD support, thanks [vcambur](https://github.com/vcambur). + - [#153](https://github.com/xmrig/xmrig/issues/153) Fixed issues with dwarfpool.com. + # v2.4.1 - [#147](https://github.com/xmrig/xmrig/issues/147) Fixed comparability with monero-stratum. From fd029201b00bab2948cbe0ed67ff162e10aa9dfe Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 23 Oct 2017 15:32:07 +0300 Subject: [PATCH 042/389] v2.4.2. --- src/net/Client.cpp | 5 ++++- src/version.h | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index c6d683a2..88134ae7 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -220,7 +220,10 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) } if (m_job == job) { - LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); + if (!m_quiet) { + LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); + } + close(); return false; } diff --git a/src/version.h b/src/version.h index 76f61596..8d2e992f 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.4.1" +#define APP_VERSION "2.4.2" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 4 -#define APP_VER_BUILD 1 +#define APP_VER_BUILD 2 #define APP_VER_REV 0 #ifdef _MSC_VER From 6479d6bb6f29bbace7ba0c3c22ecf0edb003030f Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 30 Oct 2017 00:48:49 +0300 Subject: [PATCH 043/389] #157 Explicitly add linking with librt, probably fix build issue with some old Linux. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e102fc5..fc01ed30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,7 +130,7 @@ else() src/Platform_unix.cpp ) - set(EXTRA_LIBS pthread) + set(EXTRA_LIBS pthread rt) endif() if (CMAKE_SYSTEM_NAME STREQUAL FreeBSD) From c384df9651a45467088848bf5450c8c1fa513d62 Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 2 Nov 2017 03:37:58 +0300 Subject: [PATCH 044/389] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 311f8ca3..b9ca1309 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ XMRig is high performance Monero (XMR) CPU miner, with the official full Windows support. Originally based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of legacy code, since version 1.0.0 complete rewritten from scratch on C++. -* This is the CPU-mining version, there is also a [NVIDIA GPU version](https://github.com/xmrig/xmrig-nvidia). +* This is the **CPU-mining** version, there is also a [NVIDIA GPU version](https://github.com/xmrig/xmrig-nvidia) and [AMD GPU version]( https://github.com/xmrig/xmrig-amd). * [Roadmap](https://github.com/xmrig/xmrig/issues/106) for next releases. From ee441b6b9a03f1d400fb93f67fb079e7f24b590e Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 2 Nov 2017 03:39:47 +0300 Subject: [PATCH 045/389] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b9ca1309..50ab830b 100644 --- a/README.md +++ b/README.md @@ -39,12 +39,12 @@ Originally based on cpuminer-multi with heavy optimizations/rewrites and removin ## Usage ### Basic example ``` -xmrig.exe -o pool.minemonero.pro:5555 -u YOUR_WALLET -p x -k +xmrig.exe -o pool.monero.hashvault.pro:5555 -u YOUR_WALLET -p x -k ``` ### Failover ``` -xmrig.exe -o pool.minemonero.pro:5555 -u YOUR_WALLET1 -p x -k -o pool.supportxmr.com:5555 -u YOUR_WALLET2 -p x -k +xmrig.exe -o pool.monero.hashvault.pro:5555 -u YOUR_WALLET1 -p x -k -o pool.supportxmr.com:5555 -u YOUR_WALLET2 -p x -k ``` For failover you can add multiple pools, maximum count not limited. From 1391a2d8582e91fb38bf3c5a09974f1734ea6ff4 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 2 Nov 2017 19:40:33 +0300 Subject: [PATCH 046/389] #184 Workaround for CPUs with disabled Hyper-Threading. --- src/Cpu.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Cpu.cpp b/src/Cpu.cpp index 2e79b6df..ff6f49e9 100644 --- a/src/Cpu.cpp +++ b/src/Cpu.cpp @@ -86,9 +86,13 @@ void Cpu::initCommon() strncpy(m_brand, data.brand_str, sizeof(m_brand) - 1); m_totalThreads = data.total_logical_cpus; - m_sockets = m_totalThreads / data.num_logical_cpus; - m_totalCores = data.num_cores *m_sockets; + m_sockets = m_totalThreads / data.num_logical_cpus; + if (m_sockets == 0) { + m_sockets = 1; + } + + m_totalCores = data.num_cores * m_sockets; m_l3_cache = data.l3_cache > 0 ? data.l3_cache * m_sockets : 0; // Workaround for AMD CPUs https://github.com/anrieff/libcpuid/issues/97 From c0e849b394811ceeb7b17e5cb52ac329f1e25a79 Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 2 Nov 2017 19:49:25 +0300 Subject: [PATCH 047/389] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf4b2101..8a40a859 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# v2.4.3 + - [#184](https://github.com/xmrig/xmrig/issues/184) Fixed cache size detection for CPUs with disabled Hyper-Threading. + # v2.4.2 - [#60](https://github.com/xmrig/xmrig/issues/60) Added FreeBSD support, thanks [vcambur](https://github.com/vcambur). - [#153](https://github.com/xmrig/xmrig/issues/153) Fixed issues with dwarfpool.com. From d403dcf95c815e36d0b453704e9b7e17baec868b Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 3 Nov 2017 05:35:29 +0300 Subject: [PATCH 048/389] Optimized software aes. --- CMakeLists.txt | 3 +- src/crypto/CryptoNight_p.h | 21 ++-- src/crypto/soft_aes.c | 212 ------------------------------------- src/crypto/soft_aes.h | 125 ++++++++++++++++++++++ 4 files changed, 136 insertions(+), 225 deletions(-) delete mode 100644 src/crypto/soft_aes.c create mode 100644 src/crypto/soft_aes.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fc01ed30..c86fe953 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ set(HEADERS_CRYPTO src/crypto/groestl_tables.h src/crypto/hash.h src/crypto/skein_port.h + src/crypto/soft_aes.h ) set(SOURCES @@ -99,8 +100,6 @@ set(SOURCES_CRYPTO src/crypto/c_blake256.c src/crypto/c_jh.c src/crypto/c_skein.c - src/crypto/soft_aes.c - src/crypto/soft_aes.c src/crypto/CryptoNight.cpp ) diff --git a/src/crypto/CryptoNight_p.h b/src/crypto/CryptoNight_p.h index b85a9da6..75809b81 100644 --- a/src/crypto/CryptoNight_p.h +++ b/src/crypto/CryptoNight_p.h @@ -34,6 +34,7 @@ #include "crypto/CryptoNight.h" +#include "crypto/soft_aes.h" extern "C" @@ -43,9 +44,6 @@ extern "C" #include "crypto/c_blake256.h" #include "crypto/c_jh.h" #include "crypto/c_skein.h" - -__m128i soft_aesenc(__m128i in, __m128i key); -__m128i soft_aeskeygenassist(__m128i key, uint8_t rcon); } @@ -151,13 +149,14 @@ static inline void aes_genkey_sub(__m128i* xout0, __m128i* xout2) } -static inline void soft_aes_genkey_sub(__m128i* xout0, __m128i* xout2, uint8_t rcon) +template +static inline void soft_aes_genkey_sub(__m128i* xout0, __m128i* xout2) { - __m128i xout1 = soft_aeskeygenassist(*xout2, rcon); + __m128i xout1 = soft_aeskeygenassist(*xout2); xout1 = _mm_shuffle_epi32(xout1, 0xFF); // see PSHUFD, set all elems to 4th elem *xout0 = sl_xor(*xout0); *xout0 = _mm_xor_si128(*xout0, xout1); - xout1 = soft_aeskeygenassist(*xout0, 0x00); + xout1 = soft_aeskeygenassist<0x00>(*xout0); xout1 = _mm_shuffle_epi32(xout1, 0xAA); // see PSHUFD, set all elems to 3rd elem *xout2 = sl_xor(*xout2); *xout2 = _mm_xor_si128(*xout2, xout1); @@ -168,23 +167,23 @@ template static inline void aes_genkey(const __m128i* memory, __m128i* k0, __m128i* k1, __m128i* k2, __m128i* k3, __m128i* k4, __m128i* k5, __m128i* k6, __m128i* k7, __m128i* k8, __m128i* k9) { __m128i xout0 = _mm_load_si128(memory); - __m128i xout2 = _mm_load_si128(memory +1 ); + __m128i xout2 = _mm_load_si128(memory + 1); *k0 = xout0; *k1 = xout2; - SOFT_AES ? soft_aes_genkey_sub(&xout0, &xout2, 0x01) : aes_genkey_sub<0x01>(&xout0, &xout2); + SOFT_AES ? soft_aes_genkey_sub<0x01>(&xout0, &xout2) : aes_genkey_sub<0x01>(&xout0, &xout2); *k2 = xout0; *k3 = xout2; - SOFT_AES ? soft_aes_genkey_sub(&xout0, &xout2, 0x02) : aes_genkey_sub<0x02>(&xout0, &xout2); + SOFT_AES ? soft_aes_genkey_sub<0x02>(&xout0, &xout2) : aes_genkey_sub<0x02>(&xout0, &xout2); *k4 = xout0; *k5 = xout2; - SOFT_AES ? soft_aes_genkey_sub(&xout0, &xout2, 0x04) : aes_genkey_sub<0x04>(&xout0, &xout2); + SOFT_AES ? soft_aes_genkey_sub<0x04>(&xout0, &xout2) : aes_genkey_sub<0x04>(&xout0, &xout2); *k6 = xout0; *k7 = xout2; - SOFT_AES ? soft_aes_genkey_sub(&xout0, &xout2, 0x08) : aes_genkey_sub<0x08>(&xout0, &xout2); + SOFT_AES ? soft_aes_genkey_sub<0x08>(&xout0, &xout2) : aes_genkey_sub<0x08>(&xout0, &xout2); *k8 = xout0; *k9 = xout2; } diff --git a/src/crypto/soft_aes.c b/src/crypto/soft_aes.c deleted file mode 100644 index 6904c526..00000000 --- a/src/crypto/soft_aes.c +++ /dev/null @@ -1,212 +0,0 @@ -/* - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Additional permission under GNU GPL version 3 section 7 - * - * If you modify this Program, or any covered work, by linking or combining - * it with OpenSSL (or a modified version of that library), containing parts - * covered by the terms of OpenSSL License and SSLeay License, the licensors - * of this Program grant you additional permission to convey the resulting work. - * - */ - -/* - * The orginal author of this AES implementation is Karl Malbrain. - */ - -#ifdef __GNUC__ -#include -#else -#include -#endif // __GNUC__ - -#include - -#define TABLE_ALIGN 32 -#define WPOLY 0x011b -#define N_COLS 4 -#define AES_BLOCK_SIZE 16 -#define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2)) - -#if defined(_MSC_VER) -#define ALIGN __declspec(align(TABLE_ALIGN)) -#elif defined(__GNUC__) -#define ALIGN __attribute__ ((aligned(16))) -#else -#define ALIGN -#endif - -#define rf1(r,c) (r) -#define word_in(x,c) (*((uint32_t*)(x)+(c))) -#define word_out(x,c,v) (*((uint32_t*)(x)+(c)) = (v)) - -#define s(x,c) x[c] -#define si(y,x,c) (s(y,c) = word_in(x, c)) -#define so(y,x,c) word_out(y, c, s(x,c)) -#define state_in(y,x) si(y,x,0); si(y,x,1); si(y,x,2); si(y,x,3) -#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3) -#define round(y,x,k) \ -y[0] = (k)[0] ^ (t_fn[0][x[0] & 0xff] ^ t_fn[1][(x[1] >> 8) & 0xff] ^ t_fn[2][(x[2] >> 16) & 0xff] ^ t_fn[3][x[3] >> 24]); \ -y[1] = (k)[1] ^ (t_fn[0][x[1] & 0xff] ^ t_fn[1][(x[2] >> 8) & 0xff] ^ t_fn[2][(x[3] >> 16) & 0xff] ^ t_fn[3][x[0] >> 24]); \ -y[2] = (k)[2] ^ (t_fn[0][x[2] & 0xff] ^ t_fn[1][(x[3] >> 8) & 0xff] ^ t_fn[2][(x[0] >> 16) & 0xff] ^ t_fn[3][x[1] >> 24]); \ -y[3] = (k)[3] ^ (t_fn[0][x[3] & 0xff] ^ t_fn[1][(x[0] >> 8) & 0xff] ^ t_fn[2][(x[1] >> 16) & 0xff] ^ t_fn[3][x[2] >> 24]); -#define to_byte(x) ((x) & 0xff) -#define bval(x,n) to_byte((x) >> (8 * (n))) - -#define fwd_var(x,r,c)\ - ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\ - : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\ - : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\ - : ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))) - -#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c)) - -#define sb_data(w) {\ - w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\ - w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\ - w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\ - w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\ - w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\ - w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\ - w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\ - w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\ - w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\ - w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\ - w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\ - w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\ - w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\ - w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\ - w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\ - w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\ - w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\ - w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\ - w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\ - w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\ - w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\ - w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\ - w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\ - w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\ - w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\ - w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\ - w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\ - w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\ - w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\ - w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\ - w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\ - w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16) } - -#define rc_data(w) {\ - w(0x01), w(0x02), w(0x04), w(0x08), w(0x10),w(0x20), w(0x40), w(0x80),\ - w(0x1b), w(0x36) } - -#define bytes2word(b0, b1, b2, b3) (((uint32_t)(b3) << 24) | \ - ((uint32_t)(b2) << 16) | ((uint32_t)(b1) << 8) | (b0)) - -#define h0(x) (x) -#define w0(p) bytes2word(p, 0, 0, 0) -#define w1(p) bytes2word(0, p, 0, 0) -#define w2(p) bytes2word(0, 0, p, 0) -#define w3(p) bytes2word(0, 0, 0, p) - -#define u0(p) bytes2word(f2(p), p, p, f3(p)) -#define u1(p) bytes2word(f3(p), f2(p), p, p) -#define u2(p) bytes2word(p, f3(p), f2(p), p) -#define u3(p) bytes2word(p, p, f3(p), f2(p)) - -#define v0(p) bytes2word(fe(p), f9(p), fd(p), fb(p)) -#define v1(p) bytes2word(fb(p), fe(p), f9(p), fd(p)) -#define v2(p) bytes2word(fd(p), fb(p), fe(p), f9(p)) -#define v3(p) bytes2word(f9(p), fd(p), fb(p), fe(p)) - -#define f2(x) ((x<<1) ^ (((x>>7) & 1) * WPOLY)) -#define f4(x) ((x<<2) ^ (((x>>6) & 1) * WPOLY) ^ (((x>>6) & 2) * WPOLY)) -#define f8(x) ((x<<3) ^ (((x>>5) & 1) * WPOLY) ^ (((x>>5) & 2) * WPOLY) ^ (((x>>5) & 4) * WPOLY)) -#define f3(x) (f2(x) ^ x) -#define f9(x) (f8(x) ^ x) -#define fb(x) (f8(x) ^ f2(x) ^ x) -#define fd(x) (f8(x) ^ f4(x) ^ x) -#define fe(x) (f8(x) ^ f4(x) ^ f2(x)) - -#define t_dec(m,n) t_##m##n -#define t_set(m,n) t_##m##n -#define t_use(m,n) t_##m##n - -#define d_4(t,n,b,e,f,g,h) ALIGN const t n[4][256] = { b(e), b(f), b(g), b(h) } - -#define four_tables(x,tab,vf,rf,c) \ - (tab[0][bval(vf(x,0,c),rf(0,c))] \ - ^ tab[1][bval(vf(x,1,c),rf(1,c))] \ - ^ tab[2][bval(vf(x,2,c),rf(2,c))] \ - ^ tab[3][bval(vf(x,3,c),rf(3,c))]) - -d_4(uint32_t, t_dec(f,n), sb_data, u0, u1, u2, u3); - -__m128i soft_aesenc(__m128i in, __m128i key) -{ - uint32_t x0, x1, x2, x3; - x0 = _mm_cvtsi128_si32(in); - x1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0x55)); - x2 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xAA)); - x3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xFF)); - - __m128i out = _mm_set_epi32( - (t_fn[0][x3 & 0xff] ^ t_fn[1][(x0 >> 8) & 0xff] ^ t_fn[2][(x1 >> 16) & 0xff] ^ t_fn[3][x2 >> 24]), - (t_fn[0][x2 & 0xff] ^ t_fn[1][(x3 >> 8) & 0xff] ^ t_fn[2][(x0 >> 16) & 0xff] ^ t_fn[3][x1 >> 24]), - (t_fn[0][x1 & 0xff] ^ t_fn[1][(x2 >> 8) & 0xff] ^ t_fn[2][(x3 >> 16) & 0xff] ^ t_fn[3][x0 >> 24]), - (t_fn[0][x0 & 0xff] ^ t_fn[1][(x1 >> 8) & 0xff] ^ t_fn[2][(x2 >> 16) & 0xff] ^ t_fn[3][x3 >> 24])); - - return _mm_xor_si128(out, key); -} - -uint8_t Sbox[256] = { // forward s-box -0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, -0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, -0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, -0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, -0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, -0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, -0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, -0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, -0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, -0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, -0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, -0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, -0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, -0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, -0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, -0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16}; - -static inline void sub_word(uint8_t* key) -{ - key[0] = Sbox[key[0]]; - key[1] = Sbox[key[1]]; - key[2] = Sbox[key[2]]; - key[3] = Sbox[key[3]]; -} - -#ifdef __clang__ -uint32_t _rotr(uint32_t value, uint32_t amount) -{ - return (value >> amount) | (value << ((32 - amount) & 31)); -} -#endif - -__m128i soft_aeskeygenassist(__m128i key, uint8_t rcon) -{ - uint32_t X1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(key, 0x55)); - uint32_t X3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(key, 0xFF)); - sub_word((uint8_t*)&X1); - sub_word((uint8_t*)&X3); - return _mm_set_epi32(_rotr(X3, 8) ^ rcon, X3,_rotr(X1, 8) ^ rcon, X1); -} diff --git a/src/crypto/soft_aes.h b/src/crypto/soft_aes.h new file mode 100644 index 00000000..e28be3fa --- /dev/null +++ b/src/crypto/soft_aes.h @@ -0,0 +1,125 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Additional permission under GNU GPL version 3 section 7 + * + * If you modify this Program, or any covered work, by linking or combining + * it with OpenSSL (or a modified version of that library), containing parts + * covered by the terms of OpenSSL License and SSLeay License, the licensors + * of this Program grant you additional permission to convey the resulting work. + * + */ + +/* + * Parts of this file are originally copyright (c) 2014-2017, The Monero Project + */ +#pragma once + +#ifdef __GNUC__ +#include +#else +#include +#endif // __GNUC__ + +#include + +#define saes_data(w) {\ + w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\ + w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\ + w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\ + w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\ + w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\ + w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\ + w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\ + w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\ + w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\ + w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\ + w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\ + w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\ + w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\ + w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\ + w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\ + w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\ + w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\ + w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\ + w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\ + w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\ + w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\ + w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\ + w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\ + w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\ + w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\ + w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\ + w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\ + w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\ + w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\ + w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\ + w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\ + w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16) } + +#define SAES_WPOLY 0x011b + +#define saes_b2w(b0, b1, b2, b3) (((uint32_t)(b3) << 24) | \ + ((uint32_t)(b2) << 16) | ((uint32_t)(b1) << 8) | (b0)) + +#define saes_f2(x) ((x<<1) ^ (((x>>7) & 1) * SAES_WPOLY)) +#define saes_f3(x) (saes_f2(x) ^ x) +#define saes_h0(x) (x) + +#define saes_u0(p) saes_b2w(saes_f2(p), p, p, saes_f3(p)) +#define saes_u1(p) saes_b2w(saes_f3(p), saes_f2(p), p, p) +#define saes_u2(p) saes_b2w( p, saes_f3(p), saes_f2(p), p) +#define saes_u3(p) saes_b2w( p, p, saes_f3(p), saes_f2(p)) + +alignas(16) const uint32_t saes_table[4][256] = { saes_data(saes_u0), saes_data(saes_u1), saes_data(saes_u2), saes_data(saes_u3) }; +alignas(16) const uint8_t saes_sbox[256] = saes_data(saes_h0); + +static inline __m128i soft_aesenc(__m128i in, __m128i key) +{ + const uint32_t x0 = _mm_cvtsi128_si32(in); + const uint32_t x1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0x55)); + const uint32_t x2 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xAA)); + const uint32_t x3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xFF)); + + __m128i out = _mm_set_epi32( + (saes_table[0][x3 & 0xff] ^ saes_table[1][(x0 >> 8) & 0xff] ^ saes_table[2][(x1 >> 16) & 0xff] ^ saes_table[3][x2 >> 24]), + (saes_table[0][x2 & 0xff] ^ saes_table[1][(x3 >> 8) & 0xff] ^ saes_table[2][(x0 >> 16) & 0xff] ^ saes_table[3][x1 >> 24]), + (saes_table[0][x1 & 0xff] ^ saes_table[1][(x2 >> 8) & 0xff] ^ saes_table[2][(x3 >> 16) & 0xff] ^ saes_table[3][x0 >> 24]), + (saes_table[0][x0 & 0xff] ^ saes_table[1][(x1 >> 8) & 0xff] ^ saes_table[2][(x2 >> 16) & 0xff] ^ saes_table[3][x3 >> 24])); + + return _mm_xor_si128(out, key); +} + +static inline uint32_t sub_word(uint32_t key) +{ + return (saes_sbox[key >> 24 ] << 24) | + (saes_sbox[(key >> 16) & 0xff] << 16 ) | + (saes_sbox[(key >> 8) & 0xff] << 8 ) | + saes_sbox[key & 0xff]; +} + +#ifdef __clang__ +static inline uint32_t _rotr(uint32_t value, uint32_t amount) +{ + return (value >> amount) | (value << ((32 - amount) & 31)); +} +#endif + +template +static inline __m128i soft_aeskeygenassist(__m128i key) +{ + const uint32_t X1 = sub_word(_mm_cvtsi128_si32(_mm_shuffle_epi32(key, 0x55))); + const uint32_t X3 = sub_word(_mm_cvtsi128_si32(_mm_shuffle_epi32(key, 0xFF))); + return _mm_set_epi32(_rotr(X3, 8) ^ rcon, X3, _rotr(X1, 8) ^ rcon, X1); +} From 6cc152e26fe438b23a522caf28670817906d761b Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 6 Nov 2017 03:11:35 +0300 Subject: [PATCH 049/389] Added ARMv8 (aarch64) support. --- CMakeLists.txt | 67 +- cmake/cpu.cmake | 16 + cmake/flags.cmake | 63 + src/3rdparty/aligned_malloc.h | 65 + src/Cpu_arm.cpp | 52 + src/Mem_unix.cpp | 8 +- src/crypto/CryptoNight.cpp | 8 +- src/crypto/CryptoNight_arm64.h | 458 +++++ .../{CryptoNight_p.h => CryptoNight_x86.h} | 0 src/crypto/SSE2NEON.h | 1497 +++++++++++++++++ src/crypto/soft_aes.h | 14 +- 11 files changed, 2188 insertions(+), 60 deletions(-) create mode 100644 cmake/cpu.cmake create mode 100644 cmake/flags.cmake create mode 100644 src/3rdparty/aligned_malloc.h create mode 100644 src/Cpu_arm.cpp create mode 100644 src/crypto/CryptoNight_arm64.h rename src/crypto/{CryptoNight_p.h => CryptoNight_x86.h} (100%) create mode 100644 src/crypto/SSE2NEON.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c86fe953..0275320f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ option(WITH_AEON "CryptoNight-Lite support" ON) option(WITH_HTTPD "HTTP REST API" ON) include (CheckIncludeFile) +include (cmake/cpu.cmake) set(HEADERS @@ -56,7 +57,6 @@ set(HEADERS_CRYPTO src/crypto/c_keccak.h src/crypto/c_skein.h src/crypto/CryptoNight.h - src/crypto/CryptoNight_p.h src/crypto/CryptoNight_test.h src/crypto/groestl_tables.h src/crypto/hash.h @@ -64,6 +64,12 @@ set(HEADERS_CRYPTO src/crypto/soft_aes.h ) +if (XMRIG_ARM64) + set(HEADERS_CRYPTO "${HEADERS_CRYPTO}" src/crypto/CryptoNight_arm64.h) +else() + set(HEADERS_CRYPTO "${HEADERS_CRYPTO}" src/crypto/CryptoNight_x86.h) +endif() + set(SOURCES src/api/Api.cpp src/api/ApiState.cpp @@ -138,63 +144,13 @@ endif() add_definitions(/D__STDC_FORMAT_MACROS) add_definitions(/DUNICODE) -add_definitions(/DRAPIDJSON_SSE2) #add_definitions(/DAPP_DEBUG) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") find_package(UV REQUIRED) -if ("${CMAKE_BUILD_TYPE}" STREQUAL "") - set(CMAKE_BUILD_TYPE Release) -endif() - - -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) -set(CMAKE_CXX_STANDARD 11) - - -# https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html -if (CMAKE_CXX_COMPILER_ID MATCHES GNU) - - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall -Wno-strict-aliasing") - set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") - - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") - - if (WIN32) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") - else() - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++") - endif() - - add_definitions(/D_GNU_SOURCE) - - if (${CMAKE_VERSION} VERSION_LESS "3.1.0") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - endif() - - #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -gdwarf-2") - -elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC) - - set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Ox /Ot /Oi /MT /GL") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox /Ot /Oi /MT /GL") - add_definitions(/D_CRT_SECURE_NO_WARNINGS) - add_definitions(/D_CRT_NONSTDC_NO_WARNINGS) - add_definitions(/DNOMINMAX) - -elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang) - - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall") - set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") - - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti -Wno-missing-braces") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") - -endif() +include(cmake/flags.cmake) if (WITH_LIBCPUID) add_subdirectory(src/3rdparty/libcpuid) @@ -204,7 +160,12 @@ if (WITH_LIBCPUID) set(SOURCES_CPUID src/Cpu.cpp) else() add_definitions(/DXMRIG_NO_LIBCPUID) - set(SOURCES_CPUID src/Cpu_stub.cpp) + + if (XMRIG_ARM64) + set(SOURCES_CPUID src/Cpu_arm.cpp) + else() + set(SOURCES_CPUID src/Cpu_stub.cpp) + endif() endif() CHECK_INCLUDE_FILE (syslog.h HAVE_SYSLOG_H) diff --git a/cmake/cpu.cmake b/cmake/cpu.cmake new file mode 100644 index 00000000..56bbef96 --- /dev/null +++ b/cmake/cpu.cmake @@ -0,0 +1,16 @@ +if (NOT CMAKE_SYSTEM_PROCESSOR) + message(WARNING "CMAKE_SYSTEM_PROCESSOR not defined") +endif() + + +if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64)$") + add_definitions(/DRAPIDJSON_SSE2) +endif() + + +if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64)$") + set(XMRIG_ARM64 ON) + set(WITH_LIBCPUID OFF) + + add_definitions(/DXMRIG_ARM) +endif() diff --git a/cmake/flags.cmake b/cmake/flags.cmake new file mode 100644 index 00000000..7f32e115 --- /dev/null +++ b/cmake/flags.cmake @@ -0,0 +1,63 @@ +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 11) + +if ("${CMAKE_BUILD_TYPE}" STREQUAL "") + set(CMAKE_BUILD_TYPE Release) +endif() + +if (CMAKE_CXX_COMPILER_ID MATCHES GNU) + + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-strict-aliasing") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") + + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-exceptions -fno-rtti") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") + + if (CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a+crypto") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+crypto -flax-vector-conversions") + else() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes") + endif() + + if (WIN32) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") + else() + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++") + endif() + + add_definitions(/D_GNU_SOURCE) + + if (${CMAKE_VERSION} VERSION_LESS "3.1.0") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + endif() + + #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -gdwarf-2") + +elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC) + + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Ox /Ot /Oi /MT /GL") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox /Ot /Oi /MT /GL") + add_definitions(/D_CRT_SECURE_NO_WARNINGS) + add_definitions(/D_CRT_NONSTDC_NO_WARNINGS) + add_definitions(/DNOMINMAX) + +elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang) + + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") + + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-exceptions -fno-rtti -Wno-missing-braces") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") + + if (CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a+crypto") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+crypto") + else() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes") + endif() + +endif() diff --git a/src/3rdparty/aligned_malloc.h b/src/3rdparty/aligned_malloc.h new file mode 100644 index 00000000..0b74b17e --- /dev/null +++ b/src/3rdparty/aligned_malloc.h @@ -0,0 +1,65 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __ALIGNED_MALLOC_H__ +#define __ALIGNED_MALLOC_H__ + + +#include + + +#ifndef __cplusplus +extern int posix_memalign(void **__memptr, size_t __alignment, size_t __size); +#else +// Some systems (e.g. those with GNU libc) declare posix_memalign with an +// exception specifier. Via an "egregious workaround" in +// Sema::CheckEquivalentExceptionSpec, Clang accepts the following as a valid +// redeclaration of glibc's declaration. +extern "C" int posix_memalign(void **__memptr, size_t __alignment, size_t __size); +#endif + + +static __inline__ void *__attribute__((__always_inline__, __malloc__)) _mm_malloc(size_t __size, size_t __align) +{ + if (__align == 1) { + return malloc(__size); + } + + if (!(__align & (__align - 1)) && __align < sizeof(void *)) + __align = sizeof(void *); + + void *__mallocedMemory; + if (posix_memalign(&__mallocedMemory, __align, __size)) { + return 0; + } + + return __mallocedMemory; +} + + +static __inline__ void __attribute__((__always_inline__)) _mm_free(void *__p) +{ + free(__p); +} + +#endif /* __ALIGNED_MALLOC_H__ */ diff --git a/src/Cpu_arm.cpp b/src/Cpu_arm.cpp new file mode 100644 index 00000000..906e0156 --- /dev/null +++ b/src/Cpu_arm.cpp @@ -0,0 +1,52 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include + + +#include "Cpu.h" + + +char Cpu::m_brand[64] = { 0 }; +int Cpu::m_flags = 0; +int Cpu::m_l2_cache = 0; +int Cpu::m_l3_cache = 0; +int Cpu::m_sockets = 1; +int Cpu::m_totalCores = 0; +int Cpu::m_totalThreads = 0; + + +int Cpu::optimalThreadsCount(int algo, bool doubleHash, int maxCpuUsage) +{ + return m_totalThreads; +} + + +void Cpu::initCommon() +{ + memcpy(m_brand, "Unknown", 7); + + m_flags |= X86_64; + m_flags |= AES; +} diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 2cb14bc9..43ffa664 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -23,10 +23,16 @@ #include -#include #include +#if defined(XMRIG_ARM) && !defined(__clang__) +# include "aligned_malloc.h" +#else +# include +#endif + + #include "crypto/CryptoNight.h" #include "log/Log.h" #include "Mem.h" diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index c4e56678..6fdf9d92 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -23,7 +23,13 @@ #include "crypto/CryptoNight.h" -#include "crypto/CryptoNight_p.h" + +#if defined(__aarch64__) +# include "crypto/CryptoNight_arm64.h" +#else +# include "crypto/CryptoNight_x86.h" +#endif + #include "crypto/CryptoNight_test.h" #include "net/Job.h" #include "net/JobResult.h" diff --git a/src/crypto/CryptoNight_arm64.h b/src/crypto/CryptoNight_arm64.h new file mode 100644 index 00000000..4d70b20d --- /dev/null +++ b/src/crypto/CryptoNight_arm64.h @@ -0,0 +1,458 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016 Imran Yusuff + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CRYPTONIGHT_ARM64_H__ +#define __CRYPTONIGHT_ARM64_H__ + + +#if defined(XMRIG_ARM) && !defined(__clang__) +# include "aligned_malloc.h" +#else +# include +#endif + + +#include "crypto/CryptoNight.h" +#include "crypto/soft_aes.h" + + +extern "C" +{ +#include "crypto/c_keccak.h" +#include "crypto/c_groestl.h" +#include "crypto/c_blake256.h" +#include "crypto/c_jh.h" +#include "crypto/c_skein.h" +} + + +static inline void do_blake_hash(const void* input, size_t len, char* output) { + blake256_hash(reinterpret_cast(output), static_cast(input), len); +} + + +static inline void do_groestl_hash(const void* input, size_t len, char* output) { + groestl(static_cast(input), len * 8, reinterpret_cast(output)); +} + + +static inline void do_jh_hash(const void* input, size_t len, char* output) { + jh_hash(32 * 8, static_cast(input), 8 * len, reinterpret_cast(output)); +} + + +static inline void do_skein_hash(const void* input, size_t len, char* output) { + xmr_skein(static_cast(input), reinterpret_cast(output)); +} + + +void (* const extra_hashes[4])(const void *, size_t, char *) = {do_blake_hash, do_groestl_hash, do_jh_hash, do_skein_hash}; + + +static inline __attribute__((always_inline)) __m128i _mm_set_epi64x(const uint64_t a, const uint64_t b) +{ + return vcombine_u64(vcreate_u64(b), vcreate_u64(a)); +} + + +/* this one was not implemented yet so here it is */ +static inline __attribute__((always_inline)) uint64_t _mm_cvtsi128_si64(__m128i a) +{ + return vgetq_lane_u64(a, 0); +} + + +#define EXTRACT64(X) _mm_cvtsi128_si64(X) + + +static inline uint64_t __umul128(uint64_t a, uint64_t b, uint64_t* hi) +{ + unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b; + *hi = r >> 64; + return (uint64_t) r; +} + + +// This will shift and xor tmp1 into itself as 4 32-bit vals such as +// sl_xor(a1 a2 a3 a4) = a1 (a2^a1) (a3^a2^a1) (a4^a3^a2^a1) +static inline __m128i sl_xor(__m128i tmp1) +{ + __m128i tmp4; + tmp4 = _mm_slli_si128(tmp1, 0x04); + tmp1 = _mm_xor_si128(tmp1, tmp4); + tmp4 = _mm_slli_si128(tmp4, 0x04); + tmp1 = _mm_xor_si128(tmp1, tmp4); + tmp4 = _mm_slli_si128(tmp4, 0x04); + tmp1 = _mm_xor_si128(tmp1, tmp4); + return tmp1; +} + + +template +static inline void aes_genkey_sub(__m128i* xout0, __m128i* xout2) +{ +// __m128i xout1 = _mm_aeskeygenassist_si128(*xout2, rcon); +// xout1 = _mm_shuffle_epi32(xout1, 0xFF); // see PSHUFD, set all elems to 4th elem +// *xout0 = sl_xor(*xout0); +// *xout0 = _mm_xor_si128(*xout0, xout1); +// xout1 = _mm_aeskeygenassist_si128(*xout0, 0x00); +// xout1 = _mm_shuffle_epi32(xout1, 0xAA); // see PSHUFD, set all elems to 3rd elem +// *xout2 = sl_xor(*xout2); +// *xout2 = _mm_xor_si128(*xout2, xout1); +} + + +template +static inline void soft_aes_genkey_sub(__m128i* xout0, __m128i* xout2) +{ + __m128i xout1 = soft_aeskeygenassist(*xout2); + xout1 = _mm_shuffle_epi32(xout1, 0xFF); // see PSHUFD, set all elems to 4th elem + *xout0 = sl_xor(*xout0); + *xout0 = _mm_xor_si128(*xout0, xout1); + xout1 = soft_aeskeygenassist<0x00>(*xout0); + xout1 = _mm_shuffle_epi32(xout1, 0xAA); // see PSHUFD, set all elems to 3rd elem + *xout2 = sl_xor(*xout2); + *xout2 = _mm_xor_si128(*xout2, xout1); +} + + +template +static inline void aes_genkey(const __m128i* memory, __m128i* k0, __m128i* k1, __m128i* k2, __m128i* k3, __m128i* k4, __m128i* k5, __m128i* k6, __m128i* k7, __m128i* k8, __m128i* k9) +{ + __m128i xout0 = _mm_load_si128(memory); + __m128i xout2 = _mm_load_si128(memory + 1); + *k0 = xout0; + *k1 = xout2; + + SOFT_AES ? soft_aes_genkey_sub<0x01>(&xout0, &xout2) : soft_aes_genkey_sub<0x01>(&xout0, &xout2); + *k2 = xout0; + *k3 = xout2; + + SOFT_AES ? soft_aes_genkey_sub<0x02>(&xout0, &xout2) : soft_aes_genkey_sub<0x02>(&xout0, &xout2); + *k4 = xout0; + *k5 = xout2; + + SOFT_AES ? soft_aes_genkey_sub<0x04>(&xout0, &xout2) : soft_aes_genkey_sub<0x04>(&xout0, &xout2); + *k6 = xout0; + *k7 = xout2; + + SOFT_AES ? soft_aes_genkey_sub<0x08>(&xout0, &xout2) : soft_aes_genkey_sub<0x08>(&xout0, &xout2); + *k8 = xout0; + *k9 = xout2; +} + + +template +static inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, __m128i* x3, __m128i* x4, __m128i* x5, __m128i* x6, __m128i* x7) +{ + if (SOFT_AES) { + *x0 = soft_aesenc(*x0, key); + *x1 = soft_aesenc(*x1, key); + *x2 = soft_aesenc(*x2, key); + *x3 = soft_aesenc(*x3, key); + *x4 = soft_aesenc(*x4, key); + *x5 = soft_aesenc(*x5, key); + *x6 = soft_aesenc(*x6, key); + *x7 = soft_aesenc(*x7, key); + } + else { + *x0 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x0), key)); + *x1 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x1), key)); + *x2 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x2), key)); + *x3 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x3), key)); + *x4 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x4), key)); + *x5 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x5), key)); + *x6 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x6), key)); + *x7 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x7), key)); + } +} + + +template +static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) +{ + __m128i xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7; + __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9; + + aes_genkey(input, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9); + + xin0 = _mm_load_si128(input + 4); + xin1 = _mm_load_si128(input + 5); + xin2 = _mm_load_si128(input + 6); + xin3 = _mm_load_si128(input + 7); + xin4 = _mm_load_si128(input + 8); + xin5 = _mm_load_si128(input + 9); + xin6 = _mm_load_si128(input + 10); + xin7 = _mm_load_si128(input + 11); + + for (size_t i = 0; i < MEM / sizeof(__m128i); i += 8) { + if (!SOFT_AES) { + aes_round(_mm_setzero_si128(), &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + } + + aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k3, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k4, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k5, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k6, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k7, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k8, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + + if (!SOFT_AES) { + xin0 ^= k9; + xin1 ^= k9; + xin2 ^= k9; + xin3 ^= k9; + xin4 ^= k9; + xin5 ^= k9; + xin6 ^= k9; + xin7 ^= k9; + } + else { + aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + } + + _mm_store_si128(output + i + 0, xin0); + _mm_store_si128(output + i + 1, xin1); + _mm_store_si128(output + i + 2, xin2); + _mm_store_si128(output + i + 3, xin3); + _mm_store_si128(output + i + 4, xin4); + _mm_store_si128(output + i + 5, xin5); + _mm_store_si128(output + i + 6, xin6); + _mm_store_si128(output + i + 7, xin7); + } +} + + +template +static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) +{ + __m128i xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7; + __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9; + + aes_genkey(output + 2, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9); + + xout0 = _mm_load_si128(output + 4); + xout1 = _mm_load_si128(output + 5); + xout2 = _mm_load_si128(output + 6); + xout3 = _mm_load_si128(output + 7); + xout4 = _mm_load_si128(output + 8); + xout5 = _mm_load_si128(output + 9); + xout6 = _mm_load_si128(output + 10); + xout7 = _mm_load_si128(output + 11); + + for (size_t i = 0; i < MEM / sizeof(__m128i); i += 8) + { + xout0 = _mm_xor_si128(_mm_load_si128(input + i + 0), xout0); + xout1 = _mm_xor_si128(_mm_load_si128(input + i + 1), xout1); + xout2 = _mm_xor_si128(_mm_load_si128(input + i + 2), xout2); + xout3 = _mm_xor_si128(_mm_load_si128(input + i + 3), xout3); + xout4 = _mm_xor_si128(_mm_load_si128(input + i + 4), xout4); + xout5 = _mm_xor_si128(_mm_load_si128(input + i + 5), xout5); + xout6 = _mm_xor_si128(_mm_load_si128(input + i + 6), xout6); + xout7 = _mm_xor_si128(_mm_load_si128(input + i + 7), xout7); + + if (!SOFT_AES) { + aes_round(_mm_setzero_si128(), &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + } + + aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k3, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k4, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k5, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + + if (!SOFT_AES) { + xout0 ^= k9; + xout1 ^= k9; + xout2 ^= k9; + xout3 ^= k9; + xout4 ^= k9; + xout5 ^= k9; + xout6 ^= k9; + xout7 ^= k9; + } + else { + aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + } + } + + _mm_store_si128(output + 4, xout0); + _mm_store_si128(output + 5, xout1); + _mm_store_si128(output + 6, xout2); + _mm_store_si128(output + 7, xout3); + _mm_store_si128(output + 8, xout4); + _mm_store_si128(output + 9, xout5); + _mm_store_si128(output + 10, xout6); + _mm_store_si128(output + 11, xout7); +} + + +template +inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +{ + keccak(static_cast(input), (int) size, ctx->state0, 200); + + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); + + const uint8_t* l0 = ctx->memory; + uint64_t* h0 = reinterpret_cast(ctx->state0); + + uint64_t al0 = h0[0] ^ h0[4]; + uint64_t ah0 = h0[1] ^ h0[5]; + __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + + uint64_t idx0 = h0[0] ^ h0[4]; + + for (size_t i = 0; i < ITERATIONS; i++) { + __m128i cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + + if (SOFT_AES) { + cx = soft_aesenc(cx, _mm_set_epi64x(ah0, al0)); + } + else { + cx = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah0, al0); + } + + _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + idx0 = EXTRACT64(cx); + bx0 = cx; + + uint64_t hi, lo, cl, ch; + cl = ((uint64_t*) &l0[idx0 & MASK])[0]; + ch = ((uint64_t*) &l0[idx0 & MASK])[1]; + lo = __umul128(idx0, cl, &hi); + + al0 += hi; + ah0 += lo; + + ((uint64_t*)&l0[idx0 & MASK])[0] = al0; + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + + ah0 ^= ch; + al0 ^= cl; + idx0 = al0; + } + + cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); + + keccakf(h0, 24); + extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); +} + + +template +inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +{ + keccak((const uint8_t *) input, (int) size, ctx->state0, 200); + keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); + + const uint8_t* l0 = ctx->memory; + const uint8_t* l1 = ctx->memory + MEM; + uint64_t* h0 = reinterpret_cast(ctx->state0); + uint64_t* h1 = reinterpret_cast(ctx->state1); + + cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); + cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); + + uint64_t al0 = h0[0] ^ h0[4]; + uint64_t al1 = h1[0] ^ h1[4]; + uint64_t ah0 = h0[1] ^ h0[5]; + uint64_t ah1 = h1[1] ^ h1[5]; + + __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); + + uint64_t idx0 = h0[0] ^ h0[4]; + uint64_t idx1 = h1[0] ^ h1[4]; + + for (size_t i = 0; i < ITERATIONS; i++) { + __m128i cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + __m128i cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); + + if (SOFT_AES) { + cx0 = soft_aesenc(cx0, _mm_set_epi64x(ah0, al0)); + cx1 = soft_aesenc(cx1, _mm_set_epi64x(ah1, al1)); + } + else { + cx0 = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx0, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah0, al0); + cx1 = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx1, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah1, al1); + } + + _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); + _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + + idx0 = EXTRACT64(cx0); + idx1 = EXTRACT64(cx1); + + bx0 = cx0; + bx1 = cx1; + + uint64_t hi, lo, cl, ch; + cl = ((uint64_t*) &l0[idx0 & MASK])[0]; + ch = ((uint64_t*) &l0[idx0 & MASK])[1]; + lo = __umul128(idx0, cl, &hi); + + al0 += hi; + ah0 += lo; + + ((uint64_t*) &l0[idx0 & MASK])[0] = al0; + ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + + ah0 ^= ch; + al0 ^= cl; + idx0 = al0; + + cl = ((uint64_t*) &l1[idx1 & MASK])[0]; + ch = ((uint64_t*) &l1[idx1 & MASK])[1]; + lo = __umul128(idx1, cl, &hi); + + al1 += hi; + ah1 += lo; + + ((uint64_t*) &l1[idx1 & MASK])[0] = al1; + ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + + ah1 ^= ch; + al1 ^= cl; + idx1 = al1; + } + + cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); + cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); + + keccakf(h0, 24); + keccakf(h1, 24); + + extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); + extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); +} + +#endif /* __CRYPTONIGHT_ARM64_H__ */ diff --git a/src/crypto/CryptoNight_p.h b/src/crypto/CryptoNight_x86.h similarity index 100% rename from src/crypto/CryptoNight_p.h rename to src/crypto/CryptoNight_x86.h diff --git a/src/crypto/SSE2NEON.h b/src/crypto/SSE2NEON.h new file mode 100644 index 00000000..6a00448d --- /dev/null +++ b/src/crypto/SSE2NEON.h @@ -0,0 +1,1497 @@ +#ifndef SSE2NEON_H +#define SSE2NEON_H + +// This header file provides a simple API translation layer +// between SSE intrinsics to their corresponding ARM NEON versions +// +// This header file does not (yet) translate *all* of the SSE intrinsics. +// Since this is in support of a specific porting effort, I have only +// included the intrinsics I needed to get my port to work. +// +// Questions/Comments/Feedback send to: jratcliffscarab@gmail.com +// +// If you want to improve or add to this project, send me an +// email and I will probably approve your access to the depot. +// +// Project is located here: +// +// https://github.com/jratcliff63367/sse2neon +// +// Show your appreciation for open source by sending me a bitcoin tip to the following +// address. +// +// TipJar: 1PzgWDSyq4pmdAXRH8SPUtta4SWGrt4B1p : +// https://blockchain.info/address/1PzgWDSyq4pmdAXRH8SPUtta4SWGrt4B1p +// +// +// Contributors to this project are: +// +// John W. Ratcliff : jratcliffscarab@gmail.com +// Brandon Rowlett : browlett@nvidia.com +// Ken Fast : kfast@gdeb.com +// Eric van Beurden : evanbeurden@nvidia.com +// Alexander Potylitsin : apotylitsin@nvidia.com +// +// +// ********************************************************************************************************************* +// apoty: March 17, 2017 +// Current version was changed in most to fix issues and potential issues. +// All unit tests were rewritten as a part of forge lib project to cover all implemented functions. +// ********************************************************************************************************************* +// Release notes for January 20, 2017 version: +// +// The unit tests have been refactored. They no longer assert on an error, instead they return a pass/fail condition +// The unit-tests now test 10,000 random float and int values against each intrinsic. +// +// SSE2NEON now supports 95 SSE intrinsics. 39 of them have formal unit tests which have been implemented and +// fully tested on NEON/ARM. The remaining 56 still need unit tests implemented. +// +// A struct is now defined in this header file called 'SIMDVec' which can be used by applications which +// attempt to access the contents of an _m128 struct directly. It is important to note that accessing the __m128 +// struct directly is bad coding practice by Microsoft: @see: https://msdn.microsoft.com/en-us/library/ayeb3ayc.aspx +// +// However, some legacy source code may try to access the contents of an __m128 struct directly so the developer +// can use the SIMDVec as an alias for it. Any casting must be done manually by the developer, as you cannot +// cast or otherwise alias the base NEON data type for intrinsic operations. +// +// A bug was found with the _mm_shuffle_ps intrinsic. If the shuffle permutation was not one of the ones with +// a custom/unique implementation causing it to fall through to the default shuffle implementation it was failing +// to return the correct value. This is now fixed. +// +// A bug was found with the _mm_cvtps_epi32 intrinsic. This converts floating point values to integers. +// It was not honoring the correct rounding mode. In SSE the default rounding mode when converting from float to int +// is to use 'round to even' otherwise known as 'bankers rounding'. ARMv7 did not support this feature but ARMv8 does. +// As it stands today, this header file assumes ARMv8. If you are trying to target really old ARM devices, you may get +// a build error. +// +// Support for a number of new intrinsics was added, however, none of them yet have unit-tests to 100% confirm they are +// producing the correct results on NEON. These unit tests will be added as soon as possible. +// +// Here is the list of new instrinsics which have been added: +// +// _mm_cvtss_f32 : extracts the lower order floating point value from the parameter +// _mm_add_ss : adds the scalar single - precision floating point values of a and b +// _mm_div_ps : Divides the four single - precision, floating - point values of a and b. +// _mm_div_ss : Divides the scalar single - precision floating point value of a by b. +// _mm_sqrt_ss : Computes the approximation of the square root of the scalar single - precision floating point value of in. +// _mm_rsqrt_ps : Computes the approximations of the reciprocal square roots of the four single - precision floating point values of in. +// _mm_comilt_ss : Compares the lower single - precision floating point scalar values of a and b using a less than operation +// _mm_comigt_ss : Compares the lower single - precision floating point scalar values of a and b using a greater than operation. +// _mm_comile_ss : Compares the lower single - precision floating point scalar values of a and b using a less than or equal operation. +// _mm_comige_ss : Compares the lower single - precision floating point scalar values of a and b using a greater than or equal operation. +// _mm_comieq_ss : Compares the lower single - precision floating point scalar values of a and b using an equality operation. +// _mm_comineq_s : Compares the lower single - precision floating point scalar values of a and b using an inequality operation +// _mm_unpackhi_epi8 : Interleaves the upper 8 signed or unsigned 8 - bit integers in a with the upper 8 signed or unsigned 8 - bit integers in b. +// _mm_unpackhi_epi16: Interleaves the upper 4 signed or unsigned 16 - bit integers in a with the upper 4 signed or unsigned 16 - bit integers in b. +// +// ********************************************************************************************************************* +/* +** The MIT license: +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to deal +** in the Software without restriction, including without limitation the rights +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +** copies of the Software, and to permit persons to whom the Software is furnished +** to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in all +** copies or substantial portions of the Software. + +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#define ENABLE_CPP_VERSION 0 + +#if defined(__GNUC__) || defined(__clang__) +# pragma push_macro("FORCE_INLINE") +# pragma push_macro("ALIGN_STRUCT") +# define FORCE_INLINE static inline __attribute__((always_inline)) +# define ALIGN_STRUCT(x) __attribute__((aligned(x))) +#else +# error "Macro name collisions may happens with unknown compiler" +# define FORCE_INLINE static inline +# define ALIGN_STRUCT(x) __declspec(align(x)) +#endif + +#include +#include "arm_neon.h" + + +/*******************************************************/ +/* MACRO for shuffle parameter for _mm_shuffle_ps(). */ +/* Argument fp3 is a digit[0123] that represents the fp*/ +/* from argument "b" of mm_shuffle_ps that will be */ +/* placed in fp3 of result. fp2 is the same for fp2 in */ +/* result. fp1 is a digit[0123] that represents the fp */ +/* from argument "a" of mm_shuffle_ps that will be */ +/* places in fp1 of result. fp0 is the same for fp0 of */ +/* result */ +/*******************************************************/ +#define _MM_SHUFFLE(fp3,fp2,fp1,fp0) \ + (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0))) + +/* indicate immediate constant argument in a given range */ +#define __constrange(a,b) \ + const + +typedef float32x4_t __m128; +typedef int32x4_t __m128i; + + +// ****************************************** +// type-safe casting between types +// ****************************************** + +#define vreinterpretq_m128_f16(x) \ + vreinterpretq_f32_f16(x) + +#define vreinterpretq_m128_f32(x) \ + (x) + +#define vreinterpretq_m128_f64(x) \ + vreinterpretq_f32_f64(x) + + +#define vreinterpretq_m128_u8(x) \ + vreinterpretq_f32_u8(x) + +#define vreinterpretq_m128_u16(x) \ + vreinterpretq_f32_u16(x) + +#define vreinterpretq_m128_u32(x) \ + vreinterpretq_f32_u32(x) + +#define vreinterpretq_m128_u64(x) \ + vreinterpretq_f32_u64(x) + + +#define vreinterpretq_m128_s8(x) \ + vreinterpretq_f32_s8(x) + +#define vreinterpretq_m128_s16(x) \ + vreinterpretq_f32_s16(x) + +#define vreinterpretq_m128_s32(x) \ + vreinterpretq_f32_s32(x) + +#define vreinterpretq_m128_s64(x) \ + vreinterpretq_f32_s64(x) + + +#define vreinterpretq_f16_m128(x) \ + vreinterpretq_f16_f32(x) + +#define vreinterpretq_f32_m128(x) \ + (x) + +#define vreinterpretq_f64_m128(x) \ + vreinterpretq_f64_f32(x) + + +#define vreinterpretq_u8_m128(x) \ + vreinterpretq_u8_f32(x) + +#define vreinterpretq_u16_m128(x) \ + vreinterpretq_u16_f32(x) + +#define vreinterpretq_u32_m128(x) \ + vreinterpretq_u32_f32(x) + +#define vreinterpretq_u64_m128(x) \ + vreinterpretq_u64_f32(x) + + +#define vreinterpretq_s8_m128(x) \ + vreinterpretq_s8_f32(x) + +#define vreinterpretq_s16_m128(x) \ + vreinterpretq_s16_f32(x) + +#define vreinterpretq_s32_m128(x) \ + vreinterpretq_s32_f32(x) + +#define vreinterpretq_s64_m128(x) \ + vreinterpretq_s64_f32(x) + + +#define vreinterpretq_m128i_s8(x) \ + vreinterpretq_s32_s8(x) + +#define vreinterpretq_m128i_s16(x) \ + vreinterpretq_s32_s16(x) + +#define vreinterpretq_m128i_s32(x) \ + (x) + +#define vreinterpretq_m128i_s64(x) \ + vreinterpretq_s32_s64(x) + + +#define vreinterpretq_m128i_u8(x) \ + vreinterpretq_s32_u8(x) + +#define vreinterpretq_m128i_u16(x) \ + vreinterpretq_s32_u16(x) + +#define vreinterpretq_m128i_u32(x) \ + vreinterpretq_s32_u32(x) + +#define vreinterpretq_m128i_u64(x) \ + vreinterpretq_s32_u64(x) + + +#define vreinterpretq_s8_m128i(x) \ + vreinterpretq_s8_s32(x) + +#define vreinterpretq_s16_m128i(x) \ + vreinterpretq_s16_s32(x) + +#define vreinterpretq_s32_m128i(x) \ + (x) + +#define vreinterpretq_s64_m128i(x) \ + vreinterpretq_s64_s32(x) + + +#define vreinterpretq_u8_m128i(x) \ + vreinterpretq_u8_s32(x) + +#define vreinterpretq_u16_m128i(x) \ + vreinterpretq_u16_s32(x) + +#define vreinterpretq_u32_m128i(x) \ + vreinterpretq_u32_s32(x) + +#define vreinterpretq_u64_m128i(x) \ + vreinterpretq_u64_s32(x) + + +// union intended to allow direct access to an __m128 variable using the names that the MSVC +// compiler provides. This union should really only be used when trying to access the members +// of the vector as integer values. GCC/clang allow native access to the float members through +// a simple array access operator (in C since 4.6, in C++ since 4.8). +// +// Ideally direct accesses to SIMD vectors should not be used since it can cause a performance +// hit. If it really is needed however, the original __m128 variable can be aliased with a +// pointer to this union and used to access individual components. The use of this union should +// be hidden behind a macro that is used throughout the codebase to access the members instead +// of always declaring this type of variable. +typedef union ALIGN_STRUCT(16) SIMDVec +{ + float m128_f32[4]; // as floats - do not to use this. Added for convenience. + int8_t m128_i8[16]; // as signed 8-bit integers. + int16_t m128_i16[8]; // as signed 16-bit integers. + int32_t m128_i32[4]; // as signed 32-bit integers. + int64_t m128_i64[2]; // as signed 64-bit integers. + uint8_t m128_u8[16]; // as unsigned 8-bit integers. + uint16_t m128_u16[8]; // as unsigned 16-bit integers. + uint32_t m128_u32[4]; // as unsigned 32-bit integers. + uint64_t m128_u64[2]; // as unsigned 64-bit integers. +} SIMDVec; + + +// ****************************************** +// Set/get methods +// ****************************************** + +// extracts the lower order floating point value from the parameter : https://msdn.microsoft.com/en-us/library/bb514059%28v=vs.120%29.aspx?f=255&MSPPError=-2147217396 +FORCE_INLINE float _mm_cvtss_f32(__m128 a) +{ + return vgetq_lane_f32(vreinterpretq_f32_m128(a), 0); +} + +// Sets the 128-bit value to zero https://msdn.microsoft.com/en-us/library/vstudio/ys7dw0kh(v=vs.100).aspx +FORCE_INLINE __m128i _mm_setzero_si128() +{ + return vreinterpretq_m128i_s32(vdupq_n_s32(0)); +} + +// Clears the four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/vstudio/tk1t2tbz(v=vs.100).aspx +FORCE_INLINE __m128 _mm_setzero_ps(void) +{ + return vreinterpretq_m128_f32(vdupq_n_f32(0)); +} + +// Sets the four single-precision, floating-point values to w. https://msdn.microsoft.com/en-us/library/vstudio/2x1se8ha(v=vs.100).aspx +FORCE_INLINE __m128 _mm_set1_ps(float _w) +{ + return vreinterpretq_m128_f32(vdupq_n_f32(_w)); +} + +// Sets the four single-precision, floating-point values to w. https://msdn.microsoft.com/en-us/library/vstudio/2x1se8ha(v=vs.100).aspx +FORCE_INLINE __m128 _mm_set_ps1(float _w) +{ + return vreinterpretq_m128_f32(vdupq_n_f32(_w)); +} + +// Sets the four single-precision, floating-point values to the four inputs. https://msdn.microsoft.com/en-us/library/vstudio/afh0zf75(v=vs.100).aspx +FORCE_INLINE __m128 _mm_set_ps(float w, float z, float y, float x) +{ + float __attribute__((aligned(16))) data[4] = { x, y, z, w }; + return vreinterpretq_m128_f32(vld1q_f32(data)); +} + +// Sets the four single-precision, floating-point values to the four inputs in reverse order. https://msdn.microsoft.com/en-us/library/vstudio/d2172ct3(v=vs.100).aspx +FORCE_INLINE __m128 _mm_setr_ps(float w, float z , float y , float x ) +{ + float __attribute__ ((aligned (16))) data[4] = { w, z, y, x }; + return vreinterpretq_m128_f32(vld1q_f32(data)); +} + +// Sets the 4 signed 32-bit integer values to i. https://msdn.microsoft.com/en-us/library/vstudio/h4xscxat(v=vs.100).aspx +FORCE_INLINE __m128i _mm_set1_epi32(int _i) +{ + return vreinterpretq_m128i_s32(vdupq_n_s32(_i)); +} + +// Sets the 4 signed 32-bit integer values. https://msdn.microsoft.com/en-us/library/vstudio/019beekt(v=vs.100).aspx +FORCE_INLINE __m128i _mm_set_epi32(int i3, int i2, int i1, int i0) +{ + int32_t __attribute__((aligned(16))) data[4] = { i0, i1, i2, i3 }; + return vreinterpretq_m128i_s32(vld1q_s32(data)); +} + +// Stores four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/vstudio/s3h4ay6y(v=vs.100).aspx +FORCE_INLINE void _mm_store_ps(float *p, __m128 a) +{ + vst1q_f32(p, vreinterpretq_f32_m128(a)); +} + +// Stores four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/44e30x22(v=vs.100).aspx +FORCE_INLINE void _mm_storeu_ps(float *p, __m128 a) +{ + vst1q_f32(p, vreinterpretq_f32_m128(a)); +} + +// Stores four 32-bit integer values as (as a __m128i value) at the address p. https://msdn.microsoft.com/en-us/library/vstudio/edk11s13(v=vs.100).aspx +FORCE_INLINE void _mm_store_si128(__m128i *p, __m128i a) +{ + vst1q_s32((int32_t*) p, vreinterpretq_s32_m128i(a)); +} + +// Stores the lower single - precision, floating - point value. https://msdn.microsoft.com/en-us/library/tzz10fbx(v=vs.100).aspx +FORCE_INLINE void _mm_store_ss(float *p, __m128 a) +{ + vst1q_lane_f32(p, vreinterpretq_f32_m128(a), 0); +} + +// Reads the lower 64 bits of b and stores them into the lower 64 bits of a. https://msdn.microsoft.com/en-us/library/hhwf428f%28v=vs.90%29.aspx +FORCE_INLINE void _mm_storel_epi64(__m128i* a, __m128i b) +{ + uint64x1_t hi = vget_high_u64(vreinterpretq_u64_m128i(*a)); + uint64x1_t lo = vget_low_u64(vreinterpretq_u64_m128i(b)); + *a = vreinterpretq_m128i_u64(vcombine_u64(lo, hi)); +} + +// Loads a single single-precision, floating-point value, copying it into all four words https://msdn.microsoft.com/en-us/library/vstudio/5cdkf716(v=vs.100).aspx +FORCE_INLINE __m128 _mm_load1_ps(const float * p) +{ + return vreinterpretq_m128_f32(vld1q_dup_f32(p)); +} + +// Loads four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/vstudio/zzd50xxt(v=vs.100).aspx +FORCE_INLINE __m128 _mm_load_ps(const float * p) +{ + return vreinterpretq_m128_f32(vld1q_f32(p)); +} + +// Loads four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/x1b16s7z%28v=vs.90%29.aspx +FORCE_INLINE __m128 _mm_loadu_ps(const float * p) +{ + // for neon, alignment doesn't matter, so _mm_load_ps and _mm_loadu_ps are equivalent for neon + return vreinterpretq_m128_f32(vld1q_f32(p)); +} + +// Loads an single - precision, floating - point value into the low word and clears the upper three words. https://msdn.microsoft.com/en-us/library/548bb9h4%28v=vs.90%29.aspx +FORCE_INLINE __m128 _mm_load_ss(const float * p) +{ + return vreinterpretq_m128_f32(vsetq_lane_f32(*p, vdupq_n_f32(0), 0)); +} + + +// ****************************************** +// Logic/Binary operations +// ****************************************** + +// Compares for inequality. https://msdn.microsoft.com/en-us/library/sf44thbx(v=vs.100).aspx +FORCE_INLINE __m128 _mm_cmpneq_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_u32( vmvnq_u32( vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)) ) ); +} + +// Computes the bitwise AND-NOT of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/68h7wd02(v=vs.100).aspx +FORCE_INLINE __m128 _mm_andnot_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_s32( vbicq_s32(vreinterpretq_s32_m128(b), vreinterpretq_s32_m128(a)) ); // *NOTE* argument swap +} + +// Computes the bitwise AND of the 128-bit value in b and the bitwise NOT of the 128-bit value in a. https://msdn.microsoft.com/en-us/library/vstudio/1beaceh8(v=vs.100).aspx +FORCE_INLINE __m128i _mm_andnot_si128(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s32( vbicq_s32(vreinterpretq_s32_m128i(b), vreinterpretq_s32_m128i(a)) ); // *NOTE* argument swap +} + +// Computes the bitwise AND of the 128-bit value in a and the 128-bit value in b. https://msdn.microsoft.com/en-us/library/vstudio/6d1txsa8(v=vs.100).aspx +FORCE_INLINE __m128i _mm_and_si128(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s32( vandq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)) ); +} + +// Computes the bitwise AND of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/73ck1xc5(v=vs.100).aspx +FORCE_INLINE __m128 _mm_and_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_s32( vandq_s32(vreinterpretq_s32_m128(a), vreinterpretq_s32_m128(b)) ); +} + +// Computes the bitwise OR of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/7ctdsyy0(v=vs.100).aspx +FORCE_INLINE __m128 _mm_or_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_s32( vorrq_s32(vreinterpretq_s32_m128(a), vreinterpretq_s32_m128(b)) ); +} + +// Computes bitwise EXOR (exclusive-or) of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/ss6k3wk8(v=vs.100).aspx +FORCE_INLINE __m128 _mm_xor_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_s32( veorq_s32(vreinterpretq_s32_m128(a), vreinterpretq_s32_m128(b)) ); +} + +// Computes the bitwise OR of the 128-bit value in a and the 128-bit value in b. https://msdn.microsoft.com/en-us/library/vstudio/ew8ty0db(v=vs.100).aspx +FORCE_INLINE __m128i _mm_or_si128(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s32( vorrq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)) ); +} + +// Computes the bitwise XOR of the 128-bit value in a and the 128-bit value in b. https://msdn.microsoft.com/en-us/library/fzt08www(v=vs.100).aspx +FORCE_INLINE __m128i _mm_xor_si128(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s32( veorq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)) ); +} + +// NEON does not provide this method +// Creates a 4-bit mask from the most significant bits of the four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/vstudio/4490ys29(v=vs.100).aspx +FORCE_INLINE int _mm_movemask_ps(__m128 a) +{ +#if ENABLE_CPP_VERSION // I am not yet convinced that the NEON version is faster than the C version of this + uint32x4_t &ia = *(uint32x4_t *)&a; + return (ia[0] >> 31) | ((ia[1] >> 30) & 2) | ((ia[2] >> 29) & 4) | ((ia[3] >> 28) & 8); +#else + static const uint32x4_t movemask = { 1, 2, 4, 8 }; + static const uint32x4_t highbit = { 0x80000000, 0x80000000, 0x80000000, 0x80000000 }; + uint32x4_t t0 = vreinterpretq_u32_m128(a); + uint32x4_t t1 = vtstq_u32(t0, highbit); + uint32x4_t t2 = vandq_u32(t1, movemask); + uint32x2_t t3 = vorr_u32(vget_low_u32(t2), vget_high_u32(t2)); + return vget_lane_u32(t3, 0) | vget_lane_u32(t3, 1); +#endif +} + +// Takes the upper 64 bits of a and places it in the low end of the result +// Takes the lower 64 bits of b and places it into the high end of the result. +FORCE_INLINE __m128 _mm_shuffle_ps_1032(__m128 a, __m128 b) +{ + float32x2_t a32 = vget_high_f32(vreinterpretq_f32_m128(a)); + float32x2_t b10 = vget_low_f32(vreinterpretq_f32_m128(b)); + return vreinterpretq_m128_f32(vcombine_f32(a32, b10)); +} + +// takes the lower two 32-bit values from a and swaps them and places in high end of result +// takes the higher two 32 bit values from b and swaps them and places in low end of result. +FORCE_INLINE __m128 _mm_shuffle_ps_2301(__m128 a, __m128 b) +{ + float32x2_t a01 = vrev64_f32(vget_low_f32(vreinterpretq_f32_m128(a))); + float32x2_t b23 = vrev64_f32(vget_high_f32(vreinterpretq_f32_m128(b))); + return vreinterpretq_m128_f32(vcombine_f32(a01, b23)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_0321(__m128 a, __m128 b) +{ + float32x2_t a21 = vget_high_f32(vextq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a), 3)); + float32x2_t b03 = vget_low_f32(vextq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b), 3)); + return vreinterpretq_m128_f32(vcombine_f32(a21, b03)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_2103(__m128 a, __m128 b) +{ + float32x2_t a03 = vget_low_f32(vextq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a), 3)); + float32x2_t b21 = vget_high_f32(vextq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b), 3)); + return vreinterpretq_m128_f32(vcombine_f32(a03, b21)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_1010(__m128 a, __m128 b) +{ + float32x2_t a10 = vget_low_f32(vreinterpretq_f32_m128(a)); + float32x2_t b10 = vget_low_f32(vreinterpretq_f32_m128(b)); + return vreinterpretq_m128_f32(vcombine_f32(a10, b10)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_1001(__m128 a, __m128 b) +{ + float32x2_t a01 = vrev64_f32(vget_low_f32(vreinterpretq_f32_m128(a))); + float32x2_t b10 = vget_low_f32(vreinterpretq_f32_m128(b)); + return vreinterpretq_m128_f32(vcombine_f32(a01, b10)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_0101(__m128 a, __m128 b) +{ + float32x2_t a01 = vrev64_f32(vget_low_f32(vreinterpretq_f32_m128(a))); + float32x2_t b01 = vrev64_f32(vget_low_f32(vreinterpretq_f32_m128(b))); + return vreinterpretq_m128_f32(vcombine_f32(a01, b01)); +} + +// keeps the low 64 bits of b in the low and puts the high 64 bits of a in the high +FORCE_INLINE __m128 _mm_shuffle_ps_3210(__m128 a, __m128 b) +{ + float32x2_t a10 = vget_low_f32(vreinterpretq_f32_m128(a)); + float32x2_t b32 = vget_high_f32(vreinterpretq_f32_m128(b)); + return vreinterpretq_m128_f32(vcombine_f32(a10, b32)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_0011(__m128 a, __m128 b) +{ + float32x2_t a11 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(a)), 1); + float32x2_t b00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 0); + return vreinterpretq_m128_f32(vcombine_f32(a11, b00)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_0022(__m128 a, __m128 b) +{ + float32x2_t a22 = vdup_lane_f32(vget_high_f32(vreinterpretq_f32_m128(a)), 0); + float32x2_t b00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 0); + return vreinterpretq_m128_f32(vcombine_f32(a22, b00)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_2200(__m128 a, __m128 b) +{ + float32x2_t a00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(a)), 0); + float32x2_t b22 = vdup_lane_f32(vget_high_f32(vreinterpretq_f32_m128(b)), 0); + return vreinterpretq_m128_f32(vcombine_f32(a00, b22)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_3202(__m128 a, __m128 b) +{ + float32_t a0 = vgetq_lane_f32(vreinterpretq_f32_m128(a), 0); + float32x2_t a22 = vdup_lane_f32(vget_high_f32(vreinterpretq_f32_m128(a)), 0); + float32x2_t a02 = vset_lane_f32(a0, a22, 1); /* apoty: TODO: use vzip ?*/ + float32x2_t b32 = vget_high_f32(vreinterpretq_f32_m128(b)); + return vreinterpretq_m128_f32(vcombine_f32(a02, b32)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_1133(__m128 a, __m128 b) +{ + float32x2_t a33 = vdup_lane_f32(vget_high_f32(vreinterpretq_f32_m128(a)), 1); + float32x2_t b11 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 1); + return vreinterpretq_m128_f32(vcombine_f32(a33, b11)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_2010(__m128 a, __m128 b) +{ + float32x2_t a10 = vget_low_f32(vreinterpretq_f32_m128(a)); + float32_t b2 = vgetq_lane_f32(vreinterpretq_f32_m128(b), 2); + float32x2_t b00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 0); + float32x2_t b20 = vset_lane_f32(b2, b00, 1); + return vreinterpretq_m128_f32(vcombine_f32(a10, b20)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_2001(__m128 a, __m128 b) +{ + float32x2_t a01 = vrev64_f32(vget_low_f32(vreinterpretq_f32_m128(a))); + float32_t b2 = vgetq_lane_f32(b, 2); + float32x2_t b00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 0); + float32x2_t b20 = vset_lane_f32(b2, b00, 1); + return vreinterpretq_m128_f32(vcombine_f32(a01, b20)); +} + +FORCE_INLINE __m128 _mm_shuffle_ps_2032(__m128 a, __m128 b) +{ + float32x2_t a32 = vget_high_f32(vreinterpretq_f32_m128(a)); + float32_t b2 = vgetq_lane_f32(b, 2); + float32x2_t b00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 0); + float32x2_t b20 = vset_lane_f32(b2, b00, 1); + return vreinterpretq_m128_f32(vcombine_f32(a32, b20)); +} + +// NEON does not support a general purpose permute intrinsic +// Currently I am not sure whether the C implementation is faster or slower than the NEON version. +// Note, this has to be expanded as a template because the shuffle value must be an immediate value. +// The same is true on SSE as well. +// Selects four specific single-precision, floating-point values from a and b, based on the mask i. https://msdn.microsoft.com/en-us/library/vstudio/5f0858x0(v=vs.100).aspx +#if ENABLE_CPP_VERSION // I am not convinced that the NEON version is faster than the C version yet. +FORCE_INLINE __m128 _mm_shuffle_ps_default(__m128 a, __m128 b, __constrange(0,255) int imm) +{ + __m128 ret; + ret[0] = a[imm & 0x3]; + ret[1] = a[(imm >> 2) & 0x3]; + ret[2] = b[(imm >> 4) & 0x03]; + ret[3] = b[(imm >> 6) & 0x03]; + return ret; +} +#else +#define _mm_shuffle_ps_default(a, b, imm) \ +({ \ + float32x4_t ret; \ + ret = vmovq_n_f32(vgetq_lane_f32(vreinterpretq_f32_m128(a), (imm) & 0x3)); \ + ret = vsetq_lane_f32(vgetq_lane_f32(vreinterpretq_f32_m128(a), ((imm) >> 2) & 0x3), ret, 1); \ + ret = vsetq_lane_f32(vgetq_lane_f32(vreinterpretq_f32_m128(b), ((imm) >> 4) & 0x3), ret, 2); \ + ret = vsetq_lane_f32(vgetq_lane_f32(vreinterpretq_f32_m128(b), ((imm) >> 6) & 0x3), ret, 3); \ + vreinterpretq_m128_f32(ret); \ +}) +#endif + +//FORCE_INLINE __m128 _mm_shuffle_ps(__m128 a, __m128 b, __constrange(0,255) int imm) +#define _mm_shuffle_ps(a, b, imm) \ +({ \ + __m128 ret; \ + switch (imm) \ + { \ + case _MM_SHUFFLE(1, 0, 3, 2): ret = _mm_shuffle_ps_1032((a), (b)); break; \ + case _MM_SHUFFLE(2, 3, 0, 1): ret = _mm_shuffle_ps_2301((a), (b)); break; \ + case _MM_SHUFFLE(0, 3, 2, 1): ret = _mm_shuffle_ps_0321((a), (b)); break; \ + case _MM_SHUFFLE(2, 1, 0, 3): ret = _mm_shuffle_ps_2103((a), (b)); break; \ + case _MM_SHUFFLE(1, 0, 1, 0): ret = _mm_shuffle_ps_1010((a), (b)); break; \ + case _MM_SHUFFLE(1, 0, 0, 1): ret = _mm_shuffle_ps_1001((a), (b)); break; \ + case _MM_SHUFFLE(0, 1, 0, 1): ret = _mm_shuffle_ps_0101((a), (b)); break; \ + case _MM_SHUFFLE(3, 2, 1, 0): ret = _mm_shuffle_ps_3210((a), (b)); break; \ + case _MM_SHUFFLE(0, 0, 1, 1): ret = _mm_shuffle_ps_0011((a), (b)); break; \ + case _MM_SHUFFLE(0, 0, 2, 2): ret = _mm_shuffle_ps_0022((a), (b)); break; \ + case _MM_SHUFFLE(2, 2, 0, 0): ret = _mm_shuffle_ps_2200((a), (b)); break; \ + case _MM_SHUFFLE(3, 2, 0, 2): ret = _mm_shuffle_ps_3202((a), (b)); break; \ + case _MM_SHUFFLE(1, 1, 3, 3): ret = _mm_shuffle_ps_1133((a), (b)); break; \ + case _MM_SHUFFLE(2, 0, 1, 0): ret = _mm_shuffle_ps_2010((a), (b)); break; \ + case _MM_SHUFFLE(2, 0, 0, 1): ret = _mm_shuffle_ps_2001((a), (b)); break; \ + case _MM_SHUFFLE(2, 0, 3, 2): ret = _mm_shuffle_ps_2032((a), (b)); break; \ + default: ret = _mm_shuffle_ps_default((a), (b), (imm)); break; \ + } \ + ret; \ +}) + +// Takes the upper 64 bits of a and places it in the low end of the result +// Takes the lower 64 bits of a and places it into the high end of the result. +FORCE_INLINE __m128i _mm_shuffle_epi_1032(__m128i a) +{ + int32x2_t a32 = vget_high_s32(vreinterpretq_s32_m128i(a)); + int32x2_t a10 = vget_low_s32(vreinterpretq_s32_m128i(a)); + return vreinterpretq_m128i_s32(vcombine_s32(a32, a10)); +} + +// takes the lower two 32-bit values from a and swaps them and places in low end of result +// takes the higher two 32 bit values from a and swaps them and places in high end of result. +FORCE_INLINE __m128i _mm_shuffle_epi_2301(__m128i a) +{ + int32x2_t a01 = vrev64_s32(vget_low_s32(vreinterpretq_s32_m128i(a))); + int32x2_t a23 = vrev64_s32(vget_high_s32(vreinterpretq_s32_m128i(a))); + return vreinterpretq_m128i_s32(vcombine_s32(a01, a23)); +} + +// rotates the least significant 32 bits into the most signficant 32 bits, and shifts the rest down +FORCE_INLINE __m128i _mm_shuffle_epi_0321(__m128i a) +{ + return vreinterpretq_m128i_s32(vextq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(a), 1)); +} + +// rotates the most significant 32 bits into the least signficant 32 bits, and shifts the rest up +FORCE_INLINE __m128i _mm_shuffle_epi_2103(__m128i a) +{ + return vreinterpretq_m128i_s32(vextq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(a), 3)); +} + +// gets the lower 64 bits of a, and places it in the upper 64 bits +// gets the lower 64 bits of a and places it in the lower 64 bits +FORCE_INLINE __m128i _mm_shuffle_epi_1010(__m128i a) +{ + int32x2_t a10 = vget_low_s32(vreinterpretq_s32_m128i(a)); + return vreinterpretq_m128i_s32(vcombine_s32(a10, a10)); +} + +// gets the lower 64 bits of a, swaps the 0 and 1 elements, and places it in the lower 64 bits +// gets the lower 64 bits of a, and places it in the upper 64 bits +FORCE_INLINE __m128i _mm_shuffle_epi_1001(__m128i a) +{ + int32x2_t a01 = vrev64_s32(vget_low_s32(vreinterpretq_s32_m128i(a))); + int32x2_t a10 = vget_low_s32(vreinterpretq_s32_m128i(a)); + return vreinterpretq_m128i_s32(vcombine_s32(a01, a10)); +} + +// gets the lower 64 bits of a, swaps the 0 and 1 elements and places it in the upper 64 bits +// gets the lower 64 bits of a, swaps the 0 and 1 elements, and places it in the lower 64 bits +FORCE_INLINE __m128i _mm_shuffle_epi_0101(__m128i a) +{ + int32x2_t a01 = vrev64_s32(vget_low_s32(vreinterpretq_s32_m128i(a))); + return vreinterpretq_m128i_s32(vcombine_s32(a01, a01)); +} + +FORCE_INLINE __m128i _mm_shuffle_epi_2211(__m128i a) +{ + int32x2_t a11 = vdup_lane_s32(vget_low_s32(vreinterpretq_s32_m128i(a)), 1); + int32x2_t a22 = vdup_lane_s32(vget_high_s32(vreinterpretq_s32_m128i(a)), 0); + return vreinterpretq_m128i_s32(vcombine_s32(a11, a22)); +} + +FORCE_INLINE __m128i _mm_shuffle_epi_0122(__m128i a) +{ + int32x2_t a22 = vdup_lane_s32(vget_high_s32(vreinterpretq_s32_m128i(a)), 0); + int32x2_t a01 = vrev64_s32(vget_low_s32(vreinterpretq_s32_m128i(a))); + return vreinterpretq_m128i_s32(vcombine_s32(a22, a01)); +} + +FORCE_INLINE __m128i _mm_shuffle_epi_3332(__m128i a) +{ + int32x2_t a32 = vget_high_s32(vreinterpretq_s32_m128i(a)); + int32x2_t a33 = vdup_lane_s32(vget_high_s32(vreinterpretq_s32_m128i(a)), 1); + return vreinterpretq_m128i_s32(vcombine_s32(a32, a33)); +} + +//FORCE_INLINE __m128i _mm_shuffle_epi32_default(__m128i a, __constrange(0,255) int imm) +#if ENABLE_CPP_VERSION +FORCE_INLINE __m128i _mm_shuffle_epi32_default(__m128i a, __constrange(0,255) int imm) +{ + __m128i ret; + ret[0] = a[imm & 0x3]; + ret[1] = a[(imm >> 2) & 0x3]; + ret[2] = a[(imm >> 4) & 0x03]; + ret[3] = a[(imm >> 6) & 0x03]; + return ret; +} +#else +#define _mm_shuffle_epi32_default(a, imm) \ +({ \ + int32x4_t ret; \ + ret = vmovq_n_s32(vgetq_lane_s32(vreinterpretq_s32_m128i(a), (imm) & 0x3)); \ + ret = vsetq_lane_s32(vgetq_lane_s32(vreinterpretq_s32_m128i(a), ((imm) >> 2) & 0x3), ret, 1); \ + ret = vsetq_lane_s32(vgetq_lane_s32(vreinterpretq_s32_m128i(a), ((imm) >> 4) & 0x3), ret, 2); \ + ret = vsetq_lane_s32(vgetq_lane_s32(vreinterpretq_s32_m128i(a), ((imm) >> 6) & 0x3), ret, 3); \ + vreinterpretq_m128i_s32(ret); \ +}) +#endif + +//FORCE_INLINE __m128i _mm_shuffle_epi32_splat(__m128i a, __constrange(0,255) int imm) +#if defined(__aarch64__) +#define _mm_shuffle_epi32_splat(a, imm) \ +({ \ + vreinterpretq_m128i_s32(vdupq_laneq_s32(vreinterpretq_s32_m128i(a), (imm))); \ +}) +#else +#define _mm_shuffle_epi32_splat(a, imm) \ +({ \ + vreinterpretq_m128i_s32(vdupq_n_s32(vgetq_lane_s32(vreinterpretq_s32_m128i(a), (imm)))); \ +}) +#endif + +// Shuffles the 4 signed or unsigned 32-bit integers in a as specified by imm. https://msdn.microsoft.com/en-us/library/56f67xbk%28v=vs.90%29.aspx +//FORCE_INLINE __m128i _mm_shuffle_epi32(__m128i a, __constrange(0,255) int imm) +#define _mm_shuffle_epi32(a, imm) \ +({ \ + __m128i ret; \ + switch (imm) \ + { \ + case _MM_SHUFFLE(1, 0, 3, 2): ret = _mm_shuffle_epi_1032((a)); break; \ + case _MM_SHUFFLE(2, 3, 0, 1): ret = _mm_shuffle_epi_2301((a)); break; \ + case _MM_SHUFFLE(0, 3, 2, 1): ret = _mm_shuffle_epi_0321((a)); break; \ + case _MM_SHUFFLE(2, 1, 0, 3): ret = _mm_shuffle_epi_2103((a)); break; \ + case _MM_SHUFFLE(1, 0, 1, 0): ret = _mm_shuffle_epi_1010((a)); break; \ + case _MM_SHUFFLE(1, 0, 0, 1): ret = _mm_shuffle_epi_1001((a)); break; \ + case _MM_SHUFFLE(0, 1, 0, 1): ret = _mm_shuffle_epi_0101((a)); break; \ + case _MM_SHUFFLE(2, 2, 1, 1): ret = _mm_shuffle_epi_2211((a)); break; \ + case _MM_SHUFFLE(0, 1, 2, 2): ret = _mm_shuffle_epi_0122((a)); break; \ + case _MM_SHUFFLE(3, 3, 3, 2): ret = _mm_shuffle_epi_3332((a)); break; \ + case _MM_SHUFFLE(0, 0, 0, 0): ret = _mm_shuffle_epi32_splat((a),0); break; \ + case _MM_SHUFFLE(1, 1, 1, 1): ret = _mm_shuffle_epi32_splat((a),1); break; \ + case _MM_SHUFFLE(2, 2, 2, 2): ret = _mm_shuffle_epi32_splat((a),2); break; \ + case _MM_SHUFFLE(3, 3, 3, 3): ret = _mm_shuffle_epi32_splat((a),3); break; \ + default: ret = _mm_shuffle_epi32_default((a), (imm)); break; \ + } \ + ret; \ +}) + +// Shuffles the upper 4 signed or unsigned 16 - bit integers in a as specified by imm. https://msdn.microsoft.com/en-us/library/13ywktbs(v=vs.100).aspx +//FORCE_INLINE __m128i _mm_shufflehi_epi16_function(__m128i a, __constrange(0,255) int imm) +#define _mm_shufflehi_epi16_function(a, imm) \ +({ \ + int16x8_t ret = vreinterpretq_s16_s32(a); \ + int16x4_t highBits = vget_high_s16(ret); \ + ret = vsetq_lane_s16(vget_lane_s16(highBits, (imm) & 0x3), ret, 4); \ + ret = vsetq_lane_s16(vget_lane_s16(highBits, ((imm) >> 2) & 0x3), ret, 5); \ + ret = vsetq_lane_s16(vget_lane_s16(highBits, ((imm) >> 4) & 0x3), ret, 6); \ + ret = vsetq_lane_s16(vget_lane_s16(highBits, ((imm) >> 6) & 0x3), ret, 7); \ + vreinterpretq_s32_s16(ret); \ +}) + +//FORCE_INLINE __m128i _mm_shufflehi_epi16(__m128i a, __constrange(0,255) int imm) +#define _mm_shufflehi_epi16(a, imm) \ + _mm_shufflehi_epi16_function((a), (imm)) + + +// Shifts the 4 signed or unsigned 32-bit integers in a left by count bits while shifting in zeros. : https://msdn.microsoft.com/en-us/library/z2k3bbtb%28v=vs.90%29.aspx +//FORCE_INLINE __m128i _mm_slli_epi32(__m128i a, __constrange(0,255) int imm) +#define _mm_slli_epi32(a, imm) \ +({ \ + __m128i ret; \ + if ((imm) <= 0) {\ + ret = a; \ + } \ + else if ((imm) > 31) { \ + ret = _mm_setzero_si128(); \ + } \ + else { \ + ret = vreinterpretq_m128i_s32(vshlq_n_s32(vreinterpretq_s32_m128i(a), (imm))); \ + } \ + ret; \ +}) + +//Shifts the 4 signed or unsigned 32-bit integers in a right by count bits while shifting in zeros. https://msdn.microsoft.com/en-us/library/w486zcfa(v=vs.100).aspx +//FORCE_INLINE __m128i _mm_srli_epi32(__m128i a, __constrange(0,255) int imm) +#define _mm_srli_epi32(a, imm) \ +({ \ + __m128i ret; \ + if ((imm) <= 0) { \ + ret = a; \ + } \ + else if ((imm)> 31) { \ + ret = _mm_setzero_si128(); \ + } \ + else { \ + ret = vreinterpretq_m128i_u32(vshrq_n_u32(vreinterpretq_u32_m128i(a), (imm))); \ + } \ + ret; \ +}) + +// Shifts the 4 signed 32 - bit integers in a right by count bits while shifting in the sign bit. https://msdn.microsoft.com/en-us/library/z1939387(v=vs.100).aspx +//FORCE_INLINE __m128i _mm_srai_epi32(__m128i a, __constrange(0,255) int imm) +#define _mm_srai_epi32(a, imm) \ +({ \ + __m128i ret; \ + if ((imm) <= 0) { \ + ret = a; \ + } \ + else if ((imm) > 31) { \ + ret = vreinterpretq_m128i_s32(vshrq_n_s32(vreinterpretq_s32_m128i(a), 16)); \ + ret = vreinterpretq_m128i_s32(vshrq_n_s32(vreinterpretq_s32_m128i(ret), 16)); \ + } \ + else { \ + ret = vreinterpretq_m128i_s32(vshrq_n_s32(vreinterpretq_s32_m128i(a), (imm))); \ + } \ + ret; \ +}) + +// Shifts the 128 - bit value in a right by imm bytes while shifting in zeros.imm must be an immediate. https://msdn.microsoft.com/en-us/library/305w28yz(v=vs.100).aspx +//FORCE_INLINE _mm_srli_si128(__m128i a, __constrange(0,255) int imm) +#define _mm_srli_si128(a, imm) \ +({ \ + __m128i ret; \ + if ((imm) <= 0) { \ + ret = a; \ + } \ + else if ((imm) > 15) { \ + ret = _mm_setzero_si128(); \ + } \ + else { \ + ret = vreinterpretq_m128i_s8(vextq_s8(vreinterpretq_s8_m128i(a), vdupq_n_s8(0), (imm))); \ + } \ + ret; \ +}) + +// Shifts the 128-bit value in a left by imm bytes while shifting in zeros. imm must be an immediate. https://msdn.microsoft.com/en-us/library/34d3k2kt(v=vs.100).aspx +//FORCE_INLINE __m128i _mm_slli_si128(__m128i a, __constrange(0,255) int imm) +#define _mm_slli_si128(a, imm) \ +({ \ + __m128i ret; \ + if ((imm) <= 0) { \ + ret = a; \ + } \ + else if ((imm) > 15) { \ + ret = _mm_setzero_si128(); \ + } \ + else { \ + ret = vreinterpretq_m128i_s8(vextq_s8(vdupq_n_s8(0), vreinterpretq_s8_m128i(a), 16 - (imm))); \ + } \ + ret; \ +}) + +// NEON does not provide a version of this function, here is an article about some ways to repro the results. +// http://stackoverflow.com/questions/11870910/sse-mm-movemask-epi8-equivalent-method-for-arm-neon +// Creates a 16-bit mask from the most significant bits of the 16 signed or unsigned 8-bit integers in a and zero extends the upper bits. https://msdn.microsoft.com/en-us/library/vstudio/s090c8fk(v=vs.100).aspx +FORCE_INLINE int _mm_movemask_epi8(__m128i _a) +{ + uint8x16_t input = vreinterpretq_u8_m128i(_a); + static const int8_t __attribute__((aligned(16))) xr[8] = { -7, -6, -5, -4, -3, -2, -1, 0 }; + uint8x8_t mask_and = vdup_n_u8(0x80); + int8x8_t mask_shift = vld1_s8(xr); + + uint8x8_t lo = vget_low_u8(input); + uint8x8_t hi = vget_high_u8(input); + + lo = vand_u8(lo, mask_and); + lo = vshl_u8(lo, mask_shift); + + hi = vand_u8(hi, mask_and); + hi = vshl_u8(hi, mask_shift); + + lo = vpadd_u8(lo, lo); + lo = vpadd_u8(lo, lo); + lo = vpadd_u8(lo, lo); + + hi = vpadd_u8(hi, hi); + hi = vpadd_u8(hi, hi); + hi = vpadd_u8(hi, hi); + + return ((hi[0] << 8) | (lo[0] & 0xFF)); +} + + +// ****************************************** +// Math operations +// ****************************************** + +// Subtracts the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/1zad2k61(v=vs.100).aspx +FORCE_INLINE __m128 _mm_sub_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_f32(vsubq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +// Subtracts the 4 signed or unsigned 32-bit integers of b from the 4 signed or unsigned 32-bit integers of a. https://msdn.microsoft.com/en-us/library/vstudio/fhh866h0(v=vs.100).aspx +FORCE_INLINE __m128i _mm_sub_epi32(__m128i a, __m128i b) +{ + return vreinterpretq_m128_f32(vsubq_s32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +FORCE_INLINE __m128i _mm_sub_epi16(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s16(vsubq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b))); +} + +// Adds the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/c9848chc(v=vs.100).aspx +FORCE_INLINE __m128 _mm_add_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_f32(vaddq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +// adds the scalar single-precision floating point values of a and b. https://msdn.microsoft.com/en-us/library/be94x2y6(v=vs.100).aspx +FORCE_INLINE __m128 _mm_add_ss(__m128 a, __m128 b) +{ + float32_t b0 = vgetq_lane_f32(vreinterpretq_f32_m128(b), 0); + float32x4_t value = vsetq_lane_f32(b0, vdupq_n_f32(0), 0); + //the upper values in the result must be the remnants of . + return vreinterpretq_m128_f32(vaddq_f32(a, value)); +} + +// Adds the 4 signed or unsigned 32-bit integers in a to the 4 signed or unsigned 32-bit integers in b. https://msdn.microsoft.com/en-us/library/vstudio/09xs4fkk(v=vs.100).aspx +FORCE_INLINE __m128i _mm_add_epi32(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s32(vaddq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b))); +} + +// Adds the 8 signed or unsigned 16-bit integers in a to the 8 signed or unsigned 16-bit integers in b. https://msdn.microsoft.com/en-us/library/fceha5k4(v=vs.100).aspx +FORCE_INLINE __m128i _mm_add_epi16(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s16(vaddq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b))); +} + +// Multiplies the 8 signed or unsigned 16-bit integers from a by the 8 signed or unsigned 16-bit integers from b. https://msdn.microsoft.com/en-us/library/vstudio/9ks1472s(v=vs.100).aspx +FORCE_INLINE __m128i _mm_mullo_epi16(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s16(vmulq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b))); +} + +// Multiplies the 4 signed or unsigned 32-bit integers from a by the 4 signed or unsigned 32-bit integers from b. https://msdn.microsoft.com/en-us/library/vstudio/bb531409(v=vs.100).aspx +FORCE_INLINE __m128i _mm_mullo_epi32(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s32(vmulq_s32(vreinterpretq_s32_m128i(a),vreinterpretq_s32_m128i(b))); +} + +// Multiplies the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/22kbk6t9(v=vs.100).aspx +FORCE_INLINE __m128 _mm_mul_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_f32(vmulq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +// Divides the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/edaw8147(v=vs.100).aspx +FORCE_INLINE __m128 _mm_div_ps(__m128 a, __m128 b) +{ + float32x4_t recip0 = vrecpeq_f32(vreinterpretq_f32_m128(b)); + float32x4_t recip1 = vmulq_f32(recip0, vrecpsq_f32(recip0, vreinterpretq_f32_m128(b))); + return vreinterpretq_m128_f32(vmulq_f32(vreinterpretq_f32_m128(a), recip1)); +} + +// Divides the scalar single-precision floating point value of a by b. https://msdn.microsoft.com/en-us/library/4y73xa49(v=vs.100).aspx +FORCE_INLINE __m128 _mm_div_ss(__m128 a, __m128 b) +{ + float32_t value = vgetq_lane_f32(vreinterpretq_f32_m128(_mm_div_ps(a, b)), 0); + return vreinterpretq_m128_f32(vsetq_lane_f32(value, vreinterpretq_f32_m128(a), 0)); +} + +// This version does additional iterations to improve accuracy. Between 1 and 4 recommended. +// Computes the approximations of reciprocals of the four single-precision, floating-point values of a. https://msdn.microsoft.com/en-us/library/vstudio/796k1tty(v=vs.100).aspx +FORCE_INLINE __m128 recipq_newton(__m128 in, int n) +{ + int i; + float32x4_t recip = vrecpeq_f32(vreinterpretq_f32_m128(in)); + for (i = 0; i < n; ++i) + { + recip = vmulq_f32(recip, vrecpsq_f32(recip, vreinterpretq_f32_m128(in))); + } + return vreinterpretq_m128_f32(recip); +} + +// Computes the approximations of reciprocals of the four single-precision, floating-point values of a. https://msdn.microsoft.com/en-us/library/vstudio/796k1tty(v=vs.100).aspx +FORCE_INLINE __m128 _mm_rcp_ps(__m128 in) +{ + float32x4_t recip = vrecpeq_f32(vreinterpretq_f32_m128(in)); + recip = vmulq_f32(recip, vrecpsq_f32(recip, vreinterpretq_f32_m128(in))); + return vreinterpretq_m128_f32(recip); +} + +// Computes the approximations of square roots of the four single-precision, floating-point values of a. First computes reciprocal square roots and then reciprocals of the four values. https://msdn.microsoft.com/en-us/library/vstudio/8z67bwwk(v=vs.100).aspx +FORCE_INLINE __m128 _mm_sqrt_ps(__m128 in) +{ + float32x4_t recipsq = vrsqrteq_f32(vreinterpretq_f32_m128(in)); + float32x4_t sq = vrecpeq_f32(recipsq); + // ??? use step versions of both sqrt and recip for better accuracy? + return vreinterpretq_m128_f32(sq); +} + +// Computes the approximation of the square root of the scalar single-precision floating point value of in. https://msdn.microsoft.com/en-us/library/ahfsc22d(v=vs.100).aspx +FORCE_INLINE __m128 _mm_sqrt_ss(__m128 in) +{ + float32_t value = vgetq_lane_f32(vreinterpretq_f32_m128(_mm_sqrt_ps(in)), 0); + return vreinterpretq_m128_f32(vsetq_lane_f32(value, vreinterpretq_f32_m128(in), 0)); +} + +// Computes the approximations of the reciprocal square roots of the four single-precision floating point values of in. https://msdn.microsoft.com/en-us/library/22hfsh53(v=vs.100).aspx +FORCE_INLINE __m128 _mm_rsqrt_ps(__m128 in) +{ + return vreinterpretq_m128_f32(vrsqrteq_f32(vreinterpretq_f32_m128(in))); +} + +// Computes the maximums of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/ff5d607a(v=vs.100).aspx +FORCE_INLINE __m128 _mm_max_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_f32(vmaxq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +// Computes the minima of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/wh13kadz(v=vs.100).aspx +FORCE_INLINE __m128 _mm_min_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_f32(vminq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +// Computes the maximum of the two lower scalar single-precision floating point values of a and b. https://msdn.microsoft.com/en-us/library/s6db5esz(v=vs.100).aspx +FORCE_INLINE __m128 _mm_max_ss(__m128 a, __m128 b) +{ + float32_t value = vgetq_lane_f32(vmaxq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)), 0); + return vreinterpretq_m128_f32(vsetq_lane_f32(value, vreinterpretq_f32_m128(a), 0)); +} + +// Computes the minimum of the two lower scalar single-precision floating point values of a and b. https://msdn.microsoft.com/en-us/library/0a9y7xaa(v=vs.100).aspx +FORCE_INLINE __m128 _mm_min_ss(__m128 a, __m128 b) +{ + float32_t value = vgetq_lane_f32(vminq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)), 0); + return vreinterpretq_m128_f32(vsetq_lane_f32(value, vreinterpretq_f32_m128(a), 0)); +} + +// Computes the pairwise minima of the 8 signed 16-bit integers from a and the 8 signed 16-bit integers from b. https://msdn.microsoft.com/en-us/library/vstudio/6te997ew(v=vs.100).aspx +FORCE_INLINE __m128i _mm_min_epi16(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s16(vminq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b))); +} + +// epi versions of min/max +// Computes the pariwise maximums of the four signed 32-bit integer values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/bb514055(v=vs.100).aspx +FORCE_INLINE __m128i _mm_max_epi32(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s32(vmaxq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b))); +} + +// Computes the pariwise minima of the four signed 32-bit integer values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/bb531476(v=vs.100).aspx +FORCE_INLINE __m128i _mm_min_epi32(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s32(vminq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b))); +} + +// Multiplies the 8 signed 16-bit integers from a by the 8 signed 16-bit integers from b. https://msdn.microsoft.com/en-us/library/vstudio/59hddw1d(v=vs.100).aspx +FORCE_INLINE __m128i _mm_mulhi_epi16(__m128i a, __m128i b) +{ + /* apoty: issue with large values because of result saturation */ + //int16x8_t ret = vqdmulhq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)); /* =2*a*b */ + //return vreinterpretq_m128i_s16(vshrq_n_s16(ret, 1)); + int16x4_t a3210 = vget_low_s16(vreinterpretq_s16_m128i(a)); + int16x4_t b3210 = vget_low_s16(vreinterpretq_s16_m128i(b)); + int32x4_t ab3210 = vmull_s16(a3210, b3210); /* 3333222211110000 */ + int16x4_t a7654 = vget_high_s16(vreinterpretq_s16_m128i(a)); + int16x4_t b7654 = vget_high_s16(vreinterpretq_s16_m128i(b)); + int32x4_t ab7654 = vmull_s16(a7654, b7654); /* 7777666655554444 */ + uint16x8x2_t r = vuzpq_u16(vreinterpretq_u16_s32(ab3210), vreinterpretq_u16_s32(ab7654)); + return vreinterpretq_m128i_u16(r.val[1]); +} + +// Computes pairwise add of each argument as single-precision, floating-point values a and b. +//https://msdn.microsoft.com/en-us/library/yd9wecaa.aspx +FORCE_INLINE __m128 _mm_hadd_ps(__m128 a, __m128 b ) +{ +#if defined(__aarch64__) + return vreinterpretq_m128_f32(vpaddq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); //AArch64 +#else + float32x2_t a10 = vget_low_f32(vreinterpretq_f32_m128(a)); + float32x2_t a32 = vget_high_f32(vreinterpretq_f32_m128(a)); + float32x2_t b10 = vget_low_f32(vreinterpretq_f32_m128(b)); + float32x2_t b32 = vget_high_f32(vreinterpretq_f32_m128(b)); + return vreinterpretq_m128_f32(vcombine_f32(vpadd_f32(a10, a32), vpadd_f32(b10, b32))); +#endif +} + +// ****************************************** +// Compare operations +// ****************************************** + +// Compares for less than https://msdn.microsoft.com/en-us/library/vstudio/f330yhc8(v=vs.100).aspx +FORCE_INLINE __m128 _mm_cmplt_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_u32(vcltq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +// Compares for greater than. https://msdn.microsoft.com/en-us/library/vstudio/11dy102s(v=vs.100).aspx +FORCE_INLINE __m128 _mm_cmpgt_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_u32(vcgtq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +// Compares for greater than or equal. https://msdn.microsoft.com/en-us/library/vstudio/fs813y2t(v=vs.100).aspx +FORCE_INLINE __m128 _mm_cmpge_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_u32(vcgeq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +// Compares for less than or equal. https://msdn.microsoft.com/en-us/library/vstudio/1s75w83z(v=vs.100).aspx +FORCE_INLINE __m128 _mm_cmple_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_u32(vcleq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +// Compares for equality. https://msdn.microsoft.com/en-us/library/vstudio/36aectz5(v=vs.100).aspx +FORCE_INLINE __m128 _mm_cmpeq_ps(__m128 a, __m128 b) +{ + return vreinterpretq_m128_u32(vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); +} + +// Compares the 4 signed 32-bit integers in a and the 4 signed 32-bit integers in b for less than. https://msdn.microsoft.com/en-us/library/vstudio/4ak0bf5d(v=vs.100).aspx +FORCE_INLINE __m128i _mm_cmplt_epi32(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_u32(vcltq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b))); +} + +// Compares the 4 signed 32-bit integers in a and the 4 signed 32-bit integers in b for greater than. https://msdn.microsoft.com/en-us/library/vstudio/1s9f2z0y(v=vs.100).aspx +FORCE_INLINE __m128i _mm_cmpgt_epi32(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_u32(vcgtq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b))); +} + +// Compares the four 32-bit floats in a and b to check if any values are NaN. Ordered compare between each value returns true for "orderable" and false for "not orderable" (NaN). https://msdn.microsoft.com/en-us/library/vstudio/0h9w00fx(v=vs.100).aspx +// see also: +// http://stackoverflow.com/questions/8627331/what-does-ordered-unordered-comparison-mean +// http://stackoverflow.com/questions/29349621/neon-isnanval-intrinsics +FORCE_INLINE __m128 _mm_cmpord_ps(__m128 a, __m128 b ) +{ + // Note: NEON does not have ordered compare builtin + // Need to compare a eq a and b eq b to check for NaN + // Do AND of results to get final + uint32x4_t ceqaa = vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a)); + uint32x4_t ceqbb = vceqq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b)); + return vreinterpretq_m128_u32(vandq_u32(ceqaa, ceqbb)); +} + +// Compares the lower single-precision floating point scalar values of a and b using a less than operation. : https://msdn.microsoft.com/en-us/library/2kwe606b(v=vs.90).aspx +// Important note!! The documentation on MSDN is incorrect! If either of the values is a NAN the docs say you will get a one, but in fact, it will return a zero!! +FORCE_INLINE int _mm_comilt_ss(__m128 a, __m128 b) +{ + uint32x4_t a_not_nan = vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a)); + uint32x4_t b_not_nan = vceqq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b)); + uint32x4_t a_or_b_nan = vmvnq_u32(vandq_u32(a_not_nan, b_not_nan)); + uint32x4_t a_lt_b = vcltq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)); + return (vgetq_lane_u32(vorrq_u32(a_or_b_nan, a_lt_b), 0) != 0) ? 1 : 0; +} + +// Compares the lower single-precision floating point scalar values of a and b using a greater than operation. : https://msdn.microsoft.com/en-us/library/b0738e0t(v=vs.100).aspx +FORCE_INLINE int _mm_comigt_ss(__m128 a, __m128 b) +{ + //return vgetq_lane_u32(vcgtq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)), 0); + uint32x4_t a_not_nan = vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a)); + uint32x4_t b_not_nan = vceqq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b)); + uint32x4_t a_and_b_not_nan = vandq_u32(a_not_nan, b_not_nan); + uint32x4_t a_gt_b = vcgtq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)); + return (vgetq_lane_u32(vandq_u32(a_and_b_not_nan, a_gt_b), 0) != 0) ? 1 : 0; +} + +// Compares the lower single-precision floating point scalar values of a and b using a less than or equal operation. : https://msdn.microsoft.com/en-us/library/1w4t7c57(v=vs.90).aspx +FORCE_INLINE int _mm_comile_ss(__m128 a, __m128 b) +{ + //return vgetq_lane_u32(vcleq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)), 0); + uint32x4_t a_not_nan = vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a)); + uint32x4_t b_not_nan = vceqq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b)); + uint32x4_t a_or_b_nan = vmvnq_u32(vandq_u32(a_not_nan, b_not_nan)); + uint32x4_t a_le_b = vcleq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)); + return (vgetq_lane_u32(vorrq_u32(a_or_b_nan, a_le_b), 0) != 0) ? 1 : 0; +} + +// Compares the lower single-precision floating point scalar values of a and b using a greater than or equal operation. : https://msdn.microsoft.com/en-us/library/8t80des6(v=vs.100).aspx +FORCE_INLINE int _mm_comige_ss(__m128 a, __m128 b) +{ + //return vgetq_lane_u32(vcgeq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)), 0); + uint32x4_t a_not_nan = vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a)); + uint32x4_t b_not_nan = vceqq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b)); + uint32x4_t a_and_b_not_nan = vandq_u32(a_not_nan, b_not_nan); + uint32x4_t a_ge_b = vcgeq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)); + return (vgetq_lane_u32(vandq_u32(a_and_b_not_nan, a_ge_b), 0) != 0) ? 1 : 0; +} + +// Compares the lower single-precision floating point scalar values of a and b using an equality operation. : https://msdn.microsoft.com/en-us/library/93yx2h2b(v=vs.100).aspx +FORCE_INLINE int _mm_comieq_ss(__m128 a, __m128 b) +{ + //return vgetq_lane_u32(vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)), 0); + uint32x4_t a_not_nan = vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a)); + uint32x4_t b_not_nan = vceqq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b)); + uint32x4_t a_or_b_nan = vmvnq_u32(vandq_u32(a_not_nan, b_not_nan)); + uint32x4_t a_eq_b = vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)); + return (vgetq_lane_u32(vorrq_u32(a_or_b_nan, a_eq_b), 0) != 0) ? 1 : 0; +} + +// Compares the lower single-precision floating point scalar values of a and b using an inequality operation. : https://msdn.microsoft.com/en-us/library/bafh5e0a(v=vs.90).aspx +FORCE_INLINE int _mm_comineq_ss(__m128 a, __m128 b) +{ + //return !vgetq_lane_u32(vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)), 0); + uint32x4_t a_not_nan = vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a)); + uint32x4_t b_not_nan = vceqq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b)); + uint32x4_t a_and_b_not_nan = vandq_u32(a_not_nan, b_not_nan); + uint32x4_t a_neq_b = vmvnq_u32(vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))); + return (vgetq_lane_u32(vandq_u32(a_and_b_not_nan, a_neq_b), 0) != 0) ? 1 : 0; +} + +// according to the documentation, these intrinsics behave the same as the non-'u' versions. We'll just alias them here. +#define _mm_ucomilt_ss _mm_comilt_ss +#define _mm_ucomile_ss _mm_comile_ss +#define _mm_ucomigt_ss _mm_comigt_ss +#define _mm_ucomige_ss _mm_comige_ss +#define _mm_ucomieq_ss _mm_comieq_ss +#define _mm_ucomineq_ss _mm_comineq_ss + +// ****************************************** +// Conversions +// ****************************************** + +// Converts the four single-precision, floating-point values of a to signed 32-bit integer values using truncate. https://msdn.microsoft.com/en-us/library/vstudio/1h005y6x(v=vs.100).aspx +FORCE_INLINE __m128i _mm_cvttps_epi32(__m128 a) +{ + return vreinterpretq_m128i_s32(vcvtq_s32_f32(vreinterpretq_f32_m128(a))); +} + +// Converts the four signed 32-bit integer values of a to single-precision, floating-point values https://msdn.microsoft.com/en-us/library/vstudio/36bwxcx5(v=vs.100).aspx +FORCE_INLINE __m128 _mm_cvtepi32_ps(__m128i a) +{ + return vreinterpretq_m128_f32(vcvtq_f32_s32(vreinterpretq_s32_m128i(a))); +} + +// Converts the four unsigned 8-bit integers in the lower 32 bits to four unsigned 32-bit integers. https://msdn.microsoft.com/en-us/library/bb531467%28v=vs.100%29.aspx +FORCE_INLINE __m128i _mm_cvtepu8_epi32(__m128i a) +{ + uint8x16_t u8x16 = vreinterpretq_u8_s32(a); /* xxxx xxxx xxxx DCBA */ + uint16x8_t u16x8 = vmovl_u8(vget_low_u8(u8x16)); /* 0x0x 0x0x 0D0C 0B0A */ + uint32x4_t u32x4 = vmovl_u16(vget_low_u16(u16x8)); /* 000D 000C 000B 000A */ + return vreinterpretq_s32_u32(u32x4); +} + +// Converts the four signed 16-bit integers in the lower 64 bits to four signed 32-bit integers. https://msdn.microsoft.com/en-us/library/bb514079%28v=vs.100%29.aspx +FORCE_INLINE __m128i _mm_cvtepi16_epi32(__m128i a) +{ + return vreinterpretq_m128i_s32(vmovl_s16(vget_low_s16(vreinterpretq_s16_m128i(a)))); +} + +// Converts the four single-precision, floating-point values of a to signed 32-bit integer values. https://msdn.microsoft.com/en-us/library/vstudio/xdc42k5e(v=vs.100).aspx +// *NOTE*. The default rounding mode on SSE is 'round to even', which ArmV7 does not support! +// It is supported on ARMv8 however. +FORCE_INLINE __m128i _mm_cvtps_epi32(__m128 a) +{ +#if defined(__aarch64__) + return vcvtnq_s32_f32(a); +#else + uint32x4_t signmask = vdupq_n_u32(0x80000000); + float32x4_t half = vbslq_f32(signmask, vreinterpretq_f32_m128(a), vdupq_n_f32(0.5f)); /* +/- 0.5 */ + int32x4_t r_normal = vcvtq_s32_f32(vaddq_f32(vreinterpretq_f32_m128(a), half)); /* round to integer: [a + 0.5]*/ + int32x4_t r_trunc = vcvtq_s32_f32(vreinterpretq_f32_m128(a)); /* truncate to integer: [a] */ + int32x4_t plusone = vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_s32(vnegq_s32(r_trunc)), 31)); /* 1 or 0 */ + int32x4_t r_even = vbicq_s32(vaddq_s32(r_trunc, plusone), vdupq_n_s32(1)); /* ([a] + {0,1}) & ~1 */ + float32x4_t delta = vsubq_f32(vreinterpretq_f32_m128(a), vcvtq_f32_s32(r_trunc)); /* compute delta: delta = (a - [a]) */ + uint32x4_t is_delta_half = vceqq_f32(delta, half); /* delta == +/- 0.5 */ + return vreinterpretq_m128i_s32(vbslq_s32(is_delta_half, r_even, r_normal)); +#endif +} + +// Moves the least significant 32 bits of a to a 32-bit integer. https://msdn.microsoft.com/en-us/library/5z7a9642%28v=vs.90%29.aspx +FORCE_INLINE int _mm_cvtsi128_si32(__m128i a) +{ + return vgetq_lane_s32(vreinterpretq_s32_m128i(a), 0); +} + +// Moves 32-bit integer a to the least significant 32 bits of an __m128 object, zero extending the upper bits. https://msdn.microsoft.com/en-us/library/ct3539ha%28v=vs.90%29.aspx +FORCE_INLINE __m128i _mm_cvtsi32_si128(int a) +{ + return vreinterpretq_m128i_s32(vsetq_lane_s32(a, vdupq_n_s32(0), 0)); +} + + +// Applies a type cast to reinterpret four 32-bit floating point values passed in as a 128-bit parameter as packed 32-bit integers. https://msdn.microsoft.com/en-us/library/bb514099.aspx +FORCE_INLINE __m128i _mm_castps_si128(__m128 a) +{ + return vreinterpretq_m128i_s32(vreinterpretq_s32_m128(a)); +} + +// Applies a type cast to reinterpret four 32-bit integers passed in as a 128-bit parameter as packed 32-bit floating point values. https://msdn.microsoft.com/en-us/library/bb514029.aspx +FORCE_INLINE __m128 _mm_castsi128_ps(__m128i a) +{ + return vreinterpretq_m128_s32(vreinterpretq_s32_m128i(a)); +} + +// Loads 128-bit value. : https://msdn.microsoft.com/en-us/library/atzzad1h(v=vs.80).aspx +FORCE_INLINE __m128i _mm_load_si128(const __m128i *p) +{ + return vreinterpretq_m128i_s32(vld1q_s32((int32_t *)p)); +} + +// ****************************************** +// Miscellaneous Operations +// ****************************************** + +// Packs the 16 signed 16-bit integers from a and b into 8-bit integers and saturates. https://msdn.microsoft.com/en-us/library/k4y4f7w5%28v=vs.90%29.aspx +FORCE_INLINE __m128i _mm_packs_epi16(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s8(vcombine_s8(vqmovn_s16(vreinterpretq_s16_m128i(a)), vqmovn_s16(vreinterpretq_s16_m128i(b)))); +} + +// Packs the 16 signed 16 - bit integers from a and b into 8 - bit unsigned integers and saturates. https://msdn.microsoft.com/en-us/library/07ad1wx4(v=vs.100).aspx +FORCE_INLINE __m128i _mm_packus_epi16(const __m128i a, const __m128i b) +{ + return vreinterpretq_m128i_u8(vcombine_u8(vqmovun_s16(vreinterpretq_s16_m128i(a)), vqmovun_s16(vreinterpretq_s16_m128i(b)))); +} + +// Packs the 8 signed 32-bit integers from a and b into signed 16-bit integers and saturates. https://msdn.microsoft.com/en-us/library/393t56f9%28v=vs.90%29.aspx +FORCE_INLINE __m128i _mm_packs_epi32(__m128i a, __m128i b) +{ + return vreinterpretq_m128i_s16(vcombine_s16(vqmovn_s32(vreinterpretq_s32_m128i(a)), vqmovn_s32(vreinterpretq_s32_m128i(b)))); +} + +// Interleaves the lower 8 signed or unsigned 8-bit integers in a with the lower 8 signed or unsigned 8-bit integers in b. https://msdn.microsoft.com/en-us/library/xf7k860c%28v=vs.90%29.aspx +FORCE_INLINE __m128i _mm_unpacklo_epi8(__m128i a, __m128i b) +{ + int8x8_t a1 = vreinterpret_s8_s16(vget_low_s16(vreinterpretq_s16_m128i(a))); + int8x8_t b1 = vreinterpret_s8_s16(vget_low_s16(vreinterpretq_s16_m128i(b))); + int8x8x2_t result = vzip_s8(a1, b1); + return vreinterpretq_m128i_s8(vcombine_s8(result.val[0], result.val[1])); +} + +// Interleaves the lower 4 signed or unsigned 16-bit integers in a with the lower 4 signed or unsigned 16-bit integers in b. https://msdn.microsoft.com/en-us/library/btxb17bw%28v=vs.90%29.aspx +FORCE_INLINE __m128i _mm_unpacklo_epi16(__m128i a, __m128i b) +{ + int16x4_t a1 = vget_low_s16(vreinterpretq_s16_m128i(a)); + int16x4_t b1 = vget_low_s16(vreinterpretq_s16_m128i(b)); + int16x4x2_t result = vzip_s16(a1, b1); + return vreinterpretq_m128i_s16(vcombine_s16(result.val[0], result.val[1])); +} + +// Interleaves the lower 2 signed or unsigned 32 - bit integers in a with the lower 2 signed or unsigned 32 - bit integers in b. https://msdn.microsoft.com/en-us/library/x8atst9d(v=vs.100).aspx +FORCE_INLINE __m128i _mm_unpacklo_epi32(__m128i a, __m128i b) +{ + int32x2_t a1 = vget_low_s32(vreinterpretq_s32_m128i(a)); + int32x2_t b1 = vget_low_s32(vreinterpretq_s32_m128i(b)); + int32x2x2_t result = vzip_s32(a1, b1); + return vreinterpretq_m128i_s32(vcombine_s32(result.val[0], result.val[1])); +} + +// Selects and interleaves the lower two single-precision, floating-point values from a and b. https://msdn.microsoft.com/en-us/library/25st103b%28v=vs.90%29.aspx +FORCE_INLINE __m128 _mm_unpacklo_ps(__m128 a, __m128 b) +{ + float32x2_t a1 = vget_low_f32(vreinterpretq_f32_m128(a)); + float32x2_t b1 = vget_low_f32(vreinterpretq_f32_m128(b)); + float32x2x2_t result = vzip_f32(a1, b1); + return vreinterpretq_m128_f32(vcombine_f32(result.val[0], result.val[1])); +} + +// Selects and interleaves the upper two single-precision, floating-point values from a and b. https://msdn.microsoft.com/en-us/library/skccxx7d%28v=vs.90%29.aspx +FORCE_INLINE __m128 _mm_unpackhi_ps(__m128 a, __m128 b) +{ + float32x2_t a1 = vget_high_f32(vreinterpretq_f32_m128(a)); + float32x2_t b1 = vget_high_f32(vreinterpretq_f32_m128(b)); + float32x2x2_t result = vzip_f32(a1, b1); + return vreinterpretq_m128_f32(vcombine_f32(result.val[0], result.val[1])); +} + +// Interleaves the upper 8 signed or unsigned 8-bit integers in a with the upper 8 signed or unsigned 8-bit integers in b. https://msdn.microsoft.com/en-us/library/t5h7783k(v=vs.100).aspx +FORCE_INLINE __m128i _mm_unpackhi_epi8(__m128i a, __m128i b) +{ + int8x8_t a1 = vreinterpret_s8_s16(vget_high_s16(vreinterpretq_s16_m128i(a))); + int8x8_t b1 = vreinterpret_s8_s16(vget_high_s16(vreinterpretq_s16_m128i(b))); + int8x8x2_t result = vzip_s8(a1, b1); + return vreinterpretq_m128i_s8(vcombine_s8(result.val[0], result.val[1])); +} + +// Interleaves the upper 4 signed or unsigned 16-bit integers in a with the upper 4 signed or unsigned 16-bit integers in b. https://msdn.microsoft.com/en-us/library/03196cz7(v=vs.100).aspx +FORCE_INLINE __m128i _mm_unpackhi_epi16(__m128i a, __m128i b) +{ + int16x4_t a1 = vget_high_s16(vreinterpretq_s16_m128i(a)); + int16x4_t b1 = vget_high_s16(vreinterpretq_s16_m128i(b)); + int16x4x2_t result = vzip_s16(a1, b1); + return vreinterpretq_m128i_s16(vcombine_s16(result.val[0], result.val[1])); +} + +// Interleaves the upper 2 signed or unsigned 32-bit integers in a with the upper 2 signed or unsigned 32-bit integers in b. https://msdn.microsoft.com/en-us/library/65sa7cbs(v=vs.100).aspx +FORCE_INLINE __m128i _mm_unpackhi_epi32(__m128i a, __m128i b) +{ + int32x2_t a1 = vget_high_s32(vreinterpretq_s32_m128i(a)); + int32x2_t b1 = vget_high_s32(vreinterpretq_s32_m128i(b)); + int32x2x2_t result = vzip_s32(a1, b1); + return vreinterpretq_m128i_s32(vcombine_s32(result.val[0], result.val[1])); +} + +// Extracts the selected signed or unsigned 16-bit integer from a and zero extends. https://msdn.microsoft.com/en-us/library/6dceta0c(v=vs.100).aspx +//FORCE_INLINE int _mm_extract_epi16(__m128i a, __constrange(0,8) int imm) +#define _mm_extract_epi16(a, imm) \ +({ \ + (vgetq_lane_s16(vreinterpretq_s16_m128i(a), (imm)) & 0x0000ffffUL); \ +}) + +// Inserts the least significant 16 bits of b into the selected 16-bit integer of a. https://msdn.microsoft.com/en-us/library/kaze8hz1%28v=vs.100%29.aspx +//FORCE_INLINE __m128i _mm_insert_epi16(__m128i a, const int b, __constrange(0,8) int imm) +#define _mm_insert_epi16(a, b, imm) \ +({ \ + vreinterpretq_m128i_s16(vsetq_lane_s16((b), vreinterpretq_s16_m128i(a), (imm))); \ +}) + +// ****************************************** +// Streaming Extensions +// ****************************************** + +// Guarantees that every preceding store is globally visible before any subsequent store. https://msdn.microsoft.com/en-us/library/5h2w73d1%28v=vs.90%29.aspx +FORCE_INLINE void _mm_sfence(void) +{ + __sync_synchronize(); +} + +// Stores the data in a to the address p without polluting the caches. If the cache line containing address p is already in the cache, the cache will be updated.Address p must be 16 - byte aligned. https://msdn.microsoft.com/en-us/library/ba08y07y%28v=vs.90%29.aspx +FORCE_INLINE void _mm_stream_si128(__m128i *p, __m128i a) +{ + *p = a; +} + +// Cache line containing p is flushed and invalidated from all caches in the coherency domain. : https://msdn.microsoft.com/en-us/library/ba08y07y(v=vs.100).aspx +FORCE_INLINE void _mm_clflush(void const*p) +{ + // no corollary for Neon? +} + +#if defined(__GNUC__) || defined(__clang__) +# pragma pop_macro("ALIGN_STRUCT") +# pragma pop_macro("FORCE_INLINE") +#endif + +#endif diff --git a/src/crypto/soft_aes.h b/src/crypto/soft_aes.h index e28be3fa..4b1d346f 100644 --- a/src/crypto/soft_aes.h +++ b/src/crypto/soft_aes.h @@ -26,14 +26,18 @@ */ #pragma once -#ifdef __GNUC__ -#include + +#if defined(__aarch64__) +# include "crypto/SSE2NEON.h" +#elif defined(__GNUC__) +# include #else -#include -#endif // __GNUC__ +# include +#endif #include + #define saes_data(w) {\ w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\ w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\ @@ -109,7 +113,7 @@ static inline uint32_t sub_word(uint32_t key) saes_sbox[key & 0xff]; } -#ifdef __clang__ +#if defined(__clang__) || defined(XMRIG_ARM) static inline uint32_t _rotr(uint32_t value, uint32_t amount) { return (value >> amount) | (value << ((32 - amount) & 31)); From 4b00eb4a9fbb68392a316d8ac6c36a0931567d74 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 17 Nov 2017 12:59:46 +0300 Subject: [PATCH 050/389] #196 Fix Linux build. --- src/Platform_unix.cpp | 1 + src/Summary.cpp | 1 + src/api/ApiState.cpp | 4 ++-- src/api/NetworkState.cpp | 1 + src/workers/Hashrate.cpp | 9 +++++---- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Platform_unix.cpp b/src/Platform_unix.cpp index ecccc49e..c0589307 100644 --- a/src/Platform_unix.cpp +++ b/src/Platform_unix.cpp @@ -22,6 +22,7 @@ */ #include +#include #include #include #include diff --git a/src/Summary.cpp b/src/Summary.cpp index c6c53341..2d93f429 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -23,6 +23,7 @@ #include +#include #include diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp index bade355a..0ccb34fe 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiState.cpp @@ -21,7 +21,7 @@ * along with this program. If not, see . */ -#include +#include #include #include @@ -53,7 +53,7 @@ extern "C" static inline double normalize(double d) { - if (!std::isnormal(d)) { + if (!isnormal(d)) { return 0.0; } diff --git a/src/api/NetworkState.cpp b/src/api/NetworkState.cpp index 997f52df..bae290d0 100644 --- a/src/api/NetworkState.cpp +++ b/src/api/NetworkState.cpp @@ -23,6 +23,7 @@ #include +#include #include #include diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index 5c20c247..bd5b7df6 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -23,8 +23,9 @@ #include -#include +#include #include +#include #include "log/Log.h" #include "Options.h" @@ -33,7 +34,7 @@ inline const char *format(double h, char* buf, size_t size) { - if (std::isnormal(h)) { + if (isnormal(h)) { snprintf(buf, size, "%03.1f", h); return buf; } @@ -77,7 +78,7 @@ double Hashrate::calc(size_t ms) const for (int i = 0; i < m_threads; ++i) { data = calc(i, ms); - if (std::isnormal(data)) { + if (isnormal(data)) { result += data; } } @@ -170,7 +171,7 @@ void Hashrate::stop() void Hashrate::updateHighest() { double highest = calc(ShortInterval); - if (std::isnormal(highest) && highest > m_highest) { + if (isnormal(highest) && highest > m_highest) { m_highest = highest; } } From 1961dcf82470bda4eaa15097747cbe3a65c9f4d2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 17 Nov 2017 22:44:36 +0300 Subject: [PATCH 051/389] #204 Fix Linux build, again. --- src/api/ApiState.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp index 0ccb34fe..c963a1d6 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiState.cpp @@ -57,7 +57,7 @@ static inline double normalize(double d) return 0.0; } - return std::floor(d * 100.0) / 100.0; + return floor(d * 100.0) / 100.0; } From 989c217b3f91889fd4299a0792b06d6dd65768ae Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 18 Nov 2017 14:07:04 +0300 Subject: [PATCH 052/389] #200 Use fprintf failback when fail to use uv_tty. --- src/Options.h | 1 + src/log/ConsoleLog.cpp | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Options.h b/src/Options.h index 06b86f38..9070888f 100644 --- a/src/Options.h +++ b/src/Options.h @@ -76,6 +76,7 @@ public: inline int retryPause() const { return m_retryPause; } inline int threads() const { return m_threads; } inline int64_t affinity() const { return m_affinity; } + inline void setColors(bool colors) { m_colors = colors; } inline static void release() { delete m_self; } diff --git a/src/log/ConsoleLog.cpp b/src/log/ConsoleLog.cpp index ef8516eb..45733858 100644 --- a/src/log/ConsoleLog.cpp +++ b/src/log/ConsoleLog.cpp @@ -36,6 +36,7 @@ #include "log/ConsoleLog.h" #include "log/Log.h" +#include "Options.h" ConsoleLog::ConsoleLog(bool colors) : @@ -43,6 +44,8 @@ ConsoleLog::ConsoleLog(bool colors) : m_stream(nullptr) { if (uv_tty_init(uv_default_loop(), &m_tty, 1, 0) < 0) { + Options::i()->setColors(false); + m_colors = false; return; } @@ -65,10 +68,6 @@ ConsoleLog::ConsoleLog(bool colors) : void ConsoleLog::message(int level, const char* fmt, va_list args) { - if (!isWritable()) { - return; - } - time_t now = time(nullptr); tm stime; @@ -121,10 +120,6 @@ void ConsoleLog::message(int level, const char* fmt, va_list args) void ConsoleLog::text(const char* fmt, va_list args) { - if (!isWritable()) { - return; - } - snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s\n", fmt, m_colors ? Log::kCL_N : ""); print(args); @@ -149,5 +144,11 @@ void ConsoleLog::print(va_list args) return; } - uv_try_write(m_stream, &m_uvBuf, 1); + if (!isWritable()) { + fprintf(stdout, m_buf); + fflush(stdout); + } + else { + uv_try_write(m_stream, &m_uvBuf, 1); + } } From 47527d48ee19fc55ece8e84618daf19b7037bb34 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 24 Nov 2017 00:23:04 +0300 Subject: [PATCH 053/389] Fixed build in termux environment, thanks Imran Yusuff. --- CMakeLists.txt | 7 +++++++ src/Cpu_unix.cpp | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0275320f..64d504f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -142,6 +142,13 @@ if (CMAKE_SYSTEM_NAME STREQUAL FreeBSD) set(EXTRA_LIBS ${EXTRA_LIBS} kvm) endif() +if (CMAKE_SYSTEM_NAME MATCHES "Linux") + EXECUTE_PROCESS(COMMAND uname -o COMMAND tr -d '\n' OUTPUT_VARIABLE OPERATING_SYSTEM) + if (OPERATING_SYSTEM MATCHES "Android") + set(EXTRA_LIBS ${EXTRA_LIBS} log) + endif() +endif() + add_definitions(/D__STDC_FORMAT_MACROS) add_definitions(/DUNICODE) #add_definitions(/DAPP_DEBUG) diff --git a/src/Cpu_unix.cpp b/src/Cpu_unix.cpp index 8de98c8c..9a13e7a5 100644 --- a/src/Cpu_unix.cpp +++ b/src/Cpu_unix.cpp @@ -70,6 +70,10 @@ void Cpu::setAffinity(int id, uint64_t mask) sched_setaffinity(0, sizeof(&set), &set); # endif } else { +# ifndef __ANDROID__ pthread_setaffinity_np(pthread_self(), sizeof(&set), &set); +# else + sched_setaffinity(gettid(), sizeof(&set), &set); +# endif } } From aa4f8b6fa78eb8331b5927ba5315b926ac75ce9c Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 26 Nov 2017 22:23:23 +0300 Subject: [PATCH 054/389] #216 Added ARMv7 support. --- CMakeLists.txt | 6 +-- cmake/cpu.cmake | 11 +++++- cmake/flags.cmake | 10 ++++- src/Cpu_arm.cpp | 2 + src/crypto/CryptoNight.cpp | 12 +++++- ...{CryptoNight_arm64.h => CryptoNight_arm.h} | 39 +++++++++++++++++-- src/crypto/CryptoNight_x86.h | 6 +-- src/crypto/soft_aes.h | 2 +- src/log/ConsoleLog.cpp | 2 +- 9 files changed, 74 insertions(+), 16 deletions(-) rename src/crypto/{CryptoNight_arm64.h => CryptoNight_arm.h} (93%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 64d504f7..0a1f238a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,8 +64,8 @@ set(HEADERS_CRYPTO src/crypto/soft_aes.h ) -if (XMRIG_ARM64) - set(HEADERS_CRYPTO "${HEADERS_CRYPTO}" src/crypto/CryptoNight_arm64.h) +if (XMRIG_ARM) + set(HEADERS_CRYPTO "${HEADERS_CRYPTO}" src/crypto/CryptoNight_arm.h) else() set(HEADERS_CRYPTO "${HEADERS_CRYPTO}" src/crypto/CryptoNight_x86.h) endif() @@ -168,7 +168,7 @@ if (WITH_LIBCPUID) else() add_definitions(/DXMRIG_NO_LIBCPUID) - if (XMRIG_ARM64) + if (XMRIG_ARM) set(SOURCES_CPUID src/Cpu_arm.cpp) else() set(SOURCES_CPUID src/Cpu_stub.cpp) diff --git a/cmake/cpu.cmake b/cmake/cpu.cmake index 56bbef96..96e61e2b 100644 --- a/cmake/cpu.cmake +++ b/cmake/cpu.cmake @@ -9,8 +9,17 @@ endif() if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64)$") - set(XMRIG_ARM64 ON) + set(XMRIG_ARM ON) + set(XMRIG_ARMv8 ON) set(WITH_LIBCPUID OFF) add_definitions(/DXMRIG_ARM) + add_definitions(/DXMRIG_ARMv8) +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(armv7|armv7f|armv7s|armv7k|armv7-a|armv7l)$") + set(XMRIG_ARM ON) + set(XMRIG_ARMv7 ON) + set(WITH_LIBCPUID OFF) + + add_definitions(/DXMRIG_ARM) + add_definitions(/DXMRIG_ARMv7) endif() diff --git a/cmake/flags.cmake b/cmake/flags.cmake index 7f32e115..488f1236 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -14,9 +14,12 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-exceptions -fno-rtti") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") - if (CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) + if (XMRIG_ARMv8) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a+crypto") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+crypto -flax-vector-conversions") + elseif (XMRIG_ARMv7) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfpu=neon") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon -flax-vector-conversions") else() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes") @@ -52,9 +55,12 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-exceptions -fno-rtti -Wno-missing-braces") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") - if (CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) + if (XMRIG_ARMv8) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a+crypto") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+crypto") + elseif (XMRIG_ARMv7) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfpu=neon -march=${CMAKE_SYSTEM_PROCESSOR}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon -march=${CMAKE_SYSTEM_PROCESSOR}") else() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes") diff --git a/src/Cpu_arm.cpp b/src/Cpu_arm.cpp index 906e0156..c2047ffb 100644 --- a/src/Cpu_arm.cpp +++ b/src/Cpu_arm.cpp @@ -47,6 +47,8 @@ void Cpu::initCommon() { memcpy(m_brand, "Unknown", 7); +# if defined(XMRIG_ARMv8) m_flags |= X86_64; m_flags |= AES; +# endif } diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 6fdf9d92..3ac9e94b 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -24,8 +24,8 @@ #include "crypto/CryptoNight.h" -#if defined(__aarch64__) -# include "crypto/CryptoNight_arm64.h" +#if defined(XMRIG_ARM) +# include "crypto/CryptoNight_arm.h" #else # include "crypto/CryptoNight_x86.h" #endif @@ -40,12 +40,16 @@ void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, crypt static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx) { +# if !defined(XMRIG_ARMv7) cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, false>(input, size, output, ctx); +# endif } static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { +# if !defined(XMRIG_ARMv7) cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, false>(input, size, output, ctx); +# endif } @@ -61,12 +65,16 @@ static void cryptonight_av4_softaes_double(const void *input, size_t size, void #ifndef XMRIG_NO_AEON static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { + # if !defined(XMRIG_ARMv7) cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, false>(input, size, output, ctx); +#endif } static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { +# if !defined(XMRIG_ARMv7) cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, false>(input, size, output, ctx); +# endif } diff --git a/src/crypto/CryptoNight_arm64.h b/src/crypto/CryptoNight_arm.h similarity index 93% rename from src/crypto/CryptoNight_arm64.h rename to src/crypto/CryptoNight_arm.h index 4d70b20d..15be6c3d 100644 --- a/src/crypto/CryptoNight_arm64.h +++ b/src/crypto/CryptoNight_arm.h @@ -22,8 +22,8 @@ * along with this program. If not, see . */ -#ifndef __CRYPTONIGHT_ARM64_H__ -#define __CRYPTONIGHT_ARM64_H__ +#ifndef __CRYPTONIGHT_ARM_H__ +#define __CRYPTONIGHT_ARM_H__ #if defined(XMRIG_ARM) && !defined(__clang__) @@ -86,12 +86,39 @@ static inline __attribute__((always_inline)) uint64_t _mm_cvtsi128_si64(__m128i #define EXTRACT64(X) _mm_cvtsi128_si64(X) +#if defined(XMRIG_ARMv8) static inline uint64_t __umul128(uint64_t a, uint64_t b, uint64_t* hi) { unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b; *hi = r >> 64; return (uint64_t) r; } +#else +static inline uint64_t __umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *product_hi) { + // multiplier = ab = a * 2^32 + b + // multiplicand = cd = c * 2^32 + d + // ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d + uint64_t a = multiplier >> 32; + uint64_t b = multiplier & 0xFFFFFFFF; + uint64_t c = multiplicand >> 32; + uint64_t d = multiplicand & 0xFFFFFFFF; + + //uint64_t ac = a * c; + uint64_t ad = a * d; + //uint64_t bc = b * c; + uint64_t bd = b * d; + + uint64_t adbc = ad + (b * c); + uint64_t adbc_carry = adbc < ad ? 1 : 0; + + // multiplier * multiplicand = product_hi * 2^64 + product_lo + uint64_t product_lo = bd + (adbc << 32); + uint64_t product_lo_carry = product_lo < bd ? 1 : 0; + *product_hi = (a * c) + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry; + + return product_lo; +} +#endif // This will shift and xor tmp1 into itself as 4 32-bit vals such as @@ -176,6 +203,7 @@ static inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, *x6 = soft_aesenc(*x6, key); *x7 = soft_aesenc(*x7, key); } +# ifndef XMRIG_ARMv7 else { *x0 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x0), key)); *x1 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x1), key)); @@ -186,6 +214,7 @@ static inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, *x6 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x6), key)); *x7 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x7), key)); } +# endif } @@ -338,7 +367,9 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * cx = soft_aesenc(cx, _mm_set_epi64x(ah0, al0)); } else { +# ifndef XMRIG_ARMv7 cx = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah0, al0); +# endif } _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); @@ -402,8 +433,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, cx1 = soft_aesenc(cx1, _mm_set_epi64x(ah1, al1)); } else { +# ifndef XMRIG_ARMv7 cx0 = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx0, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah0, al0); cx1 = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx1, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah1, al1); +# endif } _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); @@ -455,4 +488,4 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); } -#endif /* __CRYPTONIGHT_ARM64_H__ */ +#endif /* __CRYPTONIGHT_ARM_H__ */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 75809b81..362a1a9f 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __CRYPTONIGHT_P_H__ -#define __CRYPTONIGHT_P_H__ +#ifndef __CRYPTONIGHT_X86_H__ +#define __CRYPTONIGHT_X86_H__ #ifdef __GNUC__ @@ -448,4 +448,4 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); } -#endif /* __CRYPTONIGHT_P_H__ */ +#endif /* __CRYPTONIGHT_X86_H__ */ diff --git a/src/crypto/soft_aes.h b/src/crypto/soft_aes.h index 4b1d346f..99321c4e 100644 --- a/src/crypto/soft_aes.h +++ b/src/crypto/soft_aes.h @@ -27,7 +27,7 @@ #pragma once -#if defined(__aarch64__) +#if defined(XMRIG_ARM) # include "crypto/SSE2NEON.h" #elif defined(__GNUC__) # include diff --git a/src/log/ConsoleLog.cpp b/src/log/ConsoleLog.cpp index 45733858..3656d48c 100644 --- a/src/log/ConsoleLog.cpp +++ b/src/log/ConsoleLog.cpp @@ -145,7 +145,7 @@ void ConsoleLog::print(va_list args) } if (!isWritable()) { - fprintf(stdout, m_buf); + fputs(m_buf, stdout); fflush(stdout); } else { From 905f9190ae3960f1da37a03f9286863b92318c74 Mon Sep 17 00:00:00 2001 From: xmrig Date: Wed, 29 Nov 2017 17:54:12 +0300 Subject: [PATCH 055/389] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a40a859..0f5deeb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ # v2.4.3 + - [#94](https://github.com/xmrig/xmrig/issues/94#issuecomment-342019257) [#216](https://github.com/xmrig/xmrig/issues/216) Added **ARMv8** and **ARMv7** support. Hardware AES supported, thanks [Imran Yusuff](https://github.com/imranyusuff). + - [#157](https://github.com/xmrig/xmrig/issues/157) [#196](https://github.com/xmrig/xmrig/issues/196) Fixed Linux compile issues. - [#184](https://github.com/xmrig/xmrig/issues/184) Fixed cache size detection for CPUs with disabled Hyper-Threading. # v2.4.2 From b68b2a907bb0ace576e1081c0e5bd75f2317243e Mon Sep 17 00:00:00 2001 From: xmrig Date: Wed, 29 Nov 2017 18:49:28 +0300 Subject: [PATCH 056/389] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f5deeb6..54314eca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - [#94](https://github.com/xmrig/xmrig/issues/94#issuecomment-342019257) [#216](https://github.com/xmrig/xmrig/issues/216) Added **ARMv8** and **ARMv7** support. Hardware AES supported, thanks [Imran Yusuff](https://github.com/imranyusuff). - [#157](https://github.com/xmrig/xmrig/issues/157) [#196](https://github.com/xmrig/xmrig/issues/196) Fixed Linux compile issues. - [#184](https://github.com/xmrig/xmrig/issues/184) Fixed cache size detection for CPUs with disabled Hyper-Threading. + - [#200](https://github.com/xmrig/xmrig/issues/200) In some cases miner was doesn't write log to stdout. # v2.4.2 - [#60](https://github.com/xmrig/xmrig/issues/60) Added FreeBSD support, thanks [vcambur](https://github.com/vcambur). From e458c561398e50f7e54339ae4a9c130c0dc70793 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 30 Nov 2017 02:11:15 +0300 Subject: [PATCH 057/389] v2.4.3 --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index 8d2e992f..c362e7be 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.4.2" +#define APP_VERSION "2.4.3" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 4 -#define APP_VER_BUILD 2 +#define APP_VER_BUILD 3 #define APP_VER_REV 0 #ifdef _MSC_VER From fbd100ef10565a148dc1e5928b8b2d6343bb0325 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 16 Dec 2017 19:27:11 +0700 Subject: [PATCH 058/389] #262 Reduce cmake version requirement to 2.8. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a1f238a..377bdb94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 2.8) project(xmrig) option(WITH_LIBCPUID "Use Libcpuid" ON) From 114a9b041dc1fe9195d7923d2a5396b97c45e8f3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 2 Jan 2018 12:24:57 +0700 Subject: [PATCH 059/389] #279 Add missing stdio header --- src/Platform_mac.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Platform_mac.cpp b/src/Platform_mac.cpp index 5e53aacb..ba541f1d 100644 --- a/src/Platform_mac.cpp +++ b/src/Platform_mac.cpp @@ -22,6 +22,7 @@ */ +#include #include #include #include @@ -41,11 +42,11 @@ static inline char *createUserAgent() char *buf = new char[max]; -# ifdef XMRIG_NVIDIA_PROJECT - const int cudaVersion = cuda_get_runtime_version(); - snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s CUDA/%d.%d clang/%d.%d.%d", APP_NAME, APP_VERSION, uv_version_string(), cudaVersion / 1000, cudaVersion % 100, __clang_major__, __clang_minor__, __clang_patchlevel__); -# else - snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s clang/%d.%d.%d", APP_NAME, APP_VERSION, uv_version_string(), __clang_major__, __clang_minor__, __clang_patchlevel__); +# ifdef XMRIG_NVIDIA_PROJECT + const int cudaVersion = cuda_get_runtime_version(); + snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s CUDA/%d.%d clang/%d.%d.%d", APP_NAME, APP_VERSION, uv_version_string(), cudaVersion / 1000, cudaVersion % 100, __clang_major__, __clang_minor__, __clang_patchlevel__); +# else + snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s clang/%d.%d.%d", APP_NAME, APP_VERSION, uv_version_string(), __clang_major__, __clang_minor__, __clang_patchlevel__); # endif return buf; From 1b025f681cdb7dcc7c98fd8cf82ee353e80d2262 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 2 Jan 2018 13:41:00 +0700 Subject: [PATCH 060/389] Remove extra semicolon. --- src/net/Client.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 88134ae7..8e8ebcfd 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -582,11 +582,11 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror((int) nread)); } - return client->close();; + return client->close(); } if ((size_t) nread > (sizeof(m_buf) - 8 - client->m_recvBufPos)) { - return client->close();; + return client->close(); } client->m_recvBufPos += nread; @@ -623,7 +623,7 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) auto client = getClient(req->data); if (status < 0) { LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); - return client->reconnect();; + return client->reconnect(); } addrinfo *ptr = res; From 785df621833319b380f1af2ee936a6bb05be2a9e Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 2 Jan 2018 13:49:31 +0700 Subject: [PATCH 061/389] Update README.md. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 50ab830b..b4880b38 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # XMRig -XMRig is high performance Monero (XMR) CPU miner, with the official full Windows support. -Originally based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of legacy code, since version 1.0.0 complete rewritten from scratch on C++. +XMRig is a high performance Monero (XMR) CPU miner, with official support for Windows. +Originally based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of legacy code, since version 1.0.0 completely rewritten from scratch on C++. * This is the **CPU-mining** version, there is also a [NVIDIA GPU version](https://github.com/xmrig/xmrig-nvidia) and [AMD GPU version]( https://github.com/xmrig/xmrig-amd). * [Roadmap](https://github.com/xmrig/xmrig/issues/106) for next releases. @@ -89,7 +89,7 @@ Since version 0.8.0. ## Common Issues ### HUGE PAGES unavailable * Run XMRig as Administrator. -* Since version 0.8.0 XMRig automatically enable SeLockMemoryPrivilege for current user, but reboot or sign out still required. [Manual instruction](https://msdn.microsoft.com/en-gb/library/ms190730.aspx). +* Since version 0.8.0 XMRig automatically enables SeLockMemoryPrivilege for current user, but reboot or sign out still required. [Manual instruction](https://msdn.microsoft.com/en-gb/library/ms190730.aspx). ## Other information * No HTTP support, only stratum protocol support. @@ -106,7 +106,7 @@ Please note performance is highly dependent on system load. The numbers above ar ### Maximum performance checklist * Idle operating system. * Do not exceed optimal thread count. -* Use modern CPUs with AES-NI instructuon set. +* Use modern CPUs with AES-NI instruction set. * Try setup optimal cpu affinity. * Enable fast memory (Large/Huge pages). From 5b88213f61ac41e62b906595b60ffb62527ff650 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 4 Jan 2018 11:38:32 +0700 Subject: [PATCH 062/389] Fix wrong signal handle. --- src/App.cpp | 10 ++++++---- src/App.h | 4 +++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/App.cpp b/src/App.cpp index 1aae7ae0..c08a8be2 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -92,7 +92,9 @@ App::App(int argc, char **argv) : m_network = new Network(m_options); - uv_signal_init(uv_default_loop(), &m_signal); + uv_signal_init(uv_default_loop(), &m_sigHUP); + uv_signal_init(uv_default_loop(), &m_sigINT); + uv_signal_init(uv_default_loop(), &m_sigTERM); } @@ -114,9 +116,9 @@ int App::exec() return 0; } - uv_signal_start(&m_signal, App::onSignal, SIGHUP); - uv_signal_start(&m_signal, App::onSignal, SIGTERM); - uv_signal_start(&m_signal, App::onSignal, SIGINT); + uv_signal_start(&m_sigHUP, App::onSignal, SIGHUP); + uv_signal_start(&m_sigINT, App::onSignal, SIGINT); + uv_signal_start(&m_sigTERM, App::onSignal, SIGTERM); background(); diff --git a/src/App.h b/src/App.h index 781f78f2..fcb3b71b 100644 --- a/src/App.h +++ b/src/App.h @@ -60,7 +60,9 @@ private: Httpd *m_httpd; Network *m_network; Options *m_options; - uv_signal_t m_signal; + uv_signal_t m_sigHUP; + uv_signal_t m_sigINT; + uv_signal_t m_sigTERM; }; From 8b7a737ceb14871fd09ad4b190cd126e079852d9 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 5 Jan 2018 17:23:39 +0700 Subject: [PATCH 063/389] Fix recent MSVC 2017 version detection. --- src/version.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/version.h b/src/version.h index c362e7be..f869db8a 100644 --- a/src/version.h +++ b/src/version.h @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig + * Copyright 2016-2018 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -27,10 +27,10 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.4.3" +#define APP_VERSION "2.4.4" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" -#define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com" +#define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" #define APP_KIND "cpu" #define APP_VER_MAJOR 2 @@ -39,7 +39,7 @@ #define APP_VER_REV 0 #ifdef _MSC_VER -# if (_MSC_VER == 1910 || _MSC_VER == 1911) +# if (_MSC_VER >= 1910) # define MSVC_VERSION 2017 # elif _MSC_VER == 1900 # define MSVC_VERSION 2015 From 49b45ddd18461558e80b3e7c0eb741409072fd78 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 5 Jan 2018 19:41:19 +0700 Subject: [PATCH 064/389] Add libmicrohttpd version to --version output. --- src/Options.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Options.cpp b/src/Options.cpp index b1197223..3eaf07ce 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -33,6 +33,11 @@ #endif +#ifndef XMRIG_NO_HTTPD +# include +#endif + + #include "Cpu.h" #include "donate.h" #include "net/Url.h" @@ -665,6 +670,10 @@ void Options::showVersion() "\n"); printf("\nlibuv/%s\n", uv_version_string()); + +# ifndef XMRIG_NO_HTTPD + printf("libmicrohttpd/%s\n", MHD_get_version()); +# endif } From f93187b024610ed83f41e18a5b6145ff7b91f61d Mon Sep 17 00:00:00 2001 From: stanz2g Date: Sat, 6 Jan 2018 17:07:07 +0800 Subject: [PATCH 065/389] can build without microhttpd when WITH_HTTPD=OFF --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 377bdb94..1e1673ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,6 +195,7 @@ if (WITH_HTTPD) message(FATAL_ERROR "microhttpd NOT found: use `-DWITH_HTTPD=OFF` to build without http deamon support") endif() else() + set(MHD_LIBRARY "") add_definitions(/DXMRIG_NO_HTTPD) add_definitions(/DXMRIG_NO_API) endif() From d73bee81abe74c962a423053c19c834300de5268 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 7 Jan 2018 21:36:49 +0700 Subject: [PATCH 066/389] Update README.md --- README.md | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index b4880b38..6ab4b6a2 100644 --- a/README.md +++ b/README.md @@ -50,31 +50,34 @@ For failover you can add multiple pools, maximum count not limited. ### Options ``` - -a, --algo=ALGO cryptonight (default) or cryptonight-lite - -o, --url=URL URL of mining server - -O, --userpass=U:P username:password pair for mining server - -u, --user=USERNAME username for mining server - -p, --pass=PASSWORD password for mining server - -t, --threads=N number of miner threads - -v, --av=N algorithm variation, 0 auto select - -k, --keepalive send keepalived for prevent timeout (need pool support) - -r, --retries=N number of times to retry before switch to backup server (default: 5) - -R, --retry-pause=N time to pause between retries (default: 5) - --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1 - --cpu-priority set process priority (0 idle, 2 normal to 5 highest) - --no-huge-pages disable huge pages support - --no-color disable colored output - --donate-level=N donate level, default 5% (5 minutes in 100 minutes) - --user-agent set custom user-agent string for pool - -B, --background run the miner in the background - -c, --config=FILE load a JSON-format configuration file - -l, --log-file=FILE log all output to a file - --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75) - --safe safe adjust threads and av settings for current CPU - --nicehash enable nicehash support - --print-time=N print hashrate report every N seconds - -h, --help display this help and exit - -V, --version output version information and exit + -a, --algo=ALGO cryptonight (default) or cryptonight-lite + -o, --url=URL URL of mining server + -O, --userpass=U:P username:password pair for mining server + -u, --user=USERNAME username for mining server + -p, --pass=PASSWORD password for mining server + -t, --threads=N number of miner threads + -v, --av=N algorithm variation, 0 auto select + -k, --keepalive send keepalived for prevent timeout (need pool support) + -r, --retries=N number of times to retry before switch to backup server (default: 5) + -R, --retry-pause=N time to pause between retries (default: 5) + --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1 + --cpu-priority set process priority (0 idle, 2 normal to 5 highest) + --no-huge-pages disable huge pages support + --no-color disable colored output + --donate-level=N donate level, default 5% (5 minutes in 100 minutes) + --user-agent set custom user-agent string for pool + -B, --background run the miner in the background + -c, --config=FILE load a JSON-format configuration file + -l, --log-file=FILE log all output to a file + --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75) + --safe safe adjust threads and av settings for current CPU + --nicehash enable nicehash/xmrig-proxy support + --print-time=N print hashrate report every N seconds + --api-port=N port for the miner API + --api-access-token=T access token for API + --api-worker-id=ID custom worker-id for API + -h, --help display this help and exit + -V, --version output version information and exit ``` Also you can use configuration via config file, default **config.json**. You can load multiple config files and combine it with command line options. From 038bb1f6bc2c422a7c7c5a4c23bc2129de4cdbf0 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 10 Jan 2018 16:56:08 +0700 Subject: [PATCH 067/389] Fix version. --- src/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index f869db8a..52058eec 100644 --- a/src/version.h +++ b/src/version.h @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 4 -#define APP_VER_BUILD 3 +#define APP_VER_BUILD 4 #define APP_VER_REV 0 #ifdef _MSC_VER From e6540229cba5bdb52c3a80615bb0f5266f63d4db Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 10 Jan 2018 22:55:45 +0700 Subject: [PATCH 068/389] #328 Added guard to prevent paused message spam and crash. --- src/App.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/App.cpp b/src/App.cpp index c08a8be2..1c00e4fb 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -166,8 +166,10 @@ void App::onConsoleCommand(char command) case 'p': case 'P': - LOG_INFO(m_options->colors() ? "\x1B[01;33mpaused\x1B[0m, press \x1B[01;35mr\x1B[0m to resume" : "paused, press 'r' to resume"); - Workers::setEnabled(false); + if (Workers::isEnabled()) { + LOG_INFO(m_options->colors() ? "\x1B[01;33mpaused\x1B[0m, press \x1B[01;35mr\x1B[0m to resume" : "paused, press 'r' to resume"); + Workers::setEnabled(false); + } break; case 'r': From 916cf0ae0da6ac9a59bebb86a6106d0e34d3eb96 Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 11 Jan 2018 15:08:24 +0700 Subject: [PATCH 069/389] Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54314eca..878ff984 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v2.4.4 + - Added libmicrohttpd version to --version output. + - Fixed bug in singal handler, in some cases miner wasn't shutdown properly. + - Fixed recent MSVC 2017 version detection. + - [#279](https://github.com/xmrig/xmrig/pull/279) Fixed build on some macOS versions. + # v2.4.3 - [#94](https://github.com/xmrig/xmrig/issues/94#issuecomment-342019257) [#216](https://github.com/xmrig/xmrig/issues/216) Added **ARMv8** and **ARMv7** support. Hardware AES supported, thanks [Imran Yusuff](https://github.com/imranyusuff). - [#157](https://github.com/xmrig/xmrig/issues/157) [#196](https://github.com/xmrig/xmrig/issues/196) Fixed Linux compile issues. From 56ffa7af794c0eebaad557d1218b6ee8f766d4e5 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 20 Jan 2018 12:58:43 +0700 Subject: [PATCH 070/389] #341 Fix wrong exit code. --- src/App.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.cpp b/src/App.cpp index 1c00e4fb..2eb81f56 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -113,7 +113,7 @@ App::~App() int App::exec() { if (!m_options) { - return 0; + return 2; } uv_signal_start(&m_sigHUP, App::onSignal, SIGHUP); From 9bceb65ad87e64e6e88599e0d6988cbea51c5555 Mon Sep 17 00:00:00 2001 From: Foudge Date: Sat, 20 Jan 2018 10:43:56 +0100 Subject: [PATCH 071/389] +15% boost with non-AES CPU Performance boost validated on Core 2 Quad processor under Windows 10. But it's Windows/MS Visual C++ specific. --- src/crypto/soft_aes.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/crypto/soft_aes.h b/src/crypto/soft_aes.h index 99321c4e..148f39c1 100644 --- a/src/crypto/soft_aes.h +++ b/src/crypto/soft_aes.h @@ -91,10 +91,17 @@ alignas(16) const uint8_t saes_sbox[256] = saes_data(saes_h0); static inline __m128i soft_aesenc(__m128i in, __m128i key) { - const uint32_t x0 = _mm_cvtsi128_si32(in); - const uint32_t x1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0x55)); - const uint32_t x2 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xAA)); - const uint32_t x3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xFF)); +#if defined(_MSC_VER) + const uint32_t x0 = in.m128i_u32[0]; + const uint32_t x1 = in.m128i_u32[1]; + const uint32_t x2 = in.m128i_u32[2]; + const uint32_t x3 = in.m128i_u32[3]; +#else + const uint32_t x0 = _mm_cvtsi128_si32(in); + const uint32_t x1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0x55)); + const uint32_t x2 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xAA)); + const uint32_t x3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xFF)); +#endif __m128i out = _mm_set_epi32( (saes_table[0][x3 & 0xff] ^ saes_table[1][(x0 >> 8) & 0xff] ^ saes_table[2][(x1 >> 16) & 0xff] ^ saes_table[3][x2 >> 24]), From 631fd755c814b772dc2bf95fe3c54a3273c4607b Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 20 Jan 2018 20:43:31 +0700 Subject: [PATCH 072/389] #341 Added option --dry-run. --- src/App.cpp | 26 ++++++++++++++++++++------ src/App.h | 1 + src/Options.cpp | 14 +++++++++++--- src/Options.h | 2 ++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/App.cpp b/src/App.cpp index 2eb81f56..d656acc8 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -130,6 +130,13 @@ int App::exec() Mem::allocate(m_options->algo(), m_options->threads(), m_options->doubleHash(), m_options->hugePages()); Summary::print(); + if (m_options->dryRun()) { + LOG_NOTICE("OK"); + release(); + + return 0; + } + # ifndef XMRIG_NO_API Api::start(); # endif @@ -146,12 +153,7 @@ int App::exec() const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); uv_loop_close(uv_default_loop()); - delete m_network; - - Options::release(); - Mem::release(); - Platform::release(); - + release(); return r; } @@ -200,6 +202,18 @@ void App::close() } +void App::release() +{ + if (m_network) { + delete m_network; + } + + Options::release(); + Mem::release(); + Platform::release(); +} + + void App::onSignal(uv_signal_t *handle, int signum) { switch (signum) diff --git a/src/App.h b/src/App.h index fcb3b71b..1b96040d 100644 --- a/src/App.h +++ b/src/App.h @@ -51,6 +51,7 @@ protected: private: void background(); void close(); + void release(); static void onSignal(uv_signal_t *handle, int signum); diff --git a/src/Options.cpp b/src/Options.cpp index 3eaf07ce..4e7c75ca 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -101,12 +101,16 @@ static char const short_options[] = "a:c:khBp:Px:r:R:s:t:T:o:u:O:v:Vl:S"; static struct option const options[] = { { "algo", 1, nullptr, 'a' }, + { "api-access-token", 1, nullptr, 4001 }, + { "api-port", 1, nullptr, 4000 }, + { "api-worker-id", 1, nullptr, 4002 }, { "av", 1, nullptr, 'v' }, { "background", 0, nullptr, 'B' }, { "config", 1, nullptr, 'c' }, { "cpu-affinity", 1, nullptr, 1020 }, { "cpu-priority", 1, nullptr, 1021 }, { "donate-level", 1, nullptr, 1003 }, + { "dry-run", 0, nullptr, 5000 }, { "help", 0, nullptr, 'h' }, { "keepalive", 0, nullptr ,'k' }, { "log-file", 1, nullptr, 'l' }, @@ -126,9 +130,6 @@ static struct option const options[] = { { "user-agent", 1, nullptr, 1008 }, { "userpass", 1, nullptr, 'O' }, { "version", 0, nullptr, 'V' }, - { "api-port", 1, nullptr, 4000 }, - { "api-access-token", 1, nullptr, 4001 }, - { "api-worker-id", 1, nullptr, 4002 }, { 0, 0, 0, 0 } }; @@ -141,6 +142,7 @@ static struct option const config_options[] = { { "cpu-affinity", 1, nullptr, 1020 }, { "cpu-priority", 1, nullptr, 1021 }, { "donate-level", 1, nullptr, 1003 }, + { "dry-run", 0, nullptr, 5000 }, { "huge-pages", 0, nullptr, 1009 }, { "log-file", 1, nullptr, 'l' }, { "max-cpu-usage", 1, nullptr, 1004 }, @@ -205,6 +207,7 @@ Options::Options(int argc, char **argv) : m_background(false), m_colors(true), m_doubleHash(false), + m_dryRun(false), m_hugePages(true), m_ready(false), m_safe(false), @@ -384,6 +387,7 @@ bool Options::parseArg(int key, const char *arg) case 'S': /* --syslog */ case 1005: /* --safe */ case 1006: /* --nicehash */ + case 5000: /* --dry-run */ return parseBoolean(key, true); case 1002: /* --no-color */ @@ -557,6 +561,10 @@ bool Options::parseBoolean(int key, bool enable) m_colors = enable; break; + case 5000: /* --dry-run */ + m_dryRun = enable; + break; + default: break; } diff --git a/src/Options.h b/src/Options.h index 9070888f..6f074917 100644 --- a/src/Options.h +++ b/src/Options.h @@ -59,6 +59,7 @@ public: inline bool background() const { return m_background; } inline bool colors() const { return m_colors; } inline bool doubleHash() const { return m_doubleHash; } + inline bool dryRun() const { return m_dryRun; } inline bool hugePages() const { return m_hugePages; } inline bool syslog() const { return m_syslog; } inline const char *apiToken() const { return m_apiToken; } @@ -110,6 +111,7 @@ private: bool m_background; bool m_colors; bool m_doubleHash; + bool m_dryRun; bool m_hugePages; bool m_ready; bool m_safe; From 15fe6ce23f4aa2654bb8b458463e6213ee867486 Mon Sep 17 00:00:00 2001 From: Foudge Date: Sat, 27 Jan 2018 11:42:22 +0100 Subject: [PATCH 073/389] Remove compilation warnings under MSVC --- src/api/NetworkState.cpp | 4 ++-- src/net/Client.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/NetworkState.cpp b/src/api/NetworkState.cpp index bae290d0..d3ffddd3 100644 --- a/src/api/NetworkState.cpp +++ b/src/api/NetworkState.cpp @@ -46,7 +46,7 @@ NetworkState::NetworkState() : int NetworkState::connectionTime() const { - return m_active ? ((uv_now(uv_default_loop()) - m_connectionTime) / 1000) : 0; + return m_active ? (int)((uv_now(uv_default_loop()) - m_connectionTime) / 1000) : 0; } @@ -56,7 +56,7 @@ uint32_t NetworkState::avgTime() const return 0; } - return (uint32_t) connectionTime() / m_latency.size(); + return connectionTime() / (uint32_t)m_latency.size(); } diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 8e8ebcfd..fcaec8eb 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -532,7 +532,7 @@ void Client::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t auto client = getClient(handle->data); buf->base = &client->m_recvBuf.base[client->m_recvBufPos]; - buf->len = client->m_recvBuf.len - client->m_recvBufPos; + buf->len = client->m_recvBuf.len - (ULONG)client->m_recvBufPos; } From 9a28ad590ca6137bf5e19ba477e3f379527bbd73 Mon Sep 17 00:00:00 2001 From: Foudge Date: Sun, 28 Jan 2018 12:58:19 +0100 Subject: [PATCH 074/389] up to 20% perf increase with Cryptonight with non-AES CPU This time, the performance increase is got with MSVC and GCC. On non-AES CPU, there were an useless load/store SSE2 register. The last MSVC "hack" is replaced by a portable code and he's more complete (a load is saved). On my C2Q6600, with 3 thread, I have +16% with MSVC2015 and +20% with GCC 7.3, compared to official 2.4.4 version. --- src/crypto/CryptoNight_arm.h | 30 +++++++++-------- src/crypto/CryptoNight_x86.h | 62 ++++++++++++++++++------------------ src/crypto/soft_aes.h | 17 +++------- 3 files changed, 52 insertions(+), 57 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 15be6c3d..17bba7af 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -194,14 +194,14 @@ template static inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, __m128i* x3, __m128i* x4, __m128i* x5, __m128i* x6, __m128i* x7) { if (SOFT_AES) { - *x0 = soft_aesenc(*x0, key); - *x1 = soft_aesenc(*x1, key); - *x2 = soft_aesenc(*x2, key); - *x3 = soft_aesenc(*x3, key); - *x4 = soft_aesenc(*x4, key); - *x5 = soft_aesenc(*x5, key); - *x6 = soft_aesenc(*x6, key); - *x7 = soft_aesenc(*x7, key); + *x0 = soft_aesenc((uint32_t*)x0, key); + *x1 = soft_aesenc((uint32_t*)x1, key); + *x2 = soft_aesenc((uint32_t*)x2, key); + *x3 = soft_aesenc((uint32_t*)x3, key); + *x4 = soft_aesenc((uint32_t*)x4, key); + *x5 = soft_aesenc((uint32_t*)x5, key); + *x6 = soft_aesenc((uint32_t*)x6, key); + *x7 = soft_aesenc((uint32_t*)x7, key); } # ifndef XMRIG_ARMv7 else { @@ -361,12 +361,13 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * uint64_t idx0 = h0[0] ^ h0[4]; for (size_t i = 0; i < ITERATIONS; i++) { - __m128i cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + __m128i cx; if (SOFT_AES) { - cx = soft_aesenc(cx, _mm_set_epi64x(ah0, al0)); + cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); } else { + cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); # ifndef XMRIG_ARMv7 cx = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah0, al0); # endif @@ -425,14 +426,15 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, uint64_t idx1 = h1[0] ^ h1[4]; for (size_t i = 0; i < ITERATIONS; i++) { - __m128i cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); - __m128i cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); + __m128i cx0, cx1; if (SOFT_AES) { - cx0 = soft_aesenc(cx0, _mm_set_epi64x(ah0, al0)); - cx1 = soft_aesenc(cx1, _mm_set_epi64x(ah1, al1)); + cx0 = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); + cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], _mm_set_epi64x(ah1, al1)); } else { + cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); # ifndef XMRIG_ARMv7 cx0 = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx0, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah0, al0); cx1 = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx1, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah1, al1); diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 362a1a9f..786d28f1 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -193,14 +193,14 @@ template static inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, __m128i* x3, __m128i* x4, __m128i* x5, __m128i* x6, __m128i* x7) { if (SOFT_AES) { - *x0 = soft_aesenc(*x0, key); - *x1 = soft_aesenc(*x1, key); - *x2 = soft_aesenc(*x2, key); - *x3 = soft_aesenc(*x3, key); - *x4 = soft_aesenc(*x4, key); - *x5 = soft_aesenc(*x5, key); - *x6 = soft_aesenc(*x6, key); - *x7 = soft_aesenc(*x7, key); + *x0 = soft_aesenc((uint32_t*)x0, key); + *x1 = soft_aesenc((uint32_t*)x1, key); + *x2 = soft_aesenc((uint32_t*)x2, key); + *x3 = soft_aesenc((uint32_t*)x3, key); + *x4 = soft_aesenc((uint32_t*)x4, key); + *x5 = soft_aesenc((uint32_t*)x5, key); + *x6 = soft_aesenc((uint32_t*)x6, key); + *x7 = soft_aesenc((uint32_t*)x7, key); } else { *x0 = _mm_aesenc_si128(*x0, key); @@ -324,19 +324,18 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * uint64_t idx0 = h0[0] ^ h0[4]; for (size_t i = 0; i < ITERATIONS; i++) { - __m128i cx; - cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + __m128i cx; - if (SOFT_AES) { - cx = soft_aesenc(cx, _mm_set_epi64x(ah0, al0)); - } - else { - cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); - } - - _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); - idx0 = EXTRACT64(cx); - bx0 = cx; + if (SOFT_AES) { + cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); + } + else { + cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); + } + _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + idx0 = EXTRACT64(cx); + bx0 = cx; uint64_t hi, lo, cl, ch; cl = ((uint64_t*) &l0[idx0 & MASK])[0]; @@ -386,18 +385,19 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, uint64_t idx0 = h0[0] ^ h0[4]; uint64_t idx1 = h1[0] ^ h1[4]; - for (size_t i = 0; i < ITERATIONS; i++) { - __m128i cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); - __m128i cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); + for (size_t i = 0; i < ITERATIONS; i++) { + __m128i cx0, cx1; - if (SOFT_AES) { - cx0 = soft_aesenc(cx0, _mm_set_epi64x(ah0, al0)); - cx1 = soft_aesenc(cx1, _mm_set_epi64x(ah1, al1)); - } - else { - cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); - cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); - } + if (SOFT_AES) { + cx0 = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); + cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], _mm_set_epi64x(ah1, al1)); + } + else { + cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); + cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); + cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); + } _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); diff --git a/src/crypto/soft_aes.h b/src/crypto/soft_aes.h index 148f39c1..b7698ac4 100644 --- a/src/crypto/soft_aes.h +++ b/src/crypto/soft_aes.h @@ -89,19 +89,12 @@ alignas(16) const uint32_t saes_table[4][256] = { saes_data(saes_u0), saes_data(saes_u1), saes_data(saes_u2), saes_data(saes_u3) }; alignas(16) const uint8_t saes_sbox[256] = saes_data(saes_h0); -static inline __m128i soft_aesenc(__m128i in, __m128i key) +static inline __m128i soft_aesenc(const uint32_t* in, __m128i key) { -#if defined(_MSC_VER) - const uint32_t x0 = in.m128i_u32[0]; - const uint32_t x1 = in.m128i_u32[1]; - const uint32_t x2 = in.m128i_u32[2]; - const uint32_t x3 = in.m128i_u32[3]; -#else - const uint32_t x0 = _mm_cvtsi128_si32(in); - const uint32_t x1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0x55)); - const uint32_t x2 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xAA)); - const uint32_t x3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xFF)); -#endif + const uint32_t x0 = in[0]; + const uint32_t x1 = in[1]; + const uint32_t x2 = in[2]; + const uint32_t x3 = in[3]; __m128i out = _mm_set_epi32( (saes_table[0][x3 & 0xff] ^ saes_table[1][(x0 >> 8) & 0xff] ^ saes_table[2][(x1 >> 16) & 0xff] ^ saes_table[3][x2 >> 24]), From d2964576c7da7784d060116a4970f60f6b895f10 Mon Sep 17 00:00:00 2001 From: Foudge Date: Sun, 28 Jan 2018 18:13:00 +0100 Subject: [PATCH 075/389] Compilation error under FreeBSD ULONG is not recognized under this OS, so replaced it with more portable definition. --- src/net/Client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index fcaec8eb..fb83acd2 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -532,7 +532,7 @@ void Client::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t auto client = getClient(handle->data); buf->base = &client->m_recvBuf.base[client->m_recvBufPos]; - buf->len = client->m_recvBuf.len - (ULONG)client->m_recvBufPos; + buf->len = client->m_recvBuf.len - (unsigned long)client->m_recvBufPos; } From a91759086288313d639ea353b3c9c6ebaaa605a2 Mon Sep 17 00:00:00 2001 From: DeadManWalking <34924727+DeadManWalkingTO@users.noreply.github.com> Date: Fri, 2 Feb 2018 00:14:39 +0200 Subject: [PATCH 076/389] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 6ab4b6a2..2d32c639 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,12 @@ # XMRig +[![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) +[![GitHub release](https://img.shields.io/github/release/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/releases) +[![GitHub Release Date](https://img.shields.io/github/release-date/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/releases) +[![GitHub license](https://img.shields.io/github/license/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/blob/master/LICENSE) +[![GitHub stars](https://img.shields.io/github/stars/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/stargazers) +[![GitHub forks](https://img.shields.io/github/forks/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/network) +> XMRig - Monero (XMR) CPU miner + XMRig is a high performance Monero (XMR) CPU miner, with official support for Windows. Originally based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of legacy code, since version 1.0.0 completely rewritten from scratch on C++. From 9f92449e15fe2bc53bf3b65d6e40536e83384430 Mon Sep 17 00:00:00 2001 From: DeadManWalking <34924727+DeadManWalkingTO@users.noreply.github.com> Date: Fri, 2 Feb 2018 00:54:58 +0200 Subject: [PATCH 077/389] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2d32c639..7209f1d0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # XMRig [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub release](https://img.shields.io/github/release/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/releases) -[![GitHub Release Date](https://img.shields.io/github/release-date/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/releases) +[![GitHub Release Date](https://img.shields.io/github/release-date/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub license](https://img.shields.io/github/license/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/blob/master/LICENSE) [![GitHub stars](https://img.shields.io/github/stars/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/stargazers) [![GitHub forks](https://img.shields.io/github/forks/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/network) From 75f462f0e18ecbd9cea933da8dc872d9f50c3f5f Mon Sep 17 00:00:00 2001 From: DeadManWalking <34924727+DeadManWalkingTO@users.noreply.github.com> Date: Fri, 2 Feb 2018 00:58:11 +0200 Subject: [PATCH 078/389] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7209f1d0..5f5a9db0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # XMRig [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) -[![GitHub release](https://img.shields.io/github/release/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/releases) -[![GitHub Release Date](https://img.shields.io/github/release-date/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) +[![GitHub release](https://img.shields.io/github/release/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) +[![GitHub Release Date](https://img.shields.io/github/release-date-pre/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub license](https://img.shields.io/github/license/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/blob/master/LICENSE) [![GitHub stars](https://img.shields.io/github/stars/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/stargazers) [![GitHub forks](https://img.shields.io/github/forks/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/network) From 037abd703720cc52f228c99d1e0e205ce48abd85 Mon Sep 17 00:00:00 2001 From: Foudge Date: Sat, 3 Feb 2018 16:03:14 +0100 Subject: [PATCH 079/389] Correct L2 cache size calculation for Intel Core 2 family This is a workaround for total L2 cache size calculation of Intel Core Solo, Core Duo, Core 2 Duo, Core 2 Quad and their Xeon homologue. These processors have L2 cache shared by 2 cores. There is maybe more CPU with L2 shared cache, but I am sure that these models are concerned and they are not so numerous. A better way would be to modify libcpuid to implement L2 cache counting. --- src/Cpu.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Cpu.cpp b/src/Cpu.cpp index ff6f49e9..b122e156 100644 --- a/src/Cpu.cpp +++ b/src/Cpu.cpp @@ -100,7 +100,13 @@ void Cpu::initCommon() m_l2_cache = data.l2_cache * (m_totalCores / 2) * m_sockets; m_l2_exclusive = true; } - else { + // Workaround for Intel Core Solo, Core Duo, Core 2 Duo, Core 2 Quad and their Xeon homologue + // These processors have L2 cache shared by 2 cores. + else if (data.vendor == VENDOR_INTEL && data.family == 0x06 && (data.model == 0x0E || data.model == 0x0F || data.model == 0x07)) { + int l2_count_per_socket = m_totalCores > 1 ? m_totalCores / 2 : 1; + m_l2_cache = data.l2_cache > 0 ? data.l2_cache * l2_count_per_socket * m_sockets : 0; + } + else{ m_l2_cache = data.l2_cache > 0 ? data.l2_cache * m_totalCores * m_sockets : 0; } From e78e810cfea6bb5594a6be3720378d83e29a3675 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 8 Feb 2018 17:02:32 +0700 Subject: [PATCH 080/389] Fix code style, replace tabs to space. --- src/Cpu.cpp | 14 +++++------ src/crypto/CryptoNight_x86.h | 46 ++++++++++++++++++------------------ src/crypto/soft_aes.h | 30 +++++++++++------------ 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/Cpu.cpp b/src/Cpu.cpp index b122e156..a619781e 100644 --- a/src/Cpu.cpp +++ b/src/Cpu.cpp @@ -100,13 +100,13 @@ void Cpu::initCommon() m_l2_cache = data.l2_cache * (m_totalCores / 2) * m_sockets; m_l2_exclusive = true; } - // Workaround for Intel Core Solo, Core Duo, Core 2 Duo, Core 2 Quad and their Xeon homologue - // These processors have L2 cache shared by 2 cores. - else if (data.vendor == VENDOR_INTEL && data.family == 0x06 && (data.model == 0x0E || data.model == 0x0F || data.model == 0x07)) { - int l2_count_per_socket = m_totalCores > 1 ? m_totalCores / 2 : 1; - m_l2_cache = data.l2_cache > 0 ? data.l2_cache * l2_count_per_socket * m_sockets : 0; - } - else{ + // Workaround for Intel Core Solo, Core Duo, Core 2 Duo, Core 2 Quad and their Xeon homologue + // These processors have L2 cache shared by 2 cores. + else if (data.vendor == VENDOR_INTEL && data.family == 0x06 && (data.model == 0x0E || data.model == 0x0F || data.model == 0x07)) { + int l2_count_per_socket = m_totalCores > 1 ? m_totalCores / 2 : 1; + m_l2_cache = data.l2_cache > 0 ? data.l2_cache * l2_count_per_socket * m_sockets : 0; + } + else{ m_l2_cache = data.l2_cache > 0 ? data.l2_cache * m_totalCores * m_sockets : 0; } diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 786d28f1..927aab72 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -324,18 +324,18 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * uint64_t idx0 = h0[0] ^ h0[4]; for (size_t i = 0; i < ITERATIONS; i++) { - __m128i cx; + __m128i cx; - if (SOFT_AES) { - cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); - } - else { - cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); - cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); - } - _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); - idx0 = EXTRACT64(cx); - bx0 = cx; + if (SOFT_AES) { + cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); + } + else { + cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); + } + _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + idx0 = EXTRACT64(cx); + bx0 = cx; uint64_t hi, lo, cl, ch; cl = ((uint64_t*) &l0[idx0 & MASK])[0]; @@ -385,19 +385,19 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, uint64_t idx0 = h0[0] ^ h0[4]; uint64_t idx1 = h1[0] ^ h1[4]; - for (size_t i = 0; i < ITERATIONS; i++) { - __m128i cx0, cx1; + for (size_t i = 0; i < ITERATIONS; i++) { + __m128i cx0, cx1; - if (SOFT_AES) { - cx0 = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); - cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], _mm_set_epi64x(ah1, al1)); - } - else { - cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); - cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); - cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); - cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); - } + if (SOFT_AES) { + cx0 = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); + cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], _mm_set_epi64x(ah1, al1)); + } + else { + cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); + cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); + cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); + } _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); diff --git a/src/crypto/soft_aes.h b/src/crypto/soft_aes.h index b7698ac4..0703f98d 100644 --- a/src/crypto/soft_aes.h +++ b/src/crypto/soft_aes.h @@ -91,32 +91,32 @@ alignas(16) const uint8_t saes_sbox[256] = saes_data(saes_h0); static inline __m128i soft_aesenc(const uint32_t* in, __m128i key) { - const uint32_t x0 = in[0]; - const uint32_t x1 = in[1]; - const uint32_t x2 = in[2]; - const uint32_t x3 = in[3]; + const uint32_t x0 = in[0]; + const uint32_t x1 = in[1]; + const uint32_t x2 = in[2]; + const uint32_t x3 = in[3]; - __m128i out = _mm_set_epi32( - (saes_table[0][x3 & 0xff] ^ saes_table[1][(x0 >> 8) & 0xff] ^ saes_table[2][(x1 >> 16) & 0xff] ^ saes_table[3][x2 >> 24]), - (saes_table[0][x2 & 0xff] ^ saes_table[1][(x3 >> 8) & 0xff] ^ saes_table[2][(x0 >> 16) & 0xff] ^ saes_table[3][x1 >> 24]), - (saes_table[0][x1 & 0xff] ^ saes_table[1][(x2 >> 8) & 0xff] ^ saes_table[2][(x3 >> 16) & 0xff] ^ saes_table[3][x0 >> 24]), - (saes_table[0][x0 & 0xff] ^ saes_table[1][(x1 >> 8) & 0xff] ^ saes_table[2][(x2 >> 16) & 0xff] ^ saes_table[3][x3 >> 24])); + __m128i out = _mm_set_epi32( + (saes_table[0][x3 & 0xff] ^ saes_table[1][(x0 >> 8) & 0xff] ^ saes_table[2][(x1 >> 16) & 0xff] ^ saes_table[3][x2 >> 24]), + (saes_table[0][x2 & 0xff] ^ saes_table[1][(x3 >> 8) & 0xff] ^ saes_table[2][(x0 >> 16) & 0xff] ^ saes_table[3][x1 >> 24]), + (saes_table[0][x1 & 0xff] ^ saes_table[1][(x2 >> 8) & 0xff] ^ saes_table[2][(x3 >> 16) & 0xff] ^ saes_table[3][x0 >> 24]), + (saes_table[0][x0 & 0xff] ^ saes_table[1][(x1 >> 8) & 0xff] ^ saes_table[2][(x2 >> 16) & 0xff] ^ saes_table[3][x3 >> 24])); - return _mm_xor_si128(out, key); + return _mm_xor_si128(out, key); } static inline uint32_t sub_word(uint32_t key) { - return (saes_sbox[key >> 24 ] << 24) | - (saes_sbox[(key >> 16) & 0xff] << 16 ) | - (saes_sbox[(key >> 8) & 0xff] << 8 ) | - saes_sbox[key & 0xff]; + return (saes_sbox[key >> 24 ] << 24) | + (saes_sbox[(key >> 16) & 0xff] << 16 ) | + (saes_sbox[(key >> 8) & 0xff] << 8 ) | + saes_sbox[key & 0xff]; } #if defined(__clang__) || defined(XMRIG_ARM) static inline uint32_t _rotr(uint32_t value, uint32_t amount) { - return (value >> amount) | (value << ((32 - amount) & 31)); + return (value >> amount) | (value << ((32 - amount) & 31)); } #endif From 184f79ad3f51db2e49aebd06565b05a4d358ab2f Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 8 Feb 2018 17:21:12 +0700 Subject: [PATCH 081/389] Fix code style, replace tabs to space #2. --- src/crypto/CryptoNight_arm.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 17bba7af..4ac14f34 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -361,13 +361,13 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * uint64_t idx0 = h0[0] ^ h0[4]; for (size_t i = 0; i < ITERATIONS; i++) { - __m128i cx; + __m128i cx; if (SOFT_AES) { cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); } else { - cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); # ifndef XMRIG_ARMv7 cx = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah0, al0); # endif @@ -433,8 +433,8 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], _mm_set_epi64x(ah1, al1)); } else { - cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); - cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); + cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); # ifndef XMRIG_ARMv7 cx0 = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx0, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah0, al0); cx1 = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx1, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah1, al1); From f0604d1e975b26ec1a2e5cba85e5d6daf8ebd074 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 18 Feb 2018 05:06:10 +0700 Subject: [PATCH 082/389] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6ab4b6a2..c33216d1 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ XMRig is a high performance Monero (XMR) CPU miner, with official support for Wi Originally based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of legacy code, since version 1.0.0 completely rewritten from scratch on C++. * This is the **CPU-mining** version, there is also a [NVIDIA GPU version](https://github.com/xmrig/xmrig-nvidia) and [AMD GPU version]( https://github.com/xmrig/xmrig-amd). +* You can use [config.xmrig.com](https://config.xmrig.com/) to generate/edit/share your configurations. :new: * [Roadmap](https://github.com/xmrig/xmrig/issues/106) for next releases. From c9acc2912e9e71d234648bec2595137bec355dfc Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 18 Feb 2018 05:32:36 +0700 Subject: [PATCH 083/389] Update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 878ff984..c23c5d29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# v2.4.5 +- [#324](https://github.com/xmrig/xmrig/pull/324) Fixed build without libmicrohttpd (CMake cache issue). +- [#341](https://github.com/xmrig/xmrig/issues/341) Fixed wrong exit code and added command line option `--dry-run`. +- [#385](https://github.com/xmrig/xmrig/pull/385) Up to 20% performance increase with for non-AES CPU and fixed Intel Core 2 cache detection. + # v2.4.4 - Added libmicrohttpd version to --version output. - Fixed bug in singal handler, in some cases miner wasn't shutdown properly. From cc22c9d61c3b4fb9b40c4895596fbf2ee48a8530 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 18 Feb 2018 05:49:37 +0700 Subject: [PATCH 084/389] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index ef385625..d208dfce 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ [![GitHub license](https://img.shields.io/github/license/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/blob/master/LICENSE) [![GitHub stars](https://img.shields.io/github/stars/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/stargazers) [![GitHub forks](https://img.shields.io/github/forks/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/network) -> XMRig - Monero (XMR) CPU miner XMRig is a high performance Monero (XMR) CPU miner, with official support for Windows. Originally based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of legacy code, since version 1.0.0 completely rewritten from scratch on C++. From f5a0429f0da3bc867c0c92d82e178467f4de2ab3 Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 19 Feb 2018 04:17:50 +0700 Subject: [PATCH 085/389] Update README.md --- README.md | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/README.md b/README.md index d208dfce..b563d5b0 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,6 @@ XMRig is a high performance Monero (XMR) CPU miner, with official support for Wi Originally based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of legacy code, since version 1.0.0 completely rewritten from scratch on C++. * This is the **CPU-mining** version, there is also a [NVIDIA GPU version](https://github.com/xmrig/xmrig-nvidia) and [AMD GPU version]( https://github.com/xmrig/xmrig-amd). -* You can use [config.xmrig.com](https://config.xmrig.com/) to generate/edit/share your configurations. :new: * [Roadmap](https://github.com/xmrig/xmrig/issues/106) for next releases. @@ -45,16 +44,7 @@ Originally based on cpuminer-multi with heavy optimizations/rewrites and removin * Clone with `git clone https://github.com/xmrig/xmrig.git` :hammer: [Build instructions](https://github.com/xmrig/xmrig/wiki/Build). ## Usage -### Basic example -``` -xmrig.exe -o pool.monero.hashvault.pro:5555 -u YOUR_WALLET -p x -k -``` - -### Failover -``` -xmrig.exe -o pool.monero.hashvault.pro:5555 -u YOUR_WALLET1 -p x -k -o pool.supportxmr.com:5555 -u YOUR_WALLET2 -p x -k -``` -For failover you can add multiple pools, maximum count not limited. +Use [config.xmrig.com](https://config.xmrig.com/xmrig) to generate, edit or share configurations. ### Options ``` From 9af8ceb063289c5d809cb7a181cb17b5088fec2e Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 19 Feb 2018 04:31:50 +0700 Subject: [PATCH 086/389] v2.4.5 RC --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index 52058eec..d34b45a5 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.4.4" +#define APP_VERSION "2.4.5" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 4 -#define APP_VER_BUILD 4 +#define APP_VER_BUILD 5 #define APP_VER_REV 0 #ifdef _MSC_VER From 79345119c60e53d0d29ef2bde92c50483e009bad Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 19 Feb 2018 15:58:44 +0700 Subject: [PATCH 087/389] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c23c5d29..8e2e6522 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # v2.4.5 - [#324](https://github.com/xmrig/xmrig/pull/324) Fixed build without libmicrohttpd (CMake cache issue). - [#341](https://github.com/xmrig/xmrig/issues/341) Fixed wrong exit code and added command line option `--dry-run`. -- [#385](https://github.com/xmrig/xmrig/pull/385) Up to 20% performance increase with for non-AES CPU and fixed Intel Core 2 cache detection. +- [#385](https://github.com/xmrig/xmrig/pull/385) Up to 20% performance increase for non-AES CPU and fixed Intel Core 2 cache detection. # v2.4.4 - Added libmicrohttpd version to --version output. From a0d4e4ed3f5fa1ead45a7a808b223e99c16f0029 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 20 Feb 2018 23:22:34 +0700 Subject: [PATCH 088/389] Run internal http server in main loop to avoid requirement to thread synchronization. --- src/api/Api.cpp | 14 ++------------ src/api/Api.h | 3 +-- src/api/Httpd.cpp | 28 ++++++++++++++++++++++++++-- src/api/Httpd.h | 5 ++++- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 729ebccd..98d441c1 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig + * Copyright 2016-2018 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -29,12 +29,10 @@ ApiState *Api::m_state = nullptr; -uv_mutex_t Api::m_mutex; bool Api::start() { - uv_mutex_init(&m_mutex); m_state = new ApiState(); return true; @@ -53,11 +51,7 @@ char *Api::get(const char *url, int *status) return nullptr; } - uv_mutex_lock(&m_mutex); - char *buf = m_state->get(url, status); - uv_mutex_unlock(&m_mutex); - - return buf; + return m_state->get(url, status); } @@ -67,9 +61,7 @@ void Api::tick(const Hashrate *hashrate) return; } - uv_mutex_lock(&m_mutex); m_state->tick(hashrate); - uv_mutex_unlock(&m_mutex); } @@ -79,7 +71,5 @@ void Api::tick(const NetworkState &network) return; } - uv_mutex_lock(&m_mutex); m_state->tick(network); - uv_mutex_unlock(&m_mutex); } diff --git a/src/api/Api.h b/src/api/Api.h index 72c65c3c..1f91b382 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig + * Copyright 2016-2018 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -45,7 +45,6 @@ public: private: static ApiState *m_state; - static uv_mutex_t m_mutex; }; #endif /* __API_H__ */ diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 996bc007..be696542 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig + * Copyright 2016-2018 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -36,6 +36,18 @@ Httpd::Httpd(int port, const char *accessToken) : m_port(port), m_daemon(nullptr) { + uv_idle_init(uv_default_loop(), &m_idle); + m_idle.data = this; +} + + +Httpd::~Httpd() +{ + uv_idle_stop(&m_idle); + + if (m_daemon) { + MHD_stop_daemon(m_daemon); + } } @@ -45,12 +57,18 @@ bool Httpd::start() return false; } - m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); + unsigned int flags = 0; + if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) { + flags |= MHD_USE_EPOLL_LINUX_ONLY; + } + + m_daemon = MHD_start_daemon(flags, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); if (!m_daemon) { LOG_ERR("HTTP Daemon failed to start."); return false; } + uv_idle_start(&m_idle, Httpd::onIdle); return true; } @@ -114,3 +132,9 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url MHD_Response *rsp = MHD_create_response_from_buffer(strlen(buf), (void*) buf, MHD_RESPMEM_MUST_FREE); return done(connection, status, rsp); } + + +void Httpd::onIdle(uv_idle_t *handle) +{ + MHD_run(static_cast(handle->data)->m_daemon); +} diff --git a/src/api/Httpd.h b/src/api/Httpd.h index 7a4dd932..6563364d 100644 --- a/src/api/Httpd.h +++ b/src/api/Httpd.h @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig + * Copyright 2016-2018 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -37,6 +37,7 @@ class Httpd { public: Httpd(int port, const char *accessToken); + ~Httpd(); bool start(); private: @@ -44,10 +45,12 @@ private: static int done(MHD_Connection *connection, int status, MHD_Response *rsp); static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); + static void onIdle(uv_idle_t *handle); const char *m_accessToken; const int m_port; MHD_Daemon *m_daemon; + uv_idle_t m_idle; }; #endif /* __HTTPD_H__ */ From ff6dc727f56f7adb2a10127cb4fc9e02dbc0bbea Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 21 Feb 2018 00:52:52 +0700 Subject: [PATCH 089/389] Added XMRIG_DEPS cmake variable for unified dependencies. --- cmake/FindMHD.cmake | 8 ++++++-- cmake/FindUV.cmake | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/cmake/FindMHD.cmake b/cmake/FindMHD.cmake index 8505b337..a20ecfec 100644 --- a/cmake/FindMHD.cmake +++ b/cmake/FindMHD.cmake @@ -8,12 +8,16 @@ find_path( MHD_INCLUDE_DIR NAMES microhttpd.h + PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" + PATH_SUFFIXES "include" DOC "microhttpd include dir" ) find_library( MHD_LIBRARY - NAMES microhttpd microhttpd-10 libmicrohttpd libmicrohttpd-dll + NAMES microhttpd libmicrohttpd + PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" + PATH_SUFFIXES "lib" DOC "microhttpd library" ) @@ -34,6 +38,6 @@ if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) endif() include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(mhd DEFAULT_MSG MHD_INCLUDE_DIR MHD_LIBRARY) +find_package_handle_standard_args(MHD DEFAULT_MSG MHD_LIBRARY MHD_INCLUDE_DIR) mark_as_advanced(MHD_INCLUDE_DIR MHD_LIBRARY) diff --git a/cmake/FindUV.cmake b/cmake/FindUV.cmake index e3c22d28..7906633f 100644 --- a/cmake/FindUV.cmake +++ b/cmake/FindUV.cmake @@ -1,5 +1,16 @@ -find_path(UV_INCLUDE_DIR NAMES uv.h) -find_library(UV_LIBRARY NAMES uv libuv) +find_path( + UV_INCLUDE_DIR + NAMES uv.h + PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" + PATH_SUFFIXES "include" +) + +find_library( + UV_LIBRARY + NAMES libuv.a uv libuv + PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" + PATH_SUFFIXES "lib" +) set(UV_LIBRARIES ${UV_LIBRARY}) set(UV_INCLUDE_DIRS ${UV_INCLUDE_DIR}) From 3edc30d0672c867ffea87392b743fcd3b4f53b87 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 1 Mar 2018 09:53:27 +0700 Subject: [PATCH 090/389] Use adaptive timer instead of idle handler for HTTP server. --- src/api/Httpd.cpp | 33 ++++++++++++++++++++++++++------- src/api/Httpd.h | 9 +++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index be696542..51476e67 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -32,22 +32,25 @@ Httpd::Httpd(int port, const char *accessToken) : - m_accessToken(accessToken), + m_idle(true), + m_accessToken(accessToken ? strdup(accessToken) : nullptr), m_port(port), m_daemon(nullptr) { - uv_idle_init(uv_default_loop(), &m_idle); - m_idle.data = this; + uv_timer_init(uv_default_loop(), &m_timer); + m_timer.data = this; } Httpd::~Httpd() { - uv_idle_stop(&m_idle); + uv_timer_stop(&m_timer); if (m_daemon) { MHD_stop_daemon(m_daemon); } + + delete m_accessToken; } @@ -68,7 +71,7 @@ bool Httpd::start() return false; } - uv_idle_start(&m_idle, Httpd::onIdle); + uv_timer_start(&m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval); return true; } @@ -92,6 +95,22 @@ int Httpd::auth(const char *header) } +void Httpd::run() +{ + MHD_run(m_daemon); + + const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS); + if (m_idle && info->num_connections) { + uv_timer_set_repeat(&m_timer, kActiveInterval); + m_idle = false; + } + else if (!m_idle && !info->num_connections) { + uv_timer_set_repeat(&m_timer, kIdleInterval); + m_idle = true; + } +} + + int Httpd::done(MHD_Connection *connection, int status, MHD_Response *rsp) { if (!rsp) { @@ -134,7 +153,7 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url } -void Httpd::onIdle(uv_idle_t *handle) +void Httpd::onTimer(uv_timer_t *handle) { - MHD_run(static_cast(handle->data)->m_daemon); + static_cast(handle->data)->run(); } diff --git a/src/api/Httpd.h b/src/api/Httpd.h index 6563364d..1bdfeeb6 100644 --- a/src/api/Httpd.h +++ b/src/api/Httpd.h @@ -41,16 +41,21 @@ public: bool start(); private: + constexpr static const int kIdleInterval = 200; + constexpr static const int kActiveInterval = 50; + int auth(const char *header); + void run(); static int done(MHD_Connection *connection, int status, MHD_Response *rsp); static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); - static void onIdle(uv_idle_t *handle); + static void onTimer(uv_timer_t *handle); + bool m_idle; const char *m_accessToken; const int m_port; MHD_Daemon *m_daemon; - uv_idle_t m_idle; + uv_timer_t m_timer; }; #endif /* __HTTPD_H__ */ From 48b1de0b59bc2969fd9a2b951773488b9501ae4e Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Fri, 2 Mar 2018 22:10:46 -0500 Subject: [PATCH 091/389] Changes for the Monero v1 PoW --- src/crypto/CryptoNight.cpp | 102 +++++++++++++++++++++++------------ src/crypto/CryptoNight.h | 4 +- src/crypto/CryptoNight_arm.h | 26 +++++++-- src/crypto/CryptoNight_x86.h | 26 +++++++-- src/workers/DoubleWorker.cpp | 18 ++++--- src/workers/SingleWorker.cpp | 4 +- 6 files changed, 128 insertions(+), 52 deletions(-) diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 3ac9e94b..ecc051f2 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -24,6 +24,34 @@ #include "crypto/CryptoNight.h" +// VARIANT ALTERATIONS +#define VARIANT1_CHECK() \ + if (MONERO && version > 6 && size < 43) \ + { \ + return false; \ + } + +#define VARIANT1_INIT(part) \ + const uint64_t tweak1_2_##part =\ + (MONERO && version > 6) ? \ + (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ + *(reinterpret_cast(ctx->state##part) + 24)) : 0; + +#define VARIANT1_1(p) \ + do if(version > 6) \ + { \ + const uint8_t tmp = reinterpret_cast(p)[11]; \ + static const uint32_t table = 0x75310; \ + const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ + ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ + } while(0) + +#define VARIANT1_2(p, part) \ + do if(MONERO && version > 6) { \ + (p) ^= tweak1_2_##part ; \ + } while(0) + + #if defined(XMRIG_ARM) # include "crypto/CryptoNight_arm.h" #else @@ -36,58 +64,67 @@ #include "Options.h" -void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx) = nullptr; + +bool (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = nullptr; -static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx) { +static bool cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, false>(input, size, output, ctx); + return cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, false, true>(input, size, output, ctx, version); +# else + return false; # endif } -static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { +static bool cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, false>(input, size, output, ctx); + return cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, false, true>(input, size, output, ctx, version); +# else + return false; # endif } -static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, true>(input, size, output, ctx); +static bool cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + return cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, true, true>(input, size, output, ctx, version); } -static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, true>(input, size, output, ctx); +static bool cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + return cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, true, true>(input, size, output, ctx, version); } #ifndef XMRIG_NO_AEON -static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - # if !defined(XMRIG_ARMv7) - cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, false>(input, size, output, ctx); -#endif -} - - -static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { +static bool cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, false>(input, size, output, ctx); + return cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, false, false>(input, size, output, ctx, version); +# else + return false; # endif } -static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, true>(input, size, output, ctx); +static bool cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +# if !defined(XMRIG_ARMv7) + return cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, false, false>(input, size, output, ctx, version); +# else + return false; +# endif } -static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, true>(input, size, output, ctx); +static bool cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + return cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, true, false>(input, size, output, ctx, version); } -void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx) = { + +static bool cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + return cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, true, false>(input, size, output, ctx, version); +} + +bool (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -98,7 +135,7 @@ void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_lite_av4_softaes_double }; #else -void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx) = { +bool (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -107,11 +144,10 @@ void (*cryptonight_variations[4])(const void *input, size_t size, void *output, #endif -bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) +bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx); - - return *reinterpret_cast(result.result + 24) < job.target(); + const bool rc = cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, version); + return rc && *reinterpret_cast(result.result + 24) < job.target(); } @@ -133,9 +169,9 @@ bool CryptoNight::init(int algo, int variant) } -void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx) +bool CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_hash_ctx(input, size, output, ctx); + return cryptonight_hash_ctx(input, size, output, ctx, version); } @@ -149,14 +185,14 @@ bool CryptoNight::selfTest(int algo) { struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); ctx->memory = (uint8_t *) _mm_malloc(MEMORY * 2, 16); - cryptonight_hash_ctx(test_input, 76, output, ctx); + const bool rc = cryptonight_hash_ctx(test_input, 76, output, ctx, 0); _mm_free(ctx->memory); _mm_free(ctx); # ifndef XMRIG_NO_AEON - return memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + return rc && memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # else - return memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + return rc && memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # endif } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 64fc0fd1..890e6263 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -50,9 +50,9 @@ class JobResult; class CryptoNight { public: - static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); + static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx, uint8_t version); static bool init(int algo, int variant); - static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx); + static bool hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); private: static bool selfTest(int algo); diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 4ac14f34..513fa884 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -344,11 +344,14 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template -inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +template +inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak(static_cast(input), (int) size, ctx->state0, 200); + VARIANT1_CHECK(); + VARIANT1_INIT(0); + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); const uint8_t* l0 = ctx->memory; @@ -374,6 +377,7 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * } _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + VARIANT1_1(&l0[idx0 & MASK]); idx0 = EXTRACT64(cx); bx0 = cx; @@ -385,8 +389,10 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*)&l0[idx0 & MASK])[0] = al0; ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -397,15 +403,20 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); + return true; } -template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +template +inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); + VARIANT1_CHECK(); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEM; uint64_t* h0 = reinterpret_cast(ctx->state0); @@ -443,6 +454,8 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + VARIANT1_1(&l0[idx0 & MASK]); + VARIANT1_1(&l1[idx1 & MASK]); idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); @@ -458,8 +471,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & MASK])[0] = al0; ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -472,8 +487,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & MASK])[0] = al1; ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; @@ -488,6 +505,7 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); + return true; } #endif /* __CRYPTONIGHT_ARM_H__ */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 927aab72..a9500850 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -307,11 +307,14 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template -inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +template +inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak(static_cast(input), (int) size, ctx->state0, 200); + VARIANT1_CHECK(); + VARIANT1_INIT(0); + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); const uint8_t* l0 = ctx->memory; @@ -334,6 +337,7 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); } _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + VARIANT1_1(&l0[idx0 & MASK]); idx0 = EXTRACT64(cx); bx0 = cx; @@ -345,8 +349,10 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*)&l0[idx0 & MASK])[0] = al0; ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -357,15 +363,20 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); + return true; } -template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +template +inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); + VARIANT1_CHECK(); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEM; uint64_t* h0 = reinterpret_cast(ctx->state0); @@ -401,6 +412,8 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + VARIANT1_1(&l0[idx0 & MASK]); + VARIANT1_1(&l1[idx1 & MASK]); idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); @@ -416,8 +429,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & MASK])[0] = al0; ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -430,8 +445,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & MASK])[0] = al1; ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; @@ -446,6 +463,7 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); + return true; } #endif /* __CRYPTONIGHT_X86_H__ */ diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 46d6f366..8c49649c 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -76,6 +76,8 @@ void DoubleWorker::start() consumeJob(); } + const uint8_t version = m_state->job.size() ? m_state->job.blob()[0] : 0; + while (!Workers::isOutdated(m_sequence)) { if ((m_count & 0xF) == 0) { storeStats(); @@ -85,15 +87,15 @@ void DoubleWorker::start() *Job::nonce(m_state->blob) = ++m_state->nonce1; *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx); + if (CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, version)) { + if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { + Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); + } - if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { - Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); - } - - if (*reinterpret_cast(m_hash + 32 + 24) < m_state->job.target()) { - Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce2, m_hash + 32, m_state->job.diff())); - } + if (*reinterpret_cast(m_hash + 32 + 24) < m_state->job.target()) { + Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce2, m_hash + 32, m_state->job.diff())); + } + } // print on error ? std::this_thread::yield(); } diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index ecb566f8..3a2cc32c 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -52,6 +52,8 @@ void SingleWorker::start() consumeJob(); } + const uint8_t version = m_job.size() ? m_job.blob()[0] : 0; + while (!Workers::isOutdated(m_sequence)) { if ((m_count & 0xF) == 0) { storeStats(); @@ -60,7 +62,7 @@ void SingleWorker::start() m_count++; *m_job.nonce() = ++m_result.nonce; - if (CryptoNight::hash(m_job, m_result, m_ctx)) { + if (CryptoNight::hash(m_job, m_result, m_ctx, version)) { Workers::submit(m_result); } From fc82cf1cf21b4f0aadb867e31efb26a1bad69686 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 5 Mar 2018 02:15:05 +0700 Subject: [PATCH 092/389] #428 Fixed regression with CPU cache size detection. --- src/Cpu.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cpu.cpp b/src/Cpu.cpp index a619781e..eba993b3 100644 --- a/src/Cpu.cpp +++ b/src/Cpu.cpp @@ -100,9 +100,9 @@ void Cpu::initCommon() m_l2_cache = data.l2_cache * (m_totalCores / 2) * m_sockets; m_l2_exclusive = true; } - // Workaround for Intel Core Solo, Core Duo, Core 2 Duo, Core 2 Quad and their Xeon homologue + // Workaround for Intel Pentium Dual-Core, Core Duo, Core 2 Duo, Core 2 Quad and their Xeon homologue // These processors have L2 cache shared by 2 cores. - else if (data.vendor == VENDOR_INTEL && data.family == 0x06 && (data.model == 0x0E || data.model == 0x0F || data.model == 0x07)) { + else if (data.vendor == VENDOR_INTEL && data.ext_family == 0x06 && (data.ext_model == 0x0E || data.ext_model == 0x0F || data.ext_model == 0x17)) { int l2_count_per_socket = m_totalCores > 1 ? m_totalCores / 2 : 1; m_l2_cache = data.l2_cache > 0 ? data.l2_cache * l2_count_per_socket * m_sockets : 0; } From a398c8af2fdcc8d2594b288995d253fd6906b9b9 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 5 Mar 2018 13:54:21 +0700 Subject: [PATCH 093/389] Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy parsed incorrectly. --- src/net/Client.cpp | 10 +++++----- src/net/Client.h | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index fb83acd2..eb07bf21 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -592,7 +592,7 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) client->m_recvBufPos += nread; char* end; - char* start = buf->base; + char* start = client->m_recvBuf.base; size_t remaining = client->m_recvBufPos; while ((end = static_cast(memchr(start, '\n', remaining))) != nullptr) { @@ -609,11 +609,11 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) return; } - if (start == buf->base) { + if (start == client->m_recvBuf.base) { return; } - memcpy(buf->base, start, remaining); + memcpy(client->m_recvBuf.base, start, remaining); client->m_recvBufPos = remaining; } diff --git a/src/net/Client.h b/src/net/Client.h index 699f4903..2cc16915 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From df6a397e52dc3f08afc9b9e97cccd9ac9a8178f6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 6 Mar 2018 17:06:07 +0700 Subject: [PATCH 094/389] Update copyright and move version into Job class. --- src/crypto/CryptoNight.cpp | 9 +++++---- src/crypto/CryptoNight.h | 7 ++++--- src/crypto/CryptoNight_arm.h | 5 +++-- src/crypto/CryptoNight_x86.h | 5 +++-- src/net/Job.cpp | 5 +++-- src/net/Job.h | 6 ++++-- src/workers/DoubleWorker.cpp | 9 ++++----- src/workers/DoubleWorker.h | 5 +++-- src/workers/SingleWorker.cpp | 9 ++++----- src/workers/SingleWorker.h | 5 +++-- 10 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index ecc051f2..8de52c5a 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -144,9 +145,9 @@ bool (*cryptonight_variations[4])(const void *input, size_t size, void *output, #endif -bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx, uint8_t version) +bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) { - const bool rc = cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, version); + const bool rc = cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.version()); return rc && *reinterpret_cast(result.result + 24) < job.target(); } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 890e6263..6fa4719c 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -50,7 +51,7 @@ class JobResult; class CryptoNight { public: - static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx, uint8_t version); + static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); static bool init(int algo, int variant); static bool hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 513fa884..b5dc789d 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2016 Imran Yusuff - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index a9500850..c2f02645 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/net/Job.cpp b/src/net/Job.cpp index c3d33739..e0e49b43 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/net/Job.h b/src/net/Job.h index bf8f8835..d74b64ed 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -54,6 +55,7 @@ public: inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } inline uint32_t diff() const { return (uint32_t) m_diff; } inline uint64_t target() const { return m_target; } + inline uint8_t version() const { return m_blob[0]; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setThreadId(int threadId) { m_threadId = threadId; } diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 8c49649c..aac503f1 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -76,8 +77,6 @@ void DoubleWorker::start() consumeJob(); } - const uint8_t version = m_state->job.size() ? m_state->job.blob()[0] : 0; - while (!Workers::isOutdated(m_sequence)) { if ((m_count & 0xF) == 0) { storeStats(); @@ -87,7 +86,7 @@ void DoubleWorker::start() *Job::nonce(m_state->blob) = ++m_state->nonce1; *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - if (CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, version)) { + if (CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.version())) { if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); } diff --git a/src/workers/DoubleWorker.h b/src/workers/DoubleWorker.h index 711f4bd1..9f29fa5b 100644 --- a/src/workers/DoubleWorker.h +++ b/src/workers/DoubleWorker.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 3a2cc32c..9f4a7484 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -52,8 +53,6 @@ void SingleWorker::start() consumeJob(); } - const uint8_t version = m_job.size() ? m_job.blob()[0] : 0; - while (!Workers::isOutdated(m_sequence)) { if ((m_count & 0xF) == 0) { storeStats(); @@ -62,7 +61,7 @@ void SingleWorker::start() m_count++; *m_job.nonce() = ++m_result.nonce; - if (CryptoNight::hash(m_job, m_result, m_ctx, version)) { + if (CryptoNight::hash(m_job, m_result, m_ctx)) { Workers::submit(m_result); } diff --git a/src/workers/SingleWorker.h b/src/workers/SingleWorker.h index 08ab1857..f7d9cff8 100644 --- a/src/workers/SingleWorker.h +++ b/src/workers/SingleWorker.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 8b6058500439b9a38c072796a42d7beaf3b70cc1 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 6 Mar 2018 21:34:20 +0700 Subject: [PATCH 095/389] PoW changes WIP --- CMakeLists.txt | 1 + src/Mem.cpp | 17 +++--- src/Mem_unix.cpp | 9 ++-- src/Mem_win.cpp | 7 +-- src/crypto/CryptoNight.cpp | 92 +++++++++++---------------------- src/crypto/CryptoNight.h | 10 ++-- src/crypto/CryptoNight_monero.h | 57 ++++++++++++++++++++ src/crypto/CryptoNight_x86.h | 3 +- src/workers/DoubleWorker.cpp | 16 +++--- 9 files changed, 121 insertions(+), 91 deletions(-) create mode 100644 src/crypto/CryptoNight_monero.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e1673ea..068642bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ set(HEADERS_CRYPTO src/crypto/c_keccak.h src/crypto/c_skein.h src/crypto/CryptoNight.h + src/crypto/CryptoNight_monero.h src/crypto/CryptoNight_test.h src/crypto/groestl_tables.h src/crypto/hash.h diff --git a/src/Mem.cpp b/src/Mem.cpp index 4437c673..1943b22a 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,10 +47,10 @@ cryptonight_ctx *Mem::create(int threadId) } # endif - cryptonight_ctx *ctx = reinterpret_cast(&m_memory[MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); + cryptonight_ctx *ctx = reinterpret_cast(&m_memory[MONERO_MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); const int ratio = m_doubleHash ? 2 : 1; - ctx->memory = &m_memory[MEMORY * (threadId * ratio + 1)]; + ctx->memory = &m_memory[MONERO_MEMORY * (threadId * ratio + 1)]; return ctx; } @@ -72,15 +73,15 @@ cryptonight_ctx *Mem::createLite(int threadId) { cryptonight_ctx *ctx; if (!m_doubleHash) { - const size_t offset = MEMORY * (threadId + 1); + const size_t offset = MONERO_MEMORY * (threadId + 1); - ctx = reinterpret_cast(&m_memory[offset + MEMORY_LITE]); + ctx = reinterpret_cast(&m_memory[offset + AEON_MEMORY]); ctx->memory = &m_memory[offset]; return ctx; } - ctx = reinterpret_cast(&m_memory[MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); - ctx->memory = &m_memory[MEMORY * (threadId + 1)]; + ctx = reinterpret_cast(&m_memory[MONERO_MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); + ctx->memory = &m_memory[MONERO_MEMORY * (threadId + 1)]; return ctx; } diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 43ffa664..ec4017da 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,7 +47,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_doubleHash = doubleHash; const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MEMORY * (threads * ratio + 1); + const size_t size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { m_memory = static_cast(_mm_malloc(size, 16)); @@ -83,7 +84,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) void Mem::release() { - const int size = MEMORY * (m_threads + 1); + const int size = MONERO_MEMORY * (m_threads + 1); if (m_flags & HugepagesEnabled) { if (m_flags & Lock) { diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index a4f92cfe..79c6d12e 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -151,7 +152,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_doubleHash = doubleHash; const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MEMORY * (threads * ratio + 1); + const size_t size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { m_memory = static_cast(_mm_malloc(size, 16)); diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 8de52c5a..cdba4051 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -25,33 +25,6 @@ #include "crypto/CryptoNight.h" -// VARIANT ALTERATIONS -#define VARIANT1_CHECK() \ - if (MONERO && version > 6 && size < 43) \ - { \ - return false; \ - } - -#define VARIANT1_INIT(part) \ - const uint64_t tweak1_2_##part =\ - (MONERO && version > 6) ? \ - (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ - *(reinterpret_cast(ctx->state##part) + 24)) : 0; - -#define VARIANT1_1(p) \ - do if(version > 6) \ - { \ - const uint8_t tmp = reinterpret_cast(p)[11]; \ - static const uint32_t table = 0x75310; \ - const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ - ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ - } while(0) - -#define VARIANT1_2(p, part) \ - do if(MONERO && version > 6) { \ - (p) ^= tweak1_2_##part ; \ - } while(0) - #if defined(XMRIG_ARM) # include "crypto/CryptoNight_arm.h" @@ -66,66 +39,58 @@ -bool (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = nullptr; +void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = nullptr; -static bool cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - return cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, false, true>(input, size, output, ctx, version); -# else - return false; + cryptonight_hash(input, size, output, ctx, version); # endif } -static bool cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - return cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, false, true>(input, size, output, ctx, version); -# else - return false; + cryptonight_double_hash(input, size, output, ctx, version); # endif } -static bool cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - return cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, true, true>(input, size, output, ctx, version); +static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + cryptonight_hash(input, size, output, ctx, version); } -static bool cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - return cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, true, true>(input, size, output, ctx, version); +static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + cryptonight_double_hash(input, size, output, ctx, version); } #ifndef XMRIG_NO_AEON -static bool cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - return cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, false, false>(input, size, output, ctx, version); -# else - return false; + cryptonight_hash(input, size, output, ctx, version); # endif } -static bool cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - return cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, false, false>(input, size, output, ctx, version); -# else - return false; + cryptonight_double_hash(input, size, output, ctx, version); # endif } -static bool cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - return cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, true, false>(input, size, output, ctx, version); +static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + cryptonight_hash(input, size, output, ctx, version); } -static bool cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - return cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, true, false>(input, size, output, ctx, version); +static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + cryptonight_double_hash(input, size, output, ctx, version); } -bool (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { +void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -136,7 +101,7 @@ bool (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_lite_av4_softaes_double }; #else -bool (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { +void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -147,8 +112,9 @@ bool (*cryptonight_variations[4])(const void *input, size_t size, void *output, bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) { - const bool rc = cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.version()); - return rc && *reinterpret_cast(result.result + 24) < job.target(); + cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.version()); + + return *reinterpret_cast(result.result + 24) < job.target(); } @@ -170,9 +136,9 @@ bool CryptoNight::init(int algo, int variant) } -bool CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version) +void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version) { - return cryptonight_hash_ctx(input, size, output, ctx, version); + cryptonight_hash_ctx(input, size, output, ctx, version); } @@ -183,17 +149,17 @@ bool CryptoNight::selfTest(int algo) { char output[64]; - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); - ctx->memory = (uint8_t *) _mm_malloc(MEMORY * 2, 16); + struct cryptonight_ctx *ctx = static_cast(_mm_malloc(sizeof(cryptonight_ctx), 16)); + ctx->memory = static_cast(_mm_malloc(MONERO_MEMORY * 2, 16)); - const bool rc = cryptonight_hash_ctx(test_input, 76, output, ctx, 0); + cryptonight_hash_ctx(test_input, 76, output, ctx, 0); _mm_free(ctx->memory); _mm_free(ctx); # ifndef XMRIG_NO_AEON - return rc && memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + return memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # else - return rc && memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + return memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # endif } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 6fa4719c..06bb9888 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -32,9 +32,13 @@ #include "align.h" +#define AEON_MEMORY 1048576 +#define AEON_MASK 0xFFFF0 +#define AEON_ITER 0x40000 -#define MEMORY 2097152 /* 2 MiB */ -#define MEMORY_LITE 1048576 /* 1 MiB */ +#define MONERO_MEMORY 2097152 +#define MONERO_MASK 0x1FFFF0 +#define MONERO_ITER 0x80000 struct cryptonight_ctx { @@ -53,7 +57,7 @@ class CryptoNight public: static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); static bool init(int algo, int variant); - static bool hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); + static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); private: static bool selfTest(int algo); diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h new file mode 100644 index 00000000..9bf3db84 --- /dev/null +++ b/src/crypto/CryptoNight_monero.h @@ -0,0 +1,57 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CRYPTONIGHT_MONERO_H__ +#define __CRYPTONIGHT_MONERO_H__ + + +// VARIANT ALTERATIONS +#define VARIANT1_INIT(part) \ + uint64_t tweak1_2_##part = 0; \ + if (MONERO) { \ + if (version > 6) { \ + tweak1_2_##part = (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ + *(reinterpret_cast(ctx->state##part) + 24)); \ + } \ + } + +#define VARIANT1_1(p) \ + if (MONERO) { \ + if (version > 6) { \ + const uint8_t tmp = reinterpret_cast(p)[11]; \ + static const uint32_t table = 0x75310; \ + const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ + ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ + } \ + } + +#define VARIANT1_2(p, part) \ + if (MONERO) { \ + if (version > 6) { \ + (p) ^= tweak1_2_##part; \ + } \ + } + + +#endif /* __CRYPTONIGHT_MONERO_H__ */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index c2f02645..d39948e8 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -35,6 +35,7 @@ #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_monero.h" #include "crypto/soft_aes.h" @@ -313,7 +314,6 @@ inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void * { keccak(static_cast(input), (int) size, ctx->state0, 200); - VARIANT1_CHECK(); VARIANT1_INIT(0); cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); @@ -374,7 +374,6 @@ inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); - VARIANT1_CHECK(); VARIANT1_INIT(0); VARIANT1_INIT(1); diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index aac503f1..d2960320 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -86,15 +86,15 @@ void DoubleWorker::start() *Job::nonce(m_state->blob) = ++m_state->nonce1; *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - if (CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.version())) { - if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { - Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); - } + CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.version()); - if (*reinterpret_cast(m_hash + 32 + 24) < m_state->job.target()) { - Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce2, m_hash + 32, m_state->job.diff())); - } - } // print on error ? + if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { + Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); + } + + if (*reinterpret_cast(m_hash + 32 + 24) < m_state->job.target()) { + Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce2, m_hash + 32, m_state->job.diff())); + } std::this_thread::yield(); } From 79779b51da5b2384e9fba3d50ceb299f843901e8 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Mar 2018 15:32:44 +0700 Subject: [PATCH 096/389] Added reference hashes. --- src/Mem.h | 5 ++-- src/crypto/CryptoNight.cpp | 48 +++++++++++++++++++++++++++-------- src/crypto/CryptoNight_arm.h | 23 +++-------------- src/crypto/CryptoNight_test.h | 14 ++++++++-- 4 files changed, 56 insertions(+), 34 deletions(-) diff --git a/src/Mem.h b/src/Mem.h index 58dba848..a9635a17 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index cdba4051..8a818fda 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -39,30 +39,50 @@ -void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = nullptr; +void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) = nullptr; static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_hash(input, size, output, ctx, version); + if (version > 6) { + cryptonight_hash(input, size, output, ctx, version); + } + else { + cryptonight_hash(input, size, output, ctx, version); + } # endif } static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_double_hash(input, size, output, ctx, version); + if (version > 6) { + cryptonight_double_hash(input, size, output, ctx, version); + } + else { + cryptonight_double_hash(input, size, output, ctx, version); + } # endif } static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_hash(input, size, output, ctx, version); + if (version > 6) { + cryptonight_hash(input, size, output, ctx, version); + } + else { + cryptonight_hash(input, size, output, ctx, version); + } } static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_double_hash(input, size, output, ctx, version); + if (version > 6) { + cryptonight_double_hash(input, size, output, ctx, version); + } + else { + cryptonight_double_hash(input, size, output, ctx, version); + } } @@ -154,12 +174,20 @@ bool CryptoNight::selfTest(int algo) { cryptonight_hash_ctx(test_input, 76, output, ctx, 0); +# ifndef XMRIG_NO_AEON + bool rc = memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; +# else + bool rc = memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; +# endif + + if (rc && algo == Options::ALGO_CRYPTONIGHT) { + cryptonight_hash_ctx(test_input, 76, output, ctx, 7); + + rc = memcmp(output, test_output2, (Options::i()->doubleHash() ? 64 : 32)) == 0; + } + _mm_free(ctx->memory); _mm_free(ctx); -# ifndef XMRIG_NO_AEON - return memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; -# else - return memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; -# endif + return rc; } diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index b5dc789d..6d8c454e 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -35,6 +35,7 @@ #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_monero.h" #include "crypto/soft_aes.h" @@ -137,20 +138,6 @@ static inline __m128i sl_xor(__m128i tmp1) } -template -static inline void aes_genkey_sub(__m128i* xout0, __m128i* xout2) -{ -// __m128i xout1 = _mm_aeskeygenassist_si128(*xout2, rcon); -// xout1 = _mm_shuffle_epi32(xout1, 0xFF); // see PSHUFD, set all elems to 4th elem -// *xout0 = sl_xor(*xout0); -// *xout0 = _mm_xor_si128(*xout0, xout1); -// xout1 = _mm_aeskeygenassist_si128(*xout0, 0x00); -// xout1 = _mm_shuffle_epi32(xout1, 0xAA); // see PSHUFD, set all elems to 3rd elem -// *xout2 = sl_xor(*xout2); -// *xout2 = _mm_xor_si128(*xout2, xout1); -} - - template static inline void soft_aes_genkey_sub(__m128i* xout0, __m128i* xout2) { @@ -346,11 +333,10 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) template -inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) +inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak(static_cast(input), (int) size, ctx->state0, 200); - VARIANT1_CHECK(); VARIANT1_INIT(0); cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); @@ -404,17 +390,15 @@ inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void * keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); - return true; } template -inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) +inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); - VARIANT1_CHECK(); VARIANT1_INIT(0); VARIANT1_INIT(1); @@ -506,7 +490,6 @@ inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); - return true; } #endif /* __CRYPTONIGHT_ARM_H__ */ diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index b2985379..65200f75 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -57,4 +58,13 @@ const static uint8_t test_output1[64] = { #endif +// Monero v7 +const static uint8_t test_output2[64] = { + 0xC9, 0xFA, 0xE8, 0x42, 0x5D, 0x86, 0x88, 0xDC, 0x23, 0x6B, 0xCD, 0xBC, 0x42, 0xFD, 0xB4, 0x2D, + 0x37, 0x6C, 0x6E, 0xC1, 0x90, 0x50, 0x1A, 0xA8, 0x4B, 0x04, 0xA4, 0xB4, 0xCF, 0x1E, 0xE1, 0x22, + 0xF2, 0x2D, 0x3D, 0x62, 0x03, 0xD2, 0xA0, 0x8B, 0x41, 0xD9, 0x02, 0x72, 0x78, 0xD8, 0xBC, 0xC9, + 0x83, 0xAC, 0xAD, 0xA9, 0xB6, 0x8E, 0x52, 0xE3, 0xC6, 0x89, 0x69, 0x2A, 0x50, 0xE9, 0x21, 0xD9 +}; + + #endif /* __CRYPTONIGHT_TEST_H__ */ From 8a6988d381551cb27ccfa19dd719dd28d09c863e Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Mar 2018 16:38:58 +0700 Subject: [PATCH 097/389] Added full IPv6 support. --- CMakeLists.txt | 2 +- src/api/Httpd.cpp | 18 +++++-- src/api/Httpd.h | 4 +- src/net/Client.cpp | 110 ++++++++++++++++++++++++-------------- src/net/Client.h | 14 +++-- src/net/{JobId.h => Id.h} | 35 ++++++++---- src/net/Job.h | 6 +-- src/net/JobResult.h | 12 ++--- src/net/Url.cpp | 67 +++++++++++++++++++++-- src/net/Url.h | 9 +++- 10 files changed, 200 insertions(+), 77 deletions(-) rename src/net/{JobId.h => Id.h} (75%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 068642bb..342951f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,8 +29,8 @@ set(HEADERS src/log/Log.h src/Mem.h src/net/Client.h + src/net/Id.h src/net/Job.h - src/net/JobId.h src/net/JobResult.h src/net/Network.h src/net/strategies/DonateStrategy.h diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 996bc007..a604b5de 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -45,7 +45,19 @@ bool Httpd::start() return false; } - m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); + unsigned int flags = 0; + if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) { + flags = MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_EPOLL_INTERNALLY_LINUX_ONLY; + } + else { + flags = MHD_USE_SELECT_INTERNALLY; + } + + if (MHD_is_feature_supported(MHD_FEATURE_IPv6)) { + flags |= MHD_USE_DUAL_STACK; + } + + m_daemon = MHD_start_daemon(flags, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); if (!m_daemon) { LOG_ERR("HTTP Daemon failed to start."); return false; diff --git a/src/api/Httpd.h b/src/api/Httpd.h index 7a4dd932..30618e2a 100644 --- a/src/api/Httpd.h +++ b/src/api/Httpd.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/net/Client.cpp b/src/net/Client.cpp index eb07bf21..d347ed22 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -54,6 +54,7 @@ int64_t Client::m_sequence = 1; Client::Client(int id, const char *agent, IClientListener *listener) : + m_ipv6(false), m_quiet(false), m_agent(agent), m_listener(listener), @@ -71,7 +72,7 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_resolver.data = this; - m_hints.ai_family = PF_INET; + m_hints.ai_family = AF_UNSPEC; m_hints.ai_socktype = SOCK_STREAM; m_hints.ai_protocol = IPPROTO_TCP; @@ -109,19 +110,6 @@ void Client::connect(const Url *url) } -void Client::disconnect() -{ -# ifndef XMRIG_PROXY_PROJECT - uv_timer_stop(&m_keepAliveTimer); -# endif - - m_expire = 0; - m_failures = -1; - - close(); -} - - void Client::setUrl(const Url *url) { if (!url || !url->isValid()) { @@ -150,6 +138,19 @@ void Client::tick(uint64_t now) } +bool Client::disconnect() +{ +# ifndef XMRIG_PROXY_PROJECT + uv_timer_stop(&m_keepAliveTimer); +# endif + + m_expire = 0; + m_failures = -1; + + return close(); +} + + int64_t Client::submit(const JobResult &result) { # ifdef XMRIG_PROXY_PROJECT @@ -167,13 +168,29 @@ int64_t Client::submit(const JobResult &result) # endif const size_t size = snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRIu64 ",\"jsonrpc\":\"2.0\",\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"}}\n", - m_sequence, m_rpcId, result.jobId.data(), nonce, data); + m_sequence, m_rpcId.data(), result.jobId.data(), nonce, data); m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff()); return send(size); } +bool Client::close() +{ + if (m_state == UnconnectedState || m_state == ClosingState || !m_socket) { + return false; + } + + setState(ClosingState); + + if (uv_is_closing(reinterpret_cast(m_socket)) == 0) { + uv_close(reinterpret_cast(m_socket), Client::onClose); + } + + return true; +} + + bool Client::isCriticalError(const char *message) { if (!message) { @@ -235,15 +252,11 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) bool Client::parseLogin(const rapidjson::Value &result, int *code) { - const char *id = result["id"].GetString(); - if (!id || strlen(id) >= sizeof(m_rpcId)) { + if (!m_rpcId.setId(result["id"].GetString())) { *code = 1; return false; } - memset(m_rpcId, 0, sizeof(m_rpcId)); - memcpy(m_rpcId, id, strlen(id)); - return parseJob(result["job"], code); } @@ -291,21 +304,25 @@ int64_t Client::send(size_t size) } -void Client::close() +void Client::connect(const std::vector &ipv4, const std::vector &ipv6) { - if (m_state == UnconnectedState || m_state == ClosingState || !m_socket) { - return; + addrinfo *addr = nullptr; + m_ipv6 = ipv4.empty() && !ipv6.empty(); + + if (m_ipv6) { + addr = ipv6[ipv6.size() == 1 ? 0 : rand() % ipv6.size()]; + uv_ip6_name(reinterpret_cast(addr->ai_addr), m_ip, 45); + } + else { + addr = ipv4[ipv4.size() == 1 ? 0 : rand() % ipv4.size()]; + uv_ip4_name(reinterpret_cast(addr->ai_addr), m_ip, 16); } - setState(ClosingState); - - if (uv_is_closing(reinterpret_cast(m_socket)) == 0) { - uv_close(reinterpret_cast(m_socket), Client::onClose); - } + connect(addr->ai_addr); } -void Client::connect(struct sockaddr *addr) +void Client::connect(sockaddr *addr) { setState(ConnectingState); @@ -374,6 +391,11 @@ void Client::parse(char *line, size_t len) LOG_DEBUG("[%s:%u] received (%d bytes): \"%s\"", m_url.host(), m_url.port(), len, line); + if (len < 32 || line[0] != '{') { + LOG_ERR("[%s:%u] JSON decode failed", m_url.host(), m_url.port()); + return; + } + rapidjson::Document doc; if (doc.ParseInsitu(line).HasParseError()) { if (!m_quiet) { @@ -456,7 +478,8 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap LOG_ERR("[%s:%u] login error code: %d", m_url.host(), m_url.port(), code); } - return close(); + close(); + return; } m_failures = 0; @@ -476,7 +499,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap void Client::ping() { - send(snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, m_rpcId)); + send(snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, m_rpcId.data())); } @@ -532,7 +555,7 @@ void Client::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t auto client = getClient(handle->data); buf->base = &client->m_recvBuf.base[client->m_recvBufPos]; - buf->len = client->m_recvBuf.len - (unsigned long)client->m_recvBufPos; + buf->len = client->m_recvBuf.len - client->m_recvBufPos; } @@ -582,11 +605,13 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror((int) nread)); } - return client->close(); + client->close(); + return; } if ((size_t) nread > (sizeof(m_buf) - 8 - client->m_recvBufPos)) { - return client->close(); + client->close(); + return; } client->m_recvBufPos += nread; @@ -628,24 +653,27 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) addrinfo *ptr = res; std::vector ipv4; + std::vector ipv6; while (ptr != nullptr) { if (ptr->ai_family == AF_INET) { ipv4.push_back(ptr); } + if (ptr->ai_family == AF_INET6) { + ipv6.push_back(ptr); + } + ptr = ptr->ai_next; } - if (ipv4.empty()) { - LOG_ERR("[%s:%u] DNS error: \"No IPv4 records found\"", client->m_url.host(), client->m_url.port()); + if (ipv4.empty() && ipv6.empty()) { + LOG_ERR("[%s:%u] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", client->m_url.host(), client->m_url.port()); + + uv_freeaddrinfo(res); return client->reconnect(); } - ptr = ipv4[rand() % ipv4.size()]; - - uv_ip4_name(reinterpret_cast(ptr->ai_addr), client->m_ip, 16); - - client->connect(ptr->ai_addr); + client->connect(ipv4, ipv6); uv_freeaddrinfo(res); } diff --git a/src/net/Client.h b/src/net/Client.h index 2cc16915..db480461 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -27,8 +27,10 @@ #include #include +#include +#include "net/Id.h" #include "net/Job.h" #include "net/SubmitResult.h" #include "net/Url.h" @@ -56,10 +58,10 @@ public: Client(int id, const char *agent, IClientListener *listener); ~Client(); + bool disconnect(); int64_t submit(const JobResult &result); void connect(); void connect(const Url *url); - void disconnect(); void setUrl(const Url *url); void tick(uint64_t now); @@ -74,13 +76,14 @@ public: inline void setRetryPause(int ms) { m_retryPause = ms; } private: + bool close(); bool isCriticalError(const char *message); bool parseJob(const rapidjson::Value ¶ms, int *code); bool parseLogin(const rapidjson::Value &result, int *code); int resolve(const char *host); int64_t send(size_t size); - void close(); - void connect(struct sockaddr *addr); + void connect(const std::vector &ipv4, const std::vector &ipv6); + void connect(sockaddr *addr); void login(); void parse(char *line, size_t len); void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error); @@ -99,10 +102,10 @@ private: static inline Client *getClient(void *data) { return static_cast(data); } addrinfo m_hints; + bool m_ipv6; bool m_quiet; char m_buf[2048]; - char m_ip[17]; - char m_rpcId[64]; + char m_ip[46]; char m_sendBuf[768]; const char *m_agent; IClientListener *m_listener; @@ -120,6 +123,7 @@ private: uv_getaddrinfo_t m_resolver; uv_stream_t *m_stream; uv_tcp_t *m_socket; + xmrig::Id m_rpcId; # ifndef XMRIG_PROXY_PROJECT uv_timer_t m_keepAliveTimer; diff --git a/src/net/JobId.h b/src/net/Id.h similarity index 75% rename from src/net/JobId.h rename to src/net/Id.h index 06189779..5c77d1d4 100644 --- a/src/net/JobId.h +++ b/src/net/Id.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,40 +21,51 @@ * along with this program. If not, see . */ -#ifndef __JOBID_H__ -#define __JOBID_H__ +#ifndef __ID_H__ +#define __ID_H__ #include -class JobId +namespace xmrig { + + +class Id { public: - inline JobId() + inline Id() { memset(m_data, 0, sizeof(m_data)); } - inline JobId(const char *id, size_t sizeFix = 0) + inline Id(const char *id, size_t sizeFix = 0) { setId(id, sizeFix); } - inline bool operator==(const JobId &other) const + inline bool operator==(const Id &other) const { return memcmp(m_data, other.m_data, sizeof(m_data)) == 0; } - inline bool operator!=(const JobId &other) const + inline bool operator!=(const Id &other) const { return memcmp(m_data, other.m_data, sizeof(m_data)) != 0; } + Id &operator=(const Id &other) + { + memcpy(m_data, other.m_data, sizeof(m_data)); + + return *this; + } + + inline bool setId(const char *id, size_t sizeFix = 0) { memset(m_data, 0, sizeof(m_data)); @@ -80,4 +91,8 @@ private: char m_data[64]; }; -#endif /* __JOBID_H__ */ + +} /* namespace xmrig */ + + +#endif /* __ID_H__ */ diff --git a/src/net/Job.h b/src/net/Job.h index d74b64ed..e2fe9f16 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -31,7 +31,7 @@ #include "align.h" -#include "net/JobId.h" +#include "net/Id.h" class Job @@ -46,9 +46,9 @@ public: inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } inline bool setId(const char *id) { return m_id.setId(id); } - inline const JobId &id() const { return m_id; } inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } inline const uint8_t *blob() const { return m_blob; } + inline const xmrig::Id &id() const { return m_id; } inline int poolId() const { return m_poolId; } inline int threadId() const { return m_threadId; } inline size_t size() const { return m_size; } @@ -77,10 +77,10 @@ private: bool m_nicehash; int m_poolId; int m_threadId; - JobId m_id; size_t m_size; uint64_t m_diff; uint64_t m_target; + xmrig::Id m_id; # ifdef XMRIG_PROXY_PROJECT VAR_ALIGN(16, char m_rawBlob[169]); diff --git a/src/net/JobResult.h b/src/net/JobResult.h index 520022a7..e3282584 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,11 +36,11 @@ class JobResult { public: inline JobResult() : poolId(0), diff(0), nonce(0) {} - inline JobResult(int poolId, const JobId &jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : + inline JobResult(int poolId, const xmrig::Id &jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : poolId(poolId), - jobId(jobId), diff(diff), - nonce(nonce) + nonce(nonce), + jobId(jobId) { memcpy(this->result, result, sizeof(this->result)); } @@ -71,10 +71,10 @@ public: int poolId; - JobId jobId; uint32_t diff; uint32_t nonce; uint8_t result[32]; + xmrig::Id jobId; }; #endif /* __JOBRESULT_H__ */ diff --git a/src/net/Url.cpp b/src/net/Url.cpp index dcbe82af..f58ca48a 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -41,6 +41,7 @@ Url::Url() : m_host(nullptr), m_password(nullptr), m_user(nullptr), + m_url(nullptr), m_port(kDefaultPort) { } @@ -63,6 +64,7 @@ Url::Url(const char *url) : m_host(nullptr), m_password(nullptr), m_user(nullptr), + m_url(nullptr), m_port(kDefaultPort) { parse(url); @@ -74,6 +76,7 @@ Url::Url(const char *host, uint16_t port, const char *user, const char *password m_nicehash(nicehash), m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), + m_url(nullptr), m_port(port) { m_host = strdup(host); @@ -85,6 +88,10 @@ Url::~Url() free(m_host); free(m_password); free(m_user); + + if (m_url) { + delete [] m_url; + } } @@ -105,6 +112,10 @@ bool Url::parse(const char *url) return false; } + if (base[0] == '[') { + return parseIPv6(base); + } + const char *port = strchr(base, ':'); if (!port) { m_host = strdup(base); @@ -112,9 +123,8 @@ bool Url::parse(const char *url) } const size_t size = port++ - base + 1; - m_host = static_cast(malloc(size)); + m_host = new char[size](); memcpy(m_host, base, size - 1); - m_host[size - 1] = '\0'; m_port = (uint16_t) strtol(port, nullptr, 10); return true; @@ -139,6 +149,19 @@ bool Url::setUserpass(const char *userpass) } +const char *Url::url() const +{ + if (!m_url) { + const size_t size = strlen(m_host) + 8; + m_url = new char[size]; + + snprintf(m_url, size - 1, "%s:%d", m_host, m_port); + } + + return m_url; +} + + void Url::applyExceptions() { if (!isValid()) { @@ -178,6 +201,20 @@ void Url::setUser(const char *user) } +bool Url::operator==(const Url &other) const +{ + if (m_port != other.m_port || m_keepAlive != other.m_keepAlive || m_nicehash != other.m_nicehash) { + return false; + } + + if (strcmp(host(), other.host()) != 0 || strcmp(user(), other.user()) != 0 || strcmp(password(), other.password()) != 0) { + return false; + } + + return true; +} + + Url &Url::operator=(const Url *other) { m_keepAlive = other->m_keepAlive; @@ -192,3 +229,25 @@ Url &Url::operator=(const Url *other) return *this; } + + +bool Url::parseIPv6(const char *addr) +{ + const char *end = strchr(addr, ']'); + if (!end) { + return false; + } + + const char *port = strchr(end, ':'); + if (!port) { + return false; + } + + const size_t size = end - addr; + m_host = new char[size](); + memcpy(m_host, addr + 1, size - 1); + + m_port = (uint16_t) strtol(port + 1, nullptr, 10); + + return true; +} diff --git a/src/net/Url.h b/src/net/Url.h index a1982300..330f2d8e 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -52,18 +52,23 @@ public: bool parse(const char *url); bool setUserpass(const char *userpass); + const char *url() const; void applyExceptions(); void setPassword(const char *password); void setUser(const char *user); + bool operator==(const Url &other) const; Url &operator=(const Url *other); private: + bool parseIPv6(const char *addr); + bool m_keepAlive; bool m_nicehash; char *m_host; char *m_password; char *m_user; + mutable char *m_url; uint16_t m_port; }; From 69b8a4faf19097835665e7010657ad25af6e8d7e Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Mar 2018 19:12:18 +0700 Subject: [PATCH 098/389] Added option to disable Monero v7 PoW, may useful in future if other coins update their network to v7 without PoW change. --- src/Options.cpp | 14 +++++++++++--- src/Options.h | 4 ++-- src/net/Client.cpp | 2 +- src/net/Job.cpp | 3 ++- src/net/Job.h | 6 ++++-- src/net/Url.cpp | 11 ++++++++++- src/net/Url.h | 5 ++++- 7 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/Options.cpp b/src/Options.cpp index 4e7c75ca..f60b1285 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -74,6 +74,7 @@ Options:\n\ --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ --no-huge-pages disable huge pages support\n\ --no-color disable colored output\n\ + --no-monero disable Monero v7 PoW\n\ --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ --user-agent set custom user-agent string for pool\n\ -B, --background run the miner in the background\n\ @@ -118,6 +119,7 @@ static struct option const options[] = { { "nicehash", 0, nullptr, 1006 }, { "no-color", 0, nullptr, 1002 }, { "no-huge-pages", 0, nullptr, 1009 }, + { "no-monero", 0, nullptr, 1010 }, { "pass", 1, nullptr, 'p' }, { "print-time", 1, nullptr, 1007 }, { "retries", 1, nullptr, 'r' }, @@ -158,11 +160,12 @@ static struct option const config_options[] = { static struct option const pool_options[] = { - { "url", 1, nullptr, 'o' }, { "pass", 1, nullptr, 'p' }, + { "url", 1, nullptr, 'o' }, { "user", 1, nullptr, 'u' }, { "userpass", 1, nullptr, 'O' }, { "keepalive", 0, nullptr ,'k' }, + { "monero", 0, nullptr, 1010 }, { "nicehash", 0, nullptr, 1006 }, { 0, 0, 0, 0 } }; @@ -392,6 +395,7 @@ bool Options::parseArg(int key, const char *arg) case 1002: /* --no-color */ case 1009: /* --no-huge-pages */ + case 1010: /* --no-monero */ return parseBoolean(key, false); case 't': /* --threads */ @@ -557,6 +561,10 @@ bool Options::parseBoolean(int key, bool enable) m_hugePages = enable; break; + case 1010: /* monero */ + m_pools.back()->setMonero(enable); + break; + case 2000: /* colors */ m_colors = enable; break; diff --git a/src/Options.h b/src/Options.h index 6f074917..625ac78d 100644 --- a/src/Options.h +++ b/src/Options.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/net/Client.cpp b/src/net/Client.cpp index d347ed22..a023ef38 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -220,7 +220,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } - Job job(m_id, m_url.isNicehash()); + Job job(m_id, m_url.isNicehash(), m_url.isMonero()); if (!job.setId(params["job_id"].GetString())) { *code = 3; return false; diff --git a/src/net/Job.cpp b/src/net/Job.cpp index e0e49b43..5820581f 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -56,7 +56,8 @@ static inline char hf_bin2hex(unsigned char c) } -Job::Job(int poolId, bool nicehash) : +Job::Job(int poolId, bool nicehash, bool monero) : + m_monero(monero), m_nicehash(nicehash), m_poolId(poolId), m_threadId(-1), diff --git a/src/net/Job.h b/src/net/Job.h index e2fe9f16..dc87eb8c 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -37,12 +37,13 @@ class Job { public: - Job(int poolId = -2, bool nicehash = false); + Job(int poolId = -2, bool nicehash = false, bool monero = true); ~Job(); bool setBlob(const char *blob); bool setTarget(const char *target); + inline bool isMonero() const { return m_monero; } inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } inline bool setId(const char *id) { return m_id.setId(id); } @@ -55,7 +56,7 @@ public: inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } inline uint32_t diff() const { return (uint32_t) m_diff; } inline uint64_t target() const { return m_target; } - inline uint8_t version() const { return m_blob[0]; } + inline uint8_t version() const { return isMonero() ? m_blob[0] : 0; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setThreadId(int threadId) { m_threadId = threadId; } @@ -74,6 +75,7 @@ public: private: VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. + bool m_monero; bool m_nicehash; int m_poolId; int m_threadId; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index f58ca48a..cd020b32 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -37,6 +37,7 @@ Url::Url() : m_keepAlive(false), + m_monero(true), m_nicehash(false), m_host(nullptr), m_password(nullptr), @@ -60,6 +61,7 @@ Url::Url() : */ Url::Url(const char *url) : m_keepAlive(false), + m_monero(true), m_nicehash(false), m_host(nullptr), m_password(nullptr), @@ -71,8 +73,9 @@ Url::Url(const char *url) : } -Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash) : +Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash, bool monero) : m_keepAlive(keepAlive), + m_monero(monero), m_nicehash(nicehash), m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), @@ -218,6 +221,7 @@ bool Url::operator==(const Url &other) const Url &Url::operator=(const Url *other) { m_keepAlive = other->m_keepAlive; + m_monero = other->m_monero; m_nicehash = other->m_nicehash; m_port = other->m_port; @@ -227,6 +231,11 @@ Url &Url::operator=(const Url *other) setPassword(other->m_password); setUser(other->m_user); + if (m_url) { + delete [] m_url; + m_url = nullptr; + } + return *this; } diff --git a/src/net/Url.h b/src/net/Url.h index 330f2d8e..32eea5bf 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -37,10 +37,11 @@ public: Url(); Url(const char *url); - Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false ); + Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false, bool monero = true); ~Url(); inline bool isKeepAlive() const { return m_keepAlive; } + inline bool isMonero() const { return m_monero; } inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_host && m_port > 0; } inline const char *host() const { return m_host; } @@ -48,6 +49,7 @@ public: inline const char *user() const { return m_user ? m_user : kDefaultUser; } inline uint16_t port() const { return m_port; } inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; } + inline void setMonero(bool monero) { m_monero = monero; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } bool parse(const char *url); @@ -64,6 +66,7 @@ private: bool parseIPv6(const char *addr); bool m_keepAlive; + bool m_monero; bool m_nicehash; char *m_host; char *m_password; From 37ac1aa62c5ac1159ef7e89dd0b5d576f0a7e060 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Mar 2018 21:56:43 +0700 Subject: [PATCH 099/389] Automatically enable nicehash when use with upcoming xmrig-proxy 2.5. --- src/net/Client.cpp | 27 ++++++++++++++++++++++++++- src/net/Client.h | 2 ++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index a023ef38..9c7f373e 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -55,6 +55,7 @@ int64_t Client::m_sequence = 1; Client::Client(int id, const char *agent, IClientListener *listener) : m_ipv6(false), + m_nicehash(false), m_quiet(false), m_agent(agent), m_listener(listener), @@ -220,7 +221,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } - Job job(m_id, m_url.isNicehash(), m_url.isMonero()); + Job job(m_id, m_nicehash, m_url.isMonero()); if (!job.setId(params["job_id"].GetString())) { *code = 3; return false; @@ -257,6 +258,12 @@ bool Client::parseLogin(const rapidjson::Value &result, int *code) return false; } + m_nicehash = m_url.isNicehash(); + + if (result.HasMember("extensions")) { + parseExtensions(result["extensions"]); + } + return parseJob(result["job"], code); } @@ -419,6 +426,24 @@ void Client::parse(char *line, size_t len) } +void Client::parseExtensions(const rapidjson::Value &value) +{ + if (!value.IsArray()) { + return; + } + + for (const rapidjson::Value &ext : value.GetArray()) { + if (!ext.IsString()) { + continue; + } + + if (strcmp(ext.GetString(), "nicehash") == 0) { + m_nicehash = true; + } + } +} + + void Client::parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error) { if (error.IsObject()) { diff --git a/src/net/Client.h b/src/net/Client.h index db480461..a927bd19 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -86,6 +86,7 @@ private: void connect(sockaddr *addr); void login(); void parse(char *line, size_t len); + void parseExtensions(const rapidjson::Value &value); void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error); void parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error); void ping(); @@ -103,6 +104,7 @@ private: addrinfo m_hints; bool m_ipv6; + bool m_nicehash; bool m_quiet; char m_buf[2048]; char m_ip[46]; From 84970e9e9b5ad1689899033482921436818adab6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 9 Mar 2018 00:50:06 +0700 Subject: [PATCH 100/389] Added coin field support added in xmrig-proxy 2.5. --- src/net/Client.cpp | 4 ++++ src/net/Job.cpp | 13 +++++++++++++ src/net/Job.h | 3 +++ 3 files changed, 20 insertions(+) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 9c7f373e..b0148336 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -237,6 +237,10 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } + if (params.HasMember("coin")) { + job.setCoin(params["coin"].GetString()); + } + if (m_job == job) { if (!m_quiet) { LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 5820581f..3faed6a1 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -65,6 +65,7 @@ Job::Job(int poolId, bool nicehash, bool monero) : m_diff(0), m_target(0) { + memset(m_coin, 0, sizeof(m_coin)); } @@ -148,6 +149,18 @@ bool Job::setTarget(const char *target) } +void Job::setCoin(const char *coin) +{ + if (!coin || strlen(coin) > 4) { + memset(m_coin, 0, sizeof(m_coin)); + return; + } + + strncpy(m_coin, coin, sizeof(m_coin)); + m_monero = strcmp(m_coin, "XMR") == 0; +} + + bool Job::fromHex(const char* in, unsigned int len, unsigned char* out) { bool error = false; diff --git a/src/net/Job.h b/src/net/Job.h index dc87eb8c..755de9ec 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -42,11 +42,13 @@ public: bool setBlob(const char *blob); bool setTarget(const char *target); + void setCoin(const char *coin); inline bool isMonero() const { return m_monero; } inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } inline bool setId(const char *id) { return m_id.setId(id); } + inline const char *coin() const { return m_coin; } inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } inline const uint8_t *blob() const { return m_blob; } inline const xmrig::Id &id() const { return m_id; } @@ -77,6 +79,7 @@ private: bool m_monero; bool m_nicehash; + char m_coin[5]; int m_poolId; int m_threadId; size_t m_size; From da4aa69c891519699232d5dbe269352c2e138404 Mon Sep 17 00:00:00 2001 From: xmrig Date: Fri, 9 Mar 2018 00:56:29 +0700 Subject: [PATCH 101/389] Update CHANGELOG.md --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e2e6522..5c50842f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# v2.5.0 +- **Added support for Monero v7 PoW, scheduled on March 28.** +- Added full IPv6 support. +- Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. +- [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. +- [#428](https://github.com/xmrig/xmrig/issues/428) Fixed regression (version 2.4.5 affected) with CPU cache size detection. + # v2.4.5 - [#324](https://github.com/xmrig/xmrig/pull/324) Fixed build without libmicrohttpd (CMake cache issue). - [#341](https://github.com/xmrig/xmrig/issues/341) Fixed wrong exit code and added command line option `--dry-run`. From 6e95b0d05248cd37fe8e9440d1ea2185de4206d7 Mon Sep 17 00:00:00 2001 From: xmrig Date: Fri, 9 Mar 2018 01:30:32 +0700 Subject: [PATCH 102/389] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c50842f..b00b1f1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # v2.5.0 -- **Added support for Monero v7 PoW, scheduled on March 28.** +- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on March 28.** - Added full IPv6 support. - Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. - [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. From b662cb77a4b8d0f80df348f4799a8164530cb9f6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 9 Mar 2018 01:47:44 +0700 Subject: [PATCH 103/389] Revert changes in Api class, single threaded http server will not be included in 2.5 release. --- src/api/Api.cpp | 14 ++++++++++++-- src/api/Api.h | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 98d441c1..729ebccd 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig + * Copyright 2016-2017 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -29,10 +29,12 @@ ApiState *Api::m_state = nullptr; +uv_mutex_t Api::m_mutex; bool Api::start() { + uv_mutex_init(&m_mutex); m_state = new ApiState(); return true; @@ -51,7 +53,11 @@ char *Api::get(const char *url, int *status) return nullptr; } - return m_state->get(url, status); + uv_mutex_lock(&m_mutex); + char *buf = m_state->get(url, status); + uv_mutex_unlock(&m_mutex); + + return buf; } @@ -61,7 +67,9 @@ void Api::tick(const Hashrate *hashrate) return; } + uv_mutex_lock(&m_mutex); m_state->tick(hashrate); + uv_mutex_unlock(&m_mutex); } @@ -71,5 +79,7 @@ void Api::tick(const NetworkState &network) return; } + uv_mutex_lock(&m_mutex); m_state->tick(network); + uv_mutex_unlock(&m_mutex); } diff --git a/src/api/Api.h b/src/api/Api.h index 1f91b382..72c65c3c 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig + * Copyright 2016-2017 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -45,6 +45,7 @@ public: private: static ApiState *m_state; + static uv_mutex_t m_mutex; }; #endif /* __API_H__ */ From ff68c047f9c3ad6ffa7ef0d81bf3bcfe2ae82cd7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 9 Mar 2018 01:49:24 +0700 Subject: [PATCH 104/389] v2.5.0-dev --- src/version.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/version.h b/src/version.h index d34b45a5..e0d4511a 100644 --- a/src/version.h +++ b/src/version.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,15 +27,15 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.4.5" +#define APP_VERSION "2.5.0-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" #define APP_KIND "cpu" #define APP_VER_MAJOR 2 -#define APP_VER_MINOR 4 -#define APP_VER_BUILD 5 +#define APP_VER_MINOR 5 +#define APP_VER_BUILD 0 #define APP_VER_REV 0 #ifdef _MSC_VER From 244ca93aa4cd9e10dfc159fc2d7a7be579f8f804 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 9 Mar 2018 13:15:55 +0700 Subject: [PATCH 105/389] Change donation address to separate old and new versions. --- src/net/strategies/DonateStrategy.cpp | 6 +++--- src/net/strategies/DonateStrategy.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index d7c721c6..288e6e48 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -48,7 +48,7 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : keccak(reinterpret_cast(user), static_cast(strlen(user)), hash, sizeof(hash)); Job::toHex(hash, 32, userId); - Url *url = new Url("fee.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443, userId, nullptr, false, true); + Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 80, userId, nullptr, false, true); m_client = new Client(-1, agent, this); m_client->setUrl(url); diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 302de292..07011053 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 83e5832bc16275002d1bb98e018446c8a67be08a Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 12 Mar 2018 14:44:23 +0700 Subject: [PATCH 106/389] Some small fixes. --- src/Cpu_stub.cpp | 15 ++++++++++----- src/crypto/CryptoNight_x86.h | 6 ++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Cpu_stub.cpp b/src/Cpu_stub.cpp index 0b9196ee..87d9de75 100644 --- a/src/Cpu_stub.cpp +++ b/src/Cpu_stub.cpp @@ -24,13 +24,18 @@ #ifdef _MSC_VER # include - -# define bit_AES (1 << 25) -# define bit_BMI2 (1 << 8) #else # include #endif +#ifndef bit_AES +# define bit_AES (1 << 25) +#endif + +#ifndef bit_BMI2 +# define bit_BMI2 (1 << 8) +#endif + #include @@ -87,7 +92,7 @@ static inline bool has_aes_ni() int cpu_info[4] = { 0 }; cpuid(PROCESSOR_INFO, cpu_info); - return cpu_info[ECX_Reg] & bit_AES; + return (cpu_info[ECX_Reg] & bit_AES) != 0; } @@ -95,7 +100,7 @@ static inline bool has_bmi2() { int cpu_info[4] = { 0 }; cpuid(EXTENDED_FEATURES, cpu_info); - return cpu_info[EBX_Reg] & bit_BMI2; + return (cpu_info[EBX_Reg] & bit_BMI2) != 0; } diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index d39948e8..7393bbfa 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -310,7 +310,7 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) template -inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) +inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak(static_cast(input), (int) size, ctx->state0, 200); @@ -364,12 +364,11 @@ inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void * keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); - return true; } template -inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) +inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); @@ -463,7 +462,6 @@ inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); - return true; } #endif /* __CRYPTONIGHT_X86_H__ */ From aec31c43c0efd7e228018336db8ed8e352e11ea9 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 12 Mar 2018 22:29:44 +0700 Subject: [PATCH 107/389] Better v1 PoW implementation, added variant option. --- CMakeLists.txt | 1 + src/Mem.cpp | 3 +- src/Mem_unix.cpp | 3 +- src/Mem_win.cpp | 3 +- src/Options.cpp | 37 +++++++----- src/Options.h | 6 +- src/crypto/CryptoNight.cpp | 87 ++++++++++++--------------- src/crypto/CryptoNight.h | 2 +- src/crypto/CryptoNight_arm.h | 8 +-- src/crypto/CryptoNight_monero.h | 26 +++----- src/crypto/CryptoNight_x86.h | 8 +-- src/net/Client.cpp | 6 +- src/net/Job.cpp | 53 +++++++++++++++- src/net/Job.h | 10 +-- src/net/Url.cpp | 34 +++++++++-- src/net/Url.h | 13 ++-- src/net/strategies/DonateStrategy.cpp | 3 +- src/workers/DoubleWorker.cpp | 2 +- src/xmrig.h | 47 +++++++++++++++ 19 files changed, 236 insertions(+), 116 deletions(-) create mode 100644 src/xmrig.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 342951f3..47a4d741 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ set(HEADERS src/workers/SingleWorker.h src/workers/Worker.h src/workers/Workers.h + src/xmrig.h ) set(HEADERS_CRYPTO diff --git a/src/Mem.cpp b/src/Mem.cpp index 1943b22a..73576d20 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -29,6 +29,7 @@ #include "crypto/CryptoNight.h" #include "Mem.h" #include "Options.h" +#include "xmrig.h" bool Mem::m_doubleHash = false; @@ -42,7 +43,7 @@ uint8_t *Mem::m_memory = nullptr; cryptonight_ctx *Mem::create(int threadId) { # ifndef XMRIG_NO_AEON - if (m_algo == Options::ALGO_CRYPTONIGHT_LITE) { + if (m_algo == xmrig::ALGO_CRYPTONIGHT_LITE) { return createLite(threadId); } # endif diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index ec4017da..93cdbec0 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -38,6 +38,7 @@ #include "log/Log.h" #include "Mem.h" #include "Options.h" +#include "xmrig.h" bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) @@ -46,7 +47,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_threads = threads; m_doubleHash = doubleHash; - const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; + const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; const size_t size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 79c6d12e..28eaff49 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -38,6 +38,7 @@ #include "crypto/CryptoNight.h" #include "Mem.h" #include "Options.h" +#include "xmrig.h" /***************************************************************** @@ -151,7 +152,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_threads = threads; m_doubleHash = doubleHash; - const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; + const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; const size_t size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { diff --git a/src/Options.cpp b/src/Options.cpp index f60b1285..939075f8 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -47,6 +47,7 @@ #include "rapidjson/error/en.h" #include "rapidjson/filereadstream.h" #include "version.h" +#include "xmrig.h" #ifndef ARRAY_SIZE @@ -74,7 +75,7 @@ Options:\n\ --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ --no-huge-pages disable huge pages support\n\ --no-color disable colored output\n\ - --no-monero disable Monero v7 PoW\n\ + --variant algorithm PoW variant\n\ --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ --user-agent set custom user-agent string for pool\n\ -B, --background run the miner in the background\n\ @@ -119,7 +120,7 @@ static struct option const options[] = { { "nicehash", 0, nullptr, 1006 }, { "no-color", 0, nullptr, 1002 }, { "no-huge-pages", 0, nullptr, 1009 }, - { "no-monero", 0, nullptr, 1010 }, + { "variant", 1, nullptr, 1010 }, { "pass", 1, nullptr, 'p' }, { "print-time", 1, nullptr, 1007 }, { "retries", 1, nullptr, 'r' }, @@ -160,12 +161,12 @@ static struct option const config_options[] = { static struct option const pool_options[] = { - { "pass", 1, nullptr, 'p' }, { "url", 1, nullptr, 'o' }, + { "pass", 1, nullptr, 'p' }, { "user", 1, nullptr, 'u' }, { "userpass", 1, nullptr, 'O' }, { "keepalive", 0, nullptr ,'k' }, - { "monero", 0, nullptr, 1010 }, + { "variant", 1, nullptr, 1010 }, { "nicehash", 0, nullptr, 1006 }, { 0, 0, 0, 0 } }; @@ -275,9 +276,7 @@ Options::Options(int argc, char **argv) : } } - for (Url *url : m_pools) { - url->applyExceptions(); - } + adjust(); m_ready = true; } @@ -383,6 +382,7 @@ bool Options::parseArg(int key, const char *arg) case 1007: /* --print-time */ case 1021: /* --cpu-priority */ case 4000: /* --api-port */ + case 1010: /* --variant */ return parseArg(key, strtol(arg, nullptr, 10)); case 'B': /* --background */ @@ -395,7 +395,6 @@ bool Options::parseArg(int key, const char *arg) case 1002: /* --no-color */ case 1009: /* --no-huge-pages */ - case 1010: /* --no-monero */ return parseBoolean(key, false); case 't': /* --threads */ @@ -502,6 +501,10 @@ bool Options::parseArg(int key, uint64_t arg) m_printTime = (int) arg; break; + case 1010: /* --variant */ + m_pools.back()->setVariant((int) arg); + break; + case 1020: /* --cpu-affinity */ if (arg) { m_affinity = arg; @@ -561,10 +564,6 @@ bool Options::parseBoolean(int key, bool enable) m_hugePages = enable; break; - case 1010: /* monero */ - m_pools.back()->setMonero(enable); - break; - case 2000: /* colors */ m_colors = enable; break; @@ -593,6 +592,14 @@ Url *Options::parseUrl(const char *arg) const } +void Options::adjust() +{ + for (Url *url : m_pools) { + url->adjust(m_algo); + } +} + + void Options::parseConfig(const char *fileName) { rapidjson::Document doc; @@ -637,7 +644,7 @@ void Options::parseJSON(const struct option *option, const rapidjson::Value &obj if (option->has_arg && value.IsString()) { parseArg(option->val, value.GetString()); } - else if (option->has_arg && value.IsUint64()) { + else if (option->has_arg && value.IsInt64()) { parseArg(option->val, value.GetUint64()); } else if (!option->has_arg && value.IsBool()) { @@ -703,7 +710,7 @@ bool Options::setAlgo(const char *algo) # ifndef XMRIG_NO_AEON if (i == ARRAY_SIZE(algo_names) - 1 && !strcmp(algo, "cryptonight-light")) { - m_algo = ALGO_CRYPTONIGHT_LITE; + m_algo = xmrig::ALGO_CRYPTONIGHT_LITE; break; } # endif @@ -721,7 +728,7 @@ bool Options::setAlgo(const char *algo) int Options::getAlgoVariant() const { # ifndef XMRIG_NO_AEON - if (m_algo == ALGO_CRYPTONIGHT_LITE) { + if (m_algo == xmrig::ALGO_CRYPTONIGHT_LITE) { return getAlgoVariantLite(); } # endif diff --git a/src/Options.h b/src/Options.h index 625ac78d..e29059ec 100644 --- a/src/Options.h +++ b/src/Options.h @@ -39,11 +39,6 @@ struct option; class Options { public: - enum Algo { - ALGO_CRYPTONIGHT, /* CryptoNight (Monero) */ - ALGO_CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ - }; - enum AlgoVariant { AV0_AUTO, AV1_AESNI, @@ -96,6 +91,7 @@ private: bool parseArg(int key, uint64_t arg); bool parseBoolean(int key, bool enable); Url *parseUrl(const char *arg) const; + void adjust(); void parseConfig(const char *fileName); void parseJSON(const struct option *option, const rapidjson::Value &object); void showUsage(int status) const; diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 8a818fda..1d71d1cd 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -36,81 +36,74 @@ #include "net/Job.h" #include "net/JobResult.h" #include "Options.h" +#include "xmrig.h" - -void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) = nullptr; +void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) = nullptr; -static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { +#define CRYPTONIGHT_HASH(NAME, ITERATIONS, MEM, MASK, SOFT_AES) \ + switch (variant) { \ + case xmrig::VARIANT_V1: \ + return cryptonight_##NAME##_hash(input, size, output, ctx); \ + \ + case xmrig::VARIANT_NONE: \ + return cryptonight_##NAME##_hash(input, size, output, ctx); \ + \ + default: \ + break; \ + } + + +static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - if (version > 6) { - cryptonight_hash(input, size, output, ctx, version); - } - else { - cryptonight_hash(input, size, output, ctx, version); - } + CRYPTONIGHT_HASH(single, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, false) # endif } -static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - if (version > 6) { - cryptonight_double_hash(input, size, output, ctx, version); - } - else { - cryptonight_double_hash(input, size, output, ctx, version); - } + CRYPTONIGHT_HASH(double, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, false) # endif } -static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - if (version > 6) { - cryptonight_hash(input, size, output, ctx, version); - } - else { - cryptonight_hash(input, size, output, ctx, version); - } +static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { + CRYPTONIGHT_HASH(single, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, true) } -static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - if (version > 6) { - cryptonight_double_hash(input, size, output, ctx, version); - } - else { - cryptonight_double_hash(input, size, output, ctx, version); - } +static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { + CRYPTONIGHT_HASH(double, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, true) } #ifndef XMRIG_NO_AEON -static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - cryptonight_hash(input, size, output, ctx, version); + CRYPTONIGHT_HASH(single, AEON_ITER, AEON_MEMORY, AEON_MASK, false) # endif } -static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - cryptonight_double_hash(input, size, output, ctx, version); + CRYPTONIGHT_HASH(double, AEON_ITER, AEON_MEMORY, AEON_MASK, false) # endif } -static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_hash(input, size, output, ctx, version); +static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { + CRYPTONIGHT_HASH(single, AEON_ITER, AEON_MEMORY, AEON_MASK, true) } -static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_double_hash(input, size, output, ctx, version); +static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { + CRYPTONIGHT_HASH(double, AEON_ITER, AEON_MEMORY, AEON_MASK, true) } -void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { +void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -121,7 +114,7 @@ void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_lite_av4_softaes_double }; #else -void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { +void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -132,7 +125,7 @@ void (*cryptonight_variations[4])(const void *input, size_t size, void *output, bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) { - cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.version()); + cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.variant()); return *reinterpret_cast(result.result + 24) < job.target(); } @@ -145,7 +138,7 @@ bool CryptoNight::init(int algo, int variant) } # ifndef XMRIG_NO_AEON - const int index = algo == Options::ALGO_CRYPTONIGHT_LITE ? (variant + 3) : (variant - 1); + const int index = algo == xmrig::ALGO_CRYPTONIGHT_LITE ? (variant + 3) : (variant - 1); # else const int index = variant - 1; # endif @@ -156,9 +149,9 @@ bool CryptoNight::init(int algo, int variant) } -void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version) +void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { - cryptonight_hash_ctx(input, size, output, ctx, version); + cryptonight_hash_ctx(input, size, output, ctx, variant); } @@ -175,13 +168,13 @@ bool CryptoNight::selfTest(int algo) { cryptonight_hash_ctx(test_input, 76, output, ctx, 0); # ifndef XMRIG_NO_AEON - bool rc = memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + bool rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # else bool rc = memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # endif - if (rc && algo == Options::ALGO_CRYPTONIGHT) { - cryptonight_hash_ctx(test_input, 76, output, ctx, 7); + if (rc && algo == xmrig::ALGO_CRYPTONIGHT) { + cryptonight_hash_ctx(test_input, 76, output, ctx, 1); rc = memcmp(output, test_output2, (Options::i()->doubleHash() ? 64 : 32)) == 0; } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 06bb9888..9526309d 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -57,7 +57,7 @@ class CryptoNight public: static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); static bool init(int algo, int variant); - static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); + static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant); private: static bool selfTest(int algo); diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 6d8c454e..6b214d66 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -332,8 +332,8 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template -inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) +template +inline void cryptonight_single_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) { keccak(static_cast(input), (int) size, ctx->state0, 200); @@ -393,8 +393,8 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * } -template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) +template +inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index 9bf3db84..3c6146ac 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -29,28 +29,22 @@ // VARIANT ALTERATIONS #define VARIANT1_INIT(part) \ uint64_t tweak1_2_##part = 0; \ - if (MONERO) { \ - if (version > 6) { \ - tweak1_2_##part = (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ - *(reinterpret_cast(ctx->state##part) + 24)); \ - } \ + if (VARIANT > 0) { \ + tweak1_2_##part = (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ + *(reinterpret_cast(ctx->state##part) + 24)); \ } #define VARIANT1_1(p) \ - if (MONERO) { \ - if (version > 6) { \ - const uint8_t tmp = reinterpret_cast(p)[11]; \ - static const uint32_t table = 0x75310; \ - const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ - ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ - } \ + if (VARIANT > 0) { \ + const uint8_t tmp = reinterpret_cast(p)[11]; \ + static const uint32_t table = 0x75310; \ + const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ + ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ } #define VARIANT1_2(p, part) \ - if (MONERO) { \ - if (version > 6) { \ - (p) ^= tweak1_2_##part; \ - } \ + if (VARIANT > 0) { \ + (p) ^= tweak1_2_##part; \ } diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 7393bbfa..1e5190d8 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -309,8 +309,8 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template -inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) +template +inline void cryptonight_single_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) { keccak(static_cast(input), (int) size, ctx->state0, 200); @@ -367,8 +367,8 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * } -template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) +template +inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); diff --git a/src/net/Client.cpp b/src/net/Client.cpp index b0148336..f6543b4e 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -221,7 +221,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } - Job job(m_id, m_nicehash, m_url.isMonero()); + Job job(m_id, m_nicehash, m_url.algo(), m_url.variant()); if (!job.setId(params["job_id"].GetString())) { *code = 3; return false; @@ -241,6 +241,10 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) job.setCoin(params["coin"].GetString()); } + if (params.HasMember("variant")) { + job.setVariant(params["variant"].GetInt()); + } + if (m_job == job) { if (!m_quiet) { LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 3faed6a1..c8331fb9 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -27,6 +27,7 @@ #include "net/Job.h" +#include "xmrig.h" static inline unsigned char hf_hex2bin(char c, bool &err) @@ -56,11 +57,26 @@ static inline char hf_bin2hex(unsigned char c) } -Job::Job(int poolId, bool nicehash, bool monero) : - m_monero(monero), +Job::Job() : + m_nicehash(false), + m_algo(xmrig::ALGO_CRYPTONIGHT), + m_poolId(-2), + m_threadId(-1), + m_variant(xmrig::VARIANT_AUTO), + m_size(0), + m_diff(0), + m_target(0) +{ + memset(m_coin, 0, sizeof(m_coin)); +} + + +Job::Job(int poolId, bool nicehash, int algo, int variant) : m_nicehash(nicehash), + m_algo(algo), m_poolId(poolId), m_threadId(-1), + m_variant(variant), m_size(0), m_diff(0), m_target(0) @@ -149,6 +165,22 @@ bool Job::setTarget(const char *target) } +int Job::variant() const +{ + if (m_variant != xmrig::VARIANT_AUTO) { + return m_variant; + } + + const uint8_t version = m_blob[0]; + + if (m_algo == xmrig::ALGO_CRYPTONIGHT) { + return version > 6 ? 1 : 0; + } + + return version > 1 ? 1 : 0; +} + + void Job::setCoin(const char *coin) { if (!coin || strlen(coin) > 4) { @@ -157,7 +189,22 @@ void Job::setCoin(const char *coin) } strncpy(m_coin, coin, sizeof(m_coin)); - m_monero = strcmp(m_coin, "XMR") == 0; + m_algo = strcmp(m_coin, "AEON") == 0 ? xmrig::ALGO_CRYPTONIGHT_LITE : xmrig::ALGO_CRYPTONIGHT; +} + + +void Job::setVariant(int variant) +{ + switch (variant) { + case xmrig::VARIANT_AUTO: + case xmrig::VARIANT_NONE: + case xmrig::VARIANT_V1: + m_variant = variant; + break; + + default: + break; + } } diff --git a/src/net/Job.h b/src/net/Job.h index 755de9ec..28d5e111 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -37,14 +37,16 @@ class Job { public: - Job(int poolId = -2, bool nicehash = false, bool monero = true); + Job(); + Job(int poolId, bool nicehash, int algo, int variant); ~Job(); bool setBlob(const char *blob); bool setTarget(const char *target); + int variant() const; void setCoin(const char *coin); + void setVariant(int variant); - inline bool isMonero() const { return m_monero; } inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } inline bool setId(const char *id) { return m_id.setId(id); } @@ -58,7 +60,6 @@ public: inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } inline uint32_t diff() const { return (uint32_t) m_diff; } inline uint64_t target() const { return m_target; } - inline uint8_t version() const { return isMonero() ? m_blob[0] : 0; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setThreadId(int threadId) { m_threadId = threadId; } @@ -77,11 +78,12 @@ public: private: VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. - bool m_monero; bool m_nicehash; char m_coin[5]; + int m_algo; int m_poolId; int m_threadId; + int m_variant; size_t m_size; uint64_t m_diff; uint64_t m_target; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index cd020b32..c17ef690 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -28,6 +28,7 @@ #include "net/Url.h" +#include "xmrig.h" #ifdef _MSC_VER @@ -37,11 +38,12 @@ Url::Url() : m_keepAlive(false), - m_monero(true), m_nicehash(false), m_host(nullptr), m_password(nullptr), m_user(nullptr), + m_algo(xmrig::ALGO_CRYPTONIGHT), + m_variant(xmrig::VARIANT_AUTO), m_url(nullptr), m_port(kDefaultPort) { @@ -61,11 +63,12 @@ Url::Url() : */ Url::Url(const char *url) : m_keepAlive(false), - m_monero(true), m_nicehash(false), m_host(nullptr), m_password(nullptr), m_user(nullptr), + m_algo(xmrig::ALGO_CRYPTONIGHT), + m_variant(xmrig::VARIANT_AUTO), m_url(nullptr), m_port(kDefaultPort) { @@ -73,12 +76,13 @@ Url::Url(const char *url) : } -Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash, bool monero) : +Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash, int variant) : m_keepAlive(keepAlive), - m_monero(monero), m_nicehash(nicehash), m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), + m_algo(xmrig::ALGO_CRYPTONIGHT), + m_variant(variant), m_url(nullptr), m_port(port) { @@ -165,12 +169,14 @@ const char *Url::url() const } -void Url::applyExceptions() +void Url::adjust(int algo) { if (!isValid()) { return; } + m_algo = algo; + if (strstr(m_host, ".nicehash.com")) { m_keepAlive = false; m_nicehash = true; @@ -204,6 +210,21 @@ void Url::setUser(const char *user) } +void Url::setVariant(int variant) +{ + switch (variant) { + case xmrig::VARIANT_AUTO: + case xmrig::VARIANT_NONE: + case xmrig::VARIANT_V1: + m_variant = variant; + break; + + default: + break; + } +} + + bool Url::operator==(const Url &other) const { if (m_port != other.m_port || m_keepAlive != other.m_keepAlive || m_nicehash != other.m_nicehash) { @@ -221,7 +242,8 @@ bool Url::operator==(const Url &other) const Url &Url::operator=(const Url *other) { m_keepAlive = other->m_keepAlive; - m_monero = other->m_monero; + m_algo = other->m_algo; + m_variant = other->m_variant; m_nicehash = other->m_nicehash; m_port = other->m_port; diff --git a/src/net/Url.h b/src/net/Url.h index 32eea5bf..f861fec5 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -37,27 +37,29 @@ public: Url(); Url(const char *url); - Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false, bool monero = true); + Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false, int variant = -1); ~Url(); inline bool isKeepAlive() const { return m_keepAlive; } - inline bool isMonero() const { return m_monero; } inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_host && m_port > 0; } inline const char *host() const { return m_host; } inline const char *password() const { return m_password ? m_password : kDefaultPassword; } inline const char *user() const { return m_user ? m_user : kDefaultUser; } + inline int algo() const { return m_algo; } + inline int variant() const { return m_variant; } inline uint16_t port() const { return m_port; } inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; } - inline void setMonero(bool monero) { m_monero = monero; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } + inline void setVariant(bool monero) { m_variant = monero; } bool parse(const char *url); bool setUserpass(const char *userpass); const char *url() const; - void applyExceptions(); + void adjust(int algo); void setPassword(const char *password); void setUser(const char *user); + void setVariant(int variant); bool operator==(const Url &other) const; Url &operator=(const Url *other); @@ -66,11 +68,12 @@ private: bool parseIPv6(const char *addr); bool m_keepAlive; - bool m_monero; bool m_nicehash; char *m_host; char *m_password; char *m_user; + int m_algo; + int m_variant; mutable char *m_url; uint16_t m_port; }; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 288e6e48..22cffd28 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -27,6 +27,7 @@ #include "net/Job.h" #include "net/strategies/DonateStrategy.h" #include "Options.h" +#include "xmrig.h" extern "C" @@ -48,7 +49,7 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : keccak(reinterpret_cast(user), static_cast(strlen(user)), hash, sizeof(hash)); Job::toHex(hash, 32, userId); - Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 80, userId, nullptr, false, true); + Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == xmrig::ALGO_CRYPTONIGHT_LITE ? 3333 : 80, userId, nullptr, false, true); m_client = new Client(-1, agent, this); m_client->setUrl(url); diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index d2960320..46c8ed2e 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -86,7 +86,7 @@ void DoubleWorker::start() *Job::nonce(m_state->blob) = ++m_state->nonce1; *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.version()); + CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.variant()); if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); diff --git a/src/xmrig.h b/src/xmrig.h new file mode 100644 index 00000000..103e8a68 --- /dev/null +++ b/src/xmrig.h @@ -0,0 +1,47 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __XMRIG_H__ +#define __XMRIG_H__ + + +namespace xmrig +{ + + +enum Algo { + ALGO_CRYPTONIGHT, /* CryptoNight (Monero) */ + ALGO_CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ +}; + + +enum Variant { + VARIANT_AUTO = -1, + VARIANT_NONE = 0, + VARIANT_V1 = 1 +}; + +} /* xmrig */ + + +#endif /* __XMRIG_H__ */ From 125072a30e2465b6a77d9b4342ac14b62b3468c3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 01:29:31 +0700 Subject: [PATCH 108/389] Added test hashes for AEON. --- src/crypto/CryptoNight.cpp | 14 ++++++++++---- src/crypto/CryptoNight_test.h | 33 +++++++++++++++++++++------------ src/net/Job.cpp | 4 ++++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 1d71d1cd..996622c4 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -167,16 +167,22 @@ bool CryptoNight::selfTest(int algo) { cryptonight_hash_ctx(test_input, 76, output, ctx, 0); + const bool doubleHash = Options::i()->doubleHash(); + # ifndef XMRIG_NO_AEON - bool rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + bool rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output_v0_lite : test_output_v0, (doubleHash ? 64 : 32)) == 0; # else - bool rc = memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + bool rc = memcmp(output, test_output_v0, (doubleHash ? 64 : 32)) == 0; # endif - if (rc && algo == xmrig::ALGO_CRYPTONIGHT) { + if (rc) { cryptonight_hash_ctx(test_input, 76, output, ctx, 1); - rc = memcmp(output, test_output2, (Options::i()->doubleHash() ? 64 : 32)) == 0; +# ifndef XMRIG_NO_AEON + rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output_v1_lite : test_output_v1, (doubleHash ? 64 : 32)) == 0; +# else + rc = memcmp(output, test_output_v1, (doubleHash ? 64 : 32)) == 0; +# endif } _mm_free(ctx->memory); diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 65200f75..0f7feda1 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -40,7 +40,7 @@ const static uint8_t test_input[152] = { }; -const static uint8_t test_output0[64] = { +const static uint8_t test_output_v0[64] = { 0x1B, 0x60, 0x6A, 0x3F, 0x4A, 0x07, 0xD6, 0x48, 0x9A, 0x1B, 0xCD, 0x07, 0x69, 0x7B, 0xD1, 0x66, 0x96, 0xB6, 0x1C, 0x8A, 0xE9, 0x82, 0xF6, 0x1A, 0x90, 0x16, 0x0F, 0x4E, 0x52, 0x82, 0x8A, 0x7F, 0x1A, 0x3F, 0xFB, 0xEE, 0x90, 0x9B, 0x42, 0x0D, 0x91, 0xF7, 0xBE, 0x6E, 0x5F, 0xB5, 0x6D, 0xB7, @@ -48,18 +48,8 @@ const static uint8_t test_output0[64] = { }; -#ifndef XMRIG_NO_AEON -const static uint8_t test_output1[64] = { - 0x28, 0xA2, 0x2B, 0xAD, 0x3F, 0x93, 0xD1, 0x40, 0x8F, 0xCA, 0x47, 0x2E, 0xB5, 0xAD, 0x1C, 0xBE, - 0x75, 0xF2, 0x1D, 0x05, 0x3C, 0x8C, 0xE5, 0xB3, 0xAF, 0x10, 0x5A, 0x57, 0x71, 0x3E, 0x21, 0xDD, - 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, - 0x00, 0x4E, 0xEC, 0xE0, 0x9B, 0x83, 0xA7, 0x2E, 0xF6, 0xBA, 0x98, 0x64, 0xD3, 0x51, 0x0C, 0x88, -}; -#endif - - // Monero v7 -const static uint8_t test_output2[64] = { +const static uint8_t test_output_v1[64] = { 0xC9, 0xFA, 0xE8, 0x42, 0x5D, 0x86, 0x88, 0xDC, 0x23, 0x6B, 0xCD, 0xBC, 0x42, 0xFD, 0xB4, 0x2D, 0x37, 0x6C, 0x6E, 0xC1, 0x90, 0x50, 0x1A, 0xA8, 0x4B, 0x04, 0xA4, 0xB4, 0xCF, 0x1E, 0xE1, 0x22, 0xF2, 0x2D, 0x3D, 0x62, 0x03, 0xD2, 0xA0, 0x8B, 0x41, 0xD9, 0x02, 0x72, 0x78, 0xD8, 0xBC, 0xC9, @@ -67,4 +57,23 @@ const static uint8_t test_output2[64] = { }; +#ifndef XMRIG_NO_AEON +const static uint8_t test_output_v0_lite[64] = { + 0x28, 0xA2, 0x2B, 0xAD, 0x3F, 0x93, 0xD1, 0x40, 0x8F, 0xCA, 0x47, 0x2E, 0xB5, 0xAD, 0x1C, 0xBE, + 0x75, 0xF2, 0x1D, 0x05, 0x3C, 0x8C, 0xE5, 0xB3, 0xAF, 0x10, 0x5A, 0x57, 0x71, 0x3E, 0x21, 0xDD, + 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, + 0x00, 0x4E, 0xEC, 0xE0, 0x9B, 0x83, 0xA7, 0x2E, 0xF6, 0xBA, 0x98, 0x64, 0xD3, 0x51, 0x0C, 0x88 +}; + + +// AEON v2 +const static uint8_t test_output_v1_lite[64] = { + 0x87, 0xC4, 0xE5, 0x70, 0x65, 0x3E, 0xB4, 0xC2, 0xB4, 0x2B, 0x7A, 0x0D, 0x54, 0x65, 0x59, 0x45, + 0x2D, 0xFA, 0xB5, 0x73, 0xB8, 0x2E, 0xC5, 0x2F, 0x15, 0x2B, 0x7F, 0xF9, 0x8E, 0x79, 0x44, 0x6F, + 0x6D, 0x8C, 0xDC, 0x44, 0x4E, 0x9B, 0xBB, 0xFD, 0x68, 0xFC, 0x43, 0xFC, 0xD4, 0x85, 0x5B, 0x22, + 0x8C, 0x8A, 0x1B, 0xD9, 0x1D, 0x9D, 0x00, 0x28, 0x5B, 0xEC, 0x02, 0xB7, 0xCA, 0x2D, 0x67, 0x41 +}; +#endif + + #endif /* __CRYPTONIGHT_TEST_H__ */ diff --git a/src/net/Job.cpp b/src/net/Job.cpp index c8331fb9..b9d7372c 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -173,11 +173,15 @@ int Job::variant() const const uint8_t version = m_blob[0]; +# if !defined(XMRIG_NO_AEON) if (m_algo == xmrig::ALGO_CRYPTONIGHT) { return version > 6 ? 1 : 0; } return version > 1 ? 1 : 0; +# else + return version > 6 ? 1 : 0; +# endif } From 07f0a107d3be11bbd96cd21abfcd80e99d4e12c2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 02:17:19 +0700 Subject: [PATCH 109/389] Change port for AEON donate. --- src/net/strategies/DonateStrategy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 22cffd28..84d1e4da 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -49,7 +49,7 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : keccak(reinterpret_cast(user), static_cast(strlen(user)), hash, sizeof(hash)); Job::toHex(hash, 32, userId); - Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == xmrig::ALGO_CRYPTONIGHT_LITE ? 3333 : 80, userId, nullptr, false, true); + Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == xmrig::ALGO_CRYPTONIGHT_LITE ? 5555 : 80, userId, nullptr, false, true); m_client = new Client(-1, agent, this); m_client->setUrl(url); From 11f00a2e6e017b9330fb4e7df55d8bffd94ec53f Mon Sep 17 00:00:00 2001 From: kpcyrd Date: Mon, 12 Mar 2018 21:03:04 +0100 Subject: [PATCH 110/389] Add -DBUILD_STATIC=ON for static builds See #238 --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e1673ea..ea02d373 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(xmrig) option(WITH_LIBCPUID "Use Libcpuid" ON) option(WITH_AEON "CryptoNight-Lite support" ON) option(WITH_HTTPD "HTTP REST API" ON) +option(BUILD_STATIC "Build static binary" OFF) include (CheckIncludeFile) include (cmake/cpu.cmake) @@ -204,5 +205,9 @@ include_directories(src) include_directories(src/3rdparty) include_directories(${UV_INCLUDE_DIR}) +if (BUILD_STATIC) + set(CMAKE_EXE_LINKER_FLAGS " -static") +endif() + add_executable(xmrig ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES}) target_link_libraries(xmrig ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) From a01b4d05668572d41be3bf68b843b5b7d251fe8a Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 17:50:09 +0700 Subject: [PATCH 111/389] Simplify variant selection. --- src/net/Job.cpp | 21 --------------------- src/net/Job.h | 3 ++- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/src/net/Job.cpp b/src/net/Job.cpp index b9d7372c..8b1966aa 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -27,7 +27,6 @@ #include "net/Job.h" -#include "xmrig.h" static inline unsigned char hf_hex2bin(char c, bool &err) @@ -165,26 +164,6 @@ bool Job::setTarget(const char *target) } -int Job::variant() const -{ - if (m_variant != xmrig::VARIANT_AUTO) { - return m_variant; - } - - const uint8_t version = m_blob[0]; - -# if !defined(XMRIG_NO_AEON) - if (m_algo == xmrig::ALGO_CRYPTONIGHT) { - return version > 6 ? 1 : 0; - } - - return version > 1 ? 1 : 0; -# else - return version > 6 ? 1 : 0; -# endif -} - - void Job::setCoin(const char *coin) { if (!coin || strlen(coin) > 4) { diff --git a/src/net/Job.h b/src/net/Job.h index 28d5e111..545eaa88 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -32,6 +32,7 @@ #include "align.h" #include "net/Id.h" +#include "xmrig.h" class Job @@ -43,7 +44,6 @@ public: bool setBlob(const char *blob); bool setTarget(const char *target); - int variant() const; void setCoin(const char *coin); void setVariant(int variant); @@ -56,6 +56,7 @@ public: inline const xmrig::Id &id() const { return m_id; } inline int poolId() const { return m_poolId; } inline int threadId() const { return m_threadId; } + inline int variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? 1 : 0) : m_variant); } inline size_t size() const { return m_size; } inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } inline uint32_t diff() const { return (uint32_t) m_diff; } From 41abe17286d66c812f12f48907d9250319beea5d Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 20:59:35 +0700 Subject: [PATCH 112/389] #438 Fixed memory release. --- src/Mem.cpp | 1 + src/Mem.h | 1 + src/Mem_unix.cpp | 22 ++++++++++------------ src/Mem_win.cpp | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Mem.cpp b/src/Mem.cpp index 73576d20..9c608f73 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -37,6 +37,7 @@ int Mem::m_algo = 0; int Mem::m_flags = 0; int Mem::m_threads = 0; size_t Mem::m_offset = 0; +size_t Mem::m_size = 0; uint8_t *Mem::m_memory = nullptr; diff --git a/src/Mem.h b/src/Mem.h index a9635a17..eaa4f49a 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -62,6 +62,7 @@ private: static int m_flags; static int m_threads; static size_t m_offset; + static size_t m_size; VAR_ALIGN(16, static uint8_t *m_memory); # ifndef XMRIG_NO_AEON diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 93cdbec0..f09b5096 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -47,8 +47,8 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_threads = threads; m_doubleHash = doubleHash; - const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MONERO_MEMORY * (threads * ratio + 1); + const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; + m_size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { m_memory = static_cast(_mm_malloc(size, 16)); @@ -58,24 +58,24 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_flags |= HugepagesAvailable; # if defined(__APPLE__) - m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); + m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); # elif defined(__FreeBSD__) - m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); + m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); # else - m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); + m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); # endif if (m_memory == MAP_FAILED) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); return true; } m_flags |= HugepagesEnabled; - if (madvise(m_memory, size, MADV_RANDOM | MADV_WILLNEED) != 0) { + if (madvise(m_memory, m_size, MADV_RANDOM | MADV_WILLNEED) != 0) { LOG_ERR("madvise failed"); } - if (mlock(m_memory, size) == 0) { + if (mlock(m_memory, m_size) == 0) { m_flags |= Lock; } @@ -85,14 +85,12 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) void Mem::release() { - const int size = MONERO_MEMORY * (m_threads + 1); - if (m_flags & HugepagesEnabled) { if (m_flags & Lock) { - munlock(m_memory, size); + munlock(m_memory, m_size); } - munmap(m_memory, size); + munmap(m_memory, m_size); } else { _mm_free(m_memory); diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 28eaff49..239bda7d 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -153,10 +153,10 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_doubleHash = doubleHash; const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MONERO_MEMORY * (threads * ratio + 1); + m_size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); return true; } @@ -164,9 +164,9 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_flags |= HugepagesAvailable; } - m_memory = static_cast(VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); + m_memory = static_cast(VirtualAlloc(NULL, m_size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); if (!m_memory) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); } else { m_flags |= HugepagesEnabled; From 81ef40fbb778b16cb8069b497314e9883f987ac7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 21:01:00 +0700 Subject: [PATCH 113/389] Fix for previous commit. --- src/Mem_unix.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index f09b5096..3e699544 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -51,7 +51,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); return true; } From c0ca68e2dbbcbd5170e98569af18b012536a3343 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 14 Mar 2018 06:32:43 +0700 Subject: [PATCH 114/389] Fix FindUV.cmake and FindMHD.cmake. --- cmake/FindMHD.cmake | 4 ++++ cmake/FindUV.cmake | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/cmake/FindMHD.cmake b/cmake/FindMHD.cmake index a20ecfec..b1e4e895 100644 --- a/cmake/FindMHD.cmake +++ b/cmake/FindMHD.cmake @@ -13,6 +13,8 @@ find_path( DOC "microhttpd include dir" ) +find_path(MHD_INCLUDE_DIR NAMES microhttpd.h) + find_library( MHD_LIBRARY NAMES microhttpd libmicrohttpd @@ -21,6 +23,8 @@ find_library( DOC "microhttpd library" ) +find_library(MHD_LIBRARY NAMES microhttpd libmicrohttpd) + set(MHD_INCLUDE_DIRS ${MHD_INCLUDE_DIR}) set(MHD_LIBRARIES ${MHD_LIBRARY}) diff --git a/cmake/FindUV.cmake b/cmake/FindUV.cmake index 7906633f..ba59d1d3 100644 --- a/cmake/FindUV.cmake +++ b/cmake/FindUV.cmake @@ -3,15 +3,21 @@ find_path( NAMES uv.h PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" PATH_SUFFIXES "include" + NO_DEFAULT_PATH ) +find_path(UV_INCLUDE_DIR NAMES uv.h) + find_library( UV_LIBRARY NAMES libuv.a uv libuv PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" PATH_SUFFIXES "lib" + NO_DEFAULT_PATH ) +find_library(UV_LIBRARY NAMES libuv.a uv libuv) + set(UV_LIBRARIES ${UV_LIBRARY}) set(UV_INCLUDE_DIRS ${UV_INCLUDE_DIR}) From f9a3315d7570c5121efadc3c1fbcf54e329bec49 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 14 Mar 2018 16:58:39 +0700 Subject: [PATCH 115/389] Fixes for 32 bit gcc builds. --- src/net/Id.h | 4 ++-- src/net/Job.cpp | 20 ++++++-------------- src/net/Job.h | 14 +------------- 3 files changed, 9 insertions(+), 29 deletions(-) diff --git a/src/net/Id.h b/src/net/Id.h index 5c77d1d4..5fb2db52 100644 --- a/src/net/Id.h +++ b/src/net/Id.h @@ -34,9 +34,9 @@ namespace xmrig { class Id { public: - inline Id() + inline Id() : + m_data() { - memset(m_data, 0, sizeof(m_data)); } diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 8b1966aa..48e73a52 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -58,29 +58,31 @@ static inline char hf_bin2hex(unsigned char c) Job::Job() : m_nicehash(false), + m_coin(), m_algo(xmrig::ALGO_CRYPTONIGHT), m_poolId(-2), m_threadId(-1), m_variant(xmrig::VARIANT_AUTO), m_size(0), m_diff(0), - m_target(0) + m_target(0), + m_blob() { - memset(m_coin, 0, sizeof(m_coin)); } Job::Job(int poolId, bool nicehash, int algo, int variant) : m_nicehash(nicehash), + m_coin(), m_algo(algo), m_poolId(poolId), m_threadId(-1), m_variant(variant), m_size(0), m_diff(0), - m_target(0) + m_target(0), + m_blob() { - memset(m_coin, 0, sizeof(m_coin)); } @@ -113,11 +115,6 @@ bool Job::setBlob(const char *blob) m_nicehash = true; } -# ifdef XMRIG_PROXY_PROJECT - memset(m_rawBlob, 0, sizeof(m_rawBlob)); - memcpy(m_rawBlob, blob, m_size * 2); -# endif - return true; } @@ -154,11 +151,6 @@ bool Job::setTarget(const char *target) return false; } -# ifdef XMRIG_PROXY_PROJECT - memset(m_rawTarget, 0, sizeof(m_rawTarget)); - memcpy(m_rawTarget, target, len); -# endif - m_diff = toDiff(m_target); return true; } diff --git a/src/net/Job.h b/src/net/Job.h index 545eaa88..d981ed37 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -30,7 +30,6 @@ #include -#include "align.h" #include "net/Id.h" #include "xmrig.h" @@ -64,11 +63,6 @@ public: inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setThreadId(int threadId) { m_threadId = threadId; } -# ifdef XMRIG_PROXY_PROJECT - inline char *rawBlob() { return m_rawBlob; } - inline const char *rawTarget() const { return m_rawTarget; } -# endif - static bool fromHex(const char* in, unsigned int len, unsigned char* out); static inline uint32_t *nonce(uint8_t *blob) { return reinterpret_cast(blob + 39); } static inline uint64_t toDiff(uint64_t target) { return 0xFFFFFFFFFFFFFFFFULL / target; } @@ -77,8 +71,6 @@ public: bool operator==(const Job &other) const; private: - VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. - bool m_nicehash; char m_coin[5]; int m_algo; @@ -88,12 +80,8 @@ private: size_t m_size; uint64_t m_diff; uint64_t m_target; + uint8_t m_blob[96]; // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. xmrig::Id m_id; - -# ifdef XMRIG_PROXY_PROJECT - VAR_ALIGN(16, char m_rawBlob[169]); - VAR_ALIGN(16, char m_rawTarget[17]); -# endif }; #endif /* __JOB_H__ */ From 7adf30e326ce076c64e0aff511bf6e583ceefda9 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 14 Mar 2018 17:11:51 +0700 Subject: [PATCH 116/389] Remove align.h. --- src/3rdparty/align.h | 33 --------------------------------- src/Mem.h | 5 +---- src/crypto/CryptoNight.h | 8 +++----- src/workers/DoubleWorker.h | 1 - 4 files changed, 4 insertions(+), 43 deletions(-) delete mode 100644 src/3rdparty/align.h diff --git a/src/3rdparty/align.h b/src/3rdparty/align.h deleted file mode 100644 index b61179b9..00000000 --- a/src/3rdparty/align.h +++ /dev/null @@ -1,33 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef __ALIGN_H__ -#define __ALIGN_H__ - -#ifdef _MSC_VER -# define VAR_ALIGN(x, decl) __declspec(align(x)) decl -#else -# define VAR_ALIGN(x, decl) decl __attribute__ ((aligned(x))) -#endif - -#endif /* __ALIGN_H__ */ diff --git a/src/Mem.h b/src/Mem.h index eaa4f49a..18914d68 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -30,9 +30,6 @@ #include -#include "align.h" - - struct cryptonight_ctx; @@ -63,7 +60,7 @@ private: static int m_threads; static size_t m_offset; static size_t m_size; - VAR_ALIGN(16, static uint8_t *m_memory); + alignas(16) static uint8_t *m_memory; # ifndef XMRIG_NO_AEON static cryptonight_ctx *createLite(int threadId); diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 9526309d..13e9c8e8 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -30,8 +30,6 @@ #include -#include "align.h" - #define AEON_MEMORY 1048576 #define AEON_MASK 0xFFFF0 #define AEON_ITER 0x40000 @@ -42,9 +40,9 @@ struct cryptonight_ctx { - VAR_ALIGN(16, uint8_t state0[200]); - VAR_ALIGN(16, uint8_t state1[200]); - VAR_ALIGN(16, uint8_t* memory); + alignas(16) uint8_t state0[200]; + alignas(16) uint8_t state1[200]; + alignas(16) uint8_t* memory; }; diff --git a/src/workers/DoubleWorker.h b/src/workers/DoubleWorker.h index 9f29fa5b..57be59d0 100644 --- a/src/workers/DoubleWorker.h +++ b/src/workers/DoubleWorker.h @@ -26,7 +26,6 @@ #define __DOUBLEWORKER_H__ -#include "align.h" #include "net/Job.h" #include "net/JobResult.h" #include "workers/Worker.h" From 0e60c5802d17befda45515afd483d430557fe22f Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 14 Mar 2018 17:17:36 +0700 Subject: [PATCH 117/389] Fix. --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8959a69f..8823b078 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,6 @@ include (cmake/cpu.cmake) set(HEADERS - src/3rdparty/align.h src/api/Api.h src/api/ApiState.h src/api/NetworkState.h From 2c2b3d2f36e4b1ab4ada3e25b6eca8163e7fb900 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 14 Mar 2018 19:58:38 +0700 Subject: [PATCH 118/389] Fix FindMHD.cmake --- cmake/FindMHD.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmake/FindMHD.cmake b/cmake/FindMHD.cmake index b1e4e895..7a598e02 100644 --- a/cmake/FindMHD.cmake +++ b/cmake/FindMHD.cmake @@ -11,16 +11,18 @@ find_path( PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" PATH_SUFFIXES "include" DOC "microhttpd include dir" + NO_DEFAULT_PATH ) find_path(MHD_INCLUDE_DIR NAMES microhttpd.h) find_library( MHD_LIBRARY - NAMES microhttpd libmicrohttpd + NAMES libmicrohttpd.a microhttpd libmicrohttpd PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" PATH_SUFFIXES "lib" DOC "microhttpd library" + NO_DEFAULT_PATH ) find_library(MHD_LIBRARY NAMES microhttpd libmicrohttpd) From 04c02d4add9dcb2c2141f3e728c8665ef6184131 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 14 Mar 2018 20:22:30 +0700 Subject: [PATCH 119/389] Fix macOS compile. --- src/Mem.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Mem.cpp b/src/Mem.cpp index 9c608f73..b32d2196 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -32,13 +32,13 @@ #include "xmrig.h" -bool Mem::m_doubleHash = false; -int Mem::m_algo = 0; -int Mem::m_flags = 0; -int Mem::m_threads = 0; -size_t Mem::m_offset = 0; -size_t Mem::m_size = 0; -uint8_t *Mem::m_memory = nullptr; +bool Mem::m_doubleHash = false; +int Mem::m_algo = 0; +int Mem::m_flags = 0; +int Mem::m_threads = 0; +size_t Mem::m_offset = 0; +size_t Mem::m_size = 0; +alignas(16) uint8_t *Mem::m_memory = nullptr; cryptonight_ctx *Mem::create(int threadId) From d48921bfc8a9f7872c599d629c4c71a1809f9888 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 14 Mar 2018 20:33:17 +0700 Subject: [PATCH 120/389] v2.5.0 --- src/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index e0d4511a..cfaf7e9c 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.5.0-dev" +#define APP_VERSION "2.5.0" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" From 7959cf39fabee36d83683d25d7cd55cead3e3230 Mon Sep 17 00:00:00 2001 From: xmrig Date: Wed, 14 Mar 2018 23:43:57 +0700 Subject: [PATCH 121/389] Update README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index b563d5b0..0330f83f 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Originally based on cpuminer-multi with heavy optimizations/rewrites and removin * [Common Issues](#common-issues) * [Other information](#other-information) * [Donations](#donations) +* [Release checksums](#release-checksums) * [Contacts](#contacts) ## Features @@ -115,6 +116,15 @@ Please note performance is highly dependent on system load. The numbers above ar * XMR: `48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD` * BTC: `1P7ujsXeX7GxQwHNnJsRMgAdNkFZmNVqJT` +## Release checksums +### SHA-256 +``` +232ea929f7219c8de81c7e6fcae7437d40d8128cf55b597a6e1fca0cd77f9f5b xmrig-2.5.0-xenial-amd64.tar.gz/xmrig-2.5.0/xmrig +6149ffed21e740cac12aa61b2fdd17248cbd3e51bab2289d2766aad1d29df910 xmrig-2.5.0-gcc-win32.zip/xmrig.exe +9c89f80e21db906439a7a1b333b8215dbe07d8e42f98a63f6c10c954288a7108 xmrig-2.5.0-gcc-win64.zip/xmrig.exe +b50773c5a74ca9921597a1152e2469ec266cc89eb9765038db7e876f0bcece73 xmrig-2.5.0-msvc-win64.zip/xmrig.exe +``` + ## Contacts * support@xmrig.com * [reddit](https://www.reddit.com/user/XMRig/) From 3d41629170afcfe26d08d4c1b8ce97e38285fea6 Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 15 Mar 2018 01:32:09 +0700 Subject: [PATCH 122/389] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 0330f83f..acaf178f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ # XMRig + +:warning: **You must update miners to version 2.5 before March 28 due [Monero PoW change](https://getmonero.org/2018/02/11/PoW-change-and-key-reuse.html).** + [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub release](https://img.shields.io/github/release/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub Release Date](https://img.shields.io/github/release-date-pre/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/releases) From 7ab81ef19a5f77900b9a89efe85eb86d96399003 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 17 Mar 2018 01:07:53 +0700 Subject: [PATCH 123/389] #456 Don't show error "JSON decode failed" in quiet mode. --- src/net/Client.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index f6543b4e..cb1a53f8 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -407,7 +407,10 @@ void Client::parse(char *line, size_t len) LOG_DEBUG("[%s:%u] received (%d bytes): \"%s\"", m_url.host(), m_url.port(), len, line); if (len < 32 || line[0] != '{') { - LOG_ERR("[%s:%u] JSON decode failed", m_url.host(), m_url.port()); + if (!m_quiet) { + LOG_ERR("[%s:%u] JSON decode failed", m_url.host(), m_url.port()); + } + return; } From a2f747fb0c4b8282115302832d62b7bba5532652 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 17 Mar 2018 04:16:08 +0700 Subject: [PATCH 124/389] Pass all options to network strategies in constructor. --- src/net/Network.cpp | 6 +++--- src/net/strategies/DonateStrategy.cpp | 12 +++++------- src/net/strategies/DonateStrategy.h | 2 +- src/net/strategies/FailoverStrategy.cpp | 13 +++++++------ src/net/strategies/FailoverStrategy.h | 8 +++++--- src/net/strategies/SinglePoolStrategy.cpp | 9 ++++----- src/net/strategies/SinglePoolStrategy.h | 6 +++--- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/net/Network.cpp b/src/net/Network.cpp index e0f65497..d2cf8b1a 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -55,14 +55,14 @@ Network::Network(const Options *options) : const std::vector &pools = options->pools(); if (pools.size() > 1) { - m_strategy = new FailoverStrategy(pools, Platform::userAgent(), this); + m_strategy = new FailoverStrategy(pools, options->retryPause(), options->retries(), Platform::userAgent(), this); } else { - m_strategy = new SinglePoolStrategy(pools.front(), Platform::userAgent(), this); + m_strategy = new SinglePoolStrategy(pools.front(), options->retryPause(), Platform::userAgent(), this); } if (m_options->donateLevel() > 0) { - m_donate = new DonateStrategy(Platform::userAgent(), this); + m_donate = new DonateStrategy(options->donateLevel(), options->pools().front()->user(), options->algo(), Platform::userAgent(), this); } m_timer.data = this; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 84d1e4da..aaab222b 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -26,7 +26,6 @@ #include "net/Client.h" #include "net/Job.h" #include "net/strategies/DonateStrategy.h" -#include "Options.h" #include "xmrig.h" @@ -36,24 +35,23 @@ extern "C" } -DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : +DonateStrategy::DonateStrategy(int level, const char *user, int algo, const char *agent, IStrategyListener *listener) : m_active(false), - m_donateTime(Options::i()->donateLevel() * 60 * 1000), - m_idleTime((100 - Options::i()->donateLevel()) * 60 * 1000), + m_donateTime(level * 60 * 1000), + m_idleTime((100 - level) * 60 * 1000), m_listener(listener) { uint8_t hash[200]; char userId[65] = { 0 }; - const char *user = Options::i()->pools().front()->user(); keccak(reinterpret_cast(user), static_cast(strlen(user)), hash, sizeof(hash)); Job::toHex(hash, 32, userId); - Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == xmrig::ALGO_CRYPTONIGHT_LITE ? 5555 : 80, userId, nullptr, false, true); + Url *url = new Url("thanks.xmrig.com", algo == xmrig::ALGO_CRYPTONIGHT_LITE ? 5555 : 80, userId, nullptr, false, true); m_client = new Client(-1, agent, this); m_client->setUrl(url); - m_client->setRetryPause(Options::i()->retryPause() * 1000); + m_client->setRetryPause(1000); m_client->setQuiet(true); delete url; diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 07011053..52e3a38d 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -40,7 +40,7 @@ class Url; class DonateStrategy : public IStrategy, public IClientListener { public: - DonateStrategy(const char *agent, IStrategyListener *listener); + DonateStrategy(int level, const char *user, int algo, const char *agent, IStrategyListener *listener); public: inline bool isActive() const override { return m_active; } diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index 47d390b0..db449ccc 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,10 +25,11 @@ #include "interfaces/IStrategyListener.h" #include "net/Client.h" #include "net/strategies/FailoverStrategy.h" -#include "Options.h" -FailoverStrategy::FailoverStrategy(const std::vector &urls, const char *agent, IStrategyListener *listener) : +FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, const char *agent, IStrategyListener *listener) : + m_retries(retries), + m_retryPause(retryPause), m_active(-1), m_index(0), m_listener(listener) @@ -93,7 +94,7 @@ void FailoverStrategy::onClose(Client *client, int failures) m_listener->onPause(this); } - if (m_index == 0 && failures < Options::i()->retries()) { + if (m_index == 0 && failures < m_retries) { return; } @@ -142,7 +143,7 @@ void FailoverStrategy::add(const Url *url, const char *agent) { Client *client = new Client((int) m_pools.size(), agent, this); client->setUrl(url); - client->setRetryPause(Options::i()->retryPause() * 1000); + client->setRetryPause(m_retryPause * 1000); m_pools.push_back(client); } diff --git a/src/net/strategies/FailoverStrategy.h b/src/net/strategies/FailoverStrategy.h index 963d3157..e29c7d62 100644 --- a/src/net/strategies/FailoverStrategy.h +++ b/src/net/strategies/FailoverStrategy.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,7 +40,7 @@ class Url; class FailoverStrategy : public IStrategy, public IClientListener { public: - FailoverStrategy(const std::vector &urls, const char *agent, IStrategyListener *listener); + FailoverStrategy(const std::vector &urls, int retryPause, int retries, const char *agent, IStrategyListener *listener); public: inline bool isActive() const override { return m_active >= 0; } @@ -60,6 +60,8 @@ protected: private: void add(const Url *url, const char *agent); + const int m_retries; + const int m_retryPause; int m_active; int m_index; IStrategyListener *m_listener; diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index 997dc00b..2671ba72 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,16 +25,15 @@ #include "interfaces/IStrategyListener.h" #include "net/Client.h" #include "net/strategies/SinglePoolStrategy.h" -#include "Options.h" -SinglePoolStrategy::SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener) : +SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, const char *agent, IStrategyListener *listener) : m_active(false), m_listener(listener) { m_client = new Client(0, agent, this); m_client->setUrl(url); - m_client->setRetryPause(Options::i()->retryPause() * 1000); + m_client->setRetryPause(retryPause * 1000); } diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h index 95e21547..d444772e 100644 --- a/src/net/strategies/SinglePoolStrategy.h +++ b/src/net/strategies/SinglePoolStrategy.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,7 +37,7 @@ class Url; class SinglePoolStrategy : public IStrategy, public IClientListener { public: - SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener); + SinglePoolStrategy(const Url *url, int retryPause, const char *agent, IStrategyListener *listener); public: inline bool isActive() const override { return m_active; } From c46c019c8399e0c3f4d2d77324589034dc40dcfc Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 17 Mar 2018 05:03:14 +0700 Subject: [PATCH 125/389] Unify network strategies with upcoming proxy. --- src/interfaces/IStrategy.h | 5 ++- src/interfaces/IStrategyListener.h | 12 +++--- src/net/Network.cpp | 17 ++++---- src/net/Network.h | 10 ++--- src/net/strategies/DonateStrategy.cpp | 16 +++++--- src/net/strategies/DonateStrategy.h | 3 +- src/net/strategies/FailoverStrategy.cpp | 47 +++++++++++++++++++---- src/net/strategies/FailoverStrategy.h | 8 +++- src/net/strategies/SinglePoolStrategy.cpp | 32 ++++++++++++--- src/net/strategies/SinglePoolStrategy.h | 5 ++- 10 files changed, 110 insertions(+), 45 deletions(-) diff --git a/src/interfaces/IStrategy.h b/src/interfaces/IStrategy.h index 660529ea..6e915401 100644 --- a/src/interfaces/IStrategy.h +++ b/src/interfaces/IStrategy.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,6 +39,7 @@ public: virtual bool isActive() const = 0; virtual int64_t submit(const JobResult &result) = 0; virtual void connect() = 0; + virtual void release() = 0; virtual void resume() = 0; virtual void stop() = 0; virtual void tick(uint64_t now) = 0; diff --git a/src/interfaces/IStrategyListener.h b/src/interfaces/IStrategyListener.h index 60f95734..9f2c4487 100644 --- a/src/interfaces/IStrategyListener.h +++ b/src/interfaces/IStrategyListener.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,10 +39,10 @@ class IStrategyListener public: virtual ~IStrategyListener() {} - virtual void onActive(Client *client) = 0; - virtual void onJob(Client *client, const Job &job) = 0; - virtual void onPause(IStrategy *strategy) = 0; - virtual void onResultAccepted(Client *client, const SubmitResult &result, const char *error) = 0; + virtual void onActive(IStrategy *strategy, Client *client) = 0; + virtual void onJob(IStrategy *strategy, Client *client, const Job &job) = 0; + virtual void onPause(IStrategy *strategy) = 0; + virtual void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) = 0; }; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index d2cf8b1a..978bbf27 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,7 +40,6 @@ #include "net/SubmitResult.h" #include "net/Url.h" #include "Options.h" -#include "Platform.h" #include "workers/Workers.h" @@ -55,14 +54,14 @@ Network::Network(const Options *options) : const std::vector &pools = options->pools(); if (pools.size() > 1) { - m_strategy = new FailoverStrategy(pools, options->retryPause(), options->retries(), Platform::userAgent(), this); + m_strategy = new FailoverStrategy(pools, options->retryPause(), options->retries(), this); } else { - m_strategy = new SinglePoolStrategy(pools.front(), options->retryPause(), Platform::userAgent(), this); + m_strategy = new SinglePoolStrategy(pools.front(), options->retryPause(), this); } if (m_options->donateLevel() > 0) { - m_donate = new DonateStrategy(options->donateLevel(), options->pools().front()->user(), options->algo(), Platform::userAgent(), this); + m_donate = new DonateStrategy(options->donateLevel(), options->pools().front()->user(), options->algo(), this); } m_timer.data = this; @@ -93,7 +92,7 @@ void Network::stop() } -void Network::onActive(Client *client) +void Network::onActive(IStrategy *strategy, Client *client) { if (client->id() == -1) { LOG_NOTICE("dev donate started"); @@ -106,7 +105,7 @@ void Network::onActive(Client *client) } -void Network::onJob(Client *client, const Job &job) +void Network::onJob(IStrategy *strategy, Client *client, const Job &job) { if (m_donate && m_donate->isActive() && client->id() != -1) { return; @@ -142,7 +141,7 @@ void Network::onPause(IStrategy *strategy) } -void Network::onResultAccepted(Client *client, const SubmitResult &result, const char *error) +void Network::onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) { m_state.add(result, error); diff --git a/src/net/Network.h b/src/net/Network.h index fe13d9b7..f7eb3ff2 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -49,11 +49,11 @@ public: void stop(); protected: - void onActive(Client *client) override; - void onJob(Client *client, const Job &job) override; + void onActive(IStrategy *strategy, Client *client) override; + void onJob(IStrategy *strategy, Client *client, const Job &job) override; void onJobResult(const JobResult &result) override; void onPause(IStrategy *strategy) override; - void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; + void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override; private: constexpr static int kTickInterval = 1 * 1000; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index aaab222b..55de9bdb 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -26,6 +26,7 @@ #include "net/Client.h" #include "net/Job.h" #include "net/strategies/DonateStrategy.h" +#include "Platform.h" #include "xmrig.h" @@ -35,7 +36,7 @@ extern "C" } -DonateStrategy::DonateStrategy(int level, const char *user, int algo, const char *agent, IStrategyListener *listener) : +DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) : m_active(false), m_donateTime(level * 60 * 1000), m_idleTime((100 - level) * 60 * 1000), @@ -49,7 +50,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, const char Url *url = new Url("thanks.xmrig.com", algo == xmrig::ALGO_CRYPTONIGHT_LITE ? 5555 : 80, userId, nullptr, false, true); - m_client = new Client(-1, agent, this); + m_client = new Client(-1, Platform::userAgent(), this); m_client->setUrl(url); m_client->setRetryPause(1000); m_client->setQuiet(true); @@ -75,6 +76,11 @@ void DonateStrategy::connect() } +void DonateStrategy::release() +{ +} + + void DonateStrategy::stop() { uv_timer_stop(&m_timer); @@ -95,7 +101,7 @@ void DonateStrategy::onClose(Client *client, int failures) void DonateStrategy::onJobReceived(Client *client, const Job &job) { - m_listener->onJob(client, job); + m_listener->onJob(this, client, job); } @@ -106,13 +112,13 @@ void DonateStrategy::onLoginSuccess(Client *client) } m_active = true; - m_listener->onActive(client); + m_listener->onActive(this, client); } void DonateStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) { - m_listener->onResultAccepted(client, result, error); + m_listener->onResultAccepted(this, client, result, error); } diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 52e3a38d..f805bc78 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -40,7 +40,7 @@ class Url; class DonateStrategy : public IStrategy, public IClientListener { public: - DonateStrategy(int level, const char *user, int algo, const char *agent, IStrategyListener *listener); + DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener); public: inline bool isActive() const override { return m_active; } @@ -48,6 +48,7 @@ public: int64_t submit(const JobResult &result) override; void connect() override; + void release() override; void stop() override; void tick(uint64_t now) override; diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index db449ccc..5ead16c1 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -25,17 +25,28 @@ #include "interfaces/IStrategyListener.h" #include "net/Client.h" #include "net/strategies/FailoverStrategy.h" +#include "Platform.h" -FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, const char *agent, IStrategyListener *listener) : +FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener) : + m_release(false), m_retries(retries), m_retryPause(retryPause), m_active(-1), m_index(0), + m_remaining(0), m_listener(listener) { for (const Url *url : urls) { - add(url, agent); + add(url); + } +} + + +FailoverStrategy::~FailoverStrategy() +{ + for (Client *client : m_pools) { + delete client; } } @@ -52,13 +63,25 @@ void FailoverStrategy::connect() } +void FailoverStrategy::release() +{ + m_release = true; + + for (size_t i = 0; i < m_pools.size(); ++i) { + if (m_pools[i]->disconnect()) { + m_remaining++; + } + } +} + + void FailoverStrategy::resume() { if (!isActive()) { return; } - m_listener->onJob( m_pools[m_active], m_pools[m_active]->job()); + m_listener->onJob(this, m_pools[m_active], m_pools[m_active]->job()); } @@ -86,6 +109,14 @@ void FailoverStrategy::tick(uint64_t now) void FailoverStrategy::onClose(Client *client, int failures) { if (failures == -1) { + if (m_release) { + m_remaining--; + + if (m_remaining == 0) { + delete this; + } + } + return; } @@ -107,7 +138,7 @@ void FailoverStrategy::onClose(Client *client, int failures) void FailoverStrategy::onJobReceived(Client *client, const Job &job) { if (m_active == client->id()) { - m_listener->onJob(client, job); + m_listener->onJob(this, client, job); } } @@ -128,20 +159,20 @@ void FailoverStrategy::onLoginSuccess(Client *client) if (active >= 0 && active != m_active) { m_index = m_active = active; - m_listener->onActive(client); + m_listener->onActive(this, client); } } void FailoverStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) { - m_listener->onResultAccepted(client, result, error); + m_listener->onResultAccepted(this, client, result, error); } -void FailoverStrategy::add(const Url *url, const char *agent) +void FailoverStrategy::add(const Url *url) { - Client *client = new Client((int) m_pools.size(), agent, this); + Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this); client->setUrl(url); client->setRetryPause(m_retryPause * 1000); diff --git a/src/net/strategies/FailoverStrategy.h b/src/net/strategies/FailoverStrategy.h index e29c7d62..7b7adf72 100644 --- a/src/net/strategies/FailoverStrategy.h +++ b/src/net/strategies/FailoverStrategy.h @@ -40,13 +40,15 @@ class Url; class FailoverStrategy : public IStrategy, public IClientListener { public: - FailoverStrategy(const std::vector &urls, int retryPause, int retries, const char *agent, IStrategyListener *listener); + FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener); + ~FailoverStrategy(); public: inline bool isActive() const override { return m_active >= 0; } int64_t submit(const JobResult &result) override; void connect() override; + void release() override; void resume() override; void stop() override; void tick(uint64_t now) override; @@ -58,12 +60,14 @@ protected: void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; private: - void add(const Url *url, const char *agent); + void add(const Url *url); + bool m_release; const int m_retries; const int m_retryPause; int m_active; int m_index; + int m_remaining; IStrategyListener *m_listener; std::vector m_pools; }; diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index 2671ba72..fe37288b 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -25,18 +25,26 @@ #include "interfaces/IStrategyListener.h" #include "net/Client.h" #include "net/strategies/SinglePoolStrategy.h" +#include "Platform.h" -SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, const char *agent, IStrategyListener *listener) : +SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener) : m_active(false), + m_release(false), m_listener(listener) { - m_client = new Client(0, agent, this); + m_client = new Client(0, Platform::userAgent(), this); m_client->setUrl(url); m_client->setRetryPause(retryPause * 1000); } +SinglePoolStrategy::~SinglePoolStrategy() +{ + delete m_client; +} + + int64_t SinglePoolStrategy::submit(const JobResult &result) { return m_client->submit(result); @@ -49,13 +57,20 @@ void SinglePoolStrategy::connect() } +void SinglePoolStrategy::release() +{ + m_release = true; + m_client->disconnect(); +} + + void SinglePoolStrategy::resume() { if (!isActive()) { return; } - m_listener->onJob(m_client, m_client->job()); + m_listener->onJob(this, m_client, m_client->job()); } @@ -73,6 +88,11 @@ void SinglePoolStrategy::tick(uint64_t now) void SinglePoolStrategy::onClose(Client *client, int failures) { + if (m_release) { + delete this; + return; + } + if (!isActive()) { return; } @@ -84,18 +104,18 @@ void SinglePoolStrategy::onClose(Client *client, int failures) void SinglePoolStrategy::onJobReceived(Client *client, const Job &job) { - m_listener->onJob(client, job); + m_listener->onJob(this, client, job); } void SinglePoolStrategy::onLoginSuccess(Client *client) { m_active = true; - m_listener->onActive(client); + m_listener->onActive(this, client); } void SinglePoolStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) { - m_listener->onResultAccepted(client, result, error); + m_listener->onResultAccepted(this, client, result, error); } diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h index d444772e..a918768c 100644 --- a/src/net/strategies/SinglePoolStrategy.h +++ b/src/net/strategies/SinglePoolStrategy.h @@ -37,13 +37,15 @@ class Url; class SinglePoolStrategy : public IStrategy, public IClientListener { public: - SinglePoolStrategy(const Url *url, int retryPause, const char *agent, IStrategyListener *listener); + SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener); + ~SinglePoolStrategy(); public: inline bool isActive() const override { return m_active; } int64_t submit(const JobResult &result) override; void connect() override; + void release() override; void resume() override; void stop() override; void tick(uint64_t now) override; @@ -56,6 +58,7 @@ protected: private: bool m_active; + bool m_release; Client *m_client; IStrategyListener *m_listener; }; From 1cf1d616c680f5ed75eab8fc6e39638f86ceecd2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 17 Mar 2018 14:33:30 +0700 Subject: [PATCH 126/389] DonateStrategy now use FailoverStrategy internally and possible to use any other IStrategy. --- src/net/Job.h | 1 + src/net/Network.cpp | 10 ++-- src/net/Network.h | 2 +- src/net/strategies/DonateStrategy.cpp | 63 ++++++++++++++--------- src/net/strategies/DonateStrategy.h | 16 +++--- src/net/strategies/FailoverStrategy.cpp | 4 +- src/net/strategies/FailoverStrategy.h | 3 +- src/net/strategies/SinglePoolStrategy.cpp | 3 +- src/net/strategies/SinglePoolStrategy.h | 2 +- src/workers/Worker.cpp | 4 +- src/workers/Worker.h | 4 +- src/workers/Workers.cpp | 6 ++- src/workers/Workers.h | 2 +- 13 files changed, 73 insertions(+), 47 deletions(-) diff --git a/src/net/Job.h b/src/net/Job.h index d981ed37..0fe51aee 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -61,6 +61,7 @@ public: inline uint32_t diff() const { return (uint32_t) m_diff; } inline uint64_t target() const { return m_target; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } + inline void setPoolId(int poolId) { m_poolId = poolId; } inline void setThreadId(int threadId) { m_threadId = threadId; } static bool fromHex(const char* in, unsigned int len, unsigned char* out); diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 978bbf27..ede3f8b4 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -94,7 +94,7 @@ void Network::stop() void Network::onActive(IStrategy *strategy, Client *client) { - if (client->id() == -1) { + if (m_donate && m_donate == strategy) { LOG_NOTICE("dev donate started"); return; } @@ -107,11 +107,11 @@ void Network::onActive(IStrategy *strategy, Client *client) void Network::onJob(IStrategy *strategy, Client *client, const Job &job) { - if (m_donate && m_donate->isActive() && client->id() != -1) { + if (m_donate && m_donate->isActive() && m_donate != strategy) { return; } - setJob(client, job); + setJob(client, job, m_donate == strategy); } @@ -158,7 +158,7 @@ void Network::onResultAccepted(IStrategy *strategy, Client *client, const Submit } -void Network::setJob(Client *client, const Job &job) +void Network::setJob(Client *client, const Job &job, bool donate) { if (m_options->colors()) { LOG_INFO("\x1B[01;35mnew job\x1B[0m from \x1B[01;37m%s:%d\x1B[0m diff \x1B[01;37m%d", client->host(), client->port(), job.diff()); @@ -168,7 +168,7 @@ void Network::setJob(Client *client, const Job &job) } m_state.diff = job.diff(); - Workers::setJob(job); + Workers::setJob(job, donate); } diff --git a/src/net/Network.h b/src/net/Network.h index f7eb3ff2..fae5c563 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -58,7 +58,7 @@ protected: private: constexpr static int kTickInterval = 1 * 1000; - void setJob(Client *client, const Job &job); + void setJob(Client *client, const Job &job, bool donate); void tick(); static void onTick(uv_timer_t *handle); diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 55de9bdb..cb28e122 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -26,6 +26,7 @@ #include "net/Client.h" #include "net/Job.h" #include "net/strategies/DonateStrategy.h" +#include "net/strategies/FailoverStrategy.h" #include "Platform.h" #include "xmrig.h" @@ -36,10 +37,14 @@ extern "C" } +const static char *kDonatePool = "thanks.xmrig.com"; + + DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) : m_active(false), m_donateTime(level * 60 * 1000), m_idleTime((100 - level) * 60 * 1000), + m_strategy(nullptr), m_listener(listener) { uint8_t hash[200]; @@ -48,14 +53,17 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL keccak(reinterpret_cast(user), static_cast(strlen(user)), hash, sizeof(hash)); Job::toHex(hash, 32, userId); - Url *url = new Url("thanks.xmrig.com", algo == xmrig::ALGO_CRYPTONIGHT_LITE ? 5555 : 80, userId, nullptr, false, true); + if (algo == xmrig::ALGO_CRYPTONIGHT) { + m_pools.push_back(new Url(kDonatePool, 80, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePool, 443, userId, nullptr, false, true)); + m_pools.push_back(new Url("emergency.xmrig.com", 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); + } + else { + m_pools.push_back(new Url(kDonatePool, 3333, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePool, 5555, userId, nullptr, false, true)); + } - m_client = new Client(-1, Platform::userAgent(), this); - m_client->setUrl(url); - m_client->setRetryPause(1000); - m_client->setQuiet(true); - - delete url; + m_strategy = new FailoverStrategy(m_pools, 1, 1, this, true); m_timer.data = this; uv_timer_init(uv_default_loop(), &m_timer); @@ -64,15 +72,20 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL } +DonateStrategy::~DonateStrategy() +{ +} + + int64_t DonateStrategy::submit(const JobResult &result) { - return m_client->submit(result); + return m_strategy->submit(result); } void DonateStrategy::connect() { - m_client->connect(); + m_strategy->connect(); } @@ -84,28 +97,17 @@ void DonateStrategy::release() void DonateStrategy::stop() { uv_timer_stop(&m_timer); - m_client->disconnect(); + m_strategy->stop(); } void DonateStrategy::tick(uint64_t now) { - m_client->tick(now); + m_strategy->tick(now); } -void DonateStrategy::onClose(Client *client, int failures) -{ -} - - -void DonateStrategy::onJobReceived(Client *client, const Job &job) -{ - m_listener->onJob(this, client, job); -} - - -void DonateStrategy::onLoginSuccess(Client *client) +void DonateStrategy::onActive(IStrategy *strategy, Client *client) { if (!isActive()) { uv_timer_start(&m_timer, DonateStrategy::onTimer, m_donateTime, 0); @@ -116,7 +118,18 @@ void DonateStrategy::onLoginSuccess(Client *client) } -void DonateStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) +void DonateStrategy::onJob(IStrategy *strategy, Client *client, const Job &job) +{ + m_listener->onJob(this, client, job); +} + + +void DonateStrategy::onPause(IStrategy *strategy) +{ +} + + +void DonateStrategy::onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) { m_listener->onResultAccepted(this, client, result, error); } @@ -130,7 +143,7 @@ void DonateStrategy::idle() void DonateStrategy::suspend() { - m_client->disconnect(); + m_strategy->stop(); m_active = false; m_listener->onPause(this); diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index f805bc78..bb3b63b7 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -26,10 +26,12 @@ #include +#include #include "interfaces/IClientListener.h" #include "interfaces/IStrategy.h" +#include "interfaces/IStrategyListener.h" class Client; @@ -37,10 +39,11 @@ class IStrategyListener; class Url; -class DonateStrategy : public IStrategy, public IClientListener +class DonateStrategy : public IStrategy, public IStrategyListener { public: DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener); + ~DonateStrategy(); public: inline bool isActive() const override { return m_active; } @@ -53,10 +56,10 @@ public: void tick(uint64_t now) override; protected: - void onClose(Client *client, int failures) override; - void onJobReceived(Client *client, const Job &job) override; - void onLoginSuccess(Client *client) override; - void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; + void onActive(IStrategy *strategy, Client *client) override; + void onJob(IStrategy *strategy, Client *client, const Job &job) override; + void onPause(IStrategy *strategy) override; + void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override; private: void idle(); @@ -65,10 +68,11 @@ private: static void onTimer(uv_timer_t *handle); bool m_active; - Client *m_client; const int m_donateTime; const int m_idleTime; + IStrategy *m_strategy; IStrategyListener *m_listener; + std::vector m_pools; uv_timer_t m_timer; }; diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index 5ead16c1..52bd2fbf 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -28,8 +28,9 @@ #include "Platform.h" -FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener) : +FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : m_release(false), + m_quiet(quiet), m_retries(retries), m_retryPause(retryPause), m_active(-1), @@ -175,6 +176,7 @@ void FailoverStrategy::add(const Url *url) Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this); client->setUrl(url); client->setRetryPause(m_retryPause * 1000); + client->setQuiet(m_quiet); m_pools.push_back(client); } diff --git a/src/net/strategies/FailoverStrategy.h b/src/net/strategies/FailoverStrategy.h index 7b7adf72..c8e24fb1 100644 --- a/src/net/strategies/FailoverStrategy.h +++ b/src/net/strategies/FailoverStrategy.h @@ -40,7 +40,7 @@ class Url; class FailoverStrategy : public IStrategy, public IClientListener { public: - FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener); + FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); ~FailoverStrategy(); public: @@ -63,6 +63,7 @@ private: void add(const Url *url); bool m_release; + const bool m_quiet; const int m_retries; const int m_retryPause; int m_active; diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index fe37288b..b0d98519 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -28,7 +28,7 @@ #include "Platform.h" -SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener) : +SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet) : m_active(false), m_release(false), m_listener(listener) @@ -36,6 +36,7 @@ SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategy m_client = new Client(0, Platform::userAgent(), this); m_client->setUrl(url); m_client->setRetryPause(retryPause * 1000); + m_client->setQuiet(quiet); } diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h index a918768c..18d2192b 100644 --- a/src/net/strategies/SinglePoolStrategy.h +++ b/src/net/strategies/SinglePoolStrategy.h @@ -37,7 +37,7 @@ class Url; class SinglePoolStrategy : public IStrategy, public IClientListener { public: - SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener); + SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet = false); ~SinglePoolStrategy(); public: diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index 02646ced..7a7ff986 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/workers/Worker.h b/src/workers/Worker.h index 11c4a198..08a0551f 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 039a1793..f0aef448 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -83,10 +83,14 @@ void Workers::setEnabled(bool enabled) } -void Workers::setJob(const Job &job) +void Workers::setJob(const Job &job, bool donate) { uv_rwlock_wrlock(&m_rwlock); m_job = job; + + if (donate) { + m_job.setPoolId(-1); + } uv_rwlock_wrunlock(&m_rwlock); m_active = true; diff --git a/src/workers/Workers.h b/src/workers/Workers.h index e76d0a62..1c85089a 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -45,7 +45,7 @@ public: static Job job(); static void printHashrate(bool detail); static void setEnabled(bool enabled); - static void setJob(const Job &job); + static void setJob(const Job &job, bool donate); static void start(int64_t affinity, int priority); static void stop(); static void submit(const JobResult &result); From de5016dda82b48a755177bbf9ff6be15147c75ba Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 17 Mar 2018 16:30:41 +0700 Subject: [PATCH 127/389] #459 Fix issue with xmr.f2pool.com --- src/net/Client.cpp | 34 ++++++++++++++++++++++++---------- src/net/Client.h | 1 + src/net/Job.cpp | 6 ++++++ src/net/Job.h | 1 + 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index cb1a53f8..5d8c5919 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -65,6 +65,7 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_recvBufPos(0), m_state(UnconnectedState), m_expire(0), + m_jobs(0), m_stream(nullptr), m_socket(nullptr) { @@ -245,17 +246,22 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) job.setVariant(params["variant"].GetInt()); } - if (m_job == job) { - if (!m_quiet) { - LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); - } + if (m_job != job) { + m_jobs++; + m_job = std::move(job); + return true; + } - close(); + if (m_jobs == 0) { // https://github.com/xmrig/xmrig/issues/459 return false; } - m_job = std::move(job); - return true; + if (!m_quiet) { + LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); + } + + close(); + return false; } @@ -272,7 +278,10 @@ bool Client::parseLogin(const rapidjson::Value &result, int *code) parseExtensions(result["extensions"]); } - return parseJob(result["job"], code); + const bool rc = parseJob(result["job"], code); + m_jobs = 0; + + return rc; } @@ -683,7 +692,10 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) { auto client = getClient(req->data); if (status < 0) { - LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); + if (!client->m_quiet) { + LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); + } + return client->reconnect(); } @@ -704,7 +716,9 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) } if (ipv4.empty() && ipv6.empty()) { - LOG_ERR("[%s:%u] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", client->m_url.host(), client->m_url.port()); + if (!client->m_quiet) { + LOG_ERR("[%s:%u] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", client->m_url.host(), client->m_url.port()); + } uv_freeaddrinfo(res); return client->reconnect(); diff --git a/src/net/Client.h b/src/net/Client.h index a927bd19..ec68ab05 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -120,6 +120,7 @@ private: static int64_t m_sequence; std::map m_results; uint64_t m_expire; + uint64_t m_jobs; Url m_url; uv_buf_t m_recvBuf; uv_getaddrinfo_t m_resolver; diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 48e73a52..7d137fac 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -210,3 +210,9 @@ bool Job::operator==(const Job &other) const { return m_id == other.m_id && memcmp(m_blob, other.m_blob, sizeof(m_blob)) == 0; } + + +bool Job::operator!=(const Job &other) const +{ + return m_id != other.m_id || memcmp(m_blob, other.m_blob, sizeof(m_blob)) != 0; +} diff --git a/src/net/Job.h b/src/net/Job.h index d981ed37..4f9c472e 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -69,6 +69,7 @@ public: static void toHex(const unsigned char* in, unsigned int len, char* out); bool operator==(const Job &other) const; + bool operator!=(const Job &other) const; private: bool m_nicehash; From a7cf34d2eb341e4f37b7de8a3e320a06b2fc1d4e Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 17 Mar 2018 16:38:32 +0700 Subject: [PATCH 128/389] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b00b1f1d..ccd3fe6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# v2.5.1 +- [#459](https://github.com/xmrig/xmrig/issues/459) Fixed regression (version 2.5.0 affected) with connection to **xmr.f2pool.com**. + # v2.5.0 - [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on March 28.** - Added full IPv6 support. From ed5bf3420c2f9d7580c4ddc380aef43e1c13dc07 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 17 Mar 2018 17:55:11 +0700 Subject: [PATCH 129/389] Add donate pool IP address to avoid DNS issues. --- src/net/strategies/DonateStrategy.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index cb28e122..8100afc0 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -37,7 +37,8 @@ extern "C" } -const static char *kDonatePool = "thanks.xmrig.com"; +const static char *kDonatePool = "thanks.xmrig.com"; +const static char *kDonatePoolIP = "45.76.34.221"; DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) : @@ -54,13 +55,15 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL Job::toHex(hash, 32, userId); if (algo == xmrig::ALGO_CRYPTONIGHT) { - m_pools.push_back(new Url(kDonatePool, 80, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool, 443, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePool, 80, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePool, 443, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePoolIP, 80, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePoolIP, 443, userId, nullptr, false, true)); m_pools.push_back(new Url("emergency.xmrig.com", 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); } else { - m_pools.push_back(new Url(kDonatePool, 3333, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool, 5555, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePool, 5555, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePoolIP, 5555, userId, nullptr, false, true)); } m_strategy = new FailoverStrategy(m_pools, 1, 1, this, true); From 2e320d28d9b69cdd1c943f7db8c87c766cf35fa6 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 17 Mar 2018 18:08:17 +0700 Subject: [PATCH 130/389] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccd3fe6a..b1a010bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # v2.5.1 +- [#456](https://github.com/xmrig/xmrig/issues/459) Verbose errors related to donation pool was not fully silenced. - [#459](https://github.com/xmrig/xmrig/issues/459) Fixed regression (version 2.5.0 affected) with connection to **xmr.f2pool.com**. # v2.5.0 From 2de5d92d3a06c513648ef17ba43d4d8bcfa12779 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 18 Mar 2018 05:48:19 +0700 Subject: [PATCH 131/389] Removed unnecessary pointers cast. --- src/crypto/CryptoNight.cpp | 24 +++++++++++------------ src/crypto/CryptoNight_arm.h | 34 ++++++++++++++++----------------- src/crypto/CryptoNight_monero.h | 2 +- src/crypto/CryptoNight_x86.h | 34 ++++++++++++++++----------------- 4 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 996622c4..b605bfb8 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -39,7 +39,7 @@ #include "xmrig.h" -void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) = nullptr; +void (*cryptonight_hash_ctx)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) = nullptr; #define CRYPTONIGHT_HASH(NAME, ITERATIONS, MEM, MASK, SOFT_AES) \ @@ -55,55 +55,55 @@ void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, crypt } -static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, int variant) { +static void cryptonight_av1_aesni(const uint8_t *input, size_t size, uint8_t *output, struct cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) CRYPTONIGHT_HASH(single, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, false) # endif } -static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { +static void cryptonight_av2_aesni_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) CRYPTONIGHT_HASH(double, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, false) # endif } -static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { +static void cryptonight_av3_softaes(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { CRYPTONIGHT_HASH(single, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, true) } -static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { +static void cryptonight_av4_softaes_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { CRYPTONIGHT_HASH(double, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, true) } #ifndef XMRIG_NO_AEON -static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { +static void cryptonight_lite_av1_aesni(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) CRYPTONIGHT_HASH(single, AEON_ITER, AEON_MEMORY, AEON_MASK, false) # endif } -static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { +static void cryptonight_lite_av2_aesni_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) CRYPTONIGHT_HASH(double, AEON_ITER, AEON_MEMORY, AEON_MASK, false) # endif } -static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { +static void cryptonight_lite_av3_softaes(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { CRYPTONIGHT_HASH(single, AEON_ITER, AEON_MEMORY, AEON_MASK, true) } -static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { +static void cryptonight_lite_av4_softaes_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { CRYPTONIGHT_HASH(double, AEON_ITER, AEON_MEMORY, AEON_MASK, true) } -void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) = { +void (*cryptonight_variations[8])(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -114,7 +114,7 @@ void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_lite_av4_softaes_double }; #else -void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) = { +void (*cryptonight_variations[4])(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -160,7 +160,7 @@ bool CryptoNight::selfTest(int algo) { return false; } - char output[64]; + uint8_t output[64]; struct cryptonight_ctx *ctx = static_cast(_mm_malloc(sizeof(cryptonight_ctx), 16)); ctx->memory = static_cast(_mm_malloc(MONERO_MEMORY * 2, 16)); diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 6b214d66..18408536 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -49,27 +49,27 @@ extern "C" } -static inline void do_blake_hash(const void* input, size_t len, char* output) { - blake256_hash(reinterpret_cast(output), static_cast(input), len); +static inline void do_blake_hash(const uint8_t *input, size_t len, uint8_t *output) { + blake256_hash(output, input, len); } -static inline void do_groestl_hash(const void* input, size_t len, char* output) { - groestl(static_cast(input), len * 8, reinterpret_cast(output)); +static inline void do_groestl_hash(const uint8_t *input, size_t len, uint8_t *output) { + groestl(input, len * 8, output); } -static inline void do_jh_hash(const void* input, size_t len, char* output) { - jh_hash(32 * 8, static_cast(input), 8 * len, reinterpret_cast(output)); +static inline void do_jh_hash(const uint8_t *input, size_t len, uint8_t *output) { + jh_hash(32 * 8, input, 8 * len, output); } -static inline void do_skein_hash(const void* input, size_t len, char* output) { - xmr_skein(static_cast(input), reinterpret_cast(output)); +static inline void do_skein_hash(const uint8_t *input, size_t len, uint8_t *output) { + xmr_skein(input, output); } -void (* const extra_hashes[4])(const void *, size_t, char *) = {do_blake_hash, do_groestl_hash, do_jh_hash, do_skein_hash}; +void (* const extra_hashes[4])(const uint8_t *, size_t, uint8_t *) = {do_blake_hash, do_groestl_hash, do_jh_hash, do_skein_hash}; static inline __attribute__((always_inline)) __m128i _mm_set_epi64x(const uint64_t a, const uint64_t b) @@ -333,9 +333,9 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) template -inline void cryptonight_single_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx *__restrict__ ctx) { - keccak(static_cast(input), (int) size, ctx->state0, 200); + keccak(input, (int) size, ctx->state0, 200); VARIANT1_INIT(0); @@ -389,15 +389,15 @@ inline void cryptonight_single_hash(const void *__restrict__ input, size_t size, cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); keccakf(h0, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); + extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); } template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) { - keccak((const uint8_t *) input, (int) size, ctx->state0, 200); - keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); + keccak(input, (int) size, ctx->state0, 200); + keccak(input + size, (int) size, ctx->state1, 200); VARIANT1_INIT(0); VARIANT1_INIT(1); @@ -488,8 +488,8 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, keccakf(h0, 24); keccakf(h1, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); - extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); + extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); + extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, output + 32); } #endif /* __CRYPTONIGHT_ARM_H__ */ diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index 3c6146ac..61d63b46 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -30,7 +30,7 @@ #define VARIANT1_INIT(part) \ uint64_t tweak1_2_##part = 0; \ if (VARIANT > 0) { \ - tweak1_2_##part = (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ + tweak1_2_##part = (*reinterpret_cast(input + 35 + part * size) ^ \ *(reinterpret_cast(ctx->state##part) + 24)); \ } diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 1e5190d8..b7544dcc 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -49,27 +49,27 @@ extern "C" } -static inline void do_blake_hash(const void* input, size_t len, char* output) { - blake256_hash(reinterpret_cast(output), static_cast(input), len); +static inline void do_blake_hash(const uint8_t *input, size_t len, uint8_t *output) { + blake256_hash(output, input, len); } -static inline void do_groestl_hash(const void* input, size_t len, char* output) { - groestl(static_cast(input), len * 8, reinterpret_cast(output)); +static inline void do_groestl_hash(const uint8_t *input, size_t len, uint8_t *output) { + groestl(input, len * 8, output); } -static inline void do_jh_hash(const void* input, size_t len, char* output) { - jh_hash(32 * 8, static_cast(input), 8 * len, reinterpret_cast(output)); +static inline void do_jh_hash(const uint8_t *input, size_t len, uint8_t *output) { + jh_hash(32 * 8, input, 8 * len, output); } -static inline void do_skein_hash(const void* input, size_t len, char* output) { - xmr_skein(static_cast(input), reinterpret_cast(output)); +static inline void do_skein_hash(const uint8_t *input, size_t len, uint8_t *output) { + xmr_skein(input, output); } -void (* const extra_hashes[4])(const void *, size_t, char *) = {do_blake_hash, do_groestl_hash, do_jh_hash, do_skein_hash}; +void (* const extra_hashes[4])(const uint8_t *, size_t, uint8_t *) = {do_blake_hash, do_groestl_hash, do_jh_hash, do_skein_hash}; @@ -310,9 +310,9 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) template -inline void cryptonight_single_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx *__restrict__ ctx) { - keccak(static_cast(input), (int) size, ctx->state0, 200); + keccak(input, (int) size, ctx->state0, 200); VARIANT1_INIT(0); @@ -363,15 +363,15 @@ inline void cryptonight_single_hash(const void *__restrict__ input, size_t size, cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); keccakf(h0, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); + extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); } template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) { - keccak((const uint8_t *) input, (int) size, ctx->state0, 200); - keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); + keccak(input, (int) size, ctx->state0, 200); + keccak(input + size, (int) size, ctx->state1, 200); VARIANT1_INIT(0); VARIANT1_INIT(1); @@ -460,8 +460,8 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, keccakf(h0, 24); keccakf(h1, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); - extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); + extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); + extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, output + 32); } #endif /* __CRYPTONIGHT_X86_H__ */ From efa21b2531291ce957a9c7e8b65be81a1278bfe5 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 18 Mar 2018 08:30:56 +0700 Subject: [PATCH 132/389] #454 Fixed build with libmicrohttpd version below v0.9.35. --- CHANGELOG.md | 1 + src/api/Httpd.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1a010bb..850847ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # v2.5.1 +- [#454](https://github.com/xmrig/xmrig/issues/454) Fixed build with libmicrohttpd version below v0.9.35. - [#456](https://github.com/xmrig/xmrig/issues/459) Verbose errors related to donation pool was not fully silenced. - [#459](https://github.com/xmrig/xmrig/issues/459) Fixed regression (version 2.5.0 affected) with connection to **xmr.f2pool.com**. diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index a604b5de..861086ed 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -45,17 +45,17 @@ bool Httpd::start() return false; } - unsigned int flags = 0; + unsigned int flags = MHD_USE_SELECT_INTERNALLY; + +# if MHD_VERSION >= 0x00093500 if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) { flags = MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_EPOLL_INTERNALLY_LINUX_ONLY; } - else { - flags = MHD_USE_SELECT_INTERNALLY; - } if (MHD_is_feature_supported(MHD_FEATURE_IPv6)) { flags |= MHD_USE_DUAL_STACK; } +# endif m_daemon = MHD_start_daemon(flags, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); if (!m_daemon) { From 4440d3edd5c9054b064f7dc3be3ce901bfae123d Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 19 Mar 2018 07:03:50 +0700 Subject: [PATCH 133/389] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index acaf178f..001df323 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # XMRig -:warning: **You must update miners to version 2.5 before March 28 due [Monero PoW change](https://getmonero.org/2018/02/11/PoW-change-and-key-reuse.html).** +:warning: **You must update miners to version 2.5 before April 6 due [Monero PoW change](https://getmonero.org/2018/02/11/PoW-change-and-key-reuse.html).** [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub release](https://img.shields.io/github/release/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) From f2625b7ff5471ad59524f7956237a4a90e7f5827 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 20 Mar 2018 06:20:43 +0700 Subject: [PATCH 134/389] New app icon. --- res/app.ico | Bin 15086 -> 21497 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/res/app.ico b/res/app.ico index 7ab97bae0c63f05432da3e4bcdb12072d2700fe4..8c3d628fdfc1a97e45224d9722a44a50ab75f9bf 100644 GIT binary patch literal 21497 zcmd5?2|SeD_rEh{7*xhusmx@Fq(yIwHuDgolqDq*k|IiEtHg{gBwDnK##Aawp;TIC zT1gv8T5M@k*0L{S{?F}wpZ7n%d1pk+`}^I`=b8IF=iYP9_ndp~Iu`&OAOM(ffC{>Y z0bqi@Pnz`S_bCkk42A34O$ znZ=cN=l7-62HdF*yxUV{f48?HJ}0jt$inX`;!g#83^aq%G_s#*D=8^--8ej=RceCr zM)K(onWOe6y;@OIzlF%64kd$|@0QZtbYBMMS<`KE(q~T0AFr3!W4~SY<<`IeL!I^JJINY(*E2 z1PR%3xh>=&G12>J-cC*N$UmGi5c6mm5@O`8qd&)|ZEpN^_J}t9;g9b{xP8^6(FaW> z6)`}>o%-wNHf;6osHz~cUb{^8|Kmgbgdu}x#}`F-BLM%jwY{{`spIRr_`?G-y80b3`zG*5hWgqGQum*2MWZJVO`;pW($``ibzG9F^MQ-&XdZ5@vk zEDF}9yYSk*tJCjBlO0+T4I65%ueS(kI#kn^FGXMuYKS>{s7LkdbrnmRK}v1Mcyd?~ z0_HcJ>1_&?@lq^TBGdf;I9ImK-CLgwq=#3k7tZ5cqp(I$m0Lb1N}3EdnY0v?&1h?l zW8aDxnVk}2dN(DMa&Y+y_aorQ*|H#?4o+08@g-vZ-13Lx0^V7Kgv74RXv_M8nvGi%}_N+ z<+f_DUds|!6T$T!MBIj=VQCG@uF+;UDUE3Y!!55kAv18yyL!?q@m|r+EB3^!(R_>^ayBvY{SFt5l2882E;7AYR?>ZDWmdLo*QNO=Ve{U;( z6m#V*mknO=J1*j%N`gfpOtq}-3_pC~OMO~Y0;Pr&gS^h2V^>tKWvq=uK1k%M*&k(0 z!7~yl2gA%&sga=SokZ&BSQ2R{u<5`p-ksuNc?vLdCNb=!<#Qn?XQrP2ihdZoY ze&L}<=NsjsN8X*Ek2ancr}J`a)|f6t12p*|8dsAWKE|y8GaIjdt**A@T^8Qhc;QCZOQ-E#+gdKq z4R2j|;NqOln{Ju<8_EX9xo~SIUcHy=e0QAB^sx%u4PKdBoI-5;{RvBpjZN-+ zT5ypUlVdny`fL_*22Y-Mj>ZB`&fJnQ4OXEq2-FNmx)Cmf)0J*fb_|~>i5wx)qVZc9 zt)=05(h)AQAgwhpV}DTfY*VAqO`)x&(s`qw>|B+B=-@f49#X8R`p(=|wTvM*^YI+t zl@@3nlruq3Ng+-hG?=rueK;1`ARl*ah@2Hw`FwBJ9_8t4nR+j#Pn{p-td28vSx7eu zZQ^v*T0FZ>G?Yg6Bm|R*w{W%RXjW0gxzfnfSzS~qg4pxQk*v!MOFYur8+mR~@Zom` zs!|;i&nstJuhMjoTa%1v;m8*cX|fnJXYM1lx;4i_iO8K2wt`XUXHHk*f?-3+%Yf^P zf*^-(c0zT`y0F-FR^WN%x?6im5^}(9e`i{veyQ@utwpPmR4q`uVcJR)0$vh%I)_){ z?s)B%a(2f_R^l7gxeAO{38O;UQ0F04$q&A=&h?(4O6>r(GPCoEJLp|+FEr+L2UDLM z=AHkOJYRD1JkBNHS3zGwgVcj6L}xBe`jM=AP&Ic9ZD*?nxKdJScZAmVDWFJVspR5g z;0*1Q(Ut;@1zDaI6;`!AHwehK6t>4fWt7oCqen@+L;)yw`Rcz|R3u>|UCe^@Bg<#! zoPEB_c_EIJeLqUeJO+Qhmm?E0P5$&)kQLCoZ;ta^Sg$Gk=wnVA+Gd#+6*Ulu0oh}` z%JL<^wXr8XA_MgYg>Yi2oAVOfuCrgCv>SMS(`o6kM0}|NLocUOF2)Pii8%2pz(tXW zEa`UIJ^>PpQkD-or3KPn@tTJ?B%t1tVOfqB->V2p>gi+C=HZsn$rAfq5IbD7?=g$v zmQ$rTUE!BcU07~uhqFB0kym$K6d(u`0s zobZjlVkVMc=h+*p4feq*n=HK+e6hlNe?FJ0XgqzdW){@ONsy$`Hdf4_SIA_Y>yaOt zWdeyK0!}cS2}tT{_GVR2v>8jK_ddWhzbj@By-}Z$z z>mhgQBA$BkxmoKS?ddP^ww&TWZrCl(H*<}WsrSF*d}(SO$7UK*G?KrTUM;Y9B->~fHzDr2-IFM=s zx^5oVqNcaDlQlqAQjZZ@Vk@gx4W8?~N~C=)aww7*D?P>tcjzoTiiF$ss7-#hetOG= z2eY!$10Ri$1>y4_!^<}qv07{0x;O0oFb9%ox%PQhtKJ+@-p+uI$u zUa@?~VfC@VrfP0Nrfc{|H)Azij`nMMERv^%%r%{|_c-265-4`MmU1sC`0CDo!?i1F zdy_NP6GRwX(qe9JZ8x0{Tn}wqF<H*I5&50C>c;rsSs_ldsR}UkW0;WgDIb@ zdS|NPottzx)KeqBm5h->Y}1OoN(p^%Uif8077ocO#*EoN40`y^vv^td1bL?CTi z%-q)YT}yygdON#&&($M=U(7GPUS&}d;s3{twI}Xb1m2)Dmn{k7W>D2PE*(Rls!yJW zD?wXe9Ughx?B?iLKkH<1WY`*|rHRXRw!jMb$tJi_XV_8Zc(TT^;vojB_V@VhCFh@~ zN~2|2#<&$|nGU`%yWY{Ws{DW&O(xiBxmWP#GUK`8C&h8m(dN>o(KTitVg}(kZ`Dt9 zn7)p_U-gU%WOuDsUi&ms{hI7}z-rzshXB<{ZJIo}%@&t07u_dDD}do&A=S_@(eUXS z*~xqSjE7G&l0qD=I0aC_Xta-I*T@l#B*At1m9|cn3{4hNi%=>Vr836a((!mQ`OsNf z>}M+^0$8>+I((5}$;{EE0vt0e{^9Knrk-3oo1J)70wVEIfj~lltHbOpC2e|&F$u9w z|B{azaWWnv{LA`dI78P3#We8?ehB6tvHYa&tm zgk`N@Y7N9Ig71HRnd8thT66nV9ldGdV|Ok>rR>w%bsVT)QDN&iyD_C|p#B)VnG{2t zHB2)DIQteB)wrXyXh>U2Lb-h!K@<_2<%jzJC<=V*R`><=HqGekf<;Ob$4o(?oIi|L0YD+hqU-Kk+ ze6HEkKWi#6o4HP=?QA86|MYbu^_$)Vvql^sWG&-WR5*H;q%C=Vda-8tQMX5jSFeB3 z6j8J0NatS3Ou-WWTFWJG~KKP>W}3?`GT<37khQ0@oEUD&Krko3(>a#ei@{_sQFYW1Z=8W z0=Ceyhae9w;%qW|byDZyNJvkiGKd#@@rJuDTc$9Rf(}dELk=W;#@V2HQcY0yYkT8)LBeP+-mARW%v-{R3q00{kwh4=F;)Oi zrkwUk5Py}ti5Nb(G3$3MJBGTHSQVqd==~XYyu^>}E&68kWJKSKYqm)kPe(CZL(Jjj&z2A4wd8%u;w}BO)8>fEnRHa7 z1{9g%*EMm0G0sv~yax3fx)35WHl1?iNnU+LNmCH>yVR?LP9h}4FYP70y)vDfsjbUV zK>Je=D%R-aVXkHx+E0uPQkT({%AqS}zTZxtpXoWrAamK)yeqpai23MneAF0SAs%0K zY5i;I^f{cJpl7#lGmW*=C}7WpUL7=wsH5n0Bo1$B5QBna<|!CaVjilmMGnpHxHHk`VQVtpEc*Daa(S&m2$3;u(ES*F)#0*hVbdd$cJn~4 z$DI5FfLt?ZWSo2LlQ1pt*gCuUiCQ#ps4m;8+LiBWDE^H+a)LjhYB5cwWju&b!%G}V z9M0OgNE{7zRD+3c;fzkVu3&FVT*u;%A81CD6nEF`KTDF-0Pj=#O#>oBg%`Q{vNRkV zsZzu48+>HjUEJ0?Us4^)qrWM;rrl*~JUYA8%|KyNeBzM=US(L1!7$t@U$1ROl+z?v z)WgyEdsdAkj`uDaNe4unjx^9l-D!kd)wHDJw%k&%!xyKGhMEzsKBbPPSyOV9qyZw# z)bKF=P@?3sW~ccIxJhe_(76t^lsP^$sCR)RYLDauWc4?SM#{NfBO4r(&6~KOcm_TV z9UD-_7xg{u_2+Hrs(S!=<`db%o6rLH$i~CG`9@0MmTc(Bq(rtN*XvFUI!Y3)5b55c z8J&$WUFtrPNVc!9Ny{v`p}F5Mj34%vgg8CZj4=kJr1=s4XtPG+=gVJycS+7iilKBn zZ00wO8ZlGq^5hZ$Ii6(h?-d`t$+j8^{030H7Bp^U1jtY~@U8km*^PINdR@oK-5f+1`coF5xJmU>SBeace|qEFeEP zFp=i>F8_^m=M7Cz^W?(Xp(+*EHaGjJJbAPbc(J)TXXGUowYIj7&AhG$CO<7JbMVgS zkx!=g5+`(T^p*!M?+d!UXx>#E-`P%LAnDKy-w&fkOxN$6eeUB&ZE#0S=FF=bbYAb> zYfciN{BjK|HsFjCd!^))(y9qSfwzCB6LNA-61MNk-NmN3SDPV>WTyNYwSSAgiN z4PA-pL0h&29HRloE6aw!35TVVgWHZ)yyY&BA%W0d0D5lDYCo{&AR50ifH~9bU9YvA zBEYxez}~2jo9G&H=+0aUKS}v>OYNsmhi-dbW6Q-zgV03=;KrEyi)c!t@ra(4bEZ5o zCw5CPaJ`w?yI^E3d+r@iY1FWiwC+Y;PxF)M(d)#3>Y+J7ZE1r?B$KDfFE|CrGWs1H z>F&1rt4sW50n@{%)$4+KzO-J7Sr_h&qmI;UihoPEP-YJp8})2^%OCHfr_=XH8D(iLk*VWx?$$W;`Q-QLfCb3Xf8cCe~@cfYRj7J^_4@Xrccx*uObwO%t?esGW3X2o}RV#-eMe?DLIwJ#lH3X$J^`6mn&5a z8x2%yMu3vS8MU^zv~bi()}SC|l?0)}6JbN;_#AEJ6Mme{J<8UOB$>4W{>s@cq!`BEU z&Gqi5=wvRzC1op3Pp{>&N32f)o+`f(0>5jLf;`%P4-6SJsFx(a+k`1O zBQ9<*yYADc&BQi+bg>bZQWTMzdhZ06D^@wxm|PPP7~gs6u`WBTA}A99>bHGKcU^pL z9{R)WL)wjpGsYjAv*zK!A+K`+-%3rSkO7fugn?~S zZf=)jZe^h*&sck3S42ZX@X6O18D+DE#o8X=NrY)XjqP4*?~VSc0nCgS8D$tcqw^s8 zcM1A9qYwN`1SSUhx5@8FK0ZF?n>KG|qWL>FH@A+ZwzfQB4=jUq{;F&KJFxUuCd3>N z7p@-6hyB4fkUCK2@3dp}>eXt#etsnb!So|_p$@3) zXZ_62@8|Eo@@L{?pmsoAP$xfzAAJwpv?+U_`hQh@sQX88Lq6EIeifD;&Shz z*fE<2YQujsW-!cxd4FRG_8URo|CR6W^=YpS8%BmlM6@$PLwTYePft&wbE_yDx5viv z($dp^)zgH|-3p@m1CPhsj6Qv)%F4=qEKcR-UNm-pekG=Ra~$aU?2kPT%YJ4;|HY-2adVKLqQD!UQp6Z7^m-Jy+SSjwkBT z^seW9Y3==oE=UlJBT;b!eIO`bm|qshefdW{K|c5mF+=?N^hR@G<$|=8`@ags&mTvk z;s)A2ZF9pvaQzH^h!0{njcj5HViom$=}D(h8?ZPM6*nQLTmFF^>iAjw5IbtaJ5hKA z>3zi!o5K-mo3OaS1ZDon7$i)uFrDA)i^AlMV&lgu>U~~h zQ}%xgqbR))lPJ8x=--0W_8Z#JX!5sD*Y+U*s z@gXP^>gsP_6~rp)8;)Co^Mjz?U-1ofd~b{tg|R<$IQPN%`&VG)mjPvpnpgXSYoK+Y zF4(tzCaxeal<{xk7G@{ZkDaCVwV%fJtJuCtn5==)csvG<#{+=Z0{{)?O#sNioCAOu z%o*tYA(%tK=nr~l5db=RaS`S;06Jh!0RUw~83Cw*xy1kH9QZ8;0G2c0Hx9jvB#^_; ze={+lG9frPkj{s<3V!q1gV~AMjrjrd3+ke8e)`*QoWJ~t`4#i?Uw()8w5aHVpwHj; zw8-ek4<^rb>!xFOvH1UsRz zeSXD#C@5o~_lAD;y`X{I`++{|kAHup&iC5m-?(ET>aGNAqkrQr7Stgu9?;_^q5reP zdlsU6?Ywd&@AK!+!j9LzM-%*?TwN3cye9$m2*Ysk;zdzx{h<%Xx*}RRtuKaxf`WhO zhxb^1GNnQNbHf^S;2FB$1OIE_PyFp5=y!O|3C|TU8T@=0O;`?fi#lh8du0CqLHOH2 z(C>nKZ+`v!Ybn8ZY;7P&$G#zE{sh}W(C_@`j2It34@MJ~qwACJt{36ix~K&GE_hA| z_3$TQdIv%iYzKbXf_}&3^4Ebr#OObo3$_E4FU;?le183+Fk*H4lMC|!Jfj!I%da1{ z5q8Eb?5tjt41T(>7=UsiF8+GhyP$sl?}7S5nC~%p@Ei`)$)988Fo(87tXL9^*}nRK zpx+^{;Mt!rpTgL{9*7I}5$Hq6%inHLx2W+4@(KDK`c4=oQSHF*6JhUA_w+3d(}k4@ z`kg=C1*x$!A=-C4W9w$_xCleg>cmX~P^!NZiV1NnZ0C z7)B2Y{0e2DVpJnhKK;Foa_XOyGAGk?;lhPbU0q$tjvYHHw{G2f6zQ)O6&07ZY}s;^ z(#DM&FCy<_q_e?XPc`XKE}XPPW`pHxq12K&6_`W zOBkoK_U&S>H1@mUBDMpE4r|0De4m}|CFR#tX< zsiPlQ{NvlVZ*Sqch7a$$`H%_!rH5oyVVjKU($k&tdFH{=rTtLco4G7Yw|da=f3jV! z(Q%@@pC6XxWc$?*MBbb_dC#boQkNcUkrplv{1a`4Ijhpi2Oa<1sO;8pqP)5F^6Ntb zGW@f4`LbOZ%J;!v8+Wfh_~lRBN;>38-L6nR=v_E3Lc4w z@7bm*?f7TPopGFeaN>`#^X_XO$aypdpxkA)r%E0FY;k9-DIcukk`sT7`)7FkqInS8 zQl-it++%8)Y&<9HVJ~w^#?^NI{6w25zSHqA^G|ZO9p82Oe)5uiI^~01 z{PEoyi~U`OnLo)#oAhf1=0Dxs*-vvl*zsR=5b$%$% z7;^?x&gyeXhgcS>6P9)~+diP(e7&+u7OH%8&r4#WG$$e+%rV$T_6r^2&_nigJvi-&Pq-6~sB-UPZe*0&UPxDrM<1ag{AME@IMa2DFv=e+! zD=Ye?>$#j4PC?6HKC%cr%CHuF3;vu#8V2v%=7csqguQ~#@*8ExPu>)zzdxCsSgZGe z{{K9)5ukfo#Yk(BQunlQmrMQg+O|%gJ{`jO#`8D}jp5$M8JvY*gS?eYgOH!%T?@)Q zbN>AKtc^3ZwY3Xiw;gzNm&)~i2$ezE+r%XrjRqb!`uqDsIG2uOa!&-hcVNSIkJv8u zxLZ?zcwY4CuUk3X5jcl?4xVCx`z23;ch>LqpiXXJe|oW<&y8F zSBmk%~{)kw_%fpLHBme0=*qGh@*2#c#^hm_PqXoy%bF{bM_P zU|+$V+!QvKJ8<9t#bJo+K*h&C+UF_`ICX%BWslPipRnP#c8`U4BsnDN!VCV~1~~v; zqbe^Qe$wwWY}8W>d;JlP0m)=t40*wa?>BMIG~tGi^U$-1xdl6^7YB!WGY5(T?j)u5 zVdU5if{$~##C|km*??j z{}Jq&TK2W<@Ex%EjtDr2w7OspI(#~(p5Gu&y4Amn-;B*t8wVXe`hM4=O`_Zlf68yc z_Pt0;9CY}P9h5J>&?ED(&PwY8=J+L(Q^iA!RV?XqiIR=am>_3?E z1N&y~$|laI+t_!Ot9ryd;9d_9WA@XN#CUhLyg&VnggIbOtOz{Er;7vH+$i=-)8fw9 zEEl(l71&qJItRkWUO)-sI7x~F#>TAG1;*Gx`0Z=o4CBH)YLn)E;kRCBpAj&8mlo@^`UOAqCUsKf!fJ9 z`pqcvt^o6W?3b0T>Gi(vr0xgI^fT>K(6&S|67N&2#s8YAf96=wZ>+0JFvse~I_DbF zcrsl>9{rzKky@Ugv9=DQ4BdNuz;jiNe1I}tn9HTL8>~;mSf75WDX-m^K-mGa4csM< znYiAwvx71dTn;*xGg0yd^U-#bKK)Nc${x5|bTHR0$9%UF{h>zDp8&=xq?gzR>*G6A zPdd;oL;I)mN{M!N)1;r!<=CY6`-5>k7h}^EJ*L$Ul)Z%UFKr%AHi!rL;$SBZ+&)#8d62~U_e$6~iF!r7h%+U{=CbtLFu*T^^j~o5!gc@v From 04a95e3aa8fb913f75286f3a18ceedbe1b6cd84b Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 22 Mar 2018 10:19:29 +0700 Subject: [PATCH 135/389] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b00b1f1d..383d599a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # v2.5.0 -- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on March 28.** +- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on April 6.** - Added full IPv6 support. - Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. - [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. From 8aa73318c8ec1fda26667f3953ed7a6e4d05452a Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 24 Mar 2018 00:10:39 +0700 Subject: [PATCH 136/389] Backport changes from proxy. --- src/interfaces/IStrategy.h | 1 - src/net/Client.cpp | 67 +++++++++++++++++++++-- src/net/Client.h | 4 +- src/net/strategies/DonateStrategy.cpp | 22 +++----- src/net/strategies/DonateStrategy.h | 1 - src/net/strategies/FailoverStrategy.cpp | 27 ++------- src/net/strategies/FailoverStrategy.h | 2 - src/net/strategies/SinglePoolStrategy.cpp | 15 +---- src/net/strategies/SinglePoolStrategy.h | 2 - 9 files changed, 80 insertions(+), 61 deletions(-) diff --git a/src/interfaces/IStrategy.h b/src/interfaces/IStrategy.h index 6e915401..9f2795f9 100644 --- a/src/interfaces/IStrategy.h +++ b/src/interfaces/IStrategy.h @@ -39,7 +39,6 @@ public: virtual bool isActive() const = 0; virtual int64_t submit(const JobResult &result) = 0; virtual void connect() = 0; - virtual void release() = 0; virtual void resume() = 0; virtual void stop() = 0; virtual void tick(uint64_t now) = 0; diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 5d8c5919..b4506e44 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -112,6 +112,20 @@ void Client::connect(const Url *url) } +void Client::deleteLater() +{ + if (!m_listener) { + return; + } + + m_listener = nullptr; + + if (!disconnect()) { + delete this; + } +} + + void Client::setUrl(const Url *url) { if (!url || !url->isValid()) { @@ -172,7 +186,12 @@ int64_t Client::submit(const JobResult &result) const size_t size = snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRIu64 ",\"jsonrpc\":\"2.0\",\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"}}\n", m_sequence, m_rpcId.data(), result.jobId.data(), nonce, data); +# ifdef XMRIG_PROXY_PROJECT + m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff(), result.id); +# else m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff()); +# endif + return send(size); } @@ -185,9 +204,16 @@ bool Client::close() setState(ClosingState); - if (uv_is_closing(reinterpret_cast(m_socket)) == 0) { - uv_close(reinterpret_cast(m_socket), Client::onClose); - } + uv_read_stop(reinterpret_cast(m_socket)); + + uv_shutdown(new uv_shutdown_t, reinterpret_cast(m_socket), [](uv_shutdown_t* req, int status) { + + if (uv_is_closing(reinterpret_cast(req->handle)) == 0) { + uv_close(reinterpret_cast(req->handle), Client::onClose); + } + + delete req; + }); return true; } @@ -222,7 +248,14 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } +# ifdef XMRIG_PROXY_PROJECT + Job job(m_id, m_url.variant()); + job.setClientId(m_rpcId); + job.setCoin(m_url.coin()); +# else Job job(m_id, m_nicehash, m_url.algo(), m_url.variant()); +# endif + if (!job.setId(params["job_id"].GetString())) { *code = 3; return false; @@ -272,7 +305,9 @@ bool Client::parseLogin(const rapidjson::Value &result, int *code) return false; } +# ifndef XMRIG_PROXY_PROJECT m_nicehash = m_url.isNicehash(); +# endif if (result.HasMember("extensions")) { parseExtensions(result["extensions"]); @@ -296,7 +331,7 @@ int Client::resolve(const char *host) m_failures = 0; } - const int r = uv_getaddrinfo(uv_default_loop(), &m_resolver, Client::onResolved, host, NULL, &m_hints); + const int r = uv_getaddrinfo(uv_default_loop(), &m_resolver, Client::onResolved, host, nullptr, &m_hints); if (r) { if (!m_quiet) { LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_url.port(), uv_strerror(r)); @@ -550,6 +585,12 @@ void Client::ping() void Client::reconnect() { + if (!m_listener) { + delete this; + + return; + } + setState(ConnectingState); # ifndef XMRIG_PROXY_PROJECT @@ -598,6 +639,9 @@ void Client::startTimeout() void Client::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { auto client = getClient(handle->data); + if (!client) { + return; + } buf->base = &client->m_recvBuf.base[client->m_recvBufPos]; buf->len = client->m_recvBuf.len - client->m_recvBufPos; @@ -607,6 +651,9 @@ void Client::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t void Client::onClose(uv_handle_t *handle) { auto client = getClient(handle->data); + if (!client) { + return; + } delete client->m_socket; @@ -621,6 +668,10 @@ void Client::onClose(uv_handle_t *handle) void Client::onConnect(uv_connect_t *req, int status) { auto client = getClient(req->data); + if (!client) { + return; + } + if (status < 0) { if (!client->m_quiet) { LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); @@ -645,6 +696,10 @@ void Client::onConnect(uv_connect_t *req, int status) void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { auto client = getClient(stream->data); + if (!client) { + return; + } + if (nread < 0) { if (nread != UV_EOF && !client->m_quiet) { LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror((int) nread)); @@ -691,6 +746,10 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) { auto client = getClient(req->data); + if (!client) { + return; + } + if (status < 0) { if (!client->m_quiet) { LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); diff --git a/src/net/Client.h b/src/net/Client.h index ec68ab05..cf426119 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -56,12 +56,12 @@ public: constexpr static int kKeepAliveTimeout = 60 * 1000; Client(int id, const char *agent, IClientListener *listener); - ~Client(); bool disconnect(); int64_t submit(const JobResult &result); void connect(); void connect(const Url *url); + void deleteLater(); void setUrl(const Url *url); void tick(uint64_t now); @@ -76,6 +76,8 @@ public: inline void setRetryPause(int ms) { m_retryPause = ms; } private: + ~Client(); + bool close(); bool isCriticalError(const char *message); bool parseJob(const rapidjson::Value ¶ms, int *code); diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 8100afc0..ca2f94ca 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -37,8 +37,8 @@ extern "C" } -const static char *kDonatePool = "thanks.xmrig.com"; -const static char *kDonatePoolIP = "45.76.34.221"; +const static char *kDonatePool1 = "miner.fee.xmrig.com"; +const static char *kDonatePool2 = "emergency.fee.xmrig.com"; DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) : @@ -55,15 +55,13 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL Job::toHex(hash, 32, userId); if (algo == xmrig::ALGO_CRYPTONIGHT) { - m_pools.push_back(new Url(kDonatePool, 80, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool, 443, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePoolIP, 80, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePoolIP, 443, userId, nullptr, false, true)); - m_pools.push_back(new Url("emergency.xmrig.com", 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); + m_pools.push_back(new Url(kDonatePool1, 6666, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePool1, 80, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePool2, 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); } else { - m_pools.push_back(new Url(kDonatePool, 5555, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePoolIP, 5555, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePool1, 5555, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePool1, 7777, userId, nullptr, false, true)); } m_strategy = new FailoverStrategy(m_pools, 1, 1, this, true); @@ -77,6 +75,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL DonateStrategy::~DonateStrategy() { + delete m_strategy; } @@ -92,11 +91,6 @@ void DonateStrategy::connect() } -void DonateStrategy::release() -{ -} - - void DonateStrategy::stop() { uv_timer_stop(&m_timer); diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index bb3b63b7..9d7566f9 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -51,7 +51,6 @@ public: int64_t submit(const JobResult &result) override; void connect() override; - void release() override; void stop() override; void tick(uint64_t now) override; diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index 52bd2fbf..32dbaf1b 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -29,7 +29,6 @@ FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : - m_release(false), m_quiet(quiet), m_retries(retries), m_retryPause(retryPause), @@ -47,13 +46,17 @@ FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause FailoverStrategy::~FailoverStrategy() { for (Client *client : m_pools) { - delete client; + client->deleteLater(); } } int64_t FailoverStrategy::submit(const JobResult &result) { + if (m_active == -1) { + return -1; + } + return m_pools[m_active]->submit(result); } @@ -64,18 +67,6 @@ void FailoverStrategy::connect() } -void FailoverStrategy::release() -{ - m_release = true; - - for (size_t i = 0; i < m_pools.size(); ++i) { - if (m_pools[i]->disconnect()) { - m_remaining++; - } - } -} - - void FailoverStrategy::resume() { if (!isActive()) { @@ -110,14 +101,6 @@ void FailoverStrategy::tick(uint64_t now) void FailoverStrategy::onClose(Client *client, int failures) { if (failures == -1) { - if (m_release) { - m_remaining--; - - if (m_remaining == 0) { - delete this; - } - } - return; } diff --git a/src/net/strategies/FailoverStrategy.h b/src/net/strategies/FailoverStrategy.h index c8e24fb1..7c477b76 100644 --- a/src/net/strategies/FailoverStrategy.h +++ b/src/net/strategies/FailoverStrategy.h @@ -48,7 +48,6 @@ public: int64_t submit(const JobResult &result) override; void connect() override; - void release() override; void resume() override; void stop() override; void tick(uint64_t now) override; @@ -62,7 +61,6 @@ protected: private: void add(const Url *url); - bool m_release; const bool m_quiet; const int m_retries; const int m_retryPause; diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index b0d98519..fc7f209e 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -30,7 +30,6 @@ SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet) : m_active(false), - m_release(false), m_listener(listener) { m_client = new Client(0, Platform::userAgent(), this); @@ -42,7 +41,7 @@ SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategy SinglePoolStrategy::~SinglePoolStrategy() { - delete m_client; + m_client->deleteLater(); } @@ -58,13 +57,6 @@ void SinglePoolStrategy::connect() } -void SinglePoolStrategy::release() -{ - m_release = true; - m_client->disconnect(); -} - - void SinglePoolStrategy::resume() { if (!isActive()) { @@ -89,11 +81,6 @@ void SinglePoolStrategy::tick(uint64_t now) void SinglePoolStrategy::onClose(Client *client, int failures) { - if (m_release) { - delete this; - return; - } - if (!isActive()) { return; } diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h index 18d2192b..d5682cf7 100644 --- a/src/net/strategies/SinglePoolStrategy.h +++ b/src/net/strategies/SinglePoolStrategy.h @@ -45,7 +45,6 @@ public: int64_t submit(const JobResult &result) override; void connect() override; - void release() override; void resume() override; void stop() override; void tick(uint64_t now) override; @@ -58,7 +57,6 @@ protected: private: bool m_active; - bool m_release; Client *m_client; IStrategyListener *m_listener; }; From 673a1291e1342d2363dca1679b48dd22e7aa40fc Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 24 Mar 2018 09:41:32 +0700 Subject: [PATCH 137/389] Randomized donation start time. --- src/donate.h | 5 +++-- src/net/strategies/DonateStrategy.cpp | 13 +++++++++---- src/net/strategies/DonateStrategy.h | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/donate.h b/src/donate.h index 3a000948..e8230b87 100644 --- a/src/donate.h +++ b/src/donate.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,6 +31,7 @@ * Percentage of your hashing power that you want to donate to the developer, can be 0 if you don't want to do that. * Example of how it works for the default setting of 1: * You miner will mine into your usual pool for 99 minutes, then switch to the developer's pool for 1 minute. + * Since v2.5.1 start time randomized in range from 50 to 150 minutes minus donation time. * Switching is instant, and only happens after a successful connection, so you never loose any hashes. * * If you plan on changing this setting to 0 please consider making a one off donation to my wallet: diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index ca2f94ca..ae707e21 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -41,6 +41,11 @@ const static char *kDonatePool1 = "miner.fee.xmrig.com"; const static char *kDonatePool2 = "emergency.fee.xmrig.com"; +static inline int random(int min, int max){ + return min + rand() / (RAND_MAX / (max - min + 1) + 1); +} + + DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) : m_active(false), m_donateTime(level * 60 * 1000), @@ -69,7 +74,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL m_timer.data = this; uv_timer_init(uv_default_loop(), &m_timer); - idle(); + idle(random(3000, 9000) * 1000 - m_donateTime); } @@ -132,9 +137,9 @@ void DonateStrategy::onResultAccepted(IStrategy *strategy, Client *client, const } -void DonateStrategy::idle() +void DonateStrategy::idle(uint64_t timeout) { - uv_timer_start(&m_timer, DonateStrategy::onTimer, m_idleTime, 0); + uv_timer_start(&m_timer, DonateStrategy::onTimer, timeout, 0); } @@ -145,7 +150,7 @@ void DonateStrategy::suspend() m_active = false; m_listener->onPause(this); - idle(); + idle(m_idleTime); } diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 9d7566f9..4ef29958 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -61,7 +61,7 @@ protected: void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override; private: - void idle(); + void idle(uint64_t timeout); void suspend(); static void onTimer(uv_timer_t *handle); From 1011fd48916446ac4d03f619760c5b7615139164 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 24 Mar 2018 10:14:21 +0700 Subject: [PATCH 138/389] Partial fix for ARMv7 --- src/crypto/CryptoNight_monero.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index 61d63b46..a667a3b3 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -27,12 +27,22 @@ // VARIANT ALTERATIONS -#define VARIANT1_INIT(part) \ +#ifndef XMRIG_ARM +# define VARIANT1_INIT(part) \ uint64_t tweak1_2_##part = 0; \ if (VARIANT > 0) { \ tweak1_2_##part = (*reinterpret_cast(input + 35 + part * size) ^ \ *(reinterpret_cast(ctx->state##part) + 24)); \ } +#else +# define VARIANT1_INIT(part) \ + uint64_t tweak1_2_##part = 0; \ + if (VARIANT > 0) { \ + volatile const uint64_t a = *reinterpret_cast(input + 35 + part * size); \ + volatile const uint64_t b = *(reinterpret_cast(ctx->state##part) + 24); \ + tweak1_2_##part = a ^ b; \ + } +#endif #define VARIANT1_1(p) \ if (VARIANT > 0) { \ From e99928aa44a3b5dcc5b3ecd51025800b4386eb3a Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 24 Mar 2018 11:02:04 +0700 Subject: [PATCH 139/389] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 850847ff..89109c36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - [#459](https://github.com/xmrig/xmrig/issues/459) Fixed regression (version 2.5.0 affected) with connection to **xmr.f2pool.com**. # v2.5.0 -- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on March 28.** +- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on April 6.** - Added full IPv6 support. - Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. - [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. From f8f89a0946df69566e4364e3da331b11ce9f68d6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 24 Mar 2018 13:04:04 +0700 Subject: [PATCH 140/389] v2.5.1 release candidate. --- README.md | 2 ++ src/config.json | 11 ++++++----- src/version.h | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 001df323..cacae1dc 100644 --- a/README.md +++ b/README.md @@ -66,11 +66,13 @@ Use [config.xmrig.com](https://config.xmrig.com/xmrig) to generate, edit or shar --cpu-priority set process priority (0 idle, 2 normal to 5 highest) --no-huge-pages disable huge pages support --no-color disable colored output + --variant algorithm PoW variant --donate-level=N donate level, default 5% (5 minutes in 100 minutes) --user-agent set custom user-agent string for pool -B, --background run the miner in the background -c, --config=FILE load a JSON-format configuration file -l, --log-file=FILE log all output to a file + -S, --syslog use system log for output messages --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75) --safe safe adjust threads and av settings for current CPU --nicehash enable nicehash/xmrig-proxy support diff --git a/src/config.json b/src/config.json index 7813d4b9..acaff146 100644 --- a/src/config.json +++ b/src/config.json @@ -16,11 +16,12 @@ "threads": null, // number of miner threads "pools": [ { - "url": "pool.minemonero.pro:5555", // URL of mining server - "user": "", // username for mining server - "pass": "x", // password for mining server - "keepalive": true, // send keepalived for prevent timeout (need pool support) - "nicehash": false // enable nicehash/xmrig-proxy support + "url": "failover.xmrig.com:443", // URL of mining server + "user": "YOUR_WALLET", // username for mining server + "pass": "x", // password for mining server + "keepalive": true, // send keepalived for prevent timeout (need pool support) + "nicehash": false // enable nicehash/xmrig-proxy support + "variant": -1 // algorithm PoW variant } ], "api": { diff --git a/src/version.h b/src/version.h index cfaf7e9c..c0559845 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.5.0" +#define APP_VERSION "2.5.1" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 5 -#define APP_VER_BUILD 0 +#define APP_VER_BUILD 1 #define APP_VER_REV 0 #ifdef _MSC_VER From 89608ade3b4a4fafed8788f57ae2a854a1d16bda Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 24 Mar 2018 13:45:54 +0700 Subject: [PATCH 141/389] Removed unused private field in FailoverStrategy class. --- src/net/strategies/FailoverStrategy.cpp | 1 - src/net/strategies/FailoverStrategy.h | 1 - 2 files changed, 2 deletions(-) diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index 32dbaf1b..dbfeb311 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -34,7 +34,6 @@ FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause m_retryPause(retryPause), m_active(-1), m_index(0), - m_remaining(0), m_listener(listener) { for (const Url *url : urls) { diff --git a/src/net/strategies/FailoverStrategy.h b/src/net/strategies/FailoverStrategy.h index 7c477b76..8ad767ca 100644 --- a/src/net/strategies/FailoverStrategy.h +++ b/src/net/strategies/FailoverStrategy.h @@ -66,7 +66,6 @@ private: const int m_retryPause; int m_active; int m_index; - int m_remaining; IStrategyListener *m_listener; std::vector m_pools; }; From dd7f70d6e84a34ac6a385017fd87d983811070f1 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 24 Mar 2018 13:55:45 +0700 Subject: [PATCH 142/389] Fix error in config.json --- src/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.json b/src/config.json index acaff146..858435d2 100644 --- a/src/config.json +++ b/src/config.json @@ -20,7 +20,7 @@ "user": "YOUR_WALLET", // username for mining server "pass": "x", // password for mining server "keepalive": true, // send keepalived for prevent timeout (need pool support) - "nicehash": false // enable nicehash/xmrig-proxy support + "nicehash": false, // enable nicehash/xmrig-proxy support "variant": -1 // algorithm PoW variant } ], From 38da8ef86cbd806c6b396189ac4f148ef3d0729f Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 25 Mar 2018 15:04:00 +0700 Subject: [PATCH 143/389] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cacae1dc..59aa053c 100644 --- a/README.md +++ b/README.md @@ -125,9 +125,9 @@ Please note performance is highly dependent on system load. The numbers above ar ### SHA-256 ``` 232ea929f7219c8de81c7e6fcae7437d40d8128cf55b597a6e1fca0cd77f9f5b xmrig-2.5.0-xenial-amd64.tar.gz/xmrig-2.5.0/xmrig -6149ffed21e740cac12aa61b2fdd17248cbd3e51bab2289d2766aad1d29df910 xmrig-2.5.0-gcc-win32.zip/xmrig.exe -9c89f80e21db906439a7a1b333b8215dbe07d8e42f98a63f6c10c954288a7108 xmrig-2.5.0-gcc-win64.zip/xmrig.exe -b50773c5a74ca9921597a1152e2469ec266cc89eb9765038db7e876f0bcece73 xmrig-2.5.0-msvc-win64.zip/xmrig.exe +d2e53adb26f01ff786aab75433f8deb36beeb4e85d567428b1a596ce6a4b9034 xmrig-2.5.1-gcc-win32.zip/xmrig.exe +10740d66732f7cc48b619db2c26756e0238b6ab5e85cd6901906635ecc30af13 xmrig-2.5.1-gcc-win64.zip/xmrig.exe +33d46714b664e8e0c85c26f636ec87cb60def7008c75249a8cc09a586ec6dd5a xmrig-2.5.1-msvc-win64.zip/xmrig.exe ``` ## Contacts From f852996f97ded849e39f4ddd66f15ad047c6d6e6 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 25 Mar 2018 15:14:19 +0700 Subject: [PATCH 144/389] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 59aa053c..da371410 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -232ea929f7219c8de81c7e6fcae7437d40d8128cf55b597a6e1fca0cd77f9f5b xmrig-2.5.0-xenial-amd64.tar.gz/xmrig-2.5.0/xmrig +8e30d51cccf8f32e26648fd54da9a39146c4b15ea69284c5e7c974780dfdab4a xmrig-2.5.1-xenial-amd64.tar.gz/xmrig-2.5.1/xmrig d2e53adb26f01ff786aab75433f8deb36beeb4e85d567428b1a596ce6a4b9034 xmrig-2.5.1-gcc-win32.zip/xmrig.exe 10740d66732f7cc48b619db2c26756e0238b6ab5e85cd6901906635ecc30af13 xmrig-2.5.1-gcc-win64.zip/xmrig.exe 33d46714b664e8e0c85c26f636ec87cb60def7008c75249a8cc09a586ec6dd5a xmrig-2.5.1-msvc-win64.zip/xmrig.exe From 6f4ed5f66d1956a3936979b1015e72f8b374e8e3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 26 Mar 2018 11:47:01 +0700 Subject: [PATCH 145/389] #478 Fixed totally broken reconnect. --- cmake/flags.cmake | 4 ++++ src/App_unix.cpp | 6 ++++-- src/net/Client.cpp | 49 +++++++++++++++++++++++++++++++++------------- src/net/Client.h | 1 + 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/cmake/flags.cmake b/cmake/flags.cmake index 488f1236..13e50564 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -6,6 +6,10 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "") set(CMAKE_BUILD_TYPE Release) endif() +if (CMAKE_BUILD_TYPE STREQUAL "Release") + add_definitions(/DNDEBUG) +endif() + if (CMAKE_CXX_COMPILER_ID MATCHES GNU) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-strict-aliasing") diff --git a/src/App_unix.cpp b/src/App_unix.cpp index 66957208..674a53e6 100644 --- a/src/App_unix.cpp +++ b/src/App_unix.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,6 +36,8 @@ void App::background() { + signal(SIGPIPE, SIG_IGN); + if (m_options->affinity() != -1L) { Cpu::setAffinity(-1, m_options->affinity()); } diff --git a/src/net/Client.cpp b/src/net/Client.cpp index b4506e44..c8a93f24 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -21,6 +21,7 @@ * along with this program. If not, see . */ +#include #include #include #include @@ -204,16 +205,30 @@ bool Client::close() setState(ClosingState); - uv_read_stop(reinterpret_cast(m_socket)); + uv_stream_t *stream = reinterpret_cast(m_socket); - uv_shutdown(new uv_shutdown_t, reinterpret_cast(m_socket), [](uv_shutdown_t* req, int status) { + if (uv_is_readable(stream) == 1) { + uv_read_stop(stream); + } - if (uv_is_closing(reinterpret_cast(req->handle)) == 0) { - uv_close(reinterpret_cast(req->handle), Client::onClose); + if (uv_is_writable(stream) == 1) { + const int rc = uv_shutdown(new uv_shutdown_t, stream, [](uv_shutdown_t* req, int status) { + if (uv_is_closing(reinterpret_cast(req->handle)) == 0) { + uv_close(reinterpret_cast(req->handle), Client::onClose); + } + + delete req; + }); + + assert(rc == 0); + + if (rc != 0) { + onClose(); } - - delete req; - }); + } + else { + uv_close(reinterpret_cast(m_socket), Client::onClose); + } return true; } @@ -442,6 +457,18 @@ void Client::login() } +void Client::onClose() +{ + delete m_socket; + + m_stream = nullptr; + m_socket = nullptr; + setState(UnconnectedState); + + reconnect(); +} + + void Client::parse(char *line, size_t len) { startTimeout(); @@ -655,13 +682,7 @@ void Client::onClose(uv_handle_t *handle) return; } - delete client->m_socket; - - client->m_stream = nullptr; - client->m_socket = nullptr; - client->setState(UnconnectedState); - - client->reconnect(); + client->onClose(); } diff --git a/src/net/Client.h b/src/net/Client.h index cf426119..fff7a156 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -87,6 +87,7 @@ private: void connect(const std::vector &ipv4, const std::vector &ipv6); void connect(sockaddr *addr); void login(); + void onClose(); void parse(char *line, size_t len); void parseExtensions(const rapidjson::Value &value); void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error); From ec08550fd92f79c576cbe9adf3dc874c53751b37 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 26 Mar 2018 13:34:04 +0700 Subject: [PATCH 146/389] v2.5.2 --- CHANGELOG.md | 3 +++ src/version.h | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89109c36..ab31e698 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# v2.5.2 +- [#448](https://github.com/xmrig/xmrig/issues/478) Fixed broken reconnect. + # v2.5.1 - [#454](https://github.com/xmrig/xmrig/issues/454) Fixed build with libmicrohttpd version below v0.9.35. - [#456](https://github.com/xmrig/xmrig/issues/459) Verbose errors related to donation pool was not fully silenced. diff --git a/src/version.h b/src/version.h index c0559845..060edcb7 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.5.1" +#define APP_VERSION "2.5.2" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 5 -#define APP_VER_BUILD 1 +#define APP_VER_BUILD 2 #define APP_VER_REV 0 #ifdef _MSC_VER From 1bd05d136bde25c00bd0809810c2af96dc639b00 Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 26 Mar 2018 13:59:20 +0700 Subject: [PATCH 147/389] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index da371410..962408e9 100644 --- a/README.md +++ b/README.md @@ -124,10 +124,10 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -8e30d51cccf8f32e26648fd54da9a39146c4b15ea69284c5e7c974780dfdab4a xmrig-2.5.1-xenial-amd64.tar.gz/xmrig-2.5.1/xmrig -d2e53adb26f01ff786aab75433f8deb36beeb4e85d567428b1a596ce6a4b9034 xmrig-2.5.1-gcc-win32.zip/xmrig.exe -10740d66732f7cc48b619db2c26756e0238b6ab5e85cd6901906635ecc30af13 xmrig-2.5.1-gcc-win64.zip/xmrig.exe -33d46714b664e8e0c85c26f636ec87cb60def7008c75249a8cc09a586ec6dd5a xmrig-2.5.1-msvc-win64.zip/xmrig.exe +b070d06a3615f3db67ad3beab43d6d21f3c88026aa2b4726a93df47145cd30ec xmrig-2.5.2-xenial-amd64.tar.gz/xmrig-2.5.2/xmrig +4852135d3f04fd450ba39abce51ca40ff9131d222220c8b30804be05f6679295 xmrig-2.5.2-gcc-win32.zip/xmrig.exe +284309d07f08261af19c937ece6d2031910d9124a7359c207ded65890b2d7c5f xmrig-2.5.2-gcc-win64.zip/xmrig.exe +e1dc46158a578fb030538fb06e5663a6acc5763545fb447a00ce0a6b388c5226 xmrig-2.5.2-msvc-win64.zip/xmrig.exe ``` ## Contacts From 9fe863b5d7d554724fa79e454d662066f3a22aaf Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 27 Mar 2018 14:01:38 +0700 Subject: [PATCH 148/389] Background http changes from proxy. --- CMakeLists.txt | 17 ++- src/App.cpp | 11 +- src/App.h | 10 +- src/api/Api.cpp | 45 +++--- src/api/Api.h | 20 ++- src/api/{ApiState.cpp => ApiRouter.cpp} | 91 ++++++++---- src/api/{ApiState.h => ApiRouter.h} | 36 +++-- src/api/HttpBody.h | 69 ++++++++++ src/api/HttpReply.h | 53 +++++++ src/api/HttpRequest.cpp | 175 ++++++++++++++++++++++++ src/api/HttpRequest.h | 84 ++++++++++++ src/api/Httpd.cpp | 138 ++++++++++++------- src/api/Httpd.h | 25 +++- src/core/Controller.cpp | 125 +++++++++++++++++ src/core/Controller.h | 63 +++++++++ src/interfaces/IControllerListener.h | 46 +++++++ src/interfaces/IWatcherListener.h | 46 +++++++ 17 files changed, 929 insertions(+), 125 deletions(-) rename src/api/{ApiState.cpp => ApiRouter.cpp} (77%) rename src/api/{ApiState.h => ApiRouter.h} (62%) create mode 100644 src/api/HttpBody.h create mode 100644 src/api/HttpReply.h create mode 100644 src/api/HttpRequest.cpp create mode 100644 src/api/HttpRequest.h create mode 100644 src/core/Controller.cpp create mode 100644 src/core/Controller.h create mode 100644 src/interfaces/IControllerListener.h create mode 100644 src/interfaces/IWatcherListener.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8823b078..1be15601 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,17 +12,19 @@ include (cmake/cpu.cmake) set(HEADERS src/api/Api.h - src/api/ApiState.h src/api/NetworkState.h src/App.h src/Console.h + src/core/Controller.h src/Cpu.h src/interfaces/IClientListener.h src/interfaces/IConsoleListener.h + src/interfaces/IControllerListener.h src/interfaces/IJobResultListener.h src/interfaces/ILogBackend.h src/interfaces/IStrategy.h src/interfaces/IStrategyListener.h + src/interfaces/IWatcherListener.h src/interfaces/IWorker.h src/log/ConsoleLog.h src/log/FileLog.h @@ -74,10 +76,10 @@ endif() set(SOURCES src/api/Api.cpp - src/api/ApiState.cpp src/api/NetworkState.cpp src/App.cpp src/Console.cpp + src/core/Controller.cpp src/log/ConsoleLog.cpp src/log/FileLog.cpp src/log/Log.cpp @@ -192,7 +194,16 @@ if (WITH_HTTPD) if (MHD_FOUND) include_directories(${MHD_INCLUDE_DIRS}) - set(HTTPD_SOURCES src/api/Httpd.h src/api/Httpd.cpp) + set(HTTPD_SOURCES + src/api/ApiRouter.h + src/api/HttpBody.h + src/api/Httpd.h + src/api/HttpReply.h + src/api/HttpRequest.h + src/api/ApiRouter.cpp + src/api/Httpd.cpp + src/api/HttpRequest.cpp + ) else() message(FATAL_ERROR "microhttpd NOT found: use `-DWITH_HTTPD=OFF` to build without http deamon support") endif() diff --git a/src/App.cpp b/src/App.cpp index d656acc8..ed425f90 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,6 +29,7 @@ #include "api/Api.h" #include "App.h" #include "Console.h" +#include "core/Controller.h" #include "Cpu.h" #include "crypto/CryptoNight.h" #include "log/ConsoleLog.h" @@ -64,6 +65,8 @@ App::App(int argc, char **argv) : { m_self = this; + m_controller = new xmrig::Controller(); + Cpu::init(); m_options = Options::parse(argc, argv); if (!m_options) { @@ -138,11 +141,11 @@ int App::exec() } # ifndef XMRIG_NO_API - Api::start(); + Api::start(m_controller); # endif # ifndef XMRIG_NO_HTTPD - m_httpd = new Httpd(m_options->apiPort(), m_options->apiToken()); + m_httpd = new Httpd(m_options->apiPort(), m_options->apiToken(), true, true); m_httpd->start(); # endif diff --git a/src/App.h b/src/App.h index 1b96040d..9116a79a 100644 --- a/src/App.h +++ b/src/App.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,6 +37,11 @@ class Network; class Options; +namespace xmrig { + class Controller; +} + + class App : public IConsoleListener { public: @@ -64,6 +69,7 @@ private: uv_signal_t m_sigHUP; uv_signal_t m_sigINT; uv_signal_t m_sigTERM; + xmrig::Controller *m_controller; }; diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 729ebccd..36859136 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,17 +25,17 @@ #include "api/Api.h" -#include "api/ApiState.h" +#include "api/ApiRouter.h" +#include "api/HttpReply.h" +#include "api/HttpRequest.h" -ApiState *Api::m_state = nullptr; -uv_mutex_t Api::m_mutex; +ApiRouter *Api::m_router = nullptr; -bool Api::start() +bool Api::start(xmrig::Controller *controller) { - uv_mutex_init(&m_mutex); - m_state = new ApiState(); + m_router = new ApiRouter(controller); return true; } @@ -43,43 +43,40 @@ bool Api::start() void Api::release() { - delete m_state; + delete m_router; } -char *Api::get(const char *url, int *status) +void Api::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) { - if (!m_state) { - return nullptr; + if (!m_router) { + reply.status = 500; + return; } - uv_mutex_lock(&m_mutex); - char *buf = m_state->get(url, status); - uv_mutex_unlock(&m_mutex); + if (req.method() == xmrig::HttpRequest::Get) { + return m_router->get(req, reply); + } - return buf; + m_router->exec(req, reply); } void Api::tick(const Hashrate *hashrate) { - if (!m_state) { + if (!m_router) { return; } - uv_mutex_lock(&m_mutex); - m_state->tick(hashrate); - uv_mutex_unlock(&m_mutex); + m_router->tick(hashrate); } void Api::tick(const NetworkState &network) { - if (!m_state) { + if (!m_router) { return; } - uv_mutex_lock(&m_mutex); - m_state->tick(network); - uv_mutex_unlock(&m_mutex); + m_router->tick(network); } diff --git a/src/api/Api.h b/src/api/Api.h index 72c65c3c..43c7b17e 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,24 +28,30 @@ #include -class ApiState; +class ApiRouter; class Hashrate; class NetworkState; +namespace xmrig { + class Controller; + class HttpReply; + class HttpRequest; +} + + class Api { public: - static bool start(); + static bool start(xmrig::Controller *controller); static void release(); - static char *get(const char *url, int *status); + static void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply); static void tick(const Hashrate *hashrate); static void tick(const NetworkState &results); private: - static ApiState *m_state; - static uv_mutex_t m_mutex; + static ApiRouter *m_router; }; #endif /* __API_H__ */ diff --git a/src/api/ApiState.cpp b/src/api/ApiRouter.cpp similarity index 77% rename from src/api/ApiState.cpp rename to src/api/ApiRouter.cpp index c963a1d6..6ee8e7a4 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiRouter.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,7 +32,9 @@ #endif -#include "api/ApiState.h" +#include "api/ApiRouter.h" +#include "api/HttpReply.h" +#include "api/HttpRequest.h" #include "Cpu.h" #include "Mem.h" #include "net/Job.h" @@ -61,7 +63,8 @@ static inline double normalize(double d) } -ApiState::ApiState() +ApiRouter::ApiRouter(xmrig::Controller *controller) : + m_controller(controller) { m_threads = Options::i()->threads(); m_hashrate = new double[m_threads * 3](); @@ -69,24 +72,18 @@ ApiState::ApiState() memset(m_totalHashrate, 0, sizeof(m_totalHashrate)); memset(m_workerId, 0, sizeof(m_workerId)); - if (Options::i()->apiWorkerId()) { - strncpy(m_workerId, Options::i()->apiWorkerId(), sizeof(m_workerId) - 1); - } - else { - gethostname(m_workerId, sizeof(m_workerId) - 1); - } - + setWorkerId(Options::i()->apiWorkerId()); genId(); } -ApiState::~ApiState() +ApiRouter::~ApiRouter() { delete [] m_hashrate; } -char *ApiState::get(const char *url, int *status) const +void ApiRouter::ApiRouter::get(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) const { rapidjson::Document doc; doc.SetObject(); @@ -97,11 +94,22 @@ char *ApiState::get(const char *url, int *status) const getResults(doc); getConnection(doc); - return finalize(doc); + return finalize(reply, doc); } -void ApiState::tick(const Hashrate *hashrate) +void ApiRouter::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) +{ +// if (req.method() == xmrig::HttpRequest::Put && req.match("/1/config")) { +// m_controller->config()->reload(req.body()); +// return; +// } + + reply.status = 404; +} + + +void ApiRouter::tick(const Hashrate *hashrate) { for (int i = 0; i < m_threads; ++i) { m_hashrate[i * 3] = hashrate->calc((size_t) i, Hashrate::ShortInterval); @@ -116,24 +124,32 @@ void ApiState::tick(const Hashrate *hashrate) } -void ApiState::tick(const NetworkState &network) +void ApiRouter::tick(const NetworkState &network) { m_network = network; } -char *ApiState::finalize(rapidjson::Document &doc) const +void ApiRouter::onConfigChanged(xmrig::Config *config, xmrig::Config *previousConfig) +{ +// updateWorkerId(config->apiWorkerId(), previousConfig->apiWorkerId()); +} + + +void ApiRouter::finalize(xmrig::HttpReply &reply, rapidjson::Document &doc) const { rapidjson::StringBuffer buffer(0, 4096); rapidjson::PrettyWriter writer(buffer); writer.SetMaxDecimalPlaces(10); doc.Accept(writer); - return strdup(buffer.GetString()); + reply.status = 200; + reply.buf = strdup(buffer.GetString()); + reply.size = buffer.GetSize(); } -void ApiState::genId() +void ApiRouter::genId() { memset(m_id, 0, sizeof(m_id)); @@ -166,7 +182,7 @@ void ApiState::genId() } -void ApiState::getConnection(rapidjson::Document &doc) const +void ApiRouter::getConnection(rapidjson::Document &doc) const { auto &allocator = doc.GetAllocator(); @@ -181,7 +197,7 @@ void ApiState::getConnection(rapidjson::Document &doc) const } -void ApiState::getHashrate(rapidjson::Document &doc) const +void ApiRouter::getHashrate(rapidjson::Document &doc) const { auto &allocator = doc.GetAllocator(); @@ -209,14 +225,14 @@ void ApiState::getHashrate(rapidjson::Document &doc) const } -void ApiState::getIdentify(rapidjson::Document &doc) const +void ApiRouter::getIdentify(rapidjson::Document &doc) const { doc.AddMember("id", rapidjson::StringRef(m_id), doc.GetAllocator()); doc.AddMember("worker_id", rapidjson::StringRef(m_workerId), doc.GetAllocator()); } -void ApiState::getMiner(rapidjson::Document &doc) const +void ApiRouter::getMiner(rapidjson::Document &doc) const { auto &allocator = doc.GetAllocator(); @@ -236,7 +252,7 @@ void ApiState::getMiner(rapidjson::Document &doc) const } -void ApiState::getResults(rapidjson::Document &doc) const +void ApiRouter::getResults(rapidjson::Document &doc) const { auto &allocator = doc.GetAllocator(); @@ -258,3 +274,30 @@ void ApiState::getResults(rapidjson::Document &doc) const doc.AddMember("results", results, allocator); } + + +void ApiRouter::setWorkerId(const char *id) +{ + memset(m_workerId, 0, sizeof(m_workerId)); + + if (id && strlen(id) > 0) { + strncpy(m_workerId, id, sizeof(m_workerId) - 1); + } + else { + gethostname(m_workerId, sizeof(m_workerId) - 1); + } +} + + +void ApiRouter::updateWorkerId(const char *id, const char *previousId) +{ + if (id == previousId) { + return; + } + + if (id != nullptr && previousId != nullptr && strcmp(id, previousId) == 0) { + return; + } + + setWorkerId(id); +} diff --git a/src/api/ApiState.h b/src/api/ApiRouter.h similarity index 62% rename from src/api/ApiState.h rename to src/api/ApiRouter.h index 7ecca36d..2ae1cc80 100644 --- a/src/api/ApiState.h +++ b/src/api/ApiRouter.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,35 +21,50 @@ * along with this program. If not, see . */ -#ifndef __APISTATE_H__ -#define __APISTATE_H__ +#ifndef __APIROUTER_H__ +#define __APIROUTER_H__ #include "api/NetworkState.h" +#include "interfaces/IControllerListener.h" #include "rapidjson/fwd.h" class Hashrate; -class ApiState +namespace xmrig { + class Controller; + class HttpReply; + class HttpRequest; +} + + +class ApiRouter : public xmrig::IControllerListener { public: - ApiState(); - ~ApiState(); + ApiRouter(xmrig::Controller *controller); + ~ApiRouter(); + + void get(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) const; + void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply); - char *get(const char *url, int *status) const; void tick(const Hashrate *hashrate); void tick(const NetworkState &results); +protected: + void onConfigChanged(xmrig::Config *config, xmrig::Config *previousConfig) override; + private: - char *finalize(rapidjson::Document &doc) const; + void finalize(xmrig::HttpReply &reply, rapidjson::Document &doc) const; void genId(); void getConnection(rapidjson::Document &doc) const; void getHashrate(rapidjson::Document &doc) const; void getIdentify(rapidjson::Document &doc) const; void getMiner(rapidjson::Document &doc) const; void getResults(rapidjson::Document &doc) const; + void setWorkerId(const char *id); + void updateWorkerId(const char *id, const char *previousId); char m_id[17]; char m_workerId[128]; @@ -58,6 +73,7 @@ private: double m_totalHashrate[3]; int m_threads; NetworkState m_network; + xmrig::Controller *m_controller; }; -#endif /* __APISTATE_H__ */ +#endif /* __APIROUTER_H__ */ diff --git a/src/api/HttpBody.h b/src/api/HttpBody.h new file mode 100644 index 00000000..0b143fb7 --- /dev/null +++ b/src/api/HttpBody.h @@ -0,0 +1,69 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __HTTPBODY_H__ +#define __HTTPBODY_H__ + + +#include + + +namespace xmrig { + + +class HttpBody +{ +public: + inline HttpBody() : + m_pos(0) + {} + + + inline bool write(const char *data, size_t size) + { + if (size > (sizeof(m_data) - m_pos - 1)) { + return false; + } + + memcpy(m_data + m_pos, data, size); + + m_pos += size; + m_data[m_pos] = '\0'; + + return true; + } + + + inline const char *data() const { return m_data; } + +private: + char m_data[32768]; + size_t m_pos; +}; + + +} /* namespace xmrig */ + + +#endif /* __HTTPBODY_H__ */ diff --git a/src/api/HttpReply.h b/src/api/HttpReply.h new file mode 100644 index 00000000..6a6cb802 --- /dev/null +++ b/src/api/HttpReply.h @@ -0,0 +1,53 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __HTTPREPLY_H__ +#define __HTTPREPLY_H__ + + +#include + + +namespace xmrig { + + +class HttpReply +{ +public: + HttpReply() : + buf(nullptr), + status(200), + size(0) + {} + + char *buf; + int status; + size_t size; +}; + + +} /* namespace xmrig */ + + +#endif /* __HTTPREPLY_H__ */ diff --git a/src/api/HttpRequest.cpp b/src/api/HttpRequest.cpp new file mode 100644 index 00000000..ac7210bd --- /dev/null +++ b/src/api/HttpRequest.cpp @@ -0,0 +1,175 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include + +#include "api/HttpBody.h" +#include "api/HttpRequest.h" +#include "api/HttpReply.h" + + +#ifndef MHD_HTTP_PAYLOAD_TOO_LARGE +# define MHD_HTTP_PAYLOAD_TOO_LARGE 413 +#endif + + +xmrig::HttpRequest::HttpRequest(MHD_Connection *connection, const char *url, const char *method, const char *uploadData, size_t *uploadSize, void **cls) : + m_fulfilled(true), + m_restricted(true), + m_uploadData(uploadData), + m_url(url), + m_body(static_cast(*cls)), + m_method(Unsupported), + m_connection(connection), + m_uploadSize(uploadSize), + m_cls(cls) +{ + if (strcmp(method, MHD_HTTP_METHOD_OPTIONS) == 0) { + m_method = Options; + } + else if (strcmp(method, MHD_HTTP_METHOD_GET) == 0) { + m_method = Get; + } + else if (strcmp(method, MHD_HTTP_METHOD_PUT) == 0) { + m_method = Put; + } +} + + +xmrig::HttpRequest::~HttpRequest() +{ + if (m_fulfilled) { + delete m_body; + } +} + + +bool xmrig::HttpRequest::match(const char *path) const +{ + return strcmp(m_url, path) == 0; +} + + +bool xmrig::HttpRequest::process(const char *accessToken, bool restricted, xmrig::HttpReply &reply) +{ + m_restricted = restricted || !accessToken; + + if (m_body) { + if (*m_uploadSize != 0) { + if (!m_body->write(m_uploadData, *m_uploadSize)) { + *m_cls = nullptr; + m_fulfilled = true; + reply.status = MHD_HTTP_PAYLOAD_TOO_LARGE; + return false; + } + + *m_uploadSize = 0; + m_fulfilled = false; + return true; + } + + m_fulfilled = true; + return true; + } + + reply.status = auth(accessToken); + if (reply.status != MHD_HTTP_OK) { + return false; + } + + if (m_restricted && m_method != Get) { + reply.status = MHD_HTTP_FORBIDDEN; + return false; + } + + if (m_method == Get) { + return true; + } + + const char *contentType = MHD_lookup_connection_value(m_connection, MHD_HEADER_KIND, "Content-Type"); + if (!contentType || strcmp(contentType, "application/json") != 0) { + reply.status = MHD_HTTP_UNSUPPORTED_MEDIA_TYPE; + return false; + } + + m_body = new xmrig::HttpBody(); + m_fulfilled = false; + *m_cls = m_body; + + return true; +} + + +const char *xmrig::HttpRequest::body() const +{ + return m_body ? m_body->data() : nullptr; +} + + +int xmrig::HttpRequest::end(const HttpReply &reply) +{ + if (reply.buf) { + return end(reply.status, MHD_create_response_from_buffer(reply.size ? reply.size : strlen(reply.buf), (void*) reply.buf, MHD_RESPMEM_MUST_FREE)); + } + + return end(reply.status, nullptr); +} + + +int xmrig::HttpRequest::end(int status, MHD_Response *rsp) +{ + if (!rsp) { + rsp = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT); + } + + MHD_add_response_header(rsp, "Content-Type", "application/json"); + MHD_add_response_header(rsp, "Access-Control-Allow-Origin", "*"); + MHD_add_response_header(rsp, "Access-Control-Allow-Methods", "GET, PUT"); + MHD_add_response_header(rsp, "Access-Control-Allow-Headers", "Authorization"); + + const int ret = MHD_queue_response(m_connection, status, rsp); + MHD_destroy_response(rsp); + return ret; +} + + +int xmrig::HttpRequest::auth(const char *accessToken) +{ + if (!accessToken) { + return MHD_HTTP_OK; + } + + const char *header = MHD_lookup_connection_value(m_connection, MHD_HEADER_KIND, "Authorization"); + if (accessToken && !header) { + return MHD_HTTP_UNAUTHORIZED; + } + + const size_t size = strlen(header); + if (size < 8 || strlen(accessToken) != size - 7 || memcmp("Bearer ", header, 7) != 0) { + return MHD_HTTP_FORBIDDEN; + } + + return strncmp(accessToken, header + 7, strlen(accessToken)) == 0 ? MHD_HTTP_OK : MHD_HTTP_FORBIDDEN; +} diff --git a/src/api/HttpRequest.h b/src/api/HttpRequest.h new file mode 100644 index 00000000..f6ff9a40 --- /dev/null +++ b/src/api/HttpRequest.h @@ -0,0 +1,84 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __HTTPREQUEST_H__ +#define __HTTPREQUEST_H__ + + +#include + + +struct MHD_Connection; +struct MHD_Response; + + +namespace xmrig { + + +class HttpBody; +class HttpReply; + + +class HttpRequest +{ +public: + enum Method { + Unsupported, + Options, + Get, + Put + }; + + HttpRequest(MHD_Connection *connection, const char *url, const char *method, const char *uploadData, size_t *uploadSize, void **cls); + ~HttpRequest(); + + inline bool isFulfilled() const { return m_fulfilled; } + inline bool isRestricted() const { return m_restricted; } + inline Method method() const { return m_method; } + + bool match(const char *path) const; + bool process(const char *accessToken, bool restricted, xmrig::HttpReply &reply); + const char *body() const; + int end(const HttpReply &reply); + int end(int status, MHD_Response *rsp); + +private: + int auth(const char *accessToken); + + bool m_fulfilled; + bool m_restricted; + const char *m_uploadData; + const char *m_url; + HttpBody *m_body; + Method m_method; + MHD_Connection *m_connection; + size_t *m_uploadSize; + void **m_cls; +}; + + +} /* namespace xmrig */ + + +#endif /* __HTTPREQUEST_H__ */ diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 861086ed..45c9ce07 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -28,14 +28,64 @@ #include "api/Api.h" #include "api/Httpd.h" +#include "api/HttpReply.h" +#include "api/HttpRequest.h" #include "log/Log.h" -Httpd::Httpd(int port, const char *accessToken) : - m_accessToken(accessToken), +class UploadCtx +{ +public: + inline UploadCtx() : + m_pos(0) + {} + + + inline bool write(const char *data, size_t size) + { + if (size > (sizeof(m_data) - m_pos - 1)) { + return false; + } + + memcpy(m_data + m_pos, data, size); + + m_pos += size; + m_data[m_pos] = '\0'; + + return true; + } + + + inline const char *data() const { return m_data; } + +private: + char m_data[32768]; + size_t m_pos; +}; + + +Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) : + m_idle(true), + m_IPv6(IPv6), + m_restricted(restricted), + m_accessToken(accessToken ? strdup(accessToken) : nullptr), m_port(port), m_daemon(nullptr) { + uv_timer_init(uv_default_loop(), &m_timer); + m_timer.data = this; +} + + +Httpd::~Httpd() +{ + uv_timer_stop(&m_timer); + + if (m_daemon) { + MHD_stop_daemon(m_daemon); + } + + delete m_accessToken; } @@ -45,15 +95,14 @@ bool Httpd::start() return false; } - unsigned int flags = MHD_USE_SELECT_INTERNALLY; - + unsigned int flags = 0; # if MHD_VERSION >= 0x00093500 - if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) { - flags = MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_EPOLL_INTERNALLY_LINUX_ONLY; + if (m_IPv6 && MHD_is_feature_supported(MHD_FEATURE_IPv6)) { + flags |= MHD_USE_DUAL_STACK; } - if (MHD_is_feature_supported(MHD_FEATURE_IPv6)) { - flags |= MHD_USE_DUAL_STACK; + if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) { + flags |= MHD_USE_EPOLL_LINUX_ONLY; } # endif @@ -63,66 +112,61 @@ bool Httpd::start() return false; } + uv_timer_start(&m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval); return true; } -int Httpd::auth(const char *header) +int Httpd::process(xmrig::HttpRequest &req) { - if (!m_accessToken) { - return MHD_HTTP_OK; + xmrig::HttpReply reply; + if (!req.process(m_accessToken, m_restricted, reply)) { + return req.end(reply); } - if (m_accessToken && !header) { - return MHD_HTTP_UNAUTHORIZED; + if (!req.isFulfilled()) { + return MHD_YES; } - const size_t size = strlen(header); - if (size < 8 || strlen(m_accessToken) != size - 7 || memcmp("Bearer ", header, 7) != 0) { - return MHD_HTTP_FORBIDDEN; - } + Api::exec(req, reply); - return strncmp(m_accessToken, header + 7, strlen(m_accessToken)) == 0 ? MHD_HTTP_OK : MHD_HTTP_FORBIDDEN; + return req.end(reply); } -int Httpd::done(MHD_Connection *connection, int status, MHD_Response *rsp) +void Httpd::run() { - if (!rsp) { - rsp = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT); + MHD_run(m_daemon); + + const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS); + if (m_idle && info->num_connections) { + uv_timer_set_repeat(&m_timer, kActiveInterval); + m_idle = false; + } + else if (!m_idle && !info->num_connections) { + uv_timer_set_repeat(&m_timer, kIdleInterval); + m_idle = true; } - - MHD_add_response_header(rsp, "Content-Type", "application/json"); - MHD_add_response_header(rsp, "Access-Control-Allow-Origin", "*"); - MHD_add_response_header(rsp, "Access-Control-Allow-Methods", "GET"); - MHD_add_response_header(rsp, "Access-Control-Allow-Headers", "Authorization"); - - const int ret = MHD_queue_response(connection, status, rsp); - MHD_destroy_response(rsp); - return ret; } -int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) +int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *uploadData, size_t *uploadSize, void **con_cls) { - if (strcmp(method, "OPTIONS") == 0) { - return done(connection, MHD_HTTP_OK, nullptr); + xmrig::HttpRequest req(connection, url, method, uploadData, uploadSize, con_cls); + + if (req.method() == xmrig::HttpRequest::Options) { + return req.end(MHD_HTTP_OK, nullptr); } - if (strcmp(method, "GET") != 0) { - return MHD_NO; + if (req.method() == xmrig::HttpRequest::Unsupported) { + return req.end(MHD_HTTP_METHOD_NOT_ALLOWED, nullptr); } - int status = static_cast(cls)->auth(MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Authorization")); - if (status != MHD_HTTP_OK) { - return done(connection, status, nullptr); - } - - char *buf = Api::get(url, &status); - if (buf == nullptr) { - return MHD_NO; - } - - MHD_Response *rsp = MHD_create_response_from_buffer(strlen(buf), (void*) buf, MHD_RESPMEM_MUST_FREE); - return done(connection, status, rsp); + return static_cast(cls)->process(req); +} + + +void Httpd::onTimer(uv_timer_t *handle) +{ + static_cast(handle->data)->run(); } diff --git a/src/api/Httpd.h b/src/api/Httpd.h index 30618e2a..adec1d71 100644 --- a/src/api/Httpd.h +++ b/src/api/Httpd.h @@ -33,21 +33,38 @@ struct MHD_Daemon; struct MHD_Response; +class UploadCtx; + + +namespace xmrig { + class HttpRequest; +} + + class Httpd { public: - Httpd(int port, const char *accessToken); + Httpd(int port, const char *accessToken, bool IPv6, bool restricted); + ~Httpd(); bool start(); private: - int auth(const char *header); + constexpr static const int kIdleInterval = 200; + constexpr static const int kActiveInterval = 25; - static int done(MHD_Connection *connection, int status, MHD_Response *rsp); - static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); + int process(xmrig::HttpRequest &req); + void run(); + static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *uploadData, size_t *uploadSize, void **con_cls); + static void onTimer(uv_timer_t *handle); + + bool m_idle; + bool m_IPv6; + bool m_restricted; const char *m_accessToken; const int m_port; MHD_Daemon *m_daemon; + uv_timer_t m_timer; }; #endif /* __HTTPD_H__ */ diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp new file mode 100644 index 00000000..af8a27d5 --- /dev/null +++ b/src/core/Controller.cpp @@ -0,0 +1,125 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +//#include "core/Config.h" +//#include "core/ConfigLoader.h" +#include "core/Controller.h" +#include "log/ConsoleLog.h" +#include "log/FileLog.h" +#include "log/Log.h" +#include "Platform.h" +//#include "proxy/Proxy.h" +#include "interfaces/IControllerListener.h" + + +#ifdef HAVE_SYSLOG_H +# include "log/SysLog.h" +#endif + + +class xmrig::ControllerPrivate +{ +public: + inline ControllerPrivate() : + config(nullptr) + {} + + + inline ~ControllerPrivate() + { +// delete config; + } + + + xmrig::Config *config; + std::vector listeners; +}; + + +xmrig::Controller::Controller() + : d_ptr(new ControllerPrivate()) +{ +} + + +xmrig::Controller::~Controller() +{ +// ConfigLoader::release(); + + delete d_ptr; +} + + +xmrig::Config *xmrig::Controller::config() const +{ + return d_ptr->config; +} + + +int xmrig::Controller::init(int argc, char **argv) +{ +// d_ptr->config = xmrig::Config::load(argc, argv, this); +// if (!d_ptr->config) { +// return 1; +// } + +// Log::init(); +// Platform::init(config()->userAgent()); + +// if (!config()->background()) { +// Log::add(new ConsoleLog(this)); +// } + +// if (config()->logFile()) { +// Log::add(new FileLog(config()->logFile())); +// } + +//# ifdef HAVE_SYSLOG_H +// if (config()->syslog()) { +// Log::add(new SysLog()); +// } +//# endif + +// d_ptr->proxy = new Proxy(this); + return 0; +} + + +void xmrig::Controller::addListener(IControllerListener *listener) +{ + d_ptr->listeners.push_back(listener); +} + + +void xmrig::Controller::onNewConfig(Config *config) +{ +// xmrig::Config *previousConfig = d_ptr->config; +// d_ptr->config = config; + +// for (xmrig::IControllerListener *listener : d_ptr->listeners) { +// listener->onConfigChanged(config, previousConfig); +// } + +// delete previousConfig; +} diff --git a/src/core/Controller.h b/src/core/Controller.h new file mode 100644 index 00000000..2fe37c15 --- /dev/null +++ b/src/core/Controller.h @@ -0,0 +1,63 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CONTROLLER_H__ +#define __CONTROLLER_H__ + + +#include "interfaces/IWatcherListener.h" + + +class Proxy; +class StatsData; + + +namespace xmrig { + + +class Config; +class ControllerPrivate; +class IControllerListener; + + +class Controller : public IWatcherListener +{ +public: + Controller(); + ~Controller(); + + Config *config() const; + int init(int argc, char **argv); + Proxy *proxy() const; + void addListener(IControllerListener *listener); + +protected: + void onNewConfig(Config *config) override; + +private: + ControllerPrivate *d_ptr; +}; + +} /* namespace xmrig */ + +#endif /* __CONTROLLER_H__ */ diff --git a/src/interfaces/IControllerListener.h b/src/interfaces/IControllerListener.h new file mode 100644 index 00000000..d6077138 --- /dev/null +++ b/src/interfaces/IControllerListener.h @@ -0,0 +1,46 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __ICONTROLLERLISTENER_H__ +#define __ICONTROLLERLISTENER_H__ + + +namespace xmrig { + + +class Config; + + +class IControllerListener +{ +public: + virtual ~IControllerListener() {} + + virtual void onConfigChanged(Config *config, Config *previousConfig) = 0; +}; + + +} /* namespace xmrig */ + + +#endif // __ICONTROLLERLISTENER_H__ diff --git a/src/interfaces/IWatcherListener.h b/src/interfaces/IWatcherListener.h new file mode 100644 index 00000000..da30b1fd --- /dev/null +++ b/src/interfaces/IWatcherListener.h @@ -0,0 +1,46 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __IWATCHERLISTENER_H__ +#define __IWATCHERLISTENER_H__ + + +namespace xmrig { + + +class Config; + + +class IWatcherListener +{ +public: + virtual ~IWatcherListener() {} + + virtual void onNewConfig(Config *config) = 0; +}; + + +} /* namespace xmrig */ + + +#endif // __IWATCHERLISTENER_H__ From d24babb96eaa32d9cb346e23aa891ae802e41f99 Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 29 Mar 2018 17:15:41 +0700 Subject: [PATCH 149/389] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 962408e9..08d58966 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # XMRig -:warning: **You must update miners to version 2.5 before April 6 due [Monero PoW change](https://getmonero.org/2018/02/11/PoW-change-and-key-reuse.html).** +:warning: **You must update miners to version 2.5 before April 6 due [Monero PoW change](https://getmonero.org/2018/02/11/PoW-change-and-key-reuse.html), also check issue [#482](https://github.com/xmrig/xmrig/issues/482)** [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub release](https://img.shields.io/github/release/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) From aad19f1a35d6e05461539c866a81ee2677c7e3f8 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 31 Mar 2018 13:48:06 +0700 Subject: [PATCH 150/389] Added initial next gen config support from proxy. --- CMakeLists.txt | 12 + src/core/CommonConfig.cpp | 380 ++++++++++++++++++++++++++++++ src/core/CommonConfig.h | 106 +++++++++ src/core/Config.cpp | 329 ++++++++++++++++++++++++++ src/core/Config.h | 122 ++++++++++ src/core/ConfigCreator.h | 50 ++++ src/core/ConfigLoader.cpp | 311 ++++++++++++++++++++++++ src/core/ConfigLoader.h | 71 ++++++ src/core/ConfigLoader_platform.h | 171 ++++++++++++++ src/core/ConfigWatcher.cpp | 107 +++++++++ src/core/ConfigWatcher.h | 69 ++++++ src/core/Controller.cpp | 2 +- src/core/Controller.h | 2 +- src/donate.h | 3 +- src/interfaces/IConfig.h | 109 +++++++++ src/interfaces/IConfigCreator.h | 45 ++++ src/interfaces/IWatcherListener.h | 4 +- src/net/Url.cpp | 8 +- src/net/Url.h | 10 +- src/xmrig.h | 5 +- 20 files changed, 1901 insertions(+), 15 deletions(-) create mode 100644 src/core/CommonConfig.cpp create mode 100644 src/core/CommonConfig.h create mode 100644 src/core/Config.cpp create mode 100644 src/core/Config.h create mode 100644 src/core/ConfigCreator.h create mode 100644 src/core/ConfigLoader.cpp create mode 100644 src/core/ConfigLoader.h create mode 100644 src/core/ConfigLoader_platform.h create mode 100644 src/core/ConfigWatcher.cpp create mode 100644 src/core/ConfigWatcher.h create mode 100644 src/interfaces/IConfig.h create mode 100644 src/interfaces/IConfigCreator.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1be15601..a4347d69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,9 +15,17 @@ set(HEADERS src/api/NetworkState.h src/App.h src/Console.h + src/core/CommonConfig.h + src/core/Config.cpp + src/core/ConfigLoader.cpp + src/core/ConfigLoader.h + src/core/ConfigLoader_platform.h + src/core/ConfigWatcher.cpp src/core/Controller.h src/Cpu.h src/interfaces/IClientListener.h + src/interfaces/IConfig.h + src/interfaces/IConfigCreator.h src/interfaces/IConsoleListener.h src/interfaces/IControllerListener.h src/interfaces/IJobResultListener.h @@ -79,6 +87,10 @@ set(SOURCES src/api/NetworkState.cpp src/App.cpp src/Console.cpp + src/core/CommonConfig.cpp + src/core/Config.cpp + src/core/ConfigLoader.cpp + src/core/ConfigWatcher.cpp src/core/Controller.cpp src/log/ConsoleLog.cpp src/log/FileLog.cpp diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp new file mode 100644 index 00000000..38233b12 --- /dev/null +++ b/src/core/CommonConfig.cpp @@ -0,0 +1,380 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include +#include +#include + + +#include "core/CommonConfig.h" +#include "donate.h" +#include "log/Log.h" +#include "net/Url.h" +#include "rapidjson/document.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/prettywriter.h" +#include "xmrig.h" + + +static const char *algoNames[] = { + "cryptonight", + "cryptonight-lite", + "cryptonight-heavy" +}; + + +#if defined(_WIN32) && !defined(strcasecmp) +# define strcasecmp _stricmp +#endif + + +xmrig::CommonConfig::CommonConfig() : + m_adjusted(false), + m_apiIPv6(true), + m_apiRestricted(true), + m_background(false), + m_colors(true), + m_apiToken(nullptr), + m_apiWorkerId(nullptr), + m_fileName(nullptr), + m_logFile(nullptr), + m_userAgent(nullptr), + m_algorithm(ALGO_CRYPTONIGHT), + m_apiPort(0), + m_donateLevel(kDefaultDonateLevel), + m_printTime(60), + m_retries(5), + m_retryPause(5) +{ + m_pools.push_back(new Url()); + +# ifdef XMRIG_PROXY_PROJECT + m_retries = 2; + m_retryPause = 1; +# endif +} + + +xmrig::CommonConfig::~CommonConfig() +{ + for (Url *url : m_pools) { + delete url; + } + + m_pools.clear(); + + free(m_fileName); + free(m_apiToken); + free(m_apiWorkerId); + free(m_logFile); + free(m_userAgent); +} + + +const char *xmrig::CommonConfig::algoName() const +{ + return algoNames[m_algorithm]; +} + + +bool xmrig::CommonConfig::adjust() +{ + if (m_adjusted) { + return false; + } + + m_adjusted = true; + + for (Url *url : m_pools) { + url->adjust(algorithm()); + } + + return true; +} + + +bool xmrig::CommonConfig::isValid() const +{ + return m_pools[0]->isValid(); +} + + +bool xmrig::CommonConfig::parseBoolean(int key, bool enable) +{ + switch (key) { + case BackgroundKey: /* --background */ + m_background = enable; + break; + + case SyslogKey: /* --syslog */ + m_syslog = enable; + break; + + case KeepAliveKey: /* --keepalive */ + m_pools.back()->setKeepAlive(enable ? Url::kKeepAliveTimeout : 0); + break; + +# ifndef XMRIG_PROXY_PROJECT + case NicehashKey: /* --nicehash */ + m_pools.back()->setNicehash(enable); + break; +# endif + + case ColorKey: /* --no-color */ + m_colors = enable; + break; + + case WatchKey: /* watch */ + m_watch = enable; + break; + + case ApiIPv6Key: /* ipv6 */ + m_apiIPv6 = enable; + + case ApiRestrictedKey: /* restricted */ + m_apiRestricted = enable; + + default: + break; + } + + return true; +} + + +bool xmrig::CommonConfig::parseString(int key, const char *arg) +{ + switch (key) { + case AlgorithmKey: /* --algo */ + setAlgo(arg); + break; + + case UserpassKey: /* --userpass */ + if (!m_pools.back()->setUserpass(arg)) { + return false; + } + + break; + + case UrlKey: /* --url */ + if (m_pools.size() > 1 || m_pools[0]->isValid()) { + Url *url = new Url(arg); + if (url->isValid()) { + m_pools.push_back(url); + } + else { + delete url; + } + } + else { + m_pools[0]->parse(arg); + } + + if (!m_pools.back()->isValid()) { + return false; + } + + break; + + case UserKey: /* --user */ + m_pools.back()->setUser(arg); + break; + + case PasswordKey: /* --pass */ + m_pools.back()->setPassword(arg); + break; + + case LogFileKey: /* --log-file */ + free(m_logFile); + m_logFile = strdup(arg); + break; + + case ApiAccessTokenKey: /* --api-access-token */ + free(m_apiToken); + m_apiToken = strdup(arg); + break; + + case ApiWorkerIdKey: /* --api-worker-id */ + free(m_apiWorkerId); + m_apiWorkerId = strdup(arg); + break; + + case UserAgentKey: /* --user-agent */ + free(m_userAgent); + m_userAgent = strdup(arg); + break; + + case RetriesKey: /* --retries */ + case RetryPauseKey: /* --retry-pause */ + case VariantKey: /* --variant */ + case ApiPort: /* --api-port */ + case PrintTimeKey: /* --cpu-priority */ + return parseUint64(key, strtol(arg, nullptr, 10)); + + case BackgroundKey: /* --background */ + case SyslogKey: /* --syslog */ + case KeepAliveKey: /* --keepalive */ + case NicehashKey: /* --nicehash */ + return parseBoolean(key, true); + + case ColorKey: /* --no-color */ + case WatchKey: /* --no-watch */ + case ApiRestrictedKey: /* --api-no-restricted */ + case ApiIPv6Key: /* --api-no-ipv6 */ + return parseBoolean(key, false); + +# ifdef XMRIG_PROXY_PROJECT + case 1003: /* --donate-level */ + if (strncmp(arg, "minemonero.pro", 14) == 0) { + m_donateLevel = 0; + } + else { + parseUint64(key, strtol(arg, nullptr, 10)); + } + break; +# endif + + default: + break; + } + + return true; +} + + +bool xmrig::CommonConfig::parseUint64(int key, uint64_t arg) +{ + return parseInt(key, static_cast(arg)); +} + + +bool xmrig::CommonConfig::save() +{ + if (!m_fileName) { + return false; + } + + uv_fs_t req; + const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName, O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr); + if (fd < 0) { + return false; + } + + uv_fs_req_cleanup(&req); + + rapidjson::Document doc; + getJSON(doc); + + FILE *fp = fdopen(fd, "w"); + + char buf[4096]; + rapidjson::FileWriteStream os(fp, buf, sizeof(buf)); + rapidjson::PrettyWriter writer(os); + doc.Accept(writer); + + fclose(fp); + + uv_fs_close(uv_default_loop(), &req, fd, nullptr); + uv_fs_req_cleanup(&req); + + LOG_NOTICE("configuration saved to: \"%s\"", m_fileName); + return true; +} + + +void xmrig::CommonConfig::setFileName(const char *fileName) +{ + free(m_fileName); + m_fileName = fileName ? strdup(fileName) : nullptr; +} + + +bool xmrig::CommonConfig::parseInt(int key, int arg) +{ + switch (key) { + case RetriesKey: /* --retries */ + if (arg > 0 && arg <= 1000) { + m_retries = arg; + } + break; + + case RetryPauseKey: /* --retry-pause */ + if (arg > 0 && arg <= 3600) { + m_retryPause = arg; + } + break; + + case KeepAliveKey: /* --keepalive */ + m_pools.back()->setKeepAlive(arg); + break; + + case VariantKey: /* --variant */ + m_pools.back()->setVariant(arg); + break; + + case DonateLevelKey: /* --donate-level */ + if (arg >= kMinDonateLevel && arg <= 99) { + m_donateLevel = arg; + } + break; + + case ApiPort: /* --api-port */ + if (arg > 0 && arg <= 65536) { + m_apiPort = arg; + } + break; + + case PrintTimeKey: /* --print-time */ + if (arg >= 0 && arg <= 3600) { + m_printTime = arg; + } + break; + + default: + break; + } + + return true; +} + + +void xmrig::CommonConfig::setAlgo(const char *algo) +{ + if (strcasecmp(algo, "cryptonight-light") == 0) { + fprintf(stderr, "Algorithm \"cryptonight-light\" is deprecated, use \"cryptonight-lite\" instead\n"); + + m_algorithm = ALGO_CRYPTONIGHT_LITE; + return; + } + + const size_t size = sizeof(algoNames) / sizeof((algoNames)[0]); + + for (size_t i = 0; i < size; i++) { + if (algoNames[i] && strcasecmp(algo, algoNames[i]) == 0) { + m_algorithm = (int) i; + break; + } + } +} diff --git a/src/core/CommonConfig.h b/src/core/CommonConfig.h new file mode 100644 index 00000000..ab840a48 --- /dev/null +++ b/src/core/CommonConfig.h @@ -0,0 +1,106 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __COMMONCONFIG_H__ +#define __COMMONCONFIG_H__ + + +#include + + +#include "interfaces/IConfig.h" + + +class Url; + + +namespace xmrig { + + +class CommonConfig : public IConfig +{ +public: + CommonConfig(); + ~CommonConfig(); + + const char *algoName() const; + + inline bool isApiIPv6() const { return m_apiIPv6; } + inline bool isApiRestricted() const { return m_apiRestricted; } + inline bool isBackground() const { return m_background; } + inline bool isColors() const { return m_colors; } + inline bool isSyslog() const { return m_syslog; } + inline const char *apiToken() const { return m_apiToken; } + inline const char *apiWorkerId() const { return m_apiWorkerId; } + inline const char *logFile() const { return m_logFile; } + inline const char *userAgent() const { return m_userAgent; } + inline const std::vector &pools() const { return m_pools; } + inline int algorithm() const { return m_algorithm; } + inline int apiPort() const { return m_apiPort; } + inline int donateLevel() const { return m_donateLevel; } + inline int printTime() const { return m_printTime; } + inline int retries() const { return m_retries; } + inline int retryPause() const { return m_retryPause; } + inline void setColors(bool colors) { m_colors = colors; } + + inline bool isWatch() const override { return m_watch && m_fileName; } + inline const char *fileName() const override { return m_fileName; } + +protected: + bool adjust() override; + bool isValid() const override; + bool parseBoolean(int key, bool enable) override; + bool parseString(int key, const char *arg) override; + bool parseUint64(int key, uint64_t arg) override; + bool save() override; + void setFileName(const char *fileName) override; + + bool m_adjusted; + bool m_apiIPv6; + bool m_apiRestricted; + bool m_background; + bool m_colors; + bool m_syslog; + bool m_watch; + char *m_apiToken; + char *m_apiWorkerId; + char *m_fileName; + char *m_logFile; + char *m_userAgent; + int m_algorithm; + int m_apiPort; + int m_donateLevel; + int m_printTime; + int m_retries; + int m_retryPause; + std::vector m_pools; + +private: + bool parseInt(int key, int arg); + void setAlgo(const char *algo); +}; + + +} /* namespace xmrig */ + +#endif /* __COMMONCONFIG_H__ */ diff --git a/src/core/Config.cpp b/src/core/Config.cpp new file mode 100644 index 00000000..9bf44bc8 --- /dev/null +++ b/src/core/Config.cpp @@ -0,0 +1,329 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + + +#include "core/Config.h" +#include "core/ConfigCreator.h" +#include "core/ConfigLoader.h" +#include "Cpu.h" +#include "rapidjson/document.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/prettywriter.h" +#include "xmrig.h" + + +xmrig::Config::Config() : xmrig::CommonConfig(), + m_doubleHash(false), + m_dryRun(false), + m_hugePages(true), + m_safe(false), + m_algo(0), + m_algoVariant(0), + m_maxCpuUsage(75), + m_printTime(60), + m_priority(-1), + m_threads(0), + m_affinity(-1L) +{ + +} + + +xmrig::Config::~Config() +{ +} + + +bool xmrig::Config::reload(const char *json) +{ + return xmrig::ConfigLoader::reload(this, json); +} + + +void xmrig::Config::getJSON(rapidjson::Document &doc) const +{ + doc.SetObject(); + +// auto &allocator = doc.GetAllocator(); + +// doc.AddMember("access-log-file", accessLog() ? rapidjson::Value(rapidjson::StringRef(accessLog())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); +// doc.AddMember("algo", rapidjson::StringRef(algoName()), allocator); + +// rapidjson::Value api(rapidjson::kObjectType); +// api.AddMember("port", apiPort(), allocator); +// api.AddMember("access-token", apiToken() ? rapidjson::Value(rapidjson::StringRef(apiToken())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); +// api.AddMember("worker-id", apiWorkerId() ? rapidjson::Value(rapidjson::StringRef(apiWorkerId())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); +// api.AddMember("ipv6", isApiIPv6(), allocator); +// api.AddMember("restricted", isApiRestricted(), allocator); +// doc.AddMember("api", api, allocator); + +// doc.AddMember("background", isBackground(), allocator); + +// rapidjson::Value bind(rapidjson::kArrayType); +// for (const Addr *addr : m_addrs) { +// bind.PushBack(rapidjson::StringRef(addr->addr()), allocator); +// } + +// doc.AddMember("bind", bind, allocator); +// doc.AddMember("colors", isColors(), allocator); +// doc.AddMember("custom-diff", diff(), allocator); +// doc.AddMember("donate-level", donateLevel(), allocator); +// doc.AddMember("log-file", logFile() ? rapidjson::Value(rapidjson::StringRef(logFile())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); +// doc.AddMember("mode", rapidjson::StringRef(modeName()), allocator); + +// rapidjson::Value pools(rapidjson::kArrayType); + +// for (const Url *url : m_pools) { +// rapidjson::Value obj(rapidjson::kObjectType); + +// obj.AddMember("url", rapidjson::StringRef(url->url()), allocator); +// obj.AddMember("user", rapidjson::StringRef(url->user()), allocator); +// obj.AddMember("pass", rapidjson::StringRef(url->password()), allocator); +// obj.AddMember("coin", rapidjson::StringRef(url->coin()), allocator); + +// if (url->keepAlive() == 0 || url->keepAlive() == Url::kKeepAliveTimeout) { +// obj.AddMember("keepalive", url->keepAlive() > 0, allocator); +// } +// else { +// obj.AddMember("keepalive", url->keepAlive(), allocator); +// } + +// obj.AddMember("variant", url->variant(), allocator); + +// pools.PushBack(obj, allocator); +// } + +// doc.AddMember("pools", pools, allocator); + +// doc.AddMember("retries", retries(), allocator); +// doc.AddMember("retry-pause", retryPause(), allocator); +// doc.AddMember("reuse-timeout", reuseTimeout(), allocator); +// doc.AddMember("user-agent", userAgent() ? rapidjson::Value(rapidjson::StringRef(userAgent())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); + +//# ifdef HAVE_SYSLOG_H +// doc.AddMember("syslog", syslog(), allocator); +//# endif + +// doc.AddMember("verbose", isVerbose(), allocator); +// doc.AddMember("watch", m_watch, allocator); +// doc.AddMember("workers", isWorkers(), allocator); +} + + +xmrig::Config *xmrig::Config::load(int argc, char **argv, IWatcherListener *listener) +{ + return static_cast(ConfigLoader::load(argc, argv, new ConfigCreator(), listener)); +} + + +bool xmrig::Config::adjust() +{ + if (!CommonConfig::adjust()) { + return false; + } + + m_algoVariant = getAlgoVariant(); + if (m_algoVariant == AV2_AESNI_DOUBLE || m_algoVariant == AV4_SOFT_AES_DOUBLE) { + m_doubleHash = true; + } + + if (!m_threads) { + m_threads = Cpu::optimalThreadsCount(m_algo, m_doubleHash, m_maxCpuUsage); + } + else if (m_safe) { + const int count = Cpu::optimalThreadsCount(m_algo, m_doubleHash, m_maxCpuUsage); + if (m_threads > count) { + m_threads = count; + } + } + + return true; +} + + +bool xmrig::Config::parseBoolean(int key, bool enable) +{ + if (!CommonConfig::parseBoolean(key, enable)) { + return false; + } + + switch (key) { + case xmrig::IConfig::SafeKey: /* --safe */ + m_safe = enable; + break; + + case xmrig::IConfig::HugePagesKey: /* --no-huge-pages */ + m_hugePages = enable; + break; + + case xmrig::IConfig::DryRunKey: /* --dry-run */ + m_dryRun = enable; + break; + + default: + break; + } + + return true; +} + + +bool xmrig::Config::parseString(int key, const char *arg) +{ + if (!CommonConfig::parseString(key, arg)) { + return false; + } + + switch (key) { + case xmrig::IConfig::AVKey: /* --av */ + case xmrig::IConfig::MaxCPUUsageKey: /* --max-cpu-usage */ + case xmrig::IConfig::CPUPriorityKey: /* --cpu-priority */ + return parseUint64(key, strtol(arg, nullptr, 10)); + + case xmrig::IConfig::SafeKey: /* --safe */ + case xmrig::IConfig::DryRunKey: /* --dry-run */ + return parseBoolean(key, true); + + case xmrig::IConfig::HugePagesKey: /* --no-huge-pages */ + return parseBoolean(key, false); + + case xmrig::IConfig::ThreadsKey: /* --threads */ + if (strncmp(arg, "all", 3) == 0) { + m_threads = Cpu::threads(); + return true; + } + + return parseUint64(key, strtol(arg, nullptr, 10)); + + case xmrig::IConfig::CPUAffinityKey: /* --cpu-affinity */ + { + const char *p = strstr(arg, "0x"); + return parseUint64(key, p ? strtoull(p, nullptr, 16) : strtoull(arg, nullptr, 10)); + } + + default: + break; + } + + return true; +} + + +bool xmrig::Config::parseUint64(int key, uint64_t arg) +{ + if (!CommonConfig::parseUint64(key, arg)) { + return false; + } + + switch (key) { + case xmrig::IConfig::CPUAffinityKey: /* --cpu-affinity */ + if (arg) { + m_affinity = arg; + } + break; + + default: + return parseInt(key, static_cast(arg)); + } + + return true; +} + + +void xmrig::Config::parseJSON(const rapidjson::Document &doc) +{ +} + + +bool xmrig::Config::parseInt(int key, int arg) +{ + switch (key) { + case xmrig::IConfig::ThreadsKey: /* --threads */ + if (m_threads >= 0 && arg < 1024) { + m_threads = arg; + } + break; + + case xmrig::IConfig::AVKey: /* --av */ + if (arg >= AV0_AUTO && arg < AV_MAX) { + m_algoVariant = arg; + } + break; + + case xmrig::IConfig::MaxCPUUsageKey: /* --max-cpu-usage */ + if (m_maxCpuUsage > 0 && arg <= 100) { + m_maxCpuUsage = arg; + } + break; + + case xmrig::IConfig::CPUPriorityKey: /* --cpu-priority */ + if (arg >= 0 && arg <= 5) { + m_priority = arg; + } + break; + + default: + break; + } + + return true; +} + + +int xmrig::Config::getAlgoVariant() const +{ +# ifndef XMRIG_NO_AEON + if (m_algo == xmrig::ALGO_CRYPTONIGHT_LITE) { + return getAlgoVariantLite(); + } +# endif + + if (m_algoVariant <= AV0_AUTO || m_algoVariant >= AV_MAX) { + return Cpu::hasAES() ? AV1_AESNI : AV3_SOFT_AES; + } + + if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV2_AESNI_DOUBLE) { + return m_algoVariant + 2; + } + + return m_algoVariant; +} + + +#ifndef XMRIG_NO_AEON +int xmrig::Config::getAlgoVariantLite() const +{ + if (m_algoVariant <= AV0_AUTO || m_algoVariant >= AV_MAX) { + return Cpu::hasAES() ? AV2_AESNI_DOUBLE : AV4_SOFT_AES_DOUBLE; + } + + if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV2_AESNI_DOUBLE) { + return m_algoVariant + 2; + } + + return m_algoVariant; +} +#endif diff --git a/src/core/Config.h b/src/core/Config.h new file mode 100644 index 00000000..a10cd696 --- /dev/null +++ b/src/core/Config.h @@ -0,0 +1,122 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CONFIG_H__ +#define __CONFIG_H__ + + +#include +#include + + +#include "rapidjson/fwd.h" +#include "core/CommonConfig.h" + + +class Addr; +class Url; + + +namespace xmrig { + + +class ConfigLoader; +class IWatcherListener; + + +/** + * @brief The Config class + * + * Options with dynamic reload: + * colors + * debug + * verbose + * custom-diff (only for new connections) + * api/worker-id + * pools/ + */ +class Config : public CommonConfig +{ + friend class ConfigLoader; + +public: + enum AlgoVariant { + AV0_AUTO, + AV1_AESNI, + AV2_AESNI_DOUBLE, + AV3_SOFT_AES, + AV4_SOFT_AES_DOUBLE, + AV_MAX + }; + + Config(); + ~Config(); + + bool reload(const char *json); + + void getJSON(rapidjson::Document &doc) const override; + + inline bool isDoubleHash() const { return m_doubleHash; } + inline bool isDryRun() const { return m_dryRun; } + inline bool isHugePages() const { return m_hugePages; } + inline int algo() const { return m_algo; } + inline int algoVariant() const { return m_algoVariant; } + inline int printTime() const { return m_printTime; } + inline int priority() const { return m_priority; } + inline int threads() const { return m_threads; } + inline int64_t affinity() const { return m_affinity; } + + static Config *load(int argc, char **argv, IWatcherListener *listener); + +protected: + bool adjust() override; + bool parseBoolean(int key, bool enable) override; + bool parseString(int key, const char *arg) override; + bool parseUint64(int key, uint64_t arg) override; + void parseJSON(const rapidjson::Document &doc) override; + +private: + bool parseInt(int key, int arg); + + int getAlgoVariant() const; +# ifndef XMRIG_NO_AEON + int getAlgoVariantLite() const; +# endif + + bool m_doubleHash; + bool m_dryRun; + bool m_hugePages; + bool m_safe; + int m_algo; + int m_algoVariant; + int m_maxCpuUsage; + int m_printTime; + int m_priority; + int m_threads; + int64_t m_affinity; +}; + + +} /* namespace xmrig */ + +#endif /* __CONFIG_H__ */ diff --git a/src/core/ConfigCreator.h b/src/core/ConfigCreator.h new file mode 100644 index 00000000..fcc6c596 --- /dev/null +++ b/src/core/ConfigCreator.h @@ -0,0 +1,50 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2018 XMRig + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CONFIGCREATOR_H__ +#define __CONFIGCREATOR_H__ + + +#include "core/Config.h" +#include "interfaces/IConfigCreator.h" + + +namespace xmrig { + + +class IConfig; + + +class ConfigCreator : public IConfigCreator +{ +public: + inline IConfig *create() const override + { + return new Config(); + } +}; + + +} /* namespace xmrig */ + + +#endif // __CONFIGCREATOR_H__ diff --git a/src/core/ConfigLoader.cpp b/src/core/ConfigLoader.cpp new file mode 100644 index 00000000..1dec4eca --- /dev/null +++ b/src/core/ConfigLoader.cpp @@ -0,0 +1,311 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include +#include + + +#ifndef XMRIG_NO_HTTPD +# include +#endif + + +#include "core/ConfigCreator.h" +#include "core/ConfigLoader.h" +#include "core/ConfigLoader_platform.h" +#include "core/ConfigWatcher.h" +#include "interfaces/IConfig.h" +#include "interfaces/IWatcherListener.h" +#include "net/Url.h" +#include "Platform.h" +#include "rapidjson/document.h" +#include "rapidjson/error/en.h" +#include "rapidjson/filereadstream.h" + + +xmrig::ConfigWatcher *xmrig::ConfigLoader::m_watcher = nullptr; +xmrig::IConfigCreator *xmrig::ConfigLoader::m_creator = nullptr; +xmrig::IWatcherListener *xmrig::ConfigLoader::m_listener = nullptr; + + +#ifndef ARRAY_SIZE +# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +#endif + + +bool xmrig::ConfigLoader::loadFromFile(xmrig::IConfig *config, const char *fileName) +{ + rapidjson::Document doc; + if (!getJSON(fileName, doc)) { + return false; + } + + config->setFileName(fileName); + + return loadFromJSON(config, doc); +} + + +bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const char *json) +{ + rapidjson::Document doc; + doc.Parse(json); + + if (doc.HasParseError() || !doc.IsObject()) { + return false; + } + + return loadFromJSON(config, doc); +} + + +bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const rapidjson::Document &doc) +{ + for (size_t i = 0; i < ARRAY_SIZE(config_options); i++) { + parseJSON(config, &config_options[i], doc); + } + + const rapidjson::Value &pools = doc["pools"]; + if (pools.IsArray()) { + for (const rapidjson::Value &value : pools.GetArray()) { + if (!value.IsObject()) { + continue; + } + + for (size_t i = 0; i < ARRAY_SIZE(pool_options); i++) { + parseJSON(config, &pool_options[i], value); + } + } + } + + const rapidjson::Value &api = doc["api"]; + if (api.IsObject()) { + for (size_t i = 0; i < ARRAY_SIZE(api_options); i++) { + parseJSON(config, &api_options[i], api); + } + } + + config->parseJSON(doc); + config->adjust(); + + return config->isValid(); +} + + +bool xmrig::ConfigLoader::reload(xmrig::IConfig *oldConfig, const char *json) +{ + xmrig::IConfig *config = m_creator->create(); + if (!loadFromJSON(config, json)) { + delete config; + + return false; + } + + config->setFileName(oldConfig->fileName()); + const bool saved = config->save(); + + if (config->isWatch() && m_watcher && saved) { + delete config; + + return true; + } + + m_listener->onNewConfig(config); + return true; +} + + +xmrig::IConfig *xmrig::ConfigLoader::load(int argc, char **argv, IConfigCreator *creator, IWatcherListener *listener) +{ + m_creator = creator; + m_listener = listener; + + xmrig::IConfig *config = m_creator->create(); + int key; + + while (1) { + key = getopt_long(argc, argv, short_options, options, NULL); + if (key < 0) { + break; + } + + if (!parseArg(config, key, optarg)) { + delete config; + return nullptr; + } + } + + if (optind < argc) { + fprintf(stderr, "%s: unsupported non-option argument '%s'\n", argv[0], argv[optind]); + delete config; + return nullptr; + } + + if (!config->isValid()) { + loadFromFile(config, Platform::defaultConfigName()); + } + + if (!config->isValid()) { + fprintf(stderr, "No pool URL supplied. Exiting.\n"); + delete config; + return nullptr; + } + + if (config->isWatch()) { + m_watcher = new xmrig::ConfigWatcher(config->fileName(), creator, listener); + } + + config->adjust(); + return config; +} + + +void xmrig::ConfigLoader::release() +{ + delete m_watcher; + delete m_creator; + + m_watcher = nullptr; + m_creator = nullptr; +} + + +bool xmrig::ConfigLoader::getJSON(const char *fileName, rapidjson::Document &doc) +{ + uv_fs_t req; + const int fd = uv_fs_open(uv_default_loop(), &req, fileName, O_RDONLY, 0644, nullptr); + if (fd < 0) { + fprintf(stderr, "unable to open %s: %s\n", fileName, uv_strerror(fd)); + return false; + } + + uv_fs_req_cleanup(&req); + + FILE *fp = fdopen(fd, "rb"); + char buf[8192]; + rapidjson::FileReadStream is(fp, buf, sizeof(buf)); + + doc.ParseStream(is); + + uv_fs_close(uv_default_loop(), &req, fd, nullptr); + uv_fs_req_cleanup(&req); + + if (doc.HasParseError()) { + printf("%s<%d>: %s\n", fileName, (int) doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError())); + return false; + } + + return doc.IsObject(); +} + + +bool xmrig::ConfigLoader::parseArg(xmrig::IConfig *config, int key, const char *arg) +{ + switch (key) { + case xmrig::IConfig::VersionKey: /* --version */ + showVersion(); + return false; + + case xmrig::IConfig::HelpKey: /* --help */ + showUsage(); + return false; + + case xmrig::IConfig::ConfigKey: /* --config */ + loadFromFile(config, arg); + break; + + default: + return config->parseString(key, arg);; + } + + return true; +} + + +void xmrig::ConfigLoader::parseJSON(xmrig::IConfig *config, const struct option *option, const rapidjson::Value &object) +{ + if (!option->name || !object.HasMember(option->name)) { + return; + } + + const rapidjson::Value &value = object[option->name]; + + if (option->has_arg) { + if (value.IsString()) { + config->parseString(option->val, value.GetString()); + } + else if (value.IsInt64()) { + config->parseUint64(option->val, value.GetUint64()); + } + else if (value.IsBool()) { + config->parseBoolean(option->val, value.IsTrue()); + } + } + else if (value.IsBool()) { + config->parseBoolean(option->val, value.IsTrue()); + } +} + + +void xmrig::ConfigLoader::showUsage() +{ + printf(usage); +} + + +void xmrig::ConfigLoader::showVersion() +{ + printf(APP_NAME " " APP_VERSION "\n built on " __DATE__ + +# if defined(__clang__) + " with clang " __clang_version__); +# elif defined(__GNUC__) + " with GCC"); + printf(" %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); +# elif defined(_MSC_VER) + " with MSVC"); + printf(" %d", MSVC_VERSION); +# else + ); +# endif + + printf("\n features:" +# if defined(__i386__) || defined(_M_IX86) + " i386" +# elif defined(__x86_64__) || defined(_M_AMD64) + " x86_64" +# endif + +# if defined(__AES__) || defined(_MSC_VER) + " AES-NI" +# endif + "\n"); + + printf("\nlibuv/%s\n", uv_version_string()); + +# ifndef XMRIG_NO_HTTPD + printf("libmicrohttpd/%s\n", MHD_get_version()); +# endif +} diff --git a/src/core/ConfigLoader.h b/src/core/ConfigLoader.h new file mode 100644 index 00000000..64638af3 --- /dev/null +++ b/src/core/ConfigLoader.h @@ -0,0 +1,71 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CONFIGLOADER_H__ +#define __CONFIGLOADER_H__ + + +#include + + +#include "rapidjson/fwd.h" + + +struct option; + + +namespace xmrig { + + +class ConfigWatcher; +class IConfigCreator; +class IWatcherListener; +class IConfig; + + +class ConfigLoader +{ +public: + static bool loadFromFile(IConfig *config, const char *fileName); + static bool loadFromJSON(IConfig *config, const char *json); + static bool loadFromJSON(IConfig *config, const rapidjson::Document &doc); + static bool reload(IConfig *oldConfig, const char *json); + static IConfig *load(int argc, char **argv, IConfigCreator *creator, IWatcherListener *listener); + static void release(); + +private: + static bool getJSON(const char *fileName, rapidjson::Document &doc); + static bool parseArg(IConfig *config, int key, const char *arg); + static void parseJSON(IConfig *config, const struct option *option, const rapidjson::Value &object); + static void showUsage(); + static void showVersion(); + + static ConfigWatcher *m_watcher; + static IConfigCreator *m_creator; + static IWatcherListener *m_listener; +}; + + +} /* namespace xmrig */ + +#endif /* __CONFIGLOADER_H__ */ diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h new file mode 100644 index 00000000..59415f78 --- /dev/null +++ b/src/core/ConfigLoader_platform.h @@ -0,0 +1,171 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CONFIGLOADER_PLATFORM_H__ +#define __CONFIGLOADER_PLATFORM_H__ + + +#ifdef _MSC_VER +# include "getopt/getopt.h" +#else +# include +#endif + + +#include "version.h" +#include "interfaces/IConfig.h" + + +namespace xmrig { + + +static char const usage[] = "\ +Usage: " APP_ID " [OPTIONS]\n\ +Options:\n\ + -a, --algo=ALGO cryptonight (default) or cryptonight-lite\n\ + -o, --url=URL URL of mining server\n\ + -O, --userpass=U:P username:password pair for mining server\n\ + -u, --user=USERNAME username for mining server\n\ + -p, --pass=PASSWORD password for mining server\n\ + -t, --threads=N number of miner threads\n\ + -v, --av=N algorithm variation, 0 auto select\n\ + -k, --keepalive send keepalived for prevent timeout (need pool support)\n\ + -r, --retries=N number of times to retry before switch to backup server (default: 5)\n\ + -R, --retry-pause=N time to pause between retries (default: 5)\n\ + --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1\n\ + --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ + --no-huge-pages disable huge pages support\n\ + --no-color disable colored output\n\ + --variant algorithm PoW variant\n\ + --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ + --user-agent set custom user-agent string for pool\n\ + -B, --background run the miner in the background\n\ + -c, --config=FILE load a JSON-format configuration file\n\ + -l, --log-file=FILE log all output to a file\n" +# ifdef HAVE_SYSLOG_H +"\ + -S, --syslog use system log for output messages\n" +# endif +"\ + --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75)\n\ + --safe safe adjust threads and av settings for current CPU\n\ + --nicehash enable nicehash/xmrig-proxy support\n\ + --print-time=N print hashrate report every N seconds\n\ + --api-port=N port for the miner API\n\ + --api-access-token=T access token for API\n\ + --api-worker-id=ID custom worker-id for API\n\ + -h, --help display this help and exit\n\ + -V, --version output version information and exit\n\ +"; + + +static char const short_options[] = "a:c:khBp:Px:r:R:s:t:T:o:u:O:v:Vl:S"; + + +static struct option const options[] = { + { "algo", 1, nullptr, xmrig::IConfig::AlgorithmKey }, + { "api-access-token", 1, nullptr, xmrig::IConfig::ApiAccessTokenKey }, + { "api-port", 1, nullptr, xmrig::IConfig::ApiPort }, + { "api-worker-id", 1, nullptr, xmrig::IConfig::ApiWorkerIdKey }, + { "api-no-ipv6", 0, nullptr, xmrig::IConfig::ApiIPv6Key }, + { "api-no-restricted", 0, nullptr, xmrig::IConfig::ApiRestrictedKey }, + { "av", 1, nullptr, xmrig::IConfig::AVKey }, + { "background", 0, nullptr, xmrig::IConfig::BackgroundKey }, + { "config", 1, nullptr, xmrig::IConfig::ConfigKey }, + { "cpu-affinity", 1, nullptr, xmrig::IConfig::CPUAffinityKey }, + { "cpu-priority", 1, nullptr, xmrig::IConfig::CPUPriorityKey }, + { "donate-level", 1, nullptr, xmrig::IConfig::DonateLevelKey }, + { "dry-run", 0, nullptr, xmrig::IConfig::DryRunKey }, + { "help", 0, nullptr, xmrig::IConfig::HelpKey }, + { "keepalive", 0, nullptr, xmrig::IConfig::KeepAliveKey }, + { "log-file", 1, nullptr, xmrig::IConfig::LogFileKey }, + { "max-cpu-usage", 1, nullptr, xmrig::IConfig::MaxCPUUsageKey }, + { "nicehash", 0, nullptr, xmrig::IConfig::NicehashKey }, + { "no-color", 0, nullptr, xmrig::IConfig::ColorKey }, + { "no-huge-pages", 0, nullptr, xmrig::IConfig::HugePagesKey }, + { "variant", 1, nullptr, xmrig::IConfig::VariantKey }, + { "pass", 1, nullptr, xmrig::IConfig::PasswordKey }, + { "print-time", 1, nullptr, xmrig::IConfig::PrintTimeKey }, + { "retries", 1, nullptr, xmrig::IConfig::RetriesKey }, + { "retry-pause", 1, nullptr, xmrig::IConfig::RetryPauseKey }, + { "safe", 0, nullptr, xmrig::IConfig::SafeKey }, + { "syslog", 0, nullptr, xmrig::IConfig::SyslogKey }, + { "threads", 1, nullptr, xmrig::IConfig::ThreadsKey }, + { "url", 1, nullptr, xmrig::IConfig::UrlKey }, + { "user", 1, nullptr, xmrig::IConfig::UserKey }, + { "user-agent", 1, nullptr, xmrig::IConfig::UserAgentKey }, + { "userpass", 1, nullptr, xmrig::IConfig::UserpassKey }, + { "version", 0, nullptr, xmrig::IConfig::VersionKey }, + { 0, 0, 0, 0 } +}; + + +static struct option const config_options[] = { + { "algo", 1, nullptr, xmrig::IConfig::AlgorithmKey }, + { "av", 1, nullptr, xmrig::IConfig::AVKey }, + { "background", 0, nullptr, xmrig::IConfig::BackgroundKey }, + { "colors", 0, nullptr, xmrig::IConfig::ColorKey }, + { "cpu-affinity", 1, nullptr, xmrig::IConfig::CPUAffinityKey }, + { "cpu-priority", 1, nullptr, xmrig::IConfig::CPUPriorityKey }, + { "donate-level", 1, nullptr, xmrig::IConfig::DonateLevelKey }, + { "dry-run", 0, nullptr, xmrig::IConfig::DryRunKey }, + { "huge-pages", 0, nullptr, xmrig::IConfig::HugePagesKey }, + { "log-file", 1, nullptr, xmrig::IConfig::LogFileKey }, + { "max-cpu-usage", 1, nullptr, xmrig::IConfig::MaxCPUUsageKey }, + { "print-time", 1, nullptr, xmrig::IConfig::PrintTimeKey }, + { "retries", 1, nullptr, xmrig::IConfig::RetriesKey }, + { "retry-pause", 1, nullptr, xmrig::IConfig::RetryPauseKey }, + { "safe", 0, nullptr, xmrig::IConfig::SafeKey }, + { "syslog", 0, nullptr, xmrig::IConfig::SyslogKey }, + { "threads", 1, nullptr, xmrig::IConfig::SafeKey }, + { "user-agent", 1, nullptr, xmrig::IConfig::UserAgentKey }, + { 0, 0, 0, 0 } +}; + + +static struct option const pool_options[] = { + { "url", 1, nullptr, xmrig::IConfig::UrlKey }, + { "pass", 1, nullptr, xmrig::IConfig::PasswordKey }, + { "user", 1, nullptr, xmrig::IConfig::UserKey }, + { "userpass", 1, nullptr, xmrig::IConfig::UserpassKey }, + { "nicehash", 0, nullptr, xmrig::IConfig::NicehashKey }, + { "keepalive", 2, nullptr, xmrig::IConfig::KeepAliveKey }, + { "variant", 1, nullptr, xmrig::IConfig::VariantKey }, + { 0, 0, 0, 0 } +}; + + +static struct option const api_options[] = { + { "port", 1, nullptr, xmrig::IConfig::ApiPort }, + { "access-token", 1, nullptr, xmrig::IConfig::ApiAccessTokenKey }, + { "worker-id", 1, nullptr, xmrig::IConfig::ApiWorkerIdKey }, + { "ipv6", 0, nullptr, xmrig::IConfig::ApiIPv6Key }, + { "restricted", 0, nullptr, xmrig::IConfig::ApiRestrictedKey }, + { 0, 0, 0, 0 } +}; + + +} /* namespace xmrig */ + +#endif /* __CONFIGLOADER_PLATFORM_H__ */ diff --git a/src/core/ConfigWatcher.cpp b/src/core/ConfigWatcher.cpp new file mode 100644 index 00000000..b934b52b --- /dev/null +++ b/src/core/ConfigWatcher.cpp @@ -0,0 +1,107 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include + + +#include "core/ConfigCreator.h" +#include "core/ConfigLoader.h" +#include "core/ConfigWatcher.h" +#include "interfaces/IWatcherListener.h" +#include "log/Log.h" + + +xmrig::ConfigWatcher::ConfigWatcher(const char *path, IConfigCreator *creator, IWatcherListener *listener) : + m_path(strdup(path)), + m_creator(creator), + m_listener(listener) +{ + uv_fs_event_init(uv_default_loop(), &m_fsEvent); + uv_timer_init(uv_default_loop(), &m_timer); + + m_fsEvent.data = m_timer.data = this; + + start(); +} + + +xmrig::ConfigWatcher::~ConfigWatcher() +{ + uv_timer_stop(&m_timer); + uv_fs_event_stop(&m_fsEvent); + + free(m_path); +} + + +void xmrig::ConfigWatcher::onTimer(uv_timer_t* handle) +{ + static_cast(handle->data)->reload(); +} + + +void xmrig::ConfigWatcher::onFsEvent(uv_fs_event_t* handle, const char *filename, int events, int status) +{ + if (!filename) { + return; + } + + static_cast(handle->data)->queueUpdate(); +} + + +void xmrig::ConfigWatcher::queueUpdate() +{ + uv_timer_stop(&m_timer); + uv_timer_start(&m_timer, xmrig::ConfigWatcher::onTimer, kDelay, 0); +} + + +void xmrig::ConfigWatcher::reload() +{ + LOG_WARN("\"%s\" was changed, reloading configuration", m_path); + + IConfig *config = m_creator->create(); + ConfigLoader::loadFromFile(config, m_path); + + if (!config->isValid()) { + LOG_ERR("reloading failed"); + + delete config; + return; + } + + m_listener->onNewConfig(config); + +# ifndef _WIN32 + uv_fs_event_stop(&m_fsEvent); + start(); +# endif +} + + +void xmrig::ConfigWatcher::start() +{ + uv_fs_event_start(&m_fsEvent, xmrig::ConfigWatcher::onFsEvent, m_path, 0); +} diff --git a/src/core/ConfigWatcher.h b/src/core/ConfigWatcher.h new file mode 100644 index 00000000..74526e90 --- /dev/null +++ b/src/core/ConfigWatcher.h @@ -0,0 +1,69 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CONFIGWATCHER_H__ +#define __CONFIGWATCHER_H__ + + +#include +#include + +#include "rapidjson/fwd.h" + + +struct option; + + +namespace xmrig { + + +class IConfigCreator; +class IWatcherListener; + + +class ConfigWatcher +{ +public: + ConfigWatcher(const char *path, IConfigCreator *creator, IWatcherListener *listener); + ~ConfigWatcher(); + +private: + constexpr static int kDelay = 500; + + static void onFsEvent(uv_fs_event_t* handle, const char *filename, int events, int status); + static void onTimer(uv_timer_t* handle); + void queueUpdate(); + void reload(); + void start(); + + char *m_path; + IConfigCreator *m_creator; + IWatcherListener *m_listener; + uv_fs_event_t m_fsEvent; + uv_timer_t m_timer; +}; + + +} /* namespace xmrig */ + +#endif /* __CONFIGWATCHER_H__ */ diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index af8a27d5..a5ffe236 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -112,7 +112,7 @@ void xmrig::Controller::addListener(IControllerListener *listener) } -void xmrig::Controller::onNewConfig(Config *config) +void xmrig::Controller::onNewConfig(IConfig *config) { // xmrig::Config *previousConfig = d_ptr->config; // d_ptr->config = config; diff --git a/src/core/Controller.h b/src/core/Controller.h index 2fe37c15..9144cca7 100644 --- a/src/core/Controller.h +++ b/src/core/Controller.h @@ -52,7 +52,7 @@ public: void addListener(IControllerListener *listener); protected: - void onNewConfig(Config *config) override; + void onNewConfig(IConfig *config) override; private: ControllerPrivate *d_ptr; diff --git a/src/donate.h b/src/donate.h index e8230b87..bdf7a00d 100644 --- a/src/donate.h +++ b/src/donate.h @@ -38,7 +38,8 @@ * XMR: 48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD * BTC: 1P7ujsXeX7GxQwHNnJsRMgAdNkFZmNVqJT */ -constexpr const int kDonateLevel = 5; +constexpr const int kDefaultDonateLevel = 5; +constexpr const int kMinDonateLevel = 1; #endif /* __DONATE_H__ */ diff --git a/src/interfaces/IConfig.h b/src/interfaces/IConfig.h new file mode 100644 index 00000000..89aa6217 --- /dev/null +++ b/src/interfaces/IConfig.h @@ -0,0 +1,109 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2018 XMRig + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __ICONFIG_H__ +#define __ICONFIG_H__ + + +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class IConfig +{ +public: + enum Keys { + // common + AlgorithmKey = 'a', + ApiPort = 4000, + ApiAccessTokenKey = 4001, + ApiWorkerIdKey = 4002, + ApiIPv6Key = 4003, + ApiRestrictedKey = 4004, + BackgroundKey = 'B', + ConfigKey = 'c', + DonateLevelKey = 1003, + HelpKey = 'h', + KeepAliveKey = 'k', + LogFileKey = 'l', + ColorKey = 1002, + WatchKey = 1105, + PasswordKey = 'p', + RetriesKey = 'r', + RetryPauseKey = 'R', + SyslogKey = 'S', + UrlKey = 'o', + UserKey = 'u', + UserAgentKey = 1008, + UserpassKey = 'O', + VerboseKey = 1100, + VersionKey = 'V', + VariantKey = 1010, + + // xmrig common + CPUPriorityKey = 1021, + NicehashKey = 1006, + PrintTimeKey = 1007, + + // xmrig cpu + AVKey = 'v', + CPUAffinityKey = 1020, + DryRunKey = 5000, + HugePagesKey = 1009, + MaxCPUUsageKey = 1004, + SafeKey = 1005, + ThreadsKey = 't', + + // xmrig-proxy + AccessLogFileKey = 'A', + BindKey = 'b', + CoinKey = 1104, + CustomDiffKey = 1102, + DebugKey = 1101, + ModeKey = 'm', + PoolCoinKey = 'C', + ReuseTimeoutKey = 1106, + WorkersKey = 1103, + }; + + virtual ~IConfig() {} + + virtual bool adjust() = 0; + virtual bool isValid() const = 0; + virtual bool isWatch() const = 0; + virtual bool parseBoolean(int key, bool enable) = 0; + virtual bool parseString(int key, const char *arg) = 0; + virtual bool parseUint64(int key, uint64_t arg) = 0; + virtual bool save() = 0; + virtual const char *fileName() const = 0; + virtual void getJSON(rapidjson::Document &doc) const = 0; + virtual void parseJSON(const rapidjson::Document &doc) = 0; + virtual void setFileName(const char *fileName) = 0; +}; + + +} /* namespace xmrig */ + + +#endif // __ICONFIG_H__ diff --git a/src/interfaces/IConfigCreator.h b/src/interfaces/IConfigCreator.h new file mode 100644 index 00000000..597a6b74 --- /dev/null +++ b/src/interfaces/IConfigCreator.h @@ -0,0 +1,45 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2018 XMRig + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __ICONFIGCREATOR_H__ +#define __ICONFIGCREATOR_H__ + + +namespace xmrig { + + +class IConfig; + + +class IConfigCreator +{ +public: + virtual ~IConfigCreator() {} + + virtual IConfig *create() const = 0; +}; + + +} /* namespace xmrig */ + + +#endif // __ICONFIGCREATOR_H__ diff --git a/src/interfaces/IWatcherListener.h b/src/interfaces/IWatcherListener.h index da30b1fd..bfafb9a0 100644 --- a/src/interfaces/IWatcherListener.h +++ b/src/interfaces/IWatcherListener.h @@ -28,7 +28,7 @@ namespace xmrig { -class Config; +class IConfig; class IWatcherListener @@ -36,7 +36,7 @@ class IWatcherListener public: virtual ~IWatcherListener() {} - virtual void onNewConfig(Config *config) = 0; + virtual void onNewConfig(IConfig *config) = 0; }; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index c17ef690..8905e919 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -37,12 +37,12 @@ Url::Url() : - m_keepAlive(false), m_nicehash(false), m_host(nullptr), m_password(nullptr), m_user(nullptr), m_algo(xmrig::ALGO_CRYPTONIGHT), + m_keepAlive(0), m_variant(xmrig::VARIANT_AUTO), m_url(nullptr), m_port(kDefaultPort) @@ -62,12 +62,12 @@ Url::Url() : * @param url */ Url::Url(const char *url) : - m_keepAlive(false), m_nicehash(false), m_host(nullptr), m_password(nullptr), m_user(nullptr), m_algo(xmrig::ALGO_CRYPTONIGHT), + m_keepAlive(0), m_variant(xmrig::VARIANT_AUTO), m_url(nullptr), m_port(kDefaultPort) @@ -76,12 +76,12 @@ Url::Url(const char *url) : } -Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash, int variant) : - m_keepAlive(keepAlive), +Url::Url(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, int variant) : m_nicehash(nicehash), m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), m_algo(xmrig::ALGO_CRYPTONIGHT), + m_keepAlive(keepAlive), m_variant(variant), m_url(nullptr), m_port(port) diff --git a/src/net/Url.h b/src/net/Url.h index f861fec5..45db4457 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -34,22 +34,24 @@ public: constexpr static const char *kDefaultPassword = "x"; constexpr static const char *kDefaultUser = "x"; constexpr static uint16_t kDefaultPort = 3333; + constexpr static int kKeepAliveTimeout = 60; Url(); Url(const char *url); - Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false, int variant = -1); + Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, int keepAlive = 0, bool nicehash = false, int variant = -1); ~Url(); - inline bool isKeepAlive() const { return m_keepAlive; } + inline bool isKeepAlive() const { return m_keepAlive > 0; } // FIXME: replace isKeepAlive to keepAlive inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_host && m_port > 0; } inline const char *host() const { return m_host; } inline const char *password() const { return m_password ? m_password : kDefaultPassword; } inline const char *user() const { return m_user ? m_user : kDefaultUser; } inline int algo() const { return m_algo; } + inline int keepAlive() const { return m_keepAlive; } inline int variant() const { return m_variant; } inline uint16_t port() const { return m_port; } - inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; } + inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setVariant(bool monero) { m_variant = monero; } @@ -67,12 +69,12 @@ public: private: bool parseIPv6(const char *addr); - bool m_keepAlive; bool m_nicehash; char *m_host; char *m_password; char *m_user; int m_algo; + int m_keepAlive; int m_variant; mutable char *m_url; uint16_t m_port; diff --git a/src/xmrig.h b/src/xmrig.h index 103e8a68..9cae73c7 100644 --- a/src/xmrig.h +++ b/src/xmrig.h @@ -30,8 +30,9 @@ namespace xmrig enum Algo { - ALGO_CRYPTONIGHT, /* CryptoNight (Monero) */ - ALGO_CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ + ALGO_CRYPTONIGHT, /* CryptoNight (Monero) */ + ALGO_CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ + ALGO_CRYPTONIGHT_HEAVY, /* CryptoNight-Heavy (SUMO) */ }; From aac7b0404ae7180e5654f5ab839d0e2af07a3dc5 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 31 Mar 2018 16:29:47 +0700 Subject: [PATCH 151/389] Options class replaced to xmrig::Config. --- CMakeLists.txt | 2 - src/App.cpp | 84 ++-- src/App.h | 2 - src/App_win.cpp | 14 +- src/Mem.cpp | 1 - src/Mem_win.cpp | 1 - src/Options.cpp | 761 ------------------------------------- src/Options.h | 133 ------- src/Summary.cpp | 69 ++-- src/Summary.h | 11 +- src/api/ApiRouter.cpp | 21 +- src/core/Config.cpp | 7 +- src/core/Config.h | 2 - src/core/Controller.cpp | 83 ++-- src/core/Controller.h | 4 +- src/crypto/CryptoNight.cpp | 9 +- src/crypto/CryptoNight.h | 4 +- src/log/ConsoleLog.cpp | 26 +- src/log/ConsoleLog.h | 13 +- src/net/Network.cpp | 37 +- src/net/Network.h | 11 +- src/workers/Hashrate.cpp | 17 +- src/workers/Hashrate.h | 12 +- src/workers/Workers.cpp | 9 +- src/workers/Workers.h | 11 +- 25 files changed, 240 insertions(+), 1104 deletions(-) delete mode 100644 src/Options.cpp delete mode 100644 src/Options.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a4347d69..28d89a85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,6 @@ set(HEADERS src/net/strategies/SinglePoolStrategy.h src/net/SubmitResult.h src/net/Url.h - src/Options.h src/Platform.h src/Summary.h src/version.h @@ -104,7 +103,6 @@ set(SOURCES src/net/strategies/SinglePoolStrategy.cpp src/net/SubmitResult.cpp src/net/Url.cpp - src/Options.cpp src/Platform.cpp src/Summary.cpp src/workers/DoubleWorker.cpp diff --git a/src/App.cpp b/src/App.cpp index ed425f90..612614ae 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -29,6 +29,7 @@ #include "api/Api.h" #include "App.h" #include "Console.h" +#include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" #include "crypto/CryptoNight.h" @@ -37,17 +38,12 @@ #include "log/Log.h" #include "Mem.h" #include "net/Network.h" -#include "Options.h" #include "Platform.h" #include "Summary.h" #include "version.h" #include "workers/Workers.h" -#ifdef HAVE_SYSLOG_H -# include "log/SysLog.h" -#endif - #ifndef XMRIG_NO_HTTPD # include "api/Httpd.h" #endif @@ -59,42 +55,19 @@ App *App::m_self = nullptr; App::App(int argc, char **argv) : m_console(nullptr), - m_httpd(nullptr), - m_network(nullptr), - m_options(nullptr) + m_httpd(nullptr) { m_self = this; m_controller = new xmrig::Controller(); - - Cpu::init(); - m_options = Options::parse(argc, argv); - if (!m_options) { + if (m_controller->init(argc, argv) != 0) { return; } - Log::init(); - - if (!m_options->background()) { - Log::add(new ConsoleLog(m_options->colors())); + if (!m_controller->config()->isBackground()) { m_console = new Console(this); } - if (m_options->logFile()) { - Log::add(new FileLog(m_options->logFile())); - } - -# ifdef HAVE_SYSLOG_H - if (m_options->syslog()) { - Log::add(new SysLog()); - } -# endif - - Platform::init(m_options->userAgent()); - Platform::setProcessPriority(m_options->priority()); - - m_network = new Network(m_options); - uv_signal_init(uv_default_loop(), &m_sigHUP); uv_signal_init(uv_default_loop(), &m_sigINT); uv_signal_init(uv_default_loop(), &m_sigTERM); @@ -103,19 +76,22 @@ App::App(int argc, char **argv) : App::~App() { + Mem::release(); + uv_tty_reset_mode(); + delete m_console; + delete m_controller; + # ifndef XMRIG_NO_HTTPD delete m_httpd; # endif - - delete m_console; } int App::exec() { - if (!m_options) { + if (!m_controller->config()) { return 2; } @@ -125,15 +101,20 @@ int App::exec() background(); - if (!CryptoNight::init(m_options->algo(), m_options->algoVariant())) { - LOG_ERR("\"%s\" hash self-test failed.", m_options->algoName()); + if (!CryptoNight::init(m_controller->config()->algorithm(), m_controller->config()->algoVariant(), m_controller->config()->isDoubleHash())) { + LOG_ERR("\"%s\" hash self-test failed.", m_controller->config()->algoName()); return 1; } - Mem::allocate(m_options->algo(), m_options->threads(), m_options->doubleHash(), m_options->hugePages()); - Summary::print(); + Mem::allocate(m_controller->config()->algorithm(), + m_controller->config()->threads(), + m_controller->config()->isDoubleHash(), + m_controller->config()->isHugePages() + ); - if (m_options->dryRun()) { + Summary::print(m_controller); + + if (m_controller->config()->isDryRun()) { LOG_NOTICE("OK"); release(); @@ -145,13 +126,19 @@ int App::exec() # endif # ifndef XMRIG_NO_HTTPD - m_httpd = new Httpd(m_options->apiPort(), m_options->apiToken(), true, true); + m_httpd = new Httpd( + m_controller->config()->apiPort(), + m_controller->config()->apiToken(), + m_controller->config()->isApiIPv6(), + m_controller->config()->isApiRestricted() + ); + m_httpd->start(); # endif - Workers::start(m_options->affinity(), m_options->priority()); + Workers::start(m_controller->config()->affinity(), m_controller->config()->priority(), m_controller); - m_network->connect(); + m_controller->network()->connect(); const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); uv_loop_close(uv_default_loop()); @@ -172,7 +159,7 @@ void App::onConsoleCommand(char command) case 'p': case 'P': if (Workers::isEnabled()) { - LOG_INFO(m_options->colors() ? "\x1B[01;33mpaused\x1B[0m, press \x1B[01;35mr\x1B[0m to resume" : "paused, press 'r' to resume"); + LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;33mpaused\x1B[0m, press \x1B[01;35mr\x1B[0m to resume" : "paused, press 'r' to resume"); Workers::setEnabled(false); } break; @@ -180,7 +167,7 @@ void App::onConsoleCommand(char command) case 'r': case 'R': if (!Workers::isEnabled()) { - LOG_INFO(m_options->colors() ? "\x1B[01;32mresumed" : "resumed"); + LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;32mresumed" : "resumed"); Workers::setEnabled(true); } break; @@ -198,7 +185,7 @@ void App::onConsoleCommand(char command) void App::close() { - m_network->stop(); + m_controller->network()->stop(); Workers::stop(); uv_stop(uv_default_loop()); @@ -207,13 +194,6 @@ void App::close() void App::release() { - if (m_network) { - delete m_network; - } - - Options::release(); - Mem::release(); - Platform::release(); } diff --git a/src/App.h b/src/App.h index 9116a79a..22269f67 100644 --- a/src/App.h +++ b/src/App.h @@ -64,8 +64,6 @@ private: Console *m_console; Httpd *m_httpd; - Network *m_network; - Options *m_options; uv_signal_t m_sigHUP; uv_signal_t m_sigINT; uv_signal_t m_sigTERM; diff --git a/src/App_win.cpp b/src/App_win.cpp index 895f3bdf..b3a2c4cf 100644 --- a/src/App_win.cpp +++ b/src/App_win.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,17 +27,19 @@ #include "App.h" -#include "Options.h" #include "Cpu.h" +#include "core/Controller.h" +#include "core/Config.h" void App::background() { - if (m_options->affinity() != -1L) { - Cpu::setAffinity(-1, m_options->affinity()); + const int64_t affinity = m_controller->config()->affinity(); + if (affinity != -1L) { + Cpu::setAffinity(-1, affinity); } - if (!m_options->background()) { + if (!m_controller->config()->isBackground()) { return; } diff --git a/src/Mem.cpp b/src/Mem.cpp index b32d2196..991f4398 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -28,7 +28,6 @@ #include "crypto/CryptoNight.h" #include "Mem.h" -#include "Options.h" #include "xmrig.h" diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 239bda7d..1b35c704 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -37,7 +37,6 @@ #include "log/Log.h" #include "crypto/CryptoNight.h" #include "Mem.h" -#include "Options.h" #include "xmrig.h" diff --git a/src/Options.cpp b/src/Options.cpp deleted file mode 100644 index 939075f8..00000000 --- a/src/Options.cpp +++ /dev/null @@ -1,761 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include -#include - - -#ifdef _MSC_VER -# include "getopt/getopt.h" -#else -# include -#endif - - -#ifndef XMRIG_NO_HTTPD -# include -#endif - - -#include "Cpu.h" -#include "donate.h" -#include "net/Url.h" -#include "Options.h" -#include "Platform.h" -#include "rapidjson/document.h" -#include "rapidjson/error/en.h" -#include "rapidjson/filereadstream.h" -#include "version.h" -#include "xmrig.h" - - -#ifndef ARRAY_SIZE -# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) -#endif - - -Options *Options::m_self = nullptr; - - -static char const usage[] = "\ -Usage: " APP_ID " [OPTIONS]\n\ -Options:\n\ - -a, --algo=ALGO cryptonight (default) or cryptonight-lite\n\ - -o, --url=URL URL of mining server\n\ - -O, --userpass=U:P username:password pair for mining server\n\ - -u, --user=USERNAME username for mining server\n\ - -p, --pass=PASSWORD password for mining server\n\ - -t, --threads=N number of miner threads\n\ - -v, --av=N algorithm variation, 0 auto select\n\ - -k, --keepalive send keepalived for prevent timeout (need pool support)\n\ - -r, --retries=N number of times to retry before switch to backup server (default: 5)\n\ - -R, --retry-pause=N time to pause between retries (default: 5)\n\ - --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1\n\ - --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ - --no-huge-pages disable huge pages support\n\ - --no-color disable colored output\n\ - --variant algorithm PoW variant\n\ - --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ - --user-agent set custom user-agent string for pool\n\ - -B, --background run the miner in the background\n\ - -c, --config=FILE load a JSON-format configuration file\n\ - -l, --log-file=FILE log all output to a file\n" -# ifdef HAVE_SYSLOG_H -"\ - -S, --syslog use system log for output messages\n" -# endif -"\ - --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75)\n\ - --safe safe adjust threads and av settings for current CPU\n\ - --nicehash enable nicehash/xmrig-proxy support\n\ - --print-time=N print hashrate report every N seconds\n\ - --api-port=N port for the miner API\n\ - --api-access-token=T access token for API\n\ - --api-worker-id=ID custom worker-id for API\n\ - -h, --help display this help and exit\n\ - -V, --version output version information and exit\n\ -"; - - -static char const short_options[] = "a:c:khBp:Px:r:R:s:t:T:o:u:O:v:Vl:S"; - - -static struct option const options[] = { - { "algo", 1, nullptr, 'a' }, - { "api-access-token", 1, nullptr, 4001 }, - { "api-port", 1, nullptr, 4000 }, - { "api-worker-id", 1, nullptr, 4002 }, - { "av", 1, nullptr, 'v' }, - { "background", 0, nullptr, 'B' }, - { "config", 1, nullptr, 'c' }, - { "cpu-affinity", 1, nullptr, 1020 }, - { "cpu-priority", 1, nullptr, 1021 }, - { "donate-level", 1, nullptr, 1003 }, - { "dry-run", 0, nullptr, 5000 }, - { "help", 0, nullptr, 'h' }, - { "keepalive", 0, nullptr ,'k' }, - { "log-file", 1, nullptr, 'l' }, - { "max-cpu-usage", 1, nullptr, 1004 }, - { "nicehash", 0, nullptr, 1006 }, - { "no-color", 0, nullptr, 1002 }, - { "no-huge-pages", 0, nullptr, 1009 }, - { "variant", 1, nullptr, 1010 }, - { "pass", 1, nullptr, 'p' }, - { "print-time", 1, nullptr, 1007 }, - { "retries", 1, nullptr, 'r' }, - { "retry-pause", 1, nullptr, 'R' }, - { "safe", 0, nullptr, 1005 }, - { "syslog", 0, nullptr, 'S' }, - { "threads", 1, nullptr, 't' }, - { "url", 1, nullptr, 'o' }, - { "user", 1, nullptr, 'u' }, - { "user-agent", 1, nullptr, 1008 }, - { "userpass", 1, nullptr, 'O' }, - { "version", 0, nullptr, 'V' }, - { 0, 0, 0, 0 } -}; - - -static struct option const config_options[] = { - { "algo", 1, nullptr, 'a' }, - { "av", 1, nullptr, 'v' }, - { "background", 0, nullptr, 'B' }, - { "colors", 0, nullptr, 2000 }, - { "cpu-affinity", 1, nullptr, 1020 }, - { "cpu-priority", 1, nullptr, 1021 }, - { "donate-level", 1, nullptr, 1003 }, - { "dry-run", 0, nullptr, 5000 }, - { "huge-pages", 0, nullptr, 1009 }, - { "log-file", 1, nullptr, 'l' }, - { "max-cpu-usage", 1, nullptr, 1004 }, - { "print-time", 1, nullptr, 1007 }, - { "retries", 1, nullptr, 'r' }, - { "retry-pause", 1, nullptr, 'R' }, - { "safe", 0, nullptr, 1005 }, - { "syslog", 0, nullptr, 'S' }, - { "threads", 1, nullptr, 't' }, - { "user-agent", 1, nullptr, 1008 }, - { 0, 0, 0, 0 } -}; - - -static struct option const pool_options[] = { - { "url", 1, nullptr, 'o' }, - { "pass", 1, nullptr, 'p' }, - { "user", 1, nullptr, 'u' }, - { "userpass", 1, nullptr, 'O' }, - { "keepalive", 0, nullptr ,'k' }, - { "variant", 1, nullptr, 1010 }, - { "nicehash", 0, nullptr, 1006 }, - { 0, 0, 0, 0 } -}; - - -static struct option const api_options[] = { - { "port", 1, nullptr, 4000 }, - { "access-token", 1, nullptr, 4001 }, - { "worker-id", 1, nullptr, 4002 }, - { 0, 0, 0, 0 } -}; - - -static const char *algo_names[] = { - "cryptonight", -# ifndef XMRIG_NO_AEON - "cryptonight-lite" -# endif -}; - - -Options *Options::parse(int argc, char **argv) -{ - Options *options = new Options(argc, argv); - if (options->isReady()) { - m_self = options; - return m_self; - } - - delete options; - return nullptr; -} - - -const char *Options::algoName() const -{ - return algo_names[m_algo]; -} - - -Options::Options(int argc, char **argv) : - m_background(false), - m_colors(true), - m_doubleHash(false), - m_dryRun(false), - m_hugePages(true), - m_ready(false), - m_safe(false), - m_syslog(false), - m_apiToken(nullptr), - m_apiWorkerId(nullptr), - m_logFile(nullptr), - m_userAgent(nullptr), - m_algo(0), - m_algoVariant(0), - m_apiPort(0), - m_donateLevel(kDonateLevel), - m_maxCpuUsage(75), - m_printTime(60), - m_priority(-1), - m_retries(5), - m_retryPause(5), - m_threads(0), - m_affinity(-1L) -{ - m_pools.push_back(new Url()); - - int key; - - while (1) { - key = getopt_long(argc, argv, short_options, options, NULL); - if (key < 0) { - break; - } - - if (!parseArg(key, optarg)) { - return; - } - } - - if (optind < argc) { - fprintf(stderr, "%s: unsupported non-option argument '%s'\n", argv[0], argv[optind]); - return; - } - - if (!m_pools[0]->isValid()) { - parseConfig(Platform::defaultConfigName()); - } - - if (!m_pools[0]->isValid()) { - fprintf(stderr, "No pool URL supplied. Exiting.\n"); - return; - } - - m_algoVariant = getAlgoVariant(); - if (m_algoVariant == AV2_AESNI_DOUBLE || m_algoVariant == AV4_SOFT_AES_DOUBLE) { - m_doubleHash = true; - } - - if (!m_threads) { - m_threads = Cpu::optimalThreadsCount(m_algo, m_doubleHash, m_maxCpuUsage); - } - else if (m_safe) { - const int count = Cpu::optimalThreadsCount(m_algo, m_doubleHash, m_maxCpuUsage); - if (m_threads > count) { - m_threads = count; - } - } - - adjust(); - - m_ready = true; -} - - -Options::~Options() -{ -} - - -bool Options::getJSON(const char *fileName, rapidjson::Document &doc) -{ - uv_fs_t req; - const int fd = uv_fs_open(uv_default_loop(), &req, fileName, O_RDONLY, 0644, nullptr); - if (fd < 0) { - fprintf(stderr, "unable to open %s: %s\n", fileName, uv_strerror(fd)); - return false; - } - - uv_fs_req_cleanup(&req); - - FILE *fp = fdopen(fd, "rb"); - char buf[8192]; - rapidjson::FileReadStream is(fp, buf, sizeof(buf)); - - doc.ParseStream(is); - - uv_fs_close(uv_default_loop(), &req, fd, nullptr); - uv_fs_req_cleanup(&req); - - if (doc.HasParseError()) { - printf("%s:%d: %s\n", fileName, (int) doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError())); - return false; - } - - return doc.IsObject(); -} - - -bool Options::parseArg(int key, const char *arg) -{ - switch (key) { - case 'a': /* --algo */ - if (!setAlgo(arg)) { - return false; - } - break; - - case 'o': /* --url */ - if (m_pools.size() > 1 || m_pools[0]->isValid()) { - Url *url = new Url(arg); - if (url->isValid()) { - m_pools.push_back(url); - } - else { - delete url; - } - } - else { - m_pools[0]->parse(arg); - } - - if (!m_pools.back()->isValid()) { - return false; - } - break; - - case 'O': /* --userpass */ - if (!m_pools.back()->setUserpass(arg)) { - return false; - } - break; - - case 'u': /* --user */ - m_pools.back()->setUser(arg); - break; - - case 'p': /* --pass */ - m_pools.back()->setPassword(arg); - break; - - case 'l': /* --log-file */ - free(m_logFile); - m_logFile = strdup(arg); - m_colors = false; - break; - - case 4001: /* --access-token */ - free(m_apiToken); - m_apiToken = strdup(arg); - break; - - case 4002: /* --worker-id */ - free(m_apiWorkerId); - m_apiWorkerId = strdup(arg); - break; - - case 'r': /* --retries */ - case 'R': /* --retry-pause */ - case 'v': /* --av */ - case 1003: /* --donate-level */ - case 1004: /* --max-cpu-usage */ - case 1007: /* --print-time */ - case 1021: /* --cpu-priority */ - case 4000: /* --api-port */ - case 1010: /* --variant */ - return parseArg(key, strtol(arg, nullptr, 10)); - - case 'B': /* --background */ - case 'k': /* --keepalive */ - case 'S': /* --syslog */ - case 1005: /* --safe */ - case 1006: /* --nicehash */ - case 5000: /* --dry-run */ - return parseBoolean(key, true); - - case 1002: /* --no-color */ - case 1009: /* --no-huge-pages */ - return parseBoolean(key, false); - - case 't': /* --threads */ - if (strncmp(arg, "all", 3) == 0) { - m_threads = Cpu::threads(); - return true; - } - - return parseArg(key, strtol(arg, nullptr, 10)); - - case 'V': /* --version */ - showVersion(); - return false; - - case 'h': /* --help */ - showUsage(0); - return false; - - case 'c': /* --config */ - parseConfig(arg); - break; - - case 1020: { /* --cpu-affinity */ - const char *p = strstr(arg, "0x"); - return parseArg(key, p ? strtoull(p, nullptr, 16) : strtoull(arg, nullptr, 10)); - } - - case 1008: /* --user-agent */ - free(m_userAgent); - m_userAgent = strdup(arg); - break; - - default: - showUsage(1); - return false; - } - - return true; -} - - -bool Options::parseArg(int key, uint64_t arg) -{ - switch (key) { - case 'r': /* --retries */ - if (arg < 1 || arg > 1000) { - showUsage(1); - return false; - } - - m_retries = (int) arg; - break; - - case 'R': /* --retry-pause */ - if (arg < 1 || arg > 3600) { - showUsage(1); - return false; - } - - m_retryPause = (int) arg; - break; - - case 't': /* --threads */ - if (arg < 1 || arg > 1024) { - showUsage(1); - return false; - } - - m_threads = (int) arg; - break; - - case 'v': /* --av */ - if (arg > 1000) { - showUsage(1); - return false; - } - - m_algoVariant = (int) arg; - break; - - case 1003: /* --donate-level */ - if (arg < 1 || arg > 99) { - return true; - } - - m_donateLevel = (int) arg; - break; - - case 1004: /* --max-cpu-usage */ - if (arg < 1 || arg > 100) { - showUsage(1); - return false; - } - - m_maxCpuUsage = (int) arg; - break; - - case 1007: /* --print-time */ - if (arg > 1000) { - showUsage(1); - return false; - } - - m_printTime = (int) arg; - break; - - case 1010: /* --variant */ - m_pools.back()->setVariant((int) arg); - break; - - case 1020: /* --cpu-affinity */ - if (arg) { - m_affinity = arg; - } - break; - - case 1021: /* --cpu-priority */ - if (arg <= 5) { - m_priority = (int) arg; - } - break; - - case 4000: /* --api-port */ - if (arg <= 65536) { - m_apiPort = (int) arg; - } - break; - - default: - break; - } - - return true; -} - - -bool Options::parseBoolean(int key, bool enable) -{ - switch (key) { - case 'k': /* --keepalive */ - m_pools.back()->setKeepAlive(enable); - break; - - case 'B': /* --background */ - m_background = enable; - m_colors = enable ? false : m_colors; - break; - - case 'S': /* --syslog */ - m_syslog = enable; - m_colors = enable ? false : m_colors; - break; - - case 1002: /* --no-color */ - m_colors = enable; - break; - - case 1005: /* --safe */ - m_safe = enable; - break; - - case 1006: /* --nicehash */ - m_pools.back()->setNicehash(enable); - break; - - case 1009: /* --no-huge-pages */ - m_hugePages = enable; - break; - - case 2000: /* colors */ - m_colors = enable; - break; - - case 5000: /* --dry-run */ - m_dryRun = enable; - break; - - default: - break; - } - - return true; -} - - -Url *Options::parseUrl(const char *arg) const -{ - auto url = new Url(arg); - if (!url->isValid()) { - delete url; - return nullptr; - } - - return url; -} - - -void Options::adjust() -{ - for (Url *url : m_pools) { - url->adjust(m_algo); - } -} - - -void Options::parseConfig(const char *fileName) -{ - rapidjson::Document doc; - if (!getJSON(fileName, doc)) { - return; - } - - for (size_t i = 0; i < ARRAY_SIZE(config_options); i++) { - parseJSON(&config_options[i], doc); - } - - const rapidjson::Value &pools = doc["pools"]; - if (pools.IsArray()) { - for (const rapidjson::Value &value : pools.GetArray()) { - if (!value.IsObject()) { - continue; - } - - for (size_t i = 0; i < ARRAY_SIZE(pool_options); i++) { - parseJSON(&pool_options[i], value); - } - } - } - - const rapidjson::Value &api = doc["api"]; - if (api.IsObject()) { - for (size_t i = 0; i < ARRAY_SIZE(api_options); i++) { - parseJSON(&api_options[i], api); - } - } -} - - -void Options::parseJSON(const struct option *option, const rapidjson::Value &object) -{ - if (!option->name || !object.HasMember(option->name)) { - return; - } - - const rapidjson::Value &value = object[option->name]; - - if (option->has_arg && value.IsString()) { - parseArg(option->val, value.GetString()); - } - else if (option->has_arg && value.IsInt64()) { - parseArg(option->val, value.GetUint64()); - } - else if (!option->has_arg && value.IsBool()) { - parseBoolean(option->val, value.IsTrue()); - } -} - - -void Options::showUsage(int status) const -{ - if (status) { - fprintf(stderr, "Try \"" APP_ID "\" --help' for more information.\n"); - } - else { - printf(usage); - } -} - - -void Options::showVersion() -{ - printf(APP_NAME " " APP_VERSION "\n built on " __DATE__ - -# if defined(__clang__) - " with clang " __clang_version__); -# elif defined(__GNUC__) - " with GCC"); - printf(" %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); -# elif defined(_MSC_VER) - " with MSVC"); - printf(" %d", MSVC_VERSION); -# else - ); -# endif - - printf("\n features:" -# if defined(__i386__) || defined(_M_IX86) - " i386" -# elif defined(__x86_64__) || defined(_M_AMD64) - " x86_64" -# endif - -# if defined(__AES__) || defined(_MSC_VER) - " AES-NI" -# endif - "\n"); - - printf("\nlibuv/%s\n", uv_version_string()); - -# ifndef XMRIG_NO_HTTPD - printf("libmicrohttpd/%s\n", MHD_get_version()); -# endif -} - - -bool Options::setAlgo(const char *algo) -{ - for (size_t i = 0; i < ARRAY_SIZE(algo_names); i++) { - if (algo_names[i] && !strcmp(algo, algo_names[i])) { - m_algo = (int) i; - break; - } - -# ifndef XMRIG_NO_AEON - if (i == ARRAY_SIZE(algo_names) - 1 && !strcmp(algo, "cryptonight-light")) { - m_algo = xmrig::ALGO_CRYPTONIGHT_LITE; - break; - } -# endif - - if (i == ARRAY_SIZE(algo_names) - 1) { - showUsage(1); - return false; - } - } - - return true; -} - - -int Options::getAlgoVariant() const -{ -# ifndef XMRIG_NO_AEON - if (m_algo == xmrig::ALGO_CRYPTONIGHT_LITE) { - return getAlgoVariantLite(); - } -# endif - - if (m_algoVariant <= AV0_AUTO || m_algoVariant >= AV_MAX) { - return Cpu::hasAES() ? AV1_AESNI : AV3_SOFT_AES; - } - - if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV2_AESNI_DOUBLE) { - return m_algoVariant + 2; - } - - return m_algoVariant; -} - - -#ifndef XMRIG_NO_AEON -int Options::getAlgoVariantLite() const -{ - if (m_algoVariant <= AV0_AUTO || m_algoVariant >= AV_MAX) { - return Cpu::hasAES() ? AV2_AESNI_DOUBLE : AV4_SOFT_AES_DOUBLE; - } - - if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV2_AESNI_DOUBLE) { - return m_algoVariant + 2; - } - - return m_algoVariant; -} -#endif diff --git a/src/Options.h b/src/Options.h deleted file mode 100644 index e29059ec..00000000 --- a/src/Options.h +++ /dev/null @@ -1,133 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef __OPTIONS_H__ -#define __OPTIONS_H__ - - -#include -#include - - -#include "rapidjson/fwd.h" - - -class Url; -struct option; - - -class Options -{ -public: - enum AlgoVariant { - AV0_AUTO, - AV1_AESNI, - AV2_AESNI_DOUBLE, - AV3_SOFT_AES, - AV4_SOFT_AES_DOUBLE, - AV_MAX - }; - - static inline Options* i() { return m_self; } - static Options *parse(int argc, char **argv); - - inline bool background() const { return m_background; } - inline bool colors() const { return m_colors; } - inline bool doubleHash() const { return m_doubleHash; } - inline bool dryRun() const { return m_dryRun; } - inline bool hugePages() const { return m_hugePages; } - inline bool syslog() const { return m_syslog; } - inline const char *apiToken() const { return m_apiToken; } - inline const char *apiWorkerId() const { return m_apiWorkerId; } - inline const char *logFile() const { return m_logFile; } - inline const char *userAgent() const { return m_userAgent; } - inline const std::vector &pools() const { return m_pools; } - inline int algo() const { return m_algo; } - inline int algoVariant() const { return m_algoVariant; } - inline int apiPort() const { return m_apiPort; } - inline int donateLevel() const { return m_donateLevel; } - inline int printTime() const { return m_printTime; } - inline int priority() const { return m_priority; } - inline int retries() const { return m_retries; } - inline int retryPause() const { return m_retryPause; } - inline int threads() const { return m_threads; } - inline int64_t affinity() const { return m_affinity; } - inline void setColors(bool colors) { m_colors = colors; } - - inline static void release() { delete m_self; } - - const char *algoName() const; - -private: - Options(int argc, char **argv); - ~Options(); - - inline bool isReady() const { return m_ready; } - - static Options *m_self; - - bool getJSON(const char *fileName, rapidjson::Document &doc); - bool parseArg(int key, const char *arg); - bool parseArg(int key, uint64_t arg); - bool parseBoolean(int key, bool enable); - Url *parseUrl(const char *arg) const; - void adjust(); - void parseConfig(const char *fileName); - void parseJSON(const struct option *option, const rapidjson::Value &object); - void showUsage(int status) const; - void showVersion(void); - - bool setAlgo(const char *algo); - - int getAlgoVariant() const; -# ifndef XMRIG_NO_AEON - int getAlgoVariantLite() const; -# endif - - bool m_background; - bool m_colors; - bool m_doubleHash; - bool m_dryRun; - bool m_hugePages; - bool m_ready; - bool m_safe; - bool m_syslog; - char *m_apiToken; - char *m_apiWorkerId; - char *m_logFile; - char *m_userAgent; - int m_algo; - int m_algoVariant; - int m_apiPort; - int m_donateLevel; - int m_maxCpuUsage; - int m_printTime; - int m_priority; - int m_retries; - int m_retryPause; - int m_threads; - int64_t m_affinity; - std::vector m_pools; -}; - -#endif /* __OPTIONS_H__ */ diff --git a/src/Summary.cpp b/src/Summary.cpp index 2d93f429..4b663fbf 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,16 +27,17 @@ #include +#include "core/Config.h" +#include "core/Controller.h" #include "Cpu.h" #include "log/Log.h" #include "Mem.h" #include "net/Url.h" -#include "Options.h" #include "Summary.h" #include "version.h" -static void print_versions() +static void print_versions(xmrig::Config *config) { char buf[16]; @@ -51,13 +52,13 @@ static void print_versions() # endif - Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mVERSIONS: \x1B[01;36mXMRig/%s\x1B[01;37m libuv/%s%s" : " * VERSIONS: XMRig/%s libuv/%s%s", + Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mVERSIONS: \x1B[01;36mXMRig/%s\x1B[01;37m libuv/%s%s" : " * VERSIONS: XMRig/%s libuv/%s%s", APP_VERSION, uv_version_string(), buf); } -static void print_memory() { - if (Options::i()->colors()) { +static void print_memory(xmrig::Config *config) { + if (config->isColors()) { Log::i()->text("\x1B[01;32m * \x1B[01;37mHUGE PAGES: %s, %s", Mem::isHugepagesAvailable() ? "\x1B[01;32mavailable" : "\x1B[01;31munavailable", Mem::isHugepagesEnabled() ? "\x1B[01;32menabled" : "\x1B[01;31mdisabled"); @@ -68,9 +69,9 @@ static void print_memory() { } -static void print_cpu() +static void print_cpu(xmrig::Config *config) { - if (Options::i()->colors()) { + if (config->isColors()) { Log::i()->text("\x1B[01;32m * \x1B[01;37mCPU: %s (%d) %sx64 %sAES-NI", Cpu::brand(), Cpu::sockets(), @@ -89,32 +90,32 @@ static void print_cpu() } -static void print_threads() +static void print_threads(xmrig::Config *config) { char buf[32]; - if (Options::i()->affinity() != -1L) { - snprintf(buf, 32, ", affinity=0x%" PRIX64, Options::i()->affinity()); + if (config->affinity() != -1L) { + snprintf(buf, 32, ", affinity=0x%" PRIX64, config->affinity()); } else { buf[0] = '\0'; } - Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mTHREADS: \x1B[01;36m%d\x1B[01;37m, %s, av=%d, %sdonate=%d%%%s" : " * THREADS: %d, %s, av=%d, %sdonate=%d%%%s", - Options::i()->threads(), - Options::i()->algoName(), - Options::i()->algoVariant(), - Options::i()->colors() && Options::i()->donateLevel() == 0 ? "\x1B[01;31m" : "", - Options::i()->donateLevel(), + Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mTHREADS: \x1B[01;36m%d\x1B[01;37m, %s, av=%d, %sdonate=%d%%%s" : " * THREADS: %d, %s, av=%d, %sdonate=%d%%%s", + config->threads(), + config->algoName(), + config->algoVariant(), + config->isColors() && config->donateLevel() == 0 ? "\x1B[01;31m" : "", + config->donateLevel(), buf); } -static void print_pools() +static void print_pools(xmrig::Config *config) { - const std::vector &pools = Options::i()->pools(); + const std::vector &pools = config->pools(); for (size_t i = 0; i < pools.size(); ++i) { - Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mPOOL #%d: \x1B[01;36m%s:%d" : " * POOL #%d: %s:%d", + Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mPOOL #%d: \x1B[01;36m%s:%d" : " * POOL #%d: %s:%d", i + 1, pools[i]->host(), pools[i]->port()); @@ -129,20 +130,20 @@ static void print_pools() #ifndef XMRIG_NO_API -static void print_api() +static void print_api(xmrig::Config *config) { - if (Options::i()->apiPort() == 0) { + if (config->apiPort() == 0) { return; } - Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mAPI PORT: \x1B[01;36m%d" : " * API PORT: %d", Options::i()->apiPort()); + Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mAPI PORT: \x1B[01;36m%d" : " * API PORT: %d", config->apiPort()); } #endif -static void print_commands() +static void print_commands(xmrig::Config *config) { - if (Options::i()->colors()) { + if (config->isColors()) { Log::i()->text("\x1B[01;32m * \x1B[01;37mCOMMANDS: \x1B[01;35mh\x1B[01;37mashrate, \x1B[01;35mp\x1B[01;37mause, \x1B[01;35mr\x1B[01;37mesume"); } else { @@ -151,19 +152,19 @@ static void print_commands() } -void Summary::print() +void Summary::print(xmrig::Controller *controller) { - print_versions(); - print_memory(); - print_cpu(); - print_threads(); - print_pools(); + print_versions(controller->config()); + print_memory(controller->config()); + print_cpu(controller->config()); + print_threads(controller->config()); + print_pools(controller->config()); # ifndef XMRIG_NO_API - print_api(); + print_api(controller->config()); # endif - print_commands(); + print_commands(controller->config()); } diff --git a/src/Summary.h b/src/Summary.h index 3f64fd60..f07dba35 100644 --- a/src/Summary.h +++ b/src/Summary.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,10 +25,15 @@ #define __SUMMARY_H__ +namespace xmrig { + class Controller; +} + + class Summary { public: - static void print(); + static void print(xmrig::Controller *controller); }; diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 6ee8e7a4..bb8c7402 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -35,14 +35,15 @@ #include "api/ApiRouter.h" #include "api/HttpReply.h" #include "api/HttpRequest.h" +#include "core/Config.h" +#include "core/Controller.h" #include "Cpu.h" #include "Mem.h" #include "net/Job.h" -#include "Options.h" #include "Platform.h" #include "rapidjson/document.h" -#include "rapidjson/stringbuffer.h" #include "rapidjson/prettywriter.h" +#include "rapidjson/stringbuffer.h" #include "version.h" #include "workers/Hashrate.h" @@ -66,13 +67,13 @@ static inline double normalize(double d) ApiRouter::ApiRouter(xmrig::Controller *controller) : m_controller(controller) { - m_threads = Options::i()->threads(); + m_threads = controller->config()->threads(); m_hashrate = new double[m_threads * 3](); memset(m_totalHashrate, 0, sizeof(m_totalHashrate)); memset(m_workerId, 0, sizeof(m_workerId)); - setWorkerId(Options::i()->apiWorkerId()); + setWorkerId(controller->config()->apiWorkerId()); genId(); } @@ -100,10 +101,10 @@ void ApiRouter::ApiRouter::get(const xmrig::HttpRequest &req, xmrig::HttpReply & void ApiRouter::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) { -// if (req.method() == xmrig::HttpRequest::Put && req.match("/1/config")) { -// m_controller->config()->reload(req.body()); -// return; -// } + if (req.method() == xmrig::HttpRequest::Put && req.match("/1/config")) { + m_controller->config()->reload(req.body()); + return; + } reply.status = 404; } @@ -246,9 +247,9 @@ void ApiRouter::getMiner(rapidjson::Document &doc) const doc.AddMember("kind", APP_KIND, allocator); doc.AddMember("ua", rapidjson::StringRef(Platform::userAgent()), allocator); doc.AddMember("cpu", cpu, allocator); - doc.AddMember("algo", rapidjson::StringRef(Options::i()->algoName()), allocator); + doc.AddMember("algo", rapidjson::StringRef(m_controller->config()->algoName()), allocator); doc.AddMember("hugepages", Mem::isHugepagesEnabled(), allocator); - doc.AddMember("donate_level", Options::i()->donateLevel(), allocator); + doc.AddMember("donate_level", m_controller->config()->donateLevel(), allocator); } diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 9bf44bc8..f30ed1f4 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -40,7 +40,6 @@ xmrig::Config::Config() : xmrig::CommonConfig(), m_dryRun(false), m_hugePages(true), m_safe(false), - m_algo(0), m_algoVariant(0), m_maxCpuUsage(75), m_printTime(60), @@ -151,10 +150,10 @@ bool xmrig::Config::adjust() } if (!m_threads) { - m_threads = Cpu::optimalThreadsCount(m_algo, m_doubleHash, m_maxCpuUsage); + m_threads = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); } else if (m_safe) { - const int count = Cpu::optimalThreadsCount(m_algo, m_doubleHash, m_maxCpuUsage); + const int count = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); if (m_threads > count) { m_threads = count; } @@ -296,7 +295,7 @@ bool xmrig::Config::parseInt(int key, int arg) int xmrig::Config::getAlgoVariant() const { # ifndef XMRIG_NO_AEON - if (m_algo == xmrig::ALGO_CRYPTONIGHT_LITE) { + if (m_algorithm == xmrig::ALGO_CRYPTONIGHT_LITE) { return getAlgoVariantLite(); } # endif diff --git a/src/core/Config.h b/src/core/Config.h index a10cd696..110c61a7 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -79,7 +79,6 @@ public: inline bool isDoubleHash() const { return m_doubleHash; } inline bool isDryRun() const { return m_dryRun; } inline bool isHugePages() const { return m_hugePages; } - inline int algo() const { return m_algo; } inline int algoVariant() const { return m_algoVariant; } inline int printTime() const { return m_printTime; } inline int priority() const { return m_priority; } @@ -107,7 +106,6 @@ private: bool m_dryRun; bool m_hugePages; bool m_safe; - int m_algo; int m_algoVariant; int m_maxCpuUsage; int m_printTime; diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index a5ffe236..b8c93c82 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -22,15 +22,19 @@ */ -//#include "core/Config.h" -//#include "core/ConfigLoader.h" +#include + + +#include "core/Config.h" +#include "core/ConfigLoader.h" #include "core/Controller.h" +#include "Cpu.h" +#include "interfaces/IControllerListener.h" #include "log/ConsoleLog.h" #include "log/FileLog.h" #include "log/Log.h" +#include "net/Network.h" #include "Platform.h" -//#include "proxy/Proxy.h" -#include "interfaces/IControllerListener.h" #ifdef HAVE_SYSLOG_H @@ -42,18 +46,21 @@ class xmrig::ControllerPrivate { public: inline ControllerPrivate() : + network(nullptr), config(nullptr) {} inline ~ControllerPrivate() { -// delete config; + delete network; + delete config; } - xmrig::Config *config; + Network *network; std::vector listeners; + xmrig::Config *config; }; @@ -65,7 +72,8 @@ xmrig::Controller::Controller() xmrig::Controller::~Controller() { -// ConfigLoader::release(); + ConfigLoader::release(); + Platform::release(); delete d_ptr; } @@ -73,39 +81,52 @@ xmrig::Controller::~Controller() xmrig::Config *xmrig::Controller::config() const { + assert(d_ptr->config != nullptr); + return d_ptr->config; } int xmrig::Controller::init(int argc, char **argv) { -// d_ptr->config = xmrig::Config::load(argc, argv, this); -// if (!d_ptr->config) { -// return 1; -// } + Cpu::init(); -// Log::init(); -// Platform::init(config()->userAgent()); + d_ptr->config = xmrig::Config::load(argc, argv, this); + if (!d_ptr->config) { + return 1; + } -// if (!config()->background()) { -// Log::add(new ConsoleLog(this)); -// } + Log::init(); + Platform::init(config()->userAgent()); + Platform::setProcessPriority(d_ptr->config->priority()); -// if (config()->logFile()) { -// Log::add(new FileLog(config()->logFile())); -// } + if (!config()->isBackground()) { + Log::add(new ConsoleLog(this)); + } -//# ifdef HAVE_SYSLOG_H -// if (config()->syslog()) { -// Log::add(new SysLog()); -// } -//# endif + if (config()->logFile()) { + Log::add(new FileLog(config()->logFile())); + } -// d_ptr->proxy = new Proxy(this); +# ifdef HAVE_SYSLOG_H + if (config()->syslog()) { + Log::add(new SysLog()); + } +# endif + + d_ptr->network = new Network(this); return 0; } +Network *xmrig::Controller::network() const +{ + assert(d_ptr->network != nullptr); + + return d_ptr->network; +} + + void xmrig::Controller::addListener(IControllerListener *listener) { d_ptr->listeners.push_back(listener); @@ -114,12 +135,12 @@ void xmrig::Controller::addListener(IControllerListener *listener) void xmrig::Controller::onNewConfig(IConfig *config) { -// xmrig::Config *previousConfig = d_ptr->config; -// d_ptr->config = config; + Config *previousConfig = d_ptr->config; + d_ptr->config = static_cast(config); -// for (xmrig::IControllerListener *listener : d_ptr->listeners) { -// listener->onConfigChanged(config, previousConfig); -// } + for (xmrig::IControllerListener *listener : d_ptr->listeners) { + listener->onConfigChanged(d_ptr->config, previousConfig); + } -// delete previousConfig; + delete previousConfig; } diff --git a/src/core/Controller.h b/src/core/Controller.h index 9144cca7..2ba80f99 100644 --- a/src/core/Controller.h +++ b/src/core/Controller.h @@ -28,7 +28,7 @@ #include "interfaces/IWatcherListener.h" -class Proxy; +class Network; class StatsData; @@ -48,7 +48,7 @@ public: Config *config() const; int init(int argc, char **argv); - Proxy *proxy() const; + Network *network() const; void addListener(IControllerListener *listener); protected: diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index b605bfb8..c9a2e499 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -35,7 +35,6 @@ #include "crypto/CryptoNight_test.h" #include "net/Job.h" #include "net/JobResult.h" -#include "Options.h" #include "xmrig.h" @@ -131,7 +130,7 @@ bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) } -bool CryptoNight::init(int algo, int variant) +bool CryptoNight::init(int algo, int variant, bool doubleHash) { if (variant < 1 || variant > 4) { return false; @@ -145,7 +144,7 @@ bool CryptoNight::init(int algo, int variant) cryptonight_hash_ctx = cryptonight_variations[index]; - return selfTest(algo); + return selfTest(algo, doubleHash); } @@ -155,7 +154,7 @@ void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, crypt } -bool CryptoNight::selfTest(int algo) { +bool CryptoNight::selfTest(int algo, bool doubleHash) { if (cryptonight_hash_ctx == nullptr) { return false; } @@ -167,8 +166,6 @@ bool CryptoNight::selfTest(int algo) { cryptonight_hash_ctx(test_input, 76, output, ctx, 0); - const bool doubleHash = Options::i()->doubleHash(); - # ifndef XMRIG_NO_AEON bool rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output_v0_lite : test_output_v0, (doubleHash ? 64 : 32)) == 0; # else diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 13e9c8e8..eb17719e 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -54,11 +54,11 @@ class CryptoNight { public: static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); - static bool init(int algo, int variant); + static bool init(int algo, int variant, bool doubleHash); static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant); private: - static bool selfTest(int algo); + static bool selfTest(int algo, bool doubleHash); }; #endif /* __CRYPTONIGHT_H__ */ diff --git a/src/log/ConsoleLog.cpp b/src/log/ConsoleLog.cpp index 3656d48c..eeed7355 100644 --- a/src/log/ConsoleLog.cpp +++ b/src/log/ConsoleLog.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,18 +34,18 @@ #endif +#include "core/Config.h" +#include "core/Controller.h" #include "log/ConsoleLog.h" #include "log/Log.h" -#include "Options.h" -ConsoleLog::ConsoleLog(bool colors) : - m_colors(colors), - m_stream(nullptr) +ConsoleLog::ConsoleLog(xmrig::Controller *controller) : + m_stream(nullptr), + m_controller(controller) { if (uv_tty_init(uv_default_loop(), &m_tty, 1, 0) < 0) { - Options::i()->setColors(false); - m_colors = false; + controller->config()->setColors(false); return; } @@ -78,7 +78,9 @@ void ConsoleLog::message(int level, const char* fmt, va_list args) # endif const char* color = nullptr; - if (m_colors) { + const bool colors = m_controller->config()->isColors(); + + if (colors) { switch (level) { case Log::ERR: color = Log::kCL_RED; @@ -109,9 +111,9 @@ void ConsoleLog::message(int level, const char* fmt, va_list args) stime.tm_hour, stime.tm_min, stime.tm_sec, - m_colors ? color : "", + colors ? color : "", fmt, - m_colors ? Log::kCL_N : "" + colors ? Log::kCL_N : "" ); print(args); @@ -120,7 +122,7 @@ void ConsoleLog::message(int level, const char* fmt, va_list args) void ConsoleLog::text(const char* fmt, va_list args) { - snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s\n", fmt, m_colors ? Log::kCL_N : ""); + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s\n", fmt, m_controller->config()->isColors() ? Log::kCL_N : ""); print(args); } diff --git a/src/log/ConsoleLog.h b/src/log/ConsoleLog.h index a04a27c5..6649be84 100644 --- a/src/log/ConsoleLog.h +++ b/src/log/ConsoleLog.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,10 +31,15 @@ #include "interfaces/ILogBackend.h" +namespace xmrig { + class Controller; +} + + class ConsoleLog : public ILogBackend { public: - ConsoleLog(bool colors); + ConsoleLog(xmrig::Controller *controller); void message(int level, const char *fmt, va_list args) override; void text(const char *fmt, va_list args) override; @@ -43,12 +48,12 @@ private: bool isWritable() const; void print(va_list args); - bool m_colors; char m_buf[512]; char m_fmt[256]; uv_buf_t m_uvBuf; uv_stream_t *m_stream; uv_tty_t m_tty; + xmrig::Controller *m_controller; }; #endif /* __CONSOLELOG_H__ */ diff --git a/src/net/Network.cpp b/src/net/Network.cpp index ede3f8b4..a8e60efa 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -39,29 +39,30 @@ #include "net/strategies/SinglePoolStrategy.h" #include "net/SubmitResult.h" #include "net/Url.h" -#include "Options.h" #include "workers/Workers.h" +#include "core/Controller.h" +#include "core/Config.h" -Network::Network(const Options *options) : - m_options(options), - m_donate(nullptr) +Network::Network(xmrig::Controller *controller) : + m_donate(nullptr), + m_controller(controller) { srand(time(0) ^ (uintptr_t) this); Workers::setListener(this); - const std::vector &pools = options->pools(); + const std::vector &pools = controller->config()->pools(); if (pools.size() > 1) { - m_strategy = new FailoverStrategy(pools, options->retryPause(), options->retries(), this); + m_strategy = new FailoverStrategy(pools, controller->config()->retryPause(), controller->config()->retries(), this); } else { - m_strategy = new SinglePoolStrategy(pools.front(), options->retryPause(), this); + m_strategy = new SinglePoolStrategy(pools.front(), controller->config()->retryPause(), this); } - if (m_options->donateLevel() > 0) { - m_donate = new DonateStrategy(options->donateLevel(), options->pools().front()->user(), options->algo(), this); + if (controller->config()->donateLevel() > 0) { + m_donate = new DonateStrategy(controller->config()->donateLevel(), controller->config()->pools().front()->user(), controller->config()->algorithm(), this); } m_timer.data = this; @@ -101,7 +102,7 @@ void Network::onActive(IStrategy *strategy, Client *client) m_state.setPool(client->host(), client->port(), client->ip()); - LOG_INFO(m_options->colors() ? "\x1B[01;37muse pool \x1B[01;36m%s:%d \x1B[01;30m%s" : "use pool %s:%d %s", client->host(), client->port(), client->ip()); + LOG_INFO(isColors() ? "\x1B[01;37muse pool \x1B[01;36m%s:%d \x1B[01;30m%s" : "use pool %s:%d %s", client->host(), client->port(), client->ip()); } @@ -146,21 +147,27 @@ void Network::onResultAccepted(IStrategy *strategy, Client *client, const Submit m_state.add(result, error); if (error) { - LOG_INFO(m_options->colors() ? "\x1B[01;31mrejected\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[01;37m%u\x1B[0m \x1B[31m\"%s\"\x1B[0m \x1B[01;30m(%" PRIu64 " ms)" - : "rejected (%" PRId64 "/%" PRId64 ") diff %u \"%s\" (%" PRIu64 " ms)", + LOG_INFO(isColors() ? "\x1B[01;31mrejected\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[01;37m%u\x1B[0m \x1B[31m\"%s\"\x1B[0m \x1B[01;30m(%" PRIu64 " ms)" + : "rejected (%" PRId64 "/%" PRId64 ") diff %u \"%s\" (%" PRIu64 " ms)", m_state.accepted, m_state.rejected, result.diff, error, result.elapsed); } else { - LOG_INFO(m_options->colors() ? "\x1B[01;32maccepted\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[01;37m%u\x1B[0m \x1B[01;30m(%" PRIu64 " ms)" - : "accepted (%" PRId64 "/%" PRId64 ") diff %u (%" PRIu64 " ms)", + LOG_INFO(isColors() ? "\x1B[01;32maccepted\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[01;37m%u\x1B[0m \x1B[01;30m(%" PRIu64 " ms)" + : "accepted (%" PRId64 "/%" PRId64 ") diff %u (%" PRIu64 " ms)", m_state.accepted, m_state.rejected, result.diff, result.elapsed); } } +bool Network::isColors() const +{ + return m_controller->config()->isColors(); +} + + void Network::setJob(Client *client, const Job &job, bool donate) { - if (m_options->colors()) { + if (isColors()) { LOG_INFO("\x1B[01;35mnew job\x1B[0m from \x1B[01;37m%s:%d\x1B[0m diff \x1B[01;37m%d", client->host(), client->port(), job.diff()); } else { diff --git a/src/net/Network.h b/src/net/Network.h index fae5c563..353edc77 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -35,14 +35,18 @@ class IStrategy; -class Options; class Url; +namespace xmrig { + class Controller; +} + + class Network : public IJobResultListener, public IStrategyListener { public: - Network(const Options *options); + Network(xmrig::Controller *controller); ~Network(); void connect(); @@ -58,16 +62,17 @@ protected: private: constexpr static int kTickInterval = 1 * 1000; + bool isColors() const; void setJob(Client *client, const Job &job, bool donate); void tick(); static void onTick(uv_timer_t *handle); - const Options *m_options; IStrategy *m_donate; IStrategy *m_strategy; NetworkState m_state; uv_timer_t m_timer; + xmrig::Controller *m_controller; }; diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index bd5b7df6..ef06eb52 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,8 +27,10 @@ #include #include + +#include "core/Config.h" +#include "core/Controller.h" #include "log/Log.h" -#include "Options.h" #include "workers/Hashrate.h" @@ -43,9 +45,10 @@ inline const char *format(double h, char* buf, size_t size) } -Hashrate::Hashrate(int threads) : +Hashrate::Hashrate(int threads, xmrig::Controller *controller) : m_highest(0.0), - m_threads(threads) + m_threads(threads), + m_controller(controller) { m_counts = new uint64_t*[threads]; m_timestamps = new uint64_t*[threads]; @@ -60,7 +63,7 @@ Hashrate::Hashrate(int threads) : memset(m_timestamps[0], 0, sizeof(uint64_t) * kBucketSize); } - const int printTime = Options::i()->printTime(); + const int printTime = controller->config()->printTime(); if (printTime > 0) { uv_timer_init(uv_default_loop(), &m_timer); @@ -153,7 +156,7 @@ void Hashrate::print() char num3[8]; char num4[8]; - LOG_INFO(Options::i()->colors() ? "\x1B[01;37mspeed\x1B[0m 2.5s/60s/15m \x1B[01;36m%s \x1B[22;36m%s %s \x1B[01;36mH/s\x1B[0m max: \x1B[01;36m%s H/s" : "speed 2.5s/60s/15m %s %s %s H/s max: %s H/s", + LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;37mspeed\x1B[0m 2.5s/60s/15m \x1B[01;36m%s \x1B[22;36m%s %s \x1B[01;36mH/s\x1B[0m max: \x1B[01;36m%s H/s" : "speed 2.5s/60s/15m %s %s %s H/s max: %s H/s", format(calc(ShortInterval), num1, sizeof(num1)), format(calc(MediumInterval), num2, sizeof(num2)), format(calc(LargeInterval), num3, sizeof(num3)), diff --git a/src/workers/Hashrate.h b/src/workers/Hashrate.h index 026c0cdf..5f4f0eaa 100644 --- a/src/workers/Hashrate.h +++ b/src/workers/Hashrate.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,6 +29,11 @@ #include +namespace xmrig { + class Controller; +} + + class Hashrate { public: @@ -38,7 +43,7 @@ public: LargeInterval = 900000 }; - Hashrate(int threads); + Hashrate(int threads, xmrig::Controller *controller); double calc(size_t ms) const; double calc(size_t threadId, size_t ms) const; void add(size_t threadId, uint64_t count, uint64_t timestamp); @@ -61,6 +66,7 @@ private: uint64_t** m_counts; uint64_t** m_timestamps; uv_timer_t m_timer; + xmrig::Controller *m_controller; }; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index f0aef448..5642751a 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,7 +27,6 @@ #include "api/Api.h" #include "interfaces/IJobResultListener.h" #include "Mem.h" -#include "Options.h" #include "workers/DoubleWorker.h" #include "workers/Handle.h" #include "workers/Hashrate.h" @@ -103,10 +102,10 @@ void Workers::setJob(const Job &job, bool donate) } -void Workers::start(int64_t affinity, int priority) +void Workers::start(int64_t affinity, int priority, xmrig::Controller *controller) { const int threads = Mem::threads(); - m_hashrate = new Hashrate(threads); + m_hashrate = new Hashrate(threads, controller); uv_mutex_init(&m_mutex); uv_rwlock_init(&m_rwlock); diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 1c85089a..942a5b58 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,6 +39,11 @@ class Hashrate; class IJobResultListener; +namespace xmrig { + class Controller; +} + + class Workers { public: @@ -46,7 +51,7 @@ public: static void printHashrate(bool detail); static void setEnabled(bool enabled); static void setJob(const Job &job, bool donate); - static void start(int64_t affinity, int priority); + static void start(int64_t affinity, int priority, xmrig::Controller *controller); static void stop(); static void submit(const JobResult &result); From 7f5d7cf7ddf3e204f36db1501ac443ed8319d337 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 31 Mar 2018 16:52:58 +0700 Subject: [PATCH 152/389] Fix Linux build. --- src/App_unix.cpp | 10 ++++++---- src/Mem_unix.cpp | 1 - src/core/Controller.cpp | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/App_unix.cpp b/src/App_unix.cpp index 674a53e6..fdb2b124 100644 --- a/src/App_unix.cpp +++ b/src/App_unix.cpp @@ -29,20 +29,22 @@ #include "App.h" +#include "core/Config.h" +#include "core/Controller.h" #include "Cpu.h" #include "log/Log.h" -#include "Options.h" void App::background() { signal(SIGPIPE, SIG_IGN); - if (m_options->affinity() != -1L) { - Cpu::setAffinity(-1, m_options->affinity()); + const int64_t affinity = m_controller->config()->affinity(); + if (affinity != -1L) { + Cpu::setAffinity(-1, affinity); } - if (!m_options->background()) { + if (!m_controller->config()->isBackground()) { return; } diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 3e699544..3b69f267 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -37,7 +37,6 @@ #include "crypto/CryptoNight.h" #include "log/Log.h" #include "Mem.h" -#include "Options.h" #include "xmrig.h" diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index b8c93c82..35fab2a2 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -109,7 +109,7 @@ int xmrig::Controller::init(int argc, char **argv) } # ifdef HAVE_SYSLOG_H - if (config()->syslog()) { + if (config()->isSyslog()) { Log::add(new SysLog()); } # endif From af0a6fdf2056e311d14009da4b8e31a7f56798a2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 31 Mar 2018 17:51:33 +0700 Subject: [PATCH 153/389] Small fixes. --- src/App.cpp | 2 +- src/Summary.cpp | 6 ++++-- src/core/CommonConfig.cpp | 2 ++ src/core/Controller.cpp | 6 ++++++ src/core/Controller.h | 1 + 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/App.cpp b/src/App.cpp index 612614ae..9bdd381e 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -91,7 +91,7 @@ App::~App() int App::exec() { - if (!m_controller->config()) { + if (!m_controller->isReady()) { return 2; } diff --git a/src/Summary.cpp b/src/Summary.cpp index 4b663fbf..6aa42b74 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -132,11 +132,13 @@ static void print_pools(xmrig::Config *config) #ifndef XMRIG_NO_API static void print_api(xmrig::Config *config) { - if (config->apiPort() == 0) { + const int port = config->apiPort(); + if (port == 0) { return; } - Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mAPI PORT: \x1B[01;36m%d" : " * API PORT: %d", config->apiPort()); + Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mAPI BIND: \x1B[01;36m%s:%d" : " * API BIND: %s:%d", + config->isApiIPv6() ? "[::]" : "0.0.0.0", port); } #endif diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index 38233b12..ea17342c 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -56,6 +56,8 @@ xmrig::CommonConfig::CommonConfig() : m_apiRestricted(true), m_background(false), m_colors(true), + m_syslog(false), + m_watch(false), // TODO: enable config file watch by default when this feature propertly handled and tested. m_apiToken(nullptr), m_apiWorkerId(nullptr), m_fileName(nullptr), diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index 35fab2a2..805861ef 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -79,6 +79,12 @@ xmrig::Controller::~Controller() } +bool xmrig::Controller::isReady() const +{ + return d_ptr->config && d_ptr->network; +} + + xmrig::Config *xmrig::Controller::config() const { assert(d_ptr->config != nullptr); diff --git a/src/core/Controller.h b/src/core/Controller.h index 2ba80f99..25f91843 100644 --- a/src/core/Controller.h +++ b/src/core/Controller.h @@ -46,6 +46,7 @@ public: Controller(); ~Controller(); + bool isReady() const; Config *config() const; int init(int argc, char **argv); Network *network() const; From 341557c34e5074e1c024363c3ff38f873f632b1a Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 31 Mar 2018 18:12:52 +0700 Subject: [PATCH 154/389] Added client storage from proxy. --- CMakeLists.txt | 1 + src/net/Client.cpp | 80 ++++++++++++++++++++------------------ src/net/Client.h | 15 ++++--- src/net/Storage.h | 97 ++++++++++++++++++++++++++++++++++++++++++++++ src/net/Url.h | 1 - 5 files changed, 147 insertions(+), 47 deletions(-) create mode 100644 src/net/Storage.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 28d89a85..1f42950e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ set(HEADERS src/net/Job.h src/net/JobResult.h src/net/Network.h + src/net/Storage.h src/net/strategies/DonateStrategy.h src/net/strategies/FailoverStrategy.h src/net/strategies/SinglePoolStrategy.h diff --git a/src/net/Client.cpp b/src/net/Client.cpp index c8a93f24..ec9dc863 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -52,6 +52,7 @@ int64_t Client::m_sequence = 1; +xmrig::Storage Client::m_storage; Client::Client(int id, const char *agent, IClientListener *listener) : @@ -67,13 +68,17 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_state(UnconnectedState), m_expire(0), m_jobs(0), + m_keepAlive(0), + m_key(0), m_stream(nullptr), m_socket(nullptr) { + m_key = m_storage.add(this); + memset(m_ip, 0, sizeof(m_ip)); memset(&m_hints, 0, sizeof(m_hints)); - m_resolver.data = this; + m_resolver.data = m_storage.ptr(m_key); m_hints.ai_family = AF_UNSPEC; m_hints.ai_socktype = SOCK_STREAM; @@ -81,11 +86,6 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_recvBuf.base = m_buf; m_recvBuf.len = sizeof(m_buf); - -# ifndef XMRIG_PROXY_PROJECT - m_keepAliveTimer.data = this; - uv_timer_init(uv_default_loop(), &m_keepAliveTimer); -# endif } @@ -121,8 +121,13 @@ void Client::deleteLater() m_listener = nullptr; - if (!disconnect()) { - delete this; + if (state() == HostLookupState) { + uv_cancel(reinterpret_cast(&m_resolver)); + return; + } + + if (!disconnect() && m_state != ClosingState) { + m_storage.remove(m_key); } } @@ -139,17 +144,17 @@ void Client::setUrl(const Url *url) void Client::tick(uint64_t now) { - if (m_expire == 0 || now < m_expire) { - return; - } - if (m_state == ConnectedState) { - LOG_DEBUG_ERR("[%s:%u] timeout", m_url.host(), m_url.port()); - close(); + if (m_expire && now > m_expire) { + LOG_DEBUG_ERR("[%s:%u] timeout", m_url.host(), m_url.port()); + close(); + } + else if (m_keepAlive && now > m_keepAlive) { + ping(); + } } - - if (m_state == ConnectingState) { + if (m_expire && now > m_expire && m_state == ConnectingState) { connect(); } } @@ -157,12 +162,9 @@ void Client::tick(uint64_t now) bool Client::disconnect() { -# ifndef XMRIG_PROXY_PROJECT - uv_timer_stop(&m_keepAliveTimer); -# endif - - m_expire = 0; - m_failures = -1; + m_keepAlive = 0; + m_expire = 0; + m_failures = -1; return close(); } @@ -404,10 +406,10 @@ void Client::connect(sockaddr *addr) delete m_socket; uv_connect_t *req = new uv_connect_t; - req->data = this; + req->data = m_storage.ptr(m_key); m_socket = new uv_tcp_t; - m_socket->data = this; + m_socket->data = m_storage.ptr(m_key); uv_tcp_init(uv_default_loop(), m_socket); uv_tcp_nodelay(m_socket, 1); @@ -567,7 +569,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap LOG_ERR("[%s:%u] error: \"%s\", code: %d", m_url.host(), m_url.port(), message, error["code"].GetInt()); } - if (id == 1 || isCriticalError(message)) { + if (isCriticalError(message)) { close(); } @@ -613,18 +615,13 @@ void Client::ping() void Client::reconnect() { if (!m_listener) { - delete this; + m_storage.remove(m_key); return; } setState(ConnectingState); - -# ifndef XMRIG_PROXY_PROJECT - if (m_url.isKeepAlive()) { - uv_timer_stop(&m_keepAliveTimer); - } -# endif + m_keepAlive = 0; if (m_failures == -1) { return m_listener->onClose(this, -1); @@ -653,13 +650,9 @@ void Client::startTimeout() { m_expire = 0; -# ifndef XMRIG_PROXY_PROJECT - if (!m_url.isKeepAlive()) { - return; + if (m_url.keepAlive()) { + m_keepAlive = uv_now(uv_default_loop()) + (m_url.keepAlive() * 1000); } - - uv_timer_start(&m_keepAliveTimer, [](uv_timer_t *handle) { getClient(handle->data)->ping(); }, kKeepAliveTimeout, 0); -# endif } @@ -690,6 +683,7 @@ void Client::onConnect(uv_connect_t *req, int status) { auto client = getClient(req->data); if (!client) { + delete req; return; } @@ -735,6 +729,11 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) return; } + assert(client->m_listener != nullptr); + if (!client->m_listener) { + return client->reconnect(); + } + client->m_recvBufPos += nread; char* end; @@ -771,6 +770,11 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) return; } + assert(client->m_listener != nullptr); + if (!client->m_listener) { + return client->reconnect(); + } + if (status < 0) { if (!client->m_quiet) { LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); diff --git a/src/net/Client.h b/src/net/Client.h index fff7a156..fc0335c6 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -32,6 +32,7 @@ #include "net/Id.h" #include "net/Job.h" +#include "net/Storage.h" #include "net/SubmitResult.h" #include "net/Url.h" #include "rapidjson/fwd.h" @@ -53,9 +54,9 @@ public: }; constexpr static int kResponseTimeout = 20 * 1000; - constexpr static int kKeepAliveTimeout = 60 * 1000; Client(int id, const char *agent, IClientListener *listener); + ~Client(); bool disconnect(); int64_t submit(const JobResult &result); @@ -76,8 +77,6 @@ public: inline void setRetryPause(int ms) { m_retryPause = ms; } private: - ~Client(); - bool close(); bool isCriticalError(const char *message); bool parseJob(const rapidjson::Value ¶ms, int *code); @@ -103,7 +102,7 @@ private: static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); static void onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res); - static inline Client *getClient(void *data) { return static_cast(data); } + static inline Client *getClient(void *data) { return m_storage.get(data); } addrinfo m_hints; bool m_ipv6; @@ -120,10 +119,11 @@ private: Job m_job; size_t m_recvBufPos; SocketState m_state; - static int64_t m_sequence; std::map m_results; uint64_t m_expire; uint64_t m_jobs; + uint64_t m_keepAlive; + uintptr_t m_key; Url m_url; uv_buf_t m_recvBuf; uv_getaddrinfo_t m_resolver; @@ -131,9 +131,8 @@ private: uv_tcp_t *m_socket; xmrig::Id m_rpcId; -# ifndef XMRIG_PROXY_PROJECT - uv_timer_t m_keepAliveTimer; -# endif + static int64_t m_sequence; + static xmrig::Storage m_storage; }; diff --git a/src/net/Storage.h b/src/net/Storage.h new file mode 100644 index 00000000..105547ec --- /dev/null +++ b/src/net/Storage.h @@ -0,0 +1,97 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __STORAGE_H__ +#define __STORAGE_H__ + + +#include +#include + +#include "log/Log.h" + + +namespace xmrig { + + +template +class Storage +{ +public: + inline Storage() : + m_counter(0) + { + } + + + inline uintptr_t add(TYPE *ptr) + { + m_data[m_counter] = ptr; + + return m_counter++; + } + + + inline static void *ptr(uintptr_t id) { return reinterpret_cast(id); } + + + inline TYPE *get(void *id) const { return get(reinterpret_cast(id)); } + inline TYPE *get(uintptr_t id) const + { + assert(m_data.count(id) > 0); + + if (m_data.count(id) == 0) { + return nullptr; + } + + return m_data.at(id); + } + + + inline void remove(void *id) { remove(reinterpret_cast(id)); } + inline void remove(uintptr_t id) + { + TYPE *obj = get(id); + if (obj == nullptr) { + return; + } + + auto it = m_data.find(id); + if (it != m_data.end()) { + m_data.erase(it); + } + + delete obj; + } + + +private: + std::map m_data; + uint64_t m_counter; +}; + + +} /* namespace xmrig */ + + +#endif /* __STORAGE_H__ */ diff --git a/src/net/Url.h b/src/net/Url.h index 45db4457..4c2c9435 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -41,7 +41,6 @@ public: Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, int keepAlive = 0, bool nicehash = false, int variant = -1); ~Url(); - inline bool isKeepAlive() const { return m_keepAlive > 0; } // FIXME: replace isKeepAlive to keepAlive inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_host && m_port > 0; } inline const char *host() const { return m_host; } From 8d4d1a3285ad4888fc09d89b60552e3f9b591342 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 31 Mar 2018 19:00:31 +0700 Subject: [PATCH 155/389] Added API endpoint "GET /1/config". --- src/api/ApiRouter.cpp | 11 ++++ src/core/Config.cpp | 110 +++++++++++++++++-------------- src/core/ConfigLoader_platform.h | 2 +- 3 files changed, 74 insertions(+), 49 deletions(-) diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index bb8c7402..e1ba9e17 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -89,6 +89,17 @@ void ApiRouter::ApiRouter::get(const xmrig::HttpRequest &req, xmrig::HttpReply & rapidjson::Document doc; doc.SetObject(); + if (req.match("/1/config")) { + if (req.isRestricted()) { + reply.status = 403; + return; + } + + m_controller->config()->getJSON(doc); + + return finalize(reply, doc); + } + getIdentify(doc); getMiner(doc); getHashrate(doc); diff --git a/src/core/Config.cpp b/src/core/Config.cpp index f30ed1f4..ec7e5de1 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -23,18 +23,23 @@ #include #include +#include #include "core/Config.h" #include "core/ConfigCreator.h" #include "core/ConfigLoader.h" #include "Cpu.h" +#include "net/Url.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" #include "xmrig.h" +static char affinity_tmp[20] = { 0 }; + + xmrig::Config::Config() : xmrig::CommonConfig(), m_doubleHash(false), m_dryRun(false), @@ -66,69 +71,78 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const { doc.SetObject(); -// auto &allocator = doc.GetAllocator(); + auto &allocator = doc.GetAllocator(); -// doc.AddMember("access-log-file", accessLog() ? rapidjson::Value(rapidjson::StringRef(accessLog())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); -// doc.AddMember("algo", rapidjson::StringRef(algoName()), allocator); + doc.AddMember("algo", rapidjson::StringRef(algoName()), allocator); -// rapidjson::Value api(rapidjson::kObjectType); -// api.AddMember("port", apiPort(), allocator); -// api.AddMember("access-token", apiToken() ? rapidjson::Value(rapidjson::StringRef(apiToken())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); -// api.AddMember("worker-id", apiWorkerId() ? rapidjson::Value(rapidjson::StringRef(apiWorkerId())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); -// api.AddMember("ipv6", isApiIPv6(), allocator); -// api.AddMember("restricted", isApiRestricted(), allocator); -// doc.AddMember("api", api, allocator); + rapidjson::Value api(rapidjson::kObjectType); + api.AddMember("port", apiPort(), allocator); + api.AddMember("access-token", apiToken() ? rapidjson::Value(rapidjson::StringRef(apiToken())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); + api.AddMember("worker-id", apiWorkerId() ? rapidjson::Value(rapidjson::StringRef(apiWorkerId())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); + api.AddMember("ipv6", isApiIPv6(), allocator); + api.AddMember("restricted", isApiRestricted(), allocator); + doc.AddMember("api", api, allocator); -// doc.AddMember("background", isBackground(), allocator); + doc.AddMember("av", algoVariant(), allocator); + doc.AddMember("background", isBackground(), allocator); -// rapidjson::Value bind(rapidjson::kArrayType); -// for (const Addr *addr : m_addrs) { -// bind.PushBack(rapidjson::StringRef(addr->addr()), allocator); -// } + doc.AddMember("colors", isColors(), allocator); -// doc.AddMember("bind", bind, allocator); -// doc.AddMember("colors", isColors(), allocator); -// doc.AddMember("custom-diff", diff(), allocator); -// doc.AddMember("donate-level", donateLevel(), allocator); -// doc.AddMember("log-file", logFile() ? rapidjson::Value(rapidjson::StringRef(logFile())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); -// doc.AddMember("mode", rapidjson::StringRef(modeName()), allocator); + if (affinity() != -1L) { + snprintf(affinity_tmp, sizeof(affinity_tmp) - 1, "0x%" PRIX64, affinity()); + doc.AddMember("cpu-affinity", rapidjson::StringRef(affinity_tmp), allocator); + } + else { + doc.AddMember("cpu-affinity", rapidjson::kNullType, allocator); + } -// rapidjson::Value pools(rapidjson::kArrayType); + if (priority() != -1) { + doc.AddMember("cpu-priority", priority(), allocator); + } + else { + doc.AddMember("cpu-priority", rapidjson::kNullType, allocator); + } -// for (const Url *url : m_pools) { -// rapidjson::Value obj(rapidjson::kObjectType); + doc.AddMember("donate-level", donateLevel(), allocator); + doc.AddMember("huge-pages", isHugePages(), allocator); + doc.AddMember("log-file", logFile() ? rapidjson::Value(rapidjson::StringRef(logFile())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); + doc.AddMember("max-cpu-usage", m_maxCpuUsage, allocator); -// obj.AddMember("url", rapidjson::StringRef(url->url()), allocator); -// obj.AddMember("user", rapidjson::StringRef(url->user()), allocator); -// obj.AddMember("pass", rapidjson::StringRef(url->password()), allocator); -// obj.AddMember("coin", rapidjson::StringRef(url->coin()), allocator); + rapidjson::Value pools(rapidjson::kArrayType); -// if (url->keepAlive() == 0 || url->keepAlive() == Url::kKeepAliveTimeout) { -// obj.AddMember("keepalive", url->keepAlive() > 0, allocator); -// } -// else { -// obj.AddMember("keepalive", url->keepAlive(), allocator); -// } + for (const Url *url : m_pools) { + rapidjson::Value obj(rapidjson::kObjectType); -// obj.AddMember("variant", url->variant(), allocator); + obj.AddMember("url", rapidjson::StringRef(url->url()), allocator); + obj.AddMember("user", rapidjson::StringRef(url->user()), allocator); + obj.AddMember("pass", rapidjson::StringRef(url->password()), allocator); -// pools.PushBack(obj, allocator); -// } + if (url->keepAlive() == 0 || url->keepAlive() == Url::kKeepAliveTimeout) { + obj.AddMember("keepalive", url->keepAlive() > 0, allocator); + } + else { + obj.AddMember("keepalive", url->keepAlive(), allocator); + } -// doc.AddMember("pools", pools, allocator); + obj.AddMember("nicehash", url->isNicehash(), allocator); + obj.AddMember("variant", url->variant(), allocator); -// doc.AddMember("retries", retries(), allocator); -// doc.AddMember("retry-pause", retryPause(), allocator); -// doc.AddMember("reuse-timeout", reuseTimeout(), allocator); -// doc.AddMember("user-agent", userAgent() ? rapidjson::Value(rapidjson::StringRef(userAgent())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); + pools.PushBack(obj, allocator); + } -//# ifdef HAVE_SYSLOG_H -// doc.AddMember("syslog", syslog(), allocator); -//# endif + doc.AddMember("pools", pools, allocator); + doc.AddMember("print-time", printTime(), allocator); + doc.AddMember("retries", retries(), allocator); + doc.AddMember("retry-pause", retryPause(), allocator); + doc.AddMember("safe", m_safe, allocator); + doc.AddMember("threads", threads(), allocator); + doc.AddMember("user-agent", userAgent() ? rapidjson::Value(rapidjson::StringRef(userAgent())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); -// doc.AddMember("verbose", isVerbose(), allocator); -// doc.AddMember("watch", m_watch, allocator); -// doc.AddMember("workers", isWorkers(), allocator); +# ifdef HAVE_SYSLOG_H + doc.AddMember("syslog", syslog(), allocator); +# endif + + doc.AddMember("watch", m_watch, allocator); } diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index 59415f78..68eef639 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -138,7 +138,7 @@ static struct option const config_options[] = { { "retry-pause", 1, nullptr, xmrig::IConfig::RetryPauseKey }, { "safe", 0, nullptr, xmrig::IConfig::SafeKey }, { "syslog", 0, nullptr, xmrig::IConfig::SyslogKey }, - { "threads", 1, nullptr, xmrig::IConfig::SafeKey }, + { "threads", 1, nullptr, xmrig::IConfig::ThreadsKey }, { "user-agent", 1, nullptr, xmrig::IConfig::UserAgentKey }, { 0, 0, 0, 0 } }; From edd47b12a8b3343476cc02cf4d3102bbf3018ae7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 31 Mar 2018 19:43:16 +0700 Subject: [PATCH 156/389] Update CHANGELOG.md and version. --- CHANGELOG.md | 9 +++++++++ src/version.h | 8 ++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab31e698..f9f42d66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +# v2.6.0-beta1 + - HTTP server now runs in main loop, it make possible easy extend API without worry about thread synchronization. + - Added initial graceful reload support, miner will reload configuration if config file changed, disabled by default until it will be fully implemented and tested. + - Added API endpoint `PUT /1/config` to update current config. + - Added API endpoint `GET /1/config` to get current active config. + - API endpoint `GET /` now deprecated, use `GET /1/summary` instead. + - Added `--api-no-ipv6` and similar config option to disable IPv6 support for HTTP API. + - Added `--api-no-restricted` to enable full access to api, this option has no effect if `--api-access-token` not specified. + # v2.5.2 - [#448](https://github.com/xmrig/xmrig/issues/478) Fixed broken reconnect. diff --git a/src/version.h b/src/version.h index 060edcb7..db7c2013 100644 --- a/src/version.h +++ b/src/version.h @@ -27,16 +27,16 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.5.2" +#define APP_VERSION "2.6.0-beta1" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" #define APP_KIND "cpu" #define APP_VER_MAJOR 2 -#define APP_VER_MINOR 5 -#define APP_VER_BUILD 2 -#define APP_VER_REV 0 +#define APP_VER_MINOR 6 +#define APP_VER_BUILD 0 +#define APP_VER_REV 1 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From 44d56393dbb14bee4e7470de07f9bf3e3c0bf324 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 31 Mar 2018 20:26:07 +0700 Subject: [PATCH 157/389] Fix Linux build. --- src/core/Config.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index ec7e5de1..49b40aa8 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -139,7 +139,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("user-agent", userAgent() ? rapidjson::Value(rapidjson::StringRef(userAgent())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); # ifdef HAVE_SYSLOG_H - doc.AddMember("syslog", syslog(), allocator); + doc.AddMember("syslog", isSyslog(), allocator); # endif doc.AddMember("watch", m_watch, allocator); From a042cbf8856de5df95bac73d7413e63483a56db7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 1 Apr 2018 22:49:21 +0700 Subject: [PATCH 158/389] Added classes IThread, CpuThread and API endpoint "GET /1/threads". --- CMakeLists.txt | 3 ++ src/App.cpp | 2 +- src/Mem.cpp | 2 +- src/Mem_win.cpp | 2 +- src/Summary.cpp | 2 +- src/api/ApiRouter.cpp | 26 ++++++++-- src/api/ApiRouter.h | 1 + src/core/CommonConfig.cpp | 6 +-- src/core/CommonConfig.h | 5 +- src/core/Config.cpp | 58 ++++++++++++---------- src/core/Config.h | 41 +++++++--------- src/crypto/CryptoNight.cpp | 6 +-- src/interfaces/IThread.h | 64 ++++++++++++++++++++++++ src/net/Job.cpp | 4 +- src/net/Url.cpp | 6 +-- src/net/strategies/DonateStrategy.cpp | 2 +- src/workers/CpuThread.cpp | 68 +++++++++++++++++++++++++ src/workers/CpuThread.h | 71 +++++++++++++++++++++++++++ src/xmrig.h | 29 ++++++++--- 19 files changed, 320 insertions(+), 78 deletions(-) create mode 100644 src/interfaces/IThread.h create mode 100644 src/workers/CpuThread.cpp create mode 100644 src/workers/CpuThread.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f42950e..c9d6624f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ set(HEADERS src/interfaces/ILogBackend.h src/interfaces/IStrategy.h src/interfaces/IStrategyListener.h + src/interfaces/IThread.h src/interfaces/IWatcherListener.h src/interfaces/IWorker.h src/log/ConsoleLog.h @@ -52,6 +53,7 @@ set(HEADERS src/Platform.h src/Summary.h src/version.h + src/workers/CpuThread.h src/workers/DoubleWorker.h src/workers/Handle.h src/workers/Hashrate.h @@ -106,6 +108,7 @@ set(SOURCES src/net/Url.cpp src/Platform.cpp src/Summary.cpp + src/workers/CpuThread.cpp src/workers/DoubleWorker.cpp src/workers/Handle.cpp src/workers/Hashrate.cpp diff --git a/src/App.cpp b/src/App.cpp index 9bdd381e..a9d10774 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -107,7 +107,7 @@ int App::exec() } Mem::allocate(m_controller->config()->algorithm(), - m_controller->config()->threads(), + m_controller->config()->threadsCount(), m_controller->config()->isDoubleHash(), m_controller->config()->isHugePages() ); diff --git a/src/Mem.cpp b/src/Mem.cpp index 991f4398..f5da7865 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -43,7 +43,7 @@ alignas(16) uint8_t *Mem::m_memory = nullptr; cryptonight_ctx *Mem::create(int threadId) { # ifndef XMRIG_NO_AEON - if (m_algo == xmrig::ALGO_CRYPTONIGHT_LITE) { + if (m_algo == xmrig::CRYPTONIGHT_LITE) { return createLite(threadId); } # endif diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 1b35c704..6cc0a6ee 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -151,7 +151,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_threads = threads; m_doubleHash = doubleHash; - const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; + const int ratio = (doubleHash && algo != xmrig::CRYPTONIGHT_LITE) ? 2 : 1; m_size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { diff --git a/src/Summary.cpp b/src/Summary.cpp index 6aa42b74..7e341ce0 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -101,7 +101,7 @@ static void print_threads(xmrig::Config *config) } Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mTHREADS: \x1B[01;36m%d\x1B[01;37m, %s, av=%d, %sdonate=%d%%%s" : " * THREADS: %d, %s, av=%d, %sdonate=%d%%%s", - config->threads(), + config->threadsCount(), config->algoName(), config->algoVariant(), config->isColors() && config->donateLevel() == 0 ? "\x1B[01;31m" : "", diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index e1ba9e17..6c4f1f27 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -38,6 +38,7 @@ #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" +#include "interfaces/IThread.h" #include "Mem.h" #include "net/Job.h" #include "Platform.h" @@ -67,7 +68,7 @@ static inline double normalize(double d) ApiRouter::ApiRouter(xmrig::Controller *controller) : m_controller(controller) { - m_threads = controller->config()->threads(); + m_threads = controller->config()->threadsCount(); m_hashrate = new double[m_threads * 3](); memset(m_totalHashrate, 0, sizeof(m_totalHashrate)); @@ -87,7 +88,6 @@ ApiRouter::~ApiRouter() void ApiRouter::ApiRouter::get(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) const { rapidjson::Document doc; - doc.SetObject(); if (req.match("/1/config")) { if (req.isRestricted()) { @@ -100,6 +100,14 @@ void ApiRouter::ApiRouter::get(const xmrig::HttpRequest &req, xmrig::HttpReply & return finalize(reply, doc); } + if (req.match("/1/threads")) { + getThreads(doc); + + return finalize(reply, doc); + } + + doc.SetObject(); + getIdentify(doc); getMiner(doc); getHashrate(doc); @@ -144,7 +152,7 @@ void ApiRouter::tick(const NetworkState &network) void ApiRouter::onConfigChanged(xmrig::Config *config, xmrig::Config *previousConfig) { -// updateWorkerId(config->apiWorkerId(), previousConfig->apiWorkerId()); + updateWorkerId(config->apiWorkerId(), previousConfig->apiWorkerId()); } @@ -288,6 +296,18 @@ void ApiRouter::getResults(rapidjson::Document &doc) const } +void ApiRouter::getThreads(rapidjson::Document &doc) const +{ + doc.SetArray(); + + const std::vector &threads = m_controller->config()->threads(); + + for (const xmrig::IThread *thread : threads) { + doc.PushBack(thread->toAPI(doc), doc.GetAllocator()); + } +} + + void ApiRouter::setWorkerId(const char *id) { memset(m_workerId, 0, sizeof(m_workerId)); diff --git a/src/api/ApiRouter.h b/src/api/ApiRouter.h index 2ae1cc80..e14f9e87 100644 --- a/src/api/ApiRouter.h +++ b/src/api/ApiRouter.h @@ -63,6 +63,7 @@ private: void getIdentify(rapidjson::Document &doc) const; void getMiner(rapidjson::Document &doc) const; void getResults(rapidjson::Document &doc) const; + void getThreads(rapidjson::Document &doc) const; void setWorkerId(const char *id); void updateWorkerId(const char *id, const char *previousId); diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index ea17342c..51448654 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -51,6 +51,7 @@ static const char *algoNames[] = { xmrig::CommonConfig::CommonConfig() : + m_algorithm(CRYPTONIGHT), m_adjusted(false), m_apiIPv6(true), m_apiRestricted(true), @@ -63,7 +64,6 @@ xmrig::CommonConfig::CommonConfig() : m_fileName(nullptr), m_logFile(nullptr), m_userAgent(nullptr), - m_algorithm(ALGO_CRYPTONIGHT), m_apiPort(0), m_donateLevel(kDefaultDonateLevel), m_printTime(60), @@ -367,7 +367,7 @@ void xmrig::CommonConfig::setAlgo(const char *algo) if (strcasecmp(algo, "cryptonight-light") == 0) { fprintf(stderr, "Algorithm \"cryptonight-light\" is deprecated, use \"cryptonight-lite\" instead\n"); - m_algorithm = ALGO_CRYPTONIGHT_LITE; + m_algorithm = CRYPTONIGHT_LITE; return; } @@ -375,7 +375,7 @@ void xmrig::CommonConfig::setAlgo(const char *algo) for (size_t i = 0; i < size; i++) { if (algoNames[i] && strcasecmp(algo, algoNames[i]) == 0) { - m_algorithm = (int) i; + m_algorithm = static_cast(i); break; } } diff --git a/src/core/CommonConfig.h b/src/core/CommonConfig.h index ab840a48..0f6f6b8c 100644 --- a/src/core/CommonConfig.h +++ b/src/core/CommonConfig.h @@ -29,6 +29,7 @@ #include "interfaces/IConfig.h" +#include "xmrig.h" class Url; @@ -45,6 +46,7 @@ public: const char *algoName() const; + inline Algo algorithm() const { return m_algorithm; } inline bool isApiIPv6() const { return m_apiIPv6; } inline bool isApiRestricted() const { return m_apiRestricted; } inline bool isBackground() const { return m_background; } @@ -55,7 +57,6 @@ public: inline const char *logFile() const { return m_logFile; } inline const char *userAgent() const { return m_userAgent; } inline const std::vector &pools() const { return m_pools; } - inline int algorithm() const { return m_algorithm; } inline int apiPort() const { return m_apiPort; } inline int donateLevel() const { return m_donateLevel; } inline int printTime() const { return m_printTime; } @@ -75,6 +76,7 @@ protected: bool save() override; void setFileName(const char *fileName) override; + Algo m_algorithm; bool m_adjusted; bool m_apiIPv6; bool m_apiRestricted; @@ -87,7 +89,6 @@ protected: char *m_fileName; char *m_logFile; char *m_userAgent; - int m_algorithm; int m_apiPort; int m_donateLevel; int m_printTime; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 49b40aa8..6bc2dc0f 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -34,6 +34,7 @@ #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" +#include "workers/CpuThread.h" #include "xmrig.h" @@ -41,18 +42,17 @@ static char affinity_tmp[20] = { 0 }; xmrig::Config::Config() : xmrig::CommonConfig(), + m_algoVariant(AV_AUTO), m_doubleHash(false), m_dryRun(false), m_hugePages(true), m_safe(false), - m_algoVariant(0), m_maxCpuUsage(75), m_printTime(60), m_priority(-1), - m_threads(0), - m_affinity(-1L) + m_affinity(-1L), + m_threadsCount(0) { - } @@ -135,7 +135,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("retries", retries(), allocator); doc.AddMember("retry-pause", retryPause(), allocator); doc.AddMember("safe", m_safe, allocator); - doc.AddMember("threads", threads(), allocator); + doc.AddMember("threads", threadsCount(), allocator); doc.AddMember("user-agent", userAgent() ? rapidjson::Value(rapidjson::StringRef(userAgent())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); # ifdef HAVE_SYSLOG_H @@ -159,20 +159,24 @@ bool xmrig::Config::adjust() } m_algoVariant = getAlgoVariant(); - if (m_algoVariant == AV2_AESNI_DOUBLE || m_algoVariant == AV4_SOFT_AES_DOUBLE) { + if (m_algoVariant == AV_DOUBLE || m_algoVariant == AV_DOUBLE_SOFT) { m_doubleHash = true; } - if (!m_threads) { - m_threads = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); + if (!m_threadsCount) { + m_threadsCount = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); } else if (m_safe) { - const int count = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); - if (m_threads > count) { - m_threads = count; + const size_t count = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); + if (m_threadsCount > count) { + m_threadsCount = count; } } + for (size_t i = 0; i < m_threadsCount; ++i) { + m_threads.push_back(CpuThread::createFromAV(i, m_algorithm, m_algoVariant, m_affinity, m_priority)); + } + return true; } @@ -225,7 +229,7 @@ bool xmrig::Config::parseString(int key, const char *arg) case xmrig::IConfig::ThreadsKey: /* --threads */ if (strncmp(arg, "all", 3) == 0) { - m_threads = Cpu::threads(); + m_threadsCount = Cpu::threads(); return true; } @@ -275,14 +279,14 @@ bool xmrig::Config::parseInt(int key, int arg) { switch (key) { case xmrig::IConfig::ThreadsKey: /* --threads */ - if (m_threads >= 0 && arg < 1024) { - m_threads = arg; + if (m_threadsCount >= 0 && arg < 1024) { + m_threadsCount = arg; } break; case xmrig::IConfig::AVKey: /* --av */ - if (arg >= AV0_AUTO && arg < AV_MAX) { - m_algoVariant = arg; + if (arg >= AV_AUTO && arg < AV_MAX) { + m_algoVariant = static_cast(arg); } break; @@ -306,20 +310,20 @@ bool xmrig::Config::parseInt(int key, int arg) } -int xmrig::Config::getAlgoVariant() const +xmrig::AlgoVariant xmrig::Config::getAlgoVariant() const { # ifndef XMRIG_NO_AEON - if (m_algorithm == xmrig::ALGO_CRYPTONIGHT_LITE) { + if (m_algorithm == xmrig::CRYPTONIGHT_LITE) { return getAlgoVariantLite(); } # endif - if (m_algoVariant <= AV0_AUTO || m_algoVariant >= AV_MAX) { - return Cpu::hasAES() ? AV1_AESNI : AV3_SOFT_AES; + if (m_algoVariant <= AV_AUTO || m_algoVariant >= AV_MAX) { + return Cpu::hasAES() ? AV_SINGLE : AV_SINGLE_SOFT; } - if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV2_AESNI_DOUBLE) { - return m_algoVariant + 2; + if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV_DOUBLE) { + return static_cast(m_algoVariant + 2); } return m_algoVariant; @@ -327,14 +331,14 @@ int xmrig::Config::getAlgoVariant() const #ifndef XMRIG_NO_AEON -int xmrig::Config::getAlgoVariantLite() const +xmrig::AlgoVariant xmrig::Config::getAlgoVariantLite() const { - if (m_algoVariant <= AV0_AUTO || m_algoVariant >= AV_MAX) { - return Cpu::hasAES() ? AV2_AESNI_DOUBLE : AV4_SOFT_AES_DOUBLE; + if (m_algoVariant <= AV_AUTO || m_algoVariant >= AV_MAX) { + return Cpu::hasAES() ? AV_DOUBLE : AV_DOUBLE_SOFT; } - if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV2_AESNI_DOUBLE) { - return m_algoVariant + 2; + if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV_DOUBLE) { + return static_cast(m_algoVariant + 2); } return m_algoVariant; diff --git a/src/core/Config.h b/src/core/Config.h index 110c61a7..536e1c01 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -29,8 +29,9 @@ #include -#include "rapidjson/fwd.h" #include "core/CommonConfig.h" +#include "rapidjson/fwd.h" +#include "xmrig.h" class Addr; @@ -41,6 +42,7 @@ namespace xmrig { class ConfigLoader; +class IThread; class IWatcherListener; @@ -57,18 +59,7 @@ class IWatcherListener; */ class Config : public CommonConfig { - friend class ConfigLoader; - public: - enum AlgoVariant { - AV0_AUTO, - AV1_AESNI, - AV2_AESNI_DOUBLE, - AV3_SOFT_AES, - AV4_SOFT_AES_DOUBLE, - AV_MAX - }; - Config(); ~Config(); @@ -76,14 +67,15 @@ public: void getJSON(rapidjson::Document &doc) const override; - inline bool isDoubleHash() const { return m_doubleHash; } - inline bool isDryRun() const { return m_dryRun; } - inline bool isHugePages() const { return m_hugePages; } - inline int algoVariant() const { return m_algoVariant; } - inline int printTime() const { return m_printTime; } - inline int priority() const { return m_priority; } - inline int threads() const { return m_threads; } - inline int64_t affinity() const { return m_affinity; } + inline AlgoVariant algoVariant() const { return m_algoVariant; } + inline bool isDoubleHash() const { return m_doubleHash; } + inline bool isDryRun() const { return m_dryRun; } + inline bool isHugePages() const { return m_hugePages; } + inline const std::vector &threads() const { return m_threads; } + inline int printTime() const { return m_printTime; } + inline int priority() const { return m_priority; } + inline int threadsCount() const { return m_threadsCount; } + inline int64_t affinity() const { return m_affinity; } static Config *load(int argc, char **argv, IWatcherListener *listener); @@ -97,21 +89,22 @@ protected: private: bool parseInt(int key, int arg); - int getAlgoVariant() const; + AlgoVariant getAlgoVariant() const; # ifndef XMRIG_NO_AEON - int getAlgoVariantLite() const; + AlgoVariant getAlgoVariantLite() const; # endif + AlgoVariant m_algoVariant; bool m_doubleHash; bool m_dryRun; bool m_hugePages; bool m_safe; - int m_algoVariant; int m_maxCpuUsage; int m_printTime; int m_priority; - int m_threads; int64_t m_affinity; + size_t m_threadsCount; + std::vector m_threads; }; diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index c9a2e499..f7dc1a37 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -137,7 +137,7 @@ bool CryptoNight::init(int algo, int variant, bool doubleHash) } # ifndef XMRIG_NO_AEON - const int index = algo == xmrig::ALGO_CRYPTONIGHT_LITE ? (variant + 3) : (variant - 1); + const int index = algo == xmrig::CRYPTONIGHT_LITE ? (variant + 3) : (variant - 1); # else const int index = variant - 1; # endif @@ -167,7 +167,7 @@ bool CryptoNight::selfTest(int algo, bool doubleHash) { cryptonight_hash_ctx(test_input, 76, output, ctx, 0); # ifndef XMRIG_NO_AEON - bool rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output_v0_lite : test_output_v0, (doubleHash ? 64 : 32)) == 0; + bool rc = memcmp(output, algo == xmrig::CRYPTONIGHT_LITE ? test_output_v0_lite : test_output_v0, (doubleHash ? 64 : 32)) == 0; # else bool rc = memcmp(output, test_output_v0, (doubleHash ? 64 : 32)) == 0; # endif @@ -176,7 +176,7 @@ bool CryptoNight::selfTest(int algo, bool doubleHash) { cryptonight_hash_ctx(test_input, 76, output, ctx, 1); # ifndef XMRIG_NO_AEON - rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output_v1_lite : test_output_v1, (doubleHash ? 64 : 32)) == 0; + rc = memcmp(output, algo == xmrig::CRYPTONIGHT_LITE ? test_output_v1_lite : test_output_v1, (doubleHash ? 64 : 32)) == 0; # else rc = memcmp(output, test_output_v1, (doubleHash ? 64 : 32)) == 0; # endif diff --git a/src/interfaces/IThread.h b/src/interfaces/IThread.h new file mode 100644 index 00000000..e2325c72 --- /dev/null +++ b/src/interfaces/IThread.h @@ -0,0 +1,64 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2018 XMRig + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __ITHREAD_H__ +#define __ITHREAD_H__ + + +#include + + +#include "rapidjson/fwd.h" +#include "xmrig.h" + + +namespace xmrig { + + +class IThread +{ +public: + enum Type { + CPU, + OpenCL, + CUDA + }; + + virtual ~IThread() {} + + virtual Algo algorithm() const = 0; + virtual int multiway() const = 0; + virtual int priority() const = 0; + virtual int64_t affinity() const = 0; + virtual size_t index() const = 0; + virtual Type type() const = 0; + +# ifndef XMRIG_NO_API + virtual rapidjson::Value toAPI(rapidjson::Document &doc) const = 0; +# endif +}; + + +} /* namespace xmrig */ + + +#endif // __ITHREAD_H__ diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 7d137fac..e5714376 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -59,7 +59,7 @@ static inline char hf_bin2hex(unsigned char c) Job::Job() : m_nicehash(false), m_coin(), - m_algo(xmrig::ALGO_CRYPTONIGHT), + m_algo(xmrig::CRYPTONIGHT), m_poolId(-2), m_threadId(-1), m_variant(xmrig::VARIANT_AUTO), @@ -164,7 +164,7 @@ void Job::setCoin(const char *coin) } strncpy(m_coin, coin, sizeof(m_coin)); - m_algo = strcmp(m_coin, "AEON") == 0 ? xmrig::ALGO_CRYPTONIGHT_LITE : xmrig::ALGO_CRYPTONIGHT; + m_algo = strcmp(m_coin, "AEON") == 0 ? xmrig::CRYPTONIGHT_LITE : xmrig::CRYPTONIGHT; } diff --git a/src/net/Url.cpp b/src/net/Url.cpp index 8905e919..3d5f2cdd 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -41,7 +41,7 @@ Url::Url() : m_host(nullptr), m_password(nullptr), m_user(nullptr), - m_algo(xmrig::ALGO_CRYPTONIGHT), + m_algo(xmrig::CRYPTONIGHT), m_keepAlive(0), m_variant(xmrig::VARIANT_AUTO), m_url(nullptr), @@ -66,7 +66,7 @@ Url::Url(const char *url) : m_host(nullptr), m_password(nullptr), m_user(nullptr), - m_algo(xmrig::ALGO_CRYPTONIGHT), + m_algo(xmrig::CRYPTONIGHT), m_keepAlive(0), m_variant(xmrig::VARIANT_AUTO), m_url(nullptr), @@ -80,7 +80,7 @@ Url::Url(const char *host, uint16_t port, const char *user, const char *password m_nicehash(nicehash), m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), - m_algo(xmrig::ALGO_CRYPTONIGHT), + m_algo(xmrig::CRYPTONIGHT), m_keepAlive(keepAlive), m_variant(variant), m_url(nullptr), diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index ae707e21..49503820 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -59,7 +59,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL keccak(reinterpret_cast(user), static_cast(strlen(user)), hash, sizeof(hash)); Job::toHex(hash, 32, userId); - if (algo == xmrig::ALGO_CRYPTONIGHT) { + if (algo == xmrig::CRYPTONIGHT) { m_pools.push_back(new Url(kDonatePool1, 6666, userId, nullptr, false, true)); m_pools.push_back(new Url(kDonatePool1, 80, userId, nullptr, false, true)); m_pools.push_back(new Url(kDonatePool2, 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp new file mode 100644 index 00000000..ff3aaed7 --- /dev/null +++ b/src/workers/CpuThread.cpp @@ -0,0 +1,68 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "rapidjson/document.h" +#include "workers/CpuThread.h" + + +xmrig::CpuThread::CpuThread(size_t index, Algo algorithm, int multiway, int64_t affinity, int priority, bool softAES, bool prefetch) : + m_algorithm(algorithm), + m_prefetch(prefetch), + m_softAES(softAES), + m_multiway(multiway), + m_priority(priority), + m_affinity(affinity), + m_index(index) +{ +} + + +xmrig::CpuThread::~CpuThread() +{ +} + + +xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority) +{ + return new CpuThread(index, algorithm, 1, affinity, priority, false, false); +} + + +#ifndef XMRIG_NO_API +rapidjson::Value xmrig::CpuThread::toAPI(rapidjson::Document &doc) const +{ + rapidjson::Value obj(rapidjson::kObjectType); + auto &allocator = doc.GetAllocator(); + + obj.AddMember("type", "cpu", allocator); + obj.AddMember("algo", algorithm(), allocator); + obj.AddMember("low_power_mode", multiway(), allocator); + obj.AddMember("affine_to_cpu", affinity(), allocator); + obj.AddMember("priority", priority(), allocator); + obj.AddMember("prefetch", isPrefetch(), allocator); + obj.AddMember("soft_aes", isSoftAES(), allocator); + + return obj; +} +#endif diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h new file mode 100644 index 00000000..93ef50d5 --- /dev/null +++ b/src/workers/CpuThread.h @@ -0,0 +1,71 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CPUTHREAD_H__ +#define __CPUTHREAD_H__ + + +#include "interfaces/IThread.h" +#include "xmrig.h" + + +namespace xmrig { + + +class CpuThread : public IThread +{ +public: + CpuThread(size_t index, Algo algorithm, int multiway, int64_t affinity, int priority, bool softAES, bool prefetch); + ~CpuThread(); + + static CpuThread *createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority); + + inline bool isPrefetch() const { return m_prefetch; } + inline bool isSoftAES() const { return m_softAES; } + + inline Algo algorithm() const override { return m_algorithm; } + inline int multiway() const override { return m_multiway; } + inline int priority() const override { return m_priority; } + inline int64_t affinity() const override { return m_affinity; } + inline size_t index() const override { return m_affinity; } + inline Type type() const override { return CPU; } + +# ifndef XMRIG_NO_API + rapidjson::Value toAPI(rapidjson::Document &doc) const override; +# endif + +private: + const Algo m_algorithm; + const bool m_prefetch; + const bool m_softAES; + const int m_multiway; + const int m_priority; + const int64_t m_affinity; + const size_t m_index; +}; + + +} /* namespace xmrig */ + + +#endif /* __CPUTHREAD_H__ */ diff --git a/src/xmrig.h b/src/xmrig.h index 9cae73c7..805b7ceb 100644 --- a/src/xmrig.h +++ b/src/xmrig.h @@ -30,18 +30,35 @@ namespace xmrig enum Algo { - ALGO_CRYPTONIGHT, /* CryptoNight (Monero) */ - ALGO_CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ - ALGO_CRYPTONIGHT_HEAVY, /* CryptoNight-Heavy (SUMO) */ + CRYPTONIGHT, /* CryptoNight (Monero) */ + CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ + CRYPTONIGHT_HEAVY, /* CryptoNight-Heavy (SUMO) */ +}; + + +enum AlgoVariant { + AV_AUTO, + AV_SINGLE, + AV_DOUBLE, + AV_SINGLE_SOFT, + AV_DOUBLE_SOFT, + AV_TRIPLE, + AV_QUAD, + AV_PENTA, + AV_TRIPLE_SOFT, + AV_QUAD_SOFT, + AV_PENTA_SOFT, + AV_MAX }; enum Variant { - VARIANT_AUTO = -1, - VARIANT_NONE = 0, - VARIANT_V1 = 1 + VARIANT_AUTO = -1, // Autodetect + VARIANT_NONE = 0, // Original CryptoNight + VARIANT_V1 = 1 // Monero v7 PoW }; + } /* xmrig */ From 6c970612bfe22c9c61ba9caf3acea3664f1f2474 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 2 Apr 2018 14:05:16 +0700 Subject: [PATCH 159/389] Transform affinity and av to internal representation. --- src/core/CommonConfig.cpp | 4 +-- src/core/CommonConfig.h | 3 +- src/workers/CpuThread.cpp | 71 ++++++++++++++++++++++++++++++++++++--- src/workers/CpuThread.h | 13 +++++-- src/xmrig.h | 26 ++++++++------ 5 files changed, 96 insertions(+), 21 deletions(-) diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index 51448654..6a2c76ac 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -95,9 +95,9 @@ xmrig::CommonConfig::~CommonConfig() } -const char *xmrig::CommonConfig::algoName() const +const char *xmrig::CommonConfig::algoName(Algo algorithm) { - return algoNames[m_algorithm]; + return algoNames[algorithm]; } diff --git a/src/core/CommonConfig.h b/src/core/CommonConfig.h index 0f6f6b8c..ee9c7a20 100644 --- a/src/core/CommonConfig.h +++ b/src/core/CommonConfig.h @@ -44,7 +44,7 @@ public: CommonConfig(); ~CommonConfig(); - const char *algoName() const; + static const char *algoName(Algo algorithm); inline Algo algorithm() const { return m_algorithm; } inline bool isApiIPv6() const { return m_apiIPv6; } @@ -52,6 +52,7 @@ public: inline bool isBackground() const { return m_background; } inline bool isColors() const { return m_colors; } inline bool isSyslog() const { return m_syslog; } + inline const char *algoName() const { return algoName(m_algorithm); } inline const char *apiToken() const { return m_apiToken; } inline const char *apiWorkerId() const { return m_apiWorkerId; } inline const char *logFile() const { return m_logFile; } diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index ff3aaed7..91e98a14 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -21,18 +21,22 @@ * along with this program. If not, see . */ +#include + +#include "core/CommonConfig.h" #include "rapidjson/document.h" #include "workers/CpuThread.h" -xmrig::CpuThread::CpuThread(size_t index, Algo algorithm, int multiway, int64_t affinity, int priority, bool softAES, bool prefetch) : +xmrig::CpuThread::CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch) : m_algorithm(algorithm), + m_av(av), m_prefetch(prefetch), m_softAES(softAES), - m_multiway(multiway), m_priority(priority), m_affinity(affinity), + m_multiway(multiway), m_index(index) { } @@ -45,7 +49,64 @@ xmrig::CpuThread::~CpuThread() xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority) { - return new CpuThread(index, algorithm, 1, affinity, priority, false, false); + assert(av > AV_AUTO && av < AV_MAX); + + Multiway multiway = SingleWay; + bool softAES = false; + + switch (av) { + case AV_SINGLE_SOFT: + softAES = true; + break; + + case AV_DOUBLE: + multiway = DoubleWay; + case AV_DOUBLE_SOFT: + softAES = true; + break; + + case AV_TRIPLE: + multiway = TripleWay; + case AV_TRIPLE_SOFT: + softAES = true; + break; + + case AV_QUAD: + multiway = QuadWay; + case AV_QUAD_SOFT: + softAES = true; + break; + + case AV_PENTA: + multiway = PentaWay; + case AV_PENTA_SOFT: + softAES = true; + break; + + default: + break; + } + + int64_t cpuId = -1L; + + if (affinity != -1L) { + size_t idx = 0; + + for (size_t i = 0; i < 64; i++) { + if (!(affinity & (1ULL << i))) { + continue; + } + + if (idx == index) { + cpuId = i; + break; + } + + idx++; + } + } + + return new CpuThread(index, algorithm, av, multiway, cpuId, priority, softAES, false); } @@ -56,11 +117,11 @@ rapidjson::Value xmrig::CpuThread::toAPI(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); obj.AddMember("type", "cpu", allocator); - obj.AddMember("algo", algorithm(), allocator); + obj.AddMember("algo", rapidjson::StringRef(CommonConfig::algoName(algorithm())), allocator); + obj.AddMember("av", m_av, allocator); obj.AddMember("low_power_mode", multiway(), allocator); obj.AddMember("affine_to_cpu", affinity(), allocator); obj.AddMember("priority", priority(), allocator); - obj.AddMember("prefetch", isPrefetch(), allocator); obj.AddMember("soft_aes", isSoftAES(), allocator); return obj; diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index 93ef50d5..2b214423 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -35,7 +35,15 @@ namespace xmrig { class CpuThread : public IThread { public: - CpuThread(size_t index, Algo algorithm, int multiway, int64_t affinity, int priority, bool softAES, bool prefetch); + enum Multiway { + SingleWay, + DoubleWay, + TripleWay, + QuadWay, + PentaWay + }; + + CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch); ~CpuThread(); static CpuThread *createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority); @@ -56,11 +64,12 @@ public: private: const Algo m_algorithm; + const AlgoVariant m_av; const bool m_prefetch; const bool m_softAES; - const int m_multiway; const int m_priority; const int64_t m_affinity; + const Multiway m_multiway; const size_t m_index; }; diff --git a/src/xmrig.h b/src/xmrig.h index 805b7ceb..eba72bb4 100644 --- a/src/xmrig.h +++ b/src/xmrig.h @@ -36,18 +36,22 @@ enum Algo { }; +//--av=1 For CPUs with hardware AES. +//--av=2 Lower power mode (double hash) of 1. +//--av=3 Software AES implementation. +//--av=4 Lower power mode (double hash) of 3. enum AlgoVariant { - AV_AUTO, - AV_SINGLE, - AV_DOUBLE, - AV_SINGLE_SOFT, - AV_DOUBLE_SOFT, - AV_TRIPLE, - AV_QUAD, - AV_PENTA, - AV_TRIPLE_SOFT, - AV_QUAD_SOFT, - AV_PENTA_SOFT, + AV_AUTO, // --av=0 Automatic mode. + AV_SINGLE, // --av=1 Single hash mode + AV_DOUBLE, // --av=2 Double hash mode + AV_SINGLE_SOFT, // --av=3 Single hash mode (Software AES) + AV_DOUBLE_SOFT, // --av=4 Double hash mode (Software AES) + AV_TRIPLE, // --av=5 Triple hash mode + AV_QUAD, // --av=6 Quard hash mode + AV_PENTA, // --av=7 Penta hash mode + AV_TRIPLE_SOFT, // --av=8 Triple hash mode (Software AES) + AV_QUAD_SOFT, // --av=9 Quard hash mode (Software AES) + AV_PENTA_SOFT, // --av=10 Penta hash mode (Software AES) AV_MAX }; From 72cd6d168e94d3aa0632e214aba9401963bf1eef Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 2 Apr 2018 15:03:56 +0700 Subject: [PATCH 160/389] Now used IThread to start threads, cpu-affinity broken, nonce allocation in double mode probably broken too. --- src/workers/CpuThread.h | 4 ++-- src/workers/DoubleWorker.cpp | 8 ++++---- src/workers/Handle.cpp | 14 ++++++-------- src/workers/Handle.h | 22 +++++++++++----------- src/workers/SingleWorker.cpp | 4 ++-- src/workers/Worker.cpp | 10 +++++----- src/workers/Worker.h | 4 ++-- src/workers/Workers.cpp | 19 +++++++++++++++---- 8 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index 2b214423..ee0a3d57 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -36,7 +36,7 @@ class CpuThread : public IThread { public: enum Multiway { - SingleWay, + SingleWay = 1, DoubleWay, TripleWay, QuadWay, @@ -55,7 +55,7 @@ public: inline int multiway() const override { return m_multiway; } inline int priority() const override { return m_priority; } inline int64_t affinity() const override { return m_affinity; } - inline size_t index() const override { return m_affinity; } + inline size_t index() const override { return m_index; } inline Type type() const override { return CPU; } # ifndef XMRIG_NO_API diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 46c8ed2e..3dba4a9f 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -134,12 +134,12 @@ void DoubleWorker::consumeJob() memcpy(m_state->blob + m_state->job.size(), m_state->job.blob(), m_state->job.size()); if (m_state->job.isNicehash()) { - m_state->nonce1 = (*Job::nonce(m_state->blob) & 0xff000000U) + (0xffffffU / (m_threads * 2) * m_id); - m_state->nonce2 = (*Job::nonce(m_state->blob + m_state->job.size()) & 0xff000000U) + (0xffffffU / (m_threads * 2) * (m_id + m_threads)); + m_state->nonce1 = (*Job::nonce(m_state->blob) & 0xff000000U) + (0xffffffU / m_totalWays * m_id); + m_state->nonce2 = (*Job::nonce(m_state->blob + m_state->job.size()) & 0xff000000U) + (0xffffffU / m_totalWays * (m_id + m_totalWays)); } else { - m_state->nonce1 = 0xffffffffU / (m_threads * 2) * m_id; - m_state->nonce2 = 0xffffffffU / (m_threads * 2) * (m_id + m_threads); + m_state->nonce1 = 0xffffffffU / m_totalWays * m_id; + m_state->nonce2 = 0xffffffffU / m_totalWays * (m_id + m_totalWays); } } diff --git a/src/workers/Handle.cpp b/src/workers/Handle.cpp index c461cee7..6d7b969a 100644 --- a/src/workers/Handle.cpp +++ b/src/workers/Handle.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,12 +25,10 @@ #include "workers/Handle.h" -Handle::Handle(int threadId, int threads, int64_t affinity, int priority) : - m_priority(priority), - m_threadId(threadId), - m_threads(threads), - m_affinity(affinity), - m_worker(nullptr) +Handle::Handle(xmrig::IThread *config, size_t totalWays) : + m_worker(nullptr), + m_totalWays(totalWays), + m_config(config) { } diff --git a/src/workers/Handle.h b/src/workers/Handle.h index 9faae0d0..d63dc098 100644 --- a/src/workers/Handle.h +++ b/src/workers/Handle.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,30 +29,30 @@ #include +#include "interfaces/IThread.h" + + class IWorker; class Handle { public: - Handle(int threadId, int threads, int64_t affinity, int priority); + Handle(xmrig::IThread *config, size_t totalWays); void join(); void start(void (*callback) (void *)); - inline int priority() const { return m_priority; } - inline int threadId() const { return m_threadId; } - inline int threads() const { return m_threads; } - inline int64_t affinity() const { return m_affinity; } inline IWorker *worker() const { return m_worker; } + inline size_t threadId() const { return m_config->index(); } + inline size_t totalWays() const { return m_totalWays; } inline void setWorker(IWorker *worker) { m_worker = worker; } + inline xmrig::IThread *config() const { return m_config; } private: - int m_priority; - int m_threadId; - int m_threads; - int64_t m_affinity; IWorker *m_worker; + size_t m_totalWays; uv_thread_t m_thread; + xmrig::IThread *m_config; }; diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 9f4a7484..9d47d947 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -104,10 +104,10 @@ void SingleWorker::consumeJob() m_result = m_job; if (m_job.isNicehash()) { - m_result.nonce = (*m_job.nonce() & 0xff000000U) + (0xffffffU / m_threads * m_id); + m_result.nonce = (*m_job.nonce() & 0xff000000U) + (0xffffffU / m_totalWays * m_id); } else { - m_result.nonce = 0xffffffffU / m_threads * m_id; + m_result.nonce = 0xffffffffU / m_totalWays * m_id; } } diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index 7a7ff986..648bf7ed 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -33,17 +33,17 @@ Worker::Worker(Handle *handle) : m_id(handle->threadId()), - m_threads(handle->threads()), + m_totalWays(handle->totalWays()), m_hashCount(0), m_timestamp(0), m_count(0), m_sequence(0) { - if (Cpu::threads() > 1 && handle->affinity() != -1L) { - Cpu::setAffinity(m_id, handle->affinity()); - } +// if (Cpu::threads() > 1 && handle->affinity() != -1L) { +// Cpu::setAffinity(m_id, handle->affinity()); +// } - Platform::setThreadPriority(handle->priority()); + Platform::setThreadPriority(handle->config()->priority()); m_ctx = Mem::create(m_id); } diff --git a/src/workers/Worker.h b/src/workers/Worker.h index 08a0551f..a9c15ef4 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -49,8 +49,8 @@ protected: void storeStats(); cryptonight_ctx *m_ctx; - int m_id; - int m_threads; + size_t m_id; + size_t m_totalWays; std::atomic m_hashCount; std::atomic m_timestamp; uint64_t m_count; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 5642751a..faa207a0 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -25,7 +25,10 @@ #include "api/Api.h" +#include "core/Config.h" +#include "core/Controller.h" #include "interfaces/IJobResultListener.h" +#include "interfaces/IThread.h" #include "Mem.h" #include "workers/DoubleWorker.h" #include "workers/Handle.h" @@ -33,6 +36,8 @@ #include "workers/SingleWorker.h" #include "workers/Workers.h" +#include "log/Log.h" + bool Workers::m_active = false; bool Workers::m_enabled = true; @@ -104,8 +109,14 @@ void Workers::setJob(const Job &job, bool donate) void Workers::start(int64_t affinity, int priority, xmrig::Controller *controller) { - const int threads = Mem::threads(); - m_hashrate = new Hashrate(threads, controller); + const std::vector &threads = controller->config()->threads(); + + size_t totalWays = 0; + for (const xmrig::IThread *thread : threads) { + totalWays += thread->multiway(); + } + + m_hashrate = new Hashrate(threads.size(), controller); uv_mutex_init(&m_mutex); uv_rwlock_init(&m_rwlock); @@ -117,8 +128,8 @@ void Workers::start(int64_t affinity, int priority, xmrig::Controller *controlle uv_timer_init(uv_default_loop(), &m_timer); uv_timer_start(&m_timer, Workers::onTick, 500, 500); - for (int i = 0; i < threads; ++i) { - Handle *handle = new Handle(i, threads, affinity, priority); + for (xmrig::IThread *thread : threads) { + Handle *handle = new Handle(thread, totalWays); m_workers.push_back(handle); handle->start(Workers::onReady); } From 903b243308911d89de0c5a4e701f9b9a958a0494 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 3 Apr 2018 02:55:28 +0700 Subject: [PATCH 161/389] New style function selector. --- CMakeLists.txt | 6 ++ src/crypto/CryptoNight.cpp | 22 +++--- src/crypto/CryptoNight_constants.h | 117 +++++++++++++++++++++++++++++ src/crypto/CryptoNight_x86.h | 31 +++++++- src/net/Job.cpp | 11 ++- src/net/Job.h | 4 +- src/workers/CpuThread.cpp | 87 +++++++++++++++++++++ src/workers/CpuThread.h | 23 ++++-- src/workers/SingleWorker.cpp | 5 +- src/workers/Worker.cpp | 4 +- src/workers/Worker.h | 6 ++ src/xmrig.h | 2 +- 12 files changed, 287 insertions(+), 31 deletions(-) create mode 100644 src/crypto/CryptoNight_constants.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c9d6624f..5e2ac190 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ project(xmrig) option(WITH_LIBCPUID "Use Libcpuid" ON) option(WITH_AEON "CryptoNight-Lite support" ON) +option(WITH_SUMO "CryptoNight-Heavy support" ON) option(WITH_HTTPD "HTTP REST API" ON) option(BUILD_STATIC "Build static binary" OFF) @@ -70,6 +71,7 @@ set(HEADERS_CRYPTO src/crypto/c_keccak.h src/crypto/c_skein.h src/crypto/CryptoNight.h + src/crypto/CryptoNight_constants.h src/crypto/CryptoNight_monero.h src/crypto/CryptoNight_test.h src/crypto/groestl_tables.h @@ -203,6 +205,10 @@ if (NOT WITH_AEON) add_definitions(/DXMRIG_NO_AEON) endif() +if (NOT WITH_SUMO) + add_definitions(/DXMRIG_NO_SUMO) +endif() + if (WITH_HTTPD) find_package(MHD) diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index f7dc1a37..d817c152 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -41,13 +41,13 @@ void (*cryptonight_hash_ctx)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) = nullptr; -#define CRYPTONIGHT_HASH(NAME, ITERATIONS, MEM, MASK, SOFT_AES) \ +#define CRYPTONIGHT_HASH(NAME, ALGO, SOFT_AES) \ switch (variant) { \ case xmrig::VARIANT_V1: \ - return cryptonight_##NAME##_hash(input, size, output, ctx); \ + return cryptonight_##NAME##_hash(input, size, output, ctx); \ \ case xmrig::VARIANT_NONE: \ - return cryptonight_##NAME##_hash(input, size, output, ctx); \ + return cryptonight_##NAME##_hash(input, size, output, ctx); \ \ default: \ break; \ @@ -56,50 +56,50 @@ void (*cryptonight_hash_ctx)(const uint8_t *input, size_t size, uint8_t *output, static void cryptonight_av1_aesni(const uint8_t *input, size_t size, uint8_t *output, struct cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - CRYPTONIGHT_HASH(single, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, false) + CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT, false) # endif } static void cryptonight_av2_aesni_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - CRYPTONIGHT_HASH(double, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, false) + CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT, false) # endif } static void cryptonight_av3_softaes(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { - CRYPTONIGHT_HASH(single, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, true) + CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT, true) } static void cryptonight_av4_softaes_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { - CRYPTONIGHT_HASH(double, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, true) + CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT, true) } #ifndef XMRIG_NO_AEON static void cryptonight_lite_av1_aesni(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - CRYPTONIGHT_HASH(single, AEON_ITER, AEON_MEMORY, AEON_MASK, false) + CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT_LITE, false) # endif } static void cryptonight_lite_av2_aesni_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - CRYPTONIGHT_HASH(double, AEON_ITER, AEON_MEMORY, AEON_MASK, false) + CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT_LITE, false) # endif } static void cryptonight_lite_av3_softaes(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { - CRYPTONIGHT_HASH(single, AEON_ITER, AEON_MEMORY, AEON_MASK, true) + CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT_LITE, true) } static void cryptonight_lite_av4_softaes_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { - CRYPTONIGHT_HASH(double, AEON_ITER, AEON_MEMORY, AEON_MASK, true) + CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT_LITE, true) } void (*cryptonight_variations[8])(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) = { diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h new file mode 100644 index 00000000..5d02baf9 --- /dev/null +++ b/src/crypto/CryptoNight_constants.h @@ -0,0 +1,117 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CRYPTONIGHT_CONSTANTS_H__ +#define __CRYPTONIGHT_CONSTANTS_H__ + + +#include + + +#include "xmrig.h" + + +namespace xmrig +{ + +constexpr const size_t CRYPTONIGHT_MEMORY = 2 * 1024 * 1024; +constexpr const uint32_t CRYPTONIGHT_MASK = 0x1FFFF0; +constexpr const uint32_t CRYPTONIGHT_ITER = 0x80000; + +constexpr const size_t CRYPTONIGHT_LITE_MEMORY = 1 * 1024 * 1024; +constexpr const uint32_t CRYPTONIGHT_LITE_MASK = 0xFFFF0; +constexpr const uint32_t CRYPTONIGHT_LITE_ITER = 0x40000; + +constexpr const size_t CRYPTONIGHT_HEAVY_MEMORY = 4 * 1024 * 1024; +constexpr const uint32_t CRYPTONIGHT_HEAVY_MASK = 0x3FFFF0; +constexpr const uint32_t CRYPTONIGHT_HEAVY_ITER = 0x40000; + + +template inline constexpr size_t cn_select_memory() { return 0; } +template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_MEMORY; } +template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_LITE_MEMORY; } +template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_HEAVY_MEMORY; } + +inline size_t cn_select_memory(Algo algorithm) +{ + switch(algorithm) + { + case CRYPTONIGHT: + return CRYPTONIGHT_MEMORY; + + case CRYPTONIGHT_LITE: + return CRYPTONIGHT_LITE_MEMORY; + + case CRYPTONIGHT_HEAVY: + return CRYPTONIGHT_HEAVY_MEMORY; + } +} + + +template inline constexpr uint32_t cn_select_mask() { return 0; } +template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_MASK; } +template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_LITE_MASK; } +template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_HEAVY_MASK; } + +inline uint32_t cn_select_mask(Algo algorithm) +{ + switch(algorithm) + { + case CRYPTONIGHT: + return CRYPTONIGHT_MASK; + + case CRYPTONIGHT_LITE: + return CRYPTONIGHT_LITE_MASK; + + case CRYPTONIGHT_HEAVY: + return CRYPTONIGHT_HEAVY_MASK; + } +} + + +template inline constexpr uint32_t cn_select_iter() { return 0; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } + +inline uint32_t cn_select_iter(Algo algorithm) +{ + switch(algorithm) + { + case CRYPTONIGHT: + return CRYPTONIGHT_ITER; + + case CRYPTONIGHT_LITE: + return CRYPTONIGHT_LITE_ITER; + + case CRYPTONIGHT_HEAVY: + return CRYPTONIGHT_HEAVY_ITER; + } +} + + +} /* namespace xmrig */ + + +#endif /* __CRYPTONIGHT_CONSTANTS_H__ */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index b7544dcc..768b2a5b 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -35,6 +35,7 @@ #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_constants.h" #include "crypto/CryptoNight_monero.h" #include "crypto/soft_aes.h" @@ -309,9 +310,13 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template +template inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx *__restrict__ ctx) { + constexpr size_t MASK = xmrig::cn_select_mask(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t MEM = xmrig::cn_select_memory(); + keccak(input, (int) size, ctx->state0, 200); VARIANT1_INIT(0); @@ -367,9 +372,13 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } -template +template inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) { + constexpr size_t MASK = xmrig::cn_select_mask(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t MEM = xmrig::cn_select_memory(); + keccak(input, (int) size, ctx->state0, 200); keccak(input + size, (int) size, ctx->state1, 200); @@ -464,4 +473,22 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, output + 32); } + +template +inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +{ +} + + +template +inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +{ +} + + +template +inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +{ +} + #endif /* __CRYPTONIGHT_X86_H__ */ diff --git a/src/net/Job.cpp b/src/net/Job.cpp index e5714376..17d8266f 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -23,6 +23,7 @@ */ +#include #include @@ -62,11 +63,11 @@ Job::Job() : m_algo(xmrig::CRYPTONIGHT), m_poolId(-2), m_threadId(-1), - m_variant(xmrig::VARIANT_AUTO), m_size(0), m_diff(0), m_target(0), - m_blob() + m_blob(), + m_variant(xmrig::VARIANT_AUTO) { } @@ -77,12 +78,12 @@ Job::Job(int poolId, bool nicehash, int algo, int variant) : m_algo(algo), m_poolId(poolId), m_threadId(-1), - m_variant(variant), m_size(0), m_diff(0), m_target(0), m_blob() { + setVariant(variant); } @@ -174,10 +175,12 @@ void Job::setVariant(int variant) case xmrig::VARIANT_AUTO: case xmrig::VARIANT_NONE: case xmrig::VARIANT_V1: - m_variant = variant; + m_variant = static_cast(variant); break; default: + assert(false); + m_variant = xmrig::VARIANT_AUTO; break; } } diff --git a/src/net/Job.h b/src/net/Job.h index b8b9d6bc..e8964314 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -55,7 +55,6 @@ public: inline const xmrig::Id &id() const { return m_id; } inline int poolId() const { return m_poolId; } inline int threadId() const { return m_threadId; } - inline int variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? 1 : 0) : m_variant); } inline size_t size() const { return m_size; } inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } inline uint32_t diff() const { return (uint32_t) m_diff; } @@ -63,6 +62,7 @@ public: inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setPoolId(int poolId) { m_poolId = poolId; } inline void setThreadId(int threadId) { m_threadId = threadId; } + inline xmrig::Variant variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? xmrig::VARIANT_V1 : xmrig::VARIANT_NONE) : m_variant); } static bool fromHex(const char* in, unsigned int len, unsigned char* out); static inline uint32_t *nonce(uint8_t *blob) { return reinterpret_cast(blob + 39); } @@ -78,12 +78,12 @@ private: int m_algo; int m_poolId; int m_threadId; - int m_variant; size_t m_size; uint64_t m_diff; uint64_t m_target; uint8_t m_blob[96]; // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. xmrig::Id m_id; + xmrig::Variant m_variant; }; #endif /* __JOB_H__ */ diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 91e98a14..b8be966f 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -29,6 +29,13 @@ #include "workers/CpuThread.h" +#if defined(XMRIG_ARM) +# include "crypto/CryptoNight_arm.h" +#else +# include "crypto/CryptoNight_x86.h" +#endif + + xmrig::CpuThread::CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch) : m_algorithm(algorithm), m_av(av), @@ -47,6 +54,86 @@ xmrig::CpuThread::~CpuThread() } +xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant av, Variant variant) +{ + assert(variant == VARIANT_NONE || variant == VARIANT_V1); + + static const cn_hash_fun func_table[50] = { + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + +# ifndef XMRIG_NO_AEON + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, +# else + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, +# endif + +# ifndef XMRIG_NO_SUMO + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, +# else + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, +# endif + }; + +# ifndef XMRIG_NO_SUMO + if (algorithm == CRYPTONIGHT_HEAVY) { + variant = VARIANT_NONE; + } +# endif + + return func_table[20 * algorithm + 10 * variant + av - 1]; +} + + xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority) { assert(av > AV_AUTO && av < AV_MAX); diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index ee0a3d57..aef73719 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -29,6 +29,9 @@ #include "xmrig.h" +struct cryptonight_ctx; + + namespace xmrig { @@ -46,17 +49,21 @@ public: CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch); ~CpuThread(); + typedef void (*cn_hash_fun)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx); + + static cn_hash_fun fn(Algo algorithm, AlgoVariant av, Variant variant); static CpuThread *createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority); - inline bool isPrefetch() const { return m_prefetch; } - inline bool isSoftAES() const { return m_softAES; } + inline bool isPrefetch() const { return m_prefetch; } + inline bool isSoftAES() const { return m_softAES; } + inline cn_hash_fun fn(Variant variant) const { return fn(m_algorithm, m_av, variant); } - inline Algo algorithm() const override { return m_algorithm; } - inline int multiway() const override { return m_multiway; } - inline int priority() const override { return m_priority; } - inline int64_t affinity() const override { return m_affinity; } - inline size_t index() const override { return m_index; } - inline Type type() const override { return CPU; } + inline Algo algorithm() const override { return m_algorithm; } + inline int multiway() const override { return m_multiway; } + inline int priority() const override { return m_priority; } + inline int64_t affinity() const override { return m_affinity; } + inline size_t index() const override { return m_index; } + inline Type type() const override { return CPU; } # ifndef XMRIG_NO_API rapidjson::Value toAPI(rapidjson::Document &doc) const override; diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 9d47d947..6df44d9d 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -26,7 +26,7 @@ #include -#include "crypto/CryptoNight.h" +#include "workers/CpuThread.h" #include "workers/SingleWorker.h" #include "workers/Workers.h" @@ -61,7 +61,8 @@ void SingleWorker::start() m_count++; *m_job.nonce() = ++m_result.nonce; - if (CryptoNight::hash(m_job, m_result, m_ctx)) { + m_thread->fn(m_job.variant())(m_job.blob(), m_job.size(), m_result.result, m_ctx); + if (*reinterpret_cast(m_result.result + 24) < m_job.target()) { Workers::submit(m_result); } diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index 648bf7ed..2672f49f 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -27,6 +27,7 @@ #include "Cpu.h" #include "Mem.h" #include "Platform.h" +#include "workers/CpuThread.h" #include "workers/Handle.h" #include "workers/Worker.h" @@ -37,7 +38,8 @@ Worker::Worker(Handle *handle) : m_hashCount(0), m_timestamp(0), m_count(0), - m_sequence(0) + m_sequence(0), + m_thread(static_cast(handle->config())) { // if (Cpu::threads() > 1 && handle->affinity() != -1L) { // Cpu::setAffinity(m_id, handle->affinity()); diff --git a/src/workers/Worker.h b/src/workers/Worker.h index a9c15ef4..cb52c149 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -36,6 +36,11 @@ struct cryptonight_ctx; class Handle; +namespace xmrig { + class CpuThread; +} + + class Worker : public IWorker { public: @@ -55,6 +60,7 @@ protected: std::atomic m_timestamp; uint64_t m_count; uint64_t m_sequence; + xmrig::CpuThread *m_thread; }; diff --git a/src/xmrig.h b/src/xmrig.h index eba72bb4..f4ae8162 100644 --- a/src/xmrig.h +++ b/src/xmrig.h @@ -63,7 +63,7 @@ enum Variant { }; -} /* xmrig */ +} /* namespace xmrig */ #endif /* __XMRIG_H__ */ From c1bc6acd26045f5ec3382832a841733c3262c9e5 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 3 Apr 2018 03:01:04 +0700 Subject: [PATCH 162/389] Fix DoubleWorker. --- src/workers/DoubleWorker.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 3dba4a9f..7bde7df9 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -26,7 +26,7 @@ #include -#include "crypto/CryptoNight.h" +#include "workers/CpuThread.h" #include "workers/DoubleWorker.h" #include "workers/Workers.h" @@ -86,7 +86,7 @@ void DoubleWorker::start() *Job::nonce(m_state->blob) = ++m_state->nonce1; *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.variant()); + m_thread->fn(m_state->job.variant())(m_state->blob, m_state->job.size(), m_hash, m_ctx); if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); From d7c56305094902818b47ac7714b2296d7f3e24c2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 3 Apr 2018 03:27:44 +0700 Subject: [PATCH 163/389] Fix nonce allocation in DoubleWorker. --- src/App.cpp | 2 +- src/Mem_unix.cpp | 2 +- src/workers/DoubleWorker.cpp | 4 ++-- src/workers/Handle.cpp | 3 ++- src/workers/Handle.h | 4 +++- src/workers/Worker.cpp | 1 + src/workers/Worker.h | 1 + src/workers/Workers.cpp | 4 ++-- src/workers/Workers.h | 2 +- 9 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/App.cpp b/src/App.cpp index a9d10774..8c7a16ea 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -136,7 +136,7 @@ int App::exec() m_httpd->start(); # endif - Workers::start(m_controller->config()->affinity(), m_controller->config()->priority(), m_controller); + Workers::start(m_controller); m_controller->network()->connect(); diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 3b69f267..7978d241 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -46,7 +46,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_threads = threads; m_doubleHash = doubleHash; - const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; + const int ratio = (doubleHash && algo != xmrig::CRYPTONIGHT_LITE) ? 2 : 1; m_size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 7bde7df9..8971f0c0 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -135,11 +135,11 @@ void DoubleWorker::consumeJob() if (m_state->job.isNicehash()) { m_state->nonce1 = (*Job::nonce(m_state->blob) & 0xff000000U) + (0xffffffU / m_totalWays * m_id); - m_state->nonce2 = (*Job::nonce(m_state->blob + m_state->job.size()) & 0xff000000U) + (0xffffffU / m_totalWays * (m_id + m_totalWays)); + m_state->nonce2 = (*Job::nonce(m_state->blob + m_state->job.size()) & 0xff000000U) + (0xffffffU / m_totalWays * (m_id + m_totalThreads)); } else { m_state->nonce1 = 0xffffffffU / m_totalWays * m_id; - m_state->nonce2 = 0xffffffffU / m_totalWays * (m_id + m_totalWays); + m_state->nonce2 = 0xffffffffU / m_totalWays * (m_id + m_totalThreads); } } diff --git a/src/workers/Handle.cpp b/src/workers/Handle.cpp index 6d7b969a..29f57fb2 100644 --- a/src/workers/Handle.cpp +++ b/src/workers/Handle.cpp @@ -25,8 +25,9 @@ #include "workers/Handle.h" -Handle::Handle(xmrig::IThread *config, size_t totalWays) : +Handle::Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays) : m_worker(nullptr), + m_totalThreads(totalThreads), m_totalWays(totalWays), m_config(config) { diff --git a/src/workers/Handle.h b/src/workers/Handle.h index d63dc098..b3a7c76f 100644 --- a/src/workers/Handle.h +++ b/src/workers/Handle.h @@ -38,18 +38,20 @@ class IWorker; class Handle { public: - Handle(xmrig::IThread *config, size_t totalWays); + Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays); void join(); void start(void (*callback) (void *)); inline IWorker *worker() const { return m_worker; } inline size_t threadId() const { return m_config->index(); } + inline size_t totalThreads() const { return m_totalThreads; } inline size_t totalWays() const { return m_totalWays; } inline void setWorker(IWorker *worker) { m_worker = worker; } inline xmrig::IThread *config() const { return m_config; } private: IWorker *m_worker; + size_t m_totalThreads; size_t m_totalWays; uv_thread_t m_thread; xmrig::IThread *m_config; diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index 2672f49f..bf4f62da 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -34,6 +34,7 @@ Worker::Worker(Handle *handle) : m_id(handle->threadId()), + m_totalThreads(handle->totalThreads()), m_totalWays(handle->totalWays()), m_hashCount(0), m_timestamp(0), diff --git a/src/workers/Worker.h b/src/workers/Worker.h index cb52c149..9fbce1a2 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -55,6 +55,7 @@ protected: cryptonight_ctx *m_ctx; size_t m_id; + size_t m_totalThreads; size_t m_totalWays; std::atomic m_hashCount; std::atomic m_timestamp; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index faa207a0..8ac256da 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -107,7 +107,7 @@ void Workers::setJob(const Job &job, bool donate) } -void Workers::start(int64_t affinity, int priority, xmrig::Controller *controller) +void Workers::start(xmrig::Controller *controller) { const std::vector &threads = controller->config()->threads(); @@ -129,7 +129,7 @@ void Workers::start(int64_t affinity, int priority, xmrig::Controller *controlle uv_timer_start(&m_timer, Workers::onTick, 500, 500); for (xmrig::IThread *thread : threads) { - Handle *handle = new Handle(thread, totalWays); + Handle *handle = new Handle(thread, threads.size(), totalWays); m_workers.push_back(handle); handle->start(Workers::onReady); } diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 942a5b58..ecec9e30 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -51,7 +51,7 @@ public: static void printHashrate(bool detail); static void setEnabled(bool enabled); static void setJob(const Job &job, bool donate); - static void start(int64_t affinity, int priority, xmrig::Controller *controller); + static void start(xmrig::Controller *controller); static void stop(); static void submit(const JobResult &result); From 5c6ec587ac8020e8a84538105dab7226c415666c Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 3 Apr 2018 14:51:05 +0700 Subject: [PATCH 164/389] Move selfTest to threads, remove legacy CryptoNight.cpp. --- CMakeLists.txt | 1 - src/App.cpp | 5 - src/crypto/CryptoNight.cpp | 189 ----------------------------------- src/crypto/CryptoNight.h | 15 --- src/interfaces/IWorker.h | 7 +- src/workers/DoubleWorker.cpp | 35 ++++++- src/workers/DoubleWorker.h | 3 +- src/workers/SingleWorker.cpp | 35 ++++++- src/workers/SingleWorker.h | 3 +- src/workers/Worker.h | 1 + src/workers/Workers.cpp | 9 +- 11 files changed, 85 insertions(+), 218 deletions(-) delete mode 100644 src/crypto/CryptoNight.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e2ac190..106ab30f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,7 +126,6 @@ set(SOURCES_CRYPTO src/crypto/c_blake256.c src/crypto/c_jh.c src/crypto/c_skein.c - src/crypto/CryptoNight.cpp ) if (WIN32) diff --git a/src/App.cpp b/src/App.cpp index 8c7a16ea..dc22836d 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -101,11 +101,6 @@ int App::exec() background(); - if (!CryptoNight::init(m_controller->config()->algorithm(), m_controller->config()->algoVariant(), m_controller->config()->isDoubleHash())) { - LOG_ERR("\"%s\" hash self-test failed.", m_controller->config()->algoName()); - return 1; - } - Mem::allocate(m_controller->config()->algorithm(), m_controller->config()->threadsCount(), m_controller->config()->isDoubleHash(), diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp deleted file mode 100644 index d817c152..00000000 --- a/src/crypto/CryptoNight.cpp +++ /dev/null @@ -1,189 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2016-2018 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include "crypto/CryptoNight.h" - - -#if defined(XMRIG_ARM) -# include "crypto/CryptoNight_arm.h" -#else -# include "crypto/CryptoNight_x86.h" -#endif - -#include "crypto/CryptoNight_test.h" -#include "net/Job.h" -#include "net/JobResult.h" -#include "xmrig.h" - - -void (*cryptonight_hash_ctx)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) = nullptr; - - -#define CRYPTONIGHT_HASH(NAME, ALGO, SOFT_AES) \ - switch (variant) { \ - case xmrig::VARIANT_V1: \ - return cryptonight_##NAME##_hash(input, size, output, ctx); \ - \ - case xmrig::VARIANT_NONE: \ - return cryptonight_##NAME##_hash(input, size, output, ctx); \ - \ - default: \ - break; \ - } - - -static void cryptonight_av1_aesni(const uint8_t *input, size_t size, uint8_t *output, struct cryptonight_ctx *ctx, int variant) { -# if !defined(XMRIG_ARMv7) - CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT, false) -# endif -} - - -static void cryptonight_av2_aesni_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { -# if !defined(XMRIG_ARMv7) - CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT, false) -# endif -} - - -static void cryptonight_av3_softaes(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { - CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT, true) -} - - -static void cryptonight_av4_softaes_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { - CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT, true) -} - - -#ifndef XMRIG_NO_AEON -static void cryptonight_lite_av1_aesni(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { -# if !defined(XMRIG_ARMv7) - CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT_LITE, false) -# endif -} - - -static void cryptonight_lite_av2_aesni_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { -# if !defined(XMRIG_ARMv7) - CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT_LITE, false) -# endif -} - - -static void cryptonight_lite_av3_softaes(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { - CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT_LITE, true) -} - - -static void cryptonight_lite_av4_softaes_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { - CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT_LITE, true) -} - -void (*cryptonight_variations[8])(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) = { - cryptonight_av1_aesni, - cryptonight_av2_aesni_double, - cryptonight_av3_softaes, - cryptonight_av4_softaes_double, - cryptonight_lite_av1_aesni, - cryptonight_lite_av2_aesni_double, - cryptonight_lite_av3_softaes, - cryptonight_lite_av4_softaes_double - }; -#else -void (*cryptonight_variations[4])(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) = { - cryptonight_av1_aesni, - cryptonight_av2_aesni_double, - cryptonight_av3_softaes, - cryptonight_av4_softaes_double - }; -#endif - - -bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) -{ - cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.variant()); - - return *reinterpret_cast(result.result + 24) < job.target(); -} - - -bool CryptoNight::init(int algo, int variant, bool doubleHash) -{ - if (variant < 1 || variant > 4) { - return false; - } - -# ifndef XMRIG_NO_AEON - const int index = algo == xmrig::CRYPTONIGHT_LITE ? (variant + 3) : (variant - 1); -# else - const int index = variant - 1; -# endif - - cryptonight_hash_ctx = cryptonight_variations[index]; - - return selfTest(algo, doubleHash); -} - - -void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) -{ - cryptonight_hash_ctx(input, size, output, ctx, variant); -} - - -bool CryptoNight::selfTest(int algo, bool doubleHash) { - if (cryptonight_hash_ctx == nullptr) { - return false; - } - - uint8_t output[64]; - - struct cryptonight_ctx *ctx = static_cast(_mm_malloc(sizeof(cryptonight_ctx), 16)); - ctx->memory = static_cast(_mm_malloc(MONERO_MEMORY * 2, 16)); - - cryptonight_hash_ctx(test_input, 76, output, ctx, 0); - -# ifndef XMRIG_NO_AEON - bool rc = memcmp(output, algo == xmrig::CRYPTONIGHT_LITE ? test_output_v0_lite : test_output_v0, (doubleHash ? 64 : 32)) == 0; -# else - bool rc = memcmp(output, test_output_v0, (doubleHash ? 64 : 32)) == 0; -# endif - - if (rc) { - cryptonight_hash_ctx(test_input, 76, output, ctx, 1); - -# ifndef XMRIG_NO_AEON - rc = memcmp(output, algo == xmrig::CRYPTONIGHT_LITE ? test_output_v1_lite : test_output_v1, (doubleHash ? 64 : 32)) == 0; -# else - rc = memcmp(output, test_output_v1, (doubleHash ? 64 : 32)) == 0; -# endif - } - - _mm_free(ctx->memory); - _mm_free(ctx); - - return rc; -} diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index eb17719e..d0d61ae3 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -46,19 +46,4 @@ struct cryptonight_ctx { }; -class Job; -class JobResult; - - -class CryptoNight -{ -public: - static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); - static bool init(int algo, int variant, bool doubleHash); - static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant); - -private: - static bool selfTest(int algo, bool doubleHash); -}; - #endif /* __CRYPTONIGHT_H__ */ diff --git a/src/interfaces/IWorker.h b/src/interfaces/IWorker.h index b9b6eb0a..a90abe11 100644 --- a/src/interfaces/IWorker.h +++ b/src/interfaces/IWorker.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,9 +33,10 @@ class IWorker public: virtual ~IWorker() {} + virtual bool start() = 0; + virtual size_t id() const = 0; virtual uint64_t hashCount() const = 0; virtual uint64_t timestamp() const = 0; - virtual void start() = 0; }; diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 8971f0c0..425dd0c2 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -26,6 +26,7 @@ #include +#include "crypto/CryptoNight_test.h" #include "workers/CpuThread.h" #include "workers/DoubleWorker.h" #include "workers/Workers.h" @@ -61,8 +62,12 @@ DoubleWorker::~DoubleWorker() } -void DoubleWorker::start() +bool DoubleWorker::start() { + if (!selfTest()) { + return false; + } + while (Workers::sequence() > 0) { if (Workers::isPaused()) { do { @@ -101,6 +106,8 @@ void DoubleWorker::start() consumeJob(); } + + return true; } @@ -115,6 +122,32 @@ bool DoubleWorker::resume(const Job &job) } +bool DoubleWorker::selfTest() +{ + if (m_thread->fn(xmrig::VARIANT_NONE) == nullptr) { + return false; + } + + m_thread->fn(xmrig::VARIANT_NONE)(test_input, 76, m_hash, m_ctx); + + if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_hash, test_output_v0, 64) == 0) { + m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); + + return memcmp(m_hash, test_output_v1, 64) == 0; + } + +# ifndef XMRIG_NO_AEON + if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_hash, test_output_v0_lite, 64) == 0) { + m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); + + return memcmp(m_hash, test_output_v1_lite, 64) == 0; + } +# endif + + return false; +} + + void DoubleWorker::consumeJob() { Job job = Workers::job(); diff --git a/src/workers/DoubleWorker.h b/src/workers/DoubleWorker.h index 57be59d0..e8847282 100644 --- a/src/workers/DoubleWorker.h +++ b/src/workers/DoubleWorker.h @@ -40,10 +40,11 @@ public: DoubleWorker(Handle *handle); ~DoubleWorker(); - void start() override; + bool start() override; private: bool resume(const Job &job); + bool selfTest(); void consumeJob(); void save(const Job &job); diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 6df44d9d..2ff356c8 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -26,6 +26,7 @@ #include +#include "crypto/CryptoNight_test.h" #include "workers/CpuThread.h" #include "workers/SingleWorker.h" #include "workers/Workers.h" @@ -37,8 +38,12 @@ SingleWorker::SingleWorker(Handle *handle) } -void SingleWorker::start() +bool SingleWorker::start() { + if (!selfTest()) { + return false; + } + while (Workers::sequence() > 0) { if (Workers::isPaused()) { do { @@ -71,6 +76,8 @@ void SingleWorker::start() consumeJob(); } + + return true; } @@ -87,6 +94,32 @@ bool SingleWorker::resume(const Job &job) } +bool SingleWorker::selfTest() +{ + if (m_thread->fn(xmrig::VARIANT_NONE) == nullptr) { + return false; + } + + m_thread->fn(xmrig::VARIANT_NONE)(test_input, 76, m_result.result, m_ctx); + + if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_result.result, test_output_v0, 32) == 0) { + m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_result.result, m_ctx); + + return memcmp(m_result.result, test_output_v1, 32) == 0; + } + +# ifndef XMRIG_NO_AEON + if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_result.result, test_output_v0_lite, 32) == 0) { + m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_result.result, m_ctx); + + return memcmp(m_result.result, test_output_v1_lite, 32) == 0; + } +# endif + + return false; +} + + void SingleWorker::consumeJob() { Job job = Workers::job(); diff --git a/src/workers/SingleWorker.h b/src/workers/SingleWorker.h index f7d9cff8..061f5084 100644 --- a/src/workers/SingleWorker.h +++ b/src/workers/SingleWorker.h @@ -39,10 +39,11 @@ class SingleWorker : public Worker public: SingleWorker(Handle *handle); - void start() override; + bool start() override; private: bool resume(const Job &job); + bool selfTest(); void consumeJob(); void save(const Job &job); diff --git a/src/workers/Worker.h b/src/workers/Worker.h index 9fbce1a2..88f6ee42 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -47,6 +47,7 @@ public: Worker(Handle *handle); ~Worker(); + inline size_t id() const override { return m_id; } inline uint64_t hashCount() const override { return m_hashCount.load(std::memory_order_relaxed); } inline uint64_t timestamp() const override { return m_timestamp.load(std::memory_order_relaxed); } diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 8ac256da..00941c7b 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -22,6 +22,7 @@ */ #include +#include #include "api/Api.h" @@ -171,7 +172,13 @@ void Workers::onReady(void *arg) handle->setWorker(new SingleWorker(handle)); } - handle->worker()->start(); + const bool rc = handle->worker()->start(); + + if (!rc) { + uv_mutex_lock(&m_mutex); + LOG_ERR("thread %zu error: \"hash self-test failed\".", handle->worker()->id()); + uv_mutex_unlock(&m_mutex); + } } From 7d5a97137d5804d16c1c6697c3b427859cbd241d Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 3 Apr 2018 16:08:15 +0700 Subject: [PATCH 165/389] Fix autoconfig and memory allocation for heavy algo. --- src/Cpu.cpp | 19 +++++++++++++++---- src/Cpu.h | 9 ++++++--- src/Mem.cpp | 9 ++++++--- src/Mem.h | 5 ++++- src/Mem_unix.cpp | 6 +++++- src/Mem_win.cpp | 6 +++++- src/workers/SingleWorker.cpp | 2 +- 7 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/Cpu.cpp b/src/Cpu.cpp index eba993b3..f8fb092c 100644 --- a/src/Cpu.cpp +++ b/src/Cpu.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,7 +39,7 @@ int Cpu::m_totalCores = 0; int Cpu::m_totalThreads = 0; -int Cpu::optimalThreadsCount(int algo, bool doubleHash, int maxCpuUsage) +int Cpu::optimalThreadsCount(xmrig::Algo algo, bool doubleHash, int maxCpuUsage) { if (m_totalThreads == 1) { return 1; @@ -54,7 +54,18 @@ int Cpu::optimalThreadsCount(int algo, bool doubleHash, int maxCpuUsage) } int count = 0; - const int size = (algo ? 1024 : 2048) * (doubleHash ? 2 : 1); + int size = 2048; + + if (algo == xmrig::CRYPTONIGHT_LITE) { + size = 1024; + } + else if (algo == xmrig::CRYPTONIGHT_HEAVY) { + size = 4096; + } + + if (doubleHash) { + size *= 2; + } if (cache) { count = cache / size; diff --git a/src/Cpu.h b/src/Cpu.h index 9444274d..118c7b7d 100644 --- a/src/Cpu.h +++ b/src/Cpu.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,6 +28,9 @@ #include +#include "xmrig.h" + + class Cpu { public: @@ -37,7 +40,7 @@ public: BMI2 = 4 }; - static int optimalThreadsCount(int algo, bool doubleHash, int maxCpuUsage); + static int optimalThreadsCount(xmrig::Algo algo, bool doubleHash, int maxCpuUsage); static void init(); static void setAffinity(int id, uint64_t mask); diff --git a/src/Mem.cpp b/src/Mem.cpp index f5da7865..9de79dbd 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -27,8 +27,8 @@ #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_constants.h" #include "Mem.h" -#include "xmrig.h" bool Mem::m_doubleHash = false; @@ -48,10 +48,13 @@ cryptonight_ctx *Mem::create(int threadId) } # endif - cryptonight_ctx *ctx = reinterpret_cast(&m_memory[MONERO_MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); + const size_t size = m_algo == xmrig::CRYPTONIGHT_HEAVY ? xmrig::cn_select_memory() + : xmrig::cn_select_memory(); + + cryptonight_ctx *ctx = reinterpret_cast(&m_memory[size - sizeof(cryptonight_ctx) * (threadId + 1)]); const int ratio = m_doubleHash ? 2 : 1; - ctx->memory = &m_memory[MONERO_MEMORY * (threadId * ratio + 1)]; + ctx->memory = &m_memory[size * (threadId * ratio + 1)]; return ctx; } diff --git a/src/Mem.h b/src/Mem.h index 18914d68..73947b42 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -30,6 +30,9 @@ #include +#include "xmrig.h" + + struct cryptonight_ctx; @@ -42,7 +45,7 @@ public: Lock = 4 }; - static bool allocate(int algo, int threads, bool doubleHash, bool enabled); + static bool allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled); static cryptonight_ctx *create(int threadId); static void *calloc(size_t num, size_t size); static void release(); diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 7978d241..0dd833d7 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -40,7 +40,7 @@ #include "xmrig.h" -bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) +bool Mem::allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled) { m_algo = algo; m_threads = threads; @@ -49,6 +49,10 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) const int ratio = (doubleHash && algo != xmrig::CRYPTONIGHT_LITE) ? 2 : 1; m_size = MONERO_MEMORY * (threads * ratio + 1); + if (algo == xmrig::CRYPTONIGHT_HEAVY) { + m_size *= 2; + } + if (!enabled) { m_memory = static_cast(_mm_malloc(m_size, 16)); return true; diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 6cc0a6ee..ec30e3e8 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -145,7 +145,7 @@ static BOOL TrySetLockPagesPrivilege() { } -bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) +bool Mem::allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled) { m_algo = algo; m_threads = threads; @@ -154,6 +154,10 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) const int ratio = (doubleHash && algo != xmrig::CRYPTONIGHT_LITE) ? 2 : 1; m_size = MONERO_MEMORY * (threads * ratio + 1); + if (algo == xmrig::CRYPTONIGHT_HEAVY) { + m_size *= 2; + } + if (!enabled) { m_memory = static_cast(_mm_malloc(m_size, 16)); return true; diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 2ff356c8..5757ef77 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -116,7 +116,7 @@ bool SingleWorker::selfTest() } # endif - return false; + return true; } From dd6bc339bf04cf3dfbd2806ef4891e4f9cc05566 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 3 Apr 2018 16:55:41 +0700 Subject: [PATCH 166/389] First working cryptonight-heavy. --- src/crypto/CryptoNight_x86.h | 129 ++++++++++++++++++++++++-- src/net/strategies/DonateStrategy.cpp | 4 + src/workers/DoubleWorker.cpp | 2 +- 3 files changed, 126 insertions(+), 9 deletions(-) diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 768b2a5b..414a1f7f 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -218,7 +218,21 @@ static inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, } -template +inline void mix_and_propagate(__m128i& x0, __m128i& x1, __m128i& x2, __m128i& x3, __m128i& x4, __m128i& x5, __m128i& x6, __m128i& x7) +{ + __m128i tmp0 = x0; + x0 = _mm_xor_si128(x0, x1); + x1 = _mm_xor_si128(x1, x2); + x2 = _mm_xor_si128(x2, x3); + x3 = _mm_xor_si128(x3, x4); + x4 = _mm_xor_si128(x4, x5); + x5 = _mm_xor_si128(x5, x6); + x6 = _mm_xor_si128(x6, x7); + x7 = _mm_xor_si128(x7, tmp0); +} + + +template static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) { __m128i xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7; @@ -235,6 +249,23 @@ static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) xin6 = _mm_load_si128(input + 10); xin7 = _mm_load_si128(input + 11); + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + for (size_t i = 0; i < 16; i++) { + aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k3, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k4, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k5, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k6, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k7, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k8, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + + mix_and_propagate(xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7); + } + } + for (size_t i = 0; i < MEM / sizeof(__m128i); i += 8) { aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); @@ -259,7 +290,7 @@ static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) } -template +template static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) { __m128i xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7; @@ -297,6 +328,51 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + mix_and_propagate(xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7); + } + } + + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + for (size_t i = 0; i < MEM / sizeof(__m128i); i += 8) { + xout0 = _mm_xor_si128(_mm_load_si128(input + i + 0), xout0); + xout1 = _mm_xor_si128(_mm_load_si128(input + i + 1), xout1); + xout2 = _mm_xor_si128(_mm_load_si128(input + i + 2), xout2); + xout3 = _mm_xor_si128(_mm_load_si128(input + i + 3), xout3); + xout4 = _mm_xor_si128(_mm_load_si128(input + i + 4), xout4); + xout5 = _mm_xor_si128(_mm_load_si128(input + i + 5), xout5); + xout6 = _mm_xor_si128(_mm_load_si128(input + i + 6), xout6); + xout7 = _mm_xor_si128(_mm_load_si128(input + i + 7), xout7); + + aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k3, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k4, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k5, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + + mix_and_propagate(xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7); + } + + for (size_t i = 0; i < 16; i++) { + aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k3, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k4, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k5, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + + mix_and_propagate(xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7); + } } _mm_store_si128(output + 4, xout0); @@ -317,11 +393,16 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); + if (VARIANT > 0 && size < 43) { + memset(output, 0, 32); + return; + } + keccak(input, (int) size, ctx->state0, 200); VARIANT1_INIT(0); - cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); const uint8_t* l0 = ctx->memory; uint64_t* h0 = reinterpret_cast(ctx->state0); @@ -363,9 +444,18 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si ah0 ^= ch; al0 ^= cl; idx0 = al0; + + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; + int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; + int64_t q = n / (d | 0x5); + + ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; + idx0 = d ^ q; + } } - cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); + cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); @@ -379,6 +469,11 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); + if (VARIANT > 0 && size < 43) { + memset(output, 0, 64); + return; + } + keccak(input, (int) size, ctx->state0, 200); keccak(input + size, (int) size, ctx->state1, 200); @@ -390,8 +485,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si uint64_t* h0 = reinterpret_cast(ctx->state0); uint64_t* h1 = reinterpret_cast(ctx->state1); - cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); - cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); + cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); + cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); uint64_t al0 = h0[0] ^ h0[4]; uint64_t al1 = h1[0] ^ h1[4]; @@ -446,6 +541,15 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si al0 ^= cl; idx0 = al0; + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; + int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; + int64_t q = n / (d | 0x5); + + ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; + idx0 = d ^ q; + } + cl = ((uint64_t*) &l1[idx1 & MASK])[0]; ch = ((uint64_t*) &l1[idx1 & MASK])[1]; lo = __umul128(idx1, cl, &hi); @@ -461,10 +565,19 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ah1 ^= ch; al1 ^= cl; idx1 = al1; + + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + int64_t n = ((int64_t*)&l1[idx1 & MASK])[0]; + int32_t d = ((int32_t*)&l1[idx1 & MASK])[2]; + int64_t q = n / (d | 0x5); + + ((int64_t*)&l1[idx1 & MASK])[0] = n ^ q; + idx1 = d ^ q; + } } - cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); - cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); + cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); + cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); keccakf(h0, 24); keccakf(h1, 24); diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 49503820..6e7da0c4 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -64,6 +64,10 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL m_pools.push_back(new Url(kDonatePool1, 80, userId, nullptr, false, true)); m_pools.push_back(new Url(kDonatePool2, 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); } + else if (algo == xmrig::CRYPTONIGHT_HEAVY) { + m_pools.push_back(new Url(kDonatePool1, 8888, userId, nullptr, false, true)); + m_pools.push_back(new Url(kDonatePool1, 8889, userId, nullptr, false, true)); + } else { m_pools.push_back(new Url(kDonatePool1, 5555, userId, nullptr, false, true)); m_pools.push_back(new Url(kDonatePool1, 7777, userId, nullptr, false, true)); diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 425dd0c2..273f1555 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -144,7 +144,7 @@ bool DoubleWorker::selfTest() } # endif - return false; + return true; } From d4123b8fa67d8e7709c1caa18b626a0ddf8db9c0 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 3 Apr 2018 17:06:03 +0700 Subject: [PATCH 167/389] Quick fix, temporary use old style affinity. --- src/workers/Handle.cpp | 3 ++- src/workers/Handle.h | 4 +++- src/workers/Worker.cpp | 6 +++--- src/workers/Workers.cpp | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/workers/Handle.cpp b/src/workers/Handle.cpp index 29f57fb2..01d032e9 100644 --- a/src/workers/Handle.cpp +++ b/src/workers/Handle.cpp @@ -25,7 +25,8 @@ #include "workers/Handle.h" -Handle::Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays) : +Handle::Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays, int64_t affinity) : + m_affinity(affinity), m_worker(nullptr), m_totalThreads(totalThreads), m_totalWays(totalWays), diff --git a/src/workers/Handle.h b/src/workers/Handle.h index b3a7c76f..8a64922a 100644 --- a/src/workers/Handle.h +++ b/src/workers/Handle.h @@ -38,10 +38,11 @@ class IWorker; class Handle { public: - Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays); + Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays, int64_t affinity); void join(); void start(void (*callback) (void *)); + inline int64_t affinity() const { return m_affinity; } inline IWorker *worker() const { return m_worker; } inline size_t threadId() const { return m_config->index(); } inline size_t totalThreads() const { return m_totalThreads; } @@ -50,6 +51,7 @@ public: inline xmrig::IThread *config() const { return m_config; } private: + int64_t m_affinity; IWorker *m_worker; size_t m_totalThreads; size_t m_totalWays; diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index bf4f62da..f9162e3f 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -42,9 +42,9 @@ Worker::Worker(Handle *handle) : m_sequence(0), m_thread(static_cast(handle->config())) { -// if (Cpu::threads() > 1 && handle->affinity() != -1L) { -// Cpu::setAffinity(m_id, handle->affinity()); -// } + if (Cpu::threads() > 1 && handle->affinity() != -1L) { + Cpu::setAffinity(m_id, handle->affinity()); + } Platform::setThreadPriority(handle->config()->priority()); m_ctx = Mem::create(m_id); diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 00941c7b..38965d8a 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -130,7 +130,7 @@ void Workers::start(xmrig::Controller *controller) uv_timer_start(&m_timer, Workers::onTick, 500, 500); for (xmrig::IThread *thread : threads) { - Handle *handle = new Handle(thread, threads.size(), totalWays); + Handle *handle = new Handle(thread, threads.size(), totalWays, controller->config()->affinity()); m_workers.push_back(handle); handle->start(Workers::onReady); } From 26e1b1402024c673ad689d0e60221a39e6b45256 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 3 Apr 2018 17:19:01 +0700 Subject: [PATCH 168/389] Added test hashes for self test. --- src/crypto/CryptoNight_test.h | 12 +++++++++++- src/workers/DoubleWorker.cpp | 2 +- src/workers/SingleWorker.cpp | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 0f7feda1..5717f1d3 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -66,7 +66,7 @@ const static uint8_t test_output_v0_lite[64] = { }; -// AEON v2 +// AEON v7 const static uint8_t test_output_v1_lite[64] = { 0x87, 0xC4, 0xE5, 0x70, 0x65, 0x3E, 0xB4, 0xC2, 0xB4, 0x2B, 0x7A, 0x0D, 0x54, 0x65, 0x59, 0x45, 0x2D, 0xFA, 0xB5, 0x73, 0xB8, 0x2E, 0xC5, 0x2F, 0x15, 0x2B, 0x7F, 0xF9, 0x8E, 0x79, 0x44, 0x6F, @@ -76,4 +76,14 @@ const static uint8_t test_output_v1_lite[64] = { #endif +#ifndef XMRIG_NO_SUMO +const static uint8_t test_output_heavy[64] = { + 0x4D, 0x94, 0x7D, 0xD6, 0xDB, 0x6E, 0x07, 0x48, 0x26, 0x4A, 0x51, 0x2E, 0xAC, 0xF3, 0x25, 0x4A, + 0x1F, 0x1A, 0xA2, 0x5B, 0xFC, 0x0A, 0xAD, 0x82, 0xDE, 0xA8, 0x99, 0x96, 0x88, 0x52, 0xD2, 0x7D, + 0x99, 0x83, 0xF2, 0x1B, 0xDF, 0x20, 0x10, 0xA8, 0xD7, 0x07, 0xBB, 0x2F, 0x14, 0xD7, 0x86, 0x64, + 0xBB, 0xE1, 0x18, 0x7F, 0x55, 0x01, 0x4B, 0x39, 0xE5, 0xF3, 0xD6, 0x93, 0x28, 0xE4, 0x8F, 0xC2 +}; +#endif + + #endif /* __CRYPTONIGHT_TEST_H__ */ diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 273f1555..a6176743 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -144,7 +144,7 @@ bool DoubleWorker::selfTest() } # endif - return true; + return memcmp(m_hash, test_output_heavy, 64) == 0; } diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 5757ef77..2d428fcb 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -116,7 +116,7 @@ bool SingleWorker::selfTest() } # endif - return true; + return memcmp(m_result.result, test_output_heavy, 32) == 0; } From 4e8ef7c6ed05eb778ce6799ed674c1429659e9d7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 3 Apr 2018 17:51:06 +0700 Subject: [PATCH 169/389] Added cryptonight-heavy support for ARM --- src/Cpu_arm.cpp | 2 +- src/crypto/CryptoNight_arm.h | 211 +++++++++++++++++++++++++++++++++-- 2 files changed, 202 insertions(+), 11 deletions(-) diff --git a/src/Cpu_arm.cpp b/src/Cpu_arm.cpp index c2047ffb..1b306789 100644 --- a/src/Cpu_arm.cpp +++ b/src/Cpu_arm.cpp @@ -37,7 +37,7 @@ int Cpu::m_totalCores = 0; int Cpu::m_totalThreads = 0; -int Cpu::optimalThreadsCount(int algo, bool doubleHash, int maxCpuUsage) +int Cpu::optimalThreadsCount(xmrig::Algo algo, bool doubleHash, int maxCpuUsage) { return m_totalThreads; } diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 18408536..fd8b58ff 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -35,6 +35,7 @@ #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_constants.h" #include "crypto/CryptoNight_monero.h" #include "crypto/soft_aes.h" @@ -206,7 +207,21 @@ static inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, } -template +inline void mix_and_propagate(__m128i& x0, __m128i& x1, __m128i& x2, __m128i& x3, __m128i& x4, __m128i& x5, __m128i& x6, __m128i& x7) +{ + __m128i tmp0 = x0; + x0 = _mm_xor_si128(x0, x1); + x1 = _mm_xor_si128(x1, x2); + x2 = _mm_xor_si128(x2, x3); + x3 = _mm_xor_si128(x3, x4); + x4 = _mm_xor_si128(x4, x5); + x5 = _mm_xor_si128(x5, x6); + x6 = _mm_xor_si128(x6, x7); + x7 = _mm_xor_si128(x7, tmp0); +} + + +template static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) { __m128i xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7; @@ -223,6 +238,40 @@ static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) xin6 = _mm_load_si128(input + 10); xin7 = _mm_load_si128(input + 11); + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + for (size_t i = 0; i < 16; i++) { + if (!SOFT_AES) { + aes_round(_mm_setzero_si128(), &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + } + + aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k3, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k4, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k5, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k6, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k7, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k8, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + + if (!SOFT_AES) { + xin0 ^= k9; + xin1 ^= k9; + xin2 ^= k9; + xin3 ^= k9; + xin4 ^= k9; + xin5 ^= k9; + xin6 ^= k9; + xin7 ^= k9; + } + else { + aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + } + + mix_and_propagate(xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7); + } + } + for (size_t i = 0; i < MEM / sizeof(__m128i); i += 8) { if (!SOFT_AES) { aes_round(_mm_setzero_si128(), &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); @@ -264,7 +313,7 @@ static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) } -template +template static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) { __m128i xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7; @@ -319,6 +368,85 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) else { aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); } + + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + mix_and_propagate(xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7); + } + } + + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + for (size_t i = 0; i < MEM / sizeof(__m128i); i += 8) { + xout0 = _mm_xor_si128(_mm_load_si128(input + i + 0), xout0); + xout1 = _mm_xor_si128(_mm_load_si128(input + i + 1), xout1); + xout2 = _mm_xor_si128(_mm_load_si128(input + i + 2), xout2); + xout3 = _mm_xor_si128(_mm_load_si128(input + i + 3), xout3); + xout4 = _mm_xor_si128(_mm_load_si128(input + i + 4), xout4); + xout5 = _mm_xor_si128(_mm_load_si128(input + i + 5), xout5); + xout6 = _mm_xor_si128(_mm_load_si128(input + i + 6), xout6); + xout7 = _mm_xor_si128(_mm_load_si128(input + i + 7), xout7); + + if (!SOFT_AES) { + aes_round(_mm_setzero_si128(), &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + } + + aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k3, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k4, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k5, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + + if (!SOFT_AES) { + xout0 ^= k9; + xout1 ^= k9; + xout2 ^= k9; + xout3 ^= k9; + xout4 ^= k9; + xout5 ^= k9; + xout6 ^= k9; + xout7 ^= k9; + } + else { + aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + } + + mix_and_propagate(xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7); + } + + for (size_t i = 0; i < 16; i++) { + if (!SOFT_AES) { + aes_round(_mm_setzero_si128(), &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + } + + aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k3, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k4, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k5, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + + if (!SOFT_AES) { + xout0 ^= k9; + xout1 ^= k9; + xout2 ^= k9; + xout3 ^= k9; + xout4 ^= k9; + xout5 ^= k9; + xout6 ^= k9; + xout7 ^= k9; + } + else { + aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + } + + mix_and_propagate(xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7); + } } _mm_store_si128(output + 4, xout0); @@ -332,14 +460,23 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template +template inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx *__restrict__ ctx) { + constexpr size_t MASK = xmrig::cn_select_mask(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t MEM = xmrig::cn_select_memory(); + + if (VARIANT > 0 && size < 43) { + memset(output, 0, 32); + return; + } + keccak(input, (int) size, ctx->state0, 200); VARIANT1_INIT(0); - cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); const uint8_t* l0 = ctx->memory; uint64_t* h0 = reinterpret_cast(ctx->state0); @@ -384,18 +521,36 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si ah0 ^= ch; al0 ^= cl; idx0 = al0; + + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; + int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; + int64_t q = n / (d | 0x5); + + ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; + idx0 = d ^ q; + } } - cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); + cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); } -template +template inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) { + constexpr size_t MASK = xmrig::cn_select_mask(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t MEM = xmrig::cn_select_memory(); + + if (VARIANT > 0 && size < 43) { + memset(output, 0, 64); + return; + } + keccak(input, (int) size, ctx->state0, 200); keccak(input + size, (int) size, ctx->state1, 200); @@ -407,8 +562,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si uint64_t* h0 = reinterpret_cast(ctx->state0); uint64_t* h1 = reinterpret_cast(ctx->state1); - cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); - cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); + cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); + cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); uint64_t al0 = h0[0] ^ h0[4]; uint64_t al1 = h1[0] ^ h1[4]; @@ -465,6 +620,15 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si al0 ^= cl; idx0 = al0; + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; + int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; + int64_t q = n / (d | 0x5); + + ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; + idx0 = d ^ q; + } + cl = ((uint64_t*) &l1[idx1 & MASK])[0]; ch = ((uint64_t*) &l1[idx1 & MASK])[1]; lo = __umul128(idx1, cl, &hi); @@ -480,10 +644,19 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ah1 ^= ch; al1 ^= cl; idx1 = al1; + + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + int64_t n = ((int64_t*)&l1[idx1 & MASK])[0]; + int32_t d = ((int32_t*)&l1[idx1 & MASK])[2]; + int64_t q = n / (d | 0x5); + + ((int64_t*)&l1[idx1 & MASK])[0] = n ^ q; + idx1 = d ^ q; + } } - cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); - cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); + cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); + cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); keccakf(h0, 24); keccakf(h1, 24); @@ -492,4 +665,22 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, output + 32); } + +template +inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +{ +} + + +template +inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +{ +} + + +template +inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +{ +} + #endif /* __CRYPTONIGHT_ARM_H__ */ From 6b710ff3b31e82f46995c83ce12a57793067b3e7 Mon Sep 17 00:00:00 2001 From: xmrig Date: Tue, 3 Apr 2018 18:12:24 +0700 Subject: [PATCH 170/389] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9f42d66..397d3b62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,10 @@ # v2.6.0-beta1 + - [#476](https://github.com/xmrig/xmrig/issues/476) **Added Cryptonight-Heavy support for Sumokoin ASIC resistance fork.** - HTTP server now runs in main loop, it make possible easy extend API without worry about thread synchronization. - Added initial graceful reload support, miner will reload configuration if config file changed, disabled by default until it will be fully implemented and tested. - Added API endpoint `PUT /1/config` to update current config. - Added API endpoint `GET /1/config` to get current active config. + - Added API endpoint `GET /1/threads` to get current active threads configuration. - API endpoint `GET /` now deprecated, use `GET /1/summary` instead. - Added `--api-no-ipv6` and similar config option to disable IPv6 support for HTTP API. - Added `--api-no-restricted` to enable full access to api, this option has no effect if `--api-access-token` not specified. From 6a1c52b904f00e33570fb034134ba529f168108e Mon Sep 17 00:00:00 2001 From: xmrig Date: Tue, 3 Apr 2018 19:02:12 +0700 Subject: [PATCH 171/389] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 962408e9..3cf26d17 100644 --- a/README.md +++ b/README.md @@ -124,10 +124,10 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -b070d06a3615f3db67ad3beab43d6d21f3c88026aa2b4726a93df47145cd30ec xmrig-2.5.2-xenial-amd64.tar.gz/xmrig-2.5.2/xmrig -4852135d3f04fd450ba39abce51ca40ff9131d222220c8b30804be05f6679295 xmrig-2.5.2-gcc-win32.zip/xmrig.exe -284309d07f08261af19c937ece6d2031910d9124a7359c207ded65890b2d7c5f xmrig-2.5.2-gcc-win64.zip/xmrig.exe -e1dc46158a578fb030538fb06e5663a6acc5763545fb447a00ce0a6b388c5226 xmrig-2.5.2-msvc-win64.zip/xmrig.exe +bd14bc3cfd9528e4a7583ab39aecc876250333e1e0faab83781584bb7f65e3eb xmrig-2.6.0-beta1-xenial-amd64.tar.gz/xmrig-2.6.0-beta1/xmrig +32eebf71e5631029202ae5cbf6f03caad912f1722aa86a1be01a26d491801aba xmrig-2.6.0-beta1-gcc-win32.zip/xmrig.exe +1cc08844ff019408e2e2c9560fee0c4e0b2dbc2a72bcc1c1da4a847a1787eca3 xmrig-2.6.0-beta1-gcc-win64.zip/xmrig.exe +fdf99aa8e7792a34b1be0cc6c77e4e83ff9a4b21abb27989f7927dcfed82f6e2 xmrig-2.6.0-beta1-msvc-win64.zip/xmrig.exe ``` ## Contacts From c227e3c7b42d5371b12fa93fb3e2484e46f02638 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 4 Apr 2018 00:10:53 +0700 Subject: [PATCH 172/389] Fix command line option --donate-level. --- src/core/CommonConfig.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index 6a2c76ac..e4e03b1e 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -233,6 +233,7 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) case VariantKey: /* --variant */ case ApiPort: /* --api-port */ case PrintTimeKey: /* --cpu-priority */ + case DonateLevelKey: /* --donate-level */ return parseUint64(key, strtol(arg, nullptr, 10)); case BackgroundKey: /* --background */ @@ -248,7 +249,7 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) return parseBoolean(key, false); # ifdef XMRIG_PROXY_PROJECT - case 1003: /* --donate-level */ + case DonateLevelKey: /* --donate-level */ if (strncmp(arg, "minemonero.pro", 14) == 0) { m_donateLevel = 0; } From 3a67ee6d11448b8c046a373ae874461798d96a63 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 6 Apr 2018 23:32:54 +0700 Subject: [PATCH 173/389] Small fixes. --- src/core/CommonConfig.cpp | 17 ++++++++++------- src/crypto/CryptoNight_constants.h | 6 ++++++ src/workers/SingleWorker.cpp | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index e4e03b1e..cf18d896 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -58,7 +58,13 @@ xmrig::CommonConfig::CommonConfig() : m_background(false), m_colors(true), m_syslog(false), + +# ifdef XMRIG_PROXY_PROJECT + m_watch(true), +# else m_watch(false), // TODO: enable config file watch by default when this feature propertly handled and tested. +# endif + m_apiToken(nullptr), m_apiWorkerId(nullptr), m_fileName(nullptr), @@ -233,7 +239,6 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) case VariantKey: /* --variant */ case ApiPort: /* --api-port */ case PrintTimeKey: /* --cpu-priority */ - case DonateLevelKey: /* --donate-level */ return parseUint64(key, strtol(arg, nullptr, 10)); case BackgroundKey: /* --background */ @@ -248,16 +253,14 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) case ApiIPv6Key: /* --api-no-ipv6 */ return parseBoolean(key, false); -# ifdef XMRIG_PROXY_PROJECT case DonateLevelKey: /* --donate-level */ +# ifdef XMRIG_PROXY_PROJECT if (strncmp(arg, "minemonero.pro", 14) == 0) { m_donateLevel = 0; + return true; } - else { - parseUint64(key, strtol(arg, nullptr, 10)); - } - break; -# endif +# endif + return parseUint64(key, strtol(arg, nullptr, 10)); default: break; diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h index 5d02baf9..d576b47a 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/CryptoNight_constants.h @@ -66,6 +66,8 @@ inline size_t cn_select_memory(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_MEMORY; } + + return 0; } @@ -87,6 +89,8 @@ inline uint32_t cn_select_mask(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_MASK; } + + return 0; } @@ -108,6 +112,8 @@ inline uint32_t cn_select_iter(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_ITER; } + + return 0; } diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 2d428fcb..ca6b93a6 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -116,7 +116,7 @@ bool SingleWorker::selfTest() } # endif - return memcmp(m_result.result, test_output_heavy, 32) == 0; + return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_result.result, test_output_heavy, 32) == 0; } From ae647699a4780af83b7c9cec22ee23e7facb056c Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 7 Apr 2018 00:44:48 +0700 Subject: [PATCH 174/389] #502 Fixed build without libmicrohttpd. --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 106ab30f..43a7c1d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,6 @@ include (cmake/cpu.cmake) set(HEADERS - src/api/Api.h src/api/NetworkState.h src/App.h src/Console.h @@ -87,7 +86,6 @@ else() endif() set(SOURCES - src/api/Api.cpp src/api/NetworkState.cpp src/App.cpp src/Console.cpp @@ -214,11 +212,13 @@ if (WITH_HTTPD) if (MHD_FOUND) include_directories(${MHD_INCLUDE_DIRS}) set(HTTPD_SOURCES + src/api/Api.h src/api/ApiRouter.h src/api/HttpBody.h src/api/Httpd.h src/api/HttpReply.h src/api/HttpRequest.h + src/api/Api.cpp src/api/ApiRouter.cpp src/api/Httpd.cpp src/api/HttpRequest.cpp From bca087f1110ac3977ad42f8616ffa49a45f9bfb2 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 7 Apr 2018 00:50:24 +0700 Subject: [PATCH 175/389] Update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 397d3b62..4f0d5a98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# v2.6.0-beta2 +- Fixed regressions (v2.6.0-beta1 affected) + - [#494](https://github.com/xmrig/xmrig/issues/494) Command line option `--donate-level` was broken. + - [#502](https://github.com/xmrig/xmrig/issues/502) Build without libmicrohttpd was broken. + # v2.6.0-beta1 - [#476](https://github.com/xmrig/xmrig/issues/476) **Added Cryptonight-Heavy support for Sumokoin ASIC resistance fork.** - HTTP server now runs in main loop, it make possible easy extend API without worry about thread synchronization. From 3b83fa530c6119b15aba964e8b97a52ad72dae4e Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 7 Apr 2018 01:17:58 +0700 Subject: [PATCH 176/389] #499 Disabled IPv6 for internal HTTP API by default, was cause issues on some systems. --- src/config.json | 4 +++- src/core/CommonConfig.cpp | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/config.json b/src/config.json index 858435d2..3013ba62 100644 --- a/src/config.json +++ b/src/config.json @@ -27,6 +27,8 @@ "api": { "port": 0, // port for the miner API https://github.com/xmrig/xmrig/wiki/API "access-token": null, // access token for API - "worker-id": null // custom worker-id for API + "worker-id": null, // custom worker-id for API + "ipv6": false, + "restricted": true } } \ No newline at end of file diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index cf18d896..8d2eb03a 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -53,7 +53,7 @@ static const char *algoNames[] = { xmrig::CommonConfig::CommonConfig() : m_algorithm(CRYPTONIGHT), m_adjusted(false), - m_apiIPv6(true), + m_apiIPv6(false), m_apiRestricted(true), m_background(false), m_colors(true), From 89c095f79e0fa48a6c1b77beb3fc80297b6324d4 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 7 Apr 2018 01:21:56 +0700 Subject: [PATCH 177/389] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f0d5a98..c67b8a20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # v2.6.0-beta2 +- [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 disabled for internal HTTP API by default, was cause issues on some systems. - Fixed regressions (v2.6.0-beta1 affected) - [#494](https://github.com/xmrig/xmrig/issues/494) Command line option `--donate-level` was broken. - [#502](https://github.com/xmrig/xmrig/issues/502) Build without libmicrohttpd was broken. From 1c2b5acb2c6fd848ea5472595cb6f5b74c1c8725 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 8 Apr 2018 02:31:04 +0700 Subject: [PATCH 178/389] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08d58966..e2801fa6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # XMRig -:warning: **You must update miners to version 2.5 before April 6 due [Monero PoW change](https://getmonero.org/2018/02/11/PoW-change-and-key-reuse.html), also check issue [#482](https://github.com/xmrig/xmrig/issues/482)** +:warning: **If you mine Monero, Aeon, Sumokoin, Turtlecoin, Stellite, GRAFT, Haven Protocol, IPBC, [PLEASE READ](https://github.com/xmrig/xmrig/issues/482)!** :warning: [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub release](https://img.shields.io/github/release/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) From bb2faaddc062449cc5d4f1d4db4e388cba83b828 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 8 Apr 2018 02:44:31 +0700 Subject: [PATCH 179/389] Added short aliases for algorithm names: cn, cn-lite and cn-heavy. --- CHANGELOG.md | 1 + src/core/CommonConfig.cpp | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c67b8a20..bcaf27c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # v2.6.0-beta2 - [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 disabled for internal HTTP API by default, was cause issues on some systems. +- Added short aliases for algorithm names: `cn`, `cn-lite` and `cn-heavy`. - Fixed regressions (v2.6.0-beta1 affected) - [#494](https://github.com/xmrig/xmrig/issues/494) Command line option `--donate-level` was broken. - [#502](https://github.com/xmrig/xmrig/issues/502) Build without libmicrohttpd was broken. diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index 8d2eb03a..8950775c 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -22,6 +22,7 @@ */ +#include #include #include #include @@ -45,6 +46,13 @@ static const char *algoNames[] = { }; +static const char *algoNamesShort[] = { + "cn", + "cn-lite", + "cn-heavy" +}; + + #if defined(_WIN32) && !defined(strcasecmp) # define strcasecmp _stricmp #endif @@ -375,12 +383,21 @@ void xmrig::CommonConfig::setAlgo(const char *algo) return; } - const size_t size = sizeof(algoNames) / sizeof((algoNames)[0]); + const size_t size = sizeof(algoNames) / sizeof(algoNames[0]); + + assert(size == (sizeof(algoNamesShort) / sizeof(algoNamesShort[0]))); for (size_t i = 0; i < size; i++) { if (algoNames[i] && strcasecmp(algo, algoNames[i]) == 0) { m_algorithm = static_cast(i); - break; + return; + } + } + + for (size_t i = 0; i < size; i++) { + if (algoNamesShort[i] && strcasecmp(algo, algoNamesShort[i]) == 0) { + m_algorithm = static_cast(i); + return; } } } From eb56c2b56eb799097629dfd2f360a9138865db8d Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 8 Apr 2018 03:20:44 +0700 Subject: [PATCH 180/389] Better implementation for algorithm aliases. --- src/core/CommonConfig.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index 8950775c..a36b6553 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -388,16 +388,9 @@ void xmrig::CommonConfig::setAlgo(const char *algo) assert(size == (sizeof(algoNamesShort) / sizeof(algoNamesShort[0]))); for (size_t i = 0; i < size; i++) { - if (algoNames[i] && strcasecmp(algo, algoNames[i]) == 0) { + if (strcasecmp(algo, algoNames[i]) == 0 || strcasecmp(algo, algoNamesShort[i]) == 0) { m_algorithm = static_cast(i); - return; - } - } - - for (size_t i = 0; i < size; i++) { - if (algoNamesShort[i] && strcasecmp(algo, algoNamesShort[i]) == 0) { - m_algorithm = static_cast(i); - return; + break; } } } From de83cfd53c1da14ac80a91d9d130ef140b762747 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 8 Apr 2018 22:19:21 +0700 Subject: [PATCH 181/389] #519 Fix donation start time randomization. --- src/core/CommonConfig.cpp | 2 +- src/donate.h | 13 +++++++++---- src/net/strategies/DonateStrategy.cpp | 6 +++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index a36b6553..5b8a32ab 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -349,7 +349,7 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) break; case DonateLevelKey: /* --donate-level */ - if (arg >= kMinDonateLevel && arg <= 99) { + if (arg >= kMinimumDonateLevel && arg <= 99) { m_donateLevel = arg; } break; diff --git a/src/donate.h b/src/donate.h index bdf7a00d..46f26b73 100644 --- a/src/donate.h +++ b/src/donate.h @@ -29,9 +29,14 @@ * Dev donation. * * Percentage of your hashing power that you want to donate to the developer, can be 0 if you don't want to do that. - * Example of how it works for the default setting of 1: - * You miner will mine into your usual pool for 99 minutes, then switch to the developer's pool for 1 minute. - * Since v2.5.1 start time randomized in range from 50 to 150 minutes minus donation time. + * + * Example of how it works for the setting of 1%: + * You miner will mine into your usual pool for random time (in range from 49.5 to 148.5 minutes), + * then switch to the developer's pool for 1 minute, then switch again to your pool for 99 minutes + * and then switch agaiin to developer's pool for 1 minute, these rounds will continue until miner working. + * + * Randomised only first round, to prevent waves on the donation pool. + * * Switching is instant, and only happens after a successful connection, so you never loose any hashes. * * If you plan on changing this setting to 0 please consider making a one off donation to my wallet: @@ -39,7 +44,7 @@ * BTC: 1P7ujsXeX7GxQwHNnJsRMgAdNkFZmNVqJT */ constexpr const int kDefaultDonateLevel = 5; -constexpr const int kMinDonateLevel = 1; +constexpr const int kMinimumDonateLevel = 1; #endif /* __DONATE_H__ */ diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 6e7da0c4..f0ec5bde 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -41,8 +41,8 @@ const static char *kDonatePool1 = "miner.fee.xmrig.com"; const static char *kDonatePool2 = "emergency.fee.xmrig.com"; -static inline int random(int min, int max){ - return min + rand() / (RAND_MAX / (max - min + 1) + 1); +static inline float randomf(float min, float max) { + return (max - min) * ((((float) rand()) / (float) RAND_MAX)) + min; } @@ -78,7 +78,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL m_timer.data = this; uv_timer_init(uv_default_loop(), &m_timer); - idle(random(3000, 9000) * 1000 - m_donateTime); + idle(m_idleTime * randomf(0.5, 1.5)); } From 77207eaaae32922462fcfa788f39176a405b1437 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 9 Apr 2018 20:38:02 +0700 Subject: [PATCH 182/389] Fix build with APP_DEBUG. --- src/Summary.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Summary.cpp b/src/Summary.cpp index 7e341ce0..56f3f522 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -123,7 +123,7 @@ static void print_pools(xmrig::Config *config) # ifdef APP_DEBUG for (size_t i = 0; i < pools.size(); ++i) { - Log::i()->text("%s:%d, user: %s, pass: %s, ka: %d, nicehash: %d", pools[i]->host(), pools[i]->port(), pools[i]->user(), pools[i]->password(), pools[i]->isKeepAlive(), pools[i]->isNicehash()); + Log::i()->text("%s:%d, user: %s, pass: %s, ka: %d, nicehash: %d", pools[i]->host(), pools[i]->port(), pools[i]->user(), pools[i]->password(), pools[i]->keepAlive(), pools[i]->isNicehash()); } # endif } From 5b664f368159624d3843ab4df718ec7b3e9c6b59 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 9 Apr 2018 21:21:46 +0700 Subject: [PATCH 183/389] Fix nonce calculation for --av 4. --- src/workers/CpuThread.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index b8be966f..7e68a990 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -146,28 +146,28 @@ xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, A softAES = true; break; - case AV_DOUBLE: - multiway = DoubleWay; case AV_DOUBLE_SOFT: softAES = true; + case AV_DOUBLE: + multiway = DoubleWay; break; - case AV_TRIPLE: - multiway = TripleWay; case AV_TRIPLE_SOFT: softAES = true; + case AV_TRIPLE: + multiway = TripleWay; break; - case AV_QUAD: - multiway = QuadWay; case AV_QUAD_SOFT: softAES = true; + case AV_QUAD: + multiway = QuadWay; break; - case AV_PENTA: - multiway = PentaWay; case AV_PENTA_SOFT: softAES = true; + case AV_PENTA: + multiway = PentaWay; break; default: From ccbb78d4e989f18e1d89931cc6ba01a851fdb8a9 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 9 Apr 2018 21:56:15 +0700 Subject: [PATCH 184/389] Improved performance for cryptonight v7. --- CHANGELOG.md | 4 +++- src/crypto/CryptoNight_x86.h | 38 +++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcaf27c7..95575c3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ # v2.6.0-beta2 +- Improved performance for `cryptonight v7` especially in double hash mode. - [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 disabled for internal HTTP API by default, was cause issues on some systems. - Added short aliases for algorithm names: `cn`, `cn-lite` and `cn-heavy`. - Fixed regressions (v2.6.0-beta1 affected) - [#494](https://github.com/xmrig/xmrig/issues/494) Command line option `--donate-level` was broken. - - [#502](https://github.com/xmrig/xmrig/issues/502) Build without libmicrohttpd was broken. + - [#502](https://github.com/xmrig/xmrig/issues/502) Build without libmicrohttpd was broken. + - Fixed nonce calculation for `--av 4` (software AES, double hash) was cause reduction of effective hashrate and rejected shares on nicehash. # v2.6.0-beta1 - [#476](https://github.com/xmrig/xmrig/issues/476) **Added Cryptonight-Heavy support for Sumokoin ASIC resistance fork.** diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 414a1f7f..417404a6 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -386,6 +386,22 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } +static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) +{ + mem_out[0] = EXTRACT64(tmp); + + tmp = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(tmp), _mm_castsi128_ps(tmp))); + uint64_t vh = EXTRACT64(tmp); + + uint8_t x = vh >> 24; + static const uint16_t table = 0x7531; + const uint8_t index = (((x >> 3) & 6) | (x & 1)) << 1; + vh ^= ((table >> index) & 0x3) << 28; + + mem_out[1] = vh; +} + + template inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx *__restrict__ ctx) { @@ -400,7 +416,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si keccak(input, (int) size, ctx->state0, 200); - VARIANT1_INIT(0); + VARIANT1_INIT(0) cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); @@ -423,8 +439,13 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); } - _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); - VARIANT1_1(&l0[idx0 & MASK]); + + if (VARIANT > 0) { + cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + } else { + _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + } + idx0 = EXTRACT64(cx); bx0 = cx; @@ -513,10 +534,13 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); } - _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); - _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); - VARIANT1_1(&l0[idx0 & MASK]); - VARIANT1_1(&l1[idx1 & MASK]); + if (VARIANT > 0) { + cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); + cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + } else { + _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); + _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + } idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); From 82e1138158e793df1cddec701ca63db0057cfdf2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 9 Apr 2018 23:50:01 +0700 Subject: [PATCH 185/389] v2.6.0-beta2 --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index db7c2013..4ea70a57 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.6.0-beta1" +#define APP_VERSION "2.6.0-beta2" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -36,7 +36,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 6 #define APP_VER_BUILD 0 -#define APP_VER_REV 1 +#define APP_VER_REV 2 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From 30903686bd67f7843bec84bd99f7a56c902f206f Mon Sep 17 00:00:00 2001 From: xmrig Date: Tue, 10 Apr 2018 00:30:06 +0700 Subject: [PATCH 186/389] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3cf26d17..010a03fc 100644 --- a/README.md +++ b/README.md @@ -124,10 +124,10 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -bd14bc3cfd9528e4a7583ab39aecc876250333e1e0faab83781584bb7f65e3eb xmrig-2.6.0-beta1-xenial-amd64.tar.gz/xmrig-2.6.0-beta1/xmrig -32eebf71e5631029202ae5cbf6f03caad912f1722aa86a1be01a26d491801aba xmrig-2.6.0-beta1-gcc-win32.zip/xmrig.exe -1cc08844ff019408e2e2c9560fee0c4e0b2dbc2a72bcc1c1da4a847a1787eca3 xmrig-2.6.0-beta1-gcc-win64.zip/xmrig.exe -fdf99aa8e7792a34b1be0cc6c77e4e83ff9a4b21abb27989f7927dcfed82f6e2 xmrig-2.6.0-beta1-msvc-win64.zip/xmrig.exe +232af0c5f3b1cdbc2d90b514873a764b434d5621d2790da67954b35c17e44fe3 xmrig-2.6.0-beta2-xenial-amd64.tar.gz/xmrig-2.6.0-beta2/xmrig +2366a06729d4de538ef511862bf11d0c7ad40fd245e7aeab3c1957307d63471a xmrig-2.6.0-beta2-gcc-win32.zip/xmrig.exe +2f6538c765e001d13ca380cbc1558d51efcb97d4bccdfa40993cb872be4e9efd xmrig-2.6.0-beta2-gcc-win64.zip/xmrig.exe +3c0479acb78a3cee8fe416ee438dbff09c786acf50fbaf28a820127fcd0c6e62 xmrig-2.6.0-beta2-msvc-win64.zip/xmrig.exe ``` ## Contacts From 916ff330589bf61d324a6b6bc99978676506ac81 Mon Sep 17 00:00:00 2001 From: xmrig Date: Tue, 10 Apr 2018 03:56:19 +0700 Subject: [PATCH 187/389] Update CHANGELOG.md --- CHANGELOG.md | 310 +++++++++++++++++++++++++-------------------------- 1 file changed, 155 insertions(+), 155 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95575c3d..71d4e5b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,156 +1,156 @@ -# v2.6.0-beta2 -- Improved performance for `cryptonight v7` especially in double hash mode. -- [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 disabled for internal HTTP API by default, was cause issues on some systems. -- Added short aliases for algorithm names: `cn`, `cn-lite` and `cn-heavy`. -- Fixed regressions (v2.6.0-beta1 affected) - - [#494](https://github.com/xmrig/xmrig/issues/494) Command line option `--donate-level` was broken. +# v2.6.0-beta2 +- Improved performance for `cryptonight v7` especially in double hash mode. +- [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 disabled for internal HTTP API by default, was causing issues on some systems. +- Added short aliases for algorithm names: `cn`, `cn-lite` and `cn-heavy`. +- Fixed regressions (v2.6.0-beta1 affected) + - [#494](https://github.com/xmrig/xmrig/issues/494) Command line option `--donate-level` was broken. - [#502](https://github.com/xmrig/xmrig/issues/502) Build without libmicrohttpd was broken. - - Fixed nonce calculation for `--av 4` (software AES, double hash) was cause reduction of effective hashrate and rejected shares on nicehash. - -# v2.6.0-beta1 - - [#476](https://github.com/xmrig/xmrig/issues/476) **Added Cryptonight-Heavy support for Sumokoin ASIC resistance fork.** - - HTTP server now runs in main loop, it make possible easy extend API without worry about thread synchronization. - - Added initial graceful reload support, miner will reload configuration if config file changed, disabled by default until it will be fully implemented and tested. - - Added API endpoint `PUT /1/config` to update current config. - - Added API endpoint `GET /1/config` to get current active config. - - Added API endpoint `GET /1/threads` to get current active threads configuration. - - API endpoint `GET /` now deprecated, use `GET /1/summary` instead. - - Added `--api-no-ipv6` and similar config option to disable IPv6 support for HTTP API. - - Added `--api-no-restricted` to enable full access to api, this option has no effect if `--api-access-token` not specified. - -# v2.5.2 -- [#448](https://github.com/xmrig/xmrig/issues/478) Fixed broken reconnect. - -# v2.5.1 -- [#454](https://github.com/xmrig/xmrig/issues/454) Fixed build with libmicrohttpd version below v0.9.35. -- [#456](https://github.com/xmrig/xmrig/issues/459) Verbose errors related to donation pool was not fully silenced. -- [#459](https://github.com/xmrig/xmrig/issues/459) Fixed regression (version 2.5.0 affected) with connection to **xmr.f2pool.com**. - -# v2.5.0 -- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on April 6.** -- Added full IPv6 support. -- Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. -- [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. -- [#428](https://github.com/xmrig/xmrig/issues/428) Fixed regression (version 2.4.5 affected) with CPU cache size detection. - -# v2.4.5 -- [#324](https://github.com/xmrig/xmrig/pull/324) Fixed build without libmicrohttpd (CMake cache issue). -- [#341](https://github.com/xmrig/xmrig/issues/341) Fixed wrong exit code and added command line option `--dry-run`. -- [#385](https://github.com/xmrig/xmrig/pull/385) Up to 20% performance increase for non-AES CPU and fixed Intel Core 2 cache detection. - -# v2.4.4 - - Added libmicrohttpd version to --version output. - - Fixed bug in singal handler, in some cases miner wasn't shutdown properly. - - Fixed recent MSVC 2017 version detection. - - [#279](https://github.com/xmrig/xmrig/pull/279) Fixed build on some macOS versions. - -# v2.4.3 - - [#94](https://github.com/xmrig/xmrig/issues/94#issuecomment-342019257) [#216](https://github.com/xmrig/xmrig/issues/216) Added **ARMv8** and **ARMv7** support. Hardware AES supported, thanks [Imran Yusuff](https://github.com/imranyusuff). - - [#157](https://github.com/xmrig/xmrig/issues/157) [#196](https://github.com/xmrig/xmrig/issues/196) Fixed Linux compile issues. - - [#184](https://github.com/xmrig/xmrig/issues/184) Fixed cache size detection for CPUs with disabled Hyper-Threading. - - [#200](https://github.com/xmrig/xmrig/issues/200) In some cases miner was doesn't write log to stdout. - -# v2.4.2 - - [#60](https://github.com/xmrig/xmrig/issues/60) Added FreeBSD support, thanks [vcambur](https://github.com/vcambur). - - [#153](https://github.com/xmrig/xmrig/issues/153) Fixed issues with dwarfpool.com. - -# v2.4.1 - - [#147](https://github.com/xmrig/xmrig/issues/147) Fixed comparability with monero-stratum. - -# v2.4.0 - - Added [HTTP API](https://github.com/xmrig/xmrig/wiki/API). - - Added comments support in config file. - - libjansson replaced to rapidjson. - - [#98](https://github.com/xmrig/xmrig/issues/98) Ignore `keepalive` option with minergate.com and nicehash.com. - - [#101](https://github.com/xmrig/xmrig/issues/101) Fixed MSVC 2017 (15.3) compile time version detection. - - [#108](https://github.com/xmrig/xmrig/issues/108) Silently ignore invalid values for `donate-level` option. - - [#111](https://github.com/xmrig/xmrig/issues/111) Fixed build without AEON support. - -# v2.3.1 -- [#68](https://github.com/xmrig/xmrig/issues/68) Fixed compatibility with Docker containers, was nothing print on console. - -# v2.3.0 -- Added `--cpu-priority` option (0 idle, 2 normal to 5 highest). -- Added `--user-agent` option, to set custom user-agent string for pool. For example `cpuminer-multi/0.1`. -- Added `--no-huge-pages` option, to disable huge pages support. -- [#62](https://github.com/xmrig/xmrig/issues/62) Don't send the login to the dev pool. -- Force reconnect if pool block miner IP address. helps switch to backup pool. -- Fixed: failed open default config file if path contains non English characters. -- Fixed: error occurred if try use unavailable stdin or stdout, regression since version 2.2.0. -- Fixed: message about huge pages support successfully enabled on Windows was not shown in release builds. - -# v2.2.1 -- Fixed [terminal issues](https://github.com/xmrig/xmrig-proxy/issues/2#issuecomment-319914085) after exit on Linux and OS X. - -# v2.2.0 -- [#46](https://github.com/xmrig/xmrig/issues/46) Restored config file support. Now possible use multiple config files and combine with command line options also added support for default config. -- Improved colors support on Windows, now used uv_tty, legacy code removed. -- QuickEdit Mode now disabled on Windows. -- Added interactive commands in console window:: **h**ashrate, **p**ause, **r**esume. -- Fixed autoconf mode for AMD FX CPUs. - -# v2.1.0 -- [#40](https://github.com/xmrig/xmrig/issues/40) -Improved miner shutdown, fixed crash on exit for Linux and OS X. -- Fixed, login request was contain malformed JSON if username or password has some special characters for example `\`. -- [#220](https://github.com/fireice-uk/xmr-stak-cpu/pull/220) Better support for Round Robin DNS, IP address now always chosen randomly instead of stuck on first one. -- Changed donation address, new [xmrig-proxy](https://github.com/xmrig/xmrig-proxy) is coming soon. - -# v2.0.2 -- Better deal with possible duplicate jobs from pool, show warning and ignore duplicates. -- For Windows builds libuv updated to version 1.13.1 and gcc to 7.1.0. - -# v2.0.1 - - [#27](https://github.com/xmrig/xmrig/issues/27) Fixed possibility crash on 32bit systems. - -# v2.0.0 - - Option `--backup-url` removed, instead now possibility specify multiple pools for example: `-o example1.com:3333 -u user1 -p password1 -k -o example2.com:5555 -u user2 -o example3.com:4444 -u user3` - - [#15](https://github.com/xmrig/xmrig/issues/15) Added option `-l, --log-file=FILE` to write log to file. - - [#15](https://github.com/xmrig/xmrig/issues/15) Added option `-S, --syslog` to use syslog for logging, Linux only. - - [#18](https://github.com/xmrig/xmrig/issues/18) Added nice messages for accepted/rejected shares with diff and network latency. - - [#20](https://github.com/xmrig/xmrig/issues/20) Fixed `--cpu-affinity` for more than 32 threads. - - Fixed Windows XP support. - - Fixed regression, option `--no-color` was not fully disable colored output. - - Show resolved pool IP address in miner output. - -# v1.0.1 -- Fix broken software AES implementation, app has crashed if CPU not support AES-NI, only version 1.0.0 affected. - -# v1.0.0 -- Miner complete rewritten in C++ with libuv. -- This version should be fully compatible (except config file) with previos versions, many new nice features will come in next versions. -- This is still beta. If you found regression, stability or perfomance issues or have an idea for new feature please fell free to open new [issue](https://github.com/xmrig/xmrig/issues/new). -- Added new option `--print-time=N`, print hashrate report every N seconds. -- New hashrate reports, by default every 60 secons. -- Added Microsoft Visual C++ 2015 and 2017 support. -- Removed dependency on libcurl. -- To compile this version from source please switch to [dev](https://github.com/xmrig/xmrig/tree/dev) branch. - -# v0.8.2 -- Fixed L2 cache size detection for AMD CPUs (Bulldozer/Piledriver/Steamroller/Excavator architecture). - -# v0.8.2 -- Fixed L2 cache size detection for AMD CPUs (Bulldozer/Piledriver/Steamroller/Excavator architecture). -- Fixed gcc 7.1 support. - -# v0.8.1 -- Added nicehash support, detects automaticaly by pool URL, for example `cryptonight.eu.nicehash.com:3355` or manually via option `--nicehash`. - -# v0.8.0 -- Added double hash mode, also known as lower power mode. `--av=2` and `--av=4`. -- Added smart automatic CPU configuration. Default threads count now depends on size of the L3 cache of CPU. -- Added CryptoNight-Lite support for AEON `-a cryptonight-lite`. -- Added `--max-cpu-usage` option for auto CPU configuration mode. -- Added `--safe` option for adjust threads and algorithm variations to current CPU. -- No more manual steps to enable huge pages on Windows. XMRig will do it automatically. -- Removed BMI2 algorithm variation. -- Removed default pool URL. - -# v0.6.0 -- Added automatic cryptonight self test. -- New software AES algorithm variation. Will be automatically selected if cpu not support AES-NI. -- Added 32 bit builds. -- Documented [algorithm variations](https://github.com/xmrig/xmrig#algorithm-variations). - -# v0.5.0 -- Initial public release. + - Fixed nonce calculation for `--av 4` (software AES, double hash) was causing reduction of effective hashrate and rejected shares on nicehash. + +# v2.6.0-beta1 + - [#476](https://github.com/xmrig/xmrig/issues/476) **Added Cryptonight-Heavy support for Sumokoin ASIC resistance fork.** + - HTTP server now runs in main loop, it make possible easy extend API without worry about thread synchronization. + - Added initial graceful reload support, miner will reload configuration if config file changed, disabled by default until it will be fully implemented and tested. + - Added API endpoint `PUT /1/config` to update current config. + - Added API endpoint `GET /1/config` to get current active config. + - Added API endpoint `GET /1/threads` to get current active threads configuration. + - API endpoint `GET /` now deprecated, use `GET /1/summary` instead. + - Added `--api-no-ipv6` and similar config option to disable IPv6 support for HTTP API. + - Added `--api-no-restricted` to enable full access to api, this option has no effect if `--api-access-token` not specified. + +# v2.5.2 +- [#448](https://github.com/xmrig/xmrig/issues/478) Fixed broken reconnect. + +# v2.5.1 +- [#454](https://github.com/xmrig/xmrig/issues/454) Fixed build with libmicrohttpd version below v0.9.35. +- [#456](https://github.com/xmrig/xmrig/issues/459) Verbose errors related to donation pool was not fully silenced. +- [#459](https://github.com/xmrig/xmrig/issues/459) Fixed regression (version 2.5.0 affected) with connection to **xmr.f2pool.com**. + +# v2.5.0 +- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on April 6.** +- Added full IPv6 support. +- Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. +- [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. +- [#428](https://github.com/xmrig/xmrig/issues/428) Fixed regression (version 2.4.5 affected) with CPU cache size detection. + +# v2.4.5 +- [#324](https://github.com/xmrig/xmrig/pull/324) Fixed build without libmicrohttpd (CMake cache issue). +- [#341](https://github.com/xmrig/xmrig/issues/341) Fixed wrong exit code and added command line option `--dry-run`. +- [#385](https://github.com/xmrig/xmrig/pull/385) Up to 20% performance increase for non-AES CPU and fixed Intel Core 2 cache detection. + +# v2.4.4 + - Added libmicrohttpd version to --version output. + - Fixed bug in singal handler, in some cases miner wasn't shutdown properly. + - Fixed recent MSVC 2017 version detection. + - [#279](https://github.com/xmrig/xmrig/pull/279) Fixed build on some macOS versions. + +# v2.4.3 + - [#94](https://github.com/xmrig/xmrig/issues/94#issuecomment-342019257) [#216](https://github.com/xmrig/xmrig/issues/216) Added **ARMv8** and **ARMv7** support. Hardware AES supported, thanks [Imran Yusuff](https://github.com/imranyusuff). + - [#157](https://github.com/xmrig/xmrig/issues/157) [#196](https://github.com/xmrig/xmrig/issues/196) Fixed Linux compile issues. + - [#184](https://github.com/xmrig/xmrig/issues/184) Fixed cache size detection for CPUs with disabled Hyper-Threading. + - [#200](https://github.com/xmrig/xmrig/issues/200) In some cases miner was doesn't write log to stdout. + +# v2.4.2 + - [#60](https://github.com/xmrig/xmrig/issues/60) Added FreeBSD support, thanks [vcambur](https://github.com/vcambur). + - [#153](https://github.com/xmrig/xmrig/issues/153) Fixed issues with dwarfpool.com. + +# v2.4.1 + - [#147](https://github.com/xmrig/xmrig/issues/147) Fixed comparability with monero-stratum. + +# v2.4.0 + - Added [HTTP API](https://github.com/xmrig/xmrig/wiki/API). + - Added comments support in config file. + - libjansson replaced to rapidjson. + - [#98](https://github.com/xmrig/xmrig/issues/98) Ignore `keepalive` option with minergate.com and nicehash.com. + - [#101](https://github.com/xmrig/xmrig/issues/101) Fixed MSVC 2017 (15.3) compile time version detection. + - [#108](https://github.com/xmrig/xmrig/issues/108) Silently ignore invalid values for `donate-level` option. + - [#111](https://github.com/xmrig/xmrig/issues/111) Fixed build without AEON support. + +# v2.3.1 +- [#68](https://github.com/xmrig/xmrig/issues/68) Fixed compatibility with Docker containers, was nothing print on console. + +# v2.3.0 +- Added `--cpu-priority` option (0 idle, 2 normal to 5 highest). +- Added `--user-agent` option, to set custom user-agent string for pool. For example `cpuminer-multi/0.1`. +- Added `--no-huge-pages` option, to disable huge pages support. +- [#62](https://github.com/xmrig/xmrig/issues/62) Don't send the login to the dev pool. +- Force reconnect if pool block miner IP address. helps switch to backup pool. +- Fixed: failed open default config file if path contains non English characters. +- Fixed: error occurred if try use unavailable stdin or stdout, regression since version 2.2.0. +- Fixed: message about huge pages support successfully enabled on Windows was not shown in release builds. + +# v2.2.1 +- Fixed [terminal issues](https://github.com/xmrig/xmrig-proxy/issues/2#issuecomment-319914085) after exit on Linux and OS X. + +# v2.2.0 +- [#46](https://github.com/xmrig/xmrig/issues/46) Restored config file support. Now possible use multiple config files and combine with command line options also added support for default config. +- Improved colors support on Windows, now used uv_tty, legacy code removed. +- QuickEdit Mode now disabled on Windows. +- Added interactive commands in console window:: **h**ashrate, **p**ause, **r**esume. +- Fixed autoconf mode for AMD FX CPUs. + +# v2.1.0 +- [#40](https://github.com/xmrig/xmrig/issues/40) +Improved miner shutdown, fixed crash on exit for Linux and OS X. +- Fixed, login request was contain malformed JSON if username or password has some special characters for example `\`. +- [#220](https://github.com/fireice-uk/xmr-stak-cpu/pull/220) Better support for Round Robin DNS, IP address now always chosen randomly instead of stuck on first one. +- Changed donation address, new [xmrig-proxy](https://github.com/xmrig/xmrig-proxy) is coming soon. + +# v2.0.2 +- Better deal with possible duplicate jobs from pool, show warning and ignore duplicates. +- For Windows builds libuv updated to version 1.13.1 and gcc to 7.1.0. + +# v2.0.1 + - [#27](https://github.com/xmrig/xmrig/issues/27) Fixed possibility crash on 32bit systems. + +# v2.0.0 + - Option `--backup-url` removed, instead now possibility specify multiple pools for example: `-o example1.com:3333 -u user1 -p password1 -k -o example2.com:5555 -u user2 -o example3.com:4444 -u user3` + - [#15](https://github.com/xmrig/xmrig/issues/15) Added option `-l, --log-file=FILE` to write log to file. + - [#15](https://github.com/xmrig/xmrig/issues/15) Added option `-S, --syslog` to use syslog for logging, Linux only. + - [#18](https://github.com/xmrig/xmrig/issues/18) Added nice messages for accepted/rejected shares with diff and network latency. + - [#20](https://github.com/xmrig/xmrig/issues/20) Fixed `--cpu-affinity` for more than 32 threads. + - Fixed Windows XP support. + - Fixed regression, option `--no-color` was not fully disable colored output. + - Show resolved pool IP address in miner output. + +# v1.0.1 +- Fix broken software AES implementation, app has crashed if CPU not support AES-NI, only version 1.0.0 affected. + +# v1.0.0 +- Miner complete rewritten in C++ with libuv. +- This version should be fully compatible (except config file) with previos versions, many new nice features will come in next versions. +- This is still beta. If you found regression, stability or perfomance issues or have an idea for new feature please fell free to open new [issue](https://github.com/xmrig/xmrig/issues/new). +- Added new option `--print-time=N`, print hashrate report every N seconds. +- New hashrate reports, by default every 60 secons. +- Added Microsoft Visual C++ 2015 and 2017 support. +- Removed dependency on libcurl. +- To compile this version from source please switch to [dev](https://github.com/xmrig/xmrig/tree/dev) branch. + +# v0.8.2 +- Fixed L2 cache size detection for AMD CPUs (Bulldozer/Piledriver/Steamroller/Excavator architecture). + +# v0.8.2 +- Fixed L2 cache size detection for AMD CPUs (Bulldozer/Piledriver/Steamroller/Excavator architecture). +- Fixed gcc 7.1 support. + +# v0.8.1 +- Added nicehash support, detects automaticaly by pool URL, for example `cryptonight.eu.nicehash.com:3355` or manually via option `--nicehash`. + +# v0.8.0 +- Added double hash mode, also known as lower power mode. `--av=2` and `--av=4`. +- Added smart automatic CPU configuration. Default threads count now depends on size of the L3 cache of CPU. +- Added CryptoNight-Lite support for AEON `-a cryptonight-lite`. +- Added `--max-cpu-usage` option for auto CPU configuration mode. +- Added `--safe` option for adjust threads and algorithm variations to current CPU. +- No more manual steps to enable huge pages on Windows. XMRig will do it automatically. +- Removed BMI2 algorithm variation. +- Removed default pool URL. + +# v0.6.0 +- Added automatic cryptonight self test. +- New software AES algorithm variation. Will be automatically selected if cpu not support AES-NI. +- Added 32 bit builds. +- Documented [algorithm variations](https://github.com/xmrig/xmrig#algorithm-variations). + +# v0.5.0 +- Initial public release. From 0bfd409bdf5bf5dc401b0e26391c501867b17ada Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 10 Apr 2018 23:22:49 +0700 Subject: [PATCH 188/389] Remove unused class UploadCtx. --- src/api/Httpd.cpp | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 45c9ce07..e0901b77 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -33,37 +33,6 @@ #include "log/Log.h" -class UploadCtx -{ -public: - inline UploadCtx() : - m_pos(0) - {} - - - inline bool write(const char *data, size_t size) - { - if (size > (sizeof(m_data) - m_pos - 1)) { - return false; - } - - memcpy(m_data + m_pos, data, size); - - m_pos += size; - m_data[m_pos] = '\0'; - - return true; - } - - - inline const char *data() const { return m_data; } - -private: - char m_data[32768]; - size_t m_pos; -}; - - Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) : m_idle(true), m_IPv6(IPv6), From ad7545d1496daa7421144c123304ab22d0b52799 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 11 Apr 2018 03:52:23 +0700 Subject: [PATCH 189/389] Refactoring. --- CMakeLists.txt | 1 + src/core/CommonConfig.cpp | 30 ++----- src/core/CommonConfig.h | 23 +++--- src/core/ConfigWatcher.cpp | 12 ++- src/core/ConfigWatcher.h | 4 +- src/core/utils/c_str.h | 94 ++++++++++++++++++++++ src/net/Client.cpp | 2 +- src/net/Client.h | 1 + src/net/Url.cpp | 160 ++++++++++--------------------------- src/net/Url.h | 60 +++++++------- 10 files changed, 197 insertions(+), 190 deletions(-) create mode 100644 src/core/utils/c_str.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 43a7c1d4..734913e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ set(HEADERS src/core/ConfigLoader_platform.h src/core/ConfigWatcher.cpp src/core/Controller.h + src/core/utils/c_str.h src/Cpu.h src/interfaces/IClientListener.h src/interfaces/IConfig.h diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index 5b8a32ab..78eea5d9 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -73,11 +73,6 @@ xmrig::CommonConfig::CommonConfig() : m_watch(false), // TODO: enable config file watch by default when this feature propertly handled and tested. # endif - m_apiToken(nullptr), - m_apiWorkerId(nullptr), - m_fileName(nullptr), - m_logFile(nullptr), - m_userAgent(nullptr), m_apiPort(0), m_donateLevel(kDefaultDonateLevel), m_printTime(60), @@ -100,12 +95,6 @@ xmrig::CommonConfig::~CommonConfig() } m_pools.clear(); - - free(m_fileName); - free(m_apiToken); - free(m_apiWorkerId); - free(m_logFile); - free(m_userAgent); } @@ -223,23 +212,19 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) break; case LogFileKey: /* --log-file */ - free(m_logFile); - m_logFile = strdup(arg); + m_logFile = arg; break; case ApiAccessTokenKey: /* --api-access-token */ - free(m_apiToken); - m_apiToken = strdup(arg); + m_apiToken = arg; break; case ApiWorkerIdKey: /* --api-worker-id */ - free(m_apiWorkerId); - m_apiWorkerId = strdup(arg); + m_apiWorkerId = arg; break; case UserAgentKey: /* --user-agent */ - free(m_userAgent); - m_userAgent = strdup(arg); + m_userAgent = arg; break; case RetriesKey: /* --retries */ @@ -286,12 +271,12 @@ bool xmrig::CommonConfig::parseUint64(int key, uint64_t arg) bool xmrig::CommonConfig::save() { - if (!m_fileName) { + if (m_fileName.isNull()) { return false; } uv_fs_t req; - const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName, O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr); + const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName.data(), O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr); if (fd < 0) { return false; } @@ -320,8 +305,7 @@ bool xmrig::CommonConfig::save() void xmrig::CommonConfig::setFileName(const char *fileName) { - free(m_fileName); - m_fileName = fileName ? strdup(fileName) : nullptr; + m_fileName = fileName; } diff --git a/src/core/CommonConfig.h b/src/core/CommonConfig.h index ee9c7a20..07ff72d7 100644 --- a/src/core/CommonConfig.h +++ b/src/core/CommonConfig.h @@ -28,6 +28,7 @@ #include +#include "core/utils/c_str.h" #include "interfaces/IConfig.h" #include "xmrig.h" @@ -53,10 +54,10 @@ public: inline bool isColors() const { return m_colors; } inline bool isSyslog() const { return m_syslog; } inline const char *algoName() const { return algoName(m_algorithm); } - inline const char *apiToken() const { return m_apiToken; } - inline const char *apiWorkerId() const { return m_apiWorkerId; } - inline const char *logFile() const { return m_logFile; } - inline const char *userAgent() const { return m_userAgent; } + inline const char *apiToken() const { return m_apiToken.data(); } + inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } + inline const char *logFile() const { return m_logFile.data(); } + inline const char *userAgent() const { return m_userAgent.data(); } inline const std::vector &pools() const { return m_pools; } inline int apiPort() const { return m_apiPort; } inline int donateLevel() const { return m_donateLevel; } @@ -65,8 +66,8 @@ public: inline int retryPause() const { return m_retryPause; } inline void setColors(bool colors) { m_colors = colors; } - inline bool isWatch() const override { return m_watch && m_fileName; } - inline const char *fileName() const override { return m_fileName; } + inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); } + inline const char *fileName() const override { return m_fileName.data(); } protected: bool adjust() override; @@ -85,17 +86,17 @@ protected: bool m_colors; bool m_syslog; bool m_watch; - char *m_apiToken; - char *m_apiWorkerId; - char *m_fileName; - char *m_logFile; - char *m_userAgent; int m_apiPort; int m_donateLevel; int m_printTime; int m_retries; int m_retryPause; std::vector m_pools; + xmrig::c_str m_apiToken; + xmrig::c_str m_apiWorkerId; + xmrig::c_str m_fileName; + xmrig::c_str m_logFile; + xmrig::c_str m_userAgent; private: bool parseInt(int key, int arg); diff --git a/src/core/ConfigWatcher.cpp b/src/core/ConfigWatcher.cpp index b934b52b..092a0f3a 100644 --- a/src/core/ConfigWatcher.cpp +++ b/src/core/ConfigWatcher.cpp @@ -33,9 +33,9 @@ xmrig::ConfigWatcher::ConfigWatcher(const char *path, IConfigCreator *creator, IWatcherListener *listener) : - m_path(strdup(path)), m_creator(creator), - m_listener(listener) + m_listener(listener), + m_path(path) { uv_fs_event_init(uv_default_loop(), &m_fsEvent); uv_timer_init(uv_default_loop(), &m_timer); @@ -50,8 +50,6 @@ xmrig::ConfigWatcher::~ConfigWatcher() { uv_timer_stop(&m_timer); uv_fs_event_stop(&m_fsEvent); - - free(m_path); } @@ -80,10 +78,10 @@ void xmrig::ConfigWatcher::queueUpdate() void xmrig::ConfigWatcher::reload() { - LOG_WARN("\"%s\" was changed, reloading configuration", m_path); + LOG_WARN("\"%s\" was changed, reloading configuration", m_path.data()); IConfig *config = m_creator->create(); - ConfigLoader::loadFromFile(config, m_path); + ConfigLoader::loadFromFile(config, m_path.data()); if (!config->isValid()) { LOG_ERR("reloading failed"); @@ -103,5 +101,5 @@ void xmrig::ConfigWatcher::reload() void xmrig::ConfigWatcher::start() { - uv_fs_event_start(&m_fsEvent, xmrig::ConfigWatcher::onFsEvent, m_path, 0); + uv_fs_event_start(&m_fsEvent, xmrig::ConfigWatcher::onFsEvent, m_path.data(), 0); } diff --git a/src/core/ConfigWatcher.h b/src/core/ConfigWatcher.h index 74526e90..a5d25864 100644 --- a/src/core/ConfigWatcher.h +++ b/src/core/ConfigWatcher.h @@ -28,6 +28,8 @@ #include #include + +#include "core/utils/c_str.h" #include "rapidjson/fwd.h" @@ -56,11 +58,11 @@ private: void reload(); void start(); - char *m_path; IConfigCreator *m_creator; IWatcherListener *m_listener; uv_fs_event_t m_fsEvent; uv_timer_t m_timer; + xmrig::c_str m_path; }; diff --git a/src/core/utils/c_str.h b/src/core/utils/c_str.h new file mode 100644 index 00000000..393c349b --- /dev/null +++ b/src/core/utils/c_str.h @@ -0,0 +1,94 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __C_STR_H__ +#define __C_STR_H__ + + +#include +#include + +#include + + +namespace xmrig { + + +/** + * @brief Simple C string wrapper. + * + * 1. I know about std:string. + * 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds. + */ +class c_str +{ +public: + inline c_str() : m_data(nullptr) {} + inline c_str(const char *str) : m_data(nullptr) { set(str); } + inline ~c_str() { free(m_data); } + + + inline void set(const char *str) + { + free(m_data); + + m_data = str != nullptr ? strdup(str) : nullptr; + } + + + inline void set(char *str) + { + free(m_data); + + m_data = str; + } + + + inline bool isEqual(const char *str) const + { + return (m_data != nullptr && str != nullptr && strcmp(m_data, str)) || (m_data == nullptr && m_data == nullptr); + } + + + inline bool isNull() const { return m_data == nullptr; } + inline const char *data() const { return m_data; } + inline size_t size() const { return m_data == nullptr ? 0 : strlen(m_data); } + + + inline bool operator!=(const c_str &str) const { return !isEqual(str.data()); } + inline bool operator!=(const char *str) const { return !isEqual(str); } + inline bool operator==(const c_str &str) const { return isEqual(str.data()); } + inline bool operator==(const char *str) const { return isEqual(str); } + inline c_str &operator=(char *str) { set(str); return *this; } + inline c_str &operator=(const c_str &str) { set(str.data()); return *this; } + inline c_str &operator=(const char *str) { set(str); return *this; } + + +private: + char *m_data; +}; + + +} /* namespace xmrig */ + +#endif /* __C_STR_H__ */ diff --git a/src/net/Client.cpp b/src/net/Client.cpp index ec9dc863..bf64408a 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -138,7 +138,7 @@ void Client::setUrl(const Url *url) return; } - m_url = url; + m_url = *url; } diff --git a/src/net/Client.h b/src/net/Client.h index fc0335c6..e6af6503 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -30,6 +30,7 @@ #include +#include "core/utils/c_str.h" #include "net/Id.h" #include "net/Job.h" #include "net/Storage.h" diff --git a/src/net/Url.cpp b/src/net/Url.cpp index 3d5f2cdd..c2094d15 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -22,13 +22,13 @@ */ +#include #include #include #include #include "net/Url.h" -#include "xmrig.h" #ifdef _MSC_VER @@ -38,14 +38,10 @@ Url::Url() : m_nicehash(false), - m_host(nullptr), - m_password(nullptr), - m_user(nullptr), - m_algo(xmrig::CRYPTONIGHT), m_keepAlive(0), - m_variant(xmrig::VARIANT_AUTO), - m_url(nullptr), - m_port(kDefaultPort) + m_port(kDefaultPort), + m_algo(xmrig::CRYPTONIGHT), + m_variant(xmrig::VARIANT_AUTO) { } @@ -63,47 +59,40 @@ Url::Url() : */ Url::Url(const char *url) : m_nicehash(false), - m_host(nullptr), - m_password(nullptr), - m_user(nullptr), - m_algo(xmrig::CRYPTONIGHT), m_keepAlive(0), - m_variant(xmrig::VARIANT_AUTO), - m_url(nullptr), - m_port(kDefaultPort) + m_port(kDefaultPort), + m_algo(xmrig::CRYPTONIGHT), + m_url(url), + m_variant(xmrig::VARIANT_AUTO) { parse(url); } -Url::Url(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, int variant) : +Url::Url(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, xmrig::Variant variant) : m_nicehash(nicehash), - m_password(password ? strdup(password) : nullptr), - m_user(user ? strdup(user) : nullptr), - m_algo(xmrig::CRYPTONIGHT), m_keepAlive(keepAlive), - m_variant(variant), - m_url(nullptr), - m_port(port) + m_port(port), + m_algo(xmrig::CRYPTONIGHT), + m_host(host), + m_password(password), + m_user(user), + m_variant(variant) { - m_host = strdup(host); -} + const size_t size = m_host.size() + 8; + assert(size > 8); + char *url = new char[size](); + snprintf(url, size - 1, "%s:%d", m_host.data(), m_port); -Url::~Url() -{ - free(m_host); - free(m_password); - free(m_user); - - if (m_url) { - delete [] m_url; - } + m_url = url; } bool Url::parse(const char *url) { + assert(url != nullptr); + const char *p = strstr(url, "://"); const char *base = url; @@ -130,10 +119,12 @@ bool Url::parse(const char *url) } const size_t size = port++ - base + 1; - m_host = new char[size](); - memcpy(m_host, base, size - 1); + char *host = new char[size](); + memcpy(host, base, size - 1); + + m_host = host; + m_port = static_cast(strtol(port, nullptr, 10)); - m_port = (uint16_t) strtol(port, nullptr, 10); return true; } @@ -145,31 +136,17 @@ bool Url::setUserpass(const char *userpass) return false; } - free(m_user); - free(m_password); + char *user = new char[p - userpass + 1](); + strncpy(user, userpass, p - userpass); - m_user = static_cast(calloc(p - userpass + 1, 1)); - strncpy(m_user, userpass, p - userpass); - m_password = strdup(p + 1); + m_user = user; + m_password = p + 1; return true; } -const char *Url::url() const -{ - if (!m_url) { - const size_t size = strlen(m_host) + 8; - m_url = new char[size]; - - snprintf(m_url, size - 1, "%s:%d", m_host, m_port); - } - - return m_url; -} - - -void Url::adjust(int algo) +void Url::adjust(xmrig::Algo algo) { if (!isValid()) { return; @@ -177,91 +154,33 @@ void Url::adjust(int algo) m_algo = algo; - if (strstr(m_host, ".nicehash.com")) { + if (strstr(m_host.data(), ".nicehash.com")) { m_keepAlive = false; m_nicehash = true; } - if (strstr(m_host, ".minergate.com")) { + if (strstr(m_host.data(), ".minergate.com")) { m_keepAlive = false; } } -void Url::setPassword(const char *password) -{ - if (!password) { - return; - } - - free(m_password); - m_password = strdup(password); -} - - -void Url::setUser(const char *user) -{ - if (!user) { - return; - } - - free(m_user); - m_user = strdup(user); -} - - void Url::setVariant(int variant) { switch (variant) { case xmrig::VARIANT_AUTO: case xmrig::VARIANT_NONE: case xmrig::VARIANT_V1: - m_variant = variant; + m_variant = static_cast(variant); break; default: + assert(false); break; } } -bool Url::operator==(const Url &other) const -{ - if (m_port != other.m_port || m_keepAlive != other.m_keepAlive || m_nicehash != other.m_nicehash) { - return false; - } - - if (strcmp(host(), other.host()) != 0 || strcmp(user(), other.user()) != 0 || strcmp(password(), other.password()) != 0) { - return false; - } - - return true; -} - - -Url &Url::operator=(const Url *other) -{ - m_keepAlive = other->m_keepAlive; - m_algo = other->m_algo; - m_variant = other->m_variant; - m_nicehash = other->m_nicehash; - m_port = other->m_port; - - free(m_host); - m_host = strdup(other->m_host); - - setPassword(other->m_password); - setUser(other->m_user); - - if (m_url) { - delete [] m_url; - m_url = nullptr; - } - - return *this; -} - - bool Url::parseIPv6(const char *addr) { const char *end = strchr(addr, ']'); @@ -275,10 +194,11 @@ bool Url::parseIPv6(const char *addr) } const size_t size = end - addr; - m_host = new char[size](); - memcpy(m_host, addr + 1, size - 1); + char *host = new char[size](); + memcpy(host, addr + 1, size - 1); - m_port = (uint16_t) strtol(port + 1, nullptr, 10); + m_host = host; + m_port = static_cast(strtol(port + 1, nullptr, 10)); return true; } diff --git a/src/net/Url.h b/src/net/Url.h index 4c2c9435..7a461132 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -28,6 +28,10 @@ #include +#include "core/utils/c_str.h" +#include "xmrig.h" + + class Url { public: @@ -38,45 +42,47 @@ public: Url(); Url(const char *url); - Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, int keepAlive = 0, bool nicehash = false, int variant = -1); - ~Url(); + Url(const char *host, + uint16_t port, + const char *user = nullptr, + const char *password = nullptr, + int keepAlive = 0, + bool nicehash = false, + xmrig::Variant variant = xmrig::VARIANT_AUTO + ); - inline bool isNicehash() const { return m_nicehash; } - inline bool isValid() const { return m_host && m_port > 0; } - inline const char *host() const { return m_host; } - inline const char *password() const { return m_password ? m_password : kDefaultPassword; } - inline const char *user() const { return m_user ? m_user : kDefaultUser; } - inline int algo() const { return m_algo; } - inline int keepAlive() const { return m_keepAlive; } - inline int variant() const { return m_variant; } - inline uint16_t port() const { return m_port; } - inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } - inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } - inline void setVariant(bool monero) { m_variant = monero; } + inline bool isNicehash() const { return m_nicehash; } + inline bool isValid() const { return !m_host.isNull() && m_port > 0; } + inline const char *host() const { return m_host.data(); } + inline const char *password() const { return !m_password.isNull() ? m_password.data() : kDefaultPassword; } + inline const char *url() const { return m_url.data(); } + inline const char *user() const { return !m_user.isNull() ? m_user.data() : kDefaultUser; } + inline int keepAlive() const { return m_keepAlive; } + inline uint16_t port() const { return m_port; } + inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } + inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } + inline void setPassword(const char *password) { m_password = password; } + inline void setUser(const char *user) { m_user = user; } + inline xmrig::Algo algo() const { return m_algo; } + inline xmrig::Variant variant() const { return m_variant; } bool parse(const char *url); bool setUserpass(const char *userpass); - const char *url() const; - void adjust(int algo); - void setPassword(const char *password); - void setUser(const char *user); + void adjust(xmrig::Algo algo); void setVariant(int variant); - bool operator==(const Url &other) const; - Url &operator=(const Url *other); - private: bool parseIPv6(const char *addr); bool m_nicehash; - char *m_host; - char *m_password; - char *m_user; - int m_algo; int m_keepAlive; - int m_variant; - mutable char *m_url; uint16_t m_port; + xmrig::Algo m_algo; + xmrig::c_str m_host; + xmrig::c_str m_password; + xmrig::c_str m_url; + xmrig::c_str m_user; + xmrig::Variant m_variant; }; #endif /* __URL_H__ */ From 36ef254c730b33bb166715248f2efd9cfa42adcc Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 11 Apr 2018 06:09:34 +0700 Subject: [PATCH 190/389] Rename class Url to Pool. --- CMakeLists.txt | 4 +- src/Summary.cpp | 10 ++-- src/core/CommonConfig.cpp | 45 ++++++++---------- src/core/CommonConfig.h | 8 ++-- src/core/Config.cpp | 20 ++++---- src/core/ConfigLoader.cpp | 2 +- src/core/utils/c_str.h | 8 ++-- src/net/Client.cpp | 58 +++++++++++------------ src/net/Client.h | 13 +++-- src/net/Network.cpp | 5 +- src/net/{Url.cpp => Pool.cpp} | 34 +++++++++---- src/net/{Url.h => Pool.h} | 19 +++++--- src/net/strategies/DonateStrategy.cpp | 14 +++--- src/net/strategies/DonateStrategy.h | 3 +- src/net/strategies/FailoverStrategy.cpp | 8 ++-- src/net/strategies/FailoverStrategy.h | 5 +- src/net/strategies/SinglePoolStrategy.cpp | 4 +- src/net/strategies/SinglePoolStrategy.h | 2 +- 18 files changed, 136 insertions(+), 126 deletions(-) rename src/net/{Url.cpp => Pool.cpp} (82%) rename src/net/{Url.h => Pool.h} (90%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 734913e8..5502405c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,12 +45,12 @@ set(HEADERS src/net/Job.h src/net/JobResult.h src/net/Network.h + src/net/Pool.h src/net/Storage.h src/net/strategies/DonateStrategy.h src/net/strategies/FailoverStrategy.h src/net/strategies/SinglePoolStrategy.h src/net/SubmitResult.h - src/net/Url.h src/Platform.h src/Summary.h src/version.h @@ -102,11 +102,11 @@ set(SOURCES src/net/Client.cpp src/net/Job.cpp src/net/Network.cpp + src/net/Pool.cpp src/net/strategies/DonateStrategy.cpp src/net/strategies/FailoverStrategy.cpp src/net/strategies/SinglePoolStrategy.cpp src/net/SubmitResult.cpp - src/net/Url.cpp src/Platform.cpp src/Summary.cpp src/workers/CpuThread.cpp diff --git a/src/Summary.cpp b/src/Summary.cpp index 56f3f522..6de1cd3a 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -32,7 +32,7 @@ #include "Cpu.h" #include "log/Log.h" #include "Mem.h" -#include "net/Url.h" +#include "net/Pool.h" #include "Summary.h" #include "version.h" @@ -112,13 +112,13 @@ static void print_threads(xmrig::Config *config) static void print_pools(xmrig::Config *config) { - const std::vector &pools = config->pools(); + const std::vector &pools = config->pools(); for (size_t i = 0; i < pools.size(); ++i) { - Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mPOOL #%d: \x1B[01;36m%s:%d" : " * POOL #%d: %s:%d", + Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mPOOL #%d: \x1B[01;36m%s" : " * POOL #%d: %s", i + 1, - pools[i]->host(), - pools[i]->port()); + pools[i].url() + ); } # ifdef APP_DEBUG diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index 78eea5d9..b8af451b 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -32,7 +32,7 @@ #include "core/CommonConfig.h" #include "donate.h" #include "log/Log.h" -#include "net/Url.h" +#include "net/Pool.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" @@ -79,7 +79,7 @@ xmrig::CommonConfig::CommonConfig() : m_retries(5), m_retryPause(5) { - m_pools.push_back(new Url()); + m_pools.push_back(Pool()); # ifdef XMRIG_PROXY_PROJECT m_retries = 2; @@ -90,11 +90,6 @@ xmrig::CommonConfig::CommonConfig() : xmrig::CommonConfig::~CommonConfig() { - for (Url *url : m_pools) { - delete url; - } - - m_pools.clear(); } @@ -112,8 +107,8 @@ bool xmrig::CommonConfig::adjust() m_adjusted = true; - for (Url *url : m_pools) { - url->adjust(algorithm()); + for (Pool &pool : m_pools) { + pool.adjust(algorithm()); } return true; @@ -122,7 +117,7 @@ bool xmrig::CommonConfig::adjust() bool xmrig::CommonConfig::isValid() const { - return m_pools[0]->isValid(); + return m_pools[0].isValid(); } @@ -138,12 +133,12 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable) break; case KeepAliveKey: /* --keepalive */ - m_pools.back()->setKeepAlive(enable ? Url::kKeepAliveTimeout : 0); + m_pools.back().setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); break; # ifndef XMRIG_PROXY_PROJECT case NicehashKey: /* --nicehash */ - m_pools.back()->setNicehash(enable); + m_pools.back().setNicehash(enable); break; # endif @@ -177,38 +172,36 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) break; case UserpassKey: /* --userpass */ - if (!m_pools.back()->setUserpass(arg)) { + if (!m_pools.back().setUserpass(arg)) { return false; } break; case UrlKey: /* --url */ - if (m_pools.size() > 1 || m_pools[0]->isValid()) { - Url *url = new Url(arg); - if (url->isValid()) { - m_pools.push_back(url); - } - else { - delete url; + if (m_pools.size() > 1 || m_pools[0].isValid()) { + Pool pool(arg); + + if (pool.isValid()) { + m_pools.push_back(std::move(pool)); } } else { - m_pools[0]->parse(arg); + m_pools[0].parse(arg); } - if (!m_pools.back()->isValid()) { + if (!m_pools.back().isValid()) { return false; } break; case UserKey: /* --user */ - m_pools.back()->setUser(arg); + m_pools.back().setUser(arg); break; case PasswordKey: /* --pass */ - m_pools.back()->setPassword(arg); + m_pools.back().setPassword(arg); break; case LogFileKey: /* --log-file */ @@ -325,11 +318,11 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) break; case KeepAliveKey: /* --keepalive */ - m_pools.back()->setKeepAlive(arg); + m_pools.back().setKeepAlive(arg); break; case VariantKey: /* --variant */ - m_pools.back()->setVariant(arg); + m_pools.back().setVariant(arg); break; case DonateLevelKey: /* --donate-level */ diff --git a/src/core/CommonConfig.h b/src/core/CommonConfig.h index 07ff72d7..89dbc7ed 100644 --- a/src/core/CommonConfig.h +++ b/src/core/CommonConfig.h @@ -31,9 +31,7 @@ #include "core/utils/c_str.h" #include "interfaces/IConfig.h" #include "xmrig.h" - - -class Url; +#include "net/Pool.h" namespace xmrig { @@ -58,7 +56,7 @@ public: inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *logFile() const { return m_logFile.data(); } inline const char *userAgent() const { return m_userAgent.data(); } - inline const std::vector &pools() const { return m_pools; } + inline const std::vector &pools() const { return m_pools; } inline int apiPort() const { return m_apiPort; } inline int donateLevel() const { return m_donateLevel; } inline int printTime() const { return m_printTime; } @@ -91,7 +89,7 @@ protected: int m_printTime; int m_retries; int m_retryPause; - std::vector m_pools; + std::vector m_pools; xmrig::c_str m_apiToken; xmrig::c_str m_apiWorkerId; xmrig::c_str m_fileName; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 6bc2dc0f..a9136660 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -30,7 +30,7 @@ #include "core/ConfigCreator.h" #include "core/ConfigLoader.h" #include "Cpu.h" -#include "net/Url.h" +#include "net/Pool.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" @@ -110,22 +110,22 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const rapidjson::Value pools(rapidjson::kArrayType); - for (const Url *url : m_pools) { + for (const Pool &pool : m_pools) { rapidjson::Value obj(rapidjson::kObjectType); - obj.AddMember("url", rapidjson::StringRef(url->url()), allocator); - obj.AddMember("user", rapidjson::StringRef(url->user()), allocator); - obj.AddMember("pass", rapidjson::StringRef(url->password()), allocator); + obj.AddMember("url", rapidjson::StringRef(pool.url()), allocator); + obj.AddMember("user", rapidjson::StringRef(pool.user()), allocator); + obj.AddMember("pass", rapidjson::StringRef(pool.password()), allocator); - if (url->keepAlive() == 0 || url->keepAlive() == Url::kKeepAliveTimeout) { - obj.AddMember("keepalive", url->keepAlive() > 0, allocator); + if (pool.keepAlive() == 0 || pool.keepAlive() == Pool::kKeepAliveTimeout) { + obj.AddMember("keepalive", pool.keepAlive() > 0, allocator); } else { - obj.AddMember("keepalive", url->keepAlive(), allocator); + obj.AddMember("keepalive", pool.keepAlive(), allocator); } - obj.AddMember("nicehash", url->isNicehash(), allocator); - obj.AddMember("variant", url->variant(), allocator); + obj.AddMember("nicehash", pool.isNicehash(), allocator); + obj.AddMember("variant", pool.variant(), allocator); pools.PushBack(obj, allocator); } diff --git a/src/core/ConfigLoader.cpp b/src/core/ConfigLoader.cpp index 1dec4eca..8c7f6546 100644 --- a/src/core/ConfigLoader.cpp +++ b/src/core/ConfigLoader.cpp @@ -38,7 +38,7 @@ #include "core/ConfigWatcher.h" #include "interfaces/IConfig.h" #include "interfaces/IWatcherListener.h" -#include "net/Url.h" +#include "net/Pool.h" #include "Platform.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" diff --git a/src/core/utils/c_str.h b/src/core/utils/c_str.h index 393c349b..9a11ade6 100644 --- a/src/core/utils/c_str.h +++ b/src/core/utils/c_str.h @@ -43,9 +43,11 @@ namespace xmrig { class c_str { public: - inline c_str() : m_data(nullptr) {} - inline c_str(const char *str) : m_data(nullptr) { set(str); } - inline ~c_str() { free(m_data); } + inline c_str() : m_data(nullptr) {} + inline c_str(c_str &&other) { m_data = other.m_data; other.m_data = nullptr; } + inline c_str(const c_str &other) : m_data(nullptr) { set(other.data()); } + inline c_str(const char *str) : m_data(nullptr) { set(str); } + inline ~c_str() { free(m_data); } inline void set(const char *str) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index bf64408a..5574f37f 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -32,7 +32,6 @@ #include "interfaces/IClientListener.h" #include "log/Log.h" #include "net/Client.h" -#include "net/Url.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" #include "rapidjson/stringbuffer.h" @@ -97,7 +96,7 @@ Client::~Client() void Client::connect() { - resolve(m_url.host()); + resolve(m_pool.host()); } @@ -106,10 +105,10 @@ void Client::connect() * * @param url */ -void Client::connect(const Url *url) +void Client::connect(const Pool &url) { setUrl(url); - resolve(m_url.host()); + connect(); } @@ -132,13 +131,13 @@ void Client::deleteLater() } -void Client::setUrl(const Url *url) +void Client::setUrl(const Pool &pool) { - if (!url || !url->isValid()) { + if (!pool.isValid()) { return; } - m_url = *url; + m_pool = pool; } @@ -146,7 +145,7 @@ void Client::tick(uint64_t now) { if (m_state == ConnectedState) { if (m_expire && now > m_expire) { - LOG_DEBUG_ERR("[%s:%u] timeout", m_url.host(), m_url.port()); + LOG_DEBUG_ERR("[%s] timeout", m_pool.url()); close(); } else if (m_keepAlive && now > m_keepAlive) { @@ -266,11 +265,10 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) } # ifdef XMRIG_PROXY_PROJECT - Job job(m_id, m_url.variant()); + Job job(m_id, m_pool.variant()); job.setClientId(m_rpcId); - job.setCoin(m_url.coin()); # else - Job job(m_id, m_nicehash, m_url.algo(), m_url.variant()); + Job job(m_id, m_nicehash, m_pool.algo(), m_pool.variant()); # endif if (!job.setId(params["job_id"].GetString())) { @@ -307,7 +305,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) } if (!m_quiet) { - LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); + LOG_WARN("[%s] duplicate job received, reconnect", m_pool.url()); } close(); @@ -323,7 +321,7 @@ bool Client::parseLogin(const rapidjson::Value &result, int *code) } # ifndef XMRIG_PROXY_PROJECT - m_nicehash = m_url.isNicehash(); + m_nicehash = m_pool.isNicehash(); # endif if (result.HasMember("extensions")) { @@ -351,7 +349,7 @@ int Client::resolve(const char *host) const int r = uv_getaddrinfo(uv_default_loop(), &m_resolver, Client::onResolved, host, nullptr, &m_hints); if (r) { if (!m_quiet) { - LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_url.port(), uv_strerror(r)); + LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_pool.port(), uv_strerror(r)); } return 1; } @@ -402,7 +400,7 @@ void Client::connect(sockaddr *addr) { setState(ConnectingState); - reinterpret_cast(addr)->sin_port = htons(m_url.port()); + reinterpret_cast(addr)->sin_port = htons(m_pool.port()); delete m_socket; uv_connect_t *req = new uv_connect_t; @@ -436,9 +434,9 @@ void Client::login() doc.AddMember("method", "login", allocator); rapidjson::Value params(rapidjson::kObjectType); - params.AddMember("login", rapidjson::StringRef(m_url.user()), allocator); - params.AddMember("pass", rapidjson::StringRef(m_url.password()), allocator); - params.AddMember("agent", rapidjson::StringRef(m_agent), allocator); + params.AddMember("login", rapidjson::StringRef(m_pool.user()), allocator); + params.AddMember("pass", rapidjson::StringRef(m_pool.password()), allocator); + params.AddMember("agent", rapidjson::StringRef(m_agent), allocator); doc.AddMember("params", params, allocator); @@ -481,7 +479,7 @@ void Client::parse(char *line, size_t len) if (len < 32 || line[0] != '{') { if (!m_quiet) { - LOG_ERR("[%s:%u] JSON decode failed", m_url.host(), m_url.port()); + LOG_ERR("[%s] JSON decode failed", m_pool.url()); } return; @@ -490,7 +488,7 @@ void Client::parse(char *line, size_t len) rapidjson::Document doc; if (doc.ParseInsitu(line).HasParseError()) { if (!m_quiet) { - LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_url.host(), m_url.port(), rapidjson::GetParseError_En(doc.GetParseError())); + LOG_ERR("[%s] JSON decode failed: \"%s\"", m_pool.url(), rapidjson::GetParseError_En(doc.GetParseError())); } return; @@ -532,7 +530,7 @@ void Client::parseNotification(const char *method, const rapidjson::Value ¶m { if (error.IsObject()) { if (!m_quiet) { - LOG_ERR("[%s:%u] error: \"%s\", code: %d", m_url.host(), m_url.port(), error["message"].GetString(), error["code"].GetInt()); + LOG_ERR("[%s] error: \"%s\", code: %d", m_pool.url(), error["message"].GetString(), error["code"].GetInt()); } return; } @@ -550,7 +548,7 @@ void Client::parseNotification(const char *method, const rapidjson::Value ¶m return; } - LOG_WARN("[%s:%u] unsupported method: \"%s\"", m_url.host(), m_url.port(), method); + LOG_WARN("[%s] unsupported method: \"%s\"", m_pool.url(), method); } @@ -566,7 +564,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap m_results.erase(it); } else if (!m_quiet) { - LOG_ERR("[%s:%u] error: \"%s\", code: %d", m_url.host(), m_url.port(), message, error["code"].GetInt()); + LOG_ERR("[%s] error: \"%s\", code: %d", m_pool.url(), message, error["code"].GetInt()); } if (isCriticalError(message)) { @@ -584,7 +582,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap int code = -1; if (!parseLogin(result, &code)) { if (!m_quiet) { - LOG_ERR("[%s:%u] login error code: %d", m_url.host(), m_url.port(), code); + LOG_ERR("[%s] login error code: %d", m_pool.url(), code); } close(); @@ -650,8 +648,8 @@ void Client::startTimeout() { m_expire = 0; - if (m_url.keepAlive()) { - m_keepAlive = uv_now(uv_default_loop()) + (m_url.keepAlive() * 1000); + if (m_pool.keepAlive()) { + m_keepAlive = uv_now(uv_default_loop()) + (m_pool.keepAlive() * 1000); } } @@ -689,7 +687,7 @@ void Client::onConnect(uv_connect_t *req, int status) if (status < 0) { if (!client->m_quiet) { - LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); + LOG_ERR("[%s] connect error: \"%s\"", client->m_pool.url(), uv_strerror(status)); } delete req; @@ -717,7 +715,7 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) if (nread < 0) { if (nread != UV_EOF && !client->m_quiet) { - LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror((int) nread)); + LOG_ERR("[%s] read error: \"%s\"", client->m_pool.url(), uv_strerror((int) nread)); } client->close(); @@ -777,7 +775,7 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) if (status < 0) { if (!client->m_quiet) { - LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); + LOG_ERR("[%s] DNS error: \"%s\"", client->m_pool.url(), uv_strerror(status)); } return client->reconnect(); @@ -801,7 +799,7 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) if (ipv4.empty() && ipv6.empty()) { if (!client->m_quiet) { - LOG_ERR("[%s:%u] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", client->m_url.host(), client->m_url.port()); + LOG_ERR("[%s] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", client->m_pool.url()); } uv_freeaddrinfo(res); diff --git a/src/net/Client.h b/src/net/Client.h index e6af6503..910c8379 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -30,12 +30,11 @@ #include -#include "core/utils/c_str.h" #include "net/Id.h" #include "net/Job.h" #include "net/Storage.h" #include "net/SubmitResult.h" -#include "net/Url.h" +#include "net/Pool.h" #include "rapidjson/fwd.h" @@ -62,18 +61,18 @@ public: bool disconnect(); int64_t submit(const JobResult &result); void connect(); - void connect(const Url *url); + void connect(const Pool &pool); void deleteLater(); - void setUrl(const Url *url); + void setUrl(const Pool &pool); void tick(uint64_t now); inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; } - inline const char *host() const { return m_url.host(); } + inline const char *host() const { return m_pool.host(); } inline const char *ip() const { return m_ip; } inline const Job &job() const { return m_job; } inline int id() const { return m_id; } inline SocketState state() const { return m_state; } - inline uint16_t port() const { return m_url.port(); } + inline uint16_t port() const { return m_pool.port(); } inline void setQuiet(bool quiet) { m_quiet = quiet; } inline void setRetryPause(int ms) { m_retryPause = ms; } @@ -118,6 +117,7 @@ private: int m_retryPause; int64_t m_failures; Job m_job; + Pool m_pool; size_t m_recvBufPos; SocketState m_state; std::map m_results; @@ -125,7 +125,6 @@ private: uint64_t m_jobs; uint64_t m_keepAlive; uintptr_t m_key; - Url m_url; uv_buf_t m_recvBuf; uv_getaddrinfo_t m_resolver; uv_stream_t *m_stream; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index a8e60efa..98ce7e8d 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -38,7 +38,6 @@ #include "net/strategies/FailoverStrategy.h" #include "net/strategies/SinglePoolStrategy.h" #include "net/SubmitResult.h" -#include "net/Url.h" #include "workers/Workers.h" #include "core/Controller.h" #include "core/Config.h" @@ -52,7 +51,7 @@ Network::Network(xmrig::Controller *controller) : Workers::setListener(this); - const std::vector &pools = controller->config()->pools(); + const std::vector &pools = controller->config()->pools(); if (pools.size() > 1) { m_strategy = new FailoverStrategy(pools, controller->config()->retryPause(), controller->config()->retries(), this); @@ -62,7 +61,7 @@ Network::Network(xmrig::Controller *controller) : } if (controller->config()->donateLevel() > 0) { - m_donate = new DonateStrategy(controller->config()->donateLevel(), controller->config()->pools().front()->user(), controller->config()->algorithm(), this); + m_donate = new DonateStrategy(controller->config()->donateLevel(), controller->config()->pools().front().user(), controller->config()->algorithm(), this); } m_timer.data = this; diff --git a/src/net/Url.cpp b/src/net/Pool.cpp similarity index 82% rename from src/net/Url.cpp rename to src/net/Pool.cpp index c2094d15..ff7d461a 100644 --- a/src/net/Url.cpp +++ b/src/net/Pool.cpp @@ -28,7 +28,7 @@ #include -#include "net/Url.h" +#include "net/Pool.h" #ifdef _MSC_VER @@ -36,7 +36,7 @@ #endif -Url::Url() : +Pool::Pool() : m_nicehash(false), m_keepAlive(0), m_port(kDefaultPort), @@ -57,7 +57,7 @@ Url::Url() : * * @param url */ -Url::Url(const char *url) : +Pool::Pool(const char *url) : m_nicehash(false), m_keepAlive(0), m_port(kDefaultPort), @@ -69,7 +69,7 @@ Url::Url(const char *url) : } -Url::Url(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, xmrig::Variant variant) : +Pool::Pool(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, xmrig::Variant variant) : m_nicehash(nicehash), m_keepAlive(keepAlive), m_port(port), @@ -89,7 +89,7 @@ Url::Url(const char *host, uint16_t port, const char *user, const char *password } -bool Url::parse(const char *url) +bool Pool::parse(const char *url) { assert(url != nullptr); @@ -114,7 +114,7 @@ bool Url::parse(const char *url) const char *port = strchr(base, ':'); if (!port) { - m_host = strdup(base); + m_host = base; return false; } @@ -129,7 +129,7 @@ bool Url::parse(const char *url) } -bool Url::setUserpass(const char *userpass) +bool Pool::setUserpass(const char *userpass) { const char *p = strchr(userpass, ':'); if (!p) { @@ -146,7 +146,7 @@ bool Url::setUserpass(const char *userpass) } -void Url::adjust(xmrig::Algo algo) +void Pool::adjust(xmrig::Algo algo) { if (!isValid()) { return; @@ -165,7 +165,7 @@ void Url::adjust(xmrig::Algo algo) } -void Url::setVariant(int variant) +void Pool::setVariant(int variant) { switch (variant) { case xmrig::VARIANT_AUTO: @@ -181,7 +181,21 @@ void Url::setVariant(int variant) } -bool Url::parseIPv6(const char *addr) +bool Pool::isEqual(const Pool &other) const +{ + return (m_nicehash == other.m_nicehash + && m_keepAlive == other.m_keepAlive + && m_port == other.m_port + && m_algo == other.m_algo + && m_host == other.m_host + && m_password == other.m_password + && m_url == other.m_url + && m_user == other.m_user + && m_variant == other.m_variant); +} + + +bool Pool::parseIPv6(const char *addr) { const char *end = strchr(addr, ']'); if (!end) { diff --git a/src/net/Url.h b/src/net/Pool.h similarity index 90% rename from src/net/Url.h rename to src/net/Pool.h index 7a461132..5a2c3529 100644 --- a/src/net/Url.h +++ b/src/net/Pool.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __URL_H__ -#define __URL_H__ +#ifndef __POOL_H__ +#define __POOL_H__ #include @@ -32,7 +32,7 @@ #include "xmrig.h" -class Url +class Pool { public: constexpr static const char *kDefaultPassword = "x"; @@ -40,9 +40,9 @@ public: constexpr static uint16_t kDefaultPort = 3333; constexpr static int kKeepAliveTimeout = 60; - Url(); - Url(const char *url); - Url(const char *host, + Pool(); + Pool(const char *url); + Pool(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, @@ -66,11 +66,16 @@ public: inline xmrig::Algo algo() const { return m_algo; } inline xmrig::Variant variant() const { return m_variant; } + inline bool operator!=(const Pool &other) const { return !isEqual(other); } + inline bool operator==(const Pool &other) const { return isEqual(other); } + bool parse(const char *url); bool setUserpass(const char *userpass); void adjust(xmrig::Algo algo); void setVariant(int variant); + bool isEqual(const Pool &other) const; + private: bool parseIPv6(const char *addr); @@ -85,4 +90,4 @@ private: xmrig::Variant m_variant; }; -#endif /* __URL_H__ */ +#endif /* __POOL_H__ */ diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index f0ec5bde..311ca424 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -60,17 +60,17 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL Job::toHex(hash, 32, userId); if (algo == xmrig::CRYPTONIGHT) { - m_pools.push_back(new Url(kDonatePool1, 6666, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool1, 80, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool2, 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); + m_pools.push_back(Pool(kDonatePool1, 6666, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool1, 80, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool2, 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); } else if (algo == xmrig::CRYPTONIGHT_HEAVY) { - m_pools.push_back(new Url(kDonatePool1, 8888, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool1, 8889, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool1, 8888, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool1, 8889, userId, nullptr, false, true)); } else { - m_pools.push_back(new Url(kDonatePool1, 5555, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool1, 7777, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool1, 5555, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool1, 7777, userId, nullptr, false, true)); } m_strategy = new FailoverStrategy(m_pools, 1, 1, this, true); diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 4ef29958..cad360b1 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -32,6 +32,7 @@ #include "interfaces/IClientListener.h" #include "interfaces/IStrategy.h" #include "interfaces/IStrategyListener.h" +#include "net/Pool.h" class Client; @@ -71,7 +72,7 @@ private: const int m_idleTime; IStrategy *m_strategy; IStrategyListener *m_listener; - std::vector m_pools; + std::vector m_pools; uv_timer_t m_timer; }; diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index dbfeb311..7a581407 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -28,7 +28,7 @@ #include "Platform.h" -FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : +FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : m_quiet(quiet), m_retries(retries), m_retryPause(retryPause), @@ -36,7 +36,7 @@ FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause m_index(0), m_listener(listener) { - for (const Url *url : urls) { + for (const Pool &url : urls) { add(url); } } @@ -153,10 +153,10 @@ void FailoverStrategy::onResultAccepted(Client *client, const SubmitResult &resu } -void FailoverStrategy::add(const Url *url) +void FailoverStrategy::add(const Pool &pool) { Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this); - client->setUrl(url); + client->setUrl(pool); client->setRetryPause(m_retryPause * 1000); client->setQuiet(m_quiet); diff --git a/src/net/strategies/FailoverStrategy.h b/src/net/strategies/FailoverStrategy.h index 8ad767ca..a48496fb 100644 --- a/src/net/strategies/FailoverStrategy.h +++ b/src/net/strategies/FailoverStrategy.h @@ -30,6 +30,7 @@ #include "interfaces/IClientListener.h" #include "interfaces/IStrategy.h" +#include "net/Pool.h" class Client; @@ -40,7 +41,7 @@ class Url; class FailoverStrategy : public IStrategy, public IClientListener { public: - FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); + FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); ~FailoverStrategy(); public: @@ -59,7 +60,7 @@ protected: void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; private: - void add(const Url *url); + void add(const Pool &pool); const bool m_quiet; const int m_retries; diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index fc7f209e..42a60b30 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -28,12 +28,12 @@ #include "Platform.h" -SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet) : +SinglePoolStrategy::SinglePoolStrategy(const Pool &pool, int retryPause, IStrategyListener *listener, bool quiet) : m_active(false), m_listener(listener) { m_client = new Client(0, Platform::userAgent(), this); - m_client->setUrl(url); + m_client->setUrl(pool); m_client->setRetryPause(retryPause * 1000); m_client->setQuiet(quiet); } diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h index d5682cf7..ce3d0f7f 100644 --- a/src/net/strategies/SinglePoolStrategy.h +++ b/src/net/strategies/SinglePoolStrategy.h @@ -37,7 +37,7 @@ class Url; class SinglePoolStrategy : public IStrategy, public IClientListener { public: - SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet = false); + SinglePoolStrategy(const Pool &pool, int retryPause, IStrategyListener *listener, bool quiet = false); ~SinglePoolStrategy(); public: From 1ebaf677e0f056400b9c0ce951abd4456f2302e3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 11 Apr 2018 06:39:24 +0700 Subject: [PATCH 191/389] Move static algo name conversions to Pool class. --- src/core/CommonConfig.cpp | 43 +-------------------------------------- src/core/CommonConfig.h | 6 ++---- src/net/Pool.cpp | 42 ++++++++++++++++++++++++++++++++++++++ src/net/Pool.h | 3 +++ src/workers/CpuThread.cpp | 2 +- 5 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index b8af451b..b1758921 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -39,25 +39,6 @@ #include "xmrig.h" -static const char *algoNames[] = { - "cryptonight", - "cryptonight-lite", - "cryptonight-heavy" -}; - - -static const char *algoNamesShort[] = { - "cn", - "cn-lite", - "cn-heavy" -}; - - -#if defined(_WIN32) && !defined(strcasecmp) -# define strcasecmp _stricmp -#endif - - xmrig::CommonConfig::CommonConfig() : m_algorithm(CRYPTONIGHT), m_adjusted(false), @@ -93,12 +74,6 @@ xmrig::CommonConfig::~CommonConfig() } -const char *xmrig::CommonConfig::algoName(Algo algorithm) -{ - return algoNames[algorithm]; -} - - bool xmrig::CommonConfig::adjust() { if (m_adjusted) { @@ -353,21 +328,5 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) void xmrig::CommonConfig::setAlgo(const char *algo) { - if (strcasecmp(algo, "cryptonight-light") == 0) { - fprintf(stderr, "Algorithm \"cryptonight-light\" is deprecated, use \"cryptonight-lite\" instead\n"); - - m_algorithm = CRYPTONIGHT_LITE; - return; - } - - const size_t size = sizeof(algoNames) / sizeof(algoNames[0]); - - assert(size == (sizeof(algoNamesShort) / sizeof(algoNamesShort[0]))); - - for (size_t i = 0; i < size; i++) { - if (strcasecmp(algo, algoNames[i]) == 0 || strcasecmp(algo, algoNamesShort[i]) == 0) { - m_algorithm = static_cast(i); - break; - } - } + m_algorithm = Pool::algorithm(algo); } diff --git a/src/core/CommonConfig.h b/src/core/CommonConfig.h index 89dbc7ed..0b72b7e1 100644 --- a/src/core/CommonConfig.h +++ b/src/core/CommonConfig.h @@ -30,8 +30,8 @@ #include "core/utils/c_str.h" #include "interfaces/IConfig.h" -#include "xmrig.h" #include "net/Pool.h" +#include "xmrig.h" namespace xmrig { @@ -43,15 +43,13 @@ public: CommonConfig(); ~CommonConfig(); - static const char *algoName(Algo algorithm); - inline Algo algorithm() const { return m_algorithm; } inline bool isApiIPv6() const { return m_apiIPv6; } inline bool isApiRestricted() const { return m_apiRestricted; } inline bool isBackground() const { return m_background; } inline bool isColors() const { return m_colors; } inline bool isSyslog() const { return m_syslog; } - inline const char *algoName() const { return algoName(m_algorithm); } + inline const char *algoName() const { return Pool::algoName(m_algorithm); } inline const char *apiToken() const { return m_apiToken.data(); } inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *logFile() const { return m_logFile.data(); } diff --git a/src/net/Pool.cpp b/src/net/Pool.cpp index ff7d461a..f6816567 100644 --- a/src/net/Pool.cpp +++ b/src/net/Pool.cpp @@ -36,6 +36,20 @@ #endif +static const char *algoNames[] = { + "cryptonight", + "cryptonight-lite", + "cryptonight-heavy" +}; + + +static const char *algoNamesShort[] = { + "cn", + "cn-lite", + "cn-heavy" +}; + + Pool::Pool() : m_nicehash(false), m_keepAlive(0), @@ -89,6 +103,34 @@ Pool::Pool(const char *host, uint16_t port, const char *user, const char *passwo } +const char *Pool::algoName(xmrig::Algo algorithm) +{ + return algoNames[algorithm]; +} + + +xmrig::Algo Pool::algorithm(const char *algo) +{ + if (strcasecmp(algo, "cryptonight-light") == 0) { + fprintf(stderr, "Algorithm \"cryptonight-light\" is deprecated, use \"cryptonight-lite\" instead\n"); + + return xmrig::CRYPTONIGHT_LITE; + } + + const size_t size = sizeof(algoNames) / sizeof(algoNames[0]); + + assert(size == (sizeof(algoNamesShort) / sizeof(algoNamesShort[0]))); + + for (size_t i = 0; i < size; i++) { + if (strcasecmp(algo, algoNames[i]) == 0 || strcasecmp(algo, algoNamesShort[i]) == 0) { + return static_cast(i); + } + } + + return xmrig::CRYPTONIGHT; +} + + bool Pool::parse(const char *url) { assert(url != nullptr); diff --git a/src/net/Pool.h b/src/net/Pool.h index 5a2c3529..efa3e3a7 100644 --- a/src/net/Pool.h +++ b/src/net/Pool.h @@ -51,6 +51,9 @@ public: xmrig::Variant variant = xmrig::VARIANT_AUTO ); + static const char *algoName(xmrig::Algo algorithm); + static xmrig::Algo algorithm(const char *algo); + inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return !m_host.isNull() && m_port > 0; } inline const char *host() const { return m_host.data(); } diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 7e68a990..482b4305 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -204,7 +204,7 @@ rapidjson::Value xmrig::CpuThread::toAPI(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); obj.AddMember("type", "cpu", allocator); - obj.AddMember("algo", rapidjson::StringRef(CommonConfig::algoName(algorithm())), allocator); + obj.AddMember("algo", rapidjson::StringRef(Pool::algoName(algorithm())), allocator); obj.AddMember("av", m_av, allocator); obj.AddMember("low_power_mode", multiway(), allocator); obj.AddMember("affine_to_cpu", affinity(), allocator); From a73ad9b08924ff4b1ccfe78ae949a6e27a5cb2e1 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 11 Apr 2018 08:29:02 +0700 Subject: [PATCH 192/389] Fixed build with APP_DEBUG. --- src/Summary.cpp | 2 +- src/net/Client.cpp | 12 ++++++------ src/net/Client.h | 2 +- src/net/Pool.cpp | 4 ++-- src/net/strategies/FailoverStrategy.cpp | 2 +- src/net/strategies/SinglePoolStrategy.cpp | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Summary.cpp b/src/Summary.cpp index 6de1cd3a..5acda5c2 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -123,7 +123,7 @@ static void print_pools(xmrig::Config *config) # ifdef APP_DEBUG for (size_t i = 0; i < pools.size(); ++i) { - Log::i()->text("%s:%d, user: %s, pass: %s, ka: %d, nicehash: %d", pools[i]->host(), pools[i]->port(), pools[i]->user(), pools[i]->password(), pools[i]->keepAlive(), pools[i]->isNicehash()); + Log::i()->text("%s:%d, user: %s, pass: %s, ka: %d, nicehash: %d", pools[i].host(), pools[i].port(), pools[i].user(), pools[i].password(), pools[i].keepAlive(), pools[i].isNicehash()); } # endif } diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 5574f37f..c4387a7c 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -107,7 +107,7 @@ void Client::connect() */ void Client::connect(const Pool &url) { - setUrl(url); + setPool(url); connect(); } @@ -131,7 +131,7 @@ void Client::deleteLater() } -void Client::setUrl(const Pool &pool) +void Client::setPool(const Pool &pool) { if (!pool.isValid()) { return; @@ -360,9 +360,9 @@ int Client::resolve(const char *host) int64_t Client::send(size_t size) { - LOG_DEBUG("[%s:%u] send (%d bytes): \"%s\"", m_url.host(), m_url.port(), size, m_sendBuf); + LOG_DEBUG("[%s] send (%d bytes): \"%s\"", m_pool.url(), size, m_sendBuf); if (state() != ConnectedState || !uv_is_writable(m_stream)) { - LOG_DEBUG_ERR("[%s:%u] send failed, invalid state: %d", m_url.host(), m_url.port(), m_state); + LOG_DEBUG_ERR("[%s] send failed, invalid state: %d", m_pool.url(), m_state); return -1; } @@ -475,7 +475,7 @@ void Client::parse(char *line, size_t len) line[len - 1] = '\0'; - LOG_DEBUG("[%s:%u] received (%d bytes): \"%s\"", m_url.host(), m_url.port(), len, line); + LOG_DEBUG("[%s] received (%d bytes): \"%s\"", m_pool.url(), len, line); if (len < 32 || line[0] != '{') { if (!m_quiet) { @@ -634,7 +634,7 @@ void Client::reconnect() void Client::setState(SocketState state) { - LOG_DEBUG("[%s:%u] state: %d", m_url.host(), m_url.port(), state); + LOG_DEBUG("[%s] state: %d", m_pool.url(), state); if (m_state == state) { return; diff --git a/src/net/Client.h b/src/net/Client.h index 910c8379..0692d38b 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -63,7 +63,7 @@ public: void connect(); void connect(const Pool &pool); void deleteLater(); - void setUrl(const Pool &pool); + void setPool(const Pool &pool); void tick(uint64_t now); inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; } diff --git a/src/net/Pool.cpp b/src/net/Pool.cpp index f6816567..b3766b08 100644 --- a/src/net/Pool.cpp +++ b/src/net/Pool.cpp @@ -76,7 +76,6 @@ Pool::Pool(const char *url) : m_keepAlive(0), m_port(kDefaultPort), m_algo(xmrig::CRYPTONIGHT), - m_url(url), m_variant(xmrig::VARIANT_AUTO) { parse(url); @@ -150,6 +149,7 @@ bool Pool::parse(const char *url) return false; } + m_url = url; if (base[0] == '[') { return parseIPv6(base); } @@ -157,7 +157,7 @@ bool Pool::parse(const char *url) const char *port = strchr(base, ':'); if (!port) { m_host = base; - return false; + return true; } const size_t size = port++ - base + 1; diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index 7a581407..a4e25277 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -156,7 +156,7 @@ void FailoverStrategy::onResultAccepted(Client *client, const SubmitResult &resu void FailoverStrategy::add(const Pool &pool) { Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this); - client->setUrl(pool); + client->setPool(pool); client->setRetryPause(m_retryPause * 1000); client->setQuiet(m_quiet); diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index 42a60b30..8c113d93 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -33,7 +33,7 @@ SinglePoolStrategy::SinglePoolStrategy(const Pool &pool, int retryPause, IStrate m_listener(listener) { m_client = new Client(0, Platform::userAgent(), this); - m_client->setUrl(pool); + m_client->setPool(pool); m_client->setRetryPause(retryPause * 1000); m_client->setQuiet(quiet); } From b13640e4a161d566db325d244c6e406d603e130d Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 12 Apr 2018 11:38:43 +0700 Subject: [PATCH 193/389] Fixes for build without cn-lite and cn-heavy. --- src/core/CommonConfig.cpp | 2 +- src/core/ConfigLoader.cpp | 8 ++++---- src/core/ConfigLoader_platform.h | 12 +++++++++++- src/crypto/CryptoNight_constants.h | 9 +++++++++ src/net/Pool.cpp | 23 +++++++++++++++++++++-- src/workers/DoubleWorker.cpp | 6 +++++- src/workers/SingleWorker.cpp | 4 ++++ src/xmrig.h | 1 + 8 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index b1758921..b444537e 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -92,7 +92,7 @@ bool xmrig::CommonConfig::adjust() bool xmrig::CommonConfig::isValid() const { - return m_pools[0].isValid(); + return m_pools[0].isValid() && m_algorithm != INVALID_ALGO; } diff --git a/src/core/ConfigLoader.cpp b/src/core/ConfigLoader.cpp index 8c7f6546..c84ba962 100644 --- a/src/core/ConfigLoader.cpp +++ b/src/core/ConfigLoader.cpp @@ -168,7 +168,7 @@ xmrig::IConfig *xmrig::ConfigLoader::load(int argc, char **argv, IConfigCreator } if (!config->isValid()) { - fprintf(stderr, "No pool URL supplied. Exiting.\n"); + fprintf(stderr, "No valid configuration found. Exiting.\n"); delete config; return nullptr; } @@ -293,13 +293,13 @@ void xmrig::ConfigLoader::showVersion() printf("\n features:" # if defined(__i386__) || defined(_M_IX86) - " i386" + " 32-bit" # elif defined(__x86_64__) || defined(_M_AMD64) - " x86_64" + " 64-bit" # endif # if defined(__AES__) || defined(_MSC_VER) - " AES-NI" + " AES" # endif "\n"); diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index 68eef639..a090fb99 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -43,7 +43,17 @@ namespace xmrig { static char const usage[] = "\ Usage: " APP_ID " [OPTIONS]\n\ Options:\n\ - -a, --algo=ALGO cryptonight (default) or cryptonight-lite\n\ + -a, --algo=ALGO specify the algorithm to use\n\ + cryptonight\n" +#ifndef XMRIG_NO_AEON +"\ + cryptonight-lite\n" +#endif +#ifndef XMRIG_NO_SUMO +"\ + cryptonight-heavy\n" +#endif +"\ -o, --url=URL URL of mining server\n\ -O, --userpass=U:P username:password pair for mining server\n\ -u, --user=USERNAME username for mining server\n\ diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h index d576b47a..60f0df08 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/CryptoNight_constants.h @@ -65,6 +65,9 @@ inline size_t cn_select_memory(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_MEMORY; + + default: + break; } return 0; @@ -88,6 +91,9 @@ inline uint32_t cn_select_mask(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_MASK; + + default: + break; } return 0; @@ -111,6 +117,9 @@ inline uint32_t cn_select_iter(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_ITER; + + default: + break; } return 0; diff --git a/src/net/Pool.cpp b/src/net/Pool.cpp index b3766b08..4d1d7439 100644 --- a/src/net/Pool.cpp +++ b/src/net/Pool.cpp @@ -38,15 +38,31 @@ static const char *algoNames[] = { "cryptonight", +# ifndef XMRIG_NO_AEON "cryptonight-lite", +# else + nullptr, +# endif +# ifndef XMRIG_NO_SUMO "cryptonight-heavy" +# else + nullptr +# endif }; static const char *algoNamesShort[] = { "cn", +# ifndef XMRIG_NO_AEON "cn-lite", +# else + nullptr, +# endif +# ifndef XMRIG_NO_SUMO "cn-heavy" +# else + nullptr +# endif }; @@ -110,23 +126,26 @@ const char *Pool::algoName(xmrig::Algo algorithm) xmrig::Algo Pool::algorithm(const char *algo) { +# ifndef XMRIG_NO_AEON if (strcasecmp(algo, "cryptonight-light") == 0) { fprintf(stderr, "Algorithm \"cryptonight-light\" is deprecated, use \"cryptonight-lite\" instead\n"); return xmrig::CRYPTONIGHT_LITE; } +# endif const size_t size = sizeof(algoNames) / sizeof(algoNames[0]); assert(size == (sizeof(algoNamesShort) / sizeof(algoNamesShort[0]))); for (size_t i = 0; i < size; i++) { - if (strcasecmp(algo, algoNames[i]) == 0 || strcasecmp(algo, algoNamesShort[i]) == 0) { + if ((algoNames[i] && strcasecmp(algo, algoNames[i]) == 0) || (algoNamesShort[i] && strcasecmp(algo, algoNamesShort[i]) == 0)) { return static_cast(i); } } - return xmrig::CRYPTONIGHT; + fprintf(stderr, "Unknown algorithm \"%s\" specified.\n", algo); + return xmrig::INVALID_ALGO; } diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index a6176743..6ba5c47f 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -144,7 +144,11 @@ bool DoubleWorker::selfTest() } # endif - return memcmp(m_hash, test_output_heavy, 64) == 0; +# ifndef XMRIG_NO_SUMO + return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_hash, test_output_heavy, 64) == 0; +# else + return false; +# endif } diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index ca6b93a6..815b965d 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -116,7 +116,11 @@ bool SingleWorker::selfTest() } # endif +# ifndef XMRIG_NO_SUMO return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_result.result, test_output_heavy, 32) == 0; +# else + return false; +# endif } diff --git a/src/xmrig.h b/src/xmrig.h index f4ae8162..7196aee3 100644 --- a/src/xmrig.h +++ b/src/xmrig.h @@ -30,6 +30,7 @@ namespace xmrig enum Algo { + INVALID_ALGO = -1, CRYPTONIGHT, /* CryptoNight (Monero) */ CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ CRYPTONIGHT_HEAVY, /* CryptoNight-Heavy (SUMO) */ From 01c824584643690f9dc7e015cb88e8e831f878e9 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 13 Apr 2018 05:05:53 +0700 Subject: [PATCH 194/389] #548 Fixed macOS build. --- src/core/CommonConfig.cpp | 2 +- src/core/Config.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index b444537e..726b0e37 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -266,7 +266,7 @@ bool xmrig::CommonConfig::save() uv_fs_close(uv_default_loop(), &req, fd, nullptr); uv_fs_req_cleanup(&req); - LOG_NOTICE("configuration saved to: \"%s\"", m_fileName); + LOG_NOTICE("configuration saved to: \"%s\"", m_fileName.data()); return true; } diff --git a/src/core/Config.cpp b/src/core/Config.cpp index a9136660..f7211250 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -279,7 +279,7 @@ bool xmrig::Config::parseInt(int key, int arg) { switch (key) { case xmrig::IConfig::ThreadsKey: /* --threads */ - if (m_threadsCount >= 0 && arg < 1024) { + if (arg >= 0 && arg < 1024) { m_threadsCount = arg; } break; From f197f6b1ebb13f6c46ec1e79fe3547961a7902f8 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 13 Apr 2018 06:38:18 +0700 Subject: [PATCH 195/389] Changed directory structure. --- CMakeLists.txt | 15 +++++++-------- src/{core => common/config}/CommonConfig.cpp | 2 +- src/{core => common/config}/CommonConfig.h | 2 +- src/{core => common/config}/ConfigLoader.cpp | 4 ++-- src/{core => common/config}/ConfigLoader.h | 0 src/{core => common/config}/ConfigWatcher.cpp | 4 ++-- src/{core => common/config}/ConfigWatcher.h | 2 +- src/{core => common}/utils/c_str.h | 0 src/core/Config.cpp | 2 +- src/core/Config.h | 2 +- src/core/Controller.cpp | 2 +- src/net/Pool.h | 2 +- src/workers/CpuThread.cpp | 2 +- 13 files changed, 19 insertions(+), 20 deletions(-) rename src/{core => common/config}/CommonConfig.cpp (99%) rename src/{core => common/config}/CommonConfig.h (99%) rename src/{core => common/config}/ConfigLoader.cpp (98%) rename src/{core => common/config}/ConfigLoader.h (100%) rename src/{core => common/config}/ConfigWatcher.cpp (97%) rename src/{core => common/config}/ConfigWatcher.h (98%) rename src/{core => common}/utils/c_str.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5502405c..096fa97f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,15 +14,14 @@ include (cmake/cpu.cmake) set(HEADERS src/api/NetworkState.h src/App.h + src/common/config/CommonConfig.h + src/common/config/ConfigLoader.h + src/common/config/ConfigWatcher.h + src/common/utils/c_str.h src/Console.h - src/core/CommonConfig.h src/core/Config.cpp - src/core/ConfigLoader.cpp - src/core/ConfigLoader.h src/core/ConfigLoader_platform.h - src/core/ConfigWatcher.cpp src/core/Controller.h - src/core/utils/c_str.h src/Cpu.h src/interfaces/IClientListener.h src/interfaces/IConfig.h @@ -89,11 +88,11 @@ endif() set(SOURCES src/api/NetworkState.cpp src/App.cpp + src/common/config/CommonConfig.cpp + src/common/config/ConfigLoader.cpp + src/common/config/ConfigWatcher.cpp src/Console.cpp - src/core/CommonConfig.cpp src/core/Config.cpp - src/core/ConfigLoader.cpp - src/core/ConfigWatcher.cpp src/core/Controller.cpp src/log/ConsoleLog.cpp src/log/FileLog.cpp diff --git a/src/core/CommonConfig.cpp b/src/common/config/CommonConfig.cpp similarity index 99% rename from src/core/CommonConfig.cpp rename to src/common/config/CommonConfig.cpp index 726b0e37..6902b7a2 100644 --- a/src/core/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -29,7 +29,7 @@ #include -#include "core/CommonConfig.h" +#include "common/config/CommonConfig.h" #include "donate.h" #include "log/Log.h" #include "net/Pool.h" diff --git a/src/core/CommonConfig.h b/src/common/config/CommonConfig.h similarity index 99% rename from src/core/CommonConfig.h rename to src/common/config/CommonConfig.h index 0b72b7e1..f4cf4b43 100644 --- a/src/core/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -28,7 +28,7 @@ #include -#include "core/utils/c_str.h" +#include "common/utils/c_str.h" #include "interfaces/IConfig.h" #include "net/Pool.h" #include "xmrig.h" diff --git a/src/core/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp similarity index 98% rename from src/core/ConfigLoader.cpp rename to src/common/config/ConfigLoader.cpp index c84ba962..a9c802f6 100644 --- a/src/core/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -32,10 +32,10 @@ #endif +#include "common/config/ConfigLoader.h" +#include "common/config/ConfigWatcher.h" #include "core/ConfigCreator.h" -#include "core/ConfigLoader.h" #include "core/ConfigLoader_platform.h" -#include "core/ConfigWatcher.h" #include "interfaces/IConfig.h" #include "interfaces/IWatcherListener.h" #include "net/Pool.h" diff --git a/src/core/ConfigLoader.h b/src/common/config/ConfigLoader.h similarity index 100% rename from src/core/ConfigLoader.h rename to src/common/config/ConfigLoader.h diff --git a/src/core/ConfigWatcher.cpp b/src/common/config/ConfigWatcher.cpp similarity index 97% rename from src/core/ConfigWatcher.cpp rename to src/common/config/ConfigWatcher.cpp index 092a0f3a..21d73188 100644 --- a/src/core/ConfigWatcher.cpp +++ b/src/common/config/ConfigWatcher.cpp @@ -25,9 +25,9 @@ #include +#include "common/config/ConfigLoader.h" +#include "common/config/ConfigWatcher.h" #include "core/ConfigCreator.h" -#include "core/ConfigLoader.h" -#include "core/ConfigWatcher.h" #include "interfaces/IWatcherListener.h" #include "log/Log.h" diff --git a/src/core/ConfigWatcher.h b/src/common/config/ConfigWatcher.h similarity index 98% rename from src/core/ConfigWatcher.h rename to src/common/config/ConfigWatcher.h index a5d25864..7f38b45a 100644 --- a/src/core/ConfigWatcher.h +++ b/src/common/config/ConfigWatcher.h @@ -29,7 +29,7 @@ #include -#include "core/utils/c_str.h" +#include "common/utils/c_str.h" #include "rapidjson/fwd.h" diff --git a/src/core/utils/c_str.h b/src/common/utils/c_str.h similarity index 100% rename from src/core/utils/c_str.h rename to src/common/utils/c_str.h diff --git a/src/core/Config.cpp b/src/core/Config.cpp index f7211250..ee305091 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -26,9 +26,9 @@ #include +#include "common/config/ConfigLoader.h" #include "core/Config.h" #include "core/ConfigCreator.h" -#include "core/ConfigLoader.h" #include "Cpu.h" #include "net/Pool.h" #include "rapidjson/document.h" diff --git a/src/core/Config.h b/src/core/Config.h index 536e1c01..999f3059 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -29,7 +29,7 @@ #include -#include "core/CommonConfig.h" +#include "common/config/CommonConfig.h" #include "rapidjson/fwd.h" #include "xmrig.h" diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index 805861ef..954e5b10 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -25,8 +25,8 @@ #include +#include "common/config/ConfigLoader.h" #include "core/Config.h" -#include "core/ConfigLoader.h" #include "core/Controller.h" #include "Cpu.h" #include "interfaces/IControllerListener.h" diff --git a/src/net/Pool.h b/src/net/Pool.h index efa3e3a7..aff1ea31 100644 --- a/src/net/Pool.h +++ b/src/net/Pool.h @@ -28,7 +28,7 @@ #include -#include "core/utils/c_str.h" +#include "common/utils/c_str.h" #include "xmrig.h" diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 482b4305..20a8006f 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -24,7 +24,7 @@ #include -#include "core/CommonConfig.h" +#include "net/Pool.h" #include "rapidjson/document.h" #include "workers/CpuThread.h" From 51422f4b1e3e7fe0df5506c12b01c547d18c55df Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 13 Apr 2018 07:00:51 +0700 Subject: [PATCH 196/389] Move xmrig.h to common/xmrig.h. --- CMakeLists.txt | 2 +- src/Cpu.h | 2 +- src/Mem.h | 2 +- src/Mem_win.cpp | 4 ++-- src/common/config/CommonConfig.cpp | 1 - src/common/config/CommonConfig.h | 2 +- src/{ => common}/xmrig.h | 0 src/core/Config.cpp | 1 - src/core/Config.h | 2 +- src/crypto/CryptoNight_constants.h | 2 +- src/interfaces/IThread.h | 2 +- src/net/Job.h | 2 +- src/net/Pool.h | 2 +- src/net/strategies/DonateStrategy.cpp | 2 +- src/workers/CpuThread.h | 2 +- 15 files changed, 13 insertions(+), 15 deletions(-) rename src/{ => common}/xmrig.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 096fa97f..97d3e51b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ set(HEADERS src/common/config/ConfigLoader.h src/common/config/ConfigWatcher.h src/common/utils/c_str.h + src/common/xmrig.h src/Console.h src/core/Config.cpp src/core/ConfigLoader_platform.h @@ -60,7 +61,6 @@ set(HEADERS src/workers/SingleWorker.h src/workers/Worker.h src/workers/Workers.h - src/xmrig.h ) set(HEADERS_CRYPTO diff --git a/src/Cpu.h b/src/Cpu.h index 118c7b7d..e739ccce 100644 --- a/src/Cpu.h +++ b/src/Cpu.h @@ -28,7 +28,7 @@ #include -#include "xmrig.h" +#include "common/xmrig.h" class Cpu diff --git a/src/Mem.h b/src/Mem.h index 73947b42..06b470c8 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -30,7 +30,7 @@ #include -#include "xmrig.h" +#include "common/xmrig.h" struct cryptonight_ctx; diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index ec30e3e8..1f3066ea 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -34,10 +34,10 @@ # include #endif -#include "log/Log.h" +#include "common/xmrig.h" #include "crypto/CryptoNight.h" +#include "log/Log.h" #include "Mem.h" -#include "xmrig.h" /***************************************************************** diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 6902b7a2..2cce845c 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -36,7 +36,6 @@ #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" -#include "xmrig.h" xmrig::CommonConfig::CommonConfig() : diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index f4cf4b43..5a229269 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -29,9 +29,9 @@ #include "common/utils/c_str.h" +#include "common/xmrig.h" #include "interfaces/IConfig.h" #include "net/Pool.h" -#include "xmrig.h" namespace xmrig { diff --git a/src/xmrig.h b/src/common/xmrig.h similarity index 100% rename from src/xmrig.h rename to src/common/xmrig.h diff --git a/src/core/Config.cpp b/src/core/Config.cpp index ee305091..07f11192 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -35,7 +35,6 @@ #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" #include "workers/CpuThread.h" -#include "xmrig.h" static char affinity_tmp[20] = { 0 }; diff --git a/src/core/Config.h b/src/core/Config.h index 999f3059..fd33b614 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -30,8 +30,8 @@ #include "common/config/CommonConfig.h" +#include "common/xmrig.h" #include "rapidjson/fwd.h" -#include "xmrig.h" class Addr; diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h index 60f0df08..3c746d47 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/CryptoNight_constants.h @@ -29,7 +29,7 @@ #include -#include "xmrig.h" +#include "common/xmrig.h" namespace xmrig diff --git a/src/interfaces/IThread.h b/src/interfaces/IThread.h index e2325c72..5b3efa0d 100644 --- a/src/interfaces/IThread.h +++ b/src/interfaces/IThread.h @@ -27,8 +27,8 @@ #include +#include "common/xmrig.h" #include "rapidjson/fwd.h" -#include "xmrig.h" namespace xmrig { diff --git a/src/net/Job.h b/src/net/Job.h index e8964314..a60f061c 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -30,8 +30,8 @@ #include +#include "common/xmrig.h" #include "net/Id.h" -#include "xmrig.h" class Job diff --git a/src/net/Pool.h b/src/net/Pool.h index aff1ea31..ecce865a 100644 --- a/src/net/Pool.h +++ b/src/net/Pool.h @@ -29,7 +29,7 @@ #include "common/utils/c_str.h" -#include "xmrig.h" +#include "common/xmrig.h" class Pool diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 311ca424..da25e23f 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -22,13 +22,13 @@ */ +#include "common/xmrig.h" #include "interfaces/IStrategyListener.h" #include "net/Client.h" #include "net/Job.h" #include "net/strategies/DonateStrategy.h" #include "net/strategies/FailoverStrategy.h" #include "Platform.h" -#include "xmrig.h" extern "C" diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index aef73719..dadd0424 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -25,8 +25,8 @@ #define __CPUTHREAD_H__ +#include "common/xmrig.h" #include "interfaces/IThread.h" -#include "xmrig.h" struct cryptonight_ctx; From a6b698d4ebc93525adc592f85e82fd41d7a2d2d2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 13 Apr 2018 07:12:53 +0700 Subject: [PATCH 197/389] Move common parts of API. --- CMakeLists.txt | 12 ++++++------ src/App.cpp | 2 +- src/api/Api.cpp | 4 ++-- src/api/ApiRouter.cpp | 4 ++-- src/{ => common}/api/HttpBody.h | 0 src/{ => common}/api/HttpReply.h | 0 src/{ => common}/api/HttpRequest.cpp | 6 +++--- src/{ => common}/api/HttpRequest.h | 0 src/{ => common}/api/Httpd.cpp | 6 +++--- src/{ => common}/api/Httpd.h | 0 10 files changed, 17 insertions(+), 17 deletions(-) rename src/{ => common}/api/HttpBody.h (100%) rename src/{ => common}/api/HttpReply.h (100%) rename src/{ => common}/api/HttpRequest.cpp (97%) rename src/{ => common}/api/HttpRequest.h (100%) rename src/{ => common}/api/Httpd.cpp (97%) rename src/{ => common}/api/Httpd.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 97d3e51b..0da768b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -214,14 +214,14 @@ if (WITH_HTTPD) set(HTTPD_SOURCES src/api/Api.h src/api/ApiRouter.h - src/api/HttpBody.h - src/api/Httpd.h - src/api/HttpReply.h - src/api/HttpRequest.h + src/common/api/HttpBody.h + src/common/api/Httpd.h + src/common/api/HttpReply.h + src/common/api/HttpRequest.h src/api/Api.cpp src/api/ApiRouter.cpp - src/api/Httpd.cpp - src/api/HttpRequest.cpp + src/common/api/Httpd.cpp + src/common/api/HttpRequest.cpp ) else() message(FATAL_ERROR "microhttpd NOT found: use `-DWITH_HTTPD=OFF` to build without http deamon support") diff --git a/src/App.cpp b/src/App.cpp index dc22836d..9ac78500 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -45,7 +45,7 @@ #ifndef XMRIG_NO_HTTPD -# include "api/Httpd.h" +# include "common/api/Httpd.h" #endif diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 36859136..6f9991ee 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -26,8 +26,8 @@ #include "api/Api.h" #include "api/ApiRouter.h" -#include "api/HttpReply.h" -#include "api/HttpRequest.h" +#include "common/api/HttpReply.h" +#include "common/api/HttpRequest.h" ApiRouter *Api::m_router = nullptr; diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 6c4f1f27..81f765c5 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -33,8 +33,8 @@ #include "api/ApiRouter.h" -#include "api/HttpReply.h" -#include "api/HttpRequest.h" +#include "common/api/HttpReply.h" +#include "common/api/HttpRequest.h" #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" diff --git a/src/api/HttpBody.h b/src/common/api/HttpBody.h similarity index 100% rename from src/api/HttpBody.h rename to src/common/api/HttpBody.h diff --git a/src/api/HttpReply.h b/src/common/api/HttpReply.h similarity index 100% rename from src/api/HttpReply.h rename to src/common/api/HttpReply.h diff --git a/src/api/HttpRequest.cpp b/src/common/api/HttpRequest.cpp similarity index 97% rename from src/api/HttpRequest.cpp rename to src/common/api/HttpRequest.cpp index ac7210bd..01245dfc 100644 --- a/src/api/HttpRequest.cpp +++ b/src/common/api/HttpRequest.cpp @@ -25,9 +25,9 @@ #include #include -#include "api/HttpBody.h" -#include "api/HttpRequest.h" -#include "api/HttpReply.h" +#include "common/api/HttpBody.h" +#include "common/api/HttpRequest.h" +#include "common/api/HttpReply.h" #ifndef MHD_HTTP_PAYLOAD_TOO_LARGE diff --git a/src/api/HttpRequest.h b/src/common/api/HttpRequest.h similarity index 100% rename from src/api/HttpRequest.h rename to src/common/api/HttpRequest.h diff --git a/src/api/Httpd.cpp b/src/common/api/Httpd.cpp similarity index 97% rename from src/api/Httpd.cpp rename to src/common/api/Httpd.cpp index e0901b77..135ac7c9 100644 --- a/src/api/Httpd.cpp +++ b/src/common/api/Httpd.cpp @@ -27,9 +27,9 @@ #include "api/Api.h" -#include "api/Httpd.h" -#include "api/HttpReply.h" -#include "api/HttpRequest.h" +#include "common/api/Httpd.h" +#include "common/api/HttpReply.h" +#include "common/api/HttpRequest.h" #include "log/Log.h" diff --git a/src/api/Httpd.h b/src/common/api/Httpd.h similarity index 100% rename from src/api/Httpd.h rename to src/common/api/Httpd.h From c1800094d0a5b91755ea66498ebd0c50261a4a1b Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 13 Apr 2018 07:23:01 +0700 Subject: [PATCH 198/389] Move Platform. --- CMakeLists.txt | 10 +++++----- src/App.cpp | 2 +- src/api/ApiRouter.cpp | 2 +- src/{ => common}/Platform.cpp | 0 src/{ => common}/Platform.h | 0 src/{ => common}/Platform_mac.cpp | 0 src/{ => common}/Platform_unix.cpp | 0 src/{ => common}/Platform_win.cpp | 0 src/common/config/ConfigLoader.cpp | 2 +- src/core/Controller.cpp | 2 +- src/net/strategies/DonateStrategy.cpp | 2 +- src/net/strategies/FailoverStrategy.cpp | 2 +- src/net/strategies/SinglePoolStrategy.cpp | 2 +- src/workers/Worker.cpp | 2 +- 14 files changed, 13 insertions(+), 13 deletions(-) rename src/{ => common}/Platform.cpp (100%) rename src/{ => common}/Platform.h (100%) rename src/{ => common}/Platform_mac.cpp (100%) rename src/{ => common}/Platform_unix.cpp (100%) rename src/{ => common}/Platform_win.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0da768b2..c8d381d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ set(HEADERS src/common/config/CommonConfig.h src/common/config/ConfigLoader.h src/common/config/ConfigWatcher.h + src/common/Platform.h src/common/utils/c_str.h src/common/xmrig.h src/Console.h @@ -51,7 +52,6 @@ set(HEADERS src/net/strategies/FailoverStrategy.h src/net/strategies/SinglePoolStrategy.h src/net/SubmitResult.h - src/Platform.h src/Summary.h src/version.h src/workers/CpuThread.h @@ -91,6 +91,7 @@ set(SOURCES src/common/config/CommonConfig.cpp src/common/config/ConfigLoader.cpp src/common/config/ConfigWatcher.cpp + src/common/Platform.cpp src/Console.cpp src/core/Config.cpp src/core/Controller.cpp @@ -106,7 +107,6 @@ set(SOURCES src/net/strategies/FailoverStrategy.cpp src/net/strategies/SinglePoolStrategy.cpp src/net/SubmitResult.cpp - src/Platform.cpp src/Summary.cpp src/workers/CpuThread.cpp src/workers/DoubleWorker.cpp @@ -130,9 +130,9 @@ if (WIN32) set(SOURCES_OS res/app.rc src/App_win.cpp + src/common/Platform_win.cpp src/Cpu_win.cpp src/Mem_win.cpp - src/Platform_win.cpp ) add_definitions(/DWIN32) @@ -140,16 +140,16 @@ if (WIN32) elseif (APPLE) set(SOURCES_OS src/App_unix.cpp + src/common/Platform_mac.cpp src/Cpu_mac.cpp src/Mem_unix.cpp - src/Platform_mac.cpp ) else() set(SOURCES_OS src/App_unix.cpp + src/common/Platform_unix.cpp src/Cpu_unix.cpp src/Mem_unix.cpp - src/Platform_unix.cpp ) set(EXTRA_LIBS pthread rt) diff --git a/src/App.cpp b/src/App.cpp index 9ac78500..3d0cb3df 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -28,6 +28,7 @@ #include "api/Api.h" #include "App.h" +#include "common/Platform.h" #include "Console.h" #include "core/Config.h" #include "core/Controller.h" @@ -38,7 +39,6 @@ #include "log/Log.h" #include "Mem.h" #include "net/Network.h" -#include "Platform.h" #include "Summary.h" #include "version.h" #include "workers/Workers.h" diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 81f765c5..cb7a8005 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -35,13 +35,13 @@ #include "api/ApiRouter.h" #include "common/api/HttpReply.h" #include "common/api/HttpRequest.h" +#include "common/Platform.h" #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" #include "interfaces/IThread.h" #include "Mem.h" #include "net/Job.h" -#include "Platform.h" #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" #include "rapidjson/stringbuffer.h" diff --git a/src/Platform.cpp b/src/common/Platform.cpp similarity index 100% rename from src/Platform.cpp rename to src/common/Platform.cpp diff --git a/src/Platform.h b/src/common/Platform.h similarity index 100% rename from src/Platform.h rename to src/common/Platform.h diff --git a/src/Platform_mac.cpp b/src/common/Platform_mac.cpp similarity index 100% rename from src/Platform_mac.cpp rename to src/common/Platform_mac.cpp diff --git a/src/Platform_unix.cpp b/src/common/Platform_unix.cpp similarity index 100% rename from src/Platform_unix.cpp rename to src/common/Platform_unix.cpp diff --git a/src/Platform_win.cpp b/src/common/Platform_win.cpp similarity index 100% rename from src/Platform_win.cpp rename to src/common/Platform_win.cpp diff --git a/src/common/config/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp index a9c802f6..47c6cdf9 100644 --- a/src/common/config/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -34,12 +34,12 @@ #include "common/config/ConfigLoader.h" #include "common/config/ConfigWatcher.h" +#include "common/Platform.h" #include "core/ConfigCreator.h" #include "core/ConfigLoader_platform.h" #include "interfaces/IConfig.h" #include "interfaces/IWatcherListener.h" #include "net/Pool.h" -#include "Platform.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" #include "rapidjson/filereadstream.h" diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index 954e5b10..f120c0c1 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -26,6 +26,7 @@ #include "common/config/ConfigLoader.h" +#include "common/Platform.h" #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" @@ -34,7 +35,6 @@ #include "log/FileLog.h" #include "log/Log.h" #include "net/Network.h" -#include "Platform.h" #ifdef HAVE_SYSLOG_H diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index da25e23f..27beae33 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -22,13 +22,13 @@ */ +#include "common/Platform.h" #include "common/xmrig.h" #include "interfaces/IStrategyListener.h" #include "net/Client.h" #include "net/Job.h" #include "net/strategies/DonateStrategy.h" #include "net/strategies/FailoverStrategy.h" -#include "Platform.h" extern "C" diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index a4e25277..cef9da0c 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -22,10 +22,10 @@ */ +#include "common/Platform.h" #include "interfaces/IStrategyListener.h" #include "net/Client.h" #include "net/strategies/FailoverStrategy.h" -#include "Platform.h" FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index 8c113d93..c74a794b 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -22,10 +22,10 @@ */ +#include "common/Platform.h" #include "interfaces/IStrategyListener.h" #include "net/Client.h" #include "net/strategies/SinglePoolStrategy.h" -#include "Platform.h" SinglePoolStrategy::SinglePoolStrategy(const Pool &pool, int retryPause, IStrategyListener *listener, bool quiet) : diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index f9162e3f..30305ac6 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -24,9 +24,9 @@ #include +#include "common/Platform.h" #include "Cpu.h" #include "Mem.h" -#include "Platform.h" #include "workers/CpuThread.h" #include "workers/Handle.h" #include "workers/Worker.h" From 9ce9147dad8924e9c14537ab76639076b7eb6dbf Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 13 Apr 2018 09:27:37 +0700 Subject: [PATCH 199/389] Use new method to set affinity. --- src/App_unix.cpp | 6 ------ src/App_win.cpp | 6 ------ src/Cpu.h | 1 - src/Cpu_unix.cpp | 25 ------------------------- src/Cpu_win.cpp | 11 ----------- src/Mem_unix.cpp | 2 +- src/common/Platform.cpp | 9 +++++---- src/common/Platform.h | 14 ++++++++++---- src/common/Platform_mac.cpp | 19 +++++++++++++++---- src/common/Platform_unix.cpp | 34 ++++++++++++++++++++++++++++++---- src/common/Platform_win.cpp | 21 +++++++++++++++------ src/core/Controller.cpp | 1 - src/workers/Handle.cpp | 3 +-- src/workers/Handle.h | 4 +--- src/workers/Worker.cpp | 6 +++--- src/workers/Workers.cpp | 2 +- 16 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/App_unix.cpp b/src/App_unix.cpp index fdb2b124..f739e42e 100644 --- a/src/App_unix.cpp +++ b/src/App_unix.cpp @@ -31,7 +31,6 @@ #include "App.h" #include "core/Config.h" #include "core/Controller.h" -#include "Cpu.h" #include "log/Log.h" @@ -39,11 +38,6 @@ void App::background() { signal(SIGPIPE, SIG_IGN); - const int64_t affinity = m_controller->config()->affinity(); - if (affinity != -1L) { - Cpu::setAffinity(-1, affinity); - } - if (!m_controller->config()->isBackground()) { return; } diff --git a/src/App_win.cpp b/src/App_win.cpp index b3a2c4cf..9b923870 100644 --- a/src/App_win.cpp +++ b/src/App_win.cpp @@ -27,18 +27,12 @@ #include "App.h" -#include "Cpu.h" #include "core/Controller.h" #include "core/Config.h" void App::background() { - const int64_t affinity = m_controller->config()->affinity(); - if (affinity != -1L) { - Cpu::setAffinity(-1, affinity); - } - if (!m_controller->config()->isBackground()) { return; } diff --git a/src/Cpu.h b/src/Cpu.h index e739ccce..97e593ed 100644 --- a/src/Cpu.h +++ b/src/Cpu.h @@ -42,7 +42,6 @@ public: static int optimalThreadsCount(xmrig::Algo algo, bool doubleHash, int maxCpuUsage); static void init(); - static void setAffinity(int id, uint64_t mask); static inline bool hasAES() { return (m_flags & AES) != 0; } static inline bool isX64() { return (m_flags & X86_64) != 0; } diff --git a/src/Cpu_unix.cpp b/src/Cpu_unix.cpp index 9a13e7a5..b895c897 100644 --- a/src/Cpu_unix.cpp +++ b/src/Cpu_unix.cpp @@ -52,28 +52,3 @@ void Cpu::init() initCommon(); } - - -void Cpu::setAffinity(int id, uint64_t mask) -{ - cpu_set_t set; - CPU_ZERO(&set); - - for (int i = 0; i < m_totalThreads; i++) { - if (mask & (1UL << i)) { - CPU_SET(i, &set); - } - } - - if (id == -1) { -# ifndef __FreeBSD__ - sched_setaffinity(0, sizeof(&set), &set); -# endif - } else { -# ifndef __ANDROID__ - pthread_setaffinity_np(pthread_self(), sizeof(&set), &set); -# else - sched_setaffinity(gettid(), sizeof(&set), &set); -# endif - } -} diff --git a/src/Cpu_win.cpp b/src/Cpu_win.cpp index 13113a17..7258f726 100644 --- a/src/Cpu_win.cpp +++ b/src/Cpu_win.cpp @@ -39,14 +39,3 @@ void Cpu::init() initCommon(); } - - -void Cpu::setAffinity(int id, uint64_t mask) -{ - if (id == -1) { - SetProcessAffinityMask(GetCurrentProcess(), mask); - } - else { - SetThreadAffinityMask(GetCurrentThread(), mask); - } -} diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 0dd833d7..df7aaad2 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -34,10 +34,10 @@ #endif +#include "common/xmrig.h" #include "crypto/CryptoNight.h" #include "log/Log.h" #include "Mem.h" -#include "xmrig.h" bool Mem::allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled) diff --git a/src/common/Platform.cpp b/src/common/Platform.cpp index 4ddb1429..52b55987 100644 --- a/src/common/Platform.cpp +++ b/src/common/Platform.cpp @@ -29,16 +29,16 @@ #include "Platform.h" -char *Platform::m_defaultConfigName = nullptr; -char *Platform::m_userAgent = nullptr; +char Platform::m_defaultConfigName[520] = { 0 }; +xmrig::c_str Platform::m_userAgent; const char *Platform::defaultConfigName() { size_t size = 520; - if (m_defaultConfigName == nullptr) { - m_defaultConfigName = new char[size]; + if (*m_defaultConfigName) { + return m_defaultConfigName; } if (uv_exepath(m_defaultConfigName, &size) < 0) { @@ -58,5 +58,6 @@ const char *Platform::defaultConfigName() } } + *m_defaultConfigName = '\0'; return nullptr; } diff --git a/src/common/Platform.h b/src/common/Platform.h index 87c8cc4d..8704604a 100644 --- a/src/common/Platform.h +++ b/src/common/Platform.h @@ -25,20 +25,26 @@ #define __PLATFORM_H__ +#include + + +#include "common/utils/c_str.h" + + class Platform { public: + static bool setThreadAffinity(uint64_t cpu_id); static const char *defaultConfigName(); static void init(const char *userAgent); - static void release(); static void setProcessPriority(int priority); static void setThreadPriority(int priority); - static inline const char *userAgent() { return m_userAgent; } + static inline const char *userAgent() { return m_userAgent.data(); } private: - static char *m_defaultConfigName; - static char *m_userAgent; + static char m_defaultConfigName[520]; + static xmrig::c_str m_userAgent; }; diff --git a/src/common/Platform_mac.cpp b/src/common/Platform_mac.cpp index ba541f1d..b8181cc4 100644 --- a/src/common/Platform_mac.cpp +++ b/src/common/Platform_mac.cpp @@ -22,6 +22,8 @@ */ +#include +#include #include #include #include @@ -53,15 +55,24 @@ static inline char *createUserAgent() } -void Platform::init(const char *userAgent) +bool Platform::setThreadAffinity(uint64_t cpu_id) { - m_userAgent = userAgent ? strdup(userAgent) : createUserAgent(); + thread_port_t mach_thread; + thread_affinity_policy_data_t policy = { static_cast(cpu_id) }; + mach_thread = pthread_mach_thread_np(pthread_self()); + + return thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1) == KERN_SUCCESS; } -void Platform::release() +void Platform::init(const char *userAgent) { - delete [] m_userAgent; + if (userAgent) { + m_userAgent = userAgent; + } + else { + m_userAgent = createUserAgent(); + } } diff --git a/src/common/Platform_unix.cpp b/src/common/Platform_unix.cpp index c0589307..624594e9 100644 --- a/src/common/Platform_unix.cpp +++ b/src/common/Platform_unix.cpp @@ -21,6 +21,14 @@ * along with this program. If not, see . */ +#ifdef __FreeBSD__ +# include +# include +# include +# include +#endif + + #include #include #include @@ -37,6 +45,11 @@ #endif +#ifdef __FreeBSD__ +typedef cpuset_t cpu_set_t; +#endif + + static inline char *createUserAgent() { const size_t max = 160; @@ -63,15 +76,28 @@ static inline char *createUserAgent() } -void Platform::init(const char *userAgent) +bool Platform::setThreadAffinity(uint64_t cpu_id) { - m_userAgent = userAgent ? strdup(userAgent) : createUserAgent(); + cpu_set_t mn; + CPU_ZERO(&mn); + CPU_SET(cpu_id, &mn); + +# ifndef __ANDROID__ + return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &mn) == 0; +# else + return sched_setaffinity(gettid(), sizeof(cpu_set_t), &mn) == 0; +# endif } -void Platform::release() +void Platform::init(const char *userAgent) { - delete [] m_userAgent; + if (userAgent) { + m_userAgent = userAgent; + } + else { + m_userAgent = createUserAgent(); + } } diff --git a/src/common/Platform_win.cpp b/src/common/Platform_win.cpp index 880bdd98..47f41867 100644 --- a/src/common/Platform_win.cpp +++ b/src/common/Platform_win.cpp @@ -27,9 +27,11 @@ #include +#include "log/Log.h" #include "Platform.h" #include "version.h" + #ifdef XMRIG_NVIDIA_PROJECT # include "nvidia/cryptonight.h" #endif @@ -82,16 +84,24 @@ static inline char *createUserAgent() } -void Platform::init(const char *userAgent) +bool Platform::setThreadAffinity(uint64_t cpu_id) { - m_userAgent = userAgent ? strdup(userAgent) : createUserAgent(); + if (cpu_id >= 64) { + LOG_ERR("Unable to set affinity. Windows supports only affinity up to 63."); + } + + return SetThreadAffinityMask(GetCurrentThread(), 1ULL << cpu_id) != 0; } -void Platform::release() +void Platform::init(const char *userAgent) { - delete [] m_defaultConfigName; - delete [] m_userAgent; + if (userAgent) { + m_userAgent = userAgent; + } + else { + m_userAgent = createUserAgent(); + } } @@ -131,7 +141,6 @@ void Platform::setProcessPriority(int priority) } - void Platform::setThreadPriority(int priority) { if (priority == -1) { diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index f120c0c1..5f0a9bb3 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -73,7 +73,6 @@ xmrig::Controller::Controller() xmrig::Controller::~Controller() { ConfigLoader::release(); - Platform::release(); delete d_ptr; } diff --git a/src/workers/Handle.cpp b/src/workers/Handle.cpp index 01d032e9..29f57fb2 100644 --- a/src/workers/Handle.cpp +++ b/src/workers/Handle.cpp @@ -25,8 +25,7 @@ #include "workers/Handle.h" -Handle::Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays, int64_t affinity) : - m_affinity(affinity), +Handle::Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays) : m_worker(nullptr), m_totalThreads(totalThreads), m_totalWays(totalWays), diff --git a/src/workers/Handle.h b/src/workers/Handle.h index 8a64922a..b3a7c76f 100644 --- a/src/workers/Handle.h +++ b/src/workers/Handle.h @@ -38,11 +38,10 @@ class IWorker; class Handle { public: - Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays, int64_t affinity); + Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays); void join(); void start(void (*callback) (void *)); - inline int64_t affinity() const { return m_affinity; } inline IWorker *worker() const { return m_worker; } inline size_t threadId() const { return m_config->index(); } inline size_t totalThreads() const { return m_totalThreads; } @@ -51,7 +50,6 @@ public: inline xmrig::IThread *config() const { return m_config; } private: - int64_t m_affinity; IWorker *m_worker; size_t m_totalThreads; size_t m_totalWays; diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index 30305ac6..59434390 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -42,11 +42,11 @@ Worker::Worker(Handle *handle) : m_sequence(0), m_thread(static_cast(handle->config())) { - if (Cpu::threads() > 1 && handle->affinity() != -1L) { - Cpu::setAffinity(m_id, handle->affinity()); + if (Cpu::threads() > 1 && m_thread->affinity() != -1L) { + Platform::setThreadAffinity(m_thread->affinity()); } - Platform::setThreadPriority(handle->config()->priority()); + Platform::setThreadPriority(m_thread->priority()); m_ctx = Mem::create(m_id); } diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 38965d8a..00941c7b 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -130,7 +130,7 @@ void Workers::start(xmrig::Controller *controller) uv_timer_start(&m_timer, Workers::onTick, 500, 500); for (xmrig::IThread *thread : threads) { - Handle *handle = new Handle(thread, threads.size(), totalWays, controller->config()->affinity()); + Handle *handle = new Handle(thread, threads.size(), totalWays); m_workers.push_back(handle); handle->start(Workers::onReady); } From c44b2997504b93e0e47cce634cc494daf3ead088 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 13 Apr 2018 17:59:27 +0700 Subject: [PATCH 200/389] Added reader for advanced threads. --- src/common/xmrig.h | 7 +++++++ src/core/Config.cpp | 40 +++++++++++++++++++++++++++------------ src/core/Config.h | 25 ++++++++++++++++++------ src/workers/CpuThread.cpp | 27 ++++++++++++++++++++++++++ src/workers/CpuThread.h | 20 ++++++++++++++++++++ 5 files changed, 101 insertions(+), 18 deletions(-) diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 7196aee3..0aa6b842 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -64,6 +64,13 @@ enum Variant { }; +enum AesMode { + AES_AUTO, + AES_HW, + AES_SOFT +}; + + } /* namespace xmrig */ diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 07f11192..2b8809ff 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -41,6 +41,7 @@ static char affinity_tmp[20] = { 0 }; xmrig::Config::Config() : xmrig::CommonConfig(), + m_aesMode(AES_AUTO), m_algoVariant(AV_AUTO), m_doubleHash(false), m_dryRun(false), @@ -48,9 +49,7 @@ xmrig::Config::Config() : xmrig::CommonConfig(), m_safe(false), m_maxCpuUsage(75), m_printTime(60), - m_priority(-1), - m_affinity(-1L), - m_threadsCount(0) + m_priority(-1) { } @@ -162,18 +161,18 @@ bool xmrig::Config::adjust() m_doubleHash = true; } - if (!m_threadsCount) { - m_threadsCount = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); + if (!m_threads.count) { + m_threads.count = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); } else if (m_safe) { const size_t count = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); - if (m_threadsCount > count) { - m_threadsCount = count; + if (m_threads.count > count) { + m_threads.count = count; } } - for (size_t i = 0; i < m_threadsCount; ++i) { - m_threads.push_back(CpuThread::createFromAV(i, m_algorithm, m_algoVariant, m_affinity, m_priority)); + for (size_t i = 0; i < m_threads.count; ++i) { + m_threads.list.push_back(CpuThread::createFromAV(i, m_algorithm, m_algoVariant, m_threads.mask, m_priority)); } return true; @@ -228,7 +227,7 @@ bool xmrig::Config::parseString(int key, const char *arg) case xmrig::IConfig::ThreadsKey: /* --threads */ if (strncmp(arg, "all", 3) == 0) { - m_threadsCount = Cpu::threads(); + m_threads.count = Cpu::threads(); return true; } @@ -257,7 +256,7 @@ bool xmrig::Config::parseUint64(int key, uint64_t arg) switch (key) { case xmrig::IConfig::CPUAffinityKey: /* --cpu-affinity */ if (arg) { - m_affinity = arg; + m_threads.mask = arg; } break; @@ -271,6 +270,23 @@ bool xmrig::Config::parseUint64(int key, uint64_t arg) void xmrig::Config::parseJSON(const rapidjson::Document &doc) { + const rapidjson::Value &threads = doc["threads"]; + + if (threads.IsArray()) { + for (const rapidjson::Value &value : threads.GetArray()) { + if (!value.IsObject()) { + continue; + } + + if (value.HasMember("low_power_mode")) { + auto data = CpuThread::parse(value); + + if (data.valid) { + m_threads.cpu.push_back(std::move(data)); + } + } + } + } } @@ -279,7 +295,7 @@ bool xmrig::Config::parseInt(int key, int arg) switch (key) { case xmrig::IConfig::ThreadsKey: /* --threads */ if (arg >= 0 && arg < 1024) { - m_threadsCount = arg; + m_threads.count = arg; } break; diff --git a/src/core/Config.h b/src/core/Config.h index fd33b614..657679ec 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -32,6 +32,7 @@ #include "common/config/CommonConfig.h" #include "common/xmrig.h" #include "rapidjson/fwd.h" +#include "workers/CpuThread.h" class Addr; @@ -67,15 +68,16 @@ public: void getJSON(rapidjson::Document &doc) const override; + inline AesMode aesMode() const { return m_aesMode; } inline AlgoVariant algoVariant() const { return m_algoVariant; } inline bool isDoubleHash() const { return m_doubleHash; } inline bool isDryRun() const { return m_dryRun; } inline bool isHugePages() const { return m_hugePages; } - inline const std::vector &threads() const { return m_threads; } + inline const std::vector &threads() const { return m_threads.list; } inline int printTime() const { return m_printTime; } inline int priority() const { return m_priority; } - inline int threadsCount() const { return m_threadsCount; } - inline int64_t affinity() const { return m_affinity; } + inline int threadsCount() const { return m_threads.count; } + inline int64_t affinity() const { return m_threads.mask; } static Config *load(int argc, char **argv, IWatcherListener *listener); @@ -94,6 +96,19 @@ private: AlgoVariant getAlgoVariantLite() const; # endif + + struct Threads + { + inline Threads() : mask(-1L), count(0) {} + + int64_t mask; + size_t count; + std::vector cpu; + std::vector list; + }; + + + AesMode m_aesMode; AlgoVariant m_algoVariant; bool m_doubleHash; bool m_dryRun; @@ -102,9 +117,7 @@ private: int m_maxCpuUsage; int m_printTime; int m_priority; - int64_t m_affinity; - size_t m_threadsCount; - std::vector m_threads; + Threads m_threads; }; diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 20a8006f..5a707a58 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -197,6 +197,33 @@ xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, A } +xmrig::CpuThread::Data xmrig::CpuThread::parse(const rapidjson::Value &object) +{ + Data data; + + const auto &multiway = object["low_power_mode"]; + if (multiway.IsBool()) { + data.multiway = multiway.IsTrue() ? DoubleWay : SingleWay; + data.valid = true; + } + else if (multiway.IsUint()) { + data.setMultiway(multiway.GetInt()); + } + + if (!data.valid) { + return data; + } + + const auto &affinity = object["affine_to_cpu"]; + + if (affinity.IsUint64()) { + data.affinity = affinity.GetInt64(); + } + + return data; +} + + #ifndef XMRIG_NO_API rapidjson::Value xmrig::CpuThread::toAPI(rapidjson::Document &doc) const { diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index dadd0424..f9640a6c 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -46,6 +46,25 @@ public: PentaWay }; + + struct Data + { + inline Data() : valid(false), affinity(-1L), multiway(SingleWay) {} + + inline void setMultiway(int value) + { + if (value >= SingleWay && value <= PentaWay) { + multiway = static_cast(value); + valid = true; + } + } + + bool valid; + int64_t affinity; + Multiway multiway; + }; + + CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch); ~CpuThread(); @@ -53,6 +72,7 @@ public: static cn_hash_fun fn(Algo algorithm, AlgoVariant av, Variant variant); static CpuThread *createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority); + static Data parse(const rapidjson::Value &object); inline bool isPrefetch() const { return m_prefetch; } inline bool isSoftAES() const { return m_softAES; } From c81401ab2d2aabfc8219cdeee11f8fc5c7b79321 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 14 Apr 2018 07:01:12 +0700 Subject: [PATCH 201/389] Basic advanced config reader, only single hash supported. --- src/core/Config.cpp | 12 ++++++++++++ src/core/Config.h | 2 +- src/log/Log.cpp | 12 ++++++++++-- src/log/Log.h | 21 +++++++++++++++------ src/workers/CpuThread.cpp | 18 ++++++++++++++++++ src/workers/CpuThread.h | 1 + src/workers/Workers.cpp | 7 +++++-- 7 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 2b8809ff..ddce6980 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -156,6 +156,18 @@ bool xmrig::Config::adjust() return false; } + if (m_aesMode == AES_AUTO) { + m_aesMode = Cpu::hasAES() ? AES_SOFT : AES_SOFT; + } + + if (!m_threads.cpu.empty()) { + for (size_t i = 0; i < m_threads.cpu.size(); ++i) { + m_threads.list.push_back(CpuThread::createFromData(i, m_algorithm, m_threads.cpu[i], m_priority, m_aesMode == AES_SOFT)); + } + + return true; + } + m_algoVariant = getAlgoVariant(); if (m_algoVariant == AV_DOUBLE || m_algoVariant == AV_DOUBLE_SOFT) { m_doubleHash = true; diff --git a/src/core/Config.h b/src/core/Config.h index 657679ec..720557a7 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -76,7 +76,7 @@ public: inline const std::vector &threads() const { return m_threads.list; } inline int printTime() const { return m_printTime; } inline int priority() const { return m_priority; } - inline int threadsCount() const { return m_threads.count; } + inline int threadsCount() const { return m_threads.list.size(); } inline int64_t affinity() const { return m_threads.mask; } static Config *load(int argc, char **argv, IWatcherListener *listener); diff --git a/src/log/Log.cpp b/src/log/Log.cpp index 3e5d5671..131faa54 100644 --- a/src/log/Log.cpp +++ b/src/log/Log.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,6 +38,8 @@ Log *Log::m_self = nullptr; void Log::message(Log::Level level, const char* fmt, ...) { + uv_mutex_lock(&m_mutex); + va_list args; va_list copy; va_start(args, fmt); @@ -47,11 +49,15 @@ void Log::message(Log::Level level, const char* fmt, ...) backend->message(level, fmt, copy); va_end(copy); } + + uv_mutex_unlock(&m_mutex); } void Log::text(const char* fmt, ...) { + uv_mutex_lock(&m_mutex); + va_list args; va_list copy; va_start(args, fmt); @@ -63,6 +69,8 @@ void Log::text(const char* fmt, ...) } va_end(args); + + uv_mutex_unlock(&m_mutex); } diff --git a/src/log/Log.h b/src/log/Log.h index fd944d80..f8c6a401 100644 --- a/src/log/Log.h +++ b/src/log/Log.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,6 +25,7 @@ #define __LOG_H__ +#include #include #include @@ -54,20 +55,28 @@ public: constexpr static const char* kCL_GRAY = "\x1B[90m"; # endif - static inline Log* i() { return m_self; } + static inline Log* i() { assert(m_self != nullptr); return m_self; } static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); } - static inline void init() { if (!m_self) { m_self = new Log();} } - static inline void release() { delete m_self; } + static inline void init() { if (!m_self) { new Log(); } } + static inline void release() { assert(m_self != nullptr); delete m_self; } void message(Level level, const char* fmt, ...); void text(const char* fmt, ...); private: - inline Log() {} + inline Log() { + assert(m_self == nullptr); + + uv_mutex_init(&m_mutex); + + m_self = this; + } + ~Log(); static Log *m_self; std::vector m_backends; + uv_mutex_t m_mutex; }; diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 5a707a58..07fbbb64 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -197,6 +197,24 @@ xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, A } +xmrig::CpuThread *xmrig::CpuThread::createFromData(size_t index, Algo algorithm, const CpuThread::Data &data, int priority, bool softAES) +{ + int av = AV_AUTO; + const Multiway multiway = data.multiway; + + if (multiway <= DoubleWay) { + av = softAES ? (multiway + 2) : multiway; + } + else { + av = softAES ? (multiway + 5) : (multiway + 2); + } + + assert(av > AV_AUTO && av < AV_MAX); + + return new CpuThread(index, algorithm, static_cast(av), multiway, data.affinity, priority, softAES, false); +} + + xmrig::CpuThread::Data xmrig::CpuThread::parse(const rapidjson::Value &object) { Data data; diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index f9640a6c..9d1bc4e7 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -72,6 +72,7 @@ public: static cn_hash_fun fn(Algo algorithm, AlgoVariant av, Variant variant); static CpuThread *createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority); + static CpuThread *createFromData(size_t index, Algo algorithm, const CpuThread::Data &data, int priority, bool softAES); static Data parse(const rapidjson::Value &object); inline bool isPrefetch() const { return m_prefetch; } diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 00941c7b..44ac399f 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -112,6 +112,8 @@ void Workers::start(xmrig::Controller *controller) { const std::vector &threads = controller->config()->threads(); + LOG_NOTICE("- %d", std::this_thread::get_id()); + size_t totalWays = 0; for (const xmrig::IThread *thread : threads) { totalWays += thread->multiway(); @@ -165,6 +167,9 @@ void Workers::submit(const JobResult &result) void Workers::onReady(void *arg) { auto handle = static_cast(arg); + + LOG_NOTICE("%zu %d", handle->threadId(), std::this_thread::get_id()); + if (Mem::isDoubleHash()) { handle->setWorker(new DoubleWorker(handle)); } @@ -175,9 +180,7 @@ void Workers::onReady(void *arg) const bool rc = handle->worker()->start(); if (!rc) { - uv_mutex_lock(&m_mutex); LOG_ERR("thread %zu error: \"hash self-test failed\".", handle->worker()->id()); - uv_mutex_unlock(&m_mutex); } } From 4b71b7aa29e6ff89e20e8679625f4599edf75b40 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 14 Apr 2018 22:14:57 +0700 Subject: [PATCH 202/389] Added class MultiWorker and remove classes SingleWorker and DoubleWorker. --- CMakeLists.txt | 6 +- src/interfaces/IWorker.h | 3 +- src/net/Job.cpp | 11 + src/net/Job.h | 4 + src/workers/DoubleWorker.cpp | 189 ------------------ src/workers/DoubleWorker.h | 59 ------ src/workers/Handle.cpp | 4 +- src/workers/Handle.h | 9 +- .../{SingleWorker.cpp => MultiWorker.cpp} | 135 +++++++------ src/workers/{SingleWorker.h => MultiWorker.h} | 34 +++- src/workers/Worker.cpp | 4 +- src/workers/Worker.h | 8 +- src/workers/Workers.cpp | 52 +++-- 13 files changed, 173 insertions(+), 345 deletions(-) delete mode 100644 src/workers/DoubleWorker.cpp delete mode 100644 src/workers/DoubleWorker.h rename src/workers/{SingleWorker.cpp => MultiWorker.cpp} (61%) rename src/workers/{SingleWorker.h => MultiWorker.h} (72%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c8d381d4..ecb65b22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,10 +55,9 @@ set(HEADERS src/Summary.h src/version.h src/workers/CpuThread.h - src/workers/DoubleWorker.h src/workers/Handle.h src/workers/Hashrate.h - src/workers/SingleWorker.h + src/workers/MultiWorker.h src/workers/Worker.h src/workers/Workers.h ) @@ -109,10 +108,9 @@ set(SOURCES src/net/SubmitResult.cpp src/Summary.cpp src/workers/CpuThread.cpp - src/workers/DoubleWorker.cpp src/workers/Handle.cpp src/workers/Hashrate.cpp - src/workers/SingleWorker.cpp + src/workers/MultiWorker.cpp src/workers/Worker.cpp src/workers/Workers.cpp src/xmrig.cpp diff --git a/src/interfaces/IWorker.h b/src/interfaces/IWorker.h index a90abe11..90394c2c 100644 --- a/src/interfaces/IWorker.h +++ b/src/interfaces/IWorker.h @@ -33,10 +33,11 @@ class IWorker public: virtual ~IWorker() {} - virtual bool start() = 0; + virtual bool selfTest() = 0; virtual size_t id() const = 0; virtual uint64_t hashCount() const = 0; virtual uint64_t timestamp() const = 0; + virtual void start() = 0; }; diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 17d8266f..1434c87f 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -209,6 +209,17 @@ void Job::toHex(const unsigned char* in, unsigned int len, char* out) } +#ifdef APP_DEBUG +char *Job::toHex(const unsigned char* in, unsigned int len) +{ + char *out = new char[len * 2 + 1](); + toHex(in, len, out); + + return out; +} +#endif + + bool Job::operator==(const Job &other) const { return m_id == other.m_id && memcmp(m_blob, other.m_blob, sizeof(m_blob)) == 0; diff --git a/src/net/Job.h b/src/net/Job.h index a60f061c..ee4728e3 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -69,6 +69,10 @@ public: static inline uint64_t toDiff(uint64_t target) { return 0xFFFFFFFFFFFFFFFFULL / target; } static void toHex(const unsigned char* in, unsigned int len, char* out); +# ifdef APP_DEBUG + static char *toHex(const unsigned char* in, unsigned int len); +# endif + bool operator==(const Job &other) const; bool operator!=(const Job &other) const; diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp deleted file mode 100644 index 6ba5c47f..00000000 --- a/src/workers/DoubleWorker.cpp +++ /dev/null @@ -1,189 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2016-2018 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include - - -#include "crypto/CryptoNight_test.h" -#include "workers/CpuThread.h" -#include "workers/DoubleWorker.h" -#include "workers/Workers.h" - - -class DoubleWorker::State -{ -public: - inline State() : - nonce1(0), - nonce2(0) - {} - - Job job; - uint32_t nonce1; - uint32_t nonce2; - uint8_t blob[84 * 2]; -}; - - -DoubleWorker::DoubleWorker(Handle *handle) - : Worker(handle) -{ - m_state = new State(); - m_pausedState = new State(); -} - - -DoubleWorker::~DoubleWorker() -{ - delete m_state; - delete m_pausedState; -} - - -bool DoubleWorker::start() -{ - if (!selfTest()) { - return false; - } - - while (Workers::sequence() > 0) { - if (Workers::isPaused()) { - do { - std::this_thread::sleep_for(std::chrono::milliseconds(200)); - } - while (Workers::isPaused()); - - if (Workers::sequence() == 0) { - break; - } - - consumeJob(); - } - - while (!Workers::isOutdated(m_sequence)) { - if ((m_count & 0xF) == 0) { - storeStats(); - } - - m_count += 2; - *Job::nonce(m_state->blob) = ++m_state->nonce1; - *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - - m_thread->fn(m_state->job.variant())(m_state->blob, m_state->job.size(), m_hash, m_ctx); - - if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { - Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); - } - - if (*reinterpret_cast(m_hash + 32 + 24) < m_state->job.target()) { - Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce2, m_hash + 32, m_state->job.diff())); - } - - std::this_thread::yield(); - } - - consumeJob(); - } - - return true; -} - - -bool DoubleWorker::resume(const Job &job) -{ - if (m_state->job.poolId() == -1 && job.poolId() >= 0 && job.id() == m_pausedState->job.id()) { - *m_state = *m_pausedState; - return true; - } - - return false; -} - - -bool DoubleWorker::selfTest() -{ - if (m_thread->fn(xmrig::VARIANT_NONE) == nullptr) { - return false; - } - - m_thread->fn(xmrig::VARIANT_NONE)(test_input, 76, m_hash, m_ctx); - - if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_hash, test_output_v0, 64) == 0) { - m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); - - return memcmp(m_hash, test_output_v1, 64) == 0; - } - -# ifndef XMRIG_NO_AEON - if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_hash, test_output_v0_lite, 64) == 0) { - m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); - - return memcmp(m_hash, test_output_v1_lite, 64) == 0; - } -# endif - -# ifndef XMRIG_NO_SUMO - return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_hash, test_output_heavy, 64) == 0; -# else - return false; -# endif -} - - -void DoubleWorker::consumeJob() -{ - Job job = Workers::job(); - m_sequence = Workers::sequence(); - if (m_state->job == job) { - return; - } - - save(job); - - if (resume(job)) { - return; - } - - m_state->job = std::move(job); - memcpy(m_state->blob, m_state->job.blob(), m_state->job.size()); - memcpy(m_state->blob + m_state->job.size(), m_state->job.blob(), m_state->job.size()); - - if (m_state->job.isNicehash()) { - m_state->nonce1 = (*Job::nonce(m_state->blob) & 0xff000000U) + (0xffffffU / m_totalWays * m_id); - m_state->nonce2 = (*Job::nonce(m_state->blob + m_state->job.size()) & 0xff000000U) + (0xffffffU / m_totalWays * (m_id + m_totalThreads)); - } - else { - m_state->nonce1 = 0xffffffffU / m_totalWays * m_id; - m_state->nonce2 = 0xffffffffU / m_totalWays * (m_id + m_totalThreads); - } -} - - -void DoubleWorker::save(const Job &job) -{ - if (job.poolId() == -1 && m_state->job.poolId() >= 0) { - *m_pausedState = *m_state; - } -} diff --git a/src/workers/DoubleWorker.h b/src/workers/DoubleWorker.h deleted file mode 100644 index e8847282..00000000 --- a/src/workers/DoubleWorker.h +++ /dev/null @@ -1,59 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2016-2018 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef __DOUBLEWORKER_H__ -#define __DOUBLEWORKER_H__ - - -#include "net/Job.h" -#include "net/JobResult.h" -#include "workers/Worker.h" - - -class Handle; - - -class DoubleWorker : public Worker -{ -public: - DoubleWorker(Handle *handle); - ~DoubleWorker(); - - bool start() override; - -private: - bool resume(const Job &job); - bool selfTest(); - void consumeJob(); - void save(const Job &job); - - class State; - - uint8_t m_hash[64]; - State *m_state; - State *m_pausedState; -}; - - -#endif /* __SINGLEWORKER_H__ */ diff --git a/src/workers/Handle.cpp b/src/workers/Handle.cpp index 29f57fb2..d42ea368 100644 --- a/src/workers/Handle.cpp +++ b/src/workers/Handle.cpp @@ -25,10 +25,10 @@ #include "workers/Handle.h" -Handle::Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays) : +Handle::Handle(xmrig::IThread *config, uint32_t offset, size_t totalWays) : m_worker(nullptr), - m_totalThreads(totalThreads), m_totalWays(totalWays), + m_offset(offset), m_config(config) { } diff --git a/src/workers/Handle.h b/src/workers/Handle.h index b3a7c76f..4bb899f9 100644 --- a/src/workers/Handle.h +++ b/src/workers/Handle.h @@ -25,6 +25,7 @@ #define __HANDLE_H__ +#include #include #include @@ -38,21 +39,21 @@ class IWorker; class Handle { public: - Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays); + Handle(xmrig::IThread *config, uint32_t offset, size_t totalWays); void join(); void start(void (*callback) (void *)); inline IWorker *worker() const { return m_worker; } inline size_t threadId() const { return m_config->index(); } - inline size_t totalThreads() const { return m_totalThreads; } inline size_t totalWays() const { return m_totalWays; } - inline void setWorker(IWorker *worker) { m_worker = worker; } + inline uint32_t offset() const { return m_offset; } + inline void setWorker(IWorker *worker) { assert(worker != nullptr); m_worker = worker; } inline xmrig::IThread *config() const { return m_config; } private: IWorker *m_worker; - size_t m_totalThreads; size_t m_totalWays; + uint32_t m_offset; uv_thread_t m_thread; xmrig::IThread *m_config; }; diff --git a/src/workers/SingleWorker.cpp b/src/workers/MultiWorker.cpp similarity index 61% rename from src/workers/SingleWorker.cpp rename to src/workers/MultiWorker.cpp index 815b965d..0007b0cb 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -28,22 +28,51 @@ #include "crypto/CryptoNight_test.h" #include "workers/CpuThread.h" -#include "workers/SingleWorker.h" +#include "workers/MultiWorker.h" #include "workers/Workers.h" -SingleWorker::SingleWorker(Handle *handle) +template +MultiWorker::MultiWorker(Handle *handle) : Worker(handle) { } -bool SingleWorker::start() +template +bool MultiWorker::selfTest() { - if (!selfTest()) { + if (m_thread->fn(xmrig::VARIANT_NONE) == nullptr) { return false; } + m_thread->fn(xmrig::VARIANT_NONE)(test_input, 76, m_result.result, m_ctxLegacy); + + if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_result.result, test_output_v0, 32) == 0) { + m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_result.result, m_ctxLegacy); + + return memcmp(m_result.result, test_output_v1, 32) == 0; + } + +# ifndef XMRIG_NO_AEON + if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_result.result, test_output_v0_lite, 32) == 0) { + m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_result.result, m_ctxLegacy); + + return memcmp(m_result.result, test_output_v1_lite, 32) == 0; + } +# endif + +# ifndef XMRIG_NO_SUMO + return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_result.result, test_output_heavy, 32) == 0; +# else + return false; +# endif +} + + +template +void MultiWorker::start() +{ while (Workers::sequence() > 0) { if (Workers::isPaused()) { do { @@ -59,34 +88,35 @@ bool SingleWorker::start() } while (!Workers::isOutdated(m_sequence)) { - if ((m_count & 0xF) == 0) { + if ((m_count & 0x7) == 0) { storeStats(); } - m_count++; - *m_job.nonce() = ++m_result.nonce; + m_thread->fn(m_state.job.variant())(m_state.blob, m_state.job.size(), m_hash, m_ctxLegacy); - m_thread->fn(m_job.variant())(m_job.blob(), m_job.size(), m_result.result, m_ctx); - if (*reinterpret_cast(m_result.result + 24) < m_job.target()) { - Workers::submit(m_result); + for (size_t i = 0; i < N; ++i) { + if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) { + Workers::submit(JobResult(m_state.job.poolId(), m_state.job.id(), *nonce(i), m_hash, m_state.job.diff())); + } + + *nonce(i) += 1; } + m_count += N; + std::this_thread::yield(); } consumeJob(); } - - return true; } -bool SingleWorker::resume(const Job &job) +template +bool MultiWorker::resume(const Job &job) { - if (m_job.poolId() == -1 && job.poolId() >= 0 && job.id() == m_paused.id()) { - m_job = m_paused; - m_result = m_job; - m_result.nonce = *m_job.nonce(); + if (m_state.job.poolId() == -1 && job.poolId() >= 0 && job.id() == m_pausedState.job.id()) { + m_state = m_pausedState; return true; } @@ -94,41 +124,12 @@ bool SingleWorker::resume(const Job &job) } -bool SingleWorker::selfTest() -{ - if (m_thread->fn(xmrig::VARIANT_NONE) == nullptr) { - return false; - } - - m_thread->fn(xmrig::VARIANT_NONE)(test_input, 76, m_result.result, m_ctx); - - if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_result.result, test_output_v0, 32) == 0) { - m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_result.result, m_ctx); - - return memcmp(m_result.result, test_output_v1, 32) == 0; - } - -# ifndef XMRIG_NO_AEON - if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_result.result, test_output_v0_lite, 32) == 0) { - m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_result.result, m_ctx); - - return memcmp(m_result.result, test_output_v1_lite, 32) == 0; - } -# endif - -# ifndef XMRIG_NO_SUMO - return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_result.result, test_output_heavy, 32) == 0; -# else - return false; -# endif -} - - -void SingleWorker::consumeJob() +template +void MultiWorker::consumeJob() { Job job = Workers::job(); m_sequence = Workers::sequence(); - if (m_job == job) { + if (m_state.job == job) { return; } @@ -138,21 +139,39 @@ void SingleWorker::consumeJob() return; } - m_job = std::move(job); - m_result = m_job; + m_state.job = job; - if (m_job.isNicehash()) { - m_result.nonce = (*m_job.nonce() & 0xff000000U) + (0xffffffU / m_totalWays * m_id); + const size_t size = m_state.job.size(); + memcpy(m_state.blob, m_state.job.blob(), m_state.job.size()); + + if (N > 1) { + for (size_t i = 1; i < N; ++i) { + memcpy(m_state.blob + (i * size), m_state.blob, size); + } } - else { - m_result.nonce = 0xffffffffU / m_totalWays * m_id; + + for (size_t i = 0; i < N; ++i) { + if (m_state.job.isNicehash()) { + *nonce(i) = (*nonce(i) & 0xff000000U) + (0xffffffU / m_totalWays * (m_offset + i)); + } + else { + *nonce(i) = 0xffffffffU / m_totalWays * (m_offset + i); + } } } -void SingleWorker::save(const Job &job) +template +void MultiWorker::save(const Job &job) { - if (job.poolId() == -1 && m_job.poolId() >= 0) { - m_paused = m_job; + if (job.poolId() == -1 && m_state.job.poolId() >= 0) { + m_pausedState = m_state; } } + + +template class MultiWorker<1>; +template class MultiWorker<2>; +template class MultiWorker<3>; +template class MultiWorker<4>; +template class MultiWorker<5>; diff --git a/src/workers/SingleWorker.h b/src/workers/MultiWorker.h similarity index 72% rename from src/workers/SingleWorker.h rename to src/workers/MultiWorker.h index 061f5084..d384b0ba 100644 --- a/src/workers/SingleWorker.h +++ b/src/workers/MultiWorker.h @@ -22,8 +22,8 @@ * along with this program. If not, see . */ -#ifndef __SINGLEWORKER_H__ -#define __SINGLEWORKER_H__ +#ifndef __MULTIWORKER_H__ +#define __MULTIWORKER_H__ #include "net/Job.h" @@ -34,23 +34,43 @@ class Handle; -class SingleWorker : public Worker +template +class MultiWorker : public Worker { public: - SingleWorker(Handle *handle); + MultiWorker(Handle *handle); - bool start() override; +protected: + bool selfTest() override; + void start() override; private: bool resume(const Job &job); - bool selfTest(); void consumeJob(); void save(const Job &job); + inline uint32_t *nonce(size_t index) + { + return reinterpret_cast(m_state.blob + (index * m_state.job.size()) + 39); + } + + struct State + { + alignas(16) uint8_t blob[96 * N]; + Job job; + }; + + +// cryptonight_ctx *m_ctx[N]; + + uint8_t m_hash[N * 32]; + State m_state; + State m_pausedState; + Job m_job; Job m_paused; JobResult m_result; }; -#endif /* __SINGLEWORKER_H__ */ +#endif /* __MULTIWORKER_H__ */ diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index 59434390..e0ed846b 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -34,8 +34,8 @@ Worker::Worker(Handle *handle) : m_id(handle->threadId()), - m_totalThreads(handle->totalThreads()), m_totalWays(handle->totalWays()), + m_offset(handle->offset()), m_hashCount(0), m_timestamp(0), m_count(0), @@ -47,7 +47,7 @@ Worker::Worker(Handle *handle) : } Platform::setThreadPriority(m_thread->priority()); - m_ctx = Mem::create(m_id); + m_ctxLegacy = Mem::create(m_id); } diff --git a/src/workers/Worker.h b/src/workers/Worker.h index 88f6ee42..72b3beb2 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -54,10 +54,10 @@ public: protected: void storeStats(); - cryptonight_ctx *m_ctx; - size_t m_id; - size_t m_totalThreads; - size_t m_totalWays; + const size_t m_id; + const size_t m_totalWays; + const uint32_t m_offset; + cryptonight_ctx *m_ctxLegacy; std::atomic m_hashCount; std::atomic m_timestamp; uint64_t m_count; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 44ac399f..21379db4 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -30,15 +30,13 @@ #include "core/Controller.h" #include "interfaces/IJobResultListener.h" #include "interfaces/IThread.h" +#include "log/Log.h" #include "Mem.h" -#include "workers/DoubleWorker.h" #include "workers/Handle.h" #include "workers/Hashrate.h" -#include "workers/SingleWorker.h" +#include "workers/MultiWorker.h" #include "workers/Workers.h" -#include "log/Log.h" - bool Workers::m_active = false; bool Workers::m_enabled = true; @@ -112,8 +110,6 @@ void Workers::start(xmrig::Controller *controller) { const std::vector &threads = controller->config()->threads(); - LOG_NOTICE("- %d", std::this_thread::get_id()); - size_t totalWays = 0; for (const xmrig::IThread *thread : threads) { totalWays += thread->multiway(); @@ -131,8 +127,12 @@ void Workers::start(xmrig::Controller *controller) uv_timer_init(uv_default_loop(), &m_timer); uv_timer_start(&m_timer, Workers::onTick, 500, 500); + uint32_t offset = 0; + for (xmrig::IThread *thread : threads) { - Handle *handle = new Handle(thread, threads.size(), totalWays); + Handle *handle = new Handle(thread, offset, totalWays); + offset += thread->multiway(); + m_workers.push_back(handle); handle->start(Workers::onReady); } @@ -168,20 +168,42 @@ void Workers::onReady(void *arg) { auto handle = static_cast(arg); - LOG_NOTICE("%zu %d", handle->threadId(), std::this_thread::get_id()); + IWorker *worker = nullptr; - if (Mem::isDoubleHash()) { - handle->setWorker(new DoubleWorker(handle)); - } - else { - handle->setWorker(new SingleWorker(handle)); + switch (handle->config()->multiway()) { + case 1: + worker = new MultiWorker<1>(handle); + break; + + case 2: + worker = new MultiWorker<2>(handle); + break; + + case 3: + worker = new MultiWorker<3>(handle); + break; + + case 4: + worker = new MultiWorker<4>(handle); + break; + + case 5: + worker = new MultiWorker<5>(handle); + break; + + default: + break; } - const bool rc = handle->worker()->start(); + handle->setWorker(worker); - if (!rc) { + if (!worker->selfTest()) { LOG_ERR("thread %zu error: \"hash self-test failed\".", handle->worker()->id()); + + return; } + + worker->start(); } From 9125b6c2512bb5539424894c766ef178ded93fbd Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 15 Apr 2018 11:08:47 +0700 Subject: [PATCH 203/389] Rewrite memory allocation. --- CMakeLists.txt | 1 + src/App.cpp | 8 +--- src/Mem.cpp | 79 ++++++++++++--------------------- src/Mem.h | 35 +++++++-------- src/Mem_unix.cpp | 60 ++++++++++--------------- src/Mem_win.cpp | 66 +++++++++++++-------------- src/Summary.cpp | 9 ++-- src/api/ApiRouter.cpp | 2 +- src/common/utils/mm_malloc.h | 43 ++++++++++++++++++ src/crypto/CryptoNight.h | 3 +- src/crypto/CryptoNight_arm.h | 8 +--- src/crypto/CryptoNight_monero.h | 2 +- src/crypto/CryptoNight_x86.h | 38 ++++++++-------- src/interfaces/IThread.h | 20 ++++++--- src/workers/CpuThread.h | 13 +----- src/workers/MultiWorker.cpp | 26 +++++++---- src/workers/MultiWorker.h | 14 +++--- src/workers/Worker.cpp | 7 --- src/workers/Worker.h | 2 - 19 files changed, 212 insertions(+), 224 deletions(-) create mode 100644 src/common/utils/mm_malloc.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ecb65b22..c7017b01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(HEADERS src/common/config/ConfigWatcher.h src/common/Platform.h src/common/utils/c_str.h + src/common/utils/mm_malloc.h src/common/xmrig.h src/Console.h src/core/Config.cpp diff --git a/src/App.cpp b/src/App.cpp index 3d0cb3df..78d6b328 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -76,8 +76,6 @@ App::App(int argc, char **argv) : App::~App() { - Mem::release(); - uv_tty_reset_mode(); delete m_console; @@ -101,11 +99,7 @@ int App::exec() background(); - Mem::allocate(m_controller->config()->algorithm(), - m_controller->config()->threadsCount(), - m_controller->config()->isDoubleHash(), - m_controller->config()->isHugePages() - ); + Mem::init(m_controller->config()->isHugePages()); Summary::print(m_controller); diff --git a/src/Mem.cpp b/src/Mem.cpp index 9de79dbd..2efaac8a 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -23,70 +23,49 @@ */ -#include - - +#include "common/utils/mm_malloc.h" #include "crypto/CryptoNight.h" #include "crypto/CryptoNight_constants.h" #include "Mem.h" -bool Mem::m_doubleHash = false; -int Mem::m_algo = 0; -int Mem::m_flags = 0; -int Mem::m_threads = 0; -size_t Mem::m_offset = 0; -size_t Mem::m_size = 0; -alignas(16) uint8_t *Mem::m_memory = nullptr; +bool Mem::m_enabled = true; +int Mem::m_flags = 0; -cryptonight_ctx *Mem::create(int threadId) + +MemInfo Mem::create(cryptonight_ctx **ctx, xmrig::Algo algorithm, size_t count) { + using namespace xmrig; + + MemInfo info; + info.size = cn_select_memory(algorithm) * count; + # ifndef XMRIG_NO_AEON - if (m_algo == xmrig::CRYPTONIGHT_LITE) { - return createLite(threadId); - } + info.size += info.size % cn_select_memory(); # endif - const size_t size = m_algo == xmrig::CRYPTONIGHT_HEAVY ? xmrig::cn_select_memory() - : xmrig::cn_select_memory(); + info.pages = info.size / cn_select_memory(); - cryptonight_ctx *ctx = reinterpret_cast(&m_memory[size - sizeof(cryptonight_ctx) * (threadId + 1)]); + allocate(info, m_enabled); - const int ratio = m_doubleHash ? 2 : 1; - ctx->memory = &m_memory[size * (threadId * ratio + 1)]; + for (size_t i = 0; i < count; ++i) { + cryptonight_ctx *c = static_cast(_mm_malloc(sizeof(cryptonight_ctx), 16)); + c->memory = info.memory + (i * cn_select_memory(algorithm)); - return ctx; -} - - - -void *Mem::calloc(size_t num, size_t size) -{ - void *mem = &m_memory[m_offset]; - m_offset += (num * size); - - memset(mem, 0, num * size); - - return mem; -} - - -#ifndef XMRIG_NO_AEON -cryptonight_ctx *Mem::createLite(int threadId) { - cryptonight_ctx *ctx; - - if (!m_doubleHash) { - const size_t offset = MONERO_MEMORY * (threadId + 1); - - ctx = reinterpret_cast(&m_memory[offset + AEON_MEMORY]); - ctx->memory = &m_memory[offset]; - return ctx; + ctx[i] = c; } - ctx = reinterpret_cast(&m_memory[MONERO_MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); - ctx->memory = &m_memory[MONERO_MEMORY * (threadId + 1)]; - - return ctx; + return info; } -#endif + + +void Mem::release(cryptonight_ctx **ctx, size_t count, MemInfo &info) +{ + release(info); + + for (size_t i = 0; i < count; ++i) { + _mm_free(ctx[i]); + } +} + diff --git a/src/Mem.h b/src/Mem.h index 06b470c8..6fd18fc1 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -36,6 +36,16 @@ struct cryptonight_ctx; +struct MemInfo +{ + alignas(16) uint8_t *memory; + + size_t hugePages; + size_t pages; + size_t size; +}; + + class Mem { public: @@ -45,29 +55,18 @@ public: Lock = 4 }; - static bool allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled); - static cryptonight_ctx *create(int threadId); - static void *calloc(size_t num, size_t size); - static void release(); + static MemInfo create(cryptonight_ctx **ctx, xmrig::Algo algorithm, size_t count); + static void init(bool enabled); + static void release(cryptonight_ctx **ctx, size_t count, MemInfo &info); - static inline bool isDoubleHash() { return m_doubleHash; } static inline bool isHugepagesAvailable() { return (m_flags & HugepagesAvailable) != 0; } - static inline bool isHugepagesEnabled() { return (m_flags & HugepagesEnabled) != 0; } - static inline int flags() { return m_flags; } - static inline int threads() { return m_threads; } private: - static bool m_doubleHash; - static int m_algo; - static int m_flags; - static int m_threads; - static size_t m_offset; - static size_t m_size; - alignas(16) static uint8_t *m_memory; + static void allocate(MemInfo &info, bool enabled); + static void release(MemInfo &info); -# ifndef XMRIG_NO_AEON - static cryptonight_ctx *createLite(int threadId); -# endif + static int m_flags; + static bool m_enabled; }; diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index df7aaad2..550033c4 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -27,75 +27,63 @@ #include -#if defined(XMRIG_ARM) && !defined(__clang__) -# include "aligned_malloc.h" -#else -# include -#endif - - +#include "common/utils/mm_malloc.h" #include "common/xmrig.h" #include "crypto/CryptoNight.h" #include "log/Log.h" #include "Mem.h" -bool Mem::allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled) +void Mem::init(bool enabled) { - m_algo = algo; - m_threads = threads; - m_doubleHash = doubleHash; + m_enabled = enabled; +} - const int ratio = (doubleHash && algo != xmrig::CRYPTONIGHT_LITE) ? 2 : 1; - m_size = MONERO_MEMORY * (threads * ratio + 1); - if (algo == xmrig::CRYPTONIGHT_HEAVY) { - m_size *= 2; - } +void Mem::allocate(MemInfo &info, bool enabled) +{ + info.hugePages = 0; if (!enabled) { - m_memory = static_cast(_mm_malloc(m_size, 16)); - return true; - } + info.memory = static_cast(_mm_malloc(info.size, 16)); - m_flags |= HugepagesAvailable; + return; + } # if defined(__APPLE__) - m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); + info.memory = static_cast(mmap(0, info.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); # elif defined(__FreeBSD__) - m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); + info.memory = static_cast(mmap(0, info.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); # else - m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); + info.memory = static_cast(mmap(0, info.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); # endif - if (m_memory == MAP_FAILED) { - m_memory = static_cast(_mm_malloc(m_size, 16)); - return true; + + if (info.memory == MAP_FAILED) { + return allocate(info, false);; } - m_flags |= HugepagesEnabled; + info.hugePages = info.pages; - if (madvise(m_memory, m_size, MADV_RANDOM | MADV_WILLNEED) != 0) { + if (madvise(info.memory, info.size, MADV_RANDOM | MADV_WILLNEED) != 0) { LOG_ERR("madvise failed"); } - if (mlock(m_memory, m_size) == 0) { + if (mlock(info.memory, info.size) == 0) { m_flags |= Lock; } - - return true; } -void Mem::release() +void Mem::release(MemInfo &info) { - if (m_flags & HugepagesEnabled) { + if (info.hugePages) { if (m_flags & Lock) { - munlock(m_memory, m_size); + munlock(info.memory, info.size); } - munmap(m_memory, m_size); + munmap(info.memory, info.size); } else { - _mm_free(m_memory); + _mm_free(info.memory); } } diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 1f3066ea..d0b698f6 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -28,14 +28,11 @@ #include #include -#ifdef __GNUC__ -# include -#else -# include -#endif +#include "common/utils/mm_malloc.h" #include "common/xmrig.h" #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_constants.h" #include "log/Log.h" #include "Mem.h" @@ -145,46 +142,43 @@ static BOOL TrySetLockPagesPrivilege() { } -bool Mem::allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled) +void Mem::init(bool enabled) { - m_algo = algo; - m_threads = threads; - m_doubleHash = doubleHash; + m_enabled = enabled; - const int ratio = (doubleHash && algo != xmrig::CRYPTONIGHT_LITE) ? 2 : 1; - m_size = MONERO_MEMORY * (threads * ratio + 1); - - if (algo == xmrig::CRYPTONIGHT_HEAVY) { - m_size *= 2; - } - - if (!enabled) { - m_memory = static_cast(_mm_malloc(m_size, 16)); - return true; - } - - if (TrySetLockPagesPrivilege()) { + if (enabled && TrySetLockPagesPrivilege()) { m_flags |= HugepagesAvailable; } - - m_memory = static_cast(VirtualAlloc(NULL, m_size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); - if (!m_memory) { - m_memory = static_cast(_mm_malloc(m_size, 16)); - } - else { - m_flags |= HugepagesEnabled; - } - - return true; } -void Mem::release() +void Mem::allocate(MemInfo &info, bool enabled) { - if (m_flags & HugepagesEnabled) { - VirtualFree(m_memory, 0, MEM_RELEASE); + info.hugePages = 0; + + if (!enabled) { + info.memory = static_cast(_mm_malloc(info.size, 16)); + + return; + } + + info.memory = static_cast(VirtualAlloc(nullptr, info.size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); + if (info.memory) { + info.hugePages = info.pages; + + return; + } + + allocate(info, false); +} + + +void Mem::release(MemInfo &info) +{ + if (info.hugePages) { + VirtualFree(info.memory, 0, MEM_RELEASE); } else { - _mm_free(m_memory); + _mm_free(info.memory); } } diff --git a/src/Summary.cpp b/src/Summary.cpp index 5acda5c2..a54214e4 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -58,14 +58,15 @@ static void print_versions(xmrig::Config *config) static void print_memory(xmrig::Config *config) { +# ifdef _WIN32 if (config->isColors()) { - Log::i()->text("\x1B[01;32m * \x1B[01;37mHUGE PAGES: %s, %s", - Mem::isHugepagesAvailable() ? "\x1B[01;32mavailable" : "\x1B[01;31munavailable", - Mem::isHugepagesEnabled() ? "\x1B[01;32menabled" : "\x1B[01;31mdisabled"); + Log::i()->text("\x1B[01;32m * \x1B[01;37mHUGE PAGES: %s", + Mem::isHugepagesAvailable() ? "\x1B[01;32mavailable" : "\x1B[01;31munavailable"); } else { - Log::i()->text(" * HUGE PAGES: %s, %s", Mem::isHugepagesAvailable() ? "available" : "unavailable", Mem::isHugepagesEnabled() ? "enabled" : "disabled"); + Log::i()->text(" * HUGE PAGES: %s", Mem::isHugepagesAvailable() ? "available" : "unavailable"); } +# endif } diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index cb7a8005..0893bc8b 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -267,7 +267,7 @@ void ApiRouter::getMiner(rapidjson::Document &doc) const doc.AddMember("ua", rapidjson::StringRef(Platform::userAgent()), allocator); doc.AddMember("cpu", cpu, allocator); doc.AddMember("algo", rapidjson::StringRef(m_controller->config()->algoName()), allocator); - doc.AddMember("hugepages", Mem::isHugepagesEnabled(), allocator); + doc.AddMember("hugepages", false, allocator); doc.AddMember("donate_level", m_controller->config()->donateLevel(), allocator); } diff --git a/src/common/utils/mm_malloc.h b/src/common/utils/mm_malloc.h new file mode 100644 index 00000000..30c721a3 --- /dev/null +++ b/src/common/utils/mm_malloc.h @@ -0,0 +1,43 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __MM_MALLOC_PORTABLE_H__ +#define __MM_MALLOC_PORTABLE_H__ + + +#ifdef _WIN32 +# ifdef __GNUC__ +# include +# else +# include +# endif +#else +# if defined(XMRIG_ARM) && !defined(__clang__) +# include "aligned_malloc.h" +# else +# include +# endif +#endif + + +#endif /* __MM_MALLOC_PORTABLE_H__ */ diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index d0d61ae3..5a4a266d 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -40,8 +40,7 @@ struct cryptonight_ctx { - alignas(16) uint8_t state0[200]; - alignas(16) uint8_t state1[200]; + alignas(16) uint8_t state[200]; alignas(16) uint8_t* memory; }; diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index fd8b58ff..d9677d06 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -27,13 +27,7 @@ #define __CRYPTONIGHT_ARM_H__ -#if defined(XMRIG_ARM) && !defined(__clang__) -# include "aligned_malloc.h" -#else -# include -#endif - - +#include "common/utils/mm_malloc.h" #include "crypto/CryptoNight.h" #include "crypto/CryptoNight_constants.h" #include "crypto/CryptoNight_monero.h" diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index a667a3b3..a34f3ba8 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -32,7 +32,7 @@ uint64_t tweak1_2_##part = 0; \ if (VARIANT > 0) { \ tweak1_2_##part = (*reinterpret_cast(input + 35 + part * size) ^ \ - *(reinterpret_cast(ctx->state##part) + 24)); \ + *(reinterpret_cast(ctx[part]->state) + 24)); \ } #else # define VARIANT1_INIT(part) \ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 417404a6..824dd99c 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -403,7 +403,7 @@ static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) template -inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); @@ -414,14 +414,14 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si return; } - keccak(input, (int) size, ctx->state0, 200); + keccak(input, (int) size, ctx[0]->state, 200); VARIANT1_INIT(0) - cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); + cn_explode_scratchpad((__m128i*) ctx[0]->state, (__m128i*) ctx[0]->memory); - const uint8_t* l0 = ctx->memory; - uint64_t* h0 = reinterpret_cast(ctx->state0); + const uint8_t* l0 = ctx[0]->memory; + uint64_t* h0 = reinterpret_cast(ctx[0]->state); uint64_t al0 = h0[0] ^ h0[4]; uint64_t ah0 = h0[1] ^ h0[5]; @@ -476,15 +476,15 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } } - cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); + cn_implode_scratchpad((__m128i*) ctx[0]->memory, (__m128i*) ctx[0]->state); keccakf(h0, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); + extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); } template -inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); @@ -495,16 +495,16 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si return; } - keccak(input, (int) size, ctx->state0, 200); - keccak(input + size, (int) size, ctx->state1, 200); + keccak(input, (int) size, ctx[0]->state, 200); + keccak(input + size, (int) size, ctx[1]->state, 200); VARIANT1_INIT(0); VARIANT1_INIT(1); - const uint8_t* l0 = ctx->memory; - const uint8_t* l1 = ctx->memory + MEM; - uint64_t* h0 = reinterpret_cast(ctx->state0); - uint64_t* h1 = reinterpret_cast(ctx->state1); + const uint8_t* l0 = ctx[0]->memory; + const uint8_t* l1 = ctx[1]->memory; + uint64_t* h0 = reinterpret_cast(ctx[0]->state); + uint64_t* h1 = reinterpret_cast(ctx[1]->state); cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); @@ -606,25 +606,25 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si keccakf(h0, 24); keccakf(h1, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); - extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, output + 32); + extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); + extra_hashes[ctx[1]->state[0] & 3](ctx[1]->state, 200, output + 32); } template -inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { } template -inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { } template -inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { } diff --git a/src/interfaces/IThread.h b/src/interfaces/IThread.h index 5b3efa0d..f517ed18 100644 --- a/src/interfaces/IThread.h +++ b/src/interfaces/IThread.h @@ -43,14 +43,22 @@ public: CUDA }; + enum Multiway { + SingleWay = 1, + DoubleWay, + TripleWay, + QuadWay, + PentaWay + }; + virtual ~IThread() {} - virtual Algo algorithm() const = 0; - virtual int multiway() const = 0; - virtual int priority() const = 0; - virtual int64_t affinity() const = 0; - virtual size_t index() const = 0; - virtual Type type() const = 0; + virtual Algo algorithm() const = 0; + virtual int priority() const = 0; + virtual int64_t affinity() const = 0; + virtual Multiway multiway() const = 0; + virtual size_t index() const = 0; + virtual Type type() const = 0; # ifndef XMRIG_NO_API virtual rapidjson::Value toAPI(rapidjson::Document &doc) const = 0; diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index 9d1bc4e7..e14c79f1 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -38,15 +38,6 @@ namespace xmrig { class CpuThread : public IThread { public: - enum Multiway { - SingleWay = 1, - DoubleWay, - TripleWay, - QuadWay, - PentaWay - }; - - struct Data { inline Data() : valid(false), affinity(-1L), multiway(SingleWay) {} @@ -68,7 +59,7 @@ public: CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch); ~CpuThread(); - typedef void (*cn_hash_fun)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx); + typedef void (*cn_hash_fun)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx **ctx); static cn_hash_fun fn(Algo algorithm, AlgoVariant av, Variant variant); static CpuThread *createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority); @@ -80,9 +71,9 @@ public: inline cn_hash_fun fn(Variant variant) const { return fn(m_algorithm, m_av, variant); } inline Algo algorithm() const override { return m_algorithm; } - inline int multiway() const override { return m_multiway; } inline int priority() const override { return m_priority; } inline int64_t affinity() const override { return m_affinity; } + inline Multiway multiway() const override { return m_multiway; } inline size_t index() const override { return m_index; } inline Type type() const override { return CPU; } diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 0007b0cb..a75adecc 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -36,6 +36,14 @@ template MultiWorker::MultiWorker(Handle *handle) : Worker(handle) { + m_memory = Mem::create(m_ctx, m_thread->algorithm(), N); +} + + +template +MultiWorker::~MultiWorker() +{ + Mem::release(m_ctx, N, m_memory); } @@ -46,24 +54,24 @@ bool MultiWorker::selfTest() return false; } - m_thread->fn(xmrig::VARIANT_NONE)(test_input, 76, m_result.result, m_ctxLegacy); + m_thread->fn(xmrig::VARIANT_NONE)(test_input, 76, m_hash, m_ctx); - if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_result.result, test_output_v0, 32) == 0) { - m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_result.result, m_ctxLegacy); + if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_hash, test_output_v0, sizeof m_hash) == 0) { + m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); - return memcmp(m_result.result, test_output_v1, 32) == 0; + return memcmp(m_hash, test_output_v1, sizeof m_hash) == 0; } # ifndef XMRIG_NO_AEON - if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_result.result, test_output_v0_lite, 32) == 0) { - m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_result.result, m_ctxLegacy); + if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_hash, test_output_v0_lite, sizeof m_hash) == 0) { + m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); - return memcmp(m_result.result, test_output_v1_lite, 32) == 0; + return memcmp(m_hash, test_output_v1_lite, sizeof m_hash) == 0; } # endif # ifndef XMRIG_NO_SUMO - return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_result.result, test_output_heavy, 32) == 0; + return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_hash, test_output_heavy, sizeof m_hash) == 0; # else return false; # endif @@ -92,7 +100,7 @@ void MultiWorker::start() storeStats(); } - m_thread->fn(m_state.job.variant())(m_state.blob, m_state.job.size(), m_hash, m_ctxLegacy); + m_thread->fn(m_state.job.variant())(m_state.blob, m_state.job.size(), m_hash, m_ctx); for (size_t i = 0; i < N; ++i) { if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) { diff --git a/src/workers/MultiWorker.h b/src/workers/MultiWorker.h index d384b0ba..ba57f81e 100644 --- a/src/workers/MultiWorker.h +++ b/src/workers/MultiWorker.h @@ -26,6 +26,7 @@ #define __MULTIWORKER_H__ +#include "Mem.h" #include "net/Job.h" #include "net/JobResult.h" #include "workers/Worker.h" @@ -39,6 +40,7 @@ class MultiWorker : public Worker { public: MultiWorker(Handle *handle); + ~MultiWorker(); protected: bool selfTest() override; @@ -61,15 +63,11 @@ private: }; -// cryptonight_ctx *m_ctx[N]; - - uint8_t m_hash[N * 32]; - State m_state; + cryptonight_ctx *m_ctx[N]; + MemInfo m_memory; State m_pausedState; - - Job m_job; - Job m_paused; - JobResult m_result; + State m_state; + uint8_t m_hash[N * 32]; }; diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index e0ed846b..567b3e08 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -26,7 +26,6 @@ #include "common/Platform.h" #include "Cpu.h" -#include "Mem.h" #include "workers/CpuThread.h" #include "workers/Handle.h" #include "workers/Worker.h" @@ -47,12 +46,6 @@ Worker::Worker(Handle *handle) : } Platform::setThreadPriority(m_thread->priority()); - m_ctxLegacy = Mem::create(m_id); -} - - -Worker::~Worker() -{ } diff --git a/src/workers/Worker.h b/src/workers/Worker.h index 72b3beb2..0ae30530 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -45,7 +45,6 @@ class Worker : public IWorker { public: Worker(Handle *handle); - ~Worker(); inline size_t id() const override { return m_id; } inline uint64_t hashCount() const override { return m_hashCount.load(std::memory_order_relaxed); } @@ -57,7 +56,6 @@ protected: const size_t m_id; const size_t m_totalWays; const uint32_t m_offset; - cryptonight_ctx *m_ctxLegacy; std::atomic m_hashCount; std::atomic m_timestamp; uint64_t m_count; From 8716f362f893cd7a40c3000d6c1ef23bdb7b7208 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 15 Apr 2018 11:36:48 +0700 Subject: [PATCH 204/389] Fixed HW AES detection. --- src/core/Config.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index ddce6980..1eb8e6e5 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -157,7 +157,7 @@ bool xmrig::Config::adjust() } if (m_aesMode == AES_AUTO) { - m_aesMode = Cpu::hasAES() ? AES_SOFT : AES_SOFT; + m_aesMode = Cpu::hasAES() ? AES_HW : AES_SOFT; } if (!m_threads.cpu.empty()) { From 6b4f2d0a9137528b359506808430caef921a2efa Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 15 Apr 2018 12:58:17 +0700 Subject: [PATCH 205/389] Fixed ARM build. --- src/Mem.cpp | 2 +- src/Mem_unix.cpp | 2 +- src/Mem_win.cpp | 2 +- src/crypto/CryptoNight_arm.h | 38 ++++++++++++++++----------------- src/crypto/CryptoNight_monero.h | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Mem.cpp b/src/Mem.cpp index 2efaac8a..bb2da646 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -50,7 +50,7 @@ MemInfo Mem::create(cryptonight_ctx **ctx, xmrig::Algo algorithm, size_t count) allocate(info, m_enabled); for (size_t i = 0; i < count; ++i) { - cryptonight_ctx *c = static_cast(_mm_malloc(sizeof(cryptonight_ctx), 16)); + cryptonight_ctx *c = static_cast(_mm_malloc(sizeof(cryptonight_ctx), 4096)); c->memory = info.memory + (i * cn_select_memory(algorithm)); ctx[i] = c; diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 550033c4..229a0d46 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -45,7 +45,7 @@ void Mem::allocate(MemInfo &info, bool enabled) info.hugePages = 0; if (!enabled) { - info.memory = static_cast(_mm_malloc(info.size, 16)); + info.memory = static_cast(_mm_malloc(info.size, 4096)); return; } diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index d0b698f6..9a81b039 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -157,7 +157,7 @@ void Mem::allocate(MemInfo &info, bool enabled) info.hugePages = 0; if (!enabled) { - info.memory = static_cast(_mm_malloc(info.size, 16)); + info.memory = static_cast(_mm_malloc(info.size, 4096)); return; } diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index d9677d06..101a1f58 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -455,7 +455,7 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) template -inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); @@ -466,14 +466,14 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si return; } - keccak(input, (int) size, ctx->state0, 200); + keccak(input, (int) size, ctx[0]->state, 200); VARIANT1_INIT(0); - cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); + cn_explode_scratchpad((__m128i*) ctx[0]->state, (__m128i*) ctx[0]->memory); - const uint8_t* l0 = ctx->memory; - uint64_t* h0 = reinterpret_cast(ctx->state0); + const uint8_t* l0 = ctx[0]->memory; + uint64_t* h0 = reinterpret_cast(ctx[0]->state); uint64_t al0 = h0[0] ^ h0[4]; uint64_t ah0 = h0[1] ^ h0[5]; @@ -526,15 +526,15 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } } - cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); + cn_implode_scratchpad((__m128i*) ctx[0]->memory, (__m128i*) ctx[0]->state); keccakf(h0, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); + extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); } template -inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); @@ -545,16 +545,16 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si return; } - keccak(input, (int) size, ctx->state0, 200); - keccak(input + size, (int) size, ctx->state1, 200); + keccak(input, (int) size, ctx[0]->state, 200); + keccak(input + size, (int) size, ctx[1]->state, 200); VARIANT1_INIT(0); VARIANT1_INIT(1); - const uint8_t* l0 = ctx->memory; - const uint8_t* l1 = ctx->memory + MEM; - uint64_t* h0 = reinterpret_cast(ctx->state0); - uint64_t* h1 = reinterpret_cast(ctx->state1); + const uint8_t* l0 = ctx[0]->memory; + const uint8_t* l1 = ctx[1]->memory; + uint64_t* h0 = reinterpret_cast(ctx[0]->state); + uint64_t* h1 = reinterpret_cast(ctx[1]->state); cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); @@ -655,25 +655,25 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si keccakf(h0, 24); keccakf(h1, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); - extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, output + 32); + extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); + extra_hashes[ctx[1]->state[0] & 3](ctx[1]->state, 200, output + 32); } template -inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx **__restrict__ ctx) { } template -inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx **__restrict__ ctx) { } template -inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx **__restrict__ ctx) { } diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index a34f3ba8..ea1622ab 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -39,7 +39,7 @@ uint64_t tweak1_2_##part = 0; \ if (VARIANT > 0) { \ volatile const uint64_t a = *reinterpret_cast(input + 35 + part * size); \ - volatile const uint64_t b = *(reinterpret_cast(ctx->state##part) + 24); \ + volatile const uint64_t b = *(reinterpret_cast(ctx[part]->state) + 24); \ tweak1_2_##part = a ^ b; \ } #endif From e2d85d78a7e89ad062f68706a93a6aa3367cb4c6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 15 Apr 2018 14:49:39 +0700 Subject: [PATCH 206/389] Added information about started threads. --- src/workers/MultiWorker.h | 1 - src/workers/Worker.h | 3 +++ src/workers/Workers.cpp | 41 +++++++++++++++++++++++++++++++++++---- src/workers/Workers.h | 25 ++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/workers/MultiWorker.h b/src/workers/MultiWorker.h index ba57f81e..2c231e8d 100644 --- a/src/workers/MultiWorker.h +++ b/src/workers/MultiWorker.h @@ -64,7 +64,6 @@ private: cryptonight_ctx *m_ctx[N]; - MemInfo m_memory; State m_pausedState; State m_state; uint8_t m_hash[N * 32]; diff --git a/src/workers/Worker.h b/src/workers/Worker.h index 0ae30530..aad9e3c5 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -30,6 +30,7 @@ #include "interfaces/IWorker.h" +#include "Mem.h" struct cryptonight_ctx; @@ -46,6 +47,7 @@ class Worker : public IWorker public: Worker(Handle *handle); + inline const MemInfo &memory() const { return m_memory; } inline size_t id() const override { return m_id; } inline uint64_t hashCount() const override { return m_hashCount.load(std::memory_order_relaxed); } inline uint64_t timestamp() const override { return m_timestamp.load(std::memory_order_relaxed); } @@ -56,6 +58,7 @@ protected: const size_t m_id; const size_t m_totalWays; const uint32_t m_offset; + MemInfo m_memory; std::atomic m_hashCount; std::atomic m_timestamp; uint64_t m_count; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 21379db4..6d6deb3b 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -43,6 +43,7 @@ bool Workers::m_enabled = true; Hashrate *Workers::m_hashrate = nullptr; IJobResultListener *Workers::m_listener = nullptr; Job Workers::m_job; +Workers::LaunchStatus Workers::m_status; std::atomic Workers::m_paused; std::atomic Workers::m_sequence; std::list Workers::m_queue; @@ -109,10 +110,12 @@ void Workers::setJob(const Job &job, bool donate) void Workers::start(xmrig::Controller *controller) { const std::vector &threads = controller->config()->threads(); + m_status.algo = controller->config()->algorithm(); + m_status.colors = controller->config()->isHugePages(); + m_status.threads = threads.size(); - size_t totalWays = 0; for (const xmrig::IThread *thread : threads) { - totalWays += thread->multiway(); + m_status.ways += thread->multiway(); } m_hashrate = new Hashrate(threads.size(), controller); @@ -130,7 +133,7 @@ void Workers::start(xmrig::Controller *controller) uint32_t offset = 0; for (xmrig::IThread *thread : threads) { - Handle *handle = new Handle(thread, offset, totalWays); + Handle *handle = new Handle(thread, offset, m_status.ways); offset += thread->multiway(); m_workers.push_back(handle); @@ -203,7 +206,7 @@ void Workers::onReady(void *arg) return; } - worker->start(); + start(worker); } @@ -244,3 +247,33 @@ void Workers::onTick(uv_timer_t *handle) Api::tick(m_hashrate); # endif } + + +void Workers::start(IWorker *worker) +{ + const Worker *w = static_cast(worker); + + uv_mutex_lock(&m_mutex); + m_status.started++; + m_status.pages += w->memory().pages; + m_status.hugePages += w->memory().hugePages; + + if (m_status.started == m_status.threads) { + const double percent = (double) m_status.hugePages / m_status.pages * 100.0; + + if (m_status.colors) { + LOG_INFO("\x1B[01;32mREADY (CPU)\x1B[0m threads \x1B[01;36m%zu(%zu)\x1B[0m huge pages %s%zu/%zu %1.0f%%\x1B[0m memory \x1B[01;36m%zu.0 MB", + m_status.threads, m_status.ways, + (m_status.hugePages == m_status.pages ? "\x1B[01;32m" : (m_status.hugePages == 0 ? "\x1B[01;31m" : "\x1B[01;33m")), + m_status.hugePages, m_status.pages, percent, m_status.pages * 2); + } + else { + LOG_INFO("READY (CPU) threads %zu(%zu) huge pages %zu/%zu %f%% memory %zu.0 MB", + m_status.threads, m_status.ways, m_status.hugePages, m_status.pages, percent, m_status.pages * 2); + } + } + + uv_mutex_unlock(&m_mutex); + + worker->start(); +} diff --git a/src/workers/Workers.h b/src/workers/Workers.h index ecec9e30..81b3411a 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -37,6 +37,7 @@ class Handle; class Hashrate; class IJobResultListener; +class IWorker; namespace xmrig { @@ -66,12 +67,36 @@ private: static void onReady(void *arg); static void onResult(uv_async_t *handle); static void onTick(uv_timer_t *handle); + static void start(IWorker *worker); + + class LaunchStatus + { + public: + inline LaunchStatus() : + colors(true), + hugePages(0), + pages(0), + started(0), + threads(0), + ways(0), + algo(xmrig::CRYPTONIGHT) + {} + + bool colors; + size_t hugePages; + size_t pages; + size_t started; + size_t threads; + size_t ways; + xmrig::Algo algo; + }; static bool m_active; static bool m_enabled; static Hashrate *m_hashrate; static IJobResultListener *m_listener; static Job m_job; + static LaunchStatus m_status; static std::atomic m_paused; static std::atomic m_sequence; static std::list m_queue; From 6de83dddd6d96e6c2b48a4f04318ec6ac1cb821a Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 15 Apr 2018 15:01:41 +0700 Subject: [PATCH 207/389] Fix wrong memory usage displayed for cn-lite. --- src/workers/Workers.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 6d6deb3b..40dbb00c 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -260,16 +260,17 @@ void Workers::start(IWorker *worker) if (m_status.started == m_status.threads) { const double percent = (double) m_status.hugePages / m_status.pages * 100.0; + const size_t ratio = m_status.algo == xmrig::CRYPTONIGHT_LITE ? 1 : 2; if (m_status.colors) { LOG_INFO("\x1B[01;32mREADY (CPU)\x1B[0m threads \x1B[01;36m%zu(%zu)\x1B[0m huge pages %s%zu/%zu %1.0f%%\x1B[0m memory \x1B[01;36m%zu.0 MB", m_status.threads, m_status.ways, (m_status.hugePages == m_status.pages ? "\x1B[01;32m" : (m_status.hugePages == 0 ? "\x1B[01;31m" : "\x1B[01;33m")), - m_status.hugePages, m_status.pages, percent, m_status.pages * 2); + m_status.hugePages, m_status.pages, percent, m_status.pages * ratio); } else { LOG_INFO("READY (CPU) threads %zu(%zu) huge pages %zu/%zu %f%% memory %zu.0 MB", - m_status.threads, m_status.ways, m_status.hugePages, m_status.pages, percent, m_status.pages * 2); + m_status.threads, m_status.ways, m_status.hugePages, m_status.pages, percent, m_status.pages * ratio); } } From f0158ae5059573fe2627a2c085c8e8dbca708ef1 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 15 Apr 2018 15:10:41 +0700 Subject: [PATCH 208/389] Fix again. --- src/workers/Workers.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 40dbb00c..e26b2030 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -28,6 +28,7 @@ #include "api/Api.h" #include "core/Config.h" #include "core/Controller.h" +#include "crypto/CryptoNight_constants.h" #include "interfaces/IJobResultListener.h" #include "interfaces/IThread.h" #include "log/Log.h" @@ -260,17 +261,17 @@ void Workers::start(IWorker *worker) if (m_status.started == m_status.threads) { const double percent = (double) m_status.hugePages / m_status.pages * 100.0; - const size_t ratio = m_status.algo == xmrig::CRYPTONIGHT_LITE ? 1 : 2; + const size_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo) / 1048576; if (m_status.colors) { LOG_INFO("\x1B[01;32mREADY (CPU)\x1B[0m threads \x1B[01;36m%zu(%zu)\x1B[0m huge pages %s%zu/%zu %1.0f%%\x1B[0m memory \x1B[01;36m%zu.0 MB", m_status.threads, m_status.ways, (m_status.hugePages == m_status.pages ? "\x1B[01;32m" : (m_status.hugePages == 0 ? "\x1B[01;31m" : "\x1B[01;33m")), - m_status.hugePages, m_status.pages, percent, m_status.pages * ratio); + m_status.hugePages, m_status.pages, percent, memory); } else { LOG_INFO("READY (CPU) threads %zu(%zu) huge pages %zu/%zu %f%% memory %zu.0 MB", - m_status.threads, m_status.ways, m_status.hugePages, m_status.pages, percent, m_status.pages * ratio); + m_status.threads, m_status.ways, m_status.hugePages, m_status.pages, percent, memory); } } From f8bf48a522c3bcf6f6a3b2e3f83363cbeb3c89eb Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 15 Apr 2018 19:25:09 +0700 Subject: [PATCH 209/389] Added config only boolean option "hw-aes". --- src/core/Config.cpp | 58 +++++++++++++++----------------- src/core/ConfigLoader_platform.h | 1 + src/interfaces/IConfig.h | 1 + 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 1eb8e6e5..f75e058a 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -67,53 +67,49 @@ bool xmrig::Config::reload(const char *json) void xmrig::Config::getJSON(rapidjson::Document &doc) const { + using namespace rapidjson; + doc.SetObject(); auto &allocator = doc.GetAllocator(); - doc.AddMember("algo", rapidjson::StringRef(algoName()), allocator); + doc.AddMember("algo", StringRef(algoName()), allocator); - rapidjson::Value api(rapidjson::kObjectType); + Value api(kObjectType); api.AddMember("port", apiPort(), allocator); - api.AddMember("access-token", apiToken() ? rapidjson::Value(rapidjson::StringRef(apiToken())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); - api.AddMember("worker-id", apiWorkerId() ? rapidjson::Value(rapidjson::StringRef(apiWorkerId())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); + api.AddMember("access-token", apiToken() ? Value(StringRef(apiToken())).Move() : Value(kNullType).Move(), allocator); + api.AddMember("worker-id", apiWorkerId() ? Value(StringRef(apiWorkerId())).Move() : Value(kNullType).Move(), allocator); api.AddMember("ipv6", isApiIPv6(), allocator); api.AddMember("restricted", isApiRestricted(), allocator); doc.AddMember("api", api, allocator); doc.AddMember("av", algoVariant(), allocator); doc.AddMember("background", isBackground(), allocator); - - doc.AddMember("colors", isColors(), allocator); + doc.AddMember("colors", isColors(), allocator); if (affinity() != -1L) { snprintf(affinity_tmp, sizeof(affinity_tmp) - 1, "0x%" PRIX64, affinity()); - doc.AddMember("cpu-affinity", rapidjson::StringRef(affinity_tmp), allocator); + doc.AddMember("cpu-affinity", StringRef(affinity_tmp), allocator); } else { - doc.AddMember("cpu-affinity", rapidjson::kNullType, allocator); - } - - if (priority() != -1) { - doc.AddMember("cpu-priority", priority(), allocator); - } - else { - doc.AddMember("cpu-priority", rapidjson::kNullType, allocator); + doc.AddMember("cpu-affinity", kNullType, allocator); } + doc.AddMember("cpu-priority", priority() != -1 ? Value(priority()) : Value(kNullType), allocator); doc.AddMember("donate-level", donateLevel(), allocator); doc.AddMember("huge-pages", isHugePages(), allocator); - doc.AddMember("log-file", logFile() ? rapidjson::Value(rapidjson::StringRef(logFile())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); + doc.AddMember("hw-aes", m_aesMode == AES_AUTO ? Value(kNullType) : Value(m_aesMode == AES_HW), allocator); + doc.AddMember("log-file", logFile() ? Value(StringRef(logFile())).Move() : Value(kNullType).Move(), allocator); doc.AddMember("max-cpu-usage", m_maxCpuUsage, allocator); - rapidjson::Value pools(rapidjson::kArrayType); + Value pools(kArrayType); for (const Pool &pool : m_pools) { - rapidjson::Value obj(rapidjson::kObjectType); + Value obj(kObjectType); - obj.AddMember("url", rapidjson::StringRef(pool.url()), allocator); - obj.AddMember("user", rapidjson::StringRef(pool.user()), allocator); - obj.AddMember("pass", rapidjson::StringRef(pool.password()), allocator); + obj.AddMember("url", StringRef(pool.url()), allocator); + obj.AddMember("user", StringRef(pool.user()), allocator); + obj.AddMember("pass", StringRef(pool.password()), allocator); if (pool.keepAlive() == 0 || pool.keepAlive() == Pool::kKeepAliveTimeout) { obj.AddMember("keepalive", pool.keepAlive() > 0, allocator); @@ -134,7 +130,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("retry-pause", retryPause(), allocator); doc.AddMember("safe", m_safe, allocator); doc.AddMember("threads", threadsCount(), allocator); - doc.AddMember("user-agent", userAgent() ? rapidjson::Value(rapidjson::StringRef(userAgent())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator); + doc.AddMember("user-agent", userAgent() ? Value(StringRef(userAgent())).Move() : Value(kNullType).Move(), allocator); # ifdef HAVE_SYSLOG_H doc.AddMember("syslog", isSyslog(), allocator); @@ -156,13 +152,11 @@ bool xmrig::Config::adjust() return false; } - if (m_aesMode == AES_AUTO) { - m_aesMode = Cpu::hasAES() ? AES_HW : AES_SOFT; - } - if (!m_threads.cpu.empty()) { + const bool softAES = (m_aesMode == AES_AUTO ? (Cpu::hasAES() ? AES_HW : AES_SOFT) : m_aesMode) == AES_SOFT; + for (size_t i = 0; i < m_threads.cpu.size(); ++i) { - m_threads.list.push_back(CpuThread::createFromData(i, m_algorithm, m_threads.cpu[i], m_priority, m_aesMode == AES_SOFT)); + m_threads.list.push_back(CpuThread::createFromData(i, m_algorithm, m_threads.cpu[i], m_priority, softAES)); } return true; @@ -198,18 +192,22 @@ bool xmrig::Config::parseBoolean(int key, bool enable) } switch (key) { - case xmrig::IConfig::SafeKey: /* --safe */ + case IConfig::SafeKey: /* --safe */ m_safe = enable; break; - case xmrig::IConfig::HugePagesKey: /* --no-huge-pages */ + case IConfig::HugePagesKey: /* --no-huge-pages */ m_hugePages = enable; break; - case xmrig::IConfig::DryRunKey: /* --dry-run */ + case IConfig::DryRunKey: /* --dry-run */ m_dryRun = enable; break; + case IConfig::HardwareAESKey: /* hw-aes config only */ + m_aesMode = enable ? AES_HW : AES_SOFT; + break; + default: break; } diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index a090fb99..7e1a4baa 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -150,6 +150,7 @@ static struct option const config_options[] = { { "syslog", 0, nullptr, xmrig::IConfig::SyslogKey }, { "threads", 1, nullptr, xmrig::IConfig::ThreadsKey }, { "user-agent", 1, nullptr, xmrig::IConfig::UserAgentKey }, + { "hw-aes", 0, nullptr, xmrig::IConfig::HardwareAESKey }, { 0, 0, 0, 0 } }; diff --git a/src/interfaces/IConfig.h b/src/interfaces/IConfig.h index 89aa6217..f7433d7f 100644 --- a/src/interfaces/IConfig.h +++ b/src/interfaces/IConfig.h @@ -74,6 +74,7 @@ public: MaxCPUUsageKey = 1004, SafeKey = 1005, ThreadsKey = 't', + HardwareAESKey = 1011, // xmrig-proxy AccessLogFileKey = 'A', From dba1acd30201bf0d3bf09b44a89c72abb197b595 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 15 Apr 2018 21:41:03 +0700 Subject: [PATCH 210/389] Finalize config changes. --- src/core/Config.cpp | 32 ++++++++++++++++++++++---------- src/core/Config.h | 15 ++++++++++----- src/interfaces/IThread.h | 13 +++++++------ src/workers/CpuThread.cpp | 18 +++++++++++++++++- src/workers/CpuThread.h | 3 +++ 5 files changed, 59 insertions(+), 22 deletions(-) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index f75e058a..b3564f92 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -43,12 +43,10 @@ static char affinity_tmp[20] = { 0 }; xmrig::Config::Config() : xmrig::CommonConfig(), m_aesMode(AES_AUTO), m_algoVariant(AV_AUTO), - m_doubleHash(false), m_dryRun(false), m_hugePages(true), m_safe(false), m_maxCpuUsage(75), - m_printTime(60), m_priority(-1) { } @@ -129,8 +127,21 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("retries", retries(), allocator); doc.AddMember("retry-pause", retryPause(), allocator); doc.AddMember("safe", m_safe, allocator); - doc.AddMember("threads", threadsCount(), allocator); - doc.AddMember("user-agent", userAgent() ? Value(StringRef(userAgent())).Move() : Value(kNullType).Move(), allocator); + + if (threadsMode() == Advanced) { + Value threads(kArrayType); + + for (const IThread *thread : m_threads.list) { + threads.PushBack(thread->toConfig(doc), doc.GetAllocator()); + } + + doc.AddMember("threads", threads, allocator); + } + else { + doc.AddMember("threads", threadsMode() == Automatic ? Value(kNullType) : Value(threadsCount()), allocator); + } + + doc.AddMember("user-agent", userAgent() ? Value(StringRef(userAgent())).Move() : Value(kNullType).Move(), allocator); # ifdef HAVE_SYSLOG_H doc.AddMember("syslog", isSyslog(), allocator); @@ -153,6 +164,7 @@ bool xmrig::Config::adjust() } if (!m_threads.cpu.empty()) { + m_threads.mode = Advanced; const bool softAES = (m_aesMode == AES_AUTO ? (Cpu::hasAES() ? AES_HW : AES_SOFT) : m_aesMode) == AES_SOFT; for (size_t i = 0; i < m_threads.cpu.size(); ++i) { @@ -162,16 +174,16 @@ bool xmrig::Config::adjust() return true; } - m_algoVariant = getAlgoVariant(); - if (m_algoVariant == AV_DOUBLE || m_algoVariant == AV_DOUBLE_SOFT) { - m_doubleHash = true; - } + m_algoVariant = getAlgoVariant(); + m_threads.mode = m_threads.count ? Simple : Automatic; + + const bool doubleHash = m_algoVariant == AV_DOUBLE || m_algoVariant == AV_DOUBLE_SOFT; if (!m_threads.count) { - m_threads.count = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); + m_threads.count = Cpu::optimalThreadsCount(m_algorithm, doubleHash, m_maxCpuUsage); } else if (m_safe) { - const size_t count = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage); + const size_t count = Cpu::optimalThreadsCount(m_algorithm, doubleHash, m_maxCpuUsage); if (m_threads.count > count) { m_threads.count = count; } diff --git a/src/core/Config.h b/src/core/Config.h index 720557a7..13320a80 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -61,6 +61,13 @@ class IWatcherListener; class Config : public CommonConfig { public: + enum ThreadsMode { + Automatic, + Simple, + Advanced + }; + + Config(); ~Config(); @@ -70,14 +77,13 @@ public: inline AesMode aesMode() const { return m_aesMode; } inline AlgoVariant algoVariant() const { return m_algoVariant; } - inline bool isDoubleHash() const { return m_doubleHash; } inline bool isDryRun() const { return m_dryRun; } inline bool isHugePages() const { return m_hugePages; } inline const std::vector &threads() const { return m_threads.list; } - inline int printTime() const { return m_printTime; } inline int priority() const { return m_priority; } inline int threadsCount() const { return m_threads.list.size(); } inline int64_t affinity() const { return m_threads.mask; } + inline ThreadsMode threadsMode() const { return m_threads.mode; } static Config *load(int argc, char **argv, IWatcherListener *listener); @@ -99,23 +105,22 @@ private: struct Threads { - inline Threads() : mask(-1L), count(0) {} + inline Threads() : mask(-1L), count(0), mode(Automatic) {} int64_t mask; size_t count; std::vector cpu; std::vector list; + ThreadsMode mode; }; AesMode m_aesMode; AlgoVariant m_algoVariant; - bool m_doubleHash; bool m_dryRun; bool m_hugePages; bool m_safe; int m_maxCpuUsage; - int m_printTime; int m_priority; Threads m_threads; }; diff --git a/src/interfaces/IThread.h b/src/interfaces/IThread.h index f517ed18..2e9e3c39 100644 --- a/src/interfaces/IThread.h +++ b/src/interfaces/IThread.h @@ -53,12 +53,13 @@ public: virtual ~IThread() {} - virtual Algo algorithm() const = 0; - virtual int priority() const = 0; - virtual int64_t affinity() const = 0; - virtual Multiway multiway() const = 0; - virtual size_t index() const = 0; - virtual Type type() const = 0; + virtual Algo algorithm() const = 0; + virtual int priority() const = 0; + virtual int64_t affinity() const = 0; + virtual Multiway multiway() const = 0; + virtual rapidjson::Value toConfig(rapidjson::Document &doc) const = 0; + virtual size_t index() const = 0; + virtual Type type() const = 0; # ifndef XMRIG_NO_API virtual rapidjson::Value toAPI(rapidjson::Document &doc) const = 0; diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 07fbbb64..ac733ef6 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -245,7 +245,9 @@ xmrig::CpuThread::Data xmrig::CpuThread::parse(const rapidjson::Value &object) #ifndef XMRIG_NO_API rapidjson::Value xmrig::CpuThread::toAPI(rapidjson::Document &doc) const { - rapidjson::Value obj(rapidjson::kObjectType); + using namespace rapidjson; + + Value obj(kObjectType); auto &allocator = doc.GetAllocator(); obj.AddMember("type", "cpu", allocator); @@ -259,3 +261,17 @@ rapidjson::Value xmrig::CpuThread::toAPI(rapidjson::Document &doc) const return obj; } #endif + + +rapidjson::Value xmrig::CpuThread::toConfig(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + Value obj(kObjectType); + auto &allocator = doc.GetAllocator(); + + obj.AddMember("low_power_mode", multiway(), allocator); + obj.AddMember("affine_to_cpu", affinity() == -1L ? Value(kFalseType) : Value(affinity()), allocator); + + return obj; +} diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index e14c79f1..ba36cc87 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -77,10 +77,13 @@ public: inline size_t index() const override { return m_index; } inline Type type() const override { return CPU; } +protected: # ifndef XMRIG_NO_API rapidjson::Value toAPI(rapidjson::Document &doc) const override; # endif + rapidjson::Value toConfig(rapidjson::Document &doc) const override; + private: const Algo m_algorithm; const AlgoVariant m_av; From 9e3f2ae9f9adfdb35e26a7519acc92b261288d90 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 16 Apr 2018 15:40:37 +0700 Subject: [PATCH 211/389] Added x3 x4 x5 hashing modes. --- cmake/flags.cmake | 4 +- src/api/ApiRouter.cpp | 8 +- src/crypto/CryptoNight_test.h | 69 +++++-- src/crypto/CryptoNight_x86.h | 362 +++++++++++++++++++++++++++++++++- src/crypto/soft_aes.h | 17 ++ src/workers/MultiWorker.cpp | 2 +- 6 files changed, 442 insertions(+), 20 deletions(-) diff --git a/cmake/flags.cmake b/cmake/flags.cmake index 13e50564..498f2165 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -13,10 +13,10 @@ endif() if (CMAKE_CXX_COMPILER_ID MATCHES GNU) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-strict-aliasing") - set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-exceptions -fno-rtti") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s") if (XMRIG_ARMv8) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a+crypto") diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 0893bc8b..8206aa3e 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -298,13 +298,17 @@ void ApiRouter::getResults(rapidjson::Document &doc) const void ApiRouter::getThreads(rapidjson::Document &doc) const { - doc.SetArray(); + doc.SetObject(); + auto &allocator = doc.GetAllocator(); const std::vector &threads = m_controller->config()->threads(); + rapidjson::Value list(rapidjson::kArrayType); for (const xmrig::IThread *thread : threads) { - doc.PushBack(thread->toAPI(doc), doc.GetAllocator()); + list.PushBack(thread->toAPI(doc), allocator); } + + doc.AddMember("threads", list, allocator); } diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 5717f1d3..52dd2965 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -26,7 +26,7 @@ #define __CRYPTONIGHT_TEST_H__ -const static uint8_t test_input[152] = { +const static uint8_t test_input[380] = { 0x01, 0x00, 0xFB, 0x8E, 0x8A, 0xC8, 0x05, 0x89, 0x93, 0x23, 0x37, 0x1B, 0xB7, 0x90, 0xDB, 0x19, 0x21, 0x8A, 0xFD, 0x8D, 0xB8, 0xE3, 0x75, 0x5D, 0x8B, 0x90, 0xF3, 0x9B, 0x3D, 0x55, 0x06, 0xA9, 0xAB, 0xCE, 0x4F, 0xA9, 0x12, 0x24, 0x45, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x81, 0x46, 0xD4, 0x9F, @@ -36,52 +36,97 @@ const static uint8_t test_input[152] = { 0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, - 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01 + 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01, + 0x07, 0x07, 0xB4, 0x87, 0xD0, 0xD6, 0x05, 0x26, 0xE0, 0xC6, 0xDD, 0x9B, 0xC7, 0x18, 0xC3, 0xCF, + 0x52, 0x04, 0xBD, 0x4F, 0x9B, 0x27, 0xF6, 0x73, 0xB9, 0x3F, 0xEF, 0x7B, 0xB2, 0xF7, 0x2B, 0xBB, + 0x3F, 0x3E, 0x9C, 0x3E, 0x9D, 0x33, 0x1E, 0xDE, 0xAD, 0xBE, 0xEF, 0x4E, 0x00, 0x91, 0x81, 0x29, + 0x74, 0xB2, 0x70, 0xE7, 0x6D, 0xD2, 0x2A, 0x5F, 0x52, 0x04, 0x93, 0xE6, 0x18, 0x89, 0x40, 0xD8, + 0xC6, 0xE3, 0x90, 0x6E, 0xAA, 0x6A, 0xB7, 0xE2, 0x08, 0x7E, 0x78, 0x0E, + 0x01, 0x00, 0xEE, 0xB2, 0xD1, 0xD6, 0x05, 0xFF, 0x27, 0x7F, 0x26, 0xDB, 0xAA, 0xB2, 0xC9, 0x26, + 0x30, 0xC6, 0xCF, 0x11, 0x64, 0xEA, 0x6C, 0x8A, 0xE0, 0x98, 0x01, 0xF8, 0x75, 0x4B, 0x49, 0xAF, + 0x79, 0x70, 0xAE, 0xEE, 0xA7, 0x62, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x47, 0x8C, 0x63, 0xE7, 0xD8, + 0x40, 0x02, 0x3C, 0xDA, 0xEA, 0x92, 0x52, 0x53, 0xAC, 0xFD, 0xC7, 0x8A, 0x4C, 0x31, 0xB2, 0xF2, + 0xEC, 0x72, 0x7B, 0xFF, 0xCE, 0xC0, 0xE7, 0x12, 0xD4, 0xE9, 0x2A, 0x01, + 0x07, 0x07, 0xA9, 0xB7, 0xD1, 0xD6, 0x05, 0x3F, 0x0D, 0x5E, 0xFD, 0xC7, 0x03, 0xFC, 0xFC, 0xD2, + 0xCE, 0xBC, 0x44, 0xD8, 0xAB, 0x44, 0xA6, 0xA0, 0x3A, 0xE4, 0x4D, 0x8F, 0x15, 0xAF, 0x62, 0x17, + 0xD1, 0xE0, 0x92, 0x85, 0xE4, 0x73, 0xF9, 0x00, 0x00, 0x00, 0xA0, 0xFC, 0x09, 0xDE, 0xAB, 0xF5, + 0x8B, 0x6F, 0x1D, 0xCA, 0xA8, 0xBA, 0xAC, 0x74, 0xDD, 0x74, 0x19, 0xD5, 0xD6, 0x10, 0xEC, 0x38, + 0xCF, 0x50, 0x29, 0x6A, 0x07, 0x0B, 0x93, 0x8F, 0x8F, 0xA8, 0x10, 0x04 }; -const static uint8_t test_output_v0[64] = { +const static uint8_t test_output_v0[160] = { 0x1B, 0x60, 0x6A, 0x3F, 0x4A, 0x07, 0xD6, 0x48, 0x9A, 0x1B, 0xCD, 0x07, 0x69, 0x7B, 0xD1, 0x66, 0x96, 0xB6, 0x1C, 0x8A, 0xE9, 0x82, 0xF6, 0x1A, 0x90, 0x16, 0x0F, 0x4E, 0x52, 0x82, 0x8A, 0x7F, 0x1A, 0x3F, 0xFB, 0xEE, 0x90, 0x9B, 0x42, 0x0D, 0x91, 0xF7, 0xBE, 0x6E, 0x5F, 0xB5, 0x6D, 0xB7, - 0x1B, 0x31, 0x10, 0xD8, 0x86, 0x01, 0x1E, 0x87, 0x7E, 0xE5, 0x78, 0x6A, 0xFD, 0x08, 0x01, 0x00 + 0x1B, 0x31, 0x10, 0xD8, 0x86, 0x01, 0x1E, 0x87, 0x7E, 0xE5, 0x78, 0x6A, 0xFD, 0x08, 0x01, 0x00, + 0xA1, 0xB4, 0xFA, 0xE3, 0xE5, 0x76, 0xCE, 0xCF, 0xB7, 0x9C, 0xAF, 0x3E, 0x29, 0x92, 0xE4, 0xE0, + 0x31, 0x24, 0x05, 0x48, 0xBF, 0x8D, 0x5F, 0x7B, 0x11, 0x03, 0x60, 0xAA, 0xD7, 0x50, 0x3F, 0x0C, + 0x2D, 0x30, 0xF3, 0x87, 0x4F, 0x86, 0xA1, 0x4A, 0xB5, 0xA2, 0x1A, 0x08, 0xD0, 0x44, 0x2C, 0x9D, + 0x16, 0xE9, 0x28, 0x49, 0xA1, 0xFF, 0x85, 0x6F, 0x12, 0xBB, 0x7D, 0xAB, 0x11, 0x1C, 0xE7, 0xF7, + 0x2D, 0x9D, 0x19, 0xE4, 0xD2, 0x26, 0x44, 0x1E, 0xCD, 0x22, 0x08, 0x24, 0xA8, 0x97, 0x46, 0x62, + 0x04, 0x84, 0x90, 0x4A, 0xEE, 0x99, 0x14, 0xED, 0xB8, 0xC6, 0x0D, 0x37, 0xA1, 0x66, 0x17, 0xB0 }; // Monero v7 -const static uint8_t test_output_v1[64] = { +const static uint8_t test_output_v1[160] = { 0xC9, 0xFA, 0xE8, 0x42, 0x5D, 0x86, 0x88, 0xDC, 0x23, 0x6B, 0xCD, 0xBC, 0x42, 0xFD, 0xB4, 0x2D, 0x37, 0x6C, 0x6E, 0xC1, 0x90, 0x50, 0x1A, 0xA8, 0x4B, 0x04, 0xA4, 0xB4, 0xCF, 0x1E, 0xE1, 0x22, 0xF2, 0x2D, 0x3D, 0x62, 0x03, 0xD2, 0xA0, 0x8B, 0x41, 0xD9, 0x02, 0x72, 0x78, 0xD8, 0xBC, 0xC9, - 0x83, 0xAC, 0xAD, 0xA9, 0xB6, 0x8E, 0x52, 0xE3, 0xC6, 0x89, 0x69, 0x2A, 0x50, 0xE9, 0x21, 0xD9 + 0x83, 0xAC, 0xAD, 0xA9, 0xB6, 0x8E, 0x52, 0xE3, 0xC6, 0x89, 0x69, 0x2A, 0x50, 0xE9, 0x21, 0xD9, + 0xE7, 0x8C, 0x5A, 0x6E, 0x38, 0x30, 0x68, 0x4A, 0x73, 0xFC, 0x1B, 0xC6, 0x6D, 0xFC, 0x8D, 0x98, + 0xB4, 0xC2, 0x23, 0x39, 0xAD, 0xE0, 0x9D, 0xF6, 0x6D, 0x8C, 0x6A, 0xAA, 0xF9, 0xB2, 0xE3, 0x4C, + 0xB6, 0x90, 0x6C, 0xE6, 0x15, 0x5E, 0x46, 0x07, 0x9C, 0xB2, 0x6B, 0xAC, 0x3B, 0xAC, 0x1A, 0xDE, + 0x92, 0x2C, 0xD6, 0x0C, 0x46, 0x9D, 0x9B, 0xC2, 0x84, 0x52, 0x65, 0xF6, 0xBD, 0xFA, 0x0D, 0x74, + 0x00, 0x66, 0x10, 0x07, 0xF1, 0x19, 0x06, 0x3A, 0x6C, 0xFF, 0xEE, 0xB2, 0x40, 0xE5, 0x88, 0x2B, + 0x6C, 0xAB, 0x6B, 0x1D, 0x88, 0xB8, 0x44, 0x25, 0xF4, 0xEA, 0xB7, 0xEC, 0xBA, 0x12, 0x8A, 0x24 }; #ifndef XMRIG_NO_AEON -const static uint8_t test_output_v0_lite[64] = { +const static uint8_t test_output_v0_lite[160] = { 0x28, 0xA2, 0x2B, 0xAD, 0x3F, 0x93, 0xD1, 0x40, 0x8F, 0xCA, 0x47, 0x2E, 0xB5, 0xAD, 0x1C, 0xBE, 0x75, 0xF2, 0x1D, 0x05, 0x3C, 0x8C, 0xE5, 0xB3, 0xAF, 0x10, 0x5A, 0x57, 0x71, 0x3E, 0x21, 0xDD, 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, - 0x00, 0x4E, 0xEC, 0xE0, 0x9B, 0x83, 0xA7, 0x2E, 0xF6, 0xBA, 0x98, 0x64, 0xD3, 0x51, 0x0C, 0x88 + 0x00, 0x4E, 0xEC, 0xE0, 0x9B, 0x83, 0xA7, 0x2E, 0xF6, 0xBA, 0x98, 0x64, 0xD3, 0x51, 0x0C, 0x88, + 0x38, 0x08, 0xE1, 0x17, 0x0B, 0x99, 0x8D, 0x1A, 0x3C, 0xCE, 0x35, 0xC5, 0xC7, 0x3A, 0x00, 0x2E, + 0xCB, 0x54, 0xF0, 0x78, 0x2E, 0x9E, 0xDB, 0xC7, 0xDF, 0x2E, 0x71, 0x9A, 0x16, 0x97, 0xC4, 0x18, + 0x4B, 0x97, 0x07, 0xFE, 0x5D, 0x98, 0x9A, 0xD6, 0xD8, 0xE5, 0x92, 0x66, 0x87, 0x7F, 0x19, 0x37, + 0xA2, 0x5E, 0xE6, 0x96, 0xB5, 0x97, 0x33, 0x89, 0xE0, 0xA7, 0xC9, 0xDD, 0x4A, 0x7E, 0x9E, 0x53, + 0xBE, 0x91, 0x2B, 0xF5, 0xF5, 0xAF, 0xDD, 0x09, 0xA2, 0xF4, 0xA4, 0x56, 0xEB, 0x96, 0x22, 0xC9, + 0x94, 0xFB, 0x7B, 0x28, 0xC9, 0x97, 0x65, 0x04, 0xAC, 0x4F, 0x84, 0x71, 0xDA, 0x6E, 0xD8, 0xC5 }; // AEON v7 -const static uint8_t test_output_v1_lite[64] = { +const static uint8_t test_output_v1_lite[160] = { 0x87, 0xC4, 0xE5, 0x70, 0x65, 0x3E, 0xB4, 0xC2, 0xB4, 0x2B, 0x7A, 0x0D, 0x54, 0x65, 0x59, 0x45, 0x2D, 0xFA, 0xB5, 0x73, 0xB8, 0x2E, 0xC5, 0x2F, 0x15, 0x2B, 0x7F, 0xF9, 0x8E, 0x79, 0x44, 0x6F, 0x6D, 0x8C, 0xDC, 0x44, 0x4E, 0x9B, 0xBB, 0xFD, 0x68, 0xFC, 0x43, 0xFC, 0xD4, 0x85, 0x5B, 0x22, - 0x8C, 0x8A, 0x1B, 0xD9, 0x1D, 0x9D, 0x00, 0x28, 0x5B, 0xEC, 0x02, 0xB7, 0xCA, 0x2D, 0x67, 0x41 + 0x8C, 0x8A, 0x1B, 0xD9, 0x1D, 0x9D, 0x00, 0x28, 0x5B, 0xEC, 0x02, 0xB7, 0xCA, 0x2D, 0x67, 0x41, + 0x16, 0x08, 0x74, 0xC7, 0xA2, 0xD2, 0xA3, 0x97, 0x95, 0x76, 0xCA, 0x4D, 0x06, 0x39, 0x7A, 0xAB, + 0x6C, 0x87, 0x58, 0x33, 0x4D, 0xC8, 0x5A, 0xAB, 0x04, 0x27, 0xFE, 0x8B, 0x1C, 0x23, 0x2F, 0x32, + 0xC0, 0x44, 0xFF, 0x0D, 0xB5, 0x3B, 0x27, 0x96, 0x06, 0x89, 0x7B, 0xA3, 0x0B, 0xD0, 0xCE, 0x9E, + 0x90, 0x22, 0x77, 0x5A, 0xAD, 0xA1, 0xE5, 0xB6, 0xFC, 0xCB, 0x39, 0x7E, 0x2B, 0x10, 0xEE, 0xB4, + 0x8C, 0x2B, 0xA4, 0x1F, 0x60, 0x76, 0x39, 0xD7, 0xF6, 0x46, 0x77, 0x18, 0x20, 0xAD, 0xD4, 0xC9, + 0x87, 0xF7, 0x37, 0xDA, 0xFD, 0xBA, 0xBA, 0xD2, 0xF2, 0x68, 0xDC, 0x26, 0x8D, 0x1B, 0x08, 0xC6 }; #endif #ifndef XMRIG_NO_SUMO -const static uint8_t test_output_heavy[64] = { +const static uint8_t test_output_heavy[160] = { 0x4D, 0x94, 0x7D, 0xD6, 0xDB, 0x6E, 0x07, 0x48, 0x26, 0x4A, 0x51, 0x2E, 0xAC, 0xF3, 0x25, 0x4A, 0x1F, 0x1A, 0xA2, 0x5B, 0xFC, 0x0A, 0xAD, 0x82, 0xDE, 0xA8, 0x99, 0x96, 0x88, 0x52, 0xD2, 0x7D, 0x99, 0x83, 0xF2, 0x1B, 0xDF, 0x20, 0x10, 0xA8, 0xD7, 0x07, 0xBB, 0x2F, 0x14, 0xD7, 0x86, 0x64, - 0xBB, 0xE1, 0x18, 0x7F, 0x55, 0x01, 0x4B, 0x39, 0xE5, 0xF3, 0xD6, 0x93, 0x28, 0xE4, 0x8F, 0xC2 + 0xBB, 0xE1, 0x18, 0x7F, 0x55, 0x01, 0x4B, 0x39, 0xE5, 0xF3, 0xD6, 0x93, 0x28, 0xE4, 0x8F, 0xC2, + 0x3E, 0xE1, 0x23, 0x03, 0x5A, 0x63, 0x7B, 0x66, 0xF6, 0xD7, 0xC2, 0x2A, 0x34, 0x5E, 0x88, 0xE7, + 0xFA, 0xC4, 0x25, 0x36, 0x54, 0xCB, 0xD2, 0x5C, 0x2F, 0x80, 0x2A, 0xF9, 0xCC, 0x43, 0xF7, 0xCD, + 0xE5, 0x18, 0xA8, 0x05, 0x60, 0x18, 0xA5, 0x73, 0x72, 0x9B, 0x32, 0xDC, 0x69, 0x83, 0xC1, 0xE1, + 0x1F, 0xDB, 0xDA, 0x6B, 0xAC, 0xEC, 0x9F, 0x67, 0xF8, 0x27, 0x1D, 0xC7, 0xE6, 0x46, 0x42, 0xF9, + 0x53, 0x62, 0x0A, 0x54, 0x7D, 0x43, 0xEA, 0x18, 0x94, 0xED, 0xD8, 0x92, 0x06, 0x6A, 0xA1, 0x51, + 0xAD, 0xB1, 0xFD, 0x89, 0xFB, 0x5C, 0xB4, 0x25, 0x6A, 0xDD, 0xB0, 0x09, 0xC5, 0x72, 0x87, 0xEB }; #endif diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 824dd99c..3dafe724 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -427,7 +427,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si uint64_t ah0 = h0[1] ^ h0[5]; __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); - uint64_t idx0 = h0[0] ^ h0[4]; + uint64_t idx0 = al0; for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx; @@ -517,8 +517,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); - uint64_t idx0 = h0[0] ^ h0[4]; - uint64_t idx1 = h1[0] ^ h1[4]; + uint64_t idx0 = al0; + uint64_t idx1 = al1; for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx0, cx1; @@ -611,21 +611,377 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si } +#define CN_STEP1(a, b, c, l, ptr, idx) \ + ptr = reinterpret_cast<__m128i*>(&l[idx & MASK]); \ + c = _mm_load_si128(ptr); + + +#define CN_STEP2(a, b, c, l, ptr, idx) \ + if (SOFT_AES) { \ + c = soft_aesenc(c, a); \ + } else { \ + c = _mm_aesenc_si128(c, a); \ + } \ + \ + b = _mm_xor_si128(b, c); \ + \ + if (VARIANT > 0) { \ + cryptonight_monero_tweak(reinterpret_cast(ptr), b); \ + } else { \ + _mm_store_si128(ptr, b); \ + } + + +#define CN_STEP3(a, b, c, l, ptr, idx) \ + idx = EXTRACT64(c); \ + ptr = reinterpret_cast<__m128i*>(&l[idx & MASK]); \ + b = _mm_load_si128(ptr); + + +#define CN_STEP4(a, b, c, l, mc, ptr, idx) \ + lo = __umul128(idx, EXTRACT64(b), &hi); \ + a = _mm_add_epi64(a, _mm_set_epi64x(lo, hi)); \ + \ + if (VARIANT > 0) { \ + _mm_store_si128(ptr, _mm_xor_si128(a, mc)); \ + } else { \ + _mm_store_si128(ptr, a); \ + } \ + \ + a = _mm_xor_si128(a, b); \ + idx = EXTRACT64(a); \ + \ + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { \ + int64_t n = ((int64_t*)&l[idx & MASK])[0]; \ + int32_t d = ((int32_t*)&l[idx & MASK])[2]; \ + int64_t q = n / (d | 0x5); \ + ((int64_t*)&l[idx & MASK])[0] = n ^ q; \ + idx = d ^ q; \ + } + + +#define CONST_INIT(ctx, n) \ + __m128i mc##n; \ + if (VARIANT > 0) { \ + mc##n = _mm_set_epi64x(*reinterpret_cast(input + n * size + 35) ^ \ + *(reinterpret_cast((ctx)->state) + 24), 0); \ + } + + template inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { + constexpr size_t MASK = xmrig::cn_select_mask(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t MEM = xmrig::cn_select_memory(); + + if (VARIANT > 0 && size < 43) { + memset(output, 0, 32 * 3); + return; + } + + for (size_t i = 0; i < 3; i++) { + keccak(input + size * i, static_cast(size), ctx[i]->state, 200); + cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->state), reinterpret_cast<__m128i*>(ctx[i]->memory)); + } + + CONST_INIT(ctx[0], 0); + CONST_INIT(ctx[1], 1); + CONST_INIT(ctx[2], 2); + + uint8_t* l0 = ctx[0]->memory; + uint8_t* l1 = ctx[1]->memory; + uint8_t* l2 = ctx[2]->memory; + uint64_t* h0 = reinterpret_cast(ctx[0]->state); + uint64_t* h1 = reinterpret_cast(ctx[1]->state); + uint64_t* h2 = reinterpret_cast(ctx[2]->state); + + __m128i ax0 = _mm_set_epi64x(h0[1] ^ h0[5], h0[0] ^ h0[4]); + __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + __m128i ax1 = _mm_set_epi64x(h1[1] ^ h1[5], h1[0] ^ h1[4]); + __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); + __m128i ax2 = _mm_set_epi64x(h2[1] ^ h2[5], h2[0] ^ h2[4]); + __m128i bx2 = _mm_set_epi64x(h2[3] ^ h2[7], h2[2] ^ h2[6]); + __m128i cx0 = _mm_set_epi64x(0, 0); + __m128i cx1 = _mm_set_epi64x(0, 0); + __m128i cx2 = _mm_set_epi64x(0, 0); + + uint64_t idx0, idx1, idx2; + idx0 = EXTRACT64(ax0); + idx1 = EXTRACT64(ax1); + idx2 = EXTRACT64(ax2); + + for (size_t i = 0; i < ITERATIONS / 2; i++) { + uint64_t hi, lo; + __m128i *ptr0, *ptr1, *ptr2; + + // EVEN ROUND + CN_STEP1(ax0, bx0, cx0, l0, ptr0, idx0); + CN_STEP1(ax1, bx1, cx1, l1, ptr1, idx1); + CN_STEP1(ax2, bx2, cx2, l2, ptr2, idx2); + + CN_STEP2(ax0, bx0, cx0, l0, ptr0, idx0); + CN_STEP2(ax1, bx1, cx1, l1, ptr1, idx1); + CN_STEP2(ax2, bx2, cx2, l2, ptr2, idx2); + + CN_STEP3(ax0, bx0, cx0, l0, ptr0, idx0); + CN_STEP3(ax1, bx1, cx1, l1, ptr1, idx1); + CN_STEP3(ax2, bx2, cx2, l2, ptr2, idx2); + + CN_STEP4(ax0, bx0, cx0, l0, mc0, ptr0, idx0); + CN_STEP4(ax1, bx1, cx1, l1, mc1, ptr1, idx1); + CN_STEP4(ax2, bx2, cx2, l2, mc2, ptr2, idx2); + + // ODD ROUND + CN_STEP1(ax0, cx0, bx0, l0, ptr0, idx0); + CN_STEP1(ax1, cx1, bx1, l1, ptr1, idx1); + CN_STEP1(ax2, cx2, bx2, l2, ptr2, idx2); + + CN_STEP2(ax0, cx0, bx0, l0, ptr0, idx0); + CN_STEP2(ax1, cx1, bx1, l1, ptr1, idx1); + CN_STEP2(ax2, cx2, bx2, l2, ptr2, idx2); + + CN_STEP3(ax0, cx0, bx0, l0, ptr0, idx0); + CN_STEP3(ax1, cx1, bx1, l1, ptr1, idx1); + CN_STEP3(ax2, cx2, bx2, l2, ptr2, idx2); + + CN_STEP4(ax0, cx0, bx0, l0, mc0, ptr0, idx0); + CN_STEP4(ax1, cx1, bx1, l1, mc1, ptr1, idx1); + CN_STEP4(ax2, cx2, bx2, l2, mc2, ptr2, idx2); + } + + for (size_t i = 0; i < 3; i++) { + cn_implode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->memory), reinterpret_cast<__m128i*>(ctx[i]->state)); + keccakf(reinterpret_cast(ctx[i]->state), 24); + extra_hashes[ctx[i]->state[0] & 3](ctx[i]->state, 200, output + 32 * i); + } } template inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { + constexpr size_t MASK = xmrig::cn_select_mask(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t MEM = xmrig::cn_select_memory(); + + if (VARIANT > 0 && size < 43) { + memset(output, 0, 32 * 4); + return; + } + + for (size_t i = 0; i < 4; i++) { + keccak(input + size * i, static_cast(size), ctx[i]->state, 200); + cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->state), reinterpret_cast<__m128i*>(ctx[i]->memory)); + } + + CONST_INIT(ctx[0], 0); + CONST_INIT(ctx[1], 1); + CONST_INIT(ctx[2], 2); + CONST_INIT(ctx[3], 3); + + uint8_t* l0 = ctx[0]->memory; + uint8_t* l1 = ctx[1]->memory; + uint8_t* l2 = ctx[2]->memory; + uint8_t* l3 = ctx[3]->memory; + uint64_t* h0 = reinterpret_cast(ctx[0]->state); + uint64_t* h1 = reinterpret_cast(ctx[1]->state); + uint64_t* h2 = reinterpret_cast(ctx[2]->state); + uint64_t* h3 = reinterpret_cast(ctx[3]->state); + + __m128i ax0 = _mm_set_epi64x(h0[1] ^ h0[5], h0[0] ^ h0[4]); + __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + __m128i ax1 = _mm_set_epi64x(h1[1] ^ h1[5], h1[0] ^ h1[4]); + __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); + __m128i ax2 = _mm_set_epi64x(h2[1] ^ h2[5], h2[0] ^ h2[4]); + __m128i bx2 = _mm_set_epi64x(h2[3] ^ h2[7], h2[2] ^ h2[6]); + __m128i ax3 = _mm_set_epi64x(h3[1] ^ h3[5], h3[0] ^ h3[4]); + __m128i bx3 = _mm_set_epi64x(h3[3] ^ h3[7], h3[2] ^ h3[6]); + __m128i cx0 = _mm_set_epi64x(0, 0); + __m128i cx1 = _mm_set_epi64x(0, 0); + __m128i cx2 = _mm_set_epi64x(0, 0); + __m128i cx3 = _mm_set_epi64x(0, 0); + + uint64_t idx0, idx1, idx2, idx3; + idx0 = _mm_cvtsi128_si64(ax0); + idx1 = _mm_cvtsi128_si64(ax1); + idx2 = _mm_cvtsi128_si64(ax2); + idx3 = _mm_cvtsi128_si64(ax3); + + for (size_t i = 0; i < ITERATIONS / 2; i++) + { + uint64_t hi, lo; + __m128i *ptr0, *ptr1, *ptr2, *ptr3; + + // EVEN ROUND + CN_STEP1(ax0, bx0, cx0, l0, ptr0, idx0); + CN_STEP1(ax1, bx1, cx1, l1, ptr1, idx1); + CN_STEP1(ax2, bx2, cx2, l2, ptr2, idx2); + CN_STEP1(ax3, bx3, cx3, l3, ptr3, idx3); + + CN_STEP2(ax0, bx0, cx0, l0, ptr0, idx0); + CN_STEP2(ax1, bx1, cx1, l1, ptr1, idx1); + CN_STEP2(ax2, bx2, cx2, l2, ptr2, idx2); + CN_STEP2(ax3, bx3, cx3, l3, ptr3, idx3); + + CN_STEP3(ax0, bx0, cx0, l0, ptr0, idx0); + CN_STEP3(ax1, bx1, cx1, l1, ptr1, idx1); + CN_STEP3(ax2, bx2, cx2, l2, ptr2, idx2); + CN_STEP3(ax3, bx3, cx3, l3, ptr3, idx3); + + CN_STEP4(ax0, bx0, cx0, l0, mc0, ptr0, idx0); + CN_STEP4(ax1, bx1, cx1, l1, mc1, ptr1, idx1); + CN_STEP4(ax2, bx2, cx2, l2, mc2, ptr2, idx2); + CN_STEP4(ax3, bx3, cx3, l3, mc3, ptr3, idx3); + + // ODD ROUND + CN_STEP1(ax0, cx0, bx0, l0, ptr0, idx0); + CN_STEP1(ax1, cx1, bx1, l1, ptr1, idx1); + CN_STEP1(ax2, cx2, bx2, l2, ptr2, idx2); + CN_STEP1(ax3, cx3, bx3, l3, ptr3, idx3); + + CN_STEP2(ax0, cx0, bx0, l0, ptr0, idx0); + CN_STEP2(ax1, cx1, bx1, l1, ptr1, idx1); + CN_STEP2(ax2, cx2, bx2, l2, ptr2, idx2); + CN_STEP2(ax3, cx3, bx3, l3, ptr3, idx3); + + CN_STEP3(ax0, cx0, bx0, l0, ptr0, idx0); + CN_STEP3(ax1, cx1, bx1, l1, ptr1, idx1); + CN_STEP3(ax2, cx2, bx2, l2, ptr2, idx2); + CN_STEP3(ax3, cx3, bx3, l3, ptr3, idx3); + + CN_STEP4(ax0, cx0, bx0, l0, mc0, ptr0, idx0); + CN_STEP4(ax1, cx1, bx1, l1, mc1, ptr1, idx1); + CN_STEP4(ax2, cx2, bx2, l2, mc2, ptr2, idx2); + CN_STEP4(ax3, cx3, bx3, l3, mc3, ptr3, idx3); + } + + for (size_t i = 0; i < 4; i++) { + cn_implode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->memory), reinterpret_cast<__m128i*>(ctx[i]->state)); + keccakf(reinterpret_cast(ctx[i]->state), 24); + extra_hashes[ctx[i]->state[0] & 3](ctx[i]->state, 200, output + 32 * i); + } } template inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { + constexpr size_t MASK = xmrig::cn_select_mask(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t MEM = xmrig::cn_select_memory(); + + if (VARIANT > 0 && size < 43) { + memset(output, 0, 32 * 5); + return; + } + + for (size_t i = 0; i < 5; i++) { + keccak(input + size * i, static_cast(size), ctx[i]->state, 200); + cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->state), reinterpret_cast<__m128i*>(ctx[i]->memory)); + } + + CONST_INIT(ctx[0], 0); + CONST_INIT(ctx[1], 1); + CONST_INIT(ctx[2], 2); + CONST_INIT(ctx[3], 3); + CONST_INIT(ctx[4], 4); + + uint8_t* l0 = ctx[0]->memory; + uint8_t* l1 = ctx[1]->memory; + uint8_t* l2 = ctx[2]->memory; + uint8_t* l3 = ctx[3]->memory; + uint8_t* l4 = ctx[4]->memory; + uint64_t* h0 = reinterpret_cast(ctx[0]->state); + uint64_t* h1 = reinterpret_cast(ctx[1]->state); + uint64_t* h2 = reinterpret_cast(ctx[2]->state); + uint64_t* h3 = reinterpret_cast(ctx[3]->state); + uint64_t* h4 = reinterpret_cast(ctx[4]->state); + + __m128i ax0 = _mm_set_epi64x(h0[1] ^ h0[5], h0[0] ^ h0[4]); + __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + __m128i ax1 = _mm_set_epi64x(h1[1] ^ h1[5], h1[0] ^ h1[4]); + __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); + __m128i ax2 = _mm_set_epi64x(h2[1] ^ h2[5], h2[0] ^ h2[4]); + __m128i bx2 = _mm_set_epi64x(h2[3] ^ h2[7], h2[2] ^ h2[6]); + __m128i ax3 = _mm_set_epi64x(h3[1] ^ h3[5], h3[0] ^ h3[4]); + __m128i bx3 = _mm_set_epi64x(h3[3] ^ h3[7], h3[2] ^ h3[6]); + __m128i ax4 = _mm_set_epi64x(h4[1] ^ h4[5], h4[0] ^ h4[4]); + __m128i bx4 = _mm_set_epi64x(h4[3] ^ h4[7], h4[2] ^ h4[6]); + __m128i cx0 = _mm_set_epi64x(0, 0); + __m128i cx1 = _mm_set_epi64x(0, 0); + __m128i cx2 = _mm_set_epi64x(0, 0); + __m128i cx3 = _mm_set_epi64x(0, 0); + __m128i cx4 = _mm_set_epi64x(0, 0); + + uint64_t idx0, idx1, idx2, idx3, idx4; + idx0 = _mm_cvtsi128_si64(ax0); + idx1 = _mm_cvtsi128_si64(ax1); + idx2 = _mm_cvtsi128_si64(ax2); + idx3 = _mm_cvtsi128_si64(ax3); + idx4 = _mm_cvtsi128_si64(ax4); + + for (size_t i = 0; i < ITERATIONS / 2; i++) + { + uint64_t hi, lo; + __m128i *ptr0, *ptr1, *ptr2, *ptr3, *ptr4; + + // EVEN ROUND + CN_STEP1(ax0, bx0, cx0, l0, ptr0, idx0); + CN_STEP1(ax1, bx1, cx1, l1, ptr1, idx1); + CN_STEP1(ax2, bx2, cx2, l2, ptr2, idx2); + CN_STEP1(ax3, bx3, cx3, l3, ptr3, idx3); + CN_STEP1(ax4, bx4, cx4, l4, ptr4, idx4); + + CN_STEP2(ax0, bx0, cx0, l0, ptr0, idx0); + CN_STEP2(ax1, bx1, cx1, l1, ptr1, idx1); + CN_STEP2(ax2, bx2, cx2, l2, ptr2, idx2); + CN_STEP2(ax3, bx3, cx3, l3, ptr3, idx3); + CN_STEP2(ax4, bx4, cx4, l4, ptr4, idx4); + + CN_STEP3(ax0, bx0, cx0, l0, ptr0, idx0); + CN_STEP3(ax1, bx1, cx1, l1, ptr1, idx1); + CN_STEP3(ax2, bx2, cx2, l2, ptr2, idx2); + CN_STEP3(ax3, bx3, cx3, l3, ptr3, idx3); + CN_STEP3(ax4, bx4, cx4, l4, ptr4, idx4); + + CN_STEP4(ax0, bx0, cx0, l0, mc0, ptr0, idx0); + CN_STEP4(ax1, bx1, cx1, l1, mc1, ptr1, idx1); + CN_STEP4(ax2, bx2, cx2, l2, mc2, ptr2, idx2); + CN_STEP4(ax3, bx3, cx3, l3, mc3, ptr3, idx3); + CN_STEP4(ax4, bx4, cx4, l4, mc4, ptr4, idx4); + + // ODD ROUND + CN_STEP1(ax0, cx0, bx0, l0, ptr0, idx0); + CN_STEP1(ax1, cx1, bx1, l1, ptr1, idx1); + CN_STEP1(ax2, cx2, bx2, l2, ptr2, idx2); + CN_STEP1(ax3, cx3, bx3, l3, ptr3, idx3); + CN_STEP1(ax4, cx4, bx4, l4, ptr4, idx4); + + CN_STEP2(ax0, cx0, bx0, l0, ptr0, idx0); + CN_STEP2(ax1, cx1, bx1, l1, ptr1, idx1); + CN_STEP2(ax2, cx2, bx2, l2, ptr2, idx2); + CN_STEP2(ax3, cx3, bx3, l3, ptr3, idx3); + CN_STEP2(ax4, cx4, bx4, l4, ptr4, idx4); + + CN_STEP3(ax0, cx0, bx0, l0, ptr0, idx0); + CN_STEP3(ax1, cx1, bx1, l1, ptr1, idx1); + CN_STEP3(ax2, cx2, bx2, l2, ptr2, idx2); + CN_STEP3(ax3, cx3, bx3, l3, ptr3, idx3); + CN_STEP3(ax4, cx4, bx4, l4, ptr4, idx4); + + CN_STEP4(ax0, cx0, bx0, l0, mc0, ptr0, idx0); + CN_STEP4(ax1, cx1, bx1, l1, mc1, ptr1, idx1); + CN_STEP4(ax2, cx2, bx2, l2, mc2, ptr2, idx2); + CN_STEP4(ax3, cx3, bx3, l3, mc3, ptr3, idx3); + CN_STEP4(ax4, cx4, bx4, l4, mc4, ptr4, idx4); + } + + for (size_t i = 0; i < 5; i++) { + cn_implode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->memory), reinterpret_cast<__m128i*>(ctx[i]->state)); + keccakf(reinterpret_cast(ctx[i]->state), 24); + extra_hashes[ctx[i]->state[0] & 3](ctx[i]->state, 200, output + 32 * i); + } } #endif /* __CRYPTONIGHT_X86_H__ */ diff --git a/src/crypto/soft_aes.h b/src/crypto/soft_aes.h index 0703f98d..26c1b06a 100644 --- a/src/crypto/soft_aes.h +++ b/src/crypto/soft_aes.h @@ -105,6 +105,23 @@ static inline __m128i soft_aesenc(const uint32_t* in, __m128i key) return _mm_xor_si128(out, key); } +static inline __m128i soft_aesenc(__m128i in, __m128i key) +{ + uint32_t x0, x1, x2, x3; + x0 = _mm_cvtsi128_si32(in); + x1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0x55)); + x2 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xAA)); + x3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(in, 0xFF)); + + __m128i out = _mm_set_epi32( + (saes_table[0][x3 & 0xff] ^ saes_table[1][(x0 >> 8) & 0xff] ^ saes_table[2][(x1 >> 16) & 0xff] ^ saes_table[3][x2 >> 24]), + (saes_table[0][x2 & 0xff] ^ saes_table[1][(x3 >> 8) & 0xff] ^ saes_table[2][(x0 >> 16) & 0xff] ^ saes_table[3][x1 >> 24]), + (saes_table[0][x1 & 0xff] ^ saes_table[1][(x2 >> 8) & 0xff] ^ saes_table[2][(x3 >> 16) & 0xff] ^ saes_table[3][x0 >> 24]), + (saes_table[0][x0 & 0xff] ^ saes_table[1][(x1 >> 8) & 0xff] ^ saes_table[2][(x2 >> 16) & 0xff] ^ saes_table[3][x3 >> 24])); + + return _mm_xor_si128(out, key); +} + static inline uint32_t sub_word(uint32_t key) { return (saes_sbox[key >> 24 ] << 24) | diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index a75adecc..09dd568f 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -104,7 +104,7 @@ void MultiWorker::start() for (size_t i = 0; i < N; ++i) { if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) { - Workers::submit(JobResult(m_state.job.poolId(), m_state.job.id(), *nonce(i), m_hash, m_state.job.diff())); + Workers::submit(JobResult(m_state.job.poolId(), m_state.job.id(), *nonce(i), m_hash + (i * 32), m_state.job.diff())); } *nonce(i) += 1; From b32ec5342eefa6cec814708f81f37819787b0972 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 17 Apr 2018 09:42:53 +0700 Subject: [PATCH 212/389] Fixed automatic threads mode for --av above 4 --- CMakeLists.txt | 2 +- src/Cpu.cpp | 39 +++++++++----------- src/Cpu.h | 7 ++-- src/core/Config.cpp | 7 ++-- src/workers/CpuThread.cpp | 75 ++++++++++++++++++++------------------- src/workers/CpuThread.h | 2 ++ 6 files changed, 63 insertions(+), 69 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c7017b01..ae2a4ed7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,7 +167,7 @@ endif() add_definitions(/D__STDC_FORMAT_MACROS) add_definitions(/DUNICODE) -#add_definitions(/DAPP_DEBUG) +add_definitions(/DAPP_DEBUG) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") diff --git a/src/Cpu.cpp b/src/Cpu.cpp index f8fb092c..eebe585d 100644 --- a/src/Cpu.cpp +++ b/src/Cpu.cpp @@ -26,26 +26,27 @@ #include #include + #include "Cpu.h" -bool Cpu::m_l2_exclusive = false; -char Cpu::m_brand[64] = { 0 }; -int Cpu::m_flags = 0; -int Cpu::m_l2_cache = 0; -int Cpu::m_l3_cache = 0; -int Cpu::m_sockets = 1; -int Cpu::m_totalCores = 0; -int Cpu::m_totalThreads = 0; +bool Cpu::m_l2_exclusive = false; +char Cpu::m_brand[64] = { 0 }; +int Cpu::m_flags = 0; +int Cpu::m_l2_cache = 0; +int Cpu::m_l3_cache = 0; +int Cpu::m_sockets = 1; +int Cpu::m_totalCores = 0; +size_t Cpu::m_totalThreads = 0; -int Cpu::optimalThreadsCount(xmrig::Algo algo, bool doubleHash, int maxCpuUsage) +size_t Cpu::optimalThreadsCount(size_t size, int maxCpuUsage) { if (m_totalThreads == 1) { return 1; } - int cache = 0; + size_t cache = 0; if (m_l3_cache) { cache = m_l2_exclusive ? (m_l2_cache + m_l3_cache) : m_l3_cache; } @@ -53,22 +54,14 @@ int Cpu::optimalThreadsCount(xmrig::Algo algo, bool doubleHash, int maxCpuUsage) cache = m_l2_cache; } - int count = 0; - int size = 2048; - - if (algo == xmrig::CRYPTONIGHT_LITE) { - size = 1024; - } - else if (algo == xmrig::CRYPTONIGHT_HEAVY) { - size = 4096; - } - - if (doubleHash) { - size *= 2; - } + size_t count = 0; if (cache) { count = cache / size; + + if (cache % size >= size / 2) { + count++; + } } else { count = m_totalThreads / 2; diff --git a/src/Cpu.h b/src/Cpu.h index 97e593ed..a125bae8 100644 --- a/src/Cpu.h +++ b/src/Cpu.h @@ -28,9 +28,6 @@ #include -#include "common/xmrig.h" - - class Cpu { public: @@ -40,7 +37,7 @@ public: BMI2 = 4 }; - static int optimalThreadsCount(xmrig::Algo algo, bool doubleHash, int maxCpuUsage); + static size_t optimalThreadsCount(size_t size, int maxCpuUsage); static void init(); static inline bool hasAES() { return (m_flags & AES) != 0; } @@ -62,7 +59,7 @@ private: static int m_l3_cache; static int m_sockets; static int m_totalCores; - static int m_totalThreads; + static size_t m_totalThreads; }; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index b3564f92..4d48af29 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -30,6 +30,7 @@ #include "core/Config.h" #include "core/ConfigCreator.h" #include "Cpu.h" +#include "crypto/CryptoNight_constants.h" #include "net/Pool.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" @@ -177,13 +178,13 @@ bool xmrig::Config::adjust() m_algoVariant = getAlgoVariant(); m_threads.mode = m_threads.count ? Simple : Automatic; - const bool doubleHash = m_algoVariant == AV_DOUBLE || m_algoVariant == AV_DOUBLE_SOFT; + const size_t size = CpuThread::multiway(m_algoVariant) * cn_select_memory(m_algorithm) / 1024; if (!m_threads.count) { - m_threads.count = Cpu::optimalThreadsCount(m_algorithm, doubleHash, m_maxCpuUsage); + m_threads.count = Cpu::optimalThreadsCount(size, m_maxCpuUsage); } else if (m_safe) { - const size_t count = Cpu::optimalThreadsCount(m_algorithm, doubleHash, m_maxCpuUsage); + const size_t count = Cpu::optimalThreadsCount(size, m_maxCpuUsage); if (m_threads.count > count) { m_threads.count = count; } diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index ac733ef6..3632e193 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -54,6 +54,12 @@ xmrig::CpuThread::~CpuThread() } +bool xmrig::CpuThread::isSoftAES(AlgoVariant av) +{ + return av == AV_SINGLE_SOFT || av == AV_DOUBLE_SOFT || av > AV_PENTA; +} + + xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant av, Variant variant) { assert(variant == VARIANT_NONE || variant == VARIANT_V1); @@ -138,42 +144,6 @@ xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, A { assert(av > AV_AUTO && av < AV_MAX); - Multiway multiway = SingleWay; - bool softAES = false; - - switch (av) { - case AV_SINGLE_SOFT: - softAES = true; - break; - - case AV_DOUBLE_SOFT: - softAES = true; - case AV_DOUBLE: - multiway = DoubleWay; - break; - - case AV_TRIPLE_SOFT: - softAES = true; - case AV_TRIPLE: - multiway = TripleWay; - break; - - case AV_QUAD_SOFT: - softAES = true; - case AV_QUAD: - multiway = QuadWay; - break; - - case AV_PENTA_SOFT: - softAES = true; - case AV_PENTA: - multiway = PentaWay; - break; - - default: - break; - } - int64_t cpuId = -1L; if (affinity != -1L) { @@ -193,7 +163,7 @@ xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, A } } - return new CpuThread(index, algorithm, av, multiway, cpuId, priority, softAES, false); + return new CpuThread(index, algorithm, av, multiway(av), cpuId, priority, isSoftAES(av), false); } @@ -242,6 +212,37 @@ xmrig::CpuThread::Data xmrig::CpuThread::parse(const rapidjson::Value &object) } +xmrig::IThread::Multiway xmrig::CpuThread::multiway(AlgoVariant av) +{ + switch (av) { + case AV_SINGLE: + case AV_SINGLE_SOFT: + return SingleWay; + + case AV_DOUBLE_SOFT: + case AV_DOUBLE: + return DoubleWay; + + case AV_TRIPLE_SOFT: + case AV_TRIPLE: + return TripleWay; + + case AV_QUAD_SOFT: + case AV_QUAD: + return QuadWay; + + case AV_PENTA_SOFT: + case AV_PENTA: + return PentaWay; + + default: + break; + } + + return SingleWay; +} + + #ifndef XMRIG_NO_API rapidjson::Value xmrig::CpuThread::toAPI(rapidjson::Document &doc) const { diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index ba36cc87..0e364764 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -61,10 +61,12 @@ public: typedef void (*cn_hash_fun)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx **ctx); + static bool isSoftAES(AlgoVariant av); static cn_hash_fun fn(Algo algorithm, AlgoVariant av, Variant variant); static CpuThread *createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority); static CpuThread *createFromData(size_t index, Algo algorithm, const CpuThread::Data &data, int priority, bool softAES); static Data parse(const rapidjson::Value &object); + static Multiway multiway(AlgoVariant av); inline bool isPrefetch() const { return m_prefetch; } inline bool isSoftAES() const { return m_softAES; } From c221bf09f626eda15bbfd212dfd96d3a09b68867 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 17 Apr 2018 10:29:37 +0700 Subject: [PATCH 213/389] Use direct access to hashrate in API. --- src/api/Api.cpp | 10 ---------- src/api/Api.h | 1 - src/api/ApiRouter.cpp | 41 ++++++++++++---------------------------- src/api/ApiRouter.h | 5 ----- src/workers/Hashrate.cpp | 18 +++++++++--------- src/workers/Hashrate.h | 6 +++--- src/workers/Workers.cpp | 4 ---- src/workers/Workers.h | 2 ++ 8 files changed, 26 insertions(+), 61 deletions(-) diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 6f9991ee..3fff45b5 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -62,16 +62,6 @@ void Api::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) } -void Api::tick(const Hashrate *hashrate) -{ - if (!m_router) { - return; - } - - m_router->tick(hashrate); -} - - void Api::tick(const NetworkState &network) { if (!m_router) { diff --git a/src/api/Api.h b/src/api/Api.h index 43c7b17e..316bb0fa 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -47,7 +47,6 @@ public: static void release(); static void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply); - static void tick(const Hashrate *hashrate); static void tick(const NetworkState &results); private: diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 8206aa3e..08cd9bf6 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -47,6 +47,7 @@ #include "rapidjson/stringbuffer.h" #include "version.h" #include "workers/Hashrate.h" +#include "workers/Workers.h" extern "C" @@ -68,10 +69,6 @@ static inline double normalize(double d) ApiRouter::ApiRouter(xmrig::Controller *controller) : m_controller(controller) { - m_threads = controller->config()->threadsCount(); - m_hashrate = new double[m_threads * 3](); - - memset(m_totalHashrate, 0, sizeof(m_totalHashrate)); memset(m_workerId, 0, sizeof(m_workerId)); setWorkerId(controller->config()->apiWorkerId()); @@ -81,7 +78,6 @@ ApiRouter::ApiRouter(xmrig::Controller *controller) : ApiRouter::~ApiRouter() { - delete [] m_hashrate; } @@ -129,21 +125,6 @@ void ApiRouter::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) } -void ApiRouter::tick(const Hashrate *hashrate) -{ - for (int i = 0; i < m_threads; ++i) { - m_hashrate[i * 3] = hashrate->calc((size_t) i, Hashrate::ShortInterval); - m_hashrate[i * 3 + 1] = hashrate->calc((size_t) i, Hashrate::MediumInterval); - m_hashrate[i * 3 + 2] = hashrate->calc((size_t) i, Hashrate::LargeInterval); - } - - m_totalHashrate[0] = hashrate->calc(Hashrate::ShortInterval); - m_totalHashrate[1] = hashrate->calc(Hashrate::MediumInterval); - m_totalHashrate[2] = hashrate->calc(Hashrate::LargeInterval); - m_highestHashrate = hashrate->highest(); -} - - void ApiRouter::tick(const NetworkState &network) { m_network = network; @@ -225,21 +206,23 @@ void ApiRouter::getHashrate(rapidjson::Document &doc) const rapidjson::Value total(rapidjson::kArrayType); rapidjson::Value threads(rapidjson::kArrayType); - for (int i = 0; i < 3; ++i) { - total.PushBack(normalize(m_totalHashrate[i]), allocator); - } + const Hashrate *hr = Workers::hashrate(); - for (int i = 0; i < m_threads * 3; i += 3) { + total.PushBack(normalize(hr->calc(Hashrate::ShortInterval)), allocator); + total.PushBack(normalize(hr->calc(Hashrate::MediumInterval)), allocator); + total.PushBack(normalize(hr->calc(Hashrate::LargeInterval)), allocator); + + for (size_t i = 0; i < Workers::threads(); i++) { rapidjson::Value thread(rapidjson::kArrayType); - thread.PushBack(normalize(m_hashrate[i]), allocator); - thread.PushBack(normalize(m_hashrate[i + 1]), allocator); - thread.PushBack(normalize(m_hashrate[i + 2]), allocator); + thread.PushBack(normalize(hr->calc(i, Hashrate::ShortInterval)), allocator); + thread.PushBack(normalize(hr->calc(i, Hashrate::MediumInterval)), allocator); + thread.PushBack(normalize(hr->calc(i, Hashrate::LargeInterval)), allocator); threads.PushBack(thread, allocator); } - hashrate.AddMember("total", total, allocator); - hashrate.AddMember("highest", normalize(m_highestHashrate), allocator); + hashrate.AddMember("total", total, allocator); + hashrate.AddMember("highest", normalize(hr->highest()), allocator); hashrate.AddMember("threads", threads, allocator); doc.AddMember("hashrate", hashrate, allocator); } diff --git a/src/api/ApiRouter.h b/src/api/ApiRouter.h index e14f9e87..3ed458d4 100644 --- a/src/api/ApiRouter.h +++ b/src/api/ApiRouter.h @@ -49,7 +49,6 @@ public: void get(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) const; void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply); - void tick(const Hashrate *hashrate); void tick(const NetworkState &results); protected: @@ -69,10 +68,6 @@ private: char m_id[17]; char m_workerId[128]; - double *m_hashrate; - double m_highestHashrate; - double m_totalHashrate[3]; - int m_threads; NetworkState m_network; xmrig::Controller *m_controller; }; diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index ef06eb52..7d2e6a4c 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -22,6 +22,7 @@ */ +#include #include #include #include @@ -45,7 +46,7 @@ inline const char *format(double h, char* buf, size_t size) } -Hashrate::Hashrate(int threads, xmrig::Controller *controller) : +Hashrate::Hashrate(size_t threads, xmrig::Controller *controller) : m_highest(0.0), m_threads(threads), m_controller(controller) @@ -54,13 +55,10 @@ Hashrate::Hashrate(int threads, xmrig::Controller *controller) : m_timestamps = new uint64_t*[threads]; m_top = new uint32_t[threads]; - for (int i = 0; i < threads; i++) { - m_counts[i] = new uint64_t[kBucketSize]; - m_timestamps[i] = new uint64_t[kBucketSize]; - m_top[i] = 0; - - memset(m_counts[0], 0, sizeof(uint64_t) * kBucketSize); - memset(m_timestamps[0], 0, sizeof(uint64_t) * kBucketSize); + for (size_t i = 0; i < threads; i++) { + m_counts[i] = new uint64_t[kBucketSize](); + m_timestamps[i] = new uint64_t[kBucketSize](); + m_top[i] = 0; } const int printTime = controller->config()->printTime(); @@ -79,7 +77,7 @@ double Hashrate::calc(size_t ms) const double result = 0.0; double data; - for (int i = 0; i < m_threads; ++i) { + for (size_t i = 0; i < m_threads; ++i) { data = calc(i, ms); if (isnormal(data)) { result += data; @@ -92,6 +90,8 @@ double Hashrate::calc(size_t ms) const double Hashrate::calc(size_t threadId, size_t ms) const { + assert(threadId < m_threads); + using namespace std::chrono; const uint64_t now = time_point_cast(high_resolution_clock::now()).time_since_epoch().count(); diff --git a/src/workers/Hashrate.h b/src/workers/Hashrate.h index 5f4f0eaa..e79c757a 100644 --- a/src/workers/Hashrate.h +++ b/src/workers/Hashrate.h @@ -43,7 +43,7 @@ public: LargeInterval = 900000 }; - Hashrate(int threads, xmrig::Controller *controller); + Hashrate(size_t threads, xmrig::Controller *controller); double calc(size_t ms) const; double calc(size_t threadId, size_t ms) const; void add(size_t threadId, uint64_t count, uint64_t timestamp); @@ -52,7 +52,7 @@ public: void updateHighest(); inline double highest() const { return m_highest; } - inline int threads() const { return m_threads; } + inline size_t threads() const { return m_threads; } private: static void onReport(uv_timer_t *handle); @@ -61,7 +61,7 @@ private: constexpr static size_t kBucketMask = kBucketSize - 1; double m_highest; - int m_threads; + size_t m_threads; uint32_t* m_top; uint64_t** m_counts; uint64_t** m_timestamps; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index e26b2030..068c4258 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -243,10 +243,6 @@ void Workers::onTick(uv_timer_t *handle) if ((m_ticks++ & 0xF) == 0) { m_hashrate->updateHighest(); } - -# ifndef XMRIG_NO_API - Api::tick(m_hashrate); -# endif } diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 81b3411a..0dce78b6 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -59,6 +59,8 @@ public: static inline bool isEnabled() { return m_enabled; } static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; } static inline bool isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; } + static inline Hashrate *hashrate() { return m_hashrate; } + static inline size_t threads() { return m_status.threads; } static inline uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); } static inline void pause() { m_active = false; m_paused = 1; m_sequence++; } static inline void setListener(IJobResultListener *listener) { m_listener = listener; } From c0a72edf9a7a6002947dbee3956f1a2c9935204d Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 17 Apr 2018 10:51:29 +0700 Subject: [PATCH 214/389] Added hashrate information to "GET /1/threads" endpoint. --- src/api/ApiRouter.cpp | 13 +++++++++++-- src/workers/Workers.cpp | 20 ++++++++++++++++++++ src/workers/Workers.h | 3 ++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 08cd9bf6..b8caac9c 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -250,7 +250,7 @@ void ApiRouter::getMiner(rapidjson::Document &doc) const doc.AddMember("ua", rapidjson::StringRef(Platform::userAgent()), allocator); doc.AddMember("cpu", cpu, allocator); doc.AddMember("algo", rapidjson::StringRef(m_controller->config()->algoName()), allocator); - doc.AddMember("hugepages", false, allocator); + doc.AddMember("hugepages", Workers::hugePages() > 0, allocator); doc.AddMember("donate_level", m_controller->config()->donateLevel(), allocator); } @@ -283,12 +283,21 @@ void ApiRouter::getThreads(rapidjson::Document &doc) const { doc.SetObject(); auto &allocator = doc.GetAllocator(); + const Hashrate *hr = Workers::hashrate(); const std::vector &threads = m_controller->config()->threads(); rapidjson::Value list(rapidjson::kArrayType); for (const xmrig::IThread *thread : threads) { - list.PushBack(thread->toAPI(doc), allocator); + rapidjson::Value value = thread->toAPI(doc); + + rapidjson::Value hashrate(rapidjson::kArrayType); + hashrate.PushBack(normalize(hr->calc(thread->index(), Hashrate::ShortInterval)), allocator); + hashrate.PushBack(normalize(hr->calc(thread->index(), Hashrate::MediumInterval)), allocator); + hashrate.PushBack(normalize(hr->calc(thread->index(), Hashrate::LargeInterval)), allocator); + + value.AddMember("hashrate", hashrate, allocator); + list.PushBack(value, allocator); } doc.AddMember("threads", list, allocator); diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 068c4258..82ad54eb 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -66,6 +66,26 @@ Job Workers::job() } +size_t Workers::hugePages() +{ + uv_mutex_lock(&m_mutex); + const size_t hugePages = m_status.hugePages; + uv_mutex_unlock(&m_mutex); + + return hugePages; +} + + +size_t Workers::threads() +{ + uv_mutex_lock(&m_mutex); + const size_t threads = m_status.threads; + uv_mutex_unlock(&m_mutex); + + return threads; +} + + void Workers::printHashrate(bool detail) { m_hashrate->print(); diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 0dce78b6..f96aa2ca 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -49,6 +49,8 @@ class Workers { public: static Job job(); + static size_t hugePages(); + static size_t threads(); static void printHashrate(bool detail); static void setEnabled(bool enabled); static void setJob(const Job &job, bool donate); @@ -60,7 +62,6 @@ public: static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; } static inline bool isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; } static inline Hashrate *hashrate() { return m_hashrate; } - static inline size_t threads() { return m_status.threads; } static inline uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); } static inline void pause() { m_active = false; m_paused = 1; m_sequence++; } static inline void setListener(IJobResultListener *listener) { m_listener = listener; } From d04a1fcb8f163239ef955ec0bb4263e3d1f938fe Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 17 Apr 2018 14:36:32 +0700 Subject: [PATCH 215/389] Add extra information to threads API. --- src/Cpu_mac.cpp | 9 ++------- src/api/ApiRouter.cpp | 2 ++ src/workers/Workers.cpp | 21 +++++++++++++++++++++ src/workers/Workers.h | 5 +++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Cpu_mac.cpp b/src/Cpu_mac.cpp index 357e15ef..085148bc 100644 --- a/src/Cpu_mac.cpp +++ b/src/Cpu_mac.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2016 Jay D Dee + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,8 +38,3 @@ void Cpu::init() initCommon(); } - - -void Cpu::setAffinity(int id, uint64_t mask) -{ -} diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index b8caac9c..42256cc9 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -285,6 +285,8 @@ void ApiRouter::getThreads(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); const Hashrate *hr = Workers::hashrate(); + Workers::threadsSummary(doc); + const std::vector &threads = m_controller->config()->threads(); rapidjson::Value list(rapidjson::kArrayType); diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 82ad54eb..e1609c0e 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -33,6 +33,7 @@ #include "interfaces/IThread.h" #include "log/Log.h" #include "Mem.h" +#include "rapidjson/document.h" #include "workers/Handle.h" #include "workers/Hashrate.h" #include "workers/MultiWorker.h" @@ -188,6 +189,26 @@ void Workers::submit(const JobResult &result) } +#ifndef XMRIG_NO_API +void Workers::threadsSummary(rapidjson::Document &doc) +{ + uv_mutex_lock(&m_mutex); + const size_t pages[2] = { m_status.hugePages, m_status.pages }; + const size_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo); + uv_mutex_unlock(&m_mutex); + + auto &allocator = doc.GetAllocator(); + + rapidjson::Value hugepages(rapidjson::kArrayType); + hugepages.PushBack(pages[0], allocator); + hugepages.PushBack(pages[1], allocator); + + doc.AddMember("hugepages", hugepages, allocator); + doc.AddMember("memory", memory, allocator); +} +#endif + + void Workers::onReady(void *arg) { auto handle = static_cast(arg); diff --git a/src/workers/Workers.h b/src/workers/Workers.h index f96aa2ca..bbe9a760 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -32,6 +32,7 @@ #include "net/Job.h" #include "net/JobResult.h" +#include "rapidjson/fwd.h" class Handle; @@ -66,6 +67,10 @@ public: static inline void pause() { m_active = false; m_paused = 1; m_sequence++; } static inline void setListener(IJobResultListener *listener) { m_listener = listener; } +# ifndef XMRIG_NO_API + static void threadsSummary(rapidjson::Document &doc); +# endif + private: static void onReady(void *arg); static void onResult(uv_async_t *handle); From 0814c28998624bd86aeacfadaf9fbb4ccbd74240 Mon Sep 17 00:00:00 2001 From: chinarulezzz Date: Tue, 17 Apr 2018 23:12:19 +0300 Subject: [PATCH 216/389] log/Log.cpp: va_list 'args' was opened but not closed by va_end() --- src/log/Log.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/log/Log.cpp b/src/log/Log.cpp index 3e5d5671..c6030f66 100644 --- a/src/log/Log.cpp +++ b/src/log/Log.cpp @@ -47,6 +47,8 @@ void Log::message(Log::Level level, const char* fmt, ...) backend->message(level, fmt, copy); va_end(copy); } + + va_end(args); } From bc67216f7f14aac3f7ebfa102ded0f1278f3867e Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 18 Apr 2018 09:58:06 +0700 Subject: [PATCH 217/389] Added API docs and bug fixes. --- CMakeLists.txt | 2 +- doc/API.md | 53 ++++++++++++++++++++++ doc/api/1/config.json | 63 ++++++++++++++++++++++++++ doc/api/1/summary.json | 73 ++++++++++++++++++++++++++++++ doc/api/1/threads.json | 65 ++++++++++++++++++++++++++ src/common/config/CommonConfig.cpp | 2 +- src/core/ConfigLoader_platform.h | 4 +- src/workers/Workers.cpp | 4 +- 8 files changed, 261 insertions(+), 5 deletions(-) create mode 100644 doc/API.md create mode 100644 doc/api/1/config.json create mode 100644 doc/api/1/summary.json create mode 100644 doc/api/1/threads.json diff --git a/CMakeLists.txt b/CMakeLists.txt index ae2a4ed7..c7017b01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,7 +167,7 @@ endif() add_definitions(/D__STDC_FORMAT_MACROS) add_definitions(/DUNICODE) -add_definitions(/DAPP_DEBUG) +#add_definitions(/DAPP_DEBUG) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") diff --git a/doc/API.md b/doc/API.md new file mode 100644 index 00000000..3357eabb --- /dev/null +++ b/doc/API.md @@ -0,0 +1,53 @@ +# HTTP API + +If you want use API you need choice a port where is internal HTTP server will listen for incoming connections. API will not available if miner built without `libmicrohttpd`. + +Example configuration: + +```json +"api": { + "port": 44444, + "access-token": "TOKEN", + "worker-id": null, + "ipv6": false, + "restricted": false +}, +``` + +* **port** Port for incoming connections `http://:`. +* **access-token** [Bearer](https://gist.github.com/xmrig/c75fdd1f8e0f3bac05500be2ab718f8e#file-api-html-L54) access token to secure access to API. +* **worker-id** Optional worker name, if not set will be detected automatically. +* **ipv6** Enable (`true`) or disable (`false`) IPv6 for API. +* **restricted** Use `false` to allow remote configuration. + +If you prefer use command line options instead of config file, you can use options: `--api-port`, `--api-access-token`, `--api-worker-id`, `--api-ipv6` and `api-no-restricted`. + +## Endpoints + +### GET /1/summary + +Get miner summary information. [Example](api/1/summary.json). + +### GET /1/threads + +Get detailed information about miner threads. [Example](api/1/threads.json). + + +## Restricted endpoints + +All API endpoints below allow access to sensitive information and remote configure miner. You should set `access-token` and allow unrestricted access (`"restricted": false`). + +### GET /1/config + +Get current miner configuration. [Example](api/1/config.json). + + +### PUT /1/config + +Update current miner configuration. Common use case, get current configuration, make changes, and upload it to miner. + +Curl example: + +``` +curl -v --data-binary @config.json -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer SECRET" http://127.0.0.1:44444/1/config +``` \ No newline at end of file diff --git a/doc/api/1/config.json b/doc/api/1/config.json new file mode 100644 index 00000000..2c74cfba --- /dev/null +++ b/doc/api/1/config.json @@ -0,0 +1,63 @@ +{ + "algo": "cryptonight", + "api": { + "port": 44444, + "access-token": "TOKEN", + "worker-id": null, + "ipv6": false, + "restricted": false + }, + "av": 1, + "background": false, + "colors": true, + "cpu-affinity": null, + "cpu-priority": null, + "donate-level": 5, + "huge-pages": true, + "hw-aes": null, + "log-file": null, + "max-cpu-usage": 75, + "pools": [ + { + "url": "pool.monero.hashvault.pro:3333", + "user": "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", + "pass": "x", + "keepalive": false, + "nicehash": false, + "variant": -1 + }, + { + "url": "pool.supportxmr.com:3333", + "user": "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", + "pass": "x", + "keepalive": false, + "nicehash": false, + "variant": -1 + } + ], + "print-time": 60, + "retries": 5, + "retry-pause": 5, + "safe": false, + "threads": [ + { + "low_power_mode": 1, + "affine_to_cpu": 0 + }, + { + "low_power_mode": 1, + "affine_to_cpu": 1 + }, + { + "low_power_mode": 1, + "affine_to_cpu": 2 + }, + { + "low_power_mode": 1, + "affine_to_cpu": 3 + } + ], + "user-agent": null, + "syslog": false, + "watch": false +} \ No newline at end of file diff --git a/doc/api/1/summary.json b/doc/api/1/summary.json new file mode 100644 index 00000000..ed3cd128 --- /dev/null +++ b/doc/api/1/summary.json @@ -0,0 +1,73 @@ +{ + "id": "92f3104f9a2ee78c", + "worker_id": "Ubuntu-1604-xenial-64-minimal", + "version": "2.6.0-beta3", + "kind": "cpu", + "ua": "XMRig/2.6.0-beta3 (Linux x86_64) libuv/1.8.0 gcc/5.4.0", + "cpu": { + "brand": "Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz", + "aes": true, + "x64": true, + "sockets": 1 + }, + "algo": "cryptonight", + "hugepages": true, + "donate_level": 5, + "hashrate": { + "total": [ + 296.24, + 296.23, + 295.97 + ], + "highest": 296.5, + "threads": [ + [ + 73.39, + 73.39, + 73.28 + ], + [ + 74.72, + 74.72, + 74.71 + ], + [ + 74.72, + 74.72, + 74.71 + ], + [ + 73.39, + 73.39, + 73.27 + ] + ] + }, + "results": { + "diff_current": 9990, + "shares_good": 30, + "shares_total": 30, + "avg_time": 31, + "hashes_total": 311833, + "best": [ + 278199, + 181923, + 103717, + 96632, + 56154, + 51580, + 45667, + 33159, + 29581, + 29514 + ], + "error_log": [] + }, + "connection": { + "pool": "pool.monero.hashvault.pro:3333", + "uptime": 953, + "ping": 35, + "failures": 0, + "error_log": [] + } +} \ No newline at end of file diff --git a/doc/api/1/threads.json b/doc/api/1/threads.json new file mode 100644 index 00000000..e536883d --- /dev/null +++ b/doc/api/1/threads.json @@ -0,0 +1,65 @@ +{ + "hugepages": [ + 4, + 4 + ], + "memory": 8388608, + "threads": [ + { + "type": "cpu", + "algo": "cryptonight", + "av": 1, + "low_power_mode": 1, + "affine_to_cpu": 0, + "priority": -1, + "soft_aes": false, + "hashrate": [ + 73.39, + 73.4, + 73.28 + ] + }, + { + "type": "cpu", + "algo": "cryptonight", + "av": 1, + "low_power_mode": 1, + "affine_to_cpu": 1, + "priority": -1, + "soft_aes": false, + "hashrate": [ + 74.72, + 74.72, + 74.7 + ] + }, + { + "type": "cpu", + "algo": "cryptonight", + "av": 1, + "low_power_mode": 1, + "affine_to_cpu": 2, + "priority": -1, + "soft_aes": false, + "hashrate": [ + 74.71, + 74.72, + 74.7 + ] + }, + { + "type": "cpu", + "algo": "cryptonight", + "av": 1, + "low_power_mode": 1, + "affine_to_cpu": 3, + "priority": -1, + "soft_aes": false, + "hashrate": [ + 73.39, + 73.4, + 73.28 + ] + } + ] +} \ No newline at end of file diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 2cce845c..22fd348d 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -205,12 +205,12 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) case SyslogKey: /* --syslog */ case KeepAliveKey: /* --keepalive */ case NicehashKey: /* --nicehash */ + case ApiIPv6Key: /* --api-ipv6 */ return parseBoolean(key, true); case ColorKey: /* --no-color */ case WatchKey: /* --no-watch */ case ApiRestrictedKey: /* --api-no-restricted */ - case ApiIPv6Key: /* --api-no-ipv6 */ return parseBoolean(key, false); case DonateLevelKey: /* --donate-level */ diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index 7e1a4baa..d02a9e8f 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -85,6 +85,8 @@ Options:\n\ --api-port=N port for the miner API\n\ --api-access-token=T access token for API\n\ --api-worker-id=ID custom worker-id for API\n\ + --api-ipv6 enable IPv6 support for API\n\ + --api-no-restricted enable full remote access (only if API token set)\n\ -h, --help display this help and exit\n\ -V, --version output version information and exit\n\ "; @@ -98,7 +100,7 @@ static struct option const options[] = { { "api-access-token", 1, nullptr, xmrig::IConfig::ApiAccessTokenKey }, { "api-port", 1, nullptr, xmrig::IConfig::ApiPort }, { "api-worker-id", 1, nullptr, xmrig::IConfig::ApiWorkerIdKey }, - { "api-no-ipv6", 0, nullptr, xmrig::IConfig::ApiIPv6Key }, + { "api-ipv6", 0, nullptr, xmrig::IConfig::ApiIPv6Key }, { "api-no-restricted", 0, nullptr, xmrig::IConfig::ApiRestrictedKey }, { "av", 1, nullptr, xmrig::IConfig::AVKey }, { "background", 0, nullptr, xmrig::IConfig::BackgroundKey }, diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index e1609c0e..25e1d068 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -193,8 +193,8 @@ void Workers::submit(const JobResult &result) void Workers::threadsSummary(rapidjson::Document &doc) { uv_mutex_lock(&m_mutex); - const size_t pages[2] = { m_status.hugePages, m_status.pages }; - const size_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo); + const uint64_t pages[2] = { m_status.hugePages, m_status.pages }; + const uint64_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo); uv_mutex_unlock(&m_mutex); auto &allocator = doc.GetAllocator(); From ad94e9a7d273df6818e1817b4f04a149b6019e5f Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 19 Apr 2018 11:54:11 +0700 Subject: [PATCH 218/389] Simplify ARM implementation. --- src/Cpu_arm.cpp | 20 ++-- src/Summary.cpp | 33 ++++--- src/crypto/CryptoNight.h | 9 -- src/crypto/CryptoNight_arm.h | 172 +++++++++++------------------------ 4 files changed, 86 insertions(+), 148 deletions(-) diff --git a/src/Cpu_arm.cpp b/src/Cpu_arm.cpp index 1b306789..59ff8421 100644 --- a/src/Cpu_arm.cpp +++ b/src/Cpu_arm.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,16 +28,16 @@ #include "Cpu.h" -char Cpu::m_brand[64] = { 0 }; -int Cpu::m_flags = 0; -int Cpu::m_l2_cache = 0; -int Cpu::m_l3_cache = 0; -int Cpu::m_sockets = 1; -int Cpu::m_totalCores = 0; -int Cpu::m_totalThreads = 0; +char Cpu::m_brand[64] = { 0 }; +int Cpu::m_flags = 0; +int Cpu::m_l2_cache = 0; +int Cpu::m_l3_cache = 0; +int Cpu::m_sockets = 1; +int Cpu::m_totalCores = 0; +size_t Cpu::m_totalThreads = 0; -int Cpu::optimalThreadsCount(xmrig::Algo algo, bool doubleHash, int maxCpuUsage) +size_t Cpu::optimalThreadsCount(size_t size, int maxCpuUsage) { return m_totalThreads; } diff --git a/src/Summary.cpp b/src/Summary.cpp index a54214e4..32a0e4bb 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -93,21 +93,30 @@ static void print_cpu(xmrig::Config *config) static void print_threads(xmrig::Config *config) { - char buf[32]; - if (config->affinity() != -1L) { - snprintf(buf, 32, ", affinity=0x%" PRIX64, config->affinity()); + if (config->threadsMode() != xmrig::Config::Advanced) { + char buf[32]; + if (config->affinity() != -1L) { + snprintf(buf, 32, ", affinity=0x%" PRIX64, config->affinity()); + } + else { + buf[0] = '\0'; + } + + Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mTHREADS: \x1B[01;36m%d\x1B[01;37m, %s, av=%d, %sdonate=%d%%%s" : " * THREADS: %d, %s, av=%d, %sdonate=%d%%%s", + config->threadsCount(), + config->algoName(), + config->algoVariant(), + config->isColors() && config->donateLevel() == 0 ? "\x1B[01;31m" : "", + config->donateLevel(), + buf); } else { - buf[0] = '\0'; + Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mTHREADS: \x1B[01;36m%d\x1B[01;37m, %s, %sdonate=%d%%" : " * THREADS: %d, %s, %sdonate=%d%%", + config->threadsCount(), + config->algoName(), + config->isColors() && config->donateLevel() == 0 ? "\x1B[01;31m" : "", + config->donateLevel()); } - - Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mTHREADS: \x1B[01;36m%d\x1B[01;37m, %s, av=%d, %sdonate=%d%%%s" : " * THREADS: %d, %s, av=%d, %sdonate=%d%%%s", - config->threadsCount(), - config->algoName(), - config->algoVariant(), - config->isColors() && config->donateLevel() == 0 ? "\x1B[01;31m" : "", - config->donateLevel(), - buf); } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 5a4a266d..e8e86dc4 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -30,15 +30,6 @@ #include -#define AEON_MEMORY 1048576 -#define AEON_MASK 0xFFFF0 -#define AEON_ITER 0x40000 - -#define MONERO_MEMORY 2097152 -#define MONERO_MASK 0x1FFFF0 -#define MONERO_ITER 0x80000 - - struct cryptonight_ctx { alignas(16) uint8_t state[200]; alignas(16) uint8_t* memory; diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 101a1f58..fd8c1920 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -73,6 +73,13 @@ static inline __attribute__((always_inline)) __m128i _mm_set_epi64x(const uint64 } +static inline __attribute__((always_inline)) __m128i _mm_aesenc_si128(__m128i v, __m128i rkey) +{ + alignas(16) const __m128i zero = { 0 }; + return veorq_u8(vaesmcq_u8(vaeseq_u8(v, zero)), rkey ); +} + + /* this one was not implemented yet so here it is */ static inline __attribute__((always_inline)) uint64_t _mm_cvtsi128_si64(__m128i a) { @@ -155,19 +162,19 @@ static inline void aes_genkey(const __m128i* memory, __m128i* k0, __m128i* k1, _ *k0 = xout0; *k1 = xout2; - SOFT_AES ? soft_aes_genkey_sub<0x01>(&xout0, &xout2) : soft_aes_genkey_sub<0x01>(&xout0, &xout2); + soft_aes_genkey_sub<0x01>(&xout0, &xout2); *k2 = xout0; *k3 = xout2; - SOFT_AES ? soft_aes_genkey_sub<0x02>(&xout0, &xout2) : soft_aes_genkey_sub<0x02>(&xout0, &xout2); + soft_aes_genkey_sub<0x02>(&xout0, &xout2); *k4 = xout0; *k5 = xout2; - SOFT_AES ? soft_aes_genkey_sub<0x04>(&xout0, &xout2) : soft_aes_genkey_sub<0x04>(&xout0, &xout2); + soft_aes_genkey_sub<0x04>(&xout0, &xout2); *k6 = xout0; *k7 = xout2; - SOFT_AES ? soft_aes_genkey_sub<0x08>(&xout0, &xout2) : soft_aes_genkey_sub<0x08>(&xout0, &xout2); + soft_aes_genkey_sub<0x08>(&xout0, &xout2); *k8 = xout0; *k9 = xout2; } @@ -186,18 +193,16 @@ static inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, *x6 = soft_aesenc((uint32_t*)x6, key); *x7 = soft_aesenc((uint32_t*)x7, key); } -# ifndef XMRIG_ARMv7 else { - *x0 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x0), key)); - *x1 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x1), key)); - *x2 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x2), key)); - *x3 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x3), key)); - *x4 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x4), key)); - *x5 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x5), key)); - *x6 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x6), key)); - *x7 = vaesmcq_u8(vaeseq_u8(*((uint8x16_t *) x7), key)); + *x0 = _mm_aesenc_si128(*x0, key); + *x1 = _mm_aesenc_si128(*x1, key); + *x2 = _mm_aesenc_si128(*x2, key); + *x3 = _mm_aesenc_si128(*x3, key); + *x4 = _mm_aesenc_si128(*x4, key); + *x5 = _mm_aesenc_si128(*x5, key); + *x6 = _mm_aesenc_si128(*x6, key); + *x7 = _mm_aesenc_si128(*x7, key); } -# endif } @@ -234,10 +239,6 @@ static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { for (size_t i = 0; i < 16; i++) { - if (!SOFT_AES) { - aes_round(_mm_setzero_si128(), &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); - } - aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); @@ -247,30 +248,13 @@ static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) aes_round(k6, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k7, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k8, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); - - if (!SOFT_AES) { - xin0 ^= k9; - xin1 ^= k9; - xin2 ^= k9; - xin3 ^= k9; - xin4 ^= k9; - xin5 ^= k9; - xin6 ^= k9; - xin7 ^= k9; - } - else { - aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); - } + aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); mix_and_propagate(xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7); } } for (size_t i = 0; i < MEM / sizeof(__m128i); i += 8) { - if (!SOFT_AES) { - aes_round(_mm_setzero_si128(), &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); - } - aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); @@ -280,20 +264,7 @@ static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) aes_round(k6, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k7, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k8, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); - - if (!SOFT_AES) { - xin0 ^= k9; - xin1 ^= k9; - xin2 ^= k9; - xin3 ^= k9; - xin4 ^= k9; - xin5 ^= k9; - xin6 ^= k9; - xin7 ^= k9; - } - else { - aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); - } + aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); _mm_store_si128(output + i + 0, xin0); _mm_store_si128(output + i + 1, xin1); @@ -335,10 +306,6 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) xout6 = _mm_xor_si128(_mm_load_si128(input + i + 6), xout6); xout7 = _mm_xor_si128(_mm_load_si128(input + i + 7), xout7); - if (!SOFT_AES) { - aes_round(_mm_setzero_si128(), &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); - } - aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); @@ -348,20 +315,7 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); - - if (!SOFT_AES) { - xout0 ^= k9; - xout1 ^= k9; - xout2 ^= k9; - xout3 ^= k9; - xout4 ^= k9; - xout5 ^= k9; - xout6 ^= k9; - xout7 ^= k9; - } - else { - aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); - } + aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { mix_and_propagate(xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7); @@ -379,10 +333,6 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) xout6 = _mm_xor_si128(_mm_load_si128(input + i + 6), xout6); xout7 = _mm_xor_si128(_mm_load_si128(input + i + 7), xout7); - if (!SOFT_AES) { - aes_round(_mm_setzero_si128(), &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); - } - aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); @@ -392,29 +342,12 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); - - if (!SOFT_AES) { - xout0 ^= k9; - xout1 ^= k9; - xout2 ^= k9; - xout3 ^= k9; - xout4 ^= k9; - xout5 ^= k9; - xout6 ^= k9; - xout7 ^= k9; - } - else { - aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); - } + aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); mix_and_propagate(xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7); } for (size_t i = 0; i < 16; i++) { - if (!SOFT_AES) { - aes_round(_mm_setzero_si128(), &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); - } - aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); @@ -424,20 +357,7 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); - - if (!SOFT_AES) { - xout0 ^= k9; - xout1 ^= k9; - xout2 ^= k9; - xout3 ^= k9; - xout4 ^= k9; - xout5 ^= k9; - xout6 ^= k9; - xout7 ^= k9; - } - else { - aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); - } + aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); mix_and_propagate(xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7); } @@ -454,6 +374,21 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } +static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) +{ + mem_out[0] = EXTRACT64(tmp); + + uint64_t vh = vgetq_lane_u64(tmp, 1); + + uint8_t x = vh >> 24; + static const uint16_t table = 0x7531; + const uint8_t index = (((x >> 3) & 6) | (x & 1)) << 1; + vh ^= ((table >> index) & 0x3) << 28; + + mem_out[1] = vh; +} + + template inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { @@ -489,13 +424,15 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } else { cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); -# ifndef XMRIG_ARMv7 - cx = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah0, al0); -# endif + cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); + } + + if (VARIANT > 0) { + cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + } else { + _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } - _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); - VARIANT1_1(&l0[idx0 & MASK]); idx0 = EXTRACT64(cx); bx0 = cx; @@ -580,16 +517,17 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si else { cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); -# ifndef XMRIG_ARMv7 - cx0 = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx0, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah0, al0); - cx1 = vreinterpretq_m128i_u8(vaesmcq_u8(vaeseq_u8(cx1, vdupq_n_u8(0)))) ^ _mm_set_epi64x(ah1, al1); -# endif + cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); + cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); } - _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); - _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); - VARIANT1_1(&l0[idx0 & MASK]); - VARIANT1_1(&l1[idx1 & MASK]); + if (VARIANT > 0) { + cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); + cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + } else { + _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); + _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + }; idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); From e119f7f40217bc7ef2da412bf1dbd5977b0c95c7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 19 Apr 2018 13:50:06 +0700 Subject: [PATCH 219/389] Rearrange test vectors, for catch cn-heavy bug. --- src/crypto/CryptoNight_test.h | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 52dd2965..93cbf23d 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -27,16 +27,16 @@ const static uint8_t test_input[380] = { - 0x01, 0x00, 0xFB, 0x8E, 0x8A, 0xC8, 0x05, 0x89, 0x93, 0x23, 0x37, 0x1B, 0xB7, 0x90, 0xDB, 0x19, - 0x21, 0x8A, 0xFD, 0x8D, 0xB8, 0xE3, 0x75, 0x5D, 0x8B, 0x90, 0xF3, 0x9B, 0x3D, 0x55, 0x06, 0xA9, - 0xAB, 0xCE, 0x4F, 0xA9, 0x12, 0x24, 0x45, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x81, 0x46, 0xD4, 0x9F, - 0xA9, 0x3E, 0xE7, 0x24, 0xDE, 0xB5, 0x7D, 0x12, 0xCB, 0xC6, 0xC6, 0xF3, 0xB9, 0x24, 0xD9, 0x46, - 0x12, 0x7C, 0x7A, 0x97, 0x41, 0x8F, 0x93, 0x48, 0x82, 0x8F, 0x0F, 0x02, 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, 0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01, + 0x01, 0x00, 0xFB, 0x8E, 0x8A, 0xC8, 0x05, 0x89, 0x93, 0x23, 0x37, 0x1B, 0xB7, 0x90, 0xDB, 0x19, + 0x21, 0x8A, 0xFD, 0x8D, 0xB8, 0xE3, 0x75, 0x5D, 0x8B, 0x90, 0xF3, 0x9B, 0x3D, 0x55, 0x06, 0xA9, + 0xAB, 0xCE, 0x4F, 0xA9, 0x12, 0x24, 0x45, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x81, 0x46, 0xD4, 0x9F, + 0xA9, 0x3E, 0xE7, 0x24, 0xDE, 0xB5, 0x7D, 0x12, 0xCB, 0xC6, 0xC6, 0xF3, 0xB9, 0x24, 0xD9, 0x46, + 0x12, 0x7C, 0x7A, 0x97, 0x41, 0x8F, 0x93, 0x48, 0x82, 0x8F, 0x0F, 0x02, 0x07, 0x07, 0xB4, 0x87, 0xD0, 0xD6, 0x05, 0x26, 0xE0, 0xC6, 0xDD, 0x9B, 0xC7, 0x18, 0xC3, 0xCF, 0x52, 0x04, 0xBD, 0x4F, 0x9B, 0x27, 0xF6, 0x73, 0xB9, 0x3F, 0xEF, 0x7B, 0xB2, 0xF7, 0x2B, 0xBB, 0x3F, 0x3E, 0x9C, 0x3E, 0x9D, 0x33, 0x1E, 0xDE, 0xAD, 0xBE, 0xEF, 0x4E, 0x00, 0x91, 0x81, 0x29, @@ -56,10 +56,10 @@ const static uint8_t test_input[380] = { const static uint8_t test_output_v0[160] = { - 0x1B, 0x60, 0x6A, 0x3F, 0x4A, 0x07, 0xD6, 0x48, 0x9A, 0x1B, 0xCD, 0x07, 0x69, 0x7B, 0xD1, 0x66, - 0x96, 0xB6, 0x1C, 0x8A, 0xE9, 0x82, 0xF6, 0x1A, 0x90, 0x16, 0x0F, 0x4E, 0x52, 0x82, 0x8A, 0x7F, 0x1A, 0x3F, 0xFB, 0xEE, 0x90, 0x9B, 0x42, 0x0D, 0x91, 0xF7, 0xBE, 0x6E, 0x5F, 0xB5, 0x6D, 0xB7, 0x1B, 0x31, 0x10, 0xD8, 0x86, 0x01, 0x1E, 0x87, 0x7E, 0xE5, 0x78, 0x6A, 0xFD, 0x08, 0x01, 0x00, + 0x1B, 0x60, 0x6A, 0x3F, 0x4A, 0x07, 0xD6, 0x48, 0x9A, 0x1B, 0xCD, 0x07, 0x69, 0x7B, 0xD1, 0x66, + 0x96, 0xB6, 0x1C, 0x8A, 0xE9, 0x82, 0xF6, 0x1A, 0x90, 0x16, 0x0F, 0x4E, 0x52, 0x82, 0x8A, 0x7F, 0xA1, 0xB4, 0xFA, 0xE3, 0xE5, 0x76, 0xCE, 0xCF, 0xB7, 0x9C, 0xAF, 0x3E, 0x29, 0x92, 0xE4, 0xE0, 0x31, 0x24, 0x05, 0x48, 0xBF, 0x8D, 0x5F, 0x7B, 0x11, 0x03, 0x60, 0xAA, 0xD7, 0x50, 0x3F, 0x0C, 0x2D, 0x30, 0xF3, 0x87, 0x4F, 0x86, 0xA1, 0x4A, 0xB5, 0xA2, 0x1A, 0x08, 0xD0, 0x44, 0x2C, 0x9D, @@ -71,10 +71,10 @@ const static uint8_t test_output_v0[160] = { // Monero v7 const static uint8_t test_output_v1[160] = { - 0xC9, 0xFA, 0xE8, 0x42, 0x5D, 0x86, 0x88, 0xDC, 0x23, 0x6B, 0xCD, 0xBC, 0x42, 0xFD, 0xB4, 0x2D, - 0x37, 0x6C, 0x6E, 0xC1, 0x90, 0x50, 0x1A, 0xA8, 0x4B, 0x04, 0xA4, 0xB4, 0xCF, 0x1E, 0xE1, 0x22, 0xF2, 0x2D, 0x3D, 0x62, 0x03, 0xD2, 0xA0, 0x8B, 0x41, 0xD9, 0x02, 0x72, 0x78, 0xD8, 0xBC, 0xC9, - 0x83, 0xAC, 0xAD, 0xA9, 0xB6, 0x8E, 0x52, 0xE3, 0xC6, 0x89, 0x69, 0x2A, 0x50, 0xE9, 0x21, 0xD9, + 0x83, 0xAC, 0xAD, 0xA9, 0xB6, 0x8E, 0x52, 0xE3, 0xC6, 0x89, 0x69, 0x2A, 0x50, 0xE9, 0x21, 0xD9, + 0xC9, 0xFA, 0xE8, 0x42, 0x5D, 0x86, 0x88, 0xDC, 0x23, 0x6B, 0xCD, 0xBC, 0x42, 0xFD, 0xB4, 0x2D, + 0x37, 0x6C, 0x6E, 0xC1, 0x90, 0x50, 0x1A, 0xA8, 0x4B, 0x04, 0xA4, 0xB4, 0xCF, 0x1E, 0xE1, 0x22, 0xE7, 0x8C, 0x5A, 0x6E, 0x38, 0x30, 0x68, 0x4A, 0x73, 0xFC, 0x1B, 0xC6, 0x6D, 0xFC, 0x8D, 0x98, 0xB4, 0xC2, 0x23, 0x39, 0xAD, 0xE0, 0x9D, 0xF6, 0x6D, 0x8C, 0x6A, 0xAA, 0xF9, 0xB2, 0xE3, 0x4C, 0xB6, 0x90, 0x6C, 0xE6, 0x15, 0x5E, 0x46, 0x07, 0x9C, 0xB2, 0x6B, 0xAC, 0x3B, 0xAC, 0x1A, 0xDE, @@ -86,10 +86,10 @@ const static uint8_t test_output_v1[160] = { #ifndef XMRIG_NO_AEON const static uint8_t test_output_v0_lite[160] = { - 0x28, 0xA2, 0x2B, 0xAD, 0x3F, 0x93, 0xD1, 0x40, 0x8F, 0xCA, 0x47, 0x2E, 0xB5, 0xAD, 0x1C, 0xBE, - 0x75, 0xF2, 0x1D, 0x05, 0x3C, 0x8C, 0xE5, 0xB3, 0xAF, 0x10, 0x5A, 0x57, 0x71, 0x3E, 0x21, 0xDD, 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, 0x00, 0x4E, 0xEC, 0xE0, 0x9B, 0x83, 0xA7, 0x2E, 0xF6, 0xBA, 0x98, 0x64, 0xD3, 0x51, 0x0C, 0x88, + 0x28, 0xA2, 0x2B, 0xAD, 0x3F, 0x93, 0xD1, 0x40, 0x8F, 0xCA, 0x47, 0x2E, 0xB5, 0xAD, 0x1C, 0xBE, + 0x75, 0xF2, 0x1D, 0x05, 0x3C, 0x8C, 0xE5, 0xB3, 0xAF, 0x10, 0x5A, 0x57, 0x71, 0x3E, 0x21, 0xDD, 0x38, 0x08, 0xE1, 0x17, 0x0B, 0x99, 0x8D, 0x1A, 0x3C, 0xCE, 0x35, 0xC5, 0xC7, 0x3A, 0x00, 0x2E, 0xCB, 0x54, 0xF0, 0x78, 0x2E, 0x9E, 0xDB, 0xC7, 0xDF, 0x2E, 0x71, 0x9A, 0x16, 0x97, 0xC4, 0x18, 0x4B, 0x97, 0x07, 0xFE, 0x5D, 0x98, 0x9A, 0xD6, 0xD8, 0xE5, 0x92, 0x66, 0x87, 0x7F, 0x19, 0x37, @@ -101,10 +101,10 @@ const static uint8_t test_output_v0_lite[160] = { // AEON v7 const static uint8_t test_output_v1_lite[160] = { - 0x87, 0xC4, 0xE5, 0x70, 0x65, 0x3E, 0xB4, 0xC2, 0xB4, 0x2B, 0x7A, 0x0D, 0x54, 0x65, 0x59, 0x45, - 0x2D, 0xFA, 0xB5, 0x73, 0xB8, 0x2E, 0xC5, 0x2F, 0x15, 0x2B, 0x7F, 0xF9, 0x8E, 0x79, 0x44, 0x6F, 0x6D, 0x8C, 0xDC, 0x44, 0x4E, 0x9B, 0xBB, 0xFD, 0x68, 0xFC, 0x43, 0xFC, 0xD4, 0x85, 0x5B, 0x22, 0x8C, 0x8A, 0x1B, 0xD9, 0x1D, 0x9D, 0x00, 0x28, 0x5B, 0xEC, 0x02, 0xB7, 0xCA, 0x2D, 0x67, 0x41, + 0x87, 0xC4, 0xE5, 0x70, 0x65, 0x3E, 0xB4, 0xC2, 0xB4, 0x2B, 0x7A, 0x0D, 0x54, 0x65, 0x59, 0x45, + 0x2D, 0xFA, 0xB5, 0x73, 0xB8, 0x2E, 0xC5, 0x2F, 0x15, 0x2B, 0x7F, 0xF9, 0x8E, 0x79, 0x44, 0x6F, 0x16, 0x08, 0x74, 0xC7, 0xA2, 0xD2, 0xA3, 0x97, 0x95, 0x76, 0xCA, 0x4D, 0x06, 0x39, 0x7A, 0xAB, 0x6C, 0x87, 0x58, 0x33, 0x4D, 0xC8, 0x5A, 0xAB, 0x04, 0x27, 0xFE, 0x8B, 0x1C, 0x23, 0x2F, 0x32, 0xC0, 0x44, 0xFF, 0x0D, 0xB5, 0x3B, 0x27, 0x96, 0x06, 0x89, 0x7B, 0xA3, 0x0B, 0xD0, 0xCE, 0x9E, @@ -117,10 +117,10 @@ const static uint8_t test_output_v1_lite[160] = { #ifndef XMRIG_NO_SUMO const static uint8_t test_output_heavy[160] = { - 0x4D, 0x94, 0x7D, 0xD6, 0xDB, 0x6E, 0x07, 0x48, 0x26, 0x4A, 0x51, 0x2E, 0xAC, 0xF3, 0x25, 0x4A, - 0x1F, 0x1A, 0xA2, 0x5B, 0xFC, 0x0A, 0xAD, 0x82, 0xDE, 0xA8, 0x99, 0x96, 0x88, 0x52, 0xD2, 0x7D, 0x99, 0x83, 0xF2, 0x1B, 0xDF, 0x20, 0x10, 0xA8, 0xD7, 0x07, 0xBB, 0x2F, 0x14, 0xD7, 0x86, 0x64, 0xBB, 0xE1, 0x18, 0x7F, 0x55, 0x01, 0x4B, 0x39, 0xE5, 0xF3, 0xD6, 0x93, 0x28, 0xE4, 0x8F, 0xC2, + 0x4D, 0x94, 0x7D, 0xD6, 0xDB, 0x6E, 0x07, 0x48, 0x26, 0x4A, 0x51, 0x2E, 0xAC, 0xF3, 0x25, 0x4A, + 0x1F, 0x1A, 0xA2, 0x5B, 0xFC, 0x0A, 0xAD, 0x82, 0xDE, 0xA8, 0x99, 0x96, 0x88, 0x52, 0xD2, 0x7D, 0x3E, 0xE1, 0x23, 0x03, 0x5A, 0x63, 0x7B, 0x66, 0xF6, 0xD7, 0xC2, 0x2A, 0x34, 0x5E, 0x88, 0xE7, 0xFA, 0xC4, 0x25, 0x36, 0x54, 0xCB, 0xD2, 0x5C, 0x2F, 0x80, 0x2A, 0xF9, 0xCC, 0x43, 0xF7, 0xCD, 0xE5, 0x18, 0xA8, 0x05, 0x60, 0x18, 0xA5, 0x73, 0x72, 0x9B, 0x32, 0xDC, 0x69, 0x83, 0xC1, 0xE1, From 14576f599c3e4113704e1457d97badcbbb8bb89e Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 19 Apr 2018 19:44:17 +0700 Subject: [PATCH 220/389] Fix ARMv7 build. --- src/crypto/CryptoNight_arm.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index fd8c1920..746de79c 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -73,11 +73,17 @@ static inline __attribute__((always_inline)) __m128i _mm_set_epi64x(const uint64 } +#ifdef XMRIG_ARMv8 static inline __attribute__((always_inline)) __m128i _mm_aesenc_si128(__m128i v, __m128i rkey) { alignas(16) const __m128i zero = { 0 }; return veorq_u8(vaesmcq_u8(vaeseq_u8(v, zero)), rkey ); } +#else +static inline __attribute__((always_inline)) __m128i _mm_aesenc_si128(__m128i v, __m128i rkey) +{ +} +#endif /* this one was not implemented yet so here it is */ From fe1649a2c13254769cdc6f5e3ce9f7babca89f77 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 19 Apr 2018 20:29:23 +0700 Subject: [PATCH 221/389] Revert old Client::close. --- src/net/Client.cpp | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index c4387a7c..436bcd1a 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -120,12 +120,7 @@ void Client::deleteLater() m_listener = nullptr; - if (state() == HostLookupState) { - uv_cancel(reinterpret_cast(&m_resolver)); - return; - } - - if (!disconnect() && m_state != ClosingState) { + if (!disconnect()) { m_storage.remove(m_key); } } @@ -206,28 +201,7 @@ bool Client::close() setState(ClosingState); - uv_stream_t *stream = reinterpret_cast(m_socket); - - if (uv_is_readable(stream) == 1) { - uv_read_stop(stream); - } - - if (uv_is_writable(stream) == 1) { - const int rc = uv_shutdown(new uv_shutdown_t, stream, [](uv_shutdown_t* req, int status) { - if (uv_is_closing(reinterpret_cast(req->handle)) == 0) { - uv_close(reinterpret_cast(req->handle), Client::onClose); - } - - delete req; - }); - - assert(rc == 0); - - if (rc != 0) { - onClose(); - } - } - else { + if (uv_is_closing(reinterpret_cast(m_socket)) == 0) { uv_close(reinterpret_cast(m_socket), Client::onClose); } From 91dd5fe68aa3ed970053227f0e08948c4842214a Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 19 Apr 2018 20:57:23 +0700 Subject: [PATCH 222/389] v2.6.0-beta3 --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index 4ea70a57..c6c42e46 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.6.0-beta2" +#define APP_VERSION "2.6.0-beta3" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -36,7 +36,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 6 #define APP_VER_BUILD 0 -#define APP_VER_REV 2 +#define APP_VER_REV 3 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From 3f85b11e12f3c444747bfd84c51a17b1a6330d2a Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 19 Apr 2018 21:19:00 +0700 Subject: [PATCH 223/389] Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71d4e5b3..63090a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v2.6.0-beta3 +- [#563](https://github.com/xmrig/xmrig/issues/563) **Added [advanced threads mode](https://github.com/xmrig/xmrig/issues/563), now possible configure each thread individually.** +- [#255](https://github.com/xmrig/xmrig/issues/563) Low power mode extended to **triple**, **quard** and **penta** modes. +- [#519](https://github.com/xmrig/xmrig/issues/519) Fixed high donation levels, improved donation start time randomization. +- [#554](https://github.com/xmrig/xmrig/issues/554) Fixed regression with `print-time` option. + # v2.6.0-beta2 - Improved performance for `cryptonight v7` especially in double hash mode. - [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 disabled for internal HTTP API by default, was causing issues on some systems. From 2d2e60a1971d8a9a608ace74bf88f3a1bc2d3a8f Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 20 Apr 2018 10:14:33 +0700 Subject: [PATCH 224/389] Fix x86 build. --- src/crypto/CryptoNight_x86.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 3dafe724..e19eb2c9 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -803,10 +803,10 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size __m128i cx3 = _mm_set_epi64x(0, 0); uint64_t idx0, idx1, idx2, idx3; - idx0 = _mm_cvtsi128_si64(ax0); - idx1 = _mm_cvtsi128_si64(ax1); - idx2 = _mm_cvtsi128_si64(ax2); - idx3 = _mm_cvtsi128_si64(ax3); + idx0 = EXTRACT64(ax0); + idx1 = EXTRACT64(ax1); + idx2 = EXTRACT64(ax2); + idx3 = EXTRACT64(ax3); for (size_t i = 0; i < ITERATIONS / 2; i++) { @@ -915,11 +915,11 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz __m128i cx4 = _mm_set_epi64x(0, 0); uint64_t idx0, idx1, idx2, idx3, idx4; - idx0 = _mm_cvtsi128_si64(ax0); - idx1 = _mm_cvtsi128_si64(ax1); - idx2 = _mm_cvtsi128_si64(ax2); - idx3 = _mm_cvtsi128_si64(ax3); - idx4 = _mm_cvtsi128_si64(ax4); + idx0 = EXTRACT64(ax0); + idx1 = EXTRACT64(ax1); + idx2 = EXTRACT64(ax2); + idx3 = EXTRACT64(ax3); + idx4 = EXTRACT64(ax4); for (size_t i = 0; i < ITERATIONS / 2; i++) { From 78e2c122020421b8d9af67df86d632afb406ab58 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 20 Apr 2018 10:36:41 +0700 Subject: [PATCH 225/389] Fix msvc build. --- src/net/Pool.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/net/Pool.cpp b/src/net/Pool.cpp index 4d1d7439..2e167ebd 100644 --- a/src/net/Pool.cpp +++ b/src/net/Pool.cpp @@ -32,7 +32,8 @@ #ifdef _MSC_VER -# define strncasecmp(x,y,z) _strnicmp(x,y,z) +# define strncasecmp _strnicmp +# define strcasecmp _stricmp #endif From a9178bd46808328a522b37a71f316d8200b4a051 Mon Sep 17 00:00:00 2001 From: xmrig Date: Fri, 20 Apr 2018 11:13:00 +0700 Subject: [PATCH 226/389] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 010a03fc..3d438c5a 100644 --- a/README.md +++ b/README.md @@ -124,10 +124,10 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -232af0c5f3b1cdbc2d90b514873a764b434d5621d2790da67954b35c17e44fe3 xmrig-2.6.0-beta2-xenial-amd64.tar.gz/xmrig-2.6.0-beta2/xmrig -2366a06729d4de538ef511862bf11d0c7ad40fd245e7aeab3c1957307d63471a xmrig-2.6.0-beta2-gcc-win32.zip/xmrig.exe -2f6538c765e001d13ca380cbc1558d51efcb97d4bccdfa40993cb872be4e9efd xmrig-2.6.0-beta2-gcc-win64.zip/xmrig.exe -3c0479acb78a3cee8fe416ee438dbff09c786acf50fbaf28a820127fcd0c6e62 xmrig-2.6.0-beta2-msvc-win64.zip/xmrig.exe +6b32fefb356b27caa2180be17755d09639f0654096a1a0c61e8f10f4f2ac1626 xmrig-2.6.0-beta3-xenial-amd64.tar.gz/xmrig-2.6.0-beta3/xmrig +c8b40f4d7aac9d0cc7c3d02cadeac91b0bdb034aae2fb6b415d503272eabb987 xmrig-2.6.0-beta3-gcc-win32.zip/xmrig.exe +32cf7958f97a23296186e38a4addb9cf234adcbd9c7914b15e4209a87fe272e7 xmrig-2.6.0-beta3-gcc-win64.zip/xmrig.exe +4f176ee4c4be52701edc0d573cf9647c4aacbb4ff14975b80b98e6fe63068542 xmrig-2.6.0-beta3-msvc-win64.zip/xmrig.exe ``` ## Contacts From 2d22f2aeffe052b3a13ac95169a0eb737dbb41bb Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 20 Apr 2018 13:44:30 +0700 Subject: [PATCH 227/389] Move shared network code to common folder. --- CMakeLists.txt | 28 +++++++++---------- src/App.cpp | 2 +- src/Summary.cpp | 2 +- src/api/NetworkState.cpp | 6 ++-- src/{ => common}/Console.cpp | 6 ++-- src/{ => common}/Console.h | 4 +-- src/common/config/CommonConfig.cpp | 1 - src/common/config/CommonConfig.h | 2 +- src/common/config/ConfigLoader.cpp | 2 +- src/{ => common}/net/Client.cpp | 2 +- src/{ => common}/net/Client.h | 8 +++--- src/{ => common}/net/Id.h | 0 src/{ => common}/net/Pool.cpp | 2 +- src/{ => common}/net/Pool.h | 0 src/{ => common}/net/Storage.h | 0 src/{ => common}/net/SubmitResult.cpp | 9 +++--- src/{ => common}/net/SubmitResult.h | 9 +++--- .../net/strategies/FailoverStrategy.cpp | 4 +-- .../net/strategies/FailoverStrategy.h | 2 +- .../net/strategies/SinglePoolStrategy.cpp | 4 +-- .../net/strategies/SinglePoolStrategy.h | 0 src/core/Config.cpp | 1 - src/log/Log.cpp | 2 ++ src/net/Job.h | 2 +- src/net/Network.cpp | 12 ++++---- src/net/strategies/DonateStrategy.cpp | 4 +-- src/net/strategies/DonateStrategy.h | 2 +- src/workers/CpuThread.cpp | 2 +- 28 files changed, 60 insertions(+), 58 deletions(-) rename src/{ => common}/Console.cpp (90%) rename src/{ => common}/Console.h (88%) rename src/{ => common}/net/Client.cpp (99%) rename src/{ => common}/net/Client.h (97%) rename src/{ => common}/net/Id.h (100%) rename src/{ => common}/net/Pool.cpp (99%) rename src/{ => common}/net/Pool.h (100%) rename src/{ => common}/net/Storage.h (100%) rename src/{ => common}/net/SubmitResult.cpp (82%) rename src/{ => common}/net/SubmitResult.h (80%) rename src/{ => common}/net/strategies/FailoverStrategy.cpp (97%) rename src/{ => common}/net/strategies/FailoverStrategy.h (98%) rename src/{ => common}/net/strategies/SinglePoolStrategy.cpp (96%) rename src/{ => common}/net/strategies/SinglePoolStrategy.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c7017b01..e8dc0ba0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,11 +17,18 @@ set(HEADERS src/common/config/CommonConfig.h src/common/config/ConfigLoader.h src/common/config/ConfigWatcher.h + src/common/Console.h + src/common/net/Client.h + src/common/net/Id.h + src/common/net/Pool.h + src/common/net/Storage.h + src/common/net/strategies/FailoverStrategy.h + src/common/net/strategies/SinglePoolStrategy.h + src/common/net/SubmitResult.h src/common/Platform.h src/common/utils/c_str.h src/common/utils/mm_malloc.h src/common/xmrig.h - src/Console.h src/core/Config.cpp src/core/ConfigLoader_platform.h src/core/Controller.h @@ -42,17 +49,10 @@ set(HEADERS src/log/FileLog.h src/log/Log.h src/Mem.h - src/net/Client.h - src/net/Id.h src/net/Job.h src/net/JobResult.h src/net/Network.h - src/net/Pool.h - src/net/Storage.h src/net/strategies/DonateStrategy.h - src/net/strategies/FailoverStrategy.h - src/net/strategies/SinglePoolStrategy.h - src/net/SubmitResult.h src/Summary.h src/version.h src/workers/CpuThread.h @@ -91,22 +91,22 @@ set(SOURCES src/common/config/CommonConfig.cpp src/common/config/ConfigLoader.cpp src/common/config/ConfigWatcher.cpp + src/common/Console.cpp + src/common/net/Client.cpp + src/common/net/Pool.cpp + src/common/net/strategies/FailoverStrategy.cpp + src/common/net/strategies/SinglePoolStrategy.cpp + src/common/net/SubmitResult.cpp src/common/Platform.cpp - src/Console.cpp src/core/Config.cpp src/core/Controller.cpp src/log/ConsoleLog.cpp src/log/FileLog.cpp src/log/Log.cpp src/Mem.cpp - src/net/Client.cpp src/net/Job.cpp src/net/Network.cpp - src/net/Pool.cpp src/net/strategies/DonateStrategy.cpp - src/net/strategies/FailoverStrategy.cpp - src/net/strategies/SinglePoolStrategy.cpp - src/net/SubmitResult.cpp src/Summary.cpp src/workers/CpuThread.cpp src/workers/Handle.cpp diff --git a/src/App.cpp b/src/App.cpp index 78d6b328..e7447634 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -28,8 +28,8 @@ #include "api/Api.h" #include "App.h" +#include "common/Console.h" #include "common/Platform.h" -#include "Console.h" #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" diff --git a/src/Summary.cpp b/src/Summary.cpp index 32a0e4bb..12108d18 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -27,12 +27,12 @@ #include +#include "common/net/Pool.h" #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" #include "log/Log.h" #include "Mem.h" -#include "net/Pool.h" #include "Summary.h" #include "version.h" diff --git a/src/api/NetworkState.cpp b/src/api/NetworkState.cpp index d3ffddd3..0ab80093 100644 --- a/src/api/NetworkState.cpp +++ b/src/api/NetworkState.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,7 +29,7 @@ #include "api/NetworkState.h" -#include "net/SubmitResult.h" +#include "common/net/SubmitResult.h" NetworkState::NetworkState() : diff --git a/src/Console.cpp b/src/common/Console.cpp similarity index 90% rename from src/Console.cpp rename to src/common/Console.cpp index 3d95ada4..350fb139 100644 --- a/src/Console.cpp +++ b/src/common/Console.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,7 +22,7 @@ */ -#include "Console.h" +#include "common/Console.h" #include "interfaces/IConsoleListener.h" diff --git a/src/Console.h b/src/common/Console.h similarity index 88% rename from src/Console.h rename to src/common/Console.h index bde95d7d..7f2e3cc9 100644 --- a/src/Console.h +++ b/src/common/Console.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 22fd348d..7327b50c 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -32,7 +32,6 @@ #include "common/config/CommonConfig.h" #include "donate.h" #include "log/Log.h" -#include "net/Pool.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 5a229269..015ba04e 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -28,10 +28,10 @@ #include +#include "common/net/Pool.h" #include "common/utils/c_str.h" #include "common/xmrig.h" #include "interfaces/IConfig.h" -#include "net/Pool.h" namespace xmrig { diff --git a/src/common/config/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp index 47c6cdf9..cc92213f 100644 --- a/src/common/config/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -34,12 +34,12 @@ #include "common/config/ConfigLoader.h" #include "common/config/ConfigWatcher.h" +#include "common/net/Pool.h" #include "common/Platform.h" #include "core/ConfigCreator.h" #include "core/ConfigLoader_platform.h" #include "interfaces/IConfig.h" #include "interfaces/IWatcherListener.h" -#include "net/Pool.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" #include "rapidjson/filereadstream.h" diff --git a/src/net/Client.cpp b/src/common/net/Client.cpp similarity index 99% rename from src/net/Client.cpp rename to src/common/net/Client.cpp index 436bcd1a..c335634b 100644 --- a/src/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -29,9 +29,9 @@ #include +#include "common/net/Client.h" #include "interfaces/IClientListener.h" #include "log/Log.h" -#include "net/Client.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" #include "rapidjson/stringbuffer.h" diff --git a/src/net/Client.h b/src/common/net/Client.h similarity index 97% rename from src/net/Client.h rename to src/common/net/Client.h index 0692d38b..502fd4cc 100644 --- a/src/net/Client.h +++ b/src/common/net/Client.h @@ -30,11 +30,11 @@ #include -#include "net/Id.h" +#include "common/net/Id.h" +#include "common/net/Pool.h" +#include "common/net/Storage.h" +#include "common/net/SubmitResult.h" #include "net/Job.h" -#include "net/Storage.h" -#include "net/SubmitResult.h" -#include "net/Pool.h" #include "rapidjson/fwd.h" diff --git a/src/net/Id.h b/src/common/net/Id.h similarity index 100% rename from src/net/Id.h rename to src/common/net/Id.h diff --git a/src/net/Pool.cpp b/src/common/net/Pool.cpp similarity index 99% rename from src/net/Pool.cpp rename to src/common/net/Pool.cpp index 2e167ebd..ddf31031 100644 --- a/src/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -28,7 +28,7 @@ #include -#include "net/Pool.h" +#include "common/net/Pool.h" #ifdef _MSC_VER diff --git a/src/net/Pool.h b/src/common/net/Pool.h similarity index 100% rename from src/net/Pool.h rename to src/common/net/Pool.h diff --git a/src/net/Storage.h b/src/common/net/Storage.h similarity index 100% rename from src/net/Storage.h rename to src/common/net/Storage.h diff --git a/src/net/SubmitResult.cpp b/src/common/net/SubmitResult.cpp similarity index 82% rename from src/net/SubmitResult.cpp rename to src/common/net/SubmitResult.cpp index 2e81017c..251b2bf1 100644 --- a/src/net/SubmitResult.cpp +++ b/src/common/net/SubmitResult.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,10 +25,11 @@ #include -#include "net/SubmitResult.h" +#include "common/net/SubmitResult.h" -SubmitResult::SubmitResult(int64_t seq, uint32_t diff, uint64_t actualDiff) : +SubmitResult::SubmitResult(int64_t seq, uint32_t diff, uint64_t actualDiff, int64_t reqId) : + reqId(reqId), seq(seq), diff(diff), actualDiff(actualDiff), diff --git a/src/net/SubmitResult.h b/src/common/net/SubmitResult.h similarity index 80% rename from src/net/SubmitResult.h rename to src/common/net/SubmitResult.h index 8eddef89..e812cbf8 100644 --- a/src/net/SubmitResult.h +++ b/src/common/net/SubmitResult.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,11 +31,12 @@ class SubmitResult { public: - inline SubmitResult() : seq(0), diff(0), actualDiff(0), elapsed(0), start(0) {} - SubmitResult(int64_t seq, uint32_t diff, uint64_t actualDiff); + inline SubmitResult() : reqId(0), seq(0), diff(0), actualDiff(0), elapsed(0), start(0) {} + SubmitResult(int64_t seq, uint32_t diff, uint64_t actualDiff, int64_t reqId = 0); void done(); + int64_t reqId; int64_t seq; uint32_t diff; uint64_t actualDiff; diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/common/net/strategies/FailoverStrategy.cpp similarity index 97% rename from src/net/strategies/FailoverStrategy.cpp rename to src/common/net/strategies/FailoverStrategy.cpp index cef9da0c..58854498 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/common/net/strategies/FailoverStrategy.cpp @@ -22,10 +22,10 @@ */ +#include "common/net/Client.h" +#include "common/net/strategies/FailoverStrategy.h" #include "common/Platform.h" #include "interfaces/IStrategyListener.h" -#include "net/Client.h" -#include "net/strategies/FailoverStrategy.h" FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : diff --git a/src/net/strategies/FailoverStrategy.h b/src/common/net/strategies/FailoverStrategy.h similarity index 98% rename from src/net/strategies/FailoverStrategy.h rename to src/common/net/strategies/FailoverStrategy.h index a48496fb..f86b366a 100644 --- a/src/net/strategies/FailoverStrategy.h +++ b/src/common/net/strategies/FailoverStrategy.h @@ -28,9 +28,9 @@ #include +#include "common/net/Pool.h" #include "interfaces/IClientListener.h" #include "interfaces/IStrategy.h" -#include "net/Pool.h" class Client; diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/common/net/strategies/SinglePoolStrategy.cpp similarity index 96% rename from src/net/strategies/SinglePoolStrategy.cpp rename to src/common/net/strategies/SinglePoolStrategy.cpp index c74a794b..50620ab2 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/common/net/strategies/SinglePoolStrategy.cpp @@ -22,10 +22,10 @@ */ +#include "common/net/Client.h" +#include "common/net/strategies/SinglePoolStrategy.h" #include "common/Platform.h" #include "interfaces/IStrategyListener.h" -#include "net/Client.h" -#include "net/strategies/SinglePoolStrategy.h" SinglePoolStrategy::SinglePoolStrategy(const Pool &pool, int retryPause, IStrategyListener *listener, bool quiet) : diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/common/net/strategies/SinglePoolStrategy.h similarity index 100% rename from src/net/strategies/SinglePoolStrategy.h rename to src/common/net/strategies/SinglePoolStrategy.h diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 4d48af29..33cd01e8 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -31,7 +31,6 @@ #include "core/ConfigCreator.h" #include "Cpu.h" #include "crypto/CryptoNight_constants.h" -#include "net/Pool.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" diff --git a/src/log/Log.cpp b/src/log/Log.cpp index 131faa54..0e38634d 100644 --- a/src/log/Log.cpp +++ b/src/log/Log.cpp @@ -50,6 +50,8 @@ void Log::message(Log::Level level, const char* fmt, ...) va_end(copy); } + va_end(args); + uv_mutex_unlock(&m_mutex); } diff --git a/src/net/Job.h b/src/net/Job.h index ee4728e3..e632a26d 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -30,8 +30,8 @@ #include +#include "common/net/Id.h" #include "common/xmrig.h" -#include "net/Id.h" class Job diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 98ce7e8d..1fe02a35 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -31,16 +31,16 @@ #include "api/Api.h" +#include "common/net/Client.h" +#include "common/net/strategies/FailoverStrategy.h" +#include "common/net/strategies/SinglePoolStrategy.h" +#include "common/net/SubmitResult.h" +#include "core/Config.h" +#include "core/Controller.h" #include "log/Log.h" -#include "net/Client.h" #include "net/Network.h" #include "net/strategies/DonateStrategy.h" -#include "net/strategies/FailoverStrategy.h" -#include "net/strategies/SinglePoolStrategy.h" -#include "net/SubmitResult.h" #include "workers/Workers.h" -#include "core/Controller.h" -#include "core/Config.h" Network::Network(xmrig::Controller *controller) : diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 27beae33..3176ee52 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -22,13 +22,13 @@ */ +#include "common/net/Client.h" +#include "common/net/strategies/FailoverStrategy.h" #include "common/Platform.h" #include "common/xmrig.h" #include "interfaces/IStrategyListener.h" -#include "net/Client.h" #include "net/Job.h" #include "net/strategies/DonateStrategy.h" -#include "net/strategies/FailoverStrategy.h" extern "C" diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index cad360b1..edb15c8f 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -29,10 +29,10 @@ #include +#include "common/net/Pool.h" #include "interfaces/IClientListener.h" #include "interfaces/IStrategy.h" #include "interfaces/IStrategyListener.h" -#include "net/Pool.h" class Client; diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 3632e193..be24b2d5 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -24,7 +24,7 @@ #include -#include "net/Pool.h" +#include "common/net/Pool.h" #include "rapidjson/document.h" #include "workers/CpuThread.h" From 98e7308597f6fae8a44ac3024a2171b9f1b99cf0 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 20 Apr 2018 14:45:51 +0700 Subject: [PATCH 228/389] Move keccak to common code. --- CMakeLists.txt | 4 +- src/api/ApiRouter.cpp | 9 +--- .../c_keccak.c => common/crypto/keccak.cpp} | 50 ++++++++++++++----- src/common/crypto/keccak.h | 49 ++++++++++++++++++ src/crypto/CryptoNight_x86.h | 26 +++++----- src/crypto/c_keccak.h | 26 ---------- src/net/strategies/DonateStrategy.cpp | 9 +--- 7 files changed, 105 insertions(+), 68 deletions(-) rename src/{crypto/c_keccak.c => common/crypto/keccak.cpp} (74%) create mode 100644 src/common/crypto/keccak.h delete mode 100644 src/crypto/c_keccak.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e8dc0ba0..06f0bef9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ set(HEADERS src/common/config/ConfigLoader.h src/common/config/ConfigWatcher.h src/common/Console.h + src/common/crypto/keccak.h src/common/net/Client.h src/common/net/Id.h src/common/net/Pool.h @@ -67,7 +68,6 @@ set(HEADERS_CRYPTO src/crypto/c_blake256.h src/crypto/c_groestl.h src/crypto/c_jh.h - src/crypto/c_keccak.h src/crypto/c_skein.h src/crypto/CryptoNight.h src/crypto/CryptoNight_constants.h @@ -92,6 +92,7 @@ set(SOURCES src/common/config/ConfigLoader.cpp src/common/config/ConfigWatcher.cpp src/common/Console.cpp + src/common/crypto/keccak.cpp src/common/net/Client.cpp src/common/net/Pool.cpp src/common/net/strategies/FailoverStrategy.cpp @@ -118,7 +119,6 @@ set(SOURCES ) set(SOURCES_CRYPTO - src/crypto/c_keccak.c src/crypto/c_groestl.c src/crypto/c_blake256.c src/crypto/c_jh.c diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 42256cc9..cfe5ed9f 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -35,6 +35,7 @@ #include "api/ApiRouter.h" #include "common/api/HttpReply.h" #include "common/api/HttpRequest.h" +#include "common/crypto/keccak.h" #include "common/Platform.h" #include "core/Config.h" #include "core/Controller.h" @@ -50,12 +51,6 @@ #include "workers/Workers.h" -extern "C" -{ -#include "crypto/c_keccak.h" -} - - static inline double normalize(double d) { if (!isnormal(d)) { @@ -171,7 +166,7 @@ void ApiRouter::genId() memcpy(input, interfaces[i].phys_addr, addrSize); memcpy(input + addrSize, APP_KIND, strlen(APP_KIND)); - keccak(input, static_cast(inSize), hash, sizeof(hash)); + xmrig::keccak(input, inSize, hash); Job::toHex(hash, 8, m_id); delete [] input; diff --git a/src/crypto/c_keccak.c b/src/common/crypto/keccak.cpp similarity index 74% rename from src/crypto/c_keccak.c rename to src/common/crypto/keccak.cpp index 997db241..ecfe7700 100644 --- a/src/crypto/c_keccak.c +++ b/src/common/crypto/keccak.cpp @@ -1,10 +1,35 @@ -// keccak.c -// 19-Nov-11 Markku-Juhani O. Saarinen -// A baseline Keccak (3rd round) implementation. +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2011 Markku-Juhani O. Saarinen + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include #include + +#include "common/crypto/keccak.h" + + #define HASH_DATA_AREA 136 #define KECCAK_ROUNDS 24 @@ -26,7 +51,7 @@ const uint64_t keccakf_rndc[24] = // update the state with given number of rounds -void keccakf(uint64_t st[25], int rounds) +void xmrig::keccakf(uint64_t st[25], int rounds) { int i, j, round; uint64_t t, bc[5]; @@ -139,7 +164,8 @@ void keccakf(uint64_t st[25], int rounds) // compute a keccak hash (md) of given byte length from "in" typedef uint64_t state_t[25]; -void keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen) + +void xmrig::keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen) { state_t st; uint8_t temp[144]; @@ -151,9 +177,11 @@ void keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen) memset(st, 0, sizeof(st)); for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz) { - for (i = 0; i < rsizw; i++) + for (i = 0; i < rsizw; i++) { st[i] ^= ((uint64_t *) in)[i]; - keccakf(st, KECCAK_ROUNDS); + } + + xmrig::keccakf(st, KECCAK_ROUNDS); } // last block and padding @@ -162,15 +190,11 @@ void keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen) memset(temp + inlen, 0, rsiz - inlen); temp[rsiz - 1] |= 0x80; - for (i = 0; i < rsizw; i++) + for (i = 0; i < rsizw; i++) { st[i] ^= ((uint64_t *) temp)[i]; + } keccakf(st, KECCAK_ROUNDS); memcpy(md, st, mdlen); } - -void keccak1600(const uint8_t *in, int inlen, uint8_t *md) -{ - keccak(in, inlen, md, sizeof(state_t)); -} diff --git a/src/common/crypto/keccak.h b/src/common/crypto/keccak.h new file mode 100644 index 00000000..0413ec2d --- /dev/null +++ b/src/common/crypto/keccak.h @@ -0,0 +1,49 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2011 Markku-Juhani O. Saarinen + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef KECCAK_H_ +#define KECCAK_H_ + +#include +#include + + +namespace xmrig { + +// compute a keccak hash (md) of given byte length from "in" +void keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen); + + +inline void keccak(const uint8_t *in, size_t inlen, uint8_t *md) +{ + keccak(in, static_cast(inlen), md, 200); +} + +// update the state +void keccakf(uint64_t st[25], int norounds); + +} /* namespace xmrig */ + +#endif /* KECCAK_H_ */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index e19eb2c9..a1651cc3 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -34,6 +34,7 @@ #endif +#include "common/crypto/keccak.h" #include "crypto/CryptoNight.h" #include "crypto/CryptoNight_constants.h" #include "crypto/CryptoNight_monero.h" @@ -42,7 +43,6 @@ extern "C" { -#include "crypto/c_keccak.h" #include "crypto/c_groestl.h" #include "crypto/c_blake256.h" #include "crypto/c_jh.h" @@ -414,7 +414,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si return; } - keccak(input, (int) size, ctx[0]->state, 200); + xmrig::keccak(input, size, ctx[0]->state); VARIANT1_INIT(0) @@ -478,7 +478,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si cn_implode_scratchpad((__m128i*) ctx[0]->memory, (__m128i*) ctx[0]->state); - keccakf(h0, 24); + xmrig::keccakf(h0, 24); extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); } @@ -495,8 +495,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si return; } - keccak(input, (int) size, ctx[0]->state, 200); - keccak(input + size, (int) size, ctx[1]->state, 200); + xmrig::keccak(input, size, ctx[0]->state); + xmrig::keccak(input + size, size, ctx[1]->state); VARIANT1_INIT(0); VARIANT1_INIT(1); @@ -603,8 +603,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); - keccakf(h0, 24); - keccakf(h1, 24); + xmrig::keccakf(h0, 24); + xmrig::keccakf(h1, 24); extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); extra_hashes[ctx[1]->state[0] & 3](ctx[1]->state, 200, output + 32); @@ -681,7 +681,7 @@ inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t si } for (size_t i = 0; i < 3; i++) { - keccak(input + size * i, static_cast(size), ctx[i]->state, 200); + xmrig::keccak(input + size * i, size, ctx[i]->state); cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->state), reinterpret_cast<__m128i*>(ctx[i]->memory)); } @@ -752,7 +752,7 @@ inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t si for (size_t i = 0; i < 3; i++) { cn_implode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->memory), reinterpret_cast<__m128i*>(ctx[i]->state)); - keccakf(reinterpret_cast(ctx[i]->state), 24); + xmrig::keccakf(reinterpret_cast(ctx[i]->state), 24); extra_hashes[ctx[i]->state[0] & 3](ctx[i]->state, 200, output + 32 * i); } } @@ -771,7 +771,7 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size } for (size_t i = 0; i < 4; i++) { - keccak(input + size * i, static_cast(size), ctx[i]->state, 200); + xmrig::keccak(input + size * i, size, ctx[i]->state); cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->state), reinterpret_cast<__m128i*>(ctx[i]->memory)); } @@ -858,7 +858,7 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size for (size_t i = 0; i < 4; i++) { cn_implode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->memory), reinterpret_cast<__m128i*>(ctx[i]->state)); - keccakf(reinterpret_cast(ctx[i]->state), 24); + xmrig::keccakf(reinterpret_cast(ctx[i]->state), 24); extra_hashes[ctx[i]->state[0] & 3](ctx[i]->state, 200, output + 32 * i); } } @@ -877,7 +877,7 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz } for (size_t i = 0; i < 5; i++) { - keccak(input + size * i, static_cast(size), ctx[i]->state, 200); + xmrig::keccak(input + size * i, size, ctx[i]->state); cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->state), reinterpret_cast<__m128i*>(ctx[i]->memory)); } @@ -979,7 +979,7 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz for (size_t i = 0; i < 5; i++) { cn_implode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->memory), reinterpret_cast<__m128i*>(ctx[i]->state)); - keccakf(reinterpret_cast(ctx[i]->state), 24); + xmrig::keccakf(reinterpret_cast(ctx[i]->state), 24); extra_hashes[ctx[i]->state[0] & 3](ctx[i]->state, 200, output + 32 * i); } } diff --git a/src/crypto/c_keccak.h b/src/crypto/c_keccak.h deleted file mode 100644 index 4f7f8572..00000000 --- a/src/crypto/c_keccak.h +++ /dev/null @@ -1,26 +0,0 @@ -// keccak.h -// 19-Nov-11 Markku-Juhani O. Saarinen - -#ifndef KECCAK_H -#define KECCAK_H - -#include -#include - -#ifndef KECCAK_ROUNDS -#define KECCAK_ROUNDS 24 -#endif - -#ifndef ROTL64 -#define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y)))) -#endif - -// compute a keccak hash (md) of given byte length from "in" -int keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen); - -// update the state -void keccakf(uint64_t st[25], int norounds); - -void keccak1600(const uint8_t *in, int inlen, uint8_t *md); - -#endif diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 3176ee52..9b4a60c7 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -22,6 +22,7 @@ */ +#include "common/crypto/keccak.h" #include "common/net/Client.h" #include "common/net/strategies/FailoverStrategy.h" #include "common/Platform.h" @@ -31,12 +32,6 @@ #include "net/strategies/DonateStrategy.h" -extern "C" -{ -#include "crypto/c_keccak.h" -} - - const static char *kDonatePool1 = "miner.fee.xmrig.com"; const static char *kDonatePool2 = "emergency.fee.xmrig.com"; @@ -56,7 +51,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL uint8_t hash[200]; char userId[65] = { 0 }; - keccak(reinterpret_cast(user), static_cast(strlen(user)), hash, sizeof(hash)); + xmrig::keccak(reinterpret_cast(user), strlen(user), hash); Job::toHex(hash, 32, userId); if (algo == xmrig::CRYPTONIGHT) { From 36a612af9a2972174ff3c4bc68317369bc389d71 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 20 Apr 2018 18:54:58 +0700 Subject: [PATCH 229/389] Move logging code to common folder. --- CMakeLists.txt | 14 +++++++------- src/App.cpp | 4 +--- src/Mem_win.cpp | 2 +- src/Summary.cpp | 2 +- src/common/api/Httpd.cpp | 2 +- src/common/config/CommonConfig.cpp | 2 +- src/common/config/ConfigWatcher.cpp | 2 +- src/common/crypto/keccak.cpp | 2 +- src/{ => common}/log/ConsoleLog.cpp | 4 ++-- src/{ => common}/log/ConsoleLog.h | 0 src/{ => common}/log/FileLog.cpp | 2 +- src/{ => common}/log/FileLog.h | 0 src/{ => common}/log/Log.cpp | 2 +- src/{ => common}/log/Log.h | 0 src/{ => common}/log/SysLog.cpp | 2 +- src/{ => common}/log/SysLog.h | 0 src/common/net/Client.cpp | 2 +- src/common/net/Storage.h | 3 ++- src/core/Controller.cpp | 6 +++--- src/net/Network.cpp | 2 +- src/workers/Hashrate.cpp | 2 +- src/workers/Workers.cpp | 2 +- 22 files changed, 28 insertions(+), 29 deletions(-) rename src/{ => common}/log/ConsoleLog.cpp (98%) rename src/{ => common}/log/ConsoleLog.h (100%) rename src/{ => common}/log/FileLog.cpp (98%) rename src/{ => common}/log/FileLog.h (100%) rename src/{ => common}/log/Log.cpp (98%) rename src/{ => common}/log/Log.h (100%) rename src/{ => common}/log/SysLog.cpp (97%) rename src/{ => common}/log/SysLog.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 06f0bef9..e6c2d2d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,9 +46,9 @@ set(HEADERS src/interfaces/IThread.h src/interfaces/IWatcherListener.h src/interfaces/IWorker.h - src/log/ConsoleLog.h - src/log/FileLog.h - src/log/Log.h + src/common/log/ConsoleLog.h + src/common/log/FileLog.h + src/common/log/Log.h src/Mem.h src/net/Job.h src/net/JobResult.h @@ -101,9 +101,9 @@ set(SOURCES src/common/Platform.cpp src/core/Config.cpp src/core/Controller.cpp - src/log/ConsoleLog.cpp - src/log/FileLog.cpp - src/log/Log.cpp + src/common/log/ConsoleLog.cpp + src/common/log/FileLog.cpp + src/common/log/Log.cpp src/Mem.cpp src/net/Job.cpp src/net/Network.cpp @@ -194,7 +194,7 @@ endif() CHECK_INCLUDE_FILE (syslog.h HAVE_SYSLOG_H) if (HAVE_SYSLOG_H) add_definitions(/DHAVE_SYSLOG_H) - set(SOURCES_SYSLOG src/log/SysLog.h src/log/SysLog.cpp) + set(SOURCES_SYSLOG src/common/log/SysLog.h src/common/log/SysLog.cpp) endif() if (NOT WITH_AEON) diff --git a/src/App.cpp b/src/App.cpp index e7447634..adcc5752 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -29,14 +29,12 @@ #include "api/Api.h" #include "App.h" #include "common/Console.h" +#include "common/log/Log.h" #include "common/Platform.h" #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" #include "crypto/CryptoNight.h" -#include "log/ConsoleLog.h" -#include "log/FileLog.h" -#include "log/Log.h" #include "Mem.h" #include "net/Network.h" #include "Summary.h" diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 9a81b039..2bfcc3b0 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -29,11 +29,11 @@ #include +#include "common/log/Log.h" #include "common/utils/mm_malloc.h" #include "common/xmrig.h" #include "crypto/CryptoNight.h" #include "crypto/CryptoNight_constants.h" -#include "log/Log.h" #include "Mem.h" diff --git a/src/Summary.cpp b/src/Summary.cpp index 12108d18..38ceac8e 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -27,11 +27,11 @@ #include +#include "common/log/Log.h" #include "common/net/Pool.h" #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" -#include "log/Log.h" #include "Mem.h" #include "Summary.h" #include "version.h" diff --git a/src/common/api/Httpd.cpp b/src/common/api/Httpd.cpp index 135ac7c9..0cab01bf 100644 --- a/src/common/api/Httpd.cpp +++ b/src/common/api/Httpd.cpp @@ -30,7 +30,7 @@ #include "common/api/Httpd.h" #include "common/api/HttpReply.h" #include "common/api/HttpRequest.h" -#include "log/Log.h" +#include "common/log/Log.h" Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) : diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 7327b50c..75906621 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -30,8 +30,8 @@ #include "common/config/CommonConfig.h" +#include "common/log/Log.h" #include "donate.h" -#include "log/Log.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" diff --git a/src/common/config/ConfigWatcher.cpp b/src/common/config/ConfigWatcher.cpp index 21d73188..a08b2be7 100644 --- a/src/common/config/ConfigWatcher.cpp +++ b/src/common/config/ConfigWatcher.cpp @@ -27,9 +27,9 @@ #include "common/config/ConfigLoader.h" #include "common/config/ConfigWatcher.h" +#include "common/log/Log.h" #include "core/ConfigCreator.h" #include "interfaces/IWatcherListener.h" -#include "log/Log.h" xmrig::ConfigWatcher::ConfigWatcher(const char *path, IConfigCreator *creator, IWatcherListener *listener) : diff --git a/src/common/crypto/keccak.cpp b/src/common/crypto/keccak.cpp index ecfe7700..0219ce36 100644 --- a/src/common/crypto/keccak.cpp +++ b/src/common/crypto/keccak.cpp @@ -183,7 +183,7 @@ void xmrig::keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen) xmrig::keccakf(st, KECCAK_ROUNDS); } - + // last block and padding memcpy(temp, in, inlen); temp[inlen++] = 1; diff --git a/src/log/ConsoleLog.cpp b/src/common/log/ConsoleLog.cpp similarity index 98% rename from src/log/ConsoleLog.cpp rename to src/common/log/ConsoleLog.cpp index eeed7355..b7d187d1 100644 --- a/src/log/ConsoleLog.cpp +++ b/src/common/log/ConsoleLog.cpp @@ -34,10 +34,10 @@ #endif +#include "common/log/ConsoleLog.h" +#include "common/log/Log.h" #include "core/Config.h" #include "core/Controller.h" -#include "log/ConsoleLog.h" -#include "log/Log.h" ConsoleLog::ConsoleLog(xmrig::Controller *controller) : diff --git a/src/log/ConsoleLog.h b/src/common/log/ConsoleLog.h similarity index 100% rename from src/log/ConsoleLog.h rename to src/common/log/ConsoleLog.h diff --git a/src/log/FileLog.cpp b/src/common/log/FileLog.cpp similarity index 98% rename from src/log/FileLog.cpp rename to src/common/log/FileLog.cpp index 5eeb252c..c8eaf5f7 100644 --- a/src/log/FileLog.cpp +++ b/src/common/log/FileLog.cpp @@ -29,7 +29,7 @@ #include -#include "log/FileLog.h" +#include "common/log/FileLog.h" FileLog::FileLog(const char *fileName) diff --git a/src/log/FileLog.h b/src/common/log/FileLog.h similarity index 100% rename from src/log/FileLog.h rename to src/common/log/FileLog.h diff --git a/src/log/Log.cpp b/src/common/log/Log.cpp similarity index 98% rename from src/log/Log.cpp rename to src/common/log/Log.cpp index 0e38634d..03237f03 100644 --- a/src/log/Log.cpp +++ b/src/common/log/Log.cpp @@ -29,8 +29,8 @@ #include +#include "common/log/Log.h" #include "interfaces/ILogBackend.h" -#include "log/Log.h" Log *Log::m_self = nullptr; diff --git a/src/log/Log.h b/src/common/log/Log.h similarity index 100% rename from src/log/Log.h rename to src/common/log/Log.h diff --git a/src/log/SysLog.cpp b/src/common/log/SysLog.cpp similarity index 97% rename from src/log/SysLog.cpp rename to src/common/log/SysLog.cpp index f9b16cca..70879c33 100644 --- a/src/log/SysLog.cpp +++ b/src/common/log/SysLog.cpp @@ -25,7 +25,7 @@ #include -#include "log/SysLog.h" +#include "common/log/SysLog.h" #include "version.h" diff --git a/src/log/SysLog.h b/src/common/log/SysLog.h similarity index 100% rename from src/log/SysLog.h rename to src/common/log/SysLog.h diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index c335634b..2c28c89a 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -29,9 +29,9 @@ #include +#include "common/log/Log.h" #include "common/net/Client.h" #include "interfaces/IClientListener.h" -#include "log/Log.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" #include "rapidjson/stringbuffer.h" diff --git a/src/common/net/Storage.h b/src/common/net/Storage.h index 105547ec..752e9512 100644 --- a/src/common/net/Storage.h +++ b/src/common/net/Storage.h @@ -28,7 +28,8 @@ #include #include -#include "log/Log.h" + +#include "common/log/Log.h" namespace xmrig { diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index 5f0a9bb3..d0babe47 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -26,14 +26,14 @@ #include "common/config/ConfigLoader.h" +#include "common/log/ConsoleLog.h" +#include "common/log/FileLog.h" +#include "common/log/Log.h" #include "common/Platform.h" #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" #include "interfaces/IControllerListener.h" -#include "log/ConsoleLog.h" -#include "log/FileLog.h" -#include "log/Log.h" #include "net/Network.h" diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 1fe02a35..5f6d73ee 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -31,13 +31,13 @@ #include "api/Api.h" +#include "common/log/Log.h" #include "common/net/Client.h" #include "common/net/strategies/FailoverStrategy.h" #include "common/net/strategies/SinglePoolStrategy.h" #include "common/net/SubmitResult.h" #include "core/Config.h" #include "core/Controller.h" -#include "log/Log.h" #include "net/Network.h" #include "net/strategies/DonateStrategy.h" #include "workers/Workers.h" diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index 7d2e6a4c..60d3a6a2 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -29,9 +29,9 @@ #include +#include "common/log/Log.h" #include "core/Config.h" #include "core/Controller.h" -#include "log/Log.h" #include "workers/Hashrate.h" diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 25e1d068..1d2c1739 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -26,12 +26,12 @@ #include "api/Api.h" +#include "common/log/Log.h" #include "core/Config.h" #include "core/Controller.h" #include "crypto/CryptoNight_constants.h" #include "interfaces/IJobResultListener.h" #include "interfaces/IThread.h" -#include "log/Log.h" #include "Mem.h" #include "rapidjson/document.h" #include "workers/Handle.h" From 8fe264bbd79251ed2f62f2ade40fdb5f707723d2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 20 Apr 2018 23:44:32 +0700 Subject: [PATCH 230/389] Move Job to common. --- CMakeLists.txt | 16 ++++----- src/api/ApiRouter.cpp | 2 +- src/common/net/Client.cpp | 19 ++-------- src/common/net/Client.h | 2 +- src/{ => common}/net/Job.cpp | 37 +++++++++---------- src/{ => common}/net/Job.h | 51 ++++++++++++++++----------- src/net/JobResult.h | 2 +- src/net/strategies/DonateStrategy.cpp | 2 +- src/workers/MultiWorker.h | 2 +- src/workers/Workers.h | 2 +- 10 files changed, 63 insertions(+), 72 deletions(-) rename src/{ => common}/net/Job.cpp (90%) rename src/{ => common}/net/Job.h (57%) diff --git a/CMakeLists.txt b/CMakeLists.txt index e6c2d2d3..21d0726b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,8 +19,12 @@ set(HEADERS src/common/config/ConfigWatcher.h src/common/Console.h src/common/crypto/keccak.h + src/common/log/ConsoleLog.h + src/common/log/FileLog.h + src/common/log/Log.h src/common/net/Client.h src/common/net/Id.h + src/common/net/Job.h src/common/net/Pool.h src/common/net/Storage.h src/common/net/strategies/FailoverStrategy.h @@ -46,11 +50,7 @@ set(HEADERS src/interfaces/IThread.h src/interfaces/IWatcherListener.h src/interfaces/IWorker.h - src/common/log/ConsoleLog.h - src/common/log/FileLog.h - src/common/log/Log.h src/Mem.h - src/net/Job.h src/net/JobResult.h src/net/Network.h src/net/strategies/DonateStrategy.h @@ -93,7 +93,11 @@ set(SOURCES src/common/config/ConfigWatcher.cpp src/common/Console.cpp src/common/crypto/keccak.cpp + src/common/log/ConsoleLog.cpp + src/common/log/FileLog.cpp + src/common/log/Log.cpp src/common/net/Client.cpp + src/common/net/Job.cpp src/common/net/Pool.cpp src/common/net/strategies/FailoverStrategy.cpp src/common/net/strategies/SinglePoolStrategy.cpp @@ -101,11 +105,7 @@ set(SOURCES src/common/Platform.cpp src/core/Config.cpp src/core/Controller.cpp - src/common/log/ConsoleLog.cpp - src/common/log/FileLog.cpp - src/common/log/Log.cpp src/Mem.cpp - src/net/Job.cpp src/net/Network.cpp src/net/strategies/DonateStrategy.cpp src/Summary.cpp diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index cfe5ed9f..a9e32b18 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -36,13 +36,13 @@ #include "common/api/HttpReply.h" #include "common/api/HttpRequest.h" #include "common/crypto/keccak.h" +#include "common/net/Job.h" #include "common/Platform.h" #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" #include "interfaces/IThread.h" #include "Mem.h" -#include "net/Job.h" #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" #include "rapidjson/stringbuffer.h" diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index 2c28c89a..a110b2ce 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -32,19 +32,13 @@ #include "common/log/Log.h" #include "common/net/Client.h" #include "interfaces/IClientListener.h" +#include "net/JobResult.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" -#ifdef XMRIG_PROXY_PROJECT -# include "proxy/JobResult.h" -#else -# include "net/JobResult.h" -#endif - - #ifdef _MSC_VER # define strncasecmp(x,y,z) _strnicmp(x,y,z) #endif @@ -238,12 +232,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } -# ifdef XMRIG_PROXY_PROJECT - Job job(m_id, m_pool.variant()); - job.setClientId(m_rpcId); -# else - Job job(m_id, m_nicehash, m_pool.algo(), m_pool.variant()); -# endif + Job job(m_id, m_nicehash, m_pool.algo(), m_pool.variant(), m_rpcId); if (!job.setId(params["job_id"].GetString())) { *code = 3; @@ -260,10 +249,6 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } - if (params.HasMember("coin")) { - job.setCoin(params["coin"].GetString()); - } - if (params.HasMember("variant")) { job.setVariant(params["variant"].GetInt()); } diff --git a/src/common/net/Client.h b/src/common/net/Client.h index 502fd4cc..651f97b5 100644 --- a/src/common/net/Client.h +++ b/src/common/net/Client.h @@ -31,10 +31,10 @@ #include "common/net/Id.h" +#include "common/net/Job.h" #include "common/net/Pool.h" #include "common/net/Storage.h" #include "common/net/SubmitResult.h" -#include "net/Job.h" #include "rapidjson/fwd.h" diff --git a/src/net/Job.cpp b/src/common/net/Job.cpp similarity index 90% rename from src/net/Job.cpp rename to src/common/net/Job.cpp index 1434c87f..c5f9902c 100644 --- a/src/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -27,7 +27,7 @@ #include -#include "net/Job.h" +#include "common/net/Job.h" static inline unsigned char hf_hex2bin(char c, bool &err) @@ -59,31 +59,30 @@ static inline char hf_bin2hex(unsigned char c) Job::Job() : m_nicehash(false), - m_coin(), - m_algo(xmrig::CRYPTONIGHT), m_poolId(-2), m_threadId(-1), m_size(0), m_diff(0), m_target(0), m_blob(), + m_algo(xmrig::INVALID_ALGO), m_variant(xmrig::VARIANT_AUTO) { } -Job::Job(int poolId, bool nicehash, int algo, int variant) : +Job::Job(int poolId, bool nicehash, xmrig::Algo algo, xmrig::Variant variant, const xmrig::Id &clientId) : m_nicehash(nicehash), - m_coin(), - m_algo(algo), m_poolId(poolId), m_threadId(-1), m_size(0), m_diff(0), m_target(0), - m_blob() + m_blob(), + m_algo(algo), + m_clientId(clientId), + m_variant(variant) { - setVariant(variant); } @@ -116,6 +115,11 @@ bool Job::setBlob(const char *blob) m_nicehash = true; } +# ifdef XMRIG_PROXY_PROJECT + memset(m_rawBlob, 0, sizeof(m_rawBlob)); + memcpy(m_rawBlob, blob, m_size * 2); +# endif + return true; } @@ -152,23 +156,16 @@ bool Job::setTarget(const char *target) return false; } +# ifdef XMRIG_PROXY_PROJECT + memset(m_rawTarget, 0, sizeof(m_rawTarget)); + memcpy(m_rawTarget, target, len); +# endif + m_diff = toDiff(m_target); return true; } -void Job::setCoin(const char *coin) -{ - if (!coin || strlen(coin) > 4) { - memset(m_coin, 0, sizeof(m_coin)); - return; - } - - strncpy(m_coin, coin, sizeof(m_coin)); - m_algo = strcmp(m_coin, "AEON") == 0 ? xmrig::CRYPTONIGHT_LITE : xmrig::CRYPTONIGHT; -} - - void Job::setVariant(int variant) { switch (variant) { diff --git a/src/net/Job.h b/src/common/net/Job.h similarity index 57% rename from src/net/Job.h rename to src/common/net/Job.h index e632a26d..4eb7018e 100644 --- a/src/net/Job.h +++ b/src/common/net/Job.h @@ -38,31 +38,35 @@ class Job { public: Job(); - Job(int poolId, bool nicehash, int algo, int variant); + Job(int poolId, bool nicehash, xmrig::Algo algo, xmrig::Variant variant, const xmrig::Id &clientId); ~Job(); bool setBlob(const char *blob); bool setTarget(const char *target); - void setCoin(const char *coin); void setVariant(int variant); - inline bool isNicehash() const { return m_nicehash; } - inline bool isValid() const { return m_size > 0 && m_diff > 0; } - inline bool setId(const char *id) { return m_id.setId(id); } - inline const char *coin() const { return m_coin; } - inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } - inline const uint8_t *blob() const { return m_blob; } - inline const xmrig::Id &id() const { return m_id; } - inline int poolId() const { return m_poolId; } - inline int threadId() const { return m_threadId; } - inline size_t size() const { return m_size; } - inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } - inline uint32_t diff() const { return (uint32_t) m_diff; } - inline uint64_t target() const { return m_target; } - inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } - inline void setPoolId(int poolId) { m_poolId = poolId; } - inline void setThreadId(int threadId) { m_threadId = threadId; } - inline xmrig::Variant variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? xmrig::VARIANT_V1 : xmrig::VARIANT_NONE) : m_variant); } + inline bool isNicehash() const { return m_nicehash; } + inline bool isValid() const { return m_size > 0 && m_diff > 0; } + inline bool setId(const char *id) { return m_id.setId(id); } + inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } + inline const uint8_t *blob() const { return m_blob; } + inline const xmrig::Id &clientId() const { return m_clientId; } + inline const xmrig::Id &id() const { return m_id; } + inline int poolId() const { return m_poolId; } + inline int threadId() const { return m_threadId; } + inline size_t size() const { return m_size; } + inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } + inline uint32_t diff() const { return static_cast(m_diff); } + inline uint64_t target() const { return m_target; } + inline void setClientId(const xmrig::Id &id) { m_clientId = id; } + inline void setPoolId(int poolId) { m_poolId = poolId; } + inline void setThreadId(int threadId) { m_threadId = threadId; } + inline xmrig::Variant variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? xmrig::VARIANT_V1 : xmrig::VARIANT_NONE) : m_variant); } + +# ifdef XMRIG_PROXY_PROJECT + inline char *rawBlob() { return m_rawBlob; } + inline const char *rawTarget() const { return m_rawTarget; } +# endif static bool fromHex(const char* in, unsigned int len, unsigned char* out); static inline uint32_t *nonce(uint8_t *blob) { return reinterpret_cast(blob + 39); } @@ -78,16 +82,21 @@ public: private: bool m_nicehash; - char m_coin[5]; - int m_algo; int m_poolId; int m_threadId; size_t m_size; uint64_t m_diff; uint64_t m_target; uint8_t m_blob[96]; // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. + xmrig::Algo m_algo; + xmrig::Id m_clientId; xmrig::Id m_id; xmrig::Variant m_variant; + +# ifdef XMRIG_PROXY_PROJECT + char m_rawBlob[176]; + char m_rawTarget[24]; +# endif }; #endif /* __JOB_H__ */ diff --git a/src/net/JobResult.h b/src/net/JobResult.h index e3282584..68afc862 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -29,7 +29,7 @@ #include -#include "Job.h" +#include "common/net/Job.h" class JobResult diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 9b4a60c7..2efe4977 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -24,11 +24,11 @@ #include "common/crypto/keccak.h" #include "common/net/Client.h" +#include "common/net/Job.h" #include "common/net/strategies/FailoverStrategy.h" #include "common/Platform.h" #include "common/xmrig.h" #include "interfaces/IStrategyListener.h" -#include "net/Job.h" #include "net/strategies/DonateStrategy.h" diff --git a/src/workers/MultiWorker.h b/src/workers/MultiWorker.h index 2c231e8d..d89bbb33 100644 --- a/src/workers/MultiWorker.h +++ b/src/workers/MultiWorker.h @@ -26,8 +26,8 @@ #define __MULTIWORKER_H__ +#include "common/net/Job.h" #include "Mem.h" -#include "net/Job.h" #include "net/JobResult.h" #include "workers/Worker.h" diff --git a/src/workers/Workers.h b/src/workers/Workers.h index bbe9a760..ca01e698 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -30,7 +30,7 @@ #include #include -#include "net/Job.h" +#include "common/net/Job.h" #include "net/JobResult.h" #include "rapidjson/fwd.h" From 274992e50ddbd6dffc41d6f426047a7711daedda Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 21 Apr 2018 00:19:33 +0700 Subject: [PATCH 231/389] Basic cryptonight-ipbc definition. --- CMakeLists.txt | 5 +++++ src/common/net/Pool.cpp | 22 ++++++++++++++++------ src/common/net/Pool.h | 2 +- src/common/xmrig.h | 1 + src/crypto/CryptoNight_constants.h | 16 ++++++++++++++++ 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 21d0726b..25c20409 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(xmrig) option(WITH_LIBCPUID "Use Libcpuid" ON) option(WITH_AEON "CryptoNight-Lite support" ON) option(WITH_SUMO "CryptoNight-Heavy support" ON) +option(WITH_IPBC "CryptoNight-IPBC support" ON) option(WITH_HTTPD "HTTP REST API" ON) option(BUILD_STATIC "Build static binary" OFF) @@ -205,6 +206,10 @@ if (NOT WITH_SUMO) add_definitions(/DXMRIG_NO_SUMO) endif() +if (NOT WITH_IPBC) + add_definitions(/DXMRIG_NO_IPBC) +endif() + if (WITH_HTTPD) find_package(MHD) diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index ddf31031..22f2a647 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -45,9 +45,14 @@ static const char *algoNames[] = { nullptr, # endif # ifndef XMRIG_NO_SUMO - "cryptonight-heavy" + "cryptonight-heavy", # else - nullptr + nullptr, +# endif +# ifndef XMRIG_NO_IPBC + "cryptonight-ipbc", +# else + nullptr, # endif }; @@ -60,9 +65,14 @@ static const char *algoNamesShort[] = { nullptr, # endif # ifndef XMRIG_NO_SUMO - "cn-heavy" + "cn-heavy", # else - nullptr + nullptr, +# endif +# ifndef XMRIG_NO_IPBC + "cn-ipbc", +# else + nullptr, # endif }; @@ -119,9 +129,9 @@ Pool::Pool(const char *host, uint16_t port, const char *user, const char *passwo } -const char *Pool::algoName(xmrig::Algo algorithm) +const char *Pool::algoName(xmrig::Algo algorithm, bool shortName) { - return algoNames[algorithm]; + return (shortName ? algoNamesShort : algoNames)[algorithm]; } diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index ecce865a..62c80185 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -51,7 +51,7 @@ public: xmrig::Variant variant = xmrig::VARIANT_AUTO ); - static const char *algoName(xmrig::Algo algorithm); + static const char *algoName(xmrig::Algo algorithm, bool shortName = false); static xmrig::Algo algorithm(const char *algo); inline bool isNicehash() const { return m_nicehash; } diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 0aa6b842..6bef4186 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -34,6 +34,7 @@ enum Algo { CRYPTONIGHT, /* CryptoNight (Monero) */ CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ CRYPTONIGHT_HEAVY, /* CryptoNight-Heavy (SUMO) */ + CRYPTONIGHT_IPBC /* CryptoNight-IPBC (IPBC) */ }; diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h index 3c746d47..6004edbd 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/CryptoNight_constants.h @@ -47,11 +47,16 @@ constexpr const size_t CRYPTONIGHT_HEAVY_MEMORY = 4 * 1024 * 1024; constexpr const uint32_t CRYPTONIGHT_HEAVY_MASK = 0x3FFFF0; constexpr const uint32_t CRYPTONIGHT_HEAVY_ITER = 0x40000; +constexpr const size_t CRYPTONIGHT_IPBC_MEMORY = 1 * 1024 * 1024; +constexpr const uint32_t CRYPTONIGHT_IPBC_MASK = 0xFFFF0; +constexpr const uint32_t CRYPTONIGHT_IPBC_ITER = 0x40000; + template inline constexpr size_t cn_select_memory() { return 0; } template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_MEMORY; } template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_LITE_MEMORY; } template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_HEAVY_MEMORY; } +template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_IPBC_MEMORY; } inline size_t cn_select_memory(Algo algorithm) { @@ -66,6 +71,9 @@ inline size_t cn_select_memory(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_MEMORY; + case CRYPTONIGHT_IPBC: + return CRYPTONIGHT_IPBC_MEMORY; + default: break; } @@ -78,6 +86,7 @@ template inline constexpr uint32_t cn_select_mask() { retur template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_MASK; } template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_LITE_MASK; } template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_HEAVY_MASK; } +template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_IPBC_MASK; } inline uint32_t cn_select_mask(Algo algorithm) { @@ -92,6 +101,9 @@ inline uint32_t cn_select_mask(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_MASK; + case CRYPTONIGHT_IPBC: + return CRYPTONIGHT_IPBC_MASK; + default: break; } @@ -104,6 +116,7 @@ template inline constexpr uint32_t cn_select_iter() { retur template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_IPBC_ITER; } inline uint32_t cn_select_iter(Algo algorithm) { @@ -118,6 +131,9 @@ inline uint32_t cn_select_iter(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_ITER; + case CRYPTONIGHT_IPBC: + return CRYPTONIGHT_IPBC_ITER; + default: break; } From f9dbd7bc78a93a659b3b4b749bff597c4ee0b015 Mon Sep 17 00:00:00 2001 From: Ange Date: Sat, 21 Apr 2018 00:36:58 +0200 Subject: [PATCH 232/389] valgrind showed some conditional jumps when printing the hashrate --- src/workers/Hashrate.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index 7d2e6a4c..71aa9cc6 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -151,10 +151,10 @@ void Hashrate::add(size_t threadId, uint64_t count, uint64_t timestamp) void Hashrate::print() { - char num1[8]; - char num2[8]; - char num3[8]; - char num4[8]; + char num1[8] = { 0 }; + char num2[8] = { 0 }; + char num3[8] = { 0 }; + char num4[8] = { 0 }; LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;37mspeed\x1B[0m 2.5s/60s/15m \x1B[01;36m%s \x1B[22;36m%s %s \x1B[01;36mH/s\x1B[0m max: \x1B[01;36m%s H/s" : "speed 2.5s/60s/15m %s %s %s H/s max: %s H/s", format(calc(ShortInterval), num1, sizeof(num1)), From 45e8a0525cac390bd086e136cecff60788d99ec0 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 21 Apr 2018 19:55:51 +0700 Subject: [PATCH 233/389] Prepare for per pool and per job algorithms. --- src/common/config/CommonConfig.cpp | 10 +---- src/common/config/CommonConfig.h | 1 - src/common/log/Log.h | 6 +++ src/common/net/Client.cpp | 2 +- src/common/net/Job.cpp | 6 +-- src/common/net/Job.h | 5 ++- src/common/net/Pool.cpp | 57 +++++++++++++++++------- src/common/net/Pool.h | 13 +++--- src/common/xmrig.h | 2 +- src/net/Network.cpp | 9 ++-- src/net/strategies/DonateStrategy.cpp | 19 ++++++-- src/net/strategies/DonateStrategy.h | 2 +- src/workers/CpuThread.cpp | 64 +++++++++++++-------------- src/workers/MultiWorker.cpp | 4 +- 14 files changed, 116 insertions(+), 84 deletions(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 75906621..431eff1d 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -141,7 +141,7 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) { switch (key) { case AlgorithmKey: /* --algo */ - setAlgo(arg); + m_algorithm = Pool::algorithm(arg); break; case UserpassKey: /* --userpass */ @@ -204,7 +204,7 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) case SyslogKey: /* --syslog */ case KeepAliveKey: /* --keepalive */ case NicehashKey: /* --nicehash */ - case ApiIPv6Key: /* --api-ipv6 */ + case ApiIPv6Key: /* --api-ipv6 */ return parseBoolean(key, true); case ColorKey: /* --no-color */ @@ -322,9 +322,3 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) return true; } - - -void xmrig::CommonConfig::setAlgo(const char *algo) -{ - m_algorithm = Pool::algorithm(algo); -} diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 015ba04e..ebeb41fe 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -96,7 +96,6 @@ protected: private: bool parseInt(int key, int arg); - void setAlgo(const char *algo); }; diff --git a/src/common/log/Log.h b/src/common/log/Log.h index f8c6a401..8434d779 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -80,6 +80,12 @@ private: }; +#define MAGENTA_BOLD(x) "\e[1;35m" x "\e[0m" +#define MAGENTA(x) "\e[0;35m" x "\e[0m" +#define WHITE_BOLD(x) "\e[1;37m" x "\e[0m" +#define WHITE(x) "\e[0;37m" x "\e[0m" + + #define LOG_ERR(x, ...) Log::i()->message(Log::ERR, x, ##__VA_ARGS__) #define LOG_WARN(x, ...) Log::i()->message(Log::WARNING, x, ##__VA_ARGS__) #define LOG_NOTICE(x, ...) Log::i()->message(Log::NOTICE, x, ##__VA_ARGS__) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index a110b2ce..d7e88cbf 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -232,7 +232,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } - Job job(m_id, m_nicehash, m_pool.algo(), m_pool.variant(), m_rpcId); + Job job(m_id, m_nicehash, m_pool.algorithm(), m_pool.variant(), m_rpcId); if (!job.setId(params["job_id"].GetString())) { *code = 3; diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index c5f9902c..ec5279bf 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -65,7 +65,7 @@ Job::Job() : m_diff(0), m_target(0), m_blob(), - m_algo(xmrig::INVALID_ALGO), + m_algorithm(xmrig::INVALID_ALGO), m_variant(xmrig::VARIANT_AUTO) { } @@ -79,7 +79,7 @@ Job::Job(int poolId, bool nicehash, xmrig::Algo algo, xmrig::Variant variant, co m_diff(0), m_target(0), m_blob(), - m_algo(algo), + m_algorithm(algo), m_clientId(clientId), m_variant(variant) { @@ -170,7 +170,7 @@ void Job::setVariant(int variant) { switch (variant) { case xmrig::VARIANT_AUTO: - case xmrig::VARIANT_NONE: + case xmrig::VARIANT_V0: case xmrig::VARIANT_V1: m_variant = static_cast(variant); break; diff --git a/src/common/net/Job.h b/src/common/net/Job.h index 4eb7018e..0e2951f3 100644 --- a/src/common/net/Job.h +++ b/src/common/net/Job.h @@ -61,7 +61,8 @@ public: inline void setClientId(const xmrig::Id &id) { m_clientId = id; } inline void setPoolId(int poolId) { m_poolId = poolId; } inline void setThreadId(int threadId) { m_threadId = threadId; } - inline xmrig::Variant variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? xmrig::VARIANT_V1 : xmrig::VARIANT_NONE) : m_variant); } + inline xmrig::Algo algorithm() const { return m_algorithm; } + inline xmrig::Variant variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? xmrig::VARIANT_V1 : xmrig::VARIANT_V0) : m_variant); } # ifdef XMRIG_PROXY_PROJECT inline char *rawBlob() { return m_rawBlob; } @@ -88,7 +89,7 @@ private: uint64_t m_diff; uint64_t m_target; uint8_t m_blob[96]; // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. - xmrig::Algo m_algo; + xmrig::Algo m_algorithm; xmrig::Id m_clientId; xmrig::Id m_id; xmrig::Variant m_variant; diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 22f2a647..f1dd1352 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -81,7 +81,7 @@ Pool::Pool() : m_nicehash(false), m_keepAlive(0), m_port(kDefaultPort), - m_algo(xmrig::CRYPTONIGHT), + m_algorithm(xmrig::INVALID_ALGO), m_variant(xmrig::VARIANT_AUTO) { } @@ -102,7 +102,7 @@ Pool::Pool(const char *url) : m_nicehash(false), m_keepAlive(0), m_port(kDefaultPort), - m_algo(xmrig::CRYPTONIGHT), + m_algorithm(xmrig::INVALID_ALGO), m_variant(xmrig::VARIANT_AUTO) { parse(url); @@ -113,11 +113,11 @@ Pool::Pool(const char *host, uint16_t port, const char *user, const char *passwo m_nicehash(nicehash), m_keepAlive(keepAlive), m_port(port), - m_algo(xmrig::CRYPTONIGHT), + m_algorithm(xmrig::INVALID_ALGO), m_host(host), m_password(password), m_user(user), - m_variant(variant) + m_variant(variant) { const size_t size = m_host.size() + 8; assert(size > 8); @@ -131,6 +131,10 @@ Pool::Pool(const char *host, uint16_t port, const char *user, const char *passwo const char *Pool::algoName(xmrig::Algo algorithm, bool shortName) { + if (algorithm == xmrig::INVALID_ALGO) { + return "invalid"; + } + return (shortName ? algoNamesShort : algoNames)[algorithm]; } @@ -160,6 +164,20 @@ xmrig::Algo Pool::algorithm(const char *algo) } +bool Pool::isEqual(const Pool &other) const +{ + return (m_nicehash == other.m_nicehash + && m_keepAlive == other.m_keepAlive + && m_port == other.m_port + && m_algorithm == other.m_algorithm + && m_host == other.m_host + && m_password == other.m_password + && m_url == other.m_url + && m_user == other.m_user + && m_variant == other.m_variant); +} + + bool Pool::parse(const char *url) { assert(url != nullptr); @@ -218,13 +236,15 @@ bool Pool::setUserpass(const char *userpass) } -void Pool::adjust(xmrig::Algo algo) +void Pool::adjust(xmrig::Algo algorithm) { if (!isValid()) { return; } - m_algo = algo; + if (m_algorithm == xmrig::INVALID_ALGO) { + m_algorithm = algorithm; + } if (strstr(m_host.data(), ".nicehash.com")) { m_keepAlive = false; @@ -241,7 +261,7 @@ void Pool::setVariant(int variant) { switch (variant) { case xmrig::VARIANT_AUTO: - case xmrig::VARIANT_NONE: + case xmrig::VARIANT_V0: case xmrig::VARIANT_V1: m_variant = static_cast(variant); break; @@ -253,17 +273,20 @@ void Pool::setVariant(int variant) } -bool Pool::isEqual(const Pool &other) const +xmrig::Variant Pool::variant() const { - return (m_nicehash == other.m_nicehash - && m_keepAlive == other.m_keepAlive - && m_port == other.m_port - && m_algo == other.m_algo - && m_host == other.m_host - && m_password == other.m_password - && m_url == other.m_url - && m_user == other.m_user - && m_variant == other.m_variant); + switch (m_algorithm) { + case xmrig::CRYPTONIGHT_HEAVY: + return xmrig::VARIANT_V0; + + case xmrig::CRYPTONIGHT_IPBC: + return xmrig::VARIANT_V1; + + default: + break; + } + + return m_variant; } diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index 62c80185..a7e1ca8b 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -62,22 +62,23 @@ public: inline const char *user() const { return !m_user.isNull() ? m_user.data() : kDefaultUser; } inline int keepAlive() const { return m_keepAlive; } inline uint16_t port() const { return m_port; } + inline void setAlgo(const char *algo) { m_algorithm = algorithm(algo); } + inline void setAlgo(xmrig::Algo algorithm) { m_algorithm = algorithm; } inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setPassword(const char *password) { m_password = password; } inline void setUser(const char *user) { m_user = user; } - inline xmrig::Algo algo() const { return m_algo; } - inline xmrig::Variant variant() const { return m_variant; } + inline xmrig::Algo algorithm() const { return m_algorithm; } inline bool operator!=(const Pool &other) const { return !isEqual(other); } inline bool operator==(const Pool &other) const { return isEqual(other); } + bool isEqual(const Pool &other) const; bool parse(const char *url); bool setUserpass(const char *userpass); - void adjust(xmrig::Algo algo); + void adjust(xmrig::Algo algorithm); void setVariant(int variant); - - bool isEqual(const Pool &other) const; + xmrig::Variant variant() const; private: bool parseIPv6(const char *addr); @@ -85,7 +86,7 @@ private: bool m_nicehash; int m_keepAlive; uint16_t m_port; - xmrig::Algo m_algo; + xmrig::Algo m_algorithm; xmrig::c_str m_host; xmrig::c_str m_password; xmrig::c_str m_url; diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 6bef4186..2de1a798 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -60,7 +60,7 @@ enum AlgoVariant { enum Variant { VARIANT_AUTO = -1, // Autodetect - VARIANT_NONE = 0, // Original CryptoNight + VARIANT_V0 = 0, // Original CryptoNight or CryptoNight-Heavy VARIANT_V1 = 1 // Monero v7 PoW }; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 5f6d73ee..4101245e 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -166,12 +166,9 @@ bool Network::isColors() const void Network::setJob(Client *client, const Job &job, bool donate) { - if (isColors()) { - LOG_INFO("\x1B[01;35mnew job\x1B[0m from \x1B[01;37m%s:%d\x1B[0m diff \x1B[01;37m%d", client->host(), client->port(), job.diff()); - } - else { - LOG_INFO("new job from %s:%d diff %d", client->host(), client->port(), job.diff()); - } + LOG_INFO(isColors() ? MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s/%d") + : "new job from %s:%d diff %d algo %s/%d", + client->host(), client->port(), job.diff(), Pool::algoName(job.algorithm(), true), static_cast(job.variant())); m_state.diff = job.diff(); Workers::setJob(job, donate); diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 2efe4977..ad8e88c0 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -26,6 +26,7 @@ #include "common/net/Client.h" #include "common/net/Job.h" #include "common/net/strategies/FailoverStrategy.h" +#include "common/net/strategies/SinglePoolStrategy.h" #include "common/Platform.h" #include "common/xmrig.h" #include "interfaces/IStrategyListener.h" @@ -41,7 +42,7 @@ static inline float randomf(float min, float max) { } -DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) : +DonateStrategy::DonateStrategy(int level, const char *user, xmrig::Algo algo, IStrategyListener *listener) : m_active(false), m_donateTime(level * 60 * 1000), m_idleTime((100 - level) * 60 * 1000), @@ -61,14 +62,24 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL } else if (algo == xmrig::CRYPTONIGHT_HEAVY) { m_pools.push_back(Pool(kDonatePool1, 8888, userId, nullptr, false, true)); - m_pools.push_back(Pool(kDonatePool1, 8889, userId, nullptr, false, true)); + } + else if (algo == xmrig::CRYPTONIGHT_IPBC) { + m_pools.push_back(Pool(kDonatePool1, 13333, userId, nullptr, false, true)); } else { m_pools.push_back(Pool(kDonatePool1, 5555, userId, nullptr, false, true)); - m_pools.push_back(Pool(kDonatePool1, 7777, userId, nullptr, false, true)); } - m_strategy = new FailoverStrategy(m_pools, 1, 1, this, true); + for (Pool &pool : m_pools) { + pool.setAlgo(algo); + } + + if (m_pools.size() > 1) { + m_strategy = new FailoverStrategy(m_pools, 1, 2, this, true); + } + else { + m_strategy = new SinglePoolStrategy(m_pools.front(), 1, this, true); + } m_timer.data = this; uv_timer_init(uv_default_loop(), &m_timer); diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index edb15c8f..66c0375c 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -43,7 +43,7 @@ class Url; class DonateStrategy : public IStrategy, public IStrategyListener { public: - DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener); + DonateStrategy(int level, const char *user, xmrig::Algo algo, IStrategyListener *listener); ~DonateStrategy(); public: diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index be24b2d5..5c0e8c0b 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -62,19 +62,19 @@ bool xmrig::CpuThread::isSoftAES(AlgoVariant av) xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant av, Variant variant) { - assert(variant == VARIANT_NONE || variant == VARIANT_V1); + assert(variant == VARIANT_V0 || variant == VARIANT_V1); static const cn_hash_fun func_table[50] = { - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, cryptonight_single_hash, cryptonight_double_hash, @@ -88,16 +88,16 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_penta_hash, # ifndef XMRIG_NO_AEON - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, cryptonight_single_hash, cryptonight_double_hash, @@ -115,16 +115,16 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a # endif # ifndef XMRIG_NO_SUMO - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif @@ -132,7 +132,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a # ifndef XMRIG_NO_SUMO if (algorithm == CRYPTONIGHT_HEAVY) { - variant = VARIANT_NONE; + variant = VARIANT_V0; } # endif diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 09dd568f..33f5864d 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -50,11 +50,11 @@ MultiWorker::~MultiWorker() template bool MultiWorker::selfTest() { - if (m_thread->fn(xmrig::VARIANT_NONE) == nullptr) { + if (m_thread->fn(xmrig::VARIANT_V0) == nullptr) { return false; } - m_thread->fn(xmrig::VARIANT_NONE)(test_input, 76, m_hash, m_ctx); + m_thread->fn(xmrig::VARIANT_V0)(test_input, 76, m_hash, m_ctx); if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_hash, test_output_v0, sizeof m_hash) == 0) { m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); From 259a1774caaced0ca011bb160691f0d485ff1508 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 21 Apr 2018 21:41:39 +0700 Subject: [PATCH 234/389] Fix Linux build. --- src/App_unix.cpp | 2 +- src/Mem_unix.cpp | 2 +- src/common/net/Job.h | 1 + src/core/Controller.cpp | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/App_unix.cpp b/src/App_unix.cpp index f739e42e..45f73d2d 100644 --- a/src/App_unix.cpp +++ b/src/App_unix.cpp @@ -29,9 +29,9 @@ #include "App.h" +#include "common/log/Log.h" #include "core/Config.h" #include "core/Controller.h" -#include "log/Log.h" void App::background() diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 229a0d46..c1aa0fb1 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -27,10 +27,10 @@ #include +#include "common/log/Log.h" #include "common/utils/mm_malloc.h" #include "common/xmrig.h" #include "crypto/CryptoNight.h" -#include "log/Log.h" #include "Mem.h" diff --git a/src/common/net/Job.h b/src/common/net/Job.h index 0e2951f3..c235072e 100644 --- a/src/common/net/Job.h +++ b/src/common/net/Job.h @@ -58,6 +58,7 @@ public: inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } inline uint32_t diff() const { return static_cast(m_diff); } inline uint64_t target() const { return m_target; } + inline void reset() { m_size = 0; m_diff = 0; } inline void setClientId(const xmrig::Id &id) { m_clientId = id; } inline void setPoolId(int poolId) { m_poolId = poolId; } inline void setThreadId(int threadId) { m_threadId = threadId; } diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index d0babe47..b1e03f32 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -38,7 +38,7 @@ #ifdef HAVE_SYSLOG_H -# include "log/SysLog.h" +# include "common/log/SysLog.h" #endif From 593056113c10a1861e3ddc78d825526f82c7e2a5 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 21 Apr 2018 22:23:12 +0700 Subject: [PATCH 235/389] #541 Revert all changes in Client::close(). --- src/net/Client.cpp | 23 +---------------------- src/version.h | 4 ++-- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index c8a93f24..9e6758e3 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -205,28 +205,7 @@ bool Client::close() setState(ClosingState); - uv_stream_t *stream = reinterpret_cast(m_socket); - - if (uv_is_readable(stream) == 1) { - uv_read_stop(stream); - } - - if (uv_is_writable(stream) == 1) { - const int rc = uv_shutdown(new uv_shutdown_t, stream, [](uv_shutdown_t* req, int status) { - if (uv_is_closing(reinterpret_cast(req->handle)) == 0) { - uv_close(reinterpret_cast(req->handle), Client::onClose); - } - - delete req; - }); - - assert(rc == 0); - - if (rc != 0) { - onClose(); - } - } - else { + if (uv_is_closing(reinterpret_cast(m_socket)) == 0) { uv_close(reinterpret_cast(m_socket), Client::onClose); } diff --git a/src/version.h b/src/version.h index 060edcb7..4b44f2e8 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.5.2" +#define APP_VERSION "2.5.3" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 5 -#define APP_VER_BUILD 2 +#define APP_VER_BUILD 3 #define APP_VER_REV 0 #ifdef _MSC_VER From 7365e0486bf1e991bf77ab3e0085004d61cb0a86 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 21 Apr 2018 22:35:26 +0700 Subject: [PATCH 236/389] Disable IPv6 for API. --- src/api/Httpd.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 861086ed..a0c591c4 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -51,10 +51,6 @@ bool Httpd::start() if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) { flags = MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_EPOLL_INTERNALLY_LINUX_ONLY; } - - if (MHD_is_feature_supported(MHD_FEATURE_IPv6)) { - flags |= MHD_USE_DUAL_STACK; - } # endif m_daemon = MHD_start_daemon(flags, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); From 90a09f20b3095903884d0a092b67f78d1f4c9685 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 21 Apr 2018 22:52:06 +0700 Subject: [PATCH 237/389] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab31e698..e5a74cf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v2.5.3 +- Fixed critical bug, in some cases miner was can't recovery connection and switch to failover pool, version 2.5.2 affected. If you use v2.6.0-beta3 this issue doesn't concern you. +- [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 support disabled for internal HTTP API. + # v2.5.2 - [#448](https://github.com/xmrig/xmrig/issues/478) Fixed broken reconnect. From 38874fbb0a175d3348acc54781dc71a5034c5c7b Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 22 Apr 2018 12:57:37 +0700 Subject: [PATCH 238/389] Added workaround for nicehash.com if you use cryptonightv7..nicehash.com option variant=1 will be set automatically. --- CHANGELOG.md | 1 + src/net/Url.cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5a74cf6..082ee0c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # v2.5.3 - Fixed critical bug, in some cases miner was can't recovery connection and switch to failover pool, version 2.5.2 affected. If you use v2.6.0-beta3 this issue doesn't concern you. - [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 support disabled for internal HTTP API. +- Added workaround for nicehash.com if you use `cryptonightv7..nicehash.com` option `variant=1` will be set automatically. # v2.5.2 - [#448](https://github.com/xmrig/xmrig/issues/478) Fixed broken reconnect. diff --git a/src/net/Url.cpp b/src/net/Url.cpp index c17ef690..99fe9dc2 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -180,6 +180,10 @@ void Url::adjust(int algo) if (strstr(m_host, ".nicehash.com")) { m_keepAlive = false; m_nicehash = true; + + if (strstr(m_host, "cryptonightv7.")) { + m_variant = xmrig::VARIANT_V1; + } } if (strstr(m_host, ".minergate.com")) { From 8d9025f2caf31fc97a14a5571b5ff85d34474db3 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 22 Apr 2018 22:47:05 +0700 Subject: [PATCH 239/389] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e2801fa6..f69aa346 100644 --- a/README.md +++ b/README.md @@ -124,10 +124,10 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -b070d06a3615f3db67ad3beab43d6d21f3c88026aa2b4726a93df47145cd30ec xmrig-2.5.2-xenial-amd64.tar.gz/xmrig-2.5.2/xmrig -4852135d3f04fd450ba39abce51ca40ff9131d222220c8b30804be05f6679295 xmrig-2.5.2-gcc-win32.zip/xmrig.exe -284309d07f08261af19c937ece6d2031910d9124a7359c207ded65890b2d7c5f xmrig-2.5.2-gcc-win64.zip/xmrig.exe -e1dc46158a578fb030538fb06e5663a6acc5763545fb447a00ce0a6b388c5226 xmrig-2.5.2-msvc-win64.zip/xmrig.exe +5ae25d05b7735dd6e2482e8dba0cf0f5d10f9738855c4ad4eaf449b8ccd2e5be xmrig-2.5.3-xenial-amd64.tar.gz/xmrig-2.5.3/xmrig +f11f3b381425ca4181c425d5b693407431f964759bb903f66b7cd2345fcdd786 xmrig-2.5.3-gcc-win32.zip/xmrig.exe +67df8b89714e2921931092861361dbae4716c4ab872c767c92adae24dca01514 xmrig-2.5.3-gcc-win64.zip/xmrig.exe +52bf6e0ef72c84282f4df411125384444c521ed9143e5d8c7e7e445d7d55e143 xmrig-2.5.3-msvc-win64.zip/xmrig.exe ``` ## Contacts From 6d40f2dd1a09a949f145f1ebded9d1c983c248a6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 23 Apr 2018 00:59:58 +0700 Subject: [PATCH 240/389] Sync changes with proxy. --- src/common/Platform_unix.cpp | 4 ++-- src/common/api/Httpd.cpp | 7 +++++++ src/common/net/Pool.cpp | 20 ++++++++++++++++++++ src/common/net/Pool.h | 4 ++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/common/Platform_unix.cpp b/src/common/Platform_unix.cpp index 624594e9..63cd1c8b 100644 --- a/src/common/Platform_unix.cpp +++ b/src/common/Platform_unix.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/common/api/Httpd.cpp b/src/common/api/Httpd.cpp index 0cab01bf..eb6a4ba6 100644 --- a/src/common/api/Httpd.cpp +++ b/src/common/api/Httpd.cpp @@ -81,7 +81,12 @@ bool Httpd::start() return false; } +# if MHD_VERSION >= 0x00093900 uv_timer_start(&m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval); +# else + uv_timer_start(&m_timer, Httpd::onTimer, kActiveInterval, kActiveInterval); +# endif + return true; } @@ -107,6 +112,7 @@ void Httpd::run() { MHD_run(m_daemon); +# if MHD_VERSION >= 0x00093900 const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS); if (m_idle && info->num_connections) { uv_timer_set_repeat(&m_timer, kActiveInterval); @@ -116,6 +122,7 @@ void Httpd::run() uv_timer_set_repeat(&m_timer, kIdleInterval); m_idle = true; } +# endif } diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index f1dd1352..3be491fb 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -31,6 +31,11 @@ #include "common/net/Pool.h" +#ifdef APP_DEBUG +# include "common/log/Log.h" +#endif + + #ifdef _MSC_VER # define strncasecmp _strnicmp # define strcasecmp _stricmp @@ -290,6 +295,21 @@ xmrig::Variant Pool::variant() const } +#ifdef APP_DEBUG +void Pool::print() const +{ + LOG_NOTICE("url: %s", m_url.data()); + LOG_DEBUG ("host: %s", m_host.data()); + LOG_DEBUG ("port: %d", static_cast(m_port)); + LOG_DEBUG ("user: %s", m_user.data()); + LOG_DEBUG ("pass: %s", m_password.data()); + LOG_DEBUG ("algo: %s/%d", algoName(m_algorithm), static_cast(variant())); + LOG_DEBUG ("nicehash: %d", static_cast(m_nicehash)); + LOG_DEBUG ("keepAlive: %d", m_keepAlive); +} +#endif + + bool Pool::parseIPv6(const char *addr) { const char *end = strchr(addr, ']'); diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index a7e1ca8b..df8578bc 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -80,6 +80,10 @@ public: void setVariant(int variant); xmrig::Variant variant() const; +# ifdef APP_DEBUG + void print() const; +# endif + private: bool parseIPv6(const char *addr); From b9fec2fcc4dff2866c3ecb0d596b27015da8ef5d Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 23 Apr 2018 13:20:43 +0700 Subject: [PATCH 241/389] Added support for "rig id" protocol extension. --- src/Summary.cpp | 4 ++-- src/common/config/CommonConfig.cpp | 4 ++++ src/common/log/Log.h | 4 ++++ src/common/net/Client.cpp | 4 ++++ src/common/net/Pool.cpp | 7 +++++++ src/common/net/Pool.h | 3 +++ src/core/Config.cpp | 1 + src/core/ConfigLoader_platform.h | 3 +++ src/interfaces/IConfig.h | 13 +++++++------ src/workers/Workers.cpp | 6 +++--- 10 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/Summary.cpp b/src/Summary.cpp index 38ceac8e..a52b7945 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -132,8 +132,8 @@ static void print_pools(xmrig::Config *config) } # ifdef APP_DEBUG - for (size_t i = 0; i < pools.size(); ++i) { - Log::i()->text("%s:%d, user: %s, pass: %s, ka: %d, nicehash: %d", pools[i].host(), pools[i].port(), pools[i].user(), pools[i].password(), pools[i].keepAlive(), pools[i].isNicehash()); + for (const Pool &pool : pools) { + pool.print(); } # endif } diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 431eff1d..baf5a729 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -177,6 +177,10 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) m_pools.back().setPassword(arg); break; + case RigIdKey: /* --rig-id */ + m_pools.back().setRigId(arg); + break; + case LogFileKey: /* --log-file */ m_logFile = arg; break; diff --git a/src/common/log/Log.h b/src/common/log/Log.h index 8434d779..0b333272 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -80,8 +80,12 @@ private: }; +#define GREEN_BOLD(x) "\e[1;32m" x "\e[0m" +#define GREEN(x) "\e[0;32m" x "\e[0m" #define MAGENTA_BOLD(x) "\e[1;35m" x "\e[0m" #define MAGENTA(x) "\e[0;35m" x "\e[0m" +#define CYAN_BOLD(x) "\e[1;36m" x "\e[0m" +#define CYAN(x) "\e[0;36m" x "\e[0m" #define WHITE_BOLD(x) "\e[1;37m" x "\e[0m" #define WHITE(x) "\e[0;37m" x "\e[0m" diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index d7e88cbf..de56b315 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -397,6 +397,10 @@ void Client::login() params.AddMember("pass", rapidjson::StringRef(m_pool.password()), allocator); params.AddMember("agent", rapidjson::StringRef(m_agent), allocator); + if (m_pool.rigId()) { + params.AddMember("rigid", rapidjson::StringRef(m_pool.rigId()), allocator); + } + doc.AddMember("params", params, allocator); rapidjson::StringBuffer buffer(0, 512); diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 3be491fb..4675876a 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -177,6 +177,7 @@ bool Pool::isEqual(const Pool &other) const && m_algorithm == other.m_algorithm && m_host == other.m_host && m_password == other.m_password + && m_rigId == other.m_rigId && m_url == other.m_url && m_user == other.m_user && m_variant == other.m_variant); @@ -254,9 +255,14 @@ void Pool::adjust(xmrig::Algo algorithm) if (strstr(m_host.data(), ".nicehash.com")) { m_keepAlive = false; m_nicehash = true; + + if (strstr(m_host.data(), "cryptonightv7.")) { + m_variant = xmrig::VARIANT_V1; + } } if (strstr(m_host.data(), ".minergate.com")) { + m_variant = xmrig::VARIANT_V1; m_keepAlive = false; } } @@ -303,6 +309,7 @@ void Pool::print() const LOG_DEBUG ("port: %d", static_cast(m_port)); LOG_DEBUG ("user: %s", m_user.data()); LOG_DEBUG ("pass: %s", m_password.data()); + LOG_DEBUG ("rig_id %s", m_rigId.data()); LOG_DEBUG ("algo: %s/%d", algoName(m_algorithm), static_cast(variant())); LOG_DEBUG ("nicehash: %d", static_cast(m_nicehash)); LOG_DEBUG ("keepAlive: %d", m_keepAlive); diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index df8578bc..b1b93507 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -58,6 +58,7 @@ public: inline bool isValid() const { return !m_host.isNull() && m_port > 0; } inline const char *host() const { return m_host.data(); } inline const char *password() const { return !m_password.isNull() ? m_password.data() : kDefaultPassword; } + inline const char *rigId() const { return m_rigId.data(); } inline const char *url() const { return m_url.data(); } inline const char *user() const { return !m_user.isNull() ? m_user.data() : kDefaultUser; } inline int keepAlive() const { return m_keepAlive; } @@ -67,6 +68,7 @@ public: inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setPassword(const char *password) { m_password = password; } + inline void setRigId(const char *rigId) { m_rigId = rigId; } inline void setUser(const char *user) { m_user = user; } inline xmrig::Algo algorithm() const { return m_algorithm; } @@ -93,6 +95,7 @@ private: xmrig::Algo m_algorithm; xmrig::c_str m_host; xmrig::c_str m_password; + xmrig::c_str m_rigId; xmrig::c_str m_url; xmrig::c_str m_user; xmrig::Variant m_variant; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 33cd01e8..43abbbbd 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -108,6 +108,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const obj.AddMember("url", StringRef(pool.url()), allocator); obj.AddMember("user", StringRef(pool.user()), allocator); obj.AddMember("pass", StringRef(pool.password()), allocator); + obj.AddMember("rig-id", pool.rigId() ? Value(StringRef(pool.rigId())).Move() : Value(kNullType).Move(), allocator); if (pool.keepAlive() == 0 || pool.keepAlive() == Pool::kKeepAliveTimeout) { obj.AddMember("keepalive", pool.keepAlive() > 0, allocator); diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index d02a9e8f..9704d5e3 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -58,6 +58,7 @@ Options:\n\ -O, --userpass=U:P username:password pair for mining server\n\ -u, --user=USERNAME username for mining server\n\ -p, --pass=PASSWORD password for mining server\n\ + --rig-id=ID rig identifier for pool-side statistics (needs pool support)\n\ -t, --threads=N number of miner threads\n\ -v, --av=N algorithm variation, 0 auto select\n\ -k, --keepalive send keepalived for prevent timeout (need pool support)\n\ @@ -128,6 +129,7 @@ static struct option const options[] = { { "user", 1, nullptr, xmrig::IConfig::UserKey }, { "user-agent", 1, nullptr, xmrig::IConfig::UserAgentKey }, { "userpass", 1, nullptr, xmrig::IConfig::UserpassKey }, + { "rig-id", 1, nullptr, xmrig::IConfig::RigIdKey }, { "version", 0, nullptr, xmrig::IConfig::VersionKey }, { 0, 0, 0, 0 } }; @@ -165,6 +167,7 @@ static struct option const pool_options[] = { { "nicehash", 0, nullptr, xmrig::IConfig::NicehashKey }, { "keepalive", 2, nullptr, xmrig::IConfig::KeepAliveKey }, { "variant", 1, nullptr, xmrig::IConfig::VariantKey }, + { "rig-id", 1, nullptr, xmrig::IConfig::RigIdKey }, { 0, 0, 0, 0 } }; diff --git a/src/interfaces/IConfig.h b/src/interfaces/IConfig.h index f7433d7f..cf4f0d23 100644 --- a/src/interfaces/IConfig.h +++ b/src/interfaces/IConfig.h @@ -36,30 +36,31 @@ public: enum Keys { // common AlgorithmKey = 'a', - ApiPort = 4000, ApiAccessTokenKey = 4001, - ApiWorkerIdKey = 4002, ApiIPv6Key = 4003, + ApiPort = 4000, ApiRestrictedKey = 4004, + ApiWorkerIdKey = 4002, BackgroundKey = 'B', + ColorKey = 1002, ConfigKey = 'c', DonateLevelKey = 1003, HelpKey = 'h', KeepAliveKey = 'k', LogFileKey = 'l', - ColorKey = 1002, - WatchKey = 1105, PasswordKey = 'p', RetriesKey = 'r', RetryPauseKey = 'R', + RigIdKey = 1012, SyslogKey = 'S', UrlKey = 'o', - UserKey = 'u', UserAgentKey = 1008, + UserKey = 'u', UserpassKey = 'O', + VariantKey = 1010, VerboseKey = 1100, VersionKey = 'V', - VariantKey = 1010, + WatchKey = 1105, // xmrig common CPUPriorityKey = 1021, diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 1d2c1739..67b2973d 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -133,7 +133,7 @@ void Workers::start(xmrig::Controller *controller) { const std::vector &threads = controller->config()->threads(); m_status.algo = controller->config()->algorithm(); - m_status.colors = controller->config()->isHugePages(); + m_status.colors = controller->config()->isColors(); m_status.threads = threads.size(); for (const xmrig::IThread *thread : threads) { @@ -301,9 +301,9 @@ void Workers::start(IWorker *worker) const size_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo) / 1048576; if (m_status.colors) { - LOG_INFO("\x1B[01;32mREADY (CPU)\x1B[0m threads \x1B[01;36m%zu(%zu)\x1B[0m huge pages %s%zu/%zu %1.0f%%\x1B[0m memory \x1B[01;36m%zu.0 MB", + LOG_INFO(GREEN_BOLD("READY (CPU)") " threads " CYAN_BOLD("%zu(%zu)") " huge pages %s%zu/%zu %1.0f%%\e[0m memory " CYAN_BOLD("%zu.0 MB") "", m_status.threads, m_status.ways, - (m_status.hugePages == m_status.pages ? "\x1B[01;32m" : (m_status.hugePages == 0 ? "\x1B[01;31m" : "\x1B[01;33m")), + (m_status.hugePages == m_status.pages ? "\e[1;32m" : (m_status.hugePages == 0 ? "\e[1;31m" : "\e[1;33m")), m_status.hugePages, m_status.pages, percent, memory); } else { From ca149d2eed8f3d278bd0f322b191824f3c5b7796 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 25 Apr 2018 14:48:32 +0700 Subject: [PATCH 242/389] Sync changes with proxy. --- CMakeLists.txt | 2 + src/Summary.cpp | 4 +- src/api/ApiRouter.cpp | 2 +- src/common/config/CommonConfig.cpp | 18 ++- src/common/config/CommonConfig.h | 5 +- src/common/crypto/Algorithm.cpp | 158 +++++++++++++++++++++++ src/common/crypto/Algorithm.h | 76 +++++++++++ src/common/net/Client.cpp | 4 +- src/common/net/Job.cpp | 28 +--- src/common/net/Job.h | 51 ++++---- src/common/net/Pool.cpp | 178 ++++++++------------------ src/common/net/Pool.h | 48 +++---- src/common/net/Storage.h | 3 - src/common/utils/c_str.h | 2 +- src/common/xmrig.h | 9 +- src/core/Config.cpp | 29 +---- src/crypto/CryptoNight_constants.h | 19 +-- src/net/Network.cpp | 8 +- src/net/strategies/DonateStrategy.cpp | 5 +- src/workers/CpuThread.cpp | 105 ++++++++------- src/workers/MultiWorker.cpp | 8 +- src/workers/Workers.cpp | 2 +- 22 files changed, 436 insertions(+), 328 deletions(-) create mode 100644 src/common/crypto/Algorithm.cpp create mode 100644 src/common/crypto/Algorithm.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 25c20409..b26b1d93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(HEADERS src/common/config/ConfigLoader.h src/common/config/ConfigWatcher.h src/common/Console.h + src/common/crypto/Algorithm.h src/common/crypto/keccak.h src/common/log/ConsoleLog.h src/common/log/FileLog.h @@ -93,6 +94,7 @@ set(SOURCES src/common/config/ConfigLoader.cpp src/common/config/ConfigWatcher.cpp src/common/Console.cpp + src/common/crypto/Algorithm.cpp src/common/crypto/keccak.cpp src/common/log/ConsoleLog.cpp src/common/log/FileLog.cpp diff --git a/src/Summary.cpp b/src/Summary.cpp index a52b7945..e960dd8d 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -104,7 +104,7 @@ static void print_threads(xmrig::Config *config) Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mTHREADS: \x1B[01;36m%d\x1B[01;37m, %s, av=%d, %sdonate=%d%%%s" : " * THREADS: %d, %s, av=%d, %sdonate=%d%%%s", config->threadsCount(), - config->algoName(), + config->algorithm().name(), config->algoVariant(), config->isColors() && config->donateLevel() == 0 ? "\x1B[01;31m" : "", config->donateLevel(), @@ -113,7 +113,7 @@ static void print_threads(xmrig::Config *config) else { Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mTHREADS: \x1B[01;36m%d\x1B[01;37m, %s, %sdonate=%d%%" : " * THREADS: %d, %s, %sdonate=%d%%", config->threadsCount(), - config->algoName(), + config->algorithm().name(), config->isColors() && config->donateLevel() == 0 ? "\x1B[01;31m" : "", config->donateLevel()); } diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index a9e32b18..07e425f1 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -244,7 +244,7 @@ void ApiRouter::getMiner(rapidjson::Document &doc) const doc.AddMember("kind", APP_KIND, allocator); doc.AddMember("ua", rapidjson::StringRef(Platform::userAgent()), allocator); doc.AddMember("cpu", cpu, allocator); - doc.AddMember("algo", rapidjson::StringRef(m_controller->config()->algoName()), allocator); + doc.AddMember("algo", rapidjson::StringRef(m_controller->config()->algorithm().name()), allocator); doc.AddMember("hugepages", Workers::hugePages() > 0, allocator); doc.AddMember("donate_level", m_controller->config()->donateLevel(), allocator); } diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index baf5a729..5bad4af5 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -38,7 +38,6 @@ xmrig::CommonConfig::CommonConfig() : - m_algorithm(CRYPTONIGHT), m_adjusted(false), m_apiIPv6(false), m_apiRestricted(true), @@ -80,8 +79,12 @@ bool xmrig::CommonConfig::adjust() m_adjusted = true; + if (!m_algorithm.isValid()) { + m_algorithm.setAlgo(CRYPTONIGHT); + } + for (Pool &pool : m_pools) { - pool.adjust(algorithm()); + pool.adjust(m_algorithm.algo()); } return true; @@ -90,7 +93,7 @@ bool xmrig::CommonConfig::adjust() bool xmrig::CommonConfig::isValid() const { - return m_pools[0].isValid() && m_algorithm != INVALID_ALGO; + return m_pools[0].isValid() && m_algorithm.isValid(); } @@ -141,7 +144,7 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) { switch (key) { case AlgorithmKey: /* --algo */ - m_algorithm = Pool::algorithm(arg); + m_algorithm.parseAlgorithm(arg); break; case UserpassKey: /* --userpass */ @@ -181,6 +184,10 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) m_pools.back().setRigId(arg); break; + case VariantKey: /* --variant */ + m_pools.back().algorithm().parseVariant(arg); + break; + case LogFileKey: /* --log-file */ m_logFile = arg; break; @@ -199,7 +206,6 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) case RetriesKey: /* --retries */ case RetryPauseKey: /* --retry-pause */ - case VariantKey: /* --variant */ case ApiPort: /* --api-port */ case PrintTimeKey: /* --cpu-priority */ return parseUint64(key, strtol(arg, nullptr, 10)); @@ -299,7 +305,7 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) break; case VariantKey: /* --variant */ - m_pools.back().setVariant(arg); + m_pools.back().algorithm().parseVariant(arg); break; case DonateLevelKey: /* --donate-level */ diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index ebeb41fe..b04de6eb 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -43,13 +43,12 @@ public: CommonConfig(); ~CommonConfig(); - inline Algo algorithm() const { return m_algorithm; } inline bool isApiIPv6() const { return m_apiIPv6; } inline bool isApiRestricted() const { return m_apiRestricted; } inline bool isBackground() const { return m_background; } inline bool isColors() const { return m_colors; } inline bool isSyslog() const { return m_syslog; } - inline const char *algoName() const { return Pool::algoName(m_algorithm); } + inline const Algorithm &algorithm() const { return m_algorithm; } inline const char *apiToken() const { return m_apiToken.data(); } inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *logFile() const { return m_logFile.data(); } @@ -74,7 +73,7 @@ protected: bool save() override; void setFileName(const char *fileName) override; - Algo m_algorithm; + Algorithm m_algorithm; bool m_adjusted; bool m_apiIPv6; bool m_apiRestricted; diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp new file mode 100644 index 00000000..2e13fffd --- /dev/null +++ b/src/common/crypto/Algorithm.cpp @@ -0,0 +1,158 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include +#include +#include + + +#include "common/crypto/Algorithm.h" + + +#ifdef _MSC_VER +# define strncasecmp _strnicmp +# define strcasecmp _stricmp +#endif + + +#ifndef ARRAY_SIZE +# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +#endif + + +struct AlgoData +{ + const char *name; + const char *shortName; + xmrig::Algo algo; + xmrig::Variant variant; +}; + + +static AlgoData const algorithms[] = { + { "cryptonight", "cn", xmrig::CRYPTONIGHT, xmrig::VARIANT_AUTO }, + { "cryptonight/0", "cn/0", xmrig::CRYPTONIGHT, xmrig::VARIANT_0 }, + { "cryptonight/1", "cn/1", xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, + { "cryptonight/xtl", "cn/xtl", xmrig::CRYPTONIGHT, xmrig::VARIANT_XTL }, + +# ifndef XMRIG_NO_AEON + { "cryptonight-lite", "cn-lite", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, + { "cryptonight-lite/0", "cn-lite/0", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_0 }, + { "cryptonight-lite/1", "cn-lite/1", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, + { "cryptonight-lite/ipbc", "cn-lite/ipbc", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_IBPC }, +# endif + +# ifndef XMRIG_NO_SUMO + { "cryptonight-heavy", "cn-heavy", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_0 }, +# endif +}; + + +static const char *variants[] = { + "0", + "1", + "ipbc", + "xtl" +}; + + +const char *xmrig::Algorithm::variantName() const +{ + if (m_variant == VARIANT_AUTO) { + return "auto"; + } + + return variants[m_variant]; +} + + +void xmrig::Algorithm::parseAlgorithm(const char *algo) +{ + m_algo = INVALID_ALGO; + m_variant = VARIANT_AUTO; + + for (size_t i = 0; i < ARRAY_SIZE(algorithms); i++) { + if ((strcasecmp(algo, algorithms[i].name) == 0) || (strcasecmp(algo, algorithms[i].shortName) == 0)) { + m_algo = algorithms[i].algo; + m_variant = algorithms[i].variant; + break; + } + } + + if (m_algo == INVALID_ALGO) { + assert(false); + } +} + + +void xmrig::Algorithm::parseVariant(const char *variant) +{ + if (m_algo == CRYPTONIGHT_HEAVY) { + return; + } + + m_variant = VARIANT_AUTO; + + for (size_t i = 0; i < ARRAY_SIZE(variants); i++) { + if (strcasecmp(variant, variants[i]) == 0) { + m_variant = static_cast(i); + break; + } + } +} + + +void xmrig::Algorithm::parseVariant(int variant) +{ + if (variant >= VARIANT_AUTO && variant <= VARIANT_XTL) { + m_variant = static_cast(variant); + } + else { + assert(false); + } +} + + +void xmrig::Algorithm::setAlgo(Algo algo) +{ + m_algo = algo; + + if (m_algo == CRYPTONIGHT_HEAVY) { + m_variant = VARIANT_0; + } +} + + +const char *xmrig::Algorithm::name(bool shortName) const +{ + for (size_t i = 0; i < ARRAY_SIZE(algorithms); i++) { + if (algorithms[i].algo == m_algo && algorithms[i].variant == m_variant) { + return shortName ? algorithms[i].shortName : algorithms[i].name; + } + } + + return "invalid"; +} diff --git a/src/common/crypto/Algorithm.h b/src/common/crypto/Algorithm.h new file mode 100644 index 00000000..bbabb40f --- /dev/null +++ b/src/common/crypto/Algorithm.h @@ -0,0 +1,76 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __ALGORITHM_H__ +#define __ALGORITHM_H__ + + +#include "common/xmrig.h" + + +namespace xmrig { + + +class Algorithm +{ +public: + inline Algorithm() : + m_algo(INVALID_ALGO), + m_variant(VARIANT_AUTO) + {} + + inline Algorithm(Algo algo, Variant variant) : + m_variant(variant) + { + setAlgo(algo); + } + + bool isEqual(const Algorithm &other) const { return m_algo == other.m_algo && m_variant == other.m_variant; } + inline Algo algo() const { return m_algo; } + inline bool isValid() const { return m_algo != INVALID_ALGO; } + inline const char *name() const { return name(false); } + inline const char *shortName() const { return name(true); } + inline Variant variant() const { return m_variant; } + inline void setVariant(Variant variant) { m_variant = variant; } + + inline bool operator!=(const Algorithm &other) const { return !isEqual(other); } + inline bool operator==(const Algorithm &other) const { return isEqual(other); } + + const char *variantName() const; + void parseAlgorithm(const char *algo); + void parseVariant(const char *variant); + void parseVariant(int variant); + void setAlgo(Algo algo); + +private: + const char *name(bool shortName) const; + + Algo m_algo; + Variant m_variant; +}; + + +} /* namespace xmrig */ + +#endif /* __ALGORITHM_H__ */ diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index de56b315..0576854b 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -232,7 +232,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } - Job job(m_id, m_nicehash, m_pool.algorithm(), m_pool.variant(), m_rpcId); + Job job(m_id, m_nicehash, m_pool.algorithm(), m_rpcId); if (!job.setId(params["job_id"].GetString())) { *code = 3; @@ -250,7 +250,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) } if (params.HasMember("variant")) { - job.setVariant(params["variant"].GetInt()); + job.algorithm().parseVariant(params["variant"].GetInt()); } if (m_job != job) { diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index ec5279bf..34aab326 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -64,14 +64,12 @@ Job::Job() : m_size(0), m_diff(0), m_target(0), - m_blob(), - m_algorithm(xmrig::INVALID_ALGO), - m_variant(xmrig::VARIANT_AUTO) + m_blob() { } -Job::Job(int poolId, bool nicehash, xmrig::Algo algo, xmrig::Variant variant, const xmrig::Id &clientId) : +Job::Job(int poolId, bool nicehash, xmrig::Algorithm algorithm, const xmrig::Id &clientId) : m_nicehash(nicehash), m_poolId(poolId), m_threadId(-1), @@ -79,9 +77,8 @@ Job::Job(int poolId, bool nicehash, xmrig::Algo algo, xmrig::Variant variant, co m_diff(0), m_target(0), m_blob(), - m_algorithm(algo), - m_clientId(clientId), - m_variant(variant) + m_algorithm(algorithm), + m_clientId(clientId) { } @@ -166,23 +163,6 @@ bool Job::setTarget(const char *target) } -void Job::setVariant(int variant) -{ - switch (variant) { - case xmrig::VARIANT_AUTO: - case xmrig::VARIANT_V0: - case xmrig::VARIANT_V1: - m_variant = static_cast(variant); - break; - - default: - assert(false); - m_variant = xmrig::VARIANT_AUTO; - break; - } -} - - bool Job::fromHex(const char* in, unsigned int len, unsigned char* out) { bool error = false; diff --git a/src/common/net/Job.h b/src/common/net/Job.h index c235072e..08d5ae82 100644 --- a/src/common/net/Job.h +++ b/src/common/net/Job.h @@ -30,40 +30,44 @@ #include +#include "common/crypto/Algorithm.h" #include "common/net/Id.h" -#include "common/xmrig.h" class Job { public: Job(); - Job(int poolId, bool nicehash, xmrig::Algo algo, xmrig::Variant variant, const xmrig::Id &clientId); + Job(int poolId, bool nicehash, xmrig::Algorithm algorithm, const xmrig::Id &clientId); ~Job(); bool setBlob(const char *blob); bool setTarget(const char *target); - void setVariant(int variant); - inline bool isNicehash() const { return m_nicehash; } - inline bool isValid() const { return m_size > 0 && m_diff > 0; } - inline bool setId(const char *id) { return m_id.setId(id); } - inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } - inline const uint8_t *blob() const { return m_blob; } - inline const xmrig::Id &clientId() const { return m_clientId; } - inline const xmrig::Id &id() const { return m_id; } - inline int poolId() const { return m_poolId; } - inline int threadId() const { return m_threadId; } - inline size_t size() const { return m_size; } - inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } - inline uint32_t diff() const { return static_cast(m_diff); } - inline uint64_t target() const { return m_target; } - inline void reset() { m_size = 0; m_diff = 0; } - inline void setClientId(const xmrig::Id &id) { m_clientId = id; } - inline void setPoolId(int poolId) { m_poolId = poolId; } - inline void setThreadId(int threadId) { m_threadId = threadId; } - inline xmrig::Algo algorithm() const { return m_algorithm; } - inline xmrig::Variant variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? xmrig::VARIANT_V1 : xmrig::VARIANT_V0) : m_variant); } + inline bool isNicehash() const { return m_nicehash; } + inline bool isValid() const { return m_size > 0 && m_diff > 0; } + inline bool setId(const char *id) { return m_id.setId(id); } + inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } + inline const uint8_t *blob() const { return m_blob; } + inline const xmrig::Algorithm &algorithm() const { return m_algorithm; } + inline const xmrig::Id &clientId() const { return m_clientId; } + inline const xmrig::Id &id() const { return m_id; } + inline int poolId() const { return m_poolId; } + inline int threadId() const { return m_threadId; } + inline size_t size() const { return m_size; } + inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } + inline uint32_t diff() const { return static_cast(m_diff); } + inline uint64_t target() const { return m_target; } + inline void reset() { m_size = 0; m_diff = 0; } + inline void setClientId(const xmrig::Id &id) { m_clientId = id; } + inline void setPoolId(int poolId) { m_poolId = poolId; } + inline void setThreadId(int threadId) { m_threadId = threadId; } + inline xmrig::Algorithm &algorithm() { return m_algorithm; } + + inline xmrig::Variant variant() const + { + return (m_algorithm.variant() == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? xmrig::VARIANT_1 : xmrig::VARIANT_0) : m_algorithm.variant()); + } # ifdef XMRIG_PROXY_PROJECT inline char *rawBlob() { return m_rawBlob; } @@ -90,10 +94,9 @@ private: uint64_t m_diff; uint64_t m_target; uint8_t m_blob[96]; // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. - xmrig::Algo m_algorithm; + xmrig::Algorithm m_algorithm; xmrig::Id m_clientId; xmrig::Id m_id; - xmrig::Variant m_variant; # ifdef XMRIG_PROXY_PROJECT char m_rawBlob[176]; diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 4675876a..d3c15056 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -29,6 +29,7 @@ #include "common/net/Pool.h" +#include "rapidjson/document.h" #ifdef APP_DEBUG @@ -42,52 +43,10 @@ #endif -static const char *algoNames[] = { - "cryptonight", -# ifndef XMRIG_NO_AEON - "cryptonight-lite", -# else - nullptr, -# endif -# ifndef XMRIG_NO_SUMO - "cryptonight-heavy", -# else - nullptr, -# endif -# ifndef XMRIG_NO_IPBC - "cryptonight-ipbc", -# else - nullptr, -# endif -}; - - -static const char *algoNamesShort[] = { - "cn", -# ifndef XMRIG_NO_AEON - "cn-lite", -# else - nullptr, -# endif -# ifndef XMRIG_NO_SUMO - "cn-heavy", -# else - nullptr, -# endif -# ifndef XMRIG_NO_IPBC - "cn-ipbc", -# else - nullptr, -# endif -}; - - Pool::Pool() : m_nicehash(false), m_keepAlive(0), - m_port(kDefaultPort), - m_algorithm(xmrig::INVALID_ALGO), - m_variant(xmrig::VARIANT_AUTO) + m_port(kDefaultPort) { } @@ -106,23 +65,19 @@ Pool::Pool() : Pool::Pool(const char *url) : m_nicehash(false), m_keepAlive(0), - m_port(kDefaultPort), - m_algorithm(xmrig::INVALID_ALGO), - m_variant(xmrig::VARIANT_AUTO) + m_port(kDefaultPort) { parse(url); } -Pool::Pool(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, xmrig::Variant variant) : +Pool::Pool(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash) : m_nicehash(nicehash), m_keepAlive(keepAlive), m_port(port), - m_algorithm(xmrig::INVALID_ALGO), m_host(host), m_password(password), - m_user(user), - m_variant(variant) + m_user(user) { const size_t size = m_host.size() + 8; assert(size > 8); @@ -134,41 +89,6 @@ Pool::Pool(const char *host, uint16_t port, const char *user, const char *passwo } -const char *Pool::algoName(xmrig::Algo algorithm, bool shortName) -{ - if (algorithm == xmrig::INVALID_ALGO) { - return "invalid"; - } - - return (shortName ? algoNamesShort : algoNames)[algorithm]; -} - - -xmrig::Algo Pool::algorithm(const char *algo) -{ -# ifndef XMRIG_NO_AEON - if (strcasecmp(algo, "cryptonight-light") == 0) { - fprintf(stderr, "Algorithm \"cryptonight-light\" is deprecated, use \"cryptonight-lite\" instead\n"); - - return xmrig::CRYPTONIGHT_LITE; - } -# endif - - const size_t size = sizeof(algoNames) / sizeof(algoNames[0]); - - assert(size == (sizeof(algoNamesShort) / sizeof(algoNamesShort[0]))); - - for (size_t i = 0; i < size; i++) { - if ((algoNames[i] && strcasecmp(algo, algoNames[i]) == 0) || (algoNamesShort[i] && strcasecmp(algo, algoNamesShort[i]) == 0)) { - return static_cast(i); - } - } - - fprintf(stderr, "Unknown algorithm \"%s\" specified.\n", algo); - return xmrig::INVALID_ALGO; -} - - bool Pool::isEqual(const Pool &other) const { return (m_nicehash == other.m_nicehash @@ -179,8 +99,7 @@ bool Pool::isEqual(const Pool &other) const && m_password == other.m_password && m_rigId == other.m_rigId && m_url == other.m_url - && m_user == other.m_user - && m_variant == other.m_variant); + && m_user == other.m_user); } @@ -242,14 +161,54 @@ bool Pool::setUserpass(const char *userpass) } +rapidjson::Value Pool::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + auto &allocator = doc.GetAllocator(); + + Value obj(kObjectType); + + obj.AddMember("url", StringRef(url()), allocator); + obj.AddMember("user", StringRef(user()), allocator); + obj.AddMember("pass", StringRef(password()), allocator); + obj.AddMember("rig-id", rigId() ? Value(StringRef(rigId())).Move() : Value(kNullType).Move(), allocator); + +# ifndef XMRIG_PROXY_PROJECT + obj.AddMember("nicehash", isNicehash(), allocator); +# endif + + if (m_keepAlive == 0 || m_keepAlive == kKeepAliveTimeout) { + obj.AddMember("keepalive", m_keepAlive > 0, allocator); + } + else { + obj.AddMember("keepalive", m_keepAlive, allocator); + } + + switch (m_algorithm.variant()) { + case xmrig::VARIANT_AUTO: + case xmrig::VARIANT_0: + case xmrig::VARIANT_1: + obj.AddMember("variant", m_algorithm.variant(), allocator); + break; + + default: + obj.AddMember("variant", StringRef(m_algorithm.variantName()), allocator); + break; + } + + return obj; +} + + void Pool::adjust(xmrig::Algo algorithm) { if (!isValid()) { return; } - if (m_algorithm == xmrig::INVALID_ALGO) { - m_algorithm = algorithm; + if (!m_algorithm.isValid()) { + m_algorithm.setAlgo(algorithm); } if (strstr(m_host.data(), ".nicehash.com")) { @@ -257,50 +216,17 @@ void Pool::adjust(xmrig::Algo algorithm) m_nicehash = true; if (strstr(m_host.data(), "cryptonightv7.")) { - m_variant = xmrig::VARIANT_V1; + m_algorithm.setVariant(xmrig::VARIANT_1); } } if (strstr(m_host.data(), ".minergate.com")) { - m_variant = xmrig::VARIANT_V1; m_keepAlive = false; + m_algorithm.setVariant(xmrig::VARIANT_1); } } -void Pool::setVariant(int variant) -{ - switch (variant) { - case xmrig::VARIANT_AUTO: - case xmrig::VARIANT_V0: - case xmrig::VARIANT_V1: - m_variant = static_cast(variant); - break; - - default: - assert(false); - break; - } -} - - -xmrig::Variant Pool::variant() const -{ - switch (m_algorithm) { - case xmrig::CRYPTONIGHT_HEAVY: - return xmrig::VARIANT_V0; - - case xmrig::CRYPTONIGHT_IPBC: - return xmrig::VARIANT_V1; - - default: - break; - } - - return m_variant; -} - - #ifdef APP_DEBUG void Pool::print() const { @@ -309,8 +235,8 @@ void Pool::print() const LOG_DEBUG ("port: %d", static_cast(m_port)); LOG_DEBUG ("user: %s", m_user.data()); LOG_DEBUG ("pass: %s", m_password.data()); - LOG_DEBUG ("rig_id %s", m_rigId.data()); - LOG_DEBUG ("algo: %s/%d", algoName(m_algorithm), static_cast(variant())); + LOG_DEBUG ("rig-id %s", m_rigId.data()); + LOG_DEBUG ("algo: %s", m_algorithm.name()); LOG_DEBUG ("nicehash: %d", static_cast(m_nicehash)); LOG_DEBUG ("keepAlive: %d", m_keepAlive); } diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index b1b93507..670924f2 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -28,8 +28,9 @@ #include +#include "common/crypto/Algorithm.h" #include "common/utils/c_str.h" -#include "common/xmrig.h" +#include "rapidjson/fwd.h" class Pool @@ -47,30 +48,25 @@ public: const char *user = nullptr, const char *password = nullptr, int keepAlive = 0, - bool nicehash = false, - xmrig::Variant variant = xmrig::VARIANT_AUTO + bool nicehash = false ); - static const char *algoName(xmrig::Algo algorithm, bool shortName = false); - static xmrig::Algo algorithm(const char *algo); - - inline bool isNicehash() const { return m_nicehash; } - inline bool isValid() const { return !m_host.isNull() && m_port > 0; } - inline const char *host() const { return m_host.data(); } - inline const char *password() const { return !m_password.isNull() ? m_password.data() : kDefaultPassword; } - inline const char *rigId() const { return m_rigId.data(); } - inline const char *url() const { return m_url.data(); } - inline const char *user() const { return !m_user.isNull() ? m_user.data() : kDefaultUser; } - inline int keepAlive() const { return m_keepAlive; } - inline uint16_t port() const { return m_port; } - inline void setAlgo(const char *algo) { m_algorithm = algorithm(algo); } - inline void setAlgo(xmrig::Algo algorithm) { m_algorithm = algorithm; } - inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } - inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } - inline void setPassword(const char *password) { m_password = password; } - inline void setRigId(const char *rigId) { m_rigId = rigId; } - inline void setUser(const char *user) { m_user = user; } - inline xmrig::Algo algorithm() const { return m_algorithm; } + inline bool isNicehash() const { return m_nicehash; } + inline bool isValid() const { return !m_host.isNull() && m_port > 0; } + inline const char *host() const { return m_host.data(); } + inline const char *password() const { return !m_password.isNull() ? m_password.data() : kDefaultPassword; } + inline const char *rigId() const { return m_rigId.data(); } + inline const char *url() const { return m_url.data(); } + inline const char *user() const { return !m_user.isNull() ? m_user.data() : kDefaultUser; } + inline const xmrig::Algorithm &algorithm() const { return m_algorithm; } + inline int keepAlive() const { return m_keepAlive; } + inline uint16_t port() const { return m_port; } + inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } + inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } + inline void setPassword(const char *password) { m_password = password; } + inline void setRigId(const char *rigId) { m_rigId = rigId; } + inline void setUser(const char *user) { m_user = user; } + inline xmrig::Algorithm &algorithm() { return m_algorithm; } inline bool operator!=(const Pool &other) const { return !isEqual(other); } inline bool operator==(const Pool &other) const { return isEqual(other); } @@ -78,9 +74,8 @@ public: bool isEqual(const Pool &other) const; bool parse(const char *url); bool setUserpass(const char *userpass); + rapidjson::Value toJSON(rapidjson::Document &doc) const; void adjust(xmrig::Algo algorithm); - void setVariant(int variant); - xmrig::Variant variant() const; # ifdef APP_DEBUG void print() const; @@ -92,13 +87,12 @@ private: bool m_nicehash; int m_keepAlive; uint16_t m_port; - xmrig::Algo m_algorithm; + xmrig::Algorithm m_algorithm; xmrig::c_str m_host; xmrig::c_str m_password; xmrig::c_str m_rigId; xmrig::c_str m_url; xmrig::c_str m_user; - xmrig::Variant m_variant; }; #endif /* __POOL_H__ */ diff --git a/src/common/net/Storage.h b/src/common/net/Storage.h index 752e9512..f36ce594 100644 --- a/src/common/net/Storage.h +++ b/src/common/net/Storage.h @@ -29,9 +29,6 @@ #include -#include "common/log/Log.h" - - namespace xmrig { diff --git a/src/common/utils/c_str.h b/src/common/utils/c_str.h index 9a11ade6..3cc4d326 100644 --- a/src/common/utils/c_str.h +++ b/src/common/utils/c_str.h @@ -68,7 +68,7 @@ public: inline bool isEqual(const char *str) const { - return (m_data != nullptr && str != nullptr && strcmp(m_data, str)) || (m_data == nullptr && m_data == nullptr); + return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && m_data == nullptr); } diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 2de1a798..3349d3b6 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -33,8 +33,7 @@ enum Algo { INVALID_ALGO = -1, CRYPTONIGHT, /* CryptoNight (Monero) */ CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ - CRYPTONIGHT_HEAVY, /* CryptoNight-Heavy (SUMO) */ - CRYPTONIGHT_IPBC /* CryptoNight-IPBC (IPBC) */ + CRYPTONIGHT_HEAVY /* CryptoNight-Heavy (SUMO) */ }; @@ -60,8 +59,10 @@ enum AlgoVariant { enum Variant { VARIANT_AUTO = -1, // Autodetect - VARIANT_V0 = 0, // Original CryptoNight or CryptoNight-Heavy - VARIANT_V1 = 1 // Monero v7 PoW + VARIANT_0 = 0, // Original CryptoNight or CryptoNight-Heavy + VARIANT_1 = 1, // CryptoNight variant 1 also known as Monero7 and CryptoNightV7 + VARIANT_IBPC = 2, // CryptoNight Lite variant 1 with XOR (IPBC only) + VARIANT_XTL = 3 // CryptoNight variant 1 (Stellite only) }; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 43abbbbd..329d595a 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -71,7 +71,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); - doc.AddMember("algo", StringRef(algoName()), allocator); + doc.AddMember("algo", StringRef(algorithm().name()), allocator); Value api(kObjectType); api.AddMember("port", apiPort(), allocator); @@ -103,24 +103,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const Value pools(kArrayType); for (const Pool &pool : m_pools) { - Value obj(kObjectType); - - obj.AddMember("url", StringRef(pool.url()), allocator); - obj.AddMember("user", StringRef(pool.user()), allocator); - obj.AddMember("pass", StringRef(pool.password()), allocator); - obj.AddMember("rig-id", pool.rigId() ? Value(StringRef(pool.rigId())).Move() : Value(kNullType).Move(), allocator); - - if (pool.keepAlive() == 0 || pool.keepAlive() == Pool::kKeepAliveTimeout) { - obj.AddMember("keepalive", pool.keepAlive() > 0, allocator); - } - else { - obj.AddMember("keepalive", pool.keepAlive(), allocator); - } - - obj.AddMember("nicehash", pool.isNicehash(), allocator); - obj.AddMember("variant", pool.variant(), allocator); - - pools.PushBack(obj, allocator); + pools.PushBack(pool.toJSON(doc), allocator); } doc.AddMember("pools", pools, allocator); @@ -169,7 +152,7 @@ bool xmrig::Config::adjust() const bool softAES = (m_aesMode == AES_AUTO ? (Cpu::hasAES() ? AES_HW : AES_SOFT) : m_aesMode) == AES_SOFT; for (size_t i = 0; i < m_threads.cpu.size(); ++i) { - m_threads.list.push_back(CpuThread::createFromData(i, m_algorithm, m_threads.cpu[i], m_priority, softAES)); + m_threads.list.push_back(CpuThread::createFromData(i, m_algorithm.algo(), m_threads.cpu[i], m_priority, softAES)); } return true; @@ -178,7 +161,7 @@ bool xmrig::Config::adjust() m_algoVariant = getAlgoVariant(); m_threads.mode = m_threads.count ? Simple : Automatic; - const size_t size = CpuThread::multiway(m_algoVariant) * cn_select_memory(m_algorithm) / 1024; + const size_t size = CpuThread::multiway(m_algoVariant) * cn_select_memory(m_algorithm.algo()) / 1024; if (!m_threads.count) { m_threads.count = Cpu::optimalThreadsCount(size, m_maxCpuUsage); @@ -191,7 +174,7 @@ bool xmrig::Config::adjust() } for (size_t i = 0; i < m_threads.count; ++i) { - m_threads.list.push_back(CpuThread::createFromAV(i, m_algorithm, m_algoVariant, m_threads.mask, m_priority)); + m_threads.list.push_back(CpuThread::createFromAV(i, m_algorithm.algo(), m_algoVariant, m_threads.mask, m_priority)); } return true; @@ -351,7 +334,7 @@ bool xmrig::Config::parseInt(int key, int arg) xmrig::AlgoVariant xmrig::Config::getAlgoVariant() const { # ifndef XMRIG_NO_AEON - if (m_algorithm == xmrig::CRYPTONIGHT_LITE) { + if (m_algorithm.algo() == xmrig::CRYPTONIGHT_LITE) { return getAlgoVariantLite(); } # endif diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h index 6004edbd..7e6627fd 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/CryptoNight_constants.h @@ -47,16 +47,12 @@ constexpr const size_t CRYPTONIGHT_HEAVY_MEMORY = 4 * 1024 * 1024; constexpr const uint32_t CRYPTONIGHT_HEAVY_MASK = 0x3FFFF0; constexpr const uint32_t CRYPTONIGHT_HEAVY_ITER = 0x40000; -constexpr const size_t CRYPTONIGHT_IPBC_MEMORY = 1 * 1024 * 1024; -constexpr const uint32_t CRYPTONIGHT_IPBC_MASK = 0xFFFF0; -constexpr const uint32_t CRYPTONIGHT_IPBC_ITER = 0x40000; - template inline constexpr size_t cn_select_memory() { return 0; } template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_MEMORY; } template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_LITE_MEMORY; } template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_HEAVY_MEMORY; } -template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_IPBC_MEMORY; } + inline size_t cn_select_memory(Algo algorithm) { @@ -71,9 +67,6 @@ inline size_t cn_select_memory(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_MEMORY; - case CRYPTONIGHT_IPBC: - return CRYPTONIGHT_IPBC_MEMORY; - default: break; } @@ -86,7 +79,7 @@ template inline constexpr uint32_t cn_select_mask() { retur template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_MASK; } template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_LITE_MASK; } template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_HEAVY_MASK; } -template<> inline constexpr uint32_t cn_select_mask() { return CRYPTONIGHT_IPBC_MASK; } + inline uint32_t cn_select_mask(Algo algorithm) { @@ -101,9 +94,6 @@ inline uint32_t cn_select_mask(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_MASK; - case CRYPTONIGHT_IPBC: - return CRYPTONIGHT_IPBC_MASK; - default: break; } @@ -116,7 +106,7 @@ template inline constexpr uint32_t cn_select_iter() { retur template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_IPBC_ITER; } + inline uint32_t cn_select_iter(Algo algorithm) { @@ -131,9 +121,6 @@ inline uint32_t cn_select_iter(Algo algorithm) case CRYPTONIGHT_HEAVY: return CRYPTONIGHT_HEAVY_ITER; - case CRYPTONIGHT_IPBC: - return CRYPTONIGHT_IPBC_ITER; - default: break; } diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 4101245e..bcd13ee1 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -61,7 +61,7 @@ Network::Network(xmrig::Controller *controller) : } if (controller->config()->donateLevel() > 0) { - m_donate = new DonateStrategy(controller->config()->donateLevel(), controller->config()->pools().front().user(), controller->config()->algorithm(), this); + m_donate = new DonateStrategy(controller->config()->donateLevel(), controller->config()->pools().front().user(), controller->config()->algorithm().algo(), this); } m_timer.data = this; @@ -166,9 +166,9 @@ bool Network::isColors() const void Network::setJob(Client *client, const Job &job, bool donate) { - LOG_INFO(isColors() ? MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s/%d") - : "new job from %s:%d diff %d algo %s/%d", - client->host(), client->port(), job.diff(), Pool::algoName(job.algorithm(), true), static_cast(job.variant())); + LOG_INFO(isColors() ? MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s") + : "new job from %s:%d diff %d algo %s", + client->host(), client->port(), job.diff(), job.algorithm().shortName()); m_state.diff = job.diff(); Workers::setJob(job, donate); diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index ad8e88c0..3bb5caa2 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -63,15 +63,12 @@ DonateStrategy::DonateStrategy(int level, const char *user, xmrig::Algo algo, IS else if (algo == xmrig::CRYPTONIGHT_HEAVY) { m_pools.push_back(Pool(kDonatePool1, 8888, userId, nullptr, false, true)); } - else if (algo == xmrig::CRYPTONIGHT_IPBC) { - m_pools.push_back(Pool(kDonatePool1, 13333, userId, nullptr, false, true)); - } else { m_pools.push_back(Pool(kDonatePool1, 5555, userId, nullptr, false, true)); } for (Pool &pool : m_pools) { - pool.setAlgo(algo); + pool.algorithm().setAlgo(algo); } if (m_pools.size() > 1) { diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 5c0e8c0b..758eb982 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -62,69 +62,69 @@ bool xmrig::CpuThread::isSoftAES(AlgoVariant av) xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant av, Variant variant) { - assert(variant == VARIANT_V0 || variant == VARIANT_V1); + assert(variant == VARIANT_0 || variant == VARIANT_1); static const cn_hash_fun func_table[50] = { - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, # ifndef XMRIG_NO_AEON - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif # ifndef XMRIG_NO_SUMO - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif @@ -132,7 +132,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a # ifndef XMRIG_NO_SUMO if (algorithm == CRYPTONIGHT_HEAVY) { - variant = VARIANT_V0; + variant = VARIANT_0; } # endif @@ -252,7 +252,6 @@ rapidjson::Value xmrig::CpuThread::toAPI(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); obj.AddMember("type", "cpu", allocator); - obj.AddMember("algo", rapidjson::StringRef(Pool::algoName(algorithm())), allocator); obj.AddMember("av", m_av, allocator); obj.AddMember("low_power_mode", multiway(), allocator); obj.AddMember("affine_to_cpu", affinity(), allocator); diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 33f5864d..c3cd9782 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -50,21 +50,21 @@ MultiWorker::~MultiWorker() template bool MultiWorker::selfTest() { - if (m_thread->fn(xmrig::VARIANT_V0) == nullptr) { + if (m_thread->fn(xmrig::VARIANT_0) == nullptr) { return false; } - m_thread->fn(xmrig::VARIANT_V0)(test_input, 76, m_hash, m_ctx); + m_thread->fn(xmrig::VARIANT_0)(test_input, 76, m_hash, m_ctx); if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_hash, test_output_v0, sizeof m_hash) == 0) { - m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); + m_thread->fn(xmrig::VARIANT_1)(test_input, 76, m_hash, m_ctx); return memcmp(m_hash, test_output_v1, sizeof m_hash) == 0; } # ifndef XMRIG_NO_AEON if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_hash, test_output_v0_lite, sizeof m_hash) == 0) { - m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); + m_thread->fn(xmrig::VARIANT_1)(test_input, 76, m_hash, m_ctx); return memcmp(m_hash, test_output_v1_lite, sizeof m_hash) == 0; } diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 67b2973d..497e3060 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -132,7 +132,7 @@ void Workers::setJob(const Job &job, bool donate) void Workers::start(xmrig::Controller *controller) { const std::vector &threads = controller->config()->threads(); - m_status.algo = controller->config()->algorithm(); + m_status.algo = controller->config()->algorithm().algo(); m_status.colors = controller->config()->isColors(); m_status.threads = threads.size(); From ba5f92c6dd07b1cdd9a21e74fbee563b6e5bf42d Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 25 Apr 2018 18:31:18 +0700 Subject: [PATCH 243/389] Added support for cn-lite/ipbc. --- src/crypto/CryptoNight_test.h | 15 +++++++++ src/crypto/CryptoNight_x86.h | 59 ++++++++++++++++++++++++++--------- src/workers/CpuThread.cpp | 34 ++++++++++++++++++-- src/workers/MultiWorker.cpp | 6 +++- 4 files changed, 96 insertions(+), 18 deletions(-) diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 93cbf23d..66630393 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -112,6 +112,21 @@ const static uint8_t test_output_v1_lite[160] = { 0x8C, 0x2B, 0xA4, 0x1F, 0x60, 0x76, 0x39, 0xD7, 0xF6, 0x46, 0x77, 0x18, 0x20, 0xAD, 0xD4, 0xC9, 0x87, 0xF7, 0x37, 0xDA, 0xFD, 0xBA, 0xBA, 0xD2, 0xF2, 0x68, 0xDC, 0x26, 0x8D, 0x1B, 0x08, 0xC6 }; + + +// IPBC +const static uint8_t test_output_ipbc_lite[160] = { + 0xE4, 0x93, 0x8C, 0xAA, 0x59, 0x8D, 0x02, 0x8A, 0xB8, 0x6F, 0x25, 0xD2, 0xB1, 0x23, 0xD0, 0xD5, + 0x33, 0xE3, 0x9F, 0x37, 0xAC, 0xE5, 0xF8, 0xEB, 0x7A, 0xE8, 0x40, 0xEB, 0x5D, 0xB1, 0x35, 0x5F, + 0xB2, 0x47, 0x86, 0xF0, 0x7F, 0x6F, 0x4B, 0x55, 0x3E, 0xA1, 0xBB, 0xE8, 0xA1, 0x75, 0x00, 0x2D, + 0x07, 0x9A, 0x21, 0x0E, 0xBD, 0x06, 0x6A, 0xB0, 0xFD, 0x96, 0x9E, 0xE6, 0xE4, 0x69, 0x67, 0xBB, + 0x88, 0x45, 0x0B, 0x91, 0x0B, 0x7B, 0xCB, 0x21, 0x3C, 0x3C, 0x09, 0x30, 0x07, 0x71, 0x07, 0xD5, + 0xB8, 0x2D, 0x83, 0x09, 0xAF, 0x7E, 0xB2, 0xA8, 0xAC, 0x25, 0xDC, 0x10, 0xF8, 0x63, 0x6A, 0xBC, + 0x73, 0x01, 0x4E, 0xA8, 0x1C, 0xDA, 0x9A, 0x86, 0x17, 0xEC, 0xA8, 0xFB, 0xAA, 0x23, 0x23, 0x17, + 0xE1, 0x32, 0x68, 0x9C, 0x4C, 0xF4, 0x08, 0xED, 0xB0, 0x15, 0xC3, 0xA9, 0x0F, 0xF0, 0xA2, 0x7E, + 0xD9, 0xE4, 0x23, 0xA7, 0x9E, 0x91, 0xD8, 0x73, 0x94, 0xD6, 0x6C, 0x70, 0x9B, 0x8B, 0x72, 0x92, + 0xA3, 0xA4, 0x0A, 0xE2, 0x3C, 0x0A, 0x34, 0x88, 0xA1, 0x6D, 0xFE, 0x02, 0x44, 0x60, 0x7B, 0x3D +}; #endif diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index a1651cc3..75246b6e 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -457,13 +457,22 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si al0 += hi; ah0 += lo; - VARIANT1_2(ah0, 0); ((uint64_t*)&l0[idx0 & MASK])[0] = al0; - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; - VARIANT1_2(ah0, 0); - ah0 ^= ch; + if (VARIANT > 0) { + if (VARIANT == xmrig::VARIANT_IBPC) { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; + } + else { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; + } + } + else { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + } + al0 ^= cl; + ah0 ^= ch; idx0 = al0; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { @@ -556,13 +565,22 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si al0 += hi; ah0 += lo; - VARIANT1_2(ah0, 0); - ((uint64_t*) &l0[idx0 & MASK])[0] = al0; - ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; - VARIANT1_2(ah0, 0); + ((uint64_t*)&l0[idx0 & MASK])[0] = al0; + + if (VARIANT > 0) { + if (VARIANT == xmrig::VARIANT_IBPC) { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; + } + else { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; + } + } + else { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + } - ah0 ^= ch; al0 ^= cl; + ah0 ^= ch; idx0 = al0; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { @@ -581,13 +599,22 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si al1 += hi; ah1 += lo; - VARIANT1_2(ah1, 1); - ((uint64_t*) &l1[idx1 & MASK])[0] = al1; - ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; - VARIANT1_2(ah1, 1); + ((uint64_t*)&l1[idx1 & MASK])[0] = al1; + + if (VARIANT > 0) { + if (VARIANT == xmrig::VARIANT_IBPC) { + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; + } + else { + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1; + } + } + else { + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1; + } - ah1 ^= ch; al1 ^= cl; + ah1 ^= ch; idx1 = al1; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { @@ -644,6 +671,10 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si \ if (VARIANT > 0) { \ _mm_store_si128(ptr, _mm_xor_si128(a, mc)); \ + \ + if (VARIANT == xmrig::VARIANT_IBPC) { \ + ((uint64_t*)ptr)[1] ^= ((uint64_t*)ptr)[0]; \ + } \ } else { \ _mm_store_si128(ptr, a); \ } \ diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 758eb982..e08ffecd 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -62,9 +62,9 @@ bool xmrig::CpuThread::isSoftAES(AlgoVariant av) xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant av, Variant variant) { - assert(variant == VARIANT_0 || variant == VARIANT_1); + assert(variant == VARIANT_0 || variant == VARIANT_1 || variant == VARIANT_IBPC || variant == VARIANT_XTL); - static const cn_hash_fun func_table[50] = { + static const cn_hash_fun func_table[90] = { cryptonight_single_hash, cryptonight_double_hash, cryptonight_single_hash, @@ -87,6 +87,19 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_quad_hash, cryptonight_penta_hash, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + # ifndef XMRIG_NO_AEON cryptonight_single_hash, cryptonight_double_hash, @@ -109,9 +122,24 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_triple_hash, cryptonight_quad_hash, cryptonight_penta_hash, + + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif # ifndef XMRIG_NO_SUMO @@ -136,7 +164,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a } # endif - return func_table[20 * algorithm + 10 * variant + av - 1]; + return func_table[40 * algorithm + 10 * variant + av - 1]; } diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index c3cd9782..012876a3 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -65,8 +65,12 @@ bool MultiWorker::selfTest() # ifndef XMRIG_NO_AEON if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_hash, test_output_v0_lite, sizeof m_hash) == 0) { m_thread->fn(xmrig::VARIANT_1)(test_input, 76, m_hash, m_ctx); + if (memcmp(m_hash, test_output_v1_lite, sizeof m_hash) != 0) { + return false; + } - return memcmp(m_hash, test_output_v1_lite, sizeof m_hash) == 0; + m_thread->fn(xmrig::VARIANT_IBPC)(test_input, 76, m_hash, m_ctx); + return memcmp(m_hash, test_output_ipbc_lite, sizeof m_hash) == 0; } # endif From a9cc5c52582298c18f5b62b299d391aebc8a543a Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 25 Apr 2018 19:09:08 +0700 Subject: [PATCH 244/389] Basic algo selection from pool/proxy. --- src/common/net/Client.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index 0576854b..9a2b8b1b 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -249,6 +249,10 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } + if (params.HasMember("algo")) { + job.algorithm().parseAlgorithm(params["algo"].GetString()); + } + if (params.HasMember("variant")) { job.algorithm().parseVariant(params["variant"].GetInt()); } From 230962230f40a22f9364d24fad636ba8185c5426 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 25 Apr 2018 22:03:26 +0700 Subject: [PATCH 245/389] Better algorithm validation. --- src/common/crypto/Algorithm.cpp | 16 ++++++++++++++++ src/common/crypto/Algorithm.h | 2 +- src/workers/CpuThread.cpp | 13 ++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index 2e13fffd..8d95acde 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -79,6 +79,22 @@ static const char *variants[] = { }; +bool xmrig::Algorithm::isValid() const +{ + if (m_algo == INVALID_ALGO) { + return false; + } + + for (size_t i = 0; i < ARRAY_SIZE(algorithms); i++) { + if (algorithms[i].algo == m_algo && algorithms[i].variant == m_variant) { + return true; + } + } + + return false; +} + + const char *xmrig::Algorithm::variantName() const { if (m_variant == VARIANT_AUTO) { diff --git a/src/common/crypto/Algorithm.h b/src/common/crypto/Algorithm.h index bbabb40f..aff7a8c8 100644 --- a/src/common/crypto/Algorithm.h +++ b/src/common/crypto/Algorithm.h @@ -48,7 +48,6 @@ public: bool isEqual(const Algorithm &other) const { return m_algo == other.m_algo && m_variant == other.m_variant; } inline Algo algo() const { return m_algo; } - inline bool isValid() const { return m_algo != INVALID_ALGO; } inline const char *name() const { return name(false); } inline const char *shortName() const { return name(true); } inline Variant variant() const { return m_variant; } @@ -57,6 +56,7 @@ public: inline bool operator!=(const Algorithm &other) const { return !isEqual(other); } inline bool operator==(const Algorithm &other) const { return isEqual(other); } + bool isValid() const; const char *variantName() const; void parseAlgorithm(const char *algo); void parseVariant(const char *variant); diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index e08ffecd..86699afc 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -164,7 +164,18 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a } # endif - return func_table[40 * algorithm + 10 * variant + av - 1]; + const size_t index = 40 * algorithm + 10 * variant + av - 1; + +# ifndef NDEBUG + cn_hash_fun func = func_table[index]; + + assert(index < sizeof(func_table) / sizeof(func_table[0])); + assert(func != nullptr); + + return func; +# else + return func_table[index]; +# endif } From bc2660f509ba384087f1b86cdffe61daace9a3df Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 25 Apr 2018 23:17:27 +0700 Subject: [PATCH 246/389] Add support for skip invalid pools. --- src/common/config/CommonConfig.cpp | 31 +++++++++++++++++++---------- src/common/config/CommonConfig.h | 13 +++++++++--- src/common/config/ConfigLoader.cpp | 11 +++++----- src/common/config/ConfigWatcher.cpp | 2 +- src/core/Config.cpp | 8 ++++++-- src/core/Config.h | 2 +- src/interfaces/IConfig.h | 3 +-- 7 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 5bad4af5..5eaf68fd 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -55,7 +55,8 @@ xmrig::CommonConfig::CommonConfig() : m_donateLevel(kDefaultDonateLevel), m_printTime(60), m_retries(5), - m_retryPause(5) + m_retryPause(5), + m_state(NoneState) { m_pools.push_back(Pool()); @@ -71,13 +72,15 @@ xmrig::CommonConfig::~CommonConfig() } -bool xmrig::CommonConfig::adjust() +bool xmrig::CommonConfig::finalize() { - if (m_adjusted) { - return false; + if (m_state == ReadyState) { + return true; } - m_adjusted = true; + if (m_state == ErrorState) { + return false; + } if (!m_algorithm.isValid()) { m_algorithm.setAlgo(CRYPTONIGHT); @@ -85,18 +88,24 @@ bool xmrig::CommonConfig::adjust() for (Pool &pool : m_pools) { pool.adjust(m_algorithm.algo()); + + if (pool.isValid() && pool.algorithm().isValid()) { + m_activePools.push_back(std::move(pool)); + } } + m_pools.clear(); + + if (m_activePools.empty()) { + m_state = ErrorState; + return false; + } + + m_state = ReadyState; return true; } -bool xmrig::CommonConfig::isValid() const -{ - return m_pools[0].isValid() && m_algorithm.isValid(); -} - - bool xmrig::CommonConfig::parseBoolean(int key, bool enable) { switch (key) { diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index b04de6eb..d54afe3a 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -53,7 +53,7 @@ public: inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *logFile() const { return m_logFile.data(); } inline const char *userAgent() const { return m_userAgent.data(); } - inline const std::vector &pools() const { return m_pools; } + inline const std::vector &pools() const { return m_activePools; } inline int apiPort() const { return m_apiPort; } inline int donateLevel() const { return m_donateLevel; } inline int printTime() const { return m_printTime; } @@ -65,8 +65,13 @@ public: inline const char *fileName() const override { return m_fileName.data(); } protected: - bool adjust() override; - bool isValid() const override; + enum State { + NoneState, + ReadyState, + ErrorState + }; + + bool finalize() override; bool parseBoolean(int key, bool enable) override; bool parseString(int key, const char *arg) override; bool parseUint64(int key, uint64_t arg) override; @@ -86,6 +91,8 @@ protected: int m_printTime; int m_retries; int m_retryPause; + State m_state; + std::vector m_activePools; std::vector m_pools; xmrig::c_str m_apiToken; xmrig::c_str m_apiWorkerId; diff --git a/src/common/config/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp index cc92213f..919ff00c 100644 --- a/src/common/config/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -108,9 +108,8 @@ bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const rapidjson:: } config->parseJSON(doc); - config->adjust(); - return config->isValid(); + return config->finalize(); } @@ -163,11 +162,14 @@ xmrig::IConfig *xmrig::ConfigLoader::load(int argc, char **argv, IConfigCreator return nullptr; } - if (!config->isValid()) { + if (!config->finalize()) { + delete config; + + config = m_creator->create(); loadFromFile(config, Platform::defaultConfigName()); } - if (!config->isValid()) { + if (!config->finalize()) { fprintf(stderr, "No valid configuration found. Exiting.\n"); delete config; return nullptr; @@ -177,7 +179,6 @@ xmrig::IConfig *xmrig::ConfigLoader::load(int argc, char **argv, IConfigCreator m_watcher = new xmrig::ConfigWatcher(config->fileName(), creator, listener); } - config->adjust(); return config; } diff --git a/src/common/config/ConfigWatcher.cpp b/src/common/config/ConfigWatcher.cpp index a08b2be7..bde35f23 100644 --- a/src/common/config/ConfigWatcher.cpp +++ b/src/common/config/ConfigWatcher.cpp @@ -83,7 +83,7 @@ void xmrig::ConfigWatcher::reload() IConfig *config = m_creator->create(); ConfigLoader::loadFromFile(config, m_path.data()); - if (!config->isValid()) { + if (!config->finalize()) { LOG_ERR("reloading failed"); delete config; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 329d595a..a27dc917 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -141,9 +141,13 @@ xmrig::Config *xmrig::Config::load(int argc, char **argv, IWatcherListener *list } -bool xmrig::Config::adjust() +bool xmrig::Config::finalize() { - if (!CommonConfig::adjust()) { + if (m_state != NoneState) { + return CommonConfig::finalize(); + } + + if (!CommonConfig::finalize()) { return false; } diff --git a/src/core/Config.h b/src/core/Config.h index 13320a80..0c6a2173 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -88,7 +88,7 @@ public: static Config *load(int argc, char **argv, IWatcherListener *listener); protected: - bool adjust() override; + bool finalize() override; bool parseBoolean(int key, bool enable) override; bool parseString(int key, const char *arg) override; bool parseUint64(int key, uint64_t arg) override; diff --git a/src/interfaces/IConfig.h b/src/interfaces/IConfig.h index cf4f0d23..2422b891 100644 --- a/src/interfaces/IConfig.h +++ b/src/interfaces/IConfig.h @@ -91,8 +91,7 @@ public: virtual ~IConfig() {} - virtual bool adjust() = 0; - virtual bool isValid() const = 0; + virtual bool finalize() = 0; virtual bool isWatch() const = 0; virtual bool parseBoolean(int key, bool enable) = 0; virtual bool parseString(int key, const char *arg) = 0; From 41e8c4f887be3c0c1d4fae0faf80230a472238a7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 26 Apr 2018 15:02:01 +0700 Subject: [PATCH 247/389] Send supported algorithms to pool in login request. --- src/common/crypto/Algorithm.cpp | 2 +- src/common/crypto/Algorithm.h | 6 ++++++ src/common/net/Client.cpp | 24 ++++++++++++++-------- src/common/net/Pool.cpp | 30 +++++++++++++++++++++++++++ src/common/net/Pool.h | 36 +++++++++++++++++---------------- src/common/xmrig.h | 2 +- src/crypto/CryptoNight_x86.h | 8 ++++---- src/workers/CpuThread.cpp | 22 ++++++++++---------- src/workers/MultiWorker.cpp | 2 +- 9 files changed, 89 insertions(+), 43 deletions(-) diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index 8d95acde..3123c361 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -62,7 +62,7 @@ static AlgoData const algorithms[] = { { "cryptonight-lite", "cn-lite", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, { "cryptonight-lite/0", "cn-lite/0", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_0 }, { "cryptonight-lite/1", "cn-lite/1", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, - { "cryptonight-lite/ipbc", "cn-lite/ipbc", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_IBPC }, + { "cryptonight-lite/ipbc", "cn-lite/ipbc", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_IPBC }, # endif # ifndef XMRIG_NO_SUMO diff --git a/src/common/crypto/Algorithm.h b/src/common/crypto/Algorithm.h index aff7a8c8..a34d5c07 100644 --- a/src/common/crypto/Algorithm.h +++ b/src/common/crypto/Algorithm.h @@ -26,6 +26,9 @@ #define __ALGORITHM_H__ +#include + + #include "common/xmrig.h" @@ -71,6 +74,9 @@ private: }; +typedef std::vector Algorithms; + + } /* namespace xmrig */ #endif /* __ALGORITHM_H__ */ diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index 9a2b8b1b..c7cdd358 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -385,9 +385,10 @@ void Client::connect(sockaddr *addr) void Client::login() { + using namespace rapidjson; m_results.clear(); - rapidjson::Document doc; + Document doc; doc.SetObject(); auto &allocator = doc.GetAllocator(); @@ -396,19 +397,26 @@ void Client::login() doc.AddMember("jsonrpc", "2.0", allocator); doc.AddMember("method", "login", allocator); - rapidjson::Value params(rapidjson::kObjectType); - params.AddMember("login", rapidjson::StringRef(m_pool.user()), allocator); - params.AddMember("pass", rapidjson::StringRef(m_pool.password()), allocator); - params.AddMember("agent", rapidjson::StringRef(m_agent), allocator); + Value params(kObjectType); + params.AddMember("login", StringRef(m_pool.user()), allocator); + params.AddMember("pass", StringRef(m_pool.password()), allocator); + params.AddMember("agent", StringRef(m_agent), allocator); if (m_pool.rigId()) { - params.AddMember("rigid", rapidjson::StringRef(m_pool.rigId()), allocator); + params.AddMember("rigid", StringRef(m_pool.rigId()), allocator); } + Value algo(kArrayType); + + for (const auto &a : m_pool.algorithms()) { + algo.PushBack(StringRef(a.shortName()), allocator); + } + + params.AddMember("algo", algo, allocator); doc.AddMember("params", params, allocator); - rapidjson::StringBuffer buffer(0, 512); - rapidjson::Writer writer(buffer); + StringBuffer buffer(0, 512); + Writer writer(buffer); doc.Accept(writer); const size_t size = buffer.GetSize(); diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index d3c15056..aa1fa651 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -37,6 +37,9 @@ #endif +#define ADD_VARIANT(variant) m_algorithms.push_back(xmrig::Algorithm(m_algorithm.algo(), variant)); + + #ifdef _MSC_VER # define strncasecmp _strnicmp # define strcasecmp _stricmp @@ -224,6 +227,33 @@ void Pool::adjust(xmrig::Algo algorithm) m_keepAlive = false; m_algorithm.setVariant(xmrig::VARIANT_1); } + +# ifndef XMRIG_PROXY_PROJECT + switch (m_algorithm.algo()) { + case xmrig::CRYPTONIGHT: + ADD_VARIANT(xmrig::VARIANT_AUTO); + ADD_VARIANT(xmrig::VARIANT_0); + ADD_VARIANT(xmrig::VARIANT_1); + ADD_VARIANT(xmrig::VARIANT_XTL); + break; + + case xmrig::CRYPTONIGHT_LITE: + ADD_VARIANT(xmrig::VARIANT_AUTO); + ADD_VARIANT(xmrig::VARIANT_0); + ADD_VARIANT(xmrig::VARIANT_1); + ADD_VARIANT(xmrig::VARIANT_IPBC); + break; + + case xmrig::CRYPTONIGHT_HEAVY: + ADD_VARIANT(xmrig::VARIANT_0); + break; + + default: + break; + } +# else + m_algorithms.push_back(m_algorithm); +# endif } diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index 670924f2..eb926b3b 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -25,7 +25,7 @@ #define __POOL_H__ -#include +#include #include "common/crypto/Algorithm.h" @@ -51,22 +51,23 @@ public: bool nicehash = false ); - inline bool isNicehash() const { return m_nicehash; } - inline bool isValid() const { return !m_host.isNull() && m_port > 0; } - inline const char *host() const { return m_host.data(); } - inline const char *password() const { return !m_password.isNull() ? m_password.data() : kDefaultPassword; } - inline const char *rigId() const { return m_rigId.data(); } - inline const char *url() const { return m_url.data(); } - inline const char *user() const { return !m_user.isNull() ? m_user.data() : kDefaultUser; } - inline const xmrig::Algorithm &algorithm() const { return m_algorithm; } - inline int keepAlive() const { return m_keepAlive; } - inline uint16_t port() const { return m_port; } - inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } - inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } - inline void setPassword(const char *password) { m_password = password; } - inline void setRigId(const char *rigId) { m_rigId = rigId; } - inline void setUser(const char *user) { m_user = user; } - inline xmrig::Algorithm &algorithm() { return m_algorithm; } + inline bool isNicehash() const { return m_nicehash; } + inline bool isValid() const { return !m_host.isNull() && m_port > 0; } + inline const char *host() const { return m_host.data(); } + inline const char *password() const { return !m_password.isNull() ? m_password.data() : kDefaultPassword; } + inline const char *rigId() const { return m_rigId.data(); } + inline const char *url() const { return m_url.data(); } + inline const char *user() const { return !m_user.isNull() ? m_user.data() : kDefaultUser; } + inline const xmrig::Algorithm &algorithm() const { return m_algorithm; } + inline const xmrig::Algorithms &algorithms() const { return m_algorithms; } + inline int keepAlive() const { return m_keepAlive; } + inline uint16_t port() const { return m_port; } + inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } + inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } + inline void setPassword(const char *password) { m_password = password; } + inline void setRigId(const char *rigId) { m_rigId = rigId; } + inline void setUser(const char *user) { m_user = user; } + inline xmrig::Algorithm &algorithm() { return m_algorithm; } inline bool operator!=(const Pool &other) const { return !isEqual(other); } inline bool operator==(const Pool &other) const { return isEqual(other); } @@ -88,6 +89,7 @@ private: int m_keepAlive; uint16_t m_port; xmrig::Algorithm m_algorithm; + xmrig::Algorithms m_algorithms; xmrig::c_str m_host; xmrig::c_str m_password; xmrig::c_str m_rigId; diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 3349d3b6..a6fda9fc 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -61,7 +61,7 @@ enum Variant { VARIANT_AUTO = -1, // Autodetect VARIANT_0 = 0, // Original CryptoNight or CryptoNight-Heavy VARIANT_1 = 1, // CryptoNight variant 1 also known as Monero7 and CryptoNightV7 - VARIANT_IBPC = 2, // CryptoNight Lite variant 1 with XOR (IPBC only) + VARIANT_IPBC = 2, // CryptoNight Lite variant 1 with XOR (IPBC only) VARIANT_XTL = 3 // CryptoNight variant 1 (Stellite only) }; diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 75246b6e..75778053 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -460,7 +460,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; if (VARIANT > 0) { - if (VARIANT == xmrig::VARIANT_IBPC) { + if (VARIANT == xmrig::VARIANT_IPBC) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else { @@ -568,7 +568,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; if (VARIANT > 0) { - if (VARIANT == xmrig::VARIANT_IBPC) { + if (VARIANT == xmrig::VARIANT_IPBC) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else { @@ -602,7 +602,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l1[idx1 & MASK])[0] = al1; if (VARIANT > 0) { - if (VARIANT == xmrig::VARIANT_IBPC) { + if (VARIANT == xmrig::VARIANT_IPBC) { ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; } else { @@ -672,7 +672,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si if (VARIANT > 0) { \ _mm_store_si128(ptr, _mm_xor_si128(a, mc)); \ \ - if (VARIANT == xmrig::VARIANT_IBPC) { \ + if (VARIANT == xmrig::VARIANT_IPBC) { \ ((uint64_t*)ptr)[1] ^= ((uint64_t*)ptr)[0]; \ } \ } else { \ diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 86699afc..e42139c0 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -62,7 +62,7 @@ bool xmrig::CpuThread::isSoftAES(AlgoVariant av) xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant av, Variant variant) { - assert(variant == VARIANT_0 || variant == VARIANT_1 || variant == VARIANT_IBPC || variant == VARIANT_XTL); + assert(variant == VARIANT_0 || variant == VARIANT_1 || variant == VARIANT_IPBC || variant == VARIANT_XTL); static const cn_hash_fun func_table[90] = { cryptonight_single_hash, @@ -123,16 +123,16 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_quad_hash, cryptonight_penta_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # else diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 012876a3..b3b384f6 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -69,7 +69,7 @@ bool MultiWorker::selfTest() return false; } - m_thread->fn(xmrig::VARIANT_IBPC)(test_input, 76, m_hash, m_ctx); + m_thread->fn(xmrig::VARIANT_IPBC)(test_input, 76, m_hash, m_ctx); return memcmp(m_hash, test_output_ipbc_lite, sizeof m_hash) == 0; } # endif From 2ddac1ce6802a61d8bc463809b31d0bf607785d0 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 26 Apr 2018 15:28:33 +0700 Subject: [PATCH 248/389] Silence primary pool errors if failover active. --- src/common/net/Client.cpp | 23 ++++++++++--------- src/common/net/Client.h | 4 ++++ .../net/strategies/FailoverStrategy.cpp | 1 + .../net/strategies/SinglePoolStrategy.cpp | 3 ++- .../net/strategies/SinglePoolStrategy.h | 2 +- src/net/Network.cpp | 2 +- src/net/strategies/DonateStrategy.cpp | 2 +- 7 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index c7cdd358..f141af66 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -55,6 +55,7 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_agent(agent), m_listener(listener), m_id(id), + m_retries(5), m_retryPause(5000), m_failures(0), m_recvBufPos(0), @@ -267,7 +268,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } - if (!m_quiet) { + if (!isQuiet()) { LOG_WARN("[%s] duplicate job received, reconnect", m_pool.url()); } @@ -311,7 +312,7 @@ int Client::resolve(const char *host) const int r = uv_getaddrinfo(uv_default_loop(), &m_resolver, Client::onResolved, host, nullptr, &m_hints); if (r) { - if (!m_quiet) { + if (!isQuiet()) { LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_pool.port(), uv_strerror(r)); } return 1; @@ -453,7 +454,7 @@ void Client::parse(char *line, size_t len) LOG_DEBUG("[%s] received (%d bytes): \"%s\"", m_pool.url(), len, line); if (len < 32 || line[0] != '{') { - if (!m_quiet) { + if (!isQuiet()) { LOG_ERR("[%s] JSON decode failed", m_pool.url()); } @@ -462,7 +463,7 @@ void Client::parse(char *line, size_t len) rapidjson::Document doc; if (doc.ParseInsitu(line).HasParseError()) { - if (!m_quiet) { + if (!isQuiet()) { LOG_ERR("[%s] JSON decode failed: \"%s\"", m_pool.url(), rapidjson::GetParseError_En(doc.GetParseError())); } @@ -504,7 +505,7 @@ void Client::parseExtensions(const rapidjson::Value &value) void Client::parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error) { if (error.IsObject()) { - if (!m_quiet) { + if (!isQuiet()) { LOG_ERR("[%s] error: \"%s\", code: %d", m_pool.url(), error["message"].GetString(), error["code"].GetInt()); } return; @@ -538,7 +539,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap m_listener->onResultAccepted(this, it->second, message); m_results.erase(it); } - else if (!m_quiet) { + else if (!isQuiet()) { LOG_ERR("[%s] error: \"%s\", code: %d", m_pool.url(), message, error["code"].GetInt()); } @@ -556,7 +557,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap if (id == 1) { int code = -1; if (!parseLogin(result, &code)) { - if (!m_quiet) { + if (!isQuiet()) { LOG_ERR("[%s] login error code: %d", m_pool.url(), code); } @@ -661,7 +662,7 @@ void Client::onConnect(uv_connect_t *req, int status) } if (status < 0) { - if (!client->m_quiet) { + if (!client->isQuiet()) { LOG_ERR("[%s] connect error: \"%s\"", client->m_pool.url(), uv_strerror(status)); } @@ -689,7 +690,7 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) } if (nread < 0) { - if (nread != UV_EOF && !client->m_quiet) { + if (nread != UV_EOF && !client->isQuiet()) { LOG_ERR("[%s] read error: \"%s\"", client->m_pool.url(), uv_strerror((int) nread)); } @@ -749,7 +750,7 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) } if (status < 0) { - if (!client->m_quiet) { + if (!client->isQuiet()) { LOG_ERR("[%s] DNS error: \"%s\"", client->m_pool.url(), uv_strerror(status)); } @@ -773,7 +774,7 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) } if (ipv4.empty() && ipv6.empty()) { - if (!client->m_quiet) { + if (!client->isQuiet()) { LOG_ERR("[%s] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", client->m_pool.url()); } diff --git a/src/common/net/Client.h b/src/common/net/Client.h index 651f97b5..97bf16a5 100644 --- a/src/common/net/Client.h +++ b/src/common/net/Client.h @@ -74,6 +74,7 @@ public: inline SocketState state() const { return m_state; } inline uint16_t port() const { return m_pool.port(); } inline void setQuiet(bool quiet) { m_quiet = quiet; } + inline void setRetries(int retries) { m_retries = retries; } inline void setRetryPause(int ms) { m_retryPause = ms; } private: @@ -96,6 +97,8 @@ private: void setState(SocketState state); void startTimeout(); + inline bool isQuiet() const { return m_quiet || m_failures >= m_retries; } + static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf); static void onClose(uv_handle_t *handle); static void onConnect(uv_connect_t *req, int status); @@ -114,6 +117,7 @@ private: const char *m_agent; IClientListener *m_listener; int m_id; + int m_retries; int m_retryPause; int64_t m_failures; Job m_job; diff --git a/src/common/net/strategies/FailoverStrategy.cpp b/src/common/net/strategies/FailoverStrategy.cpp index 58854498..295b4335 100644 --- a/src/common/net/strategies/FailoverStrategy.cpp +++ b/src/common/net/strategies/FailoverStrategy.cpp @@ -157,6 +157,7 @@ void FailoverStrategy::add(const Pool &pool) { Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this); client->setPool(pool); + client->setRetries(m_retries); client->setRetryPause(m_retryPause * 1000); client->setQuiet(m_quiet); diff --git a/src/common/net/strategies/SinglePoolStrategy.cpp b/src/common/net/strategies/SinglePoolStrategy.cpp index 50620ab2..21ce7b34 100644 --- a/src/common/net/strategies/SinglePoolStrategy.cpp +++ b/src/common/net/strategies/SinglePoolStrategy.cpp @@ -28,12 +28,13 @@ #include "interfaces/IStrategyListener.h" -SinglePoolStrategy::SinglePoolStrategy(const Pool &pool, int retryPause, IStrategyListener *listener, bool quiet) : +SinglePoolStrategy::SinglePoolStrategy(const Pool &pool, int retryPause, int retries, IStrategyListener *listener, bool quiet) : m_active(false), m_listener(listener) { m_client = new Client(0, Platform::userAgent(), this); m_client->setPool(pool); + m_client->setRetries(retries); m_client->setRetryPause(retryPause * 1000); m_client->setQuiet(quiet); } diff --git a/src/common/net/strategies/SinglePoolStrategy.h b/src/common/net/strategies/SinglePoolStrategy.h index ce3d0f7f..41d90e34 100644 --- a/src/common/net/strategies/SinglePoolStrategy.h +++ b/src/common/net/strategies/SinglePoolStrategy.h @@ -37,7 +37,7 @@ class Url; class SinglePoolStrategy : public IStrategy, public IClientListener { public: - SinglePoolStrategy(const Pool &pool, int retryPause, IStrategyListener *listener, bool quiet = false); + SinglePoolStrategy(const Pool &pool, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); ~SinglePoolStrategy(); public: diff --git a/src/net/Network.cpp b/src/net/Network.cpp index bcd13ee1..7293a0ac 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -57,7 +57,7 @@ Network::Network(xmrig::Controller *controller) : m_strategy = new FailoverStrategy(pools, controller->config()->retryPause(), controller->config()->retries(), this); } else { - m_strategy = new SinglePoolStrategy(pools.front(), controller->config()->retryPause(), this); + m_strategy = new SinglePoolStrategy(pools.front(), controller->config()->retryPause(), controller->config()->retries(), this); } if (controller->config()->donateLevel() > 0) { diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 3bb5caa2..b4b41938 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -75,7 +75,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, xmrig::Algo algo, IS m_strategy = new FailoverStrategy(m_pools, 1, 2, this, true); } else { - m_strategy = new SinglePoolStrategy(m_pools.front(), 1, this, true); + m_strategy = new SinglePoolStrategy(m_pools.front(), 1, 2, this, true); } m_timer.data = this; From 85f9bd97f14eaba2432d5012772949500eb30621 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 26 Apr 2018 23:27:53 +0700 Subject: [PATCH 249/389] Verify & send algorithm name. --- src/common/net/Client.cpp | 108 ++++++++++++++++++++++++++++-------- src/common/net/Client.h | 8 +++ src/common/net/Pool.cpp | 12 ++++ src/common/net/Pool.h | 1 + src/net/JobResult.h | 22 +------- src/workers/MultiWorker.cpp | 2 +- 6 files changed, 109 insertions(+), 44 deletions(-) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index f141af66..c53397a3 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -54,6 +54,7 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_quiet(false), m_agent(agent), m_listener(listener), + m_extensions(0), m_id(id), m_retries(5), m_retryPause(5000), @@ -161,12 +162,14 @@ bool Client::disconnect() int64_t Client::submit(const JobResult &result) { + using namespace rapidjson; + # ifdef XMRIG_PROXY_PROJECT const char *nonce = result.nonce; const char *data = result.result; # else - char nonce[9]; - char data[65]; + char *nonce = m_sendBuf; + char *data = m_sendBuf + 16; Job::toHex(reinterpret_cast(&result.nonce), 4, nonce); nonce[8] = '\0'; @@ -175,8 +178,24 @@ int64_t Client::submit(const JobResult &result) data[64] = '\0'; # endif - const size_t size = snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRIu64 ",\"jsonrpc\":\"2.0\",\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"}}\n", - m_sequence, m_rpcId.data(), result.jobId.data(), nonce, data); + Document doc(kObjectType); + auto &allocator = doc.GetAllocator(); + + doc.AddMember("id", m_sequence, allocator); + doc.AddMember("jsonrpc", "2.0", allocator); + doc.AddMember("method", "submit", allocator); + + Value params(kObjectType); + params.AddMember("id", StringRef(m_rpcId.data()), allocator); + params.AddMember("job_id", StringRef(result.jobId.data()), allocator); + params.AddMember("nonce", StringRef(nonce), allocator); + params.AddMember("result", StringRef(data), allocator); + + if (m_extensions & AlgoExt) { + params.AddMember("algo", StringRef(result.algorithm.shortName()), allocator); + } + + doc.AddMember("params", params, allocator); # ifdef XMRIG_PROXY_PROJECT m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff(), result.id); @@ -184,7 +203,7 @@ int64_t Client::submit(const JobResult &result) m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff()); # endif - return send(size); + return send(doc); } @@ -258,6 +277,13 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) job.algorithm().parseVariant(params["variant"].GetInt()); } + if (!verifyAlgorithm(job.algorithm())) { + *code = 6; + + close(); + return false; + } + if (m_job != job) { m_jobs++; m_job = std::move(job); @@ -284,9 +310,7 @@ bool Client::parseLogin(const rapidjson::Value &result, int *code) return false; } -# ifndef XMRIG_PROXY_PROJECT m_nicehash = m_pool.isNicehash(); -# endif if (result.HasMember("extensions")) { parseExtensions(result["extensions"]); @@ -299,6 +323,27 @@ bool Client::parseLogin(const rapidjson::Value &result, int *code) } +bool Client::verifyAlgorithm(const xmrig::Algorithm &algorithm) const +{ + if (m_pool.isCompatible(algorithm)) { + return true; + } + + if (isQuiet()) { + return false; + } + + if (algorithm.isValid()) { + LOG_ERR("Incompatible algorithm \"%s\" detected, reconnect", algorithm.name()); + } + else { + LOG_ERR("Unknown/unsupported algorithm detected, reconnect"); + } + + return false; +} + + int Client::resolve(const char *host) { setState(HostLookupState); @@ -322,6 +367,27 @@ int Client::resolve(const char *host) } +int64_t Client::send(const rapidjson::Document &doc) +{ + using namespace rapidjson; + + StringBuffer buffer(0, 512); + Writer writer(buffer); + doc.Accept(writer); + + const size_t size = buffer.GetSize(); + if (size > (sizeof(m_buf) - 2)) { + return -1; + } + + memcpy(m_sendBuf, buffer.GetString(), size); + m_sendBuf[size] = '\n'; + m_sendBuf[size + 1] = '\0'; + + return send(size + 1); +} + + int64_t Client::send(size_t size) { LOG_DEBUG("[%s] send (%d bytes): \"%s\"", m_pool.url(), size, m_sendBuf); @@ -389,9 +455,7 @@ void Client::login() using namespace rapidjson; m_results.clear(); - Document doc; - doc.SetObject(); - + Document doc(kObjectType); auto &allocator = doc.GetAllocator(); doc.AddMember("id", 1, allocator); @@ -416,20 +480,7 @@ void Client::login() params.AddMember("algo", algo, allocator); doc.AddMember("params", params, allocator); - StringBuffer buffer(0, 512); - Writer writer(buffer); - doc.Accept(writer); - - const size_t size = buffer.GetSize(); - if (size > (sizeof(m_buf) - 2)) { - return; - } - - memcpy(m_sendBuf, buffer.GetString(), size); - m_sendBuf[size] = '\n'; - m_sendBuf[size + 1] = '\0'; - - send(size + 1); + send(doc); } @@ -486,6 +537,8 @@ void Client::parse(char *line, size_t len) void Client::parseExtensions(const rapidjson::Value &value) { + m_extensions = 0; + if (!value.IsArray()) { return; } @@ -495,8 +548,15 @@ void Client::parseExtensions(const rapidjson::Value &value) continue; } + if (strcmp(ext.GetString(), "algo") == 0) { + m_extensions |= AlgoExt; + continue; + } + if (strcmp(ext.GetString(), "nicehash") == 0) { + m_extensions |= NicehashExt; m_nicehash = true; + continue; } } } diff --git a/src/common/net/Client.h b/src/common/net/Client.h index 97bf16a5..27273092 100644 --- a/src/common/net/Client.h +++ b/src/common/net/Client.h @@ -78,11 +78,18 @@ public: inline void setRetryPause(int ms) { m_retryPause = ms; } private: + enum Extensions { + NicehashExt = 1, + AlgoExt = 2 + }; + bool close(); bool isCriticalError(const char *message); bool parseJob(const rapidjson::Value ¶ms, int *code); bool parseLogin(const rapidjson::Value &result, int *code); + bool verifyAlgorithm(const xmrig::Algorithm &algorithm) const; int resolve(const char *host); + int64_t send(const rapidjson::Document &doc); int64_t send(size_t size); void connect(const std::vector &ipv4, const std::vector &ipv6); void connect(sockaddr *addr); @@ -116,6 +123,7 @@ private: char m_sendBuf[768]; const char *m_agent; IClientListener *m_listener; + int m_extensions; int m_id; int m_retries; int m_retryPause; diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index aa1fa651..ab3bdea7 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -92,6 +92,18 @@ Pool::Pool(const char *host, uint16_t port, const char *user, const char *passwo } +bool Pool::isCompatible(const xmrig::Algorithm &algorithm) const +{ + for (const auto &a : m_algorithms) { + if (algorithm == a) { + return true; + } + } + + return false; +} + + bool Pool::isEqual(const Pool &other) const { return (m_nicehash == other.m_nicehash diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index eb926b3b..5475e10d 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -72,6 +72,7 @@ public: inline bool operator!=(const Pool &other) const { return !isEqual(other); } inline bool operator==(const Pool &other) const { return isEqual(other); } + bool isCompatible(const xmrig::Algorithm &algorithm) const; bool isEqual(const Pool &other) const; bool parse(const char *url); bool setUserpass(const char *userpass); diff --git a/src/net/JobResult.h b/src/net/JobResult.h index 68afc862..4a920ca0 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -36,34 +36,17 @@ class JobResult { public: inline JobResult() : poolId(0), diff(0), nonce(0) {} - inline JobResult(int poolId, const xmrig::Id &jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : + inline JobResult(int poolId, const xmrig::Id &jobId, uint32_t nonce, const uint8_t *result, uint32_t diff, const xmrig::Algorithm &algorithm) : poolId(poolId), diff(diff), nonce(nonce), + algorithm(algorithm), jobId(jobId) { memcpy(this->result, result, sizeof(this->result)); } - inline JobResult(const Job &job) : poolId(0), diff(0), nonce(0) - { - jobId = job.id(); - poolId = job.poolId(); - diff = job.diff(); - nonce = *job.nonce(); - } - - - inline JobResult &operator=(const Job &job) { - jobId = job.id(); - poolId = job.poolId(); - diff = job.diff(); - - return *this; - } - - inline uint64_t actualDiff() const { return Job::toDiff(reinterpret_cast(result)[3]); @@ -74,6 +57,7 @@ public: uint32_t diff; uint32_t nonce; uint8_t result[32]; + xmrig::Algorithm algorithm; xmrig::Id jobId; }; diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index b3b384f6..d4d5992c 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -108,7 +108,7 @@ void MultiWorker::start() for (size_t i = 0; i < N; ++i) { if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) { - Workers::submit(JobResult(m_state.job.poolId(), m_state.job.id(), *nonce(i), m_hash + (i * 32), m_state.job.diff())); + Workers::submit(JobResult(m_state.job.poolId(), m_state.job.id(), *nonce(i), m_hash + (i * 32), m_state.job.diff(), m_state.job.algorithm())); } *nonce(i) += 1; From 3df99fbcedb0d736e7c16e51b5d988e92518fe6d Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 27 Apr 2018 00:28:52 +0700 Subject: [PATCH 250/389] Add Stellite (XTL) support as cn/xtl. --- src/crypto/CryptoNight_test.h | 15 +++++++++++++++ src/crypto/CryptoNight_x86.h | 11 ++++++----- src/workers/MultiWorker.cpp | 6 +++++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 66630393..bf6013e8 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -84,6 +84,21 @@ const static uint8_t test_output_v1[160] = { }; +// Stellite (XTL) +const static uint8_t test_output_xtl[160] = { + 0x8F, 0xE5, 0xF0, 0x5F, 0x02, 0x2A, 0x61, 0x7D, 0xE5, 0x3F, 0x79, 0x36, 0x4B, 0x25, 0xCB, 0xC3, + 0xC0, 0x8E, 0x0E, 0x1F, 0xE3, 0xBE, 0x48, 0x57, 0x07, 0x03, 0xFE, 0xE1, 0xEC, 0x0E, 0xB0, 0xB1, + 0x21, 0x26, 0xFF, 0x98, 0xE6, 0x86, 0x08, 0x5B, 0xC9, 0x96, 0x44, 0xA3, 0xB8, 0x4E, 0x28, 0x90, + 0x76, 0xED, 0xAD, 0xB9, 0xAA, 0xAC, 0x01, 0x94, 0x1D, 0xBE, 0x3E, 0xEA, 0xAD, 0xEE, 0xB2, 0xCF, + 0xB0, 0x43, 0x4B, 0x88, 0xFC, 0xB2, 0xF3, 0x82, 0x9D, 0xD7, 0xDF, 0x51, 0x97, 0x2C, 0x5A, 0xE3, + 0xC7, 0x16, 0x0B, 0xC8, 0x7C, 0xB7, 0x2F, 0x1C, 0x55, 0x33, 0xCA, 0xE1, 0xEE, 0x08, 0xA4, 0x86, + 0x60, 0xED, 0x6E, 0x9D, 0x2D, 0x05, 0x0D, 0x7D, 0x02, 0x49, 0x23, 0x39, 0x7C, 0xC3, 0x6D, 0x3D, + 0x05, 0x51, 0x28, 0xF1, 0x9B, 0x3C, 0xDF, 0xC4, 0xEA, 0x8A, 0xA6, 0x6A, 0x3C, 0x8B, 0xE2, 0xAF, + 0x47, 0x00, 0xFC, 0x36, 0xED, 0x50, 0xBB, 0xD2, 0x2E, 0x63, 0x4B, 0x93, 0x11, 0x0C, 0xA7, 0xBA, + 0x32, 0x6E, 0x47, 0x4D, 0xCE, 0xCC, 0x82, 0x54, 0x1D, 0x06, 0xF8, 0x06, 0x86, 0xBD, 0x22, 0x48 +}; + + #ifndef XMRIG_NO_AEON const static uint8_t test_output_v0_lite[160] = { 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 75778053..66bcf8b5 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -386,6 +386,7 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } +template static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) { mem_out[0] = EXTRACT64(tmp); @@ -395,7 +396,7 @@ static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) uint8_t x = vh >> 24; static const uint16_t table = 0x7531; - const uint8_t index = (((x >> 3) & 6) | (x & 1)) << 1; + const uint8_t index = (((x >> SHIFT) & 6) | (x & 1)) << 1; vh ^= ((table >> index) & 0x3) << 28; mem_out[1] = vh; @@ -441,7 +442,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } if (VARIANT > 0) { - cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } else { _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } @@ -544,8 +545,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si } if (VARIANT > 0) { - cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); - cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); + cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); } else { _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); @@ -653,7 +654,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si b = _mm_xor_si128(b, c); \ \ if (VARIANT > 0) { \ - cryptonight_monero_tweak(reinterpret_cast(ptr), b); \ + cryptonight_monero_tweak(reinterpret_cast(ptr), b); \ } else { \ _mm_store_si128(ptr, b); \ } diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index d4d5992c..c0f88446 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -58,8 +58,12 @@ bool MultiWorker::selfTest() if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_hash, test_output_v0, sizeof m_hash) == 0) { m_thread->fn(xmrig::VARIANT_1)(test_input, 76, m_hash, m_ctx); + if (memcmp(m_hash, test_output_v1, sizeof m_hash) != 0) { + return false; + } - return memcmp(m_hash, test_output_v1, sizeof m_hash) == 0; + m_thread->fn(xmrig::VARIANT_XTL)(test_input, 76, m_hash, m_ctx); + return memcmp(m_hash, test_output_xtl, sizeof m_hash) == 0; } # ifndef XMRIG_NO_AEON From be232fa1f26b0e73f92146668c42bd3fae8e0172 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 27 Apr 2018 00:40:22 +0700 Subject: [PATCH 251/389] Also support variant received as string. --- src/common/crypto/Algorithm.cpp | 1 + src/common/net/Client.cpp | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index 3123c361..168468f4 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -127,6 +127,7 @@ void xmrig::Algorithm::parseAlgorithm(const char *algo) void xmrig::Algorithm::parseVariant(const char *variant) { if (m_algo == CRYPTONIGHT_HEAVY) { + m_variant = VARIANT_0; return; } diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index c53397a3..921fa618 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -274,7 +274,14 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) } if (params.HasMember("variant")) { - job.algorithm().parseVariant(params["variant"].GetInt()); + const rapidjson::Value &variant = params["variant"]; + + if (variant.IsInt()) { + job.algorithm().parseVariant(variant.GetInt()); + } + else if (variant.IsString()){ + job.algorithm().parseVariant(variant.GetString()); + } } if (!verifyAlgorithm(job.algorithm())) { From 2b0309e1591a4a804b666af5d9d105f509028ad5 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 29 Apr 2018 14:24:44 +0700 Subject: [PATCH 252/389] Sync changes with proxy. --- src/common/crypto/Algorithm.cpp | 34 +++++++++++++++++++++++++++++++++ src/common/crypto/Algorithm.h | 9 +++++++++ src/common/log/Log.h | 2 ++ src/core/Config.cpp | 2 +- 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index 168468f4..f3e9b23d 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -71,6 +71,19 @@ static AlgoData const algorithms[] = { }; +#ifdef XMRIG_PROXY_PROJECT +static AlgoData const xmrStakAlgorithms[] = { + { "cryptonight-monerov7", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, + { "cryptonight_v7", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, + { "cryptonight_v7_stellite", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_XTL }, + { "cryptonight_lite", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_0 }, + { "cryptonight-aeonv7", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, + { "cryptonight_lite_v7", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, + { "cryptonight_lite_v7_xor", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_IPBC }, +}; +#endif + + static const char *variants[] = { "0", "1", @@ -163,6 +176,27 @@ void xmrig::Algorithm::setAlgo(Algo algo) } +#ifdef XMRIG_PROXY_PROJECT +void xmrig::Algorithm::parseXmrStakAlgorithm(const char *algo) +{ + m_algo = INVALID_ALGO; + m_variant = VARIANT_AUTO; + + for (size_t i = 0; i < ARRAY_SIZE(xmrStakAlgorithms); i++) { + if (strcasecmp(algo, xmrStakAlgorithms[i].name) == 0) { + m_algo = xmrStakAlgorithms[i].algo; + m_variant = xmrStakAlgorithms[i].variant; + break; + } + } + + if (m_algo == INVALID_ALGO) { + assert(false); + } +} +#endif + + const char *xmrig::Algorithm::name(bool shortName) const { for (size_t i = 0; i < ARRAY_SIZE(algorithms); i++) { diff --git a/src/common/crypto/Algorithm.h b/src/common/crypto/Algorithm.h index a34d5c07..bcf029d8 100644 --- a/src/common/crypto/Algorithm.h +++ b/src/common/crypto/Algorithm.h @@ -49,6 +49,11 @@ public: setAlgo(algo); } + inline Algorithm(const char *algo) + { + parseAlgorithm(algo); + } + bool isEqual(const Algorithm &other) const { return m_algo == other.m_algo && m_variant == other.m_variant; } inline Algo algo() const { return m_algo; } inline const char *name() const { return name(false); } @@ -66,6 +71,10 @@ public: void parseVariant(int variant); void setAlgo(Algo algo); +# ifdef XMRIG_PROXY_PROJECT + void parseXmrStakAlgorithm(const char *algo); +# endif + private: const char *name(bool shortName) const; diff --git a/src/common/log/Log.h b/src/common/log/Log.h index 0b333272..463c2248 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -80,6 +80,8 @@ private: }; +#define RED_BOLD(x) "\e[1;31m" x "\e[0m" +#define RED(x) "\e[0;31m" x "\e[0m" #define GREEN_BOLD(x) "\e[1;32m" x "\e[0m" #define GREEN(x) "\e[0;32m" x "\e[0m" #define MAGENTA_BOLD(x) "\e[1;35m" x "\e[0m" diff --git a/src/core/Config.cpp b/src/core/Config.cpp index a27dc917..0c4ce779 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -102,7 +102,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const Value pools(kArrayType); - for (const Pool &pool : m_pools) { + for (const Pool &pool : m_activePools) { pools.PushBack(pool.toJSON(doc), allocator); } From 9c2318617234dadf0117f10ae05c55008c53e35d Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 30 Apr 2018 18:17:07 +0700 Subject: [PATCH 253/389] Sync changes with proxy. --- src/common/crypto/Algorithm.cpp | 10 +++++++ src/common/net/Pool.cpp | 47 +++++++++++++-------------------- src/common/net/Pool.h | 4 +++ 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index f3e9b23d..fce4d7b8 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -123,6 +123,11 @@ void xmrig::Algorithm::parseAlgorithm(const char *algo) m_algo = INVALID_ALGO; m_variant = VARIANT_AUTO; + assert(algo != nullptr); + if (algo == nullptr) { + return; + } + for (size_t i = 0; i < ARRAY_SIZE(algorithms); i++) { if ((strcasecmp(algo, algorithms[i].name) == 0) || (strcasecmp(algo, algorithms[i].shortName) == 0)) { m_algo = algorithms[i].algo; @@ -182,6 +187,11 @@ void xmrig::Algorithm::parseXmrStakAlgorithm(const char *algo) m_algo = INVALID_ALGO; m_variant = VARIANT_AUTO; + assert(algo != nullptr); + if (algo == nullptr) { + return; + } + for (size_t i = 0; i < ARRAY_SIZE(xmrStakAlgorithms); i++) { if (strcasecmp(algo, xmrStakAlgorithms[i].name) == 0) { m_algo = xmrStakAlgorithms[i].algo; diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index ab3bdea7..25d99097 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -37,9 +37,6 @@ #endif -#define ADD_VARIANT(variant) m_algorithms.push_back(xmrig::Algorithm(m_algorithm.algo(), variant)); - - #ifdef _MSC_VER # define strncasecmp _strnicmp # define strcasecmp _stricmp @@ -240,32 +237,15 @@ void Pool::adjust(xmrig::Algo algorithm) m_algorithm.setVariant(xmrig::VARIANT_1); } -# ifndef XMRIG_PROXY_PROJECT - switch (m_algorithm.algo()) { - case xmrig::CRYPTONIGHT: - ADD_VARIANT(xmrig::VARIANT_AUTO); - ADD_VARIANT(xmrig::VARIANT_0); - ADD_VARIANT(xmrig::VARIANT_1); - ADD_VARIANT(xmrig::VARIANT_XTL); - break; - - case xmrig::CRYPTONIGHT_LITE: - ADD_VARIANT(xmrig::VARIANT_AUTO); - ADD_VARIANT(xmrig::VARIANT_0); - ADD_VARIANT(xmrig::VARIANT_1); - ADD_VARIANT(xmrig::VARIANT_IPBC); - break; - - case xmrig::CRYPTONIGHT_HEAVY: - ADD_VARIANT(xmrig::VARIANT_0); - break; - - default: - break; - } -# else m_algorithms.push_back(m_algorithm); -# endif + + if (m_algorithm.algo() != xmrig::CRYPTONIGHT_HEAVY) { + addVariant(xmrig::VARIANT_1); + addVariant(xmrig::VARIANT_0); + addVariant(xmrig::VARIANT_XTL); + addVariant(xmrig::VARIANT_IPBC); + addVariant(xmrig::VARIANT_AUTO); + } } @@ -306,3 +286,14 @@ bool Pool::parseIPv6(const char *addr) return true; } + + +void Pool::addVariant(xmrig::Variant variant) +{ + const xmrig::Algorithm algorithm(m_algorithm.algo(), variant); + if (!algorithm.isValid() || m_algorithm == algorithm) { + return; + } + + m_algorithms.push_back(algorithm); +} diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index 5475e10d..ad015bf2 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -85,6 +85,7 @@ public: private: bool parseIPv6(const char *addr); + void addVariant(xmrig::Variant variant); bool m_nicehash; int m_keepAlive; @@ -98,4 +99,7 @@ private: xmrig::c_str m_user; }; + +typedef std::vector Pools; + #endif /* __POOL_H__ */ From 3ca7f3eece5215d99f3cf41e1b8dc081cbb4efde Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 3 May 2018 00:37:01 +0700 Subject: [PATCH 254/389] Sync changes with proxy. --- src/common/net/Pool.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 25d99097..8c0de46a 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -239,6 +239,7 @@ void Pool::adjust(xmrig::Algo algorithm) m_algorithms.push_back(m_algorithm); +# ifndef XMRIG_PROXY_PROJECT if (m_algorithm.algo() != xmrig::CRYPTONIGHT_HEAVY) { addVariant(xmrig::VARIANT_1); addVariant(xmrig::VARIANT_0); @@ -246,6 +247,7 @@ void Pool::adjust(xmrig::Algo algorithm) addVariant(xmrig::VARIANT_IPBC); addVariant(xmrig::VARIANT_AUTO); } +# endif } From b533644f3fcdc9b389e61adc2f26f9e7213ba49a Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 3 May 2018 04:12:53 +0700 Subject: [PATCH 255/389] v2.6.1-dev --- res/app.rc | 4 ++-- src/version.h | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/res/app.rc b/res/app.rc index 800ce2dd..037d842a 100644 --- a/res/app.rc +++ b/res/app.rc @@ -4,8 +4,8 @@ IDI_ICON1 ICON DISCARDABLE "app.ico" VS_VERSION_INFO VERSIONINFO - FILEVERSION APP_VER_MAJOR,APP_VER_MINOR,APP_VER_BUILD,APP_VER_REV - PRODUCTVERSION APP_VER_MAJOR,APP_VER_MINOR,APP_VER_BUILD,APP_VER_REV + FILEVERSION APP_VER_MAJOR,APP_VER_MINOR,APP_VER_PATCH,0 + PRODUCTVERSION APP_VER_MAJOR,APP_VER_MINOR,APP_VER_PATCH,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS VS_FF_DEBUG diff --git a/src/version.h b/src/version.h index c6c42e46..b0e4d2a5 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.6.0-beta3" +#define APP_VERSION "2.6.1-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,8 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 6 -#define APP_VER_BUILD 0 -#define APP_VER_REV 3 +#define APP_VER_PATCH 1 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From 3da58239057f66d56a2cda8088bd843acfd11533 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 3 May 2018 04:15:36 +0700 Subject: [PATCH 256/389] Remove obsolete tests. --- test/CMakeLists.txt | 9 - test/autoconf/CMakeLists.txt | 16 - test/autoconf/autoconf.c | 152 --- test/cryptonight/CMakeLists.txt | 28 - test/cryptonight/cryptonight.c | 136 -- test/cryptonight_lite/CMakeLists.txt | 27 - test/cryptonight_lite/cryptonight_lite.c | 124 -- test/unity/CMakeLists.txt | 2 - test/unity/unity.c | 1446 ---------------------- test/unity/unity.h | 293 ----- test/unity/unity_internals.h | 749 ----------- 11 files changed, 2982 deletions(-) delete mode 100644 test/CMakeLists.txt delete mode 100644 test/autoconf/CMakeLists.txt delete mode 100644 test/autoconf/autoconf.c delete mode 100644 test/cryptonight/CMakeLists.txt delete mode 100644 test/cryptonight/cryptonight.c delete mode 100644 test/cryptonight_lite/CMakeLists.txt delete mode 100644 test/cryptonight_lite/cryptonight_lite.c delete mode 100644 test/unity/CMakeLists.txt delete mode 100644 test/unity/unity.c delete mode 100644 test/unity/unity.h delete mode 100644 test/unity/unity_internals.h diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index b30cfb0b..00000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -project("xmrig-test" C) -cmake_minimum_required(VERSION 3.0) - -include(CTest) - -add_subdirectory(unity) -add_subdirectory(cryptonight) -add_subdirectory(cryptonight_lite) -add_subdirectory(autoconf) \ No newline at end of file diff --git a/test/autoconf/CMakeLists.txt b/test/autoconf/CMakeLists.txt deleted file mode 100644 index b956a9de..00000000 --- a/test/autoconf/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -set(SOURCES - autoconf.c - ../../cpu.h - ../../cpu.c - ) - -add_executable(autoconf_app ${SOURCES}) -target_link_libraries(autoconf_app unity) - -include_directories(../..) - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing") -set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") -add_definitions(-DBUILD_TEST) - -add_test(autoconf_test autoconf_app) diff --git a/test/autoconf/autoconf.c b/test/autoconf/autoconf.c deleted file mode 100644 index 86ced5f0..00000000 --- a/test/autoconf/autoconf.c +++ /dev/null @@ -1,152 +0,0 @@ -#include - -#include "cpu.h" -#include "options.h" - -struct cpu_info cpu_info = { 0 }; - - -static void set_cpu_info(int total_logical_cpus, int l2_cache, int l3_cache) { - cpu_info.total_cores = total_logical_cpus; - cpu_info.total_logical_cpus = total_logical_cpus; - cpu_info.l2_cache = l2_cache; - cpu_info.l3_cache = l3_cache; -} - - -void test_autoconf_should_GetOptimalThreadsCounti7(void) { - set_cpu_info(8, 1024, 8192); // 4C/8T 8 MB (Generic i7 CPU) - - TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); - TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); - - TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); - TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); - - TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); - TEST_ASSERT_EQUAL_INT(5, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 60)); - TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 50)); - TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 35)); - TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 20)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 5)); -} - - -void test_autoconf_should_GetOptimalThreadsCounti5(void) { - set_cpu_info(4, 1024, 6144); // 2C/4T 6 MB (Generic i5 CPU) - - TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); - - TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); - - TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); - TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); - - TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); - TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); -} - - -void test_autoconf_should_GetOptimalThreadsCounti3(void) { - set_cpu_info(4, 512, 3072); // 2C/4T 3 MB (Generic i3 CPU) - - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); - - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); - - TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); - - TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); -} - - -void test_autoconf_should_GetOptimalThreadsCountR7(void) { - set_cpu_info(16, 4096, 16384); // 8C/16T 16 MB (AMD Ryzen 7) - - TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); - TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); - - TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); - TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); - - TEST_ASSERT_EQUAL_INT(16, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); - TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); - - TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); - TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); -} - - -void test_autoconf_should_GetOptimalThreadsCountTwoE5620(void) { - set_cpu_info(16, 2048, 24576); // 8C/16T 24 MB (Two E5620) - - TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); - TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); - - TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); - TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); - - TEST_ASSERT_EQUAL_INT(16, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); - TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); - - TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); - TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); -} - - -void test_autoconf_should_GetOptimalThreadsCountVCPU(void) { - set_cpu_info(1, 1024, 15360); // 1C/1T 15 MB (Single core Virtual CPU) - - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); - - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); - - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); - - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); -} - - -void test_autoconf_should_GetOptimalThreadsCountNoL3(void) { - set_cpu_info(8, 8192, 0); // 4C/8T (Multi core Virtual CPU without L3 cache) - - TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); - TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); - - TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); - TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); - - TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); - TEST_ASSERT_EQUAL_INT(5, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 60)); - TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 50)); - TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 35)); - TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 20)); - TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 5)); -} - - -int main(void) -{ - UNITY_BEGIN(); - - RUN_TEST(test_autoconf_should_GetOptimalThreadsCounti7); - RUN_TEST(test_autoconf_should_GetOptimalThreadsCounti5); - RUN_TEST(test_autoconf_should_GetOptimalThreadsCounti3); - RUN_TEST(test_autoconf_should_GetOptimalThreadsCountR7); - RUN_TEST(test_autoconf_should_GetOptimalThreadsCountR7); - RUN_TEST(test_autoconf_should_GetOptimalThreadsCountTwoE5620); - RUN_TEST(test_autoconf_should_GetOptimalThreadsCountVCPU); - RUN_TEST(test_autoconf_should_GetOptimalThreadsCountNoL3); - - return UNITY_END(); -} diff --git a/test/cryptonight/CMakeLists.txt b/test/cryptonight/CMakeLists.txt deleted file mode 100644 index 4ebbbcc9..00000000 --- a/test/cryptonight/CMakeLists.txt +++ /dev/null @@ -1,28 +0,0 @@ -set(SOURCES - cryptonight.c - ../../options.h - ../../algo/cryptonight/cryptonight.h - ../../algo/cryptonight/cryptonight.c - ../../algo/cryptonight/cryptonight_av1_aesni.c - ../../algo/cryptonight/cryptonight_av2_aesni_double.c - ../../algo/cryptonight/cryptonight_av3_softaes.c - ../../algo/cryptonight/cryptonight_av4_softaes_double.c - ../../crypto/c_keccak.c - ../../crypto/c_blake256.c - ../../crypto/c_groestl.c - ../../crypto/c_jh.c - ../../crypto/c_skein.c - ../../crypto/soft_aes.c - ) - -add_executable(cryptonight_app ${SOURCES}) -target_link_libraries(cryptonight_app unity) - -include_directories(../..) - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -fno-strict-aliasing") -set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") -add_definitions(-DBUILD_TEST) -add_definitions(-DXMRIG_NO_AEON) - -add_test(cryptonight_test cryptonight_app) diff --git a/test/cryptonight/cryptonight.c b/test/cryptonight/cryptonight.c deleted file mode 100644 index bcc0db30..00000000 --- a/test/cryptonight/cryptonight.c +++ /dev/null @@ -1,136 +0,0 @@ -#include -#include -#include -#include -#include - -#include "options.h" -#include "algo/cryptonight/cryptonight.h" - -bool opt_double_hash = false; - -const static char input1[152] = { - 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, - 0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, - 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, - 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, - 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01, - 0x01, 0x00, 0xFB, 0x8E, 0x8A, 0xC8, 0x05, 0x89, 0x93, 0x23, 0x37, 0x1B, 0xB7, 0x90, 0xDB, 0x19, - 0x21, 0x8A, 0xFD, 0x8D, 0xB8, 0xE3, 0x75, 0x5D, 0x8B, 0x90, 0xF3, 0x9B, 0x3D, 0x55, 0x06, 0xA9, - 0xAB, 0xCE, 0x4F, 0xA9, 0x12, 0x24, 0x45, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x81, 0x46, 0xD4, 0x9F, - 0xA9, 0x3E, 0xE7, 0x24, 0xDE, 0xB5, 0x7D, 0x12, 0xCB, 0xC6, 0xC6, 0xF3, 0xB9, 0x24, 0xD9, 0x46, - 0x12, 0x7C, 0x7A, 0x97, 0x41, 0x8F, 0x93, 0x48, 0x82, 0x8F, 0x0F, 0x02, -}; - -const static char input2[] = "This is a test"; -const static char input3[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pellentesque metus."; - - -void cryptonight_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); - - -static char hash[64]; -#define RESULT1 "1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100" -#define RESULT1_DOUBLE "1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd0801001b606a3f4a07d6489a1bcd07697bd16696b61c8ae982f61a90160f4e52828a7f" -#define RESULT2 "a084f01d1437a09c6985401b60d43554ae105802c5f5d8a9b3253649c0be6605" -#define RESULT3 "0bbe54bd26caa92a1d436eec71cbef02560062fa689fe14d7efcf42566b411cf" - - -static char *bin2hex(const unsigned char *p, size_t len) -{ - char *s = malloc((len * 2) + 1); - if (!s) { - return NULL; - } - - for (int i = 0; i < len; i++) { - sprintf(s + (i * 2), "%02x", (unsigned int) p[i]); - } - - return s; -} - - -static void * create_ctx(int ratio) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); - ctx->memory = (uint8_t *) _mm_malloc(MEMORY * ratio, 16); - - return ctx; -} - - -static void free_ctx(struct cryptonight_ctx *ctx) { - _mm_free(ctx->memory); - _mm_free(ctx); -} - - -void test_cryptonight_av1_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(1); - - cryptonight_av1_aesni(input1, 76, &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - - cryptonight_av1_aesni(input2, strlen(input2), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); - - cryptonight_av1_aesni(input3, strlen(input3), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); - - free_ctx(ctx); -} - - -void test_cryptonight_av2_should_CalcHash(void) -{ - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(2); - - cryptonight_av2_aesni_double(input1, 76, &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1_DOUBLE, bin2hex(hash, 64)); - - free_ctx(ctx); -} - - -void test_cryptonight_av3_should_CalcHash(void) -{ - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(1); - - cryptonight_av3_softaes(input1, 76, &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - - cryptonight_av3_softaes(input2, strlen(input2), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); - - cryptonight_av3_softaes(input3, strlen(input3), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); - - free_ctx(ctx); -} - - -void test_cryptonight_av4_should_CalcHash(void) -{ - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(2); - - cryptonight_av4_softaes_double(input1, 76, &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1_DOUBLE, bin2hex(hash, 64)); - - free_ctx(ctx); -} - - -int main(void) -{ - UNITY_BEGIN(); - - RUN_TEST(test_cryptonight_av1_should_CalcHash); - RUN_TEST(test_cryptonight_av2_should_CalcHash); - RUN_TEST(test_cryptonight_av3_should_CalcHash); - RUN_TEST(test_cryptonight_av4_should_CalcHash); - - return UNITY_END(); -} diff --git a/test/cryptonight_lite/CMakeLists.txt b/test/cryptonight_lite/CMakeLists.txt deleted file mode 100644 index 9a83ecbc..00000000 --- a/test/cryptonight_lite/CMakeLists.txt +++ /dev/null @@ -1,27 +0,0 @@ -set(SOURCES - cryptonight_lite.c - ../../options.h - ../../algo/cryptonight/cryptonight.h - ../../algo/cryptonight/cryptonight.c - ../../algo/cryptonight-lite/cryptonight_lite_av1_aesni.c - ../../algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c - ../../algo/cryptonight-lite/cryptonight_lite_av3_softaes.c - ../../algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c - ../../crypto/c_keccak.c - ../../crypto/c_blake256.c - ../../crypto/c_groestl.c - ../../crypto/c_jh.c - ../../crypto/c_skein.c - ../../crypto/soft_aes.c - ) - -add_executable(cryptonight_lite_app ${SOURCES}) -target_link_libraries(cryptonight_lite_app unity) - -include_directories(../..) - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -fno-strict-aliasing") -set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") -add_definitions(-DBUILD_TEST) - -add_test(cryptonight_lite_test cryptonight_lite_app) diff --git a/test/cryptonight_lite/cryptonight_lite.c b/test/cryptonight_lite/cryptonight_lite.c deleted file mode 100644 index a6d5b554..00000000 --- a/test/cryptonight_lite/cryptonight_lite.c +++ /dev/null @@ -1,124 +0,0 @@ -#include -#include -#include -#include -#include - -#include "options.h" -#include "algo/cryptonight/cryptonight.h" - -bool opt_double_hash = false; -enum mining_algo opt_algo = ALGO_CRYPTONIGHT_LITE; - -const static char input1[152] = { - 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, - 0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, - 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, - 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, - 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01, - 0x01, 0x00, 0xFB, 0x8E, 0x8A, 0xC8, 0x05, 0x89, 0x93, 0x23, 0x37, 0x1B, 0xB7, 0x90, 0xDB, 0x19, - 0x21, 0x8A, 0xFD, 0x8D, 0xB8, 0xE3, 0x75, 0x5D, 0x8B, 0x90, 0xF3, 0x9B, 0x3D, 0x55, 0x06, 0xA9, - 0xAB, 0xCE, 0x4F, 0xA9, 0x12, 0x24, 0x45, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x81, 0x46, 0xD4, 0x9F, - 0xA9, 0x3E, 0xE7, 0x24, 0xDE, 0xB5, 0x7D, 0x12, 0xCB, 0xC6, 0xC6, 0xF3, 0xB9, 0x24, 0xD9, 0x46, - 0x12, 0x7C, 0x7A, 0x97, 0x41, 0x8F, 0x93, 0x48, 0x82, 0x8F, 0x0F, 0x02, -}; - - -void cryptonight_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) {} -void cryptonight_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) {} -void cryptonight_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) {} -void cryptonight_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) {} - -void cryptonight_lite_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_lite_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_lite_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_lite_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); - - -static char hash[64]; -#define RESULT1 "3695b4b53bb00358b0ad38dc160feb9e004eece09b83a72ef6ba9864d3510c88" -#define RESULT1_DOUBLE "3695b4b53bb00358b0ad38dc160feb9e004eece09b83a72ef6ba9864d3510c8828a22bad3f93d1408fca472eb5ad1cbe75f21d053c8ce5b3af105a57713e21dd" - - -static char *bin2hex(const unsigned char *p, size_t len) -{ - char *s = malloc((len * 2) + 1); - if (!s) { - return NULL; - } - - for (int i = 0; i < len; i++) { - sprintf(s + (i * 2), "%02x", (unsigned int) p[i]); - } - - return s; -} - - -static void * create_ctx(int ratio) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); - ctx->memory = (uint8_t *) _mm_malloc(MEMORY_LITE * ratio, 16); - - return ctx; -} - - -static void free_ctx(struct cryptonight_ctx *ctx) { - _mm_free(ctx->memory); - _mm_free(ctx); -} - - -void test_cryptonight_lite_av1_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(1); - - cryptonight_lite_av1_aesni(input1, 76, &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - - free_ctx(ctx); -} - - -void test_cryptonight_lite_av2_should_CalcHash(void) -{ - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(2); - - cryptonight_lite_av2_aesni_double(input1, 76, &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1_DOUBLE, bin2hex(hash, 64)); - - free_ctx(ctx); -} - - -void test_cryptonight_lite_av3_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(1); - - cryptonight_lite_av3_softaes(input1, 76, &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - - free_ctx(ctx); -} - - -void test_cryptonight_lite_av4_should_CalcHash(void) -{ - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(2); - - cryptonight_lite_av4_softaes_double(input1, 76, &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1_DOUBLE, bin2hex(hash, 64)); - - free_ctx(ctx); -} - - -int main(void) -{ - UNITY_BEGIN(); - - RUN_TEST(test_cryptonight_lite_av1_should_CalcHash); - RUN_TEST(test_cryptonight_lite_av2_should_CalcHash); - RUN_TEST(test_cryptonight_lite_av3_should_CalcHash); - RUN_TEST(test_cryptonight_lite_av4_should_CalcHash); - - return UNITY_END(); -} diff --git a/test/unity/CMakeLists.txt b/test/unity/CMakeLists.txt deleted file mode 100644 index 6e350179..00000000 --- a/test/unity/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_library(unity STATIC unity.c) -target_include_directories(unity PUBLIC .) diff --git a/test/unity/unity.c b/test/unity/unity.c deleted file mode 100644 index a43e33e1..00000000 --- a/test/unity/unity.c +++ /dev/null @@ -1,1446 +0,0 @@ -/* ========================================================================= - Unity Project - A Test Framework for C - Copyright (c) 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams - [Released under MIT License. Please refer to license.txt for details] -============================================================================ */ - -#include "unity.h" -#include - -/* If omitted from header, declare overrideable prototypes here so they're ready for use */ -#ifdef UNITY_OMIT_OUTPUT_CHAR_HEADER_DECLARATION -void UNITY_OUTPUT_CHAR(int); -#endif - -/* Helpful macros for us to use here */ -#define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; longjmp(Unity.AbortFrame, 1); } -#define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; longjmp(Unity.AbortFrame, 1); } - -/* return prematurely if we are already in failure or ignore state */ -#define UNITY_SKIP_EXECUTION { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} } - -struct _Unity Unity; - -static const char UnityStrOk[] = "OK"; -static const char UnityStrPass[] = "PASS"; -static const char UnityStrFail[] = "FAIL"; -static const char UnityStrIgnore[] = "IGNORE"; -static const char UnityStrNull[] = "NULL"; -static const char UnityStrSpacer[] = ". "; -static const char UnityStrExpected[] = " Expected "; -static const char UnityStrWas[] = " Was "; -static const char UnityStrElement[] = " Element "; -static const char UnityStrByte[] = " Byte "; -static const char UnityStrMemory[] = " Memory Mismatch."; -static const char UnityStrDelta[] = " Values Not Within Delta "; -static const char UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless."; -static const char UnityStrNullPointerForExpected[] = " Expected pointer to be NULL"; -static const char UnityStrNullPointerForActual[] = " Actual pointer was NULL"; -#ifndef UNITY_EXCLUDE_FLOAT -static const char UnityStrNot[] = "Not "; -static const char UnityStrInf[] = "Infinity"; -static const char UnityStrNegInf[] = "Negative Infinity"; -static const char UnityStrNaN[] = "NaN"; -static const char UnityStrDet[] = "Determinate"; -static const char UnityStrInvalidFloatTrait[] = "Invalid Float Trait"; -#endif -const char UnityStrErrFloat[] = "Unity Floating Point Disabled"; -const char UnityStrErrDouble[] = "Unity Double Precision Disabled"; -const char UnityStrErr64[] = "Unity 64-bit Support Disabled"; -static const char UnityStrBreaker[] = "-----------------------"; -static const char UnityStrResultsTests[] = " Tests "; -static const char UnityStrResultsFailures[] = " Failures "; -static const char UnityStrResultsIgnored[] = " Ignored "; -static const char UnityStrDetail1Name[] = UNITY_DETAIL1_NAME " "; -static const char UnityStrDetail2Name[] = " " UNITY_DETAIL2_NAME " "; - -/* compiler-generic print formatting masks */ -static const _U_UINT UnitySizeMask[] = -{ - 255u, /* 0xFF */ - 65535u, /* 0xFFFF */ - 65535u, - 4294967295u, /* 0xFFFFFFFF */ - 4294967295u, - 4294967295u, - 4294967295u -#ifdef UNITY_SUPPORT_64 - ,0xFFFFFFFFFFFFFFFF -#endif -}; - -/*----------------------------------------------- - * Pretty Printers & Test Result Output Handlers - *-----------------------------------------------*/ - -void UnityPrint(const char* string) -{ - const char* pch = string; - - if (pch != NULL) - { - while (*pch) - { - /* printable characters plus CR & LF are printed */ - if ((*pch <= 126) && (*pch >= 32)) - { - UNITY_OUTPUT_CHAR(*pch); - } - /* write escaped carriage returns */ - else if (*pch == 13) - { - UNITY_OUTPUT_CHAR('\\'); - UNITY_OUTPUT_CHAR('r'); - } - /* write escaped line feeds */ - else if (*pch == 10) - { - UNITY_OUTPUT_CHAR('\\'); - UNITY_OUTPUT_CHAR('n'); - } - /* unprintable characters are shown as codes */ - else - { - UNITY_OUTPUT_CHAR('\\'); - UNITY_OUTPUT_CHAR('x'); - UnityPrintNumberHex((_U_UINT)*pch, 2); - } - pch++; - } - } -} - -void UnityPrintLen(const char* string, const _UU32 length); -void UnityPrintLen(const char* string, const _UU32 length) -{ - const char* pch = string; - - if (pch != NULL) - { - while (*pch && (_UU32)(pch - string) < length) - { - /* printable characters plus CR & LF are printed */ - if ((*pch <= 126) && (*pch >= 32)) - { - UNITY_OUTPUT_CHAR(*pch); - } - /* write escaped carriage returns */ - else if (*pch == 13) - { - UNITY_OUTPUT_CHAR('\\'); - UNITY_OUTPUT_CHAR('r'); - } - /* write escaped line feeds */ - else if (*pch == 10) - { - UNITY_OUTPUT_CHAR('\\'); - UNITY_OUTPUT_CHAR('n'); - } - /* unprintable characters are shown as codes */ - else - { - UNITY_OUTPUT_CHAR('\\'); - UNITY_OUTPUT_CHAR('x'); - UnityPrintNumberHex((_U_UINT)*pch, 2); - } - pch++; - } - } -} - -/*-----------------------------------------------*/ -void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style) -{ - if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) - { - UnityPrintNumber(number); - } - else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT) - { - UnityPrintNumberUnsigned( (_U_UINT)number & UnitySizeMask[((_U_UINT)style & (_U_UINT)0x0F) - 1] ); - } - else - { - UNITY_OUTPUT_CHAR('0'); - UNITY_OUTPUT_CHAR('x'); - UnityPrintNumberHex((_U_UINT)number, (char)((style & 0x000F) << 1)); - } -} - -/*-----------------------------------------------*/ -void UnityPrintNumber(const _U_SINT number_to_print) -{ - _U_UINT number = (_U_UINT)number_to_print; - - if (number_to_print < 0) - { - /* A negative number, including MIN negative */ - UNITY_OUTPUT_CHAR('-'); - number = (_U_UINT)(-number_to_print); - } - UnityPrintNumberUnsigned(number); -} - -/*----------------------------------------------- - * basically do an itoa using as little ram as possible */ -void UnityPrintNumberUnsigned(const _U_UINT number) -{ - _U_UINT divisor = 1; - - /* figure out initial divisor */ - while (number / divisor > 9) - { - divisor *= 10; - } - - /* now mod and print, then divide divisor */ - do - { - UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10))); - divisor /= 10; - } - while (divisor > 0); -} - -/*-----------------------------------------------*/ -void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print) -{ - _U_UINT nibble; - char nibbles = nibbles_to_print; - - while (nibbles > 0) - { - nibble = (number >> (--nibbles << 2)) & 0x0000000F; - if (nibble <= 9) - { - UNITY_OUTPUT_CHAR((char)('0' + nibble)); - } - else - { - UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble)); - } - } -} - -/*-----------------------------------------------*/ -void UnityPrintMask(const _U_UINT mask, const _U_UINT number) -{ - _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1); - _US32 i; - - for (i = 0; i < UNITY_INT_WIDTH; i++) - { - if (current_bit & mask) - { - if (current_bit & number) - { - UNITY_OUTPUT_CHAR('1'); - } - else - { - UNITY_OUTPUT_CHAR('0'); - } - } - else - { - UNITY_OUTPUT_CHAR('X'); - } - current_bit = current_bit >> 1; - } -} - -/*-----------------------------------------------*/ -#ifdef UNITY_FLOAT_VERBOSE -#include - -#ifndef UNITY_VERBOSE_NUMBER_MAX_LENGTH -# ifdef UNITY_DOUBLE_VERBOSE -# define UNITY_VERBOSE_NUMBER_MAX_LENGTH 317 -# else -# define UNITY_VERBOSE_NUMBER_MAX_LENGTH 47 -# endif -#endif - -void UnityPrintFloat(_UD number) -{ - char TempBuffer[UNITY_VERBOSE_NUMBER_MAX_LENGTH + 1]; - snprintf(TempBuffer, sizeof(TempBuffer), "%.6f", number); - UnityPrint(TempBuffer); -} -#endif - -/*-----------------------------------------------*/ - -void UnityPrintFail(void); -void UnityPrintFail(void) -{ - UnityPrint(UnityStrFail); -} - -void UnityPrintOk(void); -void UnityPrintOk(void) -{ - UnityPrint(UnityStrOk); -} - -/*-----------------------------------------------*/ -static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line); -static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line) -{ -#ifndef UNITY_FIXTURES - UnityPrint(file); - UNITY_OUTPUT_CHAR(':'); - UnityPrintNumber((_U_SINT)line); - UNITY_OUTPUT_CHAR(':'); - UnityPrint(Unity.CurrentTestName); - UNITY_OUTPUT_CHAR(':'); -#else - UNITY_UNUSED(file); - UNITY_UNUSED(line); -#endif -} - -/*-----------------------------------------------*/ -static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line); -static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line) -{ -#ifndef UNITY_FIXTURES - UnityTestResultsBegin(Unity.TestFile, line); -#else - UNITY_UNUSED(line); -#endif - UnityPrint(UnityStrFail); - UNITY_OUTPUT_CHAR(':'); -} - -/*-----------------------------------------------*/ -void UnityConcludeTest(void) -{ - if (Unity.CurrentTestIgnored) - { - Unity.TestIgnores++; - } - else if (!Unity.CurrentTestFailed) - { - UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber); - UnityPrint(UnityStrPass); - } - else - { - Unity.TestFailures++; - } - - Unity.CurrentTestFailed = 0; - Unity.CurrentTestIgnored = 0; - UNITY_PRINT_EOL(); - UNITY_FLUSH_CALL(); -} - -/*-----------------------------------------------*/ -static void UnityAddMsgIfSpecified(const char* msg); -static void UnityAddMsgIfSpecified(const char* msg) -{ - if (msg) - { - UnityPrint(UnityStrSpacer); -#ifndef UNITY_EXCLUDE_DETAILS - if (Unity.CurrentDetail1) - { - UnityPrint(UnityStrDetail1Name); - UnityPrint(Unity.CurrentDetail1); - if (Unity.CurrentDetail2) - { - UnityPrint(UnityStrDetail2Name); - UnityPrint(Unity.CurrentDetail2); - } - UnityPrint(UnityStrSpacer); - } -#endif - UnityPrint(msg); - } -} - -/*-----------------------------------------------*/ -static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual); -static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual) -{ - UnityPrint(UnityStrExpected); - if (expected != NULL) - { - UNITY_OUTPUT_CHAR('\''); - UnityPrint(expected); - UNITY_OUTPUT_CHAR('\''); - } - else - { - UnityPrint(UnityStrNull); - } - UnityPrint(UnityStrWas); - if (actual != NULL) - { - UNITY_OUTPUT_CHAR('\''); - UnityPrint(actual); - UNITY_OUTPUT_CHAR('\''); - } - else - { - UnityPrint(UnityStrNull); - } -} - -/*-----------------------------------------------*/ -static void UnityPrintExpectedAndActualStringsLen(const char* expected, const char* actual, const _UU32 length) -{ - UnityPrint(UnityStrExpected); - if (expected != NULL) - { - UNITY_OUTPUT_CHAR('\''); - UnityPrintLen(expected, length); - UNITY_OUTPUT_CHAR('\''); - } - else - { - UnityPrint(UnityStrNull); - } - UnityPrint(UnityStrWas); - if (actual != NULL) - { - UNITY_OUTPUT_CHAR('\''); - UnityPrintLen(actual, length); - UNITY_OUTPUT_CHAR('\''); - } - else - { - UnityPrint(UnityStrNull); - } -} - - - -/*----------------------------------------------- - * Assertion & Control Helpers - *-----------------------------------------------*/ - -static int UnityCheckArraysForNull(UNITY_INTERNAL_PTR expected, UNITY_INTERNAL_PTR actual, const UNITY_LINE_TYPE lineNumber, const char* msg) -{ - /* return true if they are both NULL */ - if ((expected == NULL) && (actual == NULL)) - return 1; - - /* throw error if just expected is NULL */ - if (expected == NULL) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrNullPointerForExpected); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } - - /* throw error if just actual is NULL */ - if (actual == NULL) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrNullPointerForActual); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } - - /* return false if neither is NULL */ - return 0; -} - -/*----------------------------------------------- - * Assertion Functions - *-----------------------------------------------*/ - -void UnityAssertBits(const _U_SINT mask, - const _U_SINT expected, - const _U_SINT actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber) -{ - UNITY_SKIP_EXECUTION; - - if ((mask & expected) != (mask & actual)) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrExpected); - UnityPrintMask((_U_UINT)mask, (_U_UINT)expected); - UnityPrint(UnityStrWas); - UnityPrintMask((_U_UINT)mask, (_U_UINT)actual); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } -} - -/*-----------------------------------------------*/ -void UnityAssertEqualNumber(const _U_SINT expected, - const _U_SINT actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber, - const UNITY_DISPLAY_STYLE_T style) -{ - UNITY_SKIP_EXECUTION; - - if (expected != actual) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrExpected); - UnityPrintNumberByStyle(expected, style); - UnityPrint(UnityStrWas); - UnityPrintNumberByStyle(actual, style); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } -} - -#define UnityPrintPointlessAndBail() \ -{ \ - UnityTestResultsFailBegin(lineNumber); \ - UnityPrint(UnityStrPointless); \ - UnityAddMsgIfSpecified(msg); \ - UNITY_FAIL_AND_BAIL; } - -/*-----------------------------------------------*/ -void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, - UNITY_INTERNAL_PTR actual, - const _UU32 num_elements, - const char* msg, - const UNITY_LINE_TYPE lineNumber, - const UNITY_DISPLAY_STYLE_T style) -{ - _UU32 elements = num_elements; - UNITY_INTERNAL_PTR ptr_exp = (UNITY_INTERNAL_PTR)expected; - UNITY_INTERNAL_PTR ptr_act = (UNITY_INTERNAL_PTR)actual; - - UNITY_SKIP_EXECUTION; - - if (elements == 0) - { - UnityPrintPointlessAndBail(); - } - - if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) - return; - - /* If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case - * as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific - * variants do not. Therefore remove this flag. */ - switch(style & (UNITY_DISPLAY_STYLE_T)(~UNITY_DISPLAY_RANGE_AUTO)) - { - case UNITY_DISPLAY_STYLE_HEX8: - case UNITY_DISPLAY_STYLE_INT8: - case UNITY_DISPLAY_STYLE_UINT8: - while (elements--) - { - if (*(UNITY_PTR_ATTRIBUTE const _US8*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US8*)ptr_act) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrElement); - UnityPrintNumberUnsigned(num_elements - elements - 1); - UnityPrint(UnityStrExpected); - UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US8*)ptr_exp, style); - UnityPrint(UnityStrWas); - UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US8*)ptr_act, style); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } - ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 1); - ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 1); - } - break; - case UNITY_DISPLAY_STYLE_HEX16: - case UNITY_DISPLAY_STYLE_INT16: - case UNITY_DISPLAY_STYLE_UINT16: - while (elements--) - { - if (*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US16*)ptr_act) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrElement); - UnityPrintNumberUnsigned(num_elements - elements - 1); - UnityPrint(UnityStrExpected); - UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_exp, style); - UnityPrint(UnityStrWas); - UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_act, style); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } - ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 2); - ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 2); - } - break; -#ifdef UNITY_SUPPORT_64 - case UNITY_DISPLAY_STYLE_HEX64: - case UNITY_DISPLAY_STYLE_INT64: - case UNITY_DISPLAY_STYLE_UINT64: - while (elements--) - { - if (*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US64*)ptr_act) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrElement); - UnityPrintNumberUnsigned(num_elements - elements - 1); - UnityPrint(UnityStrExpected); - UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_exp, style); - UnityPrint(UnityStrWas); - UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_act, style); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } - ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 8); - ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 8); - } - break; -#endif - default: - while (elements--) - { - if (*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US32*)ptr_act) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrElement); - UnityPrintNumberUnsigned(num_elements - elements - 1); - UnityPrint(UnityStrExpected); - UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_exp, style); - UnityPrint(UnityStrWas); - UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_act, style); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } - ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 4); - ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 4); - } - break; - } -} - -/*-----------------------------------------------*/ -/* Wrap this define in a function with variable types as float or double */ -#define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \ - if (isinf(expected) && isinf(actual) && (isneg(expected) == isneg(actual))) return 1; \ - if (isnan(expected) && isnan(actual)) return 1; \ - diff = actual - expected; \ - if (diff < 0.0f) diff = 0.0f - diff; \ - if (delta < 0.0f) delta = 0.0f - delta; \ - return !(isnan(diff) || isinf(diff) || (delta < diff)); - /* This first part of this condition will catch any NaN or Infinite values */ - -#ifndef UNITY_EXCLUDE_FLOAT -static int UnityFloatsWithin(_UF delta, _UF expected, _UF actual) -{ - _UF diff; - UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff); -} - -void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected, - UNITY_PTR_ATTRIBUTE const _UF* actual, - const _UU32 num_elements, - const char* msg, - const UNITY_LINE_TYPE lineNumber) -{ - _UU32 elements = num_elements; - UNITY_PTR_ATTRIBUTE const _UF* ptr_expected = expected; - UNITY_PTR_ATTRIBUTE const _UF* ptr_actual = actual; - - UNITY_SKIP_EXECUTION; - - if (elements == 0) - { - UnityPrintPointlessAndBail(); - } - - if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) - return; - - while (elements--) - { - if (!UnityFloatsWithin(*ptr_expected * UNITY_FLOAT_PRECISION, *ptr_expected, *ptr_actual)) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrElement); - UnityPrintNumberUnsigned(num_elements - elements - 1); -#ifdef UNITY_FLOAT_VERBOSE - UnityPrint(UnityStrExpected); - UnityPrintFloat(*ptr_expected); - UnityPrint(UnityStrWas); - UnityPrintFloat(*ptr_actual); -#else - UnityPrint(UnityStrDelta); -#endif - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } - ptr_expected++; - ptr_actual++; - } -} - -/*-----------------------------------------------*/ -void UnityAssertFloatsWithin(const _UF delta, - const _UF expected, - const _UF actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber) -{ - UNITY_SKIP_EXECUTION; - - - if (!UnityFloatsWithin(delta, expected, actual)) - { - UnityTestResultsFailBegin(lineNumber); -#ifdef UNITY_FLOAT_VERBOSE - UnityPrint(UnityStrExpected); - UnityPrintFloat(expected); - UnityPrint(UnityStrWas); - UnityPrintFloat(actual); -#else - UnityPrint(UnityStrDelta); -#endif - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } -} - -/*-----------------------------------------------*/ -void UnityAssertFloatSpecial(const _UF actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber, - const UNITY_FLOAT_TRAIT_T style) -{ - const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet }; - _U_SINT should_be_trait = ((_U_SINT)style & 1); - _U_SINT is_trait = !should_be_trait; - _U_SINT trait_index = (_U_SINT)(style >> 1); - - UNITY_SKIP_EXECUTION; - - switch(style) - { - case UNITY_FLOAT_IS_INF: - case UNITY_FLOAT_IS_NOT_INF: - is_trait = isinf(actual) & ispos(actual); - break; - case UNITY_FLOAT_IS_NEG_INF: - case UNITY_FLOAT_IS_NOT_NEG_INF: - is_trait = isinf(actual) & isneg(actual); - break; - - case UNITY_FLOAT_IS_NAN: - case UNITY_FLOAT_IS_NOT_NAN: - is_trait = isnan(actual); - break; - - /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */ - case UNITY_FLOAT_IS_DET: - case UNITY_FLOAT_IS_NOT_DET: - if (isinf(actual) | isnan(actual)) - is_trait = 0; - else - is_trait = 1; - break; - - default: - trait_index = 0; - trait_names[0] = UnityStrInvalidFloatTrait; - break; - } - - if (is_trait != should_be_trait) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrExpected); - if (!should_be_trait) - UnityPrint(UnityStrNot); - UnityPrint(trait_names[trait_index]); - UnityPrint(UnityStrWas); -#ifdef UNITY_FLOAT_VERBOSE - UnityPrintFloat(actual); -#else - if (should_be_trait) - UnityPrint(UnityStrNot); - UnityPrint(trait_names[trait_index]); -#endif - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } -} - -#endif /* not UNITY_EXCLUDE_FLOAT */ - -/*-----------------------------------------------*/ -#ifndef UNITY_EXCLUDE_DOUBLE -static int UnityDoublesWithin(_UD delta, _UD expected, _UD actual) -{ - _UD diff; - UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff); -} - -void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected, - UNITY_PTR_ATTRIBUTE const _UD* actual, - const _UU32 num_elements, - const char* msg, - const UNITY_LINE_TYPE lineNumber) -{ - _UU32 elements = num_elements; - UNITY_PTR_ATTRIBUTE const _UD* ptr_expected = expected; - UNITY_PTR_ATTRIBUTE const _UD* ptr_actual = actual; - - UNITY_SKIP_EXECUTION; - - if (elements == 0) - { - UnityPrintPointlessAndBail(); - } - - if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) - return; - - while (elements--) - { - if (!UnityDoublesWithin(*ptr_expected * UNITY_DOUBLE_PRECISION, *ptr_expected, *ptr_actual)) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrElement); - UnityPrintNumberUnsigned(num_elements - elements - 1); -#ifdef UNITY_DOUBLE_VERBOSE - UnityPrint(UnityStrExpected); - UnityPrintFloat((float)(*ptr_expected)); - UnityPrint(UnityStrWas); - UnityPrintFloat((float)(*ptr_actual)); -#else - UnityPrint(UnityStrDelta); -#endif - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } - ptr_expected++; - ptr_actual++; - } -} - -/*-----------------------------------------------*/ -void UnityAssertDoublesWithin(const _UD delta, - const _UD expected, - const _UD actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber) -{ - UNITY_SKIP_EXECUTION; - - if (!UnityDoublesWithin(delta, expected, actual)) - { - UnityTestResultsFailBegin(lineNumber); -#ifdef UNITY_DOUBLE_VERBOSE - UnityPrint(UnityStrExpected); - UnityPrintFloat((float)expected); - UnityPrint(UnityStrWas); - UnityPrintFloat((float)actual); -#else - UnityPrint(UnityStrDelta); -#endif - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } -} - -/*-----------------------------------------------*/ - -void UnityAssertDoubleSpecial(const _UD actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber, - const UNITY_FLOAT_TRAIT_T style) -{ - const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet }; - _U_SINT should_be_trait = ((_U_SINT)style & 1); - _U_SINT is_trait = !should_be_trait; - _U_SINT trait_index = (_U_SINT)(style >> 1); - - UNITY_SKIP_EXECUTION; - - switch(style) - { - case UNITY_FLOAT_IS_INF: - case UNITY_FLOAT_IS_NOT_INF: - is_trait = isinf(actual) & ispos(actual); - break; - case UNITY_FLOAT_IS_NEG_INF: - case UNITY_FLOAT_IS_NOT_NEG_INF: - is_trait = isinf(actual) & isneg(actual); - break; - - case UNITY_FLOAT_IS_NAN: - case UNITY_FLOAT_IS_NOT_NAN: - is_trait = isnan(actual); - break; - - /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */ - case UNITY_FLOAT_IS_DET: - case UNITY_FLOAT_IS_NOT_DET: - if (isinf(actual) | isnan(actual)) - is_trait = 0; - else - is_trait = 1; - break; - - default: - trait_index = 0; - trait_names[0] = UnityStrInvalidFloatTrait; - break; - } - - if (is_trait != should_be_trait) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrExpected); - if (!should_be_trait) - UnityPrint(UnityStrNot); - UnityPrint(trait_names[trait_index]); - UnityPrint(UnityStrWas); -#ifdef UNITY_DOUBLE_VERBOSE - UnityPrintFloat(actual); -#else - if (should_be_trait) - UnityPrint(UnityStrNot); - UnityPrint(trait_names[trait_index]); -#endif - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } -} - - -#endif /* not UNITY_EXCLUDE_DOUBLE */ - -/*-----------------------------------------------*/ -void UnityAssertNumbersWithin( const _U_UINT delta, - const _U_SINT expected, - const _U_SINT actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber, - const UNITY_DISPLAY_STYLE_T style) -{ - UNITY_SKIP_EXECUTION; - - if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) - { - if (actual > expected) - Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > delta); - else - Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > delta); - } - else - { - if ((_U_UINT)actual > (_U_UINT)expected) - Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > delta); - else - Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > delta); - } - - if (Unity.CurrentTestFailed) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrDelta); - UnityPrintNumberByStyle((_U_SINT)delta, style); - UnityPrint(UnityStrExpected); - UnityPrintNumberByStyle(expected, style); - UnityPrint(UnityStrWas); - UnityPrintNumberByStyle(actual, style); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } -} - -/*-----------------------------------------------*/ -void UnityAssertEqualString(const char* expected, - const char* actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber) -{ - _UU32 i; - - UNITY_SKIP_EXECUTION; - - /* if both pointers not null compare the strings */ - if (expected && actual) - { - for (i = 0; expected[i] || actual[i]; i++) - { - if (expected[i] != actual[i]) - { - Unity.CurrentTestFailed = 1; - break; - } - } - } - else - { /* handle case of one pointers being null (if both null, test should pass) */ - if (expected != actual) - { - Unity.CurrentTestFailed = 1; - } - } - - if (Unity.CurrentTestFailed) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrintExpectedAndActualStrings(expected, actual); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } -} - -/*-----------------------------------------------*/ -void UnityAssertEqualStringLen(const char* expected, - const char* actual, - const _UU32 length, - const char* msg, - const UNITY_LINE_TYPE lineNumber) -{ - _UU32 i; - - UNITY_SKIP_EXECUTION; - - /* if both pointers not null compare the strings */ - if (expected && actual) - { - for (i = 0; (i < length) && (expected[i] || actual[i]); i++) - { - if (expected[i] != actual[i]) - { - Unity.CurrentTestFailed = 1; - break; - } - } - } - else - { /* handle case of one pointers being null (if both null, test should pass) */ - if (expected != actual) - { - Unity.CurrentTestFailed = 1; - } - } - - if (Unity.CurrentTestFailed) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrintExpectedAndActualStringsLen(expected, actual, length); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } -} - - -/*-----------------------------------------------*/ -void UnityAssertEqualStringArray( const char** expected, - const char** actual, - const _UU32 num_elements, - const char* msg, - const UNITY_LINE_TYPE lineNumber) -{ - _UU32 i, j = 0; - - UNITY_SKIP_EXECUTION; - - /* if no elements, it's an error */ - if (num_elements == 0) - { - UnityPrintPointlessAndBail(); - } - - if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) - return; - - do - { - /* if both pointers not null compare the strings */ - if (expected[j] && actual[j]) - { - for (i = 0; expected[j][i] || actual[j][i]; i++) - { - if (expected[j][i] != actual[j][i]) - { - Unity.CurrentTestFailed = 1; - break; - } - } - } - else - { /* handle case of one pointers being null (if both null, test should pass) */ - if (expected[j] != actual[j]) - { - Unity.CurrentTestFailed = 1; - } - } - - if (Unity.CurrentTestFailed) - { - UnityTestResultsFailBegin(lineNumber); - if (num_elements > 1) - { - UnityPrint(UnityStrElement); - UnityPrintNumberUnsigned(j); - } - UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j])); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } - } while (++j < num_elements); -} - -/*-----------------------------------------------*/ -void UnityAssertEqualMemory( UNITY_INTERNAL_PTR expected, - UNITY_INTERNAL_PTR actual, - const _UU32 length, - const _UU32 num_elements, - const char* msg, - const UNITY_LINE_TYPE lineNumber) -{ - UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected; - UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual; - _UU32 elements = num_elements; - _UU32 bytes; - - UNITY_SKIP_EXECUTION; - - if ((elements == 0) || (length == 0)) - { - UnityPrintPointlessAndBail(); - } - - if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) - return; - - while (elements--) - { - bytes = length; - while (bytes--) - { - if (*ptr_exp != *ptr_act) - { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrMemory); - if (num_elements > 1) - { - UnityPrint(UnityStrElement); - UnityPrintNumberUnsigned(num_elements - elements - 1); - } - UnityPrint(UnityStrByte); - UnityPrintNumberUnsigned(length - bytes - 1); - UnityPrint(UnityStrExpected); - UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8); - UnityPrint(UnityStrWas); - UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; - } - ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 1); - ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 1); - } - } -} - -/*----------------------------------------------- - * Control Functions - *-----------------------------------------------*/ - -void UnityFail(const char* msg, const UNITY_LINE_TYPE line) -{ - UNITY_SKIP_EXECUTION; - - UnityTestResultsBegin(Unity.TestFile, line); - UnityPrintFail(); - if (msg != NULL) - { - UNITY_OUTPUT_CHAR(':'); - -#ifndef UNITY_EXCLUDE_DETAILS - if (Unity.CurrentDetail1) - { - UnityPrint(UnityStrDetail1Name); - UnityPrint(Unity.CurrentDetail1); - if (Unity.CurrentDetail2) - { - UnityPrint(UnityStrDetail2Name); - UnityPrint(Unity.CurrentDetail2); - } - UnityPrint(UnityStrSpacer); - } -#endif - if (msg[0] != ' ') - { - UNITY_OUTPUT_CHAR(' '); - } - UnityPrint(msg); - } - - UNITY_FAIL_AND_BAIL; -} - -/*-----------------------------------------------*/ -void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line) -{ - UNITY_SKIP_EXECUTION; - - UnityTestResultsBegin(Unity.TestFile, line); - UnityPrint(UnityStrIgnore); - if (msg != NULL) - { - UNITY_OUTPUT_CHAR(':'); - UNITY_OUTPUT_CHAR(' '); - UnityPrint(msg); - } - UNITY_IGNORE_AND_BAIL; -} - -/*-----------------------------------------------*/ -#if defined(UNITY_WEAK_ATTRIBUTE) - UNITY_WEAK_ATTRIBUTE void setUp(void) { } - UNITY_WEAK_ATTRIBUTE void tearDown(void) { } -#elif defined(UNITY_WEAK_PRAGMA) -# pragma weak setUp - void setUp(void) { } -# pragma weak tearDown - void tearDown(void) { } -#endif -/*-----------------------------------------------*/ -void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum) -{ - Unity.CurrentTestName = FuncName; - Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum; - Unity.NumberOfTests++; - UNITY_CLR_DETAILS(); - if (TEST_PROTECT()) - { - setUp(); - Func(); - } - if (TEST_PROTECT() && !(Unity.CurrentTestIgnored)) - { - tearDown(); - } - UnityConcludeTest(); -} - -/*-----------------------------------------------*/ -void UnityBegin(const char* filename) -{ - Unity.TestFile = filename; - Unity.CurrentTestName = NULL; - Unity.CurrentTestLineNumber = 0; - Unity.NumberOfTests = 0; - Unity.TestFailures = 0; - Unity.TestIgnores = 0; - Unity.CurrentTestFailed = 0; - Unity.CurrentTestIgnored = 0; - - UNITY_CLR_DETAILS(); - UNITY_OUTPUT_START(); -} - -/*-----------------------------------------------*/ -int UnityEnd(void) -{ - UNITY_PRINT_EOL(); - UnityPrint(UnityStrBreaker); - UNITY_PRINT_EOL(); - UnityPrintNumber((_U_SINT)(Unity.NumberOfTests)); - UnityPrint(UnityStrResultsTests); - UnityPrintNumber((_U_SINT)(Unity.TestFailures)); - UnityPrint(UnityStrResultsFailures); - UnityPrintNumber((_U_SINT)(Unity.TestIgnores)); - UnityPrint(UnityStrResultsIgnored); - UNITY_PRINT_EOL(); - if (Unity.TestFailures == 0U) - { - UnityPrintOk(); - } - else - { - UnityPrintFail(); -#ifdef UNITY_DIFFERENTIATE_FINAL_FAIL - UNITY_OUTPUT_CHAR('E'); UNITY_OUTPUT_CHAR('D'); -#endif - } - UNITY_PRINT_EOL(); - UNITY_FLUSH_CALL(); - UNITY_OUTPUT_COMPLETE(); - return (int)(Unity.TestFailures); -} - -/*----------------------------------------------- - * Command Line Argument Support - *-----------------------------------------------*/ -#ifdef UNITY_USE_COMMAND_LINE_ARGS - -char* UnityOptionIncludeNamed = NULL; -char* UnityOptionExcludeNamed = NULL; -int UnityVerbosity = 1; - -int UnityParseOptions(int argc, char** argv) -{ - UnityOptionIncludeNamed = NULL; - UnityOptionExcludeNamed = NULL; - - for (int i = 1; i < argc; i++) - { - if (argv[i][0] == '-') - { - switch(argv[i][1]) - { - case 'l': /* list tests */ - return -1; - case 'n': /* include tests with name including this string */ - case 'f': /* an alias for -n */ - if (argv[i][2] == '=') - UnityOptionIncludeNamed = &argv[i][3]; - else if (++i < argc) - UnityOptionIncludeNamed = argv[i]; - else - { - UnityPrint("ERROR: No Test String to Include Matches For"); - UNITY_PRINT_EOL(); - return 1; - } - break; - case 'q': /* quiet */ - UnityVerbosity = 0; - break; - case 'v': /* verbose */ - UnityVerbosity = 2; - break; - case 'x': /* exclude tests with name including this string */ - if (argv[i][2] == '=') - UnityOptionExcludeNamed = &argv[i][3]; - else if (++i < argc) - UnityOptionExcludeNamed = argv[i]; - else - { - UnityPrint("ERROR: No Test String to Exclude Matches For"); - UNITY_PRINT_EOL(); - return 1; - } - break; - default: - UnityPrint("ERROR: Unknown Option "); - UNITY_OUTPUT_CHAR(argv[i][1]); - UNITY_PRINT_EOL(); - return 1; - } - } - } - - return 0; -} - -int IsStringInBiggerString(const char* longstring, const char* shortstring) -{ - char* lptr = (char*)longstring; - char* sptr = (char*)shortstring; - char* lnext = lptr; - - if (*sptr == '*') - return 1; - - while (*lptr) - { - lnext = lptr + 1; - - /* If they current bytes match, go on to the next bytes */ - while (*lptr && *sptr && (*lptr == *sptr)) - { - lptr++; - sptr++; - - /* We're done if we match the entire string or up to a wildcard */ - if (*sptr == '*') - return 1; - if (*sptr == ',') - return 1; - if (*sptr == '"') - return 1; - if (*sptr == '\'') - return 1; - if (*sptr == ':') - return 2; - if (*sptr == 0) - return 1; - } - - /* Otherwise we start in the long pointer 1 character further and try again */ - lptr = lnext; - sptr = (char*)shortstring; - } - return 0; -} - -int UnityStringArgumentMatches(const char* str) -{ - int retval; - const char* ptr1; - const char* ptr2; - const char* ptrf; - - /* Go through the options and get the substrings for matching one at a time */ - ptr1 = str; - while (ptr1[0] != 0) - { - if ((ptr1[0] == '"') || (ptr1[0] == '\'')) - ptr1++; - - /* look for the start of the next partial */ - ptr2 = ptr1; - ptrf = 0; - do { - ptr2++; - if ((ptr2[0] == ':') && (ptr2[1] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ',')) - ptrf = &ptr2[1]; - } while ((ptr2[0] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ',')); - while ((ptr2[0] != 0) && ((ptr2[0] == ':') || (ptr2[0] == '\'') || (ptr2[0] == '"') || (ptr2[0] == ','))) - ptr2++; - - /* done if complete filename match */ - retval = IsStringInBiggerString(Unity.TestFile, ptr1); - if (retval == 1) - return retval; - - /* done if testname match after filename partial match */ - if ((retval == 2) && (ptrf != 0)) - { - if (IsStringInBiggerString(Unity.CurrentTestName, ptrf)) - return 1; - } - - /* done if complete testname match */ - if (IsStringInBiggerString(Unity.CurrentTestName, ptr1) == 1) - return 1; - - ptr1 = ptr2; - } - - /* we couldn't find a match for any substrings */ - return 0; -} - -int UnityTestMatches(void) -{ - /* Check if this test name matches the included test pattern */ - int retval; - if (UnityOptionIncludeNamed) - { - retval = UnityStringArgumentMatches(UnityOptionIncludeNamed); - } - else - retval = 1; - - /* Check if this test name matches the excluded test pattern */ - if (UnityOptionExcludeNamed) - { - if (UnityStringArgumentMatches(UnityOptionExcludeNamed)) - retval = 0; - } - return retval; -} - -#endif /* UNITY_USE_COMMAND_LINE_ARGS */ -/*-----------------------------------------------*/ diff --git a/test/unity/unity.h b/test/unity/unity.h deleted file mode 100644 index 031ccc92..00000000 --- a/test/unity/unity.h +++ /dev/null @@ -1,293 +0,0 @@ -/* ========================================== - Unity Project - A Test Framework for C - Copyright (c) 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams - [Released under MIT License. Please refer to license.txt for details] -========================================== */ - -#ifndef UNITY_FRAMEWORK_H -#define UNITY_FRAMEWORK_H -#define UNITY - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include "unity_internals.h" - -void setUp(void); -void tearDown(void); - -/*------------------------------------------------------- - * Configuration Options - *------------------------------------------------------- - * All options described below should be passed as a compiler flag to all files using Unity. If you must add #defines, place them BEFORE the #include above. - - * Integers/longs/pointers - * - Unity attempts to automatically discover your integer sizes - * - define UNITY_EXCLUDE_STDINT_H to stop attempting to look in - * - define UNITY_EXCLUDE_LIMITS_H to stop attempting to look in - * - If you cannot use the automatic methods above, you can force Unity by using these options: - * - define UNITY_SUPPORT_64 - * - set UNITY_INT_WIDTH - * - set UNITY_LONG_WIDTH - * - set UNITY_POINTER_WIDTH - - * Floats - * - define UNITY_EXCLUDE_FLOAT to disallow floating point comparisons - * - define UNITY_FLOAT_PRECISION to specify the precision to use when doing TEST_ASSERT_EQUAL_FLOAT - * - define UNITY_FLOAT_TYPE to specify doubles instead of single precision floats - * - define UNITY_FLOAT_VERBOSE to print floating point values in errors (uses sprintf) - * - define UNITY_INCLUDE_DOUBLE to allow double floating point comparisons - * - define UNITY_EXCLUDE_DOUBLE to disallow double floating point comparisons (default) - * - define UNITY_DOUBLE_PRECISION to specify the precision to use when doing TEST_ASSERT_EQUAL_DOUBLE - * - define UNITY_DOUBLE_TYPE to specify something other than double - * - define UNITY_DOUBLE_VERBOSE to print floating point values in errors (uses sprintf) - * - define UNITY_VERBOSE_NUMBER_MAX_LENGTH to change maximum length of printed numbers (used by sprintf) - - * Output - * - by default, Unity prints to standard out with putchar. define UNITY_OUTPUT_CHAR(a) with a different function if desired - * - define UNITY_DIFFERENTIATE_FINAL_FAIL to print FAILED (vs. FAIL) at test end summary - for automated search for failure - - * Optimization - * - by default, line numbers are stored in unsigned shorts. Define UNITY_LINE_TYPE with a different type if your files are huge - * - by default, test and failure counters are unsigned shorts. Define UNITY_COUNTER_TYPE with a different type if you want to save space or have more than 65535 Tests. - - * Test Cases - * - define UNITY_SUPPORT_TEST_CASES to include the TEST_CASE macro, though really it's mostly about the runner generator script - - * Parameterized Tests - * - you'll want to create a define of TEST_CASE(...) which basically evaluates to nothing - - * Tests with Arguments - * - you'll want to define UNITY_USE_COMMAND_LINE_ARGS if you have the test runner passing arguments to Unity - - *------------------------------------------------------- - * Basic Fail and Ignore - *-------------------------------------------------------*/ - -#define TEST_FAIL_MESSAGE(message) UNITY_TEST_FAIL(__LINE__, (message)) -#define TEST_FAIL() UNITY_TEST_FAIL(__LINE__, NULL) -#define TEST_IGNORE_MESSAGE(message) UNITY_TEST_IGNORE(__LINE__, (message)) -#define TEST_IGNORE() UNITY_TEST_IGNORE(__LINE__, NULL) -#define TEST_ONLY() - -/* It is not necessary for you to call PASS. A PASS condition is assumed if nothing fails. - * This method allows you to abort a test immediately with a PASS state, ignoring the remainder of the test. */ -#define TEST_PASS() longjmp(Unity.AbortFrame, 1) - -/*------------------------------------------------------- - * Test Asserts (simple) - *-------------------------------------------------------*/ - -/* Boolean */ -#define TEST_ASSERT(condition) UNITY_TEST_ASSERT( (condition), __LINE__, " Expression Evaluated To FALSE") -#define TEST_ASSERT_TRUE(condition) UNITY_TEST_ASSERT( (condition), __LINE__, " Expected TRUE Was FALSE") -#define TEST_ASSERT_UNLESS(condition) UNITY_TEST_ASSERT( !(condition), __LINE__, " Expression Evaluated To TRUE") -#define TEST_ASSERT_FALSE(condition) UNITY_TEST_ASSERT( !(condition), __LINE__, " Expected FALSE Was TRUE") -#define TEST_ASSERT_NULL(pointer) UNITY_TEST_ASSERT_NULL( (pointer), __LINE__, " Expected NULL") -#define TEST_ASSERT_NOT_NULL(pointer) UNITY_TEST_ASSERT_NOT_NULL((pointer), __LINE__, " Expected Non-NULL") - -/* Integers (of all sizes) */ -#define TEST_ASSERT_EQUAL_INT(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_INT8(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT8((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_INT16(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT16((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_INT32(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT32((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_INT64(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT64((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_NOT_EQUAL(expected, actual) UNITY_TEST_ASSERT(((expected) != (actual)), __LINE__, " Expected Not-Equal") -#define TEST_ASSERT_EQUAL_UINT(expected, actual) UNITY_TEST_ASSERT_EQUAL_UINT( (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_UINT8(expected, actual) UNITY_TEST_ASSERT_EQUAL_UINT8( (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_UINT16(expected, actual) UNITY_TEST_ASSERT_EQUAL_UINT16( (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_UINT32(expected, actual) UNITY_TEST_ASSERT_EQUAL_UINT32( (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_UINT64(expected, actual) UNITY_TEST_ASSERT_EQUAL_UINT64( (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_HEX(expected, actual) UNITY_TEST_ASSERT_EQUAL_HEX32((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_HEX8(expected, actual) UNITY_TEST_ASSERT_EQUAL_HEX8( (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_HEX16(expected, actual) UNITY_TEST_ASSERT_EQUAL_HEX16((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_HEX32(expected, actual) UNITY_TEST_ASSERT_EQUAL_HEX32((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_HEX64(expected, actual) UNITY_TEST_ASSERT_EQUAL_HEX64((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_BITS(mask, expected, actual) UNITY_TEST_ASSERT_BITS((mask), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_BITS_HIGH(mask, actual) UNITY_TEST_ASSERT_BITS((mask), (_UU32)(-1), (actual), __LINE__, NULL) -#define TEST_ASSERT_BITS_LOW(mask, actual) UNITY_TEST_ASSERT_BITS((mask), (_UU32)(0), (actual), __LINE__, NULL) -#define TEST_ASSERT_BIT_HIGH(bit, actual) UNITY_TEST_ASSERT_BITS(((_UU32)1 << (bit)), (_UU32)(-1), (actual), __LINE__, NULL) -#define TEST_ASSERT_BIT_LOW(bit, actual) UNITY_TEST_ASSERT_BITS(((_UU32)1 << (bit)), (_UU32)(0), (actual), __LINE__, NULL) - -/* Integer Ranges (of all sizes) */ -#define TEST_ASSERT_INT_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_INT_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_INT8_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_INT8_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_INT16_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_INT16_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_INT32_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_INT32_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_INT64_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_INT64_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_UINT_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_UINT_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_UINT8_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_UINT8_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_UINT16_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_UINT16_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_UINT32_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_UINT32_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_UINT64_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_UINT64_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_HEX_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_HEX32_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_HEX8_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_HEX8_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_HEX16_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_HEX16_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_HEX32_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_HEX32_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_HEX64_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_HEX64_WITHIN((delta), (expected), (actual), __LINE__, NULL) - -/* Structs and Strings */ -#define TEST_ASSERT_EQUAL_PTR(expected, actual) UNITY_TEST_ASSERT_EQUAL_PTR((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_STRING(expected, actual) UNITY_TEST_ASSERT_EQUAL_STRING((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) UNITY_TEST_ASSERT_EQUAL_STRING_LEN((expected), (actual), (len), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) UNITY_TEST_ASSERT_EQUAL_MEMORY((expected), (actual), (len), __LINE__, NULL) - -/* Arrays */ -#define TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_INT8_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT8_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_INT16_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT16_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_INT32_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT32_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_INT64_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT64_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_UINT_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_UINT_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_UINT8_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_UINT16_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_UINT32_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_UINT32_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_UINT64_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_UINT64_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_HEX_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_HEX16_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX16_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_HEX64_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_PTR_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_PTR_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements) UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((expected), (actual), (len), (num_elements), __LINE__, NULL) - -/* Floating Point (If Enabled) */ -#define TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_FLOAT_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_FLOAT(expected, actual) UNITY_TEST_ASSERT_EQUAL_FLOAT((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_FLOAT_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_FLOAT_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_FLOAT_IS_INF(actual) UNITY_TEST_ASSERT_FLOAT_IS_INF((actual), __LINE__, NULL) -#define TEST_ASSERT_FLOAT_IS_NEG_INF(actual) UNITY_TEST_ASSERT_FLOAT_IS_NEG_INF((actual), __LINE__, NULL) -#define TEST_ASSERT_FLOAT_IS_NAN(actual) UNITY_TEST_ASSERT_FLOAT_IS_NAN((actual), __LINE__, NULL) -#define TEST_ASSERT_FLOAT_IS_DETERMINATE(actual) UNITY_TEST_ASSERT_FLOAT_IS_DETERMINATE((actual), __LINE__, NULL) -#define TEST_ASSERT_FLOAT_IS_NOT_INF(actual) UNITY_TEST_ASSERT_FLOAT_IS_NOT_INF((actual), __LINE__, NULL) -#define TEST_ASSERT_FLOAT_IS_NOT_NEG_INF(actual) UNITY_TEST_ASSERT_FLOAT_IS_NOT_NEG_INF((actual), __LINE__, NULL) -#define TEST_ASSERT_FLOAT_IS_NOT_NAN(actual) UNITY_TEST_ASSERT_FLOAT_IS_NOT_NAN((actual), __LINE__, NULL) -#define TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE(actual) UNITY_TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE((actual), __LINE__, NULL) - -/* Double (If Enabled) */ -#define TEST_ASSERT_DOUBLE_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_DOUBLE_WITHIN((delta), (expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_DOUBLE(expected, actual) UNITY_TEST_ASSERT_EQUAL_DOUBLE((expected), (actual), __LINE__, NULL) -#define TEST_ASSERT_EQUAL_DOUBLE_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_DOUBLE_ARRAY((expected), (actual), (num_elements), __LINE__, NULL) -#define TEST_ASSERT_DOUBLE_IS_INF(actual) UNITY_TEST_ASSERT_DOUBLE_IS_INF((actual), __LINE__, NULL) -#define TEST_ASSERT_DOUBLE_IS_NEG_INF(actual) UNITY_TEST_ASSERT_DOUBLE_IS_NEG_INF((actual), __LINE__, NULL) -#define TEST_ASSERT_DOUBLE_IS_NAN(actual) UNITY_TEST_ASSERT_DOUBLE_IS_NAN((actual), __LINE__, NULL) -#define TEST_ASSERT_DOUBLE_IS_DETERMINATE(actual) UNITY_TEST_ASSERT_DOUBLE_IS_DETERMINATE((actual), __LINE__, NULL) -#define TEST_ASSERT_DOUBLE_IS_NOT_INF(actual) UNITY_TEST_ASSERT_DOUBLE_IS_NOT_INF((actual), __LINE__, NULL) -#define TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF(actual) UNITY_TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF((actual), __LINE__, NULL) -#define TEST_ASSERT_DOUBLE_IS_NOT_NAN(actual) UNITY_TEST_ASSERT_DOUBLE_IS_NOT_NAN((actual), __LINE__, NULL) -#define TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE(actual) UNITY_TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE((actual), __LINE__, NULL) - -/*------------------------------------------------------- - * Test Asserts (with additional messages) - *-------------------------------------------------------*/ - -/* Boolean */ -#define TEST_ASSERT_MESSAGE(condition, message) UNITY_TEST_ASSERT( (condition), __LINE__, (message)) -#define TEST_ASSERT_TRUE_MESSAGE(condition, message) UNITY_TEST_ASSERT( (condition), __LINE__, (message)) -#define TEST_ASSERT_UNLESS_MESSAGE(condition, message) UNITY_TEST_ASSERT( !(condition), __LINE__, (message)) -#define TEST_ASSERT_FALSE_MESSAGE(condition, message) UNITY_TEST_ASSERT( !(condition), __LINE__, (message)) -#define TEST_ASSERT_NULL_MESSAGE(pointer, message) UNITY_TEST_ASSERT_NULL( (pointer), __LINE__, (message)) -#define TEST_ASSERT_NOT_NULL_MESSAGE(pointer, message) UNITY_TEST_ASSERT_NOT_NULL((pointer), __LINE__, (message)) - -/* Integers (of all sizes) */ -#define TEST_ASSERT_EQUAL_INT_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_INT8_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT8((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_INT16_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT16((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_INT32_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT32((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_INT64_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT64((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_NOT_EQUAL_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT(((expected) != (actual)), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_UINT_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_UINT( (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_UINT8_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_UINT8( (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_UINT16_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_UINT16( (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_UINT32_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_UINT32( (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_UINT64_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_UINT64( (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_HEX_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_HEX32((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_HEX8_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_HEX8( (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_HEX16_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_HEX16((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_HEX32((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_HEX64_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_HEX64((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_BITS_MESSAGE(mask, expected, actual, message) UNITY_TEST_ASSERT_BITS((mask), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_BITS_HIGH_MESSAGE(mask, actual, message) UNITY_TEST_ASSERT_BITS((mask), (_UU32)(-1), (actual), __LINE__, (message)) -#define TEST_ASSERT_BITS_LOW_MESSAGE(mask, actual, message) UNITY_TEST_ASSERT_BITS((mask), (_UU32)(0), (actual), __LINE__, (message)) -#define TEST_ASSERT_BIT_HIGH_MESSAGE(bit, actual, message) UNITY_TEST_ASSERT_BITS(((_UU32)1 << (bit)), (_UU32)(-1), (actual), __LINE__, (message)) -#define TEST_ASSERT_BIT_LOW_MESSAGE(bit, actual, message) UNITY_TEST_ASSERT_BITS(((_UU32)1 << (bit)), (_UU32)(0), (actual), __LINE__, (message)) - -/* Integer Ranges (of all sizes) */ -#define TEST_ASSERT_INT_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_INT_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_INT8_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_INT8_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_INT16_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_INT16_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_INT32_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_INT32_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_INT64_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_INT64_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_UINT_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_UINT_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_UINT8_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_UINT8_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_UINT16_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_UINT16_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_UINT32_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_UINT32_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_UINT64_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_UINT64_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_HEX_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_HEX32_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_HEX8_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_HEX8_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_HEX16_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_HEX16_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_HEX32_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_HEX32_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_HEX64_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_HEX64_WITHIN((delta), (expected), (actual), __LINE__, (message)) - -/* Structs and Strings */ -#define TEST_ASSERT_EQUAL_PTR_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_PTR((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_STRING((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_EQUAL_STRING_LEN((expected), (actual), (len), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_EQUAL_MEMORY((expected), (actual), (len), __LINE__, (message)) - -/* Arrays */ -#define TEST_ASSERT_EQUAL_INT_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_INT8_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT8_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_INT16_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT16_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_INT32_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT32_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_INT64_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT64_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_UINT_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_UINT_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_UINT8_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_UINT16_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_UINT32_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_UINT32_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_UINT64_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_UINT64_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_HEX_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_HEX8_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_HEX16_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX16_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_HEX32_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_HEX64_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_PTR_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_PTR_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_STRING_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_MEMORY_ARRAY_MESSAGE(expected, actual, len, num_elements, message) UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((expected), (actual), (len), (num_elements), __LINE__, (message)) - -/* Floating Point (If Enabled) */ -#define TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_FLOAT_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_FLOAT_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_FLOAT((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_FLOAT_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_FLOAT_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_FLOAT_IS_INF_MESSAGE(actual, message) UNITY_TEST_ASSERT_FLOAT_IS_INF((actual), __LINE__, (message)) -#define TEST_ASSERT_FLOAT_IS_NEG_INF_MESSAGE(actual, message) UNITY_TEST_ASSERT_FLOAT_IS_NEG_INF((actual), __LINE__, (message)) -#define TEST_ASSERT_FLOAT_IS_NAN_MESSAGE(actual, message) UNITY_TEST_ASSERT_FLOAT_IS_NAN((actual), __LINE__, (message)) -#define TEST_ASSERT_FLOAT_IS_DETERMINATE_MESSAGE(actual, message) UNITY_TEST_ASSERT_FLOAT_IS_DETERMINATE((actual), __LINE__, (message)) -#define TEST_ASSERT_FLOAT_IS_NOT_INF_MESSAGE(actual, message) UNITY_TEST_ASSERT_FLOAT_IS_NOT_INF((actual), __LINE__, (message)) -#define TEST_ASSERT_FLOAT_IS_NOT_NEG_INF_MESSAGE(actual, message) UNITY_TEST_ASSERT_FLOAT_IS_NOT_NEG_INF((actual), __LINE__, (message)) -#define TEST_ASSERT_FLOAT_IS_NOT_NAN_MESSAGE(actual, message) UNITY_TEST_ASSERT_FLOAT_IS_NOT_NAN((actual), __LINE__, (message)) -#define TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE_MESSAGE(actual, message) UNITY_TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE((actual), __LINE__, (message)) - -/* Double (If Enabled) */ -#define TEST_ASSERT_DOUBLE_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_DOUBLE_WITHIN((delta), (expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_DOUBLE_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_DOUBLE((expected), (actual), __LINE__, (message)) -#define TEST_ASSERT_EQUAL_DOUBLE_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_DOUBLE_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) -#define TEST_ASSERT_DOUBLE_IS_INF_MESSAGE(actual, message) UNITY_TEST_ASSERT_DOUBLE_IS_INF((actual), __LINE__, (message)) -#define TEST_ASSERT_DOUBLE_IS_NEG_INF_MESSAGE(actual, message) UNITY_TEST_ASSERT_DOUBLE_IS_NEG_INF((actual), __LINE__, (message)) -#define TEST_ASSERT_DOUBLE_IS_NAN_MESSAGE(actual, message) UNITY_TEST_ASSERT_DOUBLE_IS_NAN((actual), __LINE__, (message)) -#define TEST_ASSERT_DOUBLE_IS_DETERMINATE_MESSAGE(actual, message) UNITY_TEST_ASSERT_DOUBLE_IS_DETERMINATE((actual), __LINE__, (message)) -#define TEST_ASSERT_DOUBLE_IS_NOT_INF_MESSAGE(actual, message) UNITY_TEST_ASSERT_DOUBLE_IS_NOT_INF((actual), __LINE__, (message)) -#define TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF_MESSAGE(actual, message) UNITY_TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF((actual), __LINE__, (message)) -#define TEST_ASSERT_DOUBLE_IS_NOT_NAN_MESSAGE(actual, message) UNITY_TEST_ASSERT_DOUBLE_IS_NOT_NAN((actual), __LINE__, (message)) -#define TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE_MESSAGE(actual, message) UNITY_TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE((actual), __LINE__, (message)) - -/* end of UNITY_FRAMEWORK_H */ -#ifdef __cplusplus -} -#endif -#endif diff --git a/test/unity/unity_internals.h b/test/unity/unity_internals.h deleted file mode 100644 index 8ccd66d5..00000000 --- a/test/unity/unity_internals.h +++ /dev/null @@ -1,749 +0,0 @@ -/* ========================================== - Unity Project - A Test Framework for C - Copyright (c) 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams - [Released under MIT License. Please refer to license.txt for details] -========================================== */ - -#ifndef UNITY_INTERNALS_H -#define UNITY_INTERNALS_H - -#ifdef UNITY_INCLUDE_CONFIG_H -#include "unity_config.h" -#endif - -#include - -#ifndef UNITY_EXCLUDE_MATH_H -#include -#endif - -/* Unity Attempts to Auto-Detect Integer Types - * Attempt 1: UINT_MAX, ULONG_MAX in , or default to 32 bits - * Attempt 2: UINTPTR_MAX in , or default to same size as long - * The user may override any of these derived constants: - * UNITY_INT_WIDTH, UNITY_LONG_WIDTH, UNITY_POINTER_WIDTH */ -#ifndef UNITY_EXCLUDE_STDINT_H -#include -#endif - -#ifndef UNITY_EXCLUDE_LIMITS_H -#include -#endif - -/*------------------------------------------------------- - * Guess Widths If Not Specified - *-------------------------------------------------------*/ - -/* Determine the size of an int, if not already specified. - * We cannot use sizeof(int), because it is not yet defined - * at this stage in the translation of the C program. - * Therefore, infer it from UINT_MAX if possible. */ -#ifndef UNITY_INT_WIDTH - #ifdef UINT_MAX - #if (UINT_MAX == 0xFFFF) - #define UNITY_INT_WIDTH (16) - #elif (UINT_MAX == 0xFFFFFFFF) - #define UNITY_INT_WIDTH (32) - #elif (UINT_MAX == 0xFFFFFFFFFFFFFFFF) - #define UNITY_INT_WIDTH (64) - #endif - #else /* Set to default */ - #define UNITY_INT_WIDTH (32) - #endif /* UINT_MAX */ -#endif - -/* Determine the size of a long, if not already specified. */ -#ifndef UNITY_LONG_WIDTH - #ifdef ULONG_MAX - #if (ULONG_MAX == 0xFFFF) - #define UNITY_LONG_WIDTH (16) - #elif (ULONG_MAX == 0xFFFFFFFF) - #define UNITY_LONG_WIDTH (32) - #elif (ULONG_MAX == 0xFFFFFFFFFFFFFFFF) - #define UNITY_LONG_WIDTH (64) - #endif - #else /* Set to default */ - #define UNITY_LONG_WIDTH (32) - #endif /* ULONG_MAX */ -#endif - -/* Determine the size of a pointer, if not already specified. */ -#ifndef UNITY_POINTER_WIDTH - #ifdef UINTPTR_MAX - #if (UINTPTR_MAX <= 0xFFFF) - #define UNITY_POINTER_WIDTH (16) - #elif (UINTPTR_MAX <= 0xFFFFFFFF) - #define UNITY_POINTER_WIDTH (32) - #elif (UINTPTR_MAX <= 0xFFFFFFFFFFFFFFFF) - #define UNITY_POINTER_WIDTH (64) - #endif - #else /* Set to default */ - #define UNITY_POINTER_WIDTH UNITY_LONG_WIDTH - #endif /* UINTPTR_MAX */ -#endif - -/*------------------------------------------------------- - * Int Support (Define types based on detected sizes) - *-------------------------------------------------------*/ - -#if (UNITY_INT_WIDTH == 32) - typedef unsigned char _UU8; - typedef unsigned short _UU16; - typedef unsigned int _UU32; - typedef signed char _US8; - typedef signed short _US16; - typedef signed int _US32; -#elif (UNITY_INT_WIDTH == 16) - typedef unsigned char _UU8; - typedef unsigned int _UU16; - typedef unsigned long _UU32; - typedef signed char _US8; - typedef signed int _US16; - typedef signed long _US32; -#else - #error Invalid UNITY_INT_WIDTH specified! (16 or 32 are supported) -#endif - -/*------------------------------------------------------- - * 64-bit Support - *-------------------------------------------------------*/ - -#ifndef UNITY_SUPPORT_64 - #if UNITY_LONG_WIDTH == 64 || UNITY_POINTER_WIDTH == 64 - #define UNITY_SUPPORT_64 - #endif -#endif - -#ifndef UNITY_SUPPORT_64 - /* No 64-bit Support */ - typedef _UU32 _U_UINT; - typedef _US32 _U_SINT; -#else - - /* 64-bit Support */ - #if (UNITY_LONG_WIDTH == 32) - typedef unsigned long long _UU64; - typedef signed long long _US64; - #elif (UNITY_LONG_WIDTH == 64) - typedef unsigned long _UU64; - typedef signed long _US64; - #else - #error Invalid UNITY_LONG_WIDTH specified! (32 or 64 are supported) - #endif - typedef _UU64 _U_UINT; - typedef _US64 _U_SINT; - -#endif - -/*------------------------------------------------------- - * Pointer Support - *-------------------------------------------------------*/ - -#if (UNITY_POINTER_WIDTH == 32) - typedef _UU32 _UP; -#define UNITY_DISPLAY_STYLE_POINTER UNITY_DISPLAY_STYLE_HEX32 -#elif (UNITY_POINTER_WIDTH == 64) - typedef _UU64 _UP; -#define UNITY_DISPLAY_STYLE_POINTER UNITY_DISPLAY_STYLE_HEX64 -#elif (UNITY_POINTER_WIDTH == 16) - typedef _UU16 _UP; -#define UNITY_DISPLAY_STYLE_POINTER UNITY_DISPLAY_STYLE_HEX16 -#else - #error Invalid UNITY_POINTER_WIDTH specified! (16, 32 or 64 are supported) -#endif - -#ifndef UNITY_PTR_ATTRIBUTE -#define UNITY_PTR_ATTRIBUTE -#endif - -#ifndef UNITY_INTERNAL_PTR -#define UNITY_INTERNAL_PTR UNITY_PTR_ATTRIBUTE const void* -/* #define UNITY_INTERNAL_PTR UNITY_PTR_ATTRIBUTE const _UU8* */ -#endif - -/*------------------------------------------------------- - * Float Support - *-------------------------------------------------------*/ - -#ifdef UNITY_EXCLUDE_FLOAT - -/* No Floating Point Support */ -#undef UNITY_INCLUDE_FLOAT -#undef UNITY_FLOAT_PRECISION -#undef UNITY_FLOAT_TYPE -#undef UNITY_FLOAT_VERBOSE - -#else - -#ifndef UNITY_INCLUDE_FLOAT -#define UNITY_INCLUDE_FLOAT -#endif - -/* Floating Point Support */ -#ifndef UNITY_FLOAT_PRECISION -#define UNITY_FLOAT_PRECISION (0.00001f) -#endif -#ifndef UNITY_FLOAT_TYPE -#define UNITY_FLOAT_TYPE float -#endif -typedef UNITY_FLOAT_TYPE _UF; - -#ifndef isinf -/* The value of Inf - Inf is NaN */ -#define isinf(n) (isnan((n) - (n)) && !isnan(n)) -#endif - -#ifndef isnan -/* NaN is the only floating point value that does NOT equal itself. - * Therefore if n != n, then it is NaN. */ -#define isnan(n) ((n != n) ? 1 : 0) -#endif - -#ifndef isneg -#define isneg(n) ((n < 0.0f) ? 1 : 0) -#endif - -#ifndef ispos -#define ispos(n) ((n > 0.0f) ? 1 : 0) -#endif - -#endif - -/*------------------------------------------------------- - * Double Float Support - *-------------------------------------------------------*/ - -/* unlike FLOAT, we DON'T include by default */ -#ifndef UNITY_EXCLUDE_DOUBLE - #ifndef UNITY_INCLUDE_DOUBLE - #define UNITY_EXCLUDE_DOUBLE - #endif -#endif - -#ifdef UNITY_EXCLUDE_DOUBLE - - /* No Floating Point Support */ - #undef UNITY_DOUBLE_PRECISION - #undef UNITY_DOUBLE_TYPE - #undef UNITY_DOUBLE_VERBOSE - - #ifdef UNITY_INCLUDE_DOUBLE - #undef UNITY_INCLUDE_DOUBLE - #endif - - #ifdef UNITY_FLOAT_VERBOSE - typedef _UF _UD; - /* For parameter in UnityPrintFloat, double promotion required */ - #endif - -#else - - /* Double Floating Point Support */ - #ifndef UNITY_DOUBLE_PRECISION - #define UNITY_DOUBLE_PRECISION (1e-12f) - #endif - - #ifndef UNITY_DOUBLE_TYPE - #define UNITY_DOUBLE_TYPE double - #endif - typedef UNITY_DOUBLE_TYPE _UD; - -#endif - -#ifdef UNITY_DOUBLE_VERBOSE -#ifndef UNITY_FLOAT_VERBOSE -#define UNITY_FLOAT_VERBOSE -#endif -#endif - -/*------------------------------------------------------- - * Output Method: stdout (DEFAULT) - *-------------------------------------------------------*/ -#ifndef UNITY_OUTPUT_CHAR -/* Default to using putchar, which is defined in stdio.h */ -#include -#define UNITY_OUTPUT_CHAR(a) (void)putchar(a) -#else - /* If defined as something else, make sure we declare it here so it's ready for use */ - #ifndef UNITY_OMIT_OUTPUT_CHAR_HEADER_DECLARATION -extern void UNITY_OUTPUT_CHAR(int); - #endif -#endif - -#ifndef UNITY_OUTPUT_FLUSH -/* Default to using fflush, which is defined in stdio.h */ -#include -#define UNITY_OUTPUT_FLUSH (void)fflush(stdout) -#else - /* If defined as something else, make sure we declare it here so it's ready for use */ - #ifndef UNITY_OMIT_OUTPUT_FLUSH_HEADER_DECLARATION -extern void UNITY_OUTPUT_FLUSH(void); - #endif -#endif - -#ifndef UNITY_OUTPUT_FLUSH -#define UNITY_FLUSH_CALL() -#else -#define UNITY_FLUSH_CALL() UNITY_OUTPUT_FLUSH -#endif - -#ifndef UNITY_PRINT_EOL -#define UNITY_PRINT_EOL() UNITY_OUTPUT_CHAR('\n') -#endif - -#ifndef UNITY_OUTPUT_START -#define UNITY_OUTPUT_START() -#endif - -#ifndef UNITY_OUTPUT_COMPLETE -#define UNITY_OUTPUT_COMPLETE() -#endif - -/*------------------------------------------------------- - * Footprint - *-------------------------------------------------------*/ - -#ifndef UNITY_LINE_TYPE -#define UNITY_LINE_TYPE _U_UINT -#endif - -#ifndef UNITY_COUNTER_TYPE -#define UNITY_COUNTER_TYPE _U_UINT -#endif - -/*------------------------------------------------------- - * Language Features Available - *-------------------------------------------------------*/ -#if !defined(UNITY_WEAK_ATTRIBUTE) && !defined(UNITY_WEAK_PRAGMA) -# ifdef __GNUC__ /* includes clang */ -# if !(defined(__WIN32__) && defined(__clang__)) && !defined(__TMS470__) -# define UNITY_WEAK_ATTRIBUTE __attribute__((weak)) -# endif -# endif -#endif - -#ifdef UNITY_NO_WEAK -# undef UNITY_WEAK_ATTRIBUTE -# undef UNITY_WEAK_PRAGMA -#endif - - -/*------------------------------------------------------- - * Internal Structs Needed - *-------------------------------------------------------*/ - -typedef void (*UnityTestFunction)(void); - -#define UNITY_DISPLAY_RANGE_INT (0x10) -#define UNITY_DISPLAY_RANGE_UINT (0x20) -#define UNITY_DISPLAY_RANGE_HEX (0x40) -#define UNITY_DISPLAY_RANGE_AUTO (0x80) - -typedef enum -{ -UNITY_DISPLAY_STYLE_INT = sizeof(int)+ UNITY_DISPLAY_RANGE_INT + UNITY_DISPLAY_RANGE_AUTO, - UNITY_DISPLAY_STYLE_INT8 = 1 + UNITY_DISPLAY_RANGE_INT, - UNITY_DISPLAY_STYLE_INT16 = 2 + UNITY_DISPLAY_RANGE_INT, - UNITY_DISPLAY_STYLE_INT32 = 4 + UNITY_DISPLAY_RANGE_INT, -#ifdef UNITY_SUPPORT_64 - UNITY_DISPLAY_STYLE_INT64 = 8 + UNITY_DISPLAY_RANGE_INT, -#endif - -UNITY_DISPLAY_STYLE_UINT = sizeof(unsigned) + UNITY_DISPLAY_RANGE_UINT + UNITY_DISPLAY_RANGE_AUTO, - UNITY_DISPLAY_STYLE_UINT8 = 1 + UNITY_DISPLAY_RANGE_UINT, - UNITY_DISPLAY_STYLE_UINT16 = 2 + UNITY_DISPLAY_RANGE_UINT, - UNITY_DISPLAY_STYLE_UINT32 = 4 + UNITY_DISPLAY_RANGE_UINT, -#ifdef UNITY_SUPPORT_64 - UNITY_DISPLAY_STYLE_UINT64 = 8 + UNITY_DISPLAY_RANGE_UINT, -#endif - - UNITY_DISPLAY_STYLE_HEX8 = 1 + UNITY_DISPLAY_RANGE_HEX, - UNITY_DISPLAY_STYLE_HEX16 = 2 + UNITY_DISPLAY_RANGE_HEX, - UNITY_DISPLAY_STYLE_HEX32 = 4 + UNITY_DISPLAY_RANGE_HEX, -#ifdef UNITY_SUPPORT_64 - UNITY_DISPLAY_STYLE_HEX64 = 8 + UNITY_DISPLAY_RANGE_HEX, -#endif - - UNITY_DISPLAY_STYLE_UNKNOWN -} UNITY_DISPLAY_STYLE_T; - -#ifndef UNITY_EXCLUDE_FLOAT -typedef enum _UNITY_FLOAT_TRAIT_T -{ - UNITY_FLOAT_IS_NOT_INF = 0, - UNITY_FLOAT_IS_INF, - UNITY_FLOAT_IS_NOT_NEG_INF, - UNITY_FLOAT_IS_NEG_INF, - UNITY_FLOAT_IS_NOT_NAN, - UNITY_FLOAT_IS_NAN, - UNITY_FLOAT_IS_NOT_DET, - UNITY_FLOAT_IS_DET, - UNITY_FLOAT_INVALID_TRAIT -} UNITY_FLOAT_TRAIT_T; -#endif - -struct _Unity -{ - const char* TestFile; - const char* CurrentTestName; -#ifndef UNITY_EXCLUDE_DETAILS - const char* CurrentDetail1; - const char* CurrentDetail2; -#endif - UNITY_LINE_TYPE CurrentTestLineNumber; - UNITY_COUNTER_TYPE NumberOfTests; - UNITY_COUNTER_TYPE TestFailures; - UNITY_COUNTER_TYPE TestIgnores; - UNITY_COUNTER_TYPE CurrentTestFailed; - UNITY_COUNTER_TYPE CurrentTestIgnored; - jmp_buf AbortFrame; -}; - -extern struct _Unity Unity; - -/*------------------------------------------------------- - * Test Suite Management - *-------------------------------------------------------*/ - -void UnityBegin(const char* filename); -int UnityEnd(void); -void UnityConcludeTest(void); -void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum); - -/*------------------------------------------------------- - * Details Support - *-------------------------------------------------------*/ - -#ifdef UNITY_EXCLUDE_DETAILS -#define UNITY_CLR_DETAILS() -#define UNITY_SET_DETAIL(d1) -#define UNITY_SET_DETAILS(d1,d2) -#else -#define UNITY_CLR_DETAILS() { Unity.CurrentDetail1 = 0; Unity.CurrentDetail2 = 0; } -#define UNITY_SET_DETAIL(d1) { Unity.CurrentDetail1 = d1; Unity.CurrentDetail2 = 0; } -#define UNITY_SET_DETAILS(d1,d2) { Unity.CurrentDetail1 = d1; Unity.CurrentDetail2 = d2; } - -#ifndef UNITY_DETAIL1_NAME -#define UNITY_DETAIL1_NAME "Function" -#endif - -#ifndef UNITY_DETAIL2_NAME -#define UNITY_DETAIL2_NAME "Argument" -#endif -#endif - -/*------------------------------------------------------- - * Test Output - *-------------------------------------------------------*/ - -void UnityPrint(const char* string); -void UnityPrintMask(const _U_UINT mask, const _U_UINT number); -void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style); -void UnityPrintNumber(const _U_SINT number); -void UnityPrintNumberUnsigned(const _U_UINT number); -void UnityPrintNumberHex(const _U_UINT number, const char nibbles); - -#ifdef UNITY_FLOAT_VERBOSE -void UnityPrintFloat(const _UD number); -#endif - -/*------------------------------------------------------- - * Test Assertion Functions - *------------------------------------------------------- - * Use the macros below this section instead of calling - * these directly. The macros have a consistent naming - * convention and will pull in file and line information - * for you. */ - -void UnityAssertEqualNumber(const _U_SINT expected, - const _U_SINT actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber, - const UNITY_DISPLAY_STYLE_T style); - -void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, - UNITY_INTERNAL_PTR actual, - const _UU32 num_elements, - const char* msg, - const UNITY_LINE_TYPE lineNumber, - const UNITY_DISPLAY_STYLE_T style); - -void UnityAssertBits(const _U_SINT mask, - const _U_SINT expected, - const _U_SINT actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber); - -void UnityAssertEqualString(const char* expected, - const char* actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber); - -void UnityAssertEqualStringLen(const char* expected, - const char* actual, - const _UU32 length, - const char* msg, - const UNITY_LINE_TYPE lineNumber); - -void UnityAssertEqualStringArray( const char** expected, - const char** actual, - const _UU32 num_elements, - const char* msg, - const UNITY_LINE_TYPE lineNumber); - -void UnityAssertEqualMemory( UNITY_INTERNAL_PTR expected, - UNITY_INTERNAL_PTR actual, - const _UU32 length, - const _UU32 num_elements, - const char* msg, - const UNITY_LINE_TYPE lineNumber); - -void UnityAssertNumbersWithin(const _U_UINT delta, - const _U_SINT expected, - const _U_SINT actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber, - const UNITY_DISPLAY_STYLE_T style); - -void UnityFail(const char* message, const UNITY_LINE_TYPE line); - -void UnityIgnore(const char* message, const UNITY_LINE_TYPE line); - -#ifndef UNITY_EXCLUDE_FLOAT -void UnityAssertFloatsWithin(const _UF delta, - const _UF expected, - const _UF actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber); - -void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected, - UNITY_PTR_ATTRIBUTE const _UF* actual, - const _UU32 num_elements, - const char* msg, - const UNITY_LINE_TYPE lineNumber); - -void UnityAssertFloatSpecial(const _UF actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber, - const UNITY_FLOAT_TRAIT_T style); -#endif - -#ifndef UNITY_EXCLUDE_DOUBLE -void UnityAssertDoublesWithin(const _UD delta, - const _UD expected, - const _UD actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber); - -void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected, - UNITY_PTR_ATTRIBUTE const _UD* actual, - const _UU32 num_elements, - const char* msg, - const UNITY_LINE_TYPE lineNumber); - -void UnityAssertDoubleSpecial(const _UD actual, - const char* msg, - const UNITY_LINE_TYPE lineNumber, - const UNITY_FLOAT_TRAIT_T style); -#endif - -/*------------------------------------------------------- - * Error Strings We Might Need - *-------------------------------------------------------*/ - -extern const char UnityStrErrFloat[]; -extern const char UnityStrErrDouble[]; -extern const char UnityStrErr64[]; - -/*------------------------------------------------------- - * Test Running Macros - *-------------------------------------------------------*/ - -#define TEST_PROTECT() (setjmp(Unity.AbortFrame) == 0) - -#define TEST_ABORT() {longjmp(Unity.AbortFrame, 1);} - -/* This tricky series of macros gives us an optional line argument to treat it as RUN_TEST(func, num=__LINE__) */ -#ifndef RUN_TEST -#ifdef __STDC_VERSION__ -#if __STDC_VERSION__ >= 199901L -#define RUN_TEST(...) UnityDefaultTestRun(RUN_TEST_FIRST(__VA_ARGS__), RUN_TEST_SECOND(__VA_ARGS__)) -#define RUN_TEST_FIRST(...) RUN_TEST_FIRST_HELPER(__VA_ARGS__, throwaway) -#define RUN_TEST_FIRST_HELPER(first, ...) (first), #first -#define RUN_TEST_SECOND(...) RUN_TEST_SECOND_HELPER(__VA_ARGS__, __LINE__, throwaway) -#define RUN_TEST_SECOND_HELPER(first, second, ...) (second) -#endif -#endif -#endif - -/* If we can't do the tricky version, we'll just have to require them to always include the line number */ -#ifndef RUN_TEST -#ifdef CMOCK -#define RUN_TEST(func, num) UnityDefaultTestRun(func, #func, num) -#else -#define RUN_TEST(func) UnityDefaultTestRun(func, #func, __LINE__) -#endif -#endif - -#define TEST_LINE_NUM (Unity.CurrentTestLineNumber) -#define TEST_IS_IGNORED (Unity.CurrentTestIgnored) -#define UNITY_NEW_TEST(a) \ - Unity.CurrentTestName = (a); \ - Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)(__LINE__); \ - Unity.NumberOfTests++; - -#ifndef UNITY_BEGIN -#define UNITY_BEGIN() UnityBegin(__FILE__) -#endif - -#ifndef UNITY_END -#define UNITY_END() UnityEnd() -#endif - -#define UNITY_UNUSED(x) (void)(sizeof(x)) - -/*----------------------------------------------- - * Command Line Argument Support - *-----------------------------------------------*/ - -#ifdef UNITY_USE_COMMAND_LINE_ARGS -int UnityParseOptions(int argc, char** argv); -int UnityTestMatches(void); -#endif - -/*------------------------------------------------------- - * Basic Fail and Ignore - *-------------------------------------------------------*/ - -#define UNITY_TEST_FAIL(line, message) UnityFail( (message), (UNITY_LINE_TYPE)(line)) -#define UNITY_TEST_IGNORE(line, message) UnityIgnore( (message), (UNITY_LINE_TYPE)(line)) - -/*------------------------------------------------------- - * Test Asserts - *-------------------------------------------------------*/ - -#define UNITY_TEST_ASSERT(condition, line, message) if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));} -#define UNITY_TEST_ASSERT_NULL(pointer, line, message) UNITY_TEST_ASSERT(((pointer) == NULL), (UNITY_LINE_TYPE)(line), (message)) -#define UNITY_TEST_ASSERT_NOT_NULL(pointer, line, message) UNITY_TEST_ASSERT(((pointer) != NULL), (UNITY_LINE_TYPE)(line), (message)) - -#define UNITY_TEST_ASSERT_EQUAL_INT(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT) -#define UNITY_TEST_ASSERT_EQUAL_INT8(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_US8 )(expected), (_U_SINT)(_US8 )(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT8) -#define UNITY_TEST_ASSERT_EQUAL_INT16(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_US16)(expected), (_U_SINT)(_US16)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT16) -#define UNITY_TEST_ASSERT_EQUAL_INT32(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_US32)(expected), (_U_SINT)(_US32)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT32) -#define UNITY_TEST_ASSERT_EQUAL_UINT(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT) -#define UNITY_TEST_ASSERT_EQUAL_UINT8(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_UU8 )(expected), (_U_SINT)(_UU8 )(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT8) -#define UNITY_TEST_ASSERT_EQUAL_UINT16(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_UU16)(expected), (_U_SINT)(_UU16)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT16) -#define UNITY_TEST_ASSERT_EQUAL_UINT32(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_UU32)(expected), (_U_SINT)(_UU32)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT32) -#define UNITY_TEST_ASSERT_EQUAL_HEX8(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_US8 )(expected), (_U_SINT)(_US8 )(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX8) -#define UNITY_TEST_ASSERT_EQUAL_HEX16(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_US16)(expected), (_U_SINT)(_US16)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX16) -#define UNITY_TEST_ASSERT_EQUAL_HEX32(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_US32)(expected), (_U_SINT)(_US32)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX32) -#define UNITY_TEST_ASSERT_BITS(mask, expected, actual, line, message) UnityAssertBits((_U_SINT)(mask), (_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line)) - -#define UNITY_TEST_ASSERT_INT_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((delta), (_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT) -#define UNITY_TEST_ASSERT_INT8_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_UU8 )(delta), (_U_SINT)(_US8 )(expected), (_U_SINT)(_US8 )(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT8) -#define UNITY_TEST_ASSERT_INT16_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_UU16)(delta), (_U_SINT)(_US16)(expected), (_U_SINT)(_US16)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT16) -#define UNITY_TEST_ASSERT_INT32_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_UU32)(delta), (_U_SINT)(_US32)(expected), (_U_SINT)(_US32)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT32) -#define UNITY_TEST_ASSERT_UINT_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((delta), (_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT) -#define UNITY_TEST_ASSERT_UINT8_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_UU8 )(delta), (_U_SINT)(_U_UINT)(_UU8 )(expected), (_U_SINT)(_U_UINT)(_UU8 )(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT8) -#define UNITY_TEST_ASSERT_UINT16_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_UU16)(delta), (_U_SINT)(_U_UINT)(_UU16)(expected), (_U_SINT)(_U_UINT)(_UU16)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT16) -#define UNITY_TEST_ASSERT_UINT32_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_UU32)(delta), (_U_SINT)(_U_UINT)(_UU32)(expected), (_U_SINT)(_U_UINT)(_UU32)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT32) -#define UNITY_TEST_ASSERT_HEX8_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_UU8 )(delta), (_U_SINT)(_U_UINT)(_UU8 )(expected), (_U_SINT)(_U_UINT)(_UU8 )(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX8) -#define UNITY_TEST_ASSERT_HEX16_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_UU16)(delta), (_U_SINT)(_U_UINT)(_UU16)(expected), (_U_SINT)(_U_UINT)(_UU16)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX16) -#define UNITY_TEST_ASSERT_HEX32_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_UU32)(delta), (_U_SINT)(_U_UINT)(_UU32)(expected), (_U_SINT)(_U_UINT)(_UU32)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX32) - -#define UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_UP)(expected), (_U_SINT)(_UP)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_POINTER) -#define UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, line, message) UnityAssertEqualString((const char*)(expected), (const char*)(actual), (message), (UNITY_LINE_TYPE)(line)) -#define UNITY_TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len, line, message) UnityAssertEqualStringLen((const char*)(expected), (const char*)(actual), (_UU32)(len), (message), (UNITY_LINE_TYPE)(line)) -#define UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(len), 1, (message), (UNITY_LINE_TYPE)(line)) - -#define UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT) -#define UNITY_TEST_ASSERT_EQUAL_INT8_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT8) -#define UNITY_TEST_ASSERT_EQUAL_INT16_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT16) -#define UNITY_TEST_ASSERT_EQUAL_INT32_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT32) -#define UNITY_TEST_ASSERT_EQUAL_UINT_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT) -#define UNITY_TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT8) -#define UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT16) -#define UNITY_TEST_ASSERT_EQUAL_UINT32_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT32) -#define UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX8) -#define UNITY_TEST_ASSERT_EQUAL_HEX16_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX16) -#define UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX32) -#define UNITY_TEST_ASSERT_EQUAL_PTR_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(_UP*)(expected), (UNITY_INTERNAL_PTR)(_UP*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_POINTER) -#define UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualStringArray((const char**)(expected), (const char**)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line)) -#define UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(len), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line)) - -#ifdef UNITY_SUPPORT_64 -#define UNITY_TEST_ASSERT_EQUAL_INT64(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT64) -#define UNITY_TEST_ASSERT_EQUAL_UINT64(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT64) -#define UNITY_TEST_ASSERT_EQUAL_HEX64(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX64) -#define UNITY_TEST_ASSERT_EQUAL_INT64_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT64) -#define UNITY_TEST_ASSERT_EQUAL_UINT64_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT64) -#define UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX64) -#define UNITY_TEST_ASSERT_INT64_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((delta), (_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT64) -#define UNITY_TEST_ASSERT_UINT64_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((delta), (_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT64) -#define UNITY_TEST_ASSERT_HEX64_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((delta), (_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX64) -#else -#define UNITY_TEST_ASSERT_EQUAL_INT64(expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErr64) -#define UNITY_TEST_ASSERT_EQUAL_UINT64(expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErr64) -#define UNITY_TEST_ASSERT_EQUAL_HEX64(expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErr64) -#define UNITY_TEST_ASSERT_EQUAL_INT64_ARRAY(expected, actual, num_elements, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErr64) -#define UNITY_TEST_ASSERT_EQUAL_UINT64_ARRAY(expected, actual, num_elements, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErr64) -#define UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY(expected, actual, num_elements, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErr64) -#define UNITY_TEST_ASSERT_INT64_WITHIN(delta, expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErr64) -#define UNITY_TEST_ASSERT_UINT64_WITHIN(delta, expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErr64) -#define UNITY_TEST_ASSERT_HEX64_WITHIN(delta, expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErr64) -#endif - -#ifdef UNITY_EXCLUDE_FLOAT -#define UNITY_TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#define UNITY_TEST_ASSERT_EQUAL_FLOAT(expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#define UNITY_TEST_ASSERT_EQUAL_FLOAT_ARRAY(expected, actual, num_elements, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#define UNITY_TEST_ASSERT_FLOAT_IS_INF(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#define UNITY_TEST_ASSERT_FLOAT_IS_NEG_INF(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#define UNITY_TEST_ASSERT_FLOAT_IS_NAN(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#define UNITY_TEST_ASSERT_FLOAT_IS_DETERMINATE(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#define UNITY_TEST_ASSERT_FLOAT_IS_NOT_INF(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#define UNITY_TEST_ASSERT_FLOAT_IS_NOT_NEG_INF(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#define UNITY_TEST_ASSERT_FLOAT_IS_NOT_NAN(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#define UNITY_TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrFloat) -#else -#define UNITY_TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual, line, message) UnityAssertFloatsWithin((_UF)(delta), (_UF)(expected), (_UF)(actual), (message), (UNITY_LINE_TYPE)(line)) -#define UNITY_TEST_ASSERT_EQUAL_FLOAT(expected, actual, line, message) UNITY_TEST_ASSERT_FLOAT_WITHIN((_UF)(expected) * (_UF)UNITY_FLOAT_PRECISION, (_UF)(expected), (_UF)(actual), (UNITY_LINE_TYPE)(line), (message)) -#define UNITY_TEST_ASSERT_EQUAL_FLOAT_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualFloatArray((_UF*)(expected), (_UF*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)(line)) -#define UNITY_TEST_ASSERT_FLOAT_IS_INF(actual, line, message) UnityAssertFloatSpecial((_UF)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_INF) -#define UNITY_TEST_ASSERT_FLOAT_IS_NEG_INF(actual, line, message) UnityAssertFloatSpecial((_UF)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NEG_INF) -#define UNITY_TEST_ASSERT_FLOAT_IS_NAN(actual, line, message) UnityAssertFloatSpecial((_UF)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NAN) -#define UNITY_TEST_ASSERT_FLOAT_IS_DETERMINATE(actual, line, message) UnityAssertFloatSpecial((_UF)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_DET) -#define UNITY_TEST_ASSERT_FLOAT_IS_NOT_INF(actual, line, message) UnityAssertFloatSpecial((_UF)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NOT_INF) -#define UNITY_TEST_ASSERT_FLOAT_IS_NOT_NEG_INF(actual, line, message) UnityAssertFloatSpecial((_UF)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NOT_NEG_INF) -#define UNITY_TEST_ASSERT_FLOAT_IS_NOT_NAN(actual, line, message) UnityAssertFloatSpecial((_UF)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NOT_NAN) -#define UNITY_TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE(actual, line, message) UnityAssertFloatSpecial((_UF)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NOT_DET) -#endif - -#ifdef UNITY_EXCLUDE_DOUBLE -#define UNITY_TEST_ASSERT_DOUBLE_WITHIN(delta, expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#define UNITY_TEST_ASSERT_EQUAL_DOUBLE(expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#define UNITY_TEST_ASSERT_EQUAL_DOUBLE_ARRAY(expected, actual, num_elements, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#define UNITY_TEST_ASSERT_DOUBLE_IS_INF(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NEG_INF(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NAN(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#define UNITY_TEST_ASSERT_DOUBLE_IS_DETERMINATE(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_INF(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_NAN(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) -#else -#define UNITY_TEST_ASSERT_DOUBLE_WITHIN(delta, expected, actual, line, message) UnityAssertDoublesWithin((_UD)(delta), (_UD)(expected), (_UD)(actual), (message), (UNITY_LINE_TYPE)line) -#define UNITY_TEST_ASSERT_EQUAL_DOUBLE(expected, actual, line, message) UNITY_TEST_ASSERT_DOUBLE_WITHIN((_UD)(expected) * (_UD)UNITY_DOUBLE_PRECISION, (_UD)expected, (_UD)actual, (UNITY_LINE_TYPE)(line), message) -#define UNITY_TEST_ASSERT_EQUAL_DOUBLE_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualDoubleArray((_UD*)(expected), (_UD*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line) -#define UNITY_TEST_ASSERT_DOUBLE_IS_INF(actual, line, message) UnityAssertDoubleSpecial((_UD)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_INF) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NEG_INF(actual, line, message) UnityAssertDoubleSpecial((_UD)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NEG_INF) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NAN(actual, line, message) UnityAssertDoubleSpecial((_UD)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NAN) -#define UNITY_TEST_ASSERT_DOUBLE_IS_DETERMINATE(actual, line, message) UnityAssertDoubleSpecial((_UD)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_DET) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_INF(actual, line, message) UnityAssertDoubleSpecial((_UD)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NOT_INF) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF(actual, line, message) UnityAssertDoubleSpecial((_UD)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NOT_NEG_INF) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_NAN(actual, line, message) UnityAssertDoubleSpecial((_UD)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NOT_NAN) -#define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE(actual, line, message) UnityAssertDoubleSpecial((_UD)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NOT_DET) -#endif - -/* End of UNITY_INTERNALS_H */ -#endif From 1ab0829ab3db39600b5e0d06935f19bf4199894d Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 3 May 2018 19:31:54 +0700 Subject: [PATCH 257/389] Added doc/ALGORITHMS.md. --- doc/ALGORITHMS.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 doc/ALGORITHMS.md diff --git a/doc/ALGORITHMS.md b/doc/ALGORITHMS.md new file mode 100644 index 00000000..3e1718d3 --- /dev/null +++ b/doc/ALGORITHMS.md @@ -0,0 +1,43 @@ +# Algorithms + +XMRig uses a different way to specify algorithms, compared to other miners. + +Algorithm selection splitted to 2 parts: + + * Global base algorithm per miner or proxy instance, `algo` option. Possible values: `cryptonight`, `cryptonight-lite`, `cryptonight-heavy`. + * Algorithm variant specified separately for each pool, `variant` option. + +Possible variants for `cryptonight`: + + * `0` Original cryptonight. + * `1` cryptonight variant 1, also known as cryptonight v7 or monero7. + * `"xtl"` Stellite coin variant. + +Possible variants for `cryptonight-lite`: + + * `0` Original cryptonight-lite. + * `1` cryptonight-lite variant 1, also known as cryptonight-lite v7 or aeon7. + * `"ipbc"` IPBC coin variant. + +For `cryptonight-heavy` currently no variants. + + +### Cheatsheet + +You mine **Sumokoin** or **Haven Protocol**? +Your algorithm is `cryptonight-heavy` no variant option need. + +You mine **Aeon**, **TurtleCoin** or **IPBC**?. +Your base algorithm is `cryptonight-lite`: +Variants: + * Aeon: `-1` autodetect. `0` right now, `1` after fork. + * TurtleCoin: `1`. + * IPBC: `"ipbc"`. + +In all other cases base algorithm is `cryptonight`. + +### Mining algorithm negotiation +If your pool support [mining algorithm negotiation](https://github.com/xmrig/xmrig-proxy/issues/168) miner will choice proper variant automaticaly and if you choice wrong base algorithm you will see error message. + +Pools with mining algorithm negotiation support. + * [www.hashvault.pro](https://www.hashvault.pro/) \ No newline at end of file From 734c0dbce1637feabf677083dbcaeea215a7db09 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 3 May 2018 20:16:42 +0700 Subject: [PATCH 258/389] Use block version to detect proper XTL variant and use variant 1 by default for cryptonight. --- src/common/net/Job.cpp | 22 ++++++++++++++++++++++ src/common/net/Job.h | 6 +----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index 34aab326..81c3b8f8 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -163,6 +163,28 @@ bool Job::setTarget(const char *target) } +xmrig::Variant Job::variant() const +{ + if (m_algorithm.algo() == xmrig::CRYPTONIGHT_HEAVY) { + return xmrig::VARIANT_0; + } + + if (m_algorithm.variant() == xmrig::VARIANT_XTL && m_blob[0] < 4) { + return xmrig::VARIANT_1; + } + + if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { + if (m_algorithm.algo() == xmrig::CRYPTONIGHT) { + return xmrig::VARIANT_1; + } + + return (m_blob[0] > 6 ? xmrig::VARIANT_1 : xmrig::VARIANT_0); + } + + return m_algorithm.variant(); +} + + bool Job::fromHex(const char* in, unsigned int len, unsigned char* out) { bool error = false; diff --git a/src/common/net/Job.h b/src/common/net/Job.h index 08d5ae82..049eb7d4 100644 --- a/src/common/net/Job.h +++ b/src/common/net/Job.h @@ -43,6 +43,7 @@ public: bool setBlob(const char *blob); bool setTarget(const char *target); + xmrig::Variant variant() const; inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } @@ -64,11 +65,6 @@ public: inline void setThreadId(int threadId) { m_threadId = threadId; } inline xmrig::Algorithm &algorithm() { return m_algorithm; } - inline xmrig::Variant variant() const - { - return (m_algorithm.variant() == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? xmrig::VARIANT_1 : xmrig::VARIANT_0) : m_algorithm.variant()); - } - # ifdef XMRIG_PROXY_PROJECT inline char *rawBlob() { return m_rawBlob; } inline const char *rawTarget() const { return m_rawTarget; } From b98d44ce10613968ca240e570462d24ab668766c Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 3 May 2018 22:17:40 +0700 Subject: [PATCH 259/389] Update CHANGELOG.md. --- CHANGELOG.md | 342 ++++++++++++++++++++++++++------------------------- 1 file changed, 175 insertions(+), 167 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53e24f85..e905725e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,167 +1,175 @@ -# v2.6.0-beta3 -- [#563](https://github.com/xmrig/xmrig/issues/563) **Added [advanced threads mode](https://github.com/xmrig/xmrig/issues/563), now possible configure each thread individually.** -- [#255](https://github.com/xmrig/xmrig/issues/563) Low power mode extended to **triple**, **quard** and **penta** modes. -- [#519](https://github.com/xmrig/xmrig/issues/519) Fixed high donation levels, improved donation start time randomization. -- [#554](https://github.com/xmrig/xmrig/issues/554) Fixed regression with `print-time` option. - -# v2.6.0-beta2 -- Improved performance for `cryptonight v7` especially in double hash mode. -- [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 disabled for internal HTTP API by default, was causing issues on some systems. -- Added short aliases for algorithm names: `cn`, `cn-lite` and `cn-heavy`. -- Fixed regressions (v2.6.0-beta1 affected) - - [#494](https://github.com/xmrig/xmrig/issues/494) Command line option `--donate-level` was broken. - - [#502](https://github.com/xmrig/xmrig/issues/502) Build without libmicrohttpd was broken. - - Fixed nonce calculation for `--av 4` (software AES, double hash) was causing reduction of effective hashrate and rejected shares on nicehash. - -# v2.6.0-beta1 - - [#476](https://github.com/xmrig/xmrig/issues/476) **Added Cryptonight-Heavy support for Sumokoin ASIC resistance fork.** - - HTTP server now runs in main loop, it make possible easy extend API without worry about thread synchronization. - - Added initial graceful reload support, miner will reload configuration if config file changed, disabled by default until it will be fully implemented and tested. - - Added API endpoint `PUT /1/config` to update current config. - - Added API endpoint `GET /1/config` to get current active config. - - Added API endpoint `GET /1/threads` to get current active threads configuration. - - API endpoint `GET /` now deprecated, use `GET /1/summary` instead. - - Added `--api-no-ipv6` and similar config option to disable IPv6 support for HTTP API. - - Added `--api-no-restricted` to enable full access to api, this option has no effect if `--api-access-token` not specified. - -# v2.5.3 -- Fixed critical bug, in some cases miner was can't recovery connection and switch to failover pool, version 2.5.2 affected. If you use v2.6.0-beta3 this issue doesn't concern you. -- [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 support disabled for internal HTTP API. -- Added workaround for nicehash.com if you use `cryptonightv7..nicehash.com` option `variant=1` will be set automatically. - -# v2.5.2 -- [#448](https://github.com/xmrig/xmrig/issues/478) Fixed broken reconnect. - -# v2.5.1 -- [#454](https://github.com/xmrig/xmrig/issues/454) Fixed build with libmicrohttpd version below v0.9.35. -- [#456](https://github.com/xmrig/xmrig/issues/459) Verbose errors related to donation pool was not fully silenced. -- [#459](https://github.com/xmrig/xmrig/issues/459) Fixed regression (version 2.5.0 affected) with connection to **xmr.f2pool.com**. - -# v2.5.0 -- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on April 6.** -- Added full IPv6 support. -- Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. -- [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. -- [#428](https://github.com/xmrig/xmrig/issues/428) Fixed regression (version 2.4.5 affected) with CPU cache size detection. - -# v2.4.5 -- [#324](https://github.com/xmrig/xmrig/pull/324) Fixed build without libmicrohttpd (CMake cache issue). -- [#341](https://github.com/xmrig/xmrig/issues/341) Fixed wrong exit code and added command line option `--dry-run`. -- [#385](https://github.com/xmrig/xmrig/pull/385) Up to 20% performance increase for non-AES CPU and fixed Intel Core 2 cache detection. - -# v2.4.4 - - Added libmicrohttpd version to --version output. - - Fixed bug in singal handler, in some cases miner wasn't shutdown properly. - - Fixed recent MSVC 2017 version detection. - - [#279](https://github.com/xmrig/xmrig/pull/279) Fixed build on some macOS versions. - -# v2.4.3 - - [#94](https://github.com/xmrig/xmrig/issues/94#issuecomment-342019257) [#216](https://github.com/xmrig/xmrig/issues/216) Added **ARMv8** and **ARMv7** support. Hardware AES supported, thanks [Imran Yusuff](https://github.com/imranyusuff). - - [#157](https://github.com/xmrig/xmrig/issues/157) [#196](https://github.com/xmrig/xmrig/issues/196) Fixed Linux compile issues. - - [#184](https://github.com/xmrig/xmrig/issues/184) Fixed cache size detection for CPUs with disabled Hyper-Threading. - - [#200](https://github.com/xmrig/xmrig/issues/200) In some cases miner was doesn't write log to stdout. - -# v2.4.2 - - [#60](https://github.com/xmrig/xmrig/issues/60) Added FreeBSD support, thanks [vcambur](https://github.com/vcambur). - - [#153](https://github.com/xmrig/xmrig/issues/153) Fixed issues with dwarfpool.com. - -# v2.4.1 - - [#147](https://github.com/xmrig/xmrig/issues/147) Fixed comparability with monero-stratum. - -# v2.4.0 - - Added [HTTP API](https://github.com/xmrig/xmrig/wiki/API). - - Added comments support in config file. - - libjansson replaced to rapidjson. - - [#98](https://github.com/xmrig/xmrig/issues/98) Ignore `keepalive` option with minergate.com and nicehash.com. - - [#101](https://github.com/xmrig/xmrig/issues/101) Fixed MSVC 2017 (15.3) compile time version detection. - - [#108](https://github.com/xmrig/xmrig/issues/108) Silently ignore invalid values for `donate-level` option. - - [#111](https://github.com/xmrig/xmrig/issues/111) Fixed build without AEON support. - -# v2.3.1 -- [#68](https://github.com/xmrig/xmrig/issues/68) Fixed compatibility with Docker containers, was nothing print on console. - -# v2.3.0 -- Added `--cpu-priority` option (0 idle, 2 normal to 5 highest). -- Added `--user-agent` option, to set custom user-agent string for pool. For example `cpuminer-multi/0.1`. -- Added `--no-huge-pages` option, to disable huge pages support. -- [#62](https://github.com/xmrig/xmrig/issues/62) Don't send the login to the dev pool. -- Force reconnect if pool block miner IP address. helps switch to backup pool. -- Fixed: failed open default config file if path contains non English characters. -- Fixed: error occurred if try use unavailable stdin or stdout, regression since version 2.2.0. -- Fixed: message about huge pages support successfully enabled on Windows was not shown in release builds. - -# v2.2.1 -- Fixed [terminal issues](https://github.com/xmrig/xmrig-proxy/issues/2#issuecomment-319914085) after exit on Linux and OS X. - -# v2.2.0 -- [#46](https://github.com/xmrig/xmrig/issues/46) Restored config file support. Now possible use multiple config files and combine with command line options also added support for default config. -- Improved colors support on Windows, now used uv_tty, legacy code removed. -- QuickEdit Mode now disabled on Windows. -- Added interactive commands in console window:: **h**ashrate, **p**ause, **r**esume. -- Fixed autoconf mode for AMD FX CPUs. - -# v2.1.0 -- [#40](https://github.com/xmrig/xmrig/issues/40) -Improved miner shutdown, fixed crash on exit for Linux and OS X. -- Fixed, login request was contain malformed JSON if username or password has some special characters for example `\`. -- [#220](https://github.com/fireice-uk/xmr-stak-cpu/pull/220) Better support for Round Robin DNS, IP address now always chosen randomly instead of stuck on first one. -- Changed donation address, new [xmrig-proxy](https://github.com/xmrig/xmrig-proxy) is coming soon. - -# v2.0.2 -- Better deal with possible duplicate jobs from pool, show warning and ignore duplicates. -- For Windows builds libuv updated to version 1.13.1 and gcc to 7.1.0. - -# v2.0.1 - - [#27](https://github.com/xmrig/xmrig/issues/27) Fixed possibility crash on 32bit systems. - -# v2.0.0 - - Option `--backup-url` removed, instead now possibility specify multiple pools for example: `-o example1.com:3333 -u user1 -p password1 -k -o example2.com:5555 -u user2 -o example3.com:4444 -u user3` - - [#15](https://github.com/xmrig/xmrig/issues/15) Added option `-l, --log-file=FILE` to write log to file. - - [#15](https://github.com/xmrig/xmrig/issues/15) Added option `-S, --syslog` to use syslog for logging, Linux only. - - [#18](https://github.com/xmrig/xmrig/issues/18) Added nice messages for accepted/rejected shares with diff and network latency. - - [#20](https://github.com/xmrig/xmrig/issues/20) Fixed `--cpu-affinity` for more than 32 threads. - - Fixed Windows XP support. - - Fixed regression, option `--no-color` was not fully disable colored output. - - Show resolved pool IP address in miner output. - -# v1.0.1 -- Fix broken software AES implementation, app has crashed if CPU not support AES-NI, only version 1.0.0 affected. - -# v1.0.0 -- Miner complete rewritten in C++ with libuv. -- This version should be fully compatible (except config file) with previos versions, many new nice features will come in next versions. -- This is still beta. If you found regression, stability or perfomance issues or have an idea for new feature please fell free to open new [issue](https://github.com/xmrig/xmrig/issues/new). -- Added new option `--print-time=N`, print hashrate report every N seconds. -- New hashrate reports, by default every 60 secons. -- Added Microsoft Visual C++ 2015 and 2017 support. -- Removed dependency on libcurl. -- To compile this version from source please switch to [dev](https://github.com/xmrig/xmrig/tree/dev) branch. - -# v0.8.2 -- Fixed L2 cache size detection for AMD CPUs (Bulldozer/Piledriver/Steamroller/Excavator architecture). - -# v0.8.2 -- Fixed L2 cache size detection for AMD CPUs (Bulldozer/Piledriver/Steamroller/Excavator architecture). -- Fixed gcc 7.1 support. - -# v0.8.1 -- Added nicehash support, detects automaticaly by pool URL, for example `cryptonight.eu.nicehash.com:3355` or manually via option `--nicehash`. - -# v0.8.0 -- Added double hash mode, also known as lower power mode. `--av=2` and `--av=4`. -- Added smart automatic CPU configuration. Default threads count now depends on size of the L3 cache of CPU. -- Added CryptoNight-Lite support for AEON `-a cryptonight-lite`. -- Added `--max-cpu-usage` option for auto CPU configuration mode. -- Added `--safe` option for adjust threads and algorithm variations to current CPU. -- No more manual steps to enable huge pages on Windows. XMRig will do it automatically. -- Removed BMI2 algorithm variation. -- Removed default pool URL. - -# v0.6.0 -- Added automatic cryptonight self test. -- New software AES algorithm variation. Will be automatically selected if cpu not support AES-NI. -- Added 32 bit builds. -- Documented [algorithm variations](https://github.com/xmrig/xmrig#algorithm-variations). - -# v0.5.0 -- Initial public release. +# v2.6.1-beta + - [#168](https://github.com/xmrig/xmrig-proxy/issues/168) Added support for [mining algorithm negotiation](https://github.com/xmrig/xmrig-proxy/blob/dev/doc/STRATUM_EXT.md#1-mining-algorithm-negotiation). + - Added IPBC coin support, base algorithm `cn-lite` variant `ipbc`. + - [#581](https://github.com/xmrig/xmrig/issues/581) Added support for upcoming Stellite (XTL) fork, base algorithm `cn` variant `xtl`, variant can set now, no need do it after fork. + - Added support for **rig-id** stratum protocol extensions, compatible with xmr-stak. + - Changed behavior for option `variant=-1` for `cryptonight`, now variant is `1` by default, if you mine old coins need change `variant` to `0`. + - A lot of small fixes and better unification with proxy code. + +# v2.6.0-beta3 +- [#563](https://github.com/xmrig/xmrig/issues/563) **Added [advanced threads mode](https://github.com/xmrig/xmrig/issues/563), now possible configure each thread individually.** +- [#255](https://github.com/xmrig/xmrig/issues/563) Low power mode extended to **triple**, **quard** and **penta** modes. +- [#519](https://github.com/xmrig/xmrig/issues/519) Fixed high donation levels, improved donation start time randomization. +- [#554](https://github.com/xmrig/xmrig/issues/554) Fixed regression with `print-time` option. + +# v2.6.0-beta2 +- Improved performance for `cryptonight v7` especially in double hash mode. +- [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 disabled for internal HTTP API by default, was causing issues on some systems. +- Added short aliases for algorithm names: `cn`, `cn-lite` and `cn-heavy`. +- Fixed regressions (v2.6.0-beta1 affected) + - [#494](https://github.com/xmrig/xmrig/issues/494) Command line option `--donate-level` was broken. + - [#502](https://github.com/xmrig/xmrig/issues/502) Build without libmicrohttpd was broken. + - Fixed nonce calculation for `--av 4` (software AES, double hash) was causing reduction of effective hashrate and rejected shares on nicehash. + +# v2.6.0-beta1 + - [#476](https://github.com/xmrig/xmrig/issues/476) **Added Cryptonight-Heavy support for Sumokoin ASIC resistance fork.** + - HTTP server now runs in main loop, it make possible easy extend API without worry about thread synchronization. + - Added initial graceful reload support, miner will reload configuration if config file changed, disabled by default until it will be fully implemented and tested. + - Added API endpoint `PUT /1/config` to update current config. + - Added API endpoint `GET /1/config` to get current active config. + - Added API endpoint `GET /1/threads` to get current active threads configuration. + - API endpoint `GET /` now deprecated, use `GET /1/summary` instead. + - Added `--api-no-ipv6` and similar config option to disable IPv6 support for HTTP API. + - Added `--api-no-restricted` to enable full access to api, this option has no effect if `--api-access-token` not specified. + +# v2.5.3 +- Fixed critical bug, in some cases miner was can't recovery connection and switch to failover pool, version 2.5.2 affected. If you use v2.6.0-beta3 this issue doesn't concern you. +- [#499](https://github.com/xmrig/xmrig/issues/499) IPv6 support disabled for internal HTTP API. +- Added workaround for nicehash.com if you use `cryptonightv7..nicehash.com` option `variant=1` will be set automatically. + +# v2.5.2 +- [#448](https://github.com/xmrig/xmrig/issues/478) Fixed broken reconnect. + +# v2.5.1 +- [#454](https://github.com/xmrig/xmrig/issues/454) Fixed build with libmicrohttpd version below v0.9.35. +- [#456](https://github.com/xmrig/xmrig/issues/459) Verbose errors related to donation pool was not fully silenced. +- [#459](https://github.com/xmrig/xmrig/issues/459) Fixed regression (version 2.5.0 affected) with connection to **xmr.f2pool.com**. + +# v2.5.0 +- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on April 6.** +- Added full IPv6 support. +- Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. +- [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. +- [#428](https://github.com/xmrig/xmrig/issues/428) Fixed regression (version 2.4.5 affected) with CPU cache size detection. + +# v2.4.5 +- [#324](https://github.com/xmrig/xmrig/pull/324) Fixed build without libmicrohttpd (CMake cache issue). +- [#341](https://github.com/xmrig/xmrig/issues/341) Fixed wrong exit code and added command line option `--dry-run`. +- [#385](https://github.com/xmrig/xmrig/pull/385) Up to 20% performance increase for non-AES CPU and fixed Intel Core 2 cache detection. + +# v2.4.4 + - Added libmicrohttpd version to --version output. + - Fixed bug in singal handler, in some cases miner wasn't shutdown properly. + - Fixed recent MSVC 2017 version detection. + - [#279](https://github.com/xmrig/xmrig/pull/279) Fixed build on some macOS versions. + +# v2.4.3 + - [#94](https://github.com/xmrig/xmrig/issues/94#issuecomment-342019257) [#216](https://github.com/xmrig/xmrig/issues/216) Added **ARMv8** and **ARMv7** support. Hardware AES supported, thanks [Imran Yusuff](https://github.com/imranyusuff). + - [#157](https://github.com/xmrig/xmrig/issues/157) [#196](https://github.com/xmrig/xmrig/issues/196) Fixed Linux compile issues. + - [#184](https://github.com/xmrig/xmrig/issues/184) Fixed cache size detection for CPUs with disabled Hyper-Threading. + - [#200](https://github.com/xmrig/xmrig/issues/200) In some cases miner was doesn't write log to stdout. + +# v2.4.2 + - [#60](https://github.com/xmrig/xmrig/issues/60) Added FreeBSD support, thanks [vcambur](https://github.com/vcambur). + - [#153](https://github.com/xmrig/xmrig/issues/153) Fixed issues with dwarfpool.com. + +# v2.4.1 + - [#147](https://github.com/xmrig/xmrig/issues/147) Fixed comparability with monero-stratum. + +# v2.4.0 + - Added [HTTP API](https://github.com/xmrig/xmrig/wiki/API). + - Added comments support in config file. + - libjansson replaced to rapidjson. + - [#98](https://github.com/xmrig/xmrig/issues/98) Ignore `keepalive` option with minergate.com and nicehash.com. + - [#101](https://github.com/xmrig/xmrig/issues/101) Fixed MSVC 2017 (15.3) compile time version detection. + - [#108](https://github.com/xmrig/xmrig/issues/108) Silently ignore invalid values for `donate-level` option. + - [#111](https://github.com/xmrig/xmrig/issues/111) Fixed build without AEON support. + +# v2.3.1 +- [#68](https://github.com/xmrig/xmrig/issues/68) Fixed compatibility with Docker containers, was nothing print on console. + +# v2.3.0 +- Added `--cpu-priority` option (0 idle, 2 normal to 5 highest). +- Added `--user-agent` option, to set custom user-agent string for pool. For example `cpuminer-multi/0.1`. +- Added `--no-huge-pages` option, to disable huge pages support. +- [#62](https://github.com/xmrig/xmrig/issues/62) Don't send the login to the dev pool. +- Force reconnect if pool block miner IP address. helps switch to backup pool. +- Fixed: failed open default config file if path contains non English characters. +- Fixed: error occurred if try use unavailable stdin or stdout, regression since version 2.2.0. +- Fixed: message about huge pages support successfully enabled on Windows was not shown in release builds. + +# v2.2.1 +- Fixed [terminal issues](https://github.com/xmrig/xmrig-proxy/issues/2#issuecomment-319914085) after exit on Linux and OS X. + +# v2.2.0 +- [#46](https://github.com/xmrig/xmrig/issues/46) Restored config file support. Now possible use multiple config files and combine with command line options also added support for default config. +- Improved colors support on Windows, now used uv_tty, legacy code removed. +- QuickEdit Mode now disabled on Windows. +- Added interactive commands in console window:: **h**ashrate, **p**ause, **r**esume. +- Fixed autoconf mode for AMD FX CPUs. + +# v2.1.0 +- [#40](https://github.com/xmrig/xmrig/issues/40) +Improved miner shutdown, fixed crash on exit for Linux and OS X. +- Fixed, login request was contain malformed JSON if username or password has some special characters for example `\`. +- [#220](https://github.com/fireice-uk/xmr-stak-cpu/pull/220) Better support for Round Robin DNS, IP address now always chosen randomly instead of stuck on first one. +- Changed donation address, new [xmrig-proxy](https://github.com/xmrig/xmrig-proxy) is coming soon. + +# v2.0.2 +- Better deal with possible duplicate jobs from pool, show warning and ignore duplicates. +- For Windows builds libuv updated to version 1.13.1 and gcc to 7.1.0. + +# v2.0.1 + - [#27](https://github.com/xmrig/xmrig/issues/27) Fixed possibility crash on 32bit systems. + +# v2.0.0 + - Option `--backup-url` removed, instead now possibility specify multiple pools for example: `-o example1.com:3333 -u user1 -p password1 -k -o example2.com:5555 -u user2 -o example3.com:4444 -u user3` + - [#15](https://github.com/xmrig/xmrig/issues/15) Added option `-l, --log-file=FILE` to write log to file. + - [#15](https://github.com/xmrig/xmrig/issues/15) Added option `-S, --syslog` to use syslog for logging, Linux only. + - [#18](https://github.com/xmrig/xmrig/issues/18) Added nice messages for accepted/rejected shares with diff and network latency. + - [#20](https://github.com/xmrig/xmrig/issues/20) Fixed `--cpu-affinity` for more than 32 threads. + - Fixed Windows XP support. + - Fixed regression, option `--no-color` was not fully disable colored output. + - Show resolved pool IP address in miner output. + +# v1.0.1 +- Fix broken software AES implementation, app has crashed if CPU not support AES-NI, only version 1.0.0 affected. + +# v1.0.0 +- Miner complete rewritten in C++ with libuv. +- This version should be fully compatible (except config file) with previos versions, many new nice features will come in next versions. +- This is still beta. If you found regression, stability or perfomance issues or have an idea for new feature please fell free to open new [issue](https://github.com/xmrig/xmrig/issues/new). +- Added new option `--print-time=N`, print hashrate report every N seconds. +- New hashrate reports, by default every 60 secons. +- Added Microsoft Visual C++ 2015 and 2017 support. +- Removed dependency on libcurl. +- To compile this version from source please switch to [dev](https://github.com/xmrig/xmrig/tree/dev) branch. + +# v0.8.2 +- Fixed L2 cache size detection for AMD CPUs (Bulldozer/Piledriver/Steamroller/Excavator architecture). + +# v0.8.2 +- Fixed L2 cache size detection for AMD CPUs (Bulldozer/Piledriver/Steamroller/Excavator architecture). +- Fixed gcc 7.1 support. + +# v0.8.1 +- Added nicehash support, detects automaticaly by pool URL, for example `cryptonight.eu.nicehash.com:3355` or manually via option `--nicehash`. + +# v0.8.0 +- Added double hash mode, also known as lower power mode. `--av=2` and `--av=4`. +- Added smart automatic CPU configuration. Default threads count now depends on size of the L3 cache of CPU. +- Added CryptoNight-Lite support for AEON `-a cryptonight-lite`. +- Added `--max-cpu-usage` option for auto CPU configuration mode. +- Added `--safe` option for adjust threads and algorithm variations to current CPU. +- No more manual steps to enable huge pages on Windows. XMRig will do it automatically. +- Removed BMI2 algorithm variation. +- Removed default pool URL. + +# v0.6.0 +- Added automatic cryptonight self test. +- New software AES algorithm variation. Will be automatically selected if cpu not support AES-NI. +- Added 32 bit builds. +- Documented [algorithm variations](https://github.com/xmrig/xmrig#algorithm-variations). + +# v0.5.0 +- Initial public release. From 68e953345f818bf97e367dc6f7991d570cdae30b Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 3 May 2018 22:20:36 +0700 Subject: [PATCH 260/389] Update ALGORITHMS.md --- doc/ALGORITHMS.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/ALGORITHMS.md b/doc/ALGORITHMS.md index 3e1718d3..d9a34a16 100644 --- a/doc/ALGORITHMS.md +++ b/doc/ALGORITHMS.md @@ -27,8 +27,9 @@ For `cryptonight-heavy` currently no variants. You mine **Sumokoin** or **Haven Protocol**? Your algorithm is `cryptonight-heavy` no variant option need. -You mine **Aeon**, **TurtleCoin** or **IPBC**?. +You mine **Aeon**, **TurtleCoin** or **IPBC**? Your base algorithm is `cryptonight-lite`: + Variants: * Aeon: `-1` autodetect. `0` right now, `1` after fork. * TurtleCoin: `1`. @@ -40,4 +41,4 @@ In all other cases base algorithm is `cryptonight`. If your pool support [mining algorithm negotiation](https://github.com/xmrig/xmrig-proxy/issues/168) miner will choice proper variant automaticaly and if you choice wrong base algorithm you will see error message. Pools with mining algorithm negotiation support. - * [www.hashvault.pro](https://www.hashvault.pro/) \ No newline at end of file + * [www.hashvault.pro](https://www.hashvault.pro/) From dfe20e116b0e136f079b2da74d987467e3c04c02 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 4 May 2018 01:01:05 +0700 Subject: [PATCH 261/389] Fix color issues. --- src/common/log/Log.h | 20 ++++++++++---------- src/workers/Workers.cpp | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/common/log/Log.h b/src/common/log/Log.h index 463c2248..31600dc6 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -80,16 +80,16 @@ private: }; -#define RED_BOLD(x) "\e[1;31m" x "\e[0m" -#define RED(x) "\e[0;31m" x "\e[0m" -#define GREEN_BOLD(x) "\e[1;32m" x "\e[0m" -#define GREEN(x) "\e[0;32m" x "\e[0m" -#define MAGENTA_BOLD(x) "\e[1;35m" x "\e[0m" -#define MAGENTA(x) "\e[0;35m" x "\e[0m" -#define CYAN_BOLD(x) "\e[1;36m" x "\e[0m" -#define CYAN(x) "\e[0;36m" x "\e[0m" -#define WHITE_BOLD(x) "\e[1;37m" x "\e[0m" -#define WHITE(x) "\e[0;37m" x "\e[0m" +#define RED_BOLD(x) "\x1B[1;31m" x "\x1B[0m" +#define RED(x) "\x1B[0;31m" x "\x1B[0m" +#define GREEN_BOLD(x) "\x1B[1;32m" x "\x1B[0m" +#define GREEN(x) "\x1B[0;32m" x "\x1B[0m" +#define MAGENTA_BOLD(x) "\x1B[1;35m" x "\x1B[0m" +#define MAGENTA(x) "\x1B[0;35m" x "\x1B[0m" +#define CYAN_BOLD(x) "\x1B[1;36m" x "\x1B[0m" +#define CYAN(x) "\x1B[0;36m" x "\x1B[0m" +#define WHITE_BOLD(x) "\x1B[1;37m" x "\x1B[0m" +#define WHITE(x) "\x1B[0;37m" x "\x1B[0m" #define LOG_ERR(x, ...) Log::i()->message(Log::ERR, x, ##__VA_ARGS__) diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 497e3060..8991966e 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -301,9 +301,9 @@ void Workers::start(IWorker *worker) const size_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo) / 1048576; if (m_status.colors) { - LOG_INFO(GREEN_BOLD("READY (CPU)") " threads " CYAN_BOLD("%zu(%zu)") " huge pages %s%zu/%zu %1.0f%%\e[0m memory " CYAN_BOLD("%zu.0 MB") "", + LOG_INFO(GREEN_BOLD("READY (CPU)") " threads " CYAN_BOLD("%zu(%zu)") " huge pages %s%zu/%zu %1.0f%%\x1B[0m memory " CYAN_BOLD("%zu.0 MB") "", m_status.threads, m_status.ways, - (m_status.hugePages == m_status.pages ? "\e[1;32m" : (m_status.hugePages == 0 ? "\e[1;31m" : "\e[1;33m")), + (m_status.hugePages == m_status.pages ? "\x1B[1;32m" : (m_status.hugePages == 0 ? "\x1B[1;31m" : "\x1B[1;33m")), m_status.hugePages, m_status.pages, percent, memory); } else { From 298cf37121e108ca9198be85ed21f8797e22350f Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 4 May 2018 03:19:42 +0700 Subject: [PATCH 262/389] Force variant 1 if no algorithm specified. --- src/common/net/Pool.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 8c0de46a..06622d28 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -221,6 +221,12 @@ void Pool::adjust(xmrig::Algo algorithm) if (!m_algorithm.isValid()) { m_algorithm.setAlgo(algorithm); + + if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { + if (algorithm == xmrig::CRYPTONIGHT) { + m_algorithm.setVariant(xmrig::VARIANT_1); + } + } } if (strstr(m_host.data(), ".nicehash.com")) { From 1f1bdcde51412d44883cafb996dffdd6af8fd5a4 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 4 May 2018 03:31:17 +0700 Subject: [PATCH 263/389] Fix av option in generated config. --- src/config.json | 62 ++++++++++++++++++++++++--------------------- src/core/Config.cpp | 6 ++--- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/config.json b/src/config.json index 3013ba62..b2dad4c9 100644 --- a/src/config.json +++ b/src/config.json @@ -1,34 +1,38 @@ { - "algo": "cryptonight", // cryptonight (default) or cryptonight-lite - "av": 0, // algorithm variation, 0 auto select - "background": false, // true to run the miner in the background - "colors": true, // false to disable colored output - "cpu-affinity": null, // set process affinity to CPU core(s), mask "0x3" for cores 0 and 1 - "cpu-priority": null, // set process priority (0 idle, 2 normal to 5 highest) - "donate-level": 5, // donate level, mininum 1% - "log-file": null, // log all output to a file, example: "c:/some/path/xmrig.log" - "max-cpu-usage": 75, // maximum CPU usage for automatic mode, usually limiting factor is CPU cache not this option. - "print-time": 60, // print hashrate report every N seconds - "retries": 5, // number of times to retry before switch to backup server - "retry-pause": 5, // time to pause between retries - "safe": false, // true to safe adjust threads and av settings for current CPU - "syslog": false, // use system log for output messages - "threads": null, // number of miner threads - "pools": [ - { - "url": "failover.xmrig.com:443", // URL of mining server - "user": "YOUR_WALLET", // username for mining server - "pass": "x", // password for mining server - "keepalive": true, // send keepalived for prevent timeout (need pool support) - "nicehash": false, // enable nicehash/xmrig-proxy support - "variant": -1 // algorithm PoW variant - } - ], + "algo": "cryptonight", "api": { - "port": 0, // port for the miner API https://github.com/xmrig/xmrig/wiki/API - "access-token": null, // access token for API - "worker-id": null, // custom worker-id for API + "port": 0, + "access-token": null, + "worker-id": null, "ipv6": false, "restricted": true - } + }, + "av": 0, + "background": false, + "colors": true, + "cpu-affinity": null, + "cpu-priority": null, + "donate-level": 5, + "huge-pages": true, + "hw-aes": null, + "log-file": null, + "max-cpu-usage": 75, + "pools": [ + { + "url": "proxy.fee.xmrig.com:9999", + "user": "YOUR_WALLET", + "pass": "x", + "rig-id": null, + "nicehash": false, + "keepalive": false, + "variant": 1 + } + ], + "print-time": 60, + "retries": 5, + "retry-pause": 5, + "safe": false, + "threads": null, + "user-agent": null, + "watch": false } \ No newline at end of file diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 0c4ce779..4c283164 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -162,10 +162,10 @@ bool xmrig::Config::finalize() return true; } - m_algoVariant = getAlgoVariant(); + const AlgoVariant av = getAlgoVariant(); m_threads.mode = m_threads.count ? Simple : Automatic; - const size_t size = CpuThread::multiway(m_algoVariant) * cn_select_memory(m_algorithm.algo()) / 1024; + const size_t size = CpuThread::multiway(av) * cn_select_memory(m_algorithm.algo()) / 1024; if (!m_threads.count) { m_threads.count = Cpu::optimalThreadsCount(size, m_maxCpuUsage); @@ -178,7 +178,7 @@ bool xmrig::Config::finalize() } for (size_t i = 0; i < m_threads.count; ++i) { - m_threads.list.push_back(CpuThread::createFromAV(i, m_algorithm.algo(), m_algoVariant, m_threads.mask, m_priority)); + m_threads.list.push_back(CpuThread::createFromAV(i, m_algorithm.algo(), av, m_threads.mask, m_priority)); } return true; From 8530e6c4a54abf2a4787a3457c23c1bbd0f2fe38 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 5 May 2018 14:22:32 +0700 Subject: [PATCH 264/389] v2.6.1 --- CHANGELOG.md | 2 +- src/version.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e905725e..0c09ddc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# v2.6.1-beta +# v2.6.1 - [#168](https://github.com/xmrig/xmrig-proxy/issues/168) Added support for [mining algorithm negotiation](https://github.com/xmrig/xmrig-proxy/blob/dev/doc/STRATUM_EXT.md#1-mining-algorithm-negotiation). - Added IPBC coin support, base algorithm `cn-lite` variant `ipbc`. - [#581](https://github.com/xmrig/xmrig/issues/581) Added support for upcoming Stellite (XTL) fork, base algorithm `cn` variant `xtl`, variant can set now, no need do it after fork. diff --git a/src/version.h b/src/version.h index b0e4d2a5..2263de9d 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.6.1-dev" +#define APP_VERSION "2.6.1" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" From 719b1cb90fd8f4207f91a38915f8b121f37c4526 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 5 May 2018 15:36:21 +0700 Subject: [PATCH 265/389] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3d438c5a..d69e5ec5 100644 --- a/README.md +++ b/README.md @@ -124,10 +124,10 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -6b32fefb356b27caa2180be17755d09639f0654096a1a0c61e8f10f4f2ac1626 xmrig-2.6.0-beta3-xenial-amd64.tar.gz/xmrig-2.6.0-beta3/xmrig -c8b40f4d7aac9d0cc7c3d02cadeac91b0bdb034aae2fb6b415d503272eabb987 xmrig-2.6.0-beta3-gcc-win32.zip/xmrig.exe -32cf7958f97a23296186e38a4addb9cf234adcbd9c7914b15e4209a87fe272e7 xmrig-2.6.0-beta3-gcc-win64.zip/xmrig.exe -4f176ee4c4be52701edc0d573cf9647c4aacbb4ff14975b80b98e6fe63068542 xmrig-2.6.0-beta3-msvc-win64.zip/xmrig.exe +f8e1957e8bfd7f281a76d1e42694049c67f39dea90ac36e9d589c14cdf8924bc xmrig-2.6.1-xenial-amd64.tar.gz/xmrig-2.6.1/xmrig +472c7aaf5aacc1212bfd3f2f96daca4f42d64e2d0db0872891328e7d8503d0c8 xmrig-2.6.1-gcc-win32.zip/xmrig.exe +d53154cef24c884b2be539ac13bfb6e7dba6bbc53b62e91f2877637d43fa4b15 xmrig-2.6.1-gcc-win64.zip/xmrig.exe +a253381b617463e6e1193d49b8afbf720a1c376621da7429d97f192668cd59ad xmrig-2.6.1-msvc-win64.zip/xmrig.exe ``` ## Contacts From a00024cf51dc799675a8813a0414480214f9baf9 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 5 May 2018 22:44:20 +0700 Subject: [PATCH 266/389] Fixed ARMv8 build. --- src/crypto/CryptoNight_arm.h | 78 ++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 746de79c..284b1f5f 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -27,6 +27,7 @@ #define __CRYPTONIGHT_ARM_H__ +#include "common/crypto/keccak.h" #include "common/utils/mm_malloc.h" #include "crypto/CryptoNight.h" #include "crypto/CryptoNight_constants.h" @@ -36,7 +37,6 @@ extern "C" { -#include "crypto/c_keccak.h" #include "crypto/c_groestl.h" #include "crypto/c_blake256.h" #include "crypto/c_jh.h" @@ -380,6 +380,7 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } +template static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) { mem_out[0] = EXTRACT64(tmp); @@ -388,7 +389,7 @@ static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) uint8_t x = vh >> 24; static const uint16_t table = 0x7531; - const uint8_t index = (((x >> 3) & 6) | (x & 1)) << 1; + const uint8_t index = (((x >> SHIFT) & 6) | (x & 1)) << 1; vh ^= ((table >> index) & 0x3) << 28; mem_out[1] = vh; @@ -407,7 +408,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si return; } - keccak(input, (int) size, ctx[0]->state, 200); + xmrig::keccak(input, size, ctx[0]->state); VARIANT1_INIT(0); @@ -434,7 +435,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } if (VARIANT > 0) { - cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } else { _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } @@ -450,13 +451,22 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si al0 += hi; ah0 += lo; - VARIANT1_2(ah0, 0); ((uint64_t*)&l0[idx0 & MASK])[0] = al0; - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; - VARIANT1_2(ah0, 0); - ah0 ^= ch; + if (VARIANT > 0) { + if (VARIANT == xmrig::VARIANT_IPBC) { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; + } + else { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; + } + } + else { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + } + al0 ^= cl; + ah0 ^= ch; idx0 = al0; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { @@ -471,7 +481,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si cn_implode_scratchpad((__m128i*) ctx[0]->memory, (__m128i*) ctx[0]->state); - keccakf(h0, 24); + xmrig::keccakf(h0, 24); extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); } @@ -488,8 +498,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si return; } - keccak(input, (int) size, ctx[0]->state, 200); - keccak(input + size, (int) size, ctx[1]->state, 200); + xmrig::keccak(input, size, ctx[0]->state); + xmrig::keccak(input + size, size, ctx[1]->state); VARIANT1_INIT(0); VARIANT1_INIT(1); @@ -528,8 +538,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si } if (VARIANT > 0) { - cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); - cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); + cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); } else { _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); @@ -549,13 +559,22 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si al0 += hi; ah0 += lo; - VARIANT1_2(ah0, 0); - ((uint64_t*) &l0[idx0 & MASK])[0] = al0; - ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; - VARIANT1_2(ah0, 0); + ((uint64_t*)&l0[idx0 & MASK])[0] = al0; + + if (VARIANT > 0) { + if (VARIANT == xmrig::VARIANT_IPBC) { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; + } + else { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; + } + } + else { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + } - ah0 ^= ch; al0 ^= cl; + ah0 ^= ch; idx0 = al0; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { @@ -574,13 +593,22 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si al1 += hi; ah1 += lo; - VARIANT1_2(ah1, 1); - ((uint64_t*) &l1[idx1 & MASK])[0] = al1; - ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; - VARIANT1_2(ah1, 1); + ((uint64_t*)&l1[idx1 & MASK])[0] = al1; + + if (VARIANT > 0) { + if (VARIANT == xmrig::VARIANT_IPBC) { + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; + } + else { + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1; + } + } + else { + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1; + } - ah1 ^= ch; al1 ^= cl; + ah1 ^= ch; idx1 = al1; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { @@ -596,8 +624,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); - keccakf(h0, 24); - keccakf(h1, 24); + xmrig::keccakf(h0, 24); + xmrig::keccakf(h1, 24); extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); extra_hashes[ctx[1]->state[0] & 3](ctx[1]->state, 200, output + 32); From a1c5afa38313ea1d04ede8defac4d24098e5ed96 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 7 May 2018 00:56:39 +0700 Subject: [PATCH 267/389] Fix Termux build. --- src/common/Platform_unix.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/common/Platform_unix.cpp b/src/common/Platform_unix.cpp index 63cd1c8b..97b32ee8 100644 --- a/src/common/Platform_unix.cpp +++ b/src/common/Platform_unix.cpp @@ -29,11 +29,13 @@ #endif +#include #include #include #include #include #include +#include #include From c89889cc5782e38d79b4c381878f75fd590bce5a Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 7 May 2018 02:09:25 +0700 Subject: [PATCH 268/389] #607 Fixed donation bug. --- src/common/net/Pool.cpp | 4 ++++ src/net/strategies/DonateStrategy.cpp | 2 +- src/version.h | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 06622d28..ca73b315 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -91,6 +91,10 @@ Pool::Pool(const char *host, uint16_t port, const char *user, const char *passwo bool Pool::isCompatible(const xmrig::Algorithm &algorithm) const { + if (m_algorithms.empty()) { + return true; + } + for (const auto &a : m_algorithms) { if (algorithm == a) { return true; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index b4b41938..7de776ef 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -68,7 +68,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, xmrig::Algo algo, IS } for (Pool &pool : m_pools) { - pool.algorithm().setAlgo(algo); + pool.adjust(algo); } if (m_pools.size() > 1) { diff --git a/src/version.h b/src/version.h index 2263de9d..f9f02dcc 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.6.1" +#define APP_VERSION "2.6.2" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 6 -#define APP_VER_PATCH 1 +#define APP_VER_PATCH 2 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From bee1285e3a23d9b23ab5d8a773004b1d5f8b8100 Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 7 May 2018 02:17:20 +0700 Subject: [PATCH 269/389] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c09ddc5..c99e646f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v2.6.2 + - [#607](https://github.com/xmrig/xmrig/issues/607) Fixed donation bug. + - [#610](https://github.com/xmrig/xmrig/issues/610) Fixed ARM build. + # v2.6.1 - [#168](https://github.com/xmrig/xmrig-proxy/issues/168) Added support for [mining algorithm negotiation](https://github.com/xmrig/xmrig-proxy/blob/dev/doc/STRATUM_EXT.md#1-mining-algorithm-negotiation). - Added IPBC coin support, base algorithm `cn-lite` variant `ipbc`. From b383a382ebe6f7f3fc519b87f6234480241dc0ee Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 7 May 2018 02:37:23 +0700 Subject: [PATCH 270/389] Update README.md --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d69e5ec5..8f1540a4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # XMRig -:warning: **You must update miners to version 2.5 before April 6 due [Monero PoW change](https://getmonero.org/2018/02/11/PoW-change-and-key-reuse.html).** - [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub release](https://img.shields.io/github/release/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub Release Date](https://img.shields.io/github/release-date-pre/xmrig/xmrig.svg)](https://github.com/xmrig/xmrig/releases) @@ -124,10 +122,10 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -f8e1957e8bfd7f281a76d1e42694049c67f39dea90ac36e9d589c14cdf8924bc xmrig-2.6.1-xenial-amd64.tar.gz/xmrig-2.6.1/xmrig -472c7aaf5aacc1212bfd3f2f96daca4f42d64e2d0db0872891328e7d8503d0c8 xmrig-2.6.1-gcc-win32.zip/xmrig.exe -d53154cef24c884b2be539ac13bfb6e7dba6bbc53b62e91f2877637d43fa4b15 xmrig-2.6.1-gcc-win64.zip/xmrig.exe -a253381b617463e6e1193d49b8afbf720a1c376621da7429d97f192668cd59ad xmrig-2.6.1-msvc-win64.zip/xmrig.exe +ca60d04204aa3195e8cd72887c7deced1a7c664e538256f138b5698d381ceb00 xmrig-2.6.2-xenial-amd64.tar.gz/xmrig-2.6.2/xmrig +49dc64eeecdbfffb452ad3cd7a0118d8640208b0b898b2901464d0be6183c169 xmrig-2.6.2-gcc-win32.zip/xmrig.exe +56141544ac6d03565909b3043fab104bf40cadb32d53a12d821e1328bc50f087 xmrig-2.6.2-gcc-win64.zip/xmrig.exe +24661a8807f4b991c79e587e846aaea589720ed84d79afb41d14709a6fb908ce xmrig-2.6.2-msvc-win64.zip/xmrig.exe ``` ## Contacts From 8d61df7cc948c833f5c746f743a26d6177aa145a Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 7 May 2018 14:27:48 +0700 Subject: [PATCH 271/389] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d69e5ec5..250ca54b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # XMRig -:warning: **You must update miners to version 2.5 before April 6 due [Monero PoW change](https://getmonero.org/2018/02/11/PoW-change-and-key-reuse.html).** +:warning: **If you mine Monero, Aeon, Sumokoin, Turtlecoin, Stellite, GRAFT, Haven Protocol, IPBC, [PLEASE READ](https://github.com/xmrig/xmrig/issues/482)!** :warning: [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub release](https://img.shields.io/github/release/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) From d7d3fec0582c5b79f4af6902ccd1b3b8a9b4eec6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 8 May 2018 02:39:11 +0700 Subject: [PATCH 272/389] #614 Fix display issue with huge pages percentage when colors disabled. --- src/workers/Workers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 8991966e..f6ec04b7 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -307,7 +307,7 @@ void Workers::start(IWorker *worker) m_status.hugePages, m_status.pages, percent, memory); } else { - LOG_INFO("READY (CPU) threads %zu(%zu) huge pages %zu/%zu %f%% memory %zu.0 MB", + LOG_INFO("READY (CPU) threads %zu(%zu) huge pages %zu/%zu %1.0f%% memory %zu.0 MB", m_status.threads, m_status.ways, m_status.hugePages, m_status.pages, percent, memory); } } From 6a4f817693d51b38b70b8a7290d005f5c1ae900b Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 8 May 2018 06:19:10 +0700 Subject: [PATCH 273/389] #615 Fixed build without libcpuid. --- src/Cpu_stub.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Cpu_stub.cpp b/src/Cpu_stub.cpp index 87d9de75..8b27ad65 100644 --- a/src/Cpu_stub.cpp +++ b/src/Cpu_stub.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -110,12 +110,12 @@ int Cpu::m_l2_cache = 0; int Cpu::m_l3_cache = 0; int Cpu::m_sockets = 1; int Cpu::m_totalCores = 0; -int Cpu::m_totalThreads = 0; +size_t Cpu::m_totalThreads = 0; -int Cpu::optimalThreadsCount(int algo, bool doubleHash, int maxCpuUsage) +size_t Cpu::optimalThreadsCount(size_t size, int maxCpuUsage) { - int count = m_totalThreads / 2; + const size_t count = m_totalThreads / 2; return count < 1 ? 1 : count; } From 19f409a85bec43eb84fc2b71a5b52670c38ed306 Mon Sep 17 00:00:00 2001 From: WHR Date: Tue, 15 May 2018 03:12:12 +0800 Subject: [PATCH 274/389] Fix FileLog error 'invalid seek' on non-seekable files --- src/common/log/FileLog.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/log/FileLog.cpp b/src/common/log/FileLog.cpp index c8eaf5f7..cc7188cb 100644 --- a/src/common/log/FileLog.cpp +++ b/src/common/log/FileLog.cpp @@ -93,5 +93,5 @@ void FileLog::write(char *data, size_t size) uv_fs_t *req = new uv_fs_t; req->data = buf.base; - uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, 0, FileLog::onWrite); + uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, -1, FileLog::onWrite); } From 0086020b5c6c0199e88edd06df42af5743bbff20 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 19 May 2018 11:48:15 +0700 Subject: [PATCH 275/389] Optimize logs architecture. --- src/common/log/ConsoleLog.cpp | 35 +++++------------------------------ src/common/log/ConsoleLog.h | 2 +- src/common/log/FileLog.cpp | 8 ++++---- src/common/log/FileLog.h | 6 +++--- src/common/log/Log.cpp | 31 ++++++++++++++++++++++++++++++- src/common/log/Log.h | 29 ++++++++++++----------------- src/common/log/SysLog.cpp | 6 +++--- src/common/log/SysLog.h | 6 +++--- src/interfaces/ILogBackend.h | 16 ++++++++++++---- 9 files changed, 73 insertions(+), 66 deletions(-) diff --git a/src/common/log/ConsoleLog.cpp b/src/common/log/ConsoleLog.cpp index b7d187d1..a1b6648d 100644 --- a/src/common/log/ConsoleLog.cpp +++ b/src/common/log/ConsoleLog.cpp @@ -66,7 +66,7 @@ ConsoleLog::ConsoleLog(xmrig::Controller *controller) : } -void ConsoleLog::message(int level, const char* fmt, va_list args) +void ConsoleLog::message(Level level, const char* fmt, va_list args) { time_t now = time(nullptr); tm stime; @@ -77,43 +77,18 @@ void ConsoleLog::message(int level, const char* fmt, va_list args) localtime_r(&now, &stime); # endif - const char* color = nullptr; - const bool colors = m_controller->config()->isColors(); + const bool isColors = m_controller->config()->isColors(); - if (colors) { - switch (level) { - case Log::ERR: - color = Log::kCL_RED; - break; - - case Log::WARNING: - color = Log::kCL_YELLOW; - break; - - case Log::NOTICE: - color = Log::kCL_WHITE; - break; - - case Log::DEBUG: - color = Log::kCL_GRAY; - break; - - default: - color = ""; - break; - } - } - - snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s\n", + snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s", stime.tm_year + 1900, stime.tm_mon + 1, stime.tm_mday, stime.tm_hour, stime.tm_min, stime.tm_sec, - colors ? color : "", + Log::colorByLevel(level, isColors), fmt, - colors ? Log::kCL_N : "" + Log::endl(isColors) ); print(args); diff --git a/src/common/log/ConsoleLog.h b/src/common/log/ConsoleLog.h index 6649be84..a3b0a33a 100644 --- a/src/common/log/ConsoleLog.h +++ b/src/common/log/ConsoleLog.h @@ -41,7 +41,7 @@ class ConsoleLog : public ILogBackend public: ConsoleLog(xmrig::Controller *controller); - void message(int level, const char *fmt, va_list args) override; + void message(Level level, const char *fmt, va_list args) override; void text(const char *fmt, va_list args) override; private: diff --git a/src/common/log/FileLog.cpp b/src/common/log/FileLog.cpp index cc7188cb..367f0f68 100644 --- a/src/common/log/FileLog.cpp +++ b/src/common/log/FileLog.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,7 +40,7 @@ FileLog::FileLog(const char *fileName) } -void FileLog::message(int level, const char* fmt, va_list args) +void FileLog::message(Level level, const char* fmt, va_list args) { if (m_file < 0) { return; @@ -73,7 +73,7 @@ void FileLog::message(int level, const char* fmt, va_list args) void FileLog::text(const char* fmt, va_list args) { - message(0, fmt, args); + message(INFO, fmt, args); } diff --git a/src/common/log/FileLog.h b/src/common/log/FileLog.h index 2b3ca5d4..545056bb 100644 --- a/src/common/log/FileLog.h +++ b/src/common/log/FileLog.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,7 +36,7 @@ class FileLog : public ILogBackend public: FileLog(const char *fileName); - void message(int level, const char* fmt, va_list args) override; + void message(Level level, const char* fmt, va_list args) override; void text(const char* fmt, va_list args) override; private: diff --git a/src/common/log/Log.cpp b/src/common/log/Log.cpp index 03237f03..6144e4f1 100644 --- a/src/common/log/Log.cpp +++ b/src/common/log/Log.cpp @@ -36,7 +36,16 @@ Log *Log::m_self = nullptr; -void Log::message(Log::Level level, const char* fmt, ...) +static const char *colors[5] = { + Log::kCL_RED, /* ERR */ + Log::kCL_YELLOW, /* WARNING */ + Log::kCL_WHITE, /* NOTICE */ + "", /* INFO */ + Log::kCL_GRAY /* DEBUG */ +}; + + +void Log::message(ILogBackend::Level level, const char* fmt, ...) { uv_mutex_lock(&m_mutex); @@ -76,6 +85,26 @@ void Log::text(const char* fmt, ...) } +const char *Log::colorByLevel(ILogBackend::Level level, bool isColors) +{ + if (!isColors) { + return ""; + } + + return colors[level]; +} + + +const char *Log::endl(bool isColors) +{ +# ifdef _WIN32 + return isColors ? "\x1B[0m\r\n" : "\r\n"; +# else + return isColors ? "\x1B[0m\n" : "\n"; +# endif +} + + Log::~Log() { for (auto backend : m_backends) { diff --git a/src/common/log/Log.h b/src/common/log/Log.h index 31600dc6..6d4959ef 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -30,20 +30,12 @@ #include -class ILogBackend; +#include "interfaces/ILogBackend.h" class Log { public: - enum Level { - ERR, - WARNING, - NOTICE, - INFO, - DEBUG - }; - constexpr static const char* kCL_N = "\x1B[0m"; constexpr static const char* kCL_RED = "\x1B[31m"; constexpr static const char* kCL_YELLOW = "\x1B[33m"; @@ -60,9 +52,12 @@ public: static inline void init() { if (!m_self) { new Log(); } } static inline void release() { assert(m_self != nullptr); delete m_self; } - void message(Level level, const char* fmt, ...); + void message(ILogBackend::Level level, const char* fmt, ...); void text(const char* fmt, ...); + static const char *colorByLevel(ILogBackend::Level level, bool isColors = true); + static const char *endl(bool isColors = true); + private: inline Log() { assert(m_self == nullptr); @@ -92,20 +87,20 @@ private: #define WHITE(x) "\x1B[0;37m" x "\x1B[0m" -#define LOG_ERR(x, ...) Log::i()->message(Log::ERR, x, ##__VA_ARGS__) -#define LOG_WARN(x, ...) Log::i()->message(Log::WARNING, x, ##__VA_ARGS__) -#define LOG_NOTICE(x, ...) Log::i()->message(Log::NOTICE, x, ##__VA_ARGS__) -#define LOG_INFO(x, ...) Log::i()->message(Log::INFO, x, ##__VA_ARGS__) +#define LOG_ERR(x, ...) Log::i()->message(ILogBackend::ERR, x, ##__VA_ARGS__) +#define LOG_WARN(x, ...) Log::i()->message(ILogBackend::WARNING, x, ##__VA_ARGS__) +#define LOG_NOTICE(x, ...) Log::i()->message(ILogBackend::NOTICE, x, ##__VA_ARGS__) +#define LOG_INFO(x, ...) Log::i()->message(ILogBackend::INFO, x, ##__VA_ARGS__) #ifdef APP_DEBUG -# define LOG_DEBUG(x, ...) Log::i()->message(Log::DEBUG, x, ##__VA_ARGS__) +# define LOG_DEBUG(x, ...) Log::i()->message(ILogBackend::DEBUG, x, ##__VA_ARGS__) #else # define LOG_DEBUG(x, ...) #endif #if defined(APP_DEBUG) || defined(APP_DEVEL) -# define LOG_DEBUG_ERR(x, ...) Log::i()->message(Log::ERR, x, ##__VA_ARGS__) -# define LOG_DEBUG_WARN(x, ...) Log::i()->message(Log::WARNING, x, ##__VA_ARGS__) +# define LOG_DEBUG_ERR(x, ...) Log::i()->message(ILogBackend::ERR, x, ##__VA_ARGS__) +# define LOG_DEBUG_WARN(x, ...) Log::i()->message(ILogBackend::WARNING, x, ##__VA_ARGS__) #else # define LOG_DEBUG_ERR(x, ...) # define LOG_DEBUG_WARN(x, ...) diff --git a/src/common/log/SysLog.cpp b/src/common/log/SysLog.cpp index 70879c33..5eb4dcd4 100644 --- a/src/common/log/SysLog.cpp +++ b/src/common/log/SysLog.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ SysLog::SysLog() } -void SysLog::message(int level, const char *fmt, va_list args) +void SysLog::message(Level level, const char *fmt, va_list args) { vsyslog(level, fmt, args); } diff --git a/src/common/log/SysLog.h b/src/common/log/SysLog.h index 38de1a6a..1e49a48b 100644 --- a/src/common/log/SysLog.h +++ b/src/common/log/SysLog.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +33,7 @@ class SysLog : public ILogBackend public: SysLog(); - void message(int level, const char *fmt, va_list args) override; + void message(Level level, const char *fmt, va_list args) override; void text(const char *fmt, va_list args) override; }; diff --git a/src/interfaces/ILogBackend.h b/src/interfaces/ILogBackend.h index 458b504c..b6db72a8 100644 --- a/src/interfaces/ILogBackend.h +++ b/src/interfaces/ILogBackend.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,10 +31,18 @@ class ILogBackend { public: + enum Level { + ERR, + WARNING, + NOTICE, + INFO, + DEBUG + }; + virtual ~ILogBackend() {} - virtual void message(int level, const char* fmt, va_list args) = 0; - virtual void text(const char* fmt, va_list args) = 0; + virtual void message(Level level, const char* fmt, va_list args) = 0; + virtual void text(const char* fmt, va_list args) = 0; }; From 5019493332ae66adbc616f99fc85ef32938aa1e2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 19 May 2018 13:06:49 +0700 Subject: [PATCH 276/389] Correctly reset colors in FileLog. --- src/common/log/ConsoleLog.cpp | 2 +- src/common/log/ConsoleLog.h | 2 +- src/common/log/FileLog.cpp | 34 +++++++++++++++++++++------------- src/common/log/FileLog.h | 9 ++++++++- src/common/log/Log.cpp | 14 +++++++++----- src/common/log/Log.h | 11 ----------- src/common/log/SysLog.cpp | 4 ++-- src/core/Controller.cpp | 2 +- src/interfaces/ILogBackend.h | 2 ++ 9 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/common/log/ConsoleLog.cpp b/src/common/log/ConsoleLog.cpp index a1b6648d..6cf61980 100644 --- a/src/common/log/ConsoleLog.cpp +++ b/src/common/log/ConsoleLog.cpp @@ -97,7 +97,7 @@ void ConsoleLog::message(Level level, const char* fmt, va_list args) void ConsoleLog::text(const char* fmt, va_list args) { - snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s\n", fmt, m_controller->config()->isColors() ? Log::kCL_N : ""); + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(m_controller->config()->isColors())); print(args); } diff --git a/src/common/log/ConsoleLog.h b/src/common/log/ConsoleLog.h index a3b0a33a..81813c81 100644 --- a/src/common/log/ConsoleLog.h +++ b/src/common/log/ConsoleLog.h @@ -48,7 +48,7 @@ private: bool isWritable() const; void print(va_list args); - char m_buf[512]; + char m_buf[kBufferSize]; char m_fmt[256]; uv_buf_t m_uvBuf; uv_stream_t *m_stream; diff --git a/src/common/log/FileLog.cpp b/src/common/log/FileLog.cpp index 367f0f68..9134c7c7 100644 --- a/src/common/log/FileLog.cpp +++ b/src/common/log/FileLog.cpp @@ -30,9 +30,13 @@ #include "common/log/FileLog.h" +#include "common/log/Log.h" +#include "core/Config.h" +#include "core/Controller.h" -FileLog::FileLog(const char *fileName) +FileLog::FileLog(xmrig::Controller *controller, const char *fileName) : + m_controller(controller) { uv_fs_t req; m_file = uv_fs_open(uv_default_loop(), &req, fileName, O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr); @@ -55,19 +59,24 @@ void FileLog::message(Level level, const char* fmt, va_list args) localtime_r(&now, &stime); # endif - char *buf = new char[512]; - int size = snprintf(buf, 23, "[%d-%02d-%02d %02d:%02d:%02d] ", - stime.tm_year + 1900, - stime.tm_mon + 1, - stime.tm_mday, - stime.tm_hour, - stime.tm_min, - stime.tm_sec); + const bool isColors = m_controller->config()->isColors(); - size = vsnprintf(buf + size, 512 - size - 1, fmt, args) + size; - buf[size] = '\n'; + snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s", + stime.tm_year + 1900, + stime.tm_mon + 1, + stime.tm_mday, + stime.tm_hour, + stime.tm_min, + stime.tm_sec, + Log::colorByLevel(level, isColors), + fmt, + Log::endl(isColors) + ); - write(buf, size + 1); + char *buf = new char[kBufferSize]; + const int size = vsnprintf(buf, kBufferSize - 1, m_fmt, args); + + write(buf, size); } @@ -77,7 +86,6 @@ void FileLog::text(const char* fmt, va_list args) } - void FileLog::onWrite(uv_fs_t *req) { delete [] static_cast(req->data); diff --git a/src/common/log/FileLog.h b/src/common/log/FileLog.h index 545056bb..2c25f093 100644 --- a/src/common/log/FileLog.h +++ b/src/common/log/FileLog.h @@ -31,10 +31,15 @@ #include "interfaces/ILogBackend.h" +namespace xmrig { + class Controller; +} + + class FileLog : public ILogBackend { public: - FileLog(const char *fileName); + FileLog(xmrig::Controller *controller, const char *fileName); void message(Level level, const char* fmt, va_list args) override; void text(const char* fmt, va_list args) override; @@ -44,7 +49,9 @@ private: void write(char *data, size_t size); + char m_fmt[256]; int m_file; + xmrig::Controller *m_controller; }; #endif /* __FILELOG_H__ */ diff --git a/src/common/log/Log.cpp b/src/common/log/Log.cpp index 6144e4f1..763eb25b 100644 --- a/src/common/log/Log.cpp +++ b/src/common/log/Log.cpp @@ -37,11 +37,15 @@ Log *Log::m_self = nullptr; static const char *colors[5] = { - Log::kCL_RED, /* ERR */ - Log::kCL_YELLOW, /* WARNING */ - Log::kCL_WHITE, /* NOTICE */ - "", /* INFO */ - Log::kCL_GRAY /* DEBUG */ + "\x1B[0;31m", /* ERR */ + "\x1B[0;33m", /* WARNING */ + "\x1B[1;37m", /* NOTICE */ + "", /* INFO */ +# ifdef WIN32 + "\x1B[1;30m" /* DEBUG */ +# else + "\x1B[90m" /* DEBUG */ +# endif }; diff --git a/src/common/log/Log.h b/src/common/log/Log.h index 6d4959ef..c4803e3a 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -36,17 +36,6 @@ class Log { public: - constexpr static const char* kCL_N = "\x1B[0m"; - constexpr static const char* kCL_RED = "\x1B[31m"; - constexpr static const char* kCL_YELLOW = "\x1B[33m"; - constexpr static const char* kCL_WHITE = "\x1B[01;37m"; - -# ifdef WIN32 - constexpr static const char* kCL_GRAY = "\x1B[01;30m"; -# else - constexpr static const char* kCL_GRAY = "\x1B[90m"; -# endif - static inline Log* i() { assert(m_self != nullptr); return m_self; } static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); } static inline void init() { if (!m_self) { new Log(); } } diff --git a/src/common/log/SysLog.cpp b/src/common/log/SysLog.cpp index 5eb4dcd4..bcb96394 100644 --- a/src/common/log/SysLog.cpp +++ b/src/common/log/SysLog.cpp @@ -37,11 +37,11 @@ SysLog::SysLog() void SysLog::message(Level level, const char *fmt, va_list args) { - vsyslog(level, fmt, args); + vsyslog(static_cast(level), fmt, args); } void SysLog::text(const char *fmt, va_list args) { - message(LOG_INFO, fmt, args); + vsyslog(LOG_INFO, fmt, args); } diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index b1e03f32..ac64f173 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -110,7 +110,7 @@ int xmrig::Controller::init(int argc, char **argv) } if (config()->logFile()) { - Log::add(new FileLog(config()->logFile())); + Log::add(new FileLog(this, config()->logFile())); } # ifdef HAVE_SYSLOG_H diff --git a/src/interfaces/ILogBackend.h b/src/interfaces/ILogBackend.h index b6db72a8..30554905 100644 --- a/src/interfaces/ILogBackend.h +++ b/src/interfaces/ILogBackend.h @@ -39,6 +39,8 @@ public: DEBUG }; + constexpr static const size_t kBufferSize = 512; + virtual ~ILogBackend() {} virtual void message(Level level, const char* fmt, va_list args) = 0; From 2b3f7f43ceb22a18316d424d31eca949743faf0c Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 19 May 2018 13:47:27 +0700 Subject: [PATCH 277/389] Fix Linux build. --- src/interfaces/ILogBackend.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/interfaces/ILogBackend.h b/src/interfaces/ILogBackend.h index 30554905..6e3f5c1c 100644 --- a/src/interfaces/ILogBackend.h +++ b/src/interfaces/ILogBackend.h @@ -26,6 +26,7 @@ #include +#include class ILogBackend From 14f0e8658c91c27d63115f5f378d8fe562478002 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 19 May 2018 14:44:50 +0700 Subject: [PATCH 278/389] Sync changes with proxy. --- src/common/net/Client.h | 22 ++++++++++++---------- src/common/net/Pool.cpp | 35 +++++++++++++++++++++++++---------- src/common/net/Pool.h | 2 ++ 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/common/net/Client.h b/src/common/net/Client.h index 27273092..4be8badb 100644 --- a/src/common/net/Client.h +++ b/src/common/net/Client.h @@ -30,6 +30,7 @@ #include +#include "common/crypto/Algorithm.h" #include "common/net/Id.h" #include "common/net/Job.h" #include "common/net/Pool.h" @@ -66,16 +67,17 @@ public: void setPool(const Pool &pool); void tick(uint64_t now); - inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; } - inline const char *host() const { return m_pool.host(); } - inline const char *ip() const { return m_ip; } - inline const Job &job() const { return m_job; } - inline int id() const { return m_id; } - inline SocketState state() const { return m_state; } - inline uint16_t port() const { return m_pool.port(); } - inline void setQuiet(bool quiet) { m_quiet = quiet; } - inline void setRetries(int retries) { m_retries = retries; } - inline void setRetryPause(int ms) { m_retryPause = ms; } + inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; } + inline const char *host() const { return m_pool.host(); } + inline const char *ip() const { return m_ip; } + inline const Job &job() const { return m_job; } + inline int id() const { return m_id; } + inline SocketState state() const { return m_state; } + inline uint16_t port() const { return m_pool.port(); } + inline void setAlgo(const xmrig::Algorithm &algo) { m_pool.setAlgo(algo); } + inline void setQuiet(bool quiet) { m_quiet = quiet; } + inline void setRetries(int retries) { m_retries = retries; } + inline void setRetryPause(int ms) { m_retryPause = ms; } private: enum Extensions { diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index ca73b315..c12e05d5 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -247,17 +247,15 @@ void Pool::adjust(xmrig::Algo algorithm) m_algorithm.setVariant(xmrig::VARIANT_1); } - m_algorithms.push_back(m_algorithm); + rebuild(); +} -# ifndef XMRIG_PROXY_PROJECT - if (m_algorithm.algo() != xmrig::CRYPTONIGHT_HEAVY) { - addVariant(xmrig::VARIANT_1); - addVariant(xmrig::VARIANT_0); - addVariant(xmrig::VARIANT_XTL); - addVariant(xmrig::VARIANT_IPBC); - addVariant(xmrig::VARIANT_AUTO); - } -# endif + +void Pool::setAlgo(const xmrig::Algorithm &algorithm) +{ + m_algorithm = algorithm; + + rebuild(); } @@ -309,3 +307,20 @@ void Pool::addVariant(xmrig::Variant variant) m_algorithms.push_back(algorithm); } + + +void Pool::rebuild() +{ + m_algorithms.clear(); + m_algorithms.push_back(m_algorithm); + +# ifndef XMRIG_PROXY_PROJECT + if (m_algorithm.algo() != xmrig::CRYPTONIGHT_HEAVY) { + addVariant(xmrig::VARIANT_1); + addVariant(xmrig::VARIANT_0); + addVariant(xmrig::VARIANT_XTL); + addVariant(xmrig::VARIANT_IPBC); + addVariant(xmrig::VARIANT_AUTO); + } +# endif +} diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index ad015bf2..eaabe54d 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -78,6 +78,7 @@ public: bool setUserpass(const char *userpass); rapidjson::Value toJSON(rapidjson::Document &doc) const; void adjust(xmrig::Algo algorithm); + void setAlgo(const xmrig::Algorithm &algorithm); # ifdef APP_DEBUG void print() const; @@ -86,6 +87,7 @@ public: private: bool parseIPv6(const char *addr); void addVariant(xmrig::Variant variant); + void rebuild(); bool m_nicehash; int m_keepAlive; From 34a3454784662bc8257c31da058a355ea5102d61 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 20 May 2018 14:27:46 +0700 Subject: [PATCH 279/389] Small fix in CMakeLists.txt. --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b26b1d93..c71309c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,6 @@ set(HEADERS src/common/utils/c_str.h src/common/utils/mm_malloc.h src/common/xmrig.h - src/core/Config.cpp src/core/ConfigLoader_platform.h src/core/Controller.h src/Cpu.h From 009bd1a5073cec9073e9da67d95f4bb6064b3aa1 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 1 Jun 2018 01:48:31 +0700 Subject: [PATCH 280/389] Sync changes with amd miner and update summary. --- CMakeLists.txt | 5 +-- src/Summary.cpp | 65 +++++++++++++++------------- src/common/config/CommonConfig.cpp | 68 +++++++++++++++--------------- src/common/config/CommonConfig.h | 3 +- src/common/xmrig.h | 6 +++ src/core/Config.cpp | 2 +- src/workers/Hashrate.cpp | 16 +++++-- src/workers/Hashrate.h | 6 ++- 8 files changed, 97 insertions(+), 74 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c71309c3..a242970b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,6 @@ project(xmrig) option(WITH_LIBCPUID "Use Libcpuid" ON) option(WITH_AEON "CryptoNight-Lite support" ON) option(WITH_SUMO "CryptoNight-Heavy support" ON) -option(WITH_IPBC "CryptoNight-IPBC support" ON) option(WITH_HTTPD "HTTP REST API" ON) option(BUILD_STATIC "Build static binary" OFF) @@ -245,5 +244,5 @@ if (BUILD_STATIC) set(CMAKE_EXE_LINKER_FLAGS " -static") endif() -add_executable(xmrig ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES}) -target_link_libraries(xmrig ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) +add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES}) +target_link_libraries(${PROJECT_NAME} ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) diff --git a/src/Summary.cpp b/src/Summary.cpp index e960dd8d..fe538fda 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -39,7 +39,7 @@ static void print_versions(xmrig::Config *config) { - char buf[16]; + char buf[16] = { 0 }; # if defined(__clang__) snprintf(buf, 16, " clang/%d.%d.%d", __clang_major__, __clang_minor__, __clang_patchlevel__); @@ -47,24 +47,22 @@ static void print_versions(xmrig::Config *config) snprintf(buf, 16, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); # elif defined(_MSC_VER) snprintf(buf, 16, " MSVC/%d", MSVC_VERSION); -# else - buf[0] = '\0'; # endif - - Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mVERSIONS: \x1B[01;36mXMRig/%s\x1B[01;37m libuv/%s%s" : " * VERSIONS: XMRig/%s libuv/%s%s", - APP_VERSION, uv_version_string(), buf); + Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%s/%s") WHITE_BOLD(" libuv/%s%s") + : " * %-13s%s/%s libuv/%s%s", + "VERSIONS", APP_NAME, APP_VERSION, uv_version_string(), buf); } static void print_memory(xmrig::Config *config) { # ifdef _WIN32 if (config->isColors()) { - Log::i()->text("\x1B[01;32m * \x1B[01;37mHUGE PAGES: %s", - Mem::isHugepagesAvailable() ? "\x1B[01;32mavailable" : "\x1B[01;31munavailable"); + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") "%s", + "HUGE PAGES", Mem::isHugepagesAvailable() ? "\x1B[1;32mavailable" : "\x1B[01;31munavailable"); } else { - Log::i()->text(" * HUGE PAGES: %s", Mem::isHugepagesAvailable() ? "available" : "unavailable"); + Log::i()->text(" * %-13s%s", "HUGE PAGES", Mem::isHugepagesAvailable() ? "available" : "unavailable"); } # endif } @@ -73,19 +71,20 @@ static void print_memory(xmrig::Config *config) { static void print_cpu(xmrig::Config *config) { if (config->isColors()) { - Log::i()->text("\x1B[01;32m * \x1B[01;37mCPU: %s (%d) %sx64 %sAES-NI", + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") "%s (%d) %sx64 %sAES-NI", + "CPU", Cpu::brand(), Cpu::sockets(), - Cpu::isX64() ? "\x1B[01;32m" : "\x1B[01;31m-", - Cpu::hasAES() ? "\x1B[01;32m" : "\x1B[01;31m-"); + Cpu::isX64() ? "\x1B[1;32m" : "\x1B[1;31m-", + Cpu::hasAES() ? "\x1B[1;32m" : "\x1B[1;31m-"); # ifndef XMRIG_NO_LIBCPUID - Log::i()->text("\x1B[01;32m * \x1B[01;37mCPU L2/L3: %.1f MB/%.1f MB", Cpu::l2() / 1024.0, Cpu::l3() / 1024.0); + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") "%.1f MB/%.1f MB", "CPU L2/L3", Cpu::l2() / 1024.0, Cpu::l3() / 1024.0); # endif } else { - Log::i()->text(" * CPU: %s (%d) %sx64 %sAES-NI", Cpu::brand(), Cpu::sockets(), Cpu::isX64() ? "" : "-", Cpu::hasAES() ? "" : "-"); + Log::i()->text(" * %-13s%s (%d) %sx64 %sAES-NI", "CPU", Cpu::brand(), Cpu::sockets(), Cpu::isX64() ? "" : "-", Cpu::hasAES() ? "" : "-"); # ifndef XMRIG_NO_LIBCPUID - Log::i()->text(" * CPU L2/L3: %.1f MB/%.1f MB", Cpu::l2() / 1024.0, Cpu::l3() / 1024.0); + Log::i()->text(" * %-13s%.1f MB/%.1f MB", "CPU L2/L3", Cpu::l2() / 1024.0, Cpu::l3() / 1024.0); # endif } } @@ -94,27 +93,28 @@ static void print_cpu(xmrig::Config *config) static void print_threads(xmrig::Config *config) { if (config->threadsMode() != xmrig::Config::Advanced) { - char buf[32]; + char buf[32] = { 0 }; if (config->affinity() != -1L) { - snprintf(buf, 32, ", affinity=0x%" PRIX64, config->affinity()); - } - else { - buf[0] = '\0'; + snprintf(buf, sizeof buf, ", affinity=0x%" PRIX64, config->affinity()); } - Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mTHREADS: \x1B[01;36m%d\x1B[01;37m, %s, av=%d, %sdonate=%d%%%s" : " * THREADS: %d, %s, av=%d, %sdonate=%d%%%s", + Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", %s, av=%d, %sdonate=%d%%") WHITE_BOLD("%s") + : " * %-13s%d, %s, av=%d, %sdonate=%d%%%s", + "THREADS", config->threadsCount(), config->algorithm().name(), config->algoVariant(), - config->isColors() && config->donateLevel() == 0 ? "\x1B[01;31m" : "", + config->isColors() && config->donateLevel() == 0 ? "\x1B[1;31m" : "", config->donateLevel(), buf); } else { - Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mTHREADS: \x1B[01;36m%d\x1B[01;37m, %s, %sdonate=%d%%" : " * THREADS: %d, %s, %sdonate=%d%%", + Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", %s, %sdonate=%d%%") + : " * %-13s%d, %s, %sdonate=%d%%", + "THREADS", config->threadsCount(), config->algorithm().name(), - config->isColors() && config->donateLevel() == 0 ? "\x1B[01;31m" : "", + config->isColors() && config->donateLevel() == 0 ? "\x1B[1;31m" : "", config->donateLevel()); } } @@ -125,9 +125,11 @@ static void print_pools(xmrig::Config *config) const std::vector &pools = config->pools(); for (size_t i = 0; i < pools.size(); ++i) { - Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mPOOL #%d: \x1B[01;36m%s" : " * POOL #%d: %s", + Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") CYAN_BOLD("%s") " variant " WHITE_BOLD("%s") + : " * POOL #%-7d%s variant %s", i + 1, - pools[i].url() + pools[i].url(), + pools[i].algorithm().variantName() ); } @@ -147,8 +149,9 @@ static void print_api(xmrig::Config *config) return; } - Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mAPI BIND: \x1B[01;36m%s:%d" : " * API BIND: %s:%d", - config->isApiIPv6() ? "[::]" : "0.0.0.0", port); + Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN("%s:") CYAN_BOLD("%d") + : " * %-13s%s:%d", + "API BIND", config->isApiIPv6() ? "[::]" : "0.0.0.0", port); } #endif @@ -156,10 +159,12 @@ static void print_api(xmrig::Config *config) static void print_commands(xmrig::Config *config) { if (config->isColors()) { - Log::i()->text("\x1B[01;32m * \x1B[01;37mCOMMANDS: \x1B[01;35mh\x1B[01;37mashrate, \x1B[01;35mp\x1B[01;37mause, \x1B[01;35mr\x1B[01;37mesume"); + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("COMMANDS ") MAGENTA_BOLD("h") WHITE_BOLD("ashrate, ") + MAGENTA_BOLD("p") WHITE_BOLD("ause, ") + MAGENTA_BOLD("r") WHITE_BOLD("esume")); } else { - Log::i()->text(" * COMMANDS: 'h' hashrate, 'p' pause, 'r' resume"); + Log::i()->text(" * COMMANDS 'h' hashrate, 'p' pause, 'r' resume"); } } diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 5eaf68fd..155485d2 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -72,6 +72,40 @@ xmrig::CommonConfig::~CommonConfig() } +bool xmrig::CommonConfig::save() +{ + if (m_fileName.isNull()) { + return false; + } + + uv_fs_t req; + const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName.data(), O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr); + if (fd < 0) { + return false; + } + + uv_fs_req_cleanup(&req); + + rapidjson::Document doc; + getJSON(doc); + + FILE *fp = fdopen(fd, "w"); + + char buf[4096]; + rapidjson::FileWriteStream os(fp, buf, sizeof(buf)); + rapidjson::PrettyWriter writer(os); + doc.Accept(writer); + + fclose(fp); + + uv_fs_close(uv_default_loop(), &req, fd, nullptr); + uv_fs_req_cleanup(&req); + + LOG_NOTICE("configuration saved to: \"%s\"", m_fileName.data()); + return true; +} + + bool xmrig::CommonConfig::finalize() { if (m_state == ReadyState) { @@ -254,40 +288,6 @@ bool xmrig::CommonConfig::parseUint64(int key, uint64_t arg) } -bool xmrig::CommonConfig::save() -{ - if (m_fileName.isNull()) { - return false; - } - - uv_fs_t req; - const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName.data(), O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr); - if (fd < 0) { - return false; - } - - uv_fs_req_cleanup(&req); - - rapidjson::Document doc; - getJSON(doc); - - FILE *fp = fdopen(fd, "w"); - - char buf[4096]; - rapidjson::FileWriteStream os(fp, buf, sizeof(buf)); - rapidjson::PrettyWriter writer(os); - doc.Accept(writer); - - fclose(fp); - - uv_fs_close(uv_default_loop(), &req, fd, nullptr); - uv_fs_req_cleanup(&req); - - LOG_NOTICE("configuration saved to: \"%s\"", m_fileName.data()); - return true; -} - - void xmrig::CommonConfig::setFileName(const char *fileName) { m_fileName = fileName; diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index d54afe3a..356f550d 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -64,6 +64,8 @@ public: inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); } inline const char *fileName() const override { return m_fileName.data(); } + bool save() override; + protected: enum State { NoneState, @@ -75,7 +77,6 @@ protected: bool parseBoolean(int key, bool enable) override; bool parseString(int key, const char *arg) override; bool parseUint64(int key, uint64_t arg) override; - bool save() override; void setFileName(const char *fileName) override; Algorithm m_algorithm; diff --git a/src/common/xmrig.h b/src/common/xmrig.h index a6fda9fc..43a2a252 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -66,6 +66,12 @@ enum Variant { }; +enum AlgoVerify { + VERIFY_HW_AES = 1, + VERIFY_SOFT_AES = 2 +}; + + enum AesMode { AES_AUTO, AES_HW, diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 4c283164..232e7024 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -116,7 +116,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const Value threads(kArrayType); for (const IThread *thread : m_threads.list) { - threads.PushBack(thread->toConfig(doc), doc.GetAllocator()); + threads.PushBack(thread->toConfig(doc), allocator); } doc.AddMember("threads", threads, allocator); diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index 42068eb7..2a750318 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -35,7 +35,7 @@ #include "workers/Hashrate.h" -inline const char *format(double h, char* buf, size_t size) +inline static const char *format(double h, char *buf, size_t size) { if (isnormal(h)) { snprintf(buf, size, "%03.1f", h); @@ -91,6 +91,9 @@ double Hashrate::calc(size_t ms) const double Hashrate::calc(size_t threadId, size_t ms) const { assert(threadId < m_threads); + if (threadId >= m_threads) { + return nan(""); + } using namespace std::chrono; const uint64_t now = time_point_cast(high_resolution_clock::now()).time_since_epoch().count(); @@ -149,14 +152,15 @@ void Hashrate::add(size_t threadId, uint64_t count, uint64_t timestamp) } -void Hashrate::print() +void Hashrate::print() const { char num1[8] = { 0 }; char num2[8] = { 0 }; char num3[8] = { 0 }; char num4[8] = { 0 }; - LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;37mspeed\x1B[0m 2.5s/60s/15m \x1B[01;36m%s \x1B[22;36m%s %s \x1B[01;36mH/s\x1B[0m max: \x1B[01;36m%s H/s" : "speed 2.5s/60s/15m %s %s %s H/s max: %s H/s", + LOG_INFO(m_controller->config()->isColors() ? WHITE_BOLD("speed") " 10s/60s/15m " CYAN_BOLD("%s") CYAN(" %s %s ") CYAN_BOLD("H/s") " max " CYAN_BOLD("%s H/s") + : "speed 10s/60s/15m %s %s %s H/s max %s H/s", format(calc(ShortInterval), num1, sizeof(num1)), format(calc(MediumInterval), num2, sizeof(num2)), format(calc(LargeInterval), num3, sizeof(num3)), @@ -180,6 +184,12 @@ void Hashrate::updateHighest() } +const char *Hashrate::format(double h, char *buf, size_t size) +{ + return ::format(h, buf, size); +} + + void Hashrate::onReport(uv_timer_t *handle) { static_cast(handle->data)->print(); diff --git a/src/workers/Hashrate.h b/src/workers/Hashrate.h index e79c757a..e766f117 100644 --- a/src/workers/Hashrate.h +++ b/src/workers/Hashrate.h @@ -38,7 +38,7 @@ class Hashrate { public: enum Intervals { - ShortInterval = 2500, + ShortInterval = 10000, MediumInterval = 60000, LargeInterval = 900000 }; @@ -47,13 +47,15 @@ public: double calc(size_t ms) const; double calc(size_t threadId, size_t ms) const; void add(size_t threadId, uint64_t count, uint64_t timestamp); - void print(); + void print() const; void stop(); void updateHighest(); inline double highest() const { return m_highest; } inline size_t threads() const { return m_threads; } + static const char *format(double h, char *buf, size_t size); + private: static void onReport(uv_timer_t *handle); From 5d6a622b1813187ebdb96dc1338b88d602611a27 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 1 Jun 2018 02:33:21 +0700 Subject: [PATCH 281/389] New detailed hashrate report. --- src/workers/Workers.cpp | 31 +++++++++++++++++++++++++++++++ src/workers/Workers.h | 1 + 2 files changed, 32 insertions(+) diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index f6ec04b7..0e75e736 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -22,6 +22,7 @@ */ #include +#include #include @@ -55,6 +56,7 @@ uv_async_t Workers::m_async; uv_mutex_t Workers::m_mutex; uv_rwlock_t Workers::m_rwlock; uv_timer_t Workers::m_timer; +xmrig::Controller *Workers::m_controller = nullptr; Job Workers::job() @@ -89,6 +91,33 @@ size_t Workers::threads() void Workers::printHashrate(bool detail) { + assert(m_controller != nullptr); + if (!m_controller) { + return; + } + + if (detail) { + const bool isColors = m_controller->config()->isColors(); + char num1[8] = { 0 }; + char num2[8] = { 0 }; + char num3[8] = { 0 }; + + Log::i()->text("%s| THREAD | AFFINITY | 10s H/s | 60s H/s | 15m H/s |", isColors ? "\x1B[1;37m" : ""); + + size_t i = 0; + for (const xmrig::IThread *thread : m_controller->config()->threads()) { + Log::i()->text("| %6zu | %8" PRId64 " | %7s | %7s | %7s |", + thread->index(), + thread->affinity(), + Hashrate::format(m_hashrate->calc(thread->index(), Hashrate::ShortInterval), num1, sizeof num1), + Hashrate::format(m_hashrate->calc(thread->index(), Hashrate::MediumInterval), num2, sizeof num2), + Hashrate::format(m_hashrate->calc(thread->index(), Hashrate::LargeInterval), num3, sizeof num3) + ); + + i++; + } + } + m_hashrate->print(); } @@ -131,6 +160,8 @@ void Workers::setJob(const Job &job, bool donate) void Workers::start(xmrig::Controller *controller) { + m_controller = controller; + const std::vector &threads = controller->config()->threads(); m_status.algo = controller->config()->algorithm().algo(); m_status.colors = controller->config()->isColors(); diff --git a/src/workers/Workers.h b/src/workers/Workers.h index ca01e698..1d619cea 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -114,6 +114,7 @@ private: static uv_mutex_t m_mutex; static uv_rwlock_t m_rwlock; static uv_timer_t m_timer; + static xmrig::Controller *m_controller; }; From d900a6d9dd091008b0366fb73a01aac6d910168f Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 2 Jun 2018 04:37:12 +0700 Subject: [PATCH 282/389] #446 Fixed SIGBUS error on 32 bit ARM CPUs. --- src/crypto/CryptoNight_monero.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index ea1622ab..942940a4 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -38,8 +38,9 @@ # define VARIANT1_INIT(part) \ uint64_t tweak1_2_##part = 0; \ if (VARIANT > 0) { \ - volatile const uint64_t a = *reinterpret_cast(input + 35 + part * size); \ - volatile const uint64_t b = *(reinterpret_cast(ctx[part]->state) + 24); \ + uint64_t a, b; \ + memcpy(&a, input + 35 + part * size, sizeof a); \ + memcpy(&b, ctx[part]->state + 192, sizeof b); \ tweak1_2_##part = a ^ b; \ } #endif From 26ee5028e114afa7f4f6b6173dfcd9f7f10935ac Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 3 Jun 2018 00:09:59 +0700 Subject: [PATCH 283/389] Use native checks instead of XMRIG_ARMv8. --- src/Cpu_arm.cpp | 5 ++++- src/crypto/CryptoNight_arm.h | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Cpu_arm.cpp b/src/Cpu_arm.cpp index 59ff8421..3e259d0d 100644 --- a/src/Cpu_arm.cpp +++ b/src/Cpu_arm.cpp @@ -47,8 +47,11 @@ void Cpu::initCommon() { memcpy(m_brand, "Unknown", 7); -# if defined(XMRIG_ARMv8) +# if defined (__arm64__) || defined (__aarch64__) m_flags |= X86_64; +# endif + +# if __ARM_FEATURE_CRYPTO m_flags |= AES; # endif } diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 284b1f5f..161a095f 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -73,7 +73,7 @@ static inline __attribute__((always_inline)) __m128i _mm_set_epi64x(const uint64 } -#ifdef XMRIG_ARMv8 +#if __ARM_FEATURE_CRYPTO static inline __attribute__((always_inline)) __m128i _mm_aesenc_si128(__m128i v, __m128i rkey) { alignas(16) const __m128i zero = { 0 }; @@ -82,6 +82,8 @@ static inline __attribute__((always_inline)) __m128i _mm_aesenc_si128(__m128i v, #else static inline __attribute__((always_inline)) __m128i _mm_aesenc_si128(__m128i v, __m128i rkey) { + alignas(16) const __m128i zero = { 0 }; + return zero; } #endif @@ -96,7 +98,7 @@ static inline __attribute__((always_inline)) uint64_t _mm_cvtsi128_si64(__m128i #define EXTRACT64(X) _mm_cvtsi128_si64(X) -#if defined(XMRIG_ARMv8) +#if defined (__arm64__) || defined (__aarch64__) static inline uint64_t __umul128(uint64_t a, uint64_t b, uint64_t* hi) { unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b; From 651637d6377a25f2c9b47c48085bad0d3c598d61 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 3 Jun 2018 00:22:26 +0700 Subject: [PATCH 284/389] #446 Better fix, second variable always aligned. --- src/crypto/CryptoNight_monero.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index 942940a4..28572d88 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -38,10 +38,8 @@ # define VARIANT1_INIT(part) \ uint64_t tweak1_2_##part = 0; \ if (VARIANT > 0) { \ - uint64_t a, b; \ - memcpy(&a, input + 35 + part * size, sizeof a); \ - memcpy(&b, ctx[part]->state + 192, sizeof b); \ - tweak1_2_##part = a ^ b; \ + memcpy(&tweak1_2_##part, input + 35 + part * size, sizeof tweak1_2_##part); \ + tweak1_2_##part ^= *(reinterpret_cast(ctx[part]->state) + 24); \ } #endif From 6cf24936dfab31e5c4ba580277dae0542464927a Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 3 Jun 2018 02:56:49 +0700 Subject: [PATCH 285/389] #551 Fixed cn-heavy for ARMv8. --- src/crypto/CryptoNight_arm.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 161a095f..8c59f596 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -472,9 +472,10 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si idx0 = al0; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; - int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; - int64_t q = n / (d | 0x5); + const int64x2_t x = vld1q_s64(reinterpret_cast(&l0[idx0 & MASK])); + const int64_t n = vgetq_lane_s64(x, 0); + const int32_t d = vgetq_lane_s32(x, 2); + const int64_t q = n / (d | 0x5); ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; idx0 = d ^ q; From 1748a7bd5730561ecf917ee4d2fa363a46a662ea Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 3 Jun 2018 05:42:10 +0700 Subject: [PATCH 286/389] Fix double hash mode too. --- src/crypto/CryptoNight_arm.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 8c59f596..fd5bcb7b 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -580,10 +580,11 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ah0 ^= ch; idx0 = al0; - if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; - int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; - int64_t q = n / (d | 0x5); + if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { + const int64x2_t x = vld1q_s64(reinterpret_cast(&l0[idx0 & MASK])); + const int64_t n = vgetq_lane_s64(x, 0); + const int32_t d = vgetq_lane_s32(x, 2); + const int64_t q = n / (d | 0x5); ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; idx0 = d ^ q; @@ -615,9 +616,10 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si idx1 = al1; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)&l1[idx1 & MASK])[0]; - int32_t d = ((int32_t*)&l1[idx1 & MASK])[2]; - int64_t q = n / (d | 0x5); + const int64x2_t x = vld1q_s64(reinterpret_cast(&l1[idx1 & MASK])); + const int64_t n = vgetq_lane_s64(x, 0); + const int32_t d = vgetq_lane_s32(x, 2); + const int64_t q = n / (d | 0x5); ((int64_t*)&l1[idx1 & MASK])[0] = n ^ q; idx1 = d ^ q; From e320b2e9282f7740811722039c1e4e9cf32de0e2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 4 Jun 2018 02:09:29 +0700 Subject: [PATCH 287/389] #672 Reverted back "cryptonight-light" and exit if no valid algorithm specified. --- src/common/config/CommonConfig.cpp | 3 ++- src/common/config/CommonConfig.h | 6 +++--- src/common/config/ConfigLoader.cpp | 8 +++++++- src/common/crypto/Algorithm.cpp | 1 + src/interfaces/IConfig.h | 2 ++ 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 155485d2..668bf0d8 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -38,6 +38,7 @@ xmrig::CommonConfig::CommonConfig() : + m_algorithm(CRYPTONIGHT, VARIANT_AUTO), m_adjusted(false), m_apiIPv6(false), m_apiRestricted(true), @@ -117,7 +118,7 @@ bool xmrig::CommonConfig::finalize() } if (!m_algorithm.isValid()) { - m_algorithm.setAlgo(CRYPTONIGHT); + return false; } for (Pool &pool : m_pools) { diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 356f550d..95a02fb1 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -48,7 +48,6 @@ public: inline bool isBackground() const { return m_background; } inline bool isColors() const { return m_colors; } inline bool isSyslog() const { return m_syslog; } - inline const Algorithm &algorithm() const { return m_algorithm; } inline const char *apiToken() const { return m_apiToken.data(); } inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *logFile() const { return m_logFile.data(); } @@ -61,8 +60,9 @@ public: inline int retryPause() const { return m_retryPause; } inline void setColors(bool colors) { m_colors = colors; } - inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); } - inline const char *fileName() const override { return m_fileName.data(); } + inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); } + inline const Algorithm &algorithm() const override { return m_algorithm; } + inline const char *fileName() const override { return m_fileName.data(); } bool save() override; diff --git a/src/common/config/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp index 919ff00c..32d0850c 100644 --- a/src/common/config/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -170,7 +170,13 @@ xmrig::IConfig *xmrig::ConfigLoader::load(int argc, char **argv, IConfigCreator } if (!config->finalize()) { - fprintf(stderr, "No valid configuration found. Exiting.\n"); + if (!config->algorithm().isValid()) { + fprintf(stderr, "No valid algorithm specified. Exiting.\n"); + } + else { + fprintf(stderr, "No valid configuration found. Exiting.\n"); + } + delete config; return nullptr; } diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index fce4d7b8..f3f4c2f4 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -60,6 +60,7 @@ static AlgoData const algorithms[] = { # ifndef XMRIG_NO_AEON { "cryptonight-lite", "cn-lite", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, + { "cryptonight-light", "cn-light", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, { "cryptonight-lite/0", "cn-lite/0", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_0 }, { "cryptonight-lite/1", "cn-lite/1", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, { "cryptonight-lite/ipbc", "cn-lite/ipbc", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_IPBC }, diff --git a/src/interfaces/IConfig.h b/src/interfaces/IConfig.h index 2422b891..77e262da 100644 --- a/src/interfaces/IConfig.h +++ b/src/interfaces/IConfig.h @@ -24,6 +24,7 @@ #define __ICONFIG_H__ +#include "common/crypto/Algorithm.h" #include "rapidjson/fwd.h" @@ -97,6 +98,7 @@ public: virtual bool parseString(int key, const char *arg) = 0; virtual bool parseUint64(int key, uint64_t arg) = 0; virtual bool save() = 0; + virtual const Algorithm &algorithm() const = 0; virtual const char *fileName() const = 0; virtual void getJSON(rapidjson::Document &doc) const = 0; virtual void parseJSON(const rapidjson::Document &doc) = 0; From 48a214c3f19ff9f9e25b3975e7a9fbd2e57628b2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 5 Jun 2018 07:34:58 +0700 Subject: [PATCH 288/389] Move files. --- CMakeLists.txt | 18 +++++++++--------- src/App.h | 2 +- src/api/ApiRouter.h | 2 +- src/common/config/CommonConfig.h | 2 +- src/common/config/ConfigLoader.cpp | 4 ++-- src/common/config/ConfigWatcher.cpp | 2 +- src/{ => common}/interfaces/IClientListener.h | 0 src/{ => common}/interfaces/IConfig.h | 6 ++++++ src/{ => common}/interfaces/IConfigCreator.h | 0 src/{ => common}/interfaces/IConsoleListener.h | 0 .../interfaces/IControllerListener.h | 0 src/{ => common}/interfaces/ILogBackend.h | 4 ++++ src/{ => common}/interfaces/IStrategy.h | 0 .../interfaces/IStrategyListener.h | 0 src/{ => common}/interfaces/IWatcherListener.h | 0 src/common/log/ConsoleLog.h | 2 +- src/common/log/FileLog.h | 2 +- src/common/log/Log.cpp | 2 +- src/common/log/Log.h | 2 +- src/common/net/Client.cpp | 2 +- src/common/net/Pool.cpp | 6 ++++++ src/common/net/strategies/FailoverStrategy.cpp | 2 +- src/common/net/strategies/FailoverStrategy.h | 4 ++-- .../net/strategies/SinglePoolStrategy.cpp | 2 +- src/common/net/strategies/SinglePoolStrategy.h | 4 ++-- src/core/ConfigCreator.h | 2 +- src/core/ConfigLoader_platform.h | 2 +- src/core/Controller.cpp | 2 +- src/core/Controller.h | 2 +- src/net/Network.h | 2 +- src/net/strategies/DonateStrategy.cpp | 2 +- src/net/strategies/DonateStrategy.h | 6 +++--- 32 files changed, 51 insertions(+), 35 deletions(-) rename src/{ => common}/interfaces/IClientListener.h (100%) rename src/{ => common}/interfaces/IConfig.h (95%) rename src/{ => common}/interfaces/IConfigCreator.h (100%) rename src/{ => common}/interfaces/IConsoleListener.h (100%) rename src/{ => common}/interfaces/IControllerListener.h (100%) rename src/{ => common}/interfaces/ILogBackend.h (94%) rename src/{ => common}/interfaces/IStrategy.h (100%) rename src/{ => common}/interfaces/IStrategyListener.h (100%) rename src/{ => common}/interfaces/IWatcherListener.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index a242970b..e263808e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,15 @@ set(HEADERS src/common/Console.h src/common/crypto/Algorithm.h src/common/crypto/keccak.h + src/common/interfaces/IClientListener.h + src/common/interfaces/IConfig.h + src/common/interfaces/IConfigCreator.h + src/common/interfaces/IConsoleListener.h + src/common/interfaces/IControllerListener.h + src/common/interfaces/ILogBackend.h + src/common/interfaces/IStrategy.h + src/common/interfaces/IStrategyListener.h + src/common/interfaces/IWatcherListener.h src/common/log/ConsoleLog.h src/common/log/FileLog.h src/common/log/Log.h @@ -38,17 +47,8 @@ set(HEADERS src/core/ConfigLoader_platform.h src/core/Controller.h src/Cpu.h - src/interfaces/IClientListener.h - src/interfaces/IConfig.h - src/interfaces/IConfigCreator.h - src/interfaces/IConsoleListener.h - src/interfaces/IControllerListener.h src/interfaces/IJobResultListener.h - src/interfaces/ILogBackend.h - src/interfaces/IStrategy.h - src/interfaces/IStrategyListener.h src/interfaces/IThread.h - src/interfaces/IWatcherListener.h src/interfaces/IWorker.h src/Mem.h src/net/JobResult.h diff --git a/src/App.h b/src/App.h index 22269f67..964400e6 100644 --- a/src/App.h +++ b/src/App.h @@ -28,7 +28,7 @@ #include -#include "interfaces/IConsoleListener.h" +#include "common/interfaces/IConsoleListener.h" class Console; diff --git a/src/api/ApiRouter.h b/src/api/ApiRouter.h index 3ed458d4..9e32cdae 100644 --- a/src/api/ApiRouter.h +++ b/src/api/ApiRouter.h @@ -26,7 +26,7 @@ #include "api/NetworkState.h" -#include "interfaces/IControllerListener.h" +#include "common/interfaces/IControllerListener.h" #include "rapidjson/fwd.h" diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 95a02fb1..2a329c7e 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -28,10 +28,10 @@ #include +#include "common/interfaces/IConfig.h" #include "common/net/Pool.h" #include "common/utils/c_str.h" #include "common/xmrig.h" -#include "interfaces/IConfig.h" namespace xmrig { diff --git a/src/common/config/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp index 32d0850c..cc5d9a49 100644 --- a/src/common/config/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -34,12 +34,12 @@ #include "common/config/ConfigLoader.h" #include "common/config/ConfigWatcher.h" +#include "common/interfaces/IConfig.h" +#include "common/interfaces/IWatcherListener.h" #include "common/net/Pool.h" #include "common/Platform.h" #include "core/ConfigCreator.h" #include "core/ConfigLoader_platform.h" -#include "interfaces/IConfig.h" -#include "interfaces/IWatcherListener.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" #include "rapidjson/filereadstream.h" diff --git a/src/common/config/ConfigWatcher.cpp b/src/common/config/ConfigWatcher.cpp index bde35f23..14107b62 100644 --- a/src/common/config/ConfigWatcher.cpp +++ b/src/common/config/ConfigWatcher.cpp @@ -27,9 +27,9 @@ #include "common/config/ConfigLoader.h" #include "common/config/ConfigWatcher.h" +#include "common/interfaces/IWatcherListener.h" #include "common/log/Log.h" #include "core/ConfigCreator.h" -#include "interfaces/IWatcherListener.h" xmrig::ConfigWatcher::ConfigWatcher(const char *path, IConfigCreator *creator, IWatcherListener *listener) : diff --git a/src/interfaces/IClientListener.h b/src/common/interfaces/IClientListener.h similarity index 100% rename from src/interfaces/IClientListener.h rename to src/common/interfaces/IClientListener.h diff --git a/src/interfaces/IConfig.h b/src/common/interfaces/IConfig.h similarity index 95% rename from src/interfaces/IConfig.h rename to src/common/interfaces/IConfig.h index 77e262da..62c7ba94 100644 --- a/src/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -78,6 +78,12 @@ public: ThreadsKey = 't', HardwareAESKey = 1011, + // xmrig amd + OclPlatform = 1400, + OclAffinity = 1401, + OclDevices = 1402, + OclLaunch = 1403, + // xmrig-proxy AccessLogFileKey = 'A', BindKey = 'b', diff --git a/src/interfaces/IConfigCreator.h b/src/common/interfaces/IConfigCreator.h similarity index 100% rename from src/interfaces/IConfigCreator.h rename to src/common/interfaces/IConfigCreator.h diff --git a/src/interfaces/IConsoleListener.h b/src/common/interfaces/IConsoleListener.h similarity index 100% rename from src/interfaces/IConsoleListener.h rename to src/common/interfaces/IConsoleListener.h diff --git a/src/interfaces/IControllerListener.h b/src/common/interfaces/IControllerListener.h similarity index 100% rename from src/interfaces/IControllerListener.h rename to src/common/interfaces/IControllerListener.h diff --git a/src/interfaces/ILogBackend.h b/src/common/interfaces/ILogBackend.h similarity index 94% rename from src/interfaces/ILogBackend.h rename to src/common/interfaces/ILogBackend.h index 6e3f5c1c..85a04e93 100644 --- a/src/interfaces/ILogBackend.h +++ b/src/common/interfaces/ILogBackend.h @@ -40,7 +40,11 @@ public: DEBUG }; +# ifdef APP_DEBUG + constexpr static const size_t kBufferSize = 1024; +# else constexpr static const size_t kBufferSize = 512; +# endif virtual ~ILogBackend() {} diff --git a/src/interfaces/IStrategy.h b/src/common/interfaces/IStrategy.h similarity index 100% rename from src/interfaces/IStrategy.h rename to src/common/interfaces/IStrategy.h diff --git a/src/interfaces/IStrategyListener.h b/src/common/interfaces/IStrategyListener.h similarity index 100% rename from src/interfaces/IStrategyListener.h rename to src/common/interfaces/IStrategyListener.h diff --git a/src/interfaces/IWatcherListener.h b/src/common/interfaces/IWatcherListener.h similarity index 100% rename from src/interfaces/IWatcherListener.h rename to src/common/interfaces/IWatcherListener.h diff --git a/src/common/log/ConsoleLog.h b/src/common/log/ConsoleLog.h index 81813c81..bac09a53 100644 --- a/src/common/log/ConsoleLog.h +++ b/src/common/log/ConsoleLog.h @@ -28,7 +28,7 @@ #include -#include "interfaces/ILogBackend.h" +#include "common/interfaces/ILogBackend.h" namespace xmrig { diff --git a/src/common/log/FileLog.h b/src/common/log/FileLog.h index 2c25f093..8a58d4e4 100644 --- a/src/common/log/FileLog.h +++ b/src/common/log/FileLog.h @@ -28,7 +28,7 @@ #include -#include "interfaces/ILogBackend.h" +#include "common/interfaces/ILogBackend.h" namespace xmrig { diff --git a/src/common/log/Log.cpp b/src/common/log/Log.cpp index 763eb25b..ccf38008 100644 --- a/src/common/log/Log.cpp +++ b/src/common/log/Log.cpp @@ -29,8 +29,8 @@ #include +#include "common/interfaces/ILogBackend.h" #include "common/log/Log.h" -#include "interfaces/ILogBackend.h" Log *Log::m_self = nullptr; diff --git a/src/common/log/Log.h b/src/common/log/Log.h index c4803e3a..bfa30717 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -30,7 +30,7 @@ #include -#include "interfaces/ILogBackend.h" +#include "common/interfaces/ILogBackend.h" class Log diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index 921fa618..a552ed3c 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -29,9 +29,9 @@ #include +#include "common/interfaces/IClientListener.h" #include "common/log/Log.h" #include "common/net/Client.h" -#include "interfaces/IClientListener.h" #include "net/JobResult.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index c12e05d5..d5943545 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -101,6 +101,12 @@ bool Pool::isCompatible(const xmrig::Algorithm &algorithm) const } } +# ifdef XMRIG_PROXY_PROJECT + if (m_algorithm.algo() == xmrig::CRYPTONIGHT && algorithm.algo() == xmrig::CRYPTONIGHT && m_algorithm.variant() == xmrig::VARIANT_XTL) { + return true; + } +# endif + return false; } diff --git a/src/common/net/strategies/FailoverStrategy.cpp b/src/common/net/strategies/FailoverStrategy.cpp index 295b4335..fab78590 100644 --- a/src/common/net/strategies/FailoverStrategy.cpp +++ b/src/common/net/strategies/FailoverStrategy.cpp @@ -22,10 +22,10 @@ */ +#include "common/interfaces/IStrategyListener.h" #include "common/net/Client.h" #include "common/net/strategies/FailoverStrategy.h" #include "common/Platform.h" -#include "interfaces/IStrategyListener.h" FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : diff --git a/src/common/net/strategies/FailoverStrategy.h b/src/common/net/strategies/FailoverStrategy.h index f86b366a..07095b3b 100644 --- a/src/common/net/strategies/FailoverStrategy.h +++ b/src/common/net/strategies/FailoverStrategy.h @@ -28,9 +28,9 @@ #include +#include "common/interfaces/IClientListener.h" +#include "common/interfaces/IStrategy.h" #include "common/net/Pool.h" -#include "interfaces/IClientListener.h" -#include "interfaces/IStrategy.h" class Client; diff --git a/src/common/net/strategies/SinglePoolStrategy.cpp b/src/common/net/strategies/SinglePoolStrategy.cpp index 21ce7b34..2cfc0976 100644 --- a/src/common/net/strategies/SinglePoolStrategy.cpp +++ b/src/common/net/strategies/SinglePoolStrategy.cpp @@ -22,10 +22,10 @@ */ +#include "common/interfaces/IStrategyListener.h" #include "common/net/Client.h" #include "common/net/strategies/SinglePoolStrategy.h" #include "common/Platform.h" -#include "interfaces/IStrategyListener.h" SinglePoolStrategy::SinglePoolStrategy(const Pool &pool, int retryPause, int retries, IStrategyListener *listener, bool quiet) : diff --git a/src/common/net/strategies/SinglePoolStrategy.h b/src/common/net/strategies/SinglePoolStrategy.h index 41d90e34..1a48d678 100644 --- a/src/common/net/strategies/SinglePoolStrategy.h +++ b/src/common/net/strategies/SinglePoolStrategy.h @@ -25,8 +25,8 @@ #define __SINGLEPOOLSTRATEGY_H__ -#include "interfaces/IClientListener.h" -#include "interfaces/IStrategy.h" +#include "common/interfaces/IClientListener.h" +#include "common/interfaces/IStrategy.h" class Client; diff --git a/src/core/ConfigCreator.h b/src/core/ConfigCreator.h index fcc6c596..054eb78c 100644 --- a/src/core/ConfigCreator.h +++ b/src/core/ConfigCreator.h @@ -24,8 +24,8 @@ #define __CONFIGCREATOR_H__ +#include "common/interfaces/IConfigCreator.h" #include "core/Config.h" -#include "interfaces/IConfigCreator.h" namespace xmrig { diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index 9704d5e3..bc6657d1 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -33,8 +33,8 @@ #endif +#include "common/interfaces/IConfig.h" #include "version.h" -#include "interfaces/IConfig.h" namespace xmrig { diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index ac64f173..ce73f037 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -26,6 +26,7 @@ #include "common/config/ConfigLoader.h" +#include "common/interfaces/IControllerListener.h" #include "common/log/ConsoleLog.h" #include "common/log/FileLog.h" #include "common/log/Log.h" @@ -33,7 +34,6 @@ #include "core/Config.h" #include "core/Controller.h" #include "Cpu.h" -#include "interfaces/IControllerListener.h" #include "net/Network.h" diff --git a/src/core/Controller.h b/src/core/Controller.h index 25f91843..2c66af53 100644 --- a/src/core/Controller.h +++ b/src/core/Controller.h @@ -25,7 +25,7 @@ #define __CONTROLLER_H__ -#include "interfaces/IWatcherListener.h" +#include "common/interfaces/IWatcherListener.h" class Network; diff --git a/src/net/Network.h b/src/net/Network.h index 353edc77..51e95d6d 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -30,8 +30,8 @@ #include "api/NetworkState.h" +#include "common/interfaces/IStrategyListener.h" #include "interfaces/IJobResultListener.h" -#include "interfaces/IStrategyListener.h" class IStrategy; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 7de776ef..4d353655 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -23,13 +23,13 @@ #include "common/crypto/keccak.h" +#include "common/interfaces/IStrategyListener.h" #include "common/net/Client.h" #include "common/net/Job.h" #include "common/net/strategies/FailoverStrategy.h" #include "common/net/strategies/SinglePoolStrategy.h" #include "common/Platform.h" #include "common/xmrig.h" -#include "interfaces/IStrategyListener.h" #include "net/strategies/DonateStrategy.h" diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 66c0375c..95ff6608 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -30,9 +30,9 @@ #include "common/net/Pool.h" -#include "interfaces/IClientListener.h" -#include "interfaces/IStrategy.h" -#include "interfaces/IStrategyListener.h" +#include "common/interfaces/IClientListener.h" +#include "common/interfaces/IStrategy.h" +#include "common/interfaces/IStrategyListener.h" class Client; From dba78717fa10909bc0f3e111caf2f64cbdc5167f Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 5 Jun 2018 11:48:31 +0700 Subject: [PATCH 289/389] Move dry-run option to common code. --- src/common/config/CommonConfig.cpp | 8 +++++++ src/common/config/CommonConfig.h | 2 ++ src/core/Config.cpp | 36 +++++++++++++----------------- src/core/Config.h | 2 -- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 668bf0d8..315b09d6 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -44,6 +44,7 @@ xmrig::CommonConfig::CommonConfig() : m_apiRestricted(true), m_background(false), m_colors(true), + m_dryRun(false), m_syslog(false), # ifdef XMRIG_PROXY_PROJECT @@ -172,9 +173,15 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable) case ApiIPv6Key: /* ipv6 */ m_apiIPv6 = enable; + break; case ApiRestrictedKey: /* restricted */ m_apiRestricted = enable; + break; + + case IConfig::DryRunKey: /* --dry-run */ + m_dryRun = enable; + break; default: break; @@ -259,6 +266,7 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) case KeepAliveKey: /* --keepalive */ case NicehashKey: /* --nicehash */ case ApiIPv6Key: /* --api-ipv6 */ + case DryRunKey: /* --dry-run */ return parseBoolean(key, true); case ColorKey: /* --no-color */ diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 2a329c7e..ffebb6b2 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -47,6 +47,7 @@ public: inline bool isApiRestricted() const { return m_apiRestricted; } inline bool isBackground() const { return m_background; } inline bool isColors() const { return m_colors; } + inline bool isDryRun() const { return m_dryRun; } inline bool isSyslog() const { return m_syslog; } inline const char *apiToken() const { return m_apiToken.data(); } inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } @@ -85,6 +86,7 @@ protected: bool m_apiRestricted; bool m_background; bool m_colors; + bool m_dryRun; bool m_syslog; bool m_watch; int m_apiPort; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 232e7024..fa6afdb4 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -43,7 +43,6 @@ static char affinity_tmp[20] = { 0 }; xmrig::Config::Config() : xmrig::CommonConfig(), m_aesMode(AES_AUTO), m_algoVariant(AV_AUTO), - m_dryRun(false), m_hugePages(true), m_safe(false), m_maxCpuUsage(75), @@ -192,19 +191,15 @@ bool xmrig::Config::parseBoolean(int key, bool enable) } switch (key) { - case IConfig::SafeKey: /* --safe */ + case SafeKey: /* --safe */ m_safe = enable; break; - case IConfig::HugePagesKey: /* --no-huge-pages */ + case HugePagesKey: /* --no-huge-pages */ m_hugePages = enable; break; - case IConfig::DryRunKey: /* --dry-run */ - m_dryRun = enable; - break; - - case IConfig::HardwareAESKey: /* hw-aes config only */ + case HardwareAESKey: /* hw-aes config only */ m_aesMode = enable ? AES_HW : AES_SOFT; break; @@ -223,19 +218,18 @@ bool xmrig::Config::parseString(int key, const char *arg) } switch (key) { - case xmrig::IConfig::AVKey: /* --av */ - case xmrig::IConfig::MaxCPUUsageKey: /* --max-cpu-usage */ - case xmrig::IConfig::CPUPriorityKey: /* --cpu-priority */ + case AVKey: /* --av */ + case MaxCPUUsageKey: /* --max-cpu-usage */ + case CPUPriorityKey: /* --cpu-priority */ return parseUint64(key, strtol(arg, nullptr, 10)); - case xmrig::IConfig::SafeKey: /* --safe */ - case xmrig::IConfig::DryRunKey: /* --dry-run */ + case SafeKey: /* --safe */ return parseBoolean(key, true); - case xmrig::IConfig::HugePagesKey: /* --no-huge-pages */ + case HugePagesKey: /* --no-huge-pages */ return parseBoolean(key, false); - case xmrig::IConfig::ThreadsKey: /* --threads */ + case ThreadsKey: /* --threads */ if (strncmp(arg, "all", 3) == 0) { m_threads.count = Cpu::threads(); return true; @@ -243,7 +237,7 @@ bool xmrig::Config::parseString(int key, const char *arg) return parseUint64(key, strtol(arg, nullptr, 10)); - case xmrig::IConfig::CPUAffinityKey: /* --cpu-affinity */ + case CPUAffinityKey: /* --cpu-affinity */ { const char *p = strstr(arg, "0x"); return parseUint64(key, p ? strtoull(p, nullptr, 16) : strtoull(arg, nullptr, 10)); @@ -264,7 +258,7 @@ bool xmrig::Config::parseUint64(int key, uint64_t arg) } switch (key) { - case xmrig::IConfig::CPUAffinityKey: /* --cpu-affinity */ + case CPUAffinityKey: /* --cpu-affinity */ if (arg) { m_threads.mask = arg; } @@ -303,25 +297,25 @@ void xmrig::Config::parseJSON(const rapidjson::Document &doc) bool xmrig::Config::parseInt(int key, int arg) { switch (key) { - case xmrig::IConfig::ThreadsKey: /* --threads */ + case ThreadsKey: /* --threads */ if (arg >= 0 && arg < 1024) { m_threads.count = arg; } break; - case xmrig::IConfig::AVKey: /* --av */ + case AVKey: /* --av */ if (arg >= AV_AUTO && arg < AV_MAX) { m_algoVariant = static_cast(arg); } break; - case xmrig::IConfig::MaxCPUUsageKey: /* --max-cpu-usage */ + case MaxCPUUsageKey: /* --max-cpu-usage */ if (m_maxCpuUsage > 0 && arg <= 100) { m_maxCpuUsage = arg; } break; - case xmrig::IConfig::CPUPriorityKey: /* --cpu-priority */ + case CPUPriorityKey: /* --cpu-priority */ if (arg >= 0 && arg <= 5) { m_priority = arg; } diff --git a/src/core/Config.h b/src/core/Config.h index 0c6a2173..f0f1404f 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -77,7 +77,6 @@ public: inline AesMode aesMode() const { return m_aesMode; } inline AlgoVariant algoVariant() const { return m_algoVariant; } - inline bool isDryRun() const { return m_dryRun; } inline bool isHugePages() const { return m_hugePages; } inline const std::vector &threads() const { return m_threads.list; } inline int priority() const { return m_priority; } @@ -117,7 +116,6 @@ private: AesMode m_aesMode; AlgoVariant m_algoVariant; - bool m_dryRun; bool m_hugePages; bool m_safe; int m_maxCpuUsage; From 974cb4162accd74836effe314c83d8ee62664605 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 7 Jun 2018 07:17:30 +0700 Subject: [PATCH 290/389] Fix Linux build. --- src/common/log/SysLog.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/log/SysLog.h b/src/common/log/SysLog.h index 1e49a48b..5cfeefcd 100644 --- a/src/common/log/SysLog.h +++ b/src/common/log/SysLog.h @@ -25,7 +25,7 @@ #define __SYSLOG_H__ -#include "interfaces/ILogBackend.h" +#include "common/interfaces/ILogBackend.h" class SysLog : public ILogBackend From 8908c2c027ec4adcb6fecf80b401bd4435a635cc Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 10 Jun 2018 19:48:34 +0700 Subject: [PATCH 291/389] Added support for "cn/msr" also known as "cn-fast". --- src/common/crypto/Algorithm.cpp | 18 ++++----- src/common/net/Job.cpp | 10 ++--- src/common/net/Pool.cpp | 14 +++---- src/common/xmrig.h | 7 +++- src/crypto/CryptoNight_constants.h | 19 +++++++--- src/crypto/CryptoNight_test.h | 15 ++++++++ src/crypto/CryptoNight_x86.h | 20 +++++----- src/workers/CpuThread.cpp | 60 ++++++++++++++++++++++++------ src/workers/MultiWorker.cpp | 47 +++++++++++------------ src/workers/MultiWorker.h | 1 + 10 files changed, 136 insertions(+), 75 deletions(-) diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index f3f4c2f4..fbfd0231 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -57,6 +57,7 @@ static AlgoData const algorithms[] = { { "cryptonight/0", "cn/0", xmrig::CRYPTONIGHT, xmrig::VARIANT_0 }, { "cryptonight/1", "cn/1", xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, { "cryptonight/xtl", "cn/xtl", xmrig::CRYPTONIGHT, xmrig::VARIANT_XTL }, + { "cryptonight/msr", "cn/msr", xmrig::CRYPTONIGHT, xmrig::VARIANT_MSR }, # ifndef XMRIG_NO_AEON { "cryptonight-lite", "cn-lite", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, @@ -68,6 +69,8 @@ static AlgoData const algorithms[] = { # ifndef XMRIG_NO_SUMO { "cryptonight-heavy", "cn-heavy", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_0 }, + { "cryptonight-heavy/0", "cn-heavy/0", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_0 }, + { "cryptonight-heavy/xhv", "cn-heavy/xhv", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_XHV }, # endif }; @@ -89,7 +92,9 @@ static const char *variants[] = { "0", "1", "ipbc", - "xtl" + "xtl", + "msr", + "xhv" }; @@ -145,11 +150,6 @@ void xmrig::Algorithm::parseAlgorithm(const char *algo) void xmrig::Algorithm::parseVariant(const char *variant) { - if (m_algo == CRYPTONIGHT_HEAVY) { - m_variant = VARIANT_0; - return; - } - m_variant = VARIANT_AUTO; for (size_t i = 0; i < ARRAY_SIZE(variants); i++) { @@ -163,7 +163,7 @@ void xmrig::Algorithm::parseVariant(const char *variant) void xmrig::Algorithm::parseVariant(int variant) { - if (variant >= VARIANT_AUTO && variant <= VARIANT_XTL) { + if (variant >= VARIANT_AUTO && variant < VARIANT_MAX) { m_variant = static_cast(variant); } else { @@ -175,10 +175,6 @@ void xmrig::Algorithm::parseVariant(int variant) void xmrig::Algorithm::setAlgo(Algo algo) { m_algo = algo; - - if (m_algo == CRYPTONIGHT_HEAVY) { - m_variant = VARIANT_0; - } } diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index 81c3b8f8..9a7ede8e 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -173,12 +173,12 @@ xmrig::Variant Job::variant() const return xmrig::VARIANT_1; } - if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { - if (m_algorithm.algo() == xmrig::CRYPTONIGHT) { - return xmrig::VARIANT_1; - } + if (m_algorithm.variant() == xmrig::VARIANT_MSR && m_blob[0] < 7) { + return xmrig::VARIANT_1; + } - return (m_blob[0] > 6 ? xmrig::VARIANT_1 : xmrig::VARIANT_0); + if (m_algorithm.variant() == xmrig::VARIANT_AUTO && m_algorithm.algo() != xmrig::CRYPTONIGHT_HEAVY) { + return xmrig::VARIANT_1; } return m_algorithm.variant(); diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index d5943545..006d76a1 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -321,12 +321,12 @@ void Pool::rebuild() m_algorithms.push_back(m_algorithm); # ifndef XMRIG_PROXY_PROJECT - if (m_algorithm.algo() != xmrig::CRYPTONIGHT_HEAVY) { - addVariant(xmrig::VARIANT_1); - addVariant(xmrig::VARIANT_0); - addVariant(xmrig::VARIANT_XTL); - addVariant(xmrig::VARIANT_IPBC); - addVariant(xmrig::VARIANT_AUTO); - } + addVariant(xmrig::VARIANT_1); + addVariant(xmrig::VARIANT_0); + addVariant(xmrig::VARIANT_XTL); + addVariant(xmrig::VARIANT_IPBC); + addVariant(xmrig::VARIANT_MSR); + addVariant(xmrig::VARIANT_XHV); + addVariant(xmrig::VARIANT_AUTO); # endif } diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 43a2a252..170f4832 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -61,8 +61,11 @@ enum Variant { VARIANT_AUTO = -1, // Autodetect VARIANT_0 = 0, // Original CryptoNight or CryptoNight-Heavy VARIANT_1 = 1, // CryptoNight variant 1 also known as Monero7 and CryptoNightV7 - VARIANT_IPBC = 2, // CryptoNight Lite variant 1 with XOR (IPBC only) - VARIANT_XTL = 3 // CryptoNight variant 1 (Stellite only) + VARIANT_IPBC = 2, // Modified CryptoNight Lite variant 1 with XOR (IPBC/TUBE only) + VARIANT_XTL = 3, // Modified CryptoNight variant 1 (Stellite only) + VARIANT_MSR = 4, // Modified CryptoNight variant 1 (Masari only) + VARIANT_XHV = 5, // Modified CryptoNight-Heavy (Haven Protocol only) + VARIANT_MAX }; diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h index 7e6627fd..73f3e958 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/CryptoNight_constants.h @@ -38,6 +38,7 @@ namespace xmrig constexpr const size_t CRYPTONIGHT_MEMORY = 2 * 1024 * 1024; constexpr const uint32_t CRYPTONIGHT_MASK = 0x1FFFF0; constexpr const uint32_t CRYPTONIGHT_ITER = 0x80000; +constexpr const uint32_t CRYPTONIGHT_MSR_ITER = 0x40000; constexpr const size_t CRYPTONIGHT_LITE_MEMORY = 1 * 1024 * 1024; constexpr const uint32_t CRYPTONIGHT_LITE_MASK = 0xFFFF0; @@ -102,18 +103,24 @@ inline uint32_t cn_select_mask(Algo algorithm) } -template inline constexpr uint32_t cn_select_iter() { return 0; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } +template inline constexpr uint32_t cn_select_iter() { return 0; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_MSR_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } -inline uint32_t cn_select_iter(Algo algorithm) +inline uint32_t cn_select_iter(Algo algorithm, Variant variant) { switch(algorithm) { case CRYPTONIGHT: - return CRYPTONIGHT_ITER; + return variant == VARIANT_MSR ? CRYPTONIGHT_MSR_ITER : CRYPTONIGHT_ITER; case CRYPTONIGHT_LITE: return CRYPTONIGHT_LITE_ITER; diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index bf6013e8..8d8fe455 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -99,6 +99,21 @@ const static uint8_t test_output_xtl[160] = { }; +// Masari (MSR) +const static uint8_t test_output_msr[160] = { + 0x3C, 0x7A, 0x61, 0x08, 0x4C, 0x5E, 0xB8, 0x65, 0xB4, 0x98, 0xAB, 0x2F, 0x5A, 0x1A, 0xC5, 0x2C, + 0x49, 0xC1, 0x77, 0xC2, 0xD0, 0x13, 0x34, 0x42, 0xD6, 0x5E, 0xD5, 0x14, 0x33, 0x5C, 0x82, 0xC5, + 0x69, 0xDF, 0x38, 0x51, 0x1B, 0xB3, 0xEB, 0x7D, 0xE7, 0x6B, 0x08, 0x8E, 0xB6, 0x7E, 0xB7, 0x1C, + 0x5F, 0x3C, 0x81, 0xC9, 0xF7, 0xCE, 0xAE, 0x28, 0xC0, 0xFE, 0xEB, 0xBA, 0x0B, 0x40, 0x38, 0x1D, + 0x44, 0xD0, 0xD5, 0xD3, 0x98, 0x1F, 0xA3, 0x0E, 0xE9, 0x89, 0x1A, 0xD7, 0x88, 0xCC, 0x25, 0x76, + 0x9C, 0xFF, 0x4D, 0x7F, 0x9C, 0xCF, 0x48, 0x07, 0x91, 0xF9, 0x82, 0xF5, 0x4C, 0xE9, 0xBD, 0x82, + 0x36, 0x36, 0x64, 0x14, 0xED, 0xB8, 0x54, 0xEE, 0x22, 0xA1, 0x66, 0xA3, 0x87, 0x10, 0x76, 0x1F, + 0x5A, 0xCD, 0x4C, 0x31, 0x4C, 0xBA, 0x41, 0xD2, 0xDB, 0x6C, 0x31, 0x2E, 0x7A, 0x64, 0x15, 0xFF, + 0xA6, 0xD9, 0xB9, 0x7D, 0x1C, 0x3C, 0x98, 0xDD, 0x16, 0xE6, 0xD3, 0xAA, 0xEF, 0xB6, 0xB3, 0x53, + 0x74, 0xD1, 0xAC, 0x5C, 0x04, 0x26, 0x7D, 0x71, 0xDE, 0xAB, 0x66, 0x28, 0x91, 0x3A, 0x6F, 0x4F +}; + + #ifndef XMRIG_NO_AEON const static uint8_t test_output_v0_lite[160] = { 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 66bcf8b5..3aa2a927 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -403,11 +403,11 @@ static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) } -template +template inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); - constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); if (VARIANT > 0 && size < 43) { @@ -493,11 +493,11 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } -template +template inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); - constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); if (VARIANT > 0 && size < 43) { @@ -700,11 +700,11 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si } -template +template inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); - constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); if (VARIANT > 0 && size < 43) { @@ -790,11 +790,11 @@ inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t si } -template +template inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); - constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); if (VARIANT > 0 && size < 43) { @@ -896,11 +896,11 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size } -template +template inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); - constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); if (VARIANT > 0 && size < 43) { diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index e42139c0..926a9ba3 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -62,9 +62,15 @@ bool xmrig::CpuThread::isSoftAES(AlgoVariant av) xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant av, Variant variant) { - assert(variant == VARIANT_0 || variant == VARIANT_1 || variant == VARIANT_IPBC || variant == VARIANT_XTL); + assert(variant == VARIANT_0 || + variant == VARIANT_1 || + variant == VARIANT_IPBC || + variant == VARIANT_XTL || + variant == VARIANT_MSR || + variant == VARIANT_XHV + ); - static const cn_hash_fun func_table[90] = { + static const cn_hash_fun func_table[180] = { cryptonight_single_hash, cryptonight_double_hash, cryptonight_single_hash, @@ -87,7 +93,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_quad_hash, cryptonight_penta_hash, - nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_IPBC cryptonight_single_hash, cryptonight_double_hash, @@ -100,6 +106,19 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_quad_hash, cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XHV + # ifndef XMRIG_NO_AEON cryptonight_single_hash, cryptonight_double_hash, @@ -134,12 +153,16 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_quad_hash, cryptonight_penta_hash, - nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XTL + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_MSR + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XHV # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif # ifndef XMRIG_NO_SUMO @@ -153,18 +176,33 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_triple_hash, cryptonight_quad_hash, cryptonight_penta_hash, + + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1 + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_IPBC + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XTL + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_MSR + + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif }; -# ifndef XMRIG_NO_SUMO - if (algorithm == CRYPTONIGHT_HEAVY) { - variant = VARIANT_0; - } -# endif - - const size_t index = 40 * algorithm + 10 * variant + av - 1; + const size_t index = VARIANT_MAX * 10 * algorithm + 10 * variant + av - 1; # ifndef NDEBUG cn_hash_fun func = func_table[index]; diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index c0f88446..f3acf7f8 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -50,36 +50,23 @@ MultiWorker::~MultiWorker() template bool MultiWorker::selfTest() { - if (m_thread->fn(xmrig::VARIANT_0) == nullptr) { - return false; - } - - m_thread->fn(xmrig::VARIANT_0)(test_input, 76, m_hash, m_ctx); - - if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_hash, test_output_v0, sizeof m_hash) == 0) { - m_thread->fn(xmrig::VARIANT_1)(test_input, 76, m_hash, m_ctx); - if (memcmp(m_hash, test_output_v1, sizeof m_hash) != 0) { - return false; - } - - m_thread->fn(xmrig::VARIANT_XTL)(test_input, 76, m_hash, m_ctx); - return memcmp(m_hash, test_output_xtl, sizeof m_hash) == 0; + if (m_thread->algorithm() == xmrig::CRYPTONIGHT) { + return verify(xmrig::VARIANT_0, test_output_v0) && + verify(xmrig::VARIANT_1, test_output_v1) && + verify(xmrig::VARIANT_XTL, test_output_xtl) && + verify(xmrig::VARIANT_MSR, test_output_msr); } # ifndef XMRIG_NO_AEON - if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_hash, test_output_v0_lite, sizeof m_hash) == 0) { - m_thread->fn(xmrig::VARIANT_1)(test_input, 76, m_hash, m_ctx); - if (memcmp(m_hash, test_output_v1_lite, sizeof m_hash) != 0) { - return false; - } - - m_thread->fn(xmrig::VARIANT_IPBC)(test_input, 76, m_hash, m_ctx); - return memcmp(m_hash, test_output_ipbc_lite, sizeof m_hash) == 0; + if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE) { + return verify(xmrig::VARIANT_0, test_output_v0_lite) && + verify(xmrig::VARIANT_1, test_output_v1_lite) && + verify(xmrig::VARIANT_IPBC, test_output_ipbc_lite); } # endif # ifndef XMRIG_NO_SUMO - return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_hash, test_output_heavy, sizeof m_hash) == 0; + return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && verify(xmrig::VARIANT_0, test_output_heavy); # else return false; # endif @@ -140,6 +127,20 @@ bool MultiWorker::resume(const Job &job) } +template +bool MultiWorker::verify(xmrig::Variant variant, const uint8_t *referenceValue) +{ + + xmrig::CpuThread::cn_hash_fun func = m_thread->fn(variant); + if (!func) { + return false; + } + + func(test_input, 76, m_hash, m_ctx); + return memcmp(m_hash, referenceValue, sizeof m_hash) == 0; +} + + template void MultiWorker::consumeJob() { diff --git a/src/workers/MultiWorker.h b/src/workers/MultiWorker.h index d89bbb33..d4a6910e 100644 --- a/src/workers/MultiWorker.h +++ b/src/workers/MultiWorker.h @@ -48,6 +48,7 @@ protected: private: bool resume(const Job &job); + bool verify(xmrig::Variant variant, const uint8_t *referenceValue); void consumeJob(); void save(const Job &job); From dab4239aca670a844dac383adcd6fc5e2d343ad2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 11 Jun 2018 12:00:59 +0700 Subject: [PATCH 292/389] Added support for cn-heavy/xhv. --- src/common/net/Job.cpp | 12 ++--- src/crypto/CryptoNight_arm.h | 44 +++++++++++++----- src/crypto/CryptoNight_constants.h | 15 ++++++ src/crypto/CryptoNight_monero.h | 8 ++-- src/crypto/CryptoNight_test.h | 15 +++++- src/crypto/CryptoNight_x86.h | 74 ++++++++++++++++++++---------- src/workers/MultiWorker.cpp | 9 ++-- 7 files changed, 128 insertions(+), 49 deletions(-) diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index 9a7ede8e..80b521ea 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -165,10 +165,6 @@ bool Job::setTarget(const char *target) xmrig::Variant Job::variant() const { - if (m_algorithm.algo() == xmrig::CRYPTONIGHT_HEAVY) { - return xmrig::VARIANT_0; - } - if (m_algorithm.variant() == xmrig::VARIANT_XTL && m_blob[0] < 4) { return xmrig::VARIANT_1; } @@ -177,8 +173,12 @@ xmrig::Variant Job::variant() const return xmrig::VARIANT_1; } - if (m_algorithm.variant() == xmrig::VARIANT_AUTO && m_algorithm.algo() != xmrig::CRYPTONIGHT_HEAVY) { - return xmrig::VARIANT_1; + if (m_algorithm.variant() == xmrig::VARIANT_XHV && m_blob[0] < 3) { + return xmrig::VARIANT_0; + } + + if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { + return m_algorithm.algo() == xmrig::CRYPTONIGHT_HEAVY ? xmrig::VARIANT_0 : xmrig::VARIANT_1; } return m_algorithm.variant(); diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index fd5bcb7b..46e1f530 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -402,10 +402,11 @@ template inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); - constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); + constexpr bool IS_MONERO = xmrig::cn_is_monero(); - if (VARIANT > 0 && size < 43) { + if (IS_MONERO && size < 43) { memset(output, 0, 32); return; } @@ -436,7 +437,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); } - if (VARIANT > 0) { + if (IS_MONERO) { cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } else { _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); @@ -455,7 +456,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; - if (VARIANT > 0) { + if (IS_MONERO) { if (VARIANT == xmrig::VARIANT_IPBC) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } @@ -478,7 +479,13 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si const int64_t q = n / (d | 0x5); ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; - idx0 = d ^ q; + + if (VARIANT == xmrig::VARIANT_XHV) { + idx0 = (~d) ^ q; + } + else { + idx0 = d ^ q; + } } } @@ -493,10 +500,11 @@ template inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); - constexpr size_t ITERATIONS = xmrig::cn_select_iter(); + constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); + constexpr bool IS_MONERO = xmrig::cn_is_monero(); - if (VARIANT > 0 && size < 43) { + if (IS_MONERO && size < 43) { memset(output, 0, 64); return; } @@ -540,7 +548,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); } - if (VARIANT > 0) { + if (IS_MONERO) { cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); } else { @@ -564,7 +572,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; - if (VARIANT > 0) { + if (IS_MONERO) { if (VARIANT == xmrig::VARIANT_IPBC) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } @@ -587,7 +595,13 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si const int64_t q = n / (d | 0x5); ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; - idx0 = d ^ q; + + if (VARIANT == xmrig::VARIANT_XHV) { + idx0 = (~d) ^ q; + } + else { + idx0 = d ^ q; + } } cl = ((uint64_t*) &l1[idx1 & MASK])[0]; @@ -599,7 +613,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l1[idx1 & MASK])[0] = al1; - if (VARIANT > 0) { + if (IS_MONERO) { if (VARIANT == xmrig::VARIANT_IPBC) { ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; } @@ -622,7 +636,13 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si const int64_t q = n / (d | 0x5); ((int64_t*)&l1[idx1 & MASK])[0] = n ^ q; - idx1 = d ^ q; + + if (VARIANT == xmrig::VARIANT_XHV) { + idx1 = (~d) ^ q; + } + else { + idx1 = d ^ q; + } } } diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h index 73f3e958..85a2ab85 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/CryptoNight_constants.h @@ -136,6 +136,21 @@ inline uint32_t cn_select_iter(Algo algorithm, Variant variant) } +template inline constexpr bool cn_is_monero() { return false; } +template<> inline constexpr bool cn_is_monero() { return false; } +template<> inline constexpr bool cn_is_monero() { return true; } +template<> inline constexpr bool cn_is_monero() { return true; } +template<> inline constexpr bool cn_is_monero() { return true; } +template<> inline constexpr bool cn_is_monero() { return true; } +template<> inline constexpr bool cn_is_monero() { return false; } + + +inline bool cn_is_monero(Variant variant) +{ + return variant > VARIANT_0 && variant < VARIANT_XHV; +} + + } /* namespace xmrig */ diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index 28572d88..a758fdbc 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -30,21 +30,21 @@ #ifndef XMRIG_ARM # define VARIANT1_INIT(part) \ uint64_t tweak1_2_##part = 0; \ - if (VARIANT > 0) { \ + if (IS_MONERO) { \ tweak1_2_##part = (*reinterpret_cast(input + 35 + part * size) ^ \ *(reinterpret_cast(ctx[part]->state) + 24)); \ } #else # define VARIANT1_INIT(part) \ uint64_t tweak1_2_##part = 0; \ - if (VARIANT > 0) { \ + if (IS_MONERO) { \ memcpy(&tweak1_2_##part, input + 35 + part * size, sizeof tweak1_2_##part); \ tweak1_2_##part ^= *(reinterpret_cast(ctx[part]->state) + 24); \ } #endif #define VARIANT1_1(p) \ - if (VARIANT > 0) { \ + if (IS_MONERO) { \ const uint8_t tmp = reinterpret_cast(p)[11]; \ static const uint32_t table = 0x75310; \ const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ @@ -52,7 +52,7 @@ } #define VARIANT1_2(p, part) \ - if (VARIANT > 0) { \ + if (IS_MONERO) { \ (p) ^= tweak1_2_##part; \ } diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 8d8fe455..187ba07a 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -161,7 +161,7 @@ const static uint8_t test_output_ipbc_lite[160] = { #ifndef XMRIG_NO_SUMO -const static uint8_t test_output_heavy[160] = { +const static uint8_t test_output_v0_heavy[160] = { 0x99, 0x83, 0xF2, 0x1B, 0xDF, 0x20, 0x10, 0xA8, 0xD7, 0x07, 0xBB, 0x2F, 0x14, 0xD7, 0x86, 0x64, 0xBB, 0xE1, 0x18, 0x7F, 0x55, 0x01, 0x4B, 0x39, 0xE5, 0xF3, 0xD6, 0x93, 0x28, 0xE4, 0x8F, 0xC2, 0x4D, 0x94, 0x7D, 0xD6, 0xDB, 0x6E, 0x07, 0x48, 0x26, 0x4A, 0x51, 0x2E, 0xAC, 0xF3, 0x25, 0x4A, @@ -173,6 +173,19 @@ const static uint8_t test_output_heavy[160] = { 0x53, 0x62, 0x0A, 0x54, 0x7D, 0x43, 0xEA, 0x18, 0x94, 0xED, 0xD8, 0x92, 0x06, 0x6A, 0xA1, 0x51, 0xAD, 0xB1, 0xFD, 0x89, 0xFB, 0x5C, 0xB4, 0x25, 0x6A, 0xDD, 0xB0, 0x09, 0xC5, 0x72, 0x87, 0xEB }; + +const static uint8_t test_output_xhv_heavy[160] = { + 0x5A, 0xC3, 0xF7, 0x85, 0xC4, 0x90, 0xC5, 0x85, 0x50, 0xEC, 0x95, 0xD2, 0x72, 0x65, 0x63, 0x57, + 0x7E, 0x7C, 0x1C, 0x21, 0x2D, 0x0C, 0xDE, 0x59, 0x12, 0x73, 0x20, 0x1E, 0x44, 0xFD, 0xD5, 0xB6, + 0x1F, 0x4E, 0xB2, 0x0A, 0x36, 0x51, 0x4B, 0xF5, 0x4D, 0xC9, 0xE0, 0x90, 0x2C, 0x16, 0x47, 0x3F, + 0xDE, 0x18, 0x29, 0x8E, 0xBB, 0x34, 0x2B, 0xEF, 0x7A, 0x04, 0x22, 0xD1, 0xB1, 0xF2, 0x48, 0xDA, + 0xE3, 0x7F, 0x4B, 0x4C, 0xB4, 0xDF, 0xE8, 0xD3, 0x70, 0xE2, 0xE7, 0x44, 0x25, 0x87, 0x12, 0xF9, + 0x8F, 0x28, 0x0B, 0xCE, 0x2C, 0xEE, 0xDD, 0x88, 0x94, 0x35, 0x48, 0x51, 0xAE, 0xC8, 0x9C, 0x0B, + 0xED, 0x2F, 0xE6, 0x0F, 0x39, 0x05, 0xB4, 0x4A, 0x8F, 0x38, 0x44, 0x2D, 0x4B, 0xE9, 0x7B, 0x81, + 0xC6, 0xB0, 0xE0, 0x0A, 0x39, 0x8C, 0x38, 0xFE, 0x63, 0x31, 0x47, 0x65, 0x0D, 0x2B, 0xF4, 0x96, + 0x13, 0x91, 0x89, 0xB4, 0x5B, 0xA9, 0x2A, 0x7A, 0x09, 0x65, 0x14, 0x20, 0x76, 0x24, 0x6C, 0x80, + 0x1D, 0x3F, 0x9F, 0xCD, 0x68, 0x39, 0xA9, 0x42, 0x27, 0xC1, 0x0C, 0x53, 0x98, 0x35, 0x60, 0x7A +}; #endif diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 3aa2a927..8ab65023 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -409,8 +409,9 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); + constexpr bool IS_MONERO = xmrig::cn_is_monero(); - if (VARIANT > 0 && size < 43) { + if (IS_MONERO && size < 43) { memset(output, 0, 32); return; } @@ -441,7 +442,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); } - if (VARIANT > 0) { + if (IS_MONERO) { cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } else { _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); @@ -460,7 +461,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; - if (VARIANT > 0) { + if (IS_MONERO) { if (VARIANT == xmrig::VARIANT_IPBC) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } @@ -477,12 +478,18 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si idx0 = al0; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; - int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; + int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; + int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; int64_t q = n / (d | 0x5); ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; - idx0 = d ^ q; + + if (VARIANT == xmrig::VARIANT_XHV) { + idx0 = (~d) ^ q; + } + else { + idx0 = d ^ q; + } } } @@ -499,8 +506,9 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); + constexpr bool IS_MONERO = xmrig::cn_is_monero(); - if (VARIANT > 0 && size < 43) { + if (IS_MONERO && size < 43) { memset(output, 0, 64); return; } @@ -544,7 +552,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); } - if (VARIANT > 0) { + if (IS_MONERO) { cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); } else { @@ -568,7 +576,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; - if (VARIANT > 0) { + if (IS_MONERO) { if (VARIANT == xmrig::VARIANT_IPBC) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } @@ -585,12 +593,18 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si idx0 = al0; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; - int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; + int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; + int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; int64_t q = n / (d | 0x5); ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; - idx0 = d ^ q; + + if (VARIANT == xmrig::VARIANT_XHV) { + idx0 = (~d) ^ q; + } + else { + idx0 = d ^ q; + } } cl = ((uint64_t*) &l1[idx1 & MASK])[0]; @@ -602,7 +616,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l1[idx1 & MASK])[0] = al1; - if (VARIANT > 0) { + if (IS_MONERO) { if (VARIANT == xmrig::VARIANT_IPBC) { ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; } @@ -619,12 +633,18 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si idx1 = al1; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)&l1[idx1 & MASK])[0]; - int32_t d = ((int32_t*)&l1[idx1 & MASK])[2]; + int64_t n = ((int64_t*)&l1[idx1 & MASK])[0]; + int32_t d = ((int32_t*)&l1[idx1 & MASK])[2]; int64_t q = n / (d | 0x5); ((int64_t*)&l1[idx1 & MASK])[0] = n ^ q; - idx1 = d ^ q; + + if (VARIANT == xmrig::VARIANT_XHV) { + idx1 = (~d) ^ q; + } + else { + idx1 = d ^ q; + } } } @@ -653,7 +673,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si \ b = _mm_xor_si128(b, c); \ \ - if (VARIANT > 0) { \ + if (IS_MONERO) { \ cryptonight_monero_tweak(reinterpret_cast(ptr), b); \ } else { \ _mm_store_si128(ptr, b); \ @@ -670,7 +690,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si lo = __umul128(idx, EXTRACT64(b), &hi); \ a = _mm_add_epi64(a, _mm_set_epi64x(lo, hi)); \ \ - if (VARIANT > 0) { \ + if (IS_MONERO) { \ _mm_store_si128(ptr, _mm_xor_si128(a, mc)); \ \ if (VARIANT == xmrig::VARIANT_IPBC) { \ @@ -688,13 +708,18 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si int32_t d = ((int32_t*)&l[idx & MASK])[2]; \ int64_t q = n / (d | 0x5); \ ((int64_t*)&l[idx & MASK])[0] = n ^ q; \ - idx = d ^ q; \ + if (VARIANT == xmrig::VARIANT_XHV) { \ + idx = (~d) ^ q; \ + } \ + else { \ + idx = d ^ q; \ + } \ } #define CONST_INIT(ctx, n) \ __m128i mc##n; \ - if (VARIANT > 0) { \ + if (IS_MONERO) { \ mc##n = _mm_set_epi64x(*reinterpret_cast(input + n * size + 35) ^ \ *(reinterpret_cast((ctx)->state) + 24), 0); \ } @@ -706,8 +731,9 @@ inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); + constexpr bool IS_MONERO = xmrig::cn_is_monero(); - if (VARIANT > 0 && size < 43) { + if (IS_MONERO && size < 43) { memset(output, 0, 32 * 3); return; } @@ -796,8 +822,9 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); + constexpr bool IS_MONERO = xmrig::cn_is_monero(); - if (VARIANT > 0 && size < 43) { + if (IS_MONERO && size < 43) { memset(output, 0, 32 * 4); return; } @@ -902,8 +929,9 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); + constexpr bool IS_MONERO = xmrig::cn_is_monero(); - if (VARIANT > 0 && size < 43) { + if (IS_MONERO && size < 43) { memset(output, 0, 32 * 5); return; } diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index f3acf7f8..73f0dba7 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -66,10 +66,13 @@ bool MultiWorker::selfTest() # endif # ifndef XMRIG_NO_SUMO - return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && verify(xmrig::VARIANT_0, test_output_heavy); -# else - return false; + if (m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY) { + return verify(xmrig::VARIANT_0, test_output_v0_heavy) && + verify(xmrig::VARIANT_XHV, test_output_xhv_heavy); + } # endif + + return false; } From 1d31f1b0b8eae7c60665292d844aa49d80ae284d Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 11 Jun 2018 12:26:49 +0700 Subject: [PATCH 293/389] Fix command line option "-a cn-heavy". --- src/common/net/Pool.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 006d76a1..74e5012a 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -233,7 +233,10 @@ void Pool::adjust(xmrig::Algo algorithm) m_algorithm.setAlgo(algorithm); if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { - if (algorithm == xmrig::CRYPTONIGHT) { + if (algorithm == xmrig::CRYPTONIGHT_HEAVY) { + m_algorithm.setVariant(xmrig::VARIANT_0); + } + else { m_algorithm.setVariant(xmrig::VARIANT_1); } } From 21b2e2ca1e0761b41c44927a88710de0fa3bc392 Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 11 Jun 2018 15:23:06 +0700 Subject: [PATCH 294/389] Update CHANGELOG.md --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c99e646f..07fa9b87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# v2.6.3 +- **Added support for new cryptonight-heavy variant xhv** (`cn-heavy/xhv`) for upcoming Haven Protocol fork. +- **Added support for new cryptonight variant msr** (`cn/msr`) also known as `cryptonight-fast` for upcoming Masari fork. +- [#446](https://github.com/xmrig/xmrig/issues/446) Likely fixed SIGBUS error on 32 bit ARM CPUs. +- [#551](https://github.com/xmrig/xmrig/issues/551) Fixed `cn-heavy` algorithm on ARMv8. +- [#614](https://github.com/xmrig/xmrig/issues/614) Fixed display issue with huge pages percentage when colors disabled. +- [#615](https://github.com/xmrig/xmrig/issues/615) Fixed build without libcpuid. +- [#629](https://github.com/xmrig/xmrig/pull/629) Fixed file logging with non-seekable files. +- [#672](https://github.com/xmrig/xmrig/pull/672) Reverted back `cryptonight-light` and exit if no valid algorithm specified. + # v2.6.2 - [#607](https://github.com/xmrig/xmrig/issues/607) Fixed donation bug. - [#610](https://github.com/xmrig/xmrig/issues/610) Fixed ARM build. From 6a7a7ada2eb1935c22ccdc0d3620a8c0a991f56a Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 11 Jun 2018 15:44:45 +0700 Subject: [PATCH 295/389] Fix ARM build. --- src/crypto/CryptoNight_arm.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 46e1f530..845c027e 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -398,7 +398,7 @@ static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) } -template +template inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); @@ -496,7 +496,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } -template +template inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); @@ -657,19 +657,19 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si } -template +template inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx **__restrict__ ctx) { } -template +template inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx **__restrict__ ctx) { } -template +template inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx **__restrict__ ctx) { } From 859515315cb5493b001e93ddabc830e21075326d Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 11 Jun 2018 16:09:50 +0700 Subject: [PATCH 296/389] v2.6.3 --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index f9f02dcc..96a6412b 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.6.2" +#define APP_VERSION "2.6.3" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 6 -#define APP_VER_PATCH 2 +#define APP_VER_PATCH 3 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From 1e2fde0df1bc9dd794de2a0f219f966a076640e4 Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 11 Jun 2018 18:34:24 +0700 Subject: [PATCH 297/389] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07fa9b87..4b8f8a67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # v2.6.3 - **Added support for new cryptonight-heavy variant xhv** (`cn-heavy/xhv`) for upcoming Haven Protocol fork. - **Added support for new cryptonight variant msr** (`cn/msr`) also known as `cryptonight-fast` for upcoming Masari fork. +- Added new detailed hashrate report. - [#446](https://github.com/xmrig/xmrig/issues/446) Likely fixed SIGBUS error on 32 bit ARM CPUs. - [#551](https://github.com/xmrig/xmrig/issues/551) Fixed `cn-heavy` algorithm on ARMv8. - [#614](https://github.com/xmrig/xmrig/issues/614) Fixed display issue with huge pages percentage when colors disabled. From 8cb7483b2c311ed7ab0cf41aa8ed84106d51b42e Mon Sep 17 00:00:00 2001 From: xmrig Date: Wed, 13 Jun 2018 21:43:47 +0700 Subject: [PATCH 298/389] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 250ca54b..a07470c9 100644 --- a/README.md +++ b/README.md @@ -124,10 +124,10 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -f8e1957e8bfd7f281a76d1e42694049c67f39dea90ac36e9d589c14cdf8924bc xmrig-2.6.1-xenial-amd64.tar.gz/xmrig-2.6.1/xmrig -472c7aaf5aacc1212bfd3f2f96daca4f42d64e2d0db0872891328e7d8503d0c8 xmrig-2.6.1-gcc-win32.zip/xmrig.exe -d53154cef24c884b2be539ac13bfb6e7dba6bbc53b62e91f2877637d43fa4b15 xmrig-2.6.1-gcc-win64.zip/xmrig.exe -a253381b617463e6e1193d49b8afbf720a1c376621da7429d97f192668cd59ad xmrig-2.6.1-msvc-win64.zip/xmrig.exe +dbc2b0f92df5098dc91a361febfda46382d347ae3085415f35e1637b7ebc67e9 xmrig-2.6.3-xenial-amd64.tar.gz/xmrig-2.6.3/xmrig +b54ffe80cd54ac486f03f064c09de80b6ec29f19ddebb50a8d7316a947045b85 xmrig-2.6.3-gcc-win32.zip/xmrig.exe +c828ed21bbaf71a808b26928ab1eb6f6355029f2b731a639043733e6b48e9d77 xmrig-2.6.3-gcc-win64.zip/xmrig.exe +2e7432d14546b510506ccc22df62ef334d424d02fbc6baaa7a0498a0102de156 xmrig-2.6.3-msvc-win64.zip/xmrig.exe ``` ## Contacts From 63fed427f493cd28b2d2f2c3c4d55caa2577fcc2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 14 Jun 2018 21:25:57 +0700 Subject: [PATCH 299/389] 2.6.4-dev --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index 96a6412b..37df9006 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.6.3" +#define APP_VERSION "2.6.4-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 6 -#define APP_VER_PATCH 3 +#define APP_VER_PATCH 4 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From 33d9094cdc1181ce4599fbca9e035bc1a926e6aa Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 14 Jun 2018 21:49:27 +0700 Subject: [PATCH 300/389] Sync changes with proxy. --- src/common/crypto/Algorithm.cpp | 19 +++++++++++-------- src/common/net/Client.cpp | 20 ++++++++++++++++---- src/common/net/Pool.cpp | 2 ++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index fbfd0231..73940309 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -68,7 +68,7 @@ static AlgoData const algorithms[] = { # endif # ifndef XMRIG_NO_SUMO - { "cryptonight-heavy", "cn-heavy", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_0 }, + { "cryptonight-heavy", "cn-heavy", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_AUTO }, { "cryptonight-heavy/0", "cn-heavy/0", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_0 }, { "cryptonight-heavy/xhv", "cn-heavy/xhv", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_XHV }, # endif @@ -77,13 +77,16 @@ static AlgoData const algorithms[] = { #ifdef XMRIG_PROXY_PROJECT static AlgoData const xmrStakAlgorithms[] = { - { "cryptonight-monerov7", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, - { "cryptonight_v7", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, - { "cryptonight_v7_stellite", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_XTL }, - { "cryptonight_lite", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_0 }, - { "cryptonight-aeonv7", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, - { "cryptonight_lite_v7", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, - { "cryptonight_lite_v7_xor", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_IPBC }, + { "cryptonight-monerov7", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, + { "cryptonight_v7", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, + { "cryptonight_v7_stellite", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_XTL }, + { "cryptonight_lite", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_0 }, + { "cryptonight-aeonv7", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, + { "cryptonight_lite_v7", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, + { "cryptonight_lite_v7_xor", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_IPBC }, + { "cryptonight_heavy", nullptr, xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_0 }, + { "cryptonight_haven", nullptr, xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_XHV }, + { "cryptonight_masari", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_MSR }, }; #endif diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index a552ed3c..f4553d97 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -332,6 +332,12 @@ bool Client::parseLogin(const rapidjson::Value &result, int *code) bool Client::verifyAlgorithm(const xmrig::Algorithm &algorithm) const { +# ifdef XMRIG_PROXY_PROJECT + if (m_pool.algorithm().variant() == xmrig::VARIANT_AUTO) { + return true; + } +# endif + if (m_pool.isCompatible(algorithm)) { return true; } @@ -478,13 +484,19 @@ void Client::login() params.AddMember("rigid", StringRef(m_pool.rigId()), allocator); } - Value algo(kArrayType); +# ifdef XMRIG_PROXY_PROJECT + if (m_pool.algorithm().variant() != xmrig::VARIANT_AUTO) +# endif + { + Value algo(kArrayType); - for (const auto &a : m_pool.algorithms()) { - algo.PushBack(StringRef(a.shortName()), allocator); + for (const auto &a : m_pool.algorithms()) { + algo.PushBack(StringRef(a.shortName()), allocator); + } + + params.AddMember("algo", algo, allocator); } - params.AddMember("algo", algo, allocator); doc.AddMember("params", params, allocator); send(doc); diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 74e5012a..806664a0 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -232,6 +232,7 @@ void Pool::adjust(xmrig::Algo algorithm) if (!m_algorithm.isValid()) { m_algorithm.setAlgo(algorithm); +# ifndef XMRIG_PROXY_PROJECT if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { if (algorithm == xmrig::CRYPTONIGHT_HEAVY) { m_algorithm.setVariant(xmrig::VARIANT_0); @@ -240,6 +241,7 @@ void Pool::adjust(xmrig::Algo algorithm) m_algorithm.setVariant(xmrig::VARIANT_1); } } +# endif } if (strstr(m_host.data(), ".nicehash.com")) { From de6ee749fed1cf6a8dba4edc0309064c3f539a4f Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 14 Jun 2018 23:49:30 +0700 Subject: [PATCH 301/389] #692 Add support for global algorithm variant. --- src/common/config/CommonConfig.cpp | 2 +- src/common/net/Pool.cpp | 21 +++++++++++++-------- src/common/net/Pool.h | 2 +- src/net/strategies/DonateStrategy.cpp | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 315b09d6..7e43b39d 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -123,7 +123,7 @@ bool xmrig::CommonConfig::finalize() } for (Pool &pool : m_pools) { - pool.adjust(m_algorithm.algo()); + pool.adjust(m_algorithm); if (pool.isValid() && pool.algorithm().isValid()) { m_activePools.push_back(std::move(pool)); diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 806664a0..58be044e 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -223,22 +223,27 @@ rapidjson::Value Pool::toJSON(rapidjson::Document &doc) const } -void Pool::adjust(xmrig::Algo algorithm) +void Pool::adjust(const xmrig::Algorithm &algorithm) { + using namespace xmrig; + if (!isValid()) { return; } if (!m_algorithm.isValid()) { - m_algorithm.setAlgo(algorithm); + m_algorithm.setAlgo(algorithm.algo()); # ifndef XMRIG_PROXY_PROJECT - if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { - if (algorithm == xmrig::CRYPTONIGHT_HEAVY) { - m_algorithm.setVariant(xmrig::VARIANT_0); + if (m_algorithm.variant() == VARIANT_AUTO) { + if (algorithm.variant() != VARIANT_AUTO) { + m_algorithm.setVariant(algorithm.variant()); + } + else if (algorithm.algo() == CRYPTONIGHT_HEAVY) { + m_algorithm.setVariant(VARIANT_0); } else { - m_algorithm.setVariant(xmrig::VARIANT_1); + m_algorithm.setVariant(VARIANT_1); } } # endif @@ -249,13 +254,13 @@ void Pool::adjust(xmrig::Algo algorithm) m_nicehash = true; if (strstr(m_host.data(), "cryptonightv7.")) { - m_algorithm.setVariant(xmrig::VARIANT_1); + m_algorithm.setVariant(VARIANT_1); } } if (strstr(m_host.data(), ".minergate.com")) { m_keepAlive = false; - m_algorithm.setVariant(xmrig::VARIANT_1); + m_algorithm.setVariant(VARIANT_1); } rebuild(); diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index eaabe54d..e6ecef4a 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -77,7 +77,7 @@ public: bool parse(const char *url); bool setUserpass(const char *userpass); rapidjson::Value toJSON(rapidjson::Document &doc) const; - void adjust(xmrig::Algo algorithm); + void adjust(const xmrig::Algorithm &algorithm); void setAlgo(const xmrig::Algorithm &algorithm); # ifdef APP_DEBUG diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 4d353655..395c53bb 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -68,7 +68,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, xmrig::Algo algo, IS } for (Pool &pool : m_pools) { - pool.adjust(algo); + pool.adjust(xmrig::Algorithm(algo, xmrig::VARIANT_AUTO)); } if (m_pools.size() > 1) { From b719bbfefc0e747aedcecb737cac9f4f7f78765c Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 16 Jun 2018 16:08:08 +0700 Subject: [PATCH 302/389] Better variant detection for nicehash.com and minergate.com. --- src/common/net/Pool.cpp | 102 +++++++++++++++++++++++++++------------ src/common/net/Pool.h | 1 + src/common/utils/c_str.h | 6 +++ 3 files changed, 79 insertions(+), 30 deletions(-) diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 58be044e..83dbc290 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -225,42 +225,13 @@ rapidjson::Value Pool::toJSON(rapidjson::Document &doc) const void Pool::adjust(const xmrig::Algorithm &algorithm) { - using namespace xmrig; - if (!isValid()) { return; } if (!m_algorithm.isValid()) { m_algorithm.setAlgo(algorithm.algo()); - -# ifndef XMRIG_PROXY_PROJECT - if (m_algorithm.variant() == VARIANT_AUTO) { - if (algorithm.variant() != VARIANT_AUTO) { - m_algorithm.setVariant(algorithm.variant()); - } - else if (algorithm.algo() == CRYPTONIGHT_HEAVY) { - m_algorithm.setVariant(VARIANT_0); - } - else { - m_algorithm.setVariant(VARIANT_1); - } - } -# endif - } - - if (strstr(m_host.data(), ".nicehash.com")) { - m_keepAlive = false; - m_nicehash = true; - - if (strstr(m_host.data(), "cryptonightv7.")) { - m_algorithm.setVariant(VARIANT_1); - } - } - - if (strstr(m_host.data(), ".minergate.com")) { - m_keepAlive = false; - m_algorithm.setVariant(VARIANT_1); + adjustVariant(algorithm.variant()); } rebuild(); @@ -325,9 +296,80 @@ void Pool::addVariant(xmrig::Variant variant) } +void Pool::adjustVariant(const xmrig::Variant variantHint) +{ +# ifndef XMRIG_PROXY_PROJECT + using namespace xmrig; + + if (m_host.contains(".nicehash.com")) { + m_keepAlive = false; + m_nicehash = true; + bool valid = true; + + if (m_host.contains("cryptonight.") && m_port == 3355) { + valid = m_algorithm.algo() == CRYPTONIGHT; + m_algorithm.setVariant(VARIANT_0); + } + else if (m_host.contains("cryptonightv7.") && m_port == 3363) { + valid = m_algorithm.algo() == CRYPTONIGHT; + m_algorithm.setVariant(VARIANT_1); + } + else if (m_host.contains("cryptonightheavy.") && m_port == 3364) { + valid = m_algorithm.algo() == CRYPTONIGHT_HEAVY; + m_algorithm.setVariant(VARIANT_0); + } + + if (!valid) { + m_algorithm.setAlgo(INVALID_ALGO); + } + + return; + } + + if (m_host.contains(".minergate.com")) { + m_keepAlive = false; + bool valid = true; + m_algorithm.setVariant(VARIANT_1); + + if (m_host.contains("xmr.pool.")) { + valid = m_algorithm.algo() == CRYPTONIGHT; + m_algorithm.setVariant(m_port == 45700 ? VARIANT_1 : VARIANT_0); + } + else if (m_host.contains("aeon.pool.") && m_port == 45690) { + valid = m_algorithm.algo() == CRYPTONIGHT_LITE; + m_algorithm.setVariant(VARIANT_1); + } + + if (!valid) { + m_algorithm.setAlgo(INVALID_ALGO); + } + + return; + } + + if (variantHint != VARIANT_AUTO) { + m_algorithm.setVariant(variantHint); + return; + } + + if (m_algorithm.algo() == CRYPTONIGHT_HEAVY) { + m_algorithm.setVariant(VARIANT_0); + } + else { + m_algorithm.setVariant(VARIANT_1); + } +# endif +} + + void Pool::rebuild() { m_algorithms.clear(); + + if (!m_algorithm.isValid()) { + return; + } + m_algorithms.push_back(m_algorithm); # ifndef XMRIG_PROXY_PROJECT diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index e6ecef4a..57a30d1e 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -87,6 +87,7 @@ public: private: bool parseIPv6(const char *addr); void addVariant(xmrig::Variant variant); + void adjustVariant(const xmrig::Variant variantHint); void rebuild(); bool m_nicehash; diff --git a/src/common/utils/c_str.h b/src/common/utils/c_str.h index 3cc4d326..7ce63754 100644 --- a/src/common/utils/c_str.h +++ b/src/common/utils/c_str.h @@ -72,6 +72,12 @@ public: } + inline bool contains(const char *str) const + { + return strstr(m_data, str) != nullptr; + } + + inline bool isNull() const { return m_data == nullptr; } inline const char *data() const { return m_data; } inline size_t size() const { return m_data == nullptr ? 0 : strlen(m_data); } From a8de81a51b17b710d975bcd79b85db9a9d5d82fc Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 9 Jul 2018 00:23:27 +0700 Subject: [PATCH 303/389] #714 Simplify cn-heavy/xhv. --- src/crypto/CryptoNight_x86.h | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 8ab65023..7c8adc69 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -485,11 +485,10 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { - idx0 = (~d) ^ q; - } - else { - idx0 = d ^ q; + d = ~d; } + + idx0 = d ^ q; } } @@ -600,11 +599,10 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { - idx0 = (~d) ^ q; - } - else { - idx0 = d ^ q; + d = ~d; } + + idx0 = d ^ q; } cl = ((uint64_t*) &l1[idx1 & MASK])[0]; @@ -640,11 +638,10 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((int64_t*)&l1[idx1 & MASK])[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { - idx1 = (~d) ^ q; - } - else { - idx1 = d ^ q; + d = ~d; } + + idx1 = d ^ q; } } @@ -690,7 +687,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si lo = __umul128(idx, EXTRACT64(b), &hi); \ a = _mm_add_epi64(a, _mm_set_epi64x(lo, hi)); \ \ - if (IS_MONERO) { \ + if (IS_MONERO) { \ _mm_store_si128(ptr, _mm_xor_si128(a, mc)); \ \ if (VARIANT == xmrig::VARIANT_IPBC) { \ @@ -709,11 +706,10 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si int64_t q = n / (d | 0x5); \ ((int64_t*)&l[idx & MASK])[0] = n ^ q; \ if (VARIANT == xmrig::VARIANT_XHV) { \ - idx = (~d) ^ q; \ - } \ - else { \ - idx = d ^ q; \ + d = ~d; \ } \ + \ + idx = d ^ q; \ } From 25bac308629ac5a4db3a8c0a8047ebe4b1e05d39 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 9 Jul 2018 16:12:45 +0700 Subject: [PATCH 304/389] Added "cn/xao" and "cn/rto". --- src/common/crypto/Algorithm.cpp | 20 ++++++--- src/common/net/Pool.cpp | 8 +++- src/common/xmrig.h | 4 +- src/crypto/CryptoNight_constants.h | 50 ++++++++++++++++------ src/crypto/CryptoNight_x86.h | 8 ++-- src/workers/CpuThread.cpp | 68 +++++++++++++++++++++--------- src/workers/MultiWorker.cpp | 2 +- 7 files changed, 112 insertions(+), 48 deletions(-) diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index 73940309..31035fb1 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -58,19 +58,21 @@ static AlgoData const algorithms[] = { { "cryptonight/1", "cn/1", xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, { "cryptonight/xtl", "cn/xtl", xmrig::CRYPTONIGHT, xmrig::VARIANT_XTL }, { "cryptonight/msr", "cn/msr", xmrig::CRYPTONIGHT, xmrig::VARIANT_MSR }, + { "cryptonight/xao", "cn/xao", xmrig::CRYPTONIGHT, xmrig::VARIANT_XAO }, + { "cryptonight/rto", "cn/rto", xmrig::CRYPTONIGHT, xmrig::VARIANT_RTO }, # ifndef XMRIG_NO_AEON { "cryptonight-lite", "cn-lite", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, { "cryptonight-light", "cn-light", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, { "cryptonight-lite/0", "cn-lite/0", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_0 }, { "cryptonight-lite/1", "cn-lite/1", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, - { "cryptonight-lite/ipbc", "cn-lite/ipbc", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_IPBC }, # endif # ifndef XMRIG_NO_SUMO - { "cryptonight-heavy", "cn-heavy", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_AUTO }, - { "cryptonight-heavy/0", "cn-heavy/0", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_0 }, - { "cryptonight-heavy/xhv", "cn-heavy/xhv", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_XHV }, + { "cryptonight-heavy", "cn-heavy", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_AUTO }, + { "cryptonight-heavy/0", "cn-heavy/0", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_0 }, + { "cryptonight-heavy/xhv", "cn-heavy/xhv", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_XHV }, + { "cryptonight-heavy/tube", "cn-heavy/tube", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_TUBE }, # endif }; @@ -83,10 +85,12 @@ static AlgoData const xmrStakAlgorithms[] = { { "cryptonight_lite", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_0 }, { "cryptonight-aeonv7", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, { "cryptonight_lite_v7", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, - { "cryptonight_lite_v7_xor", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_IPBC }, { "cryptonight_heavy", nullptr, xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_0 }, { "cryptonight_haven", nullptr, xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_XHV }, { "cryptonight_masari", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_MSR }, + { "cryptonight_masari", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_MSR }, + { "cryptonight-bittube2", nullptr, xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_TUBE }, // bittube-miner + { "cryptonight_alloy", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_XAO }, // xmr-stak-alloy }; #endif @@ -94,10 +98,12 @@ static AlgoData const xmrStakAlgorithms[] = { static const char *variants[] = { "0", "1", - "ipbc", + "tube", "xtl", "msr", - "xhv" + "xhv", + "xao", + "rto" }; diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 83dbc290..053f2507 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -352,6 +352,10 @@ void Pool::adjustVariant(const xmrig::Variant variantHint) return; } + if (m_algorithm.variant() != VARIANT_AUTO) { + return; + } + if (m_algorithm.algo() == CRYPTONIGHT_HEAVY) { m_algorithm.setVariant(VARIANT_0); } @@ -376,9 +380,11 @@ void Pool::rebuild() addVariant(xmrig::VARIANT_1); addVariant(xmrig::VARIANT_0); addVariant(xmrig::VARIANT_XTL); - addVariant(xmrig::VARIANT_IPBC); + addVariant(xmrig::VARIANT_TUBE); addVariant(xmrig::VARIANT_MSR); addVariant(xmrig::VARIANT_XHV); + addVariant(xmrig::VARIANT_XAO); + addVariant(xmrig::VARIANT_RTO); addVariant(xmrig::VARIANT_AUTO); # endif } diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 170f4832..0ff945b9 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -61,10 +61,12 @@ enum Variant { VARIANT_AUTO = -1, // Autodetect VARIANT_0 = 0, // Original CryptoNight or CryptoNight-Heavy VARIANT_1 = 1, // CryptoNight variant 1 also known as Monero7 and CryptoNightV7 - VARIANT_IPBC = 2, // Modified CryptoNight Lite variant 1 with XOR (IPBC/TUBE only) + VARIANT_TUBE = 2, // Modified CryptoNight-Heavy (TUBE only) VARIANT_XTL = 3, // Modified CryptoNight variant 1 (Stellite only) VARIANT_MSR = 4, // Modified CryptoNight variant 1 (Masari only) VARIANT_XHV = 5, // Modified CryptoNight-Heavy (Haven Protocol only) + VARIANT_XAO = 6, // Modified CryptoNight variant 1 (Alloy only) + VARIANT_RTO = 7, // Modified CryptoNight variant 1 (Arto only) VARIANT_MAX }; diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h index 85a2ab85..08a755d4 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/CryptoNight_constants.h @@ -39,6 +39,7 @@ constexpr const size_t CRYPTONIGHT_MEMORY = 2 * 1024 * 1024; constexpr const uint32_t CRYPTONIGHT_MASK = 0x1FFFF0; constexpr const uint32_t CRYPTONIGHT_ITER = 0x80000; constexpr const uint32_t CRYPTONIGHT_MSR_ITER = 0x40000; +constexpr const uint32_t CRYPTONIGHT_XAO_ITER = 0x100000; constexpr const size_t CRYPTONIGHT_LITE_MEMORY = 1 * 1024 * 1024; constexpr const uint32_t CRYPTONIGHT_LITE_MASK = 0xFFFF0; @@ -103,24 +104,37 @@ inline uint32_t cn_select_mask(Algo algorithm) } -template inline constexpr uint32_t cn_select_iter() { return 0; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_MSR_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } -template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } +template inline constexpr uint32_t cn_select_iter() { return 0; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_MSR_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_XAO_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_LITE_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_HEAVY_ITER; } inline uint32_t cn_select_iter(Algo algorithm, Variant variant) { + switch (variant) { + case VARIANT_MSR: + return CRYPTONIGHT_MSR_ITER; + + case VARIANT_RTO: + return CRYPTONIGHT_XAO_ITER; + + default: + break; + } + switch(algorithm) { case CRYPTONIGHT: - return variant == VARIANT_MSR ? CRYPTONIGHT_MSR_ITER : CRYPTONIGHT_ITER; + return CRYPTONIGHT_ITER; case CRYPTONIGHT_LITE: return CRYPTONIGHT_LITE_ITER; @@ -139,15 +153,25 @@ inline uint32_t cn_select_iter(Algo algorithm, Variant variant) template inline constexpr bool cn_is_monero() { return false; } template<> inline constexpr bool cn_is_monero() { return false; } template<> inline constexpr bool cn_is_monero() { return true; } -template<> inline constexpr bool cn_is_monero() { return true; } +template<> inline constexpr bool cn_is_monero() { return true; } template<> inline constexpr bool cn_is_monero() { return true; } template<> inline constexpr bool cn_is_monero() { return true; } template<> inline constexpr bool cn_is_monero() { return false; } +template<> inline constexpr bool cn_is_monero() { return false; } +template<> inline constexpr bool cn_is_monero() { return true; } inline bool cn_is_monero(Variant variant) { - return variant > VARIANT_0 && variant < VARIANT_XHV; + switch (variant) { + case VARIANT_0: + case VARIANT_XHV: + case VARIANT_RTO: + return false; + + default: + return true; + } } diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 7c8adc69..86a1011e 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -462,7 +462,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_IPBC) { + if (VARIANT == xmrig::VARIANT_RTO) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else { @@ -576,7 +576,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_IPBC) { + if (VARIANT == xmrig::VARIANT_RTO) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else { @@ -615,7 +615,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l1[idx1 & MASK])[0] = al1; if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_IPBC) { + if (VARIANT == xmrig::VARIANT_RTO) { ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; } else { @@ -690,7 +690,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si if (IS_MONERO) { \ _mm_store_si128(ptr, _mm_xor_si128(a, mc)); \ \ - if (VARIANT == xmrig::VARIANT_IPBC) { \ + if (VARIANT == xmrig::VARIANT_RTO) { \ ((uint64_t*)ptr)[1] ^= ((uint64_t*)ptr)[0]; \ } \ } else { \ diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 926a9ba3..7cef4f3a 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -62,15 +62,9 @@ bool xmrig::CpuThread::isSoftAES(AlgoVariant av) xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant av, Variant variant) { - assert(variant == VARIANT_0 || - variant == VARIANT_1 || - variant == VARIANT_IPBC || - variant == VARIANT_XTL || - variant == VARIANT_MSR || - variant == VARIANT_XHV - ); + assert(variant >= VARIANT_0 && variant < VARIANT_MAX); - static const cn_hash_fun func_table[180] = { + static const cn_hash_fun func_table[VARIANT_MAX * 10 * 3] = { cryptonight_single_hash, cryptonight_double_hash, cryptonight_single_hash, @@ -93,7 +87,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_quad_hash, cryptonight_penta_hash, - nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_IPBC + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_TUBE cryptonight_single_hash, cryptonight_double_hash, @@ -119,6 +113,28 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XHV + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + # ifndef XMRIG_NO_AEON cryptonight_single_hash, cryptonight_double_hash, @@ -142,20 +158,12 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_quad_hash, cryptonight_penta_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_single_hash, - cryptonight_double_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - cryptonight_triple_hash, - cryptonight_quad_hash, - cryptonight_penta_hash, - + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_TUBE nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XTL nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_MSR nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XHV + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XAO + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RTO # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, @@ -163,6 +171,8 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif # ifndef XMRIG_NO_SUMO @@ -178,7 +188,18 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_penta_hash, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1 - nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_IPBC + + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XTL nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_MSR @@ -192,6 +213,9 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_triple_hash, cryptonight_quad_hash, cryptonight_penta_hash, + + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XAO + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RTO # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, @@ -199,6 +223,8 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif }; diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 73f0dba7..686a8415 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -61,7 +61,7 @@ bool MultiWorker::selfTest() if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE) { return verify(xmrig::VARIANT_0, test_output_v0_lite) && verify(xmrig::VARIANT_1, test_output_v1_lite) && - verify(xmrig::VARIANT_IPBC, test_output_ipbc_lite); + verify(xmrig::VARIANT_TUBE, test_output_ipbc_lite); } # endif From 2702b6ffc89e28635849cd105aeb73aba127e741 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 9 Jul 2018 16:54:39 +0700 Subject: [PATCH 305/389] Add reference hashes for cn/xao and cn/rto. --- src/crypto/CryptoNight_test.h | 30 ++++++++++++++++++++++++++++++ src/workers/MultiWorker.cpp | 27 +++++++++++++++------------ 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 187ba07a..96a36091 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -114,6 +114,36 @@ const static uint8_t test_output_msr[160] = { }; +// Alloy (XAO) +const static uint8_t test_output_xao[160] = { + 0x9A, 0x29, 0xD0, 0xC4, 0xAF, 0xDC, 0x63, 0x9B, 0x65, 0x53, 0xB1, 0xC8, 0x37, 0x35, 0x11, 0x4C, + 0x5D, 0x77, 0x16, 0x21, 0x42, 0x97, 0x5C, 0xB8, 0x50, 0xC0, 0xA5, 0x1F, 0x64, 0x07, 0xBD, 0x33, + 0xF1, 0xC9, 0x98, 0x40, 0x42, 0xDE, 0x39, 0xD1, 0xBA, 0x2D, 0xAD, 0xEC, 0xFE, 0xEA, 0xD8, 0x46, + 0x56, 0x1C, 0x32, 0x90, 0x42, 0x63, 0x10, 0x80, 0xD7, 0x01, 0xE4, 0xE6, 0x20, 0xB3, 0x60, 0x45, + 0x05, 0xE5, 0xC2, 0x18, 0xCD, 0x07, 0xA4, 0x40, 0x42, 0x91, 0xE2, 0xA4, 0x52, 0x54, 0x79, 0xBA, + 0xCD, 0x7E, 0x61, 0x2D, 0x7F, 0x7E, 0x69, 0x5E, 0xD7, 0xC0, 0x06, 0x65, 0xD7, 0xA1, 0xB8, 0xB8, + 0x1E, 0x31, 0x1C, 0xD3, 0xB7, 0xBC, 0x78, 0x3C, 0x01, 0xAF, 0x77, 0xAA, 0xF3, 0x0F, 0x4C, 0xF2, + 0xD1, 0x8B, 0x58, 0xC7, 0xEB, 0x99, 0x91, 0x53, 0x43, 0x71, 0x47, 0x99, 0x9E, 0x04, 0xA4, 0xEA, + 0xB8, 0xA3, 0xB0, 0x9E, 0x09, 0xF5, 0x57, 0x5C, 0xCF, 0x8A, 0xC6, 0xCA, 0x88, 0x51, 0x9A, 0x01, + 0x31, 0xCC, 0x0C, 0xA6, 0x53, 0xB5, 0x5F, 0xFD, 0x7D, 0x29, 0x3A, 0x35, 0xE9, 0x0E, 0x25, 0x6C +}; + + +// Arto (RTO) +const static uint8_t test_output_rto[160] = { + 0x82, 0x66, 0x1E, 0x1C, 0x6E, 0x64, 0x36, 0x66, 0x84, 0x06, 0x32, 0x7A, 0x9B, 0xB1, 0x13, 0x19, + 0xA5, 0x56, 0x16, 0x15, 0xDF, 0xEC, 0x1C, 0x9E, 0xE3, 0x88, 0x4A, 0x6C, 0x1C, 0xEB, 0x76, 0xA5, + 0xB3, 0xFB, 0xF4, 0x3F, 0x2B, 0x6A, 0x3A, 0x39, 0xA3, 0x6E, 0x08, 0x33, 0x67, 0x90, 0x31, 0xB9, + 0x3F, 0x27, 0xE4, 0x79, 0x32, 0x61, 0x6B, 0x5C, 0x8A, 0xF8, 0xAF, 0xC0, 0x60, 0xFD, 0x83, 0xB7, + 0x11, 0x11, 0x89, 0xB4, 0xDC, 0xAE, 0x40, 0xC8, 0x64, 0xAA, 0x4D, 0x19, 0x23, 0x7B, 0xD3, 0x27, + 0xB2, 0x0F, 0xA7, 0x50, 0x7D, 0xCA, 0xF5, 0x03, 0x06, 0xB2, 0x26, 0x62, 0xF3, 0x68, 0x2D, 0x30, + 0x6F, 0x93, 0x1E, 0xFF, 0xCD, 0x85, 0x40, 0x28, 0x5F, 0xC3, 0x8C, 0x76, 0x51, 0x9E, 0xD5, 0x06, + 0x32, 0xD6, 0x35, 0x83, 0xF6, 0x3B, 0x54, 0x4F, 0xA1, 0x9C, 0x13, 0xD8, 0xC4, 0x0E, 0x01, 0x2F, + 0x29, 0xDB, 0x8C, 0x1C, 0xB7, 0x06, 0x86, 0x79, 0x6D, 0xFF, 0x9F, 0x89, 0x3B, 0x3A, 0xA5, 0x79, + 0xE7, 0x81, 0x4E, 0x2A, 0xBD, 0x62, 0xC1, 0x1B, 0x7C, 0xB9, 0x33, 0x7B, 0xEE, 0x95, 0x80, 0xB3 +}; + + #ifndef XMRIG_NO_AEON const static uint8_t test_output_v0_lite[160] = { 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 686a8415..5e004635 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -50,25 +50,28 @@ MultiWorker::~MultiWorker() template bool MultiWorker::selfTest() { - if (m_thread->algorithm() == xmrig::CRYPTONIGHT) { - return verify(xmrig::VARIANT_0, test_output_v0) && - verify(xmrig::VARIANT_1, test_output_v1) && - verify(xmrig::VARIANT_XTL, test_output_xtl) && - verify(xmrig::VARIANT_MSR, test_output_msr); + using namespace xmrig; + + if (m_thread->algorithm() == CRYPTONIGHT) { + return verify(VARIANT_0, test_output_v0) && + verify(VARIANT_1, test_output_v1) && + verify(VARIANT_XTL, test_output_xtl) && + verify(VARIANT_MSR, test_output_msr) && + verify(VARIANT_XAO, test_output_xao) && + verify(VARIANT_RTO, test_output_rto); } # ifndef XMRIG_NO_AEON - if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE) { - return verify(xmrig::VARIANT_0, test_output_v0_lite) && - verify(xmrig::VARIANT_1, test_output_v1_lite) && - verify(xmrig::VARIANT_TUBE, test_output_ipbc_lite); + if (m_thread->algorithm() == CRYPTONIGHT_LITE) { + return verify(VARIANT_0, test_output_v0_lite) && + verify(VARIANT_1, test_output_v1_lite); } # endif # ifndef XMRIG_NO_SUMO - if (m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY) { - return verify(xmrig::VARIANT_0, test_output_v0_heavy) && - verify(xmrig::VARIANT_XHV, test_output_xhv_heavy); + if (m_thread->algorithm() == CRYPTONIGHT_HEAVY) { + return verify(VARIANT_0, test_output_v0_heavy) && + verify(VARIANT_XHV, test_output_xhv_heavy); } # endif From dac12a122f86264016b1d9959eaf0c64bfa209b7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 9 Jul 2018 20:21:53 +0700 Subject: [PATCH 306/389] Add cn-heavy/tube. --- src/crypto/CryptoNight_test.h | 30 +++++++++---------- src/crypto/CryptoNight_x86.h | 56 ++++++++++++++++++++++++++++------- src/workers/MultiWorker.cpp | 5 ++-- 3 files changed, 64 insertions(+), 27 deletions(-) diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 96a36091..16296efb 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -172,21 +172,6 @@ const static uint8_t test_output_v1_lite[160] = { 0x8C, 0x2B, 0xA4, 0x1F, 0x60, 0x76, 0x39, 0xD7, 0xF6, 0x46, 0x77, 0x18, 0x20, 0xAD, 0xD4, 0xC9, 0x87, 0xF7, 0x37, 0xDA, 0xFD, 0xBA, 0xBA, 0xD2, 0xF2, 0x68, 0xDC, 0x26, 0x8D, 0x1B, 0x08, 0xC6 }; - - -// IPBC -const static uint8_t test_output_ipbc_lite[160] = { - 0xE4, 0x93, 0x8C, 0xAA, 0x59, 0x8D, 0x02, 0x8A, 0xB8, 0x6F, 0x25, 0xD2, 0xB1, 0x23, 0xD0, 0xD5, - 0x33, 0xE3, 0x9F, 0x37, 0xAC, 0xE5, 0xF8, 0xEB, 0x7A, 0xE8, 0x40, 0xEB, 0x5D, 0xB1, 0x35, 0x5F, - 0xB2, 0x47, 0x86, 0xF0, 0x7F, 0x6F, 0x4B, 0x55, 0x3E, 0xA1, 0xBB, 0xE8, 0xA1, 0x75, 0x00, 0x2D, - 0x07, 0x9A, 0x21, 0x0E, 0xBD, 0x06, 0x6A, 0xB0, 0xFD, 0x96, 0x9E, 0xE6, 0xE4, 0x69, 0x67, 0xBB, - 0x88, 0x45, 0x0B, 0x91, 0x0B, 0x7B, 0xCB, 0x21, 0x3C, 0x3C, 0x09, 0x30, 0x07, 0x71, 0x07, 0xD5, - 0xB8, 0x2D, 0x83, 0x09, 0xAF, 0x7E, 0xB2, 0xA8, 0xAC, 0x25, 0xDC, 0x10, 0xF8, 0x63, 0x6A, 0xBC, - 0x73, 0x01, 0x4E, 0xA8, 0x1C, 0xDA, 0x9A, 0x86, 0x17, 0xEC, 0xA8, 0xFB, 0xAA, 0x23, 0x23, 0x17, - 0xE1, 0x32, 0x68, 0x9C, 0x4C, 0xF4, 0x08, 0xED, 0xB0, 0x15, 0xC3, 0xA9, 0x0F, 0xF0, 0xA2, 0x7E, - 0xD9, 0xE4, 0x23, 0xA7, 0x9E, 0x91, 0xD8, 0x73, 0x94, 0xD6, 0x6C, 0x70, 0x9B, 0x8B, 0x72, 0x92, - 0xA3, 0xA4, 0x0A, 0xE2, 0x3C, 0x0A, 0x34, 0x88, 0xA1, 0x6D, 0xFE, 0x02, 0x44, 0x60, 0x7B, 0x3D -}; #endif @@ -216,6 +201,21 @@ const static uint8_t test_output_xhv_heavy[160] = { 0x13, 0x91, 0x89, 0xB4, 0x5B, 0xA9, 0x2A, 0x7A, 0x09, 0x65, 0x14, 0x20, 0x76, 0x24, 0x6C, 0x80, 0x1D, 0x3F, 0x9F, 0xCD, 0x68, 0x39, 0xA9, 0x42, 0x27, 0xC1, 0x0C, 0x53, 0x98, 0x35, 0x60, 0x7A }; + + +// TUBE +const static uint8_t test_output_tube_heavy[160] = { + 0xFE, 0x53, 0x35, 0x20, 0x76, 0xEA, 0xE6, 0x89, 0xFA, 0x3B, 0x4F, 0xDA, 0x61, 0x46, 0x34, 0xCF, + 0xC3, 0x12, 0xEE, 0x0C, 0x38, 0x7D, 0xF2, 0xB8, 0xB7, 0x4D, 0xA2, 0xA1, 0x59, 0x74, 0x12, 0x35, + 0xCD, 0x3F, 0x29, 0xDF, 0x07, 0x4A, 0x14, 0xAD, 0x0B, 0x98, 0x99, 0x37, 0xCA, 0x14, 0x68, 0xA3, + 0x8D, 0xAE, 0x86, 0xC1, 0xA3, 0x54, 0x05, 0xBE, 0xEA, 0x6D, 0x29, 0x24, 0x0C, 0x82, 0x97, 0x74, + 0xA0, 0x64, 0x77, 0xCD, 0x8D, 0x8A, 0xC3, 0x10, 0xB4, 0x89, 0x0E, 0xBB, 0x7D, 0xE6, 0x32, 0x8F, + 0xF4, 0x2D, 0xB6, 0x9E, 0x8A, 0xF9, 0xF8, 0xEE, 0x2C, 0xD0, 0x74, 0xED, 0xA9, 0xAA, 0xA1, 0xFB, + 0xE2, 0xC9, 0x89, 0x66, 0xD6, 0x66, 0x52, 0xA2, 0x16, 0xDA, 0x36, 0xA0, 0x10, 0x62, 0xD2, 0xB1, + 0x76, 0xD1, 0x31, 0xE9, 0x1C, 0x08, 0xB6, 0xCA, 0xAF, 0x89, 0xB9, 0x3D, 0x2C, 0xFA, 0x9A, 0x30, + 0x74, 0x6A, 0x96, 0xA1, 0x95, 0x6C, 0xBB, 0x46, 0x4D, 0xE0, 0xEB, 0x28, 0xBE, 0x2A, 0x8C, 0x34, + 0x57, 0x79, 0xBE, 0x52, 0xFB, 0xBC, 0x68, 0x43, 0x45, 0xF4, 0xDF, 0xA5, 0xA8, 0xFD, 0x55, 0xA6 +}; #endif diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 86a1011e..5fa2a294 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -386,6 +386,27 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } +static inline __m128i aes_round_tweak_div(__m128i& val, const __m128i& key) +{ + alignas(16) uint32_t k[4]; + alignas(16) uint32_t x[4]; + + _mm_store_si128((__m128i*)k, key); + val = _mm_xor_si128(val, _mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128())); // val = ~val; + _mm_store_si128((__m128i*)x, val); + #define BYTE(p, i) ((unsigned char*)&p)[i] + k[0] ^= saes_table[0][BYTE(x[0], 0)] ^ saes_table[1][BYTE(x[1], 1)] ^ saes_table[2][BYTE(x[2], 2)] ^ saes_table[3][BYTE(x[3], 3)]; + x[0] ^= k[0]; + k[1] ^= saes_table[0][BYTE(x[1], 0)] ^ saes_table[1][BYTE(x[2], 1)] ^ saes_table[2][BYTE(x[3], 2)] ^ saes_table[3][BYTE(x[0], 3)]; + x[1] ^= k[1]; + k[2] ^= saes_table[0][BYTE(x[2], 0)] ^ saes_table[1][BYTE(x[3], 1)] ^ saes_table[2][BYTE(x[0], 2)] ^ saes_table[3][BYTE(x[1], 3)]; + x[2] ^= k[2]; + k[3] ^= saes_table[0][BYTE(x[3], 0)] ^ saes_table[1][BYTE(x[0], 1)] ^ saes_table[2][BYTE(x[1], 2)] ^ saes_table[3][BYTE(x[2], 3)]; + #undef BYTE + + return _mm_load_si128((__m128i*)k);} + + template static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) { @@ -433,12 +454,17 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx; + if (VARIANT == xmrig::VARIANT_TUBE || !SOFT_AES) { + cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + } - if (SOFT_AES) { + if (VARIANT == xmrig::VARIANT_TUBE) { + cx = aes_round_tweak_div(cx, _mm_set_epi64x(ah0, al0)); + } + else if (SOFT_AES) { cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); } else { - cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); } @@ -462,7 +488,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_RTO) { + if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else { @@ -539,14 +565,20 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx0, cx1; + if (VARIANT == xmrig::VARIANT_TUBE || !SOFT_AES) { + cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); + } - if (SOFT_AES) { + if (VARIANT == xmrig::VARIANT_TUBE) { + cx0 = aes_round_tweak_div(cx0, _mm_set_epi64x(ah0, al0)); + cx1 = aes_round_tweak_div(cx1, _mm_set_epi64x(ah1, al1)); + } + else if (SOFT_AES) { cx0 = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], _mm_set_epi64x(ah1, al1)); } else { - cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); - cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); } @@ -576,7 +608,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_RTO) { + if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else { @@ -615,7 +647,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l1[idx1 & MASK])[0] = al1; if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_RTO) { + if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; } else { @@ -662,7 +694,10 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si #define CN_STEP2(a, b, c, l, ptr, idx) \ - if (SOFT_AES) { \ + if (VARIANT == xmrig::VARIANT_TUBE) { \ + c = aes_round_tweak_div(c, a); \ + } \ + else if (SOFT_AES) { \ c = soft_aesenc(c, a); \ } else { \ c = _mm_aesenc_si128(c, a); \ @@ -690,7 +725,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si if (IS_MONERO) { \ _mm_store_si128(ptr, _mm_xor_si128(a, mc)); \ \ - if (VARIANT == xmrig::VARIANT_RTO) { \ + if (VARIANT == xmrig::VARIANT_TUBE || \ + VARIANT == xmrig::VARIANT_RTO) { \ ((uint64_t*)ptr)[1] ^= ((uint64_t*)ptr)[0]; \ } \ } else { \ diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 5e004635..5d43875c 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -70,8 +70,9 @@ bool MultiWorker::selfTest() # ifndef XMRIG_NO_SUMO if (m_thread->algorithm() == CRYPTONIGHT_HEAVY) { - return verify(VARIANT_0, test_output_v0_heavy) && - verify(VARIANT_XHV, test_output_xhv_heavy); + return verify(VARIANT_0, test_output_v0_heavy) && + verify(VARIANT_XHV, test_output_xhv_heavy) && + verify(VARIANT_TUBE, test_output_tube_heavy); } # endif From 969a26fb5d6cc79dadc374d9bab9153901241225 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 9 Jul 2018 22:28:53 +0700 Subject: [PATCH 307/389] Optimize cn-heavy/tube. --- src/crypto/CryptoNight_x86.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 5fa2a294..199c190b 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -386,25 +386,26 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -static inline __m128i aes_round_tweak_div(__m128i& val, const __m128i& key) +static inline __m128i aes_round_tweak_div(const __m128i &in, const __m128i &key) { alignas(16) uint32_t k[4]; alignas(16) uint32_t x[4]; - _mm_store_si128((__m128i*)k, key); - val = _mm_xor_si128(val, _mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128())); // val = ~val; - _mm_store_si128((__m128i*)x, val); - #define BYTE(p, i) ((unsigned char*)&p)[i] - k[0] ^= saes_table[0][BYTE(x[0], 0)] ^ saes_table[1][BYTE(x[1], 1)] ^ saes_table[2][BYTE(x[2], 2)] ^ saes_table[3][BYTE(x[3], 3)]; + _mm_store_si128((__m128i*) k, key); + _mm_store_si128((__m128i*) x, _mm_xor_si128(in, _mm_set_epi64x(0xffffffffffffffff, 0xffffffffffffffff))); + + #define BYTE(p, i) ((unsigned char*)&x[p])[i] + k[0] ^= saes_table[0][BYTE(0, 0)] ^ saes_table[1][BYTE(1, 1)] ^ saes_table[2][BYTE(2, 2)] ^ saes_table[3][BYTE(3, 3)]; x[0] ^= k[0]; - k[1] ^= saes_table[0][BYTE(x[1], 0)] ^ saes_table[1][BYTE(x[2], 1)] ^ saes_table[2][BYTE(x[3], 2)] ^ saes_table[3][BYTE(x[0], 3)]; + k[1] ^= saes_table[0][BYTE(1, 0)] ^ saes_table[1][BYTE(2, 1)] ^ saes_table[2][BYTE(3, 2)] ^ saes_table[3][BYTE(0, 3)]; x[1] ^= k[1]; - k[2] ^= saes_table[0][BYTE(x[2], 0)] ^ saes_table[1][BYTE(x[3], 1)] ^ saes_table[2][BYTE(x[0], 2)] ^ saes_table[3][BYTE(x[1], 3)]; + k[2] ^= saes_table[0][BYTE(2, 0)] ^ saes_table[1][BYTE(3, 1)] ^ saes_table[2][BYTE(0, 2)] ^ saes_table[3][BYTE(1, 3)]; x[2] ^= k[2]; - k[3] ^= saes_table[0][BYTE(x[3], 0)] ^ saes_table[1][BYTE(x[0], 1)] ^ saes_table[2][BYTE(x[1], 2)] ^ saes_table[3][BYTE(x[2], 3)]; + k[3] ^= saes_table[0][BYTE(3, 0)] ^ saes_table[1][BYTE(0, 1)] ^ saes_table[2][BYTE(1, 2)] ^ saes_table[3][BYTE(2, 3)]; #undef BYTE - return _mm_load_si128((__m128i*)k);} + return _mm_load_si128((__m128i*)k); +} template From 0041e9f0c1d0d24c5611f73c52ef8ffdf3bcbdda Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 10 Jul 2018 00:29:16 +0700 Subject: [PATCH 308/389] Add new algorithms for ARM. --- src/crypto/CryptoNight_arm.h | 49 ++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 845c027e..efb5759e 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -382,6 +382,28 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } +static inline __m128i aes_round_tweak_div(const __m128i &in, const __m128i &key) +{ + alignas(16) uint32_t k[4]; + alignas(16) uint32_t x[4]; + + _mm_store_si128((__m128i*) k, key); + _mm_store_si128((__m128i*) x, _mm_xor_si128(in, _mm_set_epi64x(0xffffffffffffffff, 0xffffffffffffffff))); + + #define BYTE(p, i) ((unsigned char*)&x[p])[i] + k[0] ^= saes_table[0][BYTE(0, 0)] ^ saes_table[1][BYTE(1, 1)] ^ saes_table[2][BYTE(2, 2)] ^ saes_table[3][BYTE(3, 3)]; + x[0] ^= k[0]; + k[1] ^= saes_table[0][BYTE(1, 0)] ^ saes_table[1][BYTE(2, 1)] ^ saes_table[2][BYTE(3, 2)] ^ saes_table[3][BYTE(0, 3)]; + x[1] ^= k[1]; + k[2] ^= saes_table[0][BYTE(2, 0)] ^ saes_table[1][BYTE(3, 1)] ^ saes_table[2][BYTE(0, 2)] ^ saes_table[3][BYTE(1, 3)]; + x[2] ^= k[2]; + k[3] ^= saes_table[0][BYTE(3, 0)] ^ saes_table[1][BYTE(0, 1)] ^ saes_table[2][BYTE(1, 2)] ^ saes_table[3][BYTE(2, 3)]; + #undef BYTE + + return _mm_load_si128((__m128i*)k); +} + + template static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) { @@ -428,12 +450,17 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx; + if (VARIANT == xmrig::VARIANT_TUBE || !SOFT_AES) { + cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + } - if (SOFT_AES) { + if (VARIANT == xmrig::VARIANT_TUBE) { + cx = aes_round_tweak_div(cx, _mm_set_epi64x(ah0, al0)); + } + else if (SOFT_AES) { cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); } else { - cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); } @@ -457,7 +484,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_IPBC) { + if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else { @@ -536,14 +563,20 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx0, cx1; + if (VARIANT == xmrig::VARIANT_TUBE || !SOFT_AES) { + cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); + } - if (SOFT_AES) { + if (VARIANT == xmrig::VARIANT_TUBE) { + cx0 = aes_round_tweak_div(cx0, _mm_set_epi64x(ah0, al0)); + cx1 = aes_round_tweak_div(cx1, _mm_set_epi64x(ah1, al1)); + } + else if (SOFT_AES) { cx0 = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], _mm_set_epi64x(ah1, al1)); } else { - cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); - cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); } @@ -573,7 +606,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l0[idx0 & MASK])[0] = al0; if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_IPBC) { + if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else { @@ -614,7 +647,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ((uint64_t*)&l1[idx1 & MASK])[0] = al1; if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_IPBC) { + if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; } else { From 4a39321d74637e6ede1e9b339b1a2536637f426d Mon Sep 17 00:00:00 2001 From: xmrig Date: Wed, 11 Jul 2018 20:42:16 +0700 Subject: [PATCH 309/389] Update CHANGELOG.md --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b8f8a67..8ef9573d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# v2.6.4 +- [#700](https://github.com/xmrig/xmrig/issues/700) `cryptonight-lite/ipbc` replaced to `cryptonight-heavy/tube` for **Bittube (TUBE)**. +- Added `cryptonight/rto` (cryptonight variant 1 with IPBC/TUBE mod) variant for **Arto (RTO)** coin. +- Added `cryptonight/xao` (original cryptonight with bigger iteration count) variant for **Alloy (XAO)** coin. +- Better variant detection for **nicehash.com** and **minergate.com**. +- [#692](https://github.com/xmrig/xmrig/issues/692) Added support for specify both algorithm and variant via single `algo` option. + # v2.6.3 - **Added support for new cryptonight-heavy variant xhv** (`cn-heavy/xhv`) for upcoming Haven Protocol fork. - **Added support for new cryptonight variant msr** (`cn/msr`) also known as `cryptonight-fast` for upcoming Masari fork. From 034d2f0c08f827ffb430c341f4ffc3fe3070cd20 Mon Sep 17 00:00:00 2001 From: xmrig Date: Wed, 11 Jul 2018 20:50:55 +0700 Subject: [PATCH 310/389] Update ALGORITHMS.md --- doc/ALGORITHMS.md | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/doc/ALGORITHMS.md b/doc/ALGORITHMS.md index d9a34a16..d62831de 100644 --- a/doc/ALGORITHMS.md +++ b/doc/ALGORITHMS.md @@ -7,35 +7,7 @@ Algorithm selection splitted to 2 parts: * Global base algorithm per miner or proxy instance, `algo` option. Possible values: `cryptonight`, `cryptonight-lite`, `cryptonight-heavy`. * Algorithm variant specified separately for each pool, `variant` option. -Possible variants for `cryptonight`: - - * `0` Original cryptonight. - * `1` cryptonight variant 1, also known as cryptonight v7 or monero7. - * `"xtl"` Stellite coin variant. - -Possible variants for `cryptonight-lite`: - - * `0` Original cryptonight-lite. - * `1` cryptonight-lite variant 1, also known as cryptonight-lite v7 or aeon7. - * `"ipbc"` IPBC coin variant. - -For `cryptonight-heavy` currently no variants. - - -### Cheatsheet - -You mine **Sumokoin** or **Haven Protocol**? -Your algorithm is `cryptonight-heavy` no variant option need. - -You mine **Aeon**, **TurtleCoin** or **IPBC**? -Your base algorithm is `cryptonight-lite`: - -Variants: - * Aeon: `-1` autodetect. `0` right now, `1` after fork. - * TurtleCoin: `1`. - * IPBC: `"ipbc"`. - -In all other cases base algorithm is `cryptonight`. + * [Full table for supported algorithm and variants.](https://github.com/xmrig/xmrig-proxy/blob/master/doc/STRATUM_EXT.md#14-algorithm-names-and-variants) ### Mining algorithm negotiation If your pool support [mining algorithm negotiation](https://github.com/xmrig/xmrig-proxy/issues/168) miner will choice proper variant automaticaly and if you choice wrong base algorithm you will see error message. From 807b2e9d6e9934db5284fb4e174c141b28f5cf88 Mon Sep 17 00:00:00 2001 From: xmrig Date: Wed, 11 Jul 2018 20:51:30 +0700 Subject: [PATCH 311/389] Update ALGORITHMS.md --- doc/ALGORITHMS.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/ALGORITHMS.md b/doc/ALGORITHMS.md index d62831de..f4a01e26 100644 --- a/doc/ALGORITHMS.md +++ b/doc/ALGORITHMS.md @@ -6,7 +6,6 @@ Algorithm selection splitted to 2 parts: * Global base algorithm per miner or proxy instance, `algo` option. Possible values: `cryptonight`, `cryptonight-lite`, `cryptonight-heavy`. * Algorithm variant specified separately for each pool, `variant` option. - * [Full table for supported algorithm and variants.](https://github.com/xmrig/xmrig-proxy/blob/master/doc/STRATUM_EXT.md#14-algorithm-names-and-variants) ### Mining algorithm negotiation From 6b7030cd6f2dd8ae091da92d21057d39ec8dfa13 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 12 Jul 2018 00:48:30 +0700 Subject: [PATCH 312/389] v2.6.4 --- src/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index 37df9006..76a3c77f 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.6.4-dev" +#define APP_VERSION "2.6.4" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" From 08e791b60dc4c7267209811ba453f24c155bdc37 Mon Sep 17 00:00:00 2001 From: xmrig Date: Thu, 12 Jul 2018 02:11:25 +0700 Subject: [PATCH 313/389] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a07470c9..6054b9f8 100644 --- a/README.md +++ b/README.md @@ -124,10 +124,10 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -dbc2b0f92df5098dc91a361febfda46382d347ae3085415f35e1637b7ebc67e9 xmrig-2.6.3-xenial-amd64.tar.gz/xmrig-2.6.3/xmrig -b54ffe80cd54ac486f03f064c09de80b6ec29f19ddebb50a8d7316a947045b85 xmrig-2.6.3-gcc-win32.zip/xmrig.exe -c828ed21bbaf71a808b26928ab1eb6f6355029f2b731a639043733e6b48e9d77 xmrig-2.6.3-gcc-win64.zip/xmrig.exe -2e7432d14546b510506ccc22df62ef334d424d02fbc6baaa7a0498a0102de156 xmrig-2.6.3-msvc-win64.zip/xmrig.exe +34d390a499d2098bce92e6b85b4858ee6255a7e2d4e03197ba4f6a759efe349c xmrig-2.6.4-xenial-amd64.tar.gz/xmrig-2.6.4/xmrig +cb6792c092c14f0f25d5774049a0adec403877a4564956220dcd9ba0fc488c82 xmrig-2.6.4-gcc-win32.zip/xmrig.exe +cb3c5619a8391f989c6a69135d890c3126eda9841b9dc591d44f02078a6fd49b xmrig-2.6.4-gcc-win64.zip/xmrig.exe +ea2e92bb10d0482880f8d389b7915948e11f672ca8559b0901d8a8fa8e9d733e xmrig-2.6.4-msvc-win64.zip/xmrig.exe ``` ## Contacts From e8d6514bd34c2a7b27cc7fa585eadfc61d0b60c0 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 20 Jul 2018 03:18:38 +0700 Subject: [PATCH 314/389] 2.6.5-dev --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index 76a3c77f..91a144e7 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.6.4" +#define APP_VERSION "2.6.5-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 6 -#define APP_VER_PATCH 4 +#define APP_VER_PATCH 5 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From f7b029eb05441fbc8532ca2a66d87270ffc5a9d3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 20 Jul 2018 03:37:07 +0700 Subject: [PATCH 315/389] Sync changes with AMD miner. --- CMakeLists.txt | 2 + src/common/crypto/keccak.h | 6 +++ src/common/interfaces/IConfig.h | 3 ++ src/common/log/BasicLog.cpp | 89 +++++++++++++++++++++++++++++++++ src/common/log/BasicLog.h | 55 ++++++++++++++++++++ src/common/log/Log.cpp | 9 ++++ src/common/log/Log.h | 5 +- src/common/utils/timestamp.h | 47 +++++++++++++++++ src/common/xmrig.h | 4 +- 9 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 src/common/log/BasicLog.cpp create mode 100644 src/common/log/BasicLog.h create mode 100644 src/common/utils/timestamp.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e263808e..07397ac4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ set(HEADERS src/common/interfaces/IStrategy.h src/common/interfaces/IStrategyListener.h src/common/interfaces/IWatcherListener.h + src/common/log/BasicLog.h src/common/log/ConsoleLog.h src/common/log/FileLog.h src/common/log/Log.h @@ -94,6 +95,7 @@ set(SOURCES src/common/Console.cpp src/common/crypto/Algorithm.cpp src/common/crypto/keccak.cpp + src/common/log/BasicLog.cpp src/common/log/ConsoleLog.cpp src/common/log/FileLog.cpp src/common/log/Log.cpp diff --git a/src/common/crypto/keccak.h b/src/common/crypto/keccak.h index 0413ec2d..da8d6c52 100644 --- a/src/common/crypto/keccak.h +++ b/src/common/crypto/keccak.h @@ -41,6 +41,12 @@ inline void keccak(const uint8_t *in, size_t inlen, uint8_t *md) keccak(in, static_cast(inlen), md, 200); } + +inline void keccak(const char *in, size_t inlen, uint8_t *md) +{ + keccak(reinterpret_cast(in), static_cast(inlen), md, 200); +} + // update the state void keccakf(uint64_t st[25], int norounds); diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index 62c7ba94..4b3f8788 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -83,6 +83,9 @@ public: OclAffinity = 1401, OclDevices = 1402, OclLaunch = 1403, + OclCache = 1404, + OclPrint = 1405, + OclLoader = 1406, // xmrig-proxy AccessLogFileKey = 'A', diff --git a/src/common/log/BasicLog.cpp b/src/common/log/BasicLog.cpp new file mode 100644 index 00000000..cb4defcd --- /dev/null +++ b/src/common/log/BasicLog.cpp @@ -0,0 +1,89 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include +#include +#include +#include + +#ifdef WIN32 +# include +# include +#endif + + +#include "common/log/BasicLog.h" +#include "common/log/Log.h" + + +BasicLog::BasicLog() +{ +} + + +void BasicLog::message(Level level, const char* fmt, va_list args) +{ + time_t now = time(nullptr); + tm stime; + +# ifdef _WIN32 + localtime_s(&stime, &now); +# else + localtime_r(&now, &stime); +# endif + + snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s", + stime.tm_year + 1900, + stime.tm_mon + 1, + stime.tm_mday, + stime.tm_hour, + stime.tm_min, + stime.tm_sec, + Log::colorByLevel(level, false), + fmt, + Log::endl(false) + ); + + print(args); +} + + +void BasicLog::text(const char* fmt, va_list args) +{ + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(false)); + + print(args); +} + + +void BasicLog::print(va_list args) +{ + if (vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args) <= 0) { + return; + } + + fputs(m_buf, stdout); + fflush(stdout); +} diff --git a/src/common/log/BasicLog.h b/src/common/log/BasicLog.h new file mode 100644 index 00000000..523538e9 --- /dev/null +++ b/src/common/log/BasicLog.h @@ -0,0 +1,55 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __BASICLOG_H__ +#define __BASICLOG_H__ + + +#include + + +#include "common/interfaces/ILogBackend.h" + + +namespace xmrig { + class Controller; +} + + +class BasicLog : public ILogBackend +{ +public: + BasicLog(); + + void message(Level level, const char *fmt, va_list args) override; + void text(const char *fmt, va_list args) override; + +private: + bool isWritable() const; + void print(va_list args); + + char m_buf[kBufferSize]; + char m_fmt[256]; +}; + +#endif /* __BASICLOG_H__ */ diff --git a/src/common/log/Log.cpp b/src/common/log/Log.cpp index ccf38008..2af90209 100644 --- a/src/common/log/Log.cpp +++ b/src/common/log/Log.cpp @@ -30,6 +30,7 @@ #include "common/interfaces/ILogBackend.h" +#include "common/log/BasicLog.h" #include "common/log/Log.h" @@ -109,6 +110,14 @@ const char *Log::endl(bool isColors) } +void Log::defaultInit() +{ + m_self = new Log(); + + add(new BasicLog()); +} + + Log::~Log() { for (auto backend : m_backends) { diff --git a/src/common/log/Log.h b/src/common/log/Log.h index bfa30717..2774ae0c 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -36,7 +36,7 @@ class Log { public: - static inline Log* i() { assert(m_self != nullptr); return m_self; } + static inline Log* i() { if (!m_self) { defaultInit(); } return m_self; } static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); } static inline void init() { if (!m_self) { new Log(); } } static inline void release() { assert(m_self != nullptr); delete m_self; } @@ -46,6 +46,7 @@ public: static const char *colorByLevel(ILogBackend::Level level, bool isColors = true); static const char *endl(bool isColors = true); + static void defaultInit(); private: inline Log() { @@ -68,6 +69,8 @@ private: #define RED(x) "\x1B[0;31m" x "\x1B[0m" #define GREEN_BOLD(x) "\x1B[1;32m" x "\x1B[0m" #define GREEN(x) "\x1B[0;32m" x "\x1B[0m" +#define YELLOW(x) "\x1B[0;33m" x "\x1B[0m" +#define YELLOW_BOLD(x) "\x1B[1;33m" x "\x1B[0m" #define MAGENTA_BOLD(x) "\x1B[1;35m" x "\x1B[0m" #define MAGENTA(x) "\x1B[0;35m" x "\x1B[0m" #define CYAN_BOLD(x) "\x1B[1;36m" x "\x1B[0m" diff --git a/src/common/utils/timestamp.h b/src/common/utils/timestamp.h new file mode 100644 index 00000000..b4404b2e --- /dev/null +++ b/src/common/utils/timestamp.h @@ -0,0 +1,47 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __TIMESTAMP_H__ +#define __TIMESTAMP_H__ + + +#include + + +namespace xmrig { + + +int64_t currentMSecsSinceEpoch() +{ + using namespace std::chrono; + if (high_resolution_clock::is_steady) { + return time_point_cast(high_resolution_clock::now()).time_since_epoch().count(); + } + + return time_point_cast(steady_clock::now()).time_since_epoch().count(); +} + + +} /* namespace xmrig */ + +#endif /* __TIMESTAMP_H__ */ diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 0ff945b9..58a3540c 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -33,7 +33,7 @@ enum Algo { INVALID_ALGO = -1, CRYPTONIGHT, /* CryptoNight (Monero) */ CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ - CRYPTONIGHT_HEAVY /* CryptoNight-Heavy (SUMO) */ + CRYPTONIGHT_HEAVY /* CryptoNight-Heavy (RYO) */ }; @@ -65,7 +65,7 @@ enum Variant { VARIANT_XTL = 3, // Modified CryptoNight variant 1 (Stellite only) VARIANT_MSR = 4, // Modified CryptoNight variant 1 (Masari only) VARIANT_XHV = 5, // Modified CryptoNight-Heavy (Haven Protocol only) - VARIANT_XAO = 6, // Modified CryptoNight variant 1 (Alloy only) + VARIANT_XAO = 6, // Modified CryptoNight variant 0 (Alloy only) VARIANT_RTO = 7, // Modified CryptoNight variant 1 (Arto only) VARIANT_MAX }; From dd8590e6c83c4a6141ceb99263f7ce4bb61bf3f1 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 29 Jul 2018 19:23:55 +0700 Subject: [PATCH 316/389] Update ALGORITHMS.md --- doc/ALGORITHMS.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/doc/ALGORITHMS.md b/doc/ALGORITHMS.md index f4a01e26..835a1d49 100644 --- a/doc/ALGORITHMS.md +++ b/doc/ALGORITHMS.md @@ -7,8 +7,24 @@ Algorithm selection splitted to 2 parts: * Global base algorithm per miner or proxy instance, `algo` option. Possible values: `cryptonight`, `cryptonight-lite`, `cryptonight-heavy`. * Algorithm variant specified separately for each pool, `variant` option. * [Full table for supported algorithm and variants.](https://github.com/xmrig/xmrig-proxy/blob/master/doc/STRATUM_EXT.md#14-algorithm-names-and-variants) + +#### Example +```json +{ + "algo": "cryptonight", + ... + "pools": [ + { + "url": "...", + "variant": 1, + ... + } + ], + ... +} +``` -### Mining algorithm negotiation +## Mining algorithm negotiation If your pool support [mining algorithm negotiation](https://github.com/xmrig/xmrig-proxy/issues/168) miner will choice proper variant automaticaly and if you choice wrong base algorithm you will see error message. Pools with mining algorithm negotiation support. From 36a562e1f14bfe05e364e492b109500d5b4b475a Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 28 Aug 2018 23:42:15 +0200 Subject: [PATCH 317/389] Cryptonight variant 2 support Reference code: https://github.com/monero-project/monero/pull/4218 --- src/common/crypto/Algorithm.cpp | 6 +- src/common/net/Job.cpp | 7 +- src/common/net/Pool.cpp | 2 + src/common/xmrig.h | 1 + src/crypto/CryptoNight_arm.h | 174 +++++---- src/crypto/CryptoNight_constants.h | 34 +- src/crypto/CryptoNight_monero.h | 72 +++- src/crypto/CryptoNight_test.h | 17 +- src/crypto/CryptoNight_x86.h | 546 ++++++++++++++--------------- src/workers/CpuThread.cpp | 15 + src/workers/MultiWorker.cpp | 1 + 11 files changed, 485 insertions(+), 390 deletions(-) diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index 31035fb1..05c890dc 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -60,6 +60,7 @@ static AlgoData const algorithms[] = { { "cryptonight/msr", "cn/msr", xmrig::CRYPTONIGHT, xmrig::VARIANT_MSR }, { "cryptonight/xao", "cn/xao", xmrig::CRYPTONIGHT, xmrig::VARIANT_XAO }, { "cryptonight/rto", "cn/rto", xmrig::CRYPTONIGHT, xmrig::VARIANT_RTO }, + { "cryptonight/2", "cn/2", xmrig::CRYPTONIGHT, xmrig::VARIANT_2 }, # ifndef XMRIG_NO_AEON { "cryptonight-lite", "cn-lite", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, @@ -81,6 +82,8 @@ static AlgoData const algorithms[] = { static AlgoData const xmrStakAlgorithms[] = { { "cryptonight-monerov7", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, { "cryptonight_v7", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_1 }, + { "cryptonight-monerov8", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_2 }, + { "cryptonight_v8", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_2 }, { "cryptonight_v7_stellite", nullptr, xmrig::CRYPTONIGHT, xmrig::VARIANT_XTL }, { "cryptonight_lite", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_0 }, { "cryptonight-aeonv7", nullptr, xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, @@ -103,7 +106,8 @@ static const char *variants[] = { "msr", "xhv", "xao", - "rto" + "rto", + "2", }; diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index 80b521ea..e9f81e02 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -178,7 +178,12 @@ xmrig::Variant Job::variant() const } if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { - return m_algorithm.algo() == xmrig::CRYPTONIGHT_HEAVY ? xmrig::VARIANT_0 : xmrig::VARIANT_1; + if (m_algorithm.algo() == xmrig::CRYPTONIGHT_HEAVY) { + return xmrig::VARIANT_0; + } else if (m_algorithm.algo() == xmrig::CRYPTONIGHT_LITE) { + return xmrig::VARIANT_1; + } + return (m_blob[0] >= 8) ? xmrig::VARIANT_2 : xmrig::VARIANT_1; } return m_algorithm.variant(); diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 053f2507..357cb330 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -211,6 +211,7 @@ rapidjson::Value Pool::toJSON(rapidjson::Document &doc) const case xmrig::VARIANT_AUTO: case xmrig::VARIANT_0: case xmrig::VARIANT_1: + case xmrig::VARIANT_2: obj.AddMember("variant", m_algorithm.variant(), allocator); break; @@ -377,6 +378,7 @@ void Pool::rebuild() m_algorithms.push_back(m_algorithm); # ifndef XMRIG_PROXY_PROJECT + addVariant(xmrig::VARIANT_2); addVariant(xmrig::VARIANT_1); addVariant(xmrig::VARIANT_0); addVariant(xmrig::VARIANT_XTL); diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 58a3540c..3e1b65df 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -67,6 +67,7 @@ enum Variant { VARIANT_XHV = 5, // Modified CryptoNight-Heavy (Haven Protocol only) VARIANT_XAO = 6, // Modified CryptoNight variant 0 (Alloy only) VARIANT_RTO = 7, // Modified CryptoNight variant 1 (Arto only) + VARIANT_2 = 8, // CryptoNight variant 2 VARIANT_MAX }; diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index efb5759e..7f273765 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -404,19 +404,27 @@ static inline __m128i aes_round_tweak_div(const __m128i &in, const __m128i &key) } -template -static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) +template +static inline void cryptonight_monero_tweak(const uint8_t* l, uint64_t idx, __m128i ax0, __m128i bx0, __m128i bx1, __m128i cx) { - mem_out[0] = EXTRACT64(tmp); + uint64_t* mem_out = (uint64_t*)&l[idx]; - uint64_t vh = vgetq_lane_u64(tmp, 1); + if (VARIANT == xmrig::VARIANT_2) { + VARIANT2_SHUFFLE(l, idx, ax0, bx0, bx1); + _mm_store_si128((__m128i *)mem_out, _mm_xor_si128(bx0, cx)); + } else { + __m128i tmp = _mm_xor_si128(bx0, cx); + mem_out[0] = EXTRACT64(tmp); - uint8_t x = vh >> 24; - static const uint16_t table = 0x7531; - const uint8_t index = (((x >> SHIFT) & 6) | (x & 1)) << 1; - vh ^= ((table >> index) & 0x3) << 28; + uint64_t vh = vgetq_lane_u64(tmp, 1); - mem_out[1] = vh; + uint8_t x = vh >> 24; + static const uint16_t table = 0x7531; + const uint8_t index = (((x >> (VARIANT == xmrig::VARIANT_XTL ? 4 : 3)) & 6) | (x & 1)) << 1; + vh ^= ((table >> index) & 0x3) << 28; + + mem_out[1] = vh; + } } @@ -426,27 +434,29 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_MONERO = xmrig::cn_is_monero(); + constexpr bool IS_V1 = xmrig::cn_uses_variant1(); - if (IS_MONERO && size < 43) { + if (IS_V1 && size < 43) { memset(output, 0, 32); return; } xmrig::keccak(input, size, ctx[0]->state); - VARIANT1_INIT(0); - cn_explode_scratchpad((__m128i*) ctx[0]->state, (__m128i*) ctx[0]->memory); const uint8_t* l0 = ctx[0]->memory; uint64_t* h0 = reinterpret_cast(ctx[0]->state); + VARIANT1_INIT(0); + VARIANT2_INIT(0); + uint64_t al0 = h0[0] ^ h0[4]; uint64_t ah0 = h0[1] ^ h0[5]; __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + __m128i bx1 = _mm_set_epi64x(h0[9] ^ h0[11], h0[8] ^ h0[10]); - uint64_t idx0 = h0[0] ^ h0[4]; + uint64_t idx0 = al0; for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx; @@ -454,44 +464,47 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); } + const __m128i ax0 = _mm_set_epi64x(ah0, al0); if (VARIANT == xmrig::VARIANT_TUBE) { - cx = aes_round_tweak_div(cx, _mm_set_epi64x(ah0, al0)); + cx = aes_round_tweak_div(cx, ax0); } else if (SOFT_AES) { - cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); + cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], ax0); } else { - cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); + cx = _mm_aesenc_si128(cx, ax0); } - if (IS_MONERO) { - cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + if (IS_V1 || VARIANT == xmrig::VARIANT_2) { + cryptonight_monero_tweak(l0, idx0 & MASK, ax0, bx0, bx1, cx); } else { _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } idx0 = EXTRACT64(cx); - bx0 = cx; uint64_t hi, lo, cl, ch; cl = ((uint64_t*) &l0[idx0 & MASK])[0]; ch = ((uint64_t*) &l0[idx0 & MASK])[1]; - lo = __umul128(idx0, cl, &hi); + if (VARIANT == xmrig::VARIANT_2) { + VARIANT2_INTEGER_MATH(0, cl, cx); + lo = __umul128(idx0, cl, &hi); + VARIANT2_SHUFFLE(l0, idx0 & MASK, ax0, bx0, bx1); + } + else { + lo = __umul128(idx0, cl, &hi); + } al0 += hi; ah0 += lo; ((uint64_t*)&l0[idx0 & MASK])[0] = al0; - if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; - } - else { - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; - } - } - else { + if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; + } else if (IS_V1) { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; + } else { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; } @@ -514,6 +527,8 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si idx0 = d ^ q; } } + bx1 = bx0; + bx0 = cx; } cn_implode_scratchpad((__m128i*) ctx[0]->memory, (__m128i*) ctx[0]->state); @@ -529,9 +544,9 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_MONERO = xmrig::cn_is_monero(); + constexpr bool IS_V1 = xmrig::cn_uses_variant1(); - if (IS_MONERO && size < 43) { + if (IS_V1 && size < 43) { memset(output, 0, 64); return; } @@ -539,14 +554,16 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si xmrig::keccak(input, size, ctx[0]->state); xmrig::keccak(input + size, size, ctx[1]->state); - VARIANT1_INIT(0); - VARIANT1_INIT(1); - const uint8_t* l0 = ctx[0]->memory; const uint8_t* l1 = ctx[1]->memory; uint64_t* h0 = reinterpret_cast(ctx[0]->state); uint64_t* h1 = reinterpret_cast(ctx[1]->state); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + VARIANT2_INIT(0); + VARIANT2_INIT(1); + cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); @@ -555,11 +572,13 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si uint64_t ah0 = h0[1] ^ h0[5]; uint64_t ah1 = h1[1] ^ h1[5]; - __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); - __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); + __m128i bx00 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + __m128i bx01 = _mm_set_epi64x(h0[9] ^ h0[11], h0[8] ^ h0[10]); + __m128i bx10 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); + __m128i bx11 = _mm_set_epi64x(h1[9] ^ h1[11], h1[8] ^ h1[10]); - uint64_t idx0 = h0[0] ^ h0[4]; - uint64_t idx1 = h1[0] ^ h1[4]; + uint64_t idx0 = al0; + uint64_t idx1 = al1; for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx0, cx1; @@ -568,52 +587,53 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); } + const __m128i ax0 = _mm_set_epi64x(ah0, al0); + const __m128i ax1 = _mm_set_epi64x(ah1, al1); if (VARIANT == xmrig::VARIANT_TUBE) { - cx0 = aes_round_tweak_div(cx0, _mm_set_epi64x(ah0, al0)); - cx1 = aes_round_tweak_div(cx1, _mm_set_epi64x(ah1, al1)); + cx0 = aes_round_tweak_div(cx0, ax0); + cx1 = aes_round_tweak_div(cx1, ax1); } else if (SOFT_AES) { - cx0 = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); - cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], _mm_set_epi64x(ah1, al1)); + cx0 = soft_aesenc((uint32_t*)&l0[idx0 & MASK], ax0); + cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], ax1); } else { - cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); - cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); + cx0 = _mm_aesenc_si128(cx0, ax0); + cx1 = _mm_aesenc_si128(cx1, ax1); } - if (IS_MONERO) { - cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); - cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + if (IS_V1 || (VARIANT == xmrig::VARIANT_2)) { + cryptonight_monero_tweak(l0, idx0 & MASK, ax0, bx00, bx01, cx0); + cryptonight_monero_tweak(l1, idx1 & MASK, ax1, bx10, bx11, cx1); } else { - _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); - _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); - }; + _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx00, cx0)); + _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx10, cx1)); + } idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); - bx0 = cx0; - bx1 = cx1; - uint64_t hi, lo, cl, ch; cl = ((uint64_t*) &l0[idx0 & MASK])[0]; ch = ((uint64_t*) &l0[idx0 & MASK])[1]; - lo = __umul128(idx0, cl, &hi); + if (VARIANT == xmrig::VARIANT_2) { + VARIANT2_INTEGER_MATH(0, cl, cx0); + lo = __umul128(idx0, cl, &hi); + VARIANT2_SHUFFLE(l0, idx0 & MASK, ax0, bx00, bx01); + } else { + lo = __umul128(idx0, cl, &hi); + } al0 += hi; ah0 += lo; ((uint64_t*)&l0[idx0 & MASK])[0] = al0; - if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; - } - else { - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; - } - } - else { + if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; + } else if (IS_V1) { + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; + } else { ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; } @@ -639,22 +659,24 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si cl = ((uint64_t*) &l1[idx1 & MASK])[0]; ch = ((uint64_t*) &l1[idx1 & MASK])[1]; - lo = __umul128(idx1, cl, &hi); + if (VARIANT == xmrig::VARIANT_2) { + VARIANT2_INTEGER_MATH(1, cl, cx1); + lo = __umul128(idx1, cl, &hi); + VARIANT2_SHUFFLE(l1, idx1 & MASK, ax1, bx10, bx11); + } else { + lo = __umul128(idx1, cl, &hi); + } al1 += hi; ah1 += lo; ((uint64_t*)&l1[idx1 & MASK])[0] = al1; - if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { - ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; - } - else { - ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1; - } - } - else { + if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; + } else if (IS_V1) { + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1; + } else { ((uint64_t*)&l1[idx1 & MASK])[1] = ah1; } @@ -677,6 +699,10 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si idx1 = d ^ q; } } + bx01 = bx00; + bx00 = cx0; + bx11 = bx10; + bx10 = cx1; } cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h index 08a755d4..97a77bbd 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/CryptoNight_constants.h @@ -107,6 +107,7 @@ inline uint32_t cn_select_mask(Algo algorithm) template inline constexpr uint32_t cn_select_iter() { return 0; } template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } +template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_ITER; } template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_MSR_ITER; } template<> inline constexpr uint32_t cn_select_iter() { return CRYPTONIGHT_XAO_ITER; } @@ -150,29 +151,16 @@ inline uint32_t cn_select_iter(Algo algorithm, Variant variant) } -template inline constexpr bool cn_is_monero() { return false; } -template<> inline constexpr bool cn_is_monero() { return false; } -template<> inline constexpr bool cn_is_monero() { return true; } -template<> inline constexpr bool cn_is_monero() { return true; } -template<> inline constexpr bool cn_is_monero() { return true; } -template<> inline constexpr bool cn_is_monero() { return true; } -template<> inline constexpr bool cn_is_monero() { return false; } -template<> inline constexpr bool cn_is_monero() { return false; } -template<> inline constexpr bool cn_is_monero() { return true; } - - -inline bool cn_is_monero(Variant variant) -{ - switch (variant) { - case VARIANT_0: - case VARIANT_XHV: - case VARIANT_RTO: - return false; - - default: - return true; - } -} +template inline constexpr bool cn_uses_variant1() { return false; } +template<> inline constexpr bool cn_uses_variant1() { return false; } +template<> inline constexpr bool cn_uses_variant1() { return true; } +template<> inline constexpr bool cn_uses_variant1() { return true; } +template<> inline constexpr bool cn_uses_variant1() { return true; } +template<> inline constexpr bool cn_uses_variant1() { return true; } +template<> inline constexpr bool cn_uses_variant1() { return false; } +template<> inline constexpr bool cn_uses_variant1() { return false; } +template<> inline constexpr bool cn_uses_variant1() { return true; } +template<> inline constexpr bool cn_uses_variant1() { return false; } } /* namespace xmrig */ diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index a758fdbc..3fb18d00 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -25,26 +25,27 @@ #ifndef __CRYPTONIGHT_MONERO_H__ #define __CRYPTONIGHT_MONERO_H__ +#include // VARIANT ALTERATIONS #ifndef XMRIG_ARM # define VARIANT1_INIT(part) \ uint64_t tweak1_2_##part = 0; \ - if (IS_MONERO) { \ + if (IS_V1) { \ tweak1_2_##part = (*reinterpret_cast(input + 35 + part * size) ^ \ *(reinterpret_cast(ctx[part]->state) + 24)); \ } #else # define VARIANT1_INIT(part) \ uint64_t tweak1_2_##part = 0; \ - if (IS_MONERO) { \ + if (IS_V1) { \ memcpy(&tweak1_2_##part, input + 35 + part * size, sizeof tweak1_2_##part); \ tweak1_2_##part ^= *(reinterpret_cast(ctx[part]->state) + 24); \ } #endif #define VARIANT1_1(p) \ - if (IS_MONERO) { \ + if (IS_V1) { \ const uint8_t tmp = reinterpret_cast(p)[11]; \ static const uint32_t table = 0x75310; \ const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ @@ -52,9 +53,72 @@ } #define VARIANT1_2(p, part) \ - if (IS_MONERO) { \ + if (IS_V1) { \ (p) ^= tweak1_2_##part; \ } +#ifndef XMRIG_ARM +# define VARIANT2_INIT(part) \ + __m128i division_result_xmm_##part = _mm_cvtsi64_si128(h##part[12]); \ + __m128i sqrt_result_xmm_##part = _mm_cvtsi64_si128(h##part[13]); + +#ifdef _MSC_VER +# define VARIANT2_SET_ROUNDING_MODE() if (VARIANT == xmrig::VARIANT_2) { _control87(RC_DOWN, MCW_RC); } +#else +# define VARIANT2_SET_ROUNDING_MODE() if (VARIANT == xmrig::VARIANT_2) { std::fesetround(FE_DOWNWARD); } +#endif + +# define VARIANT2_INTEGER_MATH(part, cl, cx) \ + do { \ + const uint64_t sqrt_result = static_cast(_mm_cvtsi128_si64(sqrt_result_xmm_##part)); \ + const uint64_t cx_0 = _mm_cvtsi128_si64(cx); \ + cl ^= static_cast(_mm_cvtsi128_si64(division_result_xmm_##part)) ^ (sqrt_result << 32); \ + const uint32_t d = static_cast(cx_0 + (sqrt_result << 1)) | 0x80000001UL; \ + const uint64_t cx_1 = _mm_cvtsi128_si64(_mm_srli_si128(cx, 8)); \ + const uint64_t division_result = static_cast(cx_1 / d) + ((cx_1 % d) << 32); \ + division_result_xmm_##part = _mm_cvtsi64_si128(static_cast(division_result)); \ + sqrt_result_xmm_##part = int_sqrt_v2(cx_0 + division_result); \ + } while (0) + +# define VARIANT2_SHUFFLE(base_ptr, offset, _a, _b, _b1) \ + do { \ + const __m128i chunk1 = _mm_load_si128((__m128i *)((base_ptr) + ((offset) ^ 0x10))); \ + const __m128i chunk2 = _mm_load_si128((__m128i *)((base_ptr) + ((offset) ^ 0x20))); \ + const __m128i chunk3 = _mm_load_si128((__m128i *)((base_ptr) + ((offset) ^ 0x30))); \ + _mm_store_si128((__m128i *)((base_ptr) + ((offset) ^ 0x10)), _mm_add_epi64(chunk3, _b1)); \ + _mm_store_si128((__m128i *)((base_ptr) + ((offset) ^ 0x20)), _mm_add_epi64(chunk1, _b)); \ + _mm_store_si128((__m128i *)((base_ptr) + ((offset) ^ 0x30)), _mm_add_epi64(chunk2, _a)); \ + } while (0) + +#else +# define VARIANT2_INIT(part) \ + uint64_t division_result_##part = h##part[12]; \ + uint64_t sqrt_result_##part = h##part[13]; + +# define VARIANT2_INTEGER_MATH(part, cl, cx) \ + do { \ + const uint64_t cx_0 = _mm_cvtsi128_si64(cx); \ + cl ^= division_result_##part ^ (sqrt_result_##part << 32); \ + const uint32_t d = static_cast(cx_0 + (sqrt_result_##part << 1)) | 0x80000001UL; \ + const uint64_t cx_1 = _mm_cvtsi128_si64(_mm_srli_si128(cx, 8)); \ + division_result_##part = static_cast(cx_1 / d) + ((cx_1 % d) << 32); \ + const uint64_t sqrt_input = cx_0 + division_result_##part; \ + sqrt_result_##part = sqrt(sqrt_input + 18446744073709551616.0) * 2.0 - 8589934592.0; \ + const uint64_t s = sqrt_result_##part >> 1; \ + const uint64_t b = sqrt_result_##part & 1; \ + const uint64_t r2 = (uint64_t)(s) * (s + b) + (sqrt_result_##part << 32); \ + sqrt_result_##part += ((r2 + b > sqrt_input) ? -1 : 0) + ((r2 + (1ULL << 32) < sqrt_input - s) ? 1 : 0); \ + } while (0) + +# define VARIANT2_SHUFFLE(base_ptr, offset, _a, _b, _b1) \ + do { \ + const uint64x2_t chunk1 = vld1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x10))); \ + const uint64x2_t chunk2 = vld1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x20))); \ + const uint64x2_t chunk3 = vld1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x30))); \ + vst1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x10)), vaddq_u64(chunk3, vreinterpretq_u64_u8(_b1))); \ + vst1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x20)), vaddq_u64(chunk1, vreinterpretq_u64_u8(_b))); \ + vst1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x30)), vaddq_u64(chunk2, vreinterpretq_u64_u8(_a))); \ + } while (0) +#endif #endif /* __CRYPTONIGHT_MONERO_H__ */ diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 16296efb..953f88d0 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -69,7 +69,7 @@ const static uint8_t test_output_v0[160] = { }; -// Monero v7 +// Cryptonight variant 1 (Monero v7) const static uint8_t test_output_v1[160] = { 0xF2, 0x2D, 0x3D, 0x62, 0x03, 0xD2, 0xA0, 0x8B, 0x41, 0xD9, 0x02, 0x72, 0x78, 0xD8, 0xBC, 0xC9, 0x83, 0xAC, 0xAD, 0xA9, 0xB6, 0x8E, 0x52, 0xE3, 0xC6, 0x89, 0x69, 0x2A, 0x50, 0xE9, 0x21, 0xD9, @@ -84,6 +84,21 @@ const static uint8_t test_output_v1[160] = { }; +// Cryptonight variant 2 (Monero v8) +const static uint8_t test_output_v2[160] = { + 0x6E, 0xEE, 0x53, 0xA3, 0xDA, 0xD1, 0x8C, 0x05, 0xB8, 0xCB, 0x32, 0x17, 0xAA, 0xEA, 0xEA, 0xB4, + 0x16, 0x11, 0x01, 0xA9, 0x08, 0x76, 0x37, 0x36, 0x6F, 0xDC, 0xCA, 0xC6, 0x92, 0x0D, 0xEA, 0x09, + 0x91, 0x03, 0x2F, 0x5B, 0x27, 0x4D, 0x94, 0x1D, 0x60, 0x50, 0xDC, 0x1F, 0x35, 0x57, 0xEC, 0x20, + 0xA6, 0xAC, 0x10, 0xDB, 0xCF, 0x36, 0x23, 0x8F, 0x96, 0xC7, 0x72, 0x8B, 0xF9, 0xE7, 0x30, 0xEB, + 0x50, 0x58, 0x4B, 0xFE, 0xAD, 0xC5, 0x13, 0x79, 0x50, 0x98, 0x1C, 0x67, 0xB2, 0xEB, 0xDA, 0x64, + 0xD4, 0xAA, 0xC4, 0xE8, 0xE5, 0xC9, 0xE7, 0x6B, 0x84, 0xC2, 0xD2, 0xE9, 0x1F, 0xA1, 0x0F, 0xDF, + 0x45, 0x06, 0x80, 0x25, 0x32, 0x6B, 0xC4, 0x66, 0x2A, 0x69, 0x9F, 0x1E, 0x1F, 0x4C, 0xBE, 0x89, + 0xFE, 0x61, 0xBB, 0x04, 0x79, 0xB5, 0x3B, 0x45, 0x58, 0xD9, 0x9C, 0x18, 0x7C, 0x48, 0x1B, 0x44, + 0x92, 0xC4, 0x4C, 0xD0, 0x8F, 0x16, 0x44, 0x79, 0x71, 0x48, 0x63, 0x0B, 0x51, 0xB6, 0x33, 0x8B, + 0x6B, 0x3F, 0xCC, 0x0A, 0x3A, 0x14, 0x3B, 0x49, 0x68, 0x46, 0xB9, 0x46, 0xC6, 0xA3, 0x03, 0x41 +}; + + // Stellite (XTL) const static uint8_t test_output_xtl[160] = { 0x8F, 0xE5, 0xF0, 0x5F, 0x02, 0x2A, 0x61, 0x7D, 0xE5, 0x3F, 0x79, 0x36, 0x4B, 0x25, 0xCB, 0xC3, diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 199c190b..e234d41d 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -408,20 +408,46 @@ static inline __m128i aes_round_tweak_div(const __m128i &in, const __m128i &key) } -template -static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) +static inline __m128i int_sqrt_v2(const uint64_t n0) { - mem_out[0] = EXTRACT64(tmp); + __m128d x = _mm_castsi128_pd(_mm_add_epi64(_mm_cvtsi64_si128(n0 >> 12), _mm_set_epi64x(0, 1023ULL << 52))); + x = _mm_sqrt_sd(_mm_setzero_pd(), x); + uint64_t r = static_cast(_mm_cvtsi128_si64(_mm_castpd_si128(x))); - tmp = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(tmp), _mm_castsi128_ps(tmp))); - uint64_t vh = EXTRACT64(tmp); + const uint64_t s = r >> 20; + r >>= 19; - uint8_t x = vh >> 24; - static const uint16_t table = 0x7531; - const uint8_t index = (((x >> SHIFT) & 6) | (x & 1)) << 1; - vh ^= ((table >> index) & 0x3) << 28; + uint64_t x2 = (s - (1022ULL << 32)) * (r - s - (1022ULL << 32) + 1); +#if defined _MSC_VER || (__GNUC__ >= 7) + _addcarry_u64(_subborrow_u64(0, x2, n0, (unsigned long long int*)&x2), r, 0, (unsigned long long int*)&r); +#else + if (x2 < n0) ++r; +#endif - mem_out[1] = vh; + return _mm_cvtsi64_si128(r); +} + + +template +static inline void cryptonight_monero_tweak(uint64_t* mem_out, const uint8_t* l, uint64_t idx, __m128i ax0, __m128i bx0, __m128i bx1, __m128i cx) +{ + if (VARIANT == xmrig::VARIANT_2) { + VARIANT2_SHUFFLE(l, idx, ax0, bx0, bx1); + _mm_store_si128((__m128i *)mem_out, _mm_xor_si128(bx0, cx)); + } else { + __m128i tmp = _mm_xor_si128(bx0, cx); + mem_out[0] = EXTRACT64(tmp); + + tmp = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(tmp), _mm_castsi128_ps(tmp))); + uint64_t vh = EXTRACT64(tmp); + + uint8_t x = static_cast(vh >> 24); + static const uint16_t table = 0x7531; + const uint8_t index = (((x >> (VARIANT == xmrig::VARIANT_XTL ? 4 : 3)) & 6) | (x & 1)) << 1; + vh ^= ((table >> index) & 0x3) << 28; + + mem_out[1] = vh; + } } @@ -431,92 +457,104 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_MONERO = xmrig::cn_is_monero(); + constexpr bool IS_V1 = xmrig::cn_uses_variant1(); - if (IS_MONERO && size < 43) { + if (IS_V1 && size < 43) { memset(output, 0, 32); return; } xmrig::keccak(input, size, ctx[0]->state); - VARIANT1_INIT(0) - cn_explode_scratchpad((__m128i*) ctx[0]->state, (__m128i*) ctx[0]->memory); const uint8_t* l0 = ctx[0]->memory; uint64_t* h0 = reinterpret_cast(ctx[0]->state); + VARIANT1_INIT(0); + VARIANT2_INIT(0); + VARIANT2_SET_ROUNDING_MODE(); + uint64_t al0 = h0[0] ^ h0[4]; uint64_t ah0 = h0[1] ^ h0[5]; __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + __m128i bx1 = _mm_set_epi64x(h0[9] ^ h0[11], h0[8] ^ h0[10]); uint64_t idx0 = al0; + uint64_t* ptr0 = (uint64_t*) &l0[idx0 & MASK]; for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx; if (VARIANT == xmrig::VARIANT_TUBE || !SOFT_AES) { - cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx = _mm_load_si128((__m128i *) ptr0); } + const __m128i ax0 = _mm_set_epi64x(ah0, al0); if (VARIANT == xmrig::VARIANT_TUBE) { - cx = aes_round_tweak_div(cx, _mm_set_epi64x(ah0, al0)); + cx = aes_round_tweak_div(cx, ax0); } else if (SOFT_AES) { - cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); + cx = soft_aesenc((uint32_t*) ptr0, ax0); } else { - cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); + cx = _mm_aesenc_si128(cx, ax0); } - if (IS_MONERO) { - cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + if (IS_V1 || VARIANT == xmrig::VARIANT_2) { + cryptonight_monero_tweak(ptr0, l0, idx0 & MASK, ax0, bx0, bx1, cx); } else { - _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + _mm_store_si128((__m128i *) ptr0, _mm_xor_si128(bx0, cx)); } idx0 = EXTRACT64(cx); - bx0 = cx; + ptr0 = (uint64_t*) &l0[idx0 & MASK]; uint64_t hi, lo, cl, ch; - cl = ((uint64_t*) &l0[idx0 & MASK])[0]; - ch = ((uint64_t*) &l0[idx0 & MASK])[1]; - lo = __umul128(idx0, cl, &hi); + cl = ptr0[0]; + ch = ptr0[1]; + if (VARIANT == xmrig::VARIANT_2) { + VARIANT2_INTEGER_MATH(0, cl, cx); + lo = __umul128(idx0, cl, &hi); + VARIANT2_SHUFFLE(l0, idx0 & MASK, ax0, bx0, bx1); + } + else { + lo = __umul128(idx0, cl, &hi); + } al0 += hi; ah0 += lo; - ((uint64_t*)&l0[idx0 & MASK])[0] = al0; + ptr0[0] = al0; - if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; - } - else { - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; - } - } - else { - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { + ptr0[1] = ah0 ^ tweak1_2_0 ^ al0; + } else if (IS_V1) { + ptr0[1] = ah0 ^ tweak1_2_0; + } else { + ptr0[1] = ah0; } al0 ^= cl; ah0 ^= ch; idx0 = al0; + ptr0 = (uint64_t*) &l0[idx0 & MASK]; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; - int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; + int64_t n = ((int64_t*)ptr0)[0]; + int32_t d = ((int32_t*)ptr0)[2]; int64_t q = n / (d | 0x5); - ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; + ((int64_t*) ptr0)[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { d = ~d; } idx0 = d ^ q; + ptr0 = (uint64_t*)&l0[idx0 & MASK]; } + bx1 = bx0; + bx0 = cx; } cn_implode_scratchpad((__m128i*) ctx[0]->memory, (__m128i*) ctx[0]->state); @@ -532,9 +570,9 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_MONERO = xmrig::cn_is_monero(); + constexpr bool IS_V1 = xmrig::cn_uses_variant1(); - if (IS_MONERO && size < 43) { + if (IS_V1 && size < 43) { memset(output, 0, 64); return; } @@ -542,14 +580,17 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si xmrig::keccak(input, size, ctx[0]->state); xmrig::keccak(input + size, size, ctx[1]->state); - VARIANT1_INIT(0); - VARIANT1_INIT(1); - const uint8_t* l0 = ctx[0]->memory; const uint8_t* l1 = ctx[1]->memory; uint64_t* h0 = reinterpret_cast(ctx[0]->state); uint64_t* h1 = reinterpret_cast(ctx[1]->state); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + VARIANT2_INIT(0); + VARIANT2_INIT(1); + VARIANT2_SET_ROUNDING_MODE(); + cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); @@ -558,124 +599,142 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si uint64_t ah0 = h0[1] ^ h0[5]; uint64_t ah1 = h1[1] ^ h1[5]; - __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); - __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); + __m128i bx00 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + __m128i bx01 = _mm_set_epi64x(h0[9] ^ h0[11], h0[8] ^ h0[10]); + __m128i bx10 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); + __m128i bx11 = _mm_set_epi64x(h1[9] ^ h1[11], h1[8] ^ h1[10]); uint64_t idx0 = al0; uint64_t idx1 = al1; + uint64_t* ptr0 = (uint64_t*)&l0[idx0 & MASK]; + uint64_t* ptr1 = (uint64_t*)&l1[idx1 & MASK]; for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx0, cx1; if (VARIANT == xmrig::VARIANT_TUBE || !SOFT_AES) { - cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); - cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); + cx0 = _mm_load_si128((__m128i *) ptr0); + cx1 = _mm_load_si128((__m128i *) ptr1); } + const __m128i ax0 = _mm_set_epi64x(ah0, al0); + const __m128i ax1 = _mm_set_epi64x(ah1, al1); if (VARIANT == xmrig::VARIANT_TUBE) { - cx0 = aes_round_tweak_div(cx0, _mm_set_epi64x(ah0, al0)); - cx1 = aes_round_tweak_div(cx1, _mm_set_epi64x(ah1, al1)); + cx0 = aes_round_tweak_div(cx0, ax0); + cx1 = aes_round_tweak_div(cx1, ax1); } else if (SOFT_AES) { - cx0 = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0)); - cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], _mm_set_epi64x(ah1, al1)); + cx0 = soft_aesenc((uint32_t*)ptr0, ax0); + cx1 = soft_aesenc((uint32_t*)ptr1, ax1); } else { - cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); - cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); + cx0 = _mm_aesenc_si128(cx0, ax0); + cx1 = _mm_aesenc_si128(cx1, ax1); } - if (IS_MONERO) { - cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); - cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + if (IS_V1 || (VARIANT == xmrig::VARIANT_2)) { + cryptonight_monero_tweak(ptr0, l0, idx0 & MASK, ax0, bx00, bx01, cx0); + cryptonight_monero_tweak(ptr1, l1, idx1 & MASK, ax1, bx10, bx11, cx1); } else { - _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); - _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + _mm_store_si128((__m128i *) ptr0, _mm_xor_si128(bx00, cx0)); + _mm_store_si128((__m128i *) ptr1, _mm_xor_si128(bx10, cx1)); } idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); - - bx0 = cx0; - bx1 = cx1; + ptr0 = (uint64_t*)&l0[idx0 & MASK]; + ptr1 = (uint64_t*)&l1[idx1 & MASK]; uint64_t hi, lo, cl, ch; - cl = ((uint64_t*) &l0[idx0 & MASK])[0]; - ch = ((uint64_t*) &l0[idx0 & MASK])[1]; - lo = __umul128(idx0, cl, &hi); + cl = ((uint64_t*)ptr0)[0]; + ch = ((uint64_t*)ptr0)[1]; + if (VARIANT == xmrig::VARIANT_2) { + VARIANT2_INTEGER_MATH(0, cl, cx0); + lo = __umul128(idx0, cl, &hi); + VARIANT2_SHUFFLE(l0, idx0 & MASK, ax0, bx00, bx01); + } else { + lo = __umul128(idx0, cl, &hi); + } al0 += hi; ah0 += lo; - ((uint64_t*)&l0[idx0 & MASK])[0] = al0; + ((uint64_t*)ptr0)[0] = al0; - if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; - } - else { - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; - } - } - else { - ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { + ((uint64_t*)ptr0)[1] = ah0 ^ tweak1_2_0 ^ al0; + } else if (IS_V1) { + ((uint64_t*)ptr0)[1] = ah0 ^ tweak1_2_0; + } else { + ((uint64_t*)ptr0)[1] = ah0; } al0 ^= cl; ah0 ^= ch; idx0 = al0; + ptr0 = (uint64_t*)&l0[idx0 & MASK]; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; - int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; + int64_t n = ((int64_t*)ptr0)[0]; + int32_t d = ((int32_t*)ptr0)[2]; int64_t q = n / (d | 0x5); - ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; + ((int64_t*)ptr0)[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { d = ~d; } idx0 = d ^ q; + ptr0 = (uint64_t*)&l0[idx0 & MASK]; } - cl = ((uint64_t*) &l1[idx1 & MASK])[0]; - ch = ((uint64_t*) &l1[idx1 & MASK])[1]; - lo = __umul128(idx1, cl, &hi); + cl = ptr1[0]; + ch = ptr1[1]; + if (VARIANT == xmrig::VARIANT_2) { + VARIANT2_INTEGER_MATH(1, cl, cx1); + lo = __umul128(idx1, cl, &hi); + VARIANT2_SHUFFLE(l1, idx1 & MASK, ax1, bx10, bx11); + } else { + lo = __umul128(idx1, cl, &hi); + } al1 += hi; ah1 += lo; - ((uint64_t*)&l1[idx1 & MASK])[0] = al1; + ptr1[0] = al1; - if (IS_MONERO) { - if (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO) { - ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; - } - else { - ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1; - } - } - else { - ((uint64_t*)&l1[idx1 & MASK])[1] = ah1; + if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { + ptr1[1] = ah1 ^ tweak1_2_1 ^ al1; + } else if (IS_V1) { + ptr1[1] = ah1 ^ tweak1_2_1; + } else { + ptr1[1] = ah1; } al1 ^= cl; ah1 ^= ch; idx1 = al1; + ptr1 = (uint64_t*)&l1[idx1 & MASK]; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)&l1[idx1 & MASK])[0]; - int32_t d = ((int32_t*)&l1[idx1 & MASK])[2]; + int64_t n = ((int64_t*)ptr1)[0]; + int32_t d = ((int32_t*)ptr1)[2]; int64_t q = n / (d | 0x5); - ((int64_t*)&l1[idx1 & MASK])[0] = n ^ q; + ((int64_t*)ptr1)[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { d = ~d; } idx1 = d ^ q; + ptr1 = (uint64_t*)&l1[idx1 & MASK]; } + + bx01 = bx00; + bx00 = cx0; + bx11 = bx10; + bx10 = cx1; } cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); @@ -689,12 +748,12 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si } -#define CN_STEP1(a, b, c, l, ptr, idx) \ +#define CN_STEP1(a, b0, b1, c, l, ptr, idx) \ ptr = reinterpret_cast<__m128i*>(&l[idx & MASK]); \ c = _mm_load_si128(ptr); -#define CN_STEP2(a, b, c, l, ptr, idx) \ +#define CN_STEP2(a, b0, b1, c, l, ptr, idx) \ if (VARIANT == xmrig::VARIANT_TUBE) { \ c = aes_round_tweak_div(c, a); \ } \ @@ -704,26 +763,31 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si c = _mm_aesenc_si128(c, a); \ } \ \ - b = _mm_xor_si128(b, c); \ - \ - if (IS_MONERO) { \ - cryptonight_monero_tweak(reinterpret_cast(ptr), b); \ + if (IS_V1 || (VARIANT == xmrig::VARIANT_2)) { \ + cryptonight_monero_tweak((uint64_t*)ptr, l, idx & MASK, a, b0, b1, c); \ } else { \ - _mm_store_si128(ptr, b); \ + _mm_store_si128(ptr, _mm_xor_si128(b0, c)); \ } -#define CN_STEP3(a, b, c, l, ptr, idx) \ +#define CN_STEP3(part, a, b0, b1, c, l, ptr, idx) \ idx = EXTRACT64(c); \ ptr = reinterpret_cast<__m128i*>(&l[idx & MASK]); \ - b = _mm_load_si128(ptr); + uint64_t cl##part = ((uint64_t*)ptr)[0]; \ + uint64_t ch##part = ((uint64_t*)ptr)[1]; -#define CN_STEP4(a, b, c, l, mc, ptr, idx) \ - lo = __umul128(idx, EXTRACT64(b), &hi); \ +#define CN_STEP4(part, a, b0, b1, c, l, mc, ptr, idx) \ + if (VARIANT == xmrig::VARIANT_2) { \ + VARIANT2_INTEGER_MATH(part, cl##part, c); \ + lo = __umul128(idx, cl##part, &hi); \ + VARIANT2_SHUFFLE(l, idx & MASK, a, b0, b1); \ + } else { \ + lo = __umul128(idx, cl##part, &hi); \ + } \ a = _mm_add_epi64(a, _mm_set_epi64x(lo, hi)); \ \ - if (IS_MONERO) { \ + if (IS_V1) { \ _mm_store_si128(ptr, _mm_xor_si128(a, mc)); \ \ if (VARIANT == xmrig::VARIANT_TUBE || \ @@ -734,7 +798,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si _mm_store_si128(ptr, a); \ } \ \ - a = _mm_xor_si128(a, b); \ + a = _mm_xor_si128(a, _mm_set_epi64x(ch##part, cl##part)); \ idx = EXTRACT64(a); \ \ if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { \ @@ -747,15 +811,27 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si } \ \ idx = d ^ q; \ - } + } \ + b1 = b0; \ + b0 = c; #define CONST_INIT(ctx, n) \ __m128i mc##n; \ - if (IS_MONERO) { \ + __m128i division_result_xmm_##n; \ + __m128i sqrt_result_xmm_##n; \ + if (IS_V1) { \ mc##n = _mm_set_epi64x(*reinterpret_cast(input + n * size + 35) ^ \ *(reinterpret_cast((ctx)->state) + 24), 0); \ - } + } \ + if (VARIANT == xmrig::VARIANT_2) { \ + division_result_xmm_##n = _mm_cvtsi64_si128(h##n[12]); \ + sqrt_result_xmm_##n = _mm_cvtsi64_si128(h##n[13]); \ + } \ + __m128i ax##n = _mm_set_epi64x(h##n[1] ^ h##n[5], h##n[0] ^ h##n[4]); \ + __m128i bx##n##0 = _mm_set_epi64x(h##n[3] ^ h##n[7], h##n[2] ^ h##n[6]); \ + __m128i bx##n##1 = _mm_set_epi64x(h##n[9] ^ h##n[11], h##n[8] ^ h##n[10]); \ + __m128i cx##n = _mm_setzero_si128(); template @@ -764,9 +840,9 @@ inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_MONERO = xmrig::cn_is_monero(); + constexpr bool IS_V1 = xmrig::cn_uses_variant1(); - if (IS_MONERO && size < 43) { + if (IS_V1 && size < 43) { memset(output, 0, 32 * 3); return; } @@ -776,10 +852,6 @@ inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t si cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->state), reinterpret_cast<__m128i*>(ctx[i]->memory)); } - CONST_INIT(ctx[0], 0); - CONST_INIT(ctx[1], 1); - CONST_INIT(ctx[2], 2); - uint8_t* l0 = ctx[0]->memory; uint8_t* l1 = ctx[1]->memory; uint8_t* l2 = ctx[2]->memory; @@ -787,58 +859,35 @@ inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t si uint64_t* h1 = reinterpret_cast(ctx[1]->state); uint64_t* h2 = reinterpret_cast(ctx[2]->state); - __m128i ax0 = _mm_set_epi64x(h0[1] ^ h0[5], h0[0] ^ h0[4]); - __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); - __m128i ax1 = _mm_set_epi64x(h1[1] ^ h1[5], h1[0] ^ h1[4]); - __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); - __m128i ax2 = _mm_set_epi64x(h2[1] ^ h2[5], h2[0] ^ h2[4]); - __m128i bx2 = _mm_set_epi64x(h2[3] ^ h2[7], h2[2] ^ h2[6]); - __m128i cx0 = _mm_set_epi64x(0, 0); - __m128i cx1 = _mm_set_epi64x(0, 0); - __m128i cx2 = _mm_set_epi64x(0, 0); + CONST_INIT(ctx[0], 0); + CONST_INIT(ctx[1], 1); + CONST_INIT(ctx[2], 2); + VARIANT2_SET_ROUNDING_MODE(); uint64_t idx0, idx1, idx2; idx0 = EXTRACT64(ax0); idx1 = EXTRACT64(ax1); idx2 = EXTRACT64(ax2); - for (size_t i = 0; i < ITERATIONS / 2; i++) { + for (size_t i = 0; i < ITERATIONS; i++) { uint64_t hi, lo; __m128i *ptr0, *ptr1, *ptr2; - // EVEN ROUND - CN_STEP1(ax0, bx0, cx0, l0, ptr0, idx0); - CN_STEP1(ax1, bx1, cx1, l1, ptr1, idx1); - CN_STEP1(ax2, bx2, cx2, l2, ptr2, idx2); + CN_STEP1(ax0, bx00, bx01, cx0, l0, ptr0, idx0); + CN_STEP1(ax1, bx10, bx11, cx1, l1, ptr1, idx1); + CN_STEP1(ax2, bx20, bx21, cx2, l2, ptr2, idx2); - CN_STEP2(ax0, bx0, cx0, l0, ptr0, idx0); - CN_STEP2(ax1, bx1, cx1, l1, ptr1, idx1); - CN_STEP2(ax2, bx2, cx2, l2, ptr2, idx2); + CN_STEP2(ax0, bx00, bx01, cx0, l0, ptr0, idx0); + CN_STEP2(ax1, bx10, bx11, cx1, l1, ptr1, idx1); + CN_STEP2(ax2, bx20, bx21, cx2, l2, ptr2, idx2); - CN_STEP3(ax0, bx0, cx0, l0, ptr0, idx0); - CN_STEP3(ax1, bx1, cx1, l1, ptr1, idx1); - CN_STEP3(ax2, bx2, cx2, l2, ptr2, idx2); + CN_STEP3(0, ax0, bx00, bx01, cx0, l0, ptr0, idx0); + CN_STEP3(1, ax1, bx10, bx11, cx1, l1, ptr1, idx1); + CN_STEP3(2, ax2, bx20, bx21, cx2, l2, ptr2, idx2); - CN_STEP4(ax0, bx0, cx0, l0, mc0, ptr0, idx0); - CN_STEP4(ax1, bx1, cx1, l1, mc1, ptr1, idx1); - CN_STEP4(ax2, bx2, cx2, l2, mc2, ptr2, idx2); - - // ODD ROUND - CN_STEP1(ax0, cx0, bx0, l0, ptr0, idx0); - CN_STEP1(ax1, cx1, bx1, l1, ptr1, idx1); - CN_STEP1(ax2, cx2, bx2, l2, ptr2, idx2); - - CN_STEP2(ax0, cx0, bx0, l0, ptr0, idx0); - CN_STEP2(ax1, cx1, bx1, l1, ptr1, idx1); - CN_STEP2(ax2, cx2, bx2, l2, ptr2, idx2); - - CN_STEP3(ax0, cx0, bx0, l0, ptr0, idx0); - CN_STEP3(ax1, cx1, bx1, l1, ptr1, idx1); - CN_STEP3(ax2, cx2, bx2, l2, ptr2, idx2); - - CN_STEP4(ax0, cx0, bx0, l0, mc0, ptr0, idx0); - CN_STEP4(ax1, cx1, bx1, l1, mc1, ptr1, idx1); - CN_STEP4(ax2, cx2, bx2, l2, mc2, ptr2, idx2); + CN_STEP4(0, ax0, bx00, bx01, cx0, l0, mc0, ptr0, idx0); + CN_STEP4(1, ax1, bx10, bx11, cx1, l1, mc1, ptr1, idx1); + CN_STEP4(2, ax2, bx20, bx21, cx2, l2, mc2, ptr2, idx2); } for (size_t i = 0; i < 3; i++) { @@ -855,9 +904,9 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_MONERO = xmrig::cn_is_monero(); + constexpr bool IS_V1 = xmrig::cn_uses_variant1(); - if (IS_MONERO && size < 43) { + if (IS_V1 && size < 43) { memset(output, 0, 32 * 4); return; } @@ -867,11 +916,6 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->state), reinterpret_cast<__m128i*>(ctx[i]->memory)); } - CONST_INIT(ctx[0], 0); - CONST_INIT(ctx[1], 1); - CONST_INIT(ctx[2], 2); - CONST_INIT(ctx[3], 3); - uint8_t* l0 = ctx[0]->memory; uint8_t* l1 = ctx[1]->memory; uint8_t* l2 = ctx[2]->memory; @@ -881,18 +925,11 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size uint64_t* h2 = reinterpret_cast(ctx[2]->state); uint64_t* h3 = reinterpret_cast(ctx[3]->state); - __m128i ax0 = _mm_set_epi64x(h0[1] ^ h0[5], h0[0] ^ h0[4]); - __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); - __m128i ax1 = _mm_set_epi64x(h1[1] ^ h1[5], h1[0] ^ h1[4]); - __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); - __m128i ax2 = _mm_set_epi64x(h2[1] ^ h2[5], h2[0] ^ h2[4]); - __m128i bx2 = _mm_set_epi64x(h2[3] ^ h2[7], h2[2] ^ h2[6]); - __m128i ax3 = _mm_set_epi64x(h3[1] ^ h3[5], h3[0] ^ h3[4]); - __m128i bx3 = _mm_set_epi64x(h3[3] ^ h3[7], h3[2] ^ h3[6]); - __m128i cx0 = _mm_set_epi64x(0, 0); - __m128i cx1 = _mm_set_epi64x(0, 0); - __m128i cx2 = _mm_set_epi64x(0, 0); - __m128i cx3 = _mm_set_epi64x(0, 0); + CONST_INIT(ctx[0], 0); + CONST_INIT(ctx[1], 1); + CONST_INIT(ctx[2], 2); + CONST_INIT(ctx[3], 3); + VARIANT2_SET_ROUNDING_MODE(); uint64_t idx0, idx1, idx2, idx3; idx0 = EXTRACT64(ax0); @@ -900,52 +937,30 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size idx2 = EXTRACT64(ax2); idx3 = EXTRACT64(ax3); - for (size_t i = 0; i < ITERATIONS / 2; i++) + for (size_t i = 0; i < ITERATIONS; i++) { uint64_t hi, lo; __m128i *ptr0, *ptr1, *ptr2, *ptr3; - // EVEN ROUND - CN_STEP1(ax0, bx0, cx0, l0, ptr0, idx0); - CN_STEP1(ax1, bx1, cx1, l1, ptr1, idx1); - CN_STEP1(ax2, bx2, cx2, l2, ptr2, idx2); - CN_STEP1(ax3, bx3, cx3, l3, ptr3, idx3); + CN_STEP1(ax0, bx00, bx01, cx0, l0, ptr0, idx0); + CN_STEP1(ax1, bx10, bx11, cx1, l1, ptr1, idx1); + CN_STEP1(ax2, bx20, bx21, cx2, l2, ptr2, idx2); + CN_STEP1(ax3, bx30, bx31, cx3, l3, ptr3, idx3); - CN_STEP2(ax0, bx0, cx0, l0, ptr0, idx0); - CN_STEP2(ax1, bx1, cx1, l1, ptr1, idx1); - CN_STEP2(ax2, bx2, cx2, l2, ptr2, idx2); - CN_STEP2(ax3, bx3, cx3, l3, ptr3, idx3); + CN_STEP2(ax0, bx00, bx01, cx0, l0, ptr0, idx0); + CN_STEP2(ax1, bx10, bx11, cx1, l1, ptr1, idx1); + CN_STEP2(ax2, bx20, bx21, cx2, l2, ptr2, idx2); + CN_STEP2(ax3, bx30, bx31, cx3, l3, ptr3, idx3); - CN_STEP3(ax0, bx0, cx0, l0, ptr0, idx0); - CN_STEP3(ax1, bx1, cx1, l1, ptr1, idx1); - CN_STEP3(ax2, bx2, cx2, l2, ptr2, idx2); - CN_STEP3(ax3, bx3, cx3, l3, ptr3, idx3); + CN_STEP3(0, ax0, bx00, bx01, cx0, l0, ptr0, idx0); + CN_STEP3(1, ax1, bx10, bx11, cx1, l1, ptr1, idx1); + CN_STEP3(2, ax2, bx20, bx21, cx2, l2, ptr2, idx2); + CN_STEP3(3, ax3, bx30, bx31, cx3, l3, ptr3, idx3); - CN_STEP4(ax0, bx0, cx0, l0, mc0, ptr0, idx0); - CN_STEP4(ax1, bx1, cx1, l1, mc1, ptr1, idx1); - CN_STEP4(ax2, bx2, cx2, l2, mc2, ptr2, idx2); - CN_STEP4(ax3, bx3, cx3, l3, mc3, ptr3, idx3); - - // ODD ROUND - CN_STEP1(ax0, cx0, bx0, l0, ptr0, idx0); - CN_STEP1(ax1, cx1, bx1, l1, ptr1, idx1); - CN_STEP1(ax2, cx2, bx2, l2, ptr2, idx2); - CN_STEP1(ax3, cx3, bx3, l3, ptr3, idx3); - - CN_STEP2(ax0, cx0, bx0, l0, ptr0, idx0); - CN_STEP2(ax1, cx1, bx1, l1, ptr1, idx1); - CN_STEP2(ax2, cx2, bx2, l2, ptr2, idx2); - CN_STEP2(ax3, cx3, bx3, l3, ptr3, idx3); - - CN_STEP3(ax0, cx0, bx0, l0, ptr0, idx0); - CN_STEP3(ax1, cx1, bx1, l1, ptr1, idx1); - CN_STEP3(ax2, cx2, bx2, l2, ptr2, idx2); - CN_STEP3(ax3, cx3, bx3, l3, ptr3, idx3); - - CN_STEP4(ax0, cx0, bx0, l0, mc0, ptr0, idx0); - CN_STEP4(ax1, cx1, bx1, l1, mc1, ptr1, idx1); - CN_STEP4(ax2, cx2, bx2, l2, mc2, ptr2, idx2); - CN_STEP4(ax3, cx3, bx3, l3, mc3, ptr3, idx3); + CN_STEP4(0, ax0, bx00, bx01, cx0, l0, mc0, ptr0, idx0); + CN_STEP4(1, ax1, bx10, bx11, cx1, l1, mc1, ptr1, idx1); + CN_STEP4(2, ax2, bx20, bx21, cx2, l2, mc2, ptr2, idx2); + CN_STEP4(3, ax3, bx30, bx31, cx3, l3, mc3, ptr3, idx3); } for (size_t i = 0; i < 4; i++) { @@ -962,9 +977,9 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_MONERO = xmrig::cn_is_monero(); + constexpr bool IS_V1 = xmrig::cn_uses_variant1(); - if (IS_MONERO && size < 43) { + if (IS_V1 && size < 43) { memset(output, 0, 32 * 5); return; } @@ -974,12 +989,6 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[i]->state), reinterpret_cast<__m128i*>(ctx[i]->memory)); } - CONST_INIT(ctx[0], 0); - CONST_INIT(ctx[1], 1); - CONST_INIT(ctx[2], 2); - CONST_INIT(ctx[3], 3); - CONST_INIT(ctx[4], 4); - uint8_t* l0 = ctx[0]->memory; uint8_t* l1 = ctx[1]->memory; uint8_t* l2 = ctx[2]->memory; @@ -991,21 +1000,12 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz uint64_t* h3 = reinterpret_cast(ctx[3]->state); uint64_t* h4 = reinterpret_cast(ctx[4]->state); - __m128i ax0 = _mm_set_epi64x(h0[1] ^ h0[5], h0[0] ^ h0[4]); - __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); - __m128i ax1 = _mm_set_epi64x(h1[1] ^ h1[5], h1[0] ^ h1[4]); - __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); - __m128i ax2 = _mm_set_epi64x(h2[1] ^ h2[5], h2[0] ^ h2[4]); - __m128i bx2 = _mm_set_epi64x(h2[3] ^ h2[7], h2[2] ^ h2[6]); - __m128i ax3 = _mm_set_epi64x(h3[1] ^ h3[5], h3[0] ^ h3[4]); - __m128i bx3 = _mm_set_epi64x(h3[3] ^ h3[7], h3[2] ^ h3[6]); - __m128i ax4 = _mm_set_epi64x(h4[1] ^ h4[5], h4[0] ^ h4[4]); - __m128i bx4 = _mm_set_epi64x(h4[3] ^ h4[7], h4[2] ^ h4[6]); - __m128i cx0 = _mm_set_epi64x(0, 0); - __m128i cx1 = _mm_set_epi64x(0, 0); - __m128i cx2 = _mm_set_epi64x(0, 0); - __m128i cx3 = _mm_set_epi64x(0, 0); - __m128i cx4 = _mm_set_epi64x(0, 0); + CONST_INIT(ctx[0], 0); + CONST_INIT(ctx[1], 1); + CONST_INIT(ctx[2], 2); + CONST_INIT(ctx[3], 3); + CONST_INIT(ctx[4], 4); + VARIANT2_SET_ROUNDING_MODE(); uint64_t idx0, idx1, idx2, idx3, idx4; idx0 = EXTRACT64(ax0); @@ -1014,60 +1014,34 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz idx3 = EXTRACT64(ax3); idx4 = EXTRACT64(ax4); - for (size_t i = 0; i < ITERATIONS / 2; i++) + for (size_t i = 0; i < ITERATIONS; i++) { uint64_t hi, lo; __m128i *ptr0, *ptr1, *ptr2, *ptr3, *ptr4; - // EVEN ROUND - CN_STEP1(ax0, bx0, cx0, l0, ptr0, idx0); - CN_STEP1(ax1, bx1, cx1, l1, ptr1, idx1); - CN_STEP1(ax2, bx2, cx2, l2, ptr2, idx2); - CN_STEP1(ax3, bx3, cx3, l3, ptr3, idx3); - CN_STEP1(ax4, bx4, cx4, l4, ptr4, idx4); + CN_STEP1(ax0, bx00, bx01, cx0, l0, ptr0, idx0); + CN_STEP1(ax1, bx10, bx11, cx1, l1, ptr1, idx1); + CN_STEP1(ax2, bx20, bx21, cx2, l2, ptr2, idx2); + CN_STEP1(ax3, bx30, bx31, cx3, l3, ptr3, idx3); + CN_STEP1(ax4, bx40, bx41, cx4, l4, ptr4, idx4); - CN_STEP2(ax0, bx0, cx0, l0, ptr0, idx0); - CN_STEP2(ax1, bx1, cx1, l1, ptr1, idx1); - CN_STEP2(ax2, bx2, cx2, l2, ptr2, idx2); - CN_STEP2(ax3, bx3, cx3, l3, ptr3, idx3); - CN_STEP2(ax4, bx4, cx4, l4, ptr4, idx4); + CN_STEP2(ax0, bx00, bx01, cx0, l0, ptr0, idx0); + CN_STEP2(ax1, bx10, bx11, cx1, l1, ptr1, idx1); + CN_STEP2(ax2, bx20, bx21, cx2, l2, ptr2, idx2); + CN_STEP2(ax3, bx30, bx31, cx3, l3, ptr3, idx3); + CN_STEP2(ax4, bx40, bx41, cx4, l4, ptr4, idx4); - CN_STEP3(ax0, bx0, cx0, l0, ptr0, idx0); - CN_STEP3(ax1, bx1, cx1, l1, ptr1, idx1); - CN_STEP3(ax2, bx2, cx2, l2, ptr2, idx2); - CN_STEP3(ax3, bx3, cx3, l3, ptr3, idx3); - CN_STEP3(ax4, bx4, cx4, l4, ptr4, idx4); + CN_STEP3(0, ax0, bx00, bx01, cx0, l0, ptr0, idx0); + CN_STEP3(1, ax1, bx10, bx11, cx1, l1, ptr1, idx1); + CN_STEP3(2, ax2, bx20, bx21, cx2, l2, ptr2, idx2); + CN_STEP3(3, ax3, bx30, bx31, cx3, l3, ptr3, idx3); + CN_STEP3(4, ax4, bx40, bx41, cx4, l4, ptr4, idx4); - CN_STEP4(ax0, bx0, cx0, l0, mc0, ptr0, idx0); - CN_STEP4(ax1, bx1, cx1, l1, mc1, ptr1, idx1); - CN_STEP4(ax2, bx2, cx2, l2, mc2, ptr2, idx2); - CN_STEP4(ax3, bx3, cx3, l3, mc3, ptr3, idx3); - CN_STEP4(ax4, bx4, cx4, l4, mc4, ptr4, idx4); - - // ODD ROUND - CN_STEP1(ax0, cx0, bx0, l0, ptr0, idx0); - CN_STEP1(ax1, cx1, bx1, l1, ptr1, idx1); - CN_STEP1(ax2, cx2, bx2, l2, ptr2, idx2); - CN_STEP1(ax3, cx3, bx3, l3, ptr3, idx3); - CN_STEP1(ax4, cx4, bx4, l4, ptr4, idx4); - - CN_STEP2(ax0, cx0, bx0, l0, ptr0, idx0); - CN_STEP2(ax1, cx1, bx1, l1, ptr1, idx1); - CN_STEP2(ax2, cx2, bx2, l2, ptr2, idx2); - CN_STEP2(ax3, cx3, bx3, l3, ptr3, idx3); - CN_STEP2(ax4, cx4, bx4, l4, ptr4, idx4); - - CN_STEP3(ax0, cx0, bx0, l0, ptr0, idx0); - CN_STEP3(ax1, cx1, bx1, l1, ptr1, idx1); - CN_STEP3(ax2, cx2, bx2, l2, ptr2, idx2); - CN_STEP3(ax3, cx3, bx3, l3, ptr3, idx3); - CN_STEP3(ax4, cx4, bx4, l4, ptr4, idx4); - - CN_STEP4(ax0, cx0, bx0, l0, mc0, ptr0, idx0); - CN_STEP4(ax1, cx1, bx1, l1, mc1, ptr1, idx1); - CN_STEP4(ax2, cx2, bx2, l2, mc2, ptr2, idx2); - CN_STEP4(ax3, cx3, bx3, l3, mc3, ptr3, idx3); - CN_STEP4(ax4, cx4, bx4, l4, mc4, ptr4, idx4); + CN_STEP4(0, ax0, bx00, bx01, cx0, l0, mc0, ptr0, idx0); + CN_STEP4(1, ax1, bx10, bx11, cx1, l1, mc1, ptr1, idx1); + CN_STEP4(2, ax2, bx20, bx21, cx2, l2, mc2, ptr2, idx2); + CN_STEP4(3, ax3, bx30, bx31, cx3, l3, mc3, ptr3, idx3); + CN_STEP4(4, ax4, bx40, bx41, cx4, l4, mc4, ptr4, idx4); } for (size_t i = 0; i < 5; i++) { diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 7cef4f3a..bdf09af4 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -135,6 +135,17 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_quad_hash, cryptonight_penta_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_single_hash, + cryptonight_double_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + cryptonight_triple_hash, + cryptonight_quad_hash, + cryptonight_penta_hash, + # ifndef XMRIG_NO_AEON cryptonight_single_hash, cryptonight_double_hash, @@ -164,6 +175,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XHV nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XAO nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RTO + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_2 # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, @@ -173,6 +185,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif # ifndef XMRIG_NO_SUMO @@ -216,6 +229,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XAO nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RTO + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_2 # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, @@ -225,6 +239,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif }; diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 5d43875c..475f99be 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -55,6 +55,7 @@ bool MultiWorker::selfTest() if (m_thread->algorithm() == CRYPTONIGHT) { return verify(VARIANT_0, test_output_v0) && verify(VARIANT_1, test_output_v1) && + verify(VARIANT_2, test_output_v2) && verify(VARIANT_XTL, test_output_xtl) && verify(VARIANT_MSR, test_output_msr) && verify(VARIANT_XAO, test_output_xao) && From f151c7131d4101b089ecb232d89030c4ceeafc76 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Wed, 29 Aug 2018 19:52:33 +0200 Subject: [PATCH 318/389] Removed excess code for v0 & v1 --- src/crypto/CryptoNight_arm.h | 10 +++++++--- src/crypto/CryptoNight_x86.h | 14 ++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 7f273765..1df4ec5d 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -527,7 +527,9 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si idx0 = d ^ q; } } - bx1 = bx0; + if (VARIANT == xmrig::VARIANT_2) { + bx1 = bx0; + } bx0 = cx; } @@ -699,9 +701,11 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si idx1 = d ^ q; } } - bx01 = bx00; + if (VARIANT == xmrig::VARIANT_2) { + bx01 = bx00; + bx11 = bx10; + } bx00 = cx0; - bx11 = bx10; bx10 = cx1; } diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index e234d41d..e134abac 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -553,7 +553,9 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si idx0 = d ^ q; ptr0 = (uint64_t*)&l0[idx0 & MASK]; } - bx1 = bx0; + if (VARIANT == xmrig::VARIANT_2) { + bx1 = bx0; + } bx0 = cx; } @@ -731,9 +733,11 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si ptr1 = (uint64_t*)&l1[idx1 & MASK]; } - bx01 = bx00; + if (VARIANT == xmrig::VARIANT_2) { + bx01 = bx00; + bx11 = bx10; + } bx00 = cx0; - bx11 = bx10; bx10 = cx1; } @@ -812,7 +816,9 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si \ idx = d ^ q; \ } \ - b1 = b0; \ + if (VARIANT == xmrig::VARIANT_2) { \ + b1 = b0; \ + } \ b0 = c; From 7c4b71878910ea6fa3f92d8ede7ccb3eb4c88d7b Mon Sep 17 00:00:00 2001 From: SChernykh Date: Wed, 29 Aug 2018 20:15:08 +0200 Subject: [PATCH 319/389] Fixed performance degradation for v0 & v1 --- src/crypto/CryptoNight_x86.h | 90 ++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 51 deletions(-) diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index e134abac..31d86105 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -481,12 +481,11 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si __m128i bx1 = _mm_set_epi64x(h0[9] ^ h0[11], h0[8] ^ h0[10]); uint64_t idx0 = al0; - uint64_t* ptr0 = (uint64_t*) &l0[idx0 & MASK]; for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx; if (VARIANT == xmrig::VARIANT_TUBE || !SOFT_AES) { - cx = _mm_load_si128((__m128i *) ptr0); + cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); } const __m128i ax0 = _mm_set_epi64x(ah0, al0); @@ -494,24 +493,23 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si cx = aes_round_tweak_div(cx, ax0); } else if (SOFT_AES) { - cx = soft_aesenc((uint32_t*) ptr0, ax0); + cx = soft_aesenc((uint32_t*) &l0[idx0 & MASK], ax0); } else { cx = _mm_aesenc_si128(cx, ax0); } if (IS_V1 || VARIANT == xmrig::VARIANT_2) { - cryptonight_monero_tweak(ptr0, l0, idx0 & MASK, ax0, bx0, bx1, cx); + cryptonight_monero_tweak((uint64_t*) &l0[idx0 & MASK], l0, idx0 & MASK, ax0, bx0, bx1, cx); } else { - _mm_store_si128((__m128i *) ptr0, _mm_xor_si128(bx0, cx)); + _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } idx0 = EXTRACT64(cx); - ptr0 = (uint64_t*) &l0[idx0 & MASK]; uint64_t hi, lo, cl, ch; - cl = ptr0[0]; - ch = ptr0[1]; + cl = ((uint64_t*) &l0[idx0 & MASK])[0]; + ch = ((uint64_t*) &l0[idx0 & MASK])[1]; if (VARIANT == xmrig::VARIANT_2) { VARIANT2_INTEGER_MATH(0, cl, cx); lo = __umul128(idx0, cl, &hi); @@ -524,34 +522,32 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si al0 += hi; ah0 += lo; - ptr0[0] = al0; + ((uint64_t*) &l0[idx0 & MASK])[0] = al0; if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { - ptr0[1] = ah0 ^ tweak1_2_0 ^ al0; + ((uint64_t*) &l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else if (IS_V1) { - ptr0[1] = ah0 ^ tweak1_2_0; + ((uint64_t*) &l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; } else { - ptr0[1] = ah0; + ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; } al0 ^= cl; ah0 ^= ch; idx0 = al0; - ptr0 = (uint64_t*) &l0[idx0 & MASK]; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)ptr0)[0]; - int32_t d = ((int32_t*)ptr0)[2]; + int64_t n = ((int64_t*) &l0[idx0 & MASK])[0]; + int32_t d = ((int32_t*) &l0[idx0 & MASK])[2]; int64_t q = n / (d | 0x5); - ((int64_t*) ptr0)[0] = n ^ q; + ((int64_t*) &l0[idx0 & MASK])[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { d = ~d; } idx0 = d ^ q; - ptr0 = (uint64_t*)&l0[idx0 & MASK]; } if (VARIANT == xmrig::VARIANT_2) { bx1 = bx0; @@ -608,14 +604,12 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si uint64_t idx0 = al0; uint64_t idx1 = al1; - uint64_t* ptr0 = (uint64_t*)&l0[idx0 & MASK]; - uint64_t* ptr1 = (uint64_t*)&l1[idx1 & MASK]; for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx0, cx1; if (VARIANT == xmrig::VARIANT_TUBE || !SOFT_AES) { - cx0 = _mm_load_si128((__m128i *) ptr0); - cx1 = _mm_load_si128((__m128i *) ptr1); + cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); + cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); } const __m128i ax0 = _mm_set_epi64x(ah0, al0); @@ -625,8 +619,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si cx1 = aes_round_tweak_div(cx1, ax1); } else if (SOFT_AES) { - cx0 = soft_aesenc((uint32_t*)ptr0, ax0); - cx1 = soft_aesenc((uint32_t*)ptr1, ax1); + cx0 = soft_aesenc((uint32_t*) &l0[idx0 & MASK], ax0); + cx1 = soft_aesenc((uint32_t*) &l1[idx1 & MASK], ax1); } else { cx0 = _mm_aesenc_si128(cx0, ax0); @@ -634,21 +628,19 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si } if (IS_V1 || (VARIANT == xmrig::VARIANT_2)) { - cryptonight_monero_tweak(ptr0, l0, idx0 & MASK, ax0, bx00, bx01, cx0); - cryptonight_monero_tweak(ptr1, l1, idx1 & MASK, ax1, bx10, bx11, cx1); + cryptonight_monero_tweak((uint64_t*) &l0[idx0 & MASK], l0, idx0 & MASK, ax0, bx00, bx01, cx0); + cryptonight_monero_tweak((uint64_t*) &l1[idx1 & MASK], l1, idx1 & MASK, ax1, bx10, bx11, cx1); } else { - _mm_store_si128((__m128i *) ptr0, _mm_xor_si128(bx00, cx0)); - _mm_store_si128((__m128i *) ptr1, _mm_xor_si128(bx10, cx1)); + _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx00, cx0)); + _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx10, cx1)); } idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); - ptr0 = (uint64_t*)&l0[idx0 & MASK]; - ptr1 = (uint64_t*)&l1[idx1 & MASK]; uint64_t hi, lo, cl, ch; - cl = ((uint64_t*)ptr0)[0]; - ch = ((uint64_t*)ptr0)[1]; + cl = ((uint64_t*) &l0[idx0 & MASK])[0]; + ch = ((uint64_t*) &l0[idx0 & MASK])[1]; if (VARIANT == xmrig::VARIANT_2) { VARIANT2_INTEGER_MATH(0, cl, cx0); lo = __umul128(idx0, cl, &hi); @@ -660,38 +652,36 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si al0 += hi; ah0 += lo; - ((uint64_t*)ptr0)[0] = al0; + ((uint64_t*) &l0[idx0 & MASK])[0] = al0; if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { - ((uint64_t*)ptr0)[1] = ah0 ^ tweak1_2_0 ^ al0; + ((uint64_t*) &l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else if (IS_V1) { - ((uint64_t*)ptr0)[1] = ah0 ^ tweak1_2_0; + ((uint64_t*) &l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; } else { - ((uint64_t*)ptr0)[1] = ah0; + ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; } al0 ^= cl; ah0 ^= ch; idx0 = al0; - ptr0 = (uint64_t*)&l0[idx0 & MASK]; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)ptr0)[0]; - int32_t d = ((int32_t*)ptr0)[2]; + int64_t n = ((int64_t*) &l0[idx0 & MASK])[0]; + int32_t d = ((int32_t*) &l0[idx0 & MASK])[2]; int64_t q = n / (d | 0x5); - ((int64_t*)ptr0)[0] = n ^ q; + ((int64_t*) &l0[idx0 & MASK])[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { d = ~d; } idx0 = d ^ q; - ptr0 = (uint64_t*)&l0[idx0 & MASK]; } - cl = ptr1[0]; - ch = ptr1[1]; + cl = ((uint64_t*) &l1[idx1 & MASK])[0]; + ch = ((uint64_t*) &l1[idx1 & MASK])[1]; if (VARIANT == xmrig::VARIANT_2) { VARIANT2_INTEGER_MATH(1, cl, cx1); lo = __umul128(idx1, cl, &hi); @@ -703,34 +693,32 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si al1 += hi; ah1 += lo; - ptr1[0] = al1; + ((uint64_t*)&l1[idx1 & MASK])[0] = al1; if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { - ptr1[1] = ah1 ^ tweak1_2_1 ^ al1; + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1 ^ al1; } else if (IS_V1) { - ptr1[1] = ah1 ^ tweak1_2_1; + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1 ^ tweak1_2_1; } else { - ptr1[1] = ah1; + ((uint64_t*)&l1[idx1 & MASK])[1] = ah1; } al1 ^= cl; ah1 ^= ch; idx1 = al1; - ptr1 = (uint64_t*)&l1[idx1 & MASK]; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*)ptr1)[0]; - int32_t d = ((int32_t*)ptr1)[2]; + int64_t n = ((int64_t*) &l1[idx1 & MASK])[0]; + int32_t d = ((int32_t*) &l1[idx1 & MASK])[2]; int64_t q = n / (d | 0x5); - ((int64_t*)ptr1)[0] = n ^ q; + ((int64_t*) &l1[idx1 & MASK])[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { d = ~d; } idx1 = d ^ q; - ptr1 = (uint64_t*)&l1[idx1 & MASK]; } if (VARIANT == xmrig::VARIANT_2) { From ea1658c8181d4ff8258ba0575798e7b105ddde25 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Wed, 29 Aug 2018 20:27:17 +0200 Subject: [PATCH 320/389] Fixed spaces --- src/crypto/CryptoNight_x86.h | 42 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 31d86105..00cf60b6 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -493,16 +493,16 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si cx = aes_round_tweak_div(cx, ax0); } else if (SOFT_AES) { - cx = soft_aesenc((uint32_t*) &l0[idx0 & MASK], ax0); + cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], ax0); } else { cx = _mm_aesenc_si128(cx, ax0); } if (IS_V1 || VARIANT == xmrig::VARIANT_2) { - cryptonight_monero_tweak((uint64_t*) &l0[idx0 & MASK], l0, idx0 & MASK, ax0, bx0, bx1, cx); + cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], l0, idx0 & MASK, ax0, bx0, bx1, cx); } else { - _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } idx0 = EXTRACT64(cx); @@ -522,14 +522,14 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si al0 += hi; ah0 += lo; - ((uint64_t*) &l0[idx0 & MASK])[0] = al0; + ((uint64_t*)&l0[idx0 & MASK])[0] = al0; if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { - ((uint64_t*) &l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; } else if (IS_V1) { - ((uint64_t*) &l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0; } else { - ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; } al0 ^= cl; @@ -537,11 +537,11 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si idx0 = al0; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*) &l0[idx0 & MASK])[0]; - int32_t d = ((int32_t*) &l0[idx0 & MASK])[2]; + int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; + int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; int64_t q = n / (d | 0x5); - ((int64_t*) &l0[idx0 & MASK])[0] = n ^ q; + ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { d = ~d; @@ -619,8 +619,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si cx1 = aes_round_tweak_div(cx1, ax1); } else if (SOFT_AES) { - cx0 = soft_aesenc((uint32_t*) &l0[idx0 & MASK], ax0); - cx1 = soft_aesenc((uint32_t*) &l1[idx1 & MASK], ax1); + cx0 = soft_aesenc((uint32_t*)&l0[idx0 & MASK], ax0); + cx1 = soft_aesenc((uint32_t*)&l1[idx1 & MASK], ax1); } else { cx0 = _mm_aesenc_si128(cx0, ax0); @@ -628,8 +628,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si } if (IS_V1 || (VARIANT == xmrig::VARIANT_2)) { - cryptonight_monero_tweak((uint64_t*) &l0[idx0 & MASK], l0, idx0 & MASK, ax0, bx00, bx01, cx0); - cryptonight_monero_tweak((uint64_t*) &l1[idx1 & MASK], l1, idx1 & MASK, ax1, bx10, bx11, cx1); + cryptonight_monero_tweak((uint64_t*)&l0[idx0 & MASK], l0, idx0 & MASK, ax0, bx00, bx01, cx0); + cryptonight_monero_tweak((uint64_t*)&l1[idx1 & MASK], l1, idx1 & MASK, ax1, bx10, bx11, cx1); } else { _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx00, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx10, cx1)); @@ -652,7 +652,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si al0 += hi; ah0 += lo; - ((uint64_t*) &l0[idx0 & MASK])[0] = al0; + ((uint64_t*)&l0[idx0 & MASK])[0] = al0; if (IS_V1 && (VARIANT == xmrig::VARIANT_TUBE || VARIANT == xmrig::VARIANT_RTO)) { ((uint64_t*) &l0[idx0 & MASK])[1] = ah0 ^ tweak1_2_0 ^ al0; @@ -667,11 +667,11 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si idx0 = al0; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*) &l0[idx0 & MASK])[0]; - int32_t d = ((int32_t*) &l0[idx0 & MASK])[2]; + int64_t n = ((int64_t*)&l0[idx0 & MASK])[0]; + int32_t d = ((int32_t*)&l0[idx0 & MASK])[2]; int64_t q = n / (d | 0x5); - ((int64_t*) &l0[idx0 & MASK])[0] = n ^ q; + ((int64_t*)&l0[idx0 & MASK])[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { d = ~d; @@ -708,11 +708,11 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si idx1 = al1; if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { - int64_t n = ((int64_t*) &l1[idx1 & MASK])[0]; - int32_t d = ((int32_t*) &l1[idx1 & MASK])[2]; + int64_t n = ((int64_t*)&l1[idx1 & MASK])[0]; + int32_t d = ((int32_t*)&l1[idx1 & MASK])[2]; int64_t q = n / (d | 0x5); - ((int64_t*) &l1[idx1 & MASK])[0] = n ^ q; + ((int64_t*)&l1[idx1 & MASK])[0] = n ^ q; if (VARIANT == xmrig::VARIANT_XHV) { d = ~d; From 57479cef8c440f8c8d2e83ca5ea8b7ae03949e7b Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 31 Aug 2018 18:01:15 +0300 Subject: [PATCH 321/389] Sync changes. --- src/common/api/HttpRequest.cpp | 2 +- src/common/config/CommonConfig.cpp | 4 ++++ src/common/config/CommonConfig.h | 2 ++ src/common/crypto/keccak.h | 6 +++--- src/common/interfaces/IConfig.h | 17 ++++++++++++++--- src/common/interfaces/IControllerListener.h | 6 +++--- src/common/utils/timestamp.h | 8 ++++---- 7 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/common/api/HttpRequest.cpp b/src/common/api/HttpRequest.cpp index 01245dfc..6898a385 100644 --- a/src/common/api/HttpRequest.cpp +++ b/src/common/api/HttpRequest.cpp @@ -147,7 +147,7 @@ int xmrig::HttpRequest::end(int status, MHD_Response *rsp) MHD_add_response_header(rsp, "Content-Type", "application/json"); MHD_add_response_header(rsp, "Access-Control-Allow-Origin", "*"); MHD_add_response_header(rsp, "Access-Control-Allow-Methods", "GET, PUT"); - MHD_add_response_header(rsp, "Access-Control-Allow-Headers", "Authorization"); + MHD_add_response_header(rsp, "Access-Control-Allow-Headers", "Authorization, Content-Type"); const int ret = MHD_queue_response(m_connection, status, rsp); MHD_destroy_response(rsp); diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 7e43b39d..ca901757 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -251,6 +251,10 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) m_apiWorkerId = arg; break; + case ApiIdKey: /* --api-id */ + m_apiId = arg; + break; + case UserAgentKey: /* --user-agent */ m_userAgent = arg; break; diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index ffebb6b2..fa27ea6a 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -49,6 +49,7 @@ public: inline bool isColors() const { return m_colors; } inline bool isDryRun() const { return m_dryRun; } inline bool isSyslog() const { return m_syslog; } + inline const char *apiId() const { return m_apiId.data(); } inline const char *apiToken() const { return m_apiToken.data(); } inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *logFile() const { return m_logFile.data(); } @@ -97,6 +98,7 @@ protected: State m_state; std::vector m_activePools; std::vector m_pools; + xmrig::c_str m_apiId; xmrig::c_str m_apiToken; xmrig::c_str m_apiWorkerId; xmrig::c_str m_fileName; diff --git a/src/common/crypto/keccak.h b/src/common/crypto/keccak.h index da8d6c52..6121044a 100644 --- a/src/common/crypto/keccak.h +++ b/src/common/crypto/keccak.h @@ -23,8 +23,8 @@ */ -#ifndef KECCAK_H_ -#define KECCAK_H_ +#ifndef XMRIG_KECCAK_H +#define XMRIG_KECCAK_H #include #include @@ -52,4 +52,4 @@ void keccakf(uint64_t st[25], int norounds); } /* namespace xmrig */ -#endif /* KECCAK_H_ */ +#endif /* XMRIG_KECCAK_H */ diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index 4b3f8788..fb622e37 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -20,8 +20,8 @@ * along with this program. If not, see . */ -#ifndef __ICONFIG_H__ -#define __ICONFIG_H__ +#ifndef XMRIG_ICONFIG_H +#define XMRIG_ICONFIG_H #include "common/crypto/Algorithm.h" @@ -42,6 +42,7 @@ public: ApiPort = 4000, ApiRestrictedKey = 4004, ApiWorkerIdKey = 4002, + ApiIdKey = 4005, BackgroundKey = 'B', ColorKey = 1002, ConfigKey = 'c', @@ -97,6 +98,16 @@ public: PoolCoinKey = 'C', ReuseTimeoutKey = 1106, WorkersKey = 1103, + WorkersAdvKey = 1107, + + // xmrig nvidia + CudaMaxThreadsKey = 1200, + CudaBFactorKey = 1201, + CudaBSleepKey = 1202, + CudaDevicesKey = 1203, + CudaLaunchKey = 1204, + CudaAffinityKey = 1205, + CudaMaxUsageKey = 1206, }; virtual ~IConfig() {} @@ -118,4 +129,4 @@ public: } /* namespace xmrig */ -#endif // __ICONFIG_H__ +#endif // XMRIG_ICONFIG_H diff --git a/src/common/interfaces/IControllerListener.h b/src/common/interfaces/IControllerListener.h index d6077138..35249bcd 100644 --- a/src/common/interfaces/IControllerListener.h +++ b/src/common/interfaces/IControllerListener.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __ICONTROLLERLISTENER_H__ -#define __ICONTROLLERLISTENER_H__ +#ifndef XMRIG_ICONTROLLERLISTENER_H +#define XMRIG_ICONTROLLERLISTENER_H namespace xmrig { @@ -43,4 +43,4 @@ public: } /* namespace xmrig */ -#endif // __ICONTROLLERLISTENER_H__ +#endif // XMRIG_ICONTROLLERLISTENER_H diff --git a/src/common/utils/timestamp.h b/src/common/utils/timestamp.h index b4404b2e..6b6a8ab2 100644 --- a/src/common/utils/timestamp.h +++ b/src/common/utils/timestamp.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __TIMESTAMP_H__ -#define __TIMESTAMP_H__ +#ifndef XMRIG_TIMESTAMP_H +#define XMRIG_TIMESTAMP_H #include @@ -31,7 +31,7 @@ namespace xmrig { -int64_t currentMSecsSinceEpoch() +static inline int64_t currentMSecsSinceEpoch() { using namespace std::chrono; if (high_resolution_clock::is_steady) { @@ -44,4 +44,4 @@ int64_t currentMSecsSinceEpoch() } /* namespace xmrig */ -#endif /* __TIMESTAMP_H__ */ +#endif /* XMRIG_TIMESTAMP_H */ From 73fca9114e032a2c0866d6e8f369fc001b323107 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 31 Aug 2018 18:30:51 +0300 Subject: [PATCH 322/389] * Fixed API ID collision. --- src/api/ApiRouter.cpp | 18 ++++++++++++------ src/api/ApiRouter.h | 4 ++-- src/core/Config.cpp | 1 + src/core/ConfigLoader_platform.h | 17 ++++++++++------- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 07e425f1..2c62696a 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -42,7 +42,6 @@ #include "core/Controller.h" #include "Cpu.h" #include "interfaces/IThread.h" -#include "Mem.h" #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" #include "rapidjson/stringbuffer.h" @@ -67,7 +66,7 @@ ApiRouter::ApiRouter(xmrig::Controller *controller) : memset(m_workerId, 0, sizeof(m_workerId)); setWorkerId(controller->config()->apiWorkerId()); - genId(); + genId(controller->config()->apiId()); } @@ -145,10 +144,15 @@ void ApiRouter::finalize(xmrig::HttpReply &reply, rapidjson::Document &doc) cons } -void ApiRouter::genId() +void ApiRouter::genId(const char *id) { memset(m_id, 0, sizeof(m_id)); + if (id && strlen(id) > 0) { + strncpy(m_id, id, sizeof(m_id) - 1); + return; + } + uv_interface_address_t *interfaces; int count = 0; @@ -160,11 +164,13 @@ void ApiRouter::genId() if (!interfaces[i].is_internal && interfaces[i].address.address4.sin_family == AF_INET) { uint8_t hash[200]; const size_t addrSize = sizeof(interfaces[i].phys_addr); - const size_t inSize = strlen(APP_KIND) + addrSize; + const size_t inSize = strlen(APP_KIND) + addrSize + sizeof(uint16_t); + const uint16_t port = static_cast(m_controller->config()->apiPort()); uint8_t *input = new uint8_t[inSize](); - memcpy(input, interfaces[i].phys_addr, addrSize); - memcpy(input + addrSize, APP_KIND, strlen(APP_KIND)); + memcpy(input, &port, sizeof(uint16_t)); + memcpy(input + sizeof(uint16_t), interfaces[i].phys_addr, addrSize); + memcpy(input + sizeof(uint16_t) + addrSize, APP_KIND, strlen(APP_KIND)); xmrig::keccak(input, inSize, hash); Job::toHex(hash, 8, m_id); diff --git a/src/api/ApiRouter.h b/src/api/ApiRouter.h index 9e32cdae..b781d5a2 100644 --- a/src/api/ApiRouter.h +++ b/src/api/ApiRouter.h @@ -56,7 +56,7 @@ protected: private: void finalize(xmrig::HttpReply &reply, rapidjson::Document &doc) const; - void genId(); + void genId(const char *id); void getConnection(rapidjson::Document &doc) const; void getHashrate(rapidjson::Document &doc) const; void getIdentify(rapidjson::Document &doc) const; @@ -66,7 +66,7 @@ private: void setWorkerId(const char *id); void updateWorkerId(const char *id, const char *previousId); - char m_id[17]; + char m_id[32]; char m_workerId[128]; NetworkState m_network; xmrig::Controller *m_controller; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index fa6afdb4..0380c26d 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -75,6 +75,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const Value api(kObjectType); api.AddMember("port", apiPort(), allocator); api.AddMember("access-token", apiToken() ? Value(StringRef(apiToken())).Move() : Value(kNullType).Move(), allocator); + api.AddMember("id", apiId() ? Value(StringRef(apiId())).Move() : Value(kNullType).Move(), allocator); api.AddMember("worker-id", apiWorkerId() ? Value(StringRef(apiWorkerId())).Move() : Value(kNullType).Move(), allocator); api.AddMember("ipv6", isApiIPv6(), allocator); api.AddMember("restricted", isApiRestricted(), allocator); diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index bc6657d1..98724271 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -22,8 +22,8 @@ * along with this program. If not, see . */ -#ifndef __CONFIGLOADER_PLATFORM_H__ -#define __CONFIGLOADER_PLATFORM_H__ +#ifndef XMRIG_CONFIGLOADER_PLATFORM_H +#define XMRIG_CONFIGLOADER_PLATFORM_H #ifdef _MSC_VER @@ -86,6 +86,7 @@ Options:\n\ --api-port=N port for the miner API\n\ --api-access-token=T access token for API\n\ --api-worker-id=ID custom worker-id for API\n\ + --api-id=ID custom instance ID for API\n\ --api-ipv6 enable IPv6 support for API\n\ --api-no-restricted enable full remote access (only if API token set)\n\ -h, --help display this help and exit\n\ @@ -101,6 +102,7 @@ static struct option const options[] = { { "api-access-token", 1, nullptr, xmrig::IConfig::ApiAccessTokenKey }, { "api-port", 1, nullptr, xmrig::IConfig::ApiPort }, { "api-worker-id", 1, nullptr, xmrig::IConfig::ApiWorkerIdKey }, + { "api-id", 1, nullptr, xmrig::IConfig::ApiIdKey }, { "api-ipv6", 0, nullptr, xmrig::IConfig::ApiIPv6Key }, { "api-no-restricted", 0, nullptr, xmrig::IConfig::ApiRestrictedKey }, { "av", 1, nullptr, xmrig::IConfig::AVKey }, @@ -131,7 +133,7 @@ static struct option const options[] = { { "userpass", 1, nullptr, xmrig::IConfig::UserpassKey }, { "rig-id", 1, nullptr, xmrig::IConfig::RigIdKey }, { "version", 0, nullptr, xmrig::IConfig::VersionKey }, - { 0, 0, 0, 0 } + { nullptr, 0, nullptr, 0 } }; @@ -155,7 +157,7 @@ static struct option const config_options[] = { { "threads", 1, nullptr, xmrig::IConfig::ThreadsKey }, { "user-agent", 1, nullptr, xmrig::IConfig::UserAgentKey }, { "hw-aes", 0, nullptr, xmrig::IConfig::HardwareAESKey }, - { 0, 0, 0, 0 } + { nullptr, 0, nullptr, 0 } }; @@ -168,7 +170,7 @@ static struct option const pool_options[] = { { "keepalive", 2, nullptr, xmrig::IConfig::KeepAliveKey }, { "variant", 1, nullptr, xmrig::IConfig::VariantKey }, { "rig-id", 1, nullptr, xmrig::IConfig::RigIdKey }, - { 0, 0, 0, 0 } + { nullptr, 0, nullptr, 0 } }; @@ -178,10 +180,11 @@ static struct option const api_options[] = { { "worker-id", 1, nullptr, xmrig::IConfig::ApiWorkerIdKey }, { "ipv6", 0, nullptr, xmrig::IConfig::ApiIPv6Key }, { "restricted", 0, nullptr, xmrig::IConfig::ApiRestrictedKey }, - { 0, 0, 0, 0 } + { "id", 1, nullptr, xmrig::IConfig::ApiIdKey }, + { nullptr, 0, nullptr, 0 } }; } /* namespace xmrig */ -#endif /* __CONFIGLOADER_PLATFORM_H__ */ +#endif /* XMRIG_CONFIGLOADER_PLATFORM_H */ From f11aad515b47e4ee0ce73f8b12df5c2081f87836 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 31 Aug 2018 21:51:34 +0300 Subject: [PATCH 323/389] Fixed gcc build. --- src/crypto/CryptoNight_monero.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index 3fb18d00..0ceb93fe 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -22,9 +23,10 @@ * along with this program. If not, see . */ -#ifndef __CRYPTONIGHT_MONERO_H__ -#define __CRYPTONIGHT_MONERO_H__ +#ifndef XMRIG_CRYPTONIGHT_MONERO_H +#define XMRIG_CRYPTONIGHT_MONERO_H +#include #include // VARIANT ALTERATIONS @@ -121,4 +123,4 @@ vst1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x30)), vaddq_u64(chunk2, vreinterpretq_u64_u8(_a))); \ } while (0) #endif -#endif /* __CRYPTONIGHT_MONERO_H__ */ +#endif /* XMRIG_CRYPTONIGHT_MONERO_H */ From eef4d9b10212beaded5a50b013486b6f06d057ea Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 31 Aug 2018 23:19:10 +0300 Subject: [PATCH 324/389] Fixed 32 bit support. --- src/crypto/CryptoNight_x86.h | 67 ++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 00cf60b6..a0b676a5 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -22,8 +23,8 @@ * along with this program. If not, see . */ -#ifndef __CRYPTONIGHT_X86_H__ -#define __CRYPTONIGHT_X86_H__ +#ifndef XMRIG_CRYPTONIGHT_X86_H +#define XMRIG_CRYPTONIGHT_X86_H #ifdef __GNUC__ @@ -73,10 +74,7 @@ static inline void do_skein_hash(const uint8_t *input, size_t len, uint8_t *outp void (* const extra_hashes[4])(const uint8_t *, size_t, uint8_t *) = {do_blake_hash, do_groestl_hash, do_jh_hash, do_skein_hash}; - #if defined(__x86_64__) || defined(_M_AMD64) -# define EXTRACT64(X) _mm_cvtsi128_si64(X) - # ifdef __GNUC__ static inline uint64_t __umul128(uint64_t a, uint64_t b, uint64_t* hi) { @@ -88,13 +86,14 @@ static inline uint64_t __umul128(uint64_t a, uint64_t b, uint64_t* hi) #define __umul128 _umul128 # endif #elif defined(__i386__) || defined(_M_IX86) -# define HI32(X) \ - _mm_srli_si128((X), 4) +static inline int64_t _mm_cvtsi128_si64(__m128i a) +{ + return ((uint64_t)(uint32_t)_mm_cvtsi128_si32(a) | ((uint64_t)(uint32_t)_mm_cvtsi128_si32(_mm_srli_si128(a, 4)) << 32)); +} - -# define EXTRACT64(X) \ - ((uint64_t)(uint32_t)_mm_cvtsi128_si32(X) | \ - ((uint64_t)(uint32_t)_mm_cvtsi128_si32(HI32(X)) << 32)) +static inline __m128i _mm_cvtsi64_si128(int64_t a) { + return _mm_set_epi64x(0, a); +} static inline uint64_t __umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *product_hi) { // multiplier = ab = a * 2^32 + b @@ -418,11 +417,11 @@ static inline __m128i int_sqrt_v2(const uint64_t n0) r >>= 19; uint64_t x2 = (s - (1022ULL << 32)) * (r - s - (1022ULL << 32) + 1); -#if defined _MSC_VER || (__GNUC__ >= 7) +# if (defined _MSC_VER || (__GNUC__ >= 7)) && (defined(__x86_64__) || defined(_M_AMD64)) _addcarry_u64(_subborrow_u64(0, x2, n0, (unsigned long long int*)&x2), r, 0, (unsigned long long int*)&r); -#else +# else if (x2 < n0) ++r; -#endif +# endif return _mm_cvtsi64_si128(r); } @@ -436,10 +435,10 @@ static inline void cryptonight_monero_tweak(uint64_t* mem_out, const uint8_t* l, _mm_store_si128((__m128i *)mem_out, _mm_xor_si128(bx0, cx)); } else { __m128i tmp = _mm_xor_si128(bx0, cx); - mem_out[0] = EXTRACT64(tmp); + mem_out[0] = _mm_cvtsi128_si64(tmp); tmp = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(tmp), _mm_castsi128_ps(tmp))); - uint64_t vh = EXTRACT64(tmp); + uint64_t vh = _mm_cvtsi128_si64(tmp); uint8_t x = static_cast(vh >> 24); static const uint16_t table = 0x7531; @@ -505,7 +504,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } - idx0 = EXTRACT64(cx); + idx0 = _mm_cvtsi128_si64(cx); uint64_t hi, lo, cl, ch; cl = ((uint64_t*) &l0[idx0 & MASK])[0]; @@ -635,8 +634,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx10, cx1)); } - idx0 = EXTRACT64(cx0); - idx1 = EXTRACT64(cx1); + idx0 = _mm_cvtsi128_si64(cx0); + idx1 = _mm_cvtsi128_si64(cx1); uint64_t hi, lo, cl, ch; cl = ((uint64_t*) &l0[idx0 & MASK])[0]; @@ -763,7 +762,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si #define CN_STEP3(part, a, b0, b1, c, l, ptr, idx) \ - idx = EXTRACT64(c); \ + idx = _mm_cvtsi128_si64(c); \ ptr = reinterpret_cast<__m128i*>(&l[idx & MASK]); \ uint64_t cl##part = ((uint64_t*)ptr)[0]; \ uint64_t ch##part = ((uint64_t*)ptr)[1]; @@ -791,7 +790,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si } \ \ a = _mm_xor_si128(a, _mm_set_epi64x(ch##part, cl##part)); \ - idx = EXTRACT64(a); \ + idx = _mm_cvtsi128_si64(a); \ \ if (ALGO == xmrig::CRYPTONIGHT_HEAVY) { \ int64_t n = ((int64_t*)&l[idx & MASK])[0]; \ @@ -859,9 +858,9 @@ inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t si VARIANT2_SET_ROUNDING_MODE(); uint64_t idx0, idx1, idx2; - idx0 = EXTRACT64(ax0); - idx1 = EXTRACT64(ax1); - idx2 = EXTRACT64(ax2); + idx0 = _mm_cvtsi128_si64(ax0); + idx1 = _mm_cvtsi128_si64(ax1); + idx2 = _mm_cvtsi128_si64(ax2); for (size_t i = 0; i < ITERATIONS; i++) { uint64_t hi, lo; @@ -926,10 +925,10 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size VARIANT2_SET_ROUNDING_MODE(); uint64_t idx0, idx1, idx2, idx3; - idx0 = EXTRACT64(ax0); - idx1 = EXTRACT64(ax1); - idx2 = EXTRACT64(ax2); - idx3 = EXTRACT64(ax3); + idx0 = _mm_cvtsi128_si64(ax0); + idx1 = _mm_cvtsi128_si64(ax1); + idx2 = _mm_cvtsi128_si64(ax2); + idx3 = _mm_cvtsi128_si64(ax3); for (size_t i = 0; i < ITERATIONS; i++) { @@ -1002,11 +1001,11 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz VARIANT2_SET_ROUNDING_MODE(); uint64_t idx0, idx1, idx2, idx3, idx4; - idx0 = EXTRACT64(ax0); - idx1 = EXTRACT64(ax1); - idx2 = EXTRACT64(ax2); - idx3 = EXTRACT64(ax3); - idx4 = EXTRACT64(ax4); + idx0 = _mm_cvtsi128_si64(ax0); + idx1 = _mm_cvtsi128_si64(ax1); + idx2 = _mm_cvtsi128_si64(ax2); + idx3 = _mm_cvtsi128_si64(ax3); + idx4 = _mm_cvtsi128_si64(ax4); for (size_t i = 0; i < ITERATIONS; i++) { @@ -1045,4 +1044,4 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz } } -#endif /* __CRYPTONIGHT_X86_H__ */ +#endif /* XMRIG_CRYPTONIGHT_X86_H */ From 075565e6fa4f346c90ef6cb02028d52ab6363a64 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 31 Aug 2018 23:32:33 +0300 Subject: [PATCH 325/389] Fix invalid hashes with gcc 7.1. --- src/crypto/CryptoNight_x86.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index a0b676a5..71452524 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -417,7 +417,7 @@ static inline __m128i int_sqrt_v2(const uint64_t n0) r >>= 19; uint64_t x2 = (s - (1022ULL << 32)) * (r - s - (1022ULL << 32) + 1); -# if (defined _MSC_VER || (__GNUC__ >= 7)) && (defined(__x86_64__) || defined(_M_AMD64)) +# if (defined(_MSC_VER) || __GNUC__ > 7 || (__GNUC__ == 7 && __GNUC_MINOR__ > 1)) && (defined(__x86_64__) || defined(_M_AMD64)) _addcarry_u64(_subborrow_u64(0, x2, n0, (unsigned long long int*)&x2), r, 0, (unsigned long long int*)&r); # else if (x2 < n0) ++r; From 85946b0c4084943176945e931fcfd704607d3490 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 31 Aug 2018 23:55:50 +0300 Subject: [PATCH 326/389] Fixed numeric variant 2 in config file. --- src/common/crypto/Algorithm.cpp | 21 ++++++++++++++++----- src/common/crypto/Algorithm.h | 5 +++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index 05c890dc..a3cf48b2 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -176,11 +177,21 @@ void xmrig::Algorithm::parseVariant(const char *variant) void xmrig::Algorithm::parseVariant(int variant) { - if (variant >= VARIANT_AUTO && variant < VARIANT_MAX) { - m_variant = static_cast(variant); - } - else { - assert(false); + assert(variant >= -1 && variant <= 2); + + switch (variant) { + case -1: + case 0: + case 1: + m_variant = static_cast(variant); + break; + + case 2: + m_variant = VARIANT_2; + break; + + default: + break; } } diff --git a/src/common/crypto/Algorithm.h b/src/common/crypto/Algorithm.h index bcf029d8..731fa793 100644 --- a/src/common/crypto/Algorithm.h +++ b/src/common/crypto/Algorithm.h @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -22,8 +23,8 @@ * along with this program. If not, see . */ -#ifndef __ALGORITHM_H__ -#define __ALGORITHM_H__ +#ifndef XMRIG_ALGORITHM_H +#define XMRIG_ALGORITHM_H #include From b948474d014a01f69f05e6eed9c062d9eb3017f5 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 1 Sep 2018 00:00:38 +0300 Subject: [PATCH 327/389] Small fixes. --- src/crypto/CryptoNight_arm.h | 18 ++++++++---------- src/crypto/CryptoNight_x86.h | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 1df4ec5d..3ede8111 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -7,6 +7,7 @@ * Copyright 2016 Imran Yusuff * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -23,8 +24,8 @@ * along with this program. If not, see . */ -#ifndef __CRYPTONIGHT_ARM_H__ -#define __CRYPTONIGHT_ARM_H__ +#ifndef XMRIG_CRYPTONIGHT_ARM_H +#define XMRIG_CRYPTONIGHT_ARM_H #include "common/crypto/keccak.h" @@ -95,9 +96,6 @@ static inline __attribute__((always_inline)) uint64_t _mm_cvtsi128_si64(__m128i } -#define EXTRACT64(X) _mm_cvtsi128_si64(X) - - #if defined (__arm64__) || defined (__aarch64__) static inline uint64_t __umul128(uint64_t a, uint64_t b, uint64_t* hi) { @@ -404,7 +402,7 @@ static inline __m128i aes_round_tweak_div(const __m128i &in, const __m128i &key) } -template +template static inline void cryptonight_monero_tweak(const uint8_t* l, uint64_t idx, __m128i ax0, __m128i bx0, __m128i bx1, __m128i cx) { uint64_t* mem_out = (uint64_t*)&l[idx]; @@ -414,7 +412,7 @@ static inline void cryptonight_monero_tweak(const uint8_t* l, uint64_t idx, __m1 _mm_store_si128((__m128i *)mem_out, _mm_xor_si128(bx0, cx)); } else { __m128i tmp = _mm_xor_si128(bx0, cx); - mem_out[0] = EXTRACT64(tmp); + mem_out[0] = _mm_cvtsi128_si64(tmp); uint64_t vh = vgetq_lane_u64(tmp, 1); @@ -481,7 +479,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si _mm_store_si128((__m128i *)&l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); } - idx0 = EXTRACT64(cx); + idx0 = _mm_cvtsi128_si64(cx); uint64_t hi, lo, cl, ch; cl = ((uint64_t*) &l0[idx0 & MASK])[0]; @@ -612,8 +610,8 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx10, cx1)); } - idx0 = EXTRACT64(cx0); - idx1 = EXTRACT64(cx1); + idx0 = _mm_cvtsi128_si64(cx0); + idx1 = _mm_cvtsi128_si64(cx1); uint64_t hi, lo, cl, ch; cl = ((uint64_t*) &l0[idx0 & MASK])[0]; diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 71452524..bbed8c38 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -427,7 +427,7 @@ static inline __m128i int_sqrt_v2(const uint64_t n0) } -template +template static inline void cryptonight_monero_tweak(uint64_t* mem_out, const uint8_t* l, uint64_t idx, __m128i ax0, __m128i bx0, __m128i bx1, __m128i cx) { if (VARIANT == xmrig::VARIANT_2) { From a3873930a083ac80d28d879124a4f250005841b5 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 1 Sep 2018 00:31:31 +0300 Subject: [PATCH 328/389] Fixed automatic variant. --- src/common/net/Job.cpp | 14 ++------------ src/common/net/Job.h | 7 ++++--- src/common/net/Pool.cpp | 3 ++- src/common/net/Pool.h | 7 ++++--- src/net/Network.cpp | 5 ++++- src/workers/MultiWorker.cpp | 1 + src/workers/MultiWorker.h | 7 ++++--- 7 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index e9f81e02..3d2d88ad 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -165,24 +166,13 @@ bool Job::setTarget(const char *target) xmrig::Variant Job::variant() const { - if (m_algorithm.variant() == xmrig::VARIANT_XTL && m_blob[0] < 4) { - return xmrig::VARIANT_1; - } - - if (m_algorithm.variant() == xmrig::VARIANT_MSR && m_blob[0] < 7) { - return xmrig::VARIANT_1; - } - - if (m_algorithm.variant() == xmrig::VARIANT_XHV && m_blob[0] < 3) { - return xmrig::VARIANT_0; - } - if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { if (m_algorithm.algo() == xmrig::CRYPTONIGHT_HEAVY) { return xmrig::VARIANT_0; } else if (m_algorithm.algo() == xmrig::CRYPTONIGHT_LITE) { return xmrig::VARIANT_1; } + return (m_blob[0] >= 8) ? xmrig::VARIANT_2 : xmrig::VARIANT_1; } diff --git a/src/common/net/Job.h b/src/common/net/Job.h index 049eb7d4..7ea539a2 100644 --- a/src/common/net/Job.h +++ b/src/common/net/Job.h @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -22,8 +23,8 @@ * along with this program. If not, see . */ -#ifndef __JOB_H__ -#define __JOB_H__ +#ifndef XMRIG_JOB_H +#define XMRIG_JOB_H #include @@ -100,4 +101,4 @@ private: # endif }; -#endif /* __JOB_H__ */ +#endif /* XMRIG_JOB_H */ diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 357cb330..49f4e54c 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -360,7 +361,7 @@ void Pool::adjustVariant(const xmrig::Variant variantHint) if (m_algorithm.algo() == CRYPTONIGHT_HEAVY) { m_algorithm.setVariant(VARIANT_0); } - else { + else if (m_algorithm.algo() == CRYPTONIGHT_LITE) { m_algorithm.setVariant(VARIANT_1); } # endif diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index 57a30d1e..0641b851 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -21,8 +22,8 @@ * along with this program. If not, see . */ -#ifndef __POOL_H__ -#define __POOL_H__ +#ifndef XMRIG_POOL_H +#define XMRIG_POOL_H #include @@ -105,4 +106,4 @@ private: typedef std::vector Pools; -#endif /* __POOL_H__ */ +#endif /* XMRIG_POOL_H */ diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 7293a0ac..cc979635 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -166,9 +166,12 @@ bool Network::isColors() const void Network::setJob(Client *client, const Job &job, bool donate) { + xmrig::Algorithm algorithm = job.algorithm(); + algorithm.setVariant(job.variant()); + LOG_INFO(isColors() ? MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s") : "new job from %s:%d diff %d algo %s", - client->host(), client->port(), job.diff(), job.algorithm().shortName()); + client->host(), client->port(), job.diff(), algorithm.shortName()); m_state.diff = job.diff(); Workers::setJob(job, donate); diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 475f99be..1916b205 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify diff --git a/src/workers/MultiWorker.h b/src/workers/MultiWorker.h index d4a6910e..c08e4fbe 100644 --- a/src/workers/MultiWorker.h +++ b/src/workers/MultiWorker.h @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -22,8 +23,8 @@ * along with this program. If not, see . */ -#ifndef __MULTIWORKER_H__ -#define __MULTIWORKER_H__ +#ifndef XMRIG_MULTIWORKER_H +#define XMRIG_MULTIWORKER_H #include "common/net/Job.h" @@ -71,4 +72,4 @@ private: }; -#endif /* __MULTIWORKER_H__ */ +#endif /* XMRIG_MULTIWORKER_H */ From a27dfa8b708b36a187d4b6703ff8d43e3bc2bceb Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 3 Sep 2018 01:26:19 +0300 Subject: [PATCH 329/389] Add WITH_DEBUG_LOG CMake option. --- CMakeLists.txt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 07397ac4..62ec419e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 2.8) project(xmrig) -option(WITH_LIBCPUID "Use Libcpuid" ON) -option(WITH_AEON "CryptoNight-Lite support" ON) -option(WITH_SUMO "CryptoNight-Heavy support" ON) -option(WITH_HTTPD "HTTP REST API" ON) -option(BUILD_STATIC "Build static binary" OFF) +option(WITH_LIBCPUID "Use Libcpuid" ON) +option(WITH_AEON "CryptoNight-Lite support" ON) +option(WITH_SUMO "CryptoNight-Heavy support" ON) +option(WITH_HTTPD "HTTP REST API" ON) +option(WITH_DEBUG_LOG "Enable debug log output" OFF) +option(BUILD_STATIC "Build static binary" OFF) include (CheckIncludeFile) include (cmake/cpu.cmake) @@ -170,7 +171,6 @@ endif() add_definitions(/D__STDC_FORMAT_MACROS) add_definitions(/DUNICODE) -#add_definitions(/DAPP_DEBUG) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") @@ -246,5 +246,9 @@ if (BUILD_STATIC) set(CMAKE_EXE_LINKER_FLAGS " -static") endif() +if (WITH_DEBUG_LOG) + add_definitions(/DAPP_DEBUG) +endif() + add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES}) target_link_libraries(${PROJECT_NAME} ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) From bb3990a07694e31a3a1f490680ebc527b1ff2994 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 3 Sep 2018 15:39:55 +0300 Subject: [PATCH 330/389] Rewrite automatic variant handling. --- src/common/net/Client.cpp | 6 ++--- src/common/net/Job.cpp | 46 +++++++++++++++++++++++-------------- src/common/net/Job.h | 10 +++++--- src/net/Network.cpp | 5 +--- src/workers/MultiWorker.cpp | 2 +- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index f4553d97..2a9db444 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -270,17 +270,17 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) } if (params.HasMember("algo")) { - job.algorithm().parseAlgorithm(params["algo"].GetString()); + job.setAlgorithm(params["algo"].GetString()); } if (params.HasMember("variant")) { const rapidjson::Value &variant = params["variant"]; if (variant.IsInt()) { - job.algorithm().parseVariant(variant.GetInt()); + job.setVariant(variant.GetInt()); } else if (variant.IsString()){ - job.algorithm().parseVariant(variant.GetString()); + job.setVariant(variant.GetString()); } } diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index 3d2d88ad..bb5f6a12 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -59,6 +59,7 @@ static inline char hf_bin2hex(unsigned char c) Job::Job() : + m_autoVariant(false), m_nicehash(false), m_poolId(-2), m_threadId(-1), @@ -70,7 +71,8 @@ Job::Job() : } -Job::Job(int poolId, bool nicehash, xmrig::Algorithm algorithm, const xmrig::Id &clientId) : +Job::Job(int poolId, bool nicehash, const xmrig::Algorithm &algorithm, const xmrig::Id &clientId) : + m_autoVariant(algorithm.variant() == xmrig::VARIANT_AUTO), m_nicehash(nicehash), m_poolId(poolId), m_threadId(-1), @@ -113,6 +115,10 @@ bool Job::setBlob(const char *blob) m_nicehash = true; } + if (m_autoVariant) { + m_algorithm.setVariant(variant()); + } + # ifdef XMRIG_PROXY_PROJECT memset(m_rawBlob, 0, sizeof(m_rawBlob)); memcpy(m_rawBlob, blob, m_size * 2); @@ -164,22 +170,6 @@ bool Job::setTarget(const char *target) } -xmrig::Variant Job::variant() const -{ - if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { - if (m_algorithm.algo() == xmrig::CRYPTONIGHT_HEAVY) { - return xmrig::VARIANT_0; - } else if (m_algorithm.algo() == xmrig::CRYPTONIGHT_LITE) { - return xmrig::VARIANT_1; - } - - return (m_blob[0] >= 8) ? xmrig::VARIANT_2 : xmrig::VARIANT_1; - } - - return m_algorithm.variant(); -} - - bool Job::fromHex(const char* in, unsigned int len, unsigned char* out) { bool error = false; @@ -224,3 +214,25 @@ bool Job::operator!=(const Job &other) const { return m_id != other.m_id || memcmp(m_blob, other.m_blob, sizeof(m_blob)) != 0; } + + +xmrig::Variant Job::variant() const +{ + using namespace xmrig; + + switch (m_algorithm.algo()) { + case CRYPTONIGHT: + return (m_blob[0] >= 8) ? VARIANT_2 : VARIANT_1; + + case CRYPTONIGHT_LITE: + return VARIANT_1; + + case CRYPTONIGHT_HEAVY: + return VARIANT_0; + + default: + break; + } + + return m_algorithm.variant(); +} diff --git a/src/common/net/Job.h b/src/common/net/Job.h index 7ea539a2..8bd1b8ad 100644 --- a/src/common/net/Job.h +++ b/src/common/net/Job.h @@ -39,12 +39,11 @@ class Job { public: Job(); - Job(int poolId, bool nicehash, xmrig::Algorithm algorithm, const xmrig::Id &clientId); + Job(int poolId, bool nicehash, const xmrig::Algorithm &algorithm, const xmrig::Id &clientId); ~Job(); bool setBlob(const char *blob); bool setTarget(const char *target); - xmrig::Variant variant() const; inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } @@ -61,10 +60,12 @@ public: inline uint32_t diff() const { return static_cast(m_diff); } inline uint64_t target() const { return m_target; } inline void reset() { m_size = 0; m_diff = 0; } + inline void setAlgorithm(const char *algo) { m_algorithm.parseAlgorithm(algo); } inline void setClientId(const xmrig::Id &id) { m_clientId = id; } inline void setPoolId(int poolId) { m_poolId = poolId; } inline void setThreadId(int threadId) { m_threadId = threadId; } - inline xmrig::Algorithm &algorithm() { return m_algorithm; } + inline void setVariant(const char *variant) { m_algorithm.parseVariant(variant); } + inline void setVariant(int variant) { m_algorithm.parseVariant(variant); } # ifdef XMRIG_PROXY_PROJECT inline char *rawBlob() { return m_rawBlob; } @@ -84,6 +85,9 @@ public: bool operator!=(const Job &other) const; private: + xmrig::Variant variant() const; + + bool m_autoVariant; bool m_nicehash; int m_poolId; int m_threadId; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index cc979635..7293a0ac 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -166,12 +166,9 @@ bool Network::isColors() const void Network::setJob(Client *client, const Job &job, bool donate) { - xmrig::Algorithm algorithm = job.algorithm(); - algorithm.setVariant(job.variant()); - LOG_INFO(isColors() ? MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s") : "new job from %s:%d diff %d algo %s", - client->host(), client->port(), job.diff(), algorithm.shortName()); + client->host(), client->port(), job.diff(), job.algorithm().shortName()); m_state.diff = job.diff(); Workers::setJob(job, donate); diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 1916b205..a6dbc73a 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -104,7 +104,7 @@ void MultiWorker::start() storeStats(); } - m_thread->fn(m_state.job.variant())(m_state.blob, m_state.job.size(), m_hash, m_ctx); + m_thread->fn(m_state.job.algorithm().variant())(m_state.blob, m_state.job.size(), m_hash, m_ctx); for (size_t i = 0; i < N; ++i) { if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) { From 23914e9a9fe5fd1daf1954a0071924e907111256 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 10 Sep 2018 18:35:12 +0300 Subject: [PATCH 331/389] "xmrig::cn_uses_variant1" replaced to "xmrig::cn_base_variant". --- src/crypto/CryptoNight_arm.h | 4 ++-- src/crypto/CryptoNight_constants.h | 20 ++++++++++---------- src/crypto/CryptoNight_x86.h | 10 +++++----- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 3ede8111..4cd5eba5 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -432,7 +432,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_V1 = xmrig::cn_uses_variant1(); + constexpr bool IS_V1 = xmrig::cn_base_variant() == xmrig::VARIANT_1; if (IS_V1 && size < 43) { memset(output, 0, 32); @@ -544,7 +544,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_V1 = xmrig::cn_uses_variant1(); + constexpr bool IS_V1 = xmrig::cn_base_variant() == xmrig::VARIANT_1; if (IS_V1 && size < 43) { memset(output, 0, 64); diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/CryptoNight_constants.h index 97a77bbd..f13891a7 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/CryptoNight_constants.h @@ -151,16 +151,16 @@ inline uint32_t cn_select_iter(Algo algorithm, Variant variant) } -template inline constexpr bool cn_uses_variant1() { return false; } -template<> inline constexpr bool cn_uses_variant1() { return false; } -template<> inline constexpr bool cn_uses_variant1() { return true; } -template<> inline constexpr bool cn_uses_variant1() { return true; } -template<> inline constexpr bool cn_uses_variant1() { return true; } -template<> inline constexpr bool cn_uses_variant1() { return true; } -template<> inline constexpr bool cn_uses_variant1() { return false; } -template<> inline constexpr bool cn_uses_variant1() { return false; } -template<> inline constexpr bool cn_uses_variant1() { return true; } -template<> inline constexpr bool cn_uses_variant1() { return false; } +template inline constexpr Variant cn_base_variant() { return VARIANT_0; } +template<> inline constexpr Variant cn_base_variant() { return VARIANT_0; } +template<> inline constexpr Variant cn_base_variant() { return VARIANT_1; } +template<> inline constexpr Variant cn_base_variant() { return VARIANT_1; } +template<> inline constexpr Variant cn_base_variant() { return VARIANT_1; } +template<> inline constexpr Variant cn_base_variant() { return VARIANT_1; } +template<> inline constexpr Variant cn_base_variant() { return VARIANT_0; } +template<> inline constexpr Variant cn_base_variant() { return VARIANT_0; } +template<> inline constexpr Variant cn_base_variant() { return VARIANT_1; } +template<> inline constexpr Variant cn_base_variant() { return VARIANT_2; } } /* namespace xmrig */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index bbed8c38..be879228 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -456,7 +456,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_V1 = xmrig::cn_uses_variant1(); + constexpr bool IS_V1 = xmrig::cn_base_variant() == xmrig::VARIANT_1; if (IS_V1 && size < 43) { memset(output, 0, 32); @@ -567,7 +567,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_V1 = xmrig::cn_uses_variant1(); + constexpr bool IS_V1 = xmrig::cn_base_variant() == xmrig::VARIANT_1; if (IS_V1 && size < 43) { memset(output, 0, 64); @@ -833,7 +833,7 @@ inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t si constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_V1 = xmrig::cn_uses_variant1(); + constexpr bool IS_V1 = xmrig::cn_base_variant() == xmrig::VARIANT_1; if (IS_V1 && size < 43) { memset(output, 0, 32 * 3); @@ -897,7 +897,7 @@ inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_V1 = xmrig::cn_uses_variant1(); + constexpr bool IS_V1 = xmrig::cn_base_variant() == xmrig::VARIANT_1;; if (IS_V1 && size < 43) { memset(output, 0, 32 * 4); @@ -970,7 +970,7 @@ inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t siz constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); constexpr size_t MEM = xmrig::cn_select_memory(); - constexpr bool IS_V1 = xmrig::cn_uses_variant1(); + constexpr bool IS_V1 = xmrig::cn_base_variant() == xmrig::VARIANT_1; if (IS_V1 && size < 43) { memset(output, 0, 32 * 5); From 0bc901612450372f38374a25cc853b9639a826a9 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 13 Sep 2018 11:20:10 +0300 Subject: [PATCH 332/389] v2.8.0-dev --- src/version.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/version.h b/src/version.h index 91a144e7..2a1720ea 100644 --- a/src/version.h +++ b/src/version.h @@ -21,21 +21,21 @@ * along with this program. If not, see . */ -#ifndef __VERSION_H__ -#define __VERSION_H__ +#ifndef XMRIG_VERSION_H +#define XMRIG_VERSION_H #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.6.5-dev" +#define APP_VERSION "2.8.0-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" #define APP_KIND "cpu" #define APP_VER_MAJOR 2 -#define APP_VER_MINOR 6 -#define APP_VER_PATCH 5 +#define APP_VER_MINOR 8 +#define APP_VER_PATCH 0 #ifdef _MSC_VER # if (_MSC_VER >= 1910) @@ -53,4 +53,4 @@ # endif #endif -#endif /* __VERSION_H__ */ +#endif /* XMRIG_VERSION_H */ From 357fbac62b6471da54d9d5da2973ec8c3477aa0c Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 14 Sep 2018 21:42:42 +0300 Subject: [PATCH 333/389] Suppress rapidjson warnings on gcc 8 --- cmake/flags.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/flags.cmake b/cmake/flags.cmake index 498f2165..8bc14804 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -15,7 +15,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-strict-aliasing") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-exceptions -fno-rtti") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-exceptions -fno-rtti -Wno-class-memaccess") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s") if (XMRIG_ARMv8) From 93216a51f41b71a407946ec0053336e4ba7b9066 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 14 Sep 2018 22:04:52 +0300 Subject: [PATCH 334/389] Fix compile error on macOS. --- src/crypto/CryptoNight_monero.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index 0ceb93fe..80750c6c 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -68,7 +68,7 @@ #ifdef _MSC_VER # define VARIANT2_SET_ROUNDING_MODE() if (VARIANT == xmrig::VARIANT_2) { _control87(RC_DOWN, MCW_RC); } #else -# define VARIANT2_SET_ROUNDING_MODE() if (VARIANT == xmrig::VARIANT_2) { std::fesetround(FE_DOWNWARD); } +# define VARIANT2_SET_ROUNDING_MODE() if (VARIANT == xmrig::VARIANT_2) { fesetround(FE_DOWNWARD); } #endif # define VARIANT2_INTEGER_MATH(part, cl, cx) \ From 812cd9760f73e193c2e0e5024d85d88d51325be3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 15 Sep 2018 16:48:57 +0300 Subject: [PATCH 335/389] Added debug thread log. --- src/Summary.cpp | 2 ++ src/common/interfaces/IConfig.h | 18 +++++++++++------- src/interfaces/IThread.h | 10 +++++++--- src/workers/CpuThread.cpp | 12 ++++++++++++ src/workers/CpuThread.h | 10 +++++++--- src/workers/Workers.cpp | 8 ++++++++ 6 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/Summary.cpp b/src/Summary.cpp index fe538fda..76842d5b 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -134,9 +134,11 @@ static void print_pools(xmrig::Config *config) } # ifdef APP_DEBUG + LOG_NOTICE("POOLS --------------------------------------------------------------------"); for (const Pool &pool : pools) { pool.print(); } + LOG_NOTICE("--------------------------------------------------------------------------"); # endif } diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index fb622e37..95a4babf 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -80,13 +80,17 @@ public: HardwareAESKey = 1011, // xmrig amd - OclPlatform = 1400, - OclAffinity = 1401, - OclDevices = 1402, - OclLaunch = 1403, - OclCache = 1404, - OclPrint = 1405, - OclLoader = 1406, + OclPlatformKey = 1400, + OclAffinityKey = 1401, + OclDevicesKey = 1402, + OclLaunchKey = 1403, + OclCacheKey = 1404, + OclPrintKey = 1405, + OclLoaderKey = 1406, + OclSridedIndexKey = 1407, + OclMemChunkKey = 1408, + OclUnrollKey = 1409, + OclCompModeKey = 1410, // xmrig-proxy AccessLogFileKey = 'A', diff --git a/src/interfaces/IThread.h b/src/interfaces/IThread.h index 2e9e3c39..3a8708e6 100644 --- a/src/interfaces/IThread.h +++ b/src/interfaces/IThread.h @@ -20,8 +20,8 @@ * along with this program. If not, see . */ -#ifndef __ITHREAD_H__ -#define __ITHREAD_H__ +#ifndef XMRIG_ITHREAD_H +#define XMRIG_ITHREAD_H #include @@ -64,10 +64,14 @@ public: # ifndef XMRIG_NO_API virtual rapidjson::Value toAPI(rapidjson::Document &doc) const = 0; # endif + +# ifdef APP_DEBUG + virtual void print() const = 0; +# endif }; } /* namespace xmrig */ -#endif // __ITHREAD_H__ +#endif // XMRIG_ITHREAD_H diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index bdf09af4..ca7681f0 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -24,6 +24,7 @@ #include +#include "common/log/Log.h" #include "common/net/Pool.h" #include "rapidjson/document.h" #include "workers/CpuThread.h" @@ -361,6 +362,17 @@ xmrig::IThread::Multiway xmrig::CpuThread::multiway(AlgoVariant av) } +#ifdef APP_DEBUG +void xmrig::CpuThread::print() const +{ + LOG_DEBUG(GREEN_BOLD("CPU thread: ") " index " WHITE_BOLD("%zu") ", multiway " WHITE_BOLD("%d") ", av " WHITE_BOLD("%d") ",", + index(), static_cast(multiway()), static_cast(m_av)); + + LOG_DEBUG(" affine_to_cpu: %" PRId64, affinity()); +} +#endif + + #ifndef XMRIG_NO_API rapidjson::Value xmrig::CpuThread::toAPI(rapidjson::Document &doc) const { diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index 0e364764..622dc3a2 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __CPUTHREAD_H__ -#define __CPUTHREAD_H__ +#ifndef XMRIG_CPUTHREAD_H +#define XMRIG_CPUTHREAD_H #include "common/xmrig.h" @@ -80,6 +80,10 @@ public: inline Type type() const override { return CPU; } protected: +# ifdef APP_DEBUG + void print() const override; +# endif + # ifndef XMRIG_NO_API rapidjson::Value toAPI(rapidjson::Document &doc) const override; # endif @@ -101,4 +105,4 @@ private: } /* namespace xmrig */ -#endif /* __CPUTHREAD_H__ */ +#endif /* XMRIG_CPUTHREAD_H */ diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 0e75e736..5deb14f7 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -160,6 +160,14 @@ void Workers::setJob(const Job &job, bool donate) void Workers::start(xmrig::Controller *controller) { +# ifdef APP_DEBUG + LOG_NOTICE("THREADS ------------------------------------------------------------------"); + for (const xmrig::IThread *thread : controller->config()->threads()) { + thread->print(); + } + LOG_NOTICE("--------------------------------------------------------------------------"); +# endif + m_controller = controller; const std::vector &threads = controller->config()->threads(); From 14ac7b556e3cb2c530acb44858863a416cea04da Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 16 Sep 2018 03:06:54 +0300 Subject: [PATCH 336/389] Initial TLS support. --- CMakeLists.txt | 8 +- cmake/OpenSSL.cmake | 18 +++ src/common/Platform.cpp | 26 +++++ src/common/Platform.h | 8 +- src/common/Platform_mac.cpp | 13 +-- src/common/Platform_unix.cpp | 14 +-- src/common/Platform_win.cpp | 13 +-- src/common/config/CommonConfig.cpp | 9 ++ src/common/interfaces/IConfig.h | 20 ++-- src/common/net/Client.cpp | 174 ++++++++++++++++++++++++----- src/common/net/Client.h | 29 ++++- src/common/net/Pool.cpp | 16 ++- src/common/net/Pool.h | 51 +++++---- src/common/net/Tls.cpp | 136 ++++++++++++++++++++++ src/common/net/Tls.h | 58 ++++++++++ src/core/ConfigLoader_platform.h | 22 ++-- 16 files changed, 494 insertions(+), 121 deletions(-) create mode 100644 cmake/OpenSSL.cmake create mode 100644 src/common/net/Tls.cpp create mode 100644 src/common/net/Tls.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 62ec419e..8d38b1a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ option(WITH_AEON "CryptoNight-Lite support" ON) option(WITH_SUMO "CryptoNight-Heavy support" ON) option(WITH_HTTPD "HTTP REST API" ON) option(WITH_DEBUG_LOG "Enable debug log output" OFF) +option(WITH_TLS "Enable OpenSSL support" OFF) option(BUILD_STATIC "Build static binary" OFF) include (CheckIncludeFile) @@ -194,6 +195,8 @@ else() endif() endif() +include(cmake/OpenSSL.cmake) + CHECK_INCLUDE_FILE (syslog.h HAVE_SYSLOG_H) if (HAVE_SYSLOG_H) add_definitions(/DHAVE_SYSLOG_H) @@ -233,6 +236,7 @@ if (WITH_HTTPD) message(FATAL_ERROR "microhttpd NOT found: use `-DWITH_HTTPD=OFF` to build without http deamon support") endif() else() + set(HTTPD_SOURCES "") set(MHD_LIBRARY "") add_definitions(/DXMRIG_NO_HTTPD) add_definitions(/DXMRIG_NO_API) @@ -250,5 +254,5 @@ if (WITH_DEBUG_LOG) add_definitions(/DAPP_DEBUG) endif() -add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES}) -target_link_libraries(${PROJECT_NAME} ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) +add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES} ${TLS_SOURCES}) +target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) diff --git a/cmake/OpenSSL.cmake b/cmake/OpenSSL.cmake new file mode 100644 index 00000000..154a8278 --- /dev/null +++ b/cmake/OpenSSL.cmake @@ -0,0 +1,18 @@ +if (WITH_TLS) + set(OPENSSL_ROOT_DIR ${XMRIG_DEPS}) + set(OPENSSL_USE_STATIC_LIBS TRUE) + set(OPENSSL_MSVC_STATIC_RT TRUE) + + find_package(OpenSSL) + + if (OPENSSL_FOUND) + set(TLS_SOURCES src/common/net/Tls.h src/common/net/Tls.cpp) + include_directories(${OPENSSL_INCLUDE_DIR}) + else() + message(FATAL_ERROR "OpenSSL NOT found: use `-DWITH_TLS=OFF` to build without TLS support") + endif() +else() + set(TLS_SOURCES "") + set(OPENSSL_LIBRARIES "") + add_definitions(/DXMRIG_NO_TLS) +endif() diff --git a/src/common/Platform.cpp b/src/common/Platform.cpp index 52b55987..a95f78e7 100644 --- a/src/common/Platform.cpp +++ b/src/common/Platform.cpp @@ -26,6 +26,12 @@ #include +#ifndef XMRIG_NO_TLS +# include +# include +#endif + + #include "Platform.h" @@ -61,3 +67,23 @@ const char *Platform::defaultConfigName() *m_defaultConfigName = '\0'; return nullptr; } + + +void Platform::init(const char *userAgent) +{ +# ifndef XMRIG_NO_TLS + SSL_library_init(); + SSL_load_error_strings(); + ERR_load_BIO_strings(); + ERR_load_crypto_strings(); + SSL_load_error_strings(); + OpenSSL_add_all_digests(); +# endif + + if (userAgent) { + m_userAgent = userAgent; + } + else { + m_userAgent = createUserAgent(); + } +} diff --git a/src/common/Platform.h b/src/common/Platform.h index 8704604a..5dfb9ff7 100644 --- a/src/common/Platform.h +++ b/src/common/Platform.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __PLATFORM_H__ -#define __PLATFORM_H__ +#ifndef XMRIG_PLATFORM_H +#define XMRIG_PLATFORM_H #include @@ -43,9 +43,11 @@ public: static inline const char *userAgent() { return m_userAgent.data(); } private: + static char *createUserAgent(); + static char m_defaultConfigName[520]; static xmrig::c_str m_userAgent; }; -#endif /* __PLATFORM_H__ */ +#endif /* XMRIG_PLATFORM_H */ diff --git a/src/common/Platform_mac.cpp b/src/common/Platform_mac.cpp index b8181cc4..d0c533b0 100644 --- a/src/common/Platform_mac.cpp +++ b/src/common/Platform_mac.cpp @@ -38,7 +38,7 @@ #endif -static inline char *createUserAgent() +char *Platform::createUserAgent() { const size_t max = 160; @@ -65,17 +65,6 @@ bool Platform::setThreadAffinity(uint64_t cpu_id) } -void Platform::init(const char *userAgent) -{ - if (userAgent) { - m_userAgent = userAgent; - } - else { - m_userAgent = createUserAgent(); - } -} - - void Platform::setProcessPriority(int priority) { diff --git a/src/common/Platform_unix.cpp b/src/common/Platform_unix.cpp index 97b32ee8..058920ec 100644 --- a/src/common/Platform_unix.cpp +++ b/src/common/Platform_unix.cpp @@ -52,7 +52,7 @@ typedef cpuset_t cpu_set_t; #endif -static inline char *createUserAgent() +char *Platform::createUserAgent() { const size_t max = 160; @@ -92,23 +92,11 @@ bool Platform::setThreadAffinity(uint64_t cpu_id) } -void Platform::init(const char *userAgent) -{ - if (userAgent) { - m_userAgent = userAgent; - } - else { - m_userAgent = createUserAgent(); - } -} - - void Platform::setProcessPriority(int priority) { } - void Platform::setThreadPriority(int priority) { if (priority == -1) { diff --git a/src/common/Platform_win.cpp b/src/common/Platform_win.cpp index 47f41867..32b850d1 100644 --- a/src/common/Platform_win.cpp +++ b/src/common/Platform_win.cpp @@ -55,7 +55,7 @@ static inline OSVERSIONINFOEX winOsVersion() } -static inline char *createUserAgent() +char *Platform::createUserAgent() { const auto osver = winOsVersion(); const size_t max = 160; @@ -94,17 +94,6 @@ bool Platform::setThreadAffinity(uint64_t cpu_id) } -void Platform::init(const char *userAgent) -{ - if (userAgent) { - m_userAgent = userAgent; - } - else { - m_userAgent = createUserAgent(); - } -} - - void Platform::setProcessPriority(int priority) { if (priority == -1) { diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index ca901757..f22a632c 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -157,6 +157,10 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable) m_pools.back().setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); break; + case TlsKey: /* --tls */ + m_pools.back().setTLS(enable); + break; + # ifndef XMRIG_PROXY_PROJECT case NicehashKey: /* --nicehash */ m_pools.back().setNicehash(enable); @@ -235,6 +239,10 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) m_pools.back().setRigId(arg); break; + case FingerprintKey: /* --tls-fingerprint */ + m_pools.back().setFingerprint(arg); + break; + case VariantKey: /* --variant */ m_pools.back().algorithm().parseVariant(arg); break; @@ -269,6 +277,7 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) case SyslogKey: /* --syslog */ case KeepAliveKey: /* --keepalive */ case NicehashKey: /* --nicehash */ + case TlsKey: /* --tls */ case ApiIPv6Key: /* --api-ipv6 */ case DryRunKey: /* --dry-run */ return parseBoolean(key, true); diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index 95a4babf..ece8e3b4 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -63,6 +63,8 @@ public: VerboseKey = 1100, VersionKey = 'V', WatchKey = 1105, + TlsKey = 1013, + FingerprintKey = 1014, // xmrig common CPUPriorityKey = 1021, @@ -80,17 +82,13 @@ public: HardwareAESKey = 1011, // xmrig amd - OclPlatformKey = 1400, - OclAffinityKey = 1401, - OclDevicesKey = 1402, - OclLaunchKey = 1403, - OclCacheKey = 1404, - OclPrintKey = 1405, - OclLoaderKey = 1406, - OclSridedIndexKey = 1407, - OclMemChunkKey = 1408, - OclUnrollKey = 1409, - OclCompModeKey = 1410, + OclPlatform = 1400, + OclAffinity = 1401, + OclDevices = 1402, + OclLaunch = 1403, + OclCache = 1404, + OclPrint = 1405, + OclLoader = 1406, // xmrig-proxy AccessLogFileKey = 'A', diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index 2a9db444..1057c474 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -29,6 +29,13 @@ #include +#ifndef XMRIG_NO_TLS +# include +# include +# include "common/net/Tls.h" +#endif + + #include "common/interfaces/IClientListener.h" #include "common/log/Log.h" #include "common/net/Client.h" @@ -48,6 +55,17 @@ int64_t Client::m_sequence = 1; xmrig::Storage Client::m_storage; +#ifdef APP_DEBUG +static const char *states[] = { + "unconnected", + "host-lookup", + "connecting", + "connected", + "closing" +}; +#endif + + Client::Client(int id, const char *agent, IClientListener *listener) : m_ipv6(false), m_nicehash(false), @@ -61,6 +79,7 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_failures(0), m_recvBufPos(0), m_state(UnconnectedState), + m_tls(nullptr), m_expire(0), m_jobs(0), m_keepAlive(0), @@ -92,6 +111,12 @@ Client::~Client() void Client::connect() { +# ifndef XMRIG_NO_TLS + if (m_pool.isTLS()) { + m_tls = new Tls(this); + } +# endif + resolve(m_pool.host()); } @@ -122,6 +147,7 @@ void Client::deleteLater() } + void Client::setPool(const Pool &pool) { if (!pool.isValid()) { @@ -160,6 +186,12 @@ bool Client::disconnect() } +bool Client::isTLS() const +{ + return m_pool.isTLS() && m_tls; +} + + int64_t Client::submit(const JobResult &result) { using namespace rapidjson; @@ -330,6 +362,39 @@ bool Client::parseLogin(const rapidjson::Value &result, int *code) } +bool Client::send(BIO *bio) +{ +# ifndef XMRIG_NO_TLS + uv_buf_t buf; + buf.len = BIO_get_mem_data(bio, &buf.base); + + if (buf.len == 0) { + return true; + } + + LOG_DEBUG("[%s] TLS send (%d bytes)", m_pool.url(), static_cast(buf.len)); + + bool result = false; + if (state() == ConnectedState && uv_is_writable(m_stream)) { + result = uv_try_write(m_stream, &buf, 1) > 0; + + if (!result) { + close(); + } + } + else { + LOG_DEBUG_ERR("[%s] send failed, invalid state: %d", m_pool.url(), m_state); + } + + BIO_reset(bio); + + return result; +# else + return false; +# endif +} + + bool Client::verifyAlgorithm(const xmrig::Algorithm &algorithm) const { # ifdef XMRIG_PROXY_PROJECT @@ -404,16 +469,27 @@ int64_t Client::send(const rapidjson::Document &doc) int64_t Client::send(size_t size) { LOG_DEBUG("[%s] send (%d bytes): \"%s\"", m_pool.url(), size, m_sendBuf); - if (state() != ConnectedState || !uv_is_writable(m_stream)) { - LOG_DEBUG_ERR("[%s] send failed, invalid state: %d", m_pool.url(), m_state); - return -1; + +# ifndef XMRIG_NO_TLS + if (isTLS()) { + if (!m_tls->send(m_sendBuf, size)) { + return -1; + } } + else +# endif + { + if (state() != ConnectedState || !uv_is_writable(m_stream)) { + LOG_DEBUG_ERR("[%s] send failed, invalid state: %d", m_pool.url(), m_state); + return -1; + } - uv_buf_t buf = uv_buf_init(m_sendBuf, (unsigned int) size); + uv_buf_t buf = uv_buf_init(m_sendBuf, (unsigned int) size); - if (uv_try_write(m_stream, &buf, 1) < 0) { - close(); - return -1; + if (uv_try_write(m_stream, &buf, 1) < 0) { + close(); + return -1; + } } m_expire = uv_now(uv_default_loop()) + kResponseTimeout; @@ -463,6 +539,22 @@ void Client::connect(sockaddr *addr) } +void Client::handshake() +{ +# ifndef XMRIG_NO_TLS + if (isTLS()) { + m_expire = uv_now(uv_default_loop()) + kResponseTimeout; + + m_tls->handshake(); + } + else +# endif + { + login(); + } +} + + void Client::login() { using namespace rapidjson; @@ -511,6 +603,13 @@ void Client::onClose() m_socket = nullptr; setState(UnconnectedState); +# ifndef XMRIG_NO_TLS + if (m_tls) { + delete m_tls; + m_tls = nullptr; + } +# endif + reconnect(); } @@ -665,6 +764,35 @@ void Client::ping() } +void Client::read() +{ + char* end; + char* start = m_recvBuf.base; + size_t remaining = m_recvBufPos; + + while ((end = static_cast(memchr(start, '\n', remaining))) != nullptr) { + end++; + size_t len = end - start; + parse(start, len); + + remaining -= len; + start = end; + } + + if (remaining == 0) { + m_recvBufPos = 0; + return; + } + + if (start == m_recvBuf.base) { + return; + } + + memcpy(m_recvBuf.base, start, remaining); + m_recvBufPos = remaining; +} + + void Client::reconnect() { if (!m_listener) { @@ -689,7 +817,7 @@ void Client::reconnect() void Client::setState(SocketState state) { - LOG_DEBUG("[%s] state: %d", m_pool.url(), state); + LOG_DEBUG("[%s] state: \"%s\"", m_pool.url(), states[state]); if (m_state == state) { return; @@ -757,7 +885,7 @@ void Client::onConnect(uv_connect_t *req, int status) uv_read_start(client->m_stream, Client::onAllocBuffer, Client::onRead); delete req; - client->login(); + client->handshake(); } @@ -789,30 +917,18 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) client->m_recvBufPos += nread; - char* end; - char* start = client->m_recvBuf.base; - size_t remaining = client->m_recvBufPos; +# ifndef XMRIG_NO_TLS + if (client->isTLS()) { + LOG_DEBUG("[%s] TLS received (%d bytes)", client->m_pool.url(), static_cast(nread)); - while ((end = static_cast(memchr(start, '\n', remaining))) != nullptr) { - end++; - size_t len = end - start; - client->parse(start, len); - - remaining -= len; - start = end; - } - - if (remaining == 0) { + client->m_tls->read(client->m_recvBuf.base, client->m_recvBufPos); client->m_recvBufPos = 0; - return; } - - if (start == client->m_recvBuf.base) { - return; + else +# endif + { + client->read(); } - - memcpy(client->m_recvBuf.base, start, remaining); - client->m_recvBufPos = remaining; } diff --git a/src/common/net/Client.h b/src/common/net/Client.h index 4be8badb..893fabfc 100644 --- a/src/common/net/Client.h +++ b/src/common/net/Client.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __CLIENT_H__ -#define __CLIENT_H__ +#ifndef XMRIG_CLIENT_H +#define XMRIG_CLIENT_H #include @@ -43,6 +43,9 @@ class IClientListener; class JobResult; +typedef struct bio_st BIO; + + class Client { public: @@ -54,12 +57,19 @@ public: ClosingState }; - constexpr static int kResponseTimeout = 20 * 1000; + constexpr static int kResponseTimeout = 20 * 1000; + +# ifndef XMRIG_NO_TLS + constexpr static int kInputBufferSize = 1024 * 16; +# else + constexpr static int kInputBufferSize = 1024 * 2; +# endif Client(int id, const char *agent, IClientListener *listener); ~Client(); bool disconnect(); + bool isTLS() const; int64_t submit(const JobResult &result); void connect(); void connect(const Pool &pool); @@ -80,6 +90,9 @@ public: inline void setRetryPause(int ms) { m_retryPause = ms; } private: + class Tls; + + enum Extensions { NicehashExt = 1, AlgoExt = 2 @@ -89,12 +102,14 @@ private: bool isCriticalError(const char *message); bool parseJob(const rapidjson::Value ¶ms, int *code); bool parseLogin(const rapidjson::Value &result, int *code); + bool send(BIO *bio); bool verifyAlgorithm(const xmrig::Algorithm &algorithm) const; int resolve(const char *host); int64_t send(const rapidjson::Document &doc); int64_t send(size_t size); void connect(const std::vector &ipv4, const std::vector &ipv6); void connect(sockaddr *addr); + void handshake(); void login(); void onClose(); void parse(char *line, size_t len); @@ -102,6 +117,7 @@ private: void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error); void parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error); void ping(); + void read(); void reconnect(); void setState(SocketState state); void startTimeout(); @@ -120,9 +136,9 @@ private: bool m_ipv6; bool m_nicehash; bool m_quiet; - char m_buf[2048]; + char m_buf[kInputBufferSize]; char m_ip[46]; - char m_sendBuf[768]; + char m_sendBuf[1024]; const char *m_agent; IClientListener *m_listener; int m_extensions; @@ -135,6 +151,7 @@ private: size_t m_recvBufPos; SocketState m_state; std::map m_results; + Tls *m_tls; uint64_t m_expire; uint64_t m_jobs; uint64_t m_keepAlive; @@ -150,4 +167,4 @@ private: }; -#endif /* __CLIENT_H__ */ +#endif /* XMRIG_CLIENT_H */ diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 49f4e54c..02bf5fa5 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -46,6 +46,7 @@ Pool::Pool() : m_nicehash(false), + m_tls(false), m_keepAlive(0), m_port(kDefaultPort) { @@ -65,6 +66,7 @@ Pool::Pool() : */ Pool::Pool(const char *url) : m_nicehash(false), + m_tls(false), m_keepAlive(0), m_port(kDefaultPort) { @@ -72,8 +74,9 @@ Pool::Pool(const char *url) : } -Pool::Pool(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash) : +Pool::Pool(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, bool tls) : m_nicehash(nicehash), + m_tls(tls), m_keepAlive(keepAlive), m_port(port), m_host(host), @@ -134,7 +137,13 @@ bool Pool::parse(const char *url) const char *base = url; if (p) { - if (strncasecmp(url, "stratum+tcp://", 14)) { + if (strncasecmp(url, "stratum+tcp://", 14) == 0) { + m_tls = false; + } + else if (strncasecmp(url, "stratum+ssl://", 14) == 0) { + m_tls = true; + } + else { return false; } @@ -221,6 +230,9 @@ rapidjson::Value Pool::toJSON(rapidjson::Document &doc) const break; } + obj.AddMember("tls", isTLS(), allocator); + obj.AddMember("tls-fingerprint", fingerprint() ? Value(StringRef(fingerprint())).Move() : Value(kNullType).Move(), allocator); + return obj; } diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index 0641b851..123cc131 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -45,30 +45,35 @@ public: Pool(); Pool(const char *url); Pool(const char *host, - uint16_t port, - const char *user = nullptr, - const char *password = nullptr, - int keepAlive = 0, - bool nicehash = false + uint16_t port, + const char *user = nullptr, + const char *password = nullptr, + int keepAlive = 0, + bool nicehash = false, + bool tls = false ); - inline bool isNicehash() const { return m_nicehash; } - inline bool isValid() const { return !m_host.isNull() && m_port > 0; } - inline const char *host() const { return m_host.data(); } - inline const char *password() const { return !m_password.isNull() ? m_password.data() : kDefaultPassword; } - inline const char *rigId() const { return m_rigId.data(); } - inline const char *url() const { return m_url.data(); } - inline const char *user() const { return !m_user.isNull() ? m_user.data() : kDefaultUser; } - inline const xmrig::Algorithm &algorithm() const { return m_algorithm; } - inline const xmrig::Algorithms &algorithms() const { return m_algorithms; } - inline int keepAlive() const { return m_keepAlive; } - inline uint16_t port() const { return m_port; } - inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } - inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } - inline void setPassword(const char *password) { m_password = password; } - inline void setRigId(const char *rigId) { m_rigId = rigId; } - inline void setUser(const char *user) { m_user = user; } - inline xmrig::Algorithm &algorithm() { return m_algorithm; } + inline bool isNicehash() const { return m_nicehash; } + inline bool isTLS() const { return m_tls; } + inline bool isValid() const { return !m_host.isNull() && m_port > 0; } + inline const char *fingerprint() const { return m_fingerprint.data(); } + inline const char *host() const { return m_host.data(); } + inline const char *password() const { return !m_password.isNull() ? m_password.data() : kDefaultPassword; } + inline const char *rigId() const { return m_rigId.data(); } + inline const char *url() const { return m_url.data(); } + inline const char *user() const { return !m_user.isNull() ? m_user.data() : kDefaultUser; } + inline const xmrig::Algorithm &algorithm() const { return m_algorithm; } + inline const xmrig::Algorithms &algorithms() const { return m_algorithms; } + inline int keepAlive() const { return m_keepAlive; } + inline uint16_t port() const { return m_port; } + inline void setFingerprint(const char *fingerprint) { m_fingerprint = fingerprint; } + inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } + inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } + inline void setPassword(const char *password) { m_password = password; } + inline void setRigId(const char *rigId) { m_rigId = rigId; } + inline void setTLS(bool tls) { m_tls = tls; } + inline void setUser(const char *user) { m_user = user; } + inline xmrig::Algorithm &algorithm() { return m_algorithm; } inline bool operator!=(const Pool &other) const { return !isEqual(other); } inline bool operator==(const Pool &other) const { return isEqual(other); } @@ -92,10 +97,12 @@ private: void rebuild(); bool m_nicehash; + bool m_tls; int m_keepAlive; uint16_t m_port; xmrig::Algorithm m_algorithm; xmrig::Algorithms m_algorithms; + xmrig::c_str m_fingerprint; xmrig::c_str m_host; xmrig::c_str m_password; xmrig::c_str m_rigId; diff --git a/src/common/net/Tls.cpp b/src/common/net/Tls.cpp new file mode 100644 index 00000000..679e5d24 --- /dev/null +++ b/src/common/net/Tls.cpp @@ -0,0 +1,136 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include + + +#include "common/net/Client.h" +#include "common/net/Tls.h" +#include "common/log/Log.h" + + +Client::Tls::Tls(Client *client) : + m_buf(), + m_client(client), + m_ssl(nullptr) +{ + m_ctx = SSL_CTX_new(SSLv23_method()); + assert(m_ctx != nullptr); + + if (!m_ctx) { + return; + } + + m_writeBio = BIO_new(BIO_s_mem()); + m_readBio = BIO_new(BIO_s_mem()); + SSL_CTX_set_options(m_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); +} + + +Client::Tls::~Tls() +{ + if (m_ctx) { + SSL_CTX_free(m_ctx); + } + + if (m_ssl) { + SSL_free(m_ssl); + } +} + + +bool Client::Tls::handshake() +{ + m_ssl = SSL_new(m_ctx); + assert(m_ssl != nullptr); + + if (!m_ssl) { + return false; + } + + SSL_set_connect_state(m_ssl); + SSL_set_bio(m_ssl, m_readBio, m_writeBio); + SSL_do_handshake(m_ssl); + + return send(); +} + + +bool Client::Tls::send(const char *data, size_t size) +{ + SSL_write(m_ssl, data, size); + + return send(); +} + + +void Client::Tls::read(const char *data, size_t size) +{ + BIO_write(m_readBio, data, size); + + if (!SSL_is_init_finished(m_ssl)) { + const int rc = SSL_connect(m_ssl); + + if (rc < 0 && SSL_get_error(m_ssl, rc) == SSL_ERROR_WANT_READ) { + send(); + } + + if (rc == 1) { + if (!verify()) { + LOG_ERR("[%s] TLS certificate verification failed", m_client->m_pool.url()); + m_client->close(); + + return; + } + + m_client->login(); + } + + return; + } + + int bytes_read = 0; + while ((bytes_read = SSL_read(m_ssl, m_buf, sizeof(m_buf))) > 0) { + m_client->parse(m_buf, bytes_read); + } +} + + +bool Client::Tls::send() +{ + return m_client->send(m_writeBio); +} + + +bool Client::Tls::verify() +{ + X509* cert = SSL_get_peer_certificate(m_ssl); + if (cert == nullptr) { + return false; + } + + return true; +} diff --git a/src/common/net/Tls.h b/src/common/net/Tls.h new file mode 100644 index 00000000..13474237 --- /dev/null +++ b/src/common/net/Tls.h @@ -0,0 +1,58 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_TLS_H +#define XMRIG_TLS_H + + +#include + + +#include "common/net/Client.h" + + +class Client::Tls +{ +public: + Tls(Client *client); + ~Tls(); + + bool handshake(); + bool send(const char *data, size_t size); + + void read(const char *data, size_t size); + +private: + bool send(); + bool verify(); + + BIO *m_readBio; + BIO *m_writeBio; + char m_buf[1024 * 2]; + Client *m_client; + SSL *m_ssl; + SSL_CTX *m_ctx; +}; + + +#endif /* XMRIG_TLS_H */ diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index 98724271..c034f3e7 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -132,6 +132,8 @@ static struct option const options[] = { { "user-agent", 1, nullptr, xmrig::IConfig::UserAgentKey }, { "userpass", 1, nullptr, xmrig::IConfig::UserpassKey }, { "rig-id", 1, nullptr, xmrig::IConfig::RigIdKey }, + { "tls", 0, nullptr, xmrig::IConfig::TlsKey }, + { "tls-fingerprint", 1, nullptr, xmrig::IConfig::FingerprintKey }, { "version", 0, nullptr, xmrig::IConfig::VersionKey }, { nullptr, 0, nullptr, 0 } }; @@ -162,15 +164,17 @@ static struct option const config_options[] = { static struct option const pool_options[] = { - { "url", 1, nullptr, xmrig::IConfig::UrlKey }, - { "pass", 1, nullptr, xmrig::IConfig::PasswordKey }, - { "user", 1, nullptr, xmrig::IConfig::UserKey }, - { "userpass", 1, nullptr, xmrig::IConfig::UserpassKey }, - { "nicehash", 0, nullptr, xmrig::IConfig::NicehashKey }, - { "keepalive", 2, nullptr, xmrig::IConfig::KeepAliveKey }, - { "variant", 1, nullptr, xmrig::IConfig::VariantKey }, - { "rig-id", 1, nullptr, xmrig::IConfig::RigIdKey }, - { nullptr, 0, nullptr, 0 } + { "url", 1, nullptr, xmrig::IConfig::UrlKey }, + { "pass", 1, nullptr, xmrig::IConfig::PasswordKey }, + { "user", 1, nullptr, xmrig::IConfig::UserKey }, + { "userpass", 1, nullptr, xmrig::IConfig::UserpassKey }, + { "nicehash", 0, nullptr, xmrig::IConfig::NicehashKey }, + { "keepalive", 2, nullptr, xmrig::IConfig::KeepAliveKey }, + { "variant", 1, nullptr, xmrig::IConfig::VariantKey }, + { "rig-id", 1, nullptr, xmrig::IConfig::RigIdKey }, + { "tls", 0, nullptr, xmrig::IConfig::TlsKey }, + { "tls-fingerprint", 1, nullptr, xmrig::IConfig::FingerprintKey }, + { nullptr, 0, nullptr, 0 } }; From 2f3939396ec5c2eaddbd6bb5a71b8185af058c2e Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 16 Sep 2018 05:04:20 +0300 Subject: [PATCH 337/389] Move shared summary to xmrig::CommonConfig. --- src/Summary.cpp | 65 +----------------- src/common/config/CommonConfig.cpp | 105 ++++++++++++++++++++++++++++- src/common/config/CommonConfig.h | 9 ++- src/common/config/ConfigLoader.cpp | 14 +++- 4 files changed, 126 insertions(+), 67 deletions(-) diff --git a/src/Summary.cpp b/src/Summary.cpp index 76842d5b..5feb6d7e 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -37,24 +37,6 @@ #include "version.h" -static void print_versions(xmrig::Config *config) -{ - char buf[16] = { 0 }; - -# if defined(__clang__) - snprintf(buf, 16, " clang/%d.%d.%d", __clang_major__, __clang_minor__, __clang_patchlevel__); -# elif defined(__GNUC__) - snprintf(buf, 16, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); -# elif defined(_MSC_VER) - snprintf(buf, 16, " MSVC/%d", MSVC_VERSION); -# endif - - Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%s/%s") WHITE_BOLD(" libuv/%s%s") - : " * %-13s%s/%s libuv/%s%s", - "VERSIONS", APP_NAME, APP_VERSION, uv_version_string(), buf); -} - - static void print_memory(xmrig::Config *config) { # ifdef _WIN32 if (config->isColors()) { @@ -120,44 +102,6 @@ static void print_threads(xmrig::Config *config) } -static void print_pools(xmrig::Config *config) -{ - const std::vector &pools = config->pools(); - - for (size_t i = 0; i < pools.size(); ++i) { - Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") CYAN_BOLD("%s") " variant " WHITE_BOLD("%s") - : " * POOL #%-7d%s variant %s", - i + 1, - pools[i].url(), - pools[i].algorithm().variantName() - ); - } - -# ifdef APP_DEBUG - LOG_NOTICE("POOLS --------------------------------------------------------------------"); - for (const Pool &pool : pools) { - pool.print(); - } - LOG_NOTICE("--------------------------------------------------------------------------"); -# endif -} - - -#ifndef XMRIG_NO_API -static void print_api(xmrig::Config *config) -{ - const int port = config->apiPort(); - if (port == 0) { - return; - } - - Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN("%s:") CYAN_BOLD("%d") - : " * %-13s%s:%d", - "API BIND", config->isApiIPv6() ? "[::]" : "0.0.0.0", port); -} -#endif - - static void print_commands(xmrig::Config *config) { if (config->isColors()) { @@ -173,15 +117,12 @@ static void print_commands(xmrig::Config *config) void Summary::print(xmrig::Controller *controller) { - print_versions(controller->config()); + controller->config()->printVersions(); print_memory(controller->config()); print_cpu(controller->config()); print_threads(controller->config()); - print_pools(controller->config()); - -# ifndef XMRIG_NO_API - print_api(controller->config()); -# endif + controller->config()->printPools(); + controller->config()->printAPI(); print_commands(controller->config()); } diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index f22a632c..a9df89d7 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -29,12 +29,23 @@ #include +#ifndef XMRIG_NO_HTTPD +# include +#endif + + +#ifndef XMRIG_NO_TLS +# include +#endif + + #include "common/config/CommonConfig.h" #include "common/log/Log.h" #include "donate.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" +#include "version.h" xmrig::CommonConfig::CommonConfig() : @@ -69,8 +80,100 @@ xmrig::CommonConfig::CommonConfig() : } -xmrig::CommonConfig::~CommonConfig() +void xmrig::CommonConfig::printAPI() { +# ifndef XMRIG_NO_API + if (apiPort() == 0) { + return; + } + + Log::i()->text(isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN("%s:") CYAN_BOLD("%d") + : " * %-13s%s:%d", + "API BIND", isApiIPv6() ? "[::]" : "0.0.0.0", apiPort()); +# endif +} + + +void xmrig::CommonConfig::printPools() +{ + for (size_t i = 0; i < m_activePools.size(); ++i) { + if (!isColors()) { + Log::i()->text(" * POOL #%-7zu%s variant=%s, TLS=%d", + i + 1, + m_activePools[i].url(), + m_activePools[i].algorithm().variantName(), + static_cast(m_activePools[i].isTLS()) + ); + } + else { + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") "\x1B[1;%dm%s\x1B[0m variant " WHITE_BOLD("%s"), + i + 1, + m_activePools[i].isTLS() ? 32 : 36, + m_activePools[i].url(), + m_activePools[i].algorithm().variantName() + ); + } + } + +# ifdef APP_DEBUG + LOG_NOTICE("POOLS --------------------------------------------------------------------"); + for (const Pool &pool : m_activePools) { + pool.print(); + } + LOG_NOTICE("--------------------------------------------------------------------------"); +# endif +} + + +void xmrig::CommonConfig::printVersions() +{ + char buf[256] = { 0 }; + +# if defined(__clang__) + snprintf(buf, sizeof buf, "clang/%d.%d.%d", __clang_major__, __clang_minor__, __clang_patchlevel__); +# elif defined(__GNUC__) + snprintf(buf, sizeof buf, "gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); +# elif defined(_MSC_VER) + snprintf(buf, sizeof buf, "MSVC/%d", MSVC_VERSION); +# endif + + Log::i()->text(isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%s/%s") WHITE_BOLD(" %s") + : " * %-13s%s/%s %s", + "ABOUT", APP_NAME, APP_VERSION, buf); + +# if defined(XMRIG_AMD_PROJECT) +# if CL_VERSION_2_0 + const char *ocl = "2.0"; +# elif CL_VERSION_1_2 + const char *ocl = "1.2"; +# elif CL_VERSION_1_1 + const char *ocl = "1.1"; +# elif CL_VERSION_1_0 + const char *ocl = "1.0"; +# else + const char *ocl = "0.0"; +# endif + int length = snprintf(buf, sizeof buf, "OpenCL/%s ", ocl); +# else + memset(buf, 0, 16); + int length = 0; +# endif + +# if !defined(XMRIG_NO_TLS) && defined(OPENSSL_VERSION_TEXT) + { + constexpr const char *v = OPENSSL_VERSION_TEXT + 8; + length += snprintf(buf + length, (sizeof buf) - length, "OpenSSL/%.*s ", static_cast(strchr(v, ' ') - v), v); + } +# endif + +# ifndef XMRIG_NO_HTTPD + length += snprintf(buf + length, (sizeof buf) - length, "microhttpd/%s ", MHD_get_version()); +# endif + + + Log::i()->text(isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13slibuv/%s %s") + : " * %-13slibuv/%s %s", + "LIBS", uv_version_string(), buf); } diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index fa27ea6a..7643a1a5 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __COMMONCONFIG_H__ -#define __COMMONCONFIG_H__ +#ifndef XMRIG_COMMONCONFIG_H +#define XMRIG_COMMONCONFIG_H #include @@ -41,7 +41,6 @@ class CommonConfig : public IConfig { public: CommonConfig(); - ~CommonConfig(); inline bool isApiIPv6() const { return m_apiIPv6; } inline bool isApiRestricted() const { return m_apiRestricted; } @@ -68,6 +67,10 @@ public: bool save() override; + void printAPI(); + void printPools(); + void printVersions(); + protected: enum State { NoneState, diff --git a/src/common/config/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp index cc5d9a49..484c2f8f 100644 --- a/src/common/config/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -32,6 +32,11 @@ #endif +#ifndef XMRIG_NO_TLS +# include +#endif + + #include "common/config/ConfigLoader.h" #include "common/config/ConfigWatcher.h" #include "common/interfaces/IConfig.h" @@ -313,6 +318,13 @@ void xmrig::ConfigLoader::showVersion() printf("\nlibuv/%s\n", uv_version_string()); # ifndef XMRIG_NO_HTTPD - printf("libmicrohttpd/%s\n", MHD_get_version()); + printf("microhttpd/%s\n", MHD_get_version()); +# endif + +# if !defined(XMRIG_NO_TLS) && defined(OPENSSL_VERSION_TEXT) + { + constexpr const char *v = OPENSSL_VERSION_TEXT + 8; + printf("OpenSSL/%.*s\n", static_cast(strchr(v, ' ') - v), v); + } # endif } From bc9130ded373475aa3d09e22e4007824d6f185f4 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 16 Sep 2018 06:35:49 +0300 Subject: [PATCH 338/389] Show TLS version. --- src/Summary.cpp | 6 +++--- src/common/net/Client.cpp | 20 ++++++++++++++++++-- src/common/net/Client.h | 3 ++- src/common/net/Tls.cpp | 30 ++++++++++++++++++------------ src/common/net/Tls.h | 3 ++- src/net/Network.cpp | 5 ++++- 6 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/Summary.cpp b/src/Summary.cpp index 5feb6d7e..de6b1234 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -53,18 +53,18 @@ static void print_memory(xmrig::Config *config) { static void print_cpu(xmrig::Config *config) { if (config->isColors()) { - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") "%s (%d) %sx64 %sAES-NI", + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s (%d)") " %sx64 %sAES", "CPU", Cpu::brand(), Cpu::sockets(), Cpu::isX64() ? "\x1B[1;32m" : "\x1B[1;31m-", Cpu::hasAES() ? "\x1B[1;32m" : "\x1B[1;31m-"); # ifndef XMRIG_NO_LIBCPUID - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") "%.1f MB/%.1f MB", "CPU L2/L3", Cpu::l2() / 1024.0, Cpu::l3() / 1024.0); + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%.1f MB/%.1f MB"), "CPU L2/L3", Cpu::l2() / 1024.0, Cpu::l3() / 1024.0); # endif } else { - Log::i()->text(" * %-13s%s (%d) %sx64 %sAES-NI", "CPU", Cpu::brand(), Cpu::sockets(), Cpu::isX64() ? "" : "-", Cpu::hasAES() ? "" : "-"); + Log::i()->text(" * %-13s%s (%d) %sx64 %sAES", "CPU", Cpu::brand(), Cpu::sockets(), Cpu::isX64() ? "" : "-", Cpu::hasAES() ? "" : "-"); # ifndef XMRIG_NO_LIBCPUID Log::i()->text(" * %-13s%.1f MB/%.1f MB", "CPU L2/L3", Cpu::l2() / 1024.0, Cpu::l3() / 1024.0); # endif diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index 1057c474..d789ac89 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -186,9 +186,15 @@ bool Client::disconnect() } -bool Client::isTLS() const +const char *Client::tlsVersion() const { - return m_pool.isTLS() && m_tls; +# ifndef XMRIG_NO_TLS + if (isTLS()) { + return m_tls->tlsVersion(); + } +# endif + + return nullptr; } @@ -277,6 +283,16 @@ bool Client::isCriticalError(const char *message) } +bool Client::isTLS() const +{ +# ifndef XMRIG_NO_TLS + return m_pool.isTLS() && m_tls; +# else + return false; +# endif +} + + bool Client::parseJob(const rapidjson::Value ¶ms, int *code) { if (!params.IsObject()) { diff --git a/src/common/net/Client.h b/src/common/net/Client.h index 893fabfc..b3879885 100644 --- a/src/common/net/Client.h +++ b/src/common/net/Client.h @@ -69,7 +69,7 @@ public: ~Client(); bool disconnect(); - bool isTLS() const; + const char *tlsVersion() const; int64_t submit(const JobResult &result); void connect(); void connect(const Pool &pool); @@ -100,6 +100,7 @@ private: bool close(); bool isCriticalError(const char *message); + bool isTLS() const; bool parseJob(const rapidjson::Value ¶ms, int *code); bool parseLogin(const rapidjson::Value &result, int *code); bool send(BIO *bio); diff --git a/src/common/net/Tls.cpp b/src/common/net/Tls.cpp index 679e5d24..2cfc0e52 100644 --- a/src/common/net/Tls.cpp +++ b/src/common/net/Tls.cpp @@ -33,6 +33,7 @@ Client::Tls::Tls(Client *client) : + m_ready(false), m_buf(), m_client(client), m_ssl(nullptr) @@ -87,26 +88,31 @@ bool Client::Tls::send(const char *data, size_t size) } +const char *Client::Tls::tlsVersion() const +{ + return m_ready ? SSL_get_version(m_ssl) : nullptr; +} + + void Client::Tls::read(const char *data, size_t size) { BIO_write(m_readBio, data, size); if (!SSL_is_init_finished(m_ssl)) { - const int rc = SSL_connect(m_ssl); + const int rc = SSL_connect(m_ssl); - if (rc < 0 && SSL_get_error(m_ssl, rc) == SSL_ERROR_WANT_READ) { - send(); - } + if (rc < 0 && SSL_get_error(m_ssl, rc) == SSL_ERROR_WANT_READ) { + send(); + } else if (rc == 1) { + if (!verify()) { + LOG_ERR("[%s] TLS certificate verification failed", m_client->m_pool.url()); + m_client->close(); - if (rc == 1) { - if (!verify()) { - LOG_ERR("[%s] TLS certificate verification failed", m_client->m_pool.url()); - m_client->close(); + return; + } - return; - } - - m_client->login(); + m_ready = true; + m_client->login(); } return; diff --git a/src/common/net/Tls.h b/src/common/net/Tls.h index 13474237..ee3b59ef 100644 --- a/src/common/net/Tls.h +++ b/src/common/net/Tls.h @@ -39,7 +39,7 @@ public: bool handshake(); bool send(const char *data, size_t size); - + const char *tlsVersion() const; void read(const char *data, size_t size); private: @@ -48,6 +48,7 @@ private: BIO *m_readBio; BIO *m_writeBio; + bool m_ready; char m_buf[1024 * 2]; Client *m_client; SSL *m_ssl; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 7293a0ac..9997f7e7 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -101,7 +101,10 @@ void Network::onActive(IStrategy *strategy, Client *client) m_state.setPool(client->host(), client->port(), client->ip()); - LOG_INFO(isColors() ? "\x1B[01;37muse pool \x1B[01;36m%s:%d \x1B[01;30m%s" : "use pool %s:%d %s", client->host(), client->port(), client->ip()); + const char *tlsVersion = client->tlsVersion(); + LOG_INFO(isColors() ? WHITE_BOLD("use pool ") CYAN_BOLD("%s:%d ") GREEN_BOLD("%s") " \x1B[01;30m%s " + : "use pool %s:%d %s %s", + client->host(), client->port(), tlsVersion ? tlsVersion : "", client->ip()); } From 7da9e7a599b4cbdf7debf66289b1a72d3ee23f86 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 16 Sep 2018 08:14:22 +0300 Subject: [PATCH 339/389] Added TLS fingerprint validation. --- src/common/net/Client.cpp | 14 +++++++++- src/common/net/Client.h | 1 + src/common/net/Tls.cpp | 58 +++++++++++++++++++++++++++++++++++---- src/common/net/Tls.h | 7 +++-- src/net/Network.cpp | 7 ++++- 5 files changed, 78 insertions(+), 9 deletions(-) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index d789ac89..b039727d 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -186,11 +186,23 @@ bool Client::disconnect() } +const char *Client::tlsFingerprint() const +{ +# ifndef XMRIG_NO_TLS + if (isTLS() && m_pool.fingerprint() == nullptr) { + return m_tls->fingerprint(); + } +# endif + + return nullptr; +} + + const char *Client::tlsVersion() const { # ifndef XMRIG_NO_TLS if (isTLS()) { - return m_tls->tlsVersion(); + return m_tls->version(); } # endif diff --git a/src/common/net/Client.h b/src/common/net/Client.h index b3879885..55105c0b 100644 --- a/src/common/net/Client.h +++ b/src/common/net/Client.h @@ -69,6 +69,7 @@ public: ~Client(); bool disconnect(); + const char *tlsFingerprint() const; const char *tlsVersion() const; int64_t submit(const JobResult &result); void connect(); diff --git a/src/common/net/Tls.cpp b/src/common/net/Tls.cpp index 2cfc0e52..182d86ff 100644 --- a/src/common/net/Tls.cpp +++ b/src/common/net/Tls.cpp @@ -32,9 +32,15 @@ #include "common/log/Log.h" +#ifdef _MSC_VER +# define strncasecmp(x,y,z) _strnicmp(x,y,z) +#endif + + Client::Tls::Tls(Client *client) : m_ready(false), m_buf(), + m_fingerprint(), m_client(client), m_ssl(nullptr) { @@ -88,7 +94,13 @@ bool Client::Tls::send(const char *data, size_t size) } -const char *Client::Tls::tlsVersion() const +const char *Client::Tls::fingerprint() const +{ + return m_ready ? m_fingerprint : nullptr; +} + + +const char *Client::Tls::version() const { return m_ready ? SSL_get_version(m_ssl) : nullptr; } @@ -104,13 +116,15 @@ void Client::Tls::read(const char *data, size_t size) if (rc < 0 && SSL_get_error(m_ssl, rc) == SSL_ERROR_WANT_READ) { send(); } else if (rc == 1) { - if (!verify()) { - LOG_ERR("[%s] TLS certificate verification failed", m_client->m_pool.url()); + X509 *cert = SSL_get_peer_certificate(m_ssl); + if (!verify(cert)) { + X509_free(cert); m_client->close(); return; } + X509_free(cert); m_ready = true; m_client->login(); } @@ -131,12 +145,46 @@ bool Client::Tls::send() } -bool Client::Tls::verify() +bool Client::Tls::verify(X509 *cert) { - X509* cert = SSL_get_peer_certificate(m_ssl); if (cert == nullptr) { + LOG_ERR("[%s] Failed to get server certificate", m_client->m_pool.url()); + + return false; + } + + if (!verifyFingerprint(cert)) { + LOG_ERR("[%s] Failed to verify server certificate fingerprint", m_client->m_pool.url()); + + const char *fingerprint = m_client->m_pool.fingerprint(); + if (strlen(m_fingerprint) == 64 && fingerprint != nullptr) { + LOG_ERR("\"%s\" was given", m_fingerprint); + LOG_ERR("\"%s\" was configured", fingerprint); + } + return false; } return true; } + + +bool Client::Tls::verifyFingerprint(X509 *cert) +{ + const EVP_MD *digest = EVP_get_digestbyname("sha256"); + if (digest == nullptr) { + return false; + } + + unsigned char md[EVP_MAX_MD_SIZE]; + unsigned int dlen; + + if (X509_digest(cert, digest, md, &dlen) != 1) { + return false; + } + + Job::toHex(md, 32, m_fingerprint); + const char *fingerprint = m_client->m_pool.fingerprint(); + + return fingerprint == nullptr || strncasecmp(m_fingerprint, fingerprint, 64) == 0; +} diff --git a/src/common/net/Tls.h b/src/common/net/Tls.h index ee3b59ef..6e38f32f 100644 --- a/src/common/net/Tls.h +++ b/src/common/net/Tls.h @@ -39,17 +39,20 @@ public: bool handshake(); bool send(const char *data, size_t size); - const char *tlsVersion() const; + const char *fingerprint() const; + const char *version() const; void read(const char *data, size_t size); private: bool send(); - bool verify(); + bool verify(X509 *cert); + bool verifyFingerprint(X509 *cert); BIO *m_readBio; BIO *m_writeBio; bool m_ready; char m_buf[1024 * 2]; + char m_fingerprint[32 * 2 + 8]; Client *m_client; SSL *m_ssl; SSL_CTX *m_ctx; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 9997f7e7..703e0ccf 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -102,9 +102,14 @@ void Network::onActive(IStrategy *strategy, Client *client) m_state.setPool(client->host(), client->port(), client->ip()); const char *tlsVersion = client->tlsVersion(); - LOG_INFO(isColors() ? WHITE_BOLD("use pool ") CYAN_BOLD("%s:%d ") GREEN_BOLD("%s") " \x1B[01;30m%s " + LOG_INFO(isColors() ? WHITE_BOLD("use pool ") CYAN_BOLD("%s:%d ") GREEN_BOLD("%s") " \x1B[1;30m%s " : "use pool %s:%d %s %s", client->host(), client->port(), tlsVersion ? tlsVersion : "", client->ip()); + + const char *fingerprint = client->tlsFingerprint(); + if (fingerprint != nullptr) { + LOG_INFO("\x1B[1;30mfingerprint (SHA-256): \"%s\"", fingerprint); + } } From 1059189e2cdd8dc46eea87699851c52616a83469 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 17 Sep 2018 01:02:17 +0300 Subject: [PATCH 340/389] Skip TLS pools if miner built without TLS support. --- src/common/config/CommonConfig.cpp | 6 ++++++ src/common/net/Pool.cpp | 20 +++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index a9df89d7..b39e0e2f 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -229,6 +229,12 @@ bool xmrig::CommonConfig::finalize() pool.adjust(m_algorithm); if (pool.isValid() && pool.algorithm().isValid()) { +# ifdef XMRIG_NO_TLS + if (pool.isTLS()) { + continue; + } +# endif + m_activePools.push_back(std::move(pool)); } } diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 02bf5fa5..141e5115 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -117,15 +117,17 @@ bool Pool::isCompatible(const xmrig::Algorithm &algorithm) const bool Pool::isEqual(const Pool &other) const { - return (m_nicehash == other.m_nicehash - && m_keepAlive == other.m_keepAlive - && m_port == other.m_port - && m_algorithm == other.m_algorithm - && m_host == other.m_host - && m_password == other.m_password - && m_rigId == other.m_rigId - && m_url == other.m_url - && m_user == other.m_user); + return (m_nicehash == other.m_nicehash + && m_tls == other.m_tls + && m_keepAlive == other.m_keepAlive + && m_port == other.m_port + && m_algorithm == other.m_algorithm + && m_fingerprint == other.m_fingerprint + && m_host == other.m_host + && m_password == other.m_password + && m_rigId == other.m_rigId + && m_url == other.m_url + && m_user == other.m_user); } From a2d2c0418d48cd0a6f5632dd043e0e985d9700cc Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 17 Sep 2018 01:43:44 +0300 Subject: [PATCH 341/389] Fix gcc build with OpenSSL 1.1.1 --- cmake/OpenSSL.cmake | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmake/OpenSSL.cmake b/cmake/OpenSSL.cmake index 154a8278..d0c0d164 100644 --- a/cmake/OpenSSL.cmake +++ b/cmake/OpenSSL.cmake @@ -11,6 +11,10 @@ if (WITH_TLS) else() message(FATAL_ERROR "OpenSSL NOT found: use `-DWITH_TLS=OFF` to build without TLS support") endif() + + if (WIN32) + set(EXTRA_LIBS ${EXTRA_LIBS} Crypt32) + endif() else() set(TLS_SOURCES "") set(OPENSSL_LIBRARIES "") From 2189fe94ed0d63c444e8ba3bdf93b3e0eb2c1ce1 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 17 Sep 2018 06:37:53 +0300 Subject: [PATCH 342/389] Fix Linux build. --- CMakeLists.txt | 2 +- src/common/net/Client.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d38b1a0..317724e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,7 +156,7 @@ else() src/Mem_unix.cpp ) - set(EXTRA_LIBS pthread rt) + set(EXTRA_LIBS pthread rt dl) endif() if (CMAKE_SYSTEM_NAME STREQUAL FreeBSD) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index b039727d..3a93789b 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -414,7 +414,7 @@ bool Client::send(BIO *bio) LOG_DEBUG_ERR("[%s] send failed, invalid state: %d", m_pool.url(), m_state); } - BIO_reset(bio); + (void) BIO_reset(bio); return result; # else From 4c4e0593a75ec44bdd4972af3ba1575b9d4877c4 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 17 Sep 2018 09:24:18 +0300 Subject: [PATCH 343/389] Enable TLS by default. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 317724e3..b779b74d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ option(WITH_AEON "CryptoNight-Lite support" ON) option(WITH_SUMO "CryptoNight-Heavy support" ON) option(WITH_HTTPD "HTTP REST API" ON) option(WITH_DEBUG_LOG "Enable debug log output" OFF) -option(WITH_TLS "Enable OpenSSL support" OFF) +option(WITH_TLS "Enable OpenSSL support" ON) option(BUILD_STATIC "Build static binary" OFF) include (CheckIncludeFile) From bcd27ca2598e488c515441cc22e3f8c3993393bc Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 18 Sep 2018 04:04:25 +0300 Subject: [PATCH 344/389] Sync changes. --- src/common/config/CommonConfig.cpp | 18 +++++++++++++++++- src/common/interfaces/IConfig.h | 18 +++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index b39e0e2f..b70d5e3d 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -39,6 +39,20 @@ #endif +#ifdef XMRIG_AMD_PROJECT +# if defined(__APPLE__) +# include +# else +# include "3rdparty/CL/cl.h" +# endif +#endif + + +#ifdef XMRIG_NVIDIA_PROJECT +# include "nvidia/cryptonight.h" +#endif + + #include "common/config/CommonConfig.h" #include "common/log/Log.h" #include "donate.h" @@ -154,6 +168,9 @@ void xmrig::CommonConfig::printVersions() const char *ocl = "0.0"; # endif int length = snprintf(buf, sizeof buf, "OpenCL/%s ", ocl); +# elif defined(XMRIG_NVIDIA_PROJECT) + const int cudaVersion = cuda_get_runtime_version(); + int length = snprintf(buf, sizeof buf, "CUDA/%d.%d ", cudaVersion / 1000, cudaVersion % 100); # else memset(buf, 0, 16); int length = 0; @@ -170,7 +187,6 @@ void xmrig::CommonConfig::printVersions() length += snprintf(buf + length, (sizeof buf) - length, "microhttpd/%s ", MHD_get_version()); # endif - Log::i()->text(isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13slibuv/%s %s") : " * %-13slibuv/%s %s", "LIBS", uv_version_string(), buf); diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index ece8e3b4..d3593163 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -82,13 +82,17 @@ public: HardwareAESKey = 1011, // xmrig amd - OclPlatform = 1400, - OclAffinity = 1401, - OclDevices = 1402, - OclLaunch = 1403, - OclCache = 1404, - OclPrint = 1405, - OclLoader = 1406, + OclPlatformKey = 1400, + OclAffinityKey = 1401, + OclDevicesKey = 1402, + OclLaunchKey = 1403, + OclCacheKey = 1404, + OclPrintKey = 1405, + OclLoaderKey = 1406, + OclSridedIndexKey = 1407, + OclMemChunkKey = 1408, + OclUnrollKey = 1409, + OclCompModeKey = 1410, // xmrig-proxy AccessLogFileKey = 'A', From 0adad684711ed9507e17d270955f68d909f58267 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 20 Sep 2018 07:33:32 +0300 Subject: [PATCH 345/389] # Fixed send buffer overflow and increase send buffer size. --- src/common/net/Client.cpp | 4 +++- src/common/net/Client.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index 3a93789b..6a79749d 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -482,7 +482,9 @@ int64_t Client::send(const rapidjson::Document &doc) doc.Accept(writer); const size_t size = buffer.GetSize(); - if (size > (sizeof(m_buf) - 2)) { + if (size > (sizeof(m_sendBuf) - 2)) { + LOG_ERR("[%s] send failed: \"send buffer overflow: %zu > %zu\"", m_pool.url(), size, (sizeof(m_sendBuf) - 2)); + close(); return -1; } diff --git a/src/common/net/Client.h b/src/common/net/Client.h index 55105c0b..d6418338 100644 --- a/src/common/net/Client.h +++ b/src/common/net/Client.h @@ -140,7 +140,7 @@ private: bool m_quiet; char m_buf[kInputBufferSize]; char m_ip[46]; - char m_sendBuf[1024]; + char m_sendBuf[2048]; const char *m_agent; IClientListener *m_listener; int m_extensions; From feb1a758c117e3b05ee20213a01dadf93ebe6f2b Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 22 Sep 2018 08:42:15 +0300 Subject: [PATCH 346/389] Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ef9573d..d64bb2b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v2.8.0 +- **[#753](https://github.com/xmrig/xmrig/issues/753) Added new algorithm [CryptoNight variant 2](https://github.com/xmrig/xmrig/issues/753) for Monero fork, thanks [@SChernykh](https://github.com/SChernykh).** +- **Added SSL/TLS support for secure connections to pools.** +- [#245](https://github.com/xmrig/xmrig-proxy/issues/245) Fixed API ID collision when run multiple miners on same machine. +- [#757](https://github.com/xmrig/xmrig/issues/757) Fixed send buffer overflow. + # v2.6.4 - [#700](https://github.com/xmrig/xmrig/issues/700) `cryptonight-lite/ipbc` replaced to `cryptonight-heavy/tube` for **Bittube (TUBE)**. - Added `cryptonight/rto` (cryptonight variant 1 with IPBC/TUBE mod) variant for **Arto (RTO)** coin. From f1a1e0814c222d70ca6afb366a350db5f29f0283 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 22 Sep 2018 08:54:56 +0300 Subject: [PATCH 347/389] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d64bb2b7..4e1e2412 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # v2.8.0 - **[#753](https://github.com/xmrig/xmrig/issues/753) Added new algorithm [CryptoNight variant 2](https://github.com/xmrig/xmrig/issues/753) for Monero fork, thanks [@SChernykh](https://github.com/SChernykh).** -- **Added SSL/TLS support for secure connections to pools.** +- [#758](https://github.com/xmrig/xmrig/issues/758) **Added SSL/TLS support for secure connections to pools.** - [#245](https://github.com/xmrig/xmrig-proxy/issues/245) Fixed API ID collision when run multiple miners on same machine. - [#757](https://github.com/xmrig/xmrig/issues/757) Fixed send buffer overflow. From 3a7e8647bbac67f87137cec8f1ee0939fdddcf23 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 22 Sep 2018 15:54:31 +0300 Subject: [PATCH 348/389] Fix for automatic variant specified by pool/proxy. --- src/common/net/Job.cpp | 10 ++++++++++ src/common/net/Job.h | 2 +- src/common/xmrig.h | 15 ++++++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index bb5f6a12..2bfb39f0 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -170,6 +170,16 @@ bool Job::setTarget(const char *target) } +void Job::setAlgorithm(const char *algo) +{ + m_algorithm.parseAlgorithm(algo); + + if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { + m_algorithm.setVariant(variant()); + } +} + + bool Job::fromHex(const char* in, unsigned int len, unsigned char* out) { bool error = false; diff --git a/src/common/net/Job.h b/src/common/net/Job.h index 8bd1b8ad..b561b9c1 100644 --- a/src/common/net/Job.h +++ b/src/common/net/Job.h @@ -44,6 +44,7 @@ public: bool setBlob(const char *blob); bool setTarget(const char *target); + void setAlgorithm(const char *algo); inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } @@ -60,7 +61,6 @@ public: inline uint32_t diff() const { return static_cast(m_diff); } inline uint64_t target() const { return m_target; } inline void reset() { m_size = 0; m_diff = 0; } - inline void setAlgorithm(const char *algo) { m_algorithm.parseAlgorithm(algo); } inline void setClientId(const xmrig::Id &id) { m_clientId = id; } inline void setPoolId(int poolId) { m_poolId = poolId; } inline void setThreadId(int threadId) { m_threadId = threadId; } diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 3e1b65df..e1c7702e 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __XMRIG_H__ -#define __XMRIG_H__ +#ifndef XMRIG_XMRIG_H +#define XMRIG_XMRIG_H namespace xmrig @@ -85,7 +85,16 @@ enum AesMode { }; +enum OclVendor { + OCL_VENDOR_UNKNOWN = -2, + OCL_VENDOR_MANUAL = -1, + OCL_VENDOR_AMD = 0, + OCL_VENDOR_NVIDIA = 1, + OCL_VENDOR_INTEL = 2 +}; + + } /* namespace xmrig */ -#endif /* __XMRIG_H__ */ +#endif /* XMRIG_XMRIG_H */ From 30791de5bc41baee1efea45900c9e778d009ac06 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 22 Sep 2018 16:01:27 +0300 Subject: [PATCH 349/389] Don't use static OpenSSL on Linux by default. --- cmake/OpenSSL.cmake | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cmake/OpenSSL.cmake b/cmake/OpenSSL.cmake index d0c0d164..ed287e7e 100644 --- a/cmake/OpenSSL.cmake +++ b/cmake/OpenSSL.cmake @@ -1,7 +1,12 @@ if (WITH_TLS) set(OPENSSL_ROOT_DIR ${XMRIG_DEPS}) - set(OPENSSL_USE_STATIC_LIBS TRUE) - set(OPENSSL_MSVC_STATIC_RT TRUE) + + if (WIN32) + set(OPENSSL_USE_STATIC_LIBS TRUE) + set(OPENSSL_MSVC_STATIC_RT TRUE) + + set(EXTRA_LIBS ${EXTRA_LIBS} Crypt32) + endif() find_package(OpenSSL) @@ -11,10 +16,6 @@ if (WITH_TLS) else() message(FATAL_ERROR "OpenSSL NOT found: use `-DWITH_TLS=OFF` to build without TLS support") endif() - - if (WIN32) - set(EXTRA_LIBS ${EXTRA_LIBS} Crypt32) - endif() else() set(TLS_SOURCES "") set(OPENSSL_LIBRARIES "") From d8ca5ef205dcd9a895992afa8315b10173292e09 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sat, 22 Sep 2018 17:21:32 +0200 Subject: [PATCH 350/389] Cryptonight variant 2 - final version Reference code: https://github.com/monero-project/monero/pull/4404 I tested it on x86 with av=1-10 and on ARM with av=1-4, self test passed. --- src/crypto/CryptoNight_arm.h | 6 +++--- src/crypto/CryptoNight_monero.h | 24 ++++++++++++++++++++++++ src/crypto/CryptoNight_test.h | 20 ++++++++++---------- src/crypto/CryptoNight_x86.h | 8 ++++---- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 4cd5eba5..4fcebc3e 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -487,7 +487,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si if (VARIANT == xmrig::VARIANT_2) { VARIANT2_INTEGER_MATH(0, cl, cx); lo = __umul128(idx0, cl, &hi); - VARIANT2_SHUFFLE(l0, idx0 & MASK, ax0, bx0, bx1); + VARIANT2_SHUFFLE2(l0, idx0 & MASK, ax0, bx0, bx1, hi, lo); } else { lo = __umul128(idx0, cl, &hi); @@ -619,7 +619,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si if (VARIANT == xmrig::VARIANT_2) { VARIANT2_INTEGER_MATH(0, cl, cx0); lo = __umul128(idx0, cl, &hi); - VARIANT2_SHUFFLE(l0, idx0 & MASK, ax0, bx00, bx01); + VARIANT2_SHUFFLE2(l0, idx0 & MASK, ax0, bx00, bx01, hi, lo); } else { lo = __umul128(idx0, cl, &hi); } @@ -662,7 +662,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si if (VARIANT == xmrig::VARIANT_2) { VARIANT2_INTEGER_MATH(1, cl, cx1); lo = __umul128(idx1, cl, &hi); - VARIANT2_SHUFFLE(l1, idx1 & MASK, ax1, bx10, bx11); + VARIANT2_SHUFFLE2(l1, idx1 & MASK, ax1, bx10, bx11, hi, lo); } else { lo = __umul128(idx1, cl, &hi); } diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index 80750c6c..52229026 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -93,6 +93,18 @@ _mm_store_si128((__m128i *)((base_ptr) + ((offset) ^ 0x30)), _mm_add_epi64(chunk2, _a)); \ } while (0) +# define VARIANT2_SHUFFLE2(base_ptr, offset, _a, _b, _b1, hi, lo) \ + do { \ + const __m128i chunk1 = _mm_xor_si128(_mm_load_si128((__m128i *)((base_ptr) + ((offset) ^ 0x10))), _mm_set_epi64x(lo, hi)); \ + const __m128i chunk2 = _mm_load_si128((__m128i *)((base_ptr) + ((offset) ^ 0x20))); \ + hi ^= ((uint64_t*)((base_ptr) + ((offset) ^ 0x20)))[0]; \ + lo ^= ((uint64_t*)((base_ptr) + ((offset) ^ 0x20)))[1]; \ + const __m128i chunk3 = _mm_load_si128((__m128i *)((base_ptr) + ((offset) ^ 0x30))); \ + _mm_store_si128((__m128i *)((base_ptr) + ((offset) ^ 0x10)), _mm_add_epi64(chunk3, _b1)); \ + _mm_store_si128((__m128i *)((base_ptr) + ((offset) ^ 0x20)), _mm_add_epi64(chunk1, _b)); \ + _mm_store_si128((__m128i *)((base_ptr) + ((offset) ^ 0x30)), _mm_add_epi64(chunk2, _a)); \ + } while (0) + #else # define VARIANT2_INIT(part) \ uint64_t division_result_##part = h##part[12]; \ @@ -122,5 +134,17 @@ vst1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x20)), vaddq_u64(chunk1, vreinterpretq_u64_u8(_b))); \ vst1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x30)), vaddq_u64(chunk2, vreinterpretq_u64_u8(_a))); \ } while (0) + +# define VARIANT2_SHUFFLE2(base_ptr, offset, _a, _b, _b1, hi, lo) \ + do { \ + const uint64x2_t chunk1 = veorq_u64(vld1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x10))), vcombine_u64(vcreate_u64(hi), vcreate_u64(lo))); \ + const uint64x2_t chunk2 = vld1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x20))); \ + hi ^= ((uint64_t*)((base_ptr) + ((offset) ^ 0x20)))[0]; \ + lo ^= ((uint64_t*)((base_ptr) + ((offset) ^ 0x20)))[1]; \ + const uint64x2_t chunk3 = vld1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x30))); \ + vst1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x10)), vaddq_u64(chunk3, vreinterpretq_u64_u8(_b1))); \ + vst1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x20)), vaddq_u64(chunk1, vreinterpretq_u64_u8(_b))); \ + vst1q_u64((uint64_t*)((base_ptr) + ((offset) ^ 0x30)), vaddq_u64(chunk2, vreinterpretq_u64_u8(_a))); \ + } while (0) #endif #endif /* XMRIG_CRYPTONIGHT_MONERO_H */ diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 953f88d0..95e12197 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -86,16 +86,16 @@ const static uint8_t test_output_v1[160] = { // Cryptonight variant 2 (Monero v8) const static uint8_t test_output_v2[160] = { - 0x6E, 0xEE, 0x53, 0xA3, 0xDA, 0xD1, 0x8C, 0x05, 0xB8, 0xCB, 0x32, 0x17, 0xAA, 0xEA, 0xEA, 0xB4, - 0x16, 0x11, 0x01, 0xA9, 0x08, 0x76, 0x37, 0x36, 0x6F, 0xDC, 0xCA, 0xC6, 0x92, 0x0D, 0xEA, 0x09, - 0x91, 0x03, 0x2F, 0x5B, 0x27, 0x4D, 0x94, 0x1D, 0x60, 0x50, 0xDC, 0x1F, 0x35, 0x57, 0xEC, 0x20, - 0xA6, 0xAC, 0x10, 0xDB, 0xCF, 0x36, 0x23, 0x8F, 0x96, 0xC7, 0x72, 0x8B, 0xF9, 0xE7, 0x30, 0xEB, - 0x50, 0x58, 0x4B, 0xFE, 0xAD, 0xC5, 0x13, 0x79, 0x50, 0x98, 0x1C, 0x67, 0xB2, 0xEB, 0xDA, 0x64, - 0xD4, 0xAA, 0xC4, 0xE8, 0xE5, 0xC9, 0xE7, 0x6B, 0x84, 0xC2, 0xD2, 0xE9, 0x1F, 0xA1, 0x0F, 0xDF, - 0x45, 0x06, 0x80, 0x25, 0x32, 0x6B, 0xC4, 0x66, 0x2A, 0x69, 0x9F, 0x1E, 0x1F, 0x4C, 0xBE, 0x89, - 0xFE, 0x61, 0xBB, 0x04, 0x79, 0xB5, 0x3B, 0x45, 0x58, 0xD9, 0x9C, 0x18, 0x7C, 0x48, 0x1B, 0x44, - 0x92, 0xC4, 0x4C, 0xD0, 0x8F, 0x16, 0x44, 0x79, 0x71, 0x48, 0x63, 0x0B, 0x51, 0xB6, 0x33, 0x8B, - 0x6B, 0x3F, 0xCC, 0x0A, 0x3A, 0x14, 0x3B, 0x49, 0x68, 0x46, 0xB9, 0x46, 0xC6, 0xA3, 0x03, 0x41 + 0x97, 0x37, 0x82, 0x82, 0xCF, 0x10, 0xE7, 0xAD, 0x03, 0x3F, 0x7B, 0x80, 0x74, 0xC4, 0x0E, 0x14, + 0xD0, 0x6E, 0x7F, 0x60, 0x9D, 0xDD, 0xDA, 0x78, 0x76, 0x80, 0xB5, 0x8C, 0x05, 0xF4, 0x3D, 0x21, + 0x87, 0x1F, 0xCD, 0x68, 0x23, 0xF6, 0xA8, 0x79, 0xBB, 0x3F, 0x33, 0x95, 0x1C, 0x8E, 0x8E, 0x89, + 0x1D, 0x40, 0x43, 0x88, 0x0B, 0x02, 0xDF, 0xA1, 0xBB, 0x3B, 0xE4, 0x98, 0xB5, 0x0E, 0x75, 0x78, + 0xE6, 0x0D, 0x24, 0x0F, 0x65, 0x85, 0x60, 0x3A, 0x4A, 0xE5, 0x5F, 0x54, 0x9B, 0xC8, 0x79, 0x93, + 0xEB, 0x3D, 0x98, 0x2C, 0xFE, 0x9B, 0xFB, 0x15, 0xB6, 0x88, 0x21, 0x94, 0xB0, 0x05, 0x86, 0x5C, + 0x59, 0x8B, 0x93, 0x7A, 0xDA, 0xD2, 0xA2, 0x14, 0xED, 0xB7, 0xC4, 0x5D, 0xA1, 0xEF, 0x26, 0xF3, + 0xC7, 0x73, 0x29, 0x4D, 0xF1, 0xC8, 0x2C, 0xE0, 0xD0, 0xE9, 0xED, 0x0C, 0x70, 0x75, 0x05, 0x3E, + 0x5B, 0xF6, 0xA0, 0x6E, 0xEA, 0xDE, 0x87, 0x0B, 0x06, 0x29, 0x03, 0xBF, 0xB4, 0x85, 0x9D, 0x04, + 0x75, 0x1A, 0xCD, 0x1E, 0xD6, 0xAA, 0x1B, 0x05, 0x24, 0x6A, 0x2C, 0x80, 0x69, 0x68, 0xDC, 0x97 }; diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index be879228..1cb06687 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -512,7 +512,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si if (VARIANT == xmrig::VARIANT_2) { VARIANT2_INTEGER_MATH(0, cl, cx); lo = __umul128(idx0, cl, &hi); - VARIANT2_SHUFFLE(l0, idx0 & MASK, ax0, bx0, bx1); + VARIANT2_SHUFFLE2(l0, idx0 & MASK, ax0, bx0, bx1, hi, lo); } else { lo = __umul128(idx0, cl, &hi); @@ -643,7 +643,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si if (VARIANT == xmrig::VARIANT_2) { VARIANT2_INTEGER_MATH(0, cl, cx0); lo = __umul128(idx0, cl, &hi); - VARIANT2_SHUFFLE(l0, idx0 & MASK, ax0, bx00, bx01); + VARIANT2_SHUFFLE2(l0, idx0 & MASK, ax0, bx00, bx01, hi, lo); } else { lo = __umul128(idx0, cl, &hi); } @@ -684,7 +684,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si if (VARIANT == xmrig::VARIANT_2) { VARIANT2_INTEGER_MATH(1, cl, cx1); lo = __umul128(idx1, cl, &hi); - VARIANT2_SHUFFLE(l1, idx1 & MASK, ax1, bx10, bx11); + VARIANT2_SHUFFLE2(l1, idx1 & MASK, ax1, bx10, bx11, hi, lo); } else { lo = __umul128(idx1, cl, &hi); } @@ -772,7 +772,7 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si if (VARIANT == xmrig::VARIANT_2) { \ VARIANT2_INTEGER_MATH(part, cl##part, c); \ lo = __umul128(idx, cl##part, &hi); \ - VARIANT2_SHUFFLE(l, idx & MASK, a, b0, b1); \ + VARIANT2_SHUFFLE2(l, idx & MASK, a, b0, b1, hi, lo); \ } else { \ lo = __umul128(idx, cl##part, &hi); \ } \ From 1f609c7ebdb48acb5372417e4af07e57c9bc908e Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 23 Sep 2018 13:05:03 +0300 Subject: [PATCH 351/389] Update libcpuid to recent git version. --- src/3rdparty/libcpuid/asm-bits.c | 1661 +++++++++++---------- src/3rdparty/libcpuid/asm-bits.h | 124 +- src/3rdparty/libcpuid/cpuid_main.c | 56 +- src/3rdparty/libcpuid/libcpuid.h | 39 +- src/3rdparty/libcpuid/libcpuid_internal.h | 5 +- src/3rdparty/libcpuid/libcpuid_types.h | 28 +- src/3rdparty/libcpuid/recog_amd.c | 4 + src/3rdparty/libcpuid/recog_intel.c | 3 +- 8 files changed, 991 insertions(+), 929 deletions(-) diff --git a/src/3rdparty/libcpuid/asm-bits.c b/src/3rdparty/libcpuid/asm-bits.c index b8e32284..bfabd404 100644 --- a/src/3rdparty/libcpuid/asm-bits.c +++ b/src/3rdparty/libcpuid/asm-bits.c @@ -1,825 +1,836 @@ -/* - * Copyright 2008 Veselin Georgiev, - * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "libcpuid.h" -#include "asm-bits.h" - -int cpuid_exists_by_eflags(void) -{ -#if defined(PLATFORM_X64) - return 1; /* CPUID is always present on the x86_64 */ -#elif defined(PLATFORM_X86) -# if defined(COMPILER_GCC) - int result; - __asm __volatile( - " pushfl\n" - " pop %%eax\n" - " mov %%eax, %%ecx\n" - " xor $0x200000, %%eax\n" - " push %%eax\n" - " popfl\n" - " pushfl\n" - " pop %%eax\n" - " xor %%ecx, %%eax\n" - " mov %%eax, %0\n" - " push %%ecx\n" - " popfl\n" - : "=m"(result) - : :"eax", "ecx", "memory"); - return (result != 0); -# elif defined(COMPILER_MICROSOFT) - int result; - __asm { - pushfd - pop eax - mov ecx, eax - xor eax, 0x200000 - push eax - popfd - pushfd - pop eax - xor eax, ecx - mov result, eax - push ecx - popfd - }; - return (result != 0); -# else - return 0; -# endif /* COMPILER_MICROSOFT */ -#else - return 0; -#endif /* PLATFORM_X86 */ -} - -#ifdef INLINE_ASM_SUPPORTED -/* - * with MSVC/AMD64, the exec_cpuid() and cpu_rdtsc() functions - * are implemented in separate .asm files. Otherwise, use inline assembly - */ -void exec_cpuid(uint32_t *regs) -{ -#ifdef COMPILER_GCC -# ifdef PLATFORM_X64 - __asm __volatile( - " mov %0, %%rdi\n" - - " push %%rbx\n" - " push %%rcx\n" - " push %%rdx\n" - - " mov (%%rdi), %%eax\n" - " mov 4(%%rdi), %%ebx\n" - " mov 8(%%rdi), %%ecx\n" - " mov 12(%%rdi), %%edx\n" - - " cpuid\n" - - " movl %%eax, (%%rdi)\n" - " movl %%ebx, 4(%%rdi)\n" - " movl %%ecx, 8(%%rdi)\n" - " movl %%edx, 12(%%rdi)\n" - " pop %%rdx\n" - " pop %%rcx\n" - " pop %%rbx\n" - : - :"m"(regs) - :"memory", "eax", "rdi" - ); -# else - __asm __volatile( - " mov %0, %%edi\n" - - " push %%ebx\n" - " push %%ecx\n" - " push %%edx\n" - - " mov (%%edi), %%eax\n" - " mov 4(%%edi), %%ebx\n" - " mov 8(%%edi), %%ecx\n" - " mov 12(%%edi), %%edx\n" - - " cpuid\n" - - " mov %%eax, (%%edi)\n" - " mov %%ebx, 4(%%edi)\n" - " mov %%ecx, 8(%%edi)\n" - " mov %%edx, 12(%%edi)\n" - " pop %%edx\n" - " pop %%ecx\n" - " pop %%ebx\n" - : - :"m"(regs) - :"memory", "eax", "edi" - ); -# endif /* COMPILER_GCC */ -#else -# ifdef COMPILER_MICROSOFT - __asm { - push ebx - push ecx - push edx - push edi - mov edi, regs - - mov eax, [edi] - mov ebx, [edi+4] - mov ecx, [edi+8] - mov edx, [edi+12] - - cpuid - - mov [edi], eax - mov [edi+4], ebx - mov [edi+8], ecx - mov [edi+12], edx - - pop edi - pop edx - pop ecx - pop ebx - } -# else -# error "Unsupported compiler" -# endif /* COMPILER_MICROSOFT */ -#endif -} -#endif /* INLINE_ASSEMBLY_SUPPORTED */ - -#ifdef INLINE_ASM_SUPPORTED -void cpu_rdtsc(uint64_t* result) -{ - uint32_t low_part, hi_part; -#ifdef COMPILER_GCC - __asm __volatile ( - " rdtsc\n" - " mov %%eax, %0\n" - " mov %%edx, %1\n" - :"=m"(low_part), "=m"(hi_part)::"memory", "eax", "edx" - ); -#else -# ifdef COMPILER_MICROSOFT - __asm { - rdtsc - mov low_part, eax - mov hi_part, edx - }; -# else -# error "Unsupported compiler" -# endif /* COMPILER_MICROSOFT */ -#endif /* COMPILER_GCC */ - *result = (uint64_t)low_part + (((uint64_t) hi_part) << 32); -} -#endif /* INLINE_ASM_SUPPORTED */ - -#ifdef INLINE_ASM_SUPPORTED -void busy_sse_loop(int cycles) -{ -#ifdef COMPILER_GCC -#ifndef __APPLE__ -# define XALIGN ".balign 16\n" -#else -# define XALIGN ".align 4\n" -#endif - __asm __volatile ( - " xorps %%xmm0, %%xmm0\n" - " xorps %%xmm1, %%xmm1\n" - " xorps %%xmm2, %%xmm2\n" - " xorps %%xmm3, %%xmm3\n" - " xorps %%xmm4, %%xmm4\n" - " xorps %%xmm5, %%xmm5\n" - " xorps %%xmm6, %%xmm6\n" - " xorps %%xmm7, %%xmm7\n" - XALIGN - /* ".bsLoop:\n" */ - "1:\n" - // 0: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - // 1: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - // 2: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - // 3: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - // 4: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - // 5: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - // 6: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - // 7: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - // 8: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - // 9: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //10: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //11: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //12: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //13: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //14: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //15: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //16: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //17: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //18: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //19: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //20: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //21: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //22: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //23: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //24: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //25: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //26: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //27: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //28: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //29: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //30: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - //31: - " addps %%xmm1, %%xmm0\n" - " addps %%xmm2, %%xmm1\n" - " addps %%xmm3, %%xmm2\n" - " addps %%xmm4, %%xmm3\n" - " addps %%xmm5, %%xmm4\n" - " addps %%xmm6, %%xmm5\n" - " addps %%xmm7, %%xmm6\n" - " addps %%xmm0, %%xmm7\n" - - " dec %%eax\n" - /* "jnz .bsLoop\n" */ - " jnz 1b\n" - ::"a"(cycles) - ); -#else -# ifdef COMPILER_MICROSOFT - __asm { - mov eax, cycles - xorps xmm0, xmm0 - xorps xmm1, xmm1 - xorps xmm2, xmm2 - xorps xmm3, xmm3 - xorps xmm4, xmm4 - xorps xmm5, xmm5 - xorps xmm6, xmm6 - xorps xmm7, xmm7 - //-- - align 16 -bsLoop: - // 0: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 1: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 2: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 3: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 4: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 5: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 6: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 7: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 8: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 9: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 10: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 11: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 12: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 13: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 14: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 15: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 16: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 17: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 18: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 19: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 20: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 21: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 22: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 23: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 24: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 25: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 26: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 27: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 28: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 29: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 30: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - // 31: - addps xmm0, xmm1 - addps xmm1, xmm2 - addps xmm2, xmm3 - addps xmm3, xmm4 - addps xmm4, xmm5 - addps xmm5, xmm6 - addps xmm6, xmm7 - addps xmm7, xmm0 - //---------------------- - dec eax - jnz bsLoop - } -# else -# error "Unsupported compiler" -# endif /* COMPILER_MICROSOFT */ -#endif /* COMPILER_GCC */ -} -#endif /* INLINE_ASSEMBLY_SUPPORTED */ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "libcpuid.h" +#include "asm-bits.h" + +int cpuid_exists_by_eflags(void) +{ +#if defined(PLATFORM_X64) + return 1; /* CPUID is always present on the x86_64 */ +#elif defined(PLATFORM_X86) +# if defined(COMPILER_GCC) || defined(COMPILER_CLANG) + int result; + __asm __volatile( + " pushfl\n" + " pop %%eax\n" + " mov %%eax, %%ecx\n" + " xor $0x200000, %%eax\n" + " push %%eax\n" + " popfl\n" + " pushfl\n" + " pop %%eax\n" + " xor %%ecx, %%eax\n" + " mov %%eax, %0\n" + " push %%ecx\n" + " popfl\n" + : "=m"(result) + : :"eax", "ecx", "memory"); + return (result != 0); +# elif defined(COMPILER_MICROSOFT) + int result; + __asm { + pushfd + pop eax + mov ecx, eax + xor eax, 0x200000 + push eax + popfd + pushfd + pop eax + xor eax, ecx + mov result, eax + push ecx + popfd + }; + return (result != 0); +# else + return 0; +# endif /* COMPILER_MICROSOFT */ +#elif defined(PLATFORM_ARM) + return 0; +#else + return 0; +#endif /* PLATFORM_X86 */ +} + +#ifdef INLINE_ASM_SUPPORTED +/* + * with MSVC/AMD64, the exec_cpuid() and cpu_rdtsc() functions + * are implemented in separate .asm files. Otherwise, use inline assembly + */ +void exec_cpuid(uint32_t *regs) +{ +# if defined(COMPILER_GCC) || defined(COMPILER_CLANG) +# ifdef PLATFORM_X64 + __asm __volatile( + " mov %0, %%rdi\n" + + " push %%rbx\n" + " push %%rcx\n" + " push %%rdx\n" + + " mov (%%rdi), %%eax\n" + " mov 4(%%rdi), %%ebx\n" + " mov 8(%%rdi), %%ecx\n" + " mov 12(%%rdi), %%edx\n" + + " cpuid\n" + + " movl %%eax, (%%rdi)\n" + " movl %%ebx, 4(%%rdi)\n" + " movl %%ecx, 8(%%rdi)\n" + " movl %%edx, 12(%%rdi)\n" + " pop %%rdx\n" + " pop %%rcx\n" + " pop %%rbx\n" + : + :"m"(regs) + :"memory", "eax", "rdi" + ); +# elif defined(PLATFORM_X86) + __asm __volatile( + " mov %0, %%edi\n" + + " push %%ebx\n" + " push %%ecx\n" + " push %%edx\n" + + " mov (%%edi), %%eax\n" + " mov 4(%%edi), %%ebx\n" + " mov 8(%%edi), %%ecx\n" + " mov 12(%%edi), %%edx\n" + + " cpuid\n" + + " mov %%eax, (%%edi)\n" + " mov %%ebx, 4(%%edi)\n" + " mov %%ecx, 8(%%edi)\n" + " mov %%edx, 12(%%edi)\n" + " pop %%edx\n" + " pop %%ecx\n" + " pop %%ebx\n" + : + :"m"(regs) + :"memory", "eax", "edi" + ); +# elif defined(PLATFORM_ARM) +# endif /* COMPILER_GCC */ +#else +# ifdef COMPILER_MICROSOFT + __asm { + push ebx + push ecx + push edx + push edi + mov edi, regs + + mov eax, [edi] + mov ebx, [edi+4] + mov ecx, [edi+8] + mov edx, [edi+12] + + cpuid + + mov [edi], eax + mov [edi+4], ebx + mov [edi+8], ecx + mov [edi+12], edx + + pop edi + pop edx + pop ecx + pop ebx + } +# else +# error "Unsupported compiler" +# endif /* COMPILER_MICROSOFT */ +#endif +} +#endif /* INLINE_ASSEMBLY_SUPPORTED */ + +#ifdef INLINE_ASM_SUPPORTED +void cpu_rdtsc(uint64_t* result) +{ + uint32_t low_part, hi_part; +#if defined(COMPILER_GCC) || defined(COMPILER_CLANG) +#ifdef PLATFORM_ARM + low_part = 0; + hi_part = 0; +#else + __asm __volatile ( + " rdtsc\n" + " mov %%eax, %0\n" + " mov %%edx, %1\n" + :"=m"(low_part), "=m"(hi_part)::"memory", "eax", "edx" + ); +#endif +#else +# ifdef COMPILER_MICROSOFT + __asm { + rdtsc + mov low_part, eax + mov hi_part, edx + }; +# else +# error "Unsupported compiler" +# endif /* COMPILER_MICROSOFT */ +#endif /* COMPILER_GCC */ + *result = (uint64_t)low_part + (((uint64_t) hi_part) << 32); +} +#endif /* INLINE_ASM_SUPPORTED */ + +#ifdef INLINE_ASM_SUPPORTED +void busy_sse_loop(int cycles) +{ +# if defined(COMPILER_GCC) || defined(COMPILER_CLANG) +#ifndef __APPLE__ +# define XALIGN ".balign 16\n" +#else +# define XALIGN ".align 4\n" +#endif +#ifdef PLATFORM_ARM +#else + __asm __volatile ( + " xorps %%xmm0, %%xmm0\n" + " xorps %%xmm1, %%xmm1\n" + " xorps %%xmm2, %%xmm2\n" + " xorps %%xmm3, %%xmm3\n" + " xorps %%xmm4, %%xmm4\n" + " xorps %%xmm5, %%xmm5\n" + " xorps %%xmm6, %%xmm6\n" + " xorps %%xmm7, %%xmm7\n" + XALIGN + /* ".bsLoop:\n" */ + "1:\n" + // 0: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 1: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 2: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 3: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 4: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 5: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 6: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 7: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 8: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 9: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //10: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //11: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //12: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //13: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //14: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //15: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //16: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //17: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //18: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //19: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //20: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //21: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //22: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //23: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //24: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //25: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //26: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //27: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //28: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //29: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //30: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //31: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + + " dec %%eax\n" + /* "jnz .bsLoop\n" */ + " jnz 1b\n" + ::"a"(cycles) + ); +#endif +#else +# ifdef COMPILER_MICROSOFT + __asm { + mov eax, cycles + xorps xmm0, xmm0 + xorps xmm1, xmm1 + xorps xmm2, xmm2 + xorps xmm3, xmm3 + xorps xmm4, xmm4 + xorps xmm5, xmm5 + xorps xmm6, xmm6 + xorps xmm7, xmm7 + //-- + align 16 +bsLoop: + // 0: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 1: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 2: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 3: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 4: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 5: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 6: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 7: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 8: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 9: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 10: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 11: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 12: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 13: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 14: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 15: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 16: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 17: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 18: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 19: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 20: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 21: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 22: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 23: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 24: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 25: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 26: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 27: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 28: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 29: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 30: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 31: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + //---------------------- + dec eax + jnz bsLoop + } +# else +# error "Unsupported compiler" +# endif /* COMPILER_MICROSOFT */ +#endif /* COMPILER_GCC */ +} +#endif /* INLINE_ASSEMBLY_SUPPORTED */ \ No newline at end of file diff --git a/src/3rdparty/libcpuid/asm-bits.h b/src/3rdparty/libcpuid/asm-bits.h index 3a03e11c..9049e2fe 100644 --- a/src/3rdparty/libcpuid/asm-bits.h +++ b/src/3rdparty/libcpuid/asm-bits.h @@ -1,53 +1,71 @@ -/* - * Copyright 2008 Veselin Georgiev, - * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __ASM_BITS_H__ -#define __ASM_BITS_H__ -#include "libcpuid.h" - -/* Determine Compiler: */ -#if defined(_MSC_VER) -# define COMPILER_MICROSOFT -#elif defined(__GNUC__) -# define COMPILER_GCC -#endif - -/* Determine Platform */ -#if defined(__x86_64__) || defined(_M_AMD64) -# define PLATFORM_X64 -#elif defined(__i386__) || defined(_M_IX86) -# define PLATFORM_X86 -#endif - -/* Under Windows/AMD64 with MSVC, inline assembly isn't supported */ -#if (defined(COMPILER_GCC) && defined(PLATFORM_X64)) || defined(PLATFORM_X86) -# define INLINE_ASM_SUPPORTED -#endif - -int cpuid_exists_by_eflags(void); -void exec_cpuid(uint32_t *regs); -void busy_sse_loop(int cycles); - -#endif /* __ASM_BITS_H__ */ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef __ASM_BITS_H__ +#define __ASM_BITS_H__ +#include "libcpuid.h" + +/* Determine Compiler: */ +#if defined(_MSC_VER) +#if !defined(COMPILER_MICROSOFT) +# define COMPILER_MICROSOFT +#endif +#elif defined(__GNUC__) +#if !defined(COMPILER_GCC) +# define COMPILER_GCC +#endif +#elif defined(__clang__) +#if !defined(COMPILER_CLANG) +# define COMPILER_CLANG +#endif +#endif + +/* Determine Platform */ +#if defined(__x86_64__) || defined(_M_AMD64) +#if !defined(PLATFORM_X64) +# define PLATFORM_X64 +#endif +#elif defined(__i386__) || defined(_M_IX86) +#if !defined(PLATFORM_X86) +# define PLATFORM_X86 +#endif +#elif defined(__ARMEL__) +#if !defined(PLATFORM_ARM) +# define PLATFORM_ARM +#endif +#endif + +/* Under Windows/AMD64 with MSVC, inline assembly isn't supported */ +#if (((defined(COMPILER_GCC) || defined(COMPILER_CLANG))) && \ + (defined(PLATFORM_X64) || defined(PLATFORM_X86) || defined(PLATFORM_ARM))) || \ + (defined(COMPILER_MICROSOFT) && defined(PLATFORM_X86)) +# define INLINE_ASM_SUPPORTED +#endif + +int cpuid_exists_by_eflags(void); +void exec_cpuid(uint32_t *regs); +void busy_sse_loop(int cycles); + +#endif /* __ASM_BITS_H__ */ diff --git a/src/3rdparty/libcpuid/cpuid_main.c b/src/3rdparty/libcpuid/cpuid_main.c index f22c7dd6..61cb638d 100644 --- a/src/3rdparty/libcpuid/cpuid_main.c +++ b/src/3rdparty/libcpuid/cpuid_main.c @@ -221,42 +221,42 @@ static void load_features_common(struct cpu_raw_data_t* raw, struct cpu_id_t* da static cpu_vendor_t cpuid_vendor_identify(const uint32_t *raw_vendor, char *vendor_str) { - int i; - cpu_vendor_t vendor = VENDOR_UNKNOWN; - const struct { cpu_vendor_t vendor; char match[16]; } - matchtable[NUM_CPU_VENDORS] = { - /* source: http://www.sandpile.org/ia32/cpuid.htm */ - { VENDOR_INTEL , "GenuineIntel" }, - { VENDOR_AMD , "AuthenticAMD" }, - { VENDOR_CYRIX , "CyrixInstead" }, - { VENDOR_NEXGEN , "NexGenDriven" }, - { VENDOR_TRANSMETA , "GenuineTMx86" }, - { VENDOR_UMC , "UMC UMC UMC " }, - { VENDOR_CENTAUR , "CentaurHauls" }, - { VENDOR_RISE , "RiseRiseRise" }, - { VENDOR_SIS , "SiS SiS SiS " }, - { VENDOR_NSC , "Geode by NSC" }, - }; + int i; + cpu_vendor_t vendor = VENDOR_UNKNOWN; + const struct { cpu_vendor_t vendor; char match[16]; } + matchtable[NUM_CPU_VENDORS] = { + /* source: http://www.sandpile.org/ia32/cpuid.htm */ + { VENDOR_INTEL , "GenuineIntel" }, + { VENDOR_AMD , "AuthenticAMD" }, + { VENDOR_CYRIX , "CyrixInstead" }, + { VENDOR_NEXGEN , "NexGenDriven" }, + { VENDOR_TRANSMETA , "GenuineTMx86" }, + { VENDOR_UMC , "UMC UMC UMC " }, + { VENDOR_CENTAUR , "CentaurHauls" }, + { VENDOR_RISE , "RiseRiseRise" }, + { VENDOR_SIS , "SiS SiS SiS " }, + { VENDOR_NSC , "Geode by NSC" }, + }; - memcpy(vendor_str + 0, &raw_vendor[1], 4); - memcpy(vendor_str + 4, &raw_vendor[3], 4); - memcpy(vendor_str + 8, &raw_vendor[2], 4); - vendor_str[12] = 0; + memcpy(vendor_str + 0, &raw_vendor[1], 4); + memcpy(vendor_str + 4, &raw_vendor[3], 4); + memcpy(vendor_str + 8, &raw_vendor[2], 4); + vendor_str[12] = 0; - /* Determine vendor: */ - for (i = 0; i < NUM_CPU_VENDORS; i++) - if (!strcmp(vendor_str, matchtable[i].match)) { - vendor = matchtable[i].vendor; - break; - } - return vendor; + /* Determine vendor: */ + for (i = 0; i < NUM_CPU_VENDORS; i++) + if (!strcmp(vendor_str, matchtable[i].match)) { + vendor = matchtable[i].vendor; + break; + } + return vendor; } static int cpuid_basic_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data) { int i, j, basic, xmodel, xfamily, ext; char brandstr[64] = {0}; - data->vendor = cpuid_vendor_identify(raw->basic_cpuid[0], data->vendor_str); + data->vendor = cpuid_vendor_identify(raw->basic_cpuid[0], data->vendor_str); if (data->vendor == VENDOR_UNKNOWN) return set_error(ERR_CPU_UNKN); diff --git a/src/3rdparty/libcpuid/libcpuid.h b/src/3rdparty/libcpuid/libcpuid.h index c44990c3..847e5a4a 100644 --- a/src/3rdparty/libcpuid/libcpuid.h +++ b/src/3rdparty/libcpuid/libcpuid.h @@ -60,7 +60,7 @@ */ /** @mainpage A simple libcpuid introduction - * + * * LibCPUID provides CPU identification and access to the CPUID and RDTSC * instructions on the x86. *

@@ -82,6 +82,7 @@ */ /** @defgroup libcpuid LibCPUID + * @brief LibCPUID provides CPU identification @{ */ /* Include some integer type specifications: */ @@ -535,23 +536,23 @@ typedef enum { * @brief Describes common library error codes */ typedef enum { - ERR_OK = 0, /*!< "No error" */ - ERR_NO_CPUID = -1, /*!< "CPUID instruction is not supported" */ - ERR_NO_RDTSC = -2, /*!< "RDTSC instruction is not supported" */ - ERR_NO_MEM = -3, /*!< "Memory allocation failed" */ - ERR_OPEN = -4, /*!< "File open operation failed" */ - ERR_BADFMT = -5, /*!< "Bad file format" */ - ERR_NOT_IMP = -6, /*!< "Not implemented" */ - ERR_CPU_UNKN = -7, /*!< "Unsupported processor" */ - ERR_NO_RDMSR = -8, /*!< "RDMSR instruction is not supported" */ - ERR_NO_DRIVER= -9, /*!< "RDMSR driver error (generic)" */ - ERR_NO_PERMS = -10, /*!< "No permissions to install RDMSR driver" */ - ERR_EXTRACT = -11, /*!< "Cannot extract RDMSR driver (read only media?)" */ - ERR_HANDLE = -12, /*!< "Bad handle" */ - ERR_INVMSR = -13, /*!< "Invalid MSR" */ - ERR_INVCNB = -14, /*!< "Invalid core number" */ - ERR_HANDLE_R = -15, /*!< "Error on handle read" */ - ERR_INVRANGE = -16, /*!< "Invalid given range" */ + ERR_OK = 0, /*!< No error */ + ERR_NO_CPUID = -1, /*!< CPUID instruction is not supported */ + ERR_NO_RDTSC = -2, /*!< RDTSC instruction is not supported */ + ERR_NO_MEM = -3, /*!< Memory allocation failed */ + ERR_OPEN = -4, /*!< File open operation failed */ + ERR_BADFMT = -5, /*!< Bad file format */ + ERR_NOT_IMP = -6, /*!< Not implemented */ + ERR_CPU_UNKN = -7, /*!< Unsupported processor */ + ERR_NO_RDMSR = -8, /*!< RDMSR instruction is not supported */ + ERR_NO_DRIVER= -9, /*!< RDMSR driver error (generic) */ + ERR_NO_PERMS = -10, /*!< No permissions to install RDMSR driver */ + ERR_EXTRACT = -11, /*!< Cannot extract RDMSR driver (read only media?) */ + ERR_HANDLE = -12, /*!< Bad handle */ + ERR_INVMSR = -13, /*!< Invalid MSR */ + ERR_INVCNB = -14, /*!< Invalid core number */ + ERR_HANDLE_R = -15, /*!< Error on handle read */ + ERR_INVRANGE = -16, /*!< Invalid given range */ } cpu_error_t; /** @@ -668,7 +669,7 @@ struct cpu_epc_t cpuid_get_epc(int index, const struct cpu_raw_data_t* raw); const char* cpuid_lib_version(void); #ifdef __cplusplus -}; /* extern "C" */ +} /* extern "C" */ #endif diff --git a/src/3rdparty/libcpuid/libcpuid_internal.h b/src/3rdparty/libcpuid/libcpuid_internal.h index 038aa209..64804616 100644 --- a/src/3rdparty/libcpuid/libcpuid_internal.h +++ b/src/3rdparty/libcpuid/libcpuid_internal.h @@ -75,8 +75,9 @@ enum _intel_bits_t { _3 = LBIT( 14 ), _5 = LBIT( 15 ), _7 = LBIT( 16 ), - XEON_ = LBIT( 17 ), - ATOM_ = LBIT( 18 ), + _9 = LBIT( 17 ), + XEON_ = LBIT( 18 ), + ATOM_ = LBIT( 19 ), }; typedef enum _intel_bits_t intel_bits_t; diff --git a/src/3rdparty/libcpuid/libcpuid_types.h b/src/3rdparty/libcpuid/libcpuid_types.h index 9e897275..0e667aa6 100644 --- a/src/3rdparty/libcpuid/libcpuid_types.h +++ b/src/3rdparty/libcpuid/libcpuid_types.h @@ -32,6 +32,32 @@ #ifndef __LIBCPUID_TYPES_H__ #define __LIBCPUID_TYPES_H__ -#include +#if !defined(_MSC_VER) || _MSC_VER >= 1600 +# include +#else +/* we have to provide our own: */ +# if !defined(__int32_t_defined) +typedef int int32_t; +# endif + +# if !defined(__uint32_t_defined) +typedef unsigned uint32_t; +# endif + +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef signed short int16_t; +typedef unsigned short uint16_t; +#if (defined _MSC_VER) && (_MSC_VER <= 1300) + /* MSVC 6.0: no long longs ... */ + typedef signed __int64 int64_t; + typedef unsigned __int64 uint64_t; +#else + /* all other sane compilers: */ + typedef signed long long int64_t; + typedef unsigned long long uint64_t; +#endif + +#endif #endif /* __LIBCPUID_TYPES_H__ */ diff --git a/src/3rdparty/libcpuid/recog_amd.c b/src/3rdparty/libcpuid/recog_amd.c index 352d733b..4726f633 100644 --- a/src/3rdparty/libcpuid/recog_amd.c +++ b/src/3rdparty/libcpuid/recog_amd.c @@ -49,6 +49,10 @@ enum _amd_model_codes_t { _1400, _1500, _1600, + _1900, + _2400, + _2500, + _2700, }; static void load_amd_features(struct cpu_raw_data_t* raw, struct cpu_id_t* data) diff --git a/src/3rdparty/libcpuid/recog_intel.c b/src/3rdparty/libcpuid/recog_intel.c index 5467c19f..397d750e 100644 --- a/src/3rdparty/libcpuid/recog_intel.c +++ b/src/3rdparty/libcpuid/recog_intel.c @@ -376,7 +376,7 @@ static intel_code_and_bits_t get_brand_code_and_bits(struct cpu_id_t* data) bits |= bit_matchtable[i].bit; } - if ((i = match_pattern(bs, "Core(TM) [im][357]")) != 0) { + if ((i = match_pattern(bs, "Core(TM) [im][3579]")) != 0) { bits |= CORE_; i--; switch (bs[i + 9]) { @@ -387,6 +387,7 @@ static intel_code_and_bits_t get_brand_code_and_bits(struct cpu_id_t* data) case '3': bits |= _3; break; case '5': bits |= _5; break; case '7': bits |= _7; break; + case '9': bits |= _9; break; } } for (i = 0; i < COUNT_OF(matchtable); i++) From ee4d980955f749e82e6b0bc944b155c7f00ab131 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 23 Sep 2018 17:51:56 +0300 Subject: [PATCH 352/389] Old static class Cpu replaced to interface ICpuInfo. --- CMakeLists.txt | 13 +- src/App.cpp | 2 +- src/Cpu.h | 66 --------- src/Summary.cpp | 18 +-- src/api/ApiRouter.cpp | 11 +- .../cpu/BasicCpuInfo.cpp} | 60 +++----- src/common/cpu/BasicCpuInfo.h | 70 ++++++++++ .../cpu/BasicCpuInfo_arm.cpp} | 34 ++--- src/{Cpu_unix.cpp => common/cpu/Cpu.cpp} | 45 +++--- src/{Cpu_mac.cpp => common/cpu/Cpu.h} | 30 ++-- src/common/interfaces/ICpuInfo.h | 59 ++++++++ src/common/xmrig.h | 8 ++ src/core/Config.cpp | 18 +-- src/core/Controller.cpp | 2 +- src/{Cpu.cpp => core/cpu/AdvancedCpuInfo.cpp} | 132 ++++++++---------- src/core/cpu/AdvancedCpuInfo.h | 75 ++++++++++ src/{Cpu_win.cpp => core/cpu/Cpu.cpp} | 40 ++++-- src/workers/Worker.cpp | 4 +- 18 files changed, 406 insertions(+), 281 deletions(-) delete mode 100644 src/Cpu.h rename src/{Cpu_stub.cpp => common/cpu/BasicCpuInfo.cpp} (77%) create mode 100644 src/common/cpu/BasicCpuInfo.h rename src/{Cpu_arm.cpp => common/cpu/BasicCpuInfo_arm.cpp} (74%) rename src/{Cpu_unix.cpp => common/cpu/Cpu.cpp} (71%) rename src/{Cpu_mac.cpp => common/cpu/Cpu.h} (75%) create mode 100644 src/common/interfaces/ICpuInfo.h rename src/{Cpu.cpp => core/cpu/AdvancedCpuInfo.cpp} (57%) create mode 100644 src/core/cpu/AdvancedCpuInfo.h rename src/{Cpu_win.cpp => core/cpu/Cpu.cpp} (69%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b779b74d..6f7d6291 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ set(HEADERS src/common/config/ConfigLoader.h src/common/config/ConfigWatcher.h src/common/Console.h + src/common/cpu/Cpu.h src/common/crypto/Algorithm.h src/common/crypto/keccak.h src/common/interfaces/IClientListener.h @@ -27,6 +28,7 @@ set(HEADERS src/common/interfaces/IConfigCreator.h src/common/interfaces/IConsoleListener.h src/common/interfaces/IControllerListener.h + src/common/interfaces/ICpuInfo.h src/common/interfaces/ILogBackend.h src/common/interfaces/IStrategy.h src/common/interfaces/IStrategyListener.h @@ -49,7 +51,6 @@ set(HEADERS src/common/xmrig.h src/core/ConfigLoader_platform.h src/core/Controller.h - src/Cpu.h src/interfaces/IJobResultListener.h src/interfaces/IThread.h src/interfaces/IWorker.h @@ -135,7 +136,6 @@ if (WIN32) res/app.rc src/App_win.cpp src/common/Platform_win.cpp - src/Cpu_win.cpp src/Mem_win.cpp ) @@ -145,14 +145,12 @@ elseif (APPLE) set(SOURCES_OS src/App_unix.cpp src/common/Platform_mac.cpp - src/Cpu_mac.cpp src/Mem_unix.cpp ) else() set(SOURCES_OS src/App_unix.cpp src/common/Platform_unix.cpp - src/Cpu_unix.cpp src/Mem_unix.cpp ) @@ -184,14 +182,15 @@ if (WITH_LIBCPUID) include_directories(src/3rdparty/libcpuid) set(CPUID_LIB cpuid) - set(SOURCES_CPUID src/Cpu.cpp) + set(SOURCES_CPUID src/core/cpu/AdvancedCpuInfo.h src/core/cpu/AdvancedCpuInfo.cpp src/core/cpu/Cpu.cpp) else() add_definitions(/DXMRIG_NO_LIBCPUID) + set(SOURCES_CPUID src/common/cpu/BasicCpuInfo.h src/common/cpu/Cpu.cpp) if (XMRIG_ARM) - set(SOURCES_CPUID src/Cpu_arm.cpp) + set(SOURCES_CPUID ${SOURCES_CPUID} src/common/cpu/BasicCpuInfo_arm.cpp) else() - set(SOURCES_CPUID src/Cpu_stub.cpp) + set(SOURCES_CPUID ${SOURCES_CPUID} src/common/cpu/BasicCpuInfo.cpp) endif() endif() diff --git a/src/App.cpp b/src/App.cpp index adcc5752..134e4ef5 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -29,11 +29,11 @@ #include "api/Api.h" #include "App.h" #include "common/Console.h" +#include "common/cpu/Cpu.h" #include "common/log/Log.h" #include "common/Platform.h" #include "core/Config.h" #include "core/Controller.h" -#include "Cpu.h" #include "crypto/CryptoNight.h" #include "Mem.h" #include "net/Network.h" diff --git a/src/Cpu.h b/src/Cpu.h deleted file mode 100644 index a125bae8..00000000 --- a/src/Cpu.h +++ /dev/null @@ -1,66 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef __CPU_H__ -#define __CPU_H__ - - -#include - - -class Cpu -{ -public: - enum Flags { - X86_64 = 1, - AES = 2, - BMI2 = 4 - }; - - static size_t optimalThreadsCount(size_t size, int maxCpuUsage); - static void init(); - - static inline bool hasAES() { return (m_flags & AES) != 0; } - static inline bool isX64() { return (m_flags & X86_64) != 0; } - static inline const char *brand() { return m_brand; } - static inline int cores() { return m_totalCores; } - static inline int l2() { return m_l2_cache; } - static inline int l3() { return m_l3_cache; } - static inline int sockets() { return m_sockets; } - static inline int threads() { return m_totalThreads; } - -private: - static void initCommon(); - - static bool m_l2_exclusive; - static char m_brand[64]; - static int m_flags; - static int m_l2_cache; - static int m_l3_cache; - static int m_sockets; - static int m_totalCores; - static size_t m_totalThreads; -}; - - -#endif /* __CPU_H__ */ diff --git a/src/Summary.cpp b/src/Summary.cpp index de6b1234..ba220e5b 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -27,11 +27,11 @@ #include +#include "common/cpu/Cpu.h" #include "common/log/Log.h" #include "common/net/Pool.h" #include "core/Config.h" #include "core/Controller.h" -#include "Cpu.h" #include "Mem.h" #include "Summary.h" #include "version.h" @@ -52,21 +52,23 @@ static void print_memory(xmrig::Config *config) { static void print_cpu(xmrig::Config *config) { + using namespace xmrig; + if (config->isColors()) { Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s (%d)") " %sx64 %sAES", "CPU", - Cpu::brand(), - Cpu::sockets(), - Cpu::isX64() ? "\x1B[1;32m" : "\x1B[1;31m-", - Cpu::hasAES() ? "\x1B[1;32m" : "\x1B[1;31m-"); + Cpu::info()->brand(), + Cpu::info()->sockets(), + Cpu::info()->isX64() ? "\x1B[1;32m" : "\x1B[1;31m-", + Cpu::info()->hasAES() ? "\x1B[1;32m" : "\x1B[1;31m-"); # ifndef XMRIG_NO_LIBCPUID - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%.1f MB/%.1f MB"), "CPU L2/L3", Cpu::l2() / 1024.0, Cpu::l3() / 1024.0); + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%.1f MB/%.1f MB"), "CPU L2/L3", Cpu::info()->L2() / 1024.0, Cpu::info()->L3() / 1024.0); # endif } else { - Log::i()->text(" * %-13s%s (%d) %sx64 %sAES", "CPU", Cpu::brand(), Cpu::sockets(), Cpu::isX64() ? "" : "-", Cpu::hasAES() ? "" : "-"); + Log::i()->text(" * %-13s%s (%d) %sx64 %sAES", "CPU", Cpu::info()->brand(), Cpu::info()->sockets(), Cpu::info()->isX64() ? "" : "-", Cpu::info()->hasAES() ? "" : "-"); # ifndef XMRIG_NO_LIBCPUID - Log::i()->text(" * %-13s%.1f MB/%.1f MB", "CPU L2/L3", Cpu::l2() / 1024.0, Cpu::l3() / 1024.0); + Log::i()->text(" * %-13s%.1f MB/%.1f MB", "CPU L2/L3", Cpu::info()->L2() / 1024.0, Cpu::info()->L3() / 1024.0); # endif } } diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 2c62696a..dd7accf6 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -35,12 +35,12 @@ #include "api/ApiRouter.h" #include "common/api/HttpReply.h" #include "common/api/HttpRequest.h" +#include "common/cpu/Cpu.h" #include "common/crypto/keccak.h" #include "common/net/Job.h" #include "common/Platform.h" #include "core/Config.h" #include "core/Controller.h" -#include "Cpu.h" #include "interfaces/IThread.h" #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" @@ -238,13 +238,14 @@ void ApiRouter::getIdentify(rapidjson::Document &doc) const void ApiRouter::getMiner(rapidjson::Document &doc) const { + using namespace xmrig; auto &allocator = doc.GetAllocator(); rapidjson::Value cpu(rapidjson::kObjectType); - cpu.AddMember("brand", rapidjson::StringRef(Cpu::brand()), allocator); - cpu.AddMember("aes", Cpu::hasAES(), allocator); - cpu.AddMember("x64", Cpu::isX64(), allocator); - cpu.AddMember("sockets", Cpu::sockets(), allocator); + cpu.AddMember("brand", rapidjson::StringRef(Cpu::info()->brand()), allocator); + cpu.AddMember("aes", Cpu::info()->hasAES(), allocator); + cpu.AddMember("x64", Cpu::info()->isX64(), allocator); + cpu.AddMember("sockets", Cpu::info()->sockets(), allocator); doc.AddMember("version", APP_VERSION, allocator); doc.AddMember("kind", APP_KIND, allocator); diff --git a/src/Cpu_stub.cpp b/src/common/cpu/BasicCpuInfo.cpp similarity index 77% rename from src/Cpu_stub.cpp rename to src/common/cpu/BasicCpuInfo.cpp index 8b27ad65..66af53cc 100644 --- a/src/Cpu_stub.cpp +++ b/src/common/cpu/BasicCpuInfo.cpp @@ -21,6 +21,9 @@ * along with this program. If not, see . */ +#include +#include + #ifdef _MSC_VER # include @@ -32,14 +35,8 @@ # define bit_AES (1 << 25) #endif -#ifndef bit_BMI2 -# define bit_BMI2 (1 << 8) -#endif -#include - - -#include "Cpu.h" +#include "common/cpu/BasicCpuInfo.h" #define VENDOR_ID (0) @@ -96,43 +93,18 @@ static inline bool has_aes_ni() } -static inline bool has_bmi2() { - int cpu_info[4] = { 0 }; - cpuid(EXTENDED_FEATURES, cpu_info); - - return (cpu_info[EBX_Reg] & bit_BMI2) != 0; -} - - -char Cpu::m_brand[64] = { 0 }; -int Cpu::m_flags = 0; -int Cpu::m_l2_cache = 0; -int Cpu::m_l3_cache = 0; -int Cpu::m_sockets = 1; -int Cpu::m_totalCores = 0; -size_t Cpu::m_totalThreads = 0; - - -size_t Cpu::optimalThreadsCount(size_t size, int maxCpuUsage) -{ - const size_t count = m_totalThreads / 2; - return count < 1 ? 1 : count; -} - - -void Cpu::initCommon() +xmrig::BasicCpuInfo::BasicCpuInfo() : + m_aes(has_aes_ni()), + m_brand(), + m_threads(std::thread::hardware_concurrency()) { cpu_brand_string(m_brand); - -# if defined(__x86_64__) || defined(_M_AMD64) - m_flags |= X86_64; -# endif - - if (has_aes_ni()) { - m_flags |= AES; - } - - if (has_bmi2()) { - m_flags |= BMI2; - } +} + + +size_t xmrig::BasicCpuInfo::optimalThreadsCount(size_t memSize, int maxCpuUsage) const +{ + const size_t count = threads() / 2; + + return count < 1 ? 1 : count; } diff --git a/src/common/cpu/BasicCpuInfo.h b/src/common/cpu/BasicCpuInfo.h new file mode 100644 index 00000000..f9d710d6 --- /dev/null +++ b/src/common/cpu/BasicCpuInfo.h @@ -0,0 +1,70 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_BASICCPUINFO_H +#define XMRIG_BASICCPUINFO_H + + +#include "common/interfaces/ICpuInfo.h" + + +namespace xmrig { + + +class BasicCpuInfo : public ICpuInfo +{ +public: + BasicCpuInfo(); + +protected: + size_t optimalThreadsCount(size_t memSize, int maxCpuUsage) const override; + + inline Assembly assembly() const override { return ASM_NONE; } + inline bool hasAES() const override { return m_aes; } + inline bool isSupported() const override { return true; } + inline const char *brand() const override { return m_brand; } + inline int32_t cores() const override { return -1; } + inline int32_t L2() const override { return -1; } + inline int32_t L3() const override { return -1; } + inline int32_t nodes() const override { return -1; } + inline int32_t sockets() const override { return 1; } + inline int32_t threads() const override { return m_threads; } + +# if defined(__x86_64__) || defined(_M_AMD64) || defined (__arm64__) || defined (__aarch64__) + inline bool isX64() const override { return true; } +# else + inline bool isX64() const override { return false; } +# endif + +private: + bool m_aes; + char m_brand[64]; + int32_t m_threads; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_BASICCPUINFO_H */ diff --git a/src/Cpu_arm.cpp b/src/common/cpu/BasicCpuInfo_arm.cpp similarity index 74% rename from src/Cpu_arm.cpp rename to src/common/cpu/BasicCpuInfo_arm.cpp index 3e259d0d..34eb7388 100644 --- a/src/Cpu_arm.cpp +++ b/src/common/cpu/BasicCpuInfo_arm.cpp @@ -21,37 +21,27 @@ * along with this program. If not, see . */ - #include +#include -#include "Cpu.h" +#include "common/cpu/BasicCpuInfo.h" -char Cpu::m_brand[64] = { 0 }; -int Cpu::m_flags = 0; -int Cpu::m_l2_cache = 0; -int Cpu::m_l3_cache = 0; -int Cpu::m_sockets = 1; -int Cpu::m_totalCores = 0; -size_t Cpu::m_totalThreads = 0; - - -size_t Cpu::optimalThreadsCount(size_t size, int maxCpuUsage) -{ - return m_totalThreads; -} - - -void Cpu::initCommon() +xmrig::BasicCpuInfo::BasicCpuInfo() : + m_aes(false), + m_brand(), + m_threads(std::thread::hardware_concurrency()) { memcpy(m_brand, "Unknown", 7); -# if defined (__arm64__) || defined (__aarch64__) - m_flags |= X86_64; -# endif - # if __ARM_FEATURE_CRYPTO m_flags |= AES; # endif } + + +size_t xmrig::BasicCpuInfo::optimalThreadsCount(size_t memSize, int maxCpuUsage) const +{ + return threads(); +} diff --git a/src/Cpu_unix.cpp b/src/common/cpu/Cpu.cpp similarity index 71% rename from src/Cpu_unix.cpp rename to src/common/cpu/Cpu.cpp index b895c897..b1bb28ac 100644 --- a/src/Cpu_unix.cpp +++ b/src/common/cpu/Cpu.cpp @@ -22,33 +22,36 @@ */ -#ifdef __FreeBSD__ -# include -# include -# include -# include -#endif +#include -#include -#include -#include -#include +#include "common/cpu/BasicCpuInfo.h" +#include "common/cpu/Cpu.h" -#include "Cpu.h" +static xmrig::ICpuInfo *cpuInfo = nullptr; -#ifdef __FreeBSD__ -typedef cpuset_t cpu_set_t; -#endif - - -void Cpu::init() +xmrig::ICpuInfo *xmrig::Cpu::info() { -# ifdef XMRIG_NO_LIBCPUID - m_totalThreads = sysconf(_SC_NPROCESSORS_CONF); -# endif + assert(cpuInfo != nullptr); - initCommon(); + return cpuInfo; +} + + +void xmrig::Cpu::init() +{ + assert(cpuInfo == nullptr); + + cpuInfo = new BasicCpuInfo(); +} + + +void xmrig::Cpu::release() +{ + assert(cpuInfo != nullptr); + + delete cpuInfo; + cpuInfo = nullptr; } diff --git a/src/Cpu_mac.cpp b/src/common/cpu/Cpu.h similarity index 75% rename from src/Cpu_mac.cpp rename to src/common/cpu/Cpu.h index 085148bc..1d5a9fb1 100644 --- a/src/Cpu_mac.cpp +++ b/src/common/cpu/Cpu.h @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -21,20 +21,26 @@ * along with this program. If not, see . */ - -#include -#include -#include +#ifndef XMRIG_CPU_H +#define XMRIG_CPU_H -#include "Cpu.h" +#include "common/interfaces/ICpuInfo.h" -void Cpu::init() +namespace xmrig { + + +class Cpu { -# ifdef XMRIG_NO_LIBCPUID - m_totalThreads = sysconf(_SC_NPROCESSORS_CONF); -# endif +public: + static ICpuInfo *info(); + static void init(); + static void release(); +}; - initCommon(); -} + +} /* namespace xmrig */ + + +#endif /* XMRIG_CPU_H */ diff --git a/src/common/interfaces/ICpuInfo.h b/src/common/interfaces/ICpuInfo.h new file mode 100644 index 00000000..c959bf52 --- /dev/null +++ b/src/common/interfaces/ICpuInfo.h @@ -0,0 +1,59 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2018 XMRig + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_CPUINFO_H +#define XMRIG_CPUINFO_H + + +#include + + +#include "common/xmrig.h" + + +namespace xmrig { + + +class ICpuInfo +{ +public: + virtual ~ICpuInfo() {} + + virtual bool hasAES() const = 0; + virtual bool isSupported() const = 0; + virtual bool isX64() const = 0; + virtual const char *brand() const = 0; + virtual int32_t cores() const = 0; + virtual int32_t L2() const = 0; + virtual int32_t L3() const = 0; + virtual int32_t nodes() const = 0; + virtual int32_t sockets() const = 0; + virtual int32_t threads() const = 0; + virtual size_t optimalThreadsCount(size_t memSize, int maxCpuUsage) const = 0; + virtual xmrig::Assembly assembly() const = 0; +}; + + +} /* namespace xmrig */ + + +#endif // XMRIG_CPUINFO_H diff --git a/src/common/xmrig.h b/src/common/xmrig.h index e1c7702e..820bc4fb 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -94,6 +94,14 @@ enum OclVendor { }; +enum Assembly { + ASM_NONE, + ASM_AUTO, + ASM_INTEL, + ASM_RYZEN +}; + + } /* namespace xmrig */ diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 0380c26d..d99bfb09 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -27,9 +27,9 @@ #include "common/config/ConfigLoader.h" +#include "common/cpu/Cpu.h" #include "core/Config.h" #include "core/ConfigCreator.h" -#include "Cpu.h" #include "crypto/CryptoNight_constants.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" @@ -153,7 +153,7 @@ bool xmrig::Config::finalize() if (!m_threads.cpu.empty()) { m_threads.mode = Advanced; - const bool softAES = (m_aesMode == AES_AUTO ? (Cpu::hasAES() ? AES_HW : AES_SOFT) : m_aesMode) == AES_SOFT; + const bool softAES = (m_aesMode == AES_AUTO ? (Cpu::info()->hasAES() ? AES_HW : AES_SOFT) : m_aesMode) == AES_SOFT; for (size_t i = 0; i < m_threads.cpu.size(); ++i) { m_threads.list.push_back(CpuThread::createFromData(i, m_algorithm.algo(), m_threads.cpu[i], m_priority, softAES)); @@ -168,10 +168,10 @@ bool xmrig::Config::finalize() const size_t size = CpuThread::multiway(av) * cn_select_memory(m_algorithm.algo()) / 1024; if (!m_threads.count) { - m_threads.count = Cpu::optimalThreadsCount(size, m_maxCpuUsage); + m_threads.count = Cpu::info()->optimalThreadsCount(size, m_maxCpuUsage); } else if (m_safe) { - const size_t count = Cpu::optimalThreadsCount(size, m_maxCpuUsage); + const size_t count = Cpu::info()->optimalThreadsCount(size, m_maxCpuUsage); if (m_threads.count > count) { m_threads.count = count; } @@ -232,7 +232,7 @@ bool xmrig::Config::parseString(int key, const char *arg) case ThreadsKey: /* --threads */ if (strncmp(arg, "all", 3) == 0) { - m_threads.count = Cpu::threads(); + m_threads.count = Cpu::info()->threads(); return true; } @@ -339,10 +339,10 @@ xmrig::AlgoVariant xmrig::Config::getAlgoVariant() const # endif if (m_algoVariant <= AV_AUTO || m_algoVariant >= AV_MAX) { - return Cpu::hasAES() ? AV_SINGLE : AV_SINGLE_SOFT; + return Cpu::info()->hasAES() ? AV_SINGLE : AV_SINGLE_SOFT; } - if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV_DOUBLE) { + if (m_safe && !Cpu::info()->hasAES() && m_algoVariant <= AV_DOUBLE) { return static_cast(m_algoVariant + 2); } @@ -354,10 +354,10 @@ xmrig::AlgoVariant xmrig::Config::getAlgoVariant() const xmrig::AlgoVariant xmrig::Config::getAlgoVariantLite() const { if (m_algoVariant <= AV_AUTO || m_algoVariant >= AV_MAX) { - return Cpu::hasAES() ? AV_DOUBLE : AV_DOUBLE_SOFT; + return Cpu::info()->hasAES() ? AV_DOUBLE : AV_DOUBLE_SOFT; } - if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV_DOUBLE) { + if (m_safe && !Cpu::info()->hasAES() && m_algoVariant <= AV_DOUBLE) { return static_cast(m_algoVariant + 2); } diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index ce73f037..792ac939 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -26,6 +26,7 @@ #include "common/config/ConfigLoader.h" +#include "common/cpu/Cpu.h" #include "common/interfaces/IControllerListener.h" #include "common/log/ConsoleLog.h" #include "common/log/FileLog.h" @@ -33,7 +34,6 @@ #include "common/Platform.h" #include "core/Config.h" #include "core/Controller.h" -#include "Cpu.h" #include "net/Network.h" diff --git a/src/Cpu.cpp b/src/core/cpu/AdvancedCpuInfo.cpp similarity index 57% rename from src/Cpu.cpp rename to src/core/cpu/AdvancedCpuInfo.cpp index eebe585d..009a2bcb 100644 --- a/src/Cpu.cpp +++ b/src/core/cpu/AdvancedCpuInfo.cpp @@ -21,65 +21,24 @@ * along with this program. If not, see . */ - #include #include #include +#include -#include "Cpu.h" +#include "core/cpu/AdvancedCpuInfo.h" -bool Cpu::m_l2_exclusive = false; -char Cpu::m_brand[64] = { 0 }; -int Cpu::m_flags = 0; -int Cpu::m_l2_cache = 0; -int Cpu::m_l3_cache = 0; -int Cpu::m_sockets = 1; -int Cpu::m_totalCores = 0; -size_t Cpu::m_totalThreads = 0; - - -size_t Cpu::optimalThreadsCount(size_t size, int maxCpuUsage) -{ - if (m_totalThreads == 1) { - return 1; - } - - size_t cache = 0; - if (m_l3_cache) { - cache = m_l2_exclusive ? (m_l2_cache + m_l3_cache) : m_l3_cache; - } - else { - cache = m_l2_cache; - } - - size_t count = 0; - - if (cache) { - count = cache / size; - - if (cache % size >= size / 2) { - count++; - } - } - else { - count = m_totalThreads / 2; - } - - if (count > m_totalThreads) { - count = m_totalThreads; - } - - if (((float) count / m_totalThreads * 100) > maxCpuUsage) { - count = (int) ceil((float) m_totalThreads * (maxCpuUsage / 100.0)); - } - - return count < 1 ? 1 : count; -} - - -void Cpu::initCommon() +xmrig::AdvancedCpuInfo::AdvancedCpuInfo() : + m_aes(false), + m_L2_exclusive(false), + m_brand(), + m_cores(0), + m_L2(0), + m_L3(0), + m_sockets(1), + m_threads(std::thread::hardware_concurrency()) { struct cpu_raw_data_t raw = { 0 }; struct cpu_id_t data = { 0 }; @@ -89,40 +48,67 @@ void Cpu::initCommon() strncpy(m_brand, data.brand_str, sizeof(m_brand) - 1); - m_totalThreads = data.total_logical_cpus; - m_sockets = m_totalThreads / data.num_logical_cpus; - + m_sockets = threads() / data.num_logical_cpus; if (m_sockets == 0) { m_sockets = 1; } - m_totalCores = data.num_cores * m_sockets; - m_l3_cache = data.l3_cache > 0 ? data.l3_cache * m_sockets : 0; + m_cores = data.num_cores * m_sockets; + m_L3 = data.l3_cache > 0 ? data.l3_cache * m_sockets : 0; // Workaround for AMD CPUs https://github.com/anrieff/libcpuid/issues/97 if (data.vendor == VENDOR_AMD && data.ext_family >= 0x15 && data.ext_family < 0x17) { - m_l2_cache = data.l2_cache * (m_totalCores / 2) * m_sockets; - m_l2_exclusive = true; + m_L2 = data.l2_cache * (cores() / 2) * m_sockets; + m_L2_exclusive = true; } // Workaround for Intel Pentium Dual-Core, Core Duo, Core 2 Duo, Core 2 Quad and their Xeon homologue // These processors have L2 cache shared by 2 cores. else if (data.vendor == VENDOR_INTEL && data.ext_family == 0x06 && (data.ext_model == 0x0E || data.ext_model == 0x0F || data.ext_model == 0x17)) { - int l2_count_per_socket = m_totalCores > 1 ? m_totalCores / 2 : 1; - m_l2_cache = data.l2_cache > 0 ? data.l2_cache * l2_count_per_socket * m_sockets : 0; + int l2_count_per_socket = cores() > 1 ? cores() / 2 : 1; + m_L2 = data.l2_cache > 0 ? data.l2_cache * l2_count_per_socket * m_sockets : 0; } else{ - m_l2_cache = data.l2_cache > 0 ? data.l2_cache * m_totalCores * m_sockets : 0; + m_L2 = data.l2_cache > 0 ? data.l2_cache * cores() * m_sockets : 0; } -# if defined(__x86_64__) || defined(_M_AMD64) - m_flags |= X86_64; -# endif - - if (data.flags[CPU_FEATURE_AES]) { - m_flags |= AES; - } - - if (data.flags[CPU_FEATURE_BMI2]) { - m_flags |= BMI2; - } + m_aes = data.flags[CPU_FEATURE_AES]; +} + + +size_t xmrig::AdvancedCpuInfo::optimalThreadsCount(size_t memSize, int maxCpuUsage) const +{ + if (threads() == 1) { + return 1; + } + + size_t cache = 0; + if (m_L3) { + cache = m_L2_exclusive ? (m_L2 + m_L3) : m_L3; + } + else { + cache = m_L2; + } + + size_t count = 0; + + if (cache) { + count = cache / memSize; + + if (cache % memSize >= memSize / 2) { + count++; + } + } + else { + count = threads() / 2; + } + + if (count > (size_t) threads()) { + count = threads(); + } + + if (((float) count / threads() * 100) > maxCpuUsage) { + count = (int) ceil((float) threads() * (maxCpuUsage / 100.0)); + } + + return count < 1 ? 1 : count; } diff --git a/src/core/cpu/AdvancedCpuInfo.h b/src/core/cpu/AdvancedCpuInfo.h new file mode 100644 index 00000000..96fd329a --- /dev/null +++ b/src/core/cpu/AdvancedCpuInfo.h @@ -0,0 +1,75 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ADVANCEDCPUINFO_H +#define XMRIG_ADVANCEDCPUINFO_H + + +#include "common/interfaces/ICpuInfo.h" + + +namespace xmrig { + + +class AdvancedCpuInfo : public ICpuInfo +{ +public: + AdvancedCpuInfo(); + +protected: + size_t optimalThreadsCount(size_t memSize, int maxCpuUsage) const override; + + inline Assembly assembly() const override { return ASM_NONE; } + inline bool hasAES() const override { return m_aes; } + inline bool isSupported() const override { return true; } + inline const char *brand() const override { return m_brand; } + inline int32_t cores() const override { return m_cores; } + inline int32_t L2() const override { return m_L2; } + inline int32_t L3() const override { return m_L3; } + inline int32_t nodes() const override { return -1; } + inline int32_t sockets() const override { return m_sockets; } + inline int32_t threads() const override { return m_threads; } + +# if defined(__x86_64__) || defined(_M_AMD64) + inline bool isX64() const override { return true; } +# else + inline bool isX64() const override { return false; } +# endif + +private: + bool m_aes; + bool m_L2_exclusive; + char m_brand[64]; + int32_t m_cores; + int32_t m_L2; + int32_t m_L3; + int32_t m_sockets; + int32_t m_threads; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_ADVANCEDCPUINFO_H */ diff --git a/src/Cpu_win.cpp b/src/core/cpu/Cpu.cpp similarity index 69% rename from src/Cpu_win.cpp rename to src/core/cpu/Cpu.cpp index 7258f726..773255d2 100644 --- a/src/Cpu_win.cpp +++ b/src/core/cpu/Cpu.cpp @@ -22,20 +22,40 @@ */ -#include +#include -#include "Cpu.h" +#include "common/cpu/Cpu.h" -void Cpu::init() +#ifndef XMRIG_NO_LIBCPUID +# include "core/cpu/AdvancedCpuInfo.h" +#endif + + +static xmrig::ICpuInfo *cpuInfo = nullptr; + + +xmrig::ICpuInfo *xmrig::Cpu::info() { -# ifdef XMRIG_NO_LIBCPUID - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); + assert(cpuInfo != nullptr); - m_totalThreads = sysinfo.dwNumberOfProcessors; -# endif - - initCommon(); + return cpuInfo; +} + + +void xmrig::Cpu::init() +{ + assert(cpuInfo == nullptr); + + cpuInfo = new AdvancedCpuInfo(); +} + + +void xmrig::Cpu::release() +{ + assert(cpuInfo != nullptr); + + delete cpuInfo; + cpuInfo = nullptr; } diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index 567b3e08..c569908c 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -24,8 +24,8 @@ #include +#include "common/cpu/Cpu.h" #include "common/Platform.h" -#include "Cpu.h" #include "workers/CpuThread.h" #include "workers/Handle.h" #include "workers/Worker.h" @@ -41,7 +41,7 @@ Worker::Worker(Handle *handle) : m_sequence(0), m_thread(static_cast(handle->config())) { - if (Cpu::threads() > 1 && m_thread->affinity() != -1L) { + if (xmrig::Cpu::info()->threads() > 1 && m_thread->affinity() != -1L) { Platform::setThreadAffinity(m_thread->affinity()); } From c9fd8061c27ed9df8cdca08a843d395f464e5227 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 23 Sep 2018 18:07:44 +0300 Subject: [PATCH 353/389] Fix Linux build. --- src/common/interfaces/ICpuInfo.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/interfaces/ICpuInfo.h b/src/common/interfaces/ICpuInfo.h index c959bf52..267616d0 100644 --- a/src/common/interfaces/ICpuInfo.h +++ b/src/common/interfaces/ICpuInfo.h @@ -24,6 +24,7 @@ #define XMRIG_CPUINFO_H +#include #include From 0c20d7a125f2cb823b6e4b6aa4577e951126f17a Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 23 Sep 2018 19:09:05 +0300 Subject: [PATCH 354/389] Auto-detect proper asm variant. --- src/common/cpu/BasicCpuInfo_arm.cpp | 2 +- src/core/cpu/AdvancedCpuInfo.cpp | 12 +++++++++++- src/core/cpu/AdvancedCpuInfo.h | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/common/cpu/BasicCpuInfo_arm.cpp b/src/common/cpu/BasicCpuInfo_arm.cpp index 34eb7388..c1c127db 100644 --- a/src/common/cpu/BasicCpuInfo_arm.cpp +++ b/src/common/cpu/BasicCpuInfo_arm.cpp @@ -36,7 +36,7 @@ xmrig::BasicCpuInfo::BasicCpuInfo() : memcpy(m_brand, "Unknown", 7); # if __ARM_FEATURE_CRYPTO - m_flags |= AES; + m_aes = true; # endif } diff --git a/src/core/cpu/AdvancedCpuInfo.cpp b/src/core/cpu/AdvancedCpuInfo.cpp index 009a2bcb..ac5508c3 100644 --- a/src/core/cpu/AdvancedCpuInfo.cpp +++ b/src/core/cpu/AdvancedCpuInfo.cpp @@ -31,6 +31,7 @@ xmrig::AdvancedCpuInfo::AdvancedCpuInfo() : + m_assembly(ASM_NONE), m_aes(false), m_L2_exclusive(false), m_brand(), @@ -71,7 +72,16 @@ xmrig::AdvancedCpuInfo::AdvancedCpuInfo() : m_L2 = data.l2_cache > 0 ? data.l2_cache * cores() * m_sockets : 0; } - m_aes = data.flags[CPU_FEATURE_AES]; + if (data.flags[CPU_FEATURE_AES]) { + m_aes = true; + + if (data.vendor == VENDOR_AMD && data.ext_family >= 23) { + m_assembly = ASM_RYZEN; + } + else if (data.vendor == VENDOR_INTEL && data.ext_model >= 42) { + m_assembly = ASM_INTEL; + } + } } diff --git a/src/core/cpu/AdvancedCpuInfo.h b/src/core/cpu/AdvancedCpuInfo.h index 96fd329a..5e8967ad 100644 --- a/src/core/cpu/AdvancedCpuInfo.h +++ b/src/core/cpu/AdvancedCpuInfo.h @@ -7,7 +7,6 @@ * Copyright 2017-2018 XMR-Stak , * Copyright 2016-2018 XMRig , * - * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -40,7 +39,7 @@ public: protected: size_t optimalThreadsCount(size_t memSize, int maxCpuUsage) const override; - inline Assembly assembly() const override { return ASM_NONE; } + inline Assembly assembly() const override { return m_assembly; } inline bool hasAES() const override { return m_aes; } inline bool isSupported() const override { return true; } inline const char *brand() const override { return m_brand; } @@ -58,6 +57,7 @@ protected: # endif private: + Assembly m_assembly; bool m_aes; bool m_L2_exclusive; char m_brand[64]; From dd27c422932c797021221403cb95984a2013e94b Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 23 Sep 2018 20:16:33 +0300 Subject: [PATCH 355/389] Initial compile with ASM. --- CMakeLists.txt | 4 +- cmake/asm.cmake | 23 +++ src/crypto/asm/cnv2_main_loop.S | 21 +++ src/crypto/asm/cnv2_main_loop.asm | 18 ++ src/crypto/asm/cnv2_main_loop_ivybridge.inc | 183 ++++++++++++++++++++ src/crypto/asm/cnv2_main_loop_ryzen.inc | 179 +++++++++++++++++++ 6 files changed, 427 insertions(+), 1 deletion(-) create mode 100644 cmake/asm.cmake create mode 100644 src/crypto/asm/cnv2_main_loop.S create mode 100644 src/crypto/asm/cnv2_main_loop.asm create mode 100644 src/crypto/asm/cnv2_main_loop_ivybridge.inc create mode 100644 src/crypto/asm/cnv2_main_loop_ryzen.inc diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f7d6291..22bf9a78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ option(WITH_SUMO "CryptoNight-Heavy support" ON) option(WITH_HTTPD "HTTP REST API" ON) option(WITH_DEBUG_LOG "Enable debug log output" OFF) option(WITH_TLS "Enable OpenSSL support" ON) +option(WITH_ASM "Enable ASM PoW implementations" ON) option(BUILD_STATIC "Build static binary" OFF) include (CheckIncludeFile) @@ -195,6 +196,7 @@ else() endif() include(cmake/OpenSSL.cmake) +include(cmake/asm.cmake) CHECK_INCLUDE_FILE (syslog.h HAVE_SYSLOG_H) if (HAVE_SYSLOG_H) @@ -254,4 +256,4 @@ if (WITH_DEBUG_LOG) endif() add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES} ${TLS_SOURCES}) -target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) +target_link_libraries(${PROJECT_NAME} ${${XMRIG_ASM_LIBRARY}} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) diff --git a/cmake/asm.cmake b/cmake/asm.cmake new file mode 100644 index 00000000..02093810 --- /dev/null +++ b/cmake/asm.cmake @@ -0,0 +1,23 @@ +if (WITH_ASM AND NOT XMRIG_ARM) + set(XMRIG_ASM_LIBRARY "xmrig-asm") + + if (CMAKE_CXX_COMPILER_ID MATCHES MSVC) + enable_language(ASM_MASM) + set_property(SOURCE "src/crypto/asm/cnv2_main_loop.asm" PROPERTY ASM_MASM) + add_library(${XMRIG_ASM_LIBRARY} STATIC + "src/crypto/asm/cnv2_main_loop.asm" + ) + else() + enable_language(ASM) + set_property(SOURCE "src/crypto/asm/cnv2_main_loop.S" PROPERTY C) + add_library(${XMRIG_ASM_LIBRARY} STATIC + "src/crypto/asm/cnv2_main_loop.S" + ) + endif() + + set_property(TARGET ${XMRIG_ASM_LIBRARY} PROPERTY LINKER_LANGUAGE C) +else() +# set(XMRIG_ASM_SOURCES "") + set(XMRIG_ASM_LIBRARY "") + add_definitions(/DXMRIG_NO_ASM) +endif() diff --git a/src/crypto/asm/cnv2_main_loop.S b/src/crypto/asm/cnv2_main_loop.S new file mode 100644 index 00000000..dc5a82f5 --- /dev/null +++ b/src/crypto/asm/cnv2_main_loop.S @@ -0,0 +1,21 @@ +#define ALIGN .align +.intel_syntax noprefix +.section .text +.global cnv2_mainloop_ivybridge_asm +.global cnv2_mainloop_ryzen_asm + +ALIGN 64 +cnv2_mainloop_ivybridge_asm: + sub rsp, 48 + mov rcx, rdi + #include "cnv2_main_loop_ivybridge.inc" + add rsp, 48 + ret 0 + +ALIGN 64 +cnv2_mainloop_ryzen_asm: + sub rsp, 48 + mov rcx, rdi + #include "cnv2_main_loop_ryzen.inc" + add rsp, 48 + ret 0 diff --git a/src/crypto/asm/cnv2_main_loop.asm b/src/crypto/asm/cnv2_main_loop.asm new file mode 100644 index 00000000..7ec895c4 --- /dev/null +++ b/src/crypto/asm/cnv2_main_loop.asm @@ -0,0 +1,18 @@ +_TEXT_CNV2_MAINLOOP SEGMENT PAGE READ EXECUTE +PUBLIC cnv2_mainloop_ivybridge_asm +PUBLIC cnv2_mainloop_ryzen_asm + +ALIGN 64 +cnv2_mainloop_ivybridge_asm PROC + INCLUDE cnv2_main_loop_ivybridge.inc + ret 0 +cnv2_mainloop_ivybridge_asm ENDP + +ALIGN 64 +cnv2_mainloop_ryzen_asm PROC + INCLUDE cnv2_main_loop_ryzen.inc + ret 0 +cnv2_mainloop_ryzen_asm ENDP + +_TEXT_CNV2_MAINLOOP ENDS +END diff --git a/src/crypto/asm/cnv2_main_loop_ivybridge.inc b/src/crypto/asm/cnv2_main_loop_ivybridge.inc new file mode 100644 index 00000000..0985d1bd --- /dev/null +++ b/src/crypto/asm/cnv2_main_loop_ivybridge.inc @@ -0,0 +1,183 @@ + mov QWORD PTR [rsp+24], rbx + push rbp + push rsi + push rdi + push r12 + push r13 + push r14 + push r15 + sub rsp, 80 + + stmxcsr DWORD PTR [rsp] + mov DWORD PTR [rsp+4], 24448 + ldmxcsr DWORD PTR [rsp+4] + + mov rax, QWORD PTR [rcx+48] + mov r9, rcx + xor rax, QWORD PTR [rcx+16] + mov esi, 524288 + mov r8, QWORD PTR [rcx+32] + mov r13d, -2147483647 + xor r8, QWORD PTR [rcx] + mov r11, QWORD PTR [rcx+40] + mov r10, r8 + mov rdx, QWORD PTR [rcx+56] + movq xmm4, rax + xor rdx, QWORD PTR [rcx+24] + xor r11, QWORD PTR [rcx+8] + mov rbx, QWORD PTR [rcx+224] + mov rax, QWORD PTR [r9+80] + xor rax, QWORD PTR [r9+64] + movq xmm0, rdx + mov rcx, QWORD PTR [rcx+88] + xor rcx, QWORD PTR [r9+72] + movq xmm3, QWORD PTR [r9+104] + movaps XMMWORD PTR [rsp+64], xmm6 + movaps XMMWORD PTR [rsp+48], xmm7 + movaps XMMWORD PTR [rsp+32], xmm8 + and r10d, 2097136 + movq xmm5, rax + + xor eax, eax + mov QWORD PTR [rsp+16], rax + + mov ax, 1023 + shl rax, 52 + movq xmm8, rax + mov r15, QWORD PTR [r9+96] + punpcklqdq xmm4, xmm0 + movq xmm0, rcx + punpcklqdq xmm5, xmm0 + + ALIGN 64 +$main_loop_ivybridge: + movdqu xmm6, XMMWORD PTR [r10+rbx] + lea rdx, QWORD PTR [r10+rbx] + mov ecx, r10d + mov eax, r10d + mov rdi, r15 + xor ecx, 16 + xor eax, 32 + xor r10d, 48 + movq xmm0, r11 + movq xmm7, r8 + punpcklqdq xmm7, xmm0 + aesenc xmm6, xmm7 + movdqu xmm1, XMMWORD PTR [rax+rbx] + movdqu xmm0, XMMWORD PTR [r10+rbx] + paddq xmm1, xmm7 + movdqu xmm2, XMMWORD PTR [rcx+rbx] + paddq xmm0, xmm5 + paddq xmm2, xmm4 + movdqu XMMWORD PTR [rcx+rbx], xmm0 + movq rcx, xmm3 + movdqu XMMWORD PTR [rax+rbx], xmm2 + mov rax, rcx + movdqu XMMWORD PTR [r10+rbx], xmm1 + shl rax, 32 + xor rdi, rax + movq rbp, xmm6 + movdqa xmm0, xmm6 + pxor xmm0, xmm4 + mov r10, rbp + and r10d, 2097136 + movdqu XMMWORD PTR [rdx], xmm0 + xor rdi, QWORD PTR [r10+rbx] + lea r14, QWORD PTR [r10+rbx] + xor r10d, 32 + mov r12, QWORD PTR [r14+8] + xor edx, edx + lea r9d, DWORD PTR [ecx+ecx] + add r9d, ebp + movdqa xmm0, xmm6 + psrldq xmm0, 8 + or r9d, r13d + movq rax, xmm0 + div r9 + xorps xmm3, xmm3 + mov eax, eax + shl rdx, 32 + add rdx, rax + lea r9, QWORD PTR [rdx+rbp] + mov r15, rdx + mov rax, r9 + shr rax, 12 + movq xmm0, rax + paddq xmm0, xmm8 + sqrtsd xmm3, xmm0 + psubq xmm3, XMMWORD PTR [rsp+16] + movq rdx, xmm3 + test edx, 524287 + je $sqrt_fixup_ivybridge + psrlq xmm3, 19 +$sqrt_fixup_ivybridge_ret: + + mov ecx, r10d + mov rax, rdi + mul rbp + movq xmm2, rdx + xor rdx, [rcx+rbx] + movq xmm0, rax + xor rax, [rcx+rbx+8] + punpcklqdq xmm2, xmm0 + + mov r9d, r10d + xor r9d, 48 + xor r10d, 16 + pxor xmm2, XMMWORD PTR [r9+rbx] + movdqu xmm0, XMMWORD PTR [r10+rbx] + paddq xmm0, xmm5 + movdqu xmm1, XMMWORD PTR [rcx+rbx] + paddq xmm2, xmm4 + paddq xmm1, xmm7 + movdqa xmm5, xmm4 + movdqu XMMWORD PTR [r9+rbx], xmm0 + movdqa xmm4, xmm6 + movdqu XMMWORD PTR [rcx+rbx], xmm2 + movdqu XMMWORD PTR [r10+rbx], xmm1 + add r8, rdx + mov QWORD PTR [r14], r8 + xor r8, rdi + mov r10, r8 + add r11, rax + mov QWORD PTR [r14+8], r11 + and r10d, 2097136 + xor r11, r12 + dec rsi + jne $main_loop_ivybridge + + ldmxcsr DWORD PTR [rsp] + mov rbx, QWORD PTR [rsp+160] + movaps xmm6, XMMWORD PTR [rsp+64] + movaps xmm7, XMMWORD PTR [rsp+48] + movaps xmm8, XMMWORD PTR [rsp+32] + add rsp, 80 + pop r15 + pop r14 + pop r13 + pop r12 + pop rdi + pop rsi + pop rbp + jmp $cnv2_main_loop_ivybridge_endp + +$sqrt_fixup_ivybridge: + dec rdx + mov r13d, -1022 + shl r13, 32 + mov rax, rdx + shr rdx, 19 + shr rax, 20 + mov rcx, rdx + sub rcx, rax + add rax, r13 + not r13 + sub rcx, r13 + mov r13d, -2147483647 + imul rcx, rax + sub rcx, r9 + adc rdx, 0 + movq xmm3, rdx + jmp $sqrt_fixup_ivybridge_ret + +$cnv2_main_loop_ivybridge_endp: diff --git a/src/crypto/asm/cnv2_main_loop_ryzen.inc b/src/crypto/asm/cnv2_main_loop_ryzen.inc new file mode 100644 index 00000000..3294548e --- /dev/null +++ b/src/crypto/asm/cnv2_main_loop_ryzen.inc @@ -0,0 +1,179 @@ + mov QWORD PTR [rsp+16], rbx + mov QWORD PTR [rsp+24], rbp + mov QWORD PTR [rsp+32], rsi + push rdi + push r12 + push r13 + push r14 + push r15 + sub rsp, 64 + + stmxcsr DWORD PTR [rsp] + mov DWORD PTR [rsp+4], 24448 + ldmxcsr DWORD PTR [rsp+4] + + mov rax, QWORD PTR [rcx+48] + mov r9, rcx + xor rax, QWORD PTR [rcx+16] + mov ebp, 524288 + mov r8, QWORD PTR [rcx+32] + xor r8, QWORD PTR [rcx] + mov r11, QWORD PTR [rcx+40] + mov r10, r8 + mov rdx, QWORD PTR [rcx+56] + movq xmm3, rax + xor rdx, QWORD PTR [rcx+24] + xor r11, QWORD PTR [rcx+8] + mov rbx, QWORD PTR [rcx+224] + mov rax, QWORD PTR [r9+80] + xor rax, QWORD PTR [r9+64] + movq xmm0, rdx + mov rcx, QWORD PTR [rcx+88] + xor rcx, QWORD PTR [r9+72] + mov rdi, QWORD PTR [r9+104] + and r10d, 2097136 + movaps XMMWORD PTR [rsp+48], xmm6 + movq xmm4, rax + movaps XMMWORD PTR [rsp+32], xmm7 + movaps XMMWORD PTR [rsp+16], xmm8 + xorps xmm8, xmm8 + mov ax, 1023 + shl rax, 52 + movq xmm7, rax + mov r15, QWORD PTR [r9+96] + punpcklqdq xmm3, xmm0 + movq xmm0, rcx + punpcklqdq xmm4, xmm0 + + ALIGN 64 +$main_loop_ryzen: + movdqa xmm5, XMMWORD PTR [r10+rbx] + movq xmm0, r11 + movq xmm6, r8 + punpcklqdq xmm6, xmm0 + lea rdx, QWORD PTR [r10+rbx] + lea r9, QWORD PTR [rdi+rdi] + shl rdi, 32 + + mov ecx, r10d + mov eax, r10d + xor ecx, 16 + xor eax, 32 + xor r10d, 48 + aesenc xmm5, xmm6 + movdqa xmm2, XMMWORD PTR [rcx+rbx] + movdqa xmm1, XMMWORD PTR [rax+rbx] + movdqa xmm0, XMMWORD PTR [r10+rbx] + paddq xmm2, xmm3 + paddq xmm1, xmm6 + paddq xmm0, xmm4 + movdqa XMMWORD PTR [rcx+rbx], xmm0 + movdqa XMMWORD PTR [rax+rbx], xmm2 + movdqa XMMWORD PTR [r10+rbx], xmm1 + + movaps xmm1, xmm8 + mov rsi, r15 + xor rsi, rdi + movq r14, xmm5 + movdqa xmm0, xmm5 + pxor xmm0, xmm3 + mov r10, r14 + and r10d, 2097136 + movdqa XMMWORD PTR [rdx], xmm0 + xor rsi, QWORD PTR [r10+rbx] + lea r12, QWORD PTR [r10+rbx] + mov r13, QWORD PTR [r10+rbx+8] + + add r9d, r14d + or r9d, -2147483647 + xor edx, edx + movdqa xmm0, xmm5 + psrldq xmm0, 8 + movq rax, xmm0 + + div r9 + movq xmm0, rax + movq xmm1, rdx + punpckldq xmm0, xmm1 + movq r15, xmm0 + paddq xmm0, xmm5 + movdqa xmm2, xmm0 + psrlq xmm0, 12 + paddq xmm0, xmm7 + sqrtsd xmm1, xmm0 + movq rdi, xmm1 + test rdi, 524287 + je $sqrt_fixup_ryzen + shr rdi, 19 + +$sqrt_fixup_ryzen_ret: + mov rax, rsi + mul r14 + movq xmm1, rax + movq xmm0, rdx + punpcklqdq xmm0, xmm1 + + mov r9d, r10d + mov ecx, r10d + xor r9d, 16 + xor ecx, 32 + xor r10d, 48 + movdqa xmm1, XMMWORD PTR [rcx+rbx] + xor rdx, [rcx+rbx] + xor rax, [rcx+rbx+8] + movdqa xmm2, XMMWORD PTR [r9+rbx] + pxor xmm2, xmm0 + paddq xmm4, XMMWORD PTR [r10+rbx] + paddq xmm2, xmm3 + paddq xmm1, xmm6 + movdqa XMMWORD PTR [r9+rbx], xmm4 + movdqa XMMWORD PTR [rcx+rbx], xmm2 + movdqa XMMWORD PTR [r10+rbx], xmm1 + + movdqa xmm4, xmm3 + add r8, rdx + add r11, rax + mov QWORD PTR [r12], r8 + xor r8, rsi + mov QWORD PTR [r12+8], r11 + mov r10, r8 + xor r11, r13 + and r10d, 2097136 + movdqa xmm3, xmm5 + dec ebp + jne $main_loop_ryzen + + ldmxcsr DWORD PTR [rsp] + movaps xmm6, XMMWORD PTR [rsp+48] + lea r11, QWORD PTR [rsp+64] + mov rbx, QWORD PTR [r11+56] + mov rbp, QWORD PTR [r11+64] + mov rsi, QWORD PTR [r11+72] + movaps xmm8, XMMWORD PTR [r11-48] + movaps xmm7, XMMWORD PTR [rsp+32] + mov rsp, r11 + pop r15 + pop r14 + pop r13 + pop r12 + pop rdi + jmp $cnv2_main_loop_ryzen_endp + +$sqrt_fixup_ryzen: + movq r9, xmm2 + dec rdi + mov edx, -1022 + shl rdx, 32 + mov rax, rdi + shr rdi, 19 + shr rax, 20 + mov rcx, rdi + sub rcx, rax + lea rcx, [rcx+rdx+1] + add rax, rdx + imul rcx, rax + sub rcx, r9 + adc rdi, 0 + jmp $sqrt_fixup_ryzen_ret + +$cnv2_main_loop_ryzen_endp: From f163aad38c27190759297b73a80603e0601c3e5a Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 23 Sep 2018 20:45:10 +0300 Subject: [PATCH 356/389] Fix macOS/Clang build. --- src/crypto/asm/cnv2_main_loop.S | 18 ++++++++++++------ src/crypto/asm/cnv2_main_loop_ivybridge.inc | 18 +++++++++--------- src/crypto/asm/cnv2_main_loop_ryzen.inc | 18 +++++++++--------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/crypto/asm/cnv2_main_loop.S b/src/crypto/asm/cnv2_main_loop.S index dc5a82f5..580a4588 100644 --- a/src/crypto/asm/cnv2_main_loop.S +++ b/src/crypto/asm/cnv2_main_loop.S @@ -1,19 +1,25 @@ #define ALIGN .align .intel_syntax noprefix +#ifdef __APPLE__ +# define FN_PREFIX(fn) _ ## fn +.text +#else +# define FN_PREFIX(fn) fn .section .text -.global cnv2_mainloop_ivybridge_asm -.global cnv2_mainloop_ryzen_asm +#endif +.global FN_PREFIX(cnv2_mainloop_ivybridge_asm) +.global FN_PREFIX(cnv2_mainloop_ryzen_asm) -ALIGN 64 -cnv2_mainloop_ivybridge_asm: +ALIGN 16 +FN_PREFIX(cnv2_mainloop_ivybridge_asm): sub rsp, 48 mov rcx, rdi #include "cnv2_main_loop_ivybridge.inc" add rsp, 48 ret 0 -ALIGN 64 -cnv2_mainloop_ryzen_asm: +ALIGN 16 +FN_PREFIX(cnv2_mainloop_ryzen_asm): sub rsp, 48 mov rcx, rdi #include "cnv2_main_loop_ryzen.inc" diff --git a/src/crypto/asm/cnv2_main_loop_ivybridge.inc b/src/crypto/asm/cnv2_main_loop_ivybridge.inc index 0985d1bd..a253a549 100644 --- a/src/crypto/asm/cnv2_main_loop_ivybridge.inc +++ b/src/crypto/asm/cnv2_main_loop_ivybridge.inc @@ -49,8 +49,8 @@ movq xmm0, rcx punpcklqdq xmm5, xmm0 - ALIGN 64 -$main_loop_ivybridge: + ALIGN 16 +main_loop_ivybridge: movdqu xmm6, XMMWORD PTR [r10+rbx] lea rdx, QWORD PTR [r10+rbx] mov ecx, r10d @@ -108,9 +108,9 @@ $main_loop_ivybridge: psubq xmm3, XMMWORD PTR [rsp+16] movq rdx, xmm3 test edx, 524287 - je $sqrt_fixup_ivybridge + je sqrt_fixup_ivybridge psrlq xmm3, 19 -$sqrt_fixup_ivybridge_ret: +sqrt_fixup_ivybridge_ret: mov ecx, r10d mov rax, rdi @@ -144,7 +144,7 @@ $sqrt_fixup_ivybridge_ret: and r10d, 2097136 xor r11, r12 dec rsi - jne $main_loop_ivybridge + jne main_loop_ivybridge ldmxcsr DWORD PTR [rsp] mov rbx, QWORD PTR [rsp+160] @@ -159,9 +159,9 @@ $sqrt_fixup_ivybridge_ret: pop rdi pop rsi pop rbp - jmp $cnv2_main_loop_ivybridge_endp + jmp cnv2_main_loop_ivybridge_endp -$sqrt_fixup_ivybridge: +sqrt_fixup_ivybridge: dec rdx mov r13d, -1022 shl r13, 32 @@ -178,6 +178,6 @@ $sqrt_fixup_ivybridge: sub rcx, r9 adc rdx, 0 movq xmm3, rdx - jmp $sqrt_fixup_ivybridge_ret + jmp sqrt_fixup_ivybridge_ret -$cnv2_main_loop_ivybridge_endp: +cnv2_main_loop_ivybridge_endp: diff --git a/src/crypto/asm/cnv2_main_loop_ryzen.inc b/src/crypto/asm/cnv2_main_loop_ryzen.inc index 3294548e..d386aa2d 100644 --- a/src/crypto/asm/cnv2_main_loop_ryzen.inc +++ b/src/crypto/asm/cnv2_main_loop_ryzen.inc @@ -45,8 +45,8 @@ movq xmm0, rcx punpcklqdq xmm4, xmm0 - ALIGN 64 -$main_loop_ryzen: + ALIGN 16 +main_loop_ryzen: movdqa xmm5, XMMWORD PTR [r10+rbx] movq xmm0, r11 movq xmm6, r8 @@ -103,10 +103,10 @@ $main_loop_ryzen: sqrtsd xmm1, xmm0 movq rdi, xmm1 test rdi, 524287 - je $sqrt_fixup_ryzen + je sqrt_fixup_ryzen shr rdi, 19 -$sqrt_fixup_ryzen_ret: +sqrt_fixup_ryzen_ret: mov rax, rsi mul r14 movq xmm1, rax @@ -141,7 +141,7 @@ $sqrt_fixup_ryzen_ret: and r10d, 2097136 movdqa xmm3, xmm5 dec ebp - jne $main_loop_ryzen + jne main_loop_ryzen ldmxcsr DWORD PTR [rsp] movaps xmm6, XMMWORD PTR [rsp+48] @@ -157,9 +157,9 @@ $sqrt_fixup_ryzen_ret: pop r13 pop r12 pop rdi - jmp $cnv2_main_loop_ryzen_endp + jmp cnv2_main_loop_ryzen_endp -$sqrt_fixup_ryzen: +sqrt_fixup_ryzen: movq r9, xmm2 dec rdi mov edx, -1022 @@ -174,6 +174,6 @@ $sqrt_fixup_ryzen: imul rcx, rax sub rcx, r9 adc rdi, 0 - jmp $sqrt_fixup_ryzen_ret + jmp sqrt_fixup_ryzen_ret -$cnv2_main_loop_ryzen_endp: +cnv2_main_loop_ryzen_endp: From ba65a34a017e6009ea8960aca3ae23df7443e0ff Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 24 Sep 2018 09:51:21 +0300 Subject: [PATCH 357/389] Initial ASM wrapper. --- CMakeLists.txt | 2 +- cmake/asm.cmake | 1 - src/crypto/CryptoNight.h | 10 +++--- src/crypto/CryptoNight_x86.h | 25 ++++++++++++++ src/crypto/asm/cnv2_main_loop_ivybridge.inc | 37 +++++++++++---------- src/workers/CpuThread.cpp | 5 ++- src/workers/Worker.h | 7 ++-- 7 files changed, 58 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 22bf9a78..1b3fe6f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -256,4 +256,4 @@ if (WITH_DEBUG_LOG) endif() add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES} ${TLS_SOURCES}) -target_link_libraries(${PROJECT_NAME} ${${XMRIG_ASM_LIBRARY}} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) +target_link_libraries(${PROJECT_NAME} ${XMRIG_ASM_LIBRARY} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) diff --git a/cmake/asm.cmake b/cmake/asm.cmake index 02093810..3a0bc894 100644 --- a/cmake/asm.cmake +++ b/cmake/asm.cmake @@ -17,7 +17,6 @@ if (WITH_ASM AND NOT XMRIG_ARM) set_property(TARGET ${XMRIG_ASM_LIBRARY} PROPERTY LINKER_LANGUAGE C) else() -# set(XMRIG_ASM_SOURCES "") set(XMRIG_ASM_LIBRARY "") add_definitions(/DXMRIG_NO_ASM) endif() diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index e8e86dc4..680f1740 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -22,8 +22,8 @@ * along with this program. If not, see . */ -#ifndef __CRYPTONIGHT_H__ -#define __CRYPTONIGHT_H__ +#ifndef XMRIG_CRYPTONIGHT_H +#define XMRIG_CRYPTONIGHT_H #include @@ -31,9 +31,9 @@ struct cryptonight_ctx { - alignas(16) uint8_t state[200]; - alignas(16) uint8_t* memory; + alignas(16) uint8_t state[224]; + alignas(16) uint8_t *memory; }; -#endif /* __CRYPTONIGHT_H__ */ +#endif /* XMRIG_CRYPTONIGHT_H */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 1cb06687..064dbdc2 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -561,6 +561,31 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } +extern "C" void cnv2_mainloop_ivybridge_asm(cryptonight_ctx *ctx); +extern "C" void cnv2_mainloop_ryzen_asm(cryptonight_ctx *ctx); + + +template +inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) +{ + constexpr size_t MEM = xmrig::cn_select_memory(); + + xmrig::keccak(input, size, ctx[0]->state); + cn_explode_scratchpad((__m128i*) ctx[0]->state, (__m128i*) ctx[0]->memory); + + if (ASM == xmrig::ASM_INTEL) { + cnv2_mainloop_ivybridge_asm(ctx[0]); + } + else { + cnv2_mainloop_ryzen_asm(ctx[0]); + } + + cn_implode_scratchpad((__m128i*) ctx[0]->memory, (__m128i*) ctx[0]->state); + xmrig::keccakf(reinterpret_cast(ctx[0]->state), 24); + extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); +} + + template inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { diff --git a/src/crypto/asm/cnv2_main_loop_ivybridge.inc b/src/crypto/asm/cnv2_main_loop_ivybridge.inc index a253a549..8c2c2d3b 100644 --- a/src/crypto/asm/cnv2_main_loop_ivybridge.inc +++ b/src/crypto/asm/cnv2_main_loop_ivybridge.inc @@ -48,10 +48,10 @@ punpcklqdq xmm4, xmm0 movq xmm0, rcx punpcklqdq xmm5, xmm0 + movdqu xmm6, XMMWORD PTR [r10+rbx] ALIGN 16 main_loop_ivybridge: - movdqu xmm6, XMMWORD PTR [r10+rbx] lea rdx, QWORD PTR [r10+rbx] mov ecx, r10d mov eax, r10d @@ -63,28 +63,29 @@ main_loop_ivybridge: movq xmm7, r8 punpcklqdq xmm7, xmm0 aesenc xmm6, xmm7 + movq rbp, xmm6 + mov r9, rbp + and r9d, 2097136 + movdqu xmm2, XMMWORD PTR [rcx+rbx] movdqu xmm1, XMMWORD PTR [rax+rbx] movdqu xmm0, XMMWORD PTR [r10+rbx] paddq xmm1, xmm7 - movdqu xmm2, XMMWORD PTR [rcx+rbx] paddq xmm0, xmm5 paddq xmm2, xmm4 movdqu XMMWORD PTR [rcx+rbx], xmm0 - movq rcx, xmm3 movdqu XMMWORD PTR [rax+rbx], xmm2 - mov rax, rcx movdqu XMMWORD PTR [r10+rbx], xmm1 + mov r10, r9 + xor r10d, 32 + movq rcx, xmm3 + mov rax, rcx shl rax, 32 xor rdi, rax - movq rbp, xmm6 movdqa xmm0, xmm6 pxor xmm0, xmm4 - mov r10, rbp - and r10d, 2097136 movdqu XMMWORD PTR [rdx], xmm0 - xor rdi, QWORD PTR [r10+rbx] - lea r14, QWORD PTR [r10+rbx] - xor r10d, 32 + xor rdi, QWORD PTR [r9+rbx] + lea r14, QWORD PTR [r9+rbx] mov r12, QWORD PTR [r14+8] xor edx, edx lea r9d, DWORD PTR [ecx+ecx] @@ -117,8 +118,15 @@ sqrt_fixup_ivybridge_ret: mul rbp movq xmm2, rdx xor rdx, [rcx+rbx] + add r8, rdx + mov QWORD PTR [r14], r8 + xor r8, rdi + mov edi, r8d + and edi, 2097136 movq xmm0, rax xor rax, [rcx+rbx+8] + add r11, rax + mov QWORD PTR [r14+8], r11 punpcklqdq xmm2, xmm0 mov r9d, r10d @@ -135,13 +143,8 @@ sqrt_fixup_ivybridge_ret: movdqa xmm4, xmm6 movdqu XMMWORD PTR [rcx+rbx], xmm2 movdqu XMMWORD PTR [r10+rbx], xmm1 - add r8, rdx - mov QWORD PTR [r14], r8 - xor r8, rdi - mov r10, r8 - add r11, rax - mov QWORD PTR [r14+8], r11 - and r10d, 2097136 + movdqu xmm6, [rdi+rbx] + mov r10d, edi xor r11, r12 dec rsi jne main_loop_ivybridge diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index ca7681f0..d9d60f51 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -65,7 +65,8 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a { assert(variant >= VARIANT_0 && variant < VARIANT_MAX); - static const cn_hash_fun func_table[VARIANT_MAX * 10 * 3] = { + constexpr const size_t count = VARIANT_MAX * 10 * 3; + static const cn_hash_fun func_table[count + 2] = { cryptonight_single_hash, cryptonight_double_hash, cryptonight_single_hash, @@ -242,6 +243,8 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif + cryptonight_single_hash_asm, + cryptonight_single_hash_asm }; const size_t index = VARIANT_MAX * 10 * algorithm + 10 * variant + av - 1; diff --git a/src/workers/Worker.h b/src/workers/Worker.h index aad9e3c5..73e25033 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __WORKER_H__ -#define __WORKER_H__ +#ifndef XMRIG_WORKER_H +#define XMRIG_WORKER_H #include @@ -33,7 +33,6 @@ #include "Mem.h" -struct cryptonight_ctx; class Handle; @@ -67,4 +66,4 @@ protected: }; -#endif /* __WORKER_H__ */ +#endif /* XMRIG_WORKER_H */ From c2fcf2385526223e804dc729847d7534223fd293 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 24 Sep 2018 14:19:26 +0300 Subject: [PATCH 358/389] Implemented "asm" option. --- CMakeLists.txt | 2 +- cmake/asm.cmake | 2 + src/Summary.cpp | 29 +++++++++ src/common/interfaces/IConfig.h | 1 + src/common/xmrig.h | 3 +- src/core/Config.cpp | 21 +++++-- src/core/Config.h | 9 +-- src/core/ConfigLoader_platform.h | 2 + src/core/cpu/AdvancedCpuInfo.cpp | 2 +- src/crypto/Asm.cpp | 100 +++++++++++++++++++++++++++++++ src/crypto/Asm.h | 50 ++++++++++++++++ src/crypto/CryptoNight_x86.h | 2 + src/workers/CpuThread.cpp | 71 +++++++++++++++++----- src/workers/CpuThread.h | 15 +++-- 14 files changed, 275 insertions(+), 34 deletions(-) create mode 100644 src/crypto/Asm.cpp create mode 100644 src/crypto/Asm.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b3fe6f2..1becac5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -255,5 +255,5 @@ if (WITH_DEBUG_LOG) add_definitions(/DAPP_DEBUG) endif() -add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES} ${TLS_SOURCES}) +add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES} ${TLS_SOURCES} ${XMRIG_ASM_SOURCES}) target_link_libraries(${PROJECT_NAME} ${XMRIG_ASM_LIBRARY} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) diff --git a/cmake/asm.cmake b/cmake/asm.cmake index 3a0bc894..a9b76ffd 100644 --- a/cmake/asm.cmake +++ b/cmake/asm.cmake @@ -15,8 +15,10 @@ if (WITH_ASM AND NOT XMRIG_ARM) ) endif() + set(XMRIG_ASM_SOURCES src/crypto/Asm.h src/crypto/Asm.cpp) set_property(TARGET ${XMRIG_ASM_LIBRARY} PROPERTY LINKER_LANGUAGE C) else() + set(XMRIG_ASM_SOURCES "") set(XMRIG_ASM_LIBRARY "") add_definitions(/DXMRIG_NO_ASM) endif() diff --git a/src/Summary.cpp b/src/Summary.cpp index ba220e5b..3c1d06a7 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -32,11 +32,28 @@ #include "common/net/Pool.h" #include "core/Config.h" #include "core/Controller.h" +#include "crypto/Asm.h" #include "Mem.h" #include "Summary.h" #include "version.h" +#ifndef XMRIG_NO_ASM +static const char *coloredAsmNames[] = { + "\x1B[1;31mnone\x1B[0m", + "auto", + "\x1B[1;32mintel\x1B[0m", + "\x1B[1;32mryzen\x1B[0m" +}; + + +inline static const char *asmName(xmrig::Assembly assembly, bool colors) +{ + return colors ? coloredAsmNames[assembly] : xmrig::Asm::toString(assembly); +} +#endif + + static void print_memory(xmrig::Config *config) { # ifdef _WIN32 if (config->isColors()) { @@ -101,6 +118,18 @@ static void print_threads(xmrig::Config *config) config->isColors() && config->donateLevel() == 0 ? "\x1B[1;31m" : "", config->donateLevel()); } + +# ifndef XMRIG_NO_ASM + if (config->assembly() == xmrig::ASM_AUTO) { + const xmrig::Assembly assembly = xmrig::Cpu::info()->assembly(); + + Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13sauto:%s") + : " * %-13sauto:%s", "ASSEMBLY", asmName(assembly, config->isColors())); + } + else { + Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s") : " * %-13s%s", "ASSEMBLY", asmName(config->assembly(), config->isColors())); + } +# endif } diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index d3593163..0fcac2d1 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -80,6 +80,7 @@ public: SafeKey = 1005, ThreadsKey = 't', HardwareAESKey = 1011, + AssemblyKey = 1015, // xmrig amd OclPlatformKey = 1400, diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 820bc4fb..52650f0d 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -98,7 +98,8 @@ enum Assembly { ASM_NONE, ASM_AUTO, ASM_INTEL, - ASM_RYZEN + ASM_RYZEN, + ASM_MAX }; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index d99bfb09..20a3aece 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -30,6 +30,7 @@ #include "common/cpu/Cpu.h" #include "core/Config.h" #include "core/ConfigCreator.h" +#include "crypto/Asm.h" #include "crypto/CryptoNight_constants.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" @@ -43,6 +44,7 @@ static char affinity_tmp[20] = { 0 }; xmrig::Config::Config() : xmrig::CommonConfig(), m_aesMode(AES_AUTO), m_algoVariant(AV_AUTO), + m_assembly(ASM_AUTO), m_hugePages(true), m_safe(false), m_maxCpuUsage(75), @@ -51,11 +53,6 @@ xmrig::Config::Config() : xmrig::CommonConfig(), } -xmrig::Config::~Config() -{ -} - - bool xmrig::Config::reload(const char *json) { return xmrig::ConfigLoader::reload(this, json); @@ -178,7 +175,7 @@ bool xmrig::Config::finalize() } for (size_t i = 0; i < m_threads.count; ++i) { - m_threads.list.push_back(CpuThread::createFromAV(i, m_algorithm.algo(), av, m_threads.mask, m_priority)); + m_threads.list.push_back(CpuThread::createFromAV(i, m_algorithm.algo(), av, m_threads.mask, m_priority, m_assembly)); } return true; @@ -204,6 +201,12 @@ bool xmrig::Config::parseBoolean(int key, bool enable) m_aesMode = enable ? AES_HW : AES_SOFT; break; +# ifndef XMRIG_NO_ASM + case AssemblyKey: + m_assembly = Asm::parse(enable); + break; +# endif + default: break; } @@ -244,6 +247,12 @@ bool xmrig::Config::parseString(int key, const char *arg) return parseUint64(key, p ? strtoull(p, nullptr, 16) : strtoull(arg, nullptr, 10)); } +# ifndef XMRIG_NO_ASM + case AssemblyKey: /* --asm */ + m_assembly = Asm::parse(arg); + break; +# endif + default: break; } diff --git a/src/core/Config.h b/src/core/Config.h index f0f1404f..95afc34c 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __CONFIG_H__ -#define __CONFIG_H__ +#ifndef XMRIG_CONFIG_H +#define XMRIG_CONFIG_H #include @@ -69,7 +69,6 @@ public: Config(); - ~Config(); bool reload(const char *json); @@ -77,6 +76,7 @@ public: inline AesMode aesMode() const { return m_aesMode; } inline AlgoVariant algoVariant() const { return m_algoVariant; } + inline Assembly assembly() const { return m_assembly; } inline bool isHugePages() const { return m_hugePages; } inline const std::vector &threads() const { return m_threads.list; } inline int priority() const { return m_priority; } @@ -116,6 +116,7 @@ private: AesMode m_aesMode; AlgoVariant m_algoVariant; + Assembly m_assembly; bool m_hugePages; bool m_safe; int m_maxCpuUsage; @@ -126,4 +127,4 @@ private: } /* namespace xmrig */ -#endif /* __CONFIG_H__ */ +#endif /* XMRIG_CONFIG_H */ diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index c034f3e7..3b95a90f 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -135,6 +135,7 @@ static struct option const options[] = { { "tls", 0, nullptr, xmrig::IConfig::TlsKey }, { "tls-fingerprint", 1, nullptr, xmrig::IConfig::FingerprintKey }, { "version", 0, nullptr, xmrig::IConfig::VersionKey }, + { "asm", 1, nullptr, xmrig::IConfig::AssemblyKey }, { nullptr, 0, nullptr, 0 } }; @@ -159,6 +160,7 @@ static struct option const config_options[] = { { "threads", 1, nullptr, xmrig::IConfig::ThreadsKey }, { "user-agent", 1, nullptr, xmrig::IConfig::UserAgentKey }, { "hw-aes", 0, nullptr, xmrig::IConfig::HardwareAESKey }, + { "asm", 1, nullptr, xmrig::IConfig::AssemblyKey }, { nullptr, 0, nullptr, 0 } }; diff --git a/src/core/cpu/AdvancedCpuInfo.cpp b/src/core/cpu/AdvancedCpuInfo.cpp index ac5508c3..1f86a420 100644 --- a/src/core/cpu/AdvancedCpuInfo.cpp +++ b/src/core/cpu/AdvancedCpuInfo.cpp @@ -47,7 +47,7 @@ xmrig::AdvancedCpuInfo::AdvancedCpuInfo() : cpuid_get_raw_data(&raw); cpu_identify(&raw, &data); - strncpy(m_brand, data.brand_str, sizeof(m_brand) - 1); + strncpy(m_brand, data.brand_str, sizeof(m_brand)); m_sockets = threads() / data.num_logical_cpus; if (m_sockets == 0) { diff --git a/src/crypto/Asm.cpp b/src/crypto/Asm.cpp new file mode 100644 index 00000000..79dd1cc9 --- /dev/null +++ b/src/crypto/Asm.cpp @@ -0,0 +1,100 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include + + +#ifdef _MSC_VER +# define strncasecmp _strnicmp +# define strcasecmp _stricmp +#endif + + +#include "crypto/Asm.h" +#include "rapidjson/document.h" + + +static const char *asmNames[] = { + "none", + "auto", + "intel", + "ryzen" +}; + + +xmrig::Assembly xmrig::Asm::parse(const char *assembly, Assembly defaultValue) +{ + constexpr size_t const size = sizeof(asmNames) / sizeof((asmNames)[0]); + assert(assembly != nullptr); + assert(ASM_MAX == size); + + if (assembly == nullptr) { + return defaultValue; + } + + for (size_t i = 0; i < size; i++) { + if (strcasecmp(assembly, asmNames[i]) == 0) { + return static_cast(i); + } + } + + return defaultValue; +} + + +xmrig::Assembly xmrig::Asm::parse(const rapidjson::Value &value, Assembly defaultValue) +{ + if (value.IsBool()) { + return parse(value.IsBool()); + } + + if (value.IsString()) { + return parse(value.GetString(), defaultValue); + } + + return defaultValue; +} + + +const char *xmrig::Asm::toString(Assembly assembly) +{ + return asmNames[assembly]; +} + + +rapidjson::Value xmrig::Asm::toJSON(Assembly assembly) +{ + using namespace rapidjson; + + if (assembly == ASM_NONE) { + return Value(false); + } + + if (assembly == ASM_AUTO) { + return Value(true); + } + + return Value(StringRef(toString(assembly))); +} diff --git a/src/crypto/Asm.h b/src/crypto/Asm.h new file mode 100644 index 00000000..3b755fd6 --- /dev/null +++ b/src/crypto/Asm.h @@ -0,0 +1,50 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ASM_H +#define XMRIG_ASM_H + + +#include "common/xmrig.h" +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class Asm +{ +public: + static Assembly parse(const char *assembly, Assembly defaultValue = ASM_AUTO); + static Assembly parse(const rapidjson::Value &value, Assembly defaultValue = ASM_AUTO); + static const char *toString(Assembly assembly); + static rapidjson::Value toJSON(Assembly assembly); + + inline static Assembly parse(bool enable) { return enable ? ASM_AUTO : ASM_NONE; } +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_ASM_H */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 064dbdc2..42ea37b5 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -561,6 +561,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } +#ifndef XMRIG_NO_ASM extern "C" void cnv2_mainloop_ivybridge_asm(cryptonight_ctx *ctx); extern "C" void cnv2_mainloop_ryzen_asm(cryptonight_ctx *ctx); @@ -584,6 +585,7 @@ inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_ xmrig::keccakf(reinterpret_cast(ctx[0]->state), 24); extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); } +#endif template diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index d9d60f51..ff6be585 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -24,8 +24,10 @@ #include +#include "common/cpu/Cpu.h" #include "common/log/Log.h" #include "common/net/Pool.h" +#include "crypto/Asm.h" #include "rapidjson/document.h" #include "workers/CpuThread.h" @@ -37,9 +39,10 @@ #endif -xmrig::CpuThread::CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch) : +xmrig::CpuThread::CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch, Assembly assembly) : m_algorithm(algorithm), m_av(av), + m_assembly(assembly), m_prefetch(prefetch), m_softAES(softAES), m_priority(priority), @@ -50,23 +53,23 @@ xmrig::CpuThread::CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiw } -xmrig::CpuThread::~CpuThread() -{ -} - - bool xmrig::CpuThread::isSoftAES(AlgoVariant av) { return av == AV_SINGLE_SOFT || av == AV_DOUBLE_SOFT || av > AV_PENTA; } -xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant av, Variant variant) +xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant av, Variant variant, Assembly assembly) { assert(variant >= VARIANT_0 && variant < VARIANT_MAX); +# ifndef XMRIG_NO_ASM + constexpr const size_t count = VARIANT_MAX * 10 * 3 + 2; +# else constexpr const size_t count = VARIANT_MAX * 10 * 3; - static const cn_hash_fun func_table[count + 2] = { +# endif + + static const cn_hash_fun func_table[count] = { cryptonight_single_hash, cryptonight_double_hash, cryptonight_single_hash, @@ -243,13 +246,14 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, # endif +# ifndef XMRIG_NO_ASM cryptonight_single_hash_asm, cryptonight_single_hash_asm +# endif }; - const size_t index = VARIANT_MAX * 10 * algorithm + 10 * variant + av - 1; - # ifndef NDEBUG + const size_t index = fnIndex(algorithm, av, variant, assembly); cn_hash_fun func = func_table[index]; assert(index < sizeof(func_table) / sizeof(func_table[0])); @@ -257,12 +261,12 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a return func; # else - return func_table[index]; + return func_table[fnIndex(algorithm, av, variant, assembly)]; # endif } -xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority) +xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority, Assembly assembly) { assert(av > AV_AUTO && av < AV_MAX); @@ -285,7 +289,7 @@ xmrig::CpuThread *xmrig::CpuThread::createFromAV(size_t index, Algo algorithm, A } } - return new CpuThread(index, algorithm, av, multiway(av), cpuId, priority, isSoftAES(av), false); + return new CpuThread(index, algorithm, av, multiway(av), cpuId, priority, isSoftAES(av), false, assembly); } @@ -303,7 +307,7 @@ xmrig::CpuThread *xmrig::CpuThread::createFromData(size_t index, Algo algorithm, assert(av > AV_AUTO && av < AV_MAX); - return new CpuThread(index, algorithm, static_cast(av), multiway, data.affinity, priority, softAES, false); + return new CpuThread(index, algorithm, static_cast(av), multiway, data.affinity, priority, softAES, false, data.assembly); } @@ -325,11 +329,14 @@ xmrig::CpuThread::Data xmrig::CpuThread::parse(const rapidjson::Value &object) } const auto &affinity = object["affine_to_cpu"]; - if (affinity.IsUint64()) { data.affinity = affinity.GetInt64(); } +# ifndef XMRIG_NO_ASM + data.assembly = Asm::parse(object["asm"]); +# endif + return data; } @@ -371,7 +378,11 @@ void xmrig::CpuThread::print() const LOG_DEBUG(GREEN_BOLD("CPU thread: ") " index " WHITE_BOLD("%zu") ", multiway " WHITE_BOLD("%d") ", av " WHITE_BOLD("%d") ",", index(), static_cast(multiway()), static_cast(m_av)); +# ifndef XMRIG_NO_ASM + LOG_DEBUG(" assembly: %s, affine_to_cpu: %" PRId64, Asm::toString(m_assembly), affinity()); +# else LOG_DEBUG(" affine_to_cpu: %" PRId64, affinity()); +# endif } #endif @@ -406,5 +417,35 @@ rapidjson::Value xmrig::CpuThread::toConfig(rapidjson::Document &doc) const obj.AddMember("low_power_mode", multiway(), allocator); obj.AddMember("affine_to_cpu", affinity() == -1L ? Value(kFalseType) : Value(affinity()), allocator); +# ifndef XMRIG_NO_ASM + obj.AddMember("asm", Asm::toJSON(m_assembly), allocator); +# endif + return obj; } + + +size_t xmrig::CpuThread::fnIndex(Algo algorithm, AlgoVariant av, Variant variant, Assembly assembly) +{ + const size_t index = VARIANT_MAX * 10 * algorithm + 10 * variant + av - 1; + +# ifndef XMRIG_NO_ASM + if (assembly == ASM_AUTO) { + assembly = Cpu::info()->assembly(); + } + + if (assembly == ASM_NONE) { + return index; + } + + constexpr const size_t offset = VARIANT_MAX * 10 * 3; + + if (algorithm == CRYPTONIGHT && variant == VARIANT_2) { + if (av == AV_SINGLE) { + return offset + assembly - 2; + } + } +# endif + + return index; +} diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index 622dc3a2..29ab9696 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -40,7 +40,7 @@ class CpuThread : public IThread public: struct Data { - inline Data() : valid(false), affinity(-1L), multiway(SingleWay) {} + inline Data() : assembly(ASM_AUTO), valid(false), affinity(-1L), multiway(SingleWay) {} inline void setMultiway(int value) { @@ -50,27 +50,27 @@ public: } } + Assembly assembly; bool valid; int64_t affinity; Multiway multiway; }; - CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch); - ~CpuThread(); + CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch, Assembly assembly); typedef void (*cn_hash_fun)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx **ctx); static bool isSoftAES(AlgoVariant av); - static cn_hash_fun fn(Algo algorithm, AlgoVariant av, Variant variant); - static CpuThread *createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority); + static cn_hash_fun fn(Algo algorithm, AlgoVariant av, Variant variant, Assembly assembly); + static CpuThread *createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority, Assembly assembly); static CpuThread *createFromData(size_t index, Algo algorithm, const CpuThread::Data &data, int priority, bool softAES); static Data parse(const rapidjson::Value &object); static Multiway multiway(AlgoVariant av); inline bool isPrefetch() const { return m_prefetch; } inline bool isSoftAES() const { return m_softAES; } - inline cn_hash_fun fn(Variant variant) const { return fn(m_algorithm, m_av, variant); } + inline cn_hash_fun fn(Variant variant) const { return fn(m_algorithm, m_av, variant, m_assembly); } inline Algo algorithm() const override { return m_algorithm; } inline int priority() const override { return m_priority; } @@ -91,8 +91,11 @@ protected: rapidjson::Value toConfig(rapidjson::Document &doc) const override; private: + static size_t fnIndex(Algo algorithm, AlgoVariant av, Variant variant, Assembly assembly); + const Algo m_algorithm; const AlgoVariant m_av; + const Assembly m_assembly; const bool m_prefetch; const bool m_softAES; const int m_priority; From f4a867b70f04d76bb05cda6a49f0699c9f1827c1 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 24 Sep 2018 14:57:30 +0300 Subject: [PATCH 359/389] Fix 32bit build. --- cmake/asm.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/asm.cmake b/cmake/asm.cmake index a9b76ffd..d6c88e99 100644 --- a/cmake/asm.cmake +++ b/cmake/asm.cmake @@ -1,4 +1,4 @@ -if (WITH_ASM AND NOT XMRIG_ARM) +if (WITH_ASM AND NOT XMRIG_ARM AND CMAKE_SIZEOF_VOID_P EQUAL 8) set(XMRIG_ASM_LIBRARY "xmrig-asm") if (CMAKE_CXX_COMPILER_ID MATCHES MSVC) From ebcdac7d1365ffa29f7a7f7af14e8a19a3deb0d1 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 24 Sep 2018 20:43:31 +0300 Subject: [PATCH 360/389] Fixed crash when use ASM code for MSYS2, thanks @SChernykh. --- cmake/asm.cmake | 21 ++++++++++++--------- src/crypto/asm/cnv2_main_loop_win.S | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 src/crypto/asm/cnv2_main_loop_win.S diff --git a/cmake/asm.cmake b/cmake/asm.cmake index d6c88e99..cb50f0d9 100644 --- a/cmake/asm.cmake +++ b/cmake/asm.cmake @@ -1,20 +1,23 @@ if (WITH_ASM AND NOT XMRIG_ARM AND CMAKE_SIZEOF_VOID_P EQUAL 8) set(XMRIG_ASM_LIBRARY "xmrig-asm") - if (CMAKE_CXX_COMPILER_ID MATCHES MSVC) + if (CMAKE_C_COMPILER_ID MATCHES MSVC) enable_language(ASM_MASM) - set_property(SOURCE "src/crypto/asm/cnv2_main_loop.asm" PROPERTY ASM_MASM) - add_library(${XMRIG_ASM_LIBRARY} STATIC - "src/crypto/asm/cnv2_main_loop.asm" - ) + set(XMRIG_ASM_FILE "src/crypto/asm/cnv2_main_loop.asm") + set_property(SOURCE ${XMRIG_ASM_FILE} PROPERTY ASM_MASM) else() enable_language(ASM) - set_property(SOURCE "src/crypto/asm/cnv2_main_loop.S" PROPERTY C) - add_library(${XMRIG_ASM_LIBRARY} STATIC - "src/crypto/asm/cnv2_main_loop.S" - ) + + if (WIN32 AND CMAKE_C_COMPILER_ID MATCHES GNU) + set(XMRIG_ASM_FILE "src/crypto/asm/cnv2_main_loop_win.S") + else() + set(XMRIG_ASM_FILE "src/crypto/asm/cnv2_main_loop.S") + endif() + + set_property(SOURCE ${XMRIG_ASM_FILE} PROPERTY C) endif() + add_library(${XMRIG_ASM_LIBRARY} STATIC ${XMRIG_ASM_FILE}) set(XMRIG_ASM_SOURCES src/crypto/Asm.h src/crypto/Asm.cpp) set_property(TARGET ${XMRIG_ASM_LIBRARY} PROPERTY LINKER_LANGUAGE C) else() diff --git a/src/crypto/asm/cnv2_main_loop_win.S b/src/crypto/asm/cnv2_main_loop_win.S new file mode 100644 index 00000000..3c2028b6 --- /dev/null +++ b/src/crypto/asm/cnv2_main_loop_win.S @@ -0,0 +1,15 @@ +#define ALIGN .align +.intel_syntax noprefix +.section .text +.global cnv2_mainloop_ivybridge_asm +.global cnv2_mainloop_ryzen_asm + +ALIGN 16 +cnv2_mainloop_ivybridge_asm: + #include "cnv2_main_loop_ivybridge.inc" + ret 0 + +ALIGN 16 +cnv2_mainloop_ryzen_asm: + #include "cnv2_main_loop_ryzen.inc" + ret 0 From 1a3de050968449bed800a192a8eba4cebb0fc31b Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 25 Sep 2018 09:25:47 +0300 Subject: [PATCH 361/389] Added ASM code for double hash mode, thanks @SChernykh. --- src/crypto/CryptoNight_x86.h | 29 +- .../asm/cnv2_double_main_loop_sandybridge.inc | 410 ++++++++++++++++++ src/crypto/asm/cnv2_main_loop.S | 10 + src/crypto/asm/cnv2_main_loop.asm | 7 + src/crypto/asm/cnv2_main_loop_win.S | 6 + src/workers/CpuThread.cpp | 9 +- 6 files changed, 467 insertions(+), 4 deletions(-) create mode 100644 src/crypto/asm/cnv2_double_main_loop_sandybridge.inc diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 42ea37b5..b1a72324 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -564,6 +564,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si #ifndef XMRIG_NO_ASM extern "C" void cnv2_mainloop_ivybridge_asm(cryptonight_ctx *ctx); extern "C" void cnv2_mainloop_ryzen_asm(cryptonight_ctx *ctx); +extern "C" void cnv2_double_mainloop_sandybridge_asm(cryptonight_ctx* ctx0, cryptonight_ctx* ctx1); template @@ -572,7 +573,7 @@ inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_ constexpr size_t MEM = xmrig::cn_select_memory(); xmrig::keccak(input, size, ctx[0]->state); - cn_explode_scratchpad((__m128i*) ctx[0]->state, (__m128i*) ctx[0]->memory); + cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[0]->state), reinterpret_cast<__m128i*>(ctx[0]->memory)); if (ASM == xmrig::ASM_INTEL) { cnv2_mainloop_ivybridge_asm(ctx[0]); @@ -581,10 +582,34 @@ inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_ cnv2_mainloop_ryzen_asm(ctx[0]); } - cn_implode_scratchpad((__m128i*) ctx[0]->memory, (__m128i*) ctx[0]->state); + cn_implode_scratchpad(reinterpret_cast<__m128i*>(ctx[0]->memory), reinterpret_cast<__m128i*>(ctx[0]->state)); xmrig::keccakf(reinterpret_cast(ctx[0]->state), 24); extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); } + + +template +inline void cryptonight_double_hash_asm(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) +{ + constexpr size_t MEM = xmrig::cn_select_memory(); + + xmrig::keccak(input, size, ctx[0]->state); + xmrig::keccak(input + size, size, ctx[1]->state); + + cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[0]->state), reinterpret_cast<__m128i*>(ctx[0]->memory)); + cn_explode_scratchpad(reinterpret_cast<__m128i*>(ctx[1]->state), reinterpret_cast<__m128i*>(ctx[1]->memory)); + + cnv2_double_mainloop_sandybridge_asm(ctx[0], ctx[1]); + + cn_implode_scratchpad(reinterpret_cast<__m128i*>(ctx[0]->memory), reinterpret_cast<__m128i*>(ctx[0]->state)); + cn_implode_scratchpad(reinterpret_cast<__m128i*>(ctx[1]->memory), reinterpret_cast<__m128i*>(ctx[1]->state)); + + xmrig::keccakf(reinterpret_cast(ctx[0]->state), 24); + xmrig::keccakf(reinterpret_cast(ctx[1]->state), 24); + + extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); + extra_hashes[ctx[1]->state[0] & 3](ctx[1]->state, 200, output + 32); +} #endif diff --git a/src/crypto/asm/cnv2_double_main_loop_sandybridge.inc b/src/crypto/asm/cnv2_double_main_loop_sandybridge.inc new file mode 100644 index 00000000..e8251bc7 --- /dev/null +++ b/src/crypto/asm/cnv2_double_main_loop_sandybridge.inc @@ -0,0 +1,410 @@ + mov rax, rsp + push rbx + push rbp + push rsi + push rdi + push r12 + push r13 + push r14 + push r15 + sub rsp, 184 + + stmxcsr DWORD PTR [rsp+272] + mov DWORD PTR [rsp+276], 24448 + ldmxcsr DWORD PTR [rsp+276] + + mov r13, QWORD PTR [rcx+224] + mov r9, rdx + mov r10, QWORD PTR [rcx+32] + mov r8, rcx + xor r10, QWORD PTR [rcx] + mov r14d, 524288 + mov r11, QWORD PTR [rcx+40] + xor r11, QWORD PTR [rcx+8] + mov rsi, QWORD PTR [rdx+224] + mov rdx, QWORD PTR [rcx+56] + xor rdx, QWORD PTR [rcx+24] + mov rdi, QWORD PTR [r9+32] + xor rdi, QWORD PTR [r9] + mov rbp, QWORD PTR [r9+40] + xor rbp, QWORD PTR [r9+8] + movq xmm0, rdx + movaps XMMWORD PTR [rax-88], xmm6 + movaps XMMWORD PTR [rax-104], xmm7 + movaps XMMWORD PTR [rax-120], xmm8 + movaps XMMWORD PTR [rsp+112], xmm9 + movaps XMMWORD PTR [rsp+96], xmm10 + movaps XMMWORD PTR [rsp+80], xmm11 + movaps XMMWORD PTR [rsp+64], xmm12 + movaps XMMWORD PTR [rsp+48], xmm13 + movaps XMMWORD PTR [rsp+32], xmm14 + movaps XMMWORD PTR [rsp+16], xmm15 + mov rdx, r10 + movq xmm4, QWORD PTR [r8+96] + and edx, 2097136 + mov rax, QWORD PTR [rcx+48] + xorps xmm13, xmm13 + xor rax, QWORD PTR [rcx+16] + mov rcx, QWORD PTR [rcx+88] + xor rcx, QWORD PTR [r8+72] + movq xmm5, QWORD PTR [r8+104] + movq xmm7, rax + + mov eax, 1 + shl rax, 52 + movq xmm14, rax + punpcklqdq xmm14, xmm14 + + mov eax, 1023 + shl rax, 52 + movq xmm12, rax + punpcklqdq xmm12, xmm12 + + mov rax, QWORD PTR [r8+80] + xor rax, QWORD PTR [r8+64] + punpcklqdq xmm7, xmm0 + movq xmm0, rcx + mov rcx, QWORD PTR [r9+56] + xor rcx, QWORD PTR [r9+24] + movq xmm3, rax + mov rax, QWORD PTR [r9+48] + xor rax, QWORD PTR [r9+16] + punpcklqdq xmm3, xmm0 + movq xmm0, rcx + mov QWORD PTR [rsp], r13 + mov rcx, QWORD PTR [r9+88] + xor rcx, QWORD PTR [r9+72] + movq xmm6, rax + mov rax, QWORD PTR [r9+80] + xor rax, QWORD PTR [r9+64] + punpcklqdq xmm6, xmm0 + movq xmm0, rcx + mov QWORD PTR [rsp+256], r10 + mov rcx, rdi + mov QWORD PTR [rsp+264], r11 + movq xmm8, rax + and ecx, 2097136 + punpcklqdq xmm8, xmm0 + movq xmm0, QWORD PTR [r9+96] + punpcklqdq xmm4, xmm0 + movq xmm0, QWORD PTR [r9+104] + lea r8, QWORD PTR [rcx+rsi] + movdqu xmm11, XMMWORD PTR [r8] + punpcklqdq xmm5, xmm0 + lea r9, QWORD PTR [rdx+r13] + movdqu xmm15, XMMWORD PTR [r9] + + ALIGN 16 +main_loop_double_sandybridge: + movdqu xmm9, xmm15 + mov eax, edx + mov ebx, edx + xor eax, 16 + xor ebx, 32 + xor edx, 48 + + movq xmm0, r11 + movq xmm2, r10 + punpcklqdq xmm2, xmm0 + aesenc xmm9, xmm2 + + movdqu xmm0, XMMWORD PTR [rax+r13] + movdqu xmm1, XMMWORD PTR [rbx+r13] + paddq xmm0, xmm7 + paddq xmm1, xmm2 + movdqu XMMWORD PTR [rbx+r13], xmm0 + movdqu xmm0, XMMWORD PTR [rdx+r13] + movdqu XMMWORD PTR [rdx+r13], xmm1 + paddq xmm0, xmm3 + movdqu XMMWORD PTR [rax+r13], xmm0 + + movq r11, xmm9 + mov edx, r11d + and edx, 2097136 + movdqa xmm0, xmm9 + pxor xmm0, xmm7 + movdqu XMMWORD PTR [r9], xmm0 + + lea rbx, QWORD PTR [rdx+r13] + mov r10, QWORD PTR [rdx+r13] + + movdqu xmm10, xmm11 + movq xmm0, rbp + movq xmm11, rdi + punpcklqdq xmm11, xmm0 + aesenc xmm10, xmm11 + + mov eax, ecx + mov r12d, ecx + xor eax, 16 + xor r12d, 32 + xor ecx, 48 + + movdqu xmm0, XMMWORD PTR [rax+rsi] + paddq xmm0, xmm6 + movdqu xmm1, XMMWORD PTR [r12+rsi] + movdqu XMMWORD PTR [r12+rsi], xmm0 + paddq xmm1, xmm11 + movdqu xmm0, XMMWORD PTR [rcx+rsi] + movdqu XMMWORD PTR [rcx+rsi], xmm1 + paddq xmm0, xmm8 + movdqu XMMWORD PTR [rax+rsi], xmm0 + + movq rcx, xmm10 + and ecx, 2097136 + + movdqa xmm0, xmm10 + pxor xmm0, xmm6 + movdqu XMMWORD PTR [r8], xmm0 + mov r12, QWORD PTR [rcx+rsi] + + mov r9, QWORD PTR [rbx+8] + + xor edx, 16 + mov r8d, edx + mov r15d, edx + + movq rdx, xmm5 + shl rdx, 32 + movq rax, xmm4 + xor rdx, rax + xor r10, rdx + mov rax, r10 + mul r11 + mov r11d, r8d + xor r11d, 48 + movq xmm0, rdx + xor rdx, [r11+r13] + movq xmm1, rax + xor rax, [r11+r13+8] + punpcklqdq xmm0, xmm1 + + pxor xmm0, XMMWORD PTR [r8+r13] + xor r8d, 32 + movdqu xmm1, XMMWORD PTR [r11+r13] + paddq xmm0, xmm7 + paddq xmm1, xmm2 + movdqu XMMWORD PTR [r11+r13], xmm0 + movdqu xmm0, XMMWORD PTR [r8+r13] + movdqu XMMWORD PTR [r8+r13], xmm1 + paddq xmm0, xmm3 + movdqu XMMWORD PTR [r15+r13], xmm0 + + mov r11, QWORD PTR [rsp+256] + add r11, rdx + mov rdx, QWORD PTR [rsp+264] + add rdx, rax + mov QWORD PTR [rbx], r11 + xor r11, r10 + mov QWORD PTR [rbx+8], rdx + xor rdx, r9 + mov QWORD PTR [rsp+256], r11 + and r11d, 2097136 + mov QWORD PTR [rsp+264], rdx + mov QWORD PTR [rsp+8], r11 + lea r15, QWORD PTR [r11+r13] + movdqu xmm15, XMMWORD PTR [r11+r13] + lea r13, QWORD PTR [rsi+rcx] + movdqa xmm0, xmm5 + psrldq xmm0, 8 + movaps xmm2, xmm13 + movq r10, xmm0 + psllq xmm5, 1 + shl r10, 32 + movdqa xmm0, xmm9 + psrldq xmm0, 8 + movdqa xmm1, xmm10 + movq r11, xmm0 + psrldq xmm1, 8 + movq r8, xmm1 + psrldq xmm4, 8 + movaps xmm0, xmm13 + movq rax, xmm4 + xor r10, rax + movaps xmm1, xmm13 + xor r10, r12 + lea rax, QWORD PTR [r11+1] + shr rax, 1 + movdqa xmm3, xmm9 + punpcklqdq xmm3, xmm10 + paddq xmm5, xmm3 + movq rdx, xmm5 + psrldq xmm5, 8 + cvtsi2sd xmm2, rax + or edx, -2147483647 + lea rax, QWORD PTR [r8+1] + shr rax, 1 + movq r9, xmm5 + cvtsi2sd xmm0, rax + or r9d, -2147483647 + cvtsi2sd xmm1, rdx + unpcklpd xmm2, xmm0 + movaps xmm0, xmm13 + cvtsi2sd xmm0, r9 + unpcklpd xmm1, xmm0 + divpd xmm2, xmm1 + paddq xmm2, xmm14 + cvttsd2si rax, xmm2 + psrldq xmm2, 8 + mov rbx, rax + imul rax, rdx + sub r11, rax + js div_fix_1_sandybridge +div_fix_1_ret_sandybridge: + + cvttsd2si rdx, xmm2 + mov rax, rdx + imul rax, r9 + movd xmm2, r11d + movd xmm4, ebx + sub r8, rax + js div_fix_2_sandybridge +div_fix_2_ret_sandybridge: + + movd xmm1, r8d + movd xmm0, edx + punpckldq xmm2, xmm1 + punpckldq xmm4, xmm0 + punpckldq xmm4, xmm2 + paddq xmm3, xmm4 + movdqa xmm0, xmm3 + psrlq xmm0, 12 + paddq xmm0, xmm12 + sqrtpd xmm1, xmm0 + movq r9, xmm1 + movdqa xmm5, xmm1 + psrlq xmm5, 19 + test r9, 524287 + je sqrt_fix_1_sandybridge +sqrt_fix_1_ret_sandybridge: + + movq r9, xmm10 + psrldq xmm1, 8 + movq r8, xmm1 + test r8, 524287 + je sqrt_fix_2_sandybridge +sqrt_fix_2_ret_sandybridge: + + mov r12d, ecx + mov r8d, ecx + xor r12d, 16 + xor r8d, 32 + xor ecx, 48 + mov rax, r10 + mul r9 + movq xmm0, rax + movq xmm3, rdx + punpcklqdq xmm3, xmm0 + + movdqu xmm0, XMMWORD PTR [r12+rsi] + pxor xmm0, xmm3 + movdqu xmm1, XMMWORD PTR [r8+rsi] + xor rdx, [r8+rsi] + xor rax, [r8+rsi+8] + movdqu xmm3, XMMWORD PTR [rcx+rsi] + paddq xmm0, xmm6 + paddq xmm1, xmm11 + paddq xmm3, xmm8 + movdqu XMMWORD PTR [r8+rsi], xmm0 + movdqu XMMWORD PTR [rcx+rsi], xmm1 + movdqu XMMWORD PTR [r12+rsi], xmm3 + + add rdi, rdx + mov QWORD PTR [r13], rdi + xor rdi, r10 + mov ecx, edi + and ecx, 2097136 + lea r8, QWORD PTR [rcx+rsi] + + mov rdx, QWORD PTR [r13+8] + add rbp, rax + mov QWORD PTR [r13+8], rbp + movdqu xmm11, XMMWORD PTR [rcx+rsi] + xor rbp, rdx + mov r13, QWORD PTR [rsp] + movdqa xmm3, xmm7 + mov rdx, QWORD PTR [rsp+8] + movdqa xmm8, xmm6 + mov r10, QWORD PTR [rsp+256] + movdqa xmm7, xmm9 + mov r11, QWORD PTR [rsp+264] + movdqa xmm6, xmm10 + mov r9, r15 + dec r14d + jne main_loop_double_sandybridge + + ldmxcsr DWORD PTR [rsp+272] + movaps xmm13, XMMWORD PTR [rsp+48] + lea r11, QWORD PTR [rsp+184] + movaps xmm6, XMMWORD PTR [r11-24] + movaps xmm7, XMMWORD PTR [r11-40] + movaps xmm8, XMMWORD PTR [r11-56] + movaps xmm9, XMMWORD PTR [r11-72] + movaps xmm10, XMMWORD PTR [r11-88] + movaps xmm11, XMMWORD PTR [r11-104] + movaps xmm12, XMMWORD PTR [r11-120] + movaps xmm14, XMMWORD PTR [rsp+32] + movaps xmm15, XMMWORD PTR [rsp+16] + mov rsp, r11 + pop r15 + pop r14 + pop r13 + pop r12 + pop rdi + pop rsi + pop rbp + pop rbx + jmp cnv2_double_mainloop_asm_sandybridge_endp + +div_fix_1_sandybridge: + dec rbx + add r11, rdx + jmp div_fix_1_ret_sandybridge + +div_fix_2_sandybridge: + dec rdx + add r8, r9 + jmp div_fix_2_ret_sandybridge + +sqrt_fix_1_sandybridge: + movq r8, xmm3 + movdqa xmm0, xmm5 + psrldq xmm0, 8 + dec r9 + mov r11d, -1022 + shl r11, 32 + mov rax, r9 + shr r9, 19 + shr rax, 20 + mov rdx, r9 + sub rdx, rax + lea rdx, [rdx+r11+1] + add rax, r11 + imul rdx, rax + sub rdx, r8 + adc r9, 0 + movq xmm5, r9 + punpcklqdq xmm5, xmm0 + jmp sqrt_fix_1_ret_sandybridge + +sqrt_fix_2_sandybridge: + psrldq xmm3, 8 + movq r11, xmm3 + dec r8 + mov ebx, -1022 + shl rbx, 32 + mov rax, r8 + shr r8, 19 + shr rax, 20 + mov rdx, r8 + sub rdx, rax + lea rdx, [rdx+rbx+1] + add rax, rbx + imul rdx, rax + sub rdx, r11 + adc r8, 0 + movq xmm0, r8 + punpcklqdq xmm5, xmm0 + jmp sqrt_fix_2_ret_sandybridge + +cnv2_double_mainloop_asm_sandybridge_endp: diff --git a/src/crypto/asm/cnv2_main_loop.S b/src/crypto/asm/cnv2_main_loop.S index 580a4588..4dbcbbda 100644 --- a/src/crypto/asm/cnv2_main_loop.S +++ b/src/crypto/asm/cnv2_main_loop.S @@ -9,6 +9,7 @@ #endif .global FN_PREFIX(cnv2_mainloop_ivybridge_asm) .global FN_PREFIX(cnv2_mainloop_ryzen_asm) +.global FN_PREFIX(cnv2_double_mainloop_sandybridge_asm) ALIGN 16 FN_PREFIX(cnv2_mainloop_ivybridge_asm): @@ -25,3 +26,12 @@ FN_PREFIX(cnv2_mainloop_ryzen_asm): #include "cnv2_main_loop_ryzen.inc" add rsp, 48 ret 0 + +ALIGN 16 +FN_PREFIX(cnv2_double_mainloop_sandybridge_asm): + sub rsp, 48 + mov rcx, rdi + mov rdx, rsi + #include "cnv2_double_main_loop_sandybridge.inc" + add rsp, 48 + ret 0 diff --git a/src/crypto/asm/cnv2_main_loop.asm b/src/crypto/asm/cnv2_main_loop.asm index 7ec895c4..d9522267 100644 --- a/src/crypto/asm/cnv2_main_loop.asm +++ b/src/crypto/asm/cnv2_main_loop.asm @@ -1,6 +1,7 @@ _TEXT_CNV2_MAINLOOP SEGMENT PAGE READ EXECUTE PUBLIC cnv2_mainloop_ivybridge_asm PUBLIC cnv2_mainloop_ryzen_asm +PUBLIC cnv2_double_mainloop_sandybridge_asm ALIGN 64 cnv2_mainloop_ivybridge_asm PROC @@ -14,5 +15,11 @@ cnv2_mainloop_ryzen_asm PROC ret 0 cnv2_mainloop_ryzen_asm ENDP +ALIGN 64 +cnv2_double_mainloop_sandybridge_asm PROC + INCLUDE cnv2_double_main_loop_sandybridge.inc + ret 0 +cnv2_double_mainloop_sandybridge_asm ENDP + _TEXT_CNV2_MAINLOOP ENDS END diff --git a/src/crypto/asm/cnv2_main_loop_win.S b/src/crypto/asm/cnv2_main_loop_win.S index 3c2028b6..f06e4fa4 100644 --- a/src/crypto/asm/cnv2_main_loop_win.S +++ b/src/crypto/asm/cnv2_main_loop_win.S @@ -3,6 +3,7 @@ .section .text .global cnv2_mainloop_ivybridge_asm .global cnv2_mainloop_ryzen_asm +.global cnv2_double_mainloop_sandybridge_asm ALIGN 16 cnv2_mainloop_ivybridge_asm: @@ -13,3 +14,8 @@ ALIGN 16 cnv2_mainloop_ryzen_asm: #include "cnv2_main_loop_ryzen.inc" ret 0 + +ALIGN 16 +cnv2_double_mainloop_sandybridge_asm: + #include "cnv2_double_main_loop_sandybridge.inc" + ret 0 diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index ff6be585..4b528148 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -64,7 +64,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a assert(variant >= VARIANT_0 && variant < VARIANT_MAX); # ifndef XMRIG_NO_ASM - constexpr const size_t count = VARIANT_MAX * 10 * 3 + 2; + constexpr const size_t count = VARIANT_MAX * 10 * 3 + 3; # else constexpr const size_t count = VARIANT_MAX * 10 * 3; # endif @@ -248,7 +248,8 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a # endif # ifndef XMRIG_NO_ASM cryptonight_single_hash_asm, - cryptonight_single_hash_asm + cryptonight_single_hash_asm, + cryptonight_double_hash_asm # endif }; @@ -444,6 +445,10 @@ size_t xmrig::CpuThread::fnIndex(Algo algorithm, AlgoVariant av, Variant variant if (av == AV_SINGLE) { return offset + assembly - 2; } + + if (av == AV_DOUBLE) { + return offset + 2; + } } # endif From 0d9db273a098fd58075a1388652201cd940fb2b8 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 25 Sep 2018 09:40:09 +0300 Subject: [PATCH 362/389] Fix template declaration. --- src/crypto/CryptoNight_x86.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index b1a72324..8dcdd414 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -588,7 +588,7 @@ inline void cryptonight_single_hash_asm(const uint8_t *__restrict__ input, size_ } -template +template inline void cryptonight_double_hash_asm(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MEM = xmrig::cn_select_memory(); From 18dc19b1e8eefaa519e7b06f73bbf214aabdfb73 Mon Sep 17 00:00:00 2001 From: xmrig Date: Tue, 25 Sep 2018 10:15:41 +0300 Subject: [PATCH 363/389] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e1e2412..c75025e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # v2.8.0 - **[#753](https://github.com/xmrig/xmrig/issues/753) Added new algorithm [CryptoNight variant 2](https://github.com/xmrig/xmrig/issues/753) for Monero fork, thanks [@SChernykh](https://github.com/SChernykh).** + - Added global and per thread option `"asm"` and and command line equivalent. - [#758](https://github.com/xmrig/xmrig/issues/758) **Added SSL/TLS support for secure connections to pools.** + - Added per pool options `"tls"` and `"tls-fingerprint"` and command line equivalents. - [#245](https://github.com/xmrig/xmrig-proxy/issues/245) Fixed API ID collision when run multiple miners on same machine. - [#757](https://github.com/xmrig/xmrig/issues/757) Fixed send buffer overflow. From 03b4e160ec6a52589d1f9b3f859eea012325872d Mon Sep 17 00:00:00 2001 From: xmrig Date: Tue, 25 Sep 2018 10:16:29 +0300 Subject: [PATCH 364/389] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c75025e2..bd3f23fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # v2.8.0 - **[#753](https://github.com/xmrig/xmrig/issues/753) Added new algorithm [CryptoNight variant 2](https://github.com/xmrig/xmrig/issues/753) for Monero fork, thanks [@SChernykh](https://github.com/SChernykh).** - Added global and per thread option `"asm"` and and command line equivalent. -- [#758](https://github.com/xmrig/xmrig/issues/758) **Added SSL/TLS support for secure connections to pools.** +- **[#758](https://github.com/xmrig/xmrig/issues/758) Added SSL/TLS support for secure connections to pools.** - Added per pool options `"tls"` and `"tls-fingerprint"` and command line equivalents. - [#245](https://github.com/xmrig/xmrig-proxy/issues/245) Fixed API ID collision when run multiple miners on same machine. - [#757](https://github.com/xmrig/xmrig/issues/757) Fixed send buffer overflow. From 0d197f8906bce4fc945229d714037907e7eb674e Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 26 Sep 2018 20:35:27 +0300 Subject: [PATCH 365/389] New donations proxy addresses with SSL/TLS support. --- src/net/strategies/DonateStrategy.cpp | 20 +++++--------------- src/net/strategies/DonateStrategy.h | 6 +++--- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 395c53bb..6fc90842 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -33,10 +33,6 @@ #include "net/strategies/DonateStrategy.h" -const static char *kDonatePool1 = "miner.fee.xmrig.com"; -const static char *kDonatePool2 = "emergency.fee.xmrig.com"; - - static inline float randomf(float min, float max) { return (max - min) * ((((float) rand()) / (float) RAND_MAX)) + min; } @@ -55,17 +51,11 @@ DonateStrategy::DonateStrategy(int level, const char *user, xmrig::Algo algo, IS xmrig::keccak(reinterpret_cast(user), strlen(user), hash); Job::toHex(hash, 32, userId); - if (algo == xmrig::CRYPTONIGHT) { - m_pools.push_back(Pool(kDonatePool1, 6666, userId, nullptr, false, true)); - m_pools.push_back(Pool(kDonatePool1, 80, userId, nullptr, false, true)); - m_pools.push_back(Pool(kDonatePool2, 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); - } - else if (algo == xmrig::CRYPTONIGHT_HEAVY) { - m_pools.push_back(Pool(kDonatePool1, 8888, userId, nullptr, false, true)); - } - else { - m_pools.push_back(Pool(kDonatePool1, 5555, userId, nullptr, false, true)); - } +# ifndef XMRIG_NO_TLS + m_pools.push_back(Pool("donate.ssl.xmrig.com", 443, userId, nullptr, false, true, true)); +# endif + + m_pools.push_back(Pool("donate.v2.xmrig.com", 3333, userId, nullptr, false, true)); for (Pool &pool : m_pools) { pool.adjust(xmrig::Algorithm(algo, xmrig::VARIANT_AUTO)); diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 95ff6608..e75e41a4 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __DONATESTRATEGY_H__ -#define __DONATESTRATEGY_H__ +#ifndef XMRIG_DONATESTRATEGY_H +#define XMRIG_DONATESTRATEGY_H #include @@ -76,4 +76,4 @@ private: uv_timer_t m_timer; }; -#endif /* __DONATESTRATEGY_H__ */ +#endif /* XMRIG_DONATESTRATEGY_H */ From a63677e255672ea73f556d16a3492395531279ad Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 27 Sep 2018 17:17:19 +0300 Subject: [PATCH 366/389] Fix missing "asm" in generated config. --- src/core/Config.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 20a3aece..79b19265 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -69,6 +69,10 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("algo", StringRef(algorithm().name()), allocator); +# ifndef XMRIG_NO_ASM + doc.AddMember("asm", Asm::toJSON(m_assembly), allocator); +# endif + Value api(kObjectType); api.AddMember("port", apiPort(), allocator); api.AddMember("access-token", apiToken() ? Value(StringRef(apiToken())).Move() : Value(kNullType).Move(), allocator); From 143da8380ed8052d2a41fc35264b0deadc1777e0 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 27 Sep 2018 19:07:04 +0300 Subject: [PATCH 367/389] Add "autosave" config option. --- src/common/config/CommonConfig.cpp | 9 +++++++-- src/common/config/CommonConfig.h | 2 ++ src/common/interfaces/IConfig.h | 1 + src/config.json | 13 +++++++++---- src/core/Config.cpp | 17 ++++++++++------- src/core/Config.h | 2 ++ src/core/ConfigLoader_platform.h | 1 + src/workers/Workers.cpp | 4 ++++ 8 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index b70d5e3d..beb2d0c9 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -67,6 +67,7 @@ xmrig::CommonConfig::CommonConfig() : m_adjusted(false), m_apiIPv6(false), m_apiRestricted(true), + m_autoSave(true), m_background(false), m_colors(true), m_dryRun(false), @@ -217,7 +218,7 @@ bool xmrig::CommonConfig::save() rapidjson::PrettyWriter writer(os); doc.Accept(writer); - fclose(fp); + fflush(fp); uv_fs_close(uv_default_loop(), &req, fd, nullptr); uv_fs_req_cleanup(&req); @@ -308,10 +309,14 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable) m_apiRestricted = enable; break; - case IConfig::DryRunKey: /* --dry-run */ + case DryRunKey: /* --dry-run */ m_dryRun = enable; break; + case AutoSaveKey: + m_autoSave = enable; + break; + default: break; } diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 7643a1a5..422a6bb2 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -44,6 +44,7 @@ public: inline bool isApiIPv6() const { return m_apiIPv6; } inline bool isApiRestricted() const { return m_apiRestricted; } + inline bool isAutoSave() const { return m_autoSave; } inline bool isBackground() const { return m_background; } inline bool isColors() const { return m_colors; } inline bool isDryRun() const { return m_dryRun; } @@ -88,6 +89,7 @@ protected: bool m_adjusted; bool m_apiIPv6; bool m_apiRestricted; + bool m_autoSave; bool m_background; bool m_colors; bool m_dryRun; diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index 0fcac2d1..69f2ffab 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -65,6 +65,7 @@ public: WatchKey = 1105, TlsKey = 1013, FingerprintKey = 1014, + AutoSaveKey = 1016, // xmrig common CPUPriorityKey = 1021, diff --git a/src/config.json b/src/config.json index b2dad4c9..d4fb42f3 100644 --- a/src/config.json +++ b/src/config.json @@ -3,10 +3,13 @@ "api": { "port": 0, "access-token": null, + "id": null, "worker-id": null, "ipv6": false, - "restricted": true + "restricted": false }, + "asm": true, + "autosave": true, "av": 0, "background": false, "colors": true, @@ -19,13 +22,15 @@ "max-cpu-usage": 75, "pools": [ { - "url": "proxy.fee.xmrig.com:9999", - "user": "YOUR_WALLET", + "url": "donate.v2.xmrig.com:3333", + "user": "YOUR_WALLET_ADDRESS", "pass": "x", "rig-id": null, "nicehash": false, "keepalive": false, - "variant": 1 + "variant": 1, + "tls": false, + "tls-fingerprint": null } ], "print-time": 60, diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 79b19265..c8891c77 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -47,6 +47,7 @@ xmrig::Config::Config() : xmrig::CommonConfig(), m_assembly(ASM_AUTO), m_hugePages(true), m_safe(false), + m_shouldSave(false), m_maxCpuUsage(75), m_priority(-1) { @@ -69,10 +70,6 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("algo", StringRef(algorithm().name()), allocator); -# ifndef XMRIG_NO_ASM - doc.AddMember("asm", Asm::toJSON(m_assembly), allocator); -# endif - Value api(kObjectType); api.AddMember("port", apiPort(), allocator); api.AddMember("access-token", apiToken() ? Value(StringRef(apiToken())).Move() : Value(kNullType).Move(), allocator); @@ -82,6 +79,11 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const api.AddMember("restricted", isApiRestricted(), allocator); doc.AddMember("api", api, allocator); +# ifndef XMRIG_NO_ASM + doc.AddMember("asm", Asm::toJSON(m_assembly), allocator); +# endif + + doc.AddMember("autosave", isAutoSave(), allocator); doc.AddMember("av", algoVariant(), allocator); doc.AddMember("background", isBackground(), allocator); doc.AddMember("colors", isColors(), allocator); @@ -113,7 +115,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("retry-pause", retryPause(), allocator); doc.AddMember("safe", m_safe, allocator); - if (threadsMode() == Advanced) { + if (threadsMode() != Simple) { Value threads(kArrayType); for (const IThread *thread : m_threads.list) { @@ -123,7 +125,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("threads", threads, allocator); } else { - doc.AddMember("threads", threadsMode() == Automatic ? Value(kNullType) : Value(threadsCount()), allocator); + doc.AddMember("threads", threadsCount(), allocator); } doc.AddMember("user-agent", userAgent() ? Value(StringRef(userAgent())).Move() : Value(kNullType).Move(), allocator); @@ -163,7 +165,7 @@ bool xmrig::Config::finalize() return true; } - const AlgoVariant av = getAlgoVariant(); + const AlgoVariant av = getAlgoVariant(); m_threads.mode = m_threads.count ? Simple : Automatic; const size_t size = CpuThread::multiway(av) * cn_select_memory(m_algorithm.algo()) / 1024; @@ -182,6 +184,7 @@ bool xmrig::Config::finalize() m_threads.list.push_back(CpuThread::createFromAV(i, m_algorithm.algo(), av, m_threads.mask, m_priority, m_assembly)); } + m_shouldSave = m_threads.mode == Automatic; return true; } diff --git a/src/core/Config.h b/src/core/Config.h index 95afc34c..eb33ee14 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -78,6 +78,7 @@ public: inline AlgoVariant algoVariant() const { return m_algoVariant; } inline Assembly assembly() const { return m_assembly; } inline bool isHugePages() const { return m_hugePages; } + inline bool isShouldSave() const { return m_shouldSave && isAutoSave(); } inline const std::vector &threads() const { return m_threads.list; } inline int priority() const { return m_priority; } inline int threadsCount() const { return m_threads.list.size(); } @@ -119,6 +120,7 @@ private: Assembly m_assembly; bool m_hugePages; bool m_safe; + bool m_shouldSave; int m_maxCpuUsage; int m_priority; Threads m_threads; diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index 3b95a90f..a932b235 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -161,6 +161,7 @@ static struct option const config_options[] = { { "user-agent", 1, nullptr, xmrig::IConfig::UserAgentKey }, { "hw-aes", 0, nullptr, xmrig::IConfig::HardwareAESKey }, { "asm", 1, nullptr, xmrig::IConfig::AssemblyKey }, + { "autosave", 0, nullptr, xmrig::IConfig::AutoSaveKey }, { nullptr, 0, nullptr, 0 } }; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 5deb14f7..a5109e9b 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -200,6 +200,10 @@ void Workers::start(xmrig::Controller *controller) m_workers.push_back(handle); handle->start(Workers::onReady); } + + if (controller->config()->isShouldSave()) { + controller->config()->save(); + } } From 52e871b810dcb06fce50baa21d3c30fd8712a496 Mon Sep 17 00:00:00 2001 From: xmrig Date: Fri, 28 Sep 2018 12:36:14 +0300 Subject: [PATCH 368/389] Update README.md --- README.md | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6054b9f8..48f13522 100644 --- a/README.md +++ b/README.md @@ -87,11 +87,22 @@ Use [config.xmrig.com](https://config.xmrig.com/xmrig) to generate, edit or shar Also you can use configuration via config file, default **config.json**. You can load multiple config files and combine it with command line options. ## Algorithm variations -Since version 0.8.0. -* `--av=1` For CPUs with hardware AES. -* `--av=2` Lower power mode (double hash) of `1`. -* `--av=3` Software AES implementation. -* `--av=4` Lower power mode (double hash) of `3`. + +- `av` option used for automatic and simple threads mode (when you specify only threads count). +- For [advanced threads mode](https://github.com/xmrig/xmrig/issues/563) each thread configured individually and `av` option not used. + +| av | Hashes per round | Hardware AES | +|----|------------------|--------------| +| 1 | 1 (Single) | yes | +| 2 | 2 (Double) | yes | +| 3 | 1 (Single) | no | +| 4 | 2 (Double) | no | +| 5 | 3 (Triple) | yes | +| 6 | 4 (Quard) | yes | +| 7 | 5 (Penta) | yes | +| 8 | 3 (Triple) | no | +| 9 | 4 (Quard) | no | +| 10 | 5 (Penta) | no | ## Common Issues ### HUGE PAGES unavailable From 782a91f7e615a73a72b95c9e711623fb0d6ef3e9 Mon Sep 17 00:00:00 2001 From: xmrig Date: Fri, 28 Sep 2018 12:37:21 +0300 Subject: [PATCH 369/389] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 48f13522..6e39b5f7 100644 --- a/README.md +++ b/README.md @@ -111,8 +111,7 @@ Also you can use configuration via config file, default **config.json**. You can ## Other information * No HTTP support, only stratum protocol support. -* No TLS support. -* Default donation 5% (5 minutes in 100 minutes) can be reduced to 1% via command line option `--donate-level`. +* Default donation 5% (5 minutes in 100 minutes) can be reduced to 1% via option `donate-level`. ### CPU mining performance From fbba3d1be62d65117871ad488f69716e0bd9cbc1 Mon Sep 17 00:00:00 2001 From: Roboto12 <43680809+Roboto12@users.noreply.github.com> Date: Fri, 28 Sep 2018 19:18:37 +0300 Subject: [PATCH 370/389] Update Asm.cpp parse(value.IsBool()) should be changed to parse(value.GetBool()), otherwise if the 'asm' parameter in config has a boolean value, it will be parse as 'true' even if it's actually 'false'. --- src/crypto/Asm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/Asm.cpp b/src/crypto/Asm.cpp index 79dd1cc9..48c1beb8 100644 --- a/src/crypto/Asm.cpp +++ b/src/crypto/Asm.cpp @@ -67,7 +67,7 @@ xmrig::Assembly xmrig::Asm::parse(const char *assembly, Assembly defaultValue) xmrig::Assembly xmrig::Asm::parse(const rapidjson::Value &value, Assembly defaultValue) { if (value.IsBool()) { - return parse(value.IsBool()); + return parse(value.GetBool()); } if (value.IsString()) { From 9a173ce91efdc2c0678e72fc5427d47b7aeabf02 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 29 Sep 2018 18:05:38 +0300 Subject: [PATCH 371/389] Fix default value for "restricted" option. --- src/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.json b/src/config.json index d4fb42f3..2670d3a3 100644 --- a/src/config.json +++ b/src/config.json @@ -6,7 +6,7 @@ "id": null, "worker-id": null, "ipv6": false, - "restricted": false + "restricted": true }, "asm": true, "autosave": true, From fa38d97192fbbc9ef589f215946c4516997ecf85 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 30 Sep 2018 16:20:20 +0300 Subject: [PATCH 372/389] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd3f23fb..c5849269 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Added global and per thread option `"asm"` and and command line equivalent. - **[#758](https://github.com/xmrig/xmrig/issues/758) Added SSL/TLS support for secure connections to pools.** - Added per pool options `"tls"` and `"tls-fingerprint"` and command line equivalents. +- [#767](https://github.com/xmrig/xmrig/issues/767) Added config autosave feature, same with GPU miners. - [#245](https://github.com/xmrig/xmrig-proxy/issues/245) Fixed API ID collision when run multiple miners on same machine. - [#757](https://github.com/xmrig/xmrig/issues/757) Fixed send buffer overflow. From cb87c244c346b53c491ac188c4accdb209bf910d Mon Sep 17 00:00:00 2001 From: xmrig Date: Sun, 30 Sep 2018 17:44:10 +0300 Subject: [PATCH 373/389] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6e39b5f7..77b8f394 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # XMRig -:warning: **If you mine Monero, Aeon, Sumokoin, Turtlecoin, Stellite, GRAFT, Haven Protocol, IPBC, [PLEASE READ](https://github.com/xmrig/xmrig/issues/482)!** :warning: +:warning: **[Monero will change PoW algorithm on October 18](https://github.com/xmrig/xmrig/issues/753), all miners and proxy should be updated to v2.8+** :warning: [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub release](https://img.shields.io/github/release/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) From dc67352ac5222d6fa6d794ee289a9c653e6cbe70 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 30 Sep 2018 17:57:25 +0300 Subject: [PATCH 374/389] Update default config.json. --- src/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.json b/src/config.json index 2670d3a3..ae85b1f7 100644 --- a/src/config.json +++ b/src/config.json @@ -28,7 +28,7 @@ "rig-id": null, "nicehash": false, "keepalive": false, - "variant": 1, + "variant": -1, "tls": false, "tls-fingerprint": null } From dc6d6bd53973deacc7c43ef22c50561ae1eb37c8 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 30 Sep 2018 20:06:46 +0300 Subject: [PATCH 375/389] v2.8.0-rc --- src/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index 2a1720ea..6e5bf153 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.8.0-dev" +#define APP_VERSION "2.8.0-rc" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" From a33fbe32c674b45dcb3a75a7aebad086bad472d9 Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 1 Oct 2018 13:36:03 +0300 Subject: [PATCH 376/389] Update README.md --- README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 77b8f394..81b1baea 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # XMRig -:warning: **[Monero will change PoW algorithm on October 18](https://github.com/xmrig/xmrig/issues/753), all miners and proxy should be updated to v2.8+** :warning: +:warning: **[Monero will change PoW algorithm on October 18](https://github.com/xmrig/xmrig/issues/753), all miners and proxy should be updated to [v2.8+](https://github.com/xmrig/xmrig/releases/tag/v2.8.0-rc)** :warning: [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub release](https://img.shields.io/github/release/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) @@ -134,12 +134,17 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -34d390a499d2098bce92e6b85b4858ee6255a7e2d4e03197ba4f6a759efe349c xmrig-2.6.4-xenial-amd64.tar.gz/xmrig-2.6.4/xmrig -cb6792c092c14f0f25d5774049a0adec403877a4564956220dcd9ba0fc488c82 xmrig-2.6.4-gcc-win32.zip/xmrig.exe -cb3c5619a8391f989c6a69135d890c3126eda9841b9dc591d44f02078a6fd49b xmrig-2.6.4-gcc-win64.zip/xmrig.exe -ea2e92bb10d0482880f8d389b7915948e11f672ca8559b0901d8a8fa8e9d733e xmrig-2.6.4-msvc-win64.zip/xmrig.exe +ea6a71732937e06d5434b863bedd2d627c500e8ce30b30a02054015bb3aae3fc xmrig-2.8.0-xenial-amd64.tar.gz/xmrig-2.8.0/xmrig +a067de09a2d49c39317d6a98e420fcd80ef040c8cecb6b21317cbe4eabbdb995 xmrig-2.8.0-xenial-amd64.tar.gz/xmrig-2.8.0/xmrig-notls +89b7b4616faec76c40dab714046ea08ebcd6c558299d17d44639e6bc62c54186 xmrig-2.8.0-gcc-win32.zip/xmrig.exe +0fae870bef4223905b24bb00283b3bba7ece547a09a540033beb9a2a33335124 xmrig-2.8.0-gcc-win32.zip/xmrig-notls.exe +953df29ef354d541b89a70a36ddf16d8a9d5f2c37419d3aacd1352ff6ff539e8 xmrig-2.8.0-gcc-win64.zip/xmrig.exe +84d73422a43b46879d88f87a492e99c81d5d632eb3be79793fc0b895196f131d xmrig-2.8.0-gcc-win64.zip/xmrig-notls.exe +8e1302fe632249a713d7af0b7f3f50ad6e707d71d00fcd1f3be56abc024f98ac xmrig-2.8.0-msvc-win64.zip/xmrig.exe +0042b5e7b14a4f8aca748a4c7656a0ec6705bc593c26f9987a25b10fa471f004 xmrig-2.8.0-msvc-win64.zip/xmrig-notls.exe ``` ## Contacts * support@xmrig.com * [reddit](https://www.reddit.com/user/XMRig/) +* [twitter](https://twitter.com/xmrig_dev) From 9a70f0e564af29f5806e50a5a7dcc18d84016f69 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 2 Oct 2018 01:22:13 +0300 Subject: [PATCH 377/389] v2.8.1-dev --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index 6e5bf153..3c65f25b 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.8.0-rc" +#define APP_VERSION "2.8.1-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 8 -#define APP_VER_PATCH 0 +#define APP_VER_PATCH 1 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From c2f6c70044e7bf485e9aafd92b6e28a219ff656b Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 2 Oct 2018 13:14:32 +0300 Subject: [PATCH 378/389] #769 Fixed regression with colors. --- src/net/Network.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 703e0ccf..828203a1 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -108,7 +108,7 @@ void Network::onActive(IStrategy *strategy, Client *client) const char *fingerprint = client->tlsFingerprint(); if (fingerprint != nullptr) { - LOG_INFO("\x1B[1;30mfingerprint (SHA-256): \"%s\"", fingerprint); + LOG_INFO("%sfingerprint (SHA-256): \"%s\"", isColors() ? "\x1B[1;30m" : "", fingerprint); } } From 152c4f2f1b31df7a1d346732100f7f3ee4861383 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 2 Oct 2018 17:50:19 +0300 Subject: [PATCH 379/389] #768 Fixed build error with MSVC 2015 and enabled ASM code. --- cmake/asm.cmake | 10 +- .../cnv2_double_main_loop_sandybridge.inc | 410 ++++++++++++++++++ .../cnv2_main_loop.S} | 6 +- src/crypto/asm/win64/cnv2_main_loop.asm | 25 ++ .../asm/win64/cnv2_main_loop_ivybridge.inc | 186 ++++++++ src/crypto/asm/win64/cnv2_main_loop_ryzen.inc | 179 ++++++++ 6 files changed, 811 insertions(+), 5 deletions(-) create mode 100644 src/crypto/asm/win64/cnv2_double_main_loop_sandybridge.inc rename src/crypto/asm/{cnv2_main_loop_win.S => win64/cnv2_main_loop.S} (69%) create mode 100644 src/crypto/asm/win64/cnv2_main_loop.asm create mode 100644 src/crypto/asm/win64/cnv2_main_loop_ivybridge.inc create mode 100644 src/crypto/asm/win64/cnv2_main_loop_ryzen.inc diff --git a/cmake/asm.cmake b/cmake/asm.cmake index cb50f0d9..358d5666 100644 --- a/cmake/asm.cmake +++ b/cmake/asm.cmake @@ -3,13 +3,19 @@ if (WITH_ASM AND NOT XMRIG_ARM AND CMAKE_SIZEOF_VOID_P EQUAL 8) if (CMAKE_C_COMPILER_ID MATCHES MSVC) enable_language(ASM_MASM) - set(XMRIG_ASM_FILE "src/crypto/asm/cnv2_main_loop.asm") + + if (MSVC_TOOLSET_VERSION GREATER_EQUAL 141) + set(XMRIG_ASM_FILE "src/crypto/asm/cnv2_main_loop.asm") + else() + set(XMRIG_ASM_FILE "src/crypto/asm/win64/cnv2_main_loop.asm") + endif() + set_property(SOURCE ${XMRIG_ASM_FILE} PROPERTY ASM_MASM) else() enable_language(ASM) if (WIN32 AND CMAKE_C_COMPILER_ID MATCHES GNU) - set(XMRIG_ASM_FILE "src/crypto/asm/cnv2_main_loop_win.S") + set(XMRIG_ASM_FILE "src/crypto/asm/win64/cnv2_main_loop.S") else() set(XMRIG_ASM_FILE "src/crypto/asm/cnv2_main_loop.S") endif() diff --git a/src/crypto/asm/win64/cnv2_double_main_loop_sandybridge.inc b/src/crypto/asm/win64/cnv2_double_main_loop_sandybridge.inc new file mode 100644 index 00000000..44ea8923 --- /dev/null +++ b/src/crypto/asm/win64/cnv2_double_main_loop_sandybridge.inc @@ -0,0 +1,410 @@ + mov rax, rsp + push rbx + push rbp + push rsi + push rdi + push r12 + push r13 + push r14 + push r15 + sub rsp, 184 + + stmxcsr DWORD PTR [rsp+272] + mov DWORD PTR [rsp+276], 24448 + ldmxcsr DWORD PTR [rsp+276] + + mov r13, QWORD PTR [rcx+224] + mov r9, rdx + mov r10, QWORD PTR [rcx+32] + mov r8, rcx + xor r10, QWORD PTR [rcx] + mov r14d, 524288 + mov r11, QWORD PTR [rcx+40] + xor r11, QWORD PTR [rcx+8] + mov rsi, QWORD PTR [rdx+224] + mov rdx, QWORD PTR [rcx+56] + xor rdx, QWORD PTR [rcx+24] + mov rdi, QWORD PTR [r9+32] + xor rdi, QWORD PTR [r9] + mov rbp, QWORD PTR [r9+40] + xor rbp, QWORD PTR [r9+8] + movd xmm0, rdx + movaps XMMWORD PTR [rax-88], xmm6 + movaps XMMWORD PTR [rax-104], xmm7 + movaps XMMWORD PTR [rax-120], xmm8 + movaps XMMWORD PTR [rsp+112], xmm9 + movaps XMMWORD PTR [rsp+96], xmm10 + movaps XMMWORD PTR [rsp+80], xmm11 + movaps XMMWORD PTR [rsp+64], xmm12 + movaps XMMWORD PTR [rsp+48], xmm13 + movaps XMMWORD PTR [rsp+32], xmm14 + movaps XMMWORD PTR [rsp+16], xmm15 + mov rdx, r10 + movd xmm4, QWORD PTR [r8+96] + and edx, 2097136 + mov rax, QWORD PTR [rcx+48] + xorps xmm13, xmm13 + xor rax, QWORD PTR [rcx+16] + mov rcx, QWORD PTR [rcx+88] + xor rcx, QWORD PTR [r8+72] + movd xmm5, QWORD PTR [r8+104] + movd xmm7, rax + + mov eax, 1 + shl rax, 52 + movd xmm14, rax + punpcklqdq xmm14, xmm14 + + mov eax, 1023 + shl rax, 52 + movd xmm12, rax + punpcklqdq xmm12, xmm12 + + mov rax, QWORD PTR [r8+80] + xor rax, QWORD PTR [r8+64] + punpcklqdq xmm7, xmm0 + movd xmm0, rcx + mov rcx, QWORD PTR [r9+56] + xor rcx, QWORD PTR [r9+24] + movd xmm3, rax + mov rax, QWORD PTR [r9+48] + xor rax, QWORD PTR [r9+16] + punpcklqdq xmm3, xmm0 + movd xmm0, rcx + mov QWORD PTR [rsp], r13 + mov rcx, QWORD PTR [r9+88] + xor rcx, QWORD PTR [r9+72] + movd xmm6, rax + mov rax, QWORD PTR [r9+80] + xor rax, QWORD PTR [r9+64] + punpcklqdq xmm6, xmm0 + movd xmm0, rcx + mov QWORD PTR [rsp+256], r10 + mov rcx, rdi + mov QWORD PTR [rsp+264], r11 + movd xmm8, rax + and ecx, 2097136 + punpcklqdq xmm8, xmm0 + movd xmm0, QWORD PTR [r9+96] + punpcklqdq xmm4, xmm0 + movd xmm0, QWORD PTR [r9+104] + lea r8, QWORD PTR [rcx+rsi] + movdqu xmm11, XMMWORD PTR [r8] + punpcklqdq xmm5, xmm0 + lea r9, QWORD PTR [rdx+r13] + movdqu xmm15, XMMWORD PTR [r9] + + ALIGN 16 +main_loop_double_sandybridge: + movdqu xmm9, xmm15 + mov eax, edx + mov ebx, edx + xor eax, 16 + xor ebx, 32 + xor edx, 48 + + movd xmm0, r11 + movd xmm2, r10 + punpcklqdq xmm2, xmm0 + aesenc xmm9, xmm2 + + movdqu xmm0, XMMWORD PTR [rax+r13] + movdqu xmm1, XMMWORD PTR [rbx+r13] + paddq xmm0, xmm7 + paddq xmm1, xmm2 + movdqu XMMWORD PTR [rbx+r13], xmm0 + movdqu xmm0, XMMWORD PTR [rdx+r13] + movdqu XMMWORD PTR [rdx+r13], xmm1 + paddq xmm0, xmm3 + movdqu XMMWORD PTR [rax+r13], xmm0 + + movd r11, xmm9 + mov edx, r11d + and edx, 2097136 + movdqa xmm0, xmm9 + pxor xmm0, xmm7 + movdqu XMMWORD PTR [r9], xmm0 + + lea rbx, QWORD PTR [rdx+r13] + mov r10, QWORD PTR [rdx+r13] + + movdqu xmm10, xmm11 + movd xmm0, rbp + movd xmm11, rdi + punpcklqdq xmm11, xmm0 + aesenc xmm10, xmm11 + + mov eax, ecx + mov r12d, ecx + xor eax, 16 + xor r12d, 32 + xor ecx, 48 + + movdqu xmm0, XMMWORD PTR [rax+rsi] + paddq xmm0, xmm6 + movdqu xmm1, XMMWORD PTR [r12+rsi] + movdqu XMMWORD PTR [r12+rsi], xmm0 + paddq xmm1, xmm11 + movdqu xmm0, XMMWORD PTR [rcx+rsi] + movdqu XMMWORD PTR [rcx+rsi], xmm1 + paddq xmm0, xmm8 + movdqu XMMWORD PTR [rax+rsi], xmm0 + + movd rcx, xmm10 + and ecx, 2097136 + + movdqa xmm0, xmm10 + pxor xmm0, xmm6 + movdqu XMMWORD PTR [r8], xmm0 + mov r12, QWORD PTR [rcx+rsi] + + mov r9, QWORD PTR [rbx+8] + + xor edx, 16 + mov r8d, edx + mov r15d, edx + + movd rdx, xmm5 + shl rdx, 32 + movd rax, xmm4 + xor rdx, rax + xor r10, rdx + mov rax, r10 + mul r11 + mov r11d, r8d + xor r11d, 48 + movd xmm0, rdx + xor rdx, [r11+r13] + movd xmm1, rax + xor rax, [r11+r13+8] + punpcklqdq xmm0, xmm1 + + pxor xmm0, XMMWORD PTR [r8+r13] + xor r8d, 32 + movdqu xmm1, XMMWORD PTR [r11+r13] + paddq xmm0, xmm7 + paddq xmm1, xmm2 + movdqu XMMWORD PTR [r11+r13], xmm0 + movdqu xmm0, XMMWORD PTR [r8+r13] + movdqu XMMWORD PTR [r8+r13], xmm1 + paddq xmm0, xmm3 + movdqu XMMWORD PTR [r15+r13], xmm0 + + mov r11, QWORD PTR [rsp+256] + add r11, rdx + mov rdx, QWORD PTR [rsp+264] + add rdx, rax + mov QWORD PTR [rbx], r11 + xor r11, r10 + mov QWORD PTR [rbx+8], rdx + xor rdx, r9 + mov QWORD PTR [rsp+256], r11 + and r11d, 2097136 + mov QWORD PTR [rsp+264], rdx + mov QWORD PTR [rsp+8], r11 + lea r15, QWORD PTR [r11+r13] + movdqu xmm15, XMMWORD PTR [r11+r13] + lea r13, QWORD PTR [rsi+rcx] + movdqa xmm0, xmm5 + psrldq xmm0, 8 + movaps xmm2, xmm13 + movd r10, xmm0 + psllq xmm5, 1 + shl r10, 32 + movdqa xmm0, xmm9 + psrldq xmm0, 8 + movdqa xmm1, xmm10 + movd r11, xmm0 + psrldq xmm1, 8 + movd r8, xmm1 + psrldq xmm4, 8 + movaps xmm0, xmm13 + movd rax, xmm4 + xor r10, rax + movaps xmm1, xmm13 + xor r10, r12 + lea rax, QWORD PTR [r11+1] + shr rax, 1 + movdqa xmm3, xmm9 + punpcklqdq xmm3, xmm10 + paddq xmm5, xmm3 + movd rdx, xmm5 + psrldq xmm5, 8 + cvtsi2sd xmm2, rax + or edx, -2147483647 + lea rax, QWORD PTR [r8+1] + shr rax, 1 + movd r9, xmm5 + cvtsi2sd xmm0, rax + or r9d, -2147483647 + cvtsi2sd xmm1, rdx + unpcklpd xmm2, xmm0 + movaps xmm0, xmm13 + cvtsi2sd xmm0, r9 + unpcklpd xmm1, xmm0 + divpd xmm2, xmm1 + paddq xmm2, xmm14 + cvttsd2si rax, xmm2 + psrldq xmm2, 8 + mov rbx, rax + imul rax, rdx + sub r11, rax + js div_fix_1_sandybridge +div_fix_1_ret_sandybridge: + + cvttsd2si rdx, xmm2 + mov rax, rdx + imul rax, r9 + movd xmm2, r11d + movd xmm4, ebx + sub r8, rax + js div_fix_2_sandybridge +div_fix_2_ret_sandybridge: + + movd xmm1, r8d + movd xmm0, edx + punpckldq xmm2, xmm1 + punpckldq xmm4, xmm0 + punpckldq xmm4, xmm2 + paddq xmm3, xmm4 + movdqa xmm0, xmm3 + psrlq xmm0, 12 + paddq xmm0, xmm12 + sqrtpd xmm1, xmm0 + movd r9, xmm1 + movdqa xmm5, xmm1 + psrlq xmm5, 19 + test r9, 524287 + je sqrt_fix_1_sandybridge +sqrt_fix_1_ret_sandybridge: + + movd r9, xmm10 + psrldq xmm1, 8 + movd r8, xmm1 + test r8, 524287 + je sqrt_fix_2_sandybridge +sqrt_fix_2_ret_sandybridge: + + mov r12d, ecx + mov r8d, ecx + xor r12d, 16 + xor r8d, 32 + xor ecx, 48 + mov rax, r10 + mul r9 + movd xmm0, rax + movd xmm3, rdx + punpcklqdq xmm3, xmm0 + + movdqu xmm0, XMMWORD PTR [r12+rsi] + pxor xmm0, xmm3 + movdqu xmm1, XMMWORD PTR [r8+rsi] + xor rdx, [r8+rsi] + xor rax, [r8+rsi+8] + movdqu xmm3, XMMWORD PTR [rcx+rsi] + paddq xmm0, xmm6 + paddq xmm1, xmm11 + paddq xmm3, xmm8 + movdqu XMMWORD PTR [r8+rsi], xmm0 + movdqu XMMWORD PTR [rcx+rsi], xmm1 + movdqu XMMWORD PTR [r12+rsi], xmm3 + + add rdi, rdx + mov QWORD PTR [r13], rdi + xor rdi, r10 + mov ecx, edi + and ecx, 2097136 + lea r8, QWORD PTR [rcx+rsi] + + mov rdx, QWORD PTR [r13+8] + add rbp, rax + mov QWORD PTR [r13+8], rbp + movdqu xmm11, XMMWORD PTR [rcx+rsi] + xor rbp, rdx + mov r13, QWORD PTR [rsp] + movdqa xmm3, xmm7 + mov rdx, QWORD PTR [rsp+8] + movdqa xmm8, xmm6 + mov r10, QWORD PTR [rsp+256] + movdqa xmm7, xmm9 + mov r11, QWORD PTR [rsp+264] + movdqa xmm6, xmm10 + mov r9, r15 + dec r14d + jne main_loop_double_sandybridge + + ldmxcsr DWORD PTR [rsp+272] + movaps xmm13, XMMWORD PTR [rsp+48] + lea r11, QWORD PTR [rsp+184] + movaps xmm6, XMMWORD PTR [r11-24] + movaps xmm7, XMMWORD PTR [r11-40] + movaps xmm8, XMMWORD PTR [r11-56] + movaps xmm9, XMMWORD PTR [r11-72] + movaps xmm10, XMMWORD PTR [r11-88] + movaps xmm11, XMMWORD PTR [r11-104] + movaps xmm12, XMMWORD PTR [r11-120] + movaps xmm14, XMMWORD PTR [rsp+32] + movaps xmm15, XMMWORD PTR [rsp+16] + mov rsp, r11 + pop r15 + pop r14 + pop r13 + pop r12 + pop rdi + pop rsi + pop rbp + pop rbx + jmp cnv2_double_mainloop_asm_sandybridge_endp + +div_fix_1_sandybridge: + dec rbx + add r11, rdx + jmp div_fix_1_ret_sandybridge + +div_fix_2_sandybridge: + dec rdx + add r8, r9 + jmp div_fix_2_ret_sandybridge + +sqrt_fix_1_sandybridge: + movd r8, xmm3 + movdqa xmm0, xmm5 + psrldq xmm0, 8 + dec r9 + mov r11d, -1022 + shl r11, 32 + mov rax, r9 + shr r9, 19 + shr rax, 20 + mov rdx, r9 + sub rdx, rax + lea rdx, [rdx+r11+1] + add rax, r11 + imul rdx, rax + sub rdx, r8 + adc r9, 0 + movd xmm5, r9 + punpcklqdq xmm5, xmm0 + jmp sqrt_fix_1_ret_sandybridge + +sqrt_fix_2_sandybridge: + psrldq xmm3, 8 + movd r11, xmm3 + dec r8 + mov ebx, -1022 + shl rbx, 32 + mov rax, r8 + shr r8, 19 + shr rax, 20 + mov rdx, r8 + sub rdx, rax + lea rdx, [rdx+rbx+1] + add rax, rbx + imul rdx, rax + sub rdx, r11 + adc r8, 0 + movd xmm0, r8 + punpcklqdq xmm5, xmm0 + jmp sqrt_fix_2_ret_sandybridge + +cnv2_double_mainloop_asm_sandybridge_endp: diff --git a/src/crypto/asm/cnv2_main_loop_win.S b/src/crypto/asm/win64/cnv2_main_loop.S similarity index 69% rename from src/crypto/asm/cnv2_main_loop_win.S rename to src/crypto/asm/win64/cnv2_main_loop.S index f06e4fa4..78eb1185 100644 --- a/src/crypto/asm/cnv2_main_loop_win.S +++ b/src/crypto/asm/win64/cnv2_main_loop.S @@ -7,15 +7,15 @@ ALIGN 16 cnv2_mainloop_ivybridge_asm: - #include "cnv2_main_loop_ivybridge.inc" + #include "../cnv2_main_loop_ivybridge.inc" ret 0 ALIGN 16 cnv2_mainloop_ryzen_asm: - #include "cnv2_main_loop_ryzen.inc" + #include "../cnv2_main_loop_ryzen.inc" ret 0 ALIGN 16 cnv2_double_mainloop_sandybridge_asm: - #include "cnv2_double_main_loop_sandybridge.inc" + #include "../cnv2_double_main_loop_sandybridge.inc" ret 0 diff --git a/src/crypto/asm/win64/cnv2_main_loop.asm b/src/crypto/asm/win64/cnv2_main_loop.asm new file mode 100644 index 00000000..d9522267 --- /dev/null +++ b/src/crypto/asm/win64/cnv2_main_loop.asm @@ -0,0 +1,25 @@ +_TEXT_CNV2_MAINLOOP SEGMENT PAGE READ EXECUTE +PUBLIC cnv2_mainloop_ivybridge_asm +PUBLIC cnv2_mainloop_ryzen_asm +PUBLIC cnv2_double_mainloop_sandybridge_asm + +ALIGN 64 +cnv2_mainloop_ivybridge_asm PROC + INCLUDE cnv2_main_loop_ivybridge.inc + ret 0 +cnv2_mainloop_ivybridge_asm ENDP + +ALIGN 64 +cnv2_mainloop_ryzen_asm PROC + INCLUDE cnv2_main_loop_ryzen.inc + ret 0 +cnv2_mainloop_ryzen_asm ENDP + +ALIGN 64 +cnv2_double_mainloop_sandybridge_asm PROC + INCLUDE cnv2_double_main_loop_sandybridge.inc + ret 0 +cnv2_double_mainloop_sandybridge_asm ENDP + +_TEXT_CNV2_MAINLOOP ENDS +END diff --git a/src/crypto/asm/win64/cnv2_main_loop_ivybridge.inc b/src/crypto/asm/win64/cnv2_main_loop_ivybridge.inc new file mode 100644 index 00000000..c925ca24 --- /dev/null +++ b/src/crypto/asm/win64/cnv2_main_loop_ivybridge.inc @@ -0,0 +1,186 @@ + mov QWORD PTR [rsp+24], rbx + push rbp + push rsi + push rdi + push r12 + push r13 + push r14 + push r15 + sub rsp, 80 + + stmxcsr DWORD PTR [rsp] + mov DWORD PTR [rsp+4], 24448 + ldmxcsr DWORD PTR [rsp+4] + + mov rax, QWORD PTR [rcx+48] + mov r9, rcx + xor rax, QWORD PTR [rcx+16] + mov esi, 524288 + mov r8, QWORD PTR [rcx+32] + mov r13d, -2147483647 + xor r8, QWORD PTR [rcx] + mov r11, QWORD PTR [rcx+40] + mov r10, r8 + mov rdx, QWORD PTR [rcx+56] + movd xmm4, rax + xor rdx, QWORD PTR [rcx+24] + xor r11, QWORD PTR [rcx+8] + mov rbx, QWORD PTR [rcx+224] + mov rax, QWORD PTR [r9+80] + xor rax, QWORD PTR [r9+64] + movd xmm0, rdx + mov rcx, QWORD PTR [rcx+88] + xor rcx, QWORD PTR [r9+72] + movd xmm3, QWORD PTR [r9+104] + movaps XMMWORD PTR [rsp+64], xmm6 + movaps XMMWORD PTR [rsp+48], xmm7 + movaps XMMWORD PTR [rsp+32], xmm8 + and r10d, 2097136 + movd xmm5, rax + + xor eax, eax + mov QWORD PTR [rsp+16], rax + + mov ax, 1023 + shl rax, 52 + movd xmm8, rax + mov r15, QWORD PTR [r9+96] + punpcklqdq xmm4, xmm0 + movd xmm0, rcx + punpcklqdq xmm5, xmm0 + movdqu xmm6, XMMWORD PTR [r10+rbx] + + ALIGN 16 +main_loop_ivybridge: + lea rdx, QWORD PTR [r10+rbx] + mov ecx, r10d + mov eax, r10d + mov rdi, r15 + xor ecx, 16 + xor eax, 32 + xor r10d, 48 + movd xmm0, r11 + movd xmm7, r8 + punpcklqdq xmm7, xmm0 + aesenc xmm6, xmm7 + movd rbp, xmm6 + mov r9, rbp + and r9d, 2097136 + movdqu xmm2, XMMWORD PTR [rcx+rbx] + movdqu xmm1, XMMWORD PTR [rax+rbx] + movdqu xmm0, XMMWORD PTR [r10+rbx] + paddq xmm1, xmm7 + paddq xmm0, xmm5 + paddq xmm2, xmm4 + movdqu XMMWORD PTR [rcx+rbx], xmm0 + movdqu XMMWORD PTR [rax+rbx], xmm2 + movdqu XMMWORD PTR [r10+rbx], xmm1 + mov r10, r9 + xor r10d, 32 + movd rcx, xmm3 + mov rax, rcx + shl rax, 32 + xor rdi, rax + movdqa xmm0, xmm6 + pxor xmm0, xmm4 + movdqu XMMWORD PTR [rdx], xmm0 + xor rdi, QWORD PTR [r9+rbx] + lea r14, QWORD PTR [r9+rbx] + mov r12, QWORD PTR [r14+8] + xor edx, edx + lea r9d, DWORD PTR [ecx+ecx] + add r9d, ebp + movdqa xmm0, xmm6 + psrldq xmm0, 8 + or r9d, r13d + movd rax, xmm0 + div r9 + xorps xmm3, xmm3 + mov eax, eax + shl rdx, 32 + add rdx, rax + lea r9, QWORD PTR [rdx+rbp] + mov r15, rdx + mov rax, r9 + shr rax, 12 + movd xmm0, rax + paddq xmm0, xmm8 + sqrtsd xmm3, xmm0 + psubq xmm3, XMMWORD PTR [rsp+16] + movd rdx, xmm3 + test edx, 524287 + je sqrt_fixup_ivybridge + psrlq xmm3, 19 +sqrt_fixup_ivybridge_ret: + + mov ecx, r10d + mov rax, rdi + mul rbp + movd xmm2, rdx + xor rdx, [rcx+rbx] + add r8, rdx + mov QWORD PTR [r14], r8 + xor r8, rdi + mov edi, r8d + and edi, 2097136 + movd xmm0, rax + xor rax, [rcx+rbx+8] + add r11, rax + mov QWORD PTR [r14+8], r11 + punpcklqdq xmm2, xmm0 + + mov r9d, r10d + xor r9d, 48 + xor r10d, 16 + pxor xmm2, XMMWORD PTR [r9+rbx] + movdqu xmm0, XMMWORD PTR [r10+rbx] + paddq xmm0, xmm5 + movdqu xmm1, XMMWORD PTR [rcx+rbx] + paddq xmm2, xmm4 + paddq xmm1, xmm7 + movdqa xmm5, xmm4 + movdqu XMMWORD PTR [r9+rbx], xmm0 + movdqa xmm4, xmm6 + movdqu XMMWORD PTR [rcx+rbx], xmm2 + movdqu XMMWORD PTR [r10+rbx], xmm1 + movdqu xmm6, [rdi+rbx] + mov r10d, edi + xor r11, r12 + dec rsi + jne main_loop_ivybridge + + ldmxcsr DWORD PTR [rsp] + mov rbx, QWORD PTR [rsp+160] + movaps xmm6, XMMWORD PTR [rsp+64] + movaps xmm7, XMMWORD PTR [rsp+48] + movaps xmm8, XMMWORD PTR [rsp+32] + add rsp, 80 + pop r15 + pop r14 + pop r13 + pop r12 + pop rdi + pop rsi + pop rbp + jmp cnv2_main_loop_ivybridge_endp + +sqrt_fixup_ivybridge: + dec rdx + mov r13d, -1022 + shl r13, 32 + mov rax, rdx + shr rdx, 19 + shr rax, 20 + mov rcx, rdx + sub rcx, rax + add rax, r13 + not r13 + sub rcx, r13 + mov r13d, -2147483647 + imul rcx, rax + sub rcx, r9 + adc rdx, 0 + movd xmm3, rdx + jmp sqrt_fixup_ivybridge_ret + +cnv2_main_loop_ivybridge_endp: diff --git a/src/crypto/asm/win64/cnv2_main_loop_ryzen.inc b/src/crypto/asm/win64/cnv2_main_loop_ryzen.inc new file mode 100644 index 00000000..d1cd26c4 --- /dev/null +++ b/src/crypto/asm/win64/cnv2_main_loop_ryzen.inc @@ -0,0 +1,179 @@ + mov QWORD PTR [rsp+16], rbx + mov QWORD PTR [rsp+24], rbp + mov QWORD PTR [rsp+32], rsi + push rdi + push r12 + push r13 + push r14 + push r15 + sub rsp, 64 + + stmxcsr DWORD PTR [rsp] + mov DWORD PTR [rsp+4], 24448 + ldmxcsr DWORD PTR [rsp+4] + + mov rax, QWORD PTR [rcx+48] + mov r9, rcx + xor rax, QWORD PTR [rcx+16] + mov ebp, 524288 + mov r8, QWORD PTR [rcx+32] + xor r8, QWORD PTR [rcx] + mov r11, QWORD PTR [rcx+40] + mov r10, r8 + mov rdx, QWORD PTR [rcx+56] + movd xmm3, rax + xor rdx, QWORD PTR [rcx+24] + xor r11, QWORD PTR [rcx+8] + mov rbx, QWORD PTR [rcx+224] + mov rax, QWORD PTR [r9+80] + xor rax, QWORD PTR [r9+64] + movd xmm0, rdx + mov rcx, QWORD PTR [rcx+88] + xor rcx, QWORD PTR [r9+72] + mov rdi, QWORD PTR [r9+104] + and r10d, 2097136 + movaps XMMWORD PTR [rsp+48], xmm6 + movd xmm4, rax + movaps XMMWORD PTR [rsp+32], xmm7 + movaps XMMWORD PTR [rsp+16], xmm8 + xorps xmm8, xmm8 + mov ax, 1023 + shl rax, 52 + movd xmm7, rax + mov r15, QWORD PTR [r9+96] + punpcklqdq xmm3, xmm0 + movd xmm0, rcx + punpcklqdq xmm4, xmm0 + + ALIGN 16 +main_loop_ryzen: + movdqa xmm5, XMMWORD PTR [r10+rbx] + movd xmm0, r11 + movd xmm6, r8 + punpcklqdq xmm6, xmm0 + lea rdx, QWORD PTR [r10+rbx] + lea r9, QWORD PTR [rdi+rdi] + shl rdi, 32 + + mov ecx, r10d + mov eax, r10d + xor ecx, 16 + xor eax, 32 + xor r10d, 48 + aesenc xmm5, xmm6 + movdqa xmm2, XMMWORD PTR [rcx+rbx] + movdqa xmm1, XMMWORD PTR [rax+rbx] + movdqa xmm0, XMMWORD PTR [r10+rbx] + paddq xmm2, xmm3 + paddq xmm1, xmm6 + paddq xmm0, xmm4 + movdqa XMMWORD PTR [rcx+rbx], xmm0 + movdqa XMMWORD PTR [rax+rbx], xmm2 + movdqa XMMWORD PTR [r10+rbx], xmm1 + + movaps xmm1, xmm8 + mov rsi, r15 + xor rsi, rdi + movd r14, xmm5 + movdqa xmm0, xmm5 + pxor xmm0, xmm3 + mov r10, r14 + and r10d, 2097136 + movdqa XMMWORD PTR [rdx], xmm0 + xor rsi, QWORD PTR [r10+rbx] + lea r12, QWORD PTR [r10+rbx] + mov r13, QWORD PTR [r10+rbx+8] + + add r9d, r14d + or r9d, -2147483647 + xor edx, edx + movdqa xmm0, xmm5 + psrldq xmm0, 8 + movd rax, xmm0 + + div r9 + movd xmm0, rax + movd xmm1, rdx + punpckldq xmm0, xmm1 + movd r15, xmm0 + paddq xmm0, xmm5 + movdqa xmm2, xmm0 + psrlq xmm0, 12 + paddq xmm0, xmm7 + sqrtsd xmm1, xmm0 + movd rdi, xmm1 + test rdi, 524287 + je sqrt_fixup_ryzen + shr rdi, 19 + +sqrt_fixup_ryzen_ret: + mov rax, rsi + mul r14 + movd xmm1, rax + movd xmm0, rdx + punpcklqdq xmm0, xmm1 + + mov r9d, r10d + mov ecx, r10d + xor r9d, 16 + xor ecx, 32 + xor r10d, 48 + movdqa xmm1, XMMWORD PTR [rcx+rbx] + xor rdx, [rcx+rbx] + xor rax, [rcx+rbx+8] + movdqa xmm2, XMMWORD PTR [r9+rbx] + pxor xmm2, xmm0 + paddq xmm4, XMMWORD PTR [r10+rbx] + paddq xmm2, xmm3 + paddq xmm1, xmm6 + movdqa XMMWORD PTR [r9+rbx], xmm4 + movdqa XMMWORD PTR [rcx+rbx], xmm2 + movdqa XMMWORD PTR [r10+rbx], xmm1 + + movdqa xmm4, xmm3 + add r8, rdx + add r11, rax + mov QWORD PTR [r12], r8 + xor r8, rsi + mov QWORD PTR [r12+8], r11 + mov r10, r8 + xor r11, r13 + and r10d, 2097136 + movdqa xmm3, xmm5 + dec ebp + jne main_loop_ryzen + + ldmxcsr DWORD PTR [rsp] + movaps xmm6, XMMWORD PTR [rsp+48] + lea r11, QWORD PTR [rsp+64] + mov rbx, QWORD PTR [r11+56] + mov rbp, QWORD PTR [r11+64] + mov rsi, QWORD PTR [r11+72] + movaps xmm8, XMMWORD PTR [r11-48] + movaps xmm7, XMMWORD PTR [rsp+32] + mov rsp, r11 + pop r15 + pop r14 + pop r13 + pop r12 + pop rdi + jmp cnv2_main_loop_ryzen_endp + +sqrt_fixup_ryzen: + movd r9, xmm2 + dec rdi + mov edx, -1022 + shl rdx, 32 + mov rax, rdi + shr rdi, 19 + shr rax, 20 + mov rcx, rdi + sub rcx, rax + lea rcx, [rcx+rdx+1] + add rax, rdx + imul rcx, rax + sub rcx, r9 + adc rdi, 0 + jmp sqrt_fixup_ryzen_ret + +cnv2_main_loop_ryzen_endp: From 20268d5291ae1f99ce11154562a055735e6ab530 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 4 Oct 2018 19:10:47 +0300 Subject: [PATCH 380/389] #753 Removed unnecessary ext_family and ext_model checks. --- src/core/cpu/AdvancedCpuInfo.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/cpu/AdvancedCpuInfo.cpp b/src/core/cpu/AdvancedCpuInfo.cpp index 1f86a420..c1a9f8cd 100644 --- a/src/core/cpu/AdvancedCpuInfo.cpp +++ b/src/core/cpu/AdvancedCpuInfo.cpp @@ -75,10 +75,10 @@ xmrig::AdvancedCpuInfo::AdvancedCpuInfo() : if (data.flags[CPU_FEATURE_AES]) { m_aes = true; - if (data.vendor == VENDOR_AMD && data.ext_family >= 23) { + if (data.vendor == VENDOR_AMD) { m_assembly = ASM_RYZEN; } - else if (data.vendor == VENDOR_INTEL && data.ext_model >= 42) { + else if (data.vendor == VENDOR_INTEL) { m_assembly = ASM_INTEL; } } From 8f3d405b34d606c2bacd828aa3fbd61c39fa2f86 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 6 Oct 2018 14:25:59 +0300 Subject: [PATCH 381/389] Update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5849269..8e358398 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# v2.8.1 +- [#768](https://github.com/xmrig/xmrig/issues/768) Fixed build with Visual Studio 2015. +- [#769](https://github.com/xmrig/xmrig/issues/769) Fixed regression, some ANSI escape sequences was in log with disabled colors. +- Simplified checks for ASM auto detection, only AES support necessary. + # v2.8.0 - **[#753](https://github.com/xmrig/xmrig/issues/753) Added new algorithm [CryptoNight variant 2](https://github.com/xmrig/xmrig/issues/753) for Monero fork, thanks [@SChernykh](https://github.com/SChernykh).** - Added global and per thread option `"asm"` and and command line equivalent. From ad92c3b0255cac7978a0a1f345c2a3a06a398111 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 6 Oct 2018 15:13:55 +0300 Subject: [PATCH 382/389] Add ASM detection for builds without libcpuid. --- src/common/cpu/BasicCpuInfo.cpp | 25 +++++++++++++++++++++++-- src/common/cpu/BasicCpuInfo.h | 4 ++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/common/cpu/BasicCpuInfo.cpp b/src/common/cpu/BasicCpuInfo.cpp index 66af53cc..cb1e6d1d 100644 --- a/src/common/cpu/BasicCpuInfo.cpp +++ b/src/common/cpu/BasicCpuInfo.cpp @@ -71,7 +71,7 @@ static inline void cpuid(int level, int output[4]) { static inline void cpu_brand_string(char* s) { - int cpu_info[4] = { 0 }; + int32_t cpu_info[4] = { 0 }; cpuid(VENDOR_ID, cpu_info); if (cpu_info[EAX_Reg] >= 4) { @@ -86,7 +86,7 @@ static inline void cpu_brand_string(char* s) { static inline bool has_aes_ni() { - int cpu_info[4] = { 0 }; + int32_t cpu_info[4] = { 0 }; cpuid(PROCESSOR_INFO, cpu_info); return (cpu_info[ECX_Reg] & bit_AES) != 0; @@ -94,11 +94,32 @@ static inline bool has_aes_ni() xmrig::BasicCpuInfo::BasicCpuInfo() : + m_assembly(ASM_NONE), m_aes(has_aes_ni()), m_brand(), m_threads(std::thread::hardware_concurrency()) { cpu_brand_string(m_brand); + +# ifndef XMRIG_NO_ASM + if (hasAES()) { + char vendor[13] = { 0 }; + int32_t data[4] = { 0 }; + + cpuid(0, data); + + memcpy(vendor + 0, &data[1], 4); + memcpy(vendor + 4, &data[3], 4); + memcpy(vendor + 8, &data[2], 4); + + if (memcmp(vendor, "GenuineIntel", 12) == 0) { + m_assembly = ASM_INTEL; + } + else if (memcmp(vendor, "AuthenticAMD", 12) == 0) { + m_assembly = ASM_RYZEN; + } + } +# endif } diff --git a/src/common/cpu/BasicCpuInfo.h b/src/common/cpu/BasicCpuInfo.h index f9d710d6..911674ea 100644 --- a/src/common/cpu/BasicCpuInfo.h +++ b/src/common/cpu/BasicCpuInfo.h @@ -7,7 +7,6 @@ * Copyright 2017-2018 XMR-Stak , * Copyright 2016-2018 XMRig , * - * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -40,7 +39,7 @@ public: protected: size_t optimalThreadsCount(size_t memSize, int maxCpuUsage) const override; - inline Assembly assembly() const override { return ASM_NONE; } + inline Assembly assembly() const override { return m_assembly; } inline bool hasAES() const override { return m_aes; } inline bool isSupported() const override { return true; } inline const char *brand() const override { return m_brand; } @@ -58,6 +57,7 @@ protected: # endif private: + Assembly m_assembly; bool m_aes; char m_brand[64]; int32_t m_threads; From 023062b2f13920410124a94565d04cfc65bee3eb Mon Sep 17 00:00:00 2001 From: xmrig Date: Tue, 9 Oct 2018 01:32:07 +0700 Subject: [PATCH 383/389] Update README.md --- README.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 81b1baea..e2e04aa1 100644 --- a/README.md +++ b/README.md @@ -52,14 +52,21 @@ Use [config.xmrig.com](https://config.xmrig.com/xmrig) to generate, edit or shar ### Options ``` - -a, --algo=ALGO cryptonight (default) or cryptonight-lite + -a, --algo=ALGO specify the algorithm to use + cryptonight + cryptonight-lite + cryptonight-heavy -o, --url=URL URL of mining server -O, --userpass=U:P username:password pair for mining server -u, --user=USERNAME username for mining server -p, --pass=PASSWORD password for mining server + --rig-id=ID rig identifier for pool-side statistics (needs pool support) -t, --threads=N number of miner threads -v, --av=N algorithm variation, 0 auto select - -k, --keepalive send keepalived for prevent timeout (need pool support) + -k, --keepalive send keepalived packet for prevent timeout (needs pool support) + --nicehash enable nicehash.com support + --tls enable SSL/TLS support (needs pool support) + --tls-fingerprint=F pool TLS certificate fingerprint, if set enable strict certificate pinning -r, --retries=N number of times to retry before switch to backup server (default: 5) -R, --retry-pause=N time to pause between retries (default: 5) --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1 @@ -75,16 +82,20 @@ Use [config.xmrig.com](https://config.xmrig.com/xmrig) to generate, edit or shar -S, --syslog use system log for output messages --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75) --safe safe adjust threads and av settings for current CPU - --nicehash enable nicehash/xmrig-proxy support + --asm=ASM ASM code for cn/2, possible values: auto, none, intel, ryzen. --print-time=N print hashrate report every N seconds --api-port=N port for the miner API --api-access-token=T access token for API --api-worker-id=ID custom worker-id for API + --api-id=ID custom instance ID for API + --api-ipv6 enable IPv6 support for API + --api-no-restricted enable full remote access (only if API token set) + --dry-run test configuration and exit -h, --help display this help and exit -V, --version output version information and exit ``` -Also you can use configuration via config file, default **config.json**. You can load multiple config files and combine it with command line options. +Also you can use configuration via config file, default name **config.json**. Some options available only via config file: [`autosave`](https://github.com/xmrig/xmrig/issues/767), [`hw-aes`](https://github.com/xmrig/xmrig/issues/563). `watch` option currently not implemented in miners only in proxy. ## Algorithm variations From dda8157a7bab1317227028da644d1949291f225f Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 9 Oct 2018 01:35:33 +0700 Subject: [PATCH 384/389] Add "--tls", "--tls-fingerprint", "--asm" and "--dry-run" to help output. --- src/core/ConfigLoader_platform.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index a932b235..54546211 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -61,7 +61,10 @@ Options:\n\ --rig-id=ID rig identifier for pool-side statistics (needs pool support)\n\ -t, --threads=N number of miner threads\n\ -v, --av=N algorithm variation, 0 auto select\n\ - -k, --keepalive send keepalived for prevent timeout (need pool support)\n\ + -k, --keepalive send keepalived packet for prevent timeout (needs pool support)\n\ + --nicehash enable nicehash.com support\n\ + --tls enable SSL/TLS support (needs pool support)\n\ + --tls-fingerprint=F pool TLS certificate fingerprint, if set enable strict certificate pinning\n\ -r, --retries=N number of times to retry before switch to backup server (default: 5)\n\ -R, --retry-pause=N time to pause between retries (default: 5)\n\ --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1\n\ @@ -81,7 +84,7 @@ Options:\n\ "\ --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75)\n\ --safe safe adjust threads and av settings for current CPU\n\ - --nicehash enable nicehash/xmrig-proxy support\n\ + --asm=ASM ASM code for cn/2, possible values: auto, none, intel, ryzen.\n\ --print-time=N print hashrate report every N seconds\n\ --api-port=N port for the miner API\n\ --api-access-token=T access token for API\n\ @@ -89,6 +92,7 @@ Options:\n\ --api-id=ID custom instance ID for API\n\ --api-ipv6 enable IPv6 support for API\n\ --api-no-restricted enable full remote access (only if API token set)\n\ + --dry-run test configuration and exit\n\ -h, --help display this help and exit\n\ -V, --version output version information and exit\n\ "; From 9ef59366ba74237bf285323b636997a93bffdfae Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 9 Oct 2018 02:32:29 +0700 Subject: [PATCH 385/389] Sync changes with proxy. --- src/common/net/Client.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index 6a79749d..aaffb40e 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -259,7 +259,11 @@ int64_t Client::submit(const JobResult &result) bool Client::close() { - if (m_state == UnconnectedState || m_state == ClosingState || !m_socket) { + if (m_state == ClosingState) { + return m_socket != nullptr; + } + + if (m_state == UnconnectedState || m_socket == nullptr) { return false; } @@ -426,7 +430,7 @@ bool Client::send(BIO *bio) bool Client::verifyAlgorithm(const xmrig::Algorithm &algorithm) const { # ifdef XMRIG_PROXY_PROJECT - if (m_pool.algorithm().variant() == xmrig::VARIANT_AUTO) { + if (m_pool.algorithm().variant() == xmrig::VARIANT_AUTO || m_id == -1) { return true; } # endif @@ -550,7 +554,6 @@ void Client::connect(sockaddr *addr) setState(ConnectingState); reinterpret_cast(addr)->sin_port = htons(m_pool.port()); - delete m_socket; uv_connect_t *req = new uv_connect_t; req->data = m_storage.ptr(m_key); @@ -831,13 +834,14 @@ void Client::reconnect() return; } - setState(ConnectingState); m_keepAlive = 0; if (m_failures == -1) { return m_listener->onClose(this, -1); } + setState(ConnectingState); + m_failures++; m_listener->onClose(this, (int) m_failures); From bdc16df418747daffc5200181fd90e333b072680 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 9 Oct 2018 03:47:39 +0700 Subject: [PATCH 386/389] #777 Make EOF as verbose error too. --- src/common/net/Client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index aaffb40e..1d1d86c7 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -931,7 +931,7 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) } if (nread < 0) { - if (nread != UV_EOF && !client->isQuiet()) { + if (!client->isQuiet()) { LOG_ERR("[%s] read error: \"%s\"", client->m_pool.url(), uv_strerror((int) nread)); } From ff3ae25d1647b18a5ec33c77cdaa3158e83fda29 Mon Sep 17 00:00:00 2001 From: xmrig Date: Tue, 9 Oct 2018 07:12:31 +0700 Subject: [PATCH 387/389] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e358398..8dc3b169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ # v2.8.1 - [#768](https://github.com/xmrig/xmrig/issues/768) Fixed build with Visual Studio 2015. - [#769](https://github.com/xmrig/xmrig/issues/769) Fixed regression, some ANSI escape sequences was in log with disabled colors. +- [#777](https://github.com/xmrig/xmrig/issues/777) Better report about pool connection issues. - Simplified checks for ASM auto detection, only AES support necessary. +- Added missing options to `--help` output. # v2.8.0 - **[#753](https://github.com/xmrig/xmrig/issues/753) Added new algorithm [CryptoNight variant 2](https://github.com/xmrig/xmrig/issues/753) for Monero fork, thanks [@SChernykh](https://github.com/SChernykh).** From 93b54f8f4495b132bc402e6e2f876ecb0b758c72 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 9 Oct 2018 07:22:23 +0700 Subject: [PATCH 388/389] v2.8.1 --- src/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index 3c65f25b..0ca746dd 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.8.1-dev" +#define APP_VERSION "2.8.1" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" From 4bf248f5135c63fa1fd2d085f6133981e732c304 Mon Sep 17 00:00:00 2001 From: xmrig Date: Tue, 9 Oct 2018 07:41:40 +0700 Subject: [PATCH 389/389] Update README.md --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e2e04aa1..9ad83679 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # XMRig -:warning: **[Monero will change PoW algorithm on October 18](https://github.com/xmrig/xmrig/issues/753), all miners and proxy should be updated to [v2.8+](https://github.com/xmrig/xmrig/releases/tag/v2.8.0-rc)** :warning: +:warning: **[Monero will change PoW algorithm on October 18](https://github.com/xmrig/xmrig/issues/753), all miners and proxy should be updated to [v2.8+](https://github.com/xmrig/xmrig/releases/tag/v2.8.1)** :warning: [![Github All Releases](https://img.shields.io/github/downloads/xmrig/xmrig/total.svg)](https://github.com/xmrig/xmrig/releases) [![GitHub release](https://img.shields.io/github/release/xmrig/xmrig/all.svg)](https://github.com/xmrig/xmrig/releases) @@ -145,14 +145,14 @@ Please note performance is highly dependent on system load. The numbers above ar ## Release checksums ### SHA-256 ``` -ea6a71732937e06d5434b863bedd2d627c500e8ce30b30a02054015bb3aae3fc xmrig-2.8.0-xenial-amd64.tar.gz/xmrig-2.8.0/xmrig -a067de09a2d49c39317d6a98e420fcd80ef040c8cecb6b21317cbe4eabbdb995 xmrig-2.8.0-xenial-amd64.tar.gz/xmrig-2.8.0/xmrig-notls -89b7b4616faec76c40dab714046ea08ebcd6c558299d17d44639e6bc62c54186 xmrig-2.8.0-gcc-win32.zip/xmrig.exe -0fae870bef4223905b24bb00283b3bba7ece547a09a540033beb9a2a33335124 xmrig-2.8.0-gcc-win32.zip/xmrig-notls.exe -953df29ef354d541b89a70a36ddf16d8a9d5f2c37419d3aacd1352ff6ff539e8 xmrig-2.8.0-gcc-win64.zip/xmrig.exe -84d73422a43b46879d88f87a492e99c81d5d632eb3be79793fc0b895196f131d xmrig-2.8.0-gcc-win64.zip/xmrig-notls.exe -8e1302fe632249a713d7af0b7f3f50ad6e707d71d00fcd1f3be56abc024f98ac xmrig-2.8.0-msvc-win64.zip/xmrig.exe -0042b5e7b14a4f8aca748a4c7656a0ec6705bc593c26f9987a25b10fa471f004 xmrig-2.8.0-msvc-win64.zip/xmrig-notls.exe +fd909cef75c65c1ee8facd78e968c359e4ac4d2f0b2aaef8c6d3e0138979d0ea xmrig-2.8.1-xenial-amd64.tar.gz/xmrig-2.8.1/xmrig +e4987eaec57c1784c423e03978e22262adf070ab3ad7b2a6ce8d0d0626db4cbd xmrig-2.8.1-xenial-amd64.tar.gz/xmrig-2.8.1/xmrig-notls +4ebf6053513c8b72b46f54481f33367aae6356fc5f271c9236d708a34cd16e2a xmrig-2.8.1-gcc-win32.zip/xmrig.exe +f4ce5b76a611f6768c9a035eae1e49f61666f3e5370b54bd447ecc3b0098efcb xmrig-2.8.1-gcc-win32.zip/xmrig-notls.exe +39259979b4228899c0ef985bcfc283e169afd44323eedb0341144dbc0c0f30e9 xmrig-2.8.1-gcc-win64.zip/xmrig.exe +68af0f11b29b9b67414d0b661446baa0ad6020598c04c5d0cf24797167753117 xmrig-2.8.1-gcc-win64.zip/xmrig-notls.exe +5d8b94157ebb4a7729f0eb8622945c266b756c994c60f91514b329edeb287867 xmrig-2.8.1-msvc-win64.zip/xmrig.exe +d97c691d6044f916b9253820832f1a08402bb63aa75fb146ea8c31f51cebe974 xmrig-2.8.1-msvc-win64.zip/xmrig-notls.exe ``` ## Contacts