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

std::ptr::copy_nonoverlapping failing on windows #126687

Closed
rani-sharim opened this issue Jun 19, 2024 · 3 comments
Closed

std::ptr::copy_nonoverlapping failing on windows #126687

rani-sharim opened this issue Jun 19, 2024 · 3 comments
Labels
C-gub Category: the reverse of a compiler bug is generally UB

Comments

@rani-sharim
Copy link

rani-sharim commented Jun 19, 2024

I have a rust program that is compiled as a dll. This dll is then loaded by a 3rd party app.

Part of the process is creating "sessions" for the 3rd party app. For that it gives you a void pointer and basically tells you "write whatever you want in here, we won't try to read it, just give it back with each subsequent call". So for simplicity, I figured out I'd just have a uint32 session id and write it to the pointer, and read it back when given (code below).

This workes fine on 1.77.0, but crashes the 3rd party app on 1.79.0, one what seems like a memory violation.

Writing the session id:

// Session type is "pub type Session = *mut ::std::os::raw::c_void;"
// pSess is defined as "pSess: *mut bindings::Session"

let session_id = session_manager.create_session()?;

debug!("Session Id: {session_id}");

let le_bytes = session_id.to_le_bytes();

unsafe {
    std::ptr::copy_nonoverlapping(le_bytes.as_ptr(), pSess as *mut u8,  le_bytes.len()); 
}

Reading back the session id:

fn decode_session_id(pSess: &bindings::Session) -> Result<SessionId, Error> {
    if pSess.is_null() {
        return Err(anyhow::Error::msg(format!("invalid session id: null")));
    }
   
    let mut session_id_buf = [0u8; size_of::<SessionId>()];
   
    debug!("decode_session_id: before copy"); // <-- I can see this log

    unsafe {
        std::ptr::copy_nonoverlapping(pSess, session_id_buf.as_mut_ptr() as *mut bindings::Session, session_id_buf.len());
    }

    debug!("decode_session_id: after copy"); // <-- when failing, never gets to this

Version it worked on

It most recently worked on: 1.78.0

Version with regression

rustc --version --verbose:

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-pc-windows-msvc
release: 1.79.0
LLVM version: 18.1.7
@rani-sharim rani-sharim added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Jun 19, 2024
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 19, 2024
@saethlin saethlin added the S-needs-repro Status: This issue has no reproduction and needs a reproduction to make progress. label Jun 19, 2024
@saethlin
Copy link
Member

Based on the current contents of the issue description, my best guess is that this is UB in your code, not a compiler bug. We have had a few similar reports recently. If you can share enough code to reproduce the crash on 1.79, we can figure out what's going on here.

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 19, 2024
@ChrisDenton
Copy link
Contributor

std::ptr::copy_nonoverlapping(pSess, session_id_buf.as_mut_ptr() as *mut bindings::Session, session_id_buf.len());

This line looks wrong. The count is the number of items, not the byte size.

@rani-sharim
Copy link
Author

@ChrisDenton Yes, that seems to be it. Thanks!

@saethlin saethlin added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. I-prioritize Issue: Indicates that prioritization has been requested for this issue. regression-untriaged Untriaged performance or correctness regression. S-needs-repro Status: This issue has no reproduction and needs a reproduction to make progress. labels Jun 19, 2024
@workingjubilee workingjubilee added C-gub Category: the reverse of a compiler bug is generally UB and removed C-discussion Category: Discussion or questions that doesn't represent real issues. labels Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-gub Category: the reverse of a compiler bug is generally UB
Projects
None yet
Development

No branches or pull requests

5 participants