• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "java_writer_q.h"
18 
19 #include "utils.h"
20 
21 namespace android {
22 namespace stats_log_api_gen {
23 
write_java_q_logging_constants(FILE * out,const string & indent)24 void write_java_q_logging_constants(FILE* out, const string& indent) {
25     fprintf(out, "%s// Payload limits.\n", indent.c_str());
26     fprintf(out, "%sprivate static final int LOGGER_ENTRY_MAX_PAYLOAD = 4068;\n", indent.c_str());
27     fprintf(out,
28             "%sprivate static final int MAX_EVENT_PAYLOAD = "
29             "LOGGER_ENTRY_MAX_PAYLOAD - 4;\n",
30             indent.c_str());
31 
32     // Value types. Must match with EventLog.java and log.h.
33     fprintf(out, "\n");
34     fprintf(out, "%s// Value types.\n", indent.c_str());
35     fprintf(out, "%sprivate static final byte INT_TYPE = 0;\n", indent.c_str());
36     fprintf(out, "%sprivate static final byte LONG_TYPE = 1;\n", indent.c_str());
37     fprintf(out, "%sprivate static final byte STRING_TYPE = 2;\n", indent.c_str());
38     fprintf(out, "%sprivate static final byte LIST_TYPE = 3;\n", indent.c_str());
39     fprintf(out, "%sprivate static final byte FLOAT_TYPE = 4;\n", indent.c_str());
40 
41     // Size of each value type.
42     // Booleans, ints, floats, and enums take 5 bytes, 1 for the type and 4 for
43     // the value.
44     fprintf(out, "\n");
45     fprintf(out, "%s// Size of each value type.\n", indent.c_str());
46     fprintf(out, "%sprivate static final int INT_TYPE_SIZE = 5;\n", indent.c_str());
47     fprintf(out, "%sprivate static final int FLOAT_TYPE_SIZE = 5;\n", indent.c_str());
48     // Longs take 9 bytes, 1 for the type and 8 for the value.
49     fprintf(out, "%sprivate static final int LONG_TYPE_SIZE = 9;\n", indent.c_str());
50     // Strings take 5 metadata bytes: 1 byte is for the type, 4 are for the
51     // length.
52     fprintf(out, "%sprivate static final int STRING_TYPE_OVERHEAD = 5;\n", indent.c_str());
53     fprintf(out, "%sprivate static final int LIST_TYPE_OVERHEAD = 2;\n", indent.c_str());
54 }
55 
write_java_methods_q_schema(FILE * out,const SignatureInfoMap & signatureInfoMap,const AtomDecl & attributionDecl,const string & indent)56 int write_java_methods_q_schema(FILE* out, const SignatureInfoMap& signatureInfoMap,
57                                 const AtomDecl& attributionDecl, const string& indent) {
58     int requiredHelpers = 0;
59     for (auto signatureInfoMapIt = signatureInfoMap.begin();
60          signatureInfoMapIt != signatureInfoMap.end(); signatureInfoMapIt++) {
61         // Print method signature.
62         vector<java_type_t> signature = signatureInfoMapIt->first;
63         fprintf(out, "%spublic static void write(int code", indent.c_str());
64         int argIndex = 1;
65         for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end();
66              arg++) {
67             if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
68                 for (const auto& chainField : attributionDecl.fields) {
69                     fprintf(out, ", %s[] %s", java_type_name(chainField.javaType),
70                             chainField.name.c_str());
71                 }
72             } else {
73                 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
74             }
75             argIndex++;
76         }
77         fprintf(out, ") {\n");
78 
79         // Calculate the size of the buffer.
80         fprintf(out, "%s    // Initial overhead of the list, timestamp, and atom tag.\n",
81                 indent.c_str());
82         fprintf(out,
83                 "%s    int needed = LIST_TYPE_OVERHEAD + LONG_TYPE_SIZE + "
84                 "INT_TYPE_SIZE;\n",
85                 indent.c_str());
86         argIndex = 1;
87         for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end();
88              arg++) {
89             switch (*arg) {
90                 case JAVA_TYPE_BOOLEAN:
91                 case JAVA_TYPE_INT:
92                 case JAVA_TYPE_FLOAT:
93                 case JAVA_TYPE_ENUM:
94                     fprintf(out, "%s    needed += INT_TYPE_SIZE;\n", indent.c_str());
95                     break;
96                 case JAVA_TYPE_LONG:
97                     // Longs take 9 bytes, 1 for the type and 8 for the value.
98                     fprintf(out, "%s    needed += LONG_TYPE_SIZE;\n", indent.c_str());
99                     break;
100                 case JAVA_TYPE_STRING:
101                     // Strings take 5 metadata bytes + length of byte encoded string.
102                     fprintf(out, "%s    if (arg%d == null) {\n", indent.c_str(), argIndex);
103                     fprintf(out, "%s        arg%d = \"\";\n", indent.c_str(), argIndex);
104                     fprintf(out, "%s    }\n", indent.c_str());
105                     fprintf(out,
106                             "%s    byte[] arg%dBytes = "
107                             "arg%d.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n",
108                             indent.c_str(), argIndex, argIndex);
109                     fprintf(out, "%s    needed += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n",
110                             indent.c_str(), argIndex);
111                     break;
112                 case JAVA_TYPE_BYTE_ARRAY:
113                     // Byte arrays take 5 metadata bytes + length of byte array.
114                     fprintf(out, "%s    if (arg%d == null) {\n", indent.c_str(), argIndex);
115                     fprintf(out, "%s        arg%d = new byte[0];\n", indent.c_str(), argIndex);
116                     fprintf(out, "%s    }\n", indent.c_str());
117                     fprintf(out, "%s    needed += STRING_TYPE_OVERHEAD + arg%d.length;\n",
118                             indent.c_str(), argIndex);
119                     break;
120                 case JAVA_TYPE_ATTRIBUTION_CHAIN: {
121                     const char* uidName = attributionDecl.fields.front().name.c_str();
122                     const char* tagName = attributionDecl.fields.back().name.c_str();
123                     // Null checks on the params.
124                     fprintf(out, "%s    if (%s == null) {\n", indent.c_str(), uidName);
125                     fprintf(out, "%s        %s = new %s[0];\n", indent.c_str(), uidName,
126                             java_type_name(attributionDecl.fields.front().javaType));
127                     fprintf(out, "%s    }\n", indent.c_str());
128                     fprintf(out, "%s    if (%s == null) {\n", indent.c_str(), tagName);
129                     fprintf(out, "%s        %s = new %s[0];\n", indent.c_str(), tagName,
130                             java_type_name(attributionDecl.fields.back().javaType));
131                     fprintf(out, "%s    }\n", indent.c_str());
132 
133                     // First check that the lengths of the uid and tag arrays are the
134                     // same.
135                     fprintf(out, "%s    if (%s.length != %s.length) {\n", indent.c_str(), uidName,
136                             tagName);
137                     fprintf(out, "%s        return;\n", indent.c_str());
138                     fprintf(out, "%s    }\n", indent.c_str());
139                     fprintf(out, "%s    int attrSize = LIST_TYPE_OVERHEAD;\n", indent.c_str());
140                     fprintf(out, "%s    for (int i = 0; i < %s.length; i++) {\n", indent.c_str(),
141                             tagName);
142                     fprintf(out, "%s        String str%d = (%s[i] == null) ? \"\" : %s[i];\n",
143                             indent.c_str(), argIndex, tagName, tagName);
144                     fprintf(out,
145                             "%s        int str%dlen = "
146                             "str%d.getBytes(java.nio.charset.StandardCharsets.UTF_8)."
147                             "length;\n",
148                             indent.c_str(), argIndex, argIndex);
149                     fprintf(out,
150                             "%s        attrSize += "
151                             "LIST_TYPE_OVERHEAD + INT_TYPE_SIZE + STRING_TYPE_OVERHEAD + "
152                             "str%dlen;\n",
153                             indent.c_str(), argIndex);
154                     fprintf(out, "%s    }\n", indent.c_str());
155                     fprintf(out, "%s    needed += attrSize;\n", indent.c_str());
156                     break;
157                 }
158                 default:
159                     // Unsupported types: OBJECT, DOUBLE.
160                     fprintf(stderr, "Module logging does not yet support Object and Double.\n");
161                     return 1;
162             }
163             argIndex++;
164         }
165 
166         // Now we have the size that is needed. Check for overflow and return if
167         // needed.
168         fprintf(out, "%s    if (needed > MAX_EVENT_PAYLOAD) {\n", indent.c_str());
169         fprintf(out, "%s        return;\n", indent.c_str());
170         fprintf(out, "%s    }\n", indent.c_str());
171 
172         // Create new buffer, and associated data types.
173         fprintf(out, "%s    byte[] buff = new byte[needed];\n", indent.c_str());
174         fprintf(out, "%s    int pos = 0;\n", indent.c_str());
175 
176         // Initialize the buffer with list data type.
177         fprintf(out, "%s    buff[pos] = LIST_TYPE;\n", indent.c_str());
178         fprintf(out, "%s    buff[pos + 1] = %zu;\n", indent.c_str(), signature.size() + 2);
179         fprintf(out, "%s    pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
180 
181         // Write timestamp.
182         fprintf(out, "%s    long elapsedRealtime = SystemClock.elapsedRealtimeNanos();\n",
183                 indent.c_str());
184         fprintf(out, "%s    buff[pos] = LONG_TYPE;\n", indent.c_str());
185         fprintf(out, "%s    copyLong(buff, pos + 1, elapsedRealtime);\n", indent.c_str());
186         fprintf(out, "%s    pos += LONG_TYPE_SIZE;\n", indent.c_str());
187 
188         // Write atom code.
189         fprintf(out, "%s    buff[pos] = INT_TYPE;\n", indent.c_str());
190         fprintf(out, "%s    copyInt(buff, pos + 1, code);\n", indent.c_str());
191         fprintf(out, "%s    pos += INT_TYPE_SIZE;\n", indent.c_str());
192 
193         // Write the args.
194         argIndex = 1;
195         for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end();
196              arg++) {
197             switch (*arg) {
198                 case JAVA_TYPE_BOOLEAN:
199                     fprintf(out, "%s    buff[pos] = INT_TYPE;\n", indent.c_str());
200                     fprintf(out, "%s    copyInt(buff, pos + 1, arg%d? 1 : 0);\n", indent.c_str(),
201                             argIndex);
202                     fprintf(out, "%s    pos += INT_TYPE_SIZE;\n", indent.c_str());
203                     break;
204                 case JAVA_TYPE_INT:
205                 case JAVA_TYPE_ENUM:
206                     fprintf(out, "%s    buff[pos] = INT_TYPE;\n", indent.c_str());
207                     fprintf(out, "%s    copyInt(buff, pos + 1, arg%d);\n", indent.c_str(),
208                             argIndex);
209                     fprintf(out, "%s    pos += INT_TYPE_SIZE;\n", indent.c_str());
210                     break;
211                 case JAVA_TYPE_FLOAT:
212                     requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT;
213                     fprintf(out, "%s    buff[pos] = FLOAT_TYPE;\n", indent.c_str());
214                     fprintf(out, "%s    copyFloat(buff, pos + 1, arg%d);\n", indent.c_str(),
215                             argIndex);
216                     fprintf(out, "%s    pos += FLOAT_TYPE_SIZE;\n", indent.c_str());
217                     break;
218                 case JAVA_TYPE_LONG:
219                     fprintf(out, "%s    buff[pos] = LONG_TYPE;\n", indent.c_str());
220                     fprintf(out, "%s    copyLong(buff, pos + 1, arg%d);\n", indent.c_str(),
221                             argIndex);
222                     fprintf(out, "%s    pos += LONG_TYPE_SIZE;\n", indent.c_str());
223                     break;
224                 case JAVA_TYPE_STRING:
225                     fprintf(out, "%s    buff[pos] = STRING_TYPE;\n", indent.c_str());
226                     fprintf(out, "%s    copyInt(buff, pos + 1, arg%dBytes.length);\n",
227                             indent.c_str(), argIndex);
228                     fprintf(out,
229                             "%s    System.arraycopy("
230                             "arg%dBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, "
231                             "arg%dBytes.length);\n",
232                             indent.c_str(), argIndex, argIndex);
233                     fprintf(out, "%s    pos += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n",
234                             indent.c_str(), argIndex);
235                     break;
236                 case JAVA_TYPE_BYTE_ARRAY:
237                     fprintf(out, "%s    buff[pos] = STRING_TYPE;\n", indent.c_str());
238                     fprintf(out, "%s    copyInt(buff, pos + 1, arg%d.length);\n", indent.c_str(),
239                             argIndex);
240                     fprintf(out,
241                             "%s    System.arraycopy("
242                             "arg%d, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%d.length);\n",
243                             indent.c_str(), argIndex, argIndex);
244                     fprintf(out, "%s    pos += STRING_TYPE_OVERHEAD + arg%d.length;\n",
245                             indent.c_str(), argIndex);
246                     break;
247                 case JAVA_TYPE_ATTRIBUTION_CHAIN: {
248                     requiredHelpers |= JAVA_MODULE_REQUIRES_ATTRIBUTION;
249                     const char* uidName = attributionDecl.fields.front().name.c_str();
250                     const char* tagName = attributionDecl.fields.back().name.c_str();
251 
252                     fprintf(out, "%s    writeAttributionChain(buff, pos, %s, %s);\n",
253                             indent.c_str(), uidName, tagName);
254                     fprintf(out, "%s    pos += attrSize;\n", indent.c_str());
255                     break;
256                 }
257                 default:
258                     // Unsupported types: OBJECT, DOUBLE.
259                     fprintf(stderr, "Object and Double are not supported in module logging");
260                     return 1;
261             }
262             argIndex++;
263         }
264 
265         fprintf(out, "%s    StatsLog.writeRaw(buff, pos);\n", indent.c_str());
266         fprintf(out, "%s}\n", indent.c_str());
267         fprintf(out, "\n");
268     }
269 
270     write_java_helpers_for_q_schema_methods(out, attributionDecl, requiredHelpers, indent);
271 
272     return 0;
273 }
274 
write_java_helpers_for_q_schema_methods(FILE * out,const AtomDecl & attributionDecl,const int requiredHelpers,const string & indent)275 void write_java_helpers_for_q_schema_methods(FILE* out, const AtomDecl& attributionDecl,
276                                              const int requiredHelpers, const string& indent) {
277     fprintf(out, "\n");
278     fprintf(out, "%s// Helper methods for copying primitives\n", indent.c_str());
279     fprintf(out, "%sprivate static void copyInt(byte[] buff, int pos, int val) {\n",
280             indent.c_str());
281     fprintf(out, "%s    buff[pos] = (byte) (val);\n", indent.c_str());
282     fprintf(out, "%s    buff[pos + 1] = (byte) (val >> 8);\n", indent.c_str());
283     fprintf(out, "%s    buff[pos + 2] = (byte) (val >> 16);\n", indent.c_str());
284     fprintf(out, "%s    buff[pos + 3] = (byte) (val >> 24);\n", indent.c_str());
285     fprintf(out, "%s    return;\n", indent.c_str());
286     fprintf(out, "%s}\n", indent.c_str());
287     fprintf(out, "\n");
288 
289     fprintf(out, "%sprivate static void copyLong(byte[] buff, int pos, long val) {\n",
290             indent.c_str());
291     fprintf(out, "%s    buff[pos] = (byte) (val);\n", indent.c_str());
292     fprintf(out, "%s    buff[pos + 1] = (byte) (val >> 8);\n", indent.c_str());
293     fprintf(out, "%s    buff[pos + 2] = (byte) (val >> 16);\n", indent.c_str());
294     fprintf(out, "%s    buff[pos + 3] = (byte) (val >> 24);\n", indent.c_str());
295     fprintf(out, "%s    buff[pos + 4] = (byte) (val >> 32);\n", indent.c_str());
296     fprintf(out, "%s    buff[pos + 5] = (byte) (val >> 40);\n", indent.c_str());
297     fprintf(out, "%s    buff[pos + 6] = (byte) (val >> 48);\n", indent.c_str());
298     fprintf(out, "%s    buff[pos + 7] = (byte) (val >> 56);\n", indent.c_str());
299     fprintf(out, "%s    return;\n", indent.c_str());
300     fprintf(out, "%s}\n", indent.c_str());
301     fprintf(out, "\n");
302 
303     if (requiredHelpers & JAVA_MODULE_REQUIRES_FLOAT) {
304         fprintf(out, "%sprivate static void copyFloat(byte[] buff, int pos, float val) {\n",
305                 indent.c_str());
306         fprintf(out, "%s    copyInt(buff, pos, Float.floatToIntBits(val));\n", indent.c_str());
307         fprintf(out, "%s    return;\n", indent.c_str());
308         fprintf(out, "%s}\n", indent.c_str());
309         fprintf(out, "\n");
310     }
311 
312     if (requiredHelpers & JAVA_MODULE_REQUIRES_ATTRIBUTION) {
313         fprintf(out, "%sprivate static void writeAttributionChain(byte[] buff, int pos",
314                 indent.c_str());
315         for (const auto& chainField : attributionDecl.fields) {
316             fprintf(out, ", %s[] %s", java_type_name(chainField.javaType), chainField.name.c_str());
317         }
318         fprintf(out, ") {\n");
319 
320         const char* uidName = attributionDecl.fields.front().name.c_str();
321         const char* tagName = attributionDecl.fields.back().name.c_str();
322 
323         // Write the first list begin.
324         fprintf(out, "%s    buff[pos] = LIST_TYPE;\n", indent.c_str());
325         fprintf(out, "%s    buff[pos + 1] = (byte) (%s.length);\n", indent.c_str(), tagName);
326         fprintf(out, "%s    pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
327 
328         // Iterate through the attribution chain and write the nodes.
329         fprintf(out, "%s    for (int i = 0; i < %s.length; i++) {\n", indent.c_str(), tagName);
330         // Write the list begin.
331         fprintf(out, "%s        buff[pos] = LIST_TYPE;\n", indent.c_str());
332         fprintf(out, "%s        buff[pos + 1] = %lu;\n", indent.c_str(),
333                 attributionDecl.fields.size());
334         fprintf(out, "%s        pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
335 
336         // Write the uid.
337         fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
338         fprintf(out, "%s        copyInt(buff, pos + 1, %s[i]);\n", indent.c_str(), uidName);
339         fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
340 
341         // Write the tag.
342         fprintf(out, "%s        String %sStr = (%s[i] == null) ? \"\" : %s[i];\n", indent.c_str(),
343                 tagName, tagName, tagName);
344         fprintf(out,
345                 "%s        byte[] %sByte = "
346                 "%sStr.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n",
347                 indent.c_str(), tagName, tagName);
348         fprintf(out, "%s        buff[pos] = STRING_TYPE;\n", indent.c_str());
349         fprintf(out, "%s        copyInt(buff, pos + 1, %sByte.length);\n", indent.c_str(), tagName);
350         fprintf(out,
351                 "%s        System.arraycopy("
352                 "%sByte, 0, buff, pos + STRING_TYPE_OVERHEAD, %sByte.length);\n",
353                 indent.c_str(), tagName, tagName);
354         fprintf(out, "%s        pos += STRING_TYPE_OVERHEAD + %sByte.length;\n", indent.c_str(),
355                 tagName);
356         fprintf(out, "%s    }\n", indent.c_str());
357         fprintf(out, "%s}\n", indent.c_str());
358         fprintf(out, "\n");
359     }
360 }
361 
362 }  // namespace stats_log_api_gen
363 }  // namespace android
364