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

feat(aya-log): check format and value type in proc macro #606

Merged
merged 2 commits into from
May 21, 2023

Conversation

Hanaasagi
Copy link
Contributor

Fix #513

Currently, we are not checking in the log macro if a parameter can be formatted using a specific type of format, which can lead to a runtime panic. This PR implements validation logic in proc macro.

This will add the following code in the generated code:

2023-05-18_13-48

This is a function that checks the type bound, and the function body is empty. When built with a release level optimization, e.g. opt-level=3, it will be optimized out.

Some tests

unsafe fn try_xdp_hello(ctx: XdpContext) -> Result<u32, u32> {
    // test default formatter
    info!(&ctx, "test default formatter");
    info!(&ctx, "i8 {}", 8);
    info!(&ctx, "i16 {}", 16);
    info!(&ctx, "i32 {}", 32);
    info!(&ctx, "i64 {}", 64);
    info!(&ctx, "isize {}", 128);

    info!(&ctx, "u8 {}", 8);
    info!(&ctx, "u16 {}", 16);
    info!(&ctx, "u32 {}", 32);
    info!(&ctx, "u64 {}", 64);
    info!(&ctx, "usize {}", 128);

    info!(&ctx, "str {}", "hello");

    // test lowerhex
    info!(&ctx, "test lowerhex formatter");
    info!(&ctx, "i8 {:x}", 8);
    info!(&ctx, "i16 {:x}", 16);
    info!(&ctx, "i32 {:x}", 32);
    info!(&ctx, "i64 {:x}", 64);
    info!(&ctx, "isize {:x}", 128);

    info!(&ctx, "u8 {:x}", 8);
    info!(&ctx, "u16 {:x}", 16);
    info!(&ctx, "u32 {:x}", 32);
    info!(&ctx, "u64 {:x}", 64);
    info!(&ctx, "usize {:x}", 128);

    info!(&ctx, "slice: {:x}", [1u8; 4].as_slice());

    // test upperhex
    info!(&ctx, "test upperhex formatter");
    info!(&ctx, "i8 {:X}", 8);
    info!(&ctx, "i16 {:X}", 16);
    info!(&ctx, "i32 {:X}", 32);
    info!(&ctx, "i64 {:X}", 64);
    info!(&ctx, "isize {:X}", 128);

    info!(&ctx, "u8 {:X}", 8);
    info!(&ctx, "u16 {:X}", 16);
    info!(&ctx, "u32 {:X}", 32);
    info!(&ctx, "u64 {:X}", 64);
    info!(&ctx, "usize {:X}", 128);

    info!(&ctx, "slice: {:X}", [1u8; 4].as_slice());

    info!(&ctx, "test ipv4 formatter");
    info!(&ctx, "received a packet: {:ipv4}", 0u32);

    info!(&ctx, "test ipv6 formatter");
    info!(&ctx, "received a packet: {:ipv6}", [0u8; 16]);

    info!(&ctx, "received a packet: {:mac}", [0u8; 6]);
    info!(&ctx, "received a packet: {:MAC}", [0u8; 6]);

    // comments are some invalid case
    // info!(&ctx, "received a packet: {}", [0_u8; 6]);
    // info!(&ctx, "received a packet: {:ipv6}", 10);
    // info!(&ctx, "received a packet: {:x}", "AAAAAA");
    Ok(xdp_action::XDP_PASS)
}

@netlify
Copy link

netlify bot commented May 18, 2023

Deploy Preview for aya-rs-docs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit d999a95
🔍 Latest deploy log https://app.netlify.com/sites/aya-rs-docs/deploys/6469ac330afc9f0008b811e9
😎 Deploy Preview https://deploy-preview-606--aya-rs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@Hanaasagi
Copy link
Contributor Author

Hanaasagi commented May 21, 2023

The CI failed because Clippy introduced new checks yesterday. See rust-lang/rust-clippy@dbc76a7

@Hanaasagi Hanaasagi force-pushed the check-format-in-log branch from bbf1145 to d999a95 Compare May 21, 2023 05:29
Copy link
Collaborator

@alessandrod alessandrod left a comment

Choose a reason for hiding this comment

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

Thank you!

@alessandrod alessandrod merged commit 58f1ecb into aya-rs:main May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aya-log panics when logging [u8;6] without format argument
2 participants