File: libstatssocket_lazy.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (201 lines) | stat: -rw-r--r-- 6,354 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "libstatssocket_lazy.h"

#include <mutex>

#include <dlfcn.h>
#include <stdatomic.h>

#include "log/log.h"

#include "stats_event.h"
#include "stats_socket.h"

// This file provides a lazy interface to libstatssocket.so to address early boot dependencies.
// Specifically bootanimation, surfaceflinger, and lmkd run before the statsd APEX is loaded and
// libstatssocket.so is in the statsd APEX.

// Method pointers to libstatssocket methods are held in an array which simplifies checking
// all pointers are initialized.
enum MethodIndex {
    // Stats Event APIs in stats_event.h.
    k_AStatsEvent_obtain,
    k_AStatsEvent_build,
    k_AStatsEvent_write,
    k_AStatsEvent_release,
    k_AStatsEvent_setAtomId,
    k_AStatsEvent_writeInt32,
    k_AStatsEvent_writeInt64,
    k_AStatsEvent_writeFloat,
    k_AStatsEvent_writeBool,
    k_AStatsEvent_writeByteArray,
    k_AStatsEvent_writeString,
    k_AStatsEvent_writeAttributionChain,
    k_AStatsEvent_addBoolAnnotation,
    k_AStatsEvent_addInt32Annotation,

    // Stats Socket APIs in stats_socket.h.
    k_AStatsSocket_close,

    // Marker for count of methods
    k_MethodCount
};

// Table of methods pointers in libstatssocket APIs.
static void* g_Methods[k_MethodCount];

//
// Libstatssocket lazy loading.
//

static atomic_bool gPreventLibstatssocketLoading = false;  // Allows tests to block loading.

void PreventLibstatssocketLazyLoadingForTests() {
    gPreventLibstatssocketLoading.store(true);
}

static void* LoadLibstatssocket(int dlopen_flags) {
    if (gPreventLibstatssocketLoading.load()) {
        return nullptr;
    }
    return dlopen("libstatssocket.so", dlopen_flags);
}

//
// Initialization and symbol binding.

static void BindSymbol(void* handle, const char* name, enum MethodIndex index) {
    void* symbol = dlsym(handle, name);
    LOG_ALWAYS_FATAL_IF(symbol == nullptr, "Failed to find symbol '%s' in libstatssocket.so: %s",
                        name, dlerror());
    g_Methods[index] = symbol;
}

static void InitializeOnce() {
    void* handle = LoadLibstatssocket(RTLD_NOW);
    LOG_ALWAYS_FATAL_IF(handle == nullptr, "Failed to load libstatssocket.so: %s", dlerror());

#undef BIND_SYMBOL
#define BIND_SYMBOL(name) BindSymbol(handle, #name, k_##name);
    // Methods in stats_event.h.
    BIND_SYMBOL(AStatsEvent_obtain);
    BIND_SYMBOL(AStatsEvent_build);
    BIND_SYMBOL(AStatsEvent_write);
    BIND_SYMBOL(AStatsEvent_release);
    BIND_SYMBOL(AStatsEvent_setAtomId);
    BIND_SYMBOL(AStatsEvent_writeInt32);
    BIND_SYMBOL(AStatsEvent_writeInt64);
    BIND_SYMBOL(AStatsEvent_writeFloat);
    BIND_SYMBOL(AStatsEvent_writeBool);
    BIND_SYMBOL(AStatsEvent_writeByteArray);
    BIND_SYMBOL(AStatsEvent_writeString);
    BIND_SYMBOL(AStatsEvent_writeAttributionChain);
    BIND_SYMBOL(AStatsEvent_addBoolAnnotation);
    BIND_SYMBOL(AStatsEvent_addInt32Annotation);

    // Methods in stats_socket.h.
    BIND_SYMBOL(AStatsSocket_close);
#undef BIND_SYMBOL

    // Check every symbol is bound.
    for (int i = 0; i < k_MethodCount; ++i) {
        LOG_ALWAYS_FATAL_IF(g_Methods[i] == nullptr,
                            "Uninitialized method in libstatssocket_lazy at index: %d", i);
    }
}

static void EnsureInitialized() {
    static std::once_flag initialize_flag;
    std::call_once(initialize_flag, InitializeOnce);
}

#define INVOKE_METHOD(name, args...)                            \
    do {                                                        \
        EnsureInitialized();                                    \
        void* method = g_Methods[k_##name];                     \
        return reinterpret_cast<decltype(&name)>(method)(args); \
    } while (0)

//
// Forwarding for methods in stats_event.h.
//

AStatsEvent* AStatsEvent_obtain() {
    INVOKE_METHOD(AStatsEvent_obtain);
}

void AStatsEvent_build(AStatsEvent* event) {
    INVOKE_METHOD(AStatsEvent_build, event);
}

int AStatsEvent_write(AStatsEvent* event) {
    INVOKE_METHOD(AStatsEvent_write, event);
}

void AStatsEvent_release(AStatsEvent* event) {
    INVOKE_METHOD(AStatsEvent_release, event);
}

void AStatsEvent_setAtomId(AStatsEvent* event, uint32_t atomId) {
    INVOKE_METHOD(AStatsEvent_setAtomId, event, atomId);
}

void AStatsEvent_writeInt32(AStatsEvent* event, int32_t value) {
    INVOKE_METHOD(AStatsEvent_writeInt32, event, value);
}

void AStatsEvent_writeInt64(AStatsEvent* event, int64_t value) {
    INVOKE_METHOD(AStatsEvent_writeInt64, event, value);
}

void AStatsEvent_writeFloat(AStatsEvent* event, float value) {
    INVOKE_METHOD(AStatsEvent_writeFloat, event, value);
}

void AStatsEvent_writeBool(AStatsEvent* event, bool value) {
    INVOKE_METHOD(AStatsEvent_writeBool, event, value);
}

void AStatsEvent_writeByteArray(AStatsEvent* event, const uint8_t* buf, size_t numBytes) {
    INVOKE_METHOD(AStatsEvent_writeByteArray, event, buf, numBytes);
}

void AStatsEvent_writeString(AStatsEvent* event, const char* value) {
    INVOKE_METHOD(AStatsEvent_writeString, event, value);
}

void AStatsEvent_writeAttributionChain(AStatsEvent* event, const uint32_t* uids,
                                       const char* const* tags, uint8_t numNodes) {
    INVOKE_METHOD(AStatsEvent_writeAttributionChain, event, uids, tags, numNodes);
}

void AStatsEvent_addBoolAnnotation(AStatsEvent* event, uint8_t annotationId, bool value) {
    INVOKE_METHOD(AStatsEvent_addBoolAnnotation, event, annotationId, value);
}

void AStatsEvent_addInt32Annotation(AStatsEvent* event, uint8_t annotationId, int32_t value) {
    INVOKE_METHOD(AStatsEvent_addInt32Annotation, event, annotationId, value);
}

//
// Forwarding for methods in stats_socket.h.
//

void AStatsSocket_close() {
    INVOKE_METHOD(AStatsSocket_close);
}