Skip to content
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
37 changes: 32 additions & 5 deletions rust/examples/keymgt-debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,37 @@

use std::cell::UnsafeCell;
use std::ffi::c_void;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::atomic::{AtomicI32, AtomicU32, Ordering};
use std::sync::Mutex;

use mariadb::log::{self, debug, trace};
use mariadb::plugin::encryption::{Encryption, Flags, KeyError, KeyManager};
use mariadb::plugin::{
register_plugin, Init, InitError, License, Maturity, PluginType, SysVarConstString, SysVarOpt,
SysVarString,
};

const KEY_LENGTH: usize = 4;
static KEY_VERSION: AtomicU32 = AtomicU32::new(1);
static TEST_SYSVAR_STR: SysVarConstString = SysVarConstString::new();
static TEST_SYSVAR_CONST_STR: SysVarConstString = SysVarConstString::new();
static TEST_SYSVAR_STR: SysVarString = SysVarString::new();
static TEST_SYSVAR_I32: AtomicI32 = AtomicI32::new(10);

struct DebugKeyMgmt;

impl Init for DebugKeyMgmt {
fn init() -> Result<(), InitError> {
log::set_max_level(log::LevelFilter::Trace);
debug!("DebugKeyMgmt get_latest_key_version");
trace!("current sysvar: {}", TEST_SYSVAR_STR.get());
trace!(
"current const str sysvar: {:?}",
TEST_SYSVAR_CONST_STR.get()
);
trace!("current str sysvar: {:?}", TEST_SYSVAR_STR.get());
trace!(
"current sysvar: {}",
TEST_SYSVAR_I32.load(Ordering::Relaxed)
);
Ok(())
}

Expand Down Expand Up @@ -91,12 +102,28 @@ register_plugin! {
encryption: false,
variables: [
SysVar {
ident: TEST_SYSVAR_STR,
ident: TEST_SYSVAR_CONST_STR,
vtype: SysVarConstString,
name: "test_sysvar",
name: "test_sysvar_const_string",
description: "this is a description",
options: [SysVarOpt::OptCmdArd],
default: "default value"
},
SysVar {
ident: TEST_SYSVAR_STR,
vtype: SysVarString,
name: "test_sysvar_string",
description: "this is a description",
options: [SysVarOpt::OptCmdArd],
default: "other default value"
},
SysVar {
ident: TEST_SYSVAR_I32,
vtype: AtomicI32,
name: "test_sysvar_i32",
description: "this is a description",
options: [SysVarOpt::OptCmdArd],
default: 67
}
]
}
2 changes: 0 additions & 2 deletions rust/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
#![warn(clippy::nursery)]
#![warn(clippy::str_to_string)]
#![warn(clippy::missing_inline_in_public_items)]
#![allow(clippy::missing_const_for_fn)]
#![allow(clippy::missing_panics_doc)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::option_if_let_else)]
#![allow(clippy::cast_possible_truncation)]

mod fields;
mod helpers;
Expand Down
17 changes: 11 additions & 6 deletions rust/macros/src/parse_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,23 @@ impl VariableInfo {
let max = process_default_override(&self.max, "max_val")?;
let interval = process_default_override(&self.interval, "blk_sz")?;

let st_ident = Ident::new(&format!("_st_sysvar_{}", name.value()), Span::call_site());
// https://github.com/rust-lang/rust/issues/86935#issuecomment-1146670057
let ty_wrap = Ident::new(
&format!("_st_sysvar_Type{}", name.value()),
let st_ident = Ident::new(&format!("_sysvar_st_{}", name.value()), Span::call_site());
let st_tycheck = Ident::new(
&format!("_sysvar_tychk_{}", name.value()),
Span::call_site(),
);
// https://github.com/rust-lang/rust/issues/86935#issuecomment-1146670057
let ty_wrap = Ident::new(&format!("_sysvar_Type{}", name.value()), Span::call_site());
// check to verify our vars are of the right type for our idents
let ty_check = quote! { static #st_tycheck: &#vtype = &#ident; };

let usynccell = quote! { ::mariadb::internals::UnsafeSyncCell };

let res = quote! {
type #ty_wrap<T> = T;

#ty_check

static #st_ident: #usynccell<#ty_wrap::<#ty_as_svwrap::CStructType>> = unsafe {
#usynccell::new(
#ty_wrap::<#ty_as_svwrap::CStructType> {
Expand All @@ -223,7 +228,7 @@ impl VariableInfo {

};

Ok((st_ident.clone(), res))
Ok((st_ident, res))
}

/// Take the options vector, parse it as an array, bitwise or the output,
Expand Down Expand Up @@ -319,7 +324,7 @@ fn verify_field_order(fields: &[String]) -> Result<(), String> {

/// Process "default override" style fields by these rules:
///
/// - If `field` is `None`, return an empty TokenStream
/// - If `field` is `None`, return an empty `TokenStream`
/// - Enforce it is a literal
/// - If it is a literal string, change it to a `cstr`
///
Expand Down
2 changes: 1 addition & 1 deletion rust/macros/src/register_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl Parse for PluginInfo {
}

impl PluginInfo {
fn new(main_ty: Ident, span: Span) -> Self {
const fn new(main_ty: Ident, span: Span) -> Self {
Self {
main_ty,
span,
Expand Down
2 changes: 1 addition & 1 deletion rust/macros/tests/fail/02-extra-sysargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ register_plugin! {
encryption: false,
variables: [
SysVar {
ident: _SYSVAR_STR,
ident: _SYSVAR_CONST_STR,
vtype: SysVarConstString,
name: "test_sysvar",
description: "this is a description",
Expand Down
30 changes: 30 additions & 0 deletions rust/macros/tests/fail/03-wrong-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Verify our added check for identifier-type mismatch
*/

include!("../include.rs");

register_plugin! {
TestPlugin,
ptype: PluginType::MariaEncryption,
name: "debug_key_management",
author: "Trevor Gross",
description: "Debug key management plugin",
license: License::Gpl,
maturity: Maturity::Experimental,
version: "0.1",
init: TestPlugin,
encryption: false,
variables: [
SysVar {
ident: _SYSVAR_ATOMIC,
vtype: SysVarConstString,
name: "test_sysvar",
description: "this is a description",
options: [SysVarOpt::ReadOnly, SysVarOpt::NoCmdOpt],
default: "default value"
}
]
}

fn main() {}
15 changes: 15 additions & 0 deletions rust/macros/tests/fail/03-wrong-types.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> tests/fail/03-wrong-types.rs:7:1
|
7 | / register_plugin! {
8 | | TestPlugin,
9 | | ptype: PluginType::MariaEncryption,
10 | | name: "debug_key_management",
... |
27 | | ]
28 | | }
| |_^ expected `&SysVarConstString`, found `&AtomicI32`
|
= note: expected reference `&'static mariadb::plugin::SysVarConstString`
found reference `&AtomicI32`
= note: this error originates in the macro `register_plugin` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion rust/macros/tests/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use mariadb::plugin::*;
pub use mariadb_macros::register_plugin;

static _SYSVAR_ATOMIC: AtomicI32 = AtomicI32::new(0);
static _SYSVAR_STR: SysVarConstString = SysVarConstString::new();
static _SYSVAR_CONST_STR: SysVarConstString = SysVarConstString::new();

struct TestPlugin;

Expand Down
4 changes: 2 additions & 2 deletions rust/macros/tests/pass/03-with-sysargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ register_plugin! {
encryption: false,
variables: [
SysVar {
ident: _SYSVAR_STR,
ident: _SYSVAR_CONST_STR,
vtype: SysVarConstString,
name: "test_sysvar",
description: "this is a description",
Expand All @@ -37,7 +37,7 @@ fn main() {
let plugin_def: &st_maria_plugin = unsafe { &*(_maria_plugin_declarations_[0]).get() };

let sysv_ptr: *mut *mut st_mysql_sys_var = plugin_def.system_vars;
let sysvar_st: *const sysvar_str_t = _st_sysvar_test_sysvar.get();
let sysvar_st: *const sysvar_str_t = _sysvar_st_test_sysvar.get();
let sysvar_arr: &[UnsafeSyncCell<*mut sysvar_common_t>] = &_plugin_debug_key_management_sysvars;
let idx_0: *mut sysvar_common_t = unsafe { *sysvar_arr[0].get() };
let idx_1: *mut sysvar_common_t = unsafe { *sysvar_arr[1].get() };
Expand Down
1 change: 1 addition & 0 deletions rust/mariadb/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl<T> UnsafeSyncCell<T> {
}
}

#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl<T> Send for UnsafeSyncCell<T> {}
unsafe impl<T> Sync for UnsafeSyncCell<T> {}

Expand Down
4 changes: 3 additions & 1 deletion rust/mariadb/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Crate representing safe abstractions over MariaDB bindings
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
#![warn(clippy::missing_inline_in_public_items)]
#![warn(clippy::str_to_string)]
#![allow(clippy::option_if_let_else)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::useless_conversion)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::missing_const_for_fn)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::missing_inline_in_public_items)]
Expand Down
2 changes: 1 addition & 1 deletion rust/mariadb/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod variables;
mod variables_parse;
mod wrapper;
pub use mariadb_macros::register_plugin;
pub use variables::{SysVarConstString, SysVarOpt};
pub use variables::{SysVarConstString, SysVarOpt, SysVarString};

/// Commonly used plugin types for reexport
pub mod prelude {
Expand Down
8 changes: 4 additions & 4 deletions rust/mariadb/src/plugin/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ pub enum EncryptionError {
pub struct Flags(i32);

impl Flags {
pub(crate) fn new(value: i32) -> Self {
pub(crate) const fn new(value: i32) -> Self {
Self(value)
}

pub(crate) fn should_encrypt(self) -> bool {
pub(crate) const fn should_encrypt(self) -> bool {
(self.0 & bindings::ENCRYPTION_FLAG_ENCRYPT as i32) != 0
}

pub(crate) fn should_decrypt(self) -> bool {
pub(crate) const fn should_decrypt(self) -> bool {
// (self.0 & bindings::ENCRYPTION_FLAG_DECRYPT as i32) != 0
!self.should_encrypt()
}

pub fn nopad(&self) -> bool {
pub const fn nopad(&self) -> bool {
(self.0 & bindings::ENCRYPTION_FLAG_NOPAD as i32) != 0
}
}
Expand Down
Loading