1/*
2 * Copyright (C) 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 "OptionsList.h"
29#include <wtf/StdLibExtras.h>
30
31namespace JSC {
32
33class ExecutableAllocator;
34class FixedVMPoolExecutableAllocator;
35
36#if CPU(ARM64) || PLATFORM(WATCHOS)
37constexpr size_t PageSize = 16 * KB;
38#else
39constexpr size_t PageSize = 4 * KB;
40#endif
41
42constexpr size_t ConfigSizeToProtect = PageSize;
43
44#if ENABLE(SEPARATED_WX_HEAP)
45using JITWriteSeparateHeapsFunction = void (*)(off_t, const void*, size_t);
46#endif
47
48struct Config {
49 JS_EXPORT_PRIVATE static void disableFreezingForTesting();
50 JS_EXPORT_PRIVATE static void enableRestrictedOptions();
51 JS_EXPORT_PRIVATE static void permanentlyFreeze();
52
53 static void configureForTesting()
54 {
55 disableFreezingForTesting();
56 enableRestrictedOptions();
57 }
58
59 union {
60 struct {
61 // All the fields in this struct should be chosen such that their
62 // initial value is 0 / null / falsy because Config is instantiated
63 // as a global singleton.
64
65 bool isPermanentlyFrozen;
66 bool disabledFreezingForTesting;
67 bool restrictedOptionsEnabled;
68 bool jitDisabled;
69
70 // The following HasBeenCalled flags are for auditing call_once initialization functions.
71 bool initializeThreadingHasBeenCalled;
72
73 ExecutableAllocator* executableAllocator;
74 FixedVMPoolExecutableAllocator* fixedVMPoolExecutableAllocator;
75 void* startExecutableMemory;
76 void* endExecutableMemory;
77 uintptr_t startOfFixedWritableMemoryPool;
78
79#if ENABLE(SEPARATED_WX_HEAP)
80 JITWriteSeparateHeapsFunction jitWriteSeparateHeaps;
81 bool useFastPermisionsJITCopy;
82#endif
83
84 OptionsStorage options;
85 };
86 char ensureSize[ConfigSizeToProtect];
87 };
88};
89
90extern "C" alignas(PageSize) JS_EXPORT_PRIVATE Config g_jscConfig;
91
92static_assert(sizeof(Config) == ConfigSizeToProtect, "");
93static_assert(roundUpToMultipleOf<PageSize>(ConfigSizeToProtect) == ConfigSizeToProtect, "");
94
95} // namespace JSC
96