mem: implement most of StandardAllocator

This was tested using `https://github.com/node-dot-cpp/alloc-test` plus a few other by-hand tests.

It seems to work for the case we care about (sysmodules without thread cache-ing).

External users are advised to build with assertions on and contact SciresM if you find issues.

This is a lot of code to have gotten right in one go, and it was written mostly after midnight while sick, so there are probably un-noticed issues.
This commit is contained in:
Michael Scire
2020-03-28 16:20:00 -07:00
parent 9bb5af9823
commit cc24a33600
47 changed files with 5473 additions and 43 deletions

View File

@@ -46,11 +46,11 @@ namespace ams::diag {
#endif
#ifdef AMS_ENABLE_ASSERTIONS
#define AMS_ASSERT_IMPL(expr, ...) \
({ \
if (const bool __tmp_ams_assert_val = (expr); AMS_UNLIKELY(!__tmp_ams_assert_val)) { \
AMS_CALL_ASSERT_FAIL_IMPL(#expr, ## __VA_ARGS__); \
} \
#define AMS_ASSERT_IMPL(expr, ...) \
({ \
if (const bool __tmp_ams_assert_val = static_cast<bool>(expr); AMS_UNLIKELY(!__tmp_ams_assert_val)) { \
AMS_CALL_ASSERT_FAIL_IMPL(#expr, ## __VA_ARGS__); \
} \
})
#else
#define AMS_ASSERT_IMPL(expr, ...) AMS_UNUSED(expr, ## __VA_ARGS__)
@@ -68,9 +68,9 @@ namespace ams::diag {
#define AMS_ABORT(...) AMS_CALL_ABORT_IMPL("", ## __VA_ARGS__)
#define AMS_ABORT_UNLESS(expr, ...) \
({ \
if (const bool __tmp_ams_assert_val = (expr); AMS_UNLIKELY(!__tmp_ams_assert_val)) { \
AMS_CALL_ABORT_IMPL(#expr, ##__VA_ARGS__); \
} \
#define AMS_ABORT_UNLESS(expr, ...) \
({ \
if (const bool __tmp_ams_assert_val = static_cast<bool>(expr); AMS_UNLIKELY(!__tmp_ams_assert_val)) { \
AMS_CALL_ABORT_IMPL(#expr, ##__VA_ARGS__); \
} \
})

View File

@@ -19,6 +19,7 @@
#include <vapours/crypto/crypto_memory_compare.hpp>
#include <vapours/crypto/crypto_memory_clear.hpp>
#include <vapours/crypto/crypto_sha1_generator.hpp>
#include <vapours/crypto/crypto_sha256_generator.hpp>
#include <vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp>
#include <vapours/crypto/crypto_rsa_oaep_sha256_decoder.hpp>

View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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/>.
*/
#pragma once
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util.hpp>
#include <vapours/crypto/impl/crypto_sha1_impl.hpp>
namespace ams::crypto {
class Sha1Generator {
NON_COPYABLE(Sha1Generator);
NON_MOVEABLE(Sha1Generator);
private:
using Impl = impl::Sha1Impl;
public:
static constexpr size_t HashSize = Impl::HashSize;
static constexpr size_t BlockSize = Impl::BlockSize;
static constexpr inline u8 Asn1Identifier[] = {
0x30, 0x21, /* Sequence, size 0x21 */
0x30, 0x09, /* Sequence, size 0x09 */
0x06, 0x05, /* Object Identifier */
0x2B, 0x0E, 0x03, 0x02, 0x1A, /* SHA-1 */
0x05, 0x00, /* Null */
0x04, 0x14, /* Octet string, size 0x14 */
};
static constexpr size_t Asn1IdentifierSize = util::size(Asn1Identifier);
private:
Impl impl;
public:
Sha1Generator() { /* ... */ }
void Initialize() {
this->impl.Initialize();
}
void Update(const void *data, size_t size) {
this->impl.Update(data, size);
}
void GetHash(void *dst, size_t size) {
this->impl.GetHash(dst, size);
}
};
void GenerateSha1Hash(void *dst, size_t dst_size, const void *src, size_t src_size);
}

View File

@@ -28,6 +28,8 @@ namespace ams::crypto {
};
class Sha256Generator {
NON_COPYABLE(Sha256Generator);
NON_MOVEABLE(Sha256Generator);
private:
using Impl = impl::Sha256Impl;
public:
@@ -39,7 +41,7 @@ namespace ams::crypto {
0x30, 0x0D, /* Sequence, size 0x0D */
0x06, 0x09, /* Object Identifier */
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* SHA-256 */
0x00, /* Null */
0x05, 0x00, /* Null */
0x04, 0x20, /* Octet string, size 0x20 */
};
static constexpr size_t Asn1IdentifierSize = util::size(Asn1Identifier);

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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/>.
*/
#pragma once
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util.hpp>
#include <vapours/crypto/impl/crypto_hash_function.hpp>
#include <vapours/crypto/crypto_memory_clear.hpp>
namespace ams::crypto::impl {
class Sha1Impl {
public:
static constexpr size_t HashSize = 0x14;
static constexpr size_t BlockSize = 0x40;
private:
struct State {
u32 intermediate_hash[HashSize / sizeof(u32)];
u8 buffer[BlockSize];
u64 bits_consumed;
size_t num_buffered;
bool finalized;
};
private:
State state;
public:
Sha1Impl() { /* ... */ }
~Sha1Impl() {
static_assert(std::is_trivially_destructible<State>::value);
ClearMemory(std::addressof(this->state), sizeof(this->state));
}
void Initialize();
void Update(const void *data, size_t size);
void GetHash(void *dst, size_t size);
};
/* static_assert(HashFunction<Sha1Impl>); */
}

View File

@@ -60,6 +60,8 @@ namespace ams::svc {
T pointer;
public:
constexpr ALWAYS_INLINE UserPointer(T p) : pointer(p) { /* ... */ }
constexpr ALWAYS_INLINE T GetPointerUnsafe() { return this->pointer; }
};
template<typename T>
@@ -168,11 +170,11 @@ namespace ams::svc {
InitialProcessIdRangeInfo_Maximum = 1,
};
enum PhysicalMemoryInfo : u64 {
PhysicalMemoryInfo_Application = 0,
PhysicalMemoryInfo_Applet = 1,
PhysicalMemoryInfo_System = 2,
PhysicalMemoryInfo_SystemUnsafe = 3,
enum PhysicalMemorySystemInfo : u64 {
PhysicalMemorySystemInfo_Application = 0,
PhysicalMemorySystemInfo_Applet = 1,
PhysicalMemorySystemInfo_System = 2,
PhysicalMemorySystemInfo_SystemUnsafe = 3,
};
enum LastThreadInfoFlag : u32 {