Is Rust has link time UB like C++ for duplicate symbols?

In C++ functions with the same name but different content can be dropped in favor of only one of them.
So using the same name in different translation units is UB.

Has Rust the same behavior?

In my big project (worspace) I have two sqlite libraries compiled into Rust code,
they has different version plus patched and looks like cargo/rustc just silently drop one of implementation without any duplicate symbols errors.
I can reproduce it with

// foo/build.rs
fn main() {
    cc::Build::new().file("foo.c").compile("foo");
}
// foo/foo.c
#include <stdint.h>
#include <stdio.h>

int32_t c_func(int32_t a, int32_t b)
{
	printf("foo::c_func\n");
	return a + b;
}
// foo/src/lib.rs
extern "C" {
    pub fn c_func(a: i32, b: i32) -> i32;
}

// boo/build.rs
fn main() {
    cc::Build::new().file("boo.c").compile("boo");
}
// boo/boo.c
#include <stdint.h>
#include <stdio.h>

int32_t c_func(int32_t a, int32_t b)
{
	printf("boo::c_func\n");
	return a + b;
}
// boo/src/lib.rs
extern "C" {
    pub fn c_func(a: i32, b: i32) -> i32;
}

if compile these two crates into third one and call

#[no_mangle]
pub extern "C" fn my_f(a: i32, b: i32) -> i32 {
    unsafe { foo::c_func(a, b) - boo::c_func(a, b) }
}

it prints

foo::c_func
foo::c_func

any way to get duplicate symbols error instead of such error prone silent?

2 Likes

I don't know of a way for Rust to check for this. IIRC, that's up to the linker being used.

Yes Rust has link-time UB if you use the #[no_mangle] attribute, e.g. see the issue #28179. You may also be interested in rust in an instant.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.