Skip to content

fix(libSceNpCommon): sceNpCmpNpIdInOrder refuses uncanonicalized NpIds with 0x80550605 - #24693

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
olegon85:fix/libscenpcommon-cmpnpidinorder-validates-npid-flag
Nov 21, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
olegon85:fix/libscenpcommon-cmpnpidinorder-validates-npid-flag

Conversation

@olegon85

Copy link
Copy Markdown
Contributor

What

sceNpCmpNpIdInOrder compared any two NpIds. A payload process on a PS5 Pro (firmware 13.60) refused the call with 0x80550605 (NP_UTIL_ERROR_INVALID_NP_ID) whenever either operand's reserved[0] was not the canonicalized marker 1 that sceNpUtilJidToNpId writes, leaving the order output untouched, and accepted pairs where both carried it (order -1/0/1 by handle, then opt, then reserved as before). The check is added after the null-pointer and unmapped-copy errors, matching the measured precedence: a null order pointer still gives 0x80550003 even for uncanonicalized ids.

The same session confirmed the rest of this utility family against the implementation - platform tags read and written at npid offset 24 with no tag answering 0, unknown and comp answering 0x80550004 publicly and 4 through sceNpIntGetPlatformType, boolean online-ID validity, null-pointer answers - so the debt entry now states measured facts. Those siblings needed no code change.

Probe sources and raw session output are in the first comment, including the note that an earlier run of the same session used a three-byte-short struct for manual cases; all claims here rest on the corrected re-run. Measurement context: kernel_get_fw_version = 0x13600007, payload process over elfldr (port 9021), sessions 2026-11-20T23:06Z and 23:18Z UTC; a title process was not measured.

Tested

  • Console: probes tw4/tw4b, one flushed STEP line before every call, both exited by themselves; the order matrix (equal, handle-less, tag-less, flag missing on either or both sides) is quoted in full in the first comment.
  • Server (Linux): guest_np_common_tests - 7/7 passed with the change on 01ac7c81be; the fixture now sets the marker on ids meant to compare and pins the two refusal cases with an untouched order value.
  • python3 tools/check_conventions.py --base origin/main: passed. git diff --check: clean. python3 tools/pr_overlap.py --branch fix/libscenpcommon-cmpnpidinorder-validates-npid-flag: no overlap with open pull requests (all 2297 probed; three base-conflicting shares merged and compared clean). Own sibling docs PR on docs/npcommon-utils-measured-1360 edits four other entries of the same debt file, disjoint from this one.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@olegon85

Copy link
Copy Markdown
Contributor Author

Console evidence

Measurement context: PS5 Pro, kernel_get_fw_version = 0x13600007 (firmware 13.60), payload process loaded over elfldr (port 9021); a title process was not measured. Sessions 2026-11-20T23:06:28Z (tw4) and 23:18:59Z (tw4b). Every call is preceded by a flushed STEP line; both probes exited by themselves.

First session note: tw4 declared the NpId with a 17-byte online ID, three bytes short of the console's field layout (online ID 0-16, label pair 20-23, platform tag 24-27, canonicalized marker 28), so its manual-tag and flag cases landed one field early; its SetPlatformType round-trips, JidToNpId, ParseJid and Cmp/IsValid results were layout-independent and stand. tw4b repeated every affected case with the correct 36-byte layout - that is the session this PR's claims rest on.

Raw output of tw4b (platform matrix, canonicalize matrix, order validation, 16-character JID, raw system software version):

T4B|FW|0x13600007
T4B|SYM|827a3c3c0 827a3c970 827a3c410 827a3c820 827a3c6d0 827a43a30
T4B|STEP|empty
T4B|empty|gp=0x0|igp=0x0
T4B|STEP|ps3
T4B|ps3|gp=0x1|igp=0x1
T4B|STEP|psp2
T4B|psp2|gp=0x2|igp=0x2
T4B|STEP|ps4
T4B|ps4|gp=0x3|igp=0x3
T4B|STEP|comp
T4B|comp|gp=0x80550004|igp=0x4
T4B|STEP|junk
T4B|junk|gp=0x80550004|igp=0x80550004
T4B|STEP|c-flag0
T4B|c-flag0|rc=0x80550605
T4B|c-flag0|00000000000000000000000000000000
T4B|STEP|c-flag1-repl1-empty
T4B|c-flag1-repl1-empty|rc=0x0
T4B|c-flag1-repl1-empty|00000000707334000100000000000000
T4B|STEP|c-flag1-repl0-empty
T4B|c-flag1-repl0-empty|rc=0x0
T4B|c-flag1-repl0-empty|00000000707334000100000000000000
T4B|STEP|c-flag1-repl1-ps3
T4B|c-flag1-repl1-ps3|rc=0x0
T4B|c-flag1-repl1-ps3|00000000707334000100000000000000
T4B|STEP|c-flag1-repl0-ps3
T4B|c-flag1-repl0-ps3|rc=0x0
T4B|c-flag1-repl0-ps3|00000000707333000100000000000000
T4B|STEP|c-flag1-repl1-comp
T4B|c-flag1-repl1-comp|rc=0x0
T4B|c-flag1-repl1-comp|00000000707334000100000000000000
T4B|STEP|c-set-comp-repl1
T4B|c-set-pre|rc=0x0
T4B|c-set-comp-repl1|rc=0x0
T4B|css|0000000070733400
T4B|STEP|o-eq
T4B|o-eq|rc=0x0|o=0
T4B|STEP|o-lt
T4B|o-lt|rc=0x0|o=-1
T4B|STEP|o-taglt
T4B|o-taglt|rc=0x0|o=-1
T4B|STEP|o-flag0a
T4B|o-flag0a|rc=0x80550605|o=1515870810
T4B|STEP|o-flag0b
T4B|o-flag0b|rc=0x80550605|o=1515870810
T4B|STEP|o-flag0both
T4B|o-flag0both|rc=0x80550605|o=1515870810
T4B|STEP|t-16char
T4B|t-16char|rc=0x0
T4B|t16|414141414141414141414141414141410000000061616262726573310100000000000000
T4B|SWV|818b05240
T4B|STEP|swversion
T4B|swversion|rc=0x0
T4B|swv|280000000000000031332e3539302e30303100000000000000000000000000000000000001005913eeeeeeeeeeeeeeee
T4B|DONE

Raw output of tw4 (versions, jid parsing incl. the no-@ and trailing-dots quirks, cmp/valid matrix):

TW4|FW|0x13600007
TW4|LIB|880af2e20|ok
TW4|SYM|8228fc860 8228fc950 8228fc3c0 8228fc970 8228fc410 8228fc9d0 8228fc820 8228fc7e0 8228fc4b0 8228fc6d0 8229045c0 8228fcb60 822903a30
TW4|STEP|GetSdkVersion
TW4|sdkver|31332e3539000000
TW4|STEP|GetNavSdkVersion
TW4|navver|30302e3832000000
TW4|STEP|gp-empty
TW4|gp-empty|rc=0x0
TW4|STEP|gp-ps3
TW4|gp-ps3|rc=0x0
TW4|STEP|gp-psp2
TW4|gp-psp2|rc=0x80550004
TW4|STEP|gp-ps4
TW4|gp-ps4|rc=0x0
TW4|STEP|gp-comp
TW4|gp-comp|rc=0x80550004
TW4|STEP|gp-null
TW4|gp-null|rc=0x80550003
TW4|STEP|igp-comp
TW4|igp-comp|rc=0x80550004
TW4|STEP|igp-empty
TW4|igp-empty|rc=0x0
TW4|STEP|sp-0
TW4|sp-0|rc=0x0|TW4||544553544944303100000000000000000000000000000000000000000000000000
TW4|sp-0|get=0x0
TW4|STEP|sp-1
TW4|sp-1|rc=0x0|TW4||544553544944303100000000000000000000000000000000707333000000000000
TW4|sp-1|get=0x1
TW4|STEP|sp-2
TW4|sp-2|rc=0x0|TW4||544553544944303100000000000000000000000000000000707370320000000000
TW4|sp-2|get=0x2
TW4|STEP|sp-3
TW4|sp-3|rc=0x0|TW4||544553544944303100000000000000000000000000000000707334000000000000
TW4|sp-3|get=0x3
TW4|STEP|sp-4
TW4|sp-4|rc=0x80550004|TW4||544553544944303100000000000000000000000000000000000100000000000000
TW4|sp-4|get=0x0
TW4|STEP|isp-4
TW4|isp-4|rc=0x0|igp=0x4
TW4|STEP|c-res0
TW4|c-res0|rc=0x80550605
TW4|c-res0|544553544944303100000000000000000000000000000000000000000000000000
TW4|STEP|c-repl1-empty
TW4|c-repl1-empty|rc=0x80550605
TW4|c-repl1-empty|544553544944303100000000000000000000000000000000000000000000000000
TW4|STEP|c-repl0-empty
TW4|c-repl0-empty|rc=0x80550605
TW4|c-repl0-empty|544553544944303100000000000000000000000000000000000100000000000000
TW4|STEP|c-repl1-ps3
TW4|c-repl1-ps3|rc=0x80550605
TW4|c-repl1-ps3|544553544944303100000000000000000000000000707333000000000000000000
TW4|STEP|c-repl0-ps3
TW4|c-repl0-ps3|rc=0x80550605
TW4|c-repl0-ps3|544553544944303100000000000000000000000000707333000100000000000000
TW4|STEP|c2-repl1-empty
TW4|c2-repl1-empty|rc=0x80550605
TW4|c2|544553544944303100000000000000000000000000000000000000000000000000
TW4|STEP|cmp-eq
TW4|cmp-eq|rc=0x0
TW4|STEP|cmp-ne
TW4|cmp-ne|rc=0x80550609
TW4|STEP|cmp-null
TW4|cmp-null|rc=0x80550003
TW4|STEP|ord-eq
TW4|ord-eq|rc=0x80550605|o=1515870810
TW4|STEP|ord-lt
TW4|ord-lt|rc=0x80550605|o=1515870810
TW4|STEP|ord-opt
TW4|ord-opt|rc=0x80550605|o=1515870810
TW4|STEP|ord-nullout
TW4|ord-nullout|rc=0x80550003
TW4|STEP|val-null
TW4|val-null|rc=0x0
TW4|STEP|val-ok8
TW4|val-ok8|rc=0x1
TW4|STEP|val-short2
TW4|val-short2|rc=0x0
TW4|STEP|val-nonul16
TW4|val-nonul16|rc=0x0
TW4|STEP|p-ok
TW4|p-ok|rc=0x0|req=36
TW4|p-ok|u=0 d=6 r=31
TW4|p-ok|41422e43440061612e62622e63632e706c617973746174696f6e2e6e6574007265733100eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
TW4|STEP|p-small
TW4|p-small|rc=0x80550602|req=0
TW4|p-small|u=-1 d=-1 r=-1
TW4|STEP|p-noat
TW4|p-noat|rc=0x0|req=9
TW4|p-noat|u=0 d=1 r=8
TW4|p-noat|006162636465660000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
TW4|STEP|p-dots
TW4|p-dots|rc=0x0|req=33
TW4|p-dots|u=0 d=2 r=27
TW4|p-dots|750061612e62622e63632e706c617973746174696f6e2e6e6574002e2f72657300eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
TW4|STEP|t-ok
TW4|t-ok|rc=0x0
TW4|t-ok|41422e434400000000000000000000000000000061616262726573310100000000
TW4|STEP|t-baddom
TW4|t-baddom|rc=0x80550604
TW4|t-baddom|000000000000000000000000000000000000000000000000000000000000000000
TW4|STEP|t-longuser
TW4|t-longuser|rc=0x80550604
TW4|t-longuser|000000000000000000000000000000000000000000000000000000000000000000
TW4|STEP|t-res5
TW4|t-res5|rc=0x80550604
TW4|t-res5|000000000000000000000000000000000000000000000000000000000000000000
TW4|STEP|t-nullr
TW4|t-nullr|rc=0x0
TW4|t-nullr|41422e434400000000000000000000000000000061616262000000000100000000
TW4|DONE

Probe sources:

tw4b/main.c
#include <ps5/kernel.h>
#include <dlfcn.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

typedef struct { char data[20]; uint8_t opt[8]; uint8_t reserved[8]; } NpId;

typedef int (*GetPlatFn)(const NpId*);
typedef int (*SetPlatFn)(NpId*, uint32_t);
typedef int (*CanonFn)(NpId*, int32_t);
typedef int (*OrderFn)(const NpId*, const NpId*, int32_t*);
typedef int (*JidToNpIdFn)(NpId*, const void*);
typedef int (*SwVerFn)(void*);

static void step(const char* what) {
    printf("T4B|STEP|%s\n", what);
    fflush(stdout);
}

static void dump(const char* id, const void* p, unsigned n) {
    const uint8_t* b = (const uint8_t*)p;
    printf("T4B|%s|", id);
    for (unsigned i = 0; i < n; ++i) printf("%02x", b[i]);
    printf("\n");
    fflush(stdout);
}

static void put(NpId* np, const char* online, const char* tag4, uint8_t flag) {
    memset(np, 0, sizeof(*np));
    strcpy(np->data, online);
    if (tag4) memcpy(np->opt + 4, tag4, 4);
    np->reserved[0] = flag;
}

int main(void) {
    printf("T4B|FW|0x%x\n", kernel_get_fw_version());
    fflush(stdout);

    void* h = dlopen("libSceNpCommon.sprx", RTLD_LAZY);
    GetPlatFn GETPLAT = (GetPlatFn)dlsym(h, "sceNpGetPlatformType");
    GetPlatFn IGETPLAT = (GetPlatFn)dlsym(h, "sceNpIntGetPlatformType");
    SetPlatFn SETPLAT = (SetPlatFn)dlsym(h, "sceNpSetPlatformType");
    CanonFn CANPS4 = (CanonFn)dlsym(h, "sceNpUtilCanonicalizeNpIdForPs4");
    OrderFn ORDER = (OrderFn)dlsym(h, "sceNpCmpNpIdInOrder");
    JidToNpIdFn TOID = (JidToNpIdFn)dlsym(h, "sceNpUtilJidToNpId");
    printf("T4B|SYM|%p %p %p %p %p %p\n", (void*)GETPLAT, (void*)IGETPLAT, (void*)SETPLAT,
           (void*)CANPS4, (void*)ORDER, (void*)TOID);
    fflush(stdout);

    NpId np;
    if (GETPLAT && IGETPLAT) {
        struct { const char* n; const char* tag; } cases[] = {
            {"empty", 0}, {"ps3", "ps3"}, {"psp2", "psp2"}, {"ps4", "ps4"},
            {"comp", "comp"}, {"junk", "zzzz"}
        };
        for (unsigned i = 0; i < 6; ++i) {
            step(cases[i].n);
            put(&np, "TESTID01", cases[i].tag, 1);
            printf("T4B|%s|gp=0x%x|igp=0x%x\n", cases[i].n,
                   (unsigned)GETPLAT(&np), (unsigned)IGETPLAT(&np));
            fflush(stdout);
        }
    }
    if (CANPS4) {
        struct { const char* n; const char* tag; uint8_t flag; int32_t repl; } cases[] = {
            {"c-flag0", 0, 0, 1}, {"c-flag1-repl1-empty", 0, 1, 1}, {"c-flag1-repl0-empty", 0, 1, 0},
            {"c-flag1-repl1-ps3", "ps3", 1, 1}, {"c-flag1-repl0-ps3", "ps3", 1, 0},
            {"c-flag1-repl1-comp", "comp", 1, 1}
        };
        for (unsigned i = 0; i < 6; ++i) {
            step(cases[i].n);
            put(&np, "TESTID01", cases[i].tag, cases[i].flag);
            int rc = CANPS4(&np, cases[i].repl);
            printf("T4B|%s|rc=0x%x\n", cases[i].n, (unsigned)rc);
            dump(cases[i].n, &np.opt[0], 16);
            fflush(stdout);
        }
    }
    if (SETPLAT && CANPS4) {
        step("c-set-comp-repl1");
        put(&np, "TESTID01", 0, 1);
        int rc = SETPLAT(&np, 3);
        printf("T4B|c-set-pre|rc=0x%x\n", (unsigned)rc);
        rc = CANPS4(&np, 1);
        printf("T4B|c-set-comp-repl1|rc=0x%x\n", (unsigned)rc);
        dump("css", np.opt, 8);
        fflush(stdout);
    }
    if (ORDER) {
        NpId a, b;
        int32_t o;
        struct { const char* n; const char* da; const char* db; const char* ta; const char* tb; uint8_t fa; uint8_t fb; } cases[] = {
            {"o-eq", "AAAA", "AAAA", 0, 0, 1, 1},
            {"o-lt", "AAAA", "AAAB", 0, 0, 1, 1},
            {"o-taglt", "AAAA", "AAAA", "ps3", "ps4", 1, 1},
            {"o-flag0a", "AAAA", "AAAA", 0, 0, 0, 1},
            {"o-flag0b", "AAAA", "AAAA", 0, 0, 1, 0},
            {"o-flag0both", "AAAA", "AAAA", 0, 0, 0, 0}
        };
        for (unsigned i = 0; i < 6; ++i) {
            step(cases[i].n);
            put(&a, cases[i].da, cases[i].ta, cases[i].fa);
            put(&b, cases[i].db, cases[i].tb, cases[i].fb);
            o = 0x5A5A5A5A;
            int rc = ORDER(&a, &b, &o);
            printf("T4B|%s|rc=0x%x|o=%d\n", cases[i].n, (unsigned)rc, o);
            fflush(stdout);
        }
    }
    if (TOID) {
        struct { const char* user; const char* domain; const char* resource; } j = {
            "AAAAAAAAAAAAAAAA", "aa.bb.cc.playstation.net", "res1"
        };
        uint64_t jid[4];
        NpId out;
        step("t-16char");
        jid[0] = (uint64_t)(uintptr_t)j.user;
        jid[1] = (uint64_t)(uintptr_t)j.domain;
        jid[2] = (uint64_t)(uintptr_t)j.resource;
        jid[3] = 0;
        memset(&out, 0xEE, sizeof(out));
        int rc = TOID(&out, jid);
        printf("T4B|t-16char|rc=0x%x\n", (unsigned)rc);
        dump("t16", &out, 36);
        fflush(stdout);
    }

    void* lk = dlopen("libkernel_sys.sprx", RTLD_LAZY);
    SwVerFn SWV = (SwVerFn)dlsym(lk, "sceKernelGetSystemSwVersion");
    printf("T4B|SWV|%p\n", (void*)SWV);
    fflush(stdout);
    if (SWV) {
        uint8_t buf[64];
        step("swversion");
        memset(buf, 0xEE, sizeof(buf));
        *(uint64_t*)buf = 40;
        int rc = SWV(buf);
        printf("T4B|swversion|rc=0x%x\n", (unsigned)rc);
        dump("swv", buf, 48);
        fflush(stdout);
    }

    printf("T4B|DONE\n");
    fflush(stdout);
    return 0;
}
tw4/main.c
#include <ps5/kernel.h>
#include <dlfcn.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

typedef struct { char data[17]; uint8_t opt[8]; uint8_t reserved[8]; } NpId;
typedef struct { const char* user; const char* domain; const char* resource; uint64_t reserved; } Jid;

typedef void (*VerFn)(char*);
typedef int (*GetPlatFn)(const NpId*);
typedef int (*SetPlatFn)(NpId*, uint32_t);
typedef int (*CanonFn)(NpId*, int32_t);
typedef int (*CmpIdFn)(const char*, const char*);
typedef int (*OrderFn)(const NpId*, const NpId*, int32_t*);
typedef int (*ValidFn)(const char*);
typedef int (*ParseJidFn)(Jid*, const char*, char*, uint64_t, uint64_t*);
typedef int (*JidToNpIdFn)(NpId*, const Jid*);

static void step(const char* what) {
    printf("TW4|STEP|%s\n", what);
    fflush(stdout);
}

static void dump(const char* id, const void* p, unsigned n) {
    const uint8_t* b = (const uint8_t*)p;
    printf("TW4|%s|", id);
    for (unsigned i = 0; i < n; ++i) printf("%02x", b[i]);
    printf("\n");
    fflush(stdout);
}

static void fill(NpId* np, const char* online, uint32_t tag, uint8_t res0) {
    memset(np, 0, sizeof(*np));
    strcpy(np->data, online);
    memcpy(np->opt + 4, &tag, 4);
    np->reserved[0] = res0;
}

int main(void) {
    printf("TW4|FW|0x%x\n", kernel_get_fw_version());
    fflush(stdout);

    void* h = dlopen("libSceNpCommon.sprx", RTLD_LAZY);
    printf("TW4|LIB|%p|%s\n", h, h ? "ok" : dlerror());
    fflush(stdout);
    if (!h) return 1;

    VerFn GETSDK = (VerFn)dlsym(h, "sceNpGetSdkVersion");
    VerFn GETNAV = (VerFn)dlsym(h, "sceNpGetNavSdkVersion");
    GetPlatFn GETPLAT = (GetPlatFn)dlsym(h, "sceNpGetPlatformType");
    GetPlatFn IGETPLAT = (GetPlatFn)dlsym(h, "sceNpIntGetPlatformType");
    SetPlatFn SETPLAT = (SetPlatFn)dlsym(h, "sceNpSetPlatformType");
    SetPlatFn ISETPLAT = (SetPlatFn)dlsym(h, "sceNpIntSetPlatformType");
    CanonFn CANPS4 = (CanonFn)dlsym(h, "sceNpUtilCanonicalizeNpIdForPs4");
    CanonFn CANPSP2 = (CanonFn)dlsym(h, "sceNpUtilCanonicalizeNpIdForPsp2");
    CmpIdFn CMPID = (CmpIdFn)dlsym(h, "sceNpCmpOnlineId");
    OrderFn ORDER = (OrderFn)dlsym(h, "sceNpCmpNpIdInOrder");
    ValidFn VALID = (ValidFn)dlsym(h, "sceNpIntIsValidOnlineId");
    ParseJidFn PARSE = (ParseJidFn)dlsym(h, "sceNpUtilParseJid");
    JidToNpIdFn TOID = (JidToNpIdFn)dlsym(h, "sceNpUtilJidToNpId");
    printf("TW4|SYM|%p %p %p %p %p %p %p %p %p %p %p %p %p\n",
           (void*)GETSDK, (void*)GETNAV, (void*)GETPLAT, (void*)IGETPLAT,
           (void*)SETPLAT, (void*)ISETPLAT, (void*)CANPS4, (void*)CANPSP2,
           (void*)CMPID, (void*)ORDER, (void*)VALID, (void*)PARSE, (void*)TOID);
    fflush(stdout);

    if (GETSDK) {
        char buf[8];
        memset(buf, 0xAA, sizeof(buf));
        step("GetSdkVersion");
        GETSDK(buf);
        dump("sdkver", buf, 8);
    }
    if (GETNAV) {
        char buf[8];
        memset(buf, 0xAA, sizeof(buf));
        step("GetNavSdkVersion");
        GETNAV(buf);
        dump("navver", buf, 8);
    }

    static const uint32_t TAG_PS3 = 0x00337370u, TAG_PSP2 = 0x32707370u, TAG_PS4 = 0x00347370u, TAG_COMP = 0x706d6f63u;
    NpId np;
    if (GETPLAT) {
        struct { const char* name; uint32_t tag; } cases[] = {
            {"empty", 0}, {"ps3", TAG_PS3}, {"psp2", TAG_PSP2}, {"ps4", TAG_PS4}, {"comp", TAG_COMP}
        };
        for (unsigned i = 0; i < 5; ++i) {
            char id[32];
            snprintf(id, sizeof(id), "gp-%s", cases[i].name);
            step(id);
            fill(&np, "TESTID01", cases[i].tag, 1);
            printf("TW4|%s|rc=0x%x\n", id, (unsigned)GETPLAT(&np));
            fflush(stdout);
        }
        step("gp-null");
        printf("TW4|gp-null|rc=0x%x\n", (unsigned)GETPLAT(0));
        fflush(stdout);
    }
    if (IGETPLAT) {
        fill(&np, "TESTID01", TAG_COMP, 1);
        step("igp-comp");
        printf("TW4|igp-comp|rc=0x%x\n", (unsigned)IGETPLAT(&np));
        fflush(stdout);
        fill(&np, "TESTID01", 0, 1);
        step("igp-empty");
        printf("TW4|igp-empty|rc=0x%x\n", (unsigned)IGETPLAT(&np));
        fflush(stdout);
    }
    if (SETPLAT && GETPLAT) {
        for (uint32_t t = 0; t <= 4; ++t) {
            char id[32];
            snprintf(id, sizeof(id), "sp-%u", t);
            step(id);
            fill(&np, "TESTID01", 0, 1);
            int rc = SETPLAT(&np, t);
            printf("TW4|%s|rc=0x%x|", id, (unsigned)rc);
            dump("", &np, 33);
            printf("TW4|%s|get=0x%x\n", id, (unsigned)GETPLAT(&np));
            fflush(stdout);
        }
    }
    if (ISETPLAT && IGETPLAT) {
        step("isp-4");
        fill(&np, "TESTID01", 0, 1);
        int rc = ISETPLAT(&np, 4);
        printf("TW4|isp-4|rc=0x%x|igp=0x%x\n", (unsigned)rc, (unsigned)IGETPLAT(&np));
        fflush(stdout);
    }
    if (CANPS4) {
        struct { const char* name; uint8_t res0; int32_t repl; uint32_t tag; } cases[] = {
            {"c-res0", 0, 1, 0}, {"c-repl1-empty", 1, 1, 0}, {"c-repl0-empty", 1, 0, 0},
            {"c-repl1-ps3", 1, 1, TAG_PS3}, {"c-repl0-ps3", 1, 0, TAG_PS3}
        };
        for (unsigned i = 0; i < 5; ++i) {
            step(cases[i].name);
            fill(&np, "TESTID01", cases[i].tag, cases[i].res0);
            int rc = CANPS4(&np, cases[i].repl);
            printf("TW4|%s|rc=0x%x\n", cases[i].name, (unsigned)rc);
            dump(cases[i].name, &np, 33);
            fflush(stdout);
        }
    }
    if (CANPSP2) {
        step("c2-repl1-empty");
        fill(&np, "TESTID01", 0, 1);
        int rc = CANPSP2(&np, 1);
        printf("TW4|c2-repl1-empty|rc=0x%x\n", (unsigned)rc);
        dump("c2", &np, 33);
        fflush(stdout);
    }
    if (CMPID) {
        char a[17], b[17];
        step("cmp-eq");
        strcpy(a, "SAMEID"); strcpy(b, "SAMEID");
        printf("TW4|cmp-eq|rc=0x%x\n", (unsigned)CMPID(a, b));
        fflush(stdout);
        step("cmp-ne");
        strcpy(b, "OTHERID");
        printf("TW4|cmp-ne|rc=0x%x\n", (unsigned)CMPID(a, b));
        fflush(stdout);
        step("cmp-null");
        printf("TW4|cmp-null|rc=0x%x\n", (unsigned)CMPID(0, b));
        fflush(stdout);
    }
    if (ORDER) {
        NpId x, y;
        int32_t order = 0x5A5A5A5A;
        step("ord-eq");
        fill(&x, "AAAA", 0, 0); fill(&y, "AAAA", 0, 0);
        printf("TW4|ord-eq|rc=0x%x|o=%d\n", (unsigned)ORDER(&x, &y, &order), order);
        fflush(stdout);
        step("ord-lt");
        fill(&y, "AAAB", 0, 0);
        order = 0x5A5A5A5A;
        printf("TW4|ord-lt|rc=0x%x|o=%d\n", (unsigned)ORDER(&x, &y, &order), order);
        fflush(stdout);
        step("ord-opt");
        fill(&y, "AAAA", TAG_PS3, 0);
        order = 0x5A5A5A5A;
        printf("TW4|ord-opt|rc=0x%x|o=%d\n", (unsigned)ORDER(&x, &y, &order), order);
        fflush(stdout);
        step("ord-nullout");
        printf("TW4|ord-nullout|rc=0x%x\n", (unsigned)ORDER(&x, &y, 0));
        fflush(stdout);
    }
    if (VALID) {
        char s[17];
        step("val-null");
        printf("TW4|val-null|rc=0x%x\n", (unsigned)VALID(0));
        fflush(stdout);
        step("val-ok8");
        memset(s, 0, sizeof(s)); strcpy(s, "ABCDEFGH");
        printf("TW4|val-ok8|rc=0x%x\n", (unsigned)VALID(s));
        fflush(stdout);
        step("val-short2");
        memset(s, 0, sizeof(s)); strcpy(s, "AB");
        printf("TW4|val-short2|rc=0x%x\n", (unsigned)VALID(s));
        fflush(stdout);
        step("val-nonul16");
        memset(s, 'A', 17);
        printf("TW4|val-nonul16|rc=0x%x\n", (unsigned)VALID(s));
        fflush(stdout);
    }

    if (PARSE) {
        struct { const char* name; const char* text; uint64_t size; } cases[] = {
            {"p-ok", "AB.CD@aa.bb.cc.playstation.net/res1", 256},
            {"p-small", "AB.CD@aa.bb.cc.playstation.net/res1", 4},
            {"p-noat", "abcdef", 256},
            {"p-dots", "u@aa.bb.cc.playstation.net../res", 256}
        };
        for (unsigned i = 0; i < 4; ++i) {
            static Jid jid;
            char buf[256];
            uint64_t req = 0;
            memset(&jid, 0, sizeof(jid));
            memset(buf, 0xEE, sizeof(buf));
            step(cases[i].name);
            int rc = PARSE(&jid, cases[i].text, buf, cases[i].size, &req);
            printf("TW4|%s|rc=0x%x|req=%llu\n", cases[i].name, (unsigned)rc, (unsigned long long)req);
            printf("TW4|%s|u=%lld d=%lld r=%lld\n", cases[i].name,
                   jid.user ? (long long)((const char*)jid.user - buf) : -1LL,
                   jid.domain ? (long long)((const char*)jid.domain - buf) : -1LL,
                   jid.resource ? (long long)((const char*)jid.resource - buf) : -1LL);
            fflush(stdout);
            if (rc == 0 && jid.user) dump(cases[i].name, buf, 96);
        }
    }

    if (TOID) {
        NpId out;
        struct { const char* name; const char* u; const char* d; const char* r; } cases[] = {
            {"t-ok", "AB.CD", "aa.bb.cc.playstation.net", "res1"},
            {"t-baddom", "AB.CD", "aa.bb.cc.example.com", "res1"},
            {"t-longuser", "AAAAAAAAAAAAAAAAAAAAAAAAAA", "aa.bb.cc.playstation.net", "res"},
            {"t-res5", "AB.CD", "aa.bb.cc.playstation.net", "res12"},
            {"t-nullr", "AB.CD", "aa.bb.cc.playstation.net", 0}
        };
        for (unsigned i = 0; i < 5; ++i) {
            Jid j;
            step(cases[i].name);
            memset(&j, 0, sizeof(j));
            j.user = cases[i].u; j.domain = cases[i].d; j.resource = cases[i].r;
            memset(&out, 0, sizeof(out));
            int rc = TOID(&out, &j);
            printf("TW4|%s|rc=0x%x\n", cases[i].name, (unsigned)rc);
            dump(cases[i].name, &out, 33);
            fflush(stdout);
        }
    }

    printf("TW4|DONE\n");
    fflush(stdout);
    return 0;
}

TechnicalDebt.md asks pull requests to change debt entries through a fragment. The folded file comes out the same.
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@olegon85 I pushed c933253 on top: as on #24560, I moved the debt entry edit into docs/dev/debt/changes/fix-libscenpcommon-cmpnpidinorder-validates-npid-flag.md and restored docs/dev/debt/libSceNpCommon.md. The folded text is the same as your edit and debt_fold.py --check passes. The code change looks right to me.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — no blocking findings

The changed implementation and tests are consistent with the stated validation behavior, but execution checks were not available in this review session.

What I ran

Read the prepared diff and all three changed files at head c93325348992db834b84656f072658f8e43d9abf using the review reader. No test or build command was run.

Results

Check Result Note
Diff and changed files passed All three changed files read
Tests and build not run Not executable through the available review tools

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • The reported console behavior and environment were not independently verified.
  • Tests, build, conventions, and diff checks reported in the PR description were not rerun.

To get this to a good state

Nothing is required.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against c933253489:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_np_common_tests built cleanly.
  • CTest (guest_np_common_ids, guest_np_common_sync, guest_np_common_clock, guest_np_common_wake):
    1/4 Test #1010: guest_np_common_ids ..............   Passed    0.04 sec
    2/4 Test #1011: guest_np_common_sync .............   Passed    0.06 sec
    3/4 Test #1012: guest_np_common_clock ............   Passed    0.03 sec
    4/4 Test #1013: guest_np_common_wake .............   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 4
    
  • Negative control: with core/libs/prx/libSceNpCommon/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/4 Test #1010: guest_np_common_ids ..............***Failed    0.04 sec
    2/4 Test #1011: guest_np_common_sync .............   Passed    0.05 sec
    3/4 Test #1012: guest_np_common_clock ............   Passed    0.04 sec
    4/4 Test #1013: guest_np_common_wake .............   Passed    0.05 sec
    75% tests passed, 1 tests failed out of 4
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified sceNpCmpNpIdInOrder in core/libs/prx/libSceNpCommon/Export.cpp validates null pointers and copy-in failures before checking canonicalized marker reserved[0] == 1, matching the measured console precedence.
    • Verified sceNpCmpNpIdInOrder returns NP_UTIL_ERROR_INVALID_NP_ID (0x80550605) when localId1.reserved[0] != 1 or localId2.reserved[0] != 1, leaving the guest order output unmodified.
    • Verified comparison logic for handle, opt, and reserved buffers when both NpIdFull structs are valid and canonicalized, preserving lexicographical ordering and copying out the int32_t result.
    • Verified docs/dev/debt/changes/fix-libscenpcommon-cmpnpidinorder-validates-npid-flag.md adheres to debt fragment conventions and was confirmed by maintainer oneandonlydean.
    • Verified core/libs/tests/GuestNpCommon.cpp tests initialize reserved[0] = 1 for normal ordering comparisons and explicitly test invalid/uncanonicalized NpIds with both first and second argument unflagged.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit b089680 into boykopovar:main Nov 21, 2026
14 checks passed
This was referenced Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants