mirror of
https://github.com/TheBozos/VP2S_Recomp.git
synced 2026-12-01 07:38:48 -10:00
Battle passes switch sp to the scratchpad (saved = sp; move sp, t0 = 70003ff0; call; move sp, saved). A site table now covers mod001's pair and mod028's eight (s1 or s0 saved, entries in delay slots), parking the RAM sp so queued handlers get an owned stack. Battle 0x18 now runs its whole frame budget without a boundary. Co-Authored-By: Claude Opus 5.5 <[email protected]>
170 lines
10 KiB
C++
170 lines
10 KiB
C++
// Native SDK boundary tests: no game instructions, firmware image or device model.
|
|
#include "native_kernel.h"
|
|
#include "native_assets.h"
|
|
#include "bozo/ee/dispatcher.h"
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
#include <source_location>
|
|
#include <string>
|
|
|
|
namespace {
|
|
void require(bool condition,std::source_location at=std::source_location::current()) { if(!condition) throw std::runtime_error("native kernel boundary mismatch at line "+std::to_string(at.line())); }
|
|
template<class Fn> void rejected(Fn fn) {
|
|
bool failed=false;
|
|
try { fn(); } catch(const std::runtime_error &) { failed=true; }
|
|
require(failed);
|
|
}
|
|
class Host final : public bozo::ee::Host,public vp2s::AssetOwner {
|
|
public:
|
|
unsigned callbacks=0;
|
|
vp2s::NativeAssets assets;
|
|
vp2s::NativeAssets &nativeAssets() override { return assets; }
|
|
void kernelCallBegin(bozo::ee::Dispatcher &,R5900Context *ctx,uint32_t,uint32_t kind)override{
|
|
try{vp2s::validateNativeKernelCall(ctx,kind);}catch(...){++callbacks;throw;}
|
|
}
|
|
void libraryCall(bozo::ee::Dispatcher &,R5900Context *,uint32_t) override { ++callbacks; throw std::runtime_error("unexpected library call"); }
|
|
void vu0Microprogram(bozo::ee::Dispatcher &,R5900Context *,uint32_t) override { ++callbacks; throw std::runtime_error("unexpected VU call"); }
|
|
void ioRead(bozo::ee::Dispatcher &,R5900Context *,uint32_t,uint32_t,uint64_t[2]) override { ++callbacks; throw std::runtime_error("unexpected device read"); }
|
|
void ioWrite(bozo::ee::Dispatcher &,R5900Context *,uint32_t,uint32_t,const uint64_t[2]) override { ++callbacks; throw std::runtime_error("unexpected device write"); }
|
|
void fault(bozo::ee::Dispatcher &,R5900Context *,const std::string &what) override { ++callbacks; throw std::runtime_error(what); }
|
|
};
|
|
void bind(bozo::ee::Dispatcher &d,unsigned mask) {
|
|
if(mask&1) d.bindKernelCall(0x1370d0,bozo::ee::KernelCall::GetThreadId);
|
|
if(mask&2) d.bindKernelCall(0x1371e0,bozo::ee::KernelCall::CreateSema);
|
|
if(mask&4) d.bindKernelCall(0x137460,bozo::ee::KernelCall::FlushCache);
|
|
}
|
|
}
|
|
int main() {
|
|
try {
|
|
Host host;
|
|
bozo::ee::Dispatcher d(host);
|
|
auto &rt=d.runtime();
|
|
R5900Context ctx;
|
|
ctx.pc=0x141448; ctx.r[31].uw[0]=0x141570;
|
|
rejected([&]{vp2s::initNativeSyscallServices(nullptr,&ctx,&rt);});
|
|
bind(d,7);
|
|
rejected([&]{vp2s::initNativeSyscallServices(nullptr,&ctx,&rt);}); // no boot
|
|
ctx.pc=0x141a38;
|
|
rejected([&]{vp2s::retireAlarmFirmwareInstaller(nullptr,&ctx,&rt);});
|
|
d.boot(0x100000,0x1fffff0);
|
|
{
|
|
Host h; bozo::ee::Dispatcher missing(h);
|
|
bind(missing,3); missing.boot(0x100000,0x1fffff0);
|
|
rejected([&]{vp2s::retireAlarmFirmwareInstaller(nullptr,&ctx,&missing.runtime());});
|
|
}
|
|
ctx.pc=0x141448;
|
|
for(unsigned mask=0;mask<7;++mask) {
|
|
Host h; bozo::ee::Dispatcher missing(h);
|
|
bind(missing,mask); missing.boot(0x100000,0x1fffff0);
|
|
rejected([&]{vp2s::initNativeSyscallServices(nullptr,&ctx,&missing.runtime());});
|
|
}
|
|
auto &k=d.kernel();
|
|
{
|
|
auto &frame=k.thread(1)->top();auto &c=frame.ctx;
|
|
k.threadSetup(0x1fefff0,0x10000,0);k.useOwnedHandlerStacks();
|
|
c.r[17].uw[0]=c.r[29].uw[0];c.r[8].uw[0]=0x70003ff0;
|
|
const auto original=c;
|
|
vp2s::nativeWorkStack(&c,&rt,0x2bf040);
|
|
require(frame.parkedStack==c.r[29].uw[0] && !std::memcmp(&c,&original,sizeof(c)));
|
|
rejected([&]{vp2s::nativeWorkStack(&c,&rt,0x2bf040);});
|
|
c.r[29].sd[0]=0x70003ff0; // original move SP,t0
|
|
bozo::ee::QueuedHandler h;h.pc=0x155000;k.queue(h);
|
|
require(k.stackHandler(*k.thread(1)).sp==0x1ffff70);
|
|
k.endHandler(*k.thread(1));
|
|
rejected([&]{vp2s::nativeWorkStack(&ctx,&rt,0x2bf058);});
|
|
const auto work=c;
|
|
vp2s::nativeWorkStack(&c,&rt,0x2bf058);
|
|
require(frame.parkedStack==0 && !std::memcmp(&c,&work,sizeof(c)));
|
|
c.r[29].sd[0]=c.r[17].sd[0]; // original move SP,s1
|
|
rejected([&]{vp2s::nativeWorkStack(&c,&rt,0x2bf058);});
|
|
// mod028's copy around 47cdf0 (47ddb8 / 47ddc4), word-checked.
|
|
rejected([&]{vp2s::nativeWorkStack(&c,&rt,0x47ddb8);}); // not the battle's words
|
|
const uint32_t entry=0x0100e821,back=0x0220e821;std::memcpy(rt.memory.hostPointer(0x47ddb8,4),&entry,4);std::memcpy(rt.memory.hostPointer(0x47ddc4,4),&back,4);
|
|
vp2s::nativeWorkStack(&c,&rt,0x47ddb8);require(frame.parkedStack==c.r[29].uw[0]);
|
|
c.r[29].sd[0]=0x70003ff0;
|
|
{bozo::ee::QueuedHandler h2;h2.pc=0x155000;k.queue(h2);}
|
|
require(k.stackHandler(*k.thread(1)).sp==0x1ffff70);k.endHandler(*k.thread(1));
|
|
vp2s::nativeWorkStack(&c,&rt,0x47ddc4);require(frame.parkedStack==0);c.r[29].sd[0]=c.r[17].sd[0];
|
|
// The six with s0 (entry in the call's delay slot, back at the next word), e.g. 3d740c / 3d7410.
|
|
const uint32_t back0=0x0200e821;std::memcpy(rt.memory.hostPointer(0x3d740c,4),&entry,4);std::memcpy(rt.memory.hostPointer(0x3d7410,4),&back0,4);
|
|
c.r[16].uw[0]=c.r[29].uw[0]+16;rejected([&]{vp2s::nativeWorkStack(&c,&rt,0x3d740c);}); // s0 is not the saved SP
|
|
c.r[16].uw[0]=c.r[29].uw[0];vp2s::nativeWorkStack(&c,&rt,0x3d740c);require(frame.parkedStack==c.r[29].uw[0]);
|
|
c.r[29].sd[0]=0x70003430;vp2s::nativeWorkStack(&c,&rt,0x3d7410);require(frame.parkedStack==0);c.r[29].sd[0]=c.r[16].sd[0];
|
|
require(vp2s::nativeWorkStackSite(0x424eb0) && vp2s::nativeWorkStackSite(0x424eb0)->saved==16 && !vp2s::nativeWorkStackSite(0x424eb4));
|
|
}
|
|
const auto first=k.createSema(1,2,0,0);
|
|
require(first>0);
|
|
bozo::ee::QueuedHandler pending;
|
|
pending.kind=bozo::ee::kInvokeLibraryCall; pending.pc=0x155000;
|
|
k.queue(pending);
|
|
const auto sequence=k.sequence();
|
|
const auto event=k.nextEvent();
|
|
std::memset(rt.memory.flat(),0xa7,bozo::kFlatBytes);
|
|
std::vector<uint8_t> before(rt.memory.flat(),rt.memory.flat()+bozo::kFlatBytes);
|
|
for(unsigned n=0;n<16;++n) {
|
|
std::memset(&ctx,0x20+n,sizeof(ctx));
|
|
ctx.pc=0x141448; ctx.r[31].uw[0]=0x141570+n*4;
|
|
R5900Context expected;
|
|
std::memcpy(&expected,&ctx,sizeof(ctx)); expected.pc=ctx.r[31].uw[0];
|
|
vp2s::initNativeSyscallServices(nullptr,&ctx,&rt);
|
|
require(!std::memcmp(&ctx,&expected,sizeof(ctx)));
|
|
ctx.pc=0x141a38;
|
|
std::memcpy(&expected,&ctx,sizeof(ctx)); expected.pc=ctx.r[31].uw[0];
|
|
vp2s::retireAlarmFirmwareInstaller(nullptr,&ctx,&rt);
|
|
require(!std::memcmp(&ctx,&expected,sizeof(ctx)));
|
|
ctx.pc=0x140b70;
|
|
std::memcpy(&expected,&ctx,sizeof(ctx));expected.pc=ctx.r[31].uw[0];
|
|
vp2s::retireEventFirmwareInstaller(nullptr,&ctx,&rt);
|
|
require(!std::memcmp(&ctx,&expected,sizeof(ctx)));
|
|
ctx.pc=0x137a30;
|
|
std::memcpy(&expected,&ctx,sizeof(ctx));expected.pc=ctx.r[31].uw[0];
|
|
vp2s::nativeCacheSync(nullptr,&ctx,&rt);
|
|
require(!std::memcmp(&ctx,&expected,sizeof(ctx)));
|
|
}
|
|
require(!std::memcmp(before.data(),rt.memory.flat(),bozo::kFlatBytes));
|
|
require(k.running()==1 && k.createSema(0,1,0,0)==first+1);
|
|
require(k.handlerQueued() && k.sequence()==sequence && k.nextEvent()==event);
|
|
require(k.pollSema(first)>=0 && k.pollSema(first)<0);
|
|
require(host.callbacks==0);
|
|
bozo::port::Registry registry;
|
|
vp2s::installNativeKernelBindings(registry);
|
|
require(registry.find(0x141448)==&vp2s::initNativeSyscallServices);
|
|
require(registry.find(0x141a38)==&vp2s::retireAlarmFirmwareInstaller);
|
|
require(registry.find(0x137a30)==&vp2s::nativeCacheSync);
|
|
unsigned guarded=0;
|
|
for(const auto &entry:registry.functions()) {
|
|
if(entry.address==0x1401b8 || entry.address==0x13fba8 || entry.address==0x13f608 || entry.address==0x13f680 ||
|
|
entry.address==0x137580 || entry.address==0x137590 || entry.address==0x1375a0 || entry.address==0x1375b0 || entry.address==0x137a30 || entry.address==0x13a3f0 ||
|
|
entry.address==0x132d38)continue;
|
|
if(entry.address==0x141448 || entry.address==0x141a38 || entry.address==0x141bb0 || entry.address==0x141df0 || entry.address==0x1423f8 || entry.address==0x141f00 || entry.address==0x142150 || entry.address==0x137280 || entry.address==0x137290 || entry.address==0x140b70) continue;
|
|
ctx.pc=entry.address;
|
|
rejected([&]{entry.function(nullptr,&ctx,&rt);});
|
|
++guarded;
|
|
}
|
|
require(guarded==1847 && !registry.find(0x141548)); // SetSyscall is not bypassed
|
|
for(uint32_t number:{116u,0xfcu,0xfeu,0xfdu,0xffu,0x12cu,8u,0x54u,0x55u,0x56u,0x57u,0x58u,0x59u}) {
|
|
ctx.pc=0x14154c; ctx.r[3].uw[0]=number;
|
|
ctx.r[4].uw[0]=0x83; ctx.r[5].uw[0]=0x1413c0;
|
|
rejected([&]{d.syscall(nullptr,&ctx,&rt,0);});
|
|
}
|
|
// Actual image alarm/firmware entries were rejected through the port
|
|
// registry above; generic engine enum support is not port admission.
|
|
for(uint32_t number:{60u,61u}){ctx.r[3].uw[0]=number;vp2s::validateNativeKernelCall(&ctx,3);}
|
|
require(host.callbacks==13 && !std::memcmp(before.data(),rt.memory.flat(),bozo::kFlatBytes));
|
|
require(k.handlerQueued() && k.sequence()==sequence && k.nextEvent()==event);
|
|
{ // memmove (132d38): overlapping move, the bytes' asset provenance moves with them.
|
|
require(registry.find(0x132d38)==&vp2s::nativeMemmove);
|
|
std::memcpy(rt.memory.hostPointer(0x180000,8),"abcdefgh",8);host.assets.discRead(0x180000,1,7);
|
|
R5900Context m{};m.pc=0x132d38;m.r[4].uw[0]=0x180002;m.r[5].uw[0]=0x180000;m.r[6].uw[0]=6;m.r[31].uw[0]=0x170000;
|
|
vp2s::nativeMemmove(nullptr,&m,&rt);
|
|
require(!std::memcmp(rt.memory.hostPointer(0x180000,8),"ababcdef",8) && m.r[2].uw[0]==0x180002 && m.pc==0x170000);
|
|
const auto origin=host.assets.origin(0x180002);
|
|
require(origin && origin->kind==vp2s::AssetOrigin::Kind::Disc && origin->disc==7*2048);
|
|
}
|
|
std::puts("native installer/cache visibility, 48 context cases, RAM/kernel preservation, 1847 guards, memmove provenance and unsupported runtime syscall rejection pass");
|
|
return 0;
|
|
} catch(const std::exception &e) { std::fprintf(stderr,"%s\n",e.what()); return 1; }
|
|
}
|