Results: Implement namespaced, type-safe results.

Because I was working on multiple things at once, this commit also:
- Adds wrappers for/linker flags to wrap CXX exceptions to make them
  abort. This saves ~0x8000 of memory in every system module.
- Broadly replaces lines of the pattern if (cond) { return ResultX; }
  with R_UNLESS(!cond, ResultX());.
- Reworks the R_TRY_CATCH macros (and the result macros in general).
This commit is contained in:
Michael Scire 2019-10-24 01:40:44 -07:00 committed by SciresM
parent 15773e4755
commit 4059dc6187
169 changed files with 2172 additions and 1868 deletions

View file

@ -40,29 +40,24 @@ namespace sts::ldr {
/* Helpers. */
Result ValidateSubregion(size_t allowed_start, size_t allowed_end, size_t start, size_t size, size_t min_size = 0) {
if (!(size >= min_size && allowed_start <= start && start <= allowed_end && start + size <= allowed_end)) {
return ResultLoaderInvalidMeta;
}
return ResultSuccess;
R_UNLESS(size >= min_size, ResultInvalidMeta());
R_UNLESS(allowed_start <= start, ResultInvalidMeta());
R_UNLESS(start <= allowed_end, ResultInvalidMeta());
R_UNLESS(start + size <= allowed_end, ResultInvalidMeta());
return ResultSuccess();
}
Result ValidateNpdm(const Npdm *npdm, size_t size) {
/* Validate magic. */
if (npdm->magic != Npdm::Magic) {
return ResultLoaderInvalidMeta;
}
R_UNLESS(npdm->magic == Npdm::Magic, ResultInvalidMeta());
/* Validate flags. */
if (hos::GetVersion() >= hos::Version_700) {
/* 7.0.0 added 0x10 as a valid bit to NPDM flags. */
if (npdm->flags & ~0x1F) {
return ResultLoaderInvalidMeta;
}
} else {
if (npdm->flags & ~0xF) {
return ResultLoaderInvalidMeta;
}
u32 mask = ~0x1F;
if (hos::GetVersion() < hos::Version_700) {
/* 7.0.0 added 0x10 as a valid bit to NPDM flags, so before that we only check 0xF. */
mask = ~0xF;
}
R_UNLESS(!(npdm->flags & mask), ResultInvalidMeta());
/* Validate Acid extents. */
R_TRY(ValidateSubregion(sizeof(Npdm), size, npdm->acid_offset, npdm->acid_size, sizeof(Acid)));
@ -70,14 +65,12 @@ namespace sts::ldr {
/* Validate Aci extends. */
R_TRY(ValidateSubregion(sizeof(Npdm), size, npdm->aci_offset, npdm->aci_size, sizeof(Aci)));
return ResultSuccess;
return ResultSuccess();
}
Result ValidateAcid(const Acid *acid, size_t size) {
/* Validate magic. */
if (acid->magic != Acid::Magic) {
return ResultLoaderInvalidMeta;
}
R_UNLESS(acid->magic == Acid::Magic, ResultInvalidMeta());
/* TODO: Check if retail flag is set if not development hardware. */
@ -86,21 +79,19 @@ namespace sts::ldr {
R_TRY(ValidateSubregion(sizeof(Acid), size, acid->sac_offset, acid->sac_size));
R_TRY(ValidateSubregion(sizeof(Acid), size, acid->kac_offset, acid->kac_size));
return ResultSuccess;
return ResultSuccess();
}
Result ValidateAci(const Aci *aci, size_t size) {
/* Validate magic. */
if (aci->magic != Aci::Magic) {
return ResultLoaderInvalidMeta;
}
R_UNLESS(aci->magic == Aci::Magic, ResultInvalidMeta());
/* Validate Fah, Sac, Kac. */
R_TRY(ValidateSubregion(sizeof(Aci), size, aci->fah_offset, aci->fah_size));
R_TRY(ValidateSubregion(sizeof(Aci), size, aci->sac_offset, aci->sac_size));
R_TRY(ValidateSubregion(sizeof(Aci), size, aci->kac_offset, aci->kac_size));
return ResultSuccess;
return ResultSuccess();
}
Result LoadMetaFromFile(FILE *f, MetaCache *cache) {
@ -116,15 +107,12 @@ namespace sts::ldr {
fseek(f, 0, SEEK_SET);
/* Read data into cache buffer. */
if (npdm_size > MetaCacheBufferSize || fread(cache->buffer, npdm_size, 1, f) != 1) {
return ResultLoaderTooLargeMeta;
}
R_UNLESS(npdm_size <= MetaCacheBufferSize, ResultTooLargeMeta());
R_UNLESS(fread(cache->buffer, npdm_size, 1, f) == 1, ResultTooLargeMeta());
}
/* Ensure size is big enough. */
if (npdm_size < sizeof(Npdm)) {
return ResultLoaderInvalidMeta;
}
R_UNLESS(npdm_size >= sizeof(Npdm), ResultInvalidMeta());
/* Validate the meta. */
{
@ -152,7 +140,7 @@ namespace sts::ldr {
meta->aci_kac = reinterpret_cast<u8 *>(aci) + aci->kac_offset;
}
return ResultSuccess;
return ResultSuccess();
}
}
@ -198,7 +186,7 @@ namespace sts::ldr {
g_cached_title_id = title_id;
*out_meta = *meta;
return ResultSuccess;
return ResultSuccess();
}
Result LoadMetaFromCache(Meta *out_meta, ncm::TitleId title_id) {
@ -206,7 +194,7 @@ namespace sts::ldr {
return LoadMeta(out_meta, title_id);
}
*out_meta = g_meta_cache.meta;
return ResultSuccess;
return ResultSuccess();
}
void InvalidateMetaCache() {