Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not write to user data directories on build. Fix docs.rs documentation generation #231

Merged
merged 6 commits into from
May 9, 2022
Merged

Conversation

AlexTMjugador
Copy link
Contributor

@AlexTMjugador AlexTMjugador commented May 2, 2022

This change addresses the same problem as #185. It turns out that build scripts are only meant to write to OUT_DIR, and in general it is untidy to write things all over the place. docs.rs is specially sensitive to this.

Also, docs.rs does not set the feature flag docsrs by itself, but a Cargo package metadata section can be used to instruct docs.rs to pass that flag to Cargo.

I have not tested the change.

This change addresses the same problem as #185. It turns out that build
scripts are only meant to write to OUT_DIR, and in general it is untidy
to write things all over the place. docs.rs is specially sensitive to
this.

Also, docs.rs does not set the feature flag "docsrs" by itself, but a
Cargo package metadata section can be used to instruct docs.rs to pass
that flag to Cargo.
@AlexTMjugador AlexTMjugador marked this pull request as draft May 2, 2022 19:09
@AlexTMjugador
Copy link
Contributor Author

Tested by making the usual build artifacts folder a symlink to /dev/null on a Linux machine, so any I/O write attempt to it would fail, and then running cargo build and cargo doc on the latest nightly.

@AlexTMjugador AlexTMjugador marked this pull request as ready for review May 2, 2022 19:25
@AlexTMjugador AlexTMjugador changed the title Use OUT_DIR as a data directory in build scripts Do not write to user data directories on build. Fix docs.rs documentation generation May 2, 2022
@smoelius
Copy link
Member

smoelius commented May 2, 2022

Tested by making the usual build artifacts folder a symlink to /dev/null on a Linux machine, so any I/O write attempt to it would fail, and then running cargo build and cargo doc on the latest nightly.

That is very(!) clever. But if I am understanding correctly, this only tells us that building for doc.rs is likely to succeed. We still have the issue of building for conventional platforms, and this line concerns me:

env::var("OUT_DIR").map_or_else(

Won't this line break non-doc.rs builds? In other words, won't OUT_DIR be set when afl.rs's build script is run for platforms other than doc.rs?

I am new to this issue, so please forgive me if my comments are completely off-base.

But assuming that's right, I wonder how hard it would be to incorporate one of the techniques mentioned in this issue: rust-lang/docs.rs#147

@AlexTMjugador
Copy link
Contributor Author

Won't this line break non-doc.rs builds? In other words, won't OUT_DIR be set when afl.rs's build script is run for platforms other than doc.rs?

OUT_DIR will be set for non-doc.rs builds too, but so far I have not noticed any issue with it.

However, to play it safe, I could refactor the code to only pay attention to OUT_DIR if the build is being done on a docs.rs environment. Do you think this would be a welcome change? 😄

@smoelius
Copy link
Member

smoelius commented May 3, 2022

OUT_DIR will be set for non-doc.rs builds too, but so far I have not noticed any issue with it.

Well, let me make sure I am not misunderstanding. When I build this, I see the following directories created in my target directory:

target/debug/build/afl-dbc0a948e6d7c62c/out/afl
target/debug/build/afl-dbc0a948e6d7c62c/out/afl-llvm-rt

I am afraid they are being created there instead of in:

$HOME/.local/share/afl.rs/rustc-1.60.0-7737e0b/afl.rs-0.12.2

Am I making a mistake?

However, to play it safe, I could refactor the code to only pay attention to OUT_DIR if the build is being done on a docs.rs environment. Do you think this would be a welcome change? smile

Yes, and thank you for doing this. We should definitely get this working.

Also, I noticed this: https://docs.rs/about/builds#detecting-docsrs

@AlexTMjugador
Copy link
Contributor Author

AlexTMjugador commented May 8, 2022

Am I making a mistake?

No, you're not making any mistakes 😄. That was an intended change on my part, and as I've said I didn't notice anything wrong with it.

However, I didn't look too deep into the assumptions the code makes about the directory layout, so in the commits I've just pushed I brought back the previous behavior for non-docs.rs builds as discussed. I'm fairly sure that for docs.rs builds the changes made by this PR are appropriate.

// The Cargo documentation recommends that build scripts
// place their generated files at OUT_DIR too, but we
// don't change that for now for normal builds.
if cfg!(docsrs) {
Copy link
Member

Choose a reason for hiding this comment

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

I was expecting to see if std::env::var("DOCS_RS").is_ok() here.

Is there a reason to prefer this approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This source file is used by both the build script and the application code. By checking for the feature flag we can prevent the DOCS_RS environment variable from interferring with the application code, and make sure that this code path is taken only on the intended environment.

Copy link
Member

Choose a reason for hiding this comment

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

That's a good argument. Let's see what happens.

@smoelius smoelius merged commit f40d69e into rust-fuzz:master May 9, 2022
@AlexTMjugador
Copy link
Contributor Author

AlexTMjugador commented May 9, 2022

It's sad that the documentation build still fails, but now at least now we know it's because the docrs feature is not set.

Maybe we should set rustc-args on the docs.rs Cargo metadata section instead. Anyway, because I do not fancy the idea of doing shotgun debugging that much, I'll try to set up a docs.rs container to test the changes more thoroughly when I have more free time, and then submit a hopefully more definitive PR. Sorry about this 😅

@smoelius
Copy link
Member

smoelius commented May 9, 2022

Maybe we should set rustc-args on the docs.rs Cargo metadata section instead.

I think this is worth a shot.

@smoelius
Copy link
Member

smoelius commented May 9, 2022

Huh. No change: https://docs.rs/crate/afl/0.12.4/builds/555863

@smoelius
Copy link
Member

Here is the rustdoc command from the most recent build logs (note the --target option at the end):

"/opt/rustwide/cargo-home/bin/cargo" "+nightly" "rustdoc" "--lib" "-Zrustdoc-map" "-Z" "unstable-options" "--config" "build.rustflags=[\"--cfg\", \"docsrs\"]" "--config" "build.rustdocflags=[\"-Z\", \"unstable-options\", \"--emit=invocation-specific\", \"--resource-suffix\", \"-20220508-1.62.0-nightly-cb1219871\", \"--static-root-path\", \"/\", \"--cap-lints\", \"warn\", \"--disable-per-crate-search\"]" "-Zunstable-options" "--config=doc.extern-map.registries.crates-io=\"https://docs.rs/{pkg_name}/{version}/x86_64-unknown-linux-gnu\"" "-j3" "--target" "x86_64-unknown-linux-gnu"

If I run this command, a target/x86_64-unknown-linux-gnu/debug/build/afl-035f5540df137eb5/out directory is created, but nothing is placed inside it.

But if I remove the --target option, then a target/debug/build/afl-af566d4caf516f58/out directory is created and afl and afl-llvm-rt are created inside there.

Also, I noticed this in the Configuration keys section of the Cargo Book:

[target.$triple]
...
# custom flags to pass to all compiler invocations that target $triple
# this value overrides build.rustflags when both are present
rustflags = ["..", ".."]

This suggests to me that rustdoc is silently passing target.x86_64-unknown-linux-gnu configuration options to Cargo. This would also explain why the package.metadata.docs.rs option is listed under Cross Compiling, as opposed to elsewhere.

@AlexTMjugador
Copy link
Contributor Author

Good catch! The target specification may have something to do with it. I'll think about it.

@smoelius
Copy link
Member

You raised a good point here.

One thought I had was, maybe we switch to checking the DOCS_RS environment variable, but only during the build script's execution.

@smoelius
Copy link
Member

smoelius commented Jun 2, 2022

@AlexTMjugador I feel like we were close to getting this over the finish line.

Do you think you might still work on this?

@AlexTMjugador
Copy link
Contributor Author

I want to work on this (and other things), but I don't have much free time available right now. I hope to be more available in a week or two.

@AlexTMjugador
Copy link
Contributor Author

This docs.rs generation issue is trickier than I anticipated. I might get back to it at some point, but at the moment I'm not doing it. If anyone else is interested in taking care of it, feel free to let me know and go ahead 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants