svcReplyAndReceive Invalid Handle?

Discussion in '3DS - Homebrew Development and Emulators' started by gudenau, Apr 4, 2017.

  1. gudenau
    OP

    gudenau Never a unique idea

    Member
    3,257
    1,224
    Jul 7, 2010
    United States
    /dev/random
    I am attempting to create a module for the 3DS, I am using the custom loader as a very basic framework and stripping almost all of the code out and attempting to just get a command to return a version number. In the module svcReplyAndReceive is returning 0xD8E007F7 when the userland program calls srvGetServiceHandle.

    Userland method:
    Code:
    Result gudModuleInit(void){
        Result ret;
     
        if(AtomicPostIncrement(&gudModuleRefCount)){
            return 0;
        }
     
        ret = srvGetServiceHandle(&gudModuleHandle, "gud:mod");
        if(R_FAILED(ret)){
            AtomicDecrement(&gudModuleRefCount);
        }
     
        return ret;
    }
    Module code:
    Code:
    int main(){
        Handle *notificationHandle = &handles[0];
        Handle *serviceHandle = &handles[1];
     
        if(R_FAILED(srvSysRegisterService(serviceHandle, "gud:mod", MAX_SESSIONS))){
            SETR10();
            svcBreak(USERBREAK_ASSERT);
        }
     
        if(R_FAILED(srvSysEnableNotification(notificationHandle))){
            SETR10();
            svcBreak(USERBREAK_ASSERT);
        }
     
        activeHandles = 2;
        s32 index = 1;
        int terminateRequest = 0;
        Handle replyTarget = 0;
        u32* commandBuffer;
        Result result;
        int i;
        Handle handle;
     
        do{
            if(replyTarget == 0){
                commandBuffer = getThreadCommandBuffer();
                commandBuffer[0] = 0xFFFF0000;
            }
        
            result = svcReplyAndReceive(&index, handles, activeHandles, replyTarget);
        
            if(R_FAILED(result)){
                // Check if any handles have been closed
                if(result == (int)0xC920181A){
                    if(index == -1){
                        for(i = 2; i < MAX_SESSIONS + 2; i++){
                            if(handles[i] == replyTarget){
                                index = i;
                                break;
                            }
                        }
                    }
                    svcCloseHandle(handles[index]);
                    handles[index] = handles[activeHandles - 1];
                    activeHandles--;
                    replyTarget = 0;
                }else{
                    __asm__ volatile(
                        "MOV R10, %[marker]; PUSH {r10};"
                        "MOV R10, %[index]; PUSH {r10};"
                        "MOV R10, %[handles]; PUSH {r10};"
                        "MOV R10, %[handleCount]; PUSH {r10};"
                        "MOV R10, %[reply]; PUSH {r10};"
                        "MOV R10, %[line]; PUSH {r10};"
                        "MOV R10, %[res]; PUSH {r10};"
                        "MOV R10, %[level]; PUSH {r10};"
                        "MOV R10, %[summary]; PUSH {r10};"
                        "MOV R10, %[module]; PUSH {r10};"
                        "MOV R10, %[description]; PUSH {r10};"
                        ::
                        [marker] "r" (3735929054),
                        [index] "r" ((u32)index),
                        [handles] "r" ((u32)handles),
                        [handleCount] "r" (activeHandles),
                        [reply] "r" (replyTarget),
                        [line] "r" (__LINE__),
                        [res] "r" (result),
                        [level] "r" (R_LEVEL(result)),
                        [summary] "r" (R_SUMMARY(result)),
                        [module] "r" (R_MODULE(result)),
                        [description] "r" (R_DESCRIPTION(result))
                    
                    );
                    svcBreak(USERBREAK_ASSERT);
                }
            }else{
                // Process responses
                replyTarget = 0;
                switch(index){
                    case 0:{ // Notification
                        if(R_FAILED(shouldTerminate(&terminateRequest))){
                            SETR10();
                            svcBreak(USERBREAK_ASSERT);
                        }
                        break;
                    }
                
                    case 1:{ // New session
                        if(R_FAILED(svcAcceptSession(&handle, *serviceHandle))){
                            SETR10();
                            svcBreak(USERBREAK_ASSERT);
                        }
                        if(activeHandles < MAX_SESSIONS + 2){
                            handles[activeHandles] = handle;
                            activeHandles++;
                        }else{
                            svcCloseHandle(handle);
                        }
                    }
                
                    default:{ // Session
                        handleCommands();
                        replyTarget = handles[index];
                        break;
                    }
                }
            }
        }while(!terminateRequest || activeHandles != 2);
     
        srvSysUnregisterService("gud:mod");
        svcCloseHandle(*serviceHandle);
        svcCloseHandle(*notificationHandle);
     
        return 0;
    }
    
    There is also an issue with the banner, the banner image does not render on the top screen. This is not a huge issue, but I am not sure what I messed up with that either.

    Here is the entire project directory.


    Edit:
    I saw the pointer errors and fixed them in the module, the archive does not reflect these changes.
     
    Last edited by gudenau, Apr 4, 2017