Fixed unaligned memory accesses

This commit is contained in:
SChernykh 2022-01-26 15:26:23 +01:00
parent 41a3f97060
commit 644f4cc017
9 changed files with 74 additions and 14 deletions

View file

@ -68,6 +68,7 @@ set(HEADERS_BASE
src/base/net/tools/MemPool.h
src/base/net/tools/NetBuffer.h
src/base/net/tools/Storage.h
src/base/tools/Alignment.h
src/base/tools/Arguments.h
src/base/tools/Baton.h
src/base/tools/bswap_64.h

View file

@ -30,6 +30,7 @@
#include "base/net/stratum/Job.h"
#include "base/tools/Alignment.h"
#include "base/tools/Buffer.h"
#include "base/tools/Cvt.h"
#include "base/tools/cryptonote/BlockTemplate.h"
@ -73,7 +74,7 @@ bool xmrig::Job::setBlob(const char *blob)
return false;
}
if (*nonce() != 0 && !m_nicehash) {
if (readUnaligned(nonce()) != 0 && !m_nicehash) {
m_nicehash = true;
}

View file

@ -0,0 +1,53 @@
/* XMRig
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2022 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_ALIGNMENT_H
#define XMRIG_ALIGNMENT_H
#include <type_traits>
#include <cstring>
namespace xmrig {
template<typename T>
inline T readUnaligned(const T* ptr)
{
static_assert(std::is_integral<T>::value, "Integer type required");
T result;
memcpy(&result, ptr, sizeof(T));
return result;
}
template<typename T>
inline void writeUnaligned(T* ptr, T data)
{
static_assert(std::is_integral<T>::value, "Integer type required");
memcpy(ptr, &data, sizeof(T));
}
} /* namespace xmrig */
#endif /* XMRIG_ALIGNMENT_H */