From 5f4678ca6a7244d52eefa3344aa730d9b2c95364 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 20 Jun 2022 03:05:16 +0800 Subject: [PATCH] fix concurrency issue in predictor/redraw loop So there's a bit of a nasty bug that arises from the GAM both handling drawing primitives and context swaps. In particular, because the GAM handles both of these in a single thread, context swaps can't block, because the redraws triggered by the context swap would be blocked, causing a deadlock. When the API token for the predictor was introduced, this added a blocking operation inside the context swap. This would cause a deadlock under certain conditions. This patch refactors the API token so that it's not a return result of swapping the predictors. Instead, the predictors return the API token as a non-blocking message to the GAM. This API token is correlated with the *GAM* token (not the app token). Present me thanks past me for adding the GAM token, which was as of yet unused, but now, there is a very sensible role for it-- exactly what it was made for, e.g. handling GAM-local API calls without disclosing the application token. Anyways, hopefully this patch puts the race condition to rest, while allowing us to have per-context prediction histories. --- services/gam/src/api.rs | 2 ++ services/gam/src/contexts.rs | 27 +++++++++-------- services/gam/src/lib.rs | 11 ++++++- services/gam/src/main.rs | 6 ++++ services/ime-frontend/src/main.rs | 47 ++++++++++++++++-------------- services/ime-plugin-api/src/lib.rs | 20 ++++++------- 6 files changed, 68 insertions(+), 45 deletions(-) diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index c3eeebe4a..0fc8ea445 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -156,6 +156,8 @@ pub(crate) enum Opcode { // used to claim a GAM registration token (should be used only by status.rs) ClaimToken, + // used to set a predictor API token (should be used only by ime-frontend) + PredictorApiToken, /// system-level API that can be called by the Xous process launcher to check if we're at a state where less trusted code could be run /// it basically checks that all tokens have been claimed by trusted OS procesess, thus blocking any further token creation diff --git a/services/gam/src/contexts.rs b/services/gam/src/contexts.rs index e85f9de54..8172e52ab 100644 --- a/services/gam/src/contexts.rs +++ b/services/gam/src/contexts.rs @@ -56,7 +56,7 @@ pub(crate) struct UxContext { /// a putative human-readable name given to the context. The name itself is stored in the TokenManager, not in this struct. /// Passed to the TokenManager to compute a trust level; add the app's name to tokens.rs EXPECTED_BOOT_CONTEXTS if you want this to succeed. pub app_token: [u32; 4], // shared with the app, can be used for other auths to other servers (e.g. audio codec) - /// a token associated with the UxContext, but private to the GAM (not shared with the app). [currently no use for this, just seems like a good idea...] + /// a token associated with the UxContext, but private to the GAM (not shared with the app). (used by predictor to set API tokens) pub gam_token: [u32; 4], /// set to true if keyboard vibrate is turned on pub vibe: bool, @@ -388,8 +388,8 @@ impl ContextManager { { // now re-check-out the new context and finalize things let maybe_new_focus = self.get_context_by_token(token); - let pred_api_token = if let Some(context) = maybe_new_focus { - let pred_token = if context.predictor.is_some() { + if let Some(context) = maybe_new_focus { + if context.predictor.is_some() { // only hook up the IMEF if a predictor is selected for this context let descriptor = ImefDescriptor { input_canvas: @@ -412,20 +412,15 @@ impl ContextManager { token: context.gam_token, predictor_token: context.pred_token, }; - let pred_api_token = self.imef.connect_backend(descriptor).expect("couldn't connect IMEF to the current app"); + log::debug!("context gam token: {:?}, pred token: {:?}", context.gam_token, context.pred_token); + self.imef.connect_backend(descriptor).expect("couldn't connect IMEF to the current app"); self.imef_active = true; - Some(pred_api_token) } else { self.imef_active = false; - None }; // now recompute the drawability of canvases, based on on-screen visibility and trust state recompute_canvases(canvases); - pred_token - } else { None }; - if let Some(c) = self.get_context_by_token_mut(token) { - c.pred_token = pred_api_token; } } log::trace!("foregrounding new context"); @@ -433,10 +428,10 @@ impl ContextManager { // now re-check-out the new context and finalize things let maybe_new_focus = self.get_context_by_token(token); if let Some(context) = maybe_new_focus { + self.imef.set_menu_mode(context.imef_menu_mode).expect("couldn't set menu mode"); if clear { context.layout.clear(gfx, canvases).expect("can't clear on context activation"); } - // now update the IMEF area, since we're initialized // note: we may need to skip this call if the context does not utilize a predictor... if context.predictor.is_some() { log::debug!("calling IMEF redraw"); @@ -445,7 +440,6 @@ impl ContextManager { // revert the keyboard vibe state self.kbd.set_vibe(context.vibe).expect("couldn't restore keyboard vibe"); - self.imef.set_menu_mode(context.imef_menu_mode).expect("couldn't set menu mode"); log::trace!("raised focus to: {:?}", context); let last_token = context.app_token; @@ -461,6 +455,15 @@ impl ContextManager { } Ok(()) } + pub(crate) fn set_pred_api_token(&mut self, at: ApiToken) { + for context in self.contexts.values_mut() { + if context.gam_token == at.gam_token { + log::debug!("setting {:?} token to {:?}", at.gam_token, at.api_token); + context.pred_token = Some(at.api_token); + break; + } + } + } pub(crate) fn revert_focus(&mut self, gfx: &graphics_server::Gfx, canvases: &mut HashMap, diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 05719a97d..53d50acf0 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -27,7 +27,7 @@ use xous::{send_message, CID, Message}; use xous_ipc::{String, Buffer}; use num_traits::*; -use ime_plugin_api::ImefCallback; +use ime_plugin_api::{ImefCallback, ApiToken}; #[doc = include_str!("../README.md")] @@ -280,6 +280,15 @@ impl Gam { Ok(returned_claim.token) } + pub fn set_predictor_api_token(&self, api_token: [u32; 4], gam_token: [u32; 4]) -> Result<(), xous::Error> { + let at = ApiToken { + gam_token, + api_token, + }; + let buf = Buffer::into_buf(at).or(Err(xous::Error::InternalError))?; + buf.send(self.conn, Opcode::PredictorApiToken.to_u32().unwrap()) + .or(Err(xous::Error::InternalError)).map(|_|()) + } pub fn trusted_init_done(&self) -> Result { let response = send_message(self.conn, Message::new_blocking_scalar(Opcode::TrustedInitDone.to_usize().unwrap(), 0, 0, 0, 0) diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index 8935ab78a..f76377007 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -6,6 +6,7 @@ use api::*; mod canvas; use canvas::*; mod tokens; +use ime_plugin_api::ApiToken; use tokens::*; mod layouts; use layouts::*; @@ -444,6 +445,11 @@ fn wrapped_main() -> ! { tokenclaim.token = context_mgr.claim_token(tokenclaim.name.as_str().unwrap()); buffer.replace(tokenclaim).unwrap(); }, + Some(Opcode::PredictorApiToken) => { + let buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let at = buf.to_original::().unwrap(); + context_mgr.set_pred_api_token(at); + }, Some(Opcode::TrustedInitDone) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, { if context_mgr.allow_untrusted_code() { xous::return_scalar(msg.sender, 1).unwrap(); diff --git a/services/ime-frontend/src/main.rs b/services/ime-frontend/src/main.rs index c207d6bae..158d4b61f 100644 --- a/services/ime-frontend/src/main.rs +++ b/services/ime-frontend/src/main.rs @@ -11,7 +11,7 @@ use log::{error, info}; use graphics_server::{Gid, Line, PixelColor, Point, Rectangle, TextBounds, TextView, DrawStyle}; use gam::GlyphStyle; -use ime_plugin_api::{PredictionTriggers, PredictionPlugin, PredictionApi}; +use ime_plugin_api::{PredictionTriggers, PredictionPlugin, PredictionApi, ApiToken}; use num_traits::{ToPrimitive,FromPrimitive}; use xous_ipc::Buffer; @@ -99,6 +99,13 @@ impl InputTracker { pub fn set_gam_token(&mut self, token: [u32; 4]) { self.gam_token = Some(token); } + /// this is a separate, non-blocking call instead of a return because + /// the call which sets the predictor *must* complete to allow further drawing + /// this does mean there is a tiny bit of a race condition between when + /// a context is swapped and when a predictor can run. + pub fn send_api_token(&self, at: &ApiToken) { + self.gam.set_predictor_api_token(at.api_token, at.gam_token).expect("couldn't set predictor API token"); + } pub fn set_predictor(&mut self, predictor: Option) { self.predictor = predictor; if let Some(pred) = predictor { @@ -682,16 +689,15 @@ fn main() -> ! { emoji_menu(xous::connect(imef_sid).unwrap()); log::trace!("Initialized but still waiting for my canvas Gids"); - let imef_cid = xous::connect(imef_sid).unwrap(); // the API token allows individual predictor back end uses to have their own history buffers - let mut api_token: Option<[u32; 4]> = None; + let mut api_token: Option = None; loop { - let mut msg = xous::receive_message(imef_sid).unwrap(); + let msg = xous::receive_message(imef_sid).unwrap(); log::trace!("Message: {:?}", msg); match FromPrimitive::from_usize(msg.body.id()) { Some(ImefOpcode::ConnectBackend) => { - let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut descriptor = buffer.to_original::().unwrap(); + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let descriptor = buffer.to_original::().unwrap(); if let Some(input) = descriptor.input_canvas { if debug1 || dbgcanvas {info!("got input canvas {:?}", input);} @@ -707,7 +713,7 @@ fn main() -> ! { } // disconnect any existing predictor, if we have one already if let Some(pred) = tracker.get_predictor() { - pred.release(api_token.take().unwrap()); // api token *should* be Some() if pred is Some() + pred.release(api_token.take().unwrap().api_token); // api token *should* be Some() if pred is Some() if let Some((name, token)) = tracker.predictor_conn { xns.disconnect_with_token(name.as_str().unwrap(), token) .expect("couldn't disconnect from previous predictor. Something is wrong with internal state!"); @@ -716,15 +722,15 @@ fn main() -> ! { tracker.set_predictor(None); } if let Some(s) = descriptor.predictor { - log::trace!("got prediction server: {}", s.as_str().unwrap()); match xns.request_connection_with_token(s.as_str().unwrap()) { Ok((pc, token)) => { let pred = ime_plugin_api::PredictionPlugin {connection: Some(pc)}; match pred.acquire(descriptor.predictor_token) { Ok(confirmation) => { - api_token = Some(confirmation); - // we are required to send back the returned token as confirmation of the connection - descriptor.predictor_token = Some(confirmation); + api_token = Some(ApiToken { + api_token: confirmation, + gam_token: descriptor.token, + }); }, Err(e) => log::error!("Internal error: {:?}", e), } @@ -737,8 +743,11 @@ fn main() -> ! { _ => error!("can't find predictive engine {}, retaining existing one.", s.as_str().unwrap()), } } + log::debug!("predictor: {:?}, api_token: {:?}", tracker.get_predictor(), api_token); tracker.set_gam_token(descriptor.token); - buffer.replace(descriptor).unwrap(); + if let Some(at) = &api_token { + tracker.send_api_token(at); + } } Some(ImefOpcode::RegisterListener) => msg_scalar_unpack!(msg, sid0, sid1, sid2, sid3, { let sid = xous::SID::from_u32(sid0 as _, sid1 as _, sid2 as _, sid3 as _); @@ -751,7 +760,7 @@ fn main() -> ! { } }), Some(ImefOpcode::ProcessKeys) => { - if tracker.is_init() { + if tracker.is_init() && api_token.is_some() { msg_scalar_unpack!(msg, k1, k2, k3, k4, { let keys = [ core::char::from_u32(k1 as u32).unwrap_or('\u{0000}'), @@ -763,7 +772,7 @@ fn main() -> ! { if keys[0] == '😊' { tracker.activate_emoji(); } else { - if let Some(line) = tracker.update(keys, false, api_token.unwrap()).expect("couldn't update input tracker with latest key presses") { + if let Some(line) = tracker.update(keys, false, api_token.as_ref().unwrap().api_token).expect("couldn't update input tracker with latest key presses") { if dbglistener{info!("sending listeners {:?}", line);} if let Some(conn) = listener { if dbglistener{info!("sending to conn {:?}", conn);} @@ -794,10 +803,10 @@ fn main() -> ! { } } Some(ImefOpcode::Redraw) => msg_scalar_unpack!(msg, arg, _, _, _, { - if tracker.is_init() { + if tracker.is_init() && api_token.is_some() { let force = if arg != 0 { true } else { false }; tracker.clear_area().expect("can't initially clear areas"); - tracker.update(['\u{0000}'; 4], force, api_token.unwrap()).expect("can't setup initial screen arrangement"); + tracker.update(['\u{0000}'; 4], force, api_token.as_ref().unwrap().api_token).expect("can't setup initial screen arrangement"); } else { log::trace!("got redraw, but we're not initialized"); // ignore keyboard events until we've fully initialized @@ -806,14 +815,8 @@ fn main() -> ! { Some(ImefOpcode::SetMenuMode) => msg_scalar_unpack!(msg, arg, _, _, _, { if arg == 1 { tracker.set_menu_mode(true); - xous::send_message(imef_cid, - xous::Message::new_scalar(ImefOpcode::Redraw.to_usize().unwrap(), 1, 0, 0, 0) - ).ok(); } else { tracker.set_menu_mode(false); - xous::send_message(imef_cid, - xous::Message::new_scalar(ImefOpcode::Redraw.to_usize().unwrap(), 1, 0, 0, 0) - ).ok(); } }), Some(ImefOpcode::Quit) => {log::error!("recevied quit, goodbye!"); break;} diff --git a/services/ime-plugin-api/src/lib.rs b/services/ime-plugin-api/src/lib.rs index 901674fde..724f1679c 100644 --- a/services/ime-plugin-api/src/lib.rs +++ b/services/ime-plugin-api/src/lib.rs @@ -15,6 +15,11 @@ pub struct Prediction { pub struct AcquirePredictor { pub token: Option<[u32; 4]>, } +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub struct ApiToken { + pub gam_token: [u32; 4], + pub api_token: [u32; 4], +} #[derive(Debug, Default, Copy, Clone)] pub struct PredictionTriggers { @@ -281,7 +286,7 @@ pub struct ImefDescriptor { } pub trait ImeFrontEndApi { - fn connect_backend(&self, descriptor: ImefDescriptor) -> Result<[u32; 4], xous::Error>; + fn connect_backend(&self, descriptor: ImefDescriptor) -> Result<(), xous::Error>; fn hook_listener_callback(&mut self, cb: fn(String<4000>)) -> Result<(), xous::Error>; fn redraw(&self, force_all: bool) -> Result<(), xous::Error>; fn send_keyevent(&self, keys: [char; 4]) -> Result<(), xous::Error>; @@ -344,16 +349,11 @@ impl ImeFrontEndApi for ImeFrontEnd { fn getop_process_keys(&self) -> u32 { ImefOpcode::ProcessKeys.to_u32().unwrap() } - fn connect_backend(&self, descriptor: ImefDescriptor) -> Result<[u32; 4], xous::Error> { - let mut buf = Buffer::into_buf(descriptor).or(Err(xous::Error::InternalError))?; - buf.lend_mut(self.cid, ImefOpcode::ConnectBackend.to_u32().unwrap()) + fn connect_backend(&self, descriptor: ImefDescriptor) -> Result<(), xous::Error> { + let buf = Buffer::into_buf(descriptor).or(Err(xous::Error::InternalError))?; + buf.send(self.cid, ImefOpcode::ConnectBackend.to_u32().unwrap()) .or(Err(xous::Error::InternalError)) - .map(|_| ())?; - let ret = buf.to_original::().unwrap(); - match ret.predictor_token { - Some(token) => Ok(token), - _ => Err(xous::Error::AccessDenied), - } + .map(|_| ()) } fn send_keyevent(&self, keys: [char; 4]) -> Result<(), xous::Error> {