Merge pull request #9333 from electron/process-memory-info-sandbox
Expose `process.get{System,Process}MemoryInfo` to sandbox
This commit is contained in:
commit
70e199e255
7 changed files with 120 additions and 45 deletions
|
@ -23,51 +23,6 @@ namespace {
|
|||
// Dummy class type that used for crashing the program.
|
||||
struct DummyClass { bool crash; };
|
||||
|
||||
void Hang() {
|
||||
for (;;)
|
||||
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetProcessMemoryInfo(v8::Isolate* isolate) {
|
||||
std::unique_ptr<base::ProcessMetrics> metrics(
|
||||
base::ProcessMetrics::CreateCurrentProcessMetrics());
|
||||
|
||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||
dict.Set("workingSetSize",
|
||||
static_cast<double>(metrics->GetWorkingSetSize() >> 10));
|
||||
dict.Set("peakWorkingSetSize",
|
||||
static_cast<double>(metrics->GetPeakWorkingSetSize() >> 10));
|
||||
|
||||
size_t private_bytes, shared_bytes;
|
||||
if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
|
||||
dict.Set("privateBytes", static_cast<double>(private_bytes >> 10));
|
||||
dict.Set("sharedBytes", static_cast<double>(shared_bytes >> 10));
|
||||
}
|
||||
|
||||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetSystemMemoryInfo(v8::Isolate* isolate,
|
||||
mate::Arguments* args) {
|
||||
base::SystemMemoryInfoKB mem_info;
|
||||
if (!base::GetSystemMemoryInfo(&mem_info)) {
|
||||
args->ThrowError("Unable to retrieve system memory information");
|
||||
return v8::Undefined(isolate);
|
||||
}
|
||||
|
||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||
dict.Set("total", mem_info.total);
|
||||
dict.Set("free", mem_info.free);
|
||||
|
||||
// NB: These return bogus values on macOS
|
||||
#if !defined(OS_MACOSX)
|
||||
dict.Set("swapTotal", mem_info.swap_total);
|
||||
dict.Set("swapFree", mem_info.swap_free);
|
||||
#endif
|
||||
|
||||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
// Called when there is a fatal error in V8, we just crash the process here so
|
||||
// we can get the stack trace.
|
||||
void FatalErrorCallback(const char* location, const char* message) {
|
||||
|
@ -168,4 +123,52 @@ void AtomBindings::Crash() {
|
|||
static_cast<DummyClass*>(nullptr)->crash = true;
|
||||
}
|
||||
|
||||
// static
|
||||
void AtomBindings::Hang() {
|
||||
for (;;)
|
||||
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
|
||||
}
|
||||
|
||||
// static
|
||||
v8::Local<v8::Value> AtomBindings::GetProcessMemoryInfo(v8::Isolate* isolate) {
|
||||
std::unique_ptr<base::ProcessMetrics> metrics(
|
||||
base::ProcessMetrics::CreateCurrentProcessMetrics());
|
||||
|
||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||
dict.Set("workingSetSize",
|
||||
static_cast<double>(metrics->GetWorkingSetSize() >> 10));
|
||||
dict.Set("peakWorkingSetSize",
|
||||
static_cast<double>(metrics->GetPeakWorkingSetSize() >> 10));
|
||||
|
||||
size_t private_bytes, shared_bytes;
|
||||
if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
|
||||
dict.Set("privateBytes", static_cast<double>(private_bytes >> 10));
|
||||
dict.Set("sharedBytes", static_cast<double>(shared_bytes >> 10));
|
||||
}
|
||||
|
||||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
// static
|
||||
v8::Local<v8::Value> AtomBindings::GetSystemMemoryInfo(v8::Isolate* isolate,
|
||||
mate::Arguments* args) {
|
||||
base::SystemMemoryInfoKB mem_info;
|
||||
if (!base::GetSystemMemoryInfo(&mem_info)) {
|
||||
args->ThrowError("Unable to retrieve system memory information");
|
||||
return v8::Undefined(isolate);
|
||||
}
|
||||
|
||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||
dict.Set("total", mem_info.total);
|
||||
dict.Set("free", mem_info.free);
|
||||
|
||||
// NB: These return bogus values on macOS
|
||||
#if !defined(OS_MACOSX)
|
||||
dict.Set("swapTotal", mem_info.swap_total);
|
||||
dict.Set("swapFree", mem_info.swap_free);
|
||||
#endif
|
||||
|
||||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
} // namespace atom
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include "base/macros.h"
|
||||
#include "base/strings/string16.h"
|
||||
#include "native_mate/arguments.h"
|
||||
#include "v8/include/v8.h"
|
||||
#include "vendor/node/deps/uv/include/uv.h"
|
||||
|
||||
|
@ -32,6 +33,10 @@ class AtomBindings {
|
|||
|
||||
static void Log(const base::string16& message);
|
||||
static void Crash();
|
||||
static void Hang();
|
||||
static v8::Local<v8::Value> GetProcessMemoryInfo(v8::Isolate* isolate);
|
||||
static v8::Local<v8::Value> GetSystemMemoryInfo(v8::Isolate* isolate,
|
||||
mate::Arguments* args);
|
||||
|
||||
private:
|
||||
void ActivateUVLoop(v8::Isolate* isolate);
|
||||
|
|
|
@ -86,6 +86,9 @@ void InitializeBindings(v8::Local<v8::Object> binding,
|
|||
mate::Dictionary b(isolate, binding);
|
||||
b.SetMethod("get", GetBinding);
|
||||
b.SetMethod("crash", AtomBindings::Crash);
|
||||
b.SetMethod("hang", AtomBindings::Hang);
|
||||
b.SetMethod("getProcessMemoryInfo", &AtomBindings::GetProcessMemoryInfo);
|
||||
b.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo);
|
||||
}
|
||||
|
||||
class AtomSandboxedRenderViewObserver : public AtomRenderViewObserver {
|
||||
|
|
|
@ -38,6 +38,9 @@ const preloadSrc = fs.readFileSync(preloadPath).toString()
|
|||
// access to things like `process.atomBinding`).
|
||||
const preloadProcess = new events.EventEmitter()
|
||||
preloadProcess.crash = () => binding.crash()
|
||||
preloadProcess.hang = () => binding.hang()
|
||||
preloadProcess.getProcessMemoryInfo = () => binding.getProcessMemoryInfo()
|
||||
preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo()
|
||||
process.platform = preloadProcess.platform = electron.remote.process.platform
|
||||
process.execPath = preloadProcess.execPath = electron.remote.process.execPath
|
||||
process.on('exit', () => preloadProcess.emit('exit'))
|
||||
|
|
|
@ -1150,6 +1150,29 @@ describe('BrowserWindow module', function () {
|
|||
})
|
||||
w.loadURL('file://' + path.join(fixtures, 'pages', 'window-open.html'))
|
||||
})
|
||||
|
||||
it('releases memory after popup is closed', (done) => {
|
||||
w.destroy()
|
||||
w = new BrowserWindow({
|
||||
show: false,
|
||||
webPreferences: {
|
||||
preload: preload,
|
||||
sandbox: true
|
||||
}
|
||||
})
|
||||
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?allocate-memory'))
|
||||
w.webContents.openDevTools({mode: 'detach'})
|
||||
ipcMain.once('answer', function (event, {bytesBeforeOpen, bytesAfterOpen, bytesAfterClose}) {
|
||||
const memoryIncreaseByOpen = bytesAfterOpen - bytesBeforeOpen
|
||||
const memoryDecreaseByClose = bytesAfterOpen - bytesAfterClose
|
||||
// decreased memory should be less than increased due to factors we
|
||||
// can't control, but given the amount of memory allocated in the
|
||||
// fixture, we can reasonably expect decrease to be at least 70% of
|
||||
// increase
|
||||
assert(memoryDecreaseByClose > memoryIncreaseByOpen * 0.7)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
|
11
spec/fixtures/api/allocate-memory.html
vendored
Normal file
11
spec/fixtures/api/allocate-memory.html
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
window.bigBuffer = new Uint8Array(1024 * 1024 * 64)
|
||||
window.bigBuffer.fill(5, 50, 1024 * 1024)
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
27
spec/fixtures/api/sandbox.html
vendored
27
spec/fixtures/api/sandbox.html
vendored
|
@ -1,5 +1,18 @@
|
|||
<html>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
function timeout(ms) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms)
|
||||
})
|
||||
}
|
||||
async function invokeGc () {
|
||||
// it seems calling window.gc once does not guarantee garbage will be
|
||||
// collected, so we repeat 10 times with interval of 100 ms
|
||||
for (let i = 0; i < 10; i++) {
|
||||
window.gc()
|
||||
await timeout(100)
|
||||
}
|
||||
}
|
||||
if (window.opener) {
|
||||
window.callback = () => {
|
||||
opener.require('electron').ipcRenderer.send('answer', document.body.innerHTML)
|
||||
|
@ -7,6 +20,20 @@
|
|||
} else {
|
||||
const {ipcRenderer} = require('electron')
|
||||
const tests = {
|
||||
'allocate-memory': async () => {
|
||||
await invokeGc()
|
||||
const {privateBytes: bytesBeforeOpen} = process.getProcessMemoryInfo()
|
||||
let w = open('./allocate-memory.html')
|
||||
await invokeGc()
|
||||
const {privateBytes: bytesAfterOpen} = process.getProcessMemoryInfo()
|
||||
w.close()
|
||||
w = null
|
||||
await invokeGc()
|
||||
const {privateBytes: bytesAfterClose} = process.getProcessMemoryInfo()
|
||||
ipcRenderer.send('answer', {
|
||||
bytesBeforeOpen, bytesAfterOpen, bytesAfterClose
|
||||
})
|
||||
},
|
||||
'window-events': () => {
|
||||
document.title = 'changed'
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue