Add codegen/runtime version poison pill, enforced in cargo builds.

PiperOrigin-RevId: 705980172
pull/19657/head
Protobuf Team Bot 2 months ago committed by Copybara-Service
parent 2559176061
commit 36046e3d93
  1. 43
      rust/internal.rs
  2. 1
      src/google/protobuf/compiler/rust/BUILD.bazel
  3. 11
      src/google/protobuf/compiler/rust/generator.cc
  4. 3
      src/google/protobuf/compiler/rust/message.cc
  5. 1
      src/google/protobuf/compiler/versions.h

@ -66,3 +66,46 @@ pub fn get_map_default_value<K: Proxied, V: map::ProxiedInMapValue<K> + Default>
) -> V {
Default::default()
}
/// A function that is used to assert that the generated code is compatible with
/// the current runtime version. Right now a perfect/exact match with zero skew
/// is require, except any -prerelease suffixes are ignored as long it is
/// present on both. This may be relaxed in the future.
///
/// As the generated code is permitted to use unstable internal APIs, the protoc
/// used to generate the code must correspond to the runtime dependency. This
/// const fn is used to check at compile time that the right gencode is used
/// with the right runtime; if you are seeing this fail it means your protoc
/// version mismatches the Rust runtime crate version.
#[cfg(not(bzl))]
pub const fn assert_compatible_gencode_version(gencode_version: &'static str) {
// Helper since str eq is not allowed in const context. In a future rust release
// &str PartialEq will be allowed in const contexts and we can drop this.
const fn const_str_eq(lhs: &str, rhs: &str) -> bool {
let lhs = lhs.as_bytes();
let rhs = rhs.as_bytes();
if lhs.len() != rhs.len() {
return false;
}
let mut i = 0;
while i < lhs.len() {
if lhs[i] != rhs[i] {
return false;
}
i += 1;
}
true
}
let runtime_version = env!("CARGO_PKG_VERSION");
assert!(
const_str_eq(gencode_version, runtime_version),
"Gencode version is not compatible with runtime version",
)
}
/// There is no need for gencode/runtime poison pill when running in bzl; the
/// gencode using the __internal mod which is not available to checked in
/// gencode; gencode built from source should always match.
#[cfg(bzl)]
pub const fn assert_compatible_gencode_version(_gencode_version: &'static str) {}

@ -25,6 +25,7 @@ cc_library(
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:versions",
"//src/google/protobuf/compiler/cpp:names",
"//src/google/protobuf/io",
"//src/google/protobuf/io:printer",

@ -18,7 +18,9 @@
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "absl/types/span.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/cpp/names.h"
@ -28,6 +30,7 @@
#include "google/protobuf/compiler/rust/message.h"
#include "google/protobuf/compiler/rust/naming.h"
#include "google/protobuf/compiler/rust/relative_path.h"
#include "google/protobuf/compiler/versions.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/io/printer.h"
@ -164,6 +167,14 @@ bool RustGenerator::Generate(const FileDescriptor* file,
{"Option", "::std::option::Option"},
});
std::string expected_runtime_version = absl::StrCat(
absl::StripSuffix(PROTOBUF_RUST_VERSION_STRING, "-dev"), "-beta");
ctx.Emit({{"expected_runtime_version", expected_runtime_version}},
R"rs(
const _: () = $pbi$::assert_compatible_gencode_version("$expected_runtime_version$");
)rs");
std::vector<const FileDescriptor*> file_contexts(
files_in_current_crate.begin(), files_in_current_crate.end());

@ -7,11 +7,8 @@
#include "google/protobuf/compiler/rust/message.h"
#include <string>
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/compiler/cpp/names.h"

@ -56,6 +56,7 @@
#define PROTOBUF_CPP_VERSION_STRING "5.30.0-dev"
#define PROTOBUF_JAVA_VERSION_STRING "4.30.0-dev"
#define PROTOBUF_PYTHON_VERSION_STRING "5.30.0-dev"
#define PROTOBUF_RUST_VERSION_STRING "4.30.0-dev"
namespace google {

Loading…
Cancel
Save