Changeset 241447 in webkit


Ignore:
Timestamp:
Feb 13, 2019 11:16:36 AM (5 years ago)
Author:
Tadeu Zagallo
Message:

VariableLengthObject::allocate<T> should initialize objects
https://bugs.webkit.org/show_bug.cgi?id=194534

Reviewed by Michael Saboff.

buffer() should not be called for empty VariableLengthObjects, but
these cases were not being caught due to the objects not being properly
initialized. Fix it so that allocate calls the constructor and fix the
assertion failues.

  • runtime/CachedTypes.cpp:

(JSC::CachedObject::operator new):
(JSC::VariableLengthObject::allocate):
(JSC::CachedVector::encode):
(JSC::CachedVector::decode const):
(JSC::CachedUniquedStringImpl::decode const):
(JSC::CachedBitVector::encode):
(JSC::CachedBitVector::decode const):
(JSC::CachedArray::encode):
(JSC::CachedArray::decode const):
(JSC::CachedImmutableButterfly::CachedImmutableButterfly):
(JSC::CachedBigInt::decode const):

Location:
trunk/Source/JavaScriptCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r241442 r241447  
     12019-02-13  Tadeu Zagallo  <tzagallo@apple.com>
     2
     3        VariableLengthObject::allocate<T> should initialize objects
     4        https://bugs.webkit.org/show_bug.cgi?id=194534
     5
     6        Reviewed by Michael Saboff.
     7
     8        `buffer()` should not be called for empty VariableLengthObjects, but
     9        these cases were not being caught due to the objects not being properly
     10        initialized. Fix it so that allocate calls the constructor and fix the
     11        assertion failues.
     12
     13        * runtime/CachedTypes.cpp:
     14        (JSC::CachedObject::operator new):
     15        (JSC::VariableLengthObject::allocate):
     16        (JSC::CachedVector::encode):
     17        (JSC::CachedVector::decode const):
     18        (JSC::CachedUniquedStringImpl::decode const):
     19        (JSC::CachedBitVector::encode):
     20        (JSC::CachedBitVector::decode const):
     21        (JSC::CachedArray::encode):
     22        (JSC::CachedArray::decode const):
     23        (JSC::CachedImmutableButterfly::CachedImmutableButterfly):
     24        (JSC::CachedBigInt::decode const):
     25
    1262019-02-13  Tadeu Zagallo  <tzagallo@apple.com>
    227
  • trunk/Source/JavaScriptCore/runtime/CachedTypes.cpp

    r241236 r241447  
    307307class CachedObject {
    308308    WTF_MAKE_NONCOPYABLE(CachedObject<Source>);
    309     WTF_FORBID_HEAP_ALLOCATION;
    310309
    311310public:
    312311    using SourceType_ = Source;
     312
     313    CachedObject() = default;
     314
     315    inline void* operator new(size_t, void* where) { return where; }
     316
     317    // Copied from WTF_FORBID_HEAP_ALLOCATION, since we only want to allow placement new
     318    void* operator new[](size_t, void*) = delete;
     319    void* operator new(size_t) = delete;
     320    void operator delete(void*) = delete;
     321    void* operator new[](size_t size) = delete;
     322    void operator delete[](void*) = delete;
     323    void* operator new(size_t, NotNullTag, void* location) = delete;
    313324};
    314325
     
    333344    uint8_t* allocate(Encoder& encoder, size_t size)
    334345    {
    335         if (!size)
    336             return nullptr;
    337 
    338346        ptrdiff_t offsetOffset = encoder.offsetOf(&m_offset);
    339347        auto result = encoder.malloc(size);
     
    346354    {
    347355        uint8_t* result = allocate(encoder, sizeof(T) * size);
    348         return reinterpret_cast<T*>(result);
     356        return new (result) T();
    349357    }
    350358
     
    464472    {
    465473        m_size = vector.size();
     474        if (!m_size)
     475            return;
    466476        T* buffer = this->template allocate<T>(encoder, m_size);
    467477        for (unsigned i = 0; i < m_size; ++i)
     
    472482    void decode(Decoder& decoder, Vector<SourceType<T>, InlineCapacity, OverflowHandler>& vector, Args... args) const
    473483    {
     484        if (!m_size)
     485            return;
    474486        vector.resizeToFit(m_size);
    475487        const T* buffer = this->template buffer<T>();
     
    571583                return AtomicStringImpl::add(buffer, m_length).leakRef();
    572584
    573             if (!m_length)
    574                 return &SymbolImpl::createNullSymbol().leakRef();
    575 
    576585            Identifier ident = Identifier::fromString(&decoder.vm(), buffer, m_length);
    577586            String str = decoder.vm().propertyNames->lookUpPrivateName(ident);
     
    581590        };
    582591
     592        if (!m_length) {
     593            if (m_isSymbol)
     594                return &SymbolImpl::createNullSymbol().leakRef();
     595            return AtomicStringImpl::add("").leakRef();
     596        }
     597
    583598        if (m_is8Bit)
    584599            return create(this->buffer<LChar>());
     
    741756    {
    742757        m_size = bitVector.size();
     758        if (!m_size)
     759            return;
    743760        uint8_t* buffer = this->allocate(encoder, m_size);
    744761        memcpy(buffer, bitVector.bits(), m_size);
     
    747764    void decode(Decoder&, BitVector& bitVector) const
    748765    {
     766        if (!m_size)
     767            return;
    749768        bitVector.ensureSize(m_size);
    750769        memcpy(bitVector.bits(), this->buffer(), m_size);
     
    861880    void encode(Encoder& encoder, const Source* array, unsigned size)
    862881    {
     882        if (!size)
     883            return;
    863884        T* dst = this->template allocate<T>(encoder, size);
    864885        for (unsigned i = 0; i < size; ++i)
     
    869890    void decode(Decoder& decoder, Source* array, unsigned size, Args... args) const
    870891    {
     892        if (!size)
     893            return;
    871894        const T* buffer = this->template buffer<T>();
    872895        for (unsigned i = 0; i < size; ++i)
     
    949972class CachedImmutableButterfly : public CachedObject<JSImmutableButterfly> {
    950973public:
     974    CachedImmutableButterfly()
     975        : m_cachedDoubles()
     976    {
     977    }
     978
    951979    void encode(Encoder& encoder, JSImmutableButterfly& immutableButterfly)
    952980    {
     
    10381066        JSBigInt* bigInt = JSBigInt::createWithLengthUnchecked(decoder.vm(), m_length);
    10391067        bigInt->setSign(m_sign);
    1040         memcpy(bigInt->dataStorage(), this->buffer(), sizeof(JSBigInt::Digit) * m_length);
     1068        if (m_length)
     1069            memcpy(bigInt->dataStorage(), this->buffer(), sizeof(JSBigInt::Digit) * m_length);
    10411070        return bigInt;
    10421071    }
Note: See TracChangeset for help on using the changeset viewer.