0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-25 15:29:43 -05:00

Fix StartupData memory leak (#131)

This commit is contained in:
Ry Dahl 2019-12-25 08:14:55 -05:00 committed by GitHub
parent 934dd16e89
commit ce1f74221c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 14 deletions

View file

@ -1,7 +1,5 @@
# Rusty V8 Binding
Status: Early development, not usable.
V8 Version: 8.0.427
[![ci](https://github.com/denoland/rusty_v8/workflows/ci/badge.svg?branch=master)](https://github.com/denoland/rusty_v8/actions)

View file

@ -666,6 +666,8 @@ void v8__SnapshotCreator__DESTRUCT(v8::SnapshotCreator& self) {
self.~SnapshotCreator();
}
void v8__StartupData__DESTRUCT(v8::StartupData& self) { delete[] self.data; }
v8::Isolate* v8__SnapshotCreator__GetIsolate(v8::SnapshotCreator& self) {
return self.GetIsolate();
}

View file

@ -3,6 +3,8 @@ use crate::Context;
use crate::Isolate;
use crate::Local;
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::ops::DerefMut;
extern "C" {
fn v8__SnapshotCreator__CONSTRUCT(buf: &mut MaybeUninit<SnapshotCreator>);
@ -18,13 +20,33 @@ extern "C" {
this: &mut SnapshotCreator,
context: *mut Context,
);
fn v8__StartupData__DESTRUCT(this: &mut StartupData);
}
#[derive(Debug)]
#[repr(C)]
pub struct StartupData {
pub data: *const u8,
pub raw_size: int,
data: *mut u8,
raw_size: int,
}
impl Deref for StartupData {
type Target = [u8];
fn deref(&self) -> &Self::Target {
unsafe { std::slice::from_raw_parts(self.data, self.raw_size as usize) }
}
}
impl DerefMut for StartupData {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { std::slice::from_raw_parts_mut(self.data, self.raw_size as usize) }
}
}
impl Drop for StartupData {
fn drop(&mut self) {
unsafe { v8__StartupData__DESTRUCT(self) }
}
}
#[repr(C)]
@ -66,14 +88,19 @@ impl SnapshotCreator {
/// Creates a snapshot data blob.
/// This must not be called from within a handle scope.
///
/// Returns { nullptr, 0 } on failure, and a startup snapshot on success.
/// The caller acquires ownership of the data array in the return value.
pub fn create_blob(
&mut self,
function_code_handling: FunctionCodeHandling,
) -> StartupData {
unsafe { v8__SnapshotCreator__CreateBlob(self, function_code_handling) }
) -> Option<StartupData> {
let blob =
unsafe { v8__SnapshotCreator__CreateBlob(self, function_code_handling) };
if blob.data.is_null() {
debug_assert!(blob.raw_size == 0);
None
} else {
debug_assert!(blob.raw_size > 0);
Some(blob)
}
}
/// Returns the isolate prepared by the snapshot creator.

View file

@ -1234,9 +1234,11 @@ fn snapshot_creator() {
context.exit();
}
snapshot_creator.create_blob(v8::FunctionCodeHandling::Clear)
snapshot_creator
.create_blob(v8::FunctionCodeHandling::Clear)
.unwrap()
};
assert!(startup_data.raw_size > 0);
assert!(startup_data.len() > 0);
// Now we try to load up the snapshot and check that 'a' has the correct
// value.
{
@ -1259,8 +1261,5 @@ fn snapshot_creator() {
context.exit();
}
}
// TODO(ry) startup_data is getting leaked and is not cleaned up properly!
// It must be freed using c++ delete.
drop(g);
}