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

add weak memcpy et al symbols #29

Merged
merged 7 commits into from
Aug 16, 2016
Merged

add weak memcpy et al symbols #29

merged 7 commits into from
Aug 16, 2016

Conversation

japaric
Copy link
Member

@japaric japaric commented Aug 13, 2016

closes #28


rust-lang/rust#35637 needs to land first
r? @Amanieu
cc @shepmaster

@@ -0,0 +1,60 @@
// NOTE Copied verbatim from the rlibc crate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May not be the best idea. There's a bug for all of these when n > ISIZE_MAX that I've been trying to figure out how to fix to submit a PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate on what the bug is? Is it present on all architectures or only on AVR where usize is 16-bits(?).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.offset takes an isize, which is limited to ISIZE_MAX

Copy link
Member

@shepmaster shepmaster Aug 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so if usize is a u8, then isize is an i8 with a maximum value of 127, and a value for n of 200 will do Bad Things. Scale up as appropriate for the platform.

Is there a possibility we could add the weak linking to rlibc and only depend on it for the appropriate platform(s)? I'm a big fan of laser-focused crates.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The focus of this crate is to provide all of the runtime functions that are required to run Rust with libcore. This includes both compiler-rt builtins and a few libc functions (memset, memmove, memcpy, memcmp, fmod, fmodf).

Copy link
Member

@shepmaster shepmaster Aug 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The focus of this crate is to provide all of the runtime functions

Sure, but couldn't that be accomplished by this crate using rlibc as a dependency on the platforms that need it? Then using this crate will pull in rlibc and it looks the same as if the code were implemented here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel too strongly about it either way. The point of making these symbols weak is that it allows you to override them with your own implementations if you want.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shepmaster

Given that the ultimate goal is to have this crate as part of rust-lang/rust releases. It makes sense to me to also provide these mem* functions so users don't have to reach out for an external (crates.io) crate.

Copy link
Member

@shepmaster shepmaster Aug 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I just really feel like it's a bad idea to copy-paste code like this, especially as we know there are bugs that will now need to get fixed in multiple locations. It seems like there should be some way to use rlibc without forcing a crates.io download, presumably similar to whatever will happen to this crate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I missed your point before. It makes sense to put rlibc below this crate to have to fix bugs in a single place. To do that rlibc would have to gain a "weak" Cargo feature that marks the mem* functions as weak symbols.

there should be some way to use rlibc without forcing a crates.io download

Probably just make it a submodule of rust-lang/rust. Like this crate/repository will.

@Amanieu
Copy link
Member

Amanieu commented Aug 13, 2016

Does this work on Windows/Mac?

Is memcpy getting properly overridden if one is provided by libc?

@japaric
Copy link
Member Author

japaric commented Aug 13, 2016

Does this work on Windows/Mac?

I recall that WindowsBunny reported that linkage = "weak" doesn't work on Windows. And these symbols are reported as "weak" on Linux, but not on Mac. So, I guess this linkage attribute only works with ELF files.

Is memcpy getting properly overridden if one is provided by libc?

I haven't checked if they do get overridden when linked against libc. I'll test the overrides using a no_std program and a fake libc. Might be able to turn that into an unit test.

@Amanieu
Copy link
Member

Amanieu commented Aug 13, 2016

I think we can leave out these functions on Windows/Mac since it's likely you'll be a) running with std and b) these functions will be provided by libc.

fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
fn memset(dest: *mut u8, c: i32, n: usize) -> *mut u8;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to leave this as it was since the memcpy symbol might be overridden.

@shepmaster
Copy link
Member

Also amusing: alexcrichton/rlibc#11

@japaric
Copy link
Member Author

japaric commented Aug 15, 2016

Updated this to use the rlibc crate.

@japaric
Copy link
Member Author

japaric commented Aug 16, 2016

We are now using rlibc so I'm going to go ahead and merge this. The upstream LTO fix has been approved and it's already in the homu queue.

@japaric japaric merged commit 89594e1 into master Aug 16, 2016
@japaric japaric deleted the weak-memcpy branch August 16, 2016 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add the dumbest implementation of mem{cpy,move,set}
3 participants