Squashed 'third_party/upb/' changes from 382d5afc60..60607da72e

60607da72e Merge pull request #356 from haberman/codegen-uniquefiles
7a54a5f3d6 Split the code generators for .upb and .upbdefs.
b10b02f66f Merge pull request #353 from haberman/small-fixes
cd7c2d2701 Merge pull request #352 from haberman/large-field-number
10b355a6e0 A couple minor fixes. These are exercised by Ruby.
6c30b5fe73 Fixed upb encoder for field numbers > 2**28.
31b3528424 Merge pull request #350 from haberman/encode-depthlimit
e9551022c1 Added depth limit checking to upb_encode().
f389384c5a Merge pull request #349 from lidizheng/json-public
f72c26cbc4 Make :json publically visible
5797d95172 Merge pull request #348 from haberman/json-emit-defaults
7a17493269 Removed print debugging.
695b7f4617 Added code to test UPB_JSONENC_EMITDEFAULTS.
6b357607bd Fixed bugs in JSON encoding with UPB_JSONENC_EMITDEFAULTS.
1b08391979 Merge pull request #347 from haberman/filedef-symtab
ee49a8d7df Added an accessor to get the symtab from a filedef.
a81b47025a Merge pull request #345 from haberman/encode-skipunknown
6535cbe380 Merge pull request #346 from timgates42/bugfix_typo_position
7225108720 docs: fix simple typo, posisiton -> position
871ff96252 Test SKIPUNKNOWN on regular fields.
0569c22a1e Removed debug print.
76764643ac Added option to binary encoder to skip unknown fields.
794ce6d061 Merge pull request #344 from haberman/deterministic-serialize
a04627abc8 Added map sorting to binary and text encoders.
3e071ea91a Merge pull request #343 from haberman/msvc
e70e488e09 MSVC apparently doesn't support the standard C/C++ defines.
963e501aa6 Merge pull request #342 from haberman/fuzz
8d670d8aea Renamed decode_varint32() to decode_tag().
9abf8e043f Clamp 32-bit varints to 5 bytes to fix a fuzz failure.
50a543de7f Merge pull request #341 from haberman/code-size
9c87f1168f Added size benchmark for CODE_SIZE.
6f82028236 Merge pull request #340 from haberman/darwin-setjmp
358fa14d0e Fixed headers and updated benchmark script.
bc200451ce Use a macro instead of an inline function for setjmp/longjmp.
ca279f8afa Merge branch 'master' into darwin-setjmp
fbc0639b07 Use _setjmp on mac to avoid saving/restoring the signal mask.
10f0a38450 Merge pull request #337 from haberman/size-benchmarks
4be07a2dce Increase build timeout until we can parallelize the build.
378a27b640 Force "size" to run locally.
da48e01f05 More google3 fixes.
d2446fd2db Moved cc_api_version attribute to proto_library().
4a84390c89 Added cc_proto_library() tweaks for google3.
4dd4212a34 Merge branch 'master' into size-benchmarks
ffecfef161 Merge branch 'master' into size-benchmarks
86f671d5fd Fix for Darwin (output is different, but it won't error out).
165e01ec6f Fix for old Python versions.
40fdff5e43 Merge pull request #339 from haberman/benchmark-alias
5f8bb5de1d Updated generated code.
65d166a6ba Added API for copy vs. alias and added benchmarks to test both.
27b9c999a7 Merge pull request #338 from haberman/arena-initial
9df96874e9 Start arena block doubling at initial block size.
881ddac7fe Also use .format() for gen_synthetic_protos.py.
8b7dabe1a2 Use format() instead of string interpolation, for old Python versions.
8e08282c3b Removed unused small.proto.
0f79d47215 Added missing lite binaries to size_data.txt.
555fbbc0bc Size benchmarks are working pretty well.
85cbc41a89 Merge branch 'master' into size-benchmarks
e5bdfba92c Removed accidentally-added .orig file.
07851fac58 Merge pull request #336 from haberman/fuzz
982b634bc5 Fixed a few minor bugs found by fuzzing.
c9d2e58480 Merge pull request #310 from haberman/fast-table
a01f3e23a4 Fixes for google3 build, and exclude even more tests from macOS to avoid timeout.
a83d55ee4b Exclude Clang tests from MacOS to avoid Kokoro timeouts.
73fcfe9ed0 Tried to slim down the tests a bit more.
baab25b7aa Removed excess/redundant tests from Kokoro script.
1eb7bd39e7 Some formatting fixes.
9d87055ce4 Updated Kokoro build script.
4bd34da105 WIP.
a7993615bf Merge branch 'master' into fast-table
3a3efe69a2 Added incompatible_use_toolchain_transition = True per https://github.com/bazelbuild/bazel/issues/11584
7b4e376f79 Switch unordered_set -> absl::flat_hash_set.
fe62fc83e1 Removed obsolete includes in benchmark.
5ed089dc42 Merge pull request #335 from haberman/wyhash-rule
e9b79542ad Added a BUILD file for wyhash.
7b8ae7ec4e Merge branch 'master' into size-benchmarks
a5b5445da6 Merge pull request #334 from haberman/rm-port-c
6c16cba83f Removed obsolete port.c file.
484d8f746a Updated comment in wyhash.h to correct spelling mistake.
6a9d0f45b8 Merge pull request #333 from haberman/32bitfixes
5b1f0d86a1 For Kokoro, only build/test -m32 on Linux.
0497f8deed Fixed a critical bug on 32-bit builds, and added much more Kokoro testing.
7543f851f1 Merge pull request #332 from haberman/php-amalgamation
64abb5eb11 Amalgamation no longer bundles wyhash, but #includes it.
a2a40792b5 Merge pull request #331 from haberman/json-oneof-fix
dd0994d377 Bugfix for JSON decoding: only check real oneofs for duplicates.
496f638025 Merge pull request #330 from haberman/symtab-longjmp
c9f9668234 symtab: use longjmp() for errors and avoid intermediate table.
4d9a8cd67b Merge branch 'master' into symtab-longjmp
6322d1d72d Merge pull request #329 from haberman/benchmark-ads-descriptor
5ec1d39224 Avoid building .pb.cc for ads protos, as C++ takes forever to compile.
43c207ea7e Added CMake dummy rule.
c3b5637646 Added benchmark for loading ads descriptor.
acd72c6d3f WIP.
6ae3b66cd2 Merge pull request #328 from haberman/optdefload
8113ebd6c7 Added explanatory comment about integer constants.
f2d826b9f3 Got rid of floating-point division in table init.
723cd8ffc1 Added wyhash code and LICENSE, and removed temporary benchmark.
15e20636c3 Merge branch 'master' into optdefload
154f2c25f4 Added UTF-8 validation for proto3 string fields.
e8f9eac68c Added #defines UPB_ENABLE_FASTTABLE and UPB_TRY_ENABLE_FASTTABLE.
994cf07cf3 Merge pull request #327 from benjaminp/modern-linking
b5bd5807a7 Migrate to modern Starlark linking api.
dc64613607 Merge branch 'fastest-table' into fast-table
e86541ac1d Fixed the build after the merge.
1cd0cb17d3 Merge branch 'master' into fastest-table
901744a97e Merge pull request #326 from haberman/c99
a0d16e7073 Added a few missing copts, and made some functions proper prototypes.
558315a1c3 Added COPTS to :port.
7e5bd65098 Plumbed copts (including the crucial -std=c99) to upb_proto_library() aspect.
8f3ee80d46 Drop C89/C90 support and MSVC prior to Visual Studio 2015.
a274ad786a Plumbed copts (including the crucial -std=c99) to upb_proto_library() aspect.
2c8bb6dd9d Specify C99 explicitly until/unless we stop using bool.
efd576b698 Added -std=gnu99 for fastdecode and ran Buildifier.
b928696942 A few more fixes, and test fastdecode under Kokoro.
55f3569cd2 A few minor fixes and more assertions.
8b38e8f214 Merge branch 'fastest-table' into fast-table
46eb82467a Added comment to decode_fast.h.
bd9f8f580d Fixed a few bugs with the fast decoder.
3eba47914b Allocate hasbits and table slots in "hotness" order.
021db6fcd5 Allow larger tags into the table if they are unique mod 31.
86d9908c55 Fastdecode support for packed fields.
e3e797b680 Added fasttable support for oneofs.
7ffa9c181a Fixed some small bugs and performance problems in string copying.
e2c709e047 Repeated string and primitive support.
e9103eda9e Merge branch 'master' into fastest-table
0756999ab6 Merge pull request #325 from haberman/inlined-arena
25db40bc30 Fixed upb::InlinedArena, which was compeltely broken.
d81ba58215 Optimized short string copying.
f3a2a79349 More optimization, back up to 2.56GB/s.
199c914295 Simplify push/pop when msg fits in the current buffer.
d5f5db2729 Put string-copying field parser into a separate function.
883f20d4dc Merge branch 'master' into fastest-table
1bd62e8218 Merge pull request #324 from haberman/simplemomi
f4adbe0698 Optimized varint decoding from Gerben.
48689df72e Eliminated bounds checks inside parsing a field.
a345af9883 Added a codegen parameter for whether fasttables are generated or not.
8a3470c543 WIP.
8e8dbb5258 Merge branch 'fastest-table' into fast-table
7d17a0e8c5 Merge branch 'master' into fastest-table
a7e2e8338d Fixed benchmark script.
72de7b7002 Merge branch 'fastest-table' into fast-table
cb234e652c Merge branch 'master' into fastest-table
b86cf2d789 Merge pull request #323 from haberman/build-files
4ea81ab107 Fixed pedantic warning.
6399b31f4b Removed ULL constants in json_decode.c.
c8ae197e64 Removed "U" suffixes, they are not necessary.
bc1e0b314f Fixed some strict C89 errors.
2c1664906a Removed license comments and upb_amalgamation for google3.
b7dc77415a Added licenses() to all BUILD files.
de22764b33 Updated Kokoro to test ... instead of :all.
e3f41de6c7 Split monolithic BUILD file into many build files.
fbe2bcafbc Merge pull request #4 from gerben-s/gerbens-fast-table
9e68ec033f Add repeated varints and fixed parsers
d0e4b688c6 Shorten name of kAliasString, so benchmark results don't wrap.
c0c9b5a168 Regenerated generated code.
eb8e6de8b7 Regenerated source files.
7f0d535826 Merge branch 'fastest-table' into fast-table
bf8e08074c Added a few more comments.
6e3c22e6ee Merge branch 'fastest-table' into fast-table
3238821315 Gave fast table entry a nicer name.
2a574d3d01 Added a bunch of comments for readability.
0deca8b8fb Merge branch 'master' into fast-table
bfadc99709 Merge branch 'master' into fastest-table
84e0f6127d Merge branch 'master' into fastest-table
61c51a607b Merge branch 'master' into fast-table
4f066765a9 Merge pull request #320 from haberman/string-view-benchmark
bf393bf086 Cleaned up benchmark names.
9eb8414b31 Added descriptor_sv.proto.
ee7da95367 Bzl formatting fix per buildifier.
de1b6b0718 Refactored proto2 benchmarks and added StringView benchmark.
c2901eeee1 Added missing #includes (caught by Blaze).
52721eadc7 Order LargeInitialBlock after non-initial-block test.
44a7130845 Used the correct copy of the protos in the benchmark.
3a242e7a48 Merge branch 'master' into fast-table
d01b712dbb Merge branch 'master' into fastest-table
d225dfc2ed Buildifier formatting fixes.
b0994a6501 Added missing dep from benchmark.
de80054018 Make required fields optional for benchmarking.
745661bf1f Merge branch 'master' into fastest-table
11585095b7 Merge pull request #319 from haberman/copy-descriptor
fd5a35d49d Minor comment changes.
bdd1a516e8 Fixed other tests.
27262adf93 Snapshot descriptor.proto into our own copy for benchmarking purposes.
d6b40fd5e2 Added missing dependency on reflection.
b9f1b67d07 Use quoted include.
1c8c16b9b1 Use quoted include.
4f901b6430 Passes all tests.
de84e20788 Merge branch 'master' into optdefload
d5c64476fd Merge pull request #318 from haberman/descriptor-load-benchmark
42d2f6cef9 Add MB/s measures in the benchmarks.
1ce98b86ec Added LoadDescriptor benchmark for proto2.
4890735ce2 Added benchmark for descriptor loading.
6f59f1256e Optimizations to descriptor loading.
6874d61704 Merge pull request #317 from haberman/gencode-compat
c81113e60f Added fallback code for when no enum matches.
c10b24ffb2 Simplified switch().
ded2e657a7 Added compatibility with old generated code.
5b0c5c7d4a Dispatch inline.
75edd3e59c Changed to use table pairs, seems to ever-so-slightly regress.
69d99a247e Merge pull request #3 from haberman/shifted-table
bca7edac8c Cleaned up table compression a bit.
b95f217996 A little speed boost, now hitting 2.51GB/s.
8ed6b2fe85 Stored mask in the table pointer.
a6dc88556d Tables are compressed, but perf goes down to 2.44GB/s.
91eb09b1bc Add a few comments.
7a1835d7c3 Merge branch 'master' into fast-table
8bd5c0088e Merge pull request #316 from haberman/submsg-array
a4966fd230 Added a few extra sanity checks.
99acbe0da8 Fixed bug where submsg array could have excess elements.
2a0425ecf5 Merge pull request #315 from haberman/conformance-fixes
7ccf5650c7 If we encounter "null" for a non-NullValue enum, throw an error.
0a3a94a12f Updated to a new version of protobuf and fixed a few conformance tests.
ddd5f28c4f Merge branch 'is_bazel'
10fa3a0c77 Merge pull request #314 from haberman/addunknown
504e105420 undef UPB_ASAN.
ab96d1ec41 Removed extraneous C++-style comment.
d5096f9ee8 Fixed bug in addunknown and added ASAN poisoning.
6fe84526be Mark _is_bazel to be replace in google3.
f01efe8b64 Removed another C99-ism.
1749082bbb Removed C99-ism.
147e363f56 Merge branch 'master' into fast-table
f2ddc15d76 Bugfix: initialize fastlimit and fastend.
65e49b694b Merge branch 'gerben-fast-table' into fast-table
1abf7d418d Added generated files.
3f719fa6b2 Bugfix: offsetting hasbits with 16 introduced a bug in calculating hasmasks. Removing extra <<16 shift in hasmask calculating and masking out the first 16 bits. This makes messages without hasbits work as well.
aec762e405 Merge branch 'master' into fast-table
4f77aaafd8 Merge pull request #2 from gerben-s/fast-table
4053805759 Bugfixes
d1cd80385b Merge pull request #313 from haberman/inline-arena
ad21083623 Merge pull request #313 from haberman/inline-arena
2339fc779c Updated obsolete comment.
b393849bbd Updated obsolete comment.
ebe53f8590 Fixed compile error.
b37f82b58b Fixed compile error.
71749b7caf Implemented inline array allocation, and moved type->lg2 map to reflection.
9557b97acc Implemented inline array allocation, and moved type->lg2 map to reflection.
b58d2a0ee6 Shrink overhead of message representation.
0bf063a2ca Shrink overhead of message representation.
d87ceeacab Shave off one more store.
ddc52ab9d6 Shave off one more store.
c25d895adf Shrunk the arena state that needs to be synced.
7f67f68c1c Shrunk the arena state that needs to be synced.
ff40dd6ea9 Added new internal header.
85a43e5461 Added new internal header.
36662b3735 Refactor some code. I extracted some common code from all message field parsers, to a tail recursive function. Removed the varint jmp table for a simple varint parse loop, that removes the stack frames. Also careful with not losing information in repeated message tag check. When written mindful the checks and loads that happen can be reused for tag dispatch if not the expected tag.
cbcd635917 Fixed memory leak.
bcbcdadbd2 Fixed memory leak.
e5264bd794 Merge pull request #312 from haberman/defiter
52957fa984 Merge pull request #312 from haberman/defiter
746f64692c Moved arena inline for decoder.
7363b91ac3 Moved arena inline for decoder.
b8ef1dcc57 Removed C++-style comments.
575acd85bd Re-added const for all of the pointer wrapper types.
5aa5b77b41 Added simple offset-based accessors for defs, and deprecated old iterators.
33384301e2 Merge pull request #311 from haberman/proto2-benchmark2
578e7c1f4c Merge pull request #311 from haberman/proto2-benchmark2
bc301e7da4 Use merge/partial variants to give proto2 benchmark the fairest hearing.
30f01afa83 Added LargeInitialBlock test for proto2.
5d23fd99af Used shorter protobuf:: namespace alias.
9938cf8f27 Put submsg_index directly in table data. Drop oneof support for now to focus.
d87179501d Another build fix.
89bd8b87e1 Fixed a few more C89 compat issues.
64d293894a Fixed bug introduced by last optimization.
ff957b996c Fixed C89 compat issues.
537b6f42c2 A few updates to the benchamrk and minor implementation changes.
0dcc5641eb Replicated dispatch and implemeted array resizing logic. Up to 2.67GB/s.
526e430794 I think this may have reached the optimization limit.
4c65b25daf Handle long varints, now 2GB/s!
e39ec95ca2 Hoisted updates to limits and depth out of the loop.
52a0ed3891 Fixed a bug with tag number 15.
388b6f64eb A small optimization: don't increment array length every iteration.
9e5c5ce089 Optimized memset() with cutoff and fixed group & unknown message bugs.
8dd7b5a2ca A bunch more optimization.
e46e94ec7f Added benchmarks for proto2.
405e7934b1 Handle 2-byte submessage lengths.
88b1ec7784 Table-driven supports repeated sub-messages.
f173642db4 Handle non-repeated submessages.
e219a2d91d Merge branch 'decode-arena' into fast-table
7ec2c52346 Donate/steal from arena to accelerate decoding.
d43ccfa079 Revert test changes.
fac992db83 Cleanup for showing.
3937874a85 We have a properly structured algorithm, but perf regresses by 20%.
438ecaeb5a Give all field parsers a generic table entry.
383ae5293e WIP.
26abaa2345 WIP.
34b98bc030 Avoid passing too many params to fallback.
763a3f6293 WIP.
02ff6fb996 Merge pull request #309 from haberman/decoder-forceinline
a202ce9629 Add UPB_FORCEINLINE for varint32 decoding.
d0f2c4c8a2 Merge pull request #308 from haberman/encoder
5741eb9ad7 Expanded benchmarking script and added one size opt to the encoder.
0135399e60 Fixed bug introduced in refactoring.
df3438222b Notated impossible branch as unreachable.
9b31e8fe12 Merged common encode tag paths.
5d7dc718cc Minor formatting fix.
80441e4eb4 Optimized binary encoder.
ada28896b9 Changed encoder to use longjmp() for error recovery.
6e140c267c Added benchmark for encoding.
7338facddb Merge pull request #307 from veblush/port-backport
4d2251c3e4 Add UPB_NORETURN for MSC

git-subtree-dir: third_party/upb
git-subtree-split: 60607da72e89ba0c84c84054d2e562d8b6b61177
pull/25037/head
Esun Kim 4 years ago
parent 6d7d35fa39
commit 000f1fe0ea
  1. 22
      .bazelrc
  2. 706
      BUILD
  3. 9
      WORKSPACE
  4. 78
      bazel/build_defs.bzl
  5. 10
      bazel/lua.BUILD
  6. 6
      bazel/ragel.BUILD
  7. 122
      bazel/upb_proto_library.bzl
  8. 5
      bazel/workspace_deps.bzl
  9. 32
      benchmark.py
  10. 218
      benchmarks/BUILD
  11. 29
      benchmarks/BUILD.googleapis
  12. 258
      benchmarks/benchmark.cc
  13. 63
      benchmarks/build_defs.bzl
  14. 89
      benchmarks/compare.py
  15. 905
      benchmarks/descriptor.proto
  16. 890
      benchmarks/descriptor_sv.proto
  17. 6
      benchmarks/empty.proto
  18. 38
      benchmarks/gen_protobuf_binary_cc.py
  19. 92
      benchmarks/gen_synthetic_protos.py
  20. 39
      benchmarks/gen_upb_binary_c.py
  21. 91
      cmake/BUILD
  22. 125
      cmake/CMakeLists.txt
  23. 23
      cmake/README.md
  24. 43
      cmake/build_defs.bzl
  25. 180
      cmake/google/protobuf/descriptor.upb.c
  26. 747
      cmake/google/protobuf/descriptor.upb.h
  27. 25
      cmake/make_cmakelists.py
  28. 2
      cmake/staleness_test.py
  29. 13
      cmake/staleness_test_lib.py
  30. 22
      cmake/upb/json/parser.c
  31. 3
      examples/bazel/BUILD
  32. 2
      examples/bazel/test_binary.c
  33. 35
      kokoro/ubuntu/build.sh
  34. 2
      kokoro/ubuntu/presubmit.cfg
  35. 283
      tests/BUILD
  36. 64
      tests/benchmark.cc
  37. 68
      tests/bindings/lua/BUILD
  38. 2
      tests/bindings/lua/main.c
  39. 28
      tests/bindings/lua/test.proto
  40. 234
      tests/bindings/lua/test_upb.lua
  41. 11
      tests/conformance_upb.c
  42. 81
      tests/pb/test_decoder.cc
  43. 57
      tests/pb/test_encoder.cc
  44. 126
      tests/pb/test_varint.c
  45. 25
      tests/test_cpp.cc
  46. 47
      tests/test_generated_code.c
  47. 10
      tests/test_table.cc
  48. 17
      third_party/wyhash/BUILD
  49. 25
      third_party/wyhash/LICENSE
  50. 145
      third_party/wyhash/wyhash.h
  51. 41
      upb/bindings/lua/BUILD
  52. 187
      upb/bindings/lua/def.c
  53. 11
      upb/bindings/lua/lua_proto_library.bzl
  54. 230
      upb/bindings/lua/msg.c
  55. 35
      upb/bindings/lua/upb.c
  56. 2
      upb/bindings/lua/upb.h
  57. 69
      upb/bindings/stdc++/string.h
  58. 415
      upb/decode.c
  59. 21
      upb/decode.h
  60. 163
      upb/decode.int.h
  61. 1040
      upb/decode_fast.c
  62. 126
      upb/decode_fast.h
  63. 1041
      upb/def.c
  64. 47
      upb/def.h
  65. 160
      upb/def.hpp
  66. 396
      upb/encode.c
  67. 29
      upb/encode.h
  68. 9
      upb/handlers.c
  69. 22
      upb/json/parser.rl
  70. 46
      upb/json/printer.c
  71. 76
      upb/json_decode.c
  72. 47
      upb/json_encode.c
  73. 2
      upb/json_encode.h
  74. 255
      upb/msg.c
  75. 208
      upb/msg.h
  76. 18
      upb/pb/compile_decoder.c
  77. 77
      upb/pb/encoder.c
  78. 13
      upb/pb/textprinter.c
  79. 4
      upb/pb/varint.int.h
  80. 26
      upb/port.c
  81. 135
      upb/port_def.inc
  82. 10
      upb/port_undef.inc
  83. 55
      upb/reflection.c
  84. 231
      upb/table.c
  85. 19
      upb/table.int.h
  86. 67
      upb/text_encode.c
  87. 5
      upb/text_encode.h
  88. 59
      upb/upb.c
  89. 43
      upb/upb.h
  90. 8
      upb/upb.hpp
  91. 29
      upb/upb.int.h
  92. 53
      upbc/BUILD
  93. 65
      upbc/common.cc
  94. 66
      upbc/common.h
  95. 12
      upbc/generator.h
  96. 9
      upbc/main.cc
  97. 15
      upbc/message_layout.cc
  98. 17
      upbc/message_layout.h
  99. 621
      upbc/protoc-gen-upb.cc
  100. 183
      upbc/protoc-gen-upbdefs.cc

@ -0,0 +1,22 @@
# Use our custom-configured c++ toolchain.
build:m32 --copt=-m32 --linkopt=-m32
build:asan --copt=-fsanitize=address --linkopt=-fsanitize=address
build:valgrind --run_under='valgrind --leak-check=full --error-exitcode=1'
build:ubsan --copt=-fsanitize=undefined --linkopt=-fsanitize=undefined --action_env=UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1
# Workaround for the fact that Bazel links with $CC, not $CXX
# https://github.com/bazelbuild/bazel/issues/11122#issuecomment-613746748
build:ubsan --copt=-fno-sanitize=function --copt=-fno-sanitize=vptr
build:Werror --copt=-Werror
build:Werror --per_file_copt=json/parser@-Wno-error
build:Werror --per_file_copt=com_google_protobuf@-Wno-error
# GCC's -fanalyzer, a deeper static analysis than normal warnings.
build:analyzer --copt=-fanalyzer --copt=-Werror
build:analyzer --per_file_copt=json/parser@-fno-analyzer
build:analyzer --per_file_copt=com_google_protobuf@-fno-analyzer
build:analyzer --per_file_copt=com_github_google_benchmark@-fno-analyzer
build:asan-fuzzer --copt=-fsanitize=address,fuzzer --linkopt=-fsanitize=address,fuzzer --define fuzz=true

706
BUILD

@ -1,13 +1,13 @@
load(
"//bazel:build_defs.bzl",
"generated_file_staleness_test",
"licenses", # copybara:strip_for_google3
"make_shell_script",
"upb_amalgamation",
"UPB_DEFAULT_COPTS",
"upb_amalgamation", # copybara:strip_for_google3
)
load(
"//bazel:upb_proto_library.bzl",
"upb_fasttable_enabled",
"upb_proto_library",
"upb_proto_library_copts",
"upb_proto_reflection_library",
)
@ -16,10 +16,7 @@ load(
"@rules_proto//proto:defs.bzl",
"proto_library",
)
load(
"//:upb/bindings/lua/lua_proto_library.bzl",
"lua_proto_library",
)
# copybara:strip_end
licenses(["notice"]) # BSD (Google-authored w/ possible external contributions)
@ -29,62 +26,52 @@ exports_files([
"build_defs",
])
CPPOPTS = [
# copybara:strip_for_google3_begin
"-Wextra",
# "-Wshorten-64-to-32", # not in GCC (and my Kokoro images doesn't have Clang)
"-Werror",
"-Wno-long-long",
# copybara:strip_end
]
COPTS = CPPOPTS + [
# copybara:strip_for_google3_begin
"-pedantic",
"-Werror=pedantic",
"-Wstrict-prototypes",
# copybara:strip_end
]
config_setting(
name = "darwin",
values = {"cpu": "darwin"},
name = "windows",
constraint_values = ["@bazel_tools//platforms:windows"],
)
upb_fasttable_enabled(
name = "fasttable_enabled",
build_setting_default = False,
visibility = ["//visibility:public"],
)
config_setting(
name = "windows",
constraint_values = ["@bazel_tools//platforms:windows"],
name = "fasttable_enabled_setting",
flag_values = {"//:fasttable_enabled": "true"},
)
config_setting(
name = "fuzz",
values = {"define": "fuzz=true"},
upb_proto_library_copts(
name = "upb_proto_library_copts__for_generated_code_only_do_not_use",
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
)
# Public C/C++ libraries #######################################################
cc_library(
name = "port",
srcs = [
"upb/port.c",
],
copts = UPB_DEFAULT_COPTS,
textual_hdrs = [
"upb/port_def.inc",
"upb/port_undef.inc",
],
visibility = ["//tests:__pkg__"],
)
cc_library(
name = "upb",
srcs = [
"upb/decode.c",
"upb/decode.int.h",
"upb/encode.c",
"upb/msg.c",
"upb/msg.h",
"upb/table.c",
"upb/table.int.h",
"upb/upb.c",
"upb/upb.int.h",
],
hdrs = [
"upb/decode.h",
@ -92,12 +79,29 @@ cc_library(
"upb/upb.h",
"upb/upb.hpp",
],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [":port"],
deps = [
":fastdecode",
":port",
"//third_party/wyhash",
],
)
cc_library(
name = "fastdecode",
srcs = [
"upb/decode.int.h",
"upb/decode_fast.c",
"upb/decode_fast.h",
"upb/msg.h",
"upb/upb.int.h",
],
copts = UPB_DEFAULT_COPTS,
deps = [
":port",
":table",
],
)
# Common support routines used by generated code. This library has no
@ -109,14 +113,12 @@ cc_library(
cc_library(
name = "generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
hdrs = [
"upb/decode_fast.h",
"upb/msg.h",
"upb/port_def.inc",
"upb/port_undef.inc",
],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":table",
@ -148,10 +150,7 @@ cc_library(
"upb/def.hpp",
"upb/reflection.h",
],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":descriptor_upb_proto",
@ -169,10 +168,7 @@ cc_library(
hdrs = [
"upb/text_encode.h",
],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":port",
@ -190,10 +186,8 @@ cc_library(
"upb/json_decode.h",
"upb/json_encode.h",
],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":port",
":reflection",
@ -205,10 +199,13 @@ cc_library(
cc_library(
name = "table",
hdrs = ["upb/table.int.h"],
hdrs = [
"upb/table.int.h",
"upb/upb.h",
],
visibility = ["//tests:__pkg__"],
deps = [
":port",
":upb",
],
)
@ -225,10 +222,8 @@ cc_library(
"upb/handlers.h",
"upb/sink.h",
],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
copts = UPB_DEFAULT_COPTS,
visibility = ["//tests:__pkg__"],
deps = [
":port",
":reflection",
@ -253,10 +248,8 @@ cc_library(
"upb/pb/encoder.h",
"upb/pb/textprinter.h",
],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
copts = UPB_DEFAULT_COPTS,
visibility = ["//tests:__pkg__"],
deps = [
":descriptor_upb_proto",
":handlers",
@ -278,400 +271,23 @@ cc_library(
"upb/json/parser.h",
"upb/json/printer.h",
],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
deps = [
":upb",
":upb_pb",
],
)
# copybara:strip_end
cc_library(
name = "upb_cc_bindings",
hdrs = [
"upb/bindings/stdc++/string.h",
],
deps = [
":descriptor_upb_proto",
":handlers",
":port",
":upb",
],
)
# upb compiler #################################################################
cc_library(
name = "upbc_generator",
srcs = [
"upbc/generator.cc",
"upbc/message_layout.cc",
"upbc/message_layout.h",
],
hdrs = ["upbc/generator.h"],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS,
}),
deps = [
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//:protoc_lib",
],
)
cc_binary(
name = "protoc-gen-upb",
srcs = ["upbc/main.cc"],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS,
}),
visibility = ["//visibility:public"],
deps = [
":upbc_generator",
"@com_google_protobuf//:protoc_lib",
],
)
# We strip the tests and remaining rules from google3 until the upb_proto_library()
# and upb_proto_reflection_library() rules are fixed.
# C/C++ tests ##################################################################
upb_proto_reflection_library(
name = "descriptor_upbreflection",
deps = ["@com_google_protobuf//:descriptor_proto"],
)
cc_binary(
name = "benchmark",
testonly = 1,
srcs = ["tests/benchmark.cc"],
deps = [
":descriptor_upb_proto",
":descriptor_upbreflection",
"@com_github_google_benchmark//:benchmark_main",
],
)
cc_library(
name = "upb_test",
testonly = 1,
srcs = [
"tests/testmain.cc",
],
hdrs = [
"tests/test_util.h",
"tests/upb_test.h",
],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS,
}),
deps = [
":handlers",
":port",
":upb",
],
)
cc_test(
name = "test_varint",
srcs = [
"tests/pb/test_varint.c",
"upb/pb/varint.int.h",
],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
deps = [
":port",
":upb",
":upb_pb",
":upb_test",
],
)
proto_library(
name = "test_proto",
testonly = 1,
srcs = ["tests/test.proto"],
)
upb_proto_library(
name = "test_upb_proto",
testonly = 1,
deps = [":test_proto"],
)
cc_test(
name = "test_generated_code",
srcs = ["tests/test_generated_code.c"],
deps = [
":test_messages_proto3_proto_upb",
":empty_upbdefs_proto",
":test_upb_proto",
":upb_test",
],
)
proto_library(
name = "empty_proto",
srcs = ["tests/empty.proto"],
)
upb_proto_reflection_library(
name = "empty_upbdefs_proto",
testonly = 1,
deps = [":empty_proto"],
)
upb_proto_library(
name = "test_messages_proto3_proto_upb",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto3_proto"],
)
proto_library(
name = "test_decoder_proto",
srcs = [
"tests/pb/test_decoder.proto",
],
)
upb_proto_reflection_library(
name = "test_decoder_upb_proto",
deps = [":test_decoder_proto"],
)
cc_test(
name = "test_decoder",
srcs = [
"tests/pb/test_decoder.cc",
"upb/pb/varint.int.h",
],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS,
}),
deps = [
":handlers",
":port",
":test_decoder_upb_proto",
":upb",
":upb_pb",
":upb_test",
],
)
proto_library(
name = "test_cpp_proto",
srcs = [
"tests/test_cpp.proto",
],
)
upb_proto_reflection_library(
name = "test_cpp_upb_proto",
deps = ["test_cpp_proto"],
)
cc_test(
name = "test_cpp",
srcs = ["tests/test_cpp.cc"],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS,
}),
deps = [
":handlers",
":port",
":reflection",
":test_cpp_upb_proto",
":upb",
":upb_pb",
":upb_test",
],
)
cc_test(
name = "test_table",
srcs = ["tests/test_table.cc"],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS,
}),
deps = [
":port",
":table",
":upb",
":upb_test",
],
)
# OSS-Fuzz test
cc_binary(
name = "file_descriptor_parsenew_fuzzer",
testonly = 1,
srcs = ["tests/file_descriptor_parsenew_fuzzer.cc"],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS,
}) + select({
"//conditions:default": [],
":fuzz": ["-fsanitize=fuzzer,address"],
}),
defines = select({
"//conditions:default": [],
":fuzz": ["HAVE_FUZZER"],
}),
deps = [
":descriptor_upb_proto",
":upb",
],
)
# copybara:strip_for_google3_begin
cc_test(
name = "test_encoder",
srcs = ["tests/pb/test_encoder.cc"],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS,
}),
copts = UPB_DEFAULT_COPTS,
visibility = ["//tests:__pkg__"],
deps = [
":descriptor_upb_proto",
":descriptor_upbreflection",
":upb",
":upb_cc_bindings",
":upb_pb",
":upb_test",
],
)
proto_library(
name = "test_json_enum_from_separate",
srcs = ["tests/json/enum_from_separate_file.proto"],
deps = [":test_json_proto"],
)
proto_library(
name = "test_json_proto",
srcs = ["tests/json/test.proto"],
)
upb_proto_reflection_library(
name = "test_json_upb_proto_reflection",
deps = ["test_json_proto"],
)
upb_proto_library(
name = "test_json_enum_from_separate_upb_proto",
deps = [":test_json_enum_from_separate"],
)
upb_proto_library(
name = "test_json_upb_proto",
deps = [":test_json_proto"],
)
cc_test(
name = "test_json",
srcs = [
"tests/json/test_json.cc",
],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS,
}),
deps = [
":test_json_upb_proto",
":test_json_upb_proto_reflection",
":upb_json",
":upb_test",
],
)
# copybara:strip_end
upb_proto_library(
name = "conformance_proto_upb",
testonly = 1,
deps = ["@com_google_protobuf//:conformance_proto"],
)
upb_proto_reflection_library(
name = "conformance_proto_upbdefs",
testonly = 1,
deps = ["@com_google_protobuf//:conformance_proto"],
)
upb_proto_reflection_library(
name = "test_messages_proto2_upbdefs",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto2_proto"],
)
upb_proto_reflection_library(
name = "test_messages_proto3_upbdefs",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto3_proto"],
)
cc_binary(
name = "conformance_upb",
testonly = 1,
srcs = [
"tests/conformance_upb.c",
],
data = [
"tests/conformance_upb_failures.txt",
],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}) + ["-Ibazel-out/k8-fastbuild/bin"],
deps = [
":port",
":conformance_proto_upb",
":conformance_proto_upbdefs",
":json",
":reflection",
":test_messages_proto2_upbdefs",
":test_messages_proto3_upbdefs",
":textformat",
":upb",
],
)
make_shell_script(
name = "gen_test_conformance_upb",
out = "test_conformance_upb.sh",
contents = "external/com_google_protobuf/conformance_test_runner " +
" --enforce_recommended " +
" --failure_list ./tests/conformance_upb_failures.txt" +
" ./conformance_upb",
)
sh_test(
name = "test_conformance_upb",
srcs = ["test_conformance_upb.sh"],
data = [
"tests/conformance_upb_failures.txt",
":conformance_upb",
"@com_google_protobuf//:conformance_test_runner",
],
deps = ["@bazel_tools//tools/bash/runfiles"],
genrule(
name = "generate_json_ragel",
srcs = ["//:upb/json/parser.rl"],
outs = ["upb/json/parser.c"],
cmd = "$(location @ragel//:ragelc) -C -o upb/json/parser.c $< && mv upb/json/parser.c $@",
tools = ["@ragel//:ragelc"],
visibility = ["//cmake:__pkg__"],
)
# copybara:strip_for_google3_begin
# Amalgamation #################################################################
py_binary(
@ -688,6 +304,7 @@ upb_amalgamation(
amalgamator = ":amalgamate",
libs = [
":upb",
":fastdecode",
":descriptor_upb_proto",
":reflection",
":handlers",
@ -701,15 +318,12 @@ cc_library(
name = "amalgamation",
srcs = ["upb.c"],
hdrs = ["upb.h"],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
copts = UPB_DEFAULT_COPTS,
deps = ["//third_party/wyhash"],
)
upb_amalgamation(
name = "gen_php_amalgamation",
prefix = "php-",
outs = [
"php-upb.c",
"php-upb.h",
@ -717,27 +331,26 @@ upb_amalgamation(
amalgamator = ":amalgamate",
libs = [
":upb",
":fastdecode",
":descriptor_upb_proto",
":descriptor_upb_proto_reflection",
":reflection",
":port",
":json",
],
prefix = "php-",
)
cc_library(
name = "php_amalgamation",
srcs = ["php-upb.c"],
hdrs = ["php-upb.h"],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
copts = UPB_DEFAULT_COPTS,
deps = ["//third_party/wyhash"],
)
upb_amalgamation(
name = "gen_ruby_amalgamation",
prefix = "ruby-",
outs = [
"ruby-upb.c",
"ruby-upb.h",
@ -745,190 +358,53 @@ upb_amalgamation(
amalgamator = ":amalgamate",
libs = [
":upb",
":fastdecode",
":descriptor_upb_proto",
":reflection",
":port",
":json",
],
prefix = "ruby-",
)
cc_library(
name = "ruby_amalgamation",
srcs = ["ruby-upb.c"],
hdrs = ["ruby-upb.h"],
copts = select({
":windows": [],
"//conditions:default": COPTS,
}),
copts = UPB_DEFAULT_COPTS,
deps = ["//third_party/wyhash"],
)
# Lua ##########################################################################
cc_library(
name = "lupb",
srcs = [
"upb/bindings/lua/def.c",
"upb/bindings/lua/msg.c",
"upb/bindings/lua/upb.c",
],
hdrs = [
"upb/bindings/lua/upb.h",
],
deps = [
":reflection",
":textformat",
":upb",
"@lua//:liblua",
exports_files(
[
"upb/json/parser.rl",
"BUILD",
"WORKSPACE",
],
visibility = ["//cmake:__pkg__"],
)
cc_test(
name = "test_lua",
srcs = ["tests/bindings/lua/main.c"],
data = [
"tests/bindings/lua/test_upb.lua",
exports_files(
[
"third_party/lunit/console.lua",
"third_party/lunit/lunit.lua",
"upb/bindings/lua/upb.lua",
":descriptor_proto_lua",
":test_messages_proto3_proto_lua",
":test_messages_proto2_proto_lua",
":test_proto_lua",
"@com_google_protobuf//:conformance_proto",
"@com_google_protobuf//:descriptor_proto",
],
linkstatic = 1,
deps = [
":lupb",
"@lua//:liblua",
],
)
cc_binary(
name = "protoc-gen-lua",
srcs = ["upb/bindings/lua/upbc.cc"],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS,
}),
visibility = ["//visibility:public"],
deps = [
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protoc_lib",
],
visibility = ["//tests/bindings/lua:__pkg__"],
)
lua_proto_library(
name = "test_proto_lua",
testonly = 1,
deps = [":test_proto"],
)
lua_proto_library(
name = "descriptor_proto_lua",
deps = ["@com_google_protobuf//:descriptor_proto"],
)
lua_proto_library(
name = "test_messages_proto3_proto_lua",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto3_proto"],
)
lua_proto_library(
name = "test_messages_proto2_proto_lua",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto2_proto"],
)
# Test the CMake build #########################################################
filegroup(
name = "cmake_files",
srcs = glob([
"upb/json/parser.c",
"CMakeLists.txt",
"generated_for_cmake/**/*",
"google/**/*",
"upbc/**/*",
"upb/**/*",
"tests/**/*",
"third_party/**/*",
]),
)
make_shell_script(
name = "gen_run_cmake_build",
out = "run_cmake_build.sh",
contents = "find . && mkdir build && cd build && cmake .. && make -j8 && make test",
)
sh_test(
name = "cmake_build",
srcs = ["run_cmake_build.sh"],
data = [":cmake_files"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
# Generated files ##############################################################
exports_files(["tools/staleness_test.py"])
py_library(
name = "staleness_test_lib",
testonly = 1,
srcs = ["tools/staleness_test_lib.py"],
)
py_binary(
name = "make_cmakelists",
srcs = ["tools/make_cmakelists.py"],
)
genrule(
name = "gen_cmakelists",
srcs = [
"BUILD",
"WORKSPACE",
":cmake_files",
],
outs = ["generated-in/CMakeLists.txt"],
cmd = "$(location :make_cmakelists) $@",
tools = [":make_cmakelists"],
)
genrule(
name = "generate_json_ragel",
srcs = ["upb/json/parser.rl"],
outs = ["upb/json/parser.c"],
cmd = "$(location @ragel//:ragelc) -C -o upb/json/parser.c $< && mv upb/json/parser.c $@",
tools = ["@ragel//:ragelc"],
)
genrule(
name = "copy_json_ragel",
srcs = ["upb/json/parser.c"],
outs = ["generated-in/generated_for_cmake/upb/json/parser.c"],
cmd = "cp $< $@",
)
genrule(
name = "copy_protos",
srcs = [":descriptor_upb_proto"],
outs = [
"generated-in/generated_for_cmake/google/protobuf/descriptor.upb.c",
"generated-in/generated_for_cmake/google/protobuf/descriptor.upb.h",
],
cmd = "cp $(SRCS) $(@D)/generated-in/generated_for_cmake/google/protobuf",
)
generated_file_staleness_test(
name = "test_generated_files",
outs = [
"CMakeLists.txt",
"generated_for_cmake/google/protobuf/descriptor.upb.c",
"generated_for_cmake/google/protobuf/descriptor.upb.h",
"generated_for_cmake/upb/json/parser.c",
],
generated_pattern = "generated-in/%s",
visibility = ["//cmake:__pkg__"],
)
# copybara:strip_end

@ -1,6 +1,7 @@
workspace(name = "upb")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
load("//bazel:workspace_deps.bzl", "upb_deps")
upb_deps()
@ -37,3 +38,11 @@ http_archive(
strip_prefix = "benchmark-16703ff83c1ae6d53e5155df3bb3ab0bc96083be",
sha256 = "59f918c8ccd4d74b6ac43484467b500f1d64b40cc1010daa055375b322a43ba3",
)
new_git_repository(
name = "com_google_googleapis",
remote = "https://github.com/googleapis/googleapis.git",
branch = "master",
build_file = "//benchmarks:BUILD.googleapis",
patch_cmds = ["find google -type f -name BUILD.bazel -delete"],
)

@ -2,6 +2,36 @@
load(":upb_proto_library.bzl", "GeneratedSrcsInfo")
UPB_DEFAULT_CPPOPTS = select({
"//:windows": [],
"//conditions:default": [
# copybara:strip_for_google3_begin
"-Wextra",
# "-Wshorten-64-to-32", # not in GCC (and my Kokoro images doesn't have Clang)
"-Werror",
"-Wno-long-long",
# copybara:strip_end
],
})
UPB_DEFAULT_COPTS = select({
"//:windows": [],
"//:fasttable_enabled_setting": ["-std=gnu99", "-DUPB_ENABLE_FASTTABLE"],
"//conditions:default": [
# copybara:strip_for_google3_begin
"-std=c99",
"-pedantic",
"-Werror=pedantic",
"-Wall",
"-Wstrict-prototypes",
# GCC (at least) emits spurious warnings for this that cannot be fixed
# without introducing redundant initialization (with runtime cost):
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
#"-Wno-maybe-uninitialized",
# copybara:strip_end
],
})
def _librule(name):
return name + "_lib"
@ -58,50 +88,6 @@ def make_shell_script(name, contents, out):
cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % contents,
)
def generated_file_staleness_test(name, outs, generated_pattern):
"""Tests that checked-in file(s) match the contents of generated file(s).
The resulting test will verify that all output files exist and have the
correct contents. If the test fails, it can be invoked with --fix to
bring the checked-in files up to date.
Args:
name: Name of the rule.
outs: the checked-in files that are copied from generated files.
generated_pattern: the pattern for transforming each "out" file into a
generated file. For example, if generated_pattern="generated/%s" then
a file foo.txt will look for generated file generated/foo.txt.
"""
script_name = name + ".py"
script_src = "//:tools/staleness_test.py"
# Filter out non-existing rules so Blaze doesn't error out before we even
# run the test.
existing_outs = native.glob(include = outs)
# The file list contains a few extra bits of information at the end.
# These get unpacked by the Config class in staleness_test_lib.py.
file_list = outs + [generated_pattern, native.package_name() or ".", name]
native.genrule(
name = name + "_makescript",
outs = [script_name],
srcs = [script_src],
testonly = 1,
cmd = "cat $(location " + script_src + ") > $@; " +
"sed -i.bak -e 's|INSERT_FILE_LIST_HERE|" + "\\\n ".join(file_list) + "|' $@",
)
native.py_test(
name = name,
srcs = [script_name],
data = existing_outs + [generated_pattern % file for file in outs],
deps = [
"//:staleness_test_lib",
],
)
# upb_amalgamation() rule, with file_list aspect.
SrcList = provider(
@ -156,7 +142,3 @@ upb_amalgamation = rule(
},
implementation = _upb_amalgamation,
)
def licenses(*args):
# No-op (for Google-internal usage).
pass

@ -4,7 +4,6 @@ package(
cc_library(
name = "liblua_headers",
defines = ["LUA_USE_LINUX"],
hdrs = [
"src/lauxlib.h",
"src/lua.h",
@ -12,6 +11,7 @@ cc_library(
"src/luaconf.h",
"src/lualib.h",
],
defines = ["LUA_USE_LINUX"],
includes = ["src"],
)
@ -72,7 +72,6 @@ cc_library(
"src/lzio.c",
"src/lzio.h",
],
defines = ["LUA_USE_LINUX"],
hdrs = [
"src/lauxlib.h",
"src/lua.h",
@ -80,6 +79,7 @@ cc_library(
"src/luaconf.h",
"src/lualib.h",
],
defines = ["LUA_USE_LINUX"],
includes = ["src"],
linkopts = [
"-lm",
@ -92,11 +92,11 @@ cc_binary(
srcs = [
"src/lua.c",
],
deps = [
":liblua",
],
linkopts = [
"-lreadline",
"-rdynamic",
],
deps = [
":liblua",
],
)

@ -1,4 +1,3 @@
package(
default_visibility = ["//visibility:public"],
)
@ -158,7 +157,10 @@ cc_binary(
"aapl/avlimelkey.h",
"aapl/avltree.h",
],
includes = ["ragel", "aapl"],
includes = [
"aapl",
"ragel",
],
)
config_h_contents = """

@ -9,7 +9,7 @@ load("@rules_proto//proto:defs.bzl", "ProtoInfo") # copybara:strip_for_google3
# Generic support code #########################################################
_is_bazel = not hasattr(native, "genmpm")
_is_bazel = True # copybara:replace_for_google3 _is_bazel = False
def _get_real_short_path(file):
# For some reason, files from other archives have short paths that look like:
@ -18,25 +18,20 @@ def _get_real_short_path(file):
if short_path.startswith("../"):
second_slash = short_path.index("/", 3)
short_path = short_path[second_slash + 1:]
# Sometimes it has another few prefixes like:
# _virtual_imports/any_proto/google/protobuf/any.proto
# benchmarks/_virtual_imports/100_msgs_proto/benchmarks/100_msgs.proto
# We want just google/protobuf/any.proto.
if short_path.startswith("_virtual_imports"):
short_path = short_path.split("/", 2)[-1]
virtual_imports = "_virtual_imports/"
if virtual_imports in short_path:
short_path = short_path.split(virtual_imports)[1].split("/", 1)[1]
return short_path
def _get_real_root(file):
real_short_path = _get_real_short_path(file)
return file.path[:-len(real_short_path) - 1]
def _get_real_roots(files):
roots = {}
for file in files:
real_root = _get_real_root(file)
if real_root:
roots[real_root] = True
return roots.keys()
def _generate_output_file(ctx, src, extension):
real_short_path = _get_real_short_path(src)
real_short_path = paths.relativize(real_short_path, ctx.label.package)
@ -51,7 +46,7 @@ def _filter_none(elems):
out.append(elem)
return out
def _cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos):
def _cc_library_func(ctx, name, hdrs, srcs, copts, dep_ccinfos):
"""Like cc_library(), but callable from rules.
Args:
@ -87,6 +82,7 @@ def _cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos):
name = name,
srcs = srcs,
public_hdrs = hdrs,
user_compile_flags = copts,
compilation_contexts = compilation_contexts,
**blaze_only_args
)
@ -105,6 +101,44 @@ def _cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos):
linking_context = linking_context,
)
# Build setting for whether fasttable code generation is enabled ###############
_FastTableEnabled = provider(
fields = {
"enabled": "whether fasttable is enabled",
},
)
def fasttable_enabled_impl(ctx):
raw_setting = ctx.build_setting_value
if raw_setting:
# TODO(haberman): check that the target CPU supports fasttable.
pass
return _FastTableEnabled(enabled = raw_setting)
upb_fasttable_enabled = rule(
implementation = fasttable_enabled_impl,
build_setting = config.bool(flag = True),
)
# Dummy rule to expose select() copts to aspects ##############################
_UpbProtoLibraryCopts = provider(
fields = {
"copts": "copts for upb_proto_library()",
},
)
def upb_proto_library_copts_impl(ctx):
return _UpbProtoLibraryCopts(copts = ctx.attr.copts)
upb_proto_library_copts = rule(
implementation = upb_proto_library_copts_impl,
attrs = {"copts": attr.string_list(default = [])},
)
# upb_proto_library / upb_proto_reflection_library shared code #################
GeneratedSrcsInfo = provider(
@ -119,24 +153,29 @@ _UpbDefsWrappedCcInfo = provider(fields = ["cc_info"])
_WrappedGeneratedSrcsInfo = provider(fields = ["srcs"])
_WrappedDefsGeneratedSrcsInfo = provider(fields = ["srcs"])
def _compile_upb_protos(ctx, proto_info, proto_sources, ext):
def _compile_upb_protos(ctx, generator, proto_info, proto_sources):
if len(proto_sources) == 0:
return GeneratedSrcsInfo(srcs = [], hdrs = [])
ext = "." + generator
tool = getattr(ctx.executable, "_gen_" + generator)
srcs = [_generate_output_file(ctx, name, ext + ".c") for name in proto_sources]
hdrs = [_generate_output_file(ctx, name, ext + ".h") for name in proto_sources]
transitive_sets = proto_info.transitive_descriptor_sets.to_list()
fasttable_enabled = (hasattr(ctx.attr, "_fasttable_enabled") and
ctx.attr._fasttable_enabled[_FastTableEnabled].enabled)
codegen_params = "fasttable:" if fasttable_enabled else ""
ctx.actions.run(
inputs = depset(
direct = [proto_info.direct_descriptor_set],
transitive = [proto_info.transitive_descriptor_sets],
),
tools = [ctx.executable._upbc],
tools = [tool],
outputs = srcs + hdrs,
executable = ctx.executable._protoc,
arguments = [
"--upb_out=" + _get_real_root(srcs[0]),
"--plugin=protoc-gen-upb=" + ctx.executable._upbc.path,
"--" + generator + "_out=" + codegen_params + _get_real_root(srcs[0]),
"--plugin=protoc-gen-" + generator + "=" + tool.path,
"--descriptor_set_in=" + ctx.configuration.host_path_separator.join([f.path for f in transitive_sets]),
] +
[_get_real_short_path(file) for file in proto_sources],
@ -165,10 +204,7 @@ def _upb_proto_rule_impl(ctx):
fail("proto_library rule must generate _UpbWrappedCcInfo or " +
"_UpbDefsWrappedCcInfo (aspect should have handled this).")
if type(cc_info.linking_context.libraries_to_link) == "list":
lib = cc_info.linking_context.libraries_to_link[0]
else:
lib = cc_info.linking_context.libraries_to_link.to_list()[0]
lib = cc_info.linking_context.linker_inputs.to_list()[0].libraries[0]
files = _filter_none([
lib.static_library,
lib.pic_static_library,
@ -180,33 +216,32 @@ def _upb_proto_rule_impl(ctx):
cc_info,
]
def _upb_proto_aspect_impl(target, ctx, cc_provider, file_provider):
def _upb_proto_aspect_impl(target, ctx, generator, cc_provider, file_provider):
proto_info = target[ProtoInfo]
files = _compile_upb_protos(ctx, proto_info, proto_info.direct_sources, ctx.attr._ext)
deps = ctx.rule.attr.deps + ctx.attr._upb
if cc_provider == _UpbDefsWrappedCcInfo:
deps += ctx.attr._upb_reflection
files = _compile_upb_protos(ctx, generator, proto_info, proto_info.direct_sources)
deps = ctx.rule.attr.deps + getattr(ctx.attr, "_" + generator)
dep_ccinfos = [dep[CcInfo] for dep in deps if CcInfo in dep]
dep_ccinfos += [dep[_UpbWrappedCcInfo].cc_info for dep in deps if _UpbWrappedCcInfo in dep]
dep_ccinfos += [dep[_UpbDefsWrappedCcInfo].cc_info for dep in deps if _UpbDefsWrappedCcInfo in dep]
if cc_provider == _UpbDefsWrappedCcInfo:
if generator == "upbdefs":
if _UpbWrappedCcInfo not in target:
fail("Target should have _UpbDefsWrappedCcInfo provider")
dep_ccinfos += [target[_UpbWrappedCcInfo].cc_info]
cc_info = _cc_library_func(
ctx = ctx,
name = ctx.rule.attr.name + ctx.attr._ext,
name = ctx.rule.attr.name + "." + generator,
hdrs = files.hdrs,
srcs = files.srcs,
copts = ctx.attr._copts[_UpbProtoLibraryCopts].copts,
dep_ccinfos = dep_ccinfos,
)
return [cc_provider(cc_info = cc_info), file_provider(srcs = files)]
def _upb_proto_library_aspect_impl(target, ctx):
return _upb_proto_aspect_impl(target, ctx, _UpbWrappedCcInfo, _WrappedGeneratedSrcsInfo)
return _upb_proto_aspect_impl(target, ctx, "upb", _UpbWrappedCcInfo, _WrappedGeneratedSrcsInfo)
def _upb_proto_reflection_library_aspect_impl(target, ctx):
return _upb_proto_aspect_impl(target, ctx, _UpbDefsWrappedCcInfo, _WrappedDefsGeneratedSrcsInfo)
return _upb_proto_aspect_impl(target, ctx, "upbdefs", _UpbDefsWrappedCcInfo, _WrappedDefsGeneratedSrcsInfo)
def _maybe_add(d):
if not _is_bazel:
@ -221,10 +256,13 @@ def _maybe_add(d):
_upb_proto_library_aspect = aspect(
attrs = _maybe_add({
"_upbc": attr.label(
"_copts": attr.label(
default = "//:upb_proto_library_copts__for_generated_code_only_do_not_use",
),
"_gen_upb": attr.label(
executable = True,
cfg = "host",
default = "//:protoc-gen-upb",
default = "//upbc:protoc-gen-upb",
),
"_protoc": attr.label(
executable = True,
@ -238,7 +276,7 @@ _upb_proto_library_aspect = aspect(
"//:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
"//:upb",
]),
"_ext": attr.string(default = ".upb"),
"_fasttable_enabled": attr.label(default = "//:fasttable_enabled"),
}),
implementation = _upb_proto_library_aspect_impl,
provides = [
@ -248,6 +286,7 @@ _upb_proto_library_aspect = aspect(
attr_aspects = ["deps"],
fragments = ["cpp"],
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
incompatible_use_toolchain_transition = True,
)
upb_proto_library = rule(
@ -266,10 +305,13 @@ upb_proto_library = rule(
_upb_proto_reflection_library_aspect = aspect(
attrs = _maybe_add({
"_upbc": attr.label(
"_copts": attr.label(
default = "//:upb_proto_library_copts__for_generated_code_only_do_not_use",
),
"_gen_upbdefs": attr.label(
executable = True,
cfg = "host",
default = "//:protoc-gen-upb",
default = "//upbc:protoc-gen-upbdefs",
),
"_protoc": attr.label(
executable = True,
@ -279,21 +321,12 @@ _upb_proto_reflection_library_aspect = aspect(
"_cc_toolchain": attr.label(
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
),
# For unknown reasons, this gets overwritten.
"_upb": attr.label_list(
default = [
"//:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
"//:upb",
"//:reflection",
],
),
"_upb_reflection": attr.label_list(
"_upbdefs": attr.label_list(
default = [
"//:upb",
"//:reflection",
],
),
"_ext": attr.string(default = ".upbdefs"),
}),
implementation = _upb_proto_reflection_library_aspect_impl,
provides = [
@ -307,6 +340,7 @@ _upb_proto_reflection_library_aspect = aspect(
attr_aspects = ["deps"],
fragments = ["cpp"],
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
incompatible_use_toolchain_transition = True,
)
upb_proto_reflection_library = rule(

@ -1,4 +1,3 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
@ -9,14 +8,14 @@ def upb_deps():
name = "com_google_absl",
commit = "df3ea785d8c30a9503321a3d35ee7d35808f190d", # LTS 2020-02-25
remote = "https://github.com/abseil/abseil-cpp.git",
shallow_since = "1583355457 -0500"
shallow_since = "1583355457 -0500",
)
maybe(
git_repository,
name = "com_google_protobuf",
remote = "https://github.com/protocolbuffers/protobuf.git",
commit = "5f5efe50c5bef20042645b51a697f58b0704ac89", # Need to use Git until proto3 optional is released
commit = "c8f76331abf682c289fa79f05b2ee39cc7bf5a48", # Need to use Git until proto3 optional is released
)
maybe(

@ -1,32 +0,0 @@
#!/usr/bin/env python3
import json
import subprocess
import re
def Run(cmd):
subprocess.check_call(cmd, shell=True)
def RunAgainstBranch(branch, outfile, runs=12):
tmpfile = "/tmp/bench-output.json"
Run("rm -rf {}".format(tmpfile))
Run("git checkout {}".format(branch))
Run("bazel build -c opt :benchmark")
Run("./bazel-bin/benchmark --benchmark_out_format=json --benchmark_out={} --benchmark_repetitions={}".format(tmpfile, runs))
with open(tmpfile) as f:
bench_json = json.load(f)
with open(outfile, "w") as f:
for run in bench_json["benchmarks"]:
name = re.sub(r'^BM_', 'Benchmark', run["name"])
if name.endswith("_mean") or name.endswith("_median") or name.endswith("_stddev"):
continue
values = (name, run["iterations"], run["cpu_time"])
print("{} {} {} ns/op".format(*values), file=f)
RunAgainstBranch("master", "/tmp/old.txt")
RunAgainstBranch("decoder", "/tmp/new.txt")
Run("~/go/bin/benchstat /tmp/old.txt /tmp/new.txt")

@ -0,0 +1,218 @@
load(
"//bazel:upb_proto_library.bzl",
"upb_proto_library",
"upb_proto_reflection_library",
)
load(
":build_defs.bzl",
"tmpl_cc_binary",
"cc_optimizefor_proto_library",
"expand_suffixes",
"proto_library",
)
licenses(["notice"])
proto_library(
name = "descriptor_proto",
srcs = ["descriptor.proto"],
)
upb_proto_library(
name = "benchmark_descriptor_upb_proto",
deps = [":descriptor_proto"],
)
upb_proto_reflection_library(
name = "benchmark_descriptor_upb_proto_reflection",
deps = [":descriptor_proto"],
)
upb_proto_reflection_library(
name = "ads_upb_proto_reflection",
deps = ["@com_google_googleapis//:ads_proto"],
)
cc_proto_library(
name = "benchmark_descriptor_cc_proto",
deps = [":descriptor_proto"],
)
proto_library(
name = "benchmark_descriptor_sv_proto",
srcs = ["descriptor_sv.proto"],
)
cc_proto_library(
name = "benchmark_descriptor_sv_cc_proto",
deps = [":benchmark_descriptor_sv_proto"],
)
cc_binary(
name = "benchmark",
testonly = 1,
srcs = ["benchmark.cc"],
deps = [
":ads_upb_proto_reflection",
":benchmark_descriptor_cc_proto",
":benchmark_descriptor_sv_cc_proto",
":benchmark_descriptor_upb_proto",
":benchmark_descriptor_upb_proto_reflection",
"//:descriptor_upb_proto",
"//:reflection",
"@com_github_google_benchmark//:benchmark_main",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_protobuf//:protobuf",
],
)
# Size benchmarks.
SIZE_BENCHMARKS = {
"empty": "Empty",
"descriptor": "FileDescriptorSet",
"100_msgs": "Message100",
"200_msgs": "Message200",
"100_fields": "Message",
"200_fields": "Message",
}
py_binary(
name = "gen_synthetic_protos",
srcs = ["gen_synthetic_protos.py"],
python_version = "PY3",
)
py_binary(
name = "gen_upb_binary_c",
srcs = ["gen_upb_binary_c.py"],
python_version = "PY3",
)
py_binary(
name = "gen_protobuf_binary_cc",
srcs = ["gen_protobuf_binary_cc.py"],
python_version = "PY3",
)
genrule(
name = "do_gen_synthetic_protos",
tools = [":gen_synthetic_protos"],
outs = [
"100_msgs.proto",
"200_msgs.proto",
"100_fields.proto",
"200_fields.proto",
],
cmd = "$(execpath :gen_synthetic_protos) $(RULEDIR)",
)
proto_library(
name = "100_msgs_proto",
srcs = ["100_msgs.proto"],
)
proto_library(
name = "200_msgs_proto",
srcs = ["200_msgs.proto"],
)
proto_library(
name = "100_fields_proto",
srcs = ["100_fields.proto"],
)
proto_library(
name = "200_fields_proto",
srcs = ["200_fields.proto"],
)
proto_library(
name = "empty_proto",
srcs = ["empty.proto"],
)
[(
upb_proto_library(
name = k + "_upb_proto",
deps = [":" + k + "_proto"],
),
cc_proto_library(
name = k + "_cc_proto",
deps = [":" + k + "_proto"],
),
tmpl_cc_binary(
name = k + "_upb_binary",
testonly = 1,
gen = ":gen_upb_binary_c",
args = [
package_name() + "/" + k + ".upb.h",
"upb_benchmark_" + v,
],
deps = [
":" + k + "_upb_proto",
],
),
tmpl_cc_binary(
name = k + "_protobuf_binary",
testonly = 1,
gen = ":gen_protobuf_binary_cc",
args = [
package_name() + "/" + k + ".pb.h",
"upb_benchmark::" + v,
],
deps = [
":" + k + "_cc_proto",
],
),
cc_optimizefor_proto_library(
srcs = [k + ".proto"],
outs = [k + "_lite.proto"],
name = k + "_cc_lite_proto",
optimize_for = "LITE_RUNTIME",
),
tmpl_cc_binary(
name = k + "_lite_protobuf_binary",
testonly = 1,
gen = ":gen_protobuf_binary_cc",
args = [
package_name() + "/" + k + "_lite.pb.h",
"upb_benchmark::" + v,
],
deps = [
":" + k + "_cc_lite_proto",
],
),
cc_optimizefor_proto_library(
srcs = [k + ".proto"],
outs = [k + "_codesize.proto"],
name = k + "_cc_codesize_proto",
optimize_for = "CODE_SIZE",
),
tmpl_cc_binary(
name = k + "_codesize_protobuf_binary",
testonly = 1,
gen = ":gen_protobuf_binary_cc",
args = [
package_name() + "/" + k + "_codesize.pb.h",
"upb_benchmark::" + v,
],
deps = [
":" + k + "_cc_codesize_proto",
],
)
) for k, v in SIZE_BENCHMARKS.items()]
genrule(
testonly = 1,
name = "size_data",
srcs = expand_suffixes(
SIZE_BENCHMARKS.keys(),
suffixes = ["_upb_binary", "_protobuf_binary", "_lite_protobuf_binary", "_codesize_protobuf_binary"],
),
outs = ["size_data.txt"],
# We want --format=GNU which counts rodata with data, not text.
cmd = "size $$($$OSTYPE == 'linux-gnu' ? '--format=GNU -d' : '') $(SRCS) > $@",
# "size" sometimes isn't available remotely.
local = 1,
)

@ -0,0 +1,29 @@
load(
"@rules_proto//proto:defs.bzl",
"proto_library",
)
proto_library(
name = "ads_proto",
srcs = glob([
"google/ads/googleads/v5/**/*.proto",
"google/api/**/*.proto",
"google/rpc/**/*.proto",
"google/longrunning/**/*.proto",
"google/logging/**/*.proto",
]),
#srcs = ["google/ads/googleads/v5/services/google_ads_service.proto"],
visibility = ["//visibility:public"],
deps = [
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:api_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:field_mask_proto",
"@com_google_protobuf//:struct_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:type_proto",
"@com_google_protobuf//:wrappers_proto",
],
)

@ -0,0 +1,258 @@
#include <benchmark/benchmark.h>
#include <string.h>
#include "absl/container/flat_hash_set.h"
#include "benchmarks/descriptor.pb.h"
#include "benchmarks/descriptor.upb.h"
#include "benchmarks/descriptor.upbdefs.h"
#include "benchmarks/descriptor_sv.pb.h"
#include "google/ads/googleads/v5/services/google_ads_service.upbdefs.h"
#include "google/protobuf/descriptor.pb.h"
#include "upb/def.hpp"
upb_strview descriptor = benchmarks_descriptor_proto_upbdefinit.descriptor;
namespace protobuf = ::google::protobuf;
/* A buffer big enough to parse descriptor.proto without going to heap. */
char buf[65535];
void CollectFileDescriptors(const upb_def_init* file,
std::vector<upb_strview>& serialized_files,
absl::flat_hash_set<const upb_def_init*>& seen) {
if (!seen.insert(file).second) return;
for (upb_def_init **deps = file->deps; *deps; deps++) {
CollectFileDescriptors(*deps, serialized_files, seen);
}
serialized_files.push_back(file->descriptor);
}
static void BM_ArenaOneAlloc(benchmark::State& state) {
for (auto _ : state) {
upb_arena* arena = upb_arena_new();
upb_arena_malloc(arena, 1);
upb_arena_free(arena);
}
}
BENCHMARK(BM_ArenaOneAlloc);
static void BM_ArenaInitialBlockOneAlloc(benchmark::State& state) {
for (auto _ : state) {
upb_arena* arena = upb_arena_init(buf, sizeof(buf), NULL);
upb_arena_malloc(arena, 1);
upb_arena_free(arena);
}
}
BENCHMARK(BM_ArenaInitialBlockOneAlloc);
static void BM_LoadDescriptor_Upb(benchmark::State& state) {
size_t bytes_per_iter = 0;
for (auto _ : state) {
upb::SymbolTable symtab;
upb_benchmark_DescriptorProto_getmsgdef(symtab.ptr());
bytes_per_iter = _upb_symtab_bytesloaded(symtab.ptr());
}
state.SetBytesProcessed(state.iterations() * bytes_per_iter);
}
BENCHMARK(BM_LoadDescriptor_Upb);
static void BM_LoadAdsDescriptor_Upb(benchmark::State& state) {
size_t bytes_per_iter = 0;
for (auto _ : state) {
upb::SymbolTable symtab;
google_ads_googleads_v5_services_SearchGoogleAdsRequest_getmsgdef(
symtab.ptr());
bytes_per_iter = _upb_symtab_bytesloaded(symtab.ptr());
}
state.SetBytesProcessed(state.iterations() * bytes_per_iter);
}
BENCHMARK(BM_LoadAdsDescriptor_Upb);
static void BM_LoadDescriptor_Proto2(benchmark::State& state) {
for (auto _ : state) {
protobuf::Arena arena;
protobuf::StringPiece input(descriptor.data,descriptor.size);
auto proto = protobuf::Arena::CreateMessage<protobuf::FileDescriptorProto>(
&arena);
protobuf::DescriptorPool pool;
bool ok = proto->ParseFrom<protobuf::MessageLite::kMergePartial>(input) &&
pool.BuildFile(*proto) != nullptr;
if (!ok) {
printf("Failed to add file.\n");
exit(1);
}
}
state.SetBytesProcessed(state.iterations() * descriptor.size);
}
BENCHMARK(BM_LoadDescriptor_Proto2);
static void BM_LoadAdsDescriptor_Proto2(benchmark::State& state) {
extern upb_def_init google_ads_googleads_v5_services_google_ads_service_proto_upbdefinit;
std::vector<upb_strview> serialized_files;
absl::flat_hash_set<const upb_def_init*> seen_files;
CollectFileDescriptors(
&google_ads_googleads_v5_services_google_ads_service_proto_upbdefinit,
serialized_files, seen_files);
size_t bytes_per_iter = 0;
for (auto _ : state) {
bytes_per_iter = 0;
protobuf::Arena arena;
protobuf::DescriptorPool pool;
for (auto file : serialized_files) {
protobuf::StringPiece input(file.data, file.size);
auto proto = protobuf::Arena::CreateMessage<protobuf::FileDescriptorProto>(
&arena);
bool ok = proto->ParseFrom<protobuf::MessageLite::kMergePartial>(input) &&
pool.BuildFile(*proto) != nullptr;
if (!ok) {
printf("Failed to add file.\n");
exit(1);
}
bytes_per_iter += input.size();
}
}
state.SetBytesProcessed(state.iterations() * bytes_per_iter);
}
BENCHMARK(BM_LoadAdsDescriptor_Proto2);
enum CopyStrings {
Copy,
Alias,
};
enum ArenaMode {
NoArena,
UseArena,
InitBlock,
};
template <ArenaMode AMode, CopyStrings Copy>
static void BM_Parse_Upb_FileDesc(benchmark::State& state) {
size_t bytes = 0;
for (auto _ : state) {
upb_arena *arena;
if (AMode == InitBlock) {
arena = upb_arena_init(buf, sizeof(buf), NULL);
} else {
arena = upb_arena_new();
}
upb_benchmark_FileDescriptorProto* set =
upb_benchmark_FileDescriptorProto_parse_ex(
descriptor.data, descriptor.size, arena,
Copy == Alias ? UPB_DECODE_ALIAS : 0);
if (!set) {
printf("Failed to parse.\n");
exit(1);
}
bytes += descriptor.size;
upb_arena_free(arena);
}
state.SetBytesProcessed(state.iterations() * descriptor.size);
}
BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, UseArena, Copy);
BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, UseArena, Alias);
BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, InitBlock, Copy);
BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, InitBlock, Alias);
template <ArenaMode AMode, class P>
struct Proto2Factory;
template<class P>
struct Proto2Factory<NoArena, P> {
public:
P* GetProto() { return &proto_; }
private:
P proto_;
};
template <class P>
struct Proto2Factory<UseArena, P> {
public:
P* GetProto() { return protobuf::Arena::CreateMessage<P>(&arena_); }
private:
protobuf::Arena arena_;
};
template <class P>
struct Proto2Factory<InitBlock, P> {
public:
Proto2Factory() : arena_(GetOptions()) {}
P* GetProto() { return protobuf::Arena::CreateMessage<P>(&arena_); }
private:
protobuf::ArenaOptions GetOptions() {
protobuf::ArenaOptions opts;
opts.initial_block = buf;
opts.initial_block_size = sizeof(buf);
return opts;
}
protobuf::Arena arena_;
};
using FileDesc = ::upb_benchmark::FileDescriptorProto;
using FileDescSV = ::upb_benchmark::sv::FileDescriptorProto;
template <class P, ArenaMode AMode, CopyStrings kCopy>
void BM_Parse_Proto2(benchmark::State& state) {
size_t bytes = 0;
constexpr protobuf::MessageLite::ParseFlags kParseFlags =
kCopy == Copy
? protobuf::MessageLite::ParseFlags::kMergePartial
: protobuf::MessageLite::ParseFlags::kMergePartialWithAliasing;
for (auto _ : state) {
Proto2Factory<AMode, P> proto_factory;
auto proto = proto_factory.GetProto();
protobuf::StringPiece input(descriptor.data,descriptor.size);
bool ok = proto->template ParseFrom<kParseFlags>(input);
if (!ok) {
printf("Failed to parse.\n");
exit(1);
}
bytes += descriptor.size;
}
state.SetBytesProcessed(state.iterations() * descriptor.size);
}
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDesc, NoArena, Copy);
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDesc, UseArena, Copy);
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDesc, InitBlock, Copy);
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescSV, InitBlock, Alias);
static void BM_SerializeDescriptor_Proto2(benchmark::State& state) {
size_t bytes = 0;
upb_benchmark::FileDescriptorProto proto;
proto.ParseFromArray(descriptor.data, descriptor.size);
for (auto _ : state) {
proto.SerializePartialToArray(buf, sizeof(buf));
bytes += descriptor.size;
}
state.SetBytesProcessed(state.iterations() * descriptor.size);
}
BENCHMARK(BM_SerializeDescriptor_Proto2);
static void BM_SerializeDescriptor_Upb(benchmark::State& state) {
int64_t total = 0;
upb_arena* arena = upb_arena_new();
upb_benchmark_FileDescriptorProto* set =
upb_benchmark_FileDescriptorProto_parse(descriptor.data, descriptor.size,
arena);
if (!set) {
printf("Failed to parse.\n");
exit(1);
}
for (auto _ : state) {
upb_arena* enc_arena = upb_arena_init(buf, sizeof(buf), NULL);
size_t size;
char* data =
upb_benchmark_FileDescriptorProto_serialize(set, enc_arena, &size);
if (!data) {
printf("Failed to serialize.\n");
exit(1);
}
total += size;
}
state.SetBytesProcessed(total);
}
BENCHMARK(BM_SerializeDescriptor_Upb);

@ -0,0 +1,63 @@
# copybara:insert_for_google3_begin
# load("//tools/build_defs/proto/cpp:cc_proto_library.bzl", _cc_proto_library="cc_proto_library")
# copybara:insert_end
# copybara:strip_for_google3_begin
_cc_proto_library = native.cc_proto_library
# copybara:strip_end
def proto_library(**kwargs):
native.proto_library(
# copybara:insert_for_google3_begin
# cc_api_version = 2,
# copybara:insert_end
**kwargs,
)
def tmpl_cc_binary(name, gen, args, replacements = [], **kwargs):
srcs = [name + ".cc"]
native.genrule(
name = name + "_gen_srcs",
tools = [gen],
outs = srcs,
cmd = "$(location " + gen + ") " + " ".join(args) + " > $@",
)
native.cc_binary(
# copybara:insert_for_google3_begin
# malloc="//base:system_malloc",
# features = ["-static_linking_mode"],
# copybara:insert_end
name = name,
srcs = srcs,
**kwargs,
)
def cc_optimizefor_proto_library(name, srcs, outs, optimize_for):
if len(srcs) != 1:
fail("Currently srcs must have exactly 1 element")
native.genrule(
name = name + "_gen_proto",
srcs = srcs,
outs = outs,
cmd = "cp $< $@ && chmod a+w $@ && echo 'option optimize_for = " + optimize_for + ";' >> $@",
)
proto_library(
name = name + "_proto",
srcs = outs,
)
_cc_proto_library(
name = name,
deps = [":" + name + "_proto"],
)
def expand_suffixes(vals, suffixes):
ret = []
for val in vals:
for suffix in suffixes:
ret.append(val + suffix)
return ret

@ -0,0 +1,89 @@
#!/usr/bin/env python3
"""Benchmarks the current working directory against a given baseline.
This script benchmarks both size and speed. Sample output:
"""
import contextlib
import json
import os
import re
import subprocess
import sys
import tempfile
@contextlib.contextmanager
def GitWorktree(commit):
tmpdir = tempfile.mkdtemp()
subprocess.run(['git', 'worktree', 'add', '-q', '-d', tmpdir, commit], check=True)
cwd = os.getcwd()
os.chdir(tmpdir)
try:
yield tmpdir
finally:
os.chdir(cwd)
subprocess.run(['git', 'worktree', 'remove', tmpdir], check=True)
def Run(cmd):
subprocess.check_call(cmd, shell=True)
def Benchmark(outbase, bench_cpu=True, runs=12, fasttable=False):
tmpfile = "/tmp/bench-output.json"
Run("rm -rf {}".format(tmpfile))
#Run("CC=clang bazel test ...")
if fasttable:
extra_args = " --//:fasttable_enabled=true"
else:
extra_args = ""
if bench_cpu:
Run("CC=clang bazel build -c opt --copt=-march=native benchmarks:benchmark" + extra_args)
Run("./bazel-bin/benchmarks/benchmark --benchmark_out_format=json --benchmark_out={} --benchmark_repetitions={}".format(tmpfile, runs))
with open(tmpfile) as f:
bench_json = json.load(f)
# Translate into the format expected by benchstat.
with open(outbase + ".txt", "w") as f:
for run in bench_json["benchmarks"]:
name = run["name"]
name = name.replace(" ", "")
name = re.sub(r'^BM_', 'Benchmark', name)
if name.endswith("_mean") or name.endswith("_median") or name.endswith("_stddev"):
continue
values = (name, run["iterations"], run["cpu_time"])
print("{} {} {} ns/op".format(*values), file=f)
Run("CC=clang bazel build -c opt --copt=-g tests:conformance_upb" + extra_args)
Run("cp -f bazel-bin/tests/conformance_upb {}.bin".format(outbase))
baseline = "master"
bench_cpu = False
fasttable = False
if len(sys.argv) > 1:
baseline = sys.argv[1]
# Quickly verify that the baseline exists.
with GitWorktree(baseline):
pass
# Benchmark our current directory first, since it's more likely to be broken.
Benchmark("/tmp/new", bench_cpu, fasttable=fasttable)
# Benchmark the baseline.
with GitWorktree(baseline):
Benchmark("/tmp/old", bench_cpu, fasttable=fasttable)
print()
print()
if bench_cpu:
Run("~/go/bin/benchstat /tmp/old.txt /tmp/new.txt")
print()
print()
Run("objcopy --strip-debug /tmp/old.bin /tmp/old.bin.stripped")
Run("objcopy --strip-debug /tmp/new.bin /tmp/new.bin.stripped")
Run("~/code/bloaty/bloaty /tmp/new.bin.stripped -- /tmp/old.bin.stripped --debug-file=/tmp/old.bin --debug-file=/tmp/new.bin -d compileunits,symbols")

@ -0,0 +1,905 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
//
// The messages in this file describe the definitions found in .proto files.
// A valid .proto file can be translated directly to a FileDescriptorProto
// without any other information (e.g. without reading its imports).
syntax = "proto2";
package upb_benchmark;
option go_package = "google.golang.org/protobuf/types/descriptorpb";
option java_package = "com.google.protobuf";
option java_outer_classname = "DescriptorProtos";
option csharp_namespace = "Google.Protobuf.Reflection";
option objc_class_prefix = "GPB";
option cc_enable_arenas = true;
// The protocol compiler can output a FileDescriptorSet containing the .proto
// files it parses.
message FileDescriptorSet {
repeated FileDescriptorProto file = 1;
}
// Describes a complete .proto file.
message FileDescriptorProto {
optional string name = 1; // file name, relative to root of source tree
optional string package = 2; // e.g. "foo", "foo.bar", etc.
// Names of files imported by this file.
repeated string dependency = 3;
// Indexes of the public imported files in the dependency list above.
repeated int32 public_dependency = 10;
// Indexes of the weak imported files in the dependency list.
// For Google-internal migration only. Do not use.
repeated int32 weak_dependency = 11;
// All top-level definitions in this file.
repeated DescriptorProto message_type = 4;
repeated EnumDescriptorProto enum_type = 5;
repeated ServiceDescriptorProto service = 6;
repeated FieldDescriptorProto extension = 7;
optional FileOptions options = 8;
// This field contains optional information about the original source code.
// You may safely remove this entire field without harming runtime
// functionality of the descriptors -- the information is needed only by
// development tools.
optional SourceCodeInfo source_code_info = 9;
// The syntax of the proto file.
// The supported values are "proto2" and "proto3".
optional string syntax = 12;
}
// Describes a message type.
message DescriptorProto {
optional string name = 1;
repeated FieldDescriptorProto field = 2;
repeated FieldDescriptorProto extension = 6;
repeated DescriptorProto nested_type = 3;
repeated EnumDescriptorProto enum_type = 4;
message ExtensionRange {
optional int32 start = 1; // Inclusive.
optional int32 end = 2; // Exclusive.
optional ExtensionRangeOptions options = 3;
}
repeated ExtensionRange extension_range = 5;
repeated OneofDescriptorProto oneof_decl = 8;
optional MessageOptions options = 7;
// Range of reserved tag numbers. Reserved tag numbers may not be used by
// fields or extension ranges in the same message. Reserved ranges may
// not overlap.
message ReservedRange {
optional int32 start = 1; // Inclusive.
optional int32 end = 2; // Exclusive.
}
repeated ReservedRange reserved_range = 9;
// Reserved field names, which may not be used by fields in the same message.
// A given name may only be reserved once.
repeated string reserved_name = 10;
}
message ExtensionRangeOptions {
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
// Describes a field within a message.
message FieldDescriptorProto {
enum Type {
// 0 is reserved for errors.
// Order is weird for historical reasons.
TYPE_DOUBLE = 1;
TYPE_FLOAT = 2;
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if
// negative values are likely.
TYPE_INT64 = 3;
TYPE_UINT64 = 4;
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if
// negative values are likely.
TYPE_INT32 = 5;
TYPE_FIXED64 = 6;
TYPE_FIXED32 = 7;
TYPE_BOOL = 8;
TYPE_STRING = 9;
// Tag-delimited aggregate.
// Group type is deprecated and not supported in proto3. However, Proto3
// implementations should still be able to parse the group wire format and
// treat group fields as unknown fields.
TYPE_GROUP = 10;
TYPE_MESSAGE = 11; // Length-delimited aggregate.
// New in version 2.
TYPE_BYTES = 12;
TYPE_UINT32 = 13;
TYPE_ENUM = 14;
TYPE_SFIXED32 = 15;
TYPE_SFIXED64 = 16;
TYPE_SINT32 = 17; // Uses ZigZag encoding.
TYPE_SINT64 = 18; // Uses ZigZag encoding.
}
enum Label {
// 0 is reserved for errors
LABEL_OPTIONAL = 1;
LABEL_REQUIRED = 2;
LABEL_REPEATED = 3;
}
optional string name = 1;
optional int32 number = 3;
optional Label label = 4;
// If type_name is set, this need not be set. If both this and type_name
// are set, this must be one of TYPE_ENUM, TYPE_MESSAGE or TYPE_GROUP.
optional Type type = 5;
// For message and enum types, this is the name of the type. If the name
// starts with a '.', it is fully-qualified. Otherwise, C++-like scoping
// rules are used to find the type (i.e. first the nested types within this
// message are searched, then within the parent, on up to the root
// namespace).
optional string type_name = 6;
// For extensions, this is the name of the type being extended. It is
// resolved in the same manner as type_name.
optional string extendee = 2;
// For numeric types, contains the original text representation of the value.
// For booleans, "true" or "false".
// For strings, contains the default text contents (not escaped in any way).
// For bytes, contains the C escaped value. All bytes >= 128 are escaped.
// TODO(kenton): Base-64 encode?
optional string default_value = 7;
// If set, gives the index of a oneof in the containing type's oneof_decl
// list. This field is a member of that oneof.
optional int32 oneof_index = 9;
// JSON name of this field. The value is set by protocol compiler. If the
// user has set a "json_name" option on this field, that option's value
// will be used. Otherwise, it's deduced from the field's name by converting
// it to camelCase.
optional string json_name = 10;
optional FieldOptions options = 8;
// If true, this is a proto3 "optional". When a proto3 field is optional, it
// tracks presence regardless of field type.
//
// When proto3_optional is true, this field must be belong to a oneof to
// signal to old proto3 clients that presence is tracked for this field. This
// oneof is known as a "synthetic" oneof, and this field must be its sole
// member (each proto3 optional field gets its own synthetic oneof). Synthetic
// oneofs exist in the descriptor only, and do not generate any API. Synthetic
// oneofs must be ordered after all "real" oneofs.
//
// For message fields, proto3_optional doesn't create any semantic change,
// since non-repeated message fields always track presence. However it still
// indicates the semantic detail of whether the user wrote "optional" or not.
// This can be useful for round-tripping the .proto file. For consistency we
// give message fields a synthetic oneof also, even though it is not required
// to track presence. This is especially important because the parser can't
// tell if a field is a message or an enum, so it must always create a
// synthetic oneof.
//
// Proto2 optional fields do not set this flag, because they already indicate
// optional with `LABEL_OPTIONAL`.
optional bool proto3_optional = 17;
}
// Describes a oneof.
message OneofDescriptorProto {
optional string name = 1;
optional OneofOptions options = 2;
}
// Describes an enum type.
message EnumDescriptorProto {
optional string name = 1;
repeated EnumValueDescriptorProto value = 2;
optional EnumOptions options = 3;
// Range of reserved numeric values. Reserved values may not be used by
// entries in the same enum. Reserved ranges may not overlap.
//
// Note that this is distinct from DescriptorProto.ReservedRange in that it
// is inclusive such that it can appropriately represent the entire int32
// domain.
message EnumReservedRange {
optional int32 start = 1; // Inclusive.
optional int32 end = 2; // Inclusive.
}
// Range of reserved numeric values. Reserved numeric values may not be used
// by enum values in the same enum declaration. Reserved ranges may not
// overlap.
repeated EnumReservedRange reserved_range = 4;
// Reserved enum value names, which may not be reused. A given name may only
// be reserved once.
repeated string reserved_name = 5;
}
// Describes a value within an enum.
message EnumValueDescriptorProto {
optional string name = 1;
optional int32 number = 2;
optional EnumValueOptions options = 3;
}
// Describes a service.
message ServiceDescriptorProto {
optional string name = 1;
repeated MethodDescriptorProto method = 2;
optional ServiceOptions options = 3;
}
// Describes a method of a service.
message MethodDescriptorProto {
optional string name = 1;
// Input and output type names. These are resolved in the same way as
// FieldDescriptorProto.type_name, but must refer to a message type.
optional string input_type = 2;
optional string output_type = 3;
optional MethodOptions options = 4;
// Identifies if client streams multiple client messages
optional bool client_streaming = 5 [default = false];
// Identifies if server streams multiple server messages
optional bool server_streaming = 6 [default = false];
}
// ===================================================================
// Options
// Each of the definitions above may have "options" attached. These are
// just annotations which may cause code to be generated slightly differently
// or may contain hints for code that manipulates protocol messages.
//
// Clients may define custom options as extensions of the *Options messages.
// These extensions may not yet be known at parsing time, so the parser cannot
// store the values in them. Instead it stores them in a field in the *Options
// message called uninterpreted_option. This field must have the same name
// across all *Options messages. We then use this field to populate the
// extensions when we build a descriptor, at which point all protos have been
// parsed and so all extensions are known.
//
// Extension numbers for custom options may be chosen as follows:
// * For options which will only be used within a single application or
// organization, or for experimental options, use field numbers 50000
// through 99999. It is up to you to ensure that you do not use the
// same number for multiple options.
// * For options which will be published and used publicly by multiple
// independent entities, e-mail protobuf-global-extension-registry@google.com
// to reserve extension numbers. Simply provide your project name (e.g.
// Objective-C plugin) and your project website (if available) -- there's no
// need to explain how you intend to use them. Usually you only need one
// extension number. You can declare multiple options with only one extension
// number by putting them in a sub-message. See the Custom Options section of
// the docs for examples:
// https://developers.google.com/protocol-buffers/docs/proto#options
// If this turns out to be popular, a web service will be set up
// to automatically assign option numbers.
message FileOptions {
// Sets the Java package where classes generated from this .proto will be
// placed. By default, the proto package is used, but this is often
// inappropriate because proto packages do not normally start with backwards
// domain names.
optional string java_package = 1;
// If set, all the classes from the .proto file are wrapped in a single
// outer class with the given name. This applies to both Proto1
// (equivalent to the old "--one_java_file" option) and Proto2 (where
// a .proto always translates to a single class, but you may want to
// explicitly choose the class name).
optional string java_outer_classname = 8;
// If set true, then the Java code generator will generate a separate .java
// file for each top-level message, enum, and service defined in the .proto
// file. Thus, these types will *not* be nested inside the outer class
// named by java_outer_classname. However, the outer class will still be
// generated to contain the file's getDescriptor() method as well as any
// top-level extensions defined in the file.
optional bool java_multiple_files = 10 [default = false];
// This option does nothing.
optional bool java_generate_equals_and_hash = 20 [deprecated=true];
// If set true, then the Java2 code generator will generate code that
// throws an exception whenever an attempt is made to assign a non-UTF-8
// byte sequence to a string field.
// Message reflection will do the same.
// However, an extension field still accepts non-UTF-8 byte sequences.
// This option has no effect on when used with the lite runtime.
optional bool java_string_check_utf8 = 27 [default = false];
// Generated classes can be optimized for speed or code size.
enum OptimizeMode {
SPEED = 1; // Generate complete code for parsing, serialization,
// etc.
CODE_SIZE = 2; // Use ReflectionOps to implement these methods.
LITE_RUNTIME = 3; // Generate code using MessageLite and the lite runtime.
}
optional OptimizeMode optimize_for = 9 [default = SPEED];
// Sets the Go package where structs generated from this .proto will be
// placed. If omitted, the Go package will be derived from the following:
// - The basename of the package import path, if provided.
// - Otherwise, the package statement in the .proto file, if present.
// - Otherwise, the basename of the .proto file, without extension.
optional string go_package = 11;
// Should generic services be generated in each language? "Generic" services
// are not specific to any particular RPC system. They are generated by the
// main code generators in each language (without additional plugins).
// Generic services were the only kind of service generation supported by
// early versions of google.protobuf.
//
// Generic services are now considered deprecated in favor of using plugins
// that generate code specific to your particular RPC system. Therefore,
// these default to false. Old code which depends on generic services should
// explicitly set them to true.
optional bool cc_generic_services = 16 [default = false];
optional bool java_generic_services = 17 [default = false];
optional bool py_generic_services = 18 [default = false];
optional bool php_generic_services = 42 [default = false];
// Is this file deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for everything in the file, or it will be completely ignored; in the very
// least, this is a formalization for deprecating files.
optional bool deprecated = 23 [default = false];
// Enables the use of arenas for the proto messages in this file. This applies
// only to generated classes for C++.
optional bool cc_enable_arenas = 31 [default = true];
// Sets the objective c class prefix which is prepended to all objective c
// generated classes from this .proto. There is no default.
optional string objc_class_prefix = 36;
// Namespace for generated classes; defaults to the package.
optional string csharp_namespace = 37;
// By default Swift generators will take the proto package and CamelCase it
// replacing '.' with underscore and use that to prefix the types/symbols
// defined. When this options is provided, they will use this value instead
// to prefix the types/symbols defined.
optional string swift_prefix = 39;
// Sets the php class prefix which is prepended to all php generated classes
// from this .proto. Default is empty.
optional string php_class_prefix = 40;
// Use this option to change the namespace of php generated classes. Default
// is empty. When this option is empty, the package name will be used for
// determining the namespace.
optional string php_namespace = 41;
// Use this option to change the namespace of php generated metadata classes.
// Default is empty. When this option is empty, the proto file name will be
// used for determining the namespace.
optional string php_metadata_namespace = 44;
// Use this option to change the package of ruby generated classes. Default
// is empty. When this option is not set, the package name will be used for
// determining the ruby package.
optional string ruby_package = 45;
// The parser stores options it doesn't recognize here.
// See the documentation for the "Options" section above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message.
// See the documentation for the "Options" section above.
extensions 1000 to max;
reserved 38;
}
message MessageOptions {
// Set true to use the old proto1 MessageSet wire format for extensions.
// This is provided for backwards-compatibility with the MessageSet wire
// format. You should not use this for any other reason: It's less
// efficient, has fewer features, and is more complicated.
//
// The message must be defined exactly as follows:
// message Foo {
// option message_set_wire_format = true;
// extensions 4 to max;
// }
// Note that the message cannot have any defined fields; MessageSets only
// have extensions.
//
// All extensions of your type must be singular messages; e.g. they cannot
// be int32s, enums, or repeated messages.
//
// Because this is an option, the above two restrictions are not enforced by
// the protocol compiler.
optional bool message_set_wire_format = 1 [default = false];
// Disables the generation of the standard "descriptor()" accessor, which can
// conflict with a field of the same name. This is meant to make migration
// from proto1 easier; new code should avoid fields named "descriptor".
optional bool no_standard_descriptor_accessor = 2 [default = false];
// Is this message deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for the message, or it will be completely ignored; in the very least,
// this is a formalization for deprecating messages.
optional bool deprecated = 3 [default = false];
// Whether the message is an automatically generated map entry type for the
// maps field.
//
// For maps fields:
// map<KeyType, ValueType> map_field = 1;
// The parsed descriptor looks like:
// message MapFieldEntry {
// option map_entry = true;
// optional KeyType key = 1;
// optional ValueType value = 2;
// }
// repeated MapFieldEntry map_field = 1;
//
// Implementations may choose not to generate the map_entry=true message, but
// use a native map in the target language to hold the keys and values.
// The reflection APIs in such implementations still need to work as
// if the field is a repeated message field.
//
// NOTE: Do not set the option in .proto files. Always use the maps syntax
// instead. The option should only be implicitly set by the proto compiler
// parser.
optional bool map_entry = 7;
reserved 8; // javalite_serializable
reserved 9; // javanano_as_lite
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
message FieldOptions {
// The ctype option instructs the C++ code generator to use a different
// representation of the field than it normally would. See the specific
// options below. This option is not yet implemented in the open source
// release -- sorry, we'll try to include it in a future version!
optional CType ctype = 1 [default = STRING];
enum CType {
// Default mode.
STRING = 0;
CORD = 1;
STRING_PIECE = 2;
}
// The packed option can be enabled for repeated primitive fields to enable
// a more efficient representation on the wire. Rather than repeatedly
// writing the tag and type for each element, the entire array is encoded as
// a single length-delimited blob. In proto3, only explicit setting it to
// false will avoid using packed encoding.
optional bool packed = 2;
// The jstype option determines the JavaScript type used for values of the
// field. The option is permitted only for 64 bit integral and fixed types
// (int64, uint64, sint64, fixed64, sfixed64). A field with jstype JS_STRING
// is represented as JavaScript string, which avoids loss of precision that
// can happen when a large value is converted to a floating point JavaScript.
// Specifying JS_NUMBER for the jstype causes the generated JavaScript code to
// use the JavaScript "number" type. The behavior of the default option
// JS_NORMAL is implementation dependent.
//
// This option is an enum to permit additional types to be added, e.g.
// goog.math.Integer.
optional JSType jstype = 6 [default = JS_NORMAL];
enum JSType {
// Use the default type.
JS_NORMAL = 0;
// Use JavaScript strings.
JS_STRING = 1;
// Use JavaScript numbers.
JS_NUMBER = 2;
}
// Should this field be parsed lazily? Lazy applies only to message-type
// fields. It means that when the outer message is initially parsed, the
// inner message's contents will not be parsed but instead stored in encoded
// form. The inner message will actually be parsed when it is first accessed.
//
// This is only a hint. Implementations are free to choose whether to use
// eager or lazy parsing regardless of the value of this option. However,
// setting this option true suggests that the protocol author believes that
// using lazy parsing on this field is worth the additional bookkeeping
// overhead typically needed to implement it.
//
// This option does not affect the public interface of any generated code;
// all method signatures remain the same. Furthermore, thread-safety of the
// interface is not affected by this option; const methods remain safe to
// call from multiple threads concurrently, while non-const methods continue
// to require exclusive access.
//
//
// Note that implementations may choose not to check required fields within
// a lazy sub-message. That is, calling IsInitialized() on the outer message
// may return true even if the inner message has missing required fields.
// This is necessary because otherwise the inner message would have to be
// parsed in order to perform the check, defeating the purpose of lazy
// parsing. An implementation which chooses not to check required fields
// must be consistent about it. That is, for any particular sub-message, the
// implementation must either *always* check its required fields, or *never*
// check its required fields, regardless of whether or not the message has
// been parsed.
optional bool lazy = 5 [default = false];
// Is this field deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for accessors, or it will be completely ignored; in the very least, this
// is a formalization for deprecating fields.
optional bool deprecated = 3 [default = false];
// For Google-internal migration only. Do not use.
optional bool weak = 10 [default = false];
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
reserved 4; // removed jtype
}
message OneofOptions {
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
message EnumOptions {
// Set this option to true to allow mapping different tag names to the same
// value.
optional bool allow_alias = 2;
// Is this enum deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for the enum, or it will be completely ignored; in the very least, this
// is a formalization for deprecating enums.
optional bool deprecated = 3 [default = false];
reserved 5; // javanano_as_lite
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
message EnumValueOptions {
// Is this enum value deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for the enum value, or it will be completely ignored; in the very least,
// this is a formalization for deprecating enum values.
optional bool deprecated = 1 [default = false];
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
message ServiceOptions {
// Note: Field numbers 1 through 32 are reserved for Google's internal RPC
// framework. We apologize for hoarding these numbers to ourselves, but
// we were already using them long before we decided to release Protocol
// Buffers.
// Is this service deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for the service, or it will be completely ignored; in the very least,
// this is a formalization for deprecating services.
optional bool deprecated = 33 [default = false];
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
message MethodOptions {
// Note: Field numbers 1 through 32 are reserved for Google's internal RPC
// framework. We apologize for hoarding these numbers to ourselves, but
// we were already using them long before we decided to release Protocol
// Buffers.
// Is this method deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for the method, or it will be completely ignored; in the very least,
// this is a formalization for deprecating methods.
optional bool deprecated = 33 [default = false];
// Is this method side-effect-free (or safe in HTTP parlance), or idempotent,
// or neither? HTTP based RPC implementation may choose GET verb for safe
// methods, and PUT verb for idempotent methods instead of the default POST.
enum IdempotencyLevel {
IDEMPOTENCY_UNKNOWN = 0;
NO_SIDE_EFFECTS = 1; // implies idempotent
IDEMPOTENT = 2; // idempotent, but may have side effects
}
optional IdempotencyLevel idempotency_level = 34
[default = IDEMPOTENCY_UNKNOWN];
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
// A message representing a option the parser does not recognize. This only
// appears in options protos created by the compiler::Parser class.
// DescriptorPool resolves these when building Descriptor objects. Therefore,
// options protos in descriptor objects (e.g. returned by Descriptor::options(),
// or produced by Descriptor::CopyTo()) will never have UninterpretedOptions
// in them.
message UninterpretedOption {
// The name of the uninterpreted option. Each string represents a segment in
// a dot-separated name. is_extension is true iff a segment represents an
// extension (denoted with parentheses in options specs in .proto files).
// E.g.,{ ["foo", false], ["bar.baz", true], ["qux", false] } represents
// "foo.(bar.baz).qux".
message NamePart {
optional string name_part = 1;
optional bool is_extension = 2;
}
repeated NamePart name = 2;
// The value of the uninterpreted option, in whatever type the tokenizer
// identified it as during parsing. Exactly one of these should be set.
optional string identifier_value = 3;
optional uint64 positive_int_value = 4;
optional int64 negative_int_value = 5;
optional double double_value = 6;
optional bytes string_value = 7;
optional string aggregate_value = 8;
}
// ===================================================================
// Optional source code info
// Encapsulates information about the original source file from which a
// FileDescriptorProto was generated.
message SourceCodeInfo {
// A Location identifies a piece of source code in a .proto file which
// corresponds to a particular definition. This information is intended
// to be useful to IDEs, code indexers, documentation generators, and similar
// tools.
//
// For example, say we have a file like:
// message Foo {
// optional string foo = 1;
// }
// Let's look at just the field definition:
// optional string foo = 1;
// ^ ^^ ^^ ^ ^^^
// a bc de f ghi
// We have the following locations:
// span path represents
// [a,i) [ 4, 0, 2, 0 ] The whole field definition.
// [a,b) [ 4, 0, 2, 0, 4 ] The label (optional).
// [c,d) [ 4, 0, 2, 0, 5 ] The type (string).
// [e,f) [ 4, 0, 2, 0, 1 ] The name (foo).
// [g,h) [ 4, 0, 2, 0, 3 ] The number (1).
//
// Notes:
// - A location may refer to a repeated field itself (i.e. not to any
// particular index within it). This is used whenever a set of elements are
// logically enclosed in a single code segment. For example, an entire
// extend block (possibly containing multiple extension definitions) will
// have an outer location whose path refers to the "extensions" repeated
// field without an index.
// - Multiple locations may have the same path. This happens when a single
// logical declaration is spread out across multiple places. The most
// obvious example is the "extend" block again -- there may be multiple
// extend blocks in the same scope, each of which will have the same path.
// - A location's span is not always a subset of its parent's span. For
// example, the "extendee" of an extension declaration appears at the
// beginning of the "extend" block and is shared by all extensions within
// the block.
// - Just because a location's span is a subset of some other location's span
// does not mean that it is a descendant. For example, a "group" defines
// both a type and a field in a single declaration. Thus, the locations
// corresponding to the type and field and their components will overlap.
// - Code which tries to interpret locations should probably be designed to
// ignore those that it doesn't understand, as more types of locations could
// be recorded in the future.
repeated Location location = 1;
message Location {
// Identifies which part of the FileDescriptorProto was defined at this
// location.
//
// Each element is a field number or an index. They form a path from
// the root FileDescriptorProto to the place where the definition. For
// example, this path:
// [ 4, 3, 2, 7, 1 ]
// refers to:
// file.message_type(3) // 4, 3
// .field(7) // 2, 7
// .name() // 1
// This is because FileDescriptorProto.message_type has field number 4:
// repeated DescriptorProto message_type = 4;
// and DescriptorProto.field has field number 2:
// repeated FieldDescriptorProto field = 2;
// and FieldDescriptorProto.name has field number 1:
// optional string name = 1;
//
// Thus, the above path gives the location of a field name. If we removed
// the last element:
// [ 4, 3, 2, 7 ]
// this path refers to the whole field declaration (from the beginning
// of the label to the terminating semicolon).
repeated int32 path = 1 [packed = true];
// Always has exactly three or four elements: start line, start column,
// end line (optional, otherwise assumed same as start line), end column.
// These are packed into a single field for efficiency. Note that line
// and column numbers are zero-based -- typically you will want to add
// 1 to each before displaying to a user.
repeated int32 span = 2 [packed = true];
// If this SourceCodeInfo represents a complete declaration, these are any
// comments appearing before and after the declaration which appear to be
// attached to the declaration.
//
// A series of line comments appearing on consecutive lines, with no other
// tokens appearing on those lines, will be treated as a single comment.
//
// leading_detached_comments will keep paragraphs of comments that appear
// before (but not connected to) the current element. Each paragraph,
// separated by empty lines, will be one comment element in the repeated
// field.
//
// Only the comment content is provided; comment markers (e.g. //) are
// stripped out. For block comments, leading whitespace and an asterisk
// will be stripped from the beginning of each line other than the first.
// Newlines are included in the output.
//
// Examples:
//
// optional int32 foo = 1; // Comment attached to foo.
// // Comment attached to bar.
// optional int32 bar = 2;
//
// optional string baz = 3;
// // Comment attached to baz.
// // Another line attached to baz.
//
// // Comment attached to qux.
// //
// // Another line attached to qux.
// optional double qux = 4;
//
// // Detached comment for corge. This is not leading or trailing comments
// // to qux or corge because there are blank lines separating it from
// // both.
//
// // Detached comment for corge paragraph 2.
//
// optional string corge = 5;
// /* Block comment attached
// * to corge. Leading asterisks
// * will be removed. */
// /* Block comment attached to
// * grault. */
// optional int32 grault = 6;
//
// // ignored detached comments.
optional string leading_comments = 3;
optional string trailing_comments = 4;
repeated string leading_detached_comments = 6;
}
}
// Describes the relationship between generated code and its original source
// file. A GeneratedCodeInfo message is associated with only one generated
// source file, but may contain references to different source .proto files.
message GeneratedCodeInfo {
// An Annotation connects some span of text in generated code to an element
// of its generating .proto file.
repeated Annotation annotation = 1;
message Annotation {
// Identifies the element in the original source .proto file. This field
// is formatted the same as SourceCodeInfo.Location.path.
repeated int32 path = 1 [packed = true];
// Identifies the filesystem path to the original source .proto.
optional string source_file = 2;
// Identifies the starting offset in bytes in the generated code
// that relates to the identified object.
optional int32 begin = 3;
// Identifies the ending offset in bytes in the generated code that
// relates to the identified offset. The end offset should be one past
// the last relevant byte (so the length of the text = end - begin).
optional int32 end = 4;
}
}

@ -0,0 +1,890 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
//
// The messages in this file describe the definitions found in .proto files.
// A valid .proto file can be translated directly to a FileDescriptorProto
// without any other information (e.g. without reading its imports).
syntax = "proto2";
package upb_benchmark.sv;
option go_package = "google.golang.org/protobuf/types/descriptorpb";
option java_package = "com.google.protobuf";
option java_outer_classname = "DescriptorProtos";
option csharp_namespace = "Google.Protobuf.Reflection";
option objc_class_prefix = "GPB";
option cc_enable_arenas = true;
// The protocol compiler can output a FileDescriptorSet containing the .proto
// files it parses.
message FileDescriptorSet {
repeated FileDescriptorProto file = 1;
}
// Describes a complete .proto file.
message FileDescriptorProto {
optional string name = 1
[ctype = STRING_PIECE]; // file name, relative to root of source tree
optional string package = 2
[ctype = STRING_PIECE]; // e.g. "foo", "foo.bar", etc.
// Names of files imported by this file.
repeated string dependency = 3 [ctype = STRING_PIECE];
// Indexes of the public imported files in the dependency list above.
repeated int32 public_dependency = 10;
// Indexes of the weak imported files in the dependency list.
// For Google-internal migration only. Do not use.
repeated int32 weak_dependency = 11;
// All top-level definitions in this file.
repeated DescriptorProto message_type = 4;
repeated EnumDescriptorProto enum_type = 5;
repeated ServiceDescriptorProto service = 6;
repeated FieldDescriptorProto extension = 7;
optional FileOptions options = 8;
// This field contains optional information about the original source code.
// You may safely remove this entire field without harming runtime
// functionality of the descriptors -- the information is needed only by
// development tools.
optional SourceCodeInfo source_code_info = 9;
// The syntax of the proto file.
// The supported values are "proto2" and "proto3".
optional string syntax = 12 [ctype = STRING_PIECE];
}
// Describes a message type.
message DescriptorProto {
optional string name = 1 [ctype = STRING_PIECE];
repeated FieldDescriptorProto field = 2;
repeated FieldDescriptorProto extension = 6;
repeated DescriptorProto nested_type = 3;
repeated EnumDescriptorProto enum_type = 4;
message ExtensionRange {
optional int32 start = 1; // Inclusive.
optional int32 end = 2; // Exclusive.
optional ExtensionRangeOptions options = 3;
}
repeated ExtensionRange extension_range = 5;
repeated OneofDescriptorProto oneof_decl = 8;
optional MessageOptions options = 7;
// Range of reserved tag numbers. Reserved tag numbers may not be used by
// fields or extension ranges in the same message. Reserved ranges may
// not overlap.
message ReservedRange {
optional int32 start = 1; // Inclusive.
optional int32 end = 2; // Exclusive.
}
repeated ReservedRange reserved_range = 9;
// Reserved field names, which may not be used by fields in the same message.
// A given name may only be reserved once.
repeated string reserved_name = 10 [ctype = STRING_PIECE];
}
message ExtensionRangeOptions {
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
// Describes a field within a message.
message FieldDescriptorProto {
enum Type {
// 0 is reserved for errors.
// Order is weird for historical reasons.
TYPE_DOUBLE = 1;
TYPE_FLOAT = 2;
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if
// negative values are likely.
TYPE_INT64 = 3;
TYPE_UINT64 = 4;
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if
// negative values are likely.
TYPE_INT32 = 5;
TYPE_FIXED64 = 6;
TYPE_FIXED32 = 7;
TYPE_BOOL = 8;
TYPE_STRING = 9;
// Tag-delimited aggregate.
// Group type is deprecated and not supported in proto3. However, Proto3
// implementations should still be able to parse the group wire format and
// treat group fields as unknown fields.
TYPE_GROUP = 10;
TYPE_MESSAGE = 11; // Length-delimited aggregate.
// New in version 2.
TYPE_BYTES = 12;
TYPE_UINT32 = 13;
TYPE_ENUM = 14;
TYPE_SFIXED32 = 15;
TYPE_SFIXED64 = 16;
TYPE_SINT32 = 17; // Uses ZigZag encoding.
TYPE_SINT64 = 18; // Uses ZigZag encoding.
}
enum Label {
// 0 is reserved for errors
LABEL_OPTIONAL = 1;
LABEL_REQUIRED = 2;
LABEL_REPEATED = 3;
}
optional string name = 1 [ctype = STRING_PIECE];
optional int32 number = 3;
optional Label label = 4;
// If type_name is set, this need not be set. If both this and type_name
// are set, this must be one of TYPE_ENUM, TYPE_MESSAGE or TYPE_GROUP.
optional Type type = 5;
// For message and enum types, this is the name of the type. If the name
// starts with a '.', it is fully-qualified. Otherwise, C++-like scoping
// rules are used to find the type (i.e. first the nested types within this
// message are searched, then within the parent, on up to the root
// namespace).
optional string type_name = 6 [ctype = STRING_PIECE];
// For extensions, this is the name of the type being extended. It is
// resolved in the same manner as type_name.
optional string extendee = 2 [ctype = STRING_PIECE];
// For numeric types, contains the original text representation of the value.
// For booleans, "true" or "false".
// For strings, contains the default text contents (not escaped in any way).
// For bytes, contains the C escaped value. All bytes >= 128 are escaped.
// TODO(kenton): Base-64 encode?
optional string default_value = 7 [ctype = STRING_PIECE];
// If set, gives the index of a oneof in the containing type's oneof_decl
// list. This field is a member of that oneof.
optional int32 oneof_index = 9;
// JSON name of this field. The value is set by protocol compiler. If the
// user has set a "json_name" option on this field, that option's value
// will be used. Otherwise, it's deduced from the field's name by converting
// it to camelCase.
optional string json_name = 10 [ctype = STRING_PIECE];
optional FieldOptions options = 8;
// If true, this is a proto3 "optional". When a proto3 field is optional, it
// tracks presence regardless of field type.
//
// When proto3_optional is true, this field must be belong to a oneof to
// signal to old proto3 clients that presence is tracked for this field. This
// oneof is known as a "synthetic" oneof, and this field must be its sole
// member (each proto3 optional field gets its own synthetic oneof). Synthetic
// oneofs exist in the descriptor only, and do not generate any API. Synthetic
// oneofs must be ordered after all "real" oneofs.
//
// For message fields, proto3_optional doesn't create any semantic change,
// since non-repeated message fields always track presence. However it still
// indicates the semantic detail of whether the user wrote "optional" or not.
// This can be useful for round-tripping the .proto file. For consistency we
// give message fields a synthetic oneof also, even though it is not required
// to track presence. This is especially important because the parser can't
// tell if a field is a message or an enum, so it must always create a
// synthetic oneof.
//
// Proto2 optional fields do not set this flag, because they already indicate
// optional with `LABEL_OPTIONAL`.
optional bool proto3_optional = 17;
}
// Describes a oneof.
message OneofDescriptorProto {
optional string name = 1 [ctype = STRING_PIECE];
optional OneofOptions options = 2;
}
// Describes an enum type.
message EnumDescriptorProto {
optional string name = 1 [ctype = STRING_PIECE];
repeated EnumValueDescriptorProto value = 2;
optional EnumOptions options = 3;
// Range of reserved numeric values. Reserved values may not be used by
// entries in the same enum. Reserved ranges may not overlap.
//
// Note that this is distinct from DescriptorProto.ReservedRange in that it
// is inclusive such that it can appropriately represent the entire int32
// domain.
message EnumReservedRange {
optional int32 start = 1; // Inclusive.
optional int32 end = 2; // Inclusive.
}
// Range of reserved numeric values. Reserved numeric values may not be used
// by enum values in the same enum declaration. Reserved ranges may not
// overlap.
repeated EnumReservedRange reserved_range = 4;
// Reserved enum value names, which may not be reused. A given name may only
// be reserved once.
repeated string reserved_name = 5 [ctype = STRING_PIECE];
}
// Describes a value within an enum.
message EnumValueDescriptorProto {
optional string name = 1 [ctype = STRING_PIECE];
optional int32 number = 2;
optional EnumValueOptions options = 3;
}
// Describes a service.
message ServiceDescriptorProto {
optional string name = 1 [ctype = STRING_PIECE];
repeated MethodDescriptorProto method = 2;
optional ServiceOptions options = 3;
}
// Describes a method of a service.
message MethodDescriptorProto {
optional string name = 1 [ctype = STRING_PIECE];
// Input and output type names. These are resolved in the same way as
// FieldDescriptorProto.type_name, but must refer to a message type.
optional string input_type = 2 [ctype = STRING_PIECE];
optional string output_type = 3 [ctype = STRING_PIECE];
optional MethodOptions options = 4;
// Identifies if client streams multiple client messages
optional bool client_streaming = 5 [default = false];
// Identifies if server streams multiple server messages
optional bool server_streaming = 6 [default = false];
}
// ===================================================================
// Options
// Each of the definitions above may have "options" attached. These are
// just annotations which may cause code to be generated slightly differently
// or may contain hints for code that manipulates protocol messages.
//
// Clients may define custom options as extensions of the *Options messages.
// These extensions may not yet be known at parsing time, so the parser cannot
// store the values in them. Instead it stores them in a field in the *Options
// message called uninterpreted_option. This field must have the same name
// across all *Options messages. We then use this field to populate the
// extensions when we build a descriptor, at which point all protos have been
// parsed and so all extensions are known.
//
// Extension numbers for custom options may be chosen as follows:
// * For options which will only be used within a single application or
// organization, or for experimental options, use field numbers 50000
// through 99999. It is up to you to ensure that you do not use the
// same number for multiple options.
// * For options which will be published and used publicly by multiple
// independent entities, e-mail protobuf-global-extension-registry@google.com
// to reserve extension numbers. Simply provide your project name (e.g.
// Objective-C plugin) and your project website (if available) -- there's no
// need to explain how you intend to use them. Usually you only need one
// extension number. You can declare multiple options with only one extension
// number by putting them in a sub-message. See the Custom Options section of
// the docs for examples:
// https://developers.google.com/protocol-buffers/docs/proto#options
// If this turns out to be popular, a web service will be set up
// to automatically assign option numbers.
message FileOptions {
// Sets the Java package where classes generated from this .proto will be
// placed. By default, the proto package is used, but this is often
// inappropriate because proto packages do not normally start with backwards
// domain names.
optional string java_package = 1 [ctype = STRING_PIECE];
// If set, all the classes from the .proto file are wrapped in a single
// outer class with the given name. This applies to both Proto1
// (equivalent to the old "--one_java_file" option) and Proto2 (where
// a .proto always translates to a single class, but you may want to
// explicitly choose the class name).
optional string java_outer_classname = 8 [ctype = STRING_PIECE];
// If set true, then the Java code generator will generate a separate .java
// file for each top-level message, enum, and service defined in the .proto
// file. Thus, these types will *not* be nested inside the outer class
// named by java_outer_classname. However, the outer class will still be
// generated to contain the file's getDescriptor() method as well as any
// top-level extensions defined in the file.
optional bool java_multiple_files = 10 [default = false];
// This option does nothing.
optional bool java_generate_equals_and_hash = 20 [deprecated = true];
// If set true, then the Java2 code generator will generate code that
// throws an exception whenever an attempt is made to assign a non-UTF-8
// byte sequence to a string field.
// Message reflection will do the same.
// However, an extension field still accepts non-UTF-8 byte sequences.
// This option has no effect on when used with the lite runtime.
optional bool java_string_check_utf8 = 27 [default = false];
// Generated classes can be optimized for speed or code size.
enum OptimizeMode {
SPEED = 1; // Generate complete code for parsing, serialization,
// etc.
CODE_SIZE = 2; // Use ReflectionOps to implement these methods.
LITE_RUNTIME = 3; // Generate code using MessageLite and the lite runtime.
}
optional OptimizeMode optimize_for = 9 [default = SPEED];
// Sets the Go package where structs generated from this .proto will be
// placed. If omitted, the Go package will be derived from the following:
// - The basename of the package import path, if provided.
// - Otherwise, the package statement in the .proto file, if present.
// - Otherwise, the basename of the .proto file, without extension.
optional string go_package = 11 [ctype = STRING_PIECE];
// Should generic services be generated in each language? "Generic" services
// are not specific to any particular RPC system. They are generated by the
// main code generators in each language (without additional plugins).
// Generic services were the only kind of service generation supported by
// early versions of google.protobuf.
//
// Generic services are now considered deprecated in favor of using plugins
// that generate code specific to your particular RPC system. Therefore,
// these default to false. Old code which depends on generic services should
// explicitly set them to true.
optional bool cc_generic_services = 16 [default = false];
optional bool java_generic_services = 17 [default = false];
optional bool py_generic_services = 18 [default = false];
optional bool php_generic_services = 42 [default = false];
// Is this file deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for everything in the file, or it will be completely ignored; in the very
// least, this is a formalization for deprecating files.
optional bool deprecated = 23 [default = false];
// Enables the use of arenas for the proto messages in this file. This applies
// only to generated classes for C++.
optional bool cc_enable_arenas = 31 [default = true];
// Sets the objective c class prefix which is prepended to all objective c
// generated classes from this .proto. There is no default.
optional string objc_class_prefix = 36 [ctype = STRING_PIECE];
// Namespace for generated classes; defaults to the package.
optional string csharp_namespace = 37 [ctype = STRING_PIECE];
// By default Swift generators will take the proto package and CamelCase it
// replacing '.' with underscore and use that to prefix the types/symbols
// defined. When this options is provided, they will use this value instead
// to prefix the types/symbols defined.
optional string swift_prefix = 39 [ctype = STRING_PIECE];
// Sets the php class prefix which is prepended to all php generated classes
// from this .proto. Default is empty.
optional string php_class_prefix = 40 [ctype = STRING_PIECE];
// Use this option to change the namespace of php generated classes. Default
// is empty. When this option is empty, the package name will be used for
// determining the namespace.
optional string php_namespace = 41 [ctype = STRING_PIECE];
// Use this option to change the namespace of php generated metadata classes.
// Default is empty. When this option is empty, the proto file name will be
// used for determining the namespace.
optional string php_metadata_namespace = 44 [ctype = STRING_PIECE];
// Use this option to change the package of ruby generated classes. Default
// is empty. When this option is not set, the package name will be used for
// determining the ruby package.
optional string ruby_package = 45 [ctype = STRING_PIECE];
// The parser stores options it doesn't recognize here.
// See the documentation for the "Options" section above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message.
// See the documentation for the "Options" section above.
extensions 1000 to max;
reserved 38;
}
message MessageOptions {
// Set true to use the old proto1 MessageSet wire format for extensions.
// This is provided for backwards-compatibility with the MessageSet wire
// format. You should not use this for any other reason: It's less
// efficient, has fewer features, and is more complicated.
//
// The message must be defined exactly as follows:
// message Foo {
// option message_set_wire_format = true;
// extensions 4 to max;
// }
// Note that the message cannot have any defined fields; MessageSets only
// have extensions.
//
// All extensions of your type must be singular messages; e.g. they cannot
// be int32s, enums, or repeated messages.
//
// Because this is an option, the above two restrictions are not enforced by
// the protocol compiler.
optional bool message_set_wire_format = 1 [default = false];
// Disables the generation of the standard "descriptor()" accessor, which can
// conflict with a field of the same name. This is meant to make migration
// from proto1 easier; new code should avoid fields named "descriptor".
optional bool no_standard_descriptor_accessor = 2 [default = false];
// Is this message deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for the message, or it will be completely ignored; in the very least,
// this is a formalization for deprecating messages.
optional bool deprecated = 3 [default = false];
// Whether the message is an automatically generated map entry type for the
// maps field.
//
// For maps fields:
// map<KeyType, ValueType> map_field = 1;
// The parsed descriptor looks like:
// message MapFieldEntry {
// option map_entry = true;
// optional KeyType key = 1;
// optional ValueType value = 2;
// }
// repeated MapFieldEntry map_field = 1;
//
// Implementations may choose not to generate the map_entry=true message, but
// use a native map in the target language to hold the keys and values.
// The reflection APIs in such implementations still need to work as
// if the field is a repeated message field.
//
// NOTE: Do not set the option in .proto files. Always use the maps syntax
// instead. The option should only be implicitly set by the proto compiler
// parser.
optional bool map_entry = 7;
reserved 8; // javalite_serializable
reserved 9; // javanano_as_lite
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
message FieldOptions {
// The ctype option instructs the C++ code generator to use a different
// representation of the field than it normally would. See the specific
// options below. This option is not yet implemented in the open source
// release -- sorry, we'll try to include it in a future version!
optional CType ctype = 1 [default = STRING];
enum CType {
// Default mode.
STRING = 0;
CORD = 1;
STRING_PIECE = 2;
}
// The packed option can be enabled for repeated primitive fields to enable
// a more efficient representation on the wire. Rather than repeatedly
// writing the tag and type for each element, the entire array is encoded as
// a single length-delimited blob. In proto3, only explicit setting it to
// false will avoid using packed encoding.
optional bool packed = 2;
// The jstype option determines the JavaScript type used for values of the
// field. The option is permitted only for 64 bit integral and fixed types
// (int64, uint64, sint64, fixed64, sfixed64). A field with jstype JS_STRING
// is represented as JavaScript string, which avoids loss of precision that
// can happen when a large value is converted to a floating point JavaScript.
// Specifying JS_NUMBER for the jstype causes the generated JavaScript code to
// use the JavaScript "number" type. The behavior of the default option
// JS_NORMAL is implementation dependent.
//
// This option is an enum to permit additional types to be added, e.g.
// goog.math.Integer.
optional JSType jstype = 6 [default = JS_NORMAL];
enum JSType {
// Use the default type.
JS_NORMAL = 0;
// Use JavaScript strings.
JS_STRING = 1;
// Use JavaScript numbers.
JS_NUMBER = 2;
}
// Should this field be parsed lazily? Lazy applies only to message-type
// fields. It means that when the outer message is initially parsed, the
// inner message's contents will not be parsed but instead stored in encoded
// form. The inner message will actually be parsed when it is first accessed.
//
// This is only a hint. Implementations are free to choose whether to use
// eager or lazy parsing regardless of the value of this option. However,
// setting this option true suggests that the protocol author believes that
// using lazy parsing on this field is worth the additional bookkeeping
// overhead typically needed to implement it.
//
// This option does not affect the public interface of any generated code;
// all method signatures remain the same. Furthermore, thread-safety of the
// interface is not affected by this option; const methods remain safe to
// call from multiple threads concurrently, while non-const methods continue
// to require exclusive access.
//
//
// Note that implementations may choose not to check required fields within
// a lazy sub-message. That is, calling IsInitialized() on the outer message
// may return true even if the inner message has missing required fields.
// This is necessary because otherwise the inner message would have to be
// parsed in order to perform the check, defeating the purpose of lazy
// parsing. An implementation which chooses not to check required fields
// must be consistent about it. That is, for any particular sub-message, the
// implementation must either *always* check its required fields, or *never*
// check its required fields, regardless of whether or not the message has
// been parsed.
optional bool lazy = 5 [default = false];
// Is this field deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for accessors, or it will be completely ignored; in the very least, this
// is a formalization for deprecating fields.
optional bool deprecated = 3 [default = false];
// For Google-internal migration only. Do not use.
optional bool weak = 10 [default = false];
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
reserved 4; // removed jtype
}
message OneofOptions {
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
message EnumOptions {
// Set this option to true to allow mapping different tag names to the same
// value.
optional bool allow_alias = 2;
// Is this enum deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for the enum, or it will be completely ignored; in the very least, this
// is a formalization for deprecating enums.
optional bool deprecated = 3 [default = false];
reserved 5; // javanano_as_lite
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
message EnumValueOptions {
// Is this enum value deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for the enum value, or it will be completely ignored; in the very least,
// this is a formalization for deprecating enum values.
optional bool deprecated = 1 [default = false];
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
message ServiceOptions {
// Note: Field numbers 1 through 32 are reserved for Google's internal RPC
// framework. We apologize for hoarding these numbers to ourselves, but
// we were already using them long before we decided to release Protocol
// Buffers.
// Is this service deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for the service, or it will be completely ignored; in the very least,
// this is a formalization for deprecating services.
optional bool deprecated = 33 [default = false];
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
message MethodOptions {
// Note: Field numbers 1 through 32 are reserved for Google's internal RPC
// framework. We apologize for hoarding these numbers to ourselves, but
// we were already using them long before we decided to release Protocol
// Buffers.
// Is this method deprecated?
// Depending on the target platform, this can emit Deprecated annotations
// for the method, or it will be completely ignored; in the very least,
// this is a formalization for deprecating methods.
optional bool deprecated = 33 [default = false];
// Is this method side-effect-free (or safe in HTTP parlance), or idempotent,
// or neither? HTTP based RPC implementation may choose GET verb for safe
// methods, and PUT verb for idempotent methods instead of the default POST.
enum IdempotencyLevel {
IDEMPOTENCY_UNKNOWN = 0;
NO_SIDE_EFFECTS = 1; // implies idempotent
IDEMPOTENT = 2; // idempotent, but may have side effects
}
optional IdempotencyLevel idempotency_level = 34
[default = IDEMPOTENCY_UNKNOWN];
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
// Clients can define custom options in extensions of this message. See above.
extensions 1000 to max;
}
// A message representing a option the parser does not recognize. This only
// appears in options protos created by the compiler::Parser class.
// DescriptorPool resolves these when building Descriptor objects. Therefore,
// options protos in descriptor objects (e.g. returned by Descriptor::options(),
// or produced by Descriptor::CopyTo()) will never have UninterpretedOptions
// in them.
message UninterpretedOption {
// The name of the uninterpreted option. Each string represents a segment in
// a dot-separated name. is_extension is true iff a segment represents an
// extension (denoted with parentheses in options specs in .proto files).
// E.g.,{ ["foo", false], ["bar.baz", true], ["qux", false] } represents
// "foo.(bar.baz).qux".
message NamePart {
optional string name_part = 1 [ctype = STRING_PIECE];
optional bool is_extension = 2;
}
repeated NamePart name = 2;
// The value of the uninterpreted option, in whatever type the tokenizer
// identified it as during parsing. Exactly one of these should be set.
optional string identifier_value = 3 [ctype = STRING_PIECE];
optional uint64 positive_int_value = 4;
optional int64 negative_int_value = 5;
optional double double_value = 6;
optional bytes string_value = 7;
optional string aggregate_value = 8 [ctype = STRING_PIECE];
}
// ===================================================================
// Optional source code info
// Encapsulates information about the original source file from which a
// FileDescriptorProto was generated.
message SourceCodeInfo {
// A Location identifies a piece of source code in a .proto file which
// corresponds to a particular definition. This information is intended
// to be useful to IDEs, code indexers, documentation generators, and similar
// tools.
//
// For example, say we have a file like:
// message Foo {
// optional string foo = 1 [ctype = STRING_PIECE];
// }
// Let's look at just the field definition:
// optional string foo = 1 [ctype = STRING_PIECE];
// ^ ^^ ^^ ^ ^^^
// a bc de f ghi
// We have the following locations:
// span path represents
// [a,i) [ 4, 0, 2, 0 ] The whole field definition.
// [a,b) [ 4, 0, 2, 0, 4 ] The label (optional).
// [c,d) [ 4, 0, 2, 0, 5 ] The type (string).
// [e,f) [ 4, 0, 2, 0, 1 ] The name (foo).
// [g,h) [ 4, 0, 2, 0, 3 ] The number (1).
//
// Notes:
// - A location may refer to a repeated field itself (i.e. not to any
// particular index within it). This is used whenever a set of elements are
// logically enclosed in a single code segment. For example, an entire
// extend block (possibly containing multiple extension definitions) will
// have an outer location whose path refers to the "extensions" repeated
// field without an index.
// - Multiple locations may have the same path. This happens when a single
// logical declaration is spread out across multiple places. The most
// obvious example is the "extend" block again -- there may be multiple
// extend blocks in the same scope, each of which will have the same path.
// - A location's span is not always a subset of its parent's span. For
// example, the "extendee" of an extension declaration appears at the
// beginning of the "extend" block and is shared by all extensions within
// the block.
// - Just because a location's span is a subset of some other location's span
// does not mean that it is a descendant. For example, a "group" defines
// both a type and a field in a single declaration. Thus, the locations
// corresponding to the type and field and their components will overlap.
// - Code which tries to interpret locations should probably be designed to
// ignore those that it doesn't understand, as more types of locations could
// be recorded in the future.
repeated Location location = 1;
message Location {
// Identifies which part of the FileDescriptorProto was defined at this
// location.
//
// Each element is a field number or an index. They form a path from
// the root FileDescriptorProto to the place where the definition. For
// example, this path:
// [ 4, 3, 2, 7, 1 ]
// refers to:
// file.message_type(3) // 4, 3
// .field(7) // 2, 7
// .name() // 1
// This is because FileDescriptorProto.message_type has field number 4:
// repeated DescriptorProto message_type = 4;
// and DescriptorProto.field has field number 2:
// repeated FieldDescriptorProto field = 2;
// and FieldDescriptorProto.name has field number 1:
// optional string name = 1 [ctype = STRING_PIECE];
//
// Thus, the above path gives the location of a field name. If we removed
// the last element:
// [ 4, 3, 2, 7 ]
// this path refers to the whole field declaration (from the beginning
// of the label to the terminating semicolon).
repeated int32 path = 1 [packed = true];
// Always has exactly three or four elements: start line, start column,
// end line (optional, otherwise assumed same as start line), end column.
// These are packed into a single field for efficiency. Note that line
// and column numbers are zero-based -- typically you will want to add
// 1 to each before displaying to a user.
repeated int32 span = 2 [packed = true];
// If this SourceCodeInfo represents a complete declaration, these are any
// comments appearing before and after the declaration which appear to be
// attached to the declaration.
//
// A series of line comments appearing on consecutive lines, with no other
// tokens appearing on those lines, will be treated as a single comment.
//
// leading_detached_comments will keep paragraphs of comments that appear
// before (but not connected to) the current element. Each paragraph,
// separated by empty lines, will be one comment element in the repeated
// field.
//
// Only the comment content is provided; comment markers (e.g. //) are
// stripped out. For block comments, leading whitespace and an asterisk
// will be stripped from the beginning of each line other than the first.
// Newlines are included in the output.
//
// Examples:
//
// optional int32 foo = 1; // Comment attached to foo.
// // Comment attached to bar.
// optional int32 bar = 2;
//
// optional string baz = 3 [ctype = STRING_PIECE];
// // Comment attached to baz.
// // Another line attached to baz.
//
// // Comment attached to qux.
// //
// // Another line attached to qux.
// optional double qux = 4;
//
// // Detached comment for corge. This is not leading or trailing comments
// // to qux or corge because there are blank lines separating it from
// // both.
//
// // Detached comment for corge paragraph 2.
//
// optional string corge = 5 [ctype = STRING_PIECE];
// /* Block comment attached
// * to corge. Leading asterisks
// * will be removed. */
// /* Block comment attached to
// * grault. */
// optional int32 grault = 6;
//
// // ignored detached comments.
optional string leading_comments = 3 [ctype = STRING_PIECE];
optional string trailing_comments = 4 [ctype = STRING_PIECE];
repeated string leading_detached_comments = 6 [ctype = STRING_PIECE];
}
}
// Describes the relationship between generated code and its original source
// file. A GeneratedCodeInfo message is associated with only one generated
// source file, but may contain references to different source .proto files.
message GeneratedCodeInfo {
// An Annotation connects some span of text in generated code to an element
// of its generating .proto file.
repeated Annotation annotation = 1;
message Annotation {
// Identifies the element in the original source .proto file. This field
// is formatted the same as SourceCodeInfo.Location.path.
repeated int32 path = 1 [packed = true];
// Identifies the filesystem path to the original source .proto.
optional string source_file = 2 [ctype = STRING_PIECE];
// Identifies the starting offset in bytes in the generated code
// that relates to the identified object.
optional int32 begin = 3;
// Identifies the ending offset in bytes in the generated code that
// relates to the identified offset. The end offset should be one past
// the last relevant byte (so the length of the text = end - begin).
optional int32 end = 4;
}
}

@ -0,0 +1,6 @@
syntax = "proto3";
package upb_benchmark;
message Empty {}

@ -0,0 +1,38 @@
import sys
import re
include = sys.argv[1]
msg_basename = sys.argv[2]
count = 1
m = re.search(r'(.*\D)(\d+)$', sys.argv[2])
if m:
msg_basename = m.group(1)
count = int(m.group(2))
print('''
#include "{include}"
char buf[1];
int main() {{
'''.format(include=include))
def RefMessage(name):
print('''
{{
{name} proto;
proto.ParseFromArray(buf, 0);
proto.SerializePartialToArray(&buf[0], 0);
}}
'''.format(name=name))
RefMessage(msg_basename)
for i in range(2, count + 1):
RefMessage(msg_basename + str(i))
print('''
return 0;
}''')

@ -0,0 +1,92 @@
import sys
import random
base = sys.argv[1]
field_freqs = [
(('bool', 'optional'), 8.321),
(('bool', 'repeated'), 0.033),
(('bytes', 'optional'), 0.809),
(('bytes', 'repeated'), 0.065),
(('double', 'optional'), 2.845),
(('double', 'repeated'), 0.143),
(('fixed32', 'optional'), 0.084),
(('fixed32', 'repeated'), 0.012),
(('fixed64', 'optional'), 0.204),
(('fixed64', 'repeated'), 0.027),
(('float', 'optional'), 2.355),
(('float', 'repeated'), 0.132),
(('int32', 'optional'), 6.717),
(('int32', 'repeated'), 0.366),
(('int64', 'optional'), 9.678),
(('int64', 'repeated'), 0.425),
(('sfixed32', 'optional'), 0.018),
(('sfixed32', 'repeated'), 0.005),
(('sfixed64', 'optional'), 0.022),
(('sfixed64', 'repeated'), 0.005),
(('sint32', 'optional'), 0.026),
(('sint32', 'repeated'), 0.009),
(('sint64', 'optional'), 0.018),
(('sint64', 'repeated'), 0.006),
(('string', 'optional'), 25.461),
(('string', 'repeated'), 2.606),
(('Enum', 'optional'), 6.16),
(('Enum', 'repeated'), 0.576),
(('Message', 'optional'), 22.472),
(('Message', 'repeated'), 7.766),
(('uint32', 'optional'), 1.289),
(('uint32', 'repeated'), 0.051),
(('uint64', 'optional'), 1.044),
(('uint64', 'repeated'), 0.079),
]
population = [item[0] for item in field_freqs]
weights = [item[1] for item in field_freqs]
def choices(k):
if sys.version_info >= (3, 6):
return random.choices(population=population, weights=weights, k=k)
else:
print("WARNING: old Python version, field types are not properly weighted!")
return [random.choice(population) for _ in range(k)]
with open(base + "/100_msgs.proto", "w") as f:
f.write('syntax = "proto3";\n')
f.write('package upb_benchmark;\n')
f.write('message Message {}\n')
for i in range(2, 101):
f.write('message Message{i} {{}}\n'.format(i=i))
with open(base + "/200_msgs.proto", "w") as f:
f.write('syntax = "proto3";\n')
f.write('package upb_benchmark;\n')
f.write('message Message {}\n')
for i in range(2, 501):
f.write('message Message{i} {{}}\n'.format(i=i))
with open(base + "/100_fields.proto", "w") as f:
f.write('syntax = "proto2";\n')
f.write('package upb_benchmark;\n')
f.write('enum Enum { ZERO = 0; }\n')
f.write('message Message {\n')
i = 1
random.seed(a=0, version=2)
for field in choices(100):
field_type, label = field
f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label, field_type=field_type))
i += 1
f.write('}\n')
with open(base + "/200_fields.proto", "w") as f:
f.write('syntax = "proto2";\n')
f.write('package upb_benchmark;\n')
f.write('enum Enum { ZERO = 0; }\n')
f.write('message Message {\n')
i = 1
random.seed(a=0, version=2)
for field in choices(200):
field_type, label = field
f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label,field_type=field_type))
i += 1
f.write('}\n')

@ -0,0 +1,39 @@
import sys
import re
include = sys.argv[1]
msg_basename = sys.argv[2]
count = 1
m = re.search(r'(.*\D)(\d+)$', sys.argv[2])
if m:
msg_basename = m.group(1)
count = int(m.group(2))
print('''
#include "{include}"
char buf[1];
int main() {{
upb_arena *arena = upb_arena_new();
size_t size;
'''.format(include=include))
def RefMessage(name):
print('''
{{
{name} *proto = {name}_parse(buf, 1, arena);
{name}_serialize(proto, arena, &size);
}}
'''.format(name=name))
RefMessage(msg_basename)
for i in range(2, count + 1):
RefMessage(msg_basename + str(i))
print('''
return 0;
}''')

@ -0,0 +1,91 @@
load(
":build_defs.bzl",
"generated_file_staleness_test",
)
load(
"//bazel:build_defs.bzl",
"make_shell_script",
)
licenses(["notice"])
exports_files(["staleness_test.py"])
py_library(
name = "staleness_test_lib",
testonly = 1,
srcs = ["staleness_test_lib.py"],
)
py_binary(
name = "make_cmakelists",
srcs = ["make_cmakelists.py"],
)
genrule(
name = "gen_cmakelists",
srcs = [
"//:BUILD",
"//:WORKSPACE",
"//:cmake_files",
"//third_party/wyhash:cmake_files",
":cmake_files",
],
outs = ["generated-in/CMakeLists.txt"],
cmd = "$(location :make_cmakelists) $@",
tools = [":make_cmakelists"],
)
genrule(
name = "copy_json_ragel",
srcs = ["//:upb/json/parser.c"],
outs = ["generated-in/upb/json/parser.c"],
cmd = "cp $< $@",
)
genrule(
name = "copy_protos",
srcs = ["//:descriptor_upb_proto"],
outs = [
"generated-in/google/protobuf/descriptor.upb.c",
"generated-in/google/protobuf/descriptor.upb.h",
],
cmd = "cp $(SRCS) $(@D)/generated-in/google/protobuf",
)
generated_file_staleness_test(
name = "test_generated_files",
outs = [
"CMakeLists.txt",
"google/protobuf/descriptor.upb.c",
"google/protobuf/descriptor.upb.h",
"upb/json/parser.c",
],
generated_pattern = "generated-in/%s",
)
# Test the CMake build #########################################################
filegroup(
name = "cmake_files",
srcs = glob([
"**/*",
]),
)
make_shell_script(
name = "gen_run_cmake_build",
out = "run_cmake_build.sh",
contents = "find . && mkdir build && cd build && cmake ../cmake && make -j8 && make test",
)
sh_test(
name = "cmake_build",
srcs = ["run_cmake_build.sh"],
data = [
":cmake_files",
"//:cmake_files",
"//third_party/wyhash:cmake_files",
],
deps = ["@bazel_tools//tools/bash/runfiles"],
)

@ -12,6 +12,7 @@ cmake_minimum_required (VERSION 3.0)
cmake_policy(SET CMP0048 NEW)
project(upb)
set(CMAKE_C_STANDARD 99)
# Prevent CMake from setting -rdynamic on Linux (!!).
@ -48,8 +49,8 @@ if(UPB_ENABLE_UBSAN)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
endif()
include_directories(.)
include_directories(generated_for_cmake)
include_directories(..)
include_directories(../cmake)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
if(APPLE)
@ -60,79 +61,90 @@ endif()
enable_testing()
add_library(port
upb/port.c)
add_library(port INTERFACE)
add_library(upb
upb/decode.c
upb/encode.c
upb/msg.c
upb/msg.h
upb/table.c
upb/table.int.h
upb/upb.c
upb/decode.h
upb/encode.h
upb/upb.h
upb/upb.hpp)
../upb/decode.c
../upb/decode.int.h
../upb/encode.c
../upb/msg.c
../upb/msg.h
../upb/table.c
../upb/table.int.h
../upb/upb.c
../upb/upb.int.h
../upb/decode.h
../upb/encode.h
../upb/upb.h
../upb/upb.hpp)
target_link_libraries(upb
port)
fastdecode
port
/third_party/wyhash)
add_library(fastdecode
../upb/decode.int.h
../upb/decode_fast.c
../upb/decode_fast.h
../upb/msg.h
../upb/upb.int.h)
target_link_libraries(fastdecode
port
table)
add_library(generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me INTERFACE)
target_link_libraries(generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me INTERFACE
table
upb)
add_library(reflection
upb/def.c
upb/msg.h
upb/reflection.c
upb/def.h
upb/def.hpp
upb/reflection.h)
../upb/def.c
../upb/msg.h
../upb/reflection.c
../upb/def.h
../upb/def.hpp
../upb/reflection.h)
target_link_libraries(reflection
descriptor_upb_proto
port
table
upb)
add_library(textformat
upb/text_encode.c
upb/text_encode.h)
../upb/text_encode.c
../upb/text_encode.h)
target_link_libraries(textformat
port
reflection)
add_library(json
upb/json_decode.c
upb/json_encode.c
upb/json_decode.h
upb/json_encode.h)
../upb/json_decode.c
../upb/json_encode.c
../upb/json_decode.h
../upb/json_encode.h)
target_link_libraries(json
port
reflection
upb)
add_library(table INTERFACE)
target_link_libraries(table INTERFACE
port
upb)
port)
add_library(handlers
upb/handlers.c
upb/handlers-inl.h
upb/sink.c
upb/handlers.h
upb/sink.h)
../upb/handlers.c
../upb/handlers-inl.h
../upb/sink.c
../upb/handlers.h
../upb/sink.h)
target_link_libraries(handlers
port
reflection
table
upb)
add_library(upb_pb
upb/pb/compile_decoder.c
upb/pb/decoder.c
upb/pb/decoder.int.h
upb/pb/encoder.c
upb/pb/textprinter.c
upb/pb/varint.c
upb/pb/varint.int.h
upb/pb/decoder.h
upb/pb/encoder.h
upb/pb/textprinter.h)
../upb/pb/compile_decoder.c
../upb/pb/decoder.c
../upb/pb/decoder.int.h
../upb/pb/encoder.c
../upb/pb/textprinter.c
../upb/pb/varint.c
../upb/pb/varint.int.h
../upb/pb/decoder.h
../upb/pb/encoder.h
../upb/pb/textprinter.h)
target_link_libraries(upb_pb
descriptor_upb_proto
handlers
@ -141,26 +153,13 @@ target_link_libraries(upb_pb
table
upb)
add_library(upb_json
generated_for_cmake/upb/json/parser.c
upb/json/printer.c
upb/json/parser.h
upb/json/printer.h)
../cmake/upb/json/parser.c
../upb/json/printer.c
../upb/json/parser.h
../upb/json/printer.h)
target_link_libraries(upb_json
upb
upb_pb)
add_library(upb_cc_bindings INTERFACE)
target_link_libraries(upb_cc_bindings INTERFACE
descriptor_upb_proto
handlers
port
upb)
add_library(upb_test
tests/testmain.cc
tests/test_util.h
tests/upb_test.h)
target_link_libraries(upb_test
handlers
port
upb)
add_library(wyhash INTERFACE)

@ -0,0 +1,23 @@
# upb CMake build (EXPERIMENTAL)
upb's CMake support is experimental. The core library builds successfully
under CMake, and this is verified by the Bazel tests in this directory.
However there is no support for building the upb compiler or for generating
.upb.c/upb.h files. This means upb's CMake support is incomplete at best,
unless your application is intended to be purely reflective.
If you find this CMake setup useful in its current state, please consider
filing an issue so we know. If you have suggestions for how it could be
more useful (and particularly if you can contribute some code for it)
please feel free to file an issue for that too. Do keep in mind that upb
does not currently provide any ABI stability, so we want to avoid providing
a shared library.
The CMakeLists.txt is generated from the Bazel BUILD files using the Python
scripts in this directory. We want to avoid having two separate sources of
truth that both need to be updated when a file is added or removed.
This directory also contains some generated files that would be created
on the fly during a Bazel build. These are automaticaly kept in sync by
the Bazel test `//cmake:test_generated_files`.

@ -0,0 +1,43 @@
def generated_file_staleness_test(name, outs, generated_pattern):
"""Tests that checked-in file(s) match the contents of generated file(s).
The resulting test will verify that all output files exist and have the
correct contents. If the test fails, it can be invoked with --fix to
bring the checked-in files up to date.
Args:
name: Name of the rule.
outs: the checked-in files that are copied from generated files.
generated_pattern: the pattern for transforming each "out" file into a
generated file. For example, if generated_pattern="generated/%s" then
a file foo.txt will look for generated file generated/foo.txt.
"""
script_name = name + ".py"
script_src = ":staleness_test.py"
# Filter out non-existing rules so Blaze doesn't error out before we even
# run the test.
existing_outs = native.glob(include = outs)
# The file list contains a few extra bits of information at the end.
# These get unpacked by the Config class in staleness_test_lib.py.
file_list = outs + [generated_pattern, native.package_name() or ".", name]
native.genrule(
name = name + "_makescript",
outs = [script_name],
srcs = [script_src],
testonly = 1,
cmd = "cat $(location " + script_src + ") > $@; " +
"sed -i.bak -e 's|INSERT_FILE_LIST_HERE|" + "\\\n ".join(file_list) + "|' $@",
)
native.py_test(
name = name,
srcs = [script_name],
data = existing_outs + [generated_pattern % file for file in outs],
deps = [
":staleness_test_lib",
],
)

@ -23,7 +23,7 @@ static const upb_msglayout_field google_protobuf_FileDescriptorSet__fields[1] =
const upb_msglayout google_protobuf_FileDescriptorSet_msginit = {
&google_protobuf_FileDescriptorSet_submsgs[0],
&google_protobuf_FileDescriptorSet__fields[0],
UPB_SIZE(4, 8), 1, false,
UPB_SIZE(8, 8), 1, false, 255,
};
static const upb_msglayout *const google_protobuf_FileDescriptorProto_submsgs[6] = {
@ -43,20 +43,20 @@ static const upb_msglayout_field google_protobuf_FileDescriptorProto__fields[12]
{5, UPB_SIZE(44, 88), 0, 1, 11, 3},
{6, UPB_SIZE(48, 96), 0, 4, 11, 3},
{7, UPB_SIZE(52, 104), 0, 2, 11, 3},
{8, UPB_SIZE(28, 56), 4, 3, 11, 1},
{9, UPB_SIZE(32, 64), 5, 5, 11, 1},
{8, UPB_SIZE(28, 56), 3, 3, 11, 1},
{9, UPB_SIZE(32, 64), 4, 5, 11, 1},
{10, UPB_SIZE(56, 112), 0, 0, 5, 3},
{11, UPB_SIZE(60, 120), 0, 0, 5, 3},
{12, UPB_SIZE(20, 40), 3, 0, 12, 1},
{12, UPB_SIZE(20, 40), 5, 0, 12, 1},
};
const upb_msglayout google_protobuf_FileDescriptorProto_msginit = {
&google_protobuf_FileDescriptorProto_submsgs[0],
&google_protobuf_FileDescriptorProto__fields[0],
UPB_SIZE(64, 128), 12, false,
UPB_SIZE(64, 128), 12, false, 255,
};
static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[8] = {
static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[7] = {
&google_protobuf_DescriptorProto_msginit,
&google_protobuf_DescriptorProto_ExtensionRange_msginit,
&google_protobuf_DescriptorProto_ReservedRange_msginit,
@ -82,7 +82,7 @@ static const upb_msglayout_field google_protobuf_DescriptorProto__fields[10] = {
const upb_msglayout google_protobuf_DescriptorProto_msginit = {
&google_protobuf_DescriptorProto_submsgs[0],
&google_protobuf_DescriptorProto__fields[0],
UPB_SIZE(48, 96), 10, false,
UPB_SIZE(48, 96), 10, false, 255,
};
static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = {
@ -98,7 +98,7 @@ static const upb_msglayout_field google_protobuf_DescriptorProto_ExtensionRange_
const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit = {
&google_protobuf_DescriptorProto_ExtensionRange_submsgs[0],
&google_protobuf_DescriptorProto_ExtensionRange__fields[0],
UPB_SIZE(16, 24), 3, false,
UPB_SIZE(16, 24), 3, false, 255,
};
static const upb_msglayout_field google_protobuf_DescriptorProto_ReservedRange__fields[2] = {
@ -109,7 +109,7 @@ static const upb_msglayout_field google_protobuf_DescriptorProto_ReservedRange__
const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit = {
NULL,
&google_protobuf_DescriptorProto_ReservedRange__fields[0],
UPB_SIZE(12, 12), 2, false,
UPB_SIZE(16, 16), 2, false, 255,
};
static const upb_msglayout *const google_protobuf_ExtensionRangeOptions_submsgs[1] = {
@ -123,7 +123,7 @@ static const upb_msglayout_field google_protobuf_ExtensionRangeOptions__fields[1
const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit = {
&google_protobuf_ExtensionRangeOptions_submsgs[0],
&google_protobuf_ExtensionRangeOptions__fields[0],
UPB_SIZE(4, 8), 1, false,
UPB_SIZE(8, 8), 1, false, 255,
};
static const upb_msglayout *const google_protobuf_FieldDescriptorProto_submsgs[1] = {
@ -131,23 +131,23 @@ static const upb_msglayout *const google_protobuf_FieldDescriptorProto_submsgs[1
};
static const upb_msglayout_field google_protobuf_FieldDescriptorProto__fields[11] = {
{1, UPB_SIZE(36, 40), 6, 0, 12, 1},
{2, UPB_SIZE(44, 56), 7, 0, 12, 1},
{3, UPB_SIZE(24, 24), 3, 0, 5, 1},
{4, UPB_SIZE(8, 8), 1, 0, 14, 1},
{5, UPB_SIZE(16, 16), 2, 0, 14, 1},
{6, UPB_SIZE(52, 72), 8, 0, 12, 1},
{7, UPB_SIZE(60, 88), 9, 0, 12, 1},
{8, UPB_SIZE(76, 120), 11, 0, 11, 1},
{9, UPB_SIZE(28, 28), 4, 0, 5, 1},
{10, UPB_SIZE(68, 104), 10, 0, 12, 1},
{17, UPB_SIZE(32, 32), 5, 0, 8, 1},
{1, UPB_SIZE(24, 24), 1, 0, 12, 1},
{2, UPB_SIZE(32, 40), 2, 0, 12, 1},
{3, UPB_SIZE(12, 12), 3, 0, 5, 1},
{4, UPB_SIZE(4, 4), 4, 0, 14, 1},
{5, UPB_SIZE(8, 8), 5, 0, 14, 1},
{6, UPB_SIZE(40, 56), 6, 0, 12, 1},
{7, UPB_SIZE(48, 72), 7, 0, 12, 1},
{8, UPB_SIZE(64, 104), 8, 0, 11, 1},
{9, UPB_SIZE(16, 16), 9, 0, 5, 1},
{10, UPB_SIZE(56, 88), 10, 0, 12, 1},
{17, UPB_SIZE(20, 20), 11, 0, 8, 1},
};
const upb_msglayout google_protobuf_FieldDescriptorProto_msginit = {
&google_protobuf_FieldDescriptorProto_submsgs[0],
&google_protobuf_FieldDescriptorProto__fields[0],
UPB_SIZE(80, 128), 11, false,
UPB_SIZE(72, 112), 11, false, 255,
};
static const upb_msglayout *const google_protobuf_OneofDescriptorProto_submsgs[1] = {
@ -162,7 +162,7 @@ static const upb_msglayout_field google_protobuf_OneofDescriptorProto__fields[2]
const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = {
&google_protobuf_OneofDescriptorProto_submsgs[0],
&google_protobuf_OneofDescriptorProto__fields[0],
UPB_SIZE(16, 32), 2, false,
UPB_SIZE(16, 32), 2, false, 255,
};
static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = {
@ -182,7 +182,7 @@ static const upb_msglayout_field google_protobuf_EnumDescriptorProto__fields[5]
const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = {
&google_protobuf_EnumDescriptorProto_submsgs[0],
&google_protobuf_EnumDescriptorProto__fields[0],
UPB_SIZE(32, 64), 5, false,
UPB_SIZE(32, 64), 5, false, 255,
};
static const upb_msglayout_field google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = {
@ -193,7 +193,7 @@ static const upb_msglayout_field google_protobuf_EnumDescriptorProto_EnumReserve
const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = {
NULL,
&google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0],
UPB_SIZE(12, 12), 2, false,
UPB_SIZE(16, 16), 2, false, 255,
};
static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = {
@ -201,15 +201,15 @@ static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_subms
};
static const upb_msglayout_field google_protobuf_EnumValueDescriptorProto__fields[3] = {
{1, UPB_SIZE(8, 8), 2, 0, 12, 1},
{2, UPB_SIZE(4, 4), 1, 0, 5, 1},
{1, UPB_SIZE(8, 8), 1, 0, 12, 1},
{2, UPB_SIZE(4, 4), 2, 0, 5, 1},
{3, UPB_SIZE(16, 24), 3, 0, 11, 1},
};
const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = {
&google_protobuf_EnumValueDescriptorProto_submsgs[0],
&google_protobuf_EnumValueDescriptorProto__fields[0],
UPB_SIZE(24, 32), 3, false,
UPB_SIZE(24, 32), 3, false, 255,
};
static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = {
@ -226,7 +226,7 @@ static const upb_msglayout_field google_protobuf_ServiceDescriptorProto__fields[
const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = {
&google_protobuf_ServiceDescriptorProto_submsgs[0],
&google_protobuf_ServiceDescriptorProto__fields[0],
UPB_SIZE(24, 48), 3, false,
UPB_SIZE(24, 48), 3, false, 255,
};
static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[1] = {
@ -234,18 +234,18 @@ static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[
};
static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6] = {
{1, UPB_SIZE(4, 8), 3, 0, 12, 1},
{2, UPB_SIZE(12, 24), 4, 0, 12, 1},
{3, UPB_SIZE(20, 40), 5, 0, 12, 1},
{4, UPB_SIZE(28, 56), 6, 0, 11, 1},
{5, UPB_SIZE(1, 1), 1, 0, 8, 1},
{6, UPB_SIZE(2, 2), 2, 0, 8, 1},
{1, UPB_SIZE(4, 8), 1, 0, 12, 1},
{2, UPB_SIZE(12, 24), 2, 0, 12, 1},
{3, UPB_SIZE(20, 40), 3, 0, 12, 1},
{4, UPB_SIZE(28, 56), 4, 0, 11, 1},
{5, UPB_SIZE(1, 1), 5, 0, 8, 1},
{6, UPB_SIZE(2, 2), 6, 0, 8, 1},
};
const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = {
&google_protobuf_MethodDescriptorProto_submsgs[0],
&google_protobuf_MethodDescriptorProto__fields[0],
UPB_SIZE(32, 64), 6, false,
UPB_SIZE(32, 64), 6, false, 255,
};
static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = {
@ -253,33 +253,33 @@ static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = {
};
static const upb_msglayout_field google_protobuf_FileOptions__fields[21] = {
{1, UPB_SIZE(28, 32), 11, 0, 12, 1},
{8, UPB_SIZE(36, 48), 12, 0, 12, 1},
{9, UPB_SIZE(8, 8), 1, 0, 14, 1},
{10, UPB_SIZE(16, 16), 2, 0, 8, 1},
{11, UPB_SIZE(44, 64), 13, 0, 12, 1},
{16, UPB_SIZE(17, 17), 3, 0, 8, 1},
{17, UPB_SIZE(18, 18), 4, 0, 8, 1},
{18, UPB_SIZE(19, 19), 5, 0, 8, 1},
{20, UPB_SIZE(20, 20), 6, 0, 8, 1},
{23, UPB_SIZE(21, 21), 7, 0, 8, 1},
{27, UPB_SIZE(22, 22), 8, 0, 8, 1},
{31, UPB_SIZE(23, 23), 9, 0, 8, 1},
{36, UPB_SIZE(52, 80), 14, 0, 12, 1},
{37, UPB_SIZE(60, 96), 15, 0, 12, 1},
{39, UPB_SIZE(68, 112), 16, 0, 12, 1},
{40, UPB_SIZE(76, 128), 17, 0, 12, 1},
{41, UPB_SIZE(84, 144), 18, 0, 12, 1},
{42, UPB_SIZE(24, 24), 10, 0, 8, 1},
{44, UPB_SIZE(92, 160), 19, 0, 12, 1},
{45, UPB_SIZE(100, 176), 20, 0, 12, 1},
{999, UPB_SIZE(108, 192), 0, 0, 11, 3},
{1, UPB_SIZE(20, 24), 1, 0, 12, 1},
{8, UPB_SIZE(28, 40), 2, 0, 12, 1},
{9, UPB_SIZE(4, 4), 3, 0, 14, 1},
{10, UPB_SIZE(8, 8), 4, 0, 8, 1},
{11, UPB_SIZE(36, 56), 5, 0, 12, 1},
{16, UPB_SIZE(9, 9), 6, 0, 8, 1},
{17, UPB_SIZE(10, 10), 7, 0, 8, 1},
{18, UPB_SIZE(11, 11), 8, 0, 8, 1},
{20, UPB_SIZE(12, 12), 9, 0, 8, 1},
{23, UPB_SIZE(13, 13), 10, 0, 8, 1},
{27, UPB_SIZE(14, 14), 11, 0, 8, 1},
{31, UPB_SIZE(15, 15), 12, 0, 8, 1},
{36, UPB_SIZE(44, 72), 13, 0, 12, 1},
{37, UPB_SIZE(52, 88), 14, 0, 12, 1},
{39, UPB_SIZE(60, 104), 15, 0, 12, 1},
{40, UPB_SIZE(68, 120), 16, 0, 12, 1},
{41, UPB_SIZE(76, 136), 17, 0, 12, 1},
{42, UPB_SIZE(16, 16), 18, 0, 8, 1},
{44, UPB_SIZE(84, 152), 19, 0, 12, 1},
{45, UPB_SIZE(92, 168), 20, 0, 12, 1},
{999, UPB_SIZE(100, 184), 0, 0, 11, 3},
};
const upb_msglayout google_protobuf_FileOptions_msginit = {
&google_protobuf_FileOptions_submsgs[0],
&google_protobuf_FileOptions__fields[0],
UPB_SIZE(112, 208), 21, false,
UPB_SIZE(104, 192), 21, false, 255,
};
static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = {
@ -297,7 +297,7 @@ static const upb_msglayout_field google_protobuf_MessageOptions__fields[5] = {
const upb_msglayout google_protobuf_MessageOptions_msginit = {
&google_protobuf_MessageOptions_submsgs[0],
&google_protobuf_MessageOptions__fields[0],
UPB_SIZE(12, 16), 5, false,
UPB_SIZE(16, 16), 5, false, 255,
};
static const upb_msglayout *const google_protobuf_FieldOptions_submsgs[1] = {
@ -305,19 +305,19 @@ static const upb_msglayout *const google_protobuf_FieldOptions_submsgs[1] = {
};
static const upb_msglayout_field google_protobuf_FieldOptions__fields[7] = {
{1, UPB_SIZE(8, 8), 1, 0, 14, 1},
{2, UPB_SIZE(24, 24), 3, 0, 8, 1},
{3, UPB_SIZE(25, 25), 4, 0, 8, 1},
{5, UPB_SIZE(26, 26), 5, 0, 8, 1},
{6, UPB_SIZE(16, 16), 2, 0, 14, 1},
{10, UPB_SIZE(27, 27), 6, 0, 8, 1},
{999, UPB_SIZE(28, 32), 0, 0, 11, 3},
{1, UPB_SIZE(4, 4), 1, 0, 14, 1},
{2, UPB_SIZE(12, 12), 2, 0, 8, 1},
{3, UPB_SIZE(13, 13), 3, 0, 8, 1},
{5, UPB_SIZE(14, 14), 4, 0, 8, 1},
{6, UPB_SIZE(8, 8), 5, 0, 14, 1},
{10, UPB_SIZE(15, 15), 6, 0, 8, 1},
{999, UPB_SIZE(16, 16), 0, 0, 11, 3},
};
const upb_msglayout google_protobuf_FieldOptions_msginit = {
&google_protobuf_FieldOptions_submsgs[0],
&google_protobuf_FieldOptions__fields[0],
UPB_SIZE(32, 40), 7, false,
UPB_SIZE(24, 24), 7, false, 255,
};
static const upb_msglayout *const google_protobuf_OneofOptions_submsgs[1] = {
@ -331,7 +331,7 @@ static const upb_msglayout_field google_protobuf_OneofOptions__fields[1] = {
const upb_msglayout google_protobuf_OneofOptions_msginit = {
&google_protobuf_OneofOptions_submsgs[0],
&google_protobuf_OneofOptions__fields[0],
UPB_SIZE(4, 8), 1, false,
UPB_SIZE(8, 8), 1, false, 255,
};
static const upb_msglayout *const google_protobuf_EnumOptions_submsgs[1] = {
@ -347,7 +347,7 @@ static const upb_msglayout_field google_protobuf_EnumOptions__fields[3] = {
const upb_msglayout google_protobuf_EnumOptions_msginit = {
&google_protobuf_EnumOptions_submsgs[0],
&google_protobuf_EnumOptions__fields[0],
UPB_SIZE(8, 16), 3, false,
UPB_SIZE(8, 16), 3, false, 255,
};
static const upb_msglayout *const google_protobuf_EnumValueOptions_submsgs[1] = {
@ -362,7 +362,7 @@ static const upb_msglayout_field google_protobuf_EnumValueOptions__fields[2] = {
const upb_msglayout google_protobuf_EnumValueOptions_msginit = {
&google_protobuf_EnumValueOptions_submsgs[0],
&google_protobuf_EnumValueOptions__fields[0],
UPB_SIZE(8, 16), 2, false,
UPB_SIZE(8, 16), 2, false, 255,
};
static const upb_msglayout *const google_protobuf_ServiceOptions_submsgs[1] = {
@ -377,7 +377,7 @@ static const upb_msglayout_field google_protobuf_ServiceOptions__fields[2] = {
const upb_msglayout google_protobuf_ServiceOptions_msginit = {
&google_protobuf_ServiceOptions_submsgs[0],
&google_protobuf_ServiceOptions__fields[0],
UPB_SIZE(8, 16), 2, false,
UPB_SIZE(8, 16), 2, false, 255,
};
static const upb_msglayout *const google_protobuf_MethodOptions_submsgs[1] = {
@ -385,15 +385,15 @@ static const upb_msglayout *const google_protobuf_MethodOptions_submsgs[1] = {
};
static const upb_msglayout_field google_protobuf_MethodOptions__fields[3] = {
{33, UPB_SIZE(16, 16), 2, 0, 8, 1},
{34, UPB_SIZE(8, 8), 1, 0, 14, 1},
{999, UPB_SIZE(20, 24), 0, 0, 11, 3},
{33, UPB_SIZE(8, 8), 1, 0, 8, 1},
{34, UPB_SIZE(4, 4), 2, 0, 14, 1},
{999, UPB_SIZE(12, 16), 0, 0, 11, 3},
};
const upb_msglayout google_protobuf_MethodOptions_msginit = {
&google_protobuf_MethodOptions_submsgs[0],
&google_protobuf_MethodOptions__fields[0],
UPB_SIZE(24, 32), 3, false,
UPB_SIZE(16, 24), 3, false, 255,
};
static const upb_msglayout *const google_protobuf_UninterpretedOption_submsgs[1] = {
@ -402,10 +402,10 @@ static const upb_msglayout *const google_protobuf_UninterpretedOption_submsgs[1]
static const upb_msglayout_field google_protobuf_UninterpretedOption__fields[7] = {
{2, UPB_SIZE(56, 80), 0, 0, 11, 3},
{3, UPB_SIZE(32, 32), 4, 0, 12, 1},
{4, UPB_SIZE(8, 8), 1, 0, 4, 1},
{5, UPB_SIZE(16, 16), 2, 0, 3, 1},
{6, UPB_SIZE(24, 24), 3, 0, 1, 1},
{3, UPB_SIZE(32, 32), 1, 0, 12, 1},
{4, UPB_SIZE(8, 8), 2, 0, 4, 1},
{5, UPB_SIZE(16, 16), 3, 0, 3, 1},
{6, UPB_SIZE(24, 24), 4, 0, 1, 1},
{7, UPB_SIZE(40, 48), 5, 0, 12, 1},
{8, UPB_SIZE(48, 64), 6, 0, 12, 1},
};
@ -413,18 +413,18 @@ static const upb_msglayout_field google_protobuf_UninterpretedOption__fields[7]
const upb_msglayout google_protobuf_UninterpretedOption_msginit = {
&google_protobuf_UninterpretedOption_submsgs[0],
&google_protobuf_UninterpretedOption__fields[0],
UPB_SIZE(64, 96), 7, false,
UPB_SIZE(64, 96), 7, false, 255,
};
static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = {
{1, UPB_SIZE(4, 8), 2, 0, 12, 2},
{2, UPB_SIZE(1, 1), 1, 0, 8, 2},
{1, UPB_SIZE(4, 8), 1, 0, 12, 2},
{2, UPB_SIZE(1, 1), 2, 0, 8, 2},
};
const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit = {
NULL,
&google_protobuf_UninterpretedOption_NamePart__fields[0],
UPB_SIZE(16, 32), 2, false,
UPB_SIZE(16, 32), 2, false, 255,
};
static const upb_msglayout *const google_protobuf_SourceCodeInfo_submsgs[1] = {
@ -438,7 +438,7 @@ static const upb_msglayout_field google_protobuf_SourceCodeInfo__fields[1] = {
const upb_msglayout google_protobuf_SourceCodeInfo_msginit = {
&google_protobuf_SourceCodeInfo_submsgs[0],
&google_protobuf_SourceCodeInfo__fields[0],
UPB_SIZE(4, 8), 1, false,
UPB_SIZE(8, 8), 1, false, 255,
};
static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = {
@ -452,7 +452,7 @@ static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields
const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = {
NULL,
&google_protobuf_SourceCodeInfo_Location__fields[0],
UPB_SIZE(32, 64), 5, false,
UPB_SIZE(32, 64), 5, false, 255,
};
static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = {
@ -466,20 +466,20 @@ static const upb_msglayout_field google_protobuf_GeneratedCodeInfo__fields[1] =
const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit = {
&google_protobuf_GeneratedCodeInfo_submsgs[0],
&google_protobuf_GeneratedCodeInfo__fields[0],
UPB_SIZE(4, 8), 1, false,
UPB_SIZE(8, 8), 1, false, 255,
};
static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = {
{1, UPB_SIZE(20, 32), 0, 0, 5, _UPB_LABEL_PACKED},
{2, UPB_SIZE(12, 16), 3, 0, 12, 1},
{3, UPB_SIZE(4, 4), 1, 0, 5, 1},
{4, UPB_SIZE(8, 8), 2, 0, 5, 1},
{2, UPB_SIZE(12, 16), 1, 0, 12, 1},
{3, UPB_SIZE(4, 4), 2, 0, 5, 1},
{4, UPB_SIZE(8, 8), 3, 0, 5, 1},
};
const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = {
NULL,
&google_protobuf_GeneratedCodeInfo_Annotation__fields[0],
UPB_SIZE(24, 48), 4, false,
UPB_SIZE(24, 48), 4, false, 255,
};
#include "upb/port_undef.inc"

@ -46,9 +46,9 @@ class BuildFileFunctions(object):
found_files = []
for file in files:
if os.path.isfile(file):
found_files.append(file)
elif os.path.isfile("generated_for_cmake/" + file):
found_files.append("generated_for_cmake/" + file)
found_files.append("../" + file)
elif os.path.isfile("cmake/" + file):
found_files.append("../cmake/" + file)
else:
print("Warning: no such file: " + file)
@ -117,6 +117,9 @@ class BuildFileFunctions(object):
def proto_library(self, **kwargs):
pass
def cc_proto_library(self, **kwargs):
pass
def generated_file_staleness_test(self, **kwargs):
pass
@ -126,6 +129,9 @@ class BuildFileFunctions(object):
def upb_proto_library(self, **kwargs):
pass
def upb_proto_library_copts(self, **kwargs):
pass
def upb_proto_reflection_library(self, **kwargs):
pass
@ -138,6 +144,9 @@ class BuildFileFunctions(object):
def config_setting(self, **kwargs):
pass
def upb_fasttable_enabled(self, **kwargs):
pass
def select(self, arg_dict):
return []
@ -163,6 +172,7 @@ class WorkspaceFileFunctions(object):
def workspace(self, **kwargs):
self.converter.prelude += "project(%s)\n" % (kwargs["name"])
self.converter.prelude += "set(CMAKE_C_STANDARD 99)\n"
def http_archive(self, **kwargs):
pass
@ -170,6 +180,9 @@ class WorkspaceFileFunctions(object):
def git_repository(self, **kwargs):
pass
def new_git_repository(self, **kwargs):
pass
def bazel_version_repository(self, **kwargs):
pass
@ -239,8 +252,8 @@ class Converter(object):
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
endif()
include_directories(.)
include_directories(generated_for_cmake)
include_directories(..)
include_directories(../cmake)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
if(APPLE)
@ -260,6 +273,7 @@ converter = Converter()
def GetDict(obj):
ret = {}
ret["UPB_DEFAULT_COPTS"] = [] # HACK
for k in dir(obj):
if not k.startswith("_"):
ret[k] = getattr(obj, k);
@ -269,6 +283,7 @@ globs = GetDict(converter)
exec(open("WORKSPACE").read(), GetDict(WorkspaceFileFunctions(converter)))
exec(open("BUILD").read(), GetDict(BuildFileFunctions(converter)))
exec(open("third_party/wyhash/BUILD").read(), GetDict(BuildFileFunctions(converter)))
with open(sys.argv[1], "w") as f:
f.write(converter.convert())

@ -6,7 +6,7 @@ with the actual list of files before we actually run the script.
from __future__ import absolute_import
from tools import staleness_test_lib
from cmake import staleness_test_lib
import unittest
import sys

@ -7,6 +7,7 @@ generated_file_staleness_test() rules.
from __future__ import absolute_import
from __future__ import print_function
import sys
import os
from shutil import copyfile
@ -47,13 +48,13 @@ def _GetFilePairs(config):
ret = []
has_bazel_genfiles = os.path.exists("bazel-genfiles")
has_bazel_genfiles = os.path.exists("bazel-bin")
for filename in config.file_list:
target = os.path.join(config.package_name, filename)
generated = os.path.join(config.package_name, config.pattern % filename)
if has_bazel_genfiles:
generated = os.path.join("bazel-genfiles", generated)
generated = os.path.join("bazel-bin", generated)
# Generated files should always exist. Blaze should guarantee this before
# we are run.
@ -61,6 +62,7 @@ def _GetFilePairs(config):
print("Generated file '%s' does not exist." % generated)
print("Please run this command to generate it:")
print(" bazel build %s:%s" % (config.package_name, config.target_name))
sys.exit(1)
ret.append(_FilePair(target, generated))
return ret
@ -87,10 +89,9 @@ def _GetMissingAndStaleFiles(file_pairs):
missing_files.append(pair)
continue
generated = open(pair.generated).read()
target = open(pair.target).read()
if generated != target:
stale_files.append(pair)
with open(pair.generated) as g, open(pair.target) as t:
if g.read() != t.read():
stale_files.append(pair)
return missing_files, stale_files

@ -953,7 +953,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf,
upb_fieldtype_t type = upb_fielddef_type(p->top->f);
double val;
double dummy;
double inf = UPB_INFINITY;
double inf = INFINITY;
errno = 0;
@ -3295,7 +3295,7 @@ static void json_parser_reset(upb_json_parser *p) {
static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
const upb_msgdef *md) {
upb_msg_field_iter i;
int i, n;
upb_alloc *alloc = upb_arena_alloc(c->arena);
upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m));
@ -3306,14 +3306,13 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
upb_byteshandler_setstring(&m->input_handler_, parse, m);
upb_byteshandler_setendstr(&m->input_handler_, end, m);
upb_strtable_init2(&m->name_table, UPB_CTYPE_CONSTPTR, alloc);
upb_strtable_init2(&m->name_table, UPB_CTYPE_CONSTPTR, 4, alloc);
/* Build name_table */
for(upb_msg_field_begin(&i, md);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
const upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(md);
for(i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(md, i);
upb_value v = upb_value_constptr(f);
const char *name;
@ -3402,7 +3401,7 @@ const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
const upb_msgdef *md) {
upb_json_parsermethod *m;
upb_value v;
upb_msg_field_iter i;
int i, n;
upb_alloc *alloc = upb_arena_alloc(c->arena);
if (upb_inttable_lookupptr(&c->methods, md, &v)) {
@ -3417,10 +3416,9 @@ const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
/* Populate parser methods for all submessages, so the name tables will
* be available during parsing. */
for(upb_msg_field_begin(&i, md);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(md);
for(i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(md, i);
if (upb_fielddef_issubmsg(f)) {
const upb_msgdef *subdef = upb_fielddef_msgsubdef(f);

@ -1,6 +1,8 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@upb//bazel:upb_proto_library.bzl", "upb_proto_library")
licenses(["notice"])
proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
@ -14,5 +16,6 @@ upb_proto_library(
cc_binary(
name = "test_binary",
srcs = ["test_binary.c"],
copts = ["-std=c99"],
deps = [":foo_upbproto"],
)

@ -1,7 +1,7 @@
#include <time.h>
#include "foo.upb.h"
#include "examples/bazel/foo.upb.h"
int main() {
upb_arena *arena = upb_arena_new();

@ -11,29 +11,40 @@ fi
echo PATH=$PATH
ls -l `which cmake`
cmake --version
echo CC=${CC:-cc}
${CC:-cc} --version
# Log the bazel path and version.
which bazel
bazel version
cd $(dirname $0)/../..
bazel test --test_output=errors :all
if [[ $(uname) = "Linux" ]]; then
# Verify the ASAN build. Have to exclude test_conformance_upb as protobuf
# currently leaks memory in the conformance test runner.
bazel test --copt=-fsanitize=address --linkopt=-fsanitize=address --test_output=errors :all
if which gcc; then
gcc --version
CC=gcc bazel test -c opt --test_output=errors ... -- -benchmarks:benchmark
if [[ $(uname) = "Linux" ]]; then
CC=gcc bazel test --test_output=errors ...
CC=gcc bazel test --test_output=errors ... --//:fasttable_enabled=true -- -cmake:test_generated_files -benchmarks:benchmark
fi
# TODO: work through these errors and enable this.
# if gcc -fanalyzer -x c /dev/null -c -o /dev/null; then
# CC=gcc bazel test --copt=-fanalyzer --test_output=errors ...
# fi
fi
# Verify the UBSan build. Have to exclude Lua as the version we are using
# fails some UBSan tests.
if which clang; then
if [[ $(uname) = "Linux" ]]; then
CC=clang bazel test --test_output=errors ...
CC=clang bazel test --test_output=errors -c opt ... -- -benchmarks:benchmark
CC=clang bazel test --test_output=errors ... --//:fasttable_enabled=true -- -cmake:test_generated_files
# For some reason kokoro doesn't have Clang available right now.
#CC=clang CXX=clang++ bazel test -c dbg --copt=-fsanitize=undefined --copt=-fno-sanitize=function,vptr --linkopt=-fsanitize=undefined --action_env=UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 -- :all -:test_lua
CC=clang bazel test --test_output=errors --config=m32 ... -- -benchmarks:benchmark
CC=clang bazel test --test_output=errors --config=asan ... -- -benchmarks:benchmark
# TODO: update to a newer Lua that hopefully does not trigger UBSAN.
CC=clang bazel test --test_output=errors --config=ubsan ... -- -tests/bindings/lua:test_lua
fi
fi
if which valgrind; then
bazel test --run_under='valgrind --leak-check=full --error-exitcode=1' :all -- -:test_conformance_upb -:cmake_build
bazel test --config=valgrind ... -- -tests:test_conformance_upb -cmake:cmake_build
fi

@ -1,2 +1,2 @@
build_file: "upb/kokoro/ubuntu/build.sh"
timeout_mins: 15
timeout_mins: 30

@ -0,0 +1,283 @@
load(
"//bazel:build_defs.bzl",
"UPB_DEFAULT_COPTS",
"UPB_DEFAULT_CPPOPTS",
"make_shell_script",
)
load(
"//bazel:upb_proto_library.bzl",
"upb_proto_library",
"upb_proto_reflection_library",
)
licenses(["notice"])
config_setting(
name = "fuzz",
values = {"define": "fuzz=true"},
)
cc_library(
name = "upb_test",
testonly = 1,
srcs = [
"testmain.cc",
],
hdrs = [
"test_util.h",
"upb_test.h",
],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
"//:handlers",
"//:port",
"//:upb",
],
)
proto_library(
name = "test_proto",
testonly = 1,
srcs = ["test.proto"],
)
upb_proto_library(
name = "test_upb_proto",
testonly = 1,
deps = [":test_proto"],
)
cc_test(
name = "test_generated_code",
srcs = ["test_generated_code.c"],
copts = UPB_DEFAULT_COPTS,
deps = [
":empty_upbdefs_proto",
":test_messages_proto3_proto_upb",
":test_upb_proto",
":upb_test",
],
)
proto_library(
name = "empty_proto",
srcs = ["empty.proto"],
)
upb_proto_reflection_library(
name = "empty_upbdefs_proto",
testonly = 1,
deps = [":empty_proto"],
)
upb_proto_library(
name = "test_messages_proto3_proto_upb",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto3_proto"],
)
proto_library(
name = "test_decoder_proto",
srcs = [
"pb/test_decoder.proto",
],
)
upb_proto_reflection_library(
name = "test_decoder_upb_proto",
deps = [":test_decoder_proto"],
)
cc_test(
name = "test_decoder",
srcs = ["pb/test_decoder.cc"],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":test_decoder_upb_proto",
":upb_test",
"//:handlers",
"//:port",
"//:upb",
"//:upb_pb",
],
)
proto_library(
name = "test_cpp_proto",
srcs = [
"test_cpp.proto",
],
)
upb_proto_reflection_library(
name = "test_cpp_upb_proto",
deps = ["test_cpp_proto"],
)
cc_test(
name = "test_cpp",
srcs = ["test_cpp.cc"],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":test_cpp_upb_proto",
":upb_test",
"//:handlers",
"//:port",
"//:reflection",
"//:upb",
"//:upb_pb",
],
)
cc_test(
name = "test_table",
srcs = ["test_table.cc"],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":upb_test",
"//:port",
"//:table",
"//:upb",
],
)
# OSS-Fuzz test
cc_binary(
name = "file_descriptor_parsenew_fuzzer",
testonly = 1,
srcs = ["file_descriptor_parsenew_fuzzer.cc"],
copts = UPB_DEFAULT_CPPOPTS + select({
"//conditions:default": [],
":fuzz": ["-fsanitize=fuzzer,address"],
}),
defines = select({
"//conditions:default": [],
":fuzz": ["HAVE_FUZZER"],
}),
deps = [
"//:descriptor_upb_proto",
"//:upb",
],
)
# copybara:strip_for_google3_begin
cc_test(
name = "test_encoder",
srcs = ["pb/test_encoder.cc"],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":upb_test",
"//:descriptor_upb_proto",
"//:descriptor_upb_proto_reflection",
"//:upb",
"//:upb_pb",
],
)
proto_library(
name = "test_json_enum_from_separate",
srcs = ["json/enum_from_separate_file.proto"],
deps = [":test_json_proto"],
)
proto_library(
name = "test_json_proto",
srcs = ["json/test.proto"],
)
upb_proto_reflection_library(
name = "test_json_upb_proto_reflection",
deps = ["test_json_proto"],
)
upb_proto_library(
name = "test_json_enum_from_separate_upb_proto",
deps = [":test_json_enum_from_separate"],
)
upb_proto_library(
name = "test_json_upb_proto",
deps = [":test_json_proto"],
)
cc_test(
name = "test_json",
srcs = [
"json/test_json.cc",
],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":test_json_upb_proto",
":test_json_upb_proto_reflection",
":upb_test",
"//:upb_json",
],
)
# copybara:strip_end
upb_proto_library(
name = "conformance_proto_upb",
testonly = 1,
deps = ["@com_google_protobuf//:conformance_proto"],
)
upb_proto_reflection_library(
name = "conformance_proto_upbdefs",
testonly = 1,
deps = ["@com_google_protobuf//:conformance_proto"],
)
upb_proto_reflection_library(
name = "test_messages_proto2_upbdefs",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto2_proto"],
)
upb_proto_reflection_library(
name = "test_messages_proto3_upbdefs",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto3_proto"],
)
cc_binary(
name = "conformance_upb",
testonly = 1,
srcs = [
"conformance_upb.c",
],
copts = UPB_DEFAULT_COPTS,
data = [
"conformance_upb_failures.txt",
],
deps = [
":conformance_proto_upb",
":conformance_proto_upbdefs",
":test_messages_proto2_upbdefs",
":test_messages_proto3_upbdefs",
"//:json",
"//:port",
"//:reflection",
"//:textformat",
"//:upb",
],
)
make_shell_script(
name = "gen_test_conformance_upb",
out = "test_conformance_upb.sh",
contents = "external/com_google_protobuf/conformance_test_runner " +
" --enforce_recommended " +
" --failure_list ./tests/conformance_upb_failures.txt" +
" ./tests/conformance_upb",
)
sh_test(
name = "test_conformance_upb",
srcs = ["test_conformance_upb.sh"],
data = [
"conformance_upb_failures.txt",
":conformance_upb",
"@com_google_protobuf//:conformance_test_runner",
],
deps = ["@bazel_tools//tools/bash/runfiles"],
)

@ -1,64 +0,0 @@
#include <string.h>
#include <benchmark/benchmark.h>
#include "google/protobuf/descriptor.upb.h"
#include "google/protobuf/descriptor.upbdefs.h"
upb_strview descriptor = google_protobuf_descriptor_proto_upbdefinit.descriptor;
/* A buffer big enough to parse descriptor.proto without going to heap. */
char buf[65535];
static void BM_ArenaOneAlloc(benchmark::State& state) {
for (auto _ : state) {
upb_arena* arena = upb_arena_new();
upb_arena_malloc(arena, 1);
upb_arena_free(arena);
}
}
BENCHMARK(BM_ArenaOneAlloc);
static void BM_ArenaInitialBlockOneAlloc(benchmark::State& state) {
for (auto _ : state) {
upb_arena* arena = upb_arena_init(buf, sizeof(buf), NULL);
upb_arena_malloc(arena, 1);
upb_arena_free(arena);
}
}
BENCHMARK(BM_ArenaInitialBlockOneAlloc);
static void BM_ParseDescriptorNoHeap(benchmark::State& state) {
size_t bytes = 0;
for (auto _ : state) {
upb_arena* arena = upb_arena_init(buf, sizeof(buf), NULL);
google_protobuf_FileDescriptorProto* set =
google_protobuf_FileDescriptorProto_parse(descriptor.data,
descriptor.size, arena);
if (!set) {
printf("Failed to parse.\n");
exit(1);
}
bytes += descriptor.size;
upb_arena_free(arena);
}
state.SetBytesProcessed(state.iterations() * descriptor.size);
}
BENCHMARK(BM_ParseDescriptorNoHeap);
static void BM_ParseDescriptor(benchmark::State& state) {
size_t bytes = 0;
for (auto _ : state) {
upb_arena* arena = upb_arena_new();
google_protobuf_FileDescriptorProto* set =
google_protobuf_FileDescriptorProto_parse(descriptor.data,
descriptor.size, arena);
if (!set) {
printf("Failed to parse.\n");
exit(1);
}
bytes += descriptor.size;
upb_arena_free(arena);
}
state.SetBytesProcessed(state.iterations() * descriptor.size);
}
BENCHMARK(BM_ParseDescriptor);

@ -0,0 +1,68 @@
load(
"//upb/bindings/lua:lua_proto_library.bzl",
"lua_proto_library",
)
load(
"//bazel:build_defs.bzl",
"UPB_DEFAULT_COPTS",
)
licenses(["notice"])
cc_test(
name = "test_lua",
srcs = ["main.c"],
copts = UPB_DEFAULT_COPTS,
data = [
"test_upb.lua",
":descriptor_proto_lua",
":empty_proto_lua",
":test_messages_proto2_proto_lua",
":test_messages_proto3_proto_lua",
":test_proto_lua",
"//:third_party/lunit/console.lua",
"//:third_party/lunit/lunit.lua",
"//upb/bindings/lua:upb.lua",
"@com_google_protobuf//:conformance_proto",
"@com_google_protobuf//:descriptor_proto",
],
linkstatic = 1,
deps = [
"//upb/bindings/lua:lupb",
"@lua//:liblua",
],
)
proto_library(
name = "test_proto",
testonly = 1,
srcs = ["test.proto"],
)
lua_proto_library(
name = "test_proto_lua",
testonly = 1,
deps = [":test_proto"],
)
lua_proto_library(
name = "descriptor_proto_lua",
deps = ["@com_google_protobuf//:descriptor_proto"],
)
lua_proto_library(
name = "empty_proto_lua",
deps = ["@com_google_protobuf//:empty_proto"],
)
lua_proto_library(
name = "test_messages_proto3_proto_lua",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto3_proto"],
)
lua_proto_library(
name = "test_messages_proto2_proto_lua",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto2_proto"],
)

@ -34,7 +34,7 @@ const char *init =
"upb/bindings/lua/?.lua"
"'";
int main() {
int main(int argc, char **argv) {
int ret = 0;
L = luaL_newstate();
luaL_openlibs(L);

@ -0,0 +1,28 @@
syntax = "proto2";
package upb_test;
message MapTest {
map<string, double> map_string_double = 1;
}
message PackedTest {
repeated bool bool_packed = 1 [packed = true];
repeated int32 i32_packed = 2 [packed = true];
repeated int64 i64_packed = 3 [packed = true];
repeated fixed32 f32_packed = 4 [packed = true];
repeated fixed64 f64_packed = 5 [packed = true];
}
message UnpackedTest {
repeated bool bool_packed = 1 [packed = false];
repeated int32 i32_packed = 2 [packed = false];
repeated int64 i64_packed = 3 [packed = false];
repeated fixed32 f32_packed = 4 [packed = false];
repeated fixed64 f64_packed = 5 [packed = false];
}
message TestLargeFieldNumber {
optional int32 i32 = 456214797;
}

@ -1,10 +1,11 @@
local upb = require "lupb"
local lunit = require "lunit"
local upb_test = require "tests.test_pb"
local upb_test = require "tests.bindings.lua.test_pb"
local test_messages_proto3 = require "google.protobuf.test_messages_proto3_pb"
local test_messages_proto2 = require "google.protobuf.test_messages_proto2_pb"
local descriptor = require "google.protobuf.descriptor_pb"
local empty = require "google.protobuf.empty_pb"
if _VERSION >= 'Lua 5.2' then
_ENV = lunit.module("testupb", "seeall")
@ -37,6 +38,27 @@ function test_def_readers()
assert_nil(f:containing_oneof())
assert_equal(m, f:containing_type())
assert_equal(0, f:default())
local message_field_count = 0
for field in m:fields() do
message_field_count = message_field_count + 1
end
assert_equal(message_field_count, #m)
local message_oneof_count = 0
for oneof in m:oneofs() do
message_oneof_count = message_oneof_count + 1
end
assert_equal(message_oneof_count, m:oneof_count())
-- oneof
local o = m:lookup_name("oneof_field")
assert_equal("oneof_field", o:name())
assert_equal(m, o:containing_type())
local oneof_field_count = 0
for field in o:fields() do
oneof_field_count = oneof_field_count + 1
end
assert_equal(oneof_field_count, #o)
-- enum
local e = test_messages_proto3['TestAllTypesProto3.NestedEnum']
@ -70,6 +92,69 @@ function test_msg_map()
assert_equal(12, msg2.map_int32_int32[6])
end
function test_map_sorting()
function msg_with_int32_entries(start, expand)
local msg = test_messages_proto3.TestAllTypesProto3()
for i=start,start + 8 do
msg.map_int32_int32[i] = i * 2
end
if expand then
for i=start+20,200 do
msg.map_int32_int32[i] = i
end
for i=start+20,200 do
msg.map_int32_int32[i] = nil
end
end
return msg
end
function msg_with_msg_entries(expand)
local msg = test_messages_proto3.TestAllTypesProto3()
-- 8! = 40320 possible orderings makes it overwhelmingly likely that two
-- random orderings will be different.
for i=1,8 do
local submsg = test_messages_proto3.TestAllTypesProto3.NestedMessage()
submsg.corecursive = msg_with_int32_entries(i, expand)
msg.map_string_nested_message[tostring(i)] = submsg
end
expand = false
if expand then
for i=21,2000 do
local submsg = test_messages_proto3.TestAllTypesProto3.NestedMessage()
submsg.corecursive = msg_with_int32_entries(i, expand)
msg.map_string_nested_message[tostring(i)] = submsg
end
for i=21,2000 do
msg.map_string_nested_message[tostring(i)] = nil
end
end
return msg
end
-- Create two messages with the same contents but (hopefully) different
-- map table orderings.
local msg = msg_with_msg_entries(false)
local msg2 = msg_with_msg_entries(true)
local text1 = upb.text_encode(msg)
local text2 = upb.text_encode(msg2)
assert_equal(text1, text2)
local binary1 = upb.encode(msg, {upb.ENCODE_DETERMINISTIC})
local binary2 = upb.encode(msg2, {upb.ENCODE_DETERMINISTIC})
assert_equal(binary1, binary2)
-- Non-sorted map should compare different.
local text3 = upb.text_encode(msg, {upb.TXTENC_NOSORT})
assert_not_equal(text1, text3)
local binary3 = upb.encode(msg)
assert_not_equal(binary1, binary3)
end
function test_utf8()
local proto2_msg = test_messages_proto2.TestAllTypesProto2()
proto2_msg.optional_string = "\xff"
@ -83,7 +168,7 @@ function test_utf8()
upb.decode(test_messages_proto3.TestAllTypesProto3, serialized)
end)
-- TOOD(haberman): should proto3 accessors also check UTF-8 at set time?
-- TODO(haberman): should proto3 accessors also check UTF-8 at set time?
end
function test_string_double_map()
@ -112,6 +197,65 @@ function test_string_double_map()
assert_equal(2.5, msg2.map_string_double["two point five"])
end
function test_string_double_map()
local function fill_msg(msg)
msg.i32_packed[1] = 100
msg.i32_packed[2] = 200
msg.i32_packed[3] = 50000
msg.i64_packed[1] = 101
msg.i64_packed[2] = 201
msg.i64_packed[3] = 50001
msg.f32_packed[1] = 102
msg.f32_packed[2] = 202
msg.f32_packed[3] = 50002
msg.f64_packed[1] = 103
msg.f64_packed[2] = 203
msg.f64_packed[3] = 50003
end
local function check_msg(msg)
assert_equal(100, msg.i32_packed[1])
assert_equal(200, msg.i32_packed[2])
assert_equal(50000, msg.i32_packed[3])
assert_equal(3, #msg.i32_packed)
assert_equal(101, msg.i64_packed[1])
assert_equal(201, msg.i64_packed[2])
assert_equal(50001, msg.i64_packed[3])
assert_equal(3, #msg.i64_packed)
assert_equal(102, msg.f32_packed[1])
assert_equal(202, msg.f32_packed[2])
assert_equal(50002, msg.f32_packed[3])
assert_equal(3, #msg.f32_packed)
assert_equal(103, msg.f64_packed[1])
assert_equal(203, msg.f64_packed[2])
assert_equal(50003, msg.f64_packed[3])
assert_equal(3, #msg.f64_packed)
end
local msg = upb_test.PackedTest()
fill_msg(msg)
check_msg(msg)
local serialized_packed = upb.encode(msg)
local msg2 = upb.decode(upb_test.PackedTest, serialized_packed)
local msg3 = upb.decode(upb_test.UnpackedTest, serialized_packed)
check_msg(msg2)
check_msg(msg3)
serialized_unpacked = upb.encode(msg3)
local msg4 = upb.decode(upb_test.PackedTest, serialized_unpacked)
local msg5 = upb.decode(upb_test.PackedTest, serialized_unpacked)
check_msg(msg4)
check_msg(msg5)
end
function test_msg_string_map()
msg = test_messages_proto3.TestAllTypesProto3()
msg.map_string_string["foo"] = "bar"
@ -276,6 +420,22 @@ local numeric_types = {
},
}
function test_utf8()
local invalid_utf8 = "\xff"
local proto2_msg = test_messages_proto2.TestAllTypesProto2{
optional_string = invalid_utf8,
}
-- As proto2, invalid UTF-8 parses and serializes fine.
local serialized = upb.encode(proto2_msg)
local proto2_msg2 = upb.decode(test_messages_proto2.TestAllTypesProto2, serialized)
-- Decoding as proto3 fails.
assert_error(function()
upb.decode(test_messages_proto3.TestAllTypesProto3, serialized)
end)
end
function test_msg_primitives()
local msg = test_messages_proto3.TestAllTypesProto3{
optional_int32 = 10,
@ -460,6 +620,13 @@ function test_numeric_map()
end
end
function test_unknown()
local bytes = string.rep("\x38\x00", 1000)
for i=1,1000 do
local msg = upb.decode(test_messages_proto3.TestAllTypesProto3, bytes)
end
end
function test_foo()
local symtab = upb.SymbolTable()
local filename = "external/com_google_protobuf/descriptor_proto-descriptor-set.proto.bin"
@ -484,6 +651,69 @@ function test_foo()
assert_equal(set.file[1].name, "google/protobuf/descriptor.proto")
end
function test_descriptor()
local symtab = upb.SymbolTable()
local file_proto = descriptor.FileDescriptorProto {
name = "test.proto",
message_type = upb.Array(descriptor.DescriptorProto, {
descriptor.DescriptorProto{
name = "ABC",
},
})
}
local file = symtab:add_file(upb.encode(file_proto))
assert_equal(file:symtab(), symtab)
end
function test_descriptor_error()
local symtab = upb.SymbolTable()
local file = descriptor.FileDescriptorProto()
file.name = "test.proto"
file.message_type[1] = descriptor.DescriptorProto{
name = "ABC"
}
file.message_type[2] = descriptor.DescriptorProto{
name = "BC."
}
assert_error(function () symtab:add_file(upb.encode(file)) end)
assert_nil(symtab:lookup_msg("ABC"))
end
function test_encode_skipunknown()
-- Test that upb.ENCODE_SKIPUNKNOWN does not encode unknown fields.
local msg = test_messages_proto3.TestAllTypesProto3{
optional_int32 = 10,
optional_uint32 = 20,
optional_int64 = 30,
}
-- SKIPUNKNOWN here tests that it does *not* affect regular fields.
local serialized = upb.encode(msg, {upb.ENCODE_SKIPUNKNOWN})
assert_true(#serialized > 0)
local empty_with_unknown = upb.decode(empty.Empty, serialized)
assert_true(#upb.encode(empty_with_unknown) > 0)
-- Verify that unknown fields are not serialized.
assert_true(#upb.encode(empty_with_unknown, {upb.ENCODE_SKIPUNKNOWN}) == 0)
end
function test_json_emit_defaults()
local msg = test_messages_proto3.TestAllTypesProto3()
local json = upb.json_encode(msg, {upb.JSONENC_EMITDEFAULTS})
end
function test_encode_depth_limit()
local msg = test_messages_proto3.TestAllTypesProto3()
msg.recursive_message = msg
assert_error(function() upb.encode(msg) end)
end
function test_large_field_number()
local msg = upb_test.TestLargeFieldNumber()
msg.i32 = 5
local serialized = upb.encode(msg)
local msg2 = upb.decode(upb_test.TestLargeFieldNumber, serialized)
assert_equal(msg.i32, msg2.i32)
end
function test_gc()
local top = test_messages_proto3.TestAllTypesProto3()
local n = 100

@ -201,6 +201,16 @@ void DoTest(const ctx* c) {
upb_msg *msg;
upb_strview name = conformance_ConformanceRequest_message_type(c->request);
const upb_msgdef *m = upb_symtab_lookupmsg2(c->symtab, name.data, name.size);
#if 0
// Handy code for limiting conformance tests to a single input payload.
// This is a hack since the conformance runner doesn't give an easy way to
// specify what test should be run.
const char skip[] = "\343>\010\301\002\344>\230?\001\230?\002\230?\003";
upb_strview skip_str = upb_strview_make(skip, sizeof(skip) - 1);
upb_strview pb_payload =
conformance_ConformanceRequest_protobuf_payload(c->request);
if (!upb_strview_eql(pb_payload, skip_str)) m = NULL;
#endif
if (!m) {
static const char msg[] = "Unknown message type.";
@ -286,6 +296,7 @@ int main(void) {
if (!DoTestIo(symtab)) {
fprintf(stderr, "conformance_upb: received EOF from test runner "
"after %d tests, exiting\n", test_count);
upb_symtab_free(symtab);
return 0;
}
}

@ -43,7 +43,6 @@
#else // AMALGAMATED
#include "upb/handlers.h"
#include "upb/pb/decoder.h"
#include "upb/pb/varint.int.h"
#include "upb/upb.h"
#endif // !AMALGAMATED
@ -53,17 +52,6 @@
#define PRINT_FAILURE(expr) \
fprintf(stderr, "Assertion failed: %s:%d\n", __FILE__, __LINE__); \
fprintf(stderr, "expr: %s\n", #expr); \
if (testhash) { \
fprintf(stderr, "assertion failed running test %x.\n", testhash); \
if (!filter_hash) { \
fprintf(stderr, \
"Run with the arg %x to run only this test. " \
"(This will also turn on extra debugging output)\n", \
testhash); \
} \
fprintf(stderr, "Failed at %02.2f%% through tests.\n", \
(float)completed * 100 / total); \
}
#define MAX_NESTING 64
@ -114,7 +102,7 @@ using std::string;
void vappendf(string* str, const char *format, va_list args) {
va_list copy;
_upb_va_copy(copy, args);
va_copy(copy, args);
int count = vsnprintf(NULL, 0, format, args);
if (count >= 0)
@ -147,6 +135,29 @@ void PrintBinary(const string& str) {
}
}
#define UPB_PB_VARINT_MAX_LEN 10
static size_t upb_vencode64(uint64_t val, char *buf) {
size_t i;
if (val == 0) { buf[0] = 0; return 1; }
i = 0;
while (val) {
uint8_t byte = val & 0x7fU;
val >>= 7;
if (val) byte |= 0x80U;
buf[i++] = byte;
}
return i;
}
static uint32_t upb_zzenc_32(int32_t n) {
return ((uint32_t)n << 1) ^ (n >> 31);
}
static uint64_t upb_zzenc_64(int64_t n) {
return ((uint64_t)n << 1) ^ (n >> 63);
}
/* Routines for building arbitrary protos *************************************/
const string empty;
@ -445,17 +456,6 @@ upb::pb::DecoderPtr CreateDecoder(upb::Arena* arena,
return ret;
}
uint32_t Hash(const string& proto, const string* expected_output, size_t seam1,
size_t seam2, bool may_skip) {
uint32_t hash = upb_murmur_hash2(proto.c_str(), proto.size(), 0);
if (expected_output)
hash = upb_murmur_hash2(expected_output->c_str(), expected_output->size(), hash);
hash = upb_murmur_hash2(&seam1, sizeof(seam1), hash);
hash = upb_murmur_hash2(&seam2, sizeof(seam2), hash);
hash = upb_murmur_hash2(&may_skip, sizeof(may_skip), hash);
return hash;
}
void CheckBytesParsed(upb::pb::DecoderPtr decoder, size_t ofs) {
// We can't have parsed more data than the decoder callback is telling us it
// parsed.
@ -484,13 +484,11 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder,
env->Reset(proto.c_str(), proto.size(), may_skip, expected_output == NULL);
decoder.Reset();
testhash = Hash(proto, expected_output, i, j, may_skip);
if (filter_hash && testhash != filter_hash) return;
if (test_mode != COUNT_ONLY) {
output.clear();
if (filter_hash) {
fprintf(stderr, "RUNNING TEST CASE, hash=%x\n", testhash);
fprintf(stderr, "RUNNING TEST CASE\n");
fprintf(stderr, "Input (len=%u): ", (unsigned)proto.size());
PrintBinary(proto);
fprintf(stderr, "\n");
@ -549,7 +547,6 @@ void run_decoder(const string& proto, const string* expected_output) {
}
}
}
testhash = 0;
}
const static string thirty_byte_nop = cat(
@ -849,23 +846,17 @@ void test_valid() {
// Empty protobuf where we never call PutString between
// StartString/EndString.
// Randomly generated hash for this test, hope it doesn't conflict with others
// by chance.
const uint32_t emptyhash = 0x5709be8e;
if (!filter_hash || filter_hash == testhash) {
testhash = emptyhash;
upb::Status status;
upb::Arena arena;
upb::Sink sink(global_handlers, &closures[0]);
upb::pb::DecoderPtr decoder =
CreateDecoder(&arena, global_method, sink, &status);
output.clear();
bool ok = upb::PutBuffer(std::string(), decoder.input());
ASSERT(ok);
ASSERT(status.ok());
if (test_mode == ALL_HANDLERS) {
ASSERT(output == string("<\n>\n"));
}
upb::Status status;
upb::Arena arena;
upb::Sink sink(global_handlers, &closures[0]);
upb::pb::DecoderPtr decoder =
CreateDecoder(&arena, global_method, sink, &status);
output.clear();
bool ok = upb::PutBuffer(std::string(), decoder.input());
ASSERT(ok);
ASSERT(status.ok());
if (test_mode == ALL_HANDLERS) {
ASSERT(output == string("<\n>\n"));
}
test_valid_data_for_signed_type(UPB_DESCRIPTOR_TYPE_DOUBLE,

@ -5,12 +5,65 @@
#include "google/protobuf/descriptor.upbdefs.h"
#include "tests/test_util.h"
#include "tests/upb_test.h"
#include "upb/bindings/stdc++/string.h"
#include "upb/pb/decoder.h"
#include "upb/pb/encoder.h"
#include "upb/port_def.inc"
#include "upb/upb.hpp"
template <class T>
class FillStringHandler {
public:
static void SetHandler(upb_byteshandler* handler) {
upb_byteshandler_setstartstr(handler, &FillStringHandler::StartString,
NULL);
upb_byteshandler_setstring(handler, &FillStringHandler::StringBuf, NULL);
}
private:
// TODO(haberman): add UpbBind/UpbMakeHandler support to BytesHandler so these
// can be prettier callbacks.
static void* StartString(void *c, const void *hd, size_t size) {
UPB_UNUSED(hd);
UPB_UNUSED(size);
T* str = static_cast<T*>(c);
str->clear();
return c;
}
static size_t StringBuf(void* c, const void* hd, const char* buf, size_t n,
const upb_bufhandle* h) {
UPB_UNUSED(hd);
UPB_UNUSED(h);
T* str = static_cast<T*>(c);
try {
str->append(buf, n);
return n;
} catch (const std::exception&) {
return 0;
}
}
};
class StringSink {
public:
template <class T>
explicit StringSink(T* target) {
// TODO(haberman): we need to avoid rebuilding a new handler every time,
// but with class globals disallowed for google3 C++ this is tricky.
upb_byteshandler_init(&handler_);
FillStringHandler<T>::SetHandler(&handler_);
input_.Reset(&handler_, target);
}
upb::BytesSink input() { return input_; }
private:
upb_byteshandler handler_;
upb::BytesSink input_;
};
void test_pb_roundtrip() {
std::string input(
google_protobuf_descriptor_proto_upbdefinit.descriptor.data,
@ -29,7 +82,7 @@ void test_pb_roundtrip() {
const upb::pb::DecoderMethodPtr method = decoder_cache.Get(md);
std::string output;
upb::StringSink string_sink(&output);
StringSink string_sink(&output);
upb::pb::EncoderPtr encoder =
upb::pb::EncoderPtr::Create(&arena, encoder_handlers, string_sink.input());
upb::pb::DecoderPtr decoder =

@ -1,126 +0,0 @@
#include <stdio.h>
#include "upb/pb/varint.int.h"
#include "tests/upb_test.h"
#include "upb/port_def.inc"
/* Test that we can round-trip from int->varint->int. */
static void test_varint_for_num(upb_decoderet (*decoder)(const char*),
uint64_t num) {
char buf[16];
size_t bytes;
upb_decoderet r;
memset(buf, 0xff, sizeof(buf));
bytes = upb_vencode64(num, buf);
if (num <= UINT32_MAX) {
uint64_t encoded = upb_vencode32((uint32_t)num);
char buf2[16];
upb_decoderet r;
memset(buf2, 0, sizeof(buf2));
memcpy(&buf2, &encoded, 8);
#ifdef UPB_BIG_ENDIAN
char swap[8];
swap[0] = buf2[7];
swap[1] = buf2[6];
swap[2] = buf2[5];
swap[3] = buf2[4];
swap[4] = buf2[3];
swap[5] = buf2[2];
swap[6] = buf2[1];
swap[7] = buf2[0];
buf2[0] = swap[0];
buf2[1] = swap[1];
buf2[2] = swap[2];
buf2[3] = swap[3];
buf2[4] = swap[4];
buf2[5] = swap[5];
buf2[6] = swap[6];
buf2[7] = swap[7];
#endif
r = decoder(buf2);
ASSERT(r.val == num);
ASSERT(r.p == buf2 + upb_value_size(encoded));
ASSERT(upb_zzenc_32(upb_zzdec_32((uint32_t)num)) == num);
}
r = decoder(buf);
ASSERT(r.val == num);
ASSERT(r.p == buf + bytes);
ASSERT(upb_zzenc_64(upb_zzdec_64(num)) == num);
}
/* Making up for the lack of 64-bit constants in C89. */
static uint64_t make_u64(uint32_t high, uint32_t low) {
uint64_t ret = high;
ret = (ret << 32) | low;
return ret;
}
static void test_varint_decoder(upb_decoderet (*decoder)(const char*)) {
#define TEST(bytes, expected_val) {\
size_t n = sizeof(bytes) - 1; /* for NULL */ \
char buf[UPB_PB_VARINT_MAX_LEN]; \
upb_decoderet r; \
memset(buf, 0xff, sizeof(buf)); \
memcpy(buf, bytes, n); \
r = decoder(buf); \
ASSERT(r.val == expected_val); \
ASSERT(r.p == buf + n); \
}
uint64_t num;
char twelvebyte[16] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1};
const char *twelvebyte_buf = twelvebyte;
/* A varint that terminates before hitting the end of the provided buffer,
* but in too many bytes (11 instead of 10). */
upb_decoderet r = decoder(twelvebyte_buf);
ASSERT(r.p == NULL);
TEST("\x00", 0UL);
TEST("\x01", 1UL);
TEST("\x81\x14", 0xa01UL);
TEST("\x81\x03", 0x181UL);
TEST("\x81\x83\x07", 0x1c181UL);
TEST("\x81\x83\x87\x0f", 0x1e1c181UL);
TEST("\x81\x83\x87\x8f\x1f", make_u64(0x1, 0xf1e1c181UL));
TEST("\x81\x83\x87\x8f\x9f\x3f", make_u64(0x1f9, 0xf1e1c181UL));
TEST("\x81\x83\x87\x8f\x9f\xbf\x7f", make_u64(0x1fdf9, 0xf1e1c181UL));
TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x01", make_u64(0x3fdf9, 0xf1e1c181UL));
TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x81\x03",
make_u64(0x303fdf9, 0xf1e1c181UL));
TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x81\x83\x07",
make_u64(0x8303fdf9, 0xf1e1c181UL));
#undef TEST
for (num = 5; num * 1.5 < UINT64_MAX; num *= 1.5) {
test_varint_for_num(decoder, num);
}
test_varint_for_num(decoder, 0);
}
#define TEST_VARINT_DECODER(decoder) \
/* Create non-inline versions for convenient inspection of assembly language \
* output. */ \
upb_decoderet _upb_vdecode_ ## decoder(const char *p) { \
return upb_vdecode_ ## decoder(p); \
} \
void test_ ## decoder(void) { \
test_varint_decoder(&_upb_vdecode_ ## decoder); \
} \
TEST_VARINT_DECODER(check2_branch32)
TEST_VARINT_DECODER(check2_branch64)
int run_tests(int argc, char *argv[]) {
UPB_UNUSED(argc);
UPB_UNUSED(argv);
test_check2_branch32();
test_check2_branch64();
return 0;
}

@ -952,6 +952,31 @@ void TestArena() {
}
}
void TestInlinedArena() {
int n = 100000;
struct Decrementer {
Decrementer(int* _p) : p(_p) {}
~Decrementer() { (*p)--; }
int* p;
};
{
upb::InlinedArena<1024> arena;
for (int i = 0; i < n; i++) {
arena.Own(new Decrementer(&n));
// Intersperse allocation and ensure we can write to it.
int* val = static_cast<int*>(upb_arena_malloc(arena.ptr(), sizeof(int)));
*val = i;
}
// Test a large allocation.
upb_arena_malloc(arena.ptr(), 1000000);
}
ASSERT(n == 0);
}
extern "C" {
int run_tests() {

@ -24,12 +24,13 @@ const int32_t test_int32_2 = -20;
const int32_t test_int32_3 = 30;
const int32_t test_int32_4 = -40;
static void test_scalars() {
static void test_scalars(void) {
upb_arena *arena = upb_arena_new();
protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
protobuf_test_messages_proto3_TestAllTypesProto3 *msg2;
upb_strview serialized;
upb_strview val;
protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_int32(msg, 10);
protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_int64(msg, 20);
@ -56,14 +57,35 @@ static void test_scalars() {
ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint64(
msg2) == 40);
ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_float(
msg2) == 50.5);
msg2) - 50.5 < 0.01);
ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_double(
msg2) == 60.6);
msg2) - 60.6 < 0.01);
ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_bool(
msg2) == 1);
ASSERT(upb_strview_eql(
protobuf_test_messages_proto3_TestAllTypesProto3_optional_string(msg2),
test_str_view));
val = protobuf_test_messages_proto3_TestAllTypesProto3_optional_string(msg2);
ASSERT(upb_strview_eql(val, test_str_view));
upb_arena_free(arena);
}
static void test_utf8(void) {
const char invalid_utf8[] = "\xff";
const upb_strview invalid_utf8_view = upb_strview_make(invalid_utf8, 1);
upb_arena *arena = upb_arena_new();
upb_strview serialized;
protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
protobuf_test_messages_proto3_TestAllTypesProto3 *msg2;
protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_string(
msg, invalid_utf8_view);
serialized.data = protobuf_test_messages_proto3_TestAllTypesProto3_serialize(
msg, arena, &serialized.size);
msg2 = protobuf_test_messages_proto3_TestAllTypesProto3_parse(
serialized.data, serialized.size, arena);
ASSERT(msg2 == NULL);
upb_arena_free(arena);
}
@ -117,7 +139,7 @@ static void check_string_map_one_entry(
ASSERT(!const_ent);
}
static void test_string_double_map() {
static void test_string_double_map(void) {
upb_arena *arena = upb_arena_new();
upb_strview serialized;
upb_test_MapTest *msg = upb_test_MapTest_new(arena);
@ -141,7 +163,7 @@ static void test_string_double_map() {
upb_arena_free(arena);
}
static void test_string_map() {
static void test_string_map(void) {
upb_arena *arena = upb_arena_new();
protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
@ -259,7 +281,7 @@ static void check_int32_map_one_entry(
ASSERT(!const_ent);
}
static void test_int32_map() {
static void test_int32_map(void) {
upb_arena *arena = upb_arena_new();
protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
@ -328,7 +350,7 @@ static void test_int32_map() {
upb_arena_free(arena);
}
void test_repeated() {
void test_repeated(void) {
upb_arena *arena = upb_arena_new();
protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
@ -347,7 +369,7 @@ void test_repeated() {
upb_arena_free(arena);
}
void test_null_decode_buf() {
void test_null_decode_buf(void) {
upb_arena *arena = upb_arena_new();
protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
protobuf_test_messages_proto3_TestAllTypesProto3_parse(NULL, 0, arena);
@ -359,7 +381,7 @@ void test_null_decode_buf() {
upb_arena_free(arena);
}
void test_status_truncation() {
void test_status_truncation(void) {
int i, j;
upb_status status;
upb_status status2;
@ -390,6 +412,7 @@ void test_status_truncation() {
int run_tests(int argc, char *argv[]) {
test_scalars();
test_utf8();
test_string_map();
test_string_double_map();
test_int32_map();

@ -618,6 +618,16 @@ void test_delete() {
upb_inttable_uninit(&t);
}
void test_init() {
for (int i = 0; i < 2048; i++) {
/* Tests that the size calculations in init() (lg2 size for target load)
* work for all expected sizes. */
upb_strtable t;
upb_strtable_init2(&t, UPB_CTYPE_BOOL, i, &upb_alloc_global);
upb_strtable_uninit(&t);
}
}
extern "C" {
int run_tests(int argc, char *argv[]) {

@ -0,0 +1,17 @@
licenses(["unencumbered"])
exports_files(["LICENSE"])
cc_library(
name = "wyhash",
hdrs = ["wyhash.h"],
visibility = ["//:__pkg__"],
)
filegroup(
name = "cmake_files",
srcs = glob([
"**/*",
]),
visibility = ["//cmake:__pkg__"],
)

@ -0,0 +1,25 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>

@ -0,0 +1,145 @@
/* Copyright 2020 王一 Wang Yi <godspeed_china@yeah.net>
This is free and unencumbered software released into the public domain. http://unlicense.org/
See github.com/wangyi-fudan/wyhash/ LICENSE
*/
#ifndef wyhash_final_version
#define wyhash_final_version
//defines that change behavior
#ifndef WYHASH_CONDOM
#define WYHASH_CONDOM 1 //0: read 8 bytes before and after boundaries, dangerous but fastest. 1: normal valid behavior 2: extra protection against entropy loss (probability=2^-63), aka. "blind multiplication"
#endif
#define WYHASH_32BIT_MUM 0 //faster on 32 bit system
//includes
#include <stdint.h>
#include <string.h>
#if defined(_MSC_VER) && defined(_M_X64)
#include <intrin.h>
#pragma intrinsic(_umul128)
#endif
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
#define _likely_(x) __builtin_expect(x,1)
#define _unlikely_(x) __builtin_expect(x,0)
#else
#define _likely_(x) (x)
#define _unlikely_(x) (x)
#endif
//mum function
static inline uint64_t _wyrot(uint64_t x) { return (x>>32)|(x<<32); }
static inline void _wymum(uint64_t *A, uint64_t *B){
#if(WYHASH_32BIT_MUM)
uint64_t hh=(*A>>32)*(*B>>32), hl=(*A>>32)*(unsigned)*B, lh=(unsigned)*A*(*B>>32), ll=(uint64_t)(unsigned)*A*(unsigned)*B;
#if(WYHASH_CONDOM>1)
*A^=_wyrot(hl)^hh; *B^=_wyrot(lh)^ll;
#else
*A=_wyrot(hl)^hh; *B=_wyrot(lh)^ll;
#endif
#elif defined(__SIZEOF_INT128__)
__uint128_t r=*A; r*=*B;
#if(WYHASH_CONDOM>1)
*A^=(uint64_t)r; *B^=(uint64_t)(r>>64);
#else
*A=(uint64_t)r; *B=(uint64_t)(r>>64);
#endif
#elif defined(_MSC_VER) && defined(_M_X64)
#if(WYHASH_CONDOM>1)
uint64_t a, b;
a=_umul128(*A,*B,&b);
*A^=a; *B^=b;
#else
*A=_umul128(*A,*B,B);
#endif
#else
uint64_t ha=*A>>32, hb=*B>>32, la=(uint32_t)*A, lb=(uint32_t)*B, hi, lo;
uint64_t rh=ha*hb, rm0=ha*lb, rm1=hb*la, rl=la*lb, t=rl+(rm0<<32), c=t<rl;
lo=t+(rm1<<32); c+=lo<t; hi=rh+(rm0>>32)+(rm1>>32)+c;
#if(WYHASH_CONDOM>1)
*A^=lo; *B^=hi;
#else
*A=lo; *B=hi;
#endif
#endif
}
static inline uint64_t _wymix(uint64_t A, uint64_t B){ _wymum(&A,&B); return A^B; }
//read functions
#ifndef WYHASH_LITTLE_ENDIAN
#if defined(_WIN32) || defined(__LITTLE_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
#define WYHASH_LITTLE_ENDIAN 1
#elif defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#define WYHASH_LITTLE_ENDIAN 0
#endif
#endif
#if (WYHASH_LITTLE_ENDIAN)
static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return v;}
static inline uint64_t _wyr4(const uint8_t *p) { unsigned v; memcpy(&v, p, 4); return v;}
#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return __builtin_bswap64(v);}
static inline uint64_t _wyr4(const uint8_t *p) { unsigned v; memcpy(&v, p, 4); return __builtin_bswap32(v);}
#elif defined(_MSC_VER)
static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return _byteswap_uint64(v);}
static inline uint64_t _wyr4(const uint8_t *p) { unsigned v; memcpy(&v, p, 4); return _byteswap_ulong(v);}
#endif
static inline uint64_t _wyr3(const uint8_t *p, unsigned k) { return (((uint64_t)p[0])<<16)|(((uint64_t)p[k>>1])<<8)|p[k-1];}
//wyhash function
static inline uint64_t _wyfinish16(const uint8_t *p, uint64_t len, uint64_t seed, const uint64_t *secret, uint64_t i){
#if(WYHASH_CONDOM>0)
uint64_t a, b;
if(_likely_(i<=8)){
if(_likely_(i>=4)){ a=_wyr4(p); b=_wyr4(p+i-4); }
else if (_likely_(i)){ a=_wyr3(p,i); b=0; }
else a=b=0;
}
else{ a=_wyr8(p); b=_wyr8(p+i-8); }
return _wymix(secret[1]^len,_wymix(a^secret[1], b^seed));
#else
#define oneshot_shift ((i<8)*((8-i)<<3))
return _wymix(secret[1]^len,_wymix((_wyr8(p)<<oneshot_shift)^secret[1],(_wyr8(p+i-8)>>oneshot_shift)^seed));
#endif
}
static inline uint64_t _wyfinish(const uint8_t *p, uint64_t len, uint64_t seed, const uint64_t *secret, uint64_t i){
if(_likely_(i<=16)) return _wyfinish16(p,len,seed,secret,i);
return _wyfinish(p+16,len,_wymix(_wyr8(p)^secret[1],_wyr8(p+8)^seed),secret,i-16);
}
static inline uint64_t wyhash(const void *key, uint64_t len, uint64_t seed, const uint64_t *secret){
const uint8_t *p=(const uint8_t *)key;
uint64_t i=len; seed^=*secret;
if(_unlikely_(i>64)){
uint64_t see1=seed;
do{
seed=_wymix(_wyr8(p)^secret[1],_wyr8(p+8)^seed)^_wymix(_wyr8(p+16)^secret[2],_wyr8(p+24)^seed);
see1=_wymix(_wyr8(p+32)^secret[3],_wyr8(p+40)^see1)^_wymix(_wyr8(p+48)^secret[4],_wyr8(p+56)^see1);
p+=64; i-=64;
}while(i>64);
seed^=see1;
}
return _wyfinish(p,len,seed,secret,i);
}
//utility functions
const uint64_t _wyp[5] = {0xa0761d6478bd642full, 0xe7037ed1a0b428dbull, 0x8ebc6af09c88c6e3ull, 0x589965cc75374cc3ull, 0x1d8e4e27c47d124full};
static inline uint64_t wyhash64(uint64_t A, uint64_t B){ A^=_wyp[0]; B^=_wyp[1]; _wymum(&A,&B); return _wymix(A^_wyp[0],B^_wyp[1]);}
static inline uint64_t wyrand(uint64_t *seed){ *seed+=_wyp[0]; return _wymix(*seed,*seed^_wyp[1]);}
static inline double wy2u01(uint64_t r){ const double _wynorm=1.0/(1ull<<52); return (r>>12)*_wynorm;}
static inline double wy2gau(uint64_t r){ const double _wynorm=1.0/(1ull<<20); return ((r&0x1fffff)+((r>>21)&0x1fffff)+((r>>42)&0x1fffff))*_wynorm-3.0;}
static inline uint64_t wy2u0k(uint64_t r, uint64_t k){ _wymum(&r,&k); return k; }
static inline void make_secret(uint64_t seed, uint64_t *secret){
uint8_t c[] = {15, 23, 27, 29, 30, 39, 43, 45, 46, 51, 53, 54, 57, 58, 60, 71, 75, 77, 78, 83, 85, 86, 89, 90, 92, 99, 101, 102, 105, 106, 108, 113, 114, 116, 120, 135, 139, 141, 142, 147, 149, 150, 153, 154, 156, 163, 165, 166, 169, 170, 172, 177, 178, 180, 184, 195, 197, 198, 201, 202, 204, 209, 210, 212, 216, 225, 226, 228, 232, 240 };
for(size_t i=0;i<5;i++){
uint8_t ok;
do{
ok=1; secret[i]=0;
for(size_t j=0;j<64;j+=8) secret[i]|=((uint64_t)c[wyrand(&seed)%sizeof(c)])<<j;
if(secret[i]%2==0){ ok=0; continue; }
for(size_t j=0;j<i;j++)
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
if(__builtin_popcountll(secret[j]^secret[i])!=32){ ok=0; break; }
#elif defined(_MSC_VER) && defined(_M_X64)
if(_mm_popcnt_u64(secret[j]^secret[i])!=32){ ok=0; break; }
#endif
if(!ok)continue;
for(uint64_t j=3;j<0x100000000ull;j+=2) if(secret[i]%j==0){ ok=0; break; }
}while(!ok);
}
}
#endif

@ -0,0 +1,41 @@
load(
"//bazel:build_defs.bzl",
"UPB_DEFAULT_COPTS",
"UPB_DEFAULT_CPPOPTS",
)
licenses(["notice"])
cc_library(
name = "lupb",
srcs = [
"def.c",
"msg.c",
"upb.c",
],
hdrs = [
"upb.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
"//:json",
"//:reflection",
"//:textformat",
"//:upb",
"@lua//:liblua",
],
)
cc_binary(
name = "protoc-gen-lua",
srcs = ["upbc.cc"],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//visibility:public"],
deps = [
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protoc_lib",
],
)
exports_files(["upb.lua"])

@ -53,12 +53,12 @@ static void lupb_wrapper_pushwrapper(lua_State *L, int narg, const void *def,
/* lupb_msgdef_pushsubmsgdef()
*
* Pops the msgdef wrapper at the top of the stack and replaces it with a msgdef
* wrapper for field |f| of this msgdef.
* wrapper for field |f| of this msgdef (submsg may not be direct, for example it
* may be the submessage of the map value).
*/
void lupb_msgdef_pushsubmsgdef(lua_State *L, const upb_fielddef *f) {
assert(luaL_testudata(L, -1, LUPB_MSGDEF));
const upb_msgdef *m = upb_fielddef_msgsubdef(f);
assert(upb_fielddef_containingtype(f) == lupb_msgdef_check(L, -1));
assert(m);
lupb_wrapper_pushwrapper(L, -1, m, LUPB_MSGDEF);
lua_replace(L, -2); /* Replace msgdef with submsgdef. */
}
@ -159,10 +159,11 @@ static int lupb_fielddef_name(lua_State *L) {
static int lupb_fielddef_number(lua_State *L) {
const upb_fielddef *f = lupb_fielddef_check(L, 1);
int32_t num = upb_fielddef_number(f);
if (num)
if (num) {
lua_pushinteger(L, num);
else
} else {
lua_pushnil(L);
}
return 1;
}
@ -224,58 +225,72 @@ static int lupb_oneofdef_containingtype(lua_State *L) {
return 1;
}
/* lupb_oneofdef_field()
*
* Handles:
* oneof.field(field_number)
* oneof.field(field_name)
*/
static int lupb_oneofdef_field(lua_State *L) {
const upb_oneofdef *o = lupb_oneofdef_check(L, 1);
const upb_fielddef *f;
int32_t idx = lupb_checkint32(L, 2);
int count = upb_oneofdef_fieldcount(o);
switch (lua_type(L, 2)) {
case LUA_TNUMBER:
f = upb_oneofdef_itof(o, lua_tointeger(L, 2));
break;
case LUA_TSTRING:
f = upb_oneofdef_ntofz(o, lua_tostring(L, 2));
break;
default: {
const char *msg = lua_pushfstring(L, "number or string expected, got %s",
luaL_typename(L, 2));
return luaL_argerror(L, 2, msg);
}
if (idx < 0 || idx >= count) {
const char *msg = lua_pushfstring(L, "index %d exceeds field count %d",
idx, count);
return luaL_argerror(L, 2, msg);
}
lupb_wrapper_pushwrapper(L, 1, f, LUPB_FIELDDEF);
lupb_wrapper_pushwrapper(L, 1, upb_oneofdef_field(o, idx), LUPB_FIELDDEF);
return 1;
}
static int lupb_oneofiter_next(lua_State *L) {
upb_oneof_iter *i = lua_touserdata(L, lua_upvalueindex(1));
const upb_oneofdef *o = lupb_oneofdef_check(L, lua_upvalueindex(1));
int *index = lua_touserdata(L, lua_upvalueindex(2));
const upb_fielddef *f;
if (upb_oneof_done(i)) return 0;
f = upb_oneof_iter_field(i);
upb_oneof_next(i);
lupb_symtab_pushwrapper(L, lua_upvalueindex(2), f, LUPB_FIELDDEF);
if (*index == upb_oneofdef_fieldcount(o)) return 0;
f = upb_oneofdef_field(o, (*index)++);
lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), f, LUPB_FIELDDEF);
return 1;
}
static int lupb_oneofdef_fields(lua_State *L) {
const upb_oneofdef *o = lupb_oneofdef_check(L, 1);
upb_oneof_iter *i = lua_newuserdata(L, sizeof(upb_oneof_iter));
lupb_wrapper_pushsymtab(L, 1);
upb_oneof_begin(i, o);
int *index = lua_newuserdata(L, sizeof(int));
lupb_oneofdef_check(L, 1);
*index = 0;
/* Closure upvalues are: iter, symtab. */
/* Closure upvalues are: oneofdef, index. */
lua_pushcclosure(L, &lupb_oneofiter_next, 2);
return 1;
}
static int lupb_oneofdef_len(lua_State *L) {
const upb_oneofdef *o = lupb_oneofdef_check(L, 1);
lua_pushinteger(L, upb_oneofdef_numfields(o));
lua_pushinteger(L, upb_oneofdef_fieldcount(o));
return 1;
}
/* lupb_oneofdef_lookupfield()
*
* Handles:
* oneof.lookup_field(field_number)
* oneof.lookup_field(field_name)
*/
static int lupb_oneofdef_lookupfield(lua_State *L) {
const upb_oneofdef *o = lupb_oneofdef_check(L, 1);
const upb_fielddef *f;
switch (lua_type(L, 2)) {
case LUA_TNUMBER:
f = upb_oneofdef_itof(o, lua_tointeger(L, 2));
break;
case LUA_TSTRING:
f = upb_oneofdef_ntofz(o, lua_tostring(L, 2));
break;
default: {
const char *msg = lua_pushfstring(L, "number or string expected, got %s",
luaL_typename(L, 2));
return luaL_argerror(L, 2, msg);
}
}
lupb_wrapper_pushwrapper(L, 1, f, LUPB_FIELDDEF);
return 1;
}
@ -289,6 +304,7 @@ static const struct luaL_Reg lupb_oneofdef_m[] = {
{"containing_type", lupb_oneofdef_containingtype},
{"field", lupb_oneofdef_field},
{"fields", lupb_oneofdef_fields},
{"lookup_field", lupb_oneofdef_lookupfield},
{"name", lupb_oneofdef_name},
{NULL, NULL}
};
@ -309,12 +325,38 @@ const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg) {
return lupb_wrapper_check(L, narg, LUPB_MSGDEF);
}
static int lupb_msgdef_len(lua_State *L) {
static int lupb_msgdef_fieldcount(lua_State *L) {
const upb_msgdef *m = lupb_msgdef_check(L, 1);
lua_pushinteger(L, upb_msgdef_fieldcount(m));
return 1;
}
static int lupb_msgdef_oneofcount(lua_State *L) {
const upb_msgdef *m = lupb_msgdef_check(L, 1);
lua_pushinteger(L, upb_msgdef_numfields(m));
lua_pushinteger(L, upb_msgdef_oneofcount(m));
return 1;
}
static bool lupb_msgdef_pushnested(lua_State *L, int msgdef, int name) {
const upb_msgdef *m = lupb_msgdef_check(L, msgdef);
lupb_wrapper_pushsymtab(L, msgdef);
upb_symtab *symtab = lupb_symtab_check(L, -1);
lua_pop(L, 1);
/* Construct full package.Message.SubMessage name. */
lua_pushstring(L, upb_msgdef_fullname(m));
lua_pushstring(L, ".");
lua_pushvalue(L, name);
lua_concat(L, 3);
const char *nested_name = lua_tostring(L, -1);
/* Try lookup. */
const upb_msgdef *nested = upb_symtab_lookupmsg(symtab, nested_name);
if (!nested) return false;
lupb_wrapper_pushwrapper(L, msgdef, nested, LUPB_MSGDEF);
return true;
}
/* lupb_msgdef_field()
*
* Handles:
@ -376,23 +418,21 @@ static int lupb_msgdef_name(lua_State *L) {
}
static int lupb_msgfielditer_next(lua_State *L) {
upb_msg_field_iter *i = lua_touserdata(L, lua_upvalueindex(1));
const upb_msgdef *m = lupb_msgdef_check(L, lua_upvalueindex(1));
int *index = lua_touserdata(L, lua_upvalueindex(2));
const upb_fielddef *f;
if (upb_msg_field_done(i)) return 0;
f = upb_msg_iter_field(i);
lupb_symtab_pushwrapper(L, lua_upvalueindex(2), f, LUPB_FIELDDEF);
upb_msg_field_next(i);
if (*index == upb_msgdef_fieldcount(m)) return 0;
f = upb_msgdef_field(m, (*index)++);
lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), f, LUPB_FIELDDEF);
return 1;
}
static int lupb_msgdef_fields(lua_State *L) {
const upb_msgdef *m = lupb_msgdef_check(L, 1);
upb_msg_field_iter *i = lua_newuserdata(L, sizeof(upb_msg_field_iter));
lupb_wrapper_pushsymtab(L, 1);
upb_msg_field_begin(i, m);
int *index = lua_newuserdata(L, sizeof(int));
lupb_msgdef_check(L, 1);
*index = 0;
/* Closure upvalues are: iter, symtab. */
/* Closure upvalues are: msgdef, index. */
lua_pushcclosure(L, &lupb_msgfielditer_next, 2);
return 1;
}
@ -410,23 +450,29 @@ static int lupb_msgdef_fullname(lua_State *L) {
return 1;
}
static int lupb_msgdef_index(lua_State *L) {
if (!lupb_msgdef_pushnested(L, 1, 2)) {
luaL_error(L, "No such nested message");
}
return 1;
}
static int lupb_msgoneofiter_next(lua_State *L) {
upb_msg_oneof_iter *i = lua_touserdata(L, lua_upvalueindex(1));
const upb_msgdef *m = lupb_msgdef_check(L, lua_upvalueindex(1));
int *index = lua_touserdata(L, lua_upvalueindex(2));
const upb_oneofdef *o;
if (upb_msg_oneof_done(i)) return 0;
o = upb_msg_iter_oneof(i);
upb_msg_oneof_next(i);
lupb_symtab_pushwrapper(L, lua_upvalueindex(2), o, LUPB_ONEOFDEF);
if (*index == upb_msgdef_oneofcount(m)) return 0;
o = upb_msgdef_oneof(m, (*index)++);
lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), o, LUPB_ONEOFDEF);
return 1;
}
static int lupb_msgdef_oneofs(lua_State *L) {
const upb_msgdef *m = lupb_msgdef_check(L, 1);
upb_msg_oneof_iter *i = lua_newuserdata(L, sizeof(upb_msg_oneof_iter));
lupb_wrapper_pushsymtab(L, 1);
upb_msg_oneof_begin(i, m);
int *index = lua_newuserdata(L, sizeof(int));
lupb_msgdef_check(L, 1);
*index = 0;
/* Closure upvalues are: iter, symtab. */
/* Closure upvalues are: msgdef, index. */
lua_pushcclosure(L, &lupb_msgoneofiter_next, 2);
return 1;
}
@ -451,8 +497,9 @@ static int lupb_msgdef_tostring(lua_State *L) {
}
static const struct luaL_Reg lupb_msgdef_mm[] = {
{"__call", lupb_msg_pushnew},
{"__len", lupb_msgdef_len},
{"__call", lupb_msgdef_call},
{"__index", lupb_msgdef_index},
{"__len", lupb_msgdef_fieldcount},
{"__tostring", lupb_msgdef_tostring},
{NULL, NULL}
};
@ -460,10 +507,12 @@ static const struct luaL_Reg lupb_msgdef_mm[] = {
static const struct luaL_Reg lupb_msgdef_m[] = {
{"field", lupb_msgdef_field},
{"fields", lupb_msgdef_fields},
{"field_count", lupb_msgdef_fieldcount},
{"file", lupb_msgdef_file},
{"full_name", lupb_msgdef_fullname},
{"lookup_name", lupb_msgdef_lookupname},
{"name", lupb_msgdef_name},
{"oneof_count", lupb_msgdef_oneofcount},
{"oneofs", lupb_msgdef_oneofs},
{"syntax", lupb_msgdef_syntax},
{"_map_entry", lupb_msgdef_mapentry},
@ -619,6 +668,13 @@ static int lupb_filedef_package(lua_State *L) {
return 1;
}
static int lupb_filedef_symtab(lua_State *L) {
const upb_filedef *f = lupb_filedef_check(L, 1);
const upb_symtab *symtab = upb_filedef_symtab(f);
lupb_wrapper_pushwrapper(L, 1, symtab, LUPB_SYMTAB);
return 1;
}
static int lupb_filedef_syntax(lua_State *L) {
const upb_filedef *f = lupb_filedef_check(L, 1);
lua_pushnumber(L, upb_filedef_syntax(f));
@ -634,6 +690,7 @@ static const struct luaL_Reg lupb_filedef_m[] = {
{"msgcount", lupb_filedef_msgcount},
{"name", lupb_filedef_name},
{"package", lupb_filedef_package},
{"symtab", lupb_filedef_symtab},
{"syntax", lupb_filedef_syntax},
{NULL, NULL}
};
@ -712,6 +769,10 @@ static int lupb_symtab_new(lua_State *L) {
lua_setfield(L, -2, "__mode");
lua_setmetatable(L, -2);
/* Put the symtab itself in the cache metatable. */
lua_pushvalue(L, -2);
lua_rawsetp(L, -2, lsymtab->symtab);
/* Set the cache as our userval. */
lua_setiuservalue(L, -2, LUPB_CACHE_INDEX);
@ -729,7 +790,7 @@ static int lupb_symtab_addfile(lua_State *L) {
size_t len;
upb_symtab *s = lupb_symtab_check(L, 1);
const char *str = luaL_checklstring(L, 2, &len);
upb_arena *arena = lupb_arena_pushnew(L);;
upb_arena *arena = lupb_arena_pushnew(L);
const google_protobuf_FileDescriptorProto *file;
const upb_filedef *file_def;
upb_status status;
@ -755,7 +816,7 @@ static int lupb_symtab_addset(lua_State *L) {
google_protobuf_FileDescriptorSet *set;
upb_symtab *s = lupb_symtab_check(L, 1);
const char *str = luaL_checklstring(L, 2, &len);
upb_arena *arena = lupb_arena_pushnew(L);;
upb_arena *arena = lupb_arena_pushnew(L);
upb_status status;
upb_status_clear(&status);

@ -1,4 +1,3 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
# Generic support code #########################################################
@ -12,6 +11,7 @@ def _get_real_short_path(file):
if short_path.startswith("../"):
second_slash = short_path.index("/", 3)
short_path = short_path[second_slash + 1:]
# Sometimes it has another few prefixes like:
# _virtual_imports/any_proto/google/protobuf/any.proto
# We want just google/protobuf/any.proto.
@ -64,9 +64,10 @@ def _lua_proto_rule_impl(ctx):
files = dep[_LuaFiles].files
return [
DefaultInfo(
files = files,
data_runfiles = ctx.runfiles(files = files.to_list())),
]
files = files,
data_runfiles = ctx.runfiles(files = files.to_list()),
),
]
def _lua_proto_library_aspect_impl(target, ctx):
proto_info = target[ProtoInfo]
@ -82,7 +83,7 @@ _lua_proto_library_aspect = aspect(
"_upbc": attr.label(
executable = True,
cfg = "host",
default = "//:protoc-gen-lua",
default = "//upb/bindings/lua:protoc-gen-lua",
),
"_protoc": attr.label(
executable = True,

@ -12,11 +12,12 @@
#include "lauxlib.h"
#include "upb/bindings/lua/upb.h"
#include "upb/json_decode.h"
#include "upb/json_encode.h"
#include "upb/port_def.inc"
#include "upb/reflection.h"
#include "upb/text_encode.h"
#include "upb/port_def.inc"
/*
* Message/Map/Array objects. These objects form a directed graph: a message
* can contain submessages, arrays, and maps, which can then point to other
@ -187,6 +188,13 @@ static void lupb_arena_fuse(lua_State *L, int to, int from) {
upb_arena_fuse(to_arena, from_arena);
}
static void lupb_arena_fuseobjs(lua_State *L, int to, int from) {
lua_getiuservalue(L, to, LUPB_ARENA_INDEX);
lua_getiuservalue(L, from, LUPB_ARENA_INDEX);
lupb_arena_fuse(L, lua_absindex(L, -2), lua_absindex(L, -1));
lua_pop(L, 2);
}
static int lupb_arena_gc(lua_State *L) {
upb_arena *a = lupb_arena_check(L, 1);
upb_arena_free(a);
@ -398,6 +406,10 @@ static int lupb_array_newindex(lua_State *L) {
upb_array_set(larray->arr, n, msgval);
}
if (larray->type == UPB_TYPE_MESSAGE) {
lupb_arena_fuseobjs(L, 1, 3);
}
return 0; /* 1 for chained assignments? */
}
@ -535,6 +547,9 @@ static int lupb_map_newindex(lua_State *L) {
} else {
upb_msgval val = lupb_tomsgval(L, lmap->value_type, 3, 1, LUPB_COPY);
upb_map_set(map, key, val, lupb_arenaget(L, 1));
if (lmap->value_type == UPB_TYPE_MESSAGE) {
lupb_arena_fuseobjs(L, 1, 3);
}
}
return 0;
@ -564,8 +579,8 @@ static int lupb_mapiter_next(lua_State *L) {
* pairs(map)
*/
static int lupb_map_pairs(lua_State *L) {
lupb_map_check(L, 1);
size_t *iter = lua_newuserdata(L, sizeof(*iter));
lupb_map_check(L, 1);
*iter = UPB_MAP_BEGIN;
lua_pushvalue(L, 1);
@ -600,24 +615,43 @@ static upb_msg *lupb_msg_check(lua_State *L, int narg) {
return msg->msg;
}
static const upb_fielddef *lupb_msg_checkfield(lua_State *L, int msg,
int field) {
static const upb_msgdef *lupb_msg_getmsgdef(lua_State *L, int msg) {
lua_getiuservalue(L, msg, LUPB_MSGDEF_INDEX);
const upb_msgdef *m = lupb_msgdef_check(L, -1);
lua_pop(L, 1);
return m;
}
static const upb_fielddef *lupb_msg_tofield(lua_State *L, int msg, int field) {
size_t len;
const char *fieldname = luaL_checklstring(L, field, &len);
const upb_msgdef *m;
const upb_fielddef *f;
const upb_msgdef *m = lupb_msg_getmsgdef(L, msg);
return upb_msgdef_ntof(m, fieldname, len);
}
lua_getiuservalue(L, msg, LUPB_MSGDEF_INDEX);
m = lupb_msgdef_check(L, -1);
f = upb_msgdef_ntof(m, fieldname, len);
static const upb_fielddef *lupb_msg_checkfield(lua_State *L, int msg,
int field) {
const upb_fielddef *f = lupb_msg_tofield(L, msg, field);
if (f == NULL) {
luaL_error(L, "no such field '%s'", fieldname);
luaL_error(L, "no such field '%s'", lua_tostring(L, field));
}
lua_pop(L, 1);
return f;
}
upb_msg *lupb_msg_pushnew(lua_State *L, int narg) {
const upb_msgdef *m = lupb_msgdef_check(L, narg);
lupb_msg *lmsg = lupb_newuserdata(L, sizeof(lupb_msg), 2, LUPB_MSG);
upb_arena *arena = lupb_arena_pushnew(L);
lua_setiuservalue(L, -2, LUPB_ARENA_INDEX);
lua_pushvalue(L, 1);
lua_setiuservalue(L, -2, LUPB_MSGDEF_INDEX);
lmsg->msg = upb_msg_new(m, arena);
lupb_cacheset(L, lmsg->msg);
return lmsg->msg;
}
/**
* lupb_msg_newmsgwrapper()
*
@ -707,28 +741,19 @@ static void lupb_msg_typechecksubmsg(lua_State *L, int narg, int msgarg,
/* lupb_msg Public API */
/**
* lupb_msg_pushnew
* lupb_msgdef_call
*
* Handles:
* new_msg = MessageClass()
* new_msg = MessageClass{foo = "bar", baz = 3, quux = {foo = 3}}
*/
int lupb_msg_pushnew(lua_State *L) {
int argcount = lua_gettop(L);
const upb_msgdef *m = lupb_msgdef_check(L, 1);
lupb_msg *lmsg = lupb_newuserdata(L, sizeof(lupb_msg), 2, LUPB_MSG);
upb_arena *arena = lupb_arena_pushnew(L);
int lupb_msgdef_call(lua_State *L) {
int arg_count = lua_gettop(L);
lupb_msg_pushnew(L, 1);
lua_setiuservalue(L, -2, LUPB_ARENA_INDEX);
lua_pushvalue(L, 1);
lua_setiuservalue(L, -2, LUPB_MSGDEF_INDEX);
lmsg->msg = upb_msg_new(m, arena);
lupb_cacheset(L, lmsg->msg);
if (argcount > 1) {
if (arg_count > 1) {
/* Set initial fields from table. */
int msg = lua_gettop(L);
int msg = arg_count + 1;
lua_pushnil(L);
while (lua_next(L, 2) != 0) {
lua_pushvalue(L, -2); /* now stack is key, val, key */
@ -813,10 +838,7 @@ static int lupb_msg_newindex(lua_State *L) {
}
if (merge_arenas) {
lua_getiuservalue(L, 1, LUPB_ARENA_INDEX);
lua_getiuservalue(L, 3, LUPB_ARENA_INDEX);
lupb_arena_fuse(L, lua_absindex(L, -2), lua_absindex(L, -1));
lua_pop(L, 2);
lupb_arena_fuseobjs(L, 1, 3);
}
upb_msg_set(msg, f, msgval, lupb_arenaget(L, 1));
@ -867,6 +889,19 @@ static const struct luaL_Reg lupb_msg_mm[] = {
/* lupb_msg toplevel **********************************************************/
static int lupb_getoptions(lua_State *L, int narg) {
int options = 0;
if (lua_gettop(L) >= narg) {
size_t len = lua_rawlen(L, narg);
for (size_t i = 1; i <= len; i++) {
lua_rawgeti(L, narg, i);
options |= lupb_checkuint32(L, -1);
lua_pop(L, 1);
}
}
return options;
}
/**
* lupb_decode()
*
@ -878,26 +913,16 @@ static int lupb_decode(lua_State *L) {
const upb_msgdef *m = lupb_msgdef_check(L, 1);
const char *pb = lua_tolstring(L, 2, &len);
const upb_msglayout *layout = upb_msgdef_layout(m);
upb_msg *msg = lupb_msg_pushnew(L, 1);
upb_arena *arena = lupb_arenaget(L, -1);
char *buf;
upb_msg *msg;
upb_arena *arena;
bool ok;
/* Create message. */
lua_pushcfunction(L, &lupb_msg_pushnew);
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
msg = lupb_msg_check(L, -1);
lua_getiuservalue(L, -1, LUPB_ARENA_INDEX);
arena = lupb_arena_check(L, -1);
lua_pop(L, 1);
/* Copy input data to arena, message will reference it. */
buf = upb_arena_malloc(arena, len);
memcpy(buf, pb, len);
ok = upb_decode(buf, len, msg, layout, arena);
ok = _upb_decode(buf, len, msg, layout, arena, UPB_DECODE_ALIAS);
if (!ok) {
lua_pushstring(L, "Error decoding protobuf.");
@ -915,16 +940,15 @@ static int lupb_decode(lua_State *L) {
*/
static int lupb_encode(lua_State *L) {
const upb_msg *msg = lupb_msg_check(L, 1);
const upb_msglayout *layout;
upb_arena *arena = lupb_arena_pushnew(L);
const upb_msgdef *m = lupb_msg_getmsgdef(L, 1);
const upb_msglayout *layout = upb_msgdef_layout(m);
int options = lupb_getoptions(L, 2);
upb_arena *arena;
size_t size;
char *result;
lua_getiuservalue(L, 1, LUPB_MSGDEF_INDEX);
layout = upb_msgdef_layout(lupb_msgdef_check(L, -1));
lua_pop(L, 1);
result = upb_encode(msg, (const void*)layout, arena, &size);
arena = lupb_arena_pushnew(L);
result = upb_encode_ex(msg, (const void*)layout, options, arena, &size);
if (!result) {
lua_pushstring(L, "Error encoding protobuf.");
@ -936,11 +960,101 @@ static int lupb_encode(lua_State *L) {
return 1;
}
/**
* lupb_jsondecode()
*
* Handles:
* text_string = upb.json_decode(MessageClass, json_str, {upb.JSONDEC_IGNOREUNKNOWN})
*/
static int lupb_jsondecode(lua_State *L) {
size_t len;
const upb_msgdef *m = lupb_msgdef_check(L, 1);
const char *json = lua_tolstring(L, 2, &len);
int options = lupb_getoptions(L, 3);
upb_msg *msg;
upb_arena *arena;
upb_status status;
msg = lupb_msg_pushnew(L, 1);
arena = lupb_arenaget(L, -1);
upb_status_clear(&status);
upb_json_decode(json, len, msg, m, NULL, options, arena, &status);
lupb_checkstatus(L, &status);
return 1;
}
/**
* lupb_jsonencode()
*
* Handles:
* text_string = upb.json_encode(msg, {upb.JSONENC_EMITDEFAULTS})
*/
static int lupb_jsonencode(lua_State *L) {
upb_msg *msg = lupb_msg_check(L, 1);
const upb_msgdef *m = lupb_msg_getmsgdef(L, 1);
int options = lupb_getoptions(L, 2);
char buf[1024];
size_t size;
upb_status status;
upb_status_clear(&status);
size = upb_json_encode(msg, m, NULL, options, buf, sizeof(buf), &status);
lupb_checkstatus(L, &status);
if (size < sizeof(buf)) {
lua_pushlstring(L, buf, size);
} else {
char *ptr = malloc(size + 1);
upb_json_encode(msg, m, NULL, options, ptr, size + 1, &status);
lupb_checkstatus(L, &status);
lua_pushlstring(L, ptr, size);
free(ptr);
}
return 1;
}
/**
* lupb_textencode()
*
* Handles:
* text_string = upb.text_encode(msg, {upb.TXTENC_SINGLELINE})
*/
static int lupb_textencode(lua_State *L) {
upb_msg *msg = lupb_msg_check(L, 1);
const upb_msgdef *m = lupb_msg_getmsgdef(L, 1);
int options = lupb_getoptions(L, 2);
char buf[1024];
size_t size;
size = upb_text_encode(msg, m, NULL, options, buf, sizeof(buf));
if (size < sizeof(buf)) {
lua_pushlstring(L, buf, size);
} else {
char *ptr = malloc(size + 1);
upb_text_encode(msg, m, NULL, options, ptr, size + 1);
lua_pushlstring(L, ptr, size);
free(ptr);
}
return 1;
}
static void lupb_setfieldi(lua_State *L, const char *field, int i) {
lua_pushinteger(L, i);
lua_setfield(L, -2, field);
}
static const struct luaL_Reg lupb_msg_toplevel_m[] = {
{"Array", lupb_array_new},
{"Map", lupb_map_new},
{"decode", lupb_decode},
{"encode", lupb_encode},
{"json_decode", lupb_jsondecode},
{"json_encode", lupb_jsonencode},
{"text_encode", lupb_textencode},
{NULL, NULL}
};
@ -952,5 +1066,17 @@ void lupb_msg_registertypes(lua_State *L) {
lupb_register_type(L, LUPB_MAP, NULL, lupb_map_mm);
lupb_register_type(L, LUPB_MSG, NULL, lupb_msg_mm);
lupb_setfieldi(L, "TXTENC_SINGLELINE", UPB_TXTENC_SINGLELINE);
lupb_setfieldi(L, "TXTENC_SKIPUNKNOWN", UPB_TXTENC_SKIPUNKNOWN);
lupb_setfieldi(L, "TXTENC_NOSORT", UPB_TXTENC_NOSORT);
lupb_setfieldi(L, "ENCODE_DETERMINISTIC", UPB_ENCODE_DETERMINISTIC);
lupb_setfieldi(L, "ENCODE_SKIPUNKNOWN", UPB_ENCODE_SKIPUNKNOWN);
lupb_setfieldi(L, "JSONENC_EMITDEFAULTS", UPB_JSONENC_EMITDEFAULTS);
lupb_setfieldi(L, "JSONENC_PROTONAMES", UPB_JSONENC_PROTONAMES);
lupb_setfieldi(L, "JSONDEC_IGNOREUNKNOWN", UPB_JSONDEC_IGNOREUNKNOWN);
lupb_cacheinit(L);
}

@ -84,6 +84,24 @@ int lua_getiuservalue(lua_State *L, int index, int n) {
}
#endif
/* We use this function as the __index metamethod when a type has both methods
* and an __index metamethod. */
int lupb_indexmm(lua_State *L) {
/* Look up in __index table (which is a closure param). */
lua_pushvalue(L, 2);
lua_rawget(L, lua_upvalueindex(1));
if (!lua_isnil(L, -1)) {
return 1;
}
/* Not found, chain to user __index metamethod. */
lua_pushvalue(L, lua_upvalueindex(2));
lua_pushvalue(L, 1);
lua_pushvalue(L, 2);
lua_call(L, 2, 1);
return 1;
}
void lupb_register_type(lua_State *L, const char *name, const luaL_Reg *m,
const luaL_Reg *mm) {
luaL_newmetatable(L, name);
@ -93,14 +111,17 @@ void lupb_register_type(lua_State *L, const char *name, const luaL_Reg *m,
}
if (m) {
/* Methods go in the mt's __index method. This implies that you can'
* implement __index and also have methods. */
lua_getfield(L, -1, "__index");
lupb_assert(L, lua_isnil(L, -1));
lua_pop(L, 1);
lua_createtable(L, 0, 0);
lua_createtable(L, 0, 0); /* __index table */
lupb_setfuncs(L, m);
/* Methods go in the mt's __index slot. If the user also specified an
* __index metamethod, use our custom lupb_indexmm() that can check both. */
lua_getfield(L, -2, "__index");
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
} else {
lua_pushcclosure(L, &lupb_indexmm, 2);
}
lua_setfield(L, -2, "__index");
}

@ -75,7 +75,7 @@ void lupb_def_registertypes(lua_State *L);
/** From msg.c. ***************************************************************/
int lupb_msg_pushnew(lua_State *L);
int lupb_msgdef_call(lua_State *L);
upb_arena *lupb_arena_pushnew(lua_State *L);
void lupb_msg_registertypes(lua_State *L);

@ -1,69 +0,0 @@
#ifndef UPB_STDCPP_H_
#define UPB_STDCPP_H_
#include "upb/sink.h"
#include "upb/port_def.inc"
namespace upb {
template <class T>
class FillStringHandler {
public:
static void SetHandler(upb_byteshandler* handler) {
upb_byteshandler_setstartstr(handler, &FillStringHandler::StartString,
NULL);
upb_byteshandler_setstring(handler, &FillStringHandler::StringBuf, NULL);
}
private:
// TODO(haberman): add UpbBind/UpbMakeHandler support to BytesHandler so these
// can be prettier callbacks.
static void* StartString(void *c, const void *hd, size_t size) {
UPB_UNUSED(hd);
UPB_UNUSED(size);
T* str = static_cast<T*>(c);
str->clear();
return c;
}
static size_t StringBuf(void* c, const void* hd, const char* buf, size_t n,
const upb_bufhandle* h) {
UPB_UNUSED(hd);
UPB_UNUSED(h);
T* str = static_cast<T*>(c);
try {
str->append(buf, n);
return n;
} catch (const std::exception&) {
return 0;
}
}
};
class StringSink {
public:
template <class T>
explicit StringSink(T* target) {
// TODO(haberman): we need to avoid rebuilding a new handler every time,
// but with class globals disallowed for google3 C++ this is tricky.
upb_byteshandler_init(&handler_);
FillStringHandler<T>::SetHandler(&handler_);
input_.Reset(&handler_, target);
}
BytesSink input() { return input_; }
private:
upb_byteshandler handler_;
BytesSink input_;
};
} // namespace upb
#include "upb/port_undef.inc"
#endif // UPB_STDCPP_H_

@ -1,33 +1,37 @@
#include "upb/decode.h"
#include <setjmp.h>
#include <string.h>
#include "upb/decode.h"
#include "upb/decode.int.h"
#include "upb/upb.h"
#include "upb/upb.int.h"
/* Must be last. */
#include "upb/port_def.inc"
/* Maps descriptor type -> upb field type. */
static const uint8_t desctype_to_fieldtype[] = {
/* Maps descriptor type -> elem_size_lg2. */
static const uint8_t desctype_to_elem_size_lg2[] = {
-1, /* invalid descriptor type */
UPB_TYPE_DOUBLE, /* DOUBLE */
UPB_TYPE_FLOAT, /* FLOAT */
UPB_TYPE_INT64, /* INT64 */
UPB_TYPE_UINT64, /* UINT64 */
UPB_TYPE_INT32, /* INT32 */
UPB_TYPE_UINT64, /* FIXED64 */
UPB_TYPE_UINT32, /* FIXED32 */
UPB_TYPE_BOOL, /* BOOL */
UPB_TYPE_STRING, /* STRING */
UPB_TYPE_MESSAGE, /* GROUP */
UPB_TYPE_MESSAGE, /* MESSAGE */
UPB_TYPE_BYTES, /* BYTES */
UPB_TYPE_UINT32, /* UINT32 */
UPB_TYPE_ENUM, /* ENUM */
UPB_TYPE_INT32, /* SFIXED32 */
UPB_TYPE_INT64, /* SFIXED64 */
UPB_TYPE_INT32, /* SINT32 */
UPB_TYPE_INT64, /* SINT64 */
3, /* DOUBLE */
2, /* FLOAT */
3, /* INT64 */
3, /* UINT64 */
2, /* INT32 */
3, /* FIXED64 */
2, /* FIXED32 */
0, /* BOOL */
UPB_SIZE(3, 4), /* STRING */
UPB_SIZE(2, 3), /* GROUP */
UPB_SIZE(2, 3), /* MESSAGE */
UPB_SIZE(3, 4), /* BYTES */
2, /* UINT32 */
2, /* ENUM */
2, /* SFIXED32 */
3, /* SFIXED64 */
2, /* SINT32 */
3, /* SINT64 */
};
/* Maps descriptor type -> upb map size. */
@ -134,106 +138,101 @@ static const int8_t delim_ops[37] = {
OP_VARPCK_LG2(3), /* REPEATED SINT64 */
};
/* Data pertaining to the parse. */
typedef struct {
const char *limit; /* End of delimited region or end of buffer. */
upb_arena *arena;
int depth;
uint32_t end_group; /* Set to field number of END_GROUP tag, if any. */
jmp_buf err;
} upb_decstate;
typedef union {
bool bool_val;
uint32_t uint32_val;
uint64_t uint64_val;
upb_strview str_val;
uint32_t size;
} wireval;
static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
const upb_msglayout *layout);
UPB_NORETURN static void decode_err(upb_decstate *d) { longjmp(d->err, 1); }
void decode_verifyutf8(upb_decstate *d, const char *buf, int len) {
static const uint8_t utf8_offset[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0,
};
int i, j;
uint8_t offset;
i = 0;
while (i < len) {
offset = utf8_offset[(uint8_t)buf[i]];
if (offset == 0 || i + offset > len) {
decode_err(d);
}
for (j = i + 1; j < i + offset; j++) {
if ((buf[j] & 0xc0) != 0x80) {
decode_err(d);
}
}
i += offset;
}
if (i != len) decode_err(d);
UPB_NORETURN static void decode_err(upb_decstate *d) { UPB_LONGJMP(d->err, 1); }
const char *fastdecode_err(upb_decstate *d) {
longjmp(d->err, 1);
return NULL;
}
const uint8_t upb_utf8_offsets[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0,
};
static void decode_verifyutf8(upb_decstate *d, const char *buf, int len) {
if (!decode_verifyutf8_inl(buf, len)) decode_err(d);
}
static bool decode_reserve(upb_decstate *d, upb_array *arr, size_t elem) {
bool need_realloc = arr->size - arr->len < elem;
if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, d->arena)) {
if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, &d->arena)) {
decode_err(d);
}
return need_realloc;
}
typedef struct {
const char *ptr;
uint64_t val;
} decode_vret;
UPB_NOINLINE
static const char *decode_longvarint64(upb_decstate *d, const char *ptr,
const char *limit, uint64_t *val) {
uint8_t byte;
int bitpos = 0;
uint64_t out = 0;
do {
if (bitpos >= 70 || ptr == limit) decode_err(d);
byte = *ptr;
out |= (uint64_t)(byte & 0x7F) << bitpos;
ptr++;
bitpos += 7;
} while (byte & 0x80);
*val = out;
return ptr;
static decode_vret decode_longvarint64(const char *ptr, uint64_t val) {
decode_vret ret = {NULL, 0};
uint64_t byte;
int i;
for (i = 1; i < 10; i++) {
byte = (uint8_t)ptr[i];
val += (byte - 1) << (i * 7);
if (!(byte & 0x80)) {
ret.ptr = ptr + i + 1;
ret.val = val;
return ret;
}
}
return ret;
}
UPB_FORCEINLINE
static const char *decode_varint64(upb_decstate *d, const char *ptr,
const char *limit, uint64_t *val) {
if (UPB_LIKELY(ptr < limit && (*ptr & 0x80) == 0)) {
*val = (uint8_t)*ptr;
uint64_t *val) {
uint64_t byte = (uint8_t)*ptr;
if (UPB_LIKELY((byte & 0x80) == 0)) {
*val = byte;
return ptr + 1;
} else {
return decode_longvarint64(d, ptr, limit, val);
decode_vret res = decode_longvarint64(ptr, byte);
if (!res.ptr) decode_err(d);
*val = res.val;
return res.ptr;
}
}
static const char *decode_varint32(upb_decstate *d, const char *ptr,
const char *limit, uint32_t *val) {
uint64_t u64;
ptr = decode_varint64(d, ptr, limit, &u64);
if (u64 > UINT32_MAX) decode_err(d);
*val = (uint32_t)u64;
return ptr;
UPB_FORCEINLINE
static const char *decode_tag(upb_decstate *d, const char *ptr,
uint32_t *val) {
uint64_t byte = (uint8_t)*ptr;
if (UPB_LIKELY((byte & 0x80) == 0)) {
*val = byte;
return ptr + 1;
} else {
const char *start = ptr;
decode_vret res = decode_longvarint64(ptr, byte);
ptr = res.ptr;
*val = res.val;
if (!ptr || *val > UINT32_MAX || ptr - start > 5) decode_err(d);
return ptr;
}
}
static void decode_munge(int type, wireval *val) {
@ -280,33 +279,65 @@ static const upb_msglayout_field *upb_find_field(const upb_msglayout *l,
static upb_msg *decode_newsubmsg(upb_decstate *d, const upb_msglayout *layout,
const upb_msglayout_field *field) {
const upb_msglayout *subl = layout->submsgs[field->submsg_index];
return _upb_msg_new(subl, d->arena);
return _upb_msg_new_inl(subl, &d->arena);
}
UPB_NOINLINE
const char *decode_isdonefallback(upb_decstate *d, const char *ptr,
int overrun) {
ptr = decode_isdonefallback_inl(d, ptr, overrun);
if (ptr == NULL) {
decode_err(d);
}
return ptr;
}
static const char *decode_readstr(upb_decstate *d, const char *ptr, int size,
upb_strview *str) {
if (d->alias) {
str->data = ptr;
} else {
char *data = upb_arena_malloc(&d->arena, size);
if (!data) decode_err(d);
memcpy(data, ptr, size);
str->data = data;
}
str->size = size;
return ptr + size;
}
static void decode_tosubmsg(upb_decstate *d, upb_msg *submsg,
const upb_msglayout *layout,
const upb_msglayout_field *field, upb_strview val) {
UPB_FORCEINLINE
static const char *decode_tosubmsg(upb_decstate *d, const char *ptr,
upb_msg *submsg, const upb_msglayout *layout,
const upb_msglayout_field *field, int size) {
const upb_msglayout *subl = layout->submsgs[field->submsg_index];
const char *saved_limit = d->limit;
int saved_delta = decode_pushlimit(d, ptr, size);
if (--d->depth < 0) decode_err(d);
d->limit = val.data + val.size;
decode_msg(d, val.data, submsg, subl);
d->limit = saved_limit;
if (d->end_group != 0) decode_err(d);
if (!decode_isdone(d, &ptr)) {
ptr = decode_msg(d, ptr, submsg, subl);
}
if (d->end_group != DECODE_NOGROUP) decode_err(d);
decode_poplimit(d, ptr, saved_delta);
d->depth++;
return ptr;
}
UPB_FORCEINLINE
static const char *decode_group(upb_decstate *d, const char *ptr,
upb_msg *submsg, const upb_msglayout *subl,
uint32_t number) {
if (--d->depth < 0) decode_err(d);
if (decode_isdone(d, &ptr)) {
decode_err(d);
}
ptr = decode_msg(d, ptr, submsg, subl);
if (d->end_group != number) decode_err(d);
d->end_group = 0;
d->end_group = DECODE_NOGROUP;
d->depth++;
return ptr;
}
UPB_FORCEINLINE
static const char *decode_togroup(upb_decstate *d, const char *ptr,
upb_msg *submsg, const upb_msglayout *layout,
const upb_msglayout_field *field) {
@ -322,15 +353,15 @@ static const char *decode_toarray(upb_decstate *d, const char *ptr,
upb_array *arr = *arrp;
void *mem;
if (!arr) {
upb_fieldtype_t type = desctype_to_fieldtype[field->descriptortype];
arr = _upb_array_new(d->arena, type);
if (arr) {
decode_reserve(d, arr, 1);
} else {
size_t lg2 = desctype_to_elem_size_lg2[field->descriptortype];
arr = _upb_array_new(&d->arena, 4, lg2);
if (!arr) decode_err(d);
*arrp = arr;
}
decode_reserve(d, arr, 1);
switch (op) {
case OP_SCALAR_LG2(0):
case OP_SCALAR_LG2(2):
@ -341,15 +372,14 @@ static const char *decode_toarray(upb_decstate *d, const char *ptr,
memcpy(mem, &val, 1 << op);
return ptr;
case OP_STRING:
decode_verifyutf8(d, val.str_val.data, val.str_val.size);
decode_verifyutf8(d, ptr, val.size);
/* Fallthrough. */
case OP_BYTES:
case OP_BYTES: {
/* Append bytes. */
mem =
UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(upb_strview), void);
upb_strview *str = (upb_strview*)_upb_array_ptr(arr) + arr->len;
arr->len++;
memcpy(mem, &val, sizeof(upb_strview));
return ptr;
return decode_readstr(d, ptr, val.size, str);
}
case OP_SUBMSG: {
/* Append submessage / group. */
upb_msg *submsg = decode_newsubmsg(d, layout, field);
@ -357,26 +387,25 @@ static const char *decode_toarray(upb_decstate *d, const char *ptr,
submsg;
arr->len++;
if (UPB_UNLIKELY(field->descriptortype == UPB_DTYPE_GROUP)) {
ptr = decode_togroup(d, ptr, submsg, layout, field);
return decode_togroup(d, ptr, submsg, layout, field);
} else {
decode_tosubmsg(d, submsg, layout, field, val.str_val);
return decode_tosubmsg(d, ptr, submsg, layout, field, val.size);
}
return ptr;
}
case OP_FIXPCK_LG2(2):
case OP_FIXPCK_LG2(3): {
/* Fixed packed. */
int lg2 = op - OP_FIXPCK_LG2(0);
int mask = (1 << lg2) - 1;
size_t count = val.str_val.size >> lg2;
if ((val.str_val.size & mask) != 0) {
size_t count = val.size >> lg2;
if ((val.size & mask) != 0) {
decode_err(d); /* Length isn't a round multiple of elem size. */
}
decode_reserve(d, arr, count);
mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
arr->len += count;
memcpy(mem, val.str_val.data, val.str_val.size);
return ptr;
memcpy(mem, ptr, val.size); /* XXX: ptr boundary. */
return ptr + val.size;
}
case OP_VARPCK_LG2(0):
case OP_VARPCK_LG2(2):
@ -384,12 +413,11 @@ static const char *decode_toarray(upb_decstate *d, const char *ptr,
/* Varint packed. */
int lg2 = op - OP_VARPCK_LG2(0);
int scale = 1 << lg2;
const char *ptr = val.str_val.data;
const char *end = ptr + val.str_val.size;
int saved_limit = decode_pushlimit(d, ptr, val.size);
char *out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
while (ptr < end) {
while (!decode_isdone(d, &ptr)) {
wireval elem;
ptr = decode_varint64(d, ptr, end, &elem.uint64_val);
ptr = decode_varint64(d, ptr, &elem.uint64_val);
decode_munge(field->descriptortype, &elem);
if (decode_reserve(d, arr, 1)) {
out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
@ -398,7 +426,7 @@ static const char *decode_toarray(upb_decstate *d, const char *ptr,
memcpy(out, &elem, scale);
out += scale;
}
if (ptr != end) decode_err(d);
decode_poplimit(d, ptr, saved_limit);
return ptr;
}
default:
@ -406,9 +434,9 @@ static const char *decode_toarray(upb_decstate *d, const char *ptr,
}
}
static void decode_tomap(upb_decstate *d, upb_msg *msg,
const upb_msglayout *layout,
const upb_msglayout_field *field, wireval val) {
static const char *decode_tomap(upb_decstate *d, const char *ptr, upb_msg *msg,
const upb_msglayout *layout,
const upb_msglayout_field *field, wireval val) {
upb_map **map_p = UPB_PTR_AT(msg, field->offset, upb_map *);
upb_map *map = *map_p;
upb_map_entry ent;
@ -423,7 +451,7 @@ static void decode_tomap(upb_decstate *d, upb_msg *msg,
char val_size = desctype_to_mapsize[val_field->descriptortype];
UPB_ASSERT(key_field->offset == 0);
UPB_ASSERT(val_field->offset == sizeof(upb_strview));
map = _upb_map_new(d->arena, key_size, val_size);
map = _upb_map_new(&d->arena, key_size, val_size);
*map_p = map;
}
@ -433,13 +461,12 @@ static void decode_tomap(upb_decstate *d, upb_msg *msg,
if (entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_GROUP) {
/* Create proactively to handle the case where it doesn't appear. */
ent.v.val = upb_value_ptr(_upb_msg_new(entry->submsgs[0], d->arena));
ent.v.val = upb_value_ptr(_upb_msg_new(entry->submsgs[0], &d->arena));
}
decode_tosubmsg(d, &ent.k, layout, field, val.str_val);
/* Insert into map. */
_upb_map_set(map, &ent.k, map->key_size, &ent.v, map->val_size, d->arena);
ptr = decode_tosubmsg(d, ptr, &ent.k, layout, field, val.size);
_upb_map_set(map, &ent.k, map->key_size, &ent.v, map->val_size, &d->arena);
return ptr;
}
static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg,
@ -473,16 +500,15 @@ static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg,
if (UPB_UNLIKELY(type == UPB_DTYPE_GROUP)) {
ptr = decode_togroup(d, ptr, submsg, layout, field);
} else {
decode_tosubmsg(d, submsg, layout, field, val.str_val);
ptr = decode_tosubmsg(d, ptr, submsg, layout, field, val.size);
}
break;
}
case OP_STRING:
decode_verifyutf8(d, val.str_val.data, val.str_val.size);
decode_verifyutf8(d, ptr, val.size);
/* Fallthrough. */
case OP_BYTES:
memcpy(mem, &val, sizeof(upb_strview));
break;
return decode_readstr(d, ptr, val.size, mem);
case OP_SCALAR_LG2(3):
memcpy(mem, &val, 8);
break;
@ -499,9 +525,24 @@ static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg,
return ptr;
}
UPB_FORCEINLINE
static bool decode_tryfastdispatch(upb_decstate *d, const char **ptr,
upb_msg *msg, const upb_msglayout *layout) {
#if UPB_FASTTABLE
if (layout && layout->table_mask != (unsigned char)-1) {
uint16_t tag = fastdecode_loadtag(*ptr);
intptr_t table = decode_totable(layout);
*ptr = fastdecode_tagdispatch(d, *ptr, msg, table, 0, tag);
return true;
}
#endif
return false;
}
UPB_NOINLINE
static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
const upb_msglayout *layout) {
while (ptr < d->limit) {
while (true) {
uint32_t tag;
const upb_msglayout_field *field;
int field_number;
@ -510,7 +551,8 @@ static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
wireval val;
int op;
ptr = decode_varint32(d, ptr, d->limit, &tag);
UPB_ASSERT(ptr < d->limit_ptr);
ptr = decode_tag(d, ptr, &tag);
field_number = tag >> 3;
wire_type = tag & 7;
@ -518,12 +560,11 @@ static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
switch (wire_type) {
case UPB_WIRE_TYPE_VARINT:
ptr = decode_varint64(d, ptr, d->limit, &val.uint64_val);
ptr = decode_varint64(d, ptr, &val.uint64_val);
op = varint_ops[field->descriptortype];
decode_munge(field->descriptortype, &val);
break;
case UPB_WIRE_TYPE_32BIT:
if (d->limit - ptr < 4) decode_err(d);
memcpy(&val.uint32_val, ptr, 4);
val.uint32_val = _upb_be_swap32(val.uint32_val);
ptr += 4;
@ -531,7 +572,6 @@ static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
if (((1 << field->descriptortype) & fixed32_ok) == 0) goto unknown;
break;
case UPB_WIRE_TYPE_64BIT:
if (d->limit - ptr < 8) decode_err(d);
memcpy(&val.uint64_val, ptr, 8);
val.uint64_val = _upb_be_swap64(val.uint64_val);
ptr += 8;
@ -539,17 +579,16 @@ static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
if (((1 << field->descriptortype) & fixed64_ok) == 0) goto unknown;
break;
case UPB_WIRE_TYPE_DELIMITED: {
uint32_t size;
int ndx = field->descriptortype;
uint64_t size;
if (_upb_isrepeated(field)) ndx += 18;
ptr = decode_varint32(d, ptr, d->limit, &size);
if (size >= INT32_MAX || (size_t)(d->limit - ptr) < size) {
ptr = decode_varint64(d, ptr, &size);
if (size >= INT32_MAX ||
ptr - d->end + (int32_t)size > d->limit) {
decode_err(d); /* Length overflow. */
}
val.str_val.data = ptr;
val.str_val.size = size;
ptr += size;
op = delim_ops[ndx];
val.size = size;
break;
}
case UPB_WIRE_TYPE_START_GROUP:
@ -572,7 +611,7 @@ static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
ptr = decode_toarray(d, ptr, msg, layout, field, val, op);
break;
case _UPB_LABEL_MAP:
decode_tomap(d, msg, layout, field, val);
ptr = decode_tomap(d, ptr, msg, layout, field, val);
break;
default:
ptr = decode_tomsg(d, ptr, msg, layout, field, val, op);
@ -582,36 +621,78 @@ static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
unknown:
/* Skip unknown field. */
if (field_number == 0) decode_err(d);
if (wire_type == UPB_WIRE_TYPE_START_GROUP) {
ptr = decode_group(d, ptr, NULL, NULL, field_number);
}
if (wire_type == UPB_WIRE_TYPE_DELIMITED) ptr += val.size;
if (msg) {
if (wire_type == UPB_WIRE_TYPE_START_GROUP) {
d->unknown = field_start;
d->unknown_msg = msg;
ptr = decode_group(d, ptr, NULL, NULL, field_number);
d->unknown_msg = NULL;
field_start = d->unknown;
}
if (!_upb_msg_addunknown(msg, field_start, ptr - field_start,
d->arena)) {
&d->arena)) {
decode_err(d);
}
} else if (wire_type == UPB_WIRE_TYPE_START_GROUP) {
ptr = decode_group(d, ptr, NULL, NULL, field_number);
}
}
if (decode_isdone(d, &ptr)) return ptr;
if (decode_tryfastdispatch(d, &ptr, msg, layout)) return ptr;
}
}
if (ptr != d->limit) decode_err(d);
return ptr;
const char *fastdecode_generic(struct upb_decstate *d, const char *ptr,
upb_msg *msg, intptr_t table, uint64_t hasbits,
uint64_t data) {
(void)data;
*(uint32_t*)msg |= hasbits;
return decode_msg(d, ptr, msg, decode_totablep(table));
}
bool upb_decode(const char *buf, size_t size, void *msg, const upb_msglayout *l,
upb_arena *arena) {
bool _upb_decode(const char *buf, size_t size, void *msg,
const upb_msglayout *l, upb_arena *arena, int options) {
bool ok;
upb_decstate state;
state.limit = buf + size;
state.arena = arena;
state.depth = 64;
state.end_group = 0;
unsigned depth = (unsigned)options >> 16;
if (size == 0) {
return true;
} else if (size <= 16) {
memset(&state.patch, 0, 32);
memcpy(&state.patch, buf, size);
buf = state.patch;
state.end = buf + size;
state.limit = 0;
state.alias = false;
} else {
state.end = buf + size - 16;
state.limit = 16;
state.alias = options & UPB_DECODE_ALIAS;
}
if (setjmp(state.err)) return false;
state.limit_ptr = state.end;
state.unknown_msg = NULL;
state.depth = depth ? depth : 64;
state.end_group = DECODE_NOGROUP;
state.arena.head = arena->head;
state.arena.last_size = arena->last_size;
state.arena.parent = arena;
if (size == 0) return true;
decode_msg(&state, buf, msg, l);
if (UPB_UNLIKELY(UPB_SETJMP(state.err))) {
ok = false;
} else {
if (!decode_tryfastdispatch(&state, &buf, msg, l)) {
decode_msg(&state, buf, msg, l);
}
ok = state.end_group == DECODE_NOGROUP;
}
return state.end_group == 0;
arena->head.ptr = state.arena.head.ptr;
arena->head.end = state.arena.head.end;
return ok;
}
#undef OP_SCALAR_LG2

@ -7,15 +7,34 @@
#include "upb/msg.h"
/* Must be last. */
#include "upb/port_def.inc"
#ifdef __cplusplus
extern "C" {
#endif
enum {
/* If set, strings will alias the input buffer instead of copying into the
* arena. */
UPB_DECODE_ALIAS = 1,
};
#define UPB_DECODE_MAXDEPTH(depth) ((depth) << 16)
bool _upb_decode(const char *buf, size_t size, upb_msg *msg,
const upb_msglayout *l, upb_arena *arena, int options);
UPB_INLINE
bool upb_decode(const char *buf, size_t size, upb_msg *msg,
const upb_msglayout *l, upb_arena *arena);
const upb_msglayout *l, upb_arena *arena) {
return _upb_decode(buf, size, msg, l, arena, 0);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#endif /* UPB_DECODE_H_ */

@ -0,0 +1,163 @@
/*
** Internal implementation details of the decoder that are shared between
** decode.c and decode_fast.c.
*/
#ifndef UPB_DECODE_INT_H_
#define UPB_DECODE_INT_H_
#include <setjmp.h>
#include "upb/msg.h"
#include "upb/upb.int.h"
/* Must be last. */
#include "upb/port_def.inc"
#define DECODE_NOGROUP -1
typedef struct upb_decstate {
const char *end; /* Can read up to 16 bytes slop beyond this. */
const char *limit_ptr; /* = end + UPB_MIN(limit, 0) */
upb_msg *unknown_msg; /* If non-NULL, add unknown data at buffer flip. */
const char *unknown; /* Start of unknown data. */
int limit; /* Submessage limit relative to end. */
int depth;
uint32_t end_group; /* field number of END_GROUP tag, else DECODE_NOGROUP */
bool alias;
char patch[32];
upb_arena arena;
jmp_buf err;
} upb_decstate;
/* Error function that will abort decoding with longjmp(). We can't declare this
* UPB_NORETURN, even though it is appropriate, because if we do then compilers
* will "helpfully" refuse to tailcall to it
* (see: https://stackoverflow.com/a/55657013), which will defeat a major goal
* of our optimizations. That is also why we must declare it in a separate file,
* otherwise the compiler will see that it calls longjmp() and deduce that it is
* noreturn. */
const char *fastdecode_err(upb_decstate *d);
extern const uint8_t upb_utf8_offsets[];
UPB_INLINE
bool decode_verifyutf8_inl(const char *buf, int len) {
int i, j;
uint8_t offset;
i = 0;
while (i < len) {
offset = upb_utf8_offsets[(uint8_t)buf[i]];
if (offset == 0 || i + offset > len) {
return false;
}
for (j = i + 1; j < i + offset; j++) {
if ((buf[j] & 0xc0) != 0x80) {
return false;
}
}
i += offset;
}
return i == len;
}
/* x86-64 pointers always have the high 16 bits matching. So we can shift
* left 8 and right 8 without loss of information. */
UPB_INLINE intptr_t decode_totable(const upb_msglayout *tablep) {
return ((intptr_t)tablep << 8) | tablep->table_mask;
}
UPB_INLINE const upb_msglayout *decode_totablep(intptr_t table) {
return (const upb_msglayout*)(table >> 8);
}
UPB_INLINE
const char *decode_isdonefallback_inl(upb_decstate *d, const char *ptr,
int overrun) {
if (overrun < d->limit) {
/* Need to copy remaining data into patch buffer. */
UPB_ASSERT(overrun < 16);
if (d->unknown_msg) {
if (!_upb_msg_addunknown(d->unknown_msg, d->unknown, ptr - d->unknown,
&d->arena)) {
return NULL;
}
d->unknown = &d->patch[0] + overrun;
}
memset(d->patch + 16, 0, 16);
memcpy(d->patch, d->end, 16);
ptr = &d->patch[0] + overrun;
d->end = &d->patch[16];
d->limit -= 16;
d->limit_ptr = d->end + d->limit;
d->alias = false;
UPB_ASSERT(ptr < d->limit_ptr);
return ptr;
} else {
return NULL;
}
}
const char *decode_isdonefallback(upb_decstate *d, const char *ptr,
int overrun);
UPB_INLINE
bool decode_isdone(upb_decstate *d, const char **ptr) {
int overrun = *ptr - d->end;
if (UPB_LIKELY(*ptr < d->limit_ptr)) {
return false;
} else if (UPB_LIKELY(overrun == d->limit)) {
return true;
} else {
*ptr = decode_isdonefallback(d, *ptr, overrun);
return false;
}
}
UPB_INLINE
const char *fastdecode_tagdispatch(upb_decstate *d, const char *ptr,
upb_msg *msg, intptr_t table,
uint64_t hasbits, uint32_t tag) {
const upb_msglayout *table_p = decode_totablep(table);
uint8_t mask = table;
uint64_t data;
size_t idx = tag & mask;
UPB_ASSUME((idx & 7) == 0);
idx >>= 3;
data = table_p->fasttable[idx].field_data ^ tag;
return table_p->fasttable[idx].field_parser(d, ptr, msg, table, hasbits, data);
}
UPB_INLINE uint32_t fastdecode_loadtag(const char* ptr) {
uint16_t tag;
memcpy(&tag, ptr, 2);
return tag;
}
UPB_INLINE void decode_checklimit(upb_decstate *d) {
UPB_ASSERT(d->limit_ptr == d->end + UPB_MIN(0, d->limit));
}
UPB_INLINE int decode_pushlimit(upb_decstate *d, const char *ptr, int size) {
int limit = size + (int)(ptr - d->end);
int delta = d->limit - limit;
decode_checklimit(d);
d->limit = limit;
d->limit_ptr = d->end + UPB_MIN(0, limit);
decode_checklimit(d);
return delta;
}
UPB_INLINE void decode_poplimit(upb_decstate *d, const char *ptr,
int saved_delta) {
UPB_ASSERT(ptr - d->end == d->limit);
decode_checklimit(d);
d->limit += saved_delta;
d->limit_ptr = d->end + UPB_MIN(0, d->limit);
decode_checklimit(d);
}
#include "upb/port_undef.inc"
#endif /* UPB_DECODE_INT_H_ */

File diff suppressed because it is too large Load Diff

@ -0,0 +1,126 @@
// These are the specialized field parser functions for the fast parser.
// Generated tables will refer to these by name.
//
// The function names are encoded with names like:
//
// // 123 4
// upb_pss_1bt(); // Parse singular string, 1 byte tag.
//
// In position 1:
// - 'p' for parse, most function use this
// - 'c' for copy, for when we are copying strings instead of aliasing
//
// In position 2 (cardinality):
// - 's' for singular, with or without hasbit
// - 'o' for oneof
// - 'r' for non-packed repeated
// - 'p' for packed repeated
//
// In position 3 (type):
// - 'b1' for bool
// - 'v4' for 4-byte varint
// - 'v8' for 8-byte varint
// - 'z4' for zig-zag-encoded 4-byte varint
// - 'z8' for zig-zag-encoded 8-byte varint
// - 'f4' for 4-byte fixed
// - 'f8' for 8-byte fixed
// - 'm' for sub-message
// - 's' for string (validate UTF-8)
// - 'b' for bytes
//
// In position 4 (tag length):
// - '1' for one-byte tags (field numbers 1-15)
// - '2' for two-byte tags (field numbers 16-2048)
#ifndef UPB_DECODE_FAST_H_
#define UPB_DECODE_FAST_H_
#include "upb/msg.h"
struct upb_decstate;
// The fallback, generic parsing function that can handle any field type.
// This just uses the regular (non-fast) parser to parse a single field.
const char *fastdecode_generic(struct upb_decstate *d, const char *ptr,
upb_msg *msg, intptr_t table, uint64_t hasbits,
uint64_t data);
#define UPB_PARSE_PARAMS \
struct upb_decstate *d, const char *ptr, upb_msg *msg, intptr_t table, \
uint64_t hasbits, uint64_t data
/* primitive fields ***********************************************************/
#define F(card, type, valbytes, tagbytes) \
const char *upb_p##card##type##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS);
#define TYPES(card, tagbytes) \
F(card, b, 1, tagbytes) \
F(card, v, 4, tagbytes) \
F(card, v, 8, tagbytes) \
F(card, z, 4, tagbytes) \
F(card, z, 8, tagbytes) \
F(card, f, 4, tagbytes) \
F(card, f, 8, tagbytes)
#define TAGBYTES(card) \
TYPES(card, 1) \
TYPES(card, 2)
TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)
TAGBYTES(p)
#undef F
#undef TYPES
#undef TAGBYTES
/* string fields **************************************************************/
#define F(card, tagbytes, type) \
const char *upb_p##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS); \
const char *upb_c##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS);
#define UTF8(card, tagbytes) \
F(card, tagbytes, s) \
F(card, tagbytes, b)
#define TAGBYTES(card) \
UTF8(card, 1) \
UTF8(card, 2)
TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)
#undef F
#undef TAGBYTES
/* sub-message fields *********************************************************/
#define F(card, tagbytes, size_ceil, ceil_arg) \
const char *upb_p##card##m_##tagbytes##bt_max##size_ceil##b(UPB_PARSE_PARAMS);
#define SIZES(card, tagbytes) \
F(card, tagbytes, 64, 64) \
F(card, tagbytes, 128, 128) \
F(card, tagbytes, 192, 192) \
F(card, tagbytes, 256, 256) \
F(card, tagbytes, max, -1)
#define TAGBYTES(card) \
SIZES(card, 1) \
SIZES(card, 2)
TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)
#undef TAGBYTES
#undef SIZES
#undef F
#undef UPB_PARSE_PARAMS
#endif /* UPB_DECODE_FAST_H_ */

File diff suppressed because it is too large Load Diff

@ -117,9 +117,10 @@ typedef upb_inttable_iter upb_oneof_iter;
const char *upb_oneofdef_name(const upb_oneofdef *o);
const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
int upb_oneofdef_numfields(const upb_oneofdef *o);
uint32_t upb_oneofdef_index(const upb_oneofdef *o);
bool upb_oneofdef_issynthetic(const upb_oneofdef *o);
int upb_oneofdef_fieldcount(const upb_oneofdef *o);
const upb_fielddef *upb_oneofdef_field(const upb_oneofdef *o, int i);
/* Oneof lookups:
* - ntof: look up a field by name.
@ -133,11 +134,8 @@ UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
}
const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
/* upb_oneof_iter i;
* for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) {
* // ...
* }
*/
/* DEPRECATED, slated for removal. */
int upb_oneofdef_numfields(const upb_oneofdef *o);
void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
void upb_oneof_next(upb_oneof_iter *iter);
bool upb_oneof_done(upb_oneof_iter *iter);
@ -145,6 +143,7 @@ upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
void upb_oneof_iter_setdone(upb_oneof_iter *iter);
bool upb_oneof_iter_isequal(const upb_oneof_iter *iter1,
const upb_oneof_iter *iter2);
/* END DEPRECATED */
/* upb_msgdef *****************************************************************/
@ -170,21 +169,21 @@ typedef upb_strtable_iter upb_msg_oneof_iter;
const char *upb_msgdef_fullname(const upb_msgdef *m);
const upb_filedef *upb_msgdef_file(const upb_msgdef *m);
const char *upb_msgdef_name(const upb_msgdef *m);
int upb_msgdef_numfields(const upb_msgdef *m);
int upb_msgdef_numoneofs(const upb_msgdef *m);
int upb_msgdef_numrealoneofs(const upb_msgdef *m);
upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m);
bool upb_msgdef_mapentry(const upb_msgdef *m);
upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m);
bool upb_msgdef_iswrapper(const upb_msgdef *m);
bool upb_msgdef_isnumberwrapper(const upb_msgdef *m);
int upb_msgdef_fieldcount(const upb_msgdef *m);
int upb_msgdef_oneofcount(const upb_msgdef *m);
const upb_fielddef *upb_msgdef_field(const upb_msgdef *m, int i);
const upb_oneofdef *upb_msgdef_oneof(const upb_msgdef *m, int i);
const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i);
const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
size_t len);
const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
size_t len);
const upb_msglayout *upb_msgdef_layout(const upb_msgdef *m);
const upb_fielddef *_upb_msgdef_field(const upb_msgdef *m, int i);
UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
const char *name) {
@ -216,19 +215,10 @@ UPB_INLINE bool upb_msgdef_lookupnamez(const upb_msgdef *m, const char *name,
const upb_fielddef *upb_msgdef_lookupjsonname(const upb_msgdef *m,
const char *name, size_t len);
/* Iteration over fields and oneofs. For example:
*
* upb_msg_field_iter i;
* for(upb_msg_field_begin(&i, m);
* !upb_msg_field_done(&i);
* upb_msg_field_next(&i)) {
* upb_fielddef *f = upb_msg_iter_field(&i);
* // ...
* }
*
* For C we don't have separate iterators for const and non-const.
* It is the caller's responsibility to cast the upb_fielddef* to
* const if the upb_msgdef* is const. */
/* DEPRECATED, slated for removal */
int upb_msgdef_numfields(const upb_msgdef *m);
int upb_msgdef_numoneofs(const upb_msgdef *m);
int upb_msgdef_numrealoneofs(const upb_msgdef *m);
void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
void upb_msg_field_next(upb_msg_field_iter *iter);
bool upb_msg_field_done(const upb_msg_field_iter *iter);
@ -236,9 +226,6 @@ upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1,
const upb_msg_field_iter * iter2);
/* Similar to above, we also support iterating through the oneofs in a
* msgdef. */
void upb_msg_oneof_begin(upb_msg_oneof_iter * iter, const upb_msgdef *m);
void upb_msg_oneof_next(upb_msg_oneof_iter * iter);
bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
@ -246,6 +233,7 @@ const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter * iter);
bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1,
const upb_msg_oneof_iter *iter2);
/* END DEPRECATED */
/* upb_enumdef ****************************************************************/
@ -270,11 +258,6 @@ UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e,
}
const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num);
/* upb_enum_iter i;
* for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) {
* // ...
* }
*/
void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e);
void upb_enum_next(upb_enum_iter *iter);
bool upb_enum_done(upb_enum_iter *iter);
@ -294,6 +277,7 @@ int upb_filedef_enumcount(const upb_filedef *f);
const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i);
const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i);
const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i);
const upb_symtab *upb_filedef_symtab(const upb_filedef *f);
/* upb_symtab *****************************************************************/
@ -310,6 +294,7 @@ int upb_symtab_filecount(const upb_symtab *s);
const upb_filedef *upb_symtab_addfile(
upb_symtab *s, const google_protobuf_FileDescriptorProto *file,
upb_status *status);
size_t _upb_symtab_bytesloaded(const upb_symtab *s);
/* For generated code only: loads a generated descriptor. */
typedef struct upb_def_init {

@ -129,17 +129,17 @@ class OneofDefPtr {
explicit OneofDefPtr(const upb_oneofdef* ptr) : ptr_(ptr) {}
const upb_oneofdef* ptr() const { return ptr_; }
explicit operator bool() { return ptr_ != nullptr; }
explicit operator bool() const { return ptr_ != nullptr; }
// Returns the MessageDef that owns this OneofDef.
// Returns the MessageDef that contains this OneofDef.
MessageDefPtr containing_type() const;
// Returns the name of this oneof. This is the name used to look up the oneof
// by name once added to a message def.
// Returns the name of this oneof.
const char* name() const { return upb_oneofdef_name(ptr_); }
// Returns the number of fields currently defined in the oneof.
// Returns the number of fields in the oneof.
int field_count() const { return upb_oneofdef_numfields(ptr_); }
FieldDefPtr field(int i) const { return FieldDefPtr(upb_oneofdef_field(ptr_, i)); }
// Looks up by name.
FieldDefPtr FindFieldByName(const char* name, size_t len) const {
@ -159,40 +159,6 @@ class OneofDefPtr {
return FieldDefPtr(upb_oneofdef_itof(ptr_, num));
}
class const_iterator
: public std::iterator<std::forward_iterator_tag, FieldDefPtr> {
public:
void operator++() { upb_oneof_next(&iter_); }
FieldDefPtr operator*() const {
return FieldDefPtr(upb_oneof_iter_field(&iter_));
}
bool operator!=(const const_iterator& other) const {
return !upb_oneof_iter_isequal(&iter_, &other.iter_);
}
bool operator==(const const_iterator& other) const {
return upb_oneof_iter_isequal(&iter_, &other.iter_);
}
private:
friend class OneofDefPtr;
const_iterator() {}
explicit const_iterator(OneofDefPtr o) { upb_oneof_begin(&iter_, o.ptr()); }
static const_iterator end() {
const_iterator iter;
upb_oneof_iter_setdone(&iter.iter_);
return iter;
}
upb_oneof_iter iter_;
};
const_iterator begin() const { return const_iterator(*this); }
const_iterator end() const { return const_iterator::end(); }
private:
const upb_oneofdef* ptr_;
};
@ -211,9 +177,11 @@ class MessageDefPtr {
// The number of fields that belong to the MessageDef.
int field_count() const { return upb_msgdef_numfields(ptr_); }
FieldDefPtr field(int i) const { return FieldDefPtr(upb_msgdef_field(ptr_, i)); }
// The number of oneofs that belong to the MessageDef.
int oneof_count() const { return upb_msgdef_numoneofs(ptr_); }
OneofDefPtr oneof(int i) const { return OneofDefPtr(upb_msgdef_oneof(ptr_, i)); }
upb_syntax_t syntax() const { return upb_msgdef_syntax(ptr_); }
@ -258,112 +226,58 @@ class MessageDefPtr {
// Whether is a number wrapper.
bool isnumberwrapper() const { return upb_msgdef_isnumberwrapper(ptr_); }
// Iteration over fields. The order is undefined.
class const_field_iterator
: public std::iterator<std::forward_iterator_tag, FieldDefPtr> {
private:
class FieldIter {
public:
void operator++() { upb_msg_field_next(&iter_); }
FieldDefPtr operator*() const {
return FieldDefPtr(upb_msg_iter_field(&iter_));
}
explicit FieldIter(const upb_msgdef *m, int i) : m_(m), i_(i) {}
void operator++() { i_++; }
bool operator!=(const const_field_iterator& other) const {
return !upb_msg_field_iter_isequal(&iter_, &other.iter_);
}
bool operator==(const const_field_iterator& other) const {
return upb_msg_field_iter_isequal(&iter_, &other.iter_);
}
FieldDefPtr operator*() { return FieldDefPtr(upb_msgdef_field(m_, i_)); }
bool operator!=(const FieldIter& other) { return i_ != other.i_; }
bool operator==(const FieldIter& other) { return i_ == other.i_; }
private:
friend class MessageDefPtr;
explicit const_field_iterator() {}
explicit const_field_iterator(MessageDefPtr msg) {
upb_msg_field_begin(&iter_, msg.ptr());
}
static const_field_iterator end() {
const_field_iterator iter;
upb_msg_field_iter_setdone(&iter.iter_);
return iter;
}
upb_msg_field_iter iter_;
const upb_msgdef *m_;
int i_;
};
// Iteration over oneofs. The order is undefined.
class const_oneof_iterator
: public std::iterator<std::forward_iterator_tag, OneofDefPtr> {
class FieldAccessor {
public:
void operator++() { upb_msg_oneof_next(&iter_); }
OneofDefPtr operator*() const {
return OneofDefPtr(upb_msg_iter_oneof(&iter_));
}
bool operator!=(const const_oneof_iterator& other) const {
return !upb_msg_oneof_iter_isequal(&iter_, &other.iter_);
}
bool operator==(const const_oneof_iterator& other) const {
return upb_msg_oneof_iter_isequal(&iter_, &other.iter_);
}
explicit FieldAccessor(const upb_msgdef* md) : md_(md) {}
FieldIter begin() { return FieldIter(md_, 0); }
FieldIter end() { return FieldIter(md_, upb_msgdef_fieldcount(md_)); }
private:
friend class MessageDefPtr;
const_oneof_iterator() {}
explicit const_oneof_iterator(MessageDefPtr msg) {
upb_msg_oneof_begin(&iter_, msg.ptr());
}
static const_oneof_iterator end() {
const_oneof_iterator iter;
upb_msg_oneof_iter_setdone(&iter.iter_);
return iter;
}
upb_msg_oneof_iter iter_;
const upb_msgdef* md_;
};
class ConstFieldAccessor {
class OneofIter {
public:
explicit ConstFieldAccessor(const upb_msgdef* md) : md_(md) {}
const_field_iterator begin() { return MessageDefPtr(md_).field_begin(); }
const_field_iterator end() { return MessageDefPtr(md_).field_end(); }
explicit OneofIter(const upb_msgdef *m, int i) : m_(m), i_(i) {}
void operator++() { i_++; }
OneofDefPtr operator*() { return OneofDefPtr(upb_msgdef_oneof(m_, i_)); }
bool operator!=(const OneofIter& other) { return i_ != other.i_; }
bool operator==(const OneofIter& other) { return i_ == other.i_; }
private:
const upb_msgdef* md_;
const upb_msgdef *m_;
int i_;
};
class ConstOneofAccessor {
class OneofAccessor {
public:
explicit ConstOneofAccessor(const upb_msgdef* md) : md_(md) {}
const_oneof_iterator begin() { return MessageDefPtr(md_).oneof_begin(); }
const_oneof_iterator end() { return MessageDefPtr(md_).oneof_end(); }
explicit OneofAccessor(const upb_msgdef* md) : md_(md) {}
OneofIter begin() { return OneofIter(md_, 0); }
OneofIter end() { return OneofIter(md_, upb_msgdef_oneofcount(md_)); }
private:
const upb_msgdef* md_;
};
const_field_iterator field_begin() const {
return const_field_iterator(*this);
}
const_field_iterator field_end() const { return const_field_iterator::end(); }
const_oneof_iterator oneof_begin() const {
return const_oneof_iterator(*this);
}
const_oneof_iterator oneof_end() const { return const_oneof_iterator::end(); }
ConstFieldAccessor fields() const { return ConstFieldAccessor(ptr()); }
ConstOneofAccessor oneofs() const { return ConstOneofAccessor(ptr()); }
public:
FieldAccessor fields() const { return FieldAccessor(ptr()); }
OneofAccessor oneofs() const { return OneofAccessor(ptr()); }
private:
const upb_msgdef* ptr_;

@ -2,35 +2,39 @@
#include "upb/encode.h"
#include <setjmp.h>
#include <string.h>
#include "upb/msg.h"
#include "upb/upb.h"
/* Must be last. */
#include "upb/port_def.inc"
#define UPB_PB_VARINT_MAX_LEN 10
#define CHK(x) do { if (!(x)) { return false; } } while(0)
static size_t upb_encode_varint(uint64_t val, char *buf) {
size_t i;
if (val < 128) { buf[0] = val; return 1; }
i = 0;
while (val) {
UPB_NOINLINE
static size_t encode_varint64(uint64_t val, char *buf) {
size_t i = 0;
do {
uint8_t byte = val & 0x7fU;
val >>= 7;
if (val) byte |= 0x80U;
buf[i++] = byte;
}
} while (val);
return i;
}
static uint32_t upb_zzencode_32(int32_t n) { return ((uint32_t)n << 1) ^ (n >> 31); }
static uint64_t upb_zzencode_64(int64_t n) { return ((uint64_t)n << 1) ^ (n >> 63); }
static uint32_t encode_zz32(int32_t n) { return ((uint32_t)n << 1) ^ (n >> 31); }
static uint64_t encode_zz64(int64_t n) { return ((uint64_t)n << 1) ^ (n >> 63); }
typedef struct {
jmp_buf err;
upb_alloc *alloc;
char *buf, *ptr, *limit;
int options;
int depth;
_upb_mapsorter sorter;
} upb_encstate;
static size_t upb_roundup_pow2(size_t bytes) {
@ -41,11 +45,17 @@ static size_t upb_roundup_pow2(size_t bytes) {
return ret;
}
static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
UPB_NORETURN static void encode_err(upb_encstate *e) {
UPB_LONGJMP(e->err, 1);
}
UPB_NOINLINE
static void encode_growbuffer(upb_encstate *e, size_t bytes) {
size_t old_size = e->limit - e->buf;
size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size);
CHK(new_buf);
if (!new_buf) encode_err(e);
/* We want previous data at the end, realloc() put it at the beginning. */
if (old_size > 0) {
@ -55,99 +65,116 @@ static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
e->ptr = new_buf + new_size - (e->limit - e->ptr);
e->limit = new_buf + new_size;
e->buf = new_buf;
return true;
e->ptr -= bytes;
}
/* Call to ensure that at least "bytes" bytes are available for writing at
* e->ptr. Returns false if the bytes could not be allocated. */
static bool upb_encode_reserve(upb_encstate *e, size_t bytes) {
CHK(UPB_LIKELY((size_t)(e->ptr - e->buf) >= bytes) ||
upb_encode_growbuffer(e, bytes));
UPB_FORCEINLINE
static void encode_reserve(upb_encstate *e, size_t bytes) {
if ((size_t)(e->ptr - e->buf) < bytes) {
encode_growbuffer(e, bytes);
return;
}
e->ptr -= bytes;
return true;
}
/* Writes the given bytes to the buffer, handling reserve/advance. */
static bool upb_put_bytes(upb_encstate *e, const void *data, size_t len) {
if (len == 0) return true;
CHK(upb_encode_reserve(e, len));
static void encode_bytes(upb_encstate *e, const void *data, size_t len) {
if (len == 0) return; /* memcpy() with zero size is UB */
encode_reserve(e, len);
memcpy(e->ptr, data, len);
return true;
}
static bool upb_put_fixed64(upb_encstate *e, uint64_t val) {
static void encode_fixed64(upb_encstate *e, uint64_t val) {
val = _upb_be_swap64(val);
return upb_put_bytes(e, &val, sizeof(uint64_t));
encode_bytes(e, &val, sizeof(uint64_t));
}
static bool upb_put_fixed32(upb_encstate *e, uint32_t val) {
static void encode_fixed32(upb_encstate *e, uint32_t val) {
val = _upb_be_swap32(val);
return upb_put_bytes(e, &val, sizeof(uint32_t));
encode_bytes(e, &val, sizeof(uint32_t));
}
static bool upb_put_varint(upb_encstate *e, uint64_t val) {
UPB_NOINLINE
static void encode_longvarint(upb_encstate *e, uint64_t val) {
size_t len;
char *start;
CHK(upb_encode_reserve(e, UPB_PB_VARINT_MAX_LEN));
len = upb_encode_varint(val, e->ptr);
encode_reserve(e, UPB_PB_VARINT_MAX_LEN);
len = encode_varint64(val, e->ptr);
start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
memmove(start, e->ptr, len);
e->ptr = start;
return true;
}
static bool upb_put_double(upb_encstate *e, double d) {
UPB_FORCEINLINE
static void encode_varint(upb_encstate *e, uint64_t val) {
if (val < 128 && e->ptr != e->buf) {
--e->ptr;
*e->ptr = val;
} else {
encode_longvarint(e, val);
}
}
static void encode_double(upb_encstate *e, double d) {
uint64_t u64;
UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
memcpy(&u64, &d, sizeof(uint64_t));
return upb_put_fixed64(e, u64);
encode_fixed64(e, u64);
}
static bool upb_put_float(upb_encstate *e, float d) {
static void encode_float(upb_encstate *e, float d) {
uint32_t u32;
UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
memcpy(&u32, &d, sizeof(uint32_t));
return upb_put_fixed32(e, u32);
encode_fixed32(e, u32);
}
static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) {
return upb_put_varint(e, (field_number << 3) | wire_type);
static void encode_tag(upb_encstate *e, uint32_t field_number,
uint8_t wire_type) {
encode_varint(e, (field_number << 3) | wire_type);
}
static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr,
static void encode_fixedarray(upb_encstate *e, const upb_array *arr,
size_t elem_size, uint32_t tag) {
size_t bytes = arr->len * elem_size;
const char* data = _upb_array_constptr(arr);
const char* ptr = data + bytes - elem_size;
if (tag) {
while (true) {
CHK(upb_put_bytes(e, ptr, elem_size) && upb_put_varint(e, tag));
encode_bytes(e, ptr, elem_size);
encode_varint(e, tag);
if (ptr == data) break;
ptr -= elem_size;
}
return true;
} else {
return upb_put_bytes(e, data, bytes) && upb_put_varint(e, bytes);
encode_bytes(e, data, bytes);
}
}
bool upb_encode_message(upb_encstate *e, const char *msg,
const upb_msglayout *m, size_t *size);
static void encode_message(upb_encstate *e, const char *msg,
const upb_msglayout *m, size_t *size);
static bool upb_encode_scalarfield(upb_encstate *e, const void *_field_mem,
const upb_msglayout *m,
const upb_msglayout_field *f,
bool skip_zero_value) {
static void encode_scalar(upb_encstate *e, const void *_field_mem,
const upb_msglayout *m, const upb_msglayout_field *f,
bool skip_zero_value) {
const char *field_mem = _field_mem;
#define CASE(ctype, type, wire_type, encodeval) do { \
ctype val = *(ctype*)field_mem; \
if (skip_zero_value && val == 0) { \
return true; \
} \
return upb_put_ ## type(e, encodeval) && \
upb_put_tag(e, f->number, wire_type); \
} while(0)
int wire_type;
#define CASE(ctype, type, wtype, encodeval) \
{ \
ctype val = *(ctype *)field_mem; \
if (skip_zero_value && val == 0) { \
return; \
} \
encode_##type(e, encodeval); \
wire_type = wtype; \
break; \
}
switch (f->descriptortype) {
case UPB_DESCRIPTOR_TYPE_DOUBLE:
@ -171,90 +198,95 @@ static bool upb_encode_scalarfield(upb_encstate *e, const void *_field_mem,
case UPB_DESCRIPTOR_TYPE_BOOL:
CASE(bool, varint, UPB_WIRE_TYPE_VARINT, val);
case UPB_DESCRIPTOR_TYPE_SINT32:
CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_32(val));
CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, encode_zz32(val));
case UPB_DESCRIPTOR_TYPE_SINT64:
CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, encode_zz64(val));
case UPB_DESCRIPTOR_TYPE_STRING:
case UPB_DESCRIPTOR_TYPE_BYTES: {
upb_strview view = *(upb_strview*)field_mem;
if (skip_zero_value && view.size == 0) {
return true;
return;
}
return upb_put_bytes(e, view.data, view.size) &&
upb_put_varint(e, view.size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
encode_bytes(e, view.data, view.size);
encode_varint(e, view.size);
wire_type = UPB_WIRE_TYPE_DELIMITED;
break;
}
case UPB_DESCRIPTOR_TYPE_GROUP: {
size_t size;
void *submsg = *(void **)field_mem;
const upb_msglayout *subm = m->submsgs[f->submsg_index];
if (submsg == NULL) {
return true;
return;
}
return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
upb_encode_message(e, submsg, subm, &size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
if (--e->depth == 0) encode_err(e);
encode_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP);
encode_message(e, submsg, subm, &size);
wire_type = UPB_WIRE_TYPE_START_GROUP;
e->depth++;
break;
}
case UPB_DESCRIPTOR_TYPE_MESSAGE: {
size_t size;
void *submsg = *(void **)field_mem;
const upb_msglayout *subm = m->submsgs[f->submsg_index];
if (submsg == NULL) {
return true;
return;
}
return upb_encode_message(e, submsg, subm, &size) &&
upb_put_varint(e, size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
if (--e->depth == 0) encode_err(e);
encode_message(e, submsg, subm, &size);
encode_varint(e, size);
wire_type = UPB_WIRE_TYPE_DELIMITED;
e->depth++;
break;
}
default:
UPB_UNREACHABLE();
}
#undef CASE
UPB_UNREACHABLE();
encode_tag(e, f->number, wire_type);
}
static bool upb_encode_array(upb_encstate *e, const char *field_mem,
const upb_msglayout *m,
const upb_msglayout_field *f) {
static void encode_array(upb_encstate *e, const char *field_mem,
const upb_msglayout *m, const upb_msglayout_field *f) {
const upb_array *arr = *(const upb_array**)field_mem;
bool packed = f->label == _UPB_LABEL_PACKED;
size_t pre_len = e->limit - e->ptr;
if (arr == NULL || arr->len == 0) {
return true;
return;
}
#define VARINT_CASE(ctype, encode) \
{ \
const ctype *start = _upb_array_constptr(arr); \
const ctype *ptr = start + arr->len; \
size_t pre_len = e->limit - e->ptr; \
uint32_t tag = packed ? 0 : (f->number << 3) | UPB_WIRE_TYPE_VARINT; \
do { \
ptr--; \
CHK(upb_put_varint(e, encode)); \
if (tag) CHK(upb_put_varint(e, tag)); \
encode_varint(e, encode); \
if (tag) encode_varint(e, tag); \
} while (ptr != start); \
if (!tag) CHK(upb_put_varint(e, e->limit - e->ptr - pre_len)); \
} \
break; \
do { \
; \
} while (0)
break;
#define TAG(wire_type) (packed ? 0 : (f->number << 3 | wire_type))
switch (f->descriptortype) {
case UPB_DESCRIPTOR_TYPE_DOUBLE:
CHK(upb_put_fixedarray(e, arr, sizeof(double), TAG(UPB_WIRE_TYPE_64BIT)));
encode_fixedarray(e, arr, sizeof(double), TAG(UPB_WIRE_TYPE_64BIT));
break;
case UPB_DESCRIPTOR_TYPE_FLOAT:
CHK(upb_put_fixedarray(e, arr, sizeof(float), TAG(UPB_WIRE_TYPE_32BIT)));
encode_fixedarray(e, arr, sizeof(float), TAG(UPB_WIRE_TYPE_32BIT));
break;
case UPB_DESCRIPTOR_TYPE_SFIXED64:
case UPB_DESCRIPTOR_TYPE_FIXED64:
CHK(upb_put_fixedarray(e, arr, sizeof(uint64_t), TAG(UPB_WIRE_TYPE_64BIT)));
encode_fixedarray(e, arr, sizeof(uint64_t), TAG(UPB_WIRE_TYPE_64BIT));
break;
case UPB_DESCRIPTOR_TYPE_FIXED32:
case UPB_DESCRIPTOR_TYPE_SFIXED32:
CHK(upb_put_fixedarray(e, arr, sizeof(uint32_t), TAG(UPB_WIRE_TYPE_32BIT)));
encode_fixedarray(e, arr, sizeof(uint32_t), TAG(UPB_WIRE_TYPE_32BIT));
break;
case UPB_DESCRIPTOR_TYPE_INT64:
case UPB_DESCRIPTOR_TYPE_UINT64:
@ -267,154 +299,180 @@ static bool upb_encode_array(upb_encstate *e, const char *field_mem,
case UPB_DESCRIPTOR_TYPE_BOOL:
VARINT_CASE(bool, *ptr);
case UPB_DESCRIPTOR_TYPE_SINT32:
VARINT_CASE(int32_t, upb_zzencode_32(*ptr));
VARINT_CASE(int32_t, encode_zz32(*ptr));
case UPB_DESCRIPTOR_TYPE_SINT64:
VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
VARINT_CASE(int64_t, encode_zz64(*ptr));
case UPB_DESCRIPTOR_TYPE_STRING:
case UPB_DESCRIPTOR_TYPE_BYTES: {
const upb_strview *start = _upb_array_constptr(arr);
const upb_strview *ptr = start + arr->len;
do {
ptr--;
CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
upb_put_varint(e, ptr->size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
encode_bytes(e, ptr->data, ptr->size);
encode_varint(e, ptr->size);
encode_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
} while (ptr != start);
return true;
return;
}
case UPB_DESCRIPTOR_TYPE_GROUP: {
const void *const*start = _upb_array_constptr(arr);
const void *const*ptr = start + arr->len;
const upb_msglayout *subm = m->submsgs[f->submsg_index];
if (--e->depth == 0) encode_err(e);
do {
size_t size;
ptr--;
CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
upb_encode_message(e, *ptr, subm, &size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP));
encode_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP);
encode_message(e, *ptr, subm, &size);
encode_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
} while (ptr != start);
return true;
e->depth++;
return;
}
case UPB_DESCRIPTOR_TYPE_MESSAGE: {
const void *const*start = _upb_array_constptr(arr);
const void *const*ptr = start + arr->len;
const upb_msglayout *subm = m->submsgs[f->submsg_index];
if (--e->depth == 0) encode_err(e);
do {
size_t size;
ptr--;
CHK(upb_encode_message(e, *ptr, subm, &size) &&
upb_put_varint(e, size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
encode_message(e, *ptr, subm, &size);
encode_varint(e, size);
encode_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
} while (ptr != start);
return true;
e->depth++;
return;
}
}
#undef VARINT_CASE
if (packed) {
CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
encode_varint(e, e->limit - e->ptr - pre_len);
encode_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
}
return true;
}
static bool upb_encode_map(upb_encstate *e, const char *field_mem,
const upb_msglayout *m,
const upb_msglayout_field *f) {
static void encode_mapentry(upb_encstate *e, uint32_t number,
const upb_msglayout *layout,
const upb_map_entry *ent) {
const upb_msglayout_field *key_field = &layout->fields[0];
const upb_msglayout_field *val_field = &layout->fields[1];
size_t pre_len = e->limit - e->ptr;
size_t size;
encode_scalar(e, &ent->v, layout, val_field, false);
encode_scalar(e, &ent->k, layout, key_field, false);
size = (e->limit - e->ptr) - pre_len;
encode_varint(e, size);
encode_tag(e, number, UPB_WIRE_TYPE_DELIMITED);
}
static void encode_map(upb_encstate *e, const char *field_mem,
const upb_msglayout *m, const upb_msglayout_field *f) {
const upb_map *map = *(const upb_map**)field_mem;
const upb_msglayout *entry = m->submsgs[f->submsg_index];
const upb_msglayout_field *key_field = &entry->fields[0];
const upb_msglayout_field *val_field = &entry->fields[1];
upb_strtable_iter i;
if (map == NULL) {
return true;
}
const upb_msglayout *layout = m->submsgs[f->submsg_index];
UPB_ASSERT(layout->field_count == 2);
if (map == NULL) return;
upb_strtable_begin(&i, &map->table);
for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
size_t pre_len = e->limit - e->ptr;
size_t size;
upb_strview key = upb_strtable_iter_key(&i);
const upb_value val = upb_strtable_iter_value(&i);
if (e->options & UPB_ENCODE_DETERMINISTIC) {
_upb_sortedmap sorted;
_upb_mapsorter_pushmap(&e->sorter, layout->fields[0].descriptortype, map,
&sorted);
upb_map_entry ent;
_upb_map_fromkey(key, &ent.k, map->key_size);
_upb_map_fromvalue(val, &ent.v, map->val_size);
CHK(upb_encode_scalarfield(e, &ent.v, entry, val_field, false));
CHK(upb_encode_scalarfield(e, &ent.k, entry, key_field, false));
size = (e->limit - e->ptr) - pre_len;
CHK(upb_put_varint(e, size));
CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) {
encode_mapentry(e, f->number, layout, &ent);
}
_upb_mapsorter_popmap(&e->sorter, &sorted);
} else {
upb_strtable_iter i;
upb_strtable_begin(&i, &map->table);
for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
upb_strview key = upb_strtable_iter_key(&i);
const upb_value val = upb_strtable_iter_value(&i);
upb_map_entry ent;
_upb_map_fromkey(key, &ent.k, map->key_size);
_upb_map_fromvalue(val, &ent.v, map->val_size);
encode_mapentry(e, f->number, layout, &ent);
}
}
return true;
}
static void encode_scalarfield(upb_encstate *e, const char *msg,
const upb_msglayout *m,
const upb_msglayout_field *f) {
bool skip_empty = false;
if (f->presence == 0) {
/* Proto3 presence. */
skip_empty = true;
} else if (f->presence > 0) {
/* Proto2 presence: hasbit. */
if (!_upb_hasbit_field(msg, f)) return;
} else {
/* Field is in a oneof. */
if (_upb_getoneofcase_field(msg, f) != f->number) return;
}
encode_scalar(e, msg + f->offset, m, f, skip_empty);
}
bool upb_encode_message(upb_encstate *e, const char *msg,
const upb_msglayout *m, size_t *size) {
int i;
static void encode_message(upb_encstate *e, const char *msg,
const upb_msglayout *m, size_t *size) {
size_t pre_len = e->limit - e->ptr;
const char *unknown;
size_t unknown_size;
const upb_msglayout_field *f = &m->fields[m->field_count];
const upb_msglayout_field *first = &m->fields[0];
unknown = upb_msg_getunknown(msg, &unknown_size);
if ((e->options & UPB_ENCODE_SKIPUNKNOWN) == 0) {
size_t unknown_size;
const char *unknown = upb_msg_getunknown(msg, &unknown_size);
if (unknown) {
upb_put_bytes(e, unknown, unknown_size);
if (unknown) {
encode_bytes(e, unknown, unknown_size);
}
}
for (i = m->field_count - 1; i >= 0; i--) {
const upb_msglayout_field *f = &m->fields[i];
while (f != first) {
f--;
if (_upb_isrepeated(f)) {
CHK(upb_encode_array(e, msg + f->offset, m, f));
encode_array(e, msg + f->offset, m, f);
} else if (f->label == _UPB_LABEL_MAP) {
CHK(upb_encode_map(e, msg + f->offset, m, f));
encode_map(e, msg + f->offset, m, f);
} else {
bool skip_empty = false;
if (f->presence == 0) {
/* Proto3 presence. */
skip_empty = true;
} else if (f->presence > 0) {
/* Proto2 presence: hasbit. */
if (!_upb_hasbit_field(msg, f)) {
continue;
}
} else {
/* Field is in a oneof. */
if (_upb_getoneofcase_field(msg, f) != f->number) {
continue;
}
}
CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty));
encode_scalarfield(e, msg, m, f);
}
}
*size = (e->limit - e->ptr) - pre_len;
return true;
}
char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena,
size_t *size) {
char *upb_encode_ex(const void *msg, const upb_msglayout *m, int options,
upb_arena *arena, size_t *size) {
upb_encstate e;
unsigned depth = (unsigned)options >> 16;
e.alloc = upb_arena_alloc(arena);
e.buf = NULL;
e.limit = NULL;
e.ptr = NULL;
e.depth = depth ? depth : 64;
e.options = options;
_upb_mapsorter_init(&e.sorter);
char *ret = NULL;
if (!upb_encode_message(&e, msg, m, size)) {
if (UPB_SETJMP(e.err)) {
*size = 0;
return NULL;
}
*size = e.limit - e.ptr;
if (*size == 0) {
static char ch;
return &ch;
ret = NULL;
} else {
UPB_ASSERT(e.ptr);
return e.ptr;
encode_message(&e, msg, m, size);
*size = e.limit - e.ptr;
if (*size == 0) {
static char ch;
ret = &ch;
} else {
UPB_ASSERT(e.ptr);
ret = e.ptr;
}
}
}
#undef CHK
_upb_mapsorter_destroy(&e.sorter);
return ret;
}

@ -7,12 +7,37 @@
#include "upb/msg.h"
/* Must be last. */
#include "upb/port_def.inc"
#ifdef __cplusplus
extern "C" {
#endif
char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena,
size_t *size);
enum {
/* If set, the results of serializing will be deterministic across all
* instances of this binary. There are no guarantees across different
* binary builds.
*
* If your proto contains maps, the encoder will need to malloc()/free()
* memory during encode. */
UPB_ENCODE_DETERMINISTIC = 1,
/* When set, unknown fields are not printed. */
UPB_ENCODE_SKIPUNKNOWN = 2,
};
#define UPB_ENCODE_MAXDEPTH(depth) ((depth) << 16)
char *upb_encode_ex(const void *msg, const upb_msglayout *l, int options,
upb_arena *arena, size_t *size);
UPB_INLINE char *upb_encode(const void *msg, const upb_msglayout *l,
upb_arena *arena, size_t *size) {
return upb_encode_ex(msg, l, 0, arena, size);
}
#include "upb/port_undef.inc"
#ifdef __cplusplus
} /* extern "C" */

@ -359,7 +359,7 @@ struct upb_handlercache {
const upb_handlers *upb_handlercache_get(upb_handlercache *c,
const upb_msgdef *md) {
upb_msg_field_iter i;
int i, n;
upb_value v;
upb_handlers *h;
@ -377,10 +377,9 @@ const upb_handlers *upb_handlercache_get(upb_handlercache *c,
/* For each submessage field, get or create a handlers object and set it as
* the subhandlers. */
for(upb_msg_field_begin(&i, md);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(md);
for (i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(md, i);
if (upb_fielddef_issubmsg(f)) {
const upb_msgdef *subdef = upb_fielddef_msgsubdef(f);

@ -951,7 +951,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf,
upb_fieldtype_t type = upb_fielddef_type(p->top->f);
double val;
double dummy;
double inf = UPB_INFINITY;
double inf = INFINITY;
errno = 0;
@ -2858,7 +2858,7 @@ static void json_parser_reset(upb_json_parser *p) {
static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
const upb_msgdef *md) {
upb_msg_field_iter i;
int i, n;
upb_alloc *alloc = upb_arena_alloc(c->arena);
upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m));
@ -2869,14 +2869,13 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
upb_byteshandler_setstring(&m->input_handler_, parse, m);
upb_byteshandler_setendstr(&m->input_handler_, end, m);
upb_strtable_init2(&m->name_table, UPB_CTYPE_CONSTPTR, alloc);
upb_strtable_init2(&m->name_table, UPB_CTYPE_CONSTPTR, 4, alloc);
/* Build name_table */
for(upb_msg_field_begin(&i, md);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
const upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(md);
for(i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(md, i);
upb_value v = upb_value_constptr(f);
const char *name;
@ -2965,7 +2964,7 @@ const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
const upb_msgdef *md) {
upb_json_parsermethod *m;
upb_value v;
upb_msg_field_iter i;
int i, n;
upb_alloc *alloc = upb_arena_alloc(c->arena);
if (upb_inttable_lookupptr(&c->methods, md, &v)) {
@ -2980,10 +2979,9 @@ const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
/* Populate parser methods for all submessages, so the name tables will
* be available during parsing. */
for(upb_msg_field_begin(&i, md);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(md);
for(i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(md, i);
if (upb_fielddef_issubmsg(f)) {
const upb_msgdef *subdef = upb_fielddef_msgsubdef(f);

@ -7,7 +7,9 @@
#include <ctype.h>
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
@ -139,7 +141,7 @@ static void putstring(upb_json_printer *p, const char *buf, size_t len) {
char escape_buf[8];
if (!escape) {
unsigned char byte = (unsigned char)c;
_upb_snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte);
snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte);
escape = escape_buf;
}
@ -178,53 +180,53 @@ const char neginf[] = "\"-Infinity\"";
const char inf[] = "\"Infinity\"";
static size_t fmt_double(double val, char* buf, size_t length) {
if (val == UPB_INFINITY) {
if (val == INFINITY) {
CHKLENGTH(length >= strlen(inf));
strcpy(buf, inf);
return strlen(inf);
} else if (val == -UPB_INFINITY) {
} else if (val == -INFINITY) {
CHKLENGTH(length >= strlen(neginf));
strcpy(buf, neginf);
return strlen(neginf);
} else {
size_t n = _upb_snprintf(buf, length, "%.17g", val);
size_t n = snprintf(buf, length, "%.17g", val);
CHKLENGTH(n > 0 && n < length);
return n;
}
}
static size_t fmt_float(float val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "%.8g", val);
size_t n = snprintf(buf, length, "%.8g", val);
CHKLENGTH(n > 0 && n < length);
return n;
}
static size_t fmt_bool(bool val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "%s", (val ? "true" : "false"));
size_t n = snprintf(buf, length, "%s", (val ? "true" : "false"));
CHKLENGTH(n > 0 && n < length);
return n;
}
static size_t fmt_int64_as_number(int64_t val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "%" PRId64, val);
size_t n = snprintf(buf, length, "%" PRId64, val);
CHKLENGTH(n > 0 && n < length);
return n;
}
static size_t fmt_uint64_as_number(uint64_t val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "%" PRIu64, val);
size_t n = snprintf(buf, length, "%" PRIu64, val);
CHKLENGTH(n > 0 && n < length);
return n;
}
static size_t fmt_int64_as_string(int64_t val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "\"%" PRId64 "\"", val);
size_t n = snprintf(buf, length, "\"%" PRId64 "\"", val);
CHKLENGTH(n > 0 && n < length);
return n;
}
static size_t fmt_uint64_as_string(uint64_t val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "\"%" PRIu64 "\"", val);
size_t n = snprintf(buf, length, "\"%" PRIu64 "\"", val);
CHKLENGTH(n > 0 && n < length);
return n;
}
@ -870,12 +872,12 @@ static bool printer_enddurationmsg(void *closure, const void *handler_data,
return false;
}
_upb_snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds);
snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds);
base_len = strlen(buffer);
if (p->nanos != 0) {
char nanos_buffer[UPB_DURATION_MAX_NANO_LEN + 3];
_upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f",
snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f",
p->nanos / 1000000000.0);
/* Remove trailing 0. */
for (i = UPB_DURATION_MAX_NANO_LEN + 2;
@ -949,8 +951,8 @@ static bool printer_endtimestampmsg(void *closure, const void *handler_data,
"%Y-%m-%dT%H:%M:%S", gmtime(&time));
if (p->nanos != 0) {
char nanos_buffer[UPB_TIMESTAMP_MAX_NANO_LEN + 3];
_upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f",
p->nanos / 1000000000.0);
snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f",
p->nanos / 1000000000.0);
/* Remove trailing 0. */
for (i = UPB_TIMESTAMP_MAX_NANO_LEN + 2;
nanos_buffer[i] == '0'; i--) {
@ -1124,16 +1126,16 @@ void printer_sethandlers_timestamp(const void *closure, upb_handlers *h) {
void printer_sethandlers_value(const void *closure, upb_handlers *h) {
const upb_msgdef *md = upb_handlers_msgdef(h);
upb_msg_field_iter i;
int i, n;
upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
upb_msg_field_begin(&i, md);
for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) {
const upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(md);
for (i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(md, i);
switch (upb_fielddef_type(f)) {
case UPB_TYPE_ENUM:
@ -1222,7 +1224,7 @@ void printer_sethandlers(const void *closure, upb_handlers *h) {
const upb_msgdef *md = upb_handlers_msgdef(h);
bool is_mapentry = upb_msgdef_mapentry(md);
upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
upb_msg_field_iter i;
int i, n;
const upb_json_printercache *cache = closure;
const bool preserve_fieldnames = cache->preserve_fieldnames;
@ -1287,9 +1289,9 @@ void printer_sethandlers(const void *closure, upb_handlers *h) {
} \
break;
upb_msg_field_begin(&i, md);
for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) {
const upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(md);
for (i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(md, i);
upb_handlerattr name_attr = UPB_HANDLERATTR_INIT;
name_attr.handler_data = newstrpc(h, f, preserve_fieldnames);

@ -5,6 +5,7 @@
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
@ -42,10 +43,23 @@ static bool jsondec_streql(upb_strview str, const char *lit) {
return str.size == strlen(lit) && memcmp(str.data, lit, str.size) == 0;
}
static bool jsondec_isnullvalue(const upb_fielddef *f) {
return upb_fielddef_type(f) == UPB_TYPE_ENUM &&
strcmp(upb_enumdef_fullname(upb_fielddef_enumsubdef(f)),
"google.protobuf.NullValue") == 0;
}
static bool jsondec_isvalue(const upb_fielddef *f) {
return (upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
upb_msgdef_wellknowntype(upb_fielddef_msgsubdef(f)) ==
UPB_WELLKNOWN_VALUE) ||
jsondec_isnullvalue(f);
}
UPB_NORETURN static void jsondec_err(jsondec *d, const char *msg) {
upb_status_seterrf(d->status, "Error parsing JSON @%d:%d: %s", d->line,
(int)(d->ptr - d->line_begin), msg);
longjmp(d->err, 1);
UPB_LONGJMP(d->err, 1);
}
UPB_NORETURN static void jsondec_errf(jsondec *d, const char *fmt, ...) {
@ -55,7 +69,7 @@ UPB_NORETURN static void jsondec_errf(jsondec *d, const char *fmt, ...) {
va_start(argp, fmt);
upb_status_vappenderrf(d->status, fmt, argp);
va_end(argp);
longjmp(d->err, 1);
UPB_LONGJMP(d->err, 1);
}
static void jsondec_skipws(jsondec *d) {
@ -382,6 +396,8 @@ static void jsondec_resize(jsondec *d, char **buf, char **end, char **buf_end) {
size_t size = UPB_MAX(8, 2 * oldsize);
*buf = upb_arena_realloc(d->arena, *buf, len, size);
if (!*buf) jsondec_err(d, "Out of memory");
*end = *buf + len;
*buf_end = *buf + size;
}
@ -734,11 +750,11 @@ static upb_msgval jsondec_double(jsondec *d, const upb_fielddef *f) {
case JD_STRING:
str = jsondec_string(d);
if (jsondec_streql(str, "NaN")) {
val.double_val = UPB_NAN;
val.double_val = NAN;
} else if (jsondec_streql(str, "Infinity")) {
val.double_val = UPB_INFINITY;
val.double_val = INFINITY;
} else if (jsondec_streql(str, "-Infinity")) {
val.double_val = -UPB_INFINITY;
val.double_val = -INFINITY;
} else {
val.double_val = strtod(str.data, NULL);
}
@ -748,7 +764,7 @@ static upb_msgval jsondec_double(jsondec *d, const upb_fielddef *f) {
}
if (upb_fielddef_type(f) == UPB_TYPE_FLOAT) {
if (val.double_val != UPB_INFINITY && val.double_val != -UPB_INFINITY &&
if (val.double_val != INFINITY && val.double_val != -INFINITY &&
(val.double_val > FLT_MAX || val.double_val < -FLT_MAX)) {
jsondec_err(d, "Float out of range");
}
@ -769,21 +785,32 @@ static upb_msgval jsondec_strfield(jsondec *d, const upb_fielddef *f) {
}
static upb_msgval jsondec_enum(jsondec *d, const upb_fielddef *f) {
if (jsondec_peek(d) == JD_STRING) {
const upb_enumdef *e = upb_fielddef_enumsubdef(f);
upb_strview str = jsondec_string(d);
upb_msgval val;
if (!upb_enumdef_ntoi(e, str.data, str.size, &val.int32_val)) {
if (d->options & UPB_JSONDEC_IGNOREUNKNOWN) {
switch (jsondec_peek(d)) {
case JD_STRING: {
const upb_enumdef *e = upb_fielddef_enumsubdef(f);
upb_strview str = jsondec_string(d);
upb_msgval val;
if (!upb_enumdef_ntoi(e, str.data, str.size, &val.int32_val)) {
if (d->options & UPB_JSONDEC_IGNOREUNKNOWN) {
val.int32_val = 0;
} else {
jsondec_errf(d, "Unknown enumerator: '" UPB_STRVIEW_FORMAT "'",
UPB_STRVIEW_ARGS(str));
}
}
return val;
}
case JD_NULL: {
if (jsondec_isnullvalue(f)) {
upb_msgval val;
jsondec_null(d);
val.int32_val = 0;
} else {
jsondec_errf(d, "Unknown enumerator: '" UPB_STRVIEW_FORMAT "'",
UPB_STRVIEW_ARGS(str));
return val;
}
}
return val;
} else {
return jsondec_int(d, f);
/* Fallthrough. */
default:
return jsondec_int(d, f);
}
}
@ -867,12 +894,6 @@ static upb_msgval jsondec_msg(jsondec *d, const upb_fielddef *f) {
return val;
}
static bool jsondec_isvalue(const upb_fielddef *f) {
return upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
upb_msgdef_wellknowntype(upb_fielddef_msgsubdef(f)) ==
UPB_WELLKNOWN_VALUE;
}
static void jsondec_field(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
upb_strview name;
const upb_fielddef *f;
@ -891,7 +912,7 @@ static void jsondec_field(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
return;
}
if (upb_fielddef_containingoneof(f) &&
if (upb_fielddef_realcontainingoneof(f) &&
upb_msg_whichoneof(msg, upb_fielddef_containingoneof(f))) {
jsondec_err(d, "More than one field for this oneof.");
}
@ -1078,6 +1099,7 @@ static void jsondec_duration(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
upb_strview str = jsondec_string(d);
const char *ptr = str.data;
const char *end = ptr + str.size;
const int64_t max = (uint64_t)3652500 * 86400;
/* "3.000000001s", "3s", etc. */
ptr = jsondec_buftoint64(d, ptr, end, &seconds.int64_val);
@ -1087,7 +1109,7 @@ static void jsondec_duration(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
jsondec_err(d, "Malformed duration");
}
if (seconds.int64_val < -315576000000LL || seconds.int64_val > 315576000000LL) {
if (seconds.int64_val < -max || seconds.int64_val > max) {
jsondec_err(d, "Duration out of range");
}
@ -1414,7 +1436,7 @@ bool upb_json_decode(const char *buf, size_t size, upb_msg *msg,
d.debug_field = NULL;
d.is_first = false;
if (setjmp(d.err)) return false;
if (UPB_SETJMP(d.err)) return false;
jsondec_tomsg(&d, msg, m);
return true;

@ -4,14 +4,16 @@
#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include "upb/decode.h"
#include "upb/reflection.h"
/* Must be last. */
#include "upb/port_def.inc"
typedef struct {
@ -76,7 +78,7 @@ static void jsonenc_printf(jsonenc *e, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
n = _upb_vsnprintf(e->ptr, have, fmt, args);
n = vsnprintf(e->ptr, have, fmt, args);
va_end(args);
if (UPB_LIKELY(have > n)) {
@ -167,12 +169,17 @@ static void jsonenc_duration(jsonenc *e, const upb_msg *msg, const upb_msgdef *m
static void jsonenc_enum(int32_t val, const upb_fielddef *f, jsonenc *e) {
const upb_enumdef *e_def = upb_fielddef_enumsubdef(f);
const char *name = upb_enumdef_iton(e_def, val);
if (name) {
jsonenc_printf(e, "\"%s\"", name);
if (strcmp(upb_enumdef_fullname(e_def), "google.protobuf.NullValue") == 0) {
jsonenc_putstr(e, "null");
} else {
jsonenc_printf(e, "%" PRId32, val);
const char *name = upb_enumdef_iton(e_def, val);
if (name) {
jsonenc_printf(e, "\"%s\"", name);
} else {
jsonenc_printf(e, "%" PRId32, val);
}
}
}
@ -263,9 +270,9 @@ static void jsonenc_string(jsonenc *e, upb_strview str) {
}
static void jsonenc_double(jsonenc *e, const char *fmt, double val) {
if (val == UPB_INFINITY) {
if (val == INFINITY) {
jsonenc_putstr(e, "\"Infinity\"");
} else if (val == -UPB_INFINITY) {
} else if (val == -INFINITY) {
jsonenc_putstr(e, "\"-Infinity\"");
} else if (val != val) {
jsonenc_putstr(e, "\"NaN\"");
@ -587,7 +594,7 @@ static void jsonenc_mapkey(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
static void jsonenc_array(jsonenc *e, const upb_array *arr,
const upb_fielddef *f) {
size_t i;
size_t size = upb_array_size(arr);
size_t size = arr ? upb_array_size(arr) : 0;
bool first = true;
jsonenc_putstr(e, "[");
@ -609,10 +616,12 @@ static void jsonenc_map(jsonenc *e, const upb_map *map, const upb_fielddef *f) {
jsonenc_putstr(e, "{");
while (upb_mapiter_next(map, &iter)) {
jsonenc_putsep(e, ",", &first);
jsonenc_mapkey(e, upb_mapiter_key(map, iter), key_f);
jsonenc_scalar(e, upb_mapiter_value(map, iter), val_f);
if (map) {
while (upb_mapiter_next(map, &iter)) {
jsonenc_putsep(e, ",", &first);
jsonenc_mapkey(e, upb_mapiter_key(map, iter), key_f);
jsonenc_scalar(e, upb_mapiter_value(map, iter), val_f);
}
}
jsonenc_putstr(e, "}");
@ -648,11 +657,13 @@ static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
if (e->options & UPB_JSONENC_EMITDEFAULTS) {
/* Iterate over all fields. */
upb_msg_field_iter i;
for (upb_msg_field_begin(&i, m); !upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
f = upb_msg_iter_field(&i);
jsonenc_fieldval(e, f, upb_msg_get(msg, f), &first);
int i = 0;
int n = upb_msgdef_fieldcount(m);
for (i = 0; i < n; i++) {
f = upb_msgdef_field(m, i);
if (!upb_fielddef_haspresence(f) || upb_msg_has(msg, f)) {
jsonenc_fieldval(e, f, upb_msg_get(msg, f), &first);
}
}
} else {
/* Iterate over non-empty fields. */

@ -9,7 +9,7 @@ extern "C" {
#endif
enum {
/* When set, emits 0/default values. TOOD(haberman): proto3 only? */
/* When set, emits 0/default values. TODO(haberman): proto3 only? */
UPB_JSONENC_EMITDEFAULTS = 1,
/* When set, use normal (snake_caes) field names instead of JSON (camelCase)

@ -7,106 +7,67 @@
/** upb_msg *******************************************************************/
static const char _upb_fieldtype_to_sizelg2[12] = {
0,
0, /* UPB_TYPE_BOOL */
2, /* UPB_TYPE_FLOAT */
2, /* UPB_TYPE_INT32 */
2, /* UPB_TYPE_UINT32 */
2, /* UPB_TYPE_ENUM */
UPB_SIZE(2, 3), /* UPB_TYPE_MESSAGE */
3, /* UPB_TYPE_DOUBLE */
3, /* UPB_TYPE_INT64 */
3, /* UPB_TYPE_UINT64 */
UPB_SIZE(3, 4), /* UPB_TYPE_STRING */
UPB_SIZE(3, 4), /* UPB_TYPE_BYTES */
};
static uintptr_t tag_arrptr(void* ptr, int elem_size_lg2) {
UPB_ASSERT(elem_size_lg2 <= 4);
return (uintptr_t)ptr | elem_size_lg2;
}
static int upb_msg_internalsize(const upb_msglayout *l) {
return sizeof(upb_msg_internal) - l->extendable * sizeof(void *);
}
static size_t upb_msg_sizeof(const upb_msglayout *l) {
return l->size + upb_msg_internalsize(l);
}
static const size_t overhead = sizeof(upb_msg_internal);
static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) {
ptrdiff_t size = sizeof(upb_msg_internal);
return UPB_PTR_AT(msg, -size, upb_msg_internal);
return (upb_msg_internal*)((char*)msg - size);
}
static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) {
return (upb_msg_internal*)upb_msg_getinternal_const(msg);
upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a) {
return _upb_msg_new_inl(l, a);
}
void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l) {
ptrdiff_t internal = upb_msg_internalsize(l);
void *mem = UPB_PTR_AT(msg, -internal, char);
memset(mem, 0, l->size + internal);
}
upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a) {
void *mem = upb_arena_malloc(a, upb_msg_sizeof(l));
upb_msg *msg;
if (!mem) {
return NULL;
}
msg = UPB_PTR_AT(mem, upb_msg_internalsize(l), upb_msg);
_upb_msg_clear(msg, l);
return msg;
void *mem = UPB_PTR_AT(msg, -sizeof(upb_msg_internal), char);
memset(mem, 0, upb_msg_sizeof(l));
}
bool _upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
upb_arena *arena) {
upb_msg_internal *in = upb_msg_getinternal(msg);
if (len > in->unknown_size - in->unknown_len) {
upb_alloc *alloc = upb_arena_alloc(arena);
size_t need = in->unknown_size + len;
size_t newsize = UPB_MAX(in->unknown_size * 2, need);
void *mem = upb_realloc(alloc, in->unknown, in->unknown_size, newsize);
if (!mem) return false;
in->unknown = mem;
in->unknown_size = newsize;
if (!in->unknown) {
size_t size = 128;
while (size < len) size *= 2;
in->unknown = upb_arena_malloc(arena, size + overhead);
if (!in->unknown) return false;
in->unknown->size = size;
in->unknown->len = 0;
} else if (in->unknown->size - in->unknown->len < len) {
size_t need = in->unknown->len + len;
size_t size = in->unknown->size;
while (size < need) size *= 2;
in->unknown = upb_arena_realloc(
arena, in->unknown, in->unknown->size + overhead, size + overhead);
if (!in->unknown) return false;
in->unknown->size = size;
}
memcpy(in->unknown + in->unknown_len, data, len);
in->unknown_len += len;
memcpy(UPB_PTR_AT(in->unknown + 1, in->unknown->len, char), data, len);
in->unknown->len += len;
return true;
}
void _upb_msg_discardunknown_shallow(upb_msg *msg) {
upb_msg_internal *in = upb_msg_getinternal(msg);
in->unknown_len = 0;
if (in->unknown) {
in->unknown->len = 0;
}
}
const char *upb_msg_getunknown(const upb_msg *msg, size_t *len) {
const upb_msg_internal *in = upb_msg_getinternal_const(msg);
*len = in->unknown_len;
return in->unknown;
}
/** upb_array *****************************************************************/
upb_array *_upb_array_new(upb_arena *a, upb_fieldtype_t type) {
upb_array *arr = upb_arena_malloc(a, sizeof(upb_array));
if (!arr) {
if (in->unknown) {
*len = in->unknown->len;
return (char*)(in->unknown + 1);
} else {
*len = 0;
return NULL;
}
arr->data = tag_arrptr(NULL, _upb_fieldtype_to_sizelg2[type]);
arr->len = 0;
arr->size = 0;
return arr;
}
/** upb_array *****************************************************************/
bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena) {
size_t new_size = UPB_MAX(arr->size, 4);
int elem_size_lg2 = arr->data & 7;
@ -124,16 +85,16 @@ bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena) {
return false;
}
arr->data = tag_arrptr(ptr, elem_size_lg2);
arr->data = _upb_tag_arrptr(ptr, elem_size_lg2);
arr->size = new_size;
return true;
}
static upb_array *getorcreate_array(upb_array **arr_ptr, upb_fieldtype_t type,
static upb_array *getorcreate_array(upb_array **arr_ptr, int elem_size_lg2,
upb_arena *arena) {
upb_array *arr = *arr_ptr;
if (!arr) {
arr = _upb_array_new(arena, type);
arr = _upb_array_new(arena, 4, elem_size_lg2);
if (!arr) return NULL;
*arr_ptr = arr;
}
@ -141,22 +102,25 @@ static upb_array *getorcreate_array(upb_array **arr_ptr, upb_fieldtype_t type,
}
void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
upb_fieldtype_t type, upb_arena *arena) {
upb_array *arr = getorcreate_array(arr_ptr, type, arena);
return arr && _upb_array_resize(arr, size, arena) ? _upb_array_ptr(arr) : NULL;
int elem_size_lg2, upb_arena *arena) {
upb_array *arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
return arr && _upb_array_resize(arr, size, arena) ? _upb_array_ptr(arr)
: NULL;
}
bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
upb_fieldtype_t type, upb_arena *arena) {
upb_array *arr = getorcreate_array(arr_ptr, type, arena);
size_t elem = arr->len;
int lg2 = _upb_fieldtype_to_sizelg2[type];
char *data;
int elem_size_lg2, upb_arena *arena) {
upb_array *arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
if (!arr) return false;
size_t elems = arr->len;
if (!arr || !_upb_array_resize(arr, elem + 1, arena)) return false;
if (!_upb_array_resize(arr, elems + 1, arena)) {
return false;
}
data = _upb_array_ptr(arr);
memcpy(data + (elem << lg2), value, 1 << lg2);
char *data = _upb_array_ptr(arr);
memcpy(data + (elems << elem_size_lg2), value, 1 << elem_size_lg2);
return true;
}
@ -169,9 +133,124 @@ upb_map *_upb_map_new(upb_arena *a, size_t key_size, size_t value_size) {
return NULL;
}
upb_strtable_init2(&map->table, UPB_CTYPE_INT32, upb_arena_alloc(a));
upb_strtable_init2(&map->table, UPB_CTYPE_INT32, 4, upb_arena_alloc(a));
map->key_size = key_size;
map->val_size = value_size;
return map;
}
static void _upb_mapsorter_getkeys(const void *_a, const void *_b, void *a_key,
void *b_key, size_t size) {
const upb_tabent *const*a = _a;
const upb_tabent *const*b = _b;
upb_strview a_tabkey = upb_tabstrview((*a)->key);
upb_strview b_tabkey = upb_tabstrview((*b)->key);
_upb_map_fromkey(a_tabkey, a_key, size);
_upb_map_fromkey(b_tabkey, b_key, size);
}
static int _upb_mapsorter_cmpi64(const void *_a, const void *_b) {
int64_t a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, 8);
return a - b;
}
static int _upb_mapsorter_cmpu64(const void *_a, const void *_b) {
uint64_t a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, 8);
return a - b;
}
static int _upb_mapsorter_cmpi32(const void *_a, const void *_b) {
int32_t a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, 4);
return a - b;
}
static int _upb_mapsorter_cmpu32(const void *_a, const void *_b) {
uint32_t a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, 4);
return a - b;
}
static int _upb_mapsorter_cmpbool(const void *_a, const void *_b) {
bool a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, 1);
return a - b;
}
static int _upb_mapsorter_cmpstr(const void *_a, const void *_b) {
upb_strview a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, UPB_MAPTYPE_STRING);
size_t common_size = UPB_MIN(a.size, b.size);
int cmp = memcmp(a.data, b.data, common_size);
if (cmp) return cmp;
return a.size - b.size;
}
bool _upb_mapsorter_pushmap(_upb_mapsorter *s, upb_descriptortype_t key_type,
const upb_map *map, _upb_sortedmap *sorted) {
int map_size = _upb_map_size(map);
sorted->start = s->size;
sorted->pos = sorted->start;
sorted->end = sorted->start + map_size;
/* Grow s->entries if necessary. */
if (sorted->end > s->cap) {
s->cap = _upb_lg2ceilsize(sorted->end);
s->entries = realloc(s->entries, s->cap * sizeof(*s->entries));
if (!s->entries) return false;
}
s->size = sorted->end;
/* Copy non-empty entries from the table to s->entries. */
upb_tabent const**dst = &s->entries[sorted->start];
const upb_tabent *src = map->table.t.entries;
const upb_tabent *end = src + upb_table_size(&map->table.t);
for (; src < end; src++) {
if (!upb_tabent_isempty(src)) {
*dst = src;
dst++;
}
}
UPB_ASSERT(dst == &s->entries[sorted->end]);
/* Sort entries according to the key type. */
int (*compar)(const void *, const void *);
switch (key_type) {
case UPB_DESCRIPTOR_TYPE_INT64:
case UPB_DESCRIPTOR_TYPE_SFIXED64:
case UPB_DESCRIPTOR_TYPE_SINT64:
compar = _upb_mapsorter_cmpi64;
break;
case UPB_DESCRIPTOR_TYPE_UINT64:
case UPB_DESCRIPTOR_TYPE_FIXED64:
compar = _upb_mapsorter_cmpu64;
break;
case UPB_DESCRIPTOR_TYPE_INT32:
case UPB_DESCRIPTOR_TYPE_SINT32:
case UPB_DESCRIPTOR_TYPE_SFIXED32:
case UPB_DESCRIPTOR_TYPE_ENUM:
compar = _upb_mapsorter_cmpi32;
break;
case UPB_DESCRIPTOR_TYPE_UINT32:
case UPB_DESCRIPTOR_TYPE_FIXED32:
compar = _upb_mapsorter_cmpu32;
break;
case UPB_DESCRIPTOR_TYPE_BOOL:
compar = _upb_mapsorter_cmpbool;
break;
case UPB_DESCRIPTOR_TYPE_STRING:
compar = _upb_mapsorter_cmpstr;
break;
default:
UPB_UNREACHABLE();
}
qsort(&s->entries[sorted->start], map_size, sizeof(*s->entries), compar);
return true;
}

@ -9,11 +9,13 @@
#define UPB_MSG_H_
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "upb/table.int.h"
#include "upb/upb.h"
/* Must be last. */
#include "upb/port_def.inc"
#ifdef __cplusplus
@ -46,6 +48,18 @@ typedef struct {
uint8_t label; /* google.protobuf.Label or _UPB_LABEL_* above. */
} upb_msglayout_field;
struct upb_decstate;
struct upb_msglayout;
typedef const char *_upb_field_parser(struct upb_decstate *d, const char *ptr,
upb_msg *msg, intptr_t table,
uint64_t hasbits, uint64_t data);
typedef struct {
uint64_t field_data;
_upb_field_parser *field_parser;
} _upb_fasttable_entry;
typedef struct upb_msglayout {
const struct upb_msglayout *const* submsgs;
const upb_msglayout_field *fields;
@ -54,6 +68,10 @@ typedef struct upb_msglayout {
uint16_t size;
uint16_t field_count;
bool extendable;
uint8_t table_mask;
/* To constant-initialize the tables of variable length, we need a flexible
* array member, and we need to compile in C99 mode. */
_upb_fasttable_entry fasttable[];
} upb_msglayout;
/** upb_msg *******************************************************************/
@ -62,25 +80,42 @@ typedef struct upb_msglayout {
* compatibility. We put these before the user's data. The user's upb_msg*
* points after the upb_msg_internal. */
/* Used when a message is not extendable. */
typedef struct {
char *unknown;
size_t unknown_len;
size_t unknown_size;
} upb_msg_internal;
uint32_t len;
uint32_t size;
/* Data follows. */
} upb_msg_unknowndata;
/* Used when a message is extendable. */
/* Used when a message is not extendable. */
typedef struct {
upb_inttable *extdict;
upb_msg_internal base;
} upb_msg_internal_withext;
upb_msg_unknowndata *unknown;
} upb_msg_internal;
/* Maps upb_fieldtype_t -> memory size. */
extern char _upb_fieldtype_to_size[12];
UPB_INLINE size_t upb_msg_sizeof(const upb_msglayout *l) {
return l->size + sizeof(upb_msg_internal);
}
UPB_INLINE upb_msg *_upb_msg_new_inl(const upb_msglayout *l, upb_arena *a) {
size_t size = upb_msg_sizeof(l);
void *mem = upb_arena_malloc(a, size);
upb_msg *msg;
if (UPB_UNLIKELY(!mem)) return NULL;
msg = UPB_PTR_AT(mem, sizeof(upb_msg_internal), upb_msg);
memset(mem, 0, size);
return msg;
}
/* Creates a new messages with the given layout on the given arena. */
upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a);
UPB_INLINE upb_msg_internal *upb_msg_getinternal(upb_msg *msg) {
ptrdiff_t size = sizeof(upb_msg_internal);
return (upb_msg_internal*)((char*)msg - size);
}
/* Clears the given message. */
void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l);
@ -173,27 +208,49 @@ typedef struct {
uintptr_t data; /* Tagged ptr: low 3 bits of ptr are lg2(elem size). */
size_t len; /* Measured in elements. */
size_t size; /* Measured in elements. */
uint64_t junk;
} upb_array;
UPB_INLINE const void *_upb_array_constptr(const upb_array *arr) {
UPB_ASSERT((arr->data & 7) <= 4);
return (void*)(arr->data & ~(uintptr_t)7);
}
UPB_INLINE uintptr_t _upb_array_tagptr(void* ptr, int elem_size_lg2) {
UPB_ASSERT(elem_size_lg2 <= 4);
return (uintptr_t)ptr | elem_size_lg2;
}
UPB_INLINE void *_upb_array_ptr(upb_array *arr) {
return (void*)_upb_array_constptr(arr);
}
/* Creates a new array on the given arena. */
upb_array *_upb_array_new(upb_arena *a, upb_fieldtype_t type);
UPB_INLINE uintptr_t _upb_tag_arrptr(void* ptr, int elem_size_lg2) {
UPB_ASSERT(elem_size_lg2 <= 4);
UPB_ASSERT(((uintptr_t)ptr & 7) == 0);
return (uintptr_t)ptr | (unsigned)elem_size_lg2;
}
UPB_INLINE upb_array *_upb_array_new(upb_arena *a, size_t init_size,
int elem_size_lg2) {
const size_t arr_size = UPB_ALIGN_UP(sizeof(upb_array), 8);
const size_t bytes = sizeof(upb_array) + (init_size << elem_size_lg2);
upb_array *arr = (upb_array*)upb_arena_malloc(a, bytes);
if (!arr) return NULL;
arr->data = _upb_tag_arrptr(UPB_PTR_AT(arr, arr_size, void), elem_size_lg2);
arr->len = 0;
arr->size = init_size;
return arr;
}
/* Resizes the capacity of the array to be at least min_size. */
bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena);
/* Fallback functions for when the accessors require a resize. */
void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
upb_fieldtype_t type, upb_arena *arena);
int elem_size_lg2, upb_arena *arena);
bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
upb_fieldtype_t type, upb_arena *arena);
int elem_size_lg2, upb_arena *arena);
UPB_INLINE bool _upb_array_reserve(upb_array *arr, size_t size,
upb_arena *arena) {
@ -232,29 +289,28 @@ UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs,
}
}
UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
upb_fieldtype_t type,
upb_arena *arena) {
upb_array **arr_ptr = PTR_AT(msg, ofs, upb_array*);
UPB_INLINE void *_upb_array_resize_accessor2(void *msg, size_t ofs, size_t size,
int elem_size_lg2,
upb_arena *arena) {
upb_array **arr_ptr = PTR_AT(msg, ofs, upb_array *);
upb_array *arr = *arr_ptr;
if (!arr || arr->size < size) {
return _upb_array_resize_fallback(arr_ptr, size, type, arena);
return _upb_array_resize_fallback(arr_ptr, size, elem_size_lg2, arena);
}
arr->len = size;
return _upb_array_ptr(arr);
}
UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
size_t elem_size,
upb_fieldtype_t type,
const void *value,
upb_arena *arena) {
upb_array **arr_ptr = PTR_AT(msg, ofs, upb_array*);
UPB_INLINE bool _upb_array_append_accessor2(void *msg, size_t ofs,
int elem_size_lg2,
const void *value,
upb_arena *arena) {
upb_array **arr_ptr = PTR_AT(msg, ofs, upb_array *);
size_t elem_size = 1 << elem_size_lg2;
upb_array *arr = *arr_ptr;
void* ptr;
void *ptr;
if (!arr || arr->len == arr->size) {
return _upb_array_append_fallback(arr_ptr, value, type, arena);
return _upb_array_append_fallback(arr_ptr, value, elem_size_lg2, arena);
}
ptr = _upb_array_ptr(arr);
memcpy(PTR_AT(ptr, arr->len * elem_size, char), value, elem_size);
@ -262,6 +318,42 @@ UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
return true;
}
/* Used by old generated code, remove once all code has been regenerated. */
UPB_INLINE int _upb_sizelg2(upb_fieldtype_t type) {
switch (type) {
case UPB_TYPE_BOOL:
return 0;
case UPB_TYPE_FLOAT:
case UPB_TYPE_INT32:
case UPB_TYPE_UINT32:
case UPB_TYPE_ENUM:
return 2;
case UPB_TYPE_MESSAGE:
return UPB_SIZE(2, 3);
case UPB_TYPE_DOUBLE:
case UPB_TYPE_INT64:
case UPB_TYPE_UINT64:
return 3;
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
return UPB_SIZE(3, 4);
}
UPB_UNREACHABLE();
}
UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
upb_fieldtype_t type,
upb_arena *arena) {
return _upb_array_resize_accessor2(msg, ofs, size, _upb_sizelg2(type), arena);
}
UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
size_t elem_size, upb_fieldtype_t type,
const void *value,
upb_arena *arena) {
(void)elem_size;
return _upb_array_append_accessor2(msg, ofs, _upb_sizelg2(type), value,
arena);
}
/** upb_map *******************************************************************/
/* Right now we use strmaps for everything. We'll likely want to use
@ -318,17 +410,17 @@ UPB_INLINE void _upb_map_fromkey(upb_strview key, void* out, size_t size) {
}
}
UPB_INLINE upb_value _upb_map_tovalue(const void *val, size_t size,
upb_arena *a) {
upb_value ret = {0};
UPB_INLINE bool _upb_map_tovalue(const void *val, size_t size, upb_value *msgval,
upb_arena *a) {
if (size == UPB_MAPTYPE_STRING) {
upb_strview *strp = (upb_strview*)upb_arena_malloc(a, sizeof(*strp));
if (!strp) return false;
*strp = *(upb_strview*)val;
ret = upb_value_ptr(strp);
*msgval = upb_value_ptr(strp);
} else {
memcpy(&ret, val, size);
memcpy(msgval, val, size);
}
return ret;
return true;
}
UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size) {
@ -370,7 +462,8 @@ UPB_INLINE void* _upb_map_next(const upb_map *map, size_t *iter) {
UPB_INLINE bool _upb_map_set(upb_map *map, const void *key, size_t key_size,
void *val, size_t val_size, upb_arena *arena) {
upb_strview strkey = _upb_map_tokey(key, key_size);
upb_value tabval = _upb_map_tovalue(val, val_size, arena);
upb_value tabval = {0};
if (!_upb_map_tovalue(val, val_size, &tabval, arena)) return false;
upb_alloc *a = upb_arena_alloc(arena);
/* TODO(haberman): add overwrite operation to minimize number of lookups. */
@ -462,6 +555,53 @@ UPB_INLINE void _upb_msg_map_set_value(void* msg, const void* val, size_t size)
}
}
/** _upb_mapsorter *************************************************************/
/* _upb_mapsorter sorts maps and provides ordered iteration over the entries.
* Since maps can be recursive (map values can be messages which contain other maps).
* _upb_mapsorter can contain a stack of maps. */
typedef struct {
upb_tabent const**entries;
int size;
int cap;
} _upb_mapsorter;
typedef struct {
int start;
int pos;
int end;
} _upb_sortedmap;
UPB_INLINE void _upb_mapsorter_init(_upb_mapsorter *s) {
s->entries = NULL;
s->size = 0;
s->cap = 0;
}
UPB_INLINE void _upb_mapsorter_destroy(_upb_mapsorter *s) {
if (s->entries) free(s->entries);
}
bool _upb_mapsorter_pushmap(_upb_mapsorter *s, upb_descriptortype_t key_type,
const upb_map *map, _upb_sortedmap *sorted);
UPB_INLINE void _upb_mapsorter_popmap(_upb_mapsorter *s, _upb_sortedmap *sorted) {
s->size = sorted->start;
}
UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter *s, const upb_map *map,
_upb_sortedmap *sorted,
upb_map_entry *ent) {
if (sorted->pos == sorted->end) return false;
const upb_tabent *tabent = s->entries[sorted->pos++];
upb_strview key = upb_tabstrview(tabent->key);
_upb_map_fromkey(key, &ent->k, map->key_size);
upb_value val = {tabent->val.val};
_upb_map_fromvalue(val, &ent->v, map->val_size);
return true;
}
#undef PTR_AT
#ifdef __cplusplus

@ -701,7 +701,7 @@ static void compile_method(compiler *c, upb_pbdecodermethod *method) {
const upb_handlers *h;
const upb_msgdef *md;
uint32_t* start_pc;
upb_msg_field_iter i;
int i, n;
upb_value val;
UPB_ASSERT(method);
@ -718,10 +718,9 @@ static void compile_method(compiler *c, upb_pbdecodermethod *method) {
putsel(c, OP_STARTMSG, UPB_STARTMSG_SELECTOR, h);
label(c, LABEL_FIELD);
start_pc = c->pc;
for(upb_msg_field_begin(&i, md);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
const upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(md);
for(i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(md, i);
upb_fieldtype_t type = upb_fielddef_type(f);
if (type == UPB_TYPE_MESSAGE && !(haslazyhandlers(h, f) && c->lazy)) {
@ -765,7 +764,7 @@ static void compile_method(compiler *c, upb_pbdecodermethod *method) {
* Generates a new method for every destination handlers reachable from "h". */
static void find_methods(compiler *c, const upb_handlers *h) {
upb_value v;
upb_msg_field_iter i;
int i, n;
const upb_msgdef *md;
upb_pbdecodermethod *method;
@ -777,10 +776,9 @@ static void find_methods(compiler *c, const upb_handlers *h) {
/* Find submethods. */
md = upb_handlers_msgdef(h);
for(upb_msg_field_begin(&i, md);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
const upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(md);
for (i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(md, i);
const upb_handlers *sub_h;
if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
(sub_h = upb_handlers_getsubhandlers(h, f)) != NULL) {

@ -190,7 +190,7 @@ static bool commit(upb_pb_encoder *e) {
}
/* Writes the given bytes to the buffer, handling reserve/advance. */
static bool encode_bytes(upb_pb_encoder *e, const void *data, size_t len) {
static bool encode_bytesval(upb_pb_encoder *e, const void *data, size_t len) {
if (!reserve(e, len)) {
return false;
}
@ -309,24 +309,24 @@ static void new_tag(upb_handlers *h, const upb_fielddef *f, upb_wiretype_t wt,
upb_handlers_addcleanup(h, tag, upb_gfree);
}
static bool encode_tag(upb_pb_encoder *e, const tag_t *tag) {
return encode_bytes(e, tag->tag, tag->bytes);
static bool encode_tagval(upb_pb_encoder *e, const tag_t *tag) {
return encode_bytesval(e, tag->tag, tag->bytes);
}
/* encoding of wire types *****************************************************/
static bool encode_fixed64(upb_pb_encoder *e, uint64_t val) {
static bool doencode_fixed64(upb_pb_encoder *e, uint64_t val) {
/* TODO(haberman): byte-swap for big endian. */
return encode_bytes(e, &val, sizeof(uint64_t));
return encode_bytesval(e, &val, sizeof(uint64_t));
}
static bool encode_fixed32(upb_pb_encoder *e, uint32_t val) {
static bool doencode_fixed32(upb_pb_encoder *e, uint32_t val) {
/* TODO(haberman): byte-swap for big endian. */
return encode_bytes(e, &val, sizeof(uint32_t));
return encode_bytesval(e, &val, sizeof(uint32_t));
}
static bool encode_varint(upb_pb_encoder *e, uint64_t val) {
static bool doencode_varint(upb_pb_encoder *e, uint64_t val) {
if (!reserve(e, UPB_PB_VARINT_MAX_LEN)) {
return false;
}
@ -370,14 +370,14 @@ static bool endmsg(void *c, const void *hd, upb_status *status) {
}
static void *encode_startdelimfield(void *c, const void *hd) {
bool ok = encode_tag(c, hd) && commit(c) && start_delim(c);
bool ok = encode_tagval(c, hd) && commit(c) && start_delim(c);
return ok ? c : UPB_BREAK;
}
static bool encode_unknown(void *c, const void *hd, const char *buf,
size_t len) {
UPB_UNUSED(hd);
return encode_bytes(c, buf, len) && commit(c);
return encode_bytesval(c, buf, len) && commit(c);
}
static bool encode_enddelimfield(void *c, const void *hd) {
@ -386,11 +386,11 @@ static bool encode_enddelimfield(void *c, const void *hd) {
}
static void *encode_startgroup(void *c, const void *hd) {
return (encode_tag(c, hd) && commit(c)) ? c : UPB_BREAK;
return (encode_tagval(c, hd) && commit(c)) ? c : UPB_BREAK;
}
static bool encode_endgroup(void *c, const void *hd) {
return encode_tag(c, hd) && commit(c);
return encode_tagval(c, hd) && commit(c);
}
static void *encode_startstr(void *c, const void *hd, size_t size_hint) {
@ -402,32 +402,32 @@ static size_t encode_strbuf(void *c, const void *hd, const char *buf,
size_t len, const upb_bufhandle *h) {
UPB_UNUSED(hd);
UPB_UNUSED(h);
return encode_bytes(c, buf, len) ? len : 0;
return encode_bytesval(c, buf, len) ? len : 0;
}
#define T(type, ctype, convert, encode) \
static bool encode_scalar_##type(void *e, const void *hd, ctype val) { \
return encode_tag(e, hd) && encode(e, (convert)(val)) && commit(e); \
} \
static bool encode_packed_##type(void *e, const void *hd, ctype val) { \
UPB_UNUSED(hd); \
return encode(e, (convert)(val)); \
#define T(type, ctype, convert, encode) \
static bool encode_scalar_##type(void *e, const void *hd, ctype val) { \
return encode_tagval(e, hd) && encode(e, (convert)(val)) && commit(e); \
} \
static bool encode_packed_##type(void *e, const void *hd, ctype val) { \
UPB_UNUSED(hd); \
return encode(e, (convert)(val)); \
}
T(double, double, dbl2uint64, encode_fixed64)
T(float, float, flt2uint32, encode_fixed32)
T(int64, int64_t, uint64_t, encode_varint)
T(int32, int32_t, int64_t, encode_varint)
T(fixed64, uint64_t, uint64_t, encode_fixed64)
T(fixed32, uint32_t, uint32_t, encode_fixed32)
T(bool, bool, bool, encode_varint)
T(uint32, uint32_t, uint32_t, encode_varint)
T(uint64, uint64_t, uint64_t, encode_varint)
T(enum, int32_t, uint32_t, encode_varint)
T(sfixed32, int32_t, uint32_t, encode_fixed32)
T(sfixed64, int64_t, uint64_t, encode_fixed64)
T(sint32, int32_t, upb_zzenc_32, encode_varint)
T(sint64, int64_t, upb_zzenc_64, encode_varint)
T(double, double, dbl2uint64, doencode_fixed64)
T(float, float, flt2uint32, doencode_fixed32)
T(int64, int64_t, uint64_t, doencode_varint)
T(int32, int32_t, int64_t, doencode_varint)
T(fixed64, uint64_t, uint64_t, doencode_fixed64)
T(fixed32, uint32_t, uint32_t, doencode_fixed32)
T(bool, bool, bool, doencode_varint)
T(uint32, uint32_t, uint32_t, doencode_varint)
T(uint64, uint64_t, uint64_t, doencode_varint)
T(enum, int32_t, uint32_t, doencode_varint)
T(sfixed32, int32_t, uint32_t, doencode_fixed32)
T(sfixed64, int64_t, uint64_t, doencode_fixed64)
T(sint32, int32_t, upb_zzenc_32, doencode_varint)
T(sint64, int64_t, upb_zzenc_64, doencode_varint)
#undef T
@ -437,7 +437,7 @@ T(sint64, int64_t, upb_zzenc_64, encode_varint)
#include <stdio.h>
static void newhandlers_callback(const void *closure, upb_handlers *h) {
const upb_msgdef *m;
upb_msg_field_iter i;
int i, n;
UPB_UNUSED(closure);
@ -446,10 +446,9 @@ static void newhandlers_callback(const void *closure, upb_handlers *h) {
upb_handlers_setunknown(h, encode_unknown, NULL);
m = upb_handlers_msgdef(h);
for(upb_msg_field_begin(&i, m);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
const upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(m);
for(i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(m, i);
bool packed = upb_fielddef_isseq(f) && upb_fielddef_isprimitive(f) &&
upb_fielddef_packed(f);
upb_handlerattr attr = UPB_HANDLERATTR_INIT;

@ -105,8 +105,8 @@ bool putf(upb_textprinter *p, const char *fmt, ...) {
va_start(args, fmt);
/* Run once to get the length of the string. */
_upb_va_copy(args_copy, args);
len = _upb_vsnprintf(NULL, 0, fmt, args_copy);
va_copy(args_copy, args);
len = vsnprintf(NULL, 0, fmt, args_copy);
va_end(args_copy);
/* + 1 for NULL terminator (vsprintf() requires it even if we don't). */
@ -252,16 +252,15 @@ err:
static void onmreg(const void *c, upb_handlers *h) {
const upb_msgdef *m = upb_handlers_msgdef(h);
upb_msg_field_iter i;
int i, n;
UPB_UNUSED(c);
upb_handlers_setstartmsg(h, textprinter_startmsg, NULL);
upb_handlers_setendmsg(h, textprinter_endmsg, NULL);
for(upb_msg_field_begin(&i, m);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
upb_fielddef *f = upb_msg_iter_field(&i);
n = upb_msgdef_fieldcount(m);
for(i = 0; i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(m, i);
upb_handlerattr attr = UPB_HANDLERATTR_INIT;
attr.handler_data = f;
switch (upb_fielddef_type(f)) {

@ -150,9 +150,7 @@ UPB_INLINE uint64_t upb_vencode32(uint32_t val) {
uint64_t ret = 0;
UPB_ASSERT(bytes <= 5);
memcpy(&ret, buf, bytes);
#ifdef UPB_BIG_ENDIAN
ret = byteswap64(ret);
#endif
ret = _upb_be_swap64(ret);
UPB_ASSERT(ret <= 0xffffffffffU);
return ret;
}

@ -1,26 +0,0 @@
#include "upb/port_def.inc"
#ifdef UPB_MSVC_VSNPRINTF
/* Visual C++ earlier than 2015 doesn't have standard C99 snprintf and
* vsnprintf. To support them, missing functions are manually implemented
* using the existing secure functions. */
int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg) {
if (!s) {
return _vscprintf(format, arg);
}
int ret = _vsnprintf_s(s, n, _TRUNCATE, format, arg);
if (ret < 0) {
ret = _vscprintf(format, arg);
}
return ret;
}
int msvc_snprintf(char* s, size_t n, const char* format, ...) {
va_list arg;
va_start(arg, format);
int ret = msvc_vsnprintf(s, n, format, arg);
va_end(arg);
return ret;
}
#endif

@ -20,6 +20,13 @@
*
* This file is private and must not be included by users!
*/
#if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
(defined(__cplusplus) && __cplusplus >= 201103L) || \
(defined(_MSC_VER) && _MSC_VER >= 1900))
#error upb requires C99 or C++11 or MSVC >= 2015.
#endif
#include <stdint.h>
#include <stddef.h>
@ -68,66 +75,21 @@
#define UPB_UNLIKELY(x) (x)
#endif
/* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
* doesn't provide these preprocessor symbols. */
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#define UPB_BIG_ENDIAN
#endif
/* Macros for function attributes on compilers that support them. */
#ifdef __GNUC__
#define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
#define UPB_NOINLINE __attribute__((noinline))
#define UPB_NORETURN __attribute__((__noreturn__))
#elif defined(_MSC_VER)
#define UPB_NOINLINE
#define UPB_FORCEINLINE
#define UPB_NORETURN __declspec(noreturn)
#else /* !defined(__GNUC__) */
#define UPB_FORCEINLINE
#define UPB_NOINLINE
#define UPB_NORETURN
#endif
#if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
/* C99/C++11 versions. */
#include <stdio.h>
#define _upb_snprintf snprintf
#define _upb_vsnprintf vsnprintf
#define _upb_va_copy(a, b) va_copy(a, b)
#elif defined(_MSC_VER)
/* Microsoft C/C++ versions. */
#include <stdarg.h>
#include <stdio.h>
#if _MSC_VER < 1900
int msvc_snprintf(char* s, size_t n, const char* format, ...);
int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg);
#define UPB_MSVC_VSNPRINTF
#define _upb_snprintf msvc_snprintf
#define _upb_vsnprintf msvc_vsnprintf
#else
#define _upb_snprintf snprintf
#define _upb_vsnprintf vsnprintf
#endif
#define _upb_va_copy(a, b) va_copy(a, b)
#elif defined __GNUC__
/* A few hacky workarounds for functions not in C89.
* For internal use only!
* TODO(haberman): fix these by including our own implementations, or finding
* another workaround.
*/
#define _upb_snprintf __builtin_snprintf
#define _upb_vsnprintf __builtin_vsnprintf
#define _upb_va_copy(a, b) __va_copy(a, b)
#else
#error Need implementations of [v]snprintf and va_copy
#endif
#ifdef __cplusplus
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \
(defined(_MSC_VER) && _MSC_VER >= 1900)
/* C++11 is present */
#else
#error upb requires C++11 for C++ support
#endif
#endif
#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
@ -155,25 +117,76 @@ int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg);
#define UPB_ASSERT(expr) assert(expr)
#endif
/* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only
* exist in debug mode. This turns into regular assert. */
#define UPB_ASSERT_DEBUGVAR(expr) assert(expr)
#if defined(__GNUC__) || defined(__clang__)
#define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
#else
#define UPB_UNREACHABLE() do { assert(0); } while(0)
#endif
/* UPB_INFINITY representing floating-point positive infinity. */
#include <math.h>
#ifdef INFINITY
#define UPB_INFINITY INFINITY
/* UPB_SETJMP() / UPB_LONGJMP(): avoid setting/restoring signal mask. */
#ifdef __APPLE__
#define UPB_SETJMP(buf) _setjmp(buf)
#define UPB_LONGJMP(buf, val) _longjmp(buf, val)
#else
#define UPB_SETJMP(buf) setjmp(buf)
#define UPB_LONGJMP(buf, val) longjmp(buf, val)
#endif
/* Configure whether fasttable is switched on or not. *************************/
#if defined(__x86_64__) && defined(__GNUC__)
#define UPB_FASTTABLE_SUPPORTED 1
#else
#define UPB_FASTTABLE_SUPPORTED 0
#endif
/* define UPB_ENABLE_FASTTABLE to force fast table support.
* This is useful when we want to ensure we are really getting fasttable,
* for example for testing or benchmarking. */
#if defined(UPB_ENABLE_FASTTABLE)
#if !UPB_FASTTABLE_SUPPORTED
#error fasttable is x86-64 + Clang/GCC only
#endif
#define UPB_FASTTABLE 1
/* Define UPB_TRY_ENABLE_FASTTABLE to use fasttable if possible.
* This is useful for releasing code that might be used on multiple platforms,
* for example the PHP or Ruby C extensions. */
#elif defined(UPB_TRY_ENABLE_FASTTABLE)
#define UPB_FASTTABLE UPB_FASTTABLE_SUPPORTED
#else
#define UPB_INFINITY (1.0 / 0.0)
#define UPB_FASTTABLE 0
#endif
#ifdef NAN
#define UPB_NAN NAN
/* UPB_FASTTABLE_INIT() allows protos compiled for fasttable to gracefully
* degrade to non-fasttable if we are using UPB_TRY_ENABLE_FASTTABLE. */
#if !UPB_FASTTABLE && defined(UPB_TRY_ENABLE_FASTTABLE)
#define UPB_FASTTABLE_INIT(...)
#else
#define UPB_NAN (0.0 / 0.0)
#define UPB_FASTTABLE_INIT(...) __VA_ARGS__
#endif
#undef UPB_FASTTABLE_SUPPORTED
/* ASAN poisoning (for arena) *************************************************/
#if defined(__SANITIZE_ADDRESS__)
#define UPB_ASAN 1
#ifdef __cplusplus
extern "C" {
#endif
void __asan_poison_memory_region(void const volatile *addr, size_t size);
void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
#ifdef __cplusplus
} /* extern "C" */
#endif
#define UPB_POISON_MEMORY_REGION(addr, size) \
__asan_poison_memory_region((addr), (size))
#define UPB_UNPOISON_MEMORY_REGION(addr, size) \
__asan_unpoison_memory_region((addr), (size))
#else
#define UPB_ASAN 0
#define UPB_POISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size))
#define UPB_UNPOISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size))
#endif

@ -18,11 +18,7 @@
#undef UPB_UNUSED
#undef UPB_ASSUME
#undef UPB_ASSERT
#undef UPB_ASSERT_DEBUGVAR
#undef UPB_UNREACHABLE
#undef UPB_INFINITY
#undef UPB_NAN
#undef UPB_MSVC_VSNPRINTF
#undef _upb_snprintf
#undef _upb_vsnprintf
#undef _upb_va_copy
#undef UPB_POISON_MEMORY_REGION
#undef UPB_UNPOISON_MEMORY_REGION
#undef UPB_ASAN

@ -48,6 +48,21 @@ static char _upb_fieldtype_to_mapsize[12] = {
0, /* UPB_TYPE_BYTES */
};
static const char _upb_fieldtype_to_sizelg2[12] = {
0,
0, /* UPB_TYPE_BOOL */
2, /* UPB_TYPE_FLOAT */
2, /* UPB_TYPE_INT32 */
2, /* UPB_TYPE_UINT32 */
2, /* UPB_TYPE_ENUM */
UPB_SIZE(2, 3), /* UPB_TYPE_MESSAGE */
3, /* UPB_TYPE_DOUBLE */
3, /* UPB_TYPE_INT64 */
3, /* UPB_TYPE_UINT64 */
UPB_SIZE(3, 4), /* UPB_TYPE_STRING */
UPB_SIZE(3, 4), /* UPB_TYPE_BYTES */
};
/** upb_msg *******************************************************************/
upb_msg *upb_msg_new(const upb_msgdef *m, upb_arena *a) {
@ -81,20 +96,17 @@ bool upb_msg_has(const upb_msg *msg, const upb_fielddef *f) {
const upb_fielddef *upb_msg_whichoneof(const upb_msg *msg,
const upb_oneofdef *o) {
upb_oneof_iter i;
const upb_fielddef *f;
const upb_msglayout_field *field;
const upb_msgdef *m = upb_oneofdef_containingtype(o);
uint32_t oneof_case;
/* This is far from optimal. */
upb_oneof_begin(&i, o);
if (upb_oneof_done(&i)) return false;
f = upb_oneof_iter_field(&i);
field = upb_fielddef_layout(f);
oneof_case = _upb_getoneofcase_field(msg, field);
return oneof_case ? upb_msgdef_itof(m, oneof_case) : NULL;
const upb_fielddef *f = upb_oneofdef_field(o, 0);
if (upb_oneofdef_issynthetic(o)) {
UPB_ASSERT(upb_oneofdef_fieldcount(o) == 1);
return upb_msg_has(msg, f) ? f : NULL;
} else {
const upb_msglayout_field *field = upb_fielddef_layout(f);
uint32_t oneof_case = _upb_getoneofcase_field(msg, field);
f = oneof_case ? upb_oneofdef_itof(o, oneof_case) : NULL;
UPB_ASSERT((f != NULL) == (oneof_case != 0));
return f;
}
}
upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f) {
@ -124,7 +136,7 @@ upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f) {
val.double_val = upb_fielddef_defaultdouble(f);
break;
case UPB_TYPE_BOOL:
val.double_val = upb_fielddef_defaultbool(f);
val.bool_val = upb_fielddef_defaultbool(f);
break;
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
@ -207,11 +219,12 @@ void upb_msg_clear(upb_msg *msg, const upb_msgdef *m) {
bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m,
const upb_symtab *ext_pool, const upb_fielddef **out_f,
upb_msgval *out_val, size_t *iter) {
size_t i = *iter;
int i = *iter;
int n = upb_msgdef_fieldcount(m);
const upb_msgval zero = {0};
const upb_fielddef *f;
UPB_UNUSED(ext_pool);
while ((f = _upb_msgdef_field(m, (int)++i)) != NULL) {
while (++i < n) {
const upb_fielddef *f = upb_msgdef_field(m, i);
upb_msgval val = _upb_msg_getraw(msg, f);
/* Skip field if unset or empty. */
@ -296,7 +309,7 @@ bool upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int maxdepth) {
/** upb_array *****************************************************************/
upb_array *upb_array_new(upb_arena *a, upb_fieldtype_t type) {
return _upb_array_new(a, type);
return _upb_array_new(a, 4, _upb_fieldtype_to_sizelg2[type]);
}
size_t upb_array_size(const upb_array *arr) {
@ -348,6 +361,10 @@ bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) {
return _upb_map_get(map, &key, map->key_size, val, map->val_size);
}
void upb_map_clear(upb_map *map) {
_upb_map_clear(map);
}
bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
upb_arena *arena) {
return _upb_map_set(map, &key, map->key_size, &val, map->val_size, arena);

@ -4,10 +4,12 @@
** Implementation is heavily inspired by Lua's ltable.c.
*/
#include "upb/table.int.h"
#include <string.h>
#include "third_party/wyhash/wyhash.h"
#include "upb/table.int.h"
/* Must be last. */
#include "upb/port_def.inc"
#define UPB_MAXARRSIZE 16 /* 64k. */
@ -87,11 +89,7 @@ static upb_tabent *mutable_entries(upb_table *t) {
}
static bool isfull(upb_table *t) {
if (upb_table_size(t) == 0) {
return true;
} else {
return ((double)(t->count + 1) / upb_table_size(t)) > MAX_LOAD;
}
return t->count == t->max_count;
}
static bool init(upb_table *t, uint8_t size_lg2, upb_alloc *a) {
@ -100,6 +98,7 @@ static bool init(upb_table *t, uint8_t size_lg2, upb_alloc *a) {
t->count = 0;
t->size_lg2 = size_lg2;
t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
t->max_count = upb_table_size(t) * MAX_LOAD;
bytes = upb_table_size(t) * sizeof(upb_tabent);
if (bytes > 0) {
t->entries = upb_malloc(a, bytes);
@ -115,9 +114,17 @@ static void uninit(upb_table *t, upb_alloc *a) {
upb_free(a, mutable_entries(t));
}
static upb_tabent *emptyent(upb_table *t) {
upb_tabent *e = mutable_entries(t) + upb_table_size(t);
while (1) { if (upb_tabent_isempty(--e)) return e; UPB_ASSERT(e > t->entries); }
static upb_tabent *emptyent(upb_table *t, upb_tabent *e) {
upb_tabent *begin = mutable_entries(t);
upb_tabent *end = begin + upb_table_size(t);
for (e = e + 1; e < end; e++) {
if (upb_tabent_isempty(e)) return e;
}
for (e = begin; e < end; e++) {
if (upb_tabent_isempty(e)) return e;
}
UPB_ASSERT(false);
return NULL;
}
static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) {
@ -173,11 +180,11 @@ static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey,
our_e->next = NULL;
} else {
/* Collision. */
upb_tabent *new_e = emptyent(t);
upb_tabent *new_e = emptyent(t, mainpos_e);
/* Head of collider's chain. */
upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key));
if (chain == mainpos_e) {
/* Existing ent is in its main posisiton (it has the same hash as us, and
/* Existing ent is in its main position (it has the same hash as us, and
* is the head of our chain). Insert to new ent and append to this chain. */
new_e->next = mainpos_e->next;
mainpos_e->next = new_e;
@ -268,10 +275,14 @@ static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) {
return (uintptr_t)str;
}
static uint32_t table_hash(const char *p, size_t n) {
return wyhash(p, n, 0, _wyp);
}
static uint32_t strhash(upb_tabkey key) {
uint32_t len;
char *str = upb_tabstr(key, &len);
return upb_murmur_hash2(str, len, 0);
return table_hash(str, len);
}
static bool streql(upb_tabkey k1, lookupkey_t k2) {
@ -280,9 +291,14 @@ static bool streql(upb_tabkey k1, lookupkey_t k2) {
return len == k2.str.len && (len == 0 || memcmp(str, k2.str.str, len) == 0);
}
bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) {
bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype,
size_t expected_size, upb_alloc *a) {
UPB_UNUSED(ctype); /* TODO(haberman): rm */
return init(&t->t, 2, a);
// Multiply by approximate reciprocal of MAX_LOAD (0.85), with pow2 denominator.
size_t need_entries = (expected_size + 1) * 1204 / 1024;
UPB_ASSERT(need_entries >= expected_size * 0.85);
int size_lg2 = _upb_lg2ceil(need_entries);
return init(&t->t, size_lg2, a);
}
void upb_strtable_clear(upb_strtable *t) {
@ -333,20 +349,20 @@ bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len,
tabkey = strcopy(key, a);
if (tabkey == 0) return false;
hash = upb_murmur_hash2(key.str.str, key.str.len, 0);
hash = table_hash(key.str.str, key.str.len);
insert(&t->t, key, tabkey, v, hash, &strhash, &streql);
return true;
}
bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
upb_value *v) {
uint32_t hash = upb_murmur_hash2(key, len, 0);
uint32_t hash = table_hash(key, len);
return lookup(&t->t, strkey2(key, len), v, hash, &streql);
}
bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
upb_value *val, upb_alloc *alloc) {
uint32_t hash = upb_murmur_hash2(key, len, 0);
uint32_t hash = table_hash(key, len);
upb_tabkey tabkey;
if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) {
if (alloc) {
@ -699,182 +715,3 @@ bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
return i1->t == i2->t && i1->index == i2->index &&
i1->array_part == i2->array_part;
}
#if defined(UPB_UNALIGNED_READS_OK) || defined(__s390x__)
/* -----------------------------------------------------------------------------
* MurmurHash2, by Austin Appleby (released as public domain).
* Reformatted and C99-ified by Joshua Haberman.
* Note - This code makes a few assumptions about how your machine behaves -
* 1. We can read a 4-byte value from any address without crashing
* 2. sizeof(int) == 4 (in upb this limitation is removed by using uint32_t
* And it has a few limitations -
* 1. It will not work incrementally.
* 2. It will not produce the same results on little-endian and big-endian
* machines. */
uint32_t upb_murmur_hash2(const void *key, size_t len, uint32_t seed) {
/* 'm' and 'r' are mixing constants generated offline.
* They're not really 'magic', they just happen to work well. */
const uint32_t m = 0x5bd1e995;
const int32_t r = 24;
/* Initialize the hash to a 'random' value */
uint32_t h = seed ^ len;
/* Mix 4 bytes at a time into the hash */
const uint8_t * data = (const uint8_t *)key;
while(len >= 4) {
uint32_t k;
memcpy(&k, data, sizeof(k));
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
/* Handle the last few bytes of the input array */
switch(len) {
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0]; h *= m;
};
/* Do a few final mixes of the hash to ensure the last few
* bytes are well-incorporated. */
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
#else /* !UPB_UNALIGNED_READS_OK */
/* -----------------------------------------------------------------------------
* MurmurHashAligned2, by Austin Appleby
* Same algorithm as MurmurHash2, but only does aligned reads - should be safer
* on certain platforms.
* Performance will be lower than MurmurHash2 */
#define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) {
const uint32_t m = 0x5bd1e995;
const int32_t r = 24;
const uint8_t * data = (const uint8_t *)key;
uint32_t h = (uint32_t)(seed ^ len);
uint8_t align = (uintptr_t)data & 3;
if(align && (len >= 4)) {
/* Pre-load the temp registers */
uint32_t t = 0, d = 0;
int32_t sl;
int32_t sr;
switch(align) {
case 1: t |= data[2] << 16; /* fallthrough */
case 2: t |= data[1] << 8; /* fallthrough */
case 3: t |= data[0];
}
t <<= (8 * align);
data += 4-align;
len -= 4-align;
sl = 8 * (4-align);
sr = 8 * align;
/* Mix */
while(len >= 4) {
uint32_t k;
d = *(uint32_t *)data;
t = (t >> sr) | (d << sl);
k = t;
MIX(h,k,m);
t = d;
data += 4;
len -= 4;
}
/* Handle leftover data in temp registers */
d = 0;
if(len >= align) {
uint32_t k;
switch(align) {
case 3: d |= data[2] << 16; /* fallthrough */
case 2: d |= data[1] << 8; /* fallthrough */
case 1: d |= data[0]; /* fallthrough */
}
k = (t >> sr) | (d << sl);
MIX(h,k,m);
data += align;
len -= align;
/* ----------
* Handle tail bytes */
switch(len) {
case 3: h ^= data[2] << 16; /* fallthrough */
case 2: h ^= data[1] << 8; /* fallthrough */
case 1: h ^= data[0]; h *= m; /* fallthrough */
};
} else {
switch(len) {
case 3: d |= data[2] << 16; /* fallthrough */
case 2: d |= data[1] << 8; /* fallthrough */
case 1: d |= data[0]; /* fallthrough */
case 0: h ^= (t >> sr) | (d << sl); h *= m;
}
}
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
} else {
while(len >= 4) {
uint32_t k = *(uint32_t *)data;
MIX(h,k,m);
data += 4;
len -= 4;
}
/* ----------
* Handle tail bytes */
switch(len) {
case 3: h ^= data[2] << 16; /* fallthrough */
case 2: h ^= data[1] << 8; /* fallthrough */
case 1: h ^= data[0]; h *= m;
};
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
}
#undef MIX
#endif /* UPB_UNALIGNED_READS_OK */

@ -13,7 +13,7 @@
** store pointers or integers of at least 32 bits (upb isn't really useful on
** systems where sizeof(void*) < 4).
**
** The table must be homogenous (all values of the same type). In debug
** The table must be homogeneous (all values of the same type). In debug
** mode, we check this on insert and lookup.
*/
@ -147,10 +147,17 @@ UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) {
return mem + sizeof(*len);
}
UPB_INLINE upb_strview upb_tabstrview(upb_tabkey key) {
upb_strview ret;
uint32_t len;
ret.data = upb_tabstr(key, &len);
ret.size = len;
return ret;
}
/* upb_tabval *****************************************************************/
typedef struct {
typedef struct upb_tabval {
uint64_t val;
} upb_tabval;
@ -171,7 +178,8 @@ typedef struct _upb_tabent {
typedef struct {
size_t count; /* Number of entries in the hash part. */
size_t mask; /* Mask to turn hash value -> bucket. */
uint32_t mask; /* Mask to turn hash value -> bucket. */
uint32_t max_count; /* Max count before we hit our load limit. */
uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
/* Hash table entries.
@ -230,7 +238,8 @@ UPB_INLINE bool upb_arrhas(upb_tabval key) {
/* Initialize and uninitialize a table, respectively. If memory allocation
* failed, false is returned that the table is uninitialized. */
bool upb_inttable_init2(upb_inttable *table, upb_ctype_t ctype, upb_alloc *a);
bool upb_strtable_init2(upb_strtable *table, upb_ctype_t ctype, upb_alloc *a);
bool upb_strtable_init2(upb_strtable *table, upb_ctype_t ctype,
size_t expected_size, upb_alloc *a);
void upb_inttable_uninit2(upb_inttable *table, upb_alloc *a);
void upb_strtable_uninit2(upb_strtable *table, upb_alloc *a);
@ -239,7 +248,7 @@ UPB_INLINE bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype) {
}
UPB_INLINE bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype) {
return upb_strtable_init2(table, ctype, &upb_alloc_global);
return upb_strtable_init2(table, ctype, 4, &upb_alloc_global);
}
UPB_INLINE void upb_inttable_uninit(upb_inttable *table) {

@ -17,6 +17,7 @@ typedef struct {
int indent_depth;
int options;
const upb_symtab *ext_pool;
_upb_mapsorter sorter;
} txtenc;
static void txtenc_msg(txtenc *e, const upb_msg *msg, const upb_msgdef *m);
@ -43,7 +44,7 @@ static void txtenc_printf(txtenc *e, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
n = _upb_vsnprintf(e->ptr, have, fmt, args);
n = vsnprintf(e->ptr, have, fmt, args);
va_end(args);
if (UPB_LIKELY(have > n)) {
@ -187,6 +188,25 @@ static void txtenc_array(txtenc *e, const upb_array *arr,
}
}
static void txtenc_mapentry(txtenc *e, upb_msgval key, upb_msgval val,
const upb_fielddef *f) {
const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
const upb_fielddef *key_f = upb_msgdef_field(entry, 0);
const upb_fielddef *val_f = upb_msgdef_field(entry, 1);
txtenc_indent(e);
txtenc_printf(e, "%s: {", upb_fielddef_name(f));
txtenc_endfield(e);
e->indent_depth++;
txtenc_field(e, key, key_f);
txtenc_field(e, val, val_f);
e->indent_depth--;
txtenc_indent(e);
txtenc_putstr(e, "}");
txtenc_endfield(e);
}
/*
* Maps print as messages of key/value, etc.
*
@ -200,27 +220,28 @@ static void txtenc_array(txtenc *e, const upb_array *arr,
* }
*/
static void txtenc_map(txtenc *e, const upb_map *map, const upb_fielddef *f) {
const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
size_t iter = UPB_MAP_BEGIN;
while (upb_mapiter_next(map, &iter)) {
upb_msgval key = upb_mapiter_key(map, iter);
upb_msgval val = upb_mapiter_value(map, iter);
txtenc_indent(e);
txtenc_printf(e, "%s: {", upb_fielddef_name(f));
txtenc_endfield(e);
e->indent_depth++;
txtenc_field(e, key, key_f);
txtenc_field(e, val, val_f);
e->indent_depth--;
txtenc_indent(e);
txtenc_putstr(e, "}");
txtenc_endfield(e);
if (e->options & UPB_TXTENC_NOSORT) {
size_t iter = UPB_MAP_BEGIN;
while (upb_mapiter_next(map, &iter)) {
upb_msgval key = upb_mapiter_key(map, iter);
upb_msgval val = upb_mapiter_value(map, iter);
txtenc_mapentry(e, key, val, f);
}
} else {
const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
const upb_fielddef *key_f = upb_msgdef_field(entry, 0);
_upb_sortedmap sorted;
upb_map_entry ent;
_upb_mapsorter_pushmap(&e->sorter, upb_fielddef_descriptortype(key_f), map,
&sorted);
while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) {
upb_msgval key, val;
memcpy(&key, &ent.k, sizeof(key));
memcpy(&val, &ent.v, sizeof(val));
txtenc_mapentry(e, key, val, f);
}
_upb_mapsorter_popmap(&e->sorter, &sorted);
}
}
@ -392,7 +413,9 @@ size_t upb_text_encode(const upb_msg *msg, const upb_msgdef *m,
e.indent_depth = 0;
e.options = options;
e.ext_pool = ext_pool;
_upb_mapsorter_init(&e.sorter);
txtenc_msg(&e, msg, m);
_upb_mapsorter_destroy(&e.sorter);
return txtenc_nullz(&e, size);
}

@ -13,7 +13,10 @@ enum {
UPB_TXTENC_SINGLELINE = 1,
/* When set, unknown fields are not printed. */
UPB_TXTENC_SKIPUNKNOWN = 2
UPB_TXTENC_SKIPUNKNOWN = 2,
/* When set, maps are *not* sorted (this avoids allocating tmp mem). */
UPB_TXTENC_NOSORT = 4
};
/* Encodes the given |msg| to text format. The message's reflection is given in

@ -1,5 +1,5 @@
#include "upb/upb.h"
#include "upb/upb.int.h"
#include <errno.h>
#include <stdarg.h>
@ -40,7 +40,7 @@ void upb_status_seterrf(upb_status *status, const char *fmt, ...) {
void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) {
if (!status) return;
status->ok = false;
_upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args);
vsnprintf(status->msg, sizeof(status->msg), fmt, args);
status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
}
@ -49,7 +49,7 @@ void upb_status_vappenderrf(upb_status *status, const char *fmt, va_list args) {
if (!status) return;
status->ok = false;
len = strlen(status->msg);
_upb_vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args);
vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args);
status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
}
@ -73,37 +73,18 @@ upb_alloc upb_alloc_global = {&upb_global_allocfunc};
/* Be conservative and choose 16 in case anyone is using SSE. */
typedef struct mem_block {
struct mem_block {
struct mem_block *next;
uint32_t size;
uint32_t cleanups;
/* Data follows. */
} mem_block;
};
typedef struct cleanup_ent {
upb_cleanup_func *cleanup;
void *ud;
} cleanup_ent;
struct upb_arena {
_upb_arena_head head;
uint32_t *cleanups;
/* Allocator to allocate arena blocks. We are responsible for freeing these
* when we are destroyed. */
upb_alloc *block_alloc;
uint32_t last_size;
/* When multiple arenas are fused together, each arena points to a parent
* arena (root points to itself). The root tracks how many live arenas
* reference it. */
uint32_t refcount; /* Only used when a->parent == a */
struct upb_arena *parent;
/* Linked list of blocks to free/cleanup. */
mem_block *freelist, *freelist_tail;
};
static const size_t memblock_reserve = UPB_ALIGN_UP(sizeof(mem_block), 16);
static upb_arena *arena_findroot(upb_arena *a) {
@ -117,9 +98,9 @@ static upb_arena *arena_findroot(upb_arena *a) {
return a;
}
static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size) {
static void upb_arena_addblock(upb_arena *a, upb_arena *root, void *ptr,
size_t size) {
mem_block *block = ptr;
upb_arena *root = arena_findroot(a);
/* The block is for arena |a|, but should appear in the freelist of |root|. */
block->next = root->freelist;
@ -133,26 +114,22 @@ static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size) {
a->head.end = UPB_PTR_AT(block, size, char);
a->cleanups = &block->cleanups;
/* TODO(haberman): ASAN poison. */
UPB_POISON_MEMORY_REGION(a->head.ptr, a->head.end - a->head.ptr);
}
static bool upb_arena_allocblock(upb_arena *a, size_t size) {
upb_arena *root = arena_findroot(a);
size_t block_size = UPB_MAX(size, a->last_size * 2) + memblock_reserve;
mem_block *block = upb_malloc(a->block_alloc, block_size);
mem_block *block = upb_malloc(root->block_alloc, block_size);
if (!block) return false;
upb_arena_addblock(a, block, block_size);
upb_arena_addblock(a, root, block, block_size);
return true;
}
static bool arena_has(upb_arena *a, size_t size) {
_upb_arena_head *h = (_upb_arena_head*)a;
return (size_t)(h->end - h->ptr) >= size;
}
void *_upb_arena_slowmalloc(upb_arena *a, size_t size) {
if (!upb_arena_allocblock(a, size)) return NULL; /* Out of memory. */
UPB_ASSERT(arena_has(a, size));
UPB_ASSERT(_upb_arenahas(a) >= size);
return upb_arena_malloc(a, size);
}
@ -184,7 +161,7 @@ upb_arena *arena_initslow(void *mem, size_t n, upb_alloc *alloc) {
a->freelist = NULL;
a->freelist_tail = NULL;
upb_arena_addblock(a, mem, n);
upb_arena_addblock(a, a, mem, n);
return a;
}
@ -201,15 +178,14 @@ upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) {
}
a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
n -= sizeof(*a);
a->head.alloc.func = &upb_arena_doalloc;
a->block_alloc = alloc;
a->parent = a;
a->refcount = 1;
a->last_size = 128;
a->last_size = UPB_MAX(128, n);
a->head.ptr = mem;
a->head.end = UPB_PTR_AT(mem, n, char);
a->head.end = UPB_PTR_AT(mem, n - sizeof(*a), char);
a->freelist = NULL;
a->cleanups = NULL;
@ -247,14 +223,15 @@ void upb_arena_free(upb_arena *a) {
bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func) {
cleanup_ent *ent;
if (!a->cleanups || !arena_has(a, sizeof(cleanup_ent))) {
if (!a->cleanups || _upb_arenahas(a) < sizeof(cleanup_ent)) {
if (!upb_arena_allocblock(a, 128)) return false; /* Out of memory. */
UPB_ASSERT(arena_has(a, sizeof(cleanup_ent)));
UPB_ASSERT(_upb_arenahas(a) >= sizeof(cleanup_ent));
}
a->head.end -= sizeof(cleanup_ent);
ent = (cleanup_ent*)a->head.end;
(*a->cleanups)++;
UPB_UNPOISON_MEMORY_REGION(ent, sizeof(cleanup_ent));
ent->cleanup = func;
ent->ud = ud;

@ -161,17 +161,35 @@ void *_upb_arena_slowmalloc(upb_arena *a, size_t size);
UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; }
UPB_INLINE size_t _upb_arenahas(upb_arena *a) {
_upb_arena_head *h = (_upb_arena_head*)a;
return (size_t)(h->end - h->ptr);
}
UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) {
_upb_arena_head *h = (_upb_arena_head*)a;
void* ret;
size = UPB_ALIGN_MALLOC(size);
if (UPB_UNLIKELY((size_t)(h->end - h->ptr) < size)) {
if (UPB_UNLIKELY(_upb_arenahas(a) < size)) {
return _upb_arena_slowmalloc(a, size);
}
ret = h->ptr;
h->ptr += size;
UPB_UNPOISON_MEMORY_REGION(ret, size);
#if UPB_ASAN
{
size_t guard_size = 32;
if (_upb_arenahas(a) >= guard_size) {
h->ptr += guard_size;
} else {
h->ptr = h->end;
}
}
#endif
return ret;
}
@ -283,7 +301,7 @@ UPB_INLINE uint32_t _upb_be_swap32(uint32_t val) {
return val;
} else {
return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
((val & 0xff0000ULL) >> 8) | ((val & 0xff000000ULL) >> 24);
((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
}
}
@ -291,14 +309,25 @@ UPB_INLINE uint64_t _upb_be_swap64(uint64_t val) {
if (_upb_isle()) {
return val;
} else {
return ((val & 0xff) << 56) | ((val & 0xff00) << 40) |
((val & 0xff0000) << 24) | ((val & 0xff000000) << 8) |
((val & 0xff00000000ULL) >> 8) | ((val & 0xff0000000000ULL) >> 24) |
((val & 0xff000000000000ULL) >> 40) |
((val & 0xff00000000000000ULL) >> 56);
return ((uint64_t)_upb_be_swap32(val) << 32) | _upb_be_swap32(val >> 32);
}
}
UPB_INLINE int _upb_lg2ceil(int x) {
if (x <= 1) return 0;
#ifdef __GNUC__
return 32 - __builtin_clz(x - 1);
#else
int lg2 = 0;
while (1 << lg2 < x) lg2++;
return lg2;
#endif
}
UPB_INLINE int _upb_lg2ceilsize(int x) {
return 1 << _upb_lg2ceil(x);
}
#include "upb/port_undef.inc"
#ifdef __cplusplus

@ -41,6 +41,9 @@ class Arena {
public:
// A simple arena with no initial memory block and the default allocator.
Arena() : ptr_(upb_arena_new(), upb_arena_free) {}
Arena(char *initial_block, size_t size)
: ptr_(upb_arena_init(initial_block, size, &upb_alloc_global),
upb_arena_free) {}
upb_arena* ptr() { return ptr_.get(); }
@ -71,15 +74,12 @@ class Arena {
template <int N>
class InlinedArena : public Arena {
public:
InlinedArena() : ptr_(upb_arena_new(&initial_block_, N, &upb_alloc_global)) {}
upb_arena* ptr() { return ptr_.get(); }
InlinedArena() : Arena(initial_block_, N) {}
private:
InlinedArena(const InlinedArena*) = delete;
InlinedArena& operator=(const InlinedArena*) = delete;
std::unique_ptr<upb_arena, decltype(&upb_arena_free)> ptr_;
char initial_block_[N];
};

@ -0,0 +1,29 @@
#ifndef UPB_INT_H_
#define UPB_INT_H_
#include "upb/upb.h"
struct mem_block;
typedef struct mem_block mem_block;
struct upb_arena {
_upb_arena_head head;
uint32_t *cleanups;
/* Allocator to allocate arena blocks. We are responsible for freeing these
* when we are destroyed. */
upb_alloc *block_alloc;
uint32_t last_size;
/* When multiple arenas are fused together, each arena points to a parent
* arena (root points to itself). The root tracks how many live arenas
* reference it. */
uint32_t refcount; /* Only used when a->parent == a */
struct upb_arena *parent;
/* Linked list of blocks to free/cleanup. */
mem_block *freelist, *freelist_tail;
};
#endif /* UPB_INT_H_ */

@ -0,0 +1,53 @@
load(
"//bazel:build_defs.bzl",
"UPB_DEFAULT_CPPOPTS",
)
licenses(["notice"])
cc_library(
name = "common",
hdrs = ["common.h"],
srcs = ["common.cc"],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
"@com_google_protobuf//:protobuf",
"@com_google_absl//absl/strings",
],
)
cc_binary(
name = "protoc-gen-upb",
srcs = [
"protoc-gen-upb.cc",
"message_layout.cc",
"message_layout.h",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//visibility:public"],
deps = [
":common",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//:protoc_lib",
],
)
cc_binary(
name = "protoc-gen-upbdefs",
srcs = [
"protoc-gen-upbdefs.cc",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//visibility:public"],
deps = [
":common",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//:protoc_lib",
],
)

@ -0,0 +1,65 @@
#include "absl/strings/str_replace.h"
#include "upbc/common.h"
namespace upbc {
namespace {
namespace protobuf = ::google::protobuf;
void AddMessages(const protobuf::Descriptor* message,
std::vector<const protobuf::Descriptor*>* messages) {
messages->push_back(message);
for (int i = 0; i < message->nested_type_count(); i++) {
AddMessages(message->nested_type(i), messages);
}
}
} // namespace
std::string StripExtension(absl::string_view fname) {
size_t lastdot = fname.find_last_of(".");
if (lastdot == std::string::npos) {
return std::string(fname);
}
return std::string(fname.substr(0, lastdot));
}
std::string ToCIdent(absl::string_view str) {
return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}});
}
std::string ToPreproc(absl::string_view str) {
return absl::AsciiStrToUpper(ToCIdent(str));
}
void EmitFileWarning(const protobuf::FileDescriptor* file, Output& output) {
output(
"/* This file was generated by upbc (the upb compiler) from the input\n"
" * file:\n"
" *\n"
" * $0\n"
" *\n"
" * Do not edit -- your changes will be discarded when the file is\n"
" * regenerated. */\n\n",
file->name());
}
std::vector<const protobuf::Descriptor*> SortedMessages(
const protobuf::FileDescriptor* file) {
std::vector<const protobuf::Descriptor*> messages;
for (int i = 0; i < file->message_type_count(); i++) {
AddMessages(file->message_type(i), &messages);
}
return messages;
}
std::string MessageName(const protobuf::Descriptor* descriptor) {
return ToCIdent(descriptor->full_name());
}
std::string MessageInit(const protobuf::Descriptor* descriptor) {
return MessageName(descriptor) + "_msginit";
}
} // namespace upbc

@ -0,0 +1,66 @@
#ifndef UPBC_COMMON_H
#define UPBC_COMMON_H
#include <vector>
#include "absl/strings/substitute.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/zero_copy_stream.h"
namespace upbc {
class Output {
public:
Output(google::protobuf::io::ZeroCopyOutputStream* stream)
: stream_(stream) {}
~Output() { stream_->BackUp((int)size_); }
template <class... Arg>
void operator()(absl::string_view format, const Arg&... arg) {
Write(absl::Substitute(format, arg...));
}
private:
void Write(absl::string_view data) {
while (!data.empty()) {
RefreshOutput();
size_t to_write = std::min(data.size(), size_);
memcpy(ptr_, data.data(), to_write);
data.remove_prefix(to_write);
ptr_ += to_write;
size_ -= to_write;
}
}
void RefreshOutput() {
while (size_ == 0) {
void *ptr;
int size;
if (!stream_->Next(&ptr, &size)) {
fprintf(stderr, "upbc: Failed to write to to output\n");
abort();
}
ptr_ = static_cast<char*>(ptr);
size_ = size;
}
}
google::protobuf::io::ZeroCopyOutputStream* stream_;
char *ptr_ = nullptr;
size_t size_ = 0;
};
std::string StripExtension(absl::string_view fname);
std::string ToCIdent(absl::string_view str);
std::string ToPreproc(absl::string_view str);
void EmitFileWarning(const google::protobuf::FileDescriptor* file,
Output& output);
std::vector<const google::protobuf::Descriptor*> SortedMessages(
const google::protobuf::FileDescriptor* file);
std::string MessageInit(const google::protobuf::Descriptor* descriptor);
std::string MessageName(const google::protobuf::Descriptor* descriptor);
} // namespace upbc
# endif // UPBC_COMMON_H

@ -1,12 +0,0 @@
#ifndef UPBC_GENERATOR_H_
#define UPBC_GENERATOR_H_
#include <memory>
#include <google/protobuf/compiler/code_generator.h>
namespace upbc {
std::unique_ptr<google::protobuf::compiler::CodeGenerator> GetGenerator();
}
#endif // UPBC_GENERATOR_H_

@ -1,9 +0,0 @@
#include <google/protobuf/compiler/plugin.h>
#include "upbc/generator.h"
int main(int argc, char** argv) {
return google::protobuf::compiler::PluginMain(argc, argv,
upbc::GetGenerator().get());
}

@ -24,9 +24,7 @@ MessageLayout::Size MessageLayout::Place(
}
bool MessageLayout::HasHasbit(const protobuf::FieldDescriptor* field) {
return field->file()->syntax() == protobuf::FileDescriptor::SYNTAX_PROTO2 &&
field->label() != protobuf::FieldDescriptor::LABEL_REPEATED &&
!field->containing_oneof() &&
return field->has_presence() && !field->real_containing_oneof() &&
!field->containing_type()->options().map_entry();
}
@ -51,10 +49,15 @@ MessageLayout::SizeAndAlign MessageLayout::SizeOfUnwrapped(
case protobuf::FieldDescriptor::CPPTYPE_FLOAT:
case protobuf::FieldDescriptor::CPPTYPE_INT32:
case protobuf::FieldDescriptor::CPPTYPE_UINT32:
case protobuf::FieldDescriptor::CPPTYPE_ENUM:
return {{4, 4}, {4, 4}};
default:
case protobuf::FieldDescriptor::CPPTYPE_INT64:
case protobuf::FieldDescriptor::CPPTYPE_UINT64:
case protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
return {{8, 8}, {8, 8}};
}
assert(false);
return {{-1, -1}, {-1, -1}};
}
int64_t MessageLayout::FieldLayoutRank(const protobuf::FieldDescriptor* field) {
@ -105,7 +108,7 @@ int64_t MessageLayout::FieldLayoutRank(const protobuf::FieldDescriptor* field) {
void MessageLayout::ComputeLayout(const protobuf::Descriptor* descriptor) {
size_ = Size{0, 0};
maxalign_ = Size{0, 0};
maxalign_ = Size{8, 8};
if (descriptor->options().map_entry()) {
// Map entries aren't actually stored, they are only used during parsing.
@ -140,7 +143,7 @@ void MessageLayout::PlaceNonOneofFields(
// Place/count hasbits.
int hasbit_count = 0;
for (auto field : field_order) {
for (auto field : FieldHotnessOrder(descriptor)) {
if (HasHasbit(field)) {
// We don't use hasbit 0, so that 0 can indicate "no presence" in the
// table. This wastes one hasbit, but we don't worry about it for now.

@ -102,6 +102,23 @@ class MessageLayout {
Size size_;
};
// Returns fields in order of "hotness", eg. how frequently they appear in
// serialized payloads. Ideally this will use a profile. When we don't have
// that, we assume that fields with smaller numbers are used more frequently.
inline std::vector<const google::protobuf::FieldDescriptor*> FieldHotnessOrder(
const google::protobuf::Descriptor* message) {
std::vector<const google::protobuf::FieldDescriptor*> fields;
for (int i = 0; i < message->field_count(); i++) {
fields.push_back(message->field(i));
}
std::sort(fields.begin(), fields.end(),
[](const google::protobuf::FieldDescriptor* a,
const google::protobuf::FieldDescriptor* b) {
return a->number() < b->number();
});
return fields;
}
} // namespace upbc
#endif // UPBC_MESSAGE_LAYOUT_H

@ -3,103 +3,29 @@
#include "absl/container/flat_hash_map.h"
#include "absl/strings/ascii.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/plugin.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/io/zero_copy_stream.h"
#include "upbc/generator.h"
#include "google/protobuf/wire_format.h"
#include "upbc/common.h"
#include "upbc/message_layout.h"
namespace upbc {
namespace {
namespace protoc = ::google::protobuf::compiler;
namespace protobuf = ::google::protobuf;
static std::string StripExtension(absl::string_view fname) {
size_t lastdot = fname.find_last_of(".");
if (lastdot == std::string::npos) {
return std::string(fname);
}
return std::string(fname.substr(0, lastdot));
}
static std::string HeaderFilename(std::string proto_filename) {
std::string HeaderFilename(std::string proto_filename) {
return StripExtension(proto_filename) + ".upb.h";
}
static std::string SourceFilename(std::string proto_filename) {
std::string SourceFilename(std::string proto_filename) {
return StripExtension(proto_filename) + ".upb.c";
}
static std::string DefHeaderFilename(std::string proto_filename) {
return StripExtension(proto_filename) + ".upbdefs.h";
}
static std::string DefSourceFilename(std::string proto_filename) {
return StripExtension(proto_filename) + ".upbdefs.c";
}
class Output {
public:
Output(protobuf::io::ZeroCopyOutputStream* stream) : stream_(stream) {}
~Output() { stream_->BackUp((int)size_); }
template <class... Arg>
void operator()(absl::string_view format, const Arg&... arg) {
Write(absl::Substitute(format, arg...));
}
private:
void Write(absl::string_view data) {
while (!data.empty()) {
RefreshOutput();
size_t to_write = std::min(data.size(), size_);
memcpy(ptr_, data.data(), to_write);
data.remove_prefix(to_write);
ptr_ += to_write;
size_ -= to_write;
}
}
void RefreshOutput() {
while (size_ == 0) {
void *ptr;
int size;
if (!stream_->Next(&ptr, &size)) {
fprintf(stderr, "upbc: Failed to write to to output\n");
abort();
}
ptr_ = static_cast<char*>(ptr);
size_ = size;
}
}
protobuf::io::ZeroCopyOutputStream* stream_;
char *ptr_ = nullptr;
size_t size_ = 0;
};
namespace upbc {
class Generator : public protoc::CodeGenerator {
~Generator() override {}
bool Generate(const protobuf::FileDescriptor* file,
const std::string& parameter, protoc::GeneratorContext* context,
std::string* error) const override;
uint64_t GetSupportedFeatures() const override {
return FEATURE_PROTO3_OPTIONAL;
}
};
void AddMessages(const protobuf::Descriptor* message,
std::vector<const protobuf::Descriptor*>* messages) {
messages->push_back(message);
for (int i = 0; i < message->nested_type_count(); i++) {
AddMessages(message->nested_type(i), messages);
}
}
void AddEnums(const protobuf::Descriptor* message,
std::vector<const protobuf::EnumDescriptor*>* enums) {
for (int i = 0; i < message->enum_type_count(); i++) {
@ -116,15 +42,6 @@ void SortDefs(std::vector<T>* defs) {
[](T a, T b) { return a->full_name() < b->full_name(); });
}
std::vector<const protobuf::Descriptor*> SortedMessages(
const protobuf::FileDescriptor* file) {
std::vector<const protobuf::Descriptor*> messages;
for (int i = 0; i < file->message_type_count(); i++) {
AddMessages(file->message_type(i), &messages);
}
return messages;
}
std::vector<const protobuf::EnumDescriptor*> SortedEnums(
const protobuf::FileDescriptor* file) {
std::vector<const protobuf::EnumDescriptor*> enums;
@ -140,16 +57,16 @@ std::vector<const protobuf::EnumDescriptor*> SortedEnums(
std::vector<const protobuf::FieldDescriptor*> FieldNumberOrder(
const protobuf::Descriptor* message) {
std::vector<const protobuf::FieldDescriptor*> messages;
std::vector<const protobuf::FieldDescriptor*> fields;
for (int i = 0; i < message->field_count(); i++) {
messages.push_back(message->field(i));
fields.push_back(message->field(i));
}
std::sort(messages.begin(), messages.end(),
std::sort(fields.begin(), fields.end(),
[](const protobuf::FieldDescriptor* a,
const protobuf::FieldDescriptor* b) {
return a->number() < b->number();
});
return messages;
return fields;
}
std::vector<const protobuf::FieldDescriptor*> SortedSubmessages(
@ -170,18 +87,6 @@ std::vector<const protobuf::FieldDescriptor*> SortedSubmessages(
return ret;
}
std::string ToCIdent(absl::string_view str) {
return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}});
}
std::string DefInitSymbol(const protobuf::FileDescriptor *file) {
return ToCIdent(file->name()) + "_upbdefinit";
}
std::string ToPreproc(absl::string_view str) {
return absl::AsciiStrToUpper(ToCIdent(str));
}
std::string EnumValueSymbol(const protobuf::EnumValueDescriptor* value) {
return ToCIdent(value->full_name());
}
@ -190,14 +95,6 @@ std::string GetSizeInit(const MessageLayout::Size& size) {
return absl::Substitute("UPB_SIZE($0, $1)", size.size32, size.size64);
}
std::string MessageName(const protobuf::Descriptor* descriptor) {
return ToCIdent(descriptor->full_name());
}
std::string MessageInit(const protobuf::Descriptor* descriptor) {
return MessageName(descriptor) + "_msginit";
}
std::string CTypeInternal(const protobuf::FieldDescriptor* field,
bool is_const) {
std::string maybe_const = is_const ? "const " : "";
@ -231,28 +128,28 @@ std::string CTypeInternal(const protobuf::FieldDescriptor* field,
}
}
std::string UpbType(const protobuf::FieldDescriptor* field) {
std::string SizeLg2(const protobuf::FieldDescriptor* field) {
switch (field->cpp_type()) {
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
return "UPB_TYPE_MESSAGE";
return "UPB_SIZE(2, 3)";
case protobuf::FieldDescriptor::CPPTYPE_ENUM:
return "UPB_TYPE_ENUM";
return std::to_string(2);
case protobuf::FieldDescriptor::CPPTYPE_BOOL:
return "UPB_TYPE_BOOL";
return std::to_string(1);
case protobuf::FieldDescriptor::CPPTYPE_FLOAT:
return "UPB_TYPE_FLOAT";
return std::to_string(2);
case protobuf::FieldDescriptor::CPPTYPE_INT32:
return "UPB_TYPE_INT32";
return std::to_string(2);
case protobuf::FieldDescriptor::CPPTYPE_UINT32:
return "UPB_TYPE_UINT32";
return std::to_string(2);
case protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
return "UPB_TYPE_DOUBLE";
return std::to_string(3);
case protobuf::FieldDescriptor::CPPTYPE_INT64:
return "UPB_TYPE_INT64";
return std::to_string(3);
case protobuf::FieldDescriptor::CPPTYPE_UINT64:
return "UPB_TYPE_UINT64";
return std::to_string(3);
case protobuf::FieldDescriptor::CPPTYPE_STRING:
return "UPB_TYPE_STRING";
return "UPB_SIZE(3, 4)";
default:
fprintf(stderr, "Unexpected type");
abort();
@ -318,18 +215,6 @@ void DumpEnumValues(const protobuf::EnumDescriptor* desc, Output& output) {
}
}
void EmitFileWarning(const protobuf::FileDescriptor* file, Output& output) {
output(
"/* This file was generated by upbc (the upb compiler) from the input\n"
" * file:\n"
" *\n"
" * $0\n"
" *\n"
" * Do not edit -- your changes will be discarded when the file is\n"
" * regenerated. */\n\n",
file->name());
}
void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output) {
MessageLayout layout(message);
@ -346,6 +231,12 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output
" $0 *ret = $0_new(arena);\n"
" return (ret && upb_decode(buf, size, ret, &$1, arena)) ? ret : NULL;\n"
"}\n"
"UPB_INLINE $0 *$0_parse_ex(const char *buf, size_t size,\n"
" upb_arena *arena, int options) {\n"
" $0 *ret = $0_new(arena);\n"
" return (ret && _upb_decode(buf, size, ret, &$1, arena, options))\n"
" ? ret : NULL;\n"
"}\n"
"UPB_INLINE char *$0_serialize(const $0 *msg, upb_arena *arena, size_t "
"*len) {\n"
" return upb_encode(msg, &$1, arena, len);\n"
@ -505,35 +396,33 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output
output(
"UPB_INLINE $0* $1_resize_$2($1 *msg, size_t len, "
"upb_arena *arena) {\n"
" return ($0*)_upb_array_resize_accessor(msg, $3, len, $4, arena);\n"
" return ($0*)_upb_array_resize_accessor2(msg, $3, len, $4, arena);\n"
"}\n",
CType(field), msgname, field->name(),
GetSizeInit(layout.GetFieldOffset(field)),
UpbType(field));
SizeLg2(field));
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
output(
"UPB_INLINE struct $0* $1_add_$2($1 *msg, upb_arena *arena) {\n"
" struct $0* sub = (struct $0*)_upb_msg_new(&$3, arena);\n"
" bool ok = _upb_array_append_accessor(\n"
" msg, $4, $5, $6, &sub, arena);\n"
" bool ok = _upb_array_append_accessor2(\n"
" msg, $4, $5, &sub, arena);\n"
" if (!ok) return NULL;\n"
" return sub;\n"
"}\n",
MessageName(field->message_type()), msgname, field->name(),
MessageInit(field->message_type()),
GetSizeInit(layout.GetFieldOffset(field)),
GetSizeInit(MessageLayout::SizeOfUnwrapped(field).size),
UpbType(field));
SizeLg2(field));
} else {
output(
"UPB_INLINE bool $1_add_$2($1 *msg, $0 val, upb_arena *arena) {\n"
" return _upb_array_append_accessor(msg, $3, $4, $5, &val,\n"
" return _upb_array_append_accessor2(msg, $3, $4, &val,\n"
" arena);\n"
"}\n",
CType(field), msgname, field->name(),
GetSizeInit(layout.GetFieldOffset(field)),
GetSizeInit(MessageLayout::SizeOfUnwrapped(field).size),
UpbType(field));
SizeLg2(field));
}
} else {
// Non-repeated field.
@ -600,6 +489,7 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) {
"#define $0_UPB_H_\n\n"
"#include \"upb/msg.h\"\n"
"#include \"upb/decode.h\"\n"
"#include \"upb/decode_fast.h\"\n"
"#include \"upb/encode.h\"\n\n",
ToPreproc(file->name()));
@ -703,7 +593,234 @@ int TableDescriptorType(const protobuf::FieldDescriptor* field) {
}
}
void WriteSource(const protobuf::FileDescriptor* file, Output& output) {
struct SubmsgArray {
public:
SubmsgArray(const protobuf::Descriptor* message) : message_(message) {
MessageLayout layout(message);
std::vector<const protobuf::FieldDescriptor*> sorted_submsgs =
SortedSubmessages(message);
int i = 0;
for (auto submsg : sorted_submsgs) {
if (indexes_.find(submsg->message_type()) != indexes_.end()) {
continue;
}
submsgs_.push_back(submsg->message_type());
indexes_[submsg->message_type()] = i++;
}
}
const std::vector<const protobuf::Descriptor*>& submsgs() const {
return submsgs_;
}
int GetIndex(const protobuf::FieldDescriptor* field) {
(void)message_;
assert(field->containing_type() == message_);
auto it = indexes_.find(field->message_type());
assert(it != indexes_.end());
return it->second;
}
private:
const protobuf::Descriptor* message_;
std::vector<const protobuf::Descriptor*> submsgs_;
absl::flat_hash_map<const protobuf::Descriptor*, int> indexes_;
};
typedef std::pair<std::string, uint64_t> TableEntry;
uint64_t GetEncodedTag(const protobuf::FieldDescriptor* field) {
protobuf::internal::WireFormatLite::WireType wire_type =
protobuf::internal::WireFormat::WireTypeForField(field);
uint32_t unencoded_tag =
protobuf::internal::WireFormatLite::MakeTag(field->number(), wire_type);
uint8_t tag_bytes[10] = {0};
protobuf::io::CodedOutputStream::WriteVarint32ToArray(unencoded_tag,
tag_bytes);
uint64_t encoded_tag = 0;
memcpy(&encoded_tag, tag_bytes, sizeof(encoded_tag));
// TODO: byte-swap for big endian.
return encoded_tag;
}
int GetTableSlot(const protobuf::FieldDescriptor* field) {
uint64_t tag = GetEncodedTag(field);
if (tag > 0x7fff) {
// Tag must fit within a two-byte varint.
return -1;
}
return (tag & 0xf8) >> 3;
}
bool TryFillTableEntry(const protobuf::Descriptor* message,
const MessageLayout& layout,
const protobuf::FieldDescriptor* field,
TableEntry& ent) {
std::string type = "";
std::string cardinality = "";
switch (field->type()) {
case protobuf::FieldDescriptor::TYPE_BOOL:
type = "b1";
break;
case protobuf::FieldDescriptor::TYPE_INT32:
case protobuf::FieldDescriptor::TYPE_ENUM:
case protobuf::FieldDescriptor::TYPE_UINT32:
type = "v4";
break;
case protobuf::FieldDescriptor::TYPE_INT64:
case protobuf::FieldDescriptor::TYPE_UINT64:
type = "v8";
break;
case protobuf::FieldDescriptor::TYPE_FIXED32:
case protobuf::FieldDescriptor::TYPE_SFIXED32:
case protobuf::FieldDescriptor::TYPE_FLOAT:
type = "f4";
break;
case protobuf::FieldDescriptor::TYPE_FIXED64:
case protobuf::FieldDescriptor::TYPE_SFIXED64:
case protobuf::FieldDescriptor::TYPE_DOUBLE:
type = "f8";
break;
case protobuf::FieldDescriptor::TYPE_SINT32:
type = "z4";
break;
case protobuf::FieldDescriptor::TYPE_SINT64:
type = "z8";
break;
case protobuf::FieldDescriptor::TYPE_STRING:
if (field->file()->syntax() == protobuf::FileDescriptor::SYNTAX_PROTO3) {
// Only proto3 validates UTF-8.
type = "s";
break;
}
ABSL_FALLTHROUGH_INTENDED;
case protobuf::FieldDescriptor::TYPE_BYTES:
type = "b";
break;
case protobuf::FieldDescriptor::TYPE_MESSAGE:
if (field->is_map()) {
return false; // Not supported yet (ever?).
}
type = "m";
break;
default:
return false; // Not supported yet.
}
switch (field->label()) {
case protobuf::FieldDescriptor::LABEL_REPEATED:
if (field->is_packed()) {
cardinality = "p";
} else {
cardinality = "r";
}
break;
case protobuf::FieldDescriptor::LABEL_OPTIONAL:
case protobuf::FieldDescriptor::LABEL_REQUIRED:
if (field->real_containing_oneof()) {
cardinality = "o";
} else {
cardinality = "s";
}
break;
}
uint64_t expected_tag = GetEncodedTag(field);
MessageLayout::Size offset = layout.GetFieldOffset(field);
// Data is:
//
// 48 32 16 0
// |--------|--------|--------|--------|--------|--------|--------|--------|
// | offset (16) |case offset (16) |presence| submsg | exp. tag (16) |
// |--------|--------|--------|--------|--------|--------|--------|--------|
//
// - |presence| is either hasbit index or field number for oneofs.
uint64_t data = offset.size64 << 48 | expected_tag;
if (field->is_repeated()) {
// No hasbit/oneof-related fields.
} if (field->real_containing_oneof()) {
MessageLayout::Size case_offset =
layout.GetOneofCaseOffset(field->real_containing_oneof());
if (case_offset.size64 > 0xffff) return false;
assert(field->number() < 256);
data |= field->number() << 24;
data |= case_offset.size64 << 32;
} else {
uint64_t hasbit_index = 63; // No hasbit (set a high, unused bit).
if (layout.HasHasbit(field)) {
hasbit_index = layout.GetHasbitIndex(field);
if (hasbit_index > 31) return false;
}
data |= hasbit_index << 24;
}
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
SubmsgArray submsg_array(message);
uint64_t idx = submsg_array.GetIndex(field);
if (idx > 255) return false;
data |= idx << 16;
std::string size_ceil = "max";
size_t size = SIZE_MAX;
if (field->message_type()->file() == field->file()) {
// We can only be guaranteed the size of the sub-message if it is in the
// same file as us. We could relax this to increase the speed of
// cross-file sub-message parsing if we are comfortable requiring that
// users compile all messages at the same time.
MessageLayout sub_layout(field->message_type());
size = sub_layout.message_size().size64 + 8;
}
std::vector<size_t> breaks = {64, 128, 192, 256};
for (auto brk : breaks) {
if (size <= brk) {
size_ceil = std::to_string(brk);
break;
}
}
ent.first = absl::Substitute("upb_p$0$1_$2bt_max$3b", cardinality, type,
expected_tag > 0xff ? "2" : "1", size_ceil);
} else {
ent.first = absl::Substitute("upb_p$0$1_$2bt", cardinality, type,
expected_tag > 0xff ? "2" : "1");
}
ent.second = data;
return true;
}
std::vector<TableEntry> FastDecodeTable(const protobuf::Descriptor* message,
const MessageLayout& layout) {
std::vector<TableEntry> table;
for (const auto field : FieldHotnessOrder(message)) {
TableEntry ent;
int slot = GetTableSlot(field);
// std::cerr << "table slot: " << field->number() << ": " << slot << "\n";
if (slot < 0) {
// Tag can't fit in the table.
continue;
}
if (!TryFillTableEntry(message, layout, field, ent)) {
// Unsupported field type or offset, hasbit index, etc. doesn't fit.
continue;
}
while ((size_t)slot >= table.size()) {
size_t size = std::max(static_cast<size_t>(1), table.size() * 2);
table.resize(size, TableEntry{"fastdecode_generic", 0});
}
if (table[slot].first != "fastdecode_generic") {
// A hotter field already filled this slot.
continue;
}
table[slot] = ent;
}
return table;
}
void WriteSource(const protobuf::FileDescriptor* file, Output& output,
bool fasttable_enabled) {
EmitFileWarning(file, output);
output(
@ -726,27 +843,19 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) {
std::string msgname = ToCIdent(message->full_name());
std::string fields_array_ref = "NULL";
std::string submsgs_array_ref = "NULL";
absl::flat_hash_map<const protobuf::Descriptor*, int> submsg_indexes;
MessageLayout layout(message);
std::vector<const protobuf::FieldDescriptor*> sorted_submsgs =
SortedSubmessages(message);
SubmsgArray submsg_array(message);
if (!sorted_submsgs.empty()) {
if (!submsg_array.submsgs().empty()) {
// TODO(haberman): could save a little bit of space by only generating a
// "submsgs" array for every strongly-connected component.
std::string submsgs_array_name = msgname + "_submsgs";
submsgs_array_ref = "&" + submsgs_array_name + "[0]";
output("static const upb_msglayout *const $0[$1] = {\n",
submsgs_array_name, sorted_submsgs.size());
submsgs_array_name, submsg_array.submsgs().size());
int i = 0;
for (auto submsg : sorted_submsgs) {
if (submsg_indexes.find(submsg->message_type()) !=
submsg_indexes.end()) {
continue;
}
output(" &$0,\n", MessageInit(submsg->message_type()));
submsg_indexes[submsg->message_type()] = i++;
for (auto submsg : submsg_array.submsgs()) {
output(" &$0,\n", MessageInit(submsg));
}
output("};\n\n");
@ -764,7 +873,7 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) {
std::string presence = "0";
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
submsg_index = submsg_indexes[field->message_type()];
submsg_index = submsg_array.GetIndex(field);
}
if (MessageLayout::HasHasbit(field)) {
@ -803,14 +912,34 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) {
output("};\n\n");
}
std::vector<TableEntry> table;
uint8_t table_mask = -1;
if (fasttable_enabled) {
table = FastDecodeTable(message, layout);
}
if (table.size() > 1) {
assert((table.size() & (table.size() - 1)) == 0);
table_mask = (table.size() - 1) << 3;
}
output("const upb_msglayout $0 = {\n", MessageInit(message));
output(" $0,\n", submsgs_array_ref);
output(" $0,\n", fields_array_ref);
output(" $0, $1, $2,\n", GetSizeInit(layout.message_size()),
output(" $0, $1, $2, $3,\n", GetSizeInit(layout.message_size()),
field_number_order.size(),
"false" // TODO: extendable
"false", // TODO: extendable
table_mask
);
if (!table.empty()) {
output(" UPB_FASTTABLE_INIT({\n");
for (const auto& ent : table) {
output(" {0x$1, &$0},\n", ent.first,
absl::StrCat(absl::Hex(ent.second, absl::kZeroPad16)));
}
output(" }),\n");
}
output("};\n\n");
}
@ -818,149 +947,47 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) {
output("\n");
}
void GenerateMessageDefAccessor(const protobuf::Descriptor* d, Output& output) {
output("UPB_INLINE const upb_msgdef *$0_getmsgdef(upb_symtab *s) {\n",
ToCIdent(d->full_name()));
output(" _upb_symtab_loaddefinit(s, &$0);\n", DefInitSymbol(d->file()));
output(" return upb_symtab_lookupmsg(s, \"$0\");\n", d->full_name());
output("}\n");
output("\n");
for (int i = 0; i < d->nested_type_count(); i++) {
GenerateMessageDefAccessor(d->nested_type(i), output);
}
}
void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) {
EmitFileWarning(file, output);
output(
"#ifndef $0_UPBDEFS_H_\n"
"#define $0_UPBDEFS_H_\n\n"
"#include \"upb/def.h\"\n"
"#include \"upb/port_def.inc\"\n"
"#ifdef __cplusplus\n"
"extern \"C\" {\n"
"#endif\n\n",
ToPreproc(file->name()));
output("#include \"upb/def.h\"\n");
output("\n");
output("#include \"upb/port_def.inc\"\n");
output("\n");
output("extern upb_def_init $0;\n", DefInitSymbol(file));
output("\n");
for (int i = 0; i < file->message_type_count(); i++) {
GenerateMessageDefAccessor(file->message_type(i), output);
}
output(
"#ifdef __cplusplus\n"
"} /* extern \"C\" */\n"
"#endif\n"
"\n"
"#include \"upb/port_undef.inc\"\n"
"\n"
"#endif /* $0_UPBDEFS_H_ */\n",
ToPreproc(file->name()));
}
// Escape C++ trigraphs by escaping question marks to \?
std::string EscapeTrigraphs(absl::string_view to_escape) {
return absl::StrReplaceAll(to_escape, {{"?", "\\?"}});
}
void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
EmitFileWarning(file, output);
output("#include \"upb/def.h\"\n");
output("#include \"$0\"\n", DefHeaderFilename(file->name()));
output("\n");
for (int i = 0; i < file->dependency_count(); i++) {
output("extern upb_def_init $0;\n", DefInitSymbol(file->dependency(i)));
}
std::vector<const protobuf::Descriptor*> file_messages =
SortedMessages(file);
for (auto message : file_messages) {
output("extern const upb_msglayout $0;\n", MessageInit(message));
}
output("\n");
if (!file_messages.empty()) {
output("static const upb_msglayout *layouts[$0] = {\n", file_messages.size());
for (auto message : file_messages) {
output(" &$0,\n", MessageInit(message));
}
output("};\n");
output("\n");
class Generator : public protoc::CodeGenerator {
~Generator() override {}
bool Generate(const protobuf::FileDescriptor* file,
const std::string& parameter, protoc::GeneratorContext* context,
std::string* error) const override;
uint64_t GetSupportedFeatures() const override {
return FEATURE_PROTO3_OPTIONAL;
}
};
protobuf::FileDescriptorProto file_proto;
file->CopyTo(&file_proto);
std::string file_data;
file_proto.SerializeToString(&file_data);
output("static const char descriptor[$0] = {", file_data.size());
// C90 only guarantees that strings can be up to 509 characters, and some
// implementations have limits here (for example, MSVC only allows 64k:
// https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1091.
// So we always emit an array instead of a string.
for (size_t i = 0; i < file_data.size();) {
for (size_t j = 0; j < 25 && i < file_data.size(); ++i, ++j) {
output("'$0', ", absl::CEscape(file_data.substr(i, 1)));
bool Generator::Generate(const protobuf::FileDescriptor* file,
const std::string& parameter,
protoc::GeneratorContext* context,
std::string* error) const {
bool fasttable_enabled = false;
std::vector<std::pair<std::string, std::string>> params;
google::protobuf::compiler::ParseGeneratorParameter(parameter, &params);
for (const auto& pair : params) {
if (pair.first == "fasttable") {
fasttable_enabled = true;
} else {
*error = "Unknown parameter: " + pair.first;
return false;
}
output("\n");
}
output("};\n\n");
output("static upb_def_init *deps[$0] = {\n", file->dependency_count() + 1);
for (int i = 0; i < file->dependency_count(); i++) {
output(" &$0,\n", DefInitSymbol(file->dependency(i)));
}
output(" NULL\n");
output("};\n");
output("\n");
output("upb_def_init $0 = {\n", DefInitSymbol(file));
output(" deps,\n");
if (file_messages.empty()) {
output(" NULL,\n");
} else {
output(" layouts,\n");
}
output(" \"$0\",\n", file->name());
output(" UPB_STRVIEW_INIT(descriptor, $0)\n", file_data.size());
output("};\n");
}
bool Generator::Generate(const protobuf::FileDescriptor* file,
const std::string& /* parameter */,
protoc::GeneratorContext* context,
std::string* /* error */) const {
Output h_output(context->Open(HeaderFilename(file->name())));
WriteHeader(file, h_output);
Output c_output(context->Open(SourceFilename(file->name())));
WriteSource(file, c_output);
Output h_def_output(context->Open(DefHeaderFilename(file->name())));
WriteDefHeader(file, h_def_output);
Output c_def_output(context->Open(DefSourceFilename(file->name())));
WriteDefSource(file, c_def_output);
WriteSource(file, c_output, fasttable_enabled);
return true;
}
std::unique_ptr<google::protobuf::compiler::CodeGenerator> GetGenerator() {
return std::unique_ptr<google::protobuf::compiler::CodeGenerator>(
new Generator());
}
} // namespace
} // namespace upbc
int main(int argc, char** argv) {
std::unique_ptr<google::protobuf::compiler::CodeGenerator> generator(
new upbc::Generator());
return google::protobuf::compiler::PluginMain(argc, argv, generator.get());
}

@ -0,0 +1,183 @@
#include <memory>
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/plugin.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "upbc/common.h"
namespace upbc {
namespace {
namespace protoc = ::google::protobuf::compiler;
namespace protobuf = ::google::protobuf;
std::string DefInitSymbol(const protobuf::FileDescriptor *file) {
return ToCIdent(file->name()) + "_upbdefinit";
}
static std::string DefHeaderFilename(std::string proto_filename) {
return StripExtension(proto_filename) + ".upbdefs.h";
}
static std::string DefSourceFilename(std::string proto_filename) {
return StripExtension(proto_filename) + ".upbdefs.c";
}
void GenerateMessageDefAccessor(const protobuf::Descriptor* d, Output& output) {
output("UPB_INLINE const upb_msgdef *$0_getmsgdef(upb_symtab *s) {\n",
ToCIdent(d->full_name()));
output(" _upb_symtab_loaddefinit(s, &$0);\n", DefInitSymbol(d->file()));
output(" return upb_symtab_lookupmsg(s, \"$0\");\n", d->full_name());
output("}\n");
output("\n");
for (int i = 0; i < d->nested_type_count(); i++) {
GenerateMessageDefAccessor(d->nested_type(i), output);
}
}
void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) {
EmitFileWarning(file, output);
output(
"#ifndef $0_UPBDEFS_H_\n"
"#define $0_UPBDEFS_H_\n\n"
"#include \"upb/def.h\"\n"
"#include \"upb/port_def.inc\"\n"
"#ifdef __cplusplus\n"
"extern \"C\" {\n"
"#endif\n\n",
ToPreproc(file->name()));
output("#include \"upb/def.h\"\n");
output("\n");
output("#include \"upb/port_def.inc\"\n");
output("\n");
output("extern upb_def_init $0;\n", DefInitSymbol(file));
output("\n");
for (int i = 0; i < file->message_type_count(); i++) {
GenerateMessageDefAccessor(file->message_type(i), output);
}
output(
"#ifdef __cplusplus\n"
"} /* extern \"C\" */\n"
"#endif\n"
"\n"
"#include \"upb/port_undef.inc\"\n"
"\n"
"#endif /* $0_UPBDEFS_H_ */\n",
ToPreproc(file->name()));
}
void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
EmitFileWarning(file, output);
output("#include \"upb/def.h\"\n");
output("#include \"$0\"\n", DefHeaderFilename(file->name()));
output("\n");
for (int i = 0; i < file->dependency_count(); i++) {
output("extern upb_def_init $0;\n", DefInitSymbol(file->dependency(i)));
}
std::vector<const protobuf::Descriptor*> file_messages =
SortedMessages(file);
for (auto message : file_messages) {
output("extern const upb_msglayout $0;\n", MessageInit(message));
}
output("\n");
if (!file_messages.empty()) {
output("static const upb_msglayout *layouts[$0] = {\n", file_messages.size());
for (auto message : file_messages) {
output(" &$0,\n", MessageInit(message));
}
output("};\n");
output("\n");
}
protobuf::FileDescriptorProto file_proto;
file->CopyTo(&file_proto);
std::string file_data;
file_proto.SerializeToString(&file_data);
output("static const char descriptor[$0] = {", file_data.size());
// C90 only guarantees that strings can be up to 509 characters, and some
// implementations have limits here (for example, MSVC only allows 64k:
// https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1091.
// So we always emit an array instead of a string.
for (size_t i = 0; i < file_data.size();) {
for (size_t j = 0; j < 25 && i < file_data.size(); ++i, ++j) {
output("'$0', ", absl::CEscape(file_data.substr(i, 1)));
}
output("\n");
}
output("};\n\n");
output("static upb_def_init *deps[$0] = {\n", file->dependency_count() + 1);
for (int i = 0; i < file->dependency_count(); i++) {
output(" &$0,\n", DefInitSymbol(file->dependency(i)));
}
output(" NULL\n");
output("};\n");
output("\n");
output("upb_def_init $0 = {\n", DefInitSymbol(file));
output(" deps,\n");
if (file_messages.empty()) {
output(" NULL,\n");
} else {
output(" layouts,\n");
}
output(" \"$0\",\n", file->name());
output(" UPB_STRVIEW_INIT(descriptor, $0)\n", file_data.size());
output("};\n");
}
class Generator : public protoc::CodeGenerator {
~Generator() override {}
bool Generate(const protobuf::FileDescriptor* file,
const std::string& parameter, protoc::GeneratorContext* context,
std::string* error) const override;
uint64_t GetSupportedFeatures() const override {
return FEATURE_PROTO3_OPTIONAL;
}
};
bool Generator::Generate(const protobuf::FileDescriptor* file,
const std::string& parameter,
protoc::GeneratorContext* context,
std::string* error) const {
std::vector<std::pair<std::string, std::string>> params;
google::protobuf::compiler::ParseGeneratorParameter(parameter, &params);
for (const auto& pair : params) {
*error = "Unknown parameter: " + pair.first;
return false;
}
Output h_def_output(context->Open(DefHeaderFilename(file->name())));
WriteDefHeader(file, h_def_output);
Output c_def_output(context->Open(DefSourceFilename(file->name())));
WriteDefSource(file, c_def_output);
return true;
}
} // namespace
} // namespace upbc
int main(int argc, char** argv) {
std::unique_ptr<google::protobuf::compiler::CodeGenerator> generator(
new upbc::Generator());
return google::protobuf::compiler::PluginMain(argc, argv, generator.get());
}
Loading…
Cancel
Save