How to export string symbol (not function) to C?

Trying to write wireshark plugin in rust. First step wireshark does is checking if plugin version is compatible. Unfortunately it does it not by calling a function but by checking exported *char plugin_release. I managed to export functions but can not figure out how to do a string.

This export symbol but content is wrong, because it is pointer to slice and not C string.

#[no_mangle]
#[used]
pub static plugin_release : &[u8;6] = b"2.6\0";

And this should give me what I want but it does not compile:

#[no_mangle]
#[used]
pub static plugin_release: *const u8 = "2.6\0".as_ptr();
error[E0277]: `*const u8` cannot be shared between threads safely
  --> src/lib.rs:23:1
   |
23 | pub static plugin_release: *const u8 = "2.6\0".as_ptr();
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const u8` cannot be shared between threads safely
   |
   = help: the trait `std::marker::Sync` is not implemented for `*const u8`
   = note: shared static variables must have a type that implements `Sync`

Is there a way to implement it in Rust? Maybe I can have small C code and link obj file with the result .so? Anything Cargo can help with?

1 Like

&[u8; 6] is a reference to an array which will compile down to a thin pointer, and I think that is compatible with C.


On the C side you can use a unsigned char* to represent the string.

1 Like

Right, but wireshark expects value at symbol and not a pointer.
I was quite close, what worked is:

#[no_mangle]
#[used]
pub static plugin_release: [u8; 4] = *b"2.6\0";

Just need to deref it.

1 Like