sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
main.cpp
1#include <cstdio>
2#include <cstring>
3
4extern "C" {
5#include <kernel.h>
6}
7
8#include <exception>
9#include <stdexcept>
10#include <string>
11#include <thread>
12#include <atomic>
13
14extern "C" int kputs(char *s);
15
16/* A program must never return from main on the rig: that reboots the console. */
17static int rig_failed;
18static int rig_kputs(char *s) {
19 if (strstr(s, ": FAIL")) rig_failed = 1;
20 return kputs(s);
21}
22#define kputs rig_kputs
23
24static int dtors;
25struct Guard {
26 ~Guard() { dtors++; }
27};
28
29struct Base { virtual ~Base() {} virtual int id() const { return 1; } };
30struct Derived : Base { int id() const override { return 2; } };
31
32static int deep(int n) {
33 Guard g;
34 if (n == 0) throw std::runtime_error("deep");
35 return deep(n - 1) + 1;
36}
37
38static int through_noexcept_boundary() {
39 try { return deep(5); } catch (...) { throw; }
40}
41
42static std::atomic<int> thread_result{0};
43
44static int test_main() {
45 int fails = 0;
46 auto check = [&](bool ok, int line) { if (!ok) { fails++; (void)line; } };
47
48 // unwinding through six frames runs every destructor
49 try { deep(5); check(false, __LINE__); } catch (const std::runtime_error &e) { check(std::string(e.what()) == "deep", __LINE__); }
50 check(dtors == 6, __LINE__);
51
52 // rethrow keeps the exception type
53 try { through_noexcept_boundary(); check(false, __LINE__); } catch (const std::exception &e) { check(std::string(e.what()) == "deep", __LINE__); }
54
55 // catch by base class, catch (...) and polymorphic throw
56 try { throw Derived(); } catch (const Base &b) { check(b.id() == 2, __LINE__); }
57 try { throw 7; } catch (...) { check(true, __LINE__); }
58 try { throw 7; } catch (int v) { check(v == 7, __LINE__); }
59
60 // an exception thrown and caught in another thread
61 std::thread t([] {
62 try { throw std::logic_error("thread"); } catch (const std::logic_error &) { thread_result = 5; }
63 });
64 t.join();
65 check(thread_result == 5, __LINE__);
66
67 // nested exceptions
68 try {
69 try { throw std::runtime_error("inner"); }
70 catch (...) { std::throw_with_nested(std::runtime_error("outer")); }
71 } catch (const std::exception &e) {
72 try { std::rethrow_if_nested(e); check(false, __LINE__); }
73 catch (const std::exception &inner) { check(std::string(inner.what()) == "inner", __LINE__); }
74 }
75
76 kputs(fails == 0 ? (char *)"cxx-exceptions: PASS\n" : (char *)"cxx-exceptions: FAIL\n");
77 return 0;
78}
79
80int main() {
81 int result = test_main();
82 printf("[ps2rig] exit=%d\n", (result || rig_failed) ? 1 : 0);
83 SleepThread();
84 return 0;
85}
Definition main.cpp:29
Definition main.cpp:25