Skip to content

Commit 74a4a46

Browse files
David Tolnayfacebook-github-bot
David Tolnay
authored andcommitted
Make setup_protoc() unsafe
Summary: This function mutates environment variables using `env::set_var`, which is unsafe on some platforms if anything is simultaneously reading the environment from non-Rust code. https://doc.rust-lang.org/edition-guide/rust-2024/newly-unsafe-functions.html#stdenvset_var-remove_var Reviewed By: JakobDegen Differential Revision: D74377239 fbshipit-source-id: 5e3dcef9276d9f37460a58dbcf3e557722243788
1 parent abdb211 commit 74a4a46

File tree

13 files changed

+49
-45
lines changed

13 files changed

+49
-45
lines changed

app/buck2_action_metadata_proto/build.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ fn main() -> io::Result<()> {
1919
vec![".".to_owned()]
2020
};
2121

22-
buck2_protoc_dev::configure()
23-
.setup_protoc()
24-
.compile(proto_files, &includes)
22+
let builder = buck2_protoc_dev::configure();
23+
unsafe { builder.setup_protoc() }.compile(proto_files, &includes)
2524
}

app/buck2_cli_proto/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ fn main() -> io::Result<()> {
2424
]
2525
};
2626

27-
buck2_protoc_dev::configure()
28-
.setup_protoc()
27+
let builder = buck2_protoc_dev::configure();
28+
unsafe { builder.setup_protoc() }
2929
.type_attribute(".", "#[derive(::serde::Serialize, ::serde::Deserialize)] #[serde(rename_all = \"snake_case\")]")
3030
.type_attribute(".", "#[derive(::allocative::Allocative)]")
3131
.field_attribute("start_time", "#[serde(with = \"serialize_timestamp\")]")

app/buck2_data/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ fn main() -> io::Result<()> {
1919
vec![".".to_owned(), "../buck2_host_sharing_proto".to_owned()]
2020
};
2121

22-
buck2_protoc_dev::configure()
23-
.setup_protoc()
22+
let builder = buck2_protoc_dev::configure();
23+
unsafe { builder.setup_protoc() }
2424
.type_attribute(
2525
"buck.data.BuckEvent.data",
2626
"#[allow(clippy::large_enum_variant)]",

app/buck2_downward_api_proto/build.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ fn main() -> io::Result<()> {
1919
vec![".".to_owned()]
2020
};
2121

22-
buck2_protoc_dev::configure()
23-
.setup_protoc()
24-
.compile(proto_files, &includes)
22+
let builder = buck2_protoc_dev::configure();
23+
unsafe { builder.setup_protoc() }.compile(proto_files, &includes)
2524
}

app/buck2_forkserver_proto/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ fn main() -> io::Result<()> {
2323
]
2424
};
2525

26-
buck2_protoc_dev::configure()
27-
.setup_protoc()
26+
let builder = buck2_protoc_dev::configure();
27+
unsafe { builder.setup_protoc() }
2828
.type_attribute(
2929
"buck.forkserver.RequestEvent.data",
3030
"#[derive(::derive_more::From, ::gazebo::variants::VariantName, ::gazebo::variants::UnpackVariants)]",

app/buck2_health_check_proto/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ fn main() -> io::Result<()> {
2323
]
2424
};
2525

26-
buck2_protoc_dev::configure()
27-
.setup_protoc()
26+
let builder = buck2_protoc_dev::configure();
27+
unsafe { builder.setup_protoc() }
2828
.extern_path(".buck.data", "::buck2_data")
2929
.compile(proto_files, &includes)
3030
}

app/buck2_host_sharing_proto/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ fn main() -> io::Result<()> {
1919
vec![".".to_owned()]
2020
};
2121

22-
buck2_protoc_dev::configure()
23-
.setup_protoc()
22+
let builder = buck2_protoc_dev::configure();
23+
unsafe { builder.setup_protoc() }
2424
.type_attribute(".", "#[derive(::allocative::Allocative)]")
2525
.type_attribute(".", "#[derive(::serde::Serialize, ::serde::Deserialize)]")
2626
.extern_path(".buck.data", "::buck2_data")

app/buck2_install_proto/build.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ fn main() -> io::Result<()> {
1919
vec![".".to_owned()]
2020
};
2121

22-
buck2_protoc_dev::configure()
23-
.setup_protoc()
24-
.compile(proto_files, &includes)
22+
let builder = buck2_protoc_dev::configure();
23+
unsafe { builder.setup_protoc() }.compile(proto_files, &includes)
2524
}

app/buck2_protoc_dev/src/lib.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ fn get_env(key: &str) -> Option<OsString> {
2222
}
2323

2424
#[cfg(not(buck2_build))]
25-
fn set_var(var: &str, override_var: &str, path: Result<PathBuf, protoc_bin_vendored::Error>) {
25+
unsafe fn set_var(
26+
var: &str,
27+
override_var: &str,
28+
path: Result<PathBuf, protoc_bin_vendored::Error>,
29+
) {
2630
let path = if let Some(override_var_value) = env::var_os(override_var) {
2731
eprintln!("INFO: Variable ${} is overridden by ${}", var, override_var);
2832
PathBuf::from(override_var_value)
@@ -40,13 +44,13 @@ fn set_var(var: &str, override_var: &str, path: Result<PathBuf, protoc_bin_vendo
4044

4145
let path = dunce::canonicalize(path).expect("Failed to canonicalize path");
4246
eprintln!("INFO: Variable ${} set to {:?}", var, path);
43-
env::set_var(var, path);
47+
unsafe { env::set_var(var, path) };
4448
}
4549

4650
/// Set up $PROTOC to point to the in repo binary if available.
4751
///
4852
/// Note: repo root is expected to be a relative or absolute path to the root of the repository.
49-
fn maybe_set_protoc() {
53+
unsafe fn maybe_set_protoc() {
5054
#[cfg(not(buck2_build))]
5155
{
5256
// `cargo build` of `buck2` does not require external `protoc` dependency
@@ -55,23 +59,27 @@ fn maybe_set_protoc() {
5559
// https://github.com/facebook/buck2/issues/65
5660
// So for NixOS builds path to `protoc` binary can be overridden with
5761
// `BUCK2_BUILD_PROTOC` environment variable.
58-
set_var(
59-
"PROTOC",
60-
"BUCK2_BUILD_PROTOC",
61-
protoc_bin_vendored::protoc_bin_path(),
62-
);
62+
unsafe {
63+
set_var(
64+
"PROTOC",
65+
"BUCK2_BUILD_PROTOC",
66+
protoc_bin_vendored::protoc_bin_path(),
67+
);
68+
}
6369
}
6470
}
6571

6672
/// Set $PROTOC_INCLUDE.
67-
fn maybe_set_protoc_include() {
73+
unsafe fn maybe_set_protoc_include() {
6874
#[cfg(not(buck2_build))]
6975
{
70-
set_var(
71-
"PROTOC_INCLUDE",
72-
"BUCK2_BUILD_PROTOC_INCLUDE",
73-
protoc_bin_vendored::include_path(),
74-
);
76+
unsafe {
77+
set_var(
78+
"PROTOC_INCLUDE",
79+
"BUCK2_BUILD_PROTOC_INCLUDE",
80+
protoc_bin_vendored::include_path(),
81+
);
82+
}
7583
}
7684
}
7785

@@ -112,10 +120,10 @@ impl Builder {
112120
}
113121
}
114122

115-
pub fn setup_protoc(self) -> Self {
123+
pub unsafe fn setup_protoc(self) -> Self {
116124
// It would be great if there were on the config rather than an env variables...
117-
maybe_set_protoc();
118-
maybe_set_protoc_include();
125+
unsafe { maybe_set_protoc() };
126+
unsafe { maybe_set_protoc_include() };
119127
self
120128
}
121129

app/buck2_subscription_proto/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ fn main() -> io::Result<()> {
1919
vec![".".to_owned()]
2020
};
2121

22-
buck2_protoc_dev::configure()
23-
.setup_protoc()
22+
let builder = buck2_protoc_dev::configure();
23+
unsafe { builder.setup_protoc() }
2424
.type_attribute(".", "#[derive(::serde::Serialize, ::serde::Deserialize)]")
2525
.type_attribute(".", "#[derive(::allocative::Allocative)]")
2626
.type_attribute(

app/buck2_test_proto/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ fn main() -> io::Result<()> {
2323
]
2424
};
2525

26-
buck2_protoc_dev::configure()
27-
.setup_protoc()
26+
let builder = buck2_protoc_dev::configure();
27+
unsafe { builder.setup_protoc() }
2828
.type_attribute(
2929
"buck.test.ExecuteResponse2.response",
3030
"#[allow(clippy::large_enum_variant)]",

app/buck2_worker_proto/build.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ fn main() -> io::Result<()> {
1919
vec![".".to_owned(), "../buck2_data".to_owned()]
2020
};
2121

22-
buck2_protoc_dev::configure()
23-
.setup_protoc()
24-
.compile(proto_files, &includes)
22+
let builder = buck2_protoc_dev::configure();
23+
unsafe { builder.setup_protoc() }.compile(proto_files, &includes)
2524
}

remote_execution/oss/re_grpc_proto/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ fn main() -> io::Result<()> {
2222
"proto/google/rpc/status.proto",
2323
];
2424

25-
buck2_protoc_dev::configure()
26-
.setup_protoc()
25+
let builder = buck2_protoc_dev::configure();
26+
unsafe { builder.setup_protoc() }
2727
.type_attribute(".", "#[derive(::serde::Serialize, ::serde::Deserialize)]")
2828
.field_attribute(
2929
"build.bazel.remote.execution.v2.Action.timeout",

0 commit comments

Comments
 (0)