1 /* 2 * Copyright (C) 2007 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 package dalvik.system; 18 19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; 20 21 import android.annotation.SystemApi; 22 import android.compat.annotation.ChangeId; 23 import android.compat.annotation.EnabledSince; 24 import android.compat.annotation.Disabled; 25 import android.compat.annotation.UnsupportedAppUsage; 26 27 import dalvik.annotation.compat.VersionCodes; 28 import dalvik.annotation.optimization.FastNative; 29 30 import java.lang.ref.FinalizerReference; 31 import java.util.HashMap; 32 import java.util.Map; 33 import java.util.concurrent.atomic.AtomicInteger; 34 import java.util.function.Consumer; 35 36 /** 37 * Provides an interface to VM-global, Dalvik-specific features. 38 * An application cannot create its own Runtime instance, and must obtain 39 * one from the getRuntime method. 40 * 41 * @hide 42 */ 43 @SystemApi(client = MODULE_LIBRARIES) 44 @libcore.api.IntraCoreApi 45 public final class VMRuntime { 46 47 /** 48 * Holds the VMRuntime singleton. 49 */ 50 private static final VMRuntime THE_ONE = new VMRuntime(); 51 52 // Note: Instruction set names are used to construct the names of some 53 // system properties. To be sure that the properties stay valid the 54 // instruction set name should not exceed 7 characters. See installd 55 // and the package manager for the actual propeties. 56 private static final Map<String, String> ABI_TO_INSTRUCTION_SET_MAP 57 = new HashMap<String, String>(16); 58 static { 59 ABI_TO_INSTRUCTION_SET_MAP.put("armeabi", "arm"); 60 ABI_TO_INSTRUCTION_SET_MAP.put("armeabi-v7a", "arm"); 61 ABI_TO_INSTRUCTION_SET_MAP.put("x86", "x86"); 62 ABI_TO_INSTRUCTION_SET_MAP.put("x86_64", "x86_64"); 63 ABI_TO_INSTRUCTION_SET_MAP.put("arm64-v8a", "arm64"); 64 ABI_TO_INSTRUCTION_SET_MAP.put("arm64-v8a-hwasan", "arm64"); 65 ABI_TO_INSTRUCTION_SET_MAP.put("riscv64", "riscv64"); 66 } 67 68 /** 69 * Remove meta-reflection workaround for hidden api usage for apps targeting R+. This allowed 70 * apps to obtain references to blocklist fields and methods through an extra layer of 71 * reflection. 72 */ 73 @ChangeId 74 @EnabledSince(targetSdkVersion = VersionCodes.R) 75 private static final long 76 PREVENT_META_REFLECTION_BLOCKLIST_ACCESS = 142365358; // This is a bug id. 77 78 /** 79 * Gating access to greylist-max-p APIs. 80 */ 81 @ChangeId 82 @EnabledSince(targetSdkVersion = VersionCodes.Q) 83 private static final long HIDE_MAXTARGETSDK_P_HIDDEN_APIS = 149997251; // This is a bug id. 84 85 /** 86 * Gating access to greylist-max-q APIs. 87 */ 88 @ChangeId 89 @EnabledSince(targetSdkVersion = VersionCodes.R) 90 private static final long HIDE_MAXTARGETSDK_Q_HIDDEN_APIS = 149994052; // This is a bug id. 91 92 /** 93 * Allow apps accessing @TestApi APIs. 94 * 95 * <p>This will always be disabled by default and should only be used by platform test code. 96 */ 97 @ChangeId 98 @Disabled 99 private static final long ALLOW_TEST_API_ACCESS = 166236554; // This is a bug id. 100 101 /** 102 * Interface for logging hidden API usage events. 103 * 104 * @hide 105 */ 106 @SystemApi(client = MODULE_LIBRARIES) 107 public interface HiddenApiUsageLogger { 108 109 // The following ACCESS_METHOD_ constants must match the values in 110 // art/runtime/hidden_api.h 111 /** 112 * Internal test value that does not correspond to an actual access by the 113 * application. Never logged, added for completeness. 114 * 115 * @hide 116 */ 117 @SystemApi(client = MODULE_LIBRARIES) 118 public static final int ACCESS_METHOD_NONE = 0; 119 120 /** 121 * Used when a method has been accessed via reflection. 122 * 123 * @hide 124 */ 125 @SystemApi(client = MODULE_LIBRARIES) 126 public static final int ACCESS_METHOD_REFLECTION = 1; 127 128 /** 129 * Used when a method has been accessed via JNI. 130 * 131 * @hide 132 */ 133 @SystemApi(client = MODULE_LIBRARIES) 134 public static final int ACCESS_METHOD_JNI = 2; 135 136 /** 137 * Used when a method is accessed at link time. Never logged, added only 138 * for completeness. 139 * 140 * @hide 141 */ 142 @SystemApi(client = MODULE_LIBRARIES) 143 public static final int ACCESS_METHOD_LINKING = 3; 144 145 /** 146 * Logs hidden API access 147 * 148 * @param sampledValue value that was sampled, to be compared against the 149 * sampling rate 150 * @param appPackageName package name of the app attempting the access 151 * @param signature signature of the method being called, i.e 152 * class_name->member_name:type_signature (e.g. 153 * {@code com.android.app.Activity->mDoReportFullyDrawn:Z}) for fields and 154 * class_name->method_name_and_signature for methods (e.g 155 * {@code com.android.app.Activity->finish(I)V}) 156 * @param accessType how the accessed was done 157 * @param accessDenied whether the access was allowed or not 158 * 159 * @hide 160 */ 161 @SystemApi(client = MODULE_LIBRARIES) hiddenApiUsed(int sampledValue, String appPackageName, String signature, int accessType, boolean accessDenied)162 public void hiddenApiUsed(int sampledValue, String appPackageName, 163 String signature, int accessType, boolean accessDenied); 164 } 165 166 static HiddenApiUsageLogger hiddenApiUsageLogger; 167 168 /** 169 * Sets the hidden API usage logger {@link #hiddenApiUsageLogger}. 170 * It should only be called if {@link #setHiddenApiAccessLogSamplingRate(int)} 171 * is called with a value > 0 172 * 173 * @param hiddenApiUsageLogger an object implement {@code HiddenApiUsageLogger} that the runtime 174 * will call for logging hidden API checks. 175 * 176 * @hide 177 */ 178 @SystemApi(client = MODULE_LIBRARIES) setHiddenApiUsageLogger(HiddenApiUsageLogger hiddenApiUsageLogger)179 public static void setHiddenApiUsageLogger(HiddenApiUsageLogger hiddenApiUsageLogger) { 180 VMRuntime.hiddenApiUsageLogger = hiddenApiUsageLogger; 181 } 182 183 /** 184 * Records an attempted hidden API access to 185 * {@link HiddenApiUsageLogger#hiddenApiUsed(int, String, String, int, boolean} 186 * if a logger is registered via {@link #setHiddenApiUsageLogger}. 187 */ hiddenApiUsed(int sampledValue, String appPackageName, String signature, int accessType, boolean accessDenied)188 private static void hiddenApiUsed(int sampledValue, String appPackageName, String signature, 189 int accessType, boolean accessDenied) { 190 if (VMRuntime.hiddenApiUsageLogger != null) { 191 VMRuntime.hiddenApiUsageLogger.hiddenApiUsed(sampledValue, appPackageName, 192 signature, accessType, accessDenied); 193 } 194 } 195 196 /** 197 * Magic version number for a current development build, which has not 198 * yet turned into an official release. This number must be larger than 199 * any released version in {@code android.os.Build.VERSION_CODES}. 200 * @hide 201 */ 202 @SystemApi(client = MODULE_LIBRARIES) 203 // Must match android.os.Build.VERSION_CODES.CUR_DEVELOPMENT. 204 public static final int SDK_VERSION_CUR_DEVELOPMENT = 10000; 205 206 private static Consumer<String> nonSdkApiUsageConsumer = null; 207 208 private int targetSdkVersion = SDK_VERSION_CUR_DEVELOPMENT; 209 210 // notifyNativeAllocationsInternal (below) should be called every notifyNativeInterval 211 // allocations. Initialized on demand to allow completely static class initialization. 212 private int notifyNativeInterval; 213 214 // Allocations since last call to native layer. See notifyNativeAllocation(). 215 private final AtomicInteger allocationCount = new AtomicInteger(0); 216 217 private long[] disabledCompatChanges = new long[0]; 218 219 /** 220 * Prevents this class from being instantiated. 221 */ VMRuntime()222 private VMRuntime() { 223 } 224 225 /** 226 * Returns the object that represents the current runtime. 227 * @return the runtime object 228 * 229 * @hide 230 */ 231 @UnsupportedAppUsage 232 @SystemApi(client = MODULE_LIBRARIES) 233 @libcore.api.IntraCoreApi getRuntime()234 public static VMRuntime getRuntime() { 235 return THE_ONE; 236 } 237 238 /** 239 * Returns a copy of the VM's command-line property settings. 240 * These are in the form "name=value" rather than "-Dname=value". 241 * 242 * @hide 243 */ properties()244 public native String[] properties(); 245 246 /** 247 * Returns the VM's boot class path. 248 * 249 * @hide 250 */ bootClassPath()251 public native String bootClassPath(); 252 253 /** 254 * Returns the VM's class path. 255 * 256 * @hide 257 */ classPath()258 public native String classPath(); 259 260 /** 261 * Returns the VM's version. 262 * 263 * @hide 264 */ vmVersion()265 public native String vmVersion(); 266 267 /** 268 * Returns the name of the shared library providing the VM implementation. 269 * 270 * @return the name of the shared library providing the VM implementation. 271 * 272 * @hide 273 */ 274 @UnsupportedAppUsage 275 @SystemApi(client = MODULE_LIBRARIES) vmLibrary()276 public native String vmLibrary(); 277 278 /** 279 * Returns the VM's instruction set. 280 * 281 * @return the VM's instruction set. 282 * 283 * @hide 284 */ 285 @UnsupportedAppUsage 286 @SystemApi(client = MODULE_LIBRARIES) vmInstructionSet()287 public native String vmInstructionSet(); 288 289 /** 290 * Returns whether the VM is running in 64-bit mode. 291 * 292 * @return true if VM is running in 64-bit mode, false otherwise. 293 * 294 * @hide 295 */ 296 @UnsupportedAppUsage 297 @SystemApi(client = MODULE_LIBRARIES) 298 @FastNative is64Bit()299 public native boolean is64Bit(); 300 301 /** 302 * Returns whether the VM is running with JNI checking enabled. 303 * 304 * @return true if the VM is running with JNI checking enabled, 305 * and false otherwise. 306 * 307 * @hide 308 */ 309 @SystemApi(client = MODULE_LIBRARIES) 310 @FastNative isCheckJniEnabled()311 public native boolean isCheckJniEnabled(); 312 313 /** 314 * Gets the current ideal heap utilization, represented as a number 315 * between zero and one. After a GC happens, the Dalvik heap may 316 * be resized so that (size of live objects) / (size of heap) is 317 * equal to this number. 318 * 319 * @return the current ideal heap utilization 320 * 321 * @hide 322 */ getTargetHeapUtilization()323 public native float getTargetHeapUtilization(); 324 325 /** 326 * Retrieves the finalizer timeout in milliseconds. 327 * Finalizers that fail to terminate in this amount of time cause the 328 * runtime to abort. 329 * 330 * @hide 331 */ getFinalizerTimeoutMs()332 public native long getFinalizerTimeoutMs(); 333 334 /** 335 * Sets the current ideal heap utilization, represented as a number 336 * between zero and one. After a GC happens, the Dalvik heap may 337 * be resized so that (size of live objects) / (size of heap) is 338 * equal to this number. 339 * 340 * <p>This is only a hint to the garbage collector and may be ignored. 341 * 342 * @param newTarget the new suggested ideal heap utilization. 343 * This value may be adjusted internally. 344 * @return the previous ideal heap utilization 345 * @throws IllegalArgumentException if newTarget is <= 0.0 or >= 1.0 346 * 347 * @hide 348 */ 349 @UnsupportedAppUsage setTargetHeapUtilization(float newTarget)350 public float setTargetHeapUtilization(float newTarget) { 351 if (newTarget <= 0.0f || newTarget >= 1.0f) { 352 throw new IllegalArgumentException(newTarget + " out of range (0,1)"); 353 } 354 /* The native code assumes a value >= 0.1. Clamp it to that. */ 355 if (newTarget < 0.1f) { 356 newTarget = 0.1f; 357 } 358 /* Synchronize to make sure that only one thread gets a given "old" value if both 359 * update at the same time. Allows for reliable save-and-restore semantics. 360 */ 361 synchronized (this) { 362 float oldTarget = getTargetHeapUtilization(); 363 nativeSetTargetHeapUtilization(newTarget); 364 return oldTarget; 365 } 366 } 367 368 /** 369 * Sets the target SDK version. Should only be called before the 370 * app starts to run, because it may change the VM's behavior in 371 * dangerous ways. Defaults to {@link #SDK_VERSION_CUR_DEVELOPMENT}. 372 * 373 * @param targetSdkVersion the SDK version the app wants to run with. 374 * 375 * @hide 376 */ 377 @UnsupportedAppUsage(maxTargetSdk=0, publicAlternatives="Use the {@code targetSdkVersion}" 378 +" attribute in the {@code uses-sdk} manifest tag instead.") 379 @SystemApi(client = MODULE_LIBRARIES) setTargetSdkVersion(int targetSdkVersion)380 public synchronized void setTargetSdkVersion(int targetSdkVersion) { 381 this.targetSdkVersion = targetSdkVersion; 382 setTargetSdkVersionNative(this.targetSdkVersion); 383 } 384 385 386 /** 387 * Sets the disabled compat changes. Should only be called before the 388 * app starts to run, because it may change the VM's behavior in 389 * dangerous ways. Defaults to empty. 390 * 391 * @param disabledCompatChanges An array of ChangeIds that we want to disable. 392 * 393 * @hide 394 */ 395 @SystemApi(client = MODULE_LIBRARIES) setDisabledCompatChanges(long[] disabledCompatChanges)396 public synchronized void setDisabledCompatChanges(long[] disabledCompatChanges) { 397 this.disabledCompatChanges = disabledCompatChanges; 398 setDisabledCompatChangesNative(this.disabledCompatChanges); 399 } 400 401 @FastNative getSdkVersionNative(int default_sdk_value)402 private static native int getSdkVersionNative(int default_sdk_value); 403 404 /** 405 * A container to avoid initialized by the unstarted runtime. 406 * 407 * {@link #sdkVersion} needs a separate container because {@link VMRuntime} could be initialized 408 * in the unstarted runtime where the values of the system properties could be misleading. 409 */ 410 private static class SdkVersionContainer { 411 // Similar to android.os.Build.VERSION.SDK_INT in the boot classpath, the default sdk is 0. 412 private static final int sdkVersion = getSdkVersionNative(/*default_sdk_value=*/0); 413 } 414 415 /** 416 * Gets the SDK version of the software currently running on this hardware 417 * device. This value never changes while a device is booted, but it may 418 * increase when the hardware manufacturer provides an OTA update. 419 * <p> 420 * Possible values are defined in {@link VersionCodes}. 421 * 422 * It's expected to use by the ART module. Please use android.os.Build.VERSION.SDK_INT if 423 * the usage is not in the ART module. 424 * 425 * @implNote This returns {@code "ro.build.version.sdk"} system property on Android 426 * 427 * @hide 428 */ getSdkVersion()429 public static int getSdkVersion() { 430 return SdkVersionContainer.sdkVersion; 431 } 432 433 /** 434 * Gets the target SDK version. See {@link #setTargetSdkVersion} for 435 * special values. 436 * 437 * @return the target SDK version. 438 * 439 * @hide 440 */ 441 @SystemApi(client = MODULE_LIBRARIES) getTargetSdkVersion()442 public synchronized int getTargetSdkVersion() { 443 return targetSdkVersion; 444 } 445 setTargetSdkVersionNative(int targetSdkVersion)446 private native void setTargetSdkVersionNative(int targetSdkVersion); setDisabledCompatChangesNative(long[] disabledCompatChanges)447 private native void setDisabledCompatChangesNative(long[] disabledCompatChanges); 448 449 /** 450 * This method exists for binary compatibility. It was part of a 451 * heap sizing API which was removed in Android 3.0 (Honeycomb). 452 * 453 * @hide 454 */ 455 @UnsupportedAppUsage 456 @Deprecated getMinimumHeapSize()457 public long getMinimumHeapSize() { 458 return 0; 459 } 460 461 /** 462 * This method exists for binary compatibility. It was part of a 463 * heap sizing API which was removed in Android 3.0 (Honeycomb). 464 * 465 * @hide 466 */ 467 @UnsupportedAppUsage 468 @Deprecated setMinimumHeapSize(long size)469 public long setMinimumHeapSize(long size) { 470 return 0; 471 } 472 473 /** 474 * This method exists for binary compatibility. It used to 475 * perform a garbage collection that cleared SoftReferences. 476 * 477 * @hide 478 */ 479 @UnsupportedAppUsage 480 @Deprecated gcSoftReferences()481 public void gcSoftReferences() {} 482 483 /** 484 * This method exists for binary compatibility. It is equivalent 485 * to {@link System#runFinalization}. 486 * 487 * @hide 488 */ 489 @UnsupportedAppUsage 490 @Deprecated runFinalizationSync()491 public void runFinalizationSync() { 492 System.runFinalization(); 493 } 494 495 /** 496 * Implements setTargetHeapUtilization(). 497 * 498 * @param newTarget the new suggested ideal heap utilization. 499 * This value may be adjusted internally. 500 */ nativeSetTargetHeapUtilization(float newTarget)501 private native void nativeSetTargetHeapUtilization(float newTarget); 502 503 /** 504 * This method exists for binary compatibility. It was part of 505 * the external allocation API which was removed in Android 3.0 (Honeycomb). 506 * 507 * @hide 508 */ 509 @UnsupportedAppUsage 510 @Deprecated trackExternalAllocation(long size)511 public boolean trackExternalAllocation(long size) { 512 return true; 513 } 514 515 /** 516 * This method exists for binary compatibility. It was part of 517 * the external allocation API which was removed in Android 3.0 (Honeycomb). 518 * 519 * @hide 520 */ 521 @UnsupportedAppUsage 522 @Deprecated trackExternalFree(long size)523 public void trackExternalFree(long size) {} 524 525 /** 526 * This method exists for binary compatibility. It was part of 527 * the external allocation API which was removed in Android 3.0 (Honeycomb). 528 * 529 * @hide 530 */ 531 @UnsupportedAppUsage 532 @Deprecated getExternalBytesAllocated()533 public long getExternalBytesAllocated() { 534 return 0; 535 } 536 537 /** 538 * Sets the list of exemptions from hidden API access enforcement. 539 * 540 * @param signaturePrefixes 541 * A list of signature prefixes. Each item in the list is a prefix match on the type 542 * signature of a blacklisted API. All matching APIs are treated as if they were on 543 * the whitelist: access permitted, and no logging.. 544 * 545 * @hide 546 */ 547 @SystemApi(client = MODULE_LIBRARIES) setHiddenApiExemptions(String[] signaturePrefixes)548 public native void setHiddenApiExemptions(String[] signaturePrefixes); 549 550 /** 551 * Sets the log sampling rate of hidden API accesses written to the event log. 552 * 553 * @param rate Proportion of hidden API accesses that will be logged; an integer between 554 * 0 and 0x10000 inclusive. 555 * 556 * @hide 557 */ 558 @SystemApi(client = MODULE_LIBRARIES) setHiddenApiAccessLogSamplingRate(int rate)559 public native void setHiddenApiAccessLogSamplingRate(int rate); 560 561 /** 562 * Returns an array allocated in an area of the Java heap where it will never be moved. 563 * This is used to implement native allocations on the Java heap, such as DirectByteBuffers 564 * and Bitmaps. 565 * 566 * @param componentType the component type of the returned array. 567 * @param length the length of the returned array. 568 * @return array allocated in an area of the heap where it will never be moved. 569 * 570 * @hide 571 */ 572 @UnsupportedAppUsage 573 @SystemApi(client = MODULE_LIBRARIES) 574 @libcore.api.IntraCoreApi 575 @FastNative newNonMovableArray(Class<?> componentType, int length)576 public native Object newNonMovableArray(Class<?> componentType, int length); 577 578 /** 579 * Returns an array of at least {@code minLength}, but potentially larger. The increased size 580 * comes from avoiding any padding after the array. The amount of padding varies depending on 581 * the componentType and the memory allocator implementation. 582 * 583 * @param componentType the component type of the returned array. 584 * @param minLength the minimum length of the returned array. The actual length could 585 * be greater. 586 * @return array of at least of {@code minLength} 587 * 588 * @hide 589 */ 590 @SystemApi(client = MODULE_LIBRARIES) 591 @FastNative newUnpaddedArray(Class<?> componentType, int minLength)592 public native Object newUnpaddedArray(Class<?> componentType, int minLength); 593 594 /** 595 * Returns the address of {@code array[0]}. This differs from using JNI in that JNI 596 * might lie and give you the address of a copy of the array when in forcecopy mode. 597 * 598 * @param array the object we want the native address of. Must be a non-movable 599 * primitive array. 600 * @return native address of {@code array[0]}. 601 * 602 * @hide 603 */ 604 @UnsupportedAppUsage 605 @SystemApi(client = MODULE_LIBRARIES) 606 @libcore.api.IntraCoreApi 607 @FastNative addressOf(Object array)608 public native long addressOf(Object array); 609 610 /** 611 * Removes any growth limits, allowing the application to allocate 612 * up to the maximum heap size. 613 * 614 * @hide 615 */ 616 @UnsupportedAppUsage 617 @SystemApi(client = MODULE_LIBRARIES) clearGrowthLimit()618 public native void clearGrowthLimit(); 619 620 /** 621 * Make the current growth limit the new non growth limit capacity by releasing pages which 622 * are after the growth limit but before the non growth limit capacity. 623 * 624 * @hide 625 */ 626 @SystemApi(client = MODULE_LIBRARIES) clampGrowthLimit()627 public native void clampGrowthLimit(); 628 629 /** 630 * Returns true if native debugging is on. 631 * 632 * @return true if native debugging is on, false otherwise. 633 * 634 * @hide 635 */ 636 @SystemApi(client = MODULE_LIBRARIES) 637 @FastNative isNativeDebuggable()638 public native boolean isNativeDebuggable(); 639 640 /** 641 * Returns true if Java debugging is enabled. 642 * 643 * @hide 644 */ isJavaDebuggable()645 public native boolean isJavaDebuggable(); 646 647 /** 648 * Registers a native allocation so that the heap knows about it and performs GC as required. 649 * If the number of native allocated bytes exceeds the native allocation watermark, the 650 * function requests a concurrent GC. If the native bytes allocated exceeds a second higher 651 * watermark, it is determined that the application is registering native allocations at an 652 * unusually high rate and a GC is performed inside of the function to prevent memory usage 653 * from excessively increasing. Memory allocated via system malloc() should not be included 654 * in this count. The argument must be the same as that later passed to registerNativeFree(), 655 * but may otherwise be approximate. 656 * 657 * @param bytes the number of bytes of the native object. 658 * 659 * @hide 660 */ 661 @UnsupportedAppUsage 662 @SystemApi(client = MODULE_LIBRARIES) registerNativeAllocation(long bytes)663 public native void registerNativeAllocation(long bytes); 664 665 /** 666 * Backward compatibility version of {@link #registerNativeAllocation(long)}. We used to pass 667 * an int instead of a long. The RenderScript support library looks it up via reflection. 668 * @deprecated Use {@link #registerNativeAllocation(long)} instead. 669 * 670 * @param bytes the number of bytes of the native object. 671 * 672 * @hide 673 */ 674 @UnsupportedAppUsage 675 @Deprecated 676 @SystemApi(client = MODULE_LIBRARIES) registerNativeAllocation(int bytes)677 public void registerNativeAllocation(int bytes) { 678 registerNativeAllocation((long) bytes); 679 } 680 681 /** 682 * Registers a native free by reducing the number of native bytes accounted for. 683 * 684 * @param bytes the number of bytes of the freed object. 685 * 686 * @hide 687 */ 688 @UnsupportedAppUsage 689 @SystemApi(client = MODULE_LIBRARIES) registerNativeFree(long bytes)690 public native void registerNativeFree(long bytes); 691 692 /** 693 * Backward compatibility version of {@link #registerNativeFree(long)}. 694 * @deprecated Use {@link #registerNativeFree(long)} instead. 695 * 696 * @param bytes the number of bytes of the freed object. 697 * 698 * @hide 699 */ 700 @UnsupportedAppUsage 701 @Deprecated 702 @SystemApi(client = MODULE_LIBRARIES) registerNativeFree(int bytes)703 public void registerNativeFree(int bytes) { 704 registerNativeFree((long) bytes); 705 } 706 707 /** 708 * Return the number of native objects that are reported by a single call to 709 * notifyNativeAllocation(). 710 */ getNotifyNativeInterval()711 private static native int getNotifyNativeInterval(); 712 713 /** 714 * Report a native malloc()-only allocation to the GC. 715 * 716 * @hide 717 */ notifyNativeAllocation()718 public void notifyNativeAllocation() { 719 // Minimize JNI calls by notifying once every notifyNativeInterval allocations. 720 // The native code cannot do anything without calling mallinfo(), which is too 721 // expensive to perform on every allocation. To avoid the JNI overhead on every 722 // allocation, we do the sampling here, rather than in native code. 723 // Initialize notifyNativeInterval carefully. Multiple initializations may race. 724 int myNotifyNativeInterval = notifyNativeInterval; 725 if (myNotifyNativeInterval == 0) { 726 // This can race. By Java rules, that's OK. 727 myNotifyNativeInterval = notifyNativeInterval = getNotifyNativeInterval(); 728 } 729 // myNotifyNativeInterval is correct here. If another thread won the initial race, 730 // notifyNativeInterval may not be. 731 if (allocationCount.addAndGet(1) % myNotifyNativeInterval == 0) { 732 notifyNativeAllocationsInternal(); 733 } 734 } 735 736 /** 737 * Report to the GC that roughly notifyNativeInterval native malloc()-based 738 * allocations have occurred since the last call to notifyNativeAllocationsInternal(). 739 * Hints that we should check whether a GC is required. 740 * 741 * @hide 742 */ notifyNativeAllocationsInternal()743 public native void notifyNativeAllocationsInternal(); 744 745 /** 746 * Wait for objects to be finalized. 747 * 748 * If finalization takes longer than timeout, then the function returns before all objects are 749 * finalized. 750 * 751 * @param timeout 752 * timeout in nanoseconds of the maximum time to wait until all pending finalizers 753 * are run. If timeout is 0, then there is no timeout. Note that the timeout does 754 * not stop the finalization process, it merely stops the wait. 755 * 756 * @see #Runtime.runFinalization() 757 * @see #wait(long,int) 758 * 759 * @hide 760 */ 761 @UnsupportedAppUsage runFinalization(long timeout)762 public static void runFinalization(long timeout) { 763 try { 764 FinalizerReference.finalizeAllEnqueued(timeout); 765 } catch (InterruptedException e) { 766 // Interrupt the current thread without actually throwing the InterruptionException 767 // for the caller. 768 Thread.currentThread().interrupt(); 769 } 770 } 771 772 /** 773 * Request that a garbage collection gets started on a different thread. 774 * 775 * @hide 776 */ 777 @SystemApi(client = MODULE_LIBRARIES) requestConcurrentGC()778 public native void requestConcurrentGC(); 779 780 /** 781 * 782 * @hide 783 */ requestHeapTrim()784 public native void requestHeapTrim(); 785 786 /** 787 * 788 * @hide 789 */ trimHeap()790 public native void trimHeap(); 791 792 /** 793 * 794 * @hide 795 */ startHeapTaskProcessor()796 public native void startHeapTaskProcessor(); 797 798 /** 799 * 800 * @hide 801 */ stopHeapTaskProcessor()802 public native void stopHeapTaskProcessor(); 803 804 /** 805 * 806 * @hide 807 */ runHeapTasks()808 public native void runHeapTasks(); 809 810 /** 811 * Let the heap know of the new "jank perceptibility" process state. This can change allocation 812 * and garbage collection behavior regarding trimming and compaction. Should be called when it 813 * appears likely that process response time will remain invisible to the user for an extended 814 * period, and then again immediately after slow process response becomes user-visible again. 815 * 816 * @param state The state of the process, as defined in art/runtime/process_state.h. 817 * 818 * @hide 819 */ 820 @SystemApi(client = MODULE_LIBRARIES) updateProcessState(int state)821 public native void updateProcessState(int state); 822 823 /** 824 * Let the runtime know that the application startup is completed. This may affect behavior 825 * related to profiling and startup caches. 826 * 827 * @hide 828 */ 829 @SystemApi(client = MODULE_LIBRARIES) notifyStartupCompleted()830 public native void notifyStartupCompleted(); 831 832 /** 833 * Fill in dex caches with classes, fields, and methods that are 834 * already loaded. Typically used after Zygote preloading. 835 * 836 * @hide 837 */ 838 @SystemApi(client = MODULE_LIBRARIES) preloadDexCaches()839 public native void preloadDexCaches(); 840 841 /** 842 * Flag denoting that the code paths passed to 843 * {@link #registerAppInfo(String, String, String, String[], int, boolean)} 844 * contains the app primary APK. 845 * 846 * @hide 847 */ 848 @SystemApi(client = MODULE_LIBRARIES) 849 public static final int CODE_PATH_TYPE_PRIMARY_APK = 1 << 0; 850 /** 851 * Flag denoting that the code paths passed to 852 * {@link #registerAppInfo(String, String, String, String[], int, boolean)} 853 * contains the a split APK. 854 * 855 * @hide 856 */ 857 @SystemApi(client = MODULE_LIBRARIES) 858 public static final int CODE_PATH_TYPE_SPLIT_APK = 1 << 1; 859 /** 860 * Flag denoting that the code paths passed to 861 * {@link #registerAppInfo(String, String, String, String[], int, boolean)} 862 * contains a secondary dex file (dynamically loaded by the app). 863 * 864 * @hide 865 */ 866 @SystemApi(client = MODULE_LIBRARIES) 867 public static final int CODE_PATH_TYPE_SECONDARY_DEX = 1 << 2; 868 869 /** 870 * Register application info to ART. 871 * This enables ART to support certain low level features (such as profiling) and provide 872 * better debug information. The method should be called after the application loads its 873 * apks or dex files. 874 * 875 * @param packageName the name of the package being ran. 876 * @param currentProfileFile the path of the file where the profile information for the current 877 * execution should be stored. 878 * @param referenceProfileFile the path of the file where the reference profile information 879 * (for past executions) is stored. 880 * @param appCodePaths the code paths (apk/dex files) of the applications that were loaded. 881 * These paths will also be profiled. 882 * @param codePathsTypes the type of the code paths. 883 * 884 * @hide 885 */ 886 @SystemApi(client = MODULE_LIBRARIES) registerAppInfo( String packageName, String currentProfileFile, String referenceProfileFile, String[] appCodePaths, int codePathsType)887 public static native void registerAppInfo( 888 String packageName, 889 String currentProfileFile, 890 String referenceProfileFile, 891 String[] appCodePaths, 892 int codePathsType); 893 894 /** 895 * Returns the runtime instruction set corresponding to a given ABI. Multiple 896 * compatible ABIs might map to the same instruction set. For example 897 * {@code armeabi-v7a} and {@code armeabi} might map to the instruction set {@code arm}. 898 * 899 * This influences the compilation of the applications classes. 900 * 901 * @param abi The ABI we want the instruction set from. 902 * 903 * @hide 904 */ 905 @UnsupportedAppUsage 906 @SystemApi(client = MODULE_LIBRARIES) getInstructionSet(String abi)907 public static String getInstructionSet(String abi) { 908 final String instructionSet = ABI_TO_INSTRUCTION_SET_MAP.get(abi); 909 if (instructionSet == null) { 910 throw new IllegalArgumentException("Unsupported ABI: " + abi); 911 } 912 913 return instructionSet; 914 } 915 916 /** 917 * Returns whether the given {@code instructionSet} is 64 bits. 918 * 919 * @param instructionSet a string representing an instruction set. 920 * 921 * @return true if given {@code instructionSet} is 64 bits, false otherwise. 922 * 923 * @hide 924 */ 925 @SystemApi(client = MODULE_LIBRARIES) is64BitInstructionSet(String instructionSet)926 public static boolean is64BitInstructionSet(String instructionSet) { 927 return (instructionSet != null) && instructionSet.contains("64"); 928 } 929 930 /** 931 * Returns whether the given {@code abi} is 64 bits. 932 * 933 * @param abi a string representing an ABI. 934 * 935 * @return true if given {@code abi} is 64 bits, false otherwise. 936 * 937 * @hide 938 */ 939 @UnsupportedAppUsage 940 @SystemApi(client = MODULE_LIBRARIES) is64BitAbi(String abi)941 public static boolean is64BitAbi(String abi) { 942 return is64BitInstructionSet(getInstructionSet(abi)); 943 } 944 945 /** 946 * Return false if the boot class path for the given instruction 947 * set mapped from disk storage, versus being interpretted from 948 * dirty pages in memory. 949 * 950 * @hide 951 */ isBootClassPathOnDisk(String instructionSet)952 public static native boolean isBootClassPathOnDisk(String instructionSet); 953 954 /** 955 * Used to notify the runtime that boot completed. 956 * 957 * @hide 958 */ 959 @SystemApi(client = MODULE_LIBRARIES) bootCompleted()960 public static native void bootCompleted(); 961 962 /** 963 * Used to notify the runtime to reset Jit counters. This is done for the boot image 964 * profiling configuration to avoid samples during class preloading. This helps avoid 965 * the regression from disabling class profiling. 966 * 967 * @hide 968 */ 969 @SystemApi(client = MODULE_LIBRARIES) resetJitCounters()970 public static native void resetJitCounters(); 971 972 /** 973 * Returns the instruction set of the current runtime. 974 * 975 * @return instruction set of the current runtime. 976 * 977 * @hide 978 */ 979 @UnsupportedAppUsage 980 @SystemApi(client = MODULE_LIBRARIES) getCurrentInstructionSet()981 public static native String getCurrentInstructionSet(); 982 983 /** 984 * Register the current execution thread to the runtime as sensitive thread. 985 * Should be called just once. Subsequent calls are ignored. 986 * 987 * @hide 988 */ 989 @SystemApi(client = MODULE_LIBRARIES) registerSensitiveThread()990 public static native void registerSensitiveThread(); 991 992 /** 993 * Sets up the priority of the system daemon thread (caller). 994 * 995 * @hide 996 */ setSystemDaemonThreadPriority()997 public static native void setSystemDaemonThreadPriority(); 998 999 /** 1000 * Sets a callback that the runtime can call whenever a usage of a non SDK API is detected. 1001 * 1002 * @param consumer an object implementing the {@code java.util.function.Consumer} interface that 1003 * the runtime will call whenever a usage of a non SDK API is detected. 1004 * 1005 * @hide 1006 */ 1007 @SystemApi(client = MODULE_LIBRARIES) setNonSdkApiUsageConsumer(Consumer<String> consumer)1008 public static void setNonSdkApiUsageConsumer(Consumer<String> consumer) { 1009 nonSdkApiUsageConsumer = consumer; 1010 } 1011 1012 /** 1013 * Sets whether or not the runtime should dedupe detection and warnings for hidden API usage. 1014 * 1015 * @param dedupe if set, only the first usage of each API will be detected. The default 1016 * behaviour is to dedupe. 1017 * 1018 * @hide 1019 */ 1020 @SystemApi(client = MODULE_LIBRARIES) setDedupeHiddenApiWarnings(boolean dedupe)1021 public static native void setDedupeHiddenApiWarnings(boolean dedupe); 1022 1023 /** 1024 * Sets the package name of the app running in this process. 1025 * 1026 * @param packageName the value being set 1027 * 1028 * @hide 1029 */ 1030 @SystemApi(client = MODULE_LIBRARIES) setProcessPackageName(String packageName)1031 public static native void setProcessPackageName(String packageName); 1032 1033 /** 1034 * Sets the full path to data directory of the app running in this process. 1035 * 1036 * @param dataDir the value being set 1037 * 1038 * @hide 1039 */ 1040 @SystemApi(client = MODULE_LIBRARIES) setProcessDataDirectory(String dataDir)1041 public static native void setProcessDataDirectory(String dataDir); 1042 1043 /** 1044 * Returns whether {@code encodedClassLoaderContext} is a valid encoded class loader context. 1045 * A class loader context is an internal opaque format used by the runtime to encode the 1046 * class loader hierarchy (including each ClassLoader's classpath) used to load a dex file. 1047 * 1048 * @param encodedClassLoaderContext the class loader context to analyze 1049 * @throws NullPointerException if {@code encodedClassLoaderContext is null. 1050 * @return {@code true} if {@code encodedClassLoaderContext} is a non-null valid encoded class 1051 * loader context. 1052 * 1053 * @hide 1054 */ 1055 @SystemApi(client = MODULE_LIBRARIES) isValidClassLoaderContext(String encodedClassLoaderContext)1056 public static native boolean isValidClassLoaderContext(String encodedClassLoaderContext); 1057 1058 /** 1059 * Returns the optimization status of the base APK loaded in this process. If called in a 1060 * process without an APK, returns 1061 * 1062 * @hide 1063 */ getBaseApkOptimizationInfo()1064 public static native DexFile.OptimizationInfo getBaseApkOptimizationInfo(); 1065 } 1066