Skip to content

Conversation

andreiltd
Copy link
Member

No description provided.

@andreiltd andreiltd marked this pull request as draft August 28, 2025 11:48
@andreiltd andreiltd added the kind/enhancement For PRs adding features, improving functionality, docs, tests, etc. label Aug 28, 2025
@andreiltd andreiltd force-pushed the libc-takeover branch 6 times, most recently from eaf1d54 to b18a374 Compare August 28, 2025 13:44
Copy link
Contributor

@jprendes jprendes left a comment

Choose a reason for hiding this comment

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

I love this PR!

)
.unwrap_or_default();

let n = bytes.len();
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to assert n <= count?
A wrongly implemented HostRead could cause a buffer overflow

}

let slice = unsafe { core::slice::from_raw_parts(buf as *const u8, count) };
let s = core::str::from_utf8(slice).unwrap_or("<invalid utf8>");
Copy link
Contributor

Choose a reason for hiding this comment

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

question: what do you think of using String::from_utf8_lossy instead? we are doing s.to_string() anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure! As long as from_utf8_lossy doesn't allocate if string is valid utf-8 which I believe is the case?

Copy link
Contributor

Choose a reason for hiding this comment

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

It returns a Cow, so, that's correct, but you will need to allocate a string to send it to the host function anyway... so... ¯\_(ツ)_/¯

[features]
default = ["libc", "printf"]
libc = [] # compile musl libc
printf = [ "libc" ] # compile printf
Copy link
Contributor

Choose a reason for hiding this comment

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

opinion: I would just put everything under the same libc feature

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I guess any code from libc if not referenced will be DCE anyway 🤷

pub extern "C" fn write(fd: c_int, buf: *const c_void, count: usize) -> isize {
// Only stdout (fd=1) and stderr (fd=2)
if fd != 1 && fd != 2 {
return count as isize;
Copy link
Contributor

Choose a reason for hiding this comment

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

question: shuoldn't this return -1, or some other indication of error? and set errno?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I think that in order to support this properly we would need to generate the bindings to libc headers and use that to reference errno, EBADF etc. But then, it might be better to just have picolibc-sys crate. Anyway I agree we should report an error here somehow.

pub extern "C" fn read(fd: c_int, buf: *mut c_void, count: usize) -> isize {
// Only stdin (fd=0) and only if HostRead is defined
if fd != 0 || !CAPS.host_read() {
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

question: should the case of fd != 0 return -1 and set errno instead?

@jsturtevant
Copy link
Contributor

will help with #282

@andreiltd andreiltd force-pushed the libc-takeover branch 3 times, most recently from c7610ab to 9f5c6c1 Compare September 9, 2025 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement For PRs adding features, improving functionality, docs, tests, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants