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

C API wrapper #14

Merged
merged 9 commits into from
Dec 9, 2019
Merged

C API wrapper #14

merged 9 commits into from
Dec 9, 2019

Conversation

zokier
Copy link
Contributor

@zokier zokier commented Apr 8, 2018

Resolves #13

I tried both std and no_std based versions; while std makes the code bit prettier, the compiled library size (predictably) blows up so I think no_std version is preferable here.

no_std:

-rw-r--r-- 2 zokier zokier 2.1M Apr  8 16:00 librustc_demangle.a
-rwxr-xr-x 2 zokier zokier 539K Apr  8 16:00 librustc_demangle.so

std:

-rw-r--r-- 2 zokier zokier 9.3M Apr  8 16:00 librustc_demangle.a
-rwxr-xr-x 2 zokier zokier 2.8M Apr  8 16:00 librustc_demangle.so

The panic_fmt etc functions are directly copy-pasted from the manual section about no_std, not sure if there is something more to be considered for libraries. Admittedly having a library that might call abort is not ideal, but on the other hand I don't think there are any intentional panics here.

@zokier
Copy link
Contributor Author

zokier commented Apr 8, 2018

Right, to be able to make the lib no_std I need lang_items and core_intrinsics features, which are nightly only and that is why Travis fails now. How do you prefer to resolve this? Leave this C API as nightly only feature and adjust Travis build accordingly, or revert back to std version (which should build on stable)?

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

I think #![no_std] probably shouldn't be needed here and it'd also be great to avoid nightly. Have you tried compiling with LTO and/or running strip to see if it makes a size difference?

pub unsafe extern fn rustc_demangle(mangled: *const u8, out: *mut u8, out_size: usize) -> i64 {
// homebrew version of strlen
let mut mangled_len = 0;
while *(mangled.add(mangled_len)) != 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Could this use CStr::from_ptr perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, if we use std. CStr is not available in core.

/// Returns 0 if `mangled` is not Rust symbol or if `out` buffer is too small
/// Returns 1 otherwise
#[no_mangle]
pub unsafe extern fn rustc_demangle(mangled: *const u8, out: *mut u8, out_size: usize) -> i64 {
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 this should return i32, right? Or maybe link to the libc crate and return c_int?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With std I was using use std::os::raw::{c_char,c_int};. But you are right, it probably should be i32 instead of i64, or preferably c_int if I can get it somewhere easily. I suppose I could add libc as a dependency, although it feels bit heavy solution for getting few newtypes. Of course using std would solve this issue.

@zokier
Copy link
Contributor Author

zokier commented Apr 8, 2018

I'll do some more testing with std and see if it's weight can be reduced, and also what its impact on final linked application executable size will be. Of course I can also live with the bigger binaries, but it's bit sad to make stuff 4x bigger for relatively small benefit.

@zokier
Copy link
Contributor Author

zokier commented Apr 8, 2018

You are right, with full LTO the difference between std and nostd basically disappears. These are the numbers for std version with LTO:

-rw-r--r-- 2 zokier zokier 1.8M Apr  8 21:45 librustc_demangle.a
-rwxr-xr-x 2 zokier zokier 639K Apr  8 21:45 librustc_demangle.so

Curiously enough, using this std version of rustc-demangle needs also libdl to be linked into the application executable. Not sure what is going on there, I though .a archives were supposed to be statically linked. But that's no biggie imo.

@zokier
Copy link
Contributor Author

zokier commented Apr 8, 2018

One thing, should we have separate _hash and _nohash (final names tbd) functions instead of just arbitrarily using the "without hash" format here?

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Thanks! I'm personally ok starting with this one symbol and then we could extend it in the future if need be to something like rustc_demangle_opts(..) which takes a struct of options to specify what's going on.

Cargo.toml Outdated

[profile.release]
lto = true
codegen-units = 1
Copy link
Member

Choose a reason for hiding this comment

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

With lto = true this and incremental = false shouldn't be necessary to specify as well

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.

Provide C interface
2 participants