use super::{TokenizeError, Tokenizer, TokenizerOptions};
use crate::grammar::MAX_NESTING;
fn deepest_tag() -> String {
let d = MAX_NESTING - 1;
format!("{{% tag x={}1{} /%}}", "f(1, ".repeat(d), ")".repeat(d))
}
pub(super) fn nested(construct: &str, depth: usize, inner: &str) -> String {
match construct {
"blockquote" => format!("{}{inner}\n", "> ".repeat(depth)),
"list" => format!("{}{inner}\n", "- ".repeat(depth)),
"ordered" => format!("{}{inner}\n", "1. ".repeat(depth)),
"quoted-list" => format!("{}{inner}\n", "> - ".repeat(depth)),
"image" => format!("{}{inner}{}\n", "".repeat(depth)),
"link-image" => format!(
"{}{inner}{}\n",
"[](v)".repeat(depth)
),
"bracket" => format!("{}{inner}\n", "[".repeat(depth)),
other => panic!("unknown construct {other}"),
}
}
pub(super) fn unlimited() -> Tokenizer {
let mut markdown_it = serde_json::Map::new();
markdown_it.insert("maxNesting".into(), 1e9.into());
Tokenizer::new(TokenizerOptions {
allow_indentation: true,
allow_comments: true,
markdown_it,
..TokenizerOptions::default()
})
}
fn on_stack(stack: usize, doc: String) -> Result<usize, TokenizeError> {
std::thread::Builder::new()
.stack_size(stack)
.spawn(move || unlimited().tokenize(&doc).map(|t| t.len()))
.expect("spawn")
.join()
.expect("no panic")
}
#[test]
#[ignore = "a measuring tool; see the module docs"]
fn probe() {
let var = |k: &str| std::env::var(k).ok();
let (Some(construct), Some(depth), Some(stack)) = (
var("PROBE_CONSTRUCT"),
var("PROBE_DEPTH"),
var("PROBE_STACK"),
) else {
return;
};
let inner = if var("PROBE_TAG").as_deref() == Some("1") {
deepest_tag()
} else {
"x".to_owned()
};
let doc = nested(&construct, depth.parse().unwrap(), &inner);
let outcome = on_stack(stack.parse().unwrap(), doc);
println!("PROBE {construct} {depth} {stack} {outcome:?}");
}
#[test]
#[ignore = "a measuring tool"]
fn deepest_nesting() {
for construct in [
"blockquote",
"list",
"ordered",
"quoted-list",
"image",
"link-image",
"bracket",
] {
let fits = |n: usize| on_stack(8 << 20, nested(construct, n, "x")).is_ok();
let (mut lo, mut hi) = (1, 2000);
while hi - lo > 1 {
let mid = (lo + hi) / 2;
if fits(mid) { lo = mid } else { hi = mid }
}
println!("DEEPEST {construct} {lo}");
}
}
const LIMITS: [(&str, usize); 7] = [
("blockquote", 159),
("list", 159),
("ordered", 159),
("quoted-list", 79),
("image", 159),
("link-image", 79),
("bracket", 159),
];
fn level_cost(construct: &str) -> u64 {
use crate::markdown_it::cost::*;
match construct {
"blockquote" => BLOCKQUOTE,
"list" | "ordered" => LIST,
"quoted-list" => BLOCKQUOTE + LIST,
"image" => IMAGE,
"link-image" => LINK + IMAGE,
"bracket" => LINK,
other => panic!("unknown construct {other}"),
}
}
fn limit(construct: &str) -> usize {
if crate::depth::can_grow() {
(crate::depth::STACK / level_cost(construct)) as usize
} else {
LIMITS
.iter()
.find(|(c, _)| *c == construct)
.expect("a construct")
.1
}
}
fn range_error() -> TokenizeError {
TokenizeError {
name: "RangeError".to_owned(),
message: "Maximum call stack size exceeded".to_owned(),
}
}
#[test]
fn every_construct_at_the_limit_fits_two_mib_and_one_more_level_is_a_range_error() {
let inner = if crate::depth::can_grow() {
"x".to_owned()
} else {
deepest_tag()
};
for (construct, _) in LIMITS {
let limit = limit(construct);
let at_limit = on_stack(2 << 20, nested(construct, limit, &inner));
assert!(at_limit.is_ok(), "{construct} {limit}: {at_limit:?}");
assert_eq!(
on_stack(2 << 20, nested(construct, limit + 1, &inner)),
Err(range_error()),
"{construct} {}",
limit + 1
);
}
if crate::depth::can_grow() {
use crate::depth::STACK;
use crate::grammar::cost::ARRAY;
let quotes = limit("blockquote") / 2;
let left = STACK - quotes as u64 * crate::markdown_it::cost::BLOCKQUOTE;
let arrays = (left / ARRAY) as usize;
let tag = |n: usize| format!("{{% x a={}1{} /%}}", "[".repeat(n), "]".repeat(n));
assert!(on_stack(2 << 20, nested("blockquote", quotes, &tag(arrays))).is_ok());
assert_eq!(
on_stack(2 << 20, nested("blockquote", quotes, &tag(arrays + 1))),
Err(range_error())
);
}
}
#[test]
fn the_tag_at_the_limit_is_parsed() {
let tokens = unlimited()
.tokenize(&nested("blockquote", 159, &deepest_tag()))
.unwrap();
let tag = tokens.iter().find(|t| t.kind == "tag").expect("the tag");
assert_eq!(tag.level, 159);
}
#[test]
fn far_past_the_limit_the_error_comes_at_once_and_memory_stays_small() {
let n = if crate::depth::can_grow() { 2000 } else { 1100 };
let deep = |a: &str, b: &str| format!("{}{}x\n", a.repeat(n), b.repeat(n));
for doc in [
deep("- ", "> "),
deep("> ", "- "),
deep("1. ", "![x"),
nested("link-image", 5000, "x"),
nested("bracket", 100_000, "x"),
] {
assert_eq!(on_stack(2 << 20, doc), Err(range_error()));
}
}
#[test]
fn markdown_its_own_max_nesting_still_stops_first() {
let t = Tokenizer::new(TokenizerOptions {
allow_indentation: true,
allow_comments: true,
..TokenizerOptions::default()
});
for (construct, _) in LIMITS {
let doc = nested(construct, 1100, "x");
let outcome = std::thread::Builder::new()
.stack_size(2 << 20)
.spawn({
let t = t.clone();
move || t.tokenize(&doc).map(|t| t.len())
})
.unwrap()
.join()
.unwrap();
assert!(outcome.is_ok(), "{construct}: {outcome:?}");
}
}