0

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)
}

I attempted to invoke a Rust FFI library function, which worked successfully on macOS M1. However, when running on x86-based Windows and Linux platforms, I encountered an issue where the functions were not being called and an exception error occurred.

Expected Result:

I expected the library functions to execute without any errors or exceptions.

Actual Result:

To my surprise, regardless of whether the library function contained complex business logic or simple print statements, the functions were not invoked and an exception error occurred with the message "Exception: User-mode data execution prevention (DEP) violation at location." This issue only manifested on x86-based Windows and Linux platforms, while it worked fine on macOS (M1 chip).

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.