Rollback version validations in Protobuf Java Lite.

PiperOrigin-RevId: 601122259
pull/15551/head
Protobuf Team Bot 10 months ago committed by Copybara-Service
parent 4517308a91
commit 6f86726f26
  1. 60
      java/core/src/main/java/com/google/protobuf/RuntimeVersion.java
  2. 74
      java/core/src/test/java/com/google/protobuf/RuntimeVersionTest.java
  3. 1
      java/lite/pom.xml
  4. 2
      src/google/protobuf/compiler/java/enum.cc
  5. 10
      src/google/protobuf/compiler/java/enum_lite.cc
  6. 14
      src/google/protobuf/compiler/java/file.cc
  7. 8
      src/google/protobuf/compiler/java/helpers.cc
  8. 1
      src/google/protobuf/compiler/java/helpers.h
  9. 2
      src/google/protobuf/compiler/java/message.cc
  10. 10
      src/google/protobuf/compiler/java/message_lite.cc
  11. 2
      src/google/protobuf/compiler/java/shared_code_generator.cc

@ -22,20 +22,6 @@ public final class RuntimeVersion {
PUBLIC,
}
/** Indicates whether last checked runtime is the full or lite version. */
protected enum Fullness {
UNSPECIFIED,
FULL,
LITE
}
// Fields to the altered when initializing generated classes.
@SuppressWarnings({"ProtectedMembersInFinalClass", "NonFinalStaticField"})
protected static Fullness prevFullness = Fullness.UNSPECIFIED;
@SuppressWarnings("NonFinalStaticField")
private static String prevCheckedLocation = "";
// The version of this runtime.
// Automatically updated by Protobuf release process. Do not edit manually.
@ -48,32 +34,6 @@ public final class RuntimeVersion {
private static final String VERSION_STRING = versionString(MAJOR, MINOR, PATCH, SUFFIX);
private static final Logger logger = Logger.getLogger(RuntimeVersion.class.getName());
/**
* Validates that the gencode version is compatible with this runtime version according to
* https://protobuf.dev/support/cross-version-runtime-guarantee/.
*
* <p>This method is currently only used by Protobuf Java **lite version** gencode. Do not call it
* elsewhere.
*
* @param domain the domain where Protobuf Java code was generated.
* @param major the major version of Protobuf Java gencode.
* @param minor the minor version of Protobuf Java gencode.
* @param patch the micro/patch version of Protobuf Java gencode.
* @param suffix the version suffix e.g. "-rc2", "-dev", etc.
* @param location the debugging location e.g. generated Java class to put in the error messages.
* @throws ProtobufRuntimeVersionException if versions are incompatible.
*/
public static void validateProtobufLiteGencodeVersion(
RuntimeDomain domain, int major, int minor, int patch, String suffix, String location) {
if (checkDisabled()) {
return;
}
synchronized (RuntimeVersion.class) {
preventFullAndLite(Fullness.LITE, location);
}
validateProtobufGencodeVersionImpl(domain, major, minor, patch, suffix, location);
}
/**
* Validates that the gencode version is compatible with this runtime version according to
* https://protobuf.dev/support/cross-version-runtime-guarantee/.
@ -94,9 +54,6 @@ public final class RuntimeVersion {
if (checkDisabled()) {
return;
}
synchronized (RuntimeVersion.class) {
preventFullAndLite(Fullness.FULL, location);
}
validateProtobufGencodeVersionImpl(domain, major, minor, patch, suffix, location);
}
@ -174,22 +131,5 @@ public final class RuntimeVersion {
return false;
}
/** Verifies that only full or lite linkage exists. */
private static void preventFullAndLite(Fullness fullness, String location) {
if (fullness != prevFullness && prevFullness != Fullness.UNSPECIFIED) {
String message =
"Protobuf Java version checker saw both Lite and Full linkages during runtime, which is"
+ " disallowed. Full gencode @%s; Lite gencode @%s";
if (prevFullness == Fullness.FULL) {
message = String.format(message, prevCheckedLocation, location);
} else {
message = String.format(message, location, prevCheckedLocation);
}
throw new ProtobufRuntimeVersionException(message);
}
prevFullness = fullness;
prevCheckedLocation = location;
}
private RuntimeVersion() {}
}

@ -23,7 +23,7 @@ public final class RuntimeVersionTest {
assertThrows(
RuntimeVersion.ProtobufRuntimeVersionException.class,
() ->
RuntimeVersion.validateProtobufLiteGencodeVersion(
RuntimeVersion.validateProtobufGencodeVersion(
RuntimeVersion.DOMAIN, 1, -2, -3, "", "dummy"));
assertThat(thrown).hasMessageThat().contains("Invalid gencode version: 1.-2.-3");
}
@ -37,7 +37,7 @@ public final class RuntimeVersionTest {
assertThrows(
RuntimeVersion.ProtobufRuntimeVersionException.class,
() ->
RuntimeVersion.validateProtobufLiteGencodeVersion(
RuntimeVersion.validateProtobufGencodeVersion(
gencodeDomain, 1, 2, 3, "", "testing.Foo"));
assertThat(thrown)
.hasMessageThat()
@ -51,7 +51,7 @@ public final class RuntimeVersionTest {
assertThrows(
RuntimeVersion.ProtobufRuntimeVersionException.class,
() ->
RuntimeVersion.validateProtobufLiteGencodeVersion(
RuntimeVersion.validateProtobufGencodeVersion(
RuntimeVersion.DOMAIN,
gencodeMajor,
RuntimeVersion.MINOR,
@ -66,7 +66,7 @@ public final class RuntimeVersionTest {
@Test
public void versionValidation_versionNumbersAllTheSameAllowed() {
RuntimeVersion.validateProtobufLiteGencodeVersion(
RuntimeVersion.validateProtobufGencodeVersion(
RuntimeVersion.DOMAIN,
RuntimeVersion.MAJOR,
RuntimeVersion.MINOR,
@ -78,7 +78,7 @@ public final class RuntimeVersionTest {
@Test
public void versionValidation_newerRuntimeVersionAllowed() {
int gencodeMinor = RuntimeVersion.MINOR - 1;
RuntimeVersion.validateProtobufLiteGencodeVersion(
RuntimeVersion.validateProtobufGencodeVersion(
RuntimeVersion.DOMAIN,
RuntimeVersion.MAJOR,
gencodeMinor,
@ -94,7 +94,7 @@ public final class RuntimeVersionTest {
assertThrows(
RuntimeVersion.ProtobufRuntimeVersionException.class,
() ->
RuntimeVersion.validateProtobufLiteGencodeVersion(
RuntimeVersion.validateProtobufGencodeVersion(
RuntimeVersion.DOMAIN,
RuntimeVersion.MAJOR,
gencodeMinor,
@ -111,7 +111,7 @@ public final class RuntimeVersionTest {
assertThrows(
RuntimeVersion.ProtobufRuntimeVersionException.class,
() ->
RuntimeVersion.validateProtobufLiteGencodeVersion(
RuntimeVersion.validateProtobufGencodeVersion(
RuntimeVersion.DOMAIN,
RuntimeVersion.MAJOR,
RuntimeVersion.MINOR,
@ -131,7 +131,7 @@ public final class RuntimeVersionTest {
assertThrows(
RuntimeVersion.ProtobufRuntimeVersionException.class,
() ->
RuntimeVersion.validateProtobufLiteGencodeVersion(
RuntimeVersion.validateProtobufGencodeVersion(
RuntimeVersion.DOMAIN,
RuntimeVersion.MAJOR,
RuntimeVersion.MINOR,
@ -144,62 +144,4 @@ public final class RuntimeVersionTest {
"Detected mismatched Protobuf Gencode/Runtime version suffixes when loading"
+ " testing.Foo");
}
@Test
public void versionValidation_illegalLiteAndThenFullLinkage() {
RuntimeVersion.prevFullness = RuntimeVersion.Fullness.UNSPECIFIED;
RuntimeVersion.validateProtobufLiteGencodeVersion(
RuntimeVersion.DOMAIN,
RuntimeVersion.MAJOR,
RuntimeVersion.MINOR,
RuntimeVersion.PATCH,
RuntimeVersion.SUFFIX,
"testing.Foo");
RuntimeVersion.ProtobufRuntimeVersionException thrown =
assertThrows(
RuntimeVersion.ProtobufRuntimeVersionException.class,
() ->
RuntimeVersion.validateProtobufGencodeVersion(
RuntimeVersion.DOMAIN,
RuntimeVersion.MAJOR,
RuntimeVersion.MINOR,
RuntimeVersion.PATCH,
RuntimeVersion.SUFFIX,
"testing.Bar"));
assertThat(thrown)
.hasMessageThat()
.contains(
"Protobuf Java version checker saw both Lite and Full linkages during runtime, which is"
+ " disallowed. Full gencode @testing.Bar; Lite gencode @testing.Foo");
RuntimeVersion.prevFullness = RuntimeVersion.Fullness.UNSPECIFIED;
}
@Test
public void versionValidation_illegalFullAndThenLiteLinkage_warning() {
RuntimeVersion.prevFullness = RuntimeVersion.Fullness.UNSPECIFIED;
RuntimeVersion.validateProtobufGencodeVersion(
RuntimeVersion.DOMAIN,
RuntimeVersion.MAJOR,
RuntimeVersion.MINOR,
RuntimeVersion.PATCH,
RuntimeVersion.SUFFIX,
"testing.Foo");
RuntimeVersion.ProtobufRuntimeVersionException thrown =
assertThrows(
RuntimeVersion.ProtobufRuntimeVersionException.class,
() ->
RuntimeVersion.validateProtobufLiteGencodeVersion(
RuntimeVersion.DOMAIN,
RuntimeVersion.MAJOR,
RuntimeVersion.MINOR,
RuntimeVersion.PATCH,
RuntimeVersion.SUFFIX,
"testing.Bar"));
assertThat(thrown)
.hasMessageThat()
.contains(
"Protobuf Java version checker saw both Lite and Full linkages during runtime, which is"
+ " disallowed. Full gencode @testing.Foo; Lite gencode @testing.Bar");
RuntimeVersion.prevFullness = RuntimeVersion.Fullness.UNSPECIFIED;
}
}

@ -225,7 +225,6 @@
<exclude>Proto2SchemaTest.java</exclude>
<exclude>Proto2UnknownEnumValueTest.java</exclude>
<exclude>RepeatedFieldBuilderTest.java</exclude>
<exclude>RuntimeVersionTest.java</exclude>
<exclude>ServiceTest.java</exclude>
<exclude>SingleFieldBuilderTest.java</exclude>
<exclude>TestBadIdentifiers.java</exclude>

@ -114,7 +114,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
printer->Print("static {\n");
printer->Indent();
PrintGencodeVersionValidator(printer, context_->options().opensource_runtime,
context_->EnforceLite(), descriptor_->name());
descriptor_->name());
printer->Outdent();
printer->Print("}\n");

@ -85,16 +85,6 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
// -----------------------------------------------------------------
if (context_->options().opensource_runtime) {
printer->Print("static {\n");
printer->Indent();
PrintGencodeVersionValidator(printer,
context_->options().opensource_runtime,
context_->EnforceLite(), descriptor_->name());
printer->Outdent();
printer->Print("}\n");
}
for (int i = 0; i < aliases_.size(); i++) {
absl::flat_hash_map<absl::string_view, std::string> vars;
vars["classname"] = descriptor_->name();

@ -273,12 +273,14 @@ void FileGenerator::Generate(io::Printer* printer) {
printer->Annotate("classname", file_->name());
printer->Indent();
printer->Print("static {\n");
printer->Indent();
PrintGencodeVersionValidator(printer, options_.opensource_runtime,
context_->EnforceLite(), classname_);
printer->Outdent();
printer->Print("}\n");
if (!context_->EnforceLite()) {
printer->Print("static {\n");
printer->Indent();
PrintGencodeVersionValidator(printer, options_.opensource_runtime,
classname_);
printer->Outdent();
printer->Print("}\n");
}
// -----------------------------------------------------------------

@ -86,22 +86,18 @@ void PrintEnumVerifierLogic(
}
void PrintGencodeVersionValidator(io::Printer* printer, bool oss_runtime,
bool enforce_lite,
absl::string_view java_class_name) {
if (oss_runtime) {
const auto& version = GetProtobufJavaVersion();
printer->Print(
"com.google.protobuf.RuntimeVersion.validateProtobuf$lite$"
"GencodeVersion("
"\n"
"com.google.protobuf.RuntimeVersion.validateProtobufGencodeVersion(\n"
" com.google.protobuf.RuntimeVersion.RuntimeDomain.PUBLIC,\n"
" $major$,\n"
" $minor$,\n"
" $patch$,\n"
" $suffix$,\n"
" $location$);\n",
"lite", enforce_lite ? "Lite" : "", "major",
absl::StrCat("/* major= */ ", version.major()), "minor",
"major", absl::StrCat("/* major= */ ", version.major()), "minor",
absl::StrCat("/* minor= */ ", version.minor()), "patch",
absl::StrCat("/* patch= */ ", version.patch()), "suffix",
absl::StrCat("/* suffix= */ \"", version.suffix(), "\""), "location",

@ -62,7 +62,6 @@ void PrintEnumVerifierLogic(
// Prints the Protobuf Java Version validator checking that the runtime and
// gencode versions are compatible.
void PrintGencodeVersionValidator(io::Printer* printer, bool oss_runtime,
bool enforce_lite,
absl::string_view java_class_name);
// Converts a name to camel-case. If cap_first_letter is true, capitalize the

@ -336,7 +336,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
printer->Print("static {\n");
printer->Indent();
PrintGencodeVersionValidator(printer, context_->options().opensource_runtime,
context_->EnforceLite(), descriptor_->name());
descriptor_->name());
printer->Outdent();
printer->Print("}\n");

@ -181,16 +181,6 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) {
printer->Annotate("{", "}", descriptor_);
printer->Indent();
if (context_->options().opensource_runtime) {
printer->Print("static {\n");
printer->Indent();
PrintGencodeVersionValidator(printer,
context_->options().opensource_runtime,
context_->EnforceLite(), descriptor_->name());
printer->Outdent();
printer->Print("}\n");
}
GenerateConstructor(printer);
// Nested types

@ -102,7 +102,7 @@ void SharedCodeGenerator::Generate(
printer->Indent();
GenerateDescriptors(printer.get());
PrintGencodeVersionValidator(printer.get(), options_.opensource_runtime,
options_.enforce_lite, classname);
classname);
printer->Outdent();
printer->Outdent();
printer->Print(

Loading…
Cancel
Save