| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef jit_AutoWritableJitCode_h |
| 8 |
#define jit_AutoWritableJitCode_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" |
| 11 |
#include "mozilla/Attributes.h" |
| 12 |
#include "mozilla/ScopeExit.h" |
| 13 |
#include "mozilla/TimeStamp.h" |
| 14 |
|
| 15 |
#include <stddef.h> |
| 16 |
|
| 17 |
#include "jit/ExecutableAllocator.h" |
| 18 |
#include "jit/JitCode.h" |
| 19 |
#include "jit/JitOptions.h" |
| 20 |
#include "jit/ProcessExecutableMemory.h" |
| 21 |
#include "vm/JSContext.h" |
| 22 |
#include "vm/Realm.h" |
| 23 |
#include "vm/Runtime.h" |
| 24 |
|
| 25 |
namespace js::jit { |
| 26 |
|
| 27 |
// This class ensures JIT code is executable on its destruction. Creators |
| 28 |
// must call makeWritable(), and not attempt to write to the buffer if it fails. |
| 29 |
// |
| 30 |
// AutoWritableJitCodeFallible may only fail to make code writable; it cannot |
| 31 |
// fail to make JIT code executable (because the creating code has no chance to |
| 32 |
// recover from a failed destructor). |
| 33 |
class MOZ_RAII AutoWritableJitCodeFallible { |
| 34 |
JSRuntime* rt_; |
| 35 |
void* addr_; |
| 36 |
size_t size_; |
| 37 |
AutoMarkJitCodeWritableForThread writableForThread_; |
| 38 |
|
| 39 |
public: |
| 40 |
explicit AutoWritableJitCodeFallible(JitCode* code) |
| 41 |
: rt_(code->runtimeFromMainThread()), |
| 42 |
addr_(code->allocatedMemory()), |
| 43 |
size_(code->allocatedSize()) { |
| 44 |
rt_->toggleAutoWritableJitCodeActive(true); |
| 45 |
} |
| 46 |
|
| 47 |
[[nodiscard]] bool makeWritable() { |
| 48 |
return ExecutableAllocator::makeWritable(addr_, size_); |
| 49 |
} |
| 50 |
|
| 51 |
~AutoWritableJitCodeFallible() { |
| 52 |
// Taking TimeStamps frequently can be expensive, and there's no point |
| 53 |
// measuring this if write protection is disabled. |
| 54 |
const bool measuringTime = JitOptions.writeProtectCode; |
| 55 |
const mozilla::TimeStamp startTime = |
| 56 |
measuringTime ? mozilla::TimeStamp::Now() : mozilla::TimeStamp(); |
| 57 |
auto timer = mozilla::MakeScopeExit([&] { |
| 58 |
if (measuringTime) { |
| 59 |
if (Realm* realm = rt_->mainContextFromOwnThread()->realm()) { |
| 60 |
realm->timers.protectTime += mozilla::TimeStamp::Now() - startTime; |
| 61 |
} |
| 62 |
} |
| 63 |
}); |
| 64 |
|
| 65 |
if (!ExecutableAllocator::makeExecutableAndFlushICache(addr_, size_)) { |
| 66 |
MOZ_CRASH(); |
| 67 |
} |
| 68 |
rt_->toggleAutoWritableJitCodeActive(false); |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
// Infallible variant of AutoWritableJitCodeFallible, ensures writable during |
| 73 |
// construction |
| 74 |
class MOZ_RAII AutoWritableJitCode : private AutoWritableJitCodeFallible { |
| 75 |
public: |
| 76 |
explicit AutoWritableJitCode(JitCode* code) |
| 77 |
: AutoWritableJitCodeFallible(code) { |
| 78 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 79 |
if (!makeWritable()) { |
| 80 |
oomUnsafe.crash("Failed to mmap. Likely no mappings available."); |
| 81 |
} |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
} // namespace js::jit |
| 86 |
|
| 87 |
#endif /* jit_AutoWritableJitCode_h */ |