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_watermark_and_threshold_same(struct test_case *test_case, void *param);
14 static test_case_t diag_threshold_test_watermark_and_threshold_same_test = {
15 .short_name = "diag_threshold_test_watermark_and_threshold_same",
16 .test_name = "Test on which a limit watermark and a memory threshold is set with same value",
17 .test_code = diag_threshold_test_watermark_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_watermark_and_threshold_same(struct test_case * test_case,__unused void * param)54 diag_threshold_test_watermark_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(WORKING_LIMIT);
62 bool retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT, true);
63 retValue = get_diagthreshold_limits(&limit_param, &status);
64 T_ASSERT_EQ(false, status, "Threshold is disabled automatically");
65 T_ASSERT_EQ(WORKING_LIMIT, limit_param, "Adjusted threshold is correct");
66
67 retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT << 1, true);
68 diag_mem_threshold_log_test("Modifying threshold limit,expecting threshold is automatically enabled");
69 retValue = get_diagthreshold_limits(&limit_param, &status);
70 T_ASSERT_EQ(true, status, "Threshold is enabled automatically");
71 T_ASSERT_EQ(WORKING_LIMIT << 1, limit_param, "Adjusted threshold is correct");
72
73 retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT, true);
74 diag_mem_set_jetsam_watermark(WORKING_LIMIT);
75 retValue = get_diagthreshold_limits(&limit_param, &status);
76 T_ASSERT_EQ(false, status, "Threshold is disabled automatically");
77 T_ASSERT_EQ(WORKING_LIMIT, limit_param, "Adjusted threshold is correct");
78
79 retValue = set_memory_diagnostics_threshold_limits(WORKING_LIMIT << 1, true);
80 diag_mem_threshold_log_test("Modifying threshold limit,expecting threshold is automatically enabled");
81 retValue = get_diagthreshold_limits(&limit_param, &status);
82 T_ASSERT_EQ(true, status, "Threshold is enabled automatically");
83 T_ASSERT_EQ(WORKING_LIMIT << 1, limit_param, "Adjusted threshold is correct");
84
85
86 diag_mem_set_jetsam_watermark(WORKING_LIMIT << 1);
87 diag_mem_threshold_log_test("Modifying jetsam limit,expecting threshold is automatically enabled");
88 retValue = get_diagthreshold_limits(&limit_param, &status);
89 T_ASSERT_EQ(false, status, "Threshold is disabled automatically");
90 T_ASSERT_EQ(WORKING_LIMIT << 1, limit_param, "Adjusted threshold is correct");
91 test_case->did_pass = TRUE;
92 test_case->result_already_present = TRUE;
93 }
94
95 T_DECL(diag_threshold_test_watermark_and_threshold_same,
96 "Test on which a limit watermark and a memory threshold is set with same value"
97 )
98 {
99 diag_mem_threshold_set_setup(&diag_threshold_test_watermark_and_threshold_same_test);
100 }
101