mirror of
https://bitbucket.org/smil3y/katie.git
synced 2025-02-24 19:02:59 +00:00
drop malloc validation from JavaScriptCore
compilers can do this nowdays, the only reason to keep the FastAllocBase classs is so that fastMalloc (and friends) are used which call CRASH() on allocation failure. Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
d1a2cbf056
commit
95b360b6cd
5 changed files with 5 additions and 247 deletions
|
@ -33,7 +33,6 @@
|
|||
#include "OpaqueJSString.h"
|
||||
#include "SourceCode.h"
|
||||
#include <interpreter/CallFrame.h>
|
||||
#include <runtime/InitializeThreading.h>
|
||||
#include <runtime/Completion.h>
|
||||
#include <runtime/JSGlobalObject.h>
|
||||
#include <runtime/JSLock.h>
|
||||
|
|
|
@ -93,27 +93,21 @@ namespace WTF {
|
|||
|
||||
void* operator new(size_t size)
|
||||
{
|
||||
void* p = fastMalloc(size);
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeClassNew);
|
||||
return p;
|
||||
return fastMalloc(size);
|
||||
}
|
||||
|
||||
void operator delete(void* p)
|
||||
{
|
||||
fastMallocMatchValidateFree(p, Internal::AllocTypeClassNew);
|
||||
fastFree(p);
|
||||
}
|
||||
|
||||
void* operator new[](size_t size)
|
||||
{
|
||||
void* p = fastMalloc(size);
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeClassNewArray);
|
||||
return p;
|
||||
return fastMalloc(size);
|
||||
}
|
||||
|
||||
void operator delete[](void* p)
|
||||
{
|
||||
fastMallocMatchValidateFree(p, Internal::AllocTypeClassNewArray);
|
||||
fastFree(p);
|
||||
}
|
||||
};
|
||||
|
@ -128,7 +122,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return 0;
|
||||
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeFastNew);
|
||||
return ::new(p) T;
|
||||
}
|
||||
|
||||
|
@ -140,7 +133,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return 0;
|
||||
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeFastNew);
|
||||
return ::new(p) T(arg1);
|
||||
}
|
||||
|
||||
|
@ -152,7 +144,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return 0;
|
||||
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeFastNew);
|
||||
return ::new(p) T(arg1, arg2);
|
||||
}
|
||||
|
||||
|
@ -164,7 +155,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return 0;
|
||||
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeFastNew);
|
||||
return ::new(p) T(arg1, arg2, arg3);
|
||||
}
|
||||
|
||||
|
@ -176,7 +166,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return 0;
|
||||
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeFastNew);
|
||||
return ::new(p) T(arg1, arg2, arg3, arg4);
|
||||
}
|
||||
|
||||
|
@ -188,7 +177,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return 0;
|
||||
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeFastNew);
|
||||
return ::new(p) T(arg1, arg2, arg3, arg4, arg5);
|
||||
}
|
||||
|
||||
|
@ -215,9 +203,7 @@ namespace WTF {
|
|||
struct NewArrayImpl {
|
||||
static T* fastNewArray(size_t count)
|
||||
{
|
||||
T* p = static_cast<T*>(fastMalloc(sizeof(T) * count));
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeFastNewArray);
|
||||
return p;
|
||||
return static_cast<T*>(fastMalloc(sizeof(T) * count));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -232,8 +218,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return 0;
|
||||
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeFastNewArray);
|
||||
|
||||
for (T* pObject = p, *pObjectEnd = pObject + count; pObject != pObjectEnd; ++pObject)
|
||||
::new(pObject) T;
|
||||
|
||||
|
@ -253,7 +237,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return 0;
|
||||
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeFastNewArray);
|
||||
*a.size++ = count;
|
||||
// No need to construct the objects in this case.
|
||||
|
||||
|
@ -273,7 +256,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return 0;
|
||||
|
||||
fastMallocMatchValidateMalloc(p, Internal::AllocTypeFastNewArray);
|
||||
*a.size++ = count;
|
||||
|
||||
for (T* pT = a.t, *pTEnd = pT + count; pT != pTEnd; ++pT)
|
||||
|
@ -296,7 +278,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return;
|
||||
|
||||
fastMallocMatchValidateFree(p, Internal::AllocTypeFastNew);
|
||||
p->~T();
|
||||
fastFree(p);
|
||||
}
|
||||
|
@ -307,7 +288,6 @@ namespace WTF {
|
|||
if (!p)
|
||||
return;
|
||||
|
||||
fastMallocMatchValidateFree(p, Internal::AllocTypeFastNew);
|
||||
fastFree(p);
|
||||
}
|
||||
|
||||
|
@ -320,7 +300,6 @@ namespace WTF {
|
|||
{
|
||||
// No need to destruct the objects in this case.
|
||||
// We expect that fastFree checks for null.
|
||||
fastMallocMatchValidateFree(p, Internal::AllocTypeFastNewArray);
|
||||
fastFree(p);
|
||||
}
|
||||
};
|
||||
|
@ -342,7 +321,6 @@ namespace WTF {
|
|||
while (pEnd-- != p)
|
||||
pEnd->~T();
|
||||
|
||||
fastMallocMatchValidateFree(a.size, Internal::AllocTypeFastNewArray);
|
||||
fastFree(a.size);
|
||||
}
|
||||
};
|
||||
|
@ -359,7 +337,6 @@ namespace WTF {
|
|||
template <typename T>
|
||||
inline void fastNonNullDelete(T* p)
|
||||
{
|
||||
fastMallocMatchValidateFree(p, Internal::AllocTypeFastNew);
|
||||
p->~T();
|
||||
fastFree(p);
|
||||
}
|
||||
|
@ -371,7 +348,6 @@ namespace WTF {
|
|||
struct NonNullDeleteArrayImpl {
|
||||
static void fastNonNullDeleteArray(void* p)
|
||||
{
|
||||
fastMallocMatchValidateFree(p, Internal::AllocTypeFastNewArray);
|
||||
// No need to destruct the objects in this case.
|
||||
fastFree(p);
|
||||
}
|
||||
|
@ -391,7 +367,6 @@ namespace WTF {
|
|||
while (pEnd-- != p)
|
||||
pEnd->~T();
|
||||
|
||||
fastMallocMatchValidateFree(a.size, Internal::AllocTypeFastNewArray);
|
||||
fastFree(a.size);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -153,19 +153,6 @@ void fastMallocAllow()
|
|||
|
||||
namespace WTF {
|
||||
|
||||
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
|
||||
|
||||
namespace Internal {
|
||||
|
||||
void fastMallocMatchFailed(void*)
|
||||
{
|
||||
CRASH();
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
#endif
|
||||
|
||||
void* fastZeroedMalloc(size_t n)
|
||||
{
|
||||
void* result = fastMalloc(n);
|
||||
|
@ -201,35 +188,14 @@ TryMallocReturnValue tryFastMalloc(size_t n)
|
|||
{
|
||||
ASSERT(!isForbidden());
|
||||
|
||||
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
|
||||
if (std::numeric_limits<size_t>::max() - sizeof(AllocAlignmentInteger) <= n) // If overflow would occur...
|
||||
return 0;
|
||||
|
||||
void* result = malloc(n + sizeof(AllocAlignmentInteger));
|
||||
if (!result)
|
||||
return 0;
|
||||
|
||||
*static_cast<AllocAlignmentInteger*>(result) = Internal::AllocTypeMalloc;
|
||||
result = static_cast<AllocAlignmentInteger*>(result) + 1;
|
||||
|
||||
return result;
|
||||
#else
|
||||
return malloc(n);
|
||||
#endif
|
||||
}
|
||||
|
||||
void* fastMalloc(size_t n)
|
||||
{
|
||||
ASSERT(!isForbidden());
|
||||
|
||||
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
|
||||
TryMallocReturnValue returnValue = tryFastMalloc(n);
|
||||
void* result;
|
||||
returnValue.getValue(result);
|
||||
#else
|
||||
void* result = malloc(n);
|
||||
#endif
|
||||
|
||||
if (!result)
|
||||
CRASH();
|
||||
return result;
|
||||
|
@ -239,37 +205,14 @@ TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t element_size)
|
|||
{
|
||||
ASSERT(!isForbidden());
|
||||
|
||||
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
|
||||
size_t totalBytes = n_elements * element_size;
|
||||
if (n_elements > 1 && element_size && (totalBytes / element_size) != n_elements || (std::numeric_limits<size_t>::max() - sizeof(AllocAlignmentInteger) <= totalBytes))
|
||||
return 0;
|
||||
|
||||
totalBytes += sizeof(AllocAlignmentInteger);
|
||||
void* result = malloc(totalBytes);
|
||||
if (!result)
|
||||
return 0;
|
||||
|
||||
memset(result, 0, totalBytes);
|
||||
*static_cast<AllocAlignmentInteger*>(result) = Internal::AllocTypeMalloc;
|
||||
result = static_cast<AllocAlignmentInteger*>(result) + 1;
|
||||
return result;
|
||||
#else
|
||||
return calloc(n_elements, element_size);
|
||||
#endif
|
||||
}
|
||||
|
||||
void* fastCalloc(size_t n_elements, size_t element_size)
|
||||
{
|
||||
ASSERT(!isForbidden());
|
||||
|
||||
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
|
||||
TryMallocReturnValue returnValue = tryFastCalloc(n_elements, element_size);
|
||||
void* result;
|
||||
returnValue.getValue(result);
|
||||
#else
|
||||
void* result = calloc(n_elements, element_size);
|
||||
#endif
|
||||
|
||||
if (!result)
|
||||
CRASH();
|
||||
return result;
|
||||
|
@ -279,73 +222,24 @@ void fastFree(void* p)
|
|||
{
|
||||
ASSERT(!isForbidden());
|
||||
|
||||
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
|
||||
if (!p)
|
||||
return;
|
||||
|
||||
AllocAlignmentInteger* header = Internal::fastMallocMatchValidationValue(p);
|
||||
if (*header != Internal::AllocTypeMalloc)
|
||||
Internal::fastMallocMatchFailed(p);
|
||||
free(header);
|
||||
#else
|
||||
free(p);
|
||||
#endif
|
||||
}
|
||||
|
||||
TryMallocReturnValue tryFastRealloc(void* p, size_t n)
|
||||
{
|
||||
ASSERT(!isForbidden());
|
||||
|
||||
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
|
||||
if (p) {
|
||||
if (std::numeric_limits<size_t>::max() - sizeof(AllocAlignmentInteger) <= n) // If overflow would occur...
|
||||
return 0;
|
||||
AllocAlignmentInteger* header = Internal::fastMallocMatchValidationValue(p);
|
||||
if (*header != Internal::AllocTypeMalloc)
|
||||
Internal::fastMallocMatchFailed(p);
|
||||
void* result = realloc(header, n + sizeof(AllocAlignmentInteger));
|
||||
if (!result)
|
||||
return 0;
|
||||
|
||||
// This should not be needed because the value is already there:
|
||||
// *static_cast<AllocAlignmentInteger*>(result) = Internal::AllocTypeMalloc;
|
||||
result = static_cast<AllocAlignmentInteger*>(result) + 1;
|
||||
return result;
|
||||
} else {
|
||||
return fastMalloc(n);
|
||||
}
|
||||
#else
|
||||
return realloc(p, n);
|
||||
#endif
|
||||
}
|
||||
|
||||
void* fastRealloc(void* p, size_t n)
|
||||
{
|
||||
ASSERT(!isForbidden());
|
||||
|
||||
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
|
||||
TryMallocReturnValue returnValue = tryFastRealloc(p, n);
|
||||
void* result;
|
||||
returnValue.getValue(result);
|
||||
#else
|
||||
void* result = realloc(p, n);
|
||||
#endif
|
||||
|
||||
if (!result)
|
||||
CRASH();
|
||||
return result;
|
||||
}
|
||||
|
||||
FastMallocStatistics fastMallocStatistics()
|
||||
{
|
||||
FastMallocStatistics statistics = { 0, 0, 0, 0 };
|
||||
return statistics;
|
||||
}
|
||||
|
||||
} // namespace WTF
|
||||
|
||||
#if OS(DARWIN)
|
||||
// This symbol is present in the JavaScriptCore exports file even when FastMalloc is disabled.
|
||||
// It will never be used in this case, so it's type and value are less interesting than its presence.
|
||||
extern "C" const int jscore_fastmalloc_introspection = 0;
|
||||
#endif
|
||||
|
|
|
@ -56,7 +56,7 @@ namespace WTF {
|
|||
private:
|
||||
mutable void* m_data;
|
||||
};
|
||||
|
||||
|
||||
template <typename T> bool TryMallocReturnValue::getValue(T& data)
|
||||
{
|
||||
union u { void* data; T target; } res;
|
||||
|
@ -74,108 +74,14 @@ namespace WTF {
|
|||
|
||||
void fastFree(void*);
|
||||
|
||||
#ifndef NDEBUG
|
||||
#ifndef NDEBUG
|
||||
void fastMallocForbid();
|
||||
void fastMallocAllow();
|
||||
#endif
|
||||
|
||||
struct FastMallocStatistics {
|
||||
size_t heapSize;
|
||||
size_t freeSizeInHeap;
|
||||
size_t freeSizeInCaches;
|
||||
size_t returnedSize;
|
||||
};
|
||||
FastMallocStatistics fastMallocStatistics();
|
||||
|
||||
// This defines a type which holds an unsigned integer and is the same
|
||||
// size as the minimally aligned memory allocation.
|
||||
typedef unsigned long long AllocAlignmentInteger;
|
||||
|
||||
namespace Internal {
|
||||
enum AllocType { // Start with an unusual number instead of zero, because zero is common.
|
||||
AllocTypeMalloc = 0x375d6750, // Encompasses fastMalloc, fastZeroedMalloc, fastCalloc, fastRealloc.
|
||||
AllocTypeClassNew, // Encompasses class operator new from FastAllocBase.
|
||||
AllocTypeClassNewArray, // Encompasses class operator new[] from FastAllocBase.
|
||||
AllocTypeFastNew, // Encompasses fastNew.
|
||||
AllocTypeFastNewArray, // Encompasses fastNewArray.
|
||||
AllocTypeNew, // Encompasses global operator new.
|
||||
AllocTypeNewArray // Encompasses global operator new[].
|
||||
};
|
||||
}
|
||||
|
||||
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
|
||||
|
||||
// Malloc validation is a scheme whereby a tag is attached to an
|
||||
// allocation which identifies how it was originally allocated.
|
||||
// This allows us to verify that the freeing operation matches the
|
||||
// allocation operation. If memory is allocated with operator new[]
|
||||
// but freed with free or delete, this system would detect that.
|
||||
// In the implementation here, the tag is an integer prepended to
|
||||
// the allocation memory which is assigned one of the AllocType
|
||||
// enumeration values. An alternative implementation of this
|
||||
// scheme could store the tag somewhere else or ignore it.
|
||||
// Users of FastMalloc don't need to know or care how this tagging
|
||||
// is implemented.
|
||||
|
||||
namespace Internal {
|
||||
|
||||
// Return the AllocType tag associated with the allocated block p.
|
||||
inline AllocType fastMallocMatchValidationType(const void* p)
|
||||
{
|
||||
const AllocAlignmentInteger* type = static_cast<const AllocAlignmentInteger*>(p) - 1;
|
||||
return static_cast<AllocType>(*type);
|
||||
}
|
||||
|
||||
// Return the address of the AllocType tag associated with the allocated block p.
|
||||
inline AllocAlignmentInteger* fastMallocMatchValidationValue(void* p)
|
||||
{
|
||||
return reinterpret_cast<AllocAlignmentInteger*>(static_cast<char*>(p) - sizeof(AllocAlignmentInteger));
|
||||
}
|
||||
|
||||
// Set the AllocType tag to be associaged with the allocated block p.
|
||||
inline void setFastMallocMatchValidationType(void* p, AllocType allocType)
|
||||
{
|
||||
AllocAlignmentInteger* type = static_cast<AllocAlignmentInteger*>(p) - 1;
|
||||
*type = static_cast<AllocAlignmentInteger>(allocType);
|
||||
}
|
||||
|
||||
// Handle a detected alloc/free mismatch. By default this calls CRASH().
|
||||
void fastMallocMatchFailed(void* p);
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
// This is a higher level function which is used by FastMalloc-using code.
|
||||
inline void fastMallocMatchValidateMalloc(void* p, Internal::AllocType allocType)
|
||||
{
|
||||
if (!p)
|
||||
return;
|
||||
|
||||
Internal::setFastMallocMatchValidationType(p, allocType);
|
||||
}
|
||||
|
||||
// This is a higher level function which is used by FastMalloc-using code.
|
||||
inline void fastMallocMatchValidateFree(void* p, Internal::AllocType allocType)
|
||||
{
|
||||
if (!p)
|
||||
return;
|
||||
|
||||
if (Internal::fastMallocMatchValidationType(p) != allocType)
|
||||
Internal::fastMallocMatchFailed(p);
|
||||
Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc); // Set it to this so that fastFree thinks it's OK.
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType)
|
||||
{
|
||||
}
|
||||
|
||||
inline void fastMallocMatchValidateFree(void*, Internal::AllocType)
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace WTF
|
||||
|
||||
using WTF::fastMalloc;
|
||||
|
@ -194,14 +100,4 @@ using WTF::fastMallocForbid;
|
|||
using WTF::fastMallocAllow;
|
||||
#endif
|
||||
|
||||
#if COMPILER(GCC) && OS(DARWIN)
|
||||
#define WTF_PRIVATE_INLINE __private_extern__ inline __attribute__((always_inline))
|
||||
#elif COMPILER(GCC)
|
||||
#define WTF_PRIVATE_INLINE inline __attribute__((always_inline))
|
||||
#elif COMPILER(MSVC) || COMPILER(RVCT)
|
||||
#define WTF_PRIVATE_INLINE __forceinline
|
||||
#else
|
||||
#define WTF_PRIVATE_INLINE inline
|
||||
#endif
|
||||
|
||||
#endif /* WTF_FastMalloc_h */
|
||||
|
|
|
@ -648,12 +648,6 @@
|
|||
|
||||
/* ENABLE macro defaults */
|
||||
|
||||
/* fastMalloc match validation allows for runtime verification that
|
||||
new is matched by delete, fastMalloc is matched by fastFree, etc. */
|
||||
#if !defined(ENABLE_FAST_MALLOC_MATCH_VALIDATION)
|
||||
#define ENABLE_FAST_MALLOC_MATCH_VALIDATION 0
|
||||
#endif
|
||||
|
||||
#if !defined(ENABLE_JAVASCRIPT_DEBUGGER)
|
||||
#define ENABLE_JAVASCRIPT_DEBUGGER 1
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue