From b59d49be241865e1674b68e5215442ae80e4a80d Mon Sep 17 00:00:00 2001 From: eason <30045503+Eason0729@users.noreply.github.com> Date: Thu, 7 Dec 2023 15:58:24 +0800 Subject: [PATCH] test: :test_tube: find cause of UB --- backend/Cargo.lock | 80 +++++++++++++++++++--- backend/Cargo.toml | 2 +- backend/src/controller/judger/route/mod.rs | 31 +++++---- backend/src/endpoint/util/pagination.rs | 8 ++- backend/src/init/config.rs | 2 +- 5 files changed, 96 insertions(+), 27 deletions(-) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index f564f1f9..c80c76dc 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "ahash" version = "0.7.6" @@ -184,6 +199,21 @@ dependencies = [ "uuid", ] +[[package]] +name = "backtrace" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "base64" version = "0.21.5" @@ -788,6 +818,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "gimli" +version = "0.28.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" + [[package]] name = "h2" version = "0.3.19" @@ -1299,11 +1335,20 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "miniz_oxide" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +dependencies = [ + "adler", +] + [[package]] name = "mio" -version = "0.8.8" +version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" +checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" dependencies = [ "libc", "wasi", @@ -1405,6 +1450,15 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.32.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.18.0" @@ -1532,9 +1586,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.9" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] name = "pin-utils" @@ -1912,6 +1966,12 @@ dependencies = [ "serde_json", ] +[[package]] +name = "rustc-demangle" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" + [[package]] name = "rustix" version = "0.37.19" @@ -2615,11 +2675,11 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.28.2" +version = "1.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94d7b1cfd2aa4011f2de74c2c4c63665e27a71006b0a192dcd2710272e73dfa2" +checksum = "d0c014766411e834f7af5b8f4cf46257aab4036ca95e9d2c144a10f59ad6f5b9" dependencies = [ - "autocfg", + "backtrace", "bytes", "libc", "mio", @@ -2627,7 +2687,7 @@ dependencies = [ "parking_lot", "pin-project-lite", "signal-hook-registry", - "socket2 0.4.9", + "socket2 0.5.5", "tokio-macros", "windows-sys 0.48.0", ] @@ -2644,9 +2704,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.1.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 7481a240..bb9614e7 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -42,7 +42,7 @@ version = "0.1.40" features =["async-await", "log"] [dependencies.tokio] -version = "1.28.0" +version = "1.34.0" features =["macros", "rt-multi-thread", "full","time"] [dependencies.sea-orm] diff --git a/backend/src/controller/judger/route/mod.rs b/backend/src/controller/judger/route/mod.rs index 75ef6adf..dbe6597f 100644 --- a/backend/src/controller/judger/route/mod.rs +++ b/backend/src/controller/judger/route/mod.rs @@ -1,10 +1,10 @@ -// TODO!: health check, circular reference counter pub mod direct; pub mod swarm; use super::Error; use std::{ collections::VecDeque, + mem::MaybeUninit, ops::DerefMut, sync::{ atomic::{AtomicIsize, Ordering}, @@ -26,19 +26,19 @@ use crate::{ // introduce routing layer error const HEALTHY_THRESHOLD: isize = 100; -type JudgerIntercept = - JudgerClient>; +type JudgerIntercept = JudgerClient< + service::interceptor::InterceptedService, +>; -pub struct AuthInterceptor { +pub struct BasicAuthInterceptor { secret: Option, } -impl Interceptor for AuthInterceptor { +impl Interceptor for BasicAuthInterceptor { fn call(&mut self, mut req: tonic::Request<()>) -> Result, Status> { match &self.secret { Some(secret) => { - let token: metadata::MetadataValue<_> = - format!("basic {}", secret).parse().unwrap(); + let token = secret.parse().unwrap(); req.metadata_mut().insert("Authorization", token); Ok(req) } @@ -60,8 +60,8 @@ impl ConnectionDetail { .connect() .await?; - let interceptor = AuthInterceptor { - secret: self.secret.clone(), + let interceptor = BasicAuthInterceptor { + secret: self.secret.as_ref().map(|x| ["basic ", x].concat()), }; Ok(JudgerClient::with_interceptor(channel, interceptor)) @@ -145,7 +145,7 @@ pub struct Router { impl Router { // skip because config contain basic auth secret - #[tracing::instrument(level = "debug",skip_all)] + #[tracing::instrument(level = "debug", skip_all)] pub async fn new(config: Vec) -> Result, Error> { let self_ = Arc::new(Self { routing_table: Map::new(), @@ -206,10 +206,11 @@ pub struct Upstream { impl Upstream { async fn new(detail: ConnectionDetail) -> Result<(Arc, Vec<(Uuid, LangInfo)>), Error> { let mut client = detail.connect().await?; - let info = client.judger_info(()).await?.into_inner(); + let info = client.judger_info(()).await?; + let langs = info.into_inner().langs.list; let mut result = Vec::new(); - for lang in info.langs.list.into_iter() { + for lang in langs.into_iter() { let uuid = match Uuid::parse_str(&lang.lang_uid) { Ok(x) => x, Err(err) => { @@ -219,10 +220,14 @@ impl Upstream { }; result.push((uuid, lang)); } + + let clients = Queue::new(); + clients.push(client); + Ok(( Arc::new(Self { healthy: AtomicIsize::new(HEALTHY_THRESHOLD), - clients: Queue::new(), + clients, connection: detail, }), result, diff --git a/backend/src/endpoint/util/pagination.rs b/backend/src/endpoint/util/pagination.rs index 49b25e8a..d1a0787b 100644 --- a/backend/src/endpoint/util/pagination.rs +++ b/backend/src/endpoint/util/pagination.rs @@ -155,7 +155,9 @@ where let sort = match pager.sort { RawSearchDep::Text(x) => SearchDep::Text(x), RawSearchDep::Column(sort_by, reverse) => { - let sort_by = sort_by.try_into() .map_err(|_|Error::PaginationError("Pager reconstruction failed"))?; + let sort_by = sort_by + .try_into() + .map_err(|_| Error::PaginationError("Pager reconstruction failed"))?; SearchDep::Column(sort_by, reverse) } RawSearchDep::Parent(x) => SearchDep::Parent(x), @@ -284,7 +286,9 @@ where let sort = match pager.sort { RawSearchDep::Text(x) => SearchDep::Text(x), RawSearchDep::Column(sort_by, reverse) => { - let sort_by = sort_by.try_into() .map_err(|_|Error::PaginationError("Pager reconstruction failed"))?; + let sort_by = sort_by + .try_into() + .map_err(|_| Error::PaginationError("Pager reconstruction failed"))?; SearchDep::Column(sort_by, reverse) } RawSearchDep::Parent(_) => { diff --git a/backend/src/init/config.rs b/backend/src/init/config.rs index 9e6baff1..dd28166b 100644 --- a/backend/src/init/config.rs +++ b/backend/src/init/config.rs @@ -1,4 +1,4 @@ -use std::{path::PathBuf, sync::Arc}; +use std::path::PathBuf; use serde::{Deserialize, Serialize}; use tokio::{fs, io::AsyncReadExt};