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