From 6257b4044a181fcaa462aba6c361e3c8fdcad2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 22 Oct 2019 16:49:58 +0200 Subject: core: gracefully handle bad op id (#3131) --- core/isolate.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'core/isolate.rs') diff --git a/core/isolate.rs b/core/isolate.rs index ede07b1c5..8b15befa3 100644 --- a/core/isolate.rs +++ b/core/isolate.rs @@ -311,12 +311,19 @@ impl Isolate { ) { let isolate = unsafe { Isolate::from_raw_ptr(user_data) }; - let op = isolate.op_registry.call( + let maybe_op = isolate.op_registry.call( op_id, control_buf.as_ref(), PinnedBuf::new(zero_copy_buf), ); + let op = match maybe_op { + Some(op) => op, + None => { + return isolate.throw_exception(&format!("Unknown op id: {}", op_id)) + } + }; + // To avoid latency problems we eagerly poll 50 futures and then // allow to poll ops from `pending_ops` let op = if isolate.eager_poll_count != 50 { @@ -414,6 +421,13 @@ impl Isolate { } } + fn throw_exception(&mut self, exception_text: &str) { + let text = CString::new(exception_text).unwrap(); + unsafe { + libdeno::deno_throw_exception(self.libdeno_isolate, text.as_ptr()) + } + } + fn respond( &mut self, maybe_buf: Option<(OpId, &[u8])>, @@ -1418,6 +1432,26 @@ pub mod tests { }); } + #[test] + fn test_pre_dispatch() { + run_in_task(|| { + let (mut isolate, _dispatch_count) = setup(Mode::OverflowResAsync); + js_check(isolate.execute( + "bad_op_id.js", + r#" + let thrown; + try { + Deno.core.dispatch(100, []); + } catch (e) { + thrown = e; + } + assert(thrown == "Unknown op id: 100"); + "#, + )); + assert_eq!(Async::Ready(()), isolate.poll().unwrap()); + }); + } + #[test] fn test_js() { run_in_task(|| { -- cgit v1.2.3