Support deep TeX numeric scans with bounded stack growth
This commit is contained in:
1 parent
7070cc6b82
commit
dba73f1f47
9 files changed
+206
-47
No files matched your search
Generated
+42
@@ -53,6 +53,15 @@ dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ar_archive_writer"
|
||||
version = "0.5.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "523e5e187fff36fcb8d1d19ccf0ca4ca43ec3d158316057f66dc6f72716bc5ec"
|
||||
dependencies = [
|
||||
"object",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.7.8"
|
||||
@@ -891,6 +900,15 @@ dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.39.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2e5a6c098c7a3b6547378093f5cc30bc54fd361ce711e05293a5cc589562739b"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.4"
|
||||
@@ -1023,6 +1041,16 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "psm"
|
||||
version = "0.1.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4dcd034599e63b970727f70d79e02d62390a4a84f7c6b827c27c46d5ac3fa622"
|
||||
dependencies = [
|
||||
"ar_archive_writer",
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pxfm"
|
||||
version = "0.1.30"
|
||||
@@ -1260,6 +1288,19 @@ version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
|
||||
|
||||
[[package]]
|
||||
name = "stacker"
|
||||
version = "0.1.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "707f49d46706bacf8a2b00d51dace3f9de527c13eec3778f570c411f89e69967"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"psm",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "standback"
|
||||
version = "0.2.17"
|
||||
@@ -1489,6 +1530,7 @@ dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"stacker",
|
||||
"subsetter",
|
||||
"unicode-bidi",
|
||||
"unicode-normalization",
|
||||
|
||||
@@ -18,6 +18,7 @@ unicode-bidi = "=0.3.18"
|
||||
serde = { version = "1.0", features = ["derive", "rc"] }
|
||||
serde_json = "1.0"
|
||||
sha2 = "=0.10.9"
|
||||
stacker = "=0.1.25"
|
||||
hyphenate = { path = "../../vendor/hyphenate" }
|
||||
unicode-normalization = "=0.1.25"
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ mod pdf_links;
|
||||
pub(super) mod pdf_match;
|
||||
mod quantities;
|
||||
mod restores;
|
||||
mod scan_stack;
|
||||
mod scanning;
|
||||
mod scheduling;
|
||||
mod specials;
|
||||
@@ -768,11 +769,13 @@ impl Executor {
|
||||
files: &mut impl SourceProvider,
|
||||
definition_context: bool,
|
||||
) -> Result<Option<InputToken>, Error> {
|
||||
if self.expansion_depth >= self.limits.input_depth.min(32) {
|
||||
if self.expansion_depth >= scan_stack::limit(self.limits.input_depth) {
|
||||
return self.fail("native expansion nesting limit exceeded");
|
||||
}
|
||||
self.expansion_depth += 1;
|
||||
let result = self.expand_with_pending_tokens(input, files, definition_context);
|
||||
let result = scan_stack::enter(|| {
|
||||
self.expand_with_pending_tokens(input, files, definition_context)
|
||||
});
|
||||
self.expansion_depth -= 1;
|
||||
result
|
||||
}
|
||||
@@ -2368,13 +2371,7 @@ impl Executor {
|
||||
}
|
||||
}
|
||||
fn expression(&mut self, files: &mut impl SourceProvider, depth: usize) -> Result<i32, Error> {
|
||||
if self.expression_depth >= self.limits.input_depth.min(32) {
|
||||
return self.fail("native expression nesting limit exceeded");
|
||||
}
|
||||
self.expression_depth += 1;
|
||||
let value = self.expression_sum(files, depth)?;
|
||||
self.expression_depth -= 1;
|
||||
Ok(value)
|
||||
self.with_expression_scan(|executor| executor.expression_sum(files, depth))
|
||||
}
|
||||
fn expression_sum(
|
||||
&mut self,
|
||||
@@ -2479,17 +2476,33 @@ impl Executor {
|
||||
Ok(value)
|
||||
}
|
||||
fn integer(&mut self, files: &mut impl SourceProvider) -> Result<i32, Error> {
|
||||
self.begin_value_scan()?;
|
||||
let result = self.integer_inner(files);
|
||||
self.scanner_depth -= 1;
|
||||
result
|
||||
self.with_value_scan(|executor| executor.integer_inner(files))
|
||||
}
|
||||
fn begin_value_scan(&mut self) -> Result<(), Error> {
|
||||
if self.scanner_depth >= self.limits.input_depth.min(32) {
|
||||
|
||||
fn with_value_scan<T>(
|
||||
&mut self,
|
||||
scan: impl FnOnce(&mut Self) -> Result<T, Error>,
|
||||
) -> Result<T, Error> {
|
||||
if self.scanner_depth >= scan_stack::limit(self.limits.input_depth) {
|
||||
return self.fail("native value scanner nesting limit exceeded");
|
||||
}
|
||||
self.scanner_depth += 1;
|
||||
Ok(())
|
||||
let result = scan_stack::enter(|| scan(self));
|
||||
self.scanner_depth -= 1;
|
||||
result
|
||||
}
|
||||
|
||||
fn with_expression_scan<T>(
|
||||
&mut self,
|
||||
scan: impl FnOnce(&mut Self) -> Result<T, Error>,
|
||||
) -> Result<T, Error> {
|
||||
if self.expression_depth >= scan_stack::limit(self.limits.input_depth) {
|
||||
return self.fail("native expression nesting limit exceeded");
|
||||
}
|
||||
self.expression_depth += 1;
|
||||
let result = scan_stack::enter(|| scan(self));
|
||||
self.expression_depth -= 1;
|
||||
result
|
||||
}
|
||||
fn integer_inner(&mut self, files: &mut impl SourceProvider) -> Result<i32, Error> {
|
||||
let mut sign = 1;
|
||||
|
||||
@@ -14,13 +14,9 @@ impl Executor {
|
||||
files: &mut impl SourceProvider,
|
||||
parenthesized: bool,
|
||||
) -> Result<RegisterValue, Error> {
|
||||
if self.expression_depth >= self.limits.input_depth.min(32) {
|
||||
return self.fail("native expression nesting limit exceeded");
|
||||
}
|
||||
self.expression_depth += 1;
|
||||
let result = self.quantity_expression_sum(kind, files, parenthesized);
|
||||
self.expression_depth -= 1;
|
||||
result
|
||||
self.with_expression_scan(|executor| {
|
||||
executor.quantity_expression_sum(kind, files, parenthesized)
|
||||
})
|
||||
}
|
||||
fn quantity_expression_sum(
|
||||
&mut self,
|
||||
|
||||
@@ -68,18 +68,17 @@ impl Executor {
|
||||
primitive: Primitive,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<i32, Error> {
|
||||
self.begin_value_scan()?;
|
||||
let value = self.quantity(RegisterKind::Skip, files);
|
||||
self.scanner_depth -= 1;
|
||||
let RegisterValue::Glue(glue) = value? else {
|
||||
unreachable!()
|
||||
};
|
||||
Ok(match primitive {
|
||||
Primitive::Gluestretch => glue.stretch.amount,
|
||||
Primitive::Glueshrink => glue.shrink.amount,
|
||||
Primitive::Gluestretchorder => i32::from(glue.stretch.order),
|
||||
Primitive::Glueshrinkorder => i32::from(glue.shrink.order),
|
||||
_ => unreachable!(),
|
||||
self.with_value_scan(|executor| {
|
||||
let RegisterValue::Glue(glue) = executor.quantity(RegisterKind::Skip, files)? else {
|
||||
unreachable!()
|
||||
};
|
||||
Ok(match primitive {
|
||||
Primitive::Gluestretch => glue.stretch.amount,
|
||||
Primitive::Glueshrink => glue.shrink.amount,
|
||||
Primitive::Gluestretchorder => i32::from(glue.stretch.order),
|
||||
Primitive::Glueshrinkorder => i32::from(glue.shrink.order),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
})
|
||||
}
|
||||
pub(super) fn prepare_magnification(&mut self) -> Result<i32, Error> {
|
||||
@@ -306,16 +305,11 @@ impl Executor {
|
||||
infinite: bool,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<Flex, Error> {
|
||||
self.begin_value_scan()?;
|
||||
let result = self.dimension_inner(math, infinite, true, files);
|
||||
self.scanner_depth -= 1;
|
||||
result
|
||||
self.with_value_scan(|executor| executor.dimension_inner(math, infinite, true, files))
|
||||
}
|
||||
pub(super) fn decimal_fixed(&mut self, files: &mut impl SourceProvider) -> Result<i32, Error> {
|
||||
self.begin_value_scan()?;
|
||||
let result = self.dimension_inner(false, false, false, files);
|
||||
self.scanner_depth -= 1;
|
||||
Ok(result?.amount)
|
||||
self.with_value_scan(|executor| executor.dimension_inner(false, false, false, files))
|
||||
.map(|value| value.amount)
|
||||
}
|
||||
|
||||
fn dimension_inner(
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
//! Physical stack support for mutually recursive TeX scanners and expansion.
|
||||
|
||||
const SEGMENTED: bool = cfg!(any(
|
||||
windows,
|
||||
all(
|
||||
any(target_os = "linux", target_os = "macos"),
|
||||
any(
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64",
|
||||
target_arch = "aarch64"
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
pub(super) fn limit(input_depth: usize) -> usize {
|
||||
if SEGMENTED {
|
||||
input_depth
|
||||
} else {
|
||||
input_depth.min(32)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn enter<T>(scan: impl FnOnce() -> T) -> T {
|
||||
if SEGMENTED {
|
||||
// Scanner and expansion callbacks stay on the caller's thread.
|
||||
// Resource limits count TeX frames independently of the Rust stack.
|
||||
stacker::maybe_grow(128 * 1024, 1024 * 1024, scan)
|
||||
} else {
|
||||
scan()
|
||||
}
|
||||
}
|
||||
@@ -199,9 +199,62 @@ fn expressions_are_profile_scoped_and_recursion_remains_bounded() {
|
||||
assert!(error.message.contains("undefined native control sequence"));
|
||||
let s = format!(
|
||||
"{HEADER}\\report{{\\the\\dimexpr{}1pt{}\\relax}}\\end",
|
||||
"(".repeat(50),
|
||||
")".repeat(50)
|
||||
"(".repeat(Limits::default().input_depth + 1),
|
||||
")".repeat(Limits::default().input_depth + 1)
|
||||
);
|
||||
let error = native(&s).unwrap_err();
|
||||
assert!(error.message.contains("nesting limit"), "{}", error.message);
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
windows,
|
||||
all(
|
||||
any(target_os = "linux", target_os = "macos"),
|
||||
any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"),
|
||||
),
|
||||
))]
|
||||
#[test]
|
||||
fn deep_expansion_and_parenthesized_quantities_use_a_small_host_stack() {
|
||||
let open = "(".repeat(128);
|
||||
let close = ")".repeat(128);
|
||||
let expand = "\\number ".repeat(128);
|
||||
let source = format!(
|
||||
"{HEADER}\n\\report{{{expand}7}}\n\
|
||||
\\report{{\\number\\numexpr{open}9{close}\\relax}}\n\
|
||||
\\report{{\\the\\dimexpr{open}2pt{close}\\relax}}\n\
|
||||
\\report{{\\the\\glueexpr{open}3pt plus 1fil{close}\\relax}}\n\\end"
|
||||
);
|
||||
std::thread::Builder::new()
|
||||
.stack_size(512 * 1024)
|
||||
.spawn(move || {
|
||||
compare(&source);
|
||||
})
|
||||
.unwrap()
|
||||
.join()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_expression_budget_produces_a_structured_error() {
|
||||
let open = "(".repeat(24);
|
||||
let close = ")".repeat(24);
|
||||
let source = format!("{HEADER}\n\\report{{\\number\\numexpr{open}9{close}\\relax}}\n\\end");
|
||||
let limits = Limits {
|
||||
input_depth: 16,
|
||||
..Limits::default()
|
||||
};
|
||||
let result = Executor::new(State::initex().with_etex(), limits).run(
|
||||
SourceFile {
|
||||
name: "probe.tex".into(),
|
||||
data: source.into_bytes(),
|
||||
},
|
||||
&mut Host,
|
||||
);
|
||||
let error = result
|
||||
.err()
|
||||
.expect("the caller's nesting budget must be enforced");
|
||||
assert!(
|
||||
error.message.contains("expression nesting limit exceeded"),
|
||||
"{error}"
|
||||
);
|
||||
}
|
||||
@@ -147,9 +147,10 @@ fn invalid_quantities_overflows_units_and_readonly_positions_fail() {
|
||||
"{source}"
|
||||
);
|
||||
}
|
||||
let over_budget = Limits::default().input_depth + 1;
|
||||
for source in [
|
||||
format!("\\number{}0", "\\count".repeat(130)),
|
||||
format!("\\number{}0", "\\dimen".repeat(130)),
|
||||
format!("\\number{}0", "\\count".repeat(over_budget)),
|
||||
format!("\\number{}0", "\\dimen".repeat(over_budget)),
|
||||
] {
|
||||
let error = Executor::default()
|
||||
.run(file(&source), &mut FontFiles)
|
||||
|
||||
@@ -2175,6 +2175,33 @@ PDF,恢复实际 PFB 并反转斜切时复用 1277 步,只替换第二页。
|
||||
全量编译一致。incremental-receipt.json 的 SHA-256 为
|
||||
bbc0991b0316e6d76aa58649ec873525f5544ab530d93fe491fc6e16bd00a27d。
|
||||
|
||||
### expl3 深层数值扫描与展开
|
||||
|
||||
宏展开、数值扫描和算术表达式仍由原生执行器处理。扫描与展开
|
||||
相互递归时,共用的宿主栈保护在所支持的平台按需扩展当前线程
|
||||
的栈。原有默认 TeX 输入深度为 512,调用者设定的上限继续返回
|
||||
明确的执行错误。整数与量纲表达式共用深度计数并在出错后恢复。
|
||||
其余平台保留原有的保守限制。
|
||||
[宿主栈扩展机制](https://github.com/rust-lang/stacker)
|
||||
|
||||
128 层数字展开、整数、量纲和胶表达式在 512 KiB 的线程栈上
|
||||
通过实际 pdfTeX INITEX 对照。20 项定向回归通过,覆盖扫描、
|
||||
量纲和胶、表达式边界与编译取消。直接重载原样 schema65 后,
|
||||
两个原样 expl3 输入与 XeTeX 逐行输出一致。四项结果为 70、
|
||||
8.5、7 和 8.5。已有 Unicode article 在上一版和本版生成的 PDF
|
||||
及 aux 逐字节一致。输入、格式与输出哈希保存在
|
||||
target/p3-expl3-scanner/receipt.json。
|
||||
|
||||
三页项目的第二页由实际子文件的 128 层量纲表达式决定。
|
||||
除零时保留上一版 PDF。修正后复用 636 步,只更新第二页,
|
||||
PDF、SVG 和来源映射与该修订的独立全量一致。回执位于
|
||||
target/p3-expl3-scanner/revisions/receipt.json。
|
||||
|
||||
原样 unicode-math 已执行到 \begin{document} 的数学配置。
|
||||
当前首个失败是 \Udelcode。参考 PDF 与原生失败日志均已保留。
|
||||
接续 XeTeX 扩展定界符的原语、数值查询、分组和实际定界符读取,
|
||||
再继续同一用例直至 OpenType MATH 布局与出页。
|
||||
|
||||
### XeTeX 字体探测与 OpenType MATH 参数
|
||||
|
||||
\suppressfontnotfounderror 作为 XeTeX 扩展整数进入正常赋值与分组。
|
||||
|
||||
Reference in new issue
Block a user