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