-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathti_method.cc
1294 lines (1176 loc) · 47.2 KB
/
ti_method.cc
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2016 The Android Open Source Project
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This file implements interfaces from the file jvmti.h. This implementation
* is licensed under the same terms as the file jvmti.h. The
* copyright and license information for the file jvmti.h follows.
*
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "ti_method.h"
#include <initializer_list>
#include <type_traits>
#include <variant>
#include "android-base/macros.h"
#include "arch/context.h"
#include "art_jvmti.h"
#include "art_method-inl.h"
#include "base/enums.h"
#include "base/globals.h"
#include "base/macros.h"
#include "base/mutex-inl.h"
#include "deopt_manager.h"
#include "dex/code_item_accessors-inl.h"
#include "dex/code_item_accessors.h"
#include "dex/dex_file_annotations.h"
#include "dex/dex_file_types.h"
#include "dex/dex_instruction.h"
#include "dex/dex_instruction_iterator.h"
#include "dex/modifiers.h"
#include "dex/primitive.h"
#include "events-inl.h"
#include "gc_root-inl.h"
#include "handle.h"
#include "jit/jit.h"
#include "jni/jni_internal.h"
#include "jvmti.h"
#include "mirror/class-inl.h"
#include "mirror/class_loader.h"
#include "mirror/object-inl.h"
#include "mirror/object_array-inl.h"
#include "nativehelper/scoped_local_ref.h"
#include "oat_file.h"
#include "obj_ptr.h"
#include "runtime.h"
#include "runtime_callbacks.h"
#include "scoped_thread_state_change-inl.h"
#include "scoped_thread_state_change.h"
#include "stack.h"
#include "thread-current-inl.h"
#include "thread.h"
#include "thread_list.h"
#include "ti_logging.h"
#include "ti_stack.h"
#include "ti_thread.h"
#include "ti_phase.h"
#include "verifier/register_line-inl.h"
#include "verifier/reg_type-inl.h"
#include "verifier/method_verifier-inl.h"
namespace openjdkjvmti {
struct TiMethodCallback : public art::MethodCallback {
void RegisterNativeMethod(art::ArtMethod* method,
const void* cur_method,
/*out*/void** new_method)
override REQUIRES_SHARED(art::Locks::mutator_lock_) {
if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kNativeMethodBind)) {
art::Thread* thread = art::Thread::Current();
art::JNIEnvExt* jnienv = thread->GetJniEnv();
ScopedLocalRef<jthread> thread_jni(
jnienv, PhaseUtil::IsLivePhase() ? jnienv->AddLocalReference<jthread>(thread->GetPeer())
: nullptr);
jmethodID method_id = art::jni::EncodeArtMethod(method);
art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
event_handler->DispatchEvent<ArtJvmtiEvent::kNativeMethodBind>(
thread,
static_cast<JNIEnv*>(jnienv),
thread_jni.get(),
method_id,
const_cast<void*>(cur_method),
new_method);
}
}
EventHandler* event_handler = nullptr;
};
TiMethodCallback gMethodCallback;
void MethodUtil::Register(EventHandler* handler) {
gMethodCallback.event_handler = handler;
art::ScopedThreadStateChange stsc(art::Thread::Current(),
art::ThreadState::kWaitingForDebuggerToAttach);
art::ScopedSuspendAll ssa("Add method callback");
art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
callbacks->AddMethodCallback(&gMethodCallback);
}
void MethodUtil::Unregister() {
art::ScopedThreadStateChange stsc(art::Thread::Current(),
art::ThreadState::kWaitingForDebuggerToAttach);
art::ScopedSuspendAll ssa("Remove method callback");
art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
callbacks->RemoveMethodCallback(&gMethodCallback);
}
jvmtiError MethodUtil::GetBytecodes(jvmtiEnv* env,
jmethodID method,
jint* size_ptr,
unsigned char** bytecode_ptr) {
if (method == nullptr) {
return ERR(INVALID_METHODID);
}
art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
if (art_method->IsNative()) {
return ERR(NATIVE_METHOD);
}
if (size_ptr == nullptr || bytecode_ptr == nullptr) {
return ERR(NULL_POINTER);
}
art::ScopedObjectAccess soa(art::Thread::Current());
art::CodeItemInstructionAccessor accessor(art_method->DexInstructions());
if (!accessor.HasCodeItem()) {
*size_ptr = 0;
*bytecode_ptr = nullptr;
return OK;
}
// 2 bytes per instruction for dex code.
*size_ptr = accessor.InsnsSizeInCodeUnits() * 2;
jvmtiError err = env->Allocate(*size_ptr, bytecode_ptr);
if (err != OK) {
return err;
}
memcpy(*bytecode_ptr, accessor.Insns(), *size_ptr);
return OK;
}
jvmtiError MethodUtil::GetArgumentsSize(jvmtiEnv* env ATTRIBUTE_UNUSED,
jmethodID method,
jint* size_ptr) {
if (method == nullptr) {
return ERR(INVALID_METHODID);
}
art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
if (art_method->IsNative()) {
return ERR(NATIVE_METHOD);
}
if (size_ptr == nullptr) {
return ERR(NULL_POINTER);
}
art::ScopedObjectAccess soa(art::Thread::Current());
if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
// Use the shorty.
art::ArtMethod* base_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
size_t arg_count = art::ArtMethod::NumArgRegisters(base_method->GetShorty());
if (!base_method->IsStatic()) {
arg_count++;
}
*size_ptr = static_cast<jint>(arg_count);
return ERR(NONE);
}
DCHECK(art_method->HasCodeItem());
DCHECK_NE(art_method->GetCodeItem(), nullptr);
*size_ptr = art_method->DexInstructionData().InsSize();
return ERR(NONE);
}
jvmtiError MethodUtil::GetLocalVariableTable(jvmtiEnv* env,
jmethodID method,
jint* entry_count_ptr,
jvmtiLocalVariableEntry** table_ptr) {
if (method == nullptr) {
return ERR(INVALID_METHODID);
}
art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
if (art_method->IsNative()) {
return ERR(NATIVE_METHOD);
}
if (entry_count_ptr == nullptr || table_ptr == nullptr) {
return ERR(NULL_POINTER);
}
art::ScopedObjectAccess soa(art::Thread::Current());
const art::DexFile* const dex_file = art_method->GetDexFile();
if (dex_file == nullptr) {
return ERR(ABSENT_INFORMATION);
}
// TODO HasCodeItem == false means that the method is abstract (or native, but we check that
// earlier). We should check what is returned by the RI in this situation since it's not clear
// what the appropriate return value is from the spec.
art::CodeItemDebugInfoAccessor accessor(art_method->DexInstructionDebugInfo());
if (!accessor.HasCodeItem()) {
return ERR(ABSENT_INFORMATION);
}
std::vector<jvmtiLocalVariableEntry> variables;
jvmtiError err = OK;
auto release = [&](jint* out_entry_count_ptr, jvmtiLocalVariableEntry** out_table_ptr) {
jlong table_size = sizeof(jvmtiLocalVariableEntry) * variables.size();
if (err != OK ||
(err = env->Allocate(table_size,
reinterpret_cast<unsigned char**>(out_table_ptr))) != OK) {
for (jvmtiLocalVariableEntry& e : variables) {
env->Deallocate(reinterpret_cast<unsigned char*>(e.name));
env->Deallocate(reinterpret_cast<unsigned char*>(e.signature));
env->Deallocate(reinterpret_cast<unsigned char*>(e.generic_signature));
}
return err;
}
*out_entry_count_ptr = variables.size();
memcpy(*out_table_ptr, variables.data(), table_size);
return OK;
};
auto visitor = [&](const art::DexFile::LocalInfo& entry) {
if (err != OK) {
return;
}
JvmtiUniquePtr<char[]> name_str = CopyString(env, entry.name_, &err);
if (err != OK) {
return;
}
JvmtiUniquePtr<char[]> sig_str = CopyString(env, entry.descriptor_, &err);
if (err != OK) {
return;
}
JvmtiUniquePtr<char[]> generic_sig_str = CopyString(env, entry.signature_, &err);
if (err != OK) {
return;
}
variables.push_back({
.start_location = static_cast<jlocation>(entry.start_address_),
.length = static_cast<jint>(entry.end_address_ - entry.start_address_),
.name = name_str.release(),
.signature = sig_str.release(),
.generic_signature = generic_sig_str.release(),
.slot = entry.reg_,
});
};
if (!accessor.DecodeDebugLocalInfo(art_method->IsStatic(),
art_method->GetDexMethodIndex(),
visitor)) {
// Something went wrong with decoding the debug information. It might as well not be there.
return ERR(ABSENT_INFORMATION);
}
return release(entry_count_ptr, table_ptr);
}
jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED,
jmethodID method,
jint* max_ptr) {
if (method == nullptr) {
return ERR(INVALID_METHODID);
}
art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
if (art_method->IsNative()) {
return ERR(NATIVE_METHOD);
}
if (max_ptr == nullptr) {
return ERR(NULL_POINTER);
}
art::ScopedObjectAccess soa(art::Thread::Current());
if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
// This isn't specified as an error case, so return 0.
*max_ptr = 0;
return ERR(NONE);
}
DCHECK(art_method->HasCodeItem());
DCHECK_NE(art_method->GetCodeItem(), nullptr);
*max_ptr = art_method->DexInstructionData().RegistersSize();
return ERR(NONE);
}
jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
jmethodID method,
char** name_ptr,
char** signature_ptr,
char** generic_ptr) {
art::ScopedObjectAccess soa(art::Thread::Current());
art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
JvmtiUniquePtr<char[]> name_copy;
if (name_ptr != nullptr) {
const char* method_name = art_method->GetName();
if (method_name == nullptr) {
method_name = "<error>";
}
jvmtiError ret;
name_copy = CopyString(env, method_name, &ret);
if (name_copy == nullptr) {
return ret;
}
*name_ptr = name_copy.get();
}
JvmtiUniquePtr<char[]> signature_copy;
if (signature_ptr != nullptr) {
const art::Signature sig = art_method->GetSignature();
std::string str = sig.ToString();
jvmtiError ret;
signature_copy = CopyString(env, str.c_str(), &ret);
if (signature_copy == nullptr) {
return ret;
}
*signature_ptr = signature_copy.get();
}
if (generic_ptr != nullptr) {
*generic_ptr = nullptr;
if (!art_method->GetDeclaringClass()->IsProxyClass()) {
art::ObjPtr<art::mirror::ObjectArray<art::mirror::String>> str_array =
art::annotations::GetSignatureAnnotationForMethod(art_method);
if (str_array != nullptr) {
std::ostringstream oss;
for (auto str : str_array->Iterate()) {
oss << str->ToModifiedUtf8();
}
std::string output_string = oss.str();
jvmtiError ret;
JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
if (generic_copy == nullptr) {
return ret;
}
*generic_ptr = generic_copy.release();
} else if (soa.Self()->IsExceptionPending()) {
// TODO: Should we report an error here?
soa.Self()->ClearException();
}
}
}
// Everything is fine, release the buffers.
name_copy.release();
signature_copy.release();
return ERR(NONE);
}
jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
jmethodID method,
jclass* declaring_class_ptr) {
if (declaring_class_ptr == nullptr) {
return ERR(NULL_POINTER);
}
art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
// Note: No GetInterfaceMethodIfProxy, we want to actual class.
art::ScopedObjectAccess soa(art::Thread::Current());
art::ObjPtr<art::mirror::Class> klass = art_method->GetDeclaringClass();
*declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
return ERR(NONE);
}
jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
jmethodID method,
jlocation* start_location_ptr,
jlocation* end_location_ptr) {
if (method == nullptr) {
return ERR(INVALID_METHODID);
}
art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
if (art_method->IsNative()) {
return ERR(NATIVE_METHOD);
}
if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
return ERR(NULL_POINTER);
}
art::ScopedObjectAccess soa(art::Thread::Current());
if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
// This isn't specified as an error case, so return -1/-1 as the RI does.
*start_location_ptr = -1;
*end_location_ptr = -1;
return ERR(NONE);
}
DCHECK(art_method->HasCodeItem());
DCHECK_NE(art_method->GetCodeItem(), nullptr);
*start_location_ptr = 0;
*end_location_ptr = art_method->DexInstructions().InsnsSizeInCodeUnits() - 1;
return ERR(NONE);
}
jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
jmethodID method,
jint* modifiers_ptr) {
if (modifiers_ptr == nullptr) {
return ERR(NULL_POINTER);
}
art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
uint32_t modifiers = art_method->GetAccessFlags();
// Note: Keep this code in sync with Executable.fixMethodFlags.
if ((modifiers & art::kAccAbstract) != 0) {
modifiers &= ~art::kAccNative;
}
modifiers &= ~art::kAccSynchronized;
if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
modifiers |= art::kAccSynchronized;
}
modifiers &= art::kAccJavaFlagsMask;
*modifiers_ptr = modifiers;
return ERR(NONE);
}
jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
jmethodID method,
jint* entry_count_ptr,
jvmtiLineNumberEntry** table_ptr) {
if (method == nullptr) {
return ERR(NULL_POINTER);
}
art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
DCHECK(!art_method->IsRuntimeMethod());
art::CodeItemDebugInfoAccessor accessor;
const art::DexFile* dex_file;
{
art::ScopedObjectAccess soa(art::Thread::Current());
if (art_method->IsProxyMethod()) {
return ERR(ABSENT_INFORMATION);
}
if (art_method->IsNative()) {
return ERR(NATIVE_METHOD);
}
if (entry_count_ptr == nullptr || table_ptr == nullptr) {
return ERR(NULL_POINTER);
}
accessor = art::CodeItemDebugInfoAccessor(art_method->DexInstructionDebugInfo());
dex_file = art_method->GetDexFile();
DCHECK(accessor.HasCodeItem()) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
}
std::vector<jvmtiLineNumberEntry> context;
bool success = accessor.DecodeDebugPositionInfo([&](const art::DexFile::PositionInfo& entry) {
context.push_back({static_cast<jlocation>(entry.address_), static_cast<jint>(entry.line_)});
return false;
});
if (!success) {
return ERR(ABSENT_INFORMATION);
}
unsigned char* data;
jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
jvmtiError alloc_error = env->Allocate(mem_size, &data);
if (alloc_error != ERR(NONE)) {
return alloc_error;
}
*table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
memcpy(*table_ptr, context.data(), mem_size);
*entry_count_ptr = static_cast<jint>(context.size());
return ERR(NONE);
}
template <typename T>
static jvmtiError IsMethodT(jvmtiEnv* env ATTRIBUTE_UNUSED,
jmethodID method,
T test,
jboolean* is_t_ptr) {
if (method == nullptr) {
return ERR(INVALID_METHODID);
}
if (is_t_ptr == nullptr) {
return ERR(NULL_POINTER);
}
art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
*is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE;
return ERR(NONE);
}
jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) {
auto test = [](art::ArtMethod* method) {
return method->IsNative();
};
return IsMethodT(env, m, test, is_native_ptr);
}
jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) {
auto test = [](art::ArtMethod* method) {
return method->IsObsolete();
};
return IsMethodT(env, m, test, is_obsolete_ptr);
}
jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) {
auto test = [](art::ArtMethod* method) {
return method->IsSynthetic();
};
return IsMethodT(env, m, test, is_synthetic_ptr);
}
class CommonLocalVariableClosure : public art::Closure {
public:
// The verifier isn't always able to be as specific as the local-variable-table. We can only get
// 32-bit, 64-bit or reference.
enum class VerifierPrimitiveType {
k32BitValue, // float, int, short, char, boolean, byte
k64BitValue, // double, long
kReferenceValue, // Object
kZeroValue, // null or zero constant. Might be either k32BitValue or kReferenceValue
};
using SlotType = std::variant<art::Primitive::Type, VerifierPrimitiveType>;
CommonLocalVariableClosure(jvmtiEnv* jvmti, jint depth, jint slot)
: jvmti_(jvmti), result_(ERR(INTERNAL)), depth_(depth), slot_(slot) {}
void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) {
art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
bool needs_instrument;
{
art::ScopedAssertNoThreadSuspension sants("CommonLocalVariableClosure::Run");
std::unique_ptr<art::Context> context(art::Context::Create());
FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
visitor.WalkStack();
if (!visitor.FoundFrame()) {
// Must have been a bad depth.
result_ = ERR(NO_MORE_FRAMES);
return;
}
art::ArtMethod* method = visitor.GetMethod();
// Native and 'art' proxy methods don't have registers.
if (method->IsNative() || method->IsProxyMethod()) {
// TODO It might be useful to fake up support for get at least on proxy frames.
result_ = ERR(OPAQUE_FRAME);
return;
} else if (slot_ >= method->DexInstructionData().RegistersSize() || slot_ < 0) {
result_ = ERR(INVALID_SLOT);
return;
}
needs_instrument = !visitor.IsShadowFrame();
uint32_t pc = visitor.GetDexPc(/*abort_on_failure=*/false);
if (pc == art::dex::kDexNoIndex) {
// Cannot figure out current PC.
result_ = ERR(OPAQUE_FRAME);
return;
}
std::string descriptor;
SlotType slot_type{ art::Primitive::kPrimVoid };
jvmtiError err = GetSlotType(method, pc, &descriptor, &slot_type);
if (err != OK) {
result_ = err;
return;
}
err = GetTypeError(method, slot_type, descriptor);
if (err != OK) {
result_ = err;
return;
}
result_ = Execute(method, visitor);
}
if (needs_instrument) {
DeoptManager::Get()->DeoptimizeThread(self);
}
}
virtual jvmtiError GetResult() {
return result_;
}
protected:
virtual jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
virtual jvmtiError GetTypeError(art::ArtMethod* method,
SlotType type,
const std::string& descriptor)
REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
jvmtiError GetSlotType(art::ArtMethod* method,
uint32_t dex_pc,
/*out*/std::string* descriptor,
/*out*/SlotType* type)
REQUIRES_SHARED(art::Locks::mutator_lock_);
jvmtiError InferSlotTypeFromVerifier(art::ArtMethod* method,
uint32_t dex_pc,
/*out*/ std::string* descriptor,
/*out*/ SlotType* type)
REQUIRES_SHARED(art::Locks::mutator_lock_) {
std::unique_ptr<art::verifier::MethodVerifier> verifier(
art::verifier::MethodVerifier::CalculateVerificationInfo(
art::Thread::Current(),
method,
dex_pc));
if (verifier == nullptr) {
JVMTI_LOG(WARNING, jvmti_) << "Unable to extract verification information from "
<< method->PrettyMethod() << " due to hard verification failures! "
<< "How did this method even get loaded!";
return ERR(INTERNAL);
}
art::verifier::RegisterLine* line = verifier->GetRegLine(dex_pc);
if (line == nullptr) {
JVMTI_LOG(WARNING, jvmti_) << "Unable to determine register line at dex-pc " << dex_pc
<< " for method " << method->PrettyMethod();
return ERR(OPAQUE_FRAME);
}
const art::verifier::RegType& rt = line->GetRegisterType(verifier.get(), slot_);
if (rt.IsUndefined()) {
return ERR(INVALID_SLOT);
} else if (rt.IsNonZeroReferenceTypes() || rt.IsNull()) {
*descriptor = (rt.HasClass() ? rt.GetDescriptor() : "Ljava/lang/Object;");
*type = VerifierPrimitiveType::kReferenceValue;
return OK;
} else if (rt.IsZero()) {
*descriptor = "I";
*type = VerifierPrimitiveType::kZeroValue;
return OK;
} else if (rt.IsCategory1Types()) {
*descriptor = "I";
*type = VerifierPrimitiveType::k32BitValue;
return OK;
} else if (rt.IsCategory2Types() && rt.IsLowHalf()) {
*descriptor = "J";
*type = VerifierPrimitiveType::k64BitValue;
return OK;
} else {
// The slot doesn't have a type. Must not be valid here.
return ERR(INVALID_SLOT);
}
}
constexpr VerifierPrimitiveType SquashType(SlotType t) {
if (std::holds_alternative<art::Primitive::Type>(t)) {
switch (std::get<art::Primitive::Type>(t)) {
// 32-bit primitives
case art::Primitive::kPrimByte:
case art::Primitive::kPrimChar:
case art::Primitive::kPrimInt:
case art::Primitive::kPrimShort:
case art::Primitive::kPrimBoolean:
case art::Primitive::kPrimFloat:
return VerifierPrimitiveType::k32BitValue;
// 64-bit primitives
case art::Primitive::kPrimLong:
case art::Primitive::kPrimDouble:
return VerifierPrimitiveType::k64BitValue;
case art::Primitive::kPrimNot:
return VerifierPrimitiveType::kReferenceValue;
case art::Primitive::kPrimVoid:
LOG(FATAL) << "Got kPrimVoid";
UNREACHABLE();
}
} else {
return std::get<VerifierPrimitiveType>(t);
}
}
jvmtiEnv* jvmti_;
jvmtiError result_;
jint depth_;
jint slot_;
private:
DISALLOW_COPY_AND_ASSIGN(CommonLocalVariableClosure);
};
std::ostream& operator<<(std::ostream& os,
CommonLocalVariableClosure::VerifierPrimitiveType state) {
switch (state) {
case CommonLocalVariableClosure::VerifierPrimitiveType::k32BitValue:
return os << "32BitValue";
case CommonLocalVariableClosure::VerifierPrimitiveType::k64BitValue:
return os << "64BitValue";
case CommonLocalVariableClosure::VerifierPrimitiveType::kReferenceValue:
return os << "ReferenceValue";
case CommonLocalVariableClosure::VerifierPrimitiveType::kZeroValue:
return os << "ZeroValue";
}
}
std::ostream& operator<<(std::ostream& os, CommonLocalVariableClosure::SlotType state) {
if (std::holds_alternative<art::Primitive::Type>(state)) {
return os << "Primitive::Type[" << std::get<art::Primitive::Type>(state) << "]";
} else {
return os << "VerifierPrimitiveType["
<< std::get<CommonLocalVariableClosure::VerifierPrimitiveType>(state) << "]";
}
}
jvmtiError CommonLocalVariableClosure::GetSlotType(art::ArtMethod* method,
uint32_t dex_pc,
/*out*/ std::string* descriptor,
/*out*/ SlotType* type) {
const art::DexFile* dex_file = method->GetDexFile();
if (dex_file == nullptr) {
return ERR(OPAQUE_FRAME);
}
art::CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
if (!accessor.HasCodeItem()) {
return ERR(OPAQUE_FRAME);
}
bool found = false;
*type = art::Primitive::kPrimVoid;
descriptor->clear();
auto visitor = [&](const art::DexFile::LocalInfo& entry) {
if (!found && entry.start_address_ <= dex_pc && entry.end_address_ > dex_pc &&
entry.reg_ == slot_) {
found = true;
*type = art::Primitive::GetType(entry.descriptor_[0]);
*descriptor = entry.descriptor_;
}
};
if (!accessor.DecodeDebugLocalInfo(method->IsStatic(), method->GetDexMethodIndex(), visitor) ||
!found) {
// Something went wrong with decoding the debug information. It might as well not be there.
// Try to find the type with the verifier.
// TODO This is very slow.
return InferSlotTypeFromVerifier(method, dex_pc, descriptor, type);
} else if (art::kIsDebugBuild) {
std::string type_unused;
SlotType verifier_type{ art::Primitive::kPrimVoid };
DCHECK_EQ(InferSlotTypeFromVerifier(method, dex_pc, &type_unused, &verifier_type), OK)
<< method->PrettyMethod() << " failed to verify!";
if (*type == SlotType{ art::Primitive::kPrimNot }) {
// We cannot distinguish between a constant 0 and a null reference so we return that it is a
// 32bit value (Due to the way references are read by the interpreter this is safe even if
// it's modified, the value will remain null). This is not ideal since it prevents modifying
// locals in some circumstances but generally is not a big deal (since one can just modify it
// later once it's been determined to be a reference by a later instruction).
DCHECK(verifier_type == SlotType { VerifierPrimitiveType::kZeroValue } ||
verifier_type == SlotType { VerifierPrimitiveType::kReferenceValue })
<< "Verifier disagrees on type of slot! debug: " << *type
<< " verifier: " << verifier_type;
} else if (verifier_type == SlotType { VerifierPrimitiveType::kZeroValue }) {
DCHECK(VerifierPrimitiveType::k32BitValue == SquashType(*type) ||
VerifierPrimitiveType::kReferenceValue == SquashType(*type))
<< "Verifier disagrees on type of slot! debug: " << *type
<< " verifier: " << verifier_type;
} else {
DCHECK_EQ(SquashType(verifier_type), SquashType(*type))
<< "Verifier disagrees on type of slot! debug: " << *type
<< " verifier: " << verifier_type;
}
}
return OK;
}
class GetLocalVariableClosure : public CommonLocalVariableClosure {
public:
GetLocalVariableClosure(jvmtiEnv* jvmti,
jint depth,
jint slot,
art::Primitive::Type type,
jvalue* val)
: CommonLocalVariableClosure(jvmti, depth, slot),
type_(type),
val_(val),
obj_val_(nullptr) {}
jvmtiError GetResult() override REQUIRES_SHARED(art::Locks::mutator_lock_) {
if (result_ == OK && type_ == art::Primitive::kPrimNot) {
if (obj_val_ == nullptr) {
val_->l = nullptr;
} else {
art::JNIEnvExt* jni = art::Thread::Current()->GetJniEnv();
val_->l = static_cast<JNIEnv*>(jni)->NewLocalRef(obj_val_);
jni->DeleteGlobalRef(obj_val_);
obj_val_ = nullptr;
}
}
return CommonLocalVariableClosure::GetResult();
}
protected:
jvmtiError
GetTypeError(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor) override
REQUIRES_SHARED(art::Locks::mutator_lock_) {
jvmtiError res = GetTypeErrorInner(method, slot_type, descriptor);
if (res == ERR(TYPE_MISMATCH)) {
JVMTI_LOG(INFO, jvmti_) << "Unable to Get local variable in slot " << slot_ << ". Expected"
<< " slot to be of type compatible with " << SlotType { type_ }
<< " but slot is " << slot_type;
} else if (res != OK) {
JVMTI_LOG(INFO, jvmti_) << "Unable to get local variable in slot " << slot_ << ".";
}
return res;
}
jvmtiError GetTypeErrorInner(art::ArtMethod* method ATTRIBUTE_UNUSED,
SlotType slot_type,
const std::string& descriptor ATTRIBUTE_UNUSED)
REQUIRES_SHARED(art::Locks::mutator_lock_) {
switch (type_) {
case art::Primitive::kPrimFloat:
case art::Primitive::kPrimInt: {
if (std::holds_alternative<VerifierPrimitiveType>(slot_type)) {
return (slot_type == SlotType { VerifierPrimitiveType::k32BitValue } ||
slot_type == SlotType { VerifierPrimitiveType::kZeroValue })
? OK
: ERR(TYPE_MISMATCH);
} else if (type_ == art::Primitive::kPrimFloat ||
slot_type == SlotType { art::Primitive::kPrimFloat }) {
// Check that we are actually a float.
return (SlotType { type_ } == slot_type) ? OK : ERR(TYPE_MISMATCH);
} else {
// Some smaller int type.
return SquashType(slot_type) == SquashType(SlotType { type_ }) ? OK : ERR(TYPE_MISMATCH);
}
}
case art::Primitive::kPrimLong:
case art::Primitive::kPrimDouble: {
// todo
if (std::holds_alternative<VerifierPrimitiveType>(slot_type)) {
return (slot_type == SlotType { VerifierPrimitiveType::k64BitValue })
? OK
: ERR(TYPE_MISMATCH);
} else {
return slot_type == SlotType { type_ } ? OK : ERR(TYPE_MISMATCH);
}
}
case art::Primitive::kPrimNot:
return (SquashType(slot_type) == VerifierPrimitiveType::kReferenceValue ||
SquashType(slot_type) == VerifierPrimitiveType::kZeroValue)
? OK
: ERR(TYPE_MISMATCH);
case art::Primitive::kPrimShort:
case art::Primitive::kPrimChar:
case art::Primitive::kPrimByte:
case art::Primitive::kPrimBoolean:
case art::Primitive::kPrimVoid:
LOG(FATAL) << "Unexpected primitive type " << slot_type;
UNREACHABLE();
}
}
jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
override REQUIRES_SHARED(art::Locks::mutator_lock_) {
switch (type_) {
case art::Primitive::kPrimNot: {
uint32_t ptr_val;
if (!visitor.GetVReg(method,
static_cast<uint16_t>(slot_),
art::kReferenceVReg,
&ptr_val)) {
return ERR(OPAQUE_FRAME);
}
art::ObjPtr<art::mirror::Object> obj(reinterpret_cast<art::mirror::Object*>(ptr_val));
obj_val_ = art::Runtime::Current()->GetJavaVM()->AddGlobalRef(art::Thread::Current(), obj);
break;
}
case art::Primitive::kPrimInt:
case art::Primitive::kPrimFloat: {
if (!visitor.GetVReg(method,
static_cast<uint16_t>(slot_),
type_ == art::Primitive::kPrimFloat ? art::kFloatVReg : art::kIntVReg,
reinterpret_cast<uint32_t*>(&val_->i))) {
return ERR(OPAQUE_FRAME);
}
break;
}
case art::Primitive::kPrimDouble:
case art::Primitive::kPrimLong: {
auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
if (!visitor.GetVRegPair(method,
static_cast<uint16_t>(slot_),
lo_type,
high_type,
reinterpret_cast<uint64_t*>(&val_->j))) {
return ERR(OPAQUE_FRAME);
}
break;
}
default: {
LOG(FATAL) << "unexpected register type " << type_;
UNREACHABLE();
}
}
return OK;
}
private:
art::Primitive::Type type_;
jvalue* val_;
// A global reference to the return value. We use the global reference to safely transfer the
// value between threads.
jobject obj_val_;
};
jvmtiError MethodUtil::GetLocalVariableGeneric(jvmtiEnv* env,
jthread thread,
jint depth,
jint slot,
art::Primitive::Type type,
jvalue* val) {
if (depth < 0) {
return ERR(ILLEGAL_ARGUMENT);
}
art::Thread* self = art::Thread::Current();
art::ScopedObjectAccess soa(self);
art::Locks::thread_list_lock_->ExclusiveLock(self);
art::Thread* target = nullptr;
jvmtiError err = ERR(INTERNAL);
if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
art::Locks::thread_list_lock_->ExclusiveUnlock(self);
return err;
}
GetLocalVariableClosure c(env, depth, slot, type, val);
// RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
if (!target->RequestSynchronousCheckpoint(&c)) {
return ERR(THREAD_NOT_ALIVE);
} else {
return c.GetResult();
}
}
class SetLocalVariableClosure : public CommonLocalVariableClosure {
public:
SetLocalVariableClosure(jvmtiEnv* jvmti,
art::Thread* caller,
jint depth,
jint slot,
art::Primitive::Type type,
jvalue val)
: CommonLocalVariableClosure(jvmti, depth, slot), caller_(caller), type_(type), val_(val) {}
protected:
jvmtiError
GetTypeError(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor) override
REQUIRES_SHARED(art::Locks::mutator_lock_) {
jvmtiError res = GetTypeErrorInner(method, slot_type, descriptor);
if (res != OK) {
if (res == ERR(TYPE_MISMATCH)) {
std::ostringstream desc_exp;
std::ostringstream desc_set;
if (type_ == art::Primitive::kPrimNot) {
desc_exp << " (type: " << descriptor << ")";
art::ObjPtr<art::mirror::Object> new_val(art::Thread::Current()->DecodeJObject(val_.l));
desc_set << " (type: "
<< (new_val.IsNull() ? "NULL" : new_val->GetClass()->PrettyDescriptor()) << ")";
}
JVMTI_LOG(INFO, jvmti_) << "Unable to Set local variable in slot " << slot_ << ". Expected"
<< " slot to be of type compatible with " << SlotType{ type_ }
<< desc_set.str() << " but slot is " << slot_type << desc_exp.str();
} else {
JVMTI_LOG(INFO, jvmti_) << "Unable to set local variable in slot " << slot_ << ". "
<< err_.str();
}
}
return res;
}
jvmtiError