Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avm2: Enable stack traces for Flash Player 11.5+ and SWF18+ #19571

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions core/src/avm2/globals/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::avm2::string::AvmString;
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2::TObject;
use crate::PlayerMode;

pub fn call_handler<'gc>(
activation: &mut Activation<'_, 'gc>,
Expand All @@ -24,6 +25,22 @@ pub fn get_stack_trace<'gc>(
) -> Result<Value<'gc>, Error<'gc>> {
let this = this.as_object().unwrap();

// See <https://docs.ruffle.rs/en_US/FlashPlatform/reference/actionscript/3/Error.html#getStackTrace()>
// But note that the behavior also depends on SWF version.
let stack_trace_enabled = if activation.context.player_version >= 18
&& activation.caller_movie_or_root().version() >= 18
{
// For Flash Player 11.5+ and SWF>=18, stack traces are always enabled.
true
} else {
// For Flash Player Player 11.4 and earlier, or for SWF<18, stack traces are enabled for debug only.
activation.context.player_mode == PlayerMode::Debug
};

if !stack_trace_enabled {
return Ok(Value::Null);
}

if let Some(error) = this.as_error_object() {
let call_stack = error.call_stack();
if !call_stack.is_empty() {
Expand Down
6 changes: 2 additions & 4 deletions core/src/avm2/object/error_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::string::WString;
use core::fmt;
use gc_arena::{Collect, Gc, GcWeak};
use std::fmt::Debug;
use tracing::{enabled, Level};

/// A class instance allocator that allocates Error objects.
pub fn error_allocator<'gc>(
Expand All @@ -19,9 +18,8 @@ pub fn error_allocator<'gc>(
) -> Result<Object<'gc>, Error<'gc>> {
let base = ScriptObjectData::new(class);

let call_stack = (enabled!(Level::INFO) || cfg!(feature = "avm_debug"))
.then(|| activation.avm2().call_stack().borrow().clone())
.unwrap_or_default();
// Stack trace is always collected for debugging purposes.
let call_stack = activation.avm2().call_stack().borrow().clone();

Ok(ErrorObject(Gc::new(
activation.gc(),
Expand Down
3 changes: 3 additions & 0 deletions core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use crate::stub::StubCollection;
use crate::tag_utils::{SwfMovie, SwfSlice};
use crate::timer::Timers;
use crate::vminterface::Instantiator;
use crate::PlayerMode;
use core::fmt;
use gc_arena::{Collect, Mutation};
use rand::rngs::SmallRng;
Expand Down Expand Up @@ -84,6 +85,8 @@ pub struct UpdateContext<'gc> {
/// variables.
pub player_version: u8,

pub player_mode: PlayerMode,

/// Requests that the player re-renders after this execution (e.g. due to `updateAfterEvent`).
pub needs_render: &'gc mut bool,

Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub use events::PlayerEvent;
pub use font::DefaultFont;
pub use indexmap;
pub use loader::LoadBehavior;
pub use player::{Player, PlayerBuilder, PlayerRuntime, StaticCallstack};
pub use player::{Player, PlayerBuilder, PlayerMode, PlayerRuntime, StaticCallstack};
pub use ruffle_render::backend::ViewportDimensions;
pub use swf;
pub use swf::Color;
Expand Down
21 changes: 21 additions & 0 deletions core/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ pub struct Player {
#[allow(unused)]
player_runtime: PlayerRuntime,

/// Whether we're emulating the release or the debug build.
player_mode: PlayerMode,

swf: Arc<SwfMovie>,

run_state: RunState,
Expand Down Expand Up @@ -2179,6 +2182,7 @@ impl Player {

let mut update_context = UpdateContext {
player_version: this.player_version,
player_mode: this.player_mode,
swf: &mut this.swf,
library,
rng: &mut this.rng,
Expand Down Expand Up @@ -2454,6 +2458,7 @@ pub struct PlayerBuilder {
gamepad_button_mapping: HashMap<GamepadButton, KeyCode>,
player_version: Option<u8>,
player_runtime: PlayerRuntime,
player_mode: PlayerMode,
quality: StageQuality,
page_url: Option<String>,
frame_rate: Option<f64>,
Expand Down Expand Up @@ -2505,6 +2510,7 @@ impl PlayerBuilder {
gamepad_button_mapping: HashMap::new(),
player_version: None,
player_runtime: PlayerRuntime::default(),
player_mode: PlayerMode::default(),
quality: StageQuality::High,
page_url: None,
frame_rate: None,
Expand Down Expand Up @@ -2679,6 +2685,12 @@ impl PlayerBuilder {
self
}

/// Configures the player mode (default is `PlayerMode::Release`)
pub fn with_player_mode(mut self, mode: PlayerMode) -> Self {
self.player_mode = mode;
self
}

// Configure the embedding page's URL (if applicable)
pub fn with_page_url(mut self, page_url: Option<String>) -> Self {
self.page_url = page_url;
Expand Down Expand Up @@ -2859,6 +2871,7 @@ impl PlayerBuilder {
instance_counter: 0,
player_version,
player_runtime: self.player_runtime,
player_mode: self.player_mode,
run_state: if self.autoplay {
RunState::Playing
} else {
Expand Down Expand Up @@ -3043,3 +3056,11 @@ impl FromStr for PlayerRuntime {
Ok(player_runtime)
}
}

#[derive(Default, Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub enum PlayerMode {
#[default]
Release,
Debug,
}
7 changes: 5 additions & 2 deletions tests/framework/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use approx::relative_eq;
use image::ImageFormat;
use regex::Regex;
use ruffle_core::tag_utils::SwfMovie;
use ruffle_core::{PlayerBuilder, PlayerRuntime, ViewportDimensions};
use ruffle_core::{PlayerBuilder, PlayerMode, PlayerRuntime, ViewportDimensions};
use ruffle_render::backend::RenderBackend;
use ruffle_render::quality::StageQuality;
use serde::Deserialize;
Expand Down Expand Up @@ -150,6 +150,7 @@ pub struct PlayerOptions {
with_audio: bool,
with_video: bool,
runtime: PlayerRuntime,
mode: PlayerMode,
}

impl PlayerOptions {
Expand All @@ -172,7 +173,9 @@ impl PlayerOptions {
player_builder = player_builder.with_audio(TestAudioBackend::default());
}

player_builder = player_builder.with_player_runtime(self.runtime);
player_builder = player_builder
.with_player_runtime(self.runtime)
.with_player_mode(self.mode);

if self.with_video {
#[cfg(feature = "ruffle_video_external")]
Expand Down
22 changes: 22 additions & 0 deletions tests/tests/swfs/avm2/error_stack_trace_debug_swf17/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package {
import flash.display.*;
import flash.geom.*;

[SWF(width="10", height="10")]
public class Test extends Sprite {
public function Test() {
var st = new Error().getStackTrace();
var rect = new Sprite();
if (st === null) {
rect.graphics.beginFill(0xFF0000);
} else if (st === undefined) {
rect.graphics.beginFill(0x00FF00);
} else {
rect.graphics.beginFill(0x0000FF);
}
rect.graphics.drawRect(0, 0, 10, 10);
rect.graphics.endFill();
addChild(rect);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/tests/swfs/avm2/error_stack_trace_debug_swf17/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
num_ticks = 1

[image_comparisons.output]
tolerance = 0

[player_options]
mode = "Debug"
with_renderer = { optional = false, sample_count = 4 }
22 changes: 22 additions & 0 deletions tests/tests/swfs/avm2/error_stack_trace_debug_swf18/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package {
import flash.display.*;
import flash.geom.*;

[SWF(width="10", height="10")]
public class Test extends Sprite {
public function Test() {
var st = new Error().getStackTrace();
var rect = new Sprite();
if (st === null) {
rect.graphics.beginFill(0xFF0000);
} else if (st === undefined) {
rect.graphics.beginFill(0x00FF00);
} else {
rect.graphics.beginFill(0x0000FF);
}
rect.graphics.drawRect(0, 0, 10, 10);
rect.graphics.endFill();
addChild(rect);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/tests/swfs/avm2/error_stack_trace_debug_swf18/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
num_ticks = 1

[image_comparisons.output]
tolerance = 0

[player_options]
mode = "Debug"
with_renderer = { optional = false, sample_count = 4 }
22 changes: 22 additions & 0 deletions tests/tests/swfs/avm2/error_stack_trace_release_swf17/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package {
import flash.display.*;
import flash.geom.*;

[SWF(width="10", height="10")]
public class Test extends Sprite {
public function Test() {
var st = new Error().getStackTrace();
var rect = new Sprite();
if (st === null) {
rect.graphics.beginFill(0xFF0000);
} else if (st === undefined) {
rect.graphics.beginFill(0x00FF00);
} else {
rect.graphics.beginFill(0x0000FF);
}
rect.graphics.drawRect(0, 0, 10, 10);
rect.graphics.endFill();
addChild(rect);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
num_ticks = 1

[image_comparisons.output]
tolerance = 0

[player_options]
mode = "Release"
with_renderer = { optional = false, sample_count = 4 }
22 changes: 22 additions & 0 deletions tests/tests/swfs/avm2/error_stack_trace_release_swf18/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package {
import flash.display.*;
import flash.geom.*;

[SWF(width="10", height="10")]
public class Test extends Sprite {
public function Test() {
var st = new Error().getStackTrace();
var rect = new Sprite();
if (st === null) {
rect.graphics.beginFill(0xFF0000);
} else if (st === undefined) {
rect.graphics.beginFill(0x00FF00);
} else {
rect.graphics.beginFill(0x0000FF);
}
rect.graphics.drawRect(0, 0, 10, 10);
rect.graphics.endFill();
addChild(rect);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
num_ticks = 1

[image_comparisons.output]
tolerance = 0

[player_options]
mode = "Release"
with_renderer = { optional = false, sample_count = 4 }
Loading