Skip to content
This repository was archived by the owner on Jul 15, 2024. It is now read-only.

Load the symbols dynamically - #1

Open
nical wants to merge 2 commits into
miklelappo:masterfrom
nical:dynamic
Open

Load the symbols dynamically#1
nical wants to merge 2 commits into
miklelappo:masterfrom
nical:dynamic

Conversation

@nical

@nical nical commented Jun 24, 2022

Copy link
Copy Markdown

Wgpu needs to load android property symbols dynamically in order to allow a single binary to ship to new and old versions of Android, which Firefox requires. In addition, short of using jni, this is the recommended way.

This patch represents a larger change than I would have liked, but it tries to trike a balance between enabling dynamic loading and staying true to the previous API. The main change is that an AndroidProperties object is created, containing the dlopened libc as well as the required function pointers. The functions become methods of this object.

Apologies for the large diff, here are a few upsides which hopefully make up for it:

  • As mentioned earlier the binary will be compatible with all versions of android without compile time feature selection.
  • This approach is a bit more forward compatible to the whims of future android libc changes.
  • There is a lot less conditional compilation now that the function pointers are dynamically loaded and no need for a mock implementation.

@nical

nical commented Jun 24, 2022

Copy link
Copy Markdown
Author

Note: I just got this to build and I haven't yet tested it on a device, I'm putting it up for review early so that you can take the time to look at the API etc, don't hesitate to give the logic itself a thorough look and if you are OK with the new API, don't merge it before you or I have had a chance to properly test it.

Also, for reference here is how Firefox does something similar for ICU: https://searchfox.org/mozilla-central/rev/bf6f194694c9d1ae92847f3d4e4c15c2486f3200/intl/icu/source/common/putil.cpp#1082

nical added 2 commits June 27, 2022 16:34
Wgpu needs to load android property symbols dynamically in order to allow a single binary to ship to new and old versions of Android, which Firefox requires. In addition, short of using jni, this is the recommended way.

This patch represents a larger change than I would have liked, but it tries to trike a balance between enabling dynamic loading and the previous API. The main change is that an AndroidProperties object is created, containing the dlopened libc as well as the required function pointers. The functions become methods of this object.
Comment thread src/lib.rs

// Fall back to the older approach.
if let Some(get_fn) = self.get_fn {
const PROPERTY_VALUE_MAX: usize = 92;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It'd be nice to have a comment explaining the origin of this number.

Comment thread src/lib.rs
let raw = cvalue.into_raw();
let ret = unsafe { (get_fn)(cname, raw) };
match ret {
len if len > 0 => unsafe { Some(String::from_raw_parts(raw as *mut u8, len as usize, PROPERTY_VALUE_MAX)) },

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does Android guarantee that get_fn returns UTF-8? Constructing a String from bad UTF-8 is UB. I assume that's why property_callback calls CStr's to_str method, which checks for valid UTF-8.

Comment thread src/lib.rs
Err(format!("Failed to set Android property \"{}\" to \"{}\" (error code: {})", name, value, ret))
}
} else {
Err(format!("Unable to set any Android preperty"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

"property"

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants