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