Title: [241660] trunk/Source/_javascript_Core
Revision
241660
Author
tzaga...@apple.com
Date
2019-02-18 02:20:28 -0800 (Mon, 18 Feb 2019)

Log Message

Add version number to cached bytecode
https://bugs.webkit.org/show_bug.cgi?id=194768
<rdar://problem/48147968>

Reviewed by Saam Barati.

Add a version number to the bytecode cache that should be unique per build.

* CMakeLists.txt:
* DerivedSources-output.xcfilelist:
* DerivedSources.make:
* runtime/CachedTypes.cpp:
(JSC::Encoder::malloc):
(JSC::GenericCacheEntry::GenericCacheEntry):
(JSC::CacheEntry::CacheEntry):
(JSC::CacheEntry::encode):
(JSC::CacheEntry::decode const):
(JSC::GenericCacheEntry::decode const):
(JSC::decodeCodeBlockImpl):
* runtime/CodeCache.h:
(JSC::CodeCacheMap::fetchFromDiskImpl):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/CMakeLists.txt (241659 => 241660)


--- trunk/Source/_javascript_Core/CMakeLists.txt	2019-02-18 09:12:48 UTC (rev 241659)
+++ trunk/Source/_javascript_Core/CMakeLists.txt	2019-02-18 10:20:28 UTC (rev 241660)
@@ -228,9 +228,19 @@
     COMMAND ${RUBY_EXECUTABLE} ${_javascript_CORE_DIR}/generator/main.rb --bytecodes_h ${DERIVED_SOURCES_JAVASCRIPTCORE_DIR}/Bytecodes.h --init_bytecodes_asm ${DERIVED_SOURCES_JAVASCRIPTCORE_DIR}/InitBytecodes.asm --bytecode_structs_h ${DERIVED_SOURCES_JAVASCRIPTCORE_DIR}/BytecodeStructs.h --bytecode_indices_h ${DERIVED_SOURCES_JAVASCRIPTCORE_DIR}/BytecodeIndices.h ${_javascript_CORE_DIR}/bytecode/BytecodeList.rb
     VERBATIM)
 
+
+if (WTF_OS_MAC_OS_X)
+    execute_process(COMMAND bash -c "date +'%s'" OUTPUT_VARIABLE BUILD_TIME OUTPUT_STRIP_TRAILING_WHITESPACE)
+else ()
+    set(BUILD_TIME 0)
+endif ()
+
+file(WRITE ${DERIVED_SOURCES_JAVASCRIPTCORE_DIR}/BytecodeCacheVersion.h "#define JSC_BYTECODE_CACHE_VERSION ${BUILD_TIME}\n")
+
 list(APPEND _javascript_Core_HEADERS
+    ${DERIVED_SOURCES_JAVASCRIPTCORE_DIR}/BytecodeCacheVersion.h
+    ${DERIVED_SOURCES_JAVASCRIPTCORE_DIR}/BytecodeStructs.h
     ${DERIVED_SOURCES_JAVASCRIPTCORE_DIR}/Bytecodes.h
-    ${DERIVED_SOURCES_JAVASCRIPTCORE_DIR}/BytecodeStructs.h
 )
 
 if (WIN32)

Modified: trunk/Source/_javascript_Core/ChangeLog (241659 => 241660)


--- trunk/Source/_javascript_Core/ChangeLog	2019-02-18 09:12:48 UTC (rev 241659)
+++ trunk/Source/_javascript_Core/ChangeLog	2019-02-18 10:20:28 UTC (rev 241660)
@@ -1,3 +1,27 @@
+2019-02-18  Tadeu Zagallo  <tzaga...@apple.com>
+
+        Add version number to cached bytecode
+        https://bugs.webkit.org/show_bug.cgi?id=194768
+        <rdar://problem/48147968>
+
+        Reviewed by Saam Barati.
+
+        Add a version number to the bytecode cache that should be unique per build.
+
+        * CMakeLists.txt:
+        * DerivedSources-output.xcfilelist:
+        * DerivedSources.make:
+        * runtime/CachedTypes.cpp:
+        (JSC::Encoder::malloc):
+        (JSC::GenericCacheEntry::GenericCacheEntry):
+        (JSC::CacheEntry::CacheEntry):
+        (JSC::CacheEntry::encode):
+        (JSC::CacheEntry::decode const):
+        (JSC::GenericCacheEntry::decode const):
+        (JSC::decodeCodeBlockImpl):
+        * runtime/CodeCache.h:
+        (JSC::CodeCacheMap::fetchFromDiskImpl):
+
 2019-02-17  Saam Barati  <sbar...@apple.com>
 
         WasmB3IRGenerator models some effects incorrectly

Modified: trunk/Source/_javascript_Core/DerivedSources-output.xcfilelist (241659 => 241660)


--- trunk/Source/_javascript_Core/DerivedSources-output.xcfilelist	2019-02-18 09:12:48 UTC (rev 241659)
+++ trunk/Source/_javascript_Core/DerivedSources-output.xcfilelist	2019-02-18 10:20:28 UTC (rev 241660)
@@ -9,6 +9,7 @@
 $(BUILT_PRODUCTS_DIR)/DerivedSources/_javascript_Core/BigIntConstructor.lut.h
 $(BUILT_PRODUCTS_DIR)/DerivedSources/_javascript_Core/BigIntPrototype.lut.h
 $(BUILT_PRODUCTS_DIR)/DerivedSources/_javascript_Core/BooleanPrototype.lut.h
+$(BUILT_PRODUCTS_DIR)/DerivedSources/_javascript_Core/BytecodeCacheVersion.h
 $(BUILT_PRODUCTS_DIR)/DerivedSources/_javascript_Core/BytecodeIndices.h
 $(BUILT_PRODUCTS_DIR)/DerivedSources/_javascript_Core/BytecodeStructs.h
 $(BUILT_PRODUCTS_DIR)/DerivedSources/_javascript_Core/Bytecodes.h

Modified: trunk/Source/_javascript_Core/DerivedSources.make (241659 => 241660)


--- trunk/Source/_javascript_Core/DerivedSources.make	2019-02-18 09:12:48 UTC (rev 241659)
+++ trunk/Source/_javascript_Core/DerivedSources.make	2019-02-18 10:20:28 UTC (rev 241660)
@@ -356,3 +356,10 @@
 all : \
     $(OBJECT_LUT_HEADERS) \
 #
+
+.PHONY : BytecodeCacheVersion.h
+
+BytecodeCacheVersion.h:
+	echo "#define JSC_BYTECODE_CACHE_VERSION $(shell date '+%s')" > BytecodeCacheVersion.h
+
+all : BytecodeCacheVersion.h

Modified: trunk/Source/_javascript_Core/runtime/CachedTypes.cpp (241659 => 241660)


--- trunk/Source/_javascript_Core/runtime/CachedTypes.cpp	2019-02-18 09:12:48 UTC (rev 241659)
+++ trunk/Source/_javascript_Core/runtime/CachedTypes.cpp	2019-02-18 10:20:28 UTC (rev 241660)
@@ -26,6 +26,7 @@
 #include "config.h"
 #include "CachedTypes.h"
 
+#include "BytecodeCacheVersion.h"
 #include "BytecodeLivenessAnalysis.h"
 #include "JSCast.h"
 #include "JSImmutableButterfly.h"
@@ -104,7 +105,7 @@
     template<typename T>
     T* malloc()
     {
-        return reinterpret_cast<T*>(malloc(sizeof(T)).buffer());
+        return new (malloc(sizeof(T)).buffer()) T();
     }
 
     ptrdiff_t offsetOf(const void* address)
@@ -1986,9 +1987,15 @@
 
 class GenericCacheEntry {
 public:
-    std::pair<SourceCodeKey, UnlinkedCodeBlock*> decode(Decoder&) const;
+    bool decode(Decoder&, std::pair<SourceCodeKey, UnlinkedCodeBlock*>&) const;
 
 protected:
+    GenericCacheEntry(CachedCodeBlockTag tag)
+        : m_tag(tag)
+    {
+    }
+
+    uint32_t m_cacheVersion { JSC_BYTECODE_CACHE_VERSION };
     CachedCodeBlockTag m_tag;
 };
 
@@ -1995,9 +2002,13 @@
 template<typename UnlinkedCodeBlockType>
 class CacheEntry : public GenericCacheEntry {
 public:
+    CacheEntry()
+        : GenericCacheEntry(CachedCodeBlockTypeImpl<UnlinkedCodeBlockType>::tag)
+    {
+    }
+
     void encode(Encoder& encoder, std::pair<SourceCodeKey, const UnlinkedCodeBlockType*> pair)
     {
-        m_tag = CachedCodeBlockTypeImpl<UnlinkedCodeBlockType>::tag;
         m_key.encode(encoder, pair.first);
         m_codeBlock.encode(encoder, pair.second);
     }
@@ -2005,12 +2016,17 @@
 private:
     friend GenericCacheEntry;
 
-    std::pair<SourceCodeKey, UnlinkedCodeBlockType*> decode(Decoder& decoder) const
+    bool decode(Decoder& decoder, std::pair<SourceCodeKey, UnlinkedCodeBlockType*>& result) const
     {
+        if (m_cacheVersion != JSC_BYTECODE_CACHE_VERSION)
+            return false;
         ASSERT(m_tag == CachedCodeBlockTypeImpl<UnlinkedCodeBlockType>::tag);
+        if (m_tag != CachedCodeBlockTypeImpl<UnlinkedCodeBlockType>::tag)
+            return false;
         SourceCodeKey decodedKey;
         m_key.decode(decoder, decodedKey);
-        return { WTFMove(decodedKey), m_codeBlock.decode(decoder) };
+        result = { WTFMove(decodedKey), m_codeBlock.decode(decoder) };
+        return true;
     }
 
     CachedSourceCodeKey m_key;
@@ -2017,13 +2033,13 @@
     CachedPtr<CachedCodeBlockType<UnlinkedCodeBlockType>> m_codeBlock;
 };
 
-std::pair<SourceCodeKey, UnlinkedCodeBlock*> GenericCacheEntry::decode(Decoder& decoder) const
+bool GenericCacheEntry::decode(Decoder& decoder, std::pair<SourceCodeKey, UnlinkedCodeBlock*>& result) const
 {
     switch (m_tag) {
     case CachedProgramCodeBlockTag:
-        return reinterpret_cast<const CacheEntry<UnlinkedProgramCodeBlock>*>(this)->decode(decoder);
+        return reinterpret_cast<const CacheEntry<UnlinkedProgramCodeBlock>*>(this)->decode(decoder, reinterpret_cast<std::pair<SourceCodeKey, UnlinkedProgramCodeBlock*>&>(result));
     case CachedModuleCodeBlockTag:
-        return reinterpret_cast<const CacheEntry<UnlinkedModuleProgramCodeBlock>*>(this)->decode(decoder);
+        return reinterpret_cast<const CacheEntry<UnlinkedModuleProgramCodeBlock>*>(this)->decode(decoder, reinterpret_cast<std::pair<SourceCodeKey, UnlinkedModuleProgramCodeBlock*>&>(result));
     case CachedEvalCodeBlockTag:
         // We do not cache eval code blocks
         RELEASE_ASSERT_NOT_REACHED();
@@ -2031,7 +2047,7 @@
     RELEASE_ASSERT_NOT_REACHED();
 #if COMPILER(MSVC)
     // Without this, MSVC will complain that this path does not return a value.
-    return reinterpret_cast<const CacheEntry<UnlinkedEvalCodeBlock>*>(this)->decode(decoder);
+    return false;
 #endif
 }
 
@@ -2064,7 +2080,8 @@
     std::pair<SourceCodeKey, UnlinkedCodeBlock*> entry;
     {
         DeferGC deferGC(vm.heap);
-        entry = cachedEntry->decode(decoder);
+        if (!cachedEntry->decode(decoder, entry))
+            return nullptr;
     }
 
     if (entry.first != key)

Modified: trunk/Source/_javascript_Core/runtime/CodeCache.h (241659 => 241660)


--- trunk/Source/_javascript_Core/runtime/CodeCache.h	2019-02-18 09:12:48 UTC (rev 241659)
+++ trunk/Source/_javascript_Core/runtime/CodeCache.h	2019-02-18 10:20:28 UTC (rev 241660)
@@ -41,6 +41,7 @@
 #include "UnlinkedProgramCodeBlock.h"
 #include <sys/stat.h>
 #include <wtf/Forward.h>
+#include <wtf/Scope.h>
 #include <wtf/text/WTFString.h>
 
 namespace JSC {
@@ -134,19 +135,19 @@
         if (fd == -1)
             return nullptr;
 
+        auto closeFD = makeScopeExit([&] {
+            close(fd);
+        });
+
         int rc = flock(fd, LOCK_SH | LOCK_NB);
-        if (rc) {
-            close(fd);
+        if (rc)
             return nullptr;
-        }
 
         struct stat sb;
         int res = fstat(fd, &sb);
         size_t size = static_cast<size_t>(sb.st_size);
-        if (res || !size) {
-            close(fd);
+        if (res || !size)
             return nullptr;
-        }
 
         void* buffer = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
         UnlinkedCodeBlockType* unlinkedCodeBlock = decodeCodeBlock<UnlinkedCodeBlockType>(vm, key, buffer, size);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to