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