I'm using Rust to call a Rust-written FFI library function. The library function runs fine on macOS Pro with an M1 processor. However, when running on x86-based Windows and Linux virtual machines, I encounter an error whenever any method within the library function is called. The error message is as follows:
Exception: Exception 0xc0000005 encountered at address 0x7fffd7609220: User-mode data execution prevention (DEP) violation at location
Problematic code repository: https://github.com/dingdaoyi/yanbing-edge
Code snippets:
Library function:
fn initialize(&mut self, device_list: Vec<Device>, sender: mpsc::Sender<PointEvent>, handle: Handle) -> Result<(), String> {
ModbusTcpProtocol::init_log();
println!("协议包含数据:{:?}", device_list);
self.sender = Some(sender);
self.device_list = device_list;
self.handle = Some(handle);
self.init_modbus();
self.schedule_event();
Ok(())
}
Main function invocation:
pub async fn load_protocol(
&self,
config: &ProtocolConfig,
sender: mpsc::Sender<PointEvent>,
device_list: Vec<Device>,
) -> Result<()> {
// Load the protocol library
let lib_path = Path::new(&self.lib_path);
let protocol_path = lib_path.join(get_library_filename(&config.path));
let lib = unsafe { Library::new(protocol_path) }?;
// Get the create_protocol function symbol
type CreateProtocolFn = extern "C" fn() -> *mut dyn Protocol;
let constructor: Symbol<CreateProtocolFn> = unsafe { lib.get(b"create_protocol")? };
// Call the function to obtain a raw pointer to the Protocol trait instance
let boxed_raw = constructor();
// Construct a Box from the raw pointer
let mut protocol_box = unsafe { Box::from_raw(boxed_raw) };
let protocol_box1 = unsafe { Box::from_raw(boxed_raw) };
tokio::task::spawn(async move {
let handle = tokio::runtime::Handle::current();
protocol_box.initialize(device_list, sender, handle).unwrap();
});
self.add_protocol(config.id, protocol_box1)
}
Please help me solve this issue. Thank you!