OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 14796 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14807 v8::Isolate* isolate = env->GetIsolate(); | 14807 v8::Isolate* isolate = env->GetIsolate(); |
14808 HandleScope handle_scope(isolate); | 14808 HandleScope handle_scope(isolate); |
14809 | 14809 |
14810 i::FLAG_min_preparse_length = 0; | 14810 i::FLAG_min_preparse_length = 0; |
14811 const char* script = "function foo(a) { return a+1; }"; | 14811 const char* script = "function foo(a) { return a+1; }"; |
14812 v8::ScriptCompiler::Source source(v8_str(script)); | 14812 v8::ScriptCompiler::Source source(v8_str(script)); |
14813 v8::ScriptCompiler::Compile(isolate, &source, | 14813 v8::ScriptCompiler::Compile(isolate, &source, |
14814 v8::ScriptCompiler::kProduceDataToCache); | 14814 v8::ScriptCompiler::kProduceDataToCache); |
14815 // Serialize. | 14815 // Serialize. |
14816 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); | 14816 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
14817 char* serialized_data = i::NewArray<char>(cd->length); | 14817 i::byte* serialized_data = i::NewArray<i::byte>(cd->length); |
14818 i::MemCopy(serialized_data, cd->data, cd->length); | 14818 i::MemCopy(serialized_data, cd->data, cd->length); |
14819 | 14819 |
14820 // Deserialize. | 14820 // Deserialize. |
14821 i::ScriptData* deserialized = i::ScriptData::New(serialized_data, cd->length); | 14821 i::ScriptData* deserialized = new i::ScriptData(serialized_data, cd->length); |
14822 | 14822 |
14823 // Verify that the original is the same as the deserialized. | 14823 // Verify that the original is the same as the deserialized. |
14824 CHECK_EQ(cd->length, deserialized->Length()); | 14824 CHECK_EQ(cd->length, deserialized->length()); |
14825 CHECK_EQ(0, memcmp(cd->data, deserialized->Data(), cd->length)); | 14825 CHECK_EQ(0, memcmp(cd->data, deserialized->data(), cd->length)); |
14826 | 14826 |
14827 delete deserialized; | 14827 delete deserialized; |
14828 i::DeleteArray(serialized_data); | 14828 i::DeleteArray(serialized_data); |
14829 } | 14829 } |
14830 | 14830 |
14831 | 14831 |
14832 // Attempts to deserialize bad data. | |
14833 TEST(PreCompileDeserializationError) { | |
14834 v8::V8::Initialize(); | |
14835 const char* data = "DONT CARE"; | |
14836 int invalid_size = 3; | |
14837 i::ScriptData* sd = i::ScriptData::New(data, invalid_size); | |
14838 CHECK_EQ(NULL, sd); | |
14839 } | |
14840 | |
14841 | |
14842 TEST(CompileWithInvalidCachedData) { | |
14843 v8::V8::Initialize(); | |
14844 v8::Isolate* isolate = CcTest::isolate(); | |
14845 LocalContext context; | |
14846 v8::HandleScope scope(context->GetIsolate()); | |
14847 i::FLAG_min_preparse_length = 0; | |
14848 | |
14849 const char* script = "function foo(){ return 5;}\n" | |
14850 "function bar(){ return 6 + 7;} foo();"; | |
14851 v8::ScriptCompiler::Source source(v8_str(script)); | |
14852 v8::ScriptCompiler::Compile(isolate, &source, | |
14853 v8::ScriptCompiler::kProduceDataToCache); | |
14854 // source owns its cached data. Create a ScriptData based on it. The user | |
14855 // never needs to create ScriptDatas any more; we only need it here because we | |
14856 // want to modify the data before passing it back. | |
14857 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); | |
14858 // ScriptData does not take ownership of the buffers passed to it. | |
14859 i::ScriptData* sd = | |
14860 i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); | |
14861 CHECK(!sd->HasError()); | |
14862 // ScriptData private implementation details | |
14863 const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; | |
14864 const int kFunctionEntrySize = i::FunctionEntry::kSize; | |
14865 const int kFunctionEntryStartOffset = 0; | |
14866 const int kFunctionEntryEndOffset = 1; | |
14867 unsigned* sd_data = | |
14868 reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | |
14869 | |
14870 // Overwrite function bar's end position with 0. | |
14871 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; | |
14872 v8::TryCatch try_catch; | |
14873 | |
14874 // Make the script slightly different so that we don't hit the compilation | |
14875 // cache. Don't change the lenghts of tokens. | |
14876 const char* script2 = "function foo(){ return 6;}\n" | |
14877 "function bar(){ return 6 + 7;} foo();"; | |
14878 v8::ScriptCompiler::Source source2( | |
14879 v8_str(script2), | |
14880 // CachedData doesn't take ownership of the buffers, Source takes | |
14881 // ownership of CachedData. | |
14882 new v8::ScriptCompiler::CachedData( | |
14883 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); | |
14884 Local<v8::UnboundScript> compiled_script = | |
14885 v8::ScriptCompiler::CompileUnbound(isolate, &source2); | |
14886 | |
14887 CHECK(try_catch.HasCaught()); | |
14888 { | |
14889 String::Utf8Value exception_value(try_catch.Message()->Get()); | |
14890 CHECK_EQ("Uncaught SyntaxError: Invalid cached data for function bar", | |
14891 *exception_value); | |
14892 } | |
14893 | |
14894 try_catch.Reset(); | |
14895 delete sd; | |
14896 | |
14897 // Overwrite function bar's start position with 200. The function entry will | |
14898 // not be found when searching for it by position, and the compilation fails. | |
14899 | |
14900 // ScriptData does not take ownership of the buffers passed to it. | |
14901 sd = i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); | |
14902 sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | |
14903 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = | |
14904 200; | |
14905 const char* script3 = "function foo(){ return 7;}\n" | |
14906 "function bar(){ return 6 + 7;} foo();"; | |
14907 v8::ScriptCompiler::Source source3( | |
14908 v8_str(script3), | |
14909 new v8::ScriptCompiler::CachedData( | |
14910 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); | |
14911 compiled_script = | |
14912 v8::ScriptCompiler::CompileUnbound(isolate, &source3); | |
14913 CHECK(try_catch.HasCaught()); | |
14914 { | |
14915 String::Utf8Value exception_value(try_catch.Message()->Get()); | |
14916 CHECK_EQ("Uncaught SyntaxError: Invalid cached data for function bar", | |
14917 *exception_value); | |
14918 } | |
14919 CHECK(compiled_script.IsEmpty()); | |
14920 try_catch.Reset(); | |
14921 delete sd; | |
14922 | |
14923 // Try passing in cached data which is obviously invalid (wrong length). | |
14924 sd = i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); | |
14925 const char* script4 = | |
14926 "function foo(){ return 8;}\n" | |
14927 "function bar(){ return 6 + 7;} foo();"; | |
14928 v8::ScriptCompiler::Source source4( | |
14929 v8_str(script4), | |
14930 new v8::ScriptCompiler::CachedData( | |
14931 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length() - 1)); | |
14932 compiled_script = | |
14933 v8::ScriptCompiler::CompileUnbound(isolate, &source4); | |
14934 CHECK(try_catch.HasCaught()); | |
14935 { | |
14936 String::Utf8Value exception_value(try_catch.Message()->Get()); | |
14937 CHECK_EQ("Uncaught SyntaxError: Invalid cached data", | |
14938 *exception_value); | |
14939 } | |
14940 CHECK(compiled_script.IsEmpty()); | |
14941 delete sd; | |
14942 } | |
14943 | |
14944 | |
14945 // This tests that we do not allow dictionary load/call inline caches | 14832 // This tests that we do not allow dictionary load/call inline caches |
14946 // to use functions that have not yet been compiled. The potential | 14833 // to use functions that have not yet been compiled. The potential |
14947 // problem of loading a function that has not yet been compiled can | 14834 // problem of loading a function that has not yet been compiled can |
14948 // arise because we share code between contexts via the compilation | 14835 // arise because we share code between contexts via the compilation |
14949 // cache. | 14836 // cache. |
14950 THREADED_TEST(DictionaryICLoadedFunction) { | 14837 THREADED_TEST(DictionaryICLoadedFunction) { |
14951 v8::HandleScope scope(CcTest::isolate()); | 14838 v8::HandleScope scope(CcTest::isolate()); |
14952 // Test LoadIC. | 14839 // Test LoadIC. |
14953 for (int i = 0; i < 2; i++) { | 14840 for (int i = 0; i < 2; i++) { |
14954 LocalContext context; | 14841 LocalContext context; |
(...skipping 7934 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
22889 desc = x->GetOwnPropertyDescriptor(v8_str("p1")); | 22776 desc = x->GetOwnPropertyDescriptor(v8_str("p1")); |
22890 Local<Function> set = | 22777 Local<Function> set = |
22891 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("set"))); | 22778 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("set"))); |
22892 Local<Function> get = | 22779 Local<Function> get = |
22893 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("get"))); | 22780 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("get"))); |
22894 CHECK_EQ(v8_num(13), get->Call(x, 0, NULL)); | 22781 CHECK_EQ(v8_num(13), get->Call(x, 0, NULL)); |
22895 Handle<Value> args[] = { v8_num(14) }; | 22782 Handle<Value> args[] = { v8_num(14) }; |
22896 set->Call(x, 1, args); | 22783 set->Call(x, 1, args); |
22897 CHECK_EQ(v8_num(14), get->Call(x, 0, NULL)); | 22784 CHECK_EQ(v8_num(14), get->Call(x, 0, NULL)); |
22898 } | 22785 } |
OLD | NEW |