]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/darwintests/atm_diagnostic_flag.c
xnu-4570.71.2.tar.gz
[apple/xnu.git] / tools / tests / darwintests / atm_diagnostic_flag.c
1 #include <darwintest.h>
2
3 #include <mach/mach_error.h>
4 #include <mach/mach_host.h>
5
6 T_GLOBAL_META(T_META_NAMESPACE("xnu.debugging"));
7
8 /*
9 * The low 8 bits may be in use, so modify one
10 * of the upper 8 bits to ensure round-tripping.
11 */
12 #define LIBTRACE_PRIVATE_DATA 0x01000000
13
14 extern void drop_priv(void);
15
16 static bool _needs_reset;
17 static uint32_t _original;
18
19 static uint32_t
20 _save_atm_diagnostic_flag(void)
21 {
22 kern_return_t kr;
23 kr = host_get_atm_diagnostic_flag(mach_host_self(), &_original);
24 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_get_atm_diagnostic_flag()");
25 T_LOG("Original ATM diagnostic flag: 0x%08x", _original);
26 return _original;
27 }
28
29 static kern_return_t
30 _mutate_atm_diagnostic_flag(uint32_t v)
31 {
32 T_LOG("Try to set ATM diagnostic flag to: 0x%08x", v);
33 kern_return_t kr = host_set_atm_diagnostic_flag(mach_host_self(), v);
34 if (kr == KERN_SUCCESS) _needs_reset = true;
35 return kr;
36 }
37
38 static void
39 _reset_atm_diagnostic_flag(void)
40 {
41 if (!_needs_reset) return;
42 T_LOG("Reset ATM diagnostic flag to: 0x%08x", _original);
43 kern_return_t kr;
44 kr = host_set_atm_diagnostic_flag(mach_host_self(), _original);
45 if (kr != KERN_SUCCESS) {
46 T_ASSERT_FAIL("host_set_atm_diagnostic_flag() failed: %s",
47 mach_error_string(kr));
48 }
49 }
50
51 T_DECL(toggle_atm_diagnostic_flag,
52 "change the atm_diagnostic_flag, which should use the commpage",
53 T_META_ASROOT(true))
54 {
55 T_ATEND(_reset_atm_diagnostic_flag);
56 uint32_t f = _save_atm_diagnostic_flag();
57 f ^= LIBTRACE_PRIVATE_DATA;
58 kern_return_t kr = _mutate_atm_diagnostic_flag(f);
59 if (kr == KERN_NOT_SUPPORTED) {
60 T_SKIP("Seems ATM is disabled on this platform. "
61 "Ignoring host_set_atm_diagnostic_flag functionality. "
62 "Bailing gracefully.");
63 }
64 T_EXPECT_MACH_SUCCESS(kr, "Set atm_diagnostic_flag");
65 }
66
67 T_DECL(unprivileged_atm_diagnostic_flag,
68 "expect to fail to set the atm_diagnostic_flag",
69 T_META_ASROOT(false))
70 {
71 drop_priv();
72 T_ATEND(_reset_atm_diagnostic_flag);
73 uint32_t f = _save_atm_diagnostic_flag();
74 f ^= LIBTRACE_PRIVATE_DATA;
75 kern_return_t kr = _mutate_atm_diagnostic_flag(f);
76 T_EXPECT_MACH_ERROR(KERN_INVALID_ARGUMENT, kr,
77 "Deny change to atm_diagnostic_flag");
78 }