lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/python3
# Copyright (C) 2017 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.
 
#
# Generates the src/art/Test988Intrinsics.java file.
# Re-run this every time art/compiler/intrinics_list.h is modified.
#
# $> python3.4 gen_srcs.py > src/art/Test988Intrinsics.java
#
 
import argparse
import os
import re
import collections
import sys
 
from string import Template
 
# Relative path to art/runtime/intrinsics_list.h
INTRINSICS_LIST_H = os.path.dirname(os.path.realpath(__file__)) + "/../../runtime/intrinsics_list.h"
 
# Macro parameter index to V(). Negative means from the end.
IDX_STATIC_OR_VIRTUAL = 1
IDX_SIGNATURE = -1
IDX_METHOD_NAME = -2
IDX_CLASS_NAME = -3
 
# Exclude all hidden API.
KLASS_BLACK_LIST = ['sun.misc.Unsafe', 'libcore.io.Memory', 'java.lang.StringFactory',
                    'java.lang.invoke.MethodHandle', # invokes are tested by 956-method-handles
                    'java.lang.invoke.VarHandle' ]  # TODO(b/65872996): will tested separately
METHOD_BLACK_LIST = [('java.lang.ref.Reference', 'getReferent'),
                     ('java.lang.String', 'getCharsNoCheck'),
                     ('java.lang.System', 'arraycopy')]  # arraycopy has a manual test.
 
# When testing a virtual function, it needs to operate on an instance.
# These instances will be created with the following values,
# otherwise a default 'new T()' is used.
KLASS_INSTANCE_INITIALIZERS = {
  'java.lang.String' : '"some large string"',
  'java.lang.StringBuffer' : 'new java.lang.StringBuffer("some large string buffer")',
  'java.lang.StringBuilder' : 'new java.lang.StringBuilder("some large string builder")',
  'java.lang.ref.Reference' : 'new java.lang.ref.WeakReference(new Object())'
};
 
OUTPUT_TPL = Template("""
/*
 * Copyright (C) 2017 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.
 */
 
// AUTO-GENENERATED by gen_srcs.py: DO NOT EDIT HERE DIRECTLY.
//
// $$> python3.4 gen_srcs.py > src/art/Test988Intrinsics.java
//
// RUN ABOVE COMMAND TO REGENERATE THIS FILE.
 
package art;
 
class Test988Intrinsics {
  // Pre-initialize *all* instance variables used so that their constructors are not in the trace.
$static_fields
 
  static void initialize() {
    // Ensure all static variables are initialized.
    // In addition, pre-load classes here so that we don't see diverging class loading traces.
$initialize_classes
  }
 
  static void test() {
    // Call each intrinsic from art/runtime/intrinsics_list.h to make sure they are traced.
$test_body
  }
}
""")
 
JNI_TYPES = {
  'Z' : 'boolean',
  'B' : 'byte',
  'C' : 'char',
  'S' : 'short',
  'I' : 'int',
  'J' : 'long',
  'F' : 'float',
  'D' : 'double',
  'L' : 'object'
};
 
debug_printing_enabled = False
 
def debug_print(x):
  if debug_printing_enabled:
    print(x, file=sys.stderr)
 
# Parse JNI sig into a list, e.g. "II" -> ['I', 'I'], '[[IJ' -> ['[[I', 'J'], etc.
def sig_to_parameter_type_list(sig):
  sig = re.sub(r'[(](.*)[)].*', r'\1', sig)
 
  lst = []
  obj = ""
  is_obj = False
  is_array = False
  for x in sig:
    if is_obj:
      obj = obj + x
      if x == ";":
        is_obj = False
        lst.append(obj)
        obj = ""
    elif is_array:
      obj = obj + x
      if x != "[":
        is_array = False
        lst.append(obj)
        obj = ""
    else:
      if x == "[":
        obj = "["
        is_array = True
      elif x == "L":
        obj = "L"
        is_obj = True
      else:
        lst.append(x)
 
  return lst
 
# Convert a single JNI descriptor into a pretty java name, e.g. "[I" -> "int[]", etc.
def javafy_name(kls_name):
  if kls_name.startswith("L"):
    kls_name = kls_name.lstrip("L").rstrip(";")
    return kls_name.replace("/", ".")
  elif kls_name.startswith("["):
    array_count = kls_name.count("[")
    non_array = javafy_name(kls_name.lstrip("["))
    return non_array + ("[]" * array_count)
 
  return JNI_TYPES.get(kls_name, kls_name)
 
def extract_staticness(static_or_virtual):
  if static_or_virtual == "kStatic":
    return 'static'
  return 'virtual' # kVirtual, kDirect
 
class MethodInfo:
  def __init__(self, staticness, pretty_params, method, kls):
    # 'virtual' or 'static'
    self.staticness = staticness
    # list of e.g. ['int', 'double', 'java.lang.String'] etc
    self.parameters = pretty_params
    # e.g. 'toString'
    self.method_name = method
    # e.g. 'java.lang.String'
    self.klass = kls
 
  def __str__(self):
    return "MethodInfo " + str(self.__dict__)
 
  def dummy_parameters(self):
    dummy_values = {
     'boolean' : 'false',
     'byte' : '(byte)0',
     'char' : "'x'",
     'short' : '(short)0',
     'int' : '0',
     'long' : '0L',
     'float' : '0.0f',
     'double' : '0.0'
    }
 
    def object_dummy(name):
      if name == "java.lang.String":
        return '"hello"'
      else:
        return "(%s)null" %(name)
    return [ dummy_values.get(param, object_dummy(param)) for param in self.parameters ]
 
  def dummy_instance_value(self):
    return KLASS_INSTANCE_INITIALIZERS.get(self.klass, 'new %s()' %(self.klass))
 
  def is_blacklisted(self):
    for blk in KLASS_BLACK_LIST:
      if self.klass.startswith(blk):
        return True
 
    return (self.klass, self.method_name) in METHOD_BLACK_LIST
 
# parse the V(...) \ list of items into a MethodInfo
def parse_method_info(items):
  def get_item(idx):
    return items[idx].strip().strip("\"")
 
  staticness = get_item(IDX_STATIC_OR_VIRTUAL)
  sig = get_item(IDX_SIGNATURE)
  method = get_item(IDX_METHOD_NAME)
  kls = get_item(IDX_CLASS_NAME)
 
  debug_print ((sig, method, kls))
 
  staticness = extract_staticness(staticness)
  kls = javafy_name(kls)
  param_types = sig_to_parameter_type_list(sig)
  pretty_params = param_types
  pretty_params = [javafy_name(i) for i in param_types]
 
  return MethodInfo(staticness, pretty_params, method, kls)
 
# parse a line containing '  V(...)' into a MethodInfo
def parse_line(line):
  line = line.strip()
  if not line.startswith("V("):
    return None
 
  line = re.sub(r'V[(](.*)[)]', r'\1', line)
  debug_print(line)
 
  items = line.split(",")
 
  method_info = parse_method_info(items)
  return method_info
 
# Generate all the MethodInfo that we parse from intrinsics_list.h
def parse_all_method_infos():
  with open(INTRINSICS_LIST_H) as f:
    for line in f:
      s = parse_line(line)
      if s is not None:
        yield s
 
# Format a receiver name. For statics, it's the class name, for receivers, it's an instance variable
def format_receiver_name(method_info):
  receiver = method_info.klass
  if method_info.staticness == 'virtual':
    receiver = "instance_" + method_info.klass.replace(".", "_")
  return receiver
 
# Format a dummy call with dummy method parameters to the requested method.
def format_call_to(method_info):
  dummy_args = ", ".join(method_info.dummy_parameters())
  receiver = format_receiver_name(method_info)
 
  return ("%s.%s(%s);" %(receiver, method_info.method_name, dummy_args))
 
# Format a static variable with an instance that could be used as the receiver
# (or None for non-static methods).
def format_instance_variable(method_info):
  if method_info.staticness == 'static':
    return None
  return "static %s %s = %s;" %(method_info.klass, format_receiver_name(method_info), method_info.dummy_instance_value())
 
def format_initialize_klass(method_info):
  return "%s.class.toString();" %(method_info.klass)
 
def indent_list(lst, indent):
  return [' ' * indent + i for i in lst]
 
def main():
  global debug_printing_enabled
  parser = argparse.ArgumentParser(description='Generate art/test/988-method-trace/src/art/Test988Intrinsics.java')
  parser.add_argument('-d', '--debug', action='store_true', dest='debug', help='Print extra debugging information to stderr.')
  parser.add_argument('output_file', nargs='?', metavar='<output-file>', default=sys.stdout, type=argparse.FileType('w'), help='Destination file to write to (default: stdout).')
  args = parser.parse_args()
 
  debug_printing_enabled = args.debug
 
  #####
 
  call_str_list = []
  instance_variable_dict = collections.OrderedDict()
  initialize_klass_dict = collections.OrderedDict()
  for i in parse_all_method_infos():
    debug_print(i)
    if i.is_blacklisted():
      debug_print("Blacklisted: " + str(i))
      continue
 
    call_str = format_call_to(i)
    debug_print(call_str)
 
    call_str_list.append(call_str)
 
    instance_variable = format_instance_variable(i)
    if instance_variable is not None:
      debug_print(instance_variable)
      instance_variable_dict[i.klass] = instance_variable
 
    initialize_klass_dict[i.klass] = format_initialize_klass(i)
 
  static_fields = indent_list([ value for (key, value) in instance_variable_dict.items() ], 2)
  test_body = indent_list(call_str_list, 4)
  initialize_classes = indent_list([ value for (key, value) in initialize_klass_dict.items() ], 4)
 
  print(OUTPUT_TPL.substitute(static_fields="\n".join(static_fields),
                              test_body="\n".join(test_body),
                              initialize_classes="\n".join(initialize_classes)).
                   strip("\n"), \
        file=args.output_file)
 
if __name__ == '__main__':
  main()