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