rust-embedded-community / tinyrlibc Goto Github PK
View Code? Open in Web Editor NEWTiny C library written in Rust
Tiny C library written in Rust
I think we should do a 0.3.0 release, including the latest functions needed for nrfxlib.
Hi, I'm currently attempting to use tinyrlibc in a project with the nrf9160. I've managed to build the library both standalone and as a project dependency, however, I can't seem to get the project to link using the tinyrlibc binary outputs.
rust-lld: error: undefined symbol: strncpy
>>> referenced by tls.c.obj:(tls_attach) in archive /Users/kpw/workspace/hardware/embassy/examples/nrf9160/target/thumbv8m.main-none-eabihf/release/deps/libnrfxlib_sys-fe2aec5671652a3b.rlib
rust-lld: error: undefined symbol: vsnprintf
>>> referenced by nrf_modem_at.c.obj:(at_vsprintf) in archive /Users/kpw/workspace/hardware/embassy/examples/nrf9160/target/thumbv8m.main-none-eabihf/release/deps/libnrfxlib_sys-fe2aec5671652a3b.rlib
>>> referenced by nrf_modem_at.c.obj:(at_vsprintf) in archive /Users/kpw/workspace/hardware/embassy/examples/nrf9160/target/thumbv8m.main-none-eabihf/release/deps/libnrfxlib_sys-fe2aec5671652a3b.rlib
When tinyrlibc it generates two entries in the /deps
directory, one for the .a
file generated by the c compiler, and another with the .rlib
file that contains the rust and c code together.
By default the rustc linker appears to attempt to link against the c library, e.g."-L" "/Users/kpw/workspace/hardware/embassy/examples/nrf9160/target/thumbv8m.main-none-eabihf/release/build/tinyrlibc-a7e8ade98cfa3691/out"
I added a rustc -L
arg to the .cargo/config.toml
to manually link against the libtinyrlibc.rlib
file, however the progam still exits with the same missing symbol errors.
I'm curious if there are any examples of projects successfully using the library that demonstrate how to force the linker to incorporate these symbols?
As an fyi, here's the output of nm libtinyrlibc.rlib
:
lib.rmeta:
/Users/kpw/workspace/hardware/embassy/examples/nrf9160/target/thumbv8m.main-none-eabihf/release/deps/libtinyrlibc-264159df859b252b.rlib:lib.rmeta: no symbols
tinyrlibc-264159df859b252b.tinyrlibc.406389e212438c16-cgu.0.rcgu.o:
00000000 r .Lanon.a7061450a32074659723700ab2f6815d.1
00000000 r .Lanon.a7061450a32074659723700ab2f6815d.2
U _ZN4core5slice5index24slice_end_index_len_fail17hfd7d21569e4c2694E
U _ZN4core9panicking5panic17h4cd43b1dff72db0cE
U __aeabi_memclr
U __aeabi_uldivmod
00000000 T abs
00000000 T atoi
00000000 T itoa
00000000 r str.1
00000000 T strchr
00000000 T strcmp
00000000 T strcpy
00000000 T strlen
00000000 T strncmp
00000000 T strncpy
00000000 T strstr
00000000 T strtol
00000000 T strtoul
00000000 T utoa
snprintf.o:
U itoa
00000000 T snprintf
U strtoul
U utoa
00000000 T vsnprintf
On success, the function returns the converted integral number as a long int value.
If no valid conversion could be performed, a zero value is returned (0L).
If the value read is out of the range of representable values by a long int, the function returns LONG_MAX or LONG_MIN (defined in [<climits>](https://cplusplus.com/climits)), and [errno](https://cplusplus.com/errno) is set to ERANGE.
Errno should be set to ERANGE when value is out of range. Reimplementing something similar to errno in Newlib might be possible.
This test appears to intentionally corrupt memory:
#[test]
fn two() {
let src = b"hi\0";
let mut dest = [0u8; 2]; // no space for null terminator
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
assert_eq!(unsafe { core::slice::from_raw_parts(result, 2) }, b"hi");
}
https://asan.saethlin.dev/ub?crate=tinyrlibc&version=0.3.0
test strcpy::test::two ... =================================================================
==12736==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffb5f9f7eb2 at pc 0x559635b472fc bp 0x7ffba02f4830 sp 0x7ffba02f4828
WRITE of size 1 at 0x7ffb5f9f7eb2 thread T29
#0 0x559635b472fb in strcpy /build/src/strcpy.rs:13:3
#1 0x559635b4b057 in tinyrlibc::strcpy::test::two::h743566c6ea54f540 /build/src/strcpy.rs:42:25
#2 0x559635b4af02 in tinyrlibc::strcpy::test::two::_$u7b$$u7b$closure$u7d$$u7d$::hd19b72cfcca7882c /build/src/strcpy.rs:39:10
Address 0x7ffb5f9f7eb2 is located in stack of thread T29 at offset 178 in frame
#0 0x559635b4af2f in tinyrlibc::strcpy::test::two::h743566c6ea54f540 /build/src/strcpy.rs:39
This frame has 4 object(s):
[32, 80) '_17' (line 43)
[112, 128) '_11' (line 43)
[144, 160) '_9' (line 43)
[176, 178) 'dest' (line 41) <== Memory access at offset 178 overflows this variable
SUMMARY: AddressSanitizer: stack-buffer-overflow /build/src/strcpy.rs:13:3 in strcpy
Shadow bytes around the buggy address:
0x7ffb5f9f7c00: f1 f1 f1 f1 00 00 00 f2 f2 f2 f2 f2 00 00 00 f2
0x7ffb5f9f7c80: f2 f2 f2 f2 00 00 00 f3 f3 f3 f3 f3 00 00 00 00
0x7ffb5f9f7d00: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5
0x7ffb5f9f7d80: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5
0x7ffb5f9f7e00: f1 f1 f1 f1 f8 f8 f8 f8 f8 f8 f2 f2 f2 f2 f8 f8
=>0x7ffb5f9f7e80: f2 f2 f8 f8 f2 f2[02]f3 00 00 00 00 00 00 00 00
0x7ffb5f9f7f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7ffb5f9f7f80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7ffb5f9f8000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7ffb5f9f8080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7ffb5f9f8100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==12736==ABORTING
Fix: Don't write tests that intentionally trigger undefined behavior.
Hi, I am working on binding against a C library compiled with Newlib headers. This requires implementations of some functions that:
extern crate alloc
such as malloc
and free
__errno()
, __assert_func()
fopen
, printf
math.h
(implementation of those are easy, just re-export the libm functions)What is the scope of future function implementation in tinyrlibc?
When I try to build anything containing this library, I get the following error:
The following warnings were emitted during compilation:
warning: ./src/snprintf.c:18:10: fatal error: string.h: No such file or directory
warning: 18 | #include <string.h>
warning: | ^~~~~~~~~~
warning: compilation terminated.
error: failed to run custom build command for `tinyrlibc v0.2.1`
Caused by:
process didn't exit successfully: `/home/stuart/nrf9160-dk-2/target/debug/build/tinyrlibc-8a872cb1cf75bcd4/build-script-build` (exit status: 1)
--- stdout
TARGET = Some("thumbv8m.main-none-eabihf")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-unknown-linux-gnu")
CC_thumbv8m.main-none-eabihf = None
CC_thumbv8m.main_none_eabihf = None
TARGET_CC = None
CC = None
CROSS_COMPILE = None
CFLAGS_thumbv8m.main-none-eabihf = None
CFLAGS_thumbv8m.main_none_eabihf = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
CARGO_CFG_TARGET_FEATURE = Some("mclass,thumb-mode,v5te,v6,v6k,v6t2,v7")
running: "arm-none-eabi-gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-mthumb" "-mfloat-abi=hard" "-march=armv8-m.main" "-mfpu=fpv5-sp-d16" "-Wall" "-Wextra" "-o" "/home/stuart/nrf9160-dk-2/target/thumbv8m.main-none-eabihf/debug/build/tinyrlibc-1af852193f2a9337/out/./src/snprintf.o" "-c" "./src/snprintf.c"
cargo:warning=./src/snprintf.c:18:10: fatal error: string.h: No such file or directory
cargo:warning= 18 | #include <string.h>
cargo:warning= | ^~~~~~~~~~
cargo:warning=compilation terminated.
exit status: 1
--- stderr
error occurred: Command "arm-none-eabi-gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-mthumb" "-mfloat-abi=hard" "-march=armv8-m.main" "-mfpu=fpv5-sp-d16" "-Wall" "-Wextra" "-o" "/home/stuart/nrf9160-dk-2/target/thumbv8m.main-none-eabihf/debug/build/tinyrlibc-1af852193f2a9337/out/./src/snprintf.o" "-c" "./src/snprintf.c" with args "arm-none-eabi-gcc" did not execute successfully (status code exit status: 1).
this includes nrf9160-dk examples. I believe I have all of the listed dependencies correctly installed, but I suspect the issue lies with my gcc configuration. Any assistance here would be appreciated.
OS: Manjaro
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.