|
| 1 | +/* Windows GUI / no CRT / no CLR; only kernel32. No console/shell/breakaway. |
| 2 | + * /GS- /Zl /W4 /WX; /NODEFAULTLIB /ENTRY:TinyEntry /SUBSYSTEM:WINDOWS. |
| 3 | + * Exact Job membership/identity is independently proved by the controller. |
| 4 | + */ |
| 5 | +#define WIN32_LEAN_AND_MEAN |
| 6 | +#include <Windows.h> |
| 7 | +static BOOL Eq(const WCHAR* left,const WCHAR* right) |
| 8 | +{ |
| 9 | + DWORD index; ULONGLONG start=GetTickCount64(); |
| 10 | + for(index=0;index<1024 && GetTickCount64()-start<1000;index++) { |
| 11 | + if(left[index]!=right[index]) return FALSE; |
| 12 | + if(left[index]==0) return TRUE; |
| 13 | + } |
| 14 | + return FALSE; |
| 15 | +} |
| 16 | +static void Zero(void* memory,SIZE_T bytes) |
| 17 | +{ |
| 18 | + SIZE_T index;volatile BYTE* target=(volatile BYTE*)memory;ULONGLONG start=GetTickCount64(); |
| 19 | + if(bytes>4096) ExitProcess(81); |
| 20 | + for(index=0;index<bytes && index<4096 && GetTickCount64()-start<1000;index++) target[index]=0; |
| 21 | + if(index!=bytes) ExitProcess(81); |
| 22 | +} |
| 23 | +static BOOL Append(WCHAR* target,DWORD* length,const WCHAR* value) |
| 24 | +{ |
| 25 | + DWORD index;ULONGLONG start=GetTickCount64(); |
| 26 | + for(index=0;index<1024 && GetTickCount64()-start<1000;index++) { |
| 27 | + if(value[index]==0) return TRUE; |
| 28 | + if(*length>=4095) return FALSE; |
| 29 | + target[(*length)++]=value[index];target[*length]=0; |
| 30 | + } |
| 31 | + return FALSE; |
| 32 | +} |
| 33 | +static BOOL WriteBytes(HANDLE stream,const BYTE* bytes,DWORD count) |
| 34 | +{ |
| 35 | + DWORD written=0; |
| 36 | + return stream && stream!=INVALID_HANDLE_VALUE && GetFileType(stream)==FILE_TYPE_PIPE && WriteFile(stream,bytes,count,&written,NULL) && written==count; |
| 37 | +} |
| 38 | +static DWORD Parse(WCHAR values[4][1024]) |
| 39 | +{ |
| 40 | + const WCHAR* command=GetCommandLineW();DWORD argc=0,pos=0,index;BOOL quoted;ULONGLONG start=GetTickCount64(); |
| 41 | + for(index=0;index<4 && pos<4096 && GetTickCount64()-start<1000;index++) { |
| 42 | + DWORD length=0; |
| 43 | + for(;pos<4096 && command[pos]==L' ' && GetTickCount64()-start<1000;pos++) {} |
| 44 | + if(pos>=4096 || GetTickCount64()-start>=1000) return 0; |
| 45 | + if(command[pos]==0) return argc; |
| 46 | + quoted=command[pos]==L'"';if(quoted) pos++; |
| 47 | + for(;pos<4096 && length<1023 && GetTickCount64()-start<1000;pos++) { |
| 48 | + WCHAR ch=command[pos]; |
| 49 | + if((quoted && ch==L'"') || (!quoted && (ch==L' ' || ch==0))) break; |
| 50 | + if(ch==0 || ch==L'"') return 0; |
| 51 | + values[argc][length++]=ch; |
| 52 | + } |
| 53 | + if(pos>=4096 || length>=1023 || GetTickCount64()-start>=1000) return 0; |
| 54 | + values[argc][length]=0;argc++; |
| 55 | + if(quoted){if(command[pos]!=L'"') return 0;pos++;} |
| 56 | + if(command[pos]!=0 && command[pos]!=L' ') return 0; |
| 57 | + } |
| 58 | + for(index=0;index<4096 && pos<4096 && command[pos]==L' ' && GetTickCount64()-start<1000;index++,pos++) {} |
| 59 | + return pos<4096 && command[pos]==0 && GetTickCount64()-start<1000?argc:0; |
| 60 | +} |
| 61 | +static BOOL Ready(HANDLE reader) |
| 62 | +{ |
| 63 | + DWORD check,available=0,read=0;BYTE bytes[6];ULONGLONG start=GetTickCount64(); |
| 64 | + for(check=0;check<100 && GetTickCount64()-start<2000;check++) { |
| 65 | + if(!PeekNamedPipe(reader,NULL,0,NULL,&available,NULL)) return FALSE; |
| 66 | + if(available>=6) { |
| 67 | + /* Sole reader; exactly six already available bytes, not an unbounded read. */ |
| 68 | + if(!ReadFile(reader,bytes,6,&read,NULL) || read!=6 || GetTickCount64()-start>=2000) return FALSE; |
| 69 | + return bytes[0]=='R' && bytes[1]=='E' && bytes[2]=='A' && bytes[3]=='D' && bytes[4]=='Y' && bytes[5]=='\n'; |
| 70 | + } |
| 71 | + Sleep(20); |
| 72 | + } |
| 73 | + return FALSE; |
| 74 | +} |
| 75 | +static BOOL StartChild(const WCHAR* role,const WCHAR* id) |
| 76 | +{ |
| 77 | + /* Static fixed buffers avoid compiler __chkstk/CRT helpers. One spawn per role/process. */ |
| 78 | + static WCHAR image[1024],command[4096];DWORD imageLength,length=0;BOOL ok=FALSE,initialized=FALSE,created=FALSE; |
| 79 | + HANDLE reader=NULL,writer=NULL;HANDLE handles[3];SECURITY_ATTRIBUTES security; |
| 80 | + STARTUPINFOEXW startup;PROCESS_INFORMATION process;SIZE_T size=0;void* attributes=NULL;HANDLE heap=GetProcessHeap();BOOL inJob=FALSE; |
| 81 | + ULONGLONG start=GetTickCount64();DWORD cleanupErrors=0; |
| 82 | + Zero(&startup,sizeof(startup));Zero(&process,sizeof(process));command[0]=0; |
| 83 | + imageLength=GetModuleFileNameW(NULL,image,1024); |
| 84 | + if(imageLength==0 || imageLength>=1024) goto done; |
| 85 | + if(!Append(command,&length,L"\"") || !Append(command,&length,image) || !Append(command,&length,L"\" \"--fixture\" \"") || !Append(command,&length,role) || !Append(command,&length,L"\" \"") || !Append(command,&length,id) || !Append(command,&length,L"\"")) goto done; |
| 86 | + security.nLength=sizeof(security);security.lpSecurityDescriptor=NULL;security.bInheritHandle=TRUE; |
| 87 | + if(!CreatePipe(&reader,&writer,&security,0) || !SetHandleInformation(reader,HANDLE_FLAG_INHERIT,0)) goto done; |
| 88 | + handles[0]=GetStdHandle(STD_INPUT_HANDLE);handles[1]=writer;handles[2]=GetStdHandle(STD_ERROR_HANDLE); |
| 89 | + if(handles[0]==NULL || handles[0]==INVALID_HANDLE_VALUE || handles[2]==NULL || handles[2]==INVALID_HANDLE_VALUE) goto done; |
| 90 | + if(InitializeProcThreadAttributeList(NULL,1,0,&size) || GetLastError()!=ERROR_INSUFFICIENT_BUFFER || size==0 || size>4096) goto done; |
| 91 | + attributes=HeapAlloc(heap,0,size);if(!attributes) goto done; |
| 92 | + if(!InitializeProcThreadAttributeList(attributes,1,0,&size)) goto done;initialized=TRUE; |
| 93 | + if(!UpdateProcThreadAttribute(attributes,0,PROC_THREAD_ATTRIBUTE_HANDLE_LIST,handles,sizeof(handles),NULL,NULL)) goto done; |
| 94 | + startup.StartupInfo.cb=sizeof(startup);startup.StartupInfo.dwFlags=STARTF_USESTDHANDLES; |
| 95 | + startup.StartupInfo.hStdInput=handles[0];startup.StartupInfo.hStdOutput=writer;startup.StartupInfo.hStdError=handles[2];startup.lpAttributeList=attributes; |
| 96 | + if(GetTickCount64()-start>=2000) goto done; |
| 97 | + if(!CreateProcessW(image,command,NULL,NULL,TRUE,EXTENDED_STARTUPINFO_PRESENT|CREATE_SUSPENDED,NULL,NULL,&startup.StartupInfo,&process)) goto done; |
| 98 | + created=TRUE; |
| 99 | + if(!IsProcessInJob(process.hProcess,NULL,&inJob) || !inJob || ResumeThread(process.hThread)!=1) goto done; |
| 100 | + if(!CloseHandle(writer)){cleanupErrors++;goto done;}writer=NULL; |
| 101 | + if(!Ready(reader) || GetTickCount64()-start>=2500) goto done; |
| 102 | + ok=TRUE; |
| 103 | +done: |
| 104 | + /* Exact controller Job owns all child lifetimes; never kill a PID here. */ |
| 105 | + if(created){if(!CloseHandle(process.hThread)) cleanupErrors++;if(!CloseHandle(process.hProcess)) cleanupErrors++;} |
| 106 | + if(initialized) DeleteProcThreadAttributeList(attributes); |
| 107 | + if(attributes && !HeapFree(heap,0,attributes)) cleanupErrors++; |
| 108 | + if(writer && !CloseHandle(writer)) cleanupErrors++; |
| 109 | + if(reader && !CloseHandle(reader)) cleanupErrors++; |
| 110 | + return ok && cleanupErrors==0; |
| 111 | +} |
| 112 | +static void BoundedHold(void) |
| 113 | +{ |
| 114 | + DWORD index;ULONGLONG start=GetTickCount64(); |
| 115 | + for(index=0;index<600 && GetTickCount64()-start<12000;index++) Sleep(20); |
| 116 | +} |
| 117 | +__declspec(noreturn) void WINAPI TinyEntry(void) |
| 118 | +{ |
| 119 | + static WCHAR values[4][1024];DWORD argc=Parse(values);BOOL member=FALSE; |
| 120 | + static const BYTE tiny[11]={'t','i','n','y',' ','r','o','o','t','\r','\n'}; |
| 121 | + static const BYTE ready[6]={'R','E','A','D','Y','\n'}; |
| 122 | + static const BYTE natural[17]={'c','o','n','t','r','o','l','l','e','d',' ','r','o','o','t','\r','\n'}; |
| 123 | + static const BYTE error[19]={'c','o','n','t','r','o','l','l','e','d',' ','e','x','i','t',' ','1','7','\n'}; |
| 124 | + if(!IsProcessInJob(GetCurrentProcess(),NULL,&member) || !member) ExitProcess(82); |
| 125 | + if(argc==1){if(!WriteBytes(GetStdHandle(STD_OUTPUT_HANDLE),tiny,11)) ExitProcess(73);ExitProcess(0);} |
| 126 | + if(argc!=4 || !Eq(values[1],L"--fixture")) ExitProcess(83); |
| 127 | + if(!Eq(values[3],L"natural-exit") && !Eq(values[3],L"exception-exit") && !Eq(values[3],L"timeout") && !Eq(values[3],L"cancellation") && !Eq(values[3],L"descendants-survive-root") && !Eq(values[3],L"nested-grandchild") && !Eq(values[3],L"assignment-or-capability-failure") && !Eq(values[3],L"identity-reuse-rejection")) ExitProcess(84); |
| 128 | + if(Eq(values[2],L"root")) { |
| 129 | + if(Eq(values[3],L"exception-exit")){if(!WriteBytes(GetStdHandle(STD_ERROR_HANDLE),error,19)) ExitProcess(73);ExitProcess(17);} |
| 130 | + if(Eq(values[3],L"timeout") || Eq(values[3],L"cancellation")){BoundedHold();ExitProcess(0);} |
| 131 | + if(Eq(values[3],L"descendants-survive-root") || Eq(values[3],L"nested-grandchild")){if(!StartChild(L"child",values[3])) ExitProcess(85);ExitProcess(0);} |
| 132 | + if(!WriteBytes(GetStdHandle(STD_OUTPUT_HANDLE),natural,17)) ExitProcess(73);ExitProcess(0); |
| 133 | + } |
| 134 | + if(Eq(values[2],L"child") && (Eq(values[3],L"descendants-survive-root") || Eq(values[3],L"nested-grandchild"))) { |
| 135 | + if(Eq(values[3],L"nested-grandchild") && !StartChild(L"grandchild",values[3])) ExitProcess(85); |
| 136 | + if(!WriteBytes(GetStdHandle(STD_OUTPUT_HANDLE),ready,6)) ExitProcess(73);BoundedHold();ExitProcess(0); |
| 137 | + } |
| 138 | + if(Eq(values[2],L"grandchild") && Eq(values[3],L"nested-grandchild")){if(!WriteBytes(GetStdHandle(STD_OUTPUT_HANDLE),ready,6)) ExitProcess(73);BoundedHold();ExitProcess(0);} |
| 139 | + ExitProcess(86); |
| 140 | +} |
0 commit comments