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