Skip to content

Commit

Permalink
Merge r241733 - Bytecode cache should a have a boot-specific validation
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=194769
<rdar://problem/48149509>

Reviewed by Keith Miller.

Source/JavaScriptCore:

Add the boot UUID to the cached bytecode to enforce that it is not reused
across reboots.

* runtime/CachedTypes.cpp:
(JSC::Encoder::malloc):
(JSC::GenericCacheEntry::GenericCacheEntry):
(JSC::GenericCacheEntry::tag const):
(JSC::CacheEntry::CacheEntry):
(JSC::CacheEntry::decode const):
(JSC::GenericCacheEntry::decode const):
(JSC::encodeCodeBlock):

Source/WTF:

Add helper to get kern.bootsessionuuid from sysctl

* wtf/UUID.cpp:
(WTF::bootSessionUUIDString):
* wtf/UUID.h:

Tools:

Add test for WTF::bootSessionUUIDString()

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/UUID.cpp: Added.
(TEST):
  • Loading branch information
tadeuzagallo authored and carlosgcampos committed Feb 20, 2019
1 parent 4799f10 commit 2dc42f0
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 12 deletions.
20 changes: 20 additions & 0 deletions Source/JavaScriptCore/ChangeLog
@@ -1,3 +1,23 @@
2019-02-18 Tadeu Zagallo <tzagallo@apple.com>

Bytecode cache should a have a boot-specific validation
https://bugs.webkit.org/show_bug.cgi?id=194769
<rdar://problem/48149509>

Reviewed by Keith Miller.

Add the boot UUID to the cached bytecode to enforce that it is not reused
across reboots.

* runtime/CachedTypes.cpp:
(JSC::Encoder::malloc):
(JSC::GenericCacheEntry::GenericCacheEntry):
(JSC::GenericCacheEntry::tag const):
(JSC::CacheEntry::CacheEntry):
(JSC::CacheEntry::decode const):
(JSC::GenericCacheEntry::decode const):
(JSC::encodeCodeBlock):

2019-02-16 Yusuke Suzuki <ysuzuki@apple.com>

[JSC] JSWrapperObject should not be destructible
Expand Down
31 changes: 19 additions & 12 deletions Source/JavaScriptCore/runtime/CachedTypes.cpp
Expand Up @@ -41,6 +41,7 @@
#include <wtf/FastMalloc.h>
#include <wtf/Forward.h>
#include <wtf/Optional.h>
#include <wtf/UUID.h>
#include <wtf/text/AtomicStringImpl.h>

namespace JSC {
Expand Down Expand Up @@ -102,10 +103,10 @@ class Encoder {
return malloc(size);
}

template<typename T>
T* malloc()
template<typename T, typename... Args>
T* malloc(Args&&... args)
{
return new (malloc(sizeof(T)).buffer()) T();
return new (malloc(sizeof(T)).buffer()) T(std::forward<Args>(args)...);
}

ptrdiff_t offsetOf(const void* address)
Expand Down Expand Up @@ -1990,20 +1991,25 @@ class GenericCacheEntry {
bool decode(Decoder&, std::pair<SourceCodeKey, UnlinkedCodeBlock*>&) const;

protected:
GenericCacheEntry(CachedCodeBlockTag tag)
GenericCacheEntry(Encoder& encoder, CachedCodeBlockTag tag)
: m_tag(tag)
{
m_bootSessionUUID.encode(encoder, bootSessionUUIDString());
}

CachedCodeBlockTag tag() const { return m_tag; }

private:
uint32_t m_cacheVersion { JSC_BYTECODE_CACHE_VERSION };
CachedString m_bootSessionUUID;
CachedCodeBlockTag m_tag;
};

template<typename UnlinkedCodeBlockType>
class CacheEntry : public GenericCacheEntry {
public:
CacheEntry()
: GenericCacheEntry(CachedCodeBlockTypeImpl<UnlinkedCodeBlockType>::tag)
CacheEntry(Encoder& encoder)
: GenericCacheEntry(encoder, CachedCodeBlockTypeImpl<UnlinkedCodeBlockType>::tag)
{
}

Expand All @@ -2018,11 +2024,7 @@ class CacheEntry : public GenericCacheEntry {

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;
ASSERT(tag() == CachedCodeBlockTypeImpl<UnlinkedCodeBlockType>::tag);
SourceCodeKey decodedKey;
m_key.decode(decoder, decodedKey);
result = { WTFMove(decodedKey), m_codeBlock.decode(decoder) };
Expand All @@ -2035,6 +2037,11 @@ class CacheEntry : public GenericCacheEntry {

bool GenericCacheEntry::decode(Decoder& decoder, std::pair<SourceCodeKey, UnlinkedCodeBlock*>& result) const
{
if (m_cacheVersion != JSC_BYTECODE_CACHE_VERSION)
return false;
if (m_bootSessionUUID.decode(decoder) != bootSessionUUIDString())
return false;

switch (m_tag) {
case CachedProgramCodeBlockTag:
return reinterpret_cast<const CacheEntry<UnlinkedProgramCodeBlock>*>(this)->decode(decoder, reinterpret_cast<std::pair<SourceCodeKey, UnlinkedProgramCodeBlock*>&>(result));
Expand All @@ -2054,7 +2061,7 @@ bool GenericCacheEntry::decode(Decoder& decoder, std::pair<SourceCodeKey, Unlink
template<typename UnlinkedCodeBlockType>
void encodeCodeBlock(Encoder& encoder, const SourceCodeKey& key, const UnlinkedCodeBlock* codeBlock)
{
auto* entry = encoder.template malloc<CacheEntry<UnlinkedCodeBlockType>>();
auto* entry = encoder.template malloc<CacheEntry<UnlinkedCodeBlockType>>(encoder);
entry->encode(encoder, { key, jsCast<const UnlinkedCodeBlockType*>(codeBlock) });
}

Expand Down
14 changes: 14 additions & 0 deletions Source/WTF/ChangeLog
@@ -1,3 +1,17 @@
2019-02-18 Tadeu Zagallo <tzagallo@apple.com>

Bytecode cache should a have a boot-specific validation
https://bugs.webkit.org/show_bug.cgi?id=194769
<rdar://problem/48149509>

Reviewed by Keith Miller.

Add helper to get kern.bootsessionuuid from sysctl

* wtf/UUID.cpp:
(WTF::bootSessionUUIDString):
* wtf/UUID.h:

2019-02-15 Dominik Infuehr <dinfuehr@igalia.com>

Fix deadlock on Linux/x64 between SamplingProfiler and VMTraps
Expand Down
21 changes: 21 additions & 0 deletions Source/WTF/wtf/UUID.cpp
Expand Up @@ -31,10 +31,15 @@
#include "config.h"
#include <wtf/UUID.h>

#include <mutex>
#include <wtf/CryptographicallyRandomNumber.h>
#include <wtf/HexNumber.h>
#include <wtf/text/StringBuilder.h>

#if OS(DARWIN)
#include <sys/sysctl.h>
#endif

namespace WTF {

String createCanonicalUUIDString()
Expand All @@ -59,4 +64,20 @@ String createCanonicalUUIDString()
return builder.toString();
}

String bootSessionUUIDString()
{
static LazyNeverDestroyed<String> bootSessionUUID;
#if OS(DARWIN)
static std::once_flag onceKey;
std::call_once(onceKey, [] {
size_t uuidLength = 37;
char uuid[uuidLength];
if (sysctlbyname("kern.bootsessionuuid", uuid, &uuidLength, nullptr, 0))
return;
bootSessionUUID.construct(static_cast<const char*>(uuid), uuidLength - 1);
});
#endif
return bootSessionUUID;
}

} // namespace WTF
3 changes: 3 additions & 0 deletions Source/WTF/wtf/UUID.h
Expand Up @@ -45,6 +45,9 @@ namespace WTF {

WTF_EXPORT_PRIVATE String createCanonicalUUIDString();

WTF_EXPORT_PRIVATE String bootSessionUUIDString();

}

using WTF::createCanonicalUUIDString;
using WTF::bootSessionUUIDString;
14 changes: 14 additions & 0 deletions Tools/ChangeLog
@@ -1,3 +1,17 @@
2019-02-18 Tadeu Zagallo <tzagallo@apple.com>

Bytecode cache should a have a boot-specific validation
https://bugs.webkit.org/show_bug.cgi?id=194769
<rdar://problem/48149509>

Reviewed by Keith Miller.

Add test for WTF::bootSessionUUIDString()

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/UUID.cpp: Added.
(TEST):

2019-02-16 Zalan Bujtas <zalan@apple.com>

[LFC] Apply min/max width constraints to preferred width computation
Expand Down
2 changes: 2 additions & 0 deletions Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Expand Up @@ -1352,6 +1352,7 @@
11B7FD22219F46DD0069B27F /* FirstMeaningfulPaintMilestone.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FirstMeaningfulPaintMilestone.cpp; sourceTree = "<group>"; };
11C2598C21FA618D004C9E23 /* async-script-load.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "async-script-load.html"; sourceTree = "<group>"; };
14464012167A8305000BD218 /* LayoutUnit.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LayoutUnit.cpp; sourceTree = "<group>"; };
144D40EC221B46A7004B474F /* UUID.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UUID.cpp; sourceTree = "<group>"; };
14F3B11215E45EAB00210069 /* SaturatedArithmeticOperations.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SaturatedArithmeticOperations.cpp; sourceTree = "<group>"; };
1A02C84B125D4A5E00E3F4BD /* find.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = find.html; sourceTree = "<group>"; };
1A02C84E125D4A8400E3F4BD /* Find.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Find.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3292,6 +3293,7 @@
5C5E633D1D0B67940085A025 /* UniqueRef.cpp */,
E3A1E78021B25B79008C6007 /* URL.cpp */,
E3A1E78421B25B91008C6007 /* URLParser.cpp */,
144D40EC221B46A7004B474F /* UUID.cpp */,
7CD0D5AA1D5534DE000CC9E1 /* Variant.cpp */,
BC55F5F814AD78EE00484BE1 /* Vector.cpp */,
1CB9BC371A67482300FE5678 /* WeakPtr.cpp */,
Expand Down
33 changes: 33 additions & 0 deletions Tools/TestWebKitAPI/Tests/WTF/UUID.cpp
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"

#include <wtf/UUID.h>

TEST(WTF, BootSessionUUIDIdentity)
{
EXPECT_EQ(bootSessionUUIDString(), bootSessionUUIDString());
}

0 comments on commit 2dc42f0

Please sign in to comment.