Improve names/documentation for jit kernel patch functions in detect.h/c + codestylefixes

This commit is contained in:
fincs 2019-02-16 16:42:08 +01:00
parent 1cb2c18fa5
commit ee2e19ce38
3 changed files with 35 additions and 37 deletions

View File

@ -21,6 +21,6 @@ bool kernelAbove600(void);
bool detectDebugger(void); bool detectDebugger(void);
/// Returns true if the kernel is patched to allow self-process-jit. /// Returns true if the kernel is patched to allow self-process-jit.
bool detectCfwJitPatch(void); bool detectJitKernelPatch(void);
/// After this has been called, libnx will pretend that CFW is not present. For testing purposes only. /// After this has been called, libnx will ignore the self-process-jit kernel patch. For testing purposes only.
void detectPretendNotCfwForTesting(void); void detectIgnoreJitKernelPatch(void);

View File

@ -14,9 +14,9 @@ static bool g_IsAbove400;
static bool g_IsAbove500; static bool g_IsAbove500;
static bool g_IsAbove600; static bool g_IsAbove600;
static bool g_CfwJitCached = 0; static bool g_JitKernelPatchCached = 0;
static Mutex g_CfwJitMutex; static Mutex g_JitKernelPatchMutex;
static bool g_CfwJitPatchDetected; static bool g_JitKernelPatchDetected;
static void _CacheVersion(void) static void _CacheVersion(void)
{ {
@ -47,43 +47,41 @@ static void _CacheVersion(void)
mutexUnlock(&g_VersionMutex); mutexUnlock(&g_VersionMutex);
} }
static void _CacheCfwJit(void) static void _CacheJitKernelPatch(void)
{ {
if (__atomic_load_n(&g_CfwJitCached, __ATOMIC_SEQ_CST)) if (__atomic_load_n(&g_JitKernelPatchCached, __ATOMIC_SEQ_CST))
return; return;
mutexLock(&g_CfwJitMutex); mutexLock(&g_JitKernelPatchMutex);
if (g_CfwJitCached) { if (g_JitKernelPatchCached) {
mutexUnlock(&g_CfwJitMutex); mutexUnlock(&g_JitKernelPatchMutex);
return; return;
} }
void* heap = memalign(0x1000, 0x1000); void* heap = memalign(0x1000, 0x1000);
if (heap != NULL) if (heap != NULL) {
{ Handle code;
Handle code; Result rc;
Result rc; rc = svcCreateCodeMemory(&code, heap, 0x1000);
rc = svcCreateCodeMemory(&code, heap, 0x1000);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc)) {
{ // On an unpatched kernel on 5.0.0 and above, this would return InvalidMemoryState (0xD401).
// On an unpatched kernel on 5.0.0 and above, this would return 0xD401. // It is not allowed for the creator-process of a CodeMemory object to use svcControlCodeMemory on it.
// It is not allowed for the creator-process of a CodeMemory object to use svcControlCodeMemory on it. // If the patch is present, the function should return InvalidEnumValue (0xF001), because -1 is not a valid enum CodeOperation.
// If the patch is present, the function should return 0xF001, because -1 is not a valid enum CodeOperation. rc = svcControlCodeMemory(code, -1, 0, 0x1000, 0);
rc = svcControlCodeMemory(code, -1, 0, 0x1000, 0);
g_CfwJitPatchDetected = (rc == 0xF001); g_JitKernelPatchDetected = R_VALUE(rc) == KERNELRESULT(InvalidEnumValue);
__atomic_store_n(&g_CfwJitCached, true, __ATOMIC_SEQ_CST); __atomic_store_n(&g_JitKernelPatchCached, true, __ATOMIC_SEQ_CST);
svcCloseHandle(code); svcCloseHandle(code);
} }
free(heap); free(heap);
} }
mutexUnlock(&g_CfwJitMutex); mutexUnlock(&g_JitKernelPatchMutex);
} }
bool kernelAbove200(void) { bool kernelAbove200(void) {
@ -117,14 +115,14 @@ bool detectDebugger(void) {
return !!tmp; return !!tmp;
} }
bool detectCfwJitPatch(void) { bool detectJitKernelPatch(void) {
_CacheCfwJit(); _CacheJitKernelPatch();
return g_CfwJitPatchDetected; return g_JitKernelPatchDetected;
} }
void detectPretendNotCfwForTesting(void) { void detectIgnoreJitKernelPatch(void) {
mutexLock(&g_CfwJitMutex); mutexLock(&g_JitKernelPatchMutex);
g_CfwJitPatchDetected = false; g_JitKernelPatchDetected = false;
__atomic_store_n(&g_CfwJitCached, true, __ATOMIC_SEQ_CST); __atomic_store_n(&g_JitKernelPatchCached, true, __ATOMIC_SEQ_CST);
mutexUnlock(&g_CfwJitMutex); mutexUnlock(&g_JitKernelPatchMutex);
} }

View File

@ -16,7 +16,7 @@ Result jitCreate(Jit* j, size_t size)
// Use new jit primitive introduced in 4.0.0, if available. // Use new jit primitive introduced in 4.0.0, if available.
// Not usable with 5.0.0+ since svcMapJitMemory doesn't allow using that SVC under the same process which owns that object. // Not usable with 5.0.0+ since svcMapJitMemory doesn't allow using that SVC under the same process which owns that object.
if (kernelAbove400() && envIsSyscallHinted(0x4B) && envIsSyscallHinted(0x4C) if (kernelAbove400() && envIsSyscallHinted(0x4B) && envIsSyscallHinted(0x4C)
&& (!kernelAbove500() || detectCfwJitPatch())) { && (!kernelAbove500() || detectJitKernelPatch())) {
type = JitType_JitMemory; type = JitType_JitMemory;
} }
// Fall back to MapProcessCodeMemory if available. // Fall back to MapProcessCodeMemory if available.