1*5e3eaea3SApple OSS Distributions /**
2*5e3eaea3SApple OSS Distributions * double_limit_test.c
3*5e3eaea3SApple OSS Distributions * DiagThresholdTest
4*5e3eaea3SApple OSS Distributions *
5*5e3eaea3SApple OSS Distributions * Test the check if reloading a memory diagnostics limit retriggers exceptions
6*5e3eaea3SApple OSS Distributions * Copyright (c) 2022 Apple Inc. All rights reserved.
7*5e3eaea3SApple OSS Distributions */
8*5e3eaea3SApple OSS Distributions #include <stdio.h>
9*5e3eaea3SApple OSS Distributions #include "vm/diag_threshold_test.h"
10*5e3eaea3SApple OSS Distributions #include <sys/kern_memorystatus.h>
11*5e3eaea3SApple OSS Distributions #include <darwintest.h>
12*5e3eaea3SApple OSS Distributions
13*5e3eaea3SApple OSS Distributions static void diag_threshold_test_limit_and_threshold_same(struct test_case *test_case, void *param);
14*5e3eaea3SApple OSS Distributions static test_case_t diag_threshold_test_limit_and_threshold_same_test = {
15*5e3eaea3SApple OSS Distributions .short_name = "diag_threshold_test_limit_and_threshold_same",
16*5e3eaea3SApple OSS Distributions .test_name = "Test on which a diag threshold and a limit is set with same value",
17*5e3eaea3SApple OSS Distributions .test_code = diag_threshold_test_limit_and_threshold_same,
18*5e3eaea3SApple OSS Distributions .result_already_present = FALSE,
19*5e3eaea3SApple OSS Distributions .exception_not_expected = FALSE,
20*5e3eaea3SApple OSS Distributions .exceptions_handled_in_test = TRUE,
21*5e3eaea3SApple OSS Distributions };
22*5e3eaea3SApple OSS Distributions
23*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
24*5e3eaea3SApple OSS Distributions T_META_ENABLED(TARGET_OS_IPHONE),
25*5e3eaea3SApple OSS Distributions T_META_NAMESPACE("xnu.vm.100432442"),
26*5e3eaea3SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
27*5e3eaea3SApple OSS Distributions T_META_OWNER("jsolsona"),
28*5e3eaea3SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("VM")
29*5e3eaea3SApple OSS Distributions );
30*5e3eaea3SApple OSS Distributions
31*5e3eaea3SApple OSS Distributions /**
32*5e3eaea3SApple OSS Distributions * This function sets a threshold, but is expected to fail, so we cannot use
33*5e3eaea3SApple OSS Distributions * the standard test threshold function
34*5e3eaea3SApple OSS Distributions */
35*5e3eaea3SApple OSS Distributions static bool
get_diagthreshold_limits(int * limit_param,boolean_t * status)36*5e3eaea3SApple OSS Distributions get_diagthreshold_limits(int *limit_param, boolean_t *status)
37*5e3eaea3SApple OSS Distributions {
38*5e3eaea3SApple OSS Distributions memorystatus_diag_memlimit_properties_t limit;
39*5e3eaea3SApple OSS Distributions diag_mem_threshold_log_test("Get threshold limit");
40*5e3eaea3SApple OSS Distributions int pid = getpid();
41*5e3eaea3SApple OSS Distributions int retValue = memorystatus_control(
42*5e3eaea3SApple OSS Distributions MEMORYSTATUS_CMD_GET_DIAG_LIMIT,
43*5e3eaea3SApple OSS Distributions pid,
44*5e3eaea3SApple OSS Distributions 0,
45*5e3eaea3SApple OSS Distributions &limit, sizeof(limit)
46*5e3eaea3SApple OSS Distributions );
47*5e3eaea3SApple OSS Distributions T_ASSERT_MACH_ERROR( retValue, KERN_SUCCESS, "Verification diagnostics threshold limit adjustment");
48*5e3eaea3SApple OSS Distributions *limit_param = (int)(limit.memlimit);
49*5e3eaea3SApple OSS Distributions *status = limit.threshold_enabled;
50*5e3eaea3SApple OSS Distributions
51*5e3eaea3SApple OSS Distributions return (retValue == KERN_SUCCESS) ? false : true;
52*5e3eaea3SApple OSS Distributions }
53*5e3eaea3SApple OSS Distributions static void
diag_threshold_test_limit_and_threshold_same(struct test_case * test_case,__unused void * param)54*5e3eaea3SApple OSS Distributions diag_threshold_test_limit_and_threshold_same(struct test_case *test_case, __unused void *param)
55*5e3eaea3SApple OSS Distributions {
56*5e3eaea3SApple OSS Distributions test_context_t *info = (test_context_t *)param;
57*5e3eaea3SApple OSS Distributions int limit_param;
58*5e3eaea3SApple OSS Distributions boolean_t status;
59*5e3eaea3SApple OSS Distributions
60*5e3eaea3SApple OSS Distributions dispatch_semaphore_signal(info->executor_ready_for_exceptions);
61*5e3eaea3SApple OSS Distributions diag_mem_set_jetsam_watermark(LOW_JETSAM_LIMIT);
62*5e3eaea3SApple OSS Distributions diag_mem_set_jetsam_limit(WORKING_LIMIT);
63*5e3eaea3SApple OSS Distributions bool retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT, true);
64*5e3eaea3SApple OSS Distributions retValue = get_diagthreshold_limits(&limit_param, &status);
65*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(false, status, "Threshold is disabled automatically");
66*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(WORKING_LIMIT, limit_param, "Adjusted threshold is correct");
67*5e3eaea3SApple OSS Distributions
68*5e3eaea3SApple OSS Distributions retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT << 1, true);
69*5e3eaea3SApple OSS Distributions diag_mem_threshold_log_test("Modifying threshold limit,expecting threshold is automatically enabled");
70*5e3eaea3SApple OSS Distributions retValue = get_diagthreshold_limits(&limit_param, &status);
71*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(true, status, "Threshold is enabled automatically");
72*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(WORKING_LIMIT << 1, limit_param, "Adjusted threshold is correct");
73*5e3eaea3SApple OSS Distributions
74*5e3eaea3SApple OSS Distributions retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT, true);
75*5e3eaea3SApple OSS Distributions diag_mem_set_jetsam_watermark(LOW_JETSAM_LIMIT );
76*5e3eaea3SApple OSS Distributions diag_mem_set_jetsam_limit(WORKING_LIMIT);
77*5e3eaea3SApple OSS Distributions retValue = get_diagthreshold_limits(&limit_param, &status);
78*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(false, status, "Threshold is disabled automatically");
79*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(WORKING_LIMIT, limit_param, "Adjusted threshold is correct");
80*5e3eaea3SApple OSS Distributions
81*5e3eaea3SApple OSS Distributions retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT << 1, true);
82*5e3eaea3SApple OSS Distributions diag_mem_threshold_log_test("Modifying threshold limit,expecting threshold is automatically enabled");
83*5e3eaea3SApple OSS Distributions retValue = get_diagthreshold_limits(&limit_param, &status);
84*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(true, status, "Threshold is enabled automatically");
85*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(WORKING_LIMIT << 1, limit_param, "Adjusted threshold is correct");
86*5e3eaea3SApple OSS Distributions
87*5e3eaea3SApple OSS Distributions
88*5e3eaea3SApple OSS Distributions diag_mem_set_jetsam_watermark(LOW_JETSAM_LIMIT << 1);
89*5e3eaea3SApple OSS Distributions diag_mem_set_jetsam_limit(WORKING_LIMIT << 1);
90*5e3eaea3SApple OSS Distributions diag_mem_threshold_log_test("Modifying jetsam limit,expecting threshold is automatically enabled");
91*5e3eaea3SApple OSS Distributions retValue = get_diagthreshold_limits(&limit_param, &status);
92*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(false, status, "Threshold is disabled automatically");
93*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(WORKING_LIMIT << 1, limit_param, "Adjusted threshold is correct");
94*5e3eaea3SApple OSS Distributions test_case->did_pass = TRUE;
95*5e3eaea3SApple OSS Distributions test_case->result_already_present = TRUE;
96*5e3eaea3SApple OSS Distributions }
97*5e3eaea3SApple OSS Distributions
98*5e3eaea3SApple OSS Distributions T_DECL(diag_threshold_test_limit_and_threshold_same,
99*5e3eaea3SApple OSS Distributions "Test on which a diag watermark and a threshold is set with same value"
100*5e3eaea3SApple OSS Distributions )
101*5e3eaea3SApple OSS Distributions {
102*5e3eaea3SApple OSS Distributions diag_mem_threshold_set_setup(&diag_threshold_test_limit_and_threshold_same_test);
103*5e3eaea3SApple OSS Distributions }
104