1/*
2 * Copyright (C) 2017-2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/FastMalloc.h>
29#include <wtf/StdLibExtras.h>
30
31#if defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC
32#define GIGACAGE_ENABLED 0
33
34namespace Gigacage {
35
36const size_t primitiveGigacageMask = 0;
37const size_t jsValueGigacageMask = 0;
38
39enum Kind {
40 Primitive,
41 JSValue,
42 NumberOfKinds
43};
44
45inline void ensureGigacage() { }
46inline void disablePrimitiveGigacage() { }
47inline bool shouldBeEnabled() { return false; }
48
49inline void addPrimitiveDisableCallback(void (*)(void*), void*) { }
50inline void removePrimitiveDisableCallback(void (*)(void*), void*) { }
51
52inline void forbidDisablingPrimitiveGigacage() { }
53
54inline bool isDisablingPrimitiveGigacageForbidden() { return false; }
55inline bool isPrimitiveGigacagePermanentlyEnabled() { return false; }
56inline bool canPrimitiveGigacageBeDisabled() { return true; }
57
58ALWAYS_INLINE const char* name(Kind kind)
59{
60 switch (kind) {
61 case Primitive:
62 return "Primitive";
63 case JSValue:
64 return "JSValue";
65 case NumberOfKinds:
66 break;
67 }
68 RELEASE_ASSERT_NOT_REACHED();
69 return nullptr;
70}
71
72ALWAYS_INLINE bool contains(const void*) { return false; }
73ALWAYS_INLINE bool isEnabled(Kind) { return false; }
74ALWAYS_INLINE size_t mask(Kind) { return 0; }
75
76template<typename T>
77inline T* caged(Kind, T* ptr) { return ptr; }
78template<typename T>
79inline T* cagedMayBeNull(Kind, T* ptr) { return ptr; }
80
81inline bool isCaged(Kind, const void*) { return false; }
82
83inline void* tryAlignedMalloc(Kind, size_t alignment, size_t size) { return tryFastAlignedMalloc(alignment, size); }
84inline void alignedFree(Kind, void* p) { fastAlignedFree(p); }
85WTF_EXPORT_PRIVATE void* tryMalloc(Kind, size_t size);
86WTF_EXPORT_PRIVATE void* tryRealloc(Kind, void*, size_t);
87inline void free(Kind, void* p) { fastFree(p); }
88
89WTF_EXPORT_PRIVATE void* tryAllocateZeroedVirtualPages(Kind, size_t size);
90WTF_EXPORT_PRIVATE void freeVirtualPages(Kind, void* basePtr, size_t size);
91
92} // namespace Gigacage
93#else
94#include <bmalloc/Gigacage.h>
95
96namespace Gigacage {
97
98WTF_EXPORT_PRIVATE void* tryAlignedMalloc(Kind, size_t alignment, size_t size);
99WTF_EXPORT_PRIVATE void alignedFree(Kind, void*);
100WTF_EXPORT_PRIVATE void* tryMalloc(Kind, size_t);
101WTF_EXPORT_PRIVATE void* tryRealloc(Kind, void*, size_t);
102WTF_EXPORT_PRIVATE void free(Kind, void*);
103
104WTF_EXPORT_PRIVATE void* tryAllocateZeroedVirtualPages(Kind, size_t size);
105WTF_EXPORT_PRIVATE void freeVirtualPages(Kind, void* basePtr, size_t size);
106
107} // namespace Gigacage
108#endif
109
110namespace Gigacage {
111
112WTF_EXPORT_PRIVATE void* tryMallocArray(Kind, size_t numElements, size_t elementSize);
113
114WTF_EXPORT_PRIVATE void* malloc(Kind, size_t);
115WTF_EXPORT_PRIVATE void* mallocArray(Kind, size_t numElements, size_t elementSize);
116
117} // namespace Gigacage
118
119
120