1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 2021 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions *
4*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions *
6*2c2f96dcSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions *
15*2c2f96dcSApple OSS Distributions * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions *
18*2c2f96dcSApple OSS Distributions * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions * limitations under the License.
25*2c2f96dcSApple OSS Distributions *
26*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions */
28*2c2f96dcSApple OSS Distributions
29*2c2f96dcSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*2c2f96dcSApple OSS Distributions #include <kern/debug.h>
31*2c2f96dcSApple OSS Distributions #include <libkern/coreanalytics/coreanalytics.h>
32*2c2f96dcSApple OSS Distributions #include <libkern/coreanalytics/coreanalytics_shim.h>
33*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSBoolean.h>
34*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSDictionary.h>
35*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSNumber.h>
36*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSMetaClass.h>
37*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSString.h>
38*2c2f96dcSApple OSS Distributions #include <os/atomic.h>
39*2c2f96dcSApple OSS Distributions #include <os/log.h>
40*2c2f96dcSApple OSS Distributions #include <sys/errno.h>
41*2c2f96dcSApple OSS Distributions
42*2c2f96dcSApple OSS Distributions /* Pulled from CoreAnalyticsHub.h in CoreAnalytics project */
43*2c2f96dcSApple OSS Distributions static const char *kCoreAnalyticsMatchingClassName = "CoreAnalyticsHub";
44*2c2f96dcSApple OSS Distributions
45*2c2f96dcSApple OSS Distributions core_analytics_hub_functions_t *core_analytics_hub_functions = NULL;
46*2c2f96dcSApple OSS Distributions
47*2c2f96dcSApple OSS Distributions core_analytics_family_service_t *
core_analytics_family_match()48*2c2f96dcSApple OSS Distributions core_analytics_family_match()
49*2c2f96dcSApple OSS Distributions {
50*2c2f96dcSApple OSS Distributions OSSharedPtr<OSDictionary> dict = IOService::serviceMatching(kCoreAnalyticsMatchingClassName);
51*2c2f96dcSApple OSS Distributions OSSharedPtr<IOService> service = nullptr;
52*2c2f96dcSApple OSS Distributions if (dict) {
53*2c2f96dcSApple OSS Distributions service = IOService::waitForMatchingService(dict.get());
54*2c2f96dcSApple OSS Distributions } else {
55*2c2f96dcSApple OSS Distributions os_log_with_startup_serial(OS_LOG_DEFAULT, "No service matching %s", kCoreAnalyticsMatchingClassName);
56*2c2f96dcSApple OSS Distributions }
57*2c2f96dcSApple OSS Distributions if (service) {
58*2c2f96dcSApple OSS Distributions return service.detach();
59*2c2f96dcSApple OSS Distributions } else {
60*2c2f96dcSApple OSS Distributions os_log_with_startup_serial(OS_LOG_DEFAULT, "Unable to match CoreAnalyticsHub");
61*2c2f96dcSApple OSS Distributions return nullptr;
62*2c2f96dcSApple OSS Distributions }
63*2c2f96dcSApple OSS Distributions }
64*2c2f96dcSApple OSS Distributions
65*2c2f96dcSApple OSS Distributions void
core_analytics_family_release(core_analytics_family_service_t * service)66*2c2f96dcSApple OSS Distributions core_analytics_family_release(core_analytics_family_service_t *service)
67*2c2f96dcSApple OSS Distributions {
68*2c2f96dcSApple OSS Distributions service->release();
69*2c2f96dcSApple OSS Distributions }
70*2c2f96dcSApple OSS Distributions
71*2c2f96dcSApple OSS Distributions typedef struct core_analytics_serialized_event_s {
72*2c2f96dcSApple OSS Distributions OSSharedPtr<OSString> case_event_name;
73*2c2f96dcSApple OSS Distributions OSSharedPtr<OSDictionary> case_event;
74*2c2f96dcSApple OSS Distributions } core_analytics_serialized_event_t;
75*2c2f96dcSApple OSS Distributions
76*2c2f96dcSApple OSS Distributions /*
77*2c2f96dcSApple OSS Distributions * Stringifying CA_BOOL is different in C vs. C++ b/c the underlying
78*2c2f96dcSApple OSS Distributions * bool typename is different (_Bool vs bool).
79*2c2f96dcSApple OSS Distributions * We want to support both since the event may have been defined in C or C++ code.
80*2c2f96dcSApple OSS Distributions */
81*2c2f96dcSApple OSS Distributions extern const char *core_analytics_ca_bool_c_stringified;
82*2c2f96dcSApple OSS Distributions const char *core_analytics_ca_bool_cpp_stringified = _CA_STRINGIFY_EXPAND(CA_BOOL);
83*2c2f96dcSApple OSS Distributions
84*2c2f96dcSApple OSS Distributions static OSSharedPtr<OSObject>
serialize_event_field(const char ** field_spec,ptrdiff_t * event,OSSharedPtr<const OSSymbol> * key)85*2c2f96dcSApple OSS Distributions serialize_event_field(const char **field_spec, ptrdiff_t *event, OSSharedPtr<const OSSymbol> *key)
86*2c2f96dcSApple OSS Distributions {
87*2c2f96dcSApple OSS Distributions OSSharedPtr<OSObject> field = nullptr;
88*2c2f96dcSApple OSS Distributions ptrdiff_t field_value = (ptrdiff_t) *event;
89*2c2f96dcSApple OSS Distributions uint64_t number_value;
90*2c2f96dcSApple OSS Distributions const char *str_value;
91*2c2f96dcSApple OSS Distributions size_t str_len = 0;
92*2c2f96dcSApple OSS Distributions bool bool_value;
93*2c2f96dcSApple OSS Distributions
94*2c2f96dcSApple OSS Distributions if (strcmp(*field_spec, _CA_STRINGIFY_EXPAND(CA_INT)) == 0) {
95*2c2f96dcSApple OSS Distributions number_value = *(const uint64_t *)field_value;
96*2c2f96dcSApple OSS Distributions field = OSNumber::withNumber(number_value, sizeof(uint64_t) * 8);
97*2c2f96dcSApple OSS Distributions *event += sizeof(const uint64_t);
98*2c2f96dcSApple OSS Distributions } else if (strcmp(*field_spec, core_analytics_ca_bool_cpp_stringified) == 0 ||
99*2c2f96dcSApple OSS Distributions strcmp(*field_spec, core_analytics_ca_bool_c_stringified) == 0) {
100*2c2f96dcSApple OSS Distributions bool_value = *(const bool *)field_value;
101*2c2f96dcSApple OSS Distributions field = OSBoolean::withBoolean(bool_value);
102*2c2f96dcSApple OSS Distributions *event += sizeof(const bool);
103*2c2f96dcSApple OSS Distributions } else if ((str_len = core_analytics_field_is_string(*field_spec)) != 0) {
104*2c2f96dcSApple OSS Distributions str_value = (const char *) field_value;
105*2c2f96dcSApple OSS Distributions assert(strlen(str_value) < str_len);
106*2c2f96dcSApple OSS Distributions if (strlen(str_value) < str_len) {
107*2c2f96dcSApple OSS Distributions field = OSString::withCString(str_value);
108*2c2f96dcSApple OSS Distributions } else {
109*2c2f96dcSApple OSS Distributions field = nullptr;
110*2c2f96dcSApple OSS Distributions }
111*2c2f96dcSApple OSS Distributions *event += str_len;
112*2c2f96dcSApple OSS Distributions } else {
113*2c2f96dcSApple OSS Distributions panic("Unknown CoreAnalytics event type: %s.", *field_spec);
114*2c2f96dcSApple OSS Distributions }
115*2c2f96dcSApple OSS Distributions *field_spec += strlen(*field_spec) + 1;
116*2c2f96dcSApple OSS Distributions
117*2c2f96dcSApple OSS Distributions *key = OSSymbol::withCString(*field_spec);
118*2c2f96dcSApple OSS Distributions assert(*key != nullptr);
119*2c2f96dcSApple OSS Distributions if (!*key) {
120*2c2f96dcSApple OSS Distributions return nullptr;
121*2c2f96dcSApple OSS Distributions }
122*2c2f96dcSApple OSS Distributions *field_spec += strlen(*field_spec) + 1;
123*2c2f96dcSApple OSS Distributions return field;
124*2c2f96dcSApple OSS Distributions }
125*2c2f96dcSApple OSS Distributions
126*2c2f96dcSApple OSS Distributions static int
core_analytics_serialize_event(const char * event_spec,const ca_event_t event,core_analytics_serialized_event_t * serialized_event)127*2c2f96dcSApple OSS Distributions core_analytics_serialize_event(const char *event_spec,
128*2c2f96dcSApple OSS Distributions const ca_event_t event,
129*2c2f96dcSApple OSS Distributions core_analytics_serialized_event_t *serialized_event)
130*2c2f96dcSApple OSS Distributions {
131*2c2f96dcSApple OSS Distributions serialized_event->case_event_name = nullptr;
132*2c2f96dcSApple OSS Distributions serialized_event->case_event = nullptr;
133*2c2f96dcSApple OSS Distributions OSSharedPtr<OSDictionary> dict = nullptr;
134*2c2f96dcSApple OSS Distributions bool success;
135*2c2f96dcSApple OSS Distributions OSSharedPtr<OSString> name = OSString::withCStringNoCopy(event_spec);
136*2c2f96dcSApple OSS Distributions if (!name) {
137*2c2f96dcSApple OSS Distributions return ENOMEM;
138*2c2f96dcSApple OSS Distributions }
139*2c2f96dcSApple OSS Distributions dict = OSDictionary::withCapacity(1);
140*2c2f96dcSApple OSS Distributions if (!dict) {
141*2c2f96dcSApple OSS Distributions return ENOMEM;
142*2c2f96dcSApple OSS Distributions }
143*2c2f96dcSApple OSS Distributions const char *spec_curr = event_spec + strlen(event_spec) + 1;
144*2c2f96dcSApple OSS Distributions ptrdiff_t event_curr = (ptrdiff_t) event->data;
145*2c2f96dcSApple OSS Distributions while (strlen(spec_curr) != 0) {
146*2c2f96dcSApple OSS Distributions OSSharedPtr<const OSSymbol> key = NULL;
147*2c2f96dcSApple OSS Distributions OSSharedPtr<OSObject> object = serialize_event_field(&spec_curr, &event_curr, &key);
148*2c2f96dcSApple OSS Distributions if (!object) {
149*2c2f96dcSApple OSS Distributions return ENOMEM;
150*2c2f96dcSApple OSS Distributions }
151*2c2f96dcSApple OSS Distributions success = dict->setObject(key, object);
152*2c2f96dcSApple OSS Distributions if (!success) {
153*2c2f96dcSApple OSS Distributions return ENOMEM;
154*2c2f96dcSApple OSS Distributions }
155*2c2f96dcSApple OSS Distributions }
156*2c2f96dcSApple OSS Distributions
157*2c2f96dcSApple OSS Distributions serialized_event->case_event_name = name;
158*2c2f96dcSApple OSS Distributions serialized_event->case_event = dict;
159*2c2f96dcSApple OSS Distributions return 0;
160*2c2f96dcSApple OSS Distributions }
161*2c2f96dcSApple OSS Distributions
162*2c2f96dcSApple OSS Distributions int
core_analytics_send_event_lazy(core_analytics_family_service_t * core_analytics_hub,const char * event_spec,const ca_event_t event)163*2c2f96dcSApple OSS Distributions core_analytics_send_event_lazy(
164*2c2f96dcSApple OSS Distributions core_analytics_family_service_t *core_analytics_hub,
165*2c2f96dcSApple OSS Distributions const char *event_spec, const ca_event_t event)
166*2c2f96dcSApple OSS Distributions {
167*2c2f96dcSApple OSS Distributions int ret = 0;
168*2c2f96dcSApple OSS Distributions bool success;
169*2c2f96dcSApple OSS Distributions static constexpr size_t kMaxRetries = 5;
170*2c2f96dcSApple OSS Distributions static constexpr uint64_t kDelayBetweenRetries = 1000; // microseconds
171*2c2f96dcSApple OSS Distributions core_analytics_serialized_event_t serialized;
172*2c2f96dcSApple OSS Distributions if (!core_analytics_hub_functions) {
173*2c2f96dcSApple OSS Distributions os_log_with_startup_serial(OS_LOG_DEFAULT, "Dropping attempt to send a CoreAnalytics event before CoreAnalyticsHub has registered.");
174*2c2f96dcSApple OSS Distributions return EAGAIN;
175*2c2f96dcSApple OSS Distributions }
176*2c2f96dcSApple OSS Distributions
177*2c2f96dcSApple OSS Distributions ret = core_analytics_serialize_event(event_spec, event, &serialized);
178*2c2f96dcSApple OSS Distributions if (ret != 0) {
179*2c2f96dcSApple OSS Distributions os_log_with_startup_serial(OS_LOG_DEFAULT, "Unable to serialize CoreAnalytics event: %d", ret);
180*2c2f96dcSApple OSS Distributions return ret;
181*2c2f96dcSApple OSS Distributions }
182*2c2f96dcSApple OSS Distributions for (size_t i = 0; i < kMaxRetries; i++) {
183*2c2f96dcSApple OSS Distributions success = core_analytics_hub_functions->analytics_send_event_lazy(core_analytics_hub, serialized.case_event_name.get(), serialized.case_event.get());
184*2c2f96dcSApple OSS Distributions if (success) {
185*2c2f96dcSApple OSS Distributions break;
186*2c2f96dcSApple OSS Distributions } else {
187*2c2f96dcSApple OSS Distributions if (i != kMaxRetries - 1) {
188*2c2f96dcSApple OSS Distributions os_log_with_startup_serial(OS_LOG_DEFAULT, "Unable to send CoreAnalytics event. Delaying for %llu u.s. to see if the queue drains.", kDelayBetweenRetries);
189*2c2f96dcSApple OSS Distributions delay(kDelayBetweenRetries);
190*2c2f96dcSApple OSS Distributions } else {
191*2c2f96dcSApple OSS Distributions os_log_with_startup_serial(OS_LOG_DEFAULT, "Unable to send CoreAnalytics event. Giving up.");
192*2c2f96dcSApple OSS Distributions ret = EAGAIN;
193*2c2f96dcSApple OSS Distributions break;
194*2c2f96dcSApple OSS Distributions }
195*2c2f96dcSApple OSS Distributions }
196*2c2f96dcSApple OSS Distributions }
197*2c2f96dcSApple OSS Distributions return ret;
198*2c2f96dcSApple OSS Distributions }
199*2c2f96dcSApple OSS Distributions
200*2c2f96dcSApple OSS Distributions void
core_analytics_hub_register(core_analytics_hub_functions_t * fns)201*2c2f96dcSApple OSS Distributions core_analytics_hub_register(core_analytics_hub_functions_t *fns)
202*2c2f96dcSApple OSS Distributions {
203*2c2f96dcSApple OSS Distributions if (fns->version != CORE_ANALYTICS_FUNCTIONS_TABLE_VERSION) {
204*2c2f96dcSApple OSS Distributions panic("CoreAnalyticsHub is out of sync with xnu. CoreAnalyticsHub table version: %d. xnu table version: %d", fns->version, CORE_ANALYTICS_FUNCTIONS_TABLE_VERSION);
205*2c2f96dcSApple OSS Distributions }
206*2c2f96dcSApple OSS Distributions core_analytics_hub_functions = fns;
207*2c2f96dcSApple OSS Distributions os_log_with_startup_serial(OS_LOG_DEFAULT, "Registered CoreAnalyticsHub functions with xnu.");
208*2c2f96dcSApple OSS Distributions }
209