Skip to content

Comments

FCNTL F_SETFL Ignore creation flags#4840

Open
MousseARaser06 wants to merge 3 commits intorust-lang:masterfrom
MousseARaser06:fcntl_fsetfl_ignore_flags
Open

FCNTL F_SETFL Ignore creation flags#4840
MousseARaser06 wants to merge 3 commits intorust-lang:masterfrom
MousseARaser06:fcntl_fsetfl_ignore_flags

Conversation

@MousseARaser06
Copy link

@MousseARaser06 MousseARaser06 commented Feb 1, 2026

This pull request aims at fixing #4352, it proceeds by having a list of platform-allowed flags (bits) for that operation (F_SETFL), and performing a bitwise AND assignment to exclude all of the flags that aren't allowed.
Allowed flags change from platform to platform

@rustbot
Copy link
Collaborator

rustbot commented Feb 1, 2026

Thank you for contributing to Miri! A reviewer will take a look at your PR, typically within a week or two.
Please remember to not force-push to the PR branch except when you need to rebase due to a conflict or when the reviewer asks you for it.

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Feb 1, 2026
@RalfJung
Copy link
Member

RalfJung commented Feb 2, 2026

Thanks for the PR :)

We'll get to reviewing it eventually, but there are other PRs in the queue that will be reviewed first.

@MousseARaser06 MousseARaser06 force-pushed the fcntl_fsetfl_ignore_flags branch from a940c59 to 9291577 Compare February 7, 2026 20:55
@rustbot

This comment has been minimized.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I have a concern regarding the fundamental approach.

View changes since this review

Comment on lines +199 to +220
let allowed_flags = match this.tcx.sess.target.os {
Os::MacOs =>
this.eval_libc_i32("O_NONBLOCK")
| this.eval_libc_i32("O_APPEND")
| this.eval_libc_i32("O_ASYNC"),
Os::FreeBsd =>
this.eval_libc_i32("O_NONBLOCK")
| this.eval_libc_i32("O_APPEND")
| this.eval_libc_i32("O_DIRECT")
| this.eval_libc_i32("O_ASYNC"),
Os::Solaris | Os::Illumos =>
this.eval_libc_i32("O_NONBLOCK")
| this.eval_libc_i32("O_APPEND")
| this.eval_libc_i32("O_DIRECT"),
// Linux + Android match case
_ =>
this.eval_libc_i32("O_NONBLOCK")
| this.eval_libc_i32("O_APPEND")
| this.eval_libc_i32("O_DIRECT")
| this.eval_libc_i32("O_NOATIME")
| this.eval_libc_i32("O_ASYNC"),
};
Copy link
Member

Choose a reason for hiding this comment

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

For Linux, we have docs saying "this operation can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags". Do we have anything similar for other OSes? If not, it seems kind of risky to just ignore everything we don't know about. The safer approach is to only ignore what we know can definitely be ignored.

Copy link
Author

@MousseARaser06 MousseARaser06 Feb 14, 2026

Choose a reason for hiding this comment

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

I searched the other docs (the ones in that match statement)
and they all more or less have the same ignored flags in their man pages
the only OS I'm not quite sure is Solaris and Illumos, but then if I don't include them inside the match statement, the test fails
I will re-check everything just to make sure, but I'm 95% sure about this

Copy link
Member

Choose a reason for hiding this comment

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

Well, the test should match reality. :)

Overall I think the easier approach is to just only ignore flags we are sure are ignored everywhere.

Copy link
Author

@MousseARaser06 MousseARaser06 Feb 21, 2026

Choose a reason for hiding this comment

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

MacOs's man pages states the following

The flags for the F_GETFL and F_SETFL commands are as follows:

       O_NONBLOCK   Non-blocking I/O; if no data is available to a read
                    call, or if a write operation would block, the read or
                    write call returns -1 with the error EAGAIN.

       O_APPEND     Force each write to append at the end of file; corre-sponds corresponds
                    sponds to the O_APPEND flag of [open(2)](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/open.2.html#//apple_ref/doc/man/2/open).

       O_ASYNC      Enable the SIGIO signal to be sent to the process
                    group when I/O is possible, e.g., upon availability of
                    data to be read.

FreeBSD's man pages states the following

The flags for the F_GETFL and F_SETFL commands are as follows:

   O_NONBLOCK   Non-blocking I/O; if no data is  available	to  a  [read(2)](https://man.freebsd.org/cgi/man.cgi?query=read&sektion=2&apropos=0&manpath=FreeBSD+15.0-RELEASE+and+Ports)
	    system  call,  or if a [write(2)](https://man.freebsd.org/cgi/man.cgi?query=write&sektion=2&apropos=0&manpath=FreeBSD+15.0-RELEASE+and+Ports) operation would block, the
	    read or write call returns -1 with the error EAGAIN.

   O_APPEND	    Force each write to	append at the end of file; corresponds
	    to the O_APPEND flag of [open(2)](https://man.freebsd.org/cgi/man.cgi?query=open&sektion=2&apropos=0&manpath=FreeBSD+15.0-RELEASE+and+Ports).

   O_DIRECT	    Minimize or	eliminate the cache  effects  of  reading  and
	    writing.   The  system  will  attempt to avoid caching the
	    data you read or write.  If	it cannot  avoid  caching  the
	    data,  it  will  minimize  the  impact the data has	on the
	    cache.  Use	of this	flag can  drastically  reduce  perfor-
	    mance if not used with care.

   O_ASYNC	    Enable  the	 SIGIO	signal to be sent to the process group
	    when I/O is	possible, e.g.,	upon availability of  data  to
	    be read.

   O_SYNC	    Enable synchronous writes.	Corresponds to the O_SYNC flag
	    of [open(2)](https://man.freebsd.org/cgi/man.cgi?query=open&sektion=2&apropos=0&manpath=FreeBSD+15.0-RELEASE+and+Ports).	 O_FSYNC is an historical synonym for O_SYNC.

   O_DSYNC	    Enable   synchronous  data	writes.	  Corresponds  to  the
	    O_DSYNC flag of [open(2)](https://man.freebsd.org/cgi/man.cgi?query=open&sektion=2&apropos=0&manpath=FreeBSD+15.0-RELEASE+and+Ports).

Illumos/Solaris's docs are very unclear, therefore only the most "common" flags are allowed (O_NONBLOCK, O_APPEND, O_DIRECT)

And Linux's man pages states the following

F_SETFL
Set the file status flags to the value specified by arg.
File access mode (O_RDONLY, O_WRONLY, O_RDWR) and file
creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC)
in arg are ignored. On Linux, this operation can change
only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and
O_NONBLOCK flags. It is not possible to change the O_DSYNC
and O_SYNC flags; see BUGS, below.

Copy link
Member

Choose a reason for hiding this comment

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

Okay. So sounds like ignoring O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC will work everywhere while also avoiding the risk of ignoring the wrong thing on new platforms / when new flags are added.

Copy link
Author

Choose a reason for hiding this comment

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

That sounds about right

@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Feb 14, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 14, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@MousseARaser06 MousseARaser06 force-pushed the fcntl_fsetfl_ignore_flags branch from d06918d to 28805fb Compare February 21, 2026 21:20
@rustbot
Copy link
Collaborator

rustbot commented Feb 21, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@MousseARaser06
Copy link
Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Feb 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Waiting for a review to complete

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants