Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "base64.h" |
| 4 | #include "heap.h" |
| 5 | #include "thread.h" |
| 6 | #include "class_linker.h" |
| 7 | #include "dex_file.h" |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 8 | |
| 9 | #include "gtest/gtest.h" |
| 10 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 11 | namespace art { |
| 12 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 13 | // package java.lang; |
| 14 | // public class Object {} |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame^] | 15 | |
| 16 | // package java.lang; |
| 17 | // public class Class {} |
| 18 | // |
| 19 | // package java.lang.reflect; |
| 20 | // public class Field {} |
| 21 | // |
| 22 | // package java.lang.reflect; |
| 23 | // public class Method {} |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 24 | // |
| 25 | // package java.lang; |
| 26 | // public interface Cloneable {} |
| 27 | // |
| 28 | // package java.io; |
| 29 | // public interface Serializable {} |
| 30 | static const char kJavaLangDex[] = |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame^] | 31 | "ZGV4CjAzNQCgffHhLqornhe/ZtOPPH5jBex6xYfwloPAAwAAcAAAAHhWNBIAAAAAAAAAADgDAAAO" |
| 32 | "AAAAcAAAAAcAAACoAAAAAQAAAMQAAAAAAAAAAAAAAAQAAADQAAAABgAAAPAAAAAQAgAAsAEAAAwC" |
| 33 | "AAAUAgAAIAIAADACAAA8AgAAVAIAAGcCAAB+AgAAkgIAAK0CAADJAgAA1gIAAOMCAAD2AgAABAAA" |
| 34 | "AAUAAAAGAAAABwAAAA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47 | "AiAAAA4AAAAMAgAAAyAAAAQAAAD5AgAAACAAAAQAAAANAwAAABAAAAEAAAA4AwAA"; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 48 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 49 | // package java.lang; |
| 50 | // public class Object {} |
| 51 | // |
| 52 | // class MyClass {} |
| 53 | static const char kMyClassDex[] = |
| 54 | "ZGV4CjAzNQA5Nm9IrCVm91COwepff7LhIE23GZIxGjgIAgAAcAAAAHhWNBIAAAAAAAAAAIABAAAG" |
| 55 | "AAAAcAAAAAMAAACIAAAAAQAAAJQAAAAAAAAAAAAAAAIAAACgAAAAAgAAALAAAAAYAQAA8AAAABwB" |
| 56 | "AAAkAQAALwEAAEMBAABRAQAAXgEAAAEAAAACAAAABQAAAAUAAAACAAAAAAAAAAAAAAAAAAAAAQAA" |
| 57 | "AAAAAAABAAAAAQAAAP////8AAAAABAAAAAAAAABrAQAAAAAAAAAAAAAAAAAAAQAAAAAAAAADAAAA" |
| 58 | "AAAAAHUBAAAAAAAAAQABAAAAAABhAQAAAQAAAA4AAAABAAEAAQAAAGYBAAAEAAAAcBABAAAADgAG" |
| 59 | "PGluaXQ+AAlMTXlDbGFzczsAEkxqYXZhL2xhbmcvT2JqZWN0OwAMTXlDbGFzcy5qYXZhAAtPYmpl" |
| 60 | "Y3QuamF2YQABVgACAAcOAAUABw4AAAABAAGBgATwAQAAAQAAgIAEhAIACwAAAAAAAAABAAAAAAAA" |
| 61 | "AAEAAAAGAAAAcAAAAAIAAAADAAAAiAAAAAMAAAABAAAAlAAAAAUAAAACAAAAoAAAAAYAAAACAAAA" |
| 62 | "sAAAAAEgAAACAAAA8AAAAAIgAAAGAAAAHAEAAAMgAAACAAAAYQEAAAAgAAACAAAAawEAAAAQAAAB" |
| 63 | "AAAAgAEAAA=="; |
| 64 | |
| 65 | // class Nested { |
| 66 | // class Inner { |
| 67 | // } |
| 68 | // } |
| 69 | static const char kNestedDex[] = |
| 70 | "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP" |
| 71 | "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B" |
| 72 | "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA" |
| 73 | "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA" |
| 74 | "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA" |
| 75 | "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA" |
| 76 | "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA" |
| 77 | "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu" |
| 78 | "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2" |
| 79 | "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz" |
| 80 | "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA" |
| 81 | "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA" |
| 82 | "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH" |
| 83 | "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ" |
| 84 | "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA" |
| 85 | "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA=="; |
| 86 | |
| 87 | // class ProtoCompare { |
| 88 | // int m1(short x, int y, long z) { return x + y + (int)z; } |
| 89 | // int m2(short x, int y, long z) { return x + y + (int)z; } |
| 90 | // int m3(long x, int y, short z) { return (int)x + y + z; } |
| 91 | // long m4(long x, int y, short z) { return x + y + z; } |
| 92 | // } |
| 93 | static const char kProtoCompareDex[] = |
| 94 | "ZGV4CjAzNQBLUetu+TVZ8gsYsCOFoij7ecsHaGSEGA8gAwAAcAAAAHhWNBIAAAAAAAAAAIwCAAAP" |
| 95 | "AAAAcAAAAAYAAACsAAAABAAAAMQAAAAAAAAAAAAAAAYAAAD0AAAAAQAAACQBAADcAQAARAEAAN4B" |
| 96 | "AADmAQAA6QEAAO8BAAD1AQAA+AEAAP4BAAAOAgAAIgIAADUCAAA4AgAAOwIAAD8CAABDAgAARwIA" |
| 97 | "AAEAAAAEAAAABgAAAAcAAAAJAAAACgAAAAIAAAAAAAAAyAEAAAMAAAAAAAAA1AEAAAUAAAABAAAA" |
| 98 | "yAEAAAoAAAAFAAAAAAAAAAIAAwAAAAAAAgABAAsAAAACAAEADAAAAAIAAAANAAAAAgACAA4AAAAD" |
| 99 | "AAMAAAAAAAIAAAAAAAAAAwAAAAAAAAAIAAAAAAAAAHACAAAAAAAAAQABAAEAAABLAgAABAAAAHAQ" |
| 100 | "BQAAAA4ABwAFAAAAAABQAgAABQAAAJAAAwSEUbAQDwAAAAcABQAAAAAAWAIAAAUAAACQAAMEhFGw" |
| 101 | "EA8AAAAGAAUAAAAAAGACAAAEAAAAhCCwQLBQDwAJAAUAAAAAAGgCAAAFAAAAgXC7UIGCuyAQAAAA" |
| 102 | "AwAAAAEAAAAEAAAAAwAAAAQAAAABAAY8aW5pdD4AAUkABElKSVMABElTSUoAAUoABEpKSVMADkxQ" |
| 103 | "cm90b0NvbXBhcmU7ABJMamF2YS9sYW5nL09iamVjdDsAEVByb3RvQ29tcGFyZS5qYXZhAAFTAAFW" |
| 104 | "AAJtMQACbTIAAm0zAAJtNAABAAcOAAIDAAAABw4AAwMAAAAHDgAEAwAAAAcOAAUDAAAABw4AAAAB" |
| 105 | "BACAgATEAgEA3AIBAPgCAQCUAwEArAMAAAwAAAAAAAAAAQAAAAAAAAABAAAADwAAAHAAAAACAAAA" |
| 106 | "BgAAAKwAAAADAAAABAAAAMQAAAAFAAAABgAAAPQAAAAGAAAAAQAAACQBAAABIAAABQAAAEQBAAAB" |
| 107 | "EAAAAgAAAMgBAAACIAAADwAAAN4BAAADIAAABQAAAEsCAAAAIAAAAQAAAHACAAAAEAAAAQAAAIwC" |
| 108 | "AAA="; |
| 109 | |
| 110 | // class ProtoCompare2 { |
| 111 | // int m1(short x, int y, long z) { return x + y + (int)z; } |
| 112 | // int m2(short x, int y, long z) { return x + y + (int)z; } |
| 113 | // int m3(long x, int y, short z) { return (int)x + y + z; } |
| 114 | // long m4(long x, int y, short z) { return x + y + z; } |
| 115 | // } |
| 116 | static const char kProtoCompare2Dex[] = |
| 117 | "ZGV4CjAzNQDVUXj687EpyTTDJZEZPA8dEYnDlm0Ir6YgAwAAcAAAAHhWNBIAAAAAAAAAAIwCAAAP" |
| 118 | "AAAAcAAAAAYAAACsAAAABAAAAMQAAAAAAAAAAAAAAAYAAAD0AAAAAQAAACQBAADcAQAARAEAAN4B" |
| 119 | "AADmAQAA6QEAAO8BAAD1AQAA+AEAAP4BAAAPAgAAIwIAADcCAAA6AgAAPQIAAEECAABFAgAASQIA" |
| 120 | "AAEAAAAEAAAABgAAAAcAAAAJAAAACgAAAAIAAAAAAAAAyAEAAAMAAAAAAAAA1AEAAAUAAAABAAAA" |
| 121 | "yAEAAAoAAAAFAAAAAAAAAAIAAwAAAAAAAgABAAsAAAACAAEADAAAAAIAAAANAAAAAgACAA4AAAAD" |
| 122 | "AAMAAAAAAAIAAAAAAAAAAwAAAAAAAAAIAAAAAAAAAHICAAAAAAAAAQABAAEAAABNAgAABAAAAHAQ" |
| 123 | "BQAAAA4ABwAFAAAAAABSAgAABQAAAJAAAwSEUbAQDwAAAAcABQAAAAAAWgIAAAUAAACQAAMEhFGw" |
| 124 | "EA8AAAAGAAUAAAAAAGICAAAEAAAAhCCwQLBQDwAJAAUAAAAAAGoCAAAFAAAAgXC7UIGCuyAQAAAA" |
| 125 | "AwAAAAEAAAAEAAAAAwAAAAQAAAABAAY8aW5pdD4AAUkABElKSVMABElTSUoAAUoABEpKSVMAD0xQ" |
| 126 | "cm90b0NvbXBhcmUyOwASTGphdmEvbGFuZy9PYmplY3Q7ABJQcm90b0NvbXBhcmUyLmphdmEAAVMA" |
| 127 | "AVYAAm0xAAJtMgACbTMAAm00AAEABw4AAgMAAAAHDgADAwAAAAcOAAQDAAAABw4ABQMAAAAHDgAA" |
| 128 | "AAEEAICABMQCAQDcAgEA+AIBAJQDAQCsAwwAAAAAAAAAAQAAAAAAAAABAAAADwAAAHAAAAACAAAA" |
| 129 | "BgAAAKwAAAADAAAABAAAAMQAAAAFAAAABgAAAPQAAAAGAAAAAQAAACQBAAABIAAABQAAAEQBAAAB" |
| 130 | "EAAAAgAAAMgBAAACIAAADwAAAN4BAAADIAAABQAAAE0CAAAAIAAAAQAAAHICAAAAEAAAAQAAAIwC" |
| 131 | "AAA="; |
| 132 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 133 | // javac MyClass.java && dx --dex --output=MyClass.dex |
| 134 | // --core-library MyClass.class java/lang/Object.class && base64 MyClass.dex |
| 135 | // package java.lang; |
| 136 | // public class Object {} |
| 137 | // class MyClass { |
| 138 | // native void foo(); |
| 139 | // native int fooI(int x); |
| 140 | // native int fooII(int x, int y); |
| 141 | // native double fooDD(double x, double y); |
| 142 | // native Object fooIOO(int x, Object y, Object z); |
| 143 | // static native Object fooSIOO(int x, Object y, Object z); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 144 | // static synchronized native Object fooSSIOO(int x, Object y, Object z); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 145 | // } |
| 146 | static const char kMyClassNativesDex[] = |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 147 | "ZGV4CjAzNQA4WWrpXgdlkoTHR8Yubx4LJO4HbGsX1p1EAwAAcAAAAHhWNBIAAAAAAAAAALACAAAT" |
| 148 | "AAAAcAAAAAUAAAC8AAAABQAAANAAAAAAAAAAAAAAAAkAAAAMAQAAAgAAAFQBAACwAQAAlAEAAOIB" |
| 149 | "AADqAQAA7QEAAPIBAAD1AQAA+QEAAP4BAAAEAgAADwIAACMCAAAxAgAAPgIAAEECAABGAgAATQIA" |
| 150 | "AFMCAABaAgAAYgIAAGsCAAABAAAAAwAAAAcAAAAIAAAACwAAAAIAAAAAAAAAwAEAAAQAAAABAAAA" |
| 151 | "yAEAAAUAAAABAAAA0AEAAAYAAAADAAAA2AEAAAsAAAAEAAAAAAAAAAIABAAAAAAAAgAEAAwAAAAC" |
| 152 | "AAAADQAAAAIAAQAOAAAAAgACAA8AAAACAAMAEAAAAAIAAwARAAAAAgADABIAAAADAAQAAAAAAAMA" |
| 153 | "AAABAAAA/////wAAAAAKAAAAAAAAAH8CAAAAAAAAAgAAAAAAAAADAAAAAAAAAAkAAAAAAAAAiQIA" |
| 154 | "AAAAAAABAAEAAAAAAHUCAAABAAAADgAAAAEAAQABAAAAegIAAAQAAABwEAgAAAAOAAIAAAAAAAAA" |
| 155 | "AQAAAAEAAAACAAAAAQABAAMAAAABAAMAAwAGPGluaXQ+AAFEAANEREQAAUkAAklJAANJSUkABExJ" |
| 156 | "TEwACUxNeUNsYXNzOwASTGphdmEvbGFuZy9PYmplY3Q7AAxNeUNsYXNzLmphdmEAC09iamVjdC5q" |
| 157 | "YXZhAAFWAANmb28ABWZvb0REAARmb29JAAVmb29JSQAGZm9vSU9PAAdmb29TSU9PAAhmb29TU0lP" |
| 158 | "TwADAAcOAAEABw4AAAABAAiBgASUAwAAAwUAgIAEqAMGiAIAAaiCCAABgAIAAYACAAGAAgABgAIA" |
| 159 | "AYACAAwAAAAAAAAAAQAAAAAAAAABAAAAEwAAAHAAAAACAAAABQAAALwAAAADAAAABQAAANAAAAAF" |
| 160 | "AAAACQAAAAwBAAAGAAAAAgAAAFQBAAABIAAAAgAAAJQBAAABEAAABAAAAMABAAACIAAAEwAAAOIB" |
| 161 | "AAADIAAAAgAAAHUCAAAAIAAAAgAAAH8CAAAAEAAAAQAAALACAAA="; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 162 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 163 | static inline DexFile* OpenDexFileBase64(const char* base64) { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 164 | CHECK(base64 != NULL); |
| 165 | size_t length; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 166 | byte* dex_bytes = DecodeBase64(base64, &length); |
| 167 | CHECK(dex_bytes != NULL); |
| 168 | DexFile* dex_file = DexFile::OpenPtr(dex_bytes, length); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 169 | CHECK(dex_file != NULL); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 170 | return dex_file; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | class RuntimeTest : public testing::Test { |
| 174 | protected: |
| 175 | virtual void SetUp() { |
| 176 | ASSERT_TRUE(Thread::Init()); |
| 177 | ASSERT_TRUE(Thread::Attach() != NULL); |
| 178 | ASSERT_TRUE(Heap::Init()); |
| 179 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 180 | java_lang_dex_file_.reset(OpenDexFileBase64(kJavaLangDex)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 181 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 182 | std::vector<DexFile*> boot_class_path; |
| 183 | boot_class_path.push_back(java_lang_dex_file_.get()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 184 | |
| 185 | class_linker_.reset(ClassLinker::Create(boot_class_path)); |
| 186 | CHECK(class_linker_ != NULL); |
| 187 | } |
| 188 | |
| 189 | virtual void TearDown() { |
| 190 | Heap::Destroy(); |
| 191 | } |
| 192 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 193 | scoped_ptr<DexFile> java_lang_dex_file_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 194 | scoped_ptr<ClassLinker> class_linker_; |
| 195 | }; |
| 196 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 197 | } // namespace art |