Skip to content

Commit c9509bc

Browse files
authored
Fix isolate ptr memory leak (denoland#872)
1 parent 01dd95b commit c9509bc

File tree

3 files changed

+4
-26
lines changed

3 files changed

+4
-26
lines changed

core/runtime/jsrealm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub struct ContextState {
6767
// We don't explicitly re-read this prop but need the slice to live alongside
6868
// the context
6969
pub(crate) op_ctxs: Box<[OpCtx]>,
70-
pub(crate) isolate: Option<*mut v8::OwnedIsolate>,
70+
pub(crate) isolate: Option<*mut v8::Isolate>,
7171
pub(crate) exception_state: Rc<ExceptionState>,
7272
pub(crate) has_next_tick_scheduled: Cell<bool>,
7373
pub(crate) get_error_class_fn: GetErrorClassFn,
@@ -77,7 +77,7 @@ pub struct ContextState {
7777
impl ContextState {
7878
pub(crate) fn new(
7979
op_driver: Rc<OpDriverImpl>,
80-
isolate_ptr: *mut v8::OwnedIsolate,
80+
isolate_ptr: *mut v8::Isolate,
8181
get_error_class_fn: GetErrorClassFn,
8282
op_ctxs: Box<[OpCtx]>,
8383
external_ops_tracker: ExternalOpsTracker,

core/runtime/jsruntime.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -934,20 +934,12 @@ impl JsRuntime {
934934
);
935935
}
936936

937+
let isolate_ptr = isolate.as_mut() as *mut Isolate;
937938
// ...isolate is fully set up, we can forward its pointer to the ops to finish
938939
// their' setup...
939940
for op_ctx in op_ctxs.iter_mut() {
940-
op_ctx.isolate = isolate.as_mut() as *mut Isolate;
941+
op_ctx.isolate = isolate_ptr;
941942
}
942-
943-
// TODO(Bartlomieju): this can be simplified
944-
let isolate_ptr = setup::create_isolate_ptr();
945-
// SAFETY: this is first use of `isolate_ptr` so we are sure we're
946-
// not overwriting an existing pointer.
947-
isolate = unsafe {
948-
isolate_ptr.write(isolate);
949-
isolate_ptr.read()
950-
};
951943
op_state.borrow_mut().put(isolate_ptr);
952944

953945
let mut fast_fn_infos = Vec::with_capacity(op_ctxs.len());

core/runtime/setup.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,6 @@ fn create_cpp_heap() -> v8::UniqueRef<v8::cppgc::Heap> {
118118
)
119119
}
120120

121-
pub fn create_isolate_ptr() -> *mut v8::OwnedIsolate {
122-
let align = std::mem::align_of::<usize>();
123-
let layout = std::alloc::Layout::from_size_align(
124-
std::mem::size_of::<*mut v8::OwnedIsolate>(),
125-
align,
126-
)
127-
.unwrap();
128-
assert!(layout.size() > 0);
129-
let isolate_ptr: *mut v8::OwnedIsolate =
130-
// SAFETY: we just asserted that layout has non-0 size.
131-
unsafe { std::alloc::alloc(layout) as *mut _ };
132-
isolate_ptr
133-
}
134-
135121
pub fn create_isolate(
136122
will_snapshot: bool,
137123
maybe_create_params: Option<v8::CreateParams>,

0 commit comments

Comments
 (0)