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

fix(plugin): Don't panic when ast byte not match #9562

Merged
merged 2 commits into from
Sep 19, 2024
Merged
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
8 changes: 8 additions & 0 deletions .changeset/four-tips-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
ast_node: major
swc_atoms: major
swc_common: minor
swc_plugin_proxy: minor
---

fix(swc_common): don't panic when ast byte not match
30 changes: 14 additions & 16 deletions crates/ast_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ impl VisitMut for AddAttr {
fn visit_field_mut(&mut self, f: &mut Field) {
f.attrs
.push(parse_quote!(#[cfg_attr(feature = "__rkyv", omit_bounds)]));
f.attrs
.push(parse_quote!(#[cfg_attr(feature = "__rkyv", archive_attr(omit_bounds))]));
}
}

Expand Down Expand Up @@ -203,14 +205,13 @@ pub fn ast_node(
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))]
#[cfg_attr(feature = "rkyv-impl", archive_attr(check_bytes(
bound = "__C: rkyv::validation::ArchiveContext, <__C as rkyv::Fallible>::Error: std::error::Error"
)))]
#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))]
#[cfg_attr(
feature = "rkyv-impl",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
)]
#[cfg_attr(feature = "rkyv-impl", archive(
bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")
))]
#[cfg_attr(
feature = "serde-impl",
serde(untagged)
Expand Down Expand Up @@ -267,16 +268,13 @@ pub fn ast_node(
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))]
#[cfg_attr(feature = "rkyv-impl", archive_attr(check_bytes(
bound = "__C: rkyv::validation::ArchiveContext, <__C as rkyv::Fallible>::Error: std::error::Error"
)))]
#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))]
#[cfg_attr(
feature = "rkyv-impl",
archive(
bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
)
)
)]
#[cfg_attr(feature = "rkyv-impl", archive(
bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")
))]
#serde_tag
#[cfg_attr(
feature = "serde-impl",
Expand Down
6 changes: 1 addition & 5 deletions crates/swc_atoms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@ hstr = { workspace = true }
once_cell = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }

[dependencies.rkyv]
features = ["strict", "validation"]
optional = true
workspace = true
rkyv = { workspace = true, features = ["validation"], optional = true }
5 changes: 1 addition & 4 deletions crates/swc_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ new_debug_unreachable = { workspace = true }
num-bigint = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true, optional = true }
rkyv = { workspace = true, features = [
"strict",
"validation",
], optional = true }
rkyv = { workspace = true, features = ["validation"], optional = true }
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
siphasher = { workspace = true }
Expand Down
8 changes: 6 additions & 2 deletions crates/swc_common/src/plugin/serialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,15 @@ impl PluginSerializedBytes {
pub fn deserialize<W>(&self) -> Result<VersionedSerializable<W>, Error>
where
W: rkyv::Archive,
W::Archived: rkyv::Deserialize<W, rkyv::de::deserializers::SharedDeserializeMap>,
W::Archived: rkyv::Deserialize<W, rkyv::de::deserializers::SharedDeserializeMap>
+ for<'a> rkyv::CheckBytes<rkyv::validation::validators::DefaultValidator<'a>>,
{
use anyhow::Context;

let archived = unsafe { rkyv::archived_root::<VersionedSerializable<W>>(&self.field[..]) };
let archived = rkyv::check_archived_root::<VersionedSerializable<W>>(&self.field[..])
.map_err(|err| {
anyhow::format_err!("wasm plugin bytecheck failed {:?}", err.to_string())
})?;

archived
.deserialize(&mut rkyv::de::deserializers::SharedDeserializeMap::new())
Expand Down
5 changes: 1 addition & 4 deletions crates/swc_css_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@ string_enum = { version = "0.4.4", path = "../string_enum/" }
swc_atoms = { version = "0.6.5", path = "../swc_atoms" }
swc_common = { version = "0.38.0", path = "../swc_common" }

[dependencies.rkyv]
features = ["strict", "validation"]
optional = true
workspace = true
rkyv = { workspace = true, features = ["validation"], optional = true }
12 changes: 2 additions & 10 deletions crates/swc_css_ast/src/at_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,7 @@ pub struct MediaFeatureBoolean {
#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
pub enum MediaFeatureRangeComparison {
/// `<`
Expand Down Expand Up @@ -783,11 +779,7 @@ pub struct SizeFeatureBoolean {
#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
pub enum SizeFeatureRangeComparison {
/// `<`
Expand Down
12 changes: 2 additions & 10 deletions crates/swc_css_ast/src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ pub struct Combinator {
)]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))]
Expand Down Expand Up @@ -270,11 +266,7 @@ pub struct AttributeSelector {
#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
pub enum AttributeSelectorMatcherValue {
/// `=`
Expand Down
12 changes: 2 additions & 10 deletions crates/swc_css_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ pub struct UrlKeyValue(pub Atom, pub Atom);
#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
#[cfg_attr(feature = "serde-impl", derive(Serialize, Deserialize))]
pub enum NumberType {
Expand Down Expand Up @@ -86,11 +82,7 @@ pub struct DimensionToken {
#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
#[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))]
pub enum Token {
Expand Down
18 changes: 3 additions & 15 deletions crates/swc_css_ast/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ impl EqIgnoreSpan for Str {
)]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))]
Expand Down Expand Up @@ -401,11 +397,7 @@ pub struct Ratio {
)]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))]
Expand Down Expand Up @@ -511,11 +503,7 @@ pub struct CalcOperator {
)]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))]
#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))]
Expand Down
5 changes: 1 addition & 4 deletions crates/swc_ecma_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ bytecheck = { workspace = true, optional = true }
is-macro = { workspace = true }
num-bigint = { workspace = true, features = ["serde"] }
phf = { workspace = true, features = ["macros"] }
rkyv = { workspace = true, features = [
"strict",
"validation",
], optional = true }
rkyv = { workspace = true, features = ["validation"], optional = true }
scoped-tls = { workspace = true }
serde = { workspace = true, features = ["derive"], optional = true }
string_enum = { version = "0.4.4", path = "../string_enum" }
Expand Down
8 changes: 2 additions & 6 deletions crates/swc_ecma_ast/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,12 +1273,8 @@ impl Take for Import {
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
any(feature = "rkyv-impl"),
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
feature = "rkyv",
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))]
#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))]
Expand Down
8 changes: 2 additions & 6 deletions crates/swc_ecma_ast/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ use crate::{typescript::TsTypeAnn, Expr};
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
any(feature = "rkyv-impl"),
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
feature = "rkyv",
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))]
#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))]
Expand Down
5 changes: 1 addition & 4 deletions crates/swc_html_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ serde-impl = ["serde"]

[dependencies]
is-macro = { workspace = true }
rkyv = { workspace = true, features = [
"strict",
"validation",
], optional = true }
rkyv = { workspace = true, features = ["validation"], optional = true }
serde = { workspace = true, features = ["derive"], optional = true }

string_enum = { version = "0.4.4", path = "../string_enum/" }
Expand Down
12 changes: 2 additions & 10 deletions crates/swc_html_ast/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ pub struct DocumentFragment {
)]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
pub enum DocumentMode {
/// `no-quirks`
Expand Down Expand Up @@ -81,11 +77,7 @@ impl EqIgnoreSpan for DocumentType {
)]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
pub enum Namespace {
/// `http://www.w3.org/1999/xhtml`
Expand Down
12 changes: 2 additions & 10 deletions crates/swc_html_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ pub struct AttributeToken {
)]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
#[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))]
pub enum Raw {
Expand All @@ -46,11 +42,7 @@ pub enum Raw {
)]
#[cfg_attr(
feature = "rkyv",
archive(bound(
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace + \
rkyv::ser::SharedSerializeRegistry",
deserialize = "__D: rkyv::de::SharedDeserializeRegistry"
))
archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer"))
)]
#[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))]
pub enum Token {
Expand Down
5 changes: 1 addition & 4 deletions crates/swc_plugin_proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ plugin-rt = ["__plugin_rt", "swc_common/plugin-base", "rkyv-impl"]

[dependencies]

rkyv = { workspace = true, features = [
"strict",
"validation",
], optional = true }
rkyv = { workspace = true, features = ["validation"], optional = true }
tracing = { workspace = true }

better_scoped_tls = { version = "0.1.1", path = "../better_scoped_tls" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ pub fn read_returned_result_from_host<F, R>(f: F) -> Option<R>
where
F: FnOnce(u32) -> u32,
R: rkyv::Archive,
R::Archived: rkyv::Deserialize<R, rkyv::de::deserializers::SharedDeserializeMap>,
R::Archived: rkyv::Deserialize<R, rkyv::de::deserializers::SharedDeserializeMap>
+ for<'a> rkyv::CheckBytes<rkyv::validation::validators::DefaultValidator<'a>>,
{
let allocated_returned_value_ptr = read_returned_result_from_host_inner(f);

Expand Down
Loading