summaryrefslogtreecommitdiff
path: root/src/mongo/scripting/mozjs/jscustomallocator.cpp
blob: 28afb60fa889700a74e06df5f48216eada41c54f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include <cstddef>
#include <js/Utility.h>
#include <jscustomallocator.h>
#include <type_traits>

#include "mongo/config.h"
#include "mongo/scripting/mozjs/implscope.h"

#ifdef __linux__
#include <malloc.h>
#elif defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(_WIN32)
#include <malloc.h>
#elif defined(__FreeBSD__)
#include <malloc_np.h>
#else
#define MONGO_NO_MALLOC_USABLE_SIZE
#endif

#if !defined(__has_feature)
#define __has_feature(x) 0
#endif

/**
 * This shim interface (which controls dynamic allocation within SpiderMonkey),
 * consciously uses std::malloc and friends over mongoMalloc. It does this
 * because SpiderMonkey has some plausible options in the event of OOM,
 * specifically it can begin aggressive garbage collection. It would also be
 * reasonable to go the other route and fail, but for the moment I erred on the
 * side of maintaining the contract that SpiderMonkey expects.
 *
 * The overall strategy here is to keep track of allocations in a thread local,
 * offering us the chance to enforce soft limits on memory use rather than
 * waiting for the OS to OOM us.
 */

namespace mongo {
namespace sm {

namespace {
/**
 * These two variables track the total number of bytes handed out, and the
 * maximum number of bytes we will consider handing out. They are set by
 * MozJSImplScope on start up.
 */
thread_local size_t total_bytes = 0;
thread_local size_t max_bytes = 0;

/**
 * When we don't have malloc_usable_size, we manage by adjusting our pointer by
 * kMaxAlign bytes and storing the size of the allocation kMaxAlign bytes
 * behind the pointer we hand back. That let's us get to the value at runtime.
 * We know kMaxAlign is enough (generally 8 or 16 bytes), because that's
 * literally the contract between malloc and std::max_align_t.
 *
 * This is commented out right now because std::max_align_t didn't seem to be
 * available on our solaris builder. TODO: revisit in the future to see if that
 * still holds.
 */
// const size_t kMaxAlign = std::alignment_of<std::max_align_t>::value;
const size_t kMaxAlign = 16;
}  // namespace

size_t get_total_bytes() {
    return total_bytes;
}

void reset(size_t bytes) {
    total_bytes = 0;
    max_bytes = bytes;
}

size_t get_max_bytes() {
    return max_bytes;
}

/**
 * Wraps std::Xalloc functions
 *
 * The idea here is to abstract soft limits on allocations, as well as possibly
 * necessary pointer adjustment (if we don't have a malloc_usable_size
 * replacement).
 *
 */
template <typename T>
void* wrap_alloc(T&& func, void* ptr, size_t bytes) {
    size_t mb = get_max_bytes();
    size_t tb = get_total_bytes();

    if (mb && (tb + bytes > mb)) {
        auto scope = mongo::mozjs::MozJSImplScope::getThreadScope();
        if (scope)
            scope->setOOM();

        // We fall through here because we want to let spidermonkey continue
        // with whatever it was doing.  Calling setOOM will fail the top level
        // operation as soon as possible.
    }

#ifdef MONGO_NO_MALLOC_USABLE_SIZE
    ptr = ptr ? static_cast<char*>(ptr) - kMaxAlign : nullptr;
#endif

#ifdef MONGO_NO_MALLOC_USABLE_SIZE
    void* p = func(ptr, bytes + kMaxAlign);
#else
    void* p = func(ptr, bytes);
#endif

#if __has_feature(address_sanitizer)
    {
        auto handles = mongo::mozjs::MozJSImplScope::ASANHandles::getThreadASANHandles();

        if (handles) {
            if (bytes) {
                if (ptr) {
                    // realloc
                    if (ptr != p) {
                        // actually moved the allocation
                        handles->removePointer(ptr);
                        handles->addPointer(p);
                    }
                    // else we didn't need to realloc, don't have to register
                } else {
                    // malloc/calloc
                    handles->addPointer(p);
                }
            } else {
                // free
                handles->removePointer(ptr);
            }
        }
    }
#endif

    if (!p) {
        return nullptr;
    }

#ifdef MONGO_NO_MALLOC_USABLE_SIZE
    *reinterpret_cast<size_t*>(p) = bytes;
    p = static_cast<char*>(p) + kMaxAlign;
#endif

    total_bytes = tb + bytes;

    return p;
}

size_t get_current(void* ptr) {
#ifdef MONGO_NO_MALLOC_USABLE_SIZE
    if (!ptr)
        return 0;

    return *reinterpret_cast<size_t*>(static_cast<char*>(ptr) - kMaxAlign);
#elif defined(__linux__) || defined(__FreeBSD__)
    return malloc_usable_size(ptr);
#elif defined(__APPLE__)
    return malloc_size(ptr);
#elif defined(_WIN32)
    return _msize(ptr);
#else
#error "Should be unreachable"
#endif
}

}  // namespace sm
}  // namespace mongo

JS_PUBLIC_DATA arena_id_t js::MallocArena;
JS_PUBLIC_DATA arena_id_t js::ArrayBufferContentsArena;
JS_PUBLIC_DATA arena_id_t js::StringBufferArena;

void* mongo_arena_malloc(arena_id_t arena, size_t bytes) {
    return std::malloc(bytes);
}

void* mongo_arena_calloc(arena_id_t arena, size_t nmemb, size_t size) {
    return std::calloc(nmemb, size);
}

void* mongo_arena_realloc(arena_id_t arena, void* p, size_t bytes) {
    if (!p) {
        return mongo_arena_malloc(arena, bytes);
    }

    if (!bytes) {
        js_free(p);
        return nullptr;
    }

    size_t current = mongo::sm::get_current(p);

    if (current >= bytes) {
        return p;
    }

    size_t tb = mongo::sm::total_bytes;

    if (tb >= current) {
        mongo::sm::total_bytes = tb - current;
    }

    return std::realloc(p, bytes);
}

void* js_arena_malloc(size_t arena, size_t bytes) {
    JS_OOM_POSSIBLY_FAIL();
    JS_CHECK_LARGE_ALLOC(bytes);
    return mongo::sm::wrap_alloc(
        [&](void* ptr, size_t b) { return mongo_arena_malloc(arena, bytes); }, nullptr, bytes);
}

void* js_malloc(size_t bytes) {
    return js_arena_malloc(js::MallocArena, bytes);
}

void* js_arena_calloc(arena_id_t arena, size_t bytes) {
    JS_OOM_POSSIBLY_FAIL();
    JS_CHECK_LARGE_ALLOC(bytes);
    return mongo::sm::wrap_alloc(
        [&](void* ptr, size_t b) { return mongo_arena_calloc(arena, 1, b); }, nullptr, bytes);
}

void* js_arena_calloc(arena_id_t arena, size_t nmemb, size_t size) {
    JS_OOM_POSSIBLY_FAIL();
    JS_CHECK_LARGE_ALLOC(size);
    return mongo::sm::wrap_alloc(
        [&](void* ptr, size_t b) { return mongo_arena_calloc(arena, nmemb, size); },
        nullptr,
        size * nmemb);
}

void* js_calloc(size_t bytes) {
    return js_arena_calloc(js::MallocArena, bytes);
}

void* js_calloc(size_t nmemb, size_t size) {
    return js_arena_calloc(js::MallocArena, nmemb, size);
}

void* js_arena_realloc(arena_id_t arena, void* p, size_t bytes) {
    // realloc() with zero size is not portable, as some implementations may
    // return nullptr on success and free |p| for this.  We assume nullptr
    // indicates failure and that |p| is still valid.
    MOZ_ASSERT(bytes != 0);

    JS_OOM_POSSIBLY_FAIL();
    JS_CHECK_LARGE_ALLOC(bytes);
    return mongo::sm::wrap_alloc(
        [&](void* ptr, size_t b) { return mongo_arena_realloc(arena, ptr, b); }, p, bytes);
}

void* js_realloc(void* p, size_t bytes) {
    return js_arena_realloc(js::MallocArena, p, bytes);
}

void js_free(void* p) {
    if (!p)
        return;

    size_t current = mongo::sm::get_current(p);
    size_t tb = mongo::sm::get_total_bytes();

    if (tb >= current) {
        mongo::sm::total_bytes = tb - current;
    }

    mongo::sm::wrap_alloc(
        [](void* ptr, size_t b) {
            std::free(ptr);
            return nullptr;
        },
        p,
        0);
}

void js::InitMallocAllocator() {
    MallocArena = 0;
    ArrayBufferContentsArena = 1;
    StringBufferArena = 2;
}

void js::ShutDownMallocAllocator() {}