Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Fix hygiene issues in declare_program! and declare_loader! #10905

Merged
merged 6 commits into from
Jul 14, 2020
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
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/bpf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ thiserror = "1.0"

[dev-dependencies]
rand = "0.7.3"
rustversion = "1.0.3"

[lib]
crate-type = ["lib", "cdylib"]
Expand Down
8 changes: 8 additions & 0 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ mod tests {
}
}

#[rustversion::since(1.46.0)]
#[test]
fn test_bpf_loader_same_crate() {
// Ensure that we can invoke this macro from the same crate
// where it is defined.
solana_bpf_loader_program!();
}

#[test]
#[should_panic(expected = "ExceededMaxInstructions(10)")]
fn test_bpf_loader_non_terminating_program() {
Expand Down
13 changes: 13 additions & 0 deletions programs/librapay/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions programs/move_loader/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ solana-crate-features = { path = "../crate-features", version = "1.3.0", optiona
solana-logger = { path = "../logger", version = "1.3.0", optional = true }
solana-sdk-macro = { path = "macro", version = "1.3.0" }
solana-sdk-macro-frozen-abi = { path = "macro-frozen-abi", version = "1.3.0" }
rustversion = "1.0.3"

[dev-dependencies]
tiny-bip39 = "0.7.0"
Expand Down
5 changes: 5 additions & 0 deletions sdk/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ fn main() {
}
Channel::Dev => {
println!("cargo:rustc-cfg=RUSTC_WITH_SPECIALIZATION");
// See https://github.com/solana-labs/solana/issues/11055
// We may be running the custom `rust-bpf-builder` toolchain,
// which currently needs `#![feature(proc_macro_hygiene)]` to
// be applied.
println!("cargo:rustc-cfg=RUSTC_NEEDS_PROC_MACRO_HYGIENE");
Comment on lines +20 to +24
Copy link
Contributor

@ryoqun ryoqun Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here (context: anza-xyz#109 (comment))

}
}
}
1 change: 1 addition & 0 deletions sdk/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bs58 = "0.3.0"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", features = ["full", "extra-traits"] }
rustversion = "1.0.3"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
73 changes: 71 additions & 2 deletions sdk/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::Span;
use proc_macro2::{Delimiter, Span, TokenTree};
use quote::{quote, ToTokens};
use std::convert::TryFrom;
use syn::{
Expand All @@ -14,7 +14,7 @@ use syn::{
parse_macro_input,
punctuated::Punctuated,
token::Bracket,
Expr, Ident, LitByte, LitStr, Token,
Expr, Ident, LitByte, LitStr, Path, Token,
};

struct Id(proc_macro2::TokenStream);
Expand Down Expand Up @@ -63,6 +63,75 @@ impl ToTokens for Id {
}
}

#[allow(dead_code)] // `respan` may be compiled out
struct RespanInput {
to_respan: Path,
respan_using: Span,
}

impl Parse for RespanInput {
fn parse(input: ParseStream) -> Result<Self> {
let to_respan: Path = input.parse()?;
let _comma: Token![,] = input.parse()?;
let respan_tree: TokenTree = input.parse()?;
match respan_tree {
TokenTree::Group(g) if g.delimiter() == Delimiter::None => {
let ident: Ident = syn::parse2(g.stream())?;
Ok(RespanInput {
to_respan,
respan_using: ident.span(),
})
}
val => Err(syn::Error::new_spanned(
val,
"expected None-delimited group",
)),
}
}
}

/// A proc-macro which respans the tokens in its first argument (a `Path`)
/// to be resolved at the tokens of its second argument.
/// For internal use only.
///
/// There must be exactly one comma in the input,
/// which is used to separate the two arguments.
/// The second argument should be exactly one token.
///
/// For example, `respan!($crate::foo, with_span)`
/// produces the tokens `$crate::foo`, but resolved
/// at the span of `with_span`.
///
/// The input to this function should be very short -
/// its only purpose is to override the span of a token
/// sequence containing `$crate`. For all other purposes,
/// a more general proc-macro should be used.
#[rustversion::since(1.46.0)] // `Span::resolved_at` is stable in 1.46.0 and above
#[proc_macro]
pub fn respan(input: TokenStream) -> TokenStream {
// Obtain the `Path` we are going to respan, and the ident
// whose span we will be using.
let RespanInput {
to_respan,
respan_using,
} = parse_macro_input!(input as RespanInput);
// Respan all of the tokens in the `Path`
let to_respan: proc_macro2::TokenStream = to_respan
.into_token_stream()
.into_iter()
.map(|mut t| {
// Combine the location of the token with the resolution behavior of `respan_using`
// Note: `proc_macro2::Span::resolved_at` is currently gated with cfg(procmacro2_semver_exempt)
// Once this gate is removed, we will no longer need to use 'unwrap()' to call
// the underling `proc_macro::Span::resolved_at` method.
let new_span: Span = t.span().unwrap().resolved_at(respan_using.unwrap()).into();
t.set_span(new_span);
t
})
.collect();
TokenStream::from(to_respan)
}

#[proc_macro]
pub fn declare_id(input: TokenStream) -> TokenStream {
let id = parse_macro_input!(input as Id);
Expand Down
73 changes: 60 additions & 13 deletions sdk/src/entrypoint_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,63 @@ pub type LoaderEntrypoint = unsafe extern "C" fn(
invoke_context: &dyn InvokeContext,
) -> Result<(), InstructionError>;

#[rustversion::since(1.46.0)]
#[macro_export]
macro_rules! declare_name {
($name:ident) => {
#[macro_export]
macro_rules! $name {
() => {
// Subtle:
// The outer `declare_name!` macro may be expanded in another
// crate, causing the macro `$name!` to be defined in that
// crate. We want to emit a call to `$crate::id()`, and have
// `$crate` be resolved in the crate where `$name!` gets defined,
// *not* in this crate (where `declare_name! is defined).
//
// When a macro_rules! macro gets expanded, any $crate tokens
// in its output will be 'marked' with the crate they were expanded
// from. This includes nested macros like our macro `$name` - even
// though it looks like a separate macro, Rust considers it to be
// just another part of the output of `declare_program!`.
//
// We pass `$name` as the second argument to tell `respan!` to
// apply use the `Span` of `$name` when resolving `$crate::id`.
// This causes `$crate` to behave as though it was written
// at the same location as the `$name` value passed
// to `declare_name!` (e.g. the 'foo' in
// `declare_name(foo)`
//
// See the `respan!` macro for more details.
// This should use `crate::respan!` once
// https://github.com/rust-lang/rust/pull/72121 is merged:
// see https://github.com/solana-labs/solana/issues/10933.
// For now, we need to use `::solana_sdk`
//
// `respan!` respans the path `$crate::id`, which we then call (hence the extra
// parens)
(
stringify!($name).to_string(),
::solana_sdk::respan!($crate::id, $name)(),
)
};
}
};
}

#[rustversion::not(since(1.46.0))]
#[macro_export]
macro_rules! declare_name {
($name:ident) => {
#[macro_export]
macro_rules! $name {
() => {
(stringify!($name).to_string(), $crate::id())
};
}
};
}

/// Convenience macro to declare a native program
///
/// bs58_string: bs58 string representation the program's id
Expand Down Expand Up @@ -102,13 +159,7 @@ pub type LoaderEntrypoint = unsafe extern "C" fn(
macro_rules! declare_program(
($bs58_string:expr, $name:ident, $entrypoint:expr) => (
$crate::declare_id!($bs58_string);

#[macro_export]
macro_rules! $name {
() => {
(stringify!($name).to_string(), $crate::id())
};
}
$crate::declare_name!($name);

#[no_mangle]
pub extern "C" fn $name(
Expand All @@ -126,13 +177,9 @@ macro_rules! declare_program(
macro_rules! declare_loader(
($bs58_string:expr, $name:ident, $entrypoint:expr) => (
$crate::declare_id!($bs58_string);
$crate::declare_name!($name);


#[macro_export]
macro_rules! $name {
() => {
(stringify!($name).to_string(), $crate::id())
};
}

#[no_mangle]
pub extern "C" fn $name(
Expand Down
Loading