Bypass expansion stack growth and page processing on inactive paths
This commit is contained in:
1 parent
104a512a01
commit
7f068c7a30
3 files changed
+48
-33
No files matched your search
@@ -1143,8 +1143,6 @@ impl Executor {
|
|||||||
if self.expansion_depth >= scan_stack::limit(self.limits.input_depth) {
|
if self.expansion_depth >= scan_stack::limit(self.limits.input_depth) {
|
||||||
return self.fail("native expansion nesting limit exceeded");
|
return self.fail("native expansion nesting limit exceeded");
|
||||||
}
|
}
|
||||||
// Literal and frozen tokens cannot recurse. Keep the logical
|
|
||||||
// depth check above and reserve stack growth for command expansion.
|
|
||||||
if input.frozen {
|
if input.frozen {
|
||||||
return Ok(Some((input, None)));
|
return Ok(Some((input, None)));
|
||||||
}
|
}
|
||||||
@@ -1152,27 +1150,39 @@ impl Executor {
|
|||||||
let meaning = Meaning::Character(TokenKind::Character(character, category));
|
let meaning = Meaning::Character(TokenKind::Character(character, category));
|
||||||
return Ok(Some((input, Some(meaning))));
|
return Ok(Some((input, Some(meaning))));
|
||||||
}
|
}
|
||||||
|
let meaning = self.meaning(&input.token);
|
||||||
|
if !Self::needs_expansion_dispatch(&meaning, definition_context) {
|
||||||
|
return Ok(Some((input, Some(meaning))));
|
||||||
|
}
|
||||||
|
// Only the expansion which can read further input needs a physical
|
||||||
|
// stack segment. The logical depth check still applies to every token.
|
||||||
self.expansion_depth += 1;
|
self.expansion_depth += 1;
|
||||||
let result = scan_stack::enter(|| {
|
let result = scan_stack::enter(|| {
|
||||||
self.expand_with_pending_tokens(input, files, definition_context)
|
self.expand_with_pending_tokens(input, meaning, files, definition_context)
|
||||||
});
|
});
|
||||||
self.expansion_depth -= 1;
|
self.expansion_depth -= 1;
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn needs_expansion_dispatch(meaning: &Meaning, definition_context: bool) -> bool {
|
||||||
|
match meaning {
|
||||||
|
Meaning::Undefined => true,
|
||||||
|
Meaning::Macro(definition) => !definition_context || !definition.protected,
|
||||||
|
Meaning::Primitive(primitive) => primitive.expandable(),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn expand_with_pending_tokens(
|
fn expand_with_pending_tokens(
|
||||||
&mut self,
|
&mut self,
|
||||||
mut input: InputToken,
|
mut input: InputToken,
|
||||||
|
mut meaning: Meaning,
|
||||||
files: &mut impl SourceProvider,
|
files: &mut impl SourceProvider,
|
||||||
mut definition_context: bool,
|
mut definition_context: bool,
|
||||||
) -> Result<Option<ExpandedToken>, Error> {
|
) -> Result<Option<ExpandedToken>, Error> {
|
||||||
let mut pending = Vec::new();
|
let mut pending = Vec::new();
|
||||||
let mut first = None;
|
let mut first = None;
|
||||||
let result = loop {
|
let result = loop {
|
||||||
if input.frozen {
|
|
||||||
break Some((input, None));
|
|
||||||
}
|
|
||||||
let meaning = self.meaning(&input.token);
|
|
||||||
let Meaning::Primitive(command @ (Primitive::Expandafter | Primitive::UsePrimitive)) = meaning else {
|
let Meaning::Primitive(command @ (Primitive::Expandafter | Primitive::UsePrimitive)) = meaning else {
|
||||||
break self.expand_one_inner(input, meaning, files, definition_context)?;
|
break self.expand_one_inner(input, meaning, files, definition_context)?;
|
||||||
};
|
};
|
||||||
@@ -1184,25 +1194,31 @@ impl Executor {
|
|||||||
let Some((original, expandable)) = self.frozen_primitive(&operand.token) else {
|
let Some((original, expandable)) = self.frozen_primitive(&operand.token) else {
|
||||||
break None;
|
break None;
|
||||||
};
|
};
|
||||||
if expandable {
|
if !expandable {
|
||||||
input = original;
|
self.unread(operand)?;
|
||||||
definition_context = false;
|
input.token.kind = TokenKind::Command(CommandName::FrozenPrimitivePrefix);
|
||||||
continue;
|
break Some((input, None));
|
||||||
}
|
}
|
||||||
self.unread(operand)?;
|
input = original;
|
||||||
input.token.kind = TokenKind::Command(CommandName::FrozenPrimitivePrefix);
|
} else {
|
||||||
|
// Expanding the second token is the same dispatch with deferred
|
||||||
|
// input. Only scanner re-entry needs a new expansion frame.
|
||||||
|
if let Some(outer) = first.replace(self.required_raw(files)?) {
|
||||||
|
if pending.len() >= self.limits.input_depth {
|
||||||
|
return self.fail("native input stack limit exceeded");
|
||||||
|
}
|
||||||
|
pending.push(outer);
|
||||||
|
}
|
||||||
|
input = self.required_raw(files)?;
|
||||||
|
}
|
||||||
|
definition_context = false;
|
||||||
|
if input.frozen {
|
||||||
break Some((input, None));
|
break Some((input, None));
|
||||||
}
|
}
|
||||||
// Expanding the second token is the same dispatch with deferred
|
meaning = self.meaning(&input.token);
|
||||||
// input. Only a re-entry from a scanner needs a new expansion frame.
|
if !Self::needs_expansion_dispatch(&meaning, definition_context) {
|
||||||
if let Some(outer) = first.replace(self.required_raw(files)?) {
|
break Some((input, Some(meaning)));
|
||||||
if pending.len() >= self.limits.input_depth {
|
|
||||||
return self.fail("native input stack limit exceeded");
|
|
||||||
}
|
|
||||||
pending.push(outer);
|
|
||||||
}
|
}
|
||||||
input = self.required_raw(files)?;
|
|
||||||
definition_context = false;
|
|
||||||
};
|
};
|
||||||
let Some(first) = first else {
|
let Some(first) = first else {
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
@@ -1234,14 +1250,10 @@ impl Executor {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
Meaning::Macro(definition) => {
|
Meaning::Macro(definition) => {
|
||||||
if definition.protected && definition_context {
|
|
||||||
return Ok(Some((input, Some(Meaning::Macro(definition)))));
|
|
||||||
}
|
|
||||||
self.call_macro(definition, token, files)?;
|
self.call_macro(definition, token, files)?;
|
||||||
}
|
}
|
||||||
Meaning::Primitive(primitive) => {
|
Meaning::Primitive(primitive) => {
|
||||||
if primitive.expandable()
|
if !primitive.conditional()
|
||||||
&& !primitive.conditional()
|
|
||||||
&& !matches!(primitive, Else | Or | Fi)
|
&& !matches!(primitive, Else | Or | Fi)
|
||||||
&& self.read_parameter(Parameter::Tracingcommands).width() > 1
|
&& self.read_parameter(Parameter::Tracingcommands).width() > 1
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -127,9 +127,14 @@ impl Executor {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
loop {
|
loop {
|
||||||
self.read_parameter(Parameter::Tracingpages);
|
// Preserve the read at each capture epoch, including idle polls.
|
||||||
|
self.reads.insert(Dependency::Parameter(Parameter::Tracingpages));
|
||||||
|
let page = self.native_page.as_mut().unwrap();
|
||||||
|
if !page.requested || page.output_active {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
let mut observations = Vec::new();
|
let mut observations = Vec::new();
|
||||||
let next = self.native_page.as_mut().unwrap().advance(
|
let next = page.advance_requested(
|
||||||
&mut self.state,
|
&mut self.state,
|
||||||
&mut self.reads,
|
&mut self.reads,
|
||||||
&mut self.steps,
|
&mut self.steps,
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ impl NativePageState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn advance(
|
pub(super) fn advance_requested(
|
||||||
&mut self,
|
&mut self,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
reads: &mut super::DependencyReads,
|
reads: &mut super::DependencyReads,
|
||||||
@@ -182,9 +182,7 @@ impl NativePageState {
|
|||||||
limit: usize,
|
limit: usize,
|
||||||
observations: &mut Vec<PageObservation>,
|
observations: &mut Vec<PageObservation>,
|
||||||
) -> Result<Option<PageBreak>, String> {
|
) -> Result<Option<PageBreak>, String> {
|
||||||
if !self.requested || self.output_active {
|
debug_assert!(self.requested && !self.output_active);
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
self.requested = false;
|
self.requested = false;
|
||||||
while let Some(front) = self.contributions.front() {
|
while let Some(front) = self.contributions.front() {
|
||||||
*steps = steps.saturating_add(1);
|
*steps = steps.saturating_add(1);
|
||||||
|
|||||||
Reference in new issue
Block a user