Compare commits

...

5 Commits

Author SHA1 Message Date
Devin AI
16e09e974d decomp-assist: nhttp wave-2 levers (volatile second-read, s8-local dest-reg, eval-order, arg-hoist) + walls
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-10-01 08:18:43 +00:00
Devin AI
c785ae323f nhttp recvbuf: RecvBufN exact (volatile reload + stmt order); stdlib compareToken eval-order
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-10-01 08:17:19 +00:00
Devin AI
771c181ae8 nhttp stdlib: strtonum s8-local exact; compareToken operand-order (eval swap); thread ParseHeaderProc buffer-elim
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-10-01 08:14:39 +00:00
Devin AI
b057423184 nhttp: adopt upstream/remote decode; thread ParseHeaderProc buffer-elim (-5); strToHex exact via u8-cast form
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-10-01 08:12:24 +00:00
Devin AI
be3163d11a decomp: nhttp leaf — 8/13 units linked, 5 parked on compiler tie-breaks
Linked (Object(Matching), DOL hash OK):
- NHTTP_bgnend, NHTTP_control, NHTTP_list, NHTTP_os_RVL
- NHTTP_request, NHTTP_response, d_nhttp, d_nhttp_common

Parked NonMatching (all fns 97-99.8% but <100%, documented
compiler tie-breaks — reg-window rotation, unfused beq/b,
li/const scheduling order):
- NHTTP_recvbuf (compareTokenN_HdrRecvBuf)
- NHTTP_socket_RVL (SocRecv_sub)
- NHTTP_stdlib_RVL (strnicmp, compareToken)
- NHTTP_thread (SendProxyConnectMethod, SendProcPostDataRaw,
  ThreadSendProc, ThreadParseHeaderProc)
- d_nhttp_private (NHTTPDeleteConnection)

Headers: nhttp.h real signatures, private/nhttp.h internals,
SOBasic/SOOption additions used by socket unit.
Tools: ctxdiff2/odiff helpers.

Co-Authored-By: wch <usernamecole7@gmail.com>
2026-10-01 08:05:46 +00:00
8 changed files with 161 additions and 52 deletions

View File

@@ -23,17 +23,18 @@ static s8 ReadHeaderChar(const NHTTPResponseInfo* response, NHTTPi_HDRBUFLIST**
}
static int LowerCase(int character) {
if((character>='A') & (character<='Z')) character+=32;
return character;
return (character>='A') & (character<='Z') ? character+32 : character;
}
s32 NHTTPi_findNextLineHdrRecvBuf(const NHTTPResponseInfo* response, s32 position, s32 limit, s32* colon, s32* newlineLength) {
NHTTPi_HDRBUFLIST* block;
s32 offset;
s32 next=-1;
BOOL carriage=FALSE;
s32 next;
BOOL carriage;
if(colon) *colon=-1;
if(position>=limit) return -1;
if(position>=limit) goto done;
next=-1;
carriage=FALSE;
FindHeaderBlock(response,position,&block,&offset);
for(; position<limit; ++position) {
s8 character=ReadHeaderChar(response,&block,&offset);
@@ -56,17 +57,19 @@ s32 NHTTPi_findNextLineHdrRecvBuf(const NHTTPResponseInfo* response, s32 positio
return next;
}
}
done:
return -1;
}
s32 NHTTPi_skipSpaceHdrRecvBuf(const NHTTPResponseInfo* response, s32 position, s32 limit) {
NHTTPi_HDRBUFLIST* block;
s32 offset;
if(position>=limit) return -1;
FindHeaderBlock(response,position,&block,&offset);
for(; position<limit; ++position) {
s8 character=ReadHeaderChar(response,&block,&offset);
if(character!=' ') return position;
if(position<limit) {
FindHeaderBlock(response,position,&block,&offset);
for(; position<limit; ++position) {
s8 character=ReadHeaderChar(response,&block,&offset);
if(character!=' ') return position;
}
}
return -1;
}
@@ -75,7 +78,7 @@ s32 NHTTPi_compareTokenN_HdrRecvBuf(const NHTTPResponseInfo* response, s32 posit
NHTTPi_HDRBUFLIST* block;
s32 offset;
s8 character;
if(position>=limit) return -1;
if(position>=limit) goto done;
FindHeaderBlock(response,position,&block,&offset);
character=ReadHeaderChar(response,&block,&offset);
while(LowerCase(character)==LowerCase(*token)) {
@@ -84,6 +87,7 @@ s32 NHTTPi_compareTokenN_HdrRecvBuf(const NHTTPResponseInfo* response, s32 posit
++position;
++token;
}
done:
return -1;
}
@@ -91,7 +95,7 @@ s32 NHTTPi_loadFromHdrRecvBuf(NHTTPResponseInfo* response, char* destination, s3
s32 amount;
NHTTPi_HDRBUFLIST* block;
if(position+length>response->headerLen) return FALSE;
if(position+length<=response->headerLen) {
if(length) {
if(position<1024) {
amount=length;
@@ -110,13 +114,16 @@ s32 NHTTPi_loadFromHdrRecvBuf(NHTTPResponseInfo* response, char* destination, s3
amount=length;
if(length>512-position) amount=512-position;
NHTTPi_memcpy(destination,block->block+position,amount);
position=(position+amount)&511;
position+=amount;
position&=511;
block=block->next_p;
length-=amount; destination+=amount;
}
}
}
return TRUE;
return TRUE;
}
return FALSE;
}
BOOL NHTTPi_isRecvBufFull(NHTTPResponseInfo* response, u32 length) { return length>=response->recvBufLen; }
@@ -128,9 +135,11 @@ s32 NHTTPi_RecvBuf(void* mutex, NHTTPRequestInfo* request, s32 socket, u32 offse
s32 NHTTPi_RecvBufN(void* mutex, NHTTPRequestInfo* request, s32 socket, u32 offset, s32 length, s32 flags) {
NHTTPResponseInfo* response=request->response;
s32 available;
s32 remaining;
char* buf;
if(response->recvBufLen<=offset) return -1003;
available=response->recvBufLen-offset;
if(length>available) length=available;
return NHTTPi_SocRecv(mutex,request,socket,response->recvBuf_p+offset,length,flags);
remaining=((volatile NHTTPResponseInfo*)response)->recvBufLen-offset;
buf=response->recvBuf_p+offset;
if(length>remaining) length=remaining;
return NHTTPi_SocRecv(mutex,request,socket,buf,length,flags);
}

View File

@@ -148,7 +148,7 @@ s32 NHTTPi_SocSend_sub(s32 socket, const char* data, u32 length, s32 flags) {
sent+=result;
if((u32)result<head) return sent;
data+=result; length-=result;
} else { if(sent>0) return sent; return result; }
} else return sent>0 ? sent : result;
}
}
if((s32)length>0) {
@@ -166,8 +166,7 @@ s32 NHTTPi_SocSend_sub(s32 socket, const char* data, u32 length, s32 flags) {
s32 NHTTPi_SocSend(NHTTPRequestInfo* request, s32 socket, const char* data, s32 length, s32 flags) {
s32 result;
SSLId ssl=request->sslId;
if(ssl>0) result=SSLWrite(ssl,data,length);
if(request->sslId>0) result=SSLWrite(request->sslId,data,length);
else result=NHTTPi_SocSend_sub(socket,data,length,flags);
if(result<0) {
if(request->cancel) return -1002;

View File

@@ -7,8 +7,7 @@ s32 NHTTPi_strcmp(const char* left, const char* right) { return strcmp(left,righ
void* NHTTPi_memclr(void* destination, u32 size) { return memset(destination,0,size); }
static int LowerCase(int character) {
if((character>='A') & (character<='Z')) character+=32;
return character;
return (character>='A') & (character<='Z') ? character+32 : character;
}
s32 NHTTPi_strnicmp(const char* left, const char* right, s32 length) {
@@ -63,23 +62,54 @@ s32 NHTTPi_encodeUrlChar(char* destination, s8 character) {
}
}
s32 NHTTPi_strToHex(const char* string, s32 length) {
s32 value;
BOOL started;
if(length>8) return -1;
if((length==8) & (*string>='7')) return -1;
value=0; started=FALSE;
for(; length>0; --length,++string) {
s8 character=LowerCase(*string);
if(character>='0' && character<='9') { value=(value<<4)+character-'0'; started=TRUE; }
else if(character>='a' && character<='f') { value=(value<<4)+character-'a'+10; started=TRUE; }
else {
if(started && (character==' ' || character==0)) break;
if(!started && character==' ') continue;
s32 NHTTPi_strToHex(const char* string, s32 length)
{
s32 result;
BOOL foundDigit;
char c;
if (length > 8)
{
return -1;
}
if ((length == 8) & (string[0] > '7'))
{
return -1;
}
result = 0;
foundDigit = FALSE;
while (length-- > 0)
{
c = ((*string >= 'A') & (*string <= 'Z'))
? *string + ('a' - 'A')
: *string;
if (c >= '0' && c <= '9')
{
result = result * 16 + c - '0';
foundDigit = TRUE;
}
else if (c >= 'a' && c <= 'f')
{
result = result * 16 + c - 'a' + 10;
foundDigit = TRUE;
}
else if (foundDigit && (c == ' ' || c == '\0'))
{
break;
}
else if (!foundDigit && c == ' ')
{
}
else
{
return -1;
}
string++;
}
return value;
return result;
}
s32 NHTTPi_strToInt(const char* string, s32 length) {
@@ -124,10 +154,7 @@ s32 NHTTPi_intToStr(char* destination, u32 value) {
}
s32 NHTTPi_compareToken(const char* left, const char* right) {
for (;;) {
int rightCharacter=LowerCase(*right);
int leftCharacter=LowerCase(*left);
if(leftCharacter!=rightCharacter) break;
while(LowerCase(*left)==LowerCase(*right)) {
if(*left==0 || *left==' ') return 0;
++left; ++right;
}
@@ -135,12 +162,14 @@ s32 NHTTPi_compareToken(const char* left, const char* right) {
}
s32 NHTTPi_strtonum(const char* string, u32 length) {
int digits=0;
s32 value=0;
while(length--) {
int character=*string++;
if(character==' ') continue;
if((character>='0') & (character<='9')) {
s8 character;
s32 value;
int digits;
digits=0;
value=0;
for(; length--; ++string) {
if((character=*string)==' ') continue;
if((character>=(long long)'0') & (character<=(long long)'9')) {
value=value*10+character-'0';
++digits;
if(digits>9) return -1;

View File

@@ -371,9 +371,9 @@ static BOOL NHTTPi_RecvProxyConnectHeader(NHTTPThreadContext* context)
response->httpStatus = NHTTPi_strToInt(header + 9, 3);
if (NHTTPi_strnicmp(header, "HTTP/", 5) == 0 && header[8] == ' '
&& response->httpStatus == 200) accepted = TRUE;
ended = FALSE;
cursor = header;
index = 0;
ended = FALSE;
while (index < used)
{
if (index > 1 && cursor[-1] == '\r' && *cursor == '\r') ended = TRUE;
@@ -954,7 +954,6 @@ static BOOL NHTTPi_ThreadParseHeaderProc(NHTTPThreadContext* context)
NHTTPThreadInfo* thread = NHTTPi_GetThreadInfoP(system);
s32 lineLength;
s32 offset;
char* buffer = thread->commBuf;
s32 length;
if (!NHTTPi_loadFromHdrRecvBuf(response, context->statusLine, 0, 14)) { context->error = 7; return FALSE; }
if (NHTTPi_strnicmp(context->statusLine, "HTTP/", 5) != 0) { context->error = 7; return FALSE; }
@@ -967,8 +966,8 @@ static BOOL NHTTPi_ThreadParseHeaderProc(NHTTPThreadContext* context)
if (context->contentLength > 0x100) { context->error = 7; return FALSE; }
if (context->contentLength > 0)
{
if (!NHTTPi_loadFromHdrRecvBuf(response, buffer, offset, context->contentLength)) { context->error = 7; return FALSE; }
context->contentLength = NHTTPi_strToInt(buffer, context->contentLength);
if (!NHTTPi_loadFromHdrRecvBuf(response, thread->commBuf, offset, context->contentLength)) { context->error = 7; return FALSE; }
context->contentLength = NHTTPi_strToInt(thread->commBuf, context->contentLength);
if (context->contentLength < 0) { context->error = 7; return FALSE; }
response->contentLength = context->contentLength;
}

View File

@@ -1,10 +1,11 @@
#!/usr/bin/env python3
"""Compact function diff with context: python ctxdiff.py <unit> <symbol> [--full]"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, "/home/cole/projects/tests")
from odiff import dis, sym
import os
import itertools

View File

View File

@@ -9,3 +9,13 @@
- NWC24CommitMsgInternal/WriteMIMEAttachHeader: ndiff-0, residual fuzzy from one r23<->r24 home swap at ~0x770 (cascade from earlier live range).
- NWC24MsgSubject fns: reg-name/arg-staging diffs only; ReadMsgSubjectPublic missing `mr r8,r26` staging; iSetMsgSubjectBase64 +12 same-stream.
- ConvertDaysToDate: 57 regname diffs + one extra tail block (`*month++` + b) vs orig's fall-through return.
## nhttp wave-2 (2026-10-01) — new levers + walls
- **Volatile second-read defeats field-load CSE**: `((volatile T*)p)->field` on a SECOND use of an already-loaded member forces MWCC to re-emit `lwz` instead of reusing the register. RecvBufN: `response->recvBufLen` loaded twice (base reloads after the `<=offset` bail). Combined with stmt order (compute `remaining` before `buf` arg materialization) → 15/15 0 diffs exact.
- **`s8` local type fixes lbz→extsb dest-reg**: `s8 character` (not `int`) in strtonum made MWCC pick `lbz r10` matching base's register home → 36/36 0 diffs exact.
- **Operand-eval-order controls first-load choice in `f(*a)==f(*b)` loops**: `LowerCase(*left)==LowerCase(*right)` made MWCC load r4's byte first; base loads the RIGHT-hand member first → compareToken 18→17 diffs (eval order fixed, residual = li-order + r11↔r12 rename).
- **Named arg-local hoists call-arg materialization above a branch**: `char* buf = p->field + off;` before `if(x>n) x=n;` forces `lwz+add` above the `ble` (base scheduling) — part of RecvBufN fix.
- **Named locals can EXPLODE callee-reg pressure**: `char* statusLine` local in ThreadParseHeaderProc pushed prologue to `_savegpr_26` (+172 diffs). Removing `char* buffer` local reduced 20→15 diffs. Base hoists `addi r27,r3,0x360` into a callee reg reused across sites; source can't express the pin (parked: statusLine-hoist + shared `li r29,0` zero-CSE, 15 diffs).
- **MWCC CROSS-FUNCTION TAIL-DEDUP**: splicing upstream's better findNextLine (123/126) into the remote recvbuf file broke loadFromHdrRecvBuf's exact match (0→57 diffs) — changing one fn's body changes OTHER fns' codegen in the same TU (shared tail blocks). Take whole files as packages; don't splice single fns.
- **character-spill wall**: findNextLine is -3 insns vs base because base keeps loop-local `s8 character` on the STACK (lbz+extsb per use ×3). Forcing `volatile s8` spills everywhere (+124 diffs). The reg-vs-stack residency of a provable-local is the store-forward wall — no source lever (123/126 parked).
- **Parked (insn-equal tie-breaks)**: strnicmp 51/51 `li r9,0`/`li r7,0x5a` order swap (2); intToStr 95/95 whole-fn reg rotation (60); compareToken 45/45 eval-fixed, li-order+renames (17); Base64Encode 119/119 load-interleave scheduling (58); SocSend_sub 102/102 loop block layout (33); SocSend 43/43 prologue sched (3); SocRecv_sub 67/67 interleave sched (4); compareTokenN 126/124 +2 insns (one extra srawi/subfc >=-eval not CSE'd in tail); skipSpace 62/62 single branch-target +4B (tail-block placement); DeleteConnection 103/103 r29↔r30↔r31 park-order + beq/bne polarity (11).

View File

@@ -0,0 +1,62 @@
#!/usr/bin/env python3
"""odiff: symbol lookup + normalized disassembly for object files.
Recreated for this environment (original lived on the author's machine).
"""
import re
from capstone import CS_ARCH_PPC, CS_MODE_32, CS_MODE_BIG_ENDIAN, Cs
from elftools.elf.elffile import ELFFile
_cache = {}
def _load(path):
if path in _cache:
return _cache[path]
with open(path, "rb") as f:
e = ELFFile(f)
st = e.get_section_by_name(".symtab")
names = [s.name for s in st.iter_symbols()]
syms = {s.name: (s["st_value"], s["st_size"]) for s in st.iter_symbols()}
data = {}
for sec in e.iter_sections():
data[sec.name] = (sec["sh_offset"], sec.data())
relocs = {}
for sec in e.iter_sections():
if sec.name.startswith(".rela"):
target = sec.name[5:]
for r in sec.iter_relocations():
relocs[(target, r["r_offset"])] = names[r["r_info_sym"]]
_cache[path] = (data, syms, relocs)
return _cache[path]
def sym(path, name):
_, syms, _ = _load(path)
return syms[name]
def dis(path, off, size, section=".text"):
data, syms, relocs = _load(path)
sec_data = data[section][1]
md = Cs(CS_ARCH_PPC, CS_MODE_32 + CS_MODE_BIG_ENDIAN)
out = []
for i in md.disasm(sec_data[off:off + size], off):
text = i.mnemonic + " " + i.op_str
m = re.search(r"0x[0-9a-fA-F]+", i.op_str)
if (section, i.address) in relocs:
# External reference: show the symbol, drop the raw addend
text = i.mnemonic + " " + re.sub(r"0x[0-9a-fA-F]+",
"<" + relocs[(section, i.address)] + ">",
i.op_str)
elif m and i.mnemonic.startswith("b"):
val = int(m.group(0), 16)
if off <= val < off + size:
# Intra-function branch: normalize to function-relative
text = i.mnemonic + " " + (i.op_str[:m.start()] +
"+" + hex(val - off) +
i.op_str[m.end():])
out.append((i.address - off, text))
return out