Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 34 additions & 12 deletions crates/bins/src/bin/datadog_static_analyzer_server/rule_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ mod tests {
use kernel::model::rule::{compute_sha256, RuleCategory, RuleSeverity, RuleType};
use kernel::utils::encode_base64_string;
use server::model::analysis_request::{AnalysisRequest, AnalysisRequestOptions, ServerRule};
use std::sync::{mpsc, Arc};

fn request_from(
rule_name: &str,
Expand Down Expand Up @@ -241,7 +242,7 @@ function visit(captures) {
fn test_cached_analysis_request_runtime_script_cache() {
const RULE_NAME: &str = "ruleset/rule-name";
let v8 = ddsa_lib::test_utils::cfg_test_v8();
let cache = RuleCache::new();
let cache = Arc::new(RuleCache::new());

let req_v1 = request_from(
RULE_NAME,
Expand All @@ -268,19 +269,40 @@ function visit(captures) {
// Tests the clearing of `v8::Script` cache
assert_ne!(req_v1.rules[0].code_base64, req_v2.rules[0].code_base64);

// Two runtimes to simulate two threads. Thus, there are two v8::UnboundScript caches, but the entrypoint is the same `RuleCache`
let mut rt_a = v8.new_runtime();
let mut rt_b = v8.new_runtime();
let (a_ran_v1_tx, a_ran_v1_rx) = mpsc::channel::<()>();
let (b_done_tx, b_done_rx) = mpsc::channel::<()>();

let resp = cached_analysis_request(&mut rt_a, req_v1.clone(), None, Some(&cache)).unwrap();
assert_eq!(resp[0].output.as_ref().unwrap(), "rule_v1");
let cache_a = Arc::clone(&cache);
let req_v1_a = req_v1.clone();
let req_v2_a = req_v2.clone();

let resp = cached_analysis_request(&mut rt_b, req_v2.clone(), None, Some(&cache)).unwrap();
assert_eq!(resp[0].output.as_ref().unwrap(), "rule_v2");
let handle_a = std::thread::spawn(move || {
let mut rt_a = v8.new_runtime();
let resp = cached_analysis_request(&mut rt_a, req_v1_a, None, Some(&cache_a)).unwrap();
assert_eq!(resp[0].output.as_ref().unwrap(), "rule_v1");
a_ran_v1_tx.send(()).unwrap();

// `rt_a` still contains a v8::UnboundScript for `req_v1`.
// `cached_analysis_request` must correctly instruct `rt_a` to clear its cache for `RULE_NAME`.
let resp = cached_analysis_request(&mut rt_a, req_v2.clone(), None, Some(&cache)).unwrap();
assert_eq!(resp[0].output.as_ref().unwrap(), "rule_v2");
b_done_rx.recv().unwrap();
// `rt_a` still contains a v8::UnboundScript for `req_v1`.
// `cached_analysis_request` must correctly instruct `rt_a` to clear its cache for `RULE_NAME`.
let resp = cached_analysis_request(&mut rt_a, req_v2_a, None, Some(&cache_a)).unwrap();
assert_eq!(resp[0].output.as_ref().unwrap(), "rule_v2");
});

let cache_b = Arc::clone(&cache);
let req_v2_b = req_v2.clone();

let handle_b = std::thread::spawn(move || {
let mut rt_b = v8.new_runtime();

a_ran_v1_rx.recv().unwrap();
let resp = cached_analysis_request(&mut rt_b, req_v2_b, None, Some(&cache_b)).unwrap();
assert_eq!(resp[0].output.as_ref().unwrap(), "rule_v2");
b_done_tx.send(()).unwrap();
});

// Two runtimes, each with their own v8::UnboundScript caches, but the entrypoint is the same `RuleCache`
handle_a.join().unwrap();
handle_b.join().unwrap();
}
}
2 changes: 1 addition & 1 deletion crates/static-analysis-kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ thiserror = { workspace = true }
tree-sitter = { workspace = true }

# other
deno_core = "0.344.0"
deno_core = "0.356.0"
globset = "0.4.16"
graphviz-rust = "0.9.6"
sequence_trie = "0.3.6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl JsRuntime {
let deno_error = deno_core::error::JsError::from_v8_exception(tc_ctx_scope, exception);
tc_ctx_scope.reset();
DDSAJsRuntimeError::Execution {
error: deno_error.into(),
error: (*deno_error).into(),
}
})?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub(crate) fn try_execute<'s>(
let deno_error = deno_core::error::JsError::from_v8_exception(tc_scope, exception);
tc_scope.reset();
DDSAJsRuntimeError::Execution {
error: deno_error.into(),
error: (*deno_error).into(),
}
})
}
Expand Down
Loading