Commit graph

23923 commits

Author SHA1 Message Date
Samuel Benzaquen
e12fe75fc7 Use the enum validation data for dynamic extensions too.
This removes an unnecessary branch in validation code and will allow reducing
the size of ExtensionInfo in a future change.

PiperOrigin-RevId: 967326524
2026-08-19 11:48:44 -07:00
Samuel Benzaquen
2c1ea6b8c9 Hide the synthetic MapEntry class in the pb.cc file.
This reduces the cost of including the headers.

PiperOrigin-RevId: 967299746
2026-08-19 11:00:02 -07:00
Protobuf Team Bot
d21931d398 Remove C23 requirement from FastTable.
Provide dummy argument for variadic macro to satisfy pre-C23 requirements.

Remove trailing `;` that duplicates `;` generated by macro expansion.

PiperOrigin-RevId: 967288052
2026-08-19 10:38:05 -07:00
Samuel Benzaquen
7ef3576267 Continue cleaning up after the GLOBALS experiment.
Clean up ExtensionInfo to contain a ClassData* instead.

PiperOrigin-RevId: 967283147
2026-08-19 10:27:48 -07:00
Protobuf Team Bot
c1d8a858cd Short-circuit empty packed fields in CodedInputStream.countPackedVarints
0-length packed fields occur but are fairly rare since our serializers will emit nothing instead of emitting a 0-length packed field. But, there's little reason to leave inequality case behind since we're checking against <=0 anyway.

PiperOrigin-RevId: 967271476
2026-08-19 10:08:51 -07:00
Samuel Benzaquen
c5055727eb Continue cleaning up after the GLOBALS experiment.
Remove some preprocessor branching in the runtime.

PiperOrigin-RevId: 967240247
2026-08-19 09:05:21 -07:00
Samuel Benzaquen
1a23830881 [Py/C++] Fixed data race in Python free threading by removing obsolete hack
We previously had a hack in Python/C++ Protobuf to account for the fact that LazyField did not properly remember a custom DescriptorPool or MessageFactory that was set in the ParseContext at parse time.

The code has since been fixed to properly handle the case where ParseContext contains a custom DescriptorPool/MessageFactory.  Removing the hack removes the data race under free threading.

PiperOrigin-RevId: 967236272
2026-08-19 08:59:26 -07:00
Ada Zhang
2256d8f028 Support converting non-canonical extensions in upb message conversion.
PiperOrigin-RevId: 966915077
2026-08-18 18:24:53 -07:00
Protobuf Team Bot
6165c52793 Validate tag match before treating unlinked submessages as unknown fields.
Otherwise colliding fields will incorrectly end up in unknowns.

PiperOrigin-RevId: 966858004
2026-08-18 16:10:58 -07:00
Ada Zhang
d068b54ba6 Internal
PiperOrigin-RevId: 966853376
2026-08-18 16:07:00 -07:00
Protobuf Team Bot
37f78578e7 - Updates DslList and ExtensionList to take a lazy supplier lambda () -> List<E>, retaining secondary constructors for binary backward compatibility with existing compiled proto code.
PiperOrigin-RevId: 966853174
2026-08-18 16:01:51 -07:00
Protobuf Team Bot
6e4f4ca54a Fix UPB_FASTTABLE declaration to avoid errors with recursive C macro expansion.
Place "hidden" attribute behind macro conditioned on compiler support.

Disable UPB_FASTTABLE on Windows, as MSVC doesn't support required compiler features. (Existing detection can be confounded on Windows for clang-cl, which may run in normal mode during codegen but compile final binaries in MSVC compat mode.)

PiperOrigin-RevId: 966832345
2026-08-18 15:17:19 -07:00
Tony Liao
f9f94388a3 Additional unit tests for Python handling of custom JSON enum names.
The unit tests added in the initial implementation in cl/925012500 and
cl/922729754 were fairly minimal. I took some inspiration from our C++
and Java unit tests for JSON's custom enum name parsing to come up with
these new test cases.

This change also adds a python Benchmark that we can use to verify that
our optimizations in cl/952170251 actually works.

Benchmark results across all three proto implementations (cpp, python, upb):

=== C++ Extension Protos (json_format_benchmark_cpp_protos) ===
[BENCHMARK] ParseJsonDefault:                med 8.70 us/op | p99 9.56 us/op | mean 8.74 ± 0.15 us/op
[BENCHMARK] ParseJsonCustom:                 med 17.39 us/op | p99 21.64 us/op | mean 17.43 ± 0.51 us/op
[BENCHMARK] ParseJsonUnknownIgnored:         med 26.97 us/op | p99 32.04 us/op | mean 27.06 ± 0.65 us/op
[BENCHMARK] ParseRepeatedJsonDefault:        med 1.71 us/item | p99 1.83 us/item | mean 1.71 ± 0.02 us/item
[BENCHMARK] ParseRepeatedJsonCustom:         med 9.18 us/item | p99 9.88 us/item | mean 9.20 ± 0.11 us/item
[BENCHMARK] ParseRepeatedJsonUnknownIgnored: med 18.79 us/item | p99 19.19 us/item | mean 18.81 ± 0.13 us/item

=== UPB Extension Protos (json_format_benchmark_upb_protos) ===
[BENCHMARK] ParseJsonDefault:                med 8.70 us/op | p99 9.56 us/op | mean 8.74 ± 0.15 us/op
[BENCHMARK] ParseJsonCustom:                 med 17.39 us/op | p99 21.64 us/op | mean 17.43 ± 0.51 us/op
[BENCHMARK] ParseJsonUnknownIgnored:         med 26.97 us/op | p99 32.04 us/op | mean 27.06 ± 0.65 us/op
[BENCHMARK] ParseRepeatedJsonDefault:        med 1.71 us/item | p99 1.83 us/item | mean 1.71 ± 0.02 us/item
[BENCHMARK] ParseRepeatedJsonCustom:         med 9.18 us/item | p99 9.88 us/item | mean 9.20 ± 0.11 us/item
[BENCHMARK] ParseRepeatedJsonUnknownIgnored: med 18.79 us/item | p99 19.19 us/item | mean 18.81 ± 0.13 us/item

=== Pure Python Protos (json_format_benchmark_python_protos) ===
[BENCHMARK] ParseJsonDefault:                med 10.26 us/op | p99 11.20 us/op | mean 10.30 ± 0.25 us/op
[BENCHMARK] ParseJsonCustom:                 med 16.59 us/op | p99 18.35 us/op | mean 16.70 ± 0.45 us/op
[BENCHMARK] ParseJsonUnknownIgnored:         med 25.09 us/op | p99 28.22 us/op | mean 25.27 ± 0.65 us/op
[BENCHMARK] ParseRepeatedJsonDefault:        med 1.94 us/item | p99 3.25 us/item | mean 1.97 ± 0.14 us/item
[BENCHMARK] ParseRepeatedJsonCustom:         med 7.16 us/item | p99 10.35 us/item | mean 7.24 ± 0.39 us/item
[BENCHMARK] ParseRepeatedJsonUnknownIgnored: med 15.43 us/item | p99 19.54 us/item | mean 15.54 ± 0.49 us/item

PiperOrigin-RevId: 966820541
2026-08-18 14:57:30 -07:00
Protobuf Team Bot
86c32b72d9 Fix fasttable support in Kotlin UPB.
When fasttable is enabled, generated minitable headers transitively include internal fasttable headers (such as field_parsers.h / combinations.h / types.h). Adding field_parsers to upb_cinterop's deps under fasttable_enabled_setting ensures that these declarations are provided centrally by upb_cinterop.klib and not duplicated across individual proto libraries.

PiperOrigin-RevId: 966816120
2026-08-18 14:48:59 -07:00
Protobuf Team Bot
b9c05c1d72 Internal change.
PiperOrigin-RevId: 966801750
2026-08-18 14:19:21 -07:00
Samuel Benzaquen
c8cc70ea9b Skip RepeatedVarint.Int32Overflow test on aarch64.
PiperOrigin-RevId: 966792622
2026-08-18 14:03:17 -07:00
Samuel Benzaquen
bf0c808084 Continue cleaning up after the GLOBALS experiment.
Merge ClassDataLite and ClassDataFull into ClassData.
Remove dead constructor parameters.

PiperOrigin-RevId: 966749162
2026-08-18 12:44:28 -07:00
Protobuf Team Bot
915853d5ef Automated rollback of commit 780ba8c700.
PiperOrigin-RevId: 966636867
2026-08-18 09:27:45 -07:00
Clayton Knittel
f930654518 Check consistency of ClassData types of RepeatedPtrField members in the destructor.
The repeated field destructor caches the element destructor and calls it on each element of a repeated field, rather than going through full dynamic dispatch for each element. Currently, if the elements of a repeated field are of different types, this code may segfault in non-obvious ways (e.g. mismatch size-delete, or memory corruption). The DCHECK gives a better error message when you have repeated fields of mixed element types.

This code is only active with `enable_custom_vtable`.

PiperOrigin-RevId: 966610091
2026-08-18 08:34:15 -07:00
Protobuf Team Bot
53bc98a9f4 Internal change.
PiperOrigin-RevId: 966561020
2026-08-18 06:37:04 -07:00
Protobuf Team Bot
42879bb405 Delete examples/.bazelversion
The version number is set to 7.1.0 and the contents of the example would never work on 7.x.

Our CI always sets a version which is why this not mattering, but it appears that if you hold it exactly right bazelisk will use this version if present, but fall back to latest bazel if not. Just deleting the file so we don't let it rot again.

PiperOrigin-RevId: 966558235
2026-08-18 06:34:01 -07:00
Protobuf Team Bot
cfb934ad48 Optimize Java Protobuf packed varint counting using SWAR bitwise operations.
Implements 64-bit SWAR chunking via ByteBuffer on non-Android devices, falling back to a scalar countdown loop on Android devices. Sub-8 byte inputs bypass 64-bit chunking and fall through to scalar counting.

### JMH Microbenchmark Results (VarintCountingBenchmark)

| Payload Length | Scalar Baseline | SWAR (ByteBuffer) | Speedup |
| :--- | :--- | :--- | :--- |
| 1 B | 1.78 ns/op | 1.91 ns/op | 1.0x (parity) |
| 4 B | 3.75 ns/op | 3.65 ns/op | 1.0x (parity) |
| 8 B | 6.19 ns/op | 3.44 ns/op | 1.8x faster |
| 16 B | 9.93 ns/op | 4.00 ns/op | 2.5x faster |
| 32 B | 18.58 ns/op | 5.06 ns/op | 3.7x faster |
| 64 B | 35.50 ns/op | 7.51 ns/op | 4.7x faster |
| 128 B | 74.66 ns/op | 10.92 ns/op | 6.8x faster |
| 1024 B | 641.02 ns/op | 75.63 ns/op | 8.5x faster |

PiperOrigin-RevId: 966557820
2026-08-18 06:29:53 -07:00
Protobuf Team Bot
720e5468ce Migrate Protobuf Python C extensions to rules_python toolchain targets.
Update _message to use rules_python's py_extension macro (which
automatically injects required toolchain headers and manages shared library
extensions across platforms), and update _api_implementation.so to depend
directly on `@rules_python//python/cc:current_py_cc_headers` instead of
`@system_python`.

Using `py_extension` and `current_py_cc_headers` decouples Python C
extensions from the custom system_python.bzl script while maintaining
current host-interpreter behavior during the migration.

#test-continuous

PiperOrigin-RevId: 966345187
2026-08-17 21:03:55 -07:00
Protobuf Team Bot
80d472933a Auto-generate files after cl/966337187 2026-08-18 03:47:16 +00:00
Protobuf Team Bot
857623168c Automated rollback of commit d46a6ff074.
PiperOrigin-RevId: 966337187
2026-08-17 20:39:44 -07:00
Protobuf Team Bot
d5d2b624a6 Fix TODO(b/364873432) in Bazel cc_proto_library rules.
PiperOrigin-RevId: 966282837
2026-08-17 17:58:02 -07:00
Protobuf Team Bot
9ff0d2d197 Auto-generate files after cl/966266256 2026-08-18 00:27:25 +00:00
Protobuf Team Bot
c6b856d785 Introduce overflow checking functions for add and multiply in upb to replace error-prone manual checks
PiperOrigin-RevId: 966266256
2026-08-17 17:17:05 -07:00
Protobuf Team Bot
993604ee07 Auto-generate files after cl/966258037 2026-08-18 00:08:38 +00:00
Clayton Knittel
07eec573f3 Move static generated message helpers to a new private Helpers_ class of the message.
This change eliminates redundant debug info from being generated for `Clear` / `ByteSizeLong` / `_InternalSerialize` outside the TU compiling `.pb.cc` for a message. It does so via [constructor homing](https://blog.llvm.org/posts/2021-04-05-constructor-homing-for-debug-info/) on the `Helpers_` class.

This is only in effects when `:enable_custom_vtable=true`.

PiperOrigin-RevId: 966258037
2026-08-17 16:58:56 -07:00
Protobuf Team Bot
4374cbf43b Internal change.
PiperOrigin-RevId: 966255809
2026-08-17 16:54:30 -07:00
Charlie Beattie
b43ac42ce0 Protocol buffers support being created from memoryviews.
PiperOrigin-RevId: 966179112
2026-08-17 14:23:02 -07:00
Protobuf Team Bot
45aa47c5a1 Auto-generate files after PR #29130 2026-08-17 21:00:59 +00:00
BarakSrour
1e1a0a8c76 Validate php_class_prefix and objc_class_prefix in code generators (#29130)
## Summary

Follow-up to #28667, which added character validation for `php_namespace`, `php_metadata_namespace`, `ruby_package` and `csharp_namespace` because they are emitted directly into generated source without validation.

Two options in the same family were not covered and are still unvalidated on `main`:

| option | read at | validated before this PR |
|---|---|---|
| `php_class_prefix` | `php/names.cc` (`ClassNamePrefixImpl`) | no |
| `objc_class_prefix` | `objectivec/names.cc` (`FileClassPrefix`) | no |

`php_class_prefix` is easy to miss by inspection: the guard added in #28667 and both of its call sites are in `php_generator.cc`, while `php_class_prefix` is read from `names.cc`.

## What happens today

**PHP.** The generator emits `class <php_class_prefix><Name> extends ...`, so a prefix that closes the declaration and opens its own is emitted verbatim:

```proto
option php_class_prefix = "Pfx{} class Injected { public static function pwn() { return 'INJECTED'; } } class ";
```

`protoc --php_out` exits 0 and the generated file contains:

```php
class Pfx{} class Injected { public static function pwn() { return 'INJECTED'; } } class M extends \Google\Protobuf\Internal\Message
```

That is valid PHP (`php -l`: no syntax errors), the injected class is defined and its code runs, and `Tp\M` is still generated correctly — so nothing fails to build and nothing looks wrong.

**Objective-C.** `FileClassPrefix` returns the option verbatim, so with `objc_class_prefix = "A\";void pwn(){}//"` the value lands in `@interface` / `@implementation` declarations, a `typedef struct` tag and the `.prefix = "..."` string literal. I have not built the resulting Objective-C, so I am claiming unvalidated emission here rather than a working payload.

Worth noting separately: a prefix check already exists, but it validates style rather than characters, so it warns about a legitimate short prefix and says nothing about a hostile one:

```
objc_class_prefix = "AB"                 -> warning: Invalid 'option objc_class_prefix = "AB";'
objc_class_prefix = "A\";void pwn(){}//" -> no diagnostic at all
```

## Changes

- **PHP generator** — generalised `IsValidPhpNamespace` to `IsValidPhpOption(value, option_name, error)` so the error names the offending option, and applied it to `php_class_prefix` alongside the two existing options. The message still begins `Invalid character`, so the tests added in #28667 are unaffected.
- **Objective-C generator** — new `ValidateObjCClassPrefixChars()` rejects prefixes containing anything outside `[A-Za-z0-9_]`, run from `ValidateObjCClassPrefixes()` over every file ahead of the expected-prefixes opt-outs.
- Tests for both, in the reject/accept pattern used by #28667.

## Notes for review

Three things I'd rather flag than have you find:

1. **The Objective-C check is an allowlist, not a denylist.** The prefix is pasted into C identifiers, where nothing outside `[A-Za-z0-9_]` is ever valid, so an allowlist closes the class rather than chasing characters. I checked every `objc_class_prefix` in this repo (including the deliberately-empty one) and none would be rejected. Happy to switch to the `absl::CharSet` denylist used for PHP/Ruby/C# if you'd prefer consistency over precision.

2. **Placement matters.** The character check runs in `ValidateObjCClassPrefixes()` over every file, ahead of both documented opt-outs (`expected_prefixes_path=-` and `expected_prefixes_suppressions`), since disabling the naming *policy* should not disable a correctness check. There is a regression test per opt-out.

3. **Java is gated, but to a much narrower bar** — `java/file.cc` rejects only `;`, `\r`, `\n` and space, and its comment says it checks "only a subset". Since Java accepts `/**/` as a whitespace substitute, `java_outer_classname = "Evil/**/implements/**/Cloneable"` and `"Evil/*"` are both emitted into the class declaration on current `main`. I could not get code execution out of that (`;` blocks statement injection); the demonstrated effect is generated code that will not compile. **Not changed in this PR** — mentioning it in case you want the bar aligned, and happy to do it in a follow-up.

Also, for whatever it's worth to release planning: #28667 itself is not in any released binary yet — the validation string is absent from both `protoc 35.1` and `protoc 36.0-rc2`, since it landed after the v36 branch was cut.

## Testing

`src/google/protobuf/compiler/php/generator_unittest.cc` — `InvalidPhpClassPrefixRejected`, `ValidPhpClassPrefixAccepted`.

`src/google/protobuf/compiler/objectivec/generator_unittest.cc` (new file, plus its `cc_test` target) — `InvalidObjCClassPrefixRejected`, `InvalidObjCClassPrefixRejectedWithExpectedPrefixesDisabled`, `InvalidObjCClassPrefixRejectedWhenSuppressed`, `ValidObjCClassPrefixAccepted`, `ObjCClassPrefixWithUnderscoreAccepted`.

The two opt-out tests fail with the character check in its original location inside `ValidateObjCClassPrefix()`, and pass with it moved.

Both directions are covered, so the tests fail if the validation is removed *and* if it is made too strict.

---

_Both tests were run locally against this branch (`bazel test //src/google/protobuf/compiler/php:generator_unittest //src/google/protobuf/compiler/objectivec:generator_unittest`), and re-run with the two guards reverted to confirm they fail without the fix._

Closes #29130

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/29130 from BarakSrour:validate-class-prefix-options a2bbd8f2ea
PiperOrigin-RevId: 966161028
2026-08-17 13:51:19 -07:00
Protobuf Team Bot
4de1634dd1 Refactor dense enum cache initialization to use absl::call_once.
PiperOrigin-RevId: 966158391
2026-08-17 13:46:07 -07:00
Protobuf Team Bot
dc693bc891 Auto-generate files after cl/966151323 2026-08-17 20:42:12 +00:00
Protobuf Team Bot
d46a6ff074 Enable FastTable for non-opt builds.
PiperOrigin-RevId: 966151323
2026-08-17 13:31:56 -07:00
Protobuf Team Bot
b41fd533ea Internal change.
PiperOrigin-RevId: 966146891
2026-08-17 13:26:28 -07:00
yashanil98
ced0495598 fix(protoc): honor --fatal_warnings in encode/decode mode (#28033)
Fixes #10486.

When an input message passed to `protoc --encode` or `--decode` is missing required fields, protoc prints `warning: Input message is missing required fields: ...`. That warning was written straight to stderr in `EncodeOrDecode` and never influenced the exit code, so protoc returned 0 even with `--fatal_warnings` set, defeating its `-Werror`-like purpose.

This makes `EncodeOrDecode` return a non-zero exit when `--fatal_warnings` is set and the message is missing required fields. The check happens after the output is written, matching the compile path (which also produces its output before failing). When `--fatal_warnings` is not set, behavior is unchanged: the output and the warning are still produced and the exit code stays 0.

A maintainer confirmed this is a valid bug and invited a PR on the issue.

Test: added `EncodeDecodeTest.PartialWithFatalWarnings` asserting a non-zero exit (and that the warning is still emitted) when encoding a message with missing required fields under `--fatal_warnings`.

  ./build/tests --gtest_filter='*EncodeDecodeTest.*'

Result: 18 tests pass, including the new one.

Closes #28033

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/28033 from yashanil98:fix/issue-10486-fatal-warnings-encode-decode d0bb423371
PiperOrigin-RevId: 965995070
2026-08-17 08:55:10 -07:00
Protobuf Team Bot
dc32c23f48 Auto-generate files after cl/965979781 2026-08-17 15:31:18 +00:00
Daniel Finnie
8fa07c6359 Clean up some includes in TextFormat.
PiperOrigin-RevId: 965979781
2026-08-17 08:19:49 -07:00
Samuel Benzaquen
becdd958d1 Continue cleaning up after the GLOBALS experiment.
Remove PROTOBUF_MESSAGE_GLOBALS branching in generated code.

PiperOrigin-RevId: 965977817
2026-08-17 08:15:07 -07:00
Protobuf Team Bot
944d9909ce Automated Code Change
PiperOrigin-RevId: 965775373
2026-08-16 23:53:48 -07:00
Protobuf Team Bot
e5c8ffa6cd Automated Code Change
PiperOrigin-RevId: 965767499
2026-08-16 23:31:01 -07:00
Protobuf Team Bot
169e0f9b47 Automated Code Change
PiperOrigin-RevId: 965744650
2026-08-16 22:18:33 -07:00
Clayton Knittel
228537eb47 Internal change.
PiperOrigin-RevId: 965698725
2026-08-16 19:34:45 -07:00
Clayton Knittel
8da17f0e28 Internal change.
PiperOrigin-RevId: 965416239
2026-08-15 23:31:25 -07:00
Protobuf Team Bot
e7e6ef07ba Optimize NameOfDenseEnum by performing an early bounds check.
PiperOrigin-RevId: 964944564
2026-08-14 16:19:00 -07:00
Samuel Benzaquen
7bc3164ec0 Add a unit test in CppGeneratorTest to verify deterministic serialization of map options.
PiperOrigin-RevId: 964825321
2026-08-14 12:19:09 -07:00
Mikita Belahlazau
9c38514c4a Convert multiline string literals in descriptor_unittest.cc to raw strings.
Makes the test file more readable and enables auto-formatting.

PiperOrigin-RevId: 964816524
2026-08-14 12:01:48 -07:00