/* * Evergreen 5.50.2 ARM32 relocation proof on Vita/ Vita3K. * Official Google libcobalt.so is read as INPUT DATA ONLY. * We load PT_LOAD segments and apply exclusively resolvable R_ARM_REL relocations * to a temporary, non-executable USER_RW block. * Absolutely NO module init / function calls / code execution. */ #include #include #include #include #include #include #include #include #include #include #include #include #include // Memory budget. Evergreen's libcobalt carries its own musl malloc that // grows through SbMemoryMap, so the browser heap is NOT the newlib heap: the // newlib heap only serves the platform layer (and SbMemoryAllocate users). // 64 MiB newlib + 34 MiB Cobalt image + 48 MiB VitaGL pool; everything else // stays free for SbMemoryMap (Cobalt heap, V8) and thread stacks. unsigned int sceUserMainThreadStackSize = 4 * 1024 * 1024; unsigned int _newlib_heap_size_user = 64 * 1024 * 1024; #define IMAGE_CAPACITY (34u*1024u*1024u) #define MAX_HEADERS 32u #define EXPECT_BYTES 33491716LL #define RELOC_R_ARM_RELATIVE 23u #define RELOC_R_ARM_ABS32 2u #define RELOC_R_ARM_GLOB_DAT 21u #define RELOC_R_ARM_JUMP_SLOT 22u extern uintptr_t ytv_find_real_starboard_export(const char *name); extern unsigned ytv_real_starboard_export_count(void); static int passes=0, failures=0; static uint8_t *image = NULL; static Elf32_Phdr headers[MAX_HEADERS]; static unsigned nheaders = 0; static uint32_t base32 = 0; static int fd = -1; struct RelocStats { unsigned relative, abs32, glob, jump, missing, resolved, bad; unsigned external_glob, external_jump; uint32_t normalized_hash; }; static SceUInt64 g_boot_t0=0; static void log_line(const char *message) { if(!g_boot_t0)g_boot_t0=sceKernelGetProcessTimeWide(); sceIoMkdir("ux0:data/youtubevita",0777); #if defined(YTV_COBALT_BOOT_STAGE) int f=sceIoOpen("ux0:data/youtubevita/cobalt_boot.log", #else int f=sceIoOpen("ux0:data/youtubevita/relocations.log", #endif SCE_O_WRONLY | SCE_O_CREAT | SCE_O_APPEND,0666); if(f>=0) { char stamp[24]; int sl=snprintf(stamp,sizeof(stamp),"[%7llums] ", (unsigned long long)((sceKernelGetProcessTimeWide()-g_boot_t0)/1000)); if(message[0]!='\n')sceIoWrite(f,stamp,(unsigned)sl); sceIoWrite(f,message,(unsigned)strlen(message)); sceIoWrite(f,"\n",1); sceIoClose(f); } } static void check(int ok,const char *label) { char out[192]; snprintf(out,sizeof(out),"%s %s",ok?"PASS":"FAIL",label); log_line(out); if(ok)passes++; else failures++; } static void note_u32(const char *key,unsigned a,unsigned b) { char out[192]; snprintf(out,sizeof(out),"%s %u %u",key,a,b); log_line(out); } static int read_full(int64_t off,void *target,size_t n) { if(fd<0 || sceIoLseek(fd,off,SCE_SEEK_SET)<0)return 0; uint8_t *p=(uint8_t*)target; while(n) { unsigned chunk=(n>65536)?65536:(unsigned)n; int count=sceIoRead(fd,p,chunk); if(count<=0 || (unsigned)count>chunk)return 0; n-=count;p+=count; } return 1; } static int in_load(uint32_t addr,uint32_t length) { if((uint64_t)addr+length>IMAGE_CAPACITY)return 0; for(unsigned i=0;ip_type==PT_LOAD && addr>=p->p_vaddr && (uint64_t)addr+length <= (uint64_t)p->p_vaddr + p->p_memsz) return 1; } return 0; } static int load_headers(const Elf32_Ehdr *eh,int64_t filelen) { if(eh->e_phnum==0 || eh->e_phnum>MAX_HEADERS || eh->e_phentsize!=sizeof(Elf32_Phdr) || (uint64_t)eh->e_phoff + (uint64_t)eh->e_phnum*sizeof(Elf32_Phdr)>(uint64_t)filelen)return 0; nheaders=eh->e_phnum; return read_full(eh->e_phoff,headers,nheaders*sizeof(Elf32_Phdr)); } static uint32_t read_symbol_count(const Elf32_Ehdr *eh,int64_t filelen) { if(eh->e_shentsize!=sizeof(Elf32_Shdr) || eh->e_shnum==0 || (uint64_t)eh->e_shoff + (uint64_t)eh->e_shnum*sizeof(Elf32_Shdr)>(uint64_t)filelen)return 0; for(unsigned i=0;ie_shnum;i++) { Elf32_Shdr sh={0}; if(!read_full(eh->e_shoff+(int64_t)i*sizeof(sh),&sh,sizeof(sh)))return 0; if(sh.sh_type==SHT_DYNSYM && sh.sh_entsize==sizeof(Elf32_Sym) && sh.sh_size%sizeof(Elf32_Sym)==0) return sh.sh_size/sizeof(Elf32_Sym); } return 0; } static int load_segments(int64_t filelen,unsigned *loads) { *loads=0; // Validate the entire layout before writing even one segment. for(unsigned i=0;ip_type!=PT_LOAD)continue; if(p->p_filesz>p->p_memsz || (uint64_t)p->p_offset+p->p_filesz>(uint64_t)filelen || (uint64_t)p->p_vaddr+p->p_memsz>IMAGE_CAPACITY)return 0; } for(unsigned i=0;ip_type!=PT_LOAD)continue; if(!read_full(p->p_offset,image+p->p_vaddr,p->p_filesz))return 0; if(p->p_memsz>p->p_filesz) memset(image+p->p_vaddr+p->p_filesz,0,p->p_memsz-p->p_filesz); (*loads)++; } return 1; } static int find_dynamic(uint32_t *rel_addr,uint32_t *rel_len, uint32_t *plt_addr,uint32_t *plt_len, uint32_t *sym_addr,uint32_t *sym_stride) { int found=0; for(unsigned i=0;ip_type!=PT_DYNAMIC)continue; if(!in_load(p->p_vaddr,p->p_memsz) || p->p_memsz%sizeof(Elf32_Dyn))return 0; found++; const Elf32_Dyn *d=(const Elf32_Dyn*)(image+p->p_vaddr); for(unsigned j=0;jp_memsz/sizeof(Elf32_Dyn);j++){ const uint32_t v=d[j].d_un.d_val; switch(d[j].d_tag) { case DT_REL: *rel_addr=v; break; case DT_RELSZ: *rel_len=v; break; case DT_JMPREL: *plt_addr=v; break; case DT_PLTRELSZ: *plt_len=v; break; case DT_SYMTAB: *sym_addr=v; break; case DT_SYMENT: *sym_stride=v; break; default:break; } if(d[j].d_tag==DT_NULL)break; } } return found==1 && *rel_len && *plt_len && *rel_len%sizeof(Elf32_Rel)==0 && *plt_len%sizeof(Elf32_Rel)==0 && *sym_stride==sizeof(Elf32_Sym) && in_load(*rel_addr,*rel_len) && in_load(*plt_addr,*plt_len); } static int process_table(uint32_t start,uint32_t length,uint32_t symaddr, uint32_t symcount,int apply,struct RelocStats *out) { const Elf32_Rel *table=(const Elf32_Rel*)(image+start); unsigned count=length/sizeof(Elf32_Rel); for(unsigned i=0;ibad++; return 0; } uint32_t *target=(uint32_t*)(image+r_offset); uint32_t addend=*target, value=0; Elf32_Sym sym={0}; if(ty!=RELOC_R_ARM_RELATIVE) { if(si>=symcount || !in_load(symaddr+si*sizeof(Elf32_Sym),sizeof(Elf32_Sym))) { out->bad++;return 0; } sym=*((const Elf32_Sym*)(image+symaddr+si*sizeof(Elf32_Sym))); } else if(si!=0) {out->bad++;return 0;} if(ty==RELOC_R_ARM_RELATIVE){out->relative++;value=base32+addend;} else if(ty==RELOC_R_ARM_ABS32) { out->abs32++; if(sym.st_shndx==SHN_UNDEF){out->missing++;continue;} value=(sym.st_shndx==SHN_ABS?0:base32)+sym.st_value+addend; } else if(ty==RELOC_R_ARM_GLOB_DAT || ty==RELOC_R_ARM_JUMP_SLOT) { if(ty==RELOC_R_ARM_GLOB_DAT)out->glob++; else out->jump++; if(sym.st_shndx==SHN_UNDEF) { out->missing++; if(ty==RELOC_R_ARM_GLOB_DAT)out->external_glob++; else out->external_jump++; continue; } value=(sym.st_shndx==SHN_ABS?0:base32)+sym.st_value; } else {out->bad++;return 0;} // Never dereference the relocated code pointer. Compare only the data. if(apply) { *target=value; if(*target!=value) {out->bad++;return 0;} } out->resolved++; out->normalized_hash = (out->normalized_hash*33u) ^ (value-base32) ^ (r_offset*17u); } return 1; } static int apply_real_imports(uint32_t symtab,uint32_t symcount, uint32_t rela,uint32_t relsz, uint32_t jmpa,uint32_t jmpsz, unsigned *linked,unsigned *unresolved) { uint32_t strtab=0,strsz=0; for(unsigned i=0;ip_type!=PT_DYNAMIC)continue; const Elf32_Dyn *d=(const Elf32_Dyn*)(image+p->p_vaddr); for(unsigned j=0;jp_memsz/sizeof(Elf32_Dyn);j++) { if(d[j].d_tag==DT_STRTAB)strtab=d[j].d_un.d_ptr; if(d[j].d_tag==DT_STRSZ)strsz=d[j].d_un.d_val; if(d[j].d_tag==DT_NULL)break; } } if(strtab==0 || strsz==0 || !in_load(strtab,strsz) || !in_load(symtab,symcount*sizeof(Elf32_Sym)))return 0; const uint32_t start[2]={rela,jmpa}; const uint32_t size[2]={relsz,jmpsz}; *linked=0;*unresolved=0; for(unsigned t=0;t<2;t++) { const Elf32_Rel *rels=(const Elf32_Rel*)(image+start[t]); for(unsigned i=0;i=symcount || !in_load(rels[i].r_offset,4))return 0; const Elf32_Sym *sym=(const Elf32_Sym*)(image+symtab+idx*sizeof(Elf32_Sym)); if(sym->st_shndx!=SHN_UNDEF)continue; if(sym->st_name>=strsz)return 0; const char *name=(const char*)(image+strtab+sym->st_name); unsigned available=strsz-sym->st_name; if(!memchr(name,0,available))return 0; uintptr_t address=ytv_find_real_starboard_export(name); if(address!=0) { *(uint32_t*)(image+rels[i].r_offset)=(uint32_t)address; if(*(uint32_t*)(image+rels[i].r_offset)!=(uint32_t)address)return 0; (*linked)++; } else { // Leave missing imports untouched; do NOT replace them with a fake stub. (*unresolved)++; } } } return 1; } #if defined(YTV_COBALT_BOOT_STAGE) && YTV_COBALT_BOOT_STAGE>=2 // Independent heartbeat: distinguishes a real deadlock (counters frozen) // from slow progress. Reads only counters, never touches Cobalt state. extern int ytv_runtime_once_count(void); extern int ytv_memory_map_count(void); extern unsigned ytv_memory_map_bytes(void); extern void ytv_thread_cpu_report(char* out,int n,const char* main_name); extern void ytv_gl_stats(char* out,int n); static int heartbeat_thread(SceSize args,void* argp){ (void)args;(void)argp; // Runs for the whole session (2 s steps for a minute, then 5 s) so a // later freeze still shows which threads use the CPU. for(unsigned t=2;;t+=(t<60?2:5)){ sceKernelDelayThread(t<60?2000000:5000000); SceKernelFreeMemorySizeInfo mem={0}; mem.size=sizeof(mem); sceKernelGetFreeMemorySize(&mem); char line[160]; snprintf(line,sizeof(line), "COBALT_HEARTBEAT t=%us once=%d user_free=%d cdram_free=%d " "newlib_used=%d maps=%d map_bytes=%u", t,ytv_runtime_once_count(),mem.size_user,mem.size_cdram, (int)mallinfo().uordblks,ytv_memory_map_count(), ytv_memory_map_bytes()); log_line(line); char cpu[300]; ytv_thread_cpu_report(cpu,sizeof(cpu),NULL); log_line(cpu); ytv_gl_stats(cpu,sizeof(cpu)); log_line(cpu); } return 0; } #endif // V8's ARM FlushICache is the only Linux syscall in libcobalt.so // (svc 0 with r7=0xF0002, cacheflush) at ELF 0xD35198. Vita has no such // syscall; with the JIT enabled V8 calls it after emitting code. The loader // redirects that function (in the RAM copy, before it becomes executable) to // this shim, which performs the real D/I-cache maintenance via kubridge. static void ytv_flush_icache(void* start, unsigned size) { if (start && size) kuKernelFlushCaches(start, size); } #define V8_FLUSH_ICACHE_OFFSET 0xd35198u static int get_init_array(uint32_t *addr,uint32_t *length){ if(!addr||!length)return 0; *addr=0;*length=0; for(unsigned i=0;ip_type!=PT_DYNAMIC)continue; if(!in_load(p->p_vaddr,p->p_memsz))return 0; const Elf32_Dyn* d=(const Elf32_Dyn*)(image+p->p_vaddr); for(unsigned j=0;jp_memsz/sizeof(*d);j++){ if(d[j].d_tag==DT_INIT_ARRAY)*addr=d[j].d_un.d_ptr; else if(d[j].d_tag==DT_INIT_ARRAYSZ)*length=d[j].d_un.d_val; else if(d[j].d_tag==DT_NULL)break; } } return *addr && *length && (*length%4)==0 && in_load(*addr,*length); } int main(void) { #if defined(YTV_COBALT_BOOT_STAGE) { // RAM granted to this process by its param.sfo memory mode: what is free // now plus what is already committed (newlib heap, eboot image, stack). extern long long ytv_measured_total_memory; SceKernelFreeMemorySizeInfo mem={0}; mem.size=sizeof(mem); if(sceKernelGetFreeMemorySize(&mem)>=0) ytv_measured_total_memory=(long long)mem.size_user+ (long long)_newlib_heap_size_user+sceUserMainThreadStackSize+ 4LL*1024*1024 /* eboot code+data */; } // Full application clocks (defaults are 333 MHz CPU / 111 MHz GPU). scePowerSetArmClockFrequency(444); scePowerSetBusClockFrequency(222); scePowerSetGpuClockFrequency(222); scePowerSetGpuXbarClockFrequency(166); // Main thread affinity: 3 user cores, or 4 with CapUnlocker. { extern int ytv_vita_cpu_mask(void); ytv_vita_cpu_mask(); } // probes CapUnlocker 4th core #endif log_line("\nSTART Evergreen armv7 RAM-only relocation validation"); fd=sceIoOpen("app0:libcobalt.so",SCE_O_RDONLY,0); check(fd>=0,"official Cobalt ELF data accessible"); if(fd<0)goto done; { int64_t len=sceIoLseek(fd,0,SCE_SEEK_END); check(len==EXPECT_BYTES,"official ELF byte length matches trusted asset"); if(len!=EXPECT_BYTES)goto done; Elf32_Ehdr eh={0}; check(read_full(0,&eh,sizeof(eh)) && memcmp(eh.e_ident,ELFMAG,SELFMAG)==0 && eh.e_ident[EI_CLASS]==ELFCLASS32 && eh.e_ident[EI_DATA]==ELFDATA2LSB && eh.e_machine==EM_ARM && eh.e_type==ET_DYN, "ELF32 little endian ARM shared object"); if(failures)goto done; check(load_headers(&eh,len),"validated ELF program headers"); if(failures)goto done; uint32_t symcount=read_symbol_count(&eh,len); note_u32("DYNSYM_COUNT",symcount,0); check(symcount==2434,"symbol bounds from official SHT_DYNSYM"); if(failures)goto done; SceUID uid=sceKernelAllocMemBlock("CobaltRelocRW", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW, IMAGE_CAPACITY,NULL); check(uid>=0,"contiguous 34 MiB NON-EXECUTABLE user memory"); if(uid<0)goto done; { void *base=NULL; int mapped=sceKernelGetMemBlockBase(uid,&base); check(mapped==0 && base!=NULL,"Vita user RW block mapping"); if(mapped!=0 || !base)goto release; image=(uint8_t*)base; base32=(uint32_t)(uintptr_t)base; note_u32("COBALT_IMAGE_BASE",base32,IMAGE_CAPACITY); { extern uintptr_t ytv_cobalt_image_base; ytv_cobalt_image_base=base32; } unsigned count=0; check(load_segments(len,&count) && count==4, "loaded all four official PT_LOAD segments into virtual layout"); if(failures)goto release; uint32_t ra=0,rl=0,ja=0,jl=0,sa=0,ss=0; check(find_dynamic(&ra,&rl,&ja,&jl,&sa,&ss) && in_load(sa,symcount*ss), "read true DT_REL, DT_JMPREL and DT_SYMTAB from RAM"); if(failures)goto release; note_u32("RELOCATION_TABLE_SIZE",rl/8,jl/8); struct RelocStats before={0}; // First phase: bounds-check all relocation targets and symbol indices; // NO WRITES to relocation targets. int verified=process_table(ra,rl,sa,symcount,0,&before) && process_table(ja,jl,sa,symcount,0,&before); check(verified && before.relative==61413 && before.abs32==2884 && before.glob==157 && before.jump==664 && before.missing==211 && before.bad==0 && before.resolved==64907, "all 65,118 official ARM REL entries validated without writing"); if(failures)goto release; struct RelocStats after={0}; // This phase only patches ordinary, non-executable Vita data RAM. int relocated=process_table(ra,rl,sa,symcount,1,&after) && process_table(ja,jl,sa,symcount,1,&after); note_u32("RELOCATED_AND_DEFERRED",after.resolved,after.missing); note_u32("UNRESOLVED_GLOB_JUMP",after.external_glob,after.external_jump); check(relocated && after.bad==0 && after.resolved==64907 && after.missing==211, "64,907 internally resolved relocations applied in RW RAM"); check(after.normalized_hash==before.normalized_hash, "relocation values match pre-write computed hash"); check(after.external_glob==14 && after.external_jump==197, "211 dynamic external relocation slots remain explicitly unresolved"); check(image[0]==0x7f && image[1]=='E' && image[2]=='L' && image[3]=='F', "in-memory ELF header intact after relocations"); unsigned imported=0,still_missing=0; int imports_valid=apply_real_imports(sa,symcount,ra,rl,ja,jl, &imported,&still_missing); note_u32("REAL_STARBOARD_EXPORTS_LINKED",imported,still_missing); check(imports_valid && imported==211 && still_missing==0 && ytv_real_starboard_export_count()==212 && imported+still_missing==211, "all implemented Vita Starboard exports linked; remaining imports fail closed"); if(failures)goto release; // Read-only-executable protection gate: we never invoke the Cobalt code. // Dynamic relocations in this official image all target RW data segments, // so protecting its RX segment does not interfere with unresolved imports. uint32_t xstart=0,xlen=0;unsigned execs=0; for(unsigned i=0;ip_type==PT_LOAD && (p->p_flags & PF_X)){ execs++; uint32_t lo=p->p_vaddr & ~4095u; uint32_t hi=(uint32_t)(((uint64_t)p->p_vaddr+p->p_memsz+4095u)&~4095ull); xstart=lo;xlen=hi-lo; } } check(execs==1 && xstart==0x818000u && xlen==(0x1fa0000u-0x818000u) && (uint64_t)xstart+xlen<=IMAGE_CAPACITY, "precise alignment and size of official Cobalt ARM executable segment"); if(failures)goto release; #if defined(YTV_COBALT_DIAG_CTOR_CUTOFF) // Debug VPK modifies only its private RAM copy of one constructor, // before mapping RX. Epilogue matches push-r4-lr/sub-sp-128 prologue. { unsigned target=0; int diag_fd=sceIoOpen("ux0:data/youtubevita/ctor_cutoff.txt",SCE_O_RDONLY,0); if(diag_fd>=0){ char value[24]={0}; int n=sceIoRead(diag_fd,value,sizeof(value)-1); sceIoClose(diag_fd); if(n>0){ unsigned scan=0; if(sscanf(value,"%x",&scan)==1)target=scan; } } if(target!=0){ int allowed=target==0x819c74u || target==0x819c8cu || target==0x819c98u || target==0x819cb0u || target==0x819cc8u; check(allowed && in_load(target,8), "constructor debug cutoff selected only from verified epilogues"); if(failures)goto release; uint32_t* instr=(uint32_t*)(image+target); instr[0]=0xe28dd080u; // add sp,sp,#128 instr[1]=0xe8bd8010u; // pop {r4,pc} note_u32("COBALT_CTOR_DEBUG_CUTOFF",target,1); } } #endif #if defined(YTV_COBALT_DIAG_EVENT_CUTOFF) { unsigned target=0; int f=sceIoOpen("ux0:data/youtubevita/event_cutoff.txt", SCE_O_RDONLY,0); if(f>=0){ char value[24]={0}; int n=sceIoRead(f,value,sizeof(value)-1); sceIoClose(f); if(n>0 && sscanf(value,"%x",&target)!=1)target=0; } if(target){ // Valid instruction boundaries inside official Start event handler. const unsigned allowed[]={ 0x819160u,0x81916cu,0x81917cu,0x819180u, 0x819188u,0x81918cu,0x819190u,0x819194u,0x81919cu,0x8191a8u, 0x8191b4u,0x8191c4u,0x8191c8u,0x8191d0u, 0x8191e0u,0x819200u,0x819204u,0x819210u, 0x8193f8u,0x8193fcu,0x819400u,0x819440u, 0x819444u,0x819454u,0x819458u,0x819464u, 0x819468u,0x819474u }; int okay=0; for(unsigned i=0;i=0){ char value[24]={0}; int n=sceIoRead(f,value,sizeof(value)-1); sceIoClose(f); if(n>0 && sscanf(value,"%x",&target)!=1)target=0; } if(target){ const unsigned allowed[]={ 0x13210a8u,0x13210acu,0x13210c0u,0x13210ccu, 0x13210d0u,0x13210e0u,0x13210e4u,0x1321160u, 0x1321164u,0x1321170u,0x13211b8u,0x13211ccu, 0x13211dcu,0x13211ecu,0x13211f4u,0x132124cu }; int okay=0; for(unsigned i=0;i=0){ char value[24]={0}; int n=sceIoRead(f,value,sizeof(value)-1); sceIoClose(f); if(n>0 && sscanf(value,"%x",&target)!=1)target=0; } if(target){ const unsigned allowed[]={ 0x15dc4dcu,0x15dc4f0u,0x15dc524u,0x15dc534u, 0x15dc538u,0x15dc540u,0x15dc544u,0x15dc54cu, 0x15dc59cu,0x15dc5b8u,0x15dc5c8u,0x15dc5dcu, 0x15dc5ecu,0x15dc604u,0x15dc614u }; int okay=0; for(unsigned i=0;i=0x808e10u && !in_load(stub_offset,8), "trusted executable test page is outside every Cobalt PT_LOAD segment"); if(failures)goto release; const uint32_t demo[2]={0xe3a0002au,0xe12fff1eu}; // mov r0,#42; bx lr memcpy(image+stub_offset,demo,sizeof(demo)); protection=kuKernelMemProtect(image+stub_offset,0x1000, KU_KERNEL_PROT_READ|KU_KERNEL_PROT_EXEC); check(protection==0,"trusted test page separately mapped R-X inside 34MB image"); if(failures)goto release; kuKernelFlushCaches(image+stub_offset,sizeof(demo)); int (*known_function)(void)=(int (*)(void))(image+stub_offset); int trusted_return=known_function(); note_u32("TRUSTED_ARM_EXEC_RETURN", (unsigned)trusted_return,stub_offset); check(trusted_return==42,"trusted ARM code executes inside full Cobalt allocation"); // PT_LOAD #4 has a zero-initialized BSS tail. Data must stay writable. uint32_t bss_offset=0x203d9f0u; check(in_load(bss_offset,4),"BSS address belongs to writable data PT_LOAD"); if(failures)goto release; uint32_t *bss=(uint32_t*)(image+bss_offset); uint32_t saved=*bss; *bss=0x53425654u; check(*bss==0x53425654u,"writable Cobalt BSS survives separate RX mapping"); *bss=saved; uint32_t ctor_addr=0,ctor_bytes=0; int ctor_valid=get_init_array(&ctor_addr,&ctor_bytes) && ctor_bytes==74u*sizeof(uint32_t); if(ctor_valid){ uint32_t* ctors=(uint32_t*)(image+ctor_addr); for(unsigned j=0;j=base32+xstart+xlen){ ctor_valid=0;break; } } } note_u32("ELF_INIT_ARRAY",ctor_bytes/4,ctor_valid?1u:0u); check(ctor_valid,"all 74 Cobalt C++ initializers belong to RX text"); if(failures)goto release; #if defined(YTV_COBALT_BOOT_STAGE) && (YTV_COBALT_BOOT_STAGE>=1) if(still_missing!=0)goto release; typedef void (*Ctor)(void); uint32_t *initializer=(uint32_t*)(image+ctor_addr); log_line("COBALT_CONSTRUCTOR_PROBE_BEGIN"); // Probe an official Cobalt internal ARM memcpy function which ctor #0 // calls through PLT. This checks actual cross-page execution and return. { typedef void (*ArmCopy)(void*,const void*,unsigned); ArmCopy fn=(ArmCopy)(uintptr_t)(base32+0x016b4da0u); unsigned char source[128]={},dest[128]={}; for(unsigned j=0;j=2 // The official Cobalt ELF exports SbEventHandle at fixed offset // 0x00818f7c (confirmed in readelf). A standalone test wires it into // our real Vita lifecycle dispatcher, with no mock/simulated UI. extern _Bool ytv_vita_lifecycle_begin(void(*)(const void*)); extern int ytv_starboard_pump_scheduled(int); typedef void (*CobaltEventHandler)(const void*); const uint32_t cobalt_event_offset=0x00818f7cu; check(cobalt_event_offset>=xstart && cobalt_event_offset=0)sceKernelStartThread(hb,0,NULL); } log_line("COBALT_EVENT_START_ENTER"); int started=ytv_vita_lifecycle_begin(cobalt_event); log_line("COBALT_EVENT_START_RETURN"); check(started,"official Cobalt SbEventHandle received native Starboard Start"); if(failures)goto release; // Cobalt's main-thread message pump posts its work through // SbEventSchedule, so the scheduled-event queue must keep running. // 60 s of pumping (~3600 iterations) for the boot probe. unsigned dispatched=0; // Runs until the app is closed unless pump_seconds.txt limits it // (used by the automated Vita3K tests). unsigned limit_seconds=0; { int f=sceIoOpen("ux0:data/youtubevita/pump_seconds.txt",SCE_O_RDONLY,0); if(f>=0){ char v[16]={0}; int n=sceIoRead(f,v,sizeof(v)-1); sceIoClose(f); unsigned secs=0; if(n>0 && sscanf(v,"%u",&secs)==1 && secs>0 && secs<=86400) limit_seconds=secs; } } extern void ytv_starboard_wait_scheduled(long long); extern int ytv_vita_input_pump(void); extern int ytv_vita_lifecycle_pump(int); extern _Bool ytv_vita_lifecycle_should_exit(int*); const SceUInt64 loop_start=sceKernelGetSystemTimeWide(); SceUInt64 next_note=loop_start+30000000ull; for(;;){ // Every due Cobalt task runs now (no per-frame cap: Cobalt's main // thread work, input handling and timers all arrive this way). int n=ytv_starboard_pump_scheduled(0); if(n>0)dispatched+=(unsigned)n; ytv_vita_input_pump(); int code=0; if(ytv_vita_lifecycle_should_exit(&code)){ note_u32("COBALT_LIFECYCLE_EXIT",(unsigned)code,dispatched); break; } ytv_vita_lifecycle_pump(1); SceUInt64 now=sceKernelGetSystemTimeWide(); if(now>=next_note){ note_u32("COBALT_EVENT_PUMP",(unsigned)((now-loop_start)/1000000ull),dispatched); next_note+=30000000ull; } if(limit_seconds && now-loop_start>=(SceUInt64)limit_seconds*1000000ull)break; // Sleep until the next task is due or a new one is posted; wake at // least every 8 ms to sample the controller and touch panel. ytv_starboard_wait_scheduled(8000); } log_line("COBALT_EVENT_PUMP_COMPLETE"); #endif #else log_line("COBALT_CODE_EXECUTION_NOT_ATTEMPTED"); #endif } release: check(sceKernelFreeMemBlock(uid)==0,"temporary relocation memory released"); image=NULL; } done: if(fd>=0)sceIoClose(fd); { char msg[160]; snprintf(msg,sizeof(msg),"RESULT %s (%d passed, %d failed)", failures?"FAIL":"PASS",passes,failures); log_line(msg); } sceKernelExitProcess(failures?1:0); return failures?1:0; }