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