Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Fix some compiled errors(mismatched types) #216

Closed

Conversation

poppycompass
Copy link

Building the hello-world module according to README.md results in errors.

Environment

generic/ubuntu1810 box in Vagrant with Virtualbox

vagrant@ubuntu1810/: uname -a
Linux ubuntu1810.localdomain 4.18.0-25-generic #26-Ubuntu SMP Mon Jun 24 09:32:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

vagrant@ubuntu1810:/$ clang -v
clang version 7.0.0-3 (tags/RELEASE_700/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Candidate multilib: .;@m64
Selected multilib: .;@m64
 
vagrant@ubuntu1810:/$ rustup -v
rustup 1.21.1 (7832b2ebe 2019-12-20)

error messages

$ cd linux-kernel-module-rust/hello-world
$ make
...(snip)...
error[E0308]: mismatched types
  --> /vagrant_data/linux-kernel-module-rust/src/allocator.rs:13:41
   |
13 |         bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8
   |                                         ^^^^^^^^^^^^^ expected `u64`, found `usize`
   |
help: you can convert an `usize` to `u64` and panic if the converted value wouldn't fit
   |
13 |         bindings::krealloc(ptr::null(), layout.size().try_into().unwrap(), bindings::GFP_KERNEL) as *mut u8
   |                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
   --> /vagrant_data/linux-kernel-module-rust/src/file_operations.rs:193:28
    |
193 |         self.0.read = Some(read_callback::<T>);
    |                            ^^^^^^^^^^^^^^^^^^ expected `u64`, found `usize`
    |
    = note: expected fn pointer `unsafe extern "C" fn(_, _, u64, _) -> i64`
                  found fn item `unsafe extern "C" fn(_, _, usize, _) -> isize {file_operations::read_callback::<T>}`

error[E0308]: mismatched types
   --> /vagrant_data/linux-kernel-module-rust/src/file_operations.rs:200:29
    |
200 |         self.0.write = Some(write_callback::<T>);
    |                             ^^^^^^^^^^^^^^^^^^^ expected `u64`, found `usize`
    |
    = note: expected fn pointer `unsafe extern "C" fn(_, _, u64, _) -> i64`
                  found fn item `unsafe extern "C" fn(_, _, usize, _) -> isize {file_operations::write_callback::<T>}`

error[E0308]: mismatched types
   --> /vagrant_data/linux-kernel-module-rust/src/sysctl.rs:136:36
    |
136 |                 proc_handler: Some(proc_handler::<T>),
    |                                    ^^^^^^^^^^^^^^^^^ expected `u64`, found `usize`
    |
    = note: expected fn pointer `unsafe extern "C" fn(_, _, _, *mut u64, _) -> _`
                  found fn item `unsafe extern "C" fn(_, _, _, *mut usize, _) -> _ {sysctl::proc_handler::<T>}`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.
The following warnings were emitted during compilation:
error: could not compile `linux-kernel-module`.
...(snip)...

I fixed these errors with type cast and change variable type.

@alex alex requested a review from geofft February 13, 2020 21:03
@alex
Copy link
Member

alex commented Feb 13, 2020

Thanks for the PR! I'm a bit surprised this would happen on a 4 series kernel.

@emilio
Copy link

emilio commented Feb 23, 2020

This seems more likely to be due to the wildcard dependency on bindgen, which recently made the size_t -> usize conversion opt-in, see rust-lang/rust-bindgen#1671

@poppycompass
Copy link
Author

Thank you for your comment!
I changed the bindgen version in Cargo.toml as follow.
[-] bindgen = "*"
[+] bindgen = "0.51.0"
Compile successful.

@vext01
Copy link

vext01 commented Apr 3, 2020

Hi,

Edit: Though I was commenting on the bindgen repo, sorry :P

I think I'm experiencing something similar.

// Callback for `dl_iterate_phdr(3)`.                                                              
unsafe extern "C" fn collect_objs(                                                                 
    info: *mut p_ffi::dl_phdr_info,                                                                
    _sz: size_t,                                                                                   
    data: *mut c_void,                                                                             
) -> c_int {                                                                                       
    ...                                                                                          
};                                                                                                 
                                                                                                       
...                                   
unsafe { p_ffi::dl_iterate_phdr(Some(collect_objs), ret_void_p) };                                 
error[E0308]: mismatched types
   --> src/lib.rs:299:42
    |
299 |     unsafe { p_ffi::dl_iterate_phdr(Some(collect_objs), ret_void_p) };
    |                                          ^^^^^^^^^^^^ expected `u64`, found `usize`
    |
    = note: expected fn pointer `unsafe extern "C" fn(_, u64, _) -> _`
                  found fn item `unsafe extern "C" fn(_, usize, _) -> _ {objects::collect_objs}`

error: aborting due to previous error

Here's the signature of dl_iterate_phdr():

int dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void*), void *data);

The second arg to the callback should be size_t not u64. It will probably work on amd64, but not i386.

This arose when i updated bindgen from version 0.41 to 0.53.

Thanks

vext01 added a commit to softdevteam/phdrs that referenced this pull request Apr 3, 2020
Note that bindgen-0.52 is actually not the newest. I used this version
because of a bug in 0.53:
fishinabarrel/linux-kernel-module-rust#216 (comment)
@emilio
Copy link

emilio commented Apr 3, 2020

See rust-lang/rust-bindgen#1671. You can do size_t_is_usize(true) to roll back to the previous behavior.

@poppycompass
Copy link
Author

Thank you for your comments.
Fix this error with little change is add bindgen version in linux-kernel-module-rust/Cargo.toml.
bindgen version: "*" -> "0.51"
How about this idea?

@alex alex mentioned this pull request Jul 12, 2020
@alex alex closed this in #218 Jul 12, 2020
@geofft
Copy link
Collaborator

geofft commented Aug 9, 2020

Added size_t_is_usize(true) in #235, thanks for the hint!

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.

5 participants