Merge pull request #9423 from MerryMage/arm64-movi2r-test

UnitTests: Add MOVI2R test
This commit is contained in:
JosJuice 2021-02-08 10:58:09 +01:00 committed by GitHub
commit 3e4bf57c69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 163 additions and 5 deletions

View File

@ -11,10 +11,42 @@
namespace Common::Random
{
class CSPRNG final
struct PRNG::Impl
{
Impl(void* seed, std::size_t size)
{
mbedtls_hmac_drbg_init(&m_context);
const int ret = mbedtls_hmac_drbg_seed_buf(
&m_context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), static_cast<u8*>(seed), size);
ASSERT(ret == 0);
}
~Impl() { mbedtls_hmac_drbg_free(&m_context); }
void Generate(void* buffer, std::size_t size)
{
const int ret = mbedtls_hmac_drbg_random(&m_context, static_cast<u8*>(buffer), size);
ASSERT(ret == 0);
}
mbedtls_hmac_drbg_context m_context;
};
PRNG::PRNG(void* seed, std::size_t size) : m_impl(std::make_unique<Impl>(seed, size))
{
}
PRNG::~PRNG() = default;
void PRNG::Generate(void* buffer, std::size_t size)
{
m_impl->Generate(buffer, size);
}
class EntropySeededPRNG final
{
public:
CSPRNG()
EntropySeededPRNG()
{
mbedtls_entropy_init(&m_entropy);
mbedtls_hmac_drbg_init(&m_context);
@ -23,7 +55,7 @@ public:
ASSERT(ret == 0);
}
~CSPRNG()
~EntropySeededPRNG()
{
mbedtls_hmac_drbg_free(&m_context);
mbedtls_entropy_free(&m_entropy);
@ -40,10 +72,10 @@ private:
mbedtls_hmac_drbg_context m_context;
};
static thread_local CSPRNG s_csprng;
static thread_local EntropySeededPRNG s_esprng;
void Generate(void* buffer, std::size_t size)
{
s_csprng.Generate(buffer, size);
s_esprng.Generate(buffer, size);
}
} // namespace Common::Random

View File

@ -5,12 +5,37 @@
#pragma once
#include <cstddef>
#include <memory>
#include <type_traits>
#include "Common/CommonTypes.h"
namespace Common::Random
{
/// Cryptographically secure pseudo-random number generator, with explicit seed.
class PRNG final
{
public:
explicit PRNG(u64 seed) : PRNG(&seed, sizeof(u64)) {}
PRNG(void* seed, std::size_t size);
~PRNG();
void Generate(void* buffer, std::size_t size);
template <typename T>
T GenerateValue()
{
static_assert(std::is_arithmetic<T>(), "T must be an arithmetic type in GenerateValue.");
T value;
Generate(&value, sizeof(value));
return value;
}
private:
struct Impl;
std::unique_ptr<Impl> m_impl;
};
/// Fill `buffer` with random bytes using a cryptographically secure pseudo-random number generator.
void Generate(void* buffer, std::size_t size);

View File

@ -19,4 +19,8 @@ if(_M_X86)
PowerPC/Jit64Common/ConvertDoubleToSingle.cpp
PowerPC/Jit64Common/Frsqrte.cpp
)
elseif(_M_ARM_64)
add_dolphin_test(PowerPCTest
PowerPC/JitArm64/MovI2R.cpp
)
endif()

View File

@ -0,0 +1,97 @@
// Copyright 2021 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <cstddef>
#include <type_traits>
#include "Common/Arm64Emitter.h"
#include "Common/Assert.h"
#include "Common/BitUtils.h"
#include "Common/Random.h"
#include <gtest/gtest.h>
namespace
{
using namespace Arm64Gen;
class TestMovI2R : public ARM64CodeBlock
{
public:
TestMovI2R() { AllocCodeSpace(4096); }
void Check32(u32 value)
{
ResetCodePtr();
const u8* fn = GetCodePtr();
MOVI2R(W0, value);
RET();
FlushIcacheSection(const_cast<u8*>(fn), const_cast<u8*>(GetCodePtr()));
const u64 result = Common::BitCast<u64 (*)()>(fn)();
EXPECT_EQ(value, result);
}
void Check64(u64 value)
{
ResetCodePtr();
const u8* fn = GetCodePtr();
MOVI2R(X0, value);
RET();
FlushIcacheSection(const_cast<u8*>(fn), const_cast<u8*>(GetCodePtr()));
const u64 result = Common::BitCast<u64 (*)()>(fn)();
EXPECT_EQ(value, result);
}
};
} // namespace
TEST(JitArm64, MovI2R_32BitValues)
{
Common::Random::PRNG rng{0};
TestMovI2R test;
for (u64 i = 0; i < 0x100000; i++)
{
const u32 value = rng.GenerateValue<u32>();
test.Check32(value);
test.Check64(value);
}
}
TEST(JitArm64, MovI2R_Rand)
{
Common::Random::PRNG rng{0};
TestMovI2R test;
for (u64 i = 0; i < 0x100000; i++)
{
test.Check64(rng.GenerateValue<u64>());
}
}
TEST(JitArm64, MovI2R_ADP)
{
TestMovI2R test;
const u64 base = Common::BitCast<u64>(test.GetCodePtr());
for (s64 i = -0x20000; i < 0x20000; i++)
{
const u64 offset = static_cast<u64>(i);
test.Check64(base + offset);
}
}
TEST(JitArm64, MovI2R_ADRP)
{
TestMovI2R test;
const u64 base = Common::BitCast<u64>(test.GetCodePtr()) & ~0xFFF;
for (s64 i = -0x20000; i < 0x20000; i++)
{
const u64 offset = static_cast<u64>(i) << 12;
test.Check64(base + offset);
}
}